]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/button.cpp
replaced run-time tests for wxRICHTEXT_USE_TOOLBOOK with compile-time ones to avoid...
[wxWidgets.git] / src / gtk / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/button.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_BUTTON
14
15 #include "wx/button.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/toplevel.h"
19 #endif
20
21 #include "wx/stockitem.h"
22
23 #include "wx/gtk/private.h"
24 #include "wx/gtk/win_gtk.h"
25
26 //-----------------------------------------------------------------------------
27 // classes
28 //-----------------------------------------------------------------------------
29
30 class wxButton;
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 extern bool g_blockEventsOnDrag;
37
38 //-----------------------------------------------------------------------------
39 // "clicked"
40 //-----------------------------------------------------------------------------
41
42 extern "C" {
43 static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
44 {
45 if (g_isIdle)
46 wxapp_install_idle_handler();
47
48 if (!button->m_hasVMT) return;
49 if (g_blockEventsOnDrag) return;
50
51 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
52 event.SetEventObject(button);
53 button->GetEventHandler()->ProcessEvent(event);
54 }
55 }
56
57 //-----------------------------------------------------------------------------
58 // "style_set" from m_widget
59 //-----------------------------------------------------------------------------
60
61 static gint
62 gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win )
63 {
64 if (g_isIdle)
65 wxapp_install_idle_handler();
66
67 int left_border = 0;
68 int right_border = 0;
69 int top_border = 0;
70 int bottom_border = 0;
71
72 /* the default button has a border around it */
73 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
74 {
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 }
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);
90 }
91
92 return FALSE;
93 }
94
95 //-----------------------------------------------------------------------------
96 // wxButton
97 //-----------------------------------------------------------------------------
98
99 IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
100
101 wxButton::wxButton()
102 {
103 }
104
105 wxButton::~wxButton()
106 {
107 }
108
109 bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label,
110 const wxPoint &pos, const wxSize &size,
111 long style, const wxValidator& validator, const wxString &name )
112 {
113 m_needParent = true;
114
115 if (!PreCreation( parent, pos, size ) ||
116 !CreateBase( parent, id, pos, size, style, validator, name ))
117 {
118 wxFAIL_MSG( wxT("wxButton creation failed") );
119 return false;
120 }
121
122 m_widget = gtk_button_new_with_mnemonic("");
123
124 float x_alignment = 0.5;
125 if (HasFlag(wxBU_LEFT))
126 x_alignment = 0.0;
127 else if (HasFlag(wxBU_RIGHT))
128 x_alignment = 1.0;
129
130 float y_alignment = 0.5;
131 if (HasFlag(wxBU_TOP))
132 y_alignment = 0.0;
133 else if (HasFlag(wxBU_BOTTOM))
134 y_alignment = 1.0;
135
136 #ifdef __WXGTK24__
137 if (!gtk_check_version(2,4,0))
138 {
139 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
140 }
141 else
142 #endif
143 {
144 if (GTK_IS_MISC(GTK_BIN(m_widget)->child))
145 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(m_widget)->child),
146 x_alignment, y_alignment);
147 }
148
149 SetLabel(label);
150
151 if (style & wxNO_BORDER)
152 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
153
154 g_signal_connect_after (m_widget, "clicked",
155 G_CALLBACK (gtk_button_clicked_callback),
156 this);
157
158 g_signal_connect_after (m_widget, "style_set",
159 G_CALLBACK (gtk_button_style_set_callback),
160 this);
161
162 m_parent->DoAddChild( this );
163
164 PostCreation(size);
165
166 return true;
167 }
168
169
170 void wxButton::SetDefault()
171 {
172 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
173 wxCHECK_RET( tlw, _T("button without top level window?") );
174
175 tlw->SetDefaultItem(this);
176
177 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
178 gtk_widget_grab_default( m_widget );
179
180 // resize for default border
181 gtk_button_style_set_callback( m_widget, NULL, this );
182 }
183
184 /* static */
185 wxSize wxButtonBase::GetDefaultSize()
186 {
187 static wxSize size = wxDefaultSize;
188 if (size == wxDefaultSize)
189 {
190 // NB: Default size of buttons should be same as size of stock
191 // buttons as used in most GTK+ apps. Unfortunately it's a little
192 // tricky to obtain this size: stock button's size may be smaller
193 // than size of button in GtkButtonBox and vice versa,
194 // GtkButtonBox's minimal button size may be smaller than stock
195 // button's size. We have to retrieve both values and combine them.
196
197 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
198 GtkWidget *box = gtk_hbutton_box_new();
199 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
200 gtk_container_add(GTK_CONTAINER(box), btn);
201 gtk_container_add(GTK_CONTAINER(wnd), box);
202 GtkRequisition req;
203 gtk_widget_size_request(btn, &req);
204
205 gint minwidth, minheight;
206 gtk_widget_style_get(box,
207 "child-min-width", &minwidth,
208 "child-min-height", &minheight,
209 NULL);
210
211 size.x = wxMax(minwidth, req.width);
212 size.y = wxMax(minheight, req.height);
213
214 gtk_widget_destroy(wnd);
215 }
216 return size;
217 }
218
219 void wxButton::SetLabel( const wxString &lbl )
220 {
221 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
222
223 wxString label(lbl);
224
225 if (label.empty() && wxIsStockID(m_windowId))
226 label = wxGetStockLabel(m_windowId);
227
228 wxControl::SetLabel(label);
229
230 const wxString labelGTK = GTKConvertMnemonics(label);
231
232 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
233 {
234 const char *stock = wxGetStockGtkID(m_windowId);
235 if (stock)
236 {
237 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
238 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
239 return;
240 }
241 }
242
243 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
244 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
245
246 ApplyWidgetStyle( false );
247 }
248
249 bool wxButton::Enable( bool enable )
250 {
251 if ( !wxControl::Enable( enable ) )
252 return false;
253
254 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
255
256 return true;
257 }
258
259 GdkWindow *wxButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
260 {
261 return GTK_BUTTON(m_widget)->event_window;
262 }
263
264 void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
265 {
266 gtk_widget_modify_style(m_widget, style);
267 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
268 }
269
270 wxSize wxButton::DoGetBestSize() const
271 {
272 // the default button in wxGTK is bigger than the other ones because of an
273 // extra border around it, but we don't want to take it into account in
274 // our size calculations (otherwsie the result is visually ugly), so
275 // always return the size of non default button from here
276 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
277 if ( isDefault )
278 {
279 // temporarily unset default flag
280 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
281 }
282
283 wxSize ret( wxControl::DoGetBestSize() );
284
285 if ( isDefault )
286 {
287 // set it back again
288 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
289 }
290
291 if (!HasFlag(wxBU_EXACTFIT))
292 {
293 wxSize defaultSize = GetDefaultSize();
294 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
295 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
296 }
297
298 CacheBestSize(ret);
299 return ret;
300 }
301
302 // static
303 wxVisualAttributes
304 wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
305 {
306 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
307 }
308
309 #endif // wxUSE_BUTTON