1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3 * Copyright 2019-2020,2021 Thomas E. Dickey *
4 * Copyright 1998-2011,2017 Free Software Foundation, Inc. *
6 * Permission is hereby granted, free of charge, to any person obtaining a *
7 * copy of this software and associated documentation files (the *
8 * "Software"), to deal in the Software without restriction, including *
9 * without limitation the rights to use, copy, modify, merge, publish, *
10 * distribute, distribute with modifications, sublicense, and/or sell *
11 * copies of the Software, and to permit persons to whom the Software is *
12 * furnished to do so, subject to the following conditions: *
14 * The above copyright notice and this permission notice shall be included *
15 * in all copies or substantial portions of the Software. *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
25 * Except as contained in this notice, the name(s) of the above copyright *
26 * holders shall not be used in advertising or otherwise to promote the *
27 * sale, use or other dealings in this Software without prior written *
29 ****************************************************************************/
31 /****************************************************************************
32 * Author: Juergen Pfeifer, 1997 *
33 ****************************************************************************/
37 #include "cursesapp.h"
39 MODULE_ID("$Id: cursesm.cc,v 1.27 2021/04/17 18:11:08 tom Exp $")
41 NCursesMenuItem::~NCursesMenuItem() THROWS(NCursesException)
44 OnError(::free_item(item));
48 NCursesMenuItem::action()
53 NCursesMenuCallbackItem::~NCursesMenuCallbackItem() THROWS(NCursesException)
58 NCursesMenuCallbackItem::action()
66 /* Internal hook functions. They will route the hook
67 * calls to virtual methods of the NCursesMenu class,
68 * so in C++ providing a hook is done simply by
69 * implementing a virtual method in a derived class
72 _nc_xx_mnu_init(MENU *m)
74 NCursesMenu::getHook(m)->On_Menu_Init();
78 _nc_xx_mnu_term(MENU *m)
80 NCursesMenu::getHook(m)->On_Menu_Termination();
84 _nc_xx_itm_init(MENU *m)
86 NCursesMenu* M = NCursesMenu::getHook(m);
87 M->On_Item_Init (*(M->current_item ()));
91 _nc_xx_itm_term(MENU *m)
93 NCursesMenu* M = NCursesMenu::getHook(m);
94 M->On_Item_Termination (*(M->current_item ()));
97 /* Construct an ITEM* array from an array of NCursesMenuItem
101 NCursesMenu::mapItems(NCursesMenuItem* nitems[])
103 int itemCount = 0,lcv;
105 for (lcv=0; nitems[lcv]->item; ++lcv)
108 ITEM** itemArray = new ITEM*[itemCount + 1];
110 for (lcv=0;nitems[lcv]->item;++lcv) {
111 itemArray[lcv] = nitems[lcv]->item;
113 itemArray[lcv] = NULL;
118 delete[] ::menu_items(menu);
123 NCursesMenu::InitMenu(NCursesMenuItem* nitems[],
125 bool autoDelete_Items)
132 b_framed = with_frame;
133 b_autoDelete = autoDelete_Items;
135 menu = static_cast<MENU*>(0);
136 menu = ::new_menu(mapItems(nitems));
138 OnError (E_SYSTEM_ERROR);
140 UserHook* hook = new UserHook;
143 hook->m_owner = menu;
144 ::set_menu_userptr(menu, static_cast<void*>(hook));
146 ::set_menu_init (menu, _nc_xx_mnu_init);
147 ::set_menu_term (menu, _nc_xx_mnu_term);
148 ::set_item_init (menu, _nc_xx_itm_init);
149 ::set_item_term (menu, _nc_xx_itm_term);
152 ::set_menu_win(menu, w);
155 if ((mrows > height()-2) || (mcols > width()-2))
157 sub = new NCursesWindow(*this,mrows,mcols,1,1,'r');
158 ::set_menu_sub(menu, sub->w);
162 sub = static_cast<NCursesWindow*>(0);
165 setDefaultAttributes();
169 NCursesMenu::setDefaultAttributes()
171 NCursesApplication* S = NCursesApplication::getApplication();
173 ::set_menu_fore(menu, S->foregrounds());
174 ::set_menu_back(menu, S->backgrounds());
175 ::set_menu_grey(menu, S->inactives());
179 NCursesMenu::~NCursesMenu() THROWS(NCursesException)
181 UserHook* hook = reinterpret_cast<UserHook*>(::menu_userptr(menu));
184 ::set_menu_sub(menu, static_cast<WINDOW *>(0));
188 ITEM** itms = ::menu_items(menu);
191 OnError(::set_menu_items(menu, static_cast<ITEM**>(0)));
195 for (int i=0; i <= cnt; i++)
202 // It's essential to do this after free_menu()
208 NCursesMenu::setSubWindow(NCursesWindow& nsub)
210 if (!isDescendant(nsub))
211 OnError(E_SYSTEM_ERROR);
216 ::set_menu_sub(menu,sub->w);
221 NCursesMenu::set_pattern (const char *pat)
223 int res = ::set_menu_pattern (menu, pat);
235 // call the menu driver and do basic error checking.
237 NCursesMenu::driver (int c)
239 int res = ::menu_driver (menu, c);
242 case E_REQUEST_DENIED:
243 case E_NOT_SELECTABLE:
244 case E_UNKNOWN_COMMAND:
253 static const int CMD_QUIT = MAX_COMMAND + 1;
254 static const int CMD_ACTION = MAX_COMMAND + 2;
256 // -------------------------------------------------------------------------
257 // Provide a default key virtualization. Translate the keyboard
258 // code c into a menu request code.
259 // The default implementation provides a hopefully straightforward
260 // mapping for the most common keystrokes and menu requests.
261 // -------------------------------------------------------------------------
263 NCursesMenu::virtualize(int c)
266 case CTRL('X') : return(CMD_QUIT); // eXit
268 case KEY_DOWN : return(REQ_DOWN_ITEM);
269 case CTRL('N') : return(REQ_NEXT_ITEM); // Next
270 case KEY_UP : return(REQ_UP_ITEM);
271 case CTRL('P') : return(REQ_PREV_ITEM); // Previous
273 case CTRL('U') : return(REQ_SCR_ULINE); // Up
274 case CTRL('D') : return(REQ_SCR_DLINE); // Down
275 case CTRL('F') : return(REQ_SCR_DPAGE); // Forward
276 case CTRL('B') : return(REQ_SCR_UPAGE); // Backward
278 case CTRL('Y') : return(REQ_CLEAR_PATTERN);
279 case CTRL('H') : return(REQ_BACK_PATTERN);
280 case CTRL('A') : return(REQ_NEXT_MATCH);
281 case CTRL('E') : return(REQ_PREV_MATCH);
282 case CTRL('T') : return(REQ_TOGGLE_ITEM);
285 case CTRL('M') : return(CMD_ACTION);
287 case KEY_HOME : return(REQ_FIRST_ITEM);
288 case KEY_LEFT : return(REQ_LEFT_ITEM);
289 case KEY_RIGHT : return(REQ_RIGHT_ITEM);
290 case KEY_END : return(REQ_LAST_ITEM);
291 case KEY_BACKSPACE : return(REQ_BACK_PATTERN);
292 case KEY_NPAGE : return(REQ_SCR_DPAGE);
293 case KEY_PPAGE : return(REQ_SCR_UPAGE);
301 NCursesMenu::operator()(void)
305 bool b_action = FALSE;
311 while (!b_action && ((drvCmnd = virtualize((c = getKey()))) != CMD_QUIT)) {
314 switch((err = driver(drvCmnd))) {
315 case E_REQUEST_DENIED:
316 On_Request_Denied(c);
318 case E_NOT_SELECTABLE:
319 On_Not_Selectable(c);
321 case E_UNKNOWN_COMMAND:
322 if (drvCmnd == CMD_ACTION) {
323 if (options() & O_ONEVALUE) {
324 NCursesMenuItem* itm = current_item();
326 if (itm->options() & O_SELECTABLE)
328 b_action = itm->action();
332 On_Not_Selectable(c);
336 for(int i=0; i<n; i++) {
337 NCursesMenuItem* itm = my_items[i];
339 b_action |= itm->action();
345 On_Unknown_Command(c);
360 if (options() & O_ONEVALUE)
361 return my_items[::item_index (::current_item (menu))];
367 NCursesMenu::On_Menu_Init()
372 NCursesMenu::On_Menu_Termination()
377 NCursesMenu::On_Item_Init(NCursesMenuItem& item)
383 NCursesMenu::On_Item_Termination(NCursesMenuItem& item)
389 NCursesMenu::On_Request_Denied(int c) const
396 NCursesMenu::On_Not_Selectable(int c) const
403 NCursesMenu::On_No_Match(int c) const
410 NCursesMenu::On_Unknown_Command(int c) const