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