1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3 * Copyright (c) 1998-2005,2011 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Juergen Pfeifer, 1997 *
32 ****************************************************************************/
36 #include "cursesapp.h"
38 MODULE_ID("$Id: cursesm.cc,v 1.23 2011/09/17 22:11:32 tom Exp $")
40 NCursesMenuItem::~NCursesMenuItem()
43 OnError(::free_item(item));
47 NCursesMenuItem::action()
52 NCursesMenuCallbackItem::~NCursesMenuCallbackItem()
57 NCursesMenuCallbackItem::action()
65 /* Internal hook functions. They will route the hook
66 * calls to virtual methods of the NCursesMenu class,
67 * so in C++ providing a hook is done simply by
68 * implementing a virtual method in a derived class
71 _nc_xx_mnu_init(MENU *m)
73 NCursesMenu::getHook(m)->On_Menu_Init();
77 _nc_xx_mnu_term(MENU *m)
79 NCursesMenu::getHook(m)->On_Menu_Termination();
83 _nc_xx_itm_init(MENU *m)
85 NCursesMenu* M = NCursesMenu::getHook(m);
86 M->On_Item_Init (*(M->current_item ()));
90 _nc_xx_itm_term(MENU *m)
92 NCursesMenu* M = NCursesMenu::getHook(m);
93 M->On_Item_Termination (*(M->current_item ()));
96 /* Construct an ITEM* array from an array of NCursesMenuItem
100 NCursesMenu::mapItems(NCursesMenuItem* nitems[])
102 int itemCount = 0,lcv;
104 for (lcv=0; nitems[lcv]->item; ++lcv)
107 ITEM** itemArray = new ITEM*[itemCount + 1];
109 for (lcv=0;nitems[lcv]->item;++lcv) {
110 itemArray[lcv] = nitems[lcv]->item;
112 itemArray[lcv] = NULL;
117 delete[] ::menu_items(menu);
122 NCursesMenu::InitMenu(NCursesMenuItem* nitems[],
124 bool autoDelete_Items)
131 b_framed = with_frame;
132 b_autoDelete = autoDelete_Items;
134 menu = static_cast<MENU*>(0);
135 menu = ::new_menu(mapItems(nitems));
137 OnError (E_SYSTEM_ERROR);
139 UserHook* hook = new UserHook;
142 hook->m_owner = menu;
143 ::set_menu_userptr(menu, static_cast<void*>(hook));
145 ::set_menu_init (menu, _nc_xx_mnu_init);
146 ::set_menu_term (menu, _nc_xx_mnu_term);
147 ::set_item_init (menu, _nc_xx_itm_init);
148 ::set_item_term (menu, _nc_xx_itm_term);
151 ::set_menu_win(menu, w);
154 if ((mrows > height()-2) || (mcols > width()-2))
156 sub = new NCursesWindow(*this,mrows,mcols,1,1,'r');
157 ::set_menu_sub(menu, sub->w);
161 sub = static_cast<NCursesWindow*>(0);
164 setDefaultAttributes();
168 NCursesMenu::setDefaultAttributes()
170 NCursesApplication* S = NCursesApplication::getApplication();
172 ::set_menu_fore(menu, S->foregrounds());
173 ::set_menu_back(menu, S->backgrounds());
174 ::set_menu_grey(menu, S->inactives());
178 NCursesMenu::~NCursesMenu()
180 UserHook* hook = reinterpret_cast<UserHook*>(::menu_userptr(menu));
184 ::set_menu_sub(menu, static_cast<WINDOW *>(0));
187 ITEM** itms = ::menu_items(menu);
190 OnError(::set_menu_items(menu, static_cast<ITEM**>(0)));
194 for (int i=0; i <= cnt; i++)
201 // It's essential to do this after free_menu()
207 NCursesMenu::setSubWindow(NCursesWindow& nsub)
209 if (!isDescendant(nsub))
210 OnError(E_SYSTEM_ERROR);
215 ::set_menu_sub(menu,sub->w);
220 NCursesMenu::set_pattern (const char *pat)
222 int res = ::set_menu_pattern (menu, pat);
234 // call the menu driver and do basic error checking.
236 NCursesMenu::driver (int c)
238 int res = ::menu_driver (menu, c);
241 case E_REQUEST_DENIED:
242 case E_NOT_SELECTABLE:
243 case E_UNKNOWN_COMMAND:
252 static const int CMD_QUIT = MAX_COMMAND + 1;
253 static const int CMD_ACTION = MAX_COMMAND + 2;
255 // -------------------------------------------------------------------------
256 // Provide a default key virtualization. Translate the keyboard
257 // code c into a menu request code.
258 // The default implementation provides a hopefully straightforward
259 // mapping for the most common keystrokes and menu requests.
260 // -------------------------------------------------------------------------
262 NCursesMenu::virtualize(int c)
265 case CTRL('X') : return(CMD_QUIT); // eXit
267 case KEY_DOWN : return(REQ_DOWN_ITEM);
268 case CTRL('N') : return(REQ_NEXT_ITEM); // Next
269 case KEY_UP : return(REQ_UP_ITEM);
270 case CTRL('P') : return(REQ_PREV_ITEM); // Previous
272 case CTRL('U') : return(REQ_SCR_ULINE); // Up
273 case CTRL('D') : return(REQ_SCR_DLINE); // Down
274 case CTRL('F') : return(REQ_SCR_DPAGE); // Forward
275 case CTRL('B') : return(REQ_SCR_UPAGE); // Backward
277 case CTRL('Y') : return(REQ_CLEAR_PATTERN);
278 case CTRL('H') : return(REQ_BACK_PATTERN);
279 case CTRL('A') : return(REQ_NEXT_MATCH);
280 case CTRL('E') : return(REQ_PREV_MATCH);
281 case CTRL('T') : return(REQ_TOGGLE_ITEM);
284 case CTRL('M') : return(CMD_ACTION);
286 case KEY_HOME : return(REQ_FIRST_ITEM);
287 case KEY_LEFT : return(REQ_LEFT_ITEM);
288 case KEY_RIGHT : return(REQ_RIGHT_ITEM);
289 case KEY_END : return(REQ_LAST_ITEM);
290 case KEY_BACKSPACE : return(REQ_BACK_PATTERN);
291 case KEY_NPAGE : return(REQ_SCR_DPAGE);
292 case KEY_PPAGE : return(REQ_SCR_UPAGE);
300 NCursesMenu::operator()(void)
305 bool b_action = FALSE;
311 while (!b_action && ((drvCmnd = virtualize((c=getKey()))) != CMD_QUIT)) {
313 switch((err=driver(drvCmnd))) {
314 case E_REQUEST_DENIED:
315 On_Request_Denied(c);
317 case E_NOT_SELECTABLE:
318 On_Not_Selectable(c);
320 case E_UNKNOWN_COMMAND:
321 if (drvCmnd == CMD_ACTION) {
322 if (options() & O_ONEVALUE) {
323 NCursesMenuItem* itm = current_item();
325 if (itm->options() & O_SELECTABLE)
327 b_action = itm->action();
331 On_Not_Selectable(c);
335 for(int i=0; i<n; i++) {
336 NCursesMenuItem* itm = my_items[i];
338 b_action |= itm->action();
344 On_Unknown_Command(c);
359 if (options() & O_ONEVALUE)
360 return my_items[::item_index (::current_item (menu))];
366 NCursesMenu::On_Menu_Init()
371 NCursesMenu::On_Menu_Termination()
376 NCursesMenu::On_Item_Init(NCursesMenuItem& item)
382 NCursesMenu::On_Item_Termination(NCursesMenuItem& item)
388 NCursesMenu::On_Request_Denied(int c) const
395 NCursesMenu::On_Not_Selectable(int c) const
402 NCursesMenu::On_No_Match(int c) const
409 NCursesMenu::On_Unknown_Command(int c) const