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