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