]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/stattext.cpp
g_main_set_poll_func(x) -> g_main_context_set_poll_func(NULL, x). Former deprecated...
[wxWidgets.git] / src / gtk / stattext.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: stattext.cpp
3// Purpose:
4// Author: Robert Roebling
a81258be
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
1e6feb95
VZ
12
13#if wxUSE_STATTEXT
14
c801d85f 15#include "wx/stattext.h"
fab591c5 16#include "wx/gtk/private.h"
c801d85f 17
83624f79
RR
18#include "gdk/gdk.h"
19#include "gtk/gtk.h"
20
e1f448ee
VZ
21extern "C"
22void wxgtk_window_size_request_callback(GtkWidget *widget,
23 GtkRequisition *requisition,
24 wxWindow *win);
25
c801d85f
KB
26//-----------------------------------------------------------------------------
27// wxStaticText
28//-----------------------------------------------------------------------------
29
30IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl)
31
b58197f2 32wxStaticText::wxStaticText()
c801d85f 33{
3f659fd6 34}
c801d85f 35
b58197f2
VZ
36wxStaticText::wxStaticText(wxWindow *parent,
37 wxWindowID id,
38 const wxString &label,
39 const wxPoint &pos,
40 const wxSize &size,
41 long style,
42 const wxString &name)
c801d85f
KB
43{
44 Create( parent, id, label, pos, size, style, name );
3f659fd6 45}
c801d85f 46
b58197f2
VZ
47bool wxStaticText::Create(wxWindow *parent,
48 wxWindowID id,
49 const wxString &label,
50 const wxPoint &pos,
51 const wxSize &size,
52 long style,
53 const wxString &name )
c801d85f 54{
a93109d5 55 m_needParent = TRUE;
b58197f2 56
4dcaf11a
RR
57 if (!PreCreation( parent, pos, size ) ||
58 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
59 {
8ccac798 60 wxFAIL_MSG( wxT("wxStaticText creation failed") );
185fa6bf 61 return FALSE;
4dcaf11a 62 }
b58197f2 63
bd1a4a99
JS
64 // notice that we call the base class version which will
65 // not set the label's text to it because the label is no
66 // yet created and because SetLabel() has a side
b58197f2 67 // effect of changing the control size which might not be desirable
bd1a4a99
JS
68 // wxContro::SetLabel no longer strips menu codes, so do it here.
69 wxString label1(wxStripMenuCodes(label));
a93109d5 70 wxControl::SetLabel(label);
bd1a4a99 71 m_widget = gtk_label_new( wxGTK_CONV( label1 ) );
3d257b8d 72
a93109d5
RR
73 GtkJustification justify;
74 if ( style & wxALIGN_CENTER )
75 justify = GTK_JUSTIFY_CENTER;
76 else if ( style & wxALIGN_RIGHT )
77 justify = GTK_JUSTIFY_RIGHT;
78 else // wxALIGN_LEFT is 0
79 justify = GTK_JUSTIFY_LEFT;
80 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
1a5594b8 81
1a5594b8
VZ
82 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
83 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
84 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
185fa6bf 85
a5040b80 86 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
33720b2d 87
f03fc89f 88 m_parent->DoAddChild( this );
b58197f2 89
abdeb9e7 90 PostCreation(size);
3d257b8d 91
e1f448ee 92 // the bug below only happens with GTK 2
e1f448ee
VZ
93 if ( justify != GTK_JUSTIFY_LEFT )
94 {
95 // if we let GTK call wxgtk_window_size_request_callback the label
96 // always shrinks to its minimal size for some reason and so no
97 // alignment except the default left doesn't work (in fact it does,
98 // but you don't see it)
9fa72bd2
MR
99 g_signal_handlers_disconnect_by_func (m_widget,
100 (gpointer) wxgtk_window_size_request_callback,
101 this);
e1f448ee 102 }
e1f448ee 103
a93109d5 104 return TRUE;
3f659fd6 105}
c801d85f 106
ed58dbea 107wxString wxStaticText::GetLabel() const
c801d85f 108{
3b2e67f6 109 GtkLabel *label = GTK_LABEL(m_widget);
3b2e67f6 110 wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
b58197f2
VZ
111
112 return wxString(str);
3f659fd6 113}
c801d85f
KB
114
115void wxStaticText::SetLabel( const wxString &label )
116{
8ccac798 117 wxControl::SetLabel(label);
bd1a4a99 118 wxString label1(wxStripMenuCodes(label));
8ccac798 119
8ccac798
VS
120 // Build the colorized version of the label (markup only allowed
121 // under GTK2):
174b10af
RR
122 if (m_foregroundColour.Ok())
123 {
66bd83b4
VS
124 // If the color has been set, create a markup string to pass to
125 // the label setter
126 wxString colorlabel;
8ccac798
VS
127 colorlabel.Printf(_T("<span foreground=\"#%02x%02x%02x\">%s</span>"),
128 m_foregroundColour.Red(), m_foregroundColour.Green(),
66bd83b4 129 m_foregroundColour.Blue(),
bd1a4a99 130 wxEscapeStringForPangoMarkup(label1).c_str());
66bd83b4 131 gtk_label_set_markup( GTK_LABEL(m_widget), wxGTK_CONV( colorlabel ) );
174b10af 132 }
66bd83b4 133 else
bd1a4a99 134 gtk_label_set( GTK_LABEL(m_widget), wxGTK_CONV( label1 ) );
c0e6c051
RD
135
136 // adjust the label size to the new label unless disabled
137 if (!HasFlag(wxST_NO_AUTORESIZE))
138 {
9f884528 139 InvalidateBestSize();
c0e6c051 140 SetSize( GetBestSize() );
c0e6c051 141 }
33720b2d
RR
142}
143
c0e6c051
RD
144bool wxStaticText::SetFont( const wxFont &font )
145{
146 bool ret = wxControl::SetFont(font);
147
148 // adjust the label size to the new label unless disabled
149 if (!HasFlag(wxST_NO_AUTORESIZE))
150 {
9f884528 151 InvalidateBestSize();
c0e6c051 152 SetSize( GetBestSize() );
c0e6c051
RD
153 }
154 return ret;
155}
58614078 156
a5040b80
RR
157void wxStaticText::DoSetSize(int x, int y,
158 int width, int height,
159 int sizeFlags )
160{
161 wxControl::DoSetSize( x, y, width, height, sizeFlags );
162}
163
33720b2d
RR
164wxSize wxStaticText::DoGetBestSize() const
165{
166 // Do not return any arbitrary default value...
167 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
168
088ddc4e
RR
169 // GetBestSize is supposed to return unwrapped size
170 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
3d257b8d 171
33720b2d 172 GtkRequisition req;
a5040b80
RR
173 req.width = -1;
174 req.height = -1;
2afa14f2 175 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
33720b2d
RR
176 (m_widget, &req );
177
088ddc4e 178 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
3d257b8d 179
a5040b80 180 return wxSize (req.width, req.height);
33720b2d
RR
181}
182
174b10af
RR
183bool wxStaticText::SetForegroundColour(const wxColour& colour)
184{
185 // First, we call the base class member
186 wxControl::SetForegroundColour(colour);
187 // Then, to force the color change, we set the label with the current label
188 SetLabel(GetLabel());
189 return true;
190}
191
9d522606
RD
192// static
193wxVisualAttributes
194wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
195{
196 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
197}
198
1e6feb95 199#endif // wxUSE_STATTEXT