1 // * this is for making emacs happy: -*-Mode: C++;-*-
2 /****************************************************************************
3 * Copyright (c) 1998,1999,2001 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 * Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en *
33 ****************************************************************************/
37 #include "cursesapp.h"
39 MODULE_ID("$Id: cursesm.cc,v 1.17 2002/07/06 15:47:52 juergen Exp $")
41 NCursesMenuItem::~NCursesMenuItem() {
43 OnError(::free_item(item));
47 NCursesMenuItem::action() {
51 NCursesMenuCallbackItem::~NCursesMenuCallbackItem() {
55 NCursesMenuCallbackItem::action() {
62 /* Internal hook functions. They will route the hook
63 * calls to virtual methods of the NCursesMenu class,
64 * so in C++ providing a hook is done simply by
65 * implementing a virtual method in a derived class
68 NCursesMenu::mnu_init(MENU *m) {
69 getHook(m)->On_Menu_Init();
73 NCursesMenu::mnu_term(MENU *m) {
74 getHook(m)->On_Menu_Termination();
78 NCursesMenu::itm_init(MENU *m) {
79 NCursesMenu* M = getHook(m);
80 M->On_Item_Init (*(M->current_item ()));
84 NCursesMenu::itm_term(MENU *m) {
85 NCursesMenu* M = getHook(m);
86 M->On_Item_Termination (*(M->current_item ()));
89 /* Construct an ITEM* array from an array of NCursesMenuItem
93 NCursesMenu::mapItems(NCursesMenuItem* nitems[]) {
94 int itemCount = 0,lcv;
96 for (lcv=0; nitems[lcv]->item; ++lcv)
99 ITEM** items = new ITEM*[itemCount + 1];
101 for (lcv=0;nitems[lcv]->item;++lcv) {
102 items[lcv] = nitems[lcv]->item;
109 delete[] ::menu_items(menu);
114 NCursesMenu::InitMenu(NCursesMenuItem* nitems[],
116 bool autoDelete_Items) {
122 b_framed = with_frame;
123 b_autoDelete = autoDelete_Items;
126 menu = ::new_menu(mapItems(nitems));
128 OnError (E_SYSTEM_ERROR);
130 UserHook* hook = new UserHook;
133 hook->m_owner = menu;
134 ::set_menu_userptr(menu,(void*)hook);
136 ::set_menu_init (menu, NCursesMenu::mnu_init);
137 ::set_menu_term (menu, NCursesMenu::mnu_term);
138 ::set_item_init (menu, NCursesMenu::itm_init);
139 ::set_item_term (menu, NCursesMenu::itm_term);
142 ::set_menu_win(menu, w);
145 if ((mrows > height()-2) || (mcols > width()-2))
147 sub = new NCursesWindow(*this,mrows,mcols,1,1,'r');
148 ::set_menu_sub(menu, sub->w);
152 sub = (NCursesWindow*)0;
155 setDefaultAttributes();
159 NCursesMenu::setDefaultAttributes() {
160 NCursesApplication* S = NCursesApplication::getApplication();
162 ::set_menu_fore(menu, S->foregrounds());
163 ::set_menu_back(menu, S->backgrounds());
164 ::set_menu_grey(menu, S->inactives());
168 NCursesMenu::~NCursesMenu() {
169 UserHook* hook = (UserHook*)::menu_userptr(menu);
173 ::set_menu_sub(menu,(WINDOW *)0);
176 ITEM** itms = ::menu_items(menu);
179 OnError(::set_menu_items(menu,(ITEM**)0));
183 for (int i=0; i <= cnt; i++)
190 // It's essential to do this after free_menu()
196 NCursesMenu::setSubWindow(NCursesWindow& nsub) {
197 if (!isDescendant(nsub))
198 OnError(E_SYSTEM_ERROR);
203 ::set_menu_sub(menu,sub->w);
208 NCursesMenu::set_pattern (const char *pat) {
209 int res = ::set_menu_pattern (menu, pat);
221 // call the menu driver and do basic error checking.
223 NCursesMenu::driver (int c) {
224 int res = ::menu_driver (menu, c);
227 case E_REQUEST_DENIED:
228 case E_NOT_SELECTABLE:
229 case E_UNKNOWN_COMMAND:
238 static const int CMD_QUIT = MAX_COMMAND + 1;
239 static const int CMD_ACTION = MAX_COMMAND + 2;
241 // -------------------------------------------------------------------------
242 // Provide a default key virtualization. Translate the keyboard
243 // code c into a menu request code.
244 // The default implementation provides a hopefully straightforward
245 // mapping for the most common keystrokes and menu requests.
246 // -------------------------------------------------------------------------
248 NCursesMenu::virtualize(int c) {
250 case CTRL('X') : return(CMD_QUIT); // eXit
252 case KEY_DOWN : return(REQ_DOWN_ITEM);
253 case CTRL('N') : return(REQ_NEXT_ITEM); // Next
254 case KEY_UP : return(REQ_UP_ITEM);
255 case CTRL('P') : return(REQ_PREV_ITEM); // Previous
257 case CTRL('U') : return(REQ_SCR_ULINE); // Up
258 case CTRL('D') : return(REQ_SCR_DLINE); // Down
259 case CTRL('F') : return(REQ_SCR_DPAGE); // Forward
260 case CTRL('B') : return(REQ_SCR_UPAGE); // Backward
262 case CTRL('Y') : return(REQ_CLEAR_PATTERN);
263 case CTRL('H') : return(REQ_BACK_PATTERN);
264 case CTRL('A') : return(REQ_NEXT_MATCH);
265 case CTRL('E') : return(REQ_PREV_MATCH);
266 case CTRL('T') : return(REQ_TOGGLE_ITEM);
269 case CTRL('M') : return(CMD_ACTION);
271 case KEY_HOME : return(REQ_FIRST_ITEM);
272 case KEY_LEFT : return(REQ_LEFT_ITEM);
273 case KEY_RIGHT : return(REQ_RIGHT_ITEM);
274 case KEY_END : return(REQ_LAST_ITEM);
275 case KEY_BACKSPACE : return(REQ_BACK_PATTERN);
276 case KEY_NPAGE : return(REQ_SCR_DPAGE);
277 case KEY_PPAGE : return(REQ_SCR_UPAGE);
285 NCursesMenu::operator()(void) {
289 bool b_action = FALSE;
295 while (!b_action && ((drvCmnd = virtualize((c=getKey()))) != CMD_QUIT)) {
297 switch((err=driver(drvCmnd))) {
298 case E_REQUEST_DENIED:
299 On_Request_Denied(c);
301 case E_NOT_SELECTABLE:
302 On_Not_Selectable(c);
304 case E_UNKNOWN_COMMAND:
305 if (drvCmnd == CMD_ACTION) {
306 if (options() & O_ONEVALUE) {
307 NCursesMenuItem* itm = current_item();
309 if (itm->options() & O_SELECTABLE)
311 b_action = itm->action();
315 On_Not_Selectable(c);
319 for(int i=0; i<n; i++) {
320 NCursesMenuItem* itm = my_items[i];
322 b_action |= itm->action();
328 On_Unknown_Command(c);
343 if (options() & O_ONEVALUE)
344 return my_items[::item_index (::current_item (menu))];
350 NCursesMenu::On_Menu_Init() {
354 NCursesMenu::On_Menu_Termination() {
358 NCursesMenu::On_Item_Init(NCursesMenuItem& item) {
362 NCursesMenu::On_Item_Termination(NCursesMenuItem& item) {
366 NCursesMenu::On_Request_Denied(int c) const {
371 NCursesMenu::On_Not_Selectable(int c) const {
376 NCursesMenu::On_No_Match(int c) const {
381 NCursesMenu::On_Unknown_Command(int c) const {