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