]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/stattext.cpp
GTK2: gtk_widget_set_uposition -> gtk_window_move for wxMiniFrame. No, I don't like...
[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
65 // not set the label's text to it because the label is no
66 // yet created and because SetLabel() has a side
67 // effect of changing the control size which might not be desirable
68 // wxContro::SetLabel no longer strips menu codes, so do it here.
69 wxString label1(wxStripMenuCodes(label));
70 wxControl::SetLabel(label);
71 m_widget = gtk_label_new( wxGTK_CONV( label1 ) );
72
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);
81
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);
85
86 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
87
88 m_parent->DoAddChild( this );
89
90 PostCreation(size);
91
92 // the bug below only happens with GTK 2
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)
99 g_signal_handlers_disconnect_by_func (m_widget,
100 (gpointer) wxgtk_window_size_request_callback,
101 this);
102 }
103
104 return TRUE;
105 }
106
107 wxString wxStaticText::GetLabel() const
108 {
109 GtkLabel *label = GTK_LABEL(m_widget);
110 wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) );
111
112 return wxString(str);
113 }
114
115 void wxStaticText::SetLabel( const wxString &label )
116 {
117 wxControl::SetLabel(label);
118 wxString label1(wxStripMenuCodes(label));
119
120 // Build the colorized version of the label (markup only allowed
121 // under GTK2):
122 // FIXME: Does this handle background correct? I recall bug reports - MR
123 if (m_foregroundColour.Ok())
124 {
125 // If the color has been set, create a markup string to pass to
126 // the label setter
127 wxString colorlabel;
128 colorlabel.Printf(_T("<span foreground=\"#%02x%02x%02x\">%s</span>"),
129 m_foregroundColour.Red(), m_foregroundColour.Green(),
130 m_foregroundColour.Blue(),
131 wxEscapeStringForPangoMarkup(label1).c_str());
132 gtk_label_set_markup( GTK_LABEL(m_widget), wxGTK_CONV( colorlabel ) );
133 }
134 else
135 gtk_label_set_text( GTK_LABEL(m_widget), wxGTK_CONV( label1 ) );
136
137 // adjust the label size to the new label unless disabled
138 if (!HasFlag(wxST_NO_AUTORESIZE))
139 {
140 InvalidateBestSize();
141 SetSize( GetBestSize() );
142 }
143 }
144
145 bool wxStaticText::SetFont( const wxFont &font )
146 {
147 bool ret = wxControl::SetFont(font);
148
149 // adjust the label size to the new label unless disabled
150 if (!HasFlag(wxST_NO_AUTORESIZE))
151 {
152 InvalidateBestSize();
153 SetSize( GetBestSize() );
154 }
155 return ret;
156 }
157
158 void wxStaticText::DoSetSize(int x, int y,
159 int width, int height,
160 int sizeFlags )
161 {
162 wxControl::DoSetSize( x, y, width, height, sizeFlags );
163 }
164
165 wxSize wxStaticText::DoGetBestSize() const
166 {
167 // Do not return any arbitrary default value...
168 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
169
170 // GetBestSize is supposed to return unwrapped size
171 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
172
173 GtkRequisition req;
174 req.width = -1;
175 req.height = -1;
176 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
177 (m_widget, &req );
178
179 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
180
181 return wxSize (req.width, req.height);
182 }
183
184 bool wxStaticText::SetForegroundColour(const wxColour& colour)
185 {
186 // First, we call the base class member
187 wxControl::SetForegroundColour(colour);
188 // Then, to force the color change, we set the label with the current label
189 SetLabel(GetLabel());
190 return true;
191 }
192
193 // static
194 wxVisualAttributes
195 wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
196 {
197 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
198 }
199
200 #endif // wxUSE_STATTEXT