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