]> git.saurik.com Git - wxWidgets.git/blame - src/generic/vlbox.cpp
no real changes but replace manual calls to Set{Text,Bk}Color() with matching calls...
[wxWidgets.git] / src / generic / vlbox.cpp
CommitLineData
e0c6027b 1///////////////////////////////////////////////////////////////////////////////
e19ac18a 2// Name: src/generic/vlbox.cpp
e0c6027b
VZ
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>
0a53b9b8 9// License: wxWindows license
e0c6027b
VZ
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
179e085f
RN
27#if wxUSE_LISTBOX
28
2a673eb1
WS
29#include "wx/vlbox.h"
30
e0c6027b
VZ
31#ifndef WX_PRECOMP
32 #include "wx/settings.h"
bb178b29 33 #include "wx/dcclient.h"
2a673eb1 34 #include "wx/listbox.h"
e0c6027b
VZ
35#endif //WX_PRECOMP
36
0975a8a0 37#include "wx/dcbuffer.h"
be465555 38#include "wx/selstore.h"
04125489 39#include "wx/renderer.h"
e0c6027b
VZ
40
41// ----------------------------------------------------------------------------
42// event tables
43// ----------------------------------------------------------------------------
44
45BEGIN_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)
04125489
VZ
51
52 EVT_SET_FOCUS(wxVListBox::OnSetOrKillFocus)
53 EVT_KILL_FOCUS(wxVListBox::OnSetOrKillFocus)
e0c6027b
VZ
54END_EVENT_TABLE()
55
56// ============================================================================
57// implementation
58// ============================================================================
59
0c8392ca
RD
60IMPLEMENT_ABSTRACT_CLASS(wxVListBox, wxVScrolledWindow)
61
e0c6027b
VZ
62// ----------------------------------------------------------------------------
63// wxVListBox creation
64// ----------------------------------------------------------------------------
65
66void wxVListBox::Init()
67{
970b97a2
VZ
68 m_current =
69 m_anchor = wxNOT_FOUND;
be465555 70 m_selStore = NULL;
e0c6027b
VZ
71}
72
73bool wxVListBox::Create(wxWindow *parent,
74 wxWindowID id,
75 const wxPoint& pos,
76 const wxSize& size,
e0c6027b
VZ
77 long style,
78 const wxString& name)
79{
a047aff2
JS
80#ifdef __WXMSW__
81 if ((style & wxBORDER_MASK) == wxDEFAULT)
82 style |= wxBORDER_THEME;
83#endif
84
fef7400f 85 style |= wxWANTS_CHARS | wxFULL_REPAINT_ON_RESIZE;
e0c6027b
VZ
86 if ( !wxVScrolledWindow::Create(parent, id, pos, size, style, name) )
87 return false;
88
be465555
VZ
89 if ( style & wxLB_MULTIPLE )
90 m_selStore = new wxSelectionStore;
e0c6027b 91
dc596072
RD
92 // make sure the native widget has the right colour since we do
93 // transparent drawing by default
94 SetBackgroundColour(GetBackgroundColour());
04125489
VZ
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;
9a9b4940 99
0975a8a0
JS
100 // flicker-free drawing requires this
101 SetBackgroundStyle(wxBG_STYLE_CUSTOM);
102
e0c6027b
VZ
103 return true;
104}
105
be465555
VZ
106wxVListBox::~wxVListBox()
107{
108 delete m_selStore;
109}
110
111void 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
f18eaf26 119 SetRowCount(count);
be465555
VZ
120}
121
e0c6027b
VZ
122// ----------------------------------------------------------------------------
123// selection handling
124// ----------------------------------------------------------------------------
125
be465555
VZ
126bool wxVListBox::IsSelected(size_t line) const
127{
128 return m_selStore ? m_selStore->IsSelected(line) : (int)line == m_current;
129}
130
131bool 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
e02c72fa 143 RefreshRow(item);
be465555
VZ
144 }
145
146 DoSetCurrent(item);
147
148 return changed;
149}
150
151bool wxVListBox::SelectRange(size_t from, size_t to)
e0c6027b 152{
be465555
VZ
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
e02c72fa 172 RefreshRows(from, to);
be465555
VZ
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 {
e02c72fa 186 RefreshRow(changed[n]);
be465555
VZ
187 }
188 }
189
190 // something changed
191 return true;
192}
193
194bool 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
216bool wxVListBox::DoSetCurrent(int current)
217{
6c9210a7
VZ
218 wxASSERT_MSG( current == wxNOT_FOUND ||
219 (current >= 0 && (size_t)current < GetItemCount()),
220 _T("wxVListBox::DoSetCurrent(): invalid item index") );
221
be465555 222 if ( current == m_current )
e0c6027b
VZ
223 {
224 // nothing to do
be465555 225 return false;
e0c6027b
VZ
226 }
227
be465555 228 if ( m_current != wxNOT_FOUND )
e02c72fa 229 RefreshRow(m_current);
e0c6027b 230
be465555 231 m_current = current;
e0c6027b 232
be465555 233 if ( m_current != wxNOT_FOUND )
e0c6027b
VZ
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
be465555 237 if ( !IsVisible(m_current) )
e0c6027b 238 {
e02c72fa 239 ScrollToRow(m_current);
e0c6027b
VZ
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
e02c72fa
VZ
245 while ( (size_t)m_current + 1 == GetVisibleRowsEnd() &&
246 ScrollToRow(GetVisibleBegin() + 1) ) ;
e0c6027b
VZ
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
e02c72fa 250 RefreshRow(m_current);
e0c6027b 251 }
be465555 252 }
e0c6027b 253
be465555
VZ
254 return true;
255}
e0c6027b 256
be465555
VZ
257void 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);
687706f5 264 event.SetInt(m_current);
be465555
VZ
265
266 (void)GetEventHandler()->ProcessEvent(event);
267}
268
269void wxVListBox::SetSelection(int selection)
270{
6c9210a7
VZ
271 wxCHECK_RET( selection == wxNOT_FOUND ||
272 (selection >= 0 && (size_t)selection < GetItemCount()),
273 _T("wxVListBox::SetSelection(): invalid item index") );
274
de5d3a20
VZ
275 if ( HasMultipleSelection() )
276 {
ad679137
VZ
277 if (selection != wxNOT_FOUND)
278 Select(selection);
279 else
280 DeselectAll();
de5d3a20
VZ
281 m_anchor = selection;
282 }
be465555
VZ
283
284 DoSetCurrent(selection);
285}
286
287size_t wxVListBox::GetSelectedCount() const
288{
289 return m_selStore ? m_selStore->GetSelectedCount()
290 : m_current == wxNOT_FOUND ? 0 : 1;
291}
292
293int wxVListBox::GetFirstSelected(unsigned long& cookie) const
294{
295 cookie = 0;
296
297 return GetNextSelected(cookie);
298}
299
300int 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;
e0c6027b 309 }
be465555
VZ
310
311 return wxNOT_FOUND;
e0c6027b
VZ
312}
313
04125489
VZ
314void 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) )
f18eaf26 320 RefreshRow(n);
04125489
VZ
321 }
322}
323
e0c6027b 324// ----------------------------------------------------------------------------
9a9b4940 325// wxVListBox appearance parameters
e0c6027b
VZ
326// ----------------------------------------------------------------------------
327
328void wxVListBox::SetMargins(const wxPoint& pt)
329{
330 if ( pt != m_ptMargins )
331 {
332 m_ptMargins = pt;
333
334 Refresh();
335 }
336}
337
9a9b4940
VZ
338void wxVListBox::SetSelectionBackground(const wxColour& col)
339{
340 m_colBgSel = col;
341}
342
343// ----------------------------------------------------------------------------
344// wxVListBox painting
345// ----------------------------------------------------------------------------
346
e02c72fa 347wxCoord wxVListBox::OnGetRowHeight(size_t line) const
e0c6027b
VZ
348{
349 return OnMeasureItem(line) + 2*m_ptMargins.y;
350}
351
352void wxVListBox::OnDrawSeparator(wxDC& WXUNUSED(dc),
353 wxRect& WXUNUSED(rect),
354 size_t WXUNUSED(n)) const
355{
356}
357
c848185a
VZ
358bool
359wxVListBox::DoDrawSolidBackground(const wxColour& col,
360 wxDC& dc,
361 const wxRect& rect,
362 size_t n) const
27d0dcd0 363{
c848185a
VZ
364 if ( !col.IsOk() )
365 return false;
366
367 // we need to render selected and current items differently
368 const bool isSelected = IsSelected(n),
369 isCurrent = IsCurrent(n);
370 if ( isSelected || isCurrent )
27d0dcd0 371 {
c848185a 372 if ( isSelected )
27d0dcd0 373 {
04ee05f9 374 dc.SetBrush(wxBrush(col, wxBRUSHSTYLE_SOLID));
27d0dcd0 375 }
c848185a
VZ
376 else // !selected
377 {
378 dc.SetBrush(*wxTRANSPARENT_BRUSH);
379 }
380 dc.SetPen(*(isCurrent ? wxBLACK_PEN : wxTRANSPARENT_PEN));
381 dc.DrawRectangle(rect);
04125489 382 }
c848185a
VZ
383 //else: do nothing for the normal items
384
385 return true;
386}
387
388void wxVListBox::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const
389{
390 // use wxRendererNative for more native look unless we use custom bg colour
391 if ( !DoDrawSolidBackground(m_colBgSel, dc, rect, n) )
04125489
VZ
392 {
393 int flags = 0;
394 if ( IsSelected(n) )
395 flags |= wxCONTROL_SELECTED;
396 if ( IsCurrent(n) )
397 flags |= wxCONTROL_CURRENT;
d836b8bc 398 if ( wxWindow::FindFocus() == wx_const_cast(wxVListBox*, this) )
04125489
VZ
399 flags |= wxCONTROL_FOCUSED;
400
401 wxRendererNative::Get().DrawItemSelectionRect(
402 wx_const_cast(wxVListBox *, this), dc, rect, flags);
27d0dcd0 403 }
27d0dcd0
VZ
404}
405
406void wxVListBox::OnPaint(wxPaintEvent& WXUNUSED(event))
e0c6027b 407{
0975a8a0
JS
408 wxSize clientSize = GetClientSize();
409
2e992e06 410 wxAutoBufferedPaintDC dc(this);
e0c6027b
VZ
411
412 // the update rectangle
413 wxRect rectUpdate = GetUpdateClientRect();
414
959b1a33
VZ
415 // fill it with background colour
416 dc.SetBackground(GetBackgroundColour());
0975a8a0
JS
417 dc.Clear();
418
e0c6027b 419 // the bounding rectangle of the current line
e02c72fa
VZ
420 wxRect rectRow;
421 rectRow.width = clientSize.x;
e0c6027b
VZ
422
423 // iterate over all visible lines
b1b408af 424 const size_t lineMax = GetVisibleEnd();
e02c72fa 425 for ( size_t line = GetVisibleBegin(); line < lineMax; line++ )
e0c6027b 426 {
e02c72fa 427 const wxCoord hRow = OnGetRowHeight(line);
e0c6027b 428
e02c72fa 429 rectRow.height = hRow;
e0c6027b
VZ
430
431 // and draw the ones which intersect the update rect
e02c72fa 432 if ( rectRow.Intersects(rectUpdate) )
e0c6027b
VZ
433 {
434 // don't allow drawing outside of the lines rectangle
e02c72fa 435 wxDCClipper clip(dc, rectRow);
e0c6027b 436
e02c72fa 437 wxRect rect = rectRow;
27d0dcd0
VZ
438 OnDrawBackground(dc, rect, line);
439
e0c6027b
VZ
440 OnDrawSeparator(dc, rect, line);
441
442 rect.Deflate(m_ptMargins.x, m_ptMargins.y);
443 OnDrawItem(dc, rect, line);
444 }
445 else // no intersection
446 {
e02c72fa 447 if ( rectRow.GetTop() > rectUpdate.GetBottom() )
e0c6027b
VZ
448 {
449 // we are already below the update rect, no need to continue
450 // further
451 break;
452 }
453 //else: the next line may intersect the update rect
454 }
455
e02c72fa 456 rectRow.y += hRow;
e0c6027b
VZ
457 }
458}
459
04125489
VZ
460void wxVListBox::OnSetOrKillFocus(wxFocusEvent& WXUNUSED(event))
461{
462 // we need to repaint the selection when we get the focus since
463 // wxRendererNative in general draws the focused selection differently
464 // from the unfocused selection (see OnDrawItem):
465 RefreshSelected();
466}
467
468
be465555
VZ
469// ============================================================================
470// wxVListBox keyboard/mouse handling
471// ============================================================================
472
970b97a2 473void wxVListBox::DoHandleItemClick(int item, int flags)
be465555
VZ
474{
475 // has anything worth telling the client code about happened?
476 bool notify = false;
477
478 if ( HasMultipleSelection() )
479 {
480 // select the iteem clicked?
481 bool select = true;
482
483 // NB: the keyboard interface we implement here corresponds to
484 // wxLB_EXTENDED rather than wxLB_MULTIPLE but this one makes more
485 // sense IMHO
970b97a2 486 if ( flags & ItemClick_Shift )
be465555
VZ
487 {
488 if ( m_current != wxNOT_FOUND )
489 {
970b97a2
VZ
490 if ( m_anchor == wxNOT_FOUND )
491 m_anchor = m_current;
492
be465555
VZ
493 select = false;
494
970b97a2
VZ
495 // only the range from the selection anchor to new m_current
496 // must be selected
be465555
VZ
497 if ( DeselectAll() )
498 notify = true;
499
970b97a2 500 if ( SelectRange(m_anchor, item) )
be465555
VZ
501 notify = true;
502 }
503 //else: treat it as ordinary click/keypress
504 }
970b97a2 505 else // Shift not pressed
be465555 506 {
970b97a2 507 m_anchor = item;
be465555 508
970b97a2
VZ
509 if ( flags & ItemClick_Ctrl )
510 {
511 select = false;
be465555 512
970b97a2
VZ
513 if ( !(flags & ItemClick_Kbd) )
514 {
515 Toggle(item);
516
517 // the status of the item has definitely changed
518 notify = true;
519 }
520 //else: Ctrl-arrow pressed, don't change selection
521 }
522 //else: behave as in single selection case
be465555 523 }
be465555
VZ
524
525 if ( select )
526 {
527 // make the clicked item the only selection
528 if ( DeselectAll() )
529 notify = true;
530
531 if ( Select(item) )
532 notify = true;
533 }
534 }
535
536 // in any case the item should become the current one
537 if ( DoSetCurrent(item) )
538 {
539 if ( !HasMultipleSelection() )
540 {
541 // this has also changed the selection for single selection case
542 notify = true;
543 }
544 }
545
546 if ( notify )
547 {
548 // notify the user about the selection change
549 SendSelectedEvent();
550 }
551 //else: nothing changed at all
552}
553
e0c6027b 554// ----------------------------------------------------------------------------
be465555 555// keyboard handling
e0c6027b
VZ
556// ----------------------------------------------------------------------------
557
558void wxVListBox::OnKeyDown(wxKeyEvent& event)
559{
970b97a2
VZ
560 // flags for DoHandleItemClick()
561 int flags = ItemClick_Kbd;
562
999836aa 563 int current;
e0c6027b
VZ
564 switch ( event.GetKeyCode() )
565 {
566 case WXK_HOME:
be465555 567 current = 0;
e0c6027b
VZ
568 break;
569
570 case WXK_END:
e02c72fa 571 current = GetRowCount() - 1;
e0c6027b
VZ
572 break;
573
574 case WXK_DOWN:
e02c72fa 575 if ( m_current == (int)GetRowCount() - 1 )
e0c6027b
VZ
576 return;
577
be465555 578 current = m_current + 1;
e0c6027b
VZ
579 break;
580
581 case WXK_UP:
be465555 582 if ( m_current == wxNOT_FOUND )
e02c72fa 583 current = GetRowCount() - 1;
be465555
VZ
584 else if ( m_current != 0 )
585 current = m_current - 1;
586 else // m_current == 0
e0c6027b
VZ
587 return;
588 break;
589
590 case WXK_PAGEDOWN:
e0c6027b 591 PageDown();
e02c72fa 592 current = GetVisibleBegin();
e0c6027b
VZ
593 break;
594
595 case WXK_PAGEUP:
e02c72fa 596 if ( m_current == (int)GetVisibleBegin() )
e0c6027b
VZ
597 {
598 PageUp();
599 }
600
e02c72fa 601 current = GetVisibleBegin();
e0c6027b
VZ
602 break;
603
970b97a2
VZ
604 case WXK_SPACE:
605 // hack: pressing space should work like a mouse click rather than
606 // like a keyboard arrow press, so trick DoHandleItemClick() in
607 // thinking we were clicked
608 flags &= ~ItemClick_Kbd;
609 current = m_current;
610 break;
611
80c700cb
RD
612#ifdef __WXMSW__
613 case WXK_TAB:
614 // Since we are using wxWANTS_CHARS we need to send navigation
615 // events for the tabs on MSW
f029f1d1 616 HandleAsNavigationKey(event);
80c700cb
RD
617 // fall through to default
618#endif
e0c6027b
VZ
619 default:
620 event.Skip();
999836aa 621 current = 0; // just to silent the stupid compiler warnings
8703bc01 622 wxUnusedVar(current);
e0c6027b
VZ
623 return;
624 }
625
970b97a2
VZ
626 if ( event.ShiftDown() )
627 flags |= ItemClick_Shift;
628 if ( event.ControlDown() )
629 flags |= ItemClick_Ctrl;
630
631 DoHandleItemClick(current, flags);
e0c6027b
VZ
632}
633
634// ----------------------------------------------------------------------------
635// wxVListBox mouse handling
636// ----------------------------------------------------------------------------
637
638void wxVListBox::OnLeftDown(wxMouseEvent& event)
639{
c7778877 640 SetFocus();
4e115ed2 641
10368bff 642 int item = VirtualHitTest(event.GetPosition().y);
e0c6027b 643
6c9210a7
VZ
644 if ( item != wxNOT_FOUND )
645 {
970b97a2
VZ
646 int flags = 0;
647 if ( event.ShiftDown() )
648 flags |= ItemClick_Shift;
649
6c9210a7
VZ
650 // under Mac Apple-click is used in the same way as Ctrl-click
651 // elsewhere
be465555 652#ifdef __WXMAC__
970b97a2 653 if ( event.MetaDown() )
be465555 654#else
970b97a2 655 if ( event.ControlDown() )
be465555 656#endif
970b97a2
VZ
657 flags |= ItemClick_Ctrl;
658
659 DoHandleItemClick(item, flags);
6c9210a7 660 }
e0c6027b
VZ
661}
662
4e115ed2 663void wxVListBox::OnLeftDClick(wxMouseEvent& eventMouse)
e0c6027b 664{
10368bff 665 int item = VirtualHitTest(eventMouse.GetPosition().y);
be465555 666 if ( item != wxNOT_FOUND )
e0c6027b 667 {
e0c6027b 668
0975a8a0
JS
669 // if item double-clicked was not yet selected, then treat
670 // this event as a left-click instead
671 if ( item == m_current )
672 {
673 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, GetId());
674 event.SetEventObject(this);
675 event.SetInt(item);
676
677 (void)GetEventHandler()->ProcessEvent(event);
678 }
679 else
680 {
681 OnLeftDown(eventMouse);
682 }
e19ac18a 683
e0c6027b
VZ
684 }
685}
686
dc596072
RD
687
688// ----------------------------------------------------------------------------
689// use the same default attributes as wxListBox
690// ----------------------------------------------------------------------------
691
dc596072
RD
692//static
693wxVisualAttributes
694wxVListBox::GetClassDefaultAttributes(wxWindowVariant variant)
695{
696 return wxListBox::GetClassDefaultAttributes(variant);
697}
179e085f 698
8b939bc0 699#endif