]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: button.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
dbf858b5 | 5 | // Id: $Id$ |
01111366 | 6 | // Copyright: (c) 1998 Robert Roebling |
65571936 | 7 | // Licence: wxWindows licence |
c801d85f KB |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c801d85f KB |
11 | #pragma implementation "button.h" |
12 | #endif | |
13 | ||
14f355c2 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
1e6feb95 VZ |
17 | #include "wx/defs.h" |
18 | ||
19 | #if wxUSE_BUTTON | |
20 | ||
c801d85f | 21 | #include "wx/button.h" |
5f7bcb48 | 22 | #include "wx/stockitem.h" |
c801d85f | 23 | |
9e691f46 | 24 | #include "wx/gtk/private.h" |
83624f79 | 25 | |
c801d85f KB |
26 | //----------------------------------------------------------------------------- |
27 | // classes | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | class wxButton; | |
31 | ||
acfd422a RR |
32 | //----------------------------------------------------------------------------- |
33 | // idle system | |
34 | //----------------------------------------------------------------------------- | |
35 | ||
36 | extern void wxapp_install_idle_handler(); | |
37 | extern bool g_isIdle; | |
38 | ||
66bd6b93 RR |
39 | //----------------------------------------------------------------------------- |
40 | // data | |
41 | //----------------------------------------------------------------------------- | |
42 | ||
43 | extern bool g_blockEventsOnDrag; | |
44 | ||
c801d85f | 45 | //----------------------------------------------------------------------------- |
e1e955e1 | 46 | // "clicked" |
c801d85f KB |
47 | //----------------------------------------------------------------------------- |
48 | ||
66bd6b93 | 49 | static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button ) |
c801d85f | 50 | { |
9e691f46 | 51 | if (g_isIdle) |
32ac755d | 52 | wxapp_install_idle_handler(); |
acfd422a | 53 | |
a2053b27 | 54 | if (!button->m_hasVMT) return; |
acfd422a | 55 | if (g_blockEventsOnDrag) return; |
9e691f46 | 56 | |
acfd422a RR |
57 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId()); |
58 | event.SetEventObject(button); | |
59 | button->GetEventHandler()->ProcessEvent(event); | |
6de97a3b | 60 | } |
c801d85f | 61 | |
a90c0600 RR |
62 | //----------------------------------------------------------------------------- |
63 | // "style_set" from m_widget | |
64 | //----------------------------------------------------------------------------- | |
65 | ||
66 | static gint | |
67 | gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win ) | |
68 | { | |
69 | if (g_isIdle) | |
70 | wxapp_install_idle_handler(); | |
71 | ||
72 | if (GTK_WIDGET_REALIZED(m_widget)) | |
73 | win->SetSize( win->m_x, win->m_y, win->m_width, win->m_height ); | |
74 | ||
75 | return FALSE; | |
76 | } | |
77 | ||
c801d85f | 78 | //----------------------------------------------------------------------------- |
e1e955e1 RR |
79 | // wxButton |
80 | //----------------------------------------------------------------------------- | |
81 | ||
82 | IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl) | |
c801d85f | 83 | |
fd0eed64 | 84 | wxButton::wxButton() |
c801d85f | 85 | { |
6de97a3b | 86 | } |
c801d85f | 87 | |
fd0eed64 RR |
88 | wxButton::~wxButton() |
89 | { | |
fd0eed64 RR |
90 | } |
91 | ||
c801d85f | 92 | bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label, |
32c77a71 | 93 | const wxPoint &pos, const wxSize &size, |
6de97a3b | 94 | long style, const wxValidator& validator, const wxString &name ) |
c801d85f | 95 | { |
b292e2f5 RR |
96 | m_needParent = TRUE; |
97 | m_acceptsFocus = TRUE; | |
32c77a71 | 98 | |
4dcaf11a RR |
99 | if (!PreCreation( parent, pos, size ) || |
100 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
101 | { | |
223d09f6 | 102 | wxFAIL_MSG( wxT("wxButton creation failed") ); |
8ab696e0 | 103 | return FALSE; |
4dcaf11a | 104 | } |
c801d85f | 105 | |
354aa1e3 RR |
106 | /* |
107 | wxString label2( label ); | |
108 | for (size_t i = 0; i < label2.Len(); i++) | |
109 | { | |
110 | if (label2.GetChar(i) == wxT('&')) | |
8ab696e0 | 111 | label2.SetChar(i,wxT('_')); |
354aa1e3 | 112 | } |
9e691f46 | 113 | |
354aa1e3 RR |
114 | GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() ); |
115 | gtk_widget_show( accel_label ); | |
9e691f46 | 116 | |
354aa1e3 RR |
117 | m_widget = gtk_button_new(); |
118 | gtk_container_add( GTK_CONTAINER(m_widget), accel_label ); | |
9e691f46 | 119 | |
354aa1e3 | 120 | gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget ); |
9e691f46 | 121 | |
354aa1e3 RR |
122 | guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() ); |
123 | gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) ); | |
9e691f46 | 124 | |
354aa1e3 RR |
125 | wxControl::SetLabel( label ); |
126 | */ | |
9e691f46 | 127 | |
b2fcfd94 | 128 | #ifdef __WXGTK20__ |
5f7bcb48 | 129 | m_widget = gtk_button_new_with_mnemonic(""); |
b2fcfd94 | 130 | #else |
354aa1e3 | 131 | m_widget = gtk_button_new_with_label(""); |
b2fcfd94 | 132 | #endif |
354aa1e3 | 133 | |
2e8613b7 RR |
134 | float x_alignment = 0.5; |
135 | if (HasFlag(wxBU_LEFT)) | |
136 | x_alignment = 0.0; | |
137 | else if (HasFlag(wxBU_RIGHT)) | |
138 | x_alignment = 1.0; | |
139 | ||
140 | float y_alignment = 0.5; | |
141 | if (HasFlag(wxBU_TOP)) | |
142 | y_alignment = 0.0; | |
143 | else if (HasFlag(wxBU_BOTTOM)) | |
144 | y_alignment = 1.0; | |
145 | ||
77f70672 RR |
146 | #if __WXGTK24__ |
147 | if (!gtk_check_version(2,4,0)) | |
148 | { | |
149 | gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment); | |
150 | } | |
151 | else | |
4fa87bd9 | 152 | #endif |
77f70672 RR |
153 | { |
154 | if (GTK_IS_MISC(BUTTON_CHILD(m_widget))) | |
155 | gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)), | |
156 | x_alignment, y_alignment); | |
157 | } | |
a696db45 | 158 | |
5f7bcb48 | 159 | SetLabel(label); |
354aa1e3 | 160 | |
de1c750f RR |
161 | if (style & wxNO_BORDER) |
162 | gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE ); | |
de1c750f | 163 | |
58b907f6 | 164 | gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked", |
b292e2f5 | 165 | GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this ); |
c801d85f | 166 | |
a90c0600 RR |
167 | gtk_signal_connect_after( GTK_OBJECT(m_widget), "style_set", |
168 | GTK_SIGNAL_FUNC(gtk_button_style_set_callback), (gpointer*) this ); | |
169 | ||
f03fc89f | 170 | m_parent->DoAddChild( this ); |
9e691f46 | 171 | |
abdeb9e7 | 172 | PostCreation(size); |
db434467 | 173 | |
4fa87bd9 VS |
174 | return true; |
175 | } | |
176 | ||
32c77a71 | 177 | |
da048e3d | 178 | void wxButton::SetDefault() |
c801d85f | 179 | { |
97149f18 RD |
180 | wxWindow *parent = GetParent(); |
181 | wxCHECK_RET( parent, _T("button without parent?") ); | |
182 | ||
a00b2d0a | 183 | parent->SetDefaultItem(this); |
97149f18 | 184 | |
3502e687 RR |
185 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); |
186 | gtk_widget_grab_default( m_widget ); | |
9e691f46 | 187 | |
3502e687 | 188 | SetSize( m_x, m_y, m_width, m_height ); |
6de97a3b | 189 | } |
c801d85f | 190 | |
ebea0891 | 191 | /* static */ |
4fa87bd9 | 192 | wxSize wxButtonBase::GetDefaultSize() |
8dbf4589 | 193 | { |
4fa87bd9 VS |
194 | #ifdef __WXGTK20__ |
195 | static wxSize size = wxDefaultSize; | |
196 | if (size == wxDefaultSize) | |
197 | { | |
198 | // NB: Default size of buttons should be same as size of stock | |
199 | // buttons as used in most GTK+ apps. Unfortunately it's a little | |
200 | // tricky to obtain this size: stock button's size may be smaller | |
201 | // than size of button in GtkButtonBox and vice versa, | |
202 | // GtkButtonBox's minimal button size may be smaller than stock | |
203 | // button's size. We have to retrieve both values and combine them. | |
204 | ||
205 | GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
206 | GtkWidget *box = gtk_hbutton_box_new(); | |
207 | GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL); | |
208 | gtk_container_add(GTK_CONTAINER(box), btn); | |
209 | gtk_container_add(GTK_CONTAINER(wnd), box); | |
210 | GtkRequisition req; | |
211 | gtk_widget_size_request(btn, &req); | |
212 | ||
213 | gint minwidth, minheight; | |
214 | gtk_widget_style_get(box, | |
215 | "child-min-width", &minwidth, | |
216 | "child-min-height", &minheight, | |
217 | NULL); | |
218 | ||
219 | size.x = wxMax(minwidth, req.width); | |
220 | size.y = wxMax(minheight, req.height); | |
221 | ||
222 | gtk_widget_destroy(wnd); | |
223 | } | |
224 | return size; | |
225 | #else | |
8dbf4589 | 226 | return wxSize(80,26); |
4fa87bd9 | 227 | #endif |
8dbf4589 RR |
228 | } |
229 | ||
5f7bcb48 | 230 | void wxButton::SetLabel( const wxString &lbl ) |
c801d85f | 231 | { |
223d09f6 | 232 | wxCHECK_RET( m_widget != NULL, wxT("invalid button") ); |
9e691f46 | 233 | |
5f7bcb48 VS |
234 | wxString label(lbl); |
235 | ||
5f7bcb48 VS |
236 | if (label.empty() && wxIsStockID(m_windowId)) |
237 | label = wxGetStockLabel(m_windowId); | |
5f7bcb48 VS |
238 | |
239 | wxControl::SetLabel(label); | |
9e691f46 | 240 | |
eaafd2f8 | 241 | #ifdef __WXGTK20__ |
5f7bcb48 VS |
242 | if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label)) |
243 | { | |
244 | const char *stock = wxGetStockGtkID(m_windowId); | |
245 | if (stock) | |
246 | { | |
247 | gtk_button_set_label(GTK_BUTTON(m_widget), stock); | |
248 | gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE); | |
b04683b1 | 249 | return; |
5f7bcb48 | 250 | } |
5f7bcb48 VS |
251 | } |
252 | ||
253 | wxString label2 = PrepareLabelMnemonics(label); | |
254 | gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(label2)); | |
255 | gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE); | |
19ac2f44 RR |
256 | |
257 | ApplyWidgetStyle( false ); | |
258 | ||
eaafd2f8 | 259 | #else |
5f7bcb48 | 260 | gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(GetLabel())); |
eaafd2f8 | 261 | #endif |
6de97a3b | 262 | } |
c801d85f | 263 | |
f03fc89f | 264 | bool wxButton::Enable( bool enable ) |
a9c96bcc | 265 | { |
f03fc89f VZ |
266 | if ( !wxControl::Enable( enable ) ) |
267 | return FALSE; | |
9e691f46 VZ |
268 | |
269 | gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable ); | |
f03fc89f VZ |
270 | |
271 | return TRUE; | |
a9c96bcc RR |
272 | } |
273 | ||
2b5f62a0 VZ |
274 | bool wxButton::IsOwnGtkWindow( GdkWindow *window ) |
275 | { | |
276 | #ifdef __WXGTK20__ | |
277 | return GTK_BUTTON(m_widget)->event_window; | |
278 | #else | |
279 | return (window == m_widget->window); | |
280 | #endif | |
281 | } | |
282 | ||
f40fdaa3 | 283 | void wxButton::DoApplyWidgetStyle(GtkRcStyle *style) |
868a2826 | 284 | { |
f40fdaa3 VS |
285 | gtk_widget_modify_style(m_widget, style); |
286 | gtk_widget_modify_style(BUTTON_CHILD(m_widget), style); | |
a81258be | 287 | } |
db434467 RR |
288 | |
289 | wxSize wxButton::DoGetBestSize() const | |
290 | { | |
4f819fe4 VZ |
291 | // the default button in wxGTK is bigger than the other ones because of an |
292 | // extra border around it, but we don't want to take it into account in | |
293 | // our size calculations (otherwsie the result is visually ugly), so | |
294 | // always return the size of non default button from here | |
295 | const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget); | |
296 | if ( isDefault ) | |
297 | { | |
298 | // temporarily unset default flag | |
299 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
300 | } | |
301 | ||
db434467 | 302 | wxSize ret( wxControl::DoGetBestSize() ); |
9e691f46 | 303 | |
4f819fe4 VZ |
304 | if ( isDefault ) |
305 | { | |
306 | // set it back again | |
307 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
308 | } | |
309 | ||
391ddf40 RD |
310 | #ifndef __WXGTK20__ |
311 | ret.x += 10; // add a few pixels for sloppy (but common) themes | |
312 | #endif | |
313 | ||
8ab696e0 RR |
314 | if (!HasFlag(wxBU_EXACTFIT)) |
315 | { | |
4fa87bd9 VS |
316 | wxSize defaultSize = GetDefaultSize(); |
317 | if (ret.x < defaultSize.x) ret.x = defaultSize.x; | |
318 | if (ret.y < defaultSize.y) ret.y = defaultSize.y; | |
8ab696e0 | 319 | } |
9e691f46 | 320 | |
9f884528 | 321 | CacheBestSize(ret); |
db434467 RR |
322 | return ret; |
323 | } | |
324 | ||
9d522606 RD |
325 | // static |
326 | wxVisualAttributes | |
327 | wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
328 | { | |
329 | return GetDefaultAttributesFromGTKWidget(gtk_button_new); | |
330 | } | |
331 | ||
1e6feb95 VZ |
332 | #endif // wxUSE_BUTTON |
333 |