]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: stattext.cpp | |
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 | ||
22 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl) | |
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 VZ |
75 | #ifdef __WXGTK26__ |
76 | if (!gtk_check_version(2,6,0)) | |
77 | { | |
78 | // set ellipsize mode | |
79 | PangoEllipsizeMode ellipsizeMode = PANGO_ELLIPSIZE_NONE; | |
80 | if ( style & wxST_ELLIPSIZE_START ) | |
81 | ellipsizeMode = PANGO_ELLIPSIZE_START; | |
82 | else if ( style & wxST_ELLIPSIZE_MIDDLE ) | |
83 | ellipsizeMode = PANGO_ELLIPSIZE_MIDDLE; | |
84 | else if ( style & wxST_ELLIPSIZE_END ) | |
85 | ellipsizeMode = PANGO_ELLIPSIZE_END; | |
86 | ||
87 | gtk_label_set_ellipsize( GTK_LABEL(m_widget), ellipsizeMode ); | |
88 | } | |
89 | #endif // __WXGTK26__ | |
90 | ||
6794ca46 VZ |
91 | // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2 |
92 | static const float labelAlignments[] = { 0.0, 1.0, 0.5 }; | |
93 | gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0); | |
94 | ||
95 | gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE ); | |
96 | ||
39bc0347 VZ |
97 | SetLabel(label); |
98 | ||
f03fc89f | 99 | m_parent->DoAddChild( this ); |
b58197f2 | 100 | |
abdeb9e7 | 101 | PostCreation(size); |
3d257b8d | 102 | |
7f85af82 | 103 | return true; |
3f659fd6 | 104 | } |
c801d85f | 105 | |
39bc0347 | 106 | void wxStaticText::SetLabel( const wxString& str ) |
c801d85f | 107 | { |
d44a9337 | 108 | wxCHECK_RET( m_widget != NULL, wxT("invalid static text") ); |
8ccac798 | 109 | |
39bc0347 VZ |
110 | // save the label inside m_labelOrig in case user calls GetLabel() later |
111 | m_labelOrig = str; | |
112 | ||
7f85af82 PC |
113 | InvalidateBestSize(); |
114 | ||
39bc0347 VZ |
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); | |
c0e6c051 RD |
132 | |
133 | // adjust the label size to the new label unless disabled | |
03647350 | 134 | if ( !HasFlag(wxST_NO_AUTORESIZE) && |
7517dcb5 | 135 | !IsEllipsized() ) // if ellipsization is ON, then we don't want to get resized! |
c0e6c051 | 136 | SetSize( GetBestSize() ); |
33720b2d RR |
137 | } |
138 | ||
c0e6c051 RD |
139 | bool wxStaticText::SetFont( const wxFont &font ) |
140 | { | |
ac6e0eb1 VZ |
141 | const bool wasUnderlined = GetFont().GetUnderlined(); |
142 | ||
c0e6c051 RD |
143 | bool ret = wxControl::SetFont(font); |
144 | ||
ac6e0eb1 VZ |
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; | |
97a85898 | 162 | a->end_index = (guint)-1; |
ac6e0eb1 VZ |
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 | ||
c0e6c051 RD |
169 | // adjust the label size to the new label unless disabled |
170 | if (!HasFlag(wxST_NO_AUTORESIZE)) | |
171 | { | |
172 | SetSize( GetBestSize() ); | |
c0e6c051 RD |
173 | } |
174 | return ret; | |
175 | } | |
58614078 | 176 | |
a5040b80 | 177 | void wxStaticText::DoSetSize(int x, int y, |
7517dcb5 FM |
178 | int width, int height, |
179 | int sizeFlags ) | |
a5040b80 | 180 | { |
7f85af82 | 181 | wxStaticTextBase::DoSetSize(x, y, width, height, sizeFlags); |
39bc0347 VZ |
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 | } | |
a5040b80 RR |
189 | } |
190 | ||
33720b2d RR |
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 | ||
61053de4 VZ |
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; | |
3d257b8d | 201 | |
7f85af82 | 202 | wxSize size = wxStaticTextBase::DoGetBestSize(); |
33720b2d | 203 | |
61053de4 | 204 | GTK_LABEL(m_widget)->wrap = TRUE; // restore old value |
3d257b8d | 205 | |
8d1c5c3f | 206 | // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly |
7f85af82 PC |
207 | size.x++; |
208 | CacheBestSize(size); | |
209 | return size; | |
174b10af RR |
210 | } |
211 | ||
2e1f5012 VZ |
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 | ||
39bc0347 VZ |
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 | { | |
7517dcb5 FM |
233 | // this function looks like GTKSetLabelForLabel() but here we just want to modify |
234 | // the GTK control without altering any internal wxStaticText variable | |
ce00f59b | 235 | |
7517dcb5 FM |
236 | const wxString labelGTK = GTKConvertMnemonics(str); |
237 | gtk_label_set_text_with_mnemonic(GTK_LABEL(m_widget), wxGTK_CONV(labelGTK)); | |
39bc0347 VZ |
238 | } |
239 | ||
9d522606 RD |
240 | // static |
241 | wxVisualAttributes | |
242 | wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
243 | { | |
244 | return GetDefaultAttributesFromGTKWidget(gtk_label_new); | |
245 | } | |
246 | ||
1e6feb95 | 247 | #endif // wxUSE_STATTEXT |