libyui-ncurses
2.44.1
|
00001 /* 00002 Copyright (C) 2000-2012 Novell, Inc 00003 This library is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU Lesser General Public License as 00005 published by the Free Software Foundation; either version 2.1 of the 00006 License, or (at your option) version 3.0 of the License. This library 00007 is distributed in the hope that it will be useful, but WITHOUT ANY 00008 WARRANTY; without even the implied warranty of MERCHANTABILITY or 00009 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00010 License for more details. You should have received a copy of the GNU 00011 Lesser General Public License along with this library; if not, write 00012 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth 00013 Floor, Boston, MA 02110-1301 USA 00014 */ 00015 00016 00017 /*-/ 00018 00019 File: NCPopupMenu.cc 00020 00021 Author: Michael Andres <ma@suse.de> 00022 00023 /-*/ 00024 00025 #define YUILogComponent "ncurses" 00026 #include <yui/YUILog.h> 00027 #include "NCPopupMenu.h" 00028 00029 #include "NCTable.h" 00030 #include <yui/YMenuButton.h> 00031 00032 00033 NCPopupMenu::NCPopupMenu( const wpos at, YItemIterator begin, YItemIterator end ) 00034 : NCPopupTable( at ) 00035 , itemsMap() 00036 { 00037 std::vector<std::string> row( 2 ); 00038 createList( row ); 00039 00040 for ( YItemIterator it = begin; it != end; ++it ) 00041 { 00042 YMenuItem * item = dynamic_cast<YMenuItem *>( *it ); 00043 YUI_CHECK_PTR( item ); 00044 00045 row[0] = item->label(); 00046 row[1] = item->hasChildren() ? "..." : ""; 00047 00048 YTableItem *tableItem = new YTableItem( row[0], row[1] ); 00049 yuiDebug() << "Add to std::map: TableItem: " << tableItem << " Menu item: " << item << std::endl; 00050 00051 addItem( tableItem ); 00052 itemsMap[tableItem] = item; 00053 } 00054 00055 stripHotkeys(); 00056 } 00057 00058 00059 NCPopupMenu::~NCPopupMenu() 00060 { 00061 itemsMap.clear(); 00062 } 00063 00064 00065 NCursesEvent NCPopupMenu::wHandleInput( wint_t ch ) 00066 { 00067 NCursesEvent ret; 00068 00069 switch ( ch ) 00070 { 00071 case KEY_RIGHT: 00072 { 00073 yuiDebug() << "CurrentItem: " << getCurrentItem() << std::endl; 00074 YTableItem * tableItem = dynamic_cast<YTableItem *> ( getCurrentItemPointer() ); 00075 00076 if ( tableItem ) 00077 { 00078 YMenuItem * item = itemsMap[ tableItem ]; 00079 00080 if ( item && item->hasChildren() ) 00081 ret = NCursesEvent::button; 00082 } 00083 00084 break; 00085 } 00086 00087 case KEY_LEFT: 00088 ret = NCursesEvent::cancel; 00089 ret.detail = NCursesEvent::CONTINUE; 00090 break; 00091 00092 default: 00093 ret = NCPopup::wHandleInput( ch ); 00094 break; 00095 } 00096 00097 return ret; 00098 } 00099 00100 00101 bool NCPopupMenu::postAgain() 00102 { 00103 // dont mess up postevent.detail here 00104 bool again = false; 00105 int selection = ( postevent == NCursesEvent::button ) ? getCurrentItem() 00106 : -1; 00107 yuiDebug() << "Index: " << selection << std::endl; 00108 YTableItem * tableItem = dynamic_cast<YTableItem *>( getCurrentItemPointer() ); 00109 00110 YMenuItem * item = itemsMap[ tableItem ]; 00111 00112 if ( !item ) 00113 return false; 00114 00115 yuiMilestone() << "Menu item: " << item->label() << std::endl; 00116 00117 if ( selection != -1 ) 00118 { 00119 if ( item->hasChildren() ) 00120 { 00121 // post submenu 00122 wpos at( ScreenPos() + wpos( selection, inparent.Sze.W - 1 ) ); 00123 NCPopupMenu * dialog = new NCPopupMenu( at, 00124 item->childrenBegin(), 00125 item->childrenEnd() ); 00126 YUI_CHECK_NEW( dialog ); 00127 00128 again = ( dialog->post( &postevent ) == NCursesEvent::CONTINUE ); 00129 00130 if ( !again ) 00131 YDialog::deleteTopmostDialog(); 00132 } 00133 else 00134 { 00135 // store selection 00136 //postevent.detail = menu.itemList()[selection]->getIndex(); 00137 postevent.detail = item->index(); 00138 } 00139 } 00140 00141 return again; 00142 } 00143