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