]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/button.cpp
Define KEY_WOW64_64KEY if it is missing from SDK headers.
[wxWidgets.git] / src / gtk / button.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/gtk/button.cpp
c801d85f
KB
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
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
1e6feb95
VZ
13#if wxUSE_BUTTON
14
b84aec03 15#ifndef WX_PRECOMP
94aff5ff 16 #include "wx/button.h"
b84aec03
WS
17#endif
18
5f7bcb48 19#include "wx/stockitem.h"
c801d85f 20
9e691f46 21#include "wx/gtk/private.h"
83624f79 22
b4a4eafb
VZ
23// ----------------------------------------------------------------------------
24// GTK callbacks
25// ----------------------------------------------------------------------------
66bd6b93 26
b4a4eafb
VZ
27extern "C"
28{
c801d85f 29
b4a4eafb
VZ
30static void
31wxgtk_button_clicked_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
c801d85f 32{
b4a4eafb
VZ
33 if ( button->GTKShouldIgnoreEvent() )
34 return;
9e691f46 35
acfd422a
RR
36 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
37 event.SetEventObject(button);
937013e0 38 button->HandleWindowEvent(event);
6de97a3b 39}
b4a4eafb
VZ
40
41static void
42wxgtk_button_enter_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
43{
44 if ( button->GTKShouldIgnoreEvent() )
45 return;
46
47 button->GTKMouseEnters();
48}
49
50static void
51wxgtk_button_leave_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
52{
53 if ( button->GTKShouldIgnoreEvent() )
54 return;
55
56 button->GTKMouseLeaves();
57}
58
59static void
60wxgtk_button_press_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
61{
62 if ( button->GTKShouldIgnoreEvent() )
63 return;
64
65 button->GTKPressed();
66}
67
68static void
69wxgtk_button_released_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
70{
71 if ( button->GTKShouldIgnoreEvent() )
72 return;
73
74 button->GTKReleased();
865bb325 75}
c801d85f 76
a90c0600
RR
77//-----------------------------------------------------------------------------
78// "style_set" from m_widget
79//-----------------------------------------------------------------------------
80
4cdf71be 81static void
b4a4eafb 82wxgtk_button_style_set_callback(GtkWidget* widget, GtkStyle*, wxButton* win)
a90c0600 83{
f893066b 84 /* the default button has a border around it */
4cdf71be
PC
85 wxWindow* parent = win->GetParent();
86 if (parent && parent->m_wxwindow && GTK_WIDGET_CAN_DEFAULT(widget))
f893066b 87 {
4cdf71be
PC
88 GtkBorder* border = NULL;
89 gtk_widget_style_get(widget, "default_border", &border, NULL);
90 if (border)
f893066b 91 {
4cdf71be
PC
92 win->MoveWindow(
93 win->m_x - border->left,
94 win->m_y - border->top,
95 win->m_width + border->left + border->right,
96 win->m_height + border->top + border->bottom);
97 gtk_border_free(border);
f893066b 98 }
b2ff89d6 99 }
4cdf71be 100}
b4a4eafb
VZ
101
102} // extern "C"
a90c0600 103
c801d85f 104//-----------------------------------------------------------------------------
e1e955e1
RR
105// wxButton
106//-----------------------------------------------------------------------------
107
e8375af8
VZ
108bool wxButton::Create(wxWindow *parent,
109 wxWindowID id,
110 const wxString &label,
111 const wxPoint& pos,
112 const wxSize& size,
113 long style,
114 const wxValidator& validator,
115 const wxString& name)
c801d85f 116{
4dcaf11a
RR
117 if (!PreCreation( parent, pos, size ) ||
118 !CreateBase( parent, id, pos, size, style, validator, name ))
119 {
223d09f6 120 wxFAIL_MSG( wxT("wxButton creation failed") );
93763ad5 121 return false;
4dcaf11a 122 }
c801d85f 123
c37dd6da
VZ
124 // create either a standard button with text label (which may still contain
125 // an image under GTK+ 2.6+) or a bitmap-only button if we don't have any
126 // label
a2117591
VZ
127 const bool
128 useLabel = !(style & wxBU_NOTEXT) && (!label.empty() || wxIsStockID(id));
c37dd6da
VZ
129 if ( useLabel )
130 {
131 m_widget = gtk_button_new_with_mnemonic("");
132 }
133 else // no label, suppose we will have a bitmap
134 {
135 m_widget = gtk_button_new();
136
137 GtkWidget *image = gtk_image_new();
138 gtk_widget_show(image);
139 gtk_container_add(GTK_CONTAINER(m_widget), image);
140 }
141
9ff9d30c 142 g_object_ref(m_widget);
354aa1e3 143
2e8613b7
RR
144 float x_alignment = 0.5;
145 if (HasFlag(wxBU_LEFT))
146 x_alignment = 0.0;
147 else if (HasFlag(wxBU_RIGHT))
148 x_alignment = 1.0;
149
150 float y_alignment = 0.5;
151 if (HasFlag(wxBU_TOP))
152 y_alignment = 0.0;
153 else if (HasFlag(wxBU_BOTTOM))
154 y_alignment = 1.0;
155
593ac8df 156 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
a696db45 157
c37dd6da
VZ
158 if ( useLabel )
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
9fa72bd2 164 g_signal_connect_after (m_widget, "clicked",
b4a4eafb 165 G_CALLBACK (wxgtk_button_clicked_callback),
9fa72bd2 166 this);
c801d85f 167
9fa72bd2 168 g_signal_connect_after (m_widget, "style_set",
b4a4eafb 169 G_CALLBACK (wxgtk_button_style_set_callback),
9fa72bd2 170 this);
b2ff89d6 171
f03fc89f 172 m_parent->DoAddChild( this );
9e691f46 173
abdeb9e7 174 PostCreation(size);
db434467 175
4fa87bd9
VS
176 return true;
177}
b2ff89d6 178
32c77a71 179
94aff5ff 180wxWindow *wxButton::SetDefault()
c801d85f 181{
94aff5ff 182 wxWindow *oldDefault = wxButtonBase::SetDefault();
b2ff89d6 183
3502e687
RR
184 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
185 gtk_widget_grab_default( m_widget );
b2ff89d6 186
f893066b 187 // resize for default border
b4a4eafb 188 wxgtk_button_style_set_callback( m_widget, NULL, this );
94aff5ff
VZ
189
190 return oldDefault;
6de97a3b 191}
c801d85f 192
ebea0891 193/* static */
4fa87bd9 194wxSize wxButtonBase::GetDefaultSize()
8dbf4589 195{
4fa87bd9
VS
196 static wxSize size = wxDefaultSize;
197 if (size == wxDefaultSize)
198 {
199 // NB: Default size of buttons should be same as size of stock
200 // buttons as used in most GTK+ apps. Unfortunately it's a little
201 // tricky to obtain this size: stock button's size may be smaller
202 // than size of button in GtkButtonBox and vice versa,
203 // GtkButtonBox's minimal button size may be smaller than stock
204 // button's size. We have to retrieve both values and combine them.
205
206 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
207 GtkWidget *box = gtk_hbutton_box_new();
208 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
209 gtk_container_add(GTK_CONTAINER(box), btn);
210 gtk_container_add(GTK_CONTAINER(wnd), box);
211 GtkRequisition req;
212 gtk_widget_size_request(btn, &req);
213
214 gint minwidth, minheight;
215 gtk_widget_style_get(box,
216 "child-min-width", &minwidth,
217 "child-min-height", &minheight,
218 NULL);
219
220 size.x = wxMax(minwidth, req.width);
221 size.y = wxMax(minheight, req.height);
b2ff89d6 222
4fa87bd9
VS
223 gtk_widget_destroy(wnd);
224 }
225 return size;
8dbf4589
RR
226}
227
5f7bcb48 228void wxButton::SetLabel( const wxString &lbl )
c801d85f 229{
223d09f6 230 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 231
5f7bcb48
VS
232 wxString label(lbl);
233
5f7bcb48
VS
234 if (label.empty() && wxIsStockID(m_windowId))
235 label = wxGetStockLabel(m_windowId);
5f7bcb48
VS
236
237 wxControl::SetLabel(label);
9e691f46 238
a2117591
VZ
239 // don't use label if it was explicitly disabled
240 if ( HasFlag(wxBU_NOTEXT) )
241 return;
242
5f7bcb48
VS
243 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
244 {
245 const char *stock = wxGetStockGtkID(m_windowId);
246 if (stock)
247 {
248 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
249 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
b04683b1 250 return;
5f7bcb48 251 }
5f7bcb48
VS
252 }
253
5366ff46
VZ
254 // this call is necessary if the button had been initially created without
255 // a (text) label -- then we didn't use gtk_button_new_with_mnemonic() and
256 // so "use-underline" GtkButton property remained unset
257 gtk_button_set_use_underline(GTK_BUTTON(m_widget), TRUE);
4cdf71be 258 const wxString labelGTK = GTKConvertMnemonics(label);
b2ff89d6 259 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
5f7bcb48 260 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
b2ff89d6 261
496e7ec6 262 GTKApplyWidgetStyle( false );
6de97a3b 263}
c801d85f 264
f03fc89f 265bool wxButton::Enable( bool enable )
a9c96bcc 266{
b545684e 267 if (!base_type::Enable(enable))
93763ad5 268 return false;
9e691f46 269
afa7bd1e 270 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
f03fc89f 271
b545684e 272 if (enable)
ad60f9e7 273 GTKFixSensitivity();
ad60f9e7 274
b4a4eafb
VZ
275 GTKUpdateBitmap();
276
93763ad5 277 return true;
a9c96bcc
RR
278}
279
ef5c70f9 280GdkWindow *wxButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
2b5f62a0 281{
2b5f62a0 282 return GTK_BUTTON(m_widget)->event_window;
2b5f62a0
VZ
283}
284
f40fdaa3 285void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 286{
f40fdaa3 287 gtk_widget_modify_style(m_widget, style);
dfc22083
VZ
288 GtkWidget *child = GTK_BIN(m_widget)->child;
289 gtk_widget_modify_style(child, style);
290
291 // for buttons with images, the path to the label is (at least in 2.12)
292 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
293 if ( GTK_IS_ALIGNMENT(child) )
294 {
295 GtkWidget *box = GTK_BIN(child)->child;
296 if ( GTK_IS_BOX(box) )
297 {
f4b0832d
PC
298 for (GList* item = GTK_BOX(box)->children; item; item = item->next)
299 {
300 GtkBoxChild* boxChild = static_cast<GtkBoxChild*>(item->data);
301 gtk_widget_modify_style(boxChild->widget, style);
302 }
dfc22083
VZ
303 }
304 }
a81258be 305}
db434467
RR
306
307wxSize wxButton::DoGetBestSize() const
308{
4f819fe4
VZ
309 // the default button in wxGTK is bigger than the other ones because of an
310 // extra border around it, but we don't want to take it into account in
7be740a3 311 // our size calculations (otherwise the result is visually ugly), so
4f819fe4
VZ
312 // always return the size of non default button from here
313 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
314 if ( isDefault )
315 {
316 // temporarily unset default flag
317 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
318 }
319
db434467 320 wxSize ret( wxControl::DoGetBestSize() );
9e691f46 321
4f819fe4
VZ
322 if ( isDefault )
323 {
324 // set it back again
325 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
326 }
327
8ab696e0
RR
328 if (!HasFlag(wxBU_EXACTFIT))
329 {
4fa87bd9 330 wxSize defaultSize = GetDefaultSize();
7be740a3
VZ
331 if (ret.x < defaultSize.x)
332 ret.x = defaultSize.x;
333 if (ret.y < defaultSize.y)
334 ret.y = defaultSize.y;
8ab696e0 335 }
9e691f46 336
9f884528 337 CacheBestSize(ret);
db434467
RR
338 return ret;
339}
340
9d522606
RD
341// static
342wxVisualAttributes
343wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
344{
345 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
346}
347
7be740a3
VZ
348// ----------------------------------------------------------------------------
349// bitmaps support
350// ----------------------------------------------------------------------------
351
b4a4eafb
VZ
352void wxButton::GTKMouseEnters()
353{
354 m_isCurrent = true;
355
356 GTKUpdateBitmap();
357}
358
359void wxButton::GTKMouseLeaves()
360{
361 m_isCurrent = false;
362
363 GTKUpdateBitmap();
364}
365
366void wxButton::GTKPressed()
367{
368 m_isPressed = true;
369
370 GTKUpdateBitmap();
371}
372
373void wxButton::GTKReleased()
374{
375 m_isPressed = false;
376
377 GTKUpdateBitmap();
378}
379
380void wxButton::GTKOnFocus(wxFocusEvent& event)
381{
382 event.Skip();
383
384 GTKUpdateBitmap();
385}
386
387wxButton::State wxButton::GTKGetCurrentState() const
388{
389 if ( !IsThisEnabled() )
390 return m_bitmaps[State_Disabled].IsOk() ? State_Disabled : State_Normal;
391
392 if ( m_isPressed && m_bitmaps[State_Pressed].IsOk() )
393 return State_Pressed;
394
395 if ( m_isCurrent && m_bitmaps[State_Current].IsOk() )
396 return State_Current;
397
398 if ( HasFocus() && m_bitmaps[State_Focused].IsOk() )
399 return State_Focused;
400
401 return State_Normal;
402}
403
404void wxButton::GTKUpdateBitmap()
405{
e71aec80
VZ
406 // if we don't show bitmaps at all, there is nothing to update
407 if ( m_bitmaps[State_Normal].IsOk() )
408 {
409 // if we do show them, this will return a state for which we do have a
410 // valid bitmap
411 State state = GTKGetCurrentState();
b4a4eafb 412
e71aec80
VZ
413 GTKDoShowBitmap(m_bitmaps[state]);
414 }
b4a4eafb
VZ
415}
416
417void wxButton::GTKDoShowBitmap(const wxBitmap& bitmap)
418{
419 wxASSERT_MSG( bitmap.IsOk(), "invalid bitmap" );
420
c37dd6da 421 GtkWidget *image;
a2117591 422 if ( DontShowLabel() )
b4a4eafb 423 {
c37dd6da 424 image = GTK_BIN(m_widget)->child;
b4a4eafb 425 }
c37dd6da
VZ
426 else // have both label and bitmap
427 {
428#ifdef __WXGTK26__
429 if ( !gtk_check_version(2,6,0) )
430 {
431 image = gtk_button_get_image(GTK_BUTTON(m_widget));
432 }
433 else
b4a4eafb 434#endif // __WXGTK26__
c37dd6da
VZ
435 {
436 // buttons with both label and bitmap are only supported with GTK+
437 // 2.6 so far
438 //
439 // it shouldn't be difficult to implement them ourselves for the
440 // previous GTK+ versions by stuffing a container with a label and
441 // an image inside GtkButton but there doesn't seem to be much
442 // point in doing this for ancient GTK+ versions
443 return;
444 }
445 }
446
447 wxCHECK_RET( image && GTK_IS_IMAGE(image), "must have image widget" );
448
449 gtk_image_set_from_pixbuf(GTK_IMAGE(image), bitmap.GetPixbuf());
b4a4eafb
VZ
450}
451
7be740a3
VZ
452wxBitmap wxButton::DoGetBitmap(State which) const
453{
454 return m_bitmaps[which];
455}
456
457void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which)
458{
b4a4eafb 459 switch ( which )
7be740a3 460 {
b4a4eafb 461 case State_Normal:
a2117591 462 if ( DontShowLabel() )
c37dd6da
VZ
463 {
464 // we only have the bitmap in this button, never remove it but
465 // do invalidate the best size when the bitmap (and presumably
466 // its size) changes
467 InvalidateBestSize();
468 }
b4a4eafb
VZ
469#ifdef __WXGTK26__
470 // normal image is special: setting it enables images for the button and
471 // resetting it to nothing disables all of them
c37dd6da 472 else if ( !gtk_check_version(2,6,0) )
7be740a3 473 {
b4a4eafb
VZ
474 GtkWidget *image = gtk_button_get_image(GTK_BUTTON(m_widget));
475 if ( image && !bitmap.IsOk() )
476 {
477 gtk_container_remove(GTK_CONTAINER(m_widget), image);
478 }
479 else if ( !image && bitmap.IsOk() )
480 {
481 image = gtk_image_new();
482 gtk_button_set_image(GTK_BUTTON(m_widget), image);
483 }
484 else // image presence or absence didn't change
485 {
486 // don't invalidate best size below
487 break;
488 }
489
7be740a3
VZ
490 InvalidateBestSize();
491 }
b4a4eafb
VZ
492#endif // GTK+ 2.6+
493 break;
494
495 case State_Pressed:
496 if ( bitmap.IsOk() )
497 {
498 if ( !m_bitmaps[which].IsOk() )
499 {
500 // we need to install the callbacks to be notified about
501 // the button pressed state change
502 g_signal_connect
503 (
504 m_widget,
505 "pressed",
506 G_CALLBACK(wxgtk_button_press_callback),
507 this
508 );
509
510 g_signal_connect
511 (
512 m_widget,
513 "released",
514 G_CALLBACK(wxgtk_button_released_callback),
515 this
516 );
517 }
518 }
519 else // no valid bitmap
7be740a3 520 {
b4a4eafb
VZ
521 if ( m_bitmaps[which].IsOk() )
522 {
523 // we don't need to be notified about the button pressed
524 // state changes any more
525 g_signal_handlers_disconnect_by_func
526 (
527 m_widget,
528 (gpointer)wxgtk_button_press_callback,
529 this
530 );
531
532 g_signal_handlers_disconnect_by_func
533 (
534 m_widget,
535 (gpointer)wxgtk_button_released_callback,
536 this
537 );
538
539 // also make sure we don't remain stuck in pressed state
540 if ( m_isPressed )
541 {
542 m_isPressed = false;
543 GTKUpdateBitmap();
544 }
545 }
7be740a3 546 }
b4a4eafb 547 break;
7be740a3 548
b4a4eafb
VZ
549 case State_Current:
550 // the logic here is the same as above for State_Pressed: we need
551 // to connect the handlers if we must be notified about the changes
552 // in the button current state and we disconnect them when/if we
553 // don't need them any more
7be740a3
VZ
554 if ( bitmap.IsOk() )
555 {
b4a4eafb
VZ
556 if ( !m_bitmaps[which].IsOk() )
557 {
558 g_signal_connect
559 (
560 m_widget,
561 "enter",
562 G_CALLBACK(wxgtk_button_enter_callback),
563 this
564 );
565
566 g_signal_connect
567 (
568 m_widget,
569 "leave",
570 G_CALLBACK(wxgtk_button_leave_callback),
571 this
572 );
573 }
7be740a3 574 }
b4a4eafb
VZ
575 else // no valid bitmap
576 {
577 if ( m_bitmaps[which].IsOk() )
578 {
579 g_signal_handlers_disconnect_by_func
580 (
581 m_widget,
582 (gpointer)wxgtk_button_enter_callback,
583 this
584 );
585
586 g_signal_handlers_disconnect_by_func
587 (
588 m_widget,
589 (gpointer)wxgtk_button_leave_callback,
590 this
591 );
592
593 if ( m_isCurrent )
594 {
595 m_isCurrent = false;
596 GTKUpdateBitmap();
597 }
598 }
599 }
600 break;
601
602 case State_Focused:
603 if ( bitmap.IsOk() )
604 {
605 Connect(wxEVT_SET_FOCUS,
606 wxFocusEventHandler(wxButton::GTKOnFocus));
607 Connect(wxEVT_KILL_FOCUS,
608 wxFocusEventHandler(wxButton::GTKOnFocus));
609 }
610 else // no valid focused bitmap
611 {
612 Disconnect(wxEVT_SET_FOCUS,
613 wxFocusEventHandler(wxButton::GTKOnFocus));
614 Disconnect(wxEVT_KILL_FOCUS,
615 wxFocusEventHandler(wxButton::GTKOnFocus));
616 }
617 break;
618
619 default:
620 // no callbacks to connect/disconnect
621 ;
7be740a3 622 }
7be740a3
VZ
623
624 m_bitmaps[which] = bitmap;
b4a4eafb
VZ
625
626 // update the bitmap immediately if necessary, otherwise it will be done
627 // when the bitmap for the corresponding state is needed the next time by
628 // GTKUpdateBitmap()
629 if ( bitmap.IsOk() && which == GTKGetCurrentState() )
630 {
631 GTKDoShowBitmap(bitmap);
632 }
7be740a3
VZ
633}
634
635void wxButton::DoSetBitmapPosition(wxDirection dir)
636{
637#ifdef __WXGTK210__
638 if ( !gtk_check_version(2,10,0) )
639 {
640 GtkPositionType gtkpos;
641 switch ( dir )
642 {
643 default:
644 wxFAIL_MSG( "invalid position" );
645 // fall through
646
647 case wxLEFT:
648 gtkpos = GTK_POS_LEFT;
649 break;
650
651 case wxRIGHT:
652 gtkpos = GTK_POS_RIGHT;
653 break;
654
655 case wxTOP:
656 gtkpos = GTK_POS_TOP;
657 break;
658
659 case wxBOTTOM:
660 gtkpos = GTK_POS_BOTTOM;
661 break;
662 }
663
664 gtk_button_set_image_position(GTK_BUTTON(m_widget), gtkpos);
06bb8d92 665 InvalidateBestSize();
7be740a3
VZ
666 }
667#endif // GTK+ 2.10+
668}
669
1e6feb95 670#endif // wxUSE_BUTTON