libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/NCSelectionBox.cc
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:       NCSelectionBox.cc
00020 
00021    Author:     Michael Andres <ma@suse.de>
00022 
00023 /-*/
00024 
00025 #define  YUILogComponent "ncurses"
00026 #include <yui/YUILog.h>
00027 #include "NCSelectionBox.h"
00028 
00029 
00030 
00031 NCSelectionBox::NCSelectionBox( YWidget * parent, const std::string & nlabel )
00032         : YSelectionBox( parent, nlabel )
00033         , NCPadWidget( parent )
00034         , biglist( false )
00035 {
00036     yuiDebug() << std::endl;
00037     InitPad();
00038     setLabel( nlabel );
00039 }
00040 
00041 
00042 NCSelectionBox::~NCSelectionBox()
00043 {
00044     yuiDebug() << std::endl;
00045 }
00046 
00047 
00048 int NCSelectionBox::preferredWidth()
00049 {
00050     wsze sze = ( biglist ) ? myPad()->tableSize() + 2 : wGetDefsze();
00051     return sze.W > ( int )( labelWidth() + 2 ) ? sze.W : ( labelWidth() + 2 );
00052 }
00053 
00054 
00055 int NCSelectionBox::preferredHeight()
00056 {
00057     wsze sze = ( biglist ) ? myPad()->tableSize() + 2 : wGetDefsze();
00058     return sze.H;
00059 }
00060 
00061 
00062 void NCSelectionBox::setSize( int newwidth, int newheight )
00063 {
00064     wRelocate( wpos( 0 ), wsze( newheight, newwidth ) );
00065 }
00066 
00067 
00068 void NCSelectionBox::setEnabled( bool do_bv )
00069 {
00070     NCWidget::setEnabled( do_bv );
00071     YSelectionBox::setEnabled( do_bv );
00072 }
00073 
00074 
00075 int NCSelectionBox::getCurrentItem()
00076 {
00077     if ( !myPad()->Lines() )
00078         return -1;
00079 
00080     yuiDebug() << "Current pos: " << myPad()->CurPos().L << std::endl;
00081 
00082     return myPad()->CurPos().L;
00083 }
00084 
00085 
00086 std::string NCSelectionBox::getLine( const int & index )
00087 {
00088     NCTableLine * line = const_cast<NCTableLine*>( myPad()->GetLine( index ) );
00089     NCTableCol * value;
00090     std::string val;
00091 
00092     if ( line->Cols() == 1 )
00093     {
00094         value = line->GetItems()[0];
00095         const NClabel label = value->Label();
00096         const std::list<NCstring> text = label.getText();
00097         std::list<NCstring>::const_iterator it = text.begin();
00098 
00099         while ( it != text.end() )
00100         {
00101             val += ( *it ).Str();
00102             ++it;
00103         }
00104     }
00105 
00106     return val;
00107 }
00108 
00109 
00110 void NCSelectionBox::setCurrentItem( int index )
00111 {
00112     myPad()->ScrlLine( index );
00113 }
00114 
00115 
00116 void NCSelectionBox::selectItem( YItem *item, bool selected )
00117 {
00118     YSelectionBox::selectItem( item, selected );
00119 
00120     myPad()->ScrlLine( selected ? item->index() : -1 );
00121 }
00122 
00123 
00124 void NCSelectionBox::selectItem( int index )
00125 {
00126     YSelectionBox::deselectAllItems();
00127 
00128     if ( hasItems() && index >= 0 )
00129     {
00130         YItem * item = YSelectionBox::itemAt( index );
00131 
00132         if ( item )
00133         {
00134             yuiDebug() << "selectItem:  " << item->label().c_str() << std::endl;
00135             item->setSelected( true );
00136         }
00137         else
00138             YUI_THROW( YUIException( "Can't find selected item" ) );
00139     }
00140 }
00141 
00142 
00143 void NCSelectionBox::addItem( YItem * item )
00144 {
00145     std::vector<NCTableCol*> Items( 1U, 0 );
00146 
00147     if ( item )
00148     {
00149         YSelectionBox::addItem( item );
00150         Items[0] = new NCTableCol( item->label() );
00151         myPad()->Append( Items );
00152         DrawPad();
00153 
00154         if ( item->selected() )
00155             myPad()->ScrlLine( myPad()->Lines() );
00156     }
00157 }
00158 
00159 
00160 void NCSelectionBox::addItem( const std::string & description, bool selected )
00161 {
00162     YSelectionWidget::addItem( description, selected );
00163 }
00164 
00165 
00166 void NCSelectionBox::setLabel( const std::string & nlabel )
00167 {
00168     YSelectionBox::setLabel( nlabel );
00169     NCPadWidget::setLabel( NCstring( nlabel ) );
00170 }
00171 
00172 
00173 NCPad * NCSelectionBox::CreatePad()
00174 {
00175     wsze    psze( defPadSze() );
00176     NCPad * npad = new NCTablePad( psze.H, psze.W, *this );
00177     npad->bkgd( listStyle().item.plain );
00178 
00179     return npad;
00180 }
00181 
00182 
00183 void NCSelectionBox::wRecoded()
00184 {
00185     NCPadWidget::wRecoded();
00186 }
00187 
00188 
00189 NCursesEvent NCSelectionBox::wHandleInput( wint_t key )
00190 {
00191     NCursesEvent ret = NCursesEvent::none;
00192 
00193     int oldItem = getCurrentItem();
00194 
00195     // handle key event first
00196 
00197     if ( sendKeyEvents() &&
00198          ( key == KEY_LEFT || key == KEY_RIGHT ) )
00199     {
00200         ret = NCursesEvent::key;
00201 
00202         switch ( key )
00203         {
00204             case KEY_LEFT:
00205                 ret.keySymbol = "CursorLeft";
00206                 break;
00207 
00208             case KEY_RIGHT:
00209                 ret.keySymbol = "CursorRight";
00210                 break;
00211         }
00212 
00213         return ret;
00214     }
00215 
00216     // call handleInput of NCPad
00217     handleInput( key );
00218 
00219     int citem = getCurrentItem();
00220 
00221     selectItem( citem );
00222 
00223     switch ( key )
00224     {
00225         case KEY_SPACE:
00226         case KEY_RETURN:
00227 
00228             if ( notify() && citem != -1 )
00229             {
00230                 return NCursesEvent::Activated;
00231             }
00232 
00233             break;
00234     }
00235 
00236     if ( notify() && immediateMode() && oldItem != citem )
00237     {
00238         ret = NCursesEvent::SelectionChanged;
00239     }
00240 
00241     return ret;
00242 }
00243 
00244 
00245 /**
00246  * Clear the table and the lists holding the values
00247  **/
00248 void NCSelectionBox::deleteAllItems()
00249 {
00250     YSelectionBox::deleteAllItems();
00251     clearTable();
00252     DrawPad();
00253 }
 All Classes Functions Variables