1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/button.cpp
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
28 #include "wx/dcclient.h"
29 #include "wx/dcscreen.h"
30 #include "wx/button.h"
31 #include "wx/validate.h"
32 #include "wx/settings.h"
35 #include "wx/univ/renderer.h"
36 #include "wx/univ/inphand.h"
37 #include "wx/univ/theme.h"
38 #include "wx/univ/colschem.h"
39 #include "wx/stockitem.h"
41 // ----------------------------------------------------------------------------
42 // wxStdButtonInputHandler: translates SPACE and ENTER keys and the left mouse
43 // click into button press/release actions
44 // ----------------------------------------------------------------------------
46 class WXDLLEXPORT wxStdButtonInputHandler
: public wxStdInputHandler
49 wxStdButtonInputHandler(wxInputHandler
*inphand
);
51 virtual bool HandleKey(wxInputConsumer
*consumer
,
52 const wxKeyEvent
& event
,
54 virtual bool HandleMouse(wxInputConsumer
*consumer
,
55 const wxMouseEvent
& event
);
56 virtual bool HandleMouseMove(wxInputConsumer
*consumer
,
57 const wxMouseEvent
& event
);
58 virtual bool HandleFocus(wxInputConsumer
*consumer
,
59 const wxFocusEvent
& event
);
60 virtual bool HandleActivation(wxInputConsumer
*consumer
, bool activated
);
63 // the window (button) which has capture or NULL and the flag telling if
64 // the mouse is inside the button which captured it or not
65 wxWindow
*m_winCapture
;
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // default margins around the image
74 static const wxCoord DEFAULT_BTN_MARGIN_X
= 0; // We should give space for the border, at least.
75 static const wxCoord DEFAULT_BTN_MARGIN_Y
= 0;
77 // ============================================================================
79 // ============================================================================
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
91 bool wxButton::Create(wxWindow
*parent
,
93 const wxBitmap
& bitmap
,
98 const wxValidator
& validator
,
102 if (label
.empty() && wxIsStockID(id
))
103 label
= wxGetStockLabel(id
);
105 long ctrl_style
= style
& ~wxBU_ALIGN_MASK
;
106 ctrl_style
= ctrl_style
& ~wxALIGN_MASK
;
108 if((style
& wxBU_RIGHT
) == wxBU_RIGHT
)
109 ctrl_style
|= wxALIGN_RIGHT
;
110 else if((style
& wxBU_LEFT
) == wxBU_LEFT
)
111 ctrl_style
|= wxALIGN_LEFT
;
113 ctrl_style
|= wxALIGN_CENTRE_HORIZONTAL
;
115 if((style
& wxBU_TOP
) == wxBU_TOP
)
116 ctrl_style
|= wxALIGN_TOP
;
117 else if((style
& wxBU_BOTTOM
) == wxBU_BOTTOM
)
118 ctrl_style
|= wxALIGN_BOTTOM
;
120 ctrl_style
|= wxALIGN_CENTRE_VERTICAL
;
122 if ( !wxControl::Create(parent
, id
, pos
, size
, ctrl_style
, validator
, name
) )
128 SetBitmap(bitmap
); // SetInitialSize called by SetBitmap()
130 SetInitialSize(size
);
132 CreateInputHandler(wxINP_HANDLER_BUTTON
);
137 wxButton::~wxButton()
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
146 wxSize
wxButtonBase::GetDefaultSize()
148 static wxSize s_sizeBtn
;
150 if ( s_sizeBtn
.x
== 0 )
154 // this corresponds more or less to wxMSW standard in Win32 theme (see
155 // wxWin32Renderer::AdjustSize())
156 // s_sizeBtn.x = 8*dc.GetCharWidth();
157 // s_sizeBtn.y = (11*dc.GetCharHeight())/10 + 2;
158 // Otto Wyss, Patch 664399
159 s_sizeBtn
.x
= dc
.GetCharWidth()*10 + 2;
160 s_sizeBtn
.y
= dc
.GetCharHeight()*11/10 + 2;
166 wxSize
wxButton::DoGetBestClientSize() const
168 wxClientDC
dc(wxConstCast(this, wxButton
));
169 wxCoord width
, height
;
170 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
172 if ( m_bitmap
.IsOk() )
174 // allocate extra space for the bitmap
175 wxCoord heightBmp
= m_bitmap
.GetHeight() + 2*m_marginBmpY
;
176 if ( height
< heightBmp
)
179 width
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
;
182 // The default size should not be adjusted, so the code is moved into the
183 // renderer. This is conceptual wrong but currently the only solution.
184 // (Otto Wyss, Patch 664399)
187 // for compatibility with other ports, the buttons default size is never
188 // less than the standard one, but not when display not PDAs.
189 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
191 if ( !(GetWindowStyle() & wxBU_EXACTFIT) )
193 wxSize szDef = GetDefaultSize();
194 if ( width < szDef.x )
199 return wxSize(width
, height
);
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 void wxButton::DoDraw(wxControlRenderer
*renderer
)
208 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
210 renderer
->DrawButtonBorder();
213 renderer
->DrawButtonLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
216 bool wxButton::DoDrawBackground(wxDC
& dc
)
219 wxSize size
= GetSize();
221 rect
.height
= size
.y
;
223 if ( GetBackgroundBitmap().IsOk() )
225 // get the bitmap and the flags
228 wxBitmap bmp
= GetBackgroundBitmap(&alignment
, &stretch
);
229 wxControlRenderer::DrawBitmap(dc
, bmp
, rect
, alignment
, stretch
);
233 m_renderer
->DrawButtonSurface(dc
, wxTHEME_BG_COLOUR(this),
234 rect
, GetStateFlags());
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 void wxButton::Press()
254 void wxButton::Release()
264 void wxButton::Toggle()
273 // releasing button after it had been pressed generates a click event
278 void wxButton::Click()
280 wxCommandEvent
event(wxEVT_BUTTON
, GetId());
281 InitCommandEvent(event
);
285 bool wxButton::PerformAction(const wxControlAction
& action
,
287 const wxString
& strArg
)
289 if ( action
== wxACTION_BUTTON_TOGGLE
)
291 else if ( action
== wxACTION_BUTTON_CLICK
)
293 else if ( action
== wxACTION_BUTTON_PRESS
)
295 else if ( action
== wxACTION_BUTTON_RELEASE
)
298 return wxControl::PerformAction(action
, numArg
, strArg
);
304 wxInputHandler
*wxButton::GetStdInputHandler(wxInputHandler
*handlerDef
)
306 static wxStdButtonInputHandler
s_handlerBtn(handlerDef
);
308 return &s_handlerBtn
;
311 // ----------------------------------------------------------------------------
313 // ----------------------------------------------------------------------------
315 void wxButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
317 // we support only one bitmap right now, although this wouldn't be
318 // difficult to change
319 if ( which
== State_Normal
)
322 SetBitmapMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
325 void wxButton::DoSetBitmapMargins(wxCoord x
, wxCoord y
)
327 m_marginBmpX
= x
+ 2;
328 m_marginBmpY
= y
+ 2;
330 SetInitialSize(wxDefaultSize
);
333 wxWindow
*wxButton::SetDefault()
337 return wxButtonBase::SetDefault();
340 // ============================================================================
341 // wxStdButtonInputHandler
342 // ============================================================================
344 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler
*handler
)
345 : wxStdInputHandler(handler
)
348 m_winHasMouse
= false;
351 bool wxStdButtonInputHandler::HandleKey(wxInputConsumer
*consumer
,
352 const wxKeyEvent
& event
,
355 int keycode
= event
.GetKeyCode();
356 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
358 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
363 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
366 bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer
*consumer
,
367 const wxMouseEvent
& event
)
369 // the button has 2 states: pressed and normal with the following
370 // transitions between them:
372 // normal -> left down -> capture mouse and go to pressed state
373 // pressed -> left up inside -> generate click -> go to normal
374 // outside ------------------>
376 // the other mouse buttons are ignored
377 if ( event
.Button(1) )
379 if ( event
.LeftDown() || event
.LeftDClick() )
381 m_winCapture
= consumer
->GetInputWindow();
382 m_winCapture
->CaptureMouse();
383 m_winHasMouse
= true;
385 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
389 else if ( event
.LeftUp() )
393 m_winCapture
->ReleaseMouse();
399 // this will generate a click event
400 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
404 //else: the mouse was released outside the window, this doesn't
407 //else: don't do anything special about the double click
410 return wxStdInputHandler::HandleMouse(consumer
, event
);
413 bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
414 const wxMouseEvent
& event
)
416 // we only have to do something when the mouse leaves/enters the pressed
417 // button and don't care about the other ones
418 if ( event
.GetEventObject() == m_winCapture
)
420 // leaving the button should remove its pressed state
421 if ( event
.Leaving() )
423 // remember that the mouse is now outside
424 m_winHasMouse
= false;
426 // we do have a pressed button, so release it
427 consumer
->GetInputWindow()->SetCurrent(false);
428 consumer
->PerformAction(wxACTION_BUTTON_RELEASE
);
432 // and entering it back should make it pressed again if it had been
434 else if ( event
.Entering() )
436 // the mouse is (back) inside the button
437 m_winHasMouse
= true;
439 // we did have a pressed button which we released when leaving the
440 // window, press it again
441 consumer
->GetInputWindow()->SetCurrent(true);
442 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
448 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
451 bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer
* WXUNUSED(consumer
),
452 const wxFocusEvent
& WXUNUSED(event
))
454 // buttons change appearance when they get/lose focus, so return true to
459 bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer
*consumer
,
460 bool WXUNUSED(activated
))
462 // the default button changes appearance when the app is [de]activated, so
463 // return true to refresh
464 return wxStaticCast(consumer
->GetInputWindow(), wxButton
)->IsDefault();
467 #endif // wxUSE_BUTTON