]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: stattext.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
a81258be RR |
5 | // Id: $Id$ |
6 | // Copyright: (c) 1998 Robert Roebling | |
b58197f2 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
b58197f2 | 11 | #pragma implementation "stattext.h" |
c801d85f KB |
12 | #endif |
13 | ||
14f355c2 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
1e6feb95 VZ |
16 | |
17 | #if wxUSE_STATTEXT | |
18 | ||
c801d85f | 19 | #include "wx/stattext.h" |
fab591c5 | 20 | #include "wx/gtk/private.h" |
c801d85f | 21 | |
83624f79 RR |
22 | #include "gdk/gdk.h" |
23 | #include "gtk/gtk.h" | |
24 | ||
e1f448ee VZ |
25 | extern "C" |
26 | void wxgtk_window_size_request_callback(GtkWidget *widget, | |
27 | GtkRequisition *requisition, | |
28 | wxWindow *win); | |
29 | ||
c801d85f KB |
30 | //----------------------------------------------------------------------------- |
31 | // wxStaticText | |
32 | //----------------------------------------------------------------------------- | |
33 | ||
34 | IMPLEMENT_DYNAMIC_CLASS(wxStaticText,wxControl) | |
35 | ||
b58197f2 | 36 | wxStaticText::wxStaticText() |
c801d85f | 37 | { |
3f659fd6 | 38 | } |
c801d85f | 39 | |
b58197f2 VZ |
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) | |
c801d85f KB |
47 | { |
48 | Create( parent, id, label, pos, size, style, name ); | |
3f659fd6 | 49 | } |
c801d85f | 50 | |
b58197f2 VZ |
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 ) | |
c801d85f | 58 | { |
a93109d5 | 59 | m_needParent = TRUE; |
b58197f2 | 60 | |
4dcaf11a RR |
61 | if (!PreCreation( parent, pos, size ) || |
62 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
63 | { | |
223d09f6 | 64 | wxFAIL_MSG( wxT("wxXX creation failed") ); |
185fa6bf | 65 | return FALSE; |
4dcaf11a | 66 | } |
b58197f2 VZ |
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 | |
a93109d5 | 72 | wxControl::SetLabel(label); |
02c0348e | 73 | m_widget = gtk_label_new( wxGTK_CONV( m_label ) ); |
97d7bfb8 | 74 | |
a93109d5 RR |
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); | |
1a5594b8 | 83 | |
1a5594b8 VZ |
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); | |
185fa6bf | 87 | |
33720b2d RR |
88 | // do not move this call elsewhere |
89 | gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE ); | |
90 | ||
f03fc89f | 91 | m_parent->DoAddChild( this ); |
b58197f2 | 92 | |
a93109d5 | 93 | PostCreation(); |
7e2b55cd | 94 | |
e1f448ee VZ |
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 | ||
7e2b55cd | 112 | ApplyWidgetStyle(); |
b58197f2 | 113 | |
db434467 RR |
114 | wxControl::SetFont( parent->GetFont() ); |
115 | ||
116 | wxSize size_best( DoGetBestSize() ); | |
117 | wxSize new_size( size ); | |
118 | if (new_size.x == -1) | |
119 | new_size.x = size_best.x; | |
120 | if (new_size.y == -1) | |
121 | new_size.y = size_best.y; | |
122 | if ((new_size.x != size.x) || (new_size.y != size.y)) | |
123 | SetSize( new_size.x, new_size.y ); | |
124 | ||
a93109d5 RR |
125 | SetBackgroundColour( parent->GetBackgroundColour() ); |
126 | SetForegroundColour( parent->GetForegroundColour() ); | |
a93109d5 | 127 | Show( TRUE ); |
b58197f2 | 128 | |
a93109d5 | 129 | return TRUE; |
3f659fd6 | 130 | } |
c801d85f | 131 | |
ed58dbea | 132 | wxString wxStaticText::GetLabel() const |
c801d85f | 133 | { |
3b2e67f6 RR |
134 | GtkLabel *label = GTK_LABEL(m_widget); |
135 | ||
136 | #ifdef __WXGTK20__ | |
137 | wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) ); | |
138 | #else | |
139 | wxString str = wxString( label->label ); | |
140 | #endif | |
b58197f2 VZ |
141 | |
142 | return wxString(str); | |
3f659fd6 | 143 | } |
c801d85f KB |
144 | |
145 | void wxStaticText::SetLabel( const wxString &label ) | |
146 | { | |
a93109d5 | 147 | wxControl::SetLabel(label); |
32c77a71 | 148 | |
02c0348e | 149 | gtk_label_set( GTK_LABEL(m_widget), wxGTK_CONV( m_label ) ); |
b58197f2 | 150 | |
185fa6bf | 151 | // adjust the label size to the new label unless disabled |
33720b2d | 152 | if (!HasFlag(wxST_NO_AUTORESIZE)) |
f68586e5 | 153 | SetSize( GetBestSize() ); |
33720b2d RR |
154 | } |
155 | ||
156 | bool wxStaticText::SetFont( const wxFont &font ) | |
157 | { | |
158 | bool ret = wxControl::SetFont(font); | |
159 | ||
160 | // adjust the label size to the new label unless disabled | |
161 | if (!HasFlag(wxST_NO_AUTORESIZE)) | |
162 | SetSize( GetBestSize() ); | |
163 | ||
164 | return ret; | |
b58197f2 | 165 | } |
58614078 RR |
166 | |
167 | void wxStaticText::ApplyWidgetStyle() | |
168 | { | |
a93109d5 RR |
169 | SetWidgetStyle(); |
170 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
58614078 | 171 | } |
33720b2d RR |
172 | |
173 | wxSize wxStaticText::DoGetBestSize() const | |
174 | { | |
175 | // Do not return any arbitrary default value... | |
176 | wxASSERT_MSG( m_widget, wxT("wxStaticText::DoGetBestSize called before creation") ); | |
177 | ||
178 | // this invalidates the size request | |
179 | gtk_label_set_line_wrap( GTK_LABEL(m_widget), TRUE ); | |
180 | gtk_label_set_line_wrap( GTK_LABEL(m_widget), FALSE ); | |
181 | ||
182 | GtkRequisition req; | |
183 | req.width = 2; | |
184 | req.height = 2; | |
2afa14f2 | 185 | (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request ) |
33720b2d RR |
186 | (m_widget, &req ); |
187 | ||
188 | return wxSize(req.width, req.height); | |
189 | } | |
190 | ||
1e6feb95 | 191 | #endif // wxUSE_STATTEXT |