]>
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/wrapcctl.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/renderer.h" | |
49 | #include "wx/msw/private.h" | |
50 | #include "wx/msw/dc.h" | |
51 | ||
52 | // ---------------------------------------------------------------------------- | |
53 | // private functions | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
56 | // get item (converted to right type) | |
57 | #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n))) | |
58 | ||
59 | // ============================================================================ | |
60 | // implementation | |
61 | // ============================================================================ | |
62 | ||
63 | ||
64 | #if wxUSE_EXTENDED_RTTI | |
65 | WX_DEFINE_FLAGS( wxCheckListBoxStyle ) | |
66 | ||
67 | wxBEGIN_FLAGS( wxCheckListBoxStyle ) | |
68 | // new style border flags, we put them first to | |
69 | // use them for streaming out | |
70 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) | |
71 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
72 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
73 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
74 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
75 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
76 | ||
77 | // old style border flags | |
78 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) | |
79 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
80 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
81 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
82 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
83 | wxFLAGS_MEMBER(wxBORDER) | |
84 | ||
85 | // standard window styles | |
86 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) | |
87 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
88 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
89 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
90 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) | |
91 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) | |
92 | wxFLAGS_MEMBER(wxVSCROLL) | |
93 | wxFLAGS_MEMBER(wxHSCROLL) | |
94 | ||
95 | wxFLAGS_MEMBER(wxLB_SINGLE) | |
96 | wxFLAGS_MEMBER(wxLB_MULTIPLE) | |
97 | wxFLAGS_MEMBER(wxLB_EXTENDED) | |
98 | wxFLAGS_MEMBER(wxLB_HSCROLL) | |
99 | wxFLAGS_MEMBER(wxLB_ALWAYS_SB) | |
100 | wxFLAGS_MEMBER(wxLB_NEEDED_SB) | |
101 | wxFLAGS_MEMBER(wxLB_SORT) | |
102 | wxFLAGS_MEMBER(wxLB_OWNERDRAW) | |
103 | ||
104 | wxEND_FLAGS( wxCheckListBoxStyle ) | |
105 | ||
106 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox, wxListBox,"wx/checklst.h") | |
107 | ||
108 | wxBEGIN_PROPERTIES_TABLE(wxCheckListBox) | |
109 | wxEVENT_PROPERTY( Toggle , wxEVT_COMMAND_CHECKLISTBOX_TOGGLED , wxCommandEvent ) | |
110 | wxPROPERTY_FLAGS( WindowStyle , wxCheckListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , wxLB_OWNERDRAW /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
111 | wxEND_PROPERTIES_TABLE() | |
112 | ||
113 | wxBEGIN_HANDLERS_TABLE(wxCheckListBox) | |
114 | wxEND_HANDLERS_TABLE() | |
115 | ||
116 | wxCONSTRUCTOR_4( wxCheckListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size ) | |
117 | ||
118 | #else | |
119 | IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox) | |
120 | #endif | |
121 | ||
122 | // ---------------------------------------------------------------------------- | |
123 | // declaration and implementation of wxCheckListBoxItem class | |
124 | // ---------------------------------------------------------------------------- | |
125 | ||
126 | class wxCheckListBoxItem : public wxOwnerDrawn | |
127 | { | |
128 | friend class WXDLLIMPEXP_FWD_CORE wxCheckListBox; | |
129 | public: | |
130 | // ctor | |
131 | wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex); | |
132 | ||
133 | // drawing functions | |
134 | virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat); | |
135 | ||
136 | // simple accessors and operations | |
137 | bool IsChecked() const { return m_bChecked; } | |
138 | ||
139 | void Check(bool bCheck); | |
140 | void Toggle() { Check(!IsChecked()); } | |
141 | ||
142 | void SendEvent(); | |
143 | ||
144 | private: | |
145 | bool m_bChecked; | |
146 | wxCheckListBox *m_pParent; | |
147 | size_t m_nIndex; | |
148 | ||
149 | wxDECLARE_NO_COPY_CLASS(wxCheckListBoxItem); | |
150 | }; | |
151 | ||
152 | wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex) | |
153 | : wxOwnerDrawn(wxEmptyString, true) // checkable | |
154 | { | |
155 | m_bChecked = false; | |
156 | m_pParent = pParent; | |
157 | m_nIndex = nIndex; | |
158 | ||
159 | // we don't initialize m_nCheckHeight/Width vars because it's | |
160 | // done in OnMeasure while they are used only in OnDraw and we | |
161 | // know that there will always be OnMeasure before OnDraw | |
162 | ||
163 | SetMarginWidth(::GetSystemMetrics(SM_CXMENUCHECK) - 2); | |
164 | ||
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 | wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl(); | |
179 | // now draw the check mark part | |
180 | HDC hdc = GetHdcOf(*impl); | |
181 | ||
182 | int nBmpWidth = ::GetSystemMetrics(SM_CXMENUCHECK), | |
183 | nBmpHeight = ::GetSystemMetrics(SM_CYMENUCHECK); | |
184 | ||
185 | ||
186 | // first create bitmap in a memory DC | |
187 | MemoryHDC hdcMem(hdc); | |
188 | CompatibleBitmap hBmpCheck(hdc, nBmpWidth, nBmpHeight); | |
189 | ||
190 | // then draw a check mark into it | |
191 | { | |
192 | SelectInHDC selBmp(hdcMem, hBmpCheck); | |
193 | ||
194 | int flags = wxCONTROL_FLAT; | |
195 | if ( IsChecked() ) | |
196 | flags |= wxCONTROL_CHECKED; | |
197 | ||
198 | wxDCTemp dcMem(hdcMem); | |
199 | wxRendererNative::Get().DrawCheckBox( | |
200 | m_pParent, dcMem, wxRect(0, 0, nBmpWidth, nBmpHeight), flags); | |
201 | } // select hBmpCheck out of hdcMem | |
202 | ||
203 | // shift check mark 2 pixel to the right and bottom, looks better like this | |
204 | int x = rc.GetX() + 2, | |
205 | y = rc.GetY() + 2; | |
206 | ||
207 | // finally draw bitmap to screen: uses image list functions to blend | |
208 | // the bitmap with the background colour (better for the selected items) | |
209 | HIMAGELIST himl = ImageList_Create(nBmpWidth, nBmpHeight, | |
210 | ILC_COLOR32 | ILC_MASK, 1, 1); | |
211 | ImageList_Add(himl, hBmpCheck, NULL); | |
212 | ||
213 | UINT fStyle = stat & wxOwnerDrawn::wxODSelected ? ILD_SELECTED : ILD_NORMAL; | |
214 | ImageList_Draw(himl, 0, hdc, x, y, fStyle); | |
215 | ||
216 | ImageList_Destroy(himl); | |
217 | ||
218 | if (stat & wxODHasFocus) | |
219 | wxRendererNative::Get().DrawFocusRect(m_pParent, dc, rc); | |
220 | ||
221 | return true; | |
222 | } | |
223 | ||
224 | // change the state of the item and redraw it | |
225 | void wxCheckListBoxItem::Check(bool check) | |
226 | { | |
227 | m_bChecked = check; | |
228 | ||
229 | // index may be changed because new items were added/deleted | |
230 | if ( m_pParent->GetItemIndex(this) != (int)m_nIndex ) | |
231 | { | |
232 | // update it | |
233 | int index = m_pParent->GetItemIndex(this); | |
234 | ||
235 | wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") ); | |
236 | ||
237 | m_nIndex = (size_t)index; | |
238 | } | |
239 | ||
240 | HWND hwndListbox = (HWND)m_pParent->GetHWND(); | |
241 | ||
242 | RECT rcUpdate; | |
243 | ||
244 | if ( ::SendMessage(hwndListbox, LB_GETITEMRECT, | |
245 | m_nIndex, (LPARAM)&rcUpdate) == LB_ERR ) | |
246 | { | |
247 | wxLogDebug(wxT("LB_GETITEMRECT failed")); | |
248 | } | |
249 | ||
250 | ::InvalidateRect(hwndListbox, &rcUpdate, FALSE); | |
251 | } | |
252 | ||
253 | // send an "item checked" event | |
254 | void wxCheckListBoxItem::SendEvent() | |
255 | { | |
256 | wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId()); | |
257 | event.SetInt(m_nIndex); | |
258 | event.SetEventObject(m_pParent); | |
259 | event.SetString(m_pParent->GetString(m_nIndex)); | |
260 | m_pParent->ProcessCommand(event); | |
261 | } | |
262 | ||
263 | // ---------------------------------------------------------------------------- | |
264 | // implementation of wxCheckListBox class | |
265 | // ---------------------------------------------------------------------------- | |
266 | ||
267 | // define event table | |
268 | // ------------------ | |
269 | BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) | |
270 | EVT_KEY_DOWN(wxCheckListBox::OnKeyDown) | |
271 | EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick) | |
272 | END_EVENT_TABLE() | |
273 | ||
274 | // control creation | |
275 | // ---------------- | |
276 | ||
277 | // def ctor: use Create() to really create the control | |
278 | wxCheckListBox::wxCheckListBox() | |
279 | { | |
280 | } | |
281 | ||
282 | // ctor which creates the associated control | |
283 | wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id, | |
284 | const wxPoint& pos, const wxSize& size, | |
285 | int nStrings, const wxString choices[], | |
286 | long style, const wxValidator& val, | |
287 | const wxString& name) | |
288 | { | |
289 | Create(parent, id, pos, size, nStrings, choices, style, val, name); | |
290 | } | |
291 | ||
292 | wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id, | |
293 | const wxPoint& pos, const wxSize& size, | |
294 | const wxArrayString& choices, | |
295 | long style, const wxValidator& val, | |
296 | const wxString& name) | |
297 | { | |
298 | Create(parent, id, pos, size, choices, style, val, name); | |
299 | } | |
300 | ||
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 | } | |
310 | ||
311 | bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id, | |
312 | const wxPoint& pos, const wxSize& size, | |
313 | const wxArrayString& choices, | |
314 | long style, | |
315 | const wxValidator& validator, const wxString& name) | |
316 | { | |
317 | return wxListBox::Create(parent, id, pos, size, choices, | |
318 | style | wxLB_OWNERDRAW, validator, name); | |
319 | } | |
320 | ||
321 | // misc overloaded methods | |
322 | // ----------------------- | |
323 | ||
324 | void wxCheckListBox::Delete(unsigned int n) | |
325 | { | |
326 | wxCHECK_RET( IsValid(n), | |
327 | wxT("invalid index in wxListBox::Delete") ); | |
328 | ||
329 | wxListBox::Delete(n); | |
330 | ||
331 | // free memory | |
332 | delete m_aItems[n]; | |
333 | ||
334 | m_aItems.RemoveAt(n); | |
335 | } | |
336 | ||
337 | bool wxCheckListBox::SetFont( const wxFont &font ) | |
338 | { | |
339 | unsigned int i; | |
340 | for ( i = 0; i < m_aItems.GetCount(); i++ ) | |
341 | m_aItems[i]->SetFont(font); | |
342 | ||
343 | wxListBox::SetFont(font); | |
344 | ||
345 | return true; | |
346 | } | |
347 | ||
348 | // create/retrieve item | |
349 | // -------------------- | |
350 | ||
351 | // create a check list box item | |
352 | wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex) | |
353 | { | |
354 | wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex); | |
355 | return pItem; | |
356 | } | |
357 | ||
358 | // return item size | |
359 | // ---------------- | |
360 | bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) | |
361 | { | |
362 | if ( wxListBox::MSWOnMeasure(item) ) { | |
363 | MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; | |
364 | ||
365 | // add place for the check mark | |
366 | pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth(); | |
367 | pStruct->itemHeight += 1; | |
368 | ||
369 | // save item height | |
370 | m_nItemHeight = pStruct->itemHeight; | |
371 | ||
372 | return true; | |
373 | } | |
374 | ||
375 | return false; | |
376 | } | |
377 | ||
378 | // check items | |
379 | // ----------- | |
380 | ||
381 | bool wxCheckListBox::IsChecked(unsigned int uiIndex) const | |
382 | { | |
383 | wxCHECK_MSG( IsValid(uiIndex), false, wxT("bad wxCheckListBox index") ); | |
384 | ||
385 | return GetItem(uiIndex)->IsChecked(); | |
386 | } | |
387 | ||
388 | void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck) | |
389 | { | |
390 | wxCHECK_RET( IsValid(uiIndex), wxT("bad wxCheckListBox index") ); | |
391 | ||
392 | GetItem(uiIndex)->Check(bCheck); | |
393 | } | |
394 | ||
395 | // process events | |
396 | // -------------- | |
397 | ||
398 | void wxCheckListBox::OnKeyDown(wxKeyEvent& event) | |
399 | { | |
400 | // what do we do? | |
401 | enum | |
402 | { | |
403 | None, | |
404 | Toggle, | |
405 | Set, | |
406 | Clear | |
407 | } oper; | |
408 | ||
409 | switch ( event.GetKeyCode() ) | |
410 | { | |
411 | case WXK_SPACE: | |
412 | oper = Toggle; | |
413 | break; | |
414 | ||
415 | case WXK_NUMPAD_ADD: | |
416 | case '+': | |
417 | oper = Set; | |
418 | break; | |
419 | ||
420 | case WXK_NUMPAD_SUBTRACT: | |
421 | case '-': | |
422 | oper = Clear; | |
423 | break; | |
424 | ||
425 | default: | |
426 | oper = None; | |
427 | } | |
428 | ||
429 | if ( oper != None ) | |
430 | { | |
431 | wxArrayInt selections; | |
432 | int count = 0; | |
433 | if ( HasMultipleSelection() ) | |
434 | { | |
435 | count = GetSelections(selections); | |
436 | } | |
437 | else | |
438 | { | |
439 | int sel = GetSelection(); | |
440 | if (sel != -1) | |
441 | { | |
442 | count = 1; | |
443 | selections.Add(sel); | |
444 | } | |
445 | } | |
446 | ||
447 | for ( int i = 0; i < count; i++ ) | |
448 | { | |
449 | wxCheckListBoxItem *item = GetItem(selections[i]); | |
450 | if ( !item ) | |
451 | { | |
452 | wxFAIL_MSG( wxT("no wxCheckListBoxItem?") ); | |
453 | continue; | |
454 | } | |
455 | ||
456 | switch ( oper ) | |
457 | { | |
458 | case Toggle: | |
459 | item->Toggle(); | |
460 | break; | |
461 | ||
462 | case Set: | |
463 | case Clear: | |
464 | item->Check( oper == Set ); | |
465 | break; | |
466 | ||
467 | default: | |
468 | wxFAIL_MSG( wxT("what should this key do?") ); | |
469 | } | |
470 | ||
471 | // we should send an event as this has been done by the user and | |
472 | // not by the program | |
473 | item->SendEvent(); | |
474 | } | |
475 | } | |
476 | else // nothing to do | |
477 | { | |
478 | event.Skip(); | |
479 | } | |
480 | } | |
481 | ||
482 | void wxCheckListBox::OnLeftClick(wxMouseEvent& event) | |
483 | { | |
484 | // clicking on the item selects it, clicking on the checkmark toggles | |
485 | if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) | |
486 | { | |
487 | int nItem = HitTest(event.GetX(), event.GetY()); | |
488 | ||
489 | if ( nItem != wxNOT_FOUND ) | |
490 | { | |
491 | // people expect to get "kill focus" event for the currently | |
492 | // focused control before getting events from the other controls | |
493 | // and, equally importantly, they may prevent the focus change from | |
494 | // taking place at all (e.g. because the old control contents is | |
495 | // invalid and needs to be corrected) in which case we shouldn't | |
496 | // generate this event at all | |
497 | SetFocus(); | |
498 | if ( FindFocus() == this ) | |
499 | { | |
500 | wxCheckListBoxItem *item = GetItem(nItem); | |
501 | item->Toggle(); | |
502 | item->SendEvent(); | |
503 | } | |
504 | } | |
505 | //else: it's not an error, just click outside of client zone | |
506 | } | |
507 | else | |
508 | { | |
509 | // implement default behaviour: clicking on the item selects it | |
510 | event.Skip(); | |
511 | } | |
512 | } | |
513 | ||
514 | wxSize wxCheckListBox::DoGetBestSize() const | |
515 | { | |
516 | wxSize best = wxListBox::DoGetBestSize(); | |
517 | best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox | |
518 | CacheBestSize(best); | |
519 | return best; | |
520 | } | |
521 | ||
522 | #endif // wxUSE_CHECKLISTBOX |