]> git.saurik.com Git - wxWidgets.git/blob - src/os2/checklst.cpp
added default ctor for wxDirDialog
[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 #if wxUSE_VALIDATORS
263 long style, const wxValidator& val,
264 #endif
265 const wxString& name)
266 : wxListBox()
267 {
268 Create(parent, id, pos, size, nStrings, choices,
269 style | wxLB_OWNERDRAW, val, name);
270 }
271
272 void wxCheckListBox::Delete(int N)
273 {
274 wxCHECK_RET( N >= 0 && N < m_noItems,
275 wxT("invalid index in wxListBox::Delete") );
276
277 wxListBox::Delete(N);
278
279 // free memory
280 delete m_aItems[N];
281
282 m_aItems.Remove(N);
283 }
284
285 void wxCheckListBox::InsertItems(int nItems, const wxString items[], int pos)
286 {
287 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
288 wxT("invalid index in wxCheckListBox::InsertItems") );
289
290 wxListBox::InsertItems(nItems, items, pos);
291
292 int i;
293 for ( i = 0; i < nItems; i++ ) {
294 wxOwnerDrawn *pNewItem = CreateItem((size_t)(pos + i));
295 pNewItem->SetName(items[i]);
296 m_aItems.Insert(pNewItem, (size_t)(pos + i));
297 // ListBox_SetItemData((HWND)GetHWND(), i + pos, pNewItem);
298 }
299 }
300
301
302 bool wxCheckListBox::SetFont( const wxFont &font )
303 {
304 size_t i;
305 for (i=0; i < m_aItems.GetCount(); i++)
306 m_aItems[i]->SetFont(font);
307 wxListBox::SetFont(font);
308 return TRUE;
309 }
310
311 // create/retrieve item
312 // --------------------
313
314 // create a check list box item
315 wxOwnerDrawn *wxCheckListBox::CreateItem(size_t nIndex)
316 {
317 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
318 return pItem;
319 }
320
321 // return item size
322 // ----------------
323 // TODO:
324 /*
325 bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
326 {
327 if ( wxListBox::MSWOnMeasure(item) ) {
328 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
329
330 // save item height
331 m_nItemHeight = pStruct->itemHeight;
332
333 // add place for the check mark
334 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
335
336 return TRUE;
337 }
338
339 return FALSE;
340 }
341 */
342 // check items
343 // -----------
344
345 bool wxCheckListBox::IsChecked(size_t uiIndex) const
346 {
347 return GetItem(uiIndex)->IsChecked();
348 }
349
350 void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
351 {
352 GetItem(uiIndex)->Check(bCheck);
353 }
354
355 // process events
356 // --------------
357
358 void wxCheckListBox::OnChar(wxKeyEvent& event)
359 {
360 if ( event.KeyCode() == WXK_SPACE )
361 GetItem(GetSelection())->Toggle();
362 else
363 event.Skip();
364 }
365
366 void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
367 {
368 // clicking on the item selects it, clicking on the checkmark toggles
369 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
370 // TODO:
371 /*
372 size_t nItem = (size_t)::SendMessage
373 (
374 (HWND)GetHWND(),
375 LB_ITEMFROMPOINT,
376 0,
377 MAKELPARAM(event.GetX(), event.GetY())
378 );
379 */
380 size_t nItem = 0;
381
382 if ( nItem < (size_t)m_noItems )
383 GetItem(nItem)->Toggle();
384 //else: it's not an error, just click outside of client zone
385 }
386 else {
387 // implement default behaviour: clicking on the item selects it
388 event.Skip();
389 }
390 }
391
392 #endif
393