1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/button.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
16 #include "wx/button.h"
19 #include "wx/stockitem.h"
21 #include "wx/gtk/private.h"
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
31 wxgtk_button_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
33 if ( button
->GTKShouldIgnoreEvent() )
36 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
37 event
.SetEventObject(button
);
38 button
->HandleWindowEvent(event
);
42 wxgtk_button_enter_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
44 if ( button
->GTKShouldIgnoreEvent() )
47 button
->GTKMouseEnters();
51 wxgtk_button_leave_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
53 if ( button
->GTKShouldIgnoreEvent() )
56 button
->GTKMouseLeaves();
60 wxgtk_button_press_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
62 if ( button
->GTKShouldIgnoreEvent() )
69 wxgtk_button_released_callback(GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
71 if ( button
->GTKShouldIgnoreEvent() )
74 button
->GTKReleased();
77 //-----------------------------------------------------------------------------
78 // "style_set" from m_widget
79 //-----------------------------------------------------------------------------
82 wxgtk_button_style_set_callback(GtkWidget
* widget
, GtkStyle
*, wxButton
* win
)
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
))
88 GtkBorder
* border
= NULL
;
89 gtk_widget_style_get(widget
, "default_border", &border
, NULL
);
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
);
104 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
108 bool wxButton::Create(wxWindow
*parent
,
110 const wxString
&label
,
114 const wxValidator
& validator
,
115 const wxString
& name
)
117 if (!PreCreation( parent
, pos
, size
) ||
118 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
120 wxFAIL_MSG( wxT("wxButton creation failed") );
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
128 useLabel
= !(style
& wxBU_NOTEXT
) && (!label
.empty() || wxIsStockID(id
));
131 m_widget
= gtk_button_new_with_mnemonic("");
133 else // no label, suppose we will have a bitmap
135 m_widget
= gtk_button_new();
137 GtkWidget
*image
= gtk_image_new();
138 gtk_widget_show(image
);
139 gtk_container_add(GTK_CONTAINER(m_widget
), image
);
142 g_object_ref(m_widget
);
144 float x_alignment
= 0.5;
145 if (HasFlag(wxBU_LEFT
))
147 else if (HasFlag(wxBU_RIGHT
))
150 float y_alignment
= 0.5;
151 if (HasFlag(wxBU_TOP
))
153 else if (HasFlag(wxBU_BOTTOM
))
156 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
161 if (style
& wxNO_BORDER
)
162 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
164 g_signal_connect_after (m_widget
, "clicked",
165 G_CALLBACK (wxgtk_button_clicked_callback
),
168 g_signal_connect_after (m_widget
, "style_set",
169 G_CALLBACK (wxgtk_button_style_set_callback
),
172 m_parent
->DoAddChild( this );
180 wxWindow
*wxButton::SetDefault()
182 wxWindow
*oldDefault
= wxButtonBase::SetDefault();
184 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
185 gtk_widget_grab_default( m_widget
);
187 // resize for default border
188 wxgtk_button_style_set_callback( m_widget
, NULL
, this );
194 wxSize
wxButtonBase::GetDefaultSize()
196 static wxSize size
= wxDefaultSize
;
197 if (size
== wxDefaultSize
)
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.
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
);
212 gtk_widget_size_request(btn
, &req
);
214 gint minwidth
, minheight
;
215 gtk_widget_style_get(box
,
216 "child-min-width", &minwidth
,
217 "child-min-height", &minheight
,
220 size
.x
= wxMax(minwidth
, req
.width
);
221 size
.y
= wxMax(minheight
, req
.height
);
223 gtk_widget_destroy(wnd
);
228 void wxButton::SetLabel( const wxString
&lbl
)
230 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
234 if (label
.empty() && wxIsStockID(m_windowId
))
235 label
= wxGetStockLabel(m_windowId
);
237 wxControl::SetLabel(label
);
239 // don't use label if it was explicitly disabled
240 if ( HasFlag(wxBU_NOTEXT
) )
243 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
245 const char *stock
= wxGetStockGtkID(m_windowId
);
248 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
249 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
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
);
262 GTKApplyWidgetStyle( false );
265 bool wxButton::DoSetLabelMarkup(const wxString
& markup
)
267 wxCHECK_MSG( m_widget
!= NULL
, false, "invalid button" );
269 const wxString stripped
= RemoveMarkup(markup
);
270 if ( stripped
.empty() && !markup
.empty() )
273 wxControl::SetLabel(stripped
);
275 GtkLabel
* const label
= GTKGetLabel();
276 wxCHECK_MSG( label
, false, "no label in this button?" );
278 GTKSetLabelWithMarkupForLabel(label
, markup
);
283 bool wxButton::Enable( bool enable
)
285 if (!base_type::Enable(enable
))
288 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
298 GdkWindow
*wxButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
300 return GTK_BUTTON(m_widget
)->event_window
;
303 GtkLabel
*wxButton::GTKGetLabel() const
305 GtkWidget
*child
= GTK_BIN(m_widget
)->child
;
306 if ( GTK_IS_ALIGNMENT(child
) )
308 GtkWidget
*box
= GTK_BIN(child
)->child
;
309 for (GList
* item
= GTK_BOX(box
)->children
; item
; item
= item
->next
)
311 GtkBoxChild
* boxChild
= static_cast<GtkBoxChild
*>(item
->data
);
312 if ( GTK_IS_LABEL(boxChild
->widget
) )
313 return GTK_LABEL(boxChild
->widget
);
319 return GTK_LABEL(child
);
322 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
324 gtk_widget_modify_style(m_widget
, style
);
325 GtkWidget
*child
= GTK_BIN(m_widget
)->child
;
326 gtk_widget_modify_style(child
, style
);
328 // for buttons with images, the path to the label is (at least in 2.12)
329 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
330 if ( GTK_IS_ALIGNMENT(child
) )
332 GtkWidget
*box
= GTK_BIN(child
)->child
;
333 if ( GTK_IS_BOX(box
) )
335 for (GList
* item
= GTK_BOX(box
)->children
; item
; item
= item
->next
)
337 GtkBoxChild
* boxChild
= static_cast<GtkBoxChild
*>(item
->data
);
338 gtk_widget_modify_style(boxChild
->widget
, style
);
344 wxSize
wxButton::DoGetBestSize() const
346 // the default button in wxGTK is bigger than the other ones because of an
347 // extra border around it, but we don't want to take it into account in
348 // our size calculations (otherwise the result is visually ugly), so
349 // always return the size of non default button from here
350 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
353 // temporarily unset default flag
354 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
357 wxSize
ret( wxControl::DoGetBestSize() );
362 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
365 if (!HasFlag(wxBU_EXACTFIT
))
367 wxSize defaultSize
= GetDefaultSize();
368 if (ret
.x
< defaultSize
.x
)
369 ret
.x
= defaultSize
.x
;
370 if (ret
.y
< defaultSize
.y
)
371 ret
.y
= defaultSize
.y
;
380 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
382 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
385 // ----------------------------------------------------------------------------
387 // ----------------------------------------------------------------------------
389 void wxButton::GTKMouseEnters()
396 void wxButton::GTKMouseLeaves()
403 void wxButton::GTKPressed()
410 void wxButton::GTKReleased()
417 void wxButton::GTKOnFocus(wxFocusEvent
& event
)
424 wxButton::State
wxButton::GTKGetCurrentState() const
426 if ( !IsThisEnabled() )
427 return m_bitmaps
[State_Disabled
].IsOk() ? State_Disabled
: State_Normal
;
429 if ( m_isPressed
&& m_bitmaps
[State_Pressed
].IsOk() )
430 return State_Pressed
;
432 if ( m_isCurrent
&& m_bitmaps
[State_Current
].IsOk() )
433 return State_Current
;
435 if ( HasFocus() && m_bitmaps
[State_Focused
].IsOk() )
436 return State_Focused
;
441 void wxButton::GTKUpdateBitmap()
443 // if we don't show bitmaps at all, there is nothing to update
444 if ( m_bitmaps
[State_Normal
].IsOk() )
446 // if we do show them, this will return a state for which we do have a
448 State state
= GTKGetCurrentState();
450 GTKDoShowBitmap(m_bitmaps
[state
]);
454 void wxButton::GTKDoShowBitmap(const wxBitmap
& bitmap
)
456 wxASSERT_MSG( bitmap
.IsOk(), "invalid bitmap" );
459 if ( DontShowLabel() )
461 image
= GTK_BIN(m_widget
)->child
;
463 else // have both label and bitmap
466 if ( !gtk_check_version(2,6,0) )
468 image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
471 #endif // __WXGTK26__
473 // buttons with both label and bitmap are only supported with GTK+
476 // it shouldn't be difficult to implement them ourselves for the
477 // previous GTK+ versions by stuffing a container with a label and
478 // an image inside GtkButton but there doesn't seem to be much
479 // point in doing this for ancient GTK+ versions
484 wxCHECK_RET( image
&& GTK_IS_IMAGE(image
), "must have image widget" );
486 gtk_image_set_from_pixbuf(GTK_IMAGE(image
), bitmap
.GetPixbuf());
489 wxBitmap
wxButton::DoGetBitmap(State which
) const
491 return m_bitmaps
[which
];
494 void wxButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
499 if ( DontShowLabel() )
501 // we only have the bitmap in this button, never remove it but
502 // do invalidate the best size when the bitmap (and presumably
504 InvalidateBestSize();
507 // normal image is special: setting it enables images for the button and
508 // resetting it to nothing disables all of them
509 else if ( !gtk_check_version(2,6,0) )
511 GtkWidget
*image
= gtk_button_get_image(GTK_BUTTON(m_widget
));
512 if ( image
&& !bitmap
.IsOk() )
514 gtk_container_remove(GTK_CONTAINER(m_widget
), image
);
516 else if ( !image
&& bitmap
.IsOk() )
518 image
= gtk_image_new();
519 gtk_button_set_image(GTK_BUTTON(m_widget
), image
);
521 else // image presence or absence didn't change
523 // don't invalidate best size below
527 InvalidateBestSize();
535 if ( !m_bitmaps
[which
].IsOk() )
537 // we need to install the callbacks to be notified about
538 // the button pressed state change
543 G_CALLBACK(wxgtk_button_press_callback
),
551 G_CALLBACK(wxgtk_button_released_callback
),
556 else // no valid bitmap
558 if ( m_bitmaps
[which
].IsOk() )
560 // we don't need to be notified about the button pressed
561 // state changes any more
562 g_signal_handlers_disconnect_by_func
565 (gpointer
)wxgtk_button_press_callback
,
569 g_signal_handlers_disconnect_by_func
572 (gpointer
)wxgtk_button_released_callback
,
576 // also make sure we don't remain stuck in pressed state
587 // the logic here is the same as above for State_Pressed: we need
588 // to connect the handlers if we must be notified about the changes
589 // in the button current state and we disconnect them when/if we
590 // don't need them any more
593 if ( !m_bitmaps
[which
].IsOk() )
599 G_CALLBACK(wxgtk_button_enter_callback
),
607 G_CALLBACK(wxgtk_button_leave_callback
),
612 else // no valid bitmap
614 if ( m_bitmaps
[which
].IsOk() )
616 g_signal_handlers_disconnect_by_func
619 (gpointer
)wxgtk_button_enter_callback
,
623 g_signal_handlers_disconnect_by_func
626 (gpointer
)wxgtk_button_leave_callback
,
642 Connect(wxEVT_SET_FOCUS
,
643 wxFocusEventHandler(wxButton::GTKOnFocus
));
644 Connect(wxEVT_KILL_FOCUS
,
645 wxFocusEventHandler(wxButton::GTKOnFocus
));
647 else // no valid focused bitmap
649 Disconnect(wxEVT_SET_FOCUS
,
650 wxFocusEventHandler(wxButton::GTKOnFocus
));
651 Disconnect(wxEVT_KILL_FOCUS
,
652 wxFocusEventHandler(wxButton::GTKOnFocus
));
657 // no callbacks to connect/disconnect
661 m_bitmaps
[which
] = bitmap
;
663 // update the bitmap immediately if necessary, otherwise it will be done
664 // when the bitmap for the corresponding state is needed the next time by
666 if ( bitmap
.IsOk() && which
== GTKGetCurrentState() )
668 GTKDoShowBitmap(bitmap
);
672 void wxButton::DoSetBitmapPosition(wxDirection dir
)
675 if ( !gtk_check_version(2,10,0) )
677 GtkPositionType gtkpos
;
681 wxFAIL_MSG( "invalid position" );
685 gtkpos
= GTK_POS_LEFT
;
689 gtkpos
= GTK_POS_RIGHT
;
693 gtkpos
= GTK_POS_TOP
;
697 gtkpos
= GTK_POS_BOTTOM
;
701 gtk_button_set_image_position(GTK_BUTTON(m_widget
), gtkpos
);
702 InvalidateBestSize();
707 #endif // wxUSE_BUTTON