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