]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/stattext.cpp
8c25111fbe63b03a7fda54d67988a681c225a529
[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 {
76 // set ellipsize mode
77 PangoEllipsizeMode ellipsizeMode = PANGO_ELLIPSIZE_NONE;
78 if ( style & wxST_ELLIPSIZE_START )
79 ellipsizeMode = PANGO_ELLIPSIZE_START;
80 else if ( style & wxST_ELLIPSIZE_MIDDLE )
81 ellipsizeMode = PANGO_ELLIPSIZE_MIDDLE;
82 else if ( style & wxST_ELLIPSIZE_END )
83 ellipsizeMode = PANGO_ELLIPSIZE_END;
84
85 gtk_label_set_ellipsize( GTK_LABEL(m_widget), ellipsizeMode );
86 }
87
88 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
89 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
90 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
91
92 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
93
94 SetLabel(label);
95
96 m_parent->DoAddChild( this );
97
98 PostCreation(size);
99
100 return true;
101 }
102
103 void wxStaticText::GTKDoSetLabel(GTKLabelSetter setter, const wxString& label)
104 {
105 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
106
107 InvalidateBestSize();
108
109 {
110 (this->*setter)(GTK_LABEL(m_widget), label);
111 }
112
113 // adjust the label size to the new label unless disabled
114 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
115 !IsEllipsized() ) // if ellipsization is ON, then we don't want to get resized!
116 SetSize( GetBestSize() );
117 }
118
119 void wxStaticText::SetLabel(const wxString& label)
120 {
121 m_labelOrig = label;
122
123 GTKDoSetLabel(&wxStaticText::GTKSetLabelForLabel, label);
124 }
125
126 #if wxUSE_MARKUP
127
128 bool wxStaticText::DoSetLabelMarkup(const wxString& markup)
129 {
130 const wxString stripped = RemoveMarkup(markup);
131 if ( stripped.empty() && !markup.empty() )
132 return false;
133
134 m_labelOrig = stripped;
135
136 GTKDoSetLabel(&wxStaticText::GTKSetLabelWithMarkupForLabel, markup);
137
138 return true;
139 }
140
141 #endif // wxUSE_MARKUP
142
143 bool wxStaticText::SetFont( const wxFont &font )
144 {
145 const bool wasUnderlined = GetFont().GetUnderlined();
146 const bool wasStrickenThrough = GetFont().GetStrikethrough();
147
148 bool ret = wxControl::SetFont(font);
149
150 const bool isUnderlined = GetFont().GetUnderlined();
151 const bool isStrickenThrough = GetFont().GetStrikethrough();
152
153 if ( (isUnderlined != wasUnderlined) ||
154 (isStrickenThrough != wasStrickenThrough) )
155 {
156 // We need to update the Pango attributes used for the text.
157 if ( isUnderlined || isStrickenThrough )
158 {
159 PangoAttrList* const attrs = pango_attr_list_new();
160 if ( isUnderlined )
161 {
162 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
163 a->start_index = 0;
164 a->end_index = (guint)-1;
165 pango_attr_list_insert(attrs, a);
166 }
167
168 if ( isStrickenThrough )
169 {
170 PangoAttribute *a = pango_attr_strikethrough_new( TRUE );
171 a->start_index = 0;
172 a->end_index = (guint) -1;
173 pango_attr_list_insert(attrs, a);
174 }
175
176 gtk_label_set_attributes(GTK_LABEL(m_widget), attrs);
177 pango_attr_list_unref(attrs);
178 }
179 else // No special attributes any more.
180 {
181 // Just remove any attributes we had set.
182 gtk_label_set_attributes(GTK_LABEL(m_widget), NULL);
183 }
184
185 // The underlines for mnemonics are incompatible with using attributes
186 // so turn them off when setting underlined font.
187 gtk_label_set_use_underline(GTK_LABEL(m_widget), !isUnderlined);
188 }
189
190 // adjust the label size to the new label unless disabled
191 if (!HasFlag(wxST_NO_AUTORESIZE))
192 {
193 SetSize( GetBestSize() );
194 }
195 return ret;
196 }
197
198 void wxStaticText::DoSetSize(int x, int y,
199 int width, int height,
200 int sizeFlags )
201 {
202 wxStaticTextBase::DoSetSize(x, y, width, height, sizeFlags);
203 }
204
205 wxSize wxStaticText::DoGetBestSize() const
206 {
207 // Do not return any arbitrary default value...
208 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
209
210 // GetBestSize is supposed to return unwrapped size but calling
211 // gtk_label_set_line_wrap() from here is a bad idea as it queues another
212 // size request by calling gtk_widget_queue_resize() and we end up in
213 // infinite loop sometimes (notably when the control is in a toolbar)
214 // With GTK3 however, there is no simple alternative, and the sizing loop
215 // no longer seems to occur.
216 #ifdef __WXGTK3__
217 gtk_label_set_line_wrap(GTK_LABEL(m_widget), false);
218 #else
219 GTK_LABEL(m_widget)->wrap = FALSE;
220 #endif
221 wxSize size = wxStaticTextBase::DoGetBestSize();
222 #ifdef __WXGTK3__
223 gtk_label_set_line_wrap(GTK_LABEL(m_widget), true);
224 #else
225 GTK_LABEL(m_widget)->wrap = TRUE; // restore old value
226 #endif
227
228 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
229 size.x++;
230 CacheBestSize(size);
231 return size;
232 }
233
234 bool wxStaticText::GTKWidgetNeedsMnemonic() const
235 {
236 return true;
237 }
238
239 void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w)
240 {
241 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w);
242 }
243
244
245 // These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
246
247 wxString wxStaticText::DoGetLabel() const
248 {
249 GtkLabel *label = GTK_LABEL(m_widget);
250 return wxGTK_CONV_BACK( gtk_label_get_text( label ) );
251 }
252
253 void wxStaticText::DoSetLabel(const wxString& str)
254 {
255 GTKSetLabelForLabel(GTK_LABEL(m_widget), str);
256 }
257
258 // static
259 wxVisualAttributes
260 wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
261 {
262 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
263 }
264
265 #endif // wxUSE_STATTEXT