xref: /openbsd/lib/libmenu/m_opts.c (revision 3bef86f7)
1 /* $OpenBSD: m_opts.c,v 1.9 2023/10/17 09:52:10 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright 2020 Thomas E. Dickey                                          *
5  * Copyright 1998-2004,2010 Free Software Foundation, Inc.                  *
6  *                                                                          *
7  * Permission is hereby granted, free of charge, to any person obtaining a  *
8  * copy of this software and associated documentation files (the            *
9  * "Software"), to deal in the Software without restriction, including      *
10  * without limitation the rights to use, copy, modify, merge, publish,      *
11  * distribute, distribute with modifications, sublicense, and/or sell       *
12  * copies of the Software, and to permit persons to whom the Software is    *
13  * furnished to do so, subject to the following conditions:                 *
14  *                                                                          *
15  * The above copyright notice and this permission notice shall be included  *
16  * in all copies or substantial portions of the Software.                   *
17  *                                                                          *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25  *                                                                          *
26  * Except as contained in this notice, the name(s) of the above copyright   *
27  * holders shall not be used in advertising or otherwise to promote the     *
28  * sale, use or other dealings in this Software without prior written       *
29  * authorization.                                                           *
30  ****************************************************************************/
31 
32 /****************************************************************************
33  *   Author:  Juergen Pfeifer, 1995,1997                                    *
34  ****************************************************************************/
35 
36 /***************************************************************************
37 * Module m_opts                                                            *
38 * Menus option routines                                                    *
39 ***************************************************************************/
40 
41 #include "menu.priv.h"
42 
43 MODULE_ID("$Id: m_opts.c,v 1.9 2023/10/17 09:52:10 nicm Exp $")
44 
45 /*---------------------------------------------------------------------------
46 |   Facility      :  libnmenu
47 |   Function      :  int set_menu_opts(MENU *menu, Menu_Options opts)
48 |
49 |   Description   :  Set the options for this menu. If the new settings
50 |                    end up in a change of the geometry of the menu, it
51 |                    will be recalculated. This operation is forbidden if
52 |                    the menu is already posted.
53 |
54 |   Return Values :  E_OK           - success
55 |                    E_BAD_ARGUMENT - invalid menu options
56 |                    E_POSTED       - menu is already posted
57 +--------------------------------------------------------------------------*/
58 MENU_EXPORT(int)
59 set_menu_opts(MENU *menu, Menu_Options opts)
60 {
61   T((T_CALLED("set_menu_opts(%p,%d)"), (void *)menu, opts));
62 
63   opts &= ALL_MENU_OPTS;
64 
65   if (opts & ~ALL_MENU_OPTS)
66     RETURN(E_BAD_ARGUMENT);
67 
68   if (menu)
69     {
70       if (menu->status & _POSTED)
71 	RETURN(E_POSTED);
72 
73       if ((opts & O_ROWMAJOR) != (menu->opt & O_ROWMAJOR))
74 	{
75 	  /* we need this only if the layout really changed ... */
76 	  if (menu->items && menu->items[0])
77 	    {
78 	      menu->toprow = 0;
79 	      menu->curitem = menu->items[0];
80 	      assert(menu->curitem);
81 	      set_menu_format(menu, menu->frows, menu->fcols);
82 	    }
83 	}
84 
85       menu->opt = opts;
86 
87       if (opts & O_ONEVALUE)
88 	{
89 	  ITEM **item;
90 
91 	  if (((item = menu->items) != (ITEM **)0))
92 	    for (; *item; item++)
93 	      (*item)->value = FALSE;
94 	}
95 
96       if (opts & O_SHOWDESC)	/* this also changes the geometry */
97 	_nc_Calculate_Item_Length_and_Width(menu);
98     }
99   else
100     _nc_Default_Menu.opt = opts;
101 
102   RETURN(E_OK);
103 }
104 
105 /*---------------------------------------------------------------------------
106 |   Facility      :  libnmenu
107 |   Function      :  int menu_opts_off(MENU *menu, Menu_Options opts)
108 |
109 |   Description   :  Switch off the options for this menu. If the new settings
110 |                    end up in a change of the geometry of the menu, it
111 |                    will be recalculated. This operation is forbidden if
112 |                    the menu is already posted.
113 |
114 |   Return Values :  E_OK           - success
115 |                    E_BAD_ARGUMENT - invalid options
116 |                    E_POSTED       - menu is already posted
117 +--------------------------------------------------------------------------*/
118 MENU_EXPORT(int)
119 menu_opts_off(MENU *menu, Menu_Options opts)
120 {
121   MENU *cmenu = menu;		/* use a copy because set_menu_opts must detect
122 
123 				   NULL menu itself to adjust its behavior */
124 
125   T((T_CALLED("menu_opts_off(%p,%d)"), (void *)menu, opts));
126 
127   opts &= ALL_MENU_OPTS;
128   if (opts & ~ALL_MENU_OPTS)
129     RETURN(E_BAD_ARGUMENT);
130   else
131     {
132       Normalize_Menu(cmenu);
133       opts = cmenu->opt & ~opts;
134       returnCode(set_menu_opts(menu, opts));
135     }
136 }
137 
138 /*---------------------------------------------------------------------------
139 |   Facility      :  libnmenu
140 |   Function      :  int menu_opts_on(MENU *menu, Menu_Options opts)
141 |
142 |   Description   :  Switch on the options for this menu. If the new settings
143 |                    end up in a change of the geometry of the menu, it
144 |                    will be recalculated. This operation is forbidden if
145 |                    the menu is already posted.
146 |
147 |   Return Values :  E_OK           - success
148 |                    E_BAD_ARGUMENT - invalid menu options
149 |                    E_POSTED       - menu is already posted
150 +--------------------------------------------------------------------------*/
151 MENU_EXPORT(int)
152 menu_opts_on(MENU *menu, Menu_Options opts)
153 {
154   MENU *cmenu = menu;		/* use a copy because set_menu_opts must detect
155 
156 				   NULL menu itself to adjust its behavior */
157 
158   T((T_CALLED("menu_opts_on(%p,%d)"), (void *)menu, opts));
159 
160   opts &= ALL_MENU_OPTS;
161   if (opts & ~ALL_MENU_OPTS)
162     RETURN(E_BAD_ARGUMENT);
163   else
164     {
165       Normalize_Menu(cmenu);
166       opts = cmenu->opt | opts;
167       returnCode(set_menu_opts(menu, opts));
168     }
169 }
170 
171 /*---------------------------------------------------------------------------
172 |   Facility      :  libnmenu
173 |   Function      :  Menu_Options menu_opts(const MENU *menu)
174 |
175 |   Description   :  Return the options for this menu.
176 |
177 |   Return Values :  Menu options
178 +--------------------------------------------------------------------------*/
179 MENU_EXPORT(Menu_Options)
180 menu_opts(const MENU *menu)
181 {
182   T((T_CALLED("menu_opts(%p)"), (const void *)menu));
183   returnMenuOpts(ALL_MENU_OPTS & Normalize_Menu(menu)->opt);
184 }
185 
186 /* m_opts.c ends here */
187