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