libyui-ncurses  2.44.1
 All Classes Functions Variables
NCPopupMenu.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: NCPopupMenu.cc
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCPopupMenu.h"
28 
29 #include "NCTable.h"
30 #include <yui/YMenuButton.h>
31 
32 
33 NCPopupMenu::NCPopupMenu( const wpos at, YItemIterator begin, YItemIterator end )
34  : NCPopupTable( at )
35  , itemsMap()
36 {
37  std::vector<std::string> row( 2 );
38  createList( row );
39 
40  for ( YItemIterator it = begin; it != end; ++it )
41  {
42  YMenuItem * item = dynamic_cast<YMenuItem *>( *it );
43  YUI_CHECK_PTR( item );
44 
45  row[0] = item->label();
46  row[1] = item->hasChildren() ? "..." : "";
47 
48  YTableItem *tableItem = new YTableItem( row[0], row[1] );
49  yuiDebug() << "Add to std::map: TableItem: " << tableItem << " Menu item: " << item << std::endl;
50 
51  addItem( tableItem );
52  itemsMap[tableItem] = item;
53  }
54 
55  stripHotkeys();
56 }
57 
58 
59 NCPopupMenu::~NCPopupMenu()
60 {
61  itemsMap.clear();
62 }
63 
64 
65 NCursesEvent NCPopupMenu::wHandleInput( wint_t ch )
66 {
67  NCursesEvent ret;
68 
69  switch ( ch )
70  {
71  case KEY_RIGHT:
72  {
73  yuiDebug() << "CurrentItem: " << getCurrentItem() << std::endl;
74  YTableItem * tableItem = dynamic_cast<YTableItem *> ( getCurrentItemPointer() );
75 
76  if ( tableItem )
77  {
78  YMenuItem * item = itemsMap[ tableItem ];
79 
80  if ( item && item->hasChildren() )
81  ret = NCursesEvent::button;
82  }
83 
84  break;
85  }
86 
87  case KEY_LEFT:
88  ret = NCursesEvent::cancel;
89  ret.detail = NCursesEvent::CONTINUE;
90  break;
91 
92  default:
93  ret = NCPopup::wHandleInput( ch );
94  break;
95  }
96 
97  return ret;
98 }
99 
100 
101 bool NCPopupMenu::postAgain()
102 {
103  // dont mess up postevent.detail here
104  bool again = false;
105  int selection = ( postevent == NCursesEvent::button ) ? getCurrentItem()
106  : -1;
107  yuiDebug() << "Index: " << selection << std::endl;
108  YTableItem * tableItem = dynamic_cast<YTableItem *>( getCurrentItemPointer() );
109 
110  YMenuItem * item = itemsMap[ tableItem ];
111 
112  if ( !item )
113  return false;
114 
115  yuiMilestone() << "Menu item: " << item->label() << std::endl;
116 
117  if ( selection != -1 )
118  {
119  if ( item->hasChildren() )
120  {
121  // post submenu
122  wpos at( ScreenPos() + wpos( selection, inparent.Sze.W - 1 ) );
123  NCPopupMenu * dialog = new NCPopupMenu( at,
124  item->childrenBegin(),
125  item->childrenEnd() );
126  YUI_CHECK_NEW( dialog );
127 
128  again = ( dialog->post( &postevent ) == NCursesEvent::CONTINUE );
129 
130  if ( !again )
131  YDialog::deleteTopmostDialog();
132  }
133  else
134  {
135  // store selection
136  //postevent.detail = menu.itemList()[selection]->getIndex();
137  postevent.detail = item->index();
138  }
139  }
140 
141  return again;
142 }
143 
Definition: position.h:109