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