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