libyui  3.10.0
YRadioButton.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: YRadioButton.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <string>
27 
28 #define YUILogComponent "ui"
29 #include "YUILog.h"
30 
31 #include "YUISymbols.h"
32 #include "YUIException.h"
33 #include "YMacroRecorder.h"
34 #include "YRadioButtonGroup.h"
35 #include "YRadioButton.h"
36 
37 using std::string;
38 
39 
41 {
42  /**
43  * Constructor
44  **/
45  YRadioButtonPrivate( const string & label )
46  : label( label )
47  , radioButtonGroup( 0 )
48  , useBoldFont( false )
49  {}
50 
51  //
52  // Data members
53  //
54 
55  string label;
56  YRadioButtonGroup * radioButtonGroup;
57  bool useBoldFont;
58 };
59 
60 
62  const string & label )
63  : YWidget( parent )
64  , priv( new YRadioButtonPrivate( label ) )
65 {
66  YUI_CHECK_NEW( priv );
67 
68  // Intentionally not calling
69  // buttonGroup()->addRadioButton( this );
70  // here because virtual functions can't be used yet (while the constructor
71  // isn't finished yet), and the RadioButtonGroup for sure would try to call
72  // YRadioButton::value() which is (pure) virtual, thus not available yet.
73  //
74  // The caller has to take care of this.
75 }
76 
77 
79 {
80  if ( priv->radioButtonGroup )
81  {
82  if ( ! priv->radioButtonGroup->beingDestroyed() )
83  priv->radioButtonGroup->removeRadioButton( this );
84  }
85 }
86 
87 
88 void YRadioButton::setLabel( const string & newLabel )
89 {
90  priv->label = newLabel;
91 }
92 
93 
94 string YRadioButton::label() const
95 {
96  return priv->label;
97 }
98 
99 
101 {
102  return priv->useBoldFont;
103 }
104 
105 
107 {
108  priv->useBoldFont = bold;
109 }
110 
111 
112 const YPropertySet &
114 {
115  static YPropertySet propSet;
116 
117  if ( propSet.isEmpty() )
118  {
119  /*
120  * @property boolean Value the on/off state of the RadioButton
121  * @property string Label the text on the RadioButton
122  */
123 
124  propSet.add( YProperty( YUIProperty_Value, YBoolProperty ) );
125  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
126  propSet.add( YWidget::propertySet() );
127  }
128 
129  return propSet;
130 }
131 
132 
133 bool
134 YRadioButton::setProperty( const string & propertyName, const YPropertyValue & val )
135 {
136  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
137 
138  if ( propertyName == YUIProperty_Value ) setValue( val.boolVal() );
139  else if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() );
140  else
141  {
142  return YWidget::setProperty( propertyName, val );
143  }
144 
145  return true; // success -- no special processing necessary
146 }
147 
148 
150 YRadioButton::getProperty( const string & propertyName )
151 {
152  propertySet().check( propertyName ); // throws exceptions if not found
153 
154  if ( propertyName == YUIProperty_Value ) return YPropertyValue( value() );
155  else if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
156  else
157  {
158  return YWidget::getProperty( propertyName );
159  }
160 }
161 
162 
165 {
166  if ( ! priv->radioButtonGroup )
167  {
168  priv->radioButtonGroup = findRadioButtonGroup();
169  }
170 
171  return priv->radioButtonGroup;
172 }
173 
174 
177 {
178  YWidget * widget = parent();
179 
180  while ( widget )
181  {
182  YRadioButtonGroup * radioButtonGroup = dynamic_cast<YRadioButtonGroup *> (widget);
183 
184  if ( radioButtonGroup )
185  return radioButtonGroup;
186  else
187  widget = widget->parent();
188  }
189 
190  return 0;
191 }
192 
193 
194 void
196 {
197  if ( value() )
198  {
199  // Only record if this radio button is on. By definition one radio
200  // button of the radio box _must_ be on if the user did anything, so we
201  // don't record a lot of redundant "ChangeWidget( ..., `Value, false )"
202  // calls.
203 
204  macroRecorder->recordWidgetProperty( this, YUIProperty_Value );
205  }
206 }
YWidget::parent
YWidget * parent() const
Return this widget's parent or 0 if it doesn't have a parent.
Definition: YWidget.cc:271
YPropertySet::add
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:146
YWidget
Abstract base class of all UI widgets.
Definition: YWidget.h:54
YRadioButton::buttonGroup
YRadioButtonGroup * buttonGroup()
Get a pointer to the radio button group this button belongs to.
Definition: YRadioButton.cc:164
YRadioButtonGroup::removeRadioButton
virtual void removeRadioButton(YRadioButton *radioButton)
Remove a RadioButton from this button group.
Definition: YRadioButtonGroup.cc:90
YRadioButton::findRadioButtonGroup
YRadioButtonGroup * findRadioButtonGroup() const
Traverse the widget hierarchy upwards to find the corresponding YRadioButtonGroup,...
Definition: YRadioButton.cc:176
YRadioButton::YRadioButton
YRadioButton(YWidget *parent, const std::string &label)
Constructor.
Definition: YRadioButton.cc:61
YRadioButton::setUseBoldFont
virtual void setUseBoldFont(bool bold=true)
Indicate whether or not a bold font should be used.
Definition: YRadioButton.cc:106
YPropertySet
A set of properties to check names and types against.
Definition: YProperty.h:197
YPropertySet::isEmpty
bool isEmpty() const
Returns 'true' if this property set does not contain anything.
Definition: YProperty.h:263
YPropertyValue::stringVal
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
YRadioButton::value
virtual bool value()=0
Get the current on/off value: 'true' if checked, 'false' if unchecked.
YRadioButtonPrivate
Definition: YRadioButton.cc:40
YRadioButton::label
std::string label() const
Get the label (the text on the RadioButton).
Definition: YRadioButton.cc:94
YRadioButton::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YRadioButton.cc:134
YWidget::beingDestroyed
bool beingDestroyed() const
Check if this widget is in the process of being destroyed.
Definition: YWidget.cc:258
YRadioButtonPrivate::YRadioButtonPrivate
YRadioButtonPrivate(const string &label)
Constructor.
Definition: YRadioButton.cc:45
YMacroRecorder::recordWidgetProperty
virtual void recordWidgetProperty(YWidget *widget, const char *propertyName)=0
Record one widget property.
YPropertyValue::type
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169
YRadioButton::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YRadioButton.cc:150
YWidget::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:457
YProperty
Class for widget properties.
Definition: YProperty.h:51
YRadioButton::setLabel
virtual void setLabel(const std::string &label)
Set the label (the text on the RadioButton).
Definition: YRadioButton.cc:88
YRadioButtonGroup
A group of YRadioButton widgets.
Definition: YRadioButtonGroup.h:41
YRadioButton::~YRadioButton
virtual ~YRadioButton()
Destructor: Removes the button from the radio button group.
Definition: YRadioButton.cc:78
YRadioButton::setValue
virtual void setValue(bool checked)=0
Set the radio button value (on/off).
YPropertySet::check
void check(const std::string &propertyName) const
Check if a property 'propertyName' exists in this property set.
Definition: YProperty.cc:88
YRadioButton::saveUserInput
virtual void saveUserInput(YMacroRecorder *macroRecorder)
Save the widget's user input to a macro recorder.
Definition: YRadioButton.cc:195
YPropertyValue
Transport class for the value of simple properties.
Definition: YProperty.h:104
YMacroRecorder
Abstract base class for macro recorders.
Definition: YMacroRecorder.h:38
YRadioButton::useBoldFont
bool useBoldFont() const
Returns 'true' if a bold font should be used.
Definition: YRadioButton.cc:100
YWidget::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:432
YWidget::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YWidget.cc:395
YRadioButton::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YRadioButton.cc:113