1 /*-----------------------------------------------------------------------------+
2 | The ncurses menu library is Copyright (C) 1995-1997 |
3 | by Juergen Pfeifer <Juergen.Pfeifer@T-Online.de> |
4 | All Rights Reserved. |
6 | Permission to use, copy, modify, and distribute this software and its |
7 | documentation for any purpose and without fee is hereby granted, provided |
8 | that the above copyright notice appear in all copies and that both that |
9 | copyright notice and this permission notice appear in supporting |
10 | documentation, and that the name of the above listed copyright holder(s) not |
11 | be used in advertising or publicity pertaining to distribution of the |
12 | software without specific, written prior permission. |
14 | THE ABOVE LISTED COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD TO |
15 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT- |
16 | NESS, IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR |
17 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RE- |
18 | SULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
19 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH |
20 | THE USE OR PERFORMANCE OF THIS SOFTWARE. |
21 +-----------------------------------------------------------------------------*/
23 /***************************************************************************
24 * Module menu_global *
25 * Globally used internal routines and the default menu and item structures *
26 ***************************************************************************/
28 #include "menu.priv.h"
30 MODULE_ID("$Id: m_global.c,v 1.7 1997/05/01 16:47:26 juergen Exp $")
32 MENU _nc_Default_Menu = {
33 16, /* Nr. of chars high */
34 1, /* Nr. of chars wide */
35 16, /* Nr. of items high */
36 1, /* Nr. of items wide */
37 16, /* Nr. of formatted items high */
38 1, /* Nr. of formatted items wide */
39 16, /* Nr. of items high (actual) */
40 0, /* length of widest name */
41 0, /* length of widest description */
42 1, /* length of mark */
43 1, /* length of one item */
44 1, /* Spacing for descriptor */
45 1, /* Spacing for columns */
46 1, /* Spacing for rows */
47 (char *)0, /* buffer used to store match chars */
48 0, /* Index into pattern buffer */
49 (WINDOW *)0, /* Window containing entire menu */
50 (WINDOW *)0, /* Portion of menu displayed */
51 (WINDOW *)0, /* User's window */
52 (WINDOW *)0, /* User's subwindow */
53 (ITEM **)0, /* List of items */
54 0, /* Total Nr. of items in menu */
55 (ITEM *)0, /* Current item */
56 0, /* Top row of menu */
57 (chtype)A_REVERSE, /* Attribute for selection */
58 (chtype)A_NORMAL, /* Attribute for nonselection */
59 (chtype)A_UNDERLINE, /* Attribute for inactive */
60 ' ', /* Pad character */
61 (Menu_Hook)0, /* Menu init */
62 (Menu_Hook)0, /* Menu term */
63 (Menu_Hook)0, /* Item init */
64 (Menu_Hook)0, /* Item term */
65 (void *)0, /* userptr */
67 ALL_MENU_OPTS, /* options */
71 ITEM _nc_Default_Item = {
72 { (char *)0, 0 }, /* name */
73 { (char *)0, 0 }, /* description */
74 (MENU *)0, /* Pointer to parent menu */
75 (char *)0, /* Userpointer */
76 ALL_ITEM_OPTS, /* options */
82 (ITEM *)0, /* right */
87 /*---------------------------------------------------------------------------
89 | Function : static void ComputeMaximum_NameDesc_Lenths(MENU *menu)
91 | Description : Calculates the maximum name and description lengths
92 | of the items connected to the menu
95 +--------------------------------------------------------------------------*/
96 INLINE static void ComputeMaximum_NameDesc_Lengths(MENU * menu)
98 unsigned MaximumNameLength = 0;
99 unsigned MaximumDescriptionLength = 0;
102 assert(menu && menu->items);
103 for( items = menu->items; *items ; items++ )
105 if (items[0]->name.length > MaximumNameLength )
106 MaximumNameLength = items[0]->name.length;
108 if (items[0]->description.length > MaximumDescriptionLength)
109 MaximumDescriptionLength = items[0]->description.length;
112 menu->namelen = MaximumNameLength;
113 menu->desclen = MaximumDescriptionLength;
116 /*---------------------------------------------------------------------------
117 | Facility : libnmenu
118 | Function : static void ResetConnectionInfo(MENU *, ITEM **)
120 | Description : Reset all informations in the menu and the items in
121 | the item array that indicates a connection
124 +--------------------------------------------------------------------------*/
125 INLINE static void ResetConnectionInfo(MENU *menu, ITEM **items)
129 assert(menu && items);
130 for(item=items; *item; item++)
133 (*item)->imenu = (MENU *)0;
137 menu->pattern = (char *)0;
139 menu->items = (ITEM **)0;
143 /*---------------------------------------------------------------------------
144 | Facility : libnmenu
145 | Function : bool _nc_Connect_Items(MENU *menu, ITEM **items)
147 | Description : Connect the items in the item array to the menu.
148 | Decorate all the items with a number and a backward
149 | pointer to the menu.
151 | Return Values : TRUE - successfull connection
152 | FALSE - connection failed
153 +--------------------------------------------------------------------------*/
154 bool _nc_Connect_Items(MENU *menu, ITEM **items)
157 unsigned int ItemCount = 0;
161 for(item=items; *item ; item++)
163 if ( (*item)->imenu )
165 /* if a item is already connected, reject connection */
170 /* we reached the end, so there was no connected item */
172 for(item=items; *item ; item++)
174 if (menu->opt & O_ONEVALUE)
176 (*item)->value = FALSE;
178 (*item)->index = ItemCount++;
179 (*item)->imenu = menu;
189 menu->nitems = ItemCount;
190 ComputeMaximum_NameDesc_Lengths(menu);
191 if ( (menu->pattern = (char *)malloc( (unsigned)(1 + menu->namelen))) )
194 set_menu_format(menu,menu->frows,menu->fcols);
195 menu->curitem = *items;
201 /* If we fall through to this point, we have to reset all items connection
202 and inform about a reject connection */
203 ResetConnectionInfo( menu, items );
207 /*---------------------------------------------------------------------------
208 | Facility : libnmenu
209 | Function : void _nc_Disconnect_Items(MENU *menu)
211 | Description : Disconnect the menus item array from the menu
214 +--------------------------------------------------------------------------*/
215 void _nc_Disconnect_Items(MENU * menu)
217 if (menu && menu->items)
218 ResetConnectionInfo( menu, menu->items );
221 /*---------------------------------------------------------------------------
222 | Facility : libnmenu
223 | Function : void _nc_Calculate_Item_Length_and_Width(MENU *menu)
225 | Description : Calculate the length of an item and the width of the
229 +--------------------------------------------------------------------------*/
230 void _nc_Calculate_Item_Length_and_Width(MENU * menu)
236 menu->height = 1 + menu->spc_rows * (menu->arows - 1);
238 l = menu->namelen + menu->marklen;
239 if ( (menu->opt & O_SHOWDESC) && (menu->desclen > 0) )
240 l += (menu->desclen + menu->spc_desc);
244 l += (menu->cols-1)*menu->spc_cols; /* for the padding between the columns */
248 /*---------------------------------------------------------------------------
249 | Facility : libnmenu
250 | Function : void _nc_Link_Item(MENU *menu)
252 | Description : Statically calculate for every item its four neighbours.
253 | This depends on the orientation of the menu. This
254 | static aproach simplifies navigation in the menu a lot.
257 +--------------------------------------------------------------------------*/
258 void _nc_Link_Items(MENU * menu)
260 if (menu && menu->items && *(menu->items))
264 int Number_Of_Items = menu->nitems;
265 int col = 0, row = 0;
268 bool cycle = (menu->opt & O_NONCYCLIC) ? FALSE : TRUE;
270 menu->status &= ~_LINK_NEEDED;
272 if (menu->opt & O_ROWMAJOR)
274 int Number_Of_Columns = menu->cols;
276 for(i=0; i < Number_Of_Items; i++)
278 item = menu->items[i];
280 Last_in_Row = row * Number_Of_Columns + (Number_Of_Columns-1);
283 /* if we are not in the leftmost column, we can use the
284 predecessor in the items array */
286 (cycle ? menu->items[(Last_in_Row>=Number_Of_Items) ?
291 item->right = ( (col < (Number_Of_Columns-1)) &&
292 ((i+1) < Number_Of_Items)
295 ( cycle ? menu->items[row * Number_Of_Columns] :
299 Last_in_Column = (menu->rows-1) * Number_Of_Columns + col;
301 item->up = (row) ? menu->items[i-Number_Of_Columns] :
302 (cycle ? menu->items[(Last_in_Column>=Number_Of_Items) ?
307 item->down = ( (i+Number_Of_Columns) < Number_Of_Items )
309 menu->items[i + Number_Of_Columns] :
310 (cycle ? menu->items[(row+1)<menu->rows ?
311 Number_Of_Items-1:col] :
315 if ( ++col == Number_Of_Columns )
324 int Number_Of_Rows = menu->rows;
326 for(j=0; j<Number_Of_Items; j++)
328 item = menu->items[i=(col * Number_Of_Rows + row)];
330 Last_in_Column = (menu->cols-1) * Number_Of_Rows + row;
333 menu->items[i - Number_Of_Rows] :
334 (cycle ? (Last_in_Column >= Number_Of_Items ) ?
335 menu->items[Last_in_Column-Number_Of_Rows] :
336 menu->items[Last_in_Column] :
339 item->right = ((i + Number_Of_Rows) <Number_Of_Items)
341 menu->items[i + Number_Of_Rows] :
342 (cycle ? menu->items[row] : (ITEM *)0);
344 Last_in_Row = col * Number_Of_Rows + (Number_Of_Rows - 1);
349 menu->items[(Last_in_Row>=Number_Of_Items) ?
354 item->down = (row < (Number_Of_Rows-1))
356 (menu->items[((i+1)<Number_Of_Items) ?
358 (col-1)*Number_Of_Rows + row + 1]) :
360 menu->items[col * Number_Of_Rows] :
366 if ( (++row) == Number_Of_Rows )
376 /*---------------------------------------------------------------------------
377 | Facility : libnmenu
378 | Function : void _nc_Show_Menu(const MENU *menu)
380 | Description : Update the window that is associated with the menu
383 +--------------------------------------------------------------------------*/
384 void _nc_Show_Menu(const MENU *menu)
390 if ( (menu->status & _POSTED) && !(menu->status & _IN_DRIVER) )
392 /* adjust the internal subwindow to start on the current top */
394 mvderwin(menu->sub,menu->spc_rows * menu->toprow,0);
395 win = Get_Menu_Window(menu);
400 if (menu->height < maxy)
402 if (menu->width < maxx)
405 copywin(menu->sub,win,0,0,0,0,maxy-1,maxx-1,0);
406 pos_menu_cursor(menu);
410 /*---------------------------------------------------------------------------
411 | Facility : libnmenu
412 | Function : void _nc_New_TopRow_and_CurrentItem(
415 | ITEM *new_current_item)
417 | Description : Redisplay the menu so that the given row becomes the
418 | top row and the given item becomes the new current
422 +--------------------------------------------------------------------------*/
423 void _nc_New_TopRow_and_CurrentItem(MENU *menu, int new_toprow,
424 ITEM *new_current_item)
427 bool mterm_called = FALSE;
428 bool iterm_called = FALSE;
431 if (menu->status & _POSTED)
433 if (new_current_item != menu->curitem)
435 Call_Hook(menu,itemterm);
438 if (new_toprow != menu->toprow)
440 Call_Hook(menu,menuterm);
444 cur_item = menu->curitem;
446 menu->toprow = new_toprow;
447 menu->curitem = new_current_item;
451 Call_Hook(menu,menuinit);
455 /* this means, move from the old current_item to the new one... */
456 Move_To_Current_Item( menu, cur_item );
457 Call_Hook(menu,iteminit);
459 if (mterm_called || iterm_called)
464 pos_menu_cursor(menu);
467 { /* if we are not posted, this is quite simple */
468 menu->toprow = new_toprow;
469 menu->curitem = new_current_item;
473 /* m_global.c ends here */