libyui-ncurses  2.54.5
NCRadioButton.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: NCRadioButton.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 "NCRadioButton.h"
29 #include "NCRadioButtonGroup.h"
30 #include "YNCursesUI.h"
31 
32 
33 NCRadioButton::NCRadioButton( YWidget * parent,
34  const std::string & nlabel,
35  bool check )
36  : YRadioButton( parent, nlabel )
37  , NCWidget( parent )
38  , checked( false )
39 {
40  yuiDebug() << std::endl;
41  setLabel( nlabel );
42  hotlabel = &label;
43  setValue( check );
44 }
45 
46 
47 NCRadioButton::~NCRadioButton()
48 {
49  yuiDebug() << std::endl;
50 }
51 
52 
53 int NCRadioButton::preferredWidth()
54 {
55  return wGetDefsze().W;
56 }
57 
58 
59 int NCRadioButton::preferredHeight()
60 {
61  return wGetDefsze().H;
62 }
63 
64 
65 void NCRadioButton::setEnabled( bool do_bv )
66 {
67  NCWidget::setEnabled( do_bv );
68  YRadioButton::setEnabled( do_bv );
69 }
70 
71 
72 void NCRadioButton::setSize( int newwidth, int newheight )
73 {
74  wRelocate( wpos( 0 ), wsze( newheight, newwidth ) );
75 }
76 
77 
78 void NCRadioButton::setLabel( const std::string & nlabel )
79 {
80  label = NCstring( nlabel );
81  label.stripHotkey();
82  defsze = wsze( label.height(), label.width() + 4 );
83  YRadioButton::setLabel( nlabel );
84  Redraw();
85 }
86 
87 
88 void NCRadioButton::setValue( bool newval )
89 {
90  if ( newval != checked )
91  {
92  checked = newval;
93 
94  if ( checked && buttonGroup() )
95  {
96  buttonGroup()->uncheckOtherButtons( this );
97  }
98 
99  Redraw();
100 
101  if (notify())
102  {
103  NCursesEvent event = NCursesEvent::ValueChanged;
104  event.widget = this;
105  YNCursesUI::ui()->sendEvent(event);
106  }
107  }
108 }
109 
110 
111 void NCRadioButton::wRedraw()
112 {
113  if ( !win )
114  return;
115 
116  const NCstyle::StWidget & style( widgetStyle() );
117 
118  win->bkgdset( style.plain );
119 
120  win->printw( 0, 0, "( ) " );
121 
122  label.drawAt( *win, style, wpos( 0, 4 ) );
123 
124  win->bkgdset( style.data );
125 
126  win->printw( 0, 1, "%c", ( checked ? 'x' : ' ' ) );
127 }
128 
129 
130 NCursesEvent NCRadioButton::wHandleInput( wint_t key )
131 {
132  NCursesEvent ret;
133  bool oldChecked = checked;
134  NCRadioButtonGroup * group;
135 
136  switch ( key )
137  {
138  case KEY_HOTKEY:
139  case KEY_SPACE:
140  case KEY_RETURN:
141  setValue( true );
142 
143  if ( notify() && oldChecked != checked )
144  ret = NCursesEvent::ValueChanged;
145 
146  break;
147 
148  case KEY_UP:
149  group = dynamic_cast<NCRadioButtonGroup *>( buttonGroup() );
150 
151  if ( group )
152  group->focusPrevButton();
153 
154  break;
155 
156  case KEY_DOWN:
157  group = dynamic_cast<NCRadioButtonGroup *>( buttonGroup() );
158 
159  if ( group )
160  group->focusNextButton();
161 
162  break;
163  }
164 
165  return ret;
166 }
167 
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
static YNCursesUI * ui()
Access the global Y2NCursesUI.
Definition: YNCursesUI.h:93
void sendEvent(NCursesEvent event)
Send an event to the UI.
Definition: YNCursesUI.cc:455
Definition: position.h:109
virtual void setEnabled(bool do_bv)=0
Pure virtual to make sure every widget implements it.
Definition: NCWidget.cc:391
Definition: position.h:154
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.