added support for ellipsization and markup in wxStaticText (modified patch 1629946)
[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 #include "gdk/gdk.h"
19 #include "gtk/gtk.h"
20
21 extern "C"
22 void wxgtk_window_size_request_callback(GtkWidget *widget,
23 GtkRequisition *requisition,
24 wxWindow *win);
25
26 //-----------------------------------------------------------------------------
27 // wxStaticText
28 //-----------------------------------------------------------------------------
29
30 IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl)
31
32 wxStaticText::wxStaticText()
33 {
34 }
35
36 wxStaticText::wxStaticText(wxWindow *parent,
37 wxWindowID id,
38 const wxString &label,
39 const wxPoint &pos,
40 const wxSize &size,
41 long style,
42 const wxString &name)
43 {
44 Create( parent, id, label, pos, size, style, name );
45 }
46
47 bool wxStaticText::Create(wxWindow *parent,
48 wxWindowID id,
49 const wxString &label,
50 const wxPoint &pos,
51 const wxSize &size,
52 long style,
53 const wxString &name )
54 {
55 m_needParent = TRUE;
56
57 if (!PreCreation( parent, pos, size ) ||
58 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
59 {
60 wxFAIL_MSG( wxT("wxStaticText creation failed") );
61 return FALSE;
62 }
63
64 m_widget = gtk_label_new(NULL);
65
66 GtkJustification justify;
67 if ( style & wxALIGN_CENTER )
68 justify = GTK_JUSTIFY_CENTER;
69 else if ( style & wxALIGN_RIGHT )
70 justify = GTK_JUSTIFY_RIGHT;
71 else // wxALIGN_LEFT is 0
72 justify = GTK_JUSTIFY_LEFT;
73
74 if (GetLayoutDirection() == wxLayout_RightToLeft)
75 {
76 if (justify == GTK_JUSTIFY_RIGHT)
77 justify = GTK_JUSTIFY_LEFT;
78 if (justify == GTK_JUSTIFY_LEFT)
79 justify = GTK_JUSTIFY_RIGHT;
80 }
81
82 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
83
84 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
85 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
86 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
87
88 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
89
90 #ifdef __WXGTK26__
91 if (!gtk_check_version(2,6,0))
92 {
93 // set ellipsize mode
94 PangoEllipsizeMode ellipsizeMode = PANGO_ELLIPSIZE_NONE;
95 if ( style & wxST_ELLIPSIZE_START )
96 ellipsizeMode = PANGO_ELLIPSIZE_START;
97 else if ( style & wxST_ELLIPSIZE_MIDDLE )
98 ellipsizeMode = PANGO_ELLIPSIZE_MIDDLE;
99 else if ( style & wxST_ELLIPSIZE_END )
100 ellipsizeMode = PANGO_ELLIPSIZE_END;
101
102 gtk_label_set_ellipsize( GTK_LABEL(m_widget), ellipsizeMode );
103 }
104 #endif // __WXGTK26__
105
106 SetLabel(label);
107
108 m_parent->DoAddChild( this );
109
110 PostCreation(size);
111
112 // the bug below only happens with GTK 2
113 if ( justify != GTK_JUSTIFY_LEFT )
114 {
115 // if we let GTK call wxgtk_window_size_request_callback the label
116 // always shrinks to its minimal size for some reason and so no
117 // alignment except the default left doesn't work (in fact it does,
118 // but you don't see it)
119 g_signal_handlers_disconnect_by_func (m_widget,
120 (gpointer) wxgtk_window_size_request_callback,
121 this);
122 }
123
124 return TRUE;
125 }
126
127 wxString wxStaticText::GetLabel() const
128 {
129 // we need to return the label just like it was passed to the last call
130 // to SetLabel(): i.e. with wx-style mnemonics and with markup
131 return wxControl::GetLabel();
132 }
133
134 void wxStaticText::SetLabel( const wxString& str )
135 {
136 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
137
138 // save the label inside m_labelOrig in case user calls GetLabel() later
139 m_labelOrig = str;
140
141 wxString label(str);
142 if (gtk_check_version(2,6,0) &&
143 IsEllipsized())
144 {
145 // GTK+ < 2.6 does not support ellipsization:
146 // since we need to use our generic code for ellipsization (which does not
147 // behaves well in conjunction with markup; i.e. it may break the markup
148 // validity erasing portions of the string), we also need to strip out
149 // the markup (if present) from the label.
150
151 label = GetEllipsizedLabelWithoutMarkup();
152 }
153
154 if ( HasFlag(wxST_MARKUP) )
155 GTKSetLabelWithMarkupForLabel(GTK_LABEL(m_widget), label);
156 else
157 GTKSetLabelForLabel(GTK_LABEL(m_widget), label);
158
159 // adjust the label size to the new label unless disabled
160 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
161 !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized!
162 SetSize( GetBestSize() );
163 }
164
165 bool wxStaticText::SetFont( const wxFont &font )
166 {
167 const bool wasUnderlined = GetFont().GetUnderlined();
168
169 bool ret = wxControl::SetFont(font);
170
171 if ( font.GetUnderlined() != wasUnderlined )
172 {
173 // the underlines for mnemonics are incompatible with using attributes
174 // so turn them off when setting underlined font and restore them when
175 // unsetting it
176 gtk_label_set_use_underline(GTK_LABEL(m_widget), wasUnderlined);
177
178 if ( wasUnderlined )
179 {
180 // it's not underlined any more, remove the attributes we set
181 gtk_label_set_attributes(GTK_LABEL(m_widget), NULL);
182 }
183 else // the text is underlined now
184 {
185 PangoAttrList *attrs = pango_attr_list_new();
186 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
187 a->start_index = 0;
188 a->end_index = (guint)-1;
189 pango_attr_list_insert(attrs, a);
190 gtk_label_set_attributes(GTK_LABEL(m_widget), attrs);
191 pango_attr_list_unref(attrs);
192 }
193 }
194
195 // adjust the label size to the new label unless disabled
196 if (!HasFlag(wxST_NO_AUTORESIZE))
197 {
198 InvalidateBestSize();
199 SetSize( GetBestSize() );
200 }
201 return ret;
202 }
203
204 void wxStaticText::DoSetSize(int x, int y,
205 int width, int height,
206 int sizeFlags )
207 {
208 wxControl::DoSetSize( x, y, width, height, sizeFlags );
209
210 if (gtk_check_version(2,6,0))
211 {
212 // GTK+ < 2.6 does not support ellipsization - we need to run our
213 // generic code (actually it will be run only if IsEllipsized() == true)
214 UpdateLabel();
215 }
216 }
217
218 wxSize wxStaticText::DoGetBestSize() const
219 {
220 // Do not return any arbitrary default value...
221 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
222
223 // GetBestSize is supposed to return unwrapped size but calling
224 // gtk_label_set_line_wrap() from here is a bad idea as it queues another
225 // size request by calling gtk_widget_queue_resize() and we end up in
226 // infinite loop sometimes (notably when the control is in a toolbar)
227 GTK_LABEL(m_widget)->wrap = FALSE;
228
229 GtkRequisition req;
230 req.width = -1;
231 req.height = -1;
232 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
233 (m_widget, &req );
234
235 GTK_LABEL(m_widget)->wrap = TRUE; // restore old value
236
237 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
238 return wxSize (req.width+1, req.height);
239 }
240
241 bool wxStaticText::SetForegroundColour(const wxColour& colour)
242 {
243 // First, we call the base class member
244 wxControl::SetForegroundColour(colour);
245 // Then, to force the color change, we set the label with the current label
246 SetLabel(GetLabel());
247 return true;
248 }
249
250 bool wxStaticText::GTKWidgetNeedsMnemonic() const
251 {
252 return true;
253 }
254
255 void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w)
256 {
257 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w);
258 }
259
260
261 // These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
262
263 wxString wxStaticText::DoGetLabel() const
264 {
265 GtkLabel *label = GTK_LABEL(m_widget);
266 return wxGTK_CONV_BACK( gtk_label_get_text( label ) );
267 }
268
269 void wxStaticText::DoSetLabel(const wxString& str)
270 {
271 GTKSetLabelForLabel(GTK_LABEL(m_widget), str);
272 }
273
274 // static
275 wxVisualAttributes
276 wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
277 {
278 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
279 }
280
281 #endif // wxUSE_STATTEXT