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"
18 #include "wx/gtk/private.h"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 wxStaticText::wxStaticText()
28 wxStaticText::wxStaticText(wxWindow
*parent
,
30 const wxString
&label
,
36 Create( parent
, id
, label
, pos
, size
, style
, name
);
39 bool wxStaticText::Create(wxWindow
*parent
,
41 const wxString
&label
,
45 const wxString
&name
)
47 if (!PreCreation( parent
, pos
, size
) ||
48 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
50 wxFAIL_MSG( wxT("wxStaticText creation failed") );
54 m_widget
= gtk_label_new(NULL
);
55 g_object_ref(m_widget
);
57 GtkJustification justify
;
58 if ( style
& wxALIGN_CENTER_HORIZONTAL
)
59 justify
= GTK_JUSTIFY_CENTER
;
60 else if ( style
& wxALIGN_RIGHT
)
61 justify
= GTK_JUSTIFY_RIGHT
;
63 justify
= GTK_JUSTIFY_LEFT
;
65 if (GetLayoutDirection() == wxLayout_RightToLeft
)
67 if (justify
== GTK_JUSTIFY_RIGHT
)
68 justify
= GTK_JUSTIFY_LEFT
;
69 else if (justify
== GTK_JUSTIFY_LEFT
)
70 justify
= GTK_JUSTIFY_RIGHT
;
73 gtk_label_set_justify(GTK_LABEL(m_widget
), justify
);
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
);
88 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
89 static const float labelAlignments
[] = { 0.0, 1.0, 0.5 };
90 gtk_misc_set_alignment(GTK_MISC(m_widget
), labelAlignments
[justify
], 0.0);
92 gtk_label_set_line_wrap( GTK_LABEL(m_widget
), TRUE
);
96 m_parent
->DoAddChild( this );
103 void wxStaticText::GTKDoSetLabel(GTKLabelSetter setter
, const wxString
& label
)
105 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid static text") );
107 InvalidateBestSize();
110 (this->*setter
)(GTK_LABEL(m_widget
), label
);
113 // adjust the label size to the new label unless disabled
114 if ( !HasFlag(wxST_NO_AUTORESIZE
) &&
115 !IsEllipsized() ) // if ellipsization is ON, then we don't want to get resized!
116 SetSize( GetBestSize() );
119 void wxStaticText::SetLabel(const wxString
& label
)
123 GTKDoSetLabel(&wxStaticText::GTKSetLabelForLabel
, label
);
128 bool wxStaticText::DoSetLabelMarkup(const wxString
& markup
)
130 const wxString stripped
= RemoveMarkup(markup
);
131 if ( stripped
.empty() && !markup
.empty() )
134 m_labelOrig
= stripped
;
136 GTKDoSetLabel(&wxStaticText::GTKSetLabelWithMarkupForLabel
, markup
);
141 #endif // wxUSE_MARKUP
143 bool wxStaticText::SetFont( const wxFont
&font
)
145 const bool wasUnderlined
= GetFont().GetUnderlined();
146 const bool wasStrickenThrough
= GetFont().GetStrikethrough();
148 bool ret
= wxControl::SetFont(font
);
150 const bool isUnderlined
= GetFont().GetUnderlined();
151 const bool isStrickenThrough
= GetFont().GetStrikethrough();
153 if ( (isUnderlined
!= wasUnderlined
) ||
154 (isStrickenThrough
!= wasStrickenThrough
) )
156 // We need to update the Pango attributes used for the text.
157 if ( isUnderlined
|| isStrickenThrough
)
159 PangoAttrList
* const attrs
= pango_attr_list_new();
162 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
164 a
->end_index
= (guint
)-1;
165 pango_attr_list_insert(attrs
, a
);
168 if ( isStrickenThrough
)
170 PangoAttribute
*a
= pango_attr_strikethrough_new( TRUE
);
172 a
->end_index
= (guint
) -1;
173 pango_attr_list_insert(attrs
, a
);
176 gtk_label_set_attributes(GTK_LABEL(m_widget
), attrs
);
177 pango_attr_list_unref(attrs
);
179 else // No special attributes any more.
181 // Just remove any attributes we had set.
182 gtk_label_set_attributes(GTK_LABEL(m_widget
), NULL
);
185 // The underlines for mnemonics are incompatible with using attributes
186 // so turn them off when setting underlined font.
187 gtk_label_set_use_underline(GTK_LABEL(m_widget
), !isUnderlined
);
190 // adjust the label size to the new label unless disabled
191 if (!HasFlag(wxST_NO_AUTORESIZE
))
193 SetSize( GetBestSize() );
198 void wxStaticText::DoSetSize(int x
, int y
,
199 int width
, int height
,
202 wxStaticTextBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
205 wxSize
wxStaticText::DoGetBestSize() const
207 // Do not return any arbitrary default value...
208 wxASSERT_MSG( m_widget
, wxT("wxStaticText::DoGetBestSize called before creation") );
210 // GetBestSize is supposed to return unwrapped size but calling
211 // gtk_label_set_line_wrap() from here is a bad idea as it queues another
212 // size request by calling gtk_widget_queue_resize() and we end up in
213 // infinite loop sometimes (notably when the control is in a toolbar)
214 // With GTK3 however, there is no simple alternative, and the sizing loop
215 // no longer seems to occur.
217 gtk_label_set_line_wrap(GTK_LABEL(m_widget
), false);
219 GTK_LABEL(m_widget
)->wrap
= FALSE
;
221 wxSize size
= wxStaticTextBase::DoGetBestSize();
223 gtk_label_set_line_wrap(GTK_LABEL(m_widget
), true);
225 GTK_LABEL(m_widget
)->wrap
= TRUE
; // restore old value
228 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
234 bool wxStaticText::GTKWidgetNeedsMnemonic() const
239 void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget
* w
)
241 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget
), w
);
245 // These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
247 wxString
wxStaticText::DoGetLabel() const
249 GtkLabel
*label
= GTK_LABEL(m_widget
);
250 return wxGTK_CONV_BACK( gtk_label_get_text( label
) );
253 void wxStaticText::DoSetLabel(const wxString
& str
)
255 GTKSetLabelForLabel(GTK_LABEL(m_widget
), str
);
260 wxStaticText::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
262 return GetDefaultAttributesFromGTKWidget(gtk_label_new
);
265 #endif // wxUSE_STATTEXT