libyui-gtk  2.49.0
YGLabel.cc
1 /********************************************************************
2  * YaST2-GTK - http://en.opensuse.org/YaST2-GTK *
3  ********************************************************************/
4 
5 #define YUILogComponent "gtk"
6 #include "YGUI.h"
7 #include "YGUtils.h"
8 #include "YGWidget.h"
9 #include "YLabel.h"
10 
11 #define AUTO_WRAP_WIDTH 100
12 #define AUTO_WRAP_HEIGHT 20
13 
14 class YGLabel : public YLabel, public YGWidget
15 {
16 public:
17  YGLabel (YWidget *parent, const std::string &text, bool heading, bool outputField)
18  : YLabel (NULL, text, heading, outputField),
19  YGWidget (this, parent, GTK_TYPE_LABEL, NULL), _layoutPass1Width(0), _layoutPass1Height(0)
20  {
21 # if GTK_CHECK_VERSION (3, 14, 0)
22  gtk_widget_set_halign (getWidget(), GTK_ALIGN_START);
23  gtk_widget_set_valign (getWidget(), GTK_ALIGN_CENTER);
24 # else
25  gtk_misc_set_alignment (GTK_MISC (getWidget()), 0.0, 0.5);
26 # endif
27 
28  if (outputField) {
29  gtk_label_set_selectable (GTK_LABEL (getWidget()), TRUE);
30  gtk_label_set_single_line_mode (GTK_LABEL (getWidget()), TRUE);
31  YGUtils::setWidgetFont (getWidget(), PANGO_STYLE_ITALIC, PANGO_WEIGHT_NORMAL,
32  PANGO_SCALE_MEDIUM);
33  }
34  if (heading)
35  YGUtils::setWidgetFont (getWidget(), PANGO_STYLE_NORMAL, PANGO_WEIGHT_BOLD,
36  PANGO_SCALE_LARGE);
37  setLabel (text);
38  }
39 
40  virtual void setText (const std::string &label)
41  {
42  YLabel::setText (label);
43  gtk_label_set_label (GTK_LABEL (getWidget()), label.c_str());
44  std::string::size_type i = label.find ('\n', 0);
45 
46  if (!isOutputField()) {
47  bool selectable = i != std::string::npos && i != label.size()-1;
48  gtk_label_set_selectable (GTK_LABEL (getWidget()), selectable);
49  }
50 
51  }
52 
53  void setAutoWrap( bool autoWrap )
54  {
55  yuiDebug() << endl;
56  YLabel::setAutoWrap( autoWrap );
57  gtk_label_set_line_wrap (GTK_LABEL (getWidget()), gboolean(autoWrap));
58  gtk_label_set_single_line_mode (GTK_LABEL (getWidget()), gboolean(!autoWrap));
59  }
60 
61  //YGWIDGET_IMPL_COMMON (YLabel)
62  virtual bool setKeyboardFocus() {
63  return doSetKeyboardFocus();
64  }
65 
66  virtual void setEnabled (bool enabled)
67  { YLabel::setEnabled (enabled);
68  doSetEnabled (enabled);
69  }
70 
71  virtual int preferredWidth()
72  {
73 
74  // return 100;
75  int width;
76 
77  if ( autoWrap() )
78  {
79  int lpass = layoutPass();
80  //yuiDebug() << "autowrap " << autoWrap() << " layoutpass " << lpass << endl;
81  if ( lpass != 1 )
82  {
83  width = _layoutPass1Width + 2; // some pixel more for border
84  }
85  else
86  {
87  width = doPreferredSize (YD_HORIZ);
88  }
89  }
90  else // ! autoWrap()
91  width = doPreferredSize (YD_HORIZ);
92 
93 
94  return (width > AUTO_WRAP_WIDTH ? width : AUTO_WRAP_WIDTH);
95  }
96 
97  virtual int preferredHeight()
98  {
99  int height;
100 
101  if ( autoWrap() )
102  {
103  if ( layoutPass() != 1 )
104  {
105  height = _layoutPass1Height + 4; // some pixel more for border
106  }
107  else
108  {
109  height = doPreferredSize (YD_VERT);
110  }
111  }
112  else // ! autoWrap()
113  {
114  height = doPreferredSize (YD_VERT);
115  }
116 
117  return (height > AUTO_WRAP_HEIGHT ? height : AUTO_WRAP_HEIGHT);
118 
119  }
120 
121  virtual void setSize (int width, int height)
122  {
123  if ( autoWrap() )
124  {
125  int lpass = layoutPass();
126  //yuiDebug() << "layoutpass " << lpass << endl;
127  if (lpass == 1 || _layoutPass1Width <= 0)
128  {
129  _layoutPass1Width = width;
130  gint minimum_height, natural_height;
131 
132  gtk_widget_get_preferred_height_for_width
133  (getWidget(),
134  _layoutPass1Width,
135  &minimum_height,
136  &natural_height);
137  _layoutPass1Height = natural_height;
138  }
139  }
140  doSetSize (width, height);
141  }
142 
143  YGWIDGET_IMPL_USE_BOLD (YLabel)
144 protected:
145  int _layoutPass1Width;
146  int _layoutPass1Height;
147 };
148 
149 YLabel *YGWidgetFactory::createLabel (YWidget *parent,
150  const std::string &text, bool heading, bool outputField)
151 { return new YGLabel (parent, text, heading, outputField); }
152