]>
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" |
fab591c5 | 16 | #include "wx/gtk/private.h" |
c801d85f KB |
17 | |
18 | //----------------------------------------------------------------------------- | |
19 | // wxStaticText | |
20 | //----------------------------------------------------------------------------- | |
21 | ||
b58197f2 | 22 | wxStaticText::wxStaticText() |
c801d85f | 23 | { |
3f659fd6 | 24 | } |
c801d85f | 25 | |
b58197f2 VZ |
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) | |
c801d85f | 33 | { |
39bc0347 | 34 | Create( parent, id, label, pos, size, style, name ); |
3f659fd6 | 35 | } |
c801d85f | 36 | |
b58197f2 VZ |
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 ) | |
c801d85f | 44 | { |
4dcaf11a RR |
45 | if (!PreCreation( parent, pos, size ) || |
46 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
47 | { | |
8ccac798 | 48 | wxFAIL_MSG( wxT("wxStaticText creation failed") ); |
e8375af8 | 49 | return false; |
4dcaf11a | 50 | } |
b58197f2 | 51 | |
39bc0347 | 52 | m_widget = gtk_label_new(NULL); |
9ff9d30c | 53 | g_object_ref(m_widget); |
3d257b8d | 54 | |
a93109d5 | 55 | GtkJustification justify; |
88684daa | 56 | if ( style & wxALIGN_CENTER_HORIZONTAL ) |
a93109d5 RR |
57 | justify = GTK_JUSTIFY_CENTER; |
58 | else if ( style & wxALIGN_RIGHT ) | |
59 | justify = GTK_JUSTIFY_RIGHT; | |
7f85af82 | 60 | else |
a93109d5 | 61 | justify = GTK_JUSTIFY_LEFT; |
f4322df6 | 62 | |
69597639 | 63 | if (GetLayoutDirection() == wxLayout_RightToLeft) |
f4322df6 | 64 | { |
a1a48c8a | 65 | if (justify == GTK_JUSTIFY_RIGHT) |
69597639 | 66 | justify = GTK_JUSTIFY_LEFT; |
a1a48c8a | 67 | else if (justify == GTK_JUSTIFY_LEFT) |
69597639 RR |
68 | justify = GTK_JUSTIFY_RIGHT; |
69 | } | |
f4322df6 | 70 | |
a93109d5 | 71 | gtk_label_set_justify(GTK_LABEL(m_widget), justify); |
1a5594b8 | 72 | |
39bc0347 VZ |
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 | ||
6794ca46 VZ |
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 | ||
39bc0347 VZ |
95 | SetLabel(label); |
96 | ||
f03fc89f | 97 | m_parent->DoAddChild( this ); |
b58197f2 | 98 | |
abdeb9e7 | 99 | PostCreation(size); |
3d257b8d | 100 | |
7f85af82 | 101 | return true; |
3f659fd6 | 102 | } |
c801d85f | 103 | |
3da9cffc | 104 | void wxStaticText::GTKDoSetLabel(GTKLabelSetter setter, const wxString& label) |
c801d85f | 105 | { |
d44a9337 | 106 | wxCHECK_RET( m_widget != NULL, wxT("invalid static text") ); |
8ccac798 | 107 | |
7f85af82 PC |
108 | InvalidateBestSize(); |
109 | ||
3da9cffc | 110 | if (gtk_check_version(2,6,0) && IsEllipsized()) |
39bc0347 | 111 | { |
3da9cffc VZ |
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); | |
39bc0347 | 121 | } |
c0e6c051 RD |
122 | |
123 | // adjust the label size to the new label unless disabled | |
03647350 | 124 | if ( !HasFlag(wxST_NO_AUTORESIZE) && |
7517dcb5 | 125 | !IsEllipsized() ) // if ellipsization is ON, then we don't want to get resized! |
c0e6c051 | 126 | SetSize( GetBestSize() ); |
33720b2d RR |
127 | } |
128 | ||
3da9cffc VZ |
129 | void wxStaticText::SetLabel(const wxString& label) |
130 | { | |
131 | m_labelOrig = label; | |
132 | ||
133 | GTKDoSetLabel(&wxStaticText::GTKSetLabelForLabel, label); | |
134 | } | |
135 | ||
f5bdfc69 VZ |
136 | #if wxUSE_MARKUP |
137 | ||
3da9cffc VZ |
138 | bool wxStaticText::DoSetLabelMarkup(const wxString& markup) |
139 | { | |
140 | const wxString stripped = RemoveMarkup(markup); | |
141 | if ( stripped.empty() && !markup.empty() ) | |
142 | return false; | |
143 | ||
144 | m_labelOrig = stripped; | |
145 | ||
146 | GTKDoSetLabel(&wxStaticText::GTKSetLabelWithMarkupForLabel, markup); | |
147 | ||
148 | return true; | |
149 | } | |
150 | ||
f5bdfc69 VZ |
151 | #endif // wxUSE_MARKUP |
152 | ||
c0e6c051 RD |
153 | bool wxStaticText::SetFont( const wxFont &font ) |
154 | { | |
ac6e0eb1 | 155 | const bool wasUnderlined = GetFont().GetUnderlined(); |
c7a49742 | 156 | const bool wasStrickenThrough = GetFont().GetStrikethrough(); |
ac6e0eb1 | 157 | |
c0e6c051 RD |
158 | bool ret = wxControl::SetFont(font); |
159 | ||
c7a49742 VZ |
160 | const bool isUnderlined = GetFont().GetUnderlined(); |
161 | const bool isStrickenThrough = GetFont().GetStrikethrough(); | |
ac6e0eb1 | 162 | |
c7a49742 VZ |
163 | if ( (isUnderlined != wasUnderlined) || |
164 | (isStrickenThrough != wasStrickenThrough) ) | |
165 | { | |
166 | // We need to update the Pango attributes used for the text. | |
167 | if ( isUnderlined || isStrickenThrough ) | |
ac6e0eb1 | 168 | { |
c7a49742 VZ |
169 | PangoAttrList* const attrs = pango_attr_list_new(); |
170 | if ( isUnderlined ) | |
171 | { | |
172 | PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE); | |
173 | a->start_index = 0; | |
174 | a->end_index = (guint)-1; | |
175 | pango_attr_list_insert(attrs, a); | |
176 | } | |
177 | ||
178 | if ( isStrickenThrough ) | |
179 | { | |
180 | PangoAttribute *a = pango_attr_strikethrough_new( TRUE ); | |
181 | a->start_index = 0; | |
182 | a->end_index = (guint) -1; | |
183 | pango_attr_list_insert(attrs, a); | |
184 | } | |
185 | ||
ac6e0eb1 VZ |
186 | gtk_label_set_attributes(GTK_LABEL(m_widget), attrs); |
187 | pango_attr_list_unref(attrs); | |
188 | } | |
c7a49742 VZ |
189 | else // No special attributes any more. |
190 | { | |
191 | // Just remove any attributes we had set. | |
192 | gtk_label_set_attributes(GTK_LABEL(m_widget), NULL); | |
193 | } | |
194 | ||
195 | // The underlines for mnemonics are incompatible with using attributes | |
196 | // so turn them off when setting underlined font. | |
197 | gtk_label_set_use_underline(GTK_LABEL(m_widget), !isUnderlined); | |
ac6e0eb1 VZ |
198 | } |
199 | ||
c0e6c051 RD |
200 | // adjust the label size to the new label unless disabled |
201 | if (!HasFlag(wxST_NO_AUTORESIZE)) | |
202 | { | |
203 | SetSize( GetBestSize() ); | |
c0e6c051 RD |
204 | } |
205 | return ret; | |
206 | } | |
58614078 | 207 | |
a5040b80 | 208 | void wxStaticText::DoSetSize(int x, int y, |
7517dcb5 FM |
209 | int width, int height, |
210 | int sizeFlags ) | |
a5040b80 | 211 | { |
7f85af82 | 212 | wxStaticTextBase::DoSetSize(x, y, width, height, sizeFlags); |
39bc0347 VZ |
213 | |
214 | if (gtk_check_version(2,6,0)) | |
215 | { | |
216 | // GTK+ < 2.6 does not support ellipsization - we need to run our | |
217 | // generic code (actually it will be run only if IsEllipsized() == true) | |
218 | UpdateLabel(); | |
219 | } | |
a5040b80 RR |
220 | } |
221 | ||
33720b2d RR |
222 | wxSize wxStaticText::DoGetBestSize() const |
223 | { | |
224 | // Do not return any arbitrary default value... | |
225 | wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") ); | |
226 | ||
61053de4 VZ |
227 | // GetBestSize is supposed to return unwrapped size but calling |
228 | // gtk_label_set_line_wrap() from here is a bad idea as it queues another | |
229 | // size request by calling gtk_widget_queue_resize() and we end up in | |
230 | // infinite loop sometimes (notably when the control is in a toolbar) | |
231 | GTK_LABEL(m_widget)->wrap = FALSE; | |
3d257b8d | 232 | |
7f85af82 | 233 | wxSize size = wxStaticTextBase::DoGetBestSize(); |
33720b2d | 234 | |
61053de4 | 235 | GTK_LABEL(m_widget)->wrap = TRUE; // restore old value |
3d257b8d | 236 | |
8d1c5c3f | 237 | // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly |
7f85af82 PC |
238 | size.x++; |
239 | CacheBestSize(size); | |
240 | return size; | |
174b10af RR |
241 | } |
242 | ||
2e1f5012 VZ |
243 | bool wxStaticText::GTKWidgetNeedsMnemonic() const |
244 | { | |
245 | return true; | |
246 | } | |
247 | ||
248 | void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w) | |
249 | { | |
250 | gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w); | |
251 | } | |
252 | ||
39bc0347 VZ |
253 | |
254 | // These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel() | |
255 | ||
256 | wxString wxStaticText::DoGetLabel() const | |
257 | { | |
258 | GtkLabel *label = GTK_LABEL(m_widget); | |
259 | return wxGTK_CONV_BACK( gtk_label_get_text( label ) ); | |
260 | } | |
261 | ||
262 | void wxStaticText::DoSetLabel(const wxString& str) | |
263 | { | |
3da9cffc | 264 | GTKSetLabelForLabel(GTK_LABEL(m_widget), str); |
39bc0347 VZ |
265 | } |
266 | ||
9d522606 RD |
267 | // static |
268 | wxVisualAttributes | |
269 | wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
270 | { | |
271 | return GetDefaultAttributesFromGTKWidget(gtk_label_new); | |
272 | } | |
273 | ||
1e6feb95 | 274 | #endif // wxUSE_STATTEXT |