libyui  3.0.10
 All Classes Functions Variables Enumerations Friends
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  , role( YCustomButton )
46  {}
47 
48  std::string label;
49  bool isDefaultButton;
50  bool setDefaultButtonRecursive;
51  bool isHelpButton;
52  YButtonRole role;
53 };
54 
55 
56 YPushButton::YPushButton( YWidget * parent, const std::string & label )
57  : YWidget( parent )
58  , priv( new YPushButtonPrivate( label ) )
59 {
60  int fkey = YUI::app()->defaultFunctionKey( label );
61 
62  if ( fkey > 0 && ! hasFunctionKey() )
63  setFunctionKey( fkey );
64 }
65 
66 
68 {
69  YDialog * dialog = findDialog();
70 
71  if ( dialog && dialog->defaultButton() == this )
72  {
73  dialog->setDefaultButton( 0 );
74  }
75 }
76 
77 
78 void YPushButton::setLabel( const std::string & label )
79 {
80  priv->label = label;
81 }
82 
83 
84 std::string YPushButton::label() const
85 {
86  return priv->label;
87 }
88 
89 
91 {
92  return priv->isDefaultButton;
93 }
94 
95 
96 void YPushButton::setDefaultButton( bool isDefaultButton )
97 {
98  priv->isDefaultButton = isDefaultButton;
99 
100  if ( ! priv->setDefaultButtonRecursive )
101  {
102  // Prevent endless recursion if dialog->setDefaultButton()
103  // calls this function again
104 
105  priv->setDefaultButtonRecursive = true;
106 
107  YDialog * dialog = findDialog();
108 
109  if ( dialog )
110  {
111  if ( isDefaultButton )
112  dialog->setDefaultButton( this );
113  else
114  {
115  if ( dialog->defaultButton() == this )
116  dialog->setDefaultButton( 0 );
117  }
118  }
119 
120  priv->setDefaultButtonRecursive = false;
121  }
122 }
123 
124 
126 {
127  return priv->isHelpButton;
128 }
129 
130 
131 void YPushButton::setHelpButton( bool helpButton )
132 {
133  priv->isHelpButton = helpButton;
134  priv->role = YHelpButton;
135 }
136 
137 /* setRole can try to guess function key, but only if there isn't a selected
138  function key already
139 */
140 void YPushButton::setRole( YButtonRole role )
141 {
142  priv->role = role;
143  int old_function_key = functionKey();
144  if (!hasFunctionKey()) // otherwise function key was already determined
145  {
146  switch (priv->role)
147  {
148  case YOKButton: YWidget::setFunctionKey( 10 ); break;
149  case YCancelButton: YWidget::setFunctionKey( 9 ); break;
150  case YApplyButton: YWidget::setFunctionKey( 10 ); break;
151  case YHelpButton: YWidget::setFunctionKey( 1 ); break;
152  default: break;
153  }
154  if ( functionKey() != old_function_key )
155  {
156  yuiMilestone() << "Guessing function key F" << functionKey()
157  << " for " << this
158  << " from button role " << priv->role
159  << endl;
160  }
161  }
162 }
163 
164 YButtonRole YPushButton::role() const
165 {
166  return priv->role;
167 }
168 
169 /* setFunctionKey (besides setting the function key) should try to guess button
170  role, but only if button role is not yet determined.
171 */
172 void YPushButton::setFunctionKey( int fkey_no )
173 {
174  YWidget::setFunctionKey( fkey_no );
175  YButtonRole oldRole = priv->role;
176 
177  if (priv->role == YCustomButton) // otherwise role was already determined
178  {
179  switch ( functionKey() ) // base class method might have changed it
180  {
181  case 10: priv->role = YOKButton; break;
182  case 9: priv->role = YCancelButton; break;
183  case 1: priv->role = YHelpButton; break;
184  default: break;
185  }
186  if ( priv->role != oldRole )
187  {
188  yuiMilestone() << "Guessing button role " << priv->role
189  << " for " << this
190  << " from function key F" << functionKey()
191  << endl;
192  }
193  }
194 }
195 
196 
197 const YPropertySet &
199 {
200  static YPropertySet propSet;
201 
202  if ( propSet.isEmpty() )
203  {
204  /*
205  * @property std::string Label text on the button
206  */
207  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
208  propSet.add( YWidget::propertySet() );
209  }
210 
211  return propSet;
212 }
213 
214 
215 bool
216 YPushButton::setProperty( const std::string & propertyName, const YPropertyValue & val )
217 {
218  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
219 
220  if ( propertyName == YUIProperty_Label ) setLabel( val.stringVal() );
221  else
222  {
223  YWidget::setProperty( propertyName, val );
224  }
225 
226  return true; // success -- no special handling necessary
227 }
228 
229 
231 YPushButton::getProperty( const std::string & propertyName )
232 {
233  propertySet().check( propertyName ); // throws exceptions if not found
234 
235  if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
236  else
237  {
238  return YWidget::getProperty( propertyName );
239  }
240 }
241 
242 
243 std::ostream & operator<<( std::ostream & stream, YButtonRole role )
244 {
245  switch ( role )
246  {
247  case YCustomButton: stream << "YCustomButton"; break;
248  case YOKButton: stream << "YOKButton"; break;
249  case YApplyButton: stream << "YApplyButton"; break;
250  case YCancelButton: stream << "YCancelButton"; break;
251  case YHelpButton: stream << "YHelpButton"; break;
252 
253  default:
254  stream << "<Undefined button role #" << (int) role << ">";
255  break;
256  }
257 
258  return stream;
259 }
YPushButton * defaultButton() const
Definition: YDialog.cc:297
virtual void setDefaultButton(YPushButton *defaultButton)
Definition: YDialog.cc:304
YButtonRole role() const
Definition: YPushButton.cc:164
bool isHelpButton() const
Definition: YPushButton.cc:125
void check(const std::string &propertyName) const
Definition: YProperty.cc:62
void add(const YProperty &prop)
Definition: YProperty.cc:120
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Definition: YWidget.cc:428
virtual void setDefaultButton(bool def=true)
Definition: YPushButton.cc:96
int defaultFunctionKey(const std::string &label) const
virtual ~YPushButton()
Definition: YPushButton.cc:67
std::string label() const
Definition: YPushButton.cc:84
virtual void setFunctionKey(int fkey_no)
Definition: YWidget.cc:334
std::string stringVal() const
Definition: YProperty.h:167
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Definition: YPushButton.cc:216
YDialog * findDialog()
Definition: YWidget.cc:374
virtual const YPropertySet & propertySet()
Definition: YWidget.cc:393
virtual const YPropertySet & propertySet()
Definition: YPushButton.cc:198
bool isDefaultButton() const
Definition: YPushButton.cc:90
virtual void setRole(YButtonRole role)
Definition: YPushButton.cc:140
virtual YPropertyValue getProperty(const std::string &propertyName)
Definition: YWidget.cc:453
bool isEmpty() const
Definition: YProperty.h:250
bool hasFunctionKey() const
Definition: YWidget.cc:328
int functionKey() const
Definition: YWidget.cc:322
static YApplication * app()
Definition: YUI.cc:156
virtual void setFunctionKey(int fkey_no)
Definition: YPushButton.cc:172
virtual YPropertyValue getProperty(const std::string &propertyName)
Definition: YPushButton.cc:231
YPropertyType type() const
Definition: YProperty.h:156
YPushButton(YWidget *parent, const std::string &label)
Definition: YPushButton.cc:56
virtual void setLabel(const std::string &label)
Definition: YPushButton.cc:78
virtual void setHelpButton(bool helpButton=true)
Definition: YPushButton.cc:131