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