1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/stattext.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/stattext.h"
16 #include "wx/gtk/private.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 wxStaticText::wxStaticText()
26 wxStaticText::wxStaticText(wxWindow
*parent
,
28 const wxString
&label
,
34 Create( parent
, id
, label
, pos
, size
, style
, name
);
37 bool wxStaticText::Create(wxWindow
*parent
,
39 const wxString
&label
,
43 const wxString
&name
)
45 if (!PreCreation( parent
, pos
, size
) ||
46 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
48 wxFAIL_MSG( wxT("wxStaticText creation failed") );
52 m_widget
= gtk_label_new(NULL
);
53 g_object_ref(m_widget
);
55 GtkJustification justify
;
56 if ( style
& wxALIGN_CENTER_HORIZONTAL
)
57 justify
= GTK_JUSTIFY_CENTER
;
58 else if ( style
& wxALIGN_RIGHT
)
59 justify
= GTK_JUSTIFY_RIGHT
;
61 justify
= GTK_JUSTIFY_LEFT
;
63 if (GetLayoutDirection() == wxLayout_RightToLeft
)
65 if (justify
== GTK_JUSTIFY_RIGHT
)
66 justify
= GTK_JUSTIFY_LEFT
;
67 else if (justify
== GTK_JUSTIFY_LEFT
)
68 justify
= GTK_JUSTIFY_RIGHT
;
71 gtk_label_set_justify(GTK_LABEL(m_widget
), justify
);
74 if (!gtk_check_version(2,6,0))
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
;
85 gtk_label_set_ellipsize( GTK_LABEL(m_widget
), ellipsizeMode
);
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);
93 gtk_label_set_line_wrap( GTK_LABEL(m_widget
), TRUE
);
97 m_parent
->DoAddChild( this );
104 void wxStaticText::GTKDoSetLabel(GTKLabelSetter setter
, const wxString
& label
)
106 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid static text") );
108 InvalidateBestSize();
110 if (gtk_check_version(2,6,0) && IsEllipsized())
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
116 GTKSetLabelForLabel(GTK_LABEL(m_widget
), GetEllipsizedLabel());
118 else // Ellipsization not needed or supported by GTK+.
120 (this->*setter
)(GTK_LABEL(m_widget
), label
);
123 // adjust the label size to the new label unless disabled
124 if ( !HasFlag(wxST_NO_AUTORESIZE
) &&
125 !IsEllipsized() ) // if ellipsization is ON, then we don't want to get resized!
126 SetSize( GetBestSize() );
129 void wxStaticText::SetLabel(const wxString
& label
)
133 GTKDoSetLabel(&wxStaticText::GTKSetLabelForLabel
, label
);
138 bool wxStaticText::DoSetLabelMarkup(const wxString
& markup
)
140 const wxString stripped
= RemoveMarkup(markup
);
141 if ( stripped
.empty() && !markup
.empty() )
144 m_labelOrig
= stripped
;
146 GTKDoSetLabel(&wxStaticText::GTKSetLabelWithMarkupForLabel
, markup
);
151 #endif // wxUSE_MARKUP
153 bool wxStaticText::SetFont( const wxFont
&font
)
155 const bool wasUnderlined
= GetFont().GetUnderlined();
156 const bool wasStrickenThrough
= GetFont().GetStrikethrough();
158 bool ret
= wxControl::SetFont(font
);
160 const bool isUnderlined
= GetFont().GetUnderlined();
161 const bool isStrickenThrough
= GetFont().GetStrikethrough();
163 if ( (isUnderlined
!= wasUnderlined
) ||
164 (isStrickenThrough
!= wasStrickenThrough
) )
166 // We need to update the Pango attributes used for the text.
167 if ( isUnderlined
|| isStrickenThrough
)
169 PangoAttrList
* const attrs
= pango_attr_list_new();
172 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
174 a
->end_index
= (guint
)-1;
175 pango_attr_list_insert(attrs
, a
);
178 if ( isStrickenThrough
)
180 PangoAttribute
*a
= pango_attr_strikethrough_new( TRUE
);
182 a
->end_index
= (guint
) -1;
183 pango_attr_list_insert(attrs
, a
);
186 gtk_label_set_attributes(GTK_LABEL(m_widget
), attrs
);
187 pango_attr_list_unref(attrs
);
189 else // No special attributes any more.
191 // Just remove any attributes we had set.
192 gtk_label_set_attributes(GTK_LABEL(m_widget
), NULL
);
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
);
200 // adjust the label size to the new label unless disabled
201 if (!HasFlag(wxST_NO_AUTORESIZE
))
203 SetSize( GetBestSize() );
208 void wxStaticText::DoSetSize(int x
, int y
,
209 int width
, int height
,
212 wxStaticTextBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
214 if (gtk_check_version(2,6,0))
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)
222 wxSize
wxStaticText::DoGetBestSize() const
224 // Do not return any arbitrary default value...
225 wxASSERT_MSG( m_widget
, wxT("wxStaticText::DoGetBestSize called before creation") );
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
;
233 wxSize size
= wxStaticTextBase::DoGetBestSize();
235 GTK_LABEL(m_widget
)->wrap
= TRUE
; // restore old value
237 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
243 bool wxStaticText::GTKWidgetNeedsMnemonic() const
248 void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget
* w
)
250 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget
), w
);
254 // These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
256 wxString
wxStaticText::DoGetLabel() const
258 GtkLabel
*label
= GTK_LABEL(m_widget
);
259 return wxGTK_CONV_BACK( gtk_label_get_text( label
) );
262 void wxStaticText::DoSetLabel(const wxString
& str
)
264 GTKSetLabelForLabel(GTK_LABEL(m_widget
), str
);
269 wxStaticText::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
271 return GetDefaultAttributesFromGTKWidget(gtk_label_new
);
274 #endif // wxUSE_STATTEXT