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