]> git.saurik.com Git - wxWidgets.git/blob - src/msw/tglbtn.cpp
mention ShouldInheritColours() change
[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 // default border for this control is none
77 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
78 {
79 style |= wxBORDER_NONE;
80 }
81
82 if (!CreateBase(parent, id, pos, size, style, validator, name))
83 return FALSE;
84
85 parent->AddChild(this);
86
87 m_backgroundColour = parent->GetBackgroundColour();
88 m_foregroundColour = parent->GetForegroundColour();
89
90 #ifndef BS_PUSHLIKE
91 #define BS_PUSHLIKE 0x00001000L
92 #endif
93
94 WXDWORD exStyle = 0;
95 long msStyle = MSWGetStyle(style, & exStyle) ;
96
97 msStyle |= BS_AUTOCHECKBOX | BS_PUSHLIKE | WS_TABSTOP ;
98
99 #ifdef __WIN32__
100 if(m_windowStyle & wxBU_LEFT)
101 msStyle |= BS_LEFT;
102 if(m_windowStyle & wxBU_RIGHT)
103 msStyle |= BS_RIGHT;
104 if(m_windowStyle & wxBU_TOP)
105 msStyle |= BS_TOP;
106 if(m_windowStyle & wxBU_BOTTOM)
107 msStyle |= BS_BOTTOM;
108 #endif
109
110 m_hWnd = (WXHWND)CreateWindowEx(exStyle,
111 wxT("BUTTON"), label,
112 msStyle, 0, 0, 0, 0,
113 (HWND)parent->GetHWND(),
114 (HMENU)m_windowId,
115 wxGetInstance(), NULL);
116
117 if ( m_hWnd == 0 )
118 {
119 wxLogError(_T("Failed to create a toggle button"));
120
121 return FALSE;
122 }
123
124 // Subclass again for purposes of dialog editing mode
125 SubclassWin(m_hWnd);
126
127 SetFont(parent->GetFont());
128
129 SetSize(pos.x, pos.y, size.x, size.y);
130
131 return TRUE;
132 }
133
134 void wxToggleButton::SetLabel(const wxString& label)
135 {
136 SetWindowText(GetHwnd(), label);
137 }
138
139 wxSize wxToggleButton::DoGetBestSize() const
140 {
141 wxString label = wxGetWindowText(GetHWND());
142 int wBtn;
143 GetTextExtent(label, &wBtn, NULL);
144
145 int wChar, hChar;
146 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
147
148 // add a margin - the button is wider than just its label
149 wBtn += 3*wChar;
150
151 // the button height is proportional to the height of the font used
152 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
153
154 wxSize sz = wxButton::GetDefaultSize();
155 if (wBtn > sz.x)
156 sz.x = wBtn;
157 if (hBtn > sz.y)
158 sz.y = hBtn;
159
160 return sz;
161 }
162
163 void wxToggleButton::SetValue(bool val)
164 {
165 SendMessage(GetHwnd(), BM_SETCHECK, val, 0);
166 }
167
168 #ifndef BST_CHECKED
169 #define BST_CHECKED 0x0001
170 #endif
171
172 bool wxToggleButton::GetValue() const
173 {
174 #ifdef __WIN32__
175 return (SendMessage(GetHwnd(), BM_GETCHECK, 0, 0) == BST_CHECKED);
176 #else
177 return ((0x001 & SendMessage(GetHwnd(), BM_GETCHECK, 0, 0)) == 0x001);
178 #endif
179 }
180
181 void wxToggleButton::Command(wxCommandEvent & event)
182 {
183 SetValue((event.GetInt() != 0));
184 ProcessCommand(event);
185 }
186
187 #endif // wxUSE_TOGGLEBTN
188