]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checklst.cpp
More WinCE mods
[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>
6c9a19aa 9// Licence: wxWindows licence
2bda0e17
KB
10///////////////////////////////////////////////////////////////////////////////
11
dd3c394a
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20#ifdef __GNUG__
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
3d05544e 50#include <windows.h>
dd3c394a
VZ
51#include <windowsx.h>
52
ae090fdb 53#if defined(__GNUWIN32_OLD__)
c42404a5 54 #include "wx/msw/gnuwin32/extra.h"
9a05fd8d
JS
55#endif
56
dd3c394a
VZ
57// ----------------------------------------------------------------------------
58// private functions
59// ----------------------------------------------------------------------------
60
61// get item (converted to right type)
62#define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
3d05544e 63
2bda0e17
KB
64// ============================================================================
65// implementation
66// ============================================================================
67
fd7ab28c 68IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
2bda0e17
KB
69
70// ----------------------------------------------------------------------------
71// declaration and implementation of wxCheckListBoxItem class
72// ----------------------------------------------------------------------------
73
74class wxCheckListBoxItem : public wxOwnerDrawn
75{
2b5f62a0 76friend class WXDLLEXPORT wxCheckListBox;
2bda0e17
KB
77public:
78 // ctor
c86f1403 79 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
2bda0e17
KB
80
81 // drawing functions
82 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
83
e2ca995f
VZ
84 // simple accessors and operations
85 bool IsChecked() const { return m_bChecked; }
86
dd3c394a
VZ
87 void Check(bool bCheck);
88 void Toggle() { Check(!IsChecked()); }
2bda0e17 89
e2ca995f
VZ
90 void SendEvent();
91
2bda0e17 92private:
22f3361e
VZ
93
94 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem)
2bda0e17
KB
95 bool m_bChecked;
96 wxCheckListBox *m_pParent;
e2ca995f 97 size_t m_nIndex;
2bda0e17
KB
98};
99
c86f1403 100wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
2b5f62a0 101 : wxOwnerDrawn(wxEmptyString, TRUE) // checkable
2bda0e17
KB
102{
103 m_bChecked = FALSE;
104 m_pParent = pParent;
105 m_nIndex = nIndex;
106
107 // we don't initialize m_nCheckHeight/Width vars because it's
108 // done in OnMeasure while they are used only in OnDraw and we
109 // know that there will always be OnMeasure before OnDraw
110
111 // fix appearance
2bda0e17
KB
112 SetMarginWidth(GetDefaultMarginWidth());
113}
114
115/*
116 * JACS - I've got the owner-draw stuff partially working with WIN16,
117 * with a really horrible-looking cross for wxCheckListBox instead of a
118 * check - could use a bitmap check-mark instead, defined in wx.rc.
119 * Also there's a refresh problem whereby it doesn't always draw the
120 * check until you click to the right of it, which is OK for WIN32.
121 */
122
45187197 123bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
2bda0e17
KB
124 wxODAction act, wxODStatus stat)
125{
126 if ( IsChecked() )
127 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
128
129 if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) {
130 // ## using native API for performance and precision
c86f1403 131 size_t nCheckWidth = GetDefaultMarginWidth(),
2bda0e17 132 nCheckHeight = m_pParent->GetItemHeight();
45187197
RD
133
134 int x = rc.GetX(),
2bda0e17
KB
135 y = rc.GetY();
136
137 HDC hdc = (HDC)dc.GetHDC();
138
139 // create pens
140 HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)),
141 hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)),
45187197 142 hpenPrev = (HPEN)SelectObject(hdc, hpenBack);
2bda0e17
KB
143
144 // we erase the 1-pixel border
145 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
146
147 // shift check mark 1 pixel to the right (it looks better like this)
148 x++;
149
150 if ( IsChecked() ) {
151 // first create a monochrome bitmap in a memory DC
152 HDC hdcMem = CreateCompatibleDC(hdc);
153 HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0);
45187197 154 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck);
2bda0e17
KB
155
156 // then draw a check mark into it
4ce1efe1 157
ee6e1b1d
VZ
158 RECT rect;
159 rect.left = 0;
160 rect.top = 0;
161 rect.right = nCheckWidth;
162 rect.bottom = nCheckHeight;
163
2bda0e17 164 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
2bda0e17
KB
165
166 // finally copy it to screen DC and clean up
45187197 167 BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight,
2bda0e17
KB
168 hdcMem, 0, 0, SRCCOPY);
169
170 SelectObject(hdcMem, hbmpOld);
171 DeleteObject(hbmpCheck);
172 DeleteDC(hdcMem);
173 }
174
175 // now we draw the smaller rectangle
176 y++;
177 nCheckWidth -= 2;
178 nCheckHeight -= 2;
179
180 // draw hollow gray rectangle
181 (void)SelectObject(hdc, hpenGray);
45187197 182 HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
2bda0e17
KB
183 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
184
185 // clean up
186 (void)SelectObject(hdc, hpenPrev);
187 (void)SelectObject(hdc, hbrPrev);
188
189 DeleteObject(hpenBack);
190 DeleteObject(hpenGray);
191
192 /*
193 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
194
195 if ( IsChecked() ) {
196 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
197 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
198 }
199 */
200
201 return TRUE;
202 }
203
204 return FALSE;
205}
206
207// change the state of the item and redraw it
dd3c394a 208void wxCheckListBoxItem::Check(bool check)
45187197 209{
dd3c394a 210 m_bChecked = check;
2bda0e17 211
e2ca995f 212 // index may be changed because new items were added/deleted
dd3c394a
VZ
213 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
214 {
215 // update it
216 int index = m_pParent->GetItemIndex(this);
217
223d09f6 218 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
dd3c394a
VZ
219
220 m_nIndex = (size_t)index;
221 }
2bda0e17 222
c4dbfe14
VZ
223 HWND hwndListbox = (HWND)m_pParent->GetHWND();
224
225 #ifdef __WIN32__
226 RECT rcUpdate;
0c35333e 227
c4dbfe14
VZ
228 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
229 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
230 {
223d09f6 231 wxLogDebug(wxT("LB_GETITEMRECT failed"));
c4dbfe14
VZ
232 }
233 #else // Win16
234 // FIXME this doesn't work if the listbox is scrolled!
235 size_t nHeight = m_pParent->GetItemHeight();
236 size_t y = m_nIndex * nHeight;
197dd9af
PA
237 RECT rcUpdate ;
238 rcUpdate.left = 0 ;
239 rcUpdate.top = y ;
240 rcUpdate.right = GetDefaultMarginWidth() ;
241 rcUpdate.bottom = y + nHeight ;
c4dbfe14
VZ
242 #endif // Win32/16
243
244 InvalidateRect(hwndListbox, &rcUpdate, FALSE);
e2ca995f 245}
0c35333e 246
e2ca995f
VZ
247// send an "item checked" event
248void wxCheckListBoxItem::SendEvent()
249{
dd3c394a
VZ
250 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
251 event.SetInt(m_nIndex);
252 event.SetEventObject(m_pParent);
253 m_pParent->ProcessCommand(event);
2bda0e17
KB
254}
255
256// ----------------------------------------------------------------------------
257// implementation of wxCheckListBox class
258// ----------------------------------------------------------------------------
259
260// define event table
261// ------------------
262BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
d90879fa 263 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
2bda0e17
KB
264 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
265END_EVENT_TABLE()
266
267// control creation
268// ----------------
269
270// def ctor: use Create() to really create the control
d90879fa 271wxCheckListBox::wxCheckListBox()
2bda0e17
KB
272{
273}
274
275// ctor which creates the associated control
debe6624 276wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
2bda0e17 277 const wxPoint& pos, const wxSize& size,
debe6624
JS
278 int nStrings, const wxString choices[],
279 long style, const wxValidator& val,
dd3c394a 280 const wxString& name)
2bda0e17 281{
799cea00 282 Create(parent, id, pos, size, nStrings, choices, style, val, name);
dd3c394a
VZ
283}
284
799cea00
VS
285bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
286 const wxPoint& pos, const wxSize& size,
287 int n, const wxString choices[],
288 long style,
289 const wxValidator& validator, const wxString& name)
290{
291 return wxListBox::Create(parent, id, pos, size, n, choices,
292 style | wxLB_OWNERDRAW, validator, name);
293}
799cea00 294
e2ca995f
VZ
295// misc overloaded methods
296// -----------------------
799cea00 297
dd3c394a
VZ
298void wxCheckListBox::Delete(int N)
299{
300 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 301 wxT("invalid index in wxListBox::Delete") );
dd3c394a
VZ
302
303 wxListBox::Delete(N);
304
305 // free memory
306 delete m_aItems[N];
307
b54e41c5 308 m_aItems.RemoveAt(N);
dd3c394a
VZ
309}
310
0c35333e
RD
311bool wxCheckListBox::SetFont( const wxFont &font )
312{
313 size_t i;
07cf98cb 314 for ( i = 0; i < m_aItems.GetCount(); i++ )
0c35333e 315 m_aItems[i]->SetFont(font);
07cf98cb 316
0c35333e 317 wxListBox::SetFont(font);
07cf98cb 318
0c35333e
RD
319 return TRUE;
320}
321
2bda0e17
KB
322// create/retrieve item
323// --------------------
324
325// create a check list box item
2b5f62a0 326wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
2bda0e17
KB
327{
328 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
2bda0e17
KB
329 return pItem;
330}
331
2bda0e17
KB
332// return item size
333// ----------------
334bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
335{
336 if ( wxListBox::MSWOnMeasure(item) ) {
337 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
338
339 // save item height
340 m_nItemHeight = pStruct->itemHeight;
341
342 // add place for the check mark
343 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
344
345 return TRUE;
346 }
347
348 return FALSE;
349}
350
351// check items
352// -----------
353
c86f1403 354bool wxCheckListBox::IsChecked(size_t uiIndex) const
2bda0e17 355{
d90879fa
VZ
356 wxCHECK_MSG( uiIndex < (size_t)GetCount(), FALSE, _T("bad wxCheckListBox index") );
357
358 return GetItem(uiIndex)->IsChecked();
2bda0e17
KB
359}
360
c86f1403 361void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
2bda0e17 362{
d90879fa
VZ
363 wxCHECK_RET( uiIndex < (size_t)GetCount(), _T("bad wxCheckListBox index") );
364
365 GetItem(uiIndex)->Check(bCheck);
2bda0e17
KB
366}
367
368// process events
369// --------------
370
d90879fa 371void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
2bda0e17 372{
d90879fa
VZ
373 // what do we do?
374 enum
375 {
376 None,
377 Toggle,
378 Set,
379 Clear
380 } oper;
381
77e00fe9 382 switch ( event.GetKeyCode() )
d90879fa
VZ
383 {
384 case WXK_SPACE:
385 oper = Toggle;
386 break;
387
388 case WXK_NUMPAD_ADD:
389 case '+':
390 oper = Set;
391 break;
392
393 case WXK_NUMPAD_SUBTRACT:
394 case '-':
395 oper = Clear;
396 break;
397
398 default:
399 oper = None;
400 }
401
402 if ( oper != None )
403 {
404 wxArrayInt selections;
a8f473ac 405 int count = 0;
d90879fa
VZ
406 if ( HasMultipleSelection() )
407 {
408 count = GetSelections(selections);
409 }
410 else
411 {
a8f473ac
RD
412 int sel = GetSelection();
413 if (sel != -1)
414 {
415 count = 1;
416 selections.Add(sel);
417 }
d90879fa
VZ
418 }
419
420 for ( int i = 0; i < count; i++ )
421 {
422 wxCheckListBoxItem *item = GetItem(selections[i]);
423 if ( !item )
424 {
425 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
426 continue;
427 }
428
429 switch ( oper )
430 {
431 case Toggle:
432 item->Toggle();
433 break;
434
435 case Set:
436 case Clear:
437 item->Check( oper == Set );
438 break;
439
440 default:
441 wxFAIL_MSG( _T("what should this key do?") );
442 }
e2ca995f
VZ
443
444 // we should send an event as this has been done by the user and
445 // not by the program
446 item->SendEvent();
d90879fa
VZ
447 }
448 }
449 else // nothing to do
450 {
451 event.Skip();
452 }
2bda0e17
KB
453}
454
455void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
456{
457 // clicking on the item selects it, clicking on the checkmark toggles
c1888b05 458 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
15c3723c 459 int nItem = HitTest(event.GetX(), event.GetY());
c4dbfe14 460
e2ca995f
VZ
461 if ( nItem != wxNOT_FOUND ) {
462 wxCheckListBoxItem *item = GetItem(nItem);
463 item->Toggle();
464 item->SendEvent();
465 }
2bda0e17
KB
466 //else: it's not an error, just click outside of client zone
467 }
468 else {
469 // implement default behaviour: clicking on the item selects it
470 event.Skip();
471 }
472}
1c089c47 473
15c3723c
VZ
474int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
475{
476 #ifdef __WIN32__
477 int nItem = (int)::SendMessage
478 (
479 (HWND)GetHWND(),
480 LB_ITEMFROMPOINT,
481 0,
482 MAKELPARAM(x, y)
483 );
484 #else // Win16
485 // FIXME this doesn't work when the listbox is scrolled!
486 int nItem = y / m_nItemHeight;
487 #endif // Win32/16
488
489 return nItem >= m_noItems ? wxNOT_FOUND : nItem;
490}
491
1c089c47
JS
492#endif
493