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