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"
22 void wxgtk_window_size_request_callback(GtkWidget
*widget
,
23 GtkRequisition
*requisition
,
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 IMPLEMENT_DYNAMIC_CLASS(wxStaticText
,wxControl
)
32 wxStaticText::wxStaticText()
36 wxStaticText::wxStaticText(wxWindow
*parent
,
38 const wxString
&label
,
44 Create( parent
, id
, label
, pos
, size
, style
, name
);
47 bool wxStaticText::Create(wxWindow
*parent
,
49 const wxString
&label
,
53 const wxString
&name
)
57 if (!PreCreation( parent
, pos
, size
) ||
58 !CreateBase( parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
60 wxFAIL_MSG( wxT("wxStaticText creation failed") );
64 const wxString labelGTK
= GTKConvertMnemonics(label
);
66 m_widget
= gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK
));
68 GtkJustification justify
;
69 if ( style
& wxALIGN_CENTER
)
70 justify
= GTK_JUSTIFY_CENTER
;
71 else if ( style
& wxALIGN_RIGHT
)
72 justify
= GTK_JUSTIFY_RIGHT
;
73 else // wxALIGN_LEFT is 0
74 justify
= GTK_JUSTIFY_LEFT
;
75 gtk_label_set_justify(GTK_LABEL(m_widget
), justify
);
77 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
78 static const float labelAlignments
[] = { 0.0, 1.0, 0.5 };
79 gtk_misc_set_alignment(GTK_MISC(m_widget
), labelAlignments
[justify
], 0.0);
81 gtk_label_set_line_wrap( GTK_LABEL(m_widget
), TRUE
);
83 m_parent
->DoAddChild( this );
87 // the bug below only happens with GTK 2
88 if ( justify
!= GTK_JUSTIFY_LEFT
)
90 // if we let GTK call wxgtk_window_size_request_callback the label
91 // always shrinks to its minimal size for some reason and so no
92 // alignment except the default left doesn't work (in fact it does,
93 // but you don't see it)
94 g_signal_handlers_disconnect_by_func (m_widget
,
95 (gpointer
) wxgtk_window_size_request_callback
,
102 wxString
wxStaticText::GetLabel() const
104 GtkLabel
*label
= GTK_LABEL(m_widget
);
105 wxString str
= wxGTK_CONV_BACK( gtk_label_get_text( label
) );
107 return wxString(str
);
110 void wxStaticText::SetLabel( const wxString
&label
)
112 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid static text") );
114 GTKSetLabelForLabel(GTK_LABEL(m_widget
), label
);
116 // adjust the label size to the new label unless disabled
117 if ( !HasFlag(wxST_NO_AUTORESIZE
) )
118 SetSize( GetBestSize() );
121 bool wxStaticText::SetFont( const wxFont
&font
)
123 bool ret
= wxControl::SetFont(font
);
125 // adjust the label size to the new label unless disabled
126 if (!HasFlag(wxST_NO_AUTORESIZE
))
128 InvalidateBestSize();
129 SetSize( GetBestSize() );
134 void wxStaticText::DoSetSize(int x
, int y
,
135 int width
, int height
,
138 wxControl::DoSetSize( x
, y
, width
, height
, sizeFlags
);
141 wxSize
wxStaticText::DoGetBestSize() const
143 // Do not return any arbitrary default value...
144 wxASSERT_MSG( m_widget
, wxT("wxStaticText::DoGetBestSize called before creation") );
146 // GetBestSize is supposed to return unwrapped size
147 gtk_label_set_line_wrap( GTK_LABEL(m_widget
), FALSE
);
152 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
)
155 gtk_label_set_line_wrap( GTK_LABEL(m_widget
), TRUE
);
157 // Adding 1 to width to workaround GTK sometimes wrapping the text needlessly
158 return wxSize (req
.width
+1, req
.height
);
161 bool wxStaticText::SetForegroundColour(const wxColour
& colour
)
163 // First, we call the base class member
164 wxControl::SetForegroundColour(colour
);
165 // Then, to force the color change, we set the label with the current label
166 SetLabel(GetLabel());
170 bool wxStaticText::GTKWidgetNeedsMnemonic() const
175 void wxStaticText::GTKWidgetDoSetMnemonic(GtkWidget
* w
)
177 gtk_label_set_mnemonic_widget(GTK_LABEL(m_widget
), w
);
182 wxStaticText::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
184 return GetDefaultAttributesFromGTKWidget(gtk_label_new
);
187 #endif // wxUSE_STATTEXT