]> git.saurik.com Git - wxWidgets.git/blob - src/univ/button.cpp
Always link with expat in monolithic build.
[wxWidgets.git] / src / univ / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/button.cpp
3 // Purpose: wxButton
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 14.08.00
7 // RCS-ID: $Id$
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_BUTTON
27
28 #ifndef WX_PRECOMP
29 #include "wx/dcclient.h"
30 #include "wx/dcscreen.h"
31 #include "wx/button.h"
32 #include "wx/validate.h"
33 #include "wx/settings.h"
34 #endif
35
36 #include "wx/univ/renderer.h"
37 #include "wx/univ/inphand.h"
38 #include "wx/univ/theme.h"
39 #include "wx/univ/colschem.h"
40 #include "wx/stockitem.h"
41
42 // ----------------------------------------------------------------------------
43 // wxStdButtonInputHandler: translates SPACE and ENTER keys and the left mouse
44 // click into button press/release actions
45 // ----------------------------------------------------------------------------
46
47 class WXDLLEXPORT wxStdButtonInputHandler : public wxStdInputHandler
48 {
49 public:
50 wxStdButtonInputHandler(wxInputHandler *inphand);
51
52 virtual bool HandleKey(wxInputConsumer *consumer,
53 const wxKeyEvent& event,
54 bool pressed);
55 virtual bool HandleMouse(wxInputConsumer *consumer,
56 const wxMouseEvent& event);
57 virtual bool HandleMouseMove(wxInputConsumer *consumer,
58 const wxMouseEvent& event);
59 virtual bool HandleFocus(wxInputConsumer *consumer,
60 const wxFocusEvent& event);
61 virtual bool HandleActivation(wxInputConsumer *consumer, bool activated);
62
63 private:
64 // the window (button) which has capture or NULL and the flag telling if
65 // the mouse is inside the button which captured it or not
66 wxWindow *m_winCapture;
67 bool m_winHasMouse;
68 };
69
70 // ----------------------------------------------------------------------------
71 // constants
72 // ----------------------------------------------------------------------------
73
74 // default margins around the image
75 static const wxCoord DEFAULT_BTN_MARGIN_X = 0; // We should give space for the border, at least.
76 static const wxCoord DEFAULT_BTN_MARGIN_Y = 0;
77
78 // ============================================================================
79 // implementation
80 // ============================================================================
81
82 // ----------------------------------------------------------------------------
83 // creation
84 // ----------------------------------------------------------------------------
85
86 void wxButton::Init()
87 {
88 m_isPressed =
89 m_isDefault = false;
90 }
91
92 bool wxButton::Create(wxWindow *parent,
93 wxWindowID id,
94 const wxBitmap& bitmap,
95 const wxString &lbl,
96 const wxPoint &pos,
97 const wxSize &size,
98 long style,
99 const wxValidator& validator,
100 const wxString &name)
101 {
102 wxString label(lbl);
103 if (label.empty() && wxIsStockID(id))
104 label = wxGetStockLabel(id);
105
106 long ctrl_style = style & ~wxBU_ALIGN_MASK;
107 ctrl_style = ctrl_style & ~wxALIGN_MASK;
108
109 if((style & wxBU_RIGHT) == wxBU_RIGHT)
110 ctrl_style |= wxALIGN_RIGHT;
111 else if((style & wxBU_LEFT) == wxBU_LEFT)
112 ctrl_style |= wxALIGN_LEFT;
113 else
114 ctrl_style |= wxALIGN_CENTRE_HORIZONTAL;
115
116 if((style & wxBU_TOP) == wxBU_TOP)
117 ctrl_style |= wxALIGN_TOP;
118 else if((style & wxBU_BOTTOM) == wxBU_BOTTOM)
119 ctrl_style |= wxALIGN_BOTTOM;
120 else
121 ctrl_style |= wxALIGN_CENTRE_VERTICAL;
122
123 if ( !wxControl::Create(parent, id, pos, size, ctrl_style, validator, name) )
124 return false;
125
126 SetLabel(label);
127
128 if (bitmap.IsOk())
129 SetBitmap(bitmap); // SetInitialSize called by SetBitmap()
130 else
131 SetInitialSize(size);
132
133 CreateInputHandler(wxINP_HANDLER_BUTTON);
134
135 return true;
136 }
137
138 wxButton::~wxButton()
139 {
140 }
141
142 // ----------------------------------------------------------------------------
143 // size management
144 // ----------------------------------------------------------------------------
145
146 /* static */
147 wxSize wxButtonBase::GetDefaultSize()
148 {
149 static wxSize s_sizeBtn;
150
151 if ( s_sizeBtn.x == 0 )
152 {
153 wxScreenDC dc;
154
155 // this corresponds more or less to wxMSW standard in Win32 theme (see
156 // wxWin32Renderer::AdjustSize())
157 // s_sizeBtn.x = 8*dc.GetCharWidth();
158 // s_sizeBtn.y = (11*dc.GetCharHeight())/10 + 2;
159 // Otto Wyss, Patch 664399
160 s_sizeBtn.x = dc.GetCharWidth()*10 + 2;
161 s_sizeBtn.y = dc.GetCharHeight()*11/10 + 2;
162 }
163
164 return s_sizeBtn;
165 }
166
167 wxSize wxButton::DoGetBestClientSize() const
168 {
169 wxClientDC dc(wxConstCast(this, wxButton));
170 wxCoord width, height;
171 dc.GetMultiLineTextExtent(GetLabel(), &width, &height);
172
173 if ( m_bitmap.IsOk() )
174 {
175 // allocate extra space for the bitmap
176 wxCoord heightBmp = m_bitmap.GetHeight() + 2*m_marginBmpY;
177 if ( height < heightBmp )
178 height = heightBmp;
179
180 width += m_bitmap.GetWidth() + 2*m_marginBmpX;
181 }
182
183 // The default size should not be adjusted, so the code is moved into the
184 // renderer. This is conceptual wrong but currently the only solution.
185 // (Otto Wyss, Patch 664399)
186
187 /*
188 // for compatibility with other ports, the buttons default size is never
189 // less than the standard one, but not when display not PDAs.
190 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
191 {
192 if ( !(GetWindowStyle() & wxBU_EXACTFIT) )
193 {
194 wxSize szDef = GetDefaultSize();
195 if ( width < szDef.x )
196 width = szDef.x;
197 }
198 }
199 */
200 return wxSize(width, height);
201 }
202
203 // ----------------------------------------------------------------------------
204 // drawing
205 // ----------------------------------------------------------------------------
206
207 void wxButton::DoDraw(wxControlRenderer *renderer)
208 {
209 if ( !(GetWindowStyle() & wxBORDER_NONE) )
210 {
211 renderer->DrawButtonBorder();
212 }
213
214 renderer->DrawButtonLabel(m_bitmap, m_marginBmpX, m_marginBmpY);
215 }
216
217 bool wxButton::DoDrawBackground(wxDC& dc)
218 {
219 wxRect rect;
220 wxSize size = GetSize();
221 rect.width = size.x;
222 rect.height = size.y;
223
224 if ( GetBackgroundBitmap().IsOk() )
225 {
226 // get the bitmap and the flags
227 int alignment;
228 wxStretch stretch;
229 wxBitmap bmp = GetBackgroundBitmap(&alignment, &stretch);
230 wxControlRenderer::DrawBitmap(dc, bmp, rect, alignment, stretch);
231 }
232 else
233 {
234 m_renderer->DrawButtonSurface(dc, wxTHEME_BG_COLOUR(this),
235 rect, GetStateFlags());
236 }
237
238 return true;
239 }
240
241 // ----------------------------------------------------------------------------
242 // input processing
243 // ----------------------------------------------------------------------------
244
245 void wxButton::Press()
246 {
247 if ( !m_isPressed )
248 {
249 m_isPressed = true;
250
251 Refresh();
252 }
253 }
254
255 void wxButton::Release()
256 {
257 if ( m_isPressed )
258 {
259 m_isPressed = false;
260
261 Refresh();
262 }
263 }
264
265 void wxButton::Toggle()
266 {
267 if ( m_isPressed )
268 Release();
269 else
270 Press();
271
272 if ( !m_isPressed )
273 {
274 // releasing button after it had been pressed generates a click event
275 Click();
276 }
277 }
278
279 void wxButton::Click()
280 {
281 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
282 InitCommandEvent(event);
283 Command(event);
284 }
285
286 bool wxButton::PerformAction(const wxControlAction& action,
287 long numArg,
288 const wxString& strArg)
289 {
290 if ( action == wxACTION_BUTTON_TOGGLE )
291 Toggle();
292 else if ( action == wxACTION_BUTTON_CLICK )
293 Click();
294 else if ( action == wxACTION_BUTTON_PRESS )
295 Press();
296 else if ( action == wxACTION_BUTTON_RELEASE )
297 Release();
298 else
299 return wxControl::PerformAction(action, numArg, strArg);
300
301 return true;
302 }
303
304 /* static */
305 wxInputHandler *wxButton::GetStdInputHandler(wxInputHandler *handlerDef)
306 {
307 static wxStdButtonInputHandler s_handlerBtn(handlerDef);
308
309 return &s_handlerBtn;
310 }
311
312 // ----------------------------------------------------------------------------
313 // misc
314 // ----------------------------------------------------------------------------
315
316 void wxButton::DoSetBitmap(const wxBitmap& bitmap, State which)
317 {
318 // we support only one bitmap right now, although this wouldn't be
319 // difficult to change
320 if ( which == State_Normal )
321 m_bitmap = bitmap;
322
323 SetBitmapMargins(DEFAULT_BTN_MARGIN_X, DEFAULT_BTN_MARGIN_Y);
324 }
325
326 void wxButton::DoSetBitmapMargins(wxCoord x, wxCoord y)
327 {
328 m_marginBmpX = x + 2;
329 m_marginBmpY = y + 2;
330
331 SetInitialSize(wxDefaultSize);
332 }
333
334 wxWindow *wxButton::SetDefault()
335 {
336 m_isDefault = true;
337
338 return wxButtonBase::SetDefault();
339 }
340
341 // ============================================================================
342 // wxStdButtonInputHandler
343 // ============================================================================
344
345 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler *handler)
346 : wxStdInputHandler(handler)
347 {
348 m_winCapture = NULL;
349 m_winHasMouse = false;
350 }
351
352 bool wxStdButtonInputHandler::HandleKey(wxInputConsumer *consumer,
353 const wxKeyEvent& event,
354 bool pressed)
355 {
356 int keycode = event.GetKeyCode();
357 if ( keycode == WXK_SPACE || keycode == WXK_RETURN )
358 {
359 consumer->PerformAction(wxACTION_BUTTON_TOGGLE);
360
361 return true;
362 }
363
364 return wxStdInputHandler::HandleKey(consumer, event, pressed);
365 }
366
367 bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer *consumer,
368 const wxMouseEvent& event)
369 {
370 // the button has 2 states: pressed and normal with the following
371 // transitions between them:
372 //
373 // normal -> left down -> capture mouse and go to pressed state
374 // pressed -> left up inside -> generate click -> go to normal
375 // outside ------------------>
376 //
377 // the other mouse buttons are ignored
378 if ( event.Button(1) )
379 {
380 if ( event.LeftDown() || event.LeftDClick() )
381 {
382 m_winCapture = consumer->GetInputWindow();
383 m_winCapture->CaptureMouse();
384 m_winHasMouse = true;
385
386 consumer->PerformAction(wxACTION_BUTTON_PRESS);
387
388 return true;
389 }
390 else if ( event.LeftUp() )
391 {
392 if ( m_winCapture )
393 {
394 m_winCapture->ReleaseMouse();
395 m_winCapture = NULL;
396 }
397
398 if ( m_winHasMouse )
399 {
400 // this will generate a click event
401 consumer->PerformAction(wxACTION_BUTTON_TOGGLE);
402
403 return true;
404 }
405 //else: the mouse was released outside the window, this doesn't
406 // count as a click
407 }
408 //else: don't do anything special about the double click
409 }
410
411 return wxStdInputHandler::HandleMouse(consumer, event);
412 }
413
414 bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer *consumer,
415 const wxMouseEvent& event)
416 {
417 // we only have to do something when the mouse leaves/enters the pressed
418 // button and don't care about the other ones
419 if ( event.GetEventObject() == m_winCapture )
420 {
421 // leaving the button should remove its pressed state
422 if ( event.Leaving() )
423 {
424 // remember that the mouse is now outside
425 m_winHasMouse = false;
426
427 // we do have a pressed button, so release it
428 consumer->GetInputWindow()->SetCurrent(false);
429 consumer->PerformAction(wxACTION_BUTTON_RELEASE);
430
431 return true;
432 }
433 // and entering it back should make it pressed again if it had been
434 // pressed
435 else if ( event.Entering() )
436 {
437 // the mouse is (back) inside the button
438 m_winHasMouse = true;
439
440 // we did have a pressed button which we released when leaving the
441 // window, press it again
442 consumer->GetInputWindow()->SetCurrent(true);
443 consumer->PerformAction(wxACTION_BUTTON_PRESS);
444
445 return true;
446 }
447 }
448
449 return wxStdInputHandler::HandleMouseMove(consumer, event);
450 }
451
452 bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer * WXUNUSED(consumer),
453 const wxFocusEvent& WXUNUSED(event))
454 {
455 // buttons change appearance when they get/lose focus, so return true to
456 // refresh
457 return true;
458 }
459
460 bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer *consumer,
461 bool WXUNUSED(activated))
462 {
463 // the default button changes appearance when the app is [de]activated, so
464 // return true to refresh
465 return wxStaticCast(consumer->GetInputWindow(), wxButton)->IsDefault();
466 }
467
468 #endif // wxUSE_BUTTON
469