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