]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/stattext.cpp
fix wxGTK1 compilation after wxTextEntry DoGetValue() change
[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 IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl)
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 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
76 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
77 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
78
79 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
80
81 #ifdef __WXGTK26__
82 if (!gtk_check_version(2,6,0))
83 {
84 // set ellipsize mode
85 PangoEllipsizeMode ellipsizeMode = PANGO_ELLIPSIZE_NONE;
86 if ( style & wxST_ELLIPSIZE_START )
87 ellipsizeMode = PANGO_ELLIPSIZE_START;
88 else if ( style & wxST_ELLIPSIZE_MIDDLE )
89 ellipsizeMode = PANGO_ELLIPSIZE_MIDDLE;
90 else if ( style & wxST_ELLIPSIZE_END )
91 ellipsizeMode = PANGO_ELLIPSIZE_END;
92
93 gtk_label_set_ellipsize( GTK_LABEL(m_widget), ellipsizeMode );
94 }
95 #endif // __WXGTK26__
96
97 SetLabel(label);
98
99 m_parent->DoAddChild( this );
100
101 PostCreation(size);
102
103 return true;
104 }
105
106 void wxStaticText::SetLabel( const wxString& str )
107 {
108 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
109
110 // save the label inside m_labelOrig in case user calls GetLabel() later
111 m_labelOrig = str;
112
113 InvalidateBestSize();
114
115 wxString label(str);
116 if (gtk_check_version(2,6,0) &&
117 IsEllipsized())
118 {
119 // GTK+ < 2.6 does not support ellipsization:
120 // since we need to use our generic code for ellipsization (which does not
121 // behaves well in conjunction with markup; i.e. it may break the markup
122 // validity erasing portions of the string), we also need to strip out
123 // the markup (if present) from the label.
124
125 label = GetEllipsizedLabelWithoutMarkup();
126 }
127
128 if ( HasFlag(wxST_MARKUP) )
129 GTKSetLabelWithMarkupForLabel(GTK_LABEL(m_widget), label);
130 else
131 GTKSetLabelForLabel(GTK_LABEL(m_widget), label);
132
133 // adjust the label size to the new label unless disabled
134 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
135 !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized!
136 SetSize( GetBestSize() );
137 }
138
139 bool wxStaticText::SetFont( const wxFont &font )
140 {
141 const bool wasUnderlined = GetFont().GetUnderlined();
142
143 bool ret = wxControl::SetFont(font);
144
145 if ( font.GetUnderlined() != wasUnderlined )
146 {
147 // the underlines for mnemonics are incompatible with using attributes
148 // so turn them off when setting underlined font and restore them when
149 // unsetting it
150 gtk_label_set_use_underline(GTK_LABEL(m_widget), wasUnderlined);
151
152 if ( wasUnderlined )
153 {
154 // it's not underlined any more, remove the attributes we set
155 gtk_label_set_attributes(GTK_LABEL(m_widget), NULL);
156 }
157 else // the text is underlined now
158 {
159 PangoAttrList *attrs = pango_attr_list_new();
160 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
161 a->start_index = 0;
162 a->end_index = (guint)-1;
163 pango_attr_list_insert(attrs, a);
164 gtk_label_set_attributes(GTK_LABEL(m_widget), attrs);
165 pango_attr_list_unref(attrs);
166 }
167 }
168
169 // adjust the label size to the new label unless disabled
170 if (!HasFlag(wxST_NO_AUTORESIZE))
171 {
172 SetSize( GetBestSize() );
173 }
174 return ret;
175 }
176
177 void wxStaticText::DoSetSize(int x, int y,
178 int width, int height,
179 int sizeFlags )
180 {
181 wxStaticTextBase::DoSetSize(x, y, width, height, sizeFlags);
182
183 if (gtk_check_version(2,6,0))
184 {
185 // GTK+ < 2.6 does not support ellipsization - we need to run our
186 // generic code (actually it will be run only if IsEllipsized() == true)
187 UpdateLabel();
188 }
189 }
190
191 wxSize wxStaticText::DoGetBestSize() const
192 {
193 // Do not return any arbitrary default value...
194 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
195
196 // GetBestSize is supposed to return unwrapped size but calling
197 // gtk_label_set_line_wrap() from here is a bad idea as it queues another
198 // size request by calling gtk_widget_queue_resize() and we end up in
199 // infinite loop sometimes (notably when the control is in a toolbar)
200 GTK_LABEL(m_widget)->wrap = FALSE;
201
202 wxSize size = wxStaticTextBase::DoGetBestSize();
203
204 GTK_LABEL(m_widget)->wrap = TRUE; // restore old value
205
206 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
207 size.x++;
208 CacheBestSize(size);
209 return size;
210 }
211
212 bool wxStaticText::GTKWidgetNeedsMnemonic() const
213 {
214 return true;
215 }
216
217 void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w)
218 {
219 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w);
220 }
221
222
223 // These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
224
225 wxString wxStaticText::DoGetLabel() const
226 {
227 GtkLabel *label = GTK_LABEL(m_widget);
228 return wxGTK_CONV_BACK( gtk_label_get_text( label ) );
229 }
230
231 void wxStaticText::DoSetLabel(const wxString& str)
232 {
233 GTKSetLabelForLabel(GTK_LABEL(m_widget), str);
234 }
235
236 // static
237 wxVisualAttributes
238 wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
239 {
240 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
241 }
242
243 #endif // wxUSE_STATTEXT