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 void wxCheckListBox::Delete(
298 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
299 wxT("invalid index in wxListBox::Delete") );
300 wxListBox::Delete(N
);
306 m_aItems
.RemoveAt(N
);
307 } // end of wxCheckListBox::Delete
309 void wxCheckListBox::InsertItems (
311 , const wxString asItems
[]
317 wxCHECK_RET( nPos
>= 0 && nPos
<= m_nNumItems
,
318 wxT("invalid index in wxCheckListBox::InsertItems") );
320 wxListBox::InsertItems( nItems
324 for (i
= 0; i
< nItems
; i
++)
326 wxOwnerDrawn
* pNewItem
= CreateItem((size_t)(nPos
+ i
));
328 pNewItem
->SetName(asItems
[i
]);
329 m_aItems
.Insert(pNewItem
, (size_t)(nPos
+ i
));
330 ::WinSendMsg( (HWND
)GetHWND()
336 } // end of wxCheckListBox::InsertItems
338 bool wxCheckListBox::SetFont (
344 for (i
= 0; i
< m_aItems
.GetCount(); i
++)
345 m_aItems
[i
]->SetFont(rFont
);
346 wxListBox::SetFont(rFont
);
348 } // end of wxCheckListBox::SetFont
351 // Create/retrieve item
352 // --------------------
356 // Create a check list box item
358 wxOwnerDrawn
* wxCheckListBox::CreateItem (
362 wxCheckListBoxItem
* pItem
= new wxCheckListBoxItem( this
366 } // end of wxCheckListBox::CreateItem
372 long wxCheckListBox::OS2OnMeasure (
373 WXMEASUREITEMSTRUCT
* pItem
377 pItem
= (WXMEASUREITEMSTRUCT
*)new OWNERITEM
;
378 if (wxListBox::OS2OnMeasure(pItem
) )
380 POWNERITEM pStruct
= (POWNERITEM
)pItem
;
385 m_nItemHeight
= pStruct
->rclItem
.yTop
- pStruct
->rclItem
.yBottom
;
388 // Add place for the check mark
390 pStruct
->rclItem
.xRight
+= wxOwnerDrawn::GetDefaultMarginWidth();
391 return long(MRFROM2SHORT((USHORT
)m_nItemHeight
, (USHORT
)(pStruct
->rclItem
.xRight
- pStruct
->rclItem
.xLeft
)));
394 } // end of wxCheckListBox::CreateItem
400 bool wxCheckListBox::IsChecked (
404 return GetItem(uiIndex
)->IsChecked();
405 } // end of wxCheckListBox::IsChecked
407 void wxCheckListBox::Check (
412 GetItem(uiIndex
)->Check(bCheck
);
413 } // end of wxCheckListBox::Check
419 void wxCheckListBox::OnChar (
423 if (rEvent
.KeyCode() == WXK_SPACE
)
424 GetItem(GetSelection())->Toggle();
427 } // end of wxCheckListBox::OnChar
429 void wxCheckListBox::OnLeftClick (
434 // Clicking on the item selects it, clicking on the checkmark toggles
436 if (rEvent
.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth())
445 vDc
.SetFont(GetFont());
446 vHeight
= vDc
.GetCharHeight() * 2.5;
449 // This, of course, will not work if the LB is scrolled
451 int nY
= rEvent
.GetY();
453 nY
= nParentHeight
- (nY
+ vHeight
);
455 size_t nItem
= (size_t)(nY
/ vHeight
);
457 if (nItem
< (size_t)m_nNumItems
)
458 GetItem(nItem
)->Toggle();
460 // else: it's not an error, just click outside of client zone
466 // Implement default behaviour: clicking on the item selects it
470 } // end of wxCheckListBox::OnLeftClick