1 /////////////////////////////////////////////////////////////////////////////
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 IMPLEMENT_DYNAMIC_CLASS(wxStaticText
,wxControl
)
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
);
56 GtkJustification justify
;
57 if ( style
& wxALIGN_CENTER
)
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
);
74 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
75 static const float labelAlignments
[] = { 0.0, 1.0, 0.5 };
76 gtk_misc_set_alignment(GTK_MISC(m_widget
), labelAlignments
[justify
], 0.0);
78 gtk_label_set_line_wrap( GTK_LABEL(m_widget
), TRUE
);
81 if (!gtk_check_version(2,6,0))
84 PangoEllipsizeMode ellipsizeMode
= PANGO_ELLIPSIZE_NONE
;
85 if ( style
& wxST_ELLIPSIZE_START
)
86 ellipsizeMode
= PANGO_ELLIPSIZE_START
;
87 else if ( style
& wxST_ELLIPSIZE_MIDDLE
)
88 ellipsizeMode
= PANGO_ELLIPSIZE_MIDDLE
;
89 else if ( style
& wxST_ELLIPSIZE_END
)
90 ellipsizeMode
= PANGO_ELLIPSIZE_END
;
92 gtk_label_set_ellipsize( GTK_LABEL(m_widget
), ellipsizeMode
);
98 m_parent
->DoAddChild( this );
105 void wxStaticText::SetLabel( const wxString
& str
)
107 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid static text") );
109 // save the label inside m_labelOrig in case user calls GetLabel() later
112 InvalidateBestSize();
115 if (gtk_check_version(2,6,0) &&
118 // GTK+ < 2.6 does not support ellipsization:
119 // since we need to use our generic code for ellipsization (which does not
120 // behaves well in conjunction with markup; i.e. it may break the markup
121 // validity erasing portions of the string), we also need to strip out
122 // the markup (if present) from the label.
124 label
= GetEllipsizedLabelWithoutMarkup();
127 if ( HasFlag(wxST_MARKUP
) )
128 GTKSetLabelWithMarkupForLabel(GTK_LABEL(m_widget
), label
);
130 GTKSetLabelForLabel(GTK_LABEL(m_widget
), label
);
132 // adjust the label size to the new label unless disabled
133 if ( !HasFlag(wxST_NO_AUTORESIZE
) &&
134 !IsEllipsized() ) // if ellipsize is ON, then we don't want to get resized!
135 SetSize( GetBestSize() );
138 bool wxStaticText::SetFont( const wxFont
&font
)
140 const bool wasUnderlined
= GetFont().GetUnderlined();
142 bool ret
= wxControl::SetFont(font
);
144 if ( font
.GetUnderlined() != wasUnderlined
)
146 // the underlines for mnemonics are incompatible with using attributes
147 // so turn them off when setting underlined font and restore them when
149 gtk_label_set_use_underline(GTK_LABEL(m_widget
), wasUnderlined
);
153 // it's not underlined any more, remove the attributes we set
154 gtk_label_set_attributes(GTK_LABEL(m_widget
), NULL
);
156 else // the text is underlined now
158 PangoAttrList
*attrs
= pango_attr_list_new();
159 PangoAttribute
*a
= pango_attr_underline_new(PANGO_UNDERLINE_SINGLE
);
161 a
->end_index
= (guint
)-1;
162 pango_attr_list_insert(attrs
, a
);
163 gtk_label_set_attributes(GTK_LABEL(m_widget
), attrs
);
164 pango_attr_list_unref(attrs
);
168 // adjust the label size to the new label unless disabled
169 if (!HasFlag(wxST_NO_AUTORESIZE
))
171 SetSize( GetBestSize() );
176 void wxStaticText::DoSetSize(int x
, int y
,
177 int width
, int height
,
180 wxStaticTextBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
182 if (gtk_check_version(2,6,0))
184 // GTK+ < 2.6 does not support ellipsization - we need to run our
185 // generic code (actually it will be run only if IsEllipsized() == true)
190 wxSize
wxStaticText::DoGetBestSize() const
192 // Do not return any arbitrary default value...
193 wxASSERT_MSG( m_widget
, wxT("wxStaticText::DoGetBestSize called before creation") );
195 // GetBestSize is supposed to return unwrapped size but calling
196 // gtk_label_set_line_wrap() from here is a bad idea as it queues another
197 // size request by calling gtk_widget_queue_resize() and we end up in
198 // infinite loop sometimes (notably when the control is in a toolbar)
199 GTK_LABEL(m_widget
)->wrap
= FALSE
;
201 wxSize size
= wxStaticTextBase::DoGetBestSize();
203 GTK_LABEL(m_widget
)->wrap
= TRUE
; // restore old value
205 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
211 bool wxStaticText::GTKWidgetNeedsMnemonic() const
216 void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget
* w
)
218 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget
), w
);
222 // These functions should be used only when GTK+ < 2.6 by wxStaticTextBase::UpdateLabel()
224 wxString
wxStaticText::DoGetLabel() const
226 GtkLabel
*label
= GTK_LABEL(m_widget
);
227 return wxGTK_CONV_BACK( gtk_label_get_text( label
) );
230 void wxStaticText::DoSetLabel(const wxString
& str
)
232 GTKSetLabelForLabel(GTK_LABEL(m_widget
), str
);
237 wxStaticText::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
239 return GetDefaultAttributesFromGTKWidget(gtk_label_new
);
242 #endif // wxUSE_STATTEXT