]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/checklst.cpp | |
3 | // Purpose: implementation of wxCheckListBox class | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 16.11.97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN | |
28 | ||
29 | #include "wx/checklst.h" | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/msw/wrapwin.h" | |
33 | #include "wx/object.h" | |
34 | #include "wx/colour.h" | |
35 | #include "wx/font.h" | |
36 | #include "wx/bitmap.h" | |
37 | #include "wx/window.h" | |
38 | #include "wx/listbox.h" | |
39 | #include "wx/dcmemory.h" | |
40 | #include "wx/settings.h" | |
41 | #include "wx/log.h" | |
42 | #endif | |
43 | ||
44 | #include "wx/ownerdrw.h" | |
45 | ||
46 | #include <windowsx.h> | |
47 | ||
48 | #include "wx/msw/private.h" | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // private functions | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | // get item (converted to right type) | |
55 | #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n))) | |
56 | ||
57 | // ============================================================================ | |
58 | // implementation | |
59 | // ============================================================================ | |
60 | ||
61 | ||
62 | #if wxUSE_EXTENDED_RTTI | |
63 | WX_DEFINE_FLAGS( wxCheckListBoxStyle ) | |
64 | ||
65 | wxBEGIN_FLAGS( wxCheckListBoxStyle ) | |
66 | // new style border flags, we put them first to | |
67 | // use them for streaming out | |
68 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
69 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
70 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
71 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
72 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
73 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
74 | ||
75 | // old style border flags | |
76 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
77 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
78 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
79 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
80 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
81 | wxFLAGS_MEMBER(wxBORDER) | |
82 | ||
83 | // standard window styles | |
84 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
85 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
86 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
87 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
88 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) | |
89 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) | |
90 | wxFLAGS_MEMBER(wxVSCROLL) | |
91 | wxFLAGS_MEMBER(wxHSCROLL) | |
92 | ||
93 | wxFLAGS_MEMBER(wxLB_SINGLE) | |
94 | wxFLAGS_MEMBER(wxLB_MULTIPLE) | |
95 | wxFLAGS_MEMBER(wxLB_EXTENDED) | |
96 | wxFLAGS_MEMBER(wxLB_HSCROLL) | |
97 | wxFLAGS_MEMBER(wxLB_ALWAYS_SB) | |
98 | wxFLAGS_MEMBER(wxLB_NEEDED_SB) | |
99 | wxFLAGS_MEMBER(wxLB_SORT) | |
100 | wxFLAGS_MEMBER(wxLB_OWNERDRAW) | |
101 | ||
102 | wxEND_FLAGS( wxCheckListBoxStyle ) | |
103 | ||
104 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox, wxListBox,"wx/checklst.h") | |
105 | ||
106 | wxBEGIN_PROPERTIES_TABLE(wxCheckListBox) | |
107 | wxEVENT_PROPERTY( Toggle , wxEVT_COMMAND_CHECKLISTBOX_TOGGLED , wxCommandEvent ) | |
108 | wxPROPERTY_FLAGS( WindowStyle , wxCheckListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , wxLB_OWNERDRAW /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
109 | wxEND_PROPERTIES_TABLE() | |
110 | ||
111 | wxBEGIN_HANDLERS_TABLE(wxCheckListBox) | |
112 | wxEND_HANDLERS_TABLE() | |
113 | ||
114 | wxCONSTRUCTOR_4( wxCheckListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size ) | |
115 | ||
116 | #else | |
117 | IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox) | |
118 | #endif | |
119 | ||
120 | // ---------------------------------------------------------------------------- | |
121 | // declaration and implementation of wxCheckListBoxItem class | |
122 | // ---------------------------------------------------------------------------- | |
123 | ||
124 | class wxCheckListBoxItem : public wxOwnerDrawn | |
125 | { | |
126 | friend class WXDLLEXPORT wxCheckListBox; | |
127 | public: | |
128 | // ctor | |
129 | wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex); | |
130 | ||
131 | // drawing functions | |
132 | virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat); | |
133 | ||
134 | // simple accessors and operations | |
135 | bool IsChecked() const { return m_bChecked; } | |
136 | ||
137 | void Check(bool bCheck); | |
138 | void Toggle() { Check(!IsChecked()); } | |
139 | ||
140 | void SendEvent(); | |
141 | ||
142 | private: | |
143 | bool m_bChecked; | |
144 | wxCheckListBox *m_pParent; | |
145 | size_t m_nIndex; | |
146 | ||
147 | DECLARE_NO_COPY_CLASS(wxCheckListBoxItem) | |
148 | }; | |
149 | ||
150 | wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex) | |
151 | : wxOwnerDrawn(wxEmptyString, true) // checkable | |
152 | { | |
153 | m_bChecked = false; | |
154 | m_pParent = pParent; | |
155 | m_nIndex = nIndex; | |
156 | ||
157 | // we don't initialize m_nCheckHeight/Width vars because it's | |
158 | // done in OnMeasure while they are used only in OnDraw and we | |
159 | // know that there will always be OnMeasure before OnDraw | |
160 | ||
161 | // fix appearance for check list boxes: they don't look quite the same as | |
162 | // menu icons | |
163 | SetMarginWidth(::GetSystemMetrics(SM_CXMENUCHECK) - | |
164 | 2*wxSystemSettings::GetMetric(wxSYS_EDGE_X) + 1); | |
165 | SetBackgroundColour(pParent->GetBackgroundColour()); | |
166 | } | |
167 | ||
168 | bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc, | |
169 | wxODAction act, wxODStatus stat) | |
170 | { | |
171 | // first draw the label | |
172 | if ( IsChecked() ) | |
173 | stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked); | |
174 | ||
175 | if ( !wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) | |
176 | return false; | |
177 | ||
178 | ||
179 | // now draw the check mark part | |
180 | size_t nCheckWidth = GetDefaultMarginWidth(), | |
181 | nCheckHeight = m_pParent->GetItemHeight(); | |
182 | ||
183 | int x = rc.GetX(), | |
184 | y = rc.GetY(); | |
185 | ||
186 | HDC hdc = (HDC)dc.GetHDC(); | |
187 | ||
188 | // create pens, brushes &c | |
189 | COLORREF colBg = ::GetSysColor(COLOR_WINDOW); | |
190 | AutoHPEN hpenBack(colBg), | |
191 | hpenGray(RGB(0xc0, 0xc0, 0xc0)); | |
192 | ||
193 | SelectInHDC selPen(hdc, (HGDIOBJ)hpenBack); | |
194 | AutoHBRUSH hbrBack(colBg); | |
195 | SelectInHDC selBrush(hdc, hbrBack); | |
196 | ||
197 | // erase the background: it could have been filled with the selected colour | |
198 | Rectangle(hdc, x, y, x + nCheckWidth + 1, rc.GetBottom() + 1); | |
199 | ||
200 | // shift check mark 1 pixel to the right, looks better like this | |
201 | x++; | |
202 | ||
203 | if ( IsChecked() ) | |
204 | { | |
205 | // first create a monochrome bitmap in a memory DC | |
206 | MemoryHDC hdcMem(hdc); | |
207 | MonoBitmap hbmpCheck(nCheckWidth, nCheckHeight); | |
208 | SelectInHDC selBmp(hdcMem, hbmpCheck); | |
209 | ||
210 | // then draw a check mark into it | |
211 | RECT rect = { 0, 0, nCheckWidth, nCheckHeight }; | |
212 | ::DrawFrameControl(hdcMem, &rect, | |
213 | #ifdef __WXWINCE__ | |
214 | DFC_BUTTON, DFCS_BUTTONCHECK | |
215 | #else | |
216 | DFC_MENU, DFCS_MENUCHECK | |
217 | #endif | |
218 | ); | |
219 | ||
220 | // finally copy it to screen DC | |
221 | ::BitBlt(hdc, x, y, nCheckWidth, nCheckHeight, hdcMem, 0, 0, SRCCOPY); | |
222 | } | |
223 | ||
224 | // now we draw the smaller rectangle | |
225 | y++; | |
226 | nCheckWidth -= 2; | |
227 | nCheckHeight -= 2; | |
228 | ||
229 | // draw hollow gray rectangle | |
230 | (void)::SelectObject(hdc, (HGDIOBJ)hpenGray); | |
231 | ||
232 | SelectInHDC selBrush2(hdc, ::GetStockObject(NULL_BRUSH)); | |
233 | Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight); | |
234 | ||
235 | return true; | |
236 | } | |
237 | ||
238 | // change the state of the item and redraw it | |
239 | void wxCheckListBoxItem::Check(bool check) | |
240 | { | |
241 | m_bChecked = check; | |
242 | ||
243 | // index may be changed because new items were added/deleted | |
244 | if ( m_pParent->GetItemIndex(this) != (int)m_nIndex ) | |
245 | { | |
246 | // update it | |
247 | int index = m_pParent->GetItemIndex(this); | |
248 | ||
249 | wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") ); | |
250 | ||
251 | m_nIndex = (size_t)index; | |
252 | } | |
253 | ||
254 | HWND hwndListbox = (HWND)m_pParent->GetHWND(); | |
255 | ||
256 | RECT rcUpdate; | |
257 | ||
258 | if ( ::SendMessage(hwndListbox, LB_GETITEMRECT, | |
259 | m_nIndex, (LPARAM)&rcUpdate) == LB_ERR ) | |
260 | { | |
261 | wxLogDebug(wxT("LB_GETITEMRECT failed")); | |
262 | } | |
263 | ||
264 | ::InvalidateRect(hwndListbox, &rcUpdate, FALSE); | |
265 | } | |
266 | ||
267 | // send an "item checked" event | |
268 | void wxCheckListBoxItem::SendEvent() | |
269 | { | |
270 | wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId()); | |
271 | event.SetInt(m_nIndex); | |
272 | event.SetEventObject(m_pParent); | |
273 | m_pParent->ProcessCommand(event); | |
274 | } | |
275 | ||
276 | // ---------------------------------------------------------------------------- | |
277 | // implementation of wxCheckListBox class | |
278 | // ---------------------------------------------------------------------------- | |
279 | ||
280 | // define event table | |
281 | // ------------------ | |
282 | BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) | |
283 | EVT_KEY_DOWN(wxCheckListBox::OnKeyDown) | |
284 | EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick) | |
285 | END_EVENT_TABLE() | |
286 | ||
287 | // control creation | |
288 | // ---------------- | |
289 | ||
290 | // def ctor: use Create() to really create the control | |
291 | wxCheckListBox::wxCheckListBox() | |
292 | { | |
293 | } | |
294 | ||
295 | // ctor which creates the associated control | |
296 | wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id, | |
297 | const wxPoint& pos, const wxSize& size, | |
298 | int nStrings, const wxString choices[], | |
299 | long style, const wxValidator& val, | |
300 | const wxString& name) | |
301 | { | |
302 | Create(parent, id, pos, size, nStrings, choices, style, val, name); | |
303 | } | |
304 | ||
305 | wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id, | |
306 | const wxPoint& pos, const wxSize& size, | |
307 | const wxArrayString& choices, | |
308 | long style, const wxValidator& val, | |
309 | const wxString& name) | |
310 | { | |
311 | Create(parent, id, pos, size, choices, style, val, name); | |
312 | } | |
313 | ||
314 | bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id, | |
315 | const wxPoint& pos, const wxSize& size, | |
316 | int n, const wxString choices[], | |
317 | long style, | |
318 | const wxValidator& validator, const wxString& name) | |
319 | { | |
320 | return wxListBox::Create(parent, id, pos, size, n, choices, | |
321 | style | wxLB_OWNERDRAW, validator, name); | |
322 | } | |
323 | ||
324 | bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id, | |
325 | const wxPoint& pos, const wxSize& size, | |
326 | const wxArrayString& choices, | |
327 | long style, | |
328 | const wxValidator& validator, const wxString& name) | |
329 | { | |
330 | return wxListBox::Create(parent, id, pos, size, choices, | |
331 | style | wxLB_OWNERDRAW, validator, name); | |
332 | } | |
333 | ||
334 | // misc overloaded methods | |
335 | // ----------------------- | |
336 | ||
337 | void wxCheckListBox::Delete(unsigned int n) | |
338 | { | |
339 | wxCHECK_RET( IsValid(n), | |
340 | wxT("invalid index in wxListBox::Delete") ); | |
341 | ||
342 | wxListBox::Delete(n); | |
343 | ||
344 | // free memory | |
345 | delete m_aItems[n]; | |
346 | ||
347 | m_aItems.RemoveAt(n); | |
348 | } | |
349 | ||
350 | bool wxCheckListBox::SetFont( const wxFont &font ) | |
351 | { | |
352 | unsigned int i; | |
353 | for ( i = 0; i < m_aItems.GetCount(); i++ ) | |
354 | m_aItems[i]->SetFont(font); | |
355 | ||
356 | wxListBox::SetFont(font); | |
357 | ||
358 | return true; | |
359 | } | |
360 | ||
361 | // create/retrieve item | |
362 | // -------------------- | |
363 | ||
364 | // create a check list box item | |
365 | wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex) | |
366 | { | |
367 | wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex); | |
368 | return pItem; | |
369 | } | |
370 | ||
371 | // return item size | |
372 | // ---------------- | |
373 | bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) | |
374 | { | |
375 | if ( wxListBox::MSWOnMeasure(item) ) { | |
376 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; | |
377 | ||
378 | // save item height | |
379 | m_nItemHeight = pStruct->itemHeight; | |
380 | ||
381 | // add place for the check mark | |
382 | pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth(); | |
383 | ||
384 | return true; | |
385 | } | |
386 | ||
387 | return false; | |
388 | } | |
389 | ||
390 | // check items | |
391 | // ----------- | |
392 | ||
393 | bool wxCheckListBox::IsChecked(unsigned int uiIndex) const | |
394 | { | |
395 | wxCHECK_MSG( IsValid(uiIndex), false, _T("bad wxCheckListBox index") ); | |
396 | ||
397 | return GetItem(uiIndex)->IsChecked(); | |
398 | } | |
399 | ||
400 | void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck) | |
401 | { | |
402 | wxCHECK_RET( IsValid(uiIndex), _T("bad wxCheckListBox index") ); | |
403 | ||
404 | GetItem(uiIndex)->Check(bCheck); | |
405 | } | |
406 | ||
407 | // process events | |
408 | // -------------- | |
409 | ||
410 | void wxCheckListBox::OnKeyDown(wxKeyEvent& event) | |
411 | { | |
412 | // what do we do? | |
413 | enum | |
414 | { | |
415 | None, | |
416 | Toggle, | |
417 | Set, | |
418 | Clear | |
419 | } oper; | |
420 | ||
421 | switch ( event.GetKeyCode() ) | |
422 | { | |
423 | case WXK_SPACE: | |
424 | oper = Toggle; | |
425 | break; | |
426 | ||
427 | case WXK_NUMPAD_ADD: | |
428 | case '+': | |
429 | oper = Set; | |
430 | break; | |
431 | ||
432 | case WXK_NUMPAD_SUBTRACT: | |
433 | case '-': | |
434 | oper = Clear; | |
435 | break; | |
436 | ||
437 | default: | |
438 | oper = None; | |
439 | } | |
440 | ||
441 | if ( oper != None ) | |
442 | { | |
443 | wxArrayInt selections; | |
444 | int count = 0; | |
445 | if ( HasMultipleSelection() ) | |
446 | { | |
447 | count = GetSelections(selections); | |
448 | } | |
449 | else | |
450 | { | |
451 | int sel = GetSelection(); | |
452 | if (sel != -1) | |
453 | { | |
454 | count = 1; | |
455 | selections.Add(sel); | |
456 | } | |
457 | } | |
458 | ||
459 | for ( int i = 0; i < count; i++ ) | |
460 | { | |
461 | wxCheckListBoxItem *item = GetItem(selections[i]); | |
462 | if ( !item ) | |
463 | { | |
464 | wxFAIL_MSG( _T("no wxCheckListBoxItem?") ); | |
465 | continue; | |
466 | } | |
467 | ||
468 | switch ( oper ) | |
469 | { | |
470 | case Toggle: | |
471 | item->Toggle(); | |
472 | break; | |
473 | ||
474 | case Set: | |
475 | case Clear: | |
476 | item->Check( oper == Set ); | |
477 | break; | |
478 | ||
479 | default: | |
480 | wxFAIL_MSG( _T("what should this key do?") ); | |
481 | } | |
482 | ||
483 | // we should send an event as this has been done by the user and | |
484 | // not by the program | |
485 | item->SendEvent(); | |
486 | } | |
487 | } | |
488 | else // nothing to do | |
489 | { | |
490 | event.Skip(); | |
491 | } | |
492 | } | |
493 | ||
494 | void wxCheckListBox::OnLeftClick(wxMouseEvent& event) | |
495 | { | |
496 | // clicking on the item selects it, clicking on the checkmark toggles | |
497 | if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) | |
498 | { | |
499 | int nItem = HitTest(event.GetX(), event.GetY()); | |
500 | ||
501 | if ( nItem != wxNOT_FOUND ) | |
502 | { | |
503 | wxCheckListBoxItem *item = GetItem(nItem); | |
504 | item->Toggle(); | |
505 | item->SendEvent(); | |
506 | } | |
507 | //else: it's not an error, just click outside of client zone | |
508 | } | |
509 | else | |
510 | { | |
511 | // implement default behaviour: clicking on the item selects it | |
512 | event.Skip(); | |
513 | } | |
514 | } | |
515 | ||
516 | int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const | |
517 | { | |
518 | int nItem = (int)::SendMessage | |
519 | ( | |
520 | (HWND)GetHWND(), | |
521 | LB_ITEMFROMPOINT, | |
522 | 0, | |
523 | MAKELPARAM(x, y) | |
524 | ); | |
525 | ||
526 | return nItem >= (int)m_noItems ? wxNOT_FOUND : nItem; | |
527 | } | |
528 | ||
529 | ||
530 | wxSize wxCheckListBox::DoGetBestSize() const | |
531 | { | |
532 | wxSize best = wxListBox::DoGetBestSize(); | |
533 | best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox | |
534 | CacheBestSize(best); | |
535 | return best; | |
536 | } | |
537 | ||
538 | #endif |