]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checklst.cpp
close the handle returned by OpenProcessToken() (fixes #10129)
[wxWidgets.git] / src / msw / checklst.cpp
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 DECLARE_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 return true;
219 }
220
221 // change the state of the item and redraw it
222 void wxCheckListBoxItem::Check(bool check)
223 {
224 m_bChecked = check;
225
226 // index may be changed because new items were added/deleted
227 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
228 {
229 // update it
230 int index = m_pParent->GetItemIndex(this);
231
232 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
233
234 m_nIndex = (size_t)index;
235 }
236
237 HWND hwndListbox = (HWND)m_pParent->GetHWND();
238
239 RECT rcUpdate;
240
241 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
242 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
243 {
244 wxLogDebug(wxT("LB_GETITEMRECT failed"));
245 }
246
247 ::InvalidateRect(hwndListbox, &rcUpdate, FALSE);
248 }
249
250 // send an "item checked" event
251 void wxCheckListBoxItem::SendEvent()
252 {
253 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
254 event.SetInt(m_nIndex);
255 event.SetEventObject(m_pParent);
256 event.SetString(m_pParent->GetString(m_nIndex));
257 m_pParent->ProcessCommand(event);
258 }
259
260 // ----------------------------------------------------------------------------
261 // implementation of wxCheckListBox class
262 // ----------------------------------------------------------------------------
263
264 // define event table
265 // ------------------
266 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
267 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
268 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
269 END_EVENT_TABLE()
270
271 // control creation
272 // ----------------
273
274 // def ctor: use Create() to really create the control
275 wxCheckListBox::wxCheckListBox()
276 {
277 }
278
279 // ctor which creates the associated control
280 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
281 const wxPoint& pos, const wxSize& size,
282 int nStrings, const wxString choices[],
283 long style, const wxValidator& val,
284 const wxString& name)
285 {
286 Create(parent, id, pos, size, nStrings, choices, style, val, name);
287 }
288
289 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
290 const wxPoint& pos, const wxSize& size,
291 const wxArrayString& choices,
292 long style, const wxValidator& val,
293 const wxString& name)
294 {
295 Create(parent, id, pos, size, choices, style, val, name);
296 }
297
298 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
299 const wxPoint& pos, const wxSize& size,
300 int n, const wxString choices[],
301 long style,
302 const wxValidator& validator, const wxString& name)
303 {
304 return wxListBox::Create(parent, id, pos, size, n, choices,
305 style | wxLB_OWNERDRAW, validator, name);
306 }
307
308 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
309 const wxPoint& pos, const wxSize& size,
310 const wxArrayString& choices,
311 long style,
312 const wxValidator& validator, const wxString& name)
313 {
314 return wxListBox::Create(parent, id, pos, size, choices,
315 style | wxLB_OWNERDRAW, validator, name);
316 }
317
318 // misc overloaded methods
319 // -----------------------
320
321 void wxCheckListBox::Delete(unsigned int n)
322 {
323 wxCHECK_RET( IsValid(n),
324 wxT("invalid index in wxListBox::Delete") );
325
326 wxListBox::Delete(n);
327
328 // free memory
329 delete m_aItems[n];
330
331 m_aItems.RemoveAt(n);
332 }
333
334 bool wxCheckListBox::SetFont( const wxFont &font )
335 {
336 unsigned int i;
337 for ( i = 0; i < m_aItems.GetCount(); i++ )
338 m_aItems[i]->SetFont(font);
339
340 wxListBox::SetFont(font);
341
342 return true;
343 }
344
345 // create/retrieve item
346 // --------------------
347
348 // create a check list box item
349 wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
350 {
351 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
352 return pItem;
353 }
354
355 // return item size
356 // ----------------
357 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
358 {
359 if ( wxListBox::MSWOnMeasure(item) ) {
360 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
361
362 // add place for the check mark
363 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
364 pStruct->itemHeight += 1;
365
366 // save item height
367 m_nItemHeight = pStruct->itemHeight;
368
369 return true;
370 }
371
372 return false;
373 }
374
375 // check items
376 // -----------
377
378 bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
379 {
380 wxCHECK_MSG( IsValid(uiIndex), false, _T("bad wxCheckListBox index") );
381
382 return GetItem(uiIndex)->IsChecked();
383 }
384
385 void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
386 {
387 wxCHECK_RET( IsValid(uiIndex), _T("bad wxCheckListBox index") );
388
389 GetItem(uiIndex)->Check(bCheck);
390 }
391
392 // process events
393 // --------------
394
395 void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
396 {
397 // what do we do?
398 enum
399 {
400 None,
401 Toggle,
402 Set,
403 Clear
404 } oper;
405
406 switch ( event.GetKeyCode() )
407 {
408 case WXK_SPACE:
409 oper = Toggle;
410 break;
411
412 case WXK_NUMPAD_ADD:
413 case '+':
414 oper = Set;
415 break;
416
417 case WXK_NUMPAD_SUBTRACT:
418 case '-':
419 oper = Clear;
420 break;
421
422 default:
423 oper = None;
424 }
425
426 if ( oper != None )
427 {
428 wxArrayInt selections;
429 int count = 0;
430 if ( HasMultipleSelection() )
431 {
432 count = GetSelections(selections);
433 }
434 else
435 {
436 int sel = GetSelection();
437 if (sel != -1)
438 {
439 count = 1;
440 selections.Add(sel);
441 }
442 }
443
444 for ( int i = 0; i < count; i++ )
445 {
446 wxCheckListBoxItem *item = GetItem(selections[i]);
447 if ( !item )
448 {
449 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
450 continue;
451 }
452
453 switch ( oper )
454 {
455 case Toggle:
456 item->Toggle();
457 break;
458
459 case Set:
460 case Clear:
461 item->Check( oper == Set );
462 break;
463
464 default:
465 wxFAIL_MSG( _T("what should this key do?") );
466 }
467
468 // we should send an event as this has been done by the user and
469 // not by the program
470 item->SendEvent();
471 }
472 }
473 else // nothing to do
474 {
475 event.Skip();
476 }
477 }
478
479 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
480 {
481 // clicking on the item selects it, clicking on the checkmark toggles
482 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() )
483 {
484 int nItem = HitTest(event.GetX(), event.GetY());
485
486 if ( nItem != wxNOT_FOUND )
487 {
488 // people expect to get "kill focus" event for the currently
489 // focused control before getting events from the other controls
490 // and, equally importantly, they may prevent the focus change from
491 // taking place at all (e.g. because the old control contents is
492 // invalid and needs to be corrected) in which case we shouldn't
493 // generate this event at all
494 SetFocus();
495 if ( FindFocus() == this )
496 {
497 wxCheckListBoxItem *item = GetItem(nItem);
498 item->Toggle();
499 item->SendEvent();
500 }
501 }
502 //else: it's not an error, just click outside of client zone
503 }
504 else
505 {
506 // implement default behaviour: clicking on the item selects it
507 event.Skip();
508 }
509 }
510
511 int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
512 {
513 int nItem = (int)::SendMessage
514 (
515 (HWND)GetHWND(),
516 LB_ITEMFROMPOINT,
517 0,
518 MAKELPARAM(x, y)
519 );
520
521 return nItem >= (int)m_noItems ? wxNOT_FOUND : nItem;
522 }
523
524
525 wxSize wxCheckListBox::DoGetBestSize() const
526 {
527 wxSize best = wxListBox::DoGetBestSize();
528 best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox
529 CacheBestSize(best);
530 return best;
531 }
532
533 #endif