compilation fix for PCH-less compilation after last commit
[wxWidgets.git] / src / msw / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 #include "wx/checklst.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h"
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"
40 #include "wx/settings.h"
41 #include "wx/log.h"
42 #endif
43
44 #include "wx/ownerdrw.h"
45
46 #include <windowsx.h>
47
48 #include "wx/renderer.h"
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
64 WX_DEFINE_FLAGS( wxCheckListBoxStyle )
65
66 wxBEGIN_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
103 wxEND_FLAGS( wxCheckListBoxStyle )
104
105 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox, wxListBox,"wx/checklst.h")
106
107 wxBEGIN_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
110 wxEND_PROPERTIES_TABLE()
111
112 wxBEGIN_HANDLERS_TABLE(wxCheckListBox)
113 wxEND_HANDLERS_TABLE()
114
115 wxCONSTRUCTOR_4( wxCheckListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
116
117 #else
118 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
119 #endif
120
121 // ----------------------------------------------------------------------------
122 // declaration and implementation of wxCheckListBoxItem class
123 // ----------------------------------------------------------------------------
124
125 class wxCheckListBoxItem : public wxOwnerDrawn
126 {
127 friend class WXDLLEXPORT wxCheckListBox;
128 public:
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
143 private:
144 bool m_bChecked;
145 wxCheckListBox *m_pParent;
146 size_t m_nIndex;
147
148 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem)
149 };
150
151 wxCheckListBoxItem::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 SetMarginWidth(::GetSystemMetrics(SM_CXMENUCHECK) - 2);
163
164 SetBackgroundColour(pParent->GetBackgroundColour());
165 }
166
167 bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
168 wxODAction act, wxODStatus stat)
169 {
170 // first draw the label
171 if ( IsChecked() )
172 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
173
174 if ( !wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) )
175 return false;
176
177 // now draw the check mark part
178 HDC hdc = GetHdcOf(dc);
179
180 int nBmpWidth = ::GetSystemMetrics(SM_CXMENUCHECK),
181 nBmpHeight = ::GetSystemMetrics(SM_CYMENUCHECK);
182
183
184 // first create bitmap in a memory DC
185 MemoryHDC hdcMem(hdc);
186 CompatibleBitmap hBmpCheck(hdc, nBmpWidth, nBmpHeight);
187
188 // then draw a check mark into it
189 {
190 SelectInHDC selBmp(hdcMem, hBmpCheck);
191
192 int flags = wxCONTROL_FLAT;
193 if ( IsChecked() )
194 flags |= wxCONTROL_CHECKED;
195
196 wxDCTemp dcMem(hdcMem);
197 wxRendererNative::Get().DrawCheckBox(
198 m_pParent, dcMem, wxRect(0, 0, nBmpWidth, nBmpHeight), flags);
199 } // select hBmpCheck out of hdcMem
200
201 // shift check mark 2 pixel to the right and bottom, looks better like this
202 int x = rc.GetX() + 2,
203 y = rc.GetY() + 2;
204
205 // finally draw bitmap to screen: uses image list functions to blend
206 // the bitmap with the background colour (better for the selected items)
207 HIMAGELIST himl = ImageList_Create(nBmpWidth, nBmpHeight,
208 ILC_COLOR32 | ILC_MASK, 1, 1);
209 ImageList_Add(himl, hBmpCheck, NULL);
210
211 UINT fStyle = stat & wxOwnerDrawn::wxODSelected ? ILD_SELECTED : ILD_NORMAL;
212 ImageList_Draw(himl, 0, hdc, x, y, fStyle);
213
214 ImageList_Destroy(himl);
215
216 return true;
217 }
218
219 // change the state of the item and redraw it
220 void wxCheckListBoxItem::Check(bool check)
221 {
222 m_bChecked = check;
223
224 // index may be changed because new items were added/deleted
225 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
226 {
227 // update it
228 int index = m_pParent->GetItemIndex(this);
229
230 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
231
232 m_nIndex = (size_t)index;
233 }
234
235 HWND hwndListbox = (HWND)m_pParent->GetHWND();
236
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
245 ::InvalidateRect(hwndListbox, &rcUpdate, FALSE);
246 }
247
248 // send an "item checked" event
249 void wxCheckListBoxItem::SendEvent()
250 {
251 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
252 event.SetInt(m_nIndex);
253 event.SetEventObject(m_pParent);
254 m_pParent->ProcessCommand(event);
255 }
256
257 // ----------------------------------------------------------------------------
258 // implementation of wxCheckListBox class
259 // ----------------------------------------------------------------------------
260
261 // define event table
262 // ------------------
263 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
264 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
265 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
266 END_EVENT_TABLE()
267
268 // control creation
269 // ----------------
270
271 // def ctor: use Create() to really create the control
272 wxCheckListBox::wxCheckListBox()
273 {
274 }
275
276 // ctor which creates the associated control
277 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
278 const wxPoint& pos, const wxSize& size,
279 int nStrings, const wxString choices[],
280 long style, const wxValidator& val,
281 const wxString& name)
282 {
283 Create(parent, id, pos, size, nStrings, choices, style, val, name);
284 }
285
286 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
287 const wxPoint& pos, const wxSize& size,
288 const wxArrayString& choices,
289 long style, const wxValidator& val,
290 const wxString& name)
291 {
292 Create(parent, id, pos, size, choices, style, val, name);
293 }
294
295 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
296 const wxPoint& pos, const wxSize& size,
297 int n, const wxString choices[],
298 long style,
299 const wxValidator& validator, const wxString& name)
300 {
301 return wxListBox::Create(parent, id, pos, size, n, choices,
302 style | wxLB_OWNERDRAW, validator, name);
303 }
304
305 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
306 const wxPoint& pos, const wxSize& size,
307 const wxArrayString& choices,
308 long style,
309 const wxValidator& validator, const wxString& name)
310 {
311 return wxListBox::Create(parent, id, pos, size, choices,
312 style | wxLB_OWNERDRAW, validator, name);
313 }
314
315 // misc overloaded methods
316 // -----------------------
317
318 void wxCheckListBox::Delete(unsigned int n)
319 {
320 wxCHECK_RET( IsValid(n),
321 wxT("invalid index in wxListBox::Delete") );
322
323 wxListBox::Delete(n);
324
325 // free memory
326 delete m_aItems[n];
327
328 m_aItems.RemoveAt(n);
329 }
330
331 bool wxCheckListBox::SetFont( const wxFont &font )
332 {
333 unsigned int i;
334 for ( i = 0; i < m_aItems.GetCount(); i++ )
335 m_aItems[i]->SetFont(font);
336
337 wxListBox::SetFont(font);
338
339 return true;
340 }
341
342 // create/retrieve item
343 // --------------------
344
345 // create a check list box item
346 wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
347 {
348 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
349 return pItem;
350 }
351
352 // return item size
353 // ----------------
354 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
355 {
356 if ( wxListBox::MSWOnMeasure(item) ) {
357 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
358
359 // add place for the check mark
360 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
361 pStruct->itemHeight += 1;
362
363 // save item height
364 m_nItemHeight = pStruct->itemHeight;
365
366 return true;
367 }
368
369 return false;
370 }
371
372 // check items
373 // -----------
374
375 bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
376 {
377 wxCHECK_MSG( IsValid(uiIndex), false, _T("bad wxCheckListBox index") );
378
379 return GetItem(uiIndex)->IsChecked();
380 }
381
382 void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
383 {
384 wxCHECK_RET( IsValid(uiIndex), _T("bad wxCheckListBox index") );
385
386 GetItem(uiIndex)->Check(bCheck);
387 }
388
389 // process events
390 // --------------
391
392 void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
393 {
394 // what do we do?
395 enum
396 {
397 None,
398 Toggle,
399 Set,
400 Clear
401 } oper;
402
403 switch ( event.GetKeyCode() )
404 {
405 case WXK_SPACE:
406 oper = Toggle;
407 break;
408
409 case WXK_NUMPAD_ADD:
410 case '+':
411 oper = Set;
412 break;
413
414 case WXK_NUMPAD_SUBTRACT:
415 case '-':
416 oper = Clear;
417 break;
418
419 default:
420 oper = None;
421 }
422
423 if ( oper != None )
424 {
425 wxArrayInt selections;
426 int count = 0;
427 if ( HasMultipleSelection() )
428 {
429 count = GetSelections(selections);
430 }
431 else
432 {
433 int sel = GetSelection();
434 if (sel != -1)
435 {
436 count = 1;
437 selections.Add(sel);
438 }
439 }
440
441 for ( int i = 0; i < count; i++ )
442 {
443 wxCheckListBoxItem *item = GetItem(selections[i]);
444 if ( !item )
445 {
446 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
447 continue;
448 }
449
450 switch ( oper )
451 {
452 case Toggle:
453 item->Toggle();
454 break;
455
456 case Set:
457 case Clear:
458 item->Check( oper == Set );
459 break;
460
461 default:
462 wxFAIL_MSG( _T("what should this key do?") );
463 }
464
465 // we should send an event as this has been done by the user and
466 // not by the program
467 item->SendEvent();
468 }
469 }
470 else // nothing to do
471 {
472 event.Skip();
473 }
474 }
475
476 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
477 {
478 // clicking on the item selects it, clicking on the checkmark toggles
479 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() )
480 {
481 int nItem = HitTest(event.GetX(), event.GetY());
482
483 if ( nItem != wxNOT_FOUND )
484 {
485 wxCheckListBoxItem *item = GetItem(nItem);
486 item->Toggle();
487 item->SendEvent();
488 }
489 //else: it's not an error, just click outside of client zone
490 }
491 else
492 {
493 // implement default behaviour: clicking on the item selects it
494 event.Skip();
495 }
496 }
497
498 int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
499 {
500 int nItem = (int)::SendMessage
501 (
502 (HWND)GetHWND(),
503 LB_ITEMFROMPOINT,
504 0,
505 MAKELPARAM(x, y)
506 );
507
508 return nItem >= (int)m_noItems ? wxNOT_FOUND : nItem;
509 }
510
511
512 wxSize wxCheckListBox::DoGetBestSize() const
513 {
514 wxSize best = wxListBox::DoGetBestSize();
515 best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox
516 CacheBestSize(best);
517 return best;
518 }
519
520 #endif