]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/stattext.cpp
Added support for wxTEXT_ATTR_EFFECT_SMALL_CAPITALS.
[wxWidgets.git] / src / gtk / stattext.cpp
CommitLineData
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 24wxStaticText::wxStaticText()
c801d85f 25{
3f659fd6 26}
c801d85f 27
b58197f2
VZ
28wxStaticText::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
39bool 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
c49ba211
PC
75 // set ellipsize mode
76 PangoEllipsizeMode ellipsizeMode = PANGO_ELLIPSIZE_NONE;
77 if ( style & wxST_ELLIPSIZE_START )
78 ellipsizeMode = PANGO_ELLIPSIZE_START;
79 else if ( style & wxST_ELLIPSIZE_MIDDLE )
80 ellipsizeMode = PANGO_ELLIPSIZE_MIDDLE;
81 else if ( style & wxST_ELLIPSIZE_END )
82 ellipsizeMode = PANGO_ELLIPSIZE_END;
83
84 gtk_label_set_ellipsize( GTK_LABEL(m_widget), ellipsizeMode );
39bc0347 85
6794ca46
VZ
86 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
87 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
88 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
89
90 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
91
39bc0347
VZ
92 SetLabel(label);
93
f03fc89f 94 m_parent->DoAddChild( this );
b58197f2 95
abdeb9e7 96 PostCreation(size);
3d257b8d 97
7f85af82 98 return true;
3f659fd6 99}
c801d85f 100
3da9cffc 101void wxStaticText::GTKDoSetLabel(GTKLabelSetter setter, const wxString& label)
c801d85f 102{
d44a9337 103 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
8ccac798 104
7f85af82
PC
105 InvalidateBestSize();
106
c49ba211 107 (this->*setter)(GTK_LABEL(m_widget), label);
c0e6c051
RD
108
109 // adjust the label size to the new label unless disabled
03647350 110 if ( !HasFlag(wxST_NO_AUTORESIZE) &&
7517dcb5 111 !IsEllipsized() ) // if ellipsization is ON, then we don't want to get resized!
c0e6c051 112 SetSize( GetBestSize() );
33720b2d
RR
113}
114
3da9cffc
VZ
115void wxStaticText::SetLabel(const wxString& label)
116{
117 m_labelOrig = label;
118
119 GTKDoSetLabel(&wxStaticText::GTKSetLabelForLabel, label);
120}
121
f5bdfc69
VZ
122#if wxUSE_MARKUP
123
3da9cffc
VZ
124bool wxStaticText::DoSetLabelMarkup(const wxString& markup)
125{
126 const wxString stripped = RemoveMarkup(markup);
127 if ( stripped.empty() && !markup.empty() )
128 return false;
129
130 m_labelOrig = stripped;
131
132 GTKDoSetLabel(&wxStaticText::GTKSetLabelWithMarkupForLabel, markup);
133
134 return true;
135}
136
f5bdfc69
VZ
137#endif // wxUSE_MARKUP
138
c0e6c051
RD
139bool wxStaticText::SetFont( const wxFont &font )
140{
ac6e0eb1 141 const bool wasUnderlined = GetFont().GetUnderlined();
c7a49742 142 const bool wasStrickenThrough = GetFont().GetStrikethrough();
ac6e0eb1 143
c0e6c051
RD
144 bool ret = wxControl::SetFont(font);
145
c7a49742
VZ
146 const bool isUnderlined = GetFont().GetUnderlined();
147 const bool isStrickenThrough = GetFont().GetStrikethrough();
ac6e0eb1 148
c7a49742
VZ
149 if ( (isUnderlined != wasUnderlined) ||
150 (isStrickenThrough != wasStrickenThrough) )
151 {
152 // We need to update the Pango attributes used for the text.
153 if ( isUnderlined || isStrickenThrough )
ac6e0eb1 154 {
c7a49742
VZ
155 PangoAttrList* const attrs = pango_attr_list_new();
156 if ( isUnderlined )
157 {
158 PangoAttribute *a = pango_attr_underline_new(PANGO_UNDERLINE_SINGLE);
159 a->start_index = 0;
160 a->end_index = (guint)-1;
161 pango_attr_list_insert(attrs, a);
162 }
163
164 if ( isStrickenThrough )
165 {
166 PangoAttribute *a = pango_attr_strikethrough_new( TRUE );
167 a->start_index = 0;
168 a->end_index = (guint) -1;
169 pango_attr_list_insert(attrs, a);
170 }
171
ac6e0eb1
VZ
172 gtk_label_set_attributes(GTK_LABEL(m_widget), attrs);
173 pango_attr_list_unref(attrs);
174 }
c7a49742
VZ
175 else // No special attributes any more.
176 {
177 // Just remove any attributes we had set.
178 gtk_label_set_attributes(GTK_LABEL(m_widget), NULL);
179 }
180
181 // The underlines for mnemonics are incompatible with using attributes
182 // so turn them off when setting underlined font.
183 gtk_label_set_use_underline(GTK_LABEL(m_widget), !isUnderlined);
ac6e0eb1
VZ
184 }
185
c0e6c051
RD
186 // adjust the label size to the new label unless disabled
187 if (!HasFlag(wxST_NO_AUTORESIZE))
188 {
189 SetSize( GetBestSize() );
c0e6c051
RD
190 }
191 return ret;
192}
58614078 193
33720b2d
RR
194wxSize wxStaticText::DoGetBestSize() const
195{
196 // Do not return any arbitrary default value...
197 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
198
61053de4
VZ
199 // GetBestSize is supposed to return unwrapped size but calling
200 // gtk_label_set_line_wrap() from here is a bad idea as it queues another
201 // size request by calling gtk_widget_queue_resize() and we end up in
202 // infinite loop sometimes (notably when the control is in a toolbar)
9dc44eff
PC
203 // With GTK3 however, there is no simple alternative, and the sizing loop
204 // no longer seems to occur.
205#ifdef __WXGTK3__
206 gtk_label_set_line_wrap(GTK_LABEL(m_widget), false);
207#else
61053de4 208 GTK_LABEL(m_widget)->wrap = FALSE;
9dc44eff 209#endif
7f85af82 210 wxSize size = wxStaticTextBase::DoGetBestSize();
9dc44eff
PC
211#ifdef __WXGTK3__
212 gtk_label_set_line_wrap(GTK_LABEL(m_widget), true);
213#else
61053de4 214 GTK_LABEL(m_widget)->wrap = TRUE; // restore old value
9dc44eff 215#endif
3d257b8d 216
8d1c5c3f 217 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
7f85af82
PC
218 size.x++;
219 CacheBestSize(size);
220 return size;
174b10af
RR
221}
222
2e1f5012
VZ
223bool wxStaticText::GTKWidgetNeedsMnemonic() const
224{
225 return true;
226}
227
228void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget* w)
229{
230 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget), w);
231}
232
39bc0347
VZ
233
234// These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
235
236wxString wxStaticText::DoGetLabel() const
237{
238 GtkLabel *label = GTK_LABEL(m_widget);
239 return wxGTK_CONV_BACK( gtk_label_get_text( label ) );
240}
241
242void wxStaticText::DoSetLabel(const wxString& str)
243{
3da9cffc 244 GTKSetLabelForLabel(GTK_LABEL(m_widget), str);
39bc0347
VZ
245}
246
9d522606
RD
247// static
248wxVisualAttributes
249wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
250{
251 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
252}
253
1e6feb95 254#endif // wxUSE_STATTEXT