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 EndEdit( bool discardChanges
);
470 void OnChar( wxKeyEvent
&event
);
471 void OnKeyUp( wxKeyEvent
&event
);
472 void OnKillFocus( wxFocusEvent
&event
);
474 bool AcceptChanges();
475 void Finish( bool setfocus
);
478 wxListMainWindow
*m_owner
;
480 wxString m_startValue
;
482 bool m_aboutToFinish
;
484 DECLARE_EVENT_TABLE()
487 //-----------------------------------------------------------------------------
488 // wxListMainWindow (internal)
489 //-----------------------------------------------------------------------------
491 WX_DECLARE_EXPORTED_LIST(wxListHeaderData
, wxListHeaderDataList
);
492 #include "wx/listimpl.cpp"
493 WX_DEFINE_LIST(wxListHeaderDataList
)
495 class wxListMainWindow
: public wxScrolledWindow
499 wxListMainWindow( wxWindow
*parent
,
501 const wxPoint
& pos
= wxDefaultPosition
,
502 const wxSize
& size
= wxDefaultSize
,
504 const wxString
&name
= _T("listctrlmainwindow") );
506 virtual ~wxListMainWindow();
508 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
510 // return true if this is a virtual list control
511 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
513 // return true if the control is in report mode
514 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
516 // return true if we are in single selection mode, false if multi sel
517 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
519 // do we have a header window?
520 bool HasHeader() const
521 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
523 void HighlightAll( bool on
);
525 // all these functions only do something if the line is currently visible
527 // change the line "selected" state, return true if it really changed
528 bool HighlightLine( size_t line
, bool highlight
= true);
530 // as HighlightLine() but do it for the range of lines: this is incredibly
531 // more efficient for virtual list controls!
533 // NB: unlike HighlightLine() this one does refresh the lines on screen
534 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
536 // toggle the line state and refresh it
537 void ReverseHighlight( size_t line
)
538 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
540 // return true if the line is highlighted
541 bool IsHighlighted(size_t line
) const;
543 // refresh one or several lines at once
544 void RefreshLine( size_t line
);
545 void RefreshLines( size_t lineFrom
, size_t lineTo
);
547 // refresh all selected items
548 void RefreshSelected();
550 // refresh all lines below the given one: the difference with
551 // RefreshLines() is that the index here might not be a valid one (happens
552 // when the last line is deleted)
553 void RefreshAfter( size_t lineFrom
);
555 // the methods which are forwarded to wxListLineData itself in list/icon
556 // modes but are here because the lines don't store their positions in the
559 // get the bound rect for the entire line
560 wxRect
GetLineRect(size_t line
) const;
562 // get the bound rect of the label
563 wxRect
GetLineLabelRect(size_t line
) const;
565 // get the bound rect of the items icon (only may be called if we do have
567 wxRect
GetLineIconRect(size_t line
) const;
569 // get the rect to be highlighted when the item has focus
570 wxRect
GetLineHighlightRect(size_t line
) const;
572 // get the size of the total line rect
573 wxSize
GetLineSize(size_t line
) const
574 { return GetLineRect(line
).GetSize(); }
576 // return the hit code for the corresponding position (in this line)
577 long HitTestLine(size_t line
, int x
, int y
) const;
579 // bring the selected item into view, scrolling to it if necessary
580 void MoveToItem(size_t item
);
582 bool ScrollList( int WXUNUSED(dx
), int dy
);
584 // bring the current item into view
585 void MoveToFocus() { MoveToItem(m_current
); }
587 // start editing the label of the given item
588 wxTextCtrl
*EditLabel(long item
,
589 wxClassInfo
* textControlClass
= CLASSINFO(wxTextCtrl
));
590 wxTextCtrl
*GetEditControl() const
592 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
595 void ResetTextControl(wxTextCtrl
*text
)
598 m_textctrlWrapper
= NULL
;
601 // we don't draw anything while we're frozen so we must refresh ourselves
602 // when we're thawed to make sure the changes are displayed correctly
603 virtual void DoThaw() { Refresh(); }
605 void OnRenameTimer();
606 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
607 void OnRenameCancelled(size_t itemEdit
);
609 void OnMouse( wxMouseEvent
&event
);
611 // called to switch the selection from the current item to newCurrent,
612 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
614 void OnChar( wxKeyEvent
&event
);
615 void OnKeyDown( wxKeyEvent
&event
);
616 void OnKeyUp( wxKeyEvent
&event
);
617 void OnSetFocus( wxFocusEvent
&event
);
618 void OnKillFocus( wxFocusEvent
&event
);
619 void OnScroll( wxScrollWinEvent
& event
);
621 void OnPaint( wxPaintEvent
&event
);
623 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
624 void GetImageSize( int index
, int &width
, int &height
) const;
625 int GetTextLength( const wxString
&s
) const;
627 void SetImageList( wxImageList
*imageList
, int which
);
628 void SetItemSpacing( int spacing
, bool isSmall
= false );
629 int GetItemSpacing( bool isSmall
= false );
631 void SetColumn( int col
, wxListItem
&item
);
632 void SetColumnWidth( int col
, int width
);
633 void GetColumn( int col
, wxListItem
&item
) const;
634 int GetColumnWidth( int col
) const;
635 int GetColumnCount() const { return m_columns
.GetCount(); }
637 // returns the sum of the heights of all columns
638 int GetHeaderWidth() const;
640 int GetCountPerPage() const;
642 void SetItem( wxListItem
&item
);
643 void GetItem( wxListItem
&item
) const;
644 void SetItemState( long item
, long state
, long stateMask
);
645 void SetItemStateAll( long state
, long stateMask
);
646 int GetItemState( long item
, long stateMask
) const;
647 void GetItemRect( long index
, wxRect
&rect
) const;
648 wxRect
GetViewRect() const;
649 bool GetItemPosition( long item
, wxPoint
& pos
) const;
650 int GetSelectedItemCount() const;
652 wxString
GetItemText(long item
) const
655 info
.m_mask
= wxLIST_MASK_TEXT
;
656 info
.m_itemId
= item
;
661 void SetItemText(long item
, const wxString
& value
)
664 info
.m_mask
= wxLIST_MASK_TEXT
;
665 info
.m_itemId
= item
;
670 // set the scrollbars and update the positions of the items
671 void RecalculatePositions(bool noRefresh
= false);
673 // refresh the window and the header
676 long GetNextItem( long item
, int geometry
, int state
) const;
677 void DeleteItem( long index
);
678 void DeleteAllItems();
679 void DeleteColumn( int col
);
680 void DeleteEverything();
681 void EnsureVisible( long index
);
682 long FindItem( long start
, const wxString
& str
, bool partial
= false );
683 long FindItem( long start
, wxUIntPtr data
);
684 long FindItem( const wxPoint
& pt
);
685 long HitTest( int x
, int y
, int &flags
) const;
686 void InsertItem( wxListItem
&item
);
687 void InsertColumn( long col
, wxListItem
&item
);
688 int GetItemWidthWithImage(wxListItem
* item
);
689 void SortItems( wxListCtrlCompare fn
, long data
);
691 size_t GetItemCount() const;
692 bool IsEmpty() const { return GetItemCount() == 0; }
693 void SetItemCount(long count
);
695 // change the current (== focused) item, send a notification event
696 void ChangeCurrent(size_t current
);
697 void ResetCurrent() { ChangeCurrent((size_t)-1); }
698 bool HasCurrent() const { return m_current
!= (size_t)-1; }
700 // send out a wxListEvent
701 void SendNotify( size_t line
,
703 const wxPoint
& point
= wxDefaultPosition
);
705 // override base class virtual to reset m_lineHeight when the font changes
706 virtual bool SetFont(const wxFont
& font
)
708 if ( !wxScrolledWindow::SetFont(font
) )
716 // these are for wxListLineData usage only
718 // get the backpointer to the list ctrl
719 wxGenericListCtrl
*GetListCtrl() const
721 return wxStaticCast(GetParent(), wxGenericListCtrl
);
724 // get the height of all lines (assuming they all do have the same height)
725 wxCoord
GetLineHeight() const;
727 // get the y position of the given line (only for report view)
728 wxCoord
GetLineY(size_t line
) const;
730 // get the brush to use for the item highlighting
731 wxBrush
*GetHighlightBrush() const
733 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
736 bool HasFocus() const
742 // the array of all line objects for a non virtual list control (for the
743 // virtual list control we only ever use m_lines[0])
744 wxListLineDataArray m_lines
;
746 // the list of column objects
747 wxListHeaderDataList m_columns
;
749 // currently focused item or -1
752 // the number of lines per page
755 // this flag is set when something which should result in the window
756 // redrawing happens (i.e. an item was added or deleted, or its appearance
757 // changed) and OnPaint() doesn't redraw the window while it is set which
758 // allows to minimize the number of repaintings when a lot of items are
759 // being added. The real repainting occurs only after the next OnIdle()
763 wxColour
*m_highlightColour
;
764 wxImageList
*m_small_image_list
;
765 wxImageList
*m_normal_image_list
;
767 int m_normal_spacing
;
771 wxTimer
*m_renameTimer
;
775 ColWidthArray m_aColWidths
;
777 // for double click logic
778 size_t m_lineLastClicked
,
779 m_lineBeforeLastClicked
,
780 m_lineSelectSingleOnUp
;
783 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
785 // the total count of items in a virtual list control
788 // the object maintaining the items selection state, only used in virtual
790 wxSelectionStore m_selStore
;
792 // common part of all ctors
795 // get the line data for the given index
796 wxListLineData
*GetLine(size_t n
) const
798 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
802 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
809 // get a dummy line which can be used for geometry calculations and such:
810 // you must use GetLine() if you want to really draw the line
811 wxListLineData
*GetDummyLine() const;
813 // cache the line data of the n-th line in m_lines[0]
814 void CacheLineData(size_t line
);
816 // get the range of visible lines
817 void GetVisibleLinesRange(size_t *from
, size_t *to
);
819 // force us to recalculate the range of visible lines
820 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
822 // get the colour to be used for drawing the rules
823 wxColour
GetRuleColour() const
825 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
829 // initialize the current item if needed
830 void UpdateCurrent();
832 // delete all items but don't refresh: called from dtor
833 void DoDeleteAllItems();
835 // the height of one line using the current font
836 wxCoord m_lineHeight
;
838 // the total header width or 0 if not calculated yet
839 wxCoord m_headerWidth
;
841 // the first and last lines being shown on screen right now (inclusive),
842 // both may be -1 if they must be calculated so never access them directly:
843 // use GetVisibleLinesRange() above instead
847 // the brushes to use for item highlighting when we do/don't have focus
848 wxBrush
*m_highlightBrush
,
849 *m_highlightUnfocusedBrush
;
851 // wrapper around the text control currently used for in place editing or
852 // NULL if no item is being edited
853 wxListTextCtrlWrapper
*m_textctrlWrapper
;
856 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
857 DECLARE_EVENT_TABLE()
859 friend class wxGenericListCtrl
;
863 wxListItemData::~wxListItemData()
865 // in the virtual list control the attributes are managed by the main
866 // program, so don't delete them
867 if ( !m_owner
->IsVirtual() )
873 void wxListItemData::Init()
881 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
887 if ( owner
->InReportView() )
893 void wxListItemData::SetItem( const wxListItem
&info
)
895 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
896 SetText(info
.m_text
);
897 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
898 m_image
= info
.m_image
;
899 if ( info
.m_mask
& wxLIST_MASK_DATA
)
900 m_data
= info
.m_data
;
902 if ( info
.HasAttributes() )
905 m_attr
->AssignFrom(*info
.GetAttributes());
907 m_attr
= new wxListItemAttr(*info
.GetAttributes());
915 m_rect
->width
= info
.m_width
;
919 void wxListItemData::SetPosition( int x
, int y
)
921 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
927 void wxListItemData::SetSize( int width
, int height
)
929 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
932 m_rect
->width
= width
;
934 m_rect
->height
= height
;
937 bool wxListItemData::IsHit( int x
, int y
) const
939 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
941 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
944 int wxListItemData::GetX() const
946 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
951 int wxListItemData::GetY() const
953 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
958 int wxListItemData::GetWidth() const
960 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
962 return m_rect
->width
;
965 int wxListItemData::GetHeight() const
967 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
969 return m_rect
->height
;
972 void wxListItemData::GetItem( wxListItem
&info
) const
974 long mask
= info
.m_mask
;
976 // by default, get everything for backwards compatibility
979 if ( mask
& wxLIST_MASK_TEXT
)
980 info
.m_text
= m_text
;
981 if ( mask
& wxLIST_MASK_IMAGE
)
982 info
.m_image
= m_image
;
983 if ( mask
& wxLIST_MASK_DATA
)
984 info
.m_data
= m_data
;
988 if ( m_attr
->HasTextColour() )
989 info
.SetTextColour(m_attr
->GetTextColour());
990 if ( m_attr
->HasBackgroundColour() )
991 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
992 if ( m_attr
->HasFont() )
993 info
.SetFont(m_attr
->GetFont());
997 //-----------------------------------------------------------------------------
999 //-----------------------------------------------------------------------------
1001 void wxListHeaderData::Init()
1013 wxListHeaderData::wxListHeaderData()
1018 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1025 void wxListHeaderData::SetItem( const wxListItem
&item
)
1027 m_mask
= item
.m_mask
;
1029 if ( m_mask
& wxLIST_MASK_TEXT
)
1030 m_text
= item
.m_text
;
1032 if ( m_mask
& wxLIST_MASK_IMAGE
)
1033 m_image
= item
.m_image
;
1035 if ( m_mask
& wxLIST_MASK_FORMAT
)
1036 m_format
= item
.m_format
;
1038 if ( m_mask
& wxLIST_MASK_WIDTH
)
1039 SetWidth(item
.m_width
);
1041 if ( m_mask
& wxLIST_MASK_STATE
)
1042 SetState(item
.m_state
);
1045 void wxListHeaderData::SetPosition( int x
, int y
)
1051 void wxListHeaderData::SetHeight( int h
)
1056 void wxListHeaderData::SetWidth( int w
)
1058 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1061 void wxListHeaderData::SetState( int flag
)
1066 void wxListHeaderData::SetFormat( int format
)
1071 bool wxListHeaderData::HasImage() const
1073 return m_image
!= -1;
1076 bool wxListHeaderData::IsHit( int x
, int y
) const
1078 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1081 void wxListHeaderData::GetItem( wxListItem
& item
)
1083 item
.m_mask
= m_mask
;
1084 item
.m_text
= m_text
;
1085 item
.m_image
= m_image
;
1086 item
.m_format
= m_format
;
1087 item
.m_width
= m_width
;
1088 item
.m_state
= m_state
;
1091 int wxListHeaderData::GetImage() const
1096 int wxListHeaderData::GetWidth() const
1101 int wxListHeaderData::GetFormat() const
1106 int wxListHeaderData::GetState() const
1111 //-----------------------------------------------------------------------------
1113 //-----------------------------------------------------------------------------
1115 inline int wxListLineData::GetMode() const
1117 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1120 inline bool wxListLineData::InReportView() const
1122 return m_owner
->HasFlag(wxLC_REPORT
);
1125 inline bool wxListLineData::IsVirtual() const
1127 return m_owner
->IsVirtual();
1130 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1134 if ( InReportView() )
1137 m_gi
= new GeometryInfo
;
1139 m_highlighted
= false;
1141 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1144 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1146 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1147 wxCHECK_RET( node
, _T("no subitems at all??") );
1149 wxListItemData
*item
= node
->GetData();
1154 switch ( GetMode() )
1157 case wxLC_SMALL_ICON
:
1158 m_gi
->m_rectAll
.width
= spacing
;
1160 s
= item
->GetText();
1165 m_gi
->m_rectLabel
.width
=
1166 m_gi
->m_rectLabel
.height
= 0;
1170 dc
->GetTextExtent( s
, &lw
, &lh
);
1174 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1176 m_gi
->m_rectAll
.width
= lw
;
1178 m_gi
->m_rectLabel
.width
= lw
;
1179 m_gi
->m_rectLabel
.height
= lh
;
1182 if (item
->HasImage())
1185 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1186 m_gi
->m_rectIcon
.width
= w
+ 8;
1187 m_gi
->m_rectIcon
.height
= h
+ 8;
1189 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1190 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1191 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1192 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1195 if ( item
->HasText() )
1197 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1198 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1200 else // no text, highlight the icon
1202 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1203 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1208 s
= item
->GetTextForMeasuring();
1210 dc
->GetTextExtent( s
, &lw
, &lh
);
1214 m_gi
->m_rectLabel
.width
= lw
;
1215 m_gi
->m_rectLabel
.height
= lh
;
1217 m_gi
->m_rectAll
.width
= lw
;
1218 m_gi
->m_rectAll
.height
= lh
;
1220 if (item
->HasImage())
1223 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1224 m_gi
->m_rectIcon
.width
= w
;
1225 m_gi
->m_rectIcon
.height
= h
;
1227 m_gi
->m_rectAll
.width
+= 4 + w
;
1228 if (h
> m_gi
->m_rectAll
.height
)
1229 m_gi
->m_rectAll
.height
= h
;
1232 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1233 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1237 wxFAIL_MSG( _T("unexpected call to SetSize") );
1241 wxFAIL_MSG( _T("unknown mode") );
1246 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1248 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1249 wxCHECK_RET( node
, _T("no subitems at all??") );
1251 wxListItemData
*item
= node
->GetData();
1253 switch ( GetMode() )
1256 case wxLC_SMALL_ICON
:
1257 m_gi
->m_rectAll
.x
= x
;
1258 m_gi
->m_rectAll
.y
= y
;
1260 if ( item
->HasImage() )
1262 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1263 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1264 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1267 if ( item
->HasText() )
1269 if (m_gi
->m_rectAll
.width
> spacing
)
1270 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1272 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1273 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1274 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1275 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1277 else // no text, highlight the icon
1279 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1280 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1285 m_gi
->m_rectAll
.x
= x
;
1286 m_gi
->m_rectAll
.y
= y
;
1288 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1289 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1290 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1292 if (item
->HasImage())
1294 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1295 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1296 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1300 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1305 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1309 wxFAIL_MSG( _T("unknown mode") );
1314 void wxListLineData::InitItems( int num
)
1316 for (int i
= 0; i
< num
; i
++)
1317 m_items
.Append( new wxListItemData(m_owner
) );
1320 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1322 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1323 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1325 wxListItemData
*item
= node
->GetData();
1326 item
->SetItem( info
);
1329 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1331 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1334 wxListItemData
*item
= node
->GetData();
1335 item
->GetItem( info
);
1339 wxString
wxListLineData::GetText(int index
) const
1343 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1346 wxListItemData
*item
= node
->GetData();
1347 s
= item
->GetText();
1353 void wxListLineData::SetText( int index
, const wxString
& s
)
1355 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1358 wxListItemData
*item
= node
->GetData();
1363 void wxListLineData::SetImage( int index
, int image
)
1365 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1366 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1368 wxListItemData
*item
= node
->GetData();
1369 item
->SetImage(image
);
1372 int wxListLineData::GetImage( int index
) const
1374 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1375 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1377 wxListItemData
*item
= node
->GetData();
1378 return item
->GetImage();
1381 wxListItemAttr
*wxListLineData::GetAttr() const
1383 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1384 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1386 wxListItemData
*item
= node
->GetData();
1387 return item
->GetAttr();
1390 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1392 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1393 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1395 wxListItemData
*item
= node
->GetData();
1396 item
->SetAttr(attr
);
1399 bool wxListLineData::SetAttributes(wxDC
*dc
,
1400 const wxListItemAttr
*attr
,
1403 wxWindow
*listctrl
= m_owner
->GetParent();
1407 // don't use foreground colour for drawing highlighted items - this might
1408 // make them completely invisible (and there is no way to do bit
1409 // arithmetics on wxColour, unfortunately)
1414 if (m_owner
->HasFocus()
1415 #if !defined(__WXUNIVERSAL__)
1416 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1424 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1426 else if ( attr
&& attr
->HasTextColour() )
1427 colText
= attr
->GetTextColour();
1429 colText
= listctrl
->GetForegroundColour();
1431 dc
->SetTextForeground(colText
);
1435 if ( attr
&& attr
->HasFont() )
1436 font
= attr
->GetFont();
1438 font
= listctrl
->GetFont();
1443 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1444 if ( highlighted
|| hasBgCol
)
1447 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1449 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1451 dc
->SetPen( *wxTRANSPARENT_PEN
);
1459 void wxListLineData::Draw( wxDC
*dc
)
1461 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1462 wxCHECK_RET( node
, _T("no subitems at all??") );
1464 bool highlighted
= IsHighlighted();
1466 wxListItemAttr
*attr
= GetAttr();
1468 if ( SetAttributes(dc
, attr
, highlighted
) )
1469 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1471 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1477 int flags
= wxCONTROL_SELECTED
;
1478 if (m_owner
->HasFocus()
1479 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
1480 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1483 flags
|= wxCONTROL_FOCUSED
;
1484 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1489 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1494 // just for debugging to better see where the items are
1496 dc
->SetPen(*wxRED_PEN
);
1497 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1498 dc
->DrawRectangle( m_gi
->m_rectAll
);
1499 dc
->SetPen(*wxGREEN_PEN
);
1500 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1503 wxListItemData
*item
= node
->GetData();
1504 if (item
->HasImage())
1506 // centre the image inside our rectangle, this looks nicer when items
1507 // ae aligned in a row
1508 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1510 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1513 if (item
->HasText())
1515 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1517 wxDCClipper
clipper(*dc
, rectLabel
);
1518 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1522 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1524 const wxRect
& rectHL
,
1527 // TODO: later we should support setting different attributes for
1528 // different columns - to do it, just add "col" argument to
1529 // GetAttr() and move these lines into the loop below
1530 wxListItemAttr
*attr
= GetAttr();
1531 if ( SetAttributes(dc
, attr
, highlighted
) )
1532 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1534 dc
->DrawRectangle( rectHL
);
1540 int flags
= wxCONTROL_SELECTED
;
1541 if (m_owner
->HasFocus())
1542 flags
|= wxCONTROL_FOCUSED
;
1543 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1547 dc
->DrawRectangle( rectHL
);
1552 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1553 yMid
= rect
.y
+ rect
.height
/2;
1555 // This probably needs to be done
1556 // on all platforms as the icons
1557 // otherwise nearly touch the border
1562 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1564 node
= node
->GetNext(), col
++ )
1566 wxListItemData
*item
= node
->GetData();
1568 int width
= m_owner
->GetColumnWidth(col
);
1572 const int wText
= width
- 8;
1573 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
1575 if ( item
->HasImage() )
1578 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1579 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1581 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1587 if ( item
->HasText() )
1588 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, wText
);
1592 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1593 const wxString
& textOrig
,
1599 // we don't support displaying multiple lines currently (and neither does
1600 // wxMSW FWIW) so just merge all the lines
1601 wxString
text(textOrig
);
1602 text
.Replace(_T("\n"), _T(" "));
1605 dc
->GetTextExtent(text
, &w
, &h
);
1607 const wxCoord y
= yMid
- (h
+ 1)/2;
1609 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1611 // determine if the string can fit inside the current width
1614 // it can, draw it using the items alignment
1616 m_owner
->GetColumn(col
, item
);
1617 switch ( item
.GetAlign() )
1619 case wxLIST_FORMAT_LEFT
:
1623 case wxLIST_FORMAT_RIGHT
:
1627 case wxLIST_FORMAT_CENTER
:
1628 x
+= (width
- w
) / 2;
1632 wxFAIL_MSG( _T("unknown list item format") );
1636 dc
->DrawText(text
, x
, y
);
1638 else // otherwise, truncate and add an ellipsis if possible
1640 // determine the base width
1641 wxString
ellipsis(wxT("..."));
1643 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1645 // continue until we have enough space or only one character left
1647 size_t len
= text
.length();
1648 wxString drawntext
= text
.Left(len
);
1651 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1652 drawntext
.RemoveLast();
1655 if (w
+ base_w
<= width
)
1659 // if still not enough space, remove ellipsis characters
1660 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1662 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1663 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1666 // now draw the text
1667 dc
->DrawText(drawntext
, x
, y
);
1668 dc
->DrawText(ellipsis
, x
+ w
, y
);
1672 bool wxListLineData::Highlight( bool on
)
1674 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1676 if ( on
== m_highlighted
)
1684 void wxListLineData::ReverseHighlight( void )
1686 Highlight(!IsHighlighted());
1689 //-----------------------------------------------------------------------------
1690 // wxListHeaderWindow
1691 //-----------------------------------------------------------------------------
1693 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1695 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1696 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1697 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1698 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1701 void wxListHeaderWindow::Init()
1703 m_currentCursor
= (wxCursor
*) NULL
;
1704 m_isDragging
= false;
1708 wxListHeaderWindow::wxListHeaderWindow()
1712 m_owner
= (wxListMainWindow
*) NULL
;
1713 m_resizeCursor
= (wxCursor
*) NULL
;
1716 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1718 wxListMainWindow
*owner
,
1722 const wxString
&name
)
1723 : wxWindow( win
, id
, pos
, size
, style
, name
)
1728 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1731 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1732 SetOwnForegroundColour( attr
.colFg
);
1733 SetOwnBackgroundColour( attr
.colBg
);
1735 SetOwnFont( attr
.font
);
1737 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1738 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1740 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1744 wxListHeaderWindow::~wxListHeaderWindow()
1746 delete m_resizeCursor
;
1749 #ifdef __WXUNIVERSAL__
1750 #include "wx/univ/renderer.h"
1751 #include "wx/univ/theme.h"
1754 // shift the DC origin to match the position of the main window horz
1755 // scrollbar: this allows us to always use logical coords
1756 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1759 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1762 m_owner
->GetViewStart( &view_start
, NULL
);
1767 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1769 // account for the horz scrollbar offset
1771 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1773 // Maybe we just have to check for m_signX
1774 // in the DC, but I leave the #ifdef __WXGTK__
1776 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1780 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1783 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1785 wxPaintDC
dc( this );
1790 dc
.SetFont( GetFont() );
1792 // width and height of the entire header window
1794 GetClientSize( &w
, &h
);
1795 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1797 dc
.SetBackgroundMode(wxTRANSPARENT
);
1798 dc
.SetTextForeground(GetForegroundColour());
1800 int x
= HEADER_OFFSET_X
;
1801 int numColumns
= m_owner
->GetColumnCount();
1803 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1805 m_owner
->GetColumn( i
, item
);
1806 int wCol
= item
.m_width
;
1812 if (!m_parent
->IsEnabled())
1813 flags
|= wxCONTROL_DISABLED
;
1815 // NB: The code below is not really Mac-specific, but since we are close
1816 // to 2.8 release and I don't have time to test on other platforms, I
1817 // defined this only for wxMac. If this behavior is desired on
1818 // other platforms, please go ahead and revise or remove the #ifdef.
1820 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1821 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1822 flags
|= wxCONTROL_SELECTED
;
1825 wxRendererNative::Get().DrawHeaderButton
1829 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1833 // see if we have enough space for the column label
1835 // for this we need the width of the text
1838 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1839 wLabel
+= 2 * EXTRA_WIDTH
;
1841 // and the width of the icon, if any
1842 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1843 const int image
= item
.m_image
;
1844 wxImageList
*imageList
;
1847 imageList
= m_owner
->m_small_image_list
;
1850 imageList
->GetSize(image
, ix
, iy
);
1851 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1859 // ignore alignment if there is not enough space anyhow
1861 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1864 wxFAIL_MSG( _T("unknown list item format") );
1867 case wxLIST_FORMAT_LEFT
:
1871 case wxLIST_FORMAT_RIGHT
:
1872 xAligned
= x
+ cw
- wLabel
;
1875 case wxLIST_FORMAT_CENTER
:
1876 xAligned
= x
+ (cw
- wLabel
) / 2;
1880 // draw the text and image clipping them so that they
1881 // don't overwrite the column boundary
1882 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1884 // if we have an image, draw it on the right of the label
1891 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1892 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1893 wxIMAGELIST_DRAW_TRANSPARENT
1897 dc
.DrawText( item
.GetText(),
1898 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1904 void wxListHeaderWindow::DrawCurrent()
1907 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1909 int x1
= m_currentX
;
1911 m_owner
->ClientToScreen( &x1
, &y1
);
1913 int x2
= m_currentX
;
1915 m_owner
->GetClientSize( NULL
, &y2
);
1916 m_owner
->ClientToScreen( &x2
, &y2
);
1919 dc
.SetLogicalFunction( wxINVERT
);
1920 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1921 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1925 dc
.DrawLine( x1
, y1
, x2
, y2
);
1927 dc
.SetLogicalFunction( wxCOPY
);
1929 dc
.SetPen( wxNullPen
);
1930 dc
.SetBrush( wxNullBrush
);
1934 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1936 // we want to work with logical coords
1938 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1939 int y
= event
.GetY();
1943 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1945 // we don't draw the line beyond our window, but we allow dragging it
1948 GetClientSize( &w
, NULL
);
1949 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1952 // erase the line if it was drawn
1953 if ( m_currentX
< w
)
1956 if (event
.ButtonUp())
1959 m_isDragging
= false;
1961 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1962 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1969 m_currentX
= m_minX
+ 7;
1971 // draw in the new location
1972 if ( m_currentX
< w
)
1976 else // not dragging
1979 bool hit_border
= false;
1981 // end of the current column
1984 // find the column where this event occurred
1986 countCol
= m_owner
->GetColumnCount();
1987 for (col
= 0; col
< countCol
; col
++)
1989 xpos
+= m_owner
->GetColumnWidth( col
);
1992 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1994 // near the column border
2001 // inside the column
2008 if ( col
== countCol
)
2011 if (event
.LeftDown() || event
.RightUp())
2013 if (hit_border
&& event
.LeftDown())
2015 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2016 event
.GetPosition()) )
2018 m_isDragging
= true;
2023 //else: column resizing was vetoed by the user code
2025 else // click on a column
2027 // record the selected state of the columns
2028 if (event
.LeftDown())
2030 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2033 m_owner
->GetColumn(i
, colItem
);
2034 long state
= colItem
.GetState();
2036 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2038 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2039 m_owner
->SetColumn(i
, colItem
);
2043 SendListEvent( event
.LeftDown()
2044 ? wxEVT_COMMAND_LIST_COL_CLICK
2045 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2046 event
.GetPosition());
2049 else if (event
.Moving())
2054 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2055 m_currentCursor
= m_resizeCursor
;
2059 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2060 m_currentCursor
= wxSTANDARD_CURSOR
;
2064 SetCursor(*m_currentCursor
);
2069 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2071 m_owner
->SetFocus();
2075 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2077 wxWindow
*parent
= GetParent();
2078 wxListEvent
le( type
, parent
->GetId() );
2079 le
.SetEventObject( parent
);
2080 le
.m_pointDrag
= pos
;
2082 // the position should be relative to the parent window, not
2083 // this one for compatibility with MSW and common sense: the
2084 // user code doesn't know anything at all about this header
2085 // window, so why should it get positions relative to it?
2086 le
.m_pointDrag
.y
-= GetSize().y
;
2088 le
.m_col
= m_column
;
2089 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2092 //-----------------------------------------------------------------------------
2093 // wxListRenameTimer (internal)
2094 //-----------------------------------------------------------------------------
2096 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2101 void wxListRenameTimer::Notify()
2103 m_owner
->OnRenameTimer();
2106 //-----------------------------------------------------------------------------
2107 // wxListTextCtrlWrapper (internal)
2108 //-----------------------------------------------------------------------------
2110 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2111 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2112 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2113 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2116 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2119 : m_startValue(owner
->GetItemText(itemEdit
)),
2120 m_itemEdited(itemEdit
)
2124 m_aboutToFinish
= false;
2126 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2128 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2129 &rectLabel
.x
, &rectLabel
.y
);
2131 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2132 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2133 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2136 m_text
->PushEventHandler(this);
2139 void wxListTextCtrlWrapper::EndEdit(bool discardChanges
)
2141 m_aboutToFinish
= true;
2143 if ( discardChanges
)
2145 m_owner
->OnRenameCancelled(m_itemEdited
);
2151 // Notify the owner about the changes
2154 // Even if vetoed, close the control (consistent with MSW)
2159 void wxListTextCtrlWrapper::Finish( bool setfocus
)
2161 m_text
->RemoveEventHandler(this);
2162 m_owner
->ResetTextControl( m_text
);
2164 wxPendingDelete
.Append( this );
2167 m_owner
->SetFocusIgnoringChildren();
2170 bool wxListTextCtrlWrapper::AcceptChanges()
2172 const wxString value
= m_text
->GetValue();
2174 // notice that we should always call OnRenameAccept() to generate the "end
2175 // label editing" event, even if the user hasn't really changed anything
2176 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2178 // vetoed by the user
2182 // accepted, do rename the item (unless nothing changed)
2183 if ( value
!= m_startValue
)
2184 m_owner
->SetItemText(m_itemEdited
, value
);
2189 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2191 switch ( event
.m_keyCode
)
2206 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2208 if (m_aboutToFinish
)
2210 // auto-grow the textctrl:
2211 wxSize parentSize
= m_owner
->GetSize();
2212 wxPoint myPos
= m_text
->GetPosition();
2213 wxSize mySize
= m_text
->GetSize();
2215 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2216 if (myPos
.x
+ sx
> parentSize
.x
)
2217 sx
= parentSize
.x
- myPos
.x
;
2220 m_text
->SetSize(sx
, wxDefaultCoord
);
2226 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2228 if ( !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;
2288 wxListMainWindow::wxListMainWindow()
2293 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2296 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2301 const wxString
&name
)
2302 : wxScrolledWindow( parent
, id
, pos
, size
,
2303 style
| wxHSCROLL
| wxVSCROLL
, name
)
2307 m_highlightBrush
= new wxBrush
2309 wxSystemSettings::GetColour
2311 wxSYS_COLOUR_HIGHLIGHT
2316 m_highlightUnfocusedBrush
= new wxBrush
2318 wxSystemSettings::GetColour
2320 wxSYS_COLOUR_BTNSHADOW
2325 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2327 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2328 SetOwnForegroundColour( attr
.colFg
);
2329 SetOwnBackgroundColour( attr
.colBg
);
2331 SetOwnFont( attr
.font
);
2334 wxListMainWindow::~wxListMainWindow()
2337 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2338 WX_CLEAR_ARRAY(m_aColWidths
);
2340 delete m_highlightBrush
;
2341 delete m_highlightUnfocusedBrush
;
2342 delete m_renameTimer
;
2345 void wxListMainWindow::CacheLineData(size_t line
)
2347 wxGenericListCtrl
*listctrl
= GetListCtrl();
2349 wxListLineData
*ld
= GetDummyLine();
2351 size_t countCol
= GetColumnCount();
2352 for ( size_t col
= 0; col
< countCol
; col
++ )
2354 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2355 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2358 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2361 wxListLineData
*wxListMainWindow::GetDummyLine() const
2363 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2364 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2366 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2368 // we need to recreate the dummy line if the number of columns in the
2369 // control changed as it would have the incorrect number of fields
2371 if ( !m_lines
.IsEmpty() &&
2372 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2374 self
->m_lines
.Clear();
2377 if ( m_lines
.IsEmpty() )
2379 wxListLineData
*line
= new wxListLineData(self
);
2380 self
->m_lines
.Add(line
);
2382 // don't waste extra memory -- there never going to be anything
2383 // else/more in this array
2384 self
->m_lines
.Shrink();
2390 // ----------------------------------------------------------------------------
2391 // line geometry (report mode only)
2392 // ----------------------------------------------------------------------------
2394 wxCoord
wxListMainWindow::GetLineHeight() const
2396 // we cache the line height as calling GetTextExtent() is slow
2397 if ( !m_lineHeight
)
2399 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2401 wxClientDC
dc( self
);
2402 dc
.SetFont( GetFont() );
2405 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2407 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2410 m_small_image_list
->GetSize(0, iw
, ih
);
2415 self
->m_lineHeight
= y
+ LINE_SPACING
;
2418 return m_lineHeight
;
2421 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2423 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2425 return LINE_SPACING
+ line
* GetLineHeight();
2428 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2430 if ( !InReportView() )
2431 return GetLine(line
)->m_gi
->m_rectAll
;
2434 rect
.x
= HEADER_OFFSET_X
;
2435 rect
.y
= GetLineY(line
);
2436 rect
.width
= GetHeaderWidth();
2437 rect
.height
= GetLineHeight();
2442 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2444 if ( !InReportView() )
2445 return GetLine(line
)->m_gi
->m_rectLabel
;
2448 wxListLineData
*data
= GetLine(line
);
2449 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
2452 wxListItemData
*item
= node
->GetData();
2453 if ( item
->HasImage() )
2456 GetImageSize( item
->GetImage(), ix
, iy
);
2457 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
2462 rect
.x
= image_x
+ HEADER_OFFSET_X
;
2463 rect
.y
= GetLineY(line
);
2464 rect
.width
= GetColumnWidth(0) - image_x
;
2465 rect
.height
= GetLineHeight();
2470 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2472 if ( !InReportView() )
2473 return GetLine(line
)->m_gi
->m_rectIcon
;
2475 wxListLineData
*ld
= GetLine(line
);
2476 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2479 rect
.x
= HEADER_OFFSET_X
;
2480 rect
.y
= GetLineY(line
);
2481 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2486 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2488 return InReportView() ? GetLineRect(line
)
2489 : GetLine(line
)->m_gi
->m_rectHighlight
;
2492 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2494 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2496 wxListLineData
*ld
= GetLine(line
);
2498 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2499 return wxLIST_HITTEST_ONITEMICON
;
2501 // VS: Testing for "ld->HasText() || InReportView()" instead of
2502 // "ld->HasText()" is needed to make empty lines in report view
2504 if ( ld
->HasText() || InReportView() )
2506 wxRect rect
= InReportView() ? GetLineRect(line
)
2507 : GetLineLabelRect(line
);
2509 if ( rect
.Contains(x
, y
) )
2510 return wxLIST_HITTEST_ONITEMLABEL
;
2516 // ----------------------------------------------------------------------------
2517 // highlight (selection) handling
2518 // ----------------------------------------------------------------------------
2520 bool wxListMainWindow::IsHighlighted(size_t line
) const
2524 return m_selStore
.IsSelected(line
);
2528 wxListLineData
*ld
= GetLine(line
);
2529 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2531 return ld
->IsHighlighted();
2535 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2541 wxArrayInt linesChanged
;
2542 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2545 // meny items changed state, refresh everything
2546 RefreshLines(lineFrom
, lineTo
);
2548 else // only a few items changed state, refresh only them
2550 size_t count
= linesChanged
.GetCount();
2551 for ( size_t n
= 0; n
< count
; n
++ )
2553 RefreshLine(linesChanged
[n
]);
2557 else // iterate over all items in non report view
2559 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2561 if ( HighlightLine(line
, highlight
) )
2567 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2573 changed
= m_selStore
.SelectItem(line
, highlight
);
2577 wxListLineData
*ld
= GetLine(line
);
2578 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2580 changed
= ld
->Highlight(highlight
);
2585 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2586 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2592 void wxListMainWindow::RefreshLine( size_t line
)
2594 if ( InReportView() )
2596 size_t visibleFrom
, visibleTo
;
2597 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2599 if ( line
< visibleFrom
|| line
> visibleTo
)
2603 wxRect rect
= GetLineRect(line
);
2605 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2606 RefreshRect( rect
);
2609 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2611 // we suppose that they are ordered by caller
2612 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2614 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2616 if ( InReportView() )
2618 size_t visibleFrom
, visibleTo
;
2619 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2621 if ( lineFrom
< visibleFrom
)
2622 lineFrom
= visibleFrom
;
2623 if ( lineTo
> visibleTo
)
2628 rect
.y
= GetLineY(lineFrom
);
2629 rect
.width
= GetClientSize().x
;
2630 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2632 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2633 RefreshRect( rect
);
2637 // TODO: this should be optimized...
2638 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2645 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2647 if ( InReportView() )
2649 size_t visibleFrom
, visibleTo
;
2650 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2652 if ( lineFrom
< visibleFrom
)
2653 lineFrom
= visibleFrom
;
2654 else if ( lineFrom
> visibleTo
)
2659 rect
.y
= GetLineY(lineFrom
);
2660 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2662 wxSize size
= GetClientSize();
2663 rect
.width
= size
.x
;
2665 // refresh till the bottom of the window
2666 rect
.height
= size
.y
- rect
.y
;
2668 RefreshRect( rect
);
2672 // TODO: how to do it more efficiently?
2677 void wxListMainWindow::RefreshSelected()
2683 if ( InReportView() )
2685 GetVisibleLinesRange(&from
, &to
);
2690 to
= GetItemCount() - 1;
2693 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2694 RefreshLine(m_current
);
2696 for ( size_t line
= from
; line
<= to
; line
++ )
2698 // NB: the test works as expected even if m_current == -1
2699 if ( line
!= m_current
&& IsHighlighted(line
) )
2704 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2706 // Note: a wxPaintDC must be constructed even if no drawing is
2707 // done (a Windows requirement).
2708 wxPaintDC
dc( this );
2710 if ( IsEmpty() || IsFrozen() )
2712 // nothing to draw or not the moment to draw it
2718 // delay the repainting until we calculate all the items positions
2725 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2727 dc
.SetFont( GetFont() );
2729 if ( InReportView() )
2731 int lineHeight
= GetLineHeight();
2733 size_t visibleFrom
, visibleTo
;
2734 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2737 int xOrig
= dc
.LogicalToDeviceX( 0 );
2738 int yOrig
= dc
.LogicalToDeviceY( 0 );
2740 // tell the caller cache to cache the data
2743 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2744 GetParent()->GetId());
2745 evCache
.SetEventObject( GetParent() );
2746 evCache
.m_oldItemIndex
= visibleFrom
;
2747 evCache
.m_itemIndex
= visibleTo
;
2748 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2751 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2753 rectLine
= GetLineRect(line
);
2756 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2757 rectLine
.width
, rectLine
.height
) )
2759 // don't redraw unaffected lines to avoid flicker
2763 GetLine(line
)->DrawInReportMode( &dc
,
2765 GetLineHighlightRect(line
),
2766 IsHighlighted(line
) );
2769 if ( HasFlag(wxLC_HRULES
) )
2771 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2772 wxSize clientSize
= GetClientSize();
2774 size_t i
= visibleFrom
;
2775 if (i
== 0) i
= 1; // Don't draw the first one
2776 for ( ; i
<= visibleTo
; i
++ )
2779 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2780 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2781 clientSize
.x
- dev_x
, i
* lineHeight
);
2784 // Draw last horizontal rule
2785 if ( visibleTo
== GetItemCount() - 1 )
2788 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2789 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2790 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2794 // Draw vertical rules if required
2795 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2797 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2798 wxRect firstItemRect
, lastItemRect
;
2800 GetItemRect(visibleFrom
, firstItemRect
);
2801 GetItemRect(visibleTo
, lastItemRect
);
2802 int x
= firstItemRect
.GetX();
2804 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2806 for (int col
= 0; col
< GetColumnCount(); col
++)
2808 int colWidth
= GetColumnWidth(col
);
2810 int x_pos
= x
- dev_x
;
2811 if (col
< GetColumnCount()-1) x_pos
-= 2;
2812 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2813 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2819 size_t count
= GetItemCount();
2820 for ( size_t i
= 0; i
< count
; i
++ )
2822 GetLine(i
)->Draw( &dc
);
2827 // Don't draw rect outline under Mac at all.
2832 wxRect
rect( GetLineHighlightRect( m_current
) );
2834 dc
.SetPen( *wxBLACK_PEN
);
2835 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2836 dc
.DrawRectangle( rect
);
2838 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2846 void wxListMainWindow::HighlightAll( bool on
)
2848 if ( IsSingleSel() )
2850 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2852 // we just have one item to turn off
2853 if ( HasCurrent() && IsHighlighted(m_current
) )
2855 HighlightLine(m_current
, false);
2856 RefreshLine(m_current
);
2859 else // multi selection
2862 HighlightLines(0, GetItemCount() - 1, on
);
2866 void wxListMainWindow::SendNotify( size_t line
,
2867 wxEventType command
,
2868 const wxPoint
& point
)
2870 wxListEvent
le( command
, GetParent()->GetId() );
2871 le
.SetEventObject( GetParent() );
2873 le
.m_itemIndex
= line
;
2875 // set only for events which have position
2876 if ( point
!= wxDefaultPosition
)
2877 le
.m_pointDrag
= point
;
2879 // don't try to get the line info for virtual list controls: the main
2880 // program has it anyhow and if we did it would result in accessing all
2881 // the lines, even those which are not visible now and this is precisely
2882 // what we're trying to avoid
2885 if ( line
!= (size_t)-1 )
2887 GetLine(line
)->GetItem( 0, le
.m_item
);
2889 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2891 //else: there may be no more such item
2893 GetParent()->GetEventHandler()->ProcessEvent( le
);
2896 void wxListMainWindow::ChangeCurrent(size_t current
)
2898 m_current
= current
;
2900 // as the current item changed, we shouldn't start editing it when the
2901 // "slow click" timer expires as the click happened on another item
2902 if ( m_renameTimer
->IsRunning() )
2903 m_renameTimer
->Stop();
2905 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2908 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2910 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2911 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2913 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2914 wxT("EditLabel() needs a text control") );
2916 size_t itemEdit
= (size_t)item
;
2918 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2919 le
.SetEventObject( GetParent() );
2920 le
.m_itemIndex
= item
;
2921 wxListLineData
*data
= GetLine(itemEdit
);
2922 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2923 data
->GetItem( 0, le
.m_item
);
2925 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2927 // vetoed by user code
2931 // We have to call this here because the label in question might just have
2932 // been added and no screen update taken place.
2937 // Pending events dispatched by wxSafeYield might have changed the item
2939 if ( (size_t)item
>= GetItemCount() )
2943 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2944 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2945 return m_textctrlWrapper
->GetText();
2948 void wxListMainWindow::OnRenameTimer()
2950 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2952 EditLabel( m_current
);
2955 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2957 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2958 le
.SetEventObject( GetParent() );
2959 le
.m_itemIndex
= itemEdit
;
2961 wxListLineData
*data
= GetLine(itemEdit
);
2963 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2965 data
->GetItem( 0, le
.m_item
);
2966 le
.m_item
.m_text
= value
;
2967 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2971 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2973 // let owner know that the edit was cancelled
2974 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2976 le
.SetEditCanceled(true);
2978 le
.SetEventObject( GetParent() );
2979 le
.m_itemIndex
= itemEdit
;
2981 wxListLineData
*data
= GetLine(itemEdit
);
2982 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2984 data
->GetItem( 0, le
.m_item
);
2985 GetEventHandler()->ProcessEvent( le
);
2988 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2992 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2993 // shutdown the edit control when the mouse is clicked elsewhere on the
2994 // listctrl because the order of events is different (or something like
2995 // that), so explicitly end the edit if it is active.
2996 if ( event
.LeftDown() && m_textctrlWrapper
)
2997 m_textctrlWrapper
->EndEdit( false );
3000 if ( event
.LeftDown() )
3001 SetFocusIgnoringChildren();
3003 event
.SetEventObject( GetParent() );
3004 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3007 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3009 // let the base handle mouse wheel events.
3014 if ( !HasCurrent() || IsEmpty() )
3016 if (event
.RightDown())
3018 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3020 wxContextMenuEvent
evtCtx(
3022 GetParent()->GetId(),
3023 ClientToScreen(event
.GetPosition()));
3024 evtCtx
.SetEventObject(GetParent());
3025 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3033 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3034 event
.ButtonDClick()) )
3037 int x
= event
.GetX();
3038 int y
= event
.GetY();
3039 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3041 // where did we hit it (if we did)?
3044 size_t count
= GetItemCount(),
3047 if ( InReportView() )
3049 current
= y
/ GetLineHeight();
3050 if ( current
< count
)
3051 hitResult
= HitTestLine(current
, x
, y
);
3055 // TODO: optimize it too! this is less simple than for report view but
3056 // enumerating all items is still not a way to do it!!
3057 for ( current
= 0; current
< count
; current
++ )
3059 hitResult
= HitTestLine(current
, x
, y
);
3065 if (event
.Dragging())
3067 if (m_dragCount
== 0)
3069 // we have to report the raw, physical coords as we want to be
3070 // able to call HitTest(event.m_pointDrag) from the user code to
3071 // get the item being dragged
3072 m_dragStart
= event
.GetPosition();
3077 if (m_dragCount
!= 3)
3080 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3081 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3083 wxListEvent
le( command
, GetParent()->GetId() );
3084 le
.SetEventObject( GetParent() );
3085 le
.m_itemIndex
= m_lineLastClicked
;
3086 le
.m_pointDrag
= m_dragStart
;
3087 GetParent()->GetEventHandler()->ProcessEvent( le
);
3098 // outside of any item
3099 if (event
.RightDown())
3101 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3103 wxContextMenuEvent
evtCtx(
3105 GetParent()->GetId(),
3106 ClientToScreen(event
.GetPosition()));
3107 evtCtx
.SetEventObject(GetParent());
3108 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3112 // reset the selection and bail out
3113 HighlightAll(false);
3119 bool forceClick
= false;
3120 if (event
.ButtonDClick())
3122 if ( m_renameTimer
->IsRunning() )
3123 m_renameTimer
->Stop();
3125 m_lastOnSame
= false;
3127 if ( current
== m_lineLastClicked
)
3129 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3135 // The first click was on another item, so don't interpret this as
3136 // a double click, but as a simple click instead
3143 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3145 // select single line
3146 HighlightAll( false );
3147 ReverseHighlight(m_lineSelectSingleOnUp
);
3152 if ((current
== m_current
) &&
3153 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3154 HasFlag(wxLC_EDIT_LABELS
) )
3156 if ( !InReportView() ||
3157 GetLineLabelRect(current
).Contains(x
, y
) )
3159 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
3160 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
3165 m_lastOnSame
= false;
3166 m_lineSelectSingleOnUp
= (size_t)-1;
3170 // This is necessary, because after a DnD operation in
3171 // from and to ourself, the up event is swallowed by the
3172 // DnD code. So on next non-up event (which means here and
3173 // now) m_lineSelectSingleOnUp should be reset.
3174 m_lineSelectSingleOnUp
= (size_t)-1;
3176 if (event
.RightDown())
3178 m_lineBeforeLastClicked
= m_lineLastClicked
;
3179 m_lineLastClicked
= current
;
3181 // If the item is already selected, do not update the selection.
3182 // Multi-selections should not be cleared if a selected item is clicked.
3183 if (!IsHighlighted(current
))
3185 HighlightAll(false);
3186 ChangeCurrent(current
);
3187 ReverseHighlight(m_current
);
3190 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3192 // Allow generation of context menu event
3195 else if (event
.MiddleDown())
3197 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3199 else if ( event
.LeftDown() || forceClick
)
3201 m_lineBeforeLastClicked
= m_lineLastClicked
;
3202 m_lineLastClicked
= current
;
3204 size_t oldCurrent
= m_current
;
3205 bool oldWasSelected
= IsHighlighted(m_current
);
3207 bool cmdModifierDown
= event
.CmdDown();
3208 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3210 if ( IsSingleSel() || !IsHighlighted(current
) )
3212 HighlightAll( false );
3214 ChangeCurrent(current
);
3216 ReverseHighlight(m_current
);
3218 else // multi sel & current is highlighted & no mod keys
3220 m_lineSelectSingleOnUp
= current
;
3221 ChangeCurrent(current
); // change focus
3224 else // multi sel & either ctrl or shift is down
3226 if (cmdModifierDown
)
3228 ChangeCurrent(current
);
3230 ReverseHighlight(m_current
);
3232 else if (event
.ShiftDown())
3234 ChangeCurrent(current
);
3236 size_t lineFrom
= oldCurrent
,
3239 if ( lineTo
< lineFrom
)
3242 lineFrom
= m_current
;
3245 HighlightLines(lineFrom
, lineTo
);
3247 else // !ctrl, !shift
3249 // test in the enclosing if should make it impossible
3250 wxFAIL_MSG( _T("how did we get here?") );
3254 if (m_current
!= oldCurrent
)
3255 RefreshLine( oldCurrent
);
3257 // forceClick is only set if the previous click was on another item
3258 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3262 void wxListMainWindow::MoveToItem(size_t item
)
3264 if ( item
== (size_t)-1 )
3267 wxRect rect
= GetLineRect(item
);
3269 int client_w
, client_h
;
3270 GetClientSize( &client_w
, &client_h
);
3272 const int hLine
= GetLineHeight();
3274 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3275 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3277 if ( InReportView() )
3279 // the next we need the range of lines shown it might be different,
3280 // so recalculate it
3281 ResetVisibleLinesRange();
3283 if (rect
.y
< view_y
)
3284 Scroll( -1, rect
.y
/ hLine
);
3285 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3286 Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3289 // At least on Mac the visible lines value will get reset inside of
3290 // Scroll *before* it actually scrolls the window because of the
3291 // Update() that happens there, so it will still have the wrong value.
3292 // So let's reset it again and wait for it to be recalculated in the
3293 // next paint event. I would expect this problem to show up in wxGTK
3294 // too but couldn't duplicate it there. Perhaps the order of events
3295 // is different... --Robin
3296 ResetVisibleLinesRange();
3304 if (rect
.x
-view_x
< 5)
3305 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
3306 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3307 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
3309 if (rect
.y
-view_y
< 5)
3310 sy
= (rect
.y
- 5) / hLine
;
3311 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
3312 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
3318 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
3320 if ( !InReportView() )
3322 // TODO: this should work in all views but is not implemented now
3327 GetVisibleLinesRange(&top
, &bottom
);
3329 if ( bottom
== (size_t)-1 )
3332 ResetVisibleLinesRange();
3334 int hLine
= GetLineHeight();
3336 Scroll(-1, top
+ dy
/ hLine
);
3339 // see comment in MoveToItem() for why we do this
3340 ResetVisibleLinesRange();
3346 // ----------------------------------------------------------------------------
3347 // keyboard handling
3348 // ----------------------------------------------------------------------------
3350 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3352 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3353 _T("invalid item index in OnArrowChar()") );
3355 size_t oldCurrent
= m_current
;
3357 // in single selection we just ignore Shift as we can't select several
3359 if ( event
.ShiftDown() && !IsSingleSel() )
3361 ChangeCurrent(newCurrent
);
3363 // refresh the old focus to remove it
3364 RefreshLine( oldCurrent
);
3366 // select all the items between the old and the new one
3367 if ( oldCurrent
> newCurrent
)
3369 newCurrent
= oldCurrent
;
3370 oldCurrent
= m_current
;
3373 HighlightLines(oldCurrent
, newCurrent
);
3377 // all previously selected items are unselected unless ctrl is held
3378 // in a multiselection control
3379 if ( !event
.ControlDown() || IsSingleSel() )
3380 HighlightAll(false);
3382 ChangeCurrent(newCurrent
);
3384 // refresh the old focus to remove it
3385 RefreshLine( oldCurrent
);
3387 // in single selection mode we must always have a selected item
3388 if ( !event
.ControlDown() || IsSingleSel() )
3389 HighlightLine( m_current
, true );
3392 RefreshLine( m_current
);
3397 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3399 wxWindow
*parent
= GetParent();
3401 // propagate the key event upwards
3402 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3403 ke
.m_shiftDown
= event
.m_shiftDown
;
3404 ke
.m_controlDown
= event
.m_controlDown
;
3405 ke
.m_altDown
= event
.m_altDown
;
3406 ke
.m_metaDown
= event
.m_metaDown
;
3407 ke
.m_keyCode
= event
.m_keyCode
;
3410 ke
.SetEventObject( parent
);
3411 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3416 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3418 wxWindow
*parent
= GetParent();
3420 // propagate the key event upwards
3421 wxKeyEvent
ke( wxEVT_KEY_UP
);
3422 ke
.m_shiftDown
= event
.m_shiftDown
;
3423 ke
.m_controlDown
= event
.m_controlDown
;
3424 ke
.m_altDown
= event
.m_altDown
;
3425 ke
.m_metaDown
= event
.m_metaDown
;
3426 ke
.m_keyCode
= event
.m_keyCode
;
3429 ke
.SetEventObject( parent
);
3430 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3435 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3437 wxWindow
*parent
= GetParent();
3439 // send a list_key event up
3442 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3443 le
.m_itemIndex
= m_current
;
3444 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3445 le
.m_code
= event
.GetKeyCode();
3446 le
.SetEventObject( parent
);
3447 parent
->GetEventHandler()->ProcessEvent( le
);
3450 // propagate the char event upwards
3451 wxKeyEvent
ke( wxEVT_CHAR
);
3452 ke
.m_shiftDown
= event
.m_shiftDown
;
3453 ke
.m_controlDown
= event
.m_controlDown
;
3454 ke
.m_altDown
= event
.m_altDown
;
3455 ke
.m_metaDown
= event
.m_metaDown
;
3456 ke
.m_keyCode
= event
.m_keyCode
;
3459 ke
.SetEventObject( parent
);
3460 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3462 if ( HandleAsNavigationKey(event
) )
3465 // no item -> nothing to do
3472 // don't use m_linesPerPage directly as it might not be computed yet
3473 const int pageSize
= GetCountPerPage();
3474 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3476 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3478 if (event
.GetKeyCode() == WXK_RIGHT
)
3479 event
.m_keyCode
= WXK_LEFT
;
3480 else if (event
.GetKeyCode() == WXK_LEFT
)
3481 event
.m_keyCode
= WXK_RIGHT
;
3484 switch ( event
.GetKeyCode() )
3487 if ( m_current
> 0 )
3488 OnArrowChar( m_current
- 1, event
);
3492 if ( m_current
< (size_t)GetItemCount() - 1 )
3493 OnArrowChar( m_current
+ 1, event
);
3498 OnArrowChar( GetItemCount() - 1, event
);
3503 OnArrowChar( 0, event
);
3508 int steps
= InReportView() ? pageSize
- 1
3509 : m_current
% pageSize
;
3511 int index
= m_current
- steps
;
3515 OnArrowChar( index
, event
);
3521 int steps
= InReportView()
3523 : pageSize
- (m_current
% pageSize
) - 1;
3525 size_t index
= m_current
+ steps
;
3526 size_t count
= GetItemCount();
3527 if ( index
>= count
)
3530 OnArrowChar( index
, event
);
3535 if ( !InReportView() )
3537 int index
= m_current
- pageSize
;
3541 OnArrowChar( index
, event
);
3546 if ( !InReportView() )
3548 size_t index
= m_current
+ pageSize
;
3550 size_t count
= GetItemCount();
3551 if ( index
>= count
)
3554 OnArrowChar( index
, event
);
3559 if ( IsSingleSel() )
3561 if ( event
.ControlDown() )
3563 ReverseHighlight(m_current
);
3565 else // normal space press
3567 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3570 else // multiple selection
3572 ReverseHighlight(m_current
);
3578 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3586 // ----------------------------------------------------------------------------
3588 // ----------------------------------------------------------------------------
3590 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3594 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3595 event
.SetEventObject( GetParent() );
3596 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3600 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3601 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3602 // which are already drawn correctly resulting in horrible flicker - avoid
3612 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3616 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3617 event
.SetEventObject( GetParent() );
3618 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3626 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3628 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3630 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3632 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3634 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3636 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3638 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3640 else if ( InReportView() && (m_small_image_list
))
3642 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3646 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3648 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3650 m_normal_image_list
->GetSize( index
, width
, height
);
3652 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3654 m_small_image_list
->GetSize( index
, width
, height
);
3656 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3658 m_small_image_list
->GetSize( index
, width
, height
);
3660 else if ( InReportView() && m_small_image_list
)
3662 m_small_image_list
->GetSize( index
, width
, height
);
3671 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3673 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3674 dc
.SetFont( GetFont() );
3677 dc
.GetTextExtent( s
, &lw
, NULL
);
3679 return lw
+ AUTOSIZE_COL_MARGIN
;
3682 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3686 // calc the spacing from the icon size
3687 int width
= 0, height
= 0;
3689 if ((imageList
) && (imageList
->GetImageCount()) )
3690 imageList
->GetSize(0, width
, height
);
3692 if (which
== wxIMAGE_LIST_NORMAL
)
3694 m_normal_image_list
= imageList
;
3695 m_normal_spacing
= width
+ 8;
3698 if (which
== wxIMAGE_LIST_SMALL
)
3700 m_small_image_list
= imageList
;
3701 m_small_spacing
= width
+ 14;
3702 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3706 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3710 m_small_spacing
= spacing
;
3712 m_normal_spacing
= spacing
;
3715 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3717 return isSmall
? m_small_spacing
: m_normal_spacing
;
3720 // ----------------------------------------------------------------------------
3722 // ----------------------------------------------------------------------------
3724 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3726 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3728 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3730 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3731 item
.m_width
= GetTextLength( item
.m_text
);
3733 wxListHeaderData
*column
= node
->GetData();
3734 column
->SetItem( item
);
3736 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3738 headerWin
->m_dirty
= true;
3742 // invalidate it as it has to be recalculated
3746 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3748 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3749 _T("invalid column index") );
3751 wxCHECK_RET( InReportView(),
3752 _T("SetColumnWidth() can only be called in report mode.") );
3755 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3757 headerWin
->m_dirty
= true;
3759 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3760 wxCHECK_RET( node
, _T("no column?") );
3762 wxListHeaderData
*column
= node
->GetData();
3764 size_t count
= GetItemCount();
3766 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3768 width
= GetTextLength(column
->GetText());
3769 width
+= 2*EXTRA_WIDTH
;
3771 // check for column header's image availability
3772 const int image
= column
->GetImage();
3775 if ( m_small_image_list
)
3778 m_small_image_list
->GetSize(image
, ix
, iy
);
3779 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3783 else if ( width
== wxLIST_AUTOSIZE
)
3787 // TODO: determine the max width somehow...
3788 width
= WIDTH_COL_DEFAULT
;
3792 wxClientDC
dc(this);
3793 dc
.SetFont( GetFont() );
3795 int max
= AUTOSIZE_COL_MARGIN
;
3797 // if the cached column width isn't valid then recalculate it
3798 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3800 for (size_t i
= 0; i
< count
; i
++)
3802 wxListLineData
*line
= GetLine( i
);
3803 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3805 wxCHECK_RET( n
, _T("no subitem?") );
3807 wxListItemData
*itemData
= n
->GetData();
3810 itemData
->GetItem(item
);
3811 int itemWidth
= GetItemWidthWithImage(&item
);
3812 if (itemWidth
> max
)
3816 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3817 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3820 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3821 width
= max
+ AUTOSIZE_COL_MARGIN
;
3825 column
->SetWidth( width
);
3827 // invalidate it as it has to be recalculated
3831 int wxListMainWindow::GetHeaderWidth() const
3833 if ( !m_headerWidth
)
3835 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3837 size_t count
= GetColumnCount();
3838 for ( size_t col
= 0; col
< count
; col
++ )
3840 self
->m_headerWidth
+= GetColumnWidth(col
);
3844 return m_headerWidth
;
3847 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3849 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3850 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3852 wxListHeaderData
*column
= node
->GetData();
3853 column
->GetItem( item
);
3856 int wxListMainWindow::GetColumnWidth( int col
) const
3858 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3859 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3861 wxListHeaderData
*column
= node
->GetData();
3862 return column
->GetWidth();
3865 // ----------------------------------------------------------------------------
3867 // ----------------------------------------------------------------------------
3869 void wxListMainWindow::SetItem( wxListItem
&item
)
3871 long id
= item
.m_itemId
;
3872 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3873 _T("invalid item index in SetItem") );
3877 wxListLineData
*line
= GetLine((size_t)id
);
3878 line
->SetItem( item
.m_col
, item
);
3880 // Set item state if user wants
3881 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3882 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3886 // update the Max Width Cache if needed
3887 int width
= GetItemWidthWithImage(&item
);
3889 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3890 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3894 // update the item on screen
3896 GetItemRect(id
, rectItem
);
3897 RefreshRect(rectItem
);
3900 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3905 // first deal with selection
3906 if ( stateMask
& wxLIST_STATE_SELECTED
)
3908 // set/clear select state
3911 // optimized version for virtual listctrl.
3912 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3915 else if ( state
& wxLIST_STATE_SELECTED
)
3917 const long count
= GetItemCount();
3918 for( long i
= 0; i
< count
; i
++ )
3920 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3926 // clear for non virtual (somewhat optimized by using GetNextItem())
3928 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3930 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3935 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3937 // unfocus all: only one item can be focussed, so clearing focus for
3938 // all items is simply clearing focus of the focussed item.
3939 SetItemState(m_current
, state
, stateMask
);
3941 //(setting focus to all items makes no sense, so it is not handled here.)
3944 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3948 SetItemStateAll(state
, stateMask
);
3952 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3953 _T("invalid list ctrl item index in SetItem") );
3955 size_t oldCurrent
= m_current
;
3956 size_t item
= (size_t)litem
; // safe because of the check above
3958 // do we need to change the focus?
3959 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3961 if ( state
& wxLIST_STATE_FOCUSED
)
3963 // don't do anything if this item is already focused
3964 if ( item
!= m_current
)
3966 ChangeCurrent(item
);
3968 if ( oldCurrent
!= (size_t)-1 )
3970 if ( IsSingleSel() )
3972 HighlightLine(oldCurrent
, false);
3975 RefreshLine(oldCurrent
);
3978 RefreshLine( m_current
);
3983 // don't do anything if this item is not focused
3984 if ( item
== m_current
)
3988 if ( IsSingleSel() )
3990 // we must unselect the old current item as well or we
3991 // might end up with more than one selected item in a
3992 // single selection control
3993 HighlightLine(oldCurrent
, false);
3996 RefreshLine( oldCurrent
);
4001 // do we need to change the selection state?
4002 if ( stateMask
& wxLIST_STATE_SELECTED
)
4004 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
4006 if ( IsSingleSel() )
4010 // selecting the item also makes it the focused one in the
4012 if ( m_current
!= item
)
4014 ChangeCurrent(item
);
4016 if ( oldCurrent
!= (size_t)-1 )
4018 HighlightLine( oldCurrent
, false );
4019 RefreshLine( oldCurrent
);
4025 // only the current item may be selected anyhow
4026 if ( item
!= m_current
)
4031 if ( HighlightLine(item
, on
) )
4038 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
4040 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
4041 _T("invalid list ctrl item index in GetItemState()") );
4043 int ret
= wxLIST_STATE_DONTCARE
;
4045 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4047 if ( (size_t)item
== m_current
)
4048 ret
|= wxLIST_STATE_FOCUSED
;
4051 if ( stateMask
& wxLIST_STATE_SELECTED
)
4053 if ( IsHighlighted(item
) )
4054 ret
|= wxLIST_STATE_SELECTED
;
4060 void wxListMainWindow::GetItem( wxListItem
&item
) const
4062 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
4063 _T("invalid item index in GetItem") );
4065 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4066 line
->GetItem( item
.m_col
, item
);
4068 // Get item state if user wants it
4069 if ( item
.m_mask
& wxLIST_MASK_STATE
)
4070 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
4071 wxLIST_STATE_FOCUSED
);
4074 // ----------------------------------------------------------------------------
4076 // ----------------------------------------------------------------------------
4078 size_t wxListMainWindow::GetItemCount() const
4080 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4083 void wxListMainWindow::SetItemCount(long count
)
4085 m_selStore
.SetItemCount(count
);
4086 m_countVirt
= count
;
4088 ResetVisibleLinesRange();
4090 // scrollbars must be reset
4094 int wxListMainWindow::GetSelectedItemCount() const
4096 // deal with the quick case first
4097 if ( IsSingleSel() )
4098 return HasCurrent() ? IsHighlighted(m_current
) : false;
4100 // virtual controls remmebers all its selections itself
4102 return m_selStore
.GetSelectedCount();
4104 // TODO: we probably should maintain the number of items selected even for
4105 // non virtual controls as enumerating all lines is really slow...
4106 size_t countSel
= 0;
4107 size_t count
= GetItemCount();
4108 for ( size_t line
= 0; line
< count
; line
++ )
4110 if ( GetLine(line
)->IsHighlighted() )
4117 // ----------------------------------------------------------------------------
4118 // item position/size
4119 // ----------------------------------------------------------------------------
4121 wxRect
wxListMainWindow::GetViewRect() const
4123 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
4124 _T("wxListCtrl::GetViewRect() only works in icon mode") );
4126 // we need to find the longest/tallest label
4127 wxCoord xMax
= 0, yMax
= 0;
4128 const int count
= GetItemCount();
4131 for ( int i
= 0; i
< count
; i
++ )
4136 wxCoord x
= r
.GetRight(),
4146 // some fudge needed to make it look prettier
4147 xMax
+= 2 * EXTRA_BORDER_X
;
4148 yMax
+= 2 * EXTRA_BORDER_Y
;
4150 // account for the scrollbars if necessary
4151 const wxSize sizeAll
= GetClientSize();
4152 if ( xMax
> sizeAll
.x
)
4153 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4154 if ( yMax
> sizeAll
.y
)
4155 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4157 return wxRect(0, 0, xMax
, yMax
);
4160 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
4162 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4163 _T("invalid index in GetItemRect") );
4165 // ensure that we're laid out, otherwise we could return nonsense
4168 wxConstCast(this, wxListMainWindow
)->
4169 RecalculatePositions(true /* no refresh */);
4172 rect
= GetLineRect((size_t)index
);
4174 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4177 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4180 GetItemRect(item
, rect
);
4188 // ----------------------------------------------------------------------------
4189 // geometry calculation
4190 // ----------------------------------------------------------------------------
4192 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4194 const int lineHeight
= GetLineHeight();
4196 wxClientDC
dc( this );
4197 dc
.SetFont( GetFont() );
4199 const size_t count
= GetItemCount();
4202 if ( HasFlag(wxLC_ICON
) )
4203 iconSpacing
= m_normal_spacing
;
4204 else if ( HasFlag(wxLC_SMALL_ICON
) )
4205 iconSpacing
= m_small_spacing
;
4209 // Note that we do not call GetClientSize() here but
4210 // GetSize() and subtract the border size for sunken
4211 // borders manually. This is technically incorrect,
4212 // but we need to know the client area's size WITHOUT
4213 // scrollbars here. Since we don't know if there are
4214 // any scrollbars, we use GetSize() instead. Another
4215 // solution would be to call SetScrollbars() here to
4216 // remove the scrollbars and call GetClientSize() then,
4217 // but this might result in flicker and - worse - will
4218 // reset the scrollbars to 0 which is not good at all
4219 // if you resize a dialog/window, but don't want to
4220 // reset the window scrolling. RR.
4221 // Furthermore, we actually do NOT subtract the border
4222 // width as 2 pixels is just the extra space which we
4223 // need around the actual content in the window. Other-
4224 // wise the text would e.g. touch the upper border. RR.
4227 GetSize( &clientWidth
, &clientHeight
);
4229 if ( InReportView() )
4231 // all lines have the same height and we scroll one line per step
4232 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4234 m_linesPerPage
= clientHeight
/ lineHeight
;
4236 ResetVisibleLinesRange();
4238 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4239 GetHeaderWidth() / SCROLL_UNIT_X
,
4240 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4241 GetScrollPos(wxHORIZONTAL
),
4242 GetScrollPos(wxVERTICAL
),
4247 // we have 3 different layout strategies: either layout all items
4248 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4249 // to arrange them in top to bottom, left to right (don't ask me why
4250 // not the other way round...) order
4251 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4253 int x
= EXTRA_BORDER_X
;
4254 int y
= EXTRA_BORDER_Y
;
4256 wxCoord widthMax
= 0;
4259 for ( i
= 0; i
< count
; i
++ )
4261 wxListLineData
*line
= GetLine(i
);
4262 line
->CalculateSize( &dc
, iconSpacing
);
4263 line
->SetPosition( x
, y
, iconSpacing
);
4265 wxSize sizeLine
= GetLineSize(i
);
4267 if ( HasFlag(wxLC_ALIGN_TOP
) )
4269 if ( sizeLine
.x
> widthMax
)
4270 widthMax
= sizeLine
.x
;
4274 else // wxLC_ALIGN_LEFT
4276 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4280 if ( HasFlag(wxLC_ALIGN_TOP
) )
4282 // traverse the items again and tweak their sizes so that they are
4283 // all the same in a row
4284 for ( i
= 0; i
< count
; i
++ )
4286 wxListLineData
*line
= GetLine(i
);
4287 line
->m_gi
->ExtendWidth(widthMax
);
4295 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4296 (y
+ lineHeight
) / lineHeight
,
4297 GetScrollPos( wxHORIZONTAL
),
4298 GetScrollPos( wxVERTICAL
),
4302 else // "flowed" arrangement, the most complicated case
4304 // at first we try without any scrollbars, if the items don't fit into
4305 // the window, we recalculate after subtracting the space taken by the
4308 int entireWidth
= 0;
4310 for (int tries
= 0; tries
< 2; tries
++)
4312 entireWidth
= 2 * EXTRA_BORDER_X
;
4316 // Now we have decided that the items do not fit into the
4317 // client area, so we need a scrollbar
4318 entireWidth
+= SCROLL_UNIT_X
;
4321 int x
= EXTRA_BORDER_X
;
4322 int y
= EXTRA_BORDER_Y
;
4323 int maxWidthInThisRow
= 0;
4326 int currentlyVisibleLines
= 0;
4328 for (size_t i
= 0; i
< count
; i
++)
4330 currentlyVisibleLines
++;
4331 wxListLineData
*line
= GetLine( i
);
4332 line
->CalculateSize( &dc
, iconSpacing
);
4333 line
->SetPosition( x
, y
, iconSpacing
);
4335 wxSize sizeLine
= GetLineSize( i
);
4337 if ( maxWidthInThisRow
< sizeLine
.x
)
4338 maxWidthInThisRow
= sizeLine
.x
;
4341 if (currentlyVisibleLines
> m_linesPerPage
)
4342 m_linesPerPage
= currentlyVisibleLines
;
4344 if ( y
+ sizeLine
.y
>= clientHeight
)
4346 currentlyVisibleLines
= 0;
4348 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4349 x
+= maxWidthInThisRow
;
4350 entireWidth
+= maxWidthInThisRow
;
4351 maxWidthInThisRow
= 0;
4354 // We have reached the last item.
4355 if ( i
== count
- 1 )
4356 entireWidth
+= maxWidthInThisRow
;
4358 if ( (tries
== 0) &&
4359 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4361 clientHeight
-= wxSystemSettings::
4362 GetMetric(wxSYS_HSCROLL_Y
);
4367 if ( i
== count
- 1 )
4368 tries
= 1; // Everything fits, no second try required.
4376 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4378 GetScrollPos( wxHORIZONTAL
),
4387 // FIXME: why should we call it from here?
4394 void wxListMainWindow::RefreshAll()
4399 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4400 if ( headerWin
&& headerWin
->m_dirty
)
4402 headerWin
->m_dirty
= false;
4403 headerWin
->Refresh();
4407 void wxListMainWindow::UpdateCurrent()
4409 if ( !HasCurrent() && !IsEmpty() )
4413 long wxListMainWindow::GetNextItem( long item
,
4414 int WXUNUSED(geometry
),
4418 max
= GetItemCount();
4419 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4420 _T("invalid listctrl index in GetNextItem()") );
4422 // notice that we start with the next item (or the first one if item == -1)
4423 // and this is intentional to allow writing a simple loop to iterate over
4424 // all selected items
4427 // this is not an error because the index was OK initially,
4428 // just no such item
4435 size_t count
= GetItemCount();
4436 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4438 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4441 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4448 // ----------------------------------------------------------------------------
4450 // ----------------------------------------------------------------------------
4452 void wxListMainWindow::DeleteItem( long lindex
)
4454 size_t count
= GetItemCount();
4456 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4457 _T("invalid item index in DeleteItem") );
4459 size_t index
= (size_t)lindex
;
4461 // we don't need to adjust the index for the previous items
4462 if ( HasCurrent() && m_current
>= index
)
4464 // if the current item is being deleted, we want the next one to
4465 // become selected - unless there is no next one - so don't adjust
4466 // m_current in this case
4467 if ( m_current
!= index
|| m_current
== count
- 1 )
4471 if ( InReportView() )
4473 // mark the Column Max Width cache as dirty if the items in the line
4474 // we're deleting contain the Max Column Width
4475 wxListLineData
* const line
= GetLine(index
);
4476 wxListItemDataList::compatibility_iterator n
;
4477 wxListItemData
*itemData
;
4481 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4483 n
= line
->m_items
.Item( i
);
4484 itemData
= n
->GetData();
4485 itemData
->GetItem(item
);
4487 itemWidth
= GetItemWidthWithImage(&item
);
4489 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4490 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4493 ResetVisibleLinesRange();
4496 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4501 m_selStore
.OnItemDelete(index
);
4505 m_lines
.RemoveAt( index
);
4508 // we need to refresh the (vert) scrollbar as the number of items changed
4511 RefreshAfter(index
);
4514 void wxListMainWindow::DeleteColumn( int col
)
4516 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4518 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4521 delete node
->GetData();
4522 m_columns
.Erase( node
);
4526 // update all the items
4527 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4529 wxListLineData
* const line
= GetLine(i
);
4530 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4531 delete n
->GetData();
4532 line
->m_items
.Erase(n
);
4536 if ( InReportView() ) // we only cache max widths when in Report View
4538 delete m_aColWidths
.Item(col
);
4539 m_aColWidths
.RemoveAt(col
);
4542 // invalidate it as it has to be recalculated
4546 void wxListMainWindow::DoDeleteAllItems()
4549 // nothing to do - in particular, don't send the event
4554 // to make the deletion of all items faster, we don't send the
4555 // notifications for each item deletion in this case but only one event
4556 // for all of them: this is compatible with wxMSW and documented in
4557 // DeleteAllItems() description
4559 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4560 event
.SetEventObject( GetParent() );
4561 GetParent()->GetEventHandler()->ProcessEvent( event
);
4569 if ( InReportView() )
4571 ResetVisibleLinesRange();
4572 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4574 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4581 void wxListMainWindow::DeleteAllItems()
4585 RecalculatePositions();
4588 void wxListMainWindow::DeleteEverything()
4590 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4591 WX_CLEAR_ARRAY(m_aColWidths
);
4596 // ----------------------------------------------------------------------------
4597 // scanning for an item
4598 // ----------------------------------------------------------------------------
4600 void wxListMainWindow::EnsureVisible( long index
)
4602 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4603 _T("invalid index in EnsureVisible") );
4605 // We have to call this here because the label in question might just have
4606 // been added and its position is not known yet
4608 RecalculatePositions(true /* no refresh */);
4610 MoveToItem((size_t)index
);
4613 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4619 wxString str_upper
= str
.Upper();
4623 size_t count
= GetItemCount();
4624 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4626 wxListLineData
*line
= GetLine(i
);
4627 wxString line_upper
= line
->GetText(0).Upper();
4630 if (line_upper
== str_upper
)
4635 if (line_upper
.find(str_upper
) == 0)
4643 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4649 size_t count
= GetItemCount();
4650 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4652 wxListLineData
*line
= GetLine(i
);
4654 line
->GetItem( 0, item
);
4655 if (item
.m_data
== data
)
4662 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4665 GetVisibleLinesRange( &topItem
, NULL
);
4668 GetItemPosition( GetItemCount() - 1, p
);
4672 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4673 if ( id
>= 0 && id
< (long)GetItemCount() )
4679 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4681 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4683 size_t count
= GetItemCount();
4685 if ( InReportView() )
4687 size_t current
= y
/ GetLineHeight();
4688 if ( current
< count
)
4690 flags
= HitTestLine(current
, x
, y
);
4697 // TODO: optimize it too! this is less simple than for report view but
4698 // enumerating all items is still not a way to do it!!
4699 for ( size_t current
= 0; current
< count
; current
++ )
4701 flags
= HitTestLine(current
, x
, y
);
4710 // ----------------------------------------------------------------------------
4712 // ----------------------------------------------------------------------------
4714 void wxListMainWindow::InsertItem( wxListItem
&item
)
4716 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4718 int count
= GetItemCount();
4719 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4721 if (item
.m_itemId
> count
)
4722 item
.m_itemId
= count
;
4724 size_t id
= item
.m_itemId
;
4728 if ( InReportView() )
4730 ResetVisibleLinesRange();
4732 // calculate the width of the item and adjust the max column width
4733 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4734 int width
= GetItemWidthWithImage(&item
);
4735 item
.SetWidth(width
);
4736 if (width
> pWidthInfo
->nMaxWidth
)
4737 pWidthInfo
->nMaxWidth
= width
;
4740 wxListLineData
*line
= new wxListLineData(this);
4742 line
->SetItem( item
.m_col
, item
);
4744 m_lines
.Insert( line
, id
);
4748 // If an item is selected at or below the point of insertion, we need to
4749 // increment the member variables because the current row's index has gone
4751 if ( HasCurrent() && m_current
>= id
)
4754 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4756 RefreshLines(id
, GetItemCount() - 1);
4759 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4762 if ( InReportView() )
4764 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4765 item
.m_width
= GetTextLength( item
.m_text
);
4767 wxListHeaderData
*column
= new wxListHeaderData( item
);
4768 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4770 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4773 wxListHeaderDataList::compatibility_iterator
4774 node
= m_columns
.Item( col
);
4775 m_columns
.Insert( node
, column
);
4776 m_aColWidths
.Insert( colWidthInfo
, col
);
4780 m_columns
.Append( column
);
4781 m_aColWidths
.Add( colWidthInfo
);
4786 // update all the items
4787 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4789 wxListLineData
* const line
= GetLine(i
);
4790 wxListItemData
* const data
= new wxListItemData(this);
4792 line
->m_items
.Insert(col
, data
);
4794 line
->m_items
.Append(data
);
4798 // invalidate it as it has to be recalculated
4803 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4806 wxClientDC
dc(this);
4808 dc
.SetFont( GetFont() );
4810 if (item
->GetImage() != -1)
4813 GetImageSize( item
->GetImage(), ix
, iy
);
4817 if (!item
->GetText().empty())
4820 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4827 // ----------------------------------------------------------------------------
4829 // ----------------------------------------------------------------------------
4831 wxListCtrlCompare list_ctrl_compare_func_2
;
4832 long list_ctrl_compare_data
;
4834 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4836 wxListLineData
*line1
= *arg1
;
4837 wxListLineData
*line2
= *arg2
;
4839 line1
->GetItem( 0, item
);
4840 wxUIntPtr data1
= item
.m_data
;
4841 line2
->GetItem( 0, item
);
4842 wxUIntPtr data2
= item
.m_data
;
4843 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4846 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4848 // selections won't make sense any more after sorting the items so reset
4850 HighlightAll(false);
4853 list_ctrl_compare_func_2
= fn
;
4854 list_ctrl_compare_data
= data
;
4855 m_lines
.Sort( list_ctrl_compare_func_1
);
4859 // ----------------------------------------------------------------------------
4861 // ----------------------------------------------------------------------------
4863 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4866 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4867 wxScrolledWindow::OnScroll(event
);
4869 HandleOnScroll( event
);
4872 // update our idea of which lines are shown when we redraw the window the
4874 ResetVisibleLinesRange();
4876 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4878 wxGenericListCtrl
* lc
= GetListCtrl();
4879 wxCHECK_RET( lc
, _T("no listctrl window?") );
4881 lc
->m_headerWin
->Refresh();
4882 lc
->m_headerWin
->Update();
4886 int wxListMainWindow::GetCountPerPage() const
4888 if ( !m_linesPerPage
)
4890 wxConstCast(this, wxListMainWindow
)->
4891 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4894 return m_linesPerPage
;
4897 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4899 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4901 if ( m_lineFrom
== (size_t)-1 )
4903 size_t count
= GetItemCount();
4906 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4908 // this may happen if SetScrollbars() hadn't been called yet
4909 if ( m_lineFrom
>= count
)
4910 m_lineFrom
= count
- 1;
4912 // we redraw one extra line but this is needed to make the redrawing
4913 // logic work when there is a fractional number of lines on screen
4914 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4915 if ( m_lineTo
>= count
)
4916 m_lineTo
= count
- 1;
4918 else // empty control
4921 m_lineTo
= (size_t)-1;
4925 wxASSERT_MSG( IsEmpty() ||
4926 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4927 _T("GetVisibleLinesRange() returns incorrect result") );
4935 // -------------------------------------------------------------------------------------
4936 // wxGenericListCtrl
4937 // -------------------------------------------------------------------------------------
4939 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4941 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4942 EVT_SIZE(wxGenericListCtrl::OnSize
)
4945 wxGenericListCtrl::wxGenericListCtrl()
4947 m_imageListNormal
= (wxImageList
*) NULL
;
4948 m_imageListSmall
= (wxImageList
*) NULL
;
4949 m_imageListState
= (wxImageList
*) NULL
;
4951 m_ownsImageListNormal
=
4952 m_ownsImageListSmall
=
4953 m_ownsImageListState
= false;
4955 m_mainWin
= (wxListMainWindow
*) NULL
;
4956 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4960 wxGenericListCtrl::~wxGenericListCtrl()
4962 if (m_ownsImageListNormal
)
4963 delete m_imageListNormal
;
4964 if (m_ownsImageListSmall
)
4965 delete m_imageListSmall
;
4966 if (m_ownsImageListState
)
4967 delete m_imageListState
;
4970 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4976 GetThemeMetric( kThemeMetricListHeaderHeight
, &h
);
4978 // we use 'g' to get the descent, too
4980 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4981 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4984 // only update if changed
4985 if ( h
!= m_headerHeight
)
4990 ResizeReportView(true);
4991 else //why is this needed if it doesn't have a header?
4992 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4997 void wxGenericListCtrl::CreateHeaderWindow()
4999 m_headerWin
= new wxListHeaderWindow
5001 this, wxID_ANY
, m_mainWin
,
5003 wxSize(GetClientSize().x
, m_headerHeight
),
5006 CalculateAndSetHeaderHeight();
5009 bool wxGenericListCtrl::Create(wxWindow
*parent
,
5014 const wxValidator
&validator
,
5015 const wxString
&name
)
5019 m_imageListState
= (wxImageList
*) NULL
;
5020 m_ownsImageListNormal
=
5021 m_ownsImageListSmall
=
5022 m_ownsImageListState
= false;
5024 m_mainWin
= (wxListMainWindow
*) NULL
;
5025 m_headerWin
= (wxListHeaderWindow
*) NULL
;
5029 if ( !(style
& wxLC_MASK_TYPE
) )
5031 style
= style
| wxLC_LIST
;
5034 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
5037 // this window itself shouldn't get the focus, only m_mainWin should
5040 // don't create the inner window with the border
5041 style
&= ~wxBORDER_MASK
;
5043 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
5046 // Human Interface Guidelines ask us for a special font in this case
5047 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
)
5050 font
.MacCreateFromThemeFont( kThemeViewsFont
);
5055 if ( InReportView() )
5057 CreateHeaderWindow();
5063 font
.MacCreateFromThemeFont( kThemeSmallSystemFont
);
5064 m_headerWin
->SetFont( font
);
5065 CalculateAndSetHeaderHeight();
5069 if ( HasFlag(wxLC_NO_HEADER
) )
5070 // VZ: why do we create it at all then?
5071 m_headerWin
->Show( false );
5074 SetInitialSize(size
);
5079 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
5081 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
5082 _T("wxLC_VIRTUAL can't be [un]set") );
5084 long flag
= GetWindowStyle();
5088 if (style
& wxLC_MASK_TYPE
)
5089 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5090 if (style
& wxLC_MASK_ALIGN
)
5091 flag
&= ~wxLC_MASK_ALIGN
;
5092 if (style
& wxLC_MASK_SORT
)
5093 flag
&= ~wxLC_MASK_SORT
;
5101 SetWindowStyleFlag( flag
);
5104 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5108 m_mainWin
->DeleteEverything();
5110 // has the header visibility changed?
5111 bool hasHeader
= HasHeader();
5112 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
5114 if ( hasHeader
!= willHaveHeader
)
5121 // don't delete, just hide, as we can reuse it later
5122 m_headerWin
->Show(false);
5124 //else: nothing to do
5126 else // must show header
5130 CreateHeaderWindow();
5132 else // already have it, just show
5134 m_headerWin
->Show( true );
5138 ResizeReportView(willHaveHeader
);
5142 wxWindow::SetWindowStyleFlag( flag
);
5145 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5147 m_mainWin
->GetColumn( col
, item
);
5151 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5153 m_mainWin
->SetColumn( col
, item
);
5157 int wxGenericListCtrl::GetColumnWidth( int col
) const
5159 return m_mainWin
->GetColumnWidth( col
);
5162 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5164 m_mainWin
->SetColumnWidth( col
, width
);
5168 int wxGenericListCtrl::GetCountPerPage() const
5170 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5173 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5175 m_mainWin
->GetItem( info
);
5179 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5181 m_mainWin
->SetItem( info
);
5185 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5188 info
.m_text
= label
;
5189 info
.m_mask
= wxLIST_MASK_TEXT
;
5190 info
.m_itemId
= index
;
5194 info
.m_image
= imageId
;
5195 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5198 m_mainWin
->SetItem(info
);
5202 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5204 return m_mainWin
->GetItemState( item
, stateMask
);
5207 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5209 m_mainWin
->SetItemState( item
, state
, stateMask
);
5214 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5216 return SetItemColumnImage(item
, 0, image
);
5220 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5223 info
.m_image
= image
;
5224 info
.m_mask
= wxLIST_MASK_IMAGE
;
5225 info
.m_itemId
= item
;
5226 info
.m_col
= column
;
5227 m_mainWin
->SetItem( info
);
5231 wxString
wxGenericListCtrl::GetItemText( long item
) const
5233 return m_mainWin
->GetItemText(item
);
5236 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5238 m_mainWin
->SetItemText(item
, str
);
5241 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5244 info
.m_mask
= wxLIST_MASK_DATA
;
5245 info
.m_itemId
= item
;
5246 m_mainWin
->GetItem( info
);
5250 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
5253 info
.m_mask
= wxLIST_MASK_DATA
;
5254 info
.m_itemId
= item
;
5256 m_mainWin
->SetItem( info
);
5260 wxRect
wxGenericListCtrl::GetViewRect() const
5262 return m_mainWin
->GetViewRect();
5265 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
5267 m_mainWin
->GetItemRect( item
, rect
);
5268 if ( m_mainWin
->HasHeader() )
5269 rect
.y
+= m_headerHeight
+ 1;
5273 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5275 m_mainWin
->GetItemPosition( item
, pos
);
5279 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5284 int wxGenericListCtrl::GetItemCount() const
5286 return m_mainWin
->GetItemCount();
5289 int wxGenericListCtrl::GetColumnCount() const
5291 return m_mainWin
->GetColumnCount();
5294 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5296 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5299 wxSize
wxGenericListCtrl::GetItemSpacing() const
5301 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5303 return wxSize(spacing
, spacing
);
5306 #if WXWIN_COMPATIBILITY_2_6
5307 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5309 return m_mainWin
->GetItemSpacing( isSmall
);
5311 #endif // WXWIN_COMPATIBILITY_2_6
5313 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5316 info
.m_itemId
= item
;
5317 info
.SetTextColour( col
);
5318 m_mainWin
->SetItem( info
);
5321 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5324 info
.m_itemId
= item
;
5325 m_mainWin
->GetItem( info
);
5326 return info
.GetTextColour();
5329 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5332 info
.m_itemId
= item
;
5333 info
.SetBackgroundColour( col
);
5334 m_mainWin
->SetItem( info
);
5337 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5340 info
.m_itemId
= item
;
5341 m_mainWin
->GetItem( info
);
5342 return info
.GetBackgroundColour();
5345 int wxGenericListCtrl::GetScrollPos( int orient
) const
5347 return m_mainWin
->GetScrollPos( orient
);
5350 void wxGenericListCtrl::SetScrollPos( int orient
, int pos
, bool refresh
)
5352 m_mainWin
->SetScrollPos( orient
, pos
, refresh
);
5355 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5358 info
.m_itemId
= item
;
5360 m_mainWin
->SetItem( info
);
5363 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5366 info
.m_itemId
= item
;
5367 m_mainWin
->GetItem( info
);
5368 return info
.GetFont();
5371 int wxGenericListCtrl::GetSelectedItemCount() const
5373 return m_mainWin
->GetSelectedItemCount();
5376 wxColour
wxGenericListCtrl::GetTextColour() const
5378 return GetForegroundColour();
5381 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5383 SetForegroundColour(col
);
5386 long wxGenericListCtrl::GetTopItem() const
5389 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5393 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5395 return m_mainWin
->GetNextItem( item
, geom
, state
);
5398 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5400 if (which
== wxIMAGE_LIST_NORMAL
)
5401 return m_imageListNormal
;
5402 else if (which
== wxIMAGE_LIST_SMALL
)
5403 return m_imageListSmall
;
5404 else if (which
== wxIMAGE_LIST_STATE
)
5405 return m_imageListState
;
5407 return (wxImageList
*) NULL
;
5410 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5412 if ( which
== wxIMAGE_LIST_NORMAL
)
5414 if (m_ownsImageListNormal
)
5415 delete m_imageListNormal
;
5416 m_imageListNormal
= imageList
;
5417 m_ownsImageListNormal
= false;
5419 else if ( which
== wxIMAGE_LIST_SMALL
)
5421 if (m_ownsImageListSmall
)
5422 delete m_imageListSmall
;
5423 m_imageListSmall
= imageList
;
5424 m_ownsImageListSmall
= false;
5426 else if ( which
== wxIMAGE_LIST_STATE
)
5428 if (m_ownsImageListState
)
5429 delete m_imageListState
;
5430 m_imageListState
= imageList
;
5431 m_ownsImageListState
= false;
5434 m_mainWin
->SetImageList( imageList
, which
);
5437 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5439 SetImageList(imageList
, which
);
5440 if ( which
== wxIMAGE_LIST_NORMAL
)
5441 m_ownsImageListNormal
= true;
5442 else if ( which
== wxIMAGE_LIST_SMALL
)
5443 m_ownsImageListSmall
= true;
5444 else if ( which
== wxIMAGE_LIST_STATE
)
5445 m_ownsImageListState
= true;
5448 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5453 bool wxGenericListCtrl::DeleteItem( long item
)
5455 m_mainWin
->DeleteItem( item
);
5459 bool wxGenericListCtrl::DeleteAllItems()
5461 m_mainWin
->DeleteAllItems();
5465 bool wxGenericListCtrl::DeleteAllColumns()
5467 size_t count
= m_mainWin
->m_columns
.GetCount();
5468 for ( size_t n
= 0; n
< count
; n
++ )
5473 void wxGenericListCtrl::ClearAll()
5475 m_mainWin
->DeleteEverything();
5478 bool wxGenericListCtrl::DeleteColumn( int col
)
5480 m_mainWin
->DeleteColumn( col
);
5482 // if we don't have the header any longer, we need to relayout the window
5483 if ( !GetColumnCount() )
5484 ResizeReportView(false /* no header */);
5488 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5489 wxClassInfo
* textControlClass
)
5491 return m_mainWin
->EditLabel( item
, textControlClass
);
5494 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5496 return m_mainWin
->GetEditControl();
5499 bool wxGenericListCtrl::EnsureVisible( long item
)
5501 m_mainWin
->EnsureVisible( item
);
5505 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5507 return m_mainWin
->FindItem( start
, str
, partial
);
5510 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5512 return m_mainWin
->FindItem( start
, data
);
5515 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5516 int WXUNUSED(direction
))
5518 return m_mainWin
->FindItem( pt
);
5521 // TODO: sub item hit testing
5522 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5524 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5527 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5529 m_mainWin
->InsertItem( info
);
5530 return info
.m_itemId
;
5533 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5536 info
.m_text
= label
;
5537 info
.m_mask
= wxLIST_MASK_TEXT
;
5538 info
.m_itemId
= index
;
5539 return InsertItem( info
);
5542 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5545 info
.m_mask
= wxLIST_MASK_IMAGE
;
5546 info
.m_image
= imageIndex
;
5547 info
.m_itemId
= index
;
5548 return InsertItem( info
);
5551 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5554 info
.m_text
= label
;
5555 info
.m_image
= imageIndex
;
5556 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5557 info
.m_itemId
= index
;
5558 return InsertItem( info
);
5561 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5563 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5565 m_mainWin
->InsertColumn( col
, item
);
5567 // if we hadn't had a header before but have one now
5568 // then we need to relayout the window
5569 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5570 ResizeReportView(true /* have header */);
5572 m_headerWin
->Refresh();
5577 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5578 int format
, int width
)
5581 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5582 item
.m_text
= heading
;
5585 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5586 item
.m_width
= width
;
5589 item
.m_format
= format
;
5591 return InsertColumn( col
, item
);
5594 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
5596 return m_mainWin
->ScrollList(dx
, dy
);
5600 // fn is a function which takes 3 long arguments: item1, item2, data.
5601 // item1 is the long data associated with a first item (NOT the index).
5602 // item2 is the long data associated with a second item (NOT the index).
5603 // data is the same value as passed to SortItems.
5604 // The return value is a negative number if the first item should precede the second
5605 // item, a positive number of the second item should precede the first,
5606 // or zero if the two items are equivalent.
5607 // data is arbitrary data to be passed to the sort function.
5609 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5611 m_mainWin
->SortItems( fn
, data
);
5615 // ----------------------------------------------------------------------------
5617 // ----------------------------------------------------------------------------
5619 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5624 ResizeReportView(m_mainWin
->HasHeader());
5625 m_mainWin
->RecalculatePositions();
5628 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5631 GetClientSize( &cw
, &ch
);
5635 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5636 if(ch
> m_headerHeight
)
5637 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5638 cw
, ch
- m_headerHeight
- 1 );
5640 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5643 else // no header window
5645 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5649 void wxGenericListCtrl::OnInternalIdle()
5651 wxWindow::OnInternalIdle();
5653 // do it only if needed
5654 if ( !m_mainWin
->m_dirty
)
5657 m_mainWin
->RecalculatePositions();
5660 // ----------------------------------------------------------------------------
5662 // ----------------------------------------------------------------------------
5664 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5668 m_mainWin
->SetBackgroundColour( colour
);
5669 m_mainWin
->m_dirty
= true;
5675 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5677 if ( !wxWindow::SetForegroundColour( colour
) )
5682 m_mainWin
->SetForegroundColour( colour
);
5683 m_mainWin
->m_dirty
= true;
5687 m_headerWin
->SetForegroundColour( colour
);
5692 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5694 if ( !wxWindow::SetFont( font
) )
5699 m_mainWin
->SetFont( font
);
5700 m_mainWin
->m_dirty
= true;
5705 m_headerWin
->SetFont( font
);
5706 CalculateAndSetHeaderHeight();
5716 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5719 // Use the same color scheme as wxListBox
5720 return wxListBox::GetClassDefaultAttributes(variant
);
5722 wxUnusedVar(variant
);
5723 wxVisualAttributes attr
;
5724 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5725 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5726 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5731 // ----------------------------------------------------------------------------
5732 // methods forwarded to m_mainWin
5733 // ----------------------------------------------------------------------------
5735 #if wxUSE_DRAG_AND_DROP
5737 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5739 m_mainWin
->SetDropTarget( dropTarget
);
5742 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5744 return m_mainWin
->GetDropTarget();
5749 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5751 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5754 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5756 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5759 wxColour
wxGenericListCtrl::GetForegroundColour() const
5761 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5764 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5767 return m_mainWin
->PopupMenu( menu
, x
, y
);
5773 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5775 m_mainWin
->DoClientToScreen(x
, y
);
5778 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5780 m_mainWin
->DoScreenToClient(x
, y
);
5783 void wxGenericListCtrl::SetFocus()
5785 // The test in window.cpp fails as we are a composite
5786 // window, so it checks against "this", but not m_mainWin.
5787 if ( DoFindFocus() != this )
5788 m_mainWin
->SetFocus();
5791 wxSize
wxGenericListCtrl::DoGetBestSize() const
5793 // Something is better than nothing...
5794 // 100x80 is what the MSW version will get from the default
5795 // wxControl::DoGetBestSize
5796 return wxSize(100, 80);
5799 // ----------------------------------------------------------------------------
5800 // virtual list control support
5801 // ----------------------------------------------------------------------------
5803 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5805 // this is a pure virtual function, in fact - which is not really pure
5806 // because the controls which are not virtual don't need to implement it
5807 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5809 return wxEmptyString
;
5812 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5814 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5816 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5820 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5823 return OnGetItemImage(item
);
5829 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5831 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5832 _T("invalid item index in OnGetItemAttr()") );
5834 // no attributes by default
5838 void wxGenericListCtrl::SetItemCount(long count
)
5840 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5842 m_mainWin
->SetItemCount(count
);
5845 void wxGenericListCtrl::RefreshItem(long item
)
5847 m_mainWin
->RefreshLine(item
);
5850 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5852 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5855 // Generic wxListCtrl is more or less a container for two other
5856 // windows which drawings are done upon. These are namely
5857 // 'm_headerWin' and 'm_mainWin'.
5858 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5859 // behaviour wxListCtrl has under wxMSW.
5861 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5865 // The easy case, no rectangle specified.
5867 m_headerWin
->Refresh(eraseBackground
);
5870 m_mainWin
->Refresh(eraseBackground
);
5874 // Refresh the header window
5877 wxRect rectHeader
= m_headerWin
->GetRect();
5878 rectHeader
.Intersect(*rect
);
5879 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5882 m_headerWin
->GetPosition(&x
, &y
);
5883 rectHeader
.Offset(-x
, -y
);
5884 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5888 // Refresh the main window
5891 wxRect rectMain
= m_mainWin
->GetRect();
5892 rectMain
.Intersect(*rect
);
5893 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5896 m_mainWin
->GetPosition(&x
, &y
);
5897 rectMain
.Offset(-x
, -y
);
5898 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5904 #endif // wxUSE_LISTCTRL