]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/checklst.cpp
Reverted semicolons patch
[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// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24#pragma hdrstop
25#endif
26
27#if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN
28
29#ifndef WX_PRECOMP
30 #include "wx/object.h"
31 #include "wx/colour.h"
32 #include "wx/font.h"
33 #include "wx/bitmap.h"
34 #include "wx/window.h"
35 #include "wx/listbox.h"
36 #include "wx/dcmemory.h"
37
38 #include "wx/settings.h"
39
40 #include "wx/log.h"
41#endif
42
43#include "wx/ownerdrw.h"
44#include "wx/checklst.h"
45
46#include "wx/msw/wrapwin.h"
47#include <windowsx.h>
48
49#include "wx/msw/private.h"
50
51// ----------------------------------------------------------------------------
52// private functions
53// ----------------------------------------------------------------------------
54
55// get item (converted to right type)
56#define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
57
58// ============================================================================
59// implementation
60// ============================================================================
61
62
63#if wxUSE_EXTENDED_RTTI
64WX_DEFINE_FLAGS( wxCheckListBoxStyle )
65
66wxBEGIN_FLAGS( wxCheckListBoxStyle )
67 // new style border flags, we put them first to
68 // use them for streaming out
69 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
70 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
71 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
72 wxFLAGS_MEMBER(wxBORDER_RAISED)
73 wxFLAGS_MEMBER(wxBORDER_STATIC)
74 wxFLAGS_MEMBER(wxBORDER_NONE)
75
76 // old style border flags
77 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
78 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
79 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
80 wxFLAGS_MEMBER(wxRAISED_BORDER)
81 wxFLAGS_MEMBER(wxSTATIC_BORDER)
82 wxFLAGS_MEMBER(wxBORDER)
83
84 // standard window styles
85 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
86 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
87 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
88 wxFLAGS_MEMBER(wxWANTS_CHARS)
89 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
90 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
91 wxFLAGS_MEMBER(wxVSCROLL)
92 wxFLAGS_MEMBER(wxHSCROLL)
93
94 wxFLAGS_MEMBER(wxLB_SINGLE)
95 wxFLAGS_MEMBER(wxLB_MULTIPLE)
96 wxFLAGS_MEMBER(wxLB_EXTENDED)
97 wxFLAGS_MEMBER(wxLB_HSCROLL)
98 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
99 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
100 wxFLAGS_MEMBER(wxLB_SORT)
101 wxFLAGS_MEMBER(wxLB_OWNERDRAW)
102
103wxEND_FLAGS( wxCheckListBoxStyle )
104
105IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox, wxListBox,"wx/checklst.h")
106
107wxBEGIN_PROPERTIES_TABLE(wxCheckListBox)
108 wxEVENT_PROPERTY( Toggle , wxEVT_COMMAND_CHECKLISTBOX_TOGGLED , wxCommandEvent )
109 wxPROPERTY_FLAGS( WindowStyle , wxCheckListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , wxLB_OWNERDRAW /*flags*/ , wxT("Helpstring") , wxT("group")) // style
110wxEND_PROPERTIES_TABLE()
111
112wxBEGIN_HANDLERS_TABLE(wxCheckListBox)
113wxEND_HANDLERS_TABLE()
114
115wxCONSTRUCTOR_4( wxCheckListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
116
117#else
118IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
119#endif
120
121// ----------------------------------------------------------------------------
122// declaration and implementation of wxCheckListBoxItem class
123// ----------------------------------------------------------------------------
124
125class wxCheckListBoxItem : public wxOwnerDrawn
126{
127friend class WXDLLEXPORT wxCheckListBox;
128public:
129 // ctor
130 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
131
132 // drawing functions
133 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
134
135 // simple accessors and operations
136 bool IsChecked() const { return m_bChecked; }
137
138 void Check(bool bCheck);
139 void Toggle() { Check(!IsChecked()); }
140
141 void SendEvent();
142
143private:
144 bool m_bChecked;
145 wxCheckListBox *m_pParent;
146 size_t m_nIndex;
147
148 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem)
149};
150
151wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
152 : wxOwnerDrawn(wxEmptyString, true) // checkable
153{
154 m_bChecked = false;
155 m_pParent = pParent;
156 m_nIndex = nIndex;
157
158 // we don't initialize m_nCheckHeight/Width vars because it's
159 // done in OnMeasure while they are used only in OnDraw and we
160 // know that there will always be OnMeasure before OnDraw
161
162 // fix appearance for check list boxes: they don't look quite the same as
163 // menu icons
164 SetMarginWidth(::GetSystemMetrics(SM_CXMENUCHECK) -
165 2*wxSystemSettings::GetMetric(wxSYS_EDGE_X) + 1);
166 SetBackgroundColour(pParent->GetBackgroundColour());
167}
168
169bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
170 wxODAction act, wxODStatus stat)
171{
172 // first draw the label
173 if ( IsChecked() )
174 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
175
176 if ( !wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) )
177 return false;
178
179
180 // now draw the check mark part
181 size_t nCheckWidth = GetDefaultMarginWidth(),
182 nCheckHeight = m_pParent->GetItemHeight();
183
184 int x = rc.GetX(),
185 y = rc.GetY();
186
187 HDC hdc = (HDC)dc.GetHDC();
188
189 // create pens, brushes &c
190 COLORREF colBg = ::GetSysColor(COLOR_WINDOW);
191 AutoHPEN hpenBack(colBg),
192 hpenGray(RGB(0xc0, 0xc0, 0xc0));
193
194 SelectInHDC selPen(hdc, (HGDIOBJ)hpenBack);
195 AutoHBRUSH hbrBack(colBg);
196 SelectInHDC selBrush(hdc, hbrBack);
197
198 // erase the background: it could have been filled with the selected colour
199 Rectangle(hdc, x, y, x + nCheckWidth + 1, rc.GetBottom() + 1);
200
201 // shift check mark 1 pixel to the right, looks better like this
202 x++;
203
204 if ( IsChecked() )
205 {
206 // first create a monochrome bitmap in a memory DC
207 MemoryHDC hdcMem(hdc);
208 MonoBitmap hbmpCheck(nCheckWidth, nCheckHeight);
209 SelectInHDC selBmp(hdcMem, hbmpCheck);
210
211 // then draw a check mark into it
212 RECT rect = { 0, 0, nCheckWidth, nCheckHeight };
213 ::DrawFrameControl(hdcMem, &rect,
214#ifdef __WXWINCE__
215 DFC_BUTTON, DFCS_BUTTONCHECK
216#else
217 DFC_MENU, DFCS_MENUCHECK
218#endif
219 );
220
221 // finally copy it to screen DC
222 ::BitBlt(hdc, x, y, nCheckWidth, nCheckHeight, hdcMem, 0, 0, SRCCOPY);
223 }
224
225 // now we draw the smaller rectangle
226 y++;
227 nCheckWidth -= 2;
228 nCheckHeight -= 2;
229
230 // draw hollow gray rectangle
231 (void)::SelectObject(hdc, (HGDIOBJ)hpenGray);
232
233 SelectInHDC selBrush2(hdc, ::GetStockObject(NULL_BRUSH));
234 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
235
236 return true;
237}
238
239// change the state of the item and redraw it
240void wxCheckListBoxItem::Check(bool check)
241{
242 m_bChecked = check;
243
244 // index may be changed because new items were added/deleted
245 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
246 {
247 // update it
248 int index = m_pParent->GetItemIndex(this);
249
250 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
251
252 m_nIndex = (size_t)index;
253 }
254
255 HWND hwndListbox = (HWND)m_pParent->GetHWND();
256
257 RECT rcUpdate;
258
259 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
260 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
261 {
262 wxLogDebug(wxT("LB_GETITEMRECT failed"));
263 }
264
265 ::InvalidateRect(hwndListbox, &rcUpdate, FALSE);
266}
267
268// send an "item checked" event
269void wxCheckListBoxItem::SendEvent()
270{
271 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
272 event.SetInt(m_nIndex);
273 event.SetEventObject(m_pParent);
274 m_pParent->ProcessCommand(event);
275}
276
277// ----------------------------------------------------------------------------
278// implementation of wxCheckListBox class
279// ----------------------------------------------------------------------------
280
281// define event table
282// ------------------
283BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
284 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
285 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
286END_EVENT_TABLE()
287
288// control creation
289// ----------------
290
291// def ctor: use Create() to really create the control
292wxCheckListBox::wxCheckListBox()
293{
294}
295
296// ctor which creates the associated control
297wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
298 const wxPoint& pos, const wxSize& size,
299 int nStrings, const wxString choices[],
300 long style, const wxValidator& val,
301 const wxString& name)
302{
303 Create(parent, id, pos, size, nStrings, choices, style, val, name);
304}
305
306wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
307 const wxPoint& pos, const wxSize& size,
308 const wxArrayString& choices,
309 long style, const wxValidator& val,
310 const wxString& name)
311{
312 Create(parent, id, pos, size, choices, style, val, name);
313}
314
315bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
316 const wxPoint& pos, const wxSize& size,
317 int n, const wxString choices[],
318 long style,
319 const wxValidator& validator, const wxString& name)
320{
321 return wxListBox::Create(parent, id, pos, size, n, choices,
322 style | wxLB_OWNERDRAW, validator, name);
323}
324
325bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
326 const wxPoint& pos, const wxSize& size,
327 const wxArrayString& choices,
328 long style,
329 const wxValidator& validator, const wxString& name)
330{
331 return wxListBox::Create(parent, id, pos, size, choices,
332 style | wxLB_OWNERDRAW, validator, name);
333}
334
335// misc overloaded methods
336// -----------------------
337
338void wxCheckListBox::Delete(int N)
339{
340 wxCHECK_RET( N >= 0 && N < m_noItems,
341 wxT("invalid index in wxListBox::Delete") );
342
343 wxListBox::Delete(N);
344
345 // free memory
346 delete m_aItems[N];
347
348 m_aItems.RemoveAt(N);
349}
350
351bool wxCheckListBox::SetFont( const wxFont &font )
352{
353 size_t i;
354 for ( i = 0; i < m_aItems.GetCount(); i++ )
355 m_aItems[i]->SetFont(font);
356
357 wxListBox::SetFont(font);
358
359 return true;
360}
361
362// create/retrieve item
363// --------------------
364
365// create a check list box item
366wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
367{
368 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
369 return pItem;
370}
371
372// return item size
373// ----------------
374bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
375{
376 if ( wxListBox::MSWOnMeasure(item) ) {
377 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
378
379 // save item height
380 m_nItemHeight = pStruct->itemHeight;
381
382 // add place for the check mark
383 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
384
385 return true;
386 }
387
388 return false;
389}
390
391// check items
392// -----------
393
394bool wxCheckListBox::IsChecked(size_t uiIndex) const
395{
396 wxCHECK_MSG( uiIndex < (size_t)GetCount(), false, _T("bad wxCheckListBox index") );
397
398 return GetItem(uiIndex)->IsChecked();
399}
400
401void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
402{
403 wxCHECK_RET( uiIndex < (size_t)GetCount(), _T("bad wxCheckListBox index") );
404
405 GetItem(uiIndex)->Check(bCheck);
406}
407
408// process events
409// --------------
410
411void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
412{
413 // what do we do?
414 enum
415 {
416 None,
417 Toggle,
418 Set,
419 Clear
420 } oper;
421
422 switch ( event.GetKeyCode() )
423 {
424 case WXK_SPACE:
425 oper = Toggle;
426 break;
427
428 case WXK_NUMPAD_ADD:
429 case '+':
430 oper = Set;
431 break;
432
433 case WXK_NUMPAD_SUBTRACT:
434 case '-':
435 oper = Clear;
436 break;
437
438 default:
439 oper = None;
440 }
441
442 if ( oper != None )
443 {
444 wxArrayInt selections;
445 int count = 0;
446 if ( HasMultipleSelection() )
447 {
448 count = GetSelections(selections);
449 }
450 else
451 {
452 int sel = GetSelection();
453 if (sel != -1)
454 {
455 count = 1;
456 selections.Add(sel);
457 }
458 }
459
460 for ( int i = 0; i < count; i++ )
461 {
462 wxCheckListBoxItem *item = GetItem(selections[i]);
463 if ( !item )
464 {
465 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
466 continue;
467 }
468
469 switch ( oper )
470 {
471 case Toggle:
472 item->Toggle();
473 break;
474
475 case Set:
476 case Clear:
477 item->Check( oper == Set );
478 break;
479
480 default:
481 wxFAIL_MSG( _T("what should this key do?") );
482 }
483
484 // we should send an event as this has been done by the user and
485 // not by the program
486 item->SendEvent();
487 }
488 }
489 else // nothing to do
490 {
491 event.Skip();
492 }
493}
494
495void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
496{
497 // clicking on the item selects it, clicking on the checkmark toggles
498 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
499 int nItem = HitTest(event.GetX(), event.GetY());
500
501 if ( nItem != wxNOT_FOUND ) {
502 wxCheckListBoxItem *item = GetItem(nItem);
503 item->Toggle();
504 item->SendEvent();
505 }
506 //else: it's not an error, just click outside of client zone
507 }
508 else {
509 // implement default behaviour: clicking on the item selects it
510 event.Skip();
511 }
512}
513
514int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
515{
516 int nItem = (int)::SendMessage
517 (
518 (HWND)GetHWND(),
519 LB_ITEMFROMPOINT,
520 0,
521 MAKELPARAM(x, y)
522 );
523
524 return nItem >= m_noItems ? wxNOT_FOUND : nItem;
525}
526
527
528wxSize wxCheckListBox::DoGetBestSize() const
529{
530 wxSize best = wxListBox::DoGetBestSize();
531 best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox
532 CacheBestSize(best);
533 return best;
534}
535
536#endif
537