libyui  3.10.0
YLabel.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: YLabel.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #define MAX_DEBUG_LABEL_LEN 32
26 
27 
28 #define YUILogComponent "ui"
29 #include "YUILog.h"
30 
31 #include "YUISymbols.h"
32 #include "YDialog.h"
33 #include "YLabel.h"
34 
35 using std::string;
36 
37 
39 {
40  /**
41  * Constructor
42  **/
43  YLabelPrivate( const string & text,
44  bool isHeading,
45  bool isOutputField )
46  : text( text )
47  , isHeading( isHeading )
48  , isOutputField( isOutputField )
49  , useBoldFont( false )
50  , autoWrap( false )
51  {}
52 
53  string text;
54  bool isHeading;
55  bool isOutputField;
56  bool useBoldFont;
57  bool autoWrap;
58 };
59 
60 
62  const string & text,
63  bool isHeading,
64  bool isOutputField )
65  : YWidget( parent )
66  , priv( new YLabelPrivate( text, isHeading, isOutputField ) )
67 {
68  YUI_CHECK_NEW( priv );
69 }
70 
71 
73 {
74  // NOP
75 }
76 
77 
78 string YLabel::text() const
79 {
80  return priv->text;
81 }
82 
83 
84 void YLabel::setText( const string & newText )
85 {
86  priv->text = newText;
87 }
88 
89 
90 bool YLabel::isHeading() const
91 {
92  return priv->isHeading;
93 }
94 
95 
97 {
98  return priv->isOutputField;
99 }
100 
101 
103 {
104  return priv->useBoldFont;
105 }
106 
107 
108 void YLabel::setUseBoldFont( bool bold )
109 {
110  priv->useBoldFont = bold;
111 }
112 
113 
114 bool YLabel::autoWrap() const
115 {
116  return priv->autoWrap;
117 }
118 
119 
120 void YLabel::setAutoWrap( bool autoWrap )
121 {
122  priv->autoWrap = autoWrap;
123 
124  if ( autoWrap )
125  {
126  YDialog * dialog = findDialog();
127 
128  if ( dialog )
129  dialog->requestMultiPassLayout();
130  }
131 
132  setStretchable( YD_HORIZ, autoWrap );
133  setStretchable( YD_VERT, autoWrap );
134 }
135 
136 
138 {
139  const YDialog * dialog = findDialog();
140 
141  return dialog ? dialog->layoutPass() : 0;
142 }
143 
144 
145 const YPropertySet &
147 {
148  static YPropertySet propSet;
149 
150  if ( propSet.isEmpty() )
151  {
152  /*
153  * @property string Label the label text
154  * @property string Value the label text (alias for Label)
155  * @property string Text the label text (alias for Label)
156  */
157 
158  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
159  propSet.add( YProperty( YUIProperty_Value, YStringProperty ) );
160  propSet.add( YProperty( YUIProperty_Text, YStringProperty ) );
161  propSet.add( YWidget::propertySet() );
162  }
163 
164  return propSet;
165 }
166 
167 
168 bool
169 YLabel::setProperty( const string & propertyName, const YPropertyValue & val )
170 {
171  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
172 
173  if ( propertyName == YUIProperty_Label ) setText( val.stringVal() );
174  else if ( propertyName == YUIProperty_Value ) setText( val.stringVal() );
175  else if ( propertyName == YUIProperty_Text ) setText( val.stringVal() );
176  else
177  {
178  return YWidget::setProperty( propertyName, val );
179  }
180 
181  return true; // success -- no special processing necessary
182 }
183 
184 
186 YLabel::getProperty( const string & propertyName )
187 {
188  propertySet().check( propertyName ); // throws exceptions if not found
189 
190  if ( propertyName == YUIProperty_Label ) return YPropertyValue( text() );
191  else if ( propertyName == YUIProperty_Value ) return YPropertyValue( text() );
192  else if ( propertyName == YUIProperty_Text ) return YPropertyValue( text() );
193  else
194  {
195  return YWidget::getProperty( propertyName );
196  }
197 }
198 
199 
200 string YLabel::debugLabel() const
201 {
202  string label = text();
203 
204  if ( label.size() > MAX_DEBUG_LABEL_LEN )
205  {
206  label.resize( MAX_DEBUG_LABEL_LEN );
207  label.append( "..." );
208  }
209 
210  for ( string::size_type i=0; i < label.size(); i++ )
211  {
212  if ( label[i] == '\n' ) label[i] = ' ';
213  if ( label[i] == '\"' ) label[i] = ' ';
214  }
215 
216  return label;
217 }
218 
219 
220 
221 const char *
223 {
224  if ( priv->isHeading ) return "YLabel_Heading";
225  else if ( priv->isOutputField ) return "YLabel_OutputField";
226  else return "YLabel";
227 }
YLabel::useBoldFont
bool useBoldFont() const
Return 'true' if a bold font should be used.
Definition: YLabel.cc:102
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:55
YLabel::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YLabel.cc:169
YWidget::findDialog
YDialog * findDialog()
Traverse up the widget hierarchy and find the dialog this widget belongs to.
Definition: YWidget.cc:376
YPropertySet
A set of properties to check names and types against.
Definition: YProperty.h:198
YLabelPrivate::YLabelPrivate
YLabelPrivate(const string &text, bool isHeading, bool isOutputField)
Constructor.
Definition: YLabel.cc:43
YLabel::~YLabel
virtual ~YLabel()
Destructor.
Definition: YLabel.cc:72
YPropertySet::isEmpty
bool isEmpty() const
Returns 'true' if this property set does not contain anything.
Definition: YProperty.h:263
YLabel::text
std::string text() const
Return the text the widget displays.
Definition: YLabel.cc:78
YPropertyValue::stringVal
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
YLabel::layoutPass
int layoutPass()
Convenience method for the parent dialog's layoutPass(): Return the number of the current layout pass...
Definition: YLabel.cc:137
YPropertyValue::type
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169
YWidget::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:457
YProperty
Class for widget properties.
Definition: YProperty.h:52
YLabel::isHeading
bool isHeading() const
Return 'true' if this is a Heading widget, i.e., it should display its text in a bold and/or larger f...
Definition: YLabel.cc:90
YLabel::YLabel
YLabel(YWidget *parent, const std::string &text, bool isHeading=false, bool isOutputField=false)
Constructor.
Definition: YLabel.cc:61
YWidget::setStretchable
void setStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch" regardless of any hstretch or vstretch options.
Definition: YWidget.cc:560
YLabel::widgetClass
virtual const char * widgetClass() const
Returns a descriptive name of this widget class for logging, debugging etc.
Definition: YLabel.cc:222
YLabelPrivate
Definition: YLabel.cc:39
YLabel::setText
virtual void setText(const std::string &newText)
Set the text the widget displays.
Definition: YLabel.cc:84
YDialog::layoutPass
int layoutPass() const
Return the number of the current layout pass: 0: No layout going on right now 1: First pass 2: Second...
Definition: YDialog.cc:380
YPropertySet::check
void check(const std::string &propertyName) const
Check if a property 'propertyName' exists in this property set.
Definition: YProperty.cc:88
YLabel::isOutputField
bool isOutputField() const
Return 'true' if this is an OutputField widget, i.e., it should display its text similar to an InputF...
Definition: YLabel.cc:96
YPropertyValue
Transport class for the value of simple properties.
Definition: YProperty.h:105
YDialog::requestMultiPassLayout
void requestMultiPassLayout()
Request multiple passes of the layout engine.
Definition: YDialog.cc:662
YLabel::debugLabel
virtual std::string debugLabel() const
Returns a descriptive label of this widget instance for debugging.
Definition: YLabel.cc:200
YLabel::setAutoWrap
virtual void setAutoWrap(bool autoWrap=true)
Enable or disable automatic word wrapping.
Definition: YLabel.cc:120
YLabel::autoWrap
bool autoWrap() const
Return 'true' if automatic word wrapping is enabled.
Definition: YLabel.cc:114
YLabel::getProperty
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YLabel.cc:186
YLabel::setUseBoldFont
virtual void setUseBoldFont(bool bold=true)
Switch bold font on or off.
Definition: YLabel.cc:108
YWidget::setProperty
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:432
YDialog
A window in the desktop environment.
Definition: YDialog.h:48
YLabel::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YLabel.cc:146
YWidget::propertySet
virtual const YPropertySet & propertySet()
Return this class's property set.
Definition: YWidget.cc:395