]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/button.cpp
Not all implementations of wxMDIChildFrame derive from wxFrame
[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 21#include "wx/button.h"
5f7bcb48 22#include "wx/stockitem.h"
c801d85f 23
9e691f46 24#include "wx/gtk/private.h"
f893066b 25#include "wx/gtk/win_gtk.h"
83624f79 26
c801d85f
KB
27//-----------------------------------------------------------------------------
28// classes
29//-----------------------------------------------------------------------------
30
31class wxButton;
32
acfd422a
RR
33//-----------------------------------------------------------------------------
34// idle system
35//-----------------------------------------------------------------------------
36
37extern void wxapp_install_idle_handler();
38extern bool g_isIdle;
39
66bd6b93
RR
40//-----------------------------------------------------------------------------
41// data
42//-----------------------------------------------------------------------------
43
44extern bool g_blockEventsOnDrag;
45
c801d85f 46//-----------------------------------------------------------------------------
e1e955e1 47// "clicked"
c801d85f
KB
48//-----------------------------------------------------------------------------
49
865bb325 50extern "C" {
66bd6b93 51static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
c801d85f 52{
9e691f46 53 if (g_isIdle)
32ac755d 54 wxapp_install_idle_handler();
acfd422a 55
a2053b27 56 if (!button->m_hasVMT) return;
acfd422a 57 if (g_blockEventsOnDrag) return;
9e691f46 58
acfd422a
RR
59 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
60 event.SetEventObject(button);
61 button->GetEventHandler()->ProcessEvent(event);
6de97a3b 62}
865bb325 63}
c801d85f 64
a90c0600
RR
65//-----------------------------------------------------------------------------
66// "style_set" from m_widget
67//-----------------------------------------------------------------------------
68
69static gint
70gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win )
71{
72 if (g_isIdle)
73 wxapp_install_idle_handler();
74
f893066b
RR
75 int left_border = 0;
76 int right_border = 0;
77 int top_border = 0;
78 int bottom_border = 0;
79
80 /* the default button has a border around it */
81 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
82 {
83#ifdef __WXGTK20__
84 GtkBorder *default_border = NULL;
85 gtk_widget_style_get( m_widget, "default_border", &default_border, NULL );
86 if (default_border)
87 {
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 );
93 }
94#else
95 left_border = 6;
96 right_border = 6;
97 top_border = 6;
98 bottom_border = 5;
99#endif
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 );
104 }
a90c0600
RR
105
106 return FALSE;
107}
108
c801d85f 109//-----------------------------------------------------------------------------
e1e955e1
RR
110// wxButton
111//-----------------------------------------------------------------------------
112
113IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
c801d85f 114
fd0eed64 115wxButton::wxButton()
c801d85f 116{
6de97a3b 117}
c801d85f 118
fd0eed64
RR
119wxButton::~wxButton()
120{
fd0eed64
RR
121}
122
c801d85f 123bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
32c77a71 124 const wxPoint &pos, const wxSize &size,
6de97a3b 125 long style, const wxValidator& validator, const wxString &name )
c801d85f 126{
b292e2f5
RR
127 m_needParent = TRUE;
128 m_acceptsFocus = TRUE;
32c77a71 129
4dcaf11a
RR
130 if (!PreCreation( parent, pos, size ) ||
131 !CreateBase( parent, id, pos, size, style, validator, name ))
132 {
223d09f6 133 wxFAIL_MSG( wxT("wxButton creation failed") );
8ab696e0 134 return FALSE;
4dcaf11a 135 }
c801d85f 136
354aa1e3
RR
137/*
138 wxString label2( label );
139 for (size_t i = 0; i < label2.Len(); i++)
140 {
141 if (label2.GetChar(i) == wxT('&'))
8ab696e0 142 label2.SetChar(i,wxT('_'));
354aa1e3 143 }
9e691f46 144
354aa1e3
RR
145 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
146 gtk_widget_show( accel_label );
9e691f46 147
354aa1e3
RR
148 m_widget = gtk_button_new();
149 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
9e691f46 150
354aa1e3 151 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
9e691f46 152
354aa1e3
RR
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) );
9e691f46 155
354aa1e3
RR
156 wxControl::SetLabel( label );
157*/
9e691f46 158
b2fcfd94 159#ifdef __WXGTK20__
5f7bcb48 160 m_widget = gtk_button_new_with_mnemonic("");
b2fcfd94 161#else
354aa1e3 162 m_widget = gtk_button_new_with_label("");
b2fcfd94 163#endif
354aa1e3 164
2e8613b7
RR
165 float x_alignment = 0.5;
166 if (HasFlag(wxBU_LEFT))
167 x_alignment = 0.0;
168 else if (HasFlag(wxBU_RIGHT))
169 x_alignment = 1.0;
170
171 float y_alignment = 0.5;
172 if (HasFlag(wxBU_TOP))
173 y_alignment = 0.0;
174 else if (HasFlag(wxBU_BOTTOM))
175 y_alignment = 1.0;
176
77f70672
RR
177#if __WXGTK24__
178 if (!gtk_check_version(2,4,0))
179 {
180 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
181 }
182 else
4fa87bd9 183#endif
77f70672
RR
184 {
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);
188 }
a696db45 189
5f7bcb48 190 SetLabel(label);
354aa1e3 191
de1c750f
RR
192 if (style & wxNO_BORDER)
193 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 194
58b907f6 195 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
b292e2f5 196 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
c801d85f 197
a90c0600
RR
198 gtk_signal_connect_after( GTK_OBJECT(m_widget), "style_set",
199 GTK_SIGNAL_FUNC(gtk_button_style_set_callback), (gpointer*) this );
200
f03fc89f 201 m_parent->DoAddChild( this );
9e691f46 202
abdeb9e7 203 PostCreation(size);
db434467 204
4fa87bd9
VS
205 return true;
206}
207
32c77a71 208
da048e3d 209void wxButton::SetDefault()
c801d85f 210{
97149f18
RD
211 wxWindow *parent = GetParent();
212 wxCHECK_RET( parent, _T("button without parent?") );
213
a00b2d0a 214 parent->SetDefaultItem(this);
97149f18 215
3502e687
RR
216 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
217 gtk_widget_grab_default( m_widget );
f893066b
RR
218
219 // resize for default border
220 gtk_button_style_set_callback( m_widget, NULL, this );
6de97a3b 221}
c801d85f 222
ebea0891 223/* static */
4fa87bd9 224wxSize wxButtonBase::GetDefaultSize()
8dbf4589 225{
4fa87bd9
VS
226#ifdef __WXGTK20__
227 static wxSize size = wxDefaultSize;
228 if (size == wxDefaultSize)
229 {
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.
236
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);
242 GtkRequisition req;
243 gtk_widget_size_request(btn, &req);
244
245 gint minwidth, minheight;
246 gtk_widget_style_get(box,
247 "child-min-width", &minwidth,
248 "child-min-height", &minheight,
249 NULL);
250
251 size.x = wxMax(minwidth, req.width);
252 size.y = wxMax(minheight, req.height);
253
254 gtk_widget_destroy(wnd);
255 }
256 return size;
257#else
8dbf4589 258 return wxSize(80,26);
4fa87bd9 259#endif
8dbf4589
RR
260}
261
5f7bcb48 262void wxButton::SetLabel( const wxString &lbl )
c801d85f 263{
223d09f6 264 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 265
5f7bcb48
VS
266 wxString label(lbl);
267
5f7bcb48
VS
268 if (label.empty() && wxIsStockID(m_windowId))
269 label = wxGetStockLabel(m_windowId);
5f7bcb48
VS
270
271 wxControl::SetLabel(label);
9e691f46 272
eaafd2f8 273#ifdef __WXGTK20__
5f7bcb48
VS
274 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
275 {
276 const char *stock = wxGetStockGtkID(m_windowId);
277 if (stock)
278 {
279 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
280 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
b04683b1 281 return;
5f7bcb48 282 }
5f7bcb48
VS
283 }
284
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);
19ac2f44
RR
288
289 ApplyWidgetStyle( false );
290
eaafd2f8 291#else
5f7bcb48 292 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(GetLabel()));
eaafd2f8 293#endif
6de97a3b 294}
c801d85f 295
f03fc89f 296bool wxButton::Enable( bool enable )
a9c96bcc 297{
f03fc89f
VZ
298 if ( !wxControl::Enable( enable ) )
299 return FALSE;
9e691f46
VZ
300
301 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f
VZ
302
303 return TRUE;
a9c96bcc
RR
304}
305
2b5f62a0
VZ
306bool wxButton::IsOwnGtkWindow( GdkWindow *window )
307{
308#ifdef __WXGTK20__
309 return GTK_BUTTON(m_widget)->event_window;
310#else
311 return (window == m_widget->window);
312#endif
313}
314
f40fdaa3 315void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 316{
f40fdaa3
VS
317 gtk_widget_modify_style(m_widget, style);
318 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
a81258be 319}
db434467
RR
320
321wxSize wxButton::DoGetBestSize() const
322{
4f819fe4
VZ
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);
328 if ( isDefault )
329 {
330 // temporarily unset default flag
331 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
332 }
333
db434467 334 wxSize ret( wxControl::DoGetBestSize() );
9e691f46 335
4f819fe4
VZ
336 if ( isDefault )
337 {
338 // set it back again
339 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
340 }
341
391ddf40
RD
342#ifndef __WXGTK20__
343 ret.x += 10; // add a few pixels for sloppy (but common) themes
344#endif
345
8ab696e0
RR
346 if (!HasFlag(wxBU_EXACTFIT))
347 {
4fa87bd9
VS
348 wxSize defaultSize = GetDefaultSize();
349 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
350 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
8ab696e0 351 }
9e691f46 352
9f884528 353 CacheBestSize(ret);
db434467
RR
354 return ret;
355}
356
9d522606
RD
357// static
358wxVisualAttributes
359wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
360{
361 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
362}
363
1e6feb95
VZ
364#endif // wxUSE_BUTTON
365