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