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