]> git.saurik.com Git - wxWidgets.git/blame - src/msw/checklst.cpp
Added MSWPositionForWxMenu() method which is used to translate wxWindows
[wxWidgets.git] / src / msw / checklst.cpp
CommitLineData
2bda0e17
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/checklst.cpp
3// Purpose: implementation of wxCheckListBox class
4// Author: Vadim Zeitlin
45187197 5// Modified by:
2bda0e17
KB
6// Created: 16.11.97
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
6c9a19aa 9// Licence: wxWindows licence
2bda0e17
KB
10///////////////////////////////////////////////////////////////////////////////
11
dd3c394a
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
2bda0e17
KB
21#pragma implementation "checklst.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28#pragma hdrstop
29#endif
30
47d67540 31#if wxUSE_OWNER_DRAWN
1c089c47 32
d90879fa
VZ
33#ifndef WX_PRECOMP
34 #include "wx/object.h"
35 #include "wx/colour.h"
36 #include "wx/font.h"
37 #include "wx/bitmap.h"
38 #include "wx/window.h"
39 #include "wx/listbox.h"
40 #include "wx/dcmemory.h"
41
42 #include "wx/settings.h"
43
44 #include "wx/log.h"
45#endif
46
2bda0e17 47#include "wx/ownerdrw.h"
d90879fa 48#include "wx/checklst.h"
2bda0e17 49
9ed0d735 50#include "wx/msw/wrapwin.h"
dd3c394a
VZ
51#include <windowsx.h>
52
4676948b
JS
53#include "wx/msw/private.h"
54
ae090fdb 55#if defined(__GNUWIN32_OLD__)
c42404a5 56 #include "wx/msw/gnuwin32/extra.h"
9a05fd8d
JS
57#endif
58
dd3c394a
VZ
59// ----------------------------------------------------------------------------
60// private functions
61// ----------------------------------------------------------------------------
62
63// get item (converted to right type)
64#define GetItem(n) ((wxCheckListBoxItem *)(GetItem(n)))
3d05544e 65
2bda0e17 66// ============================================================================
1169a919
JS
67// implementation of wxCheckListBoxBase
68// ============================================================================
69
70wxCheckListBoxBase::wxCheckListBoxBase()
71{
72}
73
74// ============================================================================
75// implementation of wxCheckListBox
2bda0e17
KB
76// ============================================================================
77
a2825c34
SC
78
79#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
80WX_DEFINE_FLAGS( wxCheckListBoxStyle )
81
3ff066a4 82wxBEGIN_FLAGS( wxCheckListBoxStyle )
bc9fb572
JS
83 // new style border flags, we put them first to
84 // use them for streaming out
3ff066a4
SC
85 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
86 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
87 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
88 wxFLAGS_MEMBER(wxBORDER_RAISED)
89 wxFLAGS_MEMBER(wxBORDER_STATIC)
90 wxFLAGS_MEMBER(wxBORDER_NONE)
bc9fb572
JS
91
92 // old style border flags
3ff066a4
SC
93 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
94 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
95 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
96 wxFLAGS_MEMBER(wxRAISED_BORDER)
97 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 98 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
99
100 // standard window styles
3ff066a4
SC
101 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
102 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
103 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
104 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 105 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
106 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
107 wxFLAGS_MEMBER(wxVSCROLL)
108 wxFLAGS_MEMBER(wxHSCROLL)
109
110 wxFLAGS_MEMBER(wxLB_SINGLE)
111 wxFLAGS_MEMBER(wxLB_MULTIPLE)
112 wxFLAGS_MEMBER(wxLB_EXTENDED)
113 wxFLAGS_MEMBER(wxLB_HSCROLL)
114 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
115 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
116 wxFLAGS_MEMBER(wxLB_SORT)
a2825c34 117 wxFLAGS_MEMBER(wxLB_OWNERDRAW)
3ff066a4
SC
118
119wxEND_FLAGS( wxCheckListBoxStyle )
bc9fb572
JS
120
121IMPLEMENT_DYNAMIC_CLASS_XTI(wxCheckListBox, wxListBox,"wx/checklst.h")
122
3ff066a4
SC
123wxBEGIN_PROPERTIES_TABLE(wxCheckListBox)
124 wxEVENT_PROPERTY( Toggle , wxEVT_COMMAND_CHECKLISTBOX_TOGGLED , wxCommandEvent )
a2825c34 125 wxPROPERTY_FLAGS( WindowStyle , wxCheckListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , wxLB_OWNERDRAW /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 126wxEND_PROPERTIES_TABLE()
bc9fb572 127
a2825c34
SC
128wxBEGIN_HANDLERS_TABLE(wxCheckListBox)
129wxEND_HANDLERS_TABLE()
130
131wxCONSTRUCTOR_4( wxCheckListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
132
bc9fb572 133#else
fd7ab28c 134IMPLEMENT_DYNAMIC_CLASS(wxCheckListBox, wxListBox)
bc9fb572 135#endif
2bda0e17
KB
136
137// ----------------------------------------------------------------------------
138// declaration and implementation of wxCheckListBoxItem class
139// ----------------------------------------------------------------------------
140
141class wxCheckListBoxItem : public wxOwnerDrawn
142{
2b5f62a0 143friend class WXDLLEXPORT wxCheckListBox;
2bda0e17
KB
144public:
145 // ctor
c86f1403 146 wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex);
2bda0e17
KB
147
148 // drawing functions
149 virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
150
e2ca995f
VZ
151 // simple accessors and operations
152 bool IsChecked() const { return m_bChecked; }
153
dd3c394a
VZ
154 void Check(bool bCheck);
155 void Toggle() { Check(!IsChecked()); }
2bda0e17 156
e2ca995f
VZ
157 void SendEvent();
158
2bda0e17 159private:
22f3361e
VZ
160
161 DECLARE_NO_COPY_CLASS(wxCheckListBoxItem)
2bda0e17
KB
162 bool m_bChecked;
163 wxCheckListBox *m_pParent;
e2ca995f 164 size_t m_nIndex;
2bda0e17
KB
165};
166
c86f1403 167wxCheckListBoxItem::wxCheckListBoxItem(wxCheckListBox *pParent, size_t nIndex)
2b5f62a0 168 : wxOwnerDrawn(wxEmptyString, TRUE) // checkable
2bda0e17
KB
169{
170 m_bChecked = FALSE;
171 m_pParent = pParent;
172 m_nIndex = nIndex;
173
174 // we don't initialize m_nCheckHeight/Width vars because it's
175 // done in OnMeasure while they are used only in OnDraw and we
176 // know that there will always be OnMeasure before OnDraw
177
178 // fix appearance
2bda0e17
KB
179 SetMarginWidth(GetDefaultMarginWidth());
180}
181
182/*
183 * JACS - I've got the owner-draw stuff partially working with WIN16,
184 * with a really horrible-looking cross for wxCheckListBox instead of a
185 * check - could use a bitmap check-mark instead, defined in wx.rc.
186 * Also there's a refresh problem whereby it doesn't always draw the
187 * check until you click to the right of it, which is OK for WIN32.
188 */
189
45187197 190bool wxCheckListBoxItem::OnDrawItem(wxDC& dc, const wxRect& rc,
2bda0e17
KB
191 wxODAction act, wxODStatus stat)
192{
193 if ( IsChecked() )
194 stat = (wxOwnerDrawn::wxODStatus)(stat | wxOwnerDrawn::wxODChecked);
195
196 if ( wxOwnerDrawn::OnDrawItem(dc, rc, act, stat) ) {
197 // ## using native API for performance and precision
c86f1403 198 size_t nCheckWidth = GetDefaultMarginWidth(),
2bda0e17 199 nCheckHeight = m_pParent->GetItemHeight();
45187197
RD
200
201 int x = rc.GetX(),
2bda0e17
KB
202 y = rc.GetY();
203
204 HDC hdc = (HDC)dc.GetHDC();
205
206 // create pens
207 HPEN hpenBack = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW)),
208 hpenGray = CreatePen(PS_SOLID, 0, RGB(128, 128, 128)),
45187197 209 hpenPrev = (HPEN)SelectObject(hdc, hpenBack);
2bda0e17
KB
210
211 // we erase the 1-pixel border
212 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
213
214 // shift check mark 1 pixel to the right (it looks better like this)
215 x++;
216
217 if ( IsChecked() ) {
218 // first create a monochrome bitmap in a memory DC
219 HDC hdcMem = CreateCompatibleDC(hdc);
220 HBITMAP hbmpCheck = CreateBitmap(nCheckWidth, nCheckHeight, 1, 1, 0);
45187197 221 HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, hbmpCheck);
2bda0e17
KB
222
223 // then draw a check mark into it
4ce1efe1 224
ee6e1b1d
VZ
225 RECT rect;
226 rect.left = 0;
227 rect.top = 0;
228 rect.right = nCheckWidth;
229 rect.bottom = nCheckHeight;
230
4676948b
JS
231#ifdef __WXWINCE__
232 DrawFrameControl(hdcMem, &rect, DFC_BUTTON, DFCS_BUTTONCHECK);
233#else
2bda0e17 234 DrawFrameControl(hdcMem, &rect, DFC_MENU, DFCS_MENUCHECK);
4676948b 235#endif
2bda0e17
KB
236
237 // finally copy it to screen DC and clean up
45187197 238 BitBlt(hdc, x, y, nCheckWidth - 1, nCheckHeight,
2bda0e17
KB
239 hdcMem, 0, 0, SRCCOPY);
240
241 SelectObject(hdcMem, hbmpOld);
242 DeleteObject(hbmpCheck);
243 DeleteDC(hdcMem);
244 }
245
246 // now we draw the smaller rectangle
247 y++;
248 nCheckWidth -= 2;
249 nCheckHeight -= 2;
250
251 // draw hollow gray rectangle
252 (void)SelectObject(hdc, hpenGray);
45187197 253 HBRUSH hbrPrev = (HBRUSH)SelectObject(hdc, GetStockObject(NULL_BRUSH));
2bda0e17
KB
254 Rectangle(hdc, x, y, x + nCheckWidth, y + nCheckHeight);
255
256 // clean up
257 (void)SelectObject(hdc, hpenPrev);
258 (void)SelectObject(hdc, hbrPrev);
259
260 DeleteObject(hpenBack);
261 DeleteObject(hpenGray);
262
263 /*
264 dc.DrawRectangle(x, y, nCheckWidth, nCheckHeight);
265
266 if ( IsChecked() ) {
267 dc.DrawLine(x, y, x + nCheckWidth, y + nCheckHeight);
268 dc.DrawLine(x, y + nCheckHeight, x + nCheckWidth, y);
269 }
270 */
271
272 return TRUE;
273 }
274
275 return FALSE;
276}
277
278// change the state of the item and redraw it
dd3c394a 279void wxCheckListBoxItem::Check(bool check)
45187197 280{
dd3c394a 281 m_bChecked = check;
2bda0e17 282
e2ca995f 283 // index may be changed because new items were added/deleted
dd3c394a
VZ
284 if ( m_pParent->GetItemIndex(this) != (int)m_nIndex )
285 {
286 // update it
287 int index = m_pParent->GetItemIndex(this);
288
223d09f6 289 wxASSERT_MSG( index != wxNOT_FOUND, wxT("what does this item do here?") );
dd3c394a
VZ
290
291 m_nIndex = (size_t)index;
292 }
2bda0e17 293
c4dbfe14
VZ
294 HWND hwndListbox = (HWND)m_pParent->GetHWND();
295
296 #ifdef __WIN32__
297 RECT rcUpdate;
0c35333e 298
c4dbfe14
VZ
299 if ( ::SendMessage(hwndListbox, LB_GETITEMRECT,
300 m_nIndex, (LPARAM)&rcUpdate) == LB_ERR )
301 {
223d09f6 302 wxLogDebug(wxT("LB_GETITEMRECT failed"));
c4dbfe14
VZ
303 }
304 #else // Win16
305 // FIXME this doesn't work if the listbox is scrolled!
306 size_t nHeight = m_pParent->GetItemHeight();
307 size_t y = m_nIndex * nHeight;
197dd9af
PA
308 RECT rcUpdate ;
309 rcUpdate.left = 0 ;
310 rcUpdate.top = y ;
311 rcUpdate.right = GetDefaultMarginWidth() ;
312 rcUpdate.bottom = y + nHeight ;
c4dbfe14
VZ
313 #endif // Win32/16
314
315 InvalidateRect(hwndListbox, &rcUpdate, FALSE);
e2ca995f 316}
0c35333e 317
e2ca995f
VZ
318// send an "item checked" event
319void wxCheckListBoxItem::SendEvent()
320{
dd3c394a
VZ
321 wxCommandEvent event(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, m_pParent->GetId());
322 event.SetInt(m_nIndex);
323 event.SetEventObject(m_pParent);
324 m_pParent->ProcessCommand(event);
2bda0e17
KB
325}
326
327// ----------------------------------------------------------------------------
328// implementation of wxCheckListBox class
329// ----------------------------------------------------------------------------
330
331// define event table
332// ------------------
333BEGIN_EVENT_TABLE(wxCheckListBox, wxListBox)
d90879fa 334 EVT_KEY_DOWN(wxCheckListBox::OnKeyDown)
2bda0e17
KB
335 EVT_LEFT_DOWN(wxCheckListBox::OnLeftClick)
336END_EVENT_TABLE()
337
338// control creation
339// ----------------
340
341// def ctor: use Create() to really create the control
d90879fa 342wxCheckListBox::wxCheckListBox()
2bda0e17
KB
343{
344}
345
346// ctor which creates the associated control
debe6624 347wxCheckListBox::wxCheckListBox(wxWindow *parent, wxWindowID id,
2bda0e17 348 const wxPoint& pos, const wxSize& size,
debe6624
JS
349 int nStrings, const wxString choices[],
350 long style, const wxValidator& val,
dd3c394a 351 const wxString& name)
2bda0e17 352{
799cea00 353 Create(parent, id, pos, size, nStrings, choices, style, val, name);
dd3c394a
VZ
354}
355
799cea00
VS
356bool wxCheckListBox::Create(wxWindow *parent, wxWindowID id,
357 const wxPoint& pos, const wxSize& size,
358 int n, const wxString choices[],
359 long style,
360 const wxValidator& validator, const wxString& name)
361{
362 return wxListBox::Create(parent, id, pos, size, n, choices,
363 style | wxLB_OWNERDRAW, validator, name);
364}
799cea00 365
e2ca995f
VZ
366// misc overloaded methods
367// -----------------------
799cea00 368
dd3c394a
VZ
369void wxCheckListBox::Delete(int N)
370{
371 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 372 wxT("invalid index in wxListBox::Delete") );
dd3c394a
VZ
373
374 wxListBox::Delete(N);
375
376 // free memory
377 delete m_aItems[N];
378
b54e41c5 379 m_aItems.RemoveAt(N);
dd3c394a
VZ
380}
381
0c35333e
RD
382bool wxCheckListBox::SetFont( const wxFont &font )
383{
384 size_t i;
07cf98cb 385 for ( i = 0; i < m_aItems.GetCount(); i++ )
0c35333e 386 m_aItems[i]->SetFont(font);
07cf98cb 387
0c35333e 388 wxListBox::SetFont(font);
07cf98cb 389
0c35333e
RD
390 return TRUE;
391}
392
2bda0e17
KB
393// create/retrieve item
394// --------------------
395
396// create a check list box item
2b5f62a0 397wxOwnerDrawn *wxCheckListBox::CreateLboxItem(size_t nIndex)
2bda0e17
KB
398{
399 wxCheckListBoxItem *pItem = new wxCheckListBoxItem(this, nIndex);
2bda0e17
KB
400 return pItem;
401}
402
2bda0e17
KB
403// return item size
404// ----------------
405bool wxCheckListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
406{
407 if ( wxListBox::MSWOnMeasure(item) ) {
408 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
409
410 // save item height
411 m_nItemHeight = pStruct->itemHeight;
412
413 // add place for the check mark
414 pStruct->itemWidth += wxOwnerDrawn::GetDefaultMarginWidth();
415
416 return TRUE;
417 }
418
419 return FALSE;
420}
421
422// check items
423// -----------
424
c86f1403 425bool wxCheckListBox::IsChecked(size_t uiIndex) const
2bda0e17 426{
d90879fa
VZ
427 wxCHECK_MSG( uiIndex < (size_t)GetCount(), FALSE, _T("bad wxCheckListBox index") );
428
429 return GetItem(uiIndex)->IsChecked();
2bda0e17
KB
430}
431
c86f1403 432void wxCheckListBox::Check(size_t uiIndex, bool bCheck)
2bda0e17 433{
d90879fa
VZ
434 wxCHECK_RET( uiIndex < (size_t)GetCount(), _T("bad wxCheckListBox index") );
435
436 GetItem(uiIndex)->Check(bCheck);
2bda0e17
KB
437}
438
439// process events
440// --------------
441
d90879fa 442void wxCheckListBox::OnKeyDown(wxKeyEvent& event)
2bda0e17 443{
d90879fa
VZ
444 // what do we do?
445 enum
446 {
447 None,
448 Toggle,
449 Set,
450 Clear
451 } oper;
452
77e00fe9 453 switch ( event.GetKeyCode() )
d90879fa
VZ
454 {
455 case WXK_SPACE:
456 oper = Toggle;
457 break;
458
459 case WXK_NUMPAD_ADD:
460 case '+':
461 oper = Set;
462 break;
463
464 case WXK_NUMPAD_SUBTRACT:
465 case '-':
466 oper = Clear;
467 break;
468
469 default:
470 oper = None;
471 }
472
473 if ( oper != None )
474 {
475 wxArrayInt selections;
a8f473ac 476 int count = 0;
d90879fa
VZ
477 if ( HasMultipleSelection() )
478 {
479 count = GetSelections(selections);
480 }
481 else
482 {
a8f473ac
RD
483 int sel = GetSelection();
484 if (sel != -1)
485 {
486 count = 1;
487 selections.Add(sel);
488 }
d90879fa
VZ
489 }
490
491 for ( int i = 0; i < count; i++ )
492 {
493 wxCheckListBoxItem *item = GetItem(selections[i]);
494 if ( !item )
495 {
496 wxFAIL_MSG( _T("no wxCheckListBoxItem?") );
497 continue;
498 }
499
500 switch ( oper )
501 {
502 case Toggle:
503 item->Toggle();
504 break;
505
506 case Set:
507 case Clear:
508 item->Check( oper == Set );
509 break;
510
511 default:
512 wxFAIL_MSG( _T("what should this key do?") );
513 }
e2ca995f
VZ
514
515 // we should send an event as this has been done by the user and
516 // not by the program
517 item->SendEvent();
d90879fa
VZ
518 }
519 }
520 else // nothing to do
521 {
522 event.Skip();
523 }
2bda0e17
KB
524}
525
526void wxCheckListBox::OnLeftClick(wxMouseEvent& event)
527{
528 // clicking on the item selects it, clicking on the checkmark toggles
c1888b05 529 if ( event.GetX() <= wxOwnerDrawn::GetDefaultMarginWidth() ) {
15c3723c 530 int nItem = HitTest(event.GetX(), event.GetY());
c4dbfe14 531
e2ca995f
VZ
532 if ( nItem != wxNOT_FOUND ) {
533 wxCheckListBoxItem *item = GetItem(nItem);
534 item->Toggle();
535 item->SendEvent();
536 }
2bda0e17
KB
537 //else: it's not an error, just click outside of client zone
538 }
539 else {
540 // implement default behaviour: clicking on the item selects it
541 event.Skip();
542 }
543}
1c089c47 544
15c3723c
VZ
545int wxCheckListBox::DoHitTestItem(wxCoord x, wxCoord y) const
546{
547 #ifdef __WIN32__
548 int nItem = (int)::SendMessage
549 (
550 (HWND)GetHWND(),
551 LB_ITEMFROMPOINT,
552 0,
553 MAKELPARAM(x, y)
554 );
555 #else // Win16
556 // FIXME this doesn't work when the listbox is scrolled!
557 int nItem = y / m_nItemHeight;
558 #endif // Win32/16
559
560 return nItem >= m_noItems ? wxNOT_FOUND : nItem;
561}
562
1c089c47
JS
563#endif
564