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