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; // We should give space for the border, at least.
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
140 if ( !(GetWindowStyle() & wxBU_EXACTFIT
) )
142 wxSize szDef
= GetDefaultSize();
143 if ( width
< szDef
.x
)
148 return wxSize(width
, height
);
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
155 void wxButton::DoDraw(wxControlRenderer
*renderer
)
157 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
159 renderer
->DrawButtonBorder();
162 renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 void wxButton::Press()
179 void wxButton::Release()
189 void wxButton::Toggle()
198 // releasing button after it had been pressed generates a click event
203 void wxButton::Click()
205 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
206 InitCommandEvent(event
);
210 bool wxButton::PerformAction(const wxControlAction
& action
,
212 const wxString
& strArg
)
214 if ( action
== wxACTION_BUTTON_TOGGLE
)
216 else if ( action
== wxACTION_BUTTON_CLICK
)
218 else if ( action
== wxACTION_BUTTON_PRESS
)
220 else if ( action
== wxACTION_BUTTON_RELEASE
)
223 return wxControl::PerformAction(action
, numArg
, strArg
);
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 void wxButton::SetImageLabel(const wxBitmap
& bitmap
)
236 SetImageMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
239 void wxButton::SetImageMargins(wxCoord x
, wxCoord y
)
241 m_marginBmpX
= x
+ 2;
242 m_marginBmpY
= y
+ 2;
244 SetBestSize(wxDefaultSize
);
247 void wxButton::SetDefault()
252 // ============================================================================
253 // wxStdButtonInputHandler
254 // ============================================================================
256 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler
*handler
)
257 : wxStdInputHandler(handler
)
260 m_winHasMouse
= FALSE
;
263 bool wxStdButtonInputHandler::HandleKey(wxInputConsumer
*consumer
,
264 const wxKeyEvent
& event
,
267 int keycode
= event
.GetKeyCode();
268 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
270 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
275 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
278 bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer
*consumer
,
279 const wxMouseEvent
& event
)
281 // the button has 2 states: pressed and normal with the following
282 // transitions between them:
284 // normal -> left down -> capture mouse and go to pressed state
285 // pressed -> left up inside -> generate click -> go to normal
286 // outside ------------------>
288 // the other mouse buttons are ignored
289 if ( event
.Button(1) )
291 if ( event
.LeftDown() || event
.LeftDClick() )
293 m_winCapture
= consumer
->GetInputWindow();
294 m_winCapture
->CaptureMouse();
295 m_winHasMouse
= TRUE
;
297 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
301 else if ( event
.LeftUp() )
305 m_winCapture
->ReleaseMouse();
311 // this will generate a click event
312 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
316 //else: the mouse was released outside the window, this doesn't
319 //else: don't do anything special about the double click
322 return wxStdInputHandler::HandleMouse(consumer
, event
);
325 bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
326 const wxMouseEvent
& event
)
328 // we only have to do something when the mouse leaves/enters the pressed
329 // button and don't care about the other ones
330 if ( event
.GetEventObject() == m_winCapture
)
332 // leaving the button should remove its pressed state
333 if ( event
.Leaving() )
335 // remember that the mouse is now outside
336 m_winHasMouse
= FALSE
;
338 // we do have a pressed button, so release it
339 consumer
->GetInputWindow()->SetCurrent(FALSE
);
340 consumer
->PerformAction(wxACTION_BUTTON_RELEASE
);
344 // and entering it back should make it pressed again if it had been
346 else if ( event
.Entering() )
348 // the mouse is (back) inside the button
349 m_winHasMouse
= TRUE
;
351 // we did have a pressed button which we released when leaving the
352 // window, press it again
353 consumer
->GetInputWindow()->SetCurrent(TRUE
);
354 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
360 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
363 bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer
*consumer
,
364 const wxFocusEvent
& event
)
366 // buttons change appearance when they get/lose focus, so return TRUE to
371 bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer
*consumer
,
374 // the default button changes appearance when the app is [de]activated, so
375 // return TRUE to refresh
376 return wxStaticCast(consumer
->GetInputWindow(), wxButton
)->IsDefault();
379 #endif // wxUSE_BUTTON