libyui  3.4.2
YPushButton.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: YPushButton.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #define YUILogComponent "ui"
27 #include "YUILog.h"
28 
29 #include "YUI.h"
30 #include "YApplication.h"
31 #include "YDialog.h"
32 #include "YUISymbols.h"
33 #include "YPushButton.h"
34 
35 using std::endl;
36 
37 
39 {
40  YPushButtonPrivate( const std::string & label )
41  : label( label )
42  , isDefaultButton( false )
43  , setDefaultButtonRecursive( false )
44  , isHelpButton( false )
45  , isRelNotesButton( false )
46  , role( YCustomButton )
47  {}
48 
49  std::string label;
50  bool isDefaultButton;
51  bool setDefaultButtonRecursive;
52  bool isHelpButton;
53  bool isRelNotesButton;
54  YButtonRole role;
55 };
56 
57 
58 YPushButton::YPushButton( YWidget * parent, const std::string & label )
59  : YWidget( parent )
60  , priv( new YPushButtonPrivate( label ) )
61 {
62  int fkey = YUI::app()->defaultFunctionKey( label );
63 
64  if ( fkey > 0 && ! hasFunctionKey() )
65  setFunctionKey( fkey );
66 }
67 
68 
70 {
71  YDialog * dialog = findDialog();
72 
73  if ( dialog && dialog->defaultButton() == this )
74  {
75  dialog->setDefaultButton( 0 );
76  }
77 }
78 
79 
80 void YPushButton::setLabel( const std::string & label )
81 {
82  priv->label = label;
83 }
84 
85 
86 std::string YPushButton::label() const
87 {
88  return priv->label;
89 }
90 
91 
93 {
94  return priv->isDefaultButton;
95 }
96 
97 
99 {
100  priv->isDefaultButton = isDefaultButton;
101 
102  if ( ! priv->setDefaultButtonRecursive )
103  {
104  // Prevent endless recursion if dialog->setDefaultButton()
105  // calls this function again
106 
107  priv->setDefaultButtonRecursive = true;
108 
109  YDialog * dialog = findDialog();
110 
111  if ( dialog )
112  {
113  if ( isDefaultButton )
114  dialog->setDefaultButton( this );
115  else
116  {
117  if ( dialog->defaultButton() == this )
118  dialog->setDefaultButton( 0 );
119  }
120  }
121 
122  priv->setDefaultButtonRecursive = false;
123  }
124 }
125 
126 
128 {
129  return priv->isHelpButton;
130 }
131 
132 
133 void YPushButton::setHelpButton( bool helpButton )
134 {
135  priv->isHelpButton = helpButton;
136  priv->role = YHelpButton;
137 }
138 
140 {
141  return priv->isRelNotesButton;
142 }
143 
144 
145 void YPushButton::setRelNotesButton( bool relNotesButton )
146 {
147  priv->isRelNotesButton = relNotesButton;
148  priv->role = YRelNotesButton;
149 }
150 
151 /* setRole can try to guess function key, but only if there isn't a selected
152  function key already
153 */
154 void YPushButton::setRole( YButtonRole role )
155 {
156  priv->role = role;
157  int old_function_key = functionKey();
158  if (!hasFunctionKey()) // otherwise function key was already determined
159  {
160  switch (priv->role)
161  {
162  case YOKButton: YWidget::setFunctionKey( 10 ); break;
163  case YCancelButton: YWidget::setFunctionKey( 9 ); break;
164  case YApplyButton: YWidget::setFunctionKey( 10 ); break;
165  case YHelpButton: YWidget::setFunctionKey( 1 ); break;
166  default: break;
167  }
168  if ( functionKey() != old_function_key )
169  {
170  yuiMilestone() << "Guessing function key F" << functionKey()
171  << " for " << this
172  << " from button role " << priv->role
173  << endl;
174  }
175  }
176 }
177 
178 YButtonRole YPushButton::role() const
179 {
180  return priv->role;
181 }
182 
183 /* setFunctionKey (besides setting the function key) should try to guess button
184  role, but only if button role is not yet determined.
185 */
186 void YPushButton::setFunctionKey( int fkey_no )
187 {
188  YWidget::setFunctionKey( fkey_no );
189  YButtonRole oldRole = priv->role;
190 
191  if (priv->role == YCustomButton) // otherwise role was already determined
192  {
193  switch ( functionKey() ) // base class method might have changed it
194  {
195  case 10: priv->role = YOKButton; break;
196  case 9: priv->role = YCancelButton; break;
197  case 1: priv->role = YHelpButton; break;
198  default: break;
199  }
200  if ( priv->role != oldRole )
201  {
202  yuiMilestone() << "Guessing button role " << priv->role
203  << " for " << this
204  << " from function key F" << functionKey()
205  << endl;
206  }
207  }
208 }
209 
210 
211 const YPropertySet &
213 {
214  static YPropertySet propSet;
215 
216  if ( propSet.isEmpty() )
217  {
218  /*
219  * @property std::string Label text on the button
220  */
221  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
222  propSet.add( YWidget::propertySet() );
223  }
224 
225  return propSet;
226 }
227 
228 
229 bool
230 YPushButton::setProperty( const std::string & propertyName, const YPropertyValue & val )
231 {
232  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
233 
234  if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() );
235  else
236  {
237  YWidget::setProperty( propertyName, val );
238  }
239 
240  return true; // success -- no special handling necessary
241 }
242 
243 
245 YPushButton::getProperty( const std::string & propertyName )
246 {
247  propertySet().check( propertyName ); // throws exceptions if not found
248 
249  if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
250  else
251  {
252  return YWidget::getProperty( propertyName );
253  }
254 }
255 
256 
257 std::ostream & operator<<( std::ostream & stream, YButtonRole role )
258 {
259  switch ( role )
260  {
261  case YCustomButton: stream << "YCustomButton"; break;
262  case YOKButton: stream << "YOKButton"; break;
263  case YApplyButton: stream << "YApplyButton"; break;
264  case YCancelButton: stream << "YCancelButton"; break;
265  case YHelpButton: stream << "YHelpButton"; break;
266  case YRelNotesButton: stream << "YRelNotesButton"; break;
267 
268  default:
269  stream << "<Undefined button role #" << (int) role << ">";
270  break;
271  }
272 
273  return stream;
274 }
bool isEmpty() const
Returns &#39;true&#39; if this property set does not contain anything.
Definition: YProperty.h:263
virtual void setDefaultButton(YPushButton *defaultButton)
Set this dialog&#39;s default button (the button that is activated when the user hits [Return] anywhere i...
Definition: YDialog.cc:344
bool isHelpButton() const
Returns &#39;true&#39; if this is a "Help" button.
Definition: YPushButton.cc:127
int defaultFunctionKey(const std::string &label) const
Return the default function key number for a widget with the specified label or 0 if there is none...
Transport class for the value of simple properties.
Definition: YProperty.h:104
bool isDefaultButton() const
Returns &#39;true&#39; if this is the dialog&#39;s default button, i.e.
Definition: YPushButton.cc:92
std::string label() const
Get the label (the text on the button).
Definition: YPushButton.cc:86
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 setDefaultButton(bool def=true)
Make this button the default button.
Definition: YPushButton.cc:98
A set of properties to check names and types against.
Definition: YProperty.h:197
virtual ~YPushButton()
Destructor.
Definition: YPushButton.cc:69
virtual void setFunctionKey(int fkey_no)
Assign a function key to this widget (1 for F1, 2 for F2, etc.
Definition: YWidget.cc:334
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YPushButton.cc:230
YDialog * findDialog()
Traverse up the widget hierarchy and find the dialog this widget belongs to.
Definition: YWidget.cc:374
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YWidget.cc:393
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YPushButton.cc:212
virtual void setRole(YButtonRole role)
Set a predefined role for this button.
Definition: YPushButton.cc:154
int functionKey() const
Return a function key number that is assigned to this widget.
Definition: YWidget.cc:322
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
YButtonRole role() const
Return the role of this button.
Definition: YPushButton.cc:178
Class for widget properties.
Definition: YProperty.h:51
static YApplication * app()
Return the global YApplication object.
Definition: YUI.cc:157
virtual void setFunctionKey(int fkey_no)
Assign a function key to this widget (1 for F1, 2 for F2, etc.
Definition: YPushButton.cc:186
YPushButton * defaultButton() const
Return this dialog&#39;s default button: The button that is activated when the user hits [Return] anywher...
Definition: YDialog.cc:337
virtual void setRelNotesButton(bool relNotesButton=true)
Make this button a release notes button.
Definition: YPushButton.cc:145
bool isRelNotesButton() const
Returns &#39;true&#39; if this is a "Release Notes" button.
Definition: YPushButton.cc:139
bool hasFunctionKey() const
Check if a function key is assigned to this widget.
Definition: YWidget.cc:328
A window in the desktop environment.
Definition: YDialog.h:47
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YPushButton.cc:245
void check(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:87
YPushButton(YWidget *parent, const std::string &label)
Constructor.
Definition: YPushButton.cc:58
virtual void setLabel(const std::string &label)
Set the label (the text on the button).
Definition: YPushButton.cc:80
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 setHelpButton(bool helpButton=true)
Make this button a help button.
Definition: YPushButton.cc:133