]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/button.cpp
Fixed wxRichTextCtrl base class
[wxWidgets.git] / src / gtk / button.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
93763ad5 2// Name: src/gtk/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
b84aec03 15#ifndef WX_PRECOMP
94aff5ff 16 #include "wx/button.h"
b84aec03
WS
17#endif
18
5f7bcb48 19#include "wx/stockitem.h"
c801d85f 20
9e691f46 21#include "wx/gtk/private.h"
83624f79 22
66bd6b93
RR
23//-----------------------------------------------------------------------------
24// data
25//-----------------------------------------------------------------------------
26
27extern bool g_blockEventsOnDrag;
28
c801d85f 29//-----------------------------------------------------------------------------
e1e955e1 30// "clicked"
c801d85f
KB
31//-----------------------------------------------------------------------------
32
865bb325 33extern "C" {
66bd6b93 34static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
c801d85f 35{
a2053b27 36 if (!button->m_hasVMT) return;
acfd422a 37 if (g_blockEventsOnDrag) return;
9e691f46 38
acfd422a
RR
39 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
40 event.SetEventObject(button);
937013e0 41 button->HandleWindowEvent(event);
6de97a3b 42}
865bb325 43}
c801d85f 44
a90c0600
RR
45//-----------------------------------------------------------------------------
46// "style_set" from m_widget
47//-----------------------------------------------------------------------------
48
4cdf71be
PC
49extern "C" {
50static void
51gtk_button_style_set_callback(GtkWidget* widget, GtkStyle*, wxButton* win)
a90c0600 52{
f893066b 53 /* the default button has a border around it */
4cdf71be
PC
54 wxWindow* parent = win->GetParent();
55 if (parent && parent->m_wxwindow && GTK_WIDGET_CAN_DEFAULT(widget))
f893066b 56 {
4cdf71be
PC
57 GtkBorder* border = NULL;
58 gtk_widget_style_get(widget, "default_border", &border, NULL);
59 if (border)
f893066b 60 {
4cdf71be
PC
61 win->MoveWindow(
62 win->m_x - border->left,
63 win->m_y - border->top,
64 win->m_width + border->left + border->right,
65 win->m_height + border->top + border->bottom);
66 gtk_border_free(border);
f893066b 67 }
b2ff89d6 68 }
4cdf71be 69}
a90c0600
RR
70}
71
c801d85f 72//-----------------------------------------------------------------------------
e1e955e1
RR
73// wxButton
74//-----------------------------------------------------------------------------
75
76IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
c801d85f 77
fd0eed64 78wxButton::wxButton()
c801d85f 79{
6de97a3b 80}
c801d85f 81
fd0eed64
RR
82wxButton::~wxButton()
83{
fd0eed64
RR
84}
85
e8375af8
VZ
86bool wxButton::Create(wxWindow *parent,
87 wxWindowID id,
88 const wxString &label,
89 const wxPoint& pos,
90 const wxSize& size,
91 long style,
92 const wxValidator& validator,
93 const wxString& name)
c801d85f 94{
4dcaf11a
RR
95 if (!PreCreation( parent, pos, size ) ||
96 !CreateBase( parent, id, pos, size, style, validator, name ))
97 {
223d09f6 98 wxFAIL_MSG( wxT("wxButton creation failed") );
93763ad5 99 return false;
4dcaf11a 100 }
c801d85f 101
5f7bcb48 102 m_widget = gtk_button_new_with_mnemonic("");
9ff9d30c 103 g_object_ref(m_widget);
354aa1e3 104
2e8613b7
RR
105 float x_alignment = 0.5;
106 if (HasFlag(wxBU_LEFT))
107 x_alignment = 0.0;
108 else if (HasFlag(wxBU_RIGHT))
109 x_alignment = 1.0;
110
111 float y_alignment = 0.5;
112 if (HasFlag(wxBU_TOP))
113 y_alignment = 0.0;
114 else if (HasFlag(wxBU_BOTTOM))
115 y_alignment = 1.0;
116
593ac8df 117 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
a696db45 118
5f7bcb48 119 SetLabel(label);
354aa1e3 120
de1c750f
RR
121 if (style & wxNO_BORDER)
122 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
de1c750f 123
9fa72bd2
MR
124 g_signal_connect_after (m_widget, "clicked",
125 G_CALLBACK (gtk_button_clicked_callback),
126 this);
c801d85f 127
9fa72bd2
MR
128 g_signal_connect_after (m_widget, "style_set",
129 G_CALLBACK (gtk_button_style_set_callback),
130 this);
b2ff89d6 131
f03fc89f 132 m_parent->DoAddChild( this );
9e691f46 133
abdeb9e7 134 PostCreation(size);
db434467 135
4fa87bd9
VS
136 return true;
137}
b2ff89d6 138
32c77a71 139
94aff5ff 140wxWindow *wxButton::SetDefault()
c801d85f 141{
94aff5ff 142 wxWindow *oldDefault = wxButtonBase::SetDefault();
b2ff89d6 143
3502e687
RR
144 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
145 gtk_widget_grab_default( m_widget );
b2ff89d6 146
f893066b
RR
147 // resize for default border
148 gtk_button_style_set_callback( m_widget, NULL, this );
94aff5ff
VZ
149
150 return oldDefault;
6de97a3b 151}
c801d85f 152
ebea0891 153/* static */
4fa87bd9 154wxSize wxButtonBase::GetDefaultSize()
8dbf4589 155{
4fa87bd9
VS
156 static wxSize size = wxDefaultSize;
157 if (size == wxDefaultSize)
158 {
159 // NB: Default size of buttons should be same as size of stock
160 // buttons as used in most GTK+ apps. Unfortunately it's a little
161 // tricky to obtain this size: stock button's size may be smaller
162 // than size of button in GtkButtonBox and vice versa,
163 // GtkButtonBox's minimal button size may be smaller than stock
164 // button's size. We have to retrieve both values and combine them.
165
166 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
167 GtkWidget *box = gtk_hbutton_box_new();
168 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
169 gtk_container_add(GTK_CONTAINER(box), btn);
170 gtk_container_add(GTK_CONTAINER(wnd), box);
171 GtkRequisition req;
172 gtk_widget_size_request(btn, &req);
173
174 gint minwidth, minheight;
175 gtk_widget_style_get(box,
176 "child-min-width", &minwidth,
177 "child-min-height", &minheight,
178 NULL);
179
180 size.x = wxMax(minwidth, req.width);
181 size.y = wxMax(minheight, req.height);
b2ff89d6 182
4fa87bd9
VS
183 gtk_widget_destroy(wnd);
184 }
185 return size;
8dbf4589
RR
186}
187
5f7bcb48 188void wxButton::SetLabel( const wxString &lbl )
c801d85f 189{
223d09f6 190 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
9e691f46 191
5f7bcb48
VS
192 wxString label(lbl);
193
5f7bcb48
VS
194 if (label.empty() && wxIsStockID(m_windowId))
195 label = wxGetStockLabel(m_windowId);
5f7bcb48
VS
196
197 wxControl::SetLabel(label);
9e691f46 198
5f7bcb48
VS
199 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
200 {
201 const char *stock = wxGetStockGtkID(m_windowId);
202 if (stock)
203 {
204 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
205 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
b04683b1 206 return;
5f7bcb48 207 }
5f7bcb48
VS
208 }
209
4cdf71be 210 const wxString labelGTK = GTKConvertMnemonics(label);
b2ff89d6 211 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
5f7bcb48 212 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
b2ff89d6 213
496e7ec6 214 GTKApplyWidgetStyle( false );
6de97a3b 215}
c801d85f 216
f03fc89f 217bool wxButton::Enable( bool enable )
a9c96bcc 218{
ad60f9e7
JS
219 bool isEnabled = IsEnabled();
220
f03fc89f 221 if ( !wxControl::Enable( enable ) )
93763ad5 222 return false;
9e691f46 223
afa7bd1e 224 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
f03fc89f 225
ad60f9e7
JS
226 if (!isEnabled && enable)
227 {
228 GTKFixSensitivity();
229 }
230
93763ad5 231 return true;
a9c96bcc
RR
232}
233
ef5c70f9 234GdkWindow *wxButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
2b5f62a0 235{
2b5f62a0 236 return GTK_BUTTON(m_widget)->event_window;
2b5f62a0
VZ
237}
238
f40fdaa3 239void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 240{
f40fdaa3 241 gtk_widget_modify_style(m_widget, style);
dfc22083
VZ
242 GtkWidget *child = GTK_BIN(m_widget)->child;
243 gtk_widget_modify_style(child, style);
244
245 // for buttons with images, the path to the label is (at least in 2.12)
246 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
247 if ( GTK_IS_ALIGNMENT(child) )
248 {
249 GtkWidget *box = GTK_BIN(child)->child;
250 if ( GTK_IS_BOX(box) )
251 {
f4b0832d
PC
252 for (GList* item = GTK_BOX(box)->children; item; item = item->next)
253 {
254 GtkBoxChild* boxChild = static_cast<GtkBoxChild*>(item->data);
255 gtk_widget_modify_style(boxChild->widget, style);
256 }
dfc22083
VZ
257 }
258 }
a81258be 259}
db434467
RR
260
261wxSize wxButton::DoGetBestSize() const
262{
4f819fe4
VZ
263 // the default button in wxGTK is bigger than the other ones because of an
264 // extra border around it, but we don't want to take it into account in
265 // our size calculations (otherwsie the result is visually ugly), so
266 // always return the size of non default button from here
267 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
268 if ( isDefault )
269 {
270 // temporarily unset default flag
271 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
272 }
273
db434467 274 wxSize ret( wxControl::DoGetBestSize() );
9e691f46 275
4f819fe4
VZ
276 if ( isDefault )
277 {
278 // set it back again
279 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
280 }
281
8ab696e0
RR
282 if (!HasFlag(wxBU_EXACTFIT))
283 {
4fa87bd9
VS
284 wxSize defaultSize = GetDefaultSize();
285 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
286 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
8ab696e0 287 }
9e691f46 288
9f884528 289 CacheBestSize(ret);
db434467
RR
290 return ret;
291}
292
9d522606
RD
293// static
294wxVisualAttributes
295wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
296{
297 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
298}
299
1e6feb95 300#endif // wxUSE_BUTTON