]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checklst.cpp
Fix for bug #1217874, Error in parameter name in DocManager.CreateView
[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{
67bfb377
JS
325 if (wxListBox::Create(parent, id, pos, size, n, choices,
326 style | wxLB_OWNERDRAW, validator, name))
327 {
328 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
329 return true;
330 }
331 else
332 return false;
799cea00 333}
799cea00 334
584ad2a3
MB
335bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
336 const wxPoint& pos, const wxSize& size,
337 const wxArrayString& choices,
338 long style,
339 const wxValidator& validator, const wxString& name)
340{
67bfb377
JS
341 if (wxListBox::Create(parent, id, pos, size, choices,
342 style | wxLB_OWNERDRAW, validator, name))
343 {
344 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
345 return true;
346 }
347 else
348 return false;
584ad2a3
MB
349}
350
e2ca995f
VZ
351// misc overloaded methods
352// -----------------------
799cea00 353
dd3c394a
VZ
354void wxCheckListBox::Delete(int N)
355{
356 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 357 wxT("invalid index in wxListBox::Delete") );
dd3c394a
VZ
358
359 wxListBox::Delete(N);
360
361 // free memory
362 delete m_aItems[N];
363
b54e41c5 364 m_aItems.RemoveAt(N);
dd3c394a
VZ
365}
366
0c35333e
RD
367bool wxCheckListBox::SetFont( const wxFont &font )
368{
369 size_t i;
07cf98cb 370 for ( i = 0; i < m_aItems.GetCount(); i++ )
0c35333e 371 m_aItems[i]->SetFont(font);
07cf98cb 372
0c35333e 373 wxListBox::SetFont(font);
07cf98cb 374
02b7b6b0 375 return true;
0c35333e
RD
376}
377
2bda0e17
KB
378// create/retrieve item
379// --------------------
380
381// create a check list box item
2b5f62a0 382wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
2bda0e17
KB
383{
384 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
2bda0e17
KB
385 return pItem;
386}
387
2bda0e17
KB
388// return item size
389// ----------------
390bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
391{
392 if ( wxListBox::MSWOnMeasure(item) ) {
393 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
394
395 // save item height
396 m_nItemHeight = pStruct->itemHeight;
397
398 // add place for the check mark
399 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
400
02b7b6b0 401 return true;
2bda0e17
KB
402 }
403
02b7b6b0 404 return false;
2bda0e17
KB
405}
406
407// check items
408// -----------
409
c86f1403 410bool wxCheckListBox::IsChecked(size_t uiIndex) const
2bda0e17 411{
02b7b6b0 412 wxCHECK_MSG( uiIndex < (size_t)GetCount(), false, _T("bad wxCheckListBox index") );
d90879fa
VZ
413
414 return GetItem(uiIndex)->IsChecked();
2bda0e17
KB
415}
416
c86f1403 417void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
2bda0e17 418{
d90879fa
VZ
419 wxCHECK_RET( uiIndex < (size_t)GetCount(), _T("bad wxCheckListBox index") );
420
421 GetItem(uiIndex)->Check(bCheck);
2bda0e17
KB
422}
423
424// process events
425// --------------
426
d90879fa 427void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
2bda0e17 428{
d90879fa
VZ
429 // what do we do?
430 enum
431 {
432 None,
433 Toggle,
434 Set,
435 Clear
436 } oper;
437
77e00fe9 438 switch ( event.GetKeyCode() )
d90879fa
VZ
439 {
440 case WXK_SPACE:
441 oper = Toggle;
442 break;
443
444 case WXK_NUMPAD_ADD:
445 case '+':
446 oper = Set;
447 break;
448
449 case WXK_NUMPAD_SUBTRACT:
450 case '-':
451 oper = Clear;
452 break;
453
454 default:
455 oper = None;
456 }
457
458 if ( oper != None )
459 {
460 wxArrayInt selections;
a8f473ac 461 int count = 0;
d90879fa
VZ
462 if ( HasMultipleSelection() )
463 {
464 count = GetSelections(selections);
465 }
466 else
467 {
a8f473ac
RD
468 int sel = GetSelection();
469 if (sel != -1)
470 {
471 count = 1;
472 selections.Add(sel);
473 }
d90879fa
VZ
474 }
475
476 for ( int i = 0; i < count; i++ )
477 {
478 wxCheckListBoxItem *item = GetItem(selections[i]);
479 if ( !item )
480 {
481 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
482 continue;
483 }
484
485 switch ( oper )
486 {
487 case Toggle:
488 item->Toggle();
489 break;
490
491 case Set:
492 case Clear:
493 item->Check( oper == Set );
494 break;
495
496 default:
497 wxFAIL_MSG( _T("what should this key do?") );
498 }
e2ca995f
VZ
499
500 // we should send an event as this has been done by the user and
501 // not by the program
502 item->SendEvent();
d90879fa
VZ
503 }
504 }
505 else // nothing to do
506 {
507 event.Skip();
508 }
2bda0e17
KB
509}
510
511void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
512{
513 // clicking on the item selects it, clicking on the checkmark toggles
c1888b05 514 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
15c3723c 515 int nItem = HitTest(event.GetX(), event.GetY());
c4dbfe14 516
e2ca995f
VZ
517 if ( nItem != wxNOT_FOUND ) {
518 wxCheckListBoxItem *item = GetItem(nItem);
519 item->Toggle();
520 item->SendEvent();
521 }
2bda0e17
KB
522 //else: it's not an error, just click outside of client zone
523 }
524 else {
525 // implement default behaviour: clicking on the item selects it
526 event.Skip();
527 }
528}
1c089c47 529
15c3723c
VZ
530int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
531{
15c3723c
VZ
532 int nItem = (int)::SendMessage
533 (
534 (HWND)GetHWND(),
535 LB_ITEMFROMPOINT,
536 0,
537 MAKELPARAM(x, y)
538 );
15c3723c
VZ
539
540 return nItem >= m_noItems ? wxNOT_FOUND : nItem;
541}
542
6d50c6a7
RD
543
544wxSize wxCheckListBox::DoGetBestSize() const
545{
546 wxSize best = wxListBox::DoGetBestSize();
547 best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox
31582e4e 548 CacheBestSize(best);
6d50c6a7
RD
549 return best;
550}
551
1c089c47
JS
552#endif
553