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