]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checklst.cpp
check that the version of __sync_sub_and_fetch that returns a value is supported...
[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{
f5851311 127friend class WXDLLIMPEXP_FWD_CORE 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
888dde65 177 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
0d098130 178 // now draw the check mark part
888dde65 179 HDC hdc = GetHdcOf(*impl);
45187197 180
ff0d9d83
VZ
181 int nBmpWidth = ::GetSystemMetrics(SM_CXMENUCHECK),
182 nBmpHeight = ::GetSystemMetrics(SM_CYMENUCHECK);
2bda0e17 183
2bda0e17 184
ff0d9d83
VZ
185 // first create bitmap in a memory DC
186 MemoryHDC hdcMem(hdc);
187 CompatibleBitmap hBmpCheck(hdc, nBmpWidth, nBmpHeight);
2bda0e17 188
ff0d9d83
VZ
189 // then draw a check mark into it
190 {
191 SelectInHDC selBmp(hdcMem, hBmpCheck);
2bda0e17 192
ff0d9d83
VZ
193 int flags = wxCONTROL_FLAT;
194 if ( IsChecked() )
195 flags |= wxCONTROL_CHECKED;
2bda0e17 196
ff0d9d83
VZ
197 wxDCTemp dcMem(hdcMem);
198 wxRendererNative::Get().DrawCheckBox(
199 m_pParent, dcMem, wxRect(0, 0, nBmpWidth, nBmpHeight), flags);
200 } // select hBmpCheck out of hdcMem
ee6e1b1d 201
ff0d9d83
VZ
202 // shift check mark 2 pixel to the right and bottom, looks better like this
203 int x = rc.GetX() + 2,
204 y = rc.GetY() + 2;
2bda0e17 205
ff0d9d83
VZ
206 // finally draw bitmap to screen: uses image list functions to blend
207 // the bitmap with the background colour (better for the selected items)
208 HIMAGELIST himl = ImageList_Create(nBmpWidth, nBmpHeight,
209 ILC_COLOR32 | ILC_MASK, 1, 1);
210 ImageList_Add(himl, hBmpCheck, NULL);
2bda0e17 211
ff0d9d83
VZ
212 UINT fStyle = stat & wxOwnerDrawn::wxODSelected ? ILD_SELECTED : ILD_NORMAL;
213 ImageList_Draw(himl, 0, hdc, x, y, fStyle);
2bda0e17 214
ff0d9d83 215 ImageList_Destroy(himl);
2bda0e17 216
02b7b6b0 217 return true;
2bda0e17
KB
218}
219
220// change the state of the item and redraw it
dd3c394a 221void wxCheckListBoxItem::Check(bool check)
45187197 222{
dd3c394a 223 m_bChecked = check;
2bda0e17 224
e2ca995f 225 // index may be changed because new items were added/deleted
dd3c394a
VZ
226 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
227 {
228 // update it
229 int index = m_pParent->GetItemIndex(this);
230
223d09f6 231 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
dd3c394a
VZ
232
233 m_nIndex = (size_t)index;
234 }
2bda0e17 235
c4dbfe14
VZ
236 HWND hwndListbox = (HWND)m_pParent->GetHWND();
237
02b7b6b0 238 RECT rcUpdate;
0c35333e 239
02b7b6b0
WS
240 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
241 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
242 {
243 wxLogDebug(wxT("LB_GETITEMRECT failed"));
244 }
c4dbfe14 245
02b7b6b0 246 ::InvalidateRect(hwndListbox, &rcUpdate, FALSE);
e2ca995f 247}
0c35333e 248
e2ca995f
VZ
249// send an "item checked" event
250void wxCheckListBoxItem::SendEvent()
251{
dd3c394a
VZ
252 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
253 event.SetInt(m_nIndex);
254 event.SetEventObject(m_pParent);
c70557da 255 event.SetString(m_pParent->GetString(m_nIndex));
dd3c394a 256 m_pParent->ProcessCommand(event);
2bda0e17
KB
257}
258
259// ----------------------------------------------------------------------------
260// implementation of wxCheckListBox class
261// ----------------------------------------------------------------------------
262
263// define event table
264// ------------------
265BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
d90879fa 266 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
2bda0e17
KB
267 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
268END_EVENT_TABLE()
269
270// control creation
271// ----------------
272
273// def ctor: use Create() to really create the control
d90879fa 274wxCheckListBox::wxCheckListBox()
2bda0e17
KB
275{
276}
277
278// ctor which creates the associated control
debe6624 279wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
2bda0e17 280 const wxPoint& pos, const wxSize& size,
debe6624
JS
281 int nStrings, const wxString choices[],
282 long style, const wxValidator& val,
dd3c394a 283 const wxString& name)
2bda0e17 284{
799cea00 285 Create(parent, id, pos, size, nStrings, choices, style, val, name);
dd3c394a
VZ
286}
287
584ad2a3
MB
288wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
289 const wxPoint& pos, const wxSize& size,
290 const wxArrayString& choices,
291 long style, const wxValidator& val,
292 const wxString& name)
293{
294 Create(parent, id, pos, size, choices, style, val, name);
295}
296
799cea00
VS
297bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
298 const wxPoint& pos, const wxSize& size,
299 int n, const wxString choices[],
300 long style,
301 const wxValidator& validator, const wxString& name)
302{
a374cdc4
JS
303 return wxListBox::Create(parent, id, pos, size, n, choices,
304 style | wxLB_OWNERDRAW, validator, name);
799cea00 305}
799cea00 306
584ad2a3
MB
307bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
308 const wxPoint& pos, const wxSize& size,
309 const wxArrayString& choices,
310 long style,
311 const wxValidator& validator, const wxString& name)
312{
a374cdc4
JS
313 return wxListBox::Create(parent, id, pos, size, choices,
314 style | wxLB_OWNERDRAW, validator, name);
584ad2a3
MB
315}
316
e2ca995f
VZ
317// misc overloaded methods
318// -----------------------
799cea00 319
aa61d352 320void wxCheckListBox::Delete(unsigned int n)
dd3c394a 321{
aa61d352 322 wxCHECK_RET( IsValid(n),
223d09f6 323 wxT("invalid index in wxListBox::Delete") );
dd3c394a 324
aa61d352 325 wxListBox::Delete(n);
dd3c394a
VZ
326
327 // free memory
aa61d352 328 delete m_aItems[n];
dd3c394a 329
aa61d352 330 m_aItems.RemoveAt(n);
dd3c394a
VZ
331}
332
0c35333e
RD
333bool wxCheckListBox::SetFont( const wxFont &font )
334{
aa61d352 335 unsigned int i;
07cf98cb 336 for ( i = 0; i < m_aItems.GetCount(); i++ )
0c35333e 337 m_aItems[i]->SetFont(font);
07cf98cb 338
0c35333e 339 wxListBox::SetFont(font);
07cf98cb 340
02b7b6b0 341 return true;
0c35333e
RD
342}
343
2bda0e17
KB
344// create/retrieve item
345// --------------------
346
347// create a check list box item
2b5f62a0 348wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
2bda0e17
KB
349{
350 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
2bda0e17
KB
351 return pItem;
352}
353
2bda0e17
KB
354// return item size
355// ----------------
356bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
357{
358 if ( wxListBox::MSWOnMeasure(item) ) {
359 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
360
ff0d9d83
VZ
361 // add place for the check mark
362 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
363 pStruct->itemHeight += 1;
364
2bda0e17
KB
365 // save item height
366 m_nItemHeight = pStruct->itemHeight;
367
02b7b6b0 368 return true;
2bda0e17
KB
369 }
370
02b7b6b0 371 return false;
2bda0e17
KB
372}
373
374// check items
375// -----------
376
aa61d352 377bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
2bda0e17 378{
aa61d352 379 wxCHECK_MSG( IsValid(uiIndex), false, _T("bad wxCheckListBox index") );
d90879fa
VZ
380
381 return GetItem(uiIndex)->IsChecked();
2bda0e17
KB
382}
383
aa61d352 384void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
2bda0e17 385{
aa61d352 386 wxCHECK_RET( IsValid(uiIndex), _T("bad wxCheckListBox index") );
d90879fa
VZ
387
388 GetItem(uiIndex)->Check(bCheck);
2bda0e17
KB
389}
390
391// process events
392// --------------
393
d90879fa 394void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
2bda0e17 395{
d90879fa
VZ
396 // what do we do?
397 enum
398 {
399 None,
400 Toggle,
401 Set,
402 Clear
403 } oper;
404
77e00fe9 405 switch ( event.GetKeyCode() )
d90879fa
VZ
406 {
407 case WXK_SPACE:
408 oper = Toggle;
409 break;
410
411 case WXK_NUMPAD_ADD:
412 case '+':
413 oper = Set;
414 break;
415
416 case WXK_NUMPAD_SUBTRACT:
417 case '-':
418 oper = Clear;
419 break;
420
421 default:
422 oper = None;
423 }
424
425 if ( oper != None )
426 {
427 wxArrayInt selections;
a8f473ac 428 int count = 0;
d90879fa
VZ
429 if ( HasMultipleSelection() )
430 {
431 count = GetSelections(selections);
432 }
433 else
434 {
a8f473ac
RD
435 int sel = GetSelection();
436 if (sel != -1)
437 {
438 count = 1;
439 selections.Add(sel);
440 }
d90879fa
VZ
441 }
442
443 for ( int i = 0; i < count; i++ )
444 {
445 wxCheckListBoxItem *item = GetItem(selections[i]);
446 if ( !item )
447 {
448 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
449 continue;
450 }
451
452 switch ( oper )
453 {
454 case Toggle:
455 item->Toggle();
456 break;
457
458 case Set:
459 case Clear:
460 item->Check( oper == Set );
461 break;
462
463 default:
464 wxFAIL_MSG( _T("what should this key do?") );
465 }
e2ca995f
VZ
466
467 // we should send an event as this has been done by the user and
468 // not by the program
469 item->SendEvent();
d90879fa
VZ
470 }
471 }
472 else // nothing to do
473 {
474 event.Skip();
475 }
2bda0e17
KB
476}
477
478void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
479{
aa61d352
VZ
480 // clicking on the item selects it, clicking on the checkmark toggles
481 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() )
482 {
483 int nItem = HitTest(event.GetX(), event.GetY());
484
485 if ( nItem != wxNOT_FOUND )
486 {
af8fc116
VZ
487 // people expect to get "kill focus" event for the currently
488 // focused control before getting events from the other controls
489 // and, equally importantly, they may prevent the focus change from
490 // taking place at all (e.g. because the old control contents is
491 // invalid and needs to be corrected) in which case we shouldn't
492 // generate this event at all
493 SetFocus();
494 if ( FindFocus() == this )
495 {
496 wxCheckListBoxItem *item = GetItem(nItem);
497 item->Toggle();
498 item->SendEvent();
499 }
aa61d352
VZ
500 }
501 //else: it's not an error, just click outside of client zone
502 }
503 else
504 {
505 // implement default behaviour: clicking on the item selects it
506 event.Skip();
e2ca995f 507 }
2bda0e17 508}
1c089c47 509
15c3723c
VZ
510int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
511{
15c3723c
VZ
512 int nItem = (int)::SendMessage
513 (
514 (HWND)GetHWND(),
515 LB_ITEMFROMPOINT,
516 0,
517 MAKELPARAM(x, y)
518 );
15c3723c 519
aa61d352 520 return nItem >= (int)m_noItems ? wxNOT_FOUND : nItem;
15c3723c
VZ
521}
522
6d50c6a7
RD
523
524wxSize wxCheckListBox::DoGetBestSize() const
525{
526 wxSize best = wxListBox::DoGetBestSize();
527 best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox
31582e4e 528 CacheBestSize(best);
6d50c6a7
RD
529 return best;
530}
531
1c089c47 532#endif