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
;
82 wxASSERT_MSG( (ctrl_style
& wxALIGN_MASK
) == 0,
83 _T("Some style conflicts with align flags") );
85 if((style
& wxBU_RIGHT
) == wxBU_RIGHT
)
86 ctrl_style
|= wxALIGN_RIGHT
;
87 else if((style
& wxBU_LEFT
) == wxBU_LEFT
)
88 ctrl_style
|= wxALIGN_LEFT
;
90 ctrl_style
|= wxALIGN_CENTRE_HORIZONTAL
;
92 if((style
& wxBU_TOP
) == wxBU_TOP
)
93 ctrl_style
|= wxALIGN_TOP
;
94 else if((style
& wxBU_BOTTOM
) == wxBU_BOTTOM
)
95 ctrl_style
|= wxALIGN_BOTTOM
;
97 ctrl_style
|= wxALIGN_CENTRE_VERTICAL
;
99 if ( !wxControl::Create(parent
, id
, pos
, size
, ctrl_style
, validator
, name
) )
103 SetImageLabel(bitmap
);
104 // SetBestSize(size); -- called by SetImageLabel()
106 CreateInputHandler(wxINP_HANDLER_BUTTON
);
111 wxButton::~wxButton()
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
120 wxSize
wxButtonBase::GetDefaultSize()
122 static wxSize s_sizeBtn
;
124 if ( s_sizeBtn
.x
== 0 )
128 // this corresponds more or less to wxMSW standard in Win32 theme (see
129 // wxWin32Renderer::AdjustSize())
130 // s_sizeBtn.x = 8*dc.GetCharWidth();
131 // s_sizeBtn.y = (11*dc.GetCharHeight())/10 + 2;
132 // Otto Wyss, Patch 664399
133 s_sizeBtn
.x
= dc
.GetCharWidth()*10 + 2;
134 s_sizeBtn
.y
= dc
.GetCharHeight()*11/10 + 2;
140 wxSize
wxButton::DoGetBestClientSize() const
142 wxClientDC
dc(wxConstCast(this, wxButton
));
143 wxCoord width
, height
;
144 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
148 // allocate extra space for the bitmap
149 wxCoord heightBmp
= m_bitmap
.GetHeight() + 2*m_marginBmpY
;
150 if ( height
< heightBmp
)
153 width
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
;
156 // The default size should not be adjusted, so the code is moved into the
157 // renderer. This is conceptual wrong but currently the only solution.
158 // (Otto Wyss, Patch 664399)
161 // for compatibility with other ports, the buttons default size is never
162 // less than the standard one, but not when display not PDAs.
163 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
165 if ( !(GetWindowStyle() & wxBU_EXACTFIT) )
167 wxSize szDef = GetDefaultSize();
168 if ( width < szDef.x )
173 return wxSize(width
, height
);
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 void wxButton::DoDraw(wxControlRenderer
*renderer
)
182 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
184 renderer
->DrawButtonBorder();
187 renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
190 bool wxButton::DoDrawBackground(wxDC
& dc
)
193 wxSize size
= GetSize();
195 rect
.height
= size
.y
;
197 if ( GetBackgroundBitmap().Ok() )
199 // get the bitmap and the flags
202 wxBitmap bmp
= GetBackgroundBitmap(&alignment
, &stretch
);
203 wxControlRenderer::DrawBitmap(dc
, bmp
, rect
, alignment
, stretch
);
207 m_renderer
->DrawButtonSurface(dc
, wxTHEME_BG_COLOUR(this),
208 rect
, GetStateFlags());
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 void wxButton::Press()
228 void wxButton::Release()
238 void wxButton::Toggle()
247 // releasing button after it had been pressed generates a click event
252 void wxButton::Click()
254 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
255 InitCommandEvent(event
);
259 bool wxButton::PerformAction(const wxControlAction
& action
,
261 const wxString
& strArg
)
263 if ( action
== wxACTION_BUTTON_TOGGLE
)
265 else if ( action
== wxACTION_BUTTON_CLICK
)
267 else if ( action
== wxACTION_BUTTON_PRESS
)
269 else if ( action
== wxACTION_BUTTON_RELEASE
)
272 return wxControl::PerformAction(action
, numArg
, strArg
);
277 // ----------------------------------------------------------------------------
279 // ----------------------------------------------------------------------------
281 void wxButton::SetImageLabel(const wxBitmap
& bitmap
)
285 SetImageMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
288 void wxButton::SetImageMargins(wxCoord x
, wxCoord y
)
290 m_marginBmpX
= x
+ 2;
291 m_marginBmpY
= y
+ 2;
293 SetBestSize(wxDefaultSize
);
296 void wxButton::SetDefault()
301 // ============================================================================
302 // wxStdButtonInputHandler
303 // ============================================================================
305 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler
*handler
)
306 : wxStdInputHandler(handler
)
309 m_winHasMouse
= false;
312 bool wxStdButtonInputHandler::HandleKey(wxInputConsumer
*consumer
,
313 const wxKeyEvent
& event
,
316 int keycode
= event
.GetKeyCode();
317 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
319 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
324 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
327 bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer
*consumer
,
328 const wxMouseEvent
& event
)
330 // the button has 2 states: pressed and normal with the following
331 // transitions between them:
333 // normal -> left down -> capture mouse and go to pressed state
334 // pressed -> left up inside -> generate click -> go to normal
335 // outside ------------------>
337 // the other mouse buttons are ignored
338 if ( event
.Button(1) )
340 if ( event
.LeftDown() || event
.LeftDClick() )
342 m_winCapture
= consumer
->GetInputWindow();
343 m_winCapture
->CaptureMouse();
344 m_winHasMouse
= true;
346 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
350 else if ( event
.LeftUp() )
354 m_winCapture
->ReleaseMouse();
360 // this will generate a click event
361 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
365 //else: the mouse was released outside the window, this doesn't
368 //else: don't do anything special about the double click
371 return wxStdInputHandler::HandleMouse(consumer
, event
);
374 bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
375 const wxMouseEvent
& event
)
377 // we only have to do something when the mouse leaves/enters the pressed
378 // button and don't care about the other ones
379 if ( event
.GetEventObject() == m_winCapture
)
381 // leaving the button should remove its pressed state
382 if ( event
.Leaving() )
384 // remember that the mouse is now outside
385 m_winHasMouse
= false;
387 // we do have a pressed button, so release it
388 consumer
->GetInputWindow()->SetCurrent(false);
389 consumer
->PerformAction(wxACTION_BUTTON_RELEASE
);
393 // and entering it back should make it pressed again if it had been
395 else if ( event
.Entering() )
397 // the mouse is (back) inside the button
398 m_winHasMouse
= true;
400 // we did have a pressed button which we released when leaving the
401 // window, press it again
402 consumer
->GetInputWindow()->SetCurrent(true);
403 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
409 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
412 bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer
* WXUNUSED(consumer
),
413 const wxFocusEvent
& WXUNUSED(event
))
415 // buttons change appearance when they get/lose focus, so return true to
420 bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer
*consumer
,
421 bool WXUNUSED(activated
))
423 // the default button changes appearance when the app is [de]activated, so
424 // return true to refresh
425 return wxStaticCast(consumer
->GetInputWindow(), wxButton
)->IsDefault();
428 #endif // wxUSE_BUTTON