Replace wxST_MARKUP style with wxControl::SetLabelMarkup().
[wxWidgets.git] / src / gtk / stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/gtk/private.h"
17
18 //-----------------------------------------------------------------------------
19 // wxStaticText
20 //-----------------------------------------------------------------------------
21
22 wxStaticText::wxStaticText()
23 {
24 }
25
26 wxStaticText::wxStaticText(wxWindow *parent,
27 wxWindowID id,
28 const wxString &label,
29 const wxPoint &pos,
30 const wxSize &size,
31 long style,
32 const wxString &name)
33 {
34 Create( parent, id, label, pos, size, style, name );
35 }
36
37 bool wxStaticText::Create(wxWindow *parent,
38 wxWindowID id,
39 const wxString &label,
40 const wxPoint &pos,
41 const wxSize &size,
42 long style,
43 const wxString &name )
44 {
45 if (!PreCreation( parent, pos, size ) ||
46 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
47 {
48 wxFAIL_MSG( wxT("wxStaticText creation failed") );
49 return false;
50 }
51
52 m_widget = gtk_label_new(NULL);
53 g_object_ref(m_widget);
54
55 GtkJustification justify;
56 if ( style & wxALIGN_CENTER_HORIZONTAL )
57 justify = GTK_JUSTIFY_CENTER;
58 else if ( style & wxALIGN_RIGHT )
59 justify = GTK_JUSTIFY_RIGHT;
60 else
61 justify = GTK_JUSTIFY_LEFT;
62
63 if (GetLayoutDirection() == wxLayout_RightToLeft)
64 {
65 if (justify == GTK_JUSTIFY_RIGHT)
66 justify = GTK_JUSTIFY_LEFT;
67 else if (justify == GTK_JUSTIFY_LEFT)
68 justify = GTK_JUSTIFY_RIGHT;
69 }
70
71 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
72
73 #ifdef __WXGTK26__
74 if (!gtk_check_version(2,6,0))
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 #endif // __WXGTK26__
88
89 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
90 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
91 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
92
93 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
94
95 SetLabel(label);
96
97 m_parent->DoAddChild( this );
98
99 PostCreation(size);
100
101 return true;
102 }
103
104 void wxStaticText::GTKDoSetLabel(GTKLabelSetter setter, const wxString& label)
105 {
106 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
107
108 InvalidateBestSize();
109
110 if (gtk_check_version(2,6,0) && IsEllipsized())
111 {
112 // GTK+ < 2.6 does not support ellipsization so we need to do it
113 // manually and as our ellipsization code doesn't deal with markup, we
114 // have no choice but to ignore it in this case and always use plain
115 // text.
116 GTKSetLabelForLabel(GTK_LABEL(m_widget), GetEllipsizedLabel());
117 }
118 else // Ellipsization not needed or supported by GTK+.
119 {
120 (this->*setter)(GTK_LABEL(m_widget), label);
121 }
122
123 // adjust the label size to the new label unless disabled
124 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
125 !IsEllipsized() ) // if ellipsization is ON, then we don't want to get resized!
126 SetSize( GetBestSize() );
127 }
128
129 void wxStaticText::SetLabel(const wxString& label)
130 {
131 m_labelOrig = label;
132
133 GTKDoSetLabel(&wxStaticText::GTKSetLabelForLabel, label);
134 }
135
136 bool wxStaticText::DoSetLabelMarkup(const wxString& markup)
137 {
138 const wxString stripped = RemoveMarkup(markup);
139 if ( stripped.empty() && !markup.empty() )
140 return false;
141
142 m_labelOrig = stripped;
143
144 GTKDoSetLabel(&wxStaticText::GTKSetLabelWithMarkupForLabel, markup);
145
146 return true;
147 }
148
149 bool wxStaticText::SetFont( const wxFont &font )
150 {
151 const bool wasUnderlined = GetFont().GetUnderlined();
152
153 bool ret = wxControl::SetFont(font);
154
155 if ( font.GetUnderlined() != wasUnderlined )
156 {
157 // the underlines for mnemonics are incompatible with using attributes
158 // so turn them off when setting underlined font and restore them when
159 // unsetting it
160 gtk_label_set_use_underline(GTK_LABEL(m_widget), wasUnderlined);
161
162 if ( wasUnderlined )
163 {
164 // it's not underlined any more, remove the attributes we set
165 gtk_label_set_attributes(GTK_LABEL(m_widget), NULL);
166 }
167 else // the text is underlined now
168 {
169 PangoAttrList *attrs = pango_attr_list_new();
170 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
171 a->start_index = 0;
172 a->end_index = (guint)-1;
173 pango_attr_list_insert(attrs, a);
174 gtk_label_set_attributes(GTK_LABEL(m_widget), attrs);
175 pango_attr_list_unref(attrs);
176 }
177 }
178
179 // adjust the label size to the new label unless disabled
180 if (!HasFlag(wxST_NO_AUTORESIZE))
181 {
182 SetSize( GetBestSize() );
183 }
184 return ret;
185 }
186
187 void wxStaticText::DoSetSize(int x, int y,
188 int width, int height,
189 int sizeFlags )
190 {
191 wxStaticTextBase::DoSetSize(x, y, width, height, sizeFlags);
192
193 if (gtk_check_version(2,6,0))
194 {
195 // GTK+ < 2.6 does not support ellipsization - we need to run our
196 // generic code (actually it will be run only if IsEllipsized() == true)
197 UpdateLabel();
198 }
199 }
200
201 wxSize wxStaticText::DoGetBestSize() const
202 {
203 // Do not return any arbitrary default value...
204 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
205
206 // GetBestSize is supposed to return unwrapped size but calling
207 // gtk_label_set_line_wrap() from here is a bad idea as it queues another
208 // size request by calling gtk_widget_queue_resize() and we end up in
209 // infinite loop sometimes (notably when the control is in a toolbar)
210 GTK_LABEL(m_widget)->wrap = FALSE;
211
212 wxSize size = wxStaticTextBase::DoGetBestSize();
213
214 GTK_LABEL(m_widget)->wrap = TRUE; // restore old value
215
216 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
217 size.x++;
218 CacheBestSize(size);
219 return size;
220 }
221
222 bool wxStaticText::GTKWidgetNeedsMnemonic() const
223 {
224 return true;
225 }
226
227 void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w)
228 {
229 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w);
230 }
231
232
233 // These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
234
235 wxString wxStaticText::DoGetLabel() const
236 {
237 GtkLabel *label = GTK_LABEL(m_widget);
238 return wxGTK_CONV_BACK( gtk_label_get_text( label ) );
239 }
240
241 void wxStaticText::DoSetLabel(const wxString& str)
242 {
243 GTKSetLabelForLabel(GTK_LABEL(m_widget), str);
244 }
245
246 // static
247 wxVisualAttributes
248 wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
249 {
250 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
251 }
252
253 #endif // wxUSE_STATTEXT