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 
   9 // Copyright:   (c) 2000 Johnny C. Norris II 
  10 // License:     Rocketeer license 
  11 ///////////////////////////////////////////////////////////////////////////// 
  13 // ============================================================================ 
  15 // ============================================================================ 
  17 // ---------------------------------------------------------------------------- 
  19 // ---------------------------------------------------------------------------- 
  21 #include "wx/wxprec.h" 
  27 #include "wx/tglbtn.h" 
  32     #include "wx/button.h" 
  34     #include "wx/dcscreen.h" 
  35     #include "wx/settings.h" 
  40 #include "wx/msw/private.h" 
  42 // ---------------------------------------------------------------------------- 
  44 // ---------------------------------------------------------------------------- 
  46 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
) 
  47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
) 
  49 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10) 
  51 // ============================================================================ 
  53 // ============================================================================ 
  55 // ---------------------------------------------------------------------------- 
  57 // ---------------------------------------------------------------------------- 
  59 bool wxToggleButton::MSWCommand(WXUINT 
WXUNUSED(param
), WXWORD 
WXUNUSED(id
)) 
  61    wxCommandEvent 
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, m_windowId
); 
  62    event
.SetInt(GetValue()); 
  63    event
.SetEventObject(this); 
  64    ProcessCommand(event
); 
  68 // Single check box item 
  69 bool wxToggleButton::Create(wxWindow 
*parent
, wxWindowID id
, 
  70                             const wxString
& label
, 
  72                             const wxSize
& size
, long style
, 
  73                             const wxValidator
& validator
, 
  76     // default border for this control is none 
  77     if ( (style 
& wxBORDER_MASK
) == wxBORDER_DEFAULT 
) 
  79         style 
|= wxBORDER_NONE
; 
  82    if (!CreateBase(parent
, id
, pos
, size
, style
, validator
, name
)) 
  85    parent
->AddChild(this); 
  87    m_backgroundColour 
= parent
->GetBackgroundColour(); 
  88    m_foregroundColour 
= parent
->GetForegroundColour(); 
  91 #define BS_PUSHLIKE 0x00001000L 
  95    long msStyle 
= MSWGetStyle(style
, & exStyle
) ; 
  97    msStyle 
|= BS_AUTOCHECKBOX 
| BS_PUSHLIKE 
| WS_TABSTOP 
; 
 100    if(m_windowStyle 
& wxBU_LEFT
) 
 102    if(m_windowStyle 
& wxBU_RIGHT
) 
 104    if(m_windowStyle 
& wxBU_TOP
) 
 106    if(m_windowStyle 
& wxBU_BOTTOM
) 
 107       msStyle 
|= BS_BOTTOM
; 
 110    m_hWnd 
= (WXHWND
)CreateWindowEx(exStyle
, 
 111                                    wxT("BUTTON"), label
, 
 113                                    (HWND
)parent
->GetHWND(), 
 115                                    wxGetInstance(), NULL
); 
 119         wxLogError(_T("Failed to create a toggle button")); 
 124     // Subclass again for purposes of dialog editing mode 
 127     SetFont(parent
->GetFont()); 
 129     SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
); 
 134 void wxToggleButton::SetLabel(const wxString
& label
) 
 136     SetWindowText(GetHwnd(), label
); 
 139 wxSize 
wxToggleButton::DoGetBestSize() const 
 141    wxString label 
= wxGetWindowText(GetHWND()); 
 143    GetTextExtent(label
, &wBtn
, NULL
); 
 146    wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont()); 
 148    // add a margin - the button is wider than just its label 
 151    // the button height is proportional to the height of the font used 
 152    int hBtn 
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
); 
 154    wxSize sz 
= wxButton::GetDefaultSize(); 
 163 void wxToggleButton::SetValue(bool val
) 
 165    SendMessage(GetHwnd(), BM_SETCHECK
, val
, 0); 
 169 #define BST_CHECKED 0x0001 
 172 bool wxToggleButton::GetValue() const 
 175    return (SendMessage(GetHwnd(), BM_GETCHECK
, 0, 0) == BST_CHECKED
); 
 177    return ((0x001 & SendMessage(GetHwnd(), BM_GETCHECK
, 0, 0)) == 0x001); 
 181 void wxToggleButton::Command(wxCommandEvent 
& event
) 
 183    SetValue((event
.GetInt() != 0)); 
 184    ProcessCommand(event
); 
 187 #endif // wxUSE_TOGGLEBTN