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