xref: /freebsd/contrib/ncurses/menu/m_format.c (revision bc3f5ec90bde2f3a5e4021d133c89793d68b8c73)
1 /****************************************************************************
2  * Copyright (c) 1998-2010,2012 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_format                                                          *
35 * Set and get maximum numbers of rows and columns in menus                 *
36 ***************************************************************************/
37 
38 #include "menu.priv.h"
39 
40 MODULE_ID("$Id: m_format.c,v 1.18 2012/06/09 23:54:02 tom Exp $")
41 
42 #define minimum(a,b) ((a)<(b) ? (a): (b))
43 
44 /*---------------------------------------------------------------------------
45 |   Facility      :  libnmenu
46 |   Function      :  int set_menu_format(MENU *menu, int rows, int cols)
47 |
48 |   Description   :  Sets the maximum number of rows and columns of items
49 |                    that may be displayed at one time on a menu. If the
50 |                    menu contains more items than can be displayed at
51 |                    once, the menu will be scrollable.
52 |
53 |   Return Values :  E_OK                   - success
54 |                    E_BAD_ARGUMENT         - invalid values passed
55 |                    E_NOT_CONNECTED        - there are no items connected
56 |                    E_POSTED               - the menu is already posted
57 +--------------------------------------------------------------------------*/
58 NCURSES_EXPORT(int)
59 set_menu_format(MENU * menu, int rows, int cols)
60 {
61   int total_rows, total_cols;
62 
63   T((T_CALLED("set_menu_format(%p,%d,%d)"), (void *)menu, rows, cols));
64 
65   if (rows < 0 || cols < 0)
66     RETURN(E_BAD_ARGUMENT);
67 
68   if (menu)
69     {
70       if (menu->status & _POSTED)
71 	RETURN(E_POSTED);
72 
73       if (!(menu->items))
74 	RETURN(E_NOT_CONNECTED);
75 
76       if (rows == 0)
77 	rows = menu->frows;
78       if (cols == 0)
79 	cols = menu->fcols;
80 
81       if (menu->pattern)
82 	Reset_Pattern(menu);
83 
84       menu->frows = (short)rows;
85       menu->fcols = (short)cols;
86 
87       assert(rows > 0 && cols > 0);
88       total_rows = (menu->nitems - 1) / cols + 1;
89       total_cols = (menu->opt & O_ROWMAJOR) ?
90 	minimum(menu->nitems, cols) :
91 	(menu->nitems - 1) / total_rows + 1;
92 
93       menu->rows = (short)total_rows;
94       menu->cols = (short)total_cols;
95       menu->arows = (short)minimum(total_rows, rows);
96       menu->toprow = 0;
97       menu->curitem = *(menu->items);
98       assert(menu->curitem);
99       SetStatus(menu, _LINK_NEEDED);
100       _nc_Calculate_Item_Length_and_Width(menu);
101     }
102   else
103     {
104       if (rows > 0)
105 	_nc_Default_Menu.frows = (short)rows;
106       if (cols > 0)
107 	_nc_Default_Menu.fcols = (short)cols;
108     }
109 
110   RETURN(E_OK);
111 }
112 
113 /*---------------------------------------------------------------------------
114 |   Facility      :  libnmenu
115 |   Function      :  void menu_format(const MENU *menu, int *rows, int *cols)
116 |
117 |   Description   :  Returns the maximum number of rows and columns that may
118 |                    be displayed at one time on menu.
119 |
120 |   Return Values :  -
121 +--------------------------------------------------------------------------*/
122 NCURSES_EXPORT(void)
123 menu_format(const MENU * menu, int *rows, int *cols)
124 {
125   if (rows)
126     *rows = Normalize_Menu(menu)->frows;
127   if (cols)
128     *cols = Normalize_Menu(menu)->fcols;
129 }
130 
131 /* m_format.c ends here */
132