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"
45 #include "wx/settings.h"
48 #include "wx/imaglist.h"
49 #include "wx/selstore.h"
50 #include "wx/renderer.h"
53 #include "wx/mac/private.h"
57 // NOTE: If using the wxListBox visual attributes works everywhere then this can
58 // be removed, as well as the #else case below.
59 #define _USE_VISATTR 0
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // // the height of the header window (FIXME: should depend on its font!)
67 // static const int HEADER_HEIGHT = 23;
69 static const int SCROLL_UNIT_X
= 15;
71 // the spacing between the lines (in report mode)
72 static const int LINE_SPACING
= 0;
74 // extra margins around the text label
76 static const int EXTRA_WIDTH
= 6;
78 static const int EXTRA_WIDTH
= 4;
80 static const int EXTRA_HEIGHT
= 4;
82 // margin between the window and the items
83 static const int EXTRA_BORDER_X
= 2;
84 static const int EXTRA_BORDER_Y
= 2;
86 // offset for the header window
87 static const int HEADER_OFFSET_X
= 0;
88 static const int HEADER_OFFSET_Y
= 0;
90 // margin between rows of icons in [small] icon view
91 static const int MARGIN_BETWEEN_ROWS
= 6;
93 // when autosizing the columns, add some slack
94 static const int AUTOSIZE_COL_MARGIN
= 10;
96 // default width for the header columns
97 static const int WIDTH_COL_DEFAULT
= 80;
99 // the space between the image and the text in the report mode
100 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
102 // the space between the image and the text in the report mode in header
103 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
105 // ============================================================================
107 // ============================================================================
109 //-----------------------------------------------------------------------------
110 // wxColWidthInfo (internal)
111 //-----------------------------------------------------------------------------
113 struct wxColWidthInfo
116 bool bNeedsUpdate
; // only set to true when an item whose
117 // width == nMaxWidth is removed
119 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
122 bNeedsUpdate
= needsUpdate
;
126 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
128 //-----------------------------------------------------------------------------
129 // wxListItemData (internal)
130 //-----------------------------------------------------------------------------
135 wxListItemData(wxListMainWindow
*owner
);
138 void SetItem( const wxListItem
&info
);
139 void SetImage( int image
) { m_image
= image
; }
140 void SetData( wxUIntPtr data
) { m_data
= data
; }
141 void SetPosition( int x
, int y
);
142 void SetSize( int width
, int height
);
144 bool HasText() const { return !m_text
.empty(); }
145 const wxString
& GetText() const { return m_text
; }
146 void SetText(const wxString
& text
) { m_text
= text
; }
148 // we can't use empty string for measuring the string width/height, so
149 // always return something
150 wxString
GetTextForMeasuring() const
152 wxString s
= GetText();
159 bool IsHit( int x
, int y
) const;
163 int GetWidth() const;
164 int GetHeight() const;
166 int GetImage() const { return m_image
; }
167 bool HasImage() const { return GetImage() != -1; }
169 void GetItem( wxListItem
&info
) const;
171 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
172 wxListItemAttr
*GetAttr() const { return m_attr
; }
175 // the item image or -1
178 // user data associated with the item
181 // the item coordinates are not used in report mode; instead this pointer is
182 // NULL and the owner window is used to retrieve the item position and size
185 // the list ctrl we are in
186 wxListMainWindow
*m_owner
;
188 // custom attributes or NULL
189 wxListItemAttr
*m_attr
;
192 // common part of all ctors
198 //-----------------------------------------------------------------------------
199 // wxListHeaderData (internal)
200 //-----------------------------------------------------------------------------
202 class wxListHeaderData
: public wxObject
206 wxListHeaderData( const wxListItem
&info
);
207 void SetItem( const wxListItem
&item
);
208 void SetPosition( int x
, int y
);
209 void SetWidth( int w
);
210 void SetState( int state
);
211 void SetFormat( int format
);
212 void SetHeight( int h
);
213 bool HasImage() const;
215 bool HasText() const { return !m_text
.empty(); }
216 const wxString
& GetText() const { return m_text
; }
217 void SetText(const wxString
& text
) { m_text
= text
; }
219 void GetItem( wxListItem
&item
);
221 bool IsHit( int x
, int y
) const;
222 int GetImage() const;
223 int GetWidth() const;
224 int GetFormat() const;
225 int GetState() const;
242 //-----------------------------------------------------------------------------
243 // wxListLineData (internal)
244 //-----------------------------------------------------------------------------
246 WX_DECLARE_EXPORTED_LIST(wxListItemData
, wxListItemDataList
);
247 #include "wx/listimpl.cpp"
248 WX_DEFINE_LIST(wxListItemDataList
)
253 // the list of subitems: only may have more than one item in report mode
254 wxListItemDataList m_items
;
256 // this is not used in report view
268 // the part to be highlighted
269 wxRect m_rectHighlight
;
271 // extend all our rects to be centered inside the one of given width
272 void ExtendWidth(wxCoord w
)
274 wxASSERT_MSG( m_rectAll
.width
<= w
,
275 _T("width can only be increased") );
278 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
279 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
280 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
285 // is this item selected? [NB: not used in virtual mode]
288 // back pointer to the list ctrl
289 wxListMainWindow
*m_owner
;
292 wxListLineData(wxListMainWindow
*owner
);
296 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
300 // are we in report mode?
301 inline bool InReportView() const;
303 // are we in virtual report mode?
304 inline bool IsVirtual() const;
306 // these 2 methods shouldn't be called for report view controls, in that
307 // case we determine our position/size ourselves
309 // calculate the size of the line
310 void CalculateSize( wxDC
*dc
, int spacing
);
312 // remember the position this line appears at
313 void SetPosition( int x
, int y
, int spacing
);
317 void SetImage( int image
) { SetImage(0, image
); }
318 int GetImage() const { return GetImage(0); }
319 void SetImage( int index
, int image
);
320 int GetImage( int index
) const;
322 bool HasImage() const { return GetImage() != -1; }
323 bool HasText() const { return !GetText(0).empty(); }
325 void SetItem( int index
, const wxListItem
&info
);
326 void GetItem( int index
, wxListItem
&info
);
328 wxString
GetText(int index
) const;
329 void SetText( int index
, const wxString
& s
);
331 wxListItemAttr
*GetAttr() const;
332 void SetAttr(wxListItemAttr
*attr
);
334 // return true if the highlighting really changed
335 bool Highlight( bool on
);
337 void ReverseHighlight();
339 bool IsHighlighted() const
341 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
343 return m_highlighted
;
346 // draw the line on the given DC in icon/list mode
347 void Draw( wxDC
*dc
);
349 // the same in report mode
350 void DrawInReportMode( wxDC
*dc
,
352 const wxRect
& rectHL
,
356 // set the line to contain num items (only can be > 1 in report mode)
357 void InitItems( int num
);
359 // get the mode (i.e. style) of the list control
360 inline int GetMode() const;
362 // prepare the DC for drawing with these item's attributes, return true if
363 // we need to draw the items background to highlight it, false otherwise
364 bool SetAttributes(wxDC
*dc
,
365 const wxListItemAttr
*attr
,
368 // draw the text on the DC with the correct justification; also add an
369 // ellipsis if the text is too large to fit in the current width
370 void DrawTextFormatted(wxDC
*dc
,
371 const wxString
&text
,
374 int yMid
, // this is middle, not top, of the text
378 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
379 #include "wx/arrimpl.cpp"
380 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
382 //-----------------------------------------------------------------------------
383 // wxListHeaderWindow (internal)
384 //-----------------------------------------------------------------------------
386 class wxListHeaderWindow
: public wxWindow
389 wxListMainWindow
*m_owner
;
390 const wxCursor
*m_currentCursor
;
391 wxCursor
*m_resizeCursor
;
394 // column being resized or -1
397 // divider line position in logical (unscrolled) coords
400 // minimal position beyond which the divider line
401 // can't be dragged in logical coords
405 wxListHeaderWindow();
407 wxListHeaderWindow( wxWindow
*win
,
409 wxListMainWindow
*owner
,
410 const wxPoint
&pos
= wxDefaultPosition
,
411 const wxSize
&size
= wxDefaultSize
,
413 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
415 virtual ~wxListHeaderWindow();
418 void AdjustDC( wxDC
& dc
);
420 void OnPaint( wxPaintEvent
&event
);
421 void OnMouse( wxMouseEvent
&event
);
422 void OnSetFocus( wxFocusEvent
&event
);
428 // common part of all ctors
431 // generate and process the list event of the given type, return true if
432 // it wasn't vetoed, i.e. if we should proceed
433 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
435 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
436 DECLARE_EVENT_TABLE()
439 //-----------------------------------------------------------------------------
440 // wxListRenameTimer (internal)
441 //-----------------------------------------------------------------------------
443 class wxListRenameTimer
: public wxTimer
446 wxListMainWindow
*m_owner
;
449 wxListRenameTimer( wxListMainWindow
*owner
);
453 //-----------------------------------------------------------------------------
454 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
455 //-----------------------------------------------------------------------------
457 class wxListTextCtrlWrapper
: public wxEvtHandler
460 // NB: text must be a valid object but not Create()d yet
461 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
465 wxTextCtrl
*GetText() const { return m_text
; }
467 void AcceptChangesAndFinish();
470 void OnChar( wxKeyEvent
&event
);
471 void OnKeyUp( wxKeyEvent
&event
);
472 void OnKillFocus( wxFocusEvent
&event
);
474 bool AcceptChanges();
478 wxListMainWindow
*m_owner
;
480 wxString m_startValue
;
483 bool m_aboutToFinish
;
485 DECLARE_EVENT_TABLE()
488 //-----------------------------------------------------------------------------
489 // wxListMainWindow (internal)
490 //-----------------------------------------------------------------------------
492 WX_DECLARE_EXPORTED_LIST(wxListHeaderData
, wxListHeaderDataList
);
493 #include "wx/listimpl.cpp"
494 WX_DEFINE_LIST(wxListHeaderDataList
)
496 class wxListMainWindow
: public wxScrolledWindow
500 wxListMainWindow( wxWindow
*parent
,
502 const wxPoint
& pos
= wxDefaultPosition
,
503 const wxSize
& size
= wxDefaultSize
,
505 const wxString
&name
= _T("listctrlmainwindow") );
507 virtual ~wxListMainWindow();
509 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
511 // return true if this is a virtual list control
512 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
514 // return true if the control is in report mode
515 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
517 // return true if we are in single selection mode, false if multi sel
518 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
520 // do we have a header window?
521 bool HasHeader() const
522 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
524 void HighlightAll( bool on
);
526 // all these functions only do something if the line is currently visible
528 // change the line "selected" state, return true if it really changed
529 bool HighlightLine( size_t line
, bool highlight
= true);
531 // as HighlightLine() but do it for the range of lines: this is incredibly
532 // more efficient for virtual list controls!
534 // NB: unlike HighlightLine() this one does refresh the lines on screen
535 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
537 // toggle the line state and refresh it
538 void ReverseHighlight( size_t line
)
539 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
541 // return true if the line is highlighted
542 bool IsHighlighted(size_t line
) const;
544 // refresh one or several lines at once
545 void RefreshLine( size_t line
);
546 void RefreshLines( size_t lineFrom
, size_t lineTo
);
548 // refresh all selected items
549 void RefreshSelected();
551 // refresh all lines below the given one: the difference with
552 // RefreshLines() is that the index here might not be a valid one (happens
553 // when the last line is deleted)
554 void RefreshAfter( size_t lineFrom
);
556 // the methods which are forwarded to wxListLineData itself in list/icon
557 // modes but are here because the lines don't store their positions in the
560 // get the bound rect for the entire line
561 wxRect
GetLineRect(size_t line
) const;
563 // get the bound rect of the label
564 wxRect
GetLineLabelRect(size_t line
) const;
566 // get the bound rect of the items icon (only may be called if we do have
568 wxRect
GetLineIconRect(size_t line
) const;
570 // get the rect to be highlighted when the item has focus
571 wxRect
GetLineHighlightRect(size_t line
) const;
573 // get the size of the total line rect
574 wxSize
GetLineSize(size_t line
) const
575 { return GetLineRect(line
).GetSize(); }
577 // return the hit code for the corresponding position (in this line)
578 long HitTestLine(size_t line
, int x
, int y
) const;
580 // bring the selected item into view, scrolling to it if necessary
581 void MoveToItem(size_t item
);
583 bool ScrollList( int WXUNUSED(dx
), int dy
);
585 // bring the current item into view
586 void MoveToFocus() { MoveToItem(m_current
); }
588 // start editing the label of the given item
589 wxTextCtrl
*EditLabel(long item
,
590 wxClassInfo
* textControlClass
= CLASSINFO(wxTextCtrl
));
591 wxTextCtrl
*GetEditControl() const
593 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
596 void FinishEditing(wxTextCtrl
*text
)
599 m_textctrlWrapper
= NULL
;
600 SetFocusIgnoringChildren();
603 // suspend/resume redrawing the control
607 void OnRenameTimer();
608 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
609 void OnRenameCancelled(size_t itemEdit
);
611 void OnMouse( wxMouseEvent
&event
);
613 // called to switch the selection from the current item to newCurrent,
614 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
616 void OnChar( wxKeyEvent
&event
);
617 void OnKeyDown( wxKeyEvent
&event
);
618 void OnKeyUp( wxKeyEvent
&event
);
619 void OnSetFocus( wxFocusEvent
&event
);
620 void OnKillFocus( wxFocusEvent
&event
);
621 void OnScroll( wxScrollWinEvent
& event
);
623 void OnPaint( wxPaintEvent
&event
);
625 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
626 void GetImageSize( int index
, int &width
, int &height
) const;
627 int GetTextLength( const wxString
&s
) const;
629 void SetImageList( wxImageList
*imageList
, int which
);
630 void SetItemSpacing( int spacing
, bool isSmall
= false );
631 int GetItemSpacing( bool isSmall
= false );
633 void SetColumn( int col
, wxListItem
&item
);
634 void SetColumnWidth( int col
, int width
);
635 void GetColumn( int col
, wxListItem
&item
) const;
636 int GetColumnWidth( int col
) const;
637 int GetColumnCount() const { return m_columns
.GetCount(); }
639 // returns the sum of the heights of all columns
640 int GetHeaderWidth() const;
642 int GetCountPerPage() const;
644 void SetItem( wxListItem
&item
);
645 void GetItem( wxListItem
&item
) const;
646 void SetItemState( long item
, long state
, long stateMask
);
647 void SetItemStateAll( long state
, long stateMask
);
648 int GetItemState( long item
, long stateMask
) const;
649 void GetItemRect( long index
, wxRect
&rect
) const;
650 wxRect
GetViewRect() const;
651 bool GetItemPosition( long item
, wxPoint
& pos
) const;
652 int GetSelectedItemCount() const;
654 wxString
GetItemText(long item
) const
657 info
.m_mask
= wxLIST_MASK_TEXT
;
658 info
.m_itemId
= item
;
663 void SetItemText(long item
, const wxString
& value
)
666 info
.m_mask
= wxLIST_MASK_TEXT
;
667 info
.m_itemId
= item
;
672 // set the scrollbars and update the positions of the items
673 void RecalculatePositions(bool noRefresh
= false);
675 // refresh the window and the header
678 long GetNextItem( long item
, int geometry
, int state
) const;
679 void DeleteItem( long index
);
680 void DeleteAllItems();
681 void DeleteColumn( int col
);
682 void DeleteEverything();
683 void EnsureVisible( long index
);
684 long FindItem( long start
, const wxString
& str
, bool partial
= false );
685 long FindItem( long start
, wxUIntPtr data
);
686 long FindItem( const wxPoint
& pt
);
687 long HitTest( int x
, int y
, int &flags
) const;
688 void InsertItem( wxListItem
&item
);
689 void InsertColumn( long col
, wxListItem
&item
);
690 int GetItemWidthWithImage(wxListItem
* item
);
691 void SortItems( wxListCtrlCompare fn
, long data
);
693 size_t GetItemCount() const;
694 bool IsEmpty() const { return GetItemCount() == 0; }
695 void SetItemCount(long count
);
697 // change the current (== focused) item, send a notification event
698 void ChangeCurrent(size_t current
);
699 void ResetCurrent() { ChangeCurrent((size_t)-1); }
700 bool HasCurrent() const { return m_current
!= (size_t)-1; }
702 // send out a wxListEvent
703 void SendNotify( size_t line
,
705 const wxPoint
& point
= wxDefaultPosition
);
707 // override base class virtual to reset m_lineHeight when the font changes
708 virtual bool SetFont(const wxFont
& font
)
710 if ( !wxScrolledWindow::SetFont(font
) )
718 // these are for wxListLineData usage only
720 // get the backpointer to the list ctrl
721 wxGenericListCtrl
*GetListCtrl() const
723 return wxStaticCast(GetParent(), wxGenericListCtrl
);
726 // get the height of all lines (assuming they all do have the same height)
727 wxCoord
GetLineHeight() const;
729 // get the y position of the given line (only for report view)
730 wxCoord
GetLineY(size_t line
) const;
732 // get the brush to use for the item highlighting
733 wxBrush
*GetHighlightBrush() const
735 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
738 bool HasFocus() const
744 // the array of all line objects for a non virtual list control (for the
745 // virtual list control we only ever use m_lines[0])
746 wxListLineDataArray m_lines
;
748 // the list of column objects
749 wxListHeaderDataList m_columns
;
751 // currently focused item or -1
754 // the number of lines per page
757 // this flag is set when something which should result in the window
758 // redrawing happens (i.e. an item was added or deleted, or its appearance
759 // changed) and OnPaint() doesn't redraw the window while it is set which
760 // allows to minimize the number of repaintings when a lot of items are
761 // being added. The real repainting occurs only after the next OnIdle()
765 wxColour
*m_highlightColour
;
766 wxImageList
*m_small_image_list
;
767 wxImageList
*m_normal_image_list
;
769 int m_normal_spacing
;
773 wxTimer
*m_renameTimer
;
777 ColWidthArray m_aColWidths
;
779 // for double click logic
780 size_t m_lineLastClicked
,
781 m_lineBeforeLastClicked
,
782 m_lineSelectSingleOnUp
;
785 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
787 // the total count of items in a virtual list control
790 // the object maintaining the items selection state, only used in virtual
792 wxSelectionStore m_selStore
;
794 // common part of all ctors
797 // get the line data for the given index
798 wxListLineData
*GetLine(size_t n
) const
800 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
804 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
811 // get a dummy line which can be used for geometry calculations and such:
812 // you must use GetLine() if you want to really draw the line
813 wxListLineData
*GetDummyLine() const;
815 // cache the line data of the n-th line in m_lines[0]
816 void CacheLineData(size_t line
);
818 // get the range of visible lines
819 void GetVisibleLinesRange(size_t *from
, size_t *to
);
821 // force us to recalculate the range of visible lines
822 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
824 // get the colour to be used for drawing the rules
825 wxColour
GetRuleColour() const
827 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
831 // initialize the current item if needed
832 void UpdateCurrent();
834 // delete all items but don't refresh: called from dtor
835 void DoDeleteAllItems();
837 // the height of one line using the current font
838 wxCoord m_lineHeight
;
840 // the total header width or 0 if not calculated yet
841 wxCoord m_headerWidth
;
843 // the first and last lines being shown on screen right now (inclusive),
844 // both may be -1 if they must be calculated so never access them directly:
845 // use GetVisibleLinesRange() above instead
849 // the brushes to use for item highlighting when we do/don't have focus
850 wxBrush
*m_highlightBrush
,
851 *m_highlightUnfocusedBrush
;
853 // if this is > 0, the control is frozen and doesn't redraw itself
854 size_t m_freezeCount
;
856 // wrapper around the text control currently used for in place editing or
857 // NULL if no item is being edited
858 wxListTextCtrlWrapper
*m_textctrlWrapper
;
861 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
862 DECLARE_EVENT_TABLE()
864 friend class wxGenericListCtrl
;
868 wxListItemData::~wxListItemData()
870 // in the virtual list control the attributes are managed by the main
871 // program, so don't delete them
872 if ( !m_owner
->IsVirtual() )
878 void wxListItemData::Init()
886 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
892 if ( owner
->InReportView() )
898 void wxListItemData::SetItem( const wxListItem
&info
)
900 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
901 SetText(info
.m_text
);
902 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
903 m_image
= info
.m_image
;
904 if ( info
.m_mask
& wxLIST_MASK_DATA
)
905 m_data
= info
.m_data
;
907 if ( info
.HasAttributes() )
910 m_attr
->AssignFrom(*info
.GetAttributes());
912 m_attr
= new wxListItemAttr(*info
.GetAttributes());
920 m_rect
->width
= info
.m_width
;
924 void wxListItemData::SetPosition( int x
, int y
)
926 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
932 void wxListItemData::SetSize( int width
, int height
)
934 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
937 m_rect
->width
= width
;
939 m_rect
->height
= height
;
942 bool wxListItemData::IsHit( int x
, int y
) const
944 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
946 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
949 int wxListItemData::GetX() const
951 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
956 int wxListItemData::GetY() const
958 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
963 int wxListItemData::GetWidth() const
965 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
967 return m_rect
->width
;
970 int wxListItemData::GetHeight() const
972 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
974 return m_rect
->height
;
977 void wxListItemData::GetItem( wxListItem
&info
) const
979 long mask
= info
.m_mask
;
981 // by default, get everything for backwards compatibility
984 if ( mask
& wxLIST_MASK_TEXT
)
985 info
.m_text
= m_text
;
986 if ( mask
& wxLIST_MASK_IMAGE
)
987 info
.m_image
= m_image
;
988 if ( mask
& wxLIST_MASK_DATA
)
989 info
.m_data
= m_data
;
993 if ( m_attr
->HasTextColour() )
994 info
.SetTextColour(m_attr
->GetTextColour());
995 if ( m_attr
->HasBackgroundColour() )
996 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
997 if ( m_attr
->HasFont() )
998 info
.SetFont(m_attr
->GetFont());
1002 //-----------------------------------------------------------------------------
1004 //-----------------------------------------------------------------------------
1006 void wxListHeaderData::Init()
1018 wxListHeaderData::wxListHeaderData()
1023 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1030 void wxListHeaderData::SetItem( const wxListItem
&item
)
1032 m_mask
= item
.m_mask
;
1034 if ( m_mask
& wxLIST_MASK_TEXT
)
1035 m_text
= item
.m_text
;
1037 if ( m_mask
& wxLIST_MASK_IMAGE
)
1038 m_image
= item
.m_image
;
1040 if ( m_mask
& wxLIST_MASK_FORMAT
)
1041 m_format
= item
.m_format
;
1043 if ( m_mask
& wxLIST_MASK_WIDTH
)
1044 SetWidth(item
.m_width
);
1046 if ( m_mask
& wxLIST_MASK_STATE
)
1047 SetState(item
.m_state
);
1050 void wxListHeaderData::SetPosition( int x
, int y
)
1056 void wxListHeaderData::SetHeight( int h
)
1061 void wxListHeaderData::SetWidth( int w
)
1063 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1066 void wxListHeaderData::SetState( int flag
)
1071 void wxListHeaderData::SetFormat( int format
)
1076 bool wxListHeaderData::HasImage() const
1078 return m_image
!= -1;
1081 bool wxListHeaderData::IsHit( int x
, int y
) const
1083 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1086 void wxListHeaderData::GetItem( wxListItem
& item
)
1088 item
.m_mask
= m_mask
;
1089 item
.m_text
= m_text
;
1090 item
.m_image
= m_image
;
1091 item
.m_format
= m_format
;
1092 item
.m_width
= m_width
;
1093 item
.m_state
= m_state
;
1096 int wxListHeaderData::GetImage() const
1101 int wxListHeaderData::GetWidth() const
1106 int wxListHeaderData::GetFormat() const
1111 int wxListHeaderData::GetState() const
1116 //-----------------------------------------------------------------------------
1118 //-----------------------------------------------------------------------------
1120 inline int wxListLineData::GetMode() const
1122 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1125 inline bool wxListLineData::InReportView() const
1127 return m_owner
->HasFlag(wxLC_REPORT
);
1130 inline bool wxListLineData::IsVirtual() const
1132 return m_owner
->IsVirtual();
1135 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1139 if ( InReportView() )
1142 m_gi
= new GeometryInfo
;
1144 m_highlighted
= false;
1146 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1149 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1151 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1152 wxCHECK_RET( node
, _T("no subitems at all??") );
1154 wxListItemData
*item
= node
->GetData();
1159 switch ( GetMode() )
1162 case wxLC_SMALL_ICON
:
1163 m_gi
->m_rectAll
.width
= spacing
;
1165 s
= item
->GetText();
1170 m_gi
->m_rectLabel
.width
=
1171 m_gi
->m_rectLabel
.height
= 0;
1175 dc
->GetTextExtent( s
, &lw
, &lh
);
1179 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1181 m_gi
->m_rectAll
.width
= lw
;
1183 m_gi
->m_rectLabel
.width
= lw
;
1184 m_gi
->m_rectLabel
.height
= lh
;
1187 if (item
->HasImage())
1190 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1191 m_gi
->m_rectIcon
.width
= w
+ 8;
1192 m_gi
->m_rectIcon
.height
= h
+ 8;
1194 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1195 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1196 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1197 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1200 if ( item
->HasText() )
1202 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1203 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1205 else // no text, highlight the icon
1207 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1208 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1213 s
= item
->GetTextForMeasuring();
1215 dc
->GetTextExtent( s
, &lw
, &lh
);
1219 m_gi
->m_rectLabel
.width
= lw
;
1220 m_gi
->m_rectLabel
.height
= lh
;
1222 m_gi
->m_rectAll
.width
= lw
;
1223 m_gi
->m_rectAll
.height
= lh
;
1225 if (item
->HasImage())
1228 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1229 m_gi
->m_rectIcon
.width
= w
;
1230 m_gi
->m_rectIcon
.height
= h
;
1232 m_gi
->m_rectAll
.width
+= 4 + w
;
1233 if (h
> m_gi
->m_rectAll
.height
)
1234 m_gi
->m_rectAll
.height
= h
;
1237 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1238 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1242 wxFAIL_MSG( _T("unexpected call to SetSize") );
1246 wxFAIL_MSG( _T("unknown mode") );
1251 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1253 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1254 wxCHECK_RET( node
, _T("no subitems at all??") );
1256 wxListItemData
*item
= node
->GetData();
1258 switch ( GetMode() )
1261 case wxLC_SMALL_ICON
:
1262 m_gi
->m_rectAll
.x
= x
;
1263 m_gi
->m_rectAll
.y
= y
;
1265 if ( item
->HasImage() )
1267 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1268 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1269 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1272 if ( item
->HasText() )
1274 if (m_gi
->m_rectAll
.width
> spacing
)
1275 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1277 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1278 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1279 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1280 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1282 else // no text, highlight the icon
1284 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1285 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1290 m_gi
->m_rectAll
.x
= x
;
1291 m_gi
->m_rectAll
.y
= y
;
1293 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1294 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1295 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1297 if (item
->HasImage())
1299 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1300 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1301 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1305 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1310 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1314 wxFAIL_MSG( _T("unknown mode") );
1319 void wxListLineData::InitItems( int num
)
1321 for (int i
= 0; i
< num
; i
++)
1322 m_items
.Append( new wxListItemData(m_owner
) );
1325 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1327 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1328 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1330 wxListItemData
*item
= node
->GetData();
1331 item
->SetItem( info
);
1334 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1336 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1339 wxListItemData
*item
= node
->GetData();
1340 item
->GetItem( info
);
1344 wxString
wxListLineData::GetText(int index
) const
1348 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1351 wxListItemData
*item
= node
->GetData();
1352 s
= item
->GetText();
1358 void wxListLineData::SetText( int index
, const wxString
& s
)
1360 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1363 wxListItemData
*item
= node
->GetData();
1368 void wxListLineData::SetImage( int index
, int image
)
1370 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1371 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1373 wxListItemData
*item
= node
->GetData();
1374 item
->SetImage(image
);
1377 int wxListLineData::GetImage( int index
) const
1379 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1380 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1382 wxListItemData
*item
= node
->GetData();
1383 return item
->GetImage();
1386 wxListItemAttr
*wxListLineData::GetAttr() const
1388 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1389 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1391 wxListItemData
*item
= node
->GetData();
1392 return item
->GetAttr();
1395 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1397 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1398 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1400 wxListItemData
*item
= node
->GetData();
1401 item
->SetAttr(attr
);
1404 bool wxListLineData::SetAttributes(wxDC
*dc
,
1405 const wxListItemAttr
*attr
,
1408 wxWindow
*listctrl
= m_owner
->GetParent();
1412 // don't use foreground colour for drawing highlighted items - this might
1413 // make them completely invisible (and there is no way to do bit
1414 // arithmetics on wxColour, unfortunately)
1419 if (m_owner
->HasFocus()
1420 #if !defined(__WXUNIVERSAL__)
1421 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1429 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1431 else if ( attr
&& attr
->HasTextColour() )
1432 colText
= attr
->GetTextColour();
1434 colText
= listctrl
->GetForegroundColour();
1436 dc
->SetTextForeground(colText
);
1440 if ( attr
&& attr
->HasFont() )
1441 font
= attr
->GetFont();
1443 font
= listctrl
->GetFont();
1448 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1449 if ( highlighted
|| hasBgCol
)
1452 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1454 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1456 dc
->SetPen( *wxTRANSPARENT_PEN
);
1464 void wxListLineData::Draw( wxDC
*dc
)
1466 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1467 wxCHECK_RET( node
, _T("no subitems at all??") );
1469 bool highlighted
= IsHighlighted();
1471 wxListItemAttr
*attr
= GetAttr();
1473 if ( SetAttributes(dc
, attr
, highlighted
) )
1474 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1476 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1482 int flags
= wxCONTROL_SELECTED
;
1483 if (m_owner
->HasFocus()
1484 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
1485 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1488 flags
|= wxCONTROL_FOCUSED
;
1489 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1494 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1499 // just for debugging to better see where the items are
1501 dc
->SetPen(*wxRED_PEN
);
1502 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1503 dc
->DrawRectangle( m_gi
->m_rectAll
);
1504 dc
->SetPen(*wxGREEN_PEN
);
1505 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1508 wxListItemData
*item
= node
->GetData();
1509 if (item
->HasImage())
1511 // centre the image inside our rectangle, this looks nicer when items
1512 // ae aligned in a row
1513 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1515 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1518 if (item
->HasText())
1520 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1522 wxDCClipper
clipper(*dc
, rectLabel
);
1523 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1527 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1529 const wxRect
& rectHL
,
1532 // TODO: later we should support setting different attributes for
1533 // different columns - to do it, just add "col" argument to
1534 // GetAttr() and move these lines into the loop below
1535 wxListItemAttr
*attr
= GetAttr();
1536 if ( SetAttributes(dc
, attr
, highlighted
) )
1537 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1539 dc
->DrawRectangle( rectHL
);
1545 int flags
= wxCONTROL_SELECTED
;
1546 if (m_owner
->HasFocus())
1547 flags
|= wxCONTROL_FOCUSED
;
1548 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1552 dc
->DrawRectangle( rectHL
);
1557 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1558 yMid
= rect
.y
+ rect
.height
/2;
1560 // This probably needs to be done
1561 // on all platforms as the icons
1562 // otherwise nearly touch the border
1567 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1569 node
= node
->GetNext(), col
++ )
1571 wxListItemData
*item
= node
->GetData();
1573 int width
= m_owner
->GetColumnWidth(col
);
1577 if ( item
->HasImage() )
1580 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1581 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1583 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1589 if ( item
->HasText() )
1590 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, width
- 8);
1594 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1595 const wxString
& textOrig
,
1601 // we don't support displaying multiple lines currently (and neither does
1602 // wxMSW FWIW) so just merge all the lines
1603 wxString
text(textOrig
);
1604 text
.Replace(_T("\n"), _T(" "));
1607 dc
->GetTextExtent(text
, &w
, &h
);
1609 const wxCoord y
= yMid
- (h
+ 1)/2;
1611 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1613 // determine if the string can fit inside the current width
1616 // it can, draw it using the items alignment
1618 m_owner
->GetColumn(col
, item
);
1619 switch ( item
.GetAlign() )
1621 case wxLIST_FORMAT_LEFT
:
1625 case wxLIST_FORMAT_RIGHT
:
1629 case wxLIST_FORMAT_CENTER
:
1630 x
+= (width
- w
) / 2;
1634 wxFAIL_MSG( _T("unknown list item format") );
1638 dc
->DrawText(text
, x
, y
);
1640 else // otherwise, truncate and add an ellipsis if possible
1642 // determine the base width
1643 wxString
ellipsis(wxT("..."));
1645 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1647 // continue until we have enough space or only one character left
1649 size_t len
= text
.length();
1650 wxString drawntext
= text
.Left(len
);
1653 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1654 drawntext
.RemoveLast();
1657 if (w
+ base_w
<= width
)
1661 // if still not enough space, remove ellipsis characters
1662 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1664 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1665 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1668 // now draw the text
1669 dc
->DrawText(drawntext
, x
, y
);
1670 dc
->DrawText(ellipsis
, x
+ w
, y
);
1674 bool wxListLineData::Highlight( bool on
)
1676 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1678 if ( on
== m_highlighted
)
1686 void wxListLineData::ReverseHighlight( void )
1688 Highlight(!IsHighlighted());
1691 //-----------------------------------------------------------------------------
1692 // wxListHeaderWindow
1693 //-----------------------------------------------------------------------------
1695 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1697 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1698 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1699 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1700 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1703 void wxListHeaderWindow::Init()
1705 m_currentCursor
= (wxCursor
*) NULL
;
1706 m_isDragging
= false;
1710 wxListHeaderWindow::wxListHeaderWindow()
1714 m_owner
= (wxListMainWindow
*) NULL
;
1715 m_resizeCursor
= (wxCursor
*) NULL
;
1718 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1720 wxListMainWindow
*owner
,
1724 const wxString
&name
)
1725 : wxWindow( win
, id
, pos
, size
, style
, name
)
1730 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1733 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1734 SetOwnForegroundColour( attr
.colFg
);
1735 SetOwnBackgroundColour( attr
.colBg
);
1737 SetOwnFont( attr
.font
);
1739 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1740 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1742 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1746 wxListHeaderWindow::~wxListHeaderWindow()
1748 delete m_resizeCursor
;
1751 #ifdef __WXUNIVERSAL__
1752 #include "wx/univ/renderer.h"
1753 #include "wx/univ/theme.h"
1756 // shift the DC origin to match the position of the main window horz
1757 // scrollbar: this allows us to always use logical coords
1758 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1761 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1764 m_owner
->GetViewStart( &view_start
, NULL
);
1769 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1771 // account for the horz scrollbar offset
1773 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1775 // Maybe we just have to check for m_signX
1776 // in the DC, but I leave the #ifdef __WXGTK__
1778 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1782 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1785 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1787 wxPaintDC
dc( this );
1792 dc
.SetFont( GetFont() );
1794 // width and height of the entire header window
1796 GetClientSize( &w
, &h
);
1797 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1799 dc
.SetBackgroundMode(wxTRANSPARENT
);
1800 dc
.SetTextForeground(GetForegroundColour());
1802 int x
= HEADER_OFFSET_X
;
1803 int numColumns
= m_owner
->GetColumnCount();
1805 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1807 m_owner
->GetColumn( i
, item
);
1808 int wCol
= item
.m_width
;
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 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1845 const int image
= item
.m_image
;
1846 wxImageList
*imageList
;
1849 imageList
= m_owner
->m_small_image_list
;
1852 imageList
->GetSize(image
, ix
, iy
);
1853 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1861 // ignore alignment if there is not enough space anyhow
1863 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1866 wxFAIL_MSG( _T("unknown list item format") );
1869 case wxLIST_FORMAT_LEFT
:
1873 case wxLIST_FORMAT_RIGHT
:
1874 xAligned
= x
+ cw
- wLabel
;
1877 case wxLIST_FORMAT_CENTER
:
1878 xAligned
= x
+ (cw
- wLabel
) / 2;
1882 // draw the text and image clipping them so that they
1883 // don't overwrite the column boundary
1884 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1886 // if we have an image, draw it on the right of the label
1893 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1894 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1895 wxIMAGELIST_DRAW_TRANSPARENT
1899 dc
.DrawText( item
.GetText(),
1900 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1906 void wxListHeaderWindow::DrawCurrent()
1909 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
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
);
1936 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1938 // we want to work with logical coords
1940 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1941 int y
= event
.GetY();
1945 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1947 // we don't draw the line beyond our window, but we allow dragging it
1950 GetClientSize( &w
, NULL
);
1951 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1954 // erase the line if it was drawn
1955 if ( m_currentX
< w
)
1958 if (event
.ButtonUp())
1961 m_isDragging
= false;
1963 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1964 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1971 m_currentX
= m_minX
+ 7;
1973 // draw in the new location
1974 if ( m_currentX
< w
)
1978 else // not dragging
1981 bool hit_border
= false;
1983 // end of the current column
1986 // find the column where this event occurred
1988 countCol
= m_owner
->GetColumnCount();
1989 for (col
= 0; col
< countCol
; col
++)
1991 xpos
+= m_owner
->GetColumnWidth( col
);
1994 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1996 // near the column border
2003 // inside the column
2010 if ( col
== countCol
)
2013 if (event
.LeftDown() || event
.RightUp())
2015 if (hit_border
&& event
.LeftDown())
2017 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2018 event
.GetPosition()) )
2020 m_isDragging
= true;
2025 //else: column resizing was vetoed by the user code
2027 else // click on a column
2029 // record the selected state of the columns
2030 if (event
.LeftDown())
2032 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2035 m_owner
->GetColumn(i
, colItem
);
2036 long state
= colItem
.GetState();
2038 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2040 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2041 m_owner
->SetColumn(i
, colItem
);
2045 SendListEvent( event
.LeftDown()
2046 ? wxEVT_COMMAND_LIST_COL_CLICK
2047 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2048 event
.GetPosition());
2051 else if (event
.Moving())
2056 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2057 m_currentCursor
= m_resizeCursor
;
2061 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2062 m_currentCursor
= wxSTANDARD_CURSOR
;
2066 SetCursor(*m_currentCursor
);
2071 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2073 m_owner
->SetFocus();
2077 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2079 wxWindow
*parent
= GetParent();
2080 wxListEvent
le( type
, parent
->GetId() );
2081 le
.SetEventObject( parent
);
2082 le
.m_pointDrag
= pos
;
2084 // the position should be relative to the parent window, not
2085 // this one for compatibility with MSW and common sense: the
2086 // user code doesn't know anything at all about this header
2087 // window, so why should it get positions relative to it?
2088 le
.m_pointDrag
.y
-= GetSize().y
;
2090 le
.m_col
= m_column
;
2091 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2094 //-----------------------------------------------------------------------------
2095 // wxListRenameTimer (internal)
2096 //-----------------------------------------------------------------------------
2098 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2103 void wxListRenameTimer::Notify()
2105 m_owner
->OnRenameTimer();
2108 //-----------------------------------------------------------------------------
2109 // wxListTextCtrlWrapper (internal)
2110 //-----------------------------------------------------------------------------
2112 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2113 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2114 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2115 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2118 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2121 : m_startValue(owner
->GetItemText(itemEdit
)),
2122 m_itemEdited(itemEdit
)
2127 m_aboutToFinish
= false;
2129 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2131 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2132 &rectLabel
.x
, &rectLabel
.y
);
2134 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2135 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2136 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2139 m_text
->PushEventHandler(this);
2142 void wxListTextCtrlWrapper::Finish()
2148 m_text
->RemoveEventHandler(this);
2149 m_owner
->FinishEditing(m_text
);
2151 wxPendingDelete
.Append( this );
2155 bool wxListTextCtrlWrapper::AcceptChanges()
2157 const wxString value
= m_text
->GetValue();
2159 // notice that we should always call OnRenameAccept() to generate the "end
2160 // label editing" event, even if the user hasn't really changed anything
2161 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2163 // vetoed by the user
2167 // accepted, do rename the item (unless nothing changed)
2168 if ( value
!= m_startValue
)
2169 m_owner
->SetItemText(m_itemEdited
, value
);
2174 void wxListTextCtrlWrapper::AcceptChangesAndFinish()
2176 m_aboutToFinish
= true;
2178 // Notify the owner about the changes
2181 // Even if vetoed, close the control (consistent with MSW)
2185 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2187 switch ( event
.m_keyCode
)
2190 AcceptChangesAndFinish();
2194 m_owner
->OnRenameCancelled( m_itemEdited
);
2203 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2211 // auto-grow the textctrl:
2212 wxSize parentSize
= m_owner
->GetSize();
2213 wxPoint myPos
= m_text
->GetPosition();
2214 wxSize mySize
= m_text
->GetSize();
2216 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2217 if (myPos
.x
+ sx
> parentSize
.x
)
2218 sx
= parentSize
.x
- myPos
.x
;
2221 m_text
->SetSize(sx
, wxDefaultCoord
);
2226 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2228 if ( !m_finished
&& !m_aboutToFinish
)
2230 if ( !AcceptChanges() )
2231 m_owner
->OnRenameCancelled( m_itemEdited
);
2236 // We must let the native text control handle focus
2240 //-----------------------------------------------------------------------------
2242 //-----------------------------------------------------------------------------
2244 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2246 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2247 EVT_PAINT (wxListMainWindow::OnPaint
)
2248 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2249 EVT_CHAR (wxListMainWindow::OnChar
)
2250 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2251 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
2252 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2253 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2254 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2257 void wxListMainWindow::Init()
2262 m_lineTo
= (size_t)-1;
2268 m_small_image_list
= (wxImageList
*) NULL
;
2269 m_normal_image_list
= (wxImageList
*) NULL
;
2271 m_small_spacing
= 30;
2272 m_normal_spacing
= 40;
2276 m_isCreated
= false;
2278 m_lastOnSame
= false;
2279 m_renameTimer
= new wxListRenameTimer( this );
2280 m_textctrlWrapper
= NULL
;
2284 m_lineSelectSingleOnUp
=
2285 m_lineBeforeLastClicked
= (size_t)-1;
2290 wxListMainWindow::wxListMainWindow()
2295 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2298 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2303 const wxString
&name
)
2304 : wxScrolledWindow( parent
, id
, pos
, size
,
2305 style
| wxHSCROLL
| wxVSCROLL
, name
)
2309 m_highlightBrush
= new wxBrush
2311 wxSystemSettings::GetColour
2313 wxSYS_COLOUR_HIGHLIGHT
2318 m_highlightUnfocusedBrush
= new wxBrush
2320 wxSystemSettings::GetColour
2322 wxSYS_COLOUR_BTNSHADOW
2327 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2329 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2330 SetOwnForegroundColour( attr
.colFg
);
2331 SetOwnBackgroundColour( attr
.colBg
);
2333 SetOwnFont( attr
.font
);
2336 wxListMainWindow::~wxListMainWindow()
2339 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2340 WX_CLEAR_ARRAY(m_aColWidths
);
2342 delete m_highlightBrush
;
2343 delete m_highlightUnfocusedBrush
;
2344 delete m_renameTimer
;
2347 void wxListMainWindow::CacheLineData(size_t line
)
2349 wxGenericListCtrl
*listctrl
= GetListCtrl();
2351 wxListLineData
*ld
= GetDummyLine();
2353 size_t countCol
= GetColumnCount();
2354 for ( size_t col
= 0; col
< countCol
; col
++ )
2356 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2357 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2360 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2363 wxListLineData
*wxListMainWindow::GetDummyLine() const
2365 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2366 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2368 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2370 // we need to recreate the dummy line if the number of columns in the
2371 // control changed as it would have the incorrect number of fields
2373 if ( !m_lines
.IsEmpty() &&
2374 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2376 self
->m_lines
.Clear();
2379 if ( m_lines
.IsEmpty() )
2381 wxListLineData
*line
= new wxListLineData(self
);
2382 self
->m_lines
.Add(line
);
2384 // don't waste extra memory -- there never going to be anything
2385 // else/more in this array
2386 self
->m_lines
.Shrink();
2392 // ----------------------------------------------------------------------------
2393 // line geometry (report mode only)
2394 // ----------------------------------------------------------------------------
2396 wxCoord
wxListMainWindow::GetLineHeight() const
2398 // we cache the line height as calling GetTextExtent() is slow
2399 if ( !m_lineHeight
)
2401 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2403 wxClientDC
dc( self
);
2404 dc
.SetFont( GetFont() );
2407 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2409 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2412 m_small_image_list
->GetSize(0, iw
, ih
);
2417 self
->m_lineHeight
= y
+ LINE_SPACING
;
2420 return m_lineHeight
;
2423 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2425 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2427 return LINE_SPACING
+ line
* GetLineHeight();
2430 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2432 if ( !InReportView() )
2433 return GetLine(line
)->m_gi
->m_rectAll
;
2436 rect
.x
= HEADER_OFFSET_X
;
2437 rect
.y
= GetLineY(line
);
2438 rect
.width
= GetHeaderWidth();
2439 rect
.height
= GetLineHeight();
2444 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2446 if ( !InReportView() )
2447 return GetLine(line
)->m_gi
->m_rectLabel
;
2450 wxListLineData
*data
= GetLine(line
);
2451 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
2454 wxListItemData
*item
= node
->GetData();
2455 if ( item
->HasImage() )
2458 GetImageSize( item
->GetImage(), ix
, iy
);
2459 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
2464 rect
.x
= image_x
+ HEADER_OFFSET_X
;
2465 rect
.y
= GetLineY(line
);
2466 rect
.width
= GetColumnWidth(0) - image_x
;
2467 rect
.height
= GetLineHeight();
2472 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2474 if ( !InReportView() )
2475 return GetLine(line
)->m_gi
->m_rectIcon
;
2477 wxListLineData
*ld
= GetLine(line
);
2478 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2481 rect
.x
= HEADER_OFFSET_X
;
2482 rect
.y
= GetLineY(line
);
2483 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2488 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2490 return InReportView() ? GetLineRect(line
)
2491 : GetLine(line
)->m_gi
->m_rectHighlight
;
2494 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2496 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2498 wxListLineData
*ld
= GetLine(line
);
2500 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2501 return wxLIST_HITTEST_ONITEMICON
;
2503 // VS: Testing for "ld->HasText() || InReportView()" instead of
2504 // "ld->HasText()" is needed to make empty lines in report view
2506 if ( ld
->HasText() || InReportView() )
2508 wxRect rect
= InReportView() ? GetLineRect(line
)
2509 : GetLineLabelRect(line
);
2511 if ( rect
.Contains(x
, y
) )
2512 return wxLIST_HITTEST_ONITEMLABEL
;
2518 // ----------------------------------------------------------------------------
2519 // highlight (selection) handling
2520 // ----------------------------------------------------------------------------
2522 bool wxListMainWindow::IsHighlighted(size_t line
) const
2526 return m_selStore
.IsSelected(line
);
2530 wxListLineData
*ld
= GetLine(line
);
2531 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2533 return ld
->IsHighlighted();
2537 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2543 wxArrayInt linesChanged
;
2544 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2547 // meny items changed state, refresh everything
2548 RefreshLines(lineFrom
, lineTo
);
2550 else // only a few items changed state, refresh only them
2552 size_t count
= linesChanged
.GetCount();
2553 for ( size_t n
= 0; n
< count
; n
++ )
2555 RefreshLine(linesChanged
[n
]);
2559 else // iterate over all items in non report view
2561 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2563 if ( HighlightLine(line
, highlight
) )
2569 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2575 changed
= m_selStore
.SelectItem(line
, highlight
);
2579 wxListLineData
*ld
= GetLine(line
);
2580 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2582 changed
= ld
->Highlight(highlight
);
2587 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2588 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2594 void wxListMainWindow::RefreshLine( size_t line
)
2596 if ( InReportView() )
2598 size_t visibleFrom
, visibleTo
;
2599 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2601 if ( line
< visibleFrom
|| line
> visibleTo
)
2605 wxRect rect
= GetLineRect(line
);
2607 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2608 RefreshRect( rect
);
2611 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2613 // we suppose that they are ordered by caller
2614 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2616 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2618 if ( InReportView() )
2620 size_t visibleFrom
, visibleTo
;
2621 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2623 if ( lineFrom
< visibleFrom
)
2624 lineFrom
= visibleFrom
;
2625 if ( lineTo
> visibleTo
)
2630 rect
.y
= GetLineY(lineFrom
);
2631 rect
.width
= GetClientSize().x
;
2632 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2634 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2635 RefreshRect( rect
);
2639 // TODO: this should be optimized...
2640 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2647 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2649 if ( InReportView() )
2651 size_t visibleFrom
, visibleTo
;
2652 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2654 if ( lineFrom
< visibleFrom
)
2655 lineFrom
= visibleFrom
;
2656 else if ( lineFrom
> visibleTo
)
2661 rect
.y
= GetLineY(lineFrom
);
2662 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2664 wxSize size
= GetClientSize();
2665 rect
.width
= size
.x
;
2667 // refresh till the bottom of the window
2668 rect
.height
= size
.y
- rect
.y
;
2670 RefreshRect( rect
);
2674 // TODO: how to do it more efficiently?
2679 void wxListMainWindow::RefreshSelected()
2685 if ( InReportView() )
2687 GetVisibleLinesRange(&from
, &to
);
2692 to
= GetItemCount() - 1;
2695 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2696 RefreshLine(m_current
);
2698 for ( size_t line
= from
; line
<= to
; line
++ )
2700 // NB: the test works as expected even if m_current == -1
2701 if ( line
!= m_current
&& IsHighlighted(line
) )
2706 void wxListMainWindow::Freeze()
2711 void wxListMainWindow::Thaw()
2713 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2715 if ( --m_freezeCount
== 0 )
2719 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2721 // Note: a wxPaintDC must be constructed even if no drawing is
2722 // done (a Windows requirement).
2723 wxPaintDC
dc( this );
2725 if ( IsEmpty() || m_freezeCount
)
2726 // nothing to draw or not the moment to draw it
2730 // delay the repainting until we calculate all the items positions
2736 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2738 dc
.SetFont( GetFont() );
2740 if ( InReportView() )
2742 int lineHeight
= GetLineHeight();
2744 size_t visibleFrom
, visibleTo
;
2745 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2748 int xOrig
= dc
.LogicalToDeviceX( 0 );
2749 int yOrig
= dc
.LogicalToDeviceY( 0 );
2751 // tell the caller cache to cache the data
2754 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2755 GetParent()->GetId());
2756 evCache
.SetEventObject( GetParent() );
2757 evCache
.m_oldItemIndex
= visibleFrom
;
2758 evCache
.m_itemIndex
= visibleTo
;
2759 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2762 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2764 rectLine
= GetLineRect(line
);
2767 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2768 rectLine
.width
, rectLine
.height
) )
2770 // don't redraw unaffected lines to avoid flicker
2774 GetLine(line
)->DrawInReportMode( &dc
,
2776 GetLineHighlightRect(line
),
2777 IsHighlighted(line
) );
2780 if ( HasFlag(wxLC_HRULES
) )
2782 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2783 wxSize clientSize
= GetClientSize();
2785 size_t i
= visibleFrom
;
2786 if (i
== 0) i
= 1; // Don't draw the first one
2787 for ( ; i
<= visibleTo
; i
++ )
2790 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2791 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2792 clientSize
.x
- dev_x
, i
* lineHeight
);
2795 // Draw last horizontal rule
2796 if ( visibleTo
== GetItemCount() - 1 )
2799 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2800 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2801 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2805 // Draw vertical rules if required
2806 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2808 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2809 wxRect firstItemRect
, lastItemRect
;
2811 GetItemRect(visibleFrom
, firstItemRect
);
2812 GetItemRect(visibleTo
, lastItemRect
);
2813 int x
= firstItemRect
.GetX();
2815 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2817 for (int col
= 0; col
< GetColumnCount(); col
++)
2819 int colWidth
= GetColumnWidth(col
);
2821 int x_pos
= x
- dev_x
;
2822 if (col
< GetColumnCount()-1) x_pos
-= 2;
2823 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2824 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2830 size_t count
= GetItemCount();
2831 for ( size_t i
= 0; i
< count
; i
++ )
2833 GetLine(i
)->Draw( &dc
);
2838 // Don't draw rect outline under Mac at all.
2843 wxRect
rect( GetLineHighlightRect( m_current
) );
2845 dc
.SetPen( *wxBLACK_PEN
);
2846 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2847 dc
.DrawRectangle( rect
);
2849 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2857 void wxListMainWindow::HighlightAll( bool on
)
2859 if ( IsSingleSel() )
2861 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2863 // we just have one item to turn off
2864 if ( HasCurrent() && IsHighlighted(m_current
) )
2866 HighlightLine(m_current
, false);
2867 RefreshLine(m_current
);
2870 else // multi selection
2873 HighlightLines(0, GetItemCount() - 1, on
);
2877 void wxListMainWindow::SendNotify( size_t line
,
2878 wxEventType command
,
2879 const wxPoint
& point
)
2881 wxListEvent
le( command
, GetParent()->GetId() );
2882 le
.SetEventObject( GetParent() );
2884 le
.m_itemIndex
= line
;
2886 // set only for events which have position
2887 if ( point
!= wxDefaultPosition
)
2888 le
.m_pointDrag
= point
;
2890 // don't try to get the line info for virtual list controls: the main
2891 // program has it anyhow and if we did it would result in accessing all
2892 // the lines, even those which are not visible now and this is precisely
2893 // what we're trying to avoid
2896 if ( line
!= (size_t)-1 )
2898 GetLine(line
)->GetItem( 0, le
.m_item
);
2900 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2902 //else: there may be no more such item
2904 GetParent()->GetEventHandler()->ProcessEvent( le
);
2907 void wxListMainWindow::ChangeCurrent(size_t current
)
2909 m_current
= current
;
2911 // as the current item changed, we shouldn't start editing it when the
2912 // "slow click" timer expires as the click happened on another item
2913 if ( m_renameTimer
->IsRunning() )
2914 m_renameTimer
->Stop();
2916 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2919 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2921 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2922 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2924 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2925 wxT("EditLabel() needs a text control") );
2927 size_t itemEdit
= (size_t)item
;
2929 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2930 le
.SetEventObject( GetParent() );
2931 le
.m_itemIndex
= item
;
2932 wxListLineData
*data
= GetLine(itemEdit
);
2933 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2934 data
->GetItem( 0, le
.m_item
);
2936 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2938 // vetoed by user code
2942 // We have to call this here because the label in question might just have
2943 // been added and no screen update taken place.
2948 // Pending events dispatched by wxSafeYield might have changed the item
2950 if ( (size_t)item
>= GetItemCount() )
2954 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2955 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2956 return m_textctrlWrapper
->GetText();
2959 void wxListMainWindow::OnRenameTimer()
2961 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2963 EditLabel( m_current
);
2966 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2968 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2969 le
.SetEventObject( GetParent() );
2970 le
.m_itemIndex
= itemEdit
;
2972 wxListLineData
*data
= GetLine(itemEdit
);
2974 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2976 data
->GetItem( 0, le
.m_item
);
2977 le
.m_item
.m_text
= value
;
2978 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2982 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2984 // let owner know that the edit was cancelled
2985 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2987 le
.SetEditCanceled(true);
2989 le
.SetEventObject( GetParent() );
2990 le
.m_itemIndex
= itemEdit
;
2992 wxListLineData
*data
= GetLine(itemEdit
);
2993 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2995 data
->GetItem( 0, le
.m_item
);
2996 GetEventHandler()->ProcessEvent( le
);
2999 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
3003 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
3004 // shutdown the edit control when the mouse is clicked elsewhere on the
3005 // listctrl because the order of events is different (or something like
3006 // that), so explicitly end the edit if it is active.
3007 if ( event
.LeftDown() && m_textctrlWrapper
)
3008 m_textctrlWrapper
->AcceptChangesAndFinish();
3011 if ( event
.LeftDown() )
3012 SetFocusIgnoringChildren();
3014 event
.SetEventObject( GetParent() );
3015 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3018 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3020 // let the base handle mouse wheel events.
3025 if ( !HasCurrent() || IsEmpty() )
3027 if (event
.RightDown())
3029 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3031 wxContextMenuEvent
evtCtx(
3033 GetParent()->GetId(),
3034 ClientToScreen(event
.GetPosition()));
3035 evtCtx
.SetEventObject(GetParent());
3036 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3044 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3045 event
.ButtonDClick()) )
3048 int x
= event
.GetX();
3049 int y
= event
.GetY();
3050 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3052 // where did we hit it (if we did)?
3055 size_t count
= GetItemCount(),
3058 if ( InReportView() )
3060 current
= y
/ GetLineHeight();
3061 if ( current
< count
)
3062 hitResult
= HitTestLine(current
, x
, y
);
3066 // TODO: optimize it too! this is less simple than for report view but
3067 // enumerating all items is still not a way to do it!!
3068 for ( current
= 0; current
< count
; current
++ )
3070 hitResult
= HitTestLine(current
, x
, y
);
3076 if (event
.Dragging())
3078 if (m_dragCount
== 0)
3080 // we have to report the raw, physical coords as we want to be
3081 // able to call HitTest(event.m_pointDrag) from the user code to
3082 // get the item being dragged
3083 m_dragStart
= event
.GetPosition();
3088 if (m_dragCount
!= 3)
3091 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3092 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3094 wxListEvent
le( command
, GetParent()->GetId() );
3095 le
.SetEventObject( GetParent() );
3096 le
.m_itemIndex
= m_lineLastClicked
;
3097 le
.m_pointDrag
= m_dragStart
;
3098 GetParent()->GetEventHandler()->ProcessEvent( le
);
3109 // outside of any item
3110 if (event
.RightDown())
3112 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3114 wxContextMenuEvent
evtCtx(
3116 GetParent()->GetId(),
3117 ClientToScreen(event
.GetPosition()));
3118 evtCtx
.SetEventObject(GetParent());
3119 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3123 // reset the selection and bail out
3124 HighlightAll(false);
3130 bool forceClick
= false;
3131 if (event
.ButtonDClick())
3133 if ( m_renameTimer
->IsRunning() )
3134 m_renameTimer
->Stop();
3136 m_lastOnSame
= false;
3138 if ( current
== m_lineLastClicked
)
3140 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3146 // The first click was on another item, so don't interpret this as
3147 // a double click, but as a simple click instead
3154 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3156 // select single line
3157 HighlightAll( false );
3158 ReverseHighlight(m_lineSelectSingleOnUp
);
3163 if ((current
== m_current
) &&
3164 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3165 HasFlag(wxLC_EDIT_LABELS
) )
3167 if ( !InReportView() ||
3168 GetLineLabelRect(current
).Contains(x
, y
) )
3170 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
3171 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
3176 m_lastOnSame
= false;
3177 m_lineSelectSingleOnUp
= (size_t)-1;
3181 // This is necessary, because after a DnD operation in
3182 // from and to ourself, the up event is swallowed by the
3183 // DnD code. So on next non-up event (which means here and
3184 // now) m_lineSelectSingleOnUp should be reset.
3185 m_lineSelectSingleOnUp
= (size_t)-1;
3187 if (event
.RightDown())
3189 m_lineBeforeLastClicked
= m_lineLastClicked
;
3190 m_lineLastClicked
= current
;
3192 // If the item is already selected, do not update the selection.
3193 // Multi-selections should not be cleared if a selected item is clicked.
3194 if (!IsHighlighted(current
))
3196 HighlightAll(false);
3197 ChangeCurrent(current
);
3198 ReverseHighlight(m_current
);
3201 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3203 // Allow generation of context menu event
3206 else if (event
.MiddleDown())
3208 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3210 else if ( event
.LeftDown() || forceClick
)
3212 m_lineBeforeLastClicked
= m_lineLastClicked
;
3213 m_lineLastClicked
= current
;
3215 size_t oldCurrent
= m_current
;
3216 bool oldWasSelected
= IsHighlighted(m_current
);
3218 bool cmdModifierDown
= event
.CmdDown();
3219 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3221 if ( IsSingleSel() || !IsHighlighted(current
) )
3223 HighlightAll( false );
3225 ChangeCurrent(current
);
3227 ReverseHighlight(m_current
);
3229 else // multi sel & current is highlighted & no mod keys
3231 m_lineSelectSingleOnUp
= current
;
3232 ChangeCurrent(current
); // change focus
3235 else // multi sel & either ctrl or shift is down
3237 if (cmdModifierDown
)
3239 ChangeCurrent(current
);
3241 ReverseHighlight(m_current
);
3243 else if (event
.ShiftDown())
3245 ChangeCurrent(current
);
3247 size_t lineFrom
= oldCurrent
,
3250 if ( lineTo
< lineFrom
)
3253 lineFrom
= m_current
;
3256 HighlightLines(lineFrom
, lineTo
);
3258 else // !ctrl, !shift
3260 // test in the enclosing if should make it impossible
3261 wxFAIL_MSG( _T("how did we get here?") );
3265 if (m_current
!= oldCurrent
)
3266 RefreshLine( oldCurrent
);
3268 // forceClick is only set if the previous click was on another item
3269 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3273 void wxListMainWindow::MoveToItem(size_t item
)
3275 if ( item
== (size_t)-1 )
3278 wxRect rect
= GetLineRect(item
);
3280 int client_w
, client_h
;
3281 GetClientSize( &client_w
, &client_h
);
3283 const int hLine
= GetLineHeight();
3285 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3286 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3288 if ( InReportView() )
3290 // the next we need the range of lines shown it might be different,
3291 // so recalculate it
3292 ResetVisibleLinesRange();
3294 if (rect
.y
< view_y
)
3295 Scroll( -1, rect
.y
/ hLine
);
3296 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3297 Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3300 // At least on Mac the visible lines value will get reset inside of
3301 // Scroll *before* it actually scrolls the window because of the
3302 // Update() that happens there, so it will still have the wrong value.
3303 // So let's reset it again and wait for it to be recalculated in the
3304 // next paint event. I would expect this problem to show up in wxGTK
3305 // too but couldn't duplicate it there. Perhaps the order of events
3306 // is different... --Robin
3307 ResetVisibleLinesRange();
3315 if (rect
.x
-view_x
< 5)
3316 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
3317 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3318 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
3320 if (rect
.y
-view_y
< 5)
3321 sy
= (rect
.y
- 5) / hLine
;
3322 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
3323 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
3329 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
3331 if ( !InReportView() )
3333 // TODO: this should work in all views but is not implemented now
3338 GetVisibleLinesRange(&top
, &bottom
);
3340 if ( bottom
== (size_t)-1 )
3343 ResetVisibleLinesRange();
3345 int hLine
= GetLineHeight();
3347 Scroll(-1, top
+ dy
/ hLine
);
3350 // see comment in MoveToItem() for why we do this
3351 ResetVisibleLinesRange();
3357 // ----------------------------------------------------------------------------
3358 // keyboard handling
3359 // ----------------------------------------------------------------------------
3361 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3363 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3364 _T("invalid item index in OnArrowChar()") );
3366 size_t oldCurrent
= m_current
;
3368 // in single selection we just ignore Shift as we can't select several
3370 if ( event
.ShiftDown() && !IsSingleSel() )
3372 ChangeCurrent(newCurrent
);
3374 // refresh the old focus to remove it
3375 RefreshLine( oldCurrent
);
3377 // select all the items between the old and the new one
3378 if ( oldCurrent
> newCurrent
)
3380 newCurrent
= oldCurrent
;
3381 oldCurrent
= m_current
;
3384 HighlightLines(oldCurrent
, newCurrent
);
3388 // all previously selected items are unselected unless ctrl is held
3389 // in a multiselection control
3390 if ( !event
.ControlDown() || IsSingleSel() )
3391 HighlightAll(false);
3393 ChangeCurrent(newCurrent
);
3395 // refresh the old focus to remove it
3396 RefreshLine( oldCurrent
);
3398 // in single selection mode we must always have a selected item
3399 if ( !event
.ControlDown() || IsSingleSel() )
3400 HighlightLine( m_current
, true );
3403 RefreshLine( m_current
);
3408 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3410 wxWindow
*parent
= GetParent();
3412 // propagate the key event upwards
3413 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3414 ke
.m_shiftDown
= event
.m_shiftDown
;
3415 ke
.m_controlDown
= event
.m_controlDown
;
3416 ke
.m_altDown
= event
.m_altDown
;
3417 ke
.m_metaDown
= event
.m_metaDown
;
3418 ke
.m_keyCode
= event
.m_keyCode
;
3421 ke
.SetEventObject( parent
);
3422 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3427 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3429 wxWindow
*parent
= GetParent();
3431 // propagate the key event upwards
3432 wxKeyEvent
ke( wxEVT_KEY_UP
);
3433 ke
.m_shiftDown
= event
.m_shiftDown
;
3434 ke
.m_controlDown
= event
.m_controlDown
;
3435 ke
.m_altDown
= event
.m_altDown
;
3436 ke
.m_metaDown
= event
.m_metaDown
;
3437 ke
.m_keyCode
= event
.m_keyCode
;
3440 ke
.SetEventObject( parent
);
3441 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3446 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3448 wxWindow
*parent
= GetParent();
3450 // send a list_key event up
3453 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3454 le
.m_itemIndex
= m_current
;
3455 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3456 le
.m_code
= event
.GetKeyCode();
3457 le
.SetEventObject( parent
);
3458 parent
->GetEventHandler()->ProcessEvent( le
);
3461 // propagate the char event upwards
3462 wxKeyEvent
ke( wxEVT_CHAR
);
3463 ke
.m_shiftDown
= event
.m_shiftDown
;
3464 ke
.m_controlDown
= event
.m_controlDown
;
3465 ke
.m_altDown
= event
.m_altDown
;
3466 ke
.m_metaDown
= event
.m_metaDown
;
3467 ke
.m_keyCode
= event
.m_keyCode
;
3470 ke
.SetEventObject( parent
);
3471 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3473 if (event
.GetKeyCode() == WXK_TAB
)
3475 wxNavigationKeyEvent nevent
;
3476 nevent
.SetWindowChange( event
.ControlDown() );
3477 nevent
.SetDirection( !event
.ShiftDown() );
3478 nevent
.SetEventObject( GetParent()->GetParent() );
3479 nevent
.SetCurrentFocus( m_parent
);
3480 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3484 // no item -> nothing to do
3491 // don't use m_linesPerPage directly as it might not be computed yet
3492 const int pageSize
= GetCountPerPage();
3493 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3495 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3497 if (event
.GetKeyCode() == WXK_RIGHT
)
3498 event
.m_keyCode
= WXK_LEFT
;
3499 else if (event
.GetKeyCode() == WXK_LEFT
)
3500 event
.m_keyCode
= WXK_RIGHT
;
3503 switch ( event
.GetKeyCode() )
3506 if ( m_current
> 0 )
3507 OnArrowChar( m_current
- 1, event
);
3511 if ( m_current
< (size_t)GetItemCount() - 1 )
3512 OnArrowChar( m_current
+ 1, event
);
3517 OnArrowChar( GetItemCount() - 1, event
);
3522 OnArrowChar( 0, event
);
3527 int steps
= InReportView() ? pageSize
- 1
3528 : m_current
% pageSize
;
3530 int index
= m_current
- steps
;
3534 OnArrowChar( index
, event
);
3540 int steps
= InReportView()
3542 : pageSize
- (m_current
% pageSize
) - 1;
3544 size_t index
= m_current
+ steps
;
3545 size_t count
= GetItemCount();
3546 if ( index
>= count
)
3549 OnArrowChar( index
, event
);
3554 if ( !InReportView() )
3556 int index
= m_current
- pageSize
;
3560 OnArrowChar( index
, event
);
3565 if ( !InReportView() )
3567 size_t index
= m_current
+ pageSize
;
3569 size_t count
= GetItemCount();
3570 if ( index
>= count
)
3573 OnArrowChar( index
, event
);
3578 if ( IsSingleSel() )
3580 if ( event
.ControlDown() )
3582 ReverseHighlight(m_current
);
3584 else // normal space press
3586 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3589 else // multiple selection
3591 ReverseHighlight(m_current
);
3597 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3605 // ----------------------------------------------------------------------------
3607 // ----------------------------------------------------------------------------
3609 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3613 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3614 event
.SetEventObject( GetParent() );
3615 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3619 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3620 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3621 // which are already drawn correctly resulting in horrible flicker - avoid
3631 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3635 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3636 event
.SetEventObject( GetParent() );
3637 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3645 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3647 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3649 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3651 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3653 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3655 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3657 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3659 else if ( InReportView() && (m_small_image_list
))
3661 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3665 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3667 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3669 m_normal_image_list
->GetSize( index
, width
, height
);
3671 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3673 m_small_image_list
->GetSize( index
, width
, height
);
3675 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3677 m_small_image_list
->GetSize( index
, width
, height
);
3679 else if ( InReportView() && m_small_image_list
)
3681 m_small_image_list
->GetSize( index
, width
, height
);
3690 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3692 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3693 dc
.SetFont( GetFont() );
3696 dc
.GetTextExtent( s
, &lw
, NULL
);
3698 return lw
+ AUTOSIZE_COL_MARGIN
;
3701 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3705 // calc the spacing from the icon size
3706 int width
= 0, height
= 0;
3708 if ((imageList
) && (imageList
->GetImageCount()) )
3709 imageList
->GetSize(0, width
, height
);
3711 if (which
== wxIMAGE_LIST_NORMAL
)
3713 m_normal_image_list
= imageList
;
3714 m_normal_spacing
= width
+ 8;
3717 if (which
== wxIMAGE_LIST_SMALL
)
3719 m_small_image_list
= imageList
;
3720 m_small_spacing
= width
+ 14;
3721 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3725 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3729 m_small_spacing
= spacing
;
3731 m_normal_spacing
= spacing
;
3734 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3736 return isSmall
? m_small_spacing
: m_normal_spacing
;
3739 // ----------------------------------------------------------------------------
3741 // ----------------------------------------------------------------------------
3743 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3745 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3747 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3749 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3750 item
.m_width
= GetTextLength( item
.m_text
);
3752 wxListHeaderData
*column
= node
->GetData();
3753 column
->SetItem( item
);
3755 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3757 headerWin
->m_dirty
= true;
3761 // invalidate it as it has to be recalculated
3765 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3767 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3768 _T("invalid column index") );
3770 wxCHECK_RET( InReportView(),
3771 _T("SetColumnWidth() can only be called in report mode.") );
3774 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3776 headerWin
->m_dirty
= true;
3778 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3779 wxCHECK_RET( node
, _T("no column?") );
3781 wxListHeaderData
*column
= node
->GetData();
3783 size_t count
= GetItemCount();
3785 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3787 width
= GetTextLength(column
->GetText());
3788 width
+= 2*EXTRA_WIDTH
;
3790 // check for column header's image availability
3791 const int image
= column
->GetImage();
3794 if ( m_small_image_list
)
3797 m_small_image_list
->GetSize(image
, ix
, iy
);
3798 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3802 else if ( width
== wxLIST_AUTOSIZE
)
3806 // TODO: determine the max width somehow...
3807 width
= WIDTH_COL_DEFAULT
;
3811 wxClientDC
dc(this);
3812 dc
.SetFont( GetFont() );
3814 int max
= AUTOSIZE_COL_MARGIN
;
3816 // if the cached column width isn't valid then recalculate it
3817 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3819 for (size_t i
= 0; i
< count
; i
++)
3821 wxListLineData
*line
= GetLine( i
);
3822 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3824 wxCHECK_RET( n
, _T("no subitem?") );
3826 wxListItemData
*itemData
= n
->GetData();
3829 itemData
->GetItem(item
);
3830 int itemWidth
= GetItemWidthWithImage(&item
);
3831 if (itemWidth
> max
)
3835 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3836 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3839 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3840 width
= max
+ AUTOSIZE_COL_MARGIN
;
3844 column
->SetWidth( width
);
3846 // invalidate it as it has to be recalculated
3850 int wxListMainWindow::GetHeaderWidth() const
3852 if ( !m_headerWidth
)
3854 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3856 size_t count
= GetColumnCount();
3857 for ( size_t col
= 0; col
< count
; col
++ )
3859 self
->m_headerWidth
+= GetColumnWidth(col
);
3863 return m_headerWidth
;
3866 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3868 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3869 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3871 wxListHeaderData
*column
= node
->GetData();
3872 column
->GetItem( item
);
3875 int wxListMainWindow::GetColumnWidth( int col
) const
3877 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3878 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3880 wxListHeaderData
*column
= node
->GetData();
3881 return column
->GetWidth();
3884 // ----------------------------------------------------------------------------
3886 // ----------------------------------------------------------------------------
3888 void wxListMainWindow::SetItem( wxListItem
&item
)
3890 long id
= item
.m_itemId
;
3891 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3892 _T("invalid item index in SetItem") );
3896 wxListLineData
*line
= GetLine((size_t)id
);
3897 line
->SetItem( item
.m_col
, item
);
3899 // Set item state if user wants
3900 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3901 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3905 // update the Max Width Cache if needed
3906 int width
= GetItemWidthWithImage(&item
);
3908 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3909 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3913 // update the item on screen
3915 GetItemRect(id
, rectItem
);
3916 RefreshRect(rectItem
);
3919 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3924 // first deal with selection
3925 if ( stateMask
& wxLIST_STATE_SELECTED
)
3927 // set/clear select state
3930 // optimized version for virtual listctrl.
3931 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3934 else if ( state
& wxLIST_STATE_SELECTED
)
3936 const long count
= GetItemCount();
3937 for( long i
= 0; i
< count
; i
++ )
3939 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3945 // clear for non virtual (somewhat optimized by using GetNextItem())
3947 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3949 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3954 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3956 // unfocus all: only one item can be focussed, so clearing focus for
3957 // all items is simply clearing focus of the focussed item.
3958 SetItemState(m_current
, state
, stateMask
);
3960 //(setting focus to all items makes no sense, so it is not handled here.)
3963 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3967 SetItemStateAll(state
, stateMask
);
3971 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3972 _T("invalid list ctrl item index in SetItem") );
3974 size_t oldCurrent
= m_current
;
3975 size_t item
= (size_t)litem
; // safe because of the check above
3977 // do we need to change the focus?
3978 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3980 if ( state
& wxLIST_STATE_FOCUSED
)
3982 // don't do anything if this item is already focused
3983 if ( item
!= m_current
)
3985 ChangeCurrent(item
);
3987 if ( oldCurrent
!= (size_t)-1 )
3989 if ( IsSingleSel() )
3991 HighlightLine(oldCurrent
, false);
3994 RefreshLine(oldCurrent
);
3997 RefreshLine( m_current
);
4002 // don't do anything if this item is not focused
4003 if ( item
== m_current
)
4007 if ( IsSingleSel() )
4009 // we must unselect the old current item as well or we
4010 // might end up with more than one selected item in a
4011 // single selection control
4012 HighlightLine(oldCurrent
, false);
4015 RefreshLine( oldCurrent
);
4020 // do we need to change the selection state?
4021 if ( stateMask
& wxLIST_STATE_SELECTED
)
4023 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
4025 if ( IsSingleSel() )
4029 // selecting the item also makes it the focused one in the
4031 if ( m_current
!= item
)
4033 ChangeCurrent(item
);
4035 if ( oldCurrent
!= (size_t)-1 )
4037 HighlightLine( oldCurrent
, false );
4038 RefreshLine( oldCurrent
);
4044 // only the current item may be selected anyhow
4045 if ( item
!= m_current
)
4050 if ( HighlightLine(item
, on
) )
4057 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
4059 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
4060 _T("invalid list ctrl item index in GetItemState()") );
4062 int ret
= wxLIST_STATE_DONTCARE
;
4064 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4066 if ( (size_t)item
== m_current
)
4067 ret
|= wxLIST_STATE_FOCUSED
;
4070 if ( stateMask
& wxLIST_STATE_SELECTED
)
4072 if ( IsHighlighted(item
) )
4073 ret
|= wxLIST_STATE_SELECTED
;
4079 void wxListMainWindow::GetItem( wxListItem
&item
) const
4081 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
4082 _T("invalid item index in GetItem") );
4084 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4085 line
->GetItem( item
.m_col
, item
);
4087 // Get item state if user wants it
4088 if ( item
.m_mask
& wxLIST_MASK_STATE
)
4089 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
4090 wxLIST_STATE_FOCUSED
);
4093 // ----------------------------------------------------------------------------
4095 // ----------------------------------------------------------------------------
4097 size_t wxListMainWindow::GetItemCount() const
4099 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4102 void wxListMainWindow::SetItemCount(long count
)
4104 m_selStore
.SetItemCount(count
);
4105 m_countVirt
= count
;
4107 ResetVisibleLinesRange();
4109 // scrollbars must be reset
4113 int wxListMainWindow::GetSelectedItemCount() const
4115 // deal with the quick case first
4116 if ( IsSingleSel() )
4117 return HasCurrent() ? IsHighlighted(m_current
) : false;
4119 // virtual controls remmebers all its selections itself
4121 return m_selStore
.GetSelectedCount();
4123 // TODO: we probably should maintain the number of items selected even for
4124 // non virtual controls as enumerating all lines is really slow...
4125 size_t countSel
= 0;
4126 size_t count
= GetItemCount();
4127 for ( size_t line
= 0; line
< count
; line
++ )
4129 if ( GetLine(line
)->IsHighlighted() )
4136 // ----------------------------------------------------------------------------
4137 // item position/size
4138 // ----------------------------------------------------------------------------
4140 wxRect
wxListMainWindow::GetViewRect() const
4142 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
4143 _T("wxListCtrl::GetViewRect() only works in icon mode") );
4145 // we need to find the longest/tallest label
4146 wxCoord xMax
= 0, yMax
= 0;
4147 const int count
= GetItemCount();
4150 for ( int i
= 0; i
< count
; i
++ )
4155 wxCoord x
= r
.GetRight(),
4165 // some fudge needed to make it look prettier
4166 xMax
+= 2 * EXTRA_BORDER_X
;
4167 yMax
+= 2 * EXTRA_BORDER_Y
;
4169 // account for the scrollbars if necessary
4170 const wxSize sizeAll
= GetClientSize();
4171 if ( xMax
> sizeAll
.x
)
4172 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4173 if ( yMax
> sizeAll
.y
)
4174 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4176 return wxRect(0, 0, xMax
, yMax
);
4179 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
4181 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4182 _T("invalid index in GetItemRect") );
4184 // ensure that we're laid out, otherwise we could return nonsense
4187 wxConstCast(this, wxListMainWindow
)->
4188 RecalculatePositions(true /* no refresh */);
4191 rect
= GetLineRect((size_t)index
);
4193 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4196 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4199 GetItemRect(item
, rect
);
4207 // ----------------------------------------------------------------------------
4208 // geometry calculation
4209 // ----------------------------------------------------------------------------
4211 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4213 const int lineHeight
= GetLineHeight();
4215 wxClientDC
dc( this );
4216 dc
.SetFont( GetFont() );
4218 const size_t count
= GetItemCount();
4221 if ( HasFlag(wxLC_ICON
) )
4222 iconSpacing
= m_normal_spacing
;
4223 else if ( HasFlag(wxLC_SMALL_ICON
) )
4224 iconSpacing
= m_small_spacing
;
4228 // Note that we do not call GetClientSize() here but
4229 // GetSize() and subtract the border size for sunken
4230 // borders manually. This is technically incorrect,
4231 // but we need to know the client area's size WITHOUT
4232 // scrollbars here. Since we don't know if there are
4233 // any scrollbars, we use GetSize() instead. Another
4234 // solution would be to call SetScrollbars() here to
4235 // remove the scrollbars and call GetClientSize() then,
4236 // but this might result in flicker and - worse - will
4237 // reset the scrollbars to 0 which is not good at all
4238 // if you resize a dialog/window, but don't want to
4239 // reset the window scrolling. RR.
4240 // Furthermore, we actually do NOT subtract the border
4241 // width as 2 pixels is just the extra space which we
4242 // need around the actual content in the window. Other-
4243 // wise the text would e.g. touch the upper border. RR.
4246 GetSize( &clientWidth
, &clientHeight
);
4248 if ( InReportView() )
4250 // all lines have the same height and we scroll one line per step
4251 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4253 m_linesPerPage
= clientHeight
/ lineHeight
;
4255 ResetVisibleLinesRange();
4257 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4258 GetHeaderWidth() / SCROLL_UNIT_X
,
4259 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4260 GetScrollPos(wxHORIZONTAL
),
4261 GetScrollPos(wxVERTICAL
),
4266 // we have 3 different layout strategies: either layout all items
4267 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4268 // to arrange them in top to bottom, left to right (don't ask me why
4269 // not the other way round...) order
4270 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4272 int x
= EXTRA_BORDER_X
;
4273 int y
= EXTRA_BORDER_Y
;
4275 wxCoord widthMax
= 0;
4278 for ( i
= 0; i
< count
; i
++ )
4280 wxListLineData
*line
= GetLine(i
);
4281 line
->CalculateSize( &dc
, iconSpacing
);
4282 line
->SetPosition( x
, y
, iconSpacing
);
4284 wxSize sizeLine
= GetLineSize(i
);
4286 if ( HasFlag(wxLC_ALIGN_TOP
) )
4288 if ( sizeLine
.x
> widthMax
)
4289 widthMax
= sizeLine
.x
;
4293 else // wxLC_ALIGN_LEFT
4295 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4299 if ( HasFlag(wxLC_ALIGN_TOP
) )
4301 // traverse the items again and tweak their sizes so that they are
4302 // all the same in a row
4303 for ( i
= 0; i
< count
; i
++ )
4305 wxListLineData
*line
= GetLine(i
);
4306 line
->m_gi
->ExtendWidth(widthMax
);
4314 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4315 (y
+ lineHeight
) / lineHeight
,
4316 GetScrollPos( wxHORIZONTAL
),
4317 GetScrollPos( wxVERTICAL
),
4321 else // "flowed" arrangement, the most complicated case
4323 // at first we try without any scrollbars, if the items don't fit into
4324 // the window, we recalculate after subtracting the space taken by the
4327 int entireWidth
= 0;
4329 for (int tries
= 0; tries
< 2; tries
++)
4331 entireWidth
= 2 * EXTRA_BORDER_X
;
4335 // Now we have decided that the items do not fit into the
4336 // client area, so we need a scrollbar
4337 entireWidth
+= SCROLL_UNIT_X
;
4340 int x
= EXTRA_BORDER_X
;
4341 int y
= EXTRA_BORDER_Y
;
4342 int maxWidthInThisRow
= 0;
4345 int currentlyVisibleLines
= 0;
4347 for (size_t i
= 0; i
< count
; i
++)
4349 currentlyVisibleLines
++;
4350 wxListLineData
*line
= GetLine( i
);
4351 line
->CalculateSize( &dc
, iconSpacing
);
4352 line
->SetPosition( x
, y
, iconSpacing
);
4354 wxSize sizeLine
= GetLineSize( i
);
4356 if ( maxWidthInThisRow
< sizeLine
.x
)
4357 maxWidthInThisRow
= sizeLine
.x
;
4360 if (currentlyVisibleLines
> m_linesPerPage
)
4361 m_linesPerPage
= currentlyVisibleLines
;
4363 if ( y
+ sizeLine
.y
>= clientHeight
)
4365 currentlyVisibleLines
= 0;
4367 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4368 x
+= maxWidthInThisRow
;
4369 entireWidth
+= maxWidthInThisRow
;
4370 maxWidthInThisRow
= 0;
4373 // We have reached the last item.
4374 if ( i
== count
- 1 )
4375 entireWidth
+= maxWidthInThisRow
;
4377 if ( (tries
== 0) &&
4378 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4380 clientHeight
-= wxSystemSettings::
4381 GetMetric(wxSYS_HSCROLL_Y
);
4386 if ( i
== count
- 1 )
4387 tries
= 1; // Everything fits, no second try required.
4395 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4397 GetScrollPos( wxHORIZONTAL
),
4406 // FIXME: why should we call it from here?
4413 void wxListMainWindow::RefreshAll()
4418 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4419 if ( headerWin
&& headerWin
->m_dirty
)
4421 headerWin
->m_dirty
= false;
4422 headerWin
->Refresh();
4426 void wxListMainWindow::UpdateCurrent()
4428 if ( !HasCurrent() && !IsEmpty() )
4432 long wxListMainWindow::GetNextItem( long item
,
4433 int WXUNUSED(geometry
),
4437 max
= GetItemCount();
4438 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4439 _T("invalid listctrl index in GetNextItem()") );
4441 // notice that we start with the next item (or the first one if item == -1)
4442 // and this is intentional to allow writing a simple loop to iterate over
4443 // all selected items
4446 // this is not an error because the index was OK initially,
4447 // just no such item
4454 size_t count
= GetItemCount();
4455 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4457 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4460 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4467 // ----------------------------------------------------------------------------
4469 // ----------------------------------------------------------------------------
4471 void wxListMainWindow::DeleteItem( long lindex
)
4473 size_t count
= GetItemCount();
4475 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4476 _T("invalid item index in DeleteItem") );
4478 size_t index
= (size_t)lindex
;
4480 // we don't need to adjust the index for the previous items
4481 if ( HasCurrent() && m_current
>= index
)
4483 // if the current item is being deleted, we want the next one to
4484 // become selected - unless there is no next one - so don't adjust
4485 // m_current in this case
4486 if ( m_current
!= index
|| m_current
== count
- 1 )
4490 if ( InReportView() )
4492 // mark the Column Max Width cache as dirty if the items in the line
4493 // we're deleting contain the Max Column Width
4494 wxListLineData
* const line
= GetLine(index
);
4495 wxListItemDataList::compatibility_iterator n
;
4496 wxListItemData
*itemData
;
4500 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4502 n
= line
->m_items
.Item( i
);
4503 itemData
= n
->GetData();
4504 itemData
->GetItem(item
);
4506 itemWidth
= GetItemWidthWithImage(&item
);
4508 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4509 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4512 ResetVisibleLinesRange();
4515 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4520 m_selStore
.OnItemDelete(index
);
4524 m_lines
.RemoveAt( index
);
4527 // we need to refresh the (vert) scrollbar as the number of items changed
4530 RefreshAfter(index
);
4533 void wxListMainWindow::DeleteColumn( int col
)
4535 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4537 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4540 delete node
->GetData();
4541 m_columns
.Erase( node
);
4545 // update all the items
4546 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4548 wxListLineData
* const line
= GetLine(i
);
4549 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4550 delete n
->GetData();
4551 line
->m_items
.Erase(n
);
4555 if ( InReportView() ) // we only cache max widths when in Report View
4557 delete m_aColWidths
.Item(col
);
4558 m_aColWidths
.RemoveAt(col
);
4561 // invalidate it as it has to be recalculated
4565 void wxListMainWindow::DoDeleteAllItems()
4568 // nothing to do - in particular, don't send the event
4573 // to make the deletion of all items faster, we don't send the
4574 // notifications for each item deletion in this case but only one event
4575 // for all of them: this is compatible with wxMSW and documented in
4576 // DeleteAllItems() description
4578 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4579 event
.SetEventObject( GetParent() );
4580 GetParent()->GetEventHandler()->ProcessEvent( event
);
4588 if ( InReportView() )
4590 ResetVisibleLinesRange();
4591 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4593 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4600 void wxListMainWindow::DeleteAllItems()
4604 RecalculatePositions();
4607 void wxListMainWindow::DeleteEverything()
4609 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4610 WX_CLEAR_ARRAY(m_aColWidths
);
4615 // ----------------------------------------------------------------------------
4616 // scanning for an item
4617 // ----------------------------------------------------------------------------
4619 void wxListMainWindow::EnsureVisible( long index
)
4621 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4622 _T("invalid index in EnsureVisible") );
4624 // We have to call this here because the label in question might just have
4625 // been added and its position is not known yet
4627 RecalculatePositions(true /* no refresh */);
4629 MoveToItem((size_t)index
);
4632 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4638 wxString str_upper
= str
.Upper();
4642 size_t count
= GetItemCount();
4643 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4645 wxListLineData
*line
= GetLine(i
);
4646 wxString line_upper
= line
->GetText(0).Upper();
4649 if (line_upper
== str_upper
)
4654 if (line_upper
.find(str_upper
) == 0)
4662 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4668 size_t count
= GetItemCount();
4669 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4671 wxListLineData
*line
= GetLine(i
);
4673 line
->GetItem( 0, item
);
4674 if (item
.m_data
== data
)
4681 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4684 GetVisibleLinesRange( &topItem
, NULL
);
4687 GetItemPosition( GetItemCount() - 1, p
);
4691 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4692 if ( id
>= 0 && id
< (long)GetItemCount() )
4698 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4700 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4702 size_t count
= GetItemCount();
4704 if ( InReportView() )
4706 size_t current
= y
/ GetLineHeight();
4707 if ( current
< count
)
4709 flags
= HitTestLine(current
, x
, y
);
4716 // TODO: optimize it too! this is less simple than for report view but
4717 // enumerating all items is still not a way to do it!!
4718 for ( size_t current
= 0; current
< count
; current
++ )
4720 flags
= HitTestLine(current
, x
, y
);
4729 // ----------------------------------------------------------------------------
4731 // ----------------------------------------------------------------------------
4733 void wxListMainWindow::InsertItem( wxListItem
&item
)
4735 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4737 int count
= GetItemCount();
4738 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4740 if (item
.m_itemId
> count
)
4741 item
.m_itemId
= count
;
4743 size_t id
= item
.m_itemId
;
4747 if ( InReportView() )
4749 ResetVisibleLinesRange();
4751 // calculate the width of the item and adjust the max column width
4752 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4753 int width
= GetItemWidthWithImage(&item
);
4754 item
.SetWidth(width
);
4755 if (width
> pWidthInfo
->nMaxWidth
)
4756 pWidthInfo
->nMaxWidth
= width
;
4759 wxListLineData
*line
= new wxListLineData(this);
4761 line
->SetItem( item
.m_col
, item
);
4763 m_lines
.Insert( line
, id
);
4767 // If an item is selected at or below the point of insertion, we need to
4768 // increment the member variables because the current row's index has gone
4770 if ( HasCurrent() && m_current
>= id
)
4773 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4775 RefreshLines(id
, GetItemCount() - 1);
4778 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4781 if ( InReportView() )
4783 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4784 item
.m_width
= GetTextLength( item
.m_text
);
4786 wxListHeaderData
*column
= new wxListHeaderData( item
);
4787 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4789 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4792 wxListHeaderDataList::compatibility_iterator
4793 node
= m_columns
.Item( col
);
4794 m_columns
.Insert( node
, column
);
4795 m_aColWidths
.Insert( colWidthInfo
, col
);
4799 m_columns
.Append( column
);
4800 m_aColWidths
.Add( colWidthInfo
);
4805 // update all the items
4806 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4808 wxListLineData
* const line
= GetLine(i
);
4809 wxListItemData
* const data
= new wxListItemData(this);
4811 line
->m_items
.Insert(col
, data
);
4813 line
->m_items
.Append(data
);
4817 // invalidate it as it has to be recalculated
4822 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4825 wxClientDC
dc(this);
4827 dc
.SetFont( GetFont() );
4829 if (item
->GetImage() != -1)
4832 GetImageSize( item
->GetImage(), ix
, iy
);
4836 if (!item
->GetText().empty())
4839 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4846 // ----------------------------------------------------------------------------
4848 // ----------------------------------------------------------------------------
4850 wxListCtrlCompare list_ctrl_compare_func_2
;
4851 long list_ctrl_compare_data
;
4853 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4855 wxListLineData
*line1
= *arg1
;
4856 wxListLineData
*line2
= *arg2
;
4858 line1
->GetItem( 0, item
);
4859 wxUIntPtr data1
= item
.m_data
;
4860 line2
->GetItem( 0, item
);
4861 wxUIntPtr data2
= item
.m_data
;
4862 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4865 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4867 // selections won't make sense any more after sorting the items so reset
4869 HighlightAll(false);
4872 list_ctrl_compare_func_2
= fn
;
4873 list_ctrl_compare_data
= data
;
4874 m_lines
.Sort( list_ctrl_compare_func_1
);
4878 // ----------------------------------------------------------------------------
4880 // ----------------------------------------------------------------------------
4882 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4885 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4886 wxScrolledWindow::OnScroll(event
);
4888 HandleOnScroll( event
);
4891 // update our idea of which lines are shown when we redraw the window the
4893 ResetVisibleLinesRange();
4895 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4897 wxGenericListCtrl
* lc
= GetListCtrl();
4898 wxCHECK_RET( lc
, _T("no listctrl window?") );
4900 lc
->m_headerWin
->Refresh();
4901 lc
->m_headerWin
->Update();
4905 int wxListMainWindow::GetCountPerPage() const
4907 if ( !m_linesPerPage
)
4909 wxConstCast(this, wxListMainWindow
)->
4910 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4913 return m_linesPerPage
;
4916 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4918 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4920 if ( m_lineFrom
== (size_t)-1 )
4922 size_t count
= GetItemCount();
4925 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4927 // this may happen if SetScrollbars() hadn't been called yet
4928 if ( m_lineFrom
>= count
)
4929 m_lineFrom
= count
- 1;
4931 // we redraw one extra line but this is needed to make the redrawing
4932 // logic work when there is a fractional number of lines on screen
4933 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4934 if ( m_lineTo
>= count
)
4935 m_lineTo
= count
- 1;
4937 else // empty control
4940 m_lineTo
= (size_t)-1;
4944 wxASSERT_MSG( IsEmpty() ||
4945 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4946 _T("GetVisibleLinesRange() returns incorrect result") );
4954 // -------------------------------------------------------------------------------------
4955 // wxGenericListCtrl
4956 // -------------------------------------------------------------------------------------
4958 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4960 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4961 EVT_SIZE(wxGenericListCtrl::OnSize
)
4964 wxGenericListCtrl::wxGenericListCtrl()
4966 m_imageListNormal
= (wxImageList
*) NULL
;
4967 m_imageListSmall
= (wxImageList
*) NULL
;
4968 m_imageListState
= (wxImageList
*) NULL
;
4970 m_ownsImageListNormal
=
4971 m_ownsImageListSmall
=
4972 m_ownsImageListState
= false;
4974 m_mainWin
= (wxListMainWindow
*) NULL
;
4975 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4979 wxGenericListCtrl::~wxGenericListCtrl()
4981 if (m_ownsImageListNormal
)
4982 delete m_imageListNormal
;
4983 if (m_ownsImageListSmall
)
4984 delete m_imageListSmall
;
4985 if (m_ownsImageListState
)
4986 delete m_imageListState
;
4989 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4995 GetThemeMetric( kThemeMetricListHeaderHeight
, &h
);
4997 // we use 'g' to get the descent, too
4999 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
5000 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
5003 // only update if changed
5004 if ( h
!= m_headerHeight
)
5009 ResizeReportView(true);
5010 else //why is this needed if it doesn't have a header?
5011 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
5016 void wxGenericListCtrl::CreateHeaderWindow()
5018 m_headerWin
= new wxListHeaderWindow
5020 this, wxID_ANY
, m_mainWin
,
5022 wxSize(GetClientSize().x
, m_headerHeight
),
5025 CalculateAndSetHeaderHeight();
5028 bool wxGenericListCtrl::Create(wxWindow
*parent
,
5033 const wxValidator
&validator
,
5034 const wxString
&name
)
5038 m_imageListState
= (wxImageList
*) NULL
;
5039 m_ownsImageListNormal
=
5040 m_ownsImageListSmall
=
5041 m_ownsImageListState
= false;
5043 m_mainWin
= (wxListMainWindow
*) NULL
;
5044 m_headerWin
= (wxListHeaderWindow
*) NULL
;
5048 if ( !(style
& wxLC_MASK_TYPE
) )
5050 style
= style
| wxLC_LIST
;
5053 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
5056 // this window itself shouldn't get the focus, only m_mainWin should
5059 // don't create the inner window with the border
5060 style
&= ~wxBORDER_MASK
;
5062 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
5065 // Human Interface Guidelines ask us for a special font in this case
5066 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
)
5069 font
.MacCreateFromThemeFont( kThemeViewsFont
);
5074 if ( InReportView() )
5076 CreateHeaderWindow();
5082 font
.MacCreateFromThemeFont( kThemeSmallSystemFont
);
5083 m_headerWin
->SetFont( font
);
5084 CalculateAndSetHeaderHeight();
5088 if ( HasFlag(wxLC_NO_HEADER
) )
5089 // VZ: why do we create it at all then?
5090 m_headerWin
->Show( false );
5093 SetInitialSize(size
);
5098 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
5100 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
5101 _T("wxLC_VIRTUAL can't be [un]set") );
5103 long flag
= GetWindowStyle();
5107 if (style
& wxLC_MASK_TYPE
)
5108 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5109 if (style
& wxLC_MASK_ALIGN
)
5110 flag
&= ~wxLC_MASK_ALIGN
;
5111 if (style
& wxLC_MASK_SORT
)
5112 flag
&= ~wxLC_MASK_SORT
;
5120 SetWindowStyleFlag( flag
);
5123 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5127 m_mainWin
->DeleteEverything();
5129 // has the header visibility changed?
5130 bool hasHeader
= HasHeader();
5131 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
5133 if ( hasHeader
!= willHaveHeader
)
5140 // don't delete, just hide, as we can reuse it later
5141 m_headerWin
->Show(false);
5143 //else: nothing to do
5145 else // must show header
5149 CreateHeaderWindow();
5151 else // already have it, just show
5153 m_headerWin
->Show( true );
5157 ResizeReportView(willHaveHeader
);
5161 wxWindow::SetWindowStyleFlag( flag
);
5164 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5166 m_mainWin
->GetColumn( col
, item
);
5170 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5172 m_mainWin
->SetColumn( col
, item
);
5176 int wxGenericListCtrl::GetColumnWidth( int col
) const
5178 return m_mainWin
->GetColumnWidth( col
);
5181 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5183 m_mainWin
->SetColumnWidth( col
, width
);
5187 int wxGenericListCtrl::GetCountPerPage() const
5189 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5192 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5194 m_mainWin
->GetItem( info
);
5198 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5200 m_mainWin
->SetItem( info
);
5204 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5207 info
.m_text
= label
;
5208 info
.m_mask
= wxLIST_MASK_TEXT
;
5209 info
.m_itemId
= index
;
5213 info
.m_image
= imageId
;
5214 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5217 m_mainWin
->SetItem(info
);
5221 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5223 return m_mainWin
->GetItemState( item
, stateMask
);
5226 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5228 m_mainWin
->SetItemState( item
, state
, stateMask
);
5233 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5235 return SetItemColumnImage(item
, 0, image
);
5239 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5242 info
.m_image
= image
;
5243 info
.m_mask
= wxLIST_MASK_IMAGE
;
5244 info
.m_itemId
= item
;
5245 info
.m_col
= column
;
5246 m_mainWin
->SetItem( info
);
5250 wxString
wxGenericListCtrl::GetItemText( long item
) const
5252 return m_mainWin
->GetItemText(item
);
5255 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5257 m_mainWin
->SetItemText(item
, str
);
5260 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5263 info
.m_mask
= wxLIST_MASK_DATA
;
5264 info
.m_itemId
= item
;
5265 m_mainWin
->GetItem( info
);
5269 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
5272 info
.m_mask
= wxLIST_MASK_DATA
;
5273 info
.m_itemId
= item
;
5275 m_mainWin
->SetItem( info
);
5279 wxRect
wxGenericListCtrl::GetViewRect() const
5281 return m_mainWin
->GetViewRect();
5284 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
5286 m_mainWin
->GetItemRect( item
, rect
);
5287 if ( m_mainWin
->HasHeader() )
5288 rect
.y
+= m_headerHeight
+ 1;
5292 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5294 m_mainWin
->GetItemPosition( item
, pos
);
5298 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5303 int wxGenericListCtrl::GetItemCount() const
5305 return m_mainWin
->GetItemCount();
5308 int wxGenericListCtrl::GetColumnCount() const
5310 return m_mainWin
->GetColumnCount();
5313 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5315 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5318 wxSize
wxGenericListCtrl::GetItemSpacing() const
5320 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5322 return wxSize(spacing
, spacing
);
5325 #if WXWIN_COMPATIBILITY_2_6
5326 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5328 return m_mainWin
->GetItemSpacing( isSmall
);
5330 #endif // WXWIN_COMPATIBILITY_2_6
5332 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5335 info
.m_itemId
= item
;
5336 info
.SetTextColour( col
);
5337 m_mainWin
->SetItem( info
);
5340 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5343 info
.m_itemId
= item
;
5344 m_mainWin
->GetItem( info
);
5345 return info
.GetTextColour();
5348 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5351 info
.m_itemId
= item
;
5352 info
.SetBackgroundColour( col
);
5353 m_mainWin
->SetItem( info
);
5356 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5359 info
.m_itemId
= item
;
5360 m_mainWin
->GetItem( info
);
5361 return info
.GetBackgroundColour();
5364 int wxGenericListCtrl::GetScrollPos( int orient
) const
5366 return m_mainWin
->GetScrollPos( orient
);
5369 void wxGenericListCtrl::SetScrollPos( int orient
, int pos
, bool refresh
)
5371 m_mainWin
->SetScrollPos( orient
, pos
, refresh
);
5374 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5377 info
.m_itemId
= item
;
5379 m_mainWin
->SetItem( info
);
5382 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5385 info
.m_itemId
= item
;
5386 m_mainWin
->GetItem( info
);
5387 return info
.GetFont();
5390 int wxGenericListCtrl::GetSelectedItemCount() const
5392 return m_mainWin
->GetSelectedItemCount();
5395 wxColour
wxGenericListCtrl::GetTextColour() const
5397 return GetForegroundColour();
5400 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5402 SetForegroundColour(col
);
5405 long wxGenericListCtrl::GetTopItem() const
5408 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5412 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5414 return m_mainWin
->GetNextItem( item
, geom
, state
);
5417 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5419 if (which
== wxIMAGE_LIST_NORMAL
)
5420 return m_imageListNormal
;
5421 else if (which
== wxIMAGE_LIST_SMALL
)
5422 return m_imageListSmall
;
5423 else if (which
== wxIMAGE_LIST_STATE
)
5424 return m_imageListState
;
5426 return (wxImageList
*) NULL
;
5429 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5431 if ( which
== wxIMAGE_LIST_NORMAL
)
5433 if (m_ownsImageListNormal
)
5434 delete m_imageListNormal
;
5435 m_imageListNormal
= imageList
;
5436 m_ownsImageListNormal
= false;
5438 else if ( which
== wxIMAGE_LIST_SMALL
)
5440 if (m_ownsImageListSmall
)
5441 delete m_imageListSmall
;
5442 m_imageListSmall
= imageList
;
5443 m_ownsImageListSmall
= false;
5445 else if ( which
== wxIMAGE_LIST_STATE
)
5447 if (m_ownsImageListState
)
5448 delete m_imageListState
;
5449 m_imageListState
= imageList
;
5450 m_ownsImageListState
= false;
5453 m_mainWin
->SetImageList( imageList
, which
);
5456 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5458 SetImageList(imageList
, which
);
5459 if ( which
== wxIMAGE_LIST_NORMAL
)
5460 m_ownsImageListNormal
= true;
5461 else if ( which
== wxIMAGE_LIST_SMALL
)
5462 m_ownsImageListSmall
= true;
5463 else if ( which
== wxIMAGE_LIST_STATE
)
5464 m_ownsImageListState
= true;
5467 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5472 bool wxGenericListCtrl::DeleteItem( long item
)
5474 m_mainWin
->DeleteItem( item
);
5478 bool wxGenericListCtrl::DeleteAllItems()
5480 m_mainWin
->DeleteAllItems();
5484 bool wxGenericListCtrl::DeleteAllColumns()
5486 size_t count
= m_mainWin
->m_columns
.GetCount();
5487 for ( size_t n
= 0; n
< count
; n
++ )
5492 void wxGenericListCtrl::ClearAll()
5494 m_mainWin
->DeleteEverything();
5497 bool wxGenericListCtrl::DeleteColumn( int col
)
5499 m_mainWin
->DeleteColumn( col
);
5501 // if we don't have the header any longer, we need to relayout the window
5502 if ( !GetColumnCount() )
5503 ResizeReportView(false /* no header */);
5507 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5508 wxClassInfo
* textControlClass
)
5510 return m_mainWin
->EditLabel( item
, textControlClass
);
5513 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5515 return m_mainWin
->GetEditControl();
5518 bool wxGenericListCtrl::EnsureVisible( long item
)
5520 m_mainWin
->EnsureVisible( item
);
5524 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5526 return m_mainWin
->FindItem( start
, str
, partial
);
5529 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5531 return m_mainWin
->FindItem( start
, data
);
5534 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5535 int WXUNUSED(direction
))
5537 return m_mainWin
->FindItem( pt
);
5540 // TODO: sub item hit testing
5541 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5543 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5546 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5548 m_mainWin
->InsertItem( info
);
5549 return info
.m_itemId
;
5552 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5555 info
.m_text
= label
;
5556 info
.m_mask
= wxLIST_MASK_TEXT
;
5557 info
.m_itemId
= index
;
5558 return InsertItem( info
);
5561 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5564 info
.m_mask
= wxLIST_MASK_IMAGE
;
5565 info
.m_image
= imageIndex
;
5566 info
.m_itemId
= index
;
5567 return InsertItem( info
);
5570 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5573 info
.m_text
= label
;
5574 info
.m_image
= imageIndex
;
5575 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5576 info
.m_itemId
= index
;
5577 return InsertItem( info
);
5580 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5582 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5584 m_mainWin
->InsertColumn( col
, item
);
5586 // if we hadn't had a header before but have one now
5587 // then we need to relayout the window
5588 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5589 ResizeReportView(true /* have header */);
5591 m_headerWin
->Refresh();
5596 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5597 int format
, int width
)
5600 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5601 item
.m_text
= heading
;
5604 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5605 item
.m_width
= width
;
5608 item
.m_format
= format
;
5610 return InsertColumn( col
, item
);
5613 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
5615 return m_mainWin
->ScrollList(dx
, dy
);
5619 // fn is a function which takes 3 long arguments: item1, item2, data.
5620 // item1 is the long data associated with a first item (NOT the index).
5621 // item2 is the long data associated with a second item (NOT the index).
5622 // data is the same value as passed to SortItems.
5623 // The return value is a negative number if the first item should precede the second
5624 // item, a positive number of the second item should precede the first,
5625 // or zero if the two items are equivalent.
5626 // data is arbitrary data to be passed to the sort function.
5628 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5630 m_mainWin
->SortItems( fn
, data
);
5634 // ----------------------------------------------------------------------------
5636 // ----------------------------------------------------------------------------
5638 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5643 ResizeReportView(m_mainWin
->HasHeader());
5644 m_mainWin
->RecalculatePositions();
5647 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5650 GetClientSize( &cw
, &ch
);
5654 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5655 if(ch
> m_headerHeight
)
5656 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5657 cw
, ch
- m_headerHeight
- 1 );
5659 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5662 else // no header window
5664 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5668 void wxGenericListCtrl::OnInternalIdle()
5670 wxWindow::OnInternalIdle();
5672 // do it only if needed
5673 if ( !m_mainWin
->m_dirty
)
5676 m_mainWin
->RecalculatePositions();
5679 // ----------------------------------------------------------------------------
5681 // ----------------------------------------------------------------------------
5683 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5687 m_mainWin
->SetBackgroundColour( colour
);
5688 m_mainWin
->m_dirty
= true;
5694 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5696 if ( !wxWindow::SetForegroundColour( colour
) )
5701 m_mainWin
->SetForegroundColour( colour
);
5702 m_mainWin
->m_dirty
= true;
5706 m_headerWin
->SetForegroundColour( colour
);
5711 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5713 if ( !wxWindow::SetFont( font
) )
5718 m_mainWin
->SetFont( font
);
5719 m_mainWin
->m_dirty
= true;
5724 m_headerWin
->SetFont( font
);
5725 CalculateAndSetHeaderHeight();
5735 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5738 // Use the same color scheme as wxListBox
5739 return wxListBox::GetClassDefaultAttributes(variant
);
5741 wxUnusedVar(variant
);
5742 wxVisualAttributes attr
;
5743 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5744 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5745 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5750 // ----------------------------------------------------------------------------
5751 // methods forwarded to m_mainWin
5752 // ----------------------------------------------------------------------------
5754 #if wxUSE_DRAG_AND_DROP
5756 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5758 m_mainWin
->SetDropTarget( dropTarget
);
5761 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5763 return m_mainWin
->GetDropTarget();
5768 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5770 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5773 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5775 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5778 wxColour
wxGenericListCtrl::GetForegroundColour() const
5780 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5783 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5786 return m_mainWin
->PopupMenu( menu
, x
, y
);
5792 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5794 m_mainWin
->DoClientToScreen(x
, y
);
5797 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5799 m_mainWin
->DoScreenToClient(x
, y
);
5802 void wxGenericListCtrl::SetFocus()
5804 // The test in window.cpp fails as we are a composite
5805 // window, so it checks against "this", but not m_mainWin.
5806 if ( DoFindFocus() != this )
5807 m_mainWin
->SetFocus();
5810 wxSize
wxGenericListCtrl::DoGetBestSize() const
5812 // Something is better than nothing...
5813 // 100x80 is what the MSW version will get from the default
5814 // wxControl::DoGetBestSize
5815 return wxSize(100, 80);
5818 // ----------------------------------------------------------------------------
5819 // virtual list control support
5820 // ----------------------------------------------------------------------------
5822 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5824 // this is a pure virtual function, in fact - which is not really pure
5825 // because the controls which are not virtual don't need to implement it
5826 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5828 return wxEmptyString
;
5831 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5833 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5835 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5839 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5842 return OnGetItemImage(item
);
5848 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5850 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5851 _T("invalid item index in OnGetItemAttr()") );
5853 // no attributes by default
5857 void wxGenericListCtrl::SetItemCount(long count
)
5859 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5861 m_mainWin
->SetItemCount(count
);
5864 void wxGenericListCtrl::RefreshItem(long item
)
5866 m_mainWin
->RefreshLine(item
);
5869 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5871 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5874 // Generic wxListCtrl is more or less a container for two other
5875 // windows which drawings are done upon. These are namely
5876 // 'm_headerWin' and 'm_mainWin'.
5877 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5878 // behaviour wxListCtrl has under wxMSW.
5880 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5884 // The easy case, no rectangle specified.
5886 m_headerWin
->Refresh(eraseBackground
);
5889 m_mainWin
->Refresh(eraseBackground
);
5893 // Refresh the header window
5896 wxRect rectHeader
= m_headerWin
->GetRect();
5897 rectHeader
.Intersect(*rect
);
5898 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5901 m_headerWin
->GetPosition(&x
, &y
);
5902 rectHeader
.Offset(-x
, -y
);
5903 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5907 // Refresh the main window
5910 wxRect rectMain
= m_mainWin
->GetRect();
5911 rectMain
.Intersect(*rect
);
5912 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5915 m_mainWin
->GetPosition(&x
, &y
);
5916 rectMain
.Offset(-x
, -y
);
5917 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5923 void wxGenericListCtrl::Freeze()
5925 m_mainWin
->Freeze();
5928 void wxGenericListCtrl::Thaw()
5933 #endif // wxUSE_LISTCTRL