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