]> git.saurik.com Git - wxWidgets.git/blame - src/os2/checklst.cpp
A couple demo tweaks
[wxWidgets.git] / src / os2 / checklst.cpp
CommitLineData
0e320a79
DW
1///////////////////////////////////////////////////////////////////////////////
2// Name: checklst.cpp
3// Purpose: implementation of wxCheckListBox class
37f214d5 4// Author: David Webster
0616b838 5// Modified by:
37f214d5 6// Created: 10/13/99
0e320a79 7// RCS-ID: $Id$
37f214d5 8// Copyright: (c) David Webster
0e320a79
DW
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// headers & declarations
14// ============================================================================
15
37f214d5
DW
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"
0e320a79 32
37f214d5
DW
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)))
0e320a79
DW
42
43// ============================================================================
44// implementation
45// ============================================================================
46
0e320a79 47 IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
0e320a79 48
37f214d5
DW
49// ----------------------------------------------------------------------------
50// declaration and implementation of wxCheckListBoxItem class
51// ----------------------------------------------------------------------------
52
53class wxCheckListBoxItem : public wxOwnerDrawn
54{
55friend class wxCheckListBox;
56public:
57 // ctor
58 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
59
60 // drawing functions
61 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
62
63 // simple accessors
64 bool IsChecked() const { return m_bChecked; }
65 void Check(bool bCheck);
66 void Toggle() { Check(!IsChecked()); }
67
68private:
69 bool m_bChecked;
70 wxCheckListBox *m_pParent;
71 size_t m_nIndex;
72};
73
74wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
75 : wxOwnerDrawn("", TRUE) // checkable
76{
77 m_bChecked = FALSE;
78 m_pParent = pParent;
79 m_nIndex = nIndex;
80
81 // we don't initialize m_nCheckHeight/Width vars because it's
82 // done in OnMeasure while they are used only in OnDraw and we
83 // know that there will always be OnMeasure before OnDraw
84
85 // fix appearance
86 SetMarginWidth(GetDefaultMarginWidth());
87}
88
89/*
90 * JACS - I've got the owner-draw stuff partially working with WIN16,
91 * with a really horrible-looking cross for wxCheckListBox instead of a
92 * check - could use a bitmap check-mark instead, defined in wx.rc.
93 * Also there's a refresh problem whereby it doesn't always draw the
94 * check until you click to the right of it, which is OK for WIN32.
95 */
96
97bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
98 wxODAction act, wxODStatus stat)
99{
100 if ( IsChecked() )
101 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
102
103// TODO:
104/*
105
106 if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) {
107 // ## using native API for performance and precision
108 size_t nCheckWidth = GetDefaultMarginWidth(),
109 nCheckHeight = m_pParent->GetItemHeight();
110
111 int x = rc.GetX(),
112 y = rc.GetY();
113
114 HDC hdc = (HDC)dc.GetHDC();
115
116 // create pens
117 HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)),
118 hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)),
119 hpenPrev = (HPEN)SelectObject(hdc, hpenBack);
120
121 // we erase the 1-pixel border
122 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
123
124 // shift check mark 1 pixel to the right (it looks better like this)
125 x++;
126
127 if ( IsChecked() ) {
128 // first create a monochrome bitmap in a memory DC
129 HDC hdcMem = CreateCompatibleDC(hdc);
130 HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0);
131 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck);
132
133 // then draw a check mark into it
134 RECT rect ;
135 rect.left = 0 ;
136 rect.top = 0 ;
137 rect.right = nCheckWidth ;
138 rect.bottom = nCheckHeight ;
139
140#ifdef __WIN32__
141#ifndef __SC__
142 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
143#endif
144#else
145 // In WIN16, draw a cross
146 HPEN blackPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
147 HPEN whiteBrush = (HPEN)GetStockObject(WHITE_BRUSH);
148 HPEN hPenOld = (HPEN)::SelectObject(hdcMem, blackPen);
149 HPEN hBrushOld = (HPEN)::SelectObject(hdcMem, whiteBrush);
150 ::SetROP2(hdcMem, R2_COPYPEN);
151 Rectangle(hdcMem, 0, 0, nCheckWidth, nCheckHeight);
152 MoveTo(hdcMem, 0, 0);
153 LineTo(hdcMem, nCheckWidth, nCheckHeight);
154 MoveTo(hdcMem, nCheckWidth, 0);
155 LineTo(hdcMem, 0, nCheckHeight);
156 ::SelectObject(hdcMem, hPenOld);
157 ::SelectObject(hdcMem, hBrushOld);
158 ::DeleteObject(blackPen);
159#endif
160
161 // finally copy it to screen DC and clean up
162 BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight,
163 hdcMem, 0, 0, SRCCOPY);
164
165 SelectObject(hdcMem, hbmpOld);
166 DeleteObject(hbmpCheck);
167 DeleteDC(hdcMem);
168 }
169
170 // now we draw the smaller rectangle
171 y++;
172 nCheckWidth -= 2;
173 nCheckHeight -= 2;
174
175 // draw hollow gray rectangle
176 (void)SelectObject(hdc, hpenGray);
177 HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
178 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
179
180 // clean up
181 (void)SelectObject(hdc, hpenPrev);
182 (void)SelectObject(hdc, hbrPrev);
183
184 DeleteObject(hpenBack);
185 DeleteObject(hpenGray);
186
187//
188// dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
189//
190// if ( IsChecked() ) {
191// dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
192// dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
193// }
194//
195
196 return TRUE;
197 }
198*/
199 return FALSE;
200}
201
202// change the state of the item and redraw it
203void wxCheckListBoxItem::Check(bool check)
204{
205 m_bChecked = check;
206
207 // index may be chanegd because new items were added/deleted
208 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
209 {
210 // update it
211 int index = m_pParent->GetItemIndex(this);
212
213 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
214
215 m_nIndex = (size_t)index;
216 }
217
218 HWND hwndListbox = (HWND)m_pParent->GetHWND();
219
220// TODO:
221/*
222 RECT rcUpdate;
223 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
224 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
225 {
226 wxLogDebug(wxT("LB_GETITEMRECT failed"));
227 }
228
229 InvalidateRect(hwndListbox, &rcUpdate, FALSE);
230*/
231 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
232 event.SetInt(m_nIndex);
233 event.SetEventObject(m_pParent);
234 m_pParent->ProcessCommand(event);
235}
236
0e320a79
DW
237// ----------------------------------------------------------------------------
238// implementation of wxCheckListBox class
239// ----------------------------------------------------------------------------
240
241// define event table
242// ------------------
243BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
37f214d5
DW
244 EVT_CHAR(wxCheckListBox::OnChar)
245 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
0e320a79
DW
246END_EVENT_TABLE()
247
248// control creation
249// ----------------
250
251// def ctor: use Create() to really create the control
252wxCheckListBox::wxCheckListBox() : wxListBox()
253{
254}
255
256// ctor which creates the associated control
257wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
258 const wxPoint& pos, const wxSize& size,
259 int nStrings, const wxString choices[],
5d4b632b 260#if wxUSE_VALIDATORS
0e320a79 261 long style, const wxValidator& val,
5d4b632b 262#endif
0e320a79
DW
263 const wxString& name)
264 : wxListBox()
265{
37f214d5
DW
266 Create(parent, id, pos, size, nStrings, choices,
267 style | wxLB_OWNERDRAW, val, name);
268}
269
270void wxCheckListBox::Delete(int N)
271{
49dc8caa 272 wxCHECK_RET( N >= 0 && N < m_nNumItems,
37f214d5
DW
273 wxT("invalid index in wxListBox::Delete") );
274
275 wxListBox::Delete(N);
276
277 // free memory
278 delete m_aItems[N];
279
893758d5 280 m_aItems.RemoveAt(N);
37f214d5
DW
281}
282
283void wxCheckListBox::InsertItems(int nItems, const wxString items[], int pos)
284{
49dc8caa 285 wxCHECK_RET( pos >= 0 && pos <= m_nNumItems,
37f214d5
DW
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
300bool 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
313wxOwnerDrawn *wxCheckListBox::CreateItem(size_t nIndex)
314{
315 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
316 return pItem;
0e320a79
DW
317}
318
37f214d5
DW
319// return item size
320// ----------------
321// TODO:
322/*
323bool 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*/
0e320a79
DW
340// check items
341// -----------
342
37f214d5
DW
343bool wxCheckListBox::IsChecked(size_t uiIndex) const
344{
345 return GetItem(uiIndex)->IsChecked();
346}
347
348void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
0e320a79 349{
37f214d5 350 GetItem(uiIndex)->Check(bCheck);
0e320a79
DW
351}
352
37f214d5
DW
353// process events
354// --------------
355
356void wxCheckListBox::OnChar(wxKeyEvent& event)
0e320a79 357{
37f214d5
DW
358 if ( event.KeyCode() == WXK_SPACE )
359 GetItem(GetSelection())->Toggle();
360 else
361 event.Skip();
0e320a79
DW
362}
363
37f214d5
DW
364void 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
49dc8caa 380 if ( nItem < (size_t)m_nNumItems )
37f214d5
DW
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
0e320a79 391