1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/checklst.cpp
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"
19 #if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN
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/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
, size_t nIndex
);
66 virtual bool OnDrawItem( wxDC
& rDc
,
75 bool IsChecked(void) const { return m_bChecked
; }
76 void Check(bool bCheck
);
77 void Toggle(void) { Check(!IsChecked()); }
81 wxCheckListBox
* m_pParent
;
83 }; // end of CLASS wxCheckListBoxItem
87 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox
* pParent
, size_t nIndex
)
88 :wxOwnerDrawn( wxEmptyString
, true /* checkable */ )
95 // We don't initialize m_nCheckHeight/Width vars because it's
96 // done in OnMeasure while they are used only in OnDraw and we
97 // know that there will always be OnMeasure before OnDraw
99 SetMarginWidth(GetDefaultMarginWidth());
100 } // end of wxCheckListBoxItem::wxCheckListBoxItem
104 bool wxCheckListBoxItem::OnDrawItem ( wxDC
& rDc
,
109 wxRect vRect
= rRect
;
111 ::WinQueryWindowRect( m_pParent
->GetHWND(), &rDc
.m_vRclPaint
);
113 eStat
= (wxOwnerDrawn::wxODStatus
)(eStat
| wxOwnerDrawn::wxODChecked
);
116 // Unfortunately PM doesn't quite get the text position exact. We need to alter
117 // it down and to the right, just a little bit. The coords in rRect are OS/2
118 // coords not wxWidgets coords.
122 if (wxOwnerDrawn::OnDrawItem( rDc
, vRect
, eAct
, eStat
))
124 size_t nCheckWidth
= GetDefaultMarginWidth();
125 size_t nCheckHeight
= m_pParent
->GetItemHeight();
127 int nX
= rRect
.GetX();
128 int nY
= rRect
.GetY();
130 wxColour
vColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
134 m_pParent
->GetSize( NULL
, &nParentHeight
);
136 nY
= nParentHeight
- nY
- nCheckHeight
;
137 vPenBack
= wxPen(vColour
, 1, wxSOLID
);
140 // Erase the 1-pixel border
142 rDc
.SetPen(vPenBack
);
143 rDc
.DrawRectangle( nX
, nY
, nCheckWidth
, nCheckHeight
);
146 // Now we draw the smaller rectangle
153 // Draw hollow gray rectangle
155 rDc
.SetPen(*wxGREY_PEN
);
156 rDc
.DrawRectangle( nX
, nY
, nCheckWidth
, nCheckHeight
);
162 // Draw the check by loading the sys standard bitmap and drawing it
164 HBITMAP hChkBmp
= ::WinGetSysBitmap( HWND_DESKTOP
, SBMP_MENUCHECK
);
165 POINTL vPoint
= {nX
, nOldY
+ 3};
167 ::WinDrawBitmap( rDc
.GetHPS(),
179 } // end of wxCheckListBoxItem::OnDrawItem
182 // Change the state of the item and redraw it
184 void wxCheckListBoxItem::Check( bool bCheck
)
189 // Index may be chanegd because new items were added/deleted
191 if (m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
196 int nIndex
= m_pParent
->GetItemIndex(this);
198 wxASSERT_MSG(nIndex
!= wxNOT_FOUND
, wxT("what does this item do here?"));
200 m_nIndex
= (size_t)nIndex
;
204 wxCommandEvent
vEvent( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
,m_pParent
->GetId());
206 vEvent
.SetInt(m_nIndex
);
207 vEvent
.SetEventObject(m_pParent
);
208 m_pParent
->ProcessCommand(vEvent
);
209 } // end of wxCheckListBoxItem::Check
212 // ----------------------------------------------------------------------------
213 // implementation of wxCheckListBox class
214 // ----------------------------------------------------------------------------
216 // define event table
217 // ------------------
218 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
219 EVT_CHAR(wxCheckListBox::OnChar
)
220 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
231 // Default ctor: use Create() to really create the control
233 wxCheckListBox::wxCheckListBox()
234 :wxCheckListBoxBase()
236 } // end of wxCheckListBox::wxCheckListBox
239 // Ctor which creates the associated control
241 wxCheckListBox::wxCheckListBox ( wxWindow
* pParent
,
246 const wxString asChoices
[],
248 const wxValidator
& rVal
,
249 const wxString
& rsName
)
250 :wxCheckListBoxBase()
252 Create( pParent
, vId
, rPos
, rSize
, nStrings
, asChoices
, lStyle
| wxLB_OWNERDRAW
, rVal
, rsName
);
253 } // end of wxCheckListBox::wxCheckListBox
255 wxCheckListBox::wxCheckListBox ( wxWindow
* pParent
,
259 const wxArrayString
& asChoices
,
261 const wxValidator
& rVal
,
262 const wxString
& rsName
)
263 :wxCheckListBoxBase()
265 wxCArrayString
chs(asChoices
);
266 Create( pParent
, vId
, rPos
, rSize
, chs
.GetCount(), chs
.GetStrings(),
267 lStyle
| wxLB_OWNERDRAW
, rVal
, rsName
);
268 } // end of wxCheckListBox::wxCheckListBox
270 void wxCheckListBox::Delete(unsigned int n
)
272 wxCHECK_RET( IsValid(n
),
273 wxT("invalid index in wxCheckListBox::Delete") );
274 wxListBox::Delete(n
);
280 m_aItems
.RemoveAt(n
);
281 } // end of wxCheckListBox::Delete
283 void wxCheckListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
285 // pos is validated in wxListBox
286 wxListBox::DoInsertItems( items
, pos
);
287 unsigned int n
= items
.GetCount();
288 for (unsigned int i
= 0; i
< n
; i
++)
290 wxOwnerDrawn
* pNewItem
= CreateItem((size_t)(pos
+ i
));
292 pNewItem
->SetName(items
[i
]);
293 m_aItems
.Insert(pNewItem
, (size_t)(pos
+ i
));
294 ::WinSendMsg( (HWND
)GetHWND(),
300 } // end of wxCheckListBox::InsertItems
302 bool wxCheckListBox::SetFont ( const wxFont
& rFont
)
304 for (unsigned int i
= 0; i
< m_aItems
.GetCount(); i
++)
305 m_aItems
[i
]->SetFont(rFont
);
306 wxListBox::SetFont(rFont
);
308 } // end of wxCheckListBox::SetFont
313 // Create/retrieve item
314 // --------------------
318 // Create a check list box item
320 wxOwnerDrawn
* wxCheckListBox::CreateItem(size_t nIndex
)
322 wxCheckListBoxItem
* pItem
= new wxCheckListBoxItem( this, nIndex
);
324 } // end of wxCheckListBox::CreateItem
332 long wxCheckListBox::OS2OnMeasure ( WXMEASUREITEMSTRUCT
* pItem
)
335 pItem
= (WXMEASUREITEMSTRUCT
*)new OWNERITEM
;
336 if (wxListBox::OS2OnMeasure(pItem
))
338 POWNERITEM pStruct
= (POWNERITEM
)pItem
;
343 m_nItemHeight
= pStruct
->rclItem
.yTop
- pStruct
->rclItem
.yBottom
;
346 // Add place for the check mark
348 pStruct
->rclItem
.xRight
+= wxOwnerDrawn::GetDefaultMarginWidth();
349 return long(MRFROM2SHORT((USHORT
)m_nItemHeight
, (USHORT
)(pStruct
->rclItem
.xRight
- pStruct
->rclItem
.xLeft
)));
352 } // end of wxCheckListBox::CreateItem
360 bool wxCheckListBox::IsChecked(unsigned int uiIndex
) const
362 return GetItem(uiIndex
)->IsChecked();
363 } // end of wxCheckListBox::IsChecked
365 void wxCheckListBox::Check(unsigned int uiIndex
, bool bCheck
)
367 GetItem(uiIndex
)->Check(bCheck
);
368 } // end of wxCheckListBox::Check
376 void wxCheckListBox::OnChar ( wxKeyEvent
& rEvent
)
378 if (rEvent
.GetKeyCode() == WXK_SPACE
)
379 GetItem(GetSelection())->Toggle();
382 } // end of wxCheckListBox::OnChar
384 void wxCheckListBox::OnLeftClick ( wxMouseEvent
& rEvent
)
387 // Clicking on the item selects it, clicking on the checkmark toggles
389 if (rEvent
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth())
395 GetSize( NULL
, &nParentHeight
);
396 vDc
.SetFont(GetFont());
397 vHeight
= (wxCoord
)(vDc
.GetCharHeight() * 2.5);
400 // This, of course, will not work if the LB is scrolled
402 int nY
= rEvent
.GetY();
404 nY
= nParentHeight
- (nY
+ vHeight
);
406 size_t nItem
= (size_t)(nY
/ vHeight
);
408 if (nItem
< m_nNumItems
)
409 GetItem(nItem
)->Toggle();
411 // else: it's not an error, just click outside of client zone
417 // Implement default behaviour: clicking on the item selects it
421 } // end of wxCheckListBox::OnLeftClick
423 #endif // wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN