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