Debian Codegraph @

lib/help.c

raw symbols SCIP
1 /* The Ace of Penguins - help.c
2 Copyright (C) 1998, 2001 DJ Delorie
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/keysym.h>
27
28 #include "table.h"
29 #include "cards.h"
30 #include "xwin.h"
31
32 extern Display *display;
33 extern Window window;
34 extern GC gc;
35
36 static int help_foreground, help_beyondcolor;
37
38 #define STYLE_NONE 0x00
39 #define STYLE_B 0x01
40 #define STYLE_I 0x02
41 #define STYLE_TT 0x04
42 #define STYLE_BIG 0x08
43 #define STYLE_BITS 0x0f
44 #define FLOAT_LEFT 0x01
45 #define FLOAT_RIGHT 0x02
46 #define ALIGN_CENTER 0x03
47 #define PTR_IMG 0x10
48 #define BLK_BR 0x20
49 #define BLK_P 0x40
50 #define HEADER 0x80
51
52 #define MENU_FONT 0x10
53
54 static XFontStruct *fonts[17];
55 static int thin_space[17];
56
57 static struct {
58 char *tag;
59 int taglen;
60 int bits_set;
61 int bits_reset;
62 } tags[] = {
63 { "<b>", 0, STYLE_B, 0 },
64 { "</b>", 0, 0, STYLE_B },
65 { "<i>", 0, STYLE_I, 0 },
66 { "</i>", 0, 0, STYLE_I },
67 { "<tt>", 0, STYLE_TT, 0 },
68 { "</tt>", 0, 0, STYLE_TT },
69 { "<big>", 0, STYLE_BIG, 0 },
70 { "</big>", 0, 0, STYLE_BIG },
71 { "<h1>", 0, HEADER, STYLE_BITS },
72 { "</h1>", 0, BLK_P, STYLE_BITS },
73 { "<h2>", 0, STYLE_BIG|STYLE_B|BLK_P, 0 },
74 { "</h2>", 0, BLK_P, STYLE_BITS },
75 { "<p>", 0, BLK_P, STYLE_BITS },
76 { "</p>", 0, BLK_P, STYLE_BITS },
77 { "<br>", 0, BLK_BR, 0 },
78 { "<img src=", 0, PTR_IMG, 0 },
79 { "<img align=center src=", 0, PTR_IMG|ALIGN_CENTER, 0 },
80 { "<img align=left src=", 0, PTR_IMG|FLOAT_LEFT, 0 },
81 { "<img align=right src=", 0, PTR_IMG|FLOAT_RIGHT, 0 },
82 };
83 #define NTAGS (sizeof(tags)/sizeof(tags[0]))
84
85 typedef struct Word {
86 short x, y;
87 char a, d, lb;
88 unsigned char flags;
89 short rb, width;
90 char *ptr;
91 } Word;
92
93 static Word *words=0;
94 static int nwords=0;
95 static char *file=0;
96
97 static int *menus=0;
98 static int num_menus;
99 static int cur_page = 0;
100 static int menu_height;
101
102 static int vscroll = 0;
103 static int max_vscroll = 0;
104 static int cur_menu = 0;
105
106 static void
107 show_page (int n, int m)
108 {
109 int i, y;
110 cur_page = n;
111 cur_menu = m;
112 vscroll = 0;
113 for (i=cur_page; i<nwords; i++)
114 {
115 Word *w = words+i;
116 if (w->flags & HEADER)
117 break;
118 y = w->y+w->d;
119 if (max_vscroll < y)
120 max_vscroll = y;
121 }
122 max_vscroll -= (table_height - menu_height);
123 if (max_vscroll < 0)
124 max_vscroll = 0;
125 invalidate(0, 0, table_width, table_height);
126 }
127
128 static void
129 help_init()
130 {
131 int i;
132 int bigpoints, smallpoints;
133 int use_helvetica;
134
135 if (table_type != TABLE_COLOR)
136 {
137 help_background = pixel_for(255, 255, 255);
138 help_foreground = pixel_for(0, 0, 0);
139 help_beyondcolor = pixel_for(255, 255, 255);
140 }
141 else
142 {
143 help_background = table_background;
144 help_foreground = pixel_for(255, 255, 255);
145 help_beyondcolor = pixel_for(0, 0, 0);
146 }
147 if (table_width < 300)
148 {
149 bigpoints = 120;
150 smallpoints = 80;
151 use_helvetica = 1;
152 }
153 else
154 {
155 bigpoints = 180;
156 smallpoints = 100;
157 use_helvetica = 0;
158 }
159 for (i=0; i<16; i++)
160 {
161 char name[100];
162 sprintf(name, "*-%s-%s-%s-*--*-%d-*-*-*-*-*-*",
163 i & STYLE_TT ? "courier" : use_helvetica ? "helvetica" : "times",
164 i & STYLE_B ? "bold" : "medium",
165 i & STYLE_I ? ((i & STYLE_TT || use_helvetica) ? "o" : "i") : "r",
166 i & STYLE_BIG ? bigpoints : smallpoints);
167 fonts[i] = XLoadQueryFont(display, name);
168 }
169 fonts[MENU_FONT] =
170 XLoadQueryFont(display, "*-helvetica-medium-r-normal--*-100-*-*-*-*-*-*");
171 for (i=0; i<17; i++)
172 {
173 if (!fonts[i])
174 fonts[i] = XLoadQueryFont(display, i & STYLE_TT ? "fixed" : "variable");
175 thin_space[i] = XTextWidth(fonts[i], " ", 1);
176 }
177 for (i=0; i<NTAGS; i++)
178 tags[i].taglen = strlen(tags[i].tag);
179
180 menu_height = fonts[MENU_FONT]->ascent + fonts[MENU_FONT]->descent
181 + 2*thin_space[MENU_FONT];
182 }
183
184 static int mx = 0;
185
186 static void
187 my_help_redraw()
188 {
189 int i, l, r;
190 int ts = thin_space[MENU_FONT];
191 int y = menu_height;
192 int saw_cur_menu = 0;
193 Word *w;
194
195 mx = 0;
196 w = words + menus[cur_menu];
197 i = w->x + w->width + ts;
198 if (i > table_width - 20)
199 mx = i - table_width + ts/2 + 20;
200
201 XSetForeground(display, gc, help_foreground);
202 XSetFont(display, gc, fonts[MENU_FONT]->fid);
203 for (i=0; i<num_menus; i++)
204 {
205 w = words+menus[i];
206 l = w->x-ts-mx;
207 r = w->x+w->width+ts-mx;
208 XDrawString(display, window, gc, w->x-mx, w->y, w->ptr, strlen(w->ptr));
209 XDrawLine(display, window, gc, l, y-2, l, 5);
210 XDrawLine(display, window, gc, r, y-2, r, 5);
211 XDrawLine(display, window, gc, l+4, 1, r-4, 1);
212 XDrawLine(display, window, gc, l+1, 4, l+1, 3);
213 XDrawLine(display, window, gc, r-1, 4, r-1, 3);
214 XDrawLine(display, window, gc, l+2, 2, l+3, 2);
215 XDrawLine(display, window, gc, r-2, 2, r-3, 2);
216 XDrawPoint(display, window, gc, l-1, y-1);
217 if (menus[i] == cur_page-1)
218 {
219 XDrawLine(display, window, gc, -2, y, l-2, y);
220 XDrawLine(display, window, gc, r+2, y, table_width+2, y);
221 saw_cur_menu = 1;
222 }
223 }
224 XDrawPoint(display, window, gc, r+1, y-1);
225 if (!saw_cur_menu)
226 XDrawLine(display, window, gc, 0, y, table_width, y);
227
228 XSetForeground(display, gc, help_beyondcolor);
229 XFillRectangle(display, window, gc, r+2, 0, table_width-r-2, y);
230 XDrawLine(display, window, gc, 0, 0, table_width, 0);
231 XDrawLine(display, window, gc, 0, 0, 0, y-1);
232 XDrawLine(display, window, gc, 1, 0, 1, y-2);
233 XDrawLine(display, window, gc, 2, 0, 2, 4);
234 XDrawLine(display, window, gc, 3, 0, 3, 2);
235 XDrawLine(display, window, gc, 4, 1, 5, 1);
236 XDrawPoint(display, window, gc, 3, 0);
237 for (i=0; i<num_menus; i++)
238 {
239 w = words+menus[i];
240 r = w->x+w->width+ts+1-mx;
241 XDrawLine(display, window, gc, r, 5, r, y-2);
242 XDrawLine(display, window, gc, r-1, 4, r+1, 4);
243 XDrawLine(display, window, gc, r-1, 3, r+1, 3);
244 XDrawLine(display, window, gc, r-2, 2, r+2, 2);
245 XDrawLine(display, window, gc, r-4, 1, r+4, 1);
246 }
247
248 clip_more (0, menu_height+3, table_width, table_height-menu_height-3);
249 for (i=cur_page; i<nwords; i++)
250 {
251 w = words+i;
252 if (w->flags & HEADER)
253 break;
254 if (w->flags & PTR_IMG)
255 put_picture((Picture *)w->ptr, w->x, w->y-w->a-vscroll, 0, 0, w->width, w->a+w->d);
256 else
257 {
258 XSetForeground(display, gc, help_foreground);
259 XSetFont(display, gc, fonts[w->flags & STYLE_BITS]->fid);
260 XDrawString(display, window, gc, w->x, w->y-vscroll, w->ptr, strlen(w->ptr));
261 }
262 }
263 unclip();
264 }
265
266 static void
267 my_help_key(int c, int x, int y)
268 {
269 int m, vs, old_vscroll = vscroll;
270 if (c == 'q')
271 exit(1);
272 if (c == 27 || c == ' ' || c == KEY_F(1) || c == 8 || c == 127 || c == KEY_DELETE)
273 {
274 free(file);
275 file = 0;
276 help_is_showing = 0;
277 invalidate(0, 0, table_width, table_height);
278 return;
279 }
280 if (c >= '0' && c <= '9')
281 {
282 m = c-'1';
283 if (m == -1 && !(words[0].flags & HEADER))
284 show_page(0, 0);
285 else if (m >= 0 && m < num_menus)
286 show_page(menus[m]+1, m);
287 return;
288 }
289 for (m=0; m<num_menus; m++)
290 if (tolower(words[menus[m]].ptr[0]) == tolower(c))
291 {
292 show_page(menus[m]+1, m);
293 return;
294 }
295
296 if (c == KEY_LEFT && cur_menu > 0)
297 show_page (menus[cur_menu-1]+1, cur_menu-1);
298 if (c == KEY_RIGHT && cur_menu < num_menus-1)
299 show_page (menus[cur_menu+1]+1, cur_menu+1);
300
301 vs = 0;
302 switch (c)
303 {
304 case KEY_DOWN: vs = 10; break;
305 case KEY_PGDN: vs = (table_height-menu_height)*9/10; break;
306 case KEY_UP: vs = -10; break;
307 case KEY_PGUP: vs = -(table_height-menu_height)*9/10; break;
308 }
309 if (vs)
310 {
311 vscroll += vs;
312 if (vscroll > max_vscroll)
313 vscroll = max_vscroll;
314 if (vscroll < 0)
315 vscroll = 0;
316 if (vscroll != old_vscroll)
317 invalidate(0, menu_height+3, table_width, table_height);
318 }
319 }
320
321 static void
322 my_help_click(int x, int y, int b)
323 {
324 int i, old_vscroll = vscroll;
325 if (y < menu_height)
326 {
327 Word *w = 0;
328 for (i=0; i<num_menus; i++)
329 {
330 w = words+menus[i];
331 if (x+mx > w->x && x+mx < w->x+w->width)
332 {
333 show_page(menus[i]+1, i);
334 return;
335 }
336 }
337 if (!w || ! (words[0].flags & HEADER) && x+mx > w->x+w->width+2*thin_space[MENU_FONT])
338 {
339 show_page(0, 0);
340 return;
341 }
342 }
343
344 /* rescale y to whole screen */
345 y = (y-menu_height) * table_height / (table_height - menu_height);
346
347 if (y > table_height/3 && y < table_height*2/3)
348 {
349 if (x < table_width/6)
350 {
351 my_help_key (KEY_LEFT, x, y);
352 return;
353 }
354 else if (x > table_width*5/6)
355 {
356 my_help_key (KEY_RIGHT, x, y);
357 return;
358 }
359 }
360 vscroll += (y - table_height/2);
361 if (vscroll > max_vscroll)
362 vscroll = max_vscroll;
363 if (vscroll < 0)
364 vscroll = 0;
365 if (vscroll != old_vscroll)
366 invalidate(0, menu_height+3, table_width, table_height);
367 }
368
369 #define MARGIN 10
370
371 void
372 help(char *filename, char *text)
373 {
374 int i, j, flags=0;
375 char *cp;
376 struct stat st;
377 FILE *f;
378 static int initted = 0;
379 int row_top, row_width;
380 int max_width = table_width - 2 * MARGIN;
381 int first_in_row;
382 int menu_x = 2;
383 Word *floating_left = 0, *floating_right = 0;
384 int float_margin = 0, float_left_margin = 0;
385
386 if (!initted) help_init();
387
388 max_vscroll = 0;
389
390 f = fopen(filename, "r");
391 if (!f)
392 {
393 int l = strlen(text)+1;
394 file = (char *)malloc(l);
395 memcpy(file, text, l);
396 }
397 else
398 {
399 stat(filename, &st);
400 file = (char *)malloc(st.st_size+1);
401 fread(file, 1, st.st_size, f);
402 fclose(f);
403 file[st.st_size] = 0;
404 }
405
406 nwords = 2; /* initial word plus filler */
407 for (cp=file; *cp; cp++)
408 if (*cp == '<'
409 || *cp == '>' && !isspace(cp[1])
410 || isspace(*cp) && !isspace(cp[1]))
411 nwords++;
412
413 words = (Word *)malloc(nwords * sizeof(Word));
414 nwords = 0;
415 num_menus = 2;
416 cp = file;
417 while (*cp)
418 {
419 while (*cp && isspace(*cp)) *cp++ = 0;
420 if (*cp == '<')
421 {
422 for (i=0; i<NTAGS; i++)
423 if (strncmp(cp, tags[i].tag, tags[i].taglen) == 0)
424 {
425 if (tags[i].bits_set & PTR_IMG)
426 {
427 Picture *p;
428 char *rbr;
429 cp += tags[i].taglen;
430 for (rbr=cp; *rbr && *rbr != '>'; rbr++);
431 *rbr = 0;
432 words[nwords].flags = ((flags & (HEADER|BLK_BR|BLK_P))
433 | PTR_IMG
434 | tags[i].bits_set);
435 flags &= ~(BLK_BR|BLK_P|PTR_IMG|HEADER);
436 p = get_picture(cp);
437 words[nwords].ptr = (char *)p;
438 words[nwords].a = p->h;
439 words[nwords].width = p->w;
440 words[nwords].d = words[nwords].rb = words[nwords].lb = 0;
441 if ((tags[i].bits_set & ALIGN_CENTER) == ALIGN_CENTER)
442 {
443 XFontStruct *f = fonts[0];
444 words[nwords].a = p->h/2 + (f->ascent-f->descent)/2;
445 words[nwords].d = p->h - words[nwords].a;
446 words[nwords].flags &= ~ALIGN_CENTER;
447 }
448 cp = rbr+1;
449 nwords ++;
450 }
451 else
452 {
453 flags &= ~tags[i].bits_reset;
454 flags |= tags[i].bits_set;
455 *cp = 0;
456 cp += tags[i].taglen;
457 }
458 break;
459 }
460 if (i < NTAGS)
461 continue;
462 }
463 if (*cp && !isspace(*cp))
464 {
465 words[nwords].flags = flags;
466 words[nwords].ptr = cp;
467 if (flags & HEADER)
468 num_menus++;
469 nwords ++;
470 cp++;
471 if (flags & HEADER)
472 while (*cp && *cp != '<') cp++;
473 else
474 while (*cp && !isspace(*cp) && *cp != '<') cp++;
475 flags &= ~(BLK_BR|BLK_P|PTR_IMG|HEADER);
476 }
477 }
478
479 menus = (int *)malloc(num_menus * sizeof(*menus));
480 num_menus = 0;
481
482 for (i=0; i<nwords; i++)
483 {
484 Word *w = words+i;
485 int junk;
486 XCharStruct cs;
487 XFontStruct *fs = fonts[w->flags & STYLE_BITS];
488 if (!(w->flags & PTR_IMG))
489 {
490 if (w->flags & HEADER) fs = fonts[MENU_FONT];
491 w->a = fs->ascent;
492 w->d = fs->descent;
493 XTextExtents(fs, w->ptr, strlen(w->ptr),
494 &junk, &junk, &junk, &cs);
495 w->lb = cs.lbearing;
496 w->rb = cs.rbearing;
497 w->width = cs.width;
498 if (w->flags & HEADER)
499 menus[num_menus++] = i;
500 }
501 }
502 menus[num_menus] = i;
503 if (menus[0] == 0)
504 cur_page = 1;
505 else
506 cur_page = 0;
507
508 row_top = menu_height + MARGIN;
509 row_width = 0;
510 first_in_row = 0;
511 for (i=0; i<=nwords; i++)
512 {
513 int end_para = 0;
514 Word *w = words+i;
515 int ts = (thin_space[words[i-1].flags & STYLE_BITS]
516 + thin_space[words[i].flags & STYLE_BITS])/2;
517
518 /* decide when we're done with a row */
519 #if 0
520 printf("i==%d rw=%d ts=%d rw+ts+ww=%d mw=%d f=%d\n",
521 i==nwords, row_width, ts, row_width + w->width + ts, max_width,
522 w->flags & (BLK_P|BLK_BR|HEADER));
523 #endif
524
525 if (i == nwords) end_para = 1;
526 if (row_width + w->width + ts > max_width - float_margin) end_para = 1;
527 if (w->flags & (BLK_P|BLK_BR|HEADER)) end_para = 1;
528 if (row_width == 0) end_para = 0;
529 if (row_width > 0 && w->flags & PTR_IMG && w->flags & FLOAT_LEFT)
530 end_para = 1;
531 if (w->flags & PTR_IMG && w->flags & FLOAT_LEFT && floating_left)
532 end_para = 1;
533 if (w->flags & PTR_IMG && w->flags & FLOAT_RIGHT && floating_right)
534 end_para = 1;
535 if (end_para)
536 {
537 int maxa=0, maxd=0;
538 for (j=first_in_row; j<i; j++)
539 {
540 if (words[j].flags & PTR_IMG && words[j].flags & (FLOAT_RIGHT|FLOAT_LEFT))
541 continue;
542 if (maxa < words[j].a) maxa = words[j].a;
543 if (maxd < words[j].d) maxd = words[j].d;
544 }
545 for (j=first_in_row; j<i; j++)
546 {
547 if (words[j].flags & PTR_IMG && words[j].flags & (FLOAT_RIGHT|FLOAT_LEFT))
548 continue;
549 words[j].y = row_top + maxa;
550 }
551 row_top += maxa + maxd;
552 if (w->flags & BLK_P)
553 row_top += MARGIN;
554 row_width = 0;
555 first_in_row = i;
556 }
557 if (i == nwords) break;
558
559 if (w->flags & PTR_IMG && w->flags & FLOAT_LEFT)
560 {
561 if (floating_left)
562 row_top = floating_left->y;
563 floating_left = w;
564 w->x = MARGIN;
565 w->y = row_top + w->a;
566 continue;
567 }
568
569 if (w->flags & PTR_IMG && w->flags & FLOAT_RIGHT)
570 {
571 if (floating_right)
572 row_top = floating_right->y;
573 floating_right = w;
574 w->x = table_width - MARGIN - w->width;
575 w->y = row_top + w->a;
576 continue;
577 }
578
579 float_margin = float_left_margin = 0;
580 if (floating_left)
581 {
582 if (floating_left->y <= row_top)
583 floating_left = 0;
584 else
585 {
586 float_margin += floating_left->width + MARGIN;
587 float_left_margin = floating_left->width + MARGIN;
588 }
589 }
590 if (floating_right)
591 {
592 if (floating_right->y <= row_top)
593 floating_right = 0;
594 else
595 float_margin += floating_right->width + MARGIN;
596 }
597
598 if (w->flags & HEADER)
599 {
600 w->x = menu_x + thin_space[MENU_FONT];
601 w->y = fonts[MENU_FONT]->ascent + thin_space[MENU_FONT]+1;
602 menu_x += w->width + 2*thin_space[MENU_FONT] + 2;
603 row_top = menu_height + MARGIN;
604 first_in_row++;
605 }
606 else
607 {
608 if (row_width > 0)
609 row_width += ts;
610 w->x = row_width + MARGIN + float_left_margin;
611 row_width += w->width;
612 }
613 #if 0
614 printf("[%02d] f=%02x x=%3d y=%3d a=%2d d=%2d lb=%2d rb=%2d w=%2d s=`%.*s'\n",
615 i, w->flags, w->x, w->y, w->a, w->d, w->lb, w->rb,
616 w->width, strlen(w->ptr), w->ptr);
617 #endif
618 }
619 #if 0
620 for (i=0; i<nwords; i++)
621 {
622 Word *w = words+i;
623 printf("[%02d] f=%02x x=%3d y=%3d a=%2d d=%2d lb=%2d rb=%2d w=%2d s=`%.*s'\n",
624 i, w->flags, w->x, w->y, w->a, w->d, w->lb, w->rb,
625 w->width, strlen(w->ptr), w->ptr);
626 }
627 #endif
628
629 help_is_showing = 1;
630 help_redraw = my_help_redraw;
631 help_click = my_help_click;
632 help_key = my_help_key;
633 show_page (cur_page, 0);
634 }