]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/cardfile.c
ncurses 5.6 - patch 20070324
[ncurses.git] / test / cardfile.c
1 /****************************************************************************
2  * Copyright (c) 1999-2006,2007 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /*
30  * Author: Thomas E. Dickey
31  *
32  * $Id: cardfile.c,v 1.29 2007/03/10 19:16:45 tom Exp $
33  *
34  * File format: text beginning in column 1 is a title; other text is content.
35  */
36
37 #include <test.priv.h>
38
39 #if USE_LIBFORM && USE_LIBPANEL
40
41 #include <form.h>
42 #include <panel.h>
43
44 #define VISIBLE_CARDS 10
45 #define OFFSET_CARD 2
46 #define pair_1 1
47 #define pair_2 2
48
49 #define isVisible(cardp) ((cardp)->panel != 0)
50
51 enum {
52     MY_CTRL_x = MAX_FORM_COMMAND
53     ,MY_CTRL_N
54     ,MY_CTRL_P
55     ,MY_CTRL_Q
56     ,MY_CTRL_W
57 };
58
59 typedef struct _card {
60     struct _card *link;
61     PANEL *panel;
62     FORM *form;
63     char *title;
64     char *content;
65 } CARD;
66
67 static CARD *all_cards;
68 static bool try_color = FALSE;
69 static char default_name[] = "cardfile.dat";
70
71 #if !HAVE_STRDUP
72 #define strdup my_strdup
73 static char *
74 strdup(char *s)
75 {
76     char *p = (char *) malloc(strlen(s) + 1);
77     if (p)
78         strcpy(p, s);
79     return (p);
80 }
81 #endif /* not HAVE_STRDUP */
82
83 static const char *
84 skip(const char *buffer)
85 {
86     while (isspace(UChar(*buffer)))
87         buffer++;
88     return buffer;
89 }
90
91 static void
92 trim(char *buffer)
93 {
94     unsigned n = strlen(buffer);
95     while (n-- && isspace(UChar(buffer[n])))
96         buffer[n] = 0;
97 }
98
99 /*******************************************************************************/
100
101 static CARD *
102 add_title(const char *title)
103 {
104     CARD *card, *p, *q;
105
106     for (p = all_cards, q = 0; p != 0; q = p, p = p->link) {
107         int cmp = strcmp(p->title, title);
108         if (cmp == 0)
109             return p;
110         if (cmp > 0)
111             break;
112     }
113
114     card = (CARD *) calloc(1, sizeof(CARD));
115     card->title = strdup(title);
116     card->content = strdup("");
117
118     if (q == 0) {
119         card->link = all_cards;
120         all_cards = card;
121     } else {
122         card->link = q->link;
123         q->link = card;
124     }
125
126     return card;
127 }
128
129 static void
130 add_content(CARD * card, const char *content)
131 {
132     unsigned total, offset;
133
134     content = skip(content);
135     if ((total = strlen(content)) != 0) {
136         if ((offset = strlen(card->content)) != 0) {
137             total += 1 + offset;
138             card->content = (char *) realloc(card->content, total + 1);
139             strcpy(card->content + offset++, " ");
140         } else {
141             if (card->content != 0)
142                 free(card->content);
143             card->content = (char *) malloc(total + 1);
144         }
145         strcpy(card->content + offset, content);
146     }
147 }
148
149 static CARD *
150 new_card(void)
151 {
152     CARD *card = add_title("");
153     add_content(card, "");
154     return card;
155 }
156
157 static CARD *
158 find_card(char *title)
159 {
160     CARD *card;
161
162     for (card = all_cards; card != 0; card = card->link)
163         if (!strcmp(card->title, title))
164             break;
165
166     return card;
167 }
168
169 static void
170 read_data(char *fname)
171 {
172     FILE *fp;
173     CARD *card = 0;
174     char buffer[BUFSIZ];
175
176     if ((fp = fopen(fname, "r")) != 0) {
177         while (fgets(buffer, sizeof(buffer), fp)) {
178             trim(buffer);
179             if (isspace(UChar(*buffer))) {
180                 if (card == 0)
181                     card = add_title("");
182                 add_content(card, buffer);
183             } else if ((card = find_card(buffer)) == 0) {
184                 card = add_title(buffer);
185             }
186         }
187         fclose(fp);
188     }
189 }
190
191 /*******************************************************************************/
192
193 static void
194 write_data(const char *fname)
195 {
196     FILE *fp;
197     CARD *p = 0;
198     int n;
199
200     if (!strcmp(fname, default_name))
201         fname = "cardfile.out";
202
203     if ((fp = fopen(fname, "w")) != 0) {
204         for (p = all_cards; p != 0; p = p->link) {
205             FIELD **f = form_fields(p->form);
206             for (n = 0; f[n] != 0; n++) {
207                 char *s = field_buffer(f[n], 0);
208                 if (s != 0
209                     && (s = strdup(s)) != 0) {
210                     trim(s);
211                     fprintf(fp, "%s%s\n", n ? "\t" : "", s);
212                     free(s);
213                 }
214             }
215         }
216         fclose(fp);
217     }
218 }
219
220 /*******************************************************************************/
221
222 /*
223  * Count the cards
224  */
225 static int
226 count_cards(void)
227 {
228     CARD *p;
229     int count = 0;
230
231     for (p = all_cards; p != 0; p = p->link)
232         count++;
233
234     return count;
235 }
236
237 /*
238  * Shuffle the panels to keep them in a natural hierarchy.
239  */
240 static void
241 order_cards(CARD * first, int depth)
242 {
243     if (first) {
244         if (depth && first->link)
245             order_cards(first->link, depth - 1);
246         if (isVisible(first))
247             top_panel(first->panel);
248     }
249 }
250
251 /*
252  * Return the next card in the list
253  */
254 static CARD *
255 next_card(CARD * now)
256 {
257     if (now->link != 0) {
258         CARD *tst = now->link;
259         if (isVisible(tst))
260             now = tst;
261         else
262             tst = next_card(tst);
263     }
264     return now;
265 }
266
267 /*
268  * Return the previous card in the list
269  */
270 static CARD *
271 prev_card(CARD * now)
272 {
273     CARD *p;
274     for (p = all_cards; p != 0; p = p->link) {
275         if (p->link == now) {
276             if (!isVisible(p))
277                 p = prev_card(p);
278             return p;
279         }
280     }
281     return now;
282 }
283
284 /*
285  * Returns the first card in the list that we will display.
286  */
287 static CARD *
288 first_card(CARD * now)
289 {
290     if (!isVisible(now))
291         now = next_card(now);
292     return now;
293 }
294
295 /*******************************************************************************/
296
297 static int
298 form_virtualize(WINDOW *w)
299 {
300     int c = wgetch(w);
301
302     switch (c) {
303     case CTRL('W'):
304         return (MY_CTRL_W);
305     case CTRL('N'):
306         return (MY_CTRL_N);
307     case CTRL('P'):
308         return (MY_CTRL_P);
309     case QUIT:
310     case ESCAPE:
311         return (MY_CTRL_Q);
312
313     case KEY_BACKSPACE:
314         return (REQ_DEL_PREV);
315     case KEY_DC:
316         return (REQ_DEL_CHAR);
317     case KEY_LEFT:
318         return (REQ_LEFT_CHAR);
319     case KEY_RIGHT:
320         return (REQ_RIGHT_CHAR);
321
322     case KEY_DOWN:
323     case KEY_NEXT:
324         return (REQ_NEXT_FIELD);
325     case KEY_UP:
326     case KEY_PREVIOUS:
327         return (REQ_PREV_FIELD);
328
329     default:
330         return (c);
331     }
332 }
333
334 static FIELD **
335 make_fields(CARD * p, int form_high, int form_wide)
336 {
337     FIELD **f = (FIELD **) calloc(3, sizeof(FIELD *));
338
339     f[0] = new_field(1, form_wide, 0, 0, 0, 0);
340     set_field_back(f[0], A_REVERSE);
341     set_field_buffer(f[0], 0, p->title);
342     field_opts_off(f[0], O_BLANK);
343
344     f[1] = new_field(form_high - 1, form_wide, 1, 0, 0, 0);
345     set_field_buffer(f[1], 0, p->content);
346     set_field_just(f[1], JUSTIFY_LEFT);
347     field_opts_off(f[1], O_BLANK);
348
349     f[2] = 0;
350     return f;
351 }
352
353 static void
354 show_legend(void)
355 {
356     erase();
357     move(LINES - 3, 0);
358     addstr("^Q/ESC -- exit form            ^W   -- writes data to file\n");
359     addstr("^N   -- go to next card        ^P   -- go to previous card\n");
360     addstr("Arrow keys move left/right within a field, up/down between fields");
361 }
362
363 #if (defined(KEY_RESIZE) && HAVE_WRESIZE) || NO_LEAKS
364 static void
365 free_form_fields(FIELD ** f)
366 {
367     int n;
368
369     for (n = 0; f[n] != 0; ++n) {
370         free_field(f[n]);
371     }
372     free(f);
373 }
374 #endif
375
376 /*******************************************************************************/
377
378 static void
379 cardfile(char *fname)
380 {
381     WINDOW *win;
382     CARD *p;
383     CARD *top_card;
384     int visible_cards;
385     int panel_wide;
386     int panel_high;
387     int form_wide;
388     int form_high;
389     int y;
390     int x;
391     int ch = ERR;
392     int finished = FALSE;
393
394     show_legend();
395
396     /* decide how many cards we can display */
397     visible_cards = count_cards();
398     while (
399               (panel_wide = COLS - (visible_cards * OFFSET_CARD)) < 10 ||
400               (panel_high = LINES - (visible_cards * OFFSET_CARD) - 5) < 5) {
401         --visible_cards;
402     }
403     form_wide = panel_wide - 2;
404     form_high = panel_high - 2;
405     y = (visible_cards - 1) * OFFSET_CARD;
406     x = 0;
407
408     /* make a panel for each CARD */
409     for (p = all_cards; p != 0; p = p->link) {
410
411         if ((win = newwin(panel_high, panel_wide, y, x)) == 0)
412             break;
413
414         wbkgd(win, COLOR_PAIR(pair_2));
415         keypad(win, TRUE);
416         p->panel = new_panel(win);
417         box(win, 0, 0);
418
419         p->form = new_form(make_fields(p, form_high, form_wide));
420         set_form_win(p->form, win);
421         set_form_sub(p->form, derwin(win, form_high, form_wide, 1, 1));
422         post_form(p->form);
423
424         y -= OFFSET_CARD;
425         x += OFFSET_CARD;
426     }
427
428     top_card = first_card(all_cards);
429     order_cards(top_card, visible_cards);
430
431     while (!finished) {
432         update_panels();
433         doupdate();
434
435         ch = form_virtualize(panel_window(top_card->panel));
436         switch (form_driver(top_card->form, ch)) {
437         case E_OK:
438             break;
439         case E_UNKNOWN_COMMAND:
440             switch (ch) {
441             case MY_CTRL_Q:
442                 finished = TRUE;
443                 break;
444             case MY_CTRL_P:
445                 top_card = prev_card(top_card);
446                 order_cards(top_card, visible_cards);
447                 break;
448             case MY_CTRL_N:
449                 top_card = next_card(top_card);
450                 order_cards(top_card, visible_cards);
451                 break;
452             case MY_CTRL_W:
453                 form_driver(top_card->form, REQ_VALIDATION);
454                 write_data(fname);
455                 break;
456 #if defined(KEY_RESIZE) && HAVE_WRESIZE
457             case KEY_RESIZE:
458                 /* resizeterm already did "something" reasonable, but it cannot
459                  * know much about layout.  So let's make it nicer.
460                  */
461                 panel_wide = COLS - (visible_cards * OFFSET_CARD);
462                 panel_high = LINES - (visible_cards * OFFSET_CARD) - 5;
463
464                 form_wide = panel_wide - 2;
465                 form_high = panel_high - 2;
466
467                 y = (visible_cards - 1) * OFFSET_CARD;
468                 x = 0;
469
470                 show_legend();
471                 for (p = all_cards; p != 0; p = p->link) {
472                     FIELD **oldf = form_fields(p->form);
473                     WINDOW *olds = form_sub(p->form);
474
475                     if (!isVisible(p))
476                         continue;
477                     win = form_win(p->form);
478
479                     /* move and resize the card as needed
480                      * FIXME: if the windows are shrunk too much, this won't do
481                      */
482                     mvwin(win, y, x);
483                     wresize(win, panel_high, panel_wide);
484
485                     /* reconstruct each form.  Forms are not resizable, and
486                      * there appears to be no good way to reload the text in
487                      * a resized window.
488                      */
489                     werase(win);
490
491                     unpost_form(p->form);
492                     free_form(p->form);
493
494                     p->form = new_form(make_fields(p, form_high, form_wide));
495                     set_form_win(p->form, win);
496                     set_form_sub(p->form, derwin(win, form_high, form_wide,
497                                                  1, 1));
498                     post_form(p->form);
499
500                     free_form_fields(oldf);
501                     delwin(olds);
502
503                     box(win, 0, 0);
504
505                     y -= OFFSET_CARD;
506                     x += OFFSET_CARD;
507                 }
508                 break;
509 #endif
510             default:
511                 beep();
512                 break;
513             }
514             break;
515         default:
516             flash();
517             break;
518         }
519     }
520 #if NO_LEAKS
521     while (all_cards != 0) {
522         FIELD **f;
523         int count;
524
525         p = all_cards;
526         all_cards = all_cards->link;
527
528         if (isVisible(p)) {
529             f = form_fields(p->form);
530             count = field_count(p->form);
531
532             unpost_form(p->form);       /* ...so we can free it */
533             free_form(p->form); /* this also disconnects the fields */
534
535             free_form_fields(f);
536
537             del_panel(p->panel);
538         }
539         free(p->title);
540         free(p->content);
541         free(p);
542     }
543 #endif
544 }
545
546 static void
547 usage(void)
548 {
549     static const char *msg[] =
550     {
551         "Usage: view [options] file"
552         ,""
553         ,"Options:"
554         ," -c       use color if terminal supports it"
555     };
556     size_t n;
557     for (n = 0; n < SIZEOF(msg); n++)
558         fprintf(stderr, "%s\n", msg[n]);
559     ExitProgram(EXIT_FAILURE);
560 }
561
562 /*******************************************************************************/
563
564 int
565 main(int argc, char *argv[])
566 {
567     int n;
568
569     setlocale(LC_ALL, "");
570
571     while ((n = getopt(argc, argv, "c")) != EOF) {
572         switch (n) {
573         case 'c':
574             try_color = TRUE;
575             break;
576         default:
577             usage();
578         }
579     }
580
581     initscr();
582     cbreak();
583     noecho();
584
585     if (try_color) {
586         if (has_colors()) {
587             start_color();
588             init_pair(pair_1, COLOR_WHITE, COLOR_BLUE);
589             init_pair(pair_2, COLOR_WHITE, COLOR_CYAN);
590             bkgd(COLOR_PAIR(pair_1));
591         } else {
592             try_color = FALSE;
593         }
594     }
595
596     if (optind + 1 == argc) {
597         for (n = 1; n < argc; n++)
598             read_data(argv[n]);
599         if (count_cards() == 0)
600             new_card();
601         cardfile(argv[1]);
602     } else {
603         read_data(default_name);
604         if (count_cards() == 0)
605             new_card();
606         cardfile(default_name);
607     }
608
609     endwin();
610
611     ExitProgram(EXIT_SUCCESS);
612 }
613 #else
614 int
615 main(void)
616 {
617     printf("This program requires the curses form and panel libraries\n");
618     ExitProgram(EXIT_FAILURE);
619 }
620 #endif