]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/os2/checklst.cpp | |
3 | // Purpose: implementation of wxCheckListBox class | |
4 | // Author: David Webster | |
5 | // Modified by: | |
6 | // Created: 10/13/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) David Webster | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // headers & declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #if wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN | |
20 | ||
21 | #include "wx/object.h" | |
22 | #include "wx/colour.h" | |
23 | #include "wx/font.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" | |
32 | #include "wx/log.h" | |
33 | ||
34 | #define INCL_PM | |
35 | #include <os2.h> | |
36 | ||
37 | // ---------------------------------------------------------------------------- | |
38 | // private functions | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | // get item (converted to right type) | |
42 | #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n))) | |
43 | ||
44 | // ============================================================================ | |
45 | // implementation | |
46 | // ============================================================================ | |
47 | ||
48 | IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox) | |
49 | ||
50 | // ---------------------------------------------------------------------------- | |
51 | // declaration and implementation of wxCheckListBoxItem class | |
52 | // ---------------------------------------------------------------------------- | |
53 | ||
54 | class wxCheckListBoxItem : public wxOwnerDrawn | |
55 | { | |
56 | friend class wxCheckListBox; | |
57 | public: | |
58 | // | |
59 | // ctor | |
60 | // | |
61 | wxCheckListBoxItem(wxCheckListBox* pParent, size_t nIndex); | |
62 | ||
63 | // | |
64 | // Drawing functions | |
65 | // | |
66 | virtual bool OnDrawItem( wxDC& rDc, | |
67 | const wxRect& rRect, | |
68 | wxODAction eAct, | |
69 | wxODStatus eStat | |
70 | ); | |
71 | ||
72 | // | |
73 | // Simple accessors | |
74 | // | |
75 | bool IsChecked(void) const { return m_bChecked; } | |
76 | void Check(bool bCheck); | |
77 | void Toggle(void) { Check(!IsChecked()); } | |
78 | ||
79 | private: | |
80 | bool m_bChecked; | |
81 | wxCheckListBox* m_pParent; | |
82 | size_t m_nIndex; | |
83 | }; // end of CLASS wxCheckListBoxItem | |
84 | ||
85 | ||
86 | ||
87 | wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox* pParent, size_t nIndex) | |
88 | :wxOwnerDrawn( wxEmptyString, true /* checkable */ ) | |
89 | { | |
90 | m_bChecked = false; | |
91 | m_pParent = pParent; | |
92 | m_nIndex = nIndex; | |
93 | ||
94 | // | |
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 | |
98 | // | |
99 | SetMarginWidth(GetDefaultMarginWidth()); | |
100 | } // end of wxCheckListBoxItem::wxCheckListBoxItem | |
101 | ||
102 | ||
103 | ||
104 | bool wxCheckListBoxItem::OnDrawItem ( wxDC& rDc, | |
105 | const wxRect& rRect, | |
106 | wxODAction eAct, | |
107 | wxODStatus eStat ) | |
108 | { | |
109 | wxRect vRect = rRect; | |
110 | ||
111 | ::WinQueryWindowRect( m_pParent->GetHWND(), &rDc.m_vRclPaint ); | |
112 | if (IsChecked()) | |
113 | eStat = (wxOwnerDrawn::wxODStatus)(eStat | wxOwnerDrawn::wxODChecked); | |
114 | ||
115 | // | |
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. | |
119 | // | |
120 | vRect.x += 5; | |
121 | vRect.y -= 3; | |
122 | if (wxOwnerDrawn::OnDrawItem( rDc, vRect, eAct, eStat)) | |
123 | { | |
124 | size_t nCheckWidth = GetDefaultMarginWidth(); | |
125 | size_t nCheckHeight = m_pParent->GetItemHeight(); | |
126 | int nParentHeight; | |
127 | int nX = rRect.GetX(); | |
128 | int nY = rRect.GetY(); | |
129 | int nOldY = nY; | |
130 | wxColour vColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
131 | wxPen vPenBack; | |
132 | wxPen vPenPrev; | |
133 | ||
134 | m_pParent->GetSize( NULL, &nParentHeight); | |
135 | ||
136 | nY = nParentHeight - nY - nCheckHeight; | |
137 | vPenBack = wxPen(vColour, 1, wxSOLID); | |
138 | ||
139 | // | |
140 | // Erase the 1-pixel border | |
141 | // | |
142 | rDc.SetPen(vPenBack); | |
143 | rDc.DrawRectangle( nX, nY, nCheckWidth, nCheckHeight ); | |
144 | ||
145 | // | |
146 | // Now we draw the smaller rectangle | |
147 | // | |
148 | nY++; | |
149 | nCheckWidth -= 2; | |
150 | nCheckHeight -= 2; | |
151 | ||
152 | // | |
153 | // Draw hollow gray rectangle | |
154 | // | |
155 | rDc.SetPen(*wxGREY_PEN); | |
156 | rDc.DrawRectangle( nX, nY, nCheckWidth, nCheckHeight ); | |
157 | ||
158 | nX++; | |
159 | if (IsChecked()) | |
160 | { | |
161 | // | |
162 | // Draw the check by loading the sys standard bitmap and drawing it | |
163 | // | |
164 | HBITMAP hChkBmp = ::WinGetSysBitmap( HWND_DESKTOP, SBMP_MENUCHECK ); | |
165 | POINTL vPoint = {nX, nOldY + 3}; | |
166 | ||
167 | ::WinDrawBitmap( rDc.GetHPS(), | |
168 | hChkBmp, | |
169 | NULL, | |
170 | &vPoint, | |
171 | NULL, | |
172 | NULL, | |
173 | DBM_NORMAL | |
174 | ); | |
175 | } | |
176 | return true; | |
177 | } | |
178 | return false; | |
179 | } // end of wxCheckListBoxItem::OnDrawItem | |
180 | ||
181 | // | |
182 | // Change the state of the item and redraw it | |
183 | // | |
184 | void wxCheckListBoxItem::Check( bool bCheck ) | |
185 | { | |
186 | m_bChecked = bCheck; | |
187 | ||
188 | // | |
189 | // Index may be chanegd because new items were added/deleted | |
190 | // | |
191 | if (m_pParent->GetItemIndex(this) != (int)m_nIndex) | |
192 | { | |
193 | // | |
194 | // Update it | |
195 | // | |
196 | int nIndex = m_pParent->GetItemIndex(this); | |
197 | ||
198 | wxASSERT_MSG(nIndex != wxNOT_FOUND, wxT("what does this item do here?")); | |
199 | ||
200 | m_nIndex = (size_t)nIndex; | |
201 | } | |
202 | ||
203 | ||
204 | wxCommandEvent vEvent( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED,m_pParent->GetId()); | |
205 | ||
206 | vEvent.SetInt(m_nIndex); | |
207 | vEvent.SetEventObject(m_pParent); | |
208 | m_pParent->ProcessCommand(vEvent); | |
209 | } // end of wxCheckListBoxItem::Check | |
210 | ||
211 | ||
212 | // ---------------------------------------------------------------------------- | |
213 | // implementation of wxCheckListBox class | |
214 | // ---------------------------------------------------------------------------- | |
215 | ||
216 | // define event table | |
217 | // ------------------ | |
218 | BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox) | |
219 | EVT_CHAR(wxCheckListBox::OnChar) | |
220 | EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick) | |
221 | END_EVENT_TABLE() | |
222 | ||
223 | ||
224 | ||
225 | // | |
226 | // Control creation | |
227 | // ---------------- | |
228 | // | |
229 | ||
230 | // | |
231 | // Default ctor: use Create() to really create the control | |
232 | // | |
233 | wxCheckListBox::wxCheckListBox() | |
234 | :wxCheckListBoxBase() | |
235 | { | |
236 | } // end of wxCheckListBox::wxCheckListBox | |
237 | ||
238 | // | |
239 | // Ctor which creates the associated control | |
240 | // | |
241 | wxCheckListBox::wxCheckListBox ( wxWindow* pParent, | |
242 | wxWindowID vId, | |
243 | const wxPoint& rPos, | |
244 | const wxSize& rSize, | |
245 | int nStrings, | |
246 | const wxString asChoices[], | |
247 | long lStyle, | |
248 | const wxValidator& rVal, | |
249 | const wxString& rsName) | |
250 | :wxCheckListBoxBase() | |
251 | { | |
252 | Create( pParent, vId, rPos, rSize, nStrings, asChoices, lStyle | wxLB_OWNERDRAW, rVal, rsName ); | |
253 | } // end of wxCheckListBox::wxCheckListBox | |
254 | ||
255 | wxCheckListBox::wxCheckListBox ( wxWindow* pParent, | |
256 | wxWindowID vId, | |
257 | const wxPoint& rPos, | |
258 | const wxSize& rSize, | |
259 | const wxArrayString& asChoices, | |
260 | long lStyle, | |
261 | const wxValidator& rVal, | |
262 | const wxString& rsName ) | |
263 | :wxCheckListBoxBase() | |
264 | { | |
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 | |
269 | ||
270 | void wxCheckListBox::Delete(unsigned int n) | |
271 | { | |
272 | wxCHECK_RET( IsValid(n), | |
273 | wxT("invalid index in wxCheckListBox::Delete") ); | |
274 | wxListBox::Delete(n); | |
275 | ||
276 | // | |
277 | // Free memory | |
278 | // | |
279 | delete m_aItems[n]; | |
280 | m_aItems.RemoveAt(n); | |
281 | } // end of wxCheckListBox::Delete | |
282 | ||
283 | void wxCheckListBox::DoInsertItems(const wxArrayString& items, unsigned int pos) | |
284 | { | |
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++) | |
289 | { | |
290 | wxOwnerDrawn* pNewItem = CreateItem((size_t)(pos + i)); | |
291 | ||
292 | pNewItem->SetName(items[i]); | |
293 | m_aItems.Insert(pNewItem, (size_t)(pos + i)); | |
294 | ::WinSendMsg( (HWND)GetHWND(), | |
295 | LM_SETITEMHANDLE, | |
296 | (MPARAM)(i + pos), | |
297 | MPFROMP(pNewItem) | |
298 | ); | |
299 | } | |
300 | } // end of wxCheckListBox::InsertItems | |
301 | ||
302 | bool wxCheckListBox::SetFont ( const wxFont& rFont ) | |
303 | { | |
304 | for (unsigned int i = 0; i < m_aItems.GetCount(); i++) | |
305 | m_aItems[i]->SetFont(rFont); | |
306 | wxListBox::SetFont(rFont); | |
307 | return true; | |
308 | } // end of wxCheckListBox::SetFont | |
309 | ||
310 | ||
311 | ||
312 | // | |
313 | // Create/retrieve item | |
314 | // -------------------- | |
315 | // | |
316 | ||
317 | // | |
318 | // Create a check list box item | |
319 | // | |
320 | wxOwnerDrawn* wxCheckListBox::CreateItem(size_t nIndex) | |
321 | { | |
322 | wxCheckListBoxItem* pItem = new wxCheckListBoxItem( this, nIndex ); | |
323 | return pItem; | |
324 | } // end of wxCheckListBox::CreateItem | |
325 | ||
326 | ||
327 | ||
328 | // | |
329 | // Return item size | |
330 | // ---------------- | |
331 | // | |
332 | long wxCheckListBox::OS2OnMeasure ( WXMEASUREITEMSTRUCT* pItem ) | |
333 | { | |
334 | if (!pItem) | |
335 | pItem = (WXMEASUREITEMSTRUCT*)new OWNERITEM; | |
336 | if (wxListBox::OS2OnMeasure(pItem)) | |
337 | { | |
338 | POWNERITEM pStruct = (POWNERITEM)pItem; | |
339 | ||
340 | // | |
341 | // Save item height | |
342 | // | |
343 | m_nItemHeight = pStruct->rclItem.yTop - pStruct->rclItem.yBottom; | |
344 | ||
345 | // | |
346 | // Add place for the check mark | |
347 | // | |
348 | pStruct->rclItem.xRight += wxOwnerDrawn::GetDefaultMarginWidth(); | |
349 | return long(MRFROM2SHORT((USHORT)m_nItemHeight, (USHORT)(pStruct->rclItem.xRight - pStruct->rclItem.xLeft))); | |
350 | } | |
351 | return 0L; | |
352 | } // end of wxCheckListBox::CreateItem | |
353 | ||
354 | ||
355 | ||
356 | // | |
357 | // Check items | |
358 | // ----------- | |
359 | // | |
360 | bool wxCheckListBox::IsChecked(unsigned int uiIndex) const | |
361 | { | |
362 | return GetItem(uiIndex)->IsChecked(); | |
363 | } // end of wxCheckListBox::IsChecked | |
364 | ||
365 | void wxCheckListBox::Check(unsigned int uiIndex, bool bCheck) | |
366 | { | |
367 | GetItem(uiIndex)->Check(bCheck); | |
368 | } // end of wxCheckListBox::Check | |
369 | ||
370 | ||
371 | ||
372 | // | |
373 | // Process events | |
374 | // -------------- | |
375 | // | |
376 | void wxCheckListBox::OnChar ( wxKeyEvent& rEvent ) | |
377 | { | |
378 | if (rEvent.GetKeyCode() == WXK_SPACE) | |
379 | GetItem(GetSelection())->Toggle(); | |
380 | else | |
381 | rEvent.Skip(); | |
382 | } // end of wxCheckListBox::OnChar | |
383 | ||
384 | void wxCheckListBox::OnLeftClick ( wxMouseEvent& rEvent ) | |
385 | { | |
386 | // | |
387 | // Clicking on the item selects it, clicking on the checkmark toggles | |
388 | // | |
389 | if (rEvent.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth()) | |
390 | { | |
391 | int nParentHeight; | |
392 | wxScreenDC vDc; | |
393 | wxCoord vHeight; | |
394 | ||
395 | GetSize( NULL, &nParentHeight ); | |
396 | vDc.SetFont(GetFont()); | |
397 | vHeight = (wxCoord)(vDc.GetCharHeight() * 2.5); | |
398 | ||
399 | // | |
400 | // This, of course, will not work if the LB is scrolled | |
401 | // | |
402 | int nY = rEvent.GetY(); | |
403 | ||
404 | nY = nParentHeight - (nY + vHeight); | |
405 | ||
406 | size_t nItem = (size_t)(nY / vHeight); | |
407 | ||
408 | if (nItem < m_nNumItems) | |
409 | GetItem(nItem)->Toggle(); | |
410 | // | |
411 | // else: it's not an error, just click outside of client zone | |
412 | // | |
413 | } | |
414 | else | |
415 | { | |
416 | // | |
417 | // Implement default behaviour: clicking on the item selects it | |
418 | // | |
419 | rEvent.Skip(); | |
420 | } | |
421 | } // end of wxCheckListBox::OnLeftClick | |
422 | ||
423 | #endif // wxUSE_CHECKLISTBOX && wxUSE_OWNER_DRAWN |