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"
39 #include "wx/univ/renderer.h"
40 #include "wx/univ/inphand.h"
41 #include "wx/univ/theme.h"
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 // default margins around the image
48 static const wxCoord DEFAULT_BTN_MARGIN_X
= 0;
49 static const wxCoord DEFAULT_BTN_MARGIN_Y
= 0;
51 // ============================================================================
53 // ============================================================================
55 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
67 bool wxButton::Create(wxWindow
*parent
,
69 const wxBitmap
& bitmap
,
70 const wxString
&label
,
74 const wxValidator
& validator
,
77 // center label by default
78 if ( !(style
& wxALIGN_MASK
) )
80 style
|= wxALIGN_CENTRE_HORIZONTAL
| wxALIGN_CENTRE_VERTICAL
;
83 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
87 SetImageLabel(bitmap
);
88 // SetBestSize(size); -- called by SetImageLabel()
90 CreateInputHandler(wxINP_HANDLER_BUTTON
);
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
104 wxSize
wxButtonBase::GetDefaultSize()
106 static wxSize s_sizeBtn
;
108 if ( s_sizeBtn
.x
== 0 )
112 // this corresponds more or less to wxMSW standard in Win32 theme (see
113 // wxWin32Renderer::AdjustSize())
114 s_sizeBtn
.x
= 8*dc
.GetCharWidth();
115 s_sizeBtn
.y
= (11*dc
.GetCharHeight())/10 + 2;
121 wxSize
wxButton::DoGetBestClientSize() const
123 wxClientDC
dc(wxConstCast(this, wxButton
));
124 wxCoord width
, height
;
125 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
129 // allocate extra space for the bitmap
130 wxCoord heightBmp
= m_bitmap
.GetHeight() + 2*m_marginBmpY
;
131 if ( height
< heightBmp
)
134 width
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
;
137 // for compatibility with other ports, the buttons default size is never
138 // less than the standard one
139 if ( !(GetWindowStyle() & wxBU_EXACTFIT
) )
141 wxSize szDef
= GetDefaultSize();
142 if ( width
< szDef
.x
)
146 return wxSize(width
, height
);
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 void wxButton::DoDraw(wxControlRenderer
*renderer
)
155 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
157 renderer
->DrawButtonBorder();
160 renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 void wxButton::Press()
177 void wxButton::Release()
187 void wxButton::Toggle()
196 // releasing button after it had been pressed generates a click event
201 void wxButton::Click()
203 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
204 InitCommandEvent(event
);
208 bool wxButton::PerformAction(const wxControlAction
& action
,
210 const wxString
& strArg
)
212 if ( action
== wxACTION_BUTTON_TOGGLE
)
214 else if ( action
== wxACTION_BUTTON_CLICK
)
216 else if ( action
== wxACTION_BUTTON_PRESS
)
218 else if ( action
== wxACTION_BUTTON_RELEASE
)
221 return wxControl::PerformAction(action
, numArg
, strArg
);
226 // ----------------------------------------------------------------------------
228 // ----------------------------------------------------------------------------
230 void wxButton::SetImageLabel(const wxBitmap
& bitmap
)
234 SetImageMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
237 void wxButton::SetImageMargins(wxCoord x
, wxCoord y
)
242 SetBestSize(wxDefaultSize
);
245 void wxButton::SetDefault()
250 // ============================================================================
251 // wxStdButtonInputHandler
252 // ============================================================================
254 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler
*handler
)
255 : wxStdInputHandler(handler
)
258 m_winHasMouse
= FALSE
;
261 bool wxStdButtonInputHandler::HandleKey(wxControl
*control
,
262 const wxKeyEvent
& event
,
265 int keycode
= event
.GetKeyCode();
266 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
268 control
->PerformAction(wxACTION_BUTTON_TOGGLE
);
273 return wxStdInputHandler::HandleKey(control
, event
, pressed
);
276 bool wxStdButtonInputHandler::HandleMouse(wxControl
*control
,
277 const wxMouseEvent
& event
)
279 // the button has 2 states: pressed and normal with the following
280 // transitions between them:
282 // normal -> left down -> capture mouse and go to pressed state
283 // pressed -> left up inside -> generate click -> go to normal
284 // outside ------------------>
286 // the other mouse buttons are ignored
287 if ( event
.Button(1) )
289 if ( event
.ButtonDown(1) )
291 m_winCapture
= control
;
292 m_winCapture
->CaptureMouse();
293 m_winHasMouse
= TRUE
;
295 control
->PerformAction(wxACTION_BUTTON_PRESS
);
303 m_winCapture
->ReleaseMouse();
309 // this will generate a click event
310 control
->PerformAction(wxACTION_BUTTON_TOGGLE
);
314 //else: the mouse was released outside the window, this doesn't
319 return wxStdInputHandler::HandleMouse(control
, event
);
322 bool wxStdButtonInputHandler::HandleMouseMove(wxControl
*control
,
323 const wxMouseEvent
& event
)
325 // we only have to do something when the mouse leaves/enters the pressed
326 // button and don't care about the other ones
327 if ( event
.GetEventObject() == m_winCapture
)
329 // leaving the button should remove its pressed state
330 if ( event
.Leaving() )
332 // remember that the mouse is now outside
333 m_winHasMouse
= FALSE
;
335 // we do have a pressed button, so release it
336 control
->SetCurrent(FALSE
);
337 control
->PerformAction(wxACTION_BUTTON_RELEASE
);
341 // and entering it back should make it pressed again if it had been
343 else if ( event
.Entering() )
345 // the mouse is (back) inside the button
346 m_winHasMouse
= TRUE
;
348 // we did have a pressed button which we released when leaving the
349 // window, press it again
350 control
->SetCurrent(TRUE
);
351 control
->PerformAction(wxACTION_BUTTON_PRESS
);
357 return wxStdInputHandler::HandleMouseMove(control
, event
);
360 bool wxStdButtonInputHandler::HandleFocus(wxControl
*control
,
361 const wxFocusEvent
& event
)
363 // buttons change appearance when they get/lose focus, so return TRUE to
368 bool wxStdButtonInputHandler::HandleActivation(wxControl
*control
,
371 // the default button changes appearance when the app is [de]activated, so
372 // return TRUE to refresh
373 return wxStaticCast(control
, wxButton
)->IsDefault();
376 #endif // wxUSE_BUTTON