]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/stattext.cpp
Ensure there is valid context for DrawRectangle
[wxWidgets.git] / src / gtk1 / stattext.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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/gtk1/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 wxStaticText::wxStaticText()
31 {
32 }
33
34 wxStaticText::wxStaticText(wxWindow *parent,
35 wxWindowID id,
36 const wxString &label,
37 const wxPoint &pos,
38 const wxSize &size,
39 long style,
40 const wxString &name)
41 {
42 Create( parent, id, label, pos, size, style, name );
43 }
44
45 bool wxStaticText::Create(wxWindow *parent,
46 wxWindowID id,
47 const wxString &label,
48 const wxPoint &pos,
49 const wxSize &size,
50 long style,
51 const wxString &name )
52 {
53 m_needParent = TRUE;
54
55 if (!PreCreation( parent, pos, size ) ||
56 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
57 {
58 wxFAIL_MSG( wxT("wxStaticText creation failed") );
59 return FALSE;
60 }
61
62 m_label = label;
63 m_widget = gtk_label_new( wxGTK_CONV( GTKRemoveMnemonics(label)) );
64
65 GtkJustification justify;
66 if ( style & wxALIGN_CENTER )
67 justify = GTK_JUSTIFY_CENTER;
68 else if ( style & wxALIGN_RIGHT )
69 justify = GTK_JUSTIFY_RIGHT;
70 else // wxALIGN_LEFT is 0
71 justify = GTK_JUSTIFY_LEFT;
72 gtk_label_set_justify(GTK_LABEL(m_widget), justify);
73
74 // GTK_JUSTIFY_LEFT is 0, RIGHT 1 and CENTER 2
75 static const float labelAlignments[] = { 0.0, 1.0, 0.5 };
76 gtk_misc_set_alignment(GTK_MISC(m_widget), labelAlignments[justify], 0.0);
77
78 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
79
80 m_parent->DoAddChild( this );
81
82 PostCreation(size);
83
84 return TRUE;
85 }
86
87 wxString wxStaticText::GetLabel() const
88 {
89 GtkLabel *label = GTK_LABEL(m_widget);
90 wxString str = wxString( label->label );
91 return wxString(str);
92 }
93
94 void wxStaticText::SetLabel( const wxString &label )
95 {
96 wxCHECK_RET( m_widget != NULL, wxT("invalid static text") );
97
98 GTKSetLabelForLabel(GTK_LABEL(m_widget), label);
99
100 // adjust the label size to the new label unless disabled
101 if (!HasFlag(wxST_NO_AUTORESIZE))
102 SetSize( GetBestSize() );
103 }
104
105 bool wxStaticText::SetFont( const wxFont &font )
106 {
107 bool ret = wxControl::SetFont(font);
108
109 // adjust the label size to the new label unless disabled
110 if (!HasFlag(wxST_NO_AUTORESIZE))
111 {
112 InvalidateBestSize();
113 SetSize( GetBestSize() );
114 }
115 return ret;
116 }
117
118 void wxStaticText::DoSetSize(int x, int y,
119 int width, int height,
120 int sizeFlags )
121 {
122 wxControl::DoSetSize( x, y, width, height, sizeFlags );
123 }
124
125 wxSize wxStaticText::DoGetBestSize() const
126 {
127 // Do not return any arbitrary default value...
128 wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") );
129
130 // This resets the internal GTK1 size calculation, which
131 // otherwise would be cashed (incorrectly)
132 gtk_label_set_pattern( GTK_LABEL(m_widget), NULL );
133
134 // GetBestSize is supposed to return unwrapped size
135 gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE );
136
137 GtkRequisition req;
138 req.width = -1;
139 req.height = -1;
140 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
141 (m_widget, &req );
142
143 gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE );
144
145 return wxSize (req.width, req.height);
146 }
147
148 bool wxStaticText::SetForegroundColour(const wxColour& colour)
149 {
150 // First, we call the base class member
151 wxControl::SetForegroundColour(colour);
152 // Then, to force the color change, we set the label with the current label
153 SetLabel(GetLabel());
154 return true;
155 }
156
157 // static
158 wxVisualAttributes
159 wxStaticText::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
160 {
161 return GetDefaultAttributesFromGTKWidget(gtk_label_new);
162 }
163
164 #endif // wxUSE_STATTEXT