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