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 // ---------------------------------------------------------------------------- 
  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" 
  39 #include "wx/univ/renderer.h" 
  40 #include "wx/univ/inphand.h" 
  41 #include "wx/univ/theme.h" 
  42 #include "wx/univ/colschem.h" 
  44 // ---------------------------------------------------------------------------- 
  46 // ---------------------------------------------------------------------------- 
  48 // default margins around the image 
  49 static const wxCoord DEFAULT_BTN_MARGIN_X 
= 0;  // We should give space for the border, at least. 
  50 static const wxCoord DEFAULT_BTN_MARGIN_Y 
= 0; 
  52 // ============================================================================ 
  54 // ============================================================================ 
  56 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
) 
  58 // ---------------------------------------------------------------------------- 
  60 // ---------------------------------------------------------------------------- 
  68 bool wxButton::Create(wxWindow 
*parent
, 
  70                       const wxBitmap
& bitmap
, 
  71                       const wxString 
&label
, 
  75                       const wxValidator
& validator
, 
  78     // center label by default 
  79     if ( !(style 
& wxALIGN_MASK
) ) 
  81         style 
|= wxALIGN_CENTRE_HORIZONTAL 
| wxALIGN_CENTRE_VERTICAL
; 
  84     if ( !wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) ) 
  88     SetImageLabel(bitmap
); 
  89     // SetBestSize(size); -- called by SetImageLabel() 
  91     CreateInputHandler(wxINP_HANDLER_BUTTON
); 
 100 // ---------------------------------------------------------------------------- 
 102 // ---------------------------------------------------------------------------- 
 105 wxSize 
wxButtonBase::GetDefaultSize() 
 107     static wxSize s_sizeBtn
; 
 109     if ( s_sizeBtn
.x 
== 0 ) 
 113         // this corresponds more or less to wxMSW standard in Win32 theme (see 
 114         // wxWin32Renderer::AdjustSize()) 
 115         s_sizeBtn
.x 
= 8*dc
.GetCharWidth(); 
 116         s_sizeBtn
.y 
= (11*dc
.GetCharHeight())/10 + 2; 
 122 wxSize 
wxButton::DoGetBestClientSize() const 
 124     wxClientDC 
dc(wxConstCast(this, wxButton
)); 
 125     wxCoord width
, height
; 
 126     dc
.GetMultiLineTextExtent(GetLabel(), &width
, &height
); 
 130         // allocate extra space for the bitmap 
 131         wxCoord heightBmp 
= m_bitmap
.GetHeight() + 2*m_marginBmpY
; 
 132         if ( height 
< heightBmp 
) 
 135         width 
+= m_bitmap
.GetWidth() + 2*m_marginBmpX
; 
 138     // for compatibility with other ports, the buttons default size is never 
 139     // less than the standard one 
 141     if ( !(GetWindowStyle() & wxBU_EXACTFIT
) ) 
 143         wxSize szDef 
= GetDefaultSize(); 
 144         if ( width 
< szDef
.x 
) 
 149     return wxSize(width
, height
); 
 152 // ---------------------------------------------------------------------------- 
 154 // ---------------------------------------------------------------------------- 
 156 void wxButton::DoDraw(wxControlRenderer 
*renderer
) 
 158     if ( !(GetWindowStyle() & wxBORDER_NONE
) ) 
 160         renderer
->DrawButtonBorder(); 
 163     renderer
->DrawLabel(m_bitmap
, m_marginBmpX
, m_marginBmpY
); 
 166 bool wxButton::DoDrawBackground(wxDC
& dc
) 
 169     wxSize size 
= GetSize(); 
 171     rect
.height 
= size
.y
; 
 173     if ( GetBackgroundBitmap().Ok() ) 
 175         // get the bitmap and the flags 
 178         wxBitmap bmp 
= GetBackgroundBitmap(&alignment
, &stretch
); 
 179         wxControlRenderer::DrawBitmap(dc
, bmp
, rect
, alignment
, stretch
); 
 183         m_renderer
->DrawButtonSurface(dc
, wxTHEME_BG_COLOUR(this), 
 184                                       rect
, GetStateFlags()); 
 190 // ---------------------------------------------------------------------------- 
 192 // ---------------------------------------------------------------------------- 
 194 void wxButton::Press() 
 204 void wxButton::Release() 
 214 void wxButton::Toggle() 
 223         // releasing button after it had been pressed generates a click event 
 228 void wxButton::Click() 
 230     wxCommandEvent 
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId()); 
 231     InitCommandEvent(event
); 
 235 bool wxButton::PerformAction(const wxControlAction
& action
, 
 237                              const wxString
& strArg
) 
 239     if ( action 
== wxACTION_BUTTON_TOGGLE 
) 
 241     else if ( action 
== wxACTION_BUTTON_CLICK 
) 
 243     else if ( action 
== wxACTION_BUTTON_PRESS 
) 
 245     else if ( action 
== wxACTION_BUTTON_RELEASE 
) 
 248         return wxControl::PerformAction(action
, numArg
, strArg
); 
 253 // ---------------------------------------------------------------------------- 
 255 // ---------------------------------------------------------------------------- 
 257 void wxButton::SetImageLabel(const wxBitmap
& bitmap
) 
 261     SetImageMargins(DEFAULT_BTN_MARGIN_X
, DEFAULT_BTN_MARGIN_Y
); 
 264 void wxButton::SetImageMargins(wxCoord x
, wxCoord y
) 
 266     m_marginBmpX 
= x 
+ 2; 
 267     m_marginBmpY 
= y 
+ 2; 
 269     SetBestSize(wxDefaultSize
); 
 272 void wxButton::SetDefault() 
 277 // ============================================================================ 
 278 // wxStdButtonInputHandler 
 279 // ============================================================================ 
 281 wxStdButtonInputHandler::wxStdButtonInputHandler(wxInputHandler 
*handler
) 
 282                        : wxStdInputHandler(handler
) 
 285     m_winHasMouse 
= FALSE
; 
 288 bool wxStdButtonInputHandler::HandleKey(wxInputConsumer 
*consumer
, 
 289                                         const wxKeyEvent
& event
, 
 292     int keycode 
= event
.GetKeyCode(); 
 293     if ( keycode 
== WXK_SPACE 
|| keycode 
== WXK_RETURN 
) 
 295         consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
); 
 300     return wxStdInputHandler::HandleKey(consumer
, event
, pressed
); 
 303 bool wxStdButtonInputHandler::HandleMouse(wxInputConsumer 
*consumer
, 
 304                                           const wxMouseEvent
& event
) 
 306     // the button has 2 states: pressed and normal with the following 
 307     // transitions between them: 
 309     //      normal -> left down -> capture mouse and go to pressed state 
 310     //      pressed -> left up inside -> generate click -> go to normal 
 311     //                         outside ------------------> 
 313     // the other mouse buttons are ignored 
 314     if ( event
.Button(1) ) 
 316         if ( event
.LeftDown() || event
.LeftDClick() ) 
 318             m_winCapture 
= consumer
->GetInputWindow(); 
 319             m_winCapture
->CaptureMouse(); 
 320             m_winHasMouse 
= TRUE
; 
 322             consumer
->PerformAction(wxACTION_BUTTON_PRESS
); 
 326         else if ( event
.LeftUp() ) 
 330                 m_winCapture
->ReleaseMouse(); 
 336                 // this will generate a click event 
 337                 consumer
->PerformAction(wxACTION_BUTTON_TOGGLE
); 
 341             //else: the mouse was released outside the window, this doesn't 
 344         //else: don't do anything special about the double click 
 347     return wxStdInputHandler::HandleMouse(consumer
, event
); 
 350 bool wxStdButtonInputHandler::HandleMouseMove(wxInputConsumer 
*consumer
, 
 351                                               const wxMouseEvent
& event
) 
 353     // we only have to do something when the mouse leaves/enters the pressed 
 354     // button and don't care about the other ones 
 355     if ( event
.GetEventObject() == m_winCapture 
) 
 357         // leaving the button should remove its pressed state 
 358         if ( event
.Leaving() ) 
 360             // remember that the mouse is now outside 
 361             m_winHasMouse 
= FALSE
; 
 363             // we do have a pressed button, so release it 
 364             consumer
->GetInputWindow()->SetCurrent(FALSE
); 
 365             consumer
->PerformAction(wxACTION_BUTTON_RELEASE
); 
 369         // and entering it back should make it pressed again if it had been 
 371         else if ( event
.Entering() ) 
 373             // the mouse is (back) inside the button 
 374             m_winHasMouse 
= TRUE
; 
 376             // we did have a pressed button which we released when leaving the 
 377             // window, press it again 
 378             consumer
->GetInputWindow()->SetCurrent(TRUE
); 
 379             consumer
->PerformAction(wxACTION_BUTTON_PRESS
); 
 385     return wxStdInputHandler::HandleMouseMove(consumer
, event
); 
 388 bool wxStdButtonInputHandler::HandleFocus(wxInputConsumer 
*consumer
, 
 389                                           const wxFocusEvent
& event
) 
 391     // buttons change appearance when they get/lose focus, so return TRUE to 
 396 bool wxStdButtonInputHandler::HandleActivation(wxInputConsumer 
*consumer
, 
 399     // the default button changes appearance when the app is [de]activated, so 
 400     // return TRUE to refresh 
 401     return wxStaticCast(consumer
->GetInputWindow(), wxButton
)->IsDefault(); 
 404 #endif // wxUSE_BUTTON