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