]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/button.cpp
Make brush hatches in wxGTK consistent with wxMSW.
[wxWidgets.git] / src / gtk / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/button.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_BUTTON
14
15 #ifndef WX_PRECOMP
16 #include "wx/button.h"
17 #endif
18
19 #include "wx/stockitem.h"
20
21 #include "wx/gtk/private.h"
22
23 // ----------------------------------------------------------------------------
24 // GTK callbacks
25 // ----------------------------------------------------------------------------
26
27 extern "C"
28 {
29
30 static void
31 wxgtk_button_clicked_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
32 {
33 if ( button->GTKShouldIgnoreEvent() )
34 return;
35
36 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
37 event.SetEventObject(button);
38 button->HandleWindowEvent(event);
39 }
40
41 static void
42 wxgtk_button_enter_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
43 {
44 if ( button->GTKShouldIgnoreEvent() )
45 return;
46
47 button->GTKMouseEnters();
48 }
49
50 static void
51 wxgtk_button_leave_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
52 {
53 if ( button->GTKShouldIgnoreEvent() )
54 return;
55
56 button->GTKMouseLeaves();
57 }
58
59 static void
60 wxgtk_button_press_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
61 {
62 if ( button->GTKShouldIgnoreEvent() )
63 return;
64
65 button->GTKPressed();
66 }
67
68 static void
69 wxgtk_button_released_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
70 {
71 if ( button->GTKShouldIgnoreEvent() )
72 return;
73
74 button->GTKReleased();
75 }
76
77 //-----------------------------------------------------------------------------
78 // "style_set" from m_widget
79 //-----------------------------------------------------------------------------
80
81 static void
82 wxgtk_button_style_set_callback(GtkWidget* widget, GtkStyle*, wxButton* win)
83 {
84 /* the default button has a border around it */
85 wxWindow* parent = win->GetParent();
86 if (parent && parent->m_wxwindow && GTK_WIDGET_CAN_DEFAULT(widget))
87 {
88 GtkBorder* border = NULL;
89 gtk_widget_style_get(widget, "default_border", &border, NULL);
90 if (border)
91 {
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);
98 }
99 }
100 }
101
102 } // extern "C"
103
104 //-----------------------------------------------------------------------------
105 // wxButton
106 //-----------------------------------------------------------------------------
107
108 bool 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)
116 {
117 if (!PreCreation( parent, pos, size ) ||
118 !CreateBase( parent, id, pos, size, style, validator, name ))
119 {
120 wxFAIL_MSG( wxT("wxButton creation failed") );
121 return false;
122 }
123
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
127 const bool
128 useLabel = !(style & wxBU_NOTEXT) && (!label.empty() || wxIsStockID(id));
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
142 g_object_ref(m_widget);
143
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
156 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
157
158 if ( useLabel )
159 SetLabel(label);
160
161 if (style & wxNO_BORDER)
162 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
163
164 g_signal_connect_after (m_widget, "clicked",
165 G_CALLBACK (wxgtk_button_clicked_callback),
166 this);
167
168 g_signal_connect_after (m_widget, "style_set",
169 G_CALLBACK (wxgtk_button_style_set_callback),
170 this);
171
172 m_parent->DoAddChild( this );
173
174 PostCreation(size);
175
176 return true;
177 }
178
179
180 wxWindow *wxButton::SetDefault()
181 {
182 wxWindow *oldDefault = wxButtonBase::SetDefault();
183
184 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
185 gtk_widget_grab_default( m_widget );
186
187 // resize for default border
188 wxgtk_button_style_set_callback( m_widget, NULL, this );
189
190 return oldDefault;
191 }
192
193 /* static */
194 wxSize wxButtonBase::GetDefaultSize()
195 {
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);
222
223 gtk_widget_destroy(wnd);
224 }
225 return size;
226 }
227
228 void wxButton::SetLabel( const wxString &lbl )
229 {
230 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
231
232 wxString label(lbl);
233
234 if (label.empty() && wxIsStockID(m_windowId))
235 label = wxGetStockLabel(m_windowId);
236
237 wxControl::SetLabel(label);
238
239 // don't use label if it was explicitly disabled
240 if ( HasFlag(wxBU_NOTEXT) )
241 return;
242
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);
250 return;
251 }
252 }
253
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);
258 const wxString labelGTK = GTKConvertMnemonics(label);
259 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
260 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
261
262 GTKApplyWidgetStyle( false );
263 }
264
265 #if wxUSE_MARKUP
266 bool wxButton::DoSetLabelMarkup(const wxString& markup)
267 {
268 wxCHECK_MSG( m_widget != NULL, false, "invalid button" );
269
270 const wxString stripped = RemoveMarkup(markup);
271 if ( stripped.empty() && !markup.empty() )
272 return false;
273
274 wxControl::SetLabel(stripped);
275
276 GtkLabel * const label = GTKGetLabel();
277 wxCHECK_MSG( label, false, "no label in this button?" );
278
279 GTKSetLabelWithMarkupForLabel(label, markup);
280
281 return true;
282 }
283 #endif // wxUSE_MARKUP
284
285 bool wxButton::Enable( bool enable )
286 {
287 if (!base_type::Enable(enable))
288 return false;
289
290 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
291
292 if (enable)
293 GTKFixSensitivity();
294
295 GTKUpdateBitmap();
296
297 return true;
298 }
299
300 GdkWindow *wxButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
301 {
302 return GTK_BUTTON(m_widget)->event_window;
303 }
304
305 GtkLabel *wxButton::GTKGetLabel() const
306 {
307 GtkWidget *child = GTK_BIN(m_widget)->child;
308 if ( GTK_IS_ALIGNMENT(child) )
309 {
310 GtkWidget *box = GTK_BIN(child)->child;
311 for (GList* item = GTK_BOX(box)->children; item; item = item->next)
312 {
313 GtkBoxChild* boxChild = static_cast<GtkBoxChild*>(item->data);
314 if ( GTK_IS_LABEL(boxChild->widget) )
315 return GTK_LABEL(boxChild->widget);
316 }
317
318 return NULL;
319 }
320
321 return GTK_LABEL(child);
322 }
323
324 void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
325 {
326 gtk_widget_modify_style(m_widget, style);
327 GtkWidget *child = GTK_BIN(m_widget)->child;
328 gtk_widget_modify_style(child, style);
329
330 // for buttons with images, the path to the label is (at least in 2.12)
331 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
332 if ( GTK_IS_ALIGNMENT(child) )
333 {
334 GtkWidget *box = GTK_BIN(child)->child;
335 if ( GTK_IS_BOX(box) )
336 {
337 for (GList* item = GTK_BOX(box)->children; item; item = item->next)
338 {
339 GtkBoxChild* boxChild = static_cast<GtkBoxChild*>(item->data);
340 gtk_widget_modify_style(boxChild->widget, style);
341 }
342 }
343 }
344 }
345
346 wxSize wxButton::DoGetBestSize() const
347 {
348 // the default button in wxGTK is bigger than the other ones because of an
349 // extra border around it, but we don't want to take it into account in
350 // our size calculations (otherwise the result is visually ugly), so
351 // always return the size of non default button from here
352 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
353 if ( isDefault )
354 {
355 // temporarily unset default flag
356 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
357 }
358
359 wxSize ret( wxControl::DoGetBestSize() );
360
361 if ( isDefault )
362 {
363 // set it back again
364 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
365 }
366
367 if (!HasFlag(wxBU_EXACTFIT))
368 {
369 wxSize defaultSize = GetDefaultSize();
370 if (ret.x < defaultSize.x)
371 ret.x = defaultSize.x;
372 if (ret.y < defaultSize.y)
373 ret.y = defaultSize.y;
374 }
375
376 CacheBestSize(ret);
377 return ret;
378 }
379
380 // static
381 wxVisualAttributes
382 wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
383 {
384 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
385 }
386
387 // ----------------------------------------------------------------------------
388 // bitmaps support
389 // ----------------------------------------------------------------------------
390
391 void wxButton::GTKMouseEnters()
392 {
393 m_isCurrent = true;
394
395 GTKUpdateBitmap();
396 }
397
398 void wxButton::GTKMouseLeaves()
399 {
400 m_isCurrent = false;
401
402 GTKUpdateBitmap();
403 }
404
405 void wxButton::GTKPressed()
406 {
407 m_isPressed = true;
408
409 GTKUpdateBitmap();
410 }
411
412 void wxButton::GTKReleased()
413 {
414 m_isPressed = false;
415
416 GTKUpdateBitmap();
417 }
418
419 void wxButton::GTKOnFocus(wxFocusEvent& event)
420 {
421 event.Skip();
422
423 GTKUpdateBitmap();
424 }
425
426 wxButton::State wxButton::GTKGetCurrentState() const
427 {
428 if ( !IsThisEnabled() )
429 return m_bitmaps[State_Disabled].IsOk() ? State_Disabled : State_Normal;
430
431 if ( m_isPressed && m_bitmaps[State_Pressed].IsOk() )
432 return State_Pressed;
433
434 if ( m_isCurrent && m_bitmaps[State_Current].IsOk() )
435 return State_Current;
436
437 if ( HasFocus() && m_bitmaps[State_Focused].IsOk() )
438 return State_Focused;
439
440 return State_Normal;
441 }
442
443 void wxButton::GTKUpdateBitmap()
444 {
445 // if we don't show bitmaps at all, there is nothing to update
446 if ( m_bitmaps[State_Normal].IsOk() )
447 {
448 // if we do show them, this will return a state for which we do have a
449 // valid bitmap
450 State state = GTKGetCurrentState();
451
452 GTKDoShowBitmap(m_bitmaps[state]);
453 }
454 }
455
456 void wxButton::GTKDoShowBitmap(const wxBitmap& bitmap)
457 {
458 wxASSERT_MSG( bitmap.IsOk(), "invalid bitmap" );
459
460 GtkWidget *image;
461 if ( DontShowLabel() )
462 {
463 image = GTK_BIN(m_widget)->child;
464 }
465 else // have both label and bitmap
466 {
467 #ifdef __WXGTK26__
468 if ( !gtk_check_version(2,6,0) )
469 {
470 image = gtk_button_get_image(GTK_BUTTON(m_widget));
471 }
472 else
473 #endif // __WXGTK26__
474 {
475 // buttons with both label and bitmap are only supported with GTK+
476 // 2.6 so far
477 //
478 // it shouldn't be difficult to implement them ourselves for the
479 // previous GTK+ versions by stuffing a container with a label and
480 // an image inside GtkButton but there doesn't seem to be much
481 // point in doing this for ancient GTK+ versions
482 return;
483 }
484 }
485
486 wxCHECK_RET( image && GTK_IS_IMAGE(image), "must have image widget" );
487
488 gtk_image_set_from_pixbuf(GTK_IMAGE(image), bitmap.GetPixbuf());
489 }
490
491 wxBitmap wxButton::DoGetBitmap(State which) const
492 {
493 return m_bitmaps[which];
494 }
495
496 void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which)
497 {
498 switch ( which )
499 {
500 case State_Normal:
501 if ( DontShowLabel() )
502 {
503 // we only have the bitmap in this button, never remove it but
504 // do invalidate the best size when the bitmap (and presumably
505 // its size) changes
506 InvalidateBestSize();
507 }
508 #ifdef __WXGTK26__
509 // normal image is special: setting it enables images for the button and
510 // resetting it to nothing disables all of them
511 else if ( !gtk_check_version(2,6,0) )
512 {
513 GtkWidget *image = gtk_button_get_image(GTK_BUTTON(m_widget));
514 if ( image && !bitmap.IsOk() )
515 {
516 gtk_container_remove(GTK_CONTAINER(m_widget), image);
517 }
518 else if ( !image && bitmap.IsOk() )
519 {
520 image = gtk_image_new();
521 gtk_button_set_image(GTK_BUTTON(m_widget), image);
522 }
523 else // image presence or absence didn't change
524 {
525 // don't invalidate best size below
526 break;
527 }
528
529 InvalidateBestSize();
530 }
531 #endif // GTK+ 2.6+
532 break;
533
534 case State_Pressed:
535 if ( bitmap.IsOk() )
536 {
537 if ( !m_bitmaps[which].IsOk() )
538 {
539 // we need to install the callbacks to be notified about
540 // the button pressed state change
541 g_signal_connect
542 (
543 m_widget,
544 "pressed",
545 G_CALLBACK(wxgtk_button_press_callback),
546 this
547 );
548
549 g_signal_connect
550 (
551 m_widget,
552 "released",
553 G_CALLBACK(wxgtk_button_released_callback),
554 this
555 );
556 }
557 }
558 else // no valid bitmap
559 {
560 if ( m_bitmaps[which].IsOk() )
561 {
562 // we don't need to be notified about the button pressed
563 // state changes any more
564 g_signal_handlers_disconnect_by_func
565 (
566 m_widget,
567 (gpointer)wxgtk_button_press_callback,
568 this
569 );
570
571 g_signal_handlers_disconnect_by_func
572 (
573 m_widget,
574 (gpointer)wxgtk_button_released_callback,
575 this
576 );
577
578 // also make sure we don't remain stuck in pressed state
579 if ( m_isPressed )
580 {
581 m_isPressed = false;
582 GTKUpdateBitmap();
583 }
584 }
585 }
586 break;
587
588 case State_Current:
589 // the logic here is the same as above for State_Pressed: we need
590 // to connect the handlers if we must be notified about the changes
591 // in the button current state and we disconnect them when/if we
592 // don't need them any more
593 if ( bitmap.IsOk() )
594 {
595 if ( !m_bitmaps[which].IsOk() )
596 {
597 g_signal_connect
598 (
599 m_widget,
600 "enter",
601 G_CALLBACK(wxgtk_button_enter_callback),
602 this
603 );
604
605 g_signal_connect
606 (
607 m_widget,
608 "leave",
609 G_CALLBACK(wxgtk_button_leave_callback),
610 this
611 );
612 }
613 }
614 else // no valid bitmap
615 {
616 if ( m_bitmaps[which].IsOk() )
617 {
618 g_signal_handlers_disconnect_by_func
619 (
620 m_widget,
621 (gpointer)wxgtk_button_enter_callback,
622 this
623 );
624
625 g_signal_handlers_disconnect_by_func
626 (
627 m_widget,
628 (gpointer)wxgtk_button_leave_callback,
629 this
630 );
631
632 if ( m_isCurrent )
633 {
634 m_isCurrent = false;
635 GTKUpdateBitmap();
636 }
637 }
638 }
639 break;
640
641 case State_Focused:
642 if ( bitmap.IsOk() )
643 {
644 Connect(wxEVT_SET_FOCUS,
645 wxFocusEventHandler(wxButton::GTKOnFocus));
646 Connect(wxEVT_KILL_FOCUS,
647 wxFocusEventHandler(wxButton::GTKOnFocus));
648 }
649 else // no valid focused bitmap
650 {
651 Disconnect(wxEVT_SET_FOCUS,
652 wxFocusEventHandler(wxButton::GTKOnFocus));
653 Disconnect(wxEVT_KILL_FOCUS,
654 wxFocusEventHandler(wxButton::GTKOnFocus));
655 }
656 break;
657
658 default:
659 // no callbacks to connect/disconnect
660 ;
661 }
662
663 m_bitmaps[which] = bitmap;
664
665 // update the bitmap immediately if necessary, otherwise it will be done
666 // when the bitmap for the corresponding state is needed the next time by
667 // GTKUpdateBitmap()
668 if ( bitmap.IsOk() && which == GTKGetCurrentState() )
669 {
670 GTKDoShowBitmap(bitmap);
671 }
672 }
673
674 void wxButton::DoSetBitmapPosition(wxDirection dir)
675 {
676 #ifdef __WXGTK210__
677 if ( !gtk_check_version(2,10,0) )
678 {
679 GtkPositionType gtkpos;
680 switch ( dir )
681 {
682 default:
683 wxFAIL_MSG( "invalid position" );
684 // fall through
685
686 case wxLEFT:
687 gtkpos = GTK_POS_LEFT;
688 break;
689
690 case wxRIGHT:
691 gtkpos = GTK_POS_RIGHT;
692 break;
693
694 case wxTOP:
695 gtkpos = GTK_POS_TOP;
696 break;
697
698 case wxBOTTOM:
699 gtkpos = GTK_POS_BOTTOM;
700 break;
701 }
702
703 gtk_button_set_image_position(GTK_BUTTON(m_widget), gtkpos);
704 InvalidateBestSize();
705 }
706 #endif // GTK+ 2.10+
707 }
708
709 #endif // wxUSE_BUTTON