]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/stattext.cpp
Apply Mart R.'s patch for improved font width
[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("wxStaticText 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 if (size.x == -1)
89 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
90 else
91 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
92
93 m_parent->DoAddChild( this );
94
95 PostCreation(size);
96
97 // the bug below only happens with GTK 2
98 #ifdef __WXGTK20__
99 if ( justify != GTK_JUSTIFY_LEFT )
100 {
101 // if we let GTK call wxgtk_window_size_request_callback the label
102 // always shrinks to its minimal size for some reason and so no
103 // alignment except the default left doesn't work (in fact it does,
104 // but you don't see it)
105 gtk_signal_disconnect_by_func
106 (
107 GTK_OBJECT(m_widget),
108 GTK_SIGNAL_FUNC(wxgtk_window_size_request_callback),
109 (gpointer) this
110 );
111 }
112 #endif // __WXGTK20__
113
114 return TRUE;
115 }
116
117 wxString wxStaticText::GetLabel() const
118 {
119 GtkLabel *label = GTK_LABEL(m_widget);
120
121 #ifdef __WXGTK20__
122 wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
123 #else
124 wxString str = wxString( label->label );
125 #endif
126
127 return wxString(str);
128 }
129
130 void wxStaticText::SetLabel( const wxString &label )
131 {
132 wxControl::SetLabel(label);
133
134 #ifdef __WXGTK20__
135 // Build the colorized version of the label (markup only allowed
136 // under GTK2):
137 if (m_foregroundColour.Ok())
138 {
139 // If the color has been set, create a markup string to pass to
140 // the label setter
141 wxString colorlabel;
142 colorlabel.Printf(_T("<span foreground=\"#%02x%02x%02x\">%s</span>"),
143 m_foregroundColour.Red(), m_foregroundColour.Green(),
144 m_foregroundColour.Blue(),
145 wxEscapeStringForPangoMarkup(label).c_str());
146 gtk_label_set_markup( GTK_LABEL(m_widget), wxGTK_CONV( colorlabel ) );
147 }
148 else
149 #endif
150 gtk_label_set( GTK_LABEL(m_widget), wxGTK_CONV( m_label ) );
151
152 // adjust the label size to the new label unless disabled
153 if (!HasFlag(wxST_NO_AUTORESIZE))
154 {
155 InvalidateBestSize();
156 SetSize( GetBestSize() );
157 }
158 }
159
160 bool wxStaticText::SetFont( const wxFont &font )
161 {
162 bool ret = wxControl::SetFont(font);
163
164 // adjust the label size to the new label unless disabled
165 if (!HasFlag(wxST_NO_AUTORESIZE))
166 {
167 InvalidateBestSize();
168 SetSize( GetBestSize() );
169 }
170 return ret;
171 }
172
173 void wxStaticText::DoSetSize(int x, int y,
174 int width, int height,
175 int sizeFlags )
176 {
177 wxControl::DoSetSize( x, y, width, height, sizeFlags );
178 }
179
180 wxSize wxStaticText::DoGetBestSize() const
181 {
182 // Do not return any arbitrary default value...
183 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
184
185 int width = m_width;
186 #ifndef __WXGTK20__
187 gtk_label_set_pattern( GTK_LABEL(m_widget), NULL );
188 if (width < 3) width = -1;
189 #endif
190 gtk_widget_set_usize( m_widget, width, -1 );
191
192 GtkRequisition req;
193 req.width = -1;
194 req.height = -1;
195 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
196 (m_widget, &req );
197
198 return wxSize (req.width, req.height);
199 }
200
201 bool wxStaticText::SetForegroundColour(const wxColour& colour)
202 {
203 // First, we call the base class member
204 wxControl::SetForegroundColour(colour);
205 // Then, to force the color change, we set the label with the current label
206 SetLabel(GetLabel());
207 return true;
208 }
209
210 // static
211 wxVisualAttributes
212 wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
213 {
214 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
215 }
216
217 #endif // wxUSE_STATTEXT