]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/button.cpp
Add some version checks to help compiling on OSX.
[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 #ifndef WX_PRECOMP
16 #include "wx/button.h"
17 #endif
18
19 #include "wx/stockitem.h"
20
21 #include "wx/gtk/private.h"
22
23 // ----------------------------------------------------------------------------
24 // GTK callbacks
25 // ----------------------------------------------------------------------------
26
27 extern "C"
28 {
29
30 static void
31 wxgtk_button_clicked_callback(GtkWidget *WXUNUSED(widget), wxButton *button)
32 {
33 if ( button->GTKShouldIgnoreEvent() )
34 return;
35
36 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
37 event.SetEventObject(button);
38 button->HandleWindowEvent(event);
39 }
40
41 //-----------------------------------------------------------------------------
42 // "style_set" from m_widget
43 //-----------------------------------------------------------------------------
44
45 static void
46 wxgtk_button_style_set_callback(GtkWidget* widget, GtkStyle*, wxButton* win)
47 {
48 /* the default button has a border around it */
49 wxWindow* parent = win->GetParent();
50 if (parent && parent->m_wxwindow && gtk_widget_get_can_default(widget))
51 {
52 GtkBorder* border = NULL;
53 gtk_widget_style_get(widget, "default_border", &border, NULL);
54 if (border)
55 {
56 win->MoveWindow(
57 win->m_x - border->left,
58 win->m_y - border->top,
59 win->m_width + border->left + border->right,
60 win->m_height + border->top + border->bottom);
61 gtk_border_free(border);
62 }
63 }
64 }
65
66 } // extern "C"
67
68 //-----------------------------------------------------------------------------
69 // wxButton
70 //-----------------------------------------------------------------------------
71
72 bool wxButton::Create(wxWindow *parent,
73 wxWindowID id,
74 const wxString &label,
75 const wxPoint& pos,
76 const wxSize& size,
77 long style,
78 const wxValidator& validator,
79 const wxString& name)
80 {
81 if (!PreCreation( parent, pos, size ) ||
82 !CreateBase( parent, id, pos, size, style, validator, name ))
83 {
84 wxFAIL_MSG( wxT("wxButton creation failed") );
85 return false;
86 }
87
88 // create either a standard button with text label (which may still contain
89 // an image under GTK+ 2.6+) or a bitmap-only button if we don't have any
90 // label
91 const bool
92 useLabel = !(style & wxBU_NOTEXT) && (!label.empty() || wxIsStockID(id));
93 if ( useLabel )
94 {
95 m_widget = gtk_button_new_with_mnemonic("");
96 }
97 else // no label, suppose we will have a bitmap
98 {
99 m_widget = gtk_button_new();
100
101 GtkWidget *image = gtk_image_new();
102 gtk_widget_show(image);
103 gtk_container_add(GTK_CONTAINER(m_widget), image);
104 }
105
106 g_object_ref(m_widget);
107
108 float x_alignment = 0.5;
109 if (HasFlag(wxBU_LEFT))
110 x_alignment = 0.0;
111 else if (HasFlag(wxBU_RIGHT))
112 x_alignment = 1.0;
113
114 float y_alignment = 0.5;
115 if (HasFlag(wxBU_TOP))
116 y_alignment = 0.0;
117 else if (HasFlag(wxBU_BOTTOM))
118 y_alignment = 1.0;
119
120 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
121
122 if ( useLabel )
123 SetLabel(label);
124
125 if (style & wxNO_BORDER)
126 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
127
128 g_signal_connect_after (m_widget, "clicked",
129 G_CALLBACK (wxgtk_button_clicked_callback),
130 this);
131
132 g_signal_connect_after (m_widget, "style_set",
133 G_CALLBACK (wxgtk_button_style_set_callback),
134 this);
135
136 m_parent->DoAddChild( this );
137
138 PostCreation(size);
139
140 return true;
141 }
142
143
144 wxWindow *wxButton::SetDefault()
145 {
146 wxWindow *oldDefault = wxButtonBase::SetDefault();
147
148 gtk_widget_set_can_default(m_widget, TRUE);
149 gtk_widget_grab_default( m_widget );
150
151 // resize for default border
152 wxgtk_button_style_set_callback( m_widget, NULL, this );
153
154 return oldDefault;
155 }
156
157 /* static */
158 wxSize wxButtonBase::GetDefaultSize()
159 {
160 static wxSize size = wxDefaultSize;
161 if (size == wxDefaultSize)
162 {
163 // NB: Default size of buttons should be same as size of stock
164 // buttons as used in most GTK+ apps. Unfortunately it's a little
165 // tricky to obtain this size: stock button's size may be smaller
166 // than size of button in GtkButtonBox and vice versa,
167 // GtkButtonBox's minimal button size may be smaller than stock
168 // button's size. We have to retrieve both values and combine them.
169
170 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
171 GtkWidget *box = gtk_hbutton_box_new();
172 GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
173 gtk_container_add(GTK_CONTAINER(box), btn);
174 gtk_container_add(GTK_CONTAINER(wnd), box);
175 GtkRequisition req;
176 gtk_widget_size_request(btn, &req);
177
178 gint minwidth, minheight;
179 gtk_widget_style_get(box,
180 "child-min-width", &minwidth,
181 "child-min-height", &minheight,
182 NULL);
183
184 size.x = wxMax(minwidth, req.width);
185 size.y = wxMax(minheight, req.height);
186
187 gtk_widget_destroy(wnd);
188 }
189 return size;
190 }
191
192 void wxButton::SetLabel( const wxString &lbl )
193 {
194 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
195
196 wxString label(lbl);
197
198 if (label.empty() && wxIsStockID(m_windowId))
199 label = wxGetStockLabel(m_windowId);
200
201 wxAnyButton::SetLabel(label);
202
203 // don't use label if it was explicitly disabled
204 if ( HasFlag(wxBU_NOTEXT) )
205 return;
206
207 if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label))
208 {
209 const char *stock = wxGetStockGtkID(m_windowId);
210 if (stock)
211 {
212 gtk_button_set_label(GTK_BUTTON(m_widget), stock);
213 gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE);
214 return;
215 }
216 }
217
218 // this call is necessary if the button had been initially created without
219 // a (text) label -- then we didn't use gtk_button_new_with_mnemonic() and
220 // so "use-underline" GtkButton property remained unset
221 gtk_button_set_use_underline(GTK_BUTTON(m_widget), TRUE);
222 const wxString labelGTK = GTKConvertMnemonics(label);
223 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
224 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
225
226 GTKApplyWidgetStyle( false );
227 }
228
229 #if wxUSE_MARKUP
230 bool wxButton::DoSetLabelMarkup(const wxString& markup)
231 {
232 wxCHECK_MSG( m_widget != NULL, false, "invalid button" );
233
234 const wxString stripped = RemoveMarkup(markup);
235 if ( stripped.empty() && !markup.empty() )
236 return false;
237
238 wxControl::SetLabel(stripped);
239
240 GtkLabel * const label = GTKGetLabel();
241 wxCHECK_MSG( label, false, "no label in this button?" );
242
243 GTKSetLabelWithMarkupForLabel(label, markup);
244
245 return true;
246 }
247
248 GtkLabel *wxButton::GTKGetLabel() const
249 {
250 GtkWidget* child = gtk_bin_get_child(GTK_BIN(m_widget));
251 if ( GTK_IS_ALIGNMENT(child) )
252 {
253 GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
254 GtkLabel* label = NULL;
255 GList* list = gtk_container_get_children(GTK_CONTAINER(box));
256 for (GList* item = list; item; item = item->next)
257 {
258 if (GTK_IS_LABEL(item->data))
259 label = GTK_LABEL(item->data);
260 }
261 g_list_free(list);
262
263 return label;
264 }
265
266 return GTK_LABEL(child);
267 }
268 #endif // wxUSE_MARKUP
269
270 void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
271 {
272 gtk_widget_modify_style(m_widget, style);
273 GtkWidget* child = gtk_bin_get_child(GTK_BIN(m_widget));
274 gtk_widget_modify_style(child, style);
275
276 // for buttons with images, the path to the label is (at least in 2.12)
277 // GtkButton -> GtkAlignment -> GtkHBox -> GtkLabel
278 if ( GTK_IS_ALIGNMENT(child) )
279 {
280 GtkWidget* box = gtk_bin_get_child(GTK_BIN(child));
281 if ( GTK_IS_BOX(box) )
282 {
283 GList* list = gtk_container_get_children(GTK_CONTAINER(box));
284 for (GList* item = list; item; item = item->next)
285 {
286 gtk_widget_modify_style(GTK_WIDGET(item->data), style);
287 }
288 g_list_free(list);
289 }
290 }
291 }
292
293 wxSize wxButton::DoGetBestSize() const
294 {
295 // the default button in wxGTK is bigger than the other ones because of an
296 // extra border around it, but we don't want to take it into account in
297 // our size calculations (otherwise the result is visually ugly), so
298 // always return the size of non default button from here
299 const bool isDefault = gtk_widget_has_default(m_widget);
300 if ( isDefault )
301 {
302 // temporarily unset default flag
303 gtk_widget_set_can_default(m_widget, FALSE);
304 }
305
306 wxSize ret( wxAnyButton::DoGetBestSize() );
307
308 if ( isDefault )
309 {
310 // set it back again
311 gtk_widget_set_can_default(m_widget, TRUE);
312 }
313
314 if (!HasFlag(wxBU_EXACTFIT))
315 {
316 wxSize defaultSize = GetDefaultSize();
317 if (ret.x < defaultSize.x)
318 ret.x = defaultSize.x;
319 if (ret.y < defaultSize.y)
320 ret.y = defaultSize.y;
321 }
322
323 CacheBestSize(ret);
324 return ret;
325 }
326
327 // static
328 wxVisualAttributes
329 wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
330 {
331 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
332 }
333
334 #endif // wxUSE_BUTTON