]> ncurses.scripts.mit.edu Git - ncurses.git/blob - menu/m_global.c
ncurses 6.0 - patch 20160521
[ncurses.git] / menu / m_global.c
1 /****************************************************************************
2  * Copyright (c) 1998-2012,2014 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.28 2014/03/15 20:37:22 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 NCURSES_INLINE static void
110 ComputeMaximum_NameDesc_Lengths(MENU * menu)
111 {
112   unsigned MaximumNameLength = 0;
113   unsigned MaximumDescriptionLength = 0;
114   ITEM **items;
115   unsigned check;
116
117   assert(menu && menu->items);
118   for (items = menu->items; *items; items++)
119     {
120       check = (unsigned)_nc_Calculate_Text_Width(&((*items)->name));
121       if (check > MaximumNameLength)
122         MaximumNameLength = check;
123
124       check = (unsigned)_nc_Calculate_Text_Width(&((*items)->description));
125       if (check > MaximumDescriptionLength)
126         MaximumDescriptionLength = check;
127     }
128
129   menu->namelen = (short)MaximumNameLength;
130   menu->desclen = (short)MaximumDescriptionLength;
131   T(("ComputeMaximum_NameDesc_Lengths %d,%d", menu->namelen, menu->desclen));
132 }
133
134 /*---------------------------------------------------------------------------
135 |   Facility      :  libnmenu  
136 |   Function      :  static void ResetConnectionInfo(MENU *, ITEM **)
137 |   
138 |   Description   :  Reset all informations in the menu and the items in
139 |                    the item array that indicates a connection
140 |
141 |   Return Values :  -
142 +--------------------------------------------------------------------------*/
143 NCURSES_INLINE static void
144 ResetConnectionInfo(MENU * menu, ITEM ** items)
145 {
146   ITEM **item;
147
148   assert(menu && items);
149   for (item = items; *item; item++)
150     {
151       (*item)->index = 0;
152       (*item)->imenu = (MENU *) 0;
153     }
154   if (menu->pattern)
155     free(menu->pattern);
156   menu->pattern = (char *)0;
157   menu->pindex = 0;
158   menu->items = (ITEM **) 0;
159   menu->nitems = 0;
160 }
161
162 /*---------------------------------------------------------------------------
163 |   Facility      :  libnmenu  
164 |   Function      :  bool _nc_Connect_Items(MENU *menu, ITEM **items)
165 |
166 |   Description   :  Connect the items in the item array to the menu.
167 |                    Decorate all the items with a number and a backward
168 |                    pointer to the menu.
169 |
170 |   Return Values :  TRUE       - successful connection
171 |                    FALSE      - connection failed
172 +--------------------------------------------------------------------------*/
173 NCURSES_EXPORT(bool)
174 _nc_Connect_Items(MENU * menu, ITEM ** items)
175 {
176   ITEM **item;
177   unsigned int ItemCount = 0;
178
179   if (menu && items)
180     {
181       for (item = items; *item; item++)
182         {
183           if ((*item)->imenu)
184             {
185               /* if a item is already connected, reject connection */
186               break;
187             }
188         }
189       if (!(*item))
190         /* we reached the end, so there was no connected item */
191         {
192           for (item = items; *item; item++)
193             {
194               if (menu->opt & O_ONEVALUE)
195                 {
196                   (*item)->value = FALSE;
197                 }
198               (*item)->index = (short)ItemCount++;
199               (*item)->imenu = menu;
200             }
201         }
202     }
203   else
204     return (FALSE);
205
206   if (ItemCount != 0)
207     {
208       menu->items = items;
209       menu->nitems = (short)ItemCount;
210       ComputeMaximum_NameDesc_Lengths(menu);
211       if ((menu->pattern = typeMalloc(char, (unsigned)(1 + menu->namelen))))
212         {
213           Reset_Pattern(menu);
214           set_menu_format(menu, menu->frows, menu->fcols);
215           menu->curitem = *items;
216           menu->toprow = 0;
217           return (TRUE);
218         }
219     }
220
221   /* If we fall through to this point, we have to reset all items connection 
222      and inform about a reject connection */
223   ResetConnectionInfo(menu, items);
224   return (FALSE);
225 }
226
227 /*---------------------------------------------------------------------------
228 |   Facility      :  libnmenu  
229 |   Function      :  void _nc_Disconnect_Items(MENU *menu)
230 |   
231 |   Description   :  Disconnect the menus item array from the menu
232 |
233 |   Return Values :  -
234 +--------------------------------------------------------------------------*/
235 NCURSES_EXPORT(void)
236 _nc_Disconnect_Items(MENU * menu)
237 {
238   if (menu && menu->items)
239     ResetConnectionInfo(menu, menu->items);
240 }
241
242 /*---------------------------------------------------------------------------
243 |   Facility      :  libnmenu  
244 |   Function      :  int _nc_Calculate_Text_Width(const TEXT * item)
245 |   
246 |   Description   :  Calculate the number of columns for a TEXT.
247 |
248 |   Return Values :  the width
249 +--------------------------------------------------------------------------*/
250 NCURSES_EXPORT(int)
251 _nc_Calculate_Text_Width(const TEXT * item /*FIXME: limit length */ )
252 {
253 #if USE_WIDEC_SUPPORT
254   int result = item->length;
255
256   T((T_CALLED("_nc_menu_text_width(%p)"), (const void *)item));
257   if (result != 0 && item->str != 0)
258     {
259       int count = (int)mbstowcs(0, item->str, 0);
260       wchar_t *temp = 0;
261
262       if (count > 0
263           && (temp = typeMalloc(wchar_t, 2 + count)) != 0)
264         {
265           int n;
266
267           result = 0;
268           mbstowcs(temp, item->str, (unsigned)count);
269           for (n = 0; n < count; ++n)
270             {
271               int test = wcwidth(temp[n]);
272
273               if (test <= 0)
274                 test = 1;
275               result += test;
276             }
277           free(temp);
278         }
279     }
280   returnCode(result);
281 #else
282   return item->length;
283 #endif
284 }
285
286 /*
287  * Calculate the actual width of a menu entry for wide-characters.
288  */
289 #if USE_WIDEC_SUPPORT
290 static int
291 calculate_actual_width(MENU * menu, bool name)
292 {
293   int width = 0;
294   int check = 0;
295   ITEM **items;
296
297   assert(menu && menu->items);
298
299   if (menu->items != 0)
300     {
301       for (items = menu->items; *items; items++)
302         {
303           if (name)
304             {
305               check = _nc_Calculate_Text_Width(&((*items)->name));
306             }
307           else
308             {
309               check = _nc_Calculate_Text_Width(&((*items)->description));
310             }
311           if (check > width)
312             width = check;
313         }
314     }
315   else
316     {
317       width = (name ? menu->namelen : menu->desclen);
318     }
319
320   T(("calculate_actual_width %s = %d/%d",
321      name ? "name" : "desc",
322      width,
323      name ? menu->namelen : menu->desclen));
324   return width;
325 }
326 #else
327 #define calculate_actual_width(menu, name) (name ? menu->namelen : menu->desclen)
328 #endif
329
330 /*---------------------------------------------------------------------------
331 |   Facility      :  libnmenu  
332 |   Function      :  void _nc_Calculate_Item_Length_and_Width(MENU *menu)
333 |   
334 |   Description   :  Calculate the length of an item and the width of the
335 |                    whole menu.
336 |
337 |   Return Values :  -
338 +--------------------------------------------------------------------------*/
339 NCURSES_EXPORT(void)
340 _nc_Calculate_Item_Length_and_Width(MENU * menu)
341 {
342   int l;
343
344   assert(menu);
345
346   menu->height = (short)(1 + menu->spc_rows * (menu->arows - 1));
347
348   l = calculate_actual_width(menu, TRUE);
349   l += menu->marklen;
350
351   if ((menu->opt & O_SHOWDESC) && (menu->desclen > 0))
352     {
353       l += calculate_actual_width(menu, FALSE);
354       l += menu->spc_desc;
355     }
356
357   menu->itemlen = (short)l;
358   l *= menu->cols;
359   l += (menu->cols - 1) * menu->spc_cols;       /* for the padding between the columns */
360   menu->width = (short)l;
361
362   T(("_nc_CalculateItem_Length_and_Width columns %d, item %d, width %d",
363      menu->cols,
364      menu->itemlen,
365      menu->width));
366 }
367
368 /*---------------------------------------------------------------------------
369 |   Facility      :  libnmenu  
370 |   Function      :  void _nc_Link_Item(MENU *menu)
371 |   
372 |   Description   :  Statically calculate for every item its four neighbors.
373 |                    This depends on the orientation of the menu. This
374 |                    static approach simplifies navigation in the menu a lot.
375 |
376 |   Return Values :  -
377 +--------------------------------------------------------------------------*/
378 NCURSES_EXPORT(void)
379 _nc_Link_Items(MENU * menu)
380 {
381   if (menu && menu->items && *(menu->items))
382     {
383       int i, j;
384       ITEM *item;
385       int Number_Of_Items = menu->nitems;
386       int col = 0, row = 0;
387       int Last_in_Row;
388       int Last_in_Column;
389       bool cycle = (menu->opt & O_NONCYCLIC) ? FALSE : TRUE;
390
391       ClrStatus(menu, _LINK_NEEDED);
392
393       if (menu->opt & O_ROWMAJOR)
394         {
395           int Number_Of_Columns = menu->cols;
396
397           for (i = 0; i < Number_Of_Items; i++)
398             {
399               item = menu->items[i];
400
401               Last_in_Row = row * Number_Of_Columns + (Number_Of_Columns - 1);
402
403               item->left = (col) ?
404               /* if we are not in the leftmost column, we can use the
405                  predecessor in the items array */
406                 menu->items[i - 1] :
407                 (cycle ? menu->items[(Last_in_Row >= Number_Of_Items) ?
408                                      Number_Of_Items - 1 :
409                                      Last_in_Row] :
410                  (ITEM *) 0);
411
412               item->right = ((col < (Number_Of_Columns - 1)) &&
413                              ((i + 1) < Number_Of_Items)
414                 )?
415                 menu->items[i + 1] :
416                 (cycle ? menu->items[row * Number_Of_Columns] :
417                  (ITEM *) 0
418                 );
419
420               Last_in_Column = (menu->rows - 1) * Number_Of_Columns + col;
421
422               item->up = (row) ? menu->items[i - Number_Of_Columns] :
423                 (cycle ? menu->items[(Last_in_Column >= Number_Of_Items) ?
424                                      Number_Of_Items - 1 :
425                                      Last_in_Column] :
426                  (ITEM *) 0);
427
428               item->down = ((i + Number_Of_Columns) < Number_Of_Items)
429                 ?
430                 menu->items[i + Number_Of_Columns] :
431                 (cycle ? menu->items[(row + 1) < menu->rows ?
432                                      Number_Of_Items - 1 : col] :
433                  (ITEM *) 0);
434               item->x = (short)col;
435               item->y = (short)row;
436               if (++col == Number_Of_Columns)
437                 {
438                   row++;
439                   col = 0;
440                 }
441             }
442         }
443       else
444         {
445           int Number_Of_Rows = menu->rows;
446
447           for (j = 0; j < Number_Of_Items; j++)
448             {
449               item = menu->items[i = (col * Number_Of_Rows + row)];
450
451               Last_in_Column = (menu->cols - 1) * Number_Of_Rows + row;
452
453               item->left = (col) ?
454                 menu->items[i - Number_Of_Rows] :
455                 (cycle ? (Last_in_Column >= Number_Of_Items) ?
456                  menu->items[Last_in_Column - Number_Of_Rows] :
457                  menu->items[Last_in_Column] :
458                  (ITEM *) 0);
459
460               item->right = ((i + Number_Of_Rows) < Number_Of_Items)
461                 ?
462                 menu->items[i + Number_Of_Rows] :
463                 (cycle ? menu->items[row] : (ITEM *) 0);
464
465               Last_in_Row = col * Number_Of_Rows + (Number_Of_Rows - 1);
466
467               item->up = (row) ?
468                 menu->items[i - 1] :
469                 (cycle ?
470                  menu->items[(Last_in_Row >= Number_Of_Items) ?
471                              Number_Of_Items - 1 :
472                              Last_in_Row] :
473                  (ITEM *) 0);
474
475               item->down = (row < (Number_Of_Rows - 1))
476                 ?
477                 (menu->items[((i + 1) < Number_Of_Items) ?
478                              i + 1 :
479                              (col - 1) * Number_Of_Rows + row + 1]) :
480                 (cycle ?
481                  menu->items[col * Number_Of_Rows] :
482                  (ITEM *) 0
483                 );
484
485               item->x = (short)col;
486               item->y = (short)row;
487               if ((++row) == Number_Of_Rows)
488                 {
489                   col++;
490                   row = 0;
491                 }
492             }
493         }
494     }
495 }
496
497 /*---------------------------------------------------------------------------
498 |   Facility      :  libnmenu  
499 |   Function      :  void _nc_Show_Menu(const MENU* menu)
500 |   
501 |   Description   :  Update the window that is associated with the menu
502 |
503 |   Return Values :  -
504 +--------------------------------------------------------------------------*/
505 NCURSES_EXPORT(void)
506 _nc_Show_Menu(const MENU * menu)
507 {
508   WINDOW *win;
509   int maxy, maxx;
510
511   assert(menu);
512   if ((menu->status & _POSTED) && !(menu->status & _IN_DRIVER))
513     {
514       /* adjust the internal subwindow to start on the current top */
515       assert(menu->sub);
516       mvderwin(menu->sub, menu->spc_rows * menu->toprow, 0);
517       win = Get_Menu_Window(menu);
518
519       maxy = getmaxy(win);
520       maxx = getmaxx(win);
521
522       if (menu->height < maxy)
523         maxy = menu->height;
524       if (menu->width < maxx)
525         maxx = menu->width;
526
527       copywin(menu->sub, win, 0, 0, 0, 0, maxy - 1, maxx - 1, 0);
528       pos_menu_cursor(menu);
529     }
530 }
531
532 /*---------------------------------------------------------------------------
533 |   Facility      :  libnmenu  
534 |   Function      :  void _nc_New_TopRow_and_CurrentItem(
535 |                            MENU *menu, 
536 |                            int new_toprow, 
537 |                            ITEM *new_current_item)
538 |   
539 |   Description   :  Redisplay the menu so that the given row becomes the
540 |                    top row and the given item becomes the new current
541 |                    item.
542 |
543 |   Return Values :  -
544 +--------------------------------------------------------------------------*/
545 NCURSES_EXPORT(void)
546 _nc_New_TopRow_and_CurrentItem(
547                                 MENU * menu,
548                                 int new_toprow,
549                                 ITEM * new_current_item)
550 {
551   ITEM *cur_item;
552   bool mterm_called = FALSE;
553   bool iterm_called = FALSE;
554
555   assert(menu);
556   if (menu->status & _POSTED)
557     {
558       if (new_current_item != menu->curitem)
559         {
560           Call_Hook(menu, itemterm);
561           iterm_called = TRUE;
562         }
563       if (new_toprow != menu->toprow)
564         {
565           Call_Hook(menu, menuterm);
566           mterm_called = TRUE;
567         }
568
569       cur_item = menu->curitem;
570       assert(cur_item);
571       menu->toprow = (short)(((menu->rows - menu->frows) >= 0)
572                              ? min(menu->rows - menu->frows, new_toprow)
573                              : 0);
574       menu->curitem = new_current_item;
575
576       if (mterm_called)
577         {
578           Call_Hook(menu, menuinit);
579         }
580       if (iterm_called)
581         {
582           /* this means, move from the old current_item to the new one... */
583           Move_To_Current_Item(menu, cur_item);
584           Call_Hook(menu, iteminit);
585         }
586       if (mterm_called || iterm_called)
587         {
588           _nc_Show_Menu(menu);
589         }
590       else
591         pos_menu_cursor(menu);
592     }
593   else
594     {                           /* if we are not posted, this is quite simple */
595       menu->toprow = (short)(((menu->rows - menu->frows) >= 0)
596                              ? min(menu->rows - menu->frows, new_toprow)
597                              : 0);
598       menu->curitem = new_current_item;
599     }
600 }
601
602 /* m_global.c ends here */