home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / shell / dialog-0.000 / dialog-0 / dialog-0.6c / menubox.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-18  |  9.3 KB  |  309 lines

  1. /*
  2.  *  menubox.c -- implements the menu box
  3.  *
  4.  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  5.  *
  6.  *  This program is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU General Public License
  8.  *  as published by the Free Software Foundation; either version 2
  9.  *  of the License, or (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include "dialog.h"
  22.  
  23. static int menu_width, tag_x, item_x;
  24.  
  25. /*
  26.  * Print menu item
  27.  */
  28. static void
  29. print_item (WINDOW * win, const char *tag, const char *item,
  30.         int choice, int selected)
  31. {
  32.     int i;
  33.  
  34.     /* Clear 'residue' of last item */
  35.     wattrset (win, menubox_attr);
  36.     wmove (win, choice, 0);
  37.     for (i = 0; i < menu_width; i++)
  38.     waddch (win, ' ');
  39.     wmove (win, choice, tag_x);
  40.     wattrset (win, selected ? tag_key_selected_attr : tag_key_attr);
  41.     waddch (win, tag[0]);
  42.     wattrset (win, selected ? tag_selected_attr : tag_attr);
  43.     waddstr (win, tag + 1);
  44.     wmove (win, choice, item_x);
  45.     wattrset (win, selected ? item_selected_attr : item_attr);
  46.     waddstr (win, item);
  47. }
  48.  
  49. /*
  50.  * Display a menu for choosing among a number of options
  51.  */
  52. int
  53. dialog_menu (const char *title, const char *prompt, int height, int width,
  54.         int menu_height, int item_no, const char * const * items)
  55. {
  56.     int i, x, y, cur_x, cur_y, box_x, box_y;
  57.     int key = 0, button = 0, choice = 0, scroll = 0, max_choice;
  58.     WINDOW *dialog, *menu;
  59.  
  60.     max_choice = MIN (menu_height, item_no);
  61.  
  62.     /* center dialog box on screen */
  63.     x = (COLS - width) / 2;
  64.     y = (LINES - height) / 2;
  65.  
  66. #ifdef HAVE_NCURSES
  67.     if (use_shadow)
  68.     draw_shadow (stdscr, y, x, height, width);
  69. #endif
  70.     dialog = newwin (height, width, y, x);
  71.     mouse_setbase (x, y);
  72.     keypad (dialog, TRUE);
  73.  
  74.     draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
  75.     wattrset (dialog, border_attr);
  76.     wmove (dialog, height - 3, 0);
  77.     waddch (dialog, ACS_LTEE);
  78.     for (i = 0; i < width - 2; i++)
  79.     waddch (dialog, ACS_HLINE);
  80.     wattrset (dialog, dialog_attr);
  81.     waddch (dialog, ACS_RTEE);
  82.     wmove (dialog, height - 2, 1);
  83.     for (i = 0; i < width - 2; i++)
  84.     waddch (dialog, ' ');
  85.  
  86.     if (title != NULL) {
  87.     wattrset (dialog, title_attr);
  88.     wmove (dialog, 0, (width - strlen (title)) / 2 - 1);
  89.     waddch (dialog, ' ');
  90.     waddstr (dialog, title);
  91.     waddch (dialog, ' ');
  92.     }
  93.     wattrset (dialog, dialog_attr);
  94.     print_autowrap (dialog, prompt, width - 2, 1, 3);
  95.  
  96.     menu_width = width - 6;
  97.     getyx (dialog, cur_y, cur_x);
  98.     box_y = cur_y + 1;
  99.     box_x = (width - menu_width) / 2 - 1;
  100.  
  101.     /* create new window for the menu */
  102.     menu = subwin (dialog, menu_height, menu_width, y + box_y + 1,
  103.            x + box_x + 1);
  104.     keypad (menu, TRUE);
  105.  
  106.     /* draw a box around the menu items */
  107.     draw_box (dialog, box_y, box_x, menu_height + 2, menu_width + 2,
  108.           menubox_border_attr, menubox_attr);
  109.  
  110.     tag_x = 0;
  111.     item_x = 0;
  112.     /* Find length of longest item in order to center menu */
  113.     for (i = 0; i < item_no; i++) {
  114.     tag_x = MAX (tag_x,
  115.              strlen (items[i * 2]) + strlen (items[i * 2 + 1]) + 2);
  116.     item_x = MAX (item_x, strlen (items[i * 2]));
  117.     }
  118.     tag_x = (menu_width - tag_x) / 2;
  119.     item_x = tag_x + item_x + 2;
  120.  
  121.     /* Print the menu */
  122.     for (i = 0; i < max_choice; i++)
  123.     print_item (menu, items[i * 2], items[i * 2 + 1],
  124.             i, i == choice);
  125.     wnoutrefresh (menu);
  126.  
  127.     /* register the new window, along with its borders */
  128.     mouse_mkbigregion (box_y, box_x, menu_height + 2, menu_width + 2,
  129.          item_no, item_x /* the threshold */ , 1 /* dirty mode */ );
  130.  
  131.     if (menu_height < item_no) {
  132.     wattrset (dialog, darrow_attr);
  133.     wmove (dialog, box_y + menu_height + 1, box_x + tag_x + 1);
  134.     waddch (dialog, ACS_DARROW);
  135.     wmove (dialog, box_y + menu_height + 1, box_x + tag_x + 2);
  136.     waddstr (dialog, "(+)");
  137.     }
  138.     x = width / 2 - 11;
  139.     y = height - 2;
  140.     print_button (dialog, "Cancel", y, x + 14, FALSE);
  141.     print_button (dialog, "  OK  ", y, x, TRUE);
  142.     wrefresh (dialog);
  143.  
  144.     while (key != ESC) {
  145.     key = mouse_wgetch (dialog);
  146.     /* Check if key pressed matches first character of any
  147.        item tag in menu */
  148.     for (i = 0; i < max_choice; i++)
  149.         if (toupper (key) == toupper (items[(scroll + i) * 2][0]))
  150.         break;
  151.  
  152.     if (i < max_choice ||
  153.         (key >= '1' && key <= MIN ('9', '0' + max_choice)) ||
  154.         key == KEY_UP || key == KEY_DOWN || key == '-' ||
  155.         key == '+' || (key >= M_EVENT && key - M_EVENT < ' ')) {
  156.         if (key >= '1' && key <= MIN ('9', '0' + max_choice))
  157.         i = key - '1';
  158.         else if (key >= M_EVENT)
  159.         i = key - M_EVENT;
  160.         else if (key == KEY_UP || key == '-') {
  161.         if (!choice) {
  162.             if (scroll) {
  163.  
  164.             /* Scroll menu down */
  165.             getyx (dialog, cur_y, cur_x);
  166.             if (menu_height > 1) {
  167.                 /* De-highlight current first item */
  168.                 print_item (menu, items[scroll * 2],
  169.                     items[scroll * 2 + 1], 0, FALSE);
  170.                 scrollok (menu, TRUE);
  171.                 wscrl (menu, -1);
  172.                 scrollok (menu, FALSE);
  173.             }
  174.             scroll--;
  175.             print_item (menu, items[scroll * 2],
  176.                     items[scroll * 2 + 1], 0, TRUE);
  177.             wnoutrefresh (menu);
  178.  
  179.             /* print the up/down arrows */
  180.             wmove (dialog, box_y, box_x + tag_x + 1);
  181.             wattrset (dialog, scroll ? uarrow_attr : menubox_attr);
  182.             waddch (dialog, scroll ? ACS_UARROW : ACS_HLINE);
  183.             wmove (dialog, box_y, box_x + tag_x + 2);
  184.             waddch (dialog, scroll ? '(' : ACS_HLINE);
  185.             wmove (dialog, box_y, box_x + tag_x + 3);
  186.             waddch (dialog, scroll ? '-' : ACS_HLINE);
  187.             wmove (dialog, box_y, box_x + tag_x + 4);
  188.             waddch (dialog, scroll ? ')' : ACS_HLINE);
  189.             wattrset (dialog, darrow_attr);
  190.             wmove (dialog, box_y + menu_height + 1,
  191.                    box_x + tag_x + 1);
  192.             waddch (dialog, ACS_DARROW);
  193.             wmove (dialog, box_y + menu_height + 1,
  194.                    box_x + tag_x + 2);
  195.             waddstr (dialog, "(+)");
  196.             wmove (dialog, cur_y, cur_x);
  197.             wrefresh (dialog);
  198.             }
  199.             continue;    /* wait for another key press */
  200.         } else
  201.             i = choice - 1;
  202.         } else if (key == KEY_DOWN || key == '+')
  203.         if (choice == max_choice - 1) {
  204.             if (scroll + choice < item_no - 1) {
  205.             /* Scroll menu up */
  206.             getyx (dialog, cur_y, cur_x);
  207.             if (menu_height > 1) {
  208.                 /* De-highlight current last item */
  209.                 print_item (menu, items[(scroll + max_choice - 1)
  210.                        * 2], items[(scroll + max_choice - 1)
  211.                        * 2 + 1], max_choice - 1, FALSE);
  212.                 scrollok (menu, TRUE);
  213.                 scroll (menu);
  214.                 scrollok (menu, FALSE);
  215.             }
  216.             scroll++;
  217.             print_item (menu, items[(scroll + max_choice - 1) * 2],
  218.                     items[(scroll + max_choice - 1) * 2 + 1],
  219.                     max_choice - 1, TRUE);
  220.             wnoutrefresh (menu);
  221.  
  222.             /* print the up/down arrows */
  223.             wattrset (dialog, uarrow_attr);
  224.             wmove (dialog, box_y, box_x + tag_x + 1);
  225.             waddch (dialog, ACS_UARROW);
  226.             wmove (dialog, box_y, box_x + tag_x + 2);
  227.             waddstr (dialog, "(-)");
  228.             wmove (dialog, box_y + menu_height + 1,
  229.                    box_x + tag_x + 1);
  230.             wattrset (dialog, scroll + choice < item_no - 1 ?
  231.                   darrow_attr : menubox_border_attr);
  232.             waddch (dialog, scroll + choice < item_no - 1 ?
  233.                 ACS_DARROW : ACS_HLINE);
  234.             wmove (dialog, box_y + menu_height + 1,
  235.                    box_x + tag_x + 2);
  236.             waddch (dialog, scroll + choice < item_no - 1 ?
  237.                 '(' : ACS_HLINE);
  238.             wmove (dialog, box_y + menu_height + 1,
  239.                    box_x + tag_x + 3);
  240.             waddch (dialog, scroll + choice < item_no - 1 ?
  241.                 '+' : ACS_HLINE);
  242.             wmove (dialog, box_y + menu_height + 1,
  243.                    box_x + tag_x + 4);
  244.             waddch (dialog, scroll + choice < item_no - 1 ?
  245.                 ')' : ACS_HLINE);
  246.             wmove (dialog, cur_y, cur_x);
  247.             wrefresh (dialog);
  248.             }
  249.             continue;    /* wait for another key press */
  250.         } else
  251.             i = choice + 1;
  252.  
  253.         if (i != choice) {
  254.         /* De-highlight current item */
  255.         getyx (dialog, cur_y, cur_x);    /* Save cursor position */
  256.         print_item (menu, items[(scroll + choice) * 2],
  257.                 items[(scroll + choice) * 2 + 1], choice, FALSE);
  258.  
  259.         /* Highlight new item */
  260.         choice = i;
  261.         print_item (menu, items[(scroll + choice) * 2],
  262.                 items[(scroll + choice) * 2 + 1], choice, TRUE);
  263.         wnoutrefresh (menu);
  264.         wmove (dialog, cur_y, cur_x);
  265.         wrefresh (dialog);
  266.         }
  267.         continue;        /* wait for another key press */
  268.     }
  269.     switch (key) {
  270.     case 'O':
  271.     case 'o':
  272.     case M_EVENT + 'O':
  273.         delwin (dialog);
  274.         return scroll + choice;
  275.     case 'C':
  276.     case 'c':
  277.     case M_EVENT + 'C':
  278.         delwin (dialog);
  279.         return -2;
  280.     case M_EVENT + 'o':    /* mouse enter... */
  281.     case M_EVENT + 'c':    /* use the code for toggling */
  282.         button = (key == M_EVENT + 'o');
  283.     case ' ':
  284.     case TAB:
  285.     case KEY_LEFT:
  286.     case KEY_RIGHT:
  287.         if (!button) {
  288.         button = 1;    /* Indicates "Cancel" button is selected */
  289.         print_button (dialog, "  OK  ", y, x, FALSE);
  290.         print_button (dialog, "Cancel", y, x + 14, TRUE);
  291.         } else {
  292.         button = 0;    /* Indicates "OK" button is selected */
  293.         print_button (dialog, "Cancel", y, x + 14, FALSE);
  294.         print_button (dialog, "  OK  ", y, x, TRUE);
  295.         }
  296.         wrefresh (dialog);
  297.         break;
  298.     case '\n':
  299.         delwin (dialog);
  300.         return (button? -2 : (scroll + choice));
  301.     case ESC:
  302.         break;
  303.     }
  304.     }
  305.  
  306.     delwin (dialog);
  307.     return -1;            /* ESC pressed */
  308. }
  309.