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