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