xref: /openbsd-src/lib/libmenu/m_opts.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: m_opts.c,v 1.7 2001/01/22 18:02:05 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_opts                                                            *
37 * Menus option routines                                                    *
38 ***************************************************************************/
39 
40 #include "menu.priv.h"
41 
42 MODULE_ID("$From: m_opts.c,v 1.13 2000/12/10 02:16:48 tom Exp $")
43 
44 /*---------------------------------------------------------------------------
45 |   Facility      :  libnmenu
46 |   Function      :  int set_menu_opts(MENU *menu, Menu_Options opts)
47 |
48 |   Description   :  Set the options for this menu. If the new settings
49 |                    end up in a change of the geometry of the menu, it
50 |                    will be recalculated. This operation is forbidden if
51 |                    the menu is already posted.
52 |
53 |   Return Values :  E_OK           - success
54 |                    E_BAD_ARGUMENT - invalid menu options
55 |                    E_POSTED       - menu is already posted
56 +--------------------------------------------------------------------------*/
57 NCURSES_EXPORT(int)
58 set_menu_opts (MENU * menu, Menu_Options opts)
59 {
60   opts &= ALL_MENU_OPTS;
61 
62   if (opts & ~ALL_MENU_OPTS)
63     RETURN(E_BAD_ARGUMENT);
64 
65   if (menu)
66     {
67       if ( menu->status & _POSTED )
68 	RETURN(E_POSTED);
69 
70       if ( (opts&O_ROWMAJOR) != (menu->opt&O_ROWMAJOR))
71 	{
72 	  /* we need this only if the layout really changed ... */
73 	  if (menu->items && menu->items[0])
74 	    {
75 	      menu->toprow  = 0;
76 	      menu->curitem = menu->items[0];
77 	      assert(menu->curitem);
78 	      set_menu_format( menu, menu->frows, menu->fcols );
79 	    }
80 	}
81 
82       menu->opt = opts;
83 
84       if (opts & O_ONEVALUE)
85 	{
86 	  ITEM **item;
87 
88 	  if ( ((item=menu->items) != (ITEM**)0) )
89 	    for(;*item;item++)
90 	      (*item)->value = FALSE;
91 	}
92 
93       if (opts & O_SHOWDESC)	/* this also changes the geometry */
94 	_nc_Calculate_Item_Length_and_Width( menu );
95     }
96   else
97     _nc_Default_Menu.opt = opts;
98 
99   RETURN(E_OK);
100 }
101 
102 /*---------------------------------------------------------------------------
103 |   Facility      :  libnmenu
104 |   Function      :  int menu_opts_off(MENU *menu, Menu_Options opts)
105 |
106 |   Description   :  Switch off the options for this menu. If the new settings
107 |                    end up in a change of the geometry of the menu, it
108 |                    will be recalculated. This operation is forbidden if
109 |                    the menu is already posted.
110 |
111 |   Return Values :  E_OK           - success
112 |                    E_BAD_ARGUMENT - invalid options
113 |                    E_POSTED       - menu is already posted
114 +--------------------------------------------------------------------------*/
115 NCURSES_EXPORT(int)
116 menu_opts_off (MENU *menu, Menu_Options  opts)
117 {
118   MENU *cmenu = menu; /* use a copy because set_menu_opts must detect
119                          NULL menu itself to adjust its behaviour */
120 
121   opts &= ALL_MENU_OPTS;
122   if (opts & ~ALL_MENU_OPTS)
123     RETURN(E_BAD_ARGUMENT);
124   else
125     {
126       Normalize_Menu(cmenu);
127       opts = cmenu->opt & ~opts;
128       return set_menu_opts( menu, opts );
129     }
130 }
131 
132 /*---------------------------------------------------------------------------
133 |   Facility      :  libnmenu
134 |   Function      :  int menu_opts_on(MENU *menu, Menu_Options opts)
135 |
136 |   Description   :  Switch on the options for this menu. If the new settings
137 |                    end up in a change of the geometry of the menu, it
138 |                    will be recalculated. This operation is forbidden if
139 |                    the menu is already posted.
140 |
141 |   Return Values :  E_OK           - success
142 |                    E_BAD_ARGUMENT - invalid menu options
143 |                    E_POSTED       - menu is already posted
144 +--------------------------------------------------------------------------*/
145 NCURSES_EXPORT(int)
146 menu_opts_on (MENU * menu, Menu_Options opts)
147 {
148   MENU *cmenu = menu; /* use a copy because set_menu_opts must detect
149                          NULL menu itself to adjust its behaviour */
150 
151   opts &= ALL_MENU_OPTS;
152   if (opts & ~ALL_MENU_OPTS)
153     RETURN(E_BAD_ARGUMENT);
154   else
155     {
156       Normalize_Menu(cmenu);
157       opts = cmenu->opt | opts;
158       return set_menu_opts(menu, opts);
159     }
160 }
161 
162 /*---------------------------------------------------------------------------
163 |   Facility      :  libnmenu
164 |   Function      :  Menu_Options menu_opts(const MENU *menu)
165 |
166 |   Description   :  Return the options for this menu.
167 |
168 |   Return Values :  Menu options
169 +--------------------------------------------------------------------------*/
170 NCURSES_EXPORT(Menu_Options)
171 menu_opts (const MENU *menu)
172 {
173   return (ALL_MENU_OPTS & Normalize_Menu( menu )->opt);
174 }
175 
176 /* m_opts.c ends here */
177