]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/checklst.cpp | |
3 | // Purpose: implementation of wxCheckListBox class | |
4 | // Author: Vadim Zeitlin | |
45187197 | 5 | // Modified by: |
2bda0e17 KB |
6 | // Created: 16.11.97 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
6c9a19aa | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
dd3c394a VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 KB |
20 | #ifdef __GNUG__ |
21 | #pragma implementation "checklst.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
47d67540 | 31 | #if wxUSE_OWNER_DRAWN |
1c089c47 | 32 | |
d90879fa VZ |
33 | #ifndef WX_PRECOMP |
34 | #include "wx/object.h" | |
35 | #include "wx/colour.h" | |
36 | #include "wx/font.h" | |
37 | #include "wx/bitmap.h" | |
38 | #include "wx/window.h" | |
39 | #include "wx/listbox.h" | |
40 | #include "wx/dcmemory.h" | |
41 | ||
42 | #include "wx/settings.h" | |
43 | ||
44 | #include "wx/log.h" | |
45 | #endif | |
46 | ||
2bda0e17 | 47 | #include "wx/ownerdrw.h" |
d90879fa | 48 | #include "wx/checklst.h" |
2bda0e17 | 49 | |
3d05544e | 50 | #include <windows.h> |
dd3c394a VZ |
51 | #include <windowsx.h> |
52 | ||
ae090fdb | 53 | #if defined(__GNUWIN32_OLD__) |
c42404a5 | 54 | #include "wx/msw/gnuwin32/extra.h" |
9a05fd8d JS |
55 | #endif |
56 | ||
dd3c394a VZ |
57 | // ---------------------------------------------------------------------------- |
58 | // private functions | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | // get item (converted to right type) | |
62 | #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n))) | |
3d05544e | 63 | |
2bda0e17 KB |
64 | // ============================================================================ |
65 | // implementation | |
66 | // ============================================================================ | |
67 | ||
fd7ab28c | 68 | IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox) |
2bda0e17 KB |
69 | |
70 | // ---------------------------------------------------------------------------- | |
71 | // declaration and implementation of wxCheckListBoxItem class | |
72 | // ---------------------------------------------------------------------------- | |
73 | ||
74 | class wxCheckListBoxItem : public wxOwnerDrawn | |
75 | { | |
2b5f62a0 | 76 | friend class WXDLLEXPORT wxCheckListBox; |
2bda0e17 KB |
77 | public: |
78 | // ctor | |
c86f1403 | 79 | wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex); |
2bda0e17 KB |
80 | |
81 | // drawing functions | |
82 | virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat); | |
83 | ||
e2ca995f VZ |
84 | // simple accessors and operations |
85 | bool IsChecked() const { return m_bChecked; } | |
86 | ||
dd3c394a VZ |
87 | void Check(bool bCheck); |
88 | void Toggle() { Check(!IsChecked()); } | |
2bda0e17 | 89 | |
e2ca995f VZ |
90 | void SendEvent(); |
91 | ||
2bda0e17 | 92 | private: |
22f3361e VZ |
93 | |
94 | DECLARE_NO_COPY_CLASS(wxCheckListBoxItem) | |
2bda0e17 KB |
95 | bool m_bChecked; |
96 | wxCheckListBox *m_pParent; | |
e2ca995f | 97 | size_t m_nIndex; |
2bda0e17 KB |
98 | }; |
99 | ||
c86f1403 | 100 | wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex) |
2b5f62a0 | 101 | : wxOwnerDrawn(wxEmptyString, TRUE) // checkable |
2bda0e17 KB |
102 | { |
103 | m_bChecked = FALSE; | |
104 | m_pParent = pParent; | |
105 | m_nIndex = nIndex; | |
106 | ||
107 | // we don't initialize m_nCheckHeight/Width vars because it's | |
108 | // done in OnMeasure while they are used only in OnDraw and we | |
109 | // know that there will always be OnMeasure before OnDraw | |
110 | ||
111 | // fix appearance | |
2bda0e17 KB |
112 | SetMarginWidth(GetDefaultMarginWidth()); |
113 | } | |
114 | ||
115 | /* | |
116 | * JACS - I've got the owner-draw stuff partially working with WIN16, | |
117 | * with a really horrible-looking cross for wxCheckListBox instead of a | |
118 | * check - could use a bitmap check-mark instead, defined in wx.rc. | |
119 | * Also there's a refresh problem whereby it doesn't always draw the | |
120 | * check until you click to the right of it, which is OK for WIN32. | |
121 | */ | |
122 | ||
45187197 | 123 | bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc, |
2bda0e17 KB |
124 | wxODAction act, wxODStatus stat) |
125 | { | |
126 | if ( IsChecked() ) | |
127 | stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked); | |
128 | ||
129 | if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) { | |
130 | // ## using native API for performance and precision | |
c86f1403 | 131 | size_t nCheckWidth = GetDefaultMarginWidth(), |
2bda0e17 | 132 | nCheckHeight = m_pParent->GetItemHeight(); |
45187197 RD |
133 | |
134 | int x = rc.GetX(), | |
2bda0e17 KB |
135 | y = rc.GetY(); |
136 | ||
137 | HDC hdc = (HDC)dc.GetHDC(); | |
138 | ||
139 | // create pens | |
140 | HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)), | |
141 | hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)), | |
45187197 | 142 | hpenPrev = (HPEN)SelectObject(hdc, hpenBack); |
2bda0e17 KB |
143 | |
144 | // we erase the 1-pixel border | |
145 | Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight); | |
146 | ||
147 | // shift check mark 1 pixel to the right (it looks better like this) | |
148 | x++; | |
149 | ||
150 | if ( IsChecked() ) { | |
151 | // first create a monochrome bitmap in a memory DC | |
152 | HDC hdcMem = CreateCompatibleDC(hdc); | |
153 | HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0); | |
45187197 | 154 | HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck); |
2bda0e17 KB |
155 | |
156 | // then draw a check mark into it | |
ee6e1b1d VZ |
157 | #if defined(__WIN32__) && !defined(__SC__) |
158 | RECT rect; | |
159 | rect.left = 0; | |
160 | rect.top = 0; | |
161 | rect.right = nCheckWidth; | |
162 | rect.bottom = nCheckHeight; | |
163 | ||
2bda0e17 KB |
164 | DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK); |
165 | #else | |
166 | // In WIN16, draw a cross | |
167 | HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); | |
27a9bd48 PA |
168 | HPEN whiteBrush = (HPEN)GetStockObject(WHITE_BRUSH); |
169 | HPEN hPenOld = (HPEN)::SelectObject(hdcMem, blackPen); | |
170 | HPEN hBrushOld = (HPEN)::SelectObject(hdcMem, whiteBrush); | |
2bda0e17 KB |
171 | ::SetROP2(hdcMem, R2_COPYPEN); |
172 | Rectangle(hdcMem, 0, 0, nCheckWidth, nCheckHeight); | |
173 | MoveTo(hdcMem, 0, 0); | |
174 | LineTo(hdcMem, nCheckWidth, nCheckHeight); | |
175 | MoveTo(hdcMem, nCheckWidth, 0); | |
176 | LineTo(hdcMem, 0, nCheckHeight); | |
177 | ::SelectObject(hdcMem, hPenOld); | |
178 | ::SelectObject(hdcMem, hBrushOld); | |
179 | ::DeleteObject(blackPen); | |
180 | #endif | |
181 | ||
182 | // finally copy it to screen DC and clean up | |
45187197 | 183 | BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight, |
2bda0e17 KB |
184 | hdcMem, 0, 0, SRCCOPY); |
185 | ||
186 | SelectObject(hdcMem, hbmpOld); | |
187 | DeleteObject(hbmpCheck); | |
188 | DeleteDC(hdcMem); | |
189 | } | |
190 | ||
191 | // now we draw the smaller rectangle | |
192 | y++; | |
193 | nCheckWidth -= 2; | |
194 | nCheckHeight -= 2; | |
195 | ||
196 | // draw hollow gray rectangle | |
197 | (void)SelectObject(hdc, hpenGray); | |
45187197 | 198 | HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH)); |
2bda0e17 KB |
199 | Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight); |
200 | ||
201 | // clean up | |
202 | (void)SelectObject(hdc, hpenPrev); | |
203 | (void)SelectObject(hdc, hbrPrev); | |
204 | ||
205 | DeleteObject(hpenBack); | |
206 | DeleteObject(hpenGray); | |
207 | ||
208 | /* | |
209 | dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight); | |
210 | ||
211 | if ( IsChecked() ) { | |
212 | dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight); | |
213 | dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y); | |
214 | } | |
215 | */ | |
216 | ||
217 | return TRUE; | |
218 | } | |
219 | ||
220 | return FALSE; | |
221 | } | |
222 | ||
223 | // change the state of the item and redraw it | |
dd3c394a | 224 | void wxCheckListBoxItem::Check(bool check) |
45187197 | 225 | { |
dd3c394a | 226 | m_bChecked = check; |
2bda0e17 | 227 | |
e2ca995f | 228 | // index may be changed because new items were added/deleted |
dd3c394a VZ |
229 | if ( m_pParent->GetItemIndex(this) != (int)m_nIndex ) |
230 | { | |
231 | // update it | |
232 | int index = m_pParent->GetItemIndex(this); | |
233 | ||
223d09f6 | 234 | wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") ); |
dd3c394a VZ |
235 | |
236 | m_nIndex = (size_t)index; | |
237 | } | |
2bda0e17 | 238 | |
c4dbfe14 VZ |
239 | HWND hwndListbox = (HWND)m_pParent->GetHWND(); |
240 | ||
241 | #ifdef __WIN32__ | |
242 | RECT rcUpdate; | |
0c35333e | 243 | |
c4dbfe14 VZ |
244 | if ( ::SendMessage(hwndListbox, LB_GETITEMRECT, |
245 | m_nIndex, (LPARAM)&rcUpdate) == LB_ERR ) | |
246 | { | |
223d09f6 | 247 | wxLogDebug(wxT("LB_GETITEMRECT failed")); |
c4dbfe14 VZ |
248 | } |
249 | #else // Win16 | |
250 | // FIXME this doesn't work if the listbox is scrolled! | |
251 | size_t nHeight = m_pParent->GetItemHeight(); | |
252 | size_t y = m_nIndex * nHeight; | |
197dd9af PA |
253 | RECT rcUpdate ; |
254 | rcUpdate.left = 0 ; | |
255 | rcUpdate.top = y ; | |
256 | rcUpdate.right = GetDefaultMarginWidth() ; | |
257 | rcUpdate.bottom = y + nHeight ; | |
c4dbfe14 VZ |
258 | #endif // Win32/16 |
259 | ||
260 | InvalidateRect(hwndListbox, &rcUpdate, FALSE); | |
e2ca995f | 261 | } |
0c35333e | 262 | |
e2ca995f VZ |
263 | // send an "item checked" event |
264 | void wxCheckListBoxItem::SendEvent() | |
265 | { | |
dd3c394a VZ |
266 | wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId()); |
267 | event.SetInt(m_nIndex); | |
268 | event.SetEventObject(m_pParent); | |
269 | m_pParent->ProcessCommand(event); | |
2bda0e17 KB |
270 | } |
271 | ||
272 | // ---------------------------------------------------------------------------- | |
273 | // implementation of wxCheckListBox class | |
274 | // ---------------------------------------------------------------------------- | |
275 | ||
276 | // define event table | |
277 | // ------------------ | |
278 | BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) | |
d90879fa | 279 | EVT_KEY_DOWN(wxCheckListBox::OnKeyDown) |
2bda0e17 KB |
280 | EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick) |
281 | END_EVENT_TABLE() | |
282 | ||
283 | // control creation | |
284 | // ---------------- | |
285 | ||
286 | // def ctor: use Create() to really create the control | |
d90879fa | 287 | wxCheckListBox::wxCheckListBox() |
2bda0e17 KB |
288 | { |
289 | } | |
290 | ||
291 | // ctor which creates the associated control | |
debe6624 | 292 | wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id, |
2bda0e17 | 293 | const wxPoint& pos, const wxSize& size, |
debe6624 JS |
294 | int nStrings, const wxString choices[], |
295 | long style, const wxValidator& val, | |
dd3c394a | 296 | const wxString& name) |
2bda0e17 | 297 | { |
799cea00 | 298 | Create(parent, id, pos, size, nStrings, choices, style, val, name); |
dd3c394a VZ |
299 | } |
300 | ||
799cea00 VS |
301 | bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id, |
302 | const wxPoint& pos, const wxSize& size, | |
303 | int n, const wxString choices[], | |
304 | long style, | |
305 | const wxValidator& validator, const wxString& name) | |
306 | { | |
307 | return wxListBox::Create(parent, id, pos, size, n, choices, | |
308 | style | wxLB_OWNERDRAW, validator, name); | |
309 | } | |
799cea00 | 310 | |
e2ca995f VZ |
311 | // misc overloaded methods |
312 | // ----------------------- | |
799cea00 | 313 | |
dd3c394a VZ |
314 | void wxCheckListBox::Delete(int N) |
315 | { | |
316 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
223d09f6 | 317 | wxT("invalid index in wxListBox::Delete") ); |
dd3c394a VZ |
318 | |
319 | wxListBox::Delete(N); | |
320 | ||
321 | // free memory | |
322 | delete m_aItems[N]; | |
323 | ||
b54e41c5 | 324 | m_aItems.RemoveAt(N); |
dd3c394a VZ |
325 | } |
326 | ||
0c35333e RD |
327 | bool wxCheckListBox::SetFont( const wxFont &font ) |
328 | { | |
329 | size_t i; | |
07cf98cb | 330 | for ( i = 0; i < m_aItems.GetCount(); i++ ) |
0c35333e | 331 | m_aItems[i]->SetFont(font); |
07cf98cb | 332 | |
0c35333e | 333 | wxListBox::SetFont(font); |
07cf98cb | 334 | |
0c35333e RD |
335 | return TRUE; |
336 | } | |
337 | ||
2bda0e17 KB |
338 | // create/retrieve item |
339 | // -------------------- | |
340 | ||
341 | // create a check list box item | |
2b5f62a0 | 342 | wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex) |
2bda0e17 KB |
343 | { |
344 | wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex); | |
2bda0e17 KB |
345 | return pItem; |
346 | } | |
347 | ||
2bda0e17 KB |
348 | // return item size |
349 | // ---------------- | |
350 | bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) | |
351 | { | |
352 | if ( wxListBox::MSWOnMeasure(item) ) { | |
353 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; | |
354 | ||
355 | // save item height | |
356 | m_nItemHeight = pStruct->itemHeight; | |
357 | ||
358 | // add place for the check mark | |
359 | pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth(); | |
360 | ||
361 | return TRUE; | |
362 | } | |
363 | ||
364 | return FALSE; | |
365 | } | |
366 | ||
367 | // check items | |
368 | // ----------- | |
369 | ||
c86f1403 | 370 | bool wxCheckListBox::IsChecked(size_t uiIndex) const |
2bda0e17 | 371 | { |
d90879fa VZ |
372 | wxCHECK_MSG( uiIndex < (size_t)GetCount(), FALSE, _T("bad wxCheckListBox index") ); |
373 | ||
374 | return GetItem(uiIndex)->IsChecked(); | |
2bda0e17 KB |
375 | } |
376 | ||
c86f1403 | 377 | void wxCheckListBox::Check(size_t uiIndex, bool bCheck) |
2bda0e17 | 378 | { |
d90879fa VZ |
379 | wxCHECK_RET( uiIndex < (size_t)GetCount(), _T("bad wxCheckListBox index") ); |
380 | ||
381 | GetItem(uiIndex)->Check(bCheck); | |
2bda0e17 KB |
382 | } |
383 | ||
384 | // process events | |
385 | // -------------- | |
386 | ||
d90879fa | 387 | void wxCheckListBox::OnKeyDown(wxKeyEvent& event) |
2bda0e17 | 388 | { |
d90879fa VZ |
389 | // what do we do? |
390 | enum | |
391 | { | |
392 | None, | |
393 | Toggle, | |
394 | Set, | |
395 | Clear | |
396 | } oper; | |
397 | ||
77e00fe9 | 398 | switch ( event.GetKeyCode() ) |
d90879fa VZ |
399 | { |
400 | case WXK_SPACE: | |
401 | oper = Toggle; | |
402 | break; | |
403 | ||
404 | case WXK_NUMPAD_ADD: | |
405 | case '+': | |
406 | oper = Set; | |
407 | break; | |
408 | ||
409 | case WXK_NUMPAD_SUBTRACT: | |
410 | case '-': | |
411 | oper = Clear; | |
412 | break; | |
413 | ||
414 | default: | |
415 | oper = None; | |
416 | } | |
417 | ||
418 | if ( oper != None ) | |
419 | { | |
420 | wxArrayInt selections; | |
a8f473ac | 421 | int count = 0; |
d90879fa VZ |
422 | if ( HasMultipleSelection() ) |
423 | { | |
424 | count = GetSelections(selections); | |
425 | } | |
426 | else | |
427 | { | |
a8f473ac RD |
428 | int sel = GetSelection(); |
429 | if (sel != -1) | |
430 | { | |
431 | count = 1; | |
432 | selections.Add(sel); | |
433 | } | |
d90879fa VZ |
434 | } |
435 | ||
436 | for ( int i = 0; i < count; i++ ) | |
437 | { | |
438 | wxCheckListBoxItem *item = GetItem(selections[i]); | |
439 | if ( !item ) | |
440 | { | |
441 | wxFAIL_MSG( _T("no wxCheckListBoxItem?") ); | |
442 | continue; | |
443 | } | |
444 | ||
445 | switch ( oper ) | |
446 | { | |
447 | case Toggle: | |
448 | item->Toggle(); | |
449 | break; | |
450 | ||
451 | case Set: | |
452 | case Clear: | |
453 | item->Check( oper == Set ); | |
454 | break; | |
455 | ||
456 | default: | |
457 | wxFAIL_MSG( _T("what should this key do?") ); | |
458 | } | |
e2ca995f VZ |
459 | |
460 | // we should send an event as this has been done by the user and | |
461 | // not by the program | |
462 | item->SendEvent(); | |
d90879fa VZ |
463 | } |
464 | } | |
465 | else // nothing to do | |
466 | { | |
467 | event.Skip(); | |
468 | } | |
2bda0e17 KB |
469 | } |
470 | ||
471 | void wxCheckListBox::OnLeftClick(wxMouseEvent& event) | |
472 | { | |
473 | // clicking on the item selects it, clicking on the checkmark toggles | |
c1888b05 | 474 | if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) { |
15c3723c | 475 | int nItem = HitTest(event.GetX(), event.GetY()); |
c4dbfe14 | 476 | |
e2ca995f VZ |
477 | if ( nItem != wxNOT_FOUND ) { |
478 | wxCheckListBoxItem *item = GetItem(nItem); | |
479 | item->Toggle(); | |
480 | item->SendEvent(); | |
481 | } | |
2bda0e17 KB |
482 | //else: it's not an error, just click outside of client zone |
483 | } | |
484 | else { | |
485 | // implement default behaviour: clicking on the item selects it | |
486 | event.Skip(); | |
487 | } | |
488 | } | |
1c089c47 | 489 | |
15c3723c VZ |
490 | int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const |
491 | { | |
492 | #ifdef __WIN32__ | |
493 | int nItem = (int)::SendMessage | |
494 | ( | |
495 | (HWND)GetHWND(), | |
496 | LB_ITEMFROMPOINT, | |
497 | 0, | |
498 | MAKELPARAM(x, y) | |
499 | ); | |
500 | #else // Win16 | |
501 | // FIXME this doesn't work when the listbox is scrolled! | |
502 | int nItem = y / m_nItemHeight; | |
503 | #endif // Win32/16 | |
504 | ||
505 | return nItem >= m_noItems ? wxNOT_FOUND : nItem; | |
506 | } | |
507 | ||
1c089c47 JS |
508 | #endif |
509 |