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