Debian Codegraph @

src/sdl/sf_osfdlg.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * sf_osfdlg.c - SDL interface, file dialogs
3 *
4 * This file is part of Frotz.
5 *
6 * Frotz is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * Frotz is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * Or visit http://www.fsf.org/
20 */
21
22 #include "sf_frotz.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31
32 #define STATIC static
33
34 typedef struct {
35 void *left, *right;
36 char *value;
37 } ENTRY;
38
39 extern SFONT *sf_VGA_SFONT;
40
41 #define FRAMECOLOR 222275
42
43 static char buffer[512];
44 static char lastdir[FILENAME_MAX] = "";
45 static char filename[FILENAME_MAX];
46 static char pattern[64];
47 static zword pushed = 0;
48 static int wentry;
49
50 static zlong *sbuffer = NULL;
51 static int sbpitch; /* in longs */
52 static int ewidth, eheight;
53 static int X, Y, W, H, xdlg, ydlg, wdlg, hdlg;
54
55 #define HTEXT 18
56
57 STATIC void cleanlist(ENTRY * t);
58 STATIC void drawlist(void);
59 STATIC ENTRY *dodir(char *dirname, char *pattern, char *resdir, int size,
60 int *ndirs, int *ntot);
61 static int Numdirs, Numtot, First;
62 static ENTRY *curdir = NULL, *selected;
63
64 STATIC void updatelist(void)
65 {
66 if (curdir)
67 cleanlist(curdir);
68 curdir =
69 dodir(lastdir, pattern, lastdir, FILENAME_MAX, &Numdirs, &Numtot);
70 First = 0;
71 selected = NULL;
72 drawlist();
73 } /* updatelist */
74
75 STATIC void goright(void);
76 STATIC void goleft(void);
77 /* assumes a / at end */
78 STATIC void goup(void)
79 {
80 char *p;
81 if (strlen(lastdir) < 2)
82 return;
83 lastdir[strlen(lastdir) - 1] = 0;
84 p = strrchr(lastdir, '/');
85 if (p) {
86 p[1] = 0;
87 updatelist();
88 } else
89 strcat(lastdir, "/");
90 } /* goup */
91
92 typedef struct {
93 int x, y, w, h; /* internal */
94 zword(*click) (int, int);
95 zlong back;
96 int isbutton;
97 } BAREA;
98
99 #define MAXBAREA 20
100 static BAREA bareas[MAXBAREA];
101 static int nbareas = 0;
102 static SF_textsetting *ts;
103
104 #define BFRAME 2
105 #define SPC 5
106
107 #define WDLG (63*8)
108 #define HDLG 208
109
110 #define HCURSOR 8
111
112 #define O_BLACK 0
113 #define O_GRAY1 0x8a8a8a
114 #define O_GRAY2 0xd6d6d6
115 #define O_GRAY3 0xe2e2e2
116 #define O_WHITE 0xf5f5f5
117
118
119 STATIC void frame_upframe(int x, int y, int w, int h)
120 {
121 zlong v = O_WHITE;
122 sf_chline(x, y, v, w);
123 sf_cvline(x, y, v, --h);
124 v = O_BLACK;
125 sf_chline(x, y + h, v, w--);
126 sf_cvline(x + w--, y, v, h--);
127 x++;
128 y++;
129 v = O_GRAY3;
130 sf_chline(x, y, v, w);
131 sf_cvline(x, y, v, --h);
132 v = O_GRAY1;
133 sf_chline(x, y + h, v, w--);
134 sf_cvline(x + w, y, v, h);
135 } /* frame_upframe */
136
137
138 STATIC void frame_downframe(int x, int y, int w, int h)
139 {
140 zlong v = O_BLACK;
141 sf_chline(x, y, v, w);
142 sf_cvline(x, y, v, --h);
143 v = O_WHITE;
144 sf_chline(x, y + h, v, w--);
145 sf_cvline(x + w--, y, v, h--);
146 x++;
147 y++;
148 v = O_GRAY1;
149 sf_chline(x, y, v, w);
150 sf_cvline(x, y, v, --h);
151 v = O_GRAY3;
152 sf_chline(x, y + h, v, w--);
153 sf_cvline(x + w, y, v, h);
154 } /* frame_downframe */
155
156 /* internal coords */
157 STATIC int addarea(int x, int y, int w, int h, zword(*click) (int, int))
158 {
159 BAREA *a = bareas + nbareas;
160 a->x = x;
161 a->y = y;
162 a->w = w;
163 a->h = h;
164 a->click = click;
165 a->back = O_GRAY2;
166 return nbareas++;
167 } /* addarea */
168
169
170 STATIC void clarea(int n)
171 {
172 BAREA *a = bareas + n;
173 sf_fillrect(a->back, a->x, a->y, a->w, a->h);
174 } /* clarea */
175
176
177 #ifdef USE_UTF8
178 /* Convert UTF-8 encoded char starting at in[idx] to zchar (UCS-2) if
179 * representable in 16 bits or '?' otherwise and return index to next
180 * char of input array. */
181 STATIC int utf8_to_zchar(zchar * out, const char *in, int idx)
182 {
183 zchar ch;
184 int i;
185 if ((in[idx] & 0x80) == 0) {
186 ch = in[idx++];
187 } else if ((in[idx] & 0xe0) == 0xc0) {
188 ch = in[idx++] & 0x1f;
189 if ((in[idx] & 0xc0) != 0x80)
190 goto error;
191 ch = (ch << 6) | (in[idx++] & 0x3f);
192 } else if ((in[idx] & 0xf0) == 0xe0) {
193 ch = in[idx++] & 0xf;
194 for (i = 0; i < 2; i++) {
195 if ((in[idx] & 0xc0) != 0x80)
196 goto error;
197 ch = (ch << 6) | (in[idx++] & 0x3f);
198 }
199 } else {
200 /* Consume all subsequent continuation bytes. */
201 while ((in[++idx] & 0xc0) == 0x80) ;
202 error:
203 ch = '?';
204 }
205 *out = ch;
206 return idx;
207 } /* utf8_to_zchar */
208
209
210 STATIC size_t utf8_len(const char *str)
211 {
212 size_t ret = 0;
213 while (*str) {
214 if ((*str++ & 0xc0) != 0x80)
215 ret++;
216 }
217 return ret;
218 } /* utf8_len */
219 #endif /* USE_UTF8 */
220
221
222 STATIC void writetext(zlong color, const char *s, int x, int y, int w,
223 int center)
224 {
225 int ox, oy, ow, oh;
226 int wtext, htext;
227 os_font_data(0, &htext, &wtext);
228 if (!s)
229 return;
230 if (!s[0])
231 return;
232 sf_getclip(&ox, &oy, &ow, &oh);
233 sf_setclip(x, y, w, htext);
234 if (center) {
235 #ifdef USE_UTF8
236 int wt = wtext * utf8_len(s);
237 #else
238 int wt = wtext * strlen(s);
239 #endif
240 x += (w - wt) / 2;
241 }
242 ts->cx = x;
243 ts->cy = y;
244 ts->fore = color;
245 #ifndef USE_UTF8
246 while (*s)
247 sf_writeglyph(ts->font->
248 getglyph(ts->font, (unsigned char)(*s++), 1));
249 #else
250 while (*s) {
251 zchar ch;
252 s += utf8_to_zchar(&ch, s, 0);
253 sf_writeglyph(ts->font->getglyph(ts->font, ch, 1));
254 }
255 #endif
256 sf_setclip(ox, oy, ow, oh);
257 } /* writetext */
258
259
260 STATIC int addbutton(int x, int y, int w, int h, char *text,
261 zword(*click) (int, int))
262 {
263 int b = addarea(x, y, w, h, click);
264 bareas[b].isbutton = 1;
265 frame_upframe(x - 2, y - 2, w + 4, h + 4);
266 clarea(b);
267 if (text)
268 writetext(0, text, x, y, w, 1);
269 return b;
270 } /* addbutton */
271
272 static int B_up, B_ok, B_cancel;
273 static int A_dir, A_filter, A_entry, A_list;
274
275 #define BUTTW 60
276
277
278 STATIC void showfilename(int pos)
279 {
280 BAREA *a = bareas + A_entry;
281 clarea(A_entry);
282 writetext(0, filename, a->x, a->y, a->w, 0);
283 if (pos >= 0) {
284 int width, height;
285 os_font_data(0, &height, &width);
286 sf_cvline(a->x + width * pos, a->y, O_BLACK, height);
287 }
288 } /* showfilename */
289
290
291 STATIC void clicked(BAREA * a)
292 {
293 frame_downframe(a->x - 2, a->y - 2, a->w + 4, a->h + 4);
294 sf_flushdisplay();
295 sf_sleep(100);
296 frame_upframe(a->x - 2, a->y - 2, a->w + 4, a->h + 4);
297 sf_flushdisplay();
298 } /* clicked */
299
300
301 STATIC zword checkmouse(int i0)
302 {
303 int x = mouse_x - 1, y = mouse_y - 1;
304 int i;
305 for (i = i0; i < nbareas; i++) {
306 BAREA *a = bareas + i;
307 if (x > a->x && x < a->x + a->w && y > a->y && y < a->y + a->h) {
308 if (a->click) {
309 if (a->isbutton)
310 clicked(a);
311 return a->click(x - a->x, y - a->y);
312 } else
313 return 0;
314 }
315 }
316 return 0;
317 } /* checkmouse */
318
319
320 STATIC zword Zup(int UNUSED (x), int UNUSED (y))
321 {
322 goup();
323 return 0;
324 } /* Zup */
325
326
327 STATIC zword Zok(int UNUSED (x), int UNUSED (y))
328 {
329 return ZC_RETURN;
330 } /* Zok */
331
332
333 STATIC zword Zcanc(int UNUSED (x), int UNUSED (y))
334 {
335 return ZC_ESCAPE;
336 } /* Zcanc */
337
338 STATIC zword Zselect(int x, int y);
339 STATIC zword yesnoover(int xc, int yc);
340 STATIC zword Zentry(int x, int y);
341
•342 STATIC zword inputkey(int text)
343 {
344 zword c = sf_read_key(0, false, true, text);
345 if (c == ZC_SINGLE_CLICK) {
346 switch (mouse_button) {
347 case 4:
348 c = ZC_ARROW_LEFT;
349 break;
350 case 5:
351 c = ZC_ARROW_RIGHT;
352 break;
353 case 1:
354 break;
355 default:
356 c = 0;
357 break;
358 }
359 }
360 return c;
361 } /* inputkey */
362
•363 int (*sf_sysdialog)(int existing, const char *def, const char *filt,
364 const char *tit, char **res) = NULL;
365
366
•367 STATIC int myosdialog(int existing, const char *def, const char *filt,
368 const char *tit, char **res, zlong * sbuf, int sbp,
369 int ew, int eh, int isfull)
370 {
371 char *pp;
372 zlong *saved;
373 int y0, y1, y2, x1;
374 zword c = 0;
375 int wtext, htext, buttw;
376
377 /* Allow system-specific dialog if not fullscreen */
378 if (isfull == 0)
379 if (sf_sysdialog)
380 return sf_sysdialog(existing, def, filt, tit, res);
381
382 ts = sf_curtextsetting();
383 if (!ts)
384 return SF_NOTIMP;
385
386 if (!def)
387 def = "";
388 strcpy(filename, def);
389 pp = strrchr(filename, '/');
390 if (pp) {
391 *pp = 0;
392 strcpy(lastdir, filename);
393 strcpy(filename, pp + 1);
394 }
395
396 if (!filt)
397 filt = "*|All files";
398
399 if (!lastdir[0])
400 strcpy(lastdir, "./");
401
402 strcpy(buffer, filt);
403 pp = strchr(buffer, '|');
404 if (pp)
405 *pp = 0;
406 strcpy(pattern, buffer);
407
408 ewidth = ew;
409 eheight = eh;
410 sbuffer = sbuf;
411 sbpitch = sbp;
412
413 wdlg = WDLG;
414 hdlg = HDLG;
415
416 nbareas = 0;
417
418 #ifndef USE_UTF8
419 htext = HTEXT;
420 buttw = BUTTW;
421 #else
422 os_font_data(FIXED_WIDTH_FONT, &htext, &wtext);
423 buttw = 6 * wtext + 12;
424 #endif
425
426 W = WDLG + 4 * BFRAME + 2 * SPC;
427 H = HDLG + 4 * BFRAME + 6 * SPC + 6 * BFRAME + 3 * (htext + 2) +
428 HCURSOR + htext;
429
430 if (W > ew)
431 return SF_NOTIMP;
432 if (H > eh)
433 return SF_NOTIMP;
434
435 X = (ew - W) / 2;
436 Y = (eh - H) / 2;
437
438 /* Internal!! */
439 xdlg = X + SPC + 2 * BFRAME;
440 ydlg = Y + 2 * SPC + 4 * BFRAME + htext + htext;
441
442 wentry = wdlg - buttw - SPC - 2 * BFRAME;
443
444 saved = sf_savearea(X, Y, W, H);
445 if (!saved)
446 return SF_NOTIMP;
447
448 sf_pushtextsettings();
449 #ifndef USE_UTF8
450 ts->font = sf_VGA_SFONT;
451 #else
452 os_set_font(FIXED_WIDTH_FONT);
453 #endif
454 ts->style = 0;
455 ts->oh = 0;
456 ts->fore = 0;
457 ts->backTransparent = 1;
458
459 sf_fillrect(O_GRAY2, X, Y, W, H);
460 sf_rect(FRAMECOLOR, X, Y, W, H);
461 sf_rect(FRAMECOLOR, X + 1, Y + 1, W - 2, H - 2);
462 sf_fillrect(FRAMECOLOR, X, Y + 2, W, htext);
463 if (tit)
464 writetext(O_WHITE, tit, X + 2 + SPC, Y + 2, W - 4, 0);
465 A_list = addarea(xdlg, ydlg, wdlg, hdlg, Zselect);
466 bareas[A_list].back = O_WHITE;
467 clarea(A_list);
468 frame_downframe(xdlg - 2, ydlg - 2, wdlg + 4, hdlg + 4);
469
470 y0 = Y + SPC + 2 * BFRAME + htext;
471 y2 = Y + H - SPC - 2 * BFRAME - htext;
472 y1 = y2 - SPC - htext - 2 * BFRAME;
473 x1 = xdlg + wentry + 2 * BFRAME + SPC;
474
475 A_dir = addarea(xdlg, y0, wentry, htext, NULL);
476 A_entry = addarea(xdlg, y1, wentry, htext, Zentry);
477 bareas[A_entry].back = O_WHITE;
478 clarea(A_entry);
479 frame_downframe(xdlg - 2, y1 - 2, wentry + 4, htext + 4);
480 B_up = addbutton(x1, y0, buttw, htext, "^up^", Zup);
481 A_filter = addarea(xdlg, y2, wentry, htext, NULL);
482 strcpy(buffer, "Filter: ");
483 strcat(buffer, filt);
484 writetext(0, buffer, xdlg, y2, wentry, 0);
485 B_cancel = addbutton(x1, y2, buttw, htext, "Cancel", Zcanc);
486 B_ok = addbutton(x1, y1, buttw, htext, "OK", Zok);
487
488 showfilename(-1);
489 updatelist();
490
491 for (;;) {
492 if (pushed) {
493 c = pushed;
494 pushed = 0;
495 } else
496 c = inputkey(false);
497 if (c == ZC_SINGLE_CLICK)
498 c = checkmouse(0);
499 if (c == VK_INS)
500 c = Zentry(0, -1);
501 if (c == ZC_ARROW_LEFT)
502 goleft();
503 if (c == ZC_ARROW_RIGHT)
504 goright();
505 if (c == ZC_ESCAPE)
506 break;
507 if (c == ZC_RETURN) {
508 strcpy(buffer, lastdir);
509 strcat(buffer, filename);
510 *res = buffer;
511 if ((existing == 0) && (access(buffer, F_OK) == 0))
512 c = yesnoover(xdlg + wdlg / 2, ydlg + hdlg / 2);
513 if (c == ZC_RETURN)
514 break;
515 }
516 }
517
518 sf_poptextsettings();
519
520 cleanlist(curdir);
521 curdir = NULL;
522
523 sf_restoreareaandfree(saved);
524
525 if (c == ZC_ESCAPE)
526 return -1;
527
528 if (c == ZC_RETURN) {
529 strcpy(buffer, lastdir);
530 strcat(buffer, filename);
531 *res = buffer;
532 return 0;
533 }
534
535 return SF_NOTIMP;
536 } /* myosdialog */
537
538
539 void sf_setdialog(void)
540 {
541 sf_osdialog = myosdialog;
542 } /* sf_setdialog */
543
544 /*********************************/
545
546 #include <stdlib.h>
547 #include <stdarg.h>
548 #include <stdio.h>
549 #include <string.h>
550 #ifdef WIN32
551 #define strcasecmp stricmp
552 #else
553 #include <strings.h>
554 #endif
555 #include <unistd.h>
556 #include <dirent.h>
557 #include <sys/stat.h>
558
559
560 /* simplified fnmatch - only allows a single * at beginning */
561 STATIC int myfnmatch(const char *pattern, const char *p, int UNUSED (dummy))
562 {
563 int lpat, lp;
564 if (!pattern)
565 return -1;
566 if (!p)
567 return -1;
568 if (pattern[0] != '*')
569 return strcmp(pattern, p);
570 lpat = strlen(pattern);
571 if (lpat == 1)
572 return 0; /* * matches anything */
573 lpat--;
574 pattern++;
575 lp = strlen(p);
576 if (lp < lpat)
577 return 1; /* too short */
578 return strcmp(pattern, p + lp - lpat);
579 } /* myfnmatch */
580
581
582 STATIC void cleanlist(ENTRY * t)
583 {
584 while (t) {
585 ENTRY *n = t->right;
586 if (t->value)
587 free(t->value);
588 free(t);
589 t = n;
590 }
591 } /* cleanlist */
592
593
594 STATIC ENTRY *newentry(char *s)
595 {
596 ENTRY *r = calloc(1, sizeof(ENTRY));
597
598 if (r) {
599 r->value = strdup(s);
600 if (!r->value) {
601 free(r);
602 return NULL;
603 }
604 }
605 return r;
606 } /* newentry */
607
608
609 STATIC void addentry(char *s, ENTRY ** ae)
610 {
611 ENTRY *t = *ae;
612 if (!t) {
613 *ae = newentry(s);
614 return;
615 }
616 for (;;) {
617 int k = strcasecmp(s, t->value);
618 if (!k)
619 return;
620 if (k > 0) {
621 if (t->right)
622 t = t->right;
623 else {
624 t->right = newentry(s);
625 return;
626 }
627 } else {
628 if (t->left)
629 t = t->left;
630 else {
631 t->left = newentry(s);
632 return;
633 }
634 }
635 }
636 } /* addentry */
637
638
639 STATIC char *resolvedir(char *dir, char *res, int size)
640 {
641 char cwd[FILENAME_MAX], *p;
642 int i;
643 if (!getcwd(cwd, FILENAME_MAX))
644 return NULL;
645 if (chdir(dir))
646 return NULL;
647 p = getcwd(res, size);
648 for (i = 0; p[i]; i++)
649 if (p[i] == '\\')
650 p[i] = '/';
651 chdir(cwd);
652 if (p) {
653 int n = strlen(p);
654 if (n)
655 if (p[n - 1] != '/') {
656 p[n] = '/';
657 p[n + 1] = 0;
658 }
659 }
660 return p;
661 } /* resolvedir */
662
663
664 static void exhaust(ENTRY * e, ENTRY ** resp, int *n)
665 {
666 if (!e)
667 return;
668 exhaust(e->left, resp, n);
669 e->left = *resp;
670 *resp = e;
671 (*n)++;
672 exhaust(e->right, resp, n);
673 } /* exhaust */
674
675
676 STATIC ENTRY *dodir(char *dirname, char *pattern, char *resdir, int size,
677 int *ndirs, int *ntot)
678 {
679 DIR *dir;
680 ENTRY *dirs = NULL;
681 ENTRY *files = NULL, *res = NULL;
682 struct dirent *d;
683 char *p, *resdend;
684 struct stat fst;
685 int n;
686
687 if (!resolvedir(dirname, resdir, size))
688 return NULL;
689 resdend = resdir + strlen(resdir);
690
691 #ifdef WIN32
692 n = strlen(resdir);
693 if (n > 2 && (resdir[n - 2] != ':'))
694 resdir[n - 1] = 0;
695 dir = opendir(resdir);
696 resdir[n - 1] = '/';
697 #else
698 dir = opendir(resdir);
699 #endif
700 if (!dir)
701 return NULL;
702
703 for (;;) {
704 d = readdir(dir);
705 if (!d)
706 break;
707 p = d->d_name;
708 if (strcmp(p, ".") == 0)
709 continue;
710 if (strcmp(p, "..") == 0)
711 continue;
712 strcpy(resdend, p);
713 if (stat(resdir, &fst))
714 continue;
715 if (S_ISDIR(fst.st_mode))
716 addentry(p, &dirs);
717 else {
718 if (myfnmatch(pattern, p, 0) == 0)
719 addentry(p, &files);
720 }
721 }
722
723 closedir(dir);
724 *resdend = 0;
725
726 n = 0;
727 exhaust(dirs, &res, &n);
728 *ndirs = n;
729 exhaust(files, &res, &n);
730 *ntot = n;
731
732 if (res)
733 while (res->left) {
734 ((ENTRY *) (res->left))->right = res;
735 res = res->left;
736 }
737
738 return res;
739 } /* dodir */
740
741
742 #ifdef USE_UTF8
743 /* Convert character count into index in UTF-8 encoded string.
744 * Works by skipping over continuation bytes.
745 */
746 STATIC int utf8_char_pos(char *s, int pos)
747 {
748 int cpos;
749 int idx = 0;
750 for (cpos = 0; s[cpos] && idx < pos; cpos++) {
751 if ((s[cpos + 1] & 0xc0) != 0x80)
752 idx++;
753 }
754 return cpos;
755 } /* utf8_char_pos */
756 #endif
757
758
759 /*********************************************
760 * white, black, gray, yellow
761 */
762 static zlong bcolors[4] = { 0xfcfcfc, 0, 0xa0a0a0, 0xa0d0e0 };
763
764 static unsigned char folderbmp[] = {
765 0, 0, 0, 0, 0, 0, 0, 0,
766 0, 0, 0, 0, 0, 0, 0, 0,
767 0, 0, 0, 0, 0, 0, 0, 0,
768 0, 0, 0, 0, 0, 0, 0, 0,
769 0, 0, 1, 1, 1, 1, 1, 1,
770 0, 0, 0, 0, 0, 0, 0, 0,
771 0, 1, 3, 3, 3, 3, 3, 3,
772 1, 1, 1, 1, 1, 1, 0, 0,
773 0, 1, 3, 3, 3, 3, 3, 3,
774 3, 3, 3, 3, 3, 3, 1, 0,
775 0, 1, 3, 3, 3, 3, 3, 3,
776 3, 1, 1, 1, 1, 1, 1, 0,
777 0, 1, 3, 3, 1, 1, 1, 1,
778 1, 3, 3, 3, 3, 3, 1, 0,
779 0, 1, 3, 1, 3, 3, 3, 3,
780 3, 3, 3, 3, 3, 3, 1, 0,
781 0, 1, 3, 1, 3, 3, 3, 3,
782 3, 3, 3, 3, 3, 3, 1, 0,
783 0, 1, 3, 1, 3, 3, 3, 3,
784 3, 3, 3, 3, 3, 3, 1, 0,
785 0, 1, 3, 1, 3, 3, 3, 3,
786 3, 3, 3, 3, 3, 3, 1, 0,
787 0, 1, 3, 1, 3, 3, 3, 3,
788 3, 3, 3, 3, 3, 3, 1, 0,
789 0, 1, 3, 1, 3, 3, 3, 3,
790 3, 3, 3, 3, 3, 3, 1, 0,
791 0, 0, 1, 1, 1, 1, 1, 1,
792 1, 1, 1, 1, 1, 1, 0, 0,
793 0, 0, 0, 0, 0, 0, 0, 0,
794 0, 0, 0, 0, 0, 0, 0, 0,
795 0, 0, 0, 0, 0, 0, 0, 0,
796 0, 0, 0, 0, 0, 0, 0, 0
797 };
798
799 static unsigned char docbmp[] = {
800 0, 0, 0, 0, 0, 0, 0, 0,
801 0, 0, 0, 0, 0, 0, 0, 0,
802 0, 0, 0, 0, 0, 0, 0, 0,
803 0, 0, 0, 0, 0, 0, 0, 0,
804 0, 0, 1, 1, 1, 1, 1, 1,
805 1, 1, 1, 1, 1, 1, 0, 0,
806 0, 1, 0, 0, 0, 0, 0, 0,
807 0, 0, 0, 0, 0, 0, 1, 0,
808 0, 1, 0, 2, 2, 2, 2, 2,
809 2, 2, 2, 2, 2, 0, 1, 0,
810 0, 1, 0, 0, 0, 0, 0, 0,
811 0, 0, 0, 0, 0, 0, 1, 0,
812 0, 1, 0, 2, 2, 2, 2, 2,
813 2, 2, 2, 2, 2, 0, 1, 0,
814 0, 1, 0, 0, 0, 0, 0, 0,
815 0, 0, 0, 0, 0, 0, 1, 0,
816 0, 1, 0, 2, 2, 2, 2, 2,
817 2, 2, 2, 2, 2, 0, 1, 0,
818 0, 1, 0, 0, 0, 0, 0, 0,
819 0, 0, 0, 0, 0, 0, 1, 0,
820 0, 1, 0, 2, 2, 2, 2, 2,
821 2, 2, 2, 2, 2, 0, 1, 0,
822 0, 1, 0, 0, 0, 0, 0, 0,
823 0, 0, 0, 0, 0, 0, 1, 0,
824 0, 1, 0, 0, 0, 0, 0, 0,
825 0, 0, 0, 0, 0, 0, 1, 0,
826 0, 0, 1, 1, 1, 1, 1, 1,
827 1, 1, 1, 1, 1, 1, 0, 0,
828 0, 0, 0, 0, 0, 0, 0, 0,
829 0, 0, 0, 0, 0, 0, 0, 0,
830 0, 0, 0, 0, 0, 0, 0, 0,
831 0, 0, 0, 0, 0, 0, 0, 0
832 };
833
834 /******************************/
835
836 STATIC void drawit(int x, int y, ENTRY * e, int w, int issub)
837 {
838 int i, j, n, color;
839 unsigned char *bmp;
840 char *s = e->value;
841 int width, height;
842 os_font_data(0, &height, &width);
843 if (height < 16)
844 height = 16;
845 bmp = (issub ? folderbmp : docbmp);
846 for (i = 0; i < 16; i++) {
847 for (j = 0; j < 16; j++) {
848 sf_wpixel(x + j, y + i + height / 2 - 8,
849 bcolors[*bmp++]);
850 }
851 }
852 x += 17;
853 w -= 17;
854 n = w / width;
855 if (n < 1)
856 return;
857 if (strlen(s) > n) {
858 strcpy(buffer, s);
859 #ifndef USE_UTF8
860 buffer[n] = 0;
861 buffer[n - 1] = '>';
862 s = buffer;
863 #else
864 i = utf8_len(buffer);
865 if (i > n - 1) {
866 j = utf8_char_pos(buffer, n - 1);
867 buffer[j + 1] = 0;
868 buffer[j] = '>';
869 s = buffer;
870 }
871 #endif
872 }
873 if (e == selected) {
874 color = O_WHITE;
875 sf_fillrect(0, x, y, w, 16);
876 } else
877 color = O_BLACK;
878 writetext(color, s, x, y, w, 0);
879 } /* drawit */
880
881
882 static int Nrows, Ncols, Ewid, Fh;
883
884 STATIC void drawnames(int x, int y, int w, int h, ENTRY * files, int first,
885 int nsub, int ntot, int ewid)
886 {
887 int i;
888
889 #ifdef USE_UTF8
890 int width;
891 os_font_data(0, &Fh, &width);
892 if (Fh < 16)
893 Fh = 16;
894 #else
895 Fh = 16;
896 #endif
897 Ewid = ewid;
898 Ncols = w / ewid;
899 Nrows = h / Fh;
900
901 sf_fillrect(O_WHITE, x, y, w, h);
902 if (!files)
903 return;
904 if (first < 0)
905 return;
906 if (nsub > ntot)
907 nsub = ntot;
908 while (first > 0) {
909 files = files->right;
910 if (!files)
911 return;
912 nsub--;
913 ntot--;
914 first--;
915 }
916 if (ntot <= 0)
917 return;
918 if (Ncols < 1)
919 return;
920 if (Nrows < 1)
921 return;
922 if (Nrows * Ncols < ntot)
923 ntot = Nrows * Ncols;
924 for (i = 0; i < ntot; i++) {
925 drawit(x + ewid * (i / Nrows), y + Fh * (i % Nrows), files,
926 ewid, i < nsub);
927 files = files->right;
928 }
929 } /* drawnames */
930
931
932 STATIC void drawlist(void)
933 {
934 BAREA *a = bareas + A_list, *b = bareas + A_dir;
935
936 clarea(A_dir);
937 writetext(0, lastdir, b->x, b->y, b->w, 0);
938 drawnames(a->x, a->y, a->w, a->h, curdir, First, Numdirs, Numtot,
939 21 * 8);
940
941 } /* drawlist */
942
943
944 STATIC void goright(void)
945 {
946 if (First + Nrows * Ncols > Numtot)
947 return;
948 First += Nrows;
949 drawlist();
950 } /* goright */
951
952
953 STATIC void goleft(void)
954 {
955 if (!First)
956 return;
957 First -= Nrows;
958 drawlist();
959 } /* goleft */
960
961
962 STATIC ENTRY *filesat(int n)
963 {
964 ENTRY *e = curdir;
965 while (n--) {
966 if (e)
967 e = e->right;
968 }
969 return e;
970 } /* filesat */
971
972
973 STATIC zword Zselect(int x, int y)
974 {
975 int n;
976 x /= Ewid;
977 y /= Fh;
978 n = First + y + x * Nrows;
979 if (n >= Numtot) {
980 if (selected) {
981 selected = NULL;
982 drawlist();
983 }
984 return 0;
985 }
986 if (n < Numdirs) {
987 ENTRY *e = filesat(n);
988 if (!e)
989 return 0;
990 strcat(lastdir, e->value);
991 updatelist();
992 return 0;
993 }
994 selected = curdir;
995 while (n--)
996 selected = selected->right;
997 strcpy(filename, selected->value);
998 showfilename(-1);
999 drawlist();
1000 return 0;
1001 } /* Zselect */
1002
1003
1004 extern void sf_videodata(zlong ** sb, int *sp, int *ew, int *eh);
1005 zword sf_yesnooverlay(int xc, int yc, char *t, int saverest)
1006 {
1007 zword c = ZC_RETURN;
1008 int nsav = nbareas;
1009 zlong *saved = NULL;
1010 int hx = BUTTW + 3 * SPC, hy = HTEXT + 2 * SPC, heff;
1011
1012 heff = 8 * strlen(t);
1013 if (heff > 2 * hx)
1014 hx = (heff + 3) / 2;
1015 if (saverest) {
1016 ts = sf_curtextsetting();
1017 if (!ts)
1018 return ZC_ESCAPE;
1019 saved =
1020 sf_savearea(xc - hx - 2, yc - hy - 2, 2 * hx + 4,
1021 2 * hy + 4);
1022 if (!saved)
1023 return ZC_ESCAPE;
1024 sf_pushtextsettings();
1025 ts->font = sf_VGA_SFONT;
1026 ts->style = 0;
1027 ts->oh = 0;
1028 ts->fore = 0;
1029 ts->backTransparent = 1;
1030 sf_videodata(&sbuffer, &sbpitch, &ewidth, &eheight);
1031 }
1032
1033 sf_fillrect(FRAMECOLOR, xc - hx - 2, yc - hy - 2, 2 * hx + 4,
1034 2 * hy + 4);
1035 sf_fillrect(O_WHITE, xc - hx, yc - hy, 2 * hx, 2 * hy);
1036 writetext(O_BLACK, t, xc - hx, yc - SPC - HTEXT, 2 * hx, 1);
1037 addbutton(xc - SPC - BUTTW, yc + SPC, BUTTW, HTEXT, "Yes", Zok);
1038 addbutton(xc + SPC, yc + SPC, BUTTW, HTEXT, "No", Zcanc);
1039 for (;;) {
1040 c = inputkey(false);
1041 if (c == 'n' || c == 'N')
1042 c = ZC_ESCAPE;
1043 if (c == 'y' || c == 'Y')
1044 c = ZC_RETURN;
1045 if (c == ZC_SINGLE_CLICK)
1046 c = checkmouse(nsav);
1047 if (c == ZC_ESCAPE)
1048 break;
1049 if (c == ZC_RETURN)
1050 break;
1051 }
1052
1053 if (saved) {
1054 sf_restoreareaandfree(saved);
1055 sf_poptextsettings();
1056 }
1057
1058 nbareas = nsav;
1059 return c;
1060 } /* sf_yesnooverlay */
1061
1062
1063 STATIC zword yesnoover(int xc, int yc)
1064 {
1065 zword c;
1066
1067 c = sf_yesnooverlay(xc, yc, "Overwrite file?", 0);
1068
1069 drawlist();
1070 return c;
1071 } /* yesnoover */
1072
1073
1074 /* this is needed for overlapping source and dest in Zentry
1075 * (lib does not guarantee correct behaviour in that case)
1076 */
1077 static void mystrcpy(char *d, const char *s)
1078 {
1079 while ((*d++ = *s++)) ;
1080 } /* mystrcpy */
1081
1082
1083 STATIC zword Zentry(int x, int y)
1084 {
1085 static int pos = 10000;
1086 int i, n, nmax;
1087 zword c;
1088 int cpos, clen, nchars;
1089 int width, height;
1090 os_font_data(0, &height, &width);
1091 nmax = wentry / width;
1092 if (nmax >= FILENAME_MAX)
1093 nmax = FILENAME_MAX - 1;
1094 n = strlen(filename);
1095 #ifndef USE_UTF8
1096 if (n > nmax) {
1097 n = nmax;
1098 filename[n] = 0;
1099 }
1100 nchars = n;
1101 #else
1102 nchars = utf8_len(filename);
1103 if (nchars > nmax) {
1104 nchars = nmax;
1105 n = utf8_char_pos(filename, nchars);
1106 filename[n] = 0;
1107 }
1108 #endif
1109
1110 if (y >= 0) {
1111 pos = x / (width / 2) - 1;
1112 if (pos < 0)
1113 pos = 0;
1114 pos /= 2;
1115 }
1116 if (pos > nchars)
1117 pos = nchars;
1118 showfilename(pos);
1119 for (;;) {
1120 c = inputkey(true);
1121 if (c == ZC_SINGLE_CLICK) {
1122 pushed = c;
1123 c = 0;
1124 break;
1125 }
1126 if (c == ZC_ESCAPE || c == VK_INS) {
1127 c = 0;
1128 break;
1129 }
1130 if (c == ZC_RETURN)
1131 break;
1132 if (c == ZC_ARROW_LEFT) {
1133 if (pos) {
1134 pos--;
1135 showfilename(pos);
1136 }
1137 continue;
1138 }
1139 if (c == ZC_ARROW_RIGHT) {
1140 if (pos < nchars) {
1141 pos++;
1142 showfilename(pos);
1143 }
1144 continue;
1145 }
1146 if (c == ZC_BACKSPACE) {
1147 if (pos) {
1148 clen = 1;
1149 #ifndef USE_UTF8
1150 cpos = pos;
1151 #else
1152 cpos = utf8_char_pos(filename, pos);
1153 while (cpos > clen
1154 && (filename[cpos - clen] & 0xc0) ==
1155 0x80)
1156 clen++;
1157 #endif
1158 /* needs mystrcpy() because
1159 * overlapping src-dst */
1160 if (cpos < n)
1161 mystrcpy(filename + cpos - clen,
1162 filename + cpos);
1163 n -= clen;
1164 nchars--;
1165 filename[n] = 0;
1166 pos--;
1167 showfilename(pos);
1168 }
1169 continue;
1170 }
1171 #ifndef USE_UTF8
1172 if ((c >= 32 && c < 127) || (c >= 160 && c < 256))
1173 #else
1174 if ((c >= 32 && c < 127) || (c >= 160))
1175 #endif
1176 {
1177 #ifndef USE_UTF8
1178 cpos = pos;
1179 clen = 1;
1180 #else
1181 cpos = utf8_char_pos(filename, pos);
1182 if (c < 0x80)
1183 clen = 1;
1184 else if (c < 0x800)
1185 clen = 2;
1186 else
1187 clen = 3;
1188 #endif
1189 if (nchars >= nmax)
1190 continue;
1191 if (n > cpos)
1192 for (i = n - 1; i >= cpos; i--)
1193 filename[i + clen] = filename[i];
1194 #ifndef USE_UTF8
1195 filename[cpos] = c;
1196 #else
1197 if (c > 0x7ff) {
1198 filename[cpos] = 0xe0 | ((c >> 12) & 0xf);
1199 filename[cpos + 1] = 0x80 | ((c >> 6) & 0x3f);
1200 filename[cpos + 2] = 0x80 | (c & 0x3f);
1201 } else if (c > 0x7f) {
1202 filename[cpos] = 0xc0 | ((c >> 6) & 0x1f);
1203 filename[cpos + 1] = 0x80 | (c & 0x3f);
1204 } else {
1205 filename[cpos] = c;
1206 }
1207 #endif
1208 n += clen;
1209 nchars++;
1210 filename[n] = 0;
1211 pos++;
1212 showfilename(pos);
1213 }
1214 }
1215 showfilename(-1);
1216 return c;
1217 } /* Zentry */