home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ncurses-1.9.9e-src.tgz / tar.out / fsf / ncurses / menu / m_post.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  8KB  |  267 lines

  1.  
  2. /***************************************************************************
  3. *                            COPYRIGHT NOTICE                              *
  4. ****************************************************************************
  5. *                ncurses is copyright (C) 1992-1995                        *
  6. *                          Zeyd M. Ben-Halim                               *
  7. *                          zmbenhal@netcom.com                             *
  8. *                          Eric S. Raymond                                 *
  9. *                          esr@snark.thyrsus.com                           *
  10. *                                                                          *
  11. *        Permission is hereby granted to reproduce and distribute ncurses  *
  12. *        by any means and for any fee, whether alone or as part of a       *
  13. *        larger distribution, in source or in binary form, PROVIDED        *
  14. *        this notice is included with any such distribution, and is not    *
  15. *        removed from any of its header files. Mention of ncurses in any   *
  16. *        applications linked with it is highly appreciated.                *
  17. *                                                                          *
  18. *        ncurses comes AS IS with no warranty, implied or expressed.       *
  19. *                                                                          *
  20. ***************************************************************************/
  21.  
  22. /***************************************************************************
  23. * Module menu_post                                                         *
  24. * Write or erase menus from associated subwindows                          *
  25. ***************************************************************************/
  26.  
  27. #include "menu.priv.h"
  28.  
  29. /*---------------------------------------------------------------------------
  30. |   Facility      :  libnmenu  
  31. |   Function      :  void _nc_Post_Item(MENU *menu, ITEM *item)  
  32. |   
  33. |   Description   :  Draw the item in the menus window at the current
  34. |                    window position 
  35. |
  36. |   Return Values :  -
  37. +--------------------------------------------------------------------------*/
  38. void _nc_Post_Item(const MENU * menu, const ITEM * item)
  39. {
  40.   int i;
  41.   chtype ch;
  42.   bool isfore = FALSE, isback=FALSE, isgrey = FALSE;
  43.   
  44.   assert(menu->win);
  45.   
  46.   /* First we have to calculate the attribute depending on selectability
  47.      and selection status
  48.      */
  49.   if (!(item->opt & O_SELECTABLE))
  50.     {
  51.       wattron(menu->win,menu->grey);
  52.       isgrey = TRUE;
  53.     }
  54.   else
  55.     {
  56.       if (item->value || item==menu->curitem)
  57.     {
  58.       wattron(menu->win,menu->fore);
  59.       isfore = TRUE;
  60.     }
  61.       else
  62.     {
  63.       wattron(menu->win,menu->back);
  64.       isback = TRUE;
  65.     }
  66.     }
  67.   
  68.   /* We need a marker iff
  69.      - it is a onevalued menu and it is the current item
  70.      - or it has a selection value
  71.      */
  72.   if (item->value || ((menu->opt&O_ONEVALUE) && (item==menu->curitem))    )
  73.     {
  74.       if (menu->marklen) 
  75.     waddstr(menu->win,menu->mark);
  76.     }
  77.   else            /* otherwise we have to wipe out the marker area */ 
  78.     for(ch=menu->pad,i=menu->marklen;i>0;i--) 
  79.       waddch(menu->win,ch);
  80.   
  81.   waddnstr(menu->win,item->name.str,item->name.length);
  82.   for(ch=menu->pad,i=menu->namelen-item->name.length;i>0;i--)
  83.     {
  84.       waddch(menu->win,ch);
  85.     }
  86.   
  87.   /* Show description if required and available */
  88.   if ( (menu->opt & O_SHOWDESC) && menu->desclen>0 )
  89.     {
  90.       waddch(menu->win,menu->pad);
  91.       if (item->description.length)
  92.     waddnstr(menu->win,item->description.str,item->description.length);
  93.       for(ch=menu->pad,i=menu->desclen-item->description.length; i>0; i--)
  94.     {
  95.       waddch(menu->win,ch);
  96.     }
  97.     }
  98.   
  99.   /* Remove attributes */
  100.   if (isfore)
  101.     wattroff(menu->win,menu->fore);
  102.   if (isback)
  103.     wattroff(menu->win,menu->back);
  104.   if (isgrey)
  105.     wattroff(menu->win,menu->grey);
  106. }    
  107.  
  108. /*---------------------------------------------------------------------------
  109. |   Facility      :  libnmenu  
  110. |   Function      :  void _nc_Draw_Menu(const MENU *)
  111. |   
  112. |   Description   :  Display the menu in its windows
  113. |
  114. |   Return Values :  -
  115. +--------------------------------------------------------------------------*/
  116. void _nc_Draw_Menu(const MENU * menu)
  117. {
  118.   ITEM *item = menu->items[0];
  119.   ITEM *lasthor, *lastvert;
  120.   ITEM *hitem;
  121.   int y = 0;
  122.   
  123.   assert(item && menu->win);
  124.   
  125.   lastvert = (menu->opt & O_NONCYCLIC) ? (ITEM *)0 : item;  
  126.   
  127.   do
  128.     {  
  129.       wmove(menu->win,y++,0);
  130.       
  131.       hitem   = item;
  132.       lasthor = (menu->opt & O_NONCYCLIC) ? (ITEM *)0 : hitem;
  133.       
  134.       do
  135.     {
  136.       _nc_Post_Item( menu, hitem);
  137.       if ( ((hitem = hitem->right) != lasthor) && hitem )
  138.         {
  139.           waddch( menu->win,menu->pad);
  140.         }
  141.     } while (hitem && (hitem != lasthor));
  142.       
  143.       item = item->down;
  144.       
  145.     } while( item && (item != lastvert) );    
  146. }
  147.  
  148. /*---------------------------------------------------------------------------
  149. |   Facility      :  libnmenu  
  150. |   Function      :  int post_menu(MENU *)
  151. |   
  152. |   Description   :  Post a menu to the screen. This makes it visible.
  153. |
  154. |   Return Values :  E_OK                - success
  155. |                    E_BAD_ARGUMENT      - not a valid menu pointer
  156. |                    E_SYSTEM_ERROR      - error in lower layers
  157. |                    E_NO_ROOM           - Menu to large for screen
  158. |                    E_NOT_CONNECTED     - No items connected to menu
  159. |                    E_BAD_STATE         - Menu in userexit routine
  160. |                    E_POSTED            - Menu already posted
  161. +--------------------------------------------------------------------------*/
  162. int post_menu(MENU * menu)
  163. {
  164.   if (!menu)
  165.     RETURN(E_BAD_ARGUMENT);
  166.   
  167.   if ( menu->status & _IN_DRIVER )
  168.     RETURN(E_BAD_STATE);
  169.  
  170.   if ( menu->status & _POSTED )
  171.     RETURN(E_POSTED);
  172.   
  173.   if (menu->items && *(menu->items))
  174.     {
  175.       int y;
  176.       WINDOW *win = Get_Menu_Window(menu);
  177.       int maxy = getmaxy(win);
  178.       int maxx = getmaxx(win);
  179.       
  180.       if (maxx < menu->width || maxy < menu->height)
  181.     RETURN(E_NO_ROOM);
  182.  
  183.       if ( (menu->win = newpad(menu->rows,menu->width)) )
  184.     {
  185.       y = (maxy >= menu->rows) ? menu->rows : maxy;
  186.       if (y>=menu->height) 
  187.         y = menu->height;
  188.       if(!(menu->sub = subpad(menu->win,y,menu->width,0,0)))
  189.         RETURN(E_SYSTEM_ERROR);
  190.     }
  191.       else 
  192.     RETURN(E_SYSTEM_ERROR);    
  193.       
  194.       if (menu->status & _LINK_NEEDED) 
  195.     _nc_Link_Items(menu);
  196.     }
  197.   else
  198.     RETURN(E_NOT_CONNECTED);
  199.   
  200.   menu->status |= _POSTED;
  201.  
  202.   if (!(menu->opt&O_ONEVALUE))
  203.     {
  204.       ITEM **items;
  205.   
  206.       for(items=menu->items;*items;items++)
  207.     {
  208.       (*items)->value = FALSE;
  209.     }
  210.     }
  211.   
  212.   _nc_Draw_Menu(menu);
  213.   
  214.   Call_Hook(menu,menuinit);
  215.   Call_Hook(menu,iteminit);
  216.   
  217.   _nc_Show_Menu(menu);
  218.   
  219.   RETURN(E_OK);
  220. }
  221.  
  222. /*---------------------------------------------------------------------------
  223. |   Facility      :  libnmenu  
  224. |   Function      :  int unpost_menu(MENU *)
  225. |   
  226. |   Description   :  Detach menu from screen
  227. |
  228. |   Return Values :  E_OK              - success
  229. |                    E_BAD_ARGUMENT    - not a valid menu pointer
  230. |                    E_BAD_STATE       - menu in userexit routine
  231. |                    E_NOT_POSTED      - menu is not posted
  232. +--------------------------------------------------------------------------*/
  233. int unpost_menu(MENU * menu)
  234. {
  235.   WINDOW *win;
  236.   
  237.   if (!menu)
  238.     RETURN(E_BAD_ARGUMENT);
  239.   
  240.   if ( menu->status & _IN_DRIVER )
  241.     RETURN(E_BAD_STATE);
  242.  
  243.   if ( !( menu->status & _POSTED ) )
  244.     RETURN(E_NOT_POSTED);
  245.   
  246.   Call_Hook(menu,itemterm);
  247.   Call_Hook(menu,menuterm);    
  248.   
  249.   win = Get_Menu_Window(menu);
  250.   werase(win);
  251.   wsyncup(win);
  252.   
  253.   assert(menu->sub);
  254.   delwin(menu->sub);
  255.   menu->sub = (WINDOW *)0;
  256.   
  257.   assert(menu->win);
  258.   delwin(menu->win);
  259.   menu->win = (WINDOW *)0;
  260.   
  261.   menu->status &= ~_POSTED;
  262.   
  263.   RETURN(E_OK);
  264. }
  265.  
  266. /* m_post.c ends here */
  267.