]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: button.cpp | |
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 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
c801d85f KB |
11 | #pragma implementation "button.h" |
12 | #endif | |
13 | ||
14f355c2 VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
1e6feb95 VZ |
17 | #include "wx/defs.h" |
18 | ||
19 | #if wxUSE_BUTTON | |
20 | ||
c801d85f | 21 | #include "wx/button.h" |
5f7bcb48 | 22 | #include "wx/stockitem.h" |
c801d85f | 23 | |
9e691f46 | 24 | #include "wx/gtk/private.h" |
f893066b | 25 | #include "wx/gtk/win_gtk.h" |
83624f79 | 26 | |
c801d85f KB |
27 | //----------------------------------------------------------------------------- |
28 | // classes | |
29 | //----------------------------------------------------------------------------- | |
30 | ||
31 | class wxButton; | |
32 | ||
acfd422a RR |
33 | //----------------------------------------------------------------------------- |
34 | // idle system | |
35 | //----------------------------------------------------------------------------- | |
36 | ||
37 | extern void wxapp_install_idle_handler(); | |
38 | extern bool g_isIdle; | |
39 | ||
66bd6b93 RR |
40 | //----------------------------------------------------------------------------- |
41 | // data | |
42 | //----------------------------------------------------------------------------- | |
43 | ||
44 | extern bool g_blockEventsOnDrag; | |
45 | ||
c801d85f | 46 | //----------------------------------------------------------------------------- |
e1e955e1 | 47 | // "clicked" |
c801d85f KB |
48 | //----------------------------------------------------------------------------- |
49 | ||
66bd6b93 | 50 | static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button ) |
c801d85f | 51 | { |
9e691f46 | 52 | if (g_isIdle) |
32ac755d | 53 | wxapp_install_idle_handler(); |
acfd422a | 54 | |
a2053b27 | 55 | if (!button->m_hasVMT) return; |
acfd422a | 56 | if (g_blockEventsOnDrag) return; |
9e691f46 | 57 | |
acfd422a RR |
58 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId()); |
59 | event.SetEventObject(button); | |
60 | button->GetEventHandler()->ProcessEvent(event); | |
6de97a3b | 61 | } |
c801d85f | 62 | |
a90c0600 RR |
63 | //----------------------------------------------------------------------------- |
64 | // "style_set" from m_widget | |
65 | //----------------------------------------------------------------------------- | |
66 | ||
67 | static gint | |
68 | gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win ) | |
69 | { | |
70 | if (g_isIdle) | |
71 | wxapp_install_idle_handler(); | |
72 | ||
f893066b RR |
73 | int left_border = 0; |
74 | int right_border = 0; | |
75 | int top_border = 0; | |
76 | int bottom_border = 0; | |
77 | ||
78 | /* the default button has a border around it */ | |
79 | if (GTK_WIDGET_CAN_DEFAULT(m_widget)) | |
80 | { | |
81 | #ifdef __WXGTK20__ | |
82 | GtkBorder *default_border = NULL; | |
83 | gtk_widget_style_get( m_widget, "default_border", &default_border, NULL ); | |
84 | if (default_border) | |
85 | { | |
86 | left_border += default_border->left; | |
87 | right_border += default_border->right; | |
88 | top_border += default_border->top; | |
89 | bottom_border += default_border->bottom; | |
90 | g_free( default_border ); | |
91 | } | |
92 | #else | |
93 | left_border = 6; | |
94 | right_border = 6; | |
95 | top_border = 6; | |
96 | bottom_border = 5; | |
97 | #endif | |
98 | win->DoMoveWindow( win->m_x-top_border, | |
99 | win->m_y-left_border, | |
100 | win->m_width+left_border+right_border, | |
101 | win->m_height+top_border+bottom_border ); | |
102 | } | |
a90c0600 RR |
103 | |
104 | return FALSE; | |
105 | } | |
106 | ||
c801d85f | 107 | //----------------------------------------------------------------------------- |
e1e955e1 RR |
108 | // wxButton |
109 | //----------------------------------------------------------------------------- | |
110 | ||
111 | IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl) | |
c801d85f | 112 | |
fd0eed64 | 113 | wxButton::wxButton() |
c801d85f | 114 | { |
6de97a3b | 115 | } |
c801d85f | 116 | |
fd0eed64 RR |
117 | wxButton::~wxButton() |
118 | { | |
fd0eed64 RR |
119 | } |
120 | ||
c801d85f | 121 | bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label, |
32c77a71 | 122 | const wxPoint &pos, const wxSize &size, |
6de97a3b | 123 | long style, const wxValidator& validator, const wxString &name ) |
c801d85f | 124 | { |
b292e2f5 RR |
125 | m_needParent = TRUE; |
126 | m_acceptsFocus = TRUE; | |
32c77a71 | 127 | |
4dcaf11a RR |
128 | if (!PreCreation( parent, pos, size ) || |
129 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
130 | { | |
223d09f6 | 131 | wxFAIL_MSG( wxT("wxButton creation failed") ); |
8ab696e0 | 132 | return FALSE; |
4dcaf11a | 133 | } |
c801d85f | 134 | |
354aa1e3 RR |
135 | /* |
136 | wxString label2( label ); | |
137 | for (size_t i = 0; i < label2.Len(); i++) | |
138 | { | |
139 | if (label2.GetChar(i) == wxT('&')) | |
8ab696e0 | 140 | label2.SetChar(i,wxT('_')); |
354aa1e3 | 141 | } |
9e691f46 | 142 | |
354aa1e3 RR |
143 | GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() ); |
144 | gtk_widget_show( accel_label ); | |
9e691f46 | 145 | |
354aa1e3 RR |
146 | m_widget = gtk_button_new(); |
147 | gtk_container_add( GTK_CONTAINER(m_widget), accel_label ); | |
9e691f46 | 148 | |
354aa1e3 | 149 | gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget ); |
9e691f46 | 150 | |
354aa1e3 RR |
151 | guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() ); |
152 | gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) ); | |
9e691f46 | 153 | |
354aa1e3 RR |
154 | wxControl::SetLabel( label ); |
155 | */ | |
9e691f46 | 156 | |
b2fcfd94 | 157 | #ifdef __WXGTK20__ |
5f7bcb48 | 158 | m_widget = gtk_button_new_with_mnemonic(""); |
b2fcfd94 | 159 | #else |
354aa1e3 | 160 | m_widget = gtk_button_new_with_label(""); |
b2fcfd94 | 161 | #endif |
354aa1e3 | 162 | |
2e8613b7 RR |
163 | float x_alignment = 0.5; |
164 | if (HasFlag(wxBU_LEFT)) | |
165 | x_alignment = 0.0; | |
166 | else if (HasFlag(wxBU_RIGHT)) | |
167 | x_alignment = 1.0; | |
168 | ||
169 | float y_alignment = 0.5; | |
170 | if (HasFlag(wxBU_TOP)) | |
171 | y_alignment = 0.0; | |
172 | else if (HasFlag(wxBU_BOTTOM)) | |
173 | y_alignment = 1.0; | |
174 | ||
77f70672 RR |
175 | #if __WXGTK24__ |
176 | if (!gtk_check_version(2,4,0)) | |
177 | { | |
178 | gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment); | |
179 | } | |
180 | else | |
4fa87bd9 | 181 | #endif |
77f70672 RR |
182 | { |
183 | if (GTK_IS_MISC(BUTTON_CHILD(m_widget))) | |
184 | gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)), | |
185 | x_alignment, y_alignment); | |
186 | } | |
a696db45 | 187 | |
5f7bcb48 | 188 | SetLabel(label); |
354aa1e3 | 189 | |
de1c750f RR |
190 | if (style & wxNO_BORDER) |
191 | gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE ); | |
de1c750f | 192 | |
58b907f6 | 193 | gtk_signal_connect_after( GTK_OBJECT(m_widget), "clicked", |
b292e2f5 | 194 | GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this ); |
c801d85f | 195 | |
a90c0600 RR |
196 | gtk_signal_connect_after( GTK_OBJECT(m_widget), "style_set", |
197 | GTK_SIGNAL_FUNC(gtk_button_style_set_callback), (gpointer*) this ); | |
198 | ||
f03fc89f | 199 | m_parent->DoAddChild( this ); |
9e691f46 | 200 | |
abdeb9e7 | 201 | PostCreation(size); |
db434467 | 202 | |
4fa87bd9 VS |
203 | return true; |
204 | } | |
205 | ||
32c77a71 | 206 | |
da048e3d | 207 | void wxButton::SetDefault() |
c801d85f | 208 | { |
97149f18 RD |
209 | wxWindow *parent = GetParent(); |
210 | wxCHECK_RET( parent, _T("button without parent?") ); | |
211 | ||
a00b2d0a | 212 | parent->SetDefaultItem(this); |
97149f18 | 213 | |
3502e687 RR |
214 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); |
215 | gtk_widget_grab_default( m_widget ); | |
f893066b RR |
216 | |
217 | // resize for default border | |
218 | gtk_button_style_set_callback( m_widget, NULL, this ); | |
6de97a3b | 219 | } |
c801d85f | 220 | |
ebea0891 | 221 | /* static */ |
4fa87bd9 | 222 | wxSize wxButtonBase::GetDefaultSize() |
8dbf4589 | 223 | { |
4fa87bd9 VS |
224 | #ifdef __WXGTK20__ |
225 | static wxSize size = wxDefaultSize; | |
226 | if (size == wxDefaultSize) | |
227 | { | |
228 | // NB: Default size of buttons should be same as size of stock | |
229 | // buttons as used in most GTK+ apps. Unfortunately it's a little | |
230 | // tricky to obtain this size: stock button's size may be smaller | |
231 | // than size of button in GtkButtonBox and vice versa, | |
232 | // GtkButtonBox's minimal button size may be smaller than stock | |
233 | // button's size. We have to retrieve both values and combine them. | |
234 | ||
235 | GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
236 | GtkWidget *box = gtk_hbutton_box_new(); | |
237 | GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL); | |
238 | gtk_container_add(GTK_CONTAINER(box), btn); | |
239 | gtk_container_add(GTK_CONTAINER(wnd), box); | |
240 | GtkRequisition req; | |
241 | gtk_widget_size_request(btn, &req); | |
242 | ||
243 | gint minwidth, minheight; | |
244 | gtk_widget_style_get(box, | |
245 | "child-min-width", &minwidth, | |
246 | "child-min-height", &minheight, | |
247 | NULL); | |
248 | ||
249 | size.x = wxMax(minwidth, req.width); | |
250 | size.y = wxMax(minheight, req.height); | |
251 | ||
252 | gtk_widget_destroy(wnd); | |
253 | } | |
254 | return size; | |
255 | #else | |
8dbf4589 | 256 | return wxSize(80,26); |
4fa87bd9 | 257 | #endif |
8dbf4589 RR |
258 | } |
259 | ||
5f7bcb48 | 260 | void wxButton::SetLabel( const wxString &lbl ) |
c801d85f | 261 | { |
223d09f6 | 262 | wxCHECK_RET( m_widget != NULL, wxT("invalid button") ); |
9e691f46 | 263 | |
5f7bcb48 VS |
264 | wxString label(lbl); |
265 | ||
5f7bcb48 VS |
266 | if (label.empty() && wxIsStockID(m_windowId)) |
267 | label = wxGetStockLabel(m_windowId); | |
5f7bcb48 VS |
268 | |
269 | wxControl::SetLabel(label); | |
9e691f46 | 270 | |
eaafd2f8 | 271 | #ifdef __WXGTK20__ |
5f7bcb48 VS |
272 | if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label)) |
273 | { | |
274 | const char *stock = wxGetStockGtkID(m_windowId); | |
275 | if (stock) | |
276 | { | |
277 | gtk_button_set_label(GTK_BUTTON(m_widget), stock); | |
278 | gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE); | |
b04683b1 | 279 | return; |
5f7bcb48 | 280 | } |
5f7bcb48 VS |
281 | } |
282 | ||
283 | wxString label2 = PrepareLabelMnemonics(label); | |
284 | gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(label2)); | |
285 | gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE); | |
19ac2f44 RR |
286 | |
287 | ApplyWidgetStyle( false ); | |
288 | ||
eaafd2f8 | 289 | #else |
5f7bcb48 | 290 | gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(GetLabel())); |
eaafd2f8 | 291 | #endif |
6de97a3b | 292 | } |
c801d85f | 293 | |
f03fc89f | 294 | bool wxButton::Enable( bool enable ) |
a9c96bcc | 295 | { |
f03fc89f VZ |
296 | if ( !wxControl::Enable( enable ) ) |
297 | return FALSE; | |
9e691f46 VZ |
298 | |
299 | gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable ); | |
f03fc89f VZ |
300 | |
301 | return TRUE; | |
a9c96bcc RR |
302 | } |
303 | ||
2b5f62a0 VZ |
304 | bool wxButton::IsOwnGtkWindow( GdkWindow *window ) |
305 | { | |
306 | #ifdef __WXGTK20__ | |
307 | return GTK_BUTTON(m_widget)->event_window; | |
308 | #else | |
309 | return (window == m_widget->window); | |
310 | #endif | |
311 | } | |
312 | ||
f40fdaa3 | 313 | void wxButton::DoApplyWidgetStyle(GtkRcStyle *style) |
868a2826 | 314 | { |
f40fdaa3 VS |
315 | gtk_widget_modify_style(m_widget, style); |
316 | gtk_widget_modify_style(BUTTON_CHILD(m_widget), style); | |
a81258be | 317 | } |
db434467 RR |
318 | |
319 | wxSize wxButton::DoGetBestSize() const | |
320 | { | |
4f819fe4 VZ |
321 | // the default button in wxGTK is bigger than the other ones because of an |
322 | // extra border around it, but we don't want to take it into account in | |
323 | // our size calculations (otherwsie the result is visually ugly), so | |
324 | // always return the size of non default button from here | |
325 | const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget); | |
326 | if ( isDefault ) | |
327 | { | |
328 | // temporarily unset default flag | |
329 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
330 | } | |
331 | ||
db434467 | 332 | wxSize ret( wxControl::DoGetBestSize() ); |
9e691f46 | 333 | |
4f819fe4 VZ |
334 | if ( isDefault ) |
335 | { | |
336 | // set it back again | |
337 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
338 | } | |
339 | ||
391ddf40 RD |
340 | #ifndef __WXGTK20__ |
341 | ret.x += 10; // add a few pixels for sloppy (but common) themes | |
342 | #endif | |
343 | ||
8ab696e0 RR |
344 | if (!HasFlag(wxBU_EXACTFIT)) |
345 | { | |
4fa87bd9 VS |
346 | wxSize defaultSize = GetDefaultSize(); |
347 | if (ret.x < defaultSize.x) ret.x = defaultSize.x; | |
348 | if (ret.y < defaultSize.y) ret.y = defaultSize.y; | |
8ab696e0 | 349 | } |
9e691f46 | 350 | |
9f884528 | 351 | CacheBestSize(ret); |
db434467 RR |
352 | return ret; |
353 | } | |
354 | ||
9d522606 RD |
355 | // static |
356 | wxVisualAttributes | |
357 | wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
358 | { | |
359 | return GetDefaultAttributesFromGTKWidget(gtk_button_new); | |
360 | } | |
361 | ||
1e6feb95 VZ |
362 | #endif // wxUSE_BUTTON |
363 |