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 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
29 #include "wx/dcclient.h"
30 #include "wx/dcscreen.h"
31 #include "wx/button.h"
32 #include "wx/validate.h"
33 #include "wx/settings.h"
36 #include "wx/univ/renderer.h"
37 #include "wx/univ/inphand.h"
38 #include "wx/univ/theme.h"
39 #include "wx/univ/colschem.h"
40 #include "wx/stockitem.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // default margins around the image
47 static const wxCoord DEFAULT_BTN_MARGIN_X
= 0; // We should give space for the border, at least.
48 static const wxCoord DEFAULT_BTN_MARGIN_Y
= 0;
50 // ============================================================================
52 // ============================================================================
54 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
66 bool wxButton::Create(wxWindow
*parent
,
68 const wxBitmap
& bitmap
,
73 const wxValidator
& validator
,
77 if (label
.empty() && wxIsStockID(id
))
78 label
= wxGetStockLabel(id
);
80 long ctrl_style
= style
& ~wxBU_ALIGN_MASK
;
81 ctrl_style
= ctrl_style
& ~wxALIGN_MASK
;
83 if((style
& wxBU_RIGHT
) == wxBU_RIGHT
)
84 ctrl_style
|= wxALIGN_RIGHT
;
85 else if((style
& wxBU_LEFT
) == wxBU_LEFT
)
86 ctrl_style
|= wxALIGN_LEFT
;
88 ctrl_style
|= wxALIGN_CENTRE_HORIZONTAL
;
90 if((style
& wxBU_TOP
) == wxBU_TOP
)
91 ctrl_style
|= wxALIGN_TOP
;
92 else if((style
& wxBU_BOTTOM
) == wxBU_BOTTOM
)
93 ctrl_style
|= wxALIGN_BOTTOM
;
95 ctrl_style
|= wxALIGN_CENTRE_VERTICAL
;
97 if ( !wxControl::Create(parent
, id
, pos
, size
, ctrl_style
, validator
, name
) )
101 SetImageLabel(bitmap
);
102 // SetBestSize(size); -- called by SetImageLabel()
104 CreateInputHandler(wxINP_HANDLER_BUTTON
);
109 wxButton::~wxButton()
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
118 wxSize
wxButtonBase::GetDefaultSize()
120 static wxSize s_sizeBtn
;
122 if ( s_sizeBtn
.x
== 0 )
126 // this corresponds more or less to wxMSW standard in Win32 theme (see
127 // wxWin32Renderer::AdjustSize())
128 // s_sizeBtn.x = 8*dc.GetCharWidth();
129 // s_sizeBtn.y = (11*dc.GetCharHeight())/10 + 2;
130 // Otto Wyss, Patch 664399
131 s_sizeBtn
.x
= dc
.GetCharWidth()*10 + 2;
132 s_sizeBtn
.y
= dc
.GetCharHeight()*11/10 + 2;
138 wxSize
wxButton::DoGetBestClientSize() const
140 wxClientDC
dc(wxConstCast(this, wxButton
));
141 wxCoord width
, height
;
142 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
146 // allocate extra space for the bitmap
147 wxCoord heightBmp
= m_bitmap
.GetHeight() + 2*m_marginBmpY
;
148 if ( height
< heightBmp
)
151 width
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
;
154 // The default size should not be adjusted, so the code is moved into the
155 // renderer. This is conceptual wrong but currently the only solution.
156 // (Otto Wyss, Patch 664399)
159 // for compatibility with other ports, the buttons default size is never
160 // less than the standard one, but not when display not PDAs.
161 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
163 if ( !(GetWindowStyle() & wxBU_EXACTFIT) )
165 wxSize szDef = GetDefaultSize();
166 if ( width < szDef.x )
171 return wxSize(width
, height
);
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 void wxButton::DoDraw(wxControlRenderer
*renderer
)
180 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
182 renderer
->DrawButtonBorder();
185 renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
188 bool wxButton::DoDrawBackground(wxDC
& dc
)
191 wxSize size
= GetSize();
193 rect
.height
= size
.y
;
195 if ( GetBackgroundBitmap().Ok() )
197 // get the bitmap and the flags
200 wxBitmap bmp
= GetBackgroundBitmap(&alignment
, &stretch
);
201 wxControlRenderer::DrawBitmap(dc
, bmp
, rect
, alignment
, stretch
);
205 m_renderer
->DrawButtonSurface(dc
, wxTHEME_BG_COLOUR(this),
206 rect
, GetStateFlags());
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 void wxButton::Press()
226 void wxButton::Release()
236 void wxButton::Toggle()
245 // releasing button after it had been pressed generates a click event
250 void wxButton::Click()
252 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
253 InitCommandEvent(event
);
257 bool wxButton::PerformAction(const wxControlAction
& action
,
259 const wxString
& strArg
)
261 if ( action
== wxACTION_BUTTON_TOGGLE
)
263 else if ( action
== wxACTION_BUTTON_CLICK
)
265 else if ( action
== wxACTION_BUTTON_PRESS
)
267 else if ( action
== wxACTION_BUTTON_RELEASE
)
270 return wxControl::PerformAction(action
, numArg
, strArg
);
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 void wxButton::SetImageLabel(const wxBitmap
& bitmap
)
283 SetImageMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
286 void wxButton::SetImageMargins(wxCoord x
, wxCoord y
)
288 m_marginBmpX
= x
+ 2;
289 m_marginBmpY
= y
+ 2;
291 SetBestSize(wxDefaultSize
);
294 void wxButton::SetDefault()
299 // ============================================================================
300 // wxStdButtonInputHandler
301 // ============================================================================
303 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler
*handler
)
304 : wxStdInputHandler(handler
)
307 m_winHasMouse
= false;
310 bool wxStdButtonInputHandler::HandleKey(wxInputConsumer
*consumer
,
311 const wxKeyEvent
& event
,
314 int keycode
= event
.GetKeyCode();
315 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
317 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
322 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
325 bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer
*consumer
,
326 const wxMouseEvent
& event
)
328 // the button has 2 states: pressed and normal with the following
329 // transitions between them:
331 // normal -> left down -> capture mouse and go to pressed state
332 // pressed -> left up inside -> generate click -> go to normal
333 // outside ------------------>
335 // the other mouse buttons are ignored
336 if ( event
.Button(1) )
338 if ( event
.LeftDown() || event
.LeftDClick() )
340 m_winCapture
= consumer
->GetInputWindow();
341 m_winCapture
->CaptureMouse();
342 m_winHasMouse
= true;
344 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
348 else if ( event
.LeftUp() )
352 m_winCapture
->ReleaseMouse();
358 // this will generate a click event
359 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
363 //else: the mouse was released outside the window, this doesn't
366 //else: don't do anything special about the double click
369 return wxStdInputHandler::HandleMouse(consumer
, event
);
372 bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
373 const wxMouseEvent
& event
)
375 // we only have to do something when the mouse leaves/enters the pressed
376 // button and don't care about the other ones
377 if ( event
.GetEventObject() == m_winCapture
)
379 // leaving the button should remove its pressed state
380 if ( event
.Leaving() )
382 // remember that the mouse is now outside
383 m_winHasMouse
= false;
385 // we do have a pressed button, so release it
386 consumer
->GetInputWindow()->SetCurrent(false);
387 consumer
->PerformAction(wxACTION_BUTTON_RELEASE
);
391 // and entering it back should make it pressed again if it had been
393 else if ( event
.Entering() )
395 // the mouse is (back) inside the button
396 m_winHasMouse
= true;
398 // we did have a pressed button which we released when leaving the
399 // window, press it again
400 consumer
->GetInputWindow()->SetCurrent(true);
401 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
407 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
410 bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer
* WXUNUSED(consumer
),
411 const wxFocusEvent
& WXUNUSED(event
))
413 // buttons change appearance when they get/lose focus, so return true to
418 bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer
*consumer
,
419 bool WXUNUSED(activated
))
421 // the default button changes appearance when the app is [de]activated, so
422 // return true to refresh
423 return wxStaticCast(consumer
->GetInputWindow(), wxButton
)->IsDefault();
426 #endif // wxUSE_BUTTON