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