]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/button.cpp
Always link with expat in monolithic build.
[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
fd0eed64 97wxButton::wxButton()
c801d85f 98{
6de97a3b 99}
c801d85f 100
fd0eed64
RR
101wxButton::~wxButton()
102{
fd0eed64
RR
103}
104
c801d85f 105bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
32c77a71 106 const wxPoint &pos, const wxSize &size,
6de97a3b 107 long style, const wxValidator& validator, const wxString &name )
c801d85f 108{
93763ad5
WS
109 m_needParent = true;
110 m_acceptsFocus = true;
32c77a71 111
4dcaf11a
RR
112 if (!PreCreation( parent, pos, size ) ||
113 !CreateBase( parent, id, pos, size, style, validator, name ))
114 {
223d09f6 115 wxFAIL_MSG( wxT("wxButton creation failed") );
93763ad5 116 return false;
4dcaf11a 117 }
c801d85f 118
354aa1e3
RR
119 m_widget = gtk_button_new_with_label("");
120
2e8613b7
RR
121 float x_alignment = 0.5;
122 if (HasFlag(wxBU_LEFT))
123 x_alignment = 0.0;
124 else if (HasFlag(wxBU_RIGHT))
125 x_alignment = 1.0;
126
127 float y_alignment = 0.5;
128 if (HasFlag(wxBU_TOP))
129 y_alignment = 0.0;
130 else if (HasFlag(wxBU_BOTTOM))
131 y_alignment = 1.0;
132
3cbab641
MR
133 if (GTK_IS_MISC(BUTTON_CHILD(m_widget)))
134 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)),
77f70672 135 x_alignment, y_alignment);
a696db45 136
5f7bcb48 137 SetLabel(label);
354aa1e3 138
de1c750f
RR
139 if (style & wxNO_BORDER)
140 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 141
58b907f6 142 gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked",
b292e2f5 143 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
c801d85f 144
a90c0600
RR
145 gtk_signal_connect_after( GTK_OBJECT(m_widget), "style_set",
146 GTK_SIGNAL_FUNC(gtk_button_style_set_callback), (gpointer*) this );
b2ff89d6 147
f03fc89f 148 m_parent->DoAddChild( this );
9e691f46 149
abdeb9e7 150 PostCreation(size);
db434467 151
4fa87bd9
VS
152 return true;
153}
b2ff89d6 154
32c77a71 155
94aff5ff 156wxWindow *wxButton::SetDefault()
c801d85f 157{
94aff5ff 158 wxWindow *oldDefault = wxButtonBase::SetDefault();
b2ff89d6 159
3502e687
RR
160 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
161 gtk_widget_grab_default( m_widget );
b2ff89d6 162
f893066b
RR
163 // resize for default border
164 gtk_button_style_set_callback( m_widget, NULL, this );
94aff5ff
VZ
165
166 return oldDefault;
6de97a3b 167}
c801d85f 168
ebea0891 169/* static */
4fa87bd9 170wxSize wxButtonBase::GetDefaultSize()
8dbf4589
RR
171{
172 return wxSize(80,26);
173}
174
5f7bcb48 175void wxButton::SetLabel( const wxString &lbl )
c801d85f 176{
223d09f6 177 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 178
5f7bcb48
VS
179 wxString label(lbl);
180
5f7bcb48
VS
181 if (label.empty() && wxIsStockID(m_windowId))
182 label = wxGetStockLabel(m_windowId);
5f7bcb48
VS
183
184 wxControl::SetLabel(label);
9e691f46 185
ee6dd41a 186 const wxString labelGTK = GTKRemoveMnemonics(label);
b2ff89d6 187
b2ff89d6 188 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(labelGTK));
6de97a3b 189}
c801d85f 190
f03fc89f 191bool wxButton::Enable( bool enable )
a9c96bcc 192{
f03fc89f 193 if ( !wxControl::Enable( enable ) )
93763ad5 194 return false;
9e691f46
VZ
195
196 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f 197
93763ad5 198 return true;
a9c96bcc
RR
199}
200
2b5f62a0
VZ
201bool wxButton::IsOwnGtkWindow( GdkWindow *window )
202{
2b5f62a0 203 return (window == m_widget->window);
2b5f62a0
VZ
204}
205
f40fdaa3 206void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 207{
f40fdaa3
VS
208 gtk_widget_modify_style(m_widget, style);
209 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
a81258be 210}
db434467
RR
211
212wxSize wxButton::DoGetBestSize() const
213{
4f819fe4
VZ
214 // the default button in wxGTK is bigger than the other ones because of an
215 // extra border around it, but we don't want to take it into account in
216 // our size calculations (otherwsie the result is visually ugly), so
217 // always return the size of non default button from here
218 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
219 if ( isDefault )
220 {
221 // temporarily unset default flag
222 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
223 }
224
db434467 225 wxSize ret( wxControl::DoGetBestSize() );
9e691f46 226
4f819fe4
VZ
227 if ( isDefault )
228 {
229 // set it back again
230 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
231 }
232
391ddf40 233 ret.x += 10; // add a few pixels for sloppy (but common) themes
b2ff89d6 234
8ab696e0
RR
235 if (!HasFlag(wxBU_EXACTFIT))
236 {
4fa87bd9
VS
237 wxSize defaultSize = GetDefaultSize();
238 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
239 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
8ab696e0 240 }
9e691f46 241
9f884528 242 CacheBestSize(ret);
db434467
RR
243 return ret;
244}
245
9d522606
RD
246// static
247wxVisualAttributes
248wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
249{
250 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
251}
252
1e6feb95 253#endif // wxUSE_BUTTON