new stock buttons API
[wxWidgets.git] / src / gtk1 / button.cpp
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
30 class wxButton;
31
32 //-----------------------------------------------------------------------------
33 // idle system
34 //-----------------------------------------------------------------------------
35
36 extern void wxapp_install_idle_handler();
37 extern bool g_isIdle;
38
39 //-----------------------------------------------------------------------------
40 // data
41 //-----------------------------------------------------------------------------
42
43 extern bool g_blockEventsOnDrag;
44
45 //-----------------------------------------------------------------------------
46 // "clicked"
47 //-----------------------------------------------------------------------------
48
49 static 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 // wxButton
64 //-----------------------------------------------------------------------------
65
66 IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
67
68 wxButton::wxButton()
69 {
70 }
71
72 wxButton::~wxButton()
73 {
74 }
75
76 bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
77 const wxPoint &pos, const wxSize &size,
78 long style, const wxValidator& validator, const wxString &name )
79 {
80 m_needParent = TRUE;
81 m_acceptsFocus = TRUE;
82
83 if (!PreCreation( parent, pos, size ) ||
84 !CreateBase( parent, id, pos, size, style, validator, name ))
85 {
86 wxFAIL_MSG( wxT("wxButton creation failed") );
87 return FALSE;
88 }
89
90 /*
91 wxString label2( label );
92 for (size_t i = 0; i < label2.Len(); i++)
93 {
94 if (label2.GetChar(i) == wxT('&'))
95 label2.SetChar(i,wxT('_'));
96 }
97
98 GtkWidget *accel_label = gtk_accel_label_new( label2.mb_str() );
99 gtk_widget_show( accel_label );
100
101 m_widget = gtk_button_new();
102 gtk_container_add( GTK_CONTAINER(m_widget), accel_label );
103
104 gtk_accel_label_set_accel_widget( GTK_ACCEL_LABEL(accel_label), m_widget );
105
106 guint accel_key = gtk_label_parse_uline (GTK_LABEL(accel_label), label2.mb_str() );
107 gtk_accel_label_refetch( GTK_ACCEL_LABEL(accel_label) );
108
109 wxControl::SetLabel( label );
110 */
111
112 #ifdef __WXGTK20__
113 m_widget = gtk_button_new_with_mnemonic("");
114 #else
115 m_widget = gtk_button_new_with_label("");
116 #endif
117
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
130 #if GTK_CHECK_VERSION(2,4,0)
131 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
132 #else
133 if (GTK_IS_MISC(BUTTON_CHILD(m_widget)))
134 gtk_misc_set_alignment (GTK_MISC (BUTTON_CHILD (m_widget)),
135 x_alignment, y_alignment);
136 #endif
137
138 SetLabel(label);
139
140 if (style & wxNO_BORDER)
141 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
142
143 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
144 GTK_SIGNAL_FUNC(gtk_button_clicked_callback), (gpointer*)this );
145
146 m_parent->DoAddChild( this );
147
148 PostCreation(size);
149
150 return true;
151 }
152
153
154 void wxButton::SetDefault()
155 {
156 wxWindow *parent = GetParent();
157 wxCHECK_RET( parent, _T("button without parent?") );
158
159 parent->SetDefaultItem(this);
160
161 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
162 gtk_widget_grab_default( m_widget );
163
164 SetSize( m_x, m_y, m_width, m_height );
165 }
166
167 /* static */
168 wxSize wxButtonBase::GetDefaultSize()
169 {
170 #ifdef __WXGTK20__
171 static wxSize size = wxDefaultSize;
172 if (size == wxDefaultSize)
173 {
174 // NB: Default size of buttons should be same as size of stock
175 // buttons as used in most GTK+ apps. Unfortunately it's a little
176 // tricky to obtain this size: stock button's size may be smaller
177 // than size of button in GtkButtonBox and vice versa,
178 // GtkButtonBox's minimal button size may be smaller than stock
179 // button's size. We have to retrieve both values and combine them.
180
181 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
182 GtkWidget *box = gtk_hbutton_box_new();
183 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
184 gtk_container_add(GTK_CONTAINER(box), btn);
185 gtk_container_add(GTK_CONTAINER(wnd), box);
186 GtkRequisition req;
187 gtk_widget_size_request(btn, &req);
188
189 gint minwidth, minheight;
190 gtk_widget_style_get(box,
191 "child-min-width", &minwidth,
192 "child-min-height", &minheight,
193 NULL);
194
195 size.x = wxMax(minwidth, req.width);
196 size.y = wxMax(minheight, req.height);
197
198 gtk_widget_destroy(wnd);
199 }
200 return size;
201 #else
202 return wxSize(80,26);
203 #endif
204 }
205
206 void wxButton::SetLabel( const wxString &lbl )
207 {
208 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
209
210 wxString label(lbl);
211
212 #ifndef __WXGTK20__
213 if (label.empty() && wxIsStockID(m_windowId))
214 label = wxGetStockLabel(m_windowId);
215 #endif
216
217 wxControl::SetLabel(label);
218
219 #ifdef __WXGTK20__
220 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
221 {
222 const char *stock = wxGetStockGtkID(m_windowId);
223 if (stock)
224 {
225 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
226 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
227 }
228 return;
229 }
230
231 wxString label2 = PrepareLabelMnemonics(label);
232 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(label2));
233 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
234 #else
235 gtk_label_set(GTK_LABEL(BUTTON_CHILD(m_widget)), wxGTK_CONV(GetLabel()));
236 #endif
237 }
238
239 bool wxButton::Enable( bool enable )
240 {
241 if ( !wxControl::Enable( enable ) )
242 return FALSE;
243
244 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
245
246 return TRUE;
247 }
248
249 bool wxButton::IsOwnGtkWindow( GdkWindow *window )
250 {
251 #ifdef __WXGTK20__
252 return GTK_BUTTON(m_widget)->event_window;
253 #else
254 return (window == m_widget->window);
255 #endif
256 }
257
258 void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
259 {
260 gtk_widget_modify_style(m_widget, style);
261 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
262 }
263
264 wxSize wxButton::DoGetBestSize() const
265 {
266 // the default button in wxGTK is bigger than the other ones because of an
267 // extra border around it, but we don't want to take it into account in
268 // our size calculations (otherwsie the result is visually ugly), so
269 // always return the size of non default button from here
270 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
271 if ( isDefault )
272 {
273 // temporarily unset default flag
274 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
275 }
276
277 wxSize ret( wxControl::DoGetBestSize() );
278
279 if ( isDefault )
280 {
281 // set it back again
282 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
283 }
284
285 #ifndef __WXGTK20__
286 ret.x += 10; // add a few pixels for sloppy (but common) themes
287 #endif
288
289 if (!HasFlag(wxBU_EXACTFIT))
290 {
291 wxSize defaultSize = GetDefaultSize();
292 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
293 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
294 }
295
296 CacheBestSize(ret);
297 return ret;
298 }
299
300 // static
301 wxVisualAttributes
302 wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
303 {
304 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
305 }
306
307 #endif // wxUSE_BUTTON
308