1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3 * Copyright (c) 1998-2002,2003 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.18 2003/10/25 15:04:46 tom Exp $")
40 NCursesMenuItem::~NCursesMenuItem() {
42 OnError(::free_item(item));
46 NCursesMenuItem::action() {
50 NCursesMenuCallbackItem::~NCursesMenuCallbackItem() {
54 NCursesMenuCallbackItem::action() {
61 /* Internal hook functions. They will route the hook
62 * calls to virtual methods of the NCursesMenu class,
63 * so in C++ providing a hook is done simply by
64 * implementing a virtual method in a derived class
67 NCursesMenu::mnu_init(MENU *m) {
68 getHook(m)->On_Menu_Init();
72 NCursesMenu::mnu_term(MENU *m) {
73 getHook(m)->On_Menu_Termination();
77 NCursesMenu::itm_init(MENU *m) {
78 NCursesMenu* M = getHook(m);
79 M->On_Item_Init (*(M->current_item ()));
83 NCursesMenu::itm_term(MENU *m) {
84 NCursesMenu* M = getHook(m);
85 M->On_Item_Termination (*(M->current_item ()));
88 /* Construct an ITEM* array from an array of NCursesMenuItem
92 NCursesMenu::mapItems(NCursesMenuItem* nitems[]) {
93 int itemCount = 0,lcv;
95 for (lcv=0; nitems[lcv]->item; ++lcv)
98 ITEM** items = new ITEM*[itemCount + 1];
100 for (lcv=0;nitems[lcv]->item;++lcv) {
101 items[lcv] = nitems[lcv]->item;
108 delete[] ::menu_items(menu);
113 NCursesMenu::InitMenu(NCursesMenuItem* nitems[],
115 bool autoDelete_Items) {
121 b_framed = with_frame;
122 b_autoDelete = autoDelete_Items;
125 menu = ::new_menu(mapItems(nitems));
127 OnError (E_SYSTEM_ERROR);
129 UserHook* hook = new UserHook;
132 hook->m_owner = menu;
133 ::set_menu_userptr(menu,(void*)hook);
135 ::set_menu_init (menu, NCursesMenu::mnu_init);
136 ::set_menu_term (menu, NCursesMenu::mnu_term);
137 ::set_item_init (menu, NCursesMenu::itm_init);
138 ::set_item_term (menu, NCursesMenu::itm_term);
141 ::set_menu_win(menu, w);
144 if ((mrows > height()-2) || (mcols > width()-2))
146 sub = new NCursesWindow(*this,mrows,mcols,1,1,'r');
147 ::set_menu_sub(menu, sub->w);
151 sub = (NCursesWindow*)0;
154 setDefaultAttributes();
158 NCursesMenu::setDefaultAttributes() {
159 NCursesApplication* S = NCursesApplication::getApplication();
161 ::set_menu_fore(menu, S->foregrounds());
162 ::set_menu_back(menu, S->backgrounds());
163 ::set_menu_grey(menu, S->inactives());
167 NCursesMenu::~NCursesMenu() {
168 UserHook* hook = (UserHook*)::menu_userptr(menu);
172 ::set_menu_sub(menu,(WINDOW *)0);
175 ITEM** itms = ::menu_items(menu);
178 OnError(::set_menu_items(menu,(ITEM**)0));
182 for (int i=0; i <= cnt; i++)
189 // It's essential to do this after free_menu()
195 NCursesMenu::setSubWindow(NCursesWindow& nsub) {
196 if (!isDescendant(nsub))
197 OnError(E_SYSTEM_ERROR);
202 ::set_menu_sub(menu,sub->w);
207 NCursesMenu::set_pattern (const char *pat) {
208 int res = ::set_menu_pattern (menu, pat);
220 // call the menu driver and do basic error checking.
222 NCursesMenu::driver (int c) {
223 int res = ::menu_driver (menu, c);
226 case E_REQUEST_DENIED:
227 case E_NOT_SELECTABLE:
228 case E_UNKNOWN_COMMAND:
237 static const int CMD_QUIT = MAX_COMMAND + 1;
238 static const int CMD_ACTION = MAX_COMMAND + 2;
240 // -------------------------------------------------------------------------
241 // Provide a default key virtualization. Translate the keyboard
242 // code c into a menu request code.
243 // The default implementation provides a hopefully straightforward
244 // mapping for the most common keystrokes and menu requests.
245 // -------------------------------------------------------------------------
247 NCursesMenu::virtualize(int c) {
249 case CTRL('X') : return(CMD_QUIT); // eXit
251 case KEY_DOWN : return(REQ_DOWN_ITEM);
252 case CTRL('N') : return(REQ_NEXT_ITEM); // Next
253 case KEY_UP : return(REQ_UP_ITEM);
254 case CTRL('P') : return(REQ_PREV_ITEM); // Previous
256 case CTRL('U') : return(REQ_SCR_ULINE); // Up
257 case CTRL('D') : return(REQ_SCR_DLINE); // Down
258 case CTRL('F') : return(REQ_SCR_DPAGE); // Forward
259 case CTRL('B') : return(REQ_SCR_UPAGE); // Backward
261 case CTRL('Y') : return(REQ_CLEAR_PATTERN);
262 case CTRL('H') : return(REQ_BACK_PATTERN);
263 case CTRL('A') : return(REQ_NEXT_MATCH);
264 case CTRL('E') : return(REQ_PREV_MATCH);
265 case CTRL('T') : return(REQ_TOGGLE_ITEM);
268 case CTRL('M') : return(CMD_ACTION);
270 case KEY_HOME : return(REQ_FIRST_ITEM);
271 case KEY_LEFT : return(REQ_LEFT_ITEM);
272 case KEY_RIGHT : return(REQ_RIGHT_ITEM);
273 case KEY_END : return(REQ_LAST_ITEM);
274 case KEY_BACKSPACE : return(REQ_BACK_PATTERN);
275 case KEY_NPAGE : return(REQ_SCR_DPAGE);
276 case KEY_PPAGE : return(REQ_SCR_UPAGE);
284 NCursesMenu::operator()(void) {
288 bool b_action = FALSE;
294 while (!b_action && ((drvCmnd = virtualize((c=getKey()))) != CMD_QUIT)) {
296 switch((err=driver(drvCmnd))) {
297 case E_REQUEST_DENIED:
298 On_Request_Denied(c);
300 case E_NOT_SELECTABLE:
301 On_Not_Selectable(c);
303 case E_UNKNOWN_COMMAND:
304 if (drvCmnd == CMD_ACTION) {
305 if (options() & O_ONEVALUE) {
306 NCursesMenuItem* itm = current_item();
308 if (itm->options() & O_SELECTABLE)
310 b_action = itm->action();
314 On_Not_Selectable(c);
318 for(int i=0; i<n; i++) {
319 NCursesMenuItem* itm = my_items[i];
321 b_action |= itm->action();
327 On_Unknown_Command(c);
342 if (options() & O_ONEVALUE)
343 return my_items[::item_index (::current_item (menu))];
349 NCursesMenu::On_Menu_Init() {
353 NCursesMenu::On_Menu_Termination() {
357 NCursesMenu::On_Item_Init(NCursesMenuItem& item) {
361 NCursesMenu::On_Item_Termination(NCursesMenuItem& item) {
365 NCursesMenu::On_Request_Denied(int c) const {
370 NCursesMenu::On_Not_Selectable(int c) const {
375 NCursesMenu::On_No_Match(int c) const {
380 NCursesMenu::On_Unknown_Command(int c) const {