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