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