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