libyui-ncurses  2.44.1
 All Classes Functions Variables
NCCheckBoxFrame.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: NCCheckBoxFrame.cc
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCurses.h"
28 #include "NCCheckBoxFrame.h"
29 
30 
31 NCCheckBoxFrame::NCCheckBoxFrame( YWidget * parent, const std::string & nlabel,
32  bool checked )
33  : YCheckBoxFrame( parent, nlabel, checked )
34  , NCWidget( parent )
35 {
36  yuiDebug() << std::endl;
37  wstate = NC::WSnormal;
38  framedim.Pos = wpos( 1 );
39  framedim.Sze = wsze( 2 );
40 
41  setLabel( YCheckBoxFrame::label() );
42  hotlabel = &label;
43 
44  setValue( checked );
45 
46  // setEnabled( getValue() ); is called in wRedraw()
47 }
48 
49 
50 NCCheckBoxFrame::~NCCheckBoxFrame()
51 {
52  yuiDebug() << std::endl;
53 }
54 
55 
56 int NCCheckBoxFrame::preferredWidth()
57 {
58  defsze.W = hasChildren() ? firstChild()->preferredWidth() : 0;
59 
60  if ( label.width() > ( unsigned )defsze.W )
61  defsze.W = label.width();
62 
63  defsze.W += framedim.Sze.W + 4; // add space for checkbox
64 
65  return defsze.W;
66 }
67 
68 
69 int NCCheckBoxFrame::preferredHeight()
70 {
71  defsze.H = hasChildren() ? firstChild()->preferredHeight() : 0;
72  defsze.H += framedim.Sze.H;
73 
74  return defsze.H;
75 }
76 
77 
78 void NCCheckBoxFrame::setSize( int newwidth, int newheight )
79 {
80  wsze csze( newheight, newwidth );
81  wRelocate( wpos( 0 ), csze );
82  csze = wsze::max( 0, csze - framedim.Sze );
83 
84  if ( hasChildren() )
85  firstChild()->setSize( csze.W, csze.H );
86 }
87 
88 
89 void NCCheckBoxFrame::setLabel( const std::string & nlabel )
90 {
91  YCheckBoxFrame::setLabel( nlabel );
92 
93  label = NCstring( YCheckBoxFrame::label() );
94  label.stripHotkey();
95 
96  Redraw();
97 }
98 
99 bool NCCheckBoxFrame::getParentValue( NCWidget * widget, bool initial )
100 {
101  bool enabled = initial;
102 
103  for ( tnode<NCWidget*> * c = widget->Parent();
104  c && widget->IsDescendantOf( c );
105  c = c->Parent() )
106  {
107  NCCheckBoxFrame * frame = dynamic_cast<NCCheckBoxFrame *>( c->Value() );
108  if ( frame )
109  {
110  enabled = frame->getValue();
111 
112  // invert value if required
113  if ( frame->invertAutoEnable() )
114  enabled = !enabled;
115 
116  // despite of frame->getValue(), don't enable child widgets if state
117  // of frame is NC::WSdisabeled
118  if ( frame->GetState() == NC::WSdisabeled )
119  enabled = false;
120 
121  break;
122  }
123  }
124  return enabled;
125 }
126 
127 void NCCheckBoxFrame::setEnabled( bool do_bv )
128 {
129  YWidget::setEnabled( do_bv );
130  bool do_it = do_bv;
131 
132  for ( tnode<NCWidget*> * c = this->Next();
133  c && c->IsDescendantOf( this );
134  c = c->Next() )
135  {
136  if ( c->Value()->GetState() != NC::WSdumb )
137  {
138  do_it = getParentValue( c->Value(), do_it );
139 
140  c->Value()->setEnabled( do_it );
141  // explicitely set the state (needed for first run - bug #268352)
142  c->Value()->SetState( do_it ? NC::WSnormal : NC::WSdisabeled, true );
143  }
144  }
145 }
146 
147 
148 bool NCCheckBoxFrame::gotBuddy()
149 {
150  if ( !label.hasHotkey() )
151  return false;
152 
153  for ( tnode<NCWidget*> * c = this->Next();
154  c && c->IsDescendantOf( this );
155  c = c->Next() )
156  {
157  if ( c->Value()->GetState() != NC::WSdumb )
158  return true;
159  }
160 
161  return false;
162 }
163 
164 
165 void NCCheckBoxFrame::wRedraw()
166 {
167  if ( !win )
168  return;
169 
170  chtype bg = wStyle().dumb.text;
171  win->bkgd( bg );
172  win->box();
173 
174  if ( gotBuddy() )
175  label.drawAt( *win, widgetStyle(), wpos( 0, 5 ),
176  wsze( 1, win->width() - 6 ), NC::TOPLEFT, false );
177  else
178  label.drawAt( *win, bg, bg, wpos( 0, 5 ),
179  wsze( 1, win->width() - 6 ), NC::TOPLEFT, false );
180 
181  const NCstyle::StWidget & style( widgetStyle() );
182 
183  win->bkgdset( style.plain );
184  win->printw( 0, 1, "[ ] " );
185 
186  if ( getValue() )
187  win->printw( 0, 2, "%c", 'x' );
188  else
189  win->printw( 0, 2, "%c", ' ' );
190 
191  if ( autoEnable() )
192  setEnabled( getValue() );
193 }
194 
195 
196 NCursesEvent NCCheckBoxFrame::wHandleInput( wint_t key )
197 {
198  NCursesEvent ret = NCursesEvent::handled;
199 
200  if ( key == KEY_SPACE ||
201  key == KEY_RETURN ||
202  key == KEY_HOTKEY )
203  {
204  if ( getValue() == true ) // enabled
205  {
206  setValue( false );
207  }
208  else
209  {
210  setValue( true );
211  }
212 
213  // No need to call Redraw() here, it is already done in setValue() and
214  // no need to call setEnabled(), it is called in Redraw(), resp. wRedraw().
215 
216  if ( notify() )
217  ret = NCursesEvent::ValueChanged;
218  }
219 
220  return ret;
221 }
222 
223 
224 bool NCCheckBoxFrame::setKeyboardFocus()
225 {
226  if ( !grabFocus() )
227  return YWidget::setKeyboardFocus();
228 
229  return true;
230 }
231 
232 
233 bool NCCheckBoxFrame::value()
234 {
235  return getValue();
236 }
int printw(const char *fmt,...)
Definition: ncursesw.cc:75
int bkgd(const chtype ch)
Definition: ncursesw.h:1443
void bkgdset(chtype ch)
Definition: ncursesw.h:1448
virtual void setEnabled(bool do_bv)
Definition: position.h:109
Definition: position.h:154
int width() const
Definition: ncursesw.h:1075