]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: button.cpp | |
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 | #include "wx/defs.h" |
14 | ||
15 | #if wxUSE_BUTTON | |
16 | ||
c801d85f | 17 | #include "wx/button.h" |
5f7bcb48 | 18 | #include "wx/stockitem.h" |
c801d85f | 19 | |
9e691f46 | 20 | #include "wx/gtk/private.h" |
f893066b | 21 | #include "wx/gtk/win_gtk.h" |
83624f79 | 22 | |
c801d85f KB |
23 | //----------------------------------------------------------------------------- |
24 | // classes | |
25 | //----------------------------------------------------------------------------- | |
26 | ||
27 | class wxButton; | |
28 | ||
acfd422a RR |
29 | //----------------------------------------------------------------------------- |
30 | // idle system | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
33 | extern void wxapp_install_idle_handler(); | |
34 | extern bool g_isIdle; | |
35 | ||
66bd6b93 RR |
36 | //----------------------------------------------------------------------------- |
37 | // data | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
40 | extern bool g_blockEventsOnDrag; | |
41 | ||
c801d85f | 42 | //----------------------------------------------------------------------------- |
e1e955e1 | 43 | // "clicked" |
c801d85f KB |
44 | //----------------------------------------------------------------------------- |
45 | ||
865bb325 | 46 | extern "C" { |
66bd6b93 | 47 | static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button ) |
c801d85f | 48 | { |
9e691f46 | 49 | if (g_isIdle) |
32ac755d | 50 | wxapp_install_idle_handler(); |
acfd422a | 51 | |
a2053b27 | 52 | if (!button->m_hasVMT) return; |
acfd422a | 53 | if (g_blockEventsOnDrag) return; |
9e691f46 | 54 | |
acfd422a RR |
55 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId()); |
56 | event.SetEventObject(button); | |
57 | button->GetEventHandler()->ProcessEvent(event); | |
6de97a3b | 58 | } |
865bb325 | 59 | } |
c801d85f | 60 | |
a90c0600 RR |
61 | //----------------------------------------------------------------------------- |
62 | // "style_set" from m_widget | |
63 | //----------------------------------------------------------------------------- | |
64 | ||
65 | static gint | |
66 | gtk_button_style_set_callback( GtkWidget *m_widget, GtkStyle *WXUNUSED(style), wxButton *win ) | |
67 | { | |
68 | if (g_isIdle) | |
69 | wxapp_install_idle_handler(); | |
b2ff89d6 | 70 | |
f893066b RR |
71 | int left_border = 0; |
72 | int right_border = 0; | |
73 | int top_border = 0; | |
74 | int bottom_border = 0; | |
b2ff89d6 | 75 | |
f893066b RR |
76 | /* the default button has a border around it */ |
77 | if (GTK_WIDGET_CAN_DEFAULT(m_widget)) | |
78 | { | |
f893066b RR |
79 | GtkBorder *default_border = NULL; |
80 | gtk_widget_style_get( m_widget, "default_border", &default_border, NULL ); | |
81 | if (default_border) | |
82 | { | |
83 | left_border += default_border->left; | |
84 | right_border += default_border->right; | |
85 | top_border += default_border->top; | |
86 | bottom_border += default_border->bottom; | |
87 | g_free( default_border ); | |
88 | } | |
f893066b RR |
89 | win->DoMoveWindow( win->m_x-top_border, |
90 | win->m_y-left_border, | |
91 | win->m_width+left_border+right_border, | |
92 | win->m_height+top_border+bottom_border ); | |
b2ff89d6 | 93 | } |
a90c0600 RR |
94 | |
95 | return FALSE; | |
96 | } | |
97 | ||
c801d85f | 98 | //----------------------------------------------------------------------------- |
e1e955e1 RR |
99 | // wxButton |
100 | //----------------------------------------------------------------------------- | |
101 | ||
102 | IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl) | |
c801d85f | 103 | |
fd0eed64 | 104 | wxButton::wxButton() |
c801d85f | 105 | { |
6de97a3b | 106 | } |
c801d85f | 107 | |
fd0eed64 RR |
108 | wxButton::~wxButton() |
109 | { | |
fd0eed64 RR |
110 | } |
111 | ||
c801d85f | 112 | bool wxButton::Create( wxWindow *parent, wxWindowID id, const wxString &label, |
32c77a71 | 113 | const wxPoint &pos, const wxSize &size, |
6de97a3b | 114 | long style, const wxValidator& validator, const wxString &name ) |
c801d85f | 115 | { |
b292e2f5 RR |
116 | m_needParent = TRUE; |
117 | m_acceptsFocus = TRUE; | |
32c77a71 | 118 | |
4dcaf11a RR |
119 | if (!PreCreation( parent, pos, size ) || |
120 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
121 | { | |
223d09f6 | 122 | wxFAIL_MSG( wxT("wxButton creation failed") ); |
8ab696e0 | 123 | return FALSE; |
4dcaf11a | 124 | } |
c801d85f | 125 | |
5f7bcb48 | 126 | m_widget = gtk_button_new_with_mnemonic(""); |
354aa1e3 | 127 | |
2e8613b7 RR |
128 | float x_alignment = 0.5; |
129 | if (HasFlag(wxBU_LEFT)) | |
130 | x_alignment = 0.0; | |
131 | else if (HasFlag(wxBU_RIGHT)) | |
132 | x_alignment = 1.0; | |
133 | ||
134 | float y_alignment = 0.5; | |
135 | if (HasFlag(wxBU_TOP)) | |
136 | y_alignment = 0.0; | |
137 | else if (HasFlag(wxBU_BOTTOM)) | |
138 | y_alignment = 1.0; | |
139 | ||
3dc786e0 | 140 | #ifdef __WXGTK24__ |
77f70672 RR |
141 | if (!gtk_check_version(2,4,0)) |
142 | { | |
143 | gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment); | |
144 | } | |
145 | else | |
4fa87bd9 | 146 | #endif |
77f70672 | 147 | { |
afa7bd1e MR |
148 | if (GTK_IS_MISC(GTK_BIN(m_widget)->child)) |
149 | gtk_misc_set_alignment(GTK_MISC(GTK_BIN(m_widget)->child), | |
77f70672 RR |
150 | x_alignment, y_alignment); |
151 | } | |
a696db45 | 152 | |
5f7bcb48 | 153 | SetLabel(label); |
354aa1e3 | 154 | |
de1c750f RR |
155 | if (style & wxNO_BORDER) |
156 | gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE ); | |
de1c750f | 157 | |
9fa72bd2 MR |
158 | g_signal_connect_after (m_widget, "clicked", |
159 | G_CALLBACK (gtk_button_clicked_callback), | |
160 | this); | |
c801d85f | 161 | |
9fa72bd2 MR |
162 | g_signal_connect_after (m_widget, "style_set", |
163 | G_CALLBACK (gtk_button_style_set_callback), | |
164 | this); | |
b2ff89d6 | 165 | |
f03fc89f | 166 | m_parent->DoAddChild( this ); |
9e691f46 | 167 | |
abdeb9e7 | 168 | PostCreation(size); |
db434467 | 169 | |
4fa87bd9 VS |
170 | return true; |
171 | } | |
b2ff89d6 | 172 | |
32c77a71 | 173 | |
da048e3d | 174 | void wxButton::SetDefault() |
c801d85f | 175 | { |
97149f18 RD |
176 | wxWindow *parent = GetParent(); |
177 | wxCHECK_RET( parent, _T("button without parent?") ); | |
178 | ||
a00b2d0a | 179 | parent->SetDefaultItem(this); |
b2ff89d6 | 180 | |
3502e687 RR |
181 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); |
182 | gtk_widget_grab_default( m_widget ); | |
b2ff89d6 | 183 | |
f893066b RR |
184 | // resize for default border |
185 | gtk_button_style_set_callback( m_widget, NULL, this ); | |
6de97a3b | 186 | } |
c801d85f | 187 | |
ebea0891 | 188 | /* static */ |
4fa87bd9 | 189 | wxSize wxButtonBase::GetDefaultSize() |
8dbf4589 | 190 | { |
4fa87bd9 VS |
191 | static wxSize size = wxDefaultSize; |
192 | if (size == wxDefaultSize) | |
193 | { | |
194 | // NB: Default size of buttons should be same as size of stock | |
195 | // buttons as used in most GTK+ apps. Unfortunately it's a little | |
196 | // tricky to obtain this size: stock button's size may be smaller | |
197 | // than size of button in GtkButtonBox and vice versa, | |
198 | // GtkButtonBox's minimal button size may be smaller than stock | |
199 | // button's size. We have to retrieve both values and combine them. | |
200 | ||
201 | GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
202 | GtkWidget *box = gtk_hbutton_box_new(); | |
203 | GtkWidget *btn = gtk_button_new_from_stock(GTK_STOCK_CANCEL); | |
204 | gtk_container_add(GTK_CONTAINER(box), btn); | |
205 | gtk_container_add(GTK_CONTAINER(wnd), box); | |
206 | GtkRequisition req; | |
207 | gtk_widget_size_request(btn, &req); | |
208 | ||
209 | gint minwidth, minheight; | |
210 | gtk_widget_style_get(box, | |
211 | "child-min-width", &minwidth, | |
212 | "child-min-height", &minheight, | |
213 | NULL); | |
214 | ||
215 | size.x = wxMax(minwidth, req.width); | |
216 | size.y = wxMax(minheight, req.height); | |
b2ff89d6 | 217 | |
4fa87bd9 VS |
218 | gtk_widget_destroy(wnd); |
219 | } | |
220 | return size; | |
8dbf4589 RR |
221 | } |
222 | ||
5f7bcb48 | 223 | void wxButton::SetLabel( const wxString &lbl ) |
c801d85f | 224 | { |
223d09f6 | 225 | wxCHECK_RET( m_widget != NULL, wxT("invalid button") ); |
9e691f46 | 226 | |
5f7bcb48 VS |
227 | wxString label(lbl); |
228 | ||
5f7bcb48 VS |
229 | if (label.empty() && wxIsStockID(m_windowId)) |
230 | label = wxGetStockLabel(m_windowId); | |
5f7bcb48 VS |
231 | |
232 | wxControl::SetLabel(label); | |
9e691f46 | 233 | |
b2ff89d6 VZ |
234 | const wxString labelGTK = GTKConvertMnemonics(label); |
235 | ||
5f7bcb48 VS |
236 | if (wxIsStockID(m_windowId) && wxIsStockLabel(m_windowId, label)) |
237 | { | |
238 | const char *stock = wxGetStockGtkID(m_windowId); | |
239 | if (stock) | |
240 | { | |
241 | gtk_button_set_label(GTK_BUTTON(m_widget), stock); | |
242 | gtk_button_set_use_stock(GTK_BUTTON(m_widget), TRUE); | |
b04683b1 | 243 | return; |
5f7bcb48 | 244 | } |
5f7bcb48 VS |
245 | } |
246 | ||
b2ff89d6 | 247 | gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK)); |
5f7bcb48 | 248 | gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE); |
b2ff89d6 | 249 | |
19ac2f44 | 250 | ApplyWidgetStyle( false ); |
6de97a3b | 251 | } |
c801d85f | 252 | |
f03fc89f | 253 | bool wxButton::Enable( bool enable ) |
a9c96bcc | 254 | { |
f03fc89f VZ |
255 | if ( !wxControl::Enable( enable ) ) |
256 | return FALSE; | |
9e691f46 | 257 | |
afa7bd1e | 258 | gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable); |
f03fc89f VZ |
259 | |
260 | return TRUE; | |
a9c96bcc RR |
261 | } |
262 | ||
2b5f62a0 VZ |
263 | bool wxButton::IsOwnGtkWindow( GdkWindow *window ) |
264 | { | |
2b5f62a0 | 265 | return GTK_BUTTON(m_widget)->event_window; |
2b5f62a0 VZ |
266 | } |
267 | ||
f40fdaa3 | 268 | void wxButton::DoApplyWidgetStyle(GtkRcStyle *style) |
868a2826 | 269 | { |
f40fdaa3 | 270 | gtk_widget_modify_style(m_widget, style); |
afa7bd1e | 271 | gtk_widget_modify_style(GTK_BIN(m_widget)->child, style); |
a81258be | 272 | } |
db434467 RR |
273 | |
274 | wxSize wxButton::DoGetBestSize() const | |
275 | { | |
4f819fe4 VZ |
276 | // the default button in wxGTK is bigger than the other ones because of an |
277 | // extra border around it, but we don't want to take it into account in | |
278 | // our size calculations (otherwsie the result is visually ugly), so | |
279 | // always return the size of non default button from here | |
280 | const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget); | |
281 | if ( isDefault ) | |
282 | { | |
283 | // temporarily unset default flag | |
284 | GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
285 | } | |
286 | ||
db434467 | 287 | wxSize ret( wxControl::DoGetBestSize() ); |
9e691f46 | 288 | |
4f819fe4 VZ |
289 | if ( isDefault ) |
290 | { | |
291 | // set it back again | |
292 | GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT ); | |
293 | } | |
294 | ||
8ab696e0 RR |
295 | if (!HasFlag(wxBU_EXACTFIT)) |
296 | { | |
4fa87bd9 VS |
297 | wxSize defaultSize = GetDefaultSize(); |
298 | if (ret.x < defaultSize.x) ret.x = defaultSize.x; | |
299 | if (ret.y < defaultSize.y) ret.y = defaultSize.y; | |
8ab696e0 | 300 | } |
9e691f46 | 301 | |
9f884528 | 302 | CacheBestSize(ret); |
db434467 RR |
303 | return ret; |
304 | } | |
305 | ||
9d522606 RD |
306 | // static |
307 | wxVisualAttributes | |
308 | wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) | |
309 | { | |
310 | return GetDefaultAttributesFromGTKWidget(gtk_button_new); | |
311 | } | |
312 | ||
1e6feb95 VZ |
313 | #endif // wxUSE_BUTTON |
314 |