27 #define YUILogComponent "mga-ncurses"
28 #include <yui/YUILog.h>
29 #include "NCMGAPopupMenu.h"
30 #include "YMGAMenuItem.h"
32 #include <yui/ncurses/NCTable.h>
42 std::map<YMGAMenuItem *, YMGAMenuItem *> itemsMap;
46 NCMGAPopupMenu::NCMGAPopupMenu(
const wpos & at, YItemIterator begin, YItemIterator end )
54 std::vector<std::string> row( 2 );
56 d->menu =
new NCMenu(
this );
62 yuiDebug() <<
"Menu position: " << at << std::endl;
65 for ( YItemIterator it = begin; it != end; ++it )
67 YMenuSeparator *separator =
dynamic_cast<YMenuSeparator *
>( *it );
70 YMenuSeparator * sep =
new YMenuSeparator(NULL);
71 d->menu->addItem( sep );
72 defsze.H = defsze.H > 9 ? 10 : defsze.H + 1;
76 YMGAMenuItem * item =
dynamic_cast<YMGAMenuItem *
>( *it );
77 YUI_CHECK_PTR( item );
79 std::string label = item->hasChildren() ? item->label() +
" ..." : item->label();
80 YMGAMenuItem *menuItem =
new YMGAMenuItem ( label, item->iconName() );
81 menuItem->enable(item->enabled());
82 menuItem->hide(item->hidden());
84 d->menu->addItem( menuItem );
85 d->itemsMap[menuItem] = item;
87 if (d->maxlen < label.length()+1)
88 d->maxlen = label.length()+1;
90 int h = defsze.H > 9 ? 10 : defsze.H + 1;
92 int w = d->maxlen > 40 ? 40 : d->maxlen;
94 defsze = wsze( h, w );
95 yuiDebug() <<
"Add Item: " << item->label() <<
" len: "<< label.length() <<
" HxW "<< h <<
"x" << w << std::endl;
98 yuiDebug() <<
"defsze: " << defsze <<
"line length: " << d->maxlen << std::endl;
104 NCMGAPopupMenu::~NCMGAPopupMenu()
110 int NCMGAPopupMenu::preferredHeight()
115 int NCMGAPopupMenu::preferredWidth()
121 bool NCMGAPopupMenu::HasHotkey(
int key)
123 yuiDebug() << key << std::endl;
125 return d->menu->HasHotkey(key);
128 NCursesEvent NCMGAPopupMenu::wHandleHotkey( wint_t key )
130 yuiDebug() <<
"Key: " << key << std::endl;
131 if ( key >= 0 && key < UCHAR_MAX )
133 NCursesEvent ev = d->menu->wHandleHotkey(key);
134 yuiDebug() <<
"event: " << ev << std::endl;
135 if (ev != NCursesEvent::none)
136 return wHandleInput( KEY_SPACE );
138 return NCursesEvent::none;
142 NCursesEvent NCMGAPopupMenu::wHandleInput( wint_t ch )
147 yuiDebug() <<
"ch: " << int(ch) << std::endl;
152 YMGAMenuItem * selitem =
dynamic_cast<YMGAMenuItem *
>(d->menu->currentItem());
155 YMGAMenuItem * item = d->itemsMap[ selitem ];
156 if (item->hasChildren() )
158 ret = NCursesEvent::button;
166 ret = NCursesEvent::button;
170 ret = NCursesEvent::cancel;
171 ret.detail = NCursesEvent::CONTINUE;
175 ret = NCPopup::wHandleInput( ch );
183 bool NCMGAPopupMenu::postAgain()
190 YMGAMenuItem * selected =
dynamic_cast<YMGAMenuItem *
>(d->menu->currentItem());
198 YMGAMenuItem * item = d->itemsMap[ selected ];
199 yuiMilestone() <<
"Menu item: " << item->label() <<
" " << item->index() << std::endl;
201 if ( item->hasChildren() )
204 wpos at( ScreenPos() + wpos( selected->index(), inparent.Sze.W - 1 ) );
205 yuiDebug() <<
"Submenu " << item->label() <<
" position: " << at << std::endl;
208 item->childrenBegin(),
209 item->childrenEnd() );
210 YUI_CHECK_NEW( dialog );
212 again = ( dialog->post( &postevent ) == NCursesEvent::CONTINUE );
214 YDialog::deleteTopmostDialog();
219 postevent.detail = item->index();