1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/stattext.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/stattext.h"
17 #include "wx/gtk/private.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 wxStaticText::wxStaticText()
27 wxStaticText::wxStaticText(wxWindow
*parent
,
29 const wxString
&label
,
35 Create( parent
, id
, label
, pos
, size
, style
, name
);
38 bool wxStaticText::Create(wxWindow
*parent
,
40 const wxString
&label
,
44 const wxString
&name
)
46 if (!PreCreation( parent
, pos
, size
) ||
47 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
49 wxFAIL_MSG( wxT("wxStaticText creation failed") );
53 m_widget
= gtk_label_new(NULL
);
54 g_object_ref(m_widget
);
56 GtkJustification justify
;
57 if ( style
& wxALIGN_CENTER_HORIZONTAL
)
58 justify
= GTK_JUSTIFY_CENTER
;
59 else if ( style
& wxALIGN_RIGHT
)
60 justify
= GTK_JUSTIFY_RIGHT
;
62 justify
= GTK_JUSTIFY_LEFT
;
64 if (GetLayoutDirection() == wxLayout_RightToLeft
)
66 if (justify
== GTK_JUSTIFY_RIGHT
)
67 justify
= GTK_JUSTIFY_LEFT
;
68 else if (justify
== GTK_JUSTIFY_LEFT
)
69 justify
= GTK_JUSTIFY_RIGHT
;
72 gtk_label_set_justify(GTK_LABEL(m_widget
), justify
);
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
;
83 gtk_label_set_ellipsize( GTK_LABEL(m_widget
), ellipsizeMode
);
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);
89 gtk_label_set_line_wrap( GTK_LABEL(m_widget
), TRUE
);
93 m_parent
->DoAddChild( this );
100 void wxStaticText::GTKDoSetLabel(GTKLabelSetter setter
, const wxString
& label
)
102 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid static text") );
104 InvalidateBestSize();
106 (this->*setter
)(GTK_LABEL(m_widget
), label
);
108 // adjust the label size to the new label unless disabled
109 if ( !HasFlag(wxST_NO_AUTORESIZE
) &&
110 !IsEllipsized() ) // if ellipsization is ON, then we don't want to get resized!
111 SetSize( GetBestSize() );
114 void wxStaticText::SetLabel(const wxString
& label
)
118 GTKDoSetLabel(&wxStaticText::GTKSetLabelForLabel
, label
);
123 bool wxStaticText::DoSetLabelMarkup(const wxString
& markup
)
125 const wxString stripped
= RemoveMarkup(markup
);
126 if ( stripped
.empty() && !markup
.empty() )
129 m_labelOrig
= stripped
;
131 GTKDoSetLabel(&wxStaticText::GTKSetLabelWithMarkupForLabel
, markup
);
136 #endif // wxUSE_MARKUP
138 bool wxStaticText::SetFont( const wxFont
&font
)
140 const bool wasUnderlined
= GetFont().GetUnderlined();
141 const bool wasStrickenThrough
= GetFont().GetStrikethrough();
143 bool ret
= wxControl::SetFont(font
);
145 const bool isUnderlined
= GetFont().GetUnderlined();
146 const bool isStrickenThrough
= GetFont().GetStrikethrough();
148 if ( (isUnderlined
!= wasUnderlined
) ||
149 (isStrickenThrough
!= wasStrickenThrough
) )
151 // We need to update the Pango attributes used for the text.
152 if ( isUnderlined
|| isStrickenThrough
)
154 PangoAttrList
* const attrs
= pango_attr_list_new();
157 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
159 a
->end_index
= (guint
)-1;
160 pango_attr_list_insert(attrs
, a
);
163 if ( isStrickenThrough
)
165 PangoAttribute
*a
= pango_attr_strikethrough_new( TRUE
);
167 a
->end_index
= (guint
) -1;
168 pango_attr_list_insert(attrs
, a
);
171 gtk_label_set_attributes(GTK_LABEL(m_widget
), attrs
);
172 pango_attr_list_unref(attrs
);
174 else // No special attributes any more.
176 // Just remove any attributes we had set.
177 gtk_label_set_attributes(GTK_LABEL(m_widget
), NULL
);
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
);
185 // adjust the label size to the new label unless disabled
186 if (!HasFlag(wxST_NO_AUTORESIZE
))
188 SetSize( GetBestSize() );
193 wxSize
wxStaticText::DoGetBestSize() const
195 // Do not return any arbitrary default value...
196 wxASSERT_MSG( m_widget
, wxT("wxStaticText::DoGetBestSize called before creation") );
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)
202 // With GTK3 however, there is no simple alternative, and the sizing loop
203 // no longer seems to occur.
205 gtk_label_set_line_wrap(GTK_LABEL(m_widget
), false);
207 GTK_LABEL(m_widget
)->wrap
= FALSE
;
209 wxSize size
= wxStaticTextBase::DoGetBestSize();
211 gtk_label_set_line_wrap(GTK_LABEL(m_widget
), true);
213 GTK_LABEL(m_widget
)->wrap
= TRUE
; // restore old value
216 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
222 bool wxStaticText::GTKWidgetNeedsMnemonic() const
227 void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget
* w
)
229 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget
), w
);
233 // These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
235 wxString
wxStaticText::DoGetLabel() const
237 GtkLabel
*label
= GTK_LABEL(m_widget
);
238 return wxGTK_CONV_BACK( gtk_label_get_text( label
) );
241 void wxStaticText::DoSetLabel(const wxString
& str
)
243 GTKSetLabelForLabel(GTK_LABEL(m_widget
), str
);
248 wxStaticText::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
250 return GetDefaultAttributesFromGTKWidget(gtk_label_new(""));
253 #endif // wxUSE_STATTEXT