xref: /freebsd/contrib/ncurses/menu/m_opts.c (revision ae83180158c4c937f170e31eff311b18c0286a93)
1 /****************************************************************************
2  * Copyright (c) 1998,2000 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 <juergen.pfeifer@gmx.net> 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.13 2000/12/10 02:16:48 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   opts &= ALL_MENU_OPTS;
59 
60   if (opts & ~ALL_MENU_OPTS)
61     RETURN(E_BAD_ARGUMENT);
62 
63   if (menu)
64     {
65       if ( menu->status & _POSTED )
66 	RETURN(E_POSTED);
67 
68       if ( (opts&O_ROWMAJOR) != (menu->opt&O_ROWMAJOR))
69 	{
70 	  /* we need this only if the layout really changed ... */
71 	  if (menu->items && menu->items[0])
72 	    {
73 	      menu->toprow  = 0;
74 	      menu->curitem = menu->items[0];
75 	      assert(menu->curitem);
76 	      set_menu_format( menu, menu->frows, menu->fcols );
77 	    }
78 	}
79 
80       menu->opt = opts;
81 
82       if (opts & O_ONEVALUE)
83 	{
84 	  ITEM **item;
85 
86 	  if ( ((item=menu->items) != (ITEM**)0) )
87 	    for(;*item;item++)
88 	      (*item)->value = FALSE;
89 	}
90 
91       if (opts & O_SHOWDESC)	/* this also changes the geometry */
92 	_nc_Calculate_Item_Length_and_Width( menu );
93     }
94   else
95     _nc_Default_Menu.opt = opts;
96 
97   RETURN(E_OK);
98 }
99 
100 /*---------------------------------------------------------------------------
101 |   Facility      :  libnmenu
102 |   Function      :  int menu_opts_off(MENU *menu, Menu_Options opts)
103 |
104 |   Description   :  Switch off the options for this menu. If the new settings
105 |                    end up in a change of the geometry of the menu, it
106 |                    will be recalculated. This operation is forbidden if
107 |                    the menu is already posted.
108 |
109 |   Return Values :  E_OK           - success
110 |                    E_BAD_ARGUMENT - invalid options
111 |                    E_POSTED       - menu is already posted
112 +--------------------------------------------------------------------------*/
113 NCURSES_EXPORT(int)
114 menu_opts_off (MENU *menu, Menu_Options  opts)
115 {
116   MENU *cmenu = menu; /* use a copy because set_menu_opts must detect
117                          NULL menu itself to adjust its behaviour */
118 
119   opts &= ALL_MENU_OPTS;
120   if (opts & ~ALL_MENU_OPTS)
121     RETURN(E_BAD_ARGUMENT);
122   else
123     {
124       Normalize_Menu(cmenu);
125       opts = cmenu->opt & ~opts;
126       return set_menu_opts( menu, opts );
127     }
128 }
129 
130 /*---------------------------------------------------------------------------
131 |   Facility      :  libnmenu
132 |   Function      :  int menu_opts_on(MENU *menu, Menu_Options opts)
133 |
134 |   Description   :  Switch on the options for this menu. If the new settings
135 |                    end up in a change of the geometry of the menu, it
136 |                    will be recalculated. This operation is forbidden if
137 |                    the menu is already posted.
138 |
139 |   Return Values :  E_OK           - success
140 |                    E_BAD_ARGUMENT - invalid menu options
141 |                    E_POSTED       - menu is already posted
142 +--------------------------------------------------------------------------*/
143 NCURSES_EXPORT(int)
144 menu_opts_on (MENU * menu, Menu_Options opts)
145 {
146   MENU *cmenu = menu; /* use a copy because set_menu_opts must detect
147                          NULL menu itself to adjust its behaviour */
148 
149   opts &= ALL_MENU_OPTS;
150   if (opts & ~ALL_MENU_OPTS)
151     RETURN(E_BAD_ARGUMENT);
152   else
153     {
154       Normalize_Menu(cmenu);
155       opts = cmenu->opt | opts;
156       return set_menu_opts(menu, opts);
157     }
158 }
159 
160 /*---------------------------------------------------------------------------
161 |   Facility      :  libnmenu
162 |   Function      :  Menu_Options menu_opts(const MENU *menu)
163 |
164 |   Description   :  Return the options for this menu.
165 |
166 |   Return Values :  Menu options
167 +--------------------------------------------------------------------------*/
168 NCURSES_EXPORT(Menu_Options)
169 menu_opts (const MENU *menu)
170 {
171   return (ALL_MENU_OPTS & Normalize_Menu( menu )->opt);
172 }
173 
174 /* m_opts.c ends here */
175