xref: /freebsd/contrib/ncurses/menu/m_opts.c (revision bc3f5ec90bde2f3a5e4021d133c89793d68b8c73)
1 /****************************************************************************
2  * Copyright (c) 1998-2004,2010 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /****************************************************************************
30  *   Author:  Juergen Pfeifer, 1995,1997                                    *
31  ****************************************************************************/
32 
33 /***************************************************************************
34 * Module m_opts                                                            *
35 * Menus option routines                                                    *
36 ***************************************************************************/
37 
38 #include "menu.priv.h"
39 
40 MODULE_ID("$Id: m_opts.c,v 1.20 2010/01/23 21:20:10 tom Exp $")
41 
42 /*---------------------------------------------------------------------------
43 |   Facility      :  libnmenu
44 |   Function      :  int set_menu_opts(MENU *menu, Menu_Options opts)
45 |
46 |   Description   :  Set the options for this menu. If the new settings
47 |                    end up in a change of the geometry of the menu, it
48 |                    will be recalculated. This operation is forbidden if
49 |                    the menu is already posted.
50 |
51 |   Return Values :  E_OK           - success
52 |                    E_BAD_ARGUMENT - invalid menu options
53 |                    E_POSTED       - menu is already posted
54 +--------------------------------------------------------------------------*/
55 NCURSES_EXPORT(int)
56 set_menu_opts(MENU * menu, Menu_Options opts)
57 {
58   T((T_CALLED("set_menu_opts(%p,%d)"), (void *)menu, 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 
120 				   NULL menu itself to adjust its behavior */
121 
122   T((T_CALLED("menu_opts_off(%p,%d)"), (void *)menu, opts));
123 
124   opts &= ALL_MENU_OPTS;
125   if (opts & ~ALL_MENU_OPTS)
126     RETURN(E_BAD_ARGUMENT);
127   else
128     {
129       Normalize_Menu(cmenu);
130       opts = cmenu->opt & ~opts;
131       returnCode(set_menu_opts(menu, opts));
132     }
133 }
134 
135 /*---------------------------------------------------------------------------
136 |   Facility      :  libnmenu
137 |   Function      :  int menu_opts_on(MENU *menu, Menu_Options opts)
138 |
139 |   Description   :  Switch on the options for this menu. If the new settings
140 |                    end up in a change of the geometry of the menu, it
141 |                    will be recalculated. This operation is forbidden if
142 |                    the menu is already posted.
143 |
144 |   Return Values :  E_OK           - success
145 |                    E_BAD_ARGUMENT - invalid menu options
146 |                    E_POSTED       - menu is already posted
147 +--------------------------------------------------------------------------*/
148 NCURSES_EXPORT(int)
149 menu_opts_on(MENU * menu, Menu_Options opts)
150 {
151   MENU *cmenu = menu;		/* use a copy because set_menu_opts must detect
152 
153 				   NULL menu itself to adjust its behavior */
154 
155   T((T_CALLED("menu_opts_on(%p,%d)"), (void *)menu, opts));
156 
157   opts &= ALL_MENU_OPTS;
158   if (opts & ~ALL_MENU_OPTS)
159     RETURN(E_BAD_ARGUMENT);
160   else
161     {
162       Normalize_Menu(cmenu);
163       opts = cmenu->opt | opts;
164       returnCode(set_menu_opts(menu, opts));
165     }
166 }
167 
168 /*---------------------------------------------------------------------------
169 |   Facility      :  libnmenu
170 |   Function      :  Menu_Options menu_opts(const MENU *menu)
171 |
172 |   Description   :  Return the options for this menu.
173 |
174 |   Return Values :  Menu options
175 +--------------------------------------------------------------------------*/
176 NCURSES_EXPORT(Menu_Options)
177 menu_opts(const MENU * menu)
178 {
179   T((T_CALLED("menu_opts(%p)"), (const void *)menu));
180   returnMenuOpts(ALL_MENU_OPTS & Normalize_Menu(menu)->opt);
181 }
182 
183 /* m_opts.c ends here */
184