]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/stattext.cpp
Reverted last change
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "stattext.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #if wxUSE_STATTEXT
18
19 #include "wx/stattext.h"
20 #include "wx/gtk/private.h"
21
22 #include "gdk/gdk.h"
23 #include "gtk/gtk.h"
24
25 extern "C"
26 void wxgtk_window_size_request_callback(GtkWidget *widget,
27 GtkRequisition *requisition,
28 wxWindow *win);
29
30 //-----------------------------------------------------------------------------
31 // wxStaticText
32 //-----------------------------------------------------------------------------
33
34 IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl)
35
36 wxStaticText::wxStaticText()
37 {
38 }
39
40 wxStaticText::wxStaticText(wxWindow *parent,
41 wxWindowID id,
42 const wxString &label,
43 const wxPoint &pos,
44 const wxSize &size,
45 long style,
46 const wxString &name)
47 {
48 Create( parent, id, label, pos, size, style, name );
49 }
50
51 bool wxStaticText::Create(wxWindow *parent,
52 wxWindowID id,
53 const wxString &label,
54 const wxPoint &pos,
55 const wxSize &size,
56 long style,
57 const wxString &name )
58 {
59 m_needParent = TRUE;
60
61 if (!PreCreation( parent, pos, size ) ||
62 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
63 {
64 wxFAIL_MSG( wxT("wxXX creation failed") );
65 return FALSE;
66 }
67
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 ) );
74
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);
83
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);
87
88 // do not move this call elsewhere
89 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
90
91 m_parent->DoAddChild( this );
92
93 PostCreation();
94
95 // the bug below only happens with GTK 2
96 #ifdef __WXGTK20__
97 if ( justify != GTK_JUSTIFY_LEFT )
98 {
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
104 (
105 GTK_OBJECT(m_widget),
106 GTK_SIGNAL_FUNC(wxgtk_window_size_request_callback),
107 (gpointer) this
108 );
109 }
110 #endif // __WXGTK20__
111
112 ApplyWidgetStyle();
113
114 InheritAttributes();
115 // wxControl::SetFont( parent->GetFont() );
116
117 wxSize size_best( DoGetBestSize() );
118 wxSize new_size( size );
119 if (new_size.x == -1)
120 new_size.x = size_best.x;
121 if (new_size.y == -1)
122 new_size.y = size_best.y;
123 if ((new_size.x != size.x) || (new_size.y != size.y))
124 SetSize( new_size.x, new_size.y );
125
126 // if (ShouldInheritColours())
127 // {
128 // SetBackgroundColour( parent->GetBackgroundColour() );
129 // SetForegroundColour( parent->GetForegroundColour() );
130 // }
131 Show( TRUE );
132
133 return TRUE;
134 }
135
136 wxString wxStaticText::GetLabel() const
137 {
138 GtkLabel *label = GTK_LABEL(m_widget);
139
140 #ifdef __WXGTK20__
141 wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
142 #else
143 wxString str = wxString( label->label );
144 #endif
145
146 return wxString(str);
147 }
148
149 void wxStaticText::SetLabel( const wxString &label )
150 {
151 // Build the colorized version of the label
152 wxString colorlabel = label;
153 // If the color has been set, create a markup string to pass to the label setter
154 if (m_foregroundColour.Ok())
155 {
156 colorlabel.Printf(_T("<span foreground=\"#%02x%02x%02x\">%s</span>"), m_foregroundColour.Red(),
157 m_foregroundColour.Green(), m_foregroundColour.Blue(), label.c_str());
158 }
159
160 wxControl::SetLabel(label);
161
162 // markup only allowed under GTK2
163 #ifdef __WXGTK20__
164 gtk_label_set_markup( GTK_LABEL(m_widget), wxGTK_CONV( colorlabel ) );
165 #else
166 gtk_label_set( GTK_LABEL(m_widget), wxGTK_CONV( m_label ) );
167 #endif
168
169 // adjust the label size to the new label unless disabled
170 if (!HasFlag(wxST_NO_AUTORESIZE))
171 SetSize( GetBestSize() );
172 }
173
174 bool wxStaticText::SetFont( const wxFont &font )
175 {
176 bool ret = wxControl::SetFont(font);
177
178 // adjust the label size to the new label unless disabled
179 if (!HasFlag(wxST_NO_AUTORESIZE))
180 SetSize( GetBestSize() );
181
182 return ret;
183 }
184
185 void wxStaticText::ApplyWidgetStyle()
186 {
187 SetWidgetStyle();
188 gtk_widget_set_style( m_widget, m_widgetStyle );
189 }
190
191 wxSize wxStaticText::DoGetBestSize() const
192 {
193 // Do not return any arbitrary default value...
194 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
195
196 // this invalidates the size request
197 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
198 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
199
200 GtkRequisition req;
201 req.width = 2;
202 req.height = 2;
203 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
204 (m_widget, &req );
205
206 return wxSize(req.width, req.height);
207 }
208
209 bool wxStaticText::SetForegroundColour(const wxColour& colour)
210 {
211 // First, we call the base class member
212 wxControl::SetForegroundColour(colour);
213 // Then, to force the color change, we set the label with the current label
214 SetLabel(GetLabel());
215 return true;
216 }
217
218 #endif // wxUSE_STATTEXT