]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tglbtn.cpp
corrected conditional compilation
[wxWidgets.git] / src / msw / tglbtn.cpp
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 // Modified by:
7 // Created: 08.02.01
8 // RCS-ID: $Id$
9 // Copyright: (c) 2000 Johnny C. Norris II
10 // License: Rocketeer license
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 #include "wx/tglbtn.h"
28
29 #if wxUSE_TOGGLEBTN
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 #include "wx/msw/private.h"
41
42 // ----------------------------------------------------------------------------
43 // macros
44 // ----------------------------------------------------------------------------
45
46 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
48
49 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
50
51 // ============================================================================
52 // implementation
53 // ============================================================================
54
55 // ----------------------------------------------------------------------------
56 // wxToggleButton
57 // ----------------------------------------------------------------------------
58
59 bool wxToggleButton::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
60 {
61 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, m_windowId);
62 event.SetInt(GetValue());
63 event.SetEventObject(this);
64 ProcessCommand(event);
65 return TRUE;
66 }
67
68 // Single check box item
69 bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
70 const wxString& label,
71 const wxPoint& pos,
72 const wxSize& size, long style,
73 const wxValidator& validator,
74 const wxString& name)
75 {
76 if (!CreateBase(parent, id, pos, size, style, validator, name))
77 return FALSE;
78
79 parent->AddChild(this);
80
81 m_backgroundColour = parent->GetBackgroundColour();
82 m_foregroundColour = parent->GetForegroundColour();
83
84 #ifndef BS_PUSHLIKE
85 #define BS_PUSHLIKE 0x00001000L
86 #endif
87
88 long msStyle = BS_AUTOCHECKBOX | BS_PUSHLIKE | WS_TABSTOP | WS_CHILD | WS_VISIBLE;
89
90 if ( m_windowStyle & wxCLIP_SIBLINGS )
91 msStyle |= WS_CLIPSIBLINGS;
92
93 #ifdef __WIN32__
94 if(m_windowStyle & wxBU_LEFT)
95 msStyle |= BS_LEFT;
96 if(m_windowStyle & wxBU_RIGHT)
97 msStyle |= BS_RIGHT;
98 if(m_windowStyle & wxBU_TOP)
99 msStyle |= BS_TOP;
100 if(m_windowStyle & wxBU_BOTTOM)
101 msStyle |= BS_BOTTOM;
102 #endif
103
104 m_hWnd = (WXHWND)CreateWindowEx(MakeExtendedStyle(m_windowStyle),
105 wxT("BUTTON"), label,
106 msStyle, 0, 0, 0, 0,
107 (HWND)parent->GetHWND(),
108 (HMENU)m_windowId,
109 wxGetInstance(), NULL);
110
111 if ( m_hWnd == 0 )
112 {
113 wxLogError(_T("Failed to create a toggle button"));
114
115 return FALSE;
116 }
117
118 // Subclass again for purposes of dialog editing mode
119 SubclassWin(m_hWnd);
120
121 SetFont(parent->GetFont());
122
123 SetSize(pos.x, pos.y, size.x, size.y);
124
125 return TRUE;
126 }
127
128 void wxToggleButton::SetLabel(const wxString& label)
129 {
130 SetWindowText(GetHwnd(), label);
131 }
132
133 wxSize wxToggleButton::DoGetBestSize() const
134 {
135 wxString label = wxGetWindowText(GetHWND());
136 int wBtn;
137 GetTextExtent(label, &wBtn, NULL);
138
139 int wChar, hChar;
140 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
141
142 // add a margin - the button is wider than just its label
143 wBtn += 3*wChar;
144
145 // the button height is proportional to the height of the font used
146 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
147
148 wxSize sz = wxButton::GetDefaultSize();
149 if (wBtn > sz.x)
150 sz.x = wBtn;
151 if (hBtn > sz.y)
152 sz.y = hBtn;
153
154 return sz;
155 }
156
157 void wxToggleButton::SetValue(bool val)
158 {
159 SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
160 }
161
162 #ifndef BST_CHECKED
163 #define BST_CHECKED 0x0001
164 #endif
165
166 bool wxToggleButton::GetValue() const
167 {
168 #ifdef __WIN32__
169 return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED);
170 #else
171 return ((0x001 & SendMessage(GetHwnd(), BM_GETCHECK, 0, 0)) == 0x001);
172 #endif
173 }
174
175 void wxToggleButton::Command(wxCommandEvent & event)
176 {
177 SetValue((event.GetInt() != 0));
178 ProcessCommand(event);
179 }
180
181 #endif // wxUSE_TOGGLEBTN
182