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