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