]> git.saurik.com Git - wxWidgets.git/blob - src/generic/vlbox.cpp
Don't needlessly indirect through NSNotificationCenter.
[wxWidgets.git] / src / generic / vlbox.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/vlbox.cpp
3 // Purpose: implementation of wxVListBox
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 31.05.03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_LISTBOX
28
29 #include "wx/vlbox.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/settings.h"
33 #include "wx/dcclient.h"
34 #include "wx/listbox.h"
35 #endif //WX_PRECOMP
36
37 #include "wx/dcbuffer.h"
38 #include "wx/selstore.h"
39 #include "wx/renderer.h"
40
41 // ----------------------------------------------------------------------------
42 // event tables
43 // ----------------------------------------------------------------------------
44
45 BEGIN_EVENT_TABLE(wxVListBox, wxVScrolledWindow)
46 EVT_PAINT(wxVListBox::OnPaint)
47
48 EVT_KEY_DOWN(wxVListBox::OnKeyDown)
49 EVT_LEFT_DOWN(wxVListBox::OnLeftDown)
50 EVT_LEFT_DCLICK(wxVListBox::OnLeftDClick)
51
52 EVT_SET_FOCUS(wxVListBox::OnSetOrKillFocus)
53 EVT_KILL_FOCUS(wxVListBox::OnSetOrKillFocus)
54 END_EVENT_TABLE()
55
56 // ============================================================================
57 // implementation
58 // ============================================================================
59
60 IMPLEMENT_ABSTRACT_CLASS(wxVListBox, wxVScrolledWindow)
61
62 // ----------------------------------------------------------------------------
63 // wxVListBox creation
64 // ----------------------------------------------------------------------------
65
66 void wxVListBox::Init()
67 {
68 m_current =
69 m_anchor = wxNOT_FOUND;
70 m_selStore = NULL;
71 }
72
73 bool wxVListBox::Create(wxWindow *parent,
74 wxWindowID id,
75 const wxPoint& pos,
76 const wxSize& size,
77 long style,
78 const wxString& name)
79 {
80 #ifdef __WXMSW__
81 if ((style & wxBORDER_MASK) == wxDEFAULT)
82 style |= wxBORDER_THEME;
83 #endif
84
85 style |= wxWANTS_CHARS | wxFULL_REPAINT_ON_RESIZE;
86 if ( !wxVScrolledWindow::Create(parent, id, pos, size, style, name) )
87 return false;
88
89 if ( style & wxLB_MULTIPLE )
90 m_selStore = new wxSelectionStore;
91
92 // make sure the native widget has the right colour since we do
93 // transparent drawing by default
94 SetBackgroundColour(GetBackgroundColour());
95
96 // leave m_colBgSel in an invalid state: it means for OnDrawBackground()
97 // to use wxRendererNative instead of painting selection bg ourselves
98 m_colBgSel = wxNullColour;
99
100 // flicker-free drawing requires this
101 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
102
103 return true;
104 }
105
106 wxVListBox::~wxVListBox()
107 {
108 delete m_selStore;
109 }
110
111 void wxVListBox::SetItemCount(size_t count)
112 {
113 if ( m_selStore )
114 {
115 // tell the selection store that our number of items has changed
116 m_selStore->SetItemCount(count);
117 }
118
119 SetRowCount(count);
120 }
121
122 // ----------------------------------------------------------------------------
123 // selection handling
124 // ----------------------------------------------------------------------------
125
126 bool wxVListBox::IsSelected(size_t line) const
127 {
128 return m_selStore ? m_selStore->IsSelected(line) : (int)line == m_current;
129 }
130
131 bool wxVListBox::Select(size_t item, bool select)
132 {
133 wxCHECK_MSG( m_selStore, false,
134 _T("Select() may only be used with multiselection listbox") );
135
136 wxCHECK_MSG( item < GetItemCount(), false,
137 _T("Select(): invalid item index") );
138
139 bool changed = m_selStore->SelectItem(item, select);
140 if ( changed )
141 {
142 // selection really changed
143 RefreshRow(item);
144 }
145
146 DoSetCurrent(item);
147
148 return changed;
149 }
150
151 bool wxVListBox::SelectRange(size_t from, size_t to)
152 {
153 wxCHECK_MSG( m_selStore, false,
154 _T("SelectRange() may only be used with multiselection listbox") );
155
156 // make sure items are in correct order
157 if ( from > to )
158 {
159 size_t tmp = from;
160 from = to;
161 to = tmp;
162 }
163
164 wxCHECK_MSG( to < GetItemCount(), false,
165 _T("SelectRange(): invalid item index") );
166
167 wxArrayInt changed;
168 if ( !m_selStore->SelectRange(from, to, true, &changed) )
169 {
170 // too many items have changed, we didn't record them in changed array
171 // so we have no choice but to refresh everything between from and to
172 RefreshRows(from, to);
173 }
174 else // we've got the indices of the changed items
175 {
176 const size_t count = changed.GetCount();
177 if ( !count )
178 {
179 // nothing changed
180 return false;
181 }
182
183 // refresh just the lines which have really changed
184 for ( size_t n = 0; n < count; n++ )
185 {
186 RefreshRow(changed[n]);
187 }
188 }
189
190 // something changed
191 return true;
192 }
193
194 bool wxVListBox::DoSelectAll(bool select)
195 {
196 wxCHECK_MSG( m_selStore, false,
197 _T("SelectAll may only be used with multiselection listbox") );
198
199 size_t count = GetItemCount();
200 if ( count )
201 {
202 wxArrayInt changed;
203 if ( !m_selStore->SelectRange(0, count - 1, select) ||
204 !changed.IsEmpty() )
205 {
206 Refresh();
207
208 // something changed
209 return true;
210 }
211 }
212
213 return false;
214 }
215
216 bool wxVListBox::DoSetCurrent(int current)
217 {
218 wxASSERT_MSG( current == wxNOT_FOUND ||
219 (current >= 0 && (size_t)current < GetItemCount()),
220 _T("wxVListBox::DoSetCurrent(): invalid item index") );
221
222 if ( current == m_current )
223 {
224 // nothing to do
225 return false;
226 }
227
228 if ( m_current != wxNOT_FOUND )
229 RefreshRow(m_current);
230
231 m_current = current;
232
233 if ( m_current != wxNOT_FOUND )
234 {
235 // if the line is not visible at all, we scroll it into view but we
236 // don't need to refresh it -- it will be redrawn anyhow
237 if ( !IsVisible(m_current) )
238 {
239 ScrollToRow(m_current);
240 }
241 else // line is at least partly visible
242 {
243 // it is, indeed, only partly visible, so scroll it into view to
244 // make it entirely visible
245 while ( (size_t)m_current + 1 == GetVisibleRowsEnd() &&
246 ScrollToRow(GetVisibleBegin() + 1) ) ;
247
248 // but in any case refresh it as even if it was only partly visible
249 // before we need to redraw it entirely as its background changed
250 RefreshRow(m_current);
251 }
252 }
253
254 return true;
255 }
256
257 void wxVListBox::SendSelectedEvent()
258 {
259 wxASSERT_MSG( m_current != wxNOT_FOUND,
260 _T("SendSelectedEvent() shouldn't be called") );
261
262 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, GetId());
263 event.SetEventObject(this);
264 event.SetInt(m_current);
265
266 (void)GetEventHandler()->ProcessEvent(event);
267 }
268
269 void wxVListBox::SetSelection(int selection)
270 {
271 wxCHECK_RET( selection == wxNOT_FOUND ||
272 (selection >= 0 && (size_t)selection < GetItemCount()),
273 _T("wxVListBox::SetSelection(): invalid item index") );
274
275 if ( HasMultipleSelection() )
276 {
277 if (selection != wxNOT_FOUND)
278 Select(selection);
279 else
280 DeselectAll();
281 m_anchor = selection;
282 }
283
284 DoSetCurrent(selection);
285 }
286
287 size_t wxVListBox::GetSelectedCount() const
288 {
289 return m_selStore ? m_selStore->GetSelectedCount()
290 : m_current == wxNOT_FOUND ? 0 : 1;
291 }
292
293 int wxVListBox::GetFirstSelected(unsigned long& cookie) const
294 {
295 cookie = 0;
296
297 return GetNextSelected(cookie);
298 }
299
300 int wxVListBox::GetNextSelected(unsigned long& cookie) const
301 {
302 wxCHECK_MSG( m_selStore, wxNOT_FOUND,
303 _T("GetFirst/NextSelected() may only be used with multiselection listboxes") );
304
305 while ( cookie < GetItemCount() )
306 {
307 if ( IsSelected(cookie++) )
308 return cookie - 1;
309 }
310
311 return wxNOT_FOUND;
312 }
313
314 void wxVListBox::RefreshSelected()
315 {
316 // only refresh those items which are currently visible and selected:
317 for ( size_t n = GetVisibleBegin(), end = GetVisibleEnd(); n < end; n++ )
318 {
319 if ( IsSelected(n) )
320 RefreshRow(n);
321 }
322 }
323
324 // ----------------------------------------------------------------------------
325 // wxVListBox appearance parameters
326 // ----------------------------------------------------------------------------
327
328 void wxVListBox::SetMargins(const wxPoint& pt)
329 {
330 if ( pt != m_ptMargins )
331 {
332 m_ptMargins = pt;
333
334 Refresh();
335 }
336 }
337
338 void wxVListBox::SetSelectionBackground(const wxColour& col)
339 {
340 m_colBgSel = col;
341 }
342
343 // ----------------------------------------------------------------------------
344 // wxVListBox painting
345 // ----------------------------------------------------------------------------
346
347 wxCoord wxVListBox::OnGetRowHeight(size_t line) const
348 {
349 return OnMeasureItem(line) + 2*m_ptMargins.y;
350 }
351
352 void wxVListBox::OnDrawSeparator(wxDC& WXUNUSED(dc),
353 wxRect& WXUNUSED(rect),
354 size_t WXUNUSED(n)) const
355 {
356 }
357
358 void wxVListBox::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const
359 {
360 if ( m_colBgSel.IsOk() )
361 {
362 // we need to render selected and current items differently
363 const bool isSelected = IsSelected(n),
364 isCurrent = IsCurrent(n);
365 if ( isSelected || isCurrent )
366 {
367 if ( isSelected )
368 {
369 dc.SetBrush(wxBrush(m_colBgSel, wxSOLID));
370 }
371 else // !selected
372 {
373 dc.SetBrush(*wxTRANSPARENT_BRUSH);
374 }
375 dc.SetPen(*(isCurrent ? wxBLACK_PEN : wxTRANSPARENT_PEN));
376 dc.DrawRectangle(rect);
377 }
378 //else: do nothing for the normal items
379 }
380 else // use wxRendererNative for a more native look&feel:
381 {
382 int flags = 0;
383 if ( IsSelected(n) )
384 flags |= wxCONTROL_SELECTED;
385 if ( IsCurrent(n) )
386 flags |= wxCONTROL_CURRENT;
387 if ( wxWindow::FindFocus() == wx_const_cast(wxVListBox*, this) )
388 flags |= wxCONTROL_FOCUSED;
389
390 wxRendererNative::Get().DrawItemSelectionRect(
391 wx_const_cast(wxVListBox *, this), dc, rect, flags);
392 }
393 }
394
395 void wxVListBox::OnPaint(wxPaintEvent& WXUNUSED(event))
396 {
397 wxSize clientSize = GetClientSize();
398
399 wxAutoBufferedPaintDC dc(this);
400
401 // the update rectangle
402 wxRect rectUpdate = GetUpdateClientRect();
403
404 // fill it with background colour
405 dc.SetBackground(GetBackgroundColour());
406 dc.Clear();
407
408 // the bounding rectangle of the current line
409 wxRect rectRow;
410 rectRow.width = clientSize.x;
411
412 // iterate over all visible lines
413 const size_t lineMax = GetVisibleEnd();
414 for ( size_t line = GetVisibleBegin(); line < lineMax; line++ )
415 {
416 const wxCoord hRow = OnGetRowHeight(line);
417
418 rectRow.height = hRow;
419
420 // and draw the ones which intersect the update rect
421 if ( rectRow.Intersects(rectUpdate) )
422 {
423 // don't allow drawing outside of the lines rectangle
424 wxDCClipper clip(dc, rectRow);
425
426 wxRect rect = rectRow;
427 OnDrawBackground(dc, rect, line);
428
429 OnDrawSeparator(dc, rect, line);
430
431 rect.Deflate(m_ptMargins.x, m_ptMargins.y);
432 OnDrawItem(dc, rect, line);
433 }
434 else // no intersection
435 {
436 if ( rectRow.GetTop() > rectUpdate.GetBottom() )
437 {
438 // we are already below the update rect, no need to continue
439 // further
440 break;
441 }
442 //else: the next line may intersect the update rect
443 }
444
445 rectRow.y += hRow;
446 }
447 }
448
449 void wxVListBox::OnSetOrKillFocus(wxFocusEvent& WXUNUSED(event))
450 {
451 // we need to repaint the selection when we get the focus since
452 // wxRendererNative in general draws the focused selection differently
453 // from the unfocused selection (see OnDrawItem):
454 RefreshSelected();
455 }
456
457
458 // ============================================================================
459 // wxVListBox keyboard/mouse handling
460 // ============================================================================
461
462 void wxVListBox::DoHandleItemClick(int item, int flags)
463 {
464 // has anything worth telling the client code about happened?
465 bool notify = false;
466
467 if ( HasMultipleSelection() )
468 {
469 // select the iteem clicked?
470 bool select = true;
471
472 // NB: the keyboard interface we implement here corresponds to
473 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
474 // sense IMHO
475 if ( flags & ItemClick_Shift )
476 {
477 if ( m_current != wxNOT_FOUND )
478 {
479 if ( m_anchor == wxNOT_FOUND )
480 m_anchor = m_current;
481
482 select = false;
483
484 // only the range from the selection anchor to new m_current
485 // must be selected
486 if ( DeselectAll() )
487 notify = true;
488
489 if ( SelectRange(m_anchor, item) )
490 notify = true;
491 }
492 //else: treat it as ordinary click/keypress
493 }
494 else // Shift not pressed
495 {
496 m_anchor = item;
497
498 if ( flags & ItemClick_Ctrl )
499 {
500 select = false;
501
502 if ( !(flags & ItemClick_Kbd) )
503 {
504 Toggle(item);
505
506 // the status of the item has definitely changed
507 notify = true;
508 }
509 //else: Ctrl-arrow pressed, don't change selection
510 }
511 //else: behave as in single selection case
512 }
513
514 if ( select )
515 {
516 // make the clicked item the only selection
517 if ( DeselectAll() )
518 notify = true;
519
520 if ( Select(item) )
521 notify = true;
522 }
523 }
524
525 // in any case the item should become the current one
526 if ( DoSetCurrent(item) )
527 {
528 if ( !HasMultipleSelection() )
529 {
530 // this has also changed the selection for single selection case
531 notify = true;
532 }
533 }
534
535 if ( notify )
536 {
537 // notify the user about the selection change
538 SendSelectedEvent();
539 }
540 //else: nothing changed at all
541 }
542
543 // ----------------------------------------------------------------------------
544 // keyboard handling
545 // ----------------------------------------------------------------------------
546
547 void wxVListBox::OnKeyDown(wxKeyEvent& event)
548 {
549 // flags for DoHandleItemClick()
550 int flags = ItemClick_Kbd;
551
552 int current;
553 switch ( event.GetKeyCode() )
554 {
555 case WXK_HOME:
556 current = 0;
557 break;
558
559 case WXK_END:
560 current = GetRowCount() - 1;
561 break;
562
563 case WXK_DOWN:
564 if ( m_current == (int)GetRowCount() - 1 )
565 return;
566
567 current = m_current + 1;
568 break;
569
570 case WXK_UP:
571 if ( m_current == wxNOT_FOUND )
572 current = GetRowCount() - 1;
573 else if ( m_current != 0 )
574 current = m_current - 1;
575 else // m_current == 0
576 return;
577 break;
578
579 case WXK_PAGEDOWN:
580 PageDown();
581 current = GetVisibleBegin();
582 break;
583
584 case WXK_PAGEUP:
585 if ( m_current == (int)GetVisibleBegin() )
586 {
587 PageUp();
588 }
589
590 current = GetVisibleBegin();
591 break;
592
593 case WXK_SPACE:
594 // hack: pressing space should work like a mouse click rather than
595 // like a keyboard arrow press, so trick DoHandleItemClick() in
596 // thinking we were clicked
597 flags &= ~ItemClick_Kbd;
598 current = m_current;
599 break;
600
601 #ifdef __WXMSW__
602 case WXK_TAB:
603 // Since we are using wxWANTS_CHARS we need to send navigation
604 // events for the tabs on MSW
605 {
606 wxNavigationKeyEvent ne;
607 ne.SetDirection(!event.ShiftDown());
608 ne.SetCurrentFocus(this);
609 ne.SetEventObject(this);
610 GetParent()->GetEventHandler()->ProcessEvent(ne);
611 }
612 // fall through to default
613 #endif
614 default:
615 event.Skip();
616 current = 0; // just to silent the stupid compiler warnings
617 wxUnusedVar(current);
618 return;
619 }
620
621 if ( event.ShiftDown() )
622 flags |= ItemClick_Shift;
623 if ( event.ControlDown() )
624 flags |= ItemClick_Ctrl;
625
626 DoHandleItemClick(current, flags);
627 }
628
629 // ----------------------------------------------------------------------------
630 // wxVListBox mouse handling
631 // ----------------------------------------------------------------------------
632
633 void wxVListBox::OnLeftDown(wxMouseEvent& event)
634 {
635 SetFocus();
636
637 int item = HitTest(event.GetPosition());
638
639 if ( item != wxNOT_FOUND )
640 {
641 int flags = 0;
642 if ( event.ShiftDown() )
643 flags |= ItemClick_Shift;
644
645 // under Mac Apple-click is used in the same way as Ctrl-click
646 // elsewhere
647 #ifdef __WXMAC__
648 if ( event.MetaDown() )
649 #else
650 if ( event.ControlDown() )
651 #endif
652 flags |= ItemClick_Ctrl;
653
654 DoHandleItemClick(item, flags);
655 }
656 }
657
658 void wxVListBox::OnLeftDClick(wxMouseEvent& eventMouse)
659 {
660 int item = HitTest(eventMouse.GetPosition());
661 if ( item != wxNOT_FOUND )
662 {
663
664 // if item double-clicked was not yet selected, then treat
665 // this event as a left-click instead
666 if ( item == m_current )
667 {
668 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, GetId());
669 event.SetEventObject(this);
670 event.SetInt(item);
671
672 (void)GetEventHandler()->ProcessEvent(event);
673 }
674 else
675 {
676 OnLeftDown(eventMouse);
677 }
678
679 }
680 }
681
682
683 // ----------------------------------------------------------------------------
684 // use the same default attributes as wxListBox
685 // ----------------------------------------------------------------------------
686
687 //static
688 wxVisualAttributes
689 wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant)
690 {
691 return wxListBox::GetClassDefaultAttributes(variant);
692 }
693
694 #endif