1 /****************************************************************************
2 * Copyright (c) 1999-2001,2002 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
30 * Author: Thomas E. Dickey <dickey@clark.net> 1999
32 * $Id: cardfile.c,v 1.19 2002/09/01 17:59:48 tom Exp $
34 * File format: text beginning in column 1 is a title; other text forms the content.
37 #include <test.priv.h>
39 #if HAVE_FORM_H && HAVE_PANEL_H && HAVE_LIBFORM && HAVE_LIBPANEL
46 #define VISIBLE_CARDS 10
50 #define CTRL(x) ((x) & 0x1f)
53 typedef struct _card {
61 static CARD *all_cards;
62 static char default_name[] = "cardfile.dat";
65 #define strdup my_strdup
69 char *p = (char *) malloc(strlen(s) + 1);
74 #endif /* not HAVE_STRDUP */
77 skip(const char *buffer)
79 while (isspace(UChar(*buffer)))
87 unsigned n = strlen(buffer);
88 while (n-- && isspace(UChar(buffer[n])))
92 /*******************************************************************************/
95 add_title(const char *title)
99 for (p = all_cards, q = 0; p != 0; q = p, p = p->link) {
100 int cmp = strcmp(p->title, title);
107 card = (CARD *) calloc(1, sizeof(CARD));
108 card->title = strdup(title);
109 card->content = strdup("");
112 card->link = all_cards;
115 card->link = q->link;
123 add_content(CARD * card, const char *content)
125 unsigned total, offset;
127 content = skip(content);
128 if ((total = strlen(content)) != 0) {
129 if ((offset = strlen(card->content)) != 0) {
131 card->content = (char *) realloc(card->content, total + 1);
132 strcpy(card->content + offset++, " ");
134 if (card->content != 0)
136 card->content = (char *) malloc(total + 1);
138 strcpy(card->content + offset, content);
145 CARD *card = add_title("");
146 add_content(card, "");
151 find_card(char *title)
155 for (card = all_cards; card != 0; card = card->link)
156 if (!strcmp(card->title, title))
163 read_data(char *fname)
169 if ((fp = fopen(fname, "r")) != 0) {
170 while (fgets(buffer, sizeof(buffer), fp)) {
172 if (isspace(UChar(*buffer))) {
174 card = add_title("");
175 add_content(card, buffer);
176 } else if ((card = find_card(buffer)) == 0) {
177 card = add_title(buffer);
184 /*******************************************************************************/
187 write_data(const char *fname)
193 if (!strcmp(fname, default_name))
194 fname = "cardfile.out";
196 if ((fp = fopen(fname, "w")) != 0) {
197 for (p = all_cards; p != 0; p = p->link) {
198 FIELD **f = form_fields(p->form);
199 for (n = 0; f[n] != 0; n++) {
200 char *s = field_buffer(f[n], 0);
202 && (s = strdup(s)) != 0) {
204 fprintf(fp, "%s%s\n", n ? "\t" : "", s);
213 /*******************************************************************************/
224 for (p = all_cards; p != 0; p = p->link)
231 * Shuffle the panels to keep them in a natural hierarchy.
234 order_cards(CARD * first, int depth)
237 if (depth && first->link)
238 order_cards(first->link, depth - 1);
239 top_panel(first->panel);
244 * Return the next card in the list
247 next_card(CARD * now)
255 * Return the previous card in the list
258 prev_card(CARD * now)
261 for (p = all_cards; p != 0; p = p->link)
267 /*******************************************************************************/
270 form_virtualize(WINDOW *w)
276 return (MAX_FORM_COMMAND + 4);
278 return (MAX_FORM_COMMAND + 3);
280 return (MAX_FORM_COMMAND + 2);
283 return (MAX_FORM_COMMAND + 1);
286 return (REQ_DEL_PREV);
288 return (REQ_DEL_CHAR);
290 return (REQ_LEFT_CHAR);
292 return (REQ_RIGHT_CHAR);
296 return (REQ_NEXT_FIELD);
299 return (REQ_PREV_FIELD);
307 make_fields(CARD * p, int form_high, int form_wide)
309 FIELD **f = (FIELD **) calloc(3, sizeof(FIELD *));
311 f[0] = new_field(1, form_wide, 0, 0, 0, 0);
312 set_field_back(f[0], A_REVERSE);
313 set_field_buffer(f[0], 0, p->title);
315 f[1] = new_field(form_high - 1, form_wide, 1, 0, 0, 0);
316 set_field_buffer(f[1], 0, p->content);
317 set_field_just(f[1], JUSTIFY_LEFT);
328 addstr("^Q/ESC -- exit form ^W -- writes data to file\n");
329 addstr("^N -- go to next card ^P -- go to previous card\n");
330 addstr("Arrow keys move left/right within a field, up/down between fields");
333 #if (defined(KEY_RESIZE) && HAVE_WRESIZE) || NO_LEAKS
335 free_form_fields(FIELD ** f)
339 for (n = 0; f[n] != 0; ++n) {
346 /*******************************************************************************/
349 cardfile(char *fname)
354 int visible_cards = count_cards();
355 int panel_wide = COLS - (visible_cards * OFFSET_CARD);
356 int panel_high = LINES - (visible_cards * OFFSET_CARD) - 5;
357 int form_wide = panel_wide - 2;
358 int form_high = panel_high - 2;
359 int y = (visible_cards - 1) * OFFSET_CARD;
363 int finished = FALSE;
367 /* make a panel for each CARD */
368 for (p = all_cards; p != 0; p = p->link) {
370 win = newwin(panel_high, panel_wide, y, x);
372 p->panel = new_panel(win);
375 p->form = new_form(make_fields(p, form_high, form_wide));
376 set_form_win(p->form, win);
377 set_form_sub(p->form, derwin(win, form_high, form_wide, 1, 1));
384 order_cards(top_card = all_cards, visible_cards);
391 ch = form_virtualize(panel_window(top_card->panel));
392 switch (form_driver(top_card->form, ch)) {
395 case E_UNKNOWN_COMMAND:
397 case MAX_FORM_COMMAND + 1:
400 case MAX_FORM_COMMAND + 2:
401 top_card = prev_card(top_card);
402 order_cards(top_card, visible_cards);
404 case MAX_FORM_COMMAND + 3:
405 top_card = next_card(top_card);
406 order_cards(top_card, visible_cards);
408 case MAX_FORM_COMMAND + 4:
411 #if defined(KEY_RESIZE) && HAVE_WRESIZE
413 /* resizeterm already did "something" reasonable, but it cannot
414 * know much about layout. So let's make it nicer.
416 panel_wide = COLS - (visible_cards * OFFSET_CARD);
417 panel_high = LINES - (visible_cards * OFFSET_CARD) - 5;
419 form_wide = panel_wide - 2;
420 form_high = panel_high - 2;
422 y = (visible_cards - 1) * OFFSET_CARD;
426 for (p = all_cards; p != 0; p = p->link) {
427 FIELD **oldf = form_fields(p->form);
428 WINDOW *olds = form_sub(p->form);
430 win = form_win(p->form);
432 /* move and resize the card as needed
433 * FIXME: if the windows are shrunk too much, this won't do
436 wresize(win, panel_high, panel_wide);
438 /* reconstruct each form. Forms are not resizable, and
439 * there appears to be no good way to reload the text in
444 unpost_form(p->form);
447 p->form = new_form(make_fields(p, form_high, form_wide));
448 set_form_win(p->form, win);
449 set_form_sub(p->form, derwin(win, form_high, form_wide,
453 free_form_fields(oldf);
474 while (all_cards != 0) {
479 all_cards = all_cards->link;
481 f = form_fields(p->form);
482 count = field_count(p->form);
484 unpost_form(p->form); /* ...so we can free it */
485 free_form(p->form); /* this also disconnects the fields */
497 /*******************************************************************************/
500 main(int argc, char *argv[])
504 setlocale(LC_ALL, "");
511 for (n = 1; n < argc; n++)
513 if (count_cards() == 0)
517 read_data(default_name);
518 if (count_cards() == 0)
520 cardfile(default_name);
525 ExitProgram(EXIT_SUCCESS);
531 printf("This program requires the curses form and panel libraries\n");
532 ExitProgram(EXIT_FAILURE);