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