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 // ============================================================================
45 // implementation of wxCheckListBoxBase
46 // ============================================================================
48 wxCheckListBoxBase::wxCheckListBoxBase()
52 // ============================================================================
53 // implementation of wxCheckListBox
54 // ============================================================================
56 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox
, wxListBox
)
58 // ----------------------------------------------------------------------------
59 // declaration and implementation of wxCheckListBoxItem class
60 // ----------------------------------------------------------------------------
62 class wxCheckListBoxItem
: public wxOwnerDrawn
64 friend class wxCheckListBox
;
69 wxCheckListBoxItem( wxCheckListBox
* pParent
76 virtual bool OnDrawItem( wxDC
& rDc
85 bool IsChecked(void) const { return m_bChecked
; }
86 void Check(bool bCheck
);
87 void Toggle(void) { Check(!IsChecked()); }
91 wxCheckListBox
* m_pParent
;
93 }; // end of CLASS wxCheckListBoxItem
95 wxCheckListBoxItem::wxCheckListBoxItem (
96 wxCheckListBox
* pParent
108 // We don't initialize m_nCheckHeight/Width vars because it's
109 // done in OnMeasure while they are used only in OnDraw and we
110 // know that there will always be OnMeasure before OnDraw
112 SetMarginWidth(GetDefaultMarginWidth());
113 } // end of wxCheckListBoxItem::wxCheckListBoxItem
115 bool wxCheckListBoxItem::OnDrawItem (
117 , const wxRect
& rRect
122 wxRect vRect
= rRect
;
124 ::WinQueryWindowRect( m_pParent
->GetHWND()
128 eStat
= (wxOwnerDrawn::wxODStatus
)(eStat
| wxOwnerDrawn::wxODChecked
);
131 // Unfortunately PM doesn't quite get the text position exact. We need to alter
132 // it down and to the right, just a little bit. The coords in rRect are OS/2
133 // coords not wxWindows coords.
137 if (wxOwnerDrawn::OnDrawItem( rDc
142 size_t nCheckWidth
= GetDefaultMarginWidth();
143 size_t nCheckHeight
= m_pParent
->GetItemHeight();
145 int nX
= rRect
.GetX();
146 int nY
= rRect
.GetY();
148 wxColour
vColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
153 m_pParent
->GetSize( NULL
157 nY
= nParentHeight
- nY
- nCheckHeight
;
158 vPenBack
= wxPen(vColour
, 1, wxSOLID
);
159 vPenGray
= wxPen(wxColour(127, 127, 127), 1, wxSOLID
);
162 // Erase the 1-pixel border
164 rDc
.SetPen(vPenBack
);
165 rDc
.DrawRectangle( nX
172 // Now we draw the smaller rectangle
179 // Draw hollow gray rectangle
181 rDc
.SetPen(vPenGray
);
182 rDc
.DrawRectangle( nX
192 // Draw the check by loading the sys standard bitmap and drawing it
194 HBITMAP hChkBmp
= ::WinGetSysBitmap( HWND_DESKTOP
197 POINTL vPoint
= {nX
, nOldY
+ 3};
199 ::WinDrawBitmap( rDc
.GetHPS()
211 } // end of wxCheckListBoxItem::OnDrawItem
214 // Change the state of the item and redraw it
216 void wxCheckListBoxItem::Check (
223 // Index may be chanegd because new items were added/deleted
225 if (m_pParent
->GetItemIndex(this) != (int)m_nIndex
)
230 int nIndex
= m_pParent
->GetItemIndex(this);
232 wxASSERT_MSG(nIndex
!= wxNOT_FOUND
, wxT("what does this item do here?"));
234 m_nIndex
= (size_t)nIndex
;
237 HWND hWndListbox
= (HWND
)m_pParent
->GetHWND();
241 wxCommandEvent
vEvent( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
245 vEvent
.SetInt(m_nIndex
);
246 vEvent
.SetEventObject(m_pParent
);
247 m_pParent
->ProcessCommand(vEvent
);
248 } // end of wxCheckListBoxItem::Check
250 // ----------------------------------------------------------------------------
251 // implementation of wxCheckListBox class
252 // ----------------------------------------------------------------------------
254 // define event table
255 // ------------------
256 BEGIN_EVENT_TABLE(wxCheckListBox
, wxListBox
)
257 EVT_CHAR(wxCheckListBox::OnChar
)
258 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick
)
267 // Default ctor: use Create() to really create the control
269 wxCheckListBox::wxCheckListBox()
272 } // end of wxCheckListBox::wxCheckListBox
275 // Ctor which creates the associated control
277 wxCheckListBox::wxCheckListBox (
280 , const wxPoint
& rPos
281 , const wxSize
& rSize
283 , const wxString asChoices
[]
285 , const wxValidator
& rVal
286 , const wxString
& rsName
296 ,lStyle
| wxLB_OWNERDRAW
300 } // end of wxCheckListBox::wxCheckListBox
302 void wxCheckListBox::Delete(
306 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
307 wxT("invalid index in wxListBox::Delete") );
308 wxListBox::Delete(N
);
314 m_aItems
.RemoveAt(N
);
315 } // end of wxCheckListBox::Delete
317 void wxCheckListBox::InsertItems (
319 , const wxString asItems
[]
325 wxCHECK_RET( nPos
>= 0 && nPos
<= m_nNumItems
,
326 wxT("invalid index in wxCheckListBox::InsertItems") );
328 wxListBox::InsertItems( nItems
332 for (i
= 0; i
< nItems
; i
++)
334 wxOwnerDrawn
* pNewItem
= CreateItem((size_t)(nPos
+ i
));
336 pNewItem
->SetName(asItems
[i
]);
337 m_aItems
.Insert(pNewItem
, (size_t)(nPos
+ i
));
338 ::WinSendMsg( (HWND
)GetHWND()
344 } // end of wxCheckListBox::InsertItems
346 bool wxCheckListBox::SetFont (
352 for (i
= 0; i
< m_aItems
.GetCount(); i
++)
353 m_aItems
[i
]->SetFont(rFont
);
354 wxListBox::SetFont(rFont
);
356 } // end of wxCheckListBox::SetFont
359 // Create/retrieve item
360 // --------------------
364 // Create a check list box item
366 wxOwnerDrawn
* wxCheckListBox::CreateItem (
370 wxCheckListBoxItem
* pItem
= new wxCheckListBoxItem( this
374 } // end of wxCheckListBox::CreateItem
380 long wxCheckListBox::OS2OnMeasure (
381 WXMEASUREITEMSTRUCT
* pItem
385 pItem
= (WXMEASUREITEMSTRUCT
*)new OWNERITEM
;
386 if (wxListBox::OS2OnMeasure(pItem
) )
388 POWNERITEM pStruct
= (POWNERITEM
)pItem
;
393 m_nItemHeight
= pStruct
->rclItem
.yTop
- pStruct
->rclItem
.yBottom
;
396 // Add place for the check mark
398 pStruct
->rclItem
.xRight
+= wxOwnerDrawn::GetDefaultMarginWidth();
399 return long(MRFROM2SHORT((USHORT
)m_nItemHeight
, (USHORT
)(pStruct
->rclItem
.xRight
- pStruct
->rclItem
.xLeft
)));
402 } // end of wxCheckListBox::CreateItem
408 bool wxCheckListBox::IsChecked (
412 return GetItem(uiIndex
)->IsChecked();
413 } // end of wxCheckListBox::IsChecked
415 void wxCheckListBox::Check (
420 GetItem(uiIndex
)->Check(bCheck
);
421 } // end of wxCheckListBox::Check
427 void wxCheckListBox::OnChar (
431 if (rEvent
.KeyCode() == WXK_SPACE
)
432 GetItem(GetSelection())->Toggle();
435 } // end of wxCheckListBox::OnChar
437 void wxCheckListBox::OnLeftClick (
442 // Clicking on the item selects it, clicking on the checkmark toggles
444 if (rEvent
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth())
453 vDc
.SetFont(GetFont());
454 vHeight
= vDc
.GetCharHeight() * 2.5;
457 // This, of course, will not work if the LB is scrolled
459 int nY
= rEvent
.GetY();
461 nY
= nParentHeight
- (nY
+ vHeight
);
463 size_t nItem
= (size_t)(nY
/ vHeight
);
465 if (nItem
< (size_t)m_nNumItems
)
466 GetItem(nItem
)->Toggle();
468 // else: it's not an error, just click outside of client zone
474 // Implement default behaviour: clicking on the item selects it
478 } // end of wxCheckListBox::OnLeftClick