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