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