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