Temporary fix for checklistbox refresh problem; setting the
[wxWidgets.git] / src / msw / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "checklst.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN
32
33 #ifndef WX_PRECOMP
34 #include "wx/object.h"
35 #include "wx/colour.h"
36 #include "wx/font.h"
37 #include "wx/bitmap.h"
38 #include "wx/window.h"
39 #include "wx/listbox.h"
40 #include "wx/dcmemory.h"
41
42 #include "wx/settings.h"
43
44 #include "wx/log.h"
45 #endif
46
47 #include "wx/ownerdrw.h"
48 #include "wx/checklst.h"
49
50 #include "wx/msw/wrapwin.h"
51 #include <windowsx.h>
52
53 #include "wx/msw/private.h"
54
55 // ----------------------------------------------------------------------------
56 // private functions
57 // ----------------------------------------------------------------------------
58
59 // get item (converted to right type)
60 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
61
62 // ============================================================================
63 // implementation
64 // ============================================================================
65
66
67 #if wxUSE_EXTENDED_RTTI
68 WX_DEFINE_FLAGS( wxCheckListBoxStyle )
69
70 wxBEGIN_FLAGS( wxCheckListBoxStyle )
71 // new style border flags, we put them first to
72 // use them for streaming out
73 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
74 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
75 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
76 wxFLAGS_MEMBER(wxBORDER_RAISED)
77 wxFLAGS_MEMBER(wxBORDER_STATIC)
78 wxFLAGS_MEMBER(wxBORDER_NONE)
79
80 // old style border flags
81 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
82 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
83 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
84 wxFLAGS_MEMBER(wxRAISED_BORDER)
85 wxFLAGS_MEMBER(wxSTATIC_BORDER)
86 wxFLAGS_MEMBER(wxBORDER)
87
88 // standard window styles
89 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
90 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
91 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
92 wxFLAGS_MEMBER(wxWANTS_CHARS)
93 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
94 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
95 wxFLAGS_MEMBER(wxVSCROLL)
96 wxFLAGS_MEMBER(wxHSCROLL)
97
98 wxFLAGS_MEMBER(wxLB_SINGLE)
99 wxFLAGS_MEMBER(wxLB_MULTIPLE)
100 wxFLAGS_MEMBER(wxLB_EXTENDED)
101 wxFLAGS_MEMBER(wxLB_HSCROLL)
102 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
103 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
104 wxFLAGS_MEMBER(wxLB_SORT)
105 wxFLAGS_MEMBER(wxLB_OWNERDRAW)
106
107 wxEND_FLAGS( wxCheckListBoxStyle )
108
109 IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox, wxListBox,"wx/checklst.h")
110
111 wxBEGIN_PROPERTIES_TABLE(wxCheckListBox)
112 wxEVENT_PROPERTY( Toggle , wxEVT_COMMAND_CHECKLISTBOX_TOGGLED , wxCommandEvent )
113 wxPROPERTY_FLAGS( WindowStyle , wxCheckListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , wxLB_OWNERDRAW /*flags*/ , wxT("Helpstring") , wxT("group")) // style
114 wxEND_PROPERTIES_TABLE()
115
116 wxBEGIN_HANDLERS_TABLE(wxCheckListBox)
117 wxEND_HANDLERS_TABLE()
118
119 wxCONSTRUCTOR_4( wxCheckListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
120
121 #else
122 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
123 #endif
124
125 // ----------------------------------------------------------------------------
126 // declaration and implementation of wxCheckListBoxItem class
127 // ----------------------------------------------------------------------------
128
129 class wxCheckListBoxItem : public wxOwnerDrawn
130 {
131 friend class WXDLLEXPORT wxCheckListBox;
132 public:
133 // ctor
134 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
135
136 // drawing functions
137 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
138
139 // simple accessors and operations
140 bool IsChecked() const { return m_bChecked; }
141
142 void Check(bool bCheck);
143 void Toggle() { Check(!IsChecked()); }
144
145 void SendEvent();
146
147 private:
148 bool m_bChecked;
149 wxCheckListBox *m_pParent;
150 size_t m_nIndex;
151
152 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem)
153 };
154
155 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
156 : wxOwnerDrawn(wxEmptyString, true) // checkable
157 {
158 m_bChecked = false;
159 m_pParent = pParent;
160 m_nIndex = nIndex;
161
162 // we don't initialize m_nCheckHeight/Width vars because it's
163 // done in OnMeasure while they are used only in OnDraw and we
164 // know that there will always be OnMeasure before OnDraw
165
166 // fix appearance for check list boxes: they don't look quite the same as
167 // menu icons
168 SetMarginWidth(::GetSystemMetrics(SM_CXMENUCHECK) -
169 2*wxSystemSettings::GetMetric(wxSYS_EDGE_X) + 1);
170 SetBackgroundColour(pParent->GetBackgroundColour());
171 }
172
173 bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
174 wxODAction act, wxODStatus stat)
175 {
176 // first draw the label
177 if ( IsChecked() )
178 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
179
180 if ( !wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) )
181 return false;
182
183
184 // now draw the check mark part
185 size_t nCheckWidth = GetDefaultMarginWidth(),
186 nCheckHeight = m_pParent->GetItemHeight();
187
188 int x = rc.GetX(),
189 y = rc.GetY();
190
191 HDC hdc = (HDC)dc.GetHDC();
192
193 // create pens, brushes &c
194 COLORREF colBg = ::GetSysColor(COLOR_WINDOW);
195 AutoHPEN hpenBack(colBg),
196 hpenGray(RGB(0xc0, 0xc0, 0xc0));
197
198 SelectInHDC selPen(hdc, (HGDIOBJ)hpenBack);
199 AutoHBRUSH hbrBack(colBg);
200 SelectInHDC selBrush(hdc, hbrBack);
201
202 // erase the background: it could have been filled with the selected colour
203 Rectangle(hdc, x, y, x + nCheckWidth + 1, rc.GetBottom() + 1);
204
205 // shift check mark 1 pixel to the right, looks better like this
206 x++;
207
208 if ( IsChecked() )
209 {
210 // first create a monochrome bitmap in a memory DC
211 MemoryHDC hdcMem(hdc);
212 MonoBitmap hbmpCheck(nCheckWidth, nCheckHeight);
213 SelectInHDC selBmp(hdcMem, hbmpCheck);
214
215 // then draw a check mark into it
216 RECT rect = { 0, 0, nCheckWidth, nCheckHeight };
217 ::DrawFrameControl(hdcMem, &rect,
218 #ifdef __WXWINCE__
219 DFC_BUTTON, DFCS_BUTTONCHECK
220 #else
221 DFC_MENU, DFCS_MENUCHECK
222 #endif
223 );
224
225 // finally copy it to screen DC
226 ::BitBlt(hdc, x, y, nCheckWidth, nCheckHeight, hdcMem, 0, 0, SRCCOPY);
227 }
228
229 // now we draw the smaller rectangle
230 y++;
231 nCheckWidth -= 2;
232 nCheckHeight -= 2;
233
234 // draw hollow gray rectangle
235 (void)::SelectObject(hdc, (HGDIOBJ)hpenGray);
236
237 SelectInHDC selBrush2(hdc, ::GetStockObject(NULL_BRUSH));
238 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
239
240 return true;
241 }
242
243 // change the state of the item and redraw it
244 void wxCheckListBoxItem::Check(bool check)
245 {
246 m_bChecked = check;
247
248 // index may be changed because new items were added/deleted
249 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
250 {
251 // update it
252 int index = m_pParent->GetItemIndex(this);
253
254 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
255
256 m_nIndex = (size_t)index;
257 }
258
259 HWND hwndListbox = (HWND)m_pParent->GetHWND();
260
261 RECT rcUpdate;
262
263 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
264 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
265 {
266 wxLogDebug(wxT("LB_GETITEMRECT failed"));
267 }
268
269 ::InvalidateRect(hwndListbox, &rcUpdate, FALSE);
270 }
271
272 // send an "item checked" event
273 void wxCheckListBoxItem::SendEvent()
274 {
275 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
276 event.SetInt(m_nIndex);
277 event.SetEventObject(m_pParent);
278 m_pParent->ProcessCommand(event);
279 }
280
281 // ----------------------------------------------------------------------------
282 // implementation of wxCheckListBox class
283 // ----------------------------------------------------------------------------
284
285 // define event table
286 // ------------------
287 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
288 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
289 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
290 END_EVENT_TABLE()
291
292 // control creation
293 // ----------------
294
295 // def ctor: use Create() to really create the control
296 wxCheckListBox::wxCheckListBox()
297 {
298 }
299
300 // ctor which creates the associated control
301 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
302 const wxPoint& pos, const wxSize& size,
303 int nStrings, const wxString choices[],
304 long style, const wxValidator& val,
305 const wxString& name)
306 {
307 Create(parent, id, pos, size, nStrings, choices, style, val, name);
308 }
309
310 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
311 const wxPoint& pos, const wxSize& size,
312 const wxArrayString& choices,
313 long style, const wxValidator& val,
314 const wxString& name)
315 {
316 Create(parent, id, pos, size, choices, style, val, name);
317 }
318
319 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
320 const wxPoint& pos, const wxSize& size,
321 int n, const wxString choices[],
322 long style,
323 const wxValidator& validator, const wxString& name)
324 {
325 if (wxListBox::Create(parent, id, pos, size, n, choices,
326 style | wxLB_OWNERDRAW, validator, name))
327 {
328 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
329 return true;
330 }
331 else
332 return false;
333 }
334
335 bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
336 const wxPoint& pos, const wxSize& size,
337 const wxArrayString& choices,
338 long style,
339 const wxValidator& validator, const wxString& name)
340 {
341 if (wxListBox::Create(parent, id, pos, size, choices,
342 style | wxLB_OWNERDRAW, validator, name))
343 {
344 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
345 return true;
346 }
347 else
348 return false;
349 }
350
351 // misc overloaded methods
352 // -----------------------
353
354 void wxCheckListBox::Delete(int N)
355 {
356 wxCHECK_RET( N >= 0 && N < m_noItems,
357 wxT("invalid index in wxListBox::Delete") );
358
359 wxListBox::Delete(N);
360
361 // free memory
362 delete m_aItems[N];
363
364 m_aItems.RemoveAt(N);
365 }
366
367 bool wxCheckListBox::SetFont( const wxFont &font )
368 {
369 size_t i;
370 for ( i = 0; i < m_aItems.GetCount(); i++ )
371 m_aItems[i]->SetFont(font);
372
373 wxListBox::SetFont(font);
374
375 return true;
376 }
377
378 // create/retrieve item
379 // --------------------
380
381 // create a check list box item
382 wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
383 {
384 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
385 return pItem;
386 }
387
388 // return item size
389 // ----------------
390 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
391 {
392 if ( wxListBox::MSWOnMeasure(item) ) {
393 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
394
395 // save item height
396 m_nItemHeight = pStruct->itemHeight;
397
398 // add place for the check mark
399 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
400
401 return true;
402 }
403
404 return false;
405 }
406
407 // check items
408 // -----------
409
410 bool wxCheckListBox::IsChecked(size_t uiIndex) const
411 {
412 wxCHECK_MSG( uiIndex < (size_t)GetCount(), false, _T("bad wxCheckListBox index") );
413
414 return GetItem(uiIndex)->IsChecked();
415 }
416
417 void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
418 {
419 wxCHECK_RET( uiIndex < (size_t)GetCount(), _T("bad wxCheckListBox index") );
420
421 GetItem(uiIndex)->Check(bCheck);
422 }
423
424 // process events
425 // --------------
426
427 void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
428 {
429 // what do we do?
430 enum
431 {
432 None,
433 Toggle,
434 Set,
435 Clear
436 } oper;
437
438 switch ( event.GetKeyCode() )
439 {
440 case WXK_SPACE:
441 oper = Toggle;
442 break;
443
444 case WXK_NUMPAD_ADD:
445 case '+':
446 oper = Set;
447 break;
448
449 case WXK_NUMPAD_SUBTRACT:
450 case '-':
451 oper = Clear;
452 break;
453
454 default:
455 oper = None;
456 }
457
458 if ( oper != None )
459 {
460 wxArrayInt selections;
461 int count = 0;
462 if ( HasMultipleSelection() )
463 {
464 count = GetSelections(selections);
465 }
466 else
467 {
468 int sel = GetSelection();
469 if (sel != -1)
470 {
471 count = 1;
472 selections.Add(sel);
473 }
474 }
475
476 for ( int i = 0; i < count; i++ )
477 {
478 wxCheckListBoxItem *item = GetItem(selections[i]);
479 if ( !item )
480 {
481 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
482 continue;
483 }
484
485 switch ( oper )
486 {
487 case Toggle:
488 item->Toggle();
489 break;
490
491 case Set:
492 case Clear:
493 item->Check( oper == Set );
494 break;
495
496 default:
497 wxFAIL_MSG( _T("what should this key do?") );
498 }
499
500 // we should send an event as this has been done by the user and
501 // not by the program
502 item->SendEvent();
503 }
504 }
505 else // nothing to do
506 {
507 event.Skip();
508 }
509 }
510
511 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
512 {
513 // clicking on the item selects it, clicking on the checkmark toggles
514 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
515 int nItem = HitTest(event.GetX(), event.GetY());
516
517 if ( nItem != wxNOT_FOUND ) {
518 wxCheckListBoxItem *item = GetItem(nItem);
519 item->Toggle();
520 item->SendEvent();
521 }
522 //else: it's not an error, just click outside of client zone
523 }
524 else {
525 // implement default behaviour: clicking on the item selects it
526 event.Skip();
527 }
528 }
529
530 int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
531 {
532 int nItem = (int)::SendMessage
533 (
534 (HWND)GetHWND(),
535 LB_ITEMFROMPOINT,
536 0,
537 MAKELPARAM(x, y)
538 );
539
540 return nItem >= m_noItems ? wxNOT_FOUND : nItem;
541 }
542
543
544 wxSize wxCheckListBox::DoGetBestSize() const
545 {
546 wxSize best = wxListBox::DoGetBestSize();
547 best.x += wxOwnerDrawn::GetDefaultMarginWidth(); // add room for the checkbox
548 CacheBestSize(best);
549 return best;
550 }
551
552 #endif
553