]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checklst.cpp
Output the extracted number from wxString::ToXXX() even if it returns false.
[wxWidgets.git] / src / msw / checklst.cpp
CommitLineData
2bda0e17 1///////////////////////////////////////////////////////////////////////////////
8228b893 2// Name: src/msw/checklst.cpp
2bda0e17
KB
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>
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10///////////////////////////////////////////////////////////////////////////////
11
dd3c394a
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
e6e83933 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
eecdb000 27#if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN
1c089c47 28
e6e83933
WS
29#include "wx/checklst.h"
30
d90879fa 31#ifndef WX_PRECOMP
4a36f6b0 32 #include "wx/msw/wrapcctl.h"
d90879fa
VZ
33 #include "wx/object.h"
34 #include "wx/colour.h"
35 #include "wx/font.h"
36 #include "wx/bitmap.h"
37 #include "wx/window.h"
38 #include "wx/listbox.h"
39 #include "wx/dcmemory.h"
d90879fa 40 #include "wx/settings.h"
d90879fa
VZ
41 #include "wx/log.h"
42#endif
43
2bda0e17 44#include "wx/ownerdrw.h"
2bda0e17 45
dd3c394a
VZ
46#include <windowsx.h>
47
ff0d9d83 48#include "wx/renderer.h"
4676948b 49#include "wx/msw/private.h"
74052fe8 50#include "wx/msw/dc.h"
4676948b 51
dd3c394a
VZ
52// ----------------------------------------------------------------------------
53// private functions
54// ----------------------------------------------------------------------------
55
56// get item (converted to right type)
57#define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
3d05544e 58
2bda0e17 59// ============================================================================
6463b9f5 60// implementation
2bda0e17
KB
61// ============================================================================
62
a2825c34
SC
63
64#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
65WX_DEFINE_FLAGS( wxCheckListBoxStyle )
66
3ff066a4 67wxBEGIN_FLAGS( wxCheckListBoxStyle )
bc9fb572
JS
68 // new style border flags, we put them first to
69 // use them for streaming out
3ff066a4
SC
70 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
71 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
72 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
73 wxFLAGS_MEMBER(wxBORDER_RAISED)
74 wxFLAGS_MEMBER(wxBORDER_STATIC)
75 wxFLAGS_MEMBER(wxBORDER_NONE)
02b7b6b0 76
bc9fb572 77 // old style border flags
3ff066a4
SC
78 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
79 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
80 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
81 wxFLAGS_MEMBER(wxRAISED_BORDER)
82 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 83 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
84
85 // standard window styles
3ff066a4
SC
86 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
87 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
88 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
89 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 90 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
91 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
92 wxFLAGS_MEMBER(wxVSCROLL)
93 wxFLAGS_MEMBER(wxHSCROLL)
94
95 wxFLAGS_MEMBER(wxLB_SINGLE)
96 wxFLAGS_MEMBER(wxLB_MULTIPLE)
97 wxFLAGS_MEMBER(wxLB_EXTENDED)
98 wxFLAGS_MEMBER(wxLB_HSCROLL)
99 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
100 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
101 wxFLAGS_MEMBER(wxLB_SORT)
a2825c34 102 wxFLAGS_MEMBER(wxLB_OWNERDRAW)
3ff066a4
SC
103
104wxEND_FLAGS( wxCheckListBoxStyle )
bc9fb572
JS
105
106IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox, wxListBox,"wx/checklst.h")
107
3ff066a4 108wxBEGIN_PROPERTIES_TABLE(wxCheckListBox)
02b7b6b0 109 wxEVENT_PROPERTY( Toggle , wxEVT_COMMAND_CHECKLISTBOX_TOGGLED , wxCommandEvent )
af498247 110 wxPROPERTY_FLAGS( WindowStyle , wxCheckListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , wxLB_OWNERDRAW /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 111wxEND_PROPERTIES_TABLE()
bc9fb572 112
a2825c34
SC
113wxBEGIN_HANDLERS_TABLE(wxCheckListBox)
114wxEND_HANDLERS_TABLE()
115
02b7b6b0 116wxCONSTRUCTOR_4( wxCheckListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
a2825c34 117
bc9fb572 118#else
fd7ab28c 119IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
bc9fb572 120#endif
2bda0e17
KB
121
122// ----------------------------------------------------------------------------
123// declaration and implementation of wxCheckListBoxItem class
124// ----------------------------------------------------------------------------
125
126class wxCheckListBoxItem : public wxOwnerDrawn
127{
f5851311 128friend class WXDLLIMPEXP_FWD_CORE wxCheckListBox;
2bda0e17 129public:
aa61d352
VZ
130 // ctor
131 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
2bda0e17 132
aa61d352
VZ
133 // drawing functions
134 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
2bda0e17 135
aa61d352
VZ
136 // simple accessors and operations
137 bool IsChecked() const { return m_bChecked; }
e2ca995f 138
aa61d352
VZ
139 void Check(bool bCheck);
140 void Toggle() { Check(!IsChecked()); }
2bda0e17 141
aa61d352 142 void SendEvent();
e2ca995f 143
2bda0e17 144private:
aa61d352
VZ
145 bool m_bChecked;
146 wxCheckListBox *m_pParent;
ff0d9d83 147 size_t m_nIndex;
0d098130 148
c0c133e1 149 wxDECLARE_NO_COPY_CLASS(wxCheckListBoxItem);
2bda0e17
KB
150};
151
c86f1403 152wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
02b7b6b0 153 : wxOwnerDrawn(wxEmptyString, true) // checkable
2bda0e17 154{
aa61d352
VZ
155 m_bChecked = false;
156 m_pParent = pParent;
157 m_nIndex = nIndex;
158
159 // we don't initialize m_nCheckHeight/Width vars because it's
160 // done in OnMeasure while they are used only in OnDraw and we
161 // know that there will always be OnMeasure before OnDraw
162
ff0d9d83
VZ
163 SetMarginWidth(::GetSystemMetrics(SM_CXMENUCHECK) - 2);
164
aa61d352 165 SetBackgroundColour(pParent->GetBackgroundColour());
2bda0e17
KB
166}
167
45187197 168bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
2bda0e17
KB
169 wxODAction act, wxODStatus stat)
170{
0d098130
VZ
171 // first draw the label
172 if ( IsChecked() )
173 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
174
175 if ( !wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) )
176 return false;
2bda0e17 177
888dde65 178 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
0d098130 179 // now draw the check mark part
888dde65 180 HDC hdc = GetHdcOf(*impl);
45187197 181
ff0d9d83
VZ
182 int nBmpWidth = ::GetSystemMetrics(SM_CXMENUCHECK),
183 nBmpHeight = ::GetSystemMetrics(SM_CYMENUCHECK);
2bda0e17 184
2bda0e17 185
ff0d9d83
VZ
186 // first create bitmap in a memory DC
187 MemoryHDC hdcMem(hdc);
188 CompatibleBitmap hBmpCheck(hdc, nBmpWidth, nBmpHeight);
2bda0e17 189
ff0d9d83
VZ
190 // then draw a check mark into it
191 {
192 SelectInHDC selBmp(hdcMem, hBmpCheck);
2bda0e17 193
ff0d9d83
VZ
194 int flags = wxCONTROL_FLAT;
195 if ( IsChecked() )
196 flags |= wxCONTROL_CHECKED;
2bda0e17 197
ff0d9d83
VZ
198 wxDCTemp dcMem(hdcMem);
199 wxRendererNative::Get().DrawCheckBox(
200 m_pParent, dcMem, wxRect(0, 0, nBmpWidth, nBmpHeight), flags);
201 } // select hBmpCheck out of hdcMem
ee6e1b1d 202
ff0d9d83
VZ
203 // shift check mark 2 pixel to the right and bottom, looks better like this
204 int x = rc.GetX() + 2,
205 y = rc.GetY() + 2;
2bda0e17 206
ff0d9d83
VZ
207 // finally draw bitmap to screen: uses image list functions to blend
208 // the bitmap with the background colour (better for the selected items)
209 HIMAGELIST himl = ImageList_Create(nBmpWidth, nBmpHeight,
210 ILC_COLOR32 | ILC_MASK, 1, 1);
211 ImageList_Add(himl, hBmpCheck, NULL);
2bda0e17 212
ff0d9d83
VZ
213 UINT fStyle = stat & wxOwnerDrawn::wxODSelected ? ILD_SELECTED : ILD_NORMAL;
214 ImageList_Draw(himl, 0, hdc, x, y, fStyle);
2bda0e17 215
ff0d9d83 216 ImageList_Destroy(himl);
2bda0e17 217
ab0c29ae
PC
218 if (stat & wxODHasFocus)
219 wxRendererNative::Get().DrawFocusRect(m_pParent, dc, rc);
220
02b7b6b0 221 return true;
2bda0e17
KB
222}
223
224// change the state of the item and redraw it
dd3c394a 225void wxCheckListBoxItem::Check(bool check)
45187197 226{
dd3c394a 227 m_bChecked = check;
2bda0e17 228
e2ca995f 229 // index may be changed because new items were added/deleted
dd3c394a
VZ
230 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
231 {
232 // update it
233 int index = m_pParent->GetItemIndex(this);
234
223d09f6 235 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
dd3c394a
VZ
236
237 m_nIndex = (size_t)index;
238 }
2bda0e17 239
c4dbfe14
VZ
240 HWND hwndListbox = (HWND)m_pParent->GetHWND();
241
02b7b6b0 242 RECT rcUpdate;
0c35333e 243
02b7b6b0
WS
244 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
245 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
246 {
247 wxLogDebug(wxT("LB_GETITEMRECT failed"));
248 }
c4dbfe14 249
02b7b6b0 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);
c70557da 259 event.SetString(m_pParent->GetString(m_nIndex));
dd3c394a 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
584ad2a3
MB
292wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
293 const wxPoint& pos, const wxSize& size,
294 const wxArrayString& choices,
295 long style, const wxValidator& val,
296 const wxString& name)
297{
298 Create(parent, id, pos, size, choices, style, val, name);
299}
300
799cea00
VS
301bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
302 const wxPoint& pos, const wxSize& size,
303 int n, const wxString choices[],
304 long style,
305 const wxValidator& validator, const wxString& name)
306{
a374cdc4
JS
307 return wxListBox::Create(parent, id, pos, size, n, choices,
308 style | wxLB_OWNERDRAW, validator, name);
799cea00 309}
799cea00 310
584ad2a3
MB
311bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
312 const wxPoint& pos, const wxSize& size,
313 const wxArrayString& choices,
314 long style,
315 const wxValidator& validator, const wxString& name)
316{
a374cdc4
JS
317 return wxListBox::Create(parent, id, pos, size, choices,
318 style | wxLB_OWNERDRAW, validator, name);
584ad2a3
MB
319}
320
e2ca995f
VZ
321// misc overloaded methods
322// -----------------------
799cea00 323
aa61d352 324void wxCheckListBox::Delete(unsigned int n)
dd3c394a 325{
aa61d352 326 wxCHECK_RET( IsValid(n),
223d09f6 327 wxT("invalid index in wxListBox::Delete") );
dd3c394a 328
aa61d352 329 wxListBox::Delete(n);
dd3c394a
VZ
330
331 // free memory
aa61d352 332 delete m_aItems[n];
dd3c394a 333
aa61d352 334 m_aItems.RemoveAt(n);
dd3c394a
VZ
335}
336
0c35333e
RD
337bool wxCheckListBox::SetFont( const wxFont &font )
338{
aa61d352 339 unsigned int i;
07cf98cb 340 for ( i = 0; i < m_aItems.GetCount(); i++ )
0c35333e 341 m_aItems[i]->SetFont(font);
07cf98cb 342
0c35333e 343 wxListBox::SetFont(font);
07cf98cb 344
02b7b6b0 345 return true;
0c35333e
RD
346}
347
2bda0e17
KB
348// create/retrieve item
349// --------------------
350
351// create a check list box item
2b5f62a0 352wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
2bda0e17
KB
353{
354 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
2bda0e17
KB
355 return pItem;
356}
357
2bda0e17
KB
358// return item size
359// ----------------
360bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
361{
362 if ( wxListBox::MSWOnMeasure(item) ) {
363 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
364
ff0d9d83
VZ
365 // add place for the check mark
366 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
367 pStruct->itemHeight += 1;
368
2bda0e17
KB
369 // save item height
370 m_nItemHeight = pStruct->itemHeight;
371
02b7b6b0 372 return true;
2bda0e17
KB
373 }
374
02b7b6b0 375 return false;
2bda0e17
KB
376}
377
378// check items
379// -----------
380
aa61d352 381bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
2bda0e17 382{
9a83f860 383 wxCHECK_MSG( IsValid(uiIndex), false, wxT("bad wxCheckListBox index") );
d90879fa
VZ
384
385 return GetItem(uiIndex)->IsChecked();
2bda0e17
KB
386}
387
aa61d352 388void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
2bda0e17 389{
9a83f860 390 wxCHECK_RET( IsValid(uiIndex), wxT("bad wxCheckListBox index") );
d90879fa
VZ
391
392 GetItem(uiIndex)->Check(bCheck);
2bda0e17
KB
393}
394
395// process events
396// --------------
397
d90879fa 398void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
2bda0e17 399{
d90879fa
VZ
400 // what do we do?
401 enum
402 {
403 None,
404 Toggle,
405 Set,
406 Clear
407 } oper;
408
77e00fe9 409 switch ( event.GetKeyCode() )
d90879fa
VZ
410 {
411 case WXK_SPACE:
412 oper = Toggle;
413 break;
414
415 case WXK_NUMPAD_ADD:
416 case '+':
417 oper = Set;
418 break;
419
420 case WXK_NUMPAD_SUBTRACT:
421 case '-':
422 oper = Clear;
423 break;
424
425 default:
426 oper = None;
427 }
428
429 if ( oper != None )
430 {
431 wxArrayInt selections;
a8f473ac 432 int count = 0;
d90879fa
VZ
433 if ( HasMultipleSelection() )
434 {
435 count = GetSelections(selections);
436 }
437 else
438 {
a8f473ac
RD
439 int sel = GetSelection();
440 if (sel != -1)
441 {
442 count = 1;
443 selections.Add(sel);
444 }
d90879fa
VZ
445 }
446
447 for ( int i = 0; i < count; i++ )
448 {
449 wxCheckListBoxItem *item = GetItem(selections[i]);
450 if ( !item )
451 {
9a83f860 452 wxFAIL_MSG( wxT("no wxCheckListBoxItem?") );
d90879fa
VZ
453 continue;
454 }
455
456 switch ( oper )
457 {
458 case Toggle:
459 item->Toggle();
460 break;
461
462 case Set:
463 case Clear:
464 item->Check( oper == Set );
465 break;
466
467 default:
9a83f860 468 wxFAIL_MSG( wxT("what should this key do?") );
d90879fa 469 }
e2ca995f
VZ
470
471 // we should send an event as this has been done by the user and
472 // not by the program
473 item->SendEvent();
d90879fa
VZ
474 }
475 }
476 else // nothing to do
477 {
478 event.Skip();
479 }
2bda0e17
KB
480}
481
482void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
483{
aa61d352
VZ
484 // clicking on the item selects it, clicking on the checkmark toggles
485 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() )
486 {
487 int nItem = HitTest(event.GetX(), event.GetY());
488
489 if ( nItem != wxNOT_FOUND )
490 {
af8fc116
VZ
491 // people expect to get "kill focus" event for the currently
492 // focused control before getting events from the other controls
493 // and, equally importantly, they may prevent the focus change from
494 // taking place at all (e.g. because the old control contents is
495 // invalid and needs to be corrected) in which case we shouldn't
496 // generate this event at all
497 SetFocus();
498 if ( FindFocus() == this )
499 {
500 wxCheckListBoxItem *item = GetItem(nItem);
501 item->Toggle();
502 item->SendEvent();
503 }
aa61d352
VZ
504 }
505 //else: it's not an error, just click outside of client zone
506 }
507 else
508 {
509 // implement default behaviour: clicking on the item selects it
510 event.Skip();
e2ca995f 511 }
2bda0e17 512}
1c089c47 513
6d50c6a7
RD
514wxSize wxCheckListBox::DoGetBestSize() const
515{
516 wxSize best = wxListBox::DoGetBestSize();
517 best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox
31582e4e 518 CacheBestSize(best);
6d50c6a7
RD
519 return best;
520}
521
0a03dc7a 522#endif // wxUSE_CHECKLISTBOX