libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/NCMultiSelectionBox.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:       NCMultiSelectionBox.cc
00020 
00021    Author:     Michael Andres <ma@suse.de>
00022 
00023 /-*/
00024 
00025 #define  YUILogComponent "ncurses"
00026 #include <yui/YUILog.h>
00027 #include "NCMultiSelectionBox.h"
00028 
00029 
00030 NCMultiSelectionBox::NCMultiSelectionBox( YWidget * parent,
00031                                           const std::string & nlabel )
00032         : YMultiSelectionBox( parent, nlabel )
00033         , NCPadWidget( parent )
00034 {
00035     yuiDebug() << std::endl;
00036     InitPad();
00037     setLabel( nlabel );
00038 }
00039 
00040 
00041 NCMultiSelectionBox::~NCMultiSelectionBox()
00042 {
00043     yuiDebug() << std::endl;
00044 }
00045 
00046 
00047 int NCMultiSelectionBox::preferredWidth()
00048 {
00049     wsze sze = wGetDefsze();
00050     return sze.W > ( int )( labelWidth() + 2 ) ? sze.W : ( labelWidth() + 2 );
00051 }
00052 
00053 
00054 int NCMultiSelectionBox::preferredHeight()
00055 {
00056     return wGetDefsze().H;
00057 }
00058 
00059 
00060 void NCMultiSelectionBox::setEnabled( bool do_bv )
00061 {
00062     NCWidget::setEnabled( do_bv );
00063     YMultiSelectionBox::setEnabled( do_bv );
00064 }
00065 
00066 
00067 void NCMultiSelectionBox::setSize( int newwidth, int newheight )
00068 {
00069     wRelocate( wpos( 0 ), wsze( newheight, newwidth ) );
00070 }
00071 
00072 
00073 YItem * NCMultiSelectionBox::currentItem()
00074 {
00075     if ( !myPad()->Lines() )
00076         return 0;
00077 
00078     int index = myPad()->CurPos().L;
00079 
00080     return itemAt( index );
00081 }
00082 
00083 
00084 
00085 void NCMultiSelectionBox::setCurrentItem( YItem * item )
00086 {
00087     if ( item )
00088         myPad()->ScrlLine( item->index() );
00089 }
00090 
00091 void NCMultiSelectionBox::addItem( YItem * item )
00092 {
00093     std::vector<NCTableCol*> Items( 2U, 0 );
00094 
00095     if ( item )
00096     {
00097         YMultiSelectionBox::addItem( item );
00098         Items[0] = new NCTableTag( item, item->selected() );
00099 
00100         // Do not set style to NCTableCol::PLAIN here, otherwise the current
00101         //item will not be highlighted if the cursor is not over the widget
00102 
00103         Items[1] = new NCTableCol( item->label() );
00104         myPad()->Append( Items );
00105         DrawPad();
00106     }
00107 }
00108 
00109 
00110 /**
00111  * Return pointer to current line tag
00112  * (holds state and yitem pointer)
00113  **/
00114 NCTableTag * NCMultiSelectionBox::tagCell( int index )
00115 {
00116     NCTableLine * cl = myPad()->ModifyLine( index );
00117 
00118     if ( !cl )
00119         return 0;
00120 
00121     return static_cast<NCTableTag *>( cl->GetCol( 0 ) );
00122 }
00123 
00124 
00125 const NCTableTag * NCMultiSelectionBox::tagCell( int index ) const
00126 {
00127     const NCTableLine * cl = myPad()->GetLine( index );
00128 
00129     if ( !cl )
00130         return 0;
00131 
00132     return static_cast<const NCTableTag *>( cl->GetCol( 0 ) );
00133 }
00134 
00135 
00136 
00137 void NCMultiSelectionBox::deleteAllItems()
00138 {
00139     YMultiSelectionBox::deleteAllItems();
00140     myPad()->ClearTable();
00141     DrawPad();
00142 }
00143 
00144 
00145 bool NCMultiSelectionBox::isItemSelected( YItem *item )
00146 {
00147     if ( item )
00148         return item->selected();
00149     else
00150         return false;
00151 }
00152 
00153 
00154 void NCMultiSelectionBox::selectItem( YItem *yitem, bool selected )
00155 {
00156     if ( yitem )
00157     {
00158         YMultiSelectionBox::selectItem( yitem, selected );
00159 
00160         //retrieve pointer to the line tag associated with this item
00161         NCTableTag * tag = ( NCTableTag * )yitem->data();
00162         YUI_CHECK_PTR( tag );
00163 
00164         tag->SetSelected( selected );
00165 
00166         DrawPad();
00167     }
00168 }
00169 
00170 
00171 void NCMultiSelectionBox::deselectAllItems()
00172 {
00173     YMultiSelectionBox::deselectAllItems();
00174 
00175     for ( unsigned int i = 0; i < getNumLines(); i++ )
00176     {
00177         NCTableTag *t = tagCell( i );
00178         YUI_CHECK_PTR( t );
00179 
00180         t->SetSelected( false );
00181     }
00182 
00183     DrawPad();
00184 }
00185 
00186 
00187 
00188 /**
00189  * Toggle item from selected -> deselected and vice versa
00190  **/
00191 void NCMultiSelectionBox::toggleCurrentItem()
00192 {
00193     YItem *it = currentItem();
00194     if ( it )
00195         selectItem( it, !( it->selected() ) );
00196 }
00197 
00198 
00199 void NCMultiSelectionBox::setLabel( const std::string & nlabel )
00200 {
00201     YMultiSelectionBox::setLabel( nlabel );
00202     NCPadWidget::setLabel( NCstring( nlabel ) );
00203 }
00204 
00205 
00206 /**
00207  * Create empty MsB pad
00208  **/
00209 NCPad * NCMultiSelectionBox::CreatePad()
00210 {
00211     wsze psze( defPadSze() );
00212     NCTablePad * npad = new NCTablePad( psze.H, psze.W, *this );
00213     npad->bkgd( listStyle().item.plain );
00214     npad->SetSepChar( ' ' );
00215     return npad;
00216 }
00217 
00218 
00219 void NCMultiSelectionBox::wRecoded()
00220 {
00221     NCPadWidget::wRecoded();
00222 }
00223 
00224 
00225 NCursesEvent NCMultiSelectionBox::wHandleInput( wint_t key )
00226 {
00227     NCursesEvent ret;
00228     bool valueChanged = false;
00229     YItem *oldCurrentItem = currentItem();
00230 
00231     if ( ! handleInput( key ) )
00232     {
00233         YItem *citem = currentItem();
00234 
00235         switch ( key )
00236         {
00237             case KEY_SPACE:
00238 
00239             case KEY_RETURN:
00240                 toggleCurrentItem();
00241                 valueChanged = true;
00242                 break;
00243 
00244             case '+':
00245 
00246                 if ( !isItemSelected( citem ) )
00247                 {
00248                     selectItem( citem, true );
00249                     valueChanged = true;
00250                 }
00251 
00252                 myPad()->ScrlDown();
00253 
00254                 break;
00255 
00256             case '-':
00257 
00258                 if ( isItemSelected( citem ) )
00259                 {
00260                     selectItem( citem, false );
00261                     valueChanged = true;
00262                 }
00263 
00264                 myPad()->ScrlDown();
00265 
00266                 break;
00267         }
00268     }
00269 
00270     if ( notify() )
00271     {
00272         if ( valueChanged )
00273             ret = NCursesEvent::ValueChanged;
00274         else if ( oldCurrentItem != currentItem() )
00275             ret = NCursesEvent::SelectionChanged;
00276     }
00277 
00278     return ret;
00279 }
00280 
 All Classes Functions Variables