1 /****************************************************************************
2 * Copyright (c) 1998,2000 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Juergen Pfeifer, 1995,1997 *
31 * Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en *
32 ****************************************************************************/
34 /***************************************************************************
36 * Globally used internal routines and the default menu and item structures *
37 ***************************************************************************/
39 #include "menu.priv.h"
41 MODULE_ID("$Id: m_global.c,v 1.14 2002/07/06 15:22:16 juergen Exp $")
43 static char mark[] = "-";
45 NCURSES_EXPORT_VAR(MENU) _nc_Default_Menu = {
46 16, /* Nr. of chars high */
47 1, /* Nr. of chars wide */
48 16, /* Nr. of items high */
49 1, /* Nr. of items wide */
50 16, /* Nr. of formatted items high */
51 1, /* Nr. of formatted items wide */
52 16, /* Nr. of items high (actual) */
53 0, /* length of widest name */
54 0, /* length of widest description */
55 1, /* length of mark */
56 1, /* length of one item */
57 1, /* Spacing for descriptor */
58 1, /* Spacing for columns */
59 1, /* Spacing for rows */
60 (char *)0, /* buffer used to store match chars */
61 0, /* Index into pattern buffer */
62 (WINDOW *)0, /* Window containing entire menu */
63 (WINDOW *)0, /* Portion of menu displayed */
64 (WINDOW *)0, /* User's window */
65 (WINDOW *)0, /* User's subwindow */
66 (ITEM **)0, /* List of items */
67 0, /* Total Nr. of items in menu */
68 (ITEM *)0, /* Current item */
69 0, /* Top row of menu */
70 (chtype)A_REVERSE, /* Attribute for selection */
71 (chtype)A_NORMAL, /* Attribute for nonselection */
72 (chtype)A_UNDERLINE, /* Attribute for inactive */
73 ' ', /* Pad character */
74 (Menu_Hook)0, /* Menu init */
75 (Menu_Hook)0, /* Menu term */
76 (Menu_Hook)0, /* Item init */
77 (Menu_Hook)0, /* Item term */
78 (void *)0, /* userptr */
80 ALL_MENU_OPTS, /* options */
84 NCURSES_EXPORT_VAR(ITEM) _nc_Default_Item = {
85 { (char *)0, 0 }, /* name */
86 { (char *)0, 0 }, /* description */
87 (MENU *)0, /* Pointer to parent menu */
88 (char *)0, /* Userpointer */
89 ALL_ITEM_OPTS, /* options */
95 (ITEM *)0, /* right */
100 /*---------------------------------------------------------------------------
101 | Facility : libnmenu
102 | Function : static void ComputeMaximum_NameDesc_Lenths(MENU *menu)
104 | Description : Calculates the maximum name and description lengths
105 | of the items connected to the menu
108 +--------------------------------------------------------------------------*/
109 INLINE static void ComputeMaximum_NameDesc_Lengths(MENU * menu)
111 unsigned MaximumNameLength = 0;
112 unsigned MaximumDescriptionLength = 0;
115 assert(menu && menu->items);
116 for( items = menu->items; *items ; items++ )
118 if (items[0]->name.length > MaximumNameLength )
119 MaximumNameLength = items[0]->name.length;
121 if (items[0]->description.length > MaximumDescriptionLength)
122 MaximumDescriptionLength = items[0]->description.length;
125 menu->namelen = MaximumNameLength;
126 menu->desclen = MaximumDescriptionLength;
129 /*---------------------------------------------------------------------------
130 | Facility : libnmenu
131 | Function : static void ResetConnectionInfo(MENU *, ITEM **)
133 | Description : Reset all informations in the menu and the items in
134 | the item array that indicates a connection
137 +--------------------------------------------------------------------------*/
138 INLINE static void ResetConnectionInfo(MENU *menu, ITEM **items)
142 assert(menu && items);
143 for(item=items; *item; item++)
146 (*item)->imenu = (MENU *)0;
150 menu->pattern = (char *)0;
152 menu->items = (ITEM **)0;
156 /*---------------------------------------------------------------------------
157 | Facility : libnmenu
158 | Function : bool _nc_Connect_Items(MENU *menu, ITEM **items)
160 | Description : Connect the items in the item array to the menu.
161 | Decorate all the items with a number and a backward
162 | pointer to the menu.
164 | Return Values : TRUE - successfull connection
165 | FALSE - connection failed
166 +--------------------------------------------------------------------------*/
168 _nc_Connect_Items (MENU *menu, ITEM **items)
171 unsigned int ItemCount = 0;
175 for(item=items; *item ; item++)
177 if ( (*item)->imenu )
179 /* if a item is already connected, reject connection */
184 /* we reached the end, so there was no connected item */
186 for(item=items; *item ; item++)
188 if (menu->opt & O_ONEVALUE)
190 (*item)->value = FALSE;
192 (*item)->index = ItemCount++;
193 (*item)->imenu = menu;
203 menu->nitems = ItemCount;
204 ComputeMaximum_NameDesc_Lengths(menu);
205 if ( (menu->pattern = (char *)malloc( (unsigned)(1 + menu->namelen))) )
208 set_menu_format(menu,menu->frows,menu->fcols);
209 menu->curitem = *items;
215 /* If we fall through to this point, we have to reset all items connection
216 and inform about a reject connection */
217 ResetConnectionInfo( menu, items );
221 /*---------------------------------------------------------------------------
222 | Facility : libnmenu
223 | Function : void _nc_Disconnect_Items(MENU *menu)
225 | Description : Disconnect the menus item array from the menu
228 +--------------------------------------------------------------------------*/
230 _nc_Disconnect_Items (MENU * menu)
232 if (menu && menu->items)
233 ResetConnectionInfo( menu, menu->items );
236 /*---------------------------------------------------------------------------
237 | Facility : libnmenu
238 | Function : void _nc_Calculate_Item_Length_and_Width(MENU *menu)
240 | Description : Calculate the length of an item and the width of the
244 +--------------------------------------------------------------------------*/
246 _nc_Calculate_Item_Length_and_Width (MENU * menu)
252 menu->height = 1 + menu->spc_rows * (menu->arows - 1);
254 l = menu->namelen + menu->marklen;
255 if ( (menu->opt & O_SHOWDESC) && (menu->desclen > 0) )
256 l += (menu->desclen + menu->spc_desc);
260 l += (menu->cols-1)*menu->spc_cols; /* for the padding between the columns */
264 /*---------------------------------------------------------------------------
265 | Facility : libnmenu
266 | Function : void _nc_Link_Item(MENU *menu)
268 | Description : Statically calculate for every item its four neighbours.
269 | This depends on the orientation of the menu. This
270 | static aproach simplifies navigation in the menu a lot.
273 +--------------------------------------------------------------------------*/
275 _nc_Link_Items (MENU * menu)
277 if (menu && menu->items && *(menu->items))
281 int Number_Of_Items = menu->nitems;
282 int col = 0, row = 0;
285 bool cycle = (menu->opt & O_NONCYCLIC) ? FALSE : TRUE;
287 menu->status &= ~_LINK_NEEDED;
289 if (menu->opt & O_ROWMAJOR)
291 int Number_Of_Columns = menu->cols;
293 for(i=0; i < Number_Of_Items; i++)
295 item = menu->items[i];
297 Last_in_Row = row * Number_Of_Columns + (Number_Of_Columns-1);
300 /* if we are not in the leftmost column, we can use the
301 predecessor in the items array */
303 (cycle ? menu->items[(Last_in_Row>=Number_Of_Items) ?
308 item->right = ( (col < (Number_Of_Columns-1)) &&
309 ((i+1) < Number_Of_Items)
312 ( cycle ? menu->items[row * Number_Of_Columns] :
316 Last_in_Column = (menu->rows-1) * Number_Of_Columns + col;
318 item->up = (row) ? menu->items[i-Number_Of_Columns] :
319 (cycle ? menu->items[(Last_in_Column>=Number_Of_Items) ?
324 item->down = ( (i+Number_Of_Columns) < Number_Of_Items )
326 menu->items[i + Number_Of_Columns] :
327 (cycle ? menu->items[(row+1)<menu->rows ?
328 Number_Of_Items-1:col] :
332 if ( ++col == Number_Of_Columns )
341 int Number_Of_Rows = menu->rows;
343 for(j=0; j<Number_Of_Items; j++)
345 item = menu->items[i=(col * Number_Of_Rows + row)];
347 Last_in_Column = (menu->cols-1) * Number_Of_Rows + row;
350 menu->items[i - Number_Of_Rows] :
351 (cycle ? (Last_in_Column >= Number_Of_Items ) ?
352 menu->items[Last_in_Column-Number_Of_Rows] :
353 menu->items[Last_in_Column] :
356 item->right = ((i + Number_Of_Rows) <Number_Of_Items)
358 menu->items[i + Number_Of_Rows] :
359 (cycle ? menu->items[row] : (ITEM *)0);
361 Last_in_Row = col * Number_Of_Rows + (Number_Of_Rows - 1);
366 menu->items[(Last_in_Row>=Number_Of_Items) ?
371 item->down = (row < (Number_Of_Rows-1))
373 (menu->items[((i+1)<Number_Of_Items) ?
375 (col-1)*Number_Of_Rows + row + 1]) :
377 menu->items[col * Number_Of_Rows] :
383 if ( (++row) == Number_Of_Rows )
393 /*---------------------------------------------------------------------------
394 | Facility : libnmenu
395 | Function : void _nc_Show_Menu(const MENU *menu)
397 | Description : Update the window that is associated with the menu
400 +--------------------------------------------------------------------------*/
402 _nc_Show_Menu (const MENU *menu)
408 if ( (menu->status & _POSTED) && !(menu->status & _IN_DRIVER) )
410 /* adjust the internal subwindow to start on the current top */
412 mvderwin(menu->sub,menu->spc_rows * menu->toprow,0);
413 win = Get_Menu_Window(menu);
418 if (menu->height < maxy)
420 if (menu->width < maxx)
423 copywin(menu->sub,win,0,0,0,0,maxy-1,maxx-1,0);
424 pos_menu_cursor(menu);
428 /*---------------------------------------------------------------------------
429 | Facility : libnmenu
430 | Function : void _nc_New_TopRow_and_CurrentItem(
433 | ITEM *new_current_item)
435 | Description : Redisplay the menu so that the given row becomes the
436 | top row and the given item becomes the new current
440 +--------------------------------------------------------------------------*/
442 _nc_New_TopRow_and_CurrentItem
443 (MENU *menu, int new_toprow, ITEM *new_current_item)
446 bool mterm_called = FALSE;
447 bool iterm_called = FALSE;
450 if (menu->status & _POSTED)
452 if (new_current_item != menu->curitem)
454 Call_Hook(menu,itemterm);
457 if (new_toprow != menu->toprow)
459 Call_Hook(menu,menuterm);
463 cur_item = menu->curitem;
465 menu->toprow = new_toprow;
466 menu->curitem = new_current_item;
470 Call_Hook(menu,menuinit);
474 /* this means, move from the old current_item to the new one... */
475 Move_To_Current_Item( menu, cur_item );
476 Call_Hook(menu,iteminit);
478 if (mterm_called || iterm_called)
483 pos_menu_cursor(menu);
486 { /* if we are not posted, this is quite simple */
487 menu->toprow = new_toprow;
488 menu->curitem = new_current_item;
492 /* m_global.c ends here */