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