1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/stattext.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #if wxUSE_STATTEXT
13
14 #include "wx/stattext.h"
15
16 #include <gtk/gtk.h>
17 #include "wx/gtk/private.h"
18
19 //-----------------------------------------------------------------------------
20 // wxStaticText
21 //-----------------------------------------------------------------------------
22
wxStaticText()23 wxStaticText::wxStaticText()
24 {
25 }
26
wxStaticText(wxWindow * parent,wxWindowID id,const wxString & label,const wxPoint & pos,const wxSize & size,long style,const wxString & name)27 wxStaticText::wxStaticText(wxWindow *parent,
28 wxWindowID id,
29 const wxString &label,
30 const wxPoint &pos,
31 const wxSize &size,
32 long style,
33 const wxString &name)
34 {
35 Create( parent, id, label, pos, size, style, name );
36 }
37
Create(wxWindow * parent,wxWindowID id,const wxString & label,const wxPoint & pos,const wxSize & size,long style,const wxString & name)38 bool wxStaticText::Create(wxWindow *parent,
39 wxWindowID id,
40 const wxString &label,
41 const wxPoint &pos,
42 const wxSize &size,
43 long style,
44 const wxString &name )
45 {
46 if (!PreCreation( parent, pos, size ) ||
47 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
48 {
49 wxFAIL_MSG( wxT("wxStaticText creation failed") );
50 return false;
51 }
52
53 m_widget = gtk_label_new(NULL);
54 g_object_ref(m_widget);
55
56 GtkJustification justify;
57 if ( style & wxALIGN_CENTER_HORIZONTAL )
58 justify = GTK_JUSTIFY_CENTER;
59 else if ( style & wxALIGN_RIGHT )
60 justify = GTK_JUSTIFY_RIGHT;
61 else
62 justify = GTK_JUSTIFY_LEFT;
63
64 if (GetLayoutDirection() == wxLayout_RightToLeft)
65 {
66 if (justify == GTK_JUSTIFY_RIGHT)
67 justify = GTK_JUSTIFY_LEFT;
68 else if (justify == GTK_JUSTIFY_LEFT)
69 justify = GTK_JUSTIFY_RIGHT;
70 }
71
72 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
73
74 // set ellipsize mode
75 PangoEllipsizeMode ellipsizeMode = PANGO_ELLIPSIZE_NONE;
76 if ( style & wxST_ELLIPSIZE_START )
77 ellipsizeMode = PANGO_ELLIPSIZE_START;
78 else if ( style & wxST_ELLIPSIZE_MIDDLE )
79 ellipsizeMode = PANGO_ELLIPSIZE_MIDDLE;
80 else if ( style & wxST_ELLIPSIZE_END )
81 ellipsizeMode = PANGO_ELLIPSIZE_END;
82
83 gtk_label_set_ellipsize( GTK_LABEL(m_widget), ellipsizeMode );
84
85 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
86 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
87 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
88
89 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
90
91 SetLabel(label);
92
93 m_parent->DoAddChild( this );
94
95 PostCreation(size);
96
97 #ifndef __WXGTK3__
98 // GtkLabel does its layout based on its size-request, rather than its
99 // actual size. The size-request may not always get set, specifically if
100 // the initial size is fully specified. So make sure it's set here.
101 gtk_widget_set_size_request(m_widget, m_width, m_height);
102 #endif
103
104 return true;
105 }
106
GTKDoSetLabel(GTKLabelSetter setter,const wxString & label)107 void wxStaticText::GTKDoSetLabel(GTKLabelSetter setter, const wxString& label)
108 {
109 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
110
111 InvalidateBestSize();
112
113 (this->*setter)(GTK_LABEL(m_widget), label);
114
115 // adjust the label size to the new label unless disabled
116 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
117 !IsEllipsized() ) // if ellipsization is ON, then we don't want to get resized!
118 SetSize( GetBestSize() );
119 }
120
SetLabel(const wxString & label)121 void wxStaticText::SetLabel(const wxString& label)
122 {
123 m_labelOrig = label;
124
125 GTKDoSetLabel(&wxStaticText::GTKSetLabelForLabel, label);
126 }
127
128 #if wxUSE_MARKUP
129
DoSetLabelMarkup(const wxString & markup)130 bool wxStaticText::DoSetLabelMarkup(const wxString& markup)
131 {
132 const wxString stripped = RemoveMarkup(markup);
133 if ( stripped.empty() && !markup.empty() )
134 return false;
135
136 m_labelOrig = stripped;
137
138 GTKDoSetLabel(&wxStaticText::GTKSetLabelWithMarkupForLabel, markup);
139
140 return true;
141 }
142
143 #endif // wxUSE_MARKUP
144
SetFont(const wxFont & font)145 bool wxStaticText::SetFont( const wxFont &font )
146 {
147 const bool wasUnderlined = GetFont().GetUnderlined();
148 const bool wasStrickenThrough = GetFont().GetStrikethrough();
149
150 bool ret = wxControl::SetFont(font);
151
152 const bool isUnderlined = GetFont().GetUnderlined();
153 const bool isStrickenThrough = GetFont().GetStrikethrough();
154
155 if ( (isUnderlined != wasUnderlined) ||
156 (isStrickenThrough != wasStrickenThrough) )
157 {
158 // We need to update the Pango attributes used for the text.
159 if ( isUnderlined || isStrickenThrough )
160 {
161 PangoAttrList* const attrs = pango_attr_list_new();
162 if ( isUnderlined )
163 {
164 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
165 a->start_index = 0;
166 a->end_index = (guint)-1;
167 pango_attr_list_insert(attrs, a);
168 }
169
170 if ( isStrickenThrough )
171 {
172 PangoAttribute *a = pango_attr_strikethrough_new( TRUE );
173 a->start_index = 0;
174 a->end_index = (guint) -1;
175 pango_attr_list_insert(attrs, a);
176 }
177
178 gtk_label_set_attributes(GTK_LABEL(m_widget), attrs);
179 pango_attr_list_unref(attrs);
180 }
181 else // No special attributes any more.
182 {
183 // Just remove any attributes we had set.
184 gtk_label_set_attributes(GTK_LABEL(m_widget), NULL);
185 }
186
187 // The underlines for mnemonics are incompatible with using attributes
188 // so turn them off when setting underlined font.
189 gtk_label_set_use_underline(GTK_LABEL(m_widget), !isUnderlined);
190 }
191
192 // adjust the label size to the new label unless disabled
193 if (!HasFlag(wxST_NO_AUTORESIZE))
194 {
195 SetSize( GetBestSize() );
196 }
197 return ret;
198 }
199
DoGetBestSize() const200 wxSize wxStaticText::DoGetBestSize() const
201 {
202 // Do not return any arbitrary default value...
203 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
204
205 // GetBestSize is supposed to return unwrapped size but calling
206 // gtk_label_set_line_wrap() from here is a bad idea as it queues another
207 // size request by calling gtk_widget_queue_resize() and we end up in
208 // infinite loop sometimes (notably when the control is in a toolbar)
209 // With GTK3 however, there is no simple alternative, and the sizing loop
210 // no longer seems to occur.
211 #ifdef __WXGTK3__
212 gtk_label_set_line_wrap(GTK_LABEL(m_widget), false);
213 #else
214 GTK_LABEL(m_widget)->wrap = FALSE;
215 #endif
216 wxSize size = wxStaticTextBase::DoGetBestSize();
217 #ifdef __WXGTK3__
218 gtk_label_set_line_wrap(GTK_LABEL(m_widget), true);
219 #else
220 GTK_LABEL(m_widget)->wrap = TRUE; // restore old value
221 #endif
222
223 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
224 size.x++;
225 CacheBestSize(size);
226 return size;
227 }
228
GTKWidgetNeedsMnemonic() const229 bool wxStaticText::GTKWidgetNeedsMnemonic() const
230 {
231 return true;
232 }
233
GTKWidgetDoSetMnemonic(GtkWidget * w)234 void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w)
235 {
236 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w);
237 }
238
239
240 // These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
241
DoGetLabel() const242 wxString wxStaticText::DoGetLabel() const
243 {
244 GtkLabel *label = GTK_LABEL(m_widget);
245 return wxGTK_CONV_BACK( gtk_label_get_text( label ) );
246 }
247
DoSetLabel(const wxString & str)248 void wxStaticText::DoSetLabel(const wxString& str)
249 {
250 GTKSetLabelForLabel(GTK_LABEL(m_widget), str);
251 }
252
253 // static
254 wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant WXUNUSED (variant))255 wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
256 {
257 return GetDefaultAttributesFromGTKWidget(gtk_label_new(""));
258 }
259
260 #endif // wxUSE_STATTEXT
261