1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: implementation of wxCheckListBox class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
13 // headers & declarations
14 // ============================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
21 #include "wx/object.h"
22 #include "wx/colour.h"
24 #include "wx/bitmap.h"
25 #include "wx/window.h"
26 #include "wx/listbox.h"
27 #include "wx/ownerdrw.h"
28 #include "wx/settings.h"
29 #include "wx/dcmemory.h"
30 #include "wx/dcscreen.h"
31 #include "wx/os2/checklst.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // get item (converted to right type)
42 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
44 // ============================================================================
46 // ============================================================================
48 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
50 // ----------------------------------------------------------------------------
51 // declaration and implementation of wxCheckListBoxItem class
52 // ----------------------------------------------------------------------------
54 class wxCheckListBoxItem
: public wxOwnerDrawn
56 friend class wxCheckListBox
;
61 wxCheckListBoxItem( wxCheckListBox
* pParent
68 virtual bool OnDrawItem( wxDC
& rDc
77 bool IsChecked(void) const { return m_bChecked
; }
78 void Check(bool bCheck
);
79 void Toggle(void) { Check(!IsChecked()); }
83 wxCheckListBox
* m_pParent
;
85 }; // end of CLASS wxCheckListBoxItem
87 wxCheckListBoxItem::wxCheckListBoxItem (
88 wxCheckListBox
* pParent
100 // We don't initialize m_nCheckHeight/Width vars because it's
101 // done in OnMeasure while they are used only in OnDraw and we
102 // know that there will always be OnMeasure before OnDraw
104 SetMarginWidth(GetDefaultMarginWidth());
105 } // end of wxCheckListBoxItem::wxCheckListBoxItem
107 bool wxCheckListBoxItem::OnDrawItem (
109 , const wxRect
& rRect
114 wxRect vRect
= rRect
;
116 ::WinQueryWindowRect( m_pParent
->GetHWND()
120 eStat
= (wxOwnerDrawn::wxODStatus
)(eStat
| wxOwnerDrawn::wxODChecked
);
123 // Unfortunately PM doesn't quite get the text position exact. We need to alter
124 // it down and to the right, just a little bit. The coords in rRect are OS/2
125 // coords not wxWindows coords.
129 if (wxOwnerDrawn::OnDrawItem( rDc
134 size_t nCheckWidth
= GetDefaultMarginWidth();
135 size_t nCheckHeight
= m_pParent
->GetItemHeight();
137 int nX
= rRect
.GetX();
138 int nY
= rRect
.GetY();
140 wxColour
vColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
145 m_pParent
->GetSize( NULL
149 nY
= nParentHeight
- nY
- nCheckHeight
;
150 vPenBack
= wxPen(vColour
, 1, wxSOLID
);
151 vPenGray
= wxPen(wxColour(127, 127, 127), 1, wxSOLID
);
154 // Erase the 1-pixel border
156 rDc
.SetPen(vPenBack
);
157 rDc
.DrawRectangle( nX
164 // Now we draw the smaller rectangle
171 // Draw hollow gray rectangle
173 rDc
.SetPen(vPenGray
);
174 rDc
.DrawRectangle( nX
184 // Draw the check by loading the sys standard bitmap and drawing it
186 HBITMAP hChkBmp
= ::WinGetSysBitmap( HWND_DESKTOP
189 POINTL vPoint
= {nX
, nOldY
+ 3};
191 ::WinDrawBitmap( rDc
.GetHPS()
203 } // end of wxCheckListBoxItem::OnDrawItem
206 // Change the state of the item and redraw it
208 void wxCheckListBoxItem::Check (
215 // Index may be chanegd because new items were added/deleted
217 if (m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
222 int nIndex
= m_pParent
->GetItemIndex(this);
224 wxASSERT_MSG(nIndex
!= wxNOT_FOUND
, wxT("what does this item do here?"));
226 m_nIndex
= (size_t)nIndex
;
229 HWND hWndListbox
= (HWND
)m_pParent
->GetHWND();
233 wxCommandEvent
vEvent( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
237 vEvent
.SetInt(m_nIndex
);
238 vEvent
.SetEventObject(m_pParent
);
239 m_pParent
->ProcessCommand(vEvent
);
240 } // end of wxCheckListBoxItem::Check
242 // ----------------------------------------------------------------------------
243 // implementation of wxCheckListBox class
244 // ----------------------------------------------------------------------------
246 // define event table
247 // ------------------
248 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
249 EVT_CHAR(wxCheckListBox::OnChar
)
250 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
259 // Default ctor: use Create() to really create the control
261 wxCheckListBox::wxCheckListBox()
264 } // end of wxCheckListBox::wxCheckListBox
267 // Ctor which creates the associated control
269 wxCheckListBox::wxCheckListBox (
272 , const wxPoint
& rPos
273 , const wxSize
& rSize
275 , const wxString asChoices
[]
277 , const wxValidator
& rVal
278 , const wxString
& rsName
288 ,lStyle
| wxLB_OWNERDRAW
292 } // end of wxCheckListBox::wxCheckListBox
294 wxCheckListBox::wxCheckListBox (
297 , const wxPoint
& rPos
298 , const wxSize
& rSize
299 , const wxArrayString
& asChoices
301 , const wxValidator
& rVal
302 , const wxString
& rsName
306 wxCArrayString
chs(asChoices
);
313 ,lStyle
| wxLB_OWNERDRAW
317 } // end of wxCheckListBox::wxCheckListBox
319 void wxCheckListBox::Delete(
323 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
324 wxT("invalid index in wxListBox::Delete") );
325 wxListBox::Delete(N
);
331 m_aItems
.RemoveAt(N
);
332 } // end of wxCheckListBox::Delete
334 void wxCheckListBox::InsertItems (
336 , const wxString asItems
[]
342 wxCHECK_RET( nPos
>= 0 && nPos
<= m_nNumItems
,
343 wxT("invalid index in wxCheckListBox::InsertItems") );
345 wxListBox::InsertItems( nItems
349 for (i
= 0; i
< nItems
; i
++)
351 wxOwnerDrawn
* pNewItem
= CreateItem((size_t)(nPos
+ i
));
353 pNewItem
->SetName(asItems
[i
]);
354 m_aItems
.Insert(pNewItem
, (size_t)(nPos
+ i
));
355 ::WinSendMsg( (HWND
)GetHWND()
361 } // end of wxCheckListBox::InsertItems
363 bool wxCheckListBox::SetFont (
369 for (i
= 0; i
< m_aItems
.GetCount(); i
++)
370 m_aItems
[i
]->SetFont(rFont
);
371 wxListBox::SetFont(rFont
);
373 } // end of wxCheckListBox::SetFont
376 // Create/retrieve item
377 // --------------------
381 // Create a check list box item
383 wxOwnerDrawn
* wxCheckListBox::CreateItem (
387 wxCheckListBoxItem
* pItem
= new wxCheckListBoxItem( this
391 } // end of wxCheckListBox::CreateItem
397 long wxCheckListBox::OS2OnMeasure (
398 WXMEASUREITEMSTRUCT
* pItem
402 pItem
= (WXMEASUREITEMSTRUCT
*)new OWNERITEM
;
403 if (wxListBox::OS2OnMeasure(pItem
) )
405 POWNERITEM pStruct
= (POWNERITEM
)pItem
;
410 m_nItemHeight
= pStruct
->rclItem
.yTop
- pStruct
->rclItem
.yBottom
;
413 // Add place for the check mark
415 pStruct
->rclItem
.xRight
+= wxOwnerDrawn::GetDefaultMarginWidth();
416 return long(MRFROM2SHORT((USHORT
)m_nItemHeight
, (USHORT
)(pStruct
->rclItem
.xRight
- pStruct
->rclItem
.xLeft
)));
419 } // end of wxCheckListBox::CreateItem
425 bool wxCheckListBox::IsChecked (
429 return GetItem(uiIndex
)->IsChecked();
430 } // end of wxCheckListBox::IsChecked
432 void wxCheckListBox::Check (
437 GetItem(uiIndex
)->Check(bCheck
);
438 } // end of wxCheckListBox::Check
444 void wxCheckListBox::OnChar (
448 if (rEvent
.KeyCode() == WXK_SPACE
)
449 GetItem(GetSelection())->Toggle();
452 } // end of wxCheckListBox::OnChar
454 void wxCheckListBox::OnLeftClick (
459 // Clicking on the item selects it, clicking on the checkmark toggles
461 if (rEvent
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth())
470 vDc
.SetFont(GetFont());
471 vHeight
= vDc
.GetCharHeight() * 2.5;
474 // This, of course, will not work if the LB is scrolled
476 int nY
= rEvent
.GetY();
478 nY
= nParentHeight
- (nY
+ vHeight
);
480 size_t nItem
= (size_t)(nY
/ vHeight
);
482 if (nItem
< (size_t)m_nNumItems
)
483 GetItem(nItem
)->Toggle();
485 // else: it's not an error, just click outside of client zone
491 // Implement default behaviour: clicking on the item selects it
495 } // end of wxCheckListBox::OnLeftClick