]> git.saurik.com Git - wxWidgets.git/blob - src/os2/checklst.cpp
added operator[](unsigned int) const -- testing it now on Linux/axp,
[wxWidgets.git] / src / os2 / checklst.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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_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/os2/checklst.h"
31 #include "wx/log.h"
32
33 #define INCL_PM
34 #include <os2.h>
35
36 // ----------------------------------------------------------------------------
37 // private functions
38 // ----------------------------------------------------------------------------
39
40 // get item (converted to right type)
41 #define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
42
43 // ============================================================================
44 // implementation
45 // ============================================================================
46
47 #if !USE_SHARED_LIBRARY
48 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
49 #endif
50
51 // ----------------------------------------------------------------------------
52 // declaration and implementation of wxCheckListBoxItem class
53 // ----------------------------------------------------------------------------
54
55 class wxCheckListBoxItem : public wxOwnerDrawn
56 {
57 friend class wxCheckListBox;
58 public:
59 // ctor
60 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
61
62 // drawing functions
63 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
64
65 // simple accessors
66 bool IsChecked() const { return m_bChecked; }
67 void Check(bool bCheck);
68 void Toggle() { Check(!IsChecked()); }
69
70 private:
71 bool m_bChecked;
72 wxCheckListBox *m_pParent;
73 size_t m_nIndex;
74 };
75
76 wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
77 : wxOwnerDrawn("", TRUE) // checkable
78 {
79 m_bChecked = FALSE;
80 m_pParent = pParent;
81 m_nIndex = nIndex;
82
83 // we don't initialize m_nCheckHeight/Width vars because it's
84 // done in OnMeasure while they are used only in OnDraw and we
85 // know that there will always be OnMeasure before OnDraw
86
87 // fix appearance
88 SetMarginWidth(GetDefaultMarginWidth());
89 }
90
91 /*
92 * JACS - I've got the owner-draw stuff partially working with WIN16,
93 * with a really horrible-looking cross for wxCheckListBox instead of a
94 * check - could use a bitmap check-mark instead, defined in wx.rc.
95 * Also there's a refresh problem whereby it doesn't always draw the
96 * check until you click to the right of it, which is OK for WIN32.
97 */
98
99 bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
100 wxODAction act, wxODStatus stat)
101 {
102 if ( IsChecked() )
103 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
104
105 // TODO:
106 /*
107
108 if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) {
109 // ## using native API for performance and precision
110 size_t nCheckWidth = GetDefaultMarginWidth(),
111 nCheckHeight = m_pParent->GetItemHeight();
112
113 int x = rc.GetX(),
114 y = rc.GetY();
115
116 HDC hdc = (HDC)dc.GetHDC();
117
118 // create pens
119 HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)),
120 hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)),
121 hpenPrev = (HPEN)SelectObject(hdc, hpenBack);
122
123 // we erase the 1-pixel border
124 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
125
126 // shift check mark 1 pixel to the right (it looks better like this)
127 x++;
128
129 if ( IsChecked() ) {
130 // first create a monochrome bitmap in a memory DC
131 HDC hdcMem = CreateCompatibleDC(hdc);
132 HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0);
133 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck);
134
135 // then draw a check mark into it
136 RECT rect ;
137 rect.left = 0 ;
138 rect.top = 0 ;
139 rect.right = nCheckWidth ;
140 rect.bottom = nCheckHeight ;
141
142 #ifdef __WIN32__
143 #ifndef __SC__
144 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
145 #endif
146 #else
147 // In WIN16, draw a cross
148 HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
149 HPEN whiteBrush = (HPEN)GetStockObject(WHITE_BRUSH);
150 HPEN hPenOld = (HPEN)::SelectObject(hdcMem, blackPen);
151 HPEN hBrushOld = (HPEN)::SelectObject(hdcMem, whiteBrush);
152 ::SetROP2(hdcMem, R2_COPYPEN);
153 Rectangle(hdcMem, 0, 0, nCheckWidth, nCheckHeight);
154 MoveTo(hdcMem, 0, 0);
155 LineTo(hdcMem, nCheckWidth, nCheckHeight);
156 MoveTo(hdcMem, nCheckWidth, 0);
157 LineTo(hdcMem, 0, nCheckHeight);
158 ::SelectObject(hdcMem, hPenOld);
159 ::SelectObject(hdcMem, hBrushOld);
160 ::DeleteObject(blackPen);
161 #endif
162
163 // finally copy it to screen DC and clean up
164 BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight,
165 hdcMem, 0, 0, SRCCOPY);
166
167 SelectObject(hdcMem, hbmpOld);
168 DeleteObject(hbmpCheck);
169 DeleteDC(hdcMem);
170 }
171
172 // now we draw the smaller rectangle
173 y++;
174 nCheckWidth -= 2;
175 nCheckHeight -= 2;
176
177 // draw hollow gray rectangle
178 (void)SelectObject(hdc, hpenGray);
179 HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
180 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
181
182 // clean up
183 (void)SelectObject(hdc, hpenPrev);
184 (void)SelectObject(hdc, hbrPrev);
185
186 DeleteObject(hpenBack);
187 DeleteObject(hpenGray);
188
189 //
190 // dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
191 //
192 // if ( IsChecked() ) {
193 // dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
194 // dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
195 // }
196 //
197
198 return TRUE;
199 }
200 */
201 return FALSE;
202 }
203
204 // change the state of the item and redraw it
205 void wxCheckListBoxItem::Check(bool check)
206 {
207 m_bChecked = check;
208
209 // index may be chanegd because new items were added/deleted
210 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
211 {
212 // update it
213 int index = m_pParent->GetItemIndex(this);
214
215 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
216
217 m_nIndex = (size_t)index;
218 }
219
220 HWND hwndListbox = (HWND)m_pParent->GetHWND();
221
222 // TODO:
223 /*
224 RECT rcUpdate;
225 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
226 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
227 {
228 wxLogDebug(wxT("LB_GETITEMRECT failed"));
229 }
230
231 InvalidateRect(hwndListbox, &rcUpdate, FALSE);
232 */
233 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
234 event.SetInt(m_nIndex);
235 event.SetEventObject(m_pParent);
236 m_pParent->ProcessCommand(event);
237 }
238
239 // ----------------------------------------------------------------------------
240 // implementation of wxCheckListBox class
241 // ----------------------------------------------------------------------------
242
243 // define event table
244 // ------------------
245 BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
246 EVT_CHAR(wxCheckListBox::OnChar)
247 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
248 END_EVENT_TABLE()
249
250 // control creation
251 // ----------------
252
253 // def ctor: use Create() to really create the control
254 wxCheckListBox::wxCheckListBox() : wxListBox()
255 {
256 }
257
258 // ctor which creates the associated control
259 wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
260 const wxPoint& pos, const wxSize& size,
261 int nStrings, const wxString choices[],
262 long style, const wxValidator& val,
263 const wxString& name)
264 : wxListBox()
265 {
266 Create(parent, id, pos, size, nStrings, choices,
267 style | wxLB_OWNERDRAW, val, name);
268 }
269
270 void wxCheckListBox::Delete(int N)
271 {
272 wxCHECK_RET( N >= 0 && N < m_noItems,
273 wxT("invalid index in wxListBox::Delete") );
274
275 wxListBox::Delete(N);
276
277 // free memory
278 delete m_aItems[N];
279
280 m_aItems.Remove(N);
281 }
282
283 void wxCheckListBox::InsertItems(int nItems, const wxString items[], int pos)
284 {
285 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
286 wxT("invalid index in wxCheckListBox::InsertItems") );
287
288 wxListBox::InsertItems(nItems, items, pos);
289
290 int i;
291 for ( i = 0; i < nItems; i++ ) {
292 wxOwnerDrawn *pNewItem = CreateItem((size_t)(pos + i));
293 pNewItem->SetName(items[i]);
294 m_aItems.Insert(pNewItem, (size_t)(pos + i));
295 // ListBox_SetItemData((HWND)GetHWND(), i + pos, pNewItem);
296 }
297 }
298
299
300 bool wxCheckListBox::SetFont( const wxFont &font )
301 {
302 size_t i;
303 for (i=0; i < m_aItems.GetCount(); i++)
304 m_aItems[i]->SetFont(font);
305 wxListBox::SetFont(font);
306 return TRUE;
307 }
308
309 // create/retrieve item
310 // --------------------
311
312 // create a check list box item
313 wxOwnerDrawn *wxCheckListBox::CreateItem(size_t nIndex)
314 {
315 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
316 return pItem;
317 }
318
319 // return item size
320 // ----------------
321 // TODO:
322 /*
323 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
324 {
325 if ( wxListBox::MSWOnMeasure(item) ) {
326 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
327
328 // save item height
329 m_nItemHeight = pStruct->itemHeight;
330
331 // add place for the check mark
332 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
333
334 return TRUE;
335 }
336
337 return FALSE;
338 }
339 */
340 // check items
341 // -----------
342
343 bool wxCheckListBox::IsChecked(size_t uiIndex) const
344 {
345 return GetItem(uiIndex)->IsChecked();
346 }
347
348 void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
349 {
350 GetItem(uiIndex)->Check(bCheck);
351 }
352
353 // process events
354 // --------------
355
356 void wxCheckListBox::OnChar(wxKeyEvent& event)
357 {
358 if ( event.KeyCode() == WXK_SPACE )
359 GetItem(GetSelection())->Toggle();
360 else
361 event.Skip();
362 }
363
364 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
365 {
366 // clicking on the item selects it, clicking on the checkmark toggles
367 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
368 // TODO:
369 /*
370 size_t nItem = (size_t)::SendMessage
371 (
372 (HWND)GetHWND(),
373 LB_ITEMFROMPOINT,
374 0,
375 MAKELPARAM(event.GetX(), event.GetY())
376 );
377 */
378 size_t nItem = 0;
379
380 if ( nItem < (size_t)m_noItems )
381 GetItem(nItem)->Toggle();
382 //else: it's not an error, just click outside of client zone
383 }
384 else {
385 // implement default behaviour: clicking on the item selects it
386 event.Skip();
387 }
388 }
389
390 #endif
391