warning fixes for BCC and OW (heavily modified patch 819146)
[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 if ( !wxVScrolledWindow::Create(parent, id, pos, size, style, name) )
72 return false;
73
74 if ( style & wxLB_MULTIPLE )
75 m_selStore = new wxSelectionStore;
76
77 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX));
78 m_colBgSel = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
79
80 return true;
81 }
82
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
99 // ----------------------------------------------------------------------------
100 // selection handling
101 // ----------------------------------------------------------------------------
102
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)
129 {
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 {
195 wxASSERT_MSG( current == wxNOT_FOUND ||
196 (current >= 0 && (size_t)current < GetItemCount()),
197 _T("wxVListBox::DoSetCurrent(): invalid item index") );
198
199 if ( current == m_current )
200 {
201 // nothing to do
202 return false;
203 }
204
205 if ( m_current != wxNOT_FOUND )
206 RefreshLine(m_current);
207
208 m_current = current;
209
210 if ( m_current != wxNOT_FOUND )
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
214 if ( !IsVisible(m_current) )
215 {
216 ScrollToLine(m_current);
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
222 if ( (size_t)m_current == GetLastVisibleLine() )
223 {
224 ScrollToLine(m_current);
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
229 RefreshLine(m_current);
230 }
231 }
232
233 return true;
234 }
235
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 {
250 wxCHECK_RET( selection == wxNOT_FOUND ||
251 (selection >= 0 && (size_t)selection < GetItemCount()),
252 _T("wxVListBox::SetSelection(): invalid item index") );
253
254 if ( HasMultipleSelection() )
255 {
256 Select(selection);
257 m_anchor = selection;
258 }
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;
285 }
286
287 return wxNOT_FOUND;
288 }
289
290 // ----------------------------------------------------------------------------
291 // wxVListBox appearance parameters
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
304 void wxVListBox::SetSelectionBackground(const wxColour& col)
305 {
306 m_colBgSel = col;
307 }
308
309 // ----------------------------------------------------------------------------
310 // wxVListBox painting
311 // ----------------------------------------------------------------------------
312
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
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))
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
372 wxRect rect = rectLine;
373 OnDrawBackground(dc, rect, line);
374
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
395 // ============================================================================
396 // wxVListBox keyboard/mouse handling
397 // ============================================================================
398
399 void wxVListBox::DoHandleItemClick(int item, int flags)
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
412 if ( flags & ItemClick_Shift )
413 {
414 if ( m_current != wxNOT_FOUND )
415 {
416 if ( m_anchor == wxNOT_FOUND )
417 m_anchor = m_current;
418
419 select = false;
420
421 // only the range from the selection anchor to new m_current
422 // must be selected
423 if ( DeselectAll() )
424 notify = true;
425
426 if ( SelectRange(m_anchor, item) )
427 notify = true;
428 }
429 //else: treat it as ordinary click/keypress
430 }
431 else // Shift not pressed
432 {
433 m_anchor = item;
434
435 if ( flags & ItemClick_Ctrl )
436 {
437 select = false;
438
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
449 }
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
480 // ----------------------------------------------------------------------------
481 // keyboard handling
482 // ----------------------------------------------------------------------------
483
484 void wxVListBox::OnKeyDown(wxKeyEvent& event)
485 {
486 // flags for DoHandleItemClick()
487 int flags = ItemClick_Kbd;
488
489 int current;
490 switch ( event.GetKeyCode() )
491 {
492 case WXK_HOME:
493 current = 0;
494 break;
495
496 case WXK_END:
497 current = GetLineCount() - 1;
498 break;
499
500 case WXK_DOWN:
501 if ( m_current == (int)GetLineCount() - 1 )
502 return;
503
504 current = m_current + 1;
505 break;
506
507 case WXK_UP:
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
513 return;
514 break;
515
516 case WXK_PAGEDOWN:
517 case WXK_NEXT:
518 PageDown();
519 current = GetFirstVisibleLine();
520 break;
521
522 case WXK_PAGEUP:
523 case WXK_PRIOR:
524 if ( m_current == (int)GetFirstVisibleLine() )
525 {
526 PageUp();
527 }
528
529 current = GetFirstVisibleLine();
530 break;
531
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
540 default:
541 event.Skip();
542 current = 0; // just to silent the stupid compiler warnings
543 return;
544 }
545
546 if ( event.ShiftDown() )
547 flags |= ItemClick_Shift;
548 if ( event.ControlDown() )
549 flags |= ItemClick_Ctrl;
550
551 DoHandleItemClick(current, flags);
552 }
553
554 // ----------------------------------------------------------------------------
555 // wxVListBox mouse handling
556 // ----------------------------------------------------------------------------
557
558 void wxVListBox::OnLeftDown(wxMouseEvent& event)
559 {
560 int item = HitTest(event.GetPosition());
561
562 if ( item != wxNOT_FOUND )
563 {
564 int flags = 0;
565 if ( event.ShiftDown() )
566 flags |= ItemClick_Shift;
567
568 // under Mac Apple-click is used in the same way as Ctrl-click
569 // elsewhere
570 #ifdef __WXMAC__
571 if ( event.MetaDown() )
572 #else
573 if ( event.ControlDown() )
574 #endif
575 flags |= ItemClick_Ctrl;
576
577 DoHandleItemClick(item, flags);
578 }
579 }
580
581 void wxVListBox::OnLeftDClick(wxMouseEvent& event)
582 {
583 int item = HitTest(event.GetPosition());
584 if ( item != wxNOT_FOUND )
585 {
586 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, GetId());
587 event.SetEventObject(this);
588 event.m_commandInt = item;
589
590 (void)GetEventHandler()->ProcessEvent(event);
591 }
592 }
593