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"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // default margins around the image
50 static const wxCoord DEFAULT_BTN_MARGIN_X
= 0; // We should give space for the border, at least.
51 static const wxCoord DEFAULT_BTN_MARGIN_Y
= 0;
53 // ============================================================================
55 // ============================================================================
57 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 wxButtonBase::wxButtonBase()
72 wxButton::wxButton(wxWindow
*parent
,
74 const wxBitmap
& bitmap
,
75 const wxString
& label
,
79 const wxValidator
& validator
,
84 Create(parent
, id
, bitmap
, label
, pos
, size
, style
, validator
, name
);
87 wxButton::wxButton(wxWindow
*parent
,
89 const wxString
& label
,
93 const wxValidator
& validator
,
98 Create(parent
, id
, label
, pos
, size
, style
, validator
, name
);
101 void wxButton::Init()
107 bool wxButton::Create(wxWindow
*parent
,
109 const wxBitmap
& bitmap
,
110 const wxString
&label
,
114 const wxValidator
& validator
,
115 const wxString
&name
)
117 // center label by default
118 if ( !(style
& wxALIGN_MASK
) )
120 style
|= wxALIGN_CENTRE_HORIZONTAL
| wxALIGN_CENTRE_VERTICAL
;
123 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
127 SetImageLabel(bitmap
);
128 // SetBestSize(size); -- called by SetImageLabel()
130 CreateInputHandler(wxINP_HANDLER_BUTTON
);
135 wxButton::~wxButton()
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
144 wxSize
wxButtonBase::GetDefaultSize()
146 static wxSize s_sizeBtn
;
148 if ( s_sizeBtn
.x
== 0 )
152 // this corresponds more or less to wxMSW standard in Win32 theme (see
153 // wxWin32Renderer::AdjustSize())
154 // s_sizeBtn.x = 8*dc.GetCharWidth();
155 // s_sizeBtn.y = (11*dc.GetCharHeight())/10 + 2;
156 // Otto Wyss, Patch 664399
157 s_sizeBtn
.x
= dc
.GetCharWidth()*10 + 2;
158 s_sizeBtn
.y
= dc
.GetCharHeight()*11/10 + 2;
164 wxSize
wxButton::DoGetBestClientSize() const
166 wxClientDC
dc(wxConstCast(this, wxButton
));
167 wxCoord width
, height
;
168 dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
);
172 // allocate extra space for the bitmap
173 wxCoord heightBmp
= m_bitmap
.GetHeight() + 2*m_marginBmpY
;
174 if ( height
< heightBmp
)
177 width
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
;
180 // The default size should not be adjusted, so the code is moved into the
181 // renderer. This is conceptual wrong but currently the only solution.
182 // (Otto Wyss, Patch 664399)
185 // for compatibility with other ports, the buttons default size is never
186 // less than the standard one, but not when display not PDAs.
187 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
189 if ( !(GetWindowStyle() & wxBU_EXACTFIT) )
191 wxSize szDef = GetDefaultSize();
192 if ( width < szDef.x )
197 return wxSize(width
, height
);
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 void wxButton::DoDraw(wxControlRenderer
*renderer
)
206 if ( !(GetWindowStyle() & wxBORDER_NONE
) )
208 renderer
->DrawButtonBorder();
211 renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
);
214 bool wxButton::DoDrawBackground(wxDC
& dc
)
217 wxSize size
= GetSize();
219 rect
.height
= size
.y
;
221 if ( GetBackgroundBitmap().Ok() )
223 // get the bitmap and the flags
226 wxBitmap bmp
= GetBackgroundBitmap(&alignment
, &stretch
);
227 wxControlRenderer::DrawBitmap(dc
, bmp
, rect
, alignment
, stretch
);
231 m_renderer
->DrawButtonSurface(dc
, wxTHEME_BG_COLOUR(this),
232 rect
, GetStateFlags());
238 // ----------------------------------------------------------------------------
240 // ----------------------------------------------------------------------------
242 void wxButton::Press()
252 void wxButton::Release()
262 void wxButton::Toggle()
271 // releasing button after it had been pressed generates a click event
276 void wxButton::Click()
278 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
279 InitCommandEvent(event
);
283 bool wxButton::PerformAction(const wxControlAction
& action
,
285 const wxString
& strArg
)
287 if ( action
== wxACTION_BUTTON_TOGGLE
)
289 else if ( action
== wxACTION_BUTTON_CLICK
)
291 else if ( action
== wxACTION_BUTTON_PRESS
)
293 else if ( action
== wxACTION_BUTTON_RELEASE
)
296 return wxControl::PerformAction(action
, numArg
, strArg
);
301 // ----------------------------------------------------------------------------
303 // ----------------------------------------------------------------------------
305 void wxButton::SetImageLabel(const wxBitmap
& bitmap
)
309 SetImageMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
);
312 void wxButton::SetImageMargins(wxCoord x
, wxCoord y
)
314 m_marginBmpX
= x
+ 2;
315 m_marginBmpY
= y
+ 2;
317 SetBestSize(wxDefaultSize
);
320 void wxButton::SetDefault()
325 // ============================================================================
326 // wxStdButtonInputHandler
327 // ============================================================================
329 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler
*handler
)
330 : wxStdInputHandler(handler
)
333 m_winHasMouse
= FALSE
;
336 bool wxStdButtonInputHandler::HandleKey(wxInputConsumer
*consumer
,
337 const wxKeyEvent
& event
,
340 int keycode
= event
.GetKeyCode();
341 if ( keycode
== WXK_SPACE
|| keycode
== WXK_RETURN
)
343 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
348 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
351 bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer
*consumer
,
352 const wxMouseEvent
& event
)
354 // the button has 2 states: pressed and normal with the following
355 // transitions between them:
357 // normal -> left down -> capture mouse and go to pressed state
358 // pressed -> left up inside -> generate click -> go to normal
359 // outside ------------------>
361 // the other mouse buttons are ignored
362 if ( event
.Button(1) )
364 if ( event
.LeftDown() || event
.LeftDClick() )
366 m_winCapture
= consumer
->GetInputWindow();
367 m_winCapture
->CaptureMouse();
368 m_winHasMouse
= TRUE
;
370 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
374 else if ( event
.LeftUp() )
378 m_winCapture
->ReleaseMouse();
384 // this will generate a click event
385 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
);
389 //else: the mouse was released outside the window, this doesn't
392 //else: don't do anything special about the double click
395 return wxStdInputHandler::HandleMouse(consumer
, event
);
398 bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
399 const wxMouseEvent
& event
)
401 // we only have to do something when the mouse leaves/enters the pressed
402 // button and don't care about the other ones
403 if ( event
.GetEventObject() == m_winCapture
)
405 // leaving the button should remove its pressed state
406 if ( event
.Leaving() )
408 // remember that the mouse is now outside
409 m_winHasMouse
= FALSE
;
411 // we do have a pressed button, so release it
412 consumer
->GetInputWindow()->SetCurrent(FALSE
);
413 consumer
->PerformAction(wxACTION_BUTTON_RELEASE
);
417 // and entering it back should make it pressed again if it had been
419 else if ( event
.Entering() )
421 // the mouse is (back) inside the button
422 m_winHasMouse
= TRUE
;
424 // we did have a pressed button which we released when leaving the
425 // window, press it again
426 consumer
->GetInputWindow()->SetCurrent(TRUE
);
427 consumer
->PerformAction(wxACTION_BUTTON_PRESS
);
433 return wxStdInputHandler::HandleMouseMove(consumer
, event
);
436 bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer
* WXUNUSED(consumer
),
437 const wxFocusEvent
& WXUNUSED(event
))
439 // buttons change appearance when they get/lose focus, so return TRUE to
444 bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer
*consumer
,
445 bool WXUNUSED(activated
))
447 // the default button changes appearance when the app is [de]activated, so
448 // return TRUE to refresh
449 return wxStaticCast(consumer
->GetInputWindow(), wxButton
)->IsDefault();
452 #endif // wxUSE_BUTTON