]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checklst.cpp
SourceForge patch #654210 to fix naming/numbering shared libs under OS X
[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>
9// Licence: wxWindows license
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
84 // simple accessors
85 bool IsChecked() const { return m_bChecked; }
dd3c394a
VZ
86 void Check(bool bCheck);
87 void Toggle() { Check(!IsChecked()); }
2bda0e17
KB
88
89private:
90 bool m_bChecked;
91 wxCheckListBox *m_pParent;
c86f1403 92 size_t m_nIndex;
2bda0e17
KB
93};
94
c86f1403 95wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
2b5f62a0 96 : wxOwnerDrawn(wxEmptyString, TRUE) // checkable
2bda0e17
KB
97{
98 m_bChecked = FALSE;
99 m_pParent = pParent;
100 m_nIndex = nIndex;
101
102 // we don't initialize m_nCheckHeight/Width vars because it's
103 // done in OnMeasure while they are used only in OnDraw and we
104 // know that there will always be OnMeasure before OnDraw
105
106 // fix appearance
2bda0e17
KB
107 SetMarginWidth(GetDefaultMarginWidth());
108}
109
110/*
111 * JACS - I've got the owner-draw stuff partially working with WIN16,
112 * with a really horrible-looking cross for wxCheckListBox instead of a
113 * check - could use a bitmap check-mark instead, defined in wx.rc.
114 * Also there's a refresh problem whereby it doesn't always draw the
115 * check until you click to the right of it, which is OK for WIN32.
116 */
117
45187197 118bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
2bda0e17
KB
119 wxODAction act, wxODStatus stat)
120{
121 if ( IsChecked() )
122 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
123
124 if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) {
125 // ## using native API for performance and precision
c86f1403 126 size_t nCheckWidth = GetDefaultMarginWidth(),
2bda0e17 127 nCheckHeight = m_pParent->GetItemHeight();
45187197
RD
128
129 int x = rc.GetX(),
2bda0e17
KB
130 y = rc.GetY();
131
132 HDC hdc = (HDC)dc.GetHDC();
133
134 // create pens
135 HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)),
136 hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)),
45187197 137 hpenPrev = (HPEN)SelectObject(hdc, hpenBack);
2bda0e17
KB
138
139 // we erase the 1-pixel border
140 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
141
142 // shift check mark 1 pixel to the right (it looks better like this)
143 x++;
144
145 if ( IsChecked() ) {
146 // first create a monochrome bitmap in a memory DC
147 HDC hdcMem = CreateCompatibleDC(hdc);
148 HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0);
45187197 149 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck);
2bda0e17
KB
150
151 // then draw a check mark into it
ee6e1b1d
VZ
152#if defined(__WIN32__) && !defined(__SC__)
153 RECT rect;
154 rect.left = 0;
155 rect.top = 0;
156 rect.right = nCheckWidth;
157 rect.bottom = nCheckHeight;
158
2bda0e17
KB
159 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
160#else
161 // In WIN16, draw a cross
162 HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
27a9bd48
PA
163 HPEN whiteBrush = (HPEN)GetStockObject(WHITE_BRUSH);
164 HPEN hPenOld = (HPEN)::SelectObject(hdcMem, blackPen);
165 HPEN hBrushOld = (HPEN)::SelectObject(hdcMem, whiteBrush);
2bda0e17
KB
166 ::SetROP2(hdcMem, R2_COPYPEN);
167 Rectangle(hdcMem, 0, 0, nCheckWidth, nCheckHeight);
168 MoveTo(hdcMem, 0, 0);
169 LineTo(hdcMem, nCheckWidth, nCheckHeight);
170 MoveTo(hdcMem, nCheckWidth, 0);
171 LineTo(hdcMem, 0, nCheckHeight);
172 ::SelectObject(hdcMem, hPenOld);
173 ::SelectObject(hdcMem, hBrushOld);
174 ::DeleteObject(blackPen);
175#endif
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
dd3c394a
VZ
223 // index may be chanegd because new items were added/deleted
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);
0c35333e 256
dd3c394a
VZ
257 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
258 event.SetInt(m_nIndex);
259 event.SetEventObject(m_pParent);
260 m_pParent->ProcessCommand(event);
2bda0e17
KB
261}
262
263// ----------------------------------------------------------------------------
264// implementation of wxCheckListBox class
265// ----------------------------------------------------------------------------
266
267// define event table
268// ------------------
269BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
d90879fa 270 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
2bda0e17
KB
271 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
272END_EVENT_TABLE()
273
274// control creation
275// ----------------
276
277// def ctor: use Create() to really create the control
d90879fa 278wxCheckListBox::wxCheckListBox()
2bda0e17
KB
279{
280}
281
282// ctor which creates the associated control
debe6624 283wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
2bda0e17 284 const wxPoint& pos, const wxSize& size,
debe6624
JS
285 int nStrings, const wxString choices[],
286 long style, const wxValidator& val,
dd3c394a 287 const wxString& name)
2bda0e17 288{
799cea00 289 Create(parent, id, pos, size, nStrings, choices, style, val, name);
dd3c394a
VZ
290}
291
799cea00
VS
292bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
293 const wxPoint& pos, const wxSize& size,
294 int n, const wxString choices[],
295 long style,
296 const wxValidator& validator, const wxString& name)
297{
298 return wxListBox::Create(parent, id, pos, size, n, choices,
299 style | wxLB_OWNERDRAW, validator, name);
300}
301
302
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
388 switch ( event.KeyCode() )
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;
411 int count;
412 if ( HasMultipleSelection() )
413 {
414 count = GetSelections(selections);
415 }
416 else
417 {
418 count = 1;
419 selections.Add(GetSelection());
420 }
421
422 for ( int i = 0; i < count; i++ )
423 {
424 wxCheckListBoxItem *item = GetItem(selections[i]);
425 if ( !item )
426 {
427 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
428 continue;
429 }
430
431 switch ( oper )
432 {
433 case Toggle:
434 item->Toggle();
435 break;
436
437 case Set:
438 case Clear:
439 item->Check( oper == Set );
440 break;
441
442 default:
443 wxFAIL_MSG( _T("what should this key do?") );
444 }
445 }
446 }
447 else // nothing to do
448 {
449 event.Skip();
450 }
2bda0e17
KB
451}
452
453void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
454{
455 // clicking on the item selects it, clicking on the checkmark toggles
c1888b05 456 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
15c3723c 457 int nItem = HitTest(event.GetX(), event.GetY());
c4dbfe14 458
15c3723c 459 if ( nItem != wxNOT_FOUND )
2bda0e17
KB
460 GetItem(nItem)->Toggle();
461 //else: it's not an error, just click outside of client zone
462 }
463 else {
464 // implement default behaviour: clicking on the item selects it
465 event.Skip();
466 }
467}
1c089c47 468
15c3723c
VZ
469int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
470{
471 #ifdef __WIN32__
472 int nItem = (int)::SendMessage
473 (
474 (HWND)GetHWND(),
475 LB_ITEMFROMPOINT,
476 0,
477 MAKELPARAM(x, y)
478 );
479 #else // Win16
480 // FIXME this doesn't work when the listbox is scrolled!
481 int nItem = y / m_nItemHeight;
482 #endif // Win32/16
483
484 return nItem >= m_noItems ? wxNOT_FOUND : nItem;
485}
486
1c089c47
JS
487#endif
488