1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "stattext.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
19 #include "wx/stattext.h"
20 #include "wx/gtk/private.h"
26 void wxgtk_window_size_request_callback(GtkWidget
*widget
,
27 GtkRequisition
*requisition
,
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 IMPLEMENT_DYNAMIC_CLASS(wxStaticText
,wxControl
)
36 wxStaticText::wxStaticText()
40 wxStaticText::wxStaticText(wxWindow
*parent
,
42 const wxString
&label
,
48 Create( parent
, id
, label
, pos
, size
, style
, name
);
51 bool wxStaticText::Create(wxWindow
*parent
,
53 const wxString
&label
,
57 const wxString
&name
)
61 if (!PreCreation( parent
, pos
, size
) ||
62 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
64 wxFAIL_MSG( wxT("wxStaticText creation failed") );
68 // notice that we call the base class version which will just remove the
69 // '&' characters from the string, but not set the label's text to it
70 // because the label is not yet created and because SetLabel() has a side
71 // effect of changing the control size which might not be desirable
72 wxControl::SetLabel(label
);
73 m_widget
= gtk_label_new( wxGTK_CONV( m_label
) );
75 GtkJustification justify
;
76 if ( style
& wxALIGN_CENTER
)
77 justify
= GTK_JUSTIFY_CENTER
;
78 else if ( style
& wxALIGN_RIGHT
)
79 justify
= GTK_JUSTIFY_RIGHT
;
80 else // wxALIGN_LEFT is 0
81 justify
= GTK_JUSTIFY_LEFT
;
82 gtk_label_set_justify(GTK_LABEL(m_widget
), justify
);
84 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
85 static const float labelAlignments
[] = { 0.0, 1.0, 0.5 };
86 gtk_misc_set_alignment(GTK_MISC(m_widget
), labelAlignments
[justify
], 0.0);
88 // do not move this call elsewhere
89 gtk_label_set_line_wrap( GTK_LABEL(m_widget
), FALSE
);
91 m_parent
->DoAddChild( this );
95 // the bug below only happens with GTK 2
97 if ( justify
!= GTK_JUSTIFY_LEFT
)
99 // if we let GTK call wxgtk_window_size_request_callback the label
100 // always shrinks to its minimal size for some reason and so no
101 // alignment except the default left doesn't work (in fact it does,
102 // but you don't see it)
103 gtk_signal_disconnect_by_func
105 GTK_OBJECT(m_widget
),
106 GTK_SIGNAL_FUNC(wxgtk_window_size_request_callback
),
110 #endif // __WXGTK20__
115 wxString
wxStaticText::GetLabel() const
117 GtkLabel
*label
= GTK_LABEL(m_widget
);
120 wxString str
= wxGTK_CONV_BACK( gtk_label_get_text( label
) );
122 wxString str
= wxString( label
->label
);
125 return wxString(str
);
128 void wxStaticText::SetLabel( const wxString
&label
)
130 wxControl::SetLabel(label
);
133 // Build the colorized version of the label (markup only allowed
135 wxString colorlabel
= label
;
136 // If the color has been set, create a markup string to pass to
138 if (m_foregroundColour
.Ok())
140 colorlabel
.Printf(_T("<span foreground=\"#%02x%02x%02x\">%s</span>"),
141 m_foregroundColour
.Red(), m_foregroundColour
.Green(),
142 m_foregroundColour
.Blue(), label
.c_str());
145 gtk_label_set_markup( GTK_LABEL(m_widget
), wxGTK_CONV( colorlabel
) );
147 gtk_label_set( GTK_LABEL(m_widget
), wxGTK_CONV( m_label
) );
150 // adjust the label size to the new label unless disabled
151 if (!HasFlag(wxST_NO_AUTORESIZE
))
153 SetSize( GetBestSize() );
154 SetSizeHints(GetSize());
158 bool wxStaticText::SetFont( const wxFont
&font
)
160 bool ret
= wxControl::SetFont(font
);
162 // adjust the label size to the new label unless disabled
163 if (!HasFlag(wxST_NO_AUTORESIZE
))
165 SetSize( GetBestSize() );
166 SetSizeHints(GetSize());
171 wxSize
wxStaticText::DoGetBestSize() const
173 // Do not return any arbitrary default value...
174 wxASSERT_MSG( m_widget
, wxT("wxStaticText::DoGetBestSize called before creation") );
176 // this invalidates the size request
177 gtk_label_set_line_wrap( GTK_LABEL(m_widget
), TRUE
);
178 gtk_label_set_line_wrap( GTK_LABEL(m_widget
), FALSE
);
183 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
186 return wxSize(req
.width
, req
.height
);
189 bool wxStaticText::SetForegroundColour(const wxColour
& colour
)
191 // First, we call the base class member
192 wxControl::SetForegroundColour(colour
);
193 // Then, to force the color change, we set the label with the current label
194 SetLabel(GetLabel());
200 wxStaticText::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
202 return GetDefaultAttributesFromGTKWidget(gtk_label_new
);
205 #endif // wxUSE_STATTEXT