]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/button.cpp
Get/SetTitle only for wxTopLevelWindow (wxMotif part).
[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();
70
f893066b
RR
71 int left_border = 0;
72 int right_border = 0;
73 int top_border = 0;
74 int bottom_border = 0;
75
76 /* the default button has a border around it */
77 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
78 {
79#ifdef __WXGTK20__
80 GtkBorder *default_border = NULL;
81 gtk_widget_style_get( m_widget, "default_border", &default_border, NULL );
82 if (default_border)
83 {
84 left_border += default_border->left;
85 right_border += default_border->right;
86 top_border += default_border->top;
87 bottom_border += default_border->bottom;
88 g_free( default_border );
89 }
90#else
91 left_border = 6;
92 right_border = 6;
93 top_border = 6;
94 bottom_border = 5;
95#endif
96 win->DoMoveWindow( win->m_x-top_border,
97 win->m_y-left_border,
98 win->m_width+left_border+right_border,
99 win->m_height+top_border+bottom_border );
100 }
a90c0600
RR
101
102 return FALSE;
103}
104
c801d85f 105//-----------------------------------------------------------------------------
e1e955e1
RR
106// wxButton
107//-----------------------------------------------------------------------------
108
109IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
c801d85f 110
fd0eed64 111wxButton::wxButton()
c801d85f 112{
6de97a3b 113}
c801d85f 114
fd0eed64
RR
115wxButton::~wxButton()
116{
fd0eed64
RR
117}
118
c801d85f 119bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
32c77a71 120 const wxPoint &pos, const wxSize &size,
6de97a3b 121 long style, const wxValidator& validator, const wxString &name )
c801d85f 122{
b292e2f5
RR
123 m_needParent = TRUE;
124 m_acceptsFocus = TRUE;
32c77a71 125
4dcaf11a
RR
126 if (!PreCreation( parent, pos, size ) ||
127 !CreateBase( parent, id, pos, size, style, validator, name ))
128 {
223d09f6 129 wxFAIL_MSG( wxT("wxButton creation failed") );
8ab696e0 130 return FALSE;
4dcaf11a 131 }
c801d85f 132
354aa1e3
RR
133/*
134 wxString label2( label );
135 for (size_t i = 0; i < label2.Len(); i++)
136 {
137 if (label2.GetChar(i) == wxT('&'))
8ab696e0 138 label2.SetChar(i,wxT('_'));
354aa1e3 139 }
9e691f46 140
354aa1e3
RR
141 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
142 gtk_widget_show( accel_label );
9e691f46 143
354aa1e3
RR
144 m_widget = gtk_button_new();
145 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
9e691f46 146
354aa1e3 147 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
9e691f46 148
354aa1e3
RR
149 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
150 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
9e691f46 151
354aa1e3
RR
152 wxControl::SetLabel( label );
153*/
9e691f46 154
b2fcfd94 155#ifdef __WXGTK20__
5f7bcb48 156 m_widget = gtk_button_new_with_mnemonic("");
b2fcfd94 157#else
354aa1e3 158 m_widget = gtk_button_new_with_label("");
b2fcfd94 159#endif
354aa1e3 160
2e8613b7
RR
161 float x_alignment = 0.5;
162 if (HasFlag(wxBU_LEFT))
163 x_alignment = 0.0;
164 else if (HasFlag(wxBU_RIGHT))
165 x_alignment = 1.0;
166
167 float y_alignment = 0.5;
168 if (HasFlag(wxBU_TOP))
169 y_alignment = 0.0;
170 else if (HasFlag(wxBU_BOTTOM))
171 y_alignment = 1.0;
172
3dc786e0 173#ifdef __WXGTK24__
77f70672
RR
174 if (!gtk_check_version(2,4,0))
175 {
176 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
177 }
178 else
4fa87bd9 179#endif
77f70672
RR
180 {
181 if (GTK_IS_MISC(BUTTON_CHILD(m_widget)))
182 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)),
183 x_alignment, y_alignment);
184 }
a696db45 185
5f7bcb48 186 SetLabel(label);
354aa1e3 187
de1c750f
RR
188 if (style & wxNO_BORDER)
189 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 190
58b907f6 191 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
b292e2f5 192 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
c801d85f 193
a90c0600
RR
194 gtk_signal_connect_after( GTK_OBJECT(m_widget), "style_set",
195 GTK_SIGNAL_FUNC(gtk_button_style_set_callback), (gpointer*) this );
196
f03fc89f 197 m_parent->DoAddChild( this );
9e691f46 198
abdeb9e7 199 PostCreation(size);
db434467 200
4fa87bd9
VS
201 return true;
202}
203
32c77a71 204
da048e3d 205void wxButton::SetDefault()
c801d85f 206{
97149f18
RD
207 wxWindow *parent = GetParent();
208 wxCHECK_RET( parent, _T("button without parent?") );
209
a00b2d0a 210 parent->SetDefaultItem(this);
97149f18 211
3502e687
RR
212 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
213 gtk_widget_grab_default( m_widget );
f893066b
RR
214
215 // resize for default border
216 gtk_button_style_set_callback( m_widget, NULL, this );
6de97a3b 217}
c801d85f 218
ebea0891 219/* static */
4fa87bd9 220wxSize wxButtonBase::GetDefaultSize()
8dbf4589 221{
4fa87bd9
VS
222#ifdef __WXGTK20__
223 static wxSize size = wxDefaultSize;
224 if (size == wxDefaultSize)
225 {
226 // NB: Default size of buttons should be same as size of stock
227 // buttons as used in most GTK+ apps. Unfortunately it's a little
228 // tricky to obtain this size: stock button's size may be smaller
229 // than size of button in GtkButtonBox and vice versa,
230 // GtkButtonBox's minimal button size may be smaller than stock
231 // button's size. We have to retrieve both values and combine them.
232
233 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
234 GtkWidget *box = gtk_hbutton_box_new();
235 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
236 gtk_container_add(GTK_CONTAINER(box), btn);
237 gtk_container_add(GTK_CONTAINER(wnd), box);
238 GtkRequisition req;
239 gtk_widget_size_request(btn, &req);
240
241 gint minwidth, minheight;
242 gtk_widget_style_get(box,
243 "child-min-width", &minwidth,
244 "child-min-height", &minheight,
245 NULL);
246
247 size.x = wxMax(minwidth, req.width);
248 size.y = wxMax(minheight, req.height);
249
250 gtk_widget_destroy(wnd);
251 }
252 return size;
253#else
8dbf4589 254 return wxSize(80,26);
4fa87bd9 255#endif
8dbf4589
RR
256}
257
5f7bcb48 258void wxButton::SetLabel( const wxString &lbl )
c801d85f 259{
223d09f6 260 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 261
5f7bcb48
VS
262 wxString label(lbl);
263
5f7bcb48
VS
264 if (label.empty() && wxIsStockID(m_windowId))
265 label = wxGetStockLabel(m_windowId);
5f7bcb48
VS
266
267 wxControl::SetLabel(label);
9e691f46 268
eaafd2f8 269#ifdef __WXGTK20__
5f7bcb48
VS
270 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
271 {
272 const char *stock = wxGetStockGtkID(m_windowId);
273 if (stock)
274 {
275 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
276 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
b04683b1 277 return;
5f7bcb48 278 }
5f7bcb48
VS
279 }
280
281 wxString label2 = PrepareLabelMnemonics(label);
282 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(label2));
283 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
19ac2f44
RR
284
285 ApplyWidgetStyle( false );
286
eaafd2f8 287#else
5f7bcb48 288 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(GetLabel()));
eaafd2f8 289#endif
6de97a3b 290}
c801d85f 291
f03fc89f 292bool wxButton::Enable( bool enable )
a9c96bcc 293{
f03fc89f
VZ
294 if ( !wxControl::Enable( enable ) )
295 return FALSE;
9e691f46
VZ
296
297 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f
VZ
298
299 return TRUE;
a9c96bcc
RR
300}
301
2b5f62a0
VZ
302bool wxButton::IsOwnGtkWindow( GdkWindow *window )
303{
304#ifdef __WXGTK20__
305 return GTK_BUTTON(m_widget)->event_window;
306#else
307 return (window == m_widget->window);
308#endif
309}
310
f40fdaa3 311void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 312{
f40fdaa3
VS
313 gtk_widget_modify_style(m_widget, style);
314 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
a81258be 315}
db434467
RR
316
317wxSize wxButton::DoGetBestSize() const
318{
4f819fe4
VZ
319 // the default button in wxGTK is bigger than the other ones because of an
320 // extra border around it, but we don't want to take it into account in
321 // our size calculations (otherwsie the result is visually ugly), so
322 // always return the size of non default button from here
323 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
324 if ( isDefault )
325 {
326 // temporarily unset default flag
327 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
328 }
329
db434467 330 wxSize ret( wxControl::DoGetBestSize() );
9e691f46 331
4f819fe4
VZ
332 if ( isDefault )
333 {
334 // set it back again
335 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
336 }
337
391ddf40
RD
338#ifndef __WXGTK20__
339 ret.x += 10; // add a few pixels for sloppy (but common) themes
340#endif
341
8ab696e0
RR
342 if (!HasFlag(wxBU_EXACTFIT))
343 {
4fa87bd9
VS
344 wxSize defaultSize = GetDefaultSize();
345 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
346 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
8ab696e0 347 }
9e691f46 348
9f884528 349 CacheBestSize(ret);
db434467
RR
350 return ret;
351}
352
9d522606
RD
353// static
354wxVisualAttributes
355wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
356{
357 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
358}
359
1e6feb95
VZ
360#endif // wxUSE_BUTTON
361