]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checklst.cpp
don't compile DirectDraw code when wxUSE_DIRECTDRAW==0 (patch 1461271); set wxUSE_DIR...
[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__
24#pragma hdrstop
25#endif
26
eecdb000 27#if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN
1c089c47 28
d90879fa
VZ
29#ifndef WX_PRECOMP
30 #include "wx/object.h"
31 #include "wx/colour.h"
32 #include "wx/font.h"
33 #include "wx/bitmap.h"
34 #include "wx/window.h"
35 #include "wx/listbox.h"
36 #include "wx/dcmemory.h"
37
38 #include "wx/settings.h"
39
40 #include "wx/log.h"
41#endif
42
2bda0e17 43#include "wx/ownerdrw.h"
d90879fa 44#include "wx/checklst.h"
2bda0e17 45
9ed0d735 46#include "wx/msw/wrapwin.h"
dd3c394a
VZ
47#include <windowsx.h>
48
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;
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
162 // fix appearance for check list boxes: they don't look quite the same as
163 // menu icons
164 SetMarginWidth(::GetSystemMetrics(SM_CXMENUCHECK) -
165 2*wxSystemSettings::GetMetric(wxSYS_EDGE_X) + 1);
166 SetBackgroundColour(pParent->GetBackgroundColour());
2bda0e17
KB
167}
168
45187197 169bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
2bda0e17
KB
170 wxODAction act, wxODStatus stat)
171{
0d098130
VZ
172 // first draw the label
173 if ( IsChecked() )
174 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
175
176 if ( !wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) )
177 return false;
2bda0e17 178
0d098130
VZ
179
180 // now draw the check mark part
c86f1403 181 size_t nCheckWidth = GetDefaultMarginWidth(),
0d098130 182 nCheckHeight = m_pParent->GetItemHeight();
45187197
RD
183
184 int x = rc.GetX(),
2bda0e17
KB
185 y = rc.GetY();
186
187 HDC hdc = (HDC)dc.GetHDC();
188
0d098130
VZ
189 // create pens, brushes &c
190 COLORREF colBg = ::GetSysColor(COLOR_WINDOW);
191 AutoHPEN hpenBack(colBg),
192 hpenGray(RGB(0xc0, 0xc0, 0xc0));
2bda0e17 193
0d098130
VZ
194 SelectInHDC selPen(hdc, (HGDIOBJ)hpenBack);
195 AutoHBRUSH hbrBack(colBg);
196 SelectInHDC selBrush(hdc, hbrBack);
2bda0e17 197
0d098130
VZ
198 // erase the background: it could have been filled with the selected colour
199 Rectangle(hdc, x, y, x + nCheckWidth + 1, rc.GetBottom() + 1);
2bda0e17 200
0d098130
VZ
201 // shift check mark 1 pixel to the right, looks better like this
202 x++;
ee6e1b1d 203
0d098130
VZ
204 if ( IsChecked() )
205 {
206 // first create a monochrome bitmap in a memory DC
207 MemoryHDC hdcMem(hdc);
208 MonoBitmap hbmpCheck(nCheckWidth, nCheckHeight);
209 SelectInHDC selBmp(hdcMem, hbmpCheck);
210
211 // then draw a check mark into it
212 RECT rect = { 0, 0, nCheckWidth, nCheckHeight };
213 ::DrawFrameControl(hdcMem, &rect,
4676948b 214#ifdef __WXWINCE__
0d098130 215 DFC_BUTTON, DFCS_BUTTONCHECK
4676948b 216#else
0d098130 217 DFC_MENU, DFCS_MENUCHECK
4676948b 218#endif
0d098130 219 );
2bda0e17 220
0d098130
VZ
221 // finally copy it to screen DC
222 ::BitBlt(hdc, x, y, nCheckWidth, nCheckHeight, hdcMem, 0, 0, SRCCOPY);
2bda0e17
KB
223 }
224
225 // now we draw the smaller rectangle
226 y++;
227 nCheckWidth -= 2;
228 nCheckHeight -= 2;
229
230 // draw hollow gray rectangle
0d098130 231 (void)::SelectObject(hdc, (HGDIOBJ)hpenGray);
2bda0e17 232
0d098130
VZ
233 SelectInHDC selBrush2(hdc, ::GetStockObject(NULL_BRUSH));
234 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
2bda0e17 235
02b7b6b0 236 return true;
2bda0e17
KB
237}
238
239// change the state of the item and redraw it
dd3c394a 240void wxCheckListBoxItem::Check(bool check)
45187197 241{
dd3c394a 242 m_bChecked = check;
2bda0e17 243
e2ca995f 244 // index may be changed because new items were added/deleted
dd3c394a
VZ
245 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
246 {
247 // update it
248 int index = m_pParent->GetItemIndex(this);
249
223d09f6 250 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
dd3c394a
VZ
251
252 m_nIndex = (size_t)index;
253 }
2bda0e17 254
c4dbfe14
VZ
255 HWND hwndListbox = (HWND)m_pParent->GetHWND();
256
02b7b6b0 257 RECT rcUpdate;
0c35333e 258
02b7b6b0
WS
259 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
260 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
261 {
262 wxLogDebug(wxT("LB_GETITEMRECT failed"));
263 }
c4dbfe14 264
02b7b6b0 265 ::InvalidateRect(hwndListbox, &rcUpdate, FALSE);
e2ca995f 266}
0c35333e 267
e2ca995f
VZ
268// send an "item checked" event
269void wxCheckListBoxItem::SendEvent()
270{
dd3c394a
VZ
271 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
272 event.SetInt(m_nIndex);
273 event.SetEventObject(m_pParent);
274 m_pParent->ProcessCommand(event);
2bda0e17
KB
275}
276
277// ----------------------------------------------------------------------------
278// implementation of wxCheckListBox class
279// ----------------------------------------------------------------------------
280
281// define event table
282// ------------------
283BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
d90879fa 284 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
2bda0e17
KB
285 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
286END_EVENT_TABLE()
287
288// control creation
289// ----------------
290
291// def ctor: use Create() to really create the control
d90879fa 292wxCheckListBox::wxCheckListBox()
2bda0e17
KB
293{
294}
295
296// ctor which creates the associated control
debe6624 297wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
2bda0e17 298 const wxPoint& pos, const wxSize& size,
debe6624
JS
299 int nStrings, const wxString choices[],
300 long style, const wxValidator& val,
dd3c394a 301 const wxString& name)
2bda0e17 302{
799cea00 303 Create(parent, id, pos, size, nStrings, choices, style, val, name);
dd3c394a
VZ
304}
305
584ad2a3
MB
306wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
307 const wxPoint& pos, const wxSize& size,
308 const wxArrayString& choices,
309 long style, const wxValidator& val,
310 const wxString& name)
311{
312 Create(parent, id, pos, size, choices, style, val, name);
313}
314
799cea00
VS
315bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
316 const wxPoint& pos, const wxSize& size,
317 int n, const wxString choices[],
318 long style,
319 const wxValidator& validator, const wxString& name)
320{
a374cdc4
JS
321 return wxListBox::Create(parent, id, pos, size, n, choices,
322 style | wxLB_OWNERDRAW, validator, name);
799cea00 323}
799cea00 324
584ad2a3
MB
325bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
326 const wxPoint& pos, const wxSize& size,
327 const wxArrayString& choices,
328 long style,
329 const wxValidator& validator, const wxString& name)
330{
a374cdc4
JS
331 return wxListBox::Create(parent, id, pos, size, choices,
332 style | wxLB_OWNERDRAW, validator, name);
584ad2a3
MB
333}
334
e2ca995f
VZ
335// misc overloaded methods
336// -----------------------
799cea00 337
aa61d352 338void wxCheckListBox::Delete(unsigned int n)
dd3c394a 339{
aa61d352 340 wxCHECK_RET( IsValid(n),
223d09f6 341 wxT("invalid index in wxListBox::Delete") );
dd3c394a 342
aa61d352 343 wxListBox::Delete(n);
dd3c394a
VZ
344
345 // free memory
aa61d352 346 delete m_aItems[n];
dd3c394a 347
aa61d352 348 m_aItems.RemoveAt(n);
dd3c394a
VZ
349}
350
0c35333e
RD
351bool wxCheckListBox::SetFont( const wxFont &font )
352{
aa61d352 353 unsigned int i;
07cf98cb 354 for ( i = 0; i < m_aItems.GetCount(); i++ )
0c35333e 355 m_aItems[i]->SetFont(font);
07cf98cb 356
0c35333e 357 wxListBox::SetFont(font);
07cf98cb 358
02b7b6b0 359 return true;
0c35333e
RD
360}
361
2bda0e17
KB
362// create/retrieve item
363// --------------------
364
365// create a check list box item
2b5f62a0 366wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
2bda0e17
KB
367{
368 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
2bda0e17
KB
369 return pItem;
370}
371
2bda0e17
KB
372// return item size
373// ----------------
374bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
375{
376 if ( wxListBox::MSWOnMeasure(item) ) {
377 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
378
379 // save item height
380 m_nItemHeight = pStruct->itemHeight;
381
382 // add place for the check mark
383 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
384
02b7b6b0 385 return true;
2bda0e17
KB
386 }
387
02b7b6b0 388 return false;
2bda0e17
KB
389}
390
391// check items
392// -----------
393
aa61d352 394bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
2bda0e17 395{
aa61d352 396 wxCHECK_MSG( IsValid(uiIndex), false, _T("bad wxCheckListBox index") );
d90879fa
VZ
397
398 return GetItem(uiIndex)->IsChecked();
2bda0e17
KB
399}
400
aa61d352 401void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
2bda0e17 402{
aa61d352 403 wxCHECK_RET( IsValid(uiIndex), _T("bad wxCheckListBox index") );
d90879fa
VZ
404
405 GetItem(uiIndex)->Check(bCheck);
2bda0e17
KB
406}
407
408// process events
409// --------------
410
d90879fa 411void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
2bda0e17 412{
d90879fa
VZ
413 // what do we do?
414 enum
415 {
416 None,
417 Toggle,
418 Set,
419 Clear
420 } oper;
421
77e00fe9 422 switch ( event.GetKeyCode() )
d90879fa
VZ
423 {
424 case WXK_SPACE:
425 oper = Toggle;
426 break;
427
428 case WXK_NUMPAD_ADD:
429 case '+':
430 oper = Set;
431 break;
432
433 case WXK_NUMPAD_SUBTRACT:
434 case '-':
435 oper = Clear;
436 break;
437
438 default:
439 oper = None;
440 }
441
442 if ( oper != None )
443 {
444 wxArrayInt selections;
a8f473ac 445 int count = 0;
d90879fa
VZ
446 if ( HasMultipleSelection() )
447 {
448 count = GetSelections(selections);
449 }
450 else
451 {
a8f473ac
RD
452 int sel = GetSelection();
453 if (sel != -1)
454 {
455 count = 1;
456 selections.Add(sel);
457 }
d90879fa
VZ
458 }
459
460 for ( int i = 0; i < count; i++ )
461 {
462 wxCheckListBoxItem *item = GetItem(selections[i]);
463 if ( !item )
464 {
465 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
466 continue;
467 }
468
469 switch ( oper )
470 {
471 case Toggle:
472 item->Toggle();
473 break;
474
475 case Set:
476 case Clear:
477 item->Check( oper == Set );
478 break;
479
480 default:
481 wxFAIL_MSG( _T("what should this key do?") );
482 }
e2ca995f
VZ
483
484 // we should send an event as this has been done by the user and
485 // not by the program
486 item->SendEvent();
d90879fa
VZ
487 }
488 }
489 else // nothing to do
490 {
491 event.Skip();
492 }
2bda0e17
KB
493}
494
495void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
496{
aa61d352
VZ
497 // clicking on the item selects it, clicking on the checkmark toggles
498 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() )
499 {
500 int nItem = HitTest(event.GetX(), event.GetY());
501
502 if ( nItem != wxNOT_FOUND )
503 {
504 wxCheckListBoxItem *item = GetItem(nItem);
505 item->Toggle();
506 item->SendEvent();
507 }
508 //else: it's not an error, just click outside of client zone
509 }
510 else
511 {
512 // implement default behaviour: clicking on the item selects it
513 event.Skip();
e2ca995f 514 }
2bda0e17 515}
1c089c47 516
15c3723c
VZ
517int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
518{
15c3723c
VZ
519 int nItem = (int)::SendMessage
520 (
521 (HWND)GetHWND(),
522 LB_ITEMFROMPOINT,
523 0,
524 MAKELPARAM(x, y)
525 );
15c3723c 526
aa61d352 527 return nItem >= (int)m_noItems ? wxNOT_FOUND : nItem;
15c3723c
VZ
528}
529
6d50c6a7
RD
530
531wxSize wxCheckListBox::DoGetBestSize() const
532{
533 wxSize best = wxListBox::DoGetBestSize();
534 best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox
31582e4e 535 CacheBestSize(best);
6d50c6a7
RD
536 return best;
537}
538
1c089c47 539#endif