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