The rounded corners look really dumb at this size.
[wxWidgets.git] / src / generic / listctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // TODO
11 //
12 // 1. we need to implement searching/sorting for virtual controls somehow
13 // 2. when changing selection the lines are refreshed twice
14
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #if wxUSE_LISTCTRL
24
25 #include "wx/listctrl.h"
26
27 #ifndef WX_PRECOMP
28 #include "wx/scrolwin.h"
29 #include "wx/timer.h"
30 #include "wx/settings.h"
31 #include "wx/dynarray.h"
32 #include "wx/dcclient.h"
33 #include "wx/dcscreen.h"
34 #include "wx/math.h"
35 #include "wx/settings.h"
36 #include "wx/sizer.h"
37 #endif
38
39 #include "wx/imaglist.h"
40 #include "wx/renderer.h"
41 #include "wx/generic/private/listctrl.h"
42
43 #ifdef __WXMAC__
44 #include "wx/osx/private.h"
45 #endif
46
47 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
48 #define "wx/msw/wrapwin.h"
49 #endif
50
51 // NOTE: If using the wxListBox visual attributes works everywhere then this can
52 // be removed, as well as the #else case below.
53 #define _USE_VISATTR 0
54
55
56 // ----------------------------------------------------------------------------
57 // constants
58 // ----------------------------------------------------------------------------
59
60 // // the height of the header window (FIXME: should depend on its font!)
61 // static const int HEADER_HEIGHT = 23;
62
63 static const int SCROLL_UNIT_X = 15;
64
65 // the spacing between the lines (in report mode)
66 static const int LINE_SPACING = 0;
67
68 // extra margins around the text label
69 #ifdef __WXGTK__
70 static const int EXTRA_WIDTH = 6;
71 #else
72 static const int EXTRA_WIDTH = 4;
73 #endif
74
75 #ifdef __WXGTK__
76 static const int EXTRA_HEIGHT = 6;
77 #else
78 static const int EXTRA_HEIGHT = 4;
79 #endif
80
81 // margin between the window and the items
82 static const int EXTRA_BORDER_X = 2;
83 static const int EXTRA_BORDER_Y = 2;
84
85 // offset for the header window
86 static const int HEADER_OFFSET_X = 0;
87 static const int HEADER_OFFSET_Y = 0;
88
89 // margin between rows of icons in [small] icon view
90 static const int MARGIN_BETWEEN_ROWS = 6;
91
92 // when autosizing the columns, add some slack
93 static const int AUTOSIZE_COL_MARGIN = 10;
94
95 // default width for the header columns
96 static const int WIDTH_COL_DEFAULT = 80;
97
98 // the space between the image and the text in the report mode
99 static const int IMAGE_MARGIN_IN_REPORT_MODE = 5;
100
101 // the space between the image and the text in the report mode in header
102 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE = 2;
103
104
105
106 // ----------------------------------------------------------------------------
107 // arrays/list implementations
108 // ----------------------------------------------------------------------------
109
110 #include "wx/listimpl.cpp"
111 WX_DEFINE_LIST(wxListItemDataList)
112
113 #include "wx/arrimpl.cpp"
114 WX_DEFINE_OBJARRAY(wxListLineDataArray)
115
116 #include "wx/listimpl.cpp"
117 WX_DEFINE_LIST(wxListHeaderDataList)
118
119
120 // ----------------------------------------------------------------------------
121 // wxListItemData
122 // ----------------------------------------------------------------------------
123
124 wxListItemData::~wxListItemData()
125 {
126 // in the virtual list control the attributes are managed by the main
127 // program, so don't delete them
128 if ( !m_owner->IsVirtual() )
129 delete m_attr;
130
131 delete m_rect;
132 }
133
134 void wxListItemData::Init()
135 {
136 m_image = -1;
137 m_data = 0;
138
139 m_attr = NULL;
140 }
141
142 wxListItemData::wxListItemData(wxListMainWindow *owner)
143 {
144 Init();
145
146 m_owner = owner;
147
148 if ( owner->InReportView() )
149 m_rect = NULL;
150 else
151 m_rect = new wxRect;
152 }
153
154 void wxListItemData::SetItem( const wxListItem &info )
155 {
156 if ( info.m_mask & wxLIST_MASK_TEXT )
157 SetText(info.m_text);
158 if ( info.m_mask & wxLIST_MASK_IMAGE )
159 m_image = info.m_image;
160 if ( info.m_mask & wxLIST_MASK_DATA )
161 m_data = info.m_data;
162
163 if ( info.HasAttributes() )
164 {
165 if ( m_attr )
166 m_attr->AssignFrom(*info.GetAttributes());
167 else
168 m_attr = new wxListItemAttr(*info.GetAttributes());
169 }
170
171 if ( m_rect )
172 {
173 m_rect->x =
174 m_rect->y =
175 m_rect->height = 0;
176 m_rect->width = info.m_width;
177 }
178 }
179
180 void wxListItemData::SetPosition( int x, int y )
181 {
182 wxCHECK_RET( m_rect, wxT("unexpected SetPosition() call") );
183
184 m_rect->x = x;
185 m_rect->y = y;
186 }
187
188 void wxListItemData::SetSize( int width, int height )
189 {
190 wxCHECK_RET( m_rect, wxT("unexpected SetSize() call") );
191
192 if ( width != -1 )
193 m_rect->width = width;
194 if ( height != -1 )
195 m_rect->height = height;
196 }
197
198 bool wxListItemData::IsHit( int x, int y ) const
199 {
200 wxCHECK_MSG( m_rect, false, wxT("can't be called in this mode") );
201
202 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x, y);
203 }
204
205 int wxListItemData::GetX() const
206 {
207 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
208
209 return m_rect->x;
210 }
211
212 int wxListItemData::GetY() const
213 {
214 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
215
216 return m_rect->y;
217 }
218
219 int wxListItemData::GetWidth() const
220 {
221 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
222
223 return m_rect->width;
224 }
225
226 int wxListItemData::GetHeight() const
227 {
228 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
229
230 return m_rect->height;
231 }
232
233 void wxListItemData::GetItem( wxListItem &info ) const
234 {
235 long mask = info.m_mask;
236 if ( !mask )
237 // by default, get everything for backwards compatibility
238 mask = -1;
239
240 if ( mask & wxLIST_MASK_TEXT )
241 info.m_text = m_text;
242 if ( mask & wxLIST_MASK_IMAGE )
243 info.m_image = m_image;
244 if ( mask & wxLIST_MASK_DATA )
245 info.m_data = m_data;
246
247 if ( m_attr )
248 {
249 if ( m_attr->HasTextColour() )
250 info.SetTextColour(m_attr->GetTextColour());
251 if ( m_attr->HasBackgroundColour() )
252 info.SetBackgroundColour(m_attr->GetBackgroundColour());
253 if ( m_attr->HasFont() )
254 info.SetFont(m_attr->GetFont());
255 }
256 }
257
258 //-----------------------------------------------------------------------------
259 // wxListHeaderData
260 //-----------------------------------------------------------------------------
261
262 void wxListHeaderData::Init()
263 {
264 m_mask = 0;
265 m_image = -1;
266 m_format = 0;
267 m_width = 0;
268 m_xpos = 0;
269 m_ypos = 0;
270 m_height = 0;
271 m_state = 0;
272 }
273
274 wxListHeaderData::wxListHeaderData()
275 {
276 Init();
277 }
278
279 wxListHeaderData::wxListHeaderData( const wxListItem &item )
280 {
281 Init();
282
283 SetItem( item );
284 }
285
286 void wxListHeaderData::SetItem( const wxListItem &item )
287 {
288 m_mask = item.m_mask;
289
290 if ( m_mask & wxLIST_MASK_TEXT )
291 m_text = item.m_text;
292
293 if ( m_mask & wxLIST_MASK_IMAGE )
294 m_image = item.m_image;
295
296 if ( m_mask & wxLIST_MASK_FORMAT )
297 m_format = item.m_format;
298
299 if ( m_mask & wxLIST_MASK_WIDTH )
300 SetWidth(item.m_width);
301
302 if ( m_mask & wxLIST_MASK_STATE )
303 SetState(item.m_state);
304 }
305
306 void wxListHeaderData::SetPosition( int x, int y )
307 {
308 m_xpos = x;
309 m_ypos = y;
310 }
311
312 void wxListHeaderData::SetHeight( int h )
313 {
314 m_height = h;
315 }
316
317 void wxListHeaderData::SetWidth( int w )
318 {
319 m_width = w < 0 ? WIDTH_COL_DEFAULT : w;
320 }
321
322 void wxListHeaderData::SetState( int flag )
323 {
324 m_state = flag;
325 }
326
327 void wxListHeaderData::SetFormat( int format )
328 {
329 m_format = format;
330 }
331
332 bool wxListHeaderData::HasImage() const
333 {
334 return m_image != -1;
335 }
336
337 bool wxListHeaderData::IsHit( int x, int y ) const
338 {
339 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
340 }
341
342 void wxListHeaderData::GetItem( wxListItem& item )
343 {
344 long mask = item.m_mask;
345 if ( !mask )
346 {
347 // by default, get everything for backwards compatibility
348 mask = -1;
349 }
350
351 if ( mask & wxLIST_MASK_STATE )
352 item.m_state = m_state;
353 if ( mask & wxLIST_MASK_TEXT )
354 item.m_text = m_text;
355 if ( mask & wxLIST_MASK_IMAGE )
356 item.m_image = m_image;
357 if ( mask & wxLIST_MASK_WIDTH )
358 item.m_width = m_width;
359 if ( mask & wxLIST_MASK_FORMAT )
360 item.m_format = m_format;
361 }
362
363 int wxListHeaderData::GetImage() const
364 {
365 return m_image;
366 }
367
368 int wxListHeaderData::GetWidth() const
369 {
370 return m_width;
371 }
372
373 int wxListHeaderData::GetFormat() const
374 {
375 return m_format;
376 }
377
378 int wxListHeaderData::GetState() const
379 {
380 return m_state;
381 }
382
383 //-----------------------------------------------------------------------------
384 // wxListLineData
385 //-----------------------------------------------------------------------------
386
387 inline int wxListLineData::GetMode() const
388 {
389 return m_owner->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE;
390 }
391
392 inline bool wxListLineData::InReportView() const
393 {
394 return m_owner->HasFlag(wxLC_REPORT);
395 }
396
397 inline bool wxListLineData::IsVirtual() const
398 {
399 return m_owner->IsVirtual();
400 }
401
402 wxListLineData::wxListLineData( wxListMainWindow *owner )
403 {
404 m_owner = owner;
405
406 if ( InReportView() )
407 m_gi = NULL;
408 else // !report
409 m_gi = new GeometryInfo;
410
411 m_highlighted = false;
412
413 InitItems( GetMode() == wxLC_REPORT ? m_owner->GetColumnCount() : 1 );
414 }
415
416 void wxListLineData::CalculateSize( wxDC *dc, int spacing )
417 {
418 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
419 wxCHECK_RET( node, wxT("no subitems at all??") );
420
421 wxListItemData *item = node->GetData();
422
423 wxString s;
424 wxCoord lw, lh;
425
426 switch ( GetMode() )
427 {
428 case wxLC_ICON:
429 case wxLC_SMALL_ICON:
430 m_gi->m_rectAll.width = spacing;
431
432 s = item->GetText();
433
434 if ( s.empty() )
435 {
436 lh =
437 m_gi->m_rectLabel.width =
438 m_gi->m_rectLabel.height = 0;
439 }
440 else // has label
441 {
442 dc->GetTextExtent( s, &lw, &lh );
443 lw += EXTRA_WIDTH;
444 lh += EXTRA_HEIGHT;
445
446 m_gi->m_rectAll.height = spacing + lh;
447 if (lw > spacing)
448 m_gi->m_rectAll.width = lw;
449
450 m_gi->m_rectLabel.width = lw;
451 m_gi->m_rectLabel.height = lh;
452 }
453
454 if (item->HasImage())
455 {
456 int w, h;
457 m_owner->GetImageSize( item->GetImage(), w, h );
458 m_gi->m_rectIcon.width = w + 8;
459 m_gi->m_rectIcon.height = h + 8;
460
461 if ( m_gi->m_rectIcon.width > m_gi->m_rectAll.width )
462 m_gi->m_rectAll.width = m_gi->m_rectIcon.width;
463 if ( m_gi->m_rectIcon.height + lh > m_gi->m_rectAll.height - 4 )
464 m_gi->m_rectAll.height = m_gi->m_rectIcon.height + lh + 4;
465 }
466
467 if ( item->HasText() )
468 {
469 m_gi->m_rectHighlight.width = m_gi->m_rectLabel.width;
470 m_gi->m_rectHighlight.height = m_gi->m_rectLabel.height;
471 }
472 else // no text, highlight the icon
473 {
474 m_gi->m_rectHighlight.width = m_gi->m_rectIcon.width;
475 m_gi->m_rectHighlight.height = m_gi->m_rectIcon.height;
476 }
477 break;
478
479 case wxLC_LIST:
480 s = item->GetTextForMeasuring();
481
482 dc->GetTextExtent( s, &lw, &lh );
483 lw += EXTRA_WIDTH;
484 lh += EXTRA_HEIGHT;
485
486 m_gi->m_rectLabel.width = lw;
487 m_gi->m_rectLabel.height = lh;
488
489 m_gi->m_rectAll.width = lw;
490 m_gi->m_rectAll.height = lh;
491
492 if (item->HasImage())
493 {
494 int w, h;
495 m_owner->GetImageSize( item->GetImage(), w, h );
496 m_gi->m_rectIcon.width = w;
497 m_gi->m_rectIcon.height = h;
498
499 m_gi->m_rectAll.width += 4 + w;
500 if (h > m_gi->m_rectAll.height)
501 m_gi->m_rectAll.height = h;
502 }
503
504 m_gi->m_rectHighlight.width = m_gi->m_rectAll.width;
505 m_gi->m_rectHighlight.height = m_gi->m_rectAll.height;
506 break;
507
508 case wxLC_REPORT:
509 wxFAIL_MSG( wxT("unexpected call to SetSize") );
510 break;
511
512 default:
513 wxFAIL_MSG( wxT("unknown mode") );
514 break;
515 }
516 }
517
518 void wxListLineData::SetPosition( int x, int y, int spacing )
519 {
520 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
521 wxCHECK_RET( node, wxT("no subitems at all??") );
522
523 wxListItemData *item = node->GetData();
524
525 switch ( GetMode() )
526 {
527 case wxLC_ICON:
528 case wxLC_SMALL_ICON:
529 m_gi->m_rectAll.x = x;
530 m_gi->m_rectAll.y = y;
531
532 if ( item->HasImage() )
533 {
534 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 4 +
535 (m_gi->m_rectAll.width - m_gi->m_rectIcon.width) / 2;
536 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 4;
537 }
538
539 if ( item->HasText() )
540 {
541 if (m_gi->m_rectAll.width > spacing)
542 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
543 else
544 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2) + (spacing / 2) - (m_gi->m_rectLabel.width / 2);
545 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + m_gi->m_rectAll.height + 2 - m_gi->m_rectLabel.height;
546 m_gi->m_rectHighlight.x = m_gi->m_rectLabel.x - 2;
547 m_gi->m_rectHighlight.y = m_gi->m_rectLabel.y - 2;
548 }
549 else // no text, highlight the icon
550 {
551 m_gi->m_rectHighlight.x = m_gi->m_rectIcon.x - 4;
552 m_gi->m_rectHighlight.y = m_gi->m_rectIcon.y - 4;
553 }
554 break;
555
556 case wxLC_LIST:
557 m_gi->m_rectAll.x = x;
558 m_gi->m_rectAll.y = y;
559
560 m_gi->m_rectHighlight.x = m_gi->m_rectAll.x;
561 m_gi->m_rectHighlight.y = m_gi->m_rectAll.y;
562 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + 2;
563
564 if (item->HasImage())
565 {
566 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 2;
567 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 2;
568 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 4 + (EXTRA_WIDTH/2) + m_gi->m_rectIcon.width;
569 }
570 else
571 {
572 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
573 }
574 break;
575
576 case wxLC_REPORT:
577 wxFAIL_MSG( wxT("unexpected call to SetPosition") );
578 break;
579
580 default:
581 wxFAIL_MSG( wxT("unknown mode") );
582 break;
583 }
584 }
585
586 void wxListLineData::InitItems( int num )
587 {
588 for (int i = 0; i < num; i++)
589 m_items.Append( new wxListItemData(m_owner) );
590 }
591
592 void wxListLineData::SetItem( int index, const wxListItem &info )
593 {
594 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
595 wxCHECK_RET( node, wxT("invalid column index in SetItem") );
596
597 wxListItemData *item = node->GetData();
598 item->SetItem( info );
599 }
600
601 void wxListLineData::GetItem( int index, wxListItem &info )
602 {
603 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
604 if (node)
605 {
606 wxListItemData *item = node->GetData();
607 item->GetItem( info );
608 }
609 }
610
611 wxString wxListLineData::GetText(int index) const
612 {
613 wxString s;
614
615 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
616 if (node)
617 {
618 wxListItemData *item = node->GetData();
619 s = item->GetText();
620 }
621
622 return s;
623 }
624
625 void wxListLineData::SetText( int index, const wxString& s )
626 {
627 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
628 if (node)
629 {
630 wxListItemData *item = node->GetData();
631 item->SetText( s );
632 }
633 }
634
635 void wxListLineData::SetImage( int index, int image )
636 {
637 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
638 wxCHECK_RET( node, wxT("invalid column index in SetImage()") );
639
640 wxListItemData *item = node->GetData();
641 item->SetImage(image);
642 }
643
644 int wxListLineData::GetImage( int index ) const
645 {
646 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
647 wxCHECK_MSG( node, -1, wxT("invalid column index in GetImage()") );
648
649 wxListItemData *item = node->GetData();
650 return item->GetImage();
651 }
652
653 wxListItemAttr *wxListLineData::GetAttr() const
654 {
655 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
656 wxCHECK_MSG( node, NULL, wxT("invalid column index in GetAttr()") );
657
658 wxListItemData *item = node->GetData();
659 return item->GetAttr();
660 }
661
662 void wxListLineData::SetAttr(wxListItemAttr *attr)
663 {
664 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
665 wxCHECK_RET( node, wxT("invalid column index in SetAttr()") );
666
667 wxListItemData *item = node->GetData();
668 item->SetAttr(attr);
669 }
670
671 void wxListLineData::ApplyAttributes(wxDC *dc,
672 const wxRect& rectHL,
673 bool highlighted,
674 bool current)
675 {
676 const wxListItemAttr * const attr = GetAttr();
677
678 wxWindow * const listctrl = m_owner->GetParent();
679
680 const bool hasFocus = listctrl->HasFocus()
681 #if defined(__WXMAC__) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
682 && IsControlActive( (ControlRef)listctrl->GetHandle() )
683 #endif
684 ;
685
686 // fg colour
687
688 // don't use foreground colour for drawing highlighted items - this might
689 // make them completely invisible (and there is no way to do bit
690 // arithmetics on wxColour, unfortunately)
691 wxColour colText;
692 if ( highlighted )
693 {
694 #ifdef __WXMAC__
695 if ( hasFocus )
696 colText = *wxWHITE;
697 else
698 colText = *wxBLACK;
699 #else
700 if ( hasFocus )
701 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
702 else
703 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT);
704 #endif
705 }
706 else if ( attr && attr->HasTextColour() )
707 colText = attr->GetTextColour();
708 else
709 colText = listctrl->GetForegroundColour();
710
711 dc->SetTextForeground(colText);
712
713 // font
714 wxFont font;
715 if ( attr && attr->HasFont() )
716 font = attr->GetFont();
717 else
718 font = listctrl->GetFont();
719
720 dc->SetFont(font);
721
722 // background
723 if ( highlighted )
724 {
725 // Use the renderer method to ensure that the selected items use the
726 // native look.
727 int flags = wxCONTROL_SELECTED;
728 if ( hasFocus )
729 flags |= wxCONTROL_FOCUSED;
730 if (current)
731 flags |= wxCONTROL_CURRENT;
732 wxRendererNative::Get().
733 DrawItemSelectionRect( m_owner, *dc, rectHL, flags );
734 }
735 else if ( attr && attr->HasBackgroundColour() )
736 {
737 // Draw the background using the items custom background colour.
738 dc->SetBrush(attr->GetBackgroundColour());
739 dc->SetPen(*wxTRANSPARENT_PEN);
740 dc->DrawRectangle(rectHL);
741 }
742
743 // just for debugging to better see where the items are
744 #if 0
745 dc->SetPen(*wxRED_PEN);
746 dc->SetBrush(*wxTRANSPARENT_BRUSH);
747 dc->DrawRectangle( m_gi->m_rectAll );
748 dc->SetPen(*wxGREEN_PEN);
749 dc->DrawRectangle( m_gi->m_rectIcon );
750 #endif
751 }
752
753 void wxListLineData::Draw(wxDC *dc, bool current)
754 {
755 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
756 wxCHECK_RET( node, wxT("no subitems at all??") );
757
758 ApplyAttributes(dc, m_gi->m_rectHighlight, IsHighlighted(), current);
759
760 wxListItemData *item = node->GetData();
761 if (item->HasImage())
762 {
763 // centre the image inside our rectangle, this looks nicer when items
764 // ae aligned in a row
765 const wxRect& rectIcon = m_gi->m_rectIcon;
766
767 m_owner->DrawImage(item->GetImage(), dc, rectIcon.x, rectIcon.y);
768 }
769
770 if (item->HasText())
771 {
772 const wxRect& rectLabel = m_gi->m_rectLabel;
773
774 wxDCClipper clipper(*dc, rectLabel);
775 dc->DrawText(item->GetText(), rectLabel.x, rectLabel.y);
776 }
777 }
778
779 void wxListLineData::DrawInReportMode( wxDC *dc,
780 const wxRect& rect,
781 const wxRect& rectHL,
782 bool highlighted,
783 bool current )
784 {
785 // TODO: later we should support setting different attributes for
786 // different columns - to do it, just add "col" argument to
787 // GetAttr() and move these lines into the loop below
788
789 ApplyAttributes(dc, rectHL, highlighted, current);
790
791 wxCoord x = rect.x + HEADER_OFFSET_X,
792 yMid = rect.y + rect.height/2;
793 #ifdef __WXGTK__
794 // This probably needs to be done
795 // on all platforms as the icons
796 // otherwise nearly touch the border
797 x += 2;
798 #endif
799
800 size_t col = 0;
801 for ( wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
802 node;
803 node = node->GetNext(), col++ )
804 {
805 wxListItemData *item = node->GetData();
806
807 int width = m_owner->GetColumnWidth(col);
808 int xOld = x;
809 x += width;
810
811 width -= 8;
812 const int wText = width;
813 wxDCClipper clipper(*dc, xOld, rect.y, wText, rect.height);
814
815 if ( item->HasImage() )
816 {
817 int ix, iy;
818 m_owner->GetImageSize( item->GetImage(), ix, iy );
819 m_owner->DrawImage( item->GetImage(), dc, xOld, yMid - iy/2 );
820
821 ix += IMAGE_MARGIN_IN_REPORT_MODE;
822
823 xOld += ix;
824 width -= ix;
825 }
826
827 if ( item->HasText() )
828 DrawTextFormatted(dc, item->GetText(), col, xOld, yMid, width);
829 }
830 }
831
832 void wxListLineData::DrawTextFormatted(wxDC *dc,
833 const wxString& textOrig,
834 int col,
835 int x,
836 int yMid,
837 int width)
838 {
839 // we don't support displaying multiple lines currently (and neither does
840 // wxMSW FWIW) so just merge all the lines
841 wxString text(textOrig);
842 text.Replace(wxT("\n"), wxT(" "));
843
844 wxCoord w, h;
845 dc->GetTextExtent(text, &w, &h);
846
847 const wxCoord y = yMid - (h + 1)/2;
848
849 wxDCClipper clipper(*dc, x, y, width, h);
850
851 // determine if the string can fit inside the current width
852 if (w <= width)
853 {
854 // it can, draw it using the items alignment
855 wxListItem item;
856 m_owner->GetColumn(col, item);
857 switch ( item.GetAlign() )
858 {
859 case wxLIST_FORMAT_LEFT:
860 // nothing to do
861 break;
862
863 case wxLIST_FORMAT_RIGHT:
864 x += width - w;
865 break;
866
867 case wxLIST_FORMAT_CENTER:
868 x += (width - w) / 2;
869 break;
870
871 default:
872 wxFAIL_MSG( wxT("unknown list item format") );
873 break;
874 }
875
876 dc->DrawText(text, x, y);
877 }
878 else // otherwise, truncate and add an ellipsis if possible
879 {
880 // determine the base width
881 wxString ellipsis(wxT("..."));
882 wxCoord base_w;
883 dc->GetTextExtent(ellipsis, &base_w, &h);
884
885 // continue until we have enough space or only one character left
886 wxCoord w_c, h_c;
887 size_t len = text.length();
888 wxString drawntext = text.Left(len);
889 while (len > 1)
890 {
891 dc->GetTextExtent(drawntext.Last(), &w_c, &h_c);
892 drawntext.RemoveLast();
893 len--;
894 w -= w_c;
895 if (w + base_w <= width)
896 break;
897 }
898
899 // if still not enough space, remove ellipsis characters
900 while (ellipsis.length() > 0 && w + base_w > width)
901 {
902 ellipsis = ellipsis.Left(ellipsis.length() - 1);
903 dc->GetTextExtent(ellipsis, &base_w, &h);
904 }
905
906 // now draw the text
907 dc->DrawText(drawntext, x, y);
908 dc->DrawText(ellipsis, x + w, y);
909 }
910 }
911
912 bool wxListLineData::Highlight( bool on )
913 {
914 wxCHECK_MSG( !IsVirtual(), false, wxT("unexpected call to Highlight") );
915
916 if ( on == m_highlighted )
917 return false;
918
919 m_highlighted = on;
920
921 return true;
922 }
923
924 void wxListLineData::ReverseHighlight( void )
925 {
926 Highlight(!IsHighlighted());
927 }
928
929 //-----------------------------------------------------------------------------
930 // wxListHeaderWindow
931 //-----------------------------------------------------------------------------
932
933 BEGIN_EVENT_TABLE(wxListHeaderWindow,wxWindow)
934 EVT_PAINT (wxListHeaderWindow::OnPaint)
935 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse)
936 END_EVENT_TABLE()
937
938 void wxListHeaderWindow::Init()
939 {
940 m_currentCursor = NULL;
941 m_isDragging = false;
942 m_dirty = false;
943 m_sendSetColumnWidth = false;
944 }
945
946 wxListHeaderWindow::wxListHeaderWindow()
947 {
948 Init();
949
950 m_owner = NULL;
951 m_resizeCursor = NULL;
952 }
953
954 bool wxListHeaderWindow::Create( wxWindow *win,
955 wxWindowID id,
956 wxListMainWindow *owner,
957 const wxPoint& pos,
958 const wxSize& size,
959 long style,
960 const wxString &name )
961 {
962 if ( !wxWindow::Create(win, id, pos, size, style, name) )
963 return false;
964
965 Init();
966
967 m_owner = owner;
968 m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
969
970 #if _USE_VISATTR
971 wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes();
972 SetOwnForegroundColour( attr.colFg );
973 SetOwnBackgroundColour( attr.colBg );
974 if (!m_hasFont)
975 SetOwnFont( attr.font );
976 #else
977 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
978 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
979 if (!m_hasFont)
980 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT ));
981 #endif
982
983 return true;
984 }
985
986 wxListHeaderWindow::~wxListHeaderWindow()
987 {
988 delete m_resizeCursor;
989 }
990
991 #ifdef __WXUNIVERSAL__
992 #include "wx/univ/renderer.h"
993 #include "wx/univ/theme.h"
994 #endif
995
996 // shift the DC origin to match the position of the main window horz
997 // scrollbar: this allows us to always use logical coords
998 void wxListHeaderWindow::AdjustDC(wxDC& dc)
999 {
1000 wxGenericListCtrl *parent = m_owner->GetListCtrl();
1001
1002 int xpix;
1003 parent->GetScrollPixelsPerUnit( &xpix, NULL );
1004
1005 int view_start;
1006 parent->GetViewStart( &view_start, NULL );
1007
1008
1009 int org_x = 0;
1010 int org_y = 0;
1011 dc.GetDeviceOrigin( &org_x, &org_y );
1012
1013 // account for the horz scrollbar offset
1014 #ifdef __WXGTK__
1015 if (GetLayoutDirection() == wxLayout_RightToLeft)
1016 {
1017 // Maybe we just have to check for m_signX
1018 // in the DC, but I leave the #ifdef __WXGTK__
1019 // for now
1020 dc.SetDeviceOrigin( org_x + (view_start * xpix), org_y );
1021 }
1022 else
1023 #endif
1024 dc.SetDeviceOrigin( org_x - (view_start * xpix), org_y );
1025 }
1026
1027 void wxListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1028 {
1029 wxGenericListCtrl *parent = m_owner->GetListCtrl();
1030
1031 wxPaintDC dc( this );
1032
1033 AdjustDC( dc );
1034
1035 dc.SetFont( GetFont() );
1036
1037 // width and height of the entire header window
1038 int w, h;
1039 GetClientSize( &w, &h );
1040 parent->CalcUnscrolledPosition(w, 0, &w, NULL);
1041
1042 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
1043 dc.SetTextForeground(GetForegroundColour());
1044
1045 int x = HEADER_OFFSET_X;
1046 int numColumns = m_owner->GetColumnCount();
1047 wxListItem item;
1048 for ( int i = 0; i < numColumns && x < w; i++ )
1049 {
1050 m_owner->GetColumn( i, item );
1051 int wCol = item.m_width;
1052
1053 int cw = wCol;
1054 int ch = h;
1055
1056 int flags = 0;
1057 if (!m_parent->IsEnabled())
1058 flags |= wxCONTROL_DISABLED;
1059
1060 // NB: The code below is not really Mac-specific, but since we are close
1061 // to 2.8 release and I don't have time to test on other platforms, I
1062 // defined this only for wxMac. If this behaviour is desired on
1063 // other platforms, please go ahead and revise or remove the #ifdef.
1064 #ifdef __WXMAC__
1065 if ( !m_owner->IsVirtual() && (item.m_mask & wxLIST_MASK_STATE) &&
1066 (item.m_state & wxLIST_STATE_SELECTED) )
1067 flags |= wxCONTROL_SELECTED;
1068 #endif
1069
1070 if (i == 0)
1071 flags |= wxCONTROL_SPECIAL; // mark as first column
1072
1073 wxRendererNative::Get().DrawHeaderButton
1074 (
1075 this,
1076 dc,
1077 wxRect(x, HEADER_OFFSET_Y, cw, ch),
1078 flags
1079 );
1080
1081 // see if we have enough space for the column label
1082
1083 // for this we need the width of the text
1084 wxCoord wLabel;
1085 wxCoord hLabel;
1086 dc.GetTextExtent(item.GetText(), &wLabel, &hLabel);
1087 wLabel += 2 * EXTRA_WIDTH;
1088
1089 // and the width of the icon, if any
1090 int ix = 0, iy = 0; // init them just to suppress the compiler warnings
1091 const int image = item.m_image;
1092 wxImageList *imageList;
1093 if ( image != -1 )
1094 {
1095 imageList = m_owner->GetSmallImageList();
1096 if ( imageList )
1097 {
1098 imageList->GetSize(image, ix, iy);
1099 wLabel += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
1100 }
1101 }
1102 else
1103 {
1104 imageList = NULL;
1105 }
1106
1107 // ignore alignment if there is not enough space anyhow
1108 int xAligned;
1109 switch ( wLabel < cw ? item.GetAlign() : wxLIST_FORMAT_LEFT )
1110 {
1111 default:
1112 wxFAIL_MSG( wxT("unknown list item format") );
1113 // fall through
1114
1115 case wxLIST_FORMAT_LEFT:
1116 xAligned = x;
1117 break;
1118
1119 case wxLIST_FORMAT_RIGHT:
1120 xAligned = x + cw - wLabel;
1121 break;
1122
1123 case wxLIST_FORMAT_CENTER:
1124 xAligned = x + (cw - wLabel) / 2;
1125 break;
1126 }
1127
1128 // draw the text and image clipping them so that they
1129 // don't overwrite the column boundary
1130 wxDCClipper clipper(dc, x, HEADER_OFFSET_Y, cw, h);
1131
1132 // if we have an image, draw it on the right of the label
1133 if ( imageList )
1134 {
1135 imageList->Draw
1136 (
1137 image,
1138 dc,
1139 xAligned + wLabel - ix - HEADER_IMAGE_MARGIN_IN_REPORT_MODE,
1140 HEADER_OFFSET_Y + (h - iy)/2,
1141 wxIMAGELIST_DRAW_TRANSPARENT
1142 );
1143 }
1144
1145 dc.DrawText( item.GetText(),
1146 xAligned + EXTRA_WIDTH, (h - hLabel) / 2 );
1147
1148 x += wCol;
1149 }
1150
1151 // Fill in what's missing to the right of the columns, otherwise we will
1152 // leave an unpainted area when columns are removed (and it looks better)
1153 if ( x < w )
1154 {
1155 wxRendererNative::Get().DrawHeaderButton
1156 (
1157 this,
1158 dc,
1159 wxRect(x, HEADER_OFFSET_Y, w - x, h),
1160 wxCONTROL_DIRTY // mark as last column
1161 );
1162 }
1163 }
1164
1165 void wxListHeaderWindow::OnInternalIdle()
1166 {
1167 wxWindow::OnInternalIdle();
1168
1169 if (m_sendSetColumnWidth)
1170 {
1171 m_owner->SetColumnWidth( m_colToSend, m_widthToSend );
1172 m_sendSetColumnWidth = false;
1173 }
1174 }
1175
1176 void wxListHeaderWindow::DrawCurrent()
1177 {
1178 #if 1
1179 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1180 m_sendSetColumnWidth = true;
1181 m_colToSend = m_column;
1182 m_widthToSend = m_currentX - m_minX;
1183 #else
1184 int x1 = m_currentX;
1185 int y1 = 0;
1186 m_owner->ClientToScreen( &x1, &y1 );
1187
1188 int x2 = m_currentX;
1189 int y2 = 0;
1190 m_owner->GetClientSize( NULL, &y2 );
1191 m_owner->ClientToScreen( &x2, &y2 );
1192
1193 wxScreenDC dc;
1194 dc.SetLogicalFunction( wxINVERT );
1195 dc.SetPen( wxPen(*wxBLACK, 2) );
1196 dc.SetBrush( *wxTRANSPARENT_BRUSH );
1197
1198 AdjustDC(dc);
1199
1200 dc.DrawLine( x1, y1, x2, y2 );
1201
1202 dc.SetLogicalFunction( wxCOPY );
1203
1204 dc.SetPen( wxNullPen );
1205 dc.SetBrush( wxNullBrush );
1206 #endif
1207 }
1208
1209 void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
1210 {
1211 wxGenericListCtrl *parent = m_owner->GetListCtrl();
1212
1213 // we want to work with logical coords
1214 int x;
1215 parent->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
1216 int y = event.GetY();
1217
1218 if (m_isDragging)
1219 {
1220 SendListEvent(wxEVT_LIST_COL_DRAGGING, event.GetPosition());
1221
1222 // we don't draw the line beyond our window, but we allow dragging it
1223 // there
1224 int w = 0;
1225 GetClientSize( &w, NULL );
1226 parent->CalcUnscrolledPosition(w, 0, &w, NULL);
1227 w -= 6;
1228
1229 // erase the line if it was drawn
1230 if ( m_currentX < w )
1231 DrawCurrent();
1232
1233 if (event.ButtonUp())
1234 {
1235 ReleaseMouse();
1236 m_isDragging = false;
1237 m_dirty = true;
1238 m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1239 SendListEvent(wxEVT_LIST_COL_END_DRAG, event.GetPosition());
1240 }
1241 else
1242 {
1243 if (x > m_minX + 7)
1244 m_currentX = x;
1245 else
1246 m_currentX = m_minX + 7;
1247
1248 // draw in the new location
1249 if ( m_currentX < w )
1250 DrawCurrent();
1251 }
1252 }
1253 else // not dragging
1254 {
1255 m_minX = 0;
1256 bool hit_border = false;
1257
1258 // end of the current column
1259 int xpos = 0;
1260
1261 // find the column where this event occurred
1262 int col,
1263 countCol = m_owner->GetColumnCount();
1264 for (col = 0; col < countCol; col++)
1265 {
1266 xpos += m_owner->GetColumnWidth( col );
1267 m_column = col;
1268
1269 if ( (abs(x-xpos) < 3) && (y < 22) )
1270 {
1271 // near the column border
1272 hit_border = true;
1273 break;
1274 }
1275
1276 if ( x < xpos )
1277 {
1278 // inside the column
1279 break;
1280 }
1281
1282 m_minX = xpos;
1283 }
1284
1285 if ( col == countCol )
1286 m_column = -1;
1287
1288 if (event.LeftDown() || event.RightUp())
1289 {
1290 if (hit_border && event.LeftDown())
1291 {
1292 if ( SendListEvent(wxEVT_LIST_COL_BEGIN_DRAG,
1293 event.GetPosition()) )
1294 {
1295 m_isDragging = true;
1296 m_currentX = x;
1297 CaptureMouse();
1298 DrawCurrent();
1299 }
1300 //else: column resizing was vetoed by the user code
1301 }
1302 else // click on a column
1303 {
1304 // record the selected state of the columns
1305 if (event.LeftDown())
1306 {
1307 for (int i=0; i < m_owner->GetColumnCount(); i++)
1308 {
1309 wxListItem colItem;
1310 m_owner->GetColumn(i, colItem);
1311 long state = colItem.GetState();
1312 if (i == m_column)
1313 colItem.SetState(state | wxLIST_STATE_SELECTED);
1314 else
1315 colItem.SetState(state & ~wxLIST_STATE_SELECTED);
1316 m_owner->SetColumn(i, colItem);
1317 }
1318 }
1319
1320 SendListEvent( event.LeftDown()
1321 ? wxEVT_LIST_COL_CLICK
1322 : wxEVT_LIST_COL_RIGHT_CLICK,
1323 event.GetPosition());
1324 }
1325 }
1326 else if (event.Moving())
1327 {
1328 bool setCursor;
1329 if (hit_border)
1330 {
1331 setCursor = m_currentCursor == wxSTANDARD_CURSOR;
1332 m_currentCursor = m_resizeCursor;
1333 }
1334 else
1335 {
1336 setCursor = m_currentCursor != wxSTANDARD_CURSOR;
1337 m_currentCursor = wxSTANDARD_CURSOR;
1338 }
1339
1340 if ( setCursor )
1341 SetCursor(*m_currentCursor);
1342 }
1343 }
1344 }
1345
1346 bool wxListHeaderWindow::SendListEvent(wxEventType type, const wxPoint& pos)
1347 {
1348 wxWindow *parent = GetParent();
1349 wxListEvent le( type, parent->GetId() );
1350 le.SetEventObject( parent );
1351 le.m_pointDrag = pos;
1352
1353 // the position should be relative to the parent window, not
1354 // this one for compatibility with MSW and common sense: the
1355 // user code doesn't know anything at all about this header
1356 // window, so why should it get positions relative to it?
1357 le.m_pointDrag.y -= GetSize().y;
1358
1359 le.m_col = m_column;
1360 return !parent->GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
1361 }
1362
1363 //-----------------------------------------------------------------------------
1364 // wxListRenameTimer (internal)
1365 //-----------------------------------------------------------------------------
1366
1367 wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
1368 {
1369 m_owner = owner;
1370 }
1371
1372 void wxListRenameTimer::Notify()
1373 {
1374 m_owner->OnRenameTimer();
1375 }
1376
1377 //-----------------------------------------------------------------------------
1378 // wxListFindTimer (internal)
1379 //-----------------------------------------------------------------------------
1380
1381 void wxListFindTimer::Notify()
1382 {
1383 m_owner->OnFindTimer();
1384 }
1385
1386 //-----------------------------------------------------------------------------
1387 // wxListTextCtrlWrapper (internal)
1388 //-----------------------------------------------------------------------------
1389
1390 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper, wxEvtHandler)
1391 EVT_CHAR (wxListTextCtrlWrapper::OnChar)
1392 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp)
1393 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus)
1394 END_EVENT_TABLE()
1395
1396 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow *owner,
1397 wxTextCtrl *text,
1398 size_t itemEdit)
1399 : m_startValue(owner->GetItemText(itemEdit)),
1400 m_itemEdited(itemEdit)
1401 {
1402 m_owner = owner;
1403 m_text = text;
1404 m_aboutToFinish = false;
1405
1406 wxGenericListCtrl *parent = m_owner->GetListCtrl();
1407
1408 wxRect rectLabel = owner->GetLineLabelRect(itemEdit);
1409
1410 parent->CalcScrolledPosition(rectLabel.x, rectLabel.y,
1411 &rectLabel.x, &rectLabel.y);
1412
1413 m_text->Create(owner, wxID_ANY, m_startValue,
1414 wxPoint(rectLabel.x-4,rectLabel.y-4),
1415 wxSize(rectLabel.width+11,rectLabel.height+8));
1416 m_text->SetFocus();
1417
1418 m_text->PushEventHandler(this);
1419 }
1420
1421 void wxListTextCtrlWrapper::EndEdit(EndReason reason)
1422 {
1423 m_aboutToFinish = true;
1424
1425 switch ( reason )
1426 {
1427 case End_Accept:
1428 // Notify the owner about the changes
1429 AcceptChanges();
1430
1431 // Even if vetoed, close the control (consistent with MSW)
1432 Finish( true );
1433 break;
1434
1435 case End_Discard:
1436 m_owner->OnRenameCancelled(m_itemEdited);
1437
1438 Finish( true );
1439 break;
1440
1441 case End_Destroy:
1442 // Don't generate any notifications for the control being destroyed
1443 // and don't set focus to it neither.
1444 Finish(false);
1445 break;
1446 }
1447 }
1448
1449 void wxListTextCtrlWrapper::Finish( bool setfocus )
1450 {
1451 m_text->RemoveEventHandler(this);
1452 m_owner->ResetTextControl( m_text );
1453
1454 wxPendingDelete.Append( this );
1455
1456 if (setfocus)
1457 m_owner->SetFocus();
1458 }
1459
1460 bool wxListTextCtrlWrapper::AcceptChanges()
1461 {
1462 const wxString value = m_text->GetValue();
1463
1464 // notice that we should always call OnRenameAccept() to generate the "end
1465 // label editing" event, even if the user hasn't really changed anything
1466 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
1467 {
1468 // vetoed by the user
1469 return false;
1470 }
1471
1472 // accepted, do rename the item (unless nothing changed)
1473 if ( value != m_startValue )
1474 m_owner->SetItemText(m_itemEdited, value);
1475
1476 return true;
1477 }
1478
1479 void wxListTextCtrlWrapper::OnChar( wxKeyEvent &event )
1480 {
1481 if ( !CheckForEndEditKey(event) )
1482 event.Skip();
1483 }
1484
1485 bool wxListTextCtrlWrapper::CheckForEndEditKey(const wxKeyEvent& event)
1486 {
1487 switch ( event.m_keyCode )
1488 {
1489 case WXK_RETURN:
1490 EndEdit( End_Accept );
1491 break;
1492
1493 case WXK_ESCAPE:
1494 EndEdit( End_Discard );
1495 break;
1496
1497 default:
1498 return false;
1499 }
1500
1501 return true;
1502 }
1503
1504 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
1505 {
1506 if (m_aboutToFinish)
1507 {
1508 // auto-grow the textctrl:
1509 wxSize parentSize = m_owner->GetSize();
1510 wxPoint myPos = m_text->GetPosition();
1511 wxSize mySize = m_text->GetSize();
1512 int sx, sy;
1513 m_text->GetTextExtent(m_text->GetValue() + wxT("MM"), &sx, &sy);
1514 if (myPos.x + sx > parentSize.x)
1515 sx = parentSize.x - myPos.x;
1516 if (mySize.x > sx)
1517 sx = mySize.x;
1518 m_text->SetSize(sx, wxDefaultCoord);
1519 }
1520
1521 event.Skip();
1522 }
1523
1524 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
1525 {
1526 if ( !m_aboutToFinish )
1527 {
1528 if ( !AcceptChanges() )
1529 m_owner->OnRenameCancelled( m_itemEdited );
1530
1531 Finish( false );
1532 }
1533
1534 // We must let the native text control handle focus
1535 event.Skip();
1536 }
1537
1538 //-----------------------------------------------------------------------------
1539 // wxListMainWindow
1540 //-----------------------------------------------------------------------------
1541
1542 BEGIN_EVENT_TABLE(wxListMainWindow, wxWindow)
1543 EVT_PAINT (wxListMainWindow::OnPaint)
1544 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse)
1545 EVT_CHAR_HOOK (wxListMainWindow::OnCharHook)
1546 EVT_CHAR (wxListMainWindow::OnChar)
1547 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown)
1548 EVT_KEY_UP (wxListMainWindow::OnKeyUp)
1549 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus)
1550 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus)
1551 EVT_SCROLLWIN (wxListMainWindow::OnScroll)
1552 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus)
1553 END_EVENT_TABLE()
1554
1555 void wxListMainWindow::Init()
1556 {
1557 m_dirty = true;
1558 m_countVirt = 0;
1559 m_lineFrom =
1560 m_lineTo = (size_t)-1;
1561 m_linesPerPage = 0;
1562
1563 m_headerWidth =
1564 m_lineHeight = 0;
1565
1566 m_small_image_list = NULL;
1567 m_normal_image_list = NULL;
1568
1569 m_small_spacing = 30;
1570 m_normal_spacing = 40;
1571
1572 m_hasFocus = false;
1573 m_dragCount = 0;
1574 m_isCreated = false;
1575
1576 m_lastOnSame = false;
1577 m_renameTimer = new wxListRenameTimer( this );
1578 m_findTimer = NULL;
1579 m_findBell = 0; // default is to not ring bell at all
1580 m_textctrlWrapper = NULL;
1581
1582 m_current =
1583 m_lineLastClicked =
1584 m_lineSelectSingleOnUp =
1585 m_lineBeforeLastClicked = (size_t)-1;
1586 }
1587
1588 wxListMainWindow::wxListMainWindow()
1589 {
1590 Init();
1591
1592 m_highlightBrush =
1593 m_highlightUnfocusedBrush = NULL;
1594 }
1595
1596 wxListMainWindow::wxListMainWindow( wxWindow *parent,
1597 wxWindowID id,
1598 const wxPoint& pos,
1599 const wxSize& size )
1600 : wxWindow( parent, id, pos, size,
1601 wxWANTS_CHARS | wxBORDER_NONE )
1602 {
1603 Init();
1604
1605 m_highlightBrush = new wxBrush
1606 (
1607 wxSystemSettings::GetColour
1608 (
1609 wxSYS_COLOUR_HIGHLIGHT
1610 ),
1611 wxBRUSHSTYLE_SOLID
1612 );
1613
1614 m_highlightUnfocusedBrush = new wxBrush
1615 (
1616 wxSystemSettings::GetColour
1617 (
1618 wxSYS_COLOUR_BTNSHADOW
1619 ),
1620 wxBRUSHSTYLE_SOLID
1621 );
1622
1623 wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes();
1624 SetOwnForegroundColour( attr.colFg );
1625 SetOwnBackgroundColour( attr.colBg );
1626 if (!m_hasFont)
1627 SetOwnFont( attr.font );
1628 }
1629
1630 wxListMainWindow::~wxListMainWindow()
1631 {
1632 if ( m_textctrlWrapper )
1633 m_textctrlWrapper->EndEdit(wxListTextCtrlWrapper::End_Destroy);
1634
1635 DoDeleteAllItems();
1636 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
1637 WX_CLEAR_ARRAY(m_aColWidths);
1638
1639 delete m_highlightBrush;
1640 delete m_highlightUnfocusedBrush;
1641 delete m_renameTimer;
1642 delete m_findTimer;
1643 }
1644
1645 void wxListMainWindow::SetReportView(bool inReportView)
1646 {
1647 const size_t count = m_lines.size();
1648 for ( size_t n = 0; n < count; n++ )
1649 {
1650 m_lines[n].SetReportView(inReportView);
1651 }
1652 }
1653
1654 void wxListMainWindow::CacheLineData(size_t line)
1655 {
1656 wxGenericListCtrl *listctrl = GetListCtrl();
1657
1658 wxListLineData *ld = GetDummyLine();
1659
1660 size_t countCol = GetColumnCount();
1661 for ( size_t col = 0; col < countCol; col++ )
1662 {
1663 ld->SetText(col, listctrl->OnGetItemText(line, col));
1664 ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col));
1665 }
1666
1667 ld->SetAttr(listctrl->OnGetItemAttr(line));
1668 }
1669
1670 wxListLineData *wxListMainWindow::GetDummyLine() const
1671 {
1672 wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
1673 wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
1674
1675 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
1676
1677 // we need to recreate the dummy line if the number of columns in the
1678 // control changed as it would have the incorrect number of fields
1679 // otherwise
1680 if ( !m_lines.IsEmpty() &&
1681 m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() )
1682 {
1683 self->m_lines.Clear();
1684 }
1685
1686 if ( m_lines.IsEmpty() )
1687 {
1688 wxListLineData *line = new wxListLineData(self);
1689 self->m_lines.Add(line);
1690
1691 // don't waste extra memory -- there never going to be anything
1692 // else/more in this array
1693 self->m_lines.Shrink();
1694 }
1695
1696 return &m_lines[0];
1697 }
1698
1699 // ----------------------------------------------------------------------------
1700 // line geometry (report mode only)
1701 // ----------------------------------------------------------------------------
1702
1703 wxCoord wxListMainWindow::GetLineHeight() const
1704 {
1705 // we cache the line height as calling GetTextExtent() is slow
1706 if ( !m_lineHeight )
1707 {
1708 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
1709
1710 wxClientDC dc( self );
1711 dc.SetFont( GetFont() );
1712
1713 wxCoord y;
1714 dc.GetTextExtent(wxT("H"), NULL, &y);
1715
1716 if ( m_small_image_list && m_small_image_list->GetImageCount() )
1717 {
1718 int iw = 0, ih = 0;
1719 m_small_image_list->GetSize(0, iw, ih);
1720 y = wxMax(y, ih);
1721 }
1722
1723 y += EXTRA_HEIGHT;
1724 self->m_lineHeight = y + LINE_SPACING;
1725 }
1726
1727 return m_lineHeight;
1728 }
1729
1730 wxCoord wxListMainWindow::GetLineY(size_t line) const
1731 {
1732 wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
1733
1734 return LINE_SPACING + line * GetLineHeight();
1735 }
1736
1737 wxRect wxListMainWindow::GetLineRect(size_t line) const
1738 {
1739 if ( !InReportView() )
1740 return GetLine(line)->m_gi->m_rectAll;
1741
1742 wxRect rect;
1743 rect.x = HEADER_OFFSET_X;
1744 rect.y = GetLineY(line);
1745 rect.width = GetHeaderWidth();
1746 rect.height = GetLineHeight();
1747
1748 return rect;
1749 }
1750
1751 wxRect wxListMainWindow::GetLineLabelRect(size_t line) const
1752 {
1753 if ( !InReportView() )
1754 return GetLine(line)->m_gi->m_rectLabel;
1755
1756 int image_x = 0;
1757 wxListLineData *data = GetLine(line);
1758 wxListItemDataList::compatibility_iterator node = data->m_items.GetFirst();
1759 if (node)
1760 {
1761 wxListItemData *item = node->GetData();
1762 if ( item->HasImage() )
1763 {
1764 int ix, iy;
1765 GetImageSize( item->GetImage(), ix, iy );
1766 image_x = 3 + ix + IMAGE_MARGIN_IN_REPORT_MODE;
1767 }
1768 }
1769
1770 wxRect rect;
1771 rect.x = image_x + HEADER_OFFSET_X;
1772 rect.y = GetLineY(line);
1773 rect.width = GetColumnWidth(0) - image_x;
1774 rect.height = GetLineHeight();
1775
1776 return rect;
1777 }
1778
1779 wxRect wxListMainWindow::GetLineIconRect(size_t line) const
1780 {
1781 if ( !InReportView() )
1782 return GetLine(line)->m_gi->m_rectIcon;
1783
1784 wxListLineData *ld = GetLine(line);
1785 wxASSERT_MSG( ld->HasImage(), wxT("should have an image") );
1786
1787 wxRect rect;
1788 rect.x = HEADER_OFFSET_X;
1789 rect.y = GetLineY(line);
1790 GetImageSize(ld->GetImage(), rect.width, rect.height);
1791
1792 return rect;
1793 }
1794
1795 wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const
1796 {
1797 return InReportView() ? GetLineRect(line)
1798 : GetLine(line)->m_gi->m_rectHighlight;
1799 }
1800
1801 long wxListMainWindow::HitTestLine(size_t line, int x, int y) const
1802 {
1803 wxASSERT_MSG( line < GetItemCount(), wxT("invalid line in HitTestLine") );
1804
1805 wxListLineData *ld = GetLine(line);
1806
1807 if ( ld->HasImage() && GetLineIconRect(line).Contains(x, y) )
1808 return wxLIST_HITTEST_ONITEMICON;
1809
1810 // VS: Testing for "ld->HasText() || InReportView()" instead of
1811 // "ld->HasText()" is needed to make empty lines in report view
1812 // possible
1813 if ( ld->HasText() || InReportView() )
1814 {
1815 wxRect rect = InReportView() ? GetLineRect(line)
1816 : GetLineLabelRect(line);
1817
1818 if ( rect.Contains(x, y) )
1819 return wxLIST_HITTEST_ONITEMLABEL;
1820 }
1821
1822 return 0;
1823 }
1824
1825 // ----------------------------------------------------------------------------
1826 // highlight (selection) handling
1827 // ----------------------------------------------------------------------------
1828
1829 bool wxListMainWindow::IsHighlighted(size_t line) const
1830 {
1831 if ( IsVirtual() )
1832 {
1833 return m_selStore.IsSelected(line);
1834 }
1835 else // !virtual
1836 {
1837 wxListLineData *ld = GetLine(line);
1838 wxCHECK_MSG( ld, false, wxT("invalid index in IsHighlighted") );
1839
1840 return ld->IsHighlighted();
1841 }
1842 }
1843
1844 void wxListMainWindow::HighlightLines( size_t lineFrom,
1845 size_t lineTo,
1846 bool highlight )
1847 {
1848 if ( IsVirtual() )
1849 {
1850 wxArrayInt linesChanged;
1851 if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight,
1852 &linesChanged) )
1853 {
1854 // meny items changed state, refresh everything
1855 RefreshLines(lineFrom, lineTo);
1856 }
1857 else // only a few items changed state, refresh only them
1858 {
1859 size_t count = linesChanged.GetCount();
1860 for ( size_t n = 0; n < count; n++ )
1861 {
1862 RefreshLine(linesChanged[n]);
1863 }
1864 }
1865 }
1866 else // iterate over all items in non report view
1867 {
1868 for ( size_t line = lineFrom; line <= lineTo; line++ )
1869 {
1870 if ( HighlightLine(line, highlight) )
1871 RefreshLine(line);
1872 }
1873 }
1874 }
1875
1876 bool wxListMainWindow::HighlightLine( size_t line, bool highlight )
1877 {
1878 bool changed;
1879
1880 if ( IsVirtual() )
1881 {
1882 changed = m_selStore.SelectItem(line, highlight);
1883 }
1884 else // !virtual
1885 {
1886 wxListLineData *ld = GetLine(line);
1887 wxCHECK_MSG( ld, false, wxT("invalid index in HighlightLine") );
1888
1889 changed = ld->Highlight(highlight);
1890 }
1891
1892 if ( changed )
1893 {
1894 SendNotify( line, highlight ? wxEVT_LIST_ITEM_SELECTED
1895 : wxEVT_LIST_ITEM_DESELECTED );
1896 }
1897
1898 return changed;
1899 }
1900
1901 void wxListMainWindow::RefreshLine( size_t line )
1902 {
1903 if ( InReportView() )
1904 {
1905 size_t visibleFrom, visibleTo;
1906 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1907
1908 if ( line < visibleFrom || line > visibleTo )
1909 return;
1910 }
1911
1912 wxRect rect = GetLineRect(line);
1913
1914 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1915 RefreshRect( rect );
1916 }
1917
1918 void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo )
1919 {
1920 // we suppose that they are ordered by caller
1921 wxASSERT_MSG( lineFrom <= lineTo, wxT("indices in disorder") );
1922
1923 wxASSERT_MSG( lineTo < GetItemCount(), wxT("invalid line range") );
1924
1925 if ( InReportView() )
1926 {
1927 size_t visibleFrom, visibleTo;
1928 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1929
1930 if ( lineFrom < visibleFrom )
1931 lineFrom = visibleFrom;
1932 if ( lineTo > visibleTo )
1933 lineTo = visibleTo;
1934
1935 wxRect rect;
1936 rect.x = 0;
1937 rect.y = GetLineY(lineFrom);
1938 rect.width = GetClientSize().x;
1939 rect.height = GetLineY(lineTo) - rect.y + GetLineHeight();
1940
1941 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1942 RefreshRect( rect );
1943 }
1944 else // !report
1945 {
1946 // TODO: this should be optimized...
1947 for ( size_t line = lineFrom; line <= lineTo; line++ )
1948 {
1949 RefreshLine(line);
1950 }
1951 }
1952 }
1953
1954 void wxListMainWindow::RefreshAfter( size_t lineFrom )
1955 {
1956 if ( InReportView() )
1957 {
1958 size_t visibleFrom, visibleTo;
1959 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1960
1961 if ( lineFrom < visibleFrom )
1962 lineFrom = visibleFrom;
1963 else if ( lineFrom > visibleTo )
1964 return;
1965
1966 wxRect rect;
1967 rect.x = 0;
1968 rect.y = GetLineY(lineFrom);
1969 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1970
1971 wxSize size = GetClientSize();
1972 rect.width = size.x;
1973
1974 // refresh till the bottom of the window
1975 rect.height = size.y - rect.y;
1976
1977 RefreshRect( rect );
1978 }
1979 else // !report
1980 {
1981 // TODO: how to do it more efficiently?
1982 m_dirty = true;
1983 }
1984 }
1985
1986 void wxListMainWindow::RefreshSelected()
1987 {
1988 if ( IsEmpty() )
1989 return;
1990
1991 size_t from, to;
1992 if ( InReportView() )
1993 {
1994 GetVisibleLinesRange(&from, &to);
1995 }
1996 else // !virtual
1997 {
1998 from = 0;
1999 to = GetItemCount() - 1;
2000 }
2001
2002 if ( HasCurrent() && m_current >= from && m_current <= to )
2003 RefreshLine(m_current);
2004
2005 for ( size_t line = from; line <= to; line++ )
2006 {
2007 // NB: the test works as expected even if m_current == -1
2008 if ( line != m_current && IsHighlighted(line) )
2009 RefreshLine(line);
2010 }
2011 }
2012
2013 void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2014 {
2015 // Note: a wxPaintDC must be constructed even if no drawing is
2016 // done (a Windows requirement).
2017 wxPaintDC dc( this );
2018
2019 if ( IsEmpty() )
2020 {
2021 // nothing to draw or not the moment to draw it
2022 return;
2023 }
2024
2025 if ( m_dirty )
2026 RecalculatePositions( false );
2027
2028 GetListCtrl()->PrepareDC( dc );
2029
2030 int dev_x, dev_y;
2031 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
2032
2033 dc.SetFont( GetFont() );
2034
2035 if ( InReportView() )
2036 {
2037 int lineHeight = GetLineHeight();
2038
2039 size_t visibleFrom, visibleTo;
2040 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2041
2042 wxRect rectLine;
2043 int xOrig = dc.LogicalToDeviceX( 0 );
2044 int yOrig = dc.LogicalToDeviceY( 0 );
2045
2046 // tell the caller cache to cache the data
2047 if ( IsVirtual() )
2048 {
2049 wxListEvent evCache(wxEVT_LIST_CACHE_HINT,
2050 GetParent()->GetId());
2051 evCache.SetEventObject( GetParent() );
2052 evCache.m_oldItemIndex = visibleFrom;
2053 evCache.m_item.m_itemId =
2054 evCache.m_itemIndex = visibleTo;
2055 GetParent()->GetEventHandler()->ProcessEvent( evCache );
2056 }
2057
2058 for ( size_t line = visibleFrom; line <= visibleTo; line++ )
2059 {
2060 rectLine = GetLineRect(line);
2061
2062
2063 if ( !IsExposed(rectLine.x + xOrig, rectLine.y + yOrig,
2064 rectLine.width, rectLine.height) )
2065 {
2066 // don't redraw unaffected lines to avoid flicker
2067 continue;
2068 }
2069
2070 GetLine(line)->DrawInReportMode( &dc,
2071 rectLine,
2072 GetLineHighlightRect(line),
2073 IsHighlighted(line),
2074 line == m_current );
2075 }
2076
2077 if ( HasFlag(wxLC_HRULES) )
2078 {
2079 wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
2080 wxSize clientSize = GetClientSize();
2081
2082 size_t i = visibleFrom;
2083 if (i == 0) i = 1; // Don't draw the first one
2084 for ( ; i <= visibleTo; i++ )
2085 {
2086 dc.SetPen(pen);
2087 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2088 dc.DrawLine(0 - dev_x, i * lineHeight,
2089 clientSize.x - dev_x, i * lineHeight);
2090 }
2091
2092 // Draw last horizontal rule
2093 if ( visibleTo == GetItemCount() - 1 )
2094 {
2095 dc.SetPen( pen );
2096 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2097 dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight,
2098 clientSize.x - dev_x , (m_lineTo + 1) * lineHeight );
2099 }
2100 }
2101
2102 // Draw vertical rules if required
2103 if ( HasFlag(wxLC_VRULES) && !IsEmpty() )
2104 {
2105 wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
2106 wxRect firstItemRect, lastItemRect;
2107
2108 GetItemRect(visibleFrom, firstItemRect);
2109 GetItemRect(visibleTo, lastItemRect);
2110 int x = firstItemRect.GetX();
2111 dc.SetPen(pen);
2112 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2113
2114 for (int col = 0; col < GetColumnCount(); col++)
2115 {
2116 int colWidth = GetColumnWidth(col);
2117 x += colWidth;
2118 int x_pos = x - dev_x;
2119 if (col < GetColumnCount()-1) x_pos -= 2;
2120 dc.DrawLine(x_pos, firstItemRect.GetY() - 1 - dev_y,
2121 x_pos, lastItemRect.GetBottom() + 1 - dev_y);
2122 }
2123 }
2124 }
2125 else // !report
2126 {
2127 size_t count = GetItemCount();
2128 for ( size_t i = 0; i < count; i++ )
2129 {
2130 GetLine(i)->Draw( &dc, i == m_current );
2131 }
2132 }
2133
2134 // DrawFocusRect() is unusable under Mac, it draws outside of the highlight
2135 // rectangle somehow and so leaves traces when the item is not selected any
2136 // more, see #12229.
2137 #ifndef __WXMAC__
2138 if ( HasCurrent() )
2139 {
2140 int flags = 0;
2141 if ( IsHighlighted(m_current) )
2142 flags |= wxCONTROL_SELECTED;
2143
2144 wxRendererNative::Get().
2145 DrawFocusRect(this, dc, GetLineHighlightRect(m_current), flags);
2146 }
2147 #endif // !__WXMAC__
2148 }
2149
2150 void wxListMainWindow::HighlightAll( bool on )
2151 {
2152 if ( IsSingleSel() )
2153 {
2154 wxASSERT_MSG( !on, wxT("can't do this in a single selection control") );
2155
2156 // we just have one item to turn off
2157 if ( HasCurrent() && IsHighlighted(m_current) )
2158 {
2159 HighlightLine(m_current, false);
2160 RefreshLine(m_current);
2161 }
2162 }
2163 else // multi selection
2164 {
2165 if ( !IsEmpty() )
2166 HighlightLines(0, GetItemCount() - 1, on);
2167 }
2168 }
2169
2170 void wxListMainWindow::OnChildFocus(wxChildFocusEvent& WXUNUSED(event))
2171 {
2172 // Do nothing here. This prevents the default handler in wxScrolledWindow
2173 // from needlessly scrolling the window when the edit control is
2174 // dismissed. See ticket #9563.
2175 }
2176
2177 void wxListMainWindow::SendNotify( size_t line,
2178 wxEventType command,
2179 const wxPoint& point )
2180 {
2181 wxListEvent le( command, GetParent()->GetId() );
2182 le.SetEventObject( GetParent() );
2183
2184 le.m_item.m_itemId =
2185 le.m_itemIndex = line;
2186
2187 // set only for events which have position
2188 if ( point != wxDefaultPosition )
2189 le.m_pointDrag = point;
2190
2191 // don't try to get the line info for virtual list controls: the main
2192 // program has it anyhow and if we did it would result in accessing all
2193 // the lines, even those which are not visible now and this is precisely
2194 // what we're trying to avoid
2195 if ( !IsVirtual() )
2196 {
2197 if ( line != (size_t)-1 )
2198 {
2199 GetLine(line)->GetItem( 0, le.m_item );
2200 }
2201 //else: this happens for wxEVT_LIST_ITEM_FOCUSED event
2202 }
2203 //else: there may be no more such item
2204
2205 GetParent()->GetEventHandler()->ProcessEvent( le );
2206 }
2207
2208 void wxListMainWindow::ChangeCurrent(size_t current)
2209 {
2210 m_current = current;
2211
2212 // as the current item changed, we shouldn't start editing it when the
2213 // "slow click" timer expires as the click happened on another item
2214 if ( m_renameTimer->IsRunning() )
2215 m_renameTimer->Stop();
2216
2217 SendNotify(current, wxEVT_LIST_ITEM_FOCUSED);
2218 }
2219
2220 wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
2221 {
2222 wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
2223 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2224
2225 wxASSERT_MSG( textControlClass->IsKindOf(wxCLASSINFO(wxTextCtrl)),
2226 wxT("EditLabel() needs a text control") );
2227
2228 size_t itemEdit = (size_t)item;
2229
2230 wxListEvent le( wxEVT_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
2231 le.SetEventObject( GetParent() );
2232 le.m_item.m_itemId =
2233 le.m_itemIndex = item;
2234 wxListLineData *data = GetLine(itemEdit);
2235 wxCHECK_MSG( data, NULL, wxT("invalid index in EditLabel()") );
2236 data->GetItem( 0, le.m_item );
2237
2238 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
2239 {
2240 // vetoed by user code
2241 return NULL;
2242 }
2243
2244 if ( m_dirty )
2245 {
2246 // Ensure the display is updated before we start editing.
2247 Update();
2248 }
2249
2250 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
2251 m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
2252 return m_textctrlWrapper->GetText();
2253 }
2254
2255 void wxListMainWindow::OnRenameTimer()
2256 {
2257 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2258
2259 EditLabel( m_current );
2260 }
2261
2262 bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
2263 {
2264 wxListEvent le( wxEVT_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2265 le.SetEventObject( GetParent() );
2266 le.m_item.m_itemId =
2267 le.m_itemIndex = itemEdit;
2268
2269 wxListLineData *data = GetLine(itemEdit);
2270
2271 wxCHECK_MSG( data, false, wxT("invalid index in OnRenameAccept()") );
2272
2273 data->GetItem( 0, le.m_item );
2274 le.m_item.m_text = value;
2275 return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
2276 le.IsAllowed();
2277 }
2278
2279 void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
2280 {
2281 // let owner know that the edit was cancelled
2282 wxListEvent le( wxEVT_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2283
2284 le.SetEditCanceled(true);
2285
2286 le.SetEventObject( GetParent() );
2287 le.m_item.m_itemId =
2288 le.m_itemIndex = itemEdit;
2289
2290 wxListLineData *data = GetLine(itemEdit);
2291 wxCHECK_RET( data, wxT("invalid index in OnRenameCancelled()") );
2292
2293 data->GetItem( 0, le.m_item );
2294 GetEventHandler()->ProcessEvent( le );
2295 }
2296
2297 void wxListMainWindow::OnFindTimer()
2298 {
2299 m_findPrefix.clear();
2300 if ( m_findBell )
2301 m_findBell = 1;
2302 }
2303
2304 void wxListMainWindow::EnableBellOnNoMatch( bool on )
2305 {
2306 m_findBell = on;
2307 }
2308
2309 void wxListMainWindow::OnMouse( wxMouseEvent &event )
2310 {
2311 #ifdef __WXMAC__
2312 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2313 // shutdown the edit control when the mouse is clicked elsewhere on the
2314 // listctrl because the order of events is different (or something like
2315 // that), so explicitly end the edit if it is active.
2316 if ( event.LeftDown() && m_textctrlWrapper )
2317 m_textctrlWrapper->EndEdit(wxListTextCtrlWrapper::End_Accept);
2318 #endif // __WXMAC__
2319
2320 if ( event.LeftDown() )
2321 {
2322 // Ensure we skip the event to let the system set focus to this window.
2323 event.Skip();
2324 }
2325
2326 // Pretend that the event happened in wxListCtrl itself.
2327 wxMouseEvent me(event);
2328 me.SetEventObject( GetParent() );
2329 me.SetId(GetParent()->GetId());
2330 if ( GetParent()->GetEventHandler()->ProcessEvent( me ))
2331 return;
2332
2333 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
2334 {
2335 // let the base class handle mouse wheel events.
2336 event.Skip();
2337 return;
2338 }
2339
2340 if ( !HasCurrent() || IsEmpty() )
2341 {
2342 if (event.RightDown())
2343 {
2344 SendNotify( (size_t)-1, wxEVT_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2345
2346 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
2347 GetParent()->GetId(),
2348 ClientToScreen(event.GetPosition()));
2349 evtCtx.SetEventObject(GetParent());
2350 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
2351 }
2352 return;
2353 }
2354
2355 if (m_dirty)
2356 return;
2357
2358 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
2359 event.ButtonDClick()) )
2360 return;
2361
2362 int x = event.GetX();
2363 int y = event.GetY();
2364 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
2365
2366 // where did we hit it (if we did)?
2367 long hitResult = 0;
2368
2369 size_t count = GetItemCount(),
2370 current;
2371
2372 if ( InReportView() )
2373 {
2374 current = y / GetLineHeight();
2375 if ( current < count )
2376 hitResult = HitTestLine(current, x, y);
2377 }
2378 else // !report
2379 {
2380 // TODO: optimize it too! this is less simple than for report view but
2381 // enumerating all items is still not a way to do it!!
2382 for ( current = 0; current < count; current++ )
2383 {
2384 hitResult = HitTestLine(current, x, y);
2385 if ( hitResult )
2386 break;
2387 }
2388 }
2389
2390 // Update drag events counter first as we must do it even if the mouse is
2391 // not on any item right now as we must keep count in case we started
2392 // dragging from the empty control area but continued to do it over a valid
2393 // item -- in this situation we must not start dragging this item.
2394 if (event.Dragging())
2395 m_dragCount++;
2396 else
2397 m_dragCount = 0;
2398
2399 // The only mouse event that can be generated without any valid item is
2400 // wxEVT_LIST_ITEM_RIGHT_CLICK as it can be useful to have a global
2401 // popup menu for the list control itself which should be shown even when
2402 // the user clicks outside of any item.
2403 if ( !hitResult )
2404 {
2405 // outside of any item
2406 if (event.RightDown())
2407 {
2408 SendNotify( (size_t) -1, wxEVT_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2409
2410 wxContextMenuEvent evtCtx(
2411 wxEVT_CONTEXT_MENU,
2412 GetParent()->GetId(),
2413 ClientToScreen(event.GetPosition()));
2414 evtCtx.SetEventObject(GetParent());
2415 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
2416 }
2417 else
2418 {
2419 // reset the selection and bail out
2420 HighlightAll(false);
2421 }
2422
2423 return;
2424 }
2425
2426 if ( event.Dragging() )
2427 {
2428 if (m_dragCount == 1)
2429 {
2430 // we have to report the raw, physical coords as we want to be
2431 // able to call HitTest(event.m_pointDrag) from the user code to
2432 // get the item being dragged
2433 m_dragStart = event.GetPosition();
2434 }
2435
2436 if (m_dragCount != 3)
2437 return;
2438
2439 int command = event.RightIsDown() ? wxEVT_LIST_BEGIN_RDRAG
2440 : wxEVT_LIST_BEGIN_DRAG;
2441
2442 SendNotify( m_lineLastClicked, command, m_dragStart );
2443
2444 return;
2445 }
2446
2447 bool forceClick = false;
2448 if (event.ButtonDClick())
2449 {
2450 if ( m_renameTimer->IsRunning() )
2451 m_renameTimer->Stop();
2452
2453 m_lastOnSame = false;
2454
2455 if ( current == m_lineLastClicked )
2456 {
2457 SendNotify( current, wxEVT_LIST_ITEM_ACTIVATED );
2458
2459 return;
2460 }
2461 else
2462 {
2463 // The first click was on another item, so don't interpret this as
2464 // a double click, but as a simple click instead
2465 forceClick = true;
2466 }
2467 }
2468
2469 if (event.LeftUp())
2470 {
2471 if (m_lineSelectSingleOnUp != (size_t)-1)
2472 {
2473 // select single line
2474 HighlightAll( false );
2475 ReverseHighlight(m_lineSelectSingleOnUp);
2476 }
2477
2478 if (m_lastOnSame)
2479 {
2480 if ((current == m_current) &&
2481 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
2482 HasFlag(wxLC_EDIT_LABELS) )
2483 {
2484 if ( !InReportView() ||
2485 GetLineLabelRect(current).Contains(x, y) )
2486 {
2487 int dclick = wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC);
2488 m_renameTimer->Start(dclick > 0 ? dclick : 250, true);
2489 }
2490 }
2491
2492 m_lastOnSame = false;
2493 }
2494
2495 m_lineSelectSingleOnUp = (size_t)-1;
2496 }
2497 else
2498 {
2499 // This is necessary, because after a DnD operation in
2500 // from and to ourself, the up event is swallowed by the
2501 // DnD code. So on next non-up event (which means here and
2502 // now) m_lineSelectSingleOnUp should be reset.
2503 m_lineSelectSingleOnUp = (size_t)-1;
2504 }
2505 if (event.RightDown())
2506 {
2507 m_lineBeforeLastClicked = m_lineLastClicked;
2508 m_lineLastClicked = current;
2509
2510 // If the item is already selected, do not update the selection.
2511 // Multi-selections should not be cleared if a selected item is clicked.
2512 if (!IsHighlighted(current))
2513 {
2514 HighlightAll(false);
2515 ChangeCurrent(current);
2516 ReverseHighlight(m_current);
2517 }
2518
2519 SendNotify( current, wxEVT_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2520
2521 // Allow generation of context menu event
2522 event.Skip();
2523 }
2524 else if (event.MiddleDown())
2525 {
2526 SendNotify( current, wxEVT_LIST_ITEM_MIDDLE_CLICK );
2527 }
2528 else if ( event.LeftDown() || forceClick )
2529 {
2530 m_lineBeforeLastClicked = m_lineLastClicked;
2531 m_lineLastClicked = current;
2532
2533 size_t oldCurrent = m_current;
2534 bool oldWasSelected = IsHighlighted(m_current);
2535
2536 bool cmdModifierDown = event.CmdDown();
2537 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
2538 {
2539 if ( IsSingleSel() || !IsHighlighted(current) )
2540 {
2541 HighlightAll( false );
2542
2543 ChangeCurrent(current);
2544
2545 ReverseHighlight(m_current);
2546 }
2547 else // multi sel & current is highlighted & no mod keys
2548 {
2549 m_lineSelectSingleOnUp = current;
2550 ChangeCurrent(current); // change focus
2551 }
2552 }
2553 else // multi sel & either ctrl or shift is down
2554 {
2555 if (cmdModifierDown)
2556 {
2557 ChangeCurrent(current);
2558
2559 ReverseHighlight(m_current);
2560 }
2561 else if (event.ShiftDown())
2562 {
2563 ChangeCurrent(current);
2564
2565 size_t lineFrom = oldCurrent,
2566 lineTo = current;
2567
2568 if ( lineTo < lineFrom )
2569 {
2570 lineTo = lineFrom;
2571 lineFrom = m_current;
2572 }
2573
2574 HighlightLines(lineFrom, lineTo);
2575 }
2576 else // !ctrl, !shift
2577 {
2578 // test in the enclosing if should make it impossible
2579 wxFAIL_MSG( wxT("how did we get here?") );
2580 }
2581 }
2582
2583 if (m_current != oldCurrent)
2584 RefreshLine( oldCurrent );
2585
2586 // Set the flag telling us whether the next click on this item should
2587 // start editing its label. This should happen if we clicked on the
2588 // current item and it was already selected, i.e. if this click was not
2589 // done to select it.
2590 //
2591 // It should not happen if this was a double click (forceClick is true)
2592 // nor if we hadn't had the focus before as then this click was used to
2593 // give focus to the control.
2594 m_lastOnSame = (m_current == oldCurrent) && oldWasSelected &&
2595 !forceClick && HasFocus();
2596 }
2597 }
2598
2599 void wxListMainWindow::MoveToItem(size_t item)
2600 {
2601 if ( item == (size_t)-1 )
2602 return;
2603
2604 wxRect rect = GetLineRect(item);
2605
2606 int client_w, client_h;
2607 GetClientSize( &client_w, &client_h );
2608
2609 const int hLine = GetLineHeight();
2610
2611 int view_x = SCROLL_UNIT_X * GetListCtrl()->GetScrollPos( wxHORIZONTAL );
2612 int view_y = hLine * GetListCtrl()->GetScrollPos( wxVERTICAL );
2613
2614 if ( InReportView() )
2615 {
2616 // the next we need the range of lines shown it might be different,
2617 // so recalculate it
2618 ResetVisibleLinesRange();
2619
2620 if (rect.y < view_y)
2621 GetListCtrl()->Scroll( -1, rect.y / hLine );
2622 if (rect.y + rect.height + 5 > view_y + client_h)
2623 GetListCtrl()->Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
2624
2625 #ifdef __WXMAC__
2626 // At least on Mac the visible lines value will get reset inside of
2627 // Scroll *before* it actually scrolls the window because of the
2628 // Update() that happens there, so it will still have the wrong value.
2629 // So let's reset it again and wait for it to be recalculated in the
2630 // next paint event. I would expect this problem to show up in wxGTK
2631 // too but couldn't duplicate it there. Perhaps the order of events
2632 // is different... --Robin
2633 ResetVisibleLinesRange();
2634 #endif
2635 }
2636 else // !report
2637 {
2638 int sx = -1,
2639 sy = -1;
2640
2641 if (rect.x-view_x < 5)
2642 sx = (rect.x - 5) / SCROLL_UNIT_X;
2643 if (rect.x + rect.width - 5 > view_x + client_w)
2644 sx = (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X;
2645
2646 if (rect.y-view_y < 5)
2647 sy = (rect.y - 5) / hLine;
2648 if (rect.y + rect.height - 5 > view_y + client_h)
2649 sy = (rect.y + rect.height - client_h + hLine) / hLine;
2650
2651 GetListCtrl()->Scroll(sx, sy);
2652 }
2653 }
2654
2655 bool wxListMainWindow::ScrollList(int WXUNUSED(dx), int dy)
2656 {
2657 if ( !InReportView() )
2658 {
2659 // TODO: this should work in all views but is not implemented now
2660 return false;
2661 }
2662
2663 size_t top, bottom;
2664 GetVisibleLinesRange(&top, &bottom);
2665
2666 if ( bottom == (size_t)-1 )
2667 return 0;
2668
2669 ResetVisibleLinesRange();
2670
2671 int hLine = GetLineHeight();
2672
2673 GetListCtrl()->Scroll(-1, top + dy / hLine);
2674
2675 #ifdef __WXMAC__
2676 // see comment in MoveToItem() for why we do this
2677 ResetVisibleLinesRange();
2678 #endif
2679
2680 return true;
2681 }
2682
2683 // ----------------------------------------------------------------------------
2684 // keyboard handling
2685 // ----------------------------------------------------------------------------
2686
2687 void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
2688 {
2689 wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
2690 wxT("invalid item index in OnArrowChar()") );
2691
2692 size_t oldCurrent = m_current;
2693
2694 // in single selection we just ignore Shift as we can't select several
2695 // items anyhow
2696 if ( event.ShiftDown() && !IsSingleSel() )
2697 {
2698 ChangeCurrent(newCurrent);
2699
2700 // refresh the old focus to remove it
2701 RefreshLine( oldCurrent );
2702
2703 // select all the items between the old and the new one
2704 if ( oldCurrent > newCurrent )
2705 {
2706 newCurrent = oldCurrent;
2707 oldCurrent = m_current;
2708 }
2709
2710 HighlightLines(oldCurrent, newCurrent);
2711 }
2712 else // !shift
2713 {
2714 // all previously selected items are unselected unless ctrl is held
2715 // in a multiselection control
2716 if ( !event.ControlDown() || IsSingleSel() )
2717 HighlightAll(false);
2718
2719 ChangeCurrent(newCurrent);
2720
2721 // refresh the old focus to remove it
2722 RefreshLine( oldCurrent );
2723
2724 // in single selection mode we must always have a selected item
2725 if ( !event.ControlDown() || IsSingleSel() )
2726 HighlightLine( m_current, true );
2727 }
2728
2729 RefreshLine( m_current );
2730
2731 MoveToFocus();
2732 }
2733
2734 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
2735 {
2736 wxWindow *parent = GetParent();
2737
2738 // propagate the key event upwards
2739 wxKeyEvent ke(event);
2740 ke.SetEventObject( parent );
2741 ke.SetId(GetParent()->GetId());
2742 if (parent->GetEventHandler()->ProcessEvent( ke ))
2743 return;
2744
2745 // send a list event
2746 wxListEvent le( wxEVT_LIST_KEY_DOWN, parent->GetId() );
2747 le.m_item.m_itemId =
2748 le.m_itemIndex = m_current;
2749 if (HasCurrent())
2750 GetLine(m_current)->GetItem( 0, le.m_item );
2751 le.m_code = event.GetKeyCode();
2752 le.SetEventObject( parent );
2753 if (parent->GetEventHandler()->ProcessEvent( le ))
2754 return;
2755
2756 event.Skip();
2757 }
2758
2759 void wxListMainWindow::OnKeyUp( wxKeyEvent &event )
2760 {
2761 wxWindow *parent = GetParent();
2762
2763 // propagate the key event upwards
2764 wxKeyEvent ke(event);
2765 ke.SetEventObject( parent );
2766 ke.SetId(GetParent()->GetId());
2767 if (parent->GetEventHandler()->ProcessEvent( ke ))
2768 return;
2769
2770 event.Skip();
2771 }
2772
2773 void wxListMainWindow::OnCharHook( wxKeyEvent &event )
2774 {
2775 if ( m_textctrlWrapper )
2776 {
2777 // When an in-place editor is active we should ensure that it always
2778 // gets the key events that are special to it.
2779 if ( m_textctrlWrapper->CheckForEndEditKey(event) )
2780 {
2781 // Skip the call to wxEvent::Skip() below.
2782 return;
2783 }
2784 }
2785
2786 event.Skip();
2787 }
2788
2789 void wxListMainWindow::OnChar( wxKeyEvent &event )
2790 {
2791 wxWindow *parent = GetParent();
2792
2793 // propagate the char event upwards
2794 wxKeyEvent ke(event);
2795 ke.SetEventObject( parent );
2796 ke.SetId(GetParent()->GetId());
2797 if (parent->GetEventHandler()->ProcessEvent( ke ))
2798 return;
2799
2800 if ( HandleAsNavigationKey(event) )
2801 return;
2802
2803 // no item -> nothing to do
2804 if (!HasCurrent())
2805 {
2806 event.Skip();
2807 return;
2808 }
2809
2810 // don't use m_linesPerPage directly as it might not be computed yet
2811 const int pageSize = GetCountPerPage();
2812 wxCHECK_RET( pageSize, wxT("should have non zero page size") );
2813
2814 if (GetLayoutDirection() == wxLayout_RightToLeft)
2815 {
2816 if (event.GetKeyCode() == WXK_RIGHT)
2817 event.m_keyCode = WXK_LEFT;
2818 else if (event.GetKeyCode() == WXK_LEFT)
2819 event.m_keyCode = WXK_RIGHT;
2820 }
2821
2822 int keyCode = event.GetKeyCode();
2823 switch ( keyCode )
2824 {
2825 case WXK_UP:
2826 if ( m_current > 0 )
2827 OnArrowChar( m_current - 1, event );
2828 break;
2829
2830 case WXK_DOWN:
2831 if ( m_current < (size_t)GetItemCount() - 1 )
2832 OnArrowChar( m_current + 1, event );
2833 break;
2834
2835 case WXK_END:
2836 if (!IsEmpty())
2837 OnArrowChar( GetItemCount() - 1, event );
2838 break;
2839
2840 case WXK_HOME:
2841 if (!IsEmpty())
2842 OnArrowChar( 0, event );
2843 break;
2844
2845 case WXK_PAGEUP:
2846 {
2847 int steps = InReportView() ? pageSize - 1
2848 : m_current % pageSize;
2849
2850 int index = m_current - steps;
2851 if (index < 0)
2852 index = 0;
2853
2854 OnArrowChar( index, event );
2855 }
2856 break;
2857
2858 case WXK_PAGEDOWN:
2859 {
2860 int steps = InReportView()
2861 ? pageSize - 1
2862 : pageSize - (m_current % pageSize) - 1;
2863
2864 size_t index = m_current + steps;
2865 size_t count = GetItemCount();
2866 if ( index >= count )
2867 index = count - 1;
2868
2869 OnArrowChar( index, event );
2870 }
2871 break;
2872
2873 case WXK_LEFT:
2874 if ( !InReportView() )
2875 {
2876 int index = m_current - pageSize;
2877 if (index < 0)
2878 index = 0;
2879
2880 OnArrowChar( index, event );
2881 }
2882 break;
2883
2884 case WXK_RIGHT:
2885 if ( !InReportView() )
2886 {
2887 size_t index = m_current + pageSize;
2888
2889 size_t count = GetItemCount();
2890 if ( index >= count )
2891 index = count - 1;
2892
2893 OnArrowChar( index, event );
2894 }
2895 break;
2896
2897 case WXK_SPACE:
2898 if ( IsSingleSel() )
2899 {
2900 if ( event.ControlDown() )
2901 {
2902 ReverseHighlight(m_current);
2903 }
2904 else // normal space press
2905 {
2906 SendNotify( m_current, wxEVT_LIST_ITEM_ACTIVATED );
2907 }
2908 }
2909 else // multiple selection
2910 {
2911 ReverseHighlight(m_current);
2912 }
2913 break;
2914
2915 case WXK_RETURN:
2916 case WXK_EXECUTE:
2917 SendNotify( m_current, wxEVT_LIST_ITEM_ACTIVATED );
2918 break;
2919
2920 default:
2921 if ( !event.HasModifiers() &&
2922 ((keyCode >= '0' && keyCode <= '9') ||
2923 (keyCode >= 'a' && keyCode <= 'z') ||
2924 (keyCode >= 'A' && keyCode <= 'Z') ||
2925 (keyCode == '_') ||
2926 (keyCode == '+') ||
2927 (keyCode == '*') ||
2928 (keyCode == '-')))
2929 {
2930 // find the next item starting with the given prefix
2931 wxChar ch = (wxChar)keyCode;
2932 size_t item;
2933
2934 // if the same character is typed multiple times then go to the
2935 // next entry starting with that character instead of searching
2936 // for an item starting with multiple copies of this character,
2937 // this is more useful and is how it works under Windows.
2938 if ( m_findPrefix.length() == 1 && m_findPrefix[0] == ch )
2939 {
2940 item = PrefixFindItem(m_current, ch);
2941 }
2942 else
2943 {
2944 const wxString newPrefix(m_findPrefix + ch);
2945 item = PrefixFindItem(m_current, newPrefix);
2946 if ( item != (size_t)-1 )
2947 m_findPrefix = newPrefix;
2948 }
2949
2950 // also start the timer to reset the current prefix if the user
2951 // doesn't press any more alnum keys soon -- we wouldn't want
2952 // to use this prefix for a new item search
2953 if ( !m_findTimer )
2954 {
2955 m_findTimer = new wxListFindTimer( this );
2956 }
2957
2958 // Notice that we should start the timer even if we didn't find
2959 // anything to make sure we reset the search state later.
2960 m_findTimer->Start(wxListFindTimer::DELAY, wxTIMER_ONE_SHOT);
2961
2962 // restart timer even when there's no match so bell get's reset
2963 if ( item != (size_t)-1 )
2964 {
2965 // Select the found item and go to it.
2966 HighlightAll(false);
2967 SetItemState(item,
2968 wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED,
2969 wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED);
2970
2971 // Reset the bell flag if it had been temporarily disabled
2972 // before.
2973 if ( m_findBell )
2974 m_findBell = 1;
2975 }
2976 else // No such item
2977 {
2978 // Signal it with a bell if enabled.
2979 if ( m_findBell == 1 )
2980 {
2981 ::wxBell();
2982
2983 // Disable it for the next unsuccessful match, we only
2984 // beep once, this is usually enough and continuing to
2985 // do it would be annoying.
2986 m_findBell = -1;
2987 }
2988 }
2989 }
2990 else
2991 {
2992 event.Skip();
2993 }
2994 }
2995 }
2996
2997 // ----------------------------------------------------------------------------
2998 // focus handling
2999 // ----------------------------------------------------------------------------
3000
3001 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
3002 {
3003 if ( GetParent() )
3004 {
3005 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
3006 event.SetEventObject( GetParent() );
3007 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3008 return;
3009 }
3010
3011 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3012 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3013 // which are already drawn correctly resulting in horrible flicker - avoid
3014 // it
3015 if ( !m_hasFocus )
3016 {
3017 m_hasFocus = true;
3018
3019 RefreshSelected();
3020 }
3021 }
3022
3023 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
3024 {
3025 if ( GetParent() )
3026 {
3027 wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
3028 event.SetEventObject( GetParent() );
3029 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3030 return;
3031 }
3032
3033 m_hasFocus = false;
3034 RefreshSelected();
3035 }
3036
3037 void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
3038 {
3039 if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
3040 {
3041 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3042 }
3043 else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
3044 {
3045 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3046 }
3047 else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
3048 {
3049 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3050 }
3051 else if ( InReportView() && (m_small_image_list))
3052 {
3053 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3054 }
3055 }
3056
3057 void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
3058 {
3059 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
3060 {
3061 m_normal_image_list->GetSize( index, width, height );
3062 }
3063 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
3064 {
3065 m_small_image_list->GetSize( index, width, height );
3066 }
3067 else if ( HasFlag(wxLC_LIST) && m_small_image_list )
3068 {
3069 m_small_image_list->GetSize( index, width, height );
3070 }
3071 else if ( InReportView() && m_small_image_list )
3072 {
3073 m_small_image_list->GetSize( index, width, height );
3074 }
3075 else
3076 {
3077 width =
3078 height = 0;
3079 }
3080 }
3081
3082 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
3083 {
3084 m_dirty = true;
3085
3086 // calc the spacing from the icon size
3087 int width = 0, height = 0;
3088
3089 if ((imageList) && (imageList->GetImageCount()) )
3090 imageList->GetSize(0, width, height);
3091
3092 if (which == wxIMAGE_LIST_NORMAL)
3093 {
3094 m_normal_image_list = imageList;
3095 m_normal_spacing = width + 8;
3096 }
3097
3098 if (which == wxIMAGE_LIST_SMALL)
3099 {
3100 m_small_image_list = imageList;
3101 m_small_spacing = width + 14;
3102 m_lineHeight = 0; // ensure that the line height will be recalc'd
3103 }
3104 }
3105
3106 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
3107 {
3108 m_dirty = true;
3109 if (isSmall)
3110 m_small_spacing = spacing;
3111 else
3112 m_normal_spacing = spacing;
3113 }
3114
3115 int wxListMainWindow::GetItemSpacing( bool isSmall )
3116 {
3117 return isSmall ? m_small_spacing : m_normal_spacing;
3118 }
3119
3120 // ----------------------------------------------------------------------------
3121 // columns
3122 // ----------------------------------------------------------------------------
3123
3124 int
3125 wxListMainWindow::ComputeMinHeaderWidth(const wxListHeaderData* column) const
3126 {
3127 wxClientDC dc(const_cast<wxListMainWindow*>(this));
3128
3129 int width = dc.GetTextExtent(column->GetText()).x + AUTOSIZE_COL_MARGIN;
3130
3131 width += 2*EXTRA_WIDTH;
3132
3133 // check for column header's image availability
3134 const int image = column->GetImage();
3135 if ( image != -1 )
3136 {
3137 if ( m_small_image_list )
3138 {
3139 int ix = 0, iy = 0;
3140 m_small_image_list->GetSize(image, ix, iy);
3141 width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
3142 }
3143 }
3144
3145 return width;
3146 }
3147
3148 void wxListMainWindow::SetColumn( int col, const wxListItem &item )
3149 {
3150 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3151
3152 wxCHECK_RET( node, wxT("invalid column index in SetColumn") );
3153
3154 wxListHeaderData *column = node->GetData();
3155 column->SetItem( item );
3156
3157 if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
3158 column->SetWidth(ComputeMinHeaderWidth(column));
3159
3160 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3161 if ( headerWin )
3162 headerWin->m_dirty = true;
3163
3164 m_dirty = true;
3165
3166 // invalidate it as it has to be recalculated
3167 m_headerWidth = 0;
3168 }
3169
3170 void wxListMainWindow::SetColumnWidth( int col, int width )
3171 {
3172 wxCHECK_RET( col >= 0 && col < GetColumnCount(),
3173 wxT("invalid column index") );
3174
3175 wxCHECK_RET( InReportView(),
3176 wxT("SetColumnWidth() can only be called in report mode.") );
3177
3178 m_dirty = true;
3179
3180 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3181 if ( headerWin )
3182 headerWin->m_dirty = true;
3183
3184 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3185 wxCHECK_RET( node, wxT("no column?") );
3186
3187 wxListHeaderData *column = node->GetData();
3188
3189 size_t count = GetItemCount();
3190
3191 if (width == wxLIST_AUTOSIZE_USEHEADER)
3192 {
3193 width = ComputeMinHeaderWidth(column);
3194 }
3195 else if ( width == wxLIST_AUTOSIZE )
3196 {
3197 width = ComputeMinHeaderWidth(column);
3198
3199 if ( !IsVirtual() )
3200 {
3201 wxClientDC dc(this);
3202 dc.SetFont( GetFont() );
3203
3204 int max = AUTOSIZE_COL_MARGIN;
3205
3206 // if the cached column width isn't valid then recalculate it
3207 if (m_aColWidths.Item(col)->bNeedsUpdate)
3208 {
3209 for (size_t i = 0; i < count; i++)
3210 {
3211 wxListLineData *line = GetLine( i );
3212 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3213
3214 wxCHECK_RET( n, wxT("no subitem?") );
3215
3216 wxListItemData *itemData = n->GetData();
3217 wxListItem item;
3218
3219 itemData->GetItem(item);
3220 int itemWidth = GetItemWidthWithImage(&item);
3221 if (itemWidth > max)
3222 max = itemWidth;
3223 }
3224
3225 m_aColWidths.Item(col)->bNeedsUpdate = false;
3226 m_aColWidths.Item(col)->nMaxWidth = max;
3227 }
3228
3229 max = m_aColWidths.Item(col)->nMaxWidth + AUTOSIZE_COL_MARGIN;
3230 if ( width < max )
3231 width = max;
3232 }
3233 }
3234
3235 column->SetWidth( width );
3236
3237 // invalidate it as it has to be recalculated
3238 m_headerWidth = 0;
3239 }
3240
3241 int wxListMainWindow::GetHeaderWidth() const
3242 {
3243 if ( !m_headerWidth )
3244 {
3245 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
3246
3247 size_t count = GetColumnCount();
3248 for ( size_t col = 0; col < count; col++ )
3249 {
3250 self->m_headerWidth += GetColumnWidth(col);
3251 }
3252 }
3253
3254 return m_headerWidth;
3255 }
3256
3257 void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
3258 {
3259 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3260 wxCHECK_RET( node, wxT("invalid column index in GetColumn") );
3261
3262 wxListHeaderData *column = node->GetData();
3263 column->GetItem( item );
3264 }
3265
3266 int wxListMainWindow::GetColumnWidth( int col ) const
3267 {
3268 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3269 wxCHECK_MSG( node, 0, wxT("invalid column index") );
3270
3271 wxListHeaderData *column = node->GetData();
3272 return column->GetWidth();
3273 }
3274
3275 // ----------------------------------------------------------------------------
3276 // item state
3277 // ----------------------------------------------------------------------------
3278
3279 void wxListMainWindow::SetItem( wxListItem &item )
3280 {
3281 long id = item.m_itemId;
3282 wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
3283 wxT("invalid item index in SetItem") );
3284
3285 if ( !IsVirtual() )
3286 {
3287 wxListLineData *line = GetLine((size_t)id);
3288 line->SetItem( item.m_col, item );
3289
3290 // Set item state if user wants
3291 if ( item.m_mask & wxLIST_MASK_STATE )
3292 SetItemState( item.m_itemId, item.m_state, item.m_state );
3293
3294 if (InReportView())
3295 {
3296 // update the Max Width Cache if needed
3297 int width = GetItemWidthWithImage(&item);
3298
3299 if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
3300 m_aColWidths.Item(item.m_col)->nMaxWidth = width;
3301 }
3302 }
3303
3304 // update the item on screen unless we're going to update everything soon
3305 // anyhow
3306 if ( !m_dirty )
3307 {
3308 wxRect rectItem;
3309 GetItemRect(id, rectItem);
3310 RefreshRect(rectItem);
3311 }
3312 }
3313
3314 void wxListMainWindow::SetItemStateAll(long state, long stateMask)
3315 {
3316 if ( IsEmpty() )
3317 return;
3318
3319 // first deal with selection
3320 if ( stateMask & wxLIST_STATE_SELECTED )
3321 {
3322 // set/clear select state
3323 if ( IsVirtual() )
3324 {
3325 // optimized version for virtual listctrl.
3326 m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
3327 Refresh();
3328 }
3329 else if ( state & wxLIST_STATE_SELECTED )
3330 {
3331 const long count = GetItemCount();
3332 for( long i = 0; i < count; i++ )
3333 {
3334 SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
3335 }
3336
3337 }
3338 else
3339 {
3340 // clear for non virtual (somewhat optimized by using GetNextItem())
3341 long i = -1;
3342 while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
3343 {
3344 SetItemState( i, 0, wxLIST_STATE_SELECTED );
3345 }
3346 }
3347 }
3348
3349 if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
3350 {
3351 // unfocus all: only one item can be focussed, so clearing focus for
3352 // all items is simply clearing focus of the focussed item.
3353 SetItemState(m_current, state, stateMask);
3354 }
3355 //(setting focus to all items makes no sense, so it is not handled here.)
3356 }
3357
3358 void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
3359 {
3360 if ( litem == -1 )
3361 {
3362 SetItemStateAll(state, stateMask);
3363 return;
3364 }
3365
3366 wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
3367 wxT("invalid list ctrl item index in SetItem") );
3368
3369 size_t oldCurrent = m_current;
3370 size_t item = (size_t)litem; // safe because of the check above
3371
3372 // do we need to change the focus?
3373 if ( stateMask & wxLIST_STATE_FOCUSED )
3374 {
3375 if ( state & wxLIST_STATE_FOCUSED )
3376 {
3377 // don't do anything if this item is already focused
3378 if ( item != m_current )
3379 {
3380 ChangeCurrent(item);
3381
3382 if ( oldCurrent != (size_t)-1 )
3383 {
3384 if ( IsSingleSel() )
3385 {
3386 HighlightLine(oldCurrent, false);
3387 }
3388
3389 RefreshLine(oldCurrent);
3390 }
3391
3392 RefreshLine( m_current );
3393 }
3394 }
3395 else // unfocus
3396 {
3397 // don't do anything if this item is not focused
3398 if ( item == m_current )
3399 {
3400 ResetCurrent();
3401
3402 if ( IsSingleSel() )
3403 {
3404 // we must unselect the old current item as well or we
3405 // might end up with more than one selected item in a
3406 // single selection control
3407 HighlightLine(oldCurrent, false);
3408 }
3409
3410 RefreshLine( oldCurrent );
3411 }
3412 }
3413 }
3414
3415 // do we need to change the selection state?
3416 if ( stateMask & wxLIST_STATE_SELECTED )
3417 {
3418 bool on = (state & wxLIST_STATE_SELECTED) != 0;
3419
3420 if ( IsSingleSel() )
3421 {
3422 if ( on )
3423 {
3424 // selecting the item also makes it the focused one in the
3425 // single sel mode
3426 if ( m_current != item )
3427 {
3428 ChangeCurrent(item);
3429
3430 if ( oldCurrent != (size_t)-1 )
3431 {
3432 HighlightLine( oldCurrent, false );
3433 RefreshLine( oldCurrent );
3434 }
3435 }
3436 }
3437 else // off
3438 {
3439 // only the current item may be selected anyhow
3440 if ( item != m_current )
3441 return;
3442 }
3443 }
3444
3445 if ( HighlightLine(item, on) )
3446 {
3447 RefreshLine(item);
3448 }
3449 }
3450 }
3451
3452 int wxListMainWindow::GetItemState( long item, long stateMask ) const
3453 {
3454 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
3455 wxT("invalid list ctrl item index in GetItemState()") );
3456
3457 int ret = wxLIST_STATE_DONTCARE;
3458
3459 if ( stateMask & wxLIST_STATE_FOCUSED )
3460 {
3461 if ( (size_t)item == m_current )
3462 ret |= wxLIST_STATE_FOCUSED;
3463 }
3464
3465 if ( stateMask & wxLIST_STATE_SELECTED )
3466 {
3467 if ( IsHighlighted(item) )
3468 ret |= wxLIST_STATE_SELECTED;
3469 }
3470
3471 return ret;
3472 }
3473
3474 void wxListMainWindow::GetItem( wxListItem &item ) const
3475 {
3476 wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
3477 wxT("invalid item index in GetItem") );
3478
3479 wxListLineData *line = GetLine((size_t)item.m_itemId);
3480 line->GetItem( item.m_col, item );
3481
3482 // Get item state if user wants it
3483 if ( item.m_mask & wxLIST_MASK_STATE )
3484 item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
3485 wxLIST_STATE_FOCUSED );
3486 }
3487
3488 // ----------------------------------------------------------------------------
3489 // item count
3490 // ----------------------------------------------------------------------------
3491
3492 size_t wxListMainWindow::GetItemCount() const
3493 {
3494 return IsVirtual() ? m_countVirt : m_lines.GetCount();
3495 }
3496
3497 void wxListMainWindow::SetItemCount(long count)
3498 {
3499 m_selStore.SetItemCount(count);
3500 m_countVirt = count;
3501
3502 ResetVisibleLinesRange();
3503
3504 // scrollbars must be reset
3505 m_dirty = true;
3506 }
3507
3508 int wxListMainWindow::GetSelectedItemCount() const
3509 {
3510 // deal with the quick case first
3511 if ( IsSingleSel() )
3512 return HasCurrent() ? IsHighlighted(m_current) : false;
3513
3514 // virtual controls remmebers all its selections itself
3515 if ( IsVirtual() )
3516 return m_selStore.GetSelectedCount();
3517
3518 // TODO: we probably should maintain the number of items selected even for
3519 // non virtual controls as enumerating all lines is really slow...
3520 size_t countSel = 0;
3521 size_t count = GetItemCount();
3522 for ( size_t line = 0; line < count; line++ )
3523 {
3524 if ( GetLine(line)->IsHighlighted() )
3525 countSel++;
3526 }
3527
3528 return countSel;
3529 }
3530
3531 // ----------------------------------------------------------------------------
3532 // item position/size
3533 // ----------------------------------------------------------------------------
3534
3535 wxRect wxListMainWindow::GetViewRect() const
3536 {
3537 wxASSERT_MSG( !HasFlag(wxLC_LIST), "not implemented for list view" );
3538
3539 // we need to find the longest/tallest label
3540 wxCoord xMax = 0, yMax = 0;
3541 const int count = GetItemCount();
3542 if ( count )
3543 {
3544 for ( int i = 0; i < count; i++ )
3545 {
3546 // we need logical, not physical, coordinates here, so use
3547 // GetLineRect() instead of GetItemRect()
3548 wxRect r = GetLineRect(i);
3549
3550 wxCoord x = r.GetRight(),
3551 y = r.GetBottom();
3552
3553 if ( x > xMax )
3554 xMax = x;
3555 if ( y > yMax )
3556 yMax = y;
3557 }
3558 }
3559
3560 // some fudge needed to make it look prettier
3561 xMax += 2 * EXTRA_BORDER_X;
3562 yMax += 2 * EXTRA_BORDER_Y;
3563
3564 // account for the scrollbars if necessary
3565 const wxSize sizeAll = GetClientSize();
3566 if ( xMax > sizeAll.x )
3567 yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
3568 if ( yMax > sizeAll.y )
3569 xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
3570
3571 return wxRect(0, 0, xMax, yMax);
3572 }
3573
3574 bool
3575 wxListMainWindow::GetSubItemRect(long item, long subItem, wxRect& rect) const
3576 {
3577 wxCHECK_MSG( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM || InReportView(),
3578 false,
3579 wxT("GetSubItemRect only meaningful in report view") );
3580 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), false,
3581 wxT("invalid item in GetSubItemRect") );
3582
3583 // ensure that we're laid out, otherwise we could return nonsense
3584 if ( m_dirty )
3585 {
3586 wxConstCast(this, wxListMainWindow)->
3587 RecalculatePositions(true /* no refresh */);
3588 }
3589
3590 rect = GetLineRect((size_t)item);
3591
3592 // Adjust rect to specified column
3593 if ( subItem != wxLIST_GETSUBITEMRECT_WHOLEITEM )
3594 {
3595 wxCHECK_MSG( subItem >= 0 && subItem < GetColumnCount(), false,
3596 wxT("invalid subItem in GetSubItemRect") );
3597
3598 for (int i = 0; i < subItem; i++)
3599 {
3600 rect.x += GetColumnWidth(i);
3601 }
3602 rect.width = GetColumnWidth(subItem);
3603 }
3604
3605 GetListCtrl()->CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
3606
3607 return true;
3608 }
3609
3610 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
3611 {
3612 wxRect rect;
3613 GetItemRect(item, rect);
3614
3615 pos.x = rect.x;
3616 pos.y = rect.y;
3617
3618 return true;
3619 }
3620
3621 // ----------------------------------------------------------------------------
3622 // geometry calculation
3623 // ----------------------------------------------------------------------------
3624
3625 void wxListMainWindow::RecalculatePositions(bool noRefresh)
3626 {
3627 const int lineHeight = GetLineHeight();
3628
3629 wxClientDC dc( this );
3630 dc.SetFont( GetFont() );
3631
3632 const size_t count = GetItemCount();
3633
3634 int iconSpacing;
3635 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
3636 iconSpacing = m_normal_spacing;
3637 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
3638 iconSpacing = m_small_spacing;
3639 else
3640 iconSpacing = 0;
3641
3642 // Note that we do not call GetClientSize() here but
3643 // GetSize() and subtract the border size for sunken
3644 // borders manually. This is technically incorrect,
3645 // but we need to know the client area's size WITHOUT
3646 // scrollbars here. Since we don't know if there are
3647 // any scrollbars, we use GetSize() instead. Another
3648 // solution would be to call SetScrollbars() here to
3649 // remove the scrollbars and call GetClientSize() then,
3650 // but this might result in flicker and - worse - will
3651 // reset the scrollbars to 0 which is not good at all
3652 // if you resize a dialog/window, but don't want to
3653 // reset the window scrolling. RR.
3654 // Furthermore, we actually do NOT subtract the border
3655 // width as 2 pixels is just the extra space which we
3656 // need around the actual content in the window. Other-
3657 // wise the text would e.g. touch the upper border. RR.
3658 int clientWidth,
3659 clientHeight;
3660 GetSize( &clientWidth, &clientHeight );
3661
3662 if ( InReportView() )
3663 {
3664 // all lines have the same height and we scroll one line per step
3665 int entireHeight = count * lineHeight + LINE_SPACING;
3666
3667 m_linesPerPage = clientHeight / lineHeight;
3668
3669 ResetVisibleLinesRange();
3670
3671 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X, lineHeight,
3672 GetHeaderWidth() / SCROLL_UNIT_X,
3673 (entireHeight + lineHeight - 1) / lineHeight,
3674 GetListCtrl()->GetScrollPos(wxHORIZONTAL),
3675 GetListCtrl()->GetScrollPos(wxVERTICAL),
3676 true );
3677 }
3678 else // !report
3679 {
3680 // we have 3 different layout strategies: either layout all items
3681 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3682 // to arrange them in top to bottom, left to right (don't ask me why
3683 // not the other way round...) order
3684 if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) )
3685 {
3686 int x = EXTRA_BORDER_X;
3687 int y = EXTRA_BORDER_Y;
3688
3689 wxCoord widthMax = 0;
3690
3691 size_t i;
3692 for ( i = 0; i < count; i++ )
3693 {
3694 wxListLineData *line = GetLine(i);
3695 line->CalculateSize( &dc, iconSpacing );
3696 line->SetPosition( x, y, iconSpacing );
3697
3698 wxSize sizeLine = GetLineSize(i);
3699
3700 if ( HasFlag(wxLC_ALIGN_TOP) )
3701 {
3702 if ( sizeLine.x > widthMax )
3703 widthMax = sizeLine.x;
3704
3705 y += sizeLine.y;
3706 }
3707 else // wxLC_ALIGN_LEFT
3708 {
3709 x += sizeLine.x + MARGIN_BETWEEN_ROWS;
3710 }
3711 }
3712
3713 if ( HasFlag(wxLC_ALIGN_TOP) )
3714 {
3715 // traverse the items again and tweak their sizes so that they are
3716 // all the same in a row
3717 for ( i = 0; i < count; i++ )
3718 {
3719 wxListLineData *line = GetLine(i);
3720 line->m_gi->ExtendWidth(widthMax);
3721 }
3722 }
3723
3724 GetListCtrl()->SetScrollbars
3725 (
3726 SCROLL_UNIT_X,
3727 lineHeight,
3728 (x + SCROLL_UNIT_X) / SCROLL_UNIT_X,
3729 (y + lineHeight) / lineHeight,
3730 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
3731 GetListCtrl()->GetScrollPos( wxVERTICAL ),
3732 true
3733 );
3734 }
3735 else // "flowed" arrangement, the most complicated case
3736 {
3737 // at first we try without any scrollbars, if the items don't fit into
3738 // the window, we recalculate after subtracting the space taken by the
3739 // scrollbar
3740
3741 int entireWidth = 0;
3742
3743 for (int tries = 0; tries < 2; tries++)
3744 {
3745 entireWidth = 2 * EXTRA_BORDER_X;
3746
3747 if (tries == 1)
3748 {
3749 // Now we have decided that the items do not fit into the
3750 // client area, so we need a scrollbar
3751 entireWidth += SCROLL_UNIT_X;
3752 }
3753
3754 int x = EXTRA_BORDER_X;
3755 int y = EXTRA_BORDER_Y;
3756
3757 // Note that "row" here is vertical, i.e. what is called
3758 // "column" in many other places in wxWidgets.
3759 int maxWidthInThisRow = 0;
3760
3761 m_linesPerPage = 0;
3762 int currentlyVisibleLines = 0;
3763
3764 for (size_t i = 0; i < count; i++)
3765 {
3766 currentlyVisibleLines++;
3767 wxListLineData *line = GetLine( i );
3768 line->CalculateSize( &dc, iconSpacing );
3769 line->SetPosition( x, y, iconSpacing );
3770
3771 wxSize sizeLine = GetLineSize( i );
3772
3773 if ( maxWidthInThisRow < sizeLine.x )
3774 maxWidthInThisRow = sizeLine.x;
3775
3776 y += sizeLine.y;
3777 if (currentlyVisibleLines > m_linesPerPage)
3778 m_linesPerPage = currentlyVisibleLines;
3779
3780 // Have we reached the end of the row either because no
3781 // more items would fit or because there are simply no more
3782 // items?
3783 if ( y + sizeLine.y >= clientHeight
3784 || i == count - 1)
3785 {
3786 // Adjust all items in this row to have the same
3787 // width to ensure that they all align horizontally in
3788 // icon view.
3789 if ( HasFlag(wxLC_ICON) || HasFlag(wxLC_SMALL_ICON) )
3790 {
3791 size_t firstRowLine = i - currentlyVisibleLines + 1;
3792 for (size_t j = firstRowLine; j <= i; j++)
3793 {
3794 GetLine(j)->m_gi->ExtendWidth(maxWidthInThisRow);
3795 }
3796 }
3797
3798 currentlyVisibleLines = 0;
3799 y = EXTRA_BORDER_Y;
3800 maxWidthInThisRow += MARGIN_BETWEEN_ROWS;
3801 x += maxWidthInThisRow;
3802 entireWidth += maxWidthInThisRow;
3803 maxWidthInThisRow = 0;
3804 }
3805
3806 if ( (tries == 0) &&
3807 (entireWidth + SCROLL_UNIT_X > clientWidth) )
3808 {
3809 clientHeight -= wxSystemSettings::
3810 GetMetric(wxSYS_HSCROLL_Y);
3811 m_linesPerPage = 0;
3812 break;
3813 }
3814
3815 if ( i == count - 1 )
3816 tries = 1; // Everything fits, no second try required.
3817 }
3818 }
3819
3820 GetListCtrl()->SetScrollbars
3821 (
3822 SCROLL_UNIT_X,
3823 lineHeight,
3824 (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X,
3825 0,
3826 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
3827 0,
3828 true
3829 );
3830 }
3831 }
3832
3833 if ( !noRefresh )
3834 {
3835 // FIXME: why should we call it from here?
3836 UpdateCurrent();
3837
3838 RefreshAll();
3839 }
3840 }
3841
3842 void wxListMainWindow::RefreshAll()
3843 {
3844 m_dirty = false;
3845 Refresh();
3846
3847 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3848 if ( headerWin && headerWin->m_dirty )
3849 {
3850 headerWin->m_dirty = false;
3851 headerWin->Refresh();
3852 }
3853 }
3854
3855 void wxListMainWindow::UpdateCurrent()
3856 {
3857 if ( !HasCurrent() && !IsEmpty() )
3858 ChangeCurrent(0);
3859 }
3860
3861 long wxListMainWindow::GetNextItem( long item,
3862 int WXUNUSED(geometry),
3863 int state ) const
3864 {
3865 long ret = item,
3866 max = GetItemCount();
3867 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
3868 wxT("invalid listctrl index in GetNextItem()") );
3869
3870 // notice that we start with the next item (or the first one if item == -1)
3871 // and this is intentional to allow writing a simple loop to iterate over
3872 // all selected items
3873 ret++;
3874 if ( ret == max )
3875 // this is not an error because the index was OK initially,
3876 // just no such item
3877 return -1;
3878
3879 if ( !state )
3880 // any will do
3881 return (size_t)ret;
3882
3883 size_t count = GetItemCount();
3884 for ( size_t line = (size_t)ret; line < count; line++ )
3885 {
3886 if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
3887 return line;
3888
3889 if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
3890 return line;
3891 }
3892
3893 return -1;
3894 }
3895
3896 // ----------------------------------------------------------------------------
3897 // deleting stuff
3898 // ----------------------------------------------------------------------------
3899
3900 void wxListMainWindow::DeleteItem( long lindex )
3901 {
3902 size_t count = GetItemCount();
3903
3904 wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
3905 wxT("invalid item index in DeleteItem") );
3906
3907 size_t index = (size_t)lindex;
3908
3909 // we don't need to adjust the index for the previous items
3910 if ( HasCurrent() && m_current >= index )
3911 {
3912 // if the current item is being deleted, we want the next one to
3913 // become selected - unless there is no next one - so don't adjust
3914 // m_current in this case
3915 if ( m_current != index || m_current == count - 1 )
3916 m_current--;
3917 }
3918
3919 if ( InReportView() )
3920 {
3921 // mark the Column Max Width cache as dirty if the items in the line
3922 // we're deleting contain the Max Column Width
3923 wxListLineData * const line = GetLine(index);
3924 wxListItemDataList::compatibility_iterator n;
3925 wxListItemData *itemData;
3926 wxListItem item;
3927 int itemWidth;
3928
3929 for (size_t i = 0; i < m_columns.GetCount(); i++)
3930 {
3931 n = line->m_items.Item( i );
3932 itemData = n->GetData();
3933 itemData->GetItem(item);
3934
3935 itemWidth = GetItemWidthWithImage(&item);
3936
3937 if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth)
3938 m_aColWidths.Item(i)->bNeedsUpdate = true;
3939 }
3940
3941 ResetVisibleLinesRange();
3942 }
3943
3944 SendNotify( index, wxEVT_LIST_DELETE_ITEM, wxDefaultPosition );
3945
3946 if ( IsVirtual() )
3947 {
3948 m_countVirt--;
3949 m_selStore.OnItemDelete(index);
3950 }
3951 else
3952 {
3953 m_lines.RemoveAt( index );
3954 }
3955
3956 // we need to refresh the (vert) scrollbar as the number of items changed
3957 m_dirty = true;
3958
3959 RefreshAfter(index);
3960 }
3961
3962 void wxListMainWindow::DeleteColumn( int col )
3963 {
3964 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3965
3966 wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
3967
3968 m_dirty = true;
3969 delete node->GetData();
3970 m_columns.Erase( node );
3971
3972 if ( !IsVirtual() )
3973 {
3974 // update all the items
3975 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
3976 {
3977 wxListLineData * const line = GetLine(i);
3978
3979 // In the following atypical but possible scenario it can be
3980 // legal to call DeleteColumn() but the items may not have any
3981 // values for it:
3982 // 1. In report view, insert a second column.
3983 // 2. Still in report view, add an item with 2 values.
3984 // 3. Switch to an icon (or list) view.
3985 // 4. Add an item -- necessarily with 1 value only.
3986 // 5. Switch back to report view.
3987 // 6. Call DeleteColumn().
3988 // So we need to check for this as otherwise we would simply crash
3989 // if this happens.
3990 if ( line->m_items.GetCount() <= static_cast<unsigned>(col) )
3991 continue;
3992
3993 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3994 delete n->GetData();
3995 line->m_items.Erase(n);
3996 }
3997 }
3998
3999 if ( InReportView() ) // we only cache max widths when in Report View
4000 {
4001 delete m_aColWidths.Item(col);
4002 m_aColWidths.RemoveAt(col);
4003 }
4004
4005 // invalidate it as it has to be recalculated
4006 m_headerWidth = 0;
4007 }
4008
4009 void wxListMainWindow::DoDeleteAllItems()
4010 {
4011 if ( IsEmpty() )
4012 // nothing to do - in particular, don't send the event
4013 return;
4014
4015 ResetCurrent();
4016
4017 // to make the deletion of all items faster, we don't send the
4018 // notifications for each item deletion in this case but only one event
4019 // for all of them: this is compatible with wxMSW and documented in
4020 // DeleteAllItems() description
4021
4022 wxListEvent event( wxEVT_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
4023 event.SetEventObject( GetParent() );
4024 GetParent()->GetEventHandler()->ProcessEvent( event );
4025
4026 if ( IsVirtual() )
4027 {
4028 m_countVirt = 0;
4029 m_selStore.Clear();
4030 }
4031
4032 if ( InReportView() )
4033 {
4034 ResetVisibleLinesRange();
4035 for (size_t i = 0; i < m_aColWidths.GetCount(); i++)
4036 {
4037 m_aColWidths.Item(i)->bNeedsUpdate = true;
4038 }
4039 }
4040
4041 m_lines.Clear();
4042 }
4043
4044 void wxListMainWindow::DeleteAllItems()
4045 {
4046 DoDeleteAllItems();
4047
4048 RecalculatePositions();
4049 }
4050
4051 void wxListMainWindow::DeleteEverything()
4052 {
4053 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
4054 WX_CLEAR_ARRAY(m_aColWidths);
4055
4056 DeleteAllItems();
4057 }
4058
4059 // ----------------------------------------------------------------------------
4060 // scanning for an item
4061 // ----------------------------------------------------------------------------
4062
4063 void wxListMainWindow::EnsureVisible( long index )
4064 {
4065 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4066 wxT("invalid index in EnsureVisible") );
4067
4068 // We have to call this here because the label in question might just have
4069 // been added and its position is not known yet
4070 if ( m_dirty )
4071 RecalculatePositions(true /* no refresh */);
4072
4073 MoveToItem((size_t)index);
4074 }
4075
4076 long wxListMainWindow::FindItem(long start, const wxString& str, bool partial )
4077 {
4078 if (str.empty())
4079 return wxNOT_FOUND;
4080
4081 long pos = start;
4082 wxString str_upper = str.Upper();
4083 if (pos < 0)
4084 pos = 0;
4085
4086 size_t count = GetItemCount();
4087 for ( size_t i = (size_t)pos; i < count; i++ )
4088 {
4089 wxListLineData *line = GetLine(i);
4090 wxString line_upper = line->GetText(0).Upper();
4091 if (!partial)
4092 {
4093 if (line_upper == str_upper )
4094 return i;
4095 }
4096 else
4097 {
4098 if (line_upper.find(str_upper) == 0)
4099 return i;
4100 }
4101 }
4102
4103 return wxNOT_FOUND;
4104 }
4105
4106 long wxListMainWindow::FindItem(long start, wxUIntPtr data)
4107 {
4108 long pos = start;
4109 if (pos < 0)
4110 pos = 0;
4111
4112 size_t count = GetItemCount();
4113 for (size_t i = (size_t)pos; i < count; i++)
4114 {
4115 wxListLineData *line = GetLine(i);
4116 wxListItem item;
4117 line->GetItem( 0, item );
4118 if (item.m_data == data)
4119 return i;
4120 }
4121
4122 return wxNOT_FOUND;
4123 }
4124
4125 long wxListMainWindow::FindItem( const wxPoint& pt )
4126 {
4127 size_t topItem;
4128 GetVisibleLinesRange( &topItem, NULL );
4129
4130 wxPoint p;
4131 GetItemPosition( GetItemCount() - 1, p );
4132 if ( p.y == 0 )
4133 return topItem;
4134
4135 long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem );
4136 if ( id >= 0 && id < (long)GetItemCount() )
4137 return id;
4138
4139 return wxNOT_FOUND;
4140 }
4141
4142 long wxListMainWindow::HitTest( int x, int y, int &flags ) const
4143 {
4144 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
4145
4146 size_t count = GetItemCount();
4147
4148 if ( InReportView() )
4149 {
4150 size_t current = y / GetLineHeight();
4151 if ( current < count )
4152 {
4153 flags = HitTestLine(current, x, y);
4154 if ( flags )
4155 return current;
4156 }
4157 }
4158 else // !report
4159 {
4160 // TODO: optimize it too! this is less simple than for report view but
4161 // enumerating all items is still not a way to do it!!
4162 for ( size_t current = 0; current < count; current++ )
4163 {
4164 flags = HitTestLine(current, x, y);
4165 if ( flags )
4166 return current;
4167 }
4168 }
4169
4170 return wxNOT_FOUND;
4171 }
4172
4173 // ----------------------------------------------------------------------------
4174 // adding stuff
4175 // ----------------------------------------------------------------------------
4176
4177 void wxListMainWindow::InsertItem( wxListItem &item )
4178 {
4179 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
4180
4181 int count = GetItemCount();
4182 wxCHECK_RET( item.m_itemId >= 0, wxT("invalid item index") );
4183
4184 if (item.m_itemId > count)
4185 item.m_itemId = count;
4186
4187 size_t id = item.m_itemId;
4188
4189 m_dirty = true;
4190
4191 if ( InReportView() )
4192 {
4193 ResetVisibleLinesRange();
4194
4195 const unsigned col = item.GetColumn();
4196 wxCHECK_RET( col < m_aColWidths.size(), "invalid item column" );
4197
4198 // calculate the width of the item and adjust the max column width
4199 wxColWidthInfo *pWidthInfo = m_aColWidths.Item(col);
4200 int width = GetItemWidthWithImage(&item);
4201 item.SetWidth(width);
4202 if (width > pWidthInfo->nMaxWidth)
4203 pWidthInfo->nMaxWidth = width;
4204 }
4205
4206 wxListLineData *line = new wxListLineData(this);
4207
4208 line->SetItem( item.m_col, item );
4209 if ( item.m_mask & wxLIST_MASK_IMAGE )
4210 {
4211 // Reset the buffered height if it's not big enough for the new image.
4212 int image = item.GetImage();
4213 if ( m_small_image_list && image != -1 && InReportView() )
4214 {
4215 int imageWidth, imageHeight;
4216 m_small_image_list->GetSize(image, imageWidth, imageHeight);
4217
4218 if ( imageHeight > m_lineHeight )
4219 m_lineHeight = 0;
4220 }
4221 }
4222
4223 m_lines.Insert( line, id );
4224
4225 m_dirty = true;
4226
4227 // If an item is selected at or below the point of insertion, we need to
4228 // increment the member variables because the current row's index has gone
4229 // up by one
4230 if ( HasCurrent() && m_current >= id )
4231 m_current++;
4232
4233 SendNotify(id, wxEVT_LIST_INSERT_ITEM);
4234
4235 RefreshLines(id, GetItemCount() - 1);
4236 }
4237
4238 long wxListMainWindow::InsertColumn( long col, const wxListItem &item )
4239 {
4240 long idx = -1;
4241
4242 m_dirty = true;
4243 if ( InReportView() )
4244 {
4245 wxListHeaderData *column = new wxListHeaderData( item );
4246 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
4247 column->SetWidth(ComputeMinHeaderWidth(column));
4248
4249 wxColWidthInfo *colWidthInfo = new wxColWidthInfo();
4250
4251 bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4252 if ( insert )
4253 {
4254 wxListHeaderDataList::compatibility_iterator
4255 node = m_columns.Item( col );
4256 m_columns.Insert( node, column );
4257 m_aColWidths.Insert( colWidthInfo, col );
4258 idx = col;
4259 }
4260 else
4261 {
4262 idx = m_aColWidths.GetCount();
4263 m_columns.Append( column );
4264 m_aColWidths.Add( colWidthInfo );
4265 }
4266
4267 if ( !IsVirtual() )
4268 {
4269 // update all the items
4270 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4271 {
4272 wxListLineData * const line = GetLine(i);
4273 wxListItemData * const data = new wxListItemData(this);
4274 if ( insert )
4275 line->m_items.Insert(col, data);
4276 else
4277 line->m_items.Append(data);
4278 }
4279 }
4280
4281 // invalidate it as it has to be recalculated
4282 m_headerWidth = 0;
4283 }
4284 return idx;
4285 }
4286
4287 int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
4288 {
4289 int width = 0;
4290 wxClientDC dc(this);
4291
4292 dc.SetFont( GetFont() );
4293
4294 if (item->GetImage() != -1)
4295 {
4296 int ix, iy;
4297 GetImageSize( item->GetImage(), ix, iy );
4298 width += ix + 5;
4299 }
4300
4301 if (!item->GetText().empty())
4302 {
4303 wxCoord w;
4304 dc.GetTextExtent( item->GetText(), &w, NULL );
4305 width += w;
4306 }
4307
4308 return width;
4309 }
4310
4311 // ----------------------------------------------------------------------------
4312 // sorting
4313 // ----------------------------------------------------------------------------
4314
4315 static wxListCtrlCompare list_ctrl_compare_func_2;
4316 static wxIntPtr list_ctrl_compare_data;
4317
4318 int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
4319 {
4320 wxListLineData *line1 = *arg1;
4321 wxListLineData *line2 = *arg2;
4322 wxListItem item;
4323 line1->GetItem( 0, item );
4324 wxUIntPtr data1 = item.m_data;
4325 line2->GetItem( 0, item );
4326 wxUIntPtr data2 = item.m_data;
4327 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
4328 }
4329
4330 void wxListMainWindow::SortItems( wxListCtrlCompare fn, wxIntPtr data )
4331 {
4332 // selections won't make sense any more after sorting the items so reset
4333 // them
4334 HighlightAll(false);
4335 ResetCurrent();
4336
4337 list_ctrl_compare_func_2 = fn;
4338 list_ctrl_compare_data = data;
4339 m_lines.Sort( list_ctrl_compare_func_1 );
4340 m_dirty = true;
4341 }
4342
4343 // ----------------------------------------------------------------------------
4344 // scrolling
4345 // ----------------------------------------------------------------------------
4346
4347 void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4348 {
4349 // update our idea of which lines are shown when we redraw the window the
4350 // next time
4351 ResetVisibleLinesRange();
4352
4353 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4354 {
4355 wxGenericListCtrl* lc = GetListCtrl();
4356 wxCHECK_RET( lc, wxT("no listctrl window?") );
4357
4358 if (lc->m_headerWin) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4359 {
4360 lc->m_headerWin->Refresh();
4361 lc->m_headerWin->Update();
4362 }
4363 }
4364 }
4365
4366 int wxListMainWindow::GetCountPerPage() const
4367 {
4368 if ( !m_linesPerPage )
4369 {
4370 wxConstCast(this, wxListMainWindow)->
4371 m_linesPerPage = GetClientSize().y / GetLineHeight();
4372 }
4373
4374 return m_linesPerPage;
4375 }
4376
4377 void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
4378 {
4379 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
4380
4381 if ( m_lineFrom == (size_t)-1 )
4382 {
4383 size_t count = GetItemCount();
4384 if ( count )
4385 {
4386 m_lineFrom = GetListCtrl()->GetScrollPos(wxVERTICAL);
4387
4388 // this may happen if SetScrollbars() hadn't been called yet
4389 if ( m_lineFrom >= count )
4390 m_lineFrom = count - 1;
4391
4392 // we redraw one extra line but this is needed to make the redrawing
4393 // logic work when there is a fractional number of lines on screen
4394 m_lineTo = m_lineFrom + m_linesPerPage;
4395 if ( m_lineTo >= count )
4396 m_lineTo = count - 1;
4397 }
4398 else // empty control
4399 {
4400 m_lineFrom = 0;
4401 m_lineTo = (size_t)-1;
4402 }
4403 }
4404
4405 wxASSERT_MSG( IsEmpty() ||
4406 (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
4407 wxT("GetVisibleLinesRange() returns incorrect result") );
4408
4409 if ( from )
4410 *from = m_lineFrom;
4411 if ( to )
4412 *to = m_lineTo;
4413 }
4414
4415 size_t
4416 wxListMainWindow::PrefixFindItem(size_t idParent,
4417 const wxString& prefixOrig) const
4418 {
4419 // if no items then just return
4420 if ( idParent == (size_t)-1 )
4421 return idParent;
4422
4423 // match is case insensitive as this is more convenient to the user: having
4424 // to press Shift-letter to go to the item starting with a capital letter
4425 // would be too bothersome
4426 wxString prefix = prefixOrig.Lower();
4427
4428 // determine the starting point: we shouldn't take the current item (this
4429 // allows to switch between two items starting with the same letter just by
4430 // pressing it) but we shouldn't jump to the next one if the user is
4431 // continuing to type as otherwise he might easily skip the item he wanted
4432 size_t itemid = idParent;
4433 if ( prefix.length() == 1 )
4434 {
4435 itemid += 1;
4436 }
4437
4438 // look for the item starting with the given prefix after it
4439 while ( ( itemid < (size_t)GetItemCount() ) &&
4440 !GetLine(itemid)->GetText(0).Lower().StartsWith(prefix) )
4441 {
4442 itemid += 1;
4443 }
4444
4445 // if we haven't found anything...
4446 if ( !( itemid < (size_t)GetItemCount() ) )
4447 {
4448 // ... wrap to the beginning
4449 itemid = 0;
4450
4451 // and try all the items (stop when we get to the one we started from)
4452 while ( ( itemid < (size_t)GetItemCount() ) && itemid != idParent &&
4453 !GetLine(itemid)->GetText(0).Lower().StartsWith(prefix) )
4454 {
4455 itemid += 1;
4456 }
4457 // If we haven't found the item, id will be (size_t)-1, as per
4458 // documentation
4459 if ( !( itemid < (size_t)GetItemCount() ) ||
4460 ( ( itemid == idParent ) &&
4461 !GetLine(itemid)->GetText(0).Lower().StartsWith(prefix) ) )
4462 {
4463 itemid = (size_t)-1;
4464 }
4465 }
4466
4467 return itemid;
4468 }
4469
4470 // -------------------------------------------------------------------------------------
4471 // wxGenericListCtrl
4472 // -------------------------------------------------------------------------------------
4473
4474 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl, wxControl)
4475
4476 BEGIN_EVENT_TABLE(wxGenericListCtrl,wxListCtrlBase)
4477 EVT_SIZE(wxGenericListCtrl::OnSize)
4478 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll)
4479 END_EVENT_TABLE()
4480
4481 void wxGenericListCtrl::Init()
4482 {
4483 m_imageListNormal = NULL;
4484 m_imageListSmall = NULL;
4485 m_imageListState = NULL;
4486
4487 m_ownsImageListNormal =
4488 m_ownsImageListSmall =
4489 m_ownsImageListState = false;
4490
4491 m_mainWin = NULL;
4492 m_headerWin = NULL;
4493 }
4494
4495 wxGenericListCtrl::~wxGenericListCtrl()
4496 {
4497 if (m_ownsImageListNormal)
4498 delete m_imageListNormal;
4499 if (m_ownsImageListSmall)
4500 delete m_imageListSmall;
4501 if (m_ownsImageListState)
4502 delete m_imageListState;
4503 }
4504
4505 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4506 {
4507 bool needs_header = HasHeader();
4508 bool has_header = (m_headerWin != NULL);
4509
4510 if (needs_header == has_header)
4511 return;
4512
4513 if (needs_header)
4514 {
4515 // Notice that we must initialize m_headerWin first, and create the
4516 // real window only later, so that the test in the beginning of the
4517 // function blocks repeated creation of the header as it could happen
4518 // before via wxNavigationEnabled::AddChild() -> ToggleWindowStyle() ->
4519 // SetWindowStyleFlag().
4520 m_headerWin = new wxListHeaderWindow();
4521 m_headerWin->Create
4522 (
4523 this, wxID_ANY, m_mainWin,
4524 wxPoint(0,0),
4525 wxSize
4526 (
4527 GetClientSize().x,
4528 wxRendererNative::Get().GetHeaderButtonHeight(this)
4529 ),
4530 wxTAB_TRAVERSAL
4531 );
4532
4533 #if defined( __WXMAC__ )
4534 static wxFont font( wxOSX_SYSTEM_FONT_SMALL );
4535 m_headerWin->SetFont( font );
4536 #endif
4537
4538 GetSizer()->Prepend( m_headerWin, 0, wxGROW );
4539 }
4540 else
4541 {
4542 GetSizer()->Detach( m_headerWin );
4543
4544 wxDELETE(m_headerWin);
4545 }
4546 }
4547
4548 bool wxGenericListCtrl::Create(wxWindow *parent,
4549 wxWindowID id,
4550 const wxPoint &pos,
4551 const wxSize &size,
4552 long style,
4553 const wxValidator &validator,
4554 const wxString &name)
4555 {
4556 Init();
4557
4558 // just like in other ports, an assert will fail if the user doesn't give any type style:
4559 wxASSERT_MSG( (style & wxLC_MASK_TYPE),
4560 wxT("wxListCtrl style should have exactly one mode bit set") );
4561
4562 if ( !wxListCtrlBase::Create( parent, id, pos, size,
4563 style | wxVSCROLL | wxHSCROLL,
4564 validator, name ) )
4565 return false;
4566
4567 m_mainWin = new wxListMainWindow(this, wxID_ANY, wxPoint(0, 0), size);
4568
4569 SetTargetWindow( m_mainWin );
4570
4571 // We use the cursor keys for moving the selection, not scrolling, so call
4572 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4573 // keyboard events forwarded to us from wxListMainWindow.
4574 DisableKeyboardScrolling();
4575
4576 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
4577 sizer->Add( m_mainWin, 1, wxGROW );
4578 SetSizer( sizer );
4579
4580 CreateOrDestroyHeaderWindowAsNeeded();
4581
4582 SetInitialSize(size);
4583
4584 return true;
4585 }
4586
4587 wxBorder wxGenericListCtrl::GetDefaultBorder() const
4588 {
4589 return wxBORDER_THEME;
4590 }
4591
4592 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4593 WXLRESULT wxGenericListCtrl::MSWWindowProc(WXUINT nMsg,
4594 WXWPARAM wParam,
4595 WXLPARAM lParam)
4596 {
4597 WXLRESULT rc = wxListCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
4598
4599 // we need to process arrows ourselves for scrolling
4600 if ( nMsg == WM_GETDLGCODE )
4601 {
4602 rc |= DLGC_WANTARROWS;
4603 }
4604
4605 return rc;
4606 }
4607 #endif // __WXMSW__
4608
4609 wxSize wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
4610 {
4611 wxSize newsize = size;
4612 if (m_headerWin)
4613 newsize.y -= m_headerWin->GetSize().y;
4614
4615 return newsize;
4616 }
4617
4618 void wxGenericListCtrl::OnScroll(wxScrollWinEvent& event)
4619 {
4620 // update our idea of which lines are shown when we redraw
4621 // the window the next time
4622 m_mainWin->ResetVisibleLinesRange();
4623
4624 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4625 {
4626 m_headerWin->Refresh();
4627 m_headerWin->Update();
4628 }
4629
4630 // Let the window be scrolled as usual by the default handler.
4631 event.Skip();
4632 }
4633
4634 void wxGenericListCtrl::SetSingleStyle( long style, bool add )
4635 {
4636 wxASSERT_MSG( !(style & wxLC_VIRTUAL),
4637 wxT("wxLC_VIRTUAL can't be [un]set") );
4638
4639 long flag = GetWindowStyle();
4640
4641 if (add)
4642 {
4643 if (style & wxLC_MASK_TYPE)
4644 flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
4645 if (style & wxLC_MASK_ALIGN)
4646 flag &= ~wxLC_MASK_ALIGN;
4647 if (style & wxLC_MASK_SORT)
4648 flag &= ~wxLC_MASK_SORT;
4649 }
4650
4651 if (add)
4652 flag |= style;
4653 else
4654 flag &= ~style;
4655
4656 // some styles can be set without recreating everything (as happens in
4657 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4658 if ( !(style & ~(wxLC_HRULES | wxLC_VRULES)) )
4659 {
4660 Refresh();
4661 wxWindow::SetWindowStyleFlag(flag);
4662 }
4663 else
4664 {
4665 SetWindowStyleFlag( flag );
4666 }
4667 }
4668
4669 void wxGenericListCtrl::SetWindowStyleFlag( long flag )
4670 {
4671 // we add wxHSCROLL and wxVSCROLL in ctor unconditionally and it never
4672 // makes sense to remove them as we'll always add scrollbars anyhow when
4673 // needed
4674 flag |= wxHSCROLL | wxVSCROLL;
4675
4676 const bool wasInReportView = HasFlag(wxLC_REPORT);
4677
4678 // update the window style first so that the header is created or destroyed
4679 // corresponding to the new style
4680 wxWindow::SetWindowStyleFlag( flag );
4681
4682 if (m_mainWin)
4683 {
4684 const bool inReportView = (flag & wxLC_REPORT) != 0;
4685 if ( inReportView != wasInReportView )
4686 {
4687 // we need to notify the main window about this change as it must
4688 // update its data structures
4689 m_mainWin->SetReportView(inReportView);
4690 }
4691
4692 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4693
4694 CreateOrDestroyHeaderWindowAsNeeded();
4695
4696 GetSizer()->Layout();
4697 }
4698 }
4699
4700 bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
4701 {
4702 m_mainWin->GetColumn( col, item );
4703 return true;
4704 }
4705
4706 bool wxGenericListCtrl::SetColumn( int col, const wxListItem& item )
4707 {
4708 m_mainWin->SetColumn( col, item );
4709 return true;
4710 }
4711
4712 int wxGenericListCtrl::GetColumnWidth( int col ) const
4713 {
4714 return m_mainWin->GetColumnWidth( col );
4715 }
4716
4717 bool wxGenericListCtrl::SetColumnWidth( int col, int width )
4718 {
4719 m_mainWin->SetColumnWidth( col, width );
4720 return true;
4721 }
4722
4723 int wxGenericListCtrl::GetCountPerPage() const
4724 {
4725 return m_mainWin->GetCountPerPage(); // different from Windows ?
4726 }
4727
4728 bool wxGenericListCtrl::GetItem( wxListItem &info ) const
4729 {
4730 m_mainWin->GetItem( info );
4731 return true;
4732 }
4733
4734 bool wxGenericListCtrl::SetItem( wxListItem &info )
4735 {
4736 m_mainWin->SetItem( info );
4737 return true;
4738 }
4739
4740 long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
4741 {
4742 wxListItem info;
4743 info.m_text = label;
4744 info.m_mask = wxLIST_MASK_TEXT;
4745 info.m_itemId = index;
4746 info.m_col = col;
4747 if ( imageId > -1 )
4748 {
4749 info.m_image = imageId;
4750 info.m_mask |= wxLIST_MASK_IMAGE;
4751 }
4752
4753 m_mainWin->SetItem(info);
4754 return true;
4755 }
4756
4757 int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
4758 {
4759 return m_mainWin->GetItemState( item, stateMask );
4760 }
4761
4762 bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
4763 {
4764 m_mainWin->SetItemState( item, state, stateMask );
4765 return true;
4766 }
4767
4768 bool
4769 wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
4770 {
4771 return SetItemColumnImage(item, 0, image);
4772 }
4773
4774 bool
4775 wxGenericListCtrl::SetItemColumnImage( long item, long column, int image )
4776 {
4777 wxListItem info;
4778 info.m_image = image;
4779 info.m_mask = wxLIST_MASK_IMAGE;
4780 info.m_itemId = item;
4781 info.m_col = column;
4782 m_mainWin->SetItem( info );
4783 return true;
4784 }
4785
4786 wxString wxGenericListCtrl::GetItemText( long item, int col ) const
4787 {
4788 return m_mainWin->GetItemText(item, col);
4789 }
4790
4791 void wxGenericListCtrl::SetItemText( long item, const wxString& str )
4792 {
4793 m_mainWin->SetItemText(item, str);
4794 }
4795
4796 wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const
4797 {
4798 wxListItem info;
4799 info.m_mask = wxLIST_MASK_DATA;
4800 info.m_itemId = item;
4801 m_mainWin->GetItem( info );
4802 return info.m_data;
4803 }
4804
4805 bool wxGenericListCtrl::SetItemPtrData( long item, wxUIntPtr data )
4806 {
4807 wxListItem info;
4808 info.m_mask = wxLIST_MASK_DATA;
4809 info.m_itemId = item;
4810 info.m_data = data;
4811 m_mainWin->SetItem( info );
4812 return true;
4813 }
4814
4815 wxRect wxGenericListCtrl::GetViewRect() const
4816 {
4817 return m_mainWin->GetViewRect();
4818 }
4819
4820 bool wxGenericListCtrl::GetItemRect(long item, wxRect& rect, int code) const
4821 {
4822 return GetSubItemRect(item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code);
4823 }
4824
4825 bool wxGenericListCtrl::GetSubItemRect(long item,
4826 long subItem,
4827 wxRect& rect,
4828 int WXUNUSED(code)) const
4829 {
4830 if ( !m_mainWin->GetSubItemRect( item, subItem, rect ) )
4831 return false;
4832
4833 if ( m_mainWin->HasHeader() )
4834 rect.y += m_headerWin->GetSize().y + 1;
4835
4836 return true;
4837 }
4838
4839 bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
4840 {
4841 m_mainWin->GetItemPosition( item, pos );
4842 return true;
4843 }
4844
4845 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
4846 {
4847 return false;
4848 }
4849
4850 int wxGenericListCtrl::GetItemCount() const
4851 {
4852 return m_mainWin->GetItemCount();
4853 }
4854
4855 int wxGenericListCtrl::GetColumnCount() const
4856 {
4857 return m_mainWin->GetColumnCount();
4858 }
4859
4860 void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
4861 {
4862 m_mainWin->SetItemSpacing( spacing, isSmall );
4863 }
4864
4865 wxSize wxGenericListCtrl::GetItemSpacing() const
4866 {
4867 const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON));
4868
4869 return wxSize(spacing, spacing);
4870 }
4871
4872 #if WXWIN_COMPATIBILITY_2_6
4873 int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const
4874 {
4875 return m_mainWin->GetItemSpacing( isSmall );
4876 }
4877 #endif // WXWIN_COMPATIBILITY_2_6
4878
4879 void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
4880 {
4881 wxListItem info;
4882 info.m_itemId = item;
4883 info.SetTextColour( col );
4884 m_mainWin->SetItem( info );
4885 }
4886
4887 wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
4888 {
4889 wxListItem info;
4890 info.m_itemId = item;
4891 m_mainWin->GetItem( info );
4892 return info.GetTextColour();
4893 }
4894
4895 void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
4896 {
4897 wxListItem info;
4898 info.m_itemId = item;
4899 info.SetBackgroundColour( col );
4900 m_mainWin->SetItem( info );
4901 }
4902
4903 wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
4904 {
4905 wxListItem info;
4906 info.m_itemId = item;
4907 m_mainWin->GetItem( info );
4908 return info.GetBackgroundColour();
4909 }
4910
4911 void wxGenericListCtrl::SetItemFont( long item, const wxFont &f )
4912 {
4913 wxListItem info;
4914 info.m_itemId = item;
4915 info.SetFont( f );
4916 m_mainWin->SetItem( info );
4917 }
4918
4919 wxFont wxGenericListCtrl::GetItemFont( long item ) const
4920 {
4921 wxListItem info;
4922 info.m_itemId = item;
4923 m_mainWin->GetItem( info );
4924 return info.GetFont();
4925 }
4926
4927 int wxGenericListCtrl::GetSelectedItemCount() const
4928 {
4929 return m_mainWin->GetSelectedItemCount();
4930 }
4931
4932 wxColour wxGenericListCtrl::GetTextColour() const
4933 {
4934 return GetForegroundColour();
4935 }
4936
4937 void wxGenericListCtrl::SetTextColour(const wxColour& col)
4938 {
4939 SetForegroundColour(col);
4940 }
4941
4942 long wxGenericListCtrl::GetTopItem() const
4943 {
4944 size_t top;
4945 m_mainWin->GetVisibleLinesRange(&top, NULL);
4946 return (long)top;
4947 }
4948
4949 long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
4950 {
4951 return m_mainWin->GetNextItem( item, geom, state );
4952 }
4953
4954 wxImageList *wxGenericListCtrl::GetImageList(int which) const
4955 {
4956 if (which == wxIMAGE_LIST_NORMAL)
4957 return m_imageListNormal;
4958 else if (which == wxIMAGE_LIST_SMALL)
4959 return m_imageListSmall;
4960 else if (which == wxIMAGE_LIST_STATE)
4961 return m_imageListState;
4962
4963 return NULL;
4964 }
4965
4966 void wxGenericListCtrl::SetImageList( wxImageList *imageList, int which )
4967 {
4968 if ( which == wxIMAGE_LIST_NORMAL )
4969 {
4970 if (m_ownsImageListNormal)
4971 delete m_imageListNormal;
4972 m_imageListNormal = imageList;
4973 m_ownsImageListNormal = false;
4974 }
4975 else if ( which == wxIMAGE_LIST_SMALL )
4976 {
4977 if (m_ownsImageListSmall)
4978 delete m_imageListSmall;
4979 m_imageListSmall = imageList;
4980 m_ownsImageListSmall = false;
4981 }
4982 else if ( which == wxIMAGE_LIST_STATE )
4983 {
4984 if (m_ownsImageListState)
4985 delete m_imageListState;
4986 m_imageListState = imageList;
4987 m_ownsImageListState = false;
4988 }
4989
4990 m_mainWin->SetImageList( imageList, which );
4991 }
4992
4993 void wxGenericListCtrl::AssignImageList(wxImageList *imageList, int which)
4994 {
4995 SetImageList(imageList, which);
4996 if ( which == wxIMAGE_LIST_NORMAL )
4997 m_ownsImageListNormal = true;
4998 else if ( which == wxIMAGE_LIST_SMALL )
4999 m_ownsImageListSmall = true;
5000 else if ( which == wxIMAGE_LIST_STATE )
5001 m_ownsImageListState = true;
5002 }
5003
5004 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
5005 {
5006 return 0;
5007 }
5008
5009 bool wxGenericListCtrl::DeleteItem( long item )
5010 {
5011 m_mainWin->DeleteItem( item );
5012 return true;
5013 }
5014
5015 bool wxGenericListCtrl::DeleteAllItems()
5016 {
5017 m_mainWin->DeleteAllItems();
5018 return true;
5019 }
5020
5021 bool wxGenericListCtrl::DeleteAllColumns()
5022 {
5023 size_t count = m_mainWin->m_columns.GetCount();
5024 for ( size_t n = 0; n < count; n++ )
5025 DeleteColumn( 0 );
5026 return true;
5027 }
5028
5029 void wxGenericListCtrl::ClearAll()
5030 {
5031 m_mainWin->DeleteEverything();
5032 }
5033
5034 bool wxGenericListCtrl::DeleteColumn( int col )
5035 {
5036 m_mainWin->DeleteColumn( col );
5037
5038 // if we don't have the header any longer, we need to relayout the window
5039 // if ( !GetColumnCount() )
5040
5041
5042 // Ensure that the non-existent columns are really removed from display.
5043 Refresh();
5044
5045 return true;
5046 }
5047
5048 wxTextCtrl *wxGenericListCtrl::EditLabel(long item,
5049 wxClassInfo* textControlClass)
5050 {
5051 return m_mainWin->EditLabel( item, textControlClass );
5052 }
5053
5054 wxTextCtrl *wxGenericListCtrl::GetEditControl() const
5055 {
5056 return m_mainWin->GetEditControl();
5057 }
5058
5059 bool wxGenericListCtrl::EnsureVisible( long item )
5060 {
5061 m_mainWin->EnsureVisible( item );
5062 return true;
5063 }
5064
5065 long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
5066 {
5067 return m_mainWin->FindItem( start, str, partial );
5068 }
5069
5070 long wxGenericListCtrl::FindItem( long start, wxUIntPtr data )
5071 {
5072 return m_mainWin->FindItem( start, data );
5073 }
5074
5075 long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt,
5076 int WXUNUSED(direction))
5077 {
5078 return m_mainWin->FindItem( pt );
5079 }
5080
5081 // TODO: sub item hit testing
5082 long wxGenericListCtrl::HitTest(const wxPoint& point, int& flags, long *) const
5083 {
5084 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
5085 }
5086
5087 long wxGenericListCtrl::InsertItem( wxListItem& info )
5088 {
5089 m_mainWin->InsertItem( info );
5090 return info.m_itemId;
5091 }
5092
5093 long wxGenericListCtrl::InsertItem( long index, const wxString &label )
5094 {
5095 wxListItem info;
5096 info.m_text = label;
5097 info.m_mask = wxLIST_MASK_TEXT;
5098 info.m_itemId = index;
5099 return InsertItem( info );
5100 }
5101
5102 long wxGenericListCtrl::InsertItem( long index, int imageIndex )
5103 {
5104 wxListItem info;
5105 info.m_mask = wxLIST_MASK_IMAGE;
5106 info.m_image = imageIndex;
5107 info.m_itemId = index;
5108 return InsertItem( info );
5109 }
5110
5111 long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
5112 {
5113 wxListItem info;
5114 info.m_text = label;
5115 info.m_image = imageIndex;
5116 info.m_mask = wxLIST_MASK_TEXT;
5117 if (imageIndex > -1)
5118 info.m_mask |= wxLIST_MASK_IMAGE;
5119 info.m_itemId = index;
5120 return InsertItem( info );
5121 }
5122
5123 long wxGenericListCtrl::DoInsertColumn( long col, const wxListItem &item )
5124 {
5125 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
5126
5127 long idx = m_mainWin->InsertColumn( col, item );
5128
5129 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
5130 // still have m_headerWin==NULL
5131 if (m_headerWin)
5132 m_headerWin->Refresh();
5133
5134 return idx;
5135 }
5136
5137 bool wxGenericListCtrl::ScrollList( int dx, int dy )
5138 {
5139 return m_mainWin->ScrollList(dx, dy);
5140 }
5141
5142 // Sort items.
5143 // fn is a function which takes 3 long arguments: item1, item2, data.
5144 // item1 is the long data associated with a first item (NOT the index).
5145 // item2 is the long data associated with a second item (NOT the index).
5146 // data is the same value as passed to SortItems.
5147 // The return value is a negative number if the first item should precede the second
5148 // item, a positive number of the second item should precede the first,
5149 // or zero if the two items are equivalent.
5150 // data is arbitrary data to be passed to the sort function.
5151
5152 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn, wxIntPtr data )
5153 {
5154 m_mainWin->SortItems( fn, data );
5155 return true;
5156 }
5157
5158 // ----------------------------------------------------------------------------
5159 // event handlers
5160 // ----------------------------------------------------------------------------
5161
5162 void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
5163 {
5164 if (!m_mainWin) return;
5165
5166 // We need to override OnSize so that our scrolled
5167 // window a) does call Layout() to use sizers for
5168 // positioning the controls but b) does not query
5169 // the sizer for their size and use that for setting
5170 // the scrollable area as set that ourselves by
5171 // calling SetScrollbar() further down.
5172
5173 Layout();
5174
5175 m_mainWin->RecalculatePositions();
5176
5177 AdjustScrollbars();
5178 }
5179
5180 void wxGenericListCtrl::OnInternalIdle()
5181 {
5182 wxWindow::OnInternalIdle();
5183
5184 if (m_mainWin->m_dirty)
5185 m_mainWin->RecalculatePositions();
5186 }
5187
5188 // ----------------------------------------------------------------------------
5189 // font/colours
5190 // ----------------------------------------------------------------------------
5191
5192 bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
5193 {
5194 if (m_mainWin)
5195 {
5196 m_mainWin->SetBackgroundColour( colour );
5197 m_mainWin->m_dirty = true;
5198 }
5199
5200 return true;
5201 }
5202
5203 bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
5204 {
5205 if ( !wxWindow::SetForegroundColour( colour ) )
5206 return false;
5207
5208 if (m_mainWin)
5209 {
5210 m_mainWin->SetForegroundColour( colour );
5211 m_mainWin->m_dirty = true;
5212 }
5213
5214 if (m_headerWin)
5215 m_headerWin->SetForegroundColour( colour );
5216
5217 return true;
5218 }
5219
5220 bool wxGenericListCtrl::SetFont( const wxFont &font )
5221 {
5222 if ( !wxWindow::SetFont( font ) )
5223 return false;
5224
5225 if (m_mainWin)
5226 {
5227 m_mainWin->SetFont( font );
5228 m_mainWin->m_dirty = true;
5229 }
5230
5231 if (m_headerWin)
5232 {
5233 m_headerWin->SetFont( font );
5234 // CalculateAndSetHeaderHeight();
5235 }
5236
5237 Refresh();
5238
5239 return true;
5240 }
5241
5242 // static
5243 wxVisualAttributes
5244 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
5245 {
5246 #if _USE_VISATTR
5247 // Use the same color scheme as wxListBox
5248 return wxListBox::GetClassDefaultAttributes(variant);
5249 #else
5250 wxUnusedVar(variant);
5251 wxVisualAttributes attr;
5252 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
5253 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
5254 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
5255 return attr;
5256 #endif
5257 }
5258
5259 // ----------------------------------------------------------------------------
5260 // methods forwarded to m_mainWin
5261 // ----------------------------------------------------------------------------
5262
5263 #if wxUSE_DRAG_AND_DROP
5264
5265 void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
5266 {
5267 m_mainWin->SetDropTarget( dropTarget );
5268 }
5269
5270 wxDropTarget *wxGenericListCtrl::GetDropTarget() const
5271 {
5272 return m_mainWin->GetDropTarget();
5273 }
5274
5275 #endif
5276
5277 bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
5278 {
5279 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false;
5280 }
5281
5282 wxColour wxGenericListCtrl::GetBackgroundColour() const
5283 {
5284 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5285 }
5286
5287 wxColour wxGenericListCtrl::GetForegroundColour() const
5288 {
5289 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5290 }
5291
5292 bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
5293 {
5294 #if wxUSE_MENUS
5295 return m_mainWin->PopupMenu( menu, x, y );
5296 #else
5297 return false;
5298 #endif
5299 }
5300
5301 wxSize wxGenericListCtrl::DoGetBestClientSize() const
5302 {
5303 // The base class version can compute the best size in report view only.
5304 wxSize sizeBest = wxListCtrlBase::DoGetBestClientSize();
5305
5306 if ( !InReportView() )
5307 {
5308 // Ensure that our minimal width is at least big enough to show all our
5309 // items. This is important for wxListbook to size itself correctly.
5310
5311 // Remember the offset of the first item: this corresponds to the
5312 // margins around the item so we will add it to the minimal size below
5313 // to ensure that we have equal margins on all sides.
5314 wxPoint ofs;
5315
5316 // We can iterate over all items as there shouldn't be too many of them
5317 // in non-report view. If it ever becomes a problem, we could examine
5318 // just the first few items probably, the determination of the best
5319 // size is less important if we will need scrollbars anyhow.
5320 for ( int n = 0; n < GetItemCount(); n++ )
5321 {
5322 const wxRect itemRect = m_mainWin->GetLineRect(n);
5323 if ( !n )
5324 {
5325 // Remember the position of the first item as all the rest are
5326 // offset by at least this number of pixels too.
5327 ofs = itemRect.GetPosition();
5328 }
5329
5330 sizeBest.IncTo(itemRect.GetSize());
5331 }
5332
5333 sizeBest.IncBy(2*ofs);
5334
5335
5336 // If we have the scrollbars we need to account for them too. And to
5337 // make sure the scrollbars status is up to date we need to call this
5338 // function to set them.
5339 m_mainWin->RecalculatePositions(true /* no refresh */);
5340
5341 // Unfortunately we can't use wxWindow::HasScrollbar() here as we need
5342 // to use m_mainWin client/virtual size for determination of whether we
5343 // use scrollbars and not the size of this window itself. Maybe that
5344 // function should be extended to work correctly in the case when our
5345 // scrollbars manage a different window from this one but currently it
5346 // doesn't work.
5347 const wxSize sizeClient = m_mainWin->GetClientSize();
5348 const wxSize sizeVirt = m_mainWin->GetVirtualSize();
5349
5350 if ( sizeVirt.x > sizeClient.x /* HasScrollbar(wxHORIZONTAL) */ )
5351 sizeBest.y += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
5352
5353 if ( sizeVirt.y > sizeClient.y /* HasScrollbar(wxVERTICAL) */ )
5354 sizeBest.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
5355 }
5356
5357 return sizeBest;
5358 }
5359
5360 // ----------------------------------------------------------------------------
5361 // virtual list control support
5362 // ----------------------------------------------------------------------------
5363
5364 wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
5365 {
5366 // this is a pure virtual function, in fact - which is not really pure
5367 // because the controls which are not virtual don't need to implement it
5368 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5369
5370 return wxEmptyString;
5371 }
5372
5373 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
5374 {
5375 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
5376 -1,
5377 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5378 return -1;
5379 }
5380
5381 int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const
5382 {
5383 if (!column)
5384 return OnGetItemImage(item);
5385
5386 return -1;
5387 }
5388
5389 void wxGenericListCtrl::SetItemCount(long count)
5390 {
5391 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
5392
5393 m_mainWin->SetItemCount(count);
5394 }
5395
5396 void wxGenericListCtrl::RefreshItem(long item)
5397 {
5398 m_mainWin->RefreshLine(item);
5399 }
5400
5401 void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
5402 {
5403 m_mainWin->RefreshLines(itemFrom, itemTo);
5404 }
5405
5406 void wxGenericListCtrl::EnableBellOnNoMatch( bool on )
5407 {
5408 m_mainWin->EnableBellOnNoMatch(on);
5409 }
5410
5411 // Generic wxListCtrl is more or less a container for two other
5412 // windows which drawings are done upon. These are namely
5413 // 'm_headerWin' and 'm_mainWin'.
5414 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5415 // behaviour wxListCtrl has under wxMSW.
5416 //
5417 void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5418 {
5419 if (!rect)
5420 {
5421 // The easy case, no rectangle specified.
5422 if (m_headerWin)
5423 m_headerWin->Refresh(eraseBackground);
5424
5425 if (m_mainWin)
5426 m_mainWin->Refresh(eraseBackground);
5427 }
5428 else
5429 {
5430 // Refresh the header window
5431 if (m_headerWin)
5432 {
5433 wxRect rectHeader = m_headerWin->GetRect();
5434 rectHeader.Intersect(*rect);
5435 if (rectHeader.GetWidth() && rectHeader.GetHeight())
5436 {
5437 int x, y;
5438 m_headerWin->GetPosition(&x, &y);
5439 rectHeader.Offset(-x, -y);
5440 m_headerWin->Refresh(eraseBackground, &rectHeader);
5441 }
5442 }
5443
5444 // Refresh the main window
5445 if (m_mainWin)
5446 {
5447 wxRect rectMain = m_mainWin->GetRect();
5448 rectMain.Intersect(*rect);
5449 if (rectMain.GetWidth() && rectMain.GetHeight())
5450 {
5451 int x, y;
5452 m_mainWin->GetPosition(&x, &y);
5453 rectMain.Offset(-x, -y);
5454 m_mainWin->Refresh(eraseBackground, &rectMain);
5455 }
5456 }
5457 }
5458 }
5459
5460 void wxGenericListCtrl::Update()
5461 {
5462 if ( m_mainWin )
5463 {
5464 if ( m_mainWin->m_dirty )
5465 m_mainWin->RecalculatePositions();
5466
5467 m_mainWin->Update();
5468 }
5469
5470 if ( m_headerWin )
5471 m_headerWin->Update();
5472 }
5473
5474 #endif // wxUSE_LISTCTRL