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