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