]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/gtk/stattext.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
a81258be RR |
5 | // Id: $Id$ |
6 | // Copyright: (c) 1998 Robert Roebling | |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 VS |
10 | // For compilers that support precompilation, includes "wx.h". |
11 | #include "wx/wxprec.h" | |
1e6feb95 VZ |
12 | |
13 | #if wxUSE_STATTEXT | |
14 | ||
c801d85f | 15 | #include "wx/stattext.h" |
9dc44eff PC |
16 | |
17 | #include <gtk/gtk.h> | |
fab591c5 | 18 | #include "wx/gtk/private.h" |
c801d85f KB |
19 | |
20 | //----------------------------------------------------------------------------- | |
21 | // wxStaticText | |
22 | //----------------------------------------------------------------------------- | |
23 | ||
b58197f2 | 24 | wxStaticText::wxStaticText() |
c801d85f | 25 | { |
3f659fd6 | 26 | } |
c801d85f | 27 | |
b58197f2 VZ |
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) | |
c801d85f | 35 | { |
39bc0347 | 36 | Create( parent, id, label, pos, size, style, name ); |
3f659fd6 | 37 | } |
c801d85f | 38 | |
b58197f2 VZ |
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 ) | |
c801d85f | 46 | { |
4dcaf11a RR |
47 | if (!PreCreation( parent, pos, size ) || |
48 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
49 | { | |
8ccac798 | 50 | wxFAIL_MSG( wxT("wxStaticText creation failed") ); |
e8375af8 | 51 | return false; |
4dcaf11a | 52 | } |
b58197f2 | 53 | |
39bc0347 | 54 | m_widget = gtk_label_new(NULL); |
9ff9d30c | 55 | g_object_ref(m_widget); |
3d257b8d | 56 | |
a93109d5 | 57 | GtkJustification justify; |
88684daa | 58 | if ( style & wxALIGN_CENTER_HORIZONTAL ) |
a93109d5 RR |
59 | justify = GTK_JUSTIFY_CENTER; |
60 | else if ( style & wxALIGN_RIGHT ) | |
61 | justify = GTK_JUSTIFY_RIGHT; | |
7f85af82 | 62 | else |
a93109d5 | 63 | justify = GTK_JUSTIFY_LEFT; |
f4322df6 | 64 | |
69597639 | 65 | if (GetLayoutDirection() == wxLayout_RightToLeft) |
f4322df6 | 66 | { |
a1a48c8a | 67 | if (justify == GTK_JUSTIFY_RIGHT) |
69597639 | 68 | justify = GTK_JUSTIFY_LEFT; |
a1a48c8a | 69 | else if (justify == GTK_JUSTIFY_LEFT) |
69597639 RR |
70 | justify = GTK_JUSTIFY_RIGHT; |
71 | } | |
f4322df6 | 72 | |
a93109d5 | 73 | gtk_label_set_justify(GTK_LABEL(m_widget), justify); |
1a5594b8 | 74 | |
39bc0347 | 75 | #ifdef __WXGTK26__ |
9dc44eff | 76 | #ifndef __WXGTK3__ |
39bc0347 | 77 | if (!gtk_check_version(2,6,0)) |
9dc44eff | 78 | #endif |
39bc0347 VZ |
79 | { |
80 | // set ellipsize mode | |
81 | PangoEllipsizeMode ellipsizeMode = PANGO_ELLIPSIZE_NONE; | |
82 | if ( style & wxST_ELLIPSIZE_START ) | |
83 | ellipsizeMode = PANGO_ELLIPSIZE_START; | |
84 | else if ( style & wxST_ELLIPSIZE_MIDDLE ) | |
85 | ellipsizeMode = PANGO_ELLIPSIZE_MIDDLE; | |
86 | else if ( style & wxST_ELLIPSIZE_END ) | |
87 | ellipsizeMode = PANGO_ELLIPSIZE_END; | |
88 | ||
89 | gtk_label_set_ellipsize( GTK_LABEL(m_widget), ellipsizeMode ); | |
90 | } | |
91 | #endif // __WXGTK26__ | |
92 | ||
6794ca46 VZ |
93 | // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2 |
94 | static const float labelAlignments[] = { 0.0, 1.0, 0.5 }; | |
95 | gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0); | |
96 | ||
97 | gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE ); | |
98 | ||
39bc0347 VZ |
99 | SetLabel(label); |
100 | ||
f03fc89f | 101 | m_parent->DoAddChild( this ); |
b58197f2 | 102 | |
abdeb9e7 | 103 | PostCreation(size); |
3d257b8d | 104 | |
7f85af82 | 105 | return true; |
3f659fd6 | 106 | } |
c801d85f | 107 | |
3da9cffc | 108 | void wxStaticText::GTKDoSetLabel(GTKLabelSetter setter, const wxString& label) |
c801d85f | 109 | { |
d44a9337 | 110 | wxCHECK_RET( m_widget != NULL, wxT("invalid static text") ); |
8ccac798 | 111 | |
7f85af82 PC |
112 | InvalidateBestSize(); |
113 | ||
9dc44eff | 114 | #ifndef __WXGTK3__ |
3da9cffc | 115 | if (gtk_check_version(2,6,0) && IsEllipsized()) |
39bc0347 | 116 | { |
3da9cffc VZ |
117 | // GTK+ < 2.6 does not support ellipsization so we need to do it |
118 | // manually and as our ellipsization code doesn't deal with markup, we | |
119 | // have no choice but to ignore it in this case and always use plain | |
120 | // text. | |
121 | GTKSetLabelForLabel(GTK_LABEL(m_widget), GetEllipsizedLabel()); | |
122 | } | |
123 | else // Ellipsization not needed or supported by GTK+. | |
9dc44eff | 124 | #endif |
3da9cffc VZ |
125 | { |
126 | (this->*setter)(GTK_LABEL(m_widget), label); | |
39bc0347 | 127 | } |
c0e6c051 RD |
128 | |
129 | // adjust the label size to the new label unless disabled | |
03647350 | 130 | if ( !HasFlag(wxST_NO_AUTORESIZE) && |
7517dcb5 | 131 | !IsEllipsized() ) // if ellipsization is ON, then we don't want to get resized! |
c0e6c051 | 132 | SetSize( GetBestSize() ); |
33720b2d RR |
133 | } |
134 | ||
3da9cffc VZ |
135 | void wxStaticText::SetLabel(const wxString& label) |
136 | { | |
137 | m_labelOrig = label; | |
138 | ||
139 | GTKDoSetLabel(&wxStaticText::GTKSetLabelForLabel, label); | |
140 | } | |
141 | ||
f5bdfc69 VZ |
142 | #if wxUSE_MARKUP |
143 | ||
3da9cffc VZ |
144 | bool wxStaticText::DoSetLabelMarkup(const wxString& markup) |
145 | { | |
146 | const wxString stripped = RemoveMarkup(markup); | |
147 | if ( stripped.empty() && !markup.empty() ) | |
148 | return false; | |
149 | ||
150 | m_labelOrig = stripped; | |
151 | ||
152 | GTKDoSetLabel(&wxStaticText::GTKSetLabelWithMarkupForLabel, markup); | |
153 | ||
154 | return true; | |
155 | } | |
156 | ||
f5bdfc69 VZ |
157 | #endif // wxUSE_MARKUP |
158 | ||
c0e6c051 RD |
159 | bool wxStaticText::SetFont( const wxFont &font ) |
160 | { | |
ac6e0eb1 | 161 | const bool wasUnderlined = GetFont().GetUnderlined(); |
c7a49742 | 162 | const bool wasStrickenThrough = GetFont().GetStrikethrough(); |
ac6e0eb1 | 163 | |
c0e6c051 RD |
164 | bool ret = wxControl::SetFont(font); |
165 | ||
c7a49742 VZ |
166 | const bool isUnderlined = GetFont().GetUnderlined(); |
167 | const bool isStrickenThrough = GetFont().GetStrikethrough(); | |
ac6e0eb1 | 168 | |
c7a49742 VZ |
169 | if ( (isUnderlined != wasUnderlined) || |
170 | (isStrickenThrough != wasStrickenThrough) ) | |
171 | { | |
172 | // We need to update the Pango attributes used for the text. | |
173 | if ( isUnderlined || isStrickenThrough ) | |
ac6e0eb1 | 174 | { |
c7a49742 VZ |
175 | PangoAttrList* const attrs = pango_attr_list_new(); |
176 | if ( isUnderlined ) | |
177 | { | |
178 | PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE); | |
179 | a->start_index = 0; | |
180 | a->end_index = (guint)-1; | |
181 | pango_attr_list_insert(attrs, a); | |
182 | } | |
183 | ||
184 | if ( isStrickenThrough ) | |
185 | { | |
186 | PangoAttribute *a = pango_attr_strikethrough_new( TRUE ); | |
187 | a->start_index = 0; | |
188 | a->end_index = (guint) -1; | |
189 | pango_attr_list_insert(attrs, a); | |
190 | } | |
191 | ||
ac6e0eb1 VZ |
192 | gtk_label_set_attributes(GTK_LABEL(m_widget), attrs); |
193 | pango_attr_list_unref(attrs); | |
194 | } | |
c7a49742 VZ |
195 | else // No special attributes any more. |
196 | { | |
197 | // Just remove any attributes we had set. | |
198 | gtk_label_set_attributes(GTK_LABEL(m_widget), NULL); | |
199 | } | |
200 | ||
201 | // The underlines for mnemonics are incompatible with using attributes | |
202 | // so turn them off when setting underlined font. | |
203 | gtk_label_set_use_underline(GTK_LABEL(m_widget), !isUnderlined); | |
ac6e0eb1 VZ |
204 | } |
205 | ||
c0e6c051 RD |
206 | // adjust the label size to the new label unless disabled |
207 | if (!HasFlag(wxST_NO_AUTORESIZE)) | |
208 | { | |
209 | SetSize( GetBestSize() ); | |
c0e6c051 RD |
210 | } |
211 | return ret; | |
212 | } | |
58614078 | 213 | |
a5040b80 | 214 | void wxStaticText::DoSetSize(int x, int y, |
7517dcb5 FM |
215 | int width, int height, |
216 | int sizeFlags ) | |
a5040b80 | 217 | { |
7f85af82 | 218 | wxStaticTextBase::DoSetSize(x, y, width, height, sizeFlags); |
39bc0347 | 219 | |
9dc44eff | 220 | #ifndef __WXGTK3__ |
39bc0347 VZ |
221 | if (gtk_check_version(2,6,0)) |
222 | { | |
223 | // GTK+ < 2.6 does not support ellipsization - we need to run our | |
224 | // generic code (actually it will be run only if IsEllipsized() == true) | |
225 | UpdateLabel(); | |
226 | } | |
9dc44eff | 227 | #endif |
a5040b80 RR |
228 | } |
229 | ||
33720b2d RR |
230 | wxSize wxStaticText::DoGetBestSize() const |
231 | { | |
232 | // Do not return any arbitrary default value... | |
233 | wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") ); | |
234 | ||
61053de4 VZ |
235 | // GetBestSize is supposed to return unwrapped size but calling |
236 | // gtk_label_set_line_wrap() from here is a bad idea as it queues another | |
237 | // size request by calling gtk_widget_queue_resize() and we end up in | |
238 | // infinite loop sometimes (notably when the control is in a toolbar) | |
9dc44eff PC |
239 | // With GTK3 however, there is no simple alternative, and the sizing loop |
240 | // no longer seems to occur. | |
241 | #ifdef __WXGTK3__ | |
242 | gtk_label_set_line_wrap(GTK_LABEL(m_widget), false); | |
243 | #else | |
61053de4 | 244 | GTK_LABEL(m_widget)->wrap = FALSE; |
9dc44eff | 245 | #endif |
7f85af82 | 246 | wxSize size = wxStaticTextBase::DoGetBestSize(); |
9dc44eff PC |
247 | #ifdef __WXGTK3__ |
248 | gtk_label_set_line_wrap(GTK_LABEL(m_widget), true); | |
249 | #else | |
61053de4 | 250 | GTK_LABEL(m_widget)->wrap = TRUE; // restore old value |
9dc44eff | 251 | #endif |
3d257b8d | 252 | |
8d1c5c3f | 253 | // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly |
7f85af82 PC |
254 | size.x++; |
255 | CacheBestSize(size); | |
256 | return size; | |
174b10af RR |
257 | } |
258 | ||
2e1f5012 VZ |
259 | bool wxStaticText::GTKWidgetNeedsMnemonic() const |
260 | { | |
261 | return true; | |
262 | } | |
263 | ||
264 | void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w) | |
265 | { | |
266 | gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w); | |
267 | } | |
268 | ||
39bc0347 VZ |
269 | |
270 | // These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel() | |
271 | ||
272 | wxString wxStaticText::DoGetLabel() const | |
273 | { | |
274 | GtkLabel *label = GTK_LABEL(m_widget); | |
275 | return wxGTK_CONV_BACK( gtk_label_get_text( label ) ); | |
276 | } | |
277 | ||
278 | void wxStaticText::DoSetLabel(const wxString& str) | |
279 | { | |
3da9cffc | 280 | GTKSetLabelForLabel(GTK_LABEL(m_widget), str); |
39bc0347 VZ |
281 | } |
282 | ||
9d522606 RD |
283 | // static |
284 | wxVisualAttributes | |
285 | wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
286 | { | |
287 | return GetDefaultAttributesFromGTKWidget(gtk_label_new); | |
288 | } | |
289 | ||
1e6feb95 | 290 | #endif // wxUSE_STATTEXT |