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