]> ncurses.scripts.mit.edu Git - ncurses.git/blob - menu/m_format.c
ncurses 4.1
[ncurses.git] / menu / m_format.c
1 /*-----------------------------------------------------------------------------+
2 |           The ncurses menu library is  Copyright (C) 1995-1997               |
3 |             by Juergen Pfeifer <Juergen.Pfeifer@T-Online.de>                 |
4 |                          All Rights Reserved.                                |
5 |                                                                              |
6 | Permission to use, copy, modify, and distribute this software and its        |
7 | documentation for any purpose and without fee is hereby granted, provided    |
8 | that the above copyright notice appear in all copies and that both that      |
9 | copyright notice and this permission notice appear in supporting             |
10 | documentation, and that the name of the above listed copyright holder(s) not |
11 | be used in advertising or publicity pertaining to distribution of the        |
12 | software without specific, written prior permission.                         | 
13 |                                                                              |
14 | THE ABOVE LISTED COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD TO  |
15 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-  |
16 | NESS, IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR   |
17 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RE- |
18 | SULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
19 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH    |
20 | THE USE OR PERFORMANCE OF THIS SOFTWARE.                                     |
21 +-----------------------------------------------------------------------------*/
22
23 /***************************************************************************
24 * Module menu_format                                                       *
25 * Set and get maximum numbers of rows and columns in menus                 *
26 ***************************************************************************/
27
28 #include "menu.priv.h"
29
30 MODULE_ID("$Id: m_format.c,v 1.5 1997/05/01 16:47:26 juergen Exp $")
31
32 #define minimum(a,b) ((a)<(b) ? (a): (b))
33
34 /*---------------------------------------------------------------------------
35 |   Facility      :  libnmenu  
36 |   Function      :  int set_menu_format(MENU *menu, int rows, int cols)
37 |   
38 |   Description   :  Sets the maximum number of rows and columns of items
39 |                    that may be displayed at one time on a menu. If the
40 |                    menu contains more items than can be displayed at
41 |                    once, the menu will be scrollable.
42 |
43 |   Return Values :  E_OK                   - success
44 |                    E_BAD_ARGUMENT         - invalid values passed
45 |                    E_NOT_CONNECTED        - there are no items connected
46 |                    E_POSTED               - the menu is already posted
47 +--------------------------------------------------------------------------*/
48 int set_menu_format(MENU *menu, int rows, int cols)
49 {
50   int total_rows, total_cols;
51   
52   if (rows<0 || cols<0) 
53     RETURN(E_BAD_ARGUMENT);
54   
55   if (menu)
56     {
57       if ( menu->status & _POSTED )
58         RETURN(E_POSTED);
59       
60       if (!(menu->items))
61         RETURN(E_NOT_CONNECTED);
62       
63       if (rows==0) 
64         rows = menu->frows;
65       if (cols==0) 
66         cols = menu->fcols;
67       
68       if (menu->pattern)
69         Reset_Pattern(menu);
70       
71       menu->frows = rows;
72       menu->fcols = cols;
73       
74       assert(rows>0 && cols>0);
75       total_rows = (menu->nitems - 1)/cols + 1;
76       total_cols = (menu->status & O_ROWMAJOR) ? 
77         minimum(menu->nitems,cols) :
78           (menu->nitems-1)/total_rows + 1;
79       
80       menu->rows    = total_rows;
81       menu->cols    = total_cols;
82       menu->arows   = minimum(total_rows,rows); 
83       menu->toprow  = 0;        
84       menu->curitem = *(menu->items);
85       assert(menu->curitem);
86       menu->status |= _LINK_NEEDED;
87       _nc_Calculate_Item_Length_and_Width(menu);
88     }
89   else
90     {
91       if (rows>0) _nc_Default_Menu.frows = rows;
92       if (cols>0) _nc_Default_Menu.fcols = cols;
93     }
94   
95   RETURN(E_OK);
96 }
97
98 /*---------------------------------------------------------------------------
99 |   Facility      :  libnmenu  
100 |   Function      :  void menu_format(const MENU *menu, int *rows, int *cols)
101 |   
102 |   Description   :  Returns the maximum number of rows and columns that may
103 |                    be displayed at one time on menu.
104 |
105 |   Return Values :  -
106 +--------------------------------------------------------------------------*/
107 void menu_format(const MENU *menu, int *rows, int *cols)
108 {
109   if (rows)
110     *rows = Normalize_Menu(menu)->frows;
111   if (cols)
112     *cols = Normalize_Menu(menu)->fcols;
113 }
114
115 /* m_format.c ends here */