Added upstream from http://ftp.icm.edu.pl/pub/loglan/
[loglan.git] / sources / new-s5r4 / svga1.c
1 #include <grx.h>
2 #include <mousex.h>
3 #include "eventque.h"
4
5 int Couleur,Fond,CurX,CurY;
6 int Coul[4],Pal;
7 int MOUSE_PRESENT =0;
8
9 MouseEvent *evt;
10 EventQueue *q;
11 char *ret;
12
13 void mousenit(int mo,int ke)
14 {
15   MouseEventMode(1);
16   MouseInit();
17   evt=(MouseEvent *)malloc(sizeof(MouseEvent));
18   MouseEventEnable(ke,mo);
19   MOUSE_PRESENT=1;
20 }
21
22 void mouseshow(void)
23 {
24  MouseDisplayCursor();
25 }
26
27 void mousehide(void)
28 {
29  MouseEraseCursor();
30 }
31
32 int mouse(v,p,h,l,r,c)
33 unsigned int *v,*p,*h,*l,*r,*c;
34 {
35  EventRecord e;
36  MouseEvent *ev1;
37  short result=0;
38
39  *v=0,*p=0,*h=0,*l=0,*r=0,*c=0;
40
41  ev1=evt;
42  evt=(MouseEvent *)calloc(1,sizeof(MouseEvent));
43  MouseGetEvent(M_EVENT | M_POLL | M_NOPAINT,evt);
44  *p=evt->kbstat; *l=evt->key;
45  *h=evt->x;      *v=evt->y;
46  *r=evt->flags;  *c=evt->buttons;
47  if(ev1->kbstat!=evt->kbstat || ev1->key!=evt->key || ev1->x!=evt->x ||
48     ev1->y!=evt->y || ev1->flags!=evt->flags || ev1->buttons!=evt->buttons)
49   result=1;
50  free(ev1);
51  return(result);
52 }
53
54 void afficheinteger(int x,int y,int valeur,int cf,int ce)
55 {
56  char tst[20];
57
58  sprintf(tst,"%i",valeur);
59  GrTextXY(x,y,tst,ce,cf);
60  CurX+=8*strlen(tst);
61  if(CurX>GrMaxX())
62   {
63   CurX=0;
64   CurY+=14;
65   }
66 }
67
68
69
70 int readcara(int posx,int posy,int col_f,int col_e)
71 {
72  char *t="_\0";
73  int a;
74
75  while(!kbhit())
76   {
77   GrHLine(posx,posx+8,posy+13,col_f);
78   delay(200);
79   GrHLine(posx,posx+8,posy+13,col_e);
80   delay(100);
81   }
82  GrHLine(posx,posx+8,posy+13,col_f);
83  a=getxkey();
84  if(!a)
85   return(getxkey());
86  else
87   return(a);
88 }
89
90
91 void beep(void)
92 {
93  printf("%c\n",7);
94 }
95
96 int affiche_chaine(int x,int y,int lg,char *txtd,char *txtf,int av,int *ll,int col_e,int col_f)
97 {
98  int larg;
99  char cara[2];
100
101
102  if(!av)
103   {
104   for(sprintf(&cara[0],"%c\0",*txtf),larg=0;txtf>=txtd;txtf--) /* affiche arriere */
105    {
106    GrTextXY(x+lg-larg,y,&cara[0],col_e,col_f);
107    larg+=8;
108    sprintf(&cara[0],"%c\0",*(txtf-1));
109    if((lg-larg-8)<0)
110     break;
111    }
112   if(txtf<=txtd)
113    {
114    *ll-=6;
115    return(1);
116    }
117   else
118    return(0);
119   }
120  else
121   {
122   for(sprintf(&cara[0],"%c\0",*txtd),larg=0;txtd<=txtf;txtd++) /* affiche avant */
123    {
124    GrTextXY(x+larg,y,&cara[0],col_e,col_f);
125    larg+=8;
126    sprintf(&cara[0],"%c\0",*(txtd+1));
127    if((lg-larg-8)<0)
128     break;
129    }
130   if(txtd<=txtf)
131    return(0);
132   else
133    return(1);
134   }
135 }
136
137 int gscanfnum(int x,int y,int lg,int min,int max,int deft,int col_f,int col_e,int col_c)
138 {
139
140  char t[10],a;
141  int larg=0,flg=0;
142  char *ptd,*ptc;
143  int i;
144  int cpt=0;
145  short chgt=0;
146  short signe=0;  /* par defaut 0(+) sinon 1(-) */
147
148  ptc=ptd=&t[0];
149  GrFilledBox(x,y,x+lg,y+14,col_f);
150  sprintf(t,"%lu\0",deft);
151  while(*ptc!='\0') ptc++;
152  GrTextXY(x+lg-strlen(t)*8,y,t,col_e,col_f);
153  do
154   {
155   a=readcara(x+lg-8,y,col_f,col_c);
156   switch(a)
157    {
158    case 8:
159     if(ptc>ptd)
160      {
161      ptc--;
162      cpt--;
163      if(flg)
164       larg-=8;
165      *(ptc)='\0';
166      }
167     else
168      {
169      larg=0;
170      ptc=ptd;
171      beep();
172      }
173     break;
174    case 13:
175     if(ptc==ptd)
176      {
177      *ptc='\0';
178      a=11;
179      }
180     break;
181    case 43:
182     signe=0;
183     chgt=1;
184     if(cpt==0)
185      *ptc='\0';
186     break;
187    case 45:
188     signe=1;
189     chgt=1;
190     if(cpt==0)
191      *ptc='\0';
192     break;
193    default:
194     if(cpt<10)
195      {
196      if((a<='9') && (a>='0'))
197       {
198       *(ptc++)=a;
199       cpt++;
200       *ptc='\0';
201       }
202      }
203     else
204      beep();
205     break;
206    }
207   GrFilledBox(x,y,x+lg,y+14,col_f);
208   affiche_chaine(x,y,lg,ptd,ptc,flg,&larg,col_e,col_f);
209   if(chgt)
210    if(signe)
211      GrTextXY(x,y,"-",col_e,col_f);
212    else
213      GrTextXY(x,y,"+",col_e,col_f);
214   }
215  while((a!=13) && (a!=27));
216  if(a==13)
217   {
218   i=atol(t);
219   if(signe)
220    i=-i;
221   if((i<=max) && (i>=min))
222    return(i);
223   else
224    {
225    beep();
226    return(gscanfnum(x,y,lg,min,max,deft,col_f,col_e,col_c));
227    }
228   }
229  else
230   return(gscanfnum(x,y,lg,min,max,deft,col_f,col_e,col_c));
231 }
232
233
234 /******************************************************************************/
235 char *gschar(int x,int y,int lg,int *lgmax,char *defaut,int col_f,int col_e,int col_c)
236 {
237
238  char *ptd,*ptc,a;
239  int larg=0,flg=1;
240
241  ptd=(char *)malloc(*lgmax);
242  ptc=ptd;
243  GrFilledBox(x,y,x+lg*8,y+14,col_f);
244  GrTextXY(x,y,defaut,col_e,col_f);
245  do
246   {
247   a=readcara((larg<(lg-1)*8) ? (x+larg) : (x+(lg-1)*8),y,col_f,col_c);
248   switch(a)
249    {
250    case 8:
251     if(ptc>ptd)
252      {
253      ptc--;
254      if(flg)
255       larg=(larg-8)>0 ? larg-8 : 0;
256      *(ptc)='\0';
257      }
258     else
259      {
260      larg=0;
261      ptc=ptd;
262      beep();
263      }
264     break;
265    case 13:
266     break;
267    default :
268     if(((ptc-ptd)/sizeof(char))<=*lgmax)
269      {
270      *(ptc++)=a;
271      *ptc='\0';
272      if(flg)
273       larg+=8;
274      }
275     else
276      beep();
277    }
278   GrFilledBox(x,y,x+lg*8,y+14,col_f);
279   flg=affiche_chaine(x,y,(lg-1)*8,ptd,ptc,flg,&larg,col_e,col_f);
280   }
281  while((a!=13) && (a!=27));
282  if((a==27) || (ptd==ptc))
283   {
284   ptc=ptd=defaut;
285   while(*ptc!='\0') ptc++;
286   }
287  else
288   *ptc='\0';
289  GrFilledBox(x,y,x+lg*8,y+14,col_f);
290  affiche_chaine(x,y,lg*8,ptd,ptc,1,&larg,col_e,col_f);
291  *lgmax=(ptc>ptd)? ptc-ptd+1 : 0;
292  return(ptd);
293 }