1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 1998-2010,2012 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30 /****************************************************************************
31 * Author: Juergen Pfeifer, 1995,1997 *
32 ****************************************************************************/
33
34 /***************************************************************************
35 * Module m_format *
36 * Set and get maximum numbers of rows and columns in menus *
37 ***************************************************************************/
38
39 #include "menu.priv.h"
40
41 MODULE_ID("$Id: m_format.c,v 1.19 2020/02/02 23:34:34 tom Exp $")
42
43 #define minimum(a,b) ((a)<(b) ? (a): (b))
44
45 /*---------------------------------------------------------------------------
46 | Facility : libnmenu
47 | Function : int set_menu_format(MENU *menu, int rows, int cols)
48 |
49 | Description : Sets the maximum number of rows and columns of items
50 | that may be displayed at one time on a menu. If the
51 | menu contains more items than can be displayed at
52 | once, the menu will be scrollable.
53 |
54 | Return Values : E_OK - success
55 | E_BAD_ARGUMENT - invalid values passed
56 | E_NOT_CONNECTED - there are no items connected
57 | E_POSTED - the menu is already posted
58 +--------------------------------------------------------------------------*/
NCURSES_EXPORT(int)59 NCURSES_EXPORT(int)
60 set_menu_format(MENU * menu, int rows, int cols)
61 {
62 int total_rows, total_cols;
63
64 T((T_CALLED("set_menu_format(%p,%d,%d)"), (void *)menu, rows, cols));
65
66 if (rows < 0 || cols < 0)
67 RETURN(E_BAD_ARGUMENT);
68
69 if (menu)
70 {
71 if (menu->status & _POSTED)
72 RETURN(E_POSTED);
73
74 if (!(menu->items))
75 RETURN(E_NOT_CONNECTED);
76
77 if (rows == 0)
78 rows = menu->frows;
79 if (cols == 0)
80 cols = menu->fcols;
81
82 if (menu->pattern)
83 Reset_Pattern(menu);
84
85 menu->frows = (short)rows;
86 menu->fcols = (short)cols;
87
88 assert(rows > 0 && cols > 0);
89 total_rows = (menu->nitems - 1) / cols + 1;
90 total_cols = (menu->opt & O_ROWMAJOR) ?
91 minimum(menu->nitems, cols) :
92 (menu->nitems - 1) / total_rows + 1;
93
94 menu->rows = (short)total_rows;
95 menu->cols = (short)total_cols;
96 menu->arows = (short)minimum(total_rows, rows);
97 menu->toprow = 0;
98 menu->curitem = *(menu->items);
99 assert(menu->curitem);
100 SetStatus(menu, _LINK_NEEDED);
101 _nc_Calculate_Item_Length_and_Width(menu);
102 }
103 else
104 {
105 if (rows > 0)
106 _nc_Default_Menu.frows = (short)rows;
107 if (cols > 0)
108 _nc_Default_Menu.fcols = (short)cols;
109 }
110
111 RETURN(E_OK);
112 }
113
114 /*---------------------------------------------------------------------------
115 | Facility : libnmenu
116 | Function : void menu_format(const MENU *menu, int *rows, int *cols)
117 |
118 | Description : Returns the maximum number of rows and columns that may
119 | be displayed at one time on menu.
120 |
121 | Return Values : -
122 +--------------------------------------------------------------------------*/
123 NCURSES_EXPORT(void)
menu_format(const MENU * menu,int * rows,int * cols)124 menu_format(const MENU * menu, int *rows, int *cols)
125 {
126 if (rows)
127 *rows = Normalize_Menu(menu)->frows;
128 if (cols)
129 *cols = Normalize_Menu(menu)->fcols;
130 }
131
132 /* m_format.c ends here */
133