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