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 // we don't draw anything while we're frozen so we must refresh ourselves
604 // when we're thawed to make sure the changes are displayed correctly
605 virtual void DoThaw() { Refresh(); }
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 // wrapper around the text control currently used for in place editing or
854 // NULL if no item is being edited
855 wxListTextCtrlWrapper
*m_textctrlWrapper
;
858 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
859 DECLARE_EVENT_TABLE()
861 friend class wxGenericListCtrl
;
865 wxListItemData::~wxListItemData()
867 // in the virtual list control the attributes are managed by the main
868 // program, so don't delete them
869 if ( !m_owner
->IsVirtual() )
875 void wxListItemData::Init()
883 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
889 if ( owner
->InReportView() )
895 void wxListItemData::SetItem( const wxListItem
&info
)
897 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
898 SetText(info
.m_text
);
899 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
900 m_image
= info
.m_image
;
901 if ( info
.m_mask
& wxLIST_MASK_DATA
)
902 m_data
= info
.m_data
;
904 if ( info
.HasAttributes() )
907 m_attr
->AssignFrom(*info
.GetAttributes());
909 m_attr
= new wxListItemAttr(*info
.GetAttributes());
917 m_rect
->width
= info
.m_width
;
921 void wxListItemData::SetPosition( int x
, int y
)
923 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
929 void wxListItemData::SetSize( int width
, int height
)
931 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
934 m_rect
->width
= width
;
936 m_rect
->height
= height
;
939 bool wxListItemData::IsHit( int x
, int y
) const
941 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
943 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
946 int wxListItemData::GetX() const
948 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
953 int wxListItemData::GetY() const
955 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
960 int wxListItemData::GetWidth() const
962 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
964 return m_rect
->width
;
967 int wxListItemData::GetHeight() const
969 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
971 return m_rect
->height
;
974 void wxListItemData::GetItem( wxListItem
&info
) const
976 long mask
= info
.m_mask
;
978 // by default, get everything for backwards compatibility
981 if ( mask
& wxLIST_MASK_TEXT
)
982 info
.m_text
= m_text
;
983 if ( mask
& wxLIST_MASK_IMAGE
)
984 info
.m_image
= m_image
;
985 if ( mask
& wxLIST_MASK_DATA
)
986 info
.m_data
= m_data
;
990 if ( m_attr
->HasTextColour() )
991 info
.SetTextColour(m_attr
->GetTextColour());
992 if ( m_attr
->HasBackgroundColour() )
993 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
994 if ( m_attr
->HasFont() )
995 info
.SetFont(m_attr
->GetFont());
999 //-----------------------------------------------------------------------------
1001 //-----------------------------------------------------------------------------
1003 void wxListHeaderData::Init()
1015 wxListHeaderData::wxListHeaderData()
1020 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1027 void wxListHeaderData::SetItem( const wxListItem
&item
)
1029 m_mask
= item
.m_mask
;
1031 if ( m_mask
& wxLIST_MASK_TEXT
)
1032 m_text
= item
.m_text
;
1034 if ( m_mask
& wxLIST_MASK_IMAGE
)
1035 m_image
= item
.m_image
;
1037 if ( m_mask
& wxLIST_MASK_FORMAT
)
1038 m_format
= item
.m_format
;
1040 if ( m_mask
& wxLIST_MASK_WIDTH
)
1041 SetWidth(item
.m_width
);
1043 if ( m_mask
& wxLIST_MASK_STATE
)
1044 SetState(item
.m_state
);
1047 void wxListHeaderData::SetPosition( int x
, int y
)
1053 void wxListHeaderData::SetHeight( int h
)
1058 void wxListHeaderData::SetWidth( int w
)
1060 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1063 void wxListHeaderData::SetState( int flag
)
1068 void wxListHeaderData::SetFormat( int format
)
1073 bool wxListHeaderData::HasImage() const
1075 return m_image
!= -1;
1078 bool wxListHeaderData::IsHit( int x
, int y
) const
1080 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1083 void wxListHeaderData::GetItem( wxListItem
& item
)
1085 item
.m_mask
= m_mask
;
1086 item
.m_text
= m_text
;
1087 item
.m_image
= m_image
;
1088 item
.m_format
= m_format
;
1089 item
.m_width
= m_width
;
1090 item
.m_state
= m_state
;
1093 int wxListHeaderData::GetImage() const
1098 int wxListHeaderData::GetWidth() const
1103 int wxListHeaderData::GetFormat() const
1108 int wxListHeaderData::GetState() const
1113 //-----------------------------------------------------------------------------
1115 //-----------------------------------------------------------------------------
1117 inline int wxListLineData::GetMode() const
1119 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1122 inline bool wxListLineData::InReportView() const
1124 return m_owner
->HasFlag(wxLC_REPORT
);
1127 inline bool wxListLineData::IsVirtual() const
1129 return m_owner
->IsVirtual();
1132 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1136 if ( InReportView() )
1139 m_gi
= new GeometryInfo
;
1141 m_highlighted
= false;
1143 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1146 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1148 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1149 wxCHECK_RET( node
, _T("no subitems at all??") );
1151 wxListItemData
*item
= node
->GetData();
1156 switch ( GetMode() )
1159 case wxLC_SMALL_ICON
:
1160 m_gi
->m_rectAll
.width
= spacing
;
1162 s
= item
->GetText();
1167 m_gi
->m_rectLabel
.width
=
1168 m_gi
->m_rectLabel
.height
= 0;
1172 dc
->GetTextExtent( s
, &lw
, &lh
);
1176 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1178 m_gi
->m_rectAll
.width
= lw
;
1180 m_gi
->m_rectLabel
.width
= lw
;
1181 m_gi
->m_rectLabel
.height
= lh
;
1184 if (item
->HasImage())
1187 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1188 m_gi
->m_rectIcon
.width
= w
+ 8;
1189 m_gi
->m_rectIcon
.height
= h
+ 8;
1191 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1192 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1193 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1194 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1197 if ( item
->HasText() )
1199 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1200 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1202 else // no text, highlight the icon
1204 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1205 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1210 s
= item
->GetTextForMeasuring();
1212 dc
->GetTextExtent( s
, &lw
, &lh
);
1216 m_gi
->m_rectLabel
.width
= lw
;
1217 m_gi
->m_rectLabel
.height
= lh
;
1219 m_gi
->m_rectAll
.width
= lw
;
1220 m_gi
->m_rectAll
.height
= lh
;
1222 if (item
->HasImage())
1225 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1226 m_gi
->m_rectIcon
.width
= w
;
1227 m_gi
->m_rectIcon
.height
= h
;
1229 m_gi
->m_rectAll
.width
+= 4 + w
;
1230 if (h
> m_gi
->m_rectAll
.height
)
1231 m_gi
->m_rectAll
.height
= h
;
1234 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1235 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1239 wxFAIL_MSG( _T("unexpected call to SetSize") );
1243 wxFAIL_MSG( _T("unknown mode") );
1248 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1250 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1251 wxCHECK_RET( node
, _T("no subitems at all??") );
1253 wxListItemData
*item
= node
->GetData();
1255 switch ( GetMode() )
1258 case wxLC_SMALL_ICON
:
1259 m_gi
->m_rectAll
.x
= x
;
1260 m_gi
->m_rectAll
.y
= y
;
1262 if ( item
->HasImage() )
1264 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1265 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1266 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1269 if ( item
->HasText() )
1271 if (m_gi
->m_rectAll
.width
> spacing
)
1272 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1274 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1275 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1276 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1277 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1279 else // no text, highlight the icon
1281 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1282 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1287 m_gi
->m_rectAll
.x
= x
;
1288 m_gi
->m_rectAll
.y
= y
;
1290 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1291 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1292 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1294 if (item
->HasImage())
1296 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1297 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1298 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1302 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1307 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1311 wxFAIL_MSG( _T("unknown mode") );
1316 void wxListLineData::InitItems( int num
)
1318 for (int i
= 0; i
< num
; i
++)
1319 m_items
.Append( new wxListItemData(m_owner
) );
1322 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1324 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1325 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1327 wxListItemData
*item
= node
->GetData();
1328 item
->SetItem( info
);
1331 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1333 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1336 wxListItemData
*item
= node
->GetData();
1337 item
->GetItem( info
);
1341 wxString
wxListLineData::GetText(int index
) const
1345 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1348 wxListItemData
*item
= node
->GetData();
1349 s
= item
->GetText();
1355 void wxListLineData::SetText( int index
, const wxString
& s
)
1357 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1360 wxListItemData
*item
= node
->GetData();
1365 void wxListLineData::SetImage( int index
, int image
)
1367 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1368 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1370 wxListItemData
*item
= node
->GetData();
1371 item
->SetImage(image
);
1374 int wxListLineData::GetImage( int index
) const
1376 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1377 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1379 wxListItemData
*item
= node
->GetData();
1380 return item
->GetImage();
1383 wxListItemAttr
*wxListLineData::GetAttr() const
1385 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1386 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1388 wxListItemData
*item
= node
->GetData();
1389 return item
->GetAttr();
1392 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1394 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1395 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1397 wxListItemData
*item
= node
->GetData();
1398 item
->SetAttr(attr
);
1401 bool wxListLineData::SetAttributes(wxDC
*dc
,
1402 const wxListItemAttr
*attr
,
1405 wxWindow
*listctrl
= m_owner
->GetParent();
1409 // don't use foreground colour for drawing highlighted items - this might
1410 // make them completely invisible (and there is no way to do bit
1411 // arithmetics on wxColour, unfortunately)
1416 if (m_owner
->HasFocus()
1417 #if !defined(__WXUNIVERSAL__)
1418 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1426 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1428 else if ( attr
&& attr
->HasTextColour() )
1429 colText
= attr
->GetTextColour();
1431 colText
= listctrl
->GetForegroundColour();
1433 dc
->SetTextForeground(colText
);
1437 if ( attr
&& attr
->HasFont() )
1438 font
= attr
->GetFont();
1440 font
= listctrl
->GetFont();
1445 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1446 if ( highlighted
|| hasBgCol
)
1449 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1451 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1453 dc
->SetPen( *wxTRANSPARENT_PEN
);
1461 void wxListLineData::Draw( wxDC
*dc
)
1463 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1464 wxCHECK_RET( node
, _T("no subitems at all??") );
1466 bool highlighted
= IsHighlighted();
1468 wxListItemAttr
*attr
= GetAttr();
1470 if ( SetAttributes(dc
, attr
, highlighted
) )
1471 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1473 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1479 int flags
= wxCONTROL_SELECTED
;
1480 if (m_owner
->HasFocus()
1481 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
1482 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1485 flags
|= wxCONTROL_FOCUSED
;
1486 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1491 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1496 // just for debugging to better see where the items are
1498 dc
->SetPen(*wxRED_PEN
);
1499 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1500 dc
->DrawRectangle( m_gi
->m_rectAll
);
1501 dc
->SetPen(*wxGREEN_PEN
);
1502 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1505 wxListItemData
*item
= node
->GetData();
1506 if (item
->HasImage())
1508 // centre the image inside our rectangle, this looks nicer when items
1509 // ae aligned in a row
1510 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1512 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1515 if (item
->HasText())
1517 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1519 wxDCClipper
clipper(*dc
, rectLabel
);
1520 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1524 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1526 const wxRect
& rectHL
,
1529 // TODO: later we should support setting different attributes for
1530 // different columns - to do it, just add "col" argument to
1531 // GetAttr() and move these lines into the loop below
1532 wxListItemAttr
*attr
= GetAttr();
1533 if ( SetAttributes(dc
, attr
, highlighted
) )
1534 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1536 dc
->DrawRectangle( rectHL
);
1542 int flags
= wxCONTROL_SELECTED
;
1543 if (m_owner
->HasFocus())
1544 flags
|= wxCONTROL_FOCUSED
;
1545 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1549 dc
->DrawRectangle( rectHL
);
1554 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1555 yMid
= rect
.y
+ rect
.height
/2;
1557 // This probably needs to be done
1558 // on all platforms as the icons
1559 // otherwise nearly touch the border
1564 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1566 node
= node
->GetNext(), col
++ )
1568 wxListItemData
*item
= node
->GetData();
1570 int width
= m_owner
->GetColumnWidth(col
);
1574 if ( item
->HasImage() )
1577 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1578 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1580 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1586 if ( item
->HasText() )
1587 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, width
- 8);
1591 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1592 const wxString
& textOrig
,
1598 // we don't support displaying multiple lines currently (and neither does
1599 // wxMSW FWIW) so just merge all the lines
1600 wxString
text(textOrig
);
1601 text
.Replace(_T("\n"), _T(" "));
1604 dc
->GetTextExtent(text
, &w
, &h
);
1606 const wxCoord y
= yMid
- (h
+ 1)/2;
1608 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1610 // determine if the string can fit inside the current width
1613 // it can, draw it using the items alignment
1615 m_owner
->GetColumn(col
, item
);
1616 switch ( item
.GetAlign() )
1618 case wxLIST_FORMAT_LEFT
:
1622 case wxLIST_FORMAT_RIGHT
:
1626 case wxLIST_FORMAT_CENTER
:
1627 x
+= (width
- w
) / 2;
1631 wxFAIL_MSG( _T("unknown list item format") );
1635 dc
->DrawText(text
, x
, y
);
1637 else // otherwise, truncate and add an ellipsis if possible
1639 // determine the base width
1640 wxString
ellipsis(wxT("..."));
1642 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1644 // continue until we have enough space or only one character left
1646 size_t len
= text
.length();
1647 wxString drawntext
= text
.Left(len
);
1650 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1651 drawntext
.RemoveLast();
1654 if (w
+ base_w
<= width
)
1658 // if still not enough space, remove ellipsis characters
1659 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1661 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1662 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1665 // now draw the text
1666 dc
->DrawText(drawntext
, x
, y
);
1667 dc
->DrawText(ellipsis
, x
+ w
, y
);
1671 bool wxListLineData::Highlight( bool on
)
1673 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1675 if ( on
== m_highlighted
)
1683 void wxListLineData::ReverseHighlight( void )
1685 Highlight(!IsHighlighted());
1688 //-----------------------------------------------------------------------------
1689 // wxListHeaderWindow
1690 //-----------------------------------------------------------------------------
1692 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1694 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1695 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1696 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1697 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1700 void wxListHeaderWindow::Init()
1702 m_currentCursor
= (wxCursor
*) NULL
;
1703 m_isDragging
= false;
1707 wxListHeaderWindow::wxListHeaderWindow()
1711 m_owner
= (wxListMainWindow
*) NULL
;
1712 m_resizeCursor
= (wxCursor
*) NULL
;
1715 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1717 wxListMainWindow
*owner
,
1721 const wxString
&name
)
1722 : wxWindow( win
, id
, pos
, size
, style
, name
)
1727 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1730 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1731 SetOwnForegroundColour( attr
.colFg
);
1732 SetOwnBackgroundColour( attr
.colBg
);
1734 SetOwnFont( attr
.font
);
1736 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1737 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1739 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1743 wxListHeaderWindow::~wxListHeaderWindow()
1745 delete m_resizeCursor
;
1748 #ifdef __WXUNIVERSAL__
1749 #include "wx/univ/renderer.h"
1750 #include "wx/univ/theme.h"
1753 // shift the DC origin to match the position of the main window horz
1754 // scrollbar: this allows us to always use logical coords
1755 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1758 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1761 m_owner
->GetViewStart( &view_start
, NULL
);
1766 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1768 // account for the horz scrollbar offset
1770 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1772 // Maybe we just have to check for m_signX
1773 // in the DC, but I leave the #ifdef __WXGTK__
1775 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1779 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1782 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1784 wxPaintDC
dc( this );
1789 dc
.SetFont( GetFont() );
1791 // width and height of the entire header window
1793 GetClientSize( &w
, &h
);
1794 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1796 dc
.SetBackgroundMode(wxTRANSPARENT
);
1797 dc
.SetTextForeground(GetForegroundColour());
1799 int x
= HEADER_OFFSET_X
;
1800 int numColumns
= m_owner
->GetColumnCount();
1802 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1804 m_owner
->GetColumn( i
, item
);
1805 int wCol
= item
.m_width
;
1811 if (!m_parent
->IsEnabled())
1812 flags
|= wxCONTROL_DISABLED
;
1814 // NB: The code below is not really Mac-specific, but since we are close
1815 // to 2.8 release and I don't have time to test on other platforms, I
1816 // defined this only for wxMac. If this behavior is desired on
1817 // other platforms, please go ahead and revise or remove the #ifdef.
1819 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1820 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1821 flags
|= wxCONTROL_SELECTED
;
1824 wxRendererNative::Get().DrawHeaderButton
1828 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1832 // see if we have enough space for the column label
1834 // for this we need the width of the text
1837 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1838 wLabel
+= 2 * EXTRA_WIDTH
;
1840 // and the width of the icon, if any
1841 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1842 const int image
= item
.m_image
;
1843 wxImageList
*imageList
;
1846 imageList
= m_owner
->m_small_image_list
;
1849 imageList
->GetSize(image
, ix
, iy
);
1850 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1858 // ignore alignment if there is not enough space anyhow
1860 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1863 wxFAIL_MSG( _T("unknown list item format") );
1866 case wxLIST_FORMAT_LEFT
:
1870 case wxLIST_FORMAT_RIGHT
:
1871 xAligned
= x
+ cw
- wLabel
;
1874 case wxLIST_FORMAT_CENTER
:
1875 xAligned
= x
+ (cw
- wLabel
) / 2;
1879 // draw the text and image clipping them so that they
1880 // don't overwrite the column boundary
1881 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1883 // if we have an image, draw it on the right of the label
1890 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1891 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1892 wxIMAGELIST_DRAW_TRANSPARENT
1896 dc
.DrawText( item
.GetText(),
1897 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1903 void wxListHeaderWindow::DrawCurrent()
1906 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1908 int x1
= m_currentX
;
1910 m_owner
->ClientToScreen( &x1
, &y1
);
1912 int x2
= m_currentX
;
1914 m_owner
->GetClientSize( NULL
, &y2
);
1915 m_owner
->ClientToScreen( &x2
, &y2
);
1918 dc
.SetLogicalFunction( wxINVERT
);
1919 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1920 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1924 dc
.DrawLine( x1
, y1
, x2
, y2
);
1926 dc
.SetLogicalFunction( wxCOPY
);
1928 dc
.SetPen( wxNullPen
);
1929 dc
.SetBrush( wxNullBrush
);
1933 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1935 // we want to work with logical coords
1937 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1938 int y
= event
.GetY();
1942 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1944 // we don't draw the line beyond our window, but we allow dragging it
1947 GetClientSize( &w
, NULL
);
1948 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1951 // erase the line if it was drawn
1952 if ( m_currentX
< w
)
1955 if (event
.ButtonUp())
1958 m_isDragging
= false;
1960 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1961 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1968 m_currentX
= m_minX
+ 7;
1970 // draw in the new location
1971 if ( m_currentX
< w
)
1975 else // not dragging
1978 bool hit_border
= false;
1980 // end of the current column
1983 // find the column where this event occurred
1985 countCol
= m_owner
->GetColumnCount();
1986 for (col
= 0; col
< countCol
; col
++)
1988 xpos
+= m_owner
->GetColumnWidth( col
);
1991 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1993 // near the column border
2000 // inside the column
2007 if ( col
== countCol
)
2010 if (event
.LeftDown() || event
.RightUp())
2012 if (hit_border
&& event
.LeftDown())
2014 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2015 event
.GetPosition()) )
2017 m_isDragging
= true;
2022 //else: column resizing was vetoed by the user code
2024 else // click on a column
2026 // record the selected state of the columns
2027 if (event
.LeftDown())
2029 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2032 m_owner
->GetColumn(i
, colItem
);
2033 long state
= colItem
.GetState();
2035 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2037 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2038 m_owner
->SetColumn(i
, colItem
);
2042 SendListEvent( event
.LeftDown()
2043 ? wxEVT_COMMAND_LIST_COL_CLICK
2044 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2045 event
.GetPosition());
2048 else if (event
.Moving())
2053 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2054 m_currentCursor
= m_resizeCursor
;
2058 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2059 m_currentCursor
= wxSTANDARD_CURSOR
;
2063 SetCursor(*m_currentCursor
);
2068 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2070 m_owner
->SetFocus();
2074 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2076 wxWindow
*parent
= GetParent();
2077 wxListEvent
le( type
, parent
->GetId() );
2078 le
.SetEventObject( parent
);
2079 le
.m_pointDrag
= pos
;
2081 // the position should be relative to the parent window, not
2082 // this one for compatibility with MSW and common sense: the
2083 // user code doesn't know anything at all about this header
2084 // window, so why should it get positions relative to it?
2085 le
.m_pointDrag
.y
-= GetSize().y
;
2087 le
.m_col
= m_column
;
2088 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2091 //-----------------------------------------------------------------------------
2092 // wxListRenameTimer (internal)
2093 //-----------------------------------------------------------------------------
2095 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2100 void wxListRenameTimer::Notify()
2102 m_owner
->OnRenameTimer();
2105 //-----------------------------------------------------------------------------
2106 // wxListTextCtrlWrapper (internal)
2107 //-----------------------------------------------------------------------------
2109 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2110 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2111 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2112 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2115 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2118 : m_startValue(owner
->GetItemText(itemEdit
)),
2119 m_itemEdited(itemEdit
)
2124 m_aboutToFinish
= false;
2126 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2128 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2129 &rectLabel
.x
, &rectLabel
.y
);
2131 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2132 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2133 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2136 m_text
->PushEventHandler(this);
2139 void wxListTextCtrlWrapper::Finish()
2145 m_text
->RemoveEventHandler(this);
2146 m_owner
->FinishEditing(m_text
);
2148 wxPendingDelete
.Append( this );
2152 bool wxListTextCtrlWrapper::AcceptChanges()
2154 const wxString value
= m_text
->GetValue();
2156 // notice that we should always call OnRenameAccept() to generate the "end
2157 // label editing" event, even if the user hasn't really changed anything
2158 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2160 // vetoed by the user
2164 // accepted, do rename the item (unless nothing changed)
2165 if ( value
!= m_startValue
)
2166 m_owner
->SetItemText(m_itemEdited
, value
);
2171 void wxListTextCtrlWrapper::AcceptChangesAndFinish()
2173 m_aboutToFinish
= true;
2175 // Notify the owner about the changes
2178 // Even if vetoed, close the control (consistent with MSW)
2182 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2184 switch ( event
.m_keyCode
)
2187 AcceptChangesAndFinish();
2191 m_owner
->OnRenameCancelled( m_itemEdited
);
2200 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2208 // auto-grow the textctrl:
2209 wxSize parentSize
= m_owner
->GetSize();
2210 wxPoint myPos
= m_text
->GetPosition();
2211 wxSize mySize
= m_text
->GetSize();
2213 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2214 if (myPos
.x
+ sx
> parentSize
.x
)
2215 sx
= parentSize
.x
- myPos
.x
;
2218 m_text
->SetSize(sx
, wxDefaultCoord
);
2223 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2225 if ( !m_finished
&& !m_aboutToFinish
)
2227 if ( !AcceptChanges() )
2228 m_owner
->OnRenameCancelled( m_itemEdited
);
2233 // We must let the native text control handle focus
2237 //-----------------------------------------------------------------------------
2239 //-----------------------------------------------------------------------------
2241 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2243 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2244 EVT_PAINT (wxListMainWindow::OnPaint
)
2245 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2246 EVT_CHAR (wxListMainWindow::OnChar
)
2247 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2248 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
2249 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2250 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2251 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2254 void wxListMainWindow::Init()
2259 m_lineTo
= (size_t)-1;
2265 m_small_image_list
= (wxImageList
*) NULL
;
2266 m_normal_image_list
= (wxImageList
*) NULL
;
2268 m_small_spacing
= 30;
2269 m_normal_spacing
= 40;
2273 m_isCreated
= false;
2275 m_lastOnSame
= false;
2276 m_renameTimer
= new wxListRenameTimer( this );
2277 m_textctrlWrapper
= NULL
;
2281 m_lineSelectSingleOnUp
=
2282 m_lineBeforeLastClicked
= (size_t)-1;
2285 wxListMainWindow::wxListMainWindow()
2290 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2293 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2298 const wxString
&name
)
2299 : wxScrolledWindow( parent
, id
, pos
, size
,
2300 style
| wxHSCROLL
| wxVSCROLL
, name
)
2304 m_highlightBrush
= new wxBrush
2306 wxSystemSettings::GetColour
2308 wxSYS_COLOUR_HIGHLIGHT
2313 m_highlightUnfocusedBrush
= new wxBrush
2315 wxSystemSettings::GetColour
2317 wxSYS_COLOUR_BTNSHADOW
2322 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2324 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2325 SetOwnForegroundColour( attr
.colFg
);
2326 SetOwnBackgroundColour( attr
.colBg
);
2328 SetOwnFont( attr
.font
);
2331 wxListMainWindow::~wxListMainWindow()
2334 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2335 WX_CLEAR_ARRAY(m_aColWidths
);
2337 delete m_highlightBrush
;
2338 delete m_highlightUnfocusedBrush
;
2339 delete m_renameTimer
;
2342 void wxListMainWindow::CacheLineData(size_t line
)
2344 wxGenericListCtrl
*listctrl
= GetListCtrl();
2346 wxListLineData
*ld
= GetDummyLine();
2348 size_t countCol
= GetColumnCount();
2349 for ( size_t col
= 0; col
< countCol
; col
++ )
2351 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2352 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2355 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2358 wxListLineData
*wxListMainWindow::GetDummyLine() const
2360 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2361 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2363 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2365 // we need to recreate the dummy line if the number of columns in the
2366 // control changed as it would have the incorrect number of fields
2368 if ( !m_lines
.IsEmpty() &&
2369 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2371 self
->m_lines
.Clear();
2374 if ( m_lines
.IsEmpty() )
2376 wxListLineData
*line
= new wxListLineData(self
);
2377 self
->m_lines
.Add(line
);
2379 // don't waste extra memory -- there never going to be anything
2380 // else/more in this array
2381 self
->m_lines
.Shrink();
2387 // ----------------------------------------------------------------------------
2388 // line geometry (report mode only)
2389 // ----------------------------------------------------------------------------
2391 wxCoord
wxListMainWindow::GetLineHeight() const
2393 // we cache the line height as calling GetTextExtent() is slow
2394 if ( !m_lineHeight
)
2396 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2398 wxClientDC
dc( self
);
2399 dc
.SetFont( GetFont() );
2402 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2404 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2407 m_small_image_list
->GetSize(0, iw
, ih
);
2412 self
->m_lineHeight
= y
+ LINE_SPACING
;
2415 return m_lineHeight
;
2418 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2420 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2422 return LINE_SPACING
+ line
* GetLineHeight();
2425 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2427 if ( !InReportView() )
2428 return GetLine(line
)->m_gi
->m_rectAll
;
2431 rect
.x
= HEADER_OFFSET_X
;
2432 rect
.y
= GetLineY(line
);
2433 rect
.width
= GetHeaderWidth();
2434 rect
.height
= GetLineHeight();
2439 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2441 if ( !InReportView() )
2442 return GetLine(line
)->m_gi
->m_rectLabel
;
2445 wxListLineData
*data
= GetLine(line
);
2446 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
2449 wxListItemData
*item
= node
->GetData();
2450 if ( item
->HasImage() )
2453 GetImageSize( item
->GetImage(), ix
, iy
);
2454 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
2459 rect
.x
= image_x
+ HEADER_OFFSET_X
;
2460 rect
.y
= GetLineY(line
);
2461 rect
.width
= GetColumnWidth(0) - image_x
;
2462 rect
.height
= GetLineHeight();
2467 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2469 if ( !InReportView() )
2470 return GetLine(line
)->m_gi
->m_rectIcon
;
2472 wxListLineData
*ld
= GetLine(line
);
2473 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2476 rect
.x
= HEADER_OFFSET_X
;
2477 rect
.y
= GetLineY(line
);
2478 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2483 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2485 return InReportView() ? GetLineRect(line
)
2486 : GetLine(line
)->m_gi
->m_rectHighlight
;
2489 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2491 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2493 wxListLineData
*ld
= GetLine(line
);
2495 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2496 return wxLIST_HITTEST_ONITEMICON
;
2498 // VS: Testing for "ld->HasText() || InReportView()" instead of
2499 // "ld->HasText()" is needed to make empty lines in report view
2501 if ( ld
->HasText() || InReportView() )
2503 wxRect rect
= InReportView() ? GetLineRect(line
)
2504 : GetLineLabelRect(line
);
2506 if ( rect
.Contains(x
, y
) )
2507 return wxLIST_HITTEST_ONITEMLABEL
;
2513 // ----------------------------------------------------------------------------
2514 // highlight (selection) handling
2515 // ----------------------------------------------------------------------------
2517 bool wxListMainWindow::IsHighlighted(size_t line
) const
2521 return m_selStore
.IsSelected(line
);
2525 wxListLineData
*ld
= GetLine(line
);
2526 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2528 return ld
->IsHighlighted();
2532 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2538 wxArrayInt linesChanged
;
2539 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2542 // meny items changed state, refresh everything
2543 RefreshLines(lineFrom
, lineTo
);
2545 else // only a few items changed state, refresh only them
2547 size_t count
= linesChanged
.GetCount();
2548 for ( size_t n
= 0; n
< count
; n
++ )
2550 RefreshLine(linesChanged
[n
]);
2554 else // iterate over all items in non report view
2556 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2558 if ( HighlightLine(line
, highlight
) )
2564 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2570 changed
= m_selStore
.SelectItem(line
, highlight
);
2574 wxListLineData
*ld
= GetLine(line
);
2575 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2577 changed
= ld
->Highlight(highlight
);
2582 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2583 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2589 void wxListMainWindow::RefreshLine( size_t line
)
2591 if ( InReportView() )
2593 size_t visibleFrom
, visibleTo
;
2594 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2596 if ( line
< visibleFrom
|| line
> visibleTo
)
2600 wxRect rect
= GetLineRect(line
);
2602 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2603 RefreshRect( rect
);
2606 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2608 // we suppose that they are ordered by caller
2609 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2611 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2613 if ( InReportView() )
2615 size_t visibleFrom
, visibleTo
;
2616 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2618 if ( lineFrom
< visibleFrom
)
2619 lineFrom
= visibleFrom
;
2620 if ( lineTo
> visibleTo
)
2625 rect
.y
= GetLineY(lineFrom
);
2626 rect
.width
= GetClientSize().x
;
2627 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2629 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2630 RefreshRect( rect
);
2634 // TODO: this should be optimized...
2635 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2642 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2644 if ( InReportView() )
2646 size_t visibleFrom
, visibleTo
;
2647 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2649 if ( lineFrom
< visibleFrom
)
2650 lineFrom
= visibleFrom
;
2651 else if ( lineFrom
> visibleTo
)
2656 rect
.y
= GetLineY(lineFrom
);
2657 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2659 wxSize size
= GetClientSize();
2660 rect
.width
= size
.x
;
2662 // refresh till the bottom of the window
2663 rect
.height
= size
.y
- rect
.y
;
2665 RefreshRect( rect
);
2669 // TODO: how to do it more efficiently?
2674 void wxListMainWindow::RefreshSelected()
2680 if ( InReportView() )
2682 GetVisibleLinesRange(&from
, &to
);
2687 to
= GetItemCount() - 1;
2690 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2691 RefreshLine(m_current
);
2693 for ( size_t line
= from
; line
<= to
; line
++ )
2695 // NB: the test works as expected even if m_current == -1
2696 if ( line
!= m_current
&& IsHighlighted(line
) )
2701 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2703 // Note: a wxPaintDC must be constructed even if no drawing is
2704 // done (a Windows requirement).
2705 wxPaintDC
dc( this );
2707 if ( IsEmpty() || IsFrozen() )
2709 // nothing to draw or not the moment to draw it
2715 // delay the repainting until we calculate all the items positions
2722 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2724 dc
.SetFont( GetFont() );
2726 if ( InReportView() )
2728 int lineHeight
= GetLineHeight();
2730 size_t visibleFrom
, visibleTo
;
2731 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2734 int xOrig
= dc
.LogicalToDeviceX( 0 );
2735 int yOrig
= dc
.LogicalToDeviceY( 0 );
2737 // tell the caller cache to cache the data
2740 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2741 GetParent()->GetId());
2742 evCache
.SetEventObject( GetParent() );
2743 evCache
.m_oldItemIndex
= visibleFrom
;
2744 evCache
.m_itemIndex
= visibleTo
;
2745 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2748 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2750 rectLine
= GetLineRect(line
);
2753 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2754 rectLine
.width
, rectLine
.height
) )
2756 // don't redraw unaffected lines to avoid flicker
2760 GetLine(line
)->DrawInReportMode( &dc
,
2762 GetLineHighlightRect(line
),
2763 IsHighlighted(line
) );
2766 if ( HasFlag(wxLC_HRULES
) )
2768 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2769 wxSize clientSize
= GetClientSize();
2771 size_t i
= visibleFrom
;
2772 if (i
== 0) i
= 1; // Don't draw the first one
2773 for ( ; i
<= visibleTo
; i
++ )
2776 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2777 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2778 clientSize
.x
- dev_x
, i
* lineHeight
);
2781 // Draw last horizontal rule
2782 if ( visibleTo
== GetItemCount() - 1 )
2785 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2786 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2787 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2791 // Draw vertical rules if required
2792 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2794 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2795 wxRect firstItemRect
, lastItemRect
;
2797 GetItemRect(visibleFrom
, firstItemRect
);
2798 GetItemRect(visibleTo
, lastItemRect
);
2799 int x
= firstItemRect
.GetX();
2801 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2803 for (int col
= 0; col
< GetColumnCount(); col
++)
2805 int colWidth
= GetColumnWidth(col
);
2807 int x_pos
= x
- dev_x
;
2808 if (col
< GetColumnCount()-1) x_pos
-= 2;
2809 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2810 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2816 size_t count
= GetItemCount();
2817 for ( size_t i
= 0; i
< count
; i
++ )
2819 GetLine(i
)->Draw( &dc
);
2824 // Don't draw rect outline under Mac at all.
2829 wxRect
rect( GetLineHighlightRect( m_current
) );
2831 dc
.SetPen( *wxBLACK_PEN
);
2832 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2833 dc
.DrawRectangle( rect
);
2835 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2843 void wxListMainWindow::HighlightAll( bool on
)
2845 if ( IsSingleSel() )
2847 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2849 // we just have one item to turn off
2850 if ( HasCurrent() && IsHighlighted(m_current
) )
2852 HighlightLine(m_current
, false);
2853 RefreshLine(m_current
);
2856 else // multi selection
2859 HighlightLines(0, GetItemCount() - 1, on
);
2863 void wxListMainWindow::SendNotify( size_t line
,
2864 wxEventType command
,
2865 const wxPoint
& point
)
2867 wxListEvent
le( command
, GetParent()->GetId() );
2868 le
.SetEventObject( GetParent() );
2870 le
.m_itemIndex
= line
;
2872 // set only for events which have position
2873 if ( point
!= wxDefaultPosition
)
2874 le
.m_pointDrag
= point
;
2876 // don't try to get the line info for virtual list controls: the main
2877 // program has it anyhow and if we did it would result in accessing all
2878 // the lines, even those which are not visible now and this is precisely
2879 // what we're trying to avoid
2882 if ( line
!= (size_t)-1 )
2884 GetLine(line
)->GetItem( 0, le
.m_item
);
2886 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2888 //else: there may be no more such item
2890 GetParent()->GetEventHandler()->ProcessEvent( le
);
2893 void wxListMainWindow::ChangeCurrent(size_t current
)
2895 m_current
= current
;
2897 // as the current item changed, we shouldn't start editing it when the
2898 // "slow click" timer expires as the click happened on another item
2899 if ( m_renameTimer
->IsRunning() )
2900 m_renameTimer
->Stop();
2902 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2905 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2907 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2908 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2910 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2911 wxT("EditLabel() needs a text control") );
2913 size_t itemEdit
= (size_t)item
;
2915 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2916 le
.SetEventObject( GetParent() );
2917 le
.m_itemIndex
= item
;
2918 wxListLineData
*data
= GetLine(itemEdit
);
2919 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2920 data
->GetItem( 0, le
.m_item
);
2922 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2924 // vetoed by user code
2928 // We have to call this here because the label in question might just have
2929 // been added and no screen update taken place.
2934 // Pending events dispatched by wxSafeYield might have changed the item
2936 if ( (size_t)item
>= GetItemCount() )
2940 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2941 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2942 return m_textctrlWrapper
->GetText();
2945 void wxListMainWindow::OnRenameTimer()
2947 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2949 EditLabel( m_current
);
2952 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2954 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2955 le
.SetEventObject( GetParent() );
2956 le
.m_itemIndex
= itemEdit
;
2958 wxListLineData
*data
= GetLine(itemEdit
);
2960 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2962 data
->GetItem( 0, le
.m_item
);
2963 le
.m_item
.m_text
= value
;
2964 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2968 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2970 // let owner know that the edit was cancelled
2971 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2973 le
.SetEditCanceled(true);
2975 le
.SetEventObject( GetParent() );
2976 le
.m_itemIndex
= itemEdit
;
2978 wxListLineData
*data
= GetLine(itemEdit
);
2979 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2981 data
->GetItem( 0, le
.m_item
);
2982 GetEventHandler()->ProcessEvent( le
);
2985 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2989 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2990 // shutdown the edit control when the mouse is clicked elsewhere on the
2991 // listctrl because the order of events is different (or something like
2992 // that), so explicitly end the edit if it is active.
2993 if ( event
.LeftDown() && m_textctrlWrapper
)
2994 m_textctrlWrapper
->AcceptChangesAndFinish();
2997 if ( event
.LeftDown() )
2998 SetFocusIgnoringChildren();
3000 event
.SetEventObject( GetParent() );
3001 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3004 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3006 // let the base handle mouse wheel events.
3011 if ( !HasCurrent() || IsEmpty() )
3013 if (event
.RightDown())
3015 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3017 wxContextMenuEvent
evtCtx(
3019 GetParent()->GetId(),
3020 ClientToScreen(event
.GetPosition()));
3021 evtCtx
.SetEventObject(GetParent());
3022 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3030 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3031 event
.ButtonDClick()) )
3034 int x
= event
.GetX();
3035 int y
= event
.GetY();
3036 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3038 // where did we hit it (if we did)?
3041 size_t count
= GetItemCount(),
3044 if ( InReportView() )
3046 current
= y
/ GetLineHeight();
3047 if ( current
< count
)
3048 hitResult
= HitTestLine(current
, x
, y
);
3052 // TODO: optimize it too! this is less simple than for report view but
3053 // enumerating all items is still not a way to do it!!
3054 for ( current
= 0; current
< count
; current
++ )
3056 hitResult
= HitTestLine(current
, x
, y
);
3062 if (event
.Dragging())
3064 if (m_dragCount
== 0)
3066 // we have to report the raw, physical coords as we want to be
3067 // able to call HitTest(event.m_pointDrag) from the user code to
3068 // get the item being dragged
3069 m_dragStart
= event
.GetPosition();
3074 if (m_dragCount
!= 3)
3077 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3078 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3080 wxListEvent
le( command
, GetParent()->GetId() );
3081 le
.SetEventObject( GetParent() );
3082 le
.m_itemIndex
= m_lineLastClicked
;
3083 le
.m_pointDrag
= m_dragStart
;
3084 GetParent()->GetEventHandler()->ProcessEvent( le
);
3095 // outside of any item
3096 if (event
.RightDown())
3098 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3100 wxContextMenuEvent
evtCtx(
3102 GetParent()->GetId(),
3103 ClientToScreen(event
.GetPosition()));
3104 evtCtx
.SetEventObject(GetParent());
3105 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3109 // reset the selection and bail out
3110 HighlightAll(false);
3116 bool forceClick
= false;
3117 if (event
.ButtonDClick())
3119 if ( m_renameTimer
->IsRunning() )
3120 m_renameTimer
->Stop();
3122 m_lastOnSame
= false;
3124 if ( current
== m_lineLastClicked
)
3126 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3132 // The first click was on another item, so don't interpret this as
3133 // a double click, but as a simple click instead
3140 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3142 // select single line
3143 HighlightAll( false );
3144 ReverseHighlight(m_lineSelectSingleOnUp
);
3149 if ((current
== m_current
) &&
3150 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3151 HasFlag(wxLC_EDIT_LABELS
) )
3153 if ( !InReportView() ||
3154 GetLineLabelRect(current
).Contains(x
, y
) )
3156 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
3157 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
3162 m_lastOnSame
= false;
3163 m_lineSelectSingleOnUp
= (size_t)-1;
3167 // This is necessary, because after a DnD operation in
3168 // from and to ourself, the up event is swallowed by the
3169 // DnD code. So on next non-up event (which means here and
3170 // now) m_lineSelectSingleOnUp should be reset.
3171 m_lineSelectSingleOnUp
= (size_t)-1;
3173 if (event
.RightDown())
3175 m_lineBeforeLastClicked
= m_lineLastClicked
;
3176 m_lineLastClicked
= current
;
3178 // If the item is already selected, do not update the selection.
3179 // Multi-selections should not be cleared if a selected item is clicked.
3180 if (!IsHighlighted(current
))
3182 HighlightAll(false);
3183 ChangeCurrent(current
);
3184 ReverseHighlight(m_current
);
3187 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3189 // Allow generation of context menu event
3192 else if (event
.MiddleDown())
3194 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3196 else if ( event
.LeftDown() || forceClick
)
3198 m_lineBeforeLastClicked
= m_lineLastClicked
;
3199 m_lineLastClicked
= current
;
3201 size_t oldCurrent
= m_current
;
3202 bool oldWasSelected
= IsHighlighted(m_current
);
3204 bool cmdModifierDown
= event
.CmdDown();
3205 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3207 if ( IsSingleSel() || !IsHighlighted(current
) )
3209 HighlightAll( false );
3211 ChangeCurrent(current
);
3213 ReverseHighlight(m_current
);
3215 else // multi sel & current is highlighted & no mod keys
3217 m_lineSelectSingleOnUp
= current
;
3218 ChangeCurrent(current
); // change focus
3221 else // multi sel & either ctrl or shift is down
3223 if (cmdModifierDown
)
3225 ChangeCurrent(current
);
3227 ReverseHighlight(m_current
);
3229 else if (event
.ShiftDown())
3231 ChangeCurrent(current
);
3233 size_t lineFrom
= oldCurrent
,
3236 if ( lineTo
< lineFrom
)
3239 lineFrom
= m_current
;
3242 HighlightLines(lineFrom
, lineTo
);
3244 else // !ctrl, !shift
3246 // test in the enclosing if should make it impossible
3247 wxFAIL_MSG( _T("how did we get here?") );
3251 if (m_current
!= oldCurrent
)
3252 RefreshLine( oldCurrent
);
3254 // forceClick is only set if the previous click was on another item
3255 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3259 void wxListMainWindow::MoveToItem(size_t item
)
3261 if ( item
== (size_t)-1 )
3264 wxRect rect
= GetLineRect(item
);
3266 int client_w
, client_h
;
3267 GetClientSize( &client_w
, &client_h
);
3269 const int hLine
= GetLineHeight();
3271 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3272 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3274 if ( InReportView() )
3276 // the next we need the range of lines shown it might be different,
3277 // so recalculate it
3278 ResetVisibleLinesRange();
3280 if (rect
.y
< view_y
)
3281 Scroll( -1, rect
.y
/ hLine
);
3282 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3283 Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3286 // At least on Mac the visible lines value will get reset inside of
3287 // Scroll *before* it actually scrolls the window because of the
3288 // Update() that happens there, so it will still have the wrong value.
3289 // So let's reset it again and wait for it to be recalculated in the
3290 // next paint event. I would expect this problem to show up in wxGTK
3291 // too but couldn't duplicate it there. Perhaps the order of events
3292 // is different... --Robin
3293 ResetVisibleLinesRange();
3301 if (rect
.x
-view_x
< 5)
3302 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
3303 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3304 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
3306 if (rect
.y
-view_y
< 5)
3307 sy
= (rect
.y
- 5) / hLine
;
3308 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
3309 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
3315 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
3317 if ( !InReportView() )
3319 // TODO: this should work in all views but is not implemented now
3324 GetVisibleLinesRange(&top
, &bottom
);
3326 if ( bottom
== (size_t)-1 )
3329 ResetVisibleLinesRange();
3331 int hLine
= GetLineHeight();
3333 Scroll(-1, top
+ dy
/ hLine
);
3336 // see comment in MoveToItem() for why we do this
3337 ResetVisibleLinesRange();
3343 // ----------------------------------------------------------------------------
3344 // keyboard handling
3345 // ----------------------------------------------------------------------------
3347 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3349 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3350 _T("invalid item index in OnArrowChar()") );
3352 size_t oldCurrent
= m_current
;
3354 // in single selection we just ignore Shift as we can't select several
3356 if ( event
.ShiftDown() && !IsSingleSel() )
3358 ChangeCurrent(newCurrent
);
3360 // refresh the old focus to remove it
3361 RefreshLine( oldCurrent
);
3363 // select all the items between the old and the new one
3364 if ( oldCurrent
> newCurrent
)
3366 newCurrent
= oldCurrent
;
3367 oldCurrent
= m_current
;
3370 HighlightLines(oldCurrent
, newCurrent
);
3374 // all previously selected items are unselected unless ctrl is held
3375 // in a multiselection control
3376 if ( !event
.ControlDown() || IsSingleSel() )
3377 HighlightAll(false);
3379 ChangeCurrent(newCurrent
);
3381 // refresh the old focus to remove it
3382 RefreshLine( oldCurrent
);
3384 // in single selection mode we must always have a selected item
3385 if ( !event
.ControlDown() || IsSingleSel() )
3386 HighlightLine( m_current
, true );
3389 RefreshLine( m_current
);
3394 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3396 wxWindow
*parent
= GetParent();
3398 // propagate the key event upwards
3399 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3400 ke
.m_shiftDown
= event
.m_shiftDown
;
3401 ke
.m_controlDown
= event
.m_controlDown
;
3402 ke
.m_altDown
= event
.m_altDown
;
3403 ke
.m_metaDown
= event
.m_metaDown
;
3404 ke
.m_keyCode
= event
.m_keyCode
;
3407 ke
.SetEventObject( parent
);
3408 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3413 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3415 wxWindow
*parent
= GetParent();
3417 // propagate the key event upwards
3418 wxKeyEvent
ke( wxEVT_KEY_UP
);
3419 ke
.m_shiftDown
= event
.m_shiftDown
;
3420 ke
.m_controlDown
= event
.m_controlDown
;
3421 ke
.m_altDown
= event
.m_altDown
;
3422 ke
.m_metaDown
= event
.m_metaDown
;
3423 ke
.m_keyCode
= event
.m_keyCode
;
3426 ke
.SetEventObject( parent
);
3427 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3432 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3434 wxWindow
*parent
= GetParent();
3436 // send a list_key event up
3439 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3440 le
.m_itemIndex
= m_current
;
3441 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3442 le
.m_code
= event
.GetKeyCode();
3443 le
.SetEventObject( parent
);
3444 parent
->GetEventHandler()->ProcessEvent( le
);
3447 // propagate the char event upwards
3448 wxKeyEvent
ke( wxEVT_CHAR
);
3449 ke
.m_shiftDown
= event
.m_shiftDown
;
3450 ke
.m_controlDown
= event
.m_controlDown
;
3451 ke
.m_altDown
= event
.m_altDown
;
3452 ke
.m_metaDown
= event
.m_metaDown
;
3453 ke
.m_keyCode
= event
.m_keyCode
;
3456 ke
.SetEventObject( parent
);
3457 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3459 if (event
.GetKeyCode() == WXK_TAB
)
3461 wxNavigationKeyEvent nevent
;
3462 nevent
.SetWindowChange( event
.ControlDown() );
3463 nevent
.SetDirection( !event
.ShiftDown() );
3464 nevent
.SetEventObject( GetParent()->GetParent() );
3465 nevent
.SetCurrentFocus( m_parent
);
3466 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3470 // no item -> nothing to do
3477 // don't use m_linesPerPage directly as it might not be computed yet
3478 const int pageSize
= GetCountPerPage();
3479 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3481 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3483 if (event
.GetKeyCode() == WXK_RIGHT
)
3484 event
.m_keyCode
= WXK_LEFT
;
3485 else if (event
.GetKeyCode() == WXK_LEFT
)
3486 event
.m_keyCode
= WXK_RIGHT
;
3489 switch ( event
.GetKeyCode() )
3492 if ( m_current
> 0 )
3493 OnArrowChar( m_current
- 1, event
);
3497 if ( m_current
< (size_t)GetItemCount() - 1 )
3498 OnArrowChar( m_current
+ 1, event
);
3503 OnArrowChar( GetItemCount() - 1, event
);
3508 OnArrowChar( 0, event
);
3513 int steps
= InReportView() ? pageSize
- 1
3514 : m_current
% pageSize
;
3516 int index
= m_current
- steps
;
3520 OnArrowChar( index
, event
);
3526 int steps
= InReportView()
3528 : pageSize
- (m_current
% pageSize
) - 1;
3530 size_t index
= m_current
+ steps
;
3531 size_t count
= GetItemCount();
3532 if ( index
>= count
)
3535 OnArrowChar( index
, event
);
3540 if ( !InReportView() )
3542 int index
= m_current
- pageSize
;
3546 OnArrowChar( index
, event
);
3551 if ( !InReportView() )
3553 size_t index
= m_current
+ pageSize
;
3555 size_t count
= GetItemCount();
3556 if ( index
>= count
)
3559 OnArrowChar( index
, event
);
3564 if ( IsSingleSel() )
3566 if ( event
.ControlDown() )
3568 ReverseHighlight(m_current
);
3570 else // normal space press
3572 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3575 else // multiple selection
3577 ReverseHighlight(m_current
);
3583 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3591 // ----------------------------------------------------------------------------
3593 // ----------------------------------------------------------------------------
3595 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3599 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3600 event
.SetEventObject( GetParent() );
3601 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3605 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3606 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3607 // which are already drawn correctly resulting in horrible flicker - avoid
3617 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3621 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3622 event
.SetEventObject( GetParent() );
3623 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3631 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3633 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3635 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3637 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3639 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3641 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3643 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3645 else if ( InReportView() && (m_small_image_list
))
3647 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3651 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3653 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3655 m_normal_image_list
->GetSize( index
, width
, height
);
3657 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3659 m_small_image_list
->GetSize( index
, width
, height
);
3661 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3663 m_small_image_list
->GetSize( index
, width
, height
);
3665 else if ( InReportView() && m_small_image_list
)
3667 m_small_image_list
->GetSize( index
, width
, height
);
3676 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3678 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3679 dc
.SetFont( GetFont() );
3682 dc
.GetTextExtent( s
, &lw
, NULL
);
3684 return lw
+ AUTOSIZE_COL_MARGIN
;
3687 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3691 // calc the spacing from the icon size
3692 int width
= 0, height
= 0;
3694 if ((imageList
) && (imageList
->GetImageCount()) )
3695 imageList
->GetSize(0, width
, height
);
3697 if (which
== wxIMAGE_LIST_NORMAL
)
3699 m_normal_image_list
= imageList
;
3700 m_normal_spacing
= width
+ 8;
3703 if (which
== wxIMAGE_LIST_SMALL
)
3705 m_small_image_list
= imageList
;
3706 m_small_spacing
= width
+ 14;
3707 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3711 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3715 m_small_spacing
= spacing
;
3717 m_normal_spacing
= spacing
;
3720 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3722 return isSmall
? m_small_spacing
: m_normal_spacing
;
3725 // ----------------------------------------------------------------------------
3727 // ----------------------------------------------------------------------------
3729 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3731 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3733 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3735 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3736 item
.m_width
= GetTextLength( item
.m_text
);
3738 wxListHeaderData
*column
= node
->GetData();
3739 column
->SetItem( item
);
3741 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3743 headerWin
->m_dirty
= true;
3747 // invalidate it as it has to be recalculated
3751 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3753 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3754 _T("invalid column index") );
3756 wxCHECK_RET( InReportView(),
3757 _T("SetColumnWidth() can only be called in report mode.") );
3760 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3762 headerWin
->m_dirty
= true;
3764 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3765 wxCHECK_RET( node
, _T("no column?") );
3767 wxListHeaderData
*column
= node
->GetData();
3769 size_t count
= GetItemCount();
3771 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3773 width
= GetTextLength(column
->GetText());
3774 width
+= 2*EXTRA_WIDTH
;
3776 // check for column header's image availability
3777 const int image
= column
->GetImage();
3780 if ( m_small_image_list
)
3783 m_small_image_list
->GetSize(image
, ix
, iy
);
3784 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3788 else if ( width
== wxLIST_AUTOSIZE
)
3792 // TODO: determine the max width somehow...
3793 width
= WIDTH_COL_DEFAULT
;
3797 wxClientDC
dc(this);
3798 dc
.SetFont( GetFont() );
3800 int max
= AUTOSIZE_COL_MARGIN
;
3802 // if the cached column width isn't valid then recalculate it
3803 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3805 for (size_t i
= 0; i
< count
; i
++)
3807 wxListLineData
*line
= GetLine( i
);
3808 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3810 wxCHECK_RET( n
, _T("no subitem?") );
3812 wxListItemData
*itemData
= n
->GetData();
3815 itemData
->GetItem(item
);
3816 int itemWidth
= GetItemWidthWithImage(&item
);
3817 if (itemWidth
> max
)
3821 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3822 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3825 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3826 width
= max
+ AUTOSIZE_COL_MARGIN
;
3830 column
->SetWidth( width
);
3832 // invalidate it as it has to be recalculated
3836 int wxListMainWindow::GetHeaderWidth() const
3838 if ( !m_headerWidth
)
3840 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3842 size_t count
= GetColumnCount();
3843 for ( size_t col
= 0; col
< count
; col
++ )
3845 self
->m_headerWidth
+= GetColumnWidth(col
);
3849 return m_headerWidth
;
3852 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3854 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3855 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3857 wxListHeaderData
*column
= node
->GetData();
3858 column
->GetItem( item
);
3861 int wxListMainWindow::GetColumnWidth( int col
) const
3863 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3864 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3866 wxListHeaderData
*column
= node
->GetData();
3867 return column
->GetWidth();
3870 // ----------------------------------------------------------------------------
3872 // ----------------------------------------------------------------------------
3874 void wxListMainWindow::SetItem( wxListItem
&item
)
3876 long id
= item
.m_itemId
;
3877 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3878 _T("invalid item index in SetItem") );
3882 wxListLineData
*line
= GetLine((size_t)id
);
3883 line
->SetItem( item
.m_col
, item
);
3885 // Set item state if user wants
3886 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3887 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3891 // update the Max Width Cache if needed
3892 int width
= GetItemWidthWithImage(&item
);
3894 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3895 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3899 // update the item on screen
3901 GetItemRect(id
, rectItem
);
3902 RefreshRect(rectItem
);
3905 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3910 // first deal with selection
3911 if ( stateMask
& wxLIST_STATE_SELECTED
)
3913 // set/clear select state
3916 // optimized version for virtual listctrl.
3917 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3920 else if ( state
& wxLIST_STATE_SELECTED
)
3922 const long count
= GetItemCount();
3923 for( long i
= 0; i
< count
; i
++ )
3925 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3931 // clear for non virtual (somewhat optimized by using GetNextItem())
3933 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3935 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3940 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3942 // unfocus all: only one item can be focussed, so clearing focus for
3943 // all items is simply clearing focus of the focussed item.
3944 SetItemState(m_current
, state
, stateMask
);
3946 //(setting focus to all items makes no sense, so it is not handled here.)
3949 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3953 SetItemStateAll(state
, stateMask
);
3957 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3958 _T("invalid list ctrl item index in SetItem") );
3960 size_t oldCurrent
= m_current
;
3961 size_t item
= (size_t)litem
; // safe because of the check above
3963 // do we need to change the focus?
3964 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3966 if ( state
& wxLIST_STATE_FOCUSED
)
3968 // don't do anything if this item is already focused
3969 if ( item
!= m_current
)
3971 ChangeCurrent(item
);
3973 if ( oldCurrent
!= (size_t)-1 )
3975 if ( IsSingleSel() )
3977 HighlightLine(oldCurrent
, false);
3980 RefreshLine(oldCurrent
);
3983 RefreshLine( m_current
);
3988 // don't do anything if this item is not focused
3989 if ( item
== m_current
)
3993 if ( IsSingleSel() )
3995 // we must unselect the old current item as well or we
3996 // might end up with more than one selected item in a
3997 // single selection control
3998 HighlightLine(oldCurrent
, false);
4001 RefreshLine( oldCurrent
);
4006 // do we need to change the selection state?
4007 if ( stateMask
& wxLIST_STATE_SELECTED
)
4009 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
4011 if ( IsSingleSel() )
4015 // selecting the item also makes it the focused one in the
4017 if ( m_current
!= item
)
4019 ChangeCurrent(item
);
4021 if ( oldCurrent
!= (size_t)-1 )
4023 HighlightLine( oldCurrent
, false );
4024 RefreshLine( oldCurrent
);
4030 // only the current item may be selected anyhow
4031 if ( item
!= m_current
)
4036 if ( HighlightLine(item
, on
) )
4043 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
4045 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
4046 _T("invalid list ctrl item index in GetItemState()") );
4048 int ret
= wxLIST_STATE_DONTCARE
;
4050 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4052 if ( (size_t)item
== m_current
)
4053 ret
|= wxLIST_STATE_FOCUSED
;
4056 if ( stateMask
& wxLIST_STATE_SELECTED
)
4058 if ( IsHighlighted(item
) )
4059 ret
|= wxLIST_STATE_SELECTED
;
4065 void wxListMainWindow::GetItem( wxListItem
&item
) const
4067 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
4068 _T("invalid item index in GetItem") );
4070 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4071 line
->GetItem( item
.m_col
, item
);
4073 // Get item state if user wants it
4074 if ( item
.m_mask
& wxLIST_MASK_STATE
)
4075 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
4076 wxLIST_STATE_FOCUSED
);
4079 // ----------------------------------------------------------------------------
4081 // ----------------------------------------------------------------------------
4083 size_t wxListMainWindow::GetItemCount() const
4085 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4088 void wxListMainWindow::SetItemCount(long count
)
4090 m_selStore
.SetItemCount(count
);
4091 m_countVirt
= count
;
4093 ResetVisibleLinesRange();
4095 // scrollbars must be reset
4099 int wxListMainWindow::GetSelectedItemCount() const
4101 // deal with the quick case first
4102 if ( IsSingleSel() )
4103 return HasCurrent() ? IsHighlighted(m_current
) : false;
4105 // virtual controls remmebers all its selections itself
4107 return m_selStore
.GetSelectedCount();
4109 // TODO: we probably should maintain the number of items selected even for
4110 // non virtual controls as enumerating all lines is really slow...
4111 size_t countSel
= 0;
4112 size_t count
= GetItemCount();
4113 for ( size_t line
= 0; line
< count
; line
++ )
4115 if ( GetLine(line
)->IsHighlighted() )
4122 // ----------------------------------------------------------------------------
4123 // item position/size
4124 // ----------------------------------------------------------------------------
4126 wxRect
wxListMainWindow::GetViewRect() const
4128 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
4129 _T("wxListCtrl::GetViewRect() only works in icon mode") );
4131 // we need to find the longest/tallest label
4132 wxCoord xMax
= 0, yMax
= 0;
4133 const int count
= GetItemCount();
4136 for ( int i
= 0; i
< count
; i
++ )
4141 wxCoord x
= r
.GetRight(),
4151 // some fudge needed to make it look prettier
4152 xMax
+= 2 * EXTRA_BORDER_X
;
4153 yMax
+= 2 * EXTRA_BORDER_Y
;
4155 // account for the scrollbars if necessary
4156 const wxSize sizeAll
= GetClientSize();
4157 if ( xMax
> sizeAll
.x
)
4158 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4159 if ( yMax
> sizeAll
.y
)
4160 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4162 return wxRect(0, 0, xMax
, yMax
);
4165 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
4167 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4168 _T("invalid index in GetItemRect") );
4170 // ensure that we're laid out, otherwise we could return nonsense
4173 wxConstCast(this, wxListMainWindow
)->
4174 RecalculatePositions(true /* no refresh */);
4177 rect
= GetLineRect((size_t)index
);
4179 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4182 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4185 GetItemRect(item
, rect
);
4193 // ----------------------------------------------------------------------------
4194 // geometry calculation
4195 // ----------------------------------------------------------------------------
4197 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4199 const int lineHeight
= GetLineHeight();
4201 wxClientDC
dc( this );
4202 dc
.SetFont( GetFont() );
4204 const size_t count
= GetItemCount();
4207 if ( HasFlag(wxLC_ICON
) )
4208 iconSpacing
= m_normal_spacing
;
4209 else if ( HasFlag(wxLC_SMALL_ICON
) )
4210 iconSpacing
= m_small_spacing
;
4214 // Note that we do not call GetClientSize() here but
4215 // GetSize() and subtract the border size for sunken
4216 // borders manually. This is technically incorrect,
4217 // but we need to know the client area's size WITHOUT
4218 // scrollbars here. Since we don't know if there are
4219 // any scrollbars, we use GetSize() instead. Another
4220 // solution would be to call SetScrollbars() here to
4221 // remove the scrollbars and call GetClientSize() then,
4222 // but this might result in flicker and - worse - will
4223 // reset the scrollbars to 0 which is not good at all
4224 // if you resize a dialog/window, but don't want to
4225 // reset the window scrolling. RR.
4226 // Furthermore, we actually do NOT subtract the border
4227 // width as 2 pixels is just the extra space which we
4228 // need around the actual content in the window. Other-
4229 // wise the text would e.g. touch the upper border. RR.
4232 GetSize( &clientWidth
, &clientHeight
);
4234 if ( InReportView() )
4236 // all lines have the same height and we scroll one line per step
4237 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4239 m_linesPerPage
= clientHeight
/ lineHeight
;
4241 ResetVisibleLinesRange();
4243 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4244 GetHeaderWidth() / SCROLL_UNIT_X
,
4245 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4246 GetScrollPos(wxHORIZONTAL
),
4247 GetScrollPos(wxVERTICAL
),
4252 // we have 3 different layout strategies: either layout all items
4253 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4254 // to arrange them in top to bottom, left to right (don't ask me why
4255 // not the other way round...) order
4256 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4258 int x
= EXTRA_BORDER_X
;
4259 int y
= EXTRA_BORDER_Y
;
4261 wxCoord widthMax
= 0;
4264 for ( i
= 0; i
< count
; i
++ )
4266 wxListLineData
*line
= GetLine(i
);
4267 line
->CalculateSize( &dc
, iconSpacing
);
4268 line
->SetPosition( x
, y
, iconSpacing
);
4270 wxSize sizeLine
= GetLineSize(i
);
4272 if ( HasFlag(wxLC_ALIGN_TOP
) )
4274 if ( sizeLine
.x
> widthMax
)
4275 widthMax
= sizeLine
.x
;
4279 else // wxLC_ALIGN_LEFT
4281 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4285 if ( HasFlag(wxLC_ALIGN_TOP
) )
4287 // traverse the items again and tweak their sizes so that they are
4288 // all the same in a row
4289 for ( i
= 0; i
< count
; i
++ )
4291 wxListLineData
*line
= GetLine(i
);
4292 line
->m_gi
->ExtendWidth(widthMax
);
4300 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4301 (y
+ lineHeight
) / lineHeight
,
4302 GetScrollPos( wxHORIZONTAL
),
4303 GetScrollPos( wxVERTICAL
),
4307 else // "flowed" arrangement, the most complicated case
4309 // at first we try without any scrollbars, if the items don't fit into
4310 // the window, we recalculate after subtracting the space taken by the
4313 int entireWidth
= 0;
4315 for (int tries
= 0; tries
< 2; tries
++)
4317 entireWidth
= 2 * EXTRA_BORDER_X
;
4321 // Now we have decided that the items do not fit into the
4322 // client area, so we need a scrollbar
4323 entireWidth
+= SCROLL_UNIT_X
;
4326 int x
= EXTRA_BORDER_X
;
4327 int y
= EXTRA_BORDER_Y
;
4328 int maxWidthInThisRow
= 0;
4331 int currentlyVisibleLines
= 0;
4333 for (size_t i
= 0; i
< count
; i
++)
4335 currentlyVisibleLines
++;
4336 wxListLineData
*line
= GetLine( i
);
4337 line
->CalculateSize( &dc
, iconSpacing
);
4338 line
->SetPosition( x
, y
, iconSpacing
);
4340 wxSize sizeLine
= GetLineSize( i
);
4342 if ( maxWidthInThisRow
< sizeLine
.x
)
4343 maxWidthInThisRow
= sizeLine
.x
;
4346 if (currentlyVisibleLines
> m_linesPerPage
)
4347 m_linesPerPage
= currentlyVisibleLines
;
4349 if ( y
+ sizeLine
.y
>= clientHeight
)
4351 currentlyVisibleLines
= 0;
4353 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4354 x
+= maxWidthInThisRow
;
4355 entireWidth
+= maxWidthInThisRow
;
4356 maxWidthInThisRow
= 0;
4359 // We have reached the last item.
4360 if ( i
== count
- 1 )
4361 entireWidth
+= maxWidthInThisRow
;
4363 if ( (tries
== 0) &&
4364 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4366 clientHeight
-= wxSystemSettings::
4367 GetMetric(wxSYS_HSCROLL_Y
);
4372 if ( i
== count
- 1 )
4373 tries
= 1; // Everything fits, no second try required.
4381 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4383 GetScrollPos( wxHORIZONTAL
),
4392 // FIXME: why should we call it from here?
4399 void wxListMainWindow::RefreshAll()
4404 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4405 if ( headerWin
&& headerWin
->m_dirty
)
4407 headerWin
->m_dirty
= false;
4408 headerWin
->Refresh();
4412 void wxListMainWindow::UpdateCurrent()
4414 if ( !HasCurrent() && !IsEmpty() )
4418 long wxListMainWindow::GetNextItem( long item
,
4419 int WXUNUSED(geometry
),
4423 max
= GetItemCount();
4424 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4425 _T("invalid listctrl index in GetNextItem()") );
4427 // notice that we start with the next item (or the first one if item == -1)
4428 // and this is intentional to allow writing a simple loop to iterate over
4429 // all selected items
4432 // this is not an error because the index was OK initially,
4433 // just no such item
4440 size_t count
= GetItemCount();
4441 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4443 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4446 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4453 // ----------------------------------------------------------------------------
4455 // ----------------------------------------------------------------------------
4457 void wxListMainWindow::DeleteItem( long lindex
)
4459 size_t count
= GetItemCount();
4461 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4462 _T("invalid item index in DeleteItem") );
4464 size_t index
= (size_t)lindex
;
4466 // we don't need to adjust the index for the previous items
4467 if ( HasCurrent() && m_current
>= index
)
4469 // if the current item is being deleted, we want the next one to
4470 // become selected - unless there is no next one - so don't adjust
4471 // m_current in this case
4472 if ( m_current
!= index
|| m_current
== count
- 1 )
4476 if ( InReportView() )
4478 // mark the Column Max Width cache as dirty if the items in the line
4479 // we're deleting contain the Max Column Width
4480 wxListLineData
* const line
= GetLine(index
);
4481 wxListItemDataList::compatibility_iterator n
;
4482 wxListItemData
*itemData
;
4486 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4488 n
= line
->m_items
.Item( i
);
4489 itemData
= n
->GetData();
4490 itemData
->GetItem(item
);
4492 itemWidth
= GetItemWidthWithImage(&item
);
4494 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4495 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4498 ResetVisibleLinesRange();
4501 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4506 m_selStore
.OnItemDelete(index
);
4510 m_lines
.RemoveAt( index
);
4513 // we need to refresh the (vert) scrollbar as the number of items changed
4516 RefreshAfter(index
);
4519 void wxListMainWindow::DeleteColumn( int col
)
4521 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4523 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4526 delete node
->GetData();
4527 m_columns
.Erase( node
);
4531 // update all the items
4532 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4534 wxListLineData
* const line
= GetLine(i
);
4535 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4536 delete n
->GetData();
4537 line
->m_items
.Erase(n
);
4541 if ( InReportView() ) // we only cache max widths when in Report View
4543 delete m_aColWidths
.Item(col
);
4544 m_aColWidths
.RemoveAt(col
);
4547 // invalidate it as it has to be recalculated
4551 void wxListMainWindow::DoDeleteAllItems()
4554 // nothing to do - in particular, don't send the event
4559 // to make the deletion of all items faster, we don't send the
4560 // notifications for each item deletion in this case but only one event
4561 // for all of them: this is compatible with wxMSW and documented in
4562 // DeleteAllItems() description
4564 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4565 event
.SetEventObject( GetParent() );
4566 GetParent()->GetEventHandler()->ProcessEvent( event
);
4574 if ( InReportView() )
4576 ResetVisibleLinesRange();
4577 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4579 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4586 void wxListMainWindow::DeleteAllItems()
4590 RecalculatePositions();
4593 void wxListMainWindow::DeleteEverything()
4595 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4596 WX_CLEAR_ARRAY(m_aColWidths
);
4601 // ----------------------------------------------------------------------------
4602 // scanning for an item
4603 // ----------------------------------------------------------------------------
4605 void wxListMainWindow::EnsureVisible( long index
)
4607 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4608 _T("invalid index in EnsureVisible") );
4610 // We have to call this here because the label in question might just have
4611 // been added and its position is not known yet
4613 RecalculatePositions(true /* no refresh */);
4615 MoveToItem((size_t)index
);
4618 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4624 wxString str_upper
= str
.Upper();
4628 size_t count
= GetItemCount();
4629 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4631 wxListLineData
*line
= GetLine(i
);
4632 wxString line_upper
= line
->GetText(0).Upper();
4635 if (line_upper
== str_upper
)
4640 if (line_upper
.find(str_upper
) == 0)
4648 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4654 size_t count
= GetItemCount();
4655 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4657 wxListLineData
*line
= GetLine(i
);
4659 line
->GetItem( 0, item
);
4660 if (item
.m_data
== data
)
4667 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4670 GetVisibleLinesRange( &topItem
, NULL
);
4673 GetItemPosition( GetItemCount() - 1, p
);
4677 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4678 if ( id
>= 0 && id
< (long)GetItemCount() )
4684 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4686 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4688 size_t count
= GetItemCount();
4690 if ( InReportView() )
4692 size_t current
= y
/ GetLineHeight();
4693 if ( current
< count
)
4695 flags
= HitTestLine(current
, x
, y
);
4702 // TODO: optimize it too! this is less simple than for report view but
4703 // enumerating all items is still not a way to do it!!
4704 for ( size_t current
= 0; current
< count
; current
++ )
4706 flags
= HitTestLine(current
, x
, y
);
4715 // ----------------------------------------------------------------------------
4717 // ----------------------------------------------------------------------------
4719 void wxListMainWindow::InsertItem( wxListItem
&item
)
4721 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4723 int count
= GetItemCount();
4724 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4726 if (item
.m_itemId
> count
)
4727 item
.m_itemId
= count
;
4729 size_t id
= item
.m_itemId
;
4733 if ( InReportView() )
4735 ResetVisibleLinesRange();
4737 // calculate the width of the item and adjust the max column width
4738 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4739 int width
= GetItemWidthWithImage(&item
);
4740 item
.SetWidth(width
);
4741 if (width
> pWidthInfo
->nMaxWidth
)
4742 pWidthInfo
->nMaxWidth
= width
;
4745 wxListLineData
*line
= new wxListLineData(this);
4747 line
->SetItem( item
.m_col
, item
);
4749 m_lines
.Insert( line
, id
);
4753 // If an item is selected at or below the point of insertion, we need to
4754 // increment the member variables because the current row's index has gone
4756 if ( HasCurrent() && m_current
>= id
)
4759 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4761 RefreshLines(id
, GetItemCount() - 1);
4764 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4767 if ( InReportView() )
4769 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4770 item
.m_width
= GetTextLength( item
.m_text
);
4772 wxListHeaderData
*column
= new wxListHeaderData( item
);
4773 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4775 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4778 wxListHeaderDataList::compatibility_iterator
4779 node
= m_columns
.Item( col
);
4780 m_columns
.Insert( node
, column
);
4781 m_aColWidths
.Insert( colWidthInfo
, col
);
4785 m_columns
.Append( column
);
4786 m_aColWidths
.Add( colWidthInfo
);
4791 // update all the items
4792 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4794 wxListLineData
* const line
= GetLine(i
);
4795 wxListItemData
* const data
= new wxListItemData(this);
4797 line
->m_items
.Insert(col
, data
);
4799 line
->m_items
.Append(data
);
4803 // invalidate it as it has to be recalculated
4808 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4811 wxClientDC
dc(this);
4813 dc
.SetFont( GetFont() );
4815 if (item
->GetImage() != -1)
4818 GetImageSize( item
->GetImage(), ix
, iy
);
4822 if (!item
->GetText().empty())
4825 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4832 // ----------------------------------------------------------------------------
4834 // ----------------------------------------------------------------------------
4836 wxListCtrlCompare list_ctrl_compare_func_2
;
4837 long list_ctrl_compare_data
;
4839 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4841 wxListLineData
*line1
= *arg1
;
4842 wxListLineData
*line2
= *arg2
;
4844 line1
->GetItem( 0, item
);
4845 wxUIntPtr data1
= item
.m_data
;
4846 line2
->GetItem( 0, item
);
4847 wxUIntPtr data2
= item
.m_data
;
4848 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4851 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4853 // selections won't make sense any more after sorting the items so reset
4855 HighlightAll(false);
4858 list_ctrl_compare_func_2
= fn
;
4859 list_ctrl_compare_data
= data
;
4860 m_lines
.Sort( list_ctrl_compare_func_1
);
4864 // ----------------------------------------------------------------------------
4866 // ----------------------------------------------------------------------------
4868 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4871 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4872 wxScrolledWindow::OnScroll(event
);
4874 HandleOnScroll( event
);
4877 // update our idea of which lines are shown when we redraw the window the
4879 ResetVisibleLinesRange();
4881 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4883 wxGenericListCtrl
* lc
= GetListCtrl();
4884 wxCHECK_RET( lc
, _T("no listctrl window?") );
4886 lc
->m_headerWin
->Refresh();
4887 lc
->m_headerWin
->Update();
4891 int wxListMainWindow::GetCountPerPage() const
4893 if ( !m_linesPerPage
)
4895 wxConstCast(this, wxListMainWindow
)->
4896 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4899 return m_linesPerPage
;
4902 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4904 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4906 if ( m_lineFrom
== (size_t)-1 )
4908 size_t count
= GetItemCount();
4911 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4913 // this may happen if SetScrollbars() hadn't been called yet
4914 if ( m_lineFrom
>= count
)
4915 m_lineFrom
= count
- 1;
4917 // we redraw one extra line but this is needed to make the redrawing
4918 // logic work when there is a fractional number of lines on screen
4919 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4920 if ( m_lineTo
>= count
)
4921 m_lineTo
= count
- 1;
4923 else // empty control
4926 m_lineTo
= (size_t)-1;
4930 wxASSERT_MSG( IsEmpty() ||
4931 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4932 _T("GetVisibleLinesRange() returns incorrect result") );
4940 // -------------------------------------------------------------------------------------
4941 // wxGenericListCtrl
4942 // -------------------------------------------------------------------------------------
4944 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4946 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4947 EVT_SIZE(wxGenericListCtrl::OnSize
)
4950 wxGenericListCtrl::wxGenericListCtrl()
4952 m_imageListNormal
= (wxImageList
*) NULL
;
4953 m_imageListSmall
= (wxImageList
*) NULL
;
4954 m_imageListState
= (wxImageList
*) NULL
;
4956 m_ownsImageListNormal
=
4957 m_ownsImageListSmall
=
4958 m_ownsImageListState
= false;
4960 m_mainWin
= (wxListMainWindow
*) NULL
;
4961 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4965 wxGenericListCtrl::~wxGenericListCtrl()
4967 if (m_ownsImageListNormal
)
4968 delete m_imageListNormal
;
4969 if (m_ownsImageListSmall
)
4970 delete m_imageListSmall
;
4971 if (m_ownsImageListState
)
4972 delete m_imageListState
;
4975 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4981 GetThemeMetric( kThemeMetricListHeaderHeight
, &h
);
4983 // we use 'g' to get the descent, too
4985 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4986 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4989 // only update if changed
4990 if ( h
!= m_headerHeight
)
4995 ResizeReportView(true);
4996 else //why is this needed if it doesn't have a header?
4997 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
5002 void wxGenericListCtrl::CreateHeaderWindow()
5004 m_headerWin
= new wxListHeaderWindow
5006 this, wxID_ANY
, m_mainWin
,
5008 wxSize(GetClientSize().x
, m_headerHeight
),
5011 CalculateAndSetHeaderHeight();
5014 bool wxGenericListCtrl::Create(wxWindow
*parent
,
5019 const wxValidator
&validator
,
5020 const wxString
&name
)
5024 m_imageListState
= (wxImageList
*) NULL
;
5025 m_ownsImageListNormal
=
5026 m_ownsImageListSmall
=
5027 m_ownsImageListState
= false;
5029 m_mainWin
= (wxListMainWindow
*) NULL
;
5030 m_headerWin
= (wxListHeaderWindow
*) NULL
;
5034 if ( !(style
& wxLC_MASK_TYPE
) )
5036 style
= style
| wxLC_LIST
;
5039 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
5042 // this window itself shouldn't get the focus, only m_mainWin should
5045 // don't create the inner window with the border
5046 style
&= ~wxBORDER_MASK
;
5048 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
5051 // Human Interface Guidelines ask us for a special font in this case
5052 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
)
5055 font
.MacCreateFromThemeFont( kThemeViewsFont
);
5060 if ( InReportView() )
5062 CreateHeaderWindow();
5068 font
.MacCreateFromThemeFont( kThemeSmallSystemFont
);
5069 m_headerWin
->SetFont( font
);
5070 CalculateAndSetHeaderHeight();
5074 if ( HasFlag(wxLC_NO_HEADER
) )
5075 // VZ: why do we create it at all then?
5076 m_headerWin
->Show( false );
5079 SetInitialSize(size
);
5084 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
5086 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
5087 _T("wxLC_VIRTUAL can't be [un]set") );
5089 long flag
= GetWindowStyle();
5093 if (style
& wxLC_MASK_TYPE
)
5094 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5095 if (style
& wxLC_MASK_ALIGN
)
5096 flag
&= ~wxLC_MASK_ALIGN
;
5097 if (style
& wxLC_MASK_SORT
)
5098 flag
&= ~wxLC_MASK_SORT
;
5106 SetWindowStyleFlag( flag
);
5109 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5113 m_mainWin
->DeleteEverything();
5115 // has the header visibility changed?
5116 bool hasHeader
= HasHeader();
5117 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
5119 if ( hasHeader
!= willHaveHeader
)
5126 // don't delete, just hide, as we can reuse it later
5127 m_headerWin
->Show(false);
5129 //else: nothing to do
5131 else // must show header
5135 CreateHeaderWindow();
5137 else // already have it, just show
5139 m_headerWin
->Show( true );
5143 ResizeReportView(willHaveHeader
);
5147 wxWindow::SetWindowStyleFlag( flag
);
5150 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5152 m_mainWin
->GetColumn( col
, item
);
5156 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5158 m_mainWin
->SetColumn( col
, item
);
5162 int wxGenericListCtrl::GetColumnWidth( int col
) const
5164 return m_mainWin
->GetColumnWidth( col
);
5167 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5169 m_mainWin
->SetColumnWidth( col
, width
);
5173 int wxGenericListCtrl::GetCountPerPage() const
5175 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5178 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5180 m_mainWin
->GetItem( info
);
5184 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5186 m_mainWin
->SetItem( info
);
5190 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5193 info
.m_text
= label
;
5194 info
.m_mask
= wxLIST_MASK_TEXT
;
5195 info
.m_itemId
= index
;
5199 info
.m_image
= imageId
;
5200 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5203 m_mainWin
->SetItem(info
);
5207 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5209 return m_mainWin
->GetItemState( item
, stateMask
);
5212 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5214 m_mainWin
->SetItemState( item
, state
, stateMask
);
5219 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5221 return SetItemColumnImage(item
, 0, image
);
5225 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5228 info
.m_image
= image
;
5229 info
.m_mask
= wxLIST_MASK_IMAGE
;
5230 info
.m_itemId
= item
;
5231 info
.m_col
= column
;
5232 m_mainWin
->SetItem( info
);
5236 wxString
wxGenericListCtrl::GetItemText( long item
) const
5238 return m_mainWin
->GetItemText(item
);
5241 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5243 m_mainWin
->SetItemText(item
, str
);
5246 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5249 info
.m_mask
= wxLIST_MASK_DATA
;
5250 info
.m_itemId
= item
;
5251 m_mainWin
->GetItem( info
);
5255 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
5258 info
.m_mask
= wxLIST_MASK_DATA
;
5259 info
.m_itemId
= item
;
5261 m_mainWin
->SetItem( info
);
5265 wxRect
wxGenericListCtrl::GetViewRect() const
5267 return m_mainWin
->GetViewRect();
5270 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
5272 m_mainWin
->GetItemRect( item
, rect
);
5273 if ( m_mainWin
->HasHeader() )
5274 rect
.y
+= m_headerHeight
+ 1;
5278 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5280 m_mainWin
->GetItemPosition( item
, pos
);
5284 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5289 int wxGenericListCtrl::GetItemCount() const
5291 return m_mainWin
->GetItemCount();
5294 int wxGenericListCtrl::GetColumnCount() const
5296 return m_mainWin
->GetColumnCount();
5299 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5301 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5304 wxSize
wxGenericListCtrl::GetItemSpacing() const
5306 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5308 return wxSize(spacing
, spacing
);
5311 #if WXWIN_COMPATIBILITY_2_6
5312 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5314 return m_mainWin
->GetItemSpacing( isSmall
);
5316 #endif // WXWIN_COMPATIBILITY_2_6
5318 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5321 info
.m_itemId
= item
;
5322 info
.SetTextColour( col
);
5323 m_mainWin
->SetItem( info
);
5326 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5329 info
.m_itemId
= item
;
5330 m_mainWin
->GetItem( info
);
5331 return info
.GetTextColour();
5334 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5337 info
.m_itemId
= item
;
5338 info
.SetBackgroundColour( col
);
5339 m_mainWin
->SetItem( info
);
5342 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5345 info
.m_itemId
= item
;
5346 m_mainWin
->GetItem( info
);
5347 return info
.GetBackgroundColour();
5350 int wxGenericListCtrl::GetScrollPos( int orient
) const
5352 return m_mainWin
->GetScrollPos( orient
);
5355 void wxGenericListCtrl::SetScrollPos( int orient
, int pos
, bool refresh
)
5357 m_mainWin
->SetScrollPos( orient
, pos
, refresh
);
5360 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5363 info
.m_itemId
= item
;
5365 m_mainWin
->SetItem( info
);
5368 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5371 info
.m_itemId
= item
;
5372 m_mainWin
->GetItem( info
);
5373 return info
.GetFont();
5376 int wxGenericListCtrl::GetSelectedItemCount() const
5378 return m_mainWin
->GetSelectedItemCount();
5381 wxColour
wxGenericListCtrl::GetTextColour() const
5383 return GetForegroundColour();
5386 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5388 SetForegroundColour(col
);
5391 long wxGenericListCtrl::GetTopItem() const
5394 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5398 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5400 return m_mainWin
->GetNextItem( item
, geom
, state
);
5403 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5405 if (which
== wxIMAGE_LIST_NORMAL
)
5406 return m_imageListNormal
;
5407 else if (which
== wxIMAGE_LIST_SMALL
)
5408 return m_imageListSmall
;
5409 else if (which
== wxIMAGE_LIST_STATE
)
5410 return m_imageListState
;
5412 return (wxImageList
*) NULL
;
5415 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5417 if ( which
== wxIMAGE_LIST_NORMAL
)
5419 if (m_ownsImageListNormal
)
5420 delete m_imageListNormal
;
5421 m_imageListNormal
= imageList
;
5422 m_ownsImageListNormal
= false;
5424 else if ( which
== wxIMAGE_LIST_SMALL
)
5426 if (m_ownsImageListSmall
)
5427 delete m_imageListSmall
;
5428 m_imageListSmall
= imageList
;
5429 m_ownsImageListSmall
= false;
5431 else if ( which
== wxIMAGE_LIST_STATE
)
5433 if (m_ownsImageListState
)
5434 delete m_imageListState
;
5435 m_imageListState
= imageList
;
5436 m_ownsImageListState
= false;
5439 m_mainWin
->SetImageList( imageList
, which
);
5442 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5444 SetImageList(imageList
, which
);
5445 if ( which
== wxIMAGE_LIST_NORMAL
)
5446 m_ownsImageListNormal
= true;
5447 else if ( which
== wxIMAGE_LIST_SMALL
)
5448 m_ownsImageListSmall
= true;
5449 else if ( which
== wxIMAGE_LIST_STATE
)
5450 m_ownsImageListState
= true;
5453 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5458 bool wxGenericListCtrl::DeleteItem( long item
)
5460 m_mainWin
->DeleteItem( item
);
5464 bool wxGenericListCtrl::DeleteAllItems()
5466 m_mainWin
->DeleteAllItems();
5470 bool wxGenericListCtrl::DeleteAllColumns()
5472 size_t count
= m_mainWin
->m_columns
.GetCount();
5473 for ( size_t n
= 0; n
< count
; n
++ )
5478 void wxGenericListCtrl::ClearAll()
5480 m_mainWin
->DeleteEverything();
5483 bool wxGenericListCtrl::DeleteColumn( int col
)
5485 m_mainWin
->DeleteColumn( col
);
5487 // if we don't have the header any longer, we need to relayout the window
5488 if ( !GetColumnCount() )
5489 ResizeReportView(false /* no header */);
5493 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5494 wxClassInfo
* textControlClass
)
5496 return m_mainWin
->EditLabel( item
, textControlClass
);
5499 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5501 return m_mainWin
->GetEditControl();
5504 bool wxGenericListCtrl::EnsureVisible( long item
)
5506 m_mainWin
->EnsureVisible( item
);
5510 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5512 return m_mainWin
->FindItem( start
, str
, partial
);
5515 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5517 return m_mainWin
->FindItem( start
, data
);
5520 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5521 int WXUNUSED(direction
))
5523 return m_mainWin
->FindItem( pt
);
5526 // TODO: sub item hit testing
5527 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5529 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5532 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5534 m_mainWin
->InsertItem( info
);
5535 return info
.m_itemId
;
5538 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5541 info
.m_text
= label
;
5542 info
.m_mask
= wxLIST_MASK_TEXT
;
5543 info
.m_itemId
= index
;
5544 return InsertItem( info
);
5547 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5550 info
.m_mask
= wxLIST_MASK_IMAGE
;
5551 info
.m_image
= imageIndex
;
5552 info
.m_itemId
= index
;
5553 return InsertItem( info
);
5556 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5559 info
.m_text
= label
;
5560 info
.m_image
= imageIndex
;
5561 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5562 info
.m_itemId
= index
;
5563 return InsertItem( info
);
5566 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5568 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5570 m_mainWin
->InsertColumn( col
, item
);
5572 // if we hadn't had a header before but have one now
5573 // then we need to relayout the window
5574 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5575 ResizeReportView(true /* have header */);
5577 m_headerWin
->Refresh();
5582 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5583 int format
, int width
)
5586 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5587 item
.m_text
= heading
;
5590 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5591 item
.m_width
= width
;
5594 item
.m_format
= format
;
5596 return InsertColumn( col
, item
);
5599 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
5601 return m_mainWin
->ScrollList(dx
, dy
);
5605 // fn is a function which takes 3 long arguments: item1, item2, data.
5606 // item1 is the long data associated with a first item (NOT the index).
5607 // item2 is the long data associated with a second item (NOT the index).
5608 // data is the same value as passed to SortItems.
5609 // The return value is a negative number if the first item should precede the second
5610 // item, a positive number of the second item should precede the first,
5611 // or zero if the two items are equivalent.
5612 // data is arbitrary data to be passed to the sort function.
5614 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5616 m_mainWin
->SortItems( fn
, data
);
5620 // ----------------------------------------------------------------------------
5622 // ----------------------------------------------------------------------------
5624 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5629 ResizeReportView(m_mainWin
->HasHeader());
5630 m_mainWin
->RecalculatePositions();
5633 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5636 GetClientSize( &cw
, &ch
);
5640 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5641 if(ch
> m_headerHeight
)
5642 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5643 cw
, ch
- m_headerHeight
- 1 );
5645 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5648 else // no header window
5650 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5654 void wxGenericListCtrl::OnInternalIdle()
5656 wxWindow::OnInternalIdle();
5658 // do it only if needed
5659 if ( !m_mainWin
->m_dirty
)
5662 m_mainWin
->RecalculatePositions();
5665 // ----------------------------------------------------------------------------
5667 // ----------------------------------------------------------------------------
5669 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5673 m_mainWin
->SetBackgroundColour( colour
);
5674 m_mainWin
->m_dirty
= true;
5680 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5682 if ( !wxWindow::SetForegroundColour( colour
) )
5687 m_mainWin
->SetForegroundColour( colour
);
5688 m_mainWin
->m_dirty
= true;
5692 m_headerWin
->SetForegroundColour( colour
);
5697 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5699 if ( !wxWindow::SetFont( font
) )
5704 m_mainWin
->SetFont( font
);
5705 m_mainWin
->m_dirty
= true;
5710 m_headerWin
->SetFont( font
);
5711 CalculateAndSetHeaderHeight();
5721 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5724 // Use the same color scheme as wxListBox
5725 return wxListBox::GetClassDefaultAttributes(variant
);
5727 wxUnusedVar(variant
);
5728 wxVisualAttributes attr
;
5729 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5730 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5731 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5736 // ----------------------------------------------------------------------------
5737 // methods forwarded to m_mainWin
5738 // ----------------------------------------------------------------------------
5740 #if wxUSE_DRAG_AND_DROP
5742 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5744 m_mainWin
->SetDropTarget( dropTarget
);
5747 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5749 return m_mainWin
->GetDropTarget();
5754 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5756 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5759 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5761 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5764 wxColour
wxGenericListCtrl::GetForegroundColour() const
5766 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5769 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5772 return m_mainWin
->PopupMenu( menu
, x
, y
);
5778 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5780 m_mainWin
->DoClientToScreen(x
, y
);
5783 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5785 m_mainWin
->DoScreenToClient(x
, y
);
5788 void wxGenericListCtrl::SetFocus()
5790 // The test in window.cpp fails as we are a composite
5791 // window, so it checks against "this", but not m_mainWin.
5792 if ( DoFindFocus() != this )
5793 m_mainWin
->SetFocus();
5796 wxSize
wxGenericListCtrl::DoGetBestSize() const
5798 // Something is better than nothing...
5799 // 100x80 is what the MSW version will get from the default
5800 // wxControl::DoGetBestSize
5801 return wxSize(100, 80);
5804 // ----------------------------------------------------------------------------
5805 // virtual list control support
5806 // ----------------------------------------------------------------------------
5808 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5810 // this is a pure virtual function, in fact - which is not really pure
5811 // because the controls which are not virtual don't need to implement it
5812 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5814 return wxEmptyString
;
5817 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5819 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5821 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5825 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5828 return OnGetItemImage(item
);
5834 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5836 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5837 _T("invalid item index in OnGetItemAttr()") );
5839 // no attributes by default
5843 void wxGenericListCtrl::SetItemCount(long count
)
5845 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5847 m_mainWin
->SetItemCount(count
);
5850 void wxGenericListCtrl::RefreshItem(long item
)
5852 m_mainWin
->RefreshLine(item
);
5855 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5857 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5860 // Generic wxListCtrl is more or less a container for two other
5861 // windows which drawings are done upon. These are namely
5862 // 'm_headerWin' and 'm_mainWin'.
5863 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5864 // behaviour wxListCtrl has under wxMSW.
5866 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5870 // The easy case, no rectangle specified.
5872 m_headerWin
->Refresh(eraseBackground
);
5875 m_mainWin
->Refresh(eraseBackground
);
5879 // Refresh the header window
5882 wxRect rectHeader
= m_headerWin
->GetRect();
5883 rectHeader
.Intersect(*rect
);
5884 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5887 m_headerWin
->GetPosition(&x
, &y
);
5888 rectHeader
.Offset(-x
, -y
);
5889 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5893 // Refresh the main window
5896 wxRect rectMain
= m_mainWin
->GetRect();
5897 rectMain
.Intersect(*rect
);
5898 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5901 m_mainWin
->GetPosition(&x
, &y
);
5902 rectMain
.Offset(-x
, -y
);
5903 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5909 void wxGenericListCtrl::DoFreeze()
5911 m_mainWin
->Freeze();
5914 void wxGenericListCtrl::DoThaw()
5919 #endif // wxUSE_LISTCTRL