]> ncurses.scripts.mit.edu Git - ncurses.git/blob - menu/m_post.c
759b328a5ced88af6725fae0ed7eda36aa72de79
[ncurses.git] / menu / m_post.c
1 /****************************************************************************
2  * Copyright (c) 1998,2000 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:  Juergen Pfeifer, 1995,1997                                    *
31  ****************************************************************************/
32
33 /***************************************************************************
34 * Module m_post                                                            *
35 * Write or erase menus from associated subwindows                          *
36 ***************************************************************************/
37
38 #include "menu.priv.h"
39
40 MODULE_ID("$Id: m_post.c,v 1.19 2003/10/25 14:54:48 tom Exp $")
41
42 /*---------------------------------------------------------------------------
43 |   Facility      :  libnmenu  
44 |   Function      :  void _nc_Post_Item(MENU *menu, ITEM *item)  
45 |   
46 |   Description   :  Draw the item in the menus window at the current
47 |                    window position 
48 |
49 |   Return Values :  -
50 +--------------------------------------------------------------------------*/
51 NCURSES_EXPORT(void)
52 _nc_Post_Item (const MENU * menu, const ITEM * item)
53 {
54   int i;
55   chtype ch;
56   int item_x, item_y;
57   int count = 0;
58   bool isfore = FALSE, isback=FALSE, isgrey = FALSE;
59   
60   assert(menu->win);
61   
62   getyx(menu->win,item_y,item_x);
63   
64   /* We need a marker iff
65      - it is a onevalued menu and it is the current item
66      - or it has a selection value
67      */
68   wattron(menu->win,menu->back);
69   if (item->value || (item==menu->curitem))
70     {
71       if (menu->marklen) 
72         {
73           /* In a multi selection menu we use the fore attribute
74              for a selected marker that is not the current one.
75              This improves visualization of the menu, because now
76              always the 'normal' marker denotes the current
77              item. */
78           if (!(menu->opt & O_ONEVALUE) && item->value && item!=menu->curitem)
79             {
80               wattron(menu->win,menu->fore);
81               isfore = TRUE;
82             }
83           waddstr(menu->win,menu->mark);
84           if (isfore)
85             {
86               wattron(menu->win,menu->fore);
87               isfore = FALSE;
88             }
89         }
90     }
91   else                  /* otherwise we have to wipe out the marker area */ 
92     for(ch=' ',i=menu->marklen;i>0;i--) 
93       waddch(menu->win,ch);
94   wattroff(menu->win,menu->back);
95   count += menu->marklen;
96
97   /* First we have to calculate the attribute depending on selectability
98      and selection status
99      */
100   if (!(item->opt & O_SELECTABLE))
101     {
102       wattron(menu->win,menu->grey);
103       isgrey = TRUE;
104     }
105   else
106     {
107       if (item->value || item==menu->curitem)
108         {
109           wattron(menu->win,menu->fore);
110           isfore = TRUE;
111         }
112       else
113         {
114           wattron(menu->win,menu->back);
115           isback = TRUE;
116         }
117     }
118
119   waddnstr(menu->win,item->name.str,item->name.length);
120   for(ch=' ',i=menu->namelen-item->name.length;i>0;i--)
121     {
122       waddch(menu->win,ch);
123     }
124   count += menu->namelen;
125
126   /* Show description if required and available */
127   if ( (menu->opt & O_SHOWDESC) && menu->desclen>0 )
128     {
129       int m = menu->spc_desc/2;
130       int cy = -1, cx = -1;
131
132       for(ch=' ',i=0; i < menu->spc_desc; i++)
133         {
134           if (i==m)
135             {
136               waddch(menu->win,menu->pad);
137               getyx(menu->win,cy,cx);
138             }
139           else
140             waddch(menu->win,ch);
141         }
142       if (item->description.length)
143         waddnstr(menu->win,item->description.str,item->description.length);
144       for(ch=' ',i=menu->desclen-item->description.length; i>0; i--)
145         {
146           waddch(menu->win,ch);
147         }
148       count += menu->desclen + menu->spc_desc;
149
150       if (menu->spc_rows > 1)
151         {
152           int j, k, ncy, ncx;
153
154           assert(cx>=0 && cy>=0);
155           getyx(menu->win,ncy,ncx);
156           if (isgrey) wattroff(menu->win,menu->grey);
157           else if (isfore) wattroff(menu->win,menu->fore);
158           wattron(menu->win,menu->back);
159           for(j=1; j < menu->spc_rows;j++)
160             {
161               if ((item_y+j) < getmaxy(menu->win))
162                 {
163                   wmove (menu->win,item_y+j,item_x);
164                   for(k=0;k<count;k++)
165                     waddch(menu->win,' ');
166                 }
167               if ((cy+j) < getmaxy(menu->win))
168                 mvwaddch(menu->win,cy+j,cx-1,menu->pad);
169             }
170           wmove(menu->win,ncy,ncx);
171           if (!isback)
172             wattroff(menu->win,menu->back);
173         }
174     }
175   
176   /* Remove attributes */
177   if (isfore)
178     wattroff(menu->win,menu->fore);
179   if (isback)
180     wattroff(menu->win,menu->back);
181   if (isgrey)
182     wattroff(menu->win,menu->grey);
183 }       
184
185 /*---------------------------------------------------------------------------
186 |   Facility      :  libnmenu  
187 |   Function      :  void _nc_Draw_Menu(const MENU *)
188 |   
189 |   Description   :  Display the menu in its windows
190 |
191 |   Return Values :  -
192 +--------------------------------------------------------------------------*/
193 NCURSES_EXPORT(void)
194 _nc_Draw_Menu (const MENU * menu)
195 {
196   ITEM *item = menu->items[0];
197   ITEM *lasthor, *lastvert;
198   ITEM *hitem;
199   int y = 0;
200   chtype s_bkgd;
201
202   assert(item && menu->win);
203
204   s_bkgd = getbkgd(menu->win);
205   wbkgdset(menu->win,menu->back);
206   werase(menu->win);
207   wbkgdset(menu->win,s_bkgd);
208
209   lastvert = (menu->opt & O_NONCYCLIC) ? (ITEM *)0 : item;  
210
211   do
212     {  
213       wmove(menu->win,y,0);
214
215       hitem   = item;
216       lasthor = (menu->opt & O_NONCYCLIC) ? (ITEM *)0 : hitem;
217       
218       do
219         {
220           _nc_Post_Item( menu, hitem);
221
222           wattron(menu->win,menu->back);
223           if ( ((hitem = hitem->right) != lasthor) && hitem )
224             {
225               int i,j, cy, cx;
226               chtype ch = ' ';
227
228               getyx(menu->win,cy,cx);
229               for(j=0;j<menu->spc_rows;j++)
230                 {
231                   wmove(menu->win,cy+j,cx);
232                   for(i=0; i < menu->spc_cols; i++)
233                     {
234                       waddch( menu->win,ch);
235                     }
236                 }
237               wmove(menu->win,cy,cx+menu->spc_cols);
238             }
239         } while (hitem && (hitem != lasthor));
240       wattroff(menu->win,menu->back);
241       
242       item = item->down;
243       y += menu->spc_rows;
244       
245     } while( item && (item != lastvert) );
246 }
247
248 /*---------------------------------------------------------------------------
249 |   Facility      :  libnmenu  
250 |   Function      :  int post_menu(MENU *)
251 |   
252 |   Description   :  Post a menu to the screen. This makes it visible.
253 |
254 |   Return Values :  E_OK                - success
255 |                    E_BAD_ARGUMENT      - not a valid menu pointer
256 |                    E_SYSTEM_ERROR      - error in lower layers
257 |                    E_NO_ROOM           - Menu to large for screen
258 |                    E_NOT_CONNECTED     - No items connected to menu
259 |                    E_BAD_STATE         - Menu in userexit routine
260 |                    E_POSTED            - Menu already posted
261 +--------------------------------------------------------------------------*/
262 NCURSES_EXPORT(int)
263 post_menu (MENU * menu)
264 {
265   if (!menu)
266     RETURN(E_BAD_ARGUMENT);
267   
268   if ( menu->status & _IN_DRIVER )
269     RETURN(E_BAD_STATE);
270
271   if ( menu->status & _POSTED )
272     RETURN(E_POSTED);
273   
274   if (menu->items && *(menu->items))
275     {
276       int y;
277       int h = 1 + menu->spc_rows * (menu->rows - 1);
278
279       WINDOW *win = Get_Menu_Window(menu);
280       int maxy = getmaxy(win);
281       int maxx = getmaxx(win);
282       
283       if (maxx < menu->width || maxy < menu->height)
284         RETURN(E_NO_ROOM);
285
286       if ( (menu->win = newpad(h,menu->width)) )
287         {
288           y = (maxy >= h) ? h : maxy;
289           if (y>=menu->height) 
290             y = menu->height;
291           if(!(menu->sub = subpad(menu->win,y,menu->width,0,0)))
292             RETURN(E_SYSTEM_ERROR);
293         }
294       else 
295         RETURN(E_SYSTEM_ERROR); 
296       
297       if (menu->status & _LINK_NEEDED) 
298         _nc_Link_Items(menu);
299     }
300   else
301     RETURN(E_NOT_CONNECTED);
302   
303   menu->status |= _POSTED;
304
305   if (!(menu->opt&O_ONEVALUE))
306     {
307       ITEM **items;
308   
309       for(items=menu->items;*items;items++)
310         {
311           (*items)->value = FALSE;
312         }
313     }
314   
315   _nc_Draw_Menu(menu);
316   
317   Call_Hook(menu,menuinit);
318   Call_Hook(menu,iteminit);
319   
320   _nc_Show_Menu(menu);
321   
322   RETURN(E_OK);
323 }
324
325 /*---------------------------------------------------------------------------
326 |   Facility      :  libnmenu  
327 |   Function      :  int unpost_menu(MENU *)
328 |   
329 |   Description   :  Detach menu from screen
330 |
331 |   Return Values :  E_OK              - success
332 |                    E_BAD_ARGUMENT    - not a valid menu pointer
333 |                    E_BAD_STATE       - menu in userexit routine
334 |                    E_NOT_POSTED      - menu is not posted
335 +--------------------------------------------------------------------------*/
336 NCURSES_EXPORT(int)
337 unpost_menu (MENU * menu)
338 {
339   WINDOW *win;
340   
341   if (!menu)
342     RETURN(E_BAD_ARGUMENT);
343   
344   if ( menu->status & _IN_DRIVER )
345     RETURN(E_BAD_STATE);
346
347   if ( !( menu->status & _POSTED ) )
348     RETURN(E_NOT_POSTED);
349   
350   Call_Hook(menu,itemterm);
351   Call_Hook(menu,menuterm);     
352   
353   win = Get_Menu_Window(menu);
354   werase(win);
355   wsyncup(win);
356   
357   assert(menu->sub);
358   delwin(menu->sub);
359   menu->sub = (WINDOW *)0;
360   
361   assert(menu->win);
362   delwin(menu->win);
363   menu->win = (WINDOW *)0;
364   
365   menu->status &= ~_POSTED;
366   
367   RETURN(E_OK);
368 }
369
370 /* m_post.c ends here */