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