libyui  3.0.5
 All Classes Functions Variables Enumerations Friends
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 "YLabel.h"
33 
34 
36 {
37  /**
38  * Constructor
39  **/
40  YLabelPrivate( const std::string & text,
41  bool isHeading,
42  bool isOutputField )
43  : text( text )
44  , isHeading( isHeading )
45  , isOutputField( isOutputField )
46  , useBoldFont( false )
47  {}
48 
49  std::string text;
50  bool isHeading;
51  bool isOutputField;
52  bool useBoldFont;
53 };
54 
55 
57  const std::string & text,
58  bool isHeading,
59  bool isOutputField )
60  : YWidget( parent )
61  , priv( new YLabelPrivate( text, isHeading, isOutputField ) )
62 {
63  YUI_CHECK_NEW( priv );
64 }
65 
66 
68 {
69  // NOP
70 }
71 
72 
73 std::string YLabel::text() const
74 {
75  return priv->text;
76 }
77 
78 
79 void YLabel::setText( const std::string & newText )
80 {
81  priv->text = newText;
82 }
83 
84 
85 bool YLabel::isHeading() const
86 {
87  return priv->isHeading;
88 }
89 
90 
92 {
93  return priv->isOutputField;
94 }
95 
96 
97 bool YLabel::useBoldFont() const
98 {
99  return priv->useBoldFont;
100 }
101 
102 
103 void YLabel::setUseBoldFont( bool bold )
104 {
105  priv->useBoldFont = bold;
106 }
107 
108 
109 const YPropertySet &
111 {
112  static YPropertySet propSet;
113 
114  if ( propSet.isEmpty() )
115  {
116  /*
117  * @property std::string Label the label text
118  * @property std::string Value the label text (alias for Label)
119  * @property std::string Text the label text (alias for Label)
120  */
121 
122  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
123  propSet.add( YProperty( YUIProperty_Value, YStringProperty ) );
124  propSet.add( YProperty( YUIProperty_Text, YStringProperty ) );
125  propSet.add( YWidget::propertySet() );
126  }
127 
128  return propSet;
129 }
130 
131 
132 bool
133 YLabel::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_Label ) setText( val.stringVal() );
138  else if ( propertyName == YUIProperty_Value ) setText( val.stringVal() );
139  else if ( propertyName == YUIProperty_Text ) setText( 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 YLabel::getProperty( const std::string & propertyName )
151 {
152  propertySet().check( propertyName ); // throws exceptions if not found
153 
154  if ( propertyName == YUIProperty_Label ) return YPropertyValue( text() );
155  else if ( propertyName == YUIProperty_Value ) return YPropertyValue( text() );
156  else if ( propertyName == YUIProperty_Text ) return YPropertyValue( text() );
157  else
158  {
159  return YWidget::getProperty( propertyName );
160  }
161 }
162 
163 
164 std::string YLabel::debugLabel() const
165 {
166  std::string label = text();
167 
168  if ( label.size() > MAX_DEBUG_LABEL_LEN )
169  {
170  label.resize( MAX_DEBUG_LABEL_LEN );
171  label.append( "..." );
172  }
173 
174  for ( std::string::size_type i=0; i < label.size(); i++ )
175  {
176  if ( label[i] == '\n' ) label[i] = ' ';
177  if ( label[i] == '\"' ) label[i] = ' ';
178  }
179 
180  return label;
181 }
182 
183 
184 
185 const char *
187 {
188  if ( priv->isHeading ) return "YLabel_Heading";
189  else if ( priv->isOutputField ) return "YLabel_OutputField";
190  else return "YLabel";
191 }