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 // ----------------------------------------------------------------------------
43 // wxStdButtonInputHandler: translates SPACE and ENTER keys and the left mouse
44 // click into button press/release actions
45 // ----------------------------------------------------------------------------
47 class WXDLLEXPORT wxStdButtonInputHandler
: public wxStdInputHandler
50 wxStdButtonInputHandler(wxInputHandler
*inphand
);
52 virtual bool HandleKey(wxInputConsumer
*consumer
,
53 const wxKeyEvent
& event
,
55 virtual bool HandleMouse(wxInputConsumer
*consumer
,
56 const wxMouseEvent
& event
);
57 virtual bool HandleMouseMove(wxInputConsumer
*consumer
,
58 const wxMouseEvent
& event
);
59 virtual bool HandleFocus(wxInputConsumer
*consumer
,
60 const wxFocusEvent
& event
);
61 virtual bool HandleActivation(wxInputConsumer
*consumer
, bool activated
);
64 // the window (button) which has capture or NULL and the flag telling if
65 // the mouse is inside the button which captured it or not
66 wxWindow
*m_winCapture
;
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 // default margins around the image
75 static const wxCoord DEFAULT_BTN_MARGIN_X
= 0; // We should give space for the border, at least.
76 static const wxCoord DEFAULT_BTN_MARGIN_Y
= 0;
78 // ============================================================================
80 // ============================================================================
82 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
94 bool wxButton::Create(wxWindow
*parent
,
96 const wxBitmap
& bitmap
,
101 const wxValidator
& validator
,
102 const wxString
&name
)
105 if (label
.empty() && wxIsStockID(id
))
106 label
= wxGetStockLabel(id
);
108 long ctrl_style
= style
& ~wxBU_ALIGN_MASK
;
109 ctrl_style
= ctrl_style
& ~wxALIGN_MASK
;
111 if((style
& wxBU_RIGHT
) == wxBU_RIGHT
)
112 ctrl_style
|= wxALIGN_RIGHT
;
113 else if((style
& wxBU_LEFT
) == wxBU_LEFT
)
114 ctrl_style
|= wxALIGN_LEFT
;
116 ctrl_style
|= wxALIGN_CENTRE_HORIZONTAL
;
118 if((style
& wxBU_TOP
) == wxBU_TOP
)
119 ctrl_style
|= wxALIGN_TOP
;
120 else if((style
& wxBU_BOTTOM
) == wxBU_BOTTOM
)
121 ctrl_style
|= wxALIGN_BOTTOM
;
123 ctrl_style
|= wxALIGN_CENTRE_VERTICAL
;
125 if ( !wxControl::Create(parent
, id
, pos
, size
, ctrl_style
, validator
, name
) )
131 SetBitmap(bitmap
); // SetInitialSize called by SetBitmap()
133 SetInitialSize(size
);
135 CreateInputHandler(wxINP_HANDLER_BUTTON
);
140 wxButton::~wxButton()
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
149 wxSize
wxButtonBase::GetDefaultSize()
151 static wxSize s_sizeBtn
;
153 if ( s_sizeBtn
.x
== 0 )
157 // this corresponds more or less to wxMSW standard in Win32 theme (see
158 // wxWin32Renderer::AdjustSize())
159 // s_sizeBtn.x = 8*dc.GetCharWidth();
160 // s_sizeBtn.y = (11*dc.GetCharHeight())/10 + 2;
161 // Otto Wyss, Patch 664399
162 s_sizeBtn
.x
= dc
.GetCharWidth()*10 + 2;
163 s_sizeBtn
.y
= dc
.GetCharHeight()*11/10 + 2;
169 wxSize
wxButton::DoGetBestClientSize() const
171 wxClientDC
dc(wxConstCast(this, wxButton
));
172 wxCoord width
, height
;
173 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
177 // allocate extra space for the bitmap
178 wxCoord heightBmp
= m_bitmap
.GetHeight() + 2*m_marginBmpY
;
179 if ( height
< heightBmp
)
182 width
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
;
185 // The default size should not be adjusted, so the code is moved into the
186 // renderer. This is conceptual wrong but currently the only solution.
187 // (Otto Wyss, Patch 664399)
190 // for compatibility with other ports, the buttons default size is never
191 // less than the standard one, but not when display not PDAs.
192 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
194 if ( !(GetWindowStyle() & wxBU_EXACTFIT) )
196 wxSize szDef = GetDefaultSize();
197 if ( width < szDef.x )
202 return wxSize(width
, height
);
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 void wxButton::DoDraw(wxControlRenderer
*renderer
)
211 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
213 renderer
->DrawButtonBorder();
216 renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
219 bool wxButton::DoDrawBackground(wxDC
& dc
)
222 wxSize size
= GetSize();
224 rect
.height
= size
.y
;
226 if ( GetBackgroundBitmap().Ok() )
228 // get the bitmap and the flags
231 wxBitmap bmp
= GetBackgroundBitmap(&alignment
, &stretch
);
232 wxControlRenderer::DrawBitmap(dc
, bmp
, rect
, alignment
, stretch
);
236 m_renderer
->DrawButtonSurface(dc
, wxTHEME_BG_COLOUR(this),
237 rect
, GetStateFlags());
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 void wxButton::Press()
257 void wxButton::Release()
267 void wxButton::Toggle()
276 // releasing button after it had been pressed generates a click event
281 void wxButton::Click()
283 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
284 InitCommandEvent(event
);
288 bool wxButton::PerformAction(const wxControlAction
& action
,
290 const wxString
& strArg
)
292 if ( action
== wxACTION_BUTTON_TOGGLE
)
294 else if ( action
== wxACTION_BUTTON_CLICK
)
296 else if ( action
== wxACTION_BUTTON_PRESS
)
298 else if ( action
== wxACTION_BUTTON_RELEASE
)
301 return wxControl::PerformAction(action
, numArg
, strArg
);
307 wxInputHandler
*wxButton::GetStdInputHandler(wxInputHandler
*handlerDef
)
309 static wxStdButtonInputHandler
s_handlerBtn(handlerDef
);
311 return &s_handlerBtn
;
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
318 void wxButton::DoSetBitmap(const wxBitmap
& bitmap
, State which
)
320 // we support only one bitmap right now, although this wouldn't be
321 // difficult to change
322 if ( which
== State_Normal
)
325 SetBitmapMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
328 void wxButton::DoSetBitmapMargins(wxCoord x
, wxCoord y
)
330 m_marginBmpX
= x
+ 2;
331 m_marginBmpY
= y
+ 2;
333 SetInitialSize(wxDefaultSize
);
336 wxWindow
*wxButton::SetDefault()
340 return wxButtonBase::SetDefault();
343 // ============================================================================
344 // wxStdButtonInputHandler
345 // ============================================================================
347 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler
*handler
)
348 : wxStdInputHandler(handler
)
351 m_winHasMouse
= false;
354 bool wxStdButtonInputHandler::HandleKey(wxInputConsumer
*consumer
,
355 const wxKeyEvent
& event
,
358 int keycode
= event
.GetKeyCode();
359 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
361 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
366 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
369 bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer
*consumer
,
370 const wxMouseEvent
& event
)
372 // the button has 2 states: pressed and normal with the following
373 // transitions between them:
375 // normal -> left down -> capture mouse and go to pressed state
376 // pressed -> left up inside -> generate click -> go to normal
377 // outside ------------------>
379 // the other mouse buttons are ignored
380 if ( event
.Button(1) )
382 if ( event
.LeftDown() || event
.LeftDClick() )
384 m_winCapture
= consumer
->GetInputWindow();
385 m_winCapture
->CaptureMouse();
386 m_winHasMouse
= true;
388 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
392 else if ( event
.LeftUp() )
396 m_winCapture
->ReleaseMouse();
402 // this will generate a click event
403 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
407 //else: the mouse was released outside the window, this doesn't
410 //else: don't do anything special about the double click
413 return wxStdInputHandler::HandleMouse(consumer
, event
);
416 bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
417 const wxMouseEvent
& event
)
419 // we only have to do something when the mouse leaves/enters the pressed
420 // button and don't care about the other ones
421 if ( event
.GetEventObject() == m_winCapture
)
423 // leaving the button should remove its pressed state
424 if ( event
.Leaving() )
426 // remember that the mouse is now outside
427 m_winHasMouse
= false;
429 // we do have a pressed button, so release it
430 consumer
->GetInputWindow()->SetCurrent(false);
431 consumer
->PerformAction(wxACTION_BUTTON_RELEASE
);
435 // and entering it back should make it pressed again if it had been
437 else if ( event
.Entering() )
439 // the mouse is (back) inside the button
440 m_winHasMouse
= true;
442 // we did have a pressed button which we released when leaving the
443 // window, press it again
444 consumer
->GetInputWindow()->SetCurrent(true);
445 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
451 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
454 bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer
* WXUNUSED(consumer
),
455 const wxFocusEvent
& WXUNUSED(event
))
457 // buttons change appearance when they get/lose focus, so return true to
462 bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer
*consumer
,
463 bool WXUNUSED(activated
))
465 // the default button changes appearance when the app is [de]activated, so
466 // return true to refresh
467 return wxStaticCast(consumer
->GetInputWindow(), wxButton
)->IsDefault();
470 #endif // wxUSE_BUTTON