]> git.saurik.com Git - wxWidgets.git/blob - src/msw/checklst.cpp
Don't send SELECTED events for an already selected item in wxGTK wxListBox.
[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 #include "wx/msw/dc.h"
51
52 // ----------------------------------------------------------------------------
53 // private functions
54 // ----------------------------------------------------------------------------
55
56 // get item (converted to right type)
57 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
58
59 namespace
60 {
61 // space around check mark bitmap in pixels
62 static const int CHECKMARK_EXTRA_SPACE = 1;
63
64 // space betwen check bitmap and text label
65 static const int CHECKMARK_LABEL_SPACE = 2;
66
67 } // anonymous namespace
68
69 // ============================================================================
70 // implementation
71 // ============================================================================
72
73
74 #if wxUSE_EXTENDED_RTTI
75 WX_DEFINE_FLAGS( wxCheckListBoxStyle )
76
77 wxBEGIN_FLAGS( wxCheckListBoxStyle )
78 // new style border flags, we put them first to
79 // use them for streaming out
80 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
81 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
82 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
83 wxFLAGS_MEMBER(wxBORDER_RAISED)
84 wxFLAGS_MEMBER(wxBORDER_STATIC)
85 wxFLAGS_MEMBER(wxBORDER_NONE)
86
87 // old style border flags
88 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
89 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
90 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
91 wxFLAGS_MEMBER(wxRAISED_BORDER)
92 wxFLAGS_MEMBER(wxSTATIC_BORDER)
93 wxFLAGS_MEMBER(wxBORDER)
94
95 // standard window styles
96 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
97 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
98 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
99 wxFLAGS_MEMBER(wxWANTS_CHARS)
100 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
101 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
102 wxFLAGS_MEMBER(wxVSCROLL)
103 wxFLAGS_MEMBER(wxHSCROLL)
104
105 wxFLAGS_MEMBER(wxLB_SINGLE)
106 wxFLAGS_MEMBER(wxLB_MULTIPLE)
107 wxFLAGS_MEMBER(wxLB_EXTENDED)
108 wxFLAGS_MEMBER(wxLB_HSCROLL)
109 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
110 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
111 wxFLAGS_MEMBER(wxLB_SORT)
112 wxFLAGS_MEMBER(wxLB_OWNERDRAW)
113
114 wxEND_FLAGS( wxCheckListBoxStyle )
115
116 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox, wxListBox,"wx/checklst.h")
117
118 wxBEGIN_PROPERTIES_TABLE(wxCheckListBox)
119 wxEVENT_PROPERTY( Toggle , wxEVT_COMMAND_CHECKLISTBOX_TOGGLED , wxCommandEvent )
120 wxPROPERTY_FLAGS( WindowStyle , wxCheckListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , wxLB_OWNERDRAW /*flags*/ , wxT("Helpstring") , wxT("group")) // style
121 wxEND_PROPERTIES_TABLE()
122
123 wxBEGIN_HANDLERS_TABLE(wxCheckListBox)
124 wxEND_HANDLERS_TABLE()
125
126 wxCONSTRUCTOR_4( wxCheckListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
127
128 #else
129 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
130 #endif
131
132 // ----------------------------------------------------------------------------
133 // declaration and implementation of wxCheckListBoxItem class
134 // ----------------------------------------------------------------------------
135
136 class wxCheckListBoxItem : public wxOwnerDrawn
137 {
138 public:
139 // ctor
140 wxCheckListBoxItem(wxCheckListBox *parent);
141
142 // drawing functions
143 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
144
145 // simple accessors and operations
146 wxCheckListBox *GetParent() const
147 { return m_parent; }
148
149 int GetIndex() const
150 { return m_parent->GetItemIndex(const_cast<wxCheckListBoxItem*>(this)); }
151
152 wxString GetName() const
153 { return m_parent->GetString(GetIndex()); }
154
155
156 bool IsChecked() const
157 { return m_checked; }
158
159 void Check(bool bCheck)
160 { m_checked = bCheck; }
161
162 void Toggle()
163 { Check(!IsChecked()); }
164
165 private:
166 wxCheckListBox *m_parent;
167 bool m_checked;
168
169 wxDECLARE_NO_COPY_CLASS(wxCheckListBoxItem);
170 };
171
172 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *parent)
173 {
174 m_parent = parent;
175 m_checked = false;
176
177 wxSize size = wxRendererNative::Get().GetCheckBoxSize(parent);
178 size.x += 2 * CHECKMARK_EXTRA_SPACE + CHECKMARK_LABEL_SPACE;
179
180 SetMarginWidth(size.GetWidth());
181 SetBackgroundColour(parent->GetBackgroundColour());
182 }
183
184 bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
185 wxODAction act, wxODStatus stat)
186 {
187 // first draw the label
188 if ( !wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) )
189 return false;
190
191 // now draw the check mark part
192 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
193 HDC hdc = GetHdcOf(*impl);
194
195 wxSize size = wxRendererNative::Get().GetCheckBoxSize(GetParent());
196
197 // first create bitmap in a memory DC
198 MemoryHDC hdcMem(hdc);
199 CompatibleBitmap hBmpCheck(hdc, size.GetWidth(), size.GetHeight());
200
201 // then draw a check mark into it
202 {
203 SelectInHDC selBmp(hdcMem, hBmpCheck);
204
205 int flags = wxCONTROL_FLAT;
206 if ( IsChecked() )
207 flags |= wxCONTROL_CHECKED;
208
209 wxDCTemp dcMem(hdcMem);
210 wxRendererNative::Get().DrawCheckBox(GetParent(), dcMem, wxRect(size), flags);
211 } // select hBmpCheck out of hdcMem
212
213 // finally draw bitmap to screen
214
215 // position of check mark bitmap
216 int x = rc.GetX() + CHECKMARK_EXTRA_SPACE;
217 int y = rc.GetY() + (rc.GetHeight() - size.GetHeight()) / 2;
218
219 UINT uState = stat & wxOwnerDrawn::wxODSelected ? wxDSB_SELECTED : wxDSB_NORMAL;
220 wxDrawStateBitmap(hdc, hBmpCheck, x, y, uState);
221
222 return true;
223 }
224
225 // ----------------------------------------------------------------------------
226 // implementation of wxCheckListBox class
227 // ----------------------------------------------------------------------------
228
229 // define event table
230 // ------------------
231 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
232 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
233 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
234 END_EVENT_TABLE()
235
236 // control creation
237 // ----------------
238
239 // def ctor: use Create() to really create the control
240 wxCheckListBox::wxCheckListBox()
241 {
242 }
243
244 // ctor which creates the associated control
245 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
246 const wxPoint& pos, const wxSize& size,
247 int nStrings, const wxString choices[],
248 long style, const wxValidator& val,
249 const wxString& name)
250 {
251 Create(parent, id, pos, size, nStrings, choices, style, val, name);
252 }
253
254 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
255 const wxPoint& pos, const wxSize& size,
256 const wxArrayString& choices,
257 long style, const wxValidator& val,
258 const wxString& name)
259 {
260 Create(parent, id, pos, size, choices, style, val, name);
261 }
262
263 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
264 const wxPoint& pos, const wxSize& size,
265 int n, const wxString choices[],
266 long style,
267 const wxValidator& validator, const wxString& name)
268 {
269 return wxListBox::Create(parent, id, pos, size, n, choices,
270 style | wxLB_OWNERDRAW, validator, name);
271 }
272
273 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
274 const wxPoint& pos, const wxSize& size,
275 const wxArrayString& choices,
276 long style,
277 const wxValidator& validator, const wxString& name)
278 {
279 return wxListBox::Create(parent, id, pos, size, choices,
280 style | wxLB_OWNERDRAW, validator, name);
281 }
282
283 // create/retrieve item
284 // --------------------
285
286 // create a check list box item
287 wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t WXUNUSED(n))
288 {
289 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this);
290 return pItem;
291 }
292
293 // return item size
294 // ----------------
295 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
296 {
297 if ( wxListBox::MSWOnMeasure(item) )
298 {
299 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
300
301 wxSize size = wxRendererNative::Get().GetCheckBoxSize(this);
302 size.x += 2 * CHECKMARK_EXTRA_SPACE;
303 size.y += 2 * CHECKMARK_EXTRA_SPACE;
304
305 // add place for the check mark
306 pStruct->itemWidth += size.GetWidth();
307
308 if ( pStruct->itemHeight < static_cast<unsigned int>(size.GetHeight()) )
309 pStruct->itemHeight = size.GetHeight();
310
311 return true;
312 }
313
314 return false;
315 }
316
317 // check items
318 // -----------
319
320 bool wxCheckListBox::IsChecked(unsigned int uiIndex) const
321 {
322 wxCHECK_MSG( IsValid(uiIndex), false, wxT("bad wxCheckListBox index") );
323
324 return GetItem(uiIndex)->IsChecked();
325 }
326
327 void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck)
328 {
329 wxCHECK_RET( IsValid(uiIndex), wxT("bad wxCheckListBox index") );
330
331 GetItem(uiIndex)->Check(bCheck);
332 RefreshItem(uiIndex);
333 }
334
335 void wxCheckListBox::Toggle(unsigned int uiIndex)
336 {
337 wxCHECK_RET( IsValid(uiIndex), wxT("bad wxCheckListBox index") );
338
339 GetItem(uiIndex)->Toggle();
340 RefreshItem(uiIndex);
341 }
342
343 // process events
344 // --------------
345
346 void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
347 {
348 // what do we do?
349 enum
350 {
351 NONE,
352 TOGGLE,
353 SET,
354 CLEAR
355 } oper;
356
357 switch ( event.GetKeyCode() )
358 {
359 case WXK_SPACE:
360 oper = TOGGLE;
361 break;
362
363 case WXK_NUMPAD_ADD:
364 case '+':
365 oper = SET;
366 break;
367
368 case WXK_NUMPAD_SUBTRACT:
369 case '-':
370 oper = CLEAR;
371 break;
372
373 default:
374 oper = NONE;
375 }
376
377 if ( oper != NONE )
378 {
379 wxArrayInt selections;
380 int count = 0;
381 if ( HasMultipleSelection() )
382 {
383 count = GetSelections(selections);
384 }
385 else
386 {
387 int sel = GetSelection();
388 if (sel != -1)
389 {
390 count = 1;
391 selections.Add(sel);
392 }
393 }
394
395 for ( int i = 0; i < count; i++ )
396 {
397 int nItem = selections[i];
398
399 switch ( oper )
400 {
401 case TOGGLE:
402 Toggle(nItem);
403 break;
404
405 case SET:
406 case CLEAR:
407 Check(nItem, oper == SET);
408 break;
409
410 default:
411 wxFAIL_MSG( wxT("what should this key do?") );
412 }
413
414 // we should send an event as this has been done by the user and
415 // not by the program
416 SendEvent(nItem);
417 }
418 }
419 else // nothing to do
420 {
421 event.Skip();
422 }
423 }
424
425 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
426 {
427 // clicking on the item selects it, clicking on the checkmark toggles
428
429 int nItem = HitTest(event.GetX(), event.GetY());
430
431 if ( nItem != wxNOT_FOUND )
432 {
433 wxRect rect;
434 GetItemRect(nItem, rect);
435
436 // convert item rect to check mark rect
437 wxSize size = wxRendererNative::Get().GetCheckBoxSize(this);
438 rect.x += CHECKMARK_EXTRA_SPACE;
439 rect.y += (rect.GetHeight() - size.GetHeight()) / 2;
440 rect.SetSize(size);
441
442 if ( rect.Contains(event.GetX(), event.GetY()) )
443 {
444 // people expect to get "kill focus" event for the currently
445 // focused control before getting events from the other controls
446 // and, equally importantly, they may prevent the focus change from
447 // taking place at all (e.g. because the old control contents is
448 // invalid and needs to be corrected) in which case we shouldn't
449 // generate this event at all
450 SetFocus();
451 if ( FindFocus() == this )
452 {
453 Toggle(nItem);
454 SendEvent(nItem);
455
456 // scroll one item down if the item is the last one
457 // and isn't visible at all
458 int h;
459 GetClientSize(NULL, &h);
460 if ( rect.GetBottom() > h )
461 ScrollLines(1);
462 }
463 }
464 else
465 {
466 // implement default behaviour: clicking on the item selects it
467 event.Skip();
468 }
469 }
470 else
471 {
472 // implement default behavior on click outside of client zone
473 event.Skip();
474 }
475 }
476
477 wxSize wxCheckListBox::DoGetBestClientSize() const
478 {
479 wxSize best = wxListBox::DoGetBestClientSize();
480
481 // add room for the checkbox
482 wxSize size = wxRendererNative::Get().GetCheckBoxSize(const_cast<wxCheckListBox*>(this));
483 size.x += 2 * CHECKMARK_EXTRA_SPACE;
484 size.y += 2 * CHECKMARK_EXTRA_SPACE;
485
486 best.x += size.GetWidth();
487 if ( best.y < size.GetHeight() )
488 best.y = size.GetHeight();
489
490 CacheBestSize(best);
491 return best;
492 }
493
494 #endif // wxUSE_CHECKLISTBOX