1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "button.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
21 #include "wx/button.h"
22 #include "wx/stockitem.h"
24 #include "wx/gtk/private.h"
25 #include "wx/gtk/win_gtk.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern void wxapp_install_idle_handler();
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 extern bool g_blockEventsOnDrag
;
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
51 static void gtk_button_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxButton
*button
)
54 wxapp_install_idle_handler();
56 if (!button
->m_hasVMT
) return;
57 if (g_blockEventsOnDrag
) return;
59 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, button
->GetId());
60 event
.SetEventObject(button
);
61 button
->GetEventHandler()->ProcessEvent(event
);
65 //-----------------------------------------------------------------------------
66 // "style_set" from m_widget
67 //-----------------------------------------------------------------------------
70 gtk_button_style_set_callback( GtkWidget
*m_widget
, GtkStyle
*WXUNUSED(style
), wxButton
*win
)
73 wxapp_install_idle_handler();
78 int bottom_border
= 0;
80 /* the default button has a border around it */
81 if (GTK_WIDGET_CAN_DEFAULT(m_widget
))
84 GtkBorder
*default_border
= NULL
;
85 gtk_widget_style_get( m_widget
, "default_border", &default_border
, NULL
);
88 left_border
+= default_border
->left
;
89 right_border
+= default_border
->right
;
90 top_border
+= default_border
->top
;
91 bottom_border
+= default_border
->bottom
;
92 g_free( default_border
);
100 win
->DoMoveWindow( win
->m_x
-top_border
,
101 win
->m_y
-left_border
,
102 win
->m_width
+left_border
+right_border
,
103 win
->m_height
+top_border
+bottom_border
);
109 //-----------------------------------------------------------------------------
111 //-----------------------------------------------------------------------------
113 IMPLEMENT_DYNAMIC_CLASS(wxButton
,wxControl
)
119 wxButton::~wxButton()
123 bool wxButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
&label
,
124 const wxPoint
&pos
, const wxSize
&size
,
125 long style
, const wxValidator
& validator
, const wxString
&name
)
128 m_acceptsFocus
= TRUE
;
130 if (!PreCreation( parent
, pos
, size
) ||
131 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
133 wxFAIL_MSG( wxT("wxButton creation failed") );
138 wxString label2( label );
139 for (size_t i = 0; i < label2.Len(); i++)
141 if (label2.GetChar(i) == wxT('&'))
142 label2.SetChar(i,wxT('_'));
145 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
146 gtk_widget_show( accel_label );
148 m_widget = gtk_button_new();
149 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
151 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
153 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
154 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
156 wxControl::SetLabel( label );
160 m_widget
= gtk_button_new_with_mnemonic("");
162 m_widget
= gtk_button_new_with_label("");
165 float x_alignment
= 0.5;
166 if (HasFlag(wxBU_LEFT
))
168 else if (HasFlag(wxBU_RIGHT
))
171 float y_alignment
= 0.5;
172 if (HasFlag(wxBU_TOP
))
174 else if (HasFlag(wxBU_BOTTOM
))
178 if (!gtk_check_version(2,4,0))
180 gtk_button_set_alignment(GTK_BUTTON(m_widget
), x_alignment
, y_alignment
);
185 if (GTK_IS_MISC(BUTTON_CHILD(m_widget
)))
186 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget
)),
187 x_alignment
, y_alignment
);
192 if (style
& wxNO_BORDER
)
193 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
195 gtk_signal_connect_after( GTK_OBJECT(m_widget
), "clicked",
196 GTK_SIGNAL_FUNC(gtk_button_clicked_callback
), (gpointer
*)this );
198 gtk_signal_connect_after( GTK_OBJECT(m_widget
), "style_set",
199 GTK_SIGNAL_FUNC(gtk_button_style_set_callback
), (gpointer
*) this );
201 m_parent
->DoAddChild( this );
209 void wxButton::SetDefault()
211 wxWindow
*parent
= GetParent();
212 wxCHECK_RET( parent
, _T("button without parent?") );
214 parent
->SetDefaultItem(this);
216 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
217 gtk_widget_grab_default( m_widget
);
219 // resize for default border
220 gtk_button_style_set_callback( m_widget
, NULL
, this );
224 wxSize
wxButtonBase::GetDefaultSize()
227 static wxSize size
= wxDefaultSize
;
228 if (size
== wxDefaultSize
)
230 // NB: Default size of buttons should be same as size of stock
231 // buttons as used in most GTK+ apps. Unfortunately it's a little
232 // tricky to obtain this size: stock button's size may be smaller
233 // than size of button in GtkButtonBox and vice versa,
234 // GtkButtonBox's minimal button size may be smaller than stock
235 // button's size. We have to retrieve both values and combine them.
237 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
238 GtkWidget
*box
= gtk_hbutton_box_new();
239 GtkWidget
*btn
= gtk_button_new_from_stock(GTK_STOCK_CANCEL
);
240 gtk_container_add(GTK_CONTAINER(box
), btn
);
241 gtk_container_add(GTK_CONTAINER(wnd
), box
);
243 gtk_widget_size_request(btn
, &req
);
245 gint minwidth
, minheight
;
246 gtk_widget_style_get(box
,
247 "child-min-width", &minwidth
,
248 "child-min-height", &minheight
,
251 size
.x
= wxMax(minwidth
, req
.width
);
252 size
.y
= wxMax(minheight
, req
.height
);
254 gtk_widget_destroy(wnd
);
258 return wxSize(80,26);
262 void wxButton::SetLabel( const wxString
&lbl
)
264 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid button") );
268 if (label
.empty() && wxIsStockID(m_windowId
))
269 label
= wxGetStockLabel(m_windowId
);
271 wxControl::SetLabel(label
);
274 if (wxIsStockID(m_windowId
) && wxIsStockLabel(m_windowId
, label
))
276 const char *stock
= wxGetStockGtkID(m_windowId
);
279 gtk_button_set_label(GTK_BUTTON(m_widget
), stock
);
280 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), TRUE
);
285 wxString label2
= PrepareLabelMnemonics(label
);
286 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(label2
));
287 gtk_button_set_use_stock(GTK_BUTTON(m_widget
), FALSE
);
289 ApplyWidgetStyle( false );
292 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget
)), wxGTK_CONV(GetLabel()));
296 bool wxButton::Enable( bool enable
)
298 if ( !wxControl::Enable( enable
) )
301 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
306 bool wxButton::IsOwnGtkWindow( GdkWindow
*window
)
309 return GTK_BUTTON(m_widget
)->event_window
;
311 return (window
== m_widget
->window
);
315 void wxButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
317 gtk_widget_modify_style(m_widget
, style
);
318 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
321 wxSize
wxButton::DoGetBestSize() const
323 // the default button in wxGTK is bigger than the other ones because of an
324 // extra border around it, but we don't want to take it into account in
325 // our size calculations (otherwsie the result is visually ugly), so
326 // always return the size of non default button from here
327 const bool isDefault
= GTK_WIDGET_HAS_DEFAULT(m_widget
);
330 // temporarily unset default flag
331 GTK_WIDGET_UNSET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
334 wxSize
ret( wxControl::DoGetBestSize() );
339 GTK_WIDGET_SET_FLAGS( m_widget
, GTK_CAN_DEFAULT
);
343 ret
.x
+= 10; // add a few pixels for sloppy (but common) themes
346 if (!HasFlag(wxBU_EXACTFIT
))
348 wxSize defaultSize
= GetDefaultSize();
349 if (ret
.x
< defaultSize
.x
) ret
.x
= defaultSize
.x
;
350 if (ret
.y
< defaultSize
.y
) ret
.y
= defaultSize
.y
;
359 wxButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
361 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
364 #endif // wxUSE_BUTTON