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