libyui-ncurses  2.43.2
 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  if ( invertAutoEnable() )
45  setValue( !checked );
46  else
47  setValue( checked );
48 
49  // setEnabled() is called in wRedraw()
50 }
51 
52 
53 NCCheckBoxFrame::~NCCheckBoxFrame()
54 {
55  yuiDebug() << std::endl;
56 }
57 
58 
59 int NCCheckBoxFrame::preferredWidth()
60 {
61  defsze.W = hasChildren() ? firstChild()->preferredWidth() : 0;
62 
63  if ( label.width() > ( unsigned )defsze.W )
64  defsze.W = label.width();
65 
66  defsze.W += framedim.Sze.W + 4; // add space for checkbox
67 
68  return defsze.W;
69 }
70 
71 
72 int NCCheckBoxFrame::preferredHeight()
73 {
74  defsze.H = hasChildren() ? firstChild()->preferredHeight() : 0;
75  defsze.H += framedim.Sze.H;
76 
77  return defsze.H;
78 }
79 
80 
81 void NCCheckBoxFrame::setSize( int newwidth, int newheight )
82 {
83  wsze csze( newheight, newwidth );
84  wRelocate( wpos( 0 ), csze );
85  csze = wsze::max( 0, csze - framedim.Sze );
86 
87  if ( hasChildren() )
88  firstChild()->setSize( csze.W, csze.H );
89 }
90 
91 
92 void NCCheckBoxFrame::setLabel( const std::string & nlabel )
93 {
94  YCheckBoxFrame::setLabel( nlabel );
95 
96  label = NCstring( YCheckBoxFrame::label() );
97  label.stripHotkey();
98 
99  Redraw();
100 }
101 
102 bool NCCheckBoxFrame::getParentValue( NCWidget * widget, bool initial )
103 {
104  bool enabled = initial;
105 
106  for ( tnode<NCWidget*> * c = widget->Parent();
107  c && widget->IsDescendantOf( c );
108  c = c->Parent() )
109  {
110  NCCheckBoxFrame * frame = dynamic_cast<NCCheckBoxFrame *>( c->Value() );
111  if ( frame )
112  {
113  enabled = frame->getValue();
114 
115  if ( frame->GetState() == NC::WSdisabeled )
116  enabled = false;
117 
118  break;
119  }
120  }
121  return enabled;
122 }
123 
124 void NCCheckBoxFrame::setEnabled( bool do_bv )
125 {
126  YWidget::setEnabled( do_bv );
127  bool do_it = do_bv;
128 
129  for ( tnode<NCWidget*> * c = this->Next();
130  c && c->IsDescendantOf( this );
131  c = c->Next() )
132  {
133  if ( c->Value()->GetState() != NC::WSdumb )
134  {
135  do_it = getParentValue( c->Value(), do_it );
136 
137  c->Value()->setEnabled( do_it );
138  // explicitely set the state (needed for first run - bug #268352)
139  c->Value()->SetState( do_it ? NC::WSnormal : NC::WSdisabeled, true );
140  }
141  }
142 }
143 
144 
145 bool NCCheckBoxFrame::gotBuddy()
146 {
147  if ( !label.hasHotkey() )
148  return false;
149 
150  for ( tnode<NCWidget*> * c = this->Next();
151  c && c->IsDescendantOf( this );
152  c = c->Next() )
153  {
154  if ( c->Value()->GetState() != NC::WSdumb )
155  return true;
156  }
157 
158  return false;
159 }
160 
161 
162 void NCCheckBoxFrame::wRedraw()
163 {
164  if ( !win )
165  return;
166 
167  chtype bg = wStyle().dumb.text;
168  win->bkgd( bg );
169  win->box();
170 
171  if ( gotBuddy() )
172  label.drawAt( *win, widgetStyle(), wpos( 0, 5 ),
173  wsze( 1, win->width() - 6 ), NC::TOPLEFT, false );
174  else
175  label.drawAt( *win, bg, bg, wpos( 0, 5 ),
176  wsze( 1, win->width() - 6 ), NC::TOPLEFT, false );
177 
178  const NCstyle::StWidget & style( widgetStyle() );
179 
180  win->bkgdset( style.plain );
181  win->printw( 0, 1, "[ ] " );
182 
183  if ( !invertAutoEnable() )
184  {
185  if ( getValue() )
186  win->printw( 0, 2, "%c", 'x' );
187  else
188  win->printw( 0, 2, "%c", ' ' );
189  }
190  else
191  {
192  if ( getValue() )
193  win->printw( 0, 2, "%c", ' ' );
194  else
195  win->printw( 0, 2, "%c", 'x' );
196  }
197 
198  if ( autoEnable() )
199  setEnabled( getValue() );
200 }
201 
202 
203 NCursesEvent NCCheckBoxFrame::wHandleInput( wint_t key )
204 {
205  NCursesEvent ret = NCursesEvent::handled;
206 
207  if ( key == KEY_SPACE ||
208  key == KEY_RETURN ||
209  key == KEY_HOTKEY )
210  {
211  if ( getValue() == true ) // enabled
212  {
213  setValue( false );
214  }
215  else
216  {
217  setValue( true );
218  }
219 
220  // No need to call Redraw() here, it is already done in setValue() and
221  // no need to call setEnabled(), it is called in Redraw(), resp. wRedraw().
222 
223  if ( notify() )
224  ret = NCursesEvent::ValueChanged;
225  }
226 
227  return ret;
228 }
229 
230 
231 bool NCCheckBoxFrame::setKeyboardFocus()
232 {
233  if ( !grabFocus() )
234  return YWidget::setKeyboardFocus();
235 
236  return true;
237 }
238 
239 
240 bool NCCheckBoxFrame::value()
241 {
242  return getValue();
243 }