]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/cardfile.c
ncurses 5.9 - patch 20121026
[ncurses.git] / test / cardfile.c
1 /****************************************************************************
2  * Copyright (c) 1999-2010,2012 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.40 2012/10/27 19:37:56 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 static void
72 failed(const char *s)
73 {
74     perror(s);
75     endwin();
76     ExitProgram(EXIT_FAILURE);
77 }
78
79 #if !HAVE_STRDUP
80 #define strdup my_strdup
81 static char *
82 strdup(const char *s)
83 {
84     char *p = typeMalloc(char, strlen(s) + 1);
85     if (p)
86         strcpy(p, s);
87     else
88         failed("strdup");
89     return (p);
90 }
91 #endif /* not HAVE_STRDUP */
92
93 static const char *
94 skip(const char *buffer)
95 {
96     while (isspace(UChar(*buffer)))
97         buffer++;
98     return buffer;
99 }
100
101 static void
102 trim(char *buffer)
103 {
104     size_t n = strlen(buffer);
105     while (n-- && isspace(UChar(buffer[n])))
106         buffer[n] = 0;
107 }
108
109 /*******************************************************************************/
110
111 static CARD *
112 add_title(const char *title)
113 {
114     CARD *card, *p, *q;
115
116     for (p = all_cards, q = 0; p != 0; q = p, p = p->link) {
117         int cmp = strcmp(p->title, title);
118         if (cmp == 0)
119             return p;
120         if (cmp > 0)
121             break;
122     }
123
124     card = typeCalloc(CARD, 1);
125     card->title = strdup(title);
126     card->content = strdup("");
127
128     if (q == 0) {
129         card->link = all_cards;
130         all_cards = card;
131     } else {
132         card->link = q->link;
133         q->link = card;
134     }
135
136     return card;
137 }
138
139 static void
140 add_content(CARD * card, const char *content)
141 {
142     size_t total, offset;
143
144     content = skip(content);
145     if ((total = strlen(content)) != 0) {
146         if (card->content != 0 && (offset = strlen(card->content)) != 0) {
147             total += 1 + offset;
148             card->content = typeRealloc(char, total + 1, card->content);
149             if (card->content)
150                 strcpy(card->content + offset++, " ");
151         } else {
152             offset = 0;
153             if (card->content != 0)
154                 free(card->content);
155             card->content = typeMalloc(char, total + 1);
156         }
157         if (card->content)
158             strcpy(card->content + offset, content);
159         else
160             failed("add_content");
161     }
162 }
163
164 static CARD *
165 new_card(void)
166 {
167     CARD *card = add_title("");
168     add_content(card, "");
169     return card;
170 }
171
172 static CARD *
173 find_card(char *title)
174 {
175     CARD *card;
176
177     for (card = all_cards; card != 0; card = card->link)
178         if (!strcmp(card->title, title))
179             break;
180
181     return card;
182 }
183
184 static void
185 read_data(char *fname)
186 {
187     FILE *fp;
188     CARD *card = 0;
189     char buffer[BUFSIZ];
190
191     if ((fp = fopen(fname, "r")) != 0) {
192         while (fgets(buffer, sizeof(buffer), fp)) {
193             trim(buffer);
194             if (isspace(UChar(*buffer))) {
195                 if (card == 0)
196                     card = add_title("");
197                 add_content(card, buffer);
198             } else if ((card = find_card(buffer)) == 0) {
199                 card = add_title(buffer);
200             }
201         }
202         fclose(fp);
203     }
204 }
205
206 /*******************************************************************************/
207
208 static void
209 write_data(const char *fname)
210 {
211     FILE *fp;
212     CARD *p = 0;
213     int n;
214
215     if (!strcmp(fname, default_name))
216         fname = "cardfile.out";
217
218     if ((fp = fopen(fname, "w")) != 0) {
219         for (p = all_cards; p != 0; p = p->link) {
220             FIELD **f = form_fields(p->form);
221             for (n = 0; f[n] != 0; n++) {
222                 char *s = field_buffer(f[n], 0);
223                 if (s != 0
224                     && (s = strdup(s)) != 0) {
225                     trim(s);
226                     fprintf(fp, "%s%s\n", n ? "\t" : "", s);
227                     free(s);
228                 }
229             }
230         }
231         fclose(fp);
232     }
233 }
234
235 /*******************************************************************************/
236
237 /*
238  * Count the cards
239  */
240 static int
241 count_cards(void)
242 {
243     CARD *p;
244     int count = 0;
245
246     for (p = all_cards; p != 0; p = p->link)
247         count++;
248
249     return count;
250 }
251
252 /*
253  * Shuffle the panels to keep them in a natural hierarchy.
254  */
255 static void
256 order_cards(CARD * first, int depth)
257 {
258     if (first) {
259         if (depth && first->link)
260             order_cards(first->link, depth - 1);
261         if (isVisible(first))
262             top_panel(first->panel);
263     }
264 }
265
266 /*
267  * Return the next card in the list
268  */
269 static CARD *
270 next_card(CARD * now)
271 {
272     if (now->link != 0) {
273         CARD *tst = now->link;
274         if (isVisible(tst))
275             now = tst;
276         else
277             (void) next_card(tst);
278     }
279     return now;
280 }
281
282 /*
283  * Return the previous card in the list
284  */
285 static CARD *
286 prev_card(CARD * now)
287 {
288     CARD *p;
289     for (p = all_cards; p != 0; p = p->link) {
290         if (p->link == now) {
291             if (!isVisible(p))
292                 p = prev_card(p);
293             return p;
294         }
295     }
296     return now;
297 }
298
299 /*
300  * Returns the first card in the list that we will display.
301  */
302 static CARD *
303 first_card(CARD * now)
304 {
305     if (!isVisible(now))
306         now = next_card(now);
307     return now;
308 }
309
310 /*******************************************************************************/
311
312 static int
313 form_virtualize(WINDOW *w)
314 {
315     int c = wgetch(w);
316
317     switch (c) {
318     case CTRL('W'):
319         return (MY_CTRL_W);
320     case CTRL('N'):
321         return (MY_CTRL_N);
322     case CTRL('P'):
323         return (MY_CTRL_P);
324     case QUIT:
325     case ESCAPE:
326         return (MY_CTRL_Q);
327
328     case KEY_BACKSPACE:
329         return (REQ_DEL_PREV);
330     case KEY_DC:
331         return (REQ_DEL_CHAR);
332     case KEY_LEFT:
333         return (REQ_LEFT_CHAR);
334     case KEY_RIGHT:
335         return (REQ_RIGHT_CHAR);
336
337     case KEY_DOWN:
338     case KEY_NEXT:
339         return (REQ_NEXT_FIELD);
340     case KEY_UP:
341     case KEY_PREVIOUS:
342         return (REQ_PREV_FIELD);
343
344     default:
345         return (c);
346     }
347 }
348
349 static FIELD **
350 make_fields(CARD * p, int form_high, int form_wide)
351 {
352     FIELD **f = typeCalloc(FIELD *, 3);
353
354     f[0] = new_field(1, form_wide, 0, 0, 0, 0);
355     set_field_back(f[0], A_REVERSE);
356     set_field_buffer(f[0], 0, p->title);
357     field_opts_off(f[0], O_BLANK);
358
359     f[1] = new_field(form_high - 1, form_wide, 1, 0, 0, 0);
360     set_field_buffer(f[1], 0, p->content);
361     set_field_just(f[1], JUSTIFY_LEFT);
362     field_opts_off(f[1], O_BLANK);
363
364     f[2] = 0;
365     return f;
366 }
367
368 static void
369 show_legend(void)
370 {
371     erase();
372     move(LINES - 3, 0);
373     addstr("^Q/ESC -- exit form            ^W   -- writes data to file\n");
374     addstr("^N   -- go to next card        ^P   -- go to previous card\n");
375     addstr("Arrow keys move left/right within a field, up/down between fields");
376 }
377
378 #if (defined(KEY_RESIZE) && HAVE_WRESIZE) || NO_LEAKS
379 static void
380 free_form_fields(FIELD ** f)
381 {
382     int n;
383
384     for (n = 0; f[n] != 0; ++n) {
385         free_field(f[n]);
386     }
387     free(f);
388 }
389 #endif
390
391 /*******************************************************************************/
392
393 static void
394 cardfile(char *fname)
395 {
396     WINDOW *win;
397     CARD *p;
398     CARD *top_card;
399     int visible_cards;
400     int panel_wide;
401     int panel_high;
402     int form_wide;
403     int form_high;
404     int y;
405     int x;
406     int ch = ERR;
407     int finished = FALSE;
408
409     show_legend();
410
411     /* decide how many cards we can display */
412     visible_cards = count_cards();
413     while (
414               (panel_wide = COLS - (visible_cards * OFFSET_CARD)) < 10 ||
415               (panel_high = LINES - (visible_cards * OFFSET_CARD) - 5) < 5) {
416         --visible_cards;
417     }
418     form_wide = panel_wide - 2;
419     form_high = panel_high - 2;
420     y = (visible_cards - 1) * OFFSET_CARD;
421     x = 0;
422
423     /* make a panel for each CARD */
424     for (p = all_cards; p != 0; p = p->link) {
425
426         if ((win = newwin(panel_high, panel_wide, y, x)) == 0)
427             break;
428
429         wbkgd(win, (chtype) COLOR_PAIR(pair_2));
430         keypad(win, TRUE);
431         p->panel = new_panel(win);
432         box(win, 0, 0);
433
434         p->form = new_form(make_fields(p, form_high, form_wide));
435         set_form_win(p->form, win);
436         set_form_sub(p->form, derwin(win, form_high, form_wide, 1, 1));
437         post_form(p->form);
438
439         y -= OFFSET_CARD;
440         x += OFFSET_CARD;
441     }
442
443     top_card = first_card(all_cards);
444     order_cards(top_card, visible_cards);
445
446     while (!finished) {
447         update_panels();
448         doupdate();
449
450         ch = form_virtualize(panel_window(top_card->panel));
451         switch (form_driver(top_card->form, ch)) {
452         case E_OK:
453             break;
454         case E_UNKNOWN_COMMAND:
455             switch (ch) {
456             case MY_CTRL_Q:
457                 finished = TRUE;
458                 break;
459             case MY_CTRL_P:
460                 top_card = prev_card(top_card);
461                 order_cards(top_card, visible_cards);
462                 break;
463             case MY_CTRL_N:
464                 top_card = next_card(top_card);
465                 order_cards(top_card, visible_cards);
466                 break;
467             case MY_CTRL_W:
468                 form_driver(top_card->form, REQ_VALIDATION);
469                 write_data(fname);
470                 break;
471 #if defined(KEY_RESIZE) && HAVE_WRESIZE
472             case KEY_RESIZE:
473                 /* resizeterm already did "something" reasonable, but it cannot
474                  * know much about layout.  So let's make it nicer.
475                  */
476                 panel_wide = COLS - (visible_cards * OFFSET_CARD);
477                 panel_high = LINES - (visible_cards * OFFSET_CARD) - 5;
478
479                 form_wide = panel_wide - 2;
480                 form_high = panel_high - 2;
481
482                 y = (visible_cards - 1) * OFFSET_CARD;
483                 x = 0;
484
485                 show_legend();
486                 for (p = all_cards; p != 0; p = p->link) {
487                     FIELD **oldf = form_fields(p->form);
488                     WINDOW *olds = form_sub(p->form);
489
490                     if (!isVisible(p))
491                         continue;
492                     win = form_win(p->form);
493
494                     /* move and resize the card as needed
495                      * FIXME: if the windows are shrunk too much, this won't do
496                      */
497                     mvwin(win, y, x);
498                     wresize(win, panel_high, panel_wide);
499
500                     /* reconstruct each form.  Forms are not resizable, and
501                      * there appears to be no good way to reload the text in
502                      * a resized window.
503                      */
504                     werase(win);
505
506                     unpost_form(p->form);
507                     free_form(p->form);
508
509                     p->form = new_form(make_fields(p, form_high, form_wide));
510                     set_form_win(p->form, win);
511                     set_form_sub(p->form, derwin(win, form_high, form_wide,
512                                                  1, 1));
513                     post_form(p->form);
514
515                     free_form_fields(oldf);
516                     delwin(olds);
517
518                     box(win, 0, 0);
519
520                     y -= OFFSET_CARD;
521                     x += OFFSET_CARD;
522                 }
523                 break;
524 #endif
525             default:
526                 beep();
527                 break;
528             }
529             break;
530         default:
531             flash();
532             break;
533         }
534     }
535 #if NO_LEAKS
536     while (all_cards != 0) {
537         FIELD **f;
538
539         p = all_cards;
540         all_cards = all_cards->link;
541
542         if (isVisible(p)) {
543             f = form_fields(p->form);
544
545             unpost_form(p->form);       /* ...so we can free it */
546             free_form(p->form); /* this also disconnects the fields */
547
548             free_form_fields(f);
549
550             del_panel(p->panel);
551         }
552         free(p->title);
553         free(p->content);
554         free(p);
555     }
556 #endif
557 }
558
559 static void
560 usage(void)
561 {
562     static const char *msg[] =
563     {
564         "Usage: view [options] file"
565         ,""
566         ,"Options:"
567         ," -c       use color if terminal supports it"
568     };
569     size_t n;
570     for (n = 0; n < SIZEOF(msg); n++)
571         fprintf(stderr, "%s\n", msg[n]);
572     ExitProgram(EXIT_FAILURE);
573 }
574
575 /*******************************************************************************/
576
577 int
578 main(int argc, char *argv[])
579 {
580     int n;
581
582     setlocale(LC_ALL, "");
583
584     while ((n = getopt(argc, argv, "c")) != -1) {
585         switch (n) {
586         case 'c':
587             try_color = TRUE;
588             break;
589         default:
590             usage();
591         }
592     }
593
594     initscr();
595     cbreak();
596     noecho();
597
598     if (try_color) {
599         if (has_colors()) {
600             start_color();
601             init_pair(pair_1, COLOR_WHITE, COLOR_BLUE);
602             init_pair(pair_2, COLOR_WHITE, COLOR_CYAN);
603             bkgd((chtype) COLOR_PAIR(pair_1));
604         } else {
605             try_color = FALSE;
606         }
607     }
608
609     if (optind + 1 == argc) {
610         for (n = 1; n < argc; n++)
611             read_data(argv[n]);
612         if (count_cards() == 0)
613             new_card();
614         cardfile(argv[1]);
615     } else {
616         read_data(default_name);
617         if (count_cards() == 0)
618             new_card();
619         cardfile(default_name);
620     }
621
622     endwin();
623
624     ExitProgram(EXIT_SUCCESS);
625 }
626 #else
627 int
628 main(void)
629 {
630     printf("This program requires the curses form and panel libraries\n");
631     ExitProgram(EXIT_FAILURE);
632 }
633 #endif