]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/univ/checklst.cpp | |
3 | // Purpose: wxCheckListBox implementation | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 12.09.00 | |
7 | // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com) | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | #include "wx/wxprec.h" | |
20 | ||
21 | #ifdef __BORLANDC__ | |
22 | #pragma hdrstop | |
23 | #endif | |
24 | ||
25 | #if wxUSE_CHECKLISTBOX | |
26 | ||
27 | #include "wx/checklst.h" | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/log.h" | |
31 | #include "wx/dcclient.h" | |
32 | #include "wx/validate.h" | |
33 | #endif | |
34 | ||
35 | #include "wx/univ/renderer.h" | |
36 | #include "wx/univ/inphand.h" | |
37 | #include "wx/univ/theme.h" | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // wxStdCheckListBoxInputHandler | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | class WXDLLEXPORT wxStdCheckListboxInputHandler : public wxStdInputHandler | |
44 | { | |
45 | public: | |
46 | wxStdCheckListboxInputHandler(wxInputHandler *inphand); | |
47 | ||
48 | virtual bool HandleKey(wxInputConsumer *consumer, | |
49 | const wxKeyEvent& event, | |
50 | bool pressed); | |
51 | virtual bool HandleMouse(wxInputConsumer *consumer, | |
52 | const wxMouseEvent& event); | |
53 | }; | |
54 | ||
55 | // ============================================================================ | |
56 | // implementation of wxCheckListBox | |
57 | // ============================================================================ | |
58 | ||
59 | // ---------------------------------------------------------------------------- | |
60 | // creation | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | void wxCheckListBox::Init() | |
64 | { | |
65 | } | |
66 | ||
67 | wxCheckListBox::wxCheckListBox(wxWindow *parent, | |
68 | wxWindowID id, | |
69 | const wxPoint &pos, | |
70 | const wxSize &size, | |
71 | const wxArrayString& choices, | |
72 | long style, | |
73 | const wxValidator& validator, | |
74 | const wxString &name) | |
75 | { | |
76 | Init(); | |
77 | ||
78 | Create(parent, id, pos, size, choices, style, validator, name); | |
79 | } | |
80 | ||
81 | bool wxCheckListBox::Create(wxWindow *parent, | |
82 | wxWindowID id, | |
83 | const wxPoint &pos, | |
84 | const wxSize &size, | |
85 | const wxArrayString& choices, | |
86 | long style, | |
87 | const wxValidator& validator, | |
88 | const wxString &name) | |
89 | { | |
90 | wxCArrayString chs(choices); | |
91 | ||
92 | return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
93 | style, validator, name); | |
94 | } | |
95 | ||
96 | bool wxCheckListBox::Create(wxWindow *parent, | |
97 | wxWindowID id, | |
98 | const wxPoint &pos, | |
99 | const wxSize &size, | |
100 | int n, | |
101 | const wxString choices[], | |
102 | long style, | |
103 | const wxValidator& validator, | |
104 | const wxString &name) | |
105 | { | |
106 | if ( !wxListBox::Create(parent, id, pos, size, | |
107 | n, choices, style, validator, name) ) | |
108 | return false; | |
109 | ||
110 | CreateInputHandler(wxINP_HANDLER_CHECKLISTBOX); | |
111 | ||
112 | return true; | |
113 | } | |
114 | ||
115 | // ---------------------------------------------------------------------------- | |
116 | // wxCheckListBox functions | |
117 | // ---------------------------------------------------------------------------- | |
118 | ||
119 | bool wxCheckListBox::IsChecked(unsigned int item) const | |
120 | { | |
121 | wxCHECK_MSG( IsValid(item), false, | |
122 | wxT("invalid index in wxCheckListBox::IsChecked") ); | |
123 | ||
124 | return m_checks[item] != 0; | |
125 | } | |
126 | ||
127 | void wxCheckListBox::Check(unsigned int item, bool check) | |
128 | { | |
129 | wxCHECK_RET( IsValid(item), | |
130 | wxT("invalid index in wxCheckListBox::Check") ); | |
131 | ||
132 | // intermediate var is needed to avoid compiler warning with VC++ | |
133 | bool isChecked = m_checks[item] != 0; | |
134 | if ( check != isChecked ) | |
135 | { | |
136 | m_checks[item] = check; | |
137 | ||
138 | RefreshItem(item); | |
139 | } | |
140 | } | |
141 | ||
142 | // ---------------------------------------------------------------------------- | |
143 | // methods forwarded to wxListBox | |
144 | // ---------------------------------------------------------------------------- | |
145 | ||
146 | void wxCheckListBox::DoDeleteOneItem(unsigned int n) | |
147 | { | |
148 | wxListBox::DoDeleteOneItem(n); | |
149 | ||
150 | m_checks.RemoveAt(n); | |
151 | } | |
152 | ||
153 | void wxCheckListBox::OnItemInserted(unsigned int pos) | |
154 | { | |
155 | m_checks.Insert(false, pos); | |
156 | } | |
157 | ||
158 | void wxCheckListBox::DoClear() | |
159 | { | |
160 | m_checks.Empty(); | |
161 | } | |
162 | ||
163 | // ---------------------------------------------------------------------------- | |
164 | // drawing | |
165 | // ---------------------------------------------------------------------------- | |
166 | ||
167 | wxSize wxCheckListBox::DoGetBestClientSize() const | |
168 | { | |
169 | wxSize size = wxListBox::DoGetBestClientSize(); | |
170 | size.x += GetRenderer()->GetCheckBitmapSize().x; | |
171 | ||
172 | return size; | |
173 | } | |
174 | ||
175 | void wxCheckListBox::DoDrawRange(wxControlRenderer *renderer, | |
176 | int itemFirst, int itemLast) | |
177 | { | |
178 | renderer->DrawCheckItems(this, itemFirst, itemLast); | |
179 | } | |
180 | ||
181 | // ---------------------------------------------------------------------------- | |
182 | // actions | |
183 | // ---------------------------------------------------------------------------- | |
184 | ||
185 | bool wxCheckListBox::PerformAction(const wxControlAction& action, | |
186 | long numArg, | |
187 | const wxString& strArg) | |
188 | { | |
189 | if ( action == wxACTION_CHECKLISTBOX_TOGGLE ) | |
190 | { | |
191 | int sel = (int)numArg; | |
192 | if ( sel == -1 ) | |
193 | { | |
194 | sel = GetSelection(); | |
195 | } | |
196 | ||
197 | if ( sel != -1 ) | |
198 | { | |
199 | Check(sel, !IsChecked(sel)); | |
200 | ||
201 | SendEvent(wxEVT_CHECKLISTBOX, sel); | |
202 | } | |
203 | } | |
204 | else | |
205 | { | |
206 | return wxListBox::PerformAction(action, numArg, strArg); | |
207 | } | |
208 | ||
209 | return true; | |
210 | } | |
211 | ||
212 | /* static */ | |
213 | wxInputHandler *wxCheckListBox::GetStdInputHandler(wxInputHandler *handlerDef) | |
214 | { | |
215 | static wxStdCheckListboxInputHandler s_handler(handlerDef); | |
216 | ||
217 | return &s_handler; | |
218 | } | |
219 | ||
220 | // ---------------------------------------------------------------------------- | |
221 | // wxStdCheckListboxInputHandler | |
222 | // ---------------------------------------------------------------------------- | |
223 | ||
224 | wxStdCheckListboxInputHandler:: | |
225 | wxStdCheckListboxInputHandler(wxInputHandler *inphand) | |
226 | : wxStdInputHandler(wxListBox::GetStdInputHandler(inphand)) | |
227 | { | |
228 | } | |
229 | ||
230 | bool wxStdCheckListboxInputHandler::HandleKey(wxInputConsumer *consumer, | |
231 | const wxKeyEvent& event, | |
232 | bool pressed) | |
233 | { | |
234 | if ( pressed && (event.GetKeyCode() == WXK_SPACE) ) | |
235 | consumer->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE); | |
236 | ||
237 | return wxStdInputHandler::HandleKey(consumer, event, pressed); | |
238 | } | |
239 | ||
240 | bool wxStdCheckListboxInputHandler::HandleMouse(wxInputConsumer *consumer, | |
241 | const wxMouseEvent& event) | |
242 | { | |
243 | if ( event.LeftDown() || event.LeftDClick() ) | |
244 | { | |
245 | wxCheckListBox *lbox = wxStaticCast(consumer->GetInputWindow(), wxCheckListBox); | |
246 | int x, y; | |
247 | ||
248 | wxPoint pt = event.GetPosition(); | |
249 | pt -= consumer->GetInputWindow()->GetClientAreaOrigin(); | |
250 | lbox->CalcUnscrolledPosition(pt.x, pt.y, &x, &y); | |
251 | ||
252 | wxRenderer *renderer = lbox->GetRenderer(); | |
253 | x -= renderer->GetCheckItemMargin(); | |
254 | ||
255 | int item = y / lbox->GetLineHeight(); | |
256 | if ( x >= 0 && | |
257 | x < renderer->GetCheckBitmapSize().x && | |
258 | item >= 0 && | |
259 | (unsigned int)item < lbox->GetCount() ) | |
260 | { | |
261 | lbox->PerformAction(wxACTION_CHECKLISTBOX_TOGGLE, item); | |
262 | ||
263 | return true; | |
264 | } | |
265 | } | |
266 | ||
267 | return wxStdInputHandler::HandleMouse(consumer, event); | |
268 | } | |
269 | ||
270 | #endif // wxUSE_CHECKLISTBOX |