]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checklst.cpp
fixed wxBitmapButton to use focus and hover bitmaps correctly; also fixed tons of...
[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 WXDLLIMPEXP_FWD_CORE 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 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
178 // now draw the check mark part
179 HDC hdc = GetHdcOf(*impl);
180
181 int nBmpWidth = ::GetSystemMetrics(SM_CXMENUCHECK),
182 nBmpHeight = ::GetSystemMetrics(SM_CYMENUCHECK);
183
184
185 // first create bitmap in a memory DC
186 MemoryHDC hdcMem(hdc);
187 CompatibleBitmap hBmpCheck(hdc, nBmpWidth, nBmpHeight);
188
189 // then draw a check mark into it
190 {
191 SelectInHDC selBmp(hdcMem, hBmpCheck);
192
193 int flags = wxCONTROL_FLAT;
194 if ( IsChecked() )
195 flags |= wxCONTROL_CHECKED;
196
197 wxDCTemp dcMem(hdcMem);
198 wxRendererNative::Get().DrawCheckBox(
199 m_pParent, dcMem, wxRect(0, 0, nBmpWidth, nBmpHeight), flags);
200 } // select hBmpCheck out of hdcMem
201
202 // shift check mark 2 pixel to the right and bottom, looks better like this
203 int x = rc.GetX() + 2,
204 y = rc.GetY() + 2;
205
206 // finally draw bitmap to screen: uses image list functions to blend
207 // the bitmap with the background colour (better for the selected items)
208 HIMAGELIST himl = ImageList_Create(nBmpWidth, nBmpHeight,
209 ILC_COLOR32 | ILC_MASK, 1, 1);
210 ImageList_Add(himl, hBmpCheck, NULL);
211
212 UINT fStyle = stat & wxOwnerDrawn::wxODSelected ? ILD_SELECTED : ILD_NORMAL;
213 ImageList_Draw(himl, 0, hdc, x, y, fStyle);
214
215 ImageList_Destroy(himl);
216
217 return true;
218 }
219
220 // change the state of the item and redraw it
221 void wxCheckListBoxItem::Check(bool check)
222 {
223 m_bChecked = check;
224
225 // index may be changed because new items were added/deleted
226 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
227 {
228 // update it
229 int index = m_pParent->GetItemIndex(this);
230
231 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
232
233 m_nIndex = (size_t)index;
234 }
235
236 HWND hwndListbox = (HWND)m_pParent->GetHWND();
237
238 RECT rcUpdate;
239
240 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
241 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
242 {
243 wxLogDebug(wxT("LB_GETITEMRECT failed"));
244 }
245
246 ::InvalidateRect(hwndListbox, &rcUpdate, FALSE);
247 }
248
249 // send an "item checked" event
250 void wxCheckListBoxItem::SendEvent()
251 {
252 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
253 event.SetInt(m_nIndex);
254 event.SetEventObject(m_pParent);
255 event.SetString(m_pParent->GetString(m_nIndex));
256 m_pParent->ProcessCommand(event);
257 }
258
259 // ----------------------------------------------------------------------------
260 // implementation of wxCheckListBox class
261 // ----------------------------------------------------------------------------
262
263 // define event table
264 // ------------------
265 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
266 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
267 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
268 END_EVENT_TABLE()
269
270 // control creation
271 // ----------------
272
273 // def ctor: use Create() to really create the control
274 wxCheckListBox::wxCheckListBox()
275 {
276 }
277
278 // ctor which creates the associated control
279 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
280 const wxPoint& pos, const wxSize& size,
281 int nStrings, const wxString choices[],
282 long style, const wxValidator& val,
283 const wxString& name)
284 {
285 Create(parent, id, pos, size, nStrings, choices, style, val, name);
286 }
287
288 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
289 const wxPoint& pos, const wxSize& size,
290 const wxArrayString& choices,
291 long style, const wxValidator& val,
292 const wxString& name)
293 {
294 Create(parent, id, pos, size, choices, style, val, name);
295 }
296
297 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
298 const wxPoint& pos, const wxSize& size,
299 int n, const wxString choices[],
300 long style,
301 const wxValidator& validator, const wxString& name)
302 {
303 return wxListBox::Create(parent, id, pos, size, n, choices,
304 style | wxLB_OWNERDRAW, validator, name);
305 }
306
307 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
308 const wxPoint& pos, const wxSize& size,
309 const wxArrayString& choices,
310 long style,
311 const wxValidator& validator, const wxString& name)
312 {
313 return wxListBox::Create(parent, id, pos, size, choices,
314 style | wxLB_OWNERDRAW, validator, name);
315 }
316
317 // misc overloaded methods
318 // -----------------------
319
320 void wxCheckListBox::Delete(unsigned int n)
321 {
322 wxCHECK_RET( IsValid(n),
323 wxT("invalid index in wxListBox::Delete") );
324
325 wxListBox::Delete(n);
326
327 // free memory
328 delete m_aItems[n];
329
330 m_aItems.RemoveAt(n);
331 }
332
333 bool wxCheckListBox::SetFont( const wxFont &font )
334 {
335 unsigned int i;
336 for ( i = 0; i < m_aItems.GetCount(); i++ )
337 m_aItems[i]->SetFont(font);
338
339 wxListBox::SetFont(font);
340
341 return true;
342 }
343
344 // create/retrieve item
345 // --------------------
346
347 // create a check list box item
348 wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
349 {
350 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
351 return pItem;
352 }
353
354 // return item size
355 // ----------------
356 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
357 {
358 if ( wxListBox::MSWOnMeasure(item) ) {
359 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
360
361 // add place for the check mark
362 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
363 pStruct->itemHeight += 1;
364
365 // save item height
366 m_nItemHeight = pStruct->itemHeight;
367
368 return true;
369 }
370
371 return false;
372 }
373
374 // check items
375 // -----------
376
377 bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
378 {
379 wxCHECK_MSG( IsValid(uiIndex), false, _T("bad wxCheckListBox index") );
380
381 return GetItem(uiIndex)->IsChecked();
382 }
383
384 void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
385 {
386 wxCHECK_RET( IsValid(uiIndex), _T("bad wxCheckListBox index") );
387
388 GetItem(uiIndex)->Check(bCheck);
389 }
390
391 // process events
392 // --------------
393
394 void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
395 {
396 // what do we do?
397 enum
398 {
399 None,
400 Toggle,
401 Set,
402 Clear
403 } oper;
404
405 switch ( event.GetKeyCode() )
406 {
407 case WXK_SPACE:
408 oper = Toggle;
409 break;
410
411 case WXK_NUMPAD_ADD:
412 case '+':
413 oper = Set;
414 break;
415
416 case WXK_NUMPAD_SUBTRACT:
417 case '-':
418 oper = Clear;
419 break;
420
421 default:
422 oper = None;
423 }
424
425 if ( oper != None )
426 {
427 wxArrayInt selections;
428 int count = 0;
429 if ( HasMultipleSelection() )
430 {
431 count = GetSelections(selections);
432 }
433 else
434 {
435 int sel = GetSelection();
436 if (sel != -1)
437 {
438 count = 1;
439 selections.Add(sel);
440 }
441 }
442
443 for ( int i = 0; i < count; i++ )
444 {
445 wxCheckListBoxItem *item = GetItem(selections[i]);
446 if ( !item )
447 {
448 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
449 continue;
450 }
451
452 switch ( oper )
453 {
454 case Toggle:
455 item->Toggle();
456 break;
457
458 case Set:
459 case Clear:
460 item->Check( oper == Set );
461 break;
462
463 default:
464 wxFAIL_MSG( _T("what should this key do?") );
465 }
466
467 // we should send an event as this has been done by the user and
468 // not by the program
469 item->SendEvent();
470 }
471 }
472 else // nothing to do
473 {
474 event.Skip();
475 }
476 }
477
478 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
479 {
480 // clicking on the item selects it, clicking on the checkmark toggles
481 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() )
482 {
483 int nItem = HitTest(event.GetX(), event.GetY());
484
485 if ( nItem != wxNOT_FOUND )
486 {
487 // people expect to get "kill focus" event for the currently
488 // focused control before getting events from the other controls
489 // and, equally importantly, they may prevent the focus change from
490 // taking place at all (e.g. because the old control contents is
491 // invalid and needs to be corrected) in which case we shouldn't
492 // generate this event at all
493 SetFocus();
494 if ( FindFocus() == this )
495 {
496 wxCheckListBoxItem *item = GetItem(nItem);
497 item->Toggle();
498 item->SendEvent();
499 }
500 }
501 //else: it's not an error, just click outside of client zone
502 }
503 else
504 {
505 // implement default behaviour: clicking on the item selects it
506 event.Skip();
507 }
508 }
509
510 int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
511 {
512 int nItem = (int)::SendMessage
513 (
514 (HWND)GetHWND(),
515 LB_ITEMFROMPOINT,
516 0,
517 MAKELPARAM(x, y)
518 );
519
520 return nItem >= (int)m_noItems ? wxNOT_FOUND : nItem;
521 }
522
523
524 wxSize wxCheckListBox::DoGetBestSize() const
525 {
526 wxSize best = wxListBox::DoGetBestSize();
527 best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox
528 CacheBestSize(best);
529 return best;
530 }
531
532 #endif