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