]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/checklst.cpp
xti additions / changes, trying to reduce dependencies
[wxWidgets.git] / src / msw / checklst.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/checklst.cpp
3// Purpose: implementation of wxCheckListBox class
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 16.11.97
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
31#if wxUSE_OWNER_DRAWN
32
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
47#include "wx/ownerdrw.h"
48#include "wx/checklst.h"
49
50#include "wx/msw/wrapwin.h"
51#include <windowsx.h>
52
53#include "wx/msw/private.h"
54
55#if defined(__GNUWIN32_OLD__)
56 #include "wx/msw/gnuwin32/extra.h"
57#endif
58
59// ----------------------------------------------------------------------------
60// private functions
61// ----------------------------------------------------------------------------
62
63// get item (converted to right type)
64#define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
65
66// ============================================================================
67// implementation
68// ============================================================================
69
70IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
71
72/*
73TODO PROPERTIES
74 list content
75 item , checked (no)
76*/
77// ----------------------------------------------------------------------------
78// declaration and implementation of wxCheckListBoxItem class
79// ----------------------------------------------------------------------------
80
81class wxCheckListBoxItem : public wxOwnerDrawn
82{
83friend class WXDLLEXPORT wxCheckListBox;
84public:
85 // ctor
86 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
87
88 // drawing functions
89 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
90
91 // simple accessors and operations
92 bool IsChecked() const { return m_bChecked; }
93
94 void Check(bool bCheck);
95 void Toggle() { Check(!IsChecked()); }
96
97 void SendEvent();
98
99private:
100
101 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem)
102 bool m_bChecked;
103 wxCheckListBox *m_pParent;
104 size_t m_nIndex;
105};
106
107wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
108 : wxOwnerDrawn(wxEmptyString, TRUE) // checkable
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
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
130bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
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
138 size_t nCheckWidth = GetDefaultMarginWidth(),
139 nCheckHeight = m_pParent->GetItemHeight();
140
141 int x = rc.GetX(),
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)),
149 hpenPrev = (HPEN)SelectObject(hdc, hpenBack);
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);
161 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck);
162
163 // then draw a check mark into it
164
165 RECT rect;
166 rect.left = 0;
167 rect.top = 0;
168 rect.right = nCheckWidth;
169 rect.bottom = nCheckHeight;
170
171#ifdef __WXWINCE__
172 DrawFrameControl(hdcMem, &rect, DFC_BUTTON, DFCS_BUTTONCHECK);
173#else
174 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
175#endif
176
177 // finally copy it to screen DC and clean up
178 BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight,
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);
193 HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
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
219void wxCheckListBoxItem::Check(bool check)
220{
221 m_bChecked = check;
222
223 // index may be changed 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
229 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
230
231 m_nIndex = (size_t)index;
232 }
233
234 HWND hwndListbox = (HWND)m_pParent->GetHWND();
235
236 #ifdef __WIN32__
237 RECT rcUpdate;
238
239 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
240 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
241 {
242 wxLogDebug(wxT("LB_GETITEMRECT failed"));
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;
248 RECT rcUpdate ;
249 rcUpdate.left = 0 ;
250 rcUpdate.top = y ;
251 rcUpdate.right = GetDefaultMarginWidth() ;
252 rcUpdate.bottom = y + nHeight ;
253 #endif // Win32/16
254
255 InvalidateRect(hwndListbox, &rcUpdate, FALSE);
256}
257
258// send an "item checked" event
259void wxCheckListBoxItem::SendEvent()
260{
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);
265}
266
267// ----------------------------------------------------------------------------
268// implementation of wxCheckListBox class
269// ----------------------------------------------------------------------------
270
271// define event table
272// ------------------
273BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
274 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
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
282wxCheckListBox::wxCheckListBox()
283{
284}
285
286// ctor which creates the associated control
287wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
288 const wxPoint& pos, const wxSize& size,
289 int nStrings, const wxString choices[],
290 long style, const wxValidator& val,
291 const wxString& name)
292{
293 Create(parent, id, pos, size, nStrings, choices, style, val, name);
294}
295
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}
305
306// misc overloaded methods
307// -----------------------
308
309void wxCheckListBox::Delete(int N)
310{
311 wxCHECK_RET( N >= 0 && N < m_noItems,
312 wxT("invalid index in wxListBox::Delete") );
313
314 wxListBox::Delete(N);
315
316 // free memory
317 delete m_aItems[N];
318
319 m_aItems.RemoveAt(N);
320}
321
322bool wxCheckListBox::SetFont( const wxFont &font )
323{
324 size_t i;
325 for ( i = 0; i < m_aItems.GetCount(); i++ )
326 m_aItems[i]->SetFont(font);
327
328 wxListBox::SetFont(font);
329
330 return TRUE;
331}
332
333// create/retrieve item
334// --------------------
335
336// create a check list box item
337wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
338{
339 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
340 return pItem;
341}
342
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
365bool wxCheckListBox::IsChecked(size_t uiIndex) const
366{
367 wxCHECK_MSG( uiIndex < (size_t)GetCount(), FALSE, _T("bad wxCheckListBox index") );
368
369 return GetItem(uiIndex)->IsChecked();
370}
371
372void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
373{
374 wxCHECK_RET( uiIndex < (size_t)GetCount(), _T("bad wxCheckListBox index") );
375
376 GetItem(uiIndex)->Check(bCheck);
377}
378
379// process events
380// --------------
381
382void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
383{
384 // what do we do?
385 enum
386 {
387 None,
388 Toggle,
389 Set,
390 Clear
391 } oper;
392
393 switch ( event.GetKeyCode() )
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;
416 int count = 0;
417 if ( HasMultipleSelection() )
418 {
419 count = GetSelections(selections);
420 }
421 else
422 {
423 int sel = GetSelection();
424 if (sel != -1)
425 {
426 count = 1;
427 selections.Add(sel);
428 }
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 }
454
455 // we should send an event as this has been done by the user and
456 // not by the program
457 item->SendEvent();
458 }
459 }
460 else // nothing to do
461 {
462 event.Skip();
463 }
464}
465
466void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
467{
468 // clicking on the item selects it, clicking on the checkmark toggles
469 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
470 int nItem = HitTest(event.GetX(), event.GetY());
471
472 if ( nItem != wxNOT_FOUND ) {
473 wxCheckListBoxItem *item = GetItem(nItem);
474 item->Toggle();
475 item->SendEvent();
476 }
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}
484
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
503#endif
504