1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 // 1. we need to implement searching/sorting for virtual controls somehow
14 // 2. when changing selection the lines are refreshed twice
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #include "wx/listctrl.h"
28 #if (!defined(__WXMSW__) || defined(__WXUNIVERSAL__)) && !defined(__WXMAC__)
29 // if we have a native version, its implementation file does all this
30 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
31 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
32 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
34 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
38 #include "wx/scrolwin.h"
40 #include "wx/settings.h"
41 #include "wx/dynarray.h"
42 #include "wx/dcclient.h"
43 #include "wx/dcscreen.h"
45 #include "wx/settings.h"
48 #include "wx/imaglist.h"
49 #include "wx/selstore.h"
50 #include "wx/renderer.h"
53 #include "wx/mac/private.h"
57 // NOTE: If using the wxListBox visual attributes works everywhere then this can
58 // be removed, as well as the #else case below.
59 #define _USE_VISATTR 0
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // // the height of the header window (FIXME: should depend on its font!)
67 // static const int HEADER_HEIGHT = 23;
69 static const int SCROLL_UNIT_X
= 15;
71 // the spacing between the lines (in report mode)
72 static const int LINE_SPACING
= 0;
74 // extra margins around the text label
76 static const int EXTRA_WIDTH
= 6;
78 static const int EXTRA_WIDTH
= 4;
80 static const int EXTRA_HEIGHT
= 4;
82 // margin between the window and the items
83 static const int EXTRA_BORDER_X
= 2;
84 static const int EXTRA_BORDER_Y
= 2;
86 // offset for the header window
87 static const int HEADER_OFFSET_X
= 0;
88 static const int HEADER_OFFSET_Y
= 0;
90 // margin between rows of icons in [small] icon view
91 static const int MARGIN_BETWEEN_ROWS
= 6;
93 // when autosizing the columns, add some slack
94 static const int AUTOSIZE_COL_MARGIN
= 10;
96 // default width for the header columns
97 static const int WIDTH_COL_DEFAULT
= 80;
99 // the space between the image and the text in the report mode
100 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
102 // the space between the image and the text in the report mode in header
103 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
105 // ============================================================================
107 // ============================================================================
109 //-----------------------------------------------------------------------------
110 // wxColWidthInfo (internal)
111 //-----------------------------------------------------------------------------
113 struct wxColWidthInfo
116 bool bNeedsUpdate
; // only set to true when an item whose
117 // width == nMaxWidth is removed
119 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
122 bNeedsUpdate
= needsUpdate
;
126 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
128 //-----------------------------------------------------------------------------
129 // wxListItemData (internal)
130 //-----------------------------------------------------------------------------
135 wxListItemData(wxListMainWindow
*owner
);
138 void SetItem( const wxListItem
&info
);
139 void SetImage( int image
) { m_image
= image
; }
140 void SetData( wxUIntPtr data
) { m_data
= data
; }
141 void SetPosition( int x
, int y
);
142 void SetSize( int width
, int height
);
144 bool HasText() const { return !m_text
.empty(); }
145 const wxString
& GetText() const { return m_text
; }
146 void SetText(const wxString
& text
) { m_text
= text
; }
148 // we can't use empty string for measuring the string width/height, so
149 // always return something
150 wxString
GetTextForMeasuring() const
152 wxString s
= GetText();
159 bool IsHit( int x
, int y
) const;
163 int GetWidth() const;
164 int GetHeight() const;
166 int GetImage() const { return m_image
; }
167 bool HasImage() const { return GetImage() != -1; }
169 void GetItem( wxListItem
&info
) const;
171 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
172 wxListItemAttr
*GetAttr() const { return m_attr
; }
175 // the item image or -1
178 // user data associated with the item
181 // the item coordinates are not used in report mode; instead this pointer is
182 // NULL and the owner window is used to retrieve the item position and size
185 // the list ctrl we are in
186 wxListMainWindow
*m_owner
;
188 // custom attributes or NULL
189 wxListItemAttr
*m_attr
;
192 // common part of all ctors
198 //-----------------------------------------------------------------------------
199 // wxListHeaderData (internal)
200 //-----------------------------------------------------------------------------
202 class wxListHeaderData
: public wxObject
206 wxListHeaderData( const wxListItem
&info
);
207 void SetItem( const wxListItem
&item
);
208 void SetPosition( int x
, int y
);
209 void SetWidth( int w
);
210 void SetState( int state
);
211 void SetFormat( int format
);
212 void SetHeight( int h
);
213 bool HasImage() const;
215 bool HasText() const { return !m_text
.empty(); }
216 const wxString
& GetText() const { return m_text
; }
217 void SetText(const wxString
& text
) { m_text
= text
; }
219 void GetItem( wxListItem
&item
);
221 bool IsHit( int x
, int y
) const;
222 int GetImage() const;
223 int GetWidth() const;
224 int GetFormat() const;
225 int GetState() const;
242 //-----------------------------------------------------------------------------
243 // wxListLineData (internal)
244 //-----------------------------------------------------------------------------
246 WX_DECLARE_EXPORTED_LIST(wxListItemData
, wxListItemDataList
);
247 #include "wx/listimpl.cpp"
248 WX_DEFINE_LIST(wxListItemDataList
)
253 // the list of subitems: only may have more than one item in report mode
254 wxListItemDataList m_items
;
256 // this is not used in report view
268 // the part to be highlighted
269 wxRect m_rectHighlight
;
271 // extend all our rects to be centered inside the one of given width
272 void ExtendWidth(wxCoord w
)
274 wxASSERT_MSG( m_rectAll
.width
<= w
,
275 _T("width can only be increased") );
278 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
279 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
280 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
285 // is this item selected? [NB: not used in virtual mode]
288 // back pointer to the list ctrl
289 wxListMainWindow
*m_owner
;
292 wxListLineData(wxListMainWindow
*owner
);
296 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
300 // are we in report mode?
301 inline bool InReportView() const;
303 // are we in virtual report mode?
304 inline bool IsVirtual() const;
306 // these 2 methods shouldn't be called for report view controls, in that
307 // case we determine our position/size ourselves
309 // calculate the size of the line
310 void CalculateSize( wxDC
*dc
, int spacing
);
312 // remember the position this line appears at
313 void SetPosition( int x
, int y
, int spacing
);
317 void SetImage( int image
) { SetImage(0, image
); }
318 int GetImage() const { return GetImage(0); }
319 void SetImage( int index
, int image
);
320 int GetImage( int index
) const;
322 bool HasImage() const { return GetImage() != -1; }
323 bool HasText() const { return !GetText(0).empty(); }
325 void SetItem( int index
, const wxListItem
&info
);
326 void GetItem( int index
, wxListItem
&info
);
328 wxString
GetText(int index
) const;
329 void SetText( int index
, const wxString
& s
);
331 wxListItemAttr
*GetAttr() const;
332 void SetAttr(wxListItemAttr
*attr
);
334 // return true if the highlighting really changed
335 bool Highlight( bool on
);
337 void ReverseHighlight();
339 bool IsHighlighted() const
341 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
343 return m_highlighted
;
346 // draw the line on the given DC in icon/list mode
347 void Draw( wxDC
*dc
);
349 // the same in report mode
350 void DrawInReportMode( wxDC
*dc
,
352 const wxRect
& rectHL
,
356 // set the line to contain num items (only can be > 1 in report mode)
357 void InitItems( int num
);
359 // get the mode (i.e. style) of the list control
360 inline int GetMode() const;
362 // prepare the DC for drawing with these item's attributes, return true if
363 // we need to draw the items background to highlight it, false otherwise
364 bool SetAttributes(wxDC
*dc
,
365 const wxListItemAttr
*attr
,
368 // draw the text on the DC with the correct justification; also add an
369 // ellipsis if the text is too large to fit in the current width
370 void DrawTextFormatted(wxDC
*dc
,
371 const wxString
&text
,
374 int yMid
, // this is middle, not top, of the text
378 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
379 #include "wx/arrimpl.cpp"
380 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
382 //-----------------------------------------------------------------------------
383 // wxListHeaderWindow (internal)
384 //-----------------------------------------------------------------------------
386 class wxListHeaderWindow
: public wxWindow
389 wxListMainWindow
*m_owner
;
390 const wxCursor
*m_currentCursor
;
391 wxCursor
*m_resizeCursor
;
394 // column being resized or -1
397 // divider line position in logical (unscrolled) coords
400 // minimal position beyond which the divider line
401 // can't be dragged in logical coords
405 wxListHeaderWindow();
407 wxListHeaderWindow( wxWindow
*win
,
409 wxListMainWindow
*owner
,
410 const wxPoint
&pos
= wxDefaultPosition
,
411 const wxSize
&size
= wxDefaultSize
,
413 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
415 virtual ~wxListHeaderWindow();
418 void AdjustDC( wxDC
& dc
);
420 void OnPaint( wxPaintEvent
&event
);
421 void OnMouse( wxMouseEvent
&event
);
422 void OnSetFocus( wxFocusEvent
&event
);
428 // common part of all ctors
431 // generate and process the list event of the given type, return true if
432 // it wasn't vetoed, i.e. if we should proceed
433 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
435 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
436 DECLARE_EVENT_TABLE()
439 //-----------------------------------------------------------------------------
440 // wxListRenameTimer (internal)
441 //-----------------------------------------------------------------------------
443 class wxListRenameTimer
: public wxTimer
446 wxListMainWindow
*m_owner
;
449 wxListRenameTimer( wxListMainWindow
*owner
);
453 //-----------------------------------------------------------------------------
454 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
455 //-----------------------------------------------------------------------------
457 class wxListTextCtrlWrapper
: public wxEvtHandler
460 // NB: text must be a valid object but not Create()d yet
461 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
465 wxTextCtrl
*GetText() const { return m_text
; }
467 void AcceptChangesAndFinish();
470 void OnChar( wxKeyEvent
&event
);
471 void OnKeyUp( wxKeyEvent
&event
);
472 void OnKillFocus( wxFocusEvent
&event
);
474 bool AcceptChanges();
478 wxListMainWindow
*m_owner
;
480 wxString m_startValue
;
483 bool m_aboutToFinish
;
485 DECLARE_EVENT_TABLE()
488 //-----------------------------------------------------------------------------
489 // wxListMainWindow (internal)
490 //-----------------------------------------------------------------------------
492 WX_DECLARE_EXPORTED_LIST(wxListHeaderData
, wxListHeaderDataList
);
493 #include "wx/listimpl.cpp"
494 WX_DEFINE_LIST(wxListHeaderDataList
)
496 class wxListMainWindow
: public wxScrolledWindow
500 wxListMainWindow( wxWindow
*parent
,
502 const wxPoint
& pos
= wxDefaultPosition
,
503 const wxSize
& size
= wxDefaultSize
,
505 const wxString
&name
= _T("listctrlmainwindow") );
507 virtual ~wxListMainWindow();
509 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
511 // return true if this is a virtual list control
512 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
514 // return true if the control is in report mode
515 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
517 // return true if we are in single selection mode, false if multi sel
518 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
520 // do we have a header window?
521 bool HasHeader() const
522 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
524 void HighlightAll( bool on
);
526 // all these functions only do something if the line is currently visible
528 // change the line "selected" state, return true if it really changed
529 bool HighlightLine( size_t line
, bool highlight
= true);
531 // as HighlightLine() but do it for the range of lines: this is incredibly
532 // more efficient for virtual list controls!
534 // NB: unlike HighlightLine() this one does refresh the lines on screen
535 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
537 // toggle the line state and refresh it
538 void ReverseHighlight( size_t line
)
539 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
541 // return true if the line is highlighted
542 bool IsHighlighted(size_t line
) const;
544 // refresh one or several lines at once
545 void RefreshLine( size_t line
);
546 void RefreshLines( size_t lineFrom
, size_t lineTo
);
548 // refresh all selected items
549 void RefreshSelected();
551 // refresh all lines below the given one: the difference with
552 // RefreshLines() is that the index here might not be a valid one (happens
553 // when the last line is deleted)
554 void RefreshAfter( size_t lineFrom
);
556 // the methods which are forwarded to wxListLineData itself in list/icon
557 // modes but are here because the lines don't store their positions in the
560 // get the bound rect for the entire line
561 wxRect
GetLineRect(size_t line
) const;
563 // get the bound rect of the label
564 wxRect
GetLineLabelRect(size_t line
) const;
566 // get the bound rect of the items icon (only may be called if we do have
568 wxRect
GetLineIconRect(size_t line
) const;
570 // get the rect to be highlighted when the item has focus
571 wxRect
GetLineHighlightRect(size_t line
) const;
573 // get the size of the total line rect
574 wxSize
GetLineSize(size_t line
) const
575 { return GetLineRect(line
).GetSize(); }
577 // return the hit code for the corresponding position (in this line)
578 long HitTestLine(size_t line
, int x
, int y
) const;
580 // bring the selected item into view, scrolling to it if necessary
581 void MoveToItem(size_t item
);
583 bool ScrollList( int WXUNUSED(dx
), int dy
);
585 // bring the current item into view
586 void MoveToFocus() { MoveToItem(m_current
); }
588 // start editing the label of the given item
589 wxTextCtrl
*EditLabel(long item
,
590 wxClassInfo
* textControlClass
= CLASSINFO(wxTextCtrl
));
591 wxTextCtrl
*GetEditControl() const
593 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
596 void FinishEditing(wxTextCtrl
*text
)
599 m_textctrlWrapper
= NULL
;
600 SetFocusIgnoringChildren();
603 // suspend/resume redrawing the control
607 void OnRenameTimer();
608 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
609 void OnRenameCancelled(size_t itemEdit
);
611 void OnMouse( wxMouseEvent
&event
);
613 // called to switch the selection from the current item to newCurrent,
614 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
616 void OnChar( wxKeyEvent
&event
);
617 void OnKeyDown( wxKeyEvent
&event
);
618 void OnKeyUp( wxKeyEvent
&event
);
619 void OnSetFocus( wxFocusEvent
&event
);
620 void OnKillFocus( wxFocusEvent
&event
);
621 void OnScroll( wxScrollWinEvent
& event
);
623 void OnPaint( wxPaintEvent
&event
);
625 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
626 void GetImageSize( int index
, int &width
, int &height
) const;
627 int GetTextLength( const wxString
&s
) const;
629 void SetImageList( wxImageList
*imageList
, int which
);
630 void SetItemSpacing( int spacing
, bool isSmall
= false );
631 int GetItemSpacing( bool isSmall
= false );
633 void SetColumn( int col
, wxListItem
&item
);
634 void SetColumnWidth( int col
, int width
);
635 void GetColumn( int col
, wxListItem
&item
) const;
636 int GetColumnWidth( int col
) const;
637 int GetColumnCount() const { return m_columns
.GetCount(); }
639 // returns the sum of the heights of all columns
640 int GetHeaderWidth() const;
642 int GetCountPerPage() const;
644 void SetItem( wxListItem
&item
);
645 void GetItem( wxListItem
&item
) const;
646 void SetItemState( long item
, long state
, long stateMask
);
647 void SetItemStateAll( long state
, long stateMask
);
648 int GetItemState( long item
, long stateMask
) const;
649 void GetItemRect( long index
, wxRect
&rect
) const;
650 wxRect
GetViewRect() const;
651 bool GetItemPosition( long item
, wxPoint
& pos
) const;
652 int GetSelectedItemCount() const;
654 wxString
GetItemText(long item
) const
657 info
.m_mask
= wxLIST_MASK_TEXT
;
658 info
.m_itemId
= item
;
663 void SetItemText(long item
, const wxString
& value
)
666 info
.m_mask
= wxLIST_MASK_TEXT
;
667 info
.m_itemId
= item
;
672 // set the scrollbars and update the positions of the items
673 void RecalculatePositions(bool noRefresh
= false);
675 // refresh the window and the header
678 long GetNextItem( long item
, int geometry
, int state
) const;
679 void DeleteItem( long index
);
680 void DeleteAllItems();
681 void DeleteColumn( int col
);
682 void DeleteEverything();
683 void EnsureVisible( long index
);
684 long FindItem( long start
, const wxString
& str
, bool partial
= false );
685 long FindItem( long start
, wxUIntPtr data
);
686 long FindItem( const wxPoint
& pt
);
687 long HitTest( int x
, int y
, int &flags
) const;
688 void InsertItem( wxListItem
&item
);
689 void InsertColumn( long col
, wxListItem
&item
);
690 int GetItemWidthWithImage(wxListItem
* item
);
691 void SortItems( wxListCtrlCompare fn
, long data
);
693 size_t GetItemCount() const;
694 bool IsEmpty() const { return GetItemCount() == 0; }
695 void SetItemCount(long count
);
697 // change the current (== focused) item, send a notification event
698 void ChangeCurrent(size_t current
);
699 void ResetCurrent() { ChangeCurrent((size_t)-1); }
700 bool HasCurrent() const { return m_current
!= (size_t)-1; }
702 // send out a wxListEvent
703 void SendNotify( size_t line
,
705 const wxPoint
& point
= wxDefaultPosition
);
707 // override base class virtual to reset m_lineHeight when the font changes
708 virtual bool SetFont(const wxFont
& font
)
710 if ( !wxScrolledWindow::SetFont(font
) )
718 // these are for wxListLineData usage only
720 // get the backpointer to the list ctrl
721 wxGenericListCtrl
*GetListCtrl() const
723 return wxStaticCast(GetParent(), wxGenericListCtrl
);
726 // get the height of all lines (assuming they all do have the same height)
727 wxCoord
GetLineHeight() const;
729 // get the y position of the given line (only for report view)
730 wxCoord
GetLineY(size_t line
) const;
732 // get the brush to use for the item highlighting
733 wxBrush
*GetHighlightBrush() const
735 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
738 bool HasFocus() const
744 // the array of all line objects for a non virtual list control (for the
745 // virtual list control we only ever use m_lines[0])
746 wxListLineDataArray m_lines
;
748 // the list of column objects
749 wxListHeaderDataList m_columns
;
751 // currently focused item or -1
754 // the number of lines per page
757 // this flag is set when something which should result in the window
758 // redrawing happens (i.e. an item was added or deleted, or its appearance
759 // changed) and OnPaint() doesn't redraw the window while it is set which
760 // allows to minimize the number of repaintings when a lot of items are
761 // being added. The real repainting occurs only after the next OnIdle()
765 wxColour
*m_highlightColour
;
766 wxImageList
*m_small_image_list
;
767 wxImageList
*m_normal_image_list
;
769 int m_normal_spacing
;
773 wxTimer
*m_renameTimer
;
777 ColWidthArray m_aColWidths
;
779 // for double click logic
780 size_t m_lineLastClicked
,
781 m_lineBeforeLastClicked
,
782 m_lineSelectSingleOnUp
;
785 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
787 // the total count of items in a virtual list control
790 // the object maintaining the items selection state, only used in virtual
792 wxSelectionStore m_selStore
;
794 // common part of all ctors
797 // get the line data for the given index
798 wxListLineData
*GetLine(size_t n
) const
800 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
804 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
811 // get a dummy line which can be used for geometry calculations and such:
812 // you must use GetLine() if you want to really draw the line
813 wxListLineData
*GetDummyLine() const;
815 // cache the line data of the n-th line in m_lines[0]
816 void CacheLineData(size_t line
);
818 // get the range of visible lines
819 void GetVisibleLinesRange(size_t *from
, size_t *to
);
821 // force us to recalculate the range of visible lines
822 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
824 // get the colour to be used for drawing the rules
825 wxColour
GetRuleColour() const
827 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
831 // initialize the current item if needed
832 void UpdateCurrent();
834 // delete all items but don't refresh: called from dtor
835 void DoDeleteAllItems();
837 // the height of one line using the current font
838 wxCoord m_lineHeight
;
840 // the total header width or 0 if not calculated yet
841 wxCoord m_headerWidth
;
843 // the first and last lines being shown on screen right now (inclusive),
844 // both may be -1 if they must be calculated so never access them directly:
845 // use GetVisibleLinesRange() above instead
849 // the brushes to use for item highlighting when we do/don't have focus
850 wxBrush
*m_highlightBrush
,
851 *m_highlightUnfocusedBrush
;
853 // if this is > 0, the control is frozen and doesn't redraw itself
854 size_t m_freezeCount
;
856 // wrapper around the text control currently used for in place editing or
857 // NULL if no item is being edited
858 wxListTextCtrlWrapper
*m_textctrlWrapper
;
861 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
862 DECLARE_EVENT_TABLE()
864 friend class wxGenericListCtrl
;
868 wxListItemData::~wxListItemData()
870 // in the virtual list control the attributes are managed by the main
871 // program, so don't delete them
872 if ( !m_owner
->IsVirtual() )
878 void wxListItemData::Init()
886 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
892 if ( owner
->InReportView() )
898 void wxListItemData::SetItem( const wxListItem
&info
)
900 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
901 SetText(info
.m_text
);
902 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
903 m_image
= info
.m_image
;
904 if ( info
.m_mask
& wxLIST_MASK_DATA
)
905 m_data
= info
.m_data
;
907 if ( info
.HasAttributes() )
910 m_attr
->AssignFrom(*info
.GetAttributes());
912 m_attr
= new wxListItemAttr(*info
.GetAttributes());
920 m_rect
->width
= info
.m_width
;
924 void wxListItemData::SetPosition( int x
, int y
)
926 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
932 void wxListItemData::SetSize( int width
, int height
)
934 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
937 m_rect
->width
= width
;
939 m_rect
->height
= height
;
942 bool wxListItemData::IsHit( int x
, int y
) const
944 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
946 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
949 int wxListItemData::GetX() const
951 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
956 int wxListItemData::GetY() const
958 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
963 int wxListItemData::GetWidth() const
965 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
967 return m_rect
->width
;
970 int wxListItemData::GetHeight() const
972 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
974 return m_rect
->height
;
977 void wxListItemData::GetItem( wxListItem
&info
) const
979 long mask
= info
.m_mask
;
981 // by default, get everything for backwards compatibility
984 if ( mask
& wxLIST_MASK_TEXT
)
985 info
.m_text
= m_text
;
986 if ( mask
& wxLIST_MASK_IMAGE
)
987 info
.m_image
= m_image
;
988 if ( mask
& wxLIST_MASK_DATA
)
989 info
.m_data
= m_data
;
993 if ( m_attr
->HasTextColour() )
994 info
.SetTextColour(m_attr
->GetTextColour());
995 if ( m_attr
->HasBackgroundColour() )
996 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
997 if ( m_attr
->HasFont() )
998 info
.SetFont(m_attr
->GetFont());
1002 //-----------------------------------------------------------------------------
1004 //-----------------------------------------------------------------------------
1006 void wxListHeaderData::Init()
1018 wxListHeaderData::wxListHeaderData()
1023 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1030 void wxListHeaderData::SetItem( const wxListItem
&item
)
1032 m_mask
= item
.m_mask
;
1034 if ( m_mask
& wxLIST_MASK_TEXT
)
1035 m_text
= item
.m_text
;
1037 if ( m_mask
& wxLIST_MASK_IMAGE
)
1038 m_image
= item
.m_image
;
1040 if ( m_mask
& wxLIST_MASK_FORMAT
)
1041 m_format
= item
.m_format
;
1043 if ( m_mask
& wxLIST_MASK_WIDTH
)
1044 SetWidth(item
.m_width
);
1046 if ( m_mask
& wxLIST_MASK_STATE
)
1047 SetState(item
.m_state
);
1050 void wxListHeaderData::SetPosition( int x
, int y
)
1056 void wxListHeaderData::SetHeight( int h
)
1061 void wxListHeaderData::SetWidth( int w
)
1063 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1066 void wxListHeaderData::SetState( int flag
)
1071 void wxListHeaderData::SetFormat( int format
)
1076 bool wxListHeaderData::HasImage() const
1078 return m_image
!= -1;
1081 bool wxListHeaderData::IsHit( int x
, int y
) const
1083 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1086 void wxListHeaderData::GetItem( wxListItem
& item
)
1088 item
.m_mask
= m_mask
;
1089 item
.m_text
= m_text
;
1090 item
.m_image
= m_image
;
1091 item
.m_format
= m_format
;
1092 item
.m_width
= m_width
;
1093 item
.m_state
= m_state
;
1096 int wxListHeaderData::GetImage() const
1101 int wxListHeaderData::GetWidth() const
1106 int wxListHeaderData::GetFormat() const
1111 int wxListHeaderData::GetState() const
1116 //-----------------------------------------------------------------------------
1118 //-----------------------------------------------------------------------------
1120 inline int wxListLineData::GetMode() const
1122 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1125 inline bool wxListLineData::InReportView() const
1127 return m_owner
->HasFlag(wxLC_REPORT
);
1130 inline bool wxListLineData::IsVirtual() const
1132 return m_owner
->IsVirtual();
1135 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1139 if ( InReportView() )
1142 m_gi
= new GeometryInfo
;
1144 m_highlighted
= false;
1146 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1149 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1151 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1152 wxCHECK_RET( node
, _T("no subitems at all??") );
1154 wxListItemData
*item
= node
->GetData();
1159 switch ( GetMode() )
1162 case wxLC_SMALL_ICON
:
1163 m_gi
->m_rectAll
.width
= spacing
;
1165 s
= item
->GetText();
1170 m_gi
->m_rectLabel
.width
=
1171 m_gi
->m_rectLabel
.height
= 0;
1175 dc
->GetTextExtent( s
, &lw
, &lh
);
1179 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1181 m_gi
->m_rectAll
.width
= lw
;
1183 m_gi
->m_rectLabel
.width
= lw
;
1184 m_gi
->m_rectLabel
.height
= lh
;
1187 if (item
->HasImage())
1190 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1191 m_gi
->m_rectIcon
.width
= w
+ 8;
1192 m_gi
->m_rectIcon
.height
= h
+ 8;
1194 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1195 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1196 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1197 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1200 if ( item
->HasText() )
1202 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1203 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1205 else // no text, highlight the icon
1207 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1208 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1213 s
= item
->GetTextForMeasuring();
1215 dc
->GetTextExtent( s
, &lw
, &lh
);
1219 m_gi
->m_rectLabel
.width
= lw
;
1220 m_gi
->m_rectLabel
.height
= lh
;
1222 m_gi
->m_rectAll
.width
= lw
;
1223 m_gi
->m_rectAll
.height
= lh
;
1225 if (item
->HasImage())
1228 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1229 m_gi
->m_rectIcon
.width
= w
;
1230 m_gi
->m_rectIcon
.height
= h
;
1232 m_gi
->m_rectAll
.width
+= 4 + w
;
1233 if (h
> m_gi
->m_rectAll
.height
)
1234 m_gi
->m_rectAll
.height
= h
;
1237 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1238 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1242 wxFAIL_MSG( _T("unexpected call to SetSize") );
1246 wxFAIL_MSG( _T("unknown mode") );
1251 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1253 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1254 wxCHECK_RET( node
, _T("no subitems at all??") );
1256 wxListItemData
*item
= node
->GetData();
1258 switch ( GetMode() )
1261 case wxLC_SMALL_ICON
:
1262 m_gi
->m_rectAll
.x
= x
;
1263 m_gi
->m_rectAll
.y
= y
;
1265 if ( item
->HasImage() )
1267 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1268 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1269 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1272 if ( item
->HasText() )
1274 if (m_gi
->m_rectAll
.width
> spacing
)
1275 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1277 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1278 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1279 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1280 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1282 else // no text, highlight the icon
1284 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1285 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1290 m_gi
->m_rectAll
.x
= x
;
1291 m_gi
->m_rectAll
.y
= y
;
1293 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1294 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1295 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1297 if (item
->HasImage())
1299 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1300 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1301 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1305 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1310 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1314 wxFAIL_MSG( _T("unknown mode") );
1319 void wxListLineData::InitItems( int num
)
1321 for (int i
= 0; i
< num
; i
++)
1322 m_items
.Append( new wxListItemData(m_owner
) );
1325 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1327 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1328 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1330 wxListItemData
*item
= node
->GetData();
1331 item
->SetItem( info
);
1334 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1336 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1339 wxListItemData
*item
= node
->GetData();
1340 item
->GetItem( info
);
1344 wxString
wxListLineData::GetText(int index
) const
1348 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1351 wxListItemData
*item
= node
->GetData();
1352 s
= item
->GetText();
1358 void wxListLineData::SetText( int index
, const wxString
& s
)
1360 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1363 wxListItemData
*item
= node
->GetData();
1368 void wxListLineData::SetImage( int index
, int image
)
1370 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1371 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1373 wxListItemData
*item
= node
->GetData();
1374 item
->SetImage(image
);
1377 int wxListLineData::GetImage( int index
) const
1379 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1380 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1382 wxListItemData
*item
= node
->GetData();
1383 return item
->GetImage();
1386 wxListItemAttr
*wxListLineData::GetAttr() const
1388 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1389 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1391 wxListItemData
*item
= node
->GetData();
1392 return item
->GetAttr();
1395 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1397 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1398 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1400 wxListItemData
*item
= node
->GetData();
1401 item
->SetAttr(attr
);
1404 bool wxListLineData::SetAttributes(wxDC
*dc
,
1405 const wxListItemAttr
*attr
,
1408 wxWindow
*listctrl
= m_owner
->GetParent();
1412 // don't use foreground colour for drawing highlighted items - this might
1413 // make them completely invisible (and there is no way to do bit
1414 // arithmetics on wxColour, unfortunately)
1419 if (m_owner
->HasFocus()
1420 #if !defined(__WXUNIVERSAL__)
1421 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1429 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1431 else if ( attr
&& attr
->HasTextColour() )
1432 colText
= attr
->GetTextColour();
1434 colText
= listctrl
->GetForegroundColour();
1436 dc
->SetTextForeground(colText
);
1440 if ( attr
&& attr
->HasFont() )
1441 font
= attr
->GetFont();
1443 font
= listctrl
->GetFont();
1448 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1449 if ( highlighted
|| hasBgCol
)
1452 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1454 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1456 dc
->SetPen( *wxTRANSPARENT_PEN
);
1464 void wxListLineData::Draw( wxDC
*dc
)
1466 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1467 wxCHECK_RET( node
, _T("no subitems at all??") );
1469 bool highlighted
= IsHighlighted();
1471 wxListItemAttr
*attr
= GetAttr();
1473 if ( SetAttributes(dc
, attr
, highlighted
) )
1474 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1476 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1482 int flags
= wxCONTROL_SELECTED
;
1483 if (m_owner
->HasFocus()
1484 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
1485 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1488 flags
|= wxCONTROL_FOCUSED
;
1489 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1494 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1499 // just for debugging to better see where the items are
1501 dc
->SetPen(*wxRED_PEN
);
1502 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1503 dc
->DrawRectangle( m_gi
->m_rectAll
);
1504 dc
->SetPen(*wxGREEN_PEN
);
1505 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1508 wxListItemData
*item
= node
->GetData();
1509 if (item
->HasImage())
1511 // centre the image inside our rectangle, this looks nicer when items
1512 // ae aligned in a row
1513 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1515 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1518 if (item
->HasText())
1520 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1522 wxDCClipper
clipper(*dc
, rectLabel
);
1523 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1527 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1529 const wxRect
& rectHL
,
1532 // TODO: later we should support setting different attributes for
1533 // different columns - to do it, just add "col" argument to
1534 // GetAttr() and move these lines into the loop below
1535 wxListItemAttr
*attr
= GetAttr();
1536 if ( SetAttributes(dc
, attr
, highlighted
) )
1537 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1539 dc
->DrawRectangle( rectHL
);
1545 int flags
= wxCONTROL_SELECTED
;
1546 if (m_owner
->HasFocus())
1547 flags
|= wxCONTROL_FOCUSED
;
1548 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1552 dc
->DrawRectangle( rectHL
);
1557 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1558 yMid
= rect
.y
+ rect
.height
/2;
1560 // This probably needs to be done
1561 // on all platforms as the icons
1562 // otherwise nearly touch the border
1567 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1569 node
= node
->GetNext(), col
++ )
1571 wxListItemData
*item
= node
->GetData();
1573 int width
= m_owner
->GetColumnWidth(col
);
1577 if ( item
->HasImage() )
1580 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1581 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1583 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1589 if ( item
->HasText() )
1590 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, width
- 8);
1594 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1595 const wxString
& textOrig
,
1601 // we don't support displaying multiple lines currently (and neither does
1602 // wxMSW FWIW) so just merge all the lines
1603 wxString
text(textOrig
);
1604 text
.Replace(_T("\n"), _T(" "));
1607 dc
->GetTextExtent(text
, &w
, &h
);
1609 const wxCoord y
= yMid
- (h
+ 1)/2;
1611 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1613 // determine if the string can fit inside the current width
1616 // it can, draw it using the items alignment
1618 m_owner
->GetColumn(col
, item
);
1619 switch ( item
.GetAlign() )
1621 case wxLIST_FORMAT_LEFT
:
1625 case wxLIST_FORMAT_RIGHT
:
1629 case wxLIST_FORMAT_CENTER
:
1630 x
+= (width
- w
) / 2;
1634 wxFAIL_MSG( _T("unknown list item format") );
1638 dc
->DrawText(text
, x
, y
);
1640 else // otherwise, truncate and add an ellipsis if possible
1642 // determine the base width
1643 wxString
ellipsis(wxT("..."));
1645 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1647 // continue until we have enough space or only one character left
1649 size_t len
= text
.length();
1650 wxString drawntext
= text
.Left(len
);
1653 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1654 drawntext
.RemoveLast();
1657 if (w
+ base_w
<= width
)
1661 // if still not enough space, remove ellipsis characters
1662 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1664 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1665 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1668 // now draw the text
1669 dc
->DrawText(drawntext
, x
, y
);
1670 dc
->DrawText(ellipsis
, x
+ w
, y
);
1674 bool wxListLineData::Highlight( bool on
)
1676 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1678 if ( on
== m_highlighted
)
1686 void wxListLineData::ReverseHighlight( void )
1688 Highlight(!IsHighlighted());
1691 //-----------------------------------------------------------------------------
1692 // wxListHeaderWindow
1693 //-----------------------------------------------------------------------------
1695 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1697 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1698 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1699 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1700 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1703 void wxListHeaderWindow::Init()
1705 m_currentCursor
= (wxCursor
*) NULL
;
1706 m_isDragging
= false;
1710 wxListHeaderWindow::wxListHeaderWindow()
1714 m_owner
= (wxListMainWindow
*) NULL
;
1715 m_resizeCursor
= (wxCursor
*) NULL
;
1718 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1720 wxListMainWindow
*owner
,
1724 const wxString
&name
)
1725 : wxWindow( win
, id
, pos
, size
, style
, name
)
1730 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1733 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1734 SetOwnForegroundColour( attr
.colFg
);
1735 SetOwnBackgroundColour( attr
.colBg
);
1737 SetOwnFont( attr
.font
);
1739 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1740 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1742 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1746 wxListHeaderWindow::~wxListHeaderWindow()
1748 delete m_resizeCursor
;
1751 #ifdef __WXUNIVERSAL__
1752 #include "wx/univ/renderer.h"
1753 #include "wx/univ/theme.h"
1756 // shift the DC origin to match the position of the main window horz
1757 // scrollbar: this allows us to always use logical coords
1758 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1761 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1764 m_owner
->GetViewStart( &view_start
, NULL
);
1769 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1771 // account for the horz scrollbar offset
1773 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1775 // Maybe we just have to check for m_signX
1776 // in the DC, but I leave the #ifdef __WXGTK__
1778 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1782 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1785 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1787 wxPaintDC
dc( this );
1792 dc
.SetFont( GetFont() );
1794 // width and height of the entire header window
1796 GetClientSize( &w
, &h
);
1797 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1799 dc
.SetBackgroundMode(wxTRANSPARENT
);
1800 dc
.SetTextForeground(GetForegroundColour());
1802 int x
= HEADER_OFFSET_X
;
1803 int numColumns
= m_owner
->GetColumnCount();
1805 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1807 m_owner
->GetColumn( i
, item
);
1808 int wCol
= item
.m_width
;
1814 if (!m_parent
->IsEnabled())
1815 flags
|= wxCONTROL_DISABLED
;
1817 // NB: The code below is not really Mac-specific, but since we are close
1818 // to 2.8 release and I don't have time to test on other platforms, I
1819 // defined this only for wxMac. If this behavior is desired on
1820 // other platforms, please go ahead and revise or remove the #ifdef.
1822 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1823 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1824 flags
|= wxCONTROL_SELECTED
;
1827 wxRendererNative::Get().DrawHeaderButton
1831 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1835 // see if we have enough space for the column label
1837 // for this we need the width of the text
1840 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1841 wLabel
+= 2 * EXTRA_WIDTH
;
1843 // and the width of the icon, if any
1844 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1845 const int image
= item
.m_image
;
1846 wxImageList
*imageList
;
1849 imageList
= m_owner
->m_small_image_list
;
1852 imageList
->GetSize(image
, ix
, iy
);
1853 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1861 // ignore alignment if there is not enough space anyhow
1863 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1866 wxFAIL_MSG( _T("unknown list item format") );
1869 case wxLIST_FORMAT_LEFT
:
1873 case wxLIST_FORMAT_RIGHT
:
1874 xAligned
= x
+ cw
- wLabel
;
1877 case wxLIST_FORMAT_CENTER
:
1878 xAligned
= x
+ (cw
- wLabel
) / 2;
1882 // draw the text and image clipping them so that they
1883 // don't overwrite the column boundary
1884 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1886 // if we have an image, draw it on the right of the label
1893 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1894 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1895 wxIMAGELIST_DRAW_TRANSPARENT
1899 dc
.DrawText( item
.GetText(),
1900 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1906 void wxListHeaderWindow::DrawCurrent()
1909 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1911 int x1
= m_currentX
;
1913 m_owner
->ClientToScreen( &x1
, &y1
);
1915 int x2
= m_currentX
;
1917 m_owner
->GetClientSize( NULL
, &y2
);
1918 m_owner
->ClientToScreen( &x2
, &y2
);
1921 dc
.SetLogicalFunction( wxINVERT
);
1922 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1923 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1927 dc
.DrawLine( x1
, y1
, x2
, y2
);
1929 dc
.SetLogicalFunction( wxCOPY
);
1931 dc
.SetPen( wxNullPen
);
1932 dc
.SetBrush( wxNullBrush
);
1936 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1938 // we want to work with logical coords
1940 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1941 int y
= event
.GetY();
1945 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1947 // we don't draw the line beyond our window, but we allow dragging it
1950 GetClientSize( &w
, NULL
);
1951 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1954 // erase the line if it was drawn
1955 if ( m_currentX
< w
)
1958 if (event
.ButtonUp())
1961 m_isDragging
= false;
1963 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1964 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1971 m_currentX
= m_minX
+ 7;
1973 // draw in the new location
1974 if ( m_currentX
< w
)
1978 else // not dragging
1981 bool hit_border
= false;
1983 // end of the current column
1986 // find the column where this event occurred
1988 countCol
= m_owner
->GetColumnCount();
1989 for (col
= 0; col
< countCol
; col
++)
1991 xpos
+= m_owner
->GetColumnWidth( col
);
1994 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1996 // near the column border
2003 // inside the column
2010 if ( col
== countCol
)
2013 if (event
.LeftDown() || event
.RightUp())
2015 if (hit_border
&& event
.LeftDown())
2017 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2018 event
.GetPosition()) )
2020 m_isDragging
= true;
2025 //else: column resizing was vetoed by the user code
2027 else // click on a column
2029 // record the selected state of the columns
2030 if (event
.LeftDown())
2032 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2035 m_owner
->GetColumn(i
, colItem
);
2036 long state
= colItem
.GetState();
2038 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2040 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2041 m_owner
->SetColumn(i
, colItem
);
2045 SendListEvent( event
.LeftDown()
2046 ? wxEVT_COMMAND_LIST_COL_CLICK
2047 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2048 event
.GetPosition());
2051 else if (event
.Moving())
2056 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2057 m_currentCursor
= m_resizeCursor
;
2061 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2062 m_currentCursor
= wxSTANDARD_CURSOR
;
2066 SetCursor(*m_currentCursor
);
2071 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2073 m_owner
->SetFocus();
2077 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2079 wxWindow
*parent
= GetParent();
2080 wxListEvent
le( type
, parent
->GetId() );
2081 le
.SetEventObject( parent
);
2082 le
.m_pointDrag
= pos
;
2084 // the position should be relative to the parent window, not
2085 // this one for compatibility with MSW and common sense: the
2086 // user code doesn't know anything at all about this header
2087 // window, so why should it get positions relative to it?
2088 le
.m_pointDrag
.y
-= GetSize().y
;
2090 le
.m_col
= m_column
;
2091 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2094 //-----------------------------------------------------------------------------
2095 // wxListRenameTimer (internal)
2096 //-----------------------------------------------------------------------------
2098 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2103 void wxListRenameTimer::Notify()
2105 m_owner
->OnRenameTimer();
2108 //-----------------------------------------------------------------------------
2109 // wxListTextCtrlWrapper (internal)
2110 //-----------------------------------------------------------------------------
2112 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2113 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2114 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2115 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2118 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2121 : m_startValue(owner
->GetItemText(itemEdit
)),
2122 m_itemEdited(itemEdit
)
2127 m_aboutToFinish
= false;
2129 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2131 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2132 &rectLabel
.x
, &rectLabel
.y
);
2134 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2135 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2136 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2139 m_text
->PushEventHandler(this);
2142 void wxListTextCtrlWrapper::Finish()
2148 m_text
->RemoveEventHandler(this);
2149 m_owner
->FinishEditing(m_text
);
2151 wxPendingDelete
.Append( this );
2155 bool wxListTextCtrlWrapper::AcceptChanges()
2157 const wxString value
= m_text
->GetValue();
2159 // notice that we should always call OnRenameAccept() to generate the "end
2160 // label editing" event, even if the user hasn't really changed anything
2161 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2163 // vetoed by the user
2167 // accepted, do rename the item (unless nothing changed)
2168 if ( value
!= m_startValue
)
2169 m_owner
->SetItemText(m_itemEdited
, value
);
2174 void wxListTextCtrlWrapper::AcceptChangesAndFinish()
2176 m_aboutToFinish
= true;
2178 // Notify the owner about the changes
2181 // Even if vetoed, close the control (consistent with MSW)
2185 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2187 switch ( event
.m_keyCode
)
2190 AcceptChangesAndFinish();
2194 m_owner
->OnRenameCancelled( m_itemEdited
);
2203 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2211 // auto-grow the textctrl:
2212 wxSize parentSize
= m_owner
->GetSize();
2213 wxPoint myPos
= m_text
->GetPosition();
2214 wxSize mySize
= m_text
->GetSize();
2216 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2217 if (myPos
.x
+ sx
> parentSize
.x
)
2218 sx
= parentSize
.x
- myPos
.x
;
2221 m_text
->SetSize(sx
, wxDefaultCoord
);
2226 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2228 if ( !m_finished
&& !m_aboutToFinish
)
2230 if ( !AcceptChanges() )
2231 m_owner
->OnRenameCancelled( m_itemEdited
);
2236 // We must let the native text control handle focus
2240 //-----------------------------------------------------------------------------
2242 //-----------------------------------------------------------------------------
2244 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2246 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2247 EVT_PAINT (wxListMainWindow::OnPaint
)
2248 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2249 EVT_CHAR (wxListMainWindow::OnChar
)
2250 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2251 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
2252 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2253 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2254 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2257 void wxListMainWindow::Init()
2262 m_lineTo
= (size_t)-1;
2268 m_small_image_list
= (wxImageList
*) NULL
;
2269 m_normal_image_list
= (wxImageList
*) NULL
;
2271 m_small_spacing
= 30;
2272 m_normal_spacing
= 40;
2276 m_isCreated
= false;
2278 m_lastOnSame
= false;
2279 m_renameTimer
= new wxListRenameTimer( this );
2280 m_textctrlWrapper
= NULL
;
2284 m_lineSelectSingleOnUp
=
2285 m_lineBeforeLastClicked
= (size_t)-1;
2290 wxListMainWindow::wxListMainWindow()
2295 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2298 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2303 const wxString
&name
)
2304 : wxScrolledWindow( parent
, id
, pos
, size
,
2305 style
| wxHSCROLL
| wxVSCROLL
, name
)
2309 m_highlightBrush
= new wxBrush
2311 wxSystemSettings::GetColour
2313 wxSYS_COLOUR_HIGHLIGHT
2318 m_highlightUnfocusedBrush
= new wxBrush
2320 wxSystemSettings::GetColour
2322 wxSYS_COLOUR_BTNSHADOW
2327 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2329 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2330 SetOwnForegroundColour( attr
.colFg
);
2331 SetOwnBackgroundColour( attr
.colBg
);
2333 SetOwnFont( attr
.font
);
2336 wxListMainWindow::~wxListMainWindow()
2339 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2340 WX_CLEAR_ARRAY(m_aColWidths
);
2342 delete m_highlightBrush
;
2343 delete m_highlightUnfocusedBrush
;
2344 delete m_renameTimer
;
2347 void wxListMainWindow::CacheLineData(size_t line
)
2349 wxGenericListCtrl
*listctrl
= GetListCtrl();
2351 wxListLineData
*ld
= GetDummyLine();
2353 size_t countCol
= GetColumnCount();
2354 for ( size_t col
= 0; col
< countCol
; col
++ )
2356 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2357 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2360 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2363 wxListLineData
*wxListMainWindow::GetDummyLine() const
2365 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2366 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2368 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2370 // we need to recreate the dummy line if the number of columns in the
2371 // control changed as it would have the incorrect number of fields
2373 if ( !m_lines
.IsEmpty() &&
2374 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2376 self
->m_lines
.Clear();
2379 if ( m_lines
.IsEmpty() )
2381 wxListLineData
*line
= new wxListLineData(self
);
2382 self
->m_lines
.Add(line
);
2384 // don't waste extra memory -- there never going to be anything
2385 // else/more in this array
2386 self
->m_lines
.Shrink();
2392 // ----------------------------------------------------------------------------
2393 // line geometry (report mode only)
2394 // ----------------------------------------------------------------------------
2396 wxCoord
wxListMainWindow::GetLineHeight() const
2398 // we cache the line height as calling GetTextExtent() is slow
2399 if ( !m_lineHeight
)
2401 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2403 wxClientDC
dc( self
);
2404 dc
.SetFont( GetFont() );
2407 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2409 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2412 m_small_image_list
->GetSize(0, iw
, ih
);
2417 self
->m_lineHeight
= y
+ LINE_SPACING
;
2420 return m_lineHeight
;
2423 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2425 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2427 return LINE_SPACING
+ line
* GetLineHeight();
2430 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2432 if ( !InReportView() )
2433 return GetLine(line
)->m_gi
->m_rectAll
;
2436 rect
.x
= HEADER_OFFSET_X
;
2437 rect
.y
= GetLineY(line
);
2438 rect
.width
= GetHeaderWidth();
2439 rect
.height
= GetLineHeight();
2444 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2446 if ( !InReportView() )
2447 return GetLine(line
)->m_gi
->m_rectLabel
;
2450 wxListLineData
*data
= GetLine(line
);
2451 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
2454 wxListItemData
*item
= node
->GetData();
2455 if ( item
->HasImage() )
2458 GetImageSize( item
->GetImage(), ix
, iy
);
2459 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
2464 rect
.x
= image_x
+ HEADER_OFFSET_X
;
2465 rect
.y
= GetLineY(line
);
2466 rect
.width
= GetColumnWidth(0) - image_x
;
2467 rect
.height
= GetLineHeight();
2472 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2474 if ( !InReportView() )
2475 return GetLine(line
)->m_gi
->m_rectIcon
;
2477 wxListLineData
*ld
= GetLine(line
);
2478 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2481 rect
.x
= HEADER_OFFSET_X
;
2482 rect
.y
= GetLineY(line
);
2483 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2488 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2490 return InReportView() ? GetLineRect(line
)
2491 : GetLine(line
)->m_gi
->m_rectHighlight
;
2494 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2496 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2498 wxListLineData
*ld
= GetLine(line
);
2500 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2501 return wxLIST_HITTEST_ONITEMICON
;
2503 // VS: Testing for "ld->HasText() || InReportView()" instead of
2504 // "ld->HasText()" is needed to make empty lines in report view
2506 if ( ld
->HasText() || InReportView() )
2508 wxRect rect
= InReportView() ? GetLineRect(line
)
2509 : GetLineLabelRect(line
);
2511 if ( rect
.Contains(x
, y
) )
2512 return wxLIST_HITTEST_ONITEMLABEL
;
2518 // ----------------------------------------------------------------------------
2519 // highlight (selection) handling
2520 // ----------------------------------------------------------------------------
2522 bool wxListMainWindow::IsHighlighted(size_t line
) const
2526 return m_selStore
.IsSelected(line
);
2530 wxListLineData
*ld
= GetLine(line
);
2531 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2533 return ld
->IsHighlighted();
2537 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2543 wxArrayInt linesChanged
;
2544 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2547 // meny items changed state, refresh everything
2548 RefreshLines(lineFrom
, lineTo
);
2550 else // only a few items changed state, refresh only them
2552 size_t count
= linesChanged
.GetCount();
2553 for ( size_t n
= 0; n
< count
; n
++ )
2555 RefreshLine(linesChanged
[n
]);
2559 else // iterate over all items in non report view
2561 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2563 if ( HighlightLine(line
, highlight
) )
2569 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2575 changed
= m_selStore
.SelectItem(line
, highlight
);
2579 wxListLineData
*ld
= GetLine(line
);
2580 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2582 changed
= ld
->Highlight(highlight
);
2587 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2588 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2594 void wxListMainWindow::RefreshLine( size_t line
)
2596 if ( InReportView() )
2598 size_t visibleFrom
, visibleTo
;
2599 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2601 if ( line
< visibleFrom
|| line
> visibleTo
)
2605 wxRect rect
= GetLineRect(line
);
2607 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2608 RefreshRect( rect
);
2611 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2613 // we suppose that they are ordered by caller
2614 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2616 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2618 if ( InReportView() )
2620 size_t visibleFrom
, visibleTo
;
2621 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2623 if ( lineFrom
< visibleFrom
)
2624 lineFrom
= visibleFrom
;
2625 if ( lineTo
> visibleTo
)
2630 rect
.y
= GetLineY(lineFrom
);
2631 rect
.width
= GetClientSize().x
;
2632 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2634 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2635 RefreshRect( rect
);
2639 // TODO: this should be optimized...
2640 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2647 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2649 if ( InReportView() )
2651 size_t visibleFrom
, visibleTo
;
2652 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2654 if ( lineFrom
< visibleFrom
)
2655 lineFrom
= visibleFrom
;
2656 else if ( lineFrom
> visibleTo
)
2661 rect
.y
= GetLineY(lineFrom
);
2662 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2664 wxSize size
= GetClientSize();
2665 rect
.width
= size
.x
;
2667 // refresh till the bottom of the window
2668 rect
.height
= size
.y
- rect
.y
;
2670 RefreshRect( rect
);
2674 // TODO: how to do it more efficiently?
2679 void wxListMainWindow::RefreshSelected()
2685 if ( InReportView() )
2687 GetVisibleLinesRange(&from
, &to
);
2692 to
= GetItemCount() - 1;
2695 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2696 RefreshLine(m_current
);
2698 for ( size_t line
= from
; line
<= to
; line
++ )
2700 // NB: the test works as expected even if m_current == -1
2701 if ( line
!= m_current
&& IsHighlighted(line
) )
2706 void wxListMainWindow::Freeze()
2711 void wxListMainWindow::Thaw()
2713 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2715 if ( --m_freezeCount
== 0 )
2719 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2721 // Note: a wxPaintDC must be constructed even if no drawing is
2722 // done (a Windows requirement).
2723 wxPaintDC
dc( this );
2725 if ( IsEmpty() || m_freezeCount
)
2726 // nothing to draw or not the moment to draw it
2730 // delay the repainting until we calculate all the items positions
2736 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2738 dc
.SetFont( GetFont() );
2740 if ( InReportView() )
2742 int lineHeight
= GetLineHeight();
2744 size_t visibleFrom
, visibleTo
;
2745 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2748 int xOrig
= dc
.LogicalToDeviceX( 0 );
2749 int yOrig
= dc
.LogicalToDeviceY( 0 );
2751 // tell the caller cache to cache the data
2754 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2755 GetParent()->GetId());
2756 evCache
.SetEventObject( GetParent() );
2757 evCache
.m_oldItemIndex
= visibleFrom
;
2758 evCache
.m_itemIndex
= visibleTo
;
2759 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2762 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2764 rectLine
= GetLineRect(line
);
2767 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2768 rectLine
.width
, rectLine
.height
) )
2770 // don't redraw unaffected lines to avoid flicker
2774 GetLine(line
)->DrawInReportMode( &dc
,
2776 GetLineHighlightRect(line
),
2777 IsHighlighted(line
) );
2780 if ( HasFlag(wxLC_HRULES
) )
2782 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2783 wxSize clientSize
= GetClientSize();
2785 size_t i
= visibleFrom
;
2786 if (i
== 0) i
= 1; // Don't draw the first one
2787 for ( ; i
<= visibleTo
; i
++ )
2790 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2791 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2792 clientSize
.x
- dev_x
, i
* lineHeight
);
2795 // Draw last horizontal rule
2796 if ( visibleTo
== GetItemCount() - 1 )
2799 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2800 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2801 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2805 // Draw vertical rules if required
2806 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2808 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2809 wxRect firstItemRect
, lastItemRect
;
2811 GetItemRect(visibleFrom
, firstItemRect
);
2812 GetItemRect(visibleTo
, lastItemRect
);
2813 int x
= firstItemRect
.GetX();
2815 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2817 for (int col
= 0; col
< GetColumnCount(); col
++)
2819 int colWidth
= GetColumnWidth(col
);
2821 int x_pos
= x
- dev_x
;
2822 if (col
< GetColumnCount()-1) x_pos
-= 2;
2823 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2824 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2830 size_t count
= GetItemCount();
2831 for ( size_t i
= 0; i
< count
; i
++ )
2833 GetLine(i
)->Draw( &dc
);
2838 // Don't draw rect outline under Mac at all.
2843 wxRect
rect( GetLineHighlightRect( m_current
) );
2845 dc
.SetPen( *wxBLACK_PEN
);
2846 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2847 dc
.DrawRectangle( rect
);
2849 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2857 void wxListMainWindow::HighlightAll( bool on
)
2859 if ( IsSingleSel() )
2861 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2863 // we just have one item to turn off
2864 if ( HasCurrent() && IsHighlighted(m_current
) )
2866 HighlightLine(m_current
, false);
2867 RefreshLine(m_current
);
2870 else // multi selection
2873 HighlightLines(0, GetItemCount() - 1, on
);
2877 void wxListMainWindow::SendNotify( size_t line
,
2878 wxEventType command
,
2879 const wxPoint
& point
)
2881 wxListEvent
le( command
, GetParent()->GetId() );
2882 le
.SetEventObject( GetParent() );
2884 le
.m_itemIndex
= line
;
2886 // set only for events which have position
2887 if ( point
!= wxDefaultPosition
)
2888 le
.m_pointDrag
= point
;
2890 // don't try to get the line info for virtual list controls: the main
2891 // program has it anyhow and if we did it would result in accessing all
2892 // the lines, even those which are not visible now and this is precisely
2893 // what we're trying to avoid
2896 if ( line
!= (size_t)-1 )
2898 GetLine(line
)->GetItem( 0, le
.m_item
);
2900 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2902 //else: there may be no more such item
2904 GetParent()->GetEventHandler()->ProcessEvent( le
);
2907 void wxListMainWindow::ChangeCurrent(size_t current
)
2909 m_current
= current
;
2911 // as the current item changed, we shouldn't start editing it when the
2912 // "slow click" timer expires as the click happened on another item
2913 if ( m_renameTimer
->IsRunning() )
2914 m_renameTimer
->Stop();
2916 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2919 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2921 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2922 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2924 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2925 wxT("EditLabel() needs a text control") );
2927 size_t itemEdit
= (size_t)item
;
2929 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2930 le
.SetEventObject( GetParent() );
2931 le
.m_itemIndex
= item
;
2932 wxListLineData
*data
= GetLine(itemEdit
);
2933 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2934 data
->GetItem( 0, le
.m_item
);
2936 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2938 // vetoed by user code
2942 // We have to call this here because the label in question might just have
2943 // been added and no screen update taken place.
2948 // Pending events dispatched by wxSafeYield might have changed the item
2950 if ( (size_t)item
>= GetItemCount() )
2954 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2955 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2956 return m_textctrlWrapper
->GetText();
2959 void wxListMainWindow::OnRenameTimer()
2961 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2963 EditLabel( m_current
);
2966 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2968 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2969 le
.SetEventObject( GetParent() );
2970 le
.m_itemIndex
= itemEdit
;
2972 wxListLineData
*data
= GetLine(itemEdit
);
2974 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2976 data
->GetItem( 0, le
.m_item
);
2977 le
.m_item
.m_text
= value
;
2978 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2982 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2984 // let owner know that the edit was cancelled
2985 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2987 le
.SetEditCanceled(true);
2989 le
.SetEventObject( GetParent() );
2990 le
.m_itemIndex
= itemEdit
;
2992 wxListLineData
*data
= GetLine(itemEdit
);
2993 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2995 data
->GetItem( 0, le
.m_item
);
2996 GetEventHandler()->ProcessEvent( le
);
2999 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
3003 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
3004 // shutdown the edit control when the mouse is clicked elsewhere on the
3005 // listctrl because the order of events is different (or something like
3006 // that), so explicitly end the edit if it is active.
3007 if ( event
.LeftDown() && m_textctrlWrapper
)
3008 m_textctrlWrapper
->AcceptChangesAndFinish();
3011 if ( event
.LeftDown() )
3012 SetFocusIgnoringChildren();
3014 event
.SetEventObject( GetParent() );
3015 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3018 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3020 // let the base handle mouse wheel events.
3025 if ( !HasCurrent() || IsEmpty() )
3027 if (event
.RightDown())
3029 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3031 wxContextMenuEvent
evtCtx(
3033 GetParent()->GetId(),
3034 ClientToScreen(event
.GetPosition()));
3035 evtCtx
.SetEventObject(GetParent());
3036 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3044 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3045 event
.ButtonDClick()) )
3048 int x
= event
.GetX();
3049 int y
= event
.GetY();
3050 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3052 // where did we hit it (if we did)?
3055 size_t count
= GetItemCount(),
3058 if ( InReportView() )
3060 current
= y
/ GetLineHeight();
3061 if ( current
< count
)
3062 hitResult
= HitTestLine(current
, x
, y
);
3066 // TODO: optimize it too! this is less simple than for report view but
3067 // enumerating all items is still not a way to do it!!
3068 for ( current
= 0; current
< count
; current
++ )
3070 hitResult
= HitTestLine(current
, x
, y
);
3076 if (event
.Dragging())
3078 if (m_dragCount
== 0)
3080 // we have to report the raw, physical coords as we want to be
3081 // able to call HitTest(event.m_pointDrag) from the user code to
3082 // get the item being dragged
3083 m_dragStart
= event
.GetPosition();
3088 if (m_dragCount
!= 3)
3091 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3092 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3094 wxListEvent
le( command
, GetParent()->GetId() );
3095 le
.SetEventObject( GetParent() );
3096 le
.m_itemIndex
= m_lineLastClicked
;
3097 le
.m_pointDrag
= m_dragStart
;
3098 GetParent()->GetEventHandler()->ProcessEvent( le
);
3109 // outside of any item
3110 if (event
.RightDown())
3112 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3114 wxContextMenuEvent
evtCtx(
3116 GetParent()->GetId(),
3117 ClientToScreen(event
.GetPosition()));
3118 evtCtx
.SetEventObject(GetParent());
3119 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3123 // reset the selection and bail out
3124 HighlightAll(false);
3130 bool forceClick
= false;
3131 if (event
.ButtonDClick())
3133 if ( m_renameTimer
->IsRunning() )
3134 m_renameTimer
->Stop();
3136 m_lastOnSame
= false;
3138 if ( current
== m_lineLastClicked
)
3140 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3146 // The first click was on another item, so don't interpret this as
3147 // a double click, but as a simple click instead
3154 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3156 // select single line
3157 HighlightAll( false );
3158 ReverseHighlight(m_lineSelectSingleOnUp
);
3163 if ((current
== m_current
) &&
3164 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3165 HasFlag(wxLC_EDIT_LABELS
) )
3167 if ( !InReportView() ||
3168 GetLineLabelRect(current
).Contains(x
, y
) )
3170 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
3171 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
3176 m_lastOnSame
= false;
3177 m_lineSelectSingleOnUp
= (size_t)-1;
3181 // This is necessary, because after a DnD operation in
3182 // from and to ourself, the up event is swallowed by the
3183 // DnD code. So on next non-up event (which means here and
3184 // now) m_lineSelectSingleOnUp should be reset.
3185 m_lineSelectSingleOnUp
= (size_t)-1;
3187 if (event
.RightDown())
3189 m_lineBeforeLastClicked
= m_lineLastClicked
;
3190 m_lineLastClicked
= current
;
3192 // If the item is already selected, do not update the selection.
3193 // Multi-selections should not be cleared if a selected item is clicked.
3194 if (!IsHighlighted(current
))
3196 HighlightAll(false);
3197 ChangeCurrent(current
);
3198 ReverseHighlight(m_current
);
3201 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3203 // Allow generation of context menu event
3206 else if (event
.MiddleDown())
3208 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3210 else if ( event
.LeftDown() || forceClick
)
3212 m_lineBeforeLastClicked
= m_lineLastClicked
;
3213 m_lineLastClicked
= current
;
3215 size_t oldCurrent
= m_current
;
3216 bool oldWasSelected
= IsHighlighted(m_current
);
3218 bool cmdModifierDown
= event
.CmdDown();
3219 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3221 if ( IsSingleSel() || !IsHighlighted(current
) )
3223 HighlightAll( false );
3225 ChangeCurrent(current
);
3227 ReverseHighlight(m_current
);
3229 else // multi sel & current is highlighted & no mod keys
3231 m_lineSelectSingleOnUp
= current
;
3232 ChangeCurrent(current
); // change focus
3235 else // multi sel & either ctrl or shift is down
3237 if (cmdModifierDown
)
3239 ChangeCurrent(current
);
3241 ReverseHighlight(m_current
);
3243 else if (event
.ShiftDown())
3245 ChangeCurrent(current
);
3247 size_t lineFrom
= oldCurrent
,
3250 if ( lineTo
< lineFrom
)
3253 lineFrom
= m_current
;
3256 HighlightLines(lineFrom
, lineTo
);
3258 else // !ctrl, !shift
3260 // test in the enclosing if should make it impossible
3261 wxFAIL_MSG( _T("how did we get here?") );
3265 if (m_current
!= oldCurrent
)
3266 RefreshLine( oldCurrent
);
3268 // forceClick is only set if the previous click was on another item
3269 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3273 void wxListMainWindow::MoveToItem(size_t item
)
3275 if ( item
== (size_t)-1 )
3278 wxRect rect
= GetLineRect(item
);
3280 int client_w
, client_h
;
3281 GetClientSize( &client_w
, &client_h
);
3283 const int hLine
= GetLineHeight();
3285 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3286 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3288 if ( InReportView() )
3290 // the next we need the range of lines shown it might be different,
3291 // so recalculate it
3292 ResetVisibleLinesRange();
3294 if (rect
.y
< view_y
)
3295 Scroll( -1, rect
.y
/ hLine
);
3296 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3297 Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3300 // At least on Mac the visible lines value will get reset inside of
3301 // Scroll *before* it actually scrolls the window because of the
3302 // Update() that happens there, so it will still have the wrong value.
3303 // So let's reset it again and wait for it to be recalculated in the
3304 // next paint event. I would expect this problem to show up in wxGTK
3305 // too but couldn't duplicate it there. Perhaps the order of events
3306 // is different... --Robin
3307 ResetVisibleLinesRange();
3312 if (rect
.x
-view_x
< 5)
3313 Scroll( (rect
.x
- 5) / SCROLL_UNIT_X
, -1 );
3314 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3315 Scroll( (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
, -1 );
3319 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
3321 if ( !InReportView() )
3323 // TODO: this should work in all views but is not implemented now
3328 GetVisibleLinesRange(&top
, &bottom
);
3330 if ( bottom
== (size_t)-1 )
3333 ResetVisibleLinesRange();
3335 int hLine
= GetLineHeight();
3337 Scroll(-1, top
+ dy
/ hLine
);
3340 // see comment in MoveToItem() for why we do this
3341 ResetVisibleLinesRange();
3347 // ----------------------------------------------------------------------------
3348 // keyboard handling
3349 // ----------------------------------------------------------------------------
3351 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3353 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3354 _T("invalid item index in OnArrowChar()") );
3356 size_t oldCurrent
= m_current
;
3358 // in single selection we just ignore Shift as we can't select several
3360 if ( event
.ShiftDown() && !IsSingleSel() )
3362 ChangeCurrent(newCurrent
);
3364 // refresh the old focus to remove it
3365 RefreshLine( oldCurrent
);
3367 // select all the items between the old and the new one
3368 if ( oldCurrent
> newCurrent
)
3370 newCurrent
= oldCurrent
;
3371 oldCurrent
= m_current
;
3374 HighlightLines(oldCurrent
, newCurrent
);
3378 // all previously selected items are unselected unless ctrl is held
3379 // in a multiselection control
3380 if ( !event
.ControlDown() || IsSingleSel() )
3381 HighlightAll(false);
3383 ChangeCurrent(newCurrent
);
3385 // refresh the old focus to remove it
3386 RefreshLine( oldCurrent
);
3388 // in single selection mode we must always have a selected item
3389 if ( !event
.ControlDown() || IsSingleSel() )
3390 HighlightLine( m_current
, true );
3393 RefreshLine( m_current
);
3398 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3400 wxWindow
*parent
= GetParent();
3402 // propagate the key event upwards
3403 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3404 ke
.m_shiftDown
= event
.m_shiftDown
;
3405 ke
.m_controlDown
= event
.m_controlDown
;
3406 ke
.m_altDown
= event
.m_altDown
;
3407 ke
.m_metaDown
= event
.m_metaDown
;
3408 ke
.m_keyCode
= event
.m_keyCode
;
3411 ke
.SetEventObject( parent
);
3412 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3417 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3419 wxWindow
*parent
= GetParent();
3421 // propagate the key event upwards
3422 wxKeyEvent
ke( wxEVT_KEY_UP
);
3423 ke
.m_shiftDown
= event
.m_shiftDown
;
3424 ke
.m_controlDown
= event
.m_controlDown
;
3425 ke
.m_altDown
= event
.m_altDown
;
3426 ke
.m_metaDown
= event
.m_metaDown
;
3427 ke
.m_keyCode
= event
.m_keyCode
;
3430 ke
.SetEventObject( parent
);
3431 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3436 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3438 wxWindow
*parent
= GetParent();
3440 // send a list_key event up
3443 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3444 le
.m_itemIndex
= m_current
;
3445 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3446 le
.m_code
= event
.GetKeyCode();
3447 le
.SetEventObject( parent
);
3448 parent
->GetEventHandler()->ProcessEvent( le
);
3451 // propagate the char event upwards
3452 wxKeyEvent
ke( wxEVT_CHAR
);
3453 ke
.m_shiftDown
= event
.m_shiftDown
;
3454 ke
.m_controlDown
= event
.m_controlDown
;
3455 ke
.m_altDown
= event
.m_altDown
;
3456 ke
.m_metaDown
= event
.m_metaDown
;
3457 ke
.m_keyCode
= event
.m_keyCode
;
3460 ke
.SetEventObject( parent
);
3461 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3463 if (event
.GetKeyCode() == WXK_TAB
)
3465 wxNavigationKeyEvent nevent
;
3466 nevent
.SetWindowChange( event
.ControlDown() );
3467 nevent
.SetDirection( !event
.ShiftDown() );
3468 nevent
.SetEventObject( GetParent()->GetParent() );
3469 nevent
.SetCurrentFocus( m_parent
);
3470 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3474 // no item -> nothing to do
3481 // don't use m_linesPerPage directly as it might not be computed yet
3482 const int pageSize
= GetCountPerPage();
3483 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3485 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3487 if (event
.GetKeyCode() == WXK_RIGHT
)
3488 event
.m_keyCode
= WXK_LEFT
;
3489 else if (event
.GetKeyCode() == WXK_LEFT
)
3490 event
.m_keyCode
= WXK_RIGHT
;
3493 switch ( event
.GetKeyCode() )
3496 if ( m_current
> 0 )
3497 OnArrowChar( m_current
- 1, event
);
3501 if ( m_current
< (size_t)GetItemCount() - 1 )
3502 OnArrowChar( m_current
+ 1, event
);
3507 OnArrowChar( GetItemCount() - 1, event
);
3512 OnArrowChar( 0, event
);
3517 int steps
= InReportView() ? pageSize
- 1
3518 : m_current
% pageSize
;
3520 int index
= m_current
- steps
;
3524 OnArrowChar( index
, event
);
3530 int steps
= InReportView()
3532 : pageSize
- (m_current
% pageSize
) - 1;
3534 size_t index
= m_current
+ steps
;
3535 size_t count
= GetItemCount();
3536 if ( index
>= count
)
3539 OnArrowChar( index
, event
);
3544 if ( !InReportView() )
3546 int index
= m_current
- pageSize
;
3550 OnArrowChar( index
, event
);
3555 if ( !InReportView() )
3557 size_t index
= m_current
+ pageSize
;
3559 size_t count
= GetItemCount();
3560 if ( index
>= count
)
3563 OnArrowChar( index
, event
);
3568 if ( IsSingleSel() )
3570 if ( event
.ControlDown() )
3572 ReverseHighlight(m_current
);
3574 else // normal space press
3576 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3579 else // multiple selection
3581 ReverseHighlight(m_current
);
3587 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3595 // ----------------------------------------------------------------------------
3597 // ----------------------------------------------------------------------------
3599 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3603 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3604 event
.SetEventObject( GetParent() );
3605 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3609 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3610 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3611 // which are already drawn correctly resulting in horrible flicker - avoid
3621 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3625 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3626 event
.SetEventObject( GetParent() );
3627 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3635 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3637 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3639 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3641 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3643 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3645 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3647 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3649 else if ( InReportView() && (m_small_image_list
))
3651 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3655 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3657 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3659 m_normal_image_list
->GetSize( index
, width
, height
);
3661 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3663 m_small_image_list
->GetSize( index
, width
, height
);
3665 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3667 m_small_image_list
->GetSize( index
, width
, height
);
3669 else if ( InReportView() && m_small_image_list
)
3671 m_small_image_list
->GetSize( index
, width
, height
);
3680 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3682 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3683 dc
.SetFont( GetFont() );
3686 dc
.GetTextExtent( s
, &lw
, NULL
);
3688 return lw
+ AUTOSIZE_COL_MARGIN
;
3691 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3695 // calc the spacing from the icon size
3696 int width
= 0, height
= 0;
3698 if ((imageList
) && (imageList
->GetImageCount()) )
3699 imageList
->GetSize(0, width
, height
);
3701 if (which
== wxIMAGE_LIST_NORMAL
)
3703 m_normal_image_list
= imageList
;
3704 m_normal_spacing
= width
+ 8;
3707 if (which
== wxIMAGE_LIST_SMALL
)
3709 m_small_image_list
= imageList
;
3710 m_small_spacing
= width
+ 14;
3711 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3715 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3719 m_small_spacing
= spacing
;
3721 m_normal_spacing
= spacing
;
3724 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3726 return isSmall
? m_small_spacing
: m_normal_spacing
;
3729 // ----------------------------------------------------------------------------
3731 // ----------------------------------------------------------------------------
3733 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3735 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3737 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3739 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3740 item
.m_width
= GetTextLength( item
.m_text
);
3742 wxListHeaderData
*column
= node
->GetData();
3743 column
->SetItem( item
);
3745 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3747 headerWin
->m_dirty
= true;
3751 // invalidate it as it has to be recalculated
3755 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3757 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3758 _T("invalid column index") );
3760 wxCHECK_RET( InReportView(),
3761 _T("SetColumnWidth() can only be called in report mode.") );
3764 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3766 headerWin
->m_dirty
= true;
3768 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3769 wxCHECK_RET( node
, _T("no column?") );
3771 wxListHeaderData
*column
= node
->GetData();
3773 size_t count
= GetItemCount();
3775 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3777 width
= GetTextLength(column
->GetText());
3778 width
+= 2*EXTRA_WIDTH
;
3780 // check for column header's image availability
3781 const int image
= column
->GetImage();
3784 if ( m_small_image_list
)
3787 m_small_image_list
->GetSize(image
, ix
, iy
);
3788 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3792 else if ( width
== wxLIST_AUTOSIZE
)
3796 // TODO: determine the max width somehow...
3797 width
= WIDTH_COL_DEFAULT
;
3801 wxClientDC
dc(this);
3802 dc
.SetFont( GetFont() );
3804 int max
= AUTOSIZE_COL_MARGIN
;
3806 // if the cached column width isn't valid then recalculate it
3807 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3809 for (size_t i
= 0; i
< count
; i
++)
3811 wxListLineData
*line
= GetLine( i
);
3812 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3814 wxCHECK_RET( n
, _T("no subitem?") );
3816 wxListItemData
*itemData
= n
->GetData();
3819 itemData
->GetItem(item
);
3820 int itemWidth
= GetItemWidthWithImage(&item
);
3821 if (itemWidth
> max
)
3825 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3826 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3829 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3830 width
= max
+ AUTOSIZE_COL_MARGIN
;
3834 column
->SetWidth( width
);
3836 // invalidate it as it has to be recalculated
3840 int wxListMainWindow::GetHeaderWidth() const
3842 if ( !m_headerWidth
)
3844 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3846 size_t count
= GetColumnCount();
3847 for ( size_t col
= 0; col
< count
; col
++ )
3849 self
->m_headerWidth
+= GetColumnWidth(col
);
3853 return m_headerWidth
;
3856 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3858 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3859 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3861 wxListHeaderData
*column
= node
->GetData();
3862 column
->GetItem( item
);
3865 int wxListMainWindow::GetColumnWidth( int col
) const
3867 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3868 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3870 wxListHeaderData
*column
= node
->GetData();
3871 return column
->GetWidth();
3874 // ----------------------------------------------------------------------------
3876 // ----------------------------------------------------------------------------
3878 void wxListMainWindow::SetItem( wxListItem
&item
)
3880 long id
= item
.m_itemId
;
3881 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3882 _T("invalid item index in SetItem") );
3886 wxListLineData
*line
= GetLine((size_t)id
);
3887 line
->SetItem( item
.m_col
, item
);
3889 // Set item state if user wants
3890 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3891 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3895 // update the Max Width Cache if needed
3896 int width
= GetItemWidthWithImage(&item
);
3898 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3899 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3903 // update the item on screen
3905 GetItemRect(id
, rectItem
);
3906 RefreshRect(rectItem
);
3909 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3914 // first deal with selection
3915 if ( stateMask
& wxLIST_STATE_SELECTED
)
3917 // set/clear select state
3920 // optimized version for virtual listctrl.
3921 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3924 else if ( state
& wxLIST_STATE_SELECTED
)
3926 const long count
= GetItemCount();
3927 for( long i
= 0; i
< count
; i
++ )
3929 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3935 // clear for non virtual (somewhat optimized by using GetNextItem())
3937 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3939 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3944 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3946 // unfocus all: only one item can be focussed, so clearing focus for
3947 // all items is simply clearing focus of the focussed item.
3948 SetItemState(m_current
, state
, stateMask
);
3950 //(setting focus to all items makes no sense, so it is not handled here.)
3953 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3957 SetItemStateAll(state
, stateMask
);
3961 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3962 _T("invalid list ctrl item index in SetItem") );
3964 size_t oldCurrent
= m_current
;
3965 size_t item
= (size_t)litem
; // safe because of the check above
3967 // do we need to change the focus?
3968 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3970 if ( state
& wxLIST_STATE_FOCUSED
)
3972 // don't do anything if this item is already focused
3973 if ( item
!= m_current
)
3975 ChangeCurrent(item
);
3977 if ( oldCurrent
!= (size_t)-1 )
3979 if ( IsSingleSel() )
3981 HighlightLine(oldCurrent
, false);
3984 RefreshLine(oldCurrent
);
3987 RefreshLine( m_current
);
3992 // don't do anything if this item is not focused
3993 if ( item
== m_current
)
3997 if ( IsSingleSel() )
3999 // we must unselect the old current item as well or we
4000 // might end up with more than one selected item in a
4001 // single selection control
4002 HighlightLine(oldCurrent
, false);
4005 RefreshLine( oldCurrent
);
4010 // do we need to change the selection state?
4011 if ( stateMask
& wxLIST_STATE_SELECTED
)
4013 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
4015 if ( IsSingleSel() )
4019 // selecting the item also makes it the focused one in the
4021 if ( m_current
!= item
)
4023 ChangeCurrent(item
);
4025 if ( oldCurrent
!= (size_t)-1 )
4027 HighlightLine( oldCurrent
, false );
4028 RefreshLine( oldCurrent
);
4034 // only the current item may be selected anyhow
4035 if ( item
!= m_current
)
4040 if ( HighlightLine(item
, on
) )
4047 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
4049 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
4050 _T("invalid list ctrl item index in GetItemState()") );
4052 int ret
= wxLIST_STATE_DONTCARE
;
4054 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4056 if ( (size_t)item
== m_current
)
4057 ret
|= wxLIST_STATE_FOCUSED
;
4060 if ( stateMask
& wxLIST_STATE_SELECTED
)
4062 if ( IsHighlighted(item
) )
4063 ret
|= wxLIST_STATE_SELECTED
;
4069 void wxListMainWindow::GetItem( wxListItem
&item
) const
4071 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
4072 _T("invalid item index in GetItem") );
4074 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4075 line
->GetItem( item
.m_col
, item
);
4077 // Get item state if user wants it
4078 if ( item
.m_mask
& wxLIST_MASK_STATE
)
4079 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
4080 wxLIST_STATE_FOCUSED
);
4083 // ----------------------------------------------------------------------------
4085 // ----------------------------------------------------------------------------
4087 size_t wxListMainWindow::GetItemCount() const
4089 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4092 void wxListMainWindow::SetItemCount(long count
)
4094 m_selStore
.SetItemCount(count
);
4095 m_countVirt
= count
;
4097 ResetVisibleLinesRange();
4099 // scrollbars must be reset
4103 int wxListMainWindow::GetSelectedItemCount() const
4105 // deal with the quick case first
4106 if ( IsSingleSel() )
4107 return HasCurrent() ? IsHighlighted(m_current
) : false;
4109 // virtual controls remmebers all its selections itself
4111 return m_selStore
.GetSelectedCount();
4113 // TODO: we probably should maintain the number of items selected even for
4114 // non virtual controls as enumerating all lines is really slow...
4115 size_t countSel
= 0;
4116 size_t count
= GetItemCount();
4117 for ( size_t line
= 0; line
< count
; line
++ )
4119 if ( GetLine(line
)->IsHighlighted() )
4126 // ----------------------------------------------------------------------------
4127 // item position/size
4128 // ----------------------------------------------------------------------------
4130 wxRect
wxListMainWindow::GetViewRect() const
4132 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
4133 _T("wxListCtrl::GetViewRect() only works in icon mode") );
4135 // we need to find the longest/tallest label
4136 wxCoord xMax
= 0, yMax
= 0;
4137 const int count
= GetItemCount();
4140 for ( int i
= 0; i
< count
; i
++ )
4145 wxCoord x
= r
.GetRight(),
4155 // some fudge needed to make it look prettier
4156 xMax
+= 2 * EXTRA_BORDER_X
;
4157 yMax
+= 2 * EXTRA_BORDER_Y
;
4159 // account for the scrollbars if necessary
4160 const wxSize sizeAll
= GetClientSize();
4161 if ( xMax
> sizeAll
.x
)
4162 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4163 if ( yMax
> sizeAll
.y
)
4164 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4166 return wxRect(0, 0, xMax
, yMax
);
4169 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
4171 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4172 _T("invalid index in GetItemRect") );
4174 // ensure that we're laid out, otherwise we could return nonsense
4177 wxConstCast(this, wxListMainWindow
)->
4178 RecalculatePositions(true /* no refresh */);
4181 rect
= GetLineRect((size_t)index
);
4183 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4186 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4189 GetItemRect(item
, rect
);
4197 // ----------------------------------------------------------------------------
4198 // geometry calculation
4199 // ----------------------------------------------------------------------------
4201 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4203 const int lineHeight
= GetLineHeight();
4205 wxClientDC
dc( this );
4206 dc
.SetFont( GetFont() );
4208 const size_t count
= GetItemCount();
4211 if ( HasFlag(wxLC_ICON
) )
4212 iconSpacing
= m_normal_spacing
;
4213 else if ( HasFlag(wxLC_SMALL_ICON
) )
4214 iconSpacing
= m_small_spacing
;
4218 // Note that we do not call GetClientSize() here but
4219 // GetSize() and subtract the border size for sunken
4220 // borders manually. This is technically incorrect,
4221 // but we need to know the client area's size WITHOUT
4222 // scrollbars here. Since we don't know if there are
4223 // any scrollbars, we use GetSize() instead. Another
4224 // solution would be to call SetScrollbars() here to
4225 // remove the scrollbars and call GetClientSize() then,
4226 // but this might result in flicker and - worse - will
4227 // reset the scrollbars to 0 which is not good at all
4228 // if you resize a dialog/window, but don't want to
4229 // reset the window scrolling. RR.
4230 // Furthermore, we actually do NOT subtract the border
4231 // width as 2 pixels is just the extra space which we
4232 // need around the actual content in the window. Other-
4233 // wise the text would e.g. touch the upper border. RR.
4236 GetSize( &clientWidth
, &clientHeight
);
4238 if ( InReportView() )
4240 // all lines have the same height and we scroll one line per step
4241 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4243 m_linesPerPage
= clientHeight
/ lineHeight
;
4245 ResetVisibleLinesRange();
4247 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4248 GetHeaderWidth() / SCROLL_UNIT_X
,
4249 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4250 GetScrollPos(wxHORIZONTAL
),
4251 GetScrollPos(wxVERTICAL
),
4256 // we have 3 different layout strategies: either layout all items
4257 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4258 // to arrange them in top to bottom, left to right (don't ask me why
4259 // not the other way round...) order
4260 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4262 int x
= EXTRA_BORDER_X
;
4263 int y
= EXTRA_BORDER_Y
;
4265 wxCoord widthMax
= 0;
4268 for ( i
= 0; i
< count
; i
++ )
4270 wxListLineData
*line
= GetLine(i
);
4271 line
->CalculateSize( &dc
, iconSpacing
);
4272 line
->SetPosition( x
, y
, iconSpacing
);
4274 wxSize sizeLine
= GetLineSize(i
);
4276 if ( HasFlag(wxLC_ALIGN_TOP
) )
4278 if ( sizeLine
.x
> widthMax
)
4279 widthMax
= sizeLine
.x
;
4283 else // wxLC_ALIGN_LEFT
4285 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4289 if ( HasFlag(wxLC_ALIGN_TOP
) )
4291 // traverse the items again and tweak their sizes so that they are
4292 // all the same in a row
4293 for ( i
= 0; i
< count
; i
++ )
4295 wxListLineData
*line
= GetLine(i
);
4296 line
->m_gi
->ExtendWidth(widthMax
);
4304 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4305 (y
+ lineHeight
) / lineHeight
,
4306 GetScrollPos( wxHORIZONTAL
),
4307 GetScrollPos( wxVERTICAL
),
4311 else // "flowed" arrangement, the most complicated case
4313 // at first we try without any scrollbars, if the items don't fit into
4314 // the window, we recalculate after subtracting the space taken by the
4317 int entireWidth
= 0;
4319 for (int tries
= 0; tries
< 2; tries
++)
4321 entireWidth
= 2 * EXTRA_BORDER_X
;
4325 // Now we have decided that the items do not fit into the
4326 // client area, so we need a scrollbar
4327 entireWidth
+= SCROLL_UNIT_X
;
4330 int x
= EXTRA_BORDER_X
;
4331 int y
= EXTRA_BORDER_Y
;
4332 int maxWidthInThisRow
= 0;
4335 int currentlyVisibleLines
= 0;
4337 for (size_t i
= 0; i
< count
; i
++)
4339 currentlyVisibleLines
++;
4340 wxListLineData
*line
= GetLine( i
);
4341 line
->CalculateSize( &dc
, iconSpacing
);
4342 line
->SetPosition( x
, y
, iconSpacing
);
4344 wxSize sizeLine
= GetLineSize( i
);
4346 if ( maxWidthInThisRow
< sizeLine
.x
)
4347 maxWidthInThisRow
= sizeLine
.x
;
4350 if (currentlyVisibleLines
> m_linesPerPage
)
4351 m_linesPerPage
= currentlyVisibleLines
;
4353 if ( y
+ sizeLine
.y
>= clientHeight
)
4355 currentlyVisibleLines
= 0;
4357 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4358 x
+= maxWidthInThisRow
;
4359 entireWidth
+= maxWidthInThisRow
;
4360 maxWidthInThisRow
= 0;
4363 // We have reached the last item.
4364 if ( i
== count
- 1 )
4365 entireWidth
+= maxWidthInThisRow
;
4367 if ( (tries
== 0) &&
4368 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4370 clientHeight
-= wxSystemSettings::
4371 GetMetric(wxSYS_HSCROLL_Y
);
4376 if ( i
== count
- 1 )
4377 tries
= 1; // Everything fits, no second try required.
4385 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4387 GetScrollPos( wxHORIZONTAL
),
4396 // FIXME: why should we call it from here?
4403 void wxListMainWindow::RefreshAll()
4408 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4409 if ( headerWin
&& headerWin
->m_dirty
)
4411 headerWin
->m_dirty
= false;
4412 headerWin
->Refresh();
4416 void wxListMainWindow::UpdateCurrent()
4418 if ( !HasCurrent() && !IsEmpty() )
4422 long wxListMainWindow::GetNextItem( long item
,
4423 int WXUNUSED(geometry
),
4427 max
= GetItemCount();
4428 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4429 _T("invalid listctrl index in GetNextItem()") );
4431 // notice that we start with the next item (or the first one if item == -1)
4432 // and this is intentional to allow writing a simple loop to iterate over
4433 // all selected items
4436 // this is not an error because the index was OK initially,
4437 // just no such item
4444 size_t count
= GetItemCount();
4445 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4447 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4450 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4457 // ----------------------------------------------------------------------------
4459 // ----------------------------------------------------------------------------
4461 void wxListMainWindow::DeleteItem( long lindex
)
4463 size_t count
= GetItemCount();
4465 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4466 _T("invalid item index in DeleteItem") );
4468 size_t index
= (size_t)lindex
;
4470 // we don't need to adjust the index for the previous items
4471 if ( HasCurrent() && m_current
>= index
)
4473 // if the current item is being deleted, we want the next one to
4474 // become selected - unless there is no next one - so don't adjust
4475 // m_current in this case
4476 if ( m_current
!= index
|| m_current
== count
- 1 )
4480 if ( InReportView() )
4482 // mark the Column Max Width cache as dirty if the items in the line
4483 // we're deleting contain the Max Column Width
4484 wxListLineData
* const line
= GetLine(index
);
4485 wxListItemDataList::compatibility_iterator n
;
4486 wxListItemData
*itemData
;
4490 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4492 n
= line
->m_items
.Item( i
);
4493 itemData
= n
->GetData();
4494 itemData
->GetItem(item
);
4496 itemWidth
= GetItemWidthWithImage(&item
);
4498 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4499 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4502 ResetVisibleLinesRange();
4505 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4510 m_selStore
.OnItemDelete(index
);
4514 m_lines
.RemoveAt( index
);
4517 // we need to refresh the (vert) scrollbar as the number of items changed
4520 RefreshAfter(index
);
4523 void wxListMainWindow::DeleteColumn( int col
)
4525 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4527 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4530 delete node
->GetData();
4531 m_columns
.Erase( node
);
4535 // update all the items
4536 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4538 wxListLineData
* const line
= GetLine(i
);
4539 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4540 delete n
->GetData();
4541 line
->m_items
.Erase(n
);
4545 if ( InReportView() ) // we only cache max widths when in Report View
4547 delete m_aColWidths
.Item(col
);
4548 m_aColWidths
.RemoveAt(col
);
4551 // invalidate it as it has to be recalculated
4555 void wxListMainWindow::DoDeleteAllItems()
4558 // nothing to do - in particular, don't send the event
4563 // to make the deletion of all items faster, we don't send the
4564 // notifications for each item deletion in this case but only one event
4565 // for all of them: this is compatible with wxMSW and documented in
4566 // DeleteAllItems() description
4568 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4569 event
.SetEventObject( GetParent() );
4570 GetParent()->GetEventHandler()->ProcessEvent( event
);
4578 if ( InReportView() )
4580 ResetVisibleLinesRange();
4581 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4583 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4590 void wxListMainWindow::DeleteAllItems()
4594 RecalculatePositions();
4597 void wxListMainWindow::DeleteEverything()
4599 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4600 WX_CLEAR_ARRAY(m_aColWidths
);
4605 // ----------------------------------------------------------------------------
4606 // scanning for an item
4607 // ----------------------------------------------------------------------------
4609 void wxListMainWindow::EnsureVisible( long index
)
4611 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4612 _T("invalid index in EnsureVisible") );
4614 // We have to call this here because the label in question might just have
4615 // been added and its position is not known yet
4617 RecalculatePositions(true /* no refresh */);
4619 MoveToItem((size_t)index
);
4622 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4628 wxString str_upper
= str
.Upper();
4632 size_t count
= GetItemCount();
4633 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4635 wxListLineData
*line
= GetLine(i
);
4636 wxString line_upper
= line
->GetText(0).Upper();
4639 if (line_upper
== str_upper
)
4644 if (line_upper
.find(str_upper
) == 0)
4652 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4658 size_t count
= GetItemCount();
4659 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4661 wxListLineData
*line
= GetLine(i
);
4663 line
->GetItem( 0, item
);
4664 if (item
.m_data
== data
)
4671 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4674 GetVisibleLinesRange( &topItem
, NULL
);
4677 GetItemPosition( GetItemCount() - 1, p
);
4681 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4682 if ( id
>= 0 && id
< (long)GetItemCount() )
4688 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4690 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4692 size_t count
= GetItemCount();
4694 if ( InReportView() )
4696 size_t current
= y
/ GetLineHeight();
4697 if ( current
< count
)
4699 flags
= HitTestLine(current
, x
, y
);
4706 // TODO: optimize it too! this is less simple than for report view but
4707 // enumerating all items is still not a way to do it!!
4708 for ( size_t current
= 0; current
< count
; current
++ )
4710 flags
= HitTestLine(current
, x
, y
);
4719 // ----------------------------------------------------------------------------
4721 // ----------------------------------------------------------------------------
4723 void wxListMainWindow::InsertItem( wxListItem
&item
)
4725 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4727 int count
= GetItemCount();
4728 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4730 if (item
.m_itemId
> count
)
4731 item
.m_itemId
= count
;
4733 size_t id
= item
.m_itemId
;
4737 if ( InReportView() )
4739 ResetVisibleLinesRange();
4741 // calculate the width of the item and adjust the max column width
4742 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4743 int width
= GetItemWidthWithImage(&item
);
4744 item
.SetWidth(width
);
4745 if (width
> pWidthInfo
->nMaxWidth
)
4746 pWidthInfo
->nMaxWidth
= width
;
4749 wxListLineData
*line
= new wxListLineData(this);
4751 line
->SetItem( item
.m_col
, item
);
4753 m_lines
.Insert( line
, id
);
4757 // If an item is selected at or below the point of insertion, we need to
4758 // increment the member variables because the current row's index has gone
4760 if ( HasCurrent() && m_current
>= id
)
4763 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4765 RefreshLines(id
, GetItemCount() - 1);
4768 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4771 if ( InReportView() )
4773 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4774 item
.m_width
= GetTextLength( item
.m_text
);
4776 wxListHeaderData
*column
= new wxListHeaderData( item
);
4777 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4779 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4782 wxListHeaderDataList::compatibility_iterator
4783 node
= m_columns
.Item( col
);
4784 m_columns
.Insert( node
, column
);
4785 m_aColWidths
.Insert( colWidthInfo
, col
);
4789 m_columns
.Append( column
);
4790 m_aColWidths
.Add( colWidthInfo
);
4795 // update all the items
4796 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4798 wxListLineData
* const line
= GetLine(i
);
4799 wxListItemData
* const data
= new wxListItemData(this);
4801 line
->m_items
.Insert(col
, data
);
4803 line
->m_items
.Append(data
);
4807 // invalidate it as it has to be recalculated
4812 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4815 wxClientDC
dc(this);
4817 dc
.SetFont( GetFont() );
4819 if (item
->GetImage() != -1)
4822 GetImageSize( item
->GetImage(), ix
, iy
);
4826 if (!item
->GetText().empty())
4829 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4836 // ----------------------------------------------------------------------------
4838 // ----------------------------------------------------------------------------
4840 wxListCtrlCompare list_ctrl_compare_func_2
;
4841 long list_ctrl_compare_data
;
4843 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4845 wxListLineData
*line1
= *arg1
;
4846 wxListLineData
*line2
= *arg2
;
4848 line1
->GetItem( 0, item
);
4849 wxUIntPtr data1
= item
.m_data
;
4850 line2
->GetItem( 0, item
);
4851 wxUIntPtr data2
= item
.m_data
;
4852 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4855 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4857 // selections won't make sense any more after sorting the items so reset
4859 HighlightAll(false);
4862 list_ctrl_compare_func_2
= fn
;
4863 list_ctrl_compare_data
= data
;
4864 m_lines
.Sort( list_ctrl_compare_func_1
);
4868 // ----------------------------------------------------------------------------
4870 // ----------------------------------------------------------------------------
4872 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4875 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4876 wxScrolledWindow::OnScroll(event
);
4878 HandleOnScroll( event
);
4881 // update our idea of which lines are shown when we redraw the window the
4883 ResetVisibleLinesRange();
4885 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4887 wxGenericListCtrl
* lc
= GetListCtrl();
4888 wxCHECK_RET( lc
, _T("no listctrl window?") );
4890 lc
->m_headerWin
->Refresh();
4891 lc
->m_headerWin
->Update();
4895 int wxListMainWindow::GetCountPerPage() const
4897 if ( !m_linesPerPage
)
4899 wxConstCast(this, wxListMainWindow
)->
4900 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4903 return m_linesPerPage
;
4906 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4908 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4910 if ( m_lineFrom
== (size_t)-1 )
4912 size_t count
= GetItemCount();
4915 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4917 // this may happen if SetScrollbars() hadn't been called yet
4918 if ( m_lineFrom
>= count
)
4919 m_lineFrom
= count
- 1;
4921 // we redraw one extra line but this is needed to make the redrawing
4922 // logic work when there is a fractional number of lines on screen
4923 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4924 if ( m_lineTo
>= count
)
4925 m_lineTo
= count
- 1;
4927 else // empty control
4930 m_lineTo
= (size_t)-1;
4934 wxASSERT_MSG( IsEmpty() ||
4935 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4936 _T("GetVisibleLinesRange() returns incorrect result") );
4944 // -------------------------------------------------------------------------------------
4945 // wxGenericListCtrl
4946 // -------------------------------------------------------------------------------------
4948 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4950 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4951 EVT_SIZE(wxGenericListCtrl::OnSize
)
4954 wxGenericListCtrl::wxGenericListCtrl()
4956 m_imageListNormal
= (wxImageList
*) NULL
;
4957 m_imageListSmall
= (wxImageList
*) NULL
;
4958 m_imageListState
= (wxImageList
*) NULL
;
4960 m_ownsImageListNormal
=
4961 m_ownsImageListSmall
=
4962 m_ownsImageListState
= false;
4964 m_mainWin
= (wxListMainWindow
*) NULL
;
4965 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4969 wxGenericListCtrl::~wxGenericListCtrl()
4971 if (m_ownsImageListNormal
)
4972 delete m_imageListNormal
;
4973 if (m_ownsImageListSmall
)
4974 delete m_imageListSmall
;
4975 if (m_ownsImageListState
)
4976 delete m_imageListState
;
4979 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4985 GetThemeMetric( kThemeMetricListHeaderHeight
, &h
);
4987 // we use 'g' to get the descent, too
4989 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4990 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4993 // only update if changed
4994 if ( h
!= m_headerHeight
)
4999 ResizeReportView(true);
5000 else //why is this needed if it doesn't have a header?
5001 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
5006 void wxGenericListCtrl::CreateHeaderWindow()
5008 m_headerWin
= new wxListHeaderWindow
5010 this, wxID_ANY
, m_mainWin
,
5012 wxSize(GetClientSize().x
, m_headerHeight
),
5015 CalculateAndSetHeaderHeight();
5018 bool wxGenericListCtrl::Create(wxWindow
*parent
,
5023 const wxValidator
&validator
,
5024 const wxString
&name
)
5028 m_imageListState
= (wxImageList
*) NULL
;
5029 m_ownsImageListNormal
=
5030 m_ownsImageListSmall
=
5031 m_ownsImageListState
= false;
5033 m_mainWin
= (wxListMainWindow
*) NULL
;
5034 m_headerWin
= (wxListHeaderWindow
*) NULL
;
5038 if ( !(style
& wxLC_MASK_TYPE
) )
5040 style
= style
| wxLC_LIST
;
5043 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
5046 // this window itself shouldn't get the focus, only m_mainWin should
5049 // don't create the inner window with the border
5050 style
&= ~wxBORDER_MASK
;
5052 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
5055 // Human Interface Guidelines ask us for a special font in this case
5056 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
)
5059 font
.MacCreateFromThemeFont( kThemeViewsFont
);
5064 if ( InReportView() )
5066 CreateHeaderWindow();
5072 font
.MacCreateFromThemeFont( kThemeSmallSystemFont
);
5073 m_headerWin
->SetFont( font
);
5074 CalculateAndSetHeaderHeight();
5078 if ( HasFlag(wxLC_NO_HEADER
) )
5079 // VZ: why do we create it at all then?
5080 m_headerWin
->Show( false );
5083 SetInitialSize(size
);
5088 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
5090 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
5091 _T("wxLC_VIRTUAL can't be [un]set") );
5093 long flag
= GetWindowStyle();
5097 if (style
& wxLC_MASK_TYPE
)
5098 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5099 if (style
& wxLC_MASK_ALIGN
)
5100 flag
&= ~wxLC_MASK_ALIGN
;
5101 if (style
& wxLC_MASK_SORT
)
5102 flag
&= ~wxLC_MASK_SORT
;
5110 SetWindowStyleFlag( flag
);
5113 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5117 m_mainWin
->DeleteEverything();
5119 // has the header visibility changed?
5120 bool hasHeader
= HasHeader();
5121 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
5123 if ( hasHeader
!= willHaveHeader
)
5130 // don't delete, just hide, as we can reuse it later
5131 m_headerWin
->Show(false);
5133 //else: nothing to do
5135 else // must show header
5139 CreateHeaderWindow();
5141 else // already have it, just show
5143 m_headerWin
->Show( true );
5147 ResizeReportView(willHaveHeader
);
5151 wxWindow::SetWindowStyleFlag( flag
);
5154 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5156 m_mainWin
->GetColumn( col
, item
);
5160 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5162 m_mainWin
->SetColumn( col
, item
);
5166 int wxGenericListCtrl::GetColumnWidth( int col
) const
5168 return m_mainWin
->GetColumnWidth( col
);
5171 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5173 m_mainWin
->SetColumnWidth( col
, width
);
5177 int wxGenericListCtrl::GetCountPerPage() const
5179 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5182 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5184 m_mainWin
->GetItem( info
);
5188 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5190 m_mainWin
->SetItem( info
);
5194 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5197 info
.m_text
= label
;
5198 info
.m_mask
= wxLIST_MASK_TEXT
;
5199 info
.m_itemId
= index
;
5203 info
.m_image
= imageId
;
5204 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5207 m_mainWin
->SetItem(info
);
5211 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5213 return m_mainWin
->GetItemState( item
, stateMask
);
5216 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5218 m_mainWin
->SetItemState( item
, state
, stateMask
);
5223 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5225 return SetItemColumnImage(item
, 0, image
);
5229 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5232 info
.m_image
= image
;
5233 info
.m_mask
= wxLIST_MASK_IMAGE
;
5234 info
.m_itemId
= item
;
5235 info
.m_col
= column
;
5236 m_mainWin
->SetItem( info
);
5240 wxString
wxGenericListCtrl::GetItemText( long item
) const
5242 return m_mainWin
->GetItemText(item
);
5245 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5247 m_mainWin
->SetItemText(item
, str
);
5250 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5253 info
.m_mask
= wxLIST_MASK_DATA
;
5254 info
.m_itemId
= item
;
5255 m_mainWin
->GetItem( info
);
5259 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
5262 info
.m_mask
= wxLIST_MASK_DATA
;
5263 info
.m_itemId
= item
;
5265 m_mainWin
->SetItem( info
);
5269 wxRect
wxGenericListCtrl::GetViewRect() const
5271 return m_mainWin
->GetViewRect();
5274 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
5276 m_mainWin
->GetItemRect( item
, rect
);
5277 if ( m_mainWin
->HasHeader() )
5278 rect
.y
+= m_headerHeight
+ 1;
5282 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5284 m_mainWin
->GetItemPosition( item
, pos
);
5288 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5293 int wxGenericListCtrl::GetItemCount() const
5295 return m_mainWin
->GetItemCount();
5298 int wxGenericListCtrl::GetColumnCount() const
5300 return m_mainWin
->GetColumnCount();
5303 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5305 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5308 wxSize
wxGenericListCtrl::GetItemSpacing() const
5310 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5312 return wxSize(spacing
, spacing
);
5315 #if WXWIN_COMPATIBILITY_2_6
5316 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5318 return m_mainWin
->GetItemSpacing( isSmall
);
5320 #endif // WXWIN_COMPATIBILITY_2_6
5322 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5325 info
.m_itemId
= item
;
5326 info
.SetTextColour( col
);
5327 m_mainWin
->SetItem( info
);
5330 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5333 info
.m_itemId
= item
;
5334 m_mainWin
->GetItem( info
);
5335 return info
.GetTextColour();
5338 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5341 info
.m_itemId
= item
;
5342 info
.SetBackgroundColour( col
);
5343 m_mainWin
->SetItem( info
);
5346 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5349 info
.m_itemId
= item
;
5350 m_mainWin
->GetItem( info
);
5351 return info
.GetBackgroundColour();
5354 int wxGenericListCtrl::GetScrollPos( int orient
) const
5356 return m_mainWin
->GetScrollPos( orient
);
5359 void wxGenericListCtrl::SetScrollPos( int orient
, int pos
, bool refresh
)
5361 m_mainWin
->SetScrollPos( orient
, pos
, refresh
);
5364 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5367 info
.m_itemId
= item
;
5369 m_mainWin
->SetItem( info
);
5372 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5375 info
.m_itemId
= item
;
5376 m_mainWin
->GetItem( info
);
5377 return info
.GetFont();
5380 int wxGenericListCtrl::GetSelectedItemCount() const
5382 return m_mainWin
->GetSelectedItemCount();
5385 wxColour
wxGenericListCtrl::GetTextColour() const
5387 return GetForegroundColour();
5390 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5392 SetForegroundColour(col
);
5395 long wxGenericListCtrl::GetTopItem() const
5398 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5402 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5404 return m_mainWin
->GetNextItem( item
, geom
, state
);
5407 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5409 if (which
== wxIMAGE_LIST_NORMAL
)
5410 return m_imageListNormal
;
5411 else if (which
== wxIMAGE_LIST_SMALL
)
5412 return m_imageListSmall
;
5413 else if (which
== wxIMAGE_LIST_STATE
)
5414 return m_imageListState
;
5416 return (wxImageList
*) NULL
;
5419 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5421 if ( which
== wxIMAGE_LIST_NORMAL
)
5423 if (m_ownsImageListNormal
)
5424 delete m_imageListNormal
;
5425 m_imageListNormal
= imageList
;
5426 m_ownsImageListNormal
= false;
5428 else if ( which
== wxIMAGE_LIST_SMALL
)
5430 if (m_ownsImageListSmall
)
5431 delete m_imageListSmall
;
5432 m_imageListSmall
= imageList
;
5433 m_ownsImageListSmall
= false;
5435 else if ( which
== wxIMAGE_LIST_STATE
)
5437 if (m_ownsImageListState
)
5438 delete m_imageListState
;
5439 m_imageListState
= imageList
;
5440 m_ownsImageListState
= false;
5443 m_mainWin
->SetImageList( imageList
, which
);
5446 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5448 SetImageList(imageList
, which
);
5449 if ( which
== wxIMAGE_LIST_NORMAL
)
5450 m_ownsImageListNormal
= true;
5451 else if ( which
== wxIMAGE_LIST_SMALL
)
5452 m_ownsImageListSmall
= true;
5453 else if ( which
== wxIMAGE_LIST_STATE
)
5454 m_ownsImageListState
= true;
5457 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5462 bool wxGenericListCtrl::DeleteItem( long item
)
5464 m_mainWin
->DeleteItem( item
);
5468 bool wxGenericListCtrl::DeleteAllItems()
5470 m_mainWin
->DeleteAllItems();
5474 bool wxGenericListCtrl::DeleteAllColumns()
5476 size_t count
= m_mainWin
->m_columns
.GetCount();
5477 for ( size_t n
= 0; n
< count
; n
++ )
5482 void wxGenericListCtrl::ClearAll()
5484 m_mainWin
->DeleteEverything();
5487 bool wxGenericListCtrl::DeleteColumn( int col
)
5489 m_mainWin
->DeleteColumn( col
);
5491 // if we don't have the header any longer, we need to relayout the window
5492 if ( !GetColumnCount() )
5493 ResizeReportView(false /* no header */);
5497 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5498 wxClassInfo
* textControlClass
)
5500 return m_mainWin
->EditLabel( item
, textControlClass
);
5503 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5505 return m_mainWin
->GetEditControl();
5508 bool wxGenericListCtrl::EnsureVisible( long item
)
5510 m_mainWin
->EnsureVisible( item
);
5514 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5516 return m_mainWin
->FindItem( start
, str
, partial
);
5519 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5521 return m_mainWin
->FindItem( start
, data
);
5524 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5525 int WXUNUSED(direction
))
5527 return m_mainWin
->FindItem( pt
);
5530 // TODO: sub item hit testing
5531 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5533 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5536 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5538 m_mainWin
->InsertItem( info
);
5539 return info
.m_itemId
;
5542 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5545 info
.m_text
= label
;
5546 info
.m_mask
= wxLIST_MASK_TEXT
;
5547 info
.m_itemId
= index
;
5548 return InsertItem( info
);
5551 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5554 info
.m_mask
= wxLIST_MASK_IMAGE
;
5555 info
.m_image
= imageIndex
;
5556 info
.m_itemId
= index
;
5557 return InsertItem( info
);
5560 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5563 info
.m_text
= label
;
5564 info
.m_image
= imageIndex
;
5565 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5566 info
.m_itemId
= index
;
5567 return InsertItem( info
);
5570 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5572 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5574 m_mainWin
->InsertColumn( col
, item
);
5576 // if we hadn't had a header before but have one now
5577 // then we need to relayout the window
5578 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5579 ResizeReportView(true /* have header */);
5581 m_headerWin
->Refresh();
5586 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5587 int format
, int width
)
5590 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5591 item
.m_text
= heading
;
5594 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5595 item
.m_width
= width
;
5598 item
.m_format
= format
;
5600 return InsertColumn( col
, item
);
5603 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
5605 return m_mainWin
->ScrollList(dx
, dy
);
5609 // fn is a function which takes 3 long arguments: item1, item2, data.
5610 // item1 is the long data associated with a first item (NOT the index).
5611 // item2 is the long data associated with a second item (NOT the index).
5612 // data is the same value as passed to SortItems.
5613 // The return value is a negative number if the first item should precede the second
5614 // item, a positive number of the second item should precede the first,
5615 // or zero if the two items are equivalent.
5616 // data is arbitrary data to be passed to the sort function.
5618 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5620 m_mainWin
->SortItems( fn
, data
);
5624 // ----------------------------------------------------------------------------
5626 // ----------------------------------------------------------------------------
5628 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5633 ResizeReportView(m_mainWin
->HasHeader());
5634 m_mainWin
->RecalculatePositions();
5637 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5640 GetClientSize( &cw
, &ch
);
5644 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5645 if(ch
> m_headerHeight
)
5646 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5647 cw
, ch
- m_headerHeight
- 1 );
5649 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5652 else // no header window
5654 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5658 void wxGenericListCtrl::OnInternalIdle()
5660 wxWindow::OnInternalIdle();
5662 // do it only if needed
5663 if ( !m_mainWin
->m_dirty
)
5666 m_mainWin
->RecalculatePositions();
5669 // ----------------------------------------------------------------------------
5671 // ----------------------------------------------------------------------------
5673 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5677 m_mainWin
->SetBackgroundColour( colour
);
5678 m_mainWin
->m_dirty
= true;
5684 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5686 if ( !wxWindow::SetForegroundColour( colour
) )
5691 m_mainWin
->SetForegroundColour( colour
);
5692 m_mainWin
->m_dirty
= true;
5696 m_headerWin
->SetForegroundColour( colour
);
5701 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5703 if ( !wxWindow::SetFont( font
) )
5708 m_mainWin
->SetFont( font
);
5709 m_mainWin
->m_dirty
= true;
5714 m_headerWin
->SetFont( font
);
5715 CalculateAndSetHeaderHeight();
5725 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5728 // Use the same color scheme as wxListBox
5729 return wxListBox::GetClassDefaultAttributes(variant
);
5731 wxUnusedVar(variant
);
5732 wxVisualAttributes attr
;
5733 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5734 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5735 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5740 // ----------------------------------------------------------------------------
5741 // methods forwarded to m_mainWin
5742 // ----------------------------------------------------------------------------
5744 #if wxUSE_DRAG_AND_DROP
5746 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5748 m_mainWin
->SetDropTarget( dropTarget
);
5751 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5753 return m_mainWin
->GetDropTarget();
5758 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5760 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5763 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5765 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5768 wxColour
wxGenericListCtrl::GetForegroundColour() const
5770 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5773 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5776 return m_mainWin
->PopupMenu( menu
, x
, y
);
5782 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5784 m_mainWin
->DoClientToScreen(x
, y
);
5787 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5789 m_mainWin
->DoScreenToClient(x
, y
);
5792 void wxGenericListCtrl::SetFocus()
5794 // The test in window.cpp fails as we are a composite
5795 // window, so it checks against "this", but not m_mainWin.
5796 if ( DoFindFocus() != this )
5797 m_mainWin
->SetFocus();
5800 wxSize
wxGenericListCtrl::DoGetBestSize() const
5802 // Something is better than nothing...
5803 // 100x80 is what the MSW version will get from the default
5804 // wxControl::DoGetBestSize
5805 return wxSize(100, 80);
5808 // ----------------------------------------------------------------------------
5809 // virtual list control support
5810 // ----------------------------------------------------------------------------
5812 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5814 // this is a pure virtual function, in fact - which is not really pure
5815 // because the controls which are not virtual don't need to implement it
5816 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5818 return wxEmptyString
;
5821 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5823 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5825 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5829 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5832 return OnGetItemImage(item
);
5838 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5840 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5841 _T("invalid item index in OnGetItemAttr()") );
5843 // no attributes by default
5847 void wxGenericListCtrl::SetItemCount(long count
)
5849 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5851 m_mainWin
->SetItemCount(count
);
5854 void wxGenericListCtrl::RefreshItem(long item
)
5856 m_mainWin
->RefreshLine(item
);
5859 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5861 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5864 // Generic wxListCtrl is more or less a container for two other
5865 // windows which drawings are done upon. These are namely
5866 // 'm_headerWin' and 'm_mainWin'.
5867 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5868 // behaviour wxListCtrl has under wxMSW.
5870 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5874 // The easy case, no rectangle specified.
5876 m_headerWin
->Refresh(eraseBackground
);
5879 m_mainWin
->Refresh(eraseBackground
);
5883 // Refresh the header window
5886 wxRect rectHeader
= m_headerWin
->GetRect();
5887 rectHeader
.Intersect(*rect
);
5888 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5891 m_headerWin
->GetPosition(&x
, &y
);
5892 rectHeader
.Offset(-x
, -y
);
5893 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5897 // Refresh the main window
5900 wxRect rectMain
= m_mainWin
->GetRect();
5901 rectMain
.Intersect(*rect
);
5902 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5905 m_mainWin
->GetPosition(&x
, &y
);
5906 rectMain
.Offset(-x
, -y
);
5907 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5913 void wxGenericListCtrl::Freeze()
5915 m_mainWin
->Freeze();
5918 void wxGenericListCtrl::Thaw()
5923 #endif // wxUSE_LISTCTRL