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