libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/NCDumbTab.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:       NCDumbTab.cc
00020 
00021    Author:     Gabriele Mohr <gs@suse.de>
00022 
00023 /-*/
00024 
00025 #define  YUILogComponent "ncurses"
00026 #include <yui/YUILog.h>
00027 #include <yui/YDialog.h>
00028 #include "NCDialog.h"
00029 #include "NCurses.h"
00030 #include "NCDumbTab.h"
00031 #include "NCPopupList.h"
00032 
00033 
00034 NCDumbTab::NCDumbTab( YWidget * parent )
00035         : YDumbTab( parent )
00036         , NCWidget( parent )
00037         , currentIndex( 0 )
00038 {
00039     framedim.Pos = wpos( 1 );
00040     framedim.Sze = wsze( 2 );
00041 }
00042 
00043 
00044 NCDumbTab::~NCDumbTab()
00045 {
00046     yuiDebug() << std::endl;
00047 }
00048 
00049 
00050 int NCDumbTab::preferredWidth()
00051 {
00052     defsze.W = hasChildren() ? firstChild()->preferredWidth() : 0;
00053 
00054     YItemIterator listIt = itemsBegin();
00055 
00056     unsigned int tabBarWidth = 0;
00057     NClabel tabLabel;
00058 
00059     while ( listIt != itemsEnd() )
00060     {
00061         tabLabel = NClabel( (*listIt)->label() );
00062         tabBarWidth += tabLabel.width() + 1;
00063         ++listIt;
00064     }
00065     ++tabBarWidth;
00066 
00067     if ( tabBarWidth > ( unsigned )defsze.W )
00068         defsze.W = tabBarWidth;
00069 
00070     defsze.W += framedim.Sze.W;
00071 
00072     if ( defsze.W > NCurses::cols() )
00073         defsze.W = NCurses::cols();
00074 
00075     return defsze.W;
00076 }
00077 
00078 
00079 int NCDumbTab::preferredHeight()
00080 {
00081     defsze.H  = hasChildren() ? firstChild()->preferredHeight() : 0;
00082     defsze.H += framedim.Sze.H;
00083 
00084     return defsze.H;
00085 }
00086 
00087 
00088 void NCDumbTab::setEnabled( bool do_bv )
00089 {
00090     yuiDebug() << "Set enabled" << std::endl;
00091     NCWidget::setEnabled( do_bv );
00092     YDumbTab::setEnabled( do_bv );
00093 }
00094 
00095 
00096 void NCDumbTab::setSize( int newwidth, int newheight )
00097 {
00098     wsze csze( newheight, newwidth );
00099     wRelocate( wpos( 0 ), csze );
00100     csze = wsze::max( 0, csze - framedim.Sze );
00101 
00102     if ( hasChildren() )
00103         firstChild()->setSize( csze.W, csze.H );
00104 }
00105 
00106 NCursesEvent NCDumbTab::wHandleInput( wint_t key )
00107 {
00108     NCursesEvent ret = NCursesEvent::none;
00109 
00110     switch ( key )
00111     {
00112         case KEY_LEFT:
00113             if ( currentIndex > 0 &&
00114                  currentIndex <= (unsigned)itemsCount() -1 )
00115             {
00116                 currentIndex--;
00117                 wRedraw();
00118 
00119                 ret = createMenuEvent( currentIndex );
00120             }
00121             break;
00122 
00123         case KEY_RIGHT:
00124             if ( currentIndex < (unsigned)itemsCount()-1 &&
00125                  currentIndex >= 0 )
00126             {
00127                 currentIndex++;
00128                 wRedraw();
00129 
00130                 ret = createMenuEvent( currentIndex );
00131             }
00132             break;
00133 
00134         case KEY_HOTKEY:
00135             setCurrentTab( hotKey );
00136 
00137         case KEY_RETURN:
00138             ret = createMenuEvent( currentIndex );
00139             break;
00140 
00141     }
00142 
00143     return ret;
00144 }
00145 
00146 void NCDumbTab::setCurrentTab( wint_t key )
00147 {
00148 
00149     YItemIterator listIt = itemsBegin();
00150     NClabel tablabel;
00151     unsigned int i = 0;
00152 
00153     while ( listIt != itemsEnd() )
00154     {
00155         tablabel = NCstring( (*listIt)->label() );
00156         tablabel.stripHotkey();
00157         yuiDebug() << "HOTkey: " <<  tablabel.hotkey() << " key: " << key << std::endl;
00158         if ( tolower ( tablabel.hotkey() )  == tolower ( key ) )
00159         {
00160             currentIndex = i;
00161             break;
00162         }
00163         ++listIt;
00164         ++i;
00165     }
00166 }
00167 
00168 NCursesEvent NCDumbTab::createMenuEvent( unsigned int index )
00169 {
00170     NCursesEvent ret = NCursesEvent::menu;
00171     YItem * item;
00172 
00173     item = itemAt( index );
00174     if ( item )
00175     {
00176         yuiMilestone() << "Show tab: " << item->label() << std::endl;
00177         ret.selection = (YMenuItem *)item;
00178     }
00179 
00180     return ret;
00181 }
00182 
00183 void NCDumbTab::addItem( YItem * item )
00184 {
00185     YDumbTab::addItem( item );
00186 
00187     NClabel tabLabel = NCstring( item->label() );
00188     yuiDebug() << "Add item: " << item->label() << std::endl;
00189 
00190     if ( item->selected() )
00191         currentIndex = item->index();
00192 }
00193 
00194 void NCDumbTab::selectItem( YItem * item, bool selected )
00195 {
00196     if ( selected )
00197     {
00198         currentIndex = item->index();
00199         yuiDebug() << "Select item: " << item->index() << std::endl;
00200     }
00201 
00202     YDumbTab::selectItem( item, selected );
00203 
00204     wRedraw();
00205 }
00206 
00207 void NCDumbTab::shortcutChanged()
00208 {
00209     // Any of the items might have its keyboard shortcut changed, but we don't
00210     // know which one. So let's simply set all tab labels again.
00211 
00212     wRedraw();
00213 }
00214 
00215 void NCDumbTab::wRedraw()
00216  {
00217     if ( !win )
00218         return;
00219 
00220     const NCstyle::StWidget & style( widgetStyle(true) );
00221     win->bkgd( style.plain );
00222     win->box();
00223 
00224     YItemIterator listIt = itemsBegin();
00225 
00226     int winWidth = win->width() - 2;
00227     unsigned int labelPos = 1;
00228     unsigned int i = 0;
00229     bool nonActive = false;
00230     NClabel tablabel;
00231 
00232     while ( listIt != itemsEnd() )
00233     {
00234         tablabel = NCstring( (*listIt)->label() );
00235         tablabel.stripHotkey();
00236         hotlabel = &tablabel;
00237 
00238         nonActive = (i == currentIndex)?false:true;
00239 
00240         if ( GetState() == NC::WSactive )
00241         {
00242 
00243             tablabel.drawAt( *win,
00244                               NCstyle::StWidget( widgetStyle( nonActive) ),
00245                               wpos( 0, labelPos ),
00246                               wsze( 1, winWidth ),
00247                               NC::TOPLEFT, false );
00248         }
00249         else
00250         {
00251             if ( !nonActive )
00252             {
00253                 tablabel.drawAt( *win,
00254                                   widgetStyle( ).data,
00255                                   widgetStyle( ).data,
00256                                   wpos( 0, labelPos ),
00257                                   wsze( 1, winWidth ),
00258                                   NC::TOPLEFT, false );
00259             }
00260             else
00261             {
00262                 tablabel.drawAt( *win,
00263                                   NCstyle::StWidget( frameStyle() ),
00264                                   wpos( 0, labelPos ),
00265                                   wsze( 1, winWidth ),
00266                                   NC::TOPLEFT, false );
00267             }
00268         }
00269 
00270         labelPos += tablabel.width() + 2;
00271 
00272         ++listIt;
00273         ++i;
00274 
00275         if ( listIt != itemsEnd() )
00276         {
00277             winWidth -= tablabel.width() -1;
00278         }
00279     };
00280 
00281     if ( firstChild() )
00282     {
00283         NCWidget * child = dynamic_cast<NCWidget *>( firstChild() );
00284 
00285         if ( child )
00286             child->Redraw();
00287 
00288         redrawChild( firstChild() );
00289     }
00290 }
00291 
00292 bool NCDumbTab::HasHotkey( int key )
00293 {
00294     bool ret = false;
00295 
00296     YItemIterator listIt = itemsBegin();
00297     NClabel tablabel;
00298 
00299     while ( listIt != itemsEnd() )
00300     {
00301         tablabel = NCstring( (*listIt)->label() );
00302         tablabel.stripHotkey();
00303         if ( tablabel.hasHotkey() && tolower ( tablabel.hotkey() ) == tolower ( key ) )
00304         {
00305             hotKey = tolower ( key ) ;
00306             ret = true;
00307         }
00308         ++listIt;
00309     }
00310 
00311     yuiDebug() << "Has hot key: " << key << " " << (ret?"yes":"no") << std::endl;
00312 
00313     return ret;
00314 }
00315 
00316 void NCDumbTab::redrawChild( YWidget *widget )
00317 {
00318     NCWidget * child;
00319 
00320     if ( widget->hasChildren() )
00321     {
00322         YWidgetListConstIterator widgetIt = widget->childrenBegin();
00323         while ( widgetIt != widget->childrenEnd() )
00324         {
00325             child = dynamic_cast<NCWidget *>(*widgetIt);
00326             if ( child )
00327                 child->Redraw();
00328             redrawChild( *widgetIt );
00329             ++widgetIt;
00330         }
00331     }
00332 }
 All Classes Functions Variables