libyui-ncurses  2.55.0
NCCheckBox.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: NCCheckBox.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 "NCCheckBox.h"
29 #include "YNCursesUI.h"
30 
31 
32 unsigned char NCCheckBox::statetag[3] = { '?', ' ', 'x' };
33 
34 
35 NCCheckBox::NCCheckBox( YWidget * parent,
36  const std::string & nlabel,
37  bool checked )
38  : YCheckBox( parent, nlabel )
39  , NCWidget( parent )
40  , tristate( false )
41  , checkstate( checked ? S_ON : S_OFF )
42 {
43  yuiDebug() << std::endl;
44  setLabel( nlabel );
45  hotlabel = &label;
46 }
47 
48 
49 NCCheckBox::~NCCheckBox()
50 {
51  yuiDebug() << std::endl;
52 }
53 
54 
55 int NCCheckBox::preferredWidth()
56 {
57  return wGetDefsze().W;
58 }
59 
60 
61 int NCCheckBox::preferredHeight()
62 {
63  return wGetDefsze().H;
64 }
65 
66 
67 void NCCheckBox::setEnabled( bool do_bv )
68 {
69  NCWidget::setEnabled( do_bv );
70  YCheckBox::setEnabled( do_bv );
71 }
72 
73 
74 void NCCheckBox::setSize( int newwidth, int newheight )
75 {
76  wRelocate( wpos( 0 ), wsze( newheight, newwidth ) );
77 }
78 
79 
80 void NCCheckBox::setLabel( const std::string & nlabel )
81 {
82  label = NCstring( nlabel );
83  label.stripHotkey();
84  defsze = wsze( label.height(), label.width() + 4 );
85  YCheckBox::setLabel( nlabel );
86  Redraw();
87 }
88 
89 
90 void NCCheckBox::setValue( YCheckBoxState state )
91 {
92  YCheckBoxState old = value();
93 
94  switch ( state )
95  {
96  case YCheckBox_on:
97  checkstate = S_ON;
98  tristate = false;
99  break;
100 
101  case YCheckBox_off:
102  checkstate = S_OFF;
103  tristate = false;
104  break;
105 
106  case YCheckBox_dont_care:
107  tristate = true;
108  checkstate = S_DC;
109  break;
110  }
111 
112  Redraw();
113 
114  // trigger the notify event if enabled
115  if (old != state && notify())
116  {
117  NCursesEvent event = NCursesEvent::ValueChanged;
118  event.widget = this;
119  YNCursesUI::ui()->sendEvent(event);
120  }
121 }
122 
123 
124 YCheckBoxState NCCheckBox::value()
125 {
126  if ( checkstate == S_DC )
127  return YCheckBox_dont_care;
128 
129  if ( checkstate == S_ON )
130  return YCheckBox_on;
131  else
132  return YCheckBox_off;
133 }
134 
135 
136 void NCCheckBox::wRedraw()
137 {
138  if ( !win )
139  return;
140 
141  const NCstyle::StWidget & style( widgetStyle() );
142 
143  win->bkgdset( style.plain );
144 
145  win->printw( 0, 0, "[ ] " );
146 
147  label.drawAt( *win, style, wpos( 0, 4 ) );
148 
149  win->bkgdset( style.data );
150 
151  win->printw( 0, 1, "%c", statetag[checkstate] );
152 }
153 
154 
155 NCursesEvent NCCheckBox::wHandleInput( wint_t key )
156 {
157  NCursesEvent ret;
158 
159  switch ( key )
160  {
161  case KEY_HOTKEY:
162  case KEY_SPACE:
163  case KEY_RETURN:
164 
165  switch ( checkstate )
166  {
167  case S_DC:
168  checkstate = S_ON;
169  break;
170 
171  case S_ON:
172  checkstate = S_OFF;
173  break;
174 
175  case S_OFF:
176  checkstate = tristate ? S_DC : S_ON;
177  break;
178  }
179 
180  Redraw();
181 
182  if ( notify() )
183  ret = NCursesEvent::ValueChanged;
184 
185  break;
186  }
187 
188  return ret;
189 }
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.
Definition: NCCheckBox.cc:67
virtual void setEnabled(bool do_bv)=0
Pure virtual to make sure every widget implements it.
Definition: NCWidget.cc:391
int printw(const char *fmt,...)
Do a formatted print to the window.
Definition: ncursesw.cc:75
void bkgdset(chtype ch)
Set the background property.
Definition: ncursesw.h:1447
void sendEvent(NCursesEvent event)
Send an event to the UI.
Definition: YNCursesUI.cc:455
static YNCursesUI * ui()
Access the global Y2NCursesUI.
Definition: YNCursesUI.h:93
Definition: position.h:110
Definition: position.h:155