]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/button.cpp
end label editing if the control loses focus (slightly modified patch 1084592)
[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"
83624f79 25
c801d85f
KB
26//-----------------------------------------------------------------------------
27// classes
28//-----------------------------------------------------------------------------
29
30class wxButton;
31
acfd422a
RR
32//-----------------------------------------------------------------------------
33// idle system
34//-----------------------------------------------------------------------------
35
36extern void wxapp_install_idle_handler();
37extern bool g_isIdle;
38
66bd6b93
RR
39//-----------------------------------------------------------------------------
40// data
41//-----------------------------------------------------------------------------
42
43extern bool g_blockEventsOnDrag;
44
c801d85f 45//-----------------------------------------------------------------------------
e1e955e1 46// "clicked"
c801d85f
KB
47//-----------------------------------------------------------------------------
48
66bd6b93 49static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
c801d85f 50{
9e691f46 51 if (g_isIdle)
32ac755d 52 wxapp_install_idle_handler();
acfd422a 53
a2053b27 54 if (!button->m_hasVMT) return;
acfd422a 55 if (g_blockEventsOnDrag) return;
9e691f46 56
acfd422a
RR
57 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
58 event.SetEventObject(button);
59 button->GetEventHandler()->ProcessEvent(event);
6de97a3b 60}
c801d85f
KB
61
62//-----------------------------------------------------------------------------
e1e955e1
RR
63// wxButton
64//-----------------------------------------------------------------------------
65
66IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
c801d85f 67
fd0eed64 68wxButton::wxButton()
c801d85f 69{
6de97a3b 70}
c801d85f 71
fd0eed64
RR
72wxButton::~wxButton()
73{
fd0eed64
RR
74}
75
c801d85f 76bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
32c77a71 77 const wxPoint &pos, const wxSize &size,
6de97a3b 78 long style, const wxValidator& validator, const wxString &name )
c801d85f 79{
b292e2f5
RR
80 m_needParent = TRUE;
81 m_acceptsFocus = TRUE;
32c77a71 82
4dcaf11a
RR
83 if (!PreCreation( parent, pos, size ) ||
84 !CreateBase( parent, id, pos, size, style, validator, name ))
85 {
223d09f6 86 wxFAIL_MSG( wxT("wxButton creation failed") );
8ab696e0 87 return FALSE;
4dcaf11a 88 }
c801d85f 89
354aa1e3
RR
90/*
91 wxString label2( label );
92 for (size_t i = 0; i < label2.Len(); i++)
93 {
94 if (label2.GetChar(i) == wxT('&'))
8ab696e0 95 label2.SetChar(i,wxT('_'));
354aa1e3 96 }
9e691f46 97
354aa1e3
RR
98 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
99 gtk_widget_show( accel_label );
9e691f46 100
354aa1e3
RR
101 m_widget = gtk_button_new();
102 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
9e691f46 103
354aa1e3 104 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
9e691f46 105
354aa1e3
RR
106 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
107 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
9e691f46 108
354aa1e3
RR
109 wxControl::SetLabel( label );
110*/
9e691f46 111
b2fcfd94 112#ifdef __WXGTK20__
5f7bcb48 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
77f70672
RR
130#if __WXGTK24__
131 if (!gtk_check_version(2,4,0))
132 {
133 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
134 }
135 else
4fa87bd9 136#endif
77f70672
RR
137 {
138 if (GTK_IS_MISC(BUTTON_CHILD(m_widget)))
139 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)),
140 x_alignment, y_alignment);
141 }
a696db45 142
5f7bcb48 143 SetLabel(label);
354aa1e3 144
de1c750f
RR
145 if (style & wxNO_BORDER)
146 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 147
b292e2f5
RR
148 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
149 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
c801d85f 150
f03fc89f 151 m_parent->DoAddChild( this );
9e691f46 152
abdeb9e7 153 PostCreation(size);
db434467 154
4fa87bd9
VS
155 return true;
156}
157
32c77a71 158
da048e3d 159void wxButton::SetDefault()
c801d85f 160{
97149f18
RD
161 wxWindow *parent = GetParent();
162 wxCHECK_RET( parent, _T("button without parent?") );
163
a00b2d0a 164 parent->SetDefaultItem(this);
97149f18 165
3502e687
RR
166 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
167 gtk_widget_grab_default( m_widget );
9e691f46 168
3502e687 169 SetSize( m_x, m_y, m_width, m_height );
6de97a3b 170}
c801d85f 171
ebea0891 172/* static */
4fa87bd9 173wxSize wxButtonBase::GetDefaultSize()
8dbf4589 174{
4fa87bd9
VS
175#ifdef __WXGTK20__
176 static wxSize size = wxDefaultSize;
177 if (size == wxDefaultSize)
178 {
179 // NB: Default size of buttons should be same as size of stock
180 // buttons as used in most GTK+ apps. Unfortunately it's a little
181 // tricky to obtain this size: stock button's size may be smaller
182 // than size of button in GtkButtonBox and vice versa,
183 // GtkButtonBox's minimal button size may be smaller than stock
184 // button's size. We have to retrieve both values and combine them.
185
186 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
187 GtkWidget *box = gtk_hbutton_box_new();
188 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
189 gtk_container_add(GTK_CONTAINER(box), btn);
190 gtk_container_add(GTK_CONTAINER(wnd), box);
191 GtkRequisition req;
192 gtk_widget_size_request(btn, &req);
193
194 gint minwidth, minheight;
195 gtk_widget_style_get(box,
196 "child-min-width", &minwidth,
197 "child-min-height", &minheight,
198 NULL);
199
200 size.x = wxMax(minwidth, req.width);
201 size.y = wxMax(minheight, req.height);
202
203 gtk_widget_destroy(wnd);
204 }
205 return size;
206#else
8dbf4589 207 return wxSize(80,26);
4fa87bd9 208#endif
8dbf4589
RR
209}
210
5f7bcb48 211void wxButton::SetLabel( const wxString &lbl )
c801d85f 212{
223d09f6 213 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 214
5f7bcb48
VS
215 wxString label(lbl);
216
5f7bcb48
VS
217 if (label.empty() && wxIsStockID(m_windowId))
218 label = wxGetStockLabel(m_windowId);
5f7bcb48
VS
219
220 wxControl::SetLabel(label);
9e691f46 221
eaafd2f8 222#ifdef __WXGTK20__
5f7bcb48
VS
223 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
224 {
225 const char *stock = wxGetStockGtkID(m_windowId);
226 if (stock)
227 {
228 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
229 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
b04683b1 230 return;
5f7bcb48 231 }
5f7bcb48
VS
232 }
233
234 wxString label2 = PrepareLabelMnemonics(label);
235 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(label2));
236 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
eaafd2f8 237#else
5f7bcb48 238 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(GetLabel()));
eaafd2f8 239#endif
6de97a3b 240}
c801d85f 241
f03fc89f 242bool wxButton::Enable( bool enable )
a9c96bcc 243{
f03fc89f
VZ
244 if ( !wxControl::Enable( enable ) )
245 return FALSE;
9e691f46
VZ
246
247 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f
VZ
248
249 return TRUE;
a9c96bcc
RR
250}
251
2b5f62a0
VZ
252bool wxButton::IsOwnGtkWindow( GdkWindow *window )
253{
254#ifdef __WXGTK20__
255 return GTK_BUTTON(m_widget)->event_window;
256#else
257 return (window == m_widget->window);
258#endif
259}
260
f40fdaa3 261void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 262{
f40fdaa3
VS
263 gtk_widget_modify_style(m_widget, style);
264 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
a81258be 265}
db434467
RR
266
267wxSize wxButton::DoGetBestSize() const
268{
4f819fe4
VZ
269 // the default button in wxGTK is bigger than the other ones because of an
270 // extra border around it, but we don't want to take it into account in
271 // our size calculations (otherwsie the result is visually ugly), so
272 // always return the size of non default button from here
273 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
274 if ( isDefault )
275 {
276 // temporarily unset default flag
277 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
278 }
279
db434467 280 wxSize ret( wxControl::DoGetBestSize() );
9e691f46 281
4f819fe4
VZ
282 if ( isDefault )
283 {
284 // set it back again
285 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
286 }
287
391ddf40
RD
288#ifndef __WXGTK20__
289 ret.x += 10; // add a few pixels for sloppy (but common) themes
290#endif
291
8ab696e0
RR
292 if (!HasFlag(wxBU_EXACTFIT))
293 {
4fa87bd9
VS
294 wxSize defaultSize = GetDefaultSize();
295 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
296 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
8ab696e0 297 }
9e691f46 298
9f884528 299 CacheBestSize(ret);
db434467
RR
300 return ret;
301}
302
9d522606
RD
303// static
304wxVisualAttributes
305wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
306{
307 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
308}
309
1e6feb95
VZ
310#endif // wxUSE_BUTTON
311