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