libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/NCCheckBoxFrame.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:       NCCheckBoxFrame.cc
00020 
00021    Author:     Michael Andres <ma@suse.de>
00022 
00023 /-*/
00024 
00025 #define  YUILogComponent "ncurses"
00026 #include <yui/YUILog.h>
00027 #include "NCurses.h"
00028 #include "NCCheckBoxFrame.h"
00029 
00030 
00031 NCCheckBoxFrame::NCCheckBoxFrame( YWidget * parent, const std::string & nlabel,
00032                                   bool checked )
00033     : YCheckBoxFrame( parent, nlabel, checked )
00034     , NCWidget( parent )
00035 {
00036     yuiDebug() << std::endl;
00037     wstate = NC::WSnormal;
00038     framedim.Pos = wpos( 1 );
00039     framedim.Sze = wsze( 2 );
00040 
00041     setLabel( YCheckBoxFrame::label() );
00042     hotlabel = &label;
00043 
00044     setValue( checked );
00045 
00046     // setEnabled( getValue() ); is called in wRedraw()
00047 }
00048 
00049 
00050 NCCheckBoxFrame::~NCCheckBoxFrame()
00051 {
00052     yuiDebug() << std::endl;
00053 }
00054 
00055 
00056 int NCCheckBoxFrame::preferredWidth()
00057 {
00058     defsze.W = hasChildren() ? firstChild()->preferredWidth() : 0;
00059 
00060     if ( label.width() > ( unsigned )defsze.W )
00061         defsze.W = label.width();
00062 
00063     defsze.W += framedim.Sze.W + 4;     // add space for checkbox
00064 
00065     return defsze.W;
00066 }
00067 
00068 
00069 int NCCheckBoxFrame::preferredHeight()
00070 {
00071     defsze.H  = hasChildren() ? firstChild()->preferredHeight() : 0;
00072     defsze.H += framedim.Sze.H;
00073 
00074     return defsze.H;
00075 }
00076 
00077 
00078 void NCCheckBoxFrame::setSize( int newwidth, int newheight )
00079 {
00080     wsze csze( newheight, newwidth );
00081     wRelocate( wpos( 0 ), csze );
00082     csze = wsze::max( 0, csze - framedim.Sze );
00083 
00084     if ( hasChildren() )
00085         firstChild()->setSize( csze.W, csze.H );
00086 }
00087 
00088 
00089 void NCCheckBoxFrame::setLabel( const std::string & nlabel )
00090 {
00091     YCheckBoxFrame::setLabel( nlabel );
00092 
00093     label = NCstring( YCheckBoxFrame::label() );
00094     label.stripHotkey();
00095 
00096     Redraw();
00097 }
00098 
00099 bool NCCheckBoxFrame::getParentValue( NCWidget * widget, bool initial )
00100 {
00101     bool enabled = initial;
00102 
00103     for ( tnode<NCWidget*> * c = widget->Parent();
00104           c && widget->IsDescendantOf( c );
00105           c = c->Parent() )
00106     {
00107         NCCheckBoxFrame * frame = dynamic_cast<NCCheckBoxFrame *>( c->Value() );
00108         if ( frame )
00109         {
00110             enabled = frame->getValue();
00111 
00112             // invert value if required
00113             if ( frame->invertAutoEnable() )
00114                 enabled = !enabled;
00115 
00116             // despite of frame->getValue(), don't enable child widgets if state
00117             // of frame is  NC::WSdisabeled
00118             if ( frame->GetState() == NC::WSdisabeled )
00119                 enabled = false;
00120 
00121             break;
00122         }
00123     }
00124     return enabled;
00125 }
00126 
00127 void NCCheckBoxFrame::setEnabled( bool do_bv )
00128 {
00129     YWidget::setEnabled( do_bv );
00130     bool do_it = do_bv;
00131 
00132     for ( tnode<NCWidget*> * c = this->Next();
00133           c && c->IsDescendantOf( this );
00134           c = c->Next() )
00135     {
00136         if ( c->Value()->GetState() != NC::WSdumb )
00137         {
00138             do_it = getParentValue( c->Value(), do_it );
00139 
00140             c->Value()->setEnabled( do_it );
00141             // explicitely set the state (needed for first run - bug #268352)
00142             c->Value()->SetState( do_it ? NC::WSnormal : NC::WSdisabeled, true );
00143         }
00144     }
00145 }
00146 
00147 
00148 bool NCCheckBoxFrame::gotBuddy()
00149 {
00150     if ( !label.hasHotkey() )
00151         return false;
00152 
00153     for ( tnode<NCWidget*> * c = this->Next();
00154           c && c->IsDescendantOf( this );
00155           c = c->Next() )
00156     {
00157         if ( c->Value()->GetState() != NC::WSdumb )
00158             return true;
00159     }
00160 
00161     return false;
00162 }
00163 
00164 
00165 void NCCheckBoxFrame::wRedraw()
00166 {
00167     if ( !win )
00168         return;
00169 
00170     chtype bg = wStyle().dumb.text;
00171     win->bkgd( bg );
00172     win->box();
00173 
00174     if ( gotBuddy() )
00175         label.drawAt( *win, widgetStyle(), wpos( 0, 5 ),
00176                       wsze( 1, win->width() - 6 ), NC::TOPLEFT, false );
00177     else
00178         label.drawAt( *win, bg, bg, wpos( 0, 5 ),
00179                       wsze( 1, win->width() - 6 ), NC::TOPLEFT, false );
00180 
00181     const NCstyle::StWidget & style( widgetStyle() );
00182 
00183     win->bkgdset( style.plain );
00184     win->printw( 0, 1, "[ ] " );
00185 
00186     if ( getValue() )
00187         win->printw( 0, 2, "%c", 'x' );
00188     else
00189         win->printw( 0, 2, "%c", ' ' );
00190 
00191     if ( autoEnable() )
00192         setEnabled( getValue() );
00193 }
00194 
00195 
00196 NCursesEvent NCCheckBoxFrame::wHandleInput( wint_t key )
00197 {
00198     NCursesEvent ret = NCursesEvent::handled;
00199 
00200     if ( key == KEY_SPACE ||
00201          key == KEY_RETURN ||
00202          key == KEY_HOTKEY )
00203     {
00204         if ( getValue() == true )               // enabled
00205         {
00206             setValue( false );
00207         }
00208         else
00209         {
00210             setValue( true );
00211         }
00212 
00213         // No need to call Redraw() here, it is already done in setValue() and
00214         // no need to call setEnabled(), it is called in Redraw(), resp. wRedraw().
00215 
00216         if ( notify() )
00217             ret = NCursesEvent::ValueChanged;
00218     }
00219 
00220     return ret;
00221 }
00222 
00223 
00224 bool NCCheckBoxFrame::setKeyboardFocus()
00225 {
00226     if ( !grabFocus() )
00227         return YWidget::setKeyboardFocus();
00228 
00229     return true;
00230 }
00231 
00232 
00233 bool NCCheckBoxFrame::value()
00234 {
00235     return getValue();
00236 }
 All Classes Functions Variables