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