]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checklst.cpp
Defer SetHorizontalExtent to idle, also fixes (wxListBox::Delete ignores Freeze/Thaw...
[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 49#include "wx/msw/private.h"
74052fe8 50#include "wx/msw/dc.h"
4676948b 51
dd3c394a
VZ
52// ----------------------------------------------------------------------------
53// private functions
54// ----------------------------------------------------------------------------
55
56// get item (converted to right type)
57#define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
3d05544e 58
2bda0e17 59// ============================================================================
6463b9f5 60// implementation
2bda0e17
KB
61// ============================================================================
62
a2825c34
SC
63
64#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
65WX_DEFINE_FLAGS( wxCheckListBoxStyle )
66
3ff066a4 67wxBEGIN_FLAGS( wxCheckListBoxStyle )
bc9fb572
JS
68 // new style border flags, we put them first to
69 // use them for streaming out
3ff066a4
SC
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)
02b7b6b0 76
bc9fb572 77 // old style border flags
3ff066a4
SC
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)
cb0afb26 83 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
84
85 // standard window styles
3ff066a4
SC
86 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
87 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
88 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
89 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 90 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
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)
a2825c34 102 wxFLAGS_MEMBER(wxLB_OWNERDRAW)
3ff066a4
SC
103
104wxEND_FLAGS( wxCheckListBoxStyle )
bc9fb572
JS
105
106IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox, wxListBox,"wx/checklst.h")
107
3ff066a4 108wxBEGIN_PROPERTIES_TABLE(wxCheckListBox)
02b7b6b0 109 wxEVENT_PROPERTY( Toggle , wxEVT_COMMAND_CHECKLISTBOX_TOGGLED , wxCommandEvent )
af498247 110 wxPROPERTY_FLAGS( WindowStyle , wxCheckListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , wxLB_OWNERDRAW /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 111wxEND_PROPERTIES_TABLE()
bc9fb572 112
a2825c34
SC
113wxBEGIN_HANDLERS_TABLE(wxCheckListBox)
114wxEND_HANDLERS_TABLE()
115
02b7b6b0 116wxCONSTRUCTOR_4( wxCheckListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
a2825c34 117
bc9fb572 118#else
fd7ab28c 119IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
bc9fb572 120#endif
2bda0e17
KB
121
122// ----------------------------------------------------------------------------
123// declaration and implementation of wxCheckListBoxItem class
124// ----------------------------------------------------------------------------
125
126class wxCheckListBoxItem : public wxOwnerDrawn
127{
f5851311 128friend class WXDLLIMPEXP_FWD_CORE wxCheckListBox;
2bda0e17 129public:
aa61d352
VZ
130 // ctor
131 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
2bda0e17 132
aa61d352
VZ
133 // drawing functions
134 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
2bda0e17 135
aa61d352
VZ
136 // simple accessors and operations
137 bool IsChecked() const { return m_bChecked; }
e2ca995f 138
aa61d352
VZ
139 void Check(bool bCheck);
140 void Toggle() { Check(!IsChecked()); }
2bda0e17 141
aa61d352 142 void SendEvent();
e2ca995f 143
2bda0e17 144private:
aa61d352
VZ
145 bool m_bChecked;
146 wxCheckListBox *m_pParent;
ff0d9d83 147 size_t m_nIndex;
0d098130 148
aa61d352 149 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem)
2bda0e17
KB
150};
151
c86f1403 152wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
02b7b6b0 153 : wxOwnerDrawn(wxEmptyString, true) // checkable
2bda0e17 154{
aa61d352
VZ
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
ff0d9d83
VZ
163 SetMarginWidth(::GetSystemMetrics(SM_CXMENUCHECK) - 2);
164
aa61d352 165 SetBackgroundColour(pParent->GetBackgroundColour());
2bda0e17
KB
166}
167
45187197 168bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
2bda0e17
KB
169 wxODAction act, wxODStatus stat)
170{
0d098130
VZ
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;
2bda0e17 177
888dde65 178 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
0d098130 179 // now draw the check mark part
888dde65 180 HDC hdc = GetHdcOf(*impl);
45187197 181
ff0d9d83
VZ
182 int nBmpWidth = ::GetSystemMetrics(SM_CXMENUCHECK),
183 nBmpHeight = ::GetSystemMetrics(SM_CYMENUCHECK);
2bda0e17 184
2bda0e17 185
ff0d9d83
VZ
186 // first create bitmap in a memory DC
187 MemoryHDC hdcMem(hdc);
188 CompatibleBitmap hBmpCheck(hdc, nBmpWidth, nBmpHeight);
2bda0e17 189
ff0d9d83
VZ
190 // then draw a check mark into it
191 {
192 SelectInHDC selBmp(hdcMem, hBmpCheck);
2bda0e17 193
ff0d9d83
VZ
194 int flags = wxCONTROL_FLAT;
195 if ( IsChecked() )
196 flags |= wxCONTROL_CHECKED;
2bda0e17 197
ff0d9d83
VZ
198 wxDCTemp dcMem(hdcMem);
199 wxRendererNative::Get().DrawCheckBox(
200 m_pParent, dcMem, wxRect(0, 0, nBmpWidth, nBmpHeight), flags);
201 } // select hBmpCheck out of hdcMem
ee6e1b1d 202
ff0d9d83
VZ
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;
2bda0e17 206
ff0d9d83
VZ
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);
2bda0e17 212
ff0d9d83
VZ
213 UINT fStyle = stat & wxOwnerDrawn::wxODSelected ? ILD_SELECTED : ILD_NORMAL;
214 ImageList_Draw(himl, 0, hdc, x, y, fStyle);
2bda0e17 215
ff0d9d83 216 ImageList_Destroy(himl);
2bda0e17 217
02b7b6b0 218 return true;
2bda0e17
KB
219}
220
221// change the state of the item and redraw it
dd3c394a 222void wxCheckListBoxItem::Check(bool check)
45187197 223{
dd3c394a 224 m_bChecked = check;
2bda0e17 225
e2ca995f 226 // index may be changed because new items were added/deleted
dd3c394a
VZ
227 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
228 {
229 // update it
230 int index = m_pParent->GetItemIndex(this);
231
223d09f6 232 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
dd3c394a
VZ
233
234 m_nIndex = (size_t)index;
235 }
2bda0e17 236
c4dbfe14
VZ
237 HWND hwndListbox = (HWND)m_pParent->GetHWND();
238
02b7b6b0 239 RECT rcUpdate;
0c35333e 240
02b7b6b0
WS
241 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
242 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
243 {
244 wxLogDebug(wxT("LB_GETITEMRECT failed"));
245 }
c4dbfe14 246
02b7b6b0 247 ::InvalidateRect(hwndListbox, &rcUpdate, FALSE);
e2ca995f 248}
0c35333e 249
e2ca995f
VZ
250// send an "item checked" event
251void wxCheckListBoxItem::SendEvent()
252{
dd3c394a
VZ
253 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
254 event.SetInt(m_nIndex);
255 event.SetEventObject(m_pParent);
c70557da 256 event.SetString(m_pParent->GetString(m_nIndex));
dd3c394a 257 m_pParent->ProcessCommand(event);
2bda0e17
KB
258}
259
260// ----------------------------------------------------------------------------
261// implementation of wxCheckListBox class
262// ----------------------------------------------------------------------------
263
264// define event table
265// ------------------
266BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
d90879fa 267 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
2bda0e17
KB
268 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
269END_EVENT_TABLE()
270
271// control creation
272// ----------------
273
274// def ctor: use Create() to really create the control
d90879fa 275wxCheckListBox::wxCheckListBox()
2bda0e17
KB
276{
277}
278
279// ctor which creates the associated control
debe6624 280wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
2bda0e17 281 const wxPoint& pos, const wxSize& size,
debe6624
JS
282 int nStrings, const wxString choices[],
283 long style, const wxValidator& val,
dd3c394a 284 const wxString& name)
2bda0e17 285{
799cea00 286 Create(parent, id, pos, size, nStrings, choices, style, val, name);
dd3c394a
VZ
287}
288
584ad2a3
MB
289wxCheckListBox::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
799cea00
VS
298bool 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{
a374cdc4
JS
304 return wxListBox::Create(parent, id, pos, size, n, choices,
305 style | wxLB_OWNERDRAW, validator, name);
799cea00 306}
799cea00 307
584ad2a3
MB
308bool 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{
a374cdc4
JS
314 return wxListBox::Create(parent, id, pos, size, choices,
315 style | wxLB_OWNERDRAW, validator, name);
584ad2a3
MB
316}
317
e2ca995f
VZ
318// misc overloaded methods
319// -----------------------
799cea00 320
aa61d352 321void wxCheckListBox::Delete(unsigned int n)
dd3c394a 322{
aa61d352 323 wxCHECK_RET( IsValid(n),
223d09f6 324 wxT("invalid index in wxListBox::Delete") );
dd3c394a 325
aa61d352 326 wxListBox::Delete(n);
dd3c394a
VZ
327
328 // free memory
aa61d352 329 delete m_aItems[n];
dd3c394a 330
aa61d352 331 m_aItems.RemoveAt(n);
dd3c394a
VZ
332}
333
0c35333e
RD
334bool wxCheckListBox::SetFont( const wxFont &font )
335{
aa61d352 336 unsigned int i;
07cf98cb 337 for ( i = 0; i < m_aItems.GetCount(); i++ )
0c35333e 338 m_aItems[i]->SetFont(font);
07cf98cb 339
0c35333e 340 wxListBox::SetFont(font);
07cf98cb 341
02b7b6b0 342 return true;
0c35333e
RD
343}
344
2bda0e17
KB
345// create/retrieve item
346// --------------------
347
348// create a check list box item
2b5f62a0 349wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
2bda0e17
KB
350{
351 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
2bda0e17
KB
352 return pItem;
353}
354
2bda0e17
KB
355// return item size
356// ----------------
357bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
358{
359 if ( wxListBox::MSWOnMeasure(item) ) {
360 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
361
ff0d9d83
VZ
362 // add place for the check mark
363 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
364 pStruct->itemHeight += 1;
365
2bda0e17
KB
366 // save item height
367 m_nItemHeight = pStruct->itemHeight;
368
02b7b6b0 369 return true;
2bda0e17
KB
370 }
371
02b7b6b0 372 return false;
2bda0e17
KB
373}
374
375// check items
376// -----------
377
aa61d352 378bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
2bda0e17 379{
aa61d352 380 wxCHECK_MSG( IsValid(uiIndex), false, _T("bad wxCheckListBox index") );
d90879fa
VZ
381
382 return GetItem(uiIndex)->IsChecked();
2bda0e17
KB
383}
384
aa61d352 385void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
2bda0e17 386{
aa61d352 387 wxCHECK_RET( IsValid(uiIndex), _T("bad wxCheckListBox index") );
d90879fa
VZ
388
389 GetItem(uiIndex)->Check(bCheck);
2bda0e17
KB
390}
391
392// process events
393// --------------
394
d90879fa 395void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
2bda0e17 396{
d90879fa
VZ
397 // what do we do?
398 enum
399 {
400 None,
401 Toggle,
402 Set,
403 Clear
404 } oper;
405
77e00fe9 406 switch ( event.GetKeyCode() )
d90879fa
VZ
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;
a8f473ac 429 int count = 0;
d90879fa
VZ
430 if ( HasMultipleSelection() )
431 {
432 count = GetSelections(selections);
433 }
434 else
435 {
a8f473ac
RD
436 int sel = GetSelection();
437 if (sel != -1)
438 {
439 count = 1;
440 selections.Add(sel);
441 }
d90879fa
VZ
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 }
e2ca995f
VZ
467
468 // we should send an event as this has been done by the user and
469 // not by the program
470 item->SendEvent();
d90879fa
VZ
471 }
472 }
473 else // nothing to do
474 {
475 event.Skip();
476 }
2bda0e17
KB
477}
478
479void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
480{
aa61d352
VZ
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 {
af8fc116
VZ
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 }
aa61d352
VZ
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();
e2ca995f 508 }
2bda0e17 509}
1c089c47 510
15c3723c
VZ
511int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
512{
15c3723c
VZ
513 int nItem = (int)::SendMessage
514 (
515 (HWND)GetHWND(),
516 LB_ITEMFROMPOINT,
517 0,
518 MAKELPARAM(x, y)
519 );
15c3723c 520
aa61d352 521 return nItem >= (int)m_noItems ? wxNOT_FOUND : nItem;
15c3723c
VZ
522}
523
6d50c6a7
RD
524
525wxSize wxCheckListBox::DoGetBestSize() const
526{
527 wxSize best = wxListBox::DoGetBestSize();
528 best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox
31582e4e 529 CacheBestSize(best);
6d50c6a7
RD
530 return best;
531}
532
1c089c47 533#endif