1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 // 1. we need to implement searching/sorting for virtual controls somehow
14 // 2. when changing selection the lines are refreshed twice
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #include "wx/listctrl.h"
28 #if (!defined(__WXMSW__) || defined(__WXUNIVERSAL__)) && !defined(__WXMAC__)
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
)
34 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
38 #include "wx/scrolwin.h"
40 #include "wx/settings.h"
41 #include "wx/dynarray.h"
42 #include "wx/dcclient.h"
43 #include "wx/dcscreen.h"
47 #include "wx/imaglist.h"
48 #include "wx/selstore.h"
49 #include "wx/renderer.h"
52 #include "wx/mac/private.h"
56 // NOTE: If using the wxListBox visual attributes works everywhere then this can
57 // be removed, as well as the #else case below.
58 #define _USE_VISATTR 0
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // // the height of the header window (FIXME: should depend on its font!)
66 // static const int HEADER_HEIGHT = 23;
68 static const int SCROLL_UNIT_X
= 15;
70 // the spacing between the lines (in report mode)
71 static const int LINE_SPACING
= 0;
73 // extra margins around the text label
75 static const int EXTRA_WIDTH
= 6;
77 static const int EXTRA_WIDTH
= 4;
79 static const int EXTRA_HEIGHT
= 4;
81 // margin between the window and the items
82 static const int EXTRA_BORDER_X
= 2;
83 static const int EXTRA_BORDER_Y
= 2;
85 // offset for the header window
86 static const int HEADER_OFFSET_X
= 1;
87 static const int HEADER_OFFSET_Y
= 1;
89 // margin between rows of icons in [small] icon view
90 static const int MARGIN_BETWEEN_ROWS
= 6;
92 // when autosizing the columns, add some slack
93 static const int AUTOSIZE_COL_MARGIN
= 10;
95 // default width for the header columns
96 static const int WIDTH_COL_DEFAULT
= 80;
98 // the space between the image and the text in the report mode
99 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
101 // ============================================================================
103 // ============================================================================
105 //-----------------------------------------------------------------------------
106 // wxColWidthInfo (internal)
107 //-----------------------------------------------------------------------------
109 struct wxColWidthInfo
112 bool bNeedsUpdate
; // only set to true when an item whose
113 // width == nMaxWidth is removed
115 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
118 bNeedsUpdate
= needsUpdate
;
122 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
124 //-----------------------------------------------------------------------------
125 // wxListItemData (internal)
126 //-----------------------------------------------------------------------------
131 wxListItemData(wxListMainWindow
*owner
);
134 void SetItem( const wxListItem
&info
);
135 void SetImage( int image
) { m_image
= image
; }
136 void SetData( wxUIntPtr data
) { m_data
= data
; }
137 void SetPosition( int x
, int y
);
138 void SetSize( int width
, int height
);
140 bool HasText() const { return !m_text
.empty(); }
141 const wxString
& GetText() const { return m_text
; }
142 void SetText(const wxString
& text
) { m_text
= text
; }
144 // we can't use empty string for measuring the string width/height, so
145 // always return something
146 wxString
GetTextForMeasuring() const
148 wxString s
= GetText();
155 bool IsHit( int x
, int y
) const;
159 int GetWidth() const;
160 int GetHeight() const;
162 int GetImage() const { return m_image
; }
163 bool HasImage() const { return GetImage() != -1; }
165 void GetItem( wxListItem
&info
) const;
167 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
168 wxListItemAttr
*GetAttr() const { return m_attr
; }
171 // the item image or -1
174 // user data associated with the item
177 // the item coordinates are not used in report mode; instead this pointer is
178 // NULL and the owner window is used to retrieve the item position and size
181 // the list ctrl we are in
182 wxListMainWindow
*m_owner
;
184 // custom attributes or NULL
185 wxListItemAttr
*m_attr
;
188 // common part of all ctors
194 //-----------------------------------------------------------------------------
195 // wxListHeaderData (internal)
196 //-----------------------------------------------------------------------------
198 class wxListHeaderData
: public wxObject
202 wxListHeaderData( const wxListItem
&info
);
203 void SetItem( const wxListItem
&item
);
204 void SetPosition( int x
, int y
);
205 void SetWidth( int w
);
206 void SetState( int state
);
207 void SetFormat( int format
);
208 void SetHeight( int h
);
209 bool HasImage() const;
211 bool HasText() const { return !m_text
.empty(); }
212 const wxString
& GetText() const { return m_text
; }
213 void SetText(const wxString
& text
) { m_text
= text
; }
215 void GetItem( wxListItem
&item
);
217 bool IsHit( int x
, int y
) const;
218 int GetImage() const;
219 int GetWidth() const;
220 int GetFormat() const;
221 int GetState() const;
238 //-----------------------------------------------------------------------------
239 // wxListLineData (internal)
240 //-----------------------------------------------------------------------------
242 WX_DECLARE_EXPORTED_LIST(wxListItemData
, wxListItemDataList
);
243 #include "wx/listimpl.cpp"
244 WX_DEFINE_LIST(wxListItemDataList
)
249 // the list of subitems: only may have more than one item in report mode
250 wxListItemDataList m_items
;
252 // this is not used in report view
264 // the part to be highlighted
265 wxRect m_rectHighlight
;
267 // extend all our rects to be centered inside the one of given width
268 void ExtendWidth(wxCoord w
)
270 wxASSERT_MSG( m_rectAll
.width
<= w
,
271 _T("width can only be increased") );
274 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
275 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
276 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
281 // is this item selected? [NB: not used in virtual mode]
284 // back pointer to the list ctrl
285 wxListMainWindow
*m_owner
;
288 wxListLineData(wxListMainWindow
*owner
);
292 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
296 // are we in report mode?
297 inline bool InReportView() const;
299 // are we in virtual report mode?
300 inline bool IsVirtual() const;
302 // these 2 methods shouldn't be called for report view controls, in that
303 // case we determine our position/size ourselves
305 // calculate the size of the line
306 void CalculateSize( wxDC
*dc
, int spacing
);
308 // remember the position this line appears at
309 void SetPosition( int x
, int y
, int spacing
);
313 void SetImage( int image
) { SetImage(0, image
); }
314 int GetImage() const { return GetImage(0); }
315 void SetImage( int index
, int image
);
316 int GetImage( int index
) const;
318 bool HasImage() const { return GetImage() != -1; }
319 bool HasText() const { return !GetText(0).empty(); }
321 void SetItem( int index
, const wxListItem
&info
);
322 void GetItem( int index
, wxListItem
&info
);
324 wxString
GetText(int index
) const;
325 void SetText( int index
, const wxString
& s
);
327 wxListItemAttr
*GetAttr() const;
328 void SetAttr(wxListItemAttr
*attr
);
330 // return true if the highlighting really changed
331 bool Highlight( bool on
);
333 void ReverseHighlight();
335 bool IsHighlighted() const
337 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
339 return m_highlighted
;
342 // draw the line on the given DC in icon/list mode
343 void Draw( wxDC
*dc
);
345 // the same in report mode
346 void DrawInReportMode( wxDC
*dc
,
348 const wxRect
& rectHL
,
352 // set the line to contain num items (only can be > 1 in report mode)
353 void InitItems( int num
);
355 // get the mode (i.e. style) of the list control
356 inline int GetMode() const;
358 // prepare the DC for drawing with these item's attributes, return true if
359 // we need to draw the items background to highlight it, false otherwise
360 bool SetAttributes(wxDC
*dc
,
361 const wxListItemAttr
*attr
,
364 // draw the text on the DC with the correct justification; also add an
365 // ellipsis if the text is too large to fit in the current width
366 void DrawTextFormatted(wxDC
*dc
,
367 const wxString
&text
,
370 int yMid
, // this is middle, not top, of the text
374 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
375 #include "wx/arrimpl.cpp"
376 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
378 //-----------------------------------------------------------------------------
379 // wxListHeaderWindow (internal)
380 //-----------------------------------------------------------------------------
382 class wxListHeaderWindow
: public wxWindow
385 wxListMainWindow
*m_owner
;
386 const wxCursor
*m_currentCursor
;
387 wxCursor
*m_resizeCursor
;
390 // column being resized or -1
393 // divider line position in logical (unscrolled) coords
396 // minimal position beyond which the divider line
397 // can't be dragged in logical coords
401 wxListHeaderWindow();
403 wxListHeaderWindow( wxWindow
*win
,
405 wxListMainWindow
*owner
,
406 const wxPoint
&pos
= wxDefaultPosition
,
407 const wxSize
&size
= wxDefaultSize
,
409 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
411 virtual ~wxListHeaderWindow();
414 void AdjustDC( wxDC
& dc
);
416 void OnPaint( wxPaintEvent
&event
);
417 void OnMouse( wxMouseEvent
&event
);
418 void OnSetFocus( wxFocusEvent
&event
);
424 // common part of all ctors
427 // generate and process the list event of the given type, return true if
428 // it wasn't vetoed, i.e. if we should proceed
429 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
431 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
432 DECLARE_EVENT_TABLE()
435 //-----------------------------------------------------------------------------
436 // wxListRenameTimer (internal)
437 //-----------------------------------------------------------------------------
439 class wxListRenameTimer
: public wxTimer
442 wxListMainWindow
*m_owner
;
445 wxListRenameTimer( wxListMainWindow
*owner
);
449 //-----------------------------------------------------------------------------
450 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
451 //-----------------------------------------------------------------------------
453 class wxListTextCtrlWrapper
: public wxEvtHandler
456 // NB: text must be a valid object but not Create()d yet
457 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
461 wxTextCtrl
*GetText() const { return m_text
; }
463 void AcceptChangesAndFinish();
466 void OnChar( wxKeyEvent
&event
);
467 void OnKeyUp( wxKeyEvent
&event
);
468 void OnKillFocus( wxFocusEvent
&event
);
470 bool AcceptChanges();
474 wxListMainWindow
*m_owner
;
476 wxString m_startValue
;
479 bool m_aboutToFinish
;
481 DECLARE_EVENT_TABLE()
484 //-----------------------------------------------------------------------------
485 // wxListMainWindow (internal)
486 //-----------------------------------------------------------------------------
488 WX_DECLARE_EXPORTED_LIST(wxListHeaderData
, wxListHeaderDataList
);
489 #include "wx/listimpl.cpp"
490 WX_DEFINE_LIST(wxListHeaderDataList
)
492 class wxListMainWindow
: public wxScrolledWindow
496 wxListMainWindow( wxWindow
*parent
,
498 const wxPoint
& pos
= wxDefaultPosition
,
499 const wxSize
& size
= wxDefaultSize
,
501 const wxString
&name
= _T("listctrlmainwindow") );
503 virtual ~wxListMainWindow();
505 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
507 // return true if this is a virtual list control
508 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
510 // return true if the control is in report mode
511 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
513 // return true if we are in single selection mode, false if multi sel
514 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
516 // do we have a header window?
517 bool HasHeader() const
518 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
520 void HighlightAll( bool on
);
522 // all these functions only do something if the line is currently visible
524 // change the line "selected" state, return true if it really changed
525 bool HighlightLine( size_t line
, bool highlight
= true);
527 // as HighlightLine() but do it for the range of lines: this is incredibly
528 // more efficient for virtual list controls!
530 // NB: unlike HighlightLine() this one does refresh the lines on screen
531 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
533 // toggle the line state and refresh it
534 void ReverseHighlight( size_t line
)
535 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
537 // return true if the line is highlighted
538 bool IsHighlighted(size_t line
) const;
540 // refresh one or several lines at once
541 void RefreshLine( size_t line
);
542 void RefreshLines( size_t lineFrom
, size_t lineTo
);
544 // refresh all selected items
545 void RefreshSelected();
547 // refresh all lines below the given one: the difference with
548 // RefreshLines() is that the index here might not be a valid one (happens
549 // when the last line is deleted)
550 void RefreshAfter( size_t lineFrom
);
552 // the methods which are forwarded to wxListLineData itself in list/icon
553 // modes but are here because the lines don't store their positions in the
556 // get the bound rect for the entire line
557 wxRect
GetLineRect(size_t line
) const;
559 // get the bound rect of the label
560 wxRect
GetLineLabelRect(size_t line
) const;
562 // get the bound rect of the items icon (only may be called if we do have
564 wxRect
GetLineIconRect(size_t line
) const;
566 // get the rect to be highlighted when the item has focus
567 wxRect
GetLineHighlightRect(size_t line
) const;
569 // get the size of the total line rect
570 wxSize
GetLineSize(size_t line
) const
571 { return GetLineRect(line
).GetSize(); }
573 // return the hit code for the corresponding position (in this line)
574 long HitTestLine(size_t line
, int x
, int y
) const;
576 // bring the selected item into view, scrolling to it if necessary
577 void MoveToItem(size_t item
);
579 // bring the current item into view
580 void MoveToFocus() { MoveToItem(m_current
); }
582 // start editing the label of the given item
583 wxTextCtrl
*EditLabel(long item
,
584 wxClassInfo
* textControlClass
= CLASSINFO(wxTextCtrl
));
585 wxTextCtrl
*GetEditControl() const
587 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
590 void FinishEditing(wxTextCtrl
*text
)
593 m_textctrlWrapper
= NULL
;
594 SetFocusIgnoringChildren();
597 // suspend/resume redrawing the control
601 void OnRenameTimer();
602 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
603 void OnRenameCancelled(size_t itemEdit
);
605 void OnMouse( wxMouseEvent
&event
);
607 // called to switch the selection from the current item to newCurrent,
608 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
610 void OnChar( wxKeyEvent
&event
);
611 void OnKeyDown( wxKeyEvent
&event
);
612 void OnKeyUp( wxKeyEvent
&event
);
613 void OnSetFocus( wxFocusEvent
&event
);
614 void OnKillFocus( wxFocusEvent
&event
);
615 void OnScroll( wxScrollWinEvent
& event
);
617 void OnPaint( wxPaintEvent
&event
);
619 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
620 void GetImageSize( int index
, int &width
, int &height
) const;
621 int GetTextLength( const wxString
&s
) const;
623 void SetImageList( wxImageList
*imageList
, int which
);
624 void SetItemSpacing( int spacing
, bool isSmall
= false );
625 int GetItemSpacing( bool isSmall
= false );
627 void SetColumn( int col
, wxListItem
&item
);
628 void SetColumnWidth( int col
, int width
);
629 void GetColumn( int col
, wxListItem
&item
) const;
630 int GetColumnWidth( int col
) const;
631 int GetColumnCount() const { return m_columns
.GetCount(); }
633 // returns the sum of the heights of all columns
634 int GetHeaderWidth() const;
636 int GetCountPerPage() const;
638 void SetItem( wxListItem
&item
);
639 void GetItem( wxListItem
&item
) const;
640 void SetItemState( long item
, long state
, long stateMask
);
641 void SetItemStateAll( long state
, long stateMask
);
642 int GetItemState( long item
, long stateMask
) const;
643 void GetItemRect( long index
, wxRect
&rect
) const;
644 wxRect
GetViewRect() const;
645 bool GetItemPosition( long item
, wxPoint
& pos
) const;
646 int GetSelectedItemCount() const;
648 wxString
GetItemText(long item
) const
651 info
.m_mask
= wxLIST_MASK_TEXT
;
652 info
.m_itemId
= item
;
657 void SetItemText(long item
, const wxString
& value
)
660 info
.m_mask
= wxLIST_MASK_TEXT
;
661 info
.m_itemId
= item
;
666 // set the scrollbars and update the positions of the items
667 void RecalculatePositions(bool noRefresh
= false);
669 // refresh the window and the header
672 long GetNextItem( long item
, int geometry
, int state
) const;
673 void DeleteItem( long index
);
674 void DeleteAllItems();
675 void DeleteColumn( int col
);
676 void DeleteEverything();
677 void EnsureVisible( long index
);
678 long FindItem( long start
, const wxString
& str
, bool partial
= false );
679 long FindItem( long start
, wxUIntPtr data
);
680 long FindItem( const wxPoint
& pt
);
681 long HitTest( int x
, int y
, int &flags
) const;
682 void InsertItem( wxListItem
&item
);
683 void InsertColumn( long col
, wxListItem
&item
);
684 int GetItemWidthWithImage(wxListItem
* item
);
685 void SortItems( wxListCtrlCompare fn
, long data
);
687 size_t GetItemCount() const;
688 bool IsEmpty() const { return GetItemCount() == 0; }
689 void SetItemCount(long count
);
691 // change the current (== focused) item, send a notification event
692 void ChangeCurrent(size_t current
);
693 void ResetCurrent() { ChangeCurrent((size_t)-1); }
694 bool HasCurrent() const { return m_current
!= (size_t)-1; }
696 // send out a wxListEvent
697 void SendNotify( size_t line
,
699 const wxPoint
& point
= wxDefaultPosition
);
701 // override base class virtual to reset m_lineHeight when the font changes
702 virtual bool SetFont(const wxFont
& font
)
704 if ( !wxScrolledWindow::SetFont(font
) )
712 // these are for wxListLineData usage only
714 // get the backpointer to the list ctrl
715 wxGenericListCtrl
*GetListCtrl() const
717 return wxStaticCast(GetParent(), wxGenericListCtrl
);
720 // get the height of all lines (assuming they all do have the same height)
721 wxCoord
GetLineHeight() const;
723 // get the y position of the given line (only for report view)
724 wxCoord
GetLineY(size_t line
) const;
726 // get the brush to use for the item highlighting
727 wxBrush
*GetHighlightBrush() const
729 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
732 bool HasFocus() const
738 // the array of all line objects for a non virtual list control (for the
739 // virtual list control we only ever use m_lines[0])
740 wxListLineDataArray m_lines
;
742 // the list of column objects
743 wxListHeaderDataList m_columns
;
745 // currently focused item or -1
748 // the number of lines per page
751 // this flag is set when something which should result in the window
752 // redrawing happens (i.e. an item was added or deleted, or its appearance
753 // changed) and OnPaint() doesn't redraw the window while it is set which
754 // allows to minimize the number of repaintings when a lot of items are
755 // being added. The real repainting occurs only after the next OnIdle()
759 wxColour
*m_highlightColour
;
760 wxImageList
*m_small_image_list
;
761 wxImageList
*m_normal_image_list
;
763 int m_normal_spacing
;
767 wxTimer
*m_renameTimer
;
771 ColWidthArray m_aColWidths
;
773 // for double click logic
774 size_t m_lineLastClicked
,
775 m_lineBeforeLastClicked
,
776 m_lineSelectSingleOnUp
;
779 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
781 // the total count of items in a virtual list control
784 // the object maintaining the items selection state, only used in virtual
786 wxSelectionStore m_selStore
;
788 // common part of all ctors
791 // get the line data for the given index
792 wxListLineData
*GetLine(size_t n
) const
794 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
798 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
805 // get a dummy line which can be used for geometry calculations and such:
806 // you must use GetLine() if you want to really draw the line
807 wxListLineData
*GetDummyLine() const;
809 // cache the line data of the n-th line in m_lines[0]
810 void CacheLineData(size_t line
);
812 // get the range of visible lines
813 void GetVisibleLinesRange(size_t *from
, size_t *to
);
815 // force us to recalculate the range of visible lines
816 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
818 // get the colour to be used for drawing the rules
819 wxColour
GetRuleColour() const
821 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
825 // initialize the current item if needed
826 void UpdateCurrent();
828 // delete all items but don't refresh: called from dtor
829 void DoDeleteAllItems();
831 // the height of one line using the current font
832 wxCoord m_lineHeight
;
834 // the total header width or 0 if not calculated yet
835 wxCoord m_headerWidth
;
837 // the first and last lines being shown on screen right now (inclusive),
838 // both may be -1 if they must be calculated so never access them directly:
839 // use GetVisibleLinesRange() above instead
843 // the brushes to use for item highlighting when we do/don't have focus
844 wxBrush
*m_highlightBrush
,
845 *m_highlightUnfocusedBrush
;
847 // if this is > 0, the control is frozen and doesn't redraw itself
848 size_t m_freezeCount
;
850 // wrapper around the text control currently used for in place editing or
851 // NULL if no item is being edited
852 wxListTextCtrlWrapper
*m_textctrlWrapper
;
855 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
856 DECLARE_EVENT_TABLE()
858 friend class wxGenericListCtrl
;
862 wxListItemData::~wxListItemData()
864 // in the virtual list control the attributes are managed by the main
865 // program, so don't delete them
866 if ( !m_owner
->IsVirtual() )
872 void wxListItemData::Init()
880 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
886 if ( owner
->InReportView() )
892 void wxListItemData::SetItem( const wxListItem
&info
)
894 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
895 SetText(info
.m_text
);
896 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
897 m_image
= info
.m_image
;
898 if ( info
.m_mask
& wxLIST_MASK_DATA
)
899 m_data
= info
.m_data
;
901 if ( info
.HasAttributes() )
904 m_attr
->AssignFrom(*info
.GetAttributes());
906 m_attr
= new wxListItemAttr(*info
.GetAttributes());
914 m_rect
->width
= info
.m_width
;
918 void wxListItemData::SetPosition( int x
, int y
)
920 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
926 void wxListItemData::SetSize( int width
, int height
)
928 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
931 m_rect
->width
= width
;
933 m_rect
->height
= height
;
936 bool wxListItemData::IsHit( int x
, int y
) const
938 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
940 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
943 int wxListItemData::GetX() const
945 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
950 int wxListItemData::GetY() const
952 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
957 int wxListItemData::GetWidth() const
959 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
961 return m_rect
->width
;
964 int wxListItemData::GetHeight() const
966 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
968 return m_rect
->height
;
971 void wxListItemData::GetItem( wxListItem
&info
) const
973 long mask
= info
.m_mask
;
975 // by default, get everything for backwards compatibility
978 if ( mask
& wxLIST_MASK_TEXT
)
979 info
.m_text
= m_text
;
980 if ( mask
& wxLIST_MASK_IMAGE
)
981 info
.m_image
= m_image
;
982 if ( mask
& wxLIST_MASK_DATA
)
983 info
.m_data
= m_data
;
987 if ( m_attr
->HasTextColour() )
988 info
.SetTextColour(m_attr
->GetTextColour());
989 if ( m_attr
->HasBackgroundColour() )
990 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
991 if ( m_attr
->HasFont() )
992 info
.SetFont(m_attr
->GetFont());
996 //-----------------------------------------------------------------------------
998 //-----------------------------------------------------------------------------
1000 void wxListHeaderData::Init()
1012 wxListHeaderData::wxListHeaderData()
1017 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1024 void wxListHeaderData::SetItem( const wxListItem
&item
)
1026 m_mask
= item
.m_mask
;
1028 if ( m_mask
& wxLIST_MASK_TEXT
)
1029 m_text
= item
.m_text
;
1031 if ( m_mask
& wxLIST_MASK_IMAGE
)
1032 m_image
= item
.m_image
;
1034 if ( m_mask
& wxLIST_MASK_FORMAT
)
1035 m_format
= item
.m_format
;
1037 if ( m_mask
& wxLIST_MASK_WIDTH
)
1038 SetWidth(item
.m_width
);
1040 if ( m_mask
& wxLIST_MASK_STATE
)
1041 SetState(item
.m_state
);
1044 void wxListHeaderData::SetPosition( int x
, int y
)
1050 void wxListHeaderData::SetHeight( int h
)
1055 void wxListHeaderData::SetWidth( int w
)
1057 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1060 void wxListHeaderData::SetState( int flag
)
1065 void wxListHeaderData::SetFormat( int format
)
1070 bool wxListHeaderData::HasImage() const
1072 return m_image
!= -1;
1075 bool wxListHeaderData::IsHit( int x
, int y
) const
1077 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1080 void wxListHeaderData::GetItem( wxListItem
& item
)
1082 item
.m_mask
= m_mask
;
1083 item
.m_text
= m_text
;
1084 item
.m_image
= m_image
;
1085 item
.m_format
= m_format
;
1086 item
.m_width
= m_width
;
1087 item
.m_state
= m_state
;
1090 int wxListHeaderData::GetImage() const
1095 int wxListHeaderData::GetWidth() const
1100 int wxListHeaderData::GetFormat() const
1105 int wxListHeaderData::GetState() const
1110 //-----------------------------------------------------------------------------
1112 //-----------------------------------------------------------------------------
1114 inline int wxListLineData::GetMode() const
1116 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1119 inline bool wxListLineData::InReportView() const
1121 return m_owner
->HasFlag(wxLC_REPORT
);
1124 inline bool wxListLineData::IsVirtual() const
1126 return m_owner
->IsVirtual();
1129 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1133 if ( InReportView() )
1136 m_gi
= new GeometryInfo
;
1138 m_highlighted
= false;
1140 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1143 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1145 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1146 wxCHECK_RET( node
, _T("no subitems at all??") );
1148 wxListItemData
*item
= node
->GetData();
1153 switch ( GetMode() )
1156 case wxLC_SMALL_ICON
:
1157 m_gi
->m_rectAll
.width
= spacing
;
1159 s
= item
->GetText();
1164 m_gi
->m_rectLabel
.width
=
1165 m_gi
->m_rectLabel
.height
= 0;
1169 dc
->GetTextExtent( s
, &lw
, &lh
);
1173 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1175 m_gi
->m_rectAll
.width
= lw
;
1177 m_gi
->m_rectLabel
.width
= lw
;
1178 m_gi
->m_rectLabel
.height
= lh
;
1181 if (item
->HasImage())
1184 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1185 m_gi
->m_rectIcon
.width
= w
+ 8;
1186 m_gi
->m_rectIcon
.height
= h
+ 8;
1188 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1189 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1190 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1191 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1194 if ( item
->HasText() )
1196 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1197 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1199 else // no text, highlight the icon
1201 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1202 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1207 s
= item
->GetTextForMeasuring();
1209 dc
->GetTextExtent( s
, &lw
, &lh
);
1213 m_gi
->m_rectLabel
.width
= lw
;
1214 m_gi
->m_rectLabel
.height
= lh
;
1216 m_gi
->m_rectAll
.width
= lw
;
1217 m_gi
->m_rectAll
.height
= lh
;
1219 if (item
->HasImage())
1222 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1223 m_gi
->m_rectIcon
.width
= w
;
1224 m_gi
->m_rectIcon
.height
= h
;
1226 m_gi
->m_rectAll
.width
+= 4 + w
;
1227 if (h
> m_gi
->m_rectAll
.height
)
1228 m_gi
->m_rectAll
.height
= h
;
1231 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1232 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1236 wxFAIL_MSG( _T("unexpected call to SetSize") );
1240 wxFAIL_MSG( _T("unknown mode") );
1245 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1247 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1248 wxCHECK_RET( node
, _T("no subitems at all??") );
1250 wxListItemData
*item
= node
->GetData();
1252 switch ( GetMode() )
1255 case wxLC_SMALL_ICON
:
1256 m_gi
->m_rectAll
.x
= x
;
1257 m_gi
->m_rectAll
.y
= y
;
1259 if ( item
->HasImage() )
1261 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1262 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1263 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1266 if ( item
->HasText() )
1268 if (m_gi
->m_rectAll
.width
> spacing
)
1269 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1271 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1272 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1273 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1274 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1276 else // no text, highlight the icon
1278 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1279 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1284 m_gi
->m_rectAll
.x
= x
;
1285 m_gi
->m_rectAll
.y
= y
;
1287 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1288 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1289 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1291 if (item
->HasImage())
1293 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1294 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1295 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1299 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1304 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1308 wxFAIL_MSG( _T("unknown mode") );
1313 void wxListLineData::InitItems( int num
)
1315 for (int i
= 0; i
< num
; i
++)
1316 m_items
.Append( new wxListItemData(m_owner
) );
1319 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1321 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1322 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1324 wxListItemData
*item
= node
->GetData();
1325 item
->SetItem( info
);
1328 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1330 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1333 wxListItemData
*item
= node
->GetData();
1334 item
->GetItem( info
);
1338 wxString
wxListLineData::GetText(int index
) const
1342 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1345 wxListItemData
*item
= node
->GetData();
1346 s
= item
->GetText();
1352 void wxListLineData::SetText( int index
, const wxString
& s
)
1354 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1357 wxListItemData
*item
= node
->GetData();
1362 void wxListLineData::SetImage( int index
, int image
)
1364 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1365 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1367 wxListItemData
*item
= node
->GetData();
1368 item
->SetImage(image
);
1371 int wxListLineData::GetImage( int index
) const
1373 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1374 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1376 wxListItemData
*item
= node
->GetData();
1377 return item
->GetImage();
1380 wxListItemAttr
*wxListLineData::GetAttr() const
1382 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1383 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1385 wxListItemData
*item
= node
->GetData();
1386 return item
->GetAttr();
1389 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1391 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1392 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1394 wxListItemData
*item
= node
->GetData();
1395 item
->SetAttr(attr
);
1398 bool wxListLineData::SetAttributes(wxDC
*dc
,
1399 const wxListItemAttr
*attr
,
1402 wxWindow
*listctrl
= m_owner
->GetParent();
1406 // don't use foreground colour for drawing highlighted items - this might
1407 // make them completely invisible (and there is no way to do bit
1408 // arithmetics on wxColour, unfortunately)
1413 if (m_owner
->HasFocus()
1415 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1423 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1425 else if ( attr
&& attr
->HasTextColour() )
1426 colText
= attr
->GetTextColour();
1428 colText
= listctrl
->GetForegroundColour();
1430 dc
->SetTextForeground(colText
);
1434 if ( attr
&& attr
->HasFont() )
1435 font
= attr
->GetFont();
1437 font
= listctrl
->GetFont();
1442 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1443 if ( highlighted
|| hasBgCol
)
1446 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1448 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1450 dc
->SetPen( *wxTRANSPARENT_PEN
);
1458 void wxListLineData::Draw( wxDC
*dc
)
1460 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1461 wxCHECK_RET( node
, _T("no subitems at all??") );
1463 bool highlighted
= IsHighlighted();
1465 wxListItemAttr
*attr
= GetAttr();
1467 if ( SetAttributes(dc
, attr
, highlighted
) )
1468 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1470 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1476 int flags
= wxCONTROL_SELECTED
;
1477 if (m_owner
->HasFocus()
1479 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1482 flags
|= wxCONTROL_FOCUSED
;
1483 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1488 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1493 // just for debugging to better see where the items are
1495 dc
->SetPen(*wxRED_PEN
);
1496 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1497 dc
->DrawRectangle( m_gi
->m_rectAll
);
1498 dc
->SetPen(*wxGREEN_PEN
);
1499 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1502 wxListItemData
*item
= node
->GetData();
1503 if (item
->HasImage())
1505 // centre the image inside our rectangle, this looks nicer when items
1506 // ae aligned in a row
1507 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1509 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1512 if (item
->HasText())
1514 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1516 wxDCClipper
clipper(*dc
, rectLabel
);
1517 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1521 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1523 const wxRect
& rectHL
,
1526 // TODO: later we should support setting different attributes for
1527 // different columns - to do it, just add "col" argument to
1528 // GetAttr() and move these lines into the loop below
1529 wxListItemAttr
*attr
= GetAttr();
1530 if ( SetAttributes(dc
, attr
, highlighted
) )
1531 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1533 dc
->DrawRectangle( rectHL
);
1539 int flags
= wxCONTROL_SELECTED
;
1540 if (m_owner
->HasFocus()
1542 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1545 flags
|= wxCONTROL_FOCUSED
;
1546 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1550 dc
->DrawRectangle( rectHL
);
1555 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1556 yMid
= rect
.y
+ rect
.height
/2;
1558 // This probably needs to be done
1559 // on all platforms as the icons
1560 // otherwise nearly touch the border
1565 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1567 node
= node
->GetNext(), col
++ )
1569 wxListItemData
*item
= node
->GetData();
1571 int width
= m_owner
->GetColumnWidth(col
);
1575 if ( item
->HasImage() )
1578 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1579 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1581 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1587 if ( item
->HasText() )
1588 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, width
- 8);
1592 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1593 const wxString
&text
,
1600 dc
->GetTextExtent(text
, &w
, &h
);
1602 const wxCoord y
= yMid
- (h
+ 1)/2;
1604 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1606 // determine if the string can fit inside the current width
1609 // it can, draw it using the items alignment
1611 m_owner
->GetColumn(col
, item
);
1612 switch ( item
.GetAlign() )
1614 case wxLIST_FORMAT_LEFT
:
1618 case wxLIST_FORMAT_RIGHT
:
1622 case wxLIST_FORMAT_CENTER
:
1623 x
+= (width
- w
) / 2;
1627 wxFAIL_MSG( _T("unknown list item format") );
1631 dc
->DrawText(text
, x
, y
);
1633 else // otherwise, truncate and add an ellipsis if possible
1635 // determine the base width
1636 wxString
ellipsis(wxT("..."));
1638 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1640 // continue until we have enough space or only one character left
1642 size_t len
= text
.length();
1643 wxString drawntext
= text
.Left(len
);
1646 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1647 drawntext
.RemoveLast();
1650 if (w
+ base_w
<= width
)
1654 // if still not enough space, remove ellipsis characters
1655 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1657 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1658 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1661 // now draw the text
1662 dc
->DrawText(drawntext
, x
, y
);
1663 dc
->DrawText(ellipsis
, x
+ w
, y
);
1667 bool wxListLineData::Highlight( bool on
)
1669 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1671 if ( on
== m_highlighted
)
1679 void wxListLineData::ReverseHighlight( void )
1681 Highlight(!IsHighlighted());
1684 //-----------------------------------------------------------------------------
1685 // wxListHeaderWindow
1686 //-----------------------------------------------------------------------------
1688 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1690 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1691 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1692 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1693 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1696 void wxListHeaderWindow::Init()
1698 m_currentCursor
= (wxCursor
*) NULL
;
1699 m_isDragging
= false;
1703 wxListHeaderWindow::wxListHeaderWindow()
1707 m_owner
= (wxListMainWindow
*) NULL
;
1708 m_resizeCursor
= (wxCursor
*) NULL
;
1711 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1713 wxListMainWindow
*owner
,
1717 const wxString
&name
)
1718 : wxWindow( win
, id
, pos
, size
, style
, name
)
1723 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1726 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1727 SetOwnForegroundColour( attr
.colFg
);
1728 SetOwnBackgroundColour( attr
.colBg
);
1730 SetOwnFont( attr
.font
);
1732 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1733 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1735 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1739 wxListHeaderWindow::~wxListHeaderWindow()
1741 delete m_resizeCursor
;
1744 #ifdef __WXUNIVERSAL__
1745 #include "wx/univ/renderer.h"
1746 #include "wx/univ/theme.h"
1749 // shift the DC origin to match the position of the main window horz
1750 // scrollbar: this allows us to always use logical coords
1751 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1754 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1757 m_owner
->GetViewStart( &view_start
, NULL
);
1762 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1764 // account for the horz scrollbar offset
1766 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1768 // Maybe we just have to check for m_signX
1769 // in the DC, but I leave the #ifdef __WXGTK__
1771 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1775 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1778 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1780 wxPaintDC
dc( this );
1785 dc
.SetFont( GetFont() );
1787 // width and height of the entire header window
1789 GetClientSize( &w
, &h
);
1790 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1792 dc
.SetBackgroundMode(wxTRANSPARENT
);
1793 dc
.SetTextForeground(GetForegroundColour());
1795 int x
= HEADER_OFFSET_X
;
1796 int numColumns
= m_owner
->GetColumnCount();
1798 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1800 m_owner
->GetColumn( i
, item
);
1801 int wCol
= item
.m_width
;
1803 // the width of the rect to draw: make it smaller to fit entirely
1804 // inside the column rect
1814 if (!m_parent
->IsEnabled())
1815 flags
|= wxCONTROL_DISABLED
;
1817 // NB: The code below is not really Mac-specific, but since we are close
1818 // to 2.8 release and I don't have time to test on other platforms, I
1819 // defined this only for wxMac. If this behavior is desired on
1820 // other platforms, please go ahead and revise or remove the #ifdef.
1822 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1823 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1824 flags
|= wxCONTROL_SELECTED
;
1827 wxRendererNative::Get().DrawHeaderButton
1831 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1835 // see if we have enough space for the column label
1837 // for this we need the width of the text
1840 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1841 wLabel
+= 2 * EXTRA_WIDTH
;
1843 // and the width of the icon, if any
1844 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
1845 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1846 const int image
= item
.m_image
;
1847 wxImageList
*imageList
;
1850 imageList
= m_owner
->m_small_image_list
;
1853 imageList
->GetSize(image
, ix
, iy
);
1854 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1862 // ignore alignment if there is not enough space anyhow
1864 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1867 wxFAIL_MSG( _T("unknown list item format") );
1870 case wxLIST_FORMAT_LEFT
:
1874 case wxLIST_FORMAT_RIGHT
:
1875 xAligned
= x
+ cw
- wLabel
;
1878 case wxLIST_FORMAT_CENTER
:
1879 xAligned
= x
+ (cw
- wLabel
) / 2;
1883 // if we have an image, draw it on the right of the label
1890 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
1891 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1892 wxIMAGELIST_DRAW_TRANSPARENT
1895 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1898 // draw the text clipping it so that it doesn't overwrite the column
1900 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1902 dc
.DrawText( item
.GetText(),
1903 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1909 void wxListHeaderWindow::DrawCurrent()
1911 int x1
= m_currentX
;
1913 m_owner
->ClientToScreen( &x1
, &y1
);
1915 int x2
= m_currentX
;
1917 m_owner
->GetClientSize( NULL
, &y2
);
1918 m_owner
->ClientToScreen( &x2
, &y2
);
1921 dc
.SetLogicalFunction( wxINVERT
);
1922 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1923 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1927 dc
.DrawLine( x1
, y1
, x2
, y2
);
1929 dc
.SetLogicalFunction( wxCOPY
);
1931 dc
.SetPen( wxNullPen
);
1932 dc
.SetBrush( wxNullBrush
);
1935 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1937 // we want to work with logical coords
1939 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1940 int y
= event
.GetY();
1944 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1946 // we don't draw the line beyond our window, but we allow dragging it
1949 GetClientSize( &w
, NULL
);
1950 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1953 // erase the line if it was drawn
1954 if ( m_currentX
< w
)
1957 if (event
.ButtonUp())
1960 m_isDragging
= false;
1962 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1963 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1970 m_currentX
= m_minX
+ 7;
1972 // draw in the new location
1973 if ( m_currentX
< w
)
1977 else // not dragging
1980 bool hit_border
= false;
1982 // end of the current column
1985 // find the column where this event occurred
1987 countCol
= m_owner
->GetColumnCount();
1988 for (col
= 0; col
< countCol
; col
++)
1990 xpos
+= m_owner
->GetColumnWidth( col
);
1993 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1995 // near the column border
2002 // inside the column
2009 if ( col
== countCol
)
2012 if (event
.LeftDown() || event
.RightUp())
2014 if (hit_border
&& event
.LeftDown())
2016 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2017 event
.GetPosition()) )
2019 m_isDragging
= true;
2024 //else: column resizing was vetoed by the user code
2026 else // click on a column
2028 // record the selected state of the columns
2029 if (event
.LeftDown())
2031 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2034 m_owner
->GetColumn(i
, colItem
);
2035 long state
= colItem
.GetState();
2037 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2039 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2040 m_owner
->SetColumn(i
, colItem
);
2044 SendListEvent( event
.LeftDown()
2045 ? wxEVT_COMMAND_LIST_COL_CLICK
2046 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2047 event
.GetPosition());
2050 else if (event
.Moving())
2055 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2056 m_currentCursor
= m_resizeCursor
;
2060 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2061 m_currentCursor
= wxSTANDARD_CURSOR
;
2065 SetCursor(*m_currentCursor
);
2070 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2072 m_owner
->SetFocus();
2076 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2078 wxWindow
*parent
= GetParent();
2079 wxListEvent
le( type
, parent
->GetId() );
2080 le
.SetEventObject( parent
);
2081 le
.m_pointDrag
= pos
;
2083 // the position should be relative to the parent window, not
2084 // this one for compatibility with MSW and common sense: the
2085 // user code doesn't know anything at all about this header
2086 // window, so why should it get positions relative to it?
2087 le
.m_pointDrag
.y
-= GetSize().y
;
2089 le
.m_col
= m_column
;
2090 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2093 //-----------------------------------------------------------------------------
2094 // wxListRenameTimer (internal)
2095 //-----------------------------------------------------------------------------
2097 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2102 void wxListRenameTimer::Notify()
2104 m_owner
->OnRenameTimer();
2107 //-----------------------------------------------------------------------------
2108 // wxListTextCtrlWrapper (internal)
2109 //-----------------------------------------------------------------------------
2111 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2112 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2113 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2114 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2117 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2120 : m_startValue(owner
->GetItemText(itemEdit
)),
2121 m_itemEdited(itemEdit
)
2126 m_aboutToFinish
= false;
2128 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2130 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2131 &rectLabel
.x
, &rectLabel
.y
);
2133 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2134 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2135 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2138 m_text
->PushEventHandler(this);
2141 void wxListTextCtrlWrapper::Finish()
2147 m_text
->RemoveEventHandler(this);
2148 m_owner
->FinishEditing(m_text
);
2150 wxPendingDelete
.Append( this );
2154 bool wxListTextCtrlWrapper::AcceptChanges()
2156 const wxString value
= m_text
->GetValue();
2158 if ( value
== m_startValue
)
2159 // nothing changed, always accept
2162 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2163 // vetoed by the user
2166 // accepted, do rename the item
2167 m_owner
->SetItemText(m_itemEdited
, value
);
2172 void wxListTextCtrlWrapper::AcceptChangesAndFinish()
2174 m_aboutToFinish
= true;
2176 // Notify the owner about the changes
2179 // Even if vetoed, close the control (consistent with MSW)
2183 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2185 switch ( event
.m_keyCode
)
2188 AcceptChangesAndFinish();
2192 m_owner
->OnRenameCancelled( m_itemEdited
);
2201 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2209 // auto-grow the textctrl:
2210 wxSize parentSize
= m_owner
->GetSize();
2211 wxPoint myPos
= m_text
->GetPosition();
2212 wxSize mySize
= m_text
->GetSize();
2214 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2215 if (myPos
.x
+ sx
> parentSize
.x
)
2216 sx
= parentSize
.x
- myPos
.x
;
2219 m_text
->SetSize(sx
, wxDefaultCoord
);
2224 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2226 if ( !m_finished
&& !m_aboutToFinish
)
2228 if ( !AcceptChanges() )
2229 m_owner
->OnRenameCancelled( m_itemEdited
);
2234 // We must let the native text control handle focus
2238 //-----------------------------------------------------------------------------
2240 //-----------------------------------------------------------------------------
2242 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2244 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2245 EVT_PAINT (wxListMainWindow::OnPaint
)
2246 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2247 EVT_CHAR (wxListMainWindow::OnChar
)
2248 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2249 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
2250 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2251 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2252 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2255 void wxListMainWindow::Init()
2260 m_lineTo
= (size_t)-1;
2266 m_small_image_list
= (wxImageList
*) NULL
;
2267 m_normal_image_list
= (wxImageList
*) NULL
;
2269 m_small_spacing
= 30;
2270 m_normal_spacing
= 40;
2274 m_isCreated
= false;
2276 m_lastOnSame
= false;
2277 m_renameTimer
= new wxListRenameTimer( this );
2278 m_textctrlWrapper
= NULL
;
2282 m_lineSelectSingleOnUp
=
2283 m_lineBeforeLastClicked
= (size_t)-1;
2288 wxListMainWindow::wxListMainWindow()
2293 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2296 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2301 const wxString
&name
)
2302 : wxScrolledWindow( parent
, id
, pos
, size
,
2303 style
| wxHSCROLL
| wxVSCROLL
, name
)
2307 m_highlightBrush
= new wxBrush
2309 wxSystemSettings::GetColour
2311 wxSYS_COLOUR_HIGHLIGHT
2316 m_highlightUnfocusedBrush
= new wxBrush
2318 wxSystemSettings::GetColour
2320 wxSYS_COLOUR_BTNSHADOW
2325 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2327 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2328 SetOwnForegroundColour( attr
.colFg
);
2329 SetOwnBackgroundColour( attr
.colBg
);
2331 SetOwnFont( attr
.font
);
2334 wxListMainWindow::~wxListMainWindow()
2337 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2338 WX_CLEAR_ARRAY(m_aColWidths
);
2340 delete m_highlightBrush
;
2341 delete m_highlightUnfocusedBrush
;
2342 delete m_renameTimer
;
2345 void wxListMainWindow::CacheLineData(size_t line
)
2347 wxGenericListCtrl
*listctrl
= GetListCtrl();
2349 wxListLineData
*ld
= GetDummyLine();
2351 size_t countCol
= GetColumnCount();
2352 for ( size_t col
= 0; col
< countCol
; col
++ )
2354 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2355 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2358 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2361 wxListLineData
*wxListMainWindow::GetDummyLine() const
2363 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2364 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2366 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2368 // we need to recreate the dummy line if the number of columns in the
2369 // control changed as it would have the incorrect number of fields
2371 if ( !m_lines
.IsEmpty() &&
2372 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2374 self
->m_lines
.Clear();
2377 if ( m_lines
.IsEmpty() )
2379 wxListLineData
*line
= new wxListLineData(self
);
2380 self
->m_lines
.Add(line
);
2382 // don't waste extra memory -- there never going to be anything
2383 // else/more in this array
2384 self
->m_lines
.Shrink();
2390 // ----------------------------------------------------------------------------
2391 // line geometry (report mode only)
2392 // ----------------------------------------------------------------------------
2394 wxCoord
wxListMainWindow::GetLineHeight() const
2396 // we cache the line height as calling GetTextExtent() is slow
2397 if ( !m_lineHeight
)
2399 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2401 wxClientDC
dc( self
);
2402 dc
.SetFont( GetFont() );
2405 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2407 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2410 m_small_image_list
->GetSize(0, iw
, ih
);
2415 self
->m_lineHeight
= y
+ LINE_SPACING
;
2418 return m_lineHeight
;
2421 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2423 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2425 return LINE_SPACING
+ line
* GetLineHeight();
2428 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2430 if ( !InReportView() )
2431 return GetLine(line
)->m_gi
->m_rectAll
;
2434 rect
.x
= HEADER_OFFSET_X
;
2435 rect
.y
= GetLineY(line
);
2436 rect
.width
= GetHeaderWidth();
2437 rect
.height
= GetLineHeight();
2442 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2444 if ( !InReportView() )
2445 return GetLine(line
)->m_gi
->m_rectLabel
;
2448 rect
.x
= HEADER_OFFSET_X
;
2449 rect
.y
= GetLineY(line
);
2450 rect
.width
= GetColumnWidth(0);
2451 rect
.height
= GetLineHeight();
2456 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2458 if ( !InReportView() )
2459 return GetLine(line
)->m_gi
->m_rectIcon
;
2461 wxListLineData
*ld
= GetLine(line
);
2462 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2465 rect
.x
= HEADER_OFFSET_X
;
2466 rect
.y
= GetLineY(line
);
2467 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2472 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2474 return InReportView() ? GetLineRect(line
)
2475 : GetLine(line
)->m_gi
->m_rectHighlight
;
2478 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2480 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2482 wxListLineData
*ld
= GetLine(line
);
2484 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2485 return wxLIST_HITTEST_ONITEMICON
;
2487 // VS: Testing for "ld->HasText() || InReportView()" instead of
2488 // "ld->HasText()" is needed to make empty lines in report view
2490 if ( ld
->HasText() || InReportView() )
2492 wxRect rect
= InReportView() ? GetLineRect(line
)
2493 : GetLineLabelRect(line
);
2495 if ( rect
.Contains(x
, y
) )
2496 return wxLIST_HITTEST_ONITEMLABEL
;
2502 // ----------------------------------------------------------------------------
2503 // highlight (selection) handling
2504 // ----------------------------------------------------------------------------
2506 bool wxListMainWindow::IsHighlighted(size_t line
) const
2510 return m_selStore
.IsSelected(line
);
2514 wxListLineData
*ld
= GetLine(line
);
2515 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2517 return ld
->IsHighlighted();
2521 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2527 wxArrayInt linesChanged
;
2528 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2531 // meny items changed state, refresh everything
2532 RefreshLines(lineFrom
, lineTo
);
2534 else // only a few items changed state, refresh only them
2536 size_t count
= linesChanged
.GetCount();
2537 for ( size_t n
= 0; n
< count
; n
++ )
2539 RefreshLine(linesChanged
[n
]);
2543 else // iterate over all items in non report view
2545 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2547 if ( HighlightLine(line
, highlight
) )
2553 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2559 changed
= m_selStore
.SelectItem(line
, highlight
);
2563 wxListLineData
*ld
= GetLine(line
);
2564 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2566 changed
= ld
->Highlight(highlight
);
2571 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2572 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2578 void wxListMainWindow::RefreshLine( size_t line
)
2580 if ( InReportView() )
2582 size_t visibleFrom
, visibleTo
;
2583 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2585 if ( line
< visibleFrom
|| line
> visibleTo
)
2589 wxRect rect
= GetLineRect(line
);
2591 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2592 RefreshRect( rect
);
2595 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2597 // we suppose that they are ordered by caller
2598 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2600 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2602 if ( InReportView() )
2604 size_t visibleFrom
, visibleTo
;
2605 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2607 if ( lineFrom
< visibleFrom
)
2608 lineFrom
= visibleFrom
;
2609 if ( lineTo
> visibleTo
)
2614 rect
.y
= GetLineY(lineFrom
);
2615 rect
.width
= GetClientSize().x
;
2616 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2618 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2619 RefreshRect( rect
);
2623 // TODO: this should be optimized...
2624 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2631 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2633 if ( InReportView() )
2635 size_t visibleFrom
, visibleTo
;
2636 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2638 if ( lineFrom
< visibleFrom
)
2639 lineFrom
= visibleFrom
;
2640 else if ( lineFrom
> visibleTo
)
2645 rect
.y
= GetLineY(lineFrom
);
2646 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2648 wxSize size
= GetClientSize();
2649 rect
.width
= size
.x
;
2651 // refresh till the bottom of the window
2652 rect
.height
= size
.y
- rect
.y
;
2654 RefreshRect( rect
);
2658 // TODO: how to do it more efficiently?
2663 void wxListMainWindow::RefreshSelected()
2669 if ( InReportView() )
2671 GetVisibleLinesRange(&from
, &to
);
2676 to
= GetItemCount() - 1;
2679 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2680 RefreshLine(m_current
);
2682 for ( size_t line
= from
; line
<= to
; line
++ )
2684 // NB: the test works as expected even if m_current == -1
2685 if ( line
!= m_current
&& IsHighlighted(line
) )
2690 void wxListMainWindow::Freeze()
2695 void wxListMainWindow::Thaw()
2697 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2699 if ( --m_freezeCount
== 0 )
2703 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2705 // Note: a wxPaintDC must be constructed even if no drawing is
2706 // done (a Windows requirement).
2707 wxPaintDC
dc( this );
2709 if ( IsEmpty() || m_freezeCount
)
2710 // nothing to draw or not the moment to draw it
2714 // delay the repainting until we calculate all the items positions
2720 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2722 dc
.SetFont( GetFont() );
2724 if ( InReportView() )
2726 int lineHeight
= GetLineHeight();
2728 size_t visibleFrom
, visibleTo
;
2729 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2732 int xOrig
= dc
.LogicalToDeviceX( 0 );
2733 int yOrig
= dc
.LogicalToDeviceY( 0 );
2735 // tell the caller cache to cache the data
2738 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2739 GetParent()->GetId());
2740 evCache
.SetEventObject( GetParent() );
2741 evCache
.m_oldItemIndex
= visibleFrom
;
2742 evCache
.m_itemIndex
= visibleTo
;
2743 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2746 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2748 rectLine
= GetLineRect(line
);
2751 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2752 rectLine
.width
, rectLine
.height
) )
2754 // don't redraw unaffected lines to avoid flicker
2758 GetLine(line
)->DrawInReportMode( &dc
,
2760 GetLineHighlightRect(line
),
2761 IsHighlighted(line
) );
2764 if ( HasFlag(wxLC_HRULES
) )
2766 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2767 wxSize clientSize
= GetClientSize();
2769 // Don't draw the first one
2770 for ( size_t i
= visibleFrom
+ 1; i
<= visibleTo
; i
++ )
2773 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2774 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2775 clientSize
.x
- dev_x
, i
* lineHeight
);
2778 // Draw last horizontal rule
2779 if ( visibleTo
== GetItemCount() - 1 )
2782 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2783 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2784 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2788 // Draw vertical rules if required
2789 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2791 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2792 wxRect firstItemRect
, lastItemRect
;
2794 GetItemRect(visibleFrom
, firstItemRect
);
2795 GetItemRect(visibleTo
, lastItemRect
);
2796 int x
= firstItemRect
.GetX();
2798 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2800 for (int col
= 0; col
< GetColumnCount(); col
++)
2802 int colWidth
= GetColumnWidth(col
);
2804 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2805 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2811 size_t count
= GetItemCount();
2812 for ( size_t i
= 0; i
< count
; i
++ )
2814 GetLine(i
)->Draw( &dc
);
2819 // Don't draw rect outline under Mac at all.
2824 wxRect
rect( GetLineHighlightRect( m_current
) );
2826 dc
.SetPen( *wxBLACK_PEN
);
2827 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2828 dc
.DrawRectangle( rect
);
2830 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2838 void wxListMainWindow::HighlightAll( bool on
)
2840 if ( IsSingleSel() )
2842 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2844 // we just have one item to turn off
2845 if ( HasCurrent() && IsHighlighted(m_current
) )
2847 HighlightLine(m_current
, false);
2848 RefreshLine(m_current
);
2853 HighlightLines(0, GetItemCount() - 1, on
);
2857 void wxListMainWindow::SendNotify( size_t line
,
2858 wxEventType command
,
2859 const wxPoint
& point
)
2861 wxListEvent
le( command
, GetParent()->GetId() );
2862 le
.SetEventObject( GetParent() );
2864 le
.m_itemIndex
= line
;
2866 // set only for events which have position
2867 if ( point
!= wxDefaultPosition
)
2868 le
.m_pointDrag
= point
;
2870 // don't try to get the line info for virtual list controls: the main
2871 // program has it anyhow and if we did it would result in accessing all
2872 // the lines, even those which are not visible now and this is precisely
2873 // what we're trying to avoid
2876 if ( line
!= (size_t)-1 )
2878 GetLine(line
)->GetItem( 0, le
.m_item
);
2880 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2882 //else: there may be no more such item
2884 GetParent()->GetEventHandler()->ProcessEvent( le
);
2887 void wxListMainWindow::ChangeCurrent(size_t current
)
2889 m_current
= current
;
2891 // as the current item changed, we shouldn't start editing it when the
2892 // "slow click" timer expires as the click happened on another item
2893 if ( m_renameTimer
->IsRunning() )
2894 m_renameTimer
->Stop();
2896 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2899 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2901 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2902 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2904 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2905 wxT("EditLabel() needs a text control") );
2907 size_t itemEdit
= (size_t)item
;
2909 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2910 le
.SetEventObject( GetParent() );
2911 le
.m_itemIndex
= item
;
2912 wxListLineData
*data
= GetLine(itemEdit
);
2913 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2914 data
->GetItem( 0, le
.m_item
);
2916 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2918 // vetoed by user code
2922 // We have to call this here because the label in question might just have
2923 // been added and no screen update taken place.
2928 // Pending events dispatched by wxSafeYield might have changed the item
2930 if ( (size_t)item
>= GetItemCount() )
2934 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2935 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2936 return m_textctrlWrapper
->GetText();
2939 void wxListMainWindow::OnRenameTimer()
2941 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2943 EditLabel( m_current
);
2946 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2948 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2949 le
.SetEventObject( GetParent() );
2950 le
.m_itemIndex
= itemEdit
;
2952 wxListLineData
*data
= GetLine(itemEdit
);
2954 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2956 data
->GetItem( 0, le
.m_item
);
2957 le
.m_item
.m_text
= value
;
2958 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2962 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2964 // let owner know that the edit was cancelled
2965 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2967 le
.SetEditCanceled(true);
2969 le
.SetEventObject( GetParent() );
2970 le
.m_itemIndex
= itemEdit
;
2972 wxListLineData
*data
= GetLine(itemEdit
);
2973 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2975 data
->GetItem( 0, le
.m_item
);
2976 GetEventHandler()->ProcessEvent( le
);
2979 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2983 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2984 // shutdown the edit control when the mouse is clicked elsewhere on the
2985 // listctrl because the order of events is different (or something like
2986 // that), so explicitly end the edit if it is active.
2987 if ( event
.LeftDown() && m_textctrlWrapper
)
2988 m_textctrlWrapper
->AcceptChangesAndFinish();
2991 if ( event
.LeftDown() )
2994 event
.SetEventObject( GetParent() );
2995 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2998 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3000 // let the base handle mouse wheel events.
3005 if ( !HasCurrent() || IsEmpty() )
3007 if (event
.RightDown())
3009 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3010 // Allow generation of context menu event
3019 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3020 event
.ButtonDClick()) )
3023 int x
= event
.GetX();
3024 int y
= event
.GetY();
3025 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3027 // where did we hit it (if we did)?
3030 size_t count
= GetItemCount(),
3033 if ( InReportView() )
3035 current
= y
/ GetLineHeight();
3036 if ( current
< count
)
3037 hitResult
= HitTestLine(current
, x
, y
);
3041 // TODO: optimize it too! this is less simple than for report view but
3042 // enumerating all items is still not a way to do it!!
3043 for ( current
= 0; current
< count
; current
++ )
3045 hitResult
= HitTestLine(current
, x
, y
);
3051 if (event
.Dragging())
3053 if (m_dragCount
== 0)
3055 // we have to report the raw, physical coords as we want to be
3056 // able to call HitTest(event.m_pointDrag) from the user code to
3057 // get the item being dragged
3058 m_dragStart
= event
.GetPosition();
3063 if (m_dragCount
!= 3)
3066 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3067 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3069 wxListEvent
le( command
, GetParent()->GetId() );
3070 le
.SetEventObject( GetParent() );
3071 le
.m_itemIndex
= m_lineLastClicked
;
3072 le
.m_pointDrag
= m_dragStart
;
3073 GetParent()->GetEventHandler()->ProcessEvent( le
);
3084 // outside of any item
3085 if (event
.RightDown())
3087 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3088 // Allow generation of context menu event
3093 // reset the selection and bail out
3094 HighlightAll(false);
3100 bool forceClick
= false;
3101 if (event
.ButtonDClick())
3103 if ( m_renameTimer
->IsRunning() )
3104 m_renameTimer
->Stop();
3106 m_lastOnSame
= false;
3108 if ( current
== m_lineLastClicked
)
3110 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3116 // The first click was on another item, so don't interpret this as
3117 // a double click, but as a simple click instead
3124 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3126 // select single line
3127 HighlightAll( false );
3128 ReverseHighlight(m_lineSelectSingleOnUp
);
3133 if ((current
== m_current
) &&
3134 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3135 HasFlag(wxLC_EDIT_LABELS
) )
3137 m_renameTimer
->Start( 100, true );
3141 m_lastOnSame
= false;
3142 m_lineSelectSingleOnUp
= (size_t)-1;
3146 // This is necessary, because after a DnD operation in
3147 // from and to ourself, the up event is swallowed by the
3148 // DnD code. So on next non-up event (which means here and
3149 // now) m_lineSelectSingleOnUp should be reset.
3150 m_lineSelectSingleOnUp
= (size_t)-1;
3152 if (event
.RightDown())
3154 m_lineBeforeLastClicked
= m_lineLastClicked
;
3155 m_lineLastClicked
= current
;
3157 // If the item is already selected, do not update the selection.
3158 // Multi-selections should not be cleared if a selected item is clicked.
3159 if (!IsHighlighted(current
))
3161 HighlightAll(false);
3162 ChangeCurrent(current
);
3163 ReverseHighlight(m_current
);
3166 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3168 // Allow generation of context menu event
3171 else if (event
.MiddleDown())
3173 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3175 else if ( event
.LeftDown() || forceClick
)
3177 m_lineBeforeLastClicked
= m_lineLastClicked
;
3178 m_lineLastClicked
= current
;
3180 size_t oldCurrent
= m_current
;
3181 bool oldWasSelected
= IsHighlighted(m_current
);
3183 bool cmdModifierDown
= event
.CmdDown();
3184 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3186 if ( IsSingleSel() || !IsHighlighted(current
) )
3188 HighlightAll( false );
3190 ChangeCurrent(current
);
3192 ReverseHighlight(m_current
);
3194 else // multi sel & current is highlighted & no mod keys
3196 m_lineSelectSingleOnUp
= current
;
3197 ChangeCurrent(current
); // change focus
3200 else // multi sel & either ctrl or shift is down
3202 if (cmdModifierDown
)
3204 ChangeCurrent(current
);
3206 ReverseHighlight(m_current
);
3208 else if (event
.ShiftDown())
3210 ChangeCurrent(current
);
3212 size_t lineFrom
= oldCurrent
,
3215 if ( lineTo
< lineFrom
)
3218 lineFrom
= m_current
;
3221 HighlightLines(lineFrom
, lineTo
);
3223 else // !ctrl, !shift
3225 // test in the enclosing if should make it impossible
3226 wxFAIL_MSG( _T("how did we get here?") );
3230 if (m_current
!= oldCurrent
)
3231 RefreshLine( oldCurrent
);
3233 // forceClick is only set if the previous click was on another item
3234 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3238 void wxListMainWindow::MoveToItem(size_t item
)
3240 if ( item
== (size_t)-1 )
3243 wxRect rect
= GetLineRect(item
);
3245 int client_w
, client_h
;
3246 GetClientSize( &client_w
, &client_h
);
3248 const int hLine
= GetLineHeight();
3250 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3251 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3253 if ( InReportView() )
3255 // the next we need the range of lines shown it might be different,
3256 // so recalculate it
3257 ResetVisibleLinesRange();
3259 if (rect
.y
< view_y
)
3260 Scroll( -1, rect
.y
/ hLine
);
3261 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3262 Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3266 if (rect
.x
-view_x
< 5)
3267 Scroll( (rect
.x
- 5) / SCROLL_UNIT_X
, -1 );
3268 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3269 Scroll( (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
, -1 );
3273 // ----------------------------------------------------------------------------
3274 // keyboard handling
3275 // ----------------------------------------------------------------------------
3277 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3279 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3280 _T("invalid item index in OnArrowChar()") );
3282 size_t oldCurrent
= m_current
;
3284 // in single selection we just ignore Shift as we can't select several
3286 if ( event
.ShiftDown() && !IsSingleSel() )
3288 ChangeCurrent(newCurrent
);
3290 // refresh the old focus to remove it
3291 RefreshLine( oldCurrent
);
3293 // select all the items between the old and the new one
3294 if ( oldCurrent
> newCurrent
)
3296 newCurrent
= oldCurrent
;
3297 oldCurrent
= m_current
;
3300 HighlightLines(oldCurrent
, newCurrent
);
3304 // all previously selected items are unselected unless ctrl is held
3305 if ( !event
.ControlDown() )
3306 HighlightAll(false);
3308 ChangeCurrent(newCurrent
);
3310 // refresh the old focus to remove it
3311 RefreshLine( oldCurrent
);
3313 if ( !event
.ControlDown() )
3315 HighlightLine( m_current
, true );
3319 RefreshLine( m_current
);
3324 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3326 wxWindow
*parent
= GetParent();
3328 // propagate the key event upwards
3329 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3330 ke
.m_shiftDown
= event
.m_shiftDown
;
3331 ke
.m_controlDown
= event
.m_controlDown
;
3332 ke
.m_altDown
= event
.m_altDown
;
3333 ke
.m_metaDown
= event
.m_metaDown
;
3334 ke
.m_keyCode
= event
.m_keyCode
;
3337 ke
.SetEventObject( parent
);
3338 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3343 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3345 wxWindow
*parent
= GetParent();
3347 // propagate the key event upwards
3348 wxKeyEvent
ke( wxEVT_KEY_UP
);
3349 ke
.m_shiftDown
= event
.m_shiftDown
;
3350 ke
.m_controlDown
= event
.m_controlDown
;
3351 ke
.m_altDown
= event
.m_altDown
;
3352 ke
.m_metaDown
= event
.m_metaDown
;
3353 ke
.m_keyCode
= event
.m_keyCode
;
3356 ke
.SetEventObject( parent
);
3357 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3362 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3364 wxWindow
*parent
= GetParent();
3366 // send a list_key event up
3369 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3370 le
.m_itemIndex
= m_current
;
3371 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3372 le
.m_code
= event
.GetKeyCode();
3373 le
.SetEventObject( parent
);
3374 parent
->GetEventHandler()->ProcessEvent( le
);
3377 // propagate the char event upwards
3378 wxKeyEvent
ke( wxEVT_CHAR
);
3379 ke
.m_shiftDown
= event
.m_shiftDown
;
3380 ke
.m_controlDown
= event
.m_controlDown
;
3381 ke
.m_altDown
= event
.m_altDown
;
3382 ke
.m_metaDown
= event
.m_metaDown
;
3383 ke
.m_keyCode
= event
.m_keyCode
;
3386 ke
.SetEventObject( parent
);
3387 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3389 if (event
.GetKeyCode() == WXK_TAB
)
3391 wxNavigationKeyEvent nevent
;
3392 nevent
.SetWindowChange( event
.ControlDown() );
3393 nevent
.SetDirection( !event
.ShiftDown() );
3394 nevent
.SetEventObject( GetParent()->GetParent() );
3395 nevent
.SetCurrentFocus( m_parent
);
3396 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3400 // no item -> nothing to do
3407 // don't use m_linesPerPage directly as it might not be computed yet
3408 const int pageSize
= GetCountPerPage();
3409 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3411 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3413 if (event
.GetKeyCode() == WXK_RIGHT
)
3414 event
.m_keyCode
= WXK_LEFT
;
3415 else if (event
.GetKeyCode() == WXK_LEFT
)
3416 event
.m_keyCode
= WXK_RIGHT
;
3419 switch ( event
.GetKeyCode() )
3422 if ( m_current
> 0 )
3423 OnArrowChar( m_current
- 1, event
);
3427 if ( m_current
< (size_t)GetItemCount() - 1 )
3428 OnArrowChar( m_current
+ 1, event
);
3433 OnArrowChar( GetItemCount() - 1, event
);
3438 OnArrowChar( 0, event
);
3443 int steps
= InReportView() ? pageSize
- 1
3444 : m_current
% pageSize
;
3446 int index
= m_current
- steps
;
3450 OnArrowChar( index
, event
);
3456 int steps
= InReportView()
3458 : pageSize
- (m_current
% pageSize
) - 1;
3460 size_t index
= m_current
+ steps
;
3461 size_t count
= GetItemCount();
3462 if ( index
>= count
)
3465 OnArrowChar( index
, event
);
3470 if ( !InReportView() )
3472 int index
= m_current
- pageSize
;
3476 OnArrowChar( index
, event
);
3481 if ( !InReportView() )
3483 size_t index
= m_current
+ pageSize
;
3485 size_t count
= GetItemCount();
3486 if ( index
>= count
)
3489 OnArrowChar( index
, event
);
3494 if ( IsSingleSel() )
3496 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3498 if ( IsHighlighted(m_current
) )
3500 // don't unselect the item in single selection mode
3503 //else: select it in ReverseHighlight() below if unselected
3506 ReverseHighlight(m_current
);
3511 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3519 // ----------------------------------------------------------------------------
3521 // ----------------------------------------------------------------------------
3523 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3527 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3528 event
.SetEventObject( GetParent() );
3529 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3533 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3534 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3535 // which are already drawn correctly resulting in horrible flicker - avoid
3545 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3549 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3550 event
.SetEventObject( GetParent() );
3551 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3559 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3561 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3563 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3565 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3567 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3569 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3571 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3573 else if ( InReportView() && (m_small_image_list
))
3575 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3579 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3581 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3583 m_normal_image_list
->GetSize( index
, width
, height
);
3585 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3587 m_small_image_list
->GetSize( index
, width
, height
);
3589 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3591 m_small_image_list
->GetSize( index
, width
, height
);
3593 else if ( InReportView() && m_small_image_list
)
3595 m_small_image_list
->GetSize( index
, width
, height
);
3604 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3606 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3607 dc
.SetFont( GetFont() );
3610 dc
.GetTextExtent( s
, &lw
, NULL
);
3612 return lw
+ AUTOSIZE_COL_MARGIN
;
3615 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3619 // calc the spacing from the icon size
3620 int width
= 0, height
= 0;
3622 if ((imageList
) && (imageList
->GetImageCount()) )
3623 imageList
->GetSize(0, width
, height
);
3625 if (which
== wxIMAGE_LIST_NORMAL
)
3627 m_normal_image_list
= imageList
;
3628 m_normal_spacing
= width
+ 8;
3631 if (which
== wxIMAGE_LIST_SMALL
)
3633 m_small_image_list
= imageList
;
3634 m_small_spacing
= width
+ 14;
3635 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3639 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3643 m_small_spacing
= spacing
;
3645 m_normal_spacing
= spacing
;
3648 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3650 return isSmall
? m_small_spacing
: m_normal_spacing
;
3653 // ----------------------------------------------------------------------------
3655 // ----------------------------------------------------------------------------
3657 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3659 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3661 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3663 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3664 item
.m_width
= GetTextLength( item
.m_text
);
3666 wxListHeaderData
*column
= node
->GetData();
3667 column
->SetItem( item
);
3669 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3671 headerWin
->m_dirty
= true;
3675 // invalidate it as it has to be recalculated
3679 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3681 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3682 _T("invalid column index") );
3684 wxCHECK_RET( InReportView(),
3685 _T("SetColumnWidth() can only be called in report mode.") );
3688 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3690 headerWin
->m_dirty
= true;
3692 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3693 wxCHECK_RET( node
, _T("no column?") );
3695 wxListHeaderData
*column
= node
->GetData();
3697 size_t count
= GetItemCount();
3699 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3701 width
= GetTextLength(column
->GetText());
3703 else if ( width
== wxLIST_AUTOSIZE
)
3707 // TODO: determine the max width somehow...
3708 width
= WIDTH_COL_DEFAULT
;
3712 wxClientDC
dc(this);
3713 dc
.SetFont( GetFont() );
3715 int max
= AUTOSIZE_COL_MARGIN
;
3717 // if the cached column width isn't valid then recalculate it
3718 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3720 for (size_t i
= 0; i
< count
; i
++)
3722 wxListLineData
*line
= GetLine( i
);
3723 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3725 wxCHECK_RET( n
, _T("no subitem?") );
3727 wxListItemData
*itemData
= n
->GetData();
3730 itemData
->GetItem(item
);
3731 int itemWidth
= GetItemWidthWithImage(&item
);
3732 if (itemWidth
> max
)
3736 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3737 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3740 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3741 width
= max
+ AUTOSIZE_COL_MARGIN
;
3745 column
->SetWidth( width
);
3747 // invalidate it as it has to be recalculated
3751 int wxListMainWindow::GetHeaderWidth() const
3753 if ( !m_headerWidth
)
3755 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3757 size_t count
= GetColumnCount();
3758 for ( size_t col
= 0; col
< count
; col
++ )
3760 self
->m_headerWidth
+= GetColumnWidth(col
);
3764 return m_headerWidth
;
3767 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3769 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3770 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3772 wxListHeaderData
*column
= node
->GetData();
3773 column
->GetItem( item
);
3776 int wxListMainWindow::GetColumnWidth( int col
) const
3778 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3779 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3781 wxListHeaderData
*column
= node
->GetData();
3782 return column
->GetWidth();
3785 // ----------------------------------------------------------------------------
3787 // ----------------------------------------------------------------------------
3789 void wxListMainWindow::SetItem( wxListItem
&item
)
3791 long id
= item
.m_itemId
;
3792 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3793 _T("invalid item index in SetItem") );
3797 wxListLineData
*line
= GetLine((size_t)id
);
3798 line
->SetItem( item
.m_col
, item
);
3800 // Set item state if user wants
3801 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3802 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3806 // update the Max Width Cache if needed
3807 int width
= GetItemWidthWithImage(&item
);
3809 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3810 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3814 // update the item on screen
3816 GetItemRect(id
, rectItem
);
3817 RefreshRect(rectItem
);
3820 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3825 // first deal with selection
3826 if ( stateMask
& wxLIST_STATE_SELECTED
)
3828 // set/clear select state
3831 // optimized version for virtual listctrl.
3832 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3835 else if ( state
& wxLIST_STATE_SELECTED
)
3837 const long count
= GetItemCount();
3838 for( long i
= 0; i
< count
; i
++ )
3840 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3846 // clear for non virtual (somewhat optimized by using GetNextItem())
3848 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3850 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3855 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3857 // unfocus all: only one item can be focussed, so clearing focus for
3858 // all items is simply clearing focus of the focussed item.
3859 SetItemState(m_current
, state
, stateMask
);
3861 //(setting focus to all items makes no sense, so it is not handled here.)
3864 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3868 SetItemStateAll(state
, stateMask
);
3872 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3873 _T("invalid list ctrl item index in SetItem") );
3875 size_t oldCurrent
= m_current
;
3876 size_t item
= (size_t)litem
; // safe because of the check above
3878 // do we need to change the focus?
3879 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3881 if ( state
& wxLIST_STATE_FOCUSED
)
3883 // don't do anything if this item is already focused
3884 if ( item
!= m_current
)
3886 ChangeCurrent(item
);
3888 if ( oldCurrent
!= (size_t)-1 )
3890 if ( IsSingleSel() )
3892 HighlightLine(oldCurrent
, false);
3895 RefreshLine(oldCurrent
);
3898 RefreshLine( m_current
);
3903 // don't do anything if this item is not focused
3904 if ( item
== m_current
)
3908 if ( IsSingleSel() )
3910 // we must unselect the old current item as well or we
3911 // might end up with more than one selected item in a
3912 // single selection control
3913 HighlightLine(oldCurrent
, false);
3916 RefreshLine( oldCurrent
);
3921 // do we need to change the selection state?
3922 if ( stateMask
& wxLIST_STATE_SELECTED
)
3924 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3926 if ( IsSingleSel() )
3930 // selecting the item also makes it the focused one in the
3932 if ( m_current
!= item
)
3934 ChangeCurrent(item
);
3936 if ( oldCurrent
!= (size_t)-1 )
3938 HighlightLine( oldCurrent
, false );
3939 RefreshLine( oldCurrent
);
3945 // only the current item may be selected anyhow
3946 if ( item
!= m_current
)
3951 if ( HighlightLine(item
, on
) )
3958 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3960 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3961 _T("invalid list ctrl item index in GetItemState()") );
3963 int ret
= wxLIST_STATE_DONTCARE
;
3965 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3967 if ( (size_t)item
== m_current
)
3968 ret
|= wxLIST_STATE_FOCUSED
;
3971 if ( stateMask
& wxLIST_STATE_SELECTED
)
3973 if ( IsHighlighted(item
) )
3974 ret
|= wxLIST_STATE_SELECTED
;
3980 void wxListMainWindow::GetItem( wxListItem
&item
) const
3982 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3983 _T("invalid item index in GetItem") );
3985 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3986 line
->GetItem( item
.m_col
, item
);
3988 // Get item state if user wants it
3989 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3990 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
3991 wxLIST_STATE_FOCUSED
);
3994 // ----------------------------------------------------------------------------
3996 // ----------------------------------------------------------------------------
3998 size_t wxListMainWindow::GetItemCount() const
4000 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4003 void wxListMainWindow::SetItemCount(long count
)
4005 m_selStore
.SetItemCount(count
);
4006 m_countVirt
= count
;
4008 ResetVisibleLinesRange();
4010 // scrollbars must be reset
4014 int wxListMainWindow::GetSelectedItemCount() const
4016 // deal with the quick case first
4017 if ( IsSingleSel() )
4018 return HasCurrent() ? IsHighlighted(m_current
) : false;
4020 // virtual controls remmebers all its selections itself
4022 return m_selStore
.GetSelectedCount();
4024 // TODO: we probably should maintain the number of items selected even for
4025 // non virtual controls as enumerating all lines is really slow...
4026 size_t countSel
= 0;
4027 size_t count
= GetItemCount();
4028 for ( size_t line
= 0; line
< count
; line
++ )
4030 if ( GetLine(line
)->IsHighlighted() )
4037 // ----------------------------------------------------------------------------
4038 // item position/size
4039 // ----------------------------------------------------------------------------
4041 wxRect
wxListMainWindow::GetViewRect() const
4043 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
4044 _T("wxListCtrl::GetViewRect() only works in icon mode") );
4046 // we need to find the longest/tallest label
4047 wxCoord xMax
= 0, yMax
= 0;
4048 const int count
= GetItemCount();
4051 for ( int i
= 0; i
< count
; i
++ )
4056 wxCoord x
= r
.GetRight(),
4066 // some fudge needed to make it look prettier
4067 xMax
+= 2 * EXTRA_BORDER_X
;
4068 yMax
+= 2 * EXTRA_BORDER_Y
;
4070 // account for the scrollbars if necessary
4071 const wxSize sizeAll
= GetClientSize();
4072 if ( xMax
> sizeAll
.x
)
4073 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4074 if ( yMax
> sizeAll
.y
)
4075 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4077 return wxRect(0, 0, xMax
, yMax
);
4080 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
4082 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4083 _T("invalid index in GetItemRect") );
4085 // ensure that we're laid out, otherwise we could return nonsense
4088 wxConstCast(this, wxListMainWindow
)->
4089 RecalculatePositions(true /* no refresh */);
4092 rect
= GetLineRect((size_t)index
);
4094 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4097 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4100 GetItemRect(item
, rect
);
4108 // ----------------------------------------------------------------------------
4109 // geometry calculation
4110 // ----------------------------------------------------------------------------
4112 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4114 const int lineHeight
= GetLineHeight();
4116 wxClientDC
dc( this );
4117 dc
.SetFont( GetFont() );
4119 const size_t count
= GetItemCount();
4122 if ( HasFlag(wxLC_ICON
) )
4123 iconSpacing
= m_normal_spacing
;
4124 else if ( HasFlag(wxLC_SMALL_ICON
) )
4125 iconSpacing
= m_small_spacing
;
4129 // Note that we do not call GetClientSize() here but
4130 // GetSize() and subtract the border size for sunken
4131 // borders manually. This is technically incorrect,
4132 // but we need to know the client area's size WITHOUT
4133 // scrollbars here. Since we don't know if there are
4134 // any scrollbars, we use GetSize() instead. Another
4135 // solution would be to call SetScrollbars() here to
4136 // remove the scrollbars and call GetClientSize() then,
4137 // but this might result in flicker and - worse - will
4138 // reset the scrollbars to 0 which is not good at all
4139 // if you resize a dialog/window, but don't want to
4140 // reset the window scrolling. RR.
4141 // Furthermore, we actually do NOT subtract the border
4142 // width as 2 pixels is just the extra space which we
4143 // need around the actual content in the window. Other-
4144 // wise the text would e.g. touch the upper border. RR.
4147 GetSize( &clientWidth
, &clientHeight
);
4149 if ( InReportView() )
4151 // all lines have the same height and we scroll one line per step
4152 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4154 m_linesPerPage
= clientHeight
/ lineHeight
;
4156 ResetVisibleLinesRange();
4158 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4159 GetHeaderWidth() / SCROLL_UNIT_X
,
4160 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4161 GetScrollPos(wxHORIZONTAL
),
4162 GetScrollPos(wxVERTICAL
),
4167 // we have 3 different layout strategies: either layout all items
4168 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4169 // to arrange them in top to bottom, left to right (don't ask me why
4170 // not the other way round...) order
4171 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4173 int x
= EXTRA_BORDER_X
;
4174 int y
= EXTRA_BORDER_Y
;
4176 wxCoord widthMax
= 0;
4179 for ( i
= 0; i
< count
; i
++ )
4181 wxListLineData
*line
= GetLine(i
);
4182 line
->CalculateSize( &dc
, iconSpacing
);
4183 line
->SetPosition( x
, y
, iconSpacing
);
4185 wxSize sizeLine
= GetLineSize(i
);
4187 if ( HasFlag(wxLC_ALIGN_TOP
) )
4189 if ( sizeLine
.x
> widthMax
)
4190 widthMax
= sizeLine
.x
;
4194 else // wxLC_ALIGN_LEFT
4196 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4200 if ( HasFlag(wxLC_ALIGN_TOP
) )
4202 // traverse the items again and tweak their sizes so that they are
4203 // all the same in a row
4204 for ( i
= 0; i
< count
; i
++ )
4206 wxListLineData
*line
= GetLine(i
);
4207 line
->m_gi
->ExtendWidth(widthMax
);
4215 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4216 (y
+ lineHeight
) / lineHeight
,
4217 GetScrollPos( wxHORIZONTAL
),
4218 GetScrollPos( wxVERTICAL
),
4222 else // "flowed" arrangement, the most complicated case
4224 // at first we try without any scrollbars, if the items don't fit into
4225 // the window, we recalculate after subtracting the space taken by the
4228 int entireWidth
= 0;
4230 for (int tries
= 0; tries
< 2; tries
++)
4232 entireWidth
= 2 * EXTRA_BORDER_X
;
4236 // Now we have decided that the items do not fit into the
4237 // client area, so we need a scrollbar
4238 entireWidth
+= SCROLL_UNIT_X
;
4241 int x
= EXTRA_BORDER_X
;
4242 int y
= EXTRA_BORDER_Y
;
4243 int maxWidthInThisRow
= 0;
4246 int currentlyVisibleLines
= 0;
4248 for (size_t i
= 0; i
< count
; i
++)
4250 currentlyVisibleLines
++;
4251 wxListLineData
*line
= GetLine( i
);
4252 line
->CalculateSize( &dc
, iconSpacing
);
4253 line
->SetPosition( x
, y
, iconSpacing
);
4255 wxSize sizeLine
= GetLineSize( i
);
4257 if ( maxWidthInThisRow
< sizeLine
.x
)
4258 maxWidthInThisRow
= sizeLine
.x
;
4261 if (currentlyVisibleLines
> m_linesPerPage
)
4262 m_linesPerPage
= currentlyVisibleLines
;
4264 if ( y
+ sizeLine
.y
>= clientHeight
)
4266 currentlyVisibleLines
= 0;
4268 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4269 x
+= maxWidthInThisRow
;
4270 entireWidth
+= maxWidthInThisRow
;
4271 maxWidthInThisRow
= 0;
4274 // We have reached the last item.
4275 if ( i
== count
- 1 )
4276 entireWidth
+= maxWidthInThisRow
;
4278 if ( (tries
== 0) &&
4279 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4281 clientHeight
-= wxSystemSettings::
4282 GetMetric(wxSYS_HSCROLL_Y
);
4287 if ( i
== count
- 1 )
4288 tries
= 1; // Everything fits, no second try required.
4296 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4298 GetScrollPos( wxHORIZONTAL
),
4307 // FIXME: why should we call it from here?
4314 void wxListMainWindow::RefreshAll()
4319 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4320 if ( headerWin
&& headerWin
->m_dirty
)
4322 headerWin
->m_dirty
= false;
4323 headerWin
->Refresh();
4327 void wxListMainWindow::UpdateCurrent()
4329 if ( !HasCurrent() && !IsEmpty() )
4333 long wxListMainWindow::GetNextItem( long item
,
4334 int WXUNUSED(geometry
),
4338 max
= GetItemCount();
4339 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4340 _T("invalid listctrl index in GetNextItem()") );
4342 // notice that we start with the next item (or the first one if item == -1)
4343 // and this is intentional to allow writing a simple loop to iterate over
4344 // all selected items
4347 // this is not an error because the index was OK initially,
4348 // just no such item
4355 size_t count
= GetItemCount();
4356 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4358 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4361 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4368 // ----------------------------------------------------------------------------
4370 // ----------------------------------------------------------------------------
4372 void wxListMainWindow::DeleteItem( long lindex
)
4374 size_t count
= GetItemCount();
4376 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4377 _T("invalid item index in DeleteItem") );
4379 size_t index
= (size_t)lindex
;
4381 // we don't need to adjust the index for the previous items
4382 if ( HasCurrent() && m_current
>= index
)
4384 // if the current item is being deleted, we want the next one to
4385 // become selected - unless there is no next one - so don't adjust
4386 // m_current in this case
4387 if ( m_current
!= index
|| m_current
== count
- 1 )
4391 if ( InReportView() )
4393 // mark the Column Max Width cache as dirty if the items in the line
4394 // we're deleting contain the Max Column Width
4395 wxListLineData
* const line
= GetLine(index
);
4396 wxListItemDataList::compatibility_iterator n
;
4397 wxListItemData
*itemData
;
4401 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4403 n
= line
->m_items
.Item( i
);
4404 itemData
= n
->GetData();
4405 itemData
->GetItem(item
);
4407 itemWidth
= GetItemWidthWithImage(&item
);
4409 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4410 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4413 ResetVisibleLinesRange();
4416 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4421 m_selStore
.OnItemDelete(index
);
4425 m_lines
.RemoveAt( index
);
4428 // we need to refresh the (vert) scrollbar as the number of items changed
4431 RefreshAfter(index
);
4434 void wxListMainWindow::DeleteColumn( int col
)
4436 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4438 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4441 delete node
->GetData();
4442 m_columns
.Erase( node
);
4446 // update all the items
4447 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4449 wxListLineData
* const line
= GetLine(i
);
4450 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4451 delete n
->GetData();
4452 line
->m_items
.Erase(n
);
4456 if ( InReportView() ) // we only cache max widths when in Report View
4458 delete m_aColWidths
.Item(col
);
4459 m_aColWidths
.RemoveAt(col
);
4462 // invalidate it as it has to be recalculated
4466 void wxListMainWindow::DoDeleteAllItems()
4469 // nothing to do - in particular, don't send the event
4474 // to make the deletion of all items faster, we don't send the
4475 // notifications for each item deletion in this case but only one event
4476 // for all of them: this is compatible with wxMSW and documented in
4477 // DeleteAllItems() description
4479 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4480 event
.SetEventObject( GetParent() );
4481 GetParent()->GetEventHandler()->ProcessEvent( event
);
4489 if ( InReportView() )
4491 ResetVisibleLinesRange();
4492 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4494 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4501 void wxListMainWindow::DeleteAllItems()
4505 RecalculatePositions();
4508 void wxListMainWindow::DeleteEverything()
4510 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4511 WX_CLEAR_ARRAY(m_aColWidths
);
4516 // ----------------------------------------------------------------------------
4517 // scanning for an item
4518 // ----------------------------------------------------------------------------
4520 void wxListMainWindow::EnsureVisible( long index
)
4522 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4523 _T("invalid index in EnsureVisible") );
4525 // We have to call this here because the label in question might just have
4526 // been added and its position is not known yet
4528 RecalculatePositions(true /* no refresh */);
4530 MoveToItem((size_t)index
);
4533 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4539 wxString str_upper
= str
.Upper();
4543 size_t count
= GetItemCount();
4544 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4546 wxListLineData
*line
= GetLine(i
);
4547 wxString line_upper
= line
->GetText(0).Upper();
4550 if (line_upper
== str_upper
)
4555 if (line_upper
.find(str_upper
) == 0)
4563 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4569 size_t count
= GetItemCount();
4570 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4572 wxListLineData
*line
= GetLine(i
);
4574 line
->GetItem( 0, item
);
4575 if (item
.m_data
== data
)
4582 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4585 GetVisibleLinesRange( &topItem
, NULL
);
4588 GetItemPosition( GetItemCount() - 1, p
);
4592 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4593 if ( id
>= 0 && id
< (long)GetItemCount() )
4599 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4601 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4603 size_t count
= GetItemCount();
4605 if ( InReportView() )
4607 size_t current
= y
/ GetLineHeight();
4608 if ( current
< count
)
4610 flags
= HitTestLine(current
, x
, y
);
4617 // TODO: optimize it too! this is less simple than for report view but
4618 // enumerating all items is still not a way to do it!!
4619 for ( size_t current
= 0; current
< count
; current
++ )
4621 flags
= HitTestLine(current
, x
, y
);
4630 // ----------------------------------------------------------------------------
4632 // ----------------------------------------------------------------------------
4634 void wxListMainWindow::InsertItem( wxListItem
&item
)
4636 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4638 int count
= GetItemCount();
4639 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4641 if (item
.m_itemId
> count
)
4642 item
.m_itemId
= count
;
4644 size_t id
= item
.m_itemId
;
4648 if ( InReportView() )
4650 ResetVisibleLinesRange();
4652 // calculate the width of the item and adjust the max column width
4653 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4654 int width
= GetItemWidthWithImage(&item
);
4655 item
.SetWidth(width
);
4656 if (width
> pWidthInfo
->nMaxWidth
)
4657 pWidthInfo
->nMaxWidth
= width
;
4660 wxListLineData
*line
= new wxListLineData(this);
4662 line
->SetItem( item
.m_col
, item
);
4664 m_lines
.Insert( line
, id
);
4668 // If an item is selected at or below the point of insertion, we need to
4669 // increment the member variables because the current row's index has gone
4671 if ( HasCurrent() && m_current
>= id
)
4674 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4676 RefreshLines(id
, GetItemCount() - 1);
4679 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4682 if ( InReportView() )
4684 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4685 item
.m_width
= GetTextLength( item
.m_text
);
4687 wxListHeaderData
*column
= new wxListHeaderData( item
);
4688 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4690 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4693 wxListHeaderDataList::compatibility_iterator
4694 node
= m_columns
.Item( col
);
4695 m_columns
.Insert( node
, column
);
4696 m_aColWidths
.Insert( colWidthInfo
, col
);
4700 m_columns
.Append( column
);
4701 m_aColWidths
.Add( colWidthInfo
);
4706 // update all the items
4707 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4709 wxListLineData
* const line
= GetLine(i
);
4710 wxListItemData
* const data
= new wxListItemData(this);
4712 line
->m_items
.Insert(col
, data
);
4714 line
->m_items
.Append(data
);
4718 // invalidate it as it has to be recalculated
4723 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4726 wxClientDC
dc(this);
4728 dc
.SetFont( GetFont() );
4730 if (item
->GetImage() != -1)
4733 GetImageSize( item
->GetImage(), ix
, iy
);
4737 if (!item
->GetText().empty())
4740 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4747 // ----------------------------------------------------------------------------
4749 // ----------------------------------------------------------------------------
4751 wxListCtrlCompare list_ctrl_compare_func_2
;
4752 long list_ctrl_compare_data
;
4754 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4756 wxListLineData
*line1
= *arg1
;
4757 wxListLineData
*line2
= *arg2
;
4759 line1
->GetItem( 0, item
);
4760 wxUIntPtr data1
= item
.m_data
;
4761 line2
->GetItem( 0, item
);
4762 wxUIntPtr data2
= item
.m_data
;
4763 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4766 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4768 list_ctrl_compare_func_2
= fn
;
4769 list_ctrl_compare_data
= data
;
4770 m_lines
.Sort( list_ctrl_compare_func_1
);
4774 // ----------------------------------------------------------------------------
4776 // ----------------------------------------------------------------------------
4778 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4780 // update our idea of which lines are shown when we redraw the window the
4782 ResetVisibleLinesRange();
4785 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4786 wxScrolledWindow::OnScroll(event
);
4788 HandleOnScroll( event
);
4791 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4793 wxGenericListCtrl
* lc
= GetListCtrl();
4794 wxCHECK_RET( lc
, _T("no listctrl window?") );
4796 lc
->m_headerWin
->Refresh();
4797 lc
->m_headerWin
->Update();
4801 int wxListMainWindow::GetCountPerPage() const
4803 if ( !m_linesPerPage
)
4805 wxConstCast(this, wxListMainWindow
)->
4806 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4809 return m_linesPerPage
;
4812 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4814 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4816 if ( m_lineFrom
== (size_t)-1 )
4818 size_t count
= GetItemCount();
4821 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4823 // this may happen if SetScrollbars() hadn't been called yet
4824 if ( m_lineFrom
>= count
)
4825 m_lineFrom
= count
- 1;
4827 // we redraw one extra line but this is needed to make the redrawing
4828 // logic work when there is a fractional number of lines on screen
4829 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4830 if ( m_lineTo
>= count
)
4831 m_lineTo
= count
- 1;
4833 else // empty control
4836 m_lineTo
= (size_t)-1;
4840 wxASSERT_MSG( IsEmpty() ||
4841 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4842 _T("GetVisibleLinesRange() returns incorrect result") );
4850 // -------------------------------------------------------------------------------------
4851 // wxGenericListCtrl
4852 // -------------------------------------------------------------------------------------
4854 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4856 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4857 EVT_SIZE(wxGenericListCtrl::OnSize
)
4860 wxGenericListCtrl::wxGenericListCtrl()
4862 m_imageListNormal
= (wxImageList
*) NULL
;
4863 m_imageListSmall
= (wxImageList
*) NULL
;
4864 m_imageListState
= (wxImageList
*) NULL
;
4866 m_ownsImageListNormal
=
4867 m_ownsImageListSmall
=
4868 m_ownsImageListState
= false;
4870 m_mainWin
= (wxListMainWindow
*) NULL
;
4871 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4875 wxGenericListCtrl::~wxGenericListCtrl()
4877 if (m_ownsImageListNormal
)
4878 delete m_imageListNormal
;
4879 if (m_ownsImageListSmall
)
4880 delete m_imageListSmall
;
4881 if (m_ownsImageListState
)
4882 delete m_imageListState
;
4885 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4891 GetThemeMetric( kThemeMetricListHeaderHeight
, &h
);
4893 // we use 'g' to get the descent, too
4895 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4896 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4899 // only update if changed
4900 if ( h
!= m_headerHeight
)
4905 ResizeReportView(true);
4906 else //why is this needed if it doesn't have a header?
4907 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4912 void wxGenericListCtrl::CreateHeaderWindow()
4914 m_headerWin
= new wxListHeaderWindow
4916 this, wxID_ANY
, m_mainWin
,
4918 wxSize(GetClientSize().x
, m_headerHeight
),
4921 CalculateAndSetHeaderHeight();
4924 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4929 const wxValidator
&validator
,
4930 const wxString
&name
)
4934 m_imageListState
= (wxImageList
*) NULL
;
4935 m_ownsImageListNormal
=
4936 m_ownsImageListSmall
=
4937 m_ownsImageListState
= false;
4939 m_mainWin
= (wxListMainWindow
*) NULL
;
4940 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4944 if ( !(style
& wxLC_MASK_TYPE
) )
4946 style
= style
| wxLC_LIST
;
4949 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4952 // don't create the inner window with the border
4953 style
&= ~wxBORDER_MASK
;
4955 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
4957 #ifdef __WXMAC_CARBON__
4958 // Human Interface Guidelines ask us for a special font in this case
4959 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
)
4962 font
.MacCreateThemeFont( kThemeViewsFont
);
4967 if ( InReportView() )
4969 CreateHeaderWindow();
4971 #ifdef __WXMAC_CARBON__
4975 font
.MacCreateThemeFont( kThemeSmallSystemFont
);
4976 m_headerWin
->SetFont( font
);
4977 CalculateAndSetHeaderHeight();
4981 if ( HasFlag(wxLC_NO_HEADER
) )
4982 // VZ: why do we create it at all then?
4983 m_headerWin
->Show( false );
4986 SetInitialSize(size
);
4991 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4993 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4994 _T("wxLC_VIRTUAL can't be [un]set") );
4996 long flag
= GetWindowStyle();
5000 if (style
& wxLC_MASK_TYPE
)
5001 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5002 if (style
& wxLC_MASK_ALIGN
)
5003 flag
&= ~wxLC_MASK_ALIGN
;
5004 if (style
& wxLC_MASK_SORT
)
5005 flag
&= ~wxLC_MASK_SORT
;
5013 SetWindowStyleFlag( flag
);
5016 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5020 m_mainWin
->DeleteEverything();
5022 // has the header visibility changed?
5023 bool hasHeader
= HasHeader();
5024 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
5026 if ( hasHeader
!= willHaveHeader
)
5033 // don't delete, just hide, as we can reuse it later
5034 m_headerWin
->Show(false);
5036 //else: nothing to do
5038 else // must show header
5042 CreateHeaderWindow();
5044 else // already have it, just show
5046 m_headerWin
->Show( true );
5050 ResizeReportView(willHaveHeader
);
5054 wxWindow::SetWindowStyleFlag( flag
);
5057 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5059 m_mainWin
->GetColumn( col
, item
);
5063 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5065 m_mainWin
->SetColumn( col
, item
);
5069 int wxGenericListCtrl::GetColumnWidth( int col
) const
5071 return m_mainWin
->GetColumnWidth( col
);
5074 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5076 m_mainWin
->SetColumnWidth( col
, width
);
5080 int wxGenericListCtrl::GetCountPerPage() const
5082 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5085 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5087 m_mainWin
->GetItem( info
);
5091 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5093 m_mainWin
->SetItem( info
);
5097 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5100 info
.m_text
= label
;
5101 info
.m_mask
= wxLIST_MASK_TEXT
;
5102 info
.m_itemId
= index
;
5106 info
.m_image
= imageId
;
5107 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5110 m_mainWin
->SetItem(info
);
5114 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5116 return m_mainWin
->GetItemState( item
, stateMask
);
5119 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5121 m_mainWin
->SetItemState( item
, state
, stateMask
);
5126 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5128 return SetItemColumnImage(item
, 0, image
);
5132 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5135 info
.m_image
= image
;
5136 info
.m_mask
= wxLIST_MASK_IMAGE
;
5137 info
.m_itemId
= item
;
5138 info
.m_col
= column
;
5139 m_mainWin
->SetItem( info
);
5143 wxString
wxGenericListCtrl::GetItemText( long item
) const
5145 return m_mainWin
->GetItemText(item
);
5148 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5150 m_mainWin
->SetItemText(item
, str
);
5153 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5156 info
.m_mask
= wxLIST_MASK_DATA
;
5157 info
.m_itemId
= item
;
5158 m_mainWin
->GetItem( info
);
5162 bool wxGenericListCtrl::SetItemData( long item
, long data
)
5165 info
.m_mask
= wxLIST_MASK_DATA
;
5166 info
.m_itemId
= item
;
5168 m_mainWin
->SetItem( info
);
5172 wxRect
wxGenericListCtrl::GetViewRect() const
5174 return m_mainWin
->GetViewRect();
5177 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
5179 m_mainWin
->GetItemRect( item
, rect
);
5180 if ( m_mainWin
->HasHeader() )
5181 rect
.y
+= m_headerHeight
+ 1;
5185 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5187 m_mainWin
->GetItemPosition( item
, pos
);
5191 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5196 int wxGenericListCtrl::GetItemCount() const
5198 return m_mainWin
->GetItemCount();
5201 int wxGenericListCtrl::GetColumnCount() const
5203 return m_mainWin
->GetColumnCount();
5206 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5208 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5211 wxSize
wxGenericListCtrl::GetItemSpacing() const
5213 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5215 return wxSize(spacing
, spacing
);
5218 #if WXWIN_COMPATIBILITY_2_6
5219 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5221 return m_mainWin
->GetItemSpacing( isSmall
);
5223 #endif // WXWIN_COMPATIBILITY_2_6
5225 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5228 info
.m_itemId
= item
;
5229 info
.SetTextColour( col
);
5230 m_mainWin
->SetItem( info
);
5233 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5236 info
.m_itemId
= item
;
5237 m_mainWin
->GetItem( info
);
5238 return info
.GetTextColour();
5241 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5244 info
.m_itemId
= item
;
5245 info
.SetBackgroundColour( col
);
5246 m_mainWin
->SetItem( info
);
5249 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5252 info
.m_itemId
= item
;
5253 m_mainWin
->GetItem( info
);
5254 return info
.GetBackgroundColour();
5257 int wxGenericListCtrl::GetScrollPos( int orient
) const
5259 return m_mainWin
->GetScrollPos( orient
);
5262 void wxGenericListCtrl::SetScrollPos( int orient
, int pos
, bool refresh
)
5264 m_mainWin
->SetScrollPos( orient
, pos
, refresh
);
5267 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5270 info
.m_itemId
= item
;
5272 m_mainWin
->SetItem( info
);
5275 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5278 info
.m_itemId
= item
;
5279 m_mainWin
->GetItem( info
);
5280 return info
.GetFont();
5283 int wxGenericListCtrl::GetSelectedItemCount() const
5285 return m_mainWin
->GetSelectedItemCount();
5288 wxColour
wxGenericListCtrl::GetTextColour() const
5290 return GetForegroundColour();
5293 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5295 SetForegroundColour(col
);
5298 long wxGenericListCtrl::GetTopItem() const
5301 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5305 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5307 return m_mainWin
->GetNextItem( item
, geom
, state
);
5310 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5312 if (which
== wxIMAGE_LIST_NORMAL
)
5313 return m_imageListNormal
;
5314 else if (which
== wxIMAGE_LIST_SMALL
)
5315 return m_imageListSmall
;
5316 else if (which
== wxIMAGE_LIST_STATE
)
5317 return m_imageListState
;
5319 return (wxImageList
*) NULL
;
5322 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5324 if ( which
== wxIMAGE_LIST_NORMAL
)
5326 if (m_ownsImageListNormal
)
5327 delete m_imageListNormal
;
5328 m_imageListNormal
= imageList
;
5329 m_ownsImageListNormal
= false;
5331 else if ( which
== wxIMAGE_LIST_SMALL
)
5333 if (m_ownsImageListSmall
)
5334 delete m_imageListSmall
;
5335 m_imageListSmall
= imageList
;
5336 m_ownsImageListSmall
= false;
5338 else if ( which
== wxIMAGE_LIST_STATE
)
5340 if (m_ownsImageListState
)
5341 delete m_imageListState
;
5342 m_imageListState
= imageList
;
5343 m_ownsImageListState
= false;
5346 m_mainWin
->SetImageList( imageList
, which
);
5349 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5351 SetImageList(imageList
, which
);
5352 if ( which
== wxIMAGE_LIST_NORMAL
)
5353 m_ownsImageListNormal
= true;
5354 else if ( which
== wxIMAGE_LIST_SMALL
)
5355 m_ownsImageListSmall
= true;
5356 else if ( which
== wxIMAGE_LIST_STATE
)
5357 m_ownsImageListState
= true;
5360 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5365 bool wxGenericListCtrl::DeleteItem( long item
)
5367 m_mainWin
->DeleteItem( item
);
5371 bool wxGenericListCtrl::DeleteAllItems()
5373 m_mainWin
->DeleteAllItems();
5377 bool wxGenericListCtrl::DeleteAllColumns()
5379 size_t count
= m_mainWin
->m_columns
.GetCount();
5380 for ( size_t n
= 0; n
< count
; n
++ )
5385 void wxGenericListCtrl::ClearAll()
5387 m_mainWin
->DeleteEverything();
5390 bool wxGenericListCtrl::DeleteColumn( int col
)
5392 m_mainWin
->DeleteColumn( col
);
5394 // if we don't have the header any longer, we need to relayout the window
5395 if ( !GetColumnCount() )
5396 ResizeReportView(false /* no header */);
5400 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5401 wxClassInfo
* textControlClass
)
5403 return m_mainWin
->EditLabel( item
, textControlClass
);
5406 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5408 return m_mainWin
->GetEditControl();
5411 bool wxGenericListCtrl::EnsureVisible( long item
)
5413 m_mainWin
->EnsureVisible( item
);
5417 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5419 return m_mainWin
->FindItem( start
, str
, partial
);
5422 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5424 return m_mainWin
->FindItem( start
, data
);
5427 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5428 int WXUNUSED(direction
))
5430 return m_mainWin
->FindItem( pt
);
5433 // TODO: sub item hit testing
5434 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5436 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5439 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5441 m_mainWin
->InsertItem( info
);
5442 return info
.m_itemId
;
5445 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5448 info
.m_text
= label
;
5449 info
.m_mask
= wxLIST_MASK_TEXT
;
5450 info
.m_itemId
= index
;
5451 return InsertItem( info
);
5454 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5457 info
.m_mask
= wxLIST_MASK_IMAGE
;
5458 info
.m_image
= imageIndex
;
5459 info
.m_itemId
= index
;
5460 return InsertItem( info
);
5463 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5466 info
.m_text
= label
;
5467 info
.m_image
= imageIndex
;
5468 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5469 info
.m_itemId
= index
;
5470 return InsertItem( info
);
5473 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5475 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5477 m_mainWin
->InsertColumn( col
, item
);
5479 // if we hadn't had a header before but have one now
5480 // then we need to relayout the window
5481 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5482 ResizeReportView(true /* have header */);
5484 m_headerWin
->Refresh();
5489 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5490 int format
, int width
)
5493 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5494 item
.m_text
= heading
;
5497 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5498 item
.m_width
= width
;
5501 item
.m_format
= format
;
5503 return InsertColumn( col
, item
);
5506 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5512 // fn is a function which takes 3 long arguments: item1, item2, data.
5513 // item1 is the long data associated with a first item (NOT the index).
5514 // item2 is the long data associated with a second item (NOT the index).
5515 // data is the same value as passed to SortItems.
5516 // The return value is a negative number if the first item should precede the second
5517 // item, a positive number of the second item should precede the first,
5518 // or zero if the two items are equivalent.
5519 // data is arbitrary data to be passed to the sort function.
5521 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5523 m_mainWin
->SortItems( fn
, data
);
5527 // ----------------------------------------------------------------------------
5529 // ----------------------------------------------------------------------------
5531 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5536 ResizeReportView(m_mainWin
->HasHeader());
5537 m_mainWin
->RecalculatePositions();
5540 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5543 GetClientSize( &cw
, &ch
);
5547 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5548 if(ch
> m_headerHeight
)
5549 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5550 cw
, ch
- m_headerHeight
- 1 );
5552 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5555 else // no header window
5557 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5561 void wxGenericListCtrl::OnInternalIdle()
5563 wxWindow::OnInternalIdle();
5565 // do it only if needed
5566 if ( !m_mainWin
->m_dirty
)
5569 m_mainWin
->RecalculatePositions();
5572 // ----------------------------------------------------------------------------
5574 // ----------------------------------------------------------------------------
5576 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5580 m_mainWin
->SetBackgroundColour( colour
);
5581 m_mainWin
->m_dirty
= true;
5587 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5589 if ( !wxWindow::SetForegroundColour( colour
) )
5594 m_mainWin
->SetForegroundColour( colour
);
5595 m_mainWin
->m_dirty
= true;
5599 m_headerWin
->SetForegroundColour( colour
);
5604 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5606 if ( !wxWindow::SetFont( font
) )
5611 m_mainWin
->SetFont( font
);
5612 m_mainWin
->m_dirty
= true;
5617 m_headerWin
->SetFont( font
);
5618 CalculateAndSetHeaderHeight();
5628 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5631 // Use the same color scheme as wxListBox
5632 return wxListBox::GetClassDefaultAttributes(variant
);
5634 wxUnusedVar(variant
);
5635 wxVisualAttributes attr
;
5636 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5637 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5638 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5643 // ----------------------------------------------------------------------------
5644 // methods forwarded to m_mainWin
5645 // ----------------------------------------------------------------------------
5647 #if wxUSE_DRAG_AND_DROP
5649 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5651 m_mainWin
->SetDropTarget( dropTarget
);
5654 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5656 return m_mainWin
->GetDropTarget();
5661 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5663 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5666 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5668 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5671 wxColour
wxGenericListCtrl::GetForegroundColour() const
5673 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5676 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5679 return m_mainWin
->PopupMenu( menu
, x
, y
);
5685 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5687 m_mainWin
->DoClientToScreen(x
, y
);
5690 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5692 m_mainWin
->DoScreenToClient(x
, y
);
5695 void wxGenericListCtrl::SetFocus()
5697 // The test in window.cpp fails as we are a composite
5698 // window, so it checks against "this", but not m_mainWin.
5699 if ( DoFindFocus() != this )
5700 m_mainWin
->SetFocus();
5703 wxSize
wxGenericListCtrl::DoGetBestSize() const
5705 // Something is better than nothing...
5706 // 100x80 is what the MSW version will get from the default
5707 // wxControl::DoGetBestSize
5708 return wxSize(100, 80);
5711 // ----------------------------------------------------------------------------
5712 // virtual list control support
5713 // ----------------------------------------------------------------------------
5715 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5717 // this is a pure virtual function, in fact - which is not really pure
5718 // because the controls which are not virtual don't need to implement it
5719 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5721 return wxEmptyString
;
5724 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5726 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5728 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5732 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5735 return OnGetItemImage(item
);
5741 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5743 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5744 _T("invalid item index in OnGetItemAttr()") );
5746 // no attributes by default
5750 void wxGenericListCtrl::SetItemCount(long count
)
5752 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5754 m_mainWin
->SetItemCount(count
);
5757 void wxGenericListCtrl::RefreshItem(long item
)
5759 m_mainWin
->RefreshLine(item
);
5762 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5764 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5767 // Generic wxListCtrl is more or less a container for two other
5768 // windows which drawings are done upon. These are namely
5769 // 'm_headerWin' and 'm_mainWin'.
5770 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5771 // behaviour wxListCtrl has under wxMSW.
5773 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5777 // The easy case, no rectangle specified.
5779 m_headerWin
->Refresh(eraseBackground
);
5782 m_mainWin
->Refresh(eraseBackground
);
5786 // Refresh the header window
5789 wxRect rectHeader
= m_headerWin
->GetRect();
5790 rectHeader
.Intersect(*rect
);
5791 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5794 m_headerWin
->GetPosition(&x
, &y
);
5795 rectHeader
.Offset(-x
, -y
);
5796 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5800 // Refresh the main window
5803 wxRect rectMain
= m_mainWin
->GetRect();
5804 rectMain
.Intersect(*rect
);
5805 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5808 m_mainWin
->GetPosition(&x
, &y
);
5809 rectMain
.Offset(-x
, -y
);
5810 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5816 void wxGenericListCtrl::Freeze()
5818 m_mainWin
->Freeze();
5821 void wxGenericListCtrl::Thaw()
5826 #endif // wxUSE_LISTCTRL