]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/button.cpp
Source cleaning: -1/wxID_ANY/wxDefaultCoord, ::, !!, TRUE/true, FALSE/false, whitespa...
[wxWidgets.git] / src / gtk1 / 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 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
11#pragma implementation "button.h"
12#endif
13
14f355c2
VS
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
1e6feb95
VZ
17#include "wx/defs.h"
18
19#if wxUSE_BUTTON
20
c801d85f
KB
21#include "wx/button.h"
22
9e691f46 23#include "wx/gtk/private.h"
83624f79 24
c801d85f
KB
25//-----------------------------------------------------------------------------
26// classes
27//-----------------------------------------------------------------------------
28
29class wxButton;
30
acfd422a
RR
31//-----------------------------------------------------------------------------
32// idle system
33//-----------------------------------------------------------------------------
34
35extern void wxapp_install_idle_handler();
36extern bool g_isIdle;
37
66bd6b93
RR
38//-----------------------------------------------------------------------------
39// data
40//-----------------------------------------------------------------------------
41
42extern bool g_blockEventsOnDrag;
43
c801d85f 44//-----------------------------------------------------------------------------
e1e955e1 45// "clicked"
c801d85f
KB
46//-----------------------------------------------------------------------------
47
66bd6b93 48static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
c801d85f 49{
9e691f46 50 if (g_isIdle)
32ac755d 51 wxapp_install_idle_handler();
acfd422a 52
a2053b27 53 if (!button->m_hasVMT) return;
acfd422a 54 if (g_blockEventsOnDrag) return;
9e691f46 55
acfd422a
RR
56 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
57 event.SetEventObject(button);
58 button->GetEventHandler()->ProcessEvent(event);
6de97a3b 59}
c801d85f
KB
60
61//-----------------------------------------------------------------------------
e1e955e1
RR
62// wxButton
63//-----------------------------------------------------------------------------
64
65IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
c801d85f 66
fd0eed64 67wxButton::wxButton()
c801d85f 68{
6de97a3b 69}
c801d85f 70
fd0eed64
RR
71wxButton::~wxButton()
72{
fd0eed64
RR
73}
74
c801d85f 75bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
32c77a71 76 const wxPoint &pos, const wxSize &size,
6de97a3b 77 long style, const wxValidator& validator, const wxString &name )
c801d85f 78{
b292e2f5
RR
79 m_needParent = TRUE;
80 m_acceptsFocus = TRUE;
32c77a71 81
4dcaf11a
RR
82 if (!PreCreation( parent, pos, size ) ||
83 !CreateBase( parent, id, pos, size, style, validator, name ))
84 {
223d09f6 85 wxFAIL_MSG( wxT("wxButton creation failed") );
8ab696e0 86 return FALSE;
4dcaf11a 87 }
c801d85f 88
354aa1e3
RR
89/*
90 wxString label2( label );
91 for (size_t i = 0; i < label2.Len(); i++)
92 {
93 if (label2.GetChar(i) == wxT('&'))
8ab696e0 94 label2.SetChar(i,wxT('_'));
354aa1e3 95 }
9e691f46 96
354aa1e3
RR
97 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
98 gtk_widget_show( accel_label );
9e691f46 99
354aa1e3
RR
100 m_widget = gtk_button_new();
101 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
9e691f46 102
354aa1e3 103 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
9e691f46 104
354aa1e3
RR
105 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
106 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
9e691f46 107
354aa1e3
RR
108 wxControl::SetLabel( label );
109*/
9e691f46 110
b2fcfd94 111#ifdef __WXGTK20__
4fa87bd9
VS
112 if (m_widget == NULL) // may be !=NULL if stock form of Create was used
113 m_widget = gtk_button_new_with_mnemonic("");
b2fcfd94 114#else
354aa1e3 115 m_widget = gtk_button_new_with_label("");
b2fcfd94 116#endif
354aa1e3 117
2e8613b7
RR
118 float x_alignment = 0.5;
119 if (HasFlag(wxBU_LEFT))
120 x_alignment = 0.0;
121 else if (HasFlag(wxBU_RIGHT))
122 x_alignment = 1.0;
123
124 float y_alignment = 0.5;
125 if (HasFlag(wxBU_TOP))
126 y_alignment = 0.0;
127 else if (HasFlag(wxBU_BOTTOM))
128 y_alignment = 1.0;
129
a650b927 130#if GTK_CHECK_VERSION(2,4,0)
4fa87bd9
VS
131 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
132#else
a650b927
VS
133 if (GTK_IS_MISC(BUTTON_CHILD(m_widget)))
134 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)),
135 x_alignment, y_alignment);
4fa87bd9 136#endif
a696db45 137
4fa87bd9
VS
138 if (!label.empty())
139 SetLabel(label);
354aa1e3 140
de1c750f
RR
141 if (style & wxNO_BORDER)
142 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 143
b292e2f5
RR
144 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
145 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
c801d85f 146
f03fc89f 147 m_parent->DoAddChild( this );
9e691f46 148
abdeb9e7 149 PostCreation(size);
db434467 150
4fa87bd9
VS
151 return true;
152}
153
154bool wxButton::Create(wxWindow *parent, wxWindowID id, wxStockItemID stock,
155 const wxString& descriptiveLabel,
156 const wxPoint& pos, long style,
157 const wxValidator& validator, const wxString& name)
158{
159#ifdef __WXGTK20__
c20889ab
VS
160 const char *gtkstock = wxStockItemToGTK(stock);
161 if (gtkstock)
162 {
163 m_widget = gtk_button_new_from_stock(gtkstock);
164 return Create(parent, id, wxEmptyString,
165 pos, wxDefaultSize, style, validator, name);
166 }
167 else
168 {
169 // not supported by this GTK+ version
170 return Create(parent, id, wxGetStockItemLabel(stock),
171 pos, wxDefaultSize, style, validator, name);
172 }
4fa87bd9
VS
173#else
174 return CreateStock(parent, id, stock, descriptiveLabel,
175 pos, style, validator, name);
176#endif
6de97a3b 177}
32c77a71 178
da048e3d 179void wxButton::SetDefault()
c801d85f 180{
97149f18
RD
181 wxWindow *parent = GetParent();
182 wxCHECK_RET( parent, _T("button without parent?") );
183
a00b2d0a 184 parent->SetDefaultItem(this);
97149f18 185
3502e687
RR
186 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
187 gtk_widget_grab_default( m_widget );
9e691f46 188
3502e687 189 SetSize( m_x, m_y, m_width, m_height );
6de97a3b 190}
c801d85f 191
ebea0891 192/* static */
4fa87bd9 193wxSize wxButtonBase::GetDefaultSize()
8dbf4589 194{
4fa87bd9
VS
195#ifdef __WXGTK20__
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#else
8dbf4589 227 return wxSize(80,26);
4fa87bd9 228#endif
8dbf4589
RR
229}
230
c801d85f
KB
231void wxButton::SetLabel( const wxString &label )
232{
223d09f6 233 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 234
b292e2f5 235 wxControl::SetLabel( label );
9e691f46 236
eaafd2f8
VS
237#ifdef __WXGTK20__
238 wxString label2 = PrepareLabelMnemonics( label );
b2fcfd94 239 gtk_button_set_label( GTK_BUTTON(m_widget), wxGTK_CONV(label2) );
eaafd2f8 240#else
fab591c5 241 gtk_label_set( GTK_LABEL( BUTTON_CHILD(m_widget) ), wxGTK_CONV( GetLabel() ) );
eaafd2f8 242#endif
6de97a3b 243}
c801d85f 244
f03fc89f 245bool wxButton::Enable( bool enable )
a9c96bcc 246{
f03fc89f
VZ
247 if ( !wxControl::Enable( enable ) )
248 return FALSE;
9e691f46
VZ
249
250 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f
VZ
251
252 return TRUE;
a9c96bcc
RR
253}
254
2b5f62a0
VZ
255bool wxButton::IsOwnGtkWindow( GdkWindow *window )
256{
257#ifdef __WXGTK20__
258 return GTK_BUTTON(m_widget)->event_window;
259#else
260 return (window == m_widget->window);
261#endif
262}
263
f40fdaa3 264void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 265{
f40fdaa3
VS
266 gtk_widget_modify_style(m_widget, style);
267 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
a81258be 268}
db434467
RR
269
270wxSize wxButton::DoGetBestSize() const
271{
4f819fe4
VZ
272 // the default button in wxGTK is bigger than the other ones because of an
273 // extra border around it, but we don't want to take it into account in
274 // our size calculations (otherwsie the result is visually ugly), so
275 // always return the size of non default button from here
276 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
277 if ( isDefault )
278 {
279 // temporarily unset default flag
280 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
281 }
282
db434467 283 wxSize ret( wxControl::DoGetBestSize() );
9e691f46 284
4f819fe4
VZ
285 if ( isDefault )
286 {
287 // set it back again
288 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
289 }
290
391ddf40
RD
291#ifndef __WXGTK20__
292 ret.x += 10; // add a few pixels for sloppy (but common) themes
293#endif
294
8ab696e0
RR
295 if (!HasFlag(wxBU_EXACTFIT))
296 {
4fa87bd9
VS
297 wxSize defaultSize = GetDefaultSize();
298 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
299 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
8ab696e0 300 }
9e691f46 301
9f884528 302 CacheBestSize(ret);
db434467
RR
303 return ret;
304}
305
9d522606
RD
306// static
307wxVisualAttributes
308wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
309{
310 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
311}
312
1e6feb95
VZ
313#endif // wxUSE_BUTTON
314