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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
37 #include "wx/settings.h"
40 #include "wx/univ/renderer.h"
41 #include "wx/univ/inphand.h"
42 #include "wx/univ/theme.h"
43 #include "wx/univ/colschem.h"
44 #include "wx/stockitem.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // default margins around the image
51 static const wxCoord DEFAULT_BTN_MARGIN_X
= 0; // We should give space for the border, at least.
52 static const wxCoord DEFAULT_BTN_MARGIN_Y
= 0;
54 // ============================================================================
56 // ============================================================================
58 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
70 bool wxButton::Create(wxWindow
*parent
,
72 const wxBitmap
& bitmap
,
77 const wxValidator
& validator
,
81 if (label
.empty() && wxIsStockID(id
))
82 label
= wxGetStockLabel(id
);
84 long ctrl_style
= style
& !wxBU_ALIGN_MASK
;
86 wxASSERT_MSG( (ctrl_style
& wxALIGN_MASK
) == 0,
87 _T("Some style conflicts with align flags") );
89 if((style
& wxBU_RIGHT
) == wxBU_RIGHT
)
90 ctrl_style
|= wxALIGN_RIGHT
;
91 else if((style
& wxBU_LEFT
) == wxBU_LEFT
)
92 ctrl_style
|= wxALIGN_LEFT
;
94 ctrl_style
|= wxALIGN_CENTRE_HORIZONTAL
;
96 if((style
& wxBU_TOP
) == wxBU_TOP
)
97 ctrl_style
|= wxALIGN_TOP
;
98 else if((style
& wxBU_BOTTOM
) == wxBU_BOTTOM
)
99 ctrl_style
|= wxALIGN_BOTTOM
;
101 ctrl_style
|= wxALIGN_CENTRE_VERTICAL
;
103 if ( !wxControl::Create(parent
, id
, pos
, size
, ctrl_style
, validator
, name
) )
107 SetImageLabel(bitmap
);
108 // SetBestSize(size); -- called by SetImageLabel()
110 CreateInputHandler(wxINP_HANDLER_BUTTON
);
115 wxButton::~wxButton()
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
124 wxSize
wxButtonBase::GetDefaultSize()
126 static wxSize s_sizeBtn
;
128 if ( s_sizeBtn
.x
== 0 )
132 // this corresponds more or less to wxMSW standard in Win32 theme (see
133 // wxWin32Renderer::AdjustSize())
134 // s_sizeBtn.x = 8*dc.GetCharWidth();
135 // s_sizeBtn.y = (11*dc.GetCharHeight())/10 + 2;
136 // Otto Wyss, Patch 664399
137 s_sizeBtn
.x
= dc
.GetCharWidth()*10 + 2;
138 s_sizeBtn
.y
= dc
.GetCharHeight()*11/10 + 2;
144 wxSize
wxButton::DoGetBestClientSize() const
146 wxClientDC
dc(wxConstCast(this, wxButton
));
147 wxCoord width
, height
;
148 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
152 // allocate extra space for the bitmap
153 wxCoord heightBmp
= m_bitmap
.GetHeight() + 2*m_marginBmpY
;
154 if ( height
< heightBmp
)
157 width
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
;
160 // The default size should not be adjusted, so the code is moved into the
161 // renderer. This is conceptual wrong but currently the only solution.
162 // (Otto Wyss, Patch 664399)
165 // for compatibility with other ports, the buttons default size is never
166 // less than the standard one, but not when display not PDAs.
167 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
169 if ( !(GetWindowStyle() & wxBU_EXACTFIT) )
171 wxSize szDef = GetDefaultSize();
172 if ( width < szDef.x )
177 return wxSize(width
, height
);
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 void wxButton::DoDraw(wxControlRenderer
*renderer
)
186 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
188 renderer
->DrawButtonBorder();
191 renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
194 bool wxButton::DoDrawBackground(wxDC
& dc
)
197 wxSize size
= GetSize();
199 rect
.height
= size
.y
;
201 if ( GetBackgroundBitmap().Ok() )
203 // get the bitmap and the flags
206 wxBitmap bmp
= GetBackgroundBitmap(&alignment
, &stretch
);
207 wxControlRenderer::DrawBitmap(dc
, bmp
, rect
, alignment
, stretch
);
211 m_renderer
->DrawButtonSurface(dc
, wxTHEME_BG_COLOUR(this),
212 rect
, GetStateFlags());
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 void wxButton::Press()
232 void wxButton::Release()
242 void wxButton::Toggle()
251 // releasing button after it had been pressed generates a click event
256 void wxButton::Click()
258 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
259 InitCommandEvent(event
);
263 bool wxButton::PerformAction(const wxControlAction
& action
,
265 const wxString
& strArg
)
267 if ( action
== wxACTION_BUTTON_TOGGLE
)
269 else if ( action
== wxACTION_BUTTON_CLICK
)
271 else if ( action
== wxACTION_BUTTON_PRESS
)
273 else if ( action
== wxACTION_BUTTON_RELEASE
)
276 return wxControl::PerformAction(action
, numArg
, strArg
);
281 // ----------------------------------------------------------------------------
283 // ----------------------------------------------------------------------------
285 void wxButton::SetImageLabel(const wxBitmap
& bitmap
)
289 SetImageMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
292 void wxButton::SetImageMargins(wxCoord x
, wxCoord y
)
294 m_marginBmpX
= x
+ 2;
295 m_marginBmpY
= y
+ 2;
297 SetBestSize(wxDefaultSize
);
300 void wxButton::SetDefault()
305 // ============================================================================
306 // wxStdButtonInputHandler
307 // ============================================================================
309 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler
*handler
)
310 : wxStdInputHandler(handler
)
313 m_winHasMouse
= false;
316 bool wxStdButtonInputHandler::HandleKey(wxInputConsumer
*consumer
,
317 const wxKeyEvent
& event
,
320 int keycode
= event
.GetKeyCode();
321 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
323 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
328 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
331 bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer
*consumer
,
332 const wxMouseEvent
& event
)
334 // the button has 2 states: pressed and normal with the following
335 // transitions between them:
337 // normal -> left down -> capture mouse and go to pressed state
338 // pressed -> left up inside -> generate click -> go to normal
339 // outside ------------------>
341 // the other mouse buttons are ignored
342 if ( event
.Button(1) )
344 if ( event
.LeftDown() || event
.LeftDClick() )
346 m_winCapture
= consumer
->GetInputWindow();
347 m_winCapture
->CaptureMouse();
348 m_winHasMouse
= true;
350 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
354 else if ( event
.LeftUp() )
358 m_winCapture
->ReleaseMouse();
364 // this will generate a click event
365 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
369 //else: the mouse was released outside the window, this doesn't
372 //else: don't do anything special about the double click
375 return wxStdInputHandler::HandleMouse(consumer
, event
);
378 bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
379 const wxMouseEvent
& event
)
381 // we only have to do something when the mouse leaves/enters the pressed
382 // button and don't care about the other ones
383 if ( event
.GetEventObject() == m_winCapture
)
385 // leaving the button should remove its pressed state
386 if ( event
.Leaving() )
388 // remember that the mouse is now outside
389 m_winHasMouse
= false;
391 // we do have a pressed button, so release it
392 consumer
->GetInputWindow()->SetCurrent(false);
393 consumer
->PerformAction(wxACTION_BUTTON_RELEASE
);
397 // and entering it back should make it pressed again if it had been
399 else if ( event
.Entering() )
401 // the mouse is (back) inside the button
402 m_winHasMouse
= true;
404 // we did have a pressed button which we released when leaving the
405 // window, press it again
406 consumer
->GetInputWindow()->SetCurrent(true);
407 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
413 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
416 bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer
* WXUNUSED(consumer
),
417 const wxFocusEvent
& WXUNUSED(event
))
419 // buttons change appearance when they get/lose focus, so return true to
424 bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer
*consumer
,
425 bool WXUNUSED(activated
))
427 // the default button changes appearance when the app is [de]activated, so
428 // return true to refresh
429 return wxStaticCast(consumer
->GetInputWindow(), wxButton
)->IsDefault();
432 #endif // wxUSE_BUTTON