1 /****************************************************************************
2  * Copyright (c) 1998-2004,2005 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_global                                                          *
35 * Globally used internal routines and the default menu and item structures *
36 ***************************************************************************/
37 
38 #include "menu.priv.h"
39 
40 MODULE_ID("$Id: m_global.c,v 1.20 2005/04/16 17:30:57 tom Exp $")
41 
42 static char mark[] = "-";
43 /* *INDENT-OFF* */
44 NCURSES_EXPORT_VAR(MENU) _nc_Default_Menu = {
45   16,				  /* Nr. of chars high */
46   1,				  /* Nr. of chars wide */
47   16,				  /* Nr. of items high */
48   1,			          /* Nr. of items wide */
49   16,				  /* Nr. of formatted items high */
50   1,				  /* Nr. of formatted items wide */
51   16,				  /* Nr. of items high (actual) */
52   0,				  /* length of widest name */
53   0,				  /* length of widest description */
54   1,				  /* length of mark */
55   1,				  /* length of one item */
56   1,                              /* Spacing for descriptor */
57   1,                              /* Spacing for columns */
58   1,                              /* Spacing for rows */
59   (char *)0,			  /* buffer used to store match chars */
60   0,				  /* Index into pattern buffer */
61   (WINDOW *)0,			  /* Window containing entire menu */
62   (WINDOW *)0,			  /* Portion of menu displayed */
63   (WINDOW *)0,			  /* User's window */
64   (WINDOW *)0,			  /* User's subwindow */
65   (ITEM **)0,			  /* List of items */
66   0,				  /* Total Nr. of items in menu */
67   (ITEM *)0,			  /* Current item */
68   0,				  /* Top row of menu */
69   (chtype)A_REVERSE,		  /* Attribute for selection */
70   (chtype)A_NORMAL,		  /* Attribute for nonselection */
71   (chtype)A_UNDERLINE,		  /* Attribute for inactive */
72   ' ',  			  /* Pad character */
73   (Menu_Hook)0,			  /* Menu init */
74   (Menu_Hook)0,			  /* Menu term */
75   (Menu_Hook)0,			  /* Item init */
76   (Menu_Hook)0,			  /* Item term */
77   (void *)0,			  /* userptr */
78   mark,				  /* mark */
79   ALL_MENU_OPTS,                  /* options */
80   0			          /* status */
81 };
82 
83 NCURSES_EXPORT_VAR(ITEM) _nc_Default_Item = {
84   { (char *)0, 0 },		  /* name */
85   { (char *)0, 0 },		  /* description */
86   (MENU *)0,		          /* Pointer to parent menu */
87   (char *)0,			  /* Userpointer */
88   ALL_ITEM_OPTS,		  /* options */
89   0,				  /* Item Nr. */
90   0,				  /* y */
91   0,				  /* x */
92   FALSE,			  /* value */
93   (ITEM *)0,		          /* left */
94   (ITEM *)0,		          /* right */
95   (ITEM *)0,		          /* up */
96   (ITEM *)0		          /* down */
97   };
98 /* *INDENT-ON* */
99 
100 /*---------------------------------------------------------------------------
101 |   Facility      :  libnmenu
102 |   Function      :  static void ComputeMaximum_NameDesc_Lenths(MENU *menu)
103 |
104 |   Description   :  Calculates the maximum name and description lengths
105 |                    of the items connected to the menu
106 |
107 |   Return Values :  -
108 +--------------------------------------------------------------------------*/
109 INLINE static void
ComputeMaximum_NameDesc_Lengths(MENU * menu)110 ComputeMaximum_NameDesc_Lengths(MENU * menu)
111 {
112   unsigned MaximumNameLength = 0;
113   unsigned MaximumDescriptionLength = 0;
114   ITEM **items;
115 
116   assert(menu && menu->items);
117   for (items = menu->items; *items; items++)
118     {
119       if (items[0]->name.length > MaximumNameLength)
120 	MaximumNameLength = items[0]->name.length;
121 
122       if (items[0]->description.length > MaximumDescriptionLength)
123 	MaximumDescriptionLength = items[0]->description.length;
124     }
125 
126   menu->namelen = MaximumNameLength;
127   menu->desclen = MaximumDescriptionLength;
128   T(("ComputeMaximum_NameDesc_Lengths %d,%d", menu->namelen, menu->desclen));
129 }
130 
131 /*---------------------------------------------------------------------------
132 |   Facility      :  libnmenu
133 |   Function      :  static void ResetConnectionInfo(MENU *, ITEM **)
134 |
135 |   Description   :  Reset all informations in the menu and the items in
136 |                    the item array that indicates a connection
137 |
138 |   Return Values :  -
139 +--------------------------------------------------------------------------*/
140 INLINE static void
ResetConnectionInfo(MENU * menu,ITEM ** items)141 ResetConnectionInfo(MENU * menu, ITEM ** items)
142 {
143   ITEM **item;
144 
145   assert(menu && items);
146   for (item = items; *item; item++)
147     {
148       (*item)->index = 0;
149       (*item)->imenu = (MENU *) 0;
150     }
151   if (menu->pattern)
152     free(menu->pattern);
153   menu->pattern = (char *)0;
154   menu->pindex = 0;
155   menu->items = (ITEM **) 0;
156   menu->nitems = 0;
157 }
158 
159 /*---------------------------------------------------------------------------
160 |   Facility      :  libnmenu
161 |   Function      :  bool _nc_Connect_Items(MENU *menu, ITEM **items)
162 |
163 |   Description   :  Connect the items in the item array to the menu.
164 |                    Decorate all the items with a number and a backward
165 |                    pointer to the menu.
166 |
167 |   Return Values :  TRUE       - successful connection
168 |                    FALSE      - connection failed
169 +--------------------------------------------------------------------------*/
170 NCURSES_EXPORT(bool)
_nc_Connect_Items(MENU * menu,ITEM ** items)171 _nc_Connect_Items(MENU * menu, ITEM ** items)
172 {
173   ITEM **item;
174   unsigned int ItemCount = 0;
175 
176   if (menu && items)
177     {
178       for (item = items; *item; item++)
179 	{
180 	  if ((*item)->imenu)
181 	    {
182 	      /* if a item is already connected, reject connection */
183 	      break;
184 	    }
185 	}
186       if (!(*item))
187 	/* we reached the end, so there was no connected item */
188 	{
189 	  for (item = items; *item; item++)
190 	    {
191 	      if (menu->opt & O_ONEVALUE)
192 		{
193 		  (*item)->value = FALSE;
194 		}
195 	      (*item)->index = ItemCount++;
196 	      (*item)->imenu = menu;
197 	    }
198 	}
199     }
200   else
201     return (FALSE);
202 
203   if (ItemCount != 0)
204     {
205       menu->items = items;
206       menu->nitems = ItemCount;
207       ComputeMaximum_NameDesc_Lengths(menu);
208       if ((menu->pattern = typeMalloc(char, (unsigned)(1 + menu->namelen))))
209 	{
210 	  Reset_Pattern(menu);
211 	  set_menu_format(menu, menu->frows, menu->fcols);
212 	  menu->curitem = *items;
213 	  menu->toprow = 0;
214 	  return (TRUE);
215 	}
216     }
217 
218   /* If we fall through to this point, we have to reset all items connection
219      and inform about a reject connection */
220   ResetConnectionInfo(menu, items);
221   return (FALSE);
222 }
223 
224 /*---------------------------------------------------------------------------
225 |   Facility      :  libnmenu
226 |   Function      :  void _nc_Disconnect_Items(MENU *menu)
227 |
228 |   Description   :  Disconnect the menus item array from the menu
229 |
230 |   Return Values :  -
231 +--------------------------------------------------------------------------*/
232 NCURSES_EXPORT(void)
_nc_Disconnect_Items(MENU * menu)233 _nc_Disconnect_Items(MENU * menu)
234 {
235   if (menu && menu->items)
236     ResetConnectionInfo(menu, menu->items);
237 }
238 
239 /*---------------------------------------------------------------------------
240 |   Facility      :  libnmenu
241 |   Function      :  int _nc_Calculate_Text_Width(const TEXT * item)
242 |
243 |   Description   :  Calculate the number of columns for a TEXT.
244 |
245 |   Return Values :  the width
246 +--------------------------------------------------------------------------*/
247 NCURSES_EXPORT(int)
_nc_Calculate_Text_Width(const TEXT * item)248 _nc_Calculate_Text_Width(const TEXT * item /*FIXME: limit length */ )
249 {
250 #if USE_WIDEC_SUPPORT
251   int result = item->length;
252   int count = mbstowcs(0, item->str, 0);
253   wchar_t *temp = 0;
254 
255   T((T_CALLED("_nc_menu_text_width(%p)"), item));
256   if (count > 0
257       && (temp = typeMalloc(wchar_t, 2 + count)) != 0)
258     {
259       int n;
260 
261       result = 0;
262       mbstowcs(temp, item->str, (unsigned)count);
263       for (n = 0; n < count; ++n)
264 	{
265 	  int test = wcwidth(temp[n]);
266 
267 	  if (test <= 0)
268 	    test = 1;
269 	  result += test;
270 	}
271       free(temp);
272     }
273   returnCode(result);
274 #else
275   return item->length;
276 #endif
277 }
278 
279 /* FIXME: this is experimental, should cache the results but don't want to
280  * modify the MENU struct to do this until it's complete.
281  */
282 #if 0				/* USE_WIDEC_SUPPORT */
283 static int
284 calculate_actual_width(MENU * menu, bool name)
285 {
286   int width = 0;
287   int check = 0;
288   ITEM **items;
289 
290   assert(menu && menu->items);
291   for (items = menu->items; *items; items++)
292     {
293       if (name)
294 	{
295 	  check = _nc_Calculate_Text_Width(&((*items)->name));
296 	}
297       else
298 	{
299 	  check = _nc_Calculate_Text_Width(&((*items)->description));
300 	}
301       if (check > width)
302 	width = check;
303     }
304 
305   T(("calculate_actual_width %s = %d/%d",
306      name ? "name" : "desc",
307      width,
308      name ? menu->namelen : menu->desclen));
309   width += 2;			/* FIXME - need this? */
310   return width;
311 }
312 #else
313 #define calculate_actual_width(menu, name) (name ? menu->namelen : menu->desclen)
314 #endif
315 
316 /*---------------------------------------------------------------------------
317 |   Facility      :  libnmenu
318 |   Function      :  void _nc_Calculate_Item_Length_and_Width(MENU *menu)
319 |
320 |   Description   :  Calculate the length of an item and the width of the
321 |                    whole menu.
322 |
323 |   Return Values :  -
324 +--------------------------------------------------------------------------*/
325 NCURSES_EXPORT(void)
_nc_Calculate_Item_Length_and_Width(MENU * menu)326 _nc_Calculate_Item_Length_and_Width(MENU * menu)
327 {
328   int l;
329 
330   assert(menu);
331 
332   menu->height = 1 + menu->spc_rows * (menu->arows - 1);
333 
334   l = calculate_actual_width(menu, TRUE);
335   l += menu->marklen;
336 
337   if ((menu->opt & O_SHOWDESC) && (menu->desclen > 0))
338     {
339       l += calculate_actual_width(menu, FALSE);
340       l += menu->spc_desc;
341     }
342 
343   menu->itemlen = l;
344   l *= menu->cols;
345   l += (menu->cols - 1) * menu->spc_cols;	/* for the padding between the columns */
346   menu->width = l;
347 
348   T(("_nc_CalculateItem_Length_and_Width columns %d, item %d, width %d",
349      menu->cols,
350      menu->itemlen,
351      menu->width));
352 }
353 
354 /*---------------------------------------------------------------------------
355 |   Facility      :  libnmenu
356 |   Function      :  void _nc_Link_Item(MENU *menu)
357 |
358 |   Description   :  Statically calculate for every item its four neighbors.
359 |                    This depends on the orientation of the menu. This
360 |                    static approach simplifies navigation in the menu a lot.
361 |
362 |   Return Values :  -
363 +--------------------------------------------------------------------------*/
364 NCURSES_EXPORT(void)
_nc_Link_Items(MENU * menu)365 _nc_Link_Items(MENU * menu)
366 {
367   if (menu && menu->items && *(menu->items))
368     {
369       int i, j;
370       ITEM *item;
371       int Number_Of_Items = menu->nitems;
372       int col = 0, row = 0;
373       int Last_in_Row;
374       int Last_in_Column;
375       bool cycle = (menu->opt & O_NONCYCLIC) ? FALSE : TRUE;
376 
377       menu->status &= ~_LINK_NEEDED;
378 
379       if (menu->opt & O_ROWMAJOR)
380 	{
381 	  int Number_Of_Columns = menu->cols;
382 
383 	  for (i = 0; i < Number_Of_Items; i++)
384 	    {
385 	      item = menu->items[i];
386 
387 	      Last_in_Row = row * Number_Of_Columns + (Number_Of_Columns - 1);
388 
389 	      item->left = (col) ?
390 	      /* if we are not in the leftmost column, we can use the
391 	         predecessor in the items array */
392 		menu->items[i - 1] :
393 		(cycle ? menu->items[(Last_in_Row >= Number_Of_Items) ?
394 				     Number_Of_Items - 1 :
395 				     Last_in_Row] :
396 		 (ITEM *) 0);
397 
398 	      item->right = ((col < (Number_Of_Columns - 1)) &&
399 			     ((i + 1) < Number_Of_Items)
400 		)?
401 		menu->items[i + 1] :
402 		(cycle ? menu->items[row * Number_Of_Columns] :
403 		 (ITEM *) 0
404 		);
405 
406 	      Last_in_Column = (menu->rows - 1) * Number_Of_Columns + col;
407 
408 	      item->up = (row) ? menu->items[i - Number_Of_Columns] :
409 		(cycle ? menu->items[(Last_in_Column >= Number_Of_Items) ?
410 				     Number_Of_Items - 1 :
411 				     Last_in_Column] :
412 		 (ITEM *) 0);
413 
414 	      item->down = ((i + Number_Of_Columns) < Number_Of_Items)
415 		?
416 		menu->items[i + Number_Of_Columns] :
417 		(cycle ? menu->items[(row + 1) < menu->rows ?
418 				     Number_Of_Items - 1 : col] :
419 		 (ITEM *) 0);
420 	      item->x = col;
421 	      item->y = row;
422 	      if (++col == Number_Of_Columns)
423 		{
424 		  row++;
425 		  col = 0;
426 		}
427 	    }
428 	}
429       else
430 	{
431 	  int Number_Of_Rows = menu->rows;
432 
433 	  for (j = 0; j < Number_Of_Items; j++)
434 	    {
435 	      item = menu->items[i = (col * Number_Of_Rows + row)];
436 
437 	      Last_in_Column = (menu->cols - 1) * Number_Of_Rows + row;
438 
439 	      item->left = (col) ?
440 		menu->items[i - Number_Of_Rows] :
441 		(cycle ? (Last_in_Column >= Number_Of_Items) ?
442 		 menu->items[Last_in_Column - Number_Of_Rows] :
443 		 menu->items[Last_in_Column] :
444 		 (ITEM *) 0);
445 
446 	      item->right = ((i + Number_Of_Rows) < Number_Of_Items)
447 		?
448 		menu->items[i + Number_Of_Rows] :
449 		(cycle ? menu->items[row] : (ITEM *) 0);
450 
451 	      Last_in_Row = col * Number_Of_Rows + (Number_Of_Rows - 1);
452 
453 	      item->up = (row) ?
454 		menu->items[i - 1] :
455 		(cycle ?
456 		 menu->items[(Last_in_Row >= Number_Of_Items) ?
457 			     Number_Of_Items - 1 :
458 			     Last_in_Row] :
459 		 (ITEM *) 0);
460 
461 	      item->down = (row < (Number_Of_Rows - 1))
462 		?
463 		(menu->items[((i + 1) < Number_Of_Items) ?
464 			     i + 1 :
465 			     (col - 1) * Number_Of_Rows + row + 1]) :
466 		(cycle ?
467 		 menu->items[col * Number_Of_Rows] :
468 		 (ITEM *) 0
469 		);
470 
471 	      item->x = col;
472 	      item->y = row;
473 	      if ((++row) == Number_Of_Rows)
474 		{
475 		  col++;
476 		  row = 0;
477 		}
478 	    }
479 	}
480     }
481 }
482 
483 /*---------------------------------------------------------------------------
484 |   Facility      :  libnmenu
485 |   Function      :  void _nc_Show_Menu(const MENU *menu)
486 |
487 |   Description   :  Update the window that is associated with the menu
488 |
489 |   Return Values :  -
490 +--------------------------------------------------------------------------*/
491 NCURSES_EXPORT(void)
_nc_Show_Menu(const MENU * menu)492 _nc_Show_Menu(const MENU * menu)
493 {
494   WINDOW *win;
495   int maxy, maxx;
496 
497   assert(menu);
498   if ((menu->status & _POSTED) && !(menu->status & _IN_DRIVER))
499     {
500       /* adjust the internal subwindow to start on the current top */
501       assert(menu->sub);
502       mvderwin(menu->sub, menu->spc_rows * menu->toprow, 0);
503       win = Get_Menu_Window(menu);
504 
505       maxy = getmaxy(win);
506       maxx = getmaxx(win);
507 
508       if (menu->height < maxy)
509 	maxy = menu->height;
510       if (menu->width < maxx)
511 	maxx = menu->width;
512 
513       copywin(menu->sub, win, 0, 0, 0, 0, maxy - 1, maxx - 1, 0);
514       pos_menu_cursor(menu);
515     }
516 }
517 
518 /*---------------------------------------------------------------------------
519 |   Facility      :  libnmenu
520 |   Function      :  void _nc_New_TopRow_and_CurrentItem(
521 |                            MENU *menu,
522 |                            int new_toprow,
523 |                            ITEM *new_current_item)
524 |
525 |   Description   :  Redisplay the menu so that the given row becomes the
526 |                    top row and the given item becomes the new current
527 |                    item.
528 |
529 |   Return Values :  -
530 +--------------------------------------------------------------------------*/
531 NCURSES_EXPORT(void)
_nc_New_TopRow_and_CurrentItem(MENU * menu,int new_toprow,ITEM * new_current_item)532   _nc_New_TopRow_and_CurrentItem
533   (MENU * menu, int new_toprow, ITEM * new_current_item)
534 {
535   ITEM *cur_item;
536   bool mterm_called = FALSE;
537   bool iterm_called = FALSE;
538 
539   assert(menu);
540   if (menu->status & _POSTED)
541     {
542       if (new_current_item != menu->curitem)
543 	{
544 	  Call_Hook(menu, itemterm);
545 	  iterm_called = TRUE;
546 	}
547       if (new_toprow != menu->toprow)
548 	{
549 	  Call_Hook(menu, menuterm);
550 	  mterm_called = TRUE;
551 	}
552 
553       cur_item = menu->curitem;
554       assert(cur_item);
555       menu->toprow = new_toprow;
556       menu->curitem = new_current_item;
557 
558       if (mterm_called)
559 	{
560 	  Call_Hook(menu, menuinit);
561 	}
562       if (iterm_called)
563 	{
564 	  /* this means, move from the old current_item to the new one... */
565 	  Move_To_Current_Item(menu, cur_item);
566 	  Call_Hook(menu, iteminit);
567 	}
568       if (mterm_called || iterm_called)
569 	{
570 	  _nc_Show_Menu(menu);
571 	}
572       else
573 	pos_menu_cursor(menu);
574     }
575   else
576     {				/* if we are not posted, this is quite simple */
577       menu->toprow = new_toprow;
578       menu->curitem = new_current_item;
579     }
580 }
581 
582 /* m_global.c ends here */
583