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