]> ncurses.scripts.mit.edu Git - ncurses.git/blob - menu/m_format.c
4f19f76c33d713cf9619810f00e29db97f5fd284
[ncurses.git] / menu / m_format.c
1 /****************************************************************************
2  * Copyright (c) 1998,2000 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  *   Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en             *
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.11 2002/07/06 15:22:16 juergen 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 +--------------------------------------------------------------------------*/
59 NCURSES_EXPORT(int)
60 set_menu_format (MENU *menu, int rows, int cols)
61 {
62   int total_rows, total_cols;
63   
64   if (rows<0 || cols<0) 
65     RETURN(E_BAD_ARGUMENT);
66   
67   if (menu)
68     {
69       if ( menu->status & _POSTED )
70         RETURN(E_POSTED);
71       
72       if (!(menu->items))
73         RETURN(E_NOT_CONNECTED);
74       
75       if (rows==0) 
76         rows = menu->frows;
77       if (cols==0) 
78         cols = menu->fcols;
79       
80       if (menu->pattern)
81         Reset_Pattern(menu);
82       
83       menu->frows = rows;
84       menu->fcols = cols;
85       
86       assert(rows>0 && cols>0);
87       total_rows = (menu->nitems - 1)/cols + 1;
88       total_cols = (menu->status & O_ROWMAJOR) ? 
89         minimum(menu->nitems,cols) :
90           (menu->nitems-1)/total_rows + 1;
91       
92       menu->rows    = total_rows;
93       menu->cols    = total_cols;
94       menu->arows   = minimum(total_rows,rows); 
95       menu->toprow  = 0;        
96       menu->curitem = *(menu->items);
97       assert(menu->curitem);
98       menu->status |= _LINK_NEEDED;
99       _nc_Calculate_Item_Length_and_Width(menu);
100     }
101   else
102     {
103       if (rows>0) _nc_Default_Menu.frows = rows;
104       if (cols>0) _nc_Default_Menu.fcols = cols;
105     }
106   
107   RETURN(E_OK);
108 }
109
110 /*---------------------------------------------------------------------------
111 |   Facility      :  libnmenu  
112 |   Function      :  void menu_format(const MENU *menu, int *rows, int *cols)
113 |   
114 |   Description   :  Returns the maximum number of rows and columns that may
115 |                    be displayed at one time on menu.
116 |
117 |   Return Values :  -
118 +--------------------------------------------------------------------------*/
119 NCURSES_EXPORT(void)
120 menu_format (const MENU *menu, int *rows, int *cols)
121 {
122   if (rows)
123     *rows = Normalize_Menu(menu)->frows;
124   if (cols)
125     *cols = Normalize_Menu(menu)->fcols;
126 }
127
128 /* m_format.c ends here */