xref: /openbsd/lib/libmenu/m_item_opt.c (revision c7ef0cfc)
1 /* $OpenBSD: m_item_opt.c,v 1.10 2023/10/17 09:52:10 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright 2020,2021 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_item_opt                                                        *
38 * Menus item option routines                                               *
39 ***************************************************************************/
40 
41 #include "menu.priv.h"
42 
43 MODULE_ID("$Id: m_item_opt.c,v 1.10 2023/10/17 09:52:10 nicm Exp $")
44 
45 /*---------------------------------------------------------------------------
46 |   Facility      :  libnmenu
47 |   Function      :  int set_item_opts(ITEM *item, Item_Options opts)
48 |
49 |   Description   :  Set the options of the item. If there are relevant
50 |                    changes, the item is connected and the menu is posted,
51 |                    the menu will be redisplayed.
52 |
53 |   Return Values :  E_OK            - success
54 |                    E_BAD_ARGUMENT  - invalid item options
55 +--------------------------------------------------------------------------*/
MENU_EXPORT(int)56 MENU_EXPORT(int)
57 set_item_opts(ITEM *item, Item_Options opts)
58 {
59   T((T_CALLED("set_menu_opts(%p,%d)"), (void *)item, opts));
60 
61   opts &= ALL_ITEM_OPTS;
62 
63   if (opts & ~ALL_ITEM_OPTS)
64     RETURN(E_BAD_ARGUMENT);
65 
66   if (item)
67     {
68       if (item->opt != opts)
69 	{
70 	  MENU *menu = item->imenu;
71 
72 	  item->opt = opts;
73 
74 	  if ((!(opts & O_SELECTABLE)) && item->value)
75 	    item->value = FALSE;
76 
77 	  if (menu && (menu->status & _POSTED))
78 	    {
79 	      Move_And_Post_Item(menu, item);
80 	      _nc_Show_Menu(menu);
81 	    }
82 	}
83     }
84   else
85     _nc_Default_Item.opt = opts;
86 
87   RETURN(E_OK);
88 }
89 
90 /*---------------------------------------------------------------------------
91 |   Facility      :  libnmenu
92 |   Function      :  int item_opts_off(ITEM *item, Item_Options opts)
93 |
94 |   Description   :  Switch of the options for this item.
95 |
96 |   Return Values :  E_OK            - success
97 |                    E_BAD_ARGUMENT  - invalid options
98 +--------------------------------------------------------------------------*/
99 MENU_EXPORT(int)
item_opts_off(ITEM * item,Item_Options opts)100 item_opts_off(ITEM *item, Item_Options opts)
101 {
102   ITEM *citem = item;		/* use a copy because set_item_opts must detect
103 
104 				   NULL item itself to adjust its behavior */
105 
106   T((T_CALLED("item_opts_off(%p,%d)"), (void *)item, opts));
107 
108   if (opts & ~ALL_ITEM_OPTS)
109     RETURN(E_BAD_ARGUMENT);
110   else
111     {
112       Normalize_Item(citem);
113       opts = citem->opt & ~(opts & ALL_ITEM_OPTS);
114       returnCode(set_item_opts(item, opts));
115     }
116 }
117 
118 /*---------------------------------------------------------------------------
119 |   Facility      :  libnmenu
120 |   Function      :  int item_opts_on(ITEM *item, Item_Options opts)
121 |
122 |   Description   :  Switch on the options for this item.
123 |
124 |   Return Values :  E_OK            - success
125 |                    E_BAD_ARGUMENT  - invalid options
126 +--------------------------------------------------------------------------*/
127 MENU_EXPORT(int)
item_opts_on(ITEM * item,Item_Options opts)128 item_opts_on(ITEM *item, Item_Options opts)
129 {
130   ITEM *citem = item;		/* use a copy because set_item_opts must detect
131 
132 				   NULL item itself to adjust its behavior */
133 
134   T((T_CALLED("item_opts_on(%p,%d)"), (void *)item, opts));
135 
136   opts &= ALL_ITEM_OPTS;
137   if (opts & ~ALL_ITEM_OPTS)
138     RETURN(E_BAD_ARGUMENT);
139   else
140     {
141       Normalize_Item(citem);
142       opts = citem->opt | opts;
143       returnCode(set_item_opts(item, opts));
144     }
145 }
146 
147 /*---------------------------------------------------------------------------
148 |   Facility      :  libnmenu
149 |   Function      :  Item_Options item_opts(const ITEM *item)
150 |
151 |   Description   :  Switch of the options for this item.
152 |
153 |   Return Values :  Items options
154 +--------------------------------------------------------------------------*/
155 MENU_EXPORT(Item_Options)
item_opts(const ITEM * item)156 item_opts(const ITEM *item)
157 {
158   T((T_CALLED("item_opts(%p)"), (const void *)item));
159   returnItemOpts(ALL_ITEM_OPTS & Normalize_Item(item)->opt);
160 }
161 
162 /* m_item_opt.c ends here */
163