1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 // 1. we need to implement searching/sorting for virtual controls somehow
14 // 2. when changing selection the lines are refreshed twice
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #include "wx/listctrl.h"
28 #if (!defined(__WXMSW__) || defined(__WXUNIVERSAL__)) && !defined(__WXMAC__)
29 // if we have a native version, its implementation file does all this
30 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
31 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
32 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
34 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
38 #include "wx/scrolwin.h"
40 #include "wx/settings.h"
41 #include "wx/dynarray.h"
42 #include "wx/dcclient.h"
43 #include "wx/dcscreen.h"
45 #include "wx/settings.h"
48 #include "wx/imaglist.h"
49 #include "wx/selstore.h"
50 #include "wx/renderer.h"
53 #include "wx/mac/private.h"
57 // NOTE: If using the wxListBox visual attributes works everywhere then this can
58 // be removed, as well as the #else case below.
59 #define _USE_VISATTR 0
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // // the height of the header window (FIXME: should depend on its font!)
67 // static const int HEADER_HEIGHT = 23;
69 static const int SCROLL_UNIT_X
= 15;
71 // the spacing between the lines (in report mode)
72 static const int LINE_SPACING
= 0;
74 // extra margins around the text label
76 static const int EXTRA_WIDTH
= 6;
78 static const int EXTRA_WIDTH
= 4;
80 static const int EXTRA_HEIGHT
= 4;
82 // margin between the window and the items
83 static const int EXTRA_BORDER_X
= 2;
84 static const int EXTRA_BORDER_Y
= 2;
86 // offset for the header window
87 static const int HEADER_OFFSET_X
= 0;
88 static const int HEADER_OFFSET_Y
= 0;
90 // margin between rows of icons in [small] icon view
91 static const int MARGIN_BETWEEN_ROWS
= 6;
93 // when autosizing the columns, add some slack
94 static const int AUTOSIZE_COL_MARGIN
= 10;
96 // default width for the header columns
97 static const int WIDTH_COL_DEFAULT
= 80;
99 // the space between the image and the text in the report mode
100 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
102 // the space between the image and the text in the report mode in header
103 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
105 // ============================================================================
107 // ============================================================================
109 //-----------------------------------------------------------------------------
110 // wxColWidthInfo (internal)
111 //-----------------------------------------------------------------------------
113 struct wxColWidthInfo
116 bool bNeedsUpdate
; // only set to true when an item whose
117 // width == nMaxWidth is removed
119 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
122 bNeedsUpdate
= needsUpdate
;
126 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
128 //-----------------------------------------------------------------------------
129 // wxListItemData (internal)
130 //-----------------------------------------------------------------------------
135 wxListItemData(wxListMainWindow
*owner
);
138 void SetItem( const wxListItem
&info
);
139 void SetImage( int image
) { m_image
= image
; }
140 void SetData( wxUIntPtr data
) { m_data
= data
; }
141 void SetPosition( int x
, int y
);
142 void SetSize( int width
, int height
);
144 bool HasText() const { return !m_text
.empty(); }
145 const wxString
& GetText() const { return m_text
; }
146 void SetText(const wxString
& text
) { m_text
= text
; }
148 // we can't use empty string for measuring the string width/height, so
149 // always return something
150 wxString
GetTextForMeasuring() const
152 wxString s
= GetText();
159 bool IsHit( int x
, int y
) const;
163 int GetWidth() const;
164 int GetHeight() const;
166 int GetImage() const { return m_image
; }
167 bool HasImage() const { return GetImage() != -1; }
169 void GetItem( wxListItem
&info
) const;
171 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
172 wxListItemAttr
*GetAttr() const { return m_attr
; }
175 // the item image or -1
178 // user data associated with the item
181 // the item coordinates are not used in report mode; instead this pointer is
182 // NULL and the owner window is used to retrieve the item position and size
185 // the list ctrl we are in
186 wxListMainWindow
*m_owner
;
188 // custom attributes or NULL
189 wxListItemAttr
*m_attr
;
192 // common part of all ctors
198 //-----------------------------------------------------------------------------
199 // wxListHeaderData (internal)
200 //-----------------------------------------------------------------------------
202 class wxListHeaderData
: public wxObject
206 wxListHeaderData( const wxListItem
&info
);
207 void SetItem( const wxListItem
&item
);
208 void SetPosition( int x
, int y
);
209 void SetWidth( int w
);
210 void SetState( int state
);
211 void SetFormat( int format
);
212 void SetHeight( int h
);
213 bool HasImage() const;
215 bool HasText() const { return !m_text
.empty(); }
216 const wxString
& GetText() const { return m_text
; }
217 void SetText(const wxString
& text
) { m_text
= text
; }
219 void GetItem( wxListItem
&item
);
221 bool IsHit( int x
, int y
) const;
222 int GetImage() const;
223 int GetWidth() const;
224 int GetFormat() const;
225 int GetState() const;
242 //-----------------------------------------------------------------------------
243 // wxListLineData (internal)
244 //-----------------------------------------------------------------------------
246 WX_DECLARE_EXPORTED_LIST(wxListItemData
, wxListItemDataList
);
247 #include "wx/listimpl.cpp"
248 WX_DEFINE_LIST(wxListItemDataList
)
253 // the list of subitems: only may have more than one item in report mode
254 wxListItemDataList m_items
;
256 // this is not used in report view
268 // the part to be highlighted
269 wxRect m_rectHighlight
;
271 // extend all our rects to be centered inside the one of given width
272 void ExtendWidth(wxCoord w
)
274 wxASSERT_MSG( m_rectAll
.width
<= w
,
275 _T("width can only be increased") );
278 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
279 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
280 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
285 // is this item selected? [NB: not used in virtual mode]
288 // back pointer to the list ctrl
289 wxListMainWindow
*m_owner
;
292 wxListLineData(wxListMainWindow
*owner
);
296 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
300 // are we in report mode?
301 inline bool InReportView() const;
303 // are we in virtual report mode?
304 inline bool IsVirtual() const;
306 // these 2 methods shouldn't be called for report view controls, in that
307 // case we determine our position/size ourselves
309 // calculate the size of the line
310 void CalculateSize( wxDC
*dc
, int spacing
);
312 // remember the position this line appears at
313 void SetPosition( int x
, int y
, int spacing
);
317 void SetImage( int image
) { SetImage(0, image
); }
318 int GetImage() const { return GetImage(0); }
319 void SetImage( int index
, int image
);
320 int GetImage( int index
) const;
322 bool HasImage() const { return GetImage() != -1; }
323 bool HasText() const { return !GetText(0).empty(); }
325 void SetItem( int index
, const wxListItem
&info
);
326 void GetItem( int index
, wxListItem
&info
);
328 wxString
GetText(int index
) const;
329 void SetText( int index
, const wxString
& s
);
331 wxListItemAttr
*GetAttr() const;
332 void SetAttr(wxListItemAttr
*attr
);
334 // return true if the highlighting really changed
335 bool Highlight( bool on
);
337 void ReverseHighlight();
339 bool IsHighlighted() const
341 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
343 return m_highlighted
;
346 // draw the line on the given DC in icon/list mode
347 void Draw( wxDC
*dc
);
349 // the same in report mode
350 void DrawInReportMode( wxDC
*dc
,
352 const wxRect
& rectHL
,
356 // set the line to contain num items (only can be > 1 in report mode)
357 void InitItems( int num
);
359 // get the mode (i.e. style) of the list control
360 inline int GetMode() const;
362 // prepare the DC for drawing with these item's attributes, return true if
363 // we need to draw the items background to highlight it, false otherwise
364 bool SetAttributes(wxDC
*dc
,
365 const wxListItemAttr
*attr
,
368 // draw the text on the DC with the correct justification; also add an
369 // ellipsis if the text is too large to fit in the current width
370 void DrawTextFormatted(wxDC
*dc
,
371 const wxString
&text
,
374 int yMid
, // this is middle, not top, of the text
378 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
379 #include "wx/arrimpl.cpp"
380 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
382 //-----------------------------------------------------------------------------
383 // wxListHeaderWindow (internal)
384 //-----------------------------------------------------------------------------
386 class wxListHeaderWindow
: public wxWindow
389 wxListMainWindow
*m_owner
;
390 const wxCursor
*m_currentCursor
;
391 wxCursor
*m_resizeCursor
;
394 // column being resized or -1
397 // divider line position in logical (unscrolled) coords
400 // minimal position beyond which the divider line
401 // can't be dragged in logical coords
405 wxListHeaderWindow();
407 wxListHeaderWindow( wxWindow
*win
,
409 wxListMainWindow
*owner
,
410 const wxPoint
&pos
= wxDefaultPosition
,
411 const wxSize
&size
= wxDefaultSize
,
413 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
415 virtual ~wxListHeaderWindow();
418 void AdjustDC( wxDC
& dc
);
420 void OnPaint( wxPaintEvent
&event
);
421 void OnMouse( wxMouseEvent
&event
);
422 void OnSetFocus( wxFocusEvent
&event
);
428 // common part of all ctors
431 // generate and process the list event of the given type, return true if
432 // it wasn't vetoed, i.e. if we should proceed
433 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
435 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
436 DECLARE_EVENT_TABLE()
439 //-----------------------------------------------------------------------------
440 // wxListRenameTimer (internal)
441 //-----------------------------------------------------------------------------
443 class wxListRenameTimer
: public wxTimer
446 wxListMainWindow
*m_owner
;
449 wxListRenameTimer( wxListMainWindow
*owner
);
453 //-----------------------------------------------------------------------------
454 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
455 //-----------------------------------------------------------------------------
457 class wxListTextCtrlWrapper
: public wxEvtHandler
460 // NB: text must be a valid object but not Create()d yet
461 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
465 wxTextCtrl
*GetText() const { return m_text
; }
467 void AcceptChangesAndFinish();
470 void OnChar( wxKeyEvent
&event
);
471 void OnKeyUp( wxKeyEvent
&event
);
472 void OnKillFocus( wxFocusEvent
&event
);
474 bool AcceptChanges();
478 wxListMainWindow
*m_owner
;
480 wxString m_startValue
;
483 bool m_aboutToFinish
;
485 DECLARE_EVENT_TABLE()
488 //-----------------------------------------------------------------------------
489 // wxListMainWindow (internal)
490 //-----------------------------------------------------------------------------
492 WX_DECLARE_EXPORTED_LIST(wxListHeaderData
, wxListHeaderDataList
);
493 #include "wx/listimpl.cpp"
494 WX_DEFINE_LIST(wxListHeaderDataList
)
496 class wxListMainWindow
: public wxScrolledWindow
500 wxListMainWindow( wxWindow
*parent
,
502 const wxPoint
& pos
= wxDefaultPosition
,
503 const wxSize
& size
= wxDefaultSize
,
505 const wxString
&name
= _T("listctrlmainwindow") );
507 virtual ~wxListMainWindow();
509 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
511 // return true if this is a virtual list control
512 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
514 // return true if the control is in report mode
515 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
517 // return true if we are in single selection mode, false if multi sel
518 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
520 // do we have a header window?
521 bool HasHeader() const
522 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
524 void HighlightAll( bool on
);
526 // all these functions only do something if the line is currently visible
528 // change the line "selected" state, return true if it really changed
529 bool HighlightLine( size_t line
, bool highlight
= true);
531 // as HighlightLine() but do it for the range of lines: this is incredibly
532 // more efficient for virtual list controls!
534 // NB: unlike HighlightLine() this one does refresh the lines on screen
535 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
537 // toggle the line state and refresh it
538 void ReverseHighlight( size_t line
)
539 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
541 // return true if the line is highlighted
542 bool IsHighlighted(size_t line
) const;
544 // refresh one or several lines at once
545 void RefreshLine( size_t line
);
546 void RefreshLines( size_t lineFrom
, size_t lineTo
);
548 // refresh all selected items
549 void RefreshSelected();
551 // refresh all lines below the given one: the difference with
552 // RefreshLines() is that the index here might not be a valid one (happens
553 // when the last line is deleted)
554 void RefreshAfter( size_t lineFrom
);
556 // the methods which are forwarded to wxListLineData itself in list/icon
557 // modes but are here because the lines don't store their positions in the
560 // get the bound rect for the entire line
561 wxRect
GetLineRect(size_t line
) const;
563 // get the bound rect of the label
564 wxRect
GetLineLabelRect(size_t line
) const;
566 // get the bound rect of the items icon (only may be called if we do have
568 wxRect
GetLineIconRect(size_t line
) const;
570 // get the rect to be highlighted when the item has focus
571 wxRect
GetLineHighlightRect(size_t line
) const;
573 // get the size of the total line rect
574 wxSize
GetLineSize(size_t line
) const
575 { return GetLineRect(line
).GetSize(); }
577 // return the hit code for the corresponding position (in this line)
578 long HitTestLine(size_t line
, int x
, int y
) const;
580 // bring the selected item into view, scrolling to it if necessary
581 void MoveToItem(size_t item
);
583 // bring the current item into view
584 void MoveToFocus() { MoveToItem(m_current
); }
586 // start editing the label of the given item
587 wxTextCtrl
*EditLabel(long item
,
588 wxClassInfo
* textControlClass
= CLASSINFO(wxTextCtrl
));
589 wxTextCtrl
*GetEditControl() const
591 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
594 void FinishEditing(wxTextCtrl
*text
)
597 m_textctrlWrapper
= NULL
;
598 SetFocusIgnoringChildren();
601 // suspend/resume redrawing the control
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 // if this is > 0, the control is frozen and doesn't redraw itself
852 size_t m_freezeCount
;
854 // wrapper around the text control currently used for in place editing or
855 // NULL if no item is being edited
856 wxListTextCtrlWrapper
*m_textctrlWrapper
;
859 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
860 DECLARE_EVENT_TABLE()
862 friend class wxGenericListCtrl
;
866 wxListItemData::~wxListItemData()
868 // in the virtual list control the attributes are managed by the main
869 // program, so don't delete them
870 if ( !m_owner
->IsVirtual() )
876 void wxListItemData::Init()
884 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
890 if ( owner
->InReportView() )
896 void wxListItemData::SetItem( const wxListItem
&info
)
898 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
899 SetText(info
.m_text
);
900 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
901 m_image
= info
.m_image
;
902 if ( info
.m_mask
& wxLIST_MASK_DATA
)
903 m_data
= info
.m_data
;
905 if ( info
.HasAttributes() )
908 m_attr
->AssignFrom(*info
.GetAttributes());
910 m_attr
= new wxListItemAttr(*info
.GetAttributes());
918 m_rect
->width
= info
.m_width
;
922 void wxListItemData::SetPosition( int x
, int y
)
924 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
930 void wxListItemData::SetSize( int width
, int height
)
932 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
935 m_rect
->width
= width
;
937 m_rect
->height
= height
;
940 bool wxListItemData::IsHit( int x
, int y
) const
942 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
944 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
947 int wxListItemData::GetX() const
949 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
954 int wxListItemData::GetY() const
956 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
961 int wxListItemData::GetWidth() const
963 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
965 return m_rect
->width
;
968 int wxListItemData::GetHeight() const
970 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
972 return m_rect
->height
;
975 void wxListItemData::GetItem( wxListItem
&info
) const
977 long mask
= info
.m_mask
;
979 // by default, get everything for backwards compatibility
982 if ( mask
& wxLIST_MASK_TEXT
)
983 info
.m_text
= m_text
;
984 if ( mask
& wxLIST_MASK_IMAGE
)
985 info
.m_image
= m_image
;
986 if ( mask
& wxLIST_MASK_DATA
)
987 info
.m_data
= m_data
;
991 if ( m_attr
->HasTextColour() )
992 info
.SetTextColour(m_attr
->GetTextColour());
993 if ( m_attr
->HasBackgroundColour() )
994 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
995 if ( m_attr
->HasFont() )
996 info
.SetFont(m_attr
->GetFont());
1000 //-----------------------------------------------------------------------------
1002 //-----------------------------------------------------------------------------
1004 void wxListHeaderData::Init()
1016 wxListHeaderData::wxListHeaderData()
1021 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1028 void wxListHeaderData::SetItem( const wxListItem
&item
)
1030 m_mask
= item
.m_mask
;
1032 if ( m_mask
& wxLIST_MASK_TEXT
)
1033 m_text
= item
.m_text
;
1035 if ( m_mask
& wxLIST_MASK_IMAGE
)
1036 m_image
= item
.m_image
;
1038 if ( m_mask
& wxLIST_MASK_FORMAT
)
1039 m_format
= item
.m_format
;
1041 if ( m_mask
& wxLIST_MASK_WIDTH
)
1042 SetWidth(item
.m_width
);
1044 if ( m_mask
& wxLIST_MASK_STATE
)
1045 SetState(item
.m_state
);
1048 void wxListHeaderData::SetPosition( int x
, int y
)
1054 void wxListHeaderData::SetHeight( int h
)
1059 void wxListHeaderData::SetWidth( int w
)
1061 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1064 void wxListHeaderData::SetState( int flag
)
1069 void wxListHeaderData::SetFormat( int format
)
1074 bool wxListHeaderData::HasImage() const
1076 return m_image
!= -1;
1079 bool wxListHeaderData::IsHit( int x
, int y
) const
1081 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1084 void wxListHeaderData::GetItem( wxListItem
& item
)
1086 item
.m_mask
= m_mask
;
1087 item
.m_text
= m_text
;
1088 item
.m_image
= m_image
;
1089 item
.m_format
= m_format
;
1090 item
.m_width
= m_width
;
1091 item
.m_state
= m_state
;
1094 int wxListHeaderData::GetImage() const
1099 int wxListHeaderData::GetWidth() const
1104 int wxListHeaderData::GetFormat() const
1109 int wxListHeaderData::GetState() const
1114 //-----------------------------------------------------------------------------
1116 //-----------------------------------------------------------------------------
1118 inline int wxListLineData::GetMode() const
1120 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1123 inline bool wxListLineData::InReportView() const
1125 return m_owner
->HasFlag(wxLC_REPORT
);
1128 inline bool wxListLineData::IsVirtual() const
1130 return m_owner
->IsVirtual();
1133 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1137 if ( InReportView() )
1140 m_gi
= new GeometryInfo
;
1142 m_highlighted
= false;
1144 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1147 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1149 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1150 wxCHECK_RET( node
, _T("no subitems at all??") );
1152 wxListItemData
*item
= node
->GetData();
1157 switch ( GetMode() )
1160 case wxLC_SMALL_ICON
:
1161 m_gi
->m_rectAll
.width
= spacing
;
1163 s
= item
->GetText();
1168 m_gi
->m_rectLabel
.width
=
1169 m_gi
->m_rectLabel
.height
= 0;
1173 dc
->GetTextExtent( s
, &lw
, &lh
);
1177 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1179 m_gi
->m_rectAll
.width
= lw
;
1181 m_gi
->m_rectLabel
.width
= lw
;
1182 m_gi
->m_rectLabel
.height
= lh
;
1185 if (item
->HasImage())
1188 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1189 m_gi
->m_rectIcon
.width
= w
+ 8;
1190 m_gi
->m_rectIcon
.height
= h
+ 8;
1192 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1193 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1194 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1195 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1198 if ( item
->HasText() )
1200 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1201 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1203 else // no text, highlight the icon
1205 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1206 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1211 s
= item
->GetTextForMeasuring();
1213 dc
->GetTextExtent( s
, &lw
, &lh
);
1217 m_gi
->m_rectLabel
.width
= lw
;
1218 m_gi
->m_rectLabel
.height
= lh
;
1220 m_gi
->m_rectAll
.width
= lw
;
1221 m_gi
->m_rectAll
.height
= lh
;
1223 if (item
->HasImage())
1226 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1227 m_gi
->m_rectIcon
.width
= w
;
1228 m_gi
->m_rectIcon
.height
= h
;
1230 m_gi
->m_rectAll
.width
+= 4 + w
;
1231 if (h
> m_gi
->m_rectAll
.height
)
1232 m_gi
->m_rectAll
.height
= h
;
1235 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1236 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1240 wxFAIL_MSG( _T("unexpected call to SetSize") );
1244 wxFAIL_MSG( _T("unknown mode") );
1249 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1251 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1252 wxCHECK_RET( node
, _T("no subitems at all??") );
1254 wxListItemData
*item
= node
->GetData();
1256 switch ( GetMode() )
1259 case wxLC_SMALL_ICON
:
1260 m_gi
->m_rectAll
.x
= x
;
1261 m_gi
->m_rectAll
.y
= y
;
1263 if ( item
->HasImage() )
1265 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1266 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1267 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1270 if ( item
->HasText() )
1272 if (m_gi
->m_rectAll
.width
> spacing
)
1273 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1275 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1276 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1277 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1278 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1280 else // no text, highlight the icon
1282 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1283 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1288 m_gi
->m_rectAll
.x
= x
;
1289 m_gi
->m_rectAll
.y
= y
;
1291 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1292 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1293 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1295 if (item
->HasImage())
1297 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1298 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1299 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1303 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1308 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1312 wxFAIL_MSG( _T("unknown mode") );
1317 void wxListLineData::InitItems( int num
)
1319 for (int i
= 0; i
< num
; i
++)
1320 m_items
.Append( new wxListItemData(m_owner
) );
1323 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1325 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1326 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1328 wxListItemData
*item
= node
->GetData();
1329 item
->SetItem( info
);
1332 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1334 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1337 wxListItemData
*item
= node
->GetData();
1338 item
->GetItem( info
);
1342 wxString
wxListLineData::GetText(int index
) const
1346 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1349 wxListItemData
*item
= node
->GetData();
1350 s
= item
->GetText();
1356 void wxListLineData::SetText( int index
, const wxString
& s
)
1358 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1361 wxListItemData
*item
= node
->GetData();
1366 void wxListLineData::SetImage( int index
, int image
)
1368 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1369 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1371 wxListItemData
*item
= node
->GetData();
1372 item
->SetImage(image
);
1375 int wxListLineData::GetImage( int index
) const
1377 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1378 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1380 wxListItemData
*item
= node
->GetData();
1381 return item
->GetImage();
1384 wxListItemAttr
*wxListLineData::GetAttr() const
1386 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1387 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1389 wxListItemData
*item
= node
->GetData();
1390 return item
->GetAttr();
1393 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1395 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1396 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1398 wxListItemData
*item
= node
->GetData();
1399 item
->SetAttr(attr
);
1402 bool wxListLineData::SetAttributes(wxDC
*dc
,
1403 const wxListItemAttr
*attr
,
1406 wxWindow
*listctrl
= m_owner
->GetParent();
1410 // don't use foreground colour for drawing highlighted items - this might
1411 // make them completely invisible (and there is no way to do bit
1412 // arithmetics on wxColour, unfortunately)
1417 if (m_owner
->HasFocus()
1419 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1427 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1429 else if ( attr
&& attr
->HasTextColour() )
1430 colText
= attr
->GetTextColour();
1432 colText
= listctrl
->GetForegroundColour();
1434 dc
->SetTextForeground(colText
);
1438 if ( attr
&& attr
->HasFont() )
1439 font
= attr
->GetFont();
1441 font
= listctrl
->GetFont();
1446 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1447 if ( highlighted
|| hasBgCol
)
1450 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1452 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1454 dc
->SetPen( *wxTRANSPARENT_PEN
);
1462 void wxListLineData::Draw( wxDC
*dc
)
1464 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1465 wxCHECK_RET( node
, _T("no subitems at all??") );
1467 bool highlighted
= IsHighlighted();
1469 wxListItemAttr
*attr
= GetAttr();
1471 if ( SetAttributes(dc
, attr
, highlighted
) )
1472 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1474 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1480 int flags
= wxCONTROL_SELECTED
;
1481 if (m_owner
->HasFocus()
1483 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1486 flags
|= wxCONTROL_FOCUSED
;
1487 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1492 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1497 // just for debugging to better see where the items are
1499 dc
->SetPen(*wxRED_PEN
);
1500 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1501 dc
->DrawRectangle( m_gi
->m_rectAll
);
1502 dc
->SetPen(*wxGREEN_PEN
);
1503 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1506 wxListItemData
*item
= node
->GetData();
1507 if (item
->HasImage())
1509 // centre the image inside our rectangle, this looks nicer when items
1510 // ae aligned in a row
1511 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1513 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1516 if (item
->HasText())
1518 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1520 wxDCClipper
clipper(*dc
, rectLabel
);
1521 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1525 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1527 const wxRect
& rectHL
,
1530 // TODO: later we should support setting different attributes for
1531 // different columns - to do it, just add "col" argument to
1532 // GetAttr() and move these lines into the loop below
1533 wxListItemAttr
*attr
= GetAttr();
1534 if ( SetAttributes(dc
, attr
, highlighted
) )
1535 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1537 dc
->DrawRectangle( rectHL
);
1543 int flags
= wxCONTROL_SELECTED
;
1544 if (m_owner
->HasFocus()
1546 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1549 flags
|= wxCONTROL_FOCUSED
;
1550 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1554 dc
->DrawRectangle( rectHL
);
1559 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1560 yMid
= rect
.y
+ rect
.height
/2;
1562 // This probably needs to be done
1563 // on all platforms as the icons
1564 // otherwise nearly touch the border
1569 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1571 node
= node
->GetNext(), col
++ )
1573 wxListItemData
*item
= node
->GetData();
1575 int width
= m_owner
->GetColumnWidth(col
);
1579 if ( item
->HasImage() )
1582 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1583 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1585 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1591 if ( item
->HasText() )
1592 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, width
- 8);
1596 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1597 const wxString
& textOrig
,
1603 // we don't support displaying multiple lines currently (and neither does
1604 // wxMSW FWIW) so just merge all the lines
1605 wxString
text(textOrig
);
1606 text
.Replace(_T("\n"), _T(" "));
1609 dc
->GetTextExtent(text
, &w
, &h
);
1611 const wxCoord y
= yMid
- (h
+ 1)/2;
1613 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1615 // determine if the string can fit inside the current width
1618 // it can, draw it using the items alignment
1620 m_owner
->GetColumn(col
, item
);
1621 switch ( item
.GetAlign() )
1623 case wxLIST_FORMAT_LEFT
:
1627 case wxLIST_FORMAT_RIGHT
:
1631 case wxLIST_FORMAT_CENTER
:
1632 x
+= (width
- w
) / 2;
1636 wxFAIL_MSG( _T("unknown list item format") );
1640 dc
->DrawText(text
, x
, y
);
1642 else // otherwise, truncate and add an ellipsis if possible
1644 // determine the base width
1645 wxString
ellipsis(wxT("..."));
1647 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1649 // continue until we have enough space or only one character left
1651 size_t len
= text
.length();
1652 wxString drawntext
= text
.Left(len
);
1655 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1656 drawntext
.RemoveLast();
1659 if (w
+ base_w
<= width
)
1663 // if still not enough space, remove ellipsis characters
1664 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1666 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1667 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1670 // now draw the text
1671 dc
->DrawText(drawntext
, x
, y
);
1672 dc
->DrawText(ellipsis
, x
+ w
, y
);
1676 bool wxListLineData::Highlight( bool on
)
1678 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1680 if ( on
== m_highlighted
)
1688 void wxListLineData::ReverseHighlight( void )
1690 Highlight(!IsHighlighted());
1693 //-----------------------------------------------------------------------------
1694 // wxListHeaderWindow
1695 //-----------------------------------------------------------------------------
1697 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1699 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1700 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1701 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1702 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1705 void wxListHeaderWindow::Init()
1707 m_currentCursor
= (wxCursor
*) NULL
;
1708 m_isDragging
= false;
1712 wxListHeaderWindow::wxListHeaderWindow()
1716 m_owner
= (wxListMainWindow
*) NULL
;
1717 m_resizeCursor
= (wxCursor
*) NULL
;
1720 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1722 wxListMainWindow
*owner
,
1726 const wxString
&name
)
1727 : wxWindow( win
, id
, pos
, size
, style
, name
)
1732 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1735 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1736 SetOwnForegroundColour( attr
.colFg
);
1737 SetOwnBackgroundColour( attr
.colBg
);
1739 SetOwnFont( attr
.font
);
1741 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1742 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1744 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1748 wxListHeaderWindow::~wxListHeaderWindow()
1750 delete m_resizeCursor
;
1753 #ifdef __WXUNIVERSAL__
1754 #include "wx/univ/renderer.h"
1755 #include "wx/univ/theme.h"
1758 // shift the DC origin to match the position of the main window horz
1759 // scrollbar: this allows us to always use logical coords
1760 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1763 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1766 m_owner
->GetViewStart( &view_start
, NULL
);
1771 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1773 // account for the horz scrollbar offset
1775 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1777 // Maybe we just have to check for m_signX
1778 // in the DC, but I leave the #ifdef __WXGTK__
1780 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1784 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1787 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1789 wxPaintDC
dc( this );
1794 dc
.SetFont( GetFont() );
1796 // width and height of the entire header window
1798 GetClientSize( &w
, &h
);
1799 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1801 dc
.SetBackgroundMode(wxTRANSPARENT
);
1802 dc
.SetTextForeground(GetForegroundColour());
1804 int x
= HEADER_OFFSET_X
;
1805 int numColumns
= m_owner
->GetColumnCount();
1807 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1809 m_owner
->GetColumn( i
, item
);
1810 int wCol
= item
.m_width
;
1816 if (!m_parent
->IsEnabled())
1817 flags
|= wxCONTROL_DISABLED
;
1819 // NB: The code below is not really Mac-specific, but since we are close
1820 // to 2.8 release and I don't have time to test on other platforms, I
1821 // defined this only for wxMac. If this behavior is desired on
1822 // other platforms, please go ahead and revise or remove the #ifdef.
1824 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1825 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1826 flags
|= wxCONTROL_SELECTED
;
1829 wxRendererNative::Get().DrawHeaderButton
1833 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1837 // see if we have enough space for the column label
1839 // for this we need the width of the text
1842 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1843 wLabel
+= 2 * EXTRA_WIDTH
;
1845 // and the width of the icon, if any
1846 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1847 const int image
= item
.m_image
;
1848 wxImageList
*imageList
;
1851 imageList
= m_owner
->m_small_image_list
;
1854 imageList
->GetSize(image
, ix
, iy
);
1855 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1863 // ignore alignment if there is not enough space anyhow
1865 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1868 wxFAIL_MSG( _T("unknown list item format") );
1871 case wxLIST_FORMAT_LEFT
:
1875 case wxLIST_FORMAT_RIGHT
:
1876 xAligned
= x
+ cw
- wLabel
;
1879 case wxLIST_FORMAT_CENTER
:
1880 xAligned
= x
+ (cw
- wLabel
) / 2;
1884 // draw the text and image clipping them so that they
1885 // don't overwrite the column boundary
1886 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1888 // if we have an image, draw it on the right of the label
1895 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1896 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1897 wxIMAGELIST_DRAW_TRANSPARENT
1901 dc
.DrawText( item
.GetText(),
1902 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1908 void wxListHeaderWindow::DrawCurrent()
1911 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1913 int x1
= m_currentX
;
1915 m_owner
->ClientToScreen( &x1
, &y1
);
1917 int x2
= m_currentX
;
1919 m_owner
->GetClientSize( NULL
, &y2
);
1920 m_owner
->ClientToScreen( &x2
, &y2
);
1923 dc
.SetLogicalFunction( wxINVERT
);
1924 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1925 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1929 dc
.DrawLine( x1
, y1
, x2
, y2
);
1931 dc
.SetLogicalFunction( wxCOPY
);
1933 dc
.SetPen( wxNullPen
);
1934 dc
.SetBrush( wxNullBrush
);
1938 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1940 // we want to work with logical coords
1942 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1943 int y
= event
.GetY();
1947 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1949 // we don't draw the line beyond our window, but we allow dragging it
1952 GetClientSize( &w
, NULL
);
1953 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1956 // erase the line if it was drawn
1957 if ( m_currentX
< w
)
1960 if (event
.ButtonUp())
1963 m_isDragging
= false;
1965 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1966 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1973 m_currentX
= m_minX
+ 7;
1975 // draw in the new location
1976 if ( m_currentX
< w
)
1980 else // not dragging
1983 bool hit_border
= false;
1985 // end of the current column
1988 // find the column where this event occurred
1990 countCol
= m_owner
->GetColumnCount();
1991 for (col
= 0; col
< countCol
; col
++)
1993 xpos
+= m_owner
->GetColumnWidth( col
);
1996 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1998 // near the column border
2005 // inside the column
2012 if ( col
== countCol
)
2015 if (event
.LeftDown() || event
.RightUp())
2017 if (hit_border
&& event
.LeftDown())
2019 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2020 event
.GetPosition()) )
2022 m_isDragging
= true;
2027 //else: column resizing was vetoed by the user code
2029 else // click on a column
2031 // record the selected state of the columns
2032 if (event
.LeftDown())
2034 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2037 m_owner
->GetColumn(i
, colItem
);
2038 long state
= colItem
.GetState();
2040 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2042 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2043 m_owner
->SetColumn(i
, colItem
);
2047 SendListEvent( event
.LeftDown()
2048 ? wxEVT_COMMAND_LIST_COL_CLICK
2049 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2050 event
.GetPosition());
2053 else if (event
.Moving())
2058 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2059 m_currentCursor
= m_resizeCursor
;
2063 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2064 m_currentCursor
= wxSTANDARD_CURSOR
;
2068 SetCursor(*m_currentCursor
);
2073 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2075 m_owner
->SetFocus();
2079 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2081 wxWindow
*parent
= GetParent();
2082 wxListEvent
le( type
, parent
->GetId() );
2083 le
.SetEventObject( parent
);
2084 le
.m_pointDrag
= pos
;
2086 // the position should be relative to the parent window, not
2087 // this one for compatibility with MSW and common sense: the
2088 // user code doesn't know anything at all about this header
2089 // window, so why should it get positions relative to it?
2090 le
.m_pointDrag
.y
-= GetSize().y
;
2092 le
.m_col
= m_column
;
2093 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2096 //-----------------------------------------------------------------------------
2097 // wxListRenameTimer (internal)
2098 //-----------------------------------------------------------------------------
2100 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2105 void wxListRenameTimer::Notify()
2107 m_owner
->OnRenameTimer();
2110 //-----------------------------------------------------------------------------
2111 // wxListTextCtrlWrapper (internal)
2112 //-----------------------------------------------------------------------------
2114 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2115 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2116 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2117 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2120 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2123 : m_startValue(owner
->GetItemText(itemEdit
)),
2124 m_itemEdited(itemEdit
)
2129 m_aboutToFinish
= false;
2131 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2133 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2134 &rectLabel
.x
, &rectLabel
.y
);
2136 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2137 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2138 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2141 m_text
->PushEventHandler(this);
2144 void wxListTextCtrlWrapper::Finish()
2150 m_text
->RemoveEventHandler(this);
2151 m_owner
->FinishEditing(m_text
);
2153 wxPendingDelete
.Append( this );
2157 bool wxListTextCtrlWrapper::AcceptChanges()
2159 const wxString value
= m_text
->GetValue();
2161 // notice that we should always call OnRenameAccept() to generate the "end
2162 // label editing" event, even if the user hasn't really changed anything
2163 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2165 // vetoed by the user
2169 // accepted, do rename the item (unless nothing changed)
2170 if ( value
!= m_startValue
)
2171 m_owner
->SetItemText(m_itemEdited
, value
);
2176 void wxListTextCtrlWrapper::AcceptChangesAndFinish()
2178 m_aboutToFinish
= true;
2180 // Notify the owner about the changes
2183 // Even if vetoed, close the control (consistent with MSW)
2187 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2189 switch ( event
.m_keyCode
)
2192 AcceptChangesAndFinish();
2196 m_owner
->OnRenameCancelled( m_itemEdited
);
2205 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2213 // auto-grow the textctrl:
2214 wxSize parentSize
= m_owner
->GetSize();
2215 wxPoint myPos
= m_text
->GetPosition();
2216 wxSize mySize
= m_text
->GetSize();
2218 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2219 if (myPos
.x
+ sx
> parentSize
.x
)
2220 sx
= parentSize
.x
- myPos
.x
;
2223 m_text
->SetSize(sx
, wxDefaultCoord
);
2228 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2230 if ( !m_finished
&& !m_aboutToFinish
)
2232 if ( !AcceptChanges() )
2233 m_owner
->OnRenameCancelled( m_itemEdited
);
2238 // We must let the native text control handle focus
2242 //-----------------------------------------------------------------------------
2244 //-----------------------------------------------------------------------------
2246 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2248 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2249 EVT_PAINT (wxListMainWindow::OnPaint
)
2250 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2251 EVT_CHAR (wxListMainWindow::OnChar
)
2252 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2253 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
2254 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2255 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2256 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2259 void wxListMainWindow::Init()
2264 m_lineTo
= (size_t)-1;
2270 m_small_image_list
= (wxImageList
*) NULL
;
2271 m_normal_image_list
= (wxImageList
*) NULL
;
2273 m_small_spacing
= 30;
2274 m_normal_spacing
= 40;
2278 m_isCreated
= false;
2280 m_lastOnSame
= false;
2281 m_renameTimer
= new wxListRenameTimer( this );
2282 m_textctrlWrapper
= NULL
;
2286 m_lineSelectSingleOnUp
=
2287 m_lineBeforeLastClicked
= (size_t)-1;
2292 wxListMainWindow::wxListMainWindow()
2297 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2300 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2305 const wxString
&name
)
2306 : wxScrolledWindow( parent
, id
, pos
, size
,
2307 style
| wxHSCROLL
| wxVSCROLL
, name
)
2311 m_highlightBrush
= new wxBrush
2313 wxSystemSettings::GetColour
2315 wxSYS_COLOUR_HIGHLIGHT
2320 m_highlightUnfocusedBrush
= new wxBrush
2322 wxSystemSettings::GetColour
2324 wxSYS_COLOUR_BTNSHADOW
2329 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2331 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2332 SetOwnForegroundColour( attr
.colFg
);
2333 SetOwnBackgroundColour( attr
.colBg
);
2335 SetOwnFont( attr
.font
);
2338 wxListMainWindow::~wxListMainWindow()
2341 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2342 WX_CLEAR_ARRAY(m_aColWidths
);
2344 delete m_highlightBrush
;
2345 delete m_highlightUnfocusedBrush
;
2346 delete m_renameTimer
;
2349 void wxListMainWindow::CacheLineData(size_t line
)
2351 wxGenericListCtrl
*listctrl
= GetListCtrl();
2353 wxListLineData
*ld
= GetDummyLine();
2355 size_t countCol
= GetColumnCount();
2356 for ( size_t col
= 0; col
< countCol
; col
++ )
2358 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2359 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2362 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2365 wxListLineData
*wxListMainWindow::GetDummyLine() const
2367 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2368 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2370 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2372 // we need to recreate the dummy line if the number of columns in the
2373 // control changed as it would have the incorrect number of fields
2375 if ( !m_lines
.IsEmpty() &&
2376 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2378 self
->m_lines
.Clear();
2381 if ( m_lines
.IsEmpty() )
2383 wxListLineData
*line
= new wxListLineData(self
);
2384 self
->m_lines
.Add(line
);
2386 // don't waste extra memory -- there never going to be anything
2387 // else/more in this array
2388 self
->m_lines
.Shrink();
2394 // ----------------------------------------------------------------------------
2395 // line geometry (report mode only)
2396 // ----------------------------------------------------------------------------
2398 wxCoord
wxListMainWindow::GetLineHeight() const
2400 // we cache the line height as calling GetTextExtent() is slow
2401 if ( !m_lineHeight
)
2403 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2405 wxClientDC
dc( self
);
2406 dc
.SetFont( GetFont() );
2409 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2411 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2414 m_small_image_list
->GetSize(0, iw
, ih
);
2419 self
->m_lineHeight
= y
+ LINE_SPACING
;
2422 return m_lineHeight
;
2425 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2427 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2429 return LINE_SPACING
+ line
* GetLineHeight();
2432 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2434 if ( !InReportView() )
2435 return GetLine(line
)->m_gi
->m_rectAll
;
2438 rect
.x
= HEADER_OFFSET_X
;
2439 rect
.y
= GetLineY(line
);
2440 rect
.width
= GetHeaderWidth();
2441 rect
.height
= GetLineHeight();
2446 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2448 if ( !InReportView() )
2449 return GetLine(line
)->m_gi
->m_rectLabel
;
2452 wxListLineData
*data
= GetLine(line
);
2453 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
2456 wxListItemData
*item
= node
->GetData();
2457 if ( item
->HasImage() )
2460 GetImageSize( item
->GetImage(), ix
, iy
);
2461 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
2466 rect
.x
= image_x
+ HEADER_OFFSET_X
;
2467 rect
.y
= GetLineY(line
);
2468 rect
.width
= GetColumnWidth(0) - image_x
;
2469 rect
.height
= GetLineHeight();
2474 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2476 if ( !InReportView() )
2477 return GetLine(line
)->m_gi
->m_rectIcon
;
2479 wxListLineData
*ld
= GetLine(line
);
2480 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2483 rect
.x
= HEADER_OFFSET_X
;
2484 rect
.y
= GetLineY(line
);
2485 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2490 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2492 return InReportView() ? GetLineRect(line
)
2493 : GetLine(line
)->m_gi
->m_rectHighlight
;
2496 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2498 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2500 wxListLineData
*ld
= GetLine(line
);
2502 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2503 return wxLIST_HITTEST_ONITEMICON
;
2505 // VS: Testing for "ld->HasText() || InReportView()" instead of
2506 // "ld->HasText()" is needed to make empty lines in report view
2508 if ( ld
->HasText() || InReportView() )
2510 wxRect rect
= InReportView() ? GetLineRect(line
)
2511 : GetLineLabelRect(line
);
2513 if ( rect
.Contains(x
, y
) )
2514 return wxLIST_HITTEST_ONITEMLABEL
;
2520 // ----------------------------------------------------------------------------
2521 // highlight (selection) handling
2522 // ----------------------------------------------------------------------------
2524 bool wxListMainWindow::IsHighlighted(size_t line
) const
2528 return m_selStore
.IsSelected(line
);
2532 wxListLineData
*ld
= GetLine(line
);
2533 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2535 return ld
->IsHighlighted();
2539 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2545 wxArrayInt linesChanged
;
2546 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2549 // meny items changed state, refresh everything
2550 RefreshLines(lineFrom
, lineTo
);
2552 else // only a few items changed state, refresh only them
2554 size_t count
= linesChanged
.GetCount();
2555 for ( size_t n
= 0; n
< count
; n
++ )
2557 RefreshLine(linesChanged
[n
]);
2561 else // iterate over all items in non report view
2563 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2565 if ( HighlightLine(line
, highlight
) )
2571 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2577 changed
= m_selStore
.SelectItem(line
, highlight
);
2581 wxListLineData
*ld
= GetLine(line
);
2582 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2584 changed
= ld
->Highlight(highlight
);
2589 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2590 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2596 void wxListMainWindow::RefreshLine( size_t line
)
2598 if ( InReportView() )
2600 size_t visibleFrom
, visibleTo
;
2601 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2603 if ( line
< visibleFrom
|| line
> visibleTo
)
2607 wxRect rect
= GetLineRect(line
);
2609 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2610 RefreshRect( rect
);
2613 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2615 // we suppose that they are ordered by caller
2616 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2618 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2620 if ( InReportView() )
2622 size_t visibleFrom
, visibleTo
;
2623 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2625 if ( lineFrom
< visibleFrom
)
2626 lineFrom
= visibleFrom
;
2627 if ( lineTo
> visibleTo
)
2632 rect
.y
= GetLineY(lineFrom
);
2633 rect
.width
= GetClientSize().x
;
2634 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2636 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2637 RefreshRect( rect
);
2641 // TODO: this should be optimized...
2642 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2649 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2651 if ( InReportView() )
2653 size_t visibleFrom
, visibleTo
;
2654 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2656 if ( lineFrom
< visibleFrom
)
2657 lineFrom
= visibleFrom
;
2658 else if ( lineFrom
> visibleTo
)
2663 rect
.y
= GetLineY(lineFrom
);
2664 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2666 wxSize size
= GetClientSize();
2667 rect
.width
= size
.x
;
2669 // refresh till the bottom of the window
2670 rect
.height
= size
.y
- rect
.y
;
2672 RefreshRect( rect
);
2676 // TODO: how to do it more efficiently?
2681 void wxListMainWindow::RefreshSelected()
2687 if ( InReportView() )
2689 GetVisibleLinesRange(&from
, &to
);
2694 to
= GetItemCount() - 1;
2697 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2698 RefreshLine(m_current
);
2700 for ( size_t line
= from
; line
<= to
; line
++ )
2702 // NB: the test works as expected even if m_current == -1
2703 if ( line
!= m_current
&& IsHighlighted(line
) )
2708 void wxListMainWindow::Freeze()
2713 void wxListMainWindow::Thaw()
2715 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2717 if ( --m_freezeCount
== 0 )
2721 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2723 // Note: a wxPaintDC must be constructed even if no drawing is
2724 // done (a Windows requirement).
2725 wxPaintDC
dc( this );
2727 if ( IsEmpty() || m_freezeCount
)
2728 // nothing to draw or not the moment to draw it
2732 // delay the repainting until we calculate all the items positions
2738 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2740 dc
.SetFont( GetFont() );
2742 if ( InReportView() )
2744 int lineHeight
= GetLineHeight();
2746 size_t visibleFrom
, visibleTo
;
2747 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2750 int xOrig
= dc
.LogicalToDeviceX( 0 );
2751 int yOrig
= dc
.LogicalToDeviceY( 0 );
2753 // tell the caller cache to cache the data
2756 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2757 GetParent()->GetId());
2758 evCache
.SetEventObject( GetParent() );
2759 evCache
.m_oldItemIndex
= visibleFrom
;
2760 evCache
.m_itemIndex
= visibleTo
;
2761 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2764 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2766 rectLine
= GetLineRect(line
);
2769 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2770 rectLine
.width
, rectLine
.height
) )
2772 // don't redraw unaffected lines to avoid flicker
2776 GetLine(line
)->DrawInReportMode( &dc
,
2778 GetLineHighlightRect(line
),
2779 IsHighlighted(line
) );
2782 if ( HasFlag(wxLC_HRULES
) )
2784 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2785 wxSize clientSize
= GetClientSize();
2787 size_t i
= visibleFrom
;
2788 if (i
== 0) i
= 1; // Don't draw the first one
2789 for ( ; i
<= visibleTo
; i
++ )
2792 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2793 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2794 clientSize
.x
- dev_x
, i
* lineHeight
);
2797 // Draw last horizontal rule
2798 if ( visibleTo
== GetItemCount() - 1 )
2801 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2802 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2803 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2807 // Draw vertical rules if required
2808 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2810 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2811 wxRect firstItemRect
, lastItemRect
;
2813 GetItemRect(visibleFrom
, firstItemRect
);
2814 GetItemRect(visibleTo
, lastItemRect
);
2815 int x
= firstItemRect
.GetX();
2817 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2819 for (int col
= 0; col
< GetColumnCount(); col
++)
2821 int colWidth
= GetColumnWidth(col
);
2823 int x_pos
= x
- dev_x
;
2824 if (col
< GetColumnCount()-1) x_pos
-= 2;
2825 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2826 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2832 size_t count
= GetItemCount();
2833 for ( size_t i
= 0; i
< count
; i
++ )
2835 GetLine(i
)->Draw( &dc
);
2840 // Don't draw rect outline under Mac at all.
2845 wxRect
rect( GetLineHighlightRect( m_current
) );
2847 dc
.SetPen( *wxBLACK_PEN
);
2848 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2849 dc
.DrawRectangle( rect
);
2851 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2859 void wxListMainWindow::HighlightAll( bool on
)
2861 if ( IsSingleSel() )
2863 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2865 // we just have one item to turn off
2866 if ( HasCurrent() && IsHighlighted(m_current
) )
2868 HighlightLine(m_current
, false);
2869 RefreshLine(m_current
);
2874 HighlightLines(0, GetItemCount() - 1, on
);
2878 void wxListMainWindow::SendNotify( size_t line
,
2879 wxEventType command
,
2880 const wxPoint
& point
)
2882 wxListEvent
le( command
, GetParent()->GetId() );
2883 le
.SetEventObject( GetParent() );
2885 le
.m_itemIndex
= line
;
2887 // set only for events which have position
2888 if ( point
!= wxDefaultPosition
)
2889 le
.m_pointDrag
= point
;
2891 // don't try to get the line info for virtual list controls: the main
2892 // program has it anyhow and if we did it would result in accessing all
2893 // the lines, even those which are not visible now and this is precisely
2894 // what we're trying to avoid
2897 if ( line
!= (size_t)-1 )
2899 GetLine(line
)->GetItem( 0, le
.m_item
);
2901 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2903 //else: there may be no more such item
2905 GetParent()->GetEventHandler()->ProcessEvent( le
);
2908 void wxListMainWindow::ChangeCurrent(size_t current
)
2910 m_current
= current
;
2912 // as the current item changed, we shouldn't start editing it when the
2913 // "slow click" timer expires as the click happened on another item
2914 if ( m_renameTimer
->IsRunning() )
2915 m_renameTimer
->Stop();
2917 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2920 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2922 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2923 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2925 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2926 wxT("EditLabel() needs a text control") );
2928 size_t itemEdit
= (size_t)item
;
2930 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2931 le
.SetEventObject( GetParent() );
2932 le
.m_itemIndex
= item
;
2933 wxListLineData
*data
= GetLine(itemEdit
);
2934 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2935 data
->GetItem( 0, le
.m_item
);
2937 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2939 // vetoed by user code
2943 // We have to call this here because the label in question might just have
2944 // been added and no screen update taken place.
2949 // Pending events dispatched by wxSafeYield might have changed the item
2951 if ( (size_t)item
>= GetItemCount() )
2955 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2956 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2957 return m_textctrlWrapper
->GetText();
2960 void wxListMainWindow::OnRenameTimer()
2962 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2964 EditLabel( m_current
);
2967 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2969 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2970 le
.SetEventObject( GetParent() );
2971 le
.m_itemIndex
= itemEdit
;
2973 wxListLineData
*data
= GetLine(itemEdit
);
2975 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2977 data
->GetItem( 0, le
.m_item
);
2978 le
.m_item
.m_text
= value
;
2979 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2983 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2985 // let owner know that the edit was cancelled
2986 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2988 le
.SetEditCanceled(true);
2990 le
.SetEventObject( GetParent() );
2991 le
.m_itemIndex
= itemEdit
;
2993 wxListLineData
*data
= GetLine(itemEdit
);
2994 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2996 data
->GetItem( 0, le
.m_item
);
2997 GetEventHandler()->ProcessEvent( le
);
3000 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
3004 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
3005 // shutdown the edit control when the mouse is clicked elsewhere on the
3006 // listctrl because the order of events is different (or something like
3007 // that), so explicitly end the edit if it is active.
3008 if ( event
.LeftDown() && m_textctrlWrapper
)
3009 m_textctrlWrapper
->AcceptChangesAndFinish();
3012 if ( event
.LeftDown() )
3013 SetFocusIgnoringChildren();
3015 event
.SetEventObject( GetParent() );
3016 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3019 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3021 // let the base handle mouse wheel events.
3026 if ( !HasCurrent() || IsEmpty() )
3028 if (event
.RightDown())
3030 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3031 // Allow generation of context menu event
3040 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3041 event
.ButtonDClick()) )
3044 int x
= event
.GetX();
3045 int y
= event
.GetY();
3046 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3048 // where did we hit it (if we did)?
3051 size_t count
= GetItemCount(),
3054 if ( InReportView() )
3056 current
= y
/ GetLineHeight();
3057 if ( current
< count
)
3058 hitResult
= HitTestLine(current
, x
, y
);
3062 // TODO: optimize it too! this is less simple than for report view but
3063 // enumerating all items is still not a way to do it!!
3064 for ( current
= 0; current
< count
; current
++ )
3066 hitResult
= HitTestLine(current
, x
, y
);
3072 if (event
.Dragging())
3074 if (m_dragCount
== 0)
3076 // we have to report the raw, physical coords as we want to be
3077 // able to call HitTest(event.m_pointDrag) from the user code to
3078 // get the item being dragged
3079 m_dragStart
= event
.GetPosition();
3084 if (m_dragCount
!= 3)
3087 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3088 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3090 wxListEvent
le( command
, GetParent()->GetId() );
3091 le
.SetEventObject( GetParent() );
3092 le
.m_itemIndex
= m_lineLastClicked
;
3093 le
.m_pointDrag
= m_dragStart
;
3094 GetParent()->GetEventHandler()->ProcessEvent( le
);
3105 // outside of any item
3106 if (event
.RightDown())
3108 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3109 // Allow generation of context menu event
3114 // reset the selection and bail out
3115 HighlightAll(false);
3121 bool forceClick
= false;
3122 if (event
.ButtonDClick())
3124 if ( m_renameTimer
->IsRunning() )
3125 m_renameTimer
->Stop();
3127 m_lastOnSame
= false;
3129 if ( current
== m_lineLastClicked
)
3131 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3137 // The first click was on another item, so don't interpret this as
3138 // a double click, but as a simple click instead
3145 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3147 // select single line
3148 HighlightAll( false );
3149 ReverseHighlight(m_lineSelectSingleOnUp
);
3154 if ((current
== m_current
) &&
3155 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3156 HasFlag(wxLC_EDIT_LABELS
) )
3158 if ( !InReportView() ||
3159 GetLineLabelRect(current
).Contains(x
, y
) )
3161 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
3162 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
3167 m_lastOnSame
= false;
3168 m_lineSelectSingleOnUp
= (size_t)-1;
3172 // This is necessary, because after a DnD operation in
3173 // from and to ourself, the up event is swallowed by the
3174 // DnD code. So on next non-up event (which means here and
3175 // now) m_lineSelectSingleOnUp should be reset.
3176 m_lineSelectSingleOnUp
= (size_t)-1;
3178 if (event
.RightDown())
3180 m_lineBeforeLastClicked
= m_lineLastClicked
;
3181 m_lineLastClicked
= current
;
3183 // If the item is already selected, do not update the selection.
3184 // Multi-selections should not be cleared if a selected item is clicked.
3185 if (!IsHighlighted(current
))
3187 HighlightAll(false);
3188 ChangeCurrent(current
);
3189 ReverseHighlight(m_current
);
3192 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3194 // Allow generation of context menu event
3197 else if (event
.MiddleDown())
3199 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3201 else if ( event
.LeftDown() || forceClick
)
3203 m_lineBeforeLastClicked
= m_lineLastClicked
;
3204 m_lineLastClicked
= current
;
3206 size_t oldCurrent
= m_current
;
3207 bool oldWasSelected
= IsHighlighted(m_current
);
3209 bool cmdModifierDown
= event
.CmdDown();
3210 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3212 if ( IsSingleSel() || !IsHighlighted(current
) )
3214 HighlightAll( false );
3216 ChangeCurrent(current
);
3218 ReverseHighlight(m_current
);
3220 else // multi sel & current is highlighted & no mod keys
3222 m_lineSelectSingleOnUp
= current
;
3223 ChangeCurrent(current
); // change focus
3226 else // multi sel & either ctrl or shift is down
3228 if (cmdModifierDown
)
3230 ChangeCurrent(current
);
3232 ReverseHighlight(m_current
);
3234 else if (event
.ShiftDown())
3236 ChangeCurrent(current
);
3238 size_t lineFrom
= oldCurrent
,
3241 if ( lineTo
< lineFrom
)
3244 lineFrom
= m_current
;
3247 HighlightLines(lineFrom
, lineTo
);
3249 else // !ctrl, !shift
3251 // test in the enclosing if should make it impossible
3252 wxFAIL_MSG( _T("how did we get here?") );
3256 if (m_current
!= oldCurrent
)
3257 RefreshLine( oldCurrent
);
3259 // forceClick is only set if the previous click was on another item
3260 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3264 void wxListMainWindow::MoveToItem(size_t item
)
3266 if ( item
== (size_t)-1 )
3269 wxRect rect
= GetLineRect(item
);
3271 int client_w
, client_h
;
3272 GetClientSize( &client_w
, &client_h
);
3274 const int hLine
= GetLineHeight();
3276 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3277 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3279 if ( InReportView() )
3281 // the next we need the range of lines shown it might be different,
3282 // so recalculate it
3283 ResetVisibleLinesRange();
3285 if (rect
.y
< view_y
)
3286 Scroll( -1, rect
.y
/ hLine
);
3287 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3288 Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3292 if (rect
.x
-view_x
< 5)
3293 Scroll( (rect
.x
- 5) / SCROLL_UNIT_X
, -1 );
3294 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3295 Scroll( (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
, -1 );
3299 // ----------------------------------------------------------------------------
3300 // keyboard handling
3301 // ----------------------------------------------------------------------------
3303 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3305 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3306 _T("invalid item index in OnArrowChar()") );
3308 size_t oldCurrent
= m_current
;
3310 // in single selection we just ignore Shift as we can't select several
3312 if ( event
.ShiftDown() && !IsSingleSel() )
3314 ChangeCurrent(newCurrent
);
3316 // refresh the old focus to remove it
3317 RefreshLine( oldCurrent
);
3319 // select all the items between the old and the new one
3320 if ( oldCurrent
> newCurrent
)
3322 newCurrent
= oldCurrent
;
3323 oldCurrent
= m_current
;
3326 HighlightLines(oldCurrent
, newCurrent
);
3330 // all previously selected items are unselected unless ctrl is held
3331 // in a multiselection control
3332 if ( !event
.ControlDown() || IsSingleSel() )
3333 HighlightAll(false);
3335 ChangeCurrent(newCurrent
);
3337 // refresh the old focus to remove it
3338 RefreshLine( oldCurrent
);
3340 // in single selection mode we must always have a selected item
3341 if ( !event
.ControlDown() || IsSingleSel() )
3342 HighlightLine( m_current
, true );
3345 RefreshLine( m_current
);
3350 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3352 wxWindow
*parent
= GetParent();
3354 // propagate the key event upwards
3355 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3356 ke
.m_shiftDown
= event
.m_shiftDown
;
3357 ke
.m_controlDown
= event
.m_controlDown
;
3358 ke
.m_altDown
= event
.m_altDown
;
3359 ke
.m_metaDown
= event
.m_metaDown
;
3360 ke
.m_keyCode
= event
.m_keyCode
;
3363 ke
.SetEventObject( parent
);
3364 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3369 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3371 wxWindow
*parent
= GetParent();
3373 // propagate the key event upwards
3374 wxKeyEvent
ke( wxEVT_KEY_UP
);
3375 ke
.m_shiftDown
= event
.m_shiftDown
;
3376 ke
.m_controlDown
= event
.m_controlDown
;
3377 ke
.m_altDown
= event
.m_altDown
;
3378 ke
.m_metaDown
= event
.m_metaDown
;
3379 ke
.m_keyCode
= event
.m_keyCode
;
3382 ke
.SetEventObject( parent
);
3383 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3388 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3390 wxWindow
*parent
= GetParent();
3392 // send a list_key event up
3395 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3396 le
.m_itemIndex
= m_current
;
3397 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3398 le
.m_code
= event
.GetKeyCode();
3399 le
.SetEventObject( parent
);
3400 parent
->GetEventHandler()->ProcessEvent( le
);
3403 // propagate the char event upwards
3404 wxKeyEvent
ke( wxEVT_CHAR
);
3405 ke
.m_shiftDown
= event
.m_shiftDown
;
3406 ke
.m_controlDown
= event
.m_controlDown
;
3407 ke
.m_altDown
= event
.m_altDown
;
3408 ke
.m_metaDown
= event
.m_metaDown
;
3409 ke
.m_keyCode
= event
.m_keyCode
;
3412 ke
.SetEventObject( parent
);
3413 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3415 if (event
.GetKeyCode() == WXK_TAB
)
3417 wxNavigationKeyEvent nevent
;
3418 nevent
.SetWindowChange( event
.ControlDown() );
3419 nevent
.SetDirection( !event
.ShiftDown() );
3420 nevent
.SetEventObject( GetParent()->GetParent() );
3421 nevent
.SetCurrentFocus( m_parent
);
3422 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3426 // no item -> nothing to do
3433 // don't use m_linesPerPage directly as it might not be computed yet
3434 const int pageSize
= GetCountPerPage();
3435 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3437 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3439 if (event
.GetKeyCode() == WXK_RIGHT
)
3440 event
.m_keyCode
= WXK_LEFT
;
3441 else if (event
.GetKeyCode() == WXK_LEFT
)
3442 event
.m_keyCode
= WXK_RIGHT
;
3445 switch ( event
.GetKeyCode() )
3448 if ( m_current
> 0 )
3449 OnArrowChar( m_current
- 1, event
);
3453 if ( m_current
< (size_t)GetItemCount() - 1 )
3454 OnArrowChar( m_current
+ 1, event
);
3459 OnArrowChar( GetItemCount() - 1, event
);
3464 OnArrowChar( 0, event
);
3469 int steps
= InReportView() ? pageSize
- 1
3470 : m_current
% pageSize
;
3472 int index
= m_current
- steps
;
3476 OnArrowChar( index
, event
);
3482 int steps
= InReportView()
3484 : pageSize
- (m_current
% pageSize
) - 1;
3486 size_t index
= m_current
+ steps
;
3487 size_t count
= GetItemCount();
3488 if ( index
>= count
)
3491 OnArrowChar( index
, event
);
3496 if ( !InReportView() )
3498 int index
= m_current
- pageSize
;
3502 OnArrowChar( index
, event
);
3507 if ( !InReportView() )
3509 size_t index
= m_current
+ pageSize
;
3511 size_t count
= GetItemCount();
3512 if ( index
>= count
)
3515 OnArrowChar( index
, event
);
3520 if ( IsSingleSel() )
3522 if ( event
.ControlDown() )
3524 ReverseHighlight(m_current
);
3526 else // normal space press
3528 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3531 else // multiple selection
3533 ReverseHighlight(m_current
);
3539 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3547 // ----------------------------------------------------------------------------
3549 // ----------------------------------------------------------------------------
3551 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3555 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3556 event
.SetEventObject( GetParent() );
3557 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3561 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3562 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3563 // which are already drawn correctly resulting in horrible flicker - avoid
3573 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3577 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3578 event
.SetEventObject( GetParent() );
3579 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3587 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3589 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3591 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3593 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3595 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3597 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3599 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3601 else if ( InReportView() && (m_small_image_list
))
3603 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3607 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3609 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3611 m_normal_image_list
->GetSize( index
, width
, height
);
3613 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3615 m_small_image_list
->GetSize( index
, width
, height
);
3617 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3619 m_small_image_list
->GetSize( index
, width
, height
);
3621 else if ( InReportView() && m_small_image_list
)
3623 m_small_image_list
->GetSize( index
, width
, height
);
3632 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3634 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3635 dc
.SetFont( GetFont() );
3638 dc
.GetTextExtent( s
, &lw
, NULL
);
3640 return lw
+ AUTOSIZE_COL_MARGIN
;
3643 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3647 // calc the spacing from the icon size
3648 int width
= 0, height
= 0;
3650 if ((imageList
) && (imageList
->GetImageCount()) )
3651 imageList
->GetSize(0, width
, height
);
3653 if (which
== wxIMAGE_LIST_NORMAL
)
3655 m_normal_image_list
= imageList
;
3656 m_normal_spacing
= width
+ 8;
3659 if (which
== wxIMAGE_LIST_SMALL
)
3661 m_small_image_list
= imageList
;
3662 m_small_spacing
= width
+ 14;
3663 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3667 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3671 m_small_spacing
= spacing
;
3673 m_normal_spacing
= spacing
;
3676 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3678 return isSmall
? m_small_spacing
: m_normal_spacing
;
3681 // ----------------------------------------------------------------------------
3683 // ----------------------------------------------------------------------------
3685 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3687 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3689 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3691 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3692 item
.m_width
= GetTextLength( item
.m_text
);
3694 wxListHeaderData
*column
= node
->GetData();
3695 column
->SetItem( item
);
3697 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3699 headerWin
->m_dirty
= true;
3703 // invalidate it as it has to be recalculated
3707 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3709 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3710 _T("invalid column index") );
3712 wxCHECK_RET( InReportView(),
3713 _T("SetColumnWidth() can only be called in report mode.") );
3716 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3718 headerWin
->m_dirty
= true;
3720 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3721 wxCHECK_RET( node
, _T("no column?") );
3723 wxListHeaderData
*column
= node
->GetData();
3725 size_t count
= GetItemCount();
3727 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3729 width
= GetTextLength(column
->GetText());
3730 width
+= 2*EXTRA_WIDTH
;
3732 // check for column header's image availability
3733 const int image
= column
->GetImage();
3736 if ( m_small_image_list
)
3739 m_small_image_list
->GetSize(image
, ix
, iy
);
3740 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3744 else if ( width
== wxLIST_AUTOSIZE
)
3748 // TODO: determine the max width somehow...
3749 width
= WIDTH_COL_DEFAULT
;
3753 wxClientDC
dc(this);
3754 dc
.SetFont( GetFont() );
3756 int max
= AUTOSIZE_COL_MARGIN
;
3758 // if the cached column width isn't valid then recalculate it
3759 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3761 for (size_t i
= 0; i
< count
; i
++)
3763 wxListLineData
*line
= GetLine( i
);
3764 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3766 wxCHECK_RET( n
, _T("no subitem?") );
3768 wxListItemData
*itemData
= n
->GetData();
3771 itemData
->GetItem(item
);
3772 int itemWidth
= GetItemWidthWithImage(&item
);
3773 if (itemWidth
> max
)
3777 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3778 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3781 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3782 width
= max
+ AUTOSIZE_COL_MARGIN
;
3786 column
->SetWidth( width
);
3788 // invalidate it as it has to be recalculated
3792 int wxListMainWindow::GetHeaderWidth() const
3794 if ( !m_headerWidth
)
3796 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3798 size_t count
= GetColumnCount();
3799 for ( size_t col
= 0; col
< count
; col
++ )
3801 self
->m_headerWidth
+= GetColumnWidth(col
);
3805 return m_headerWidth
;
3808 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3810 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3811 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3813 wxListHeaderData
*column
= node
->GetData();
3814 column
->GetItem( item
);
3817 int wxListMainWindow::GetColumnWidth( int col
) const
3819 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3820 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3822 wxListHeaderData
*column
= node
->GetData();
3823 return column
->GetWidth();
3826 // ----------------------------------------------------------------------------
3828 // ----------------------------------------------------------------------------
3830 void wxListMainWindow::SetItem( wxListItem
&item
)
3832 long id
= item
.m_itemId
;
3833 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3834 _T("invalid item index in SetItem") );
3838 wxListLineData
*line
= GetLine((size_t)id
);
3839 line
->SetItem( item
.m_col
, item
);
3841 // Set item state if user wants
3842 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3843 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3847 // update the Max Width Cache if needed
3848 int width
= GetItemWidthWithImage(&item
);
3850 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3851 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3855 // update the item on screen
3857 GetItemRect(id
, rectItem
);
3858 RefreshRect(rectItem
);
3861 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3866 // first deal with selection
3867 if ( stateMask
& wxLIST_STATE_SELECTED
)
3869 // set/clear select state
3872 // optimized version for virtual listctrl.
3873 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3876 else if ( state
& wxLIST_STATE_SELECTED
)
3878 const long count
= GetItemCount();
3879 for( long i
= 0; i
< count
; i
++ )
3881 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3887 // clear for non virtual (somewhat optimized by using GetNextItem())
3889 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3891 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3896 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3898 // unfocus all: only one item can be focussed, so clearing focus for
3899 // all items is simply clearing focus of the focussed item.
3900 SetItemState(m_current
, state
, stateMask
);
3902 //(setting focus to all items makes no sense, so it is not handled here.)
3905 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3909 SetItemStateAll(state
, stateMask
);
3913 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3914 _T("invalid list ctrl item index in SetItem") );
3916 size_t oldCurrent
= m_current
;
3917 size_t item
= (size_t)litem
; // safe because of the check above
3919 // do we need to change the focus?
3920 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3922 if ( state
& wxLIST_STATE_FOCUSED
)
3924 // don't do anything if this item is already focused
3925 if ( item
!= m_current
)
3927 ChangeCurrent(item
);
3929 if ( oldCurrent
!= (size_t)-1 )
3931 if ( IsSingleSel() )
3933 HighlightLine(oldCurrent
, false);
3936 RefreshLine(oldCurrent
);
3939 RefreshLine( m_current
);
3944 // don't do anything if this item is not focused
3945 if ( item
== m_current
)
3949 if ( IsSingleSel() )
3951 // we must unselect the old current item as well or we
3952 // might end up with more than one selected item in a
3953 // single selection control
3954 HighlightLine(oldCurrent
, false);
3957 RefreshLine( oldCurrent
);
3962 // do we need to change the selection state?
3963 if ( stateMask
& wxLIST_STATE_SELECTED
)
3965 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3967 if ( IsSingleSel() )
3971 // selecting the item also makes it the focused one in the
3973 if ( m_current
!= item
)
3975 ChangeCurrent(item
);
3977 if ( oldCurrent
!= (size_t)-1 )
3979 HighlightLine( oldCurrent
, false );
3980 RefreshLine( oldCurrent
);
3986 // only the current item may be selected anyhow
3987 if ( item
!= m_current
)
3992 if ( HighlightLine(item
, on
) )
3999 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
4001 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
4002 _T("invalid list ctrl item index in GetItemState()") );
4004 int ret
= wxLIST_STATE_DONTCARE
;
4006 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4008 if ( (size_t)item
== m_current
)
4009 ret
|= wxLIST_STATE_FOCUSED
;
4012 if ( stateMask
& wxLIST_STATE_SELECTED
)
4014 if ( IsHighlighted(item
) )
4015 ret
|= wxLIST_STATE_SELECTED
;
4021 void wxListMainWindow::GetItem( wxListItem
&item
) const
4023 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
4024 _T("invalid item index in GetItem") );
4026 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4027 line
->GetItem( item
.m_col
, item
);
4029 // Get item state if user wants it
4030 if ( item
.m_mask
& wxLIST_MASK_STATE
)
4031 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
4032 wxLIST_STATE_FOCUSED
);
4035 // ----------------------------------------------------------------------------
4037 // ----------------------------------------------------------------------------
4039 size_t wxListMainWindow::GetItemCount() const
4041 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4044 void wxListMainWindow::SetItemCount(long count
)
4046 m_selStore
.SetItemCount(count
);
4047 m_countVirt
= count
;
4049 ResetVisibleLinesRange();
4051 // scrollbars must be reset
4055 int wxListMainWindow::GetSelectedItemCount() const
4057 // deal with the quick case first
4058 if ( IsSingleSel() )
4059 return HasCurrent() ? IsHighlighted(m_current
) : false;
4061 // virtual controls remmebers all its selections itself
4063 return m_selStore
.GetSelectedCount();
4065 // TODO: we probably should maintain the number of items selected even for
4066 // non virtual controls as enumerating all lines is really slow...
4067 size_t countSel
= 0;
4068 size_t count
= GetItemCount();
4069 for ( size_t line
= 0; line
< count
; line
++ )
4071 if ( GetLine(line
)->IsHighlighted() )
4078 // ----------------------------------------------------------------------------
4079 // item position/size
4080 // ----------------------------------------------------------------------------
4082 wxRect
wxListMainWindow::GetViewRect() const
4084 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
4085 _T("wxListCtrl::GetViewRect() only works in icon mode") );
4087 // we need to find the longest/tallest label
4088 wxCoord xMax
= 0, yMax
= 0;
4089 const int count
= GetItemCount();
4092 for ( int i
= 0; i
< count
; i
++ )
4097 wxCoord x
= r
.GetRight(),
4107 // some fudge needed to make it look prettier
4108 xMax
+= 2 * EXTRA_BORDER_X
;
4109 yMax
+= 2 * EXTRA_BORDER_Y
;
4111 // account for the scrollbars if necessary
4112 const wxSize sizeAll
= GetClientSize();
4113 if ( xMax
> sizeAll
.x
)
4114 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4115 if ( yMax
> sizeAll
.y
)
4116 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4118 return wxRect(0, 0, xMax
, yMax
);
4121 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
4123 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4124 _T("invalid index in GetItemRect") );
4126 // ensure that we're laid out, otherwise we could return nonsense
4129 wxConstCast(this, wxListMainWindow
)->
4130 RecalculatePositions(true /* no refresh */);
4133 rect
= GetLineRect((size_t)index
);
4135 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4138 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4141 GetItemRect(item
, rect
);
4149 // ----------------------------------------------------------------------------
4150 // geometry calculation
4151 // ----------------------------------------------------------------------------
4153 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4155 const int lineHeight
= GetLineHeight();
4157 wxClientDC
dc( this );
4158 dc
.SetFont( GetFont() );
4160 const size_t count
= GetItemCount();
4163 if ( HasFlag(wxLC_ICON
) )
4164 iconSpacing
= m_normal_spacing
;
4165 else if ( HasFlag(wxLC_SMALL_ICON
) )
4166 iconSpacing
= m_small_spacing
;
4170 // Note that we do not call GetClientSize() here but
4171 // GetSize() and subtract the border size for sunken
4172 // borders manually. This is technically incorrect,
4173 // but we need to know the client area's size WITHOUT
4174 // scrollbars here. Since we don't know if there are
4175 // any scrollbars, we use GetSize() instead. Another
4176 // solution would be to call SetScrollbars() here to
4177 // remove the scrollbars and call GetClientSize() then,
4178 // but this might result in flicker and - worse - will
4179 // reset the scrollbars to 0 which is not good at all
4180 // if you resize a dialog/window, but don't want to
4181 // reset the window scrolling. RR.
4182 // Furthermore, we actually do NOT subtract the border
4183 // width as 2 pixels is just the extra space which we
4184 // need around the actual content in the window. Other-
4185 // wise the text would e.g. touch the upper border. RR.
4188 GetSize( &clientWidth
, &clientHeight
);
4190 if ( InReportView() )
4192 // all lines have the same height and we scroll one line per step
4193 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4195 m_linesPerPage
= clientHeight
/ lineHeight
;
4197 ResetVisibleLinesRange();
4199 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4200 GetHeaderWidth() / SCROLL_UNIT_X
,
4201 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4202 GetScrollPos(wxHORIZONTAL
),
4203 GetScrollPos(wxVERTICAL
),
4208 // we have 3 different layout strategies: either layout all items
4209 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4210 // to arrange them in top to bottom, left to right (don't ask me why
4211 // not the other way round...) order
4212 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4214 int x
= EXTRA_BORDER_X
;
4215 int y
= EXTRA_BORDER_Y
;
4217 wxCoord widthMax
= 0;
4220 for ( i
= 0; i
< count
; i
++ )
4222 wxListLineData
*line
= GetLine(i
);
4223 line
->CalculateSize( &dc
, iconSpacing
);
4224 line
->SetPosition( x
, y
, iconSpacing
);
4226 wxSize sizeLine
= GetLineSize(i
);
4228 if ( HasFlag(wxLC_ALIGN_TOP
) )
4230 if ( sizeLine
.x
> widthMax
)
4231 widthMax
= sizeLine
.x
;
4235 else // wxLC_ALIGN_LEFT
4237 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4241 if ( HasFlag(wxLC_ALIGN_TOP
) )
4243 // traverse the items again and tweak their sizes so that they are
4244 // all the same in a row
4245 for ( i
= 0; i
< count
; i
++ )
4247 wxListLineData
*line
= GetLine(i
);
4248 line
->m_gi
->ExtendWidth(widthMax
);
4256 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4257 (y
+ lineHeight
) / lineHeight
,
4258 GetScrollPos( wxHORIZONTAL
),
4259 GetScrollPos( wxVERTICAL
),
4263 else // "flowed" arrangement, the most complicated case
4265 // at first we try without any scrollbars, if the items don't fit into
4266 // the window, we recalculate after subtracting the space taken by the
4269 int entireWidth
= 0;
4271 for (int tries
= 0; tries
< 2; tries
++)
4273 entireWidth
= 2 * EXTRA_BORDER_X
;
4277 // Now we have decided that the items do not fit into the
4278 // client area, so we need a scrollbar
4279 entireWidth
+= SCROLL_UNIT_X
;
4282 int x
= EXTRA_BORDER_X
;
4283 int y
= EXTRA_BORDER_Y
;
4284 int maxWidthInThisRow
= 0;
4287 int currentlyVisibleLines
= 0;
4289 for (size_t i
= 0; i
< count
; i
++)
4291 currentlyVisibleLines
++;
4292 wxListLineData
*line
= GetLine( i
);
4293 line
->CalculateSize( &dc
, iconSpacing
);
4294 line
->SetPosition( x
, y
, iconSpacing
);
4296 wxSize sizeLine
= GetLineSize( i
);
4298 if ( maxWidthInThisRow
< sizeLine
.x
)
4299 maxWidthInThisRow
= sizeLine
.x
;
4302 if (currentlyVisibleLines
> m_linesPerPage
)
4303 m_linesPerPage
= currentlyVisibleLines
;
4305 if ( y
+ sizeLine
.y
>= clientHeight
)
4307 currentlyVisibleLines
= 0;
4309 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4310 x
+= maxWidthInThisRow
;
4311 entireWidth
+= maxWidthInThisRow
;
4312 maxWidthInThisRow
= 0;
4315 // We have reached the last item.
4316 if ( i
== count
- 1 )
4317 entireWidth
+= maxWidthInThisRow
;
4319 if ( (tries
== 0) &&
4320 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4322 clientHeight
-= wxSystemSettings::
4323 GetMetric(wxSYS_HSCROLL_Y
);
4328 if ( i
== count
- 1 )
4329 tries
= 1; // Everything fits, no second try required.
4337 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4339 GetScrollPos( wxHORIZONTAL
),
4348 // FIXME: why should we call it from here?
4355 void wxListMainWindow::RefreshAll()
4360 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4361 if ( headerWin
&& headerWin
->m_dirty
)
4363 headerWin
->m_dirty
= false;
4364 headerWin
->Refresh();
4368 void wxListMainWindow::UpdateCurrent()
4370 if ( !HasCurrent() && !IsEmpty() )
4374 long wxListMainWindow::GetNextItem( long item
,
4375 int WXUNUSED(geometry
),
4379 max
= GetItemCount();
4380 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4381 _T("invalid listctrl index in GetNextItem()") );
4383 // notice that we start with the next item (or the first one if item == -1)
4384 // and this is intentional to allow writing a simple loop to iterate over
4385 // all selected items
4388 // this is not an error because the index was OK initially,
4389 // just no such item
4396 size_t count
= GetItemCount();
4397 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4399 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4402 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4409 // ----------------------------------------------------------------------------
4411 // ----------------------------------------------------------------------------
4413 void wxListMainWindow::DeleteItem( long lindex
)
4415 size_t count
= GetItemCount();
4417 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4418 _T("invalid item index in DeleteItem") );
4420 size_t index
= (size_t)lindex
;
4422 // we don't need to adjust the index for the previous items
4423 if ( HasCurrent() && m_current
>= index
)
4425 // if the current item is being deleted, we want the next one to
4426 // become selected - unless there is no next one - so don't adjust
4427 // m_current in this case
4428 if ( m_current
!= index
|| m_current
== count
- 1 )
4432 if ( InReportView() )
4434 // mark the Column Max Width cache as dirty if the items in the line
4435 // we're deleting contain the Max Column Width
4436 wxListLineData
* const line
= GetLine(index
);
4437 wxListItemDataList::compatibility_iterator n
;
4438 wxListItemData
*itemData
;
4442 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4444 n
= line
->m_items
.Item( i
);
4445 itemData
= n
->GetData();
4446 itemData
->GetItem(item
);
4448 itemWidth
= GetItemWidthWithImage(&item
);
4450 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4451 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4454 ResetVisibleLinesRange();
4457 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4462 m_selStore
.OnItemDelete(index
);
4466 m_lines
.RemoveAt( index
);
4469 // we need to refresh the (vert) scrollbar as the number of items changed
4472 RefreshAfter(index
);
4475 void wxListMainWindow::DeleteColumn( int col
)
4477 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4479 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4482 delete node
->GetData();
4483 m_columns
.Erase( node
);
4487 // update all the items
4488 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4490 wxListLineData
* const line
= GetLine(i
);
4491 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4492 delete n
->GetData();
4493 line
->m_items
.Erase(n
);
4497 if ( InReportView() ) // we only cache max widths when in Report View
4499 delete m_aColWidths
.Item(col
);
4500 m_aColWidths
.RemoveAt(col
);
4503 // invalidate it as it has to be recalculated
4507 void wxListMainWindow::DoDeleteAllItems()
4510 // nothing to do - in particular, don't send the event
4515 // to make the deletion of all items faster, we don't send the
4516 // notifications for each item deletion in this case but only one event
4517 // for all of them: this is compatible with wxMSW and documented in
4518 // DeleteAllItems() description
4520 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4521 event
.SetEventObject( GetParent() );
4522 GetParent()->GetEventHandler()->ProcessEvent( event
);
4530 if ( InReportView() )
4532 ResetVisibleLinesRange();
4533 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4535 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4542 void wxListMainWindow::DeleteAllItems()
4546 RecalculatePositions();
4549 void wxListMainWindow::DeleteEverything()
4551 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4552 WX_CLEAR_ARRAY(m_aColWidths
);
4557 // ----------------------------------------------------------------------------
4558 // scanning for an item
4559 // ----------------------------------------------------------------------------
4561 void wxListMainWindow::EnsureVisible( long index
)
4563 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4564 _T("invalid index in EnsureVisible") );
4566 // We have to call this here because the label in question might just have
4567 // been added and its position is not known yet
4569 RecalculatePositions(true /* no refresh */);
4571 MoveToItem((size_t)index
);
4574 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4580 wxString str_upper
= str
.Upper();
4584 size_t count
= GetItemCount();
4585 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4587 wxListLineData
*line
= GetLine(i
);
4588 wxString line_upper
= line
->GetText(0).Upper();
4591 if (line_upper
== str_upper
)
4596 if (line_upper
.find(str_upper
) == 0)
4604 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4610 size_t count
= GetItemCount();
4611 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4613 wxListLineData
*line
= GetLine(i
);
4615 line
->GetItem( 0, item
);
4616 if (item
.m_data
== data
)
4623 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4626 GetVisibleLinesRange( &topItem
, NULL
);
4629 GetItemPosition( GetItemCount() - 1, p
);
4633 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4634 if ( id
>= 0 && id
< (long)GetItemCount() )
4640 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4642 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4644 size_t count
= GetItemCount();
4646 if ( InReportView() )
4648 size_t current
= y
/ GetLineHeight();
4649 if ( current
< count
)
4651 flags
= HitTestLine(current
, x
, y
);
4658 // TODO: optimize it too! this is less simple than for report view but
4659 // enumerating all items is still not a way to do it!!
4660 for ( size_t current
= 0; current
< count
; current
++ )
4662 flags
= HitTestLine(current
, x
, y
);
4671 // ----------------------------------------------------------------------------
4673 // ----------------------------------------------------------------------------
4675 void wxListMainWindow::InsertItem( wxListItem
&item
)
4677 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4679 int count
= GetItemCount();
4680 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4682 if (item
.m_itemId
> count
)
4683 item
.m_itemId
= count
;
4685 size_t id
= item
.m_itemId
;
4689 if ( InReportView() )
4691 ResetVisibleLinesRange();
4693 // calculate the width of the item and adjust the max column width
4694 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4695 int width
= GetItemWidthWithImage(&item
);
4696 item
.SetWidth(width
);
4697 if (width
> pWidthInfo
->nMaxWidth
)
4698 pWidthInfo
->nMaxWidth
= width
;
4701 wxListLineData
*line
= new wxListLineData(this);
4703 line
->SetItem( item
.m_col
, item
);
4705 m_lines
.Insert( line
, id
);
4709 // If an item is selected at or below the point of insertion, we need to
4710 // increment the member variables because the current row's index has gone
4712 if ( HasCurrent() && m_current
>= id
)
4715 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4717 RefreshLines(id
, GetItemCount() - 1);
4720 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4723 if ( InReportView() )
4725 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4726 item
.m_width
= GetTextLength( item
.m_text
);
4728 wxListHeaderData
*column
= new wxListHeaderData( item
);
4729 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4731 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4734 wxListHeaderDataList::compatibility_iterator
4735 node
= m_columns
.Item( col
);
4736 m_columns
.Insert( node
, column
);
4737 m_aColWidths
.Insert( colWidthInfo
, col
);
4741 m_columns
.Append( column
);
4742 m_aColWidths
.Add( colWidthInfo
);
4747 // update all the items
4748 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4750 wxListLineData
* const line
= GetLine(i
);
4751 wxListItemData
* const data
= new wxListItemData(this);
4753 line
->m_items
.Insert(col
, data
);
4755 line
->m_items
.Append(data
);
4759 // invalidate it as it has to be recalculated
4764 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4767 wxClientDC
dc(this);
4769 dc
.SetFont( GetFont() );
4771 if (item
->GetImage() != -1)
4774 GetImageSize( item
->GetImage(), ix
, iy
);
4778 if (!item
->GetText().empty())
4781 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4788 // ----------------------------------------------------------------------------
4790 // ----------------------------------------------------------------------------
4792 wxListCtrlCompare list_ctrl_compare_func_2
;
4793 long list_ctrl_compare_data
;
4795 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4797 wxListLineData
*line1
= *arg1
;
4798 wxListLineData
*line2
= *arg2
;
4800 line1
->GetItem( 0, item
);
4801 wxUIntPtr data1
= item
.m_data
;
4802 line2
->GetItem( 0, item
);
4803 wxUIntPtr data2
= item
.m_data
;
4804 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4807 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4809 // selections won't make sense any more after sorting the items so reset
4811 HighlightAll(false);
4814 list_ctrl_compare_func_2
= fn
;
4815 list_ctrl_compare_data
= data
;
4816 m_lines
.Sort( list_ctrl_compare_func_1
);
4820 // ----------------------------------------------------------------------------
4822 // ----------------------------------------------------------------------------
4824 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4827 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4828 wxScrolledWindow::OnScroll(event
);
4830 HandleOnScroll( event
);
4833 // update our idea of which lines are shown when we redraw the window the
4835 ResetVisibleLinesRange();
4837 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4839 wxGenericListCtrl
* lc
= GetListCtrl();
4840 wxCHECK_RET( lc
, _T("no listctrl window?") );
4842 lc
->m_headerWin
->Refresh();
4843 lc
->m_headerWin
->Update();
4847 int wxListMainWindow::GetCountPerPage() const
4849 if ( !m_linesPerPage
)
4851 wxConstCast(this, wxListMainWindow
)->
4852 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4855 return m_linesPerPage
;
4858 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4860 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4862 if ( m_lineFrom
== (size_t)-1 )
4864 size_t count
= GetItemCount();
4867 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4869 // this may happen if SetScrollbars() hadn't been called yet
4870 if ( m_lineFrom
>= count
)
4871 m_lineFrom
= count
- 1;
4873 // we redraw one extra line but this is needed to make the redrawing
4874 // logic work when there is a fractional number of lines on screen
4875 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4876 if ( m_lineTo
>= count
)
4877 m_lineTo
= count
- 1;
4879 else // empty control
4882 m_lineTo
= (size_t)-1;
4886 wxASSERT_MSG( IsEmpty() ||
4887 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4888 _T("GetVisibleLinesRange() returns incorrect result") );
4896 // -------------------------------------------------------------------------------------
4897 // wxGenericListCtrl
4898 // -------------------------------------------------------------------------------------
4900 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4902 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4903 EVT_SIZE(wxGenericListCtrl::OnSize
)
4906 wxGenericListCtrl::wxGenericListCtrl()
4908 m_imageListNormal
= (wxImageList
*) NULL
;
4909 m_imageListSmall
= (wxImageList
*) NULL
;
4910 m_imageListState
= (wxImageList
*) NULL
;
4912 m_ownsImageListNormal
=
4913 m_ownsImageListSmall
=
4914 m_ownsImageListState
= false;
4916 m_mainWin
= (wxListMainWindow
*) NULL
;
4917 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4921 wxGenericListCtrl::~wxGenericListCtrl()
4923 if (m_ownsImageListNormal
)
4924 delete m_imageListNormal
;
4925 if (m_ownsImageListSmall
)
4926 delete m_imageListSmall
;
4927 if (m_ownsImageListState
)
4928 delete m_imageListState
;
4931 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4937 GetThemeMetric( kThemeMetricListHeaderHeight
, &h
);
4939 // we use 'g' to get the descent, too
4941 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4942 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4945 // only update if changed
4946 if ( h
!= m_headerHeight
)
4951 ResizeReportView(true);
4952 else //why is this needed if it doesn't have a header?
4953 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4958 void wxGenericListCtrl::CreateHeaderWindow()
4960 m_headerWin
= new wxListHeaderWindow
4962 this, wxID_ANY
, m_mainWin
,
4964 wxSize(GetClientSize().x
, m_headerHeight
),
4967 CalculateAndSetHeaderHeight();
4970 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4975 const wxValidator
&validator
,
4976 const wxString
&name
)
4980 m_imageListState
= (wxImageList
*) NULL
;
4981 m_ownsImageListNormal
=
4982 m_ownsImageListSmall
=
4983 m_ownsImageListState
= false;
4985 m_mainWin
= (wxListMainWindow
*) NULL
;
4986 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4990 if ( !(style
& wxLC_MASK_TYPE
) )
4992 style
= style
| wxLC_LIST
;
4995 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4998 // this window itself shouldn't get the focus, only m_mainWin should
5001 // don't create the inner window with the border
5002 style
&= ~wxBORDER_MASK
;
5004 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
5006 #ifdef __WXMAC_CARBON__
5007 // Human Interface Guidelines ask us for a special font in this case
5008 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
)
5011 font
.MacCreateThemeFont( kThemeViewsFont
);
5016 if ( InReportView() )
5018 CreateHeaderWindow();
5020 #ifdef __WXMAC_CARBON__
5024 font
.MacCreateThemeFont( kThemeSmallSystemFont
);
5025 m_headerWin
->SetFont( font
);
5026 CalculateAndSetHeaderHeight();
5030 if ( HasFlag(wxLC_NO_HEADER
) )
5031 // VZ: why do we create it at all then?
5032 m_headerWin
->Show( false );
5035 SetInitialSize(size
);
5040 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
5042 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
5043 _T("wxLC_VIRTUAL can't be [un]set") );
5045 long flag
= GetWindowStyle();
5049 if (style
& wxLC_MASK_TYPE
)
5050 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5051 if (style
& wxLC_MASK_ALIGN
)
5052 flag
&= ~wxLC_MASK_ALIGN
;
5053 if (style
& wxLC_MASK_SORT
)
5054 flag
&= ~wxLC_MASK_SORT
;
5062 SetWindowStyleFlag( flag
);
5065 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5069 m_mainWin
->DeleteEverything();
5071 // has the header visibility changed?
5072 bool hasHeader
= HasHeader();
5073 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
5075 if ( hasHeader
!= willHaveHeader
)
5082 // don't delete, just hide, as we can reuse it later
5083 m_headerWin
->Show(false);
5085 //else: nothing to do
5087 else // must show header
5091 CreateHeaderWindow();
5093 else // already have it, just show
5095 m_headerWin
->Show( true );
5099 ResizeReportView(willHaveHeader
);
5103 wxWindow::SetWindowStyleFlag( flag
);
5106 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5108 m_mainWin
->GetColumn( col
, item
);
5112 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5114 m_mainWin
->SetColumn( col
, item
);
5118 int wxGenericListCtrl::GetColumnWidth( int col
) const
5120 return m_mainWin
->GetColumnWidth( col
);
5123 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5125 m_mainWin
->SetColumnWidth( col
, width
);
5129 int wxGenericListCtrl::GetCountPerPage() const
5131 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5134 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5136 m_mainWin
->GetItem( info
);
5140 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5142 m_mainWin
->SetItem( info
);
5146 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5149 info
.m_text
= label
;
5150 info
.m_mask
= wxLIST_MASK_TEXT
;
5151 info
.m_itemId
= index
;
5155 info
.m_image
= imageId
;
5156 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5159 m_mainWin
->SetItem(info
);
5163 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5165 return m_mainWin
->GetItemState( item
, stateMask
);
5168 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5170 m_mainWin
->SetItemState( item
, state
, stateMask
);
5175 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5177 return SetItemColumnImage(item
, 0, image
);
5181 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5184 info
.m_image
= image
;
5185 info
.m_mask
= wxLIST_MASK_IMAGE
;
5186 info
.m_itemId
= item
;
5187 info
.m_col
= column
;
5188 m_mainWin
->SetItem( info
);
5192 wxString
wxGenericListCtrl::GetItemText( long item
) const
5194 return m_mainWin
->GetItemText(item
);
5197 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5199 m_mainWin
->SetItemText(item
, str
);
5202 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5205 info
.m_mask
= wxLIST_MASK_DATA
;
5206 info
.m_itemId
= item
;
5207 m_mainWin
->GetItem( info
);
5211 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
5214 info
.m_mask
= wxLIST_MASK_DATA
;
5215 info
.m_itemId
= item
;
5217 m_mainWin
->SetItem( info
);
5221 wxRect
wxGenericListCtrl::GetViewRect() const
5223 return m_mainWin
->GetViewRect();
5226 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
5228 m_mainWin
->GetItemRect( item
, rect
);
5229 if ( m_mainWin
->HasHeader() )
5230 rect
.y
+= m_headerHeight
+ 1;
5234 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5236 m_mainWin
->GetItemPosition( item
, pos
);
5240 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5245 int wxGenericListCtrl::GetItemCount() const
5247 return m_mainWin
->GetItemCount();
5250 int wxGenericListCtrl::GetColumnCount() const
5252 return m_mainWin
->GetColumnCount();
5255 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5257 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5260 wxSize
wxGenericListCtrl::GetItemSpacing() const
5262 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5264 return wxSize(spacing
, spacing
);
5267 #if WXWIN_COMPATIBILITY_2_6
5268 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5270 return m_mainWin
->GetItemSpacing( isSmall
);
5272 #endif // WXWIN_COMPATIBILITY_2_6
5274 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5277 info
.m_itemId
= item
;
5278 info
.SetTextColour( col
);
5279 m_mainWin
->SetItem( info
);
5282 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5285 info
.m_itemId
= item
;
5286 m_mainWin
->GetItem( info
);
5287 return info
.GetTextColour();
5290 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5293 info
.m_itemId
= item
;
5294 info
.SetBackgroundColour( col
);
5295 m_mainWin
->SetItem( info
);
5298 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5301 info
.m_itemId
= item
;
5302 m_mainWin
->GetItem( info
);
5303 return info
.GetBackgroundColour();
5306 int wxGenericListCtrl::GetScrollPos( int orient
) const
5308 return m_mainWin
->GetScrollPos( orient
);
5311 void wxGenericListCtrl::SetScrollPos( int orient
, int pos
, bool refresh
)
5313 m_mainWin
->SetScrollPos( orient
, pos
, refresh
);
5316 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5319 info
.m_itemId
= item
;
5321 m_mainWin
->SetItem( info
);
5324 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5327 info
.m_itemId
= item
;
5328 m_mainWin
->GetItem( info
);
5329 return info
.GetFont();
5332 int wxGenericListCtrl::GetSelectedItemCount() const
5334 return m_mainWin
->GetSelectedItemCount();
5337 wxColour
wxGenericListCtrl::GetTextColour() const
5339 return GetForegroundColour();
5342 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5344 SetForegroundColour(col
);
5347 long wxGenericListCtrl::GetTopItem() const
5350 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5354 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5356 return m_mainWin
->GetNextItem( item
, geom
, state
);
5359 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5361 if (which
== wxIMAGE_LIST_NORMAL
)
5362 return m_imageListNormal
;
5363 else if (which
== wxIMAGE_LIST_SMALL
)
5364 return m_imageListSmall
;
5365 else if (which
== wxIMAGE_LIST_STATE
)
5366 return m_imageListState
;
5368 return (wxImageList
*) NULL
;
5371 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5373 if ( which
== wxIMAGE_LIST_NORMAL
)
5375 if (m_ownsImageListNormal
)
5376 delete m_imageListNormal
;
5377 m_imageListNormal
= imageList
;
5378 m_ownsImageListNormal
= false;
5380 else if ( which
== wxIMAGE_LIST_SMALL
)
5382 if (m_ownsImageListSmall
)
5383 delete m_imageListSmall
;
5384 m_imageListSmall
= imageList
;
5385 m_ownsImageListSmall
= false;
5387 else if ( which
== wxIMAGE_LIST_STATE
)
5389 if (m_ownsImageListState
)
5390 delete m_imageListState
;
5391 m_imageListState
= imageList
;
5392 m_ownsImageListState
= false;
5395 m_mainWin
->SetImageList( imageList
, which
);
5398 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5400 SetImageList(imageList
, which
);
5401 if ( which
== wxIMAGE_LIST_NORMAL
)
5402 m_ownsImageListNormal
= true;
5403 else if ( which
== wxIMAGE_LIST_SMALL
)
5404 m_ownsImageListSmall
= true;
5405 else if ( which
== wxIMAGE_LIST_STATE
)
5406 m_ownsImageListState
= true;
5409 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5414 bool wxGenericListCtrl::DeleteItem( long item
)
5416 m_mainWin
->DeleteItem( item
);
5420 bool wxGenericListCtrl::DeleteAllItems()
5422 m_mainWin
->DeleteAllItems();
5426 bool wxGenericListCtrl::DeleteAllColumns()
5428 size_t count
= m_mainWin
->m_columns
.GetCount();
5429 for ( size_t n
= 0; n
< count
; n
++ )
5434 void wxGenericListCtrl::ClearAll()
5436 m_mainWin
->DeleteEverything();
5439 bool wxGenericListCtrl::DeleteColumn( int col
)
5441 m_mainWin
->DeleteColumn( col
);
5443 // if we don't have the header any longer, we need to relayout the window
5444 if ( !GetColumnCount() )
5445 ResizeReportView(false /* no header */);
5449 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5450 wxClassInfo
* textControlClass
)
5452 return m_mainWin
->EditLabel( item
, textControlClass
);
5455 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5457 return m_mainWin
->GetEditControl();
5460 bool wxGenericListCtrl::EnsureVisible( long item
)
5462 m_mainWin
->EnsureVisible( item
);
5466 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5468 return m_mainWin
->FindItem( start
, str
, partial
);
5471 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5473 return m_mainWin
->FindItem( start
, data
);
5476 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5477 int WXUNUSED(direction
))
5479 return m_mainWin
->FindItem( pt
);
5482 // TODO: sub item hit testing
5483 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5485 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5488 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5490 m_mainWin
->InsertItem( info
);
5491 return info
.m_itemId
;
5494 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5497 info
.m_text
= label
;
5498 info
.m_mask
= wxLIST_MASK_TEXT
;
5499 info
.m_itemId
= index
;
5500 return InsertItem( info
);
5503 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5506 info
.m_mask
= wxLIST_MASK_IMAGE
;
5507 info
.m_image
= imageIndex
;
5508 info
.m_itemId
= index
;
5509 return InsertItem( info
);
5512 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5515 info
.m_text
= label
;
5516 info
.m_image
= imageIndex
;
5517 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5518 info
.m_itemId
= index
;
5519 return InsertItem( info
);
5522 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5524 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5526 m_mainWin
->InsertColumn( col
, item
);
5528 // if we hadn't had a header before but have one now
5529 // then we need to relayout the window
5530 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5531 ResizeReportView(true /* have header */);
5533 m_headerWin
->Refresh();
5538 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5539 int format
, int width
)
5542 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5543 item
.m_text
= heading
;
5546 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5547 item
.m_width
= width
;
5550 item
.m_format
= format
;
5552 return InsertColumn( col
, item
);
5555 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5561 // fn is a function which takes 3 long arguments: item1, item2, data.
5562 // item1 is the long data associated with a first item (NOT the index).
5563 // item2 is the long data associated with a second item (NOT the index).
5564 // data is the same value as passed to SortItems.
5565 // The return value is a negative number if the first item should precede the second
5566 // item, a positive number of the second item should precede the first,
5567 // or zero if the two items are equivalent.
5568 // data is arbitrary data to be passed to the sort function.
5570 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5572 m_mainWin
->SortItems( fn
, data
);
5576 // ----------------------------------------------------------------------------
5578 // ----------------------------------------------------------------------------
5580 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5585 ResizeReportView(m_mainWin
->HasHeader());
5586 m_mainWin
->RecalculatePositions();
5589 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5592 GetClientSize( &cw
, &ch
);
5596 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5597 if(ch
> m_headerHeight
)
5598 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5599 cw
, ch
- m_headerHeight
- 1 );
5601 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5604 else // no header window
5606 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5610 void wxGenericListCtrl::OnInternalIdle()
5612 wxWindow::OnInternalIdle();
5614 // do it only if needed
5615 if ( !m_mainWin
->m_dirty
)
5618 m_mainWin
->RecalculatePositions();
5621 // ----------------------------------------------------------------------------
5623 // ----------------------------------------------------------------------------
5625 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5629 m_mainWin
->SetBackgroundColour( colour
);
5630 m_mainWin
->m_dirty
= true;
5636 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5638 if ( !wxWindow::SetForegroundColour( colour
) )
5643 m_mainWin
->SetForegroundColour( colour
);
5644 m_mainWin
->m_dirty
= true;
5648 m_headerWin
->SetForegroundColour( colour
);
5653 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5655 if ( !wxWindow::SetFont( font
) )
5660 m_mainWin
->SetFont( font
);
5661 m_mainWin
->m_dirty
= true;
5666 m_headerWin
->SetFont( font
);
5667 CalculateAndSetHeaderHeight();
5677 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5680 // Use the same color scheme as wxListBox
5681 return wxListBox::GetClassDefaultAttributes(variant
);
5683 wxUnusedVar(variant
);
5684 wxVisualAttributes attr
;
5685 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5686 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5687 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5692 // ----------------------------------------------------------------------------
5693 // methods forwarded to m_mainWin
5694 // ----------------------------------------------------------------------------
5696 #if wxUSE_DRAG_AND_DROP
5698 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5700 m_mainWin
->SetDropTarget( dropTarget
);
5703 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5705 return m_mainWin
->GetDropTarget();
5710 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5712 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5715 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5717 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5720 wxColour
wxGenericListCtrl::GetForegroundColour() const
5722 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5725 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5728 return m_mainWin
->PopupMenu( menu
, x
, y
);
5734 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5736 m_mainWin
->DoClientToScreen(x
, y
);
5739 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5741 m_mainWin
->DoScreenToClient(x
, y
);
5744 void wxGenericListCtrl::SetFocus()
5746 // The test in window.cpp fails as we are a composite
5747 // window, so it checks against "this", but not m_mainWin.
5748 if ( DoFindFocus() != this )
5749 m_mainWin
->SetFocus();
5752 wxSize
wxGenericListCtrl::DoGetBestSize() const
5754 // Something is better than nothing...
5755 // 100x80 is what the MSW version will get from the default
5756 // wxControl::DoGetBestSize
5757 return wxSize(100, 80);
5760 // ----------------------------------------------------------------------------
5761 // virtual list control support
5762 // ----------------------------------------------------------------------------
5764 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5766 // this is a pure virtual function, in fact - which is not really pure
5767 // because the controls which are not virtual don't need to implement it
5768 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5770 return wxEmptyString
;
5773 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5775 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5777 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5781 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5784 return OnGetItemImage(item
);
5790 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5792 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5793 _T("invalid item index in OnGetItemAttr()") );
5795 // no attributes by default
5799 void wxGenericListCtrl::SetItemCount(long count
)
5801 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5803 m_mainWin
->SetItemCount(count
);
5806 void wxGenericListCtrl::RefreshItem(long item
)
5808 m_mainWin
->RefreshLine(item
);
5811 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5813 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5816 // Generic wxListCtrl is more or less a container for two other
5817 // windows which drawings are done upon. These are namely
5818 // 'm_headerWin' and 'm_mainWin'.
5819 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5820 // behaviour wxListCtrl has under wxMSW.
5822 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5826 // The easy case, no rectangle specified.
5828 m_headerWin
->Refresh(eraseBackground
);
5831 m_mainWin
->Refresh(eraseBackground
);
5835 // Refresh the header window
5838 wxRect rectHeader
= m_headerWin
->GetRect();
5839 rectHeader
.Intersect(*rect
);
5840 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5843 m_headerWin
->GetPosition(&x
, &y
);
5844 rectHeader
.Offset(-x
, -y
);
5845 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5849 // Refresh the main window
5852 wxRect rectMain
= m_mainWin
->GetRect();
5853 rectMain
.Intersect(*rect
);
5854 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5857 m_mainWin
->GetPosition(&x
, &y
);
5858 rectMain
.Offset(-x
, -y
);
5859 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5865 void wxGenericListCtrl::Freeze()
5867 m_mainWin
->Freeze();
5870 void wxGenericListCtrl::Thaw()
5875 #endif // wxUSE_LISTCTRL