]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/button.cpp
Improve wxMenu docs
[wxWidgets.git] / src / gtk / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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 #if wxUSE_BUTTON
14
15 #ifndef WX_PRECOMP
16 #include "wx/button.h"
17 #endif
18
19 #include "wx/stockitem.h"
20
21 #include "wx/gtk/private.h"
22
23 //-----------------------------------------------------------------------------
24 // classes
25 //-----------------------------------------------------------------------------
26
27 class wxButton;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern bool g_blockEventsOnDrag;
34
35 //-----------------------------------------------------------------------------
36 // "clicked"
37 //-----------------------------------------------------------------------------
38
39 extern "C" {
40 static void gtk_button_clicked_callback( GtkWidget *WXUNUSED(widget), wxButton *button )
41 {
42 if (!button->m_hasVMT) return;
43 if (g_blockEventsOnDrag) return;
44
45 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, button->GetId());
46 event.SetEventObject(button);
47 button->HandleWindowEvent(event);
48 }
49 }
50
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 {
58 int left_border = 0;
59 int right_border = 0;
60 int top_border = 0;
61 int bottom_border = 0;
62
63 /* the default button has a border around it */
64 if (GTK_WIDGET_CAN_DEFAULT(m_widget))
65 {
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;
74 gtk_border_free( default_border );
75 }
76 win->MoveWindow(
77 win->m_x - left_border,
78 win->m_y - top_border,
79 win->m_width + left_border + right_border,
80 win->m_height + top_border + bottom_border);
81 }
82
83 return FALSE;
84 }
85
86 //-----------------------------------------------------------------------------
87 // wxButton
88 //-----------------------------------------------------------------------------
89
90 IMPLEMENT_DYNAMIC_CLASS(wxButton,wxControl)
91
92 wxButton::wxButton()
93 {
94 }
95
96 wxButton::~wxButton()
97 {
98 }
99
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)
108 {
109 if (!PreCreation( parent, pos, size ) ||
110 !CreateBase( parent, id, pos, size, style, validator, name ))
111 {
112 wxFAIL_MSG( wxT("wxButton creation failed") );
113 return false;
114 }
115
116 m_widget = gtk_button_new_with_mnemonic("");
117
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
130 gtk_button_set_alignment(GTK_BUTTON(m_widget), x_alignment, y_alignment);
131
132 SetLabel(label);
133
134 if (style & wxNO_BORDER)
135 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
136
137 g_signal_connect_after (m_widget, "clicked",
138 G_CALLBACK (gtk_button_clicked_callback),
139 this);
140
141 g_signal_connect_after (m_widget, "style_set",
142 G_CALLBACK (gtk_button_style_set_callback),
143 this);
144
145 m_parent->DoAddChild( this );
146
147 PostCreation(size);
148
149 return true;
150 }
151
152
153 wxWindow *wxButton::SetDefault()
154 {
155 wxWindow *oldDefault = wxButtonBase::SetDefault();
156
157 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
158 gtk_widget_grab_default( m_widget );
159
160 // resize for default border
161 gtk_button_style_set_callback( m_widget, NULL, this );
162
163 return oldDefault;
164 }
165
166 /* static */
167 wxSize wxButtonBase::GetDefaultSize()
168 {
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);
195
196 gtk_widget_destroy(wnd);
197 }
198 return size;
199 }
200
201 void wxButton::SetLabel( const wxString &lbl )
202 {
203 wxCHECK_RET( m_widget != NULL, wxT("invalid button") );
204
205 wxString label(lbl);
206
207 if (label.empty() && wxIsStockID(m_windowId))
208 label = wxGetStockLabel(m_windowId);
209
210 wxControl::SetLabel(label);
211
212 const wxString labelGTK = GTKConvertMnemonics(label);
213
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);
221 return;
222 }
223 }
224
225 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
226 gtk_button_set_use_stock(GTK_BUTTON(m_widget), FALSE);
227
228 ApplyWidgetStyle( false );
229 }
230
231 bool wxButton::Enable( bool enable )
232 {
233 if ( !wxControl::Enable( enable ) )
234 return false;
235
236 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
237
238 return true;
239 }
240
241 GdkWindow *wxButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
242 {
243 return GTK_BUTTON(m_widget)->event_window;
244 }
245
246 void wxButton::DoApplyWidgetStyle(GtkRcStyle *style)
247 {
248 gtk_widget_modify_style(m_widget, style);
249 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
250 }
251
252 wxSize wxButton::DoGetBestSize() const
253 {
254 // the default button in wxGTK is bigger than the other ones because of an
255 // extra border around it, but we don't want to take it into account in
256 // our size calculations (otherwsie the result is visually ugly), so
257 // always return the size of non default button from here
258 const bool isDefault = GTK_WIDGET_HAS_DEFAULT(m_widget);
259 if ( isDefault )
260 {
261 // temporarily unset default flag
262 GTK_WIDGET_UNSET_FLAGS( m_widget, GTK_CAN_DEFAULT );
263 }
264
265 wxSize ret( wxControl::DoGetBestSize() );
266
267 if ( isDefault )
268 {
269 // set it back again
270 GTK_WIDGET_SET_FLAGS( m_widget, GTK_CAN_DEFAULT );
271 }
272
273 if (!HasFlag(wxBU_EXACTFIT))
274 {
275 wxSize defaultSize = GetDefaultSize();
276 if (ret.x < defaultSize.x) ret.x = defaultSize.x;
277 if (ret.y < defaultSize.y) ret.y = defaultSize.y;
278 }
279
280 CacheBestSize(ret);
281 return ret;
282 }
283
284 // static
285 wxVisualAttributes
286 wxButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
287 {
288 return GetDefaultAttributesFromGTKWidget(gtk_button_new);
289 }
290
291 #endif // wxUSE_BUTTON