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