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