1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/*
ezx81 - X11 ZX81 emulator
Copyright (C) 2003 Ian Cowburn (ianc@noddybox.demon.co.uk)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-------------------------------------------------------------------------
Basic GUI routines
*/
static const char ident[]="$Id$";
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "gui.h"
#include "gfx.h"
#include "util.h"
#include "exit.h"
static const char ident_h[]=EZX81_GUI_H;
/* ---------------------------------------- MACROS
*/
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define WHITE GFXRGB(255,255,255)
#define BLACK GFXRGB(0,0,0)
#define RED GFXRGB(255,100,100)
#define GREY GFXRGB(160,160,160)
#define LOGREY GFXRGB(100,100,100)
#define GREEN GFXRGB(100,255,100)
/* ---------------------------------------- STATICS
*/
/* ---------------------------------------- PRIVATE FUNCTIONS
*/
static void Trim(char *p,size_t len)
{
if (strlen(p)>len)
{
p[len]=0;
p[len-1]='.';
p[len-2]='.';
}
}
static void Centre(const char *p, int y, Uint32 col)
{
GFXPrint((GFX_WIDTH-strlen(p)*8)/2,y,col,"%s",p);
}
static void Box(const char *title, int x, int y, int width, int height)
{
GFXRect(x+1,y+1,
width-2,height-2,
BLACK,TRUE);
GFXRect(x+1,y+1,
width-2,11,
LOGREY,TRUE);
GFXRect(x,y,
width,height,
GREY,FALSE);
Centre(title,y+2,GREEN);
GFXHLine(x,x+width-1,y+11,GREY);
}
static int DoList(const char *title, int no, char * const list[], int *option)
{
static const int max=GFX_HEIGHT/8-8;
SDL_Event *e;
int top;
int cur;
int done;
int f;
if (no==0)
return -1;
top=0;
cur=0;
done=FALSE;
while(!done)
{
Box(title,7,7,GFX_WIDTH-14,GFX_HEIGHT-14);
if (option)
{
Centre("Cursors to move, RETURN to accept",
GFX_HEIGHT-44,WHITE);
Centre("SPACE toggles, I inverts all",
GFX_HEIGHT-36,WHITE);
Centre("S select all, C clear all",
GFX_HEIGHT-28,WHITE);
}
else
Centre("Cursors and RETURN to select",GFX_HEIGHT-28,WHITE);
Centre("ESCAPE to cancel",GFX_HEIGHT-20,WHITE);
for(f=0;f<max;f++)
{
if (f+top<no)
{
Uint32 pen,paper;
if (f+top==cur)
{
pen=WHITE;
paper=RED;
}
else
{
pen=GREY;
paper=BLACK;
}
if (option)
GFXPrintPaper(16,20+f*8,pen,paper,"%c %-34.34s",
option[f+top] ? FONT_TICK:' ',list[f+top]);
else
GFXPrintPaper(16,20+f*8,pen,paper,"%-36.36s",list[f+top]);
}
}
GFXEndFrame(FALSE);
e=GFXWaitKey();
switch(e->key.keysym.sym)
{
case SDLK_RETURN:
done=TRUE;
break;
case SDLK_SPACE:
if (option)
option[cur]=!option[cur];
break;
case SDLK_i:
if (option)
for(f=0;f<no;f++)
option[f]=!option[f];
break;
case SDLK_s:
if (option)
for(f=0;f<no;f++)
option[f]=TRUE;
break;
case SDLK_c:
if (option)
for(f=0;f<no;f++)
option[f]=FALSE;
break;
case SDLK_ESCAPE:
cur=-1;
done=TRUE;
break;
case SDLK_UP:
if (cur>0)
{
cur--;
if (cur<top)
top=cur;
}
break;
case SDLK_DOWN:
if (cur<no-1)
{
cur++;
if (cur>top+max-2)
top=cur-max+2;
}
break;
default:
break;
}
}
return cur;
}
/* ---------------------------------------- EXPORTED INTERFACES
*/
int GUIMessage(GUIBoxType type, const char *title, const char *format,...)
{
char buff[1025];
va_list va;
char *p;
int no;
int f;
char **line;
int width;
int height;
int x,y;
int ret;
ret=FALSE;
va_start(va,format);
vsprintf(buff,format,va);
va_end(va);
p=buff;
no=1;
while(*p)
{
if (*p=='\n')
no++;
p++;
}
line=Malloc(sizeof *line * no);
width=16;
line[0]=strtok(buff,"\n");
Trim(line[0],38);
if (strlen(line[0])>width)
width=strlen(line[0]);
for(f=1;f<no;f++)
{
line[f]=strtok(NULL,"\n");
Trim(line[f],38);
if (strlen(line[f])>width)
width=strlen(line[f]);
}
width=(width*8)+18;
height=(no+5)*10;
if (width>(GFX_WIDTH-10))
width=GFX_WIDTH-10;
if (height>(GFX_HEIGHT-10))
height=GFX_HEIGHT-10;
y=(GFX_HEIGHT-height)/2;
x=(GFX_WIDTH-width)/2;
Box(title,x,y,width,height);
for(f=0;f<no;f++)
Centre(line[f],y+5+10*(f+1),WHITE);
if (type==eMessageBox)
Centre("Press a key",y+height-12,RED);
else
Centre("Press Y/N",y+height-12,RED);
GFXEndFrame(FALSE);
if (type==eYesNoBox)
{
SDL_Event *e;
while(TRUE)
{
e=GFXWaitKey();
if (e->key.keysym.sym==SDLK_y)
{
ret=TRUE;
break;
}
else if (e->key.keysym.sym==SDLK_n)
{
ret=FALSE;
break;
}
}
}
else
GFXWaitKey();
free(line);
return ret;
}
const char *GUIInputString(const char *prompt, const char *orig)
{
static const int y_pos=GFX_HEIGHT-8;
static char buff[41];
size_t len;
int done=FALSE;
unsigned char c;
SDL_Event *e;
buff[0]=0;
strncat(buff,orig,40);
len=strlen(buff);
SDL_EnableUNICODE(1);
while(!done)
{
GFXRect(0,y_pos,GFX_WIDTH,8,BLACK,TRUE);
GFXPrint(0,y_pos,WHITE,"%s %s%c",prompt,buff,FONT_CURSOR);
GFXEndFrame(FALSE);
e=GFXWaitKey();
switch(e->key.keysym.sym)
{
case SDLK_RETURN:
case SDLK_KP_ENTER:
done=TRUE;
break;
case SDLK_ESCAPE:
buff[0]=0;
len=0;
break;
case SDLK_BACKSPACE:
case SDLK_DELETE:
if (len)
buff[--len]=0;
break;
default:
c=(unsigned char)e->key.keysym.unicode;
if (len<40 && isprint(c))
{
buff[len++]=c;
buff[len]=0;
}
break;
}
}
SDL_EnableUNICODE(0);
return buff;
}
int GUIListSelect(const char *title, int no, char * const list[])
{
return DoList(title,no,list,NULL);
}
int GUIListOption(const char *title, int no, char * const list[], int option[])
{
int *o;
int sel;
int f;
if (no==0)
return FALSE;
o=Malloc(no * sizeof *o);
for(f=0;f<no;f++)
o[f]=option[f];
sel=DoList(title,no,list,o);
if (sel!=-1)
for(f=0;f<no;f++)
option[f]=o[f];
free(o);
return sel!=-1;
}
/* END OF FILE */
|