1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/msw/tglbtn.cpp 
   3 // Purpose:     Definition of the wxToggleButton class, which implements a 
   4 //              toggle button under wxMSW. 
   5 // Author: John Norris, minor changes by Axel Schlueter 
   6 // and William Gallafent. 
  10 // Copyright:   (c) 2000 Johnny C. Norris II 
  11 // License:     wxWindows licence 
  12 ///////////////////////////////////////////////////////////////////////////// 
  14 // ============================================================================ 
  16 // ============================================================================ 
  18 // ---------------------------------------------------------------------------- 
  20 // ---------------------------------------------------------------------------- 
  22 #include "wx/wxprec.h" 
  28 #include "wx/tglbtn.h" 
  33     #include "wx/button.h" 
  35     #include "wx/dcscreen.h" 
  36     #include "wx/settings.h" 
  41 // ---------------------------------------------------------------------------- 
  43 // ---------------------------------------------------------------------------- 
  45 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
) 
  46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
) 
  48 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10) 
  50 // ============================================================================ 
  52 // ============================================================================ 
  54 // ---------------------------------------------------------------------------- 
  56 // ---------------------------------------------------------------------------- 
  58 bool wxToggleButton::OS2Command(WXUINT 
WXUNUSED(param
), WXWORD 
WXUNUSED(id
)) 
  60    wxCommandEvent 
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, m_windowId
); 
  61    event
.SetInt(GetValue()); 
  62    event
.SetEventObject(this); 
  63    ProcessCommand(event
); 
  67 // Single check box item 
  68 bool wxToggleButton::Create(wxWindow 
*parent
, wxWindowID id
, 
  69                             const wxString
& label
, 
  71                             const wxSize
& size
, long style
, 
  72                             const wxValidator
& validator
, 
  75     if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) ) 
  78     if ( !OS2CreateControl(wxT("BUTTON"), label
, pos
, size
, 0) ) 
  84 wxBorder 
wxToggleButton::GetDefaultBorder() const 
  89 WXDWORD 
wxToggleButton::OS2GetStyle(long style
, WXDWORD 
*exstyle
) const 
  91     WXDWORD msStyle 
= wxControl::OS2GetStyle(style
, exstyle
); 
  94 #define BS_PUSHLIKE 0x00001000L 
  97     msStyle 
|= BS_AUTOCHECKBOX 
| BS_PUSHLIKE 
| WS_TABSTOP
; 
 102 wxSize 
wxToggleButton::DoGetBestSize() const 
 104    wxString                         label 
= wxGetWindowText(GetHWND()); 
 106    wxFont                           vFont 
=  GetFont(); 
 110    GetTextExtent(label
, &wBtn
, NULL
); 
 113    wxGetCharSize(GetHWND(), &wChar
, &hChar
, &vFont
); 
 115    // add a margin - the button is wider than just its label 
 118    // the button height is proportional to the height of the font used 
 119    int hBtn 
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
); 
 122    wxSize sz 
= wxButton::GetDefaultSize(); 
 128    wxSize 
sz(wBtn
, hBtn
); 
 134 void wxToggleButton::SetValue(bool val
) 
 136    ::WinSendMsg(GetHwnd(), BM_SETCHECK
, MPFROMSHORT(val
), (MPARAM
)0); 
 140 #define BST_CHECKED 0x0001 
 143 bool wxToggleButton::GetValue() const 
 145    return (::WinSendMsg(GetHwnd(), BM_QUERYCHECK
, 0, 0) == (MRESULT
)BST_CHECKED
); 
 148 void wxToggleButton::Command(wxCommandEvent 
& event
) 
 150    SetValue((event
.GetInt() != 0)); 
 151    ProcessCommand(event
); 
 154 #endif // wxUSE_TOGGLEBTN