1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/button.cpp
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "univbutton.h"
24 #include "wx/wxprec.h"
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"
40 #include "wx/univ/renderer.h"
41 #include "wx/univ/inphand.h"
42 #include "wx/univ/theme.h"
43 #include "wx/univ/colschem.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
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;
53 // ============================================================================
55 // ============================================================================
57 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
69 bool wxButton::Create(wxWindow
*parent
,
71 const wxBitmap
& bitmap
,
72 const wxString
&label
,
76 const wxValidator
& validator
,
79 // center label by default
80 if ( !(style
& wxALIGN_MASK
) )
82 style
|= wxALIGN_CENTRE_HORIZONTAL
| wxALIGN_CENTRE_VERTICAL
;
85 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
89 SetImageLabel(bitmap
);
90 // SetBestSize(size); -- called by SetImageLabel()
92 CreateInputHandler(wxINP_HANDLER_BUTTON
);
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
106 wxSize
wxButtonBase::GetDefaultSize()
108 static wxSize s_sizeBtn
;
110 if ( s_sizeBtn
.x
== 0 )
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;
123 wxSize
wxButton::DoGetBestClientSize() const
125 wxClientDC
dc(wxConstCast(this, wxButton
));
126 wxCoord width
, height
;
127 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
131 // allocate extra space for the bitmap
132 wxCoord heightBmp
= m_bitmap
.GetHeight() + 2*m_marginBmpY
;
133 if ( height
< heightBmp
)
136 width
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
;
139 // for compatibility with other ports, the buttons default size is never
140 // less than the standard one, but not when display not PDAs.
141 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA
)
143 if ( !(GetWindowStyle() & wxBU_EXACTFIT
) )
145 wxSize szDef
= GetDefaultSize();
146 if ( width
< szDef
.x
)
151 return wxSize(width
, height
);
154 // ----------------------------------------------------------------------------
156 // ----------------------------------------------------------------------------
158 void wxButton::DoDraw(wxControlRenderer
*renderer
)
160 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
162 renderer
->DrawButtonBorder();
165 renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
168 bool wxButton::DoDrawBackground(wxDC
& dc
)
171 wxSize size
= GetSize();
173 rect
.height
= size
.y
;
175 if ( GetBackgroundBitmap().Ok() )
177 // get the bitmap and the flags
180 wxBitmap bmp
= GetBackgroundBitmap(&alignment
, &stretch
);
181 wxControlRenderer::DrawBitmap(dc
, bmp
, rect
, alignment
, stretch
);
185 m_renderer
->DrawButtonSurface(dc
, wxTHEME_BG_COLOUR(this),
186 rect
, GetStateFlags());
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
196 void wxButton::Press()
206 void wxButton::Release()
216 void wxButton::Toggle()
225 // releasing button after it had been pressed generates a click event
230 void wxButton::Click()
232 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
233 InitCommandEvent(event
);
237 bool wxButton::PerformAction(const wxControlAction
& action
,
239 const wxString
& strArg
)
241 if ( action
== wxACTION_BUTTON_TOGGLE
)
243 else if ( action
== wxACTION_BUTTON_CLICK
)
245 else if ( action
== wxACTION_BUTTON_PRESS
)
247 else if ( action
== wxACTION_BUTTON_RELEASE
)
250 return wxControl::PerformAction(action
, numArg
, strArg
);
255 // ----------------------------------------------------------------------------
257 // ----------------------------------------------------------------------------
259 void wxButton::SetImageLabel(const wxBitmap
& bitmap
)
263 SetImageMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
266 void wxButton::SetImageMargins(wxCoord x
, wxCoord y
)
268 m_marginBmpX
= x
+ 2;
269 m_marginBmpY
= y
+ 2;
271 SetBestSize(wxDefaultSize
);
274 void wxButton::SetDefault()
279 // ============================================================================
280 // wxStdButtonInputHandler
281 // ============================================================================
283 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler
*handler
)
284 : wxStdInputHandler(handler
)
287 m_winHasMouse
= FALSE
;
290 bool wxStdButtonInputHandler::HandleKey(wxInputConsumer
*consumer
,
291 const wxKeyEvent
& event
,
294 int keycode
= event
.GetKeyCode();
295 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
297 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
302 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
305 bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer
*consumer
,
306 const wxMouseEvent
& event
)
308 // the button has 2 states: pressed and normal with the following
309 // transitions between them:
311 // normal -> left down -> capture mouse and go to pressed state
312 // pressed -> left up inside -> generate click -> go to normal
313 // outside ------------------>
315 // the other mouse buttons are ignored
316 if ( event
.Button(1) )
318 if ( event
.LeftDown() || event
.LeftDClick() )
320 m_winCapture
= consumer
->GetInputWindow();
321 m_winCapture
->CaptureMouse();
322 m_winHasMouse
= TRUE
;
324 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
328 else if ( event
.LeftUp() )
332 m_winCapture
->ReleaseMouse();
338 // this will generate a click event
339 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
343 //else: the mouse was released outside the window, this doesn't
346 //else: don't do anything special about the double click
349 return wxStdInputHandler::HandleMouse(consumer
, event
);
352 bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
353 const wxMouseEvent
& event
)
355 // we only have to do something when the mouse leaves/enters the pressed
356 // button and don't care about the other ones
357 if ( event
.GetEventObject() == m_winCapture
)
359 // leaving the button should remove its pressed state
360 if ( event
.Leaving() )
362 // remember that the mouse is now outside
363 m_winHasMouse
= FALSE
;
365 // we do have a pressed button, so release it
366 consumer
->GetInputWindow()->SetCurrent(FALSE
);
367 consumer
->PerformAction(wxACTION_BUTTON_RELEASE
);
371 // and entering it back should make it pressed again if it had been
373 else if ( event
.Entering() )
375 // the mouse is (back) inside the button
376 m_winHasMouse
= TRUE
;
378 // we did have a pressed button which we released when leaving the
379 // window, press it again
380 consumer
->GetInputWindow()->SetCurrent(TRUE
);
381 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
387 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
390 bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer
*consumer
,
391 const wxFocusEvent
& event
)
393 // buttons change appearance when they get/lose focus, so return TRUE to
398 bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer
*consumer
,
401 // the default button changes appearance when the app is [de]activated, so
402 // return TRUE to refresh
403 return wxStaticCast(consumer
->GetInputWindow(), wxButton
)->IsDefault();
406 #endif // wxUSE_BUTTON