X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fcardfile.c;h=a0034728fd113b0df66c3d15599d0b87d9202b1e;hp=09a9823910465f223e3433b5298414f810b0373a;hb=396a05943b7da5039dd15d79c4385c7d2a75d6d4;hpb=46722468f47c2b77b3987729b4bcf2321cccfd01 diff --git a/test/cardfile.c b/test/cardfile.c index 09a98239..a0034728 100644 --- a/test/cardfile.c +++ b/test/cardfile.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 1999-2001,2002 Free Software Foundation, Inc. * + * Copyright (c) 1999-2007,2008 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -27,28 +27,34 @@ ****************************************************************************/ /* - * Author: Thomas E. Dickey 1999 + * Author: Thomas E. Dickey * - * $Id: cardfile.c,v 1.19 2002/09/01 17:59:48 tom Exp $ + * $Id: cardfile.c,v 1.33 2008/04/12 22:05:53 tom Exp $ * - * File format: text beginning in column 1 is a title; other text forms the content. + * File format: text beginning in column 1 is a title; other text is content. */ #include -#if HAVE_FORM_H && HAVE_PANEL_H && HAVE_LIBFORM && HAVE_LIBPANEL +#if USE_LIBFORM && USE_LIBPANEL #include #include -#include - #define VISIBLE_CARDS 10 #define OFFSET_CARD 2 +#define pair_1 1 +#define pair_2 2 -#ifndef CTRL -#define CTRL(x) ((x) & 0x1f) -#endif +#define isVisible(cardp) ((cardp)->panel != 0) + +enum { + MY_CTRL_x = MAX_FORM_COMMAND + ,MY_CTRL_N + ,MY_CTRL_P + ,MY_CTRL_Q + ,MY_CTRL_W +}; typedef struct _card { struct _card *link; @@ -59,14 +65,15 @@ typedef struct _card { } CARD; static CARD *all_cards; +static bool try_color = FALSE; static char default_name[] = "cardfile.dat"; #if !HAVE_STRDUP #define strdup my_strdup static char * -strdup(char *s) +strdup(const char *s) { - char *p = (char *) malloc(strlen(s) + 1); + char *p = typeMalloc(char, strlen(s) + 1); if (p) strcpy(p, s); return (p); @@ -104,7 +111,7 @@ add_title(const char *title) break; } - card = (CARD *) calloc(1, sizeof(CARD)); + card = typeCalloc(CARD, 1); card->title = strdup(title); card->content = strdup(""); @@ -128,14 +135,16 @@ add_content(CARD * card, const char *content) if ((total = strlen(content)) != 0) { if ((offset = strlen(card->content)) != 0) { total += 1 + offset; - card->content = (char *) realloc(card->content, total + 1); - strcpy(card->content + offset++, " "); + card->content = typeRealloc(char, total + 1, card->content); + if (card->content) + strcpy(card->content + offset++, " "); } else { if (card->content != 0) free(card->content); - card->content = (char *) malloc(total + 1); + card->content = typeMalloc(char, total + 1); } - strcpy(card->content + offset, content); + if (card->content) + strcpy(card->content + offset, content); } } @@ -236,7 +245,8 @@ order_cards(CARD * first, int depth) if (first) { if (depth && first->link) order_cards(first->link, depth - 1); - top_panel(first->panel); + if (isVisible(first)) + top_panel(first->panel); } } @@ -246,8 +256,13 @@ order_cards(CARD * first, int depth) static CARD * next_card(CARD * now) { - if (now->link) - now = now->link; + if (now->link != 0) { + CARD *tst = now->link; + if (isVisible(tst)) + now = tst; + else + tst = next_card(tst); + } return now; } @@ -258,9 +273,24 @@ static CARD * prev_card(CARD * now) { CARD *p; - for (p = all_cards; p != 0; p = p->link) - if (p->link == now) + for (p = all_cards; p != 0; p = p->link) { + if (p->link == now) { + if (!isVisible(p)) + p = prev_card(p); return p; + } + } + return now; +} + +/* + * Returns the first card in the list that we will display. + */ +static CARD * +first_card(CARD * now) +{ + if (!isVisible(now)) + now = next_card(now); return now; } @@ -273,14 +303,14 @@ form_virtualize(WINDOW *w) switch (c) { case CTRL('W'): - return (MAX_FORM_COMMAND + 4); + return (MY_CTRL_W); case CTRL('N'): - return (MAX_FORM_COMMAND + 3); + return (MY_CTRL_N); case CTRL('P'): - return (MAX_FORM_COMMAND + 2); - case CTRL('Q'): - case 033: - return (MAX_FORM_COMMAND + 1); + return (MY_CTRL_P); + case QUIT: + case ESCAPE: + return (MY_CTRL_Q); case KEY_BACKSPACE: return (REQ_DEL_PREV); @@ -306,15 +336,17 @@ form_virtualize(WINDOW *w) static FIELD ** make_fields(CARD * p, int form_high, int form_wide) { - FIELD **f = (FIELD **) calloc(3, sizeof(FIELD *)); + FIELD **f = typeCalloc(FIELD *, 3); f[0] = new_field(1, form_wide, 0, 0, 0, 0); set_field_back(f[0], A_REVERSE); set_field_buffer(f[0], 0, p->title); + field_opts_off(f[0], O_BLANK); f[1] = new_field(form_high - 1, form_wide, 1, 0, 0, 0); set_field_buffer(f[1], 0, p->content); set_field_just(f[1], JUSTIFY_LEFT); + field_opts_off(f[1], O_BLANK); f[2] = 0; return f; @@ -351,23 +383,37 @@ cardfile(char *fname) WINDOW *win; CARD *p; CARD *top_card; - int visible_cards = count_cards(); - int panel_wide = COLS - (visible_cards * OFFSET_CARD); - int panel_high = LINES - (visible_cards * OFFSET_CARD) - 5; - int form_wide = panel_wide - 2; - int form_high = panel_high - 2; - int y = (visible_cards - 1) * OFFSET_CARD; - int x = 0; + int visible_cards; + int panel_wide; + int panel_high; + int form_wide; + int form_high; + int y; + int x; int ch = ERR; - int last_ch; int finished = FALSE; show_legend(); + /* decide how many cards we can display */ + visible_cards = count_cards(); + while ( + (panel_wide = COLS - (visible_cards * OFFSET_CARD)) < 10 || + (panel_high = LINES - (visible_cards * OFFSET_CARD) - 5) < 5) { + --visible_cards; + } + form_wide = panel_wide - 2; + form_high = panel_high - 2; + y = (visible_cards - 1) * OFFSET_CARD; + x = 0; + /* make a panel for each CARD */ for (p = all_cards; p != 0; p = p->link) { - win = newwin(panel_high, panel_wide, y, x); + if ((win = newwin(panel_high, panel_wide, y, x)) == 0) + break; + + wbkgd(win, COLOR_PAIR(pair_2)); keypad(win, TRUE); p->panel = new_panel(win); box(win, 0, 0); @@ -381,31 +427,32 @@ cardfile(char *fname) x += OFFSET_CARD; } - order_cards(top_card = all_cards, visible_cards); + top_card = first_card(all_cards); + order_cards(top_card, visible_cards); while (!finished) { update_panels(); doupdate(); - last_ch = ch; ch = form_virtualize(panel_window(top_card->panel)); switch (form_driver(top_card->form, ch)) { case E_OK: break; case E_UNKNOWN_COMMAND: switch (ch) { - case MAX_FORM_COMMAND + 1: + case MY_CTRL_Q: finished = TRUE; break; - case MAX_FORM_COMMAND + 2: + case MY_CTRL_P: top_card = prev_card(top_card); order_cards(top_card, visible_cards); break; - case MAX_FORM_COMMAND + 3: + case MY_CTRL_N: top_card = next_card(top_card); order_cards(top_card, visible_cards); break; - case MAX_FORM_COMMAND + 4: + case MY_CTRL_W: + form_driver(top_card->form, REQ_VALIDATION); write_data(fname); break; #if defined(KEY_RESIZE) && HAVE_WRESIZE @@ -427,6 +474,8 @@ cardfile(char *fname) FIELD **oldf = form_fields(p->form); WINDOW *olds = form_sub(p->form); + if (!isVisible(p)) + continue; win = form_win(p->form); /* move and resize the card as needed @@ -478,15 +527,17 @@ cardfile(char *fname) p = all_cards; all_cards = all_cards->link; - f = form_fields(p->form); - count = field_count(p->form); + if (isVisible(p)) { + f = form_fields(p->form); + count = field_count(p->form); - unpost_form(p->form); /* ...so we can free it */ - free_form(p->form); /* this also disconnects the fields */ + unpost_form(p->form); /* ...so we can free it */ + free_form(p->form); /* this also disconnects the fields */ - free_form_fields(f); + free_form_fields(f); - del_panel(p->panel); + del_panel(p->panel); + } free(p->title); free(p->content); free(p); @@ -494,6 +545,22 @@ cardfile(char *fname) #endif } +static void +usage(void) +{ + static const char *msg[] = + { + "Usage: view [options] file" + ,"" + ,"Options:" + ," -c use color if terminal supports it" + }; + size_t n; + for (n = 0; n < SIZEOF(msg); n++) + fprintf(stderr, "%s\n", msg[n]); + ExitProgram(EXIT_FAILURE); +} + /*******************************************************************************/ int @@ -503,11 +570,32 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); + while ((n = getopt(argc, argv, "c")) != -1) { + switch (n) { + case 'c': + try_color = TRUE; + break; + default: + usage(); + } + } + initscr(); cbreak(); noecho(); - if (argc > 1) { + if (try_color) { + if (has_colors()) { + start_color(); + init_pair(pair_1, COLOR_WHITE, COLOR_BLUE); + init_pair(pair_2, COLOR_WHITE, COLOR_CYAN); + bkgd(COLOR_PAIR(pair_1)); + } else { + try_color = FALSE; + } + } + + if (optind + 1 == argc) { for (n = 1; n < argc; n++) read_data(argv[n]); if (count_cards() == 0)