1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/os2/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.
7 // Modified by:
8 // Created:     08.02.01
9 // Copyright:   (c) 2000 Johnny C. Norris II
10 // Licence:     wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12 
13 // ============================================================================
14 // declatations
15 // ============================================================================
16 
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20 
21 #include "wx/wxprec.h"
22 
23 #ifdef __BORLANDC__
24     #pragma hdrstop
25 #endif
26 
27 #if wxUSE_TOGGLEBTN
28 
29 #include "wx/tglbtn.h"
30 
31 #ifndef WX_PRECOMP
32     #include "wx/button.h"
33     #include "wx/brush.h"
34     #include "wx/dcscreen.h"
35     #include "wx/settings.h"
36 
37     #include "wx/log.h"
38 #endif // WX_PRECOMP
39 
40 // ----------------------------------------------------------------------------
41 // macros
42 // ----------------------------------------------------------------------------
43 
44 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
45 wxDEFINE_EVENT( wxEVT_TOGGLEBUTTON, wxCommandEvent );
46 
47 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
48 
49 // ============================================================================
50 // implementation
51 // ============================================================================
52 
53 // ----------------------------------------------------------------------------
54 // wxToggleButton
55 // ----------------------------------------------------------------------------
56 
OS2Command(WXUINT WXUNUSED (param),WXWORD WXUNUSED (id))57 bool wxToggleButton::OS2Command(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
58 {
59    wxCommandEvent event(wxEVT_TOGGLEBUTTON, m_windowId);
60    event.SetInt(GetValue());
61    event.SetEventObject(this);
62    ProcessCommand(event);
63    return true;
64 }
65 
66 // Single check box item
Create(wxWindow * parent,wxWindowID id,const wxString & label,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)67 bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
68                             const wxString& label,
69                             const wxPoint& pos,
70                             const wxSize& size, long style,
71                             const wxValidator& validator,
72                             const wxString& name)
73 {
74     if ( !CreateControl(parent, id, pos, size, style, validator, name) )
75         return false;
76 
77     if ( !OS2CreateControl(wxT("BUTTON"), label, pos, size, 0) )
78       return false;
79 
80     return true;
81 }
82 
GetDefaultBorder() const83 wxBorder wxToggleButton::GetDefaultBorder() const
84 {
85     return wxBORDER_NONE;
86 }
87 
OS2GetStyle(long style,WXDWORD * exstyle) const88 WXDWORD wxToggleButton::OS2GetStyle(long style, WXDWORD *exstyle) const
89 {
90     WXDWORD msStyle = wxControl::OS2GetStyle(style, exstyle);
91 
92 #ifndef BS_PUSHLIKE
93 #define BS_PUSHLIKE 0x00001000L
94 #endif
95 
96     msStyle |= BS_AUTOCHECKBOX | BS_PUSHLIKE | WS_TABSTOP;
97 
98     return msStyle;
99 }
100 
DoGetBestSize() const101 wxSize wxToggleButton::DoGetBestSize() const
102 {
103    wxString                         label = wxGetWindowText(GetHWND());
104    int                              wBtn;
105    wxFont                           vFont =  GetFont();
106    int                              wChar;
107    int                              hChar;
108 
109    GetTextExtent(label, &wBtn, NULL);
110 
111 
112    wxGetCharSize(GetHWND(), &wChar, &hChar, &vFont);
113 
114    // add a margin - the button is wider than just its label
115    wBtn += 3*wChar;
116 
117    // the button height is proportional to the height of the font used
118    int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
119 
120 #if wxUSE_BUTTON
121    wxSize sz = wxButton::GetDefaultSize();
122    if (wBtn > sz.x)
123        sz.x = wBtn;
124    if (hBtn > sz.y)
125        sz.y = hBtn;
126 #else
127    wxSize sz(wBtn, hBtn);
128 #endif
129 
130    return sz;
131 }
132 
SetValue(bool val)133 void wxToggleButton::SetValue(bool val)
134 {
135    ::WinSendMsg(GetHwnd(), BM_SETCHECK, MPFROMSHORT(val), (MPARAM)0);
136 }
137 
138 #ifndef BST_CHECKED
139 #define BST_CHECKED 0x0001
140 #endif
141 
GetValue() const142 bool wxToggleButton::GetValue() const
143 {
144    return (::WinSendMsg(GetHwnd(), BM_QUERYCHECK, 0, 0) == (MRESULT)BST_CHECKED);
145 }
146 
Command(wxCommandEvent & event)147 void wxToggleButton::Command(wxCommandEvent & event)
148 {
149    SetValue((event.GetInt() != 0));
150    ProcessCommand(event);
151 }
152 
153 #endif // wxUSE_TOGGLEBTN
154