]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/button.cpp
allow calling SetItemLabel() for items not attached to the menu or menu bar yet ...
[wxWidgets.git] / src / gtk1 / button.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/gtk1/button.cpp
c801d85f
KB
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#if wxUSE_BUTTON
14
2cdcee61 15#ifndef WX_PRECOMP
94aff5ff 16 #include "wx/button.h"
2cdcee61
WS
17#endif
18
5f7bcb48 19#include "wx/stockitem.h"
c801d85f 20
3cbab641
MR
21#include "wx/gtk1/private.h"
22#include "wx/gtk1/win_gtk.h"
83624f79 23
c801d85f
KB
24//-----------------------------------------------------------------------------
25// classes
26//-----------------------------------------------------------------------------
27
28class wxButton;
29
acfd422a
RR
30//-----------------------------------------------------------------------------
31// idle system
32//-----------------------------------------------------------------------------
33
34extern void wxapp_install_idle_handler();
35extern bool g_isIdle;
36
66bd6b93
RR
37//-----------------------------------------------------------------------------
38// data
39//-----------------------------------------------------------------------------
40
41extern bool g_blockEventsOnDrag;
42
c801d85f 43//-----------------------------------------------------------------------------
e1e955e1 44// "clicked"
c801d85f
KB
45//-----------------------------------------------------------------------------
46
865bb325 47extern "C" {
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);
937013e0 58 button->HandleWindowEvent(event);
6de97a3b 59}
865bb325 60}
c801d85f 61
a90c0600
RR
62//-----------------------------------------------------------------------------
63// "style_set" from m_widget
64//-----------------------------------------------------------------------------
65
66static gint
67gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win )
68{
69 if (g_isIdle)
70 wxapp_install_idle_handler();
b2ff89d6 71
f893066b
RR
72 int left_border = 0;
73 int right_border = 0;
74 int top_border = 0;
75 int bottom_border = 0;
b2ff89d6 76
f893066b
RR
77 /* the default button has a border around it */
78 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
79 {
f893066b
RR
80 left_border = 6;
81 right_border = 6;
82 top_border = 6;
83 bottom_border = 5;
f893066b
RR
84 win->DoMoveWindow( win->m_x-top_border,
85 win->m_y-left_border,
86 win->m_width+left_border+right_border,
87 win->m_height+top_border+bottom_border );
b2ff89d6 88 }
a90c0600
RR
89
90 return FALSE;
91}
92
c801d85f 93//-----------------------------------------------------------------------------
e1e955e1
RR
94// wxButton
95//-----------------------------------------------------------------------------
96
97IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
c801d85f 98
fd0eed64 99wxButton::wxButton()
c801d85f 100{
6de97a3b 101}
c801d85f 102
fd0eed64
RR
103wxButton::~wxButton()
104{
fd0eed64
RR
105}
106
c801d85f 107bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
32c77a71 108 const wxPoint &pos, const wxSize &size,
6de97a3b 109 long style, const wxValidator& validator, const wxString &name )
c801d85f 110{
93763ad5
WS
111 m_needParent = true;
112 m_acceptsFocus = true;
32c77a71 113
4dcaf11a
RR
114 if (!PreCreation( parent, pos, size ) ||
115 !CreateBase( parent, id, pos, size, style, validator, name ))
116 {
223d09f6 117 wxFAIL_MSG( wxT("wxButton creation failed") );
93763ad5 118 return false;
4dcaf11a 119 }
c801d85f 120
354aa1e3
RR
121 m_widget = gtk_button_new_with_label("");
122
2e8613b7
RR
123 float x_alignment = 0.5;
124 if (HasFlag(wxBU_LEFT))
125 x_alignment = 0.0;
126 else if (HasFlag(wxBU_RIGHT))
127 x_alignment = 1.0;
128
129 float y_alignment = 0.5;
130 if (HasFlag(wxBU_TOP))
131 y_alignment = 0.0;
132 else if (HasFlag(wxBU_BOTTOM))
133 y_alignment = 1.0;
134
3cbab641
MR
135 if (GTK_IS_MISC(BUTTON_CHILD(m_widget)))
136 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)),
77f70672 137 x_alignment, y_alignment);
a696db45 138
5f7bcb48 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
58b907f6 144 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
b292e2f5 145 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
c801d85f 146
a90c0600
RR
147 gtk_signal_connect_after( GTK_OBJECT(m_widget), "style_set",
148 GTK_SIGNAL_FUNC(gtk_button_style_set_callback), (gpointer*) this );
b2ff89d6 149
f03fc89f 150 m_parent->DoAddChild( this );
9e691f46 151
abdeb9e7 152 PostCreation(size);
db434467 153
4fa87bd9
VS
154 return true;
155}
b2ff89d6 156
32c77a71 157
94aff5ff 158wxWindow *wxButton::SetDefault()
c801d85f 159{
94aff5ff 160 wxWindow *oldDefault = wxButtonBase::SetDefault();
b2ff89d6 161
3502e687
RR
162 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
163 gtk_widget_grab_default( m_widget );
b2ff89d6 164
f893066b
RR
165 // resize for default border
166 gtk_button_style_set_callback( m_widget, NULL, this );
94aff5ff
VZ
167
168 return oldDefault;
6de97a3b 169}
c801d85f 170
ebea0891 171/* static */
4fa87bd9 172wxSize wxButtonBase::GetDefaultSize()
8dbf4589
RR
173{
174 return wxSize(80,26);
175}
176
5f7bcb48 177void wxButton::SetLabel( const wxString &lbl )
c801d85f 178{
223d09f6 179 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 180
5f7bcb48
VS
181 wxString label(lbl);
182
5f7bcb48
VS
183 if (label.empty() && wxIsStockID(m_windowId))
184 label = wxGetStockLabel(m_windowId);
5f7bcb48
VS
185
186 wxControl::SetLabel(label);
9e691f46 187
ee6dd41a 188 const wxString labelGTK = GTKRemoveMnemonics(label);
b2ff89d6 189
b2ff89d6 190 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(labelGTK));
6de97a3b 191}
c801d85f 192
f03fc89f 193bool wxButton::Enable( bool enable )
a9c96bcc 194{
f03fc89f 195 if ( !wxControl::Enable( enable ) )
93763ad5 196 return false;
9e691f46
VZ
197
198 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f 199
93763ad5 200 return true;
a9c96bcc
RR
201}
202
2b5f62a0
VZ
203bool wxButton::IsOwnGtkWindow( GdkWindow *window )
204{
2b5f62a0 205 return (window == m_widget->window);
2b5f62a0
VZ
206}
207
f40fdaa3 208void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 209{
f40fdaa3
VS
210 gtk_widget_modify_style(m_widget, style);
211 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
a81258be 212}
db434467
RR
213
214wxSize wxButton::DoGetBestSize() const
215{
4f819fe4
VZ
216 // the default button in wxGTK is bigger than the other ones because of an
217 // extra border around it, but we don't want to take it into account in
218 // our size calculations (otherwsie the result is visually ugly), so
219 // always return the size of non default button from here
220 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
221 if ( isDefault )
222 {
223 // temporarily unset default flag
224 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
225 }
226
db434467 227 wxSize ret( wxControl::DoGetBestSize() );
9e691f46 228
4f819fe4
VZ
229 if ( isDefault )
230 {
231 // set it back again
232 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
233 }
234
391ddf40 235 ret.x += 10; // add a few pixels for sloppy (but common) themes
b2ff89d6 236
8ab696e0
RR
237 if (!HasFlag(wxBU_EXACTFIT))
238 {
4fa87bd9
VS
239 wxSize defaultSize = GetDefaultSize();
240 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
241 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
8ab696e0 242 }
9e691f46 243
9f884528 244 CacheBestSize(ret);
db434467
RR
245 return ret;
246}
247
9d522606
RD
248// static
249wxVisualAttributes
250wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
251{
252 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
253}
254
1e6feb95 255#endif // wxUSE_BUTTON