]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listctrl.cpp
wxVariant <-> wxAny conversion functionality. Includes implicit construction of wxAny...
[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);
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 - iy)/2,
1174 wxIMAGELIST_DRAW_TRANSPARENT
1175 );
1176 }
1177
1178 dc.DrawText( item.GetText(),
1179 xAligned + EXTRA_WIDTH, (h - hLabel) / 2 );
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::SetReportView(bool inReportView)
1654 {
1655 const size_t count = m_lines.size();
1656 for ( size_t n = 0; n < count; n++ )
1657 {
1658 m_lines[n].SetReportView(inReportView);
1659 }
1660 }
1661
1662 void wxListMainWindow::CacheLineData(size_t line)
1663 {
1664 wxGenericListCtrl *listctrl = GetListCtrl();
1665
1666 wxListLineData *ld = GetDummyLine();
1667
1668 size_t countCol = GetColumnCount();
1669 for ( size_t col = 0; col < countCol; col++ )
1670 {
1671 ld->SetText(col, listctrl->OnGetItemText(line, col));
1672 ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col));
1673 }
1674
1675 ld->SetAttr(listctrl->OnGetItemAttr(line));
1676 }
1677
1678 wxListLineData *wxListMainWindow::GetDummyLine() const
1679 {
1680 wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
1681 wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
1682
1683 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
1684
1685 // we need to recreate the dummy line if the number of columns in the
1686 // control changed as it would have the incorrect number of fields
1687 // otherwise
1688 if ( !m_lines.IsEmpty() &&
1689 m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() )
1690 {
1691 self->m_lines.Clear();
1692 }
1693
1694 if ( m_lines.IsEmpty() )
1695 {
1696 wxListLineData *line = new wxListLineData(self);
1697 self->m_lines.Add(line);
1698
1699 // don't waste extra memory -- there never going to be anything
1700 // else/more in this array
1701 self->m_lines.Shrink();
1702 }
1703
1704 return &m_lines[0];
1705 }
1706
1707 // ----------------------------------------------------------------------------
1708 // line geometry (report mode only)
1709 // ----------------------------------------------------------------------------
1710
1711 wxCoord wxListMainWindow::GetLineHeight() const
1712 {
1713 // we cache the line height as calling GetTextExtent() is slow
1714 if ( !m_lineHeight )
1715 {
1716 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
1717
1718 wxClientDC dc( self );
1719 dc.SetFont( GetFont() );
1720
1721 wxCoord y;
1722 dc.GetTextExtent(wxT("H"), NULL, &y);
1723
1724 if ( m_small_image_list && m_small_image_list->GetImageCount() )
1725 {
1726 int iw = 0, ih = 0;
1727 m_small_image_list->GetSize(0, iw, ih);
1728 y = wxMax(y, ih);
1729 }
1730
1731 y += EXTRA_HEIGHT;
1732 self->m_lineHeight = y + LINE_SPACING;
1733 }
1734
1735 return m_lineHeight;
1736 }
1737
1738 wxCoord wxListMainWindow::GetLineY(size_t line) const
1739 {
1740 wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
1741
1742 return LINE_SPACING + line * GetLineHeight();
1743 }
1744
1745 wxRect wxListMainWindow::GetLineRect(size_t line) const
1746 {
1747 if ( !InReportView() )
1748 return GetLine(line)->m_gi->m_rectAll;
1749
1750 wxRect rect;
1751 rect.x = HEADER_OFFSET_X;
1752 rect.y = GetLineY(line);
1753 rect.width = GetHeaderWidth();
1754 rect.height = GetLineHeight();
1755
1756 return rect;
1757 }
1758
1759 wxRect wxListMainWindow::GetLineLabelRect(size_t line) const
1760 {
1761 if ( !InReportView() )
1762 return GetLine(line)->m_gi->m_rectLabel;
1763
1764 int image_x = 0;
1765 wxListLineData *data = GetLine(line);
1766 wxListItemDataList::compatibility_iterator node = data->m_items.GetFirst();
1767 if (node)
1768 {
1769 wxListItemData *item = node->GetData();
1770 if ( item->HasImage() )
1771 {
1772 int ix, iy;
1773 GetImageSize( item->GetImage(), ix, iy );
1774 image_x = 3 + ix + IMAGE_MARGIN_IN_REPORT_MODE;
1775 }
1776 }
1777
1778 wxRect rect;
1779 rect.x = image_x + HEADER_OFFSET_X;
1780 rect.y = GetLineY(line);
1781 rect.width = GetColumnWidth(0) - image_x;
1782 rect.height = GetLineHeight();
1783
1784 return rect;
1785 }
1786
1787 wxRect wxListMainWindow::GetLineIconRect(size_t line) const
1788 {
1789 if ( !InReportView() )
1790 return GetLine(line)->m_gi->m_rectIcon;
1791
1792 wxListLineData *ld = GetLine(line);
1793 wxASSERT_MSG( ld->HasImage(), wxT("should have an image") );
1794
1795 wxRect rect;
1796 rect.x = HEADER_OFFSET_X;
1797 rect.y = GetLineY(line);
1798 GetImageSize(ld->GetImage(), rect.width, rect.height);
1799
1800 return rect;
1801 }
1802
1803 wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const
1804 {
1805 return InReportView() ? GetLineRect(line)
1806 : GetLine(line)->m_gi->m_rectHighlight;
1807 }
1808
1809 long wxListMainWindow::HitTestLine(size_t line, int x, int y) const
1810 {
1811 wxASSERT_MSG( line < GetItemCount(), wxT("invalid line in HitTestLine") );
1812
1813 wxListLineData *ld = GetLine(line);
1814
1815 if ( ld->HasImage() && GetLineIconRect(line).Contains(x, y) )
1816 return wxLIST_HITTEST_ONITEMICON;
1817
1818 // VS: Testing for "ld->HasText() || InReportView()" instead of
1819 // "ld->HasText()" is needed to make empty lines in report view
1820 // possible
1821 if ( ld->HasText() || InReportView() )
1822 {
1823 wxRect rect = InReportView() ? GetLineRect(line)
1824 : GetLineLabelRect(line);
1825
1826 if ( rect.Contains(x, y) )
1827 return wxLIST_HITTEST_ONITEMLABEL;
1828 }
1829
1830 return 0;
1831 }
1832
1833 // ----------------------------------------------------------------------------
1834 // highlight (selection) handling
1835 // ----------------------------------------------------------------------------
1836
1837 bool wxListMainWindow::IsHighlighted(size_t line) const
1838 {
1839 if ( IsVirtual() )
1840 {
1841 return m_selStore.IsSelected(line);
1842 }
1843 else // !virtual
1844 {
1845 wxListLineData *ld = GetLine(line);
1846 wxCHECK_MSG( ld, false, wxT("invalid index in IsHighlighted") );
1847
1848 return ld->IsHighlighted();
1849 }
1850 }
1851
1852 void wxListMainWindow::HighlightLines( size_t lineFrom,
1853 size_t lineTo,
1854 bool highlight )
1855 {
1856 if ( IsVirtual() )
1857 {
1858 wxArrayInt linesChanged;
1859 if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight,
1860 &linesChanged) )
1861 {
1862 // meny items changed state, refresh everything
1863 RefreshLines(lineFrom, lineTo);
1864 }
1865 else // only a few items changed state, refresh only them
1866 {
1867 size_t count = linesChanged.GetCount();
1868 for ( size_t n = 0; n < count; n++ )
1869 {
1870 RefreshLine(linesChanged[n]);
1871 }
1872 }
1873 }
1874 else // iterate over all items in non report view
1875 {
1876 for ( size_t line = lineFrom; line <= lineTo; line++ )
1877 {
1878 if ( HighlightLine(line, highlight) )
1879 RefreshLine(line);
1880 }
1881 }
1882 }
1883
1884 bool wxListMainWindow::HighlightLine( size_t line, bool highlight )
1885 {
1886 bool changed;
1887
1888 if ( IsVirtual() )
1889 {
1890 changed = m_selStore.SelectItem(line, highlight);
1891 }
1892 else // !virtual
1893 {
1894 wxListLineData *ld = GetLine(line);
1895 wxCHECK_MSG( ld, false, wxT("invalid index in HighlightLine") );
1896
1897 changed = ld->Highlight(highlight);
1898 }
1899
1900 if ( changed )
1901 {
1902 SendNotify( line, highlight ? wxEVT_COMMAND_LIST_ITEM_SELECTED
1903 : wxEVT_COMMAND_LIST_ITEM_DESELECTED );
1904 }
1905
1906 return changed;
1907 }
1908
1909 void wxListMainWindow::RefreshLine( size_t line )
1910 {
1911 if ( InReportView() )
1912 {
1913 size_t visibleFrom, visibleTo;
1914 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1915
1916 if ( line < visibleFrom || line > visibleTo )
1917 return;
1918 }
1919
1920 wxRect rect = GetLineRect(line);
1921
1922 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1923 RefreshRect( rect );
1924 }
1925
1926 void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo )
1927 {
1928 // we suppose that they are ordered by caller
1929 wxASSERT_MSG( lineFrom <= lineTo, wxT("indices in disorder") );
1930
1931 wxASSERT_MSG( lineTo < GetItemCount(), wxT("invalid line range") );
1932
1933 if ( InReportView() )
1934 {
1935 size_t visibleFrom, visibleTo;
1936 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1937
1938 if ( lineFrom < visibleFrom )
1939 lineFrom = visibleFrom;
1940 if ( lineTo > visibleTo )
1941 lineTo = visibleTo;
1942
1943 wxRect rect;
1944 rect.x = 0;
1945 rect.y = GetLineY(lineFrom);
1946 rect.width = GetClientSize().x;
1947 rect.height = GetLineY(lineTo) - rect.y + GetLineHeight();
1948
1949 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1950 RefreshRect( rect );
1951 }
1952 else // !report
1953 {
1954 // TODO: this should be optimized...
1955 for ( size_t line = lineFrom; line <= lineTo; line++ )
1956 {
1957 RefreshLine(line);
1958 }
1959 }
1960 }
1961
1962 void wxListMainWindow::RefreshAfter( size_t lineFrom )
1963 {
1964 if ( InReportView() )
1965 {
1966 size_t visibleFrom, visibleTo;
1967 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1968
1969 if ( lineFrom < visibleFrom )
1970 lineFrom = visibleFrom;
1971 else if ( lineFrom > visibleTo )
1972 return;
1973
1974 wxRect rect;
1975 rect.x = 0;
1976 rect.y = GetLineY(lineFrom);
1977 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
1978
1979 wxSize size = GetClientSize();
1980 rect.width = size.x;
1981
1982 // refresh till the bottom of the window
1983 rect.height = size.y - rect.y;
1984
1985 RefreshRect( rect );
1986 }
1987 else // !report
1988 {
1989 // TODO: how to do it more efficiently?
1990 m_dirty = true;
1991 }
1992 }
1993
1994 void wxListMainWindow::RefreshSelected()
1995 {
1996 if ( IsEmpty() )
1997 return;
1998
1999 size_t from, to;
2000 if ( InReportView() )
2001 {
2002 GetVisibleLinesRange(&from, &to);
2003 }
2004 else // !virtual
2005 {
2006 from = 0;
2007 to = GetItemCount() - 1;
2008 }
2009
2010 if ( HasCurrent() && m_current >= from && m_current <= to )
2011 RefreshLine(m_current);
2012
2013 for ( size_t line = from; line <= to; line++ )
2014 {
2015 // NB: the test works as expected even if m_current == -1
2016 if ( line != m_current && IsHighlighted(line) )
2017 RefreshLine(line);
2018 }
2019 }
2020
2021 void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2022 {
2023 // Note: a wxPaintDC must be constructed even if no drawing is
2024 // done (a Windows requirement).
2025 wxPaintDC dc( this );
2026
2027 if ( IsEmpty() )
2028 {
2029 // nothing to draw or not the moment to draw it
2030 return;
2031 }
2032
2033 if ( m_dirty )
2034 RecalculatePositions( false );
2035
2036 GetListCtrl()->PrepareDC( dc );
2037
2038 int dev_x, dev_y;
2039 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
2040
2041 dc.SetFont( GetFont() );
2042
2043 if ( InReportView() )
2044 {
2045 int lineHeight = GetLineHeight();
2046
2047 size_t visibleFrom, visibleTo;
2048 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2049
2050 wxRect rectLine;
2051 int xOrig = dc.LogicalToDeviceX( 0 );
2052 int yOrig = dc.LogicalToDeviceY( 0 );
2053
2054 // tell the caller cache to cache the data
2055 if ( IsVirtual() )
2056 {
2057 wxListEvent evCache(wxEVT_COMMAND_LIST_CACHE_HINT,
2058 GetParent()->GetId());
2059 evCache.SetEventObject( GetParent() );
2060 evCache.m_oldItemIndex = visibleFrom;
2061 evCache.m_itemIndex = visibleTo;
2062 GetParent()->GetEventHandler()->ProcessEvent( evCache );
2063 }
2064
2065 for ( size_t line = visibleFrom; line <= visibleTo; line++ )
2066 {
2067 rectLine = GetLineRect(line);
2068
2069
2070 if ( !IsExposed(rectLine.x + xOrig, rectLine.y + yOrig,
2071 rectLine.width, rectLine.height) )
2072 {
2073 // don't redraw unaffected lines to avoid flicker
2074 continue;
2075 }
2076
2077 GetLine(line)->DrawInReportMode( &dc,
2078 rectLine,
2079 GetLineHighlightRect(line),
2080 IsHighlighted(line),
2081 line == m_current );
2082 }
2083
2084 if ( HasFlag(wxLC_HRULES) )
2085 {
2086 wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
2087 wxSize clientSize = GetClientSize();
2088
2089 size_t i = visibleFrom;
2090 if (i == 0) i = 1; // Don't draw the first one
2091 for ( ; i <= visibleTo; i++ )
2092 {
2093 dc.SetPen(pen);
2094 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2095 dc.DrawLine(0 - dev_x, i * lineHeight,
2096 clientSize.x - dev_x, i * lineHeight);
2097 }
2098
2099 // Draw last horizontal rule
2100 if ( visibleTo == GetItemCount() - 1 )
2101 {
2102 dc.SetPen( pen );
2103 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2104 dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight,
2105 clientSize.x - dev_x , (m_lineTo + 1) * lineHeight );
2106 }
2107 }
2108
2109 // Draw vertical rules if required
2110 if ( HasFlag(wxLC_VRULES) && !IsEmpty() )
2111 {
2112 wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
2113 wxRect firstItemRect, lastItemRect;
2114
2115 GetItemRect(visibleFrom, firstItemRect);
2116 GetItemRect(visibleTo, lastItemRect);
2117 int x = firstItemRect.GetX();
2118 dc.SetPen(pen);
2119 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2120
2121 for (int col = 0; col < GetColumnCount(); col++)
2122 {
2123 int colWidth = GetColumnWidth(col);
2124 x += colWidth;
2125 int x_pos = x - dev_x;
2126 if (col < GetColumnCount()-1) x_pos -= 2;
2127 dc.DrawLine(x_pos, firstItemRect.GetY() - 1 - dev_y,
2128 x_pos, lastItemRect.GetBottom() + 1 - dev_y);
2129 }
2130 }
2131 }
2132 else // !report
2133 {
2134 size_t count = GetItemCount();
2135 for ( size_t i = 0; i < count; i++ )
2136 {
2137 GetLine(i)->Draw( &dc );
2138 }
2139 }
2140
2141 #if !defined( __WXMAC__) && !defined(__WXGTK20__)
2142 // Don't draw rect outline under Mac at all.
2143 // Draw it elsewhere under GTK.
2144 if ( HasCurrent() )
2145 {
2146 if ( m_hasFocus )
2147 {
2148 wxRect rect( GetLineHighlightRect( m_current ) );
2149 dc.SetPen( *wxBLACK_PEN );
2150 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2151 dc.DrawRectangle( rect );
2152 }
2153 }
2154 #endif
2155 }
2156
2157 void wxListMainWindow::HighlightAll( bool on )
2158 {
2159 if ( IsSingleSel() )
2160 {
2161 wxASSERT_MSG( !on, wxT("can't do this in a single selection control") );
2162
2163 // we just have one item to turn off
2164 if ( HasCurrent() && IsHighlighted(m_current) )
2165 {
2166 HighlightLine(m_current, false);
2167 RefreshLine(m_current);
2168 }
2169 }
2170 else // multi selection
2171 {
2172 if ( !IsEmpty() )
2173 HighlightLines(0, GetItemCount() - 1, on);
2174 }
2175 }
2176
2177 void wxListMainWindow::OnChildFocus(wxChildFocusEvent& WXUNUSED(event))
2178 {
2179 // Do nothing here. This prevents the default handler in wxScrolledWindow
2180 // from needlessly scrolling the window when the edit control is
2181 // dismissed. See ticket #9563.
2182 }
2183
2184 void wxListMainWindow::SendNotify( size_t line,
2185 wxEventType command,
2186 const wxPoint& point )
2187 {
2188 wxListEvent le( command, GetParent()->GetId() );
2189 le.SetEventObject( GetParent() );
2190
2191 le.m_itemIndex = line;
2192
2193 // set only for events which have position
2194 if ( point != wxDefaultPosition )
2195 le.m_pointDrag = point;
2196
2197 // don't try to get the line info for virtual list controls: the main
2198 // program has it anyhow and if we did it would result in accessing all
2199 // the lines, even those which are not visible now and this is precisely
2200 // what we're trying to avoid
2201 if ( !IsVirtual() )
2202 {
2203 if ( line != (size_t)-1 )
2204 {
2205 GetLine(line)->GetItem( 0, le.m_item );
2206 }
2207 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2208 }
2209 //else: there may be no more such item
2210
2211 GetParent()->GetEventHandler()->ProcessEvent( le );
2212 }
2213
2214 void wxListMainWindow::ChangeCurrent(size_t current)
2215 {
2216 m_current = current;
2217
2218 // as the current item changed, we shouldn't start editing it when the
2219 // "slow click" timer expires as the click happened on another item
2220 if ( m_renameTimer->IsRunning() )
2221 m_renameTimer->Stop();
2222
2223 SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
2224 }
2225
2226 wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
2227 {
2228 wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
2229 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2230
2231 wxASSERT_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)),
2232 wxT("EditLabel() needs a text control") );
2233
2234 size_t itemEdit = (size_t)item;
2235
2236 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
2237 le.SetEventObject( GetParent() );
2238 le.m_itemIndex = item;
2239 wxListLineData *data = GetLine(itemEdit);
2240 wxCHECK_MSG( data, NULL, wxT("invalid index in EditLabel()") );
2241 data->GetItem( 0, le.m_item );
2242
2243 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
2244 {
2245 // vetoed by user code
2246 return NULL;
2247 }
2248
2249 // We have to call this here because the label in question might just have
2250 // been added and no screen update taken place.
2251 if ( m_dirty )
2252 {
2253 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
2254 // so that no pending events may change the item count (see below)
2255 // IMPORTANT: needs to be tested!
2256 wxSafeYield();
2257
2258 // Pending events dispatched by wxSafeYield might have changed the item
2259 // count
2260 if ( (size_t)item >= GetItemCount() )
2261 return NULL;
2262 }
2263
2264 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
2265 m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
2266 return m_textctrlWrapper->GetText();
2267 }
2268
2269 void wxListMainWindow::OnRenameTimer()
2270 {
2271 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2272
2273 EditLabel( m_current );
2274 }
2275
2276 bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
2277 {
2278 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2279 le.SetEventObject( GetParent() );
2280 le.m_itemIndex = itemEdit;
2281
2282 wxListLineData *data = GetLine(itemEdit);
2283
2284 wxCHECK_MSG( data, false, wxT("invalid index in OnRenameAccept()") );
2285
2286 data->GetItem( 0, le.m_item );
2287 le.m_item.m_text = value;
2288 return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
2289 le.IsAllowed();
2290 }
2291
2292 void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
2293 {
2294 // let owner know that the edit was cancelled
2295 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2296
2297 le.SetEditCanceled(true);
2298
2299 le.SetEventObject( GetParent() );
2300 le.m_itemIndex = itemEdit;
2301
2302 wxListLineData *data = GetLine(itemEdit);
2303 wxCHECK_RET( data, wxT("invalid index in OnRenameCancelled()") );
2304
2305 data->GetItem( 0, le.m_item );
2306 GetEventHandler()->ProcessEvent( le );
2307 }
2308
2309 void wxListMainWindow::OnMouse( wxMouseEvent &event )
2310 {
2311 #ifdef __WXMAC__
2312 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2313 // shutdown the edit control when the mouse is clicked elsewhere on the
2314 // listctrl because the order of events is different (or something like
2315 // that), so explicitly end the edit if it is active.
2316 if ( event.LeftDown() && m_textctrlWrapper )
2317 m_textctrlWrapper->EndEdit( false );
2318 #endif // __WXMAC__
2319
2320 if ( event.LeftDown() )
2321 SetFocus();
2322
2323 event.SetEventObject( GetParent() );
2324 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
2325 return;
2326
2327 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
2328 {
2329 // let the base class handle mouse wheel events.
2330 event.Skip();
2331 return;
2332 }
2333
2334 if ( !HasCurrent() || IsEmpty() )
2335 {
2336 if (event.RightDown())
2337 {
2338 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2339
2340 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
2341 GetParent()->GetId(),
2342 ClientToScreen(event.GetPosition()));
2343 evtCtx.SetEventObject(GetParent());
2344 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
2345 }
2346 return;
2347 }
2348
2349 if (m_dirty)
2350 return;
2351
2352 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
2353 event.ButtonDClick()) )
2354 return;
2355
2356 int x = event.GetX();
2357 int y = event.GetY();
2358 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
2359
2360 // where did we hit it (if we did)?
2361 long hitResult = 0;
2362
2363 size_t count = GetItemCount(),
2364 current;
2365
2366 if ( InReportView() )
2367 {
2368 current = y / GetLineHeight();
2369 if ( current < count )
2370 hitResult = HitTestLine(current, x, y);
2371 }
2372 else // !report
2373 {
2374 // TODO: optimize it too! this is less simple than for report view but
2375 // enumerating all items is still not a way to do it!!
2376 for ( current = 0; current < count; current++ )
2377 {
2378 hitResult = HitTestLine(current, x, y);
2379 if ( hitResult )
2380 break;
2381 }
2382 }
2383
2384 if (event.Dragging())
2385 {
2386 if (m_dragCount == 0)
2387 {
2388 // we have to report the raw, physical coords as we want to be
2389 // able to call HitTest(event.m_pointDrag) from the user code to
2390 // get the item being dragged
2391 m_dragStart = event.GetPosition();
2392 }
2393
2394 m_dragCount++;
2395
2396 if (m_dragCount != 3)
2397 return;
2398
2399 int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2400 : wxEVT_COMMAND_LIST_BEGIN_DRAG;
2401
2402 wxListEvent le( command, GetParent()->GetId() );
2403 le.SetEventObject( GetParent() );
2404 le.m_itemIndex = m_lineLastClicked;
2405 le.m_pointDrag = m_dragStart;
2406 GetParent()->GetEventHandler()->ProcessEvent( le );
2407
2408 return;
2409 }
2410 else
2411 {
2412 m_dragCount = 0;
2413 }
2414
2415 if ( !hitResult )
2416 {
2417 // outside of any item
2418 if (event.RightDown())
2419 {
2420 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2421
2422 wxContextMenuEvent evtCtx(
2423 wxEVT_CONTEXT_MENU,
2424 GetParent()->GetId(),
2425 ClientToScreen(event.GetPosition()));
2426 evtCtx.SetEventObject(GetParent());
2427 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
2428 }
2429 else
2430 {
2431 // reset the selection and bail out
2432 HighlightAll(false);
2433 }
2434
2435 return;
2436 }
2437
2438 bool forceClick = false;
2439 if (event.ButtonDClick())
2440 {
2441 if ( m_renameTimer->IsRunning() )
2442 m_renameTimer->Stop();
2443
2444 m_lastOnSame = false;
2445
2446 if ( current == m_lineLastClicked )
2447 {
2448 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2449
2450 return;
2451 }
2452 else
2453 {
2454 // The first click was on another item, so don't interpret this as
2455 // a double click, but as a simple click instead
2456 forceClick = true;
2457 }
2458 }
2459
2460 if (event.LeftUp())
2461 {
2462 if (m_lineSelectSingleOnUp != (size_t)-1)
2463 {
2464 // select single line
2465 HighlightAll( false );
2466 ReverseHighlight(m_lineSelectSingleOnUp);
2467 }
2468
2469 if (m_lastOnSame)
2470 {
2471 if ((current == m_current) &&
2472 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
2473 HasFlag(wxLC_EDIT_LABELS) )
2474 {
2475 if ( !InReportView() ||
2476 GetLineLabelRect(current).Contains(x, y) )
2477 {
2478 int dclick = wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC);
2479 m_renameTimer->Start(dclick > 0 ? dclick : 250, true);
2480 }
2481 }
2482 }
2483
2484 m_lastOnSame = false;
2485 m_lineSelectSingleOnUp = (size_t)-1;
2486 }
2487 else
2488 {
2489 // This is necessary, because after a DnD operation in
2490 // from and to ourself, the up event is swallowed by the
2491 // DnD code. So on next non-up event (which means here and
2492 // now) m_lineSelectSingleOnUp should be reset.
2493 m_lineSelectSingleOnUp = (size_t)-1;
2494 }
2495 if (event.RightDown())
2496 {
2497 m_lineBeforeLastClicked = m_lineLastClicked;
2498 m_lineLastClicked = current;
2499
2500 // If the item is already selected, do not update the selection.
2501 // Multi-selections should not be cleared if a selected item is clicked.
2502 if (!IsHighlighted(current))
2503 {
2504 HighlightAll(false);
2505 ChangeCurrent(current);
2506 ReverseHighlight(m_current);
2507 }
2508
2509 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2510
2511 // Allow generation of context menu event
2512 event.Skip();
2513 }
2514 else if (event.MiddleDown())
2515 {
2516 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
2517 }
2518 else if ( event.LeftDown() || forceClick )
2519 {
2520 m_lineBeforeLastClicked = m_lineLastClicked;
2521 m_lineLastClicked = current;
2522
2523 size_t oldCurrent = m_current;
2524 bool oldWasSelected = IsHighlighted(m_current);
2525
2526 bool cmdModifierDown = event.CmdDown();
2527 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
2528 {
2529 if ( IsSingleSel() || !IsHighlighted(current) )
2530 {
2531 HighlightAll( false );
2532
2533 ChangeCurrent(current);
2534
2535 ReverseHighlight(m_current);
2536 }
2537 else // multi sel & current is highlighted & no mod keys
2538 {
2539 m_lineSelectSingleOnUp = current;
2540 ChangeCurrent(current); // change focus
2541 }
2542 }
2543 else // multi sel & either ctrl or shift is down
2544 {
2545 if (cmdModifierDown)
2546 {
2547 ChangeCurrent(current);
2548
2549 ReverseHighlight(m_current);
2550 }
2551 else if (event.ShiftDown())
2552 {
2553 ChangeCurrent(current);
2554
2555 size_t lineFrom = oldCurrent,
2556 lineTo = current;
2557
2558 if ( lineTo < lineFrom )
2559 {
2560 lineTo = lineFrom;
2561 lineFrom = m_current;
2562 }
2563
2564 HighlightLines(lineFrom, lineTo);
2565 }
2566 else // !ctrl, !shift
2567 {
2568 // test in the enclosing if should make it impossible
2569 wxFAIL_MSG( wxT("how did we get here?") );
2570 }
2571 }
2572
2573 if (m_current != oldCurrent)
2574 RefreshLine( oldCurrent );
2575
2576 // forceClick is only set if the previous click was on another item
2577 m_lastOnSame = !forceClick && (m_current == oldCurrent) && oldWasSelected;
2578 }
2579 }
2580
2581 void wxListMainWindow::MoveToItem(size_t item)
2582 {
2583 if ( item == (size_t)-1 )
2584 return;
2585
2586 wxRect rect = GetLineRect(item);
2587
2588 int client_w, client_h;
2589 GetClientSize( &client_w, &client_h );
2590
2591 const int hLine = GetLineHeight();
2592
2593 int view_x = SCROLL_UNIT_X * GetListCtrl()->GetScrollPos( wxHORIZONTAL );
2594 int view_y = hLine * GetListCtrl()->GetScrollPos( wxVERTICAL );
2595
2596 if ( InReportView() )
2597 {
2598 // the next we need the range of lines shown it might be different,
2599 // so recalculate it
2600 ResetVisibleLinesRange();
2601
2602 if (rect.y < view_y)
2603 GetListCtrl()->Scroll( -1, rect.y / hLine );
2604 if (rect.y + rect.height + 5 > view_y + client_h)
2605 GetListCtrl()->Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
2606
2607 #ifdef __WXMAC__
2608 // At least on Mac the visible lines value will get reset inside of
2609 // Scroll *before* it actually scrolls the window because of the
2610 // Update() that happens there, so it will still have the wrong value.
2611 // So let's reset it again and wait for it to be recalculated in the
2612 // next paint event. I would expect this problem to show up in wxGTK
2613 // too but couldn't duplicate it there. Perhaps the order of events
2614 // is different... --Robin
2615 ResetVisibleLinesRange();
2616 #endif
2617 }
2618 else // !report
2619 {
2620 int sx = -1,
2621 sy = -1;
2622
2623 if (rect.x-view_x < 5)
2624 sx = (rect.x - 5) / SCROLL_UNIT_X;
2625 if (rect.x + rect.width - 5 > view_x + client_w)
2626 sx = (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X;
2627
2628 if (rect.y-view_y < 5)
2629 sy = (rect.y - 5) / hLine;
2630 if (rect.y + rect.height - 5 > view_y + client_h)
2631 sy = (rect.y + rect.height - client_h + hLine) / hLine;
2632
2633 GetListCtrl()->Scroll(sx, sy);
2634 }
2635 }
2636
2637 bool wxListMainWindow::ScrollList(int WXUNUSED(dx), int dy)
2638 {
2639 if ( !InReportView() )
2640 {
2641 // TODO: this should work in all views but is not implemented now
2642 return false;
2643 }
2644
2645 size_t top, bottom;
2646 GetVisibleLinesRange(&top, &bottom);
2647
2648 if ( bottom == (size_t)-1 )
2649 return 0;
2650
2651 ResetVisibleLinesRange();
2652
2653 int hLine = GetLineHeight();
2654
2655 GetListCtrl()->Scroll(-1, top + dy / hLine);
2656
2657 #ifdef __WXMAC__
2658 // see comment in MoveToItem() for why we do this
2659 ResetVisibleLinesRange();
2660 #endif
2661
2662 return true;
2663 }
2664
2665 // ----------------------------------------------------------------------------
2666 // keyboard handling
2667 // ----------------------------------------------------------------------------
2668
2669 void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
2670 {
2671 wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
2672 wxT("invalid item index in OnArrowChar()") );
2673
2674 size_t oldCurrent = m_current;
2675
2676 // in single selection we just ignore Shift as we can't select several
2677 // items anyhow
2678 if ( event.ShiftDown() && !IsSingleSel() )
2679 {
2680 ChangeCurrent(newCurrent);
2681
2682 // refresh the old focus to remove it
2683 RefreshLine( oldCurrent );
2684
2685 // select all the items between the old and the new one
2686 if ( oldCurrent > newCurrent )
2687 {
2688 newCurrent = oldCurrent;
2689 oldCurrent = m_current;
2690 }
2691
2692 HighlightLines(oldCurrent, newCurrent);
2693 }
2694 else // !shift
2695 {
2696 // all previously selected items are unselected unless ctrl is held
2697 // in a multiselection control
2698 if ( !event.ControlDown() || IsSingleSel() )
2699 HighlightAll(false);
2700
2701 ChangeCurrent(newCurrent);
2702
2703 // refresh the old focus to remove it
2704 RefreshLine( oldCurrent );
2705
2706 // in single selection mode we must always have a selected item
2707 if ( !event.ControlDown() || IsSingleSel() )
2708 HighlightLine( m_current, true );
2709 }
2710
2711 RefreshLine( m_current );
2712
2713 MoveToFocus();
2714 }
2715
2716 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
2717 {
2718 wxWindow *parent = GetParent();
2719
2720 // propagate the key event upwards
2721 wxKeyEvent ke(event);
2722 ke.SetEventObject( parent );
2723 if (parent->GetEventHandler()->ProcessEvent( ke ))
2724 return;
2725
2726 event.Skip();
2727 }
2728
2729 void wxListMainWindow::OnKeyUp( wxKeyEvent &event )
2730 {
2731 wxWindow *parent = GetParent();
2732
2733 // propagate the key event upwards
2734 wxKeyEvent ke(event);
2735 if (parent->GetEventHandler()->ProcessEvent( ke ))
2736 return;
2737
2738 event.Skip();
2739 }
2740
2741 void wxListMainWindow::OnChar( wxKeyEvent &event )
2742 {
2743 wxWindow *parent = GetParent();
2744
2745 // send a list_key event up
2746 if ( HasCurrent() )
2747 {
2748 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() );
2749 le.m_itemIndex = m_current;
2750 GetLine(m_current)->GetItem( 0, le.m_item );
2751 le.m_code = event.GetKeyCode();
2752 le.SetEventObject( parent );
2753 parent->GetEventHandler()->ProcessEvent( le );
2754 }
2755
2756 if ( (event.GetKeyCode() != WXK_UP) &&
2757 (event.GetKeyCode() != WXK_DOWN) &&
2758 (event.GetKeyCode() != WXK_RIGHT) &&
2759 (event.GetKeyCode() != WXK_LEFT) &&
2760 (event.GetKeyCode() != WXK_PAGEUP) &&
2761 (event.GetKeyCode() != WXK_PAGEDOWN) &&
2762 (event.GetKeyCode() != WXK_END) &&
2763 (event.GetKeyCode() != WXK_HOME) )
2764 {
2765 // propagate the char event upwards
2766 wxKeyEvent ke(event);
2767 ke.SetEventObject( parent );
2768 if (parent->GetEventHandler()->ProcessEvent( ke ))
2769 return;
2770 }
2771
2772 if ( HandleAsNavigationKey(event) )
2773 return;
2774
2775 // no item -> nothing to do
2776 if (!HasCurrent())
2777 {
2778 event.Skip();
2779 return;
2780 }
2781
2782 // don't use m_linesPerPage directly as it might not be computed yet
2783 const int pageSize = GetCountPerPage();
2784 wxCHECK_RET( pageSize, wxT("should have non zero page size") );
2785
2786 if (GetLayoutDirection() == wxLayout_RightToLeft)
2787 {
2788 if (event.GetKeyCode() == WXK_RIGHT)
2789 event.m_keyCode = WXK_LEFT;
2790 else if (event.GetKeyCode() == WXK_LEFT)
2791 event.m_keyCode = WXK_RIGHT;
2792 }
2793
2794 switch ( event.GetKeyCode() )
2795 {
2796 case WXK_UP:
2797 if ( m_current > 0 )
2798 OnArrowChar( m_current - 1, event );
2799 break;
2800
2801 case WXK_DOWN:
2802 if ( m_current < (size_t)GetItemCount() - 1 )
2803 OnArrowChar( m_current + 1, event );
2804 break;
2805
2806 case WXK_END:
2807 if (!IsEmpty())
2808 OnArrowChar( GetItemCount() - 1, event );
2809 break;
2810
2811 case WXK_HOME:
2812 if (!IsEmpty())
2813 OnArrowChar( 0, event );
2814 break;
2815
2816 case WXK_PAGEUP:
2817 {
2818 int steps = InReportView() ? pageSize - 1
2819 : m_current % pageSize;
2820
2821 int index = m_current - steps;
2822 if (index < 0)
2823 index = 0;
2824
2825 OnArrowChar( index, event );
2826 }
2827 break;
2828
2829 case WXK_PAGEDOWN:
2830 {
2831 int steps = InReportView()
2832 ? pageSize - 1
2833 : pageSize - (m_current % pageSize) - 1;
2834
2835 size_t index = m_current + steps;
2836 size_t count = GetItemCount();
2837 if ( index >= count )
2838 index = count - 1;
2839
2840 OnArrowChar( index, event );
2841 }
2842 break;
2843
2844 case WXK_LEFT:
2845 if ( !InReportView() )
2846 {
2847 int index = m_current - pageSize;
2848 if (index < 0)
2849 index = 0;
2850
2851 OnArrowChar( index, event );
2852 }
2853 break;
2854
2855 case WXK_RIGHT:
2856 if ( !InReportView() )
2857 {
2858 size_t index = m_current + pageSize;
2859
2860 size_t count = GetItemCount();
2861 if ( index >= count )
2862 index = count - 1;
2863
2864 OnArrowChar( index, event );
2865 }
2866 break;
2867
2868 case WXK_SPACE:
2869 if ( IsSingleSel() )
2870 {
2871 if ( event.ControlDown() )
2872 {
2873 ReverseHighlight(m_current);
2874 }
2875 else // normal space press
2876 {
2877 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2878 }
2879 }
2880 else // multiple selection
2881 {
2882 ReverseHighlight(m_current);
2883 }
2884 break;
2885
2886 case WXK_RETURN:
2887 case WXK_EXECUTE:
2888 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
2889 break;
2890
2891 default:
2892 event.Skip();
2893 }
2894 }
2895
2896 // ----------------------------------------------------------------------------
2897 // focus handling
2898 // ----------------------------------------------------------------------------
2899
2900 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
2901 {
2902 if ( GetParent() )
2903 {
2904 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
2905 event.SetEventObject( GetParent() );
2906 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
2907 return;
2908 }
2909
2910 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2911 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2912 // which are already drawn correctly resulting in horrible flicker - avoid
2913 // it
2914 if ( !m_hasFocus )
2915 {
2916 m_hasFocus = true;
2917
2918 RefreshSelected();
2919 }
2920 }
2921
2922 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
2923 {
2924 if ( GetParent() )
2925 {
2926 wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
2927 event.SetEventObject( GetParent() );
2928 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
2929 return;
2930 }
2931
2932 m_hasFocus = false;
2933 RefreshSelected();
2934 }
2935
2936 void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
2937 {
2938 if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
2939 {
2940 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2941 }
2942 else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
2943 {
2944 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2945 }
2946 else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
2947 {
2948 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2949 }
2950 else if ( InReportView() && (m_small_image_list))
2951 {
2952 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2953 }
2954 }
2955
2956 void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
2957 {
2958 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
2959 {
2960 m_normal_image_list->GetSize( index, width, height );
2961 }
2962 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
2963 {
2964 m_small_image_list->GetSize( index, width, height );
2965 }
2966 else if ( HasFlag(wxLC_LIST) && m_small_image_list )
2967 {
2968 m_small_image_list->GetSize( index, width, height );
2969 }
2970 else if ( InReportView() && m_small_image_list )
2971 {
2972 m_small_image_list->GetSize( index, width, height );
2973 }
2974 else
2975 {
2976 width =
2977 height = 0;
2978 }
2979 }
2980
2981 int wxListMainWindow::GetTextLength( const wxString &s ) const
2982 {
2983 wxClientDC dc( wxConstCast(this, wxListMainWindow) );
2984 dc.SetFont( GetFont() );
2985
2986 wxCoord lw;
2987 dc.GetTextExtent( s, &lw, NULL );
2988
2989 return lw + AUTOSIZE_COL_MARGIN;
2990 }
2991
2992 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
2993 {
2994 m_dirty = true;
2995
2996 // calc the spacing from the icon size
2997 int width = 0, height = 0;
2998
2999 if ((imageList) && (imageList->GetImageCount()) )
3000 imageList->GetSize(0, width, height);
3001
3002 if (which == wxIMAGE_LIST_NORMAL)
3003 {
3004 m_normal_image_list = imageList;
3005 m_normal_spacing = width + 8;
3006 }
3007
3008 if (which == wxIMAGE_LIST_SMALL)
3009 {
3010 m_small_image_list = imageList;
3011 m_small_spacing = width + 14;
3012 m_lineHeight = 0; // ensure that the line height will be recalc'd
3013 }
3014 }
3015
3016 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
3017 {
3018 m_dirty = true;
3019 if (isSmall)
3020 m_small_spacing = spacing;
3021 else
3022 m_normal_spacing = spacing;
3023 }
3024
3025 int wxListMainWindow::GetItemSpacing( bool isSmall )
3026 {
3027 return isSmall ? m_small_spacing : m_normal_spacing;
3028 }
3029
3030 // ----------------------------------------------------------------------------
3031 // columns
3032 // ----------------------------------------------------------------------------
3033
3034 void wxListMainWindow::SetColumn( int col, wxListItem &item )
3035 {
3036 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3037
3038 wxCHECK_RET( node, wxT("invalid column index in SetColumn") );
3039
3040 if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
3041 item.m_width = GetTextLength( item.m_text );
3042
3043 wxListHeaderData *column = node->GetData();
3044 column->SetItem( item );
3045
3046 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3047 if ( headerWin )
3048 headerWin->m_dirty = true;
3049
3050 m_dirty = true;
3051
3052 // invalidate it as it has to be recalculated
3053 m_headerWidth = 0;
3054 }
3055
3056 void wxListMainWindow::SetColumnWidth( int col, int width )
3057 {
3058 wxCHECK_RET( col >= 0 && col < GetColumnCount(),
3059 wxT("invalid column index") );
3060
3061 wxCHECK_RET( InReportView(),
3062 wxT("SetColumnWidth() can only be called in report mode.") );
3063
3064 m_dirty = true;
3065
3066 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3067 if ( headerWin )
3068 headerWin->m_dirty = true;
3069
3070 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3071 wxCHECK_RET( node, wxT("no column?") );
3072
3073 wxListHeaderData *column = node->GetData();
3074
3075 size_t count = GetItemCount();
3076
3077 if (width == wxLIST_AUTOSIZE_USEHEADER)
3078 {
3079 width = GetTextLength(column->GetText());
3080 width += 2*EXTRA_WIDTH;
3081
3082 // check for column header's image availability
3083 const int image = column->GetImage();
3084 if ( image != -1 )
3085 {
3086 if ( m_small_image_list )
3087 {
3088 int ix = 0, iy = 0;
3089 m_small_image_list->GetSize(image, ix, iy);
3090 width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
3091 }
3092 }
3093 }
3094 else if ( width == wxLIST_AUTOSIZE )
3095 {
3096 if ( IsVirtual() )
3097 {
3098 // TODO: determine the max width somehow...
3099 width = WIDTH_COL_DEFAULT;
3100 }
3101 else // !virtual
3102 {
3103 wxClientDC dc(this);
3104 dc.SetFont( GetFont() );
3105
3106 int max = AUTOSIZE_COL_MARGIN;
3107
3108 // if the cached column width isn't valid then recalculate it
3109 if (m_aColWidths.Item(col)->bNeedsUpdate)
3110 {
3111 for (size_t i = 0; i < count; i++)
3112 {
3113 wxListLineData *line = GetLine( i );
3114 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3115
3116 wxCHECK_RET( n, wxT("no subitem?") );
3117
3118 wxListItemData *itemData = n->GetData();
3119 wxListItem item;
3120
3121 itemData->GetItem(item);
3122 int itemWidth = GetItemWidthWithImage(&item);
3123 if (itemWidth > max)
3124 max = itemWidth;
3125 }
3126
3127 m_aColWidths.Item(col)->bNeedsUpdate = false;
3128 m_aColWidths.Item(col)->nMaxWidth = max;
3129 }
3130
3131 max = m_aColWidths.Item(col)->nMaxWidth;
3132 width = max + AUTOSIZE_COL_MARGIN;
3133 }
3134 }
3135
3136 column->SetWidth( width );
3137
3138 // invalidate it as it has to be recalculated
3139 m_headerWidth = 0;
3140 }
3141
3142 int wxListMainWindow::GetHeaderWidth() const
3143 {
3144 if ( !m_headerWidth )
3145 {
3146 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
3147
3148 size_t count = GetColumnCount();
3149 for ( size_t col = 0; col < count; col++ )
3150 {
3151 self->m_headerWidth += GetColumnWidth(col);
3152 }
3153 }
3154
3155 return m_headerWidth;
3156 }
3157
3158 void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
3159 {
3160 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3161 wxCHECK_RET( node, wxT("invalid column index in GetColumn") );
3162
3163 wxListHeaderData *column = node->GetData();
3164 column->GetItem( item );
3165 }
3166
3167 int wxListMainWindow::GetColumnWidth( int col ) const
3168 {
3169 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3170 wxCHECK_MSG( node, 0, wxT("invalid column index") );
3171
3172 wxListHeaderData *column = node->GetData();
3173 return column->GetWidth();
3174 }
3175
3176 // ----------------------------------------------------------------------------
3177 // item state
3178 // ----------------------------------------------------------------------------
3179
3180 void wxListMainWindow::SetItem( wxListItem &item )
3181 {
3182 long id = item.m_itemId;
3183 wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
3184 wxT("invalid item index in SetItem") );
3185
3186 if ( !IsVirtual() )
3187 {
3188 wxListLineData *line = GetLine((size_t)id);
3189 line->SetItem( item.m_col, item );
3190
3191 // Set item state if user wants
3192 if ( item.m_mask & wxLIST_MASK_STATE )
3193 SetItemState( item.m_itemId, item.m_state, item.m_state );
3194
3195 if (InReportView())
3196 {
3197 // update the Max Width Cache if needed
3198 int width = GetItemWidthWithImage(&item);
3199
3200 if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
3201 m_aColWidths.Item(item.m_col)->nMaxWidth = width;
3202 }
3203 }
3204
3205 // update the item on screen
3206 wxRect rectItem;
3207 GetItemRect(id, rectItem);
3208 RefreshRect(rectItem);
3209 }
3210
3211 void wxListMainWindow::SetItemStateAll(long state, long stateMask)
3212 {
3213 if ( IsEmpty() )
3214 return;
3215
3216 // first deal with selection
3217 if ( stateMask & wxLIST_STATE_SELECTED )
3218 {
3219 // set/clear select state
3220 if ( IsVirtual() )
3221 {
3222 // optimized version for virtual listctrl.
3223 m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
3224 Refresh();
3225 }
3226 else if ( state & wxLIST_STATE_SELECTED )
3227 {
3228 const long count = GetItemCount();
3229 for( long i = 0; i < count; i++ )
3230 {
3231 SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
3232 }
3233
3234 }
3235 else
3236 {
3237 // clear for non virtual (somewhat optimized by using GetNextItem())
3238 long i = -1;
3239 while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
3240 {
3241 SetItemState( i, 0, wxLIST_STATE_SELECTED );
3242 }
3243 }
3244 }
3245
3246 if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
3247 {
3248 // unfocus all: only one item can be focussed, so clearing focus for
3249 // all items is simply clearing focus of the focussed item.
3250 SetItemState(m_current, state, stateMask);
3251 }
3252 //(setting focus to all items makes no sense, so it is not handled here.)
3253 }
3254
3255 void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
3256 {
3257 if ( litem == -1 )
3258 {
3259 SetItemStateAll(state, stateMask);
3260 return;
3261 }
3262
3263 wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
3264 wxT("invalid list ctrl item index in SetItem") );
3265
3266 size_t oldCurrent = m_current;
3267 size_t item = (size_t)litem; // safe because of the check above
3268
3269 // do we need to change the focus?
3270 if ( stateMask & wxLIST_STATE_FOCUSED )
3271 {
3272 if ( state & wxLIST_STATE_FOCUSED )
3273 {
3274 // don't do anything if this item is already focused
3275 if ( item != m_current )
3276 {
3277 ChangeCurrent(item);
3278
3279 if ( oldCurrent != (size_t)-1 )
3280 {
3281 if ( IsSingleSel() )
3282 {
3283 HighlightLine(oldCurrent, false);
3284 }
3285
3286 RefreshLine(oldCurrent);
3287 }
3288
3289 RefreshLine( m_current );
3290 }
3291 }
3292 else // unfocus
3293 {
3294 // don't do anything if this item is not focused
3295 if ( item == m_current )
3296 {
3297 ResetCurrent();
3298
3299 if ( IsSingleSel() )
3300 {
3301 // we must unselect the old current item as well or we
3302 // might end up with more than one selected item in a
3303 // single selection control
3304 HighlightLine(oldCurrent, false);
3305 }
3306
3307 RefreshLine( oldCurrent );
3308 }
3309 }
3310 }
3311
3312 // do we need to change the selection state?
3313 if ( stateMask & wxLIST_STATE_SELECTED )
3314 {
3315 bool on = (state & wxLIST_STATE_SELECTED) != 0;
3316
3317 if ( IsSingleSel() )
3318 {
3319 if ( on )
3320 {
3321 // selecting the item also makes it the focused one in the
3322 // single sel mode
3323 if ( m_current != item )
3324 {
3325 ChangeCurrent(item);
3326
3327 if ( oldCurrent != (size_t)-1 )
3328 {
3329 HighlightLine( oldCurrent, false );
3330 RefreshLine( oldCurrent );
3331 }
3332 }
3333 }
3334 else // off
3335 {
3336 // only the current item may be selected anyhow
3337 if ( item != m_current )
3338 return;
3339 }
3340 }
3341
3342 if ( HighlightLine(item, on) )
3343 {
3344 RefreshLine(item);
3345 }
3346 }
3347 }
3348
3349 int wxListMainWindow::GetItemState( long item, long stateMask ) const
3350 {
3351 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
3352 wxT("invalid list ctrl item index in GetItemState()") );
3353
3354 int ret = wxLIST_STATE_DONTCARE;
3355
3356 if ( stateMask & wxLIST_STATE_FOCUSED )
3357 {
3358 if ( (size_t)item == m_current )
3359 ret |= wxLIST_STATE_FOCUSED;
3360 }
3361
3362 if ( stateMask & wxLIST_STATE_SELECTED )
3363 {
3364 if ( IsHighlighted(item) )
3365 ret |= wxLIST_STATE_SELECTED;
3366 }
3367
3368 return ret;
3369 }
3370
3371 void wxListMainWindow::GetItem( wxListItem &item ) const
3372 {
3373 wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
3374 wxT("invalid item index in GetItem") );
3375
3376 wxListLineData *line = GetLine((size_t)item.m_itemId);
3377 line->GetItem( item.m_col, item );
3378
3379 // Get item state if user wants it
3380 if ( item.m_mask & wxLIST_MASK_STATE )
3381 item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
3382 wxLIST_STATE_FOCUSED );
3383 }
3384
3385 // ----------------------------------------------------------------------------
3386 // item count
3387 // ----------------------------------------------------------------------------
3388
3389 size_t wxListMainWindow::GetItemCount() const
3390 {
3391 return IsVirtual() ? m_countVirt : m_lines.GetCount();
3392 }
3393
3394 void wxListMainWindow::SetItemCount(long count)
3395 {
3396 m_selStore.SetItemCount(count);
3397 m_countVirt = count;
3398
3399 ResetVisibleLinesRange();
3400
3401 // scrollbars must be reset
3402 m_dirty = true;
3403 }
3404
3405 int wxListMainWindow::GetSelectedItemCount() const
3406 {
3407 // deal with the quick case first
3408 if ( IsSingleSel() )
3409 return HasCurrent() ? IsHighlighted(m_current) : false;
3410
3411 // virtual controls remmebers all its selections itself
3412 if ( IsVirtual() )
3413 return m_selStore.GetSelectedCount();
3414
3415 // TODO: we probably should maintain the number of items selected even for
3416 // non virtual controls as enumerating all lines is really slow...
3417 size_t countSel = 0;
3418 size_t count = GetItemCount();
3419 for ( size_t line = 0; line < count; line++ )
3420 {
3421 if ( GetLine(line)->IsHighlighted() )
3422 countSel++;
3423 }
3424
3425 return countSel;
3426 }
3427
3428 // ----------------------------------------------------------------------------
3429 // item position/size
3430 // ----------------------------------------------------------------------------
3431
3432 wxRect wxListMainWindow::GetViewRect() const
3433 {
3434 wxASSERT_MSG( !HasFlag(wxLC_LIST), "not implemented for list view" );
3435
3436 // we need to find the longest/tallest label
3437 wxCoord xMax = 0, yMax = 0;
3438 const int count = GetItemCount();
3439 if ( count )
3440 {
3441 for ( int i = 0; i < count; i++ )
3442 {
3443 // we need logical, not physical, coordinates here, so use
3444 // GetLineRect() instead of GetItemRect()
3445 wxRect r = GetLineRect(i);
3446
3447 wxCoord x = r.GetRight(),
3448 y = r.GetBottom();
3449
3450 if ( x > xMax )
3451 xMax = x;
3452 if ( y > yMax )
3453 yMax = y;
3454 }
3455 }
3456
3457 // some fudge needed to make it look prettier
3458 xMax += 2 * EXTRA_BORDER_X;
3459 yMax += 2 * EXTRA_BORDER_Y;
3460
3461 // account for the scrollbars if necessary
3462 const wxSize sizeAll = GetClientSize();
3463 if ( xMax > sizeAll.x )
3464 yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
3465 if ( yMax > sizeAll.y )
3466 xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
3467
3468 return wxRect(0, 0, xMax, yMax);
3469 }
3470
3471 bool
3472 wxListMainWindow::GetSubItemRect(long item, long subItem, wxRect& rect) const
3473 {
3474 wxCHECK_MSG( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM || InReportView(),
3475 false,
3476 wxT("GetSubItemRect only meaningful in report view") );
3477 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), false,
3478 wxT("invalid item in GetSubItemRect") );
3479
3480 // ensure that we're laid out, otherwise we could return nonsense
3481 if ( m_dirty )
3482 {
3483 wxConstCast(this, wxListMainWindow)->
3484 RecalculatePositions(true /* no refresh */);
3485 }
3486
3487 rect = GetLineRect((size_t)item);
3488
3489 // Adjust rect to specified column
3490 if ( subItem != wxLIST_GETSUBITEMRECT_WHOLEITEM )
3491 {
3492 wxCHECK_MSG( subItem >= 0 && subItem < GetColumnCount(), false,
3493 wxT("invalid subItem in GetSubItemRect") );
3494
3495 for (int i = 0; i < subItem; i++)
3496 {
3497 rect.x += GetColumnWidth(i);
3498 }
3499 rect.width = GetColumnWidth(subItem);
3500 }
3501
3502 GetListCtrl()->CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
3503
3504 return true;
3505 }
3506
3507 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
3508 {
3509 wxRect rect;
3510 GetItemRect(item, rect);
3511
3512 pos.x = rect.x;
3513 pos.y = rect.y;
3514
3515 return true;
3516 }
3517
3518 // ----------------------------------------------------------------------------
3519 // geometry calculation
3520 // ----------------------------------------------------------------------------
3521
3522 void wxListMainWindow::RecalculatePositions(bool noRefresh)
3523 {
3524 const int lineHeight = GetLineHeight();
3525
3526 wxClientDC dc( this );
3527 dc.SetFont( GetFont() );
3528
3529 const size_t count = GetItemCount();
3530
3531 int iconSpacing;
3532 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
3533 iconSpacing = m_normal_spacing;
3534 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
3535 iconSpacing = m_small_spacing;
3536 else
3537 iconSpacing = 0;
3538
3539 // Note that we do not call GetClientSize() here but
3540 // GetSize() and subtract the border size for sunken
3541 // borders manually. This is technically incorrect,
3542 // but we need to know the client area's size WITHOUT
3543 // scrollbars here. Since we don't know if there are
3544 // any scrollbars, we use GetSize() instead. Another
3545 // solution would be to call SetScrollbars() here to
3546 // remove the scrollbars and call GetClientSize() then,
3547 // but this might result in flicker and - worse - will
3548 // reset the scrollbars to 0 which is not good at all
3549 // if you resize a dialog/window, but don't want to
3550 // reset the window scrolling. RR.
3551 // Furthermore, we actually do NOT subtract the border
3552 // width as 2 pixels is just the extra space which we
3553 // need around the actual content in the window. Other-
3554 // wise the text would e.g. touch the upper border. RR.
3555 int clientWidth,
3556 clientHeight;
3557 GetSize( &clientWidth, &clientHeight );
3558
3559 if ( InReportView() )
3560 {
3561 // all lines have the same height and we scroll one line per step
3562 int entireHeight = count * lineHeight + LINE_SPACING;
3563
3564 m_linesPerPage = clientHeight / lineHeight;
3565
3566 ResetVisibleLinesRange();
3567
3568 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X, lineHeight,
3569 GetHeaderWidth() / SCROLL_UNIT_X,
3570 (entireHeight + lineHeight - 1) / lineHeight,
3571 GetListCtrl()->GetScrollPos(wxHORIZONTAL),
3572 GetListCtrl()->GetScrollPos(wxVERTICAL),
3573 true );
3574 }
3575 else // !report
3576 {
3577 // we have 3 different layout strategies: either layout all items
3578 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3579 // to arrange them in top to bottom, left to right (don't ask me why
3580 // not the other way round...) order
3581 if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) )
3582 {
3583 int x = EXTRA_BORDER_X;
3584 int y = EXTRA_BORDER_Y;
3585
3586 wxCoord widthMax = 0;
3587
3588 size_t i;
3589 for ( i = 0; i < count; i++ )
3590 {
3591 wxListLineData *line = GetLine(i);
3592 line->CalculateSize( &dc, iconSpacing );
3593 line->SetPosition( x, y, iconSpacing );
3594
3595 wxSize sizeLine = GetLineSize(i);
3596
3597 if ( HasFlag(wxLC_ALIGN_TOP) )
3598 {
3599 if ( sizeLine.x > widthMax )
3600 widthMax = sizeLine.x;
3601
3602 y += sizeLine.y;
3603 }
3604 else // wxLC_ALIGN_LEFT
3605 {
3606 x += sizeLine.x + MARGIN_BETWEEN_ROWS;
3607 }
3608 }
3609
3610 if ( HasFlag(wxLC_ALIGN_TOP) )
3611 {
3612 // traverse the items again and tweak their sizes so that they are
3613 // all the same in a row
3614 for ( i = 0; i < count; i++ )
3615 {
3616 wxListLineData *line = GetLine(i);
3617 line->m_gi->ExtendWidth(widthMax);
3618 }
3619 }
3620
3621 GetListCtrl()->SetScrollbars
3622 (
3623 SCROLL_UNIT_X,
3624 lineHeight,
3625 (x + SCROLL_UNIT_X) / SCROLL_UNIT_X,
3626 (y + lineHeight) / lineHeight,
3627 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
3628 GetListCtrl()->GetScrollPos( wxVERTICAL ),
3629 true
3630 );
3631 }
3632 else // "flowed" arrangement, the most complicated case
3633 {
3634 // at first we try without any scrollbars, if the items don't fit into
3635 // the window, we recalculate after subtracting the space taken by the
3636 // scrollbar
3637
3638 int entireWidth = 0;
3639
3640 for (int tries = 0; tries < 2; tries++)
3641 {
3642 entireWidth = 2 * EXTRA_BORDER_X;
3643
3644 if (tries == 1)
3645 {
3646 // Now we have decided that the items do not fit into the
3647 // client area, so we need a scrollbar
3648 entireWidth += SCROLL_UNIT_X;
3649 }
3650
3651 int x = EXTRA_BORDER_X;
3652 int y = EXTRA_BORDER_Y;
3653 int maxWidthInThisRow = 0;
3654
3655 m_linesPerPage = 0;
3656 int currentlyVisibleLines = 0;
3657
3658 for (size_t i = 0; i < count; i++)
3659 {
3660 currentlyVisibleLines++;
3661 wxListLineData *line = GetLine( i );
3662 line->CalculateSize( &dc, iconSpacing );
3663 line->SetPosition( x, y, iconSpacing );
3664
3665 wxSize sizeLine = GetLineSize( i );
3666
3667 if ( maxWidthInThisRow < sizeLine.x )
3668 maxWidthInThisRow = sizeLine.x;
3669
3670 y += sizeLine.y;
3671 if (currentlyVisibleLines > m_linesPerPage)
3672 m_linesPerPage = currentlyVisibleLines;
3673
3674 if ( y + sizeLine.y >= clientHeight )
3675 {
3676 currentlyVisibleLines = 0;
3677 y = EXTRA_BORDER_Y;
3678 maxWidthInThisRow += MARGIN_BETWEEN_ROWS;
3679 x += maxWidthInThisRow;
3680 entireWidth += maxWidthInThisRow;
3681 maxWidthInThisRow = 0;
3682 }
3683
3684 // We have reached the last item.
3685 if ( i == count - 1 )
3686 entireWidth += maxWidthInThisRow;
3687
3688 if ( (tries == 0) &&
3689 (entireWidth + SCROLL_UNIT_X > clientWidth) )
3690 {
3691 clientHeight -= wxSystemSettings::
3692 GetMetric(wxSYS_HSCROLL_Y);
3693 m_linesPerPage = 0;
3694 break;
3695 }
3696
3697 if ( i == count - 1 )
3698 tries = 1; // Everything fits, no second try required.
3699 }
3700 }
3701
3702 GetListCtrl()->SetScrollbars
3703 (
3704 SCROLL_UNIT_X,
3705 lineHeight,
3706 (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X,
3707 0,
3708 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
3709 0,
3710 true
3711 );
3712 }
3713 }
3714
3715 if ( !noRefresh )
3716 {
3717 // FIXME: why should we call it from here?
3718 UpdateCurrent();
3719
3720 RefreshAll();
3721 }
3722 }
3723
3724 void wxListMainWindow::RefreshAll()
3725 {
3726 m_dirty = false;
3727 Refresh();
3728
3729 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3730 if ( headerWin && headerWin->m_dirty )
3731 {
3732 headerWin->m_dirty = false;
3733 headerWin->Refresh();
3734 }
3735 }
3736
3737 void wxListMainWindow::UpdateCurrent()
3738 {
3739 if ( !HasCurrent() && !IsEmpty() )
3740 ChangeCurrent(0);
3741 }
3742
3743 long wxListMainWindow::GetNextItem( long item,
3744 int WXUNUSED(geometry),
3745 int state ) const
3746 {
3747 long ret = item,
3748 max = GetItemCount();
3749 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
3750 wxT("invalid listctrl index in GetNextItem()") );
3751
3752 // notice that we start with the next item (or the first one if item == -1)
3753 // and this is intentional to allow writing a simple loop to iterate over
3754 // all selected items
3755 ret++;
3756 if ( ret == max )
3757 // this is not an error because the index was OK initially,
3758 // just no such item
3759 return -1;
3760
3761 if ( !state )
3762 // any will do
3763 return (size_t)ret;
3764
3765 size_t count = GetItemCount();
3766 for ( size_t line = (size_t)ret; line < count; line++ )
3767 {
3768 if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
3769 return line;
3770
3771 if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
3772 return line;
3773 }
3774
3775 return -1;
3776 }
3777
3778 // ----------------------------------------------------------------------------
3779 // deleting stuff
3780 // ----------------------------------------------------------------------------
3781
3782 void wxListMainWindow::DeleteItem( long lindex )
3783 {
3784 size_t count = GetItemCount();
3785
3786 wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
3787 wxT("invalid item index in DeleteItem") );
3788
3789 size_t index = (size_t)lindex;
3790
3791 // we don't need to adjust the index for the previous items
3792 if ( HasCurrent() && m_current >= index )
3793 {
3794 // if the current item is being deleted, we want the next one to
3795 // become selected - unless there is no next one - so don't adjust
3796 // m_current in this case
3797 if ( m_current != index || m_current == count - 1 )
3798 m_current--;
3799 }
3800
3801 if ( InReportView() )
3802 {
3803 // mark the Column Max Width cache as dirty if the items in the line
3804 // we're deleting contain the Max Column Width
3805 wxListLineData * const line = GetLine(index);
3806 wxListItemDataList::compatibility_iterator n;
3807 wxListItemData *itemData;
3808 wxListItem item;
3809 int itemWidth;
3810
3811 for (size_t i = 0; i < m_columns.GetCount(); i++)
3812 {
3813 n = line->m_items.Item( i );
3814 itemData = n->GetData();
3815 itemData->GetItem(item);
3816
3817 itemWidth = GetItemWidthWithImage(&item);
3818
3819 if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth)
3820 m_aColWidths.Item(i)->bNeedsUpdate = true;
3821 }
3822
3823 ResetVisibleLinesRange();
3824 }
3825
3826 SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM, wxDefaultPosition );
3827
3828 if ( IsVirtual() )
3829 {
3830 m_countVirt--;
3831 m_selStore.OnItemDelete(index);
3832 }
3833 else
3834 {
3835 m_lines.RemoveAt( index );
3836 }
3837
3838 // we need to refresh the (vert) scrollbar as the number of items changed
3839 m_dirty = true;
3840
3841 RefreshAfter(index);
3842 }
3843
3844 void wxListMainWindow::DeleteColumn( int col )
3845 {
3846 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3847
3848 wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
3849
3850 m_dirty = true;
3851 delete node->GetData();
3852 m_columns.Erase( node );
3853
3854 if ( !IsVirtual() )
3855 {
3856 // update all the items
3857 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
3858 {
3859 wxListLineData * const line = GetLine(i);
3860 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3861 delete n->GetData();
3862 line->m_items.Erase(n);
3863 }
3864 }
3865
3866 if ( InReportView() ) // we only cache max widths when in Report View
3867 {
3868 delete m_aColWidths.Item(col);
3869 m_aColWidths.RemoveAt(col);
3870 }
3871
3872 // invalidate it as it has to be recalculated
3873 m_headerWidth = 0;
3874 }
3875
3876 void wxListMainWindow::DoDeleteAllItems()
3877 {
3878 if ( IsEmpty() )
3879 // nothing to do - in particular, don't send the event
3880 return;
3881
3882 ResetCurrent();
3883
3884 // to make the deletion of all items faster, we don't send the
3885 // notifications for each item deletion in this case but only one event
3886 // for all of them: this is compatible with wxMSW and documented in
3887 // DeleteAllItems() description
3888
3889 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
3890 event.SetEventObject( GetParent() );
3891 GetParent()->GetEventHandler()->ProcessEvent( event );
3892
3893 if ( IsVirtual() )
3894 {
3895 m_countVirt = 0;
3896 m_selStore.Clear();
3897 }
3898
3899 if ( InReportView() )
3900 {
3901 ResetVisibleLinesRange();
3902 for (size_t i = 0; i < m_aColWidths.GetCount(); i++)
3903 {
3904 m_aColWidths.Item(i)->bNeedsUpdate = true;
3905 }
3906 }
3907
3908 m_lines.Clear();
3909 }
3910
3911 void wxListMainWindow::DeleteAllItems()
3912 {
3913 DoDeleteAllItems();
3914
3915 RecalculatePositions();
3916 }
3917
3918 void wxListMainWindow::DeleteEverything()
3919 {
3920 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
3921 WX_CLEAR_ARRAY(m_aColWidths);
3922
3923 DeleteAllItems();
3924 }
3925
3926 // ----------------------------------------------------------------------------
3927 // scanning for an item
3928 // ----------------------------------------------------------------------------
3929
3930 void wxListMainWindow::EnsureVisible( long index )
3931 {
3932 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
3933 wxT("invalid index in EnsureVisible") );
3934
3935 // We have to call this here because the label in question might just have
3936 // been added and its position is not known yet
3937 if ( m_dirty )
3938 RecalculatePositions(true /* no refresh */);
3939
3940 MoveToItem((size_t)index);
3941 }
3942
3943 long wxListMainWindow::FindItem(long start, const wxString& str, bool partial )
3944 {
3945 if (str.empty())
3946 return wxNOT_FOUND;
3947
3948 long pos = start;
3949 wxString str_upper = str.Upper();
3950 if (pos < 0)
3951 pos = 0;
3952
3953 size_t count = GetItemCount();
3954 for ( size_t i = (size_t)pos; i < count; i++ )
3955 {
3956 wxListLineData *line = GetLine(i);
3957 wxString line_upper = line->GetText(0).Upper();
3958 if (!partial)
3959 {
3960 if (line_upper == str_upper )
3961 return i;
3962 }
3963 else
3964 {
3965 if (line_upper.find(str_upper) == 0)
3966 return i;
3967 }
3968 }
3969
3970 return wxNOT_FOUND;
3971 }
3972
3973 long wxListMainWindow::FindItem(long start, wxUIntPtr data)
3974 {
3975 long pos = start;
3976 if (pos < 0)
3977 pos = 0;
3978
3979 size_t count = GetItemCount();
3980 for (size_t i = (size_t)pos; i < count; i++)
3981 {
3982 wxListLineData *line = GetLine(i);
3983 wxListItem item;
3984 line->GetItem( 0, item );
3985 if (item.m_data == data)
3986 return i;
3987 }
3988
3989 return wxNOT_FOUND;
3990 }
3991
3992 long wxListMainWindow::FindItem( const wxPoint& pt )
3993 {
3994 size_t topItem;
3995 GetVisibleLinesRange( &topItem, NULL );
3996
3997 wxPoint p;
3998 GetItemPosition( GetItemCount() - 1, p );
3999 if ( p.y == 0 )
4000 return topItem;
4001
4002 long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem );
4003 if ( id >= 0 && id < (long)GetItemCount() )
4004 return id;
4005
4006 return wxNOT_FOUND;
4007 }
4008
4009 long wxListMainWindow::HitTest( int x, int y, int &flags ) const
4010 {
4011 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
4012
4013 size_t count = GetItemCount();
4014
4015 if ( InReportView() )
4016 {
4017 size_t current = y / GetLineHeight();
4018 if ( current < count )
4019 {
4020 flags = HitTestLine(current, x, y);
4021 if ( flags )
4022 return current;
4023 }
4024 }
4025 else // !report
4026 {
4027 // TODO: optimize it too! this is less simple than for report view but
4028 // enumerating all items is still not a way to do it!!
4029 for ( size_t current = 0; current < count; current++ )
4030 {
4031 flags = HitTestLine(current, x, y);
4032 if ( flags )
4033 return current;
4034 }
4035 }
4036
4037 return wxNOT_FOUND;
4038 }
4039
4040 // ----------------------------------------------------------------------------
4041 // adding stuff
4042 // ----------------------------------------------------------------------------
4043
4044 void wxListMainWindow::InsertItem( wxListItem &item )
4045 {
4046 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
4047
4048 int count = GetItemCount();
4049 wxCHECK_RET( item.m_itemId >= 0, wxT("invalid item index") );
4050
4051 if (item.m_itemId > count)
4052 item.m_itemId = count;
4053
4054 size_t id = item.m_itemId;
4055
4056 m_dirty = true;
4057
4058 if ( InReportView() )
4059 {
4060 ResetVisibleLinesRange();
4061
4062 // calculate the width of the item and adjust the max column width
4063 wxColWidthInfo *pWidthInfo = m_aColWidths.Item(item.GetColumn());
4064 int width = GetItemWidthWithImage(&item);
4065 item.SetWidth(width);
4066 if (width > pWidthInfo->nMaxWidth)
4067 pWidthInfo->nMaxWidth = width;
4068 }
4069
4070 wxListLineData *line = new wxListLineData(this);
4071
4072 line->SetItem( item.m_col, item );
4073
4074 m_lines.Insert( line, id );
4075
4076 m_dirty = true;
4077
4078 // If an item is selected at or below the point of insertion, we need to
4079 // increment the member variables because the current row's index has gone
4080 // up by one
4081 if ( HasCurrent() && m_current >= id )
4082 m_current++;
4083
4084 SendNotify(id, wxEVT_COMMAND_LIST_INSERT_ITEM);
4085
4086 RefreshLines(id, GetItemCount() - 1);
4087 }
4088
4089 void wxListMainWindow::InsertColumn( long col, wxListItem &item )
4090 {
4091 m_dirty = true;
4092 if ( InReportView() )
4093 {
4094 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
4095 item.m_width = GetTextLength( item.m_text );
4096
4097 wxListHeaderData *column = new wxListHeaderData( item );
4098 wxColWidthInfo *colWidthInfo = new wxColWidthInfo();
4099
4100 bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4101 if ( insert )
4102 {
4103 wxListHeaderDataList::compatibility_iterator
4104 node = m_columns.Item( col );
4105 m_columns.Insert( node, column );
4106 m_aColWidths.Insert( colWidthInfo, col );
4107 }
4108 else
4109 {
4110 m_columns.Append( column );
4111 m_aColWidths.Add( colWidthInfo );
4112 }
4113
4114 if ( !IsVirtual() )
4115 {
4116 // update all the items
4117 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4118 {
4119 wxListLineData * const line = GetLine(i);
4120 wxListItemData * const data = new wxListItemData(this);
4121 if ( insert )
4122 line->m_items.Insert(col, data);
4123 else
4124 line->m_items.Append(data);
4125 }
4126 }
4127
4128 // invalidate it as it has to be recalculated
4129 m_headerWidth = 0;
4130 }
4131 }
4132
4133 int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
4134 {
4135 int width = 0;
4136 wxClientDC dc(this);
4137
4138 dc.SetFont( GetFont() );
4139
4140 if (item->GetImage() != -1)
4141 {
4142 int ix, iy;
4143 GetImageSize( item->GetImage(), ix, iy );
4144 width += ix + 5;
4145 }
4146
4147 if (!item->GetText().empty())
4148 {
4149 wxCoord w;
4150 dc.GetTextExtent( item->GetText(), &w, NULL );
4151 width += w;
4152 }
4153
4154 return width;
4155 }
4156
4157 // ----------------------------------------------------------------------------
4158 // sorting
4159 // ----------------------------------------------------------------------------
4160
4161 static wxListCtrlCompare list_ctrl_compare_func_2;
4162 static wxIntPtr list_ctrl_compare_data;
4163
4164 int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
4165 {
4166 wxListLineData *line1 = *arg1;
4167 wxListLineData *line2 = *arg2;
4168 wxListItem item;
4169 line1->GetItem( 0, item );
4170 wxUIntPtr data1 = item.m_data;
4171 line2->GetItem( 0, item );
4172 wxUIntPtr data2 = item.m_data;
4173 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
4174 }
4175
4176 void wxListMainWindow::SortItems( wxListCtrlCompare fn, wxIntPtr data )
4177 {
4178 // selections won't make sense any more after sorting the items so reset
4179 // them
4180 HighlightAll(false);
4181 ResetCurrent();
4182
4183 list_ctrl_compare_func_2 = fn;
4184 list_ctrl_compare_data = data;
4185 m_lines.Sort( list_ctrl_compare_func_1 );
4186 m_dirty = true;
4187 }
4188
4189 // ----------------------------------------------------------------------------
4190 // scrolling
4191 // ----------------------------------------------------------------------------
4192
4193 void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4194 {
4195 // update our idea of which lines are shown when we redraw the window the
4196 // next time
4197 ResetVisibleLinesRange();
4198
4199 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4200 {
4201 wxGenericListCtrl* lc = GetListCtrl();
4202 wxCHECK_RET( lc, wxT("no listctrl window?") );
4203
4204 if (lc->m_headerWin) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4205 {
4206 lc->m_headerWin->Refresh();
4207 lc->m_headerWin->Update();
4208 }
4209 }
4210 }
4211
4212 int wxListMainWindow::GetCountPerPage() const
4213 {
4214 if ( !m_linesPerPage )
4215 {
4216 wxConstCast(this, wxListMainWindow)->
4217 m_linesPerPage = GetClientSize().y / GetLineHeight();
4218 }
4219
4220 return m_linesPerPage;
4221 }
4222
4223 void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
4224 {
4225 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
4226
4227 if ( m_lineFrom == (size_t)-1 )
4228 {
4229 size_t count = GetItemCount();
4230 if ( count )
4231 {
4232 m_lineFrom = GetListCtrl()->GetScrollPos(wxVERTICAL);
4233
4234 // this may happen if SetScrollbars() hadn't been called yet
4235 if ( m_lineFrom >= count )
4236 m_lineFrom = count - 1;
4237
4238 // we redraw one extra line but this is needed to make the redrawing
4239 // logic work when there is a fractional number of lines on screen
4240 m_lineTo = m_lineFrom + m_linesPerPage;
4241 if ( m_lineTo >= count )
4242 m_lineTo = count - 1;
4243 }
4244 else // empty control
4245 {
4246 m_lineFrom = 0;
4247 m_lineTo = (size_t)-1;
4248 }
4249 }
4250
4251 wxASSERT_MSG( IsEmpty() ||
4252 (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
4253 wxT("GetVisibleLinesRange() returns incorrect result") );
4254
4255 if ( from )
4256 *from = m_lineFrom;
4257 if ( to )
4258 *to = m_lineTo;
4259 }
4260
4261 // -------------------------------------------------------------------------------------
4262 // wxGenericListCtrl
4263 // -------------------------------------------------------------------------------------
4264
4265 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl, wxControl)
4266
4267 BEGIN_EVENT_TABLE(wxGenericListCtrl,wxControl)
4268 EVT_SIZE(wxGenericListCtrl::OnSize)
4269 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll)
4270 END_EVENT_TABLE()
4271
4272 void wxGenericListCtrl::Init()
4273 {
4274 m_imageListNormal = NULL;
4275 m_imageListSmall = NULL;
4276 m_imageListState = NULL;
4277
4278 m_ownsImageListNormal =
4279 m_ownsImageListSmall =
4280 m_ownsImageListState = false;
4281
4282 m_mainWin = NULL;
4283 m_headerWin = NULL;
4284 }
4285
4286 wxGenericListCtrl::~wxGenericListCtrl()
4287 {
4288 if (m_ownsImageListNormal)
4289 delete m_imageListNormal;
4290 if (m_ownsImageListSmall)
4291 delete m_imageListSmall;
4292 if (m_ownsImageListState)
4293 delete m_imageListState;
4294 }
4295
4296 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
4297 {
4298 bool needs_header = HasHeader();
4299 bool has_header = (m_headerWin != NULL);
4300
4301 if (needs_header == has_header)
4302 return;
4303
4304 if (needs_header)
4305 {
4306 m_headerWin = new wxListHeaderWindow
4307 (
4308 this, wxID_ANY, m_mainWin,
4309 wxPoint(0,0),
4310 wxSize
4311 (
4312 GetClientSize().x,
4313 wxRendererNative::Get().GetHeaderButtonHeight(this)
4314 ),
4315 wxTAB_TRAVERSAL
4316 );
4317
4318 #if defined( __WXMAC__ )
4319 static wxFont font( wxOSX_SYSTEM_FONT_SMALL );
4320 m_headerWin->SetFont( font );
4321 #endif
4322
4323 GetSizer()->Prepend( m_headerWin, 0, wxGROW );
4324 }
4325 else
4326 {
4327 GetSizer()->Detach( m_headerWin );
4328
4329 delete m_headerWin;
4330
4331 m_headerWin = NULL;
4332 }
4333 }
4334
4335 bool wxGenericListCtrl::Create(wxWindow *parent,
4336 wxWindowID id,
4337 const wxPoint &pos,
4338 const wxSize &size,
4339 long style,
4340 const wxValidator &validator,
4341 const wxString &name)
4342 {
4343 Init();
4344
4345 // just like in other ports, an assert will fail if the user doesn't give any type style:
4346 wxASSERT_MSG( (style & wxLC_MASK_TYPE),
4347 wxT("wxListCtrl style should have exactly one mode bit set") );
4348
4349 if ( !wxControl::Create( parent, id, pos, size, style|wxVSCROLL|wxHSCROLL, validator, name ) )
4350 return false;
4351
4352 #ifdef __WXGTK__
4353 style &= ~wxBORDER_MASK;
4354 style |= wxBORDER_THEME;
4355 #endif
4356
4357 m_mainWin = new wxListMainWindow( this, wxID_ANY, wxPoint(0, 0), size, style );
4358
4359 SetTargetWindow( m_mainWin );
4360
4361 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
4362 sizer->Add( m_mainWin, 1, wxGROW );
4363 SetSizer( sizer );
4364
4365 CreateOrDestroyHeaderWindowAsNeeded();
4366
4367 SetInitialSize(size);
4368
4369 return true;
4370 }
4371
4372 wxBorder wxGenericListCtrl::GetDefaultBorder() const
4373 {
4374 return wxBORDER_THEME;
4375 }
4376
4377 #if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
4378 WXLRESULT wxGenericListCtrl::MSWWindowProc(WXUINT nMsg,
4379 WXWPARAM wParam,
4380 WXLPARAM lParam)
4381 {
4382 WXLRESULT rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
4383
4384 // we need to process arrows ourselves for scrolling
4385 if ( nMsg == WM_GETDLGCODE )
4386 {
4387 rc |= DLGC_WANTARROWS;
4388 }
4389
4390 return rc;
4391 }
4392 #endif // __WXMSW__
4393
4394 wxSize wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
4395 {
4396 wxSize newsize = size;
4397 if (m_headerWin)
4398 newsize.y -= m_headerWin->GetSize().y;
4399
4400 return newsize;
4401 }
4402
4403 void wxGenericListCtrl::OnScroll(wxScrollWinEvent& event)
4404 {
4405 // update our idea of which lines are shown when we redraw
4406 // the window the next time
4407 m_mainWin->ResetVisibleLinesRange();
4408
4409 HandleOnScroll( event );
4410
4411 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4412 {
4413 m_headerWin->Refresh();
4414 m_headerWin->Update();
4415 }
4416 }
4417
4418 void wxGenericListCtrl::SetSingleStyle( long style, bool add )
4419 {
4420 wxASSERT_MSG( !(style & wxLC_VIRTUAL),
4421 wxT("wxLC_VIRTUAL can't be [un]set") );
4422
4423 long flag = GetWindowStyle();
4424
4425 if (add)
4426 {
4427 if (style & wxLC_MASK_TYPE)
4428 flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
4429 if (style & wxLC_MASK_ALIGN)
4430 flag &= ~wxLC_MASK_ALIGN;
4431 if (style & wxLC_MASK_SORT)
4432 flag &= ~wxLC_MASK_SORT;
4433 }
4434
4435 if (add)
4436 flag |= style;
4437 else
4438 flag &= ~style;
4439
4440 // some styles can be set without recreating everything (as happens in
4441 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4442 if ( !(style & ~(wxLC_HRULES | wxLC_VRULES)) )
4443 {
4444 Refresh();
4445 wxWindow::SetWindowStyleFlag(flag);
4446 }
4447 else
4448 {
4449 SetWindowStyleFlag( flag );
4450 }
4451 }
4452
4453 void wxGenericListCtrl::SetWindowStyleFlag( long flag )
4454 {
4455 const bool wasInReportView = HasFlag(wxLC_REPORT);
4456
4457 // update the window style first so that the header is created or destroyed
4458 // corresponding to the new style
4459 wxWindow::SetWindowStyleFlag( flag );
4460
4461 if (m_mainWin)
4462 {
4463 const bool inReportView = (flag & wxLC_REPORT) != 0;
4464 if ( inReportView != wasInReportView )
4465 {
4466 // we need to notify the main window about this change as it must
4467 // update its data structures
4468 m_mainWin->SetReportView(inReportView);
4469 }
4470
4471 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
4472
4473 CreateOrDestroyHeaderWindowAsNeeded();
4474
4475 GetSizer()->Layout();
4476 }
4477 }
4478
4479 bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
4480 {
4481 m_mainWin->GetColumn( col, item );
4482 return true;
4483 }
4484
4485 bool wxGenericListCtrl::SetColumn( int col, wxListItem& item )
4486 {
4487 m_mainWin->SetColumn( col, item );
4488 return true;
4489 }
4490
4491 int wxGenericListCtrl::GetColumnWidth( int col ) const
4492 {
4493 return m_mainWin->GetColumnWidth( col );
4494 }
4495
4496 bool wxGenericListCtrl::SetColumnWidth( int col, int width )
4497 {
4498 m_mainWin->SetColumnWidth( col, width );
4499 return true;
4500 }
4501
4502 int wxGenericListCtrl::GetCountPerPage() const
4503 {
4504 return m_mainWin->GetCountPerPage(); // different from Windows ?
4505 }
4506
4507 bool wxGenericListCtrl::GetItem( wxListItem &info ) const
4508 {
4509 m_mainWin->GetItem( info );
4510 return true;
4511 }
4512
4513 bool wxGenericListCtrl::SetItem( wxListItem &info )
4514 {
4515 m_mainWin->SetItem( info );
4516 return true;
4517 }
4518
4519 long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
4520 {
4521 wxListItem info;
4522 info.m_text = label;
4523 info.m_mask = wxLIST_MASK_TEXT;
4524 info.m_itemId = index;
4525 info.m_col = col;
4526 if ( imageId > -1 )
4527 {
4528 info.m_image = imageId;
4529 info.m_mask |= wxLIST_MASK_IMAGE;
4530 }
4531
4532 m_mainWin->SetItem(info);
4533 return true;
4534 }
4535
4536 int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
4537 {
4538 return m_mainWin->GetItemState( item, stateMask );
4539 }
4540
4541 bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
4542 {
4543 m_mainWin->SetItemState( item, state, stateMask );
4544 return true;
4545 }
4546
4547 bool
4548 wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
4549 {
4550 return SetItemColumnImage(item, 0, image);
4551 }
4552
4553 bool
4554 wxGenericListCtrl::SetItemColumnImage( long item, long column, int image )
4555 {
4556 wxListItem info;
4557 info.m_image = image;
4558 info.m_mask = wxLIST_MASK_IMAGE;
4559 info.m_itemId = item;
4560 info.m_col = column;
4561 m_mainWin->SetItem( info );
4562 return true;
4563 }
4564
4565 wxString wxGenericListCtrl::GetItemText( long item ) const
4566 {
4567 return m_mainWin->GetItemText(item);
4568 }
4569
4570 void wxGenericListCtrl::SetItemText( long item, const wxString& str )
4571 {
4572 m_mainWin->SetItemText(item, str);
4573 }
4574
4575 wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const
4576 {
4577 wxListItem info;
4578 info.m_mask = wxLIST_MASK_DATA;
4579 info.m_itemId = item;
4580 m_mainWin->GetItem( info );
4581 return info.m_data;
4582 }
4583
4584 bool wxGenericListCtrl::SetItemPtrData( long item, wxUIntPtr data )
4585 {
4586 wxListItem info;
4587 info.m_mask = wxLIST_MASK_DATA;
4588 info.m_itemId = item;
4589 info.m_data = data;
4590 m_mainWin->SetItem( info );
4591 return true;
4592 }
4593
4594 wxRect wxGenericListCtrl::GetViewRect() const
4595 {
4596 return m_mainWin->GetViewRect();
4597 }
4598
4599 bool wxGenericListCtrl::GetItemRect(long item, wxRect& rect, int code) const
4600 {
4601 return GetSubItemRect(item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code);
4602 }
4603
4604 bool wxGenericListCtrl::GetSubItemRect(long item,
4605 long subItem,
4606 wxRect& rect,
4607 int WXUNUSED(code)) const
4608 {
4609 if ( !m_mainWin->GetSubItemRect( item, subItem, rect ) )
4610 return false;
4611
4612 if ( m_mainWin->HasHeader() )
4613 rect.y += m_headerWin->GetSize().y + 1;
4614
4615 return true;
4616 }
4617
4618 bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
4619 {
4620 m_mainWin->GetItemPosition( item, pos );
4621 return true;
4622 }
4623
4624 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
4625 {
4626 return false;
4627 }
4628
4629 int wxGenericListCtrl::GetItemCount() const
4630 {
4631 return m_mainWin->GetItemCount();
4632 }
4633
4634 int wxGenericListCtrl::GetColumnCount() const
4635 {
4636 return m_mainWin->GetColumnCount();
4637 }
4638
4639 void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
4640 {
4641 m_mainWin->SetItemSpacing( spacing, isSmall );
4642 }
4643
4644 wxSize wxGenericListCtrl::GetItemSpacing() const
4645 {
4646 const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON));
4647
4648 return wxSize(spacing, spacing);
4649 }
4650
4651 #if WXWIN_COMPATIBILITY_2_6
4652 int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const
4653 {
4654 return m_mainWin->GetItemSpacing( isSmall );
4655 }
4656 #endif // WXWIN_COMPATIBILITY_2_6
4657
4658 void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
4659 {
4660 wxListItem info;
4661 info.m_itemId = item;
4662 info.SetTextColour( col );
4663 m_mainWin->SetItem( info );
4664 }
4665
4666 wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
4667 {
4668 wxListItem info;
4669 info.m_itemId = item;
4670 m_mainWin->GetItem( info );
4671 return info.GetTextColour();
4672 }
4673
4674 void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
4675 {
4676 wxListItem info;
4677 info.m_itemId = item;
4678 info.SetBackgroundColour( col );
4679 m_mainWin->SetItem( info );
4680 }
4681
4682 wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
4683 {
4684 wxListItem info;
4685 info.m_itemId = item;
4686 m_mainWin->GetItem( info );
4687 return info.GetBackgroundColour();
4688 }
4689
4690 void wxGenericListCtrl::SetItemFont( long item, const wxFont &f )
4691 {
4692 wxListItem info;
4693 info.m_itemId = item;
4694 info.SetFont( f );
4695 m_mainWin->SetItem( info );
4696 }
4697
4698 wxFont wxGenericListCtrl::GetItemFont( long item ) const
4699 {
4700 wxListItem info;
4701 info.m_itemId = item;
4702 m_mainWin->GetItem( info );
4703 return info.GetFont();
4704 }
4705
4706 int wxGenericListCtrl::GetSelectedItemCount() const
4707 {
4708 return m_mainWin->GetSelectedItemCount();
4709 }
4710
4711 wxColour wxGenericListCtrl::GetTextColour() const
4712 {
4713 return GetForegroundColour();
4714 }
4715
4716 void wxGenericListCtrl::SetTextColour(const wxColour& col)
4717 {
4718 SetForegroundColour(col);
4719 }
4720
4721 long wxGenericListCtrl::GetTopItem() const
4722 {
4723 size_t top;
4724 m_mainWin->GetVisibleLinesRange(&top, NULL);
4725 return (long)top;
4726 }
4727
4728 long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
4729 {
4730 return m_mainWin->GetNextItem( item, geom, state );
4731 }
4732
4733 wxImageList *wxGenericListCtrl::GetImageList(int which) const
4734 {
4735 if (which == wxIMAGE_LIST_NORMAL)
4736 return m_imageListNormal;
4737 else if (which == wxIMAGE_LIST_SMALL)
4738 return m_imageListSmall;
4739 else if (which == wxIMAGE_LIST_STATE)
4740 return m_imageListState;
4741
4742 return NULL;
4743 }
4744
4745 void wxGenericListCtrl::SetImageList( wxImageList *imageList, int which )
4746 {
4747 if ( which == wxIMAGE_LIST_NORMAL )
4748 {
4749 if (m_ownsImageListNormal)
4750 delete m_imageListNormal;
4751 m_imageListNormal = imageList;
4752 m_ownsImageListNormal = false;
4753 }
4754 else if ( which == wxIMAGE_LIST_SMALL )
4755 {
4756 if (m_ownsImageListSmall)
4757 delete m_imageListSmall;
4758 m_imageListSmall = imageList;
4759 m_ownsImageListSmall = false;
4760 }
4761 else if ( which == wxIMAGE_LIST_STATE )
4762 {
4763 if (m_ownsImageListState)
4764 delete m_imageListState;
4765 m_imageListState = imageList;
4766 m_ownsImageListState = false;
4767 }
4768
4769 m_mainWin->SetImageList( imageList, which );
4770 }
4771
4772 void wxGenericListCtrl::AssignImageList(wxImageList *imageList, int which)
4773 {
4774 SetImageList(imageList, which);
4775 if ( which == wxIMAGE_LIST_NORMAL )
4776 m_ownsImageListNormal = true;
4777 else if ( which == wxIMAGE_LIST_SMALL )
4778 m_ownsImageListSmall = true;
4779 else if ( which == wxIMAGE_LIST_STATE )
4780 m_ownsImageListState = true;
4781 }
4782
4783 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
4784 {
4785 return 0;
4786 }
4787
4788 bool wxGenericListCtrl::DeleteItem( long item )
4789 {
4790 m_mainWin->DeleteItem( item );
4791 return true;
4792 }
4793
4794 bool wxGenericListCtrl::DeleteAllItems()
4795 {
4796 m_mainWin->DeleteAllItems();
4797 return true;
4798 }
4799
4800 bool wxGenericListCtrl::DeleteAllColumns()
4801 {
4802 size_t count = m_mainWin->m_columns.GetCount();
4803 for ( size_t n = 0; n < count; n++ )
4804 DeleteColumn( 0 );
4805 return true;
4806 }
4807
4808 void wxGenericListCtrl::ClearAll()
4809 {
4810 m_mainWin->DeleteEverything();
4811 }
4812
4813 bool wxGenericListCtrl::DeleteColumn( int col )
4814 {
4815 m_mainWin->DeleteColumn( col );
4816
4817 // if we don't have the header any longer, we need to relayout the window
4818 // if ( !GetColumnCount() )
4819
4820 return true;
4821 }
4822
4823 wxTextCtrl *wxGenericListCtrl::EditLabel(long item,
4824 wxClassInfo* textControlClass)
4825 {
4826 return m_mainWin->EditLabel( item, textControlClass );
4827 }
4828
4829 wxTextCtrl *wxGenericListCtrl::GetEditControl() const
4830 {
4831 return m_mainWin->GetEditControl();
4832 }
4833
4834 bool wxGenericListCtrl::EnsureVisible( long item )
4835 {
4836 m_mainWin->EnsureVisible( item );
4837 return true;
4838 }
4839
4840 long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
4841 {
4842 return m_mainWin->FindItem( start, str, partial );
4843 }
4844
4845 long wxGenericListCtrl::FindItem( long start, wxUIntPtr data )
4846 {
4847 return m_mainWin->FindItem( start, data );
4848 }
4849
4850 long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt,
4851 int WXUNUSED(direction))
4852 {
4853 return m_mainWin->FindItem( pt );
4854 }
4855
4856 // TODO: sub item hit testing
4857 long wxGenericListCtrl::HitTest(const wxPoint& point, int& flags, long *) const
4858 {
4859 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
4860 }
4861
4862 long wxGenericListCtrl::InsertItem( wxListItem& info )
4863 {
4864 m_mainWin->InsertItem( info );
4865 return info.m_itemId;
4866 }
4867
4868 long wxGenericListCtrl::InsertItem( long index, const wxString &label )
4869 {
4870 wxListItem info;
4871 info.m_text = label;
4872 info.m_mask = wxLIST_MASK_TEXT;
4873 info.m_itemId = index;
4874 return InsertItem( info );
4875 }
4876
4877 long wxGenericListCtrl::InsertItem( long index, int imageIndex )
4878 {
4879 wxListItem info;
4880 info.m_mask = wxLIST_MASK_IMAGE;
4881 info.m_image = imageIndex;
4882 info.m_itemId = index;
4883 return InsertItem( info );
4884 }
4885
4886 long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
4887 {
4888 wxListItem info;
4889 info.m_text = label;
4890 info.m_image = imageIndex;
4891 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
4892 info.m_itemId = index;
4893 return InsertItem( info );
4894 }
4895
4896 long wxGenericListCtrl::InsertColumn( long col, wxListItem &item )
4897 {
4898 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
4899
4900 m_mainWin->InsertColumn( col, item );
4901
4902 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4903 // still have m_headerWin==NULL
4904 if (m_headerWin)
4905 m_headerWin->Refresh();
4906
4907 return 0;
4908 }
4909
4910 long wxGenericListCtrl::InsertColumn( long col, const wxString &heading,
4911 int format, int width )
4912 {
4913 wxListItem item;
4914 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
4915 item.m_text = heading;
4916 if (width >= -2)
4917 {
4918 item.m_mask |= wxLIST_MASK_WIDTH;
4919 item.m_width = width;
4920 }
4921
4922 item.m_format = format;
4923
4924 return InsertColumn( col, item );
4925 }
4926
4927 bool wxGenericListCtrl::ScrollList( int dx, int dy )
4928 {
4929 return m_mainWin->ScrollList(dx, dy);
4930 }
4931
4932 // Sort items.
4933 // fn is a function which takes 3 long arguments: item1, item2, data.
4934 // item1 is the long data associated with a first item (NOT the index).
4935 // item2 is the long data associated with a second item (NOT the index).
4936 // data is the same value as passed to SortItems.
4937 // The return value is a negative number if the first item should precede the second
4938 // item, a positive number of the second item should precede the first,
4939 // or zero if the two items are equivalent.
4940 // data is arbitrary data to be passed to the sort function.
4941
4942 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn, wxIntPtr data )
4943 {
4944 m_mainWin->SortItems( fn, data );
4945 return true;
4946 }
4947
4948 // ----------------------------------------------------------------------------
4949 // event handlers
4950 // ----------------------------------------------------------------------------
4951
4952 void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
4953 {
4954 if (!m_mainWin) return;
4955
4956 // We need to override OnSize so that our scrolled
4957 // window a) does call Layout() to use sizers for
4958 // positioning the controls but b) does not query
4959 // the sizer for their size and use that for setting
4960 // the scrollable area as set that ourselves by
4961 // calling SetScrollbar() further down.
4962
4963 Layout();
4964
4965 m_mainWin->RecalculatePositions();
4966
4967 AdjustScrollbars();
4968 }
4969
4970 void wxGenericListCtrl::OnInternalIdle()
4971 {
4972 wxWindow::OnInternalIdle();
4973
4974 if (m_mainWin->m_dirty)
4975 m_mainWin->RecalculatePositions();
4976 }
4977
4978 // ----------------------------------------------------------------------------
4979 // font/colours
4980 // ----------------------------------------------------------------------------
4981
4982 bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
4983 {
4984 if (m_mainWin)
4985 {
4986 m_mainWin->SetBackgroundColour( colour );
4987 m_mainWin->m_dirty = true;
4988 }
4989
4990 return true;
4991 }
4992
4993 bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
4994 {
4995 if ( !wxWindow::SetForegroundColour( colour ) )
4996 return false;
4997
4998 if (m_mainWin)
4999 {
5000 m_mainWin->SetForegroundColour( colour );
5001 m_mainWin->m_dirty = true;
5002 }
5003
5004 if (m_headerWin)
5005 m_headerWin->SetForegroundColour( colour );
5006
5007 return true;
5008 }
5009
5010 bool wxGenericListCtrl::SetFont( const wxFont &font )
5011 {
5012 if ( !wxWindow::SetFont( font ) )
5013 return false;
5014
5015 if (m_mainWin)
5016 {
5017 m_mainWin->SetFont( font );
5018 m_mainWin->m_dirty = true;
5019 }
5020
5021 if (m_headerWin)
5022 {
5023 m_headerWin->SetFont( font );
5024 // CalculateAndSetHeaderHeight();
5025 }
5026
5027 Refresh();
5028
5029 return true;
5030 }
5031
5032 // static
5033 wxVisualAttributes
5034 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
5035 {
5036 #if _USE_VISATTR
5037 // Use the same color scheme as wxListBox
5038 return wxListBox::GetClassDefaultAttributes(variant);
5039 #else
5040 wxUnusedVar(variant);
5041 wxVisualAttributes attr;
5042 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
5043 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
5044 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
5045 return attr;
5046 #endif
5047 }
5048
5049 // ----------------------------------------------------------------------------
5050 // methods forwarded to m_mainWin
5051 // ----------------------------------------------------------------------------
5052
5053 #if wxUSE_DRAG_AND_DROP
5054
5055 void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
5056 {
5057 m_mainWin->SetDropTarget( dropTarget );
5058 }
5059
5060 wxDropTarget *wxGenericListCtrl::GetDropTarget() const
5061 {
5062 return m_mainWin->GetDropTarget();
5063 }
5064
5065 #endif
5066
5067 bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
5068 {
5069 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false;
5070 }
5071
5072 wxColour wxGenericListCtrl::GetBackgroundColour() const
5073 {
5074 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5075 }
5076
5077 wxColour wxGenericListCtrl::GetForegroundColour() const
5078 {
5079 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5080 }
5081
5082 bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
5083 {
5084 #if wxUSE_MENUS
5085 return m_mainWin->PopupMenu( menu, x, y );
5086 #else
5087 return false;
5088 #endif
5089 }
5090
5091 void wxGenericListCtrl::DoClientToScreen( int *x, int *y ) const
5092 {
5093 m_mainWin->DoClientToScreen(x, y);
5094 }
5095
5096 void wxGenericListCtrl::DoScreenToClient( int *x, int *y ) const
5097 {
5098 m_mainWin->DoScreenToClient(x, y);
5099 }
5100
5101 void wxGenericListCtrl::SetFocus()
5102 {
5103 // The test in window.cpp fails as we are a composite
5104 // window, so it checks against "this", but not m_mainWin.
5105 if ( DoFindFocus() != this )
5106 m_mainWin->SetFocus();
5107 }
5108
5109 wxSize wxGenericListCtrl::DoGetBestSize() const
5110 {
5111 // Something is better than nothing...
5112 // 100x80 is what the MSW version will get from the default
5113 // wxControl::DoGetBestSize
5114 return wxSize(100, 80);
5115 }
5116
5117 // ----------------------------------------------------------------------------
5118 // virtual list control support
5119 // ----------------------------------------------------------------------------
5120
5121 wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
5122 {
5123 // this is a pure virtual function, in fact - which is not really pure
5124 // because the controls which are not virtual don't need to implement it
5125 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5126
5127 return wxEmptyString;
5128 }
5129
5130 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
5131 {
5132 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
5133 -1,
5134 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5135 return -1;
5136 }
5137
5138 int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const
5139 {
5140 if (!column)
5141 return OnGetItemImage(item);
5142
5143 return -1;
5144 }
5145
5146 wxListItemAttr *
5147 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
5148 {
5149 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
5150 wxT("invalid item index in OnGetItemAttr()") );
5151
5152 // no attributes by default
5153 return NULL;
5154 }
5155
5156 void wxGenericListCtrl::SetItemCount(long count)
5157 {
5158 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
5159
5160 m_mainWin->SetItemCount(count);
5161 }
5162
5163 void wxGenericListCtrl::RefreshItem(long item)
5164 {
5165 m_mainWin->RefreshLine(item);
5166 }
5167
5168 void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
5169 {
5170 m_mainWin->RefreshLines(itemFrom, itemTo);
5171 }
5172
5173 // Generic wxListCtrl is more or less a container for two other
5174 // windows which drawings are done upon. These are namely
5175 // 'm_headerWin' and 'm_mainWin'.
5176 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5177 // behaviour wxListCtrl has under wxMSW.
5178 //
5179 void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5180 {
5181 if (!rect)
5182 {
5183 // The easy case, no rectangle specified.
5184 if (m_headerWin)
5185 m_headerWin->Refresh(eraseBackground);
5186
5187 if (m_mainWin)
5188 m_mainWin->Refresh(eraseBackground);
5189 }
5190 else
5191 {
5192 // Refresh the header window
5193 if (m_headerWin)
5194 {
5195 wxRect rectHeader = m_headerWin->GetRect();
5196 rectHeader.Intersect(*rect);
5197 if (rectHeader.GetWidth() && rectHeader.GetHeight())
5198 {
5199 int x, y;
5200 m_headerWin->GetPosition(&x, &y);
5201 rectHeader.Offset(-x, -y);
5202 m_headerWin->Refresh(eraseBackground, &rectHeader);
5203 }
5204 }
5205
5206 // Refresh the main window
5207 if (m_mainWin)
5208 {
5209 wxRect rectMain = m_mainWin->GetRect();
5210 rectMain.Intersect(*rect);
5211 if (rectMain.GetWidth() && rectMain.GetHeight())
5212 {
5213 int x, y;
5214 m_mainWin->GetPosition(&x, &y);
5215 rectMain.Offset(-x, -y);
5216 m_mainWin->Refresh(eraseBackground, &rectMain);
5217 }
5218 }
5219 }
5220 }
5221
5222 void wxGenericListCtrl::Update()
5223 {
5224 if ( m_mainWin )
5225 {
5226 if ( m_mainWin->m_dirty )
5227 m_mainWin->RecalculatePositions();
5228
5229 m_mainWin->Update();
5230 }
5231
5232 if ( m_headerWin )
5233 m_headerWin->Update();
5234 }
5235
5236 #endif // wxUSE_LISTCTRL