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