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(__WXMAC__) && wxOSX_USE_CARBON)) || defined(__WXUNIVERSAL__))
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"
49 #include "wx/imaglist.h"
50 #include "wx/selstore.h"
51 #include "wx/renderer.h"
54 #include "wx/osx/private.h"
55 // for themeing support
56 #include <Carbon/Carbon.h>
60 // NOTE: If using the wxListBox visual attributes works everywhere then this can
61 // be removed, as well as the #else case below.
62 #define _USE_VISATTR 0
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // // the height of the header window (FIXME: should depend on its font!)
70 // static const int HEADER_HEIGHT = 23;
72 static const int SCROLL_UNIT_X
= 15;
74 // the spacing between the lines (in report mode)
75 static const int LINE_SPACING
= 0;
77 // extra margins around the text label
79 static const int EXTRA_WIDTH
= 6;
81 static const int EXTRA_WIDTH
= 4;
85 static const int EXTRA_HEIGHT
= 6;
87 static const int EXTRA_HEIGHT
= 4;
90 // margin between the window and the items
91 static const int EXTRA_BORDER_X
= 2;
92 static const int EXTRA_BORDER_Y
= 2;
94 // offset for the header window
95 static const int HEADER_OFFSET_X
= 0;
96 static const int HEADER_OFFSET_Y
= 0;
98 // margin between rows of icons in [small] icon view
99 static const int MARGIN_BETWEEN_ROWS
= 6;
101 // when autosizing the columns, add some slack
102 static const int AUTOSIZE_COL_MARGIN
= 10;
104 // default width for the header columns
105 static const int WIDTH_COL_DEFAULT
= 80;
107 // the space between the image and the text in the report mode
108 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
110 // the space between the image and the text in the report mode in header
111 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
113 // ============================================================================
115 // ============================================================================
117 //-----------------------------------------------------------------------------
118 // wxColWidthInfo (internal)
119 //-----------------------------------------------------------------------------
121 struct wxColWidthInfo
124 bool bNeedsUpdate
; // only set to true when an item whose
125 // width == nMaxWidth is removed
127 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
130 bNeedsUpdate
= needsUpdate
;
134 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
136 //-----------------------------------------------------------------------------
137 // wxListItemData (internal)
138 //-----------------------------------------------------------------------------
143 wxListItemData(wxListMainWindow
*owner
);
146 void SetItem( const wxListItem
&info
);
147 void SetImage( int image
) { m_image
= image
; }
148 void SetData( wxUIntPtr data
) { m_data
= data
; }
149 void SetPosition( int x
, int y
);
150 void SetSize( int width
, int height
);
152 bool HasText() const { return !m_text
.empty(); }
153 const wxString
& GetText() const { return m_text
; }
154 void SetText(const wxString
& text
) { m_text
= text
; }
156 // we can't use empty string for measuring the string width/height, so
157 // always return something
158 wxString
GetTextForMeasuring() const
160 wxString s
= GetText();
167 bool IsHit( int x
, int y
) const;
171 int GetWidth() const;
172 int GetHeight() const;
174 int GetImage() const { return m_image
; }
175 bool HasImage() const { return GetImage() != -1; }
177 void GetItem( wxListItem
&info
) const;
179 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
180 wxListItemAttr
*GetAttr() const { return m_attr
; }
183 // the item image or -1
186 // user data associated with the item
189 // the item coordinates are not used in report mode; instead this pointer is
190 // NULL and the owner window is used to retrieve the item position and size
193 // the list ctrl we are in
194 wxListMainWindow
*m_owner
;
196 // custom attributes or NULL
197 wxListItemAttr
*m_attr
;
200 // common part of all ctors
206 //-----------------------------------------------------------------------------
207 // wxListHeaderData (internal)
208 //-----------------------------------------------------------------------------
210 class wxListHeaderData
: public wxObject
214 wxListHeaderData( const wxListItem
&info
);
215 void SetItem( const wxListItem
&item
);
216 void SetPosition( int x
, int y
);
217 void SetWidth( int w
);
218 void SetState( int state
);
219 void SetFormat( int format
);
220 void SetHeight( int h
);
221 bool HasImage() const;
223 bool HasText() const { return !m_text
.empty(); }
224 const wxString
& GetText() const { return m_text
; }
225 void SetText(const wxString
& text
) { m_text
= text
; }
227 void GetItem( wxListItem
&item
);
229 bool IsHit( int x
, int y
) const;
230 int GetImage() const;
231 int GetWidth() const;
232 int GetFormat() const;
233 int GetState() const;
250 //-----------------------------------------------------------------------------
251 // wxListLineData (internal)
252 //-----------------------------------------------------------------------------
254 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
255 #include "wx/listimpl.cpp"
256 WX_DEFINE_LIST(wxListItemDataList
)
261 // the list of subitems: only may have more than one item in report mode
262 wxListItemDataList m_items
;
264 // this is not used in report view
276 // the part to be highlighted
277 wxRect m_rectHighlight
;
279 // extend all our rects to be centered inside the one of given width
280 void ExtendWidth(wxCoord w
)
282 wxASSERT_MSG( m_rectAll
.width
<= w
,
283 _T("width can only be increased") );
286 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
287 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
288 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
293 // is this item selected? [NB: not used in virtual mode]
296 // back pointer to the list ctrl
297 wxListMainWindow
*m_owner
;
300 wxListLineData(wxListMainWindow
*owner
);
304 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
308 // are we in report mode?
309 inline bool InReportView() const;
311 // are we in virtual report mode?
312 inline bool IsVirtual() const;
314 // these 2 methods shouldn't be called for report view controls, in that
315 // case we determine our position/size ourselves
317 // calculate the size of the line
318 void CalculateSize( wxDC
*dc
, int spacing
);
320 // remember the position this line appears at
321 void SetPosition( int x
, int y
, int spacing
);
325 void SetImage( int image
) { SetImage(0, image
); }
326 int GetImage() const { return GetImage(0); }
327 void SetImage( int index
, int image
);
328 int GetImage( int index
) const;
330 bool HasImage() const { return GetImage() != -1; }
331 bool HasText() const { return !GetText(0).empty(); }
333 void SetItem( int index
, const wxListItem
&info
);
334 void GetItem( int index
, wxListItem
&info
);
336 wxString
GetText(int index
) const;
337 void SetText( int index
, const wxString
& s
);
339 wxListItemAttr
*GetAttr() const;
340 void SetAttr(wxListItemAttr
*attr
);
342 // return true if the highlighting really changed
343 bool Highlight( bool on
);
345 void ReverseHighlight();
347 bool IsHighlighted() const
349 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
351 return m_highlighted
;
354 // draw the line on the given DC in icon/list mode
355 void Draw( wxDC
*dc
);
357 // the same in report mode
358 void DrawInReportMode( wxDC
*dc
,
360 const wxRect
& rectHL
,
364 // set the line to contain num items (only can be > 1 in report mode)
365 void InitItems( int num
);
367 // get the mode (i.e. style) of the list control
368 inline int GetMode() const;
370 // prepare the DC for drawing with these item's attributes, return true if
371 // we need to draw the items background to highlight it, false otherwise
372 bool SetAttributes(wxDC
*dc
,
373 const wxListItemAttr
*attr
,
376 // draw the text on the DC with the correct justification; also add an
377 // ellipsis if the text is too large to fit in the current width
378 void DrawTextFormatted(wxDC
*dc
,
379 const wxString
&text
,
382 int yMid
, // this is middle, not top, of the text
386 WX_DECLARE_OBJARRAY(wxListLineData
, wxListLineDataArray
);
387 #include "wx/arrimpl.cpp"
388 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
390 //-----------------------------------------------------------------------------
391 // wxListHeaderWindow (internal)
392 //-----------------------------------------------------------------------------
394 class wxListHeaderWindow
: public wxWindow
397 wxListMainWindow
*m_owner
;
398 const wxCursor
*m_currentCursor
;
399 wxCursor
*m_resizeCursor
;
402 // column being resized or -1
405 // divider line position in logical (unscrolled) coords
408 // minimal position beyond which the divider line
409 // can't be dragged in logical coords
413 wxListHeaderWindow();
415 wxListHeaderWindow( wxWindow
*win
,
417 wxListMainWindow
*owner
,
418 const wxPoint
&pos
= wxDefaultPosition
,
419 const wxSize
&size
= wxDefaultSize
,
421 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
423 virtual ~wxListHeaderWindow();
426 void AdjustDC( wxDC
& dc
);
428 void OnPaint( wxPaintEvent
&event
);
429 void OnMouse( wxMouseEvent
&event
);
430 void OnSetFocus( wxFocusEvent
&event
);
435 // Update main window's column later
436 bool m_sendSetColumnWidth
;
440 virtual void OnInternalIdle();
443 // common part of all ctors
446 // generate and process the list event of the given type, return true if
447 // it wasn't vetoed, i.e. if we should proceed
448 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
450 DECLARE_EVENT_TABLE()
453 //-----------------------------------------------------------------------------
454 // wxListRenameTimer (internal)
455 //-----------------------------------------------------------------------------
457 class wxListRenameTimer
: public wxTimer
460 wxListMainWindow
*m_owner
;
463 wxListRenameTimer( wxListMainWindow
*owner
);
467 //-----------------------------------------------------------------------------
468 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
469 //-----------------------------------------------------------------------------
471 class wxListTextCtrlWrapper
: public wxEvtHandler
474 // NB: text must be a valid object but not Create()d yet
475 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
479 wxTextCtrl
*GetText() const { return m_text
; }
481 void EndEdit( bool discardChanges
);
484 void OnChar( wxKeyEvent
&event
);
485 void OnKeyUp( wxKeyEvent
&event
);
486 void OnKillFocus( wxFocusEvent
&event
);
488 bool AcceptChanges();
489 void Finish( bool setfocus
);
492 wxListMainWindow
*m_owner
;
494 wxString m_startValue
;
496 bool m_aboutToFinish
;
498 DECLARE_EVENT_TABLE()
501 //-----------------------------------------------------------------------------
502 // wxListMainWindow (internal)
503 //-----------------------------------------------------------------------------
505 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
506 #include "wx/listimpl.cpp"
507 WX_DEFINE_LIST(wxListHeaderDataList
)
509 class wxListMainWindow
: public wxWindow
513 wxListMainWindow( wxWindow
*parent
,
515 const wxPoint
& pos
= wxDefaultPosition
,
516 const wxSize
& size
= wxDefaultSize
,
518 const wxString
&name
= _T("listctrlmainwindow") );
520 virtual ~wxListMainWindow();
522 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
524 // return true if this is a virtual list control
525 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
527 // return true if the control is in report mode
528 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
530 // return true if we are in single selection mode, false if multi sel
531 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
533 // do we have a header window?
534 bool HasHeader() const
535 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
537 void HighlightAll( bool on
);
539 // all these functions only do something if the line is currently visible
541 // change the line "selected" state, return true if it really changed
542 bool HighlightLine( size_t line
, bool highlight
= true);
544 // as HighlightLine() but do it for the range of lines: this is incredibly
545 // more efficient for virtual list controls!
547 // NB: unlike HighlightLine() this one does refresh the lines on screen
548 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
550 // toggle the line state and refresh it
551 void ReverseHighlight( size_t line
)
552 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
554 // return true if the line is highlighted
555 bool IsHighlighted(size_t line
) const;
557 // refresh one or several lines at once
558 void RefreshLine( size_t line
);
559 void RefreshLines( size_t lineFrom
, size_t lineTo
);
561 // refresh all selected items
562 void RefreshSelected();
564 // refresh all lines below the given one: the difference with
565 // RefreshLines() is that the index here might not be a valid one (happens
566 // when the last line is deleted)
567 void RefreshAfter( size_t lineFrom
);
569 // the methods which are forwarded to wxListLineData itself in list/icon
570 // modes but are here because the lines don't store their positions in the
573 // get the bound rect for the entire line
574 wxRect
GetLineRect(size_t line
) const;
576 // get the bound rect of the label
577 wxRect
GetLineLabelRect(size_t line
) const;
579 // get the bound rect of the items icon (only may be called if we do have
581 wxRect
GetLineIconRect(size_t line
) const;
583 // get the rect to be highlighted when the item has focus
584 wxRect
GetLineHighlightRect(size_t line
) const;
586 // get the size of the total line rect
587 wxSize
GetLineSize(size_t line
) const
588 { return GetLineRect(line
).GetSize(); }
590 // return the hit code for the corresponding position (in this line)
591 long HitTestLine(size_t line
, int x
, int y
) const;
593 // bring the selected item into view, scrolling to it if necessary
594 void MoveToItem(size_t item
);
596 bool ScrollList( int WXUNUSED(dx
), int dy
);
598 // bring the current item into view
599 void MoveToFocus() { MoveToItem(m_current
); }
601 // start editing the label of the given item
602 wxTextCtrl
*EditLabel(long item
,
603 wxClassInfo
* textControlClass
= CLASSINFO(wxTextCtrl
));
604 wxTextCtrl
*GetEditControl() const
606 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
609 void ResetTextControl(wxTextCtrl
*text
)
612 m_textctrlWrapper
= NULL
;
615 void OnRenameTimer();
616 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
617 void OnRenameCancelled(size_t itemEdit
);
619 void OnMouse( wxMouseEvent
&event
);
621 // called to switch the selection from the current item to newCurrent,
622 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
624 void OnChar( wxKeyEvent
&event
);
625 void OnKeyDown( wxKeyEvent
&event
);
626 void OnKeyUp( wxKeyEvent
&event
);
627 void OnSetFocus( wxFocusEvent
&event
);
628 void OnKillFocus( wxFocusEvent
&event
);
629 void OnScroll( wxScrollWinEvent
& event
);
631 void OnPaint( wxPaintEvent
&event
);
633 void OnChildFocus(wxChildFocusEvent
& event
);
635 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
636 void GetImageSize( int index
, int &width
, int &height
) const;
637 int GetTextLength( const wxString
&s
) const;
639 void SetImageList( wxImageList
*imageList
, int which
);
640 void SetItemSpacing( int spacing
, bool isSmall
= false );
641 int GetItemSpacing( bool isSmall
= false );
643 void SetColumn( int col
, wxListItem
&item
);
644 void SetColumnWidth( int col
, int width
);
645 void GetColumn( int col
, wxListItem
&item
) const;
646 int GetColumnWidth( int col
) const;
647 int GetColumnCount() const { return m_columns
.GetCount(); }
649 // returns the sum of the heights of all columns
650 int GetHeaderWidth() const;
652 int GetCountPerPage() const;
654 void SetItem( wxListItem
&item
);
655 void GetItem( wxListItem
&item
) const;
656 void SetItemState( long item
, long state
, long stateMask
);
657 void SetItemStateAll( long state
, long stateMask
);
658 int GetItemState( long item
, long stateMask
) const;
659 bool GetItemRect( long item
, wxRect
&rect
) const
661 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
);
663 bool GetSubItemRect( long item
, long subItem
, wxRect
& rect
) const;
664 wxRect
GetViewRect() const;
665 bool GetItemPosition( long item
, wxPoint
& pos
) const;
666 int GetSelectedItemCount() const;
668 wxString
GetItemText(long item
) const
671 info
.m_mask
= wxLIST_MASK_TEXT
;
672 info
.m_itemId
= item
;
677 void SetItemText(long item
, const wxString
& value
)
680 info
.m_mask
= wxLIST_MASK_TEXT
;
681 info
.m_itemId
= item
;
686 // set the scrollbars and update the positions of the items
687 void RecalculatePositions(bool noRefresh
= false);
689 // refresh the window and the header
692 long GetNextItem( long item
, int geometry
, int state
) const;
693 void DeleteItem( long index
);
694 void DeleteAllItems();
695 void DeleteColumn( int col
);
696 void DeleteEverything();
697 void EnsureVisible( long index
);
698 long FindItem( long start
, const wxString
& str
, bool partial
= false );
699 long FindItem( long start
, wxUIntPtr data
);
700 long FindItem( const wxPoint
& pt
);
701 long HitTest( int x
, int y
, int &flags
) const;
702 void InsertItem( wxListItem
&item
);
703 void InsertColumn( long col
, wxListItem
&item
);
704 int GetItemWidthWithImage(wxListItem
* item
);
705 void SortItems( wxListCtrlCompare fn
, long data
);
707 size_t GetItemCount() const;
708 bool IsEmpty() const { return GetItemCount() == 0; }
709 void SetItemCount(long count
);
711 // change the current (== focused) item, send a notification event
712 void ChangeCurrent(size_t current
);
713 void ResetCurrent() { ChangeCurrent((size_t)-1); }
714 bool HasCurrent() const { return m_current
!= (size_t)-1; }
716 // send out a wxListEvent
717 void SendNotify( size_t line
,
719 const wxPoint
& point
= wxDefaultPosition
);
721 // override base class virtual to reset m_lineHeight when the font changes
722 virtual bool SetFont(const wxFont
& font
)
724 if ( !wxWindow::SetFont(font
) )
732 // these are for wxListLineData usage only
734 // get the backpointer to the list ctrl
735 wxGenericListCtrl
*GetListCtrl() const
737 return wxStaticCast(GetParent(), wxGenericListCtrl
);
740 // get the height of all lines (assuming they all do have the same height)
741 wxCoord
GetLineHeight() const;
743 // get the y position of the given line (only for report view)
744 wxCoord
GetLineY(size_t line
) const;
746 // get the brush to use for the item highlighting
747 wxBrush
*GetHighlightBrush() const
749 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
752 bool HasFocus() const
758 // the array of all line objects for a non virtual list control (for the
759 // virtual list control we only ever use m_lines[0])
760 wxListLineDataArray m_lines
;
762 // the list of column objects
763 wxListHeaderDataList m_columns
;
765 // currently focused item or -1
768 // the number of lines per page
771 // this flag is set when something which should result in the window
772 // redrawing happens (i.e. an item was added or deleted, or its appearance
773 // changed) and OnPaint() doesn't redraw the window while it is set which
774 // allows to minimize the number of repaintings when a lot of items are
775 // being added. The real repainting occurs only after the next OnIdle()
779 wxColour
*m_highlightColour
;
780 wxImageList
*m_small_image_list
;
781 wxImageList
*m_normal_image_list
;
783 int m_normal_spacing
;
787 wxTimer
*m_renameTimer
;
791 ColWidthArray m_aColWidths
;
793 // for double click logic
794 size_t m_lineLastClicked
,
795 m_lineBeforeLastClicked
,
796 m_lineSelectSingleOnUp
;
799 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
801 // the total count of items in a virtual list control
804 // the object maintaining the items selection state, only used in virtual
806 wxSelectionStore m_selStore
;
808 // common part of all ctors
811 // get the line data for the given index
812 wxListLineData
*GetLine(size_t n
) const
814 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
818 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
825 // get a dummy line which can be used for geometry calculations and such:
826 // you must use GetLine() if you want to really draw the line
827 wxListLineData
*GetDummyLine() const;
829 // cache the line data of the n-th line in m_lines[0]
830 void CacheLineData(size_t line
);
832 // get the range of visible lines
833 void GetVisibleLinesRange(size_t *from
, size_t *to
);
835 // force us to recalculate the range of visible lines
836 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
838 // get the colour to be used for drawing the rules
839 wxColour
GetRuleColour() const
841 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
845 // initialize the current item if needed
846 void UpdateCurrent();
848 // delete all items but don't refresh: called from dtor
849 void DoDeleteAllItems();
851 // the height of one line using the current font
852 wxCoord m_lineHeight
;
854 // the total header width or 0 if not calculated yet
855 wxCoord m_headerWidth
;
857 // the first and last lines being shown on screen right now (inclusive),
858 // both may be -1 if they must be calculated so never access them directly:
859 // use GetVisibleLinesRange() above instead
863 // the brushes to use for item highlighting when we do/don't have focus
864 wxBrush
*m_highlightBrush
,
865 *m_highlightUnfocusedBrush
;
867 // wrapper around the text control currently used for in place editing or
868 // NULL if no item is being edited
869 wxListTextCtrlWrapper
*m_textctrlWrapper
;
872 DECLARE_EVENT_TABLE()
874 friend class wxGenericListCtrl
;
878 wxListItemData::~wxListItemData()
880 // in the virtual list control the attributes are managed by the main
881 // program, so don't delete them
882 if ( !m_owner
->IsVirtual() )
888 void wxListItemData::Init()
896 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
902 if ( owner
->InReportView() )
908 void wxListItemData::SetItem( const wxListItem
&info
)
910 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
911 SetText(info
.m_text
);
912 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
913 m_image
= info
.m_image
;
914 if ( info
.m_mask
& wxLIST_MASK_DATA
)
915 m_data
= info
.m_data
;
917 if ( info
.HasAttributes() )
920 m_attr
->AssignFrom(*info
.GetAttributes());
922 m_attr
= new wxListItemAttr(*info
.GetAttributes());
930 m_rect
->width
= info
.m_width
;
934 void wxListItemData::SetPosition( int x
, int y
)
936 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
942 void wxListItemData::SetSize( int width
, int height
)
944 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
947 m_rect
->width
= width
;
949 m_rect
->height
= height
;
952 bool wxListItemData::IsHit( int x
, int y
) const
954 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
956 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
959 int wxListItemData::GetX() const
961 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
966 int wxListItemData::GetY() const
968 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
973 int wxListItemData::GetWidth() const
975 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
977 return m_rect
->width
;
980 int wxListItemData::GetHeight() const
982 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
984 return m_rect
->height
;
987 void wxListItemData::GetItem( wxListItem
&info
) const
989 long mask
= info
.m_mask
;
991 // by default, get everything for backwards compatibility
994 if ( mask
& wxLIST_MASK_TEXT
)
995 info
.m_text
= m_text
;
996 if ( mask
& wxLIST_MASK_IMAGE
)
997 info
.m_image
= m_image
;
998 if ( mask
& wxLIST_MASK_DATA
)
999 info
.m_data
= m_data
;
1003 if ( m_attr
->HasTextColour() )
1004 info
.SetTextColour(m_attr
->GetTextColour());
1005 if ( m_attr
->HasBackgroundColour() )
1006 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1007 if ( m_attr
->HasFont() )
1008 info
.SetFont(m_attr
->GetFont());
1012 //-----------------------------------------------------------------------------
1014 //-----------------------------------------------------------------------------
1016 void wxListHeaderData::Init()
1028 wxListHeaderData::wxListHeaderData()
1033 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1040 void wxListHeaderData::SetItem( const wxListItem
&item
)
1042 m_mask
= item
.m_mask
;
1044 if ( m_mask
& wxLIST_MASK_TEXT
)
1045 m_text
= item
.m_text
;
1047 if ( m_mask
& wxLIST_MASK_IMAGE
)
1048 m_image
= item
.m_image
;
1050 if ( m_mask
& wxLIST_MASK_FORMAT
)
1051 m_format
= item
.m_format
;
1053 if ( m_mask
& wxLIST_MASK_WIDTH
)
1054 SetWidth(item
.m_width
);
1056 if ( m_mask
& wxLIST_MASK_STATE
)
1057 SetState(item
.m_state
);
1060 void wxListHeaderData::SetPosition( int x
, int y
)
1066 void wxListHeaderData::SetHeight( int h
)
1071 void wxListHeaderData::SetWidth( int w
)
1073 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1076 void wxListHeaderData::SetState( int flag
)
1081 void wxListHeaderData::SetFormat( int format
)
1086 bool wxListHeaderData::HasImage() const
1088 return m_image
!= -1;
1091 bool wxListHeaderData::IsHit( int x
, int y
) const
1093 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1096 void wxListHeaderData::GetItem( wxListItem
& item
)
1098 item
.m_mask
= m_mask
;
1099 item
.m_text
= m_text
;
1100 item
.m_image
= m_image
;
1101 item
.m_format
= m_format
;
1102 item
.m_width
= m_width
;
1103 item
.m_state
= m_state
;
1106 int wxListHeaderData::GetImage() const
1111 int wxListHeaderData::GetWidth() const
1116 int wxListHeaderData::GetFormat() const
1121 int wxListHeaderData::GetState() const
1126 //-----------------------------------------------------------------------------
1128 //-----------------------------------------------------------------------------
1130 inline int wxListLineData::GetMode() const
1132 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1135 inline bool wxListLineData::InReportView() const
1137 return m_owner
->HasFlag(wxLC_REPORT
);
1140 inline bool wxListLineData::IsVirtual() const
1142 return m_owner
->IsVirtual();
1145 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1149 if ( InReportView() )
1152 m_gi
= new GeometryInfo
;
1154 m_highlighted
= false;
1156 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1159 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1161 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1162 wxCHECK_RET( node
, _T("no subitems at all??") );
1164 wxListItemData
*item
= node
->GetData();
1169 switch ( GetMode() )
1172 case wxLC_SMALL_ICON
:
1173 m_gi
->m_rectAll
.width
= spacing
;
1175 s
= item
->GetText();
1180 m_gi
->m_rectLabel
.width
=
1181 m_gi
->m_rectLabel
.height
= 0;
1185 dc
->GetTextExtent( s
, &lw
, &lh
);
1189 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1191 m_gi
->m_rectAll
.width
= lw
;
1193 m_gi
->m_rectLabel
.width
= lw
;
1194 m_gi
->m_rectLabel
.height
= lh
;
1197 if (item
->HasImage())
1200 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1201 m_gi
->m_rectIcon
.width
= w
+ 8;
1202 m_gi
->m_rectIcon
.height
= h
+ 8;
1204 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1205 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1206 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1207 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1210 if ( item
->HasText() )
1212 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1213 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1215 else // no text, highlight the icon
1217 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1218 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1223 s
= item
->GetTextForMeasuring();
1225 dc
->GetTextExtent( s
, &lw
, &lh
);
1229 m_gi
->m_rectLabel
.width
= lw
;
1230 m_gi
->m_rectLabel
.height
= lh
;
1232 m_gi
->m_rectAll
.width
= lw
;
1233 m_gi
->m_rectAll
.height
= lh
;
1235 if (item
->HasImage())
1238 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1239 m_gi
->m_rectIcon
.width
= w
;
1240 m_gi
->m_rectIcon
.height
= h
;
1242 m_gi
->m_rectAll
.width
+= 4 + w
;
1243 if (h
> m_gi
->m_rectAll
.height
)
1244 m_gi
->m_rectAll
.height
= h
;
1247 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1248 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1252 wxFAIL_MSG( _T("unexpected call to SetSize") );
1256 wxFAIL_MSG( _T("unknown mode") );
1261 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1263 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1264 wxCHECK_RET( node
, _T("no subitems at all??") );
1266 wxListItemData
*item
= node
->GetData();
1268 switch ( GetMode() )
1271 case wxLC_SMALL_ICON
:
1272 m_gi
->m_rectAll
.x
= x
;
1273 m_gi
->m_rectAll
.y
= y
;
1275 if ( item
->HasImage() )
1277 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1278 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1279 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1282 if ( item
->HasText() )
1284 if (m_gi
->m_rectAll
.width
> spacing
)
1285 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1287 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1288 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1289 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1290 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1292 else // no text, highlight the icon
1294 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1295 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1300 m_gi
->m_rectAll
.x
= x
;
1301 m_gi
->m_rectAll
.y
= y
;
1303 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1304 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1305 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1307 if (item
->HasImage())
1309 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1310 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1311 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1315 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1320 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1324 wxFAIL_MSG( _T("unknown mode") );
1329 void wxListLineData::InitItems( int num
)
1331 for (int i
= 0; i
< num
; i
++)
1332 m_items
.Append( new wxListItemData(m_owner
) );
1335 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1337 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1338 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1340 wxListItemData
*item
= node
->GetData();
1341 item
->SetItem( info
);
1344 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1346 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1349 wxListItemData
*item
= node
->GetData();
1350 item
->GetItem( info
);
1354 wxString
wxListLineData::GetText(int index
) const
1358 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1361 wxListItemData
*item
= node
->GetData();
1362 s
= item
->GetText();
1368 void wxListLineData::SetText( int index
, const wxString
& s
)
1370 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1373 wxListItemData
*item
= node
->GetData();
1378 void wxListLineData::SetImage( int index
, int image
)
1380 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1381 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1383 wxListItemData
*item
= node
->GetData();
1384 item
->SetImage(image
);
1387 int wxListLineData::GetImage( int index
) const
1389 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1390 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1392 wxListItemData
*item
= node
->GetData();
1393 return item
->GetImage();
1396 wxListItemAttr
*wxListLineData::GetAttr() const
1398 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1399 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1401 wxListItemData
*item
= node
->GetData();
1402 return item
->GetAttr();
1405 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1407 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1408 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1410 wxListItemData
*item
= node
->GetData();
1411 item
->SetAttr(attr
);
1414 bool wxListLineData::SetAttributes(wxDC
*dc
,
1415 const wxListItemAttr
*attr
,
1418 wxWindow
*listctrl
= m_owner
->GetParent();
1422 // don't use foreground colour for drawing highlighted items - this might
1423 // make them completely invisible (and there is no way to do bit
1424 // arithmetics on wxColour, unfortunately)
1429 if (m_owner
->HasFocus()
1430 #if !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
1431 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1439 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1441 else if ( attr
&& attr
->HasTextColour() )
1442 colText
= attr
->GetTextColour();
1444 colText
= listctrl
->GetForegroundColour();
1446 dc
->SetTextForeground(colText
);
1450 if ( attr
&& attr
->HasFont() )
1451 font
= attr
->GetFont();
1453 font
= listctrl
->GetFont();
1458 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1459 if ( highlighted
|| hasBgCol
)
1462 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1464 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
1466 dc
->SetPen( *wxTRANSPARENT_PEN
);
1474 void wxListLineData::Draw( wxDC
*dc
)
1476 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1477 wxCHECK_RET( node
, _T("no subitems at all??") );
1479 bool highlighted
= IsHighlighted();
1481 wxListItemAttr
*attr
= GetAttr();
1483 if ( SetAttributes(dc
, attr
, highlighted
) )
1484 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1486 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1492 int flags
= wxCONTROL_SELECTED
;
1493 if (m_owner
->HasFocus()
1494 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
1495 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1498 flags
|= wxCONTROL_FOCUSED
;
1499 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1504 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1509 // just for debugging to better see where the items are
1511 dc
->SetPen(*wxRED_PEN
);
1512 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1513 dc
->DrawRectangle( m_gi
->m_rectAll
);
1514 dc
->SetPen(*wxGREEN_PEN
);
1515 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1518 wxListItemData
*item
= node
->GetData();
1519 if (item
->HasImage())
1521 // centre the image inside our rectangle, this looks nicer when items
1522 // ae aligned in a row
1523 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1525 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1528 if (item
->HasText())
1530 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1532 wxDCClipper
clipper(*dc
, rectLabel
);
1533 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1537 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1539 const wxRect
& rectHL
,
1542 // TODO: later we should support setting different attributes for
1543 // different columns - to do it, just add "col" argument to
1544 // GetAttr() and move these lines into the loop below
1545 wxListItemAttr
*attr
= GetAttr();
1546 if ( SetAttributes(dc
, attr
, highlighted
) )
1547 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1549 dc
->DrawRectangle( rectHL
);
1555 int flags
= wxCONTROL_SELECTED
;
1556 if (m_owner
->HasFocus())
1557 flags
|= wxCONTROL_FOCUSED
;
1558 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1562 dc
->DrawRectangle( rectHL
);
1567 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1568 yMid
= rect
.y
+ rect
.height
/2;
1570 // This probably needs to be done
1571 // on all platforms as the icons
1572 // otherwise nearly touch the border
1577 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1579 node
= node
->GetNext(), col
++ )
1581 wxListItemData
*item
= node
->GetData();
1583 int width
= m_owner
->GetColumnWidth(col
);
1587 const int wText
= width
- 8;
1588 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
1590 if ( item
->HasImage() )
1593 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1594 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1596 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1602 if ( item
->HasText() )
1603 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, wText
);
1607 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1608 const wxString
& textOrig
,
1614 // we don't support displaying multiple lines currently (and neither does
1615 // wxMSW FWIW) so just merge all the lines
1616 wxString
text(textOrig
);
1617 text
.Replace(_T("\n"), _T(" "));
1620 dc
->GetTextExtent(text
, &w
, &h
);
1622 const wxCoord y
= yMid
- (h
+ 1)/2;
1624 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1626 // determine if the string can fit inside the current width
1629 // it can, draw it using the items alignment
1631 m_owner
->GetColumn(col
, item
);
1632 switch ( item
.GetAlign() )
1634 case wxLIST_FORMAT_LEFT
:
1638 case wxLIST_FORMAT_RIGHT
:
1642 case wxLIST_FORMAT_CENTER
:
1643 x
+= (width
- w
) / 2;
1647 wxFAIL_MSG( _T("unknown list item format") );
1651 dc
->DrawText(text
, x
, y
);
1653 else // otherwise, truncate and add an ellipsis if possible
1655 // determine the base width
1656 wxString
ellipsis(wxT("..."));
1658 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1660 // continue until we have enough space or only one character left
1662 size_t len
= text
.length();
1663 wxString drawntext
= text
.Left(len
);
1666 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1667 drawntext
.RemoveLast();
1670 if (w
+ base_w
<= width
)
1674 // if still not enough space, remove ellipsis characters
1675 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1677 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1678 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1681 // now draw the text
1682 dc
->DrawText(drawntext
, x
, y
);
1683 dc
->DrawText(ellipsis
, x
+ w
, y
);
1687 bool wxListLineData::Highlight( bool on
)
1689 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1691 if ( on
== m_highlighted
)
1699 void wxListLineData::ReverseHighlight( void )
1701 Highlight(!IsHighlighted());
1704 //-----------------------------------------------------------------------------
1705 // wxListHeaderWindow
1706 //-----------------------------------------------------------------------------
1708 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1709 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1710 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1711 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1714 void wxListHeaderWindow::Init()
1716 m_currentCursor
= NULL
;
1717 m_isDragging
= false;
1719 m_sendSetColumnWidth
= false;
1722 wxListHeaderWindow::wxListHeaderWindow()
1727 m_resizeCursor
= NULL
;
1730 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1732 wxListMainWindow
*owner
,
1736 const wxString
&name
)
1737 : wxWindow( win
, id
, pos
, size
, style
, name
)
1742 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1745 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1746 SetOwnForegroundColour( attr
.colFg
);
1747 SetOwnBackgroundColour( attr
.colBg
);
1749 SetOwnFont( attr
.font
);
1751 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1752 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1754 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1758 wxListHeaderWindow::~wxListHeaderWindow()
1760 delete m_resizeCursor
;
1763 #ifdef __WXUNIVERSAL__
1764 #include "wx/univ/renderer.h"
1765 #include "wx/univ/theme.h"
1768 // shift the DC origin to match the position of the main window horz
1769 // scrollbar: this allows us to always use logical coords
1770 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1772 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1775 parent
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1778 parent
->GetViewStart( &view_start
, NULL
);
1783 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1785 // account for the horz scrollbar offset
1787 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1789 // Maybe we just have to check for m_signX
1790 // in the DC, but I leave the #ifdef __WXGTK__
1792 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1796 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1799 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1801 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1803 wxPaintDC
dc( this );
1807 dc
.SetFont( GetFont() );
1809 // width and height of the entire header window
1811 GetClientSize( &w
, &h
);
1812 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1814 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1815 dc
.SetTextForeground(GetForegroundColour());
1817 int x
= HEADER_OFFSET_X
;
1818 int numColumns
= m_owner
->GetColumnCount();
1820 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1822 m_owner
->GetColumn( i
, item
);
1823 int wCol
= item
.m_width
;
1829 if (!m_parent
->IsEnabled())
1830 flags
|= wxCONTROL_DISABLED
;
1832 // NB: The code below is not really Mac-specific, but since we are close
1833 // to 2.8 release and I don't have time to test on other platforms, I
1834 // defined this only for wxMac. If this behavior is desired on
1835 // other platforms, please go ahead and revise or remove the #ifdef.
1837 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1838 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1839 flags
|= wxCONTROL_SELECTED
;
1843 flags
|= wxCONTROL_SPECIAL
; // mark as first column
1845 wxRendererNative::Get().DrawHeaderButton
1849 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1853 // see if we have enough space for the column label
1855 // for this we need the width of the text
1858 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1859 wLabel
+= 2 * EXTRA_WIDTH
;
1861 // and the width of the icon, if any
1862 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1863 const int image
= item
.m_image
;
1864 wxImageList
*imageList
;
1867 imageList
= m_owner
->m_small_image_list
;
1870 imageList
->GetSize(image
, ix
, iy
);
1871 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1879 // ignore alignment if there is not enough space anyhow
1881 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1884 wxFAIL_MSG( _T("unknown list item format") );
1887 case wxLIST_FORMAT_LEFT
:
1891 case wxLIST_FORMAT_RIGHT
:
1892 xAligned
= x
+ cw
- wLabel
;
1895 case wxLIST_FORMAT_CENTER
:
1896 xAligned
= x
+ (cw
- wLabel
) / 2;
1900 // draw the text and image clipping them so that they
1901 // don't overwrite the column boundary
1902 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1904 // if we have an image, draw it on the right of the label
1911 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1912 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1913 wxIMAGELIST_DRAW_TRANSPARENT
1917 dc
.DrawText( item
.GetText(),
1918 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1923 // Fill in what's missing to the right of the columns, otherwise we will
1924 // leave an unpainted area when columns are removed (and it looks better)
1927 wxRendererNative::Get().DrawHeaderButton
1931 wxRect(x
, HEADER_OFFSET_Y
, w
- x
, h
),
1932 wxCONTROL_DIRTY
// mark as last column
1937 void wxListHeaderWindow::OnInternalIdle()
1939 wxWindow::OnInternalIdle();
1941 if (m_sendSetColumnWidth
)
1943 m_owner
->SetColumnWidth( m_colToSend
, m_widthToSend
);
1944 m_sendSetColumnWidth
= false;
1948 void wxListHeaderWindow::DrawCurrent()
1951 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1952 m_sendSetColumnWidth
= true;
1953 m_colToSend
= m_column
;
1954 m_widthToSend
= m_currentX
- m_minX
;
1956 int x1
= m_currentX
;
1958 m_owner
->ClientToScreen( &x1
, &y1
);
1960 int x2
= m_currentX
;
1962 m_owner
->GetClientSize( NULL
, &y2
);
1963 m_owner
->ClientToScreen( &x2
, &y2
);
1966 dc
.SetLogicalFunction( wxINVERT
);
1967 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1968 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1972 dc
.DrawLine( x1
, y1
, x2
, y2
);
1974 dc
.SetLogicalFunction( wxCOPY
);
1976 dc
.SetPen( wxNullPen
);
1977 dc
.SetBrush( wxNullBrush
);
1981 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1983 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1985 // we want to work with logical coords
1987 parent
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1988 int y
= event
.GetY();
1992 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1994 // we don't draw the line beyond our window, but we allow dragging it
1997 GetClientSize( &w
, NULL
);
1998 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
2001 // erase the line if it was drawn
2002 if ( m_currentX
< w
)
2005 if (event
.ButtonUp())
2008 m_isDragging
= false;
2010 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
2011 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
2018 m_currentX
= m_minX
+ 7;
2020 // draw in the new location
2021 if ( m_currentX
< w
)
2025 else // not dragging
2028 bool hit_border
= false;
2030 // end of the current column
2033 // find the column where this event occurred
2035 countCol
= m_owner
->GetColumnCount();
2036 for (col
= 0; col
< countCol
; col
++)
2038 xpos
+= m_owner
->GetColumnWidth( col
);
2041 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2043 // near the column border
2050 // inside the column
2057 if ( col
== countCol
)
2060 if (event
.LeftDown() || event
.RightUp())
2062 if (hit_border
&& event
.LeftDown())
2064 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2065 event
.GetPosition()) )
2067 m_isDragging
= true;
2072 //else: column resizing was vetoed by the user code
2074 else // click on a column
2076 // record the selected state of the columns
2077 if (event
.LeftDown())
2079 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2082 m_owner
->GetColumn(i
, colItem
);
2083 long state
= colItem
.GetState();
2085 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2087 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2088 m_owner
->SetColumn(i
, colItem
);
2092 SendListEvent( event
.LeftDown()
2093 ? wxEVT_COMMAND_LIST_COL_CLICK
2094 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2095 event
.GetPosition());
2098 else if (event
.Moving())
2103 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2104 m_currentCursor
= m_resizeCursor
;
2108 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2109 m_currentCursor
= wxSTANDARD_CURSOR
;
2113 SetCursor(*m_currentCursor
);
2118 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2120 m_owner
->SetFocus();
2124 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2126 wxWindow
*parent
= GetParent();
2127 wxListEvent
le( type
, parent
->GetId() );
2128 le
.SetEventObject( parent
);
2129 le
.m_pointDrag
= pos
;
2131 // the position should be relative to the parent window, not
2132 // this one for compatibility with MSW and common sense: the
2133 // user code doesn't know anything at all about this header
2134 // window, so why should it get positions relative to it?
2135 le
.m_pointDrag
.y
-= GetSize().y
;
2137 le
.m_col
= m_column
;
2138 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2141 //-----------------------------------------------------------------------------
2142 // wxListRenameTimer (internal)
2143 //-----------------------------------------------------------------------------
2145 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2150 void wxListRenameTimer::Notify()
2152 m_owner
->OnRenameTimer();
2155 //-----------------------------------------------------------------------------
2156 // wxListTextCtrlWrapper (internal)
2157 //-----------------------------------------------------------------------------
2159 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2160 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2161 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2162 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2165 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2168 : m_startValue(owner
->GetItemText(itemEdit
)),
2169 m_itemEdited(itemEdit
)
2173 m_aboutToFinish
= false;
2175 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
2177 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2179 parent
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2180 &rectLabel
.x
, &rectLabel
.y
);
2182 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2183 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2184 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2187 m_text
->PushEventHandler(this);
2190 void wxListTextCtrlWrapper::EndEdit(bool discardChanges
)
2192 m_aboutToFinish
= true;
2194 if ( discardChanges
)
2196 m_owner
->OnRenameCancelled(m_itemEdited
);
2202 // Notify the owner about the changes
2205 // Even if vetoed, close the control (consistent with MSW)
2210 void wxListTextCtrlWrapper::Finish( bool setfocus
)
2212 m_text
->RemoveEventHandler(this);
2213 m_owner
->ResetTextControl( m_text
);
2215 wxPendingDelete
.Append( this );
2218 m_owner
->SetFocus();
2221 bool wxListTextCtrlWrapper::AcceptChanges()
2223 const wxString value
= m_text
->GetValue();
2225 // notice that we should always call OnRenameAccept() to generate the "end
2226 // label editing" event, even if the user hasn't really changed anything
2227 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2229 // vetoed by the user
2233 // accepted, do rename the item (unless nothing changed)
2234 if ( value
!= m_startValue
)
2235 m_owner
->SetItemText(m_itemEdited
, value
);
2240 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2242 switch ( event
.m_keyCode
)
2257 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2259 if (m_aboutToFinish
)
2261 // auto-grow the textctrl:
2262 wxSize parentSize
= m_owner
->GetSize();
2263 wxPoint myPos
= m_text
->GetPosition();
2264 wxSize mySize
= m_text
->GetSize();
2266 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2267 if (myPos
.x
+ sx
> parentSize
.x
)
2268 sx
= parentSize
.x
- myPos
.x
;
2271 m_text
->SetSize(sx
, wxDefaultCoord
);
2277 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2279 if ( !m_aboutToFinish
)
2281 if ( !AcceptChanges() )
2282 m_owner
->OnRenameCancelled( m_itemEdited
);
2287 // We must let the native text control handle focus
2291 //-----------------------------------------------------------------------------
2293 //-----------------------------------------------------------------------------
2295 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledCanvas
)
2296 EVT_PAINT (wxListMainWindow::OnPaint
)
2297 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2298 EVT_CHAR (wxListMainWindow::OnChar
)
2299 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2300 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
2301 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2302 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2303 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2304 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus
)
2307 void wxListMainWindow::Init()
2312 m_lineTo
= (size_t)-1;
2318 m_small_image_list
= NULL
;
2319 m_normal_image_list
= NULL
;
2321 m_small_spacing
= 30;
2322 m_normal_spacing
= 40;
2326 m_isCreated
= false;
2328 m_lastOnSame
= false;
2329 m_renameTimer
= new wxListRenameTimer( this );
2330 m_textctrlWrapper
= NULL
;
2334 m_lineSelectSingleOnUp
=
2335 m_lineBeforeLastClicked
= (size_t)-1;
2338 wxListMainWindow::wxListMainWindow()
2343 m_highlightUnfocusedBrush
= NULL
;
2346 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2351 const wxString
&name
)
2352 : wxWindow( parent
, id
, pos
, size
, style
, name
)
2356 m_highlightBrush
= new wxBrush
2358 wxSystemSettings::GetColour
2360 wxSYS_COLOUR_HIGHLIGHT
2365 m_highlightUnfocusedBrush
= new wxBrush
2367 wxSystemSettings::GetColour
2369 wxSYS_COLOUR_BTNSHADOW
2374 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2375 SetOwnForegroundColour( attr
.colFg
);
2376 SetOwnBackgroundColour( attr
.colBg
);
2378 SetOwnFont( attr
.font
);
2381 wxListMainWindow::~wxListMainWindow()
2384 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2385 WX_CLEAR_ARRAY(m_aColWidths
);
2387 delete m_highlightBrush
;
2388 delete m_highlightUnfocusedBrush
;
2389 delete m_renameTimer
;
2392 void wxListMainWindow::CacheLineData(size_t line
)
2394 wxGenericListCtrl
*listctrl
= GetListCtrl();
2396 wxListLineData
*ld
= GetDummyLine();
2398 size_t countCol
= GetColumnCount();
2399 for ( size_t col
= 0; col
< countCol
; col
++ )
2401 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2402 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2405 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2408 wxListLineData
*wxListMainWindow::GetDummyLine() const
2410 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2411 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2413 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2415 // we need to recreate the dummy line if the number of columns in the
2416 // control changed as it would have the incorrect number of fields
2418 if ( !m_lines
.IsEmpty() &&
2419 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2421 self
->m_lines
.Clear();
2424 if ( m_lines
.IsEmpty() )
2426 wxListLineData
*line
= new wxListLineData(self
);
2427 self
->m_lines
.Add(line
);
2429 // don't waste extra memory -- there never going to be anything
2430 // else/more in this array
2431 self
->m_lines
.Shrink();
2437 // ----------------------------------------------------------------------------
2438 // line geometry (report mode only)
2439 // ----------------------------------------------------------------------------
2441 wxCoord
wxListMainWindow::GetLineHeight() const
2443 // we cache the line height as calling GetTextExtent() is slow
2444 if ( !m_lineHeight
)
2446 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2448 wxClientDC
dc( self
);
2449 dc
.SetFont( GetFont() );
2452 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2454 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2457 m_small_image_list
->GetSize(0, iw
, ih
);
2462 self
->m_lineHeight
= y
+ LINE_SPACING
;
2465 return m_lineHeight
;
2468 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2470 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2472 return LINE_SPACING
+ line
* GetLineHeight();
2475 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2477 if ( !InReportView() )
2478 return GetLine(line
)->m_gi
->m_rectAll
;
2481 rect
.x
= HEADER_OFFSET_X
;
2482 rect
.y
= GetLineY(line
);
2483 rect
.width
= GetHeaderWidth();
2484 rect
.height
= GetLineHeight();
2489 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2491 if ( !InReportView() )
2492 return GetLine(line
)->m_gi
->m_rectLabel
;
2495 wxListLineData
*data
= GetLine(line
);
2496 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
2499 wxListItemData
*item
= node
->GetData();
2500 if ( item
->HasImage() )
2503 GetImageSize( item
->GetImage(), ix
, iy
);
2504 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
2509 rect
.x
= image_x
+ HEADER_OFFSET_X
;
2510 rect
.y
= GetLineY(line
);
2511 rect
.width
= GetColumnWidth(0) - image_x
;
2512 rect
.height
= GetLineHeight();
2517 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2519 if ( !InReportView() )
2520 return GetLine(line
)->m_gi
->m_rectIcon
;
2522 wxListLineData
*ld
= GetLine(line
);
2523 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2526 rect
.x
= HEADER_OFFSET_X
;
2527 rect
.y
= GetLineY(line
);
2528 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2533 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2535 return InReportView() ? GetLineRect(line
)
2536 : GetLine(line
)->m_gi
->m_rectHighlight
;
2539 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2541 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2543 wxListLineData
*ld
= GetLine(line
);
2545 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2546 return wxLIST_HITTEST_ONITEMICON
;
2548 // VS: Testing for "ld->HasText() || InReportView()" instead of
2549 // "ld->HasText()" is needed to make empty lines in report view
2551 if ( ld
->HasText() || InReportView() )
2553 wxRect rect
= InReportView() ? GetLineRect(line
)
2554 : GetLineLabelRect(line
);
2556 if ( rect
.Contains(x
, y
) )
2557 return wxLIST_HITTEST_ONITEMLABEL
;
2563 // ----------------------------------------------------------------------------
2564 // highlight (selection) handling
2565 // ----------------------------------------------------------------------------
2567 bool wxListMainWindow::IsHighlighted(size_t line
) const
2571 return m_selStore
.IsSelected(line
);
2575 wxListLineData
*ld
= GetLine(line
);
2576 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2578 return ld
->IsHighlighted();
2582 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2588 wxArrayInt linesChanged
;
2589 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2592 // meny items changed state, refresh everything
2593 RefreshLines(lineFrom
, lineTo
);
2595 else // only a few items changed state, refresh only them
2597 size_t count
= linesChanged
.GetCount();
2598 for ( size_t n
= 0; n
< count
; n
++ )
2600 RefreshLine(linesChanged
[n
]);
2604 else // iterate over all items in non report view
2606 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2608 if ( HighlightLine(line
, highlight
) )
2614 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2620 changed
= m_selStore
.SelectItem(line
, highlight
);
2624 wxListLineData
*ld
= GetLine(line
);
2625 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2627 changed
= ld
->Highlight(highlight
);
2632 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2633 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2639 void wxListMainWindow::RefreshLine( size_t line
)
2641 if ( InReportView() )
2643 size_t visibleFrom
, visibleTo
;
2644 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2646 if ( line
< visibleFrom
|| line
> visibleTo
)
2650 wxRect rect
= GetLineRect(line
);
2652 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2653 RefreshRect( rect
);
2656 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2658 // we suppose that they are ordered by caller
2659 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2661 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2663 if ( InReportView() )
2665 size_t visibleFrom
, visibleTo
;
2666 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2668 if ( lineFrom
< visibleFrom
)
2669 lineFrom
= visibleFrom
;
2670 if ( lineTo
> visibleTo
)
2675 rect
.y
= GetLineY(lineFrom
);
2676 rect
.width
= GetClientSize().x
;
2677 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2679 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2680 RefreshRect( rect
);
2684 // TODO: this should be optimized...
2685 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2692 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2694 if ( InReportView() )
2696 size_t visibleFrom
, visibleTo
;
2697 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2699 if ( lineFrom
< visibleFrom
)
2700 lineFrom
= visibleFrom
;
2701 else if ( lineFrom
> visibleTo
)
2706 rect
.y
= GetLineY(lineFrom
);
2707 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2709 wxSize size
= GetClientSize();
2710 rect
.width
= size
.x
;
2712 // refresh till the bottom of the window
2713 rect
.height
= size
.y
- rect
.y
;
2715 RefreshRect( rect
);
2719 // TODO: how to do it more efficiently?
2724 void wxListMainWindow::RefreshSelected()
2730 if ( InReportView() )
2732 GetVisibleLinesRange(&from
, &to
);
2737 to
= GetItemCount() - 1;
2740 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2741 RefreshLine(m_current
);
2743 for ( size_t line
= from
; line
<= to
; line
++ )
2745 // NB: the test works as expected even if m_current == -1
2746 if ( line
!= m_current
&& IsHighlighted(line
) )
2751 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2753 // Note: a wxPaintDC must be constructed even if no drawing is
2754 // done (a Windows requirement).
2755 wxPaintDC
dc( this );
2759 // nothing to draw or not the moment to draw it
2764 RecalculatePositions( false );
2766 GetListCtrl()->PrepareDC( dc
);
2769 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2771 dc
.SetFont( GetFont() );
2773 if ( InReportView() )
2775 int lineHeight
= GetLineHeight();
2777 size_t visibleFrom
, visibleTo
;
2778 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2781 int xOrig
= dc
.LogicalToDeviceX( 0 );
2782 int yOrig
= dc
.LogicalToDeviceY( 0 );
2784 // tell the caller cache to cache the data
2787 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2788 GetParent()->GetId());
2789 evCache
.SetEventObject( GetParent() );
2790 evCache
.m_oldItemIndex
= visibleFrom
;
2791 evCache
.m_itemIndex
= visibleTo
;
2792 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2795 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2797 rectLine
= GetLineRect(line
);
2800 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2801 rectLine
.width
, rectLine
.height
) )
2803 // don't redraw unaffected lines to avoid flicker
2807 GetLine(line
)->DrawInReportMode( &dc
,
2809 GetLineHighlightRect(line
),
2810 IsHighlighted(line
) );
2813 if ( HasFlag(wxLC_HRULES
) )
2815 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2816 wxSize clientSize
= GetClientSize();
2818 size_t i
= visibleFrom
;
2819 if (i
== 0) i
= 1; // Don't draw the first one
2820 for ( ; i
<= visibleTo
; i
++ )
2823 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2824 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2825 clientSize
.x
- dev_x
, i
* lineHeight
);
2828 // Draw last horizontal rule
2829 if ( visibleTo
== GetItemCount() - 1 )
2832 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2833 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2834 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2838 // Draw vertical rules if required
2839 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2841 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2842 wxRect firstItemRect
, lastItemRect
;
2844 GetItemRect(visibleFrom
, firstItemRect
);
2845 GetItemRect(visibleTo
, lastItemRect
);
2846 int x
= firstItemRect
.GetX();
2848 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2850 for (int col
= 0; col
< GetColumnCount(); col
++)
2852 int colWidth
= GetColumnWidth(col
);
2854 int x_pos
= x
- dev_x
;
2855 if (col
< GetColumnCount()-1) x_pos
-= 2;
2856 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2857 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2863 size_t count
= GetItemCount();
2864 for ( size_t i
= 0; i
< count
; i
++ )
2866 GetLine(i
)->Draw( &dc
);
2871 // Don't draw rect outline under Mac at all.
2876 wxRect
rect( GetLineHighlightRect( m_current
) );
2878 dc
.SetPen( *wxBLACK_PEN
);
2879 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2880 dc
.DrawRectangle( rect
);
2882 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2890 void wxListMainWindow::HighlightAll( bool on
)
2892 if ( IsSingleSel() )
2894 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2896 // we just have one item to turn off
2897 if ( HasCurrent() && IsHighlighted(m_current
) )
2899 HighlightLine(m_current
, false);
2900 RefreshLine(m_current
);
2903 else // multi selection
2906 HighlightLines(0, GetItemCount() - 1, on
);
2910 void wxListMainWindow::OnChildFocus(wxChildFocusEvent
& WXUNUSED(event
))
2912 // Do nothing here. This prevents the default handler in wxScrolledWindow
2913 // from needlessly scrolling the window when the edit control is
2914 // dismissed. See ticket #9563.
2917 void wxListMainWindow::SendNotify( size_t line
,
2918 wxEventType command
,
2919 const wxPoint
& point
)
2921 wxListEvent
le( command
, GetParent()->GetId() );
2922 le
.SetEventObject( GetParent() );
2924 le
.m_itemIndex
= line
;
2926 // set only for events which have position
2927 if ( point
!= wxDefaultPosition
)
2928 le
.m_pointDrag
= point
;
2930 // don't try to get the line info for virtual list controls: the main
2931 // program has it anyhow and if we did it would result in accessing all
2932 // the lines, even those which are not visible now and this is precisely
2933 // what we're trying to avoid
2936 if ( line
!= (size_t)-1 )
2938 GetLine(line
)->GetItem( 0, le
.m_item
);
2940 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2942 //else: there may be no more such item
2944 GetParent()->GetEventHandler()->ProcessEvent( le
);
2947 void wxListMainWindow::ChangeCurrent(size_t current
)
2949 m_current
= current
;
2951 // as the current item changed, we shouldn't start editing it when the
2952 // "slow click" timer expires as the click happened on another item
2953 if ( m_renameTimer
->IsRunning() )
2954 m_renameTimer
->Stop();
2956 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2959 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2961 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2962 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2964 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2965 wxT("EditLabel() needs a text control") );
2967 size_t itemEdit
= (size_t)item
;
2969 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2970 le
.SetEventObject( GetParent() );
2971 le
.m_itemIndex
= item
;
2972 wxListLineData
*data
= GetLine(itemEdit
);
2973 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2974 data
->GetItem( 0, le
.m_item
);
2976 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2978 // vetoed by user code
2982 // We have to call this here because the label in question might just have
2983 // been added and no screen update taken place.
2988 // Pending events dispatched by wxSafeYield might have changed the item
2990 if ( (size_t)item
>= GetItemCount() )
2994 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2995 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2996 return m_textctrlWrapper
->GetText();
2999 void wxListMainWindow::OnRenameTimer()
3001 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
3003 EditLabel( m_current
);
3006 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
3008 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
3009 le
.SetEventObject( GetParent() );
3010 le
.m_itemIndex
= itemEdit
;
3012 wxListLineData
*data
= GetLine(itemEdit
);
3014 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
3016 data
->GetItem( 0, le
.m_item
);
3017 le
.m_item
.m_text
= value
;
3018 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
3022 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
3024 // let owner know that the edit was cancelled
3025 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
3027 le
.SetEditCanceled(true);
3029 le
.SetEventObject( GetParent() );
3030 le
.m_itemIndex
= itemEdit
;
3032 wxListLineData
*data
= GetLine(itemEdit
);
3033 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
3035 data
->GetItem( 0, le
.m_item
);
3036 GetEventHandler()->ProcessEvent( le
);
3039 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
3043 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
3044 // shutdown the edit control when the mouse is clicked elsewhere on the
3045 // listctrl because the order of events is different (or something like
3046 // that), so explicitly end the edit if it is active.
3047 if ( event
.LeftDown() && m_textctrlWrapper
)
3048 m_textctrlWrapper
->EndEdit( false );
3051 if ( event
.LeftDown() )
3054 event
.SetEventObject( GetParent() );
3055 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3058 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3060 // let the base handle mouse wheel events.
3065 if ( !HasCurrent() || IsEmpty() )
3067 if (event
.RightDown())
3069 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3071 wxContextMenuEvent
evtCtx(
3073 GetParent()->GetId(),
3074 ClientToScreen(event
.GetPosition()));
3075 evtCtx
.SetEventObject(GetParent());
3076 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3084 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3085 event
.ButtonDClick()) )
3088 int x
= event
.GetX();
3089 int y
= event
.GetY();
3090 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3092 // where did we hit it (if we did)?
3095 size_t count
= GetItemCount(),
3098 if ( InReportView() )
3100 current
= y
/ GetLineHeight();
3101 if ( current
< count
)
3102 hitResult
= HitTestLine(current
, x
, y
);
3106 // TODO: optimize it too! this is less simple than for report view but
3107 // enumerating all items is still not a way to do it!!
3108 for ( current
= 0; current
< count
; current
++ )
3110 hitResult
= HitTestLine(current
, x
, y
);
3116 if (event
.Dragging())
3118 if (m_dragCount
== 0)
3120 // we have to report the raw, physical coords as we want to be
3121 // able to call HitTest(event.m_pointDrag) from the user code to
3122 // get the item being dragged
3123 m_dragStart
= event
.GetPosition();
3128 if (m_dragCount
!= 3)
3131 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3132 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3134 wxListEvent
le( command
, GetParent()->GetId() );
3135 le
.SetEventObject( GetParent() );
3136 le
.m_itemIndex
= m_lineLastClicked
;
3137 le
.m_pointDrag
= m_dragStart
;
3138 GetParent()->GetEventHandler()->ProcessEvent( le
);
3149 // outside of any item
3150 if (event
.RightDown())
3152 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3154 wxContextMenuEvent
evtCtx(
3156 GetParent()->GetId(),
3157 ClientToScreen(event
.GetPosition()));
3158 evtCtx
.SetEventObject(GetParent());
3159 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3163 // reset the selection and bail out
3164 HighlightAll(false);
3170 bool forceClick
= false;
3171 if (event
.ButtonDClick())
3173 if ( m_renameTimer
->IsRunning() )
3174 m_renameTimer
->Stop();
3176 m_lastOnSame
= false;
3178 if ( current
== m_lineLastClicked
)
3180 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3186 // The first click was on another item, so don't interpret this as
3187 // a double click, but as a simple click instead
3194 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3196 // select single line
3197 HighlightAll( false );
3198 ReverseHighlight(m_lineSelectSingleOnUp
);
3203 if ((current
== m_current
) &&
3204 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3205 HasFlag(wxLC_EDIT_LABELS
) )
3207 if ( !InReportView() ||
3208 GetLineLabelRect(current
).Contains(x
, y
) )
3210 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
3211 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
3216 m_lastOnSame
= false;
3217 m_lineSelectSingleOnUp
= (size_t)-1;
3221 // This is necessary, because after a DnD operation in
3222 // from and to ourself, the up event is swallowed by the
3223 // DnD code. So on next non-up event (which means here and
3224 // now) m_lineSelectSingleOnUp should be reset.
3225 m_lineSelectSingleOnUp
= (size_t)-1;
3227 if (event
.RightDown())
3229 m_lineBeforeLastClicked
= m_lineLastClicked
;
3230 m_lineLastClicked
= current
;
3232 // If the item is already selected, do not update the selection.
3233 // Multi-selections should not be cleared if a selected item is clicked.
3234 if (!IsHighlighted(current
))
3236 HighlightAll(false);
3237 ChangeCurrent(current
);
3238 ReverseHighlight(m_current
);
3241 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3243 // Allow generation of context menu event
3246 else if (event
.MiddleDown())
3248 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3250 else if ( event
.LeftDown() || forceClick
)
3252 m_lineBeforeLastClicked
= m_lineLastClicked
;
3253 m_lineLastClicked
= current
;
3255 size_t oldCurrent
= m_current
;
3256 bool oldWasSelected
= IsHighlighted(m_current
);
3258 bool cmdModifierDown
= event
.CmdDown();
3259 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3261 if ( IsSingleSel() || !IsHighlighted(current
) )
3263 HighlightAll( false );
3265 ChangeCurrent(current
);
3267 ReverseHighlight(m_current
);
3269 else // multi sel & current is highlighted & no mod keys
3271 m_lineSelectSingleOnUp
= current
;
3272 ChangeCurrent(current
); // change focus
3275 else // multi sel & either ctrl or shift is down
3277 if (cmdModifierDown
)
3279 ChangeCurrent(current
);
3281 ReverseHighlight(m_current
);
3283 else if (event
.ShiftDown())
3285 ChangeCurrent(current
);
3287 size_t lineFrom
= oldCurrent
,
3290 if ( lineTo
< lineFrom
)
3293 lineFrom
= m_current
;
3296 HighlightLines(lineFrom
, lineTo
);
3298 else // !ctrl, !shift
3300 // test in the enclosing if should make it impossible
3301 wxFAIL_MSG( _T("how did we get here?") );
3305 if (m_current
!= oldCurrent
)
3306 RefreshLine( oldCurrent
);
3308 // forceClick is only set if the previous click was on another item
3309 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3313 void wxListMainWindow::MoveToItem(size_t item
)
3315 if ( item
== (size_t)-1 )
3318 wxRect rect
= GetLineRect(item
);
3320 int client_w
, client_h
;
3321 GetClientSize( &client_w
, &client_h
);
3323 const int hLine
= GetLineHeight();
3325 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3326 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3328 if ( InReportView() )
3330 // the next we need the range of lines shown it might be different,
3331 // so recalculate it
3332 ResetVisibleLinesRange();
3334 if (rect
.y
< view_y
)
3335 GetListCtrl()->Scroll( -1, rect
.y
/ hLine
);
3336 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3337 GetListCtrl()->Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3340 // At least on Mac the visible lines value will get reset inside of
3341 // Scroll *before* it actually scrolls the window because of the
3342 // Update() that happens there, so it will still have the wrong value.
3343 // So let's reset it again and wait for it to be recalculated in the
3344 // next paint event. I would expect this problem to show up in wxGTK
3345 // too but couldn't duplicate it there. Perhaps the order of events
3346 // is different... --Robin
3347 ResetVisibleLinesRange();
3355 if (rect
.x
-view_x
< 5)
3356 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
3357 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3358 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
3360 if (rect
.y
-view_y
< 5)
3361 sy
= (rect
.y
- 5) / hLine
;
3362 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
3363 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
3365 GetListCtrl()->Scroll(sx
, sy
);
3369 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
3371 if ( !InReportView() )
3373 // TODO: this should work in all views but is not implemented now
3378 GetVisibleLinesRange(&top
, &bottom
);
3380 if ( bottom
== (size_t)-1 )
3383 ResetVisibleLinesRange();
3385 int hLine
= GetLineHeight();
3387 GetListCtrl()->Scroll(-1, top
+ dy
/ hLine
);
3390 // see comment in MoveToItem() for why we do this
3391 ResetVisibleLinesRange();
3397 // ----------------------------------------------------------------------------
3398 // keyboard handling
3399 // ----------------------------------------------------------------------------
3401 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3403 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3404 _T("invalid item index in OnArrowChar()") );
3406 size_t oldCurrent
= m_current
;
3408 // in single selection we just ignore Shift as we can't select several
3410 if ( event
.ShiftDown() && !IsSingleSel() )
3412 ChangeCurrent(newCurrent
);
3414 // refresh the old focus to remove it
3415 RefreshLine( oldCurrent
);
3417 // select all the items between the old and the new one
3418 if ( oldCurrent
> newCurrent
)
3420 newCurrent
= oldCurrent
;
3421 oldCurrent
= m_current
;
3424 HighlightLines(oldCurrent
, newCurrent
);
3428 // all previously selected items are unselected unless ctrl is held
3429 // in a multiselection control
3430 if ( !event
.ControlDown() || IsSingleSel() )
3431 HighlightAll(false);
3433 ChangeCurrent(newCurrent
);
3435 // refresh the old focus to remove it
3436 RefreshLine( oldCurrent
);
3438 // in single selection mode we must always have a selected item
3439 if ( !event
.ControlDown() || IsSingleSel() )
3440 HighlightLine( m_current
, true );
3443 RefreshLine( m_current
);
3448 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3450 wxWindow
*parent
= GetParent();
3452 // propagate the key event upwards
3453 wxKeyEvent
ke(event
);
3454 ke
.SetEventObject( parent
);
3455 if (parent
->GetEventHandler()->ProcessEvent( ke
))
3461 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3463 wxWindow
*parent
= GetParent();
3465 // propagate the key event upwards
3466 wxKeyEvent
ke(event
);
3467 if (parent
->GetEventHandler()->ProcessEvent( ke
))
3473 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3475 wxWindow
*parent
= GetParent();
3477 // send a list_key event up
3480 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3481 le
.m_itemIndex
= m_current
;
3482 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3483 le
.m_code
= event
.GetKeyCode();
3484 le
.SetEventObject( parent
);
3485 parent
->GetEventHandler()->ProcessEvent( le
);
3488 if ( (event
.GetKeyCode() != WXK_UP
) &&
3489 (event
.GetKeyCode() != WXK_DOWN
) &&
3490 (event
.GetKeyCode() != WXK_RIGHT
) &&
3491 (event
.GetKeyCode() != WXK_LEFT
) &&
3492 (event
.GetKeyCode() != WXK_PAGEUP
) &&
3493 (event
.GetKeyCode() != WXK_PAGEDOWN
) &&
3494 (event
.GetKeyCode() != WXK_END
) &&
3495 (event
.GetKeyCode() != WXK_HOME
) )
3497 // propagate the char event upwards
3498 wxKeyEvent
ke(event
);
3499 ke
.SetEventObject( parent
);
3500 if (parent
->GetEventHandler()->ProcessEvent( ke
))
3504 if ( HandleAsNavigationKey(event
) )
3507 // no item -> nothing to do
3514 // don't use m_linesPerPage directly as it might not be computed yet
3515 const int pageSize
= GetCountPerPage();
3516 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3518 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3520 if (event
.GetKeyCode() == WXK_RIGHT
)
3521 event
.m_keyCode
= WXK_LEFT
;
3522 else if (event
.GetKeyCode() == WXK_LEFT
)
3523 event
.m_keyCode
= WXK_RIGHT
;
3526 switch ( event
.GetKeyCode() )
3529 if ( m_current
> 0 )
3530 OnArrowChar( m_current
- 1, event
);
3534 if ( m_current
< (size_t)GetItemCount() - 1 )
3535 OnArrowChar( m_current
+ 1, event
);
3540 OnArrowChar( GetItemCount() - 1, event
);
3545 OnArrowChar( 0, event
);
3550 int steps
= InReportView() ? pageSize
- 1
3551 : m_current
% pageSize
;
3553 int index
= m_current
- steps
;
3557 OnArrowChar( index
, event
);
3563 int steps
= InReportView()
3565 : pageSize
- (m_current
% pageSize
) - 1;
3567 size_t index
= m_current
+ steps
;
3568 size_t count
= GetItemCount();
3569 if ( index
>= count
)
3572 OnArrowChar( index
, event
);
3577 if ( !InReportView() )
3579 int index
= m_current
- pageSize
;
3583 OnArrowChar( index
, event
);
3588 if ( !InReportView() )
3590 size_t index
= m_current
+ pageSize
;
3592 size_t count
= GetItemCount();
3593 if ( index
>= count
)
3596 OnArrowChar( index
, event
);
3601 if ( IsSingleSel() )
3603 if ( event
.ControlDown() )
3605 ReverseHighlight(m_current
);
3607 else // normal space press
3609 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3612 else // multiple selection
3614 ReverseHighlight(m_current
);
3620 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3628 // ----------------------------------------------------------------------------
3630 // ----------------------------------------------------------------------------
3632 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3636 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3637 event
.SetEventObject( GetParent() );
3638 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3642 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3643 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3644 // which are already drawn correctly resulting in horrible flicker - avoid
3654 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3658 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3659 event
.SetEventObject( GetParent() );
3660 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3668 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3670 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3672 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3674 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3676 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3678 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3680 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3682 else if ( InReportView() && (m_small_image_list
))
3684 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3688 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3690 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3692 m_normal_image_list
->GetSize( index
, width
, height
);
3694 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3696 m_small_image_list
->GetSize( index
, width
, height
);
3698 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3700 m_small_image_list
->GetSize( index
, width
, height
);
3702 else if ( InReportView() && m_small_image_list
)
3704 m_small_image_list
->GetSize( index
, width
, height
);
3713 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3715 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3716 dc
.SetFont( GetFont() );
3719 dc
.GetTextExtent( s
, &lw
, NULL
);
3721 return lw
+ AUTOSIZE_COL_MARGIN
;
3724 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3728 // calc the spacing from the icon size
3729 int width
= 0, height
= 0;
3731 if ((imageList
) && (imageList
->GetImageCount()) )
3732 imageList
->GetSize(0, width
, height
);
3734 if (which
== wxIMAGE_LIST_NORMAL
)
3736 m_normal_image_list
= imageList
;
3737 m_normal_spacing
= width
+ 8;
3740 if (which
== wxIMAGE_LIST_SMALL
)
3742 m_small_image_list
= imageList
;
3743 m_small_spacing
= width
+ 14;
3744 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3748 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3752 m_small_spacing
= spacing
;
3754 m_normal_spacing
= spacing
;
3757 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3759 return isSmall
? m_small_spacing
: m_normal_spacing
;
3762 // ----------------------------------------------------------------------------
3764 // ----------------------------------------------------------------------------
3766 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3768 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3770 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3772 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3773 item
.m_width
= GetTextLength( item
.m_text
);
3775 wxListHeaderData
*column
= node
->GetData();
3776 column
->SetItem( item
);
3778 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3780 headerWin
->m_dirty
= true;
3784 // invalidate it as it has to be recalculated
3788 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3790 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3791 _T("invalid column index") );
3793 wxCHECK_RET( InReportView(),
3794 _T("SetColumnWidth() can only be called in report mode.") );
3798 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3800 headerWin
->m_dirty
= true;
3802 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3803 wxCHECK_RET( node
, _T("no column?") );
3805 wxListHeaderData
*column
= node
->GetData();
3807 size_t count
= GetItemCount();
3809 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3811 width
= GetTextLength(column
->GetText());
3812 width
+= 2*EXTRA_WIDTH
;
3814 // check for column header's image availability
3815 const int image
= column
->GetImage();
3818 if ( m_small_image_list
)
3821 m_small_image_list
->GetSize(image
, ix
, iy
);
3822 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3826 else if ( width
== wxLIST_AUTOSIZE
)
3830 // TODO: determine the max width somehow...
3831 width
= WIDTH_COL_DEFAULT
;
3835 wxClientDC
dc(this);
3836 dc
.SetFont( GetFont() );
3838 int max
= AUTOSIZE_COL_MARGIN
;
3840 // if the cached column width isn't valid then recalculate it
3841 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3843 for (size_t i
= 0; i
< count
; i
++)
3845 wxListLineData
*line
= GetLine( i
);
3846 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3848 wxCHECK_RET( n
, _T("no subitem?") );
3850 wxListItemData
*itemData
= n
->GetData();
3853 itemData
->GetItem(item
);
3854 int itemWidth
= GetItemWidthWithImage(&item
);
3855 if (itemWidth
> max
)
3859 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3860 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3863 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3864 width
= max
+ AUTOSIZE_COL_MARGIN
;
3868 column
->SetWidth( width
);
3870 // invalidate it as it has to be recalculated
3874 int wxListMainWindow::GetHeaderWidth() const
3876 if ( !m_headerWidth
)
3878 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3880 size_t count
= GetColumnCount();
3881 for ( size_t col
= 0; col
< count
; col
++ )
3883 self
->m_headerWidth
+= GetColumnWidth(col
);
3887 return m_headerWidth
;
3890 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3892 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3893 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3895 wxListHeaderData
*column
= node
->GetData();
3896 column
->GetItem( item
);
3899 int wxListMainWindow::GetColumnWidth( int col
) const
3901 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3902 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3904 wxListHeaderData
*column
= node
->GetData();
3905 return column
->GetWidth();
3908 // ----------------------------------------------------------------------------
3910 // ----------------------------------------------------------------------------
3912 void wxListMainWindow::SetItem( wxListItem
&item
)
3914 long id
= item
.m_itemId
;
3915 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3916 _T("invalid item index in SetItem") );
3920 wxListLineData
*line
= GetLine((size_t)id
);
3921 line
->SetItem( item
.m_col
, item
);
3923 // Set item state if user wants
3924 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3925 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3929 // update the Max Width Cache if needed
3930 int width
= GetItemWidthWithImage(&item
);
3932 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3933 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3937 // update the item on screen
3939 GetItemRect(id
, rectItem
);
3940 RefreshRect(rectItem
);
3943 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3948 // first deal with selection
3949 if ( stateMask
& wxLIST_STATE_SELECTED
)
3951 // set/clear select state
3954 // optimized version for virtual listctrl.
3955 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3958 else if ( state
& wxLIST_STATE_SELECTED
)
3960 const long count
= GetItemCount();
3961 for( long i
= 0; i
< count
; i
++ )
3963 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3969 // clear for non virtual (somewhat optimized by using GetNextItem())
3971 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3973 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3978 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3980 // unfocus all: only one item can be focussed, so clearing focus for
3981 // all items is simply clearing focus of the focussed item.
3982 SetItemState(m_current
, state
, stateMask
);
3984 //(setting focus to all items makes no sense, so it is not handled here.)
3987 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3991 SetItemStateAll(state
, stateMask
);
3995 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3996 _T("invalid list ctrl item index in SetItem") );
3998 size_t oldCurrent
= m_current
;
3999 size_t item
= (size_t)litem
; // safe because of the check above
4001 // do we need to change the focus?
4002 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4004 if ( state
& wxLIST_STATE_FOCUSED
)
4006 // don't do anything if this item is already focused
4007 if ( item
!= m_current
)
4009 ChangeCurrent(item
);
4011 if ( oldCurrent
!= (size_t)-1 )
4013 if ( IsSingleSel() )
4015 HighlightLine(oldCurrent
, false);
4018 RefreshLine(oldCurrent
);
4021 RefreshLine( m_current
);
4026 // don't do anything if this item is not focused
4027 if ( item
== m_current
)
4031 if ( IsSingleSel() )
4033 // we must unselect the old current item as well or we
4034 // might end up with more than one selected item in a
4035 // single selection control
4036 HighlightLine(oldCurrent
, false);
4039 RefreshLine( oldCurrent
);
4044 // do we need to change the selection state?
4045 if ( stateMask
& wxLIST_STATE_SELECTED
)
4047 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
4049 if ( IsSingleSel() )
4053 // selecting the item also makes it the focused one in the
4055 if ( m_current
!= item
)
4057 ChangeCurrent(item
);
4059 if ( oldCurrent
!= (size_t)-1 )
4061 HighlightLine( oldCurrent
, false );
4062 RefreshLine( oldCurrent
);
4068 // only the current item may be selected anyhow
4069 if ( item
!= m_current
)
4074 if ( HighlightLine(item
, on
) )
4081 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
4083 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
4084 _T("invalid list ctrl item index in GetItemState()") );
4086 int ret
= wxLIST_STATE_DONTCARE
;
4088 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4090 if ( (size_t)item
== m_current
)
4091 ret
|= wxLIST_STATE_FOCUSED
;
4094 if ( stateMask
& wxLIST_STATE_SELECTED
)
4096 if ( IsHighlighted(item
) )
4097 ret
|= wxLIST_STATE_SELECTED
;
4103 void wxListMainWindow::GetItem( wxListItem
&item
) const
4105 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
4106 _T("invalid item index in GetItem") );
4108 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4109 line
->GetItem( item
.m_col
, item
);
4111 // Get item state if user wants it
4112 if ( item
.m_mask
& wxLIST_MASK_STATE
)
4113 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
4114 wxLIST_STATE_FOCUSED
);
4117 // ----------------------------------------------------------------------------
4119 // ----------------------------------------------------------------------------
4121 size_t wxListMainWindow::GetItemCount() const
4123 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4126 void wxListMainWindow::SetItemCount(long count
)
4128 m_selStore
.SetItemCount(count
);
4129 m_countVirt
= count
;
4131 ResetVisibleLinesRange();
4133 // scrollbars must be reset
4137 int wxListMainWindow::GetSelectedItemCount() const
4139 // deal with the quick case first
4140 if ( IsSingleSel() )
4141 return HasCurrent() ? IsHighlighted(m_current
) : false;
4143 // virtual controls remmebers all its selections itself
4145 return m_selStore
.GetSelectedCount();
4147 // TODO: we probably should maintain the number of items selected even for
4148 // non virtual controls as enumerating all lines is really slow...
4149 size_t countSel
= 0;
4150 size_t count
= GetItemCount();
4151 for ( size_t line
= 0; line
< count
; line
++ )
4153 if ( GetLine(line
)->IsHighlighted() )
4160 // ----------------------------------------------------------------------------
4161 // item position/size
4162 // ----------------------------------------------------------------------------
4164 wxRect
wxListMainWindow::GetViewRect() const
4166 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
4168 // we need to find the longest/tallest label
4169 wxCoord xMax
= 0, yMax
= 0;
4170 const int count
= GetItemCount();
4173 for ( int i
= 0; i
< count
; i
++ )
4175 // we need logical, not physical, coordinates here, so use
4176 // GetLineRect() instead of GetItemRect()
4177 wxRect r
= GetLineRect(i
);
4179 wxCoord x
= r
.GetRight(),
4189 // some fudge needed to make it look prettier
4190 xMax
+= 2 * EXTRA_BORDER_X
;
4191 yMax
+= 2 * EXTRA_BORDER_Y
;
4193 // account for the scrollbars if necessary
4194 const wxSize sizeAll
= GetClientSize();
4195 if ( xMax
> sizeAll
.x
)
4196 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4197 if ( yMax
> sizeAll
.y
)
4198 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4200 return wxRect(0, 0, xMax
, yMax
);
4204 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
4206 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
4208 _T("GetSubItemRect only meaningful in report view") );
4209 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
4210 _T("invalid item in GetSubItemRect") );
4212 // ensure that we're laid out, otherwise we could return nonsense
4215 wxConstCast(this, wxListMainWindow
)->
4216 RecalculatePositions(true /* no refresh */);
4219 rect
= GetLineRect((size_t)item
);
4221 // Adjust rect to specified column
4222 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
4224 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
4225 _T("invalid subItem in GetSubItemRect") );
4227 for (int i
= 0; i
< subItem
; i
++)
4229 rect
.x
+= GetColumnWidth(i
);
4231 rect
.width
= GetColumnWidth(subItem
);
4234 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4239 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4242 GetItemRect(item
, rect
);
4250 // ----------------------------------------------------------------------------
4251 // geometry calculation
4252 // ----------------------------------------------------------------------------
4254 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4256 const int lineHeight
= GetLineHeight();
4258 wxClientDC
dc( this );
4259 dc
.SetFont( GetFont() );
4261 const size_t count
= GetItemCount();
4264 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
4265 iconSpacing
= m_normal_spacing
;
4266 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
4267 iconSpacing
= m_small_spacing
;
4271 // Note that we do not call GetClientSize() here but
4272 // GetSize() and subtract the border size for sunken
4273 // borders manually. This is technically incorrect,
4274 // but we need to know the client area's size WITHOUT
4275 // scrollbars here. Since we don't know if there are
4276 // any scrollbars, we use GetSize() instead. Another
4277 // solution would be to call SetScrollbars() here to
4278 // remove the scrollbars and call GetClientSize() then,
4279 // but this might result in flicker and - worse - will
4280 // reset the scrollbars to 0 which is not good at all
4281 // if you resize a dialog/window, but don't want to
4282 // reset the window scrolling. RR.
4283 // Furthermore, we actually do NOT subtract the border
4284 // width as 2 pixels is just the extra space which we
4285 // need around the actual content in the window. Other-
4286 // wise the text would e.g. touch the upper border. RR.
4289 GetSize( &clientWidth
, &clientHeight
);
4291 if ( InReportView() )
4293 // all lines have the same height and we scroll one line per step
4294 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4296 m_linesPerPage
= clientHeight
/ lineHeight
;
4298 ResetVisibleLinesRange();
4300 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4301 GetHeaderWidth() / SCROLL_UNIT_X
,
4302 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4303 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
4304 GetListCtrl()->GetScrollPos(wxVERTICAL
),
4309 // we have 3 different layout strategies: either layout all items
4310 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4311 // to arrange them in top to bottom, left to right (don't ask me why
4312 // not the other way round...) order
4313 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4315 int x
= EXTRA_BORDER_X
;
4316 int y
= EXTRA_BORDER_Y
;
4318 wxCoord widthMax
= 0;
4321 for ( i
= 0; i
< count
; i
++ )
4323 wxListLineData
*line
= GetLine(i
);
4324 line
->CalculateSize( &dc
, iconSpacing
);
4325 line
->SetPosition( x
, y
, iconSpacing
);
4327 wxSize sizeLine
= GetLineSize(i
);
4329 if ( HasFlag(wxLC_ALIGN_TOP
) )
4331 if ( sizeLine
.x
> widthMax
)
4332 widthMax
= sizeLine
.x
;
4336 else // wxLC_ALIGN_LEFT
4338 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4342 if ( HasFlag(wxLC_ALIGN_TOP
) )
4344 // traverse the items again and tweak their sizes so that they are
4345 // all the same in a row
4346 for ( i
= 0; i
< count
; i
++ )
4348 wxListLineData
*line
= GetLine(i
);
4349 line
->m_gi
->ExtendWidth(widthMax
);
4353 GetListCtrl()->SetScrollbars
4357 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4358 (y
+ lineHeight
) / lineHeight
,
4359 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
4360 GetListCtrl()->GetScrollPos( wxVERTICAL
),
4364 else // "flowed" arrangement, the most complicated case
4366 // at first we try without any scrollbars, if the items don't fit into
4367 // the window, we recalculate after subtracting the space taken by the
4370 int entireWidth
= 0;
4372 for (int tries
= 0; tries
< 2; tries
++)
4374 entireWidth
= 2 * EXTRA_BORDER_X
;
4378 // Now we have decided that the items do not fit into the
4379 // client area, so we need a scrollbar
4380 entireWidth
+= SCROLL_UNIT_X
;
4383 int x
= EXTRA_BORDER_X
;
4384 int y
= EXTRA_BORDER_Y
;
4385 int maxWidthInThisRow
= 0;
4388 int currentlyVisibleLines
= 0;
4390 for (size_t i
= 0; i
< count
; i
++)
4392 currentlyVisibleLines
++;
4393 wxListLineData
*line
= GetLine( i
);
4394 line
->CalculateSize( &dc
, iconSpacing
);
4395 line
->SetPosition( x
, y
, iconSpacing
);
4397 wxSize sizeLine
= GetLineSize( i
);
4399 if ( maxWidthInThisRow
< sizeLine
.x
)
4400 maxWidthInThisRow
= sizeLine
.x
;
4403 if (currentlyVisibleLines
> m_linesPerPage
)
4404 m_linesPerPage
= currentlyVisibleLines
;
4406 if ( y
+ sizeLine
.y
>= clientHeight
)
4408 currentlyVisibleLines
= 0;
4410 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4411 x
+= maxWidthInThisRow
;
4412 entireWidth
+= maxWidthInThisRow
;
4413 maxWidthInThisRow
= 0;
4416 // We have reached the last item.
4417 if ( i
== count
- 1 )
4418 entireWidth
+= maxWidthInThisRow
;
4420 if ( (tries
== 0) &&
4421 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4423 clientHeight
-= wxSystemSettings::
4424 GetMetric(wxSYS_HSCROLL_Y
);
4429 if ( i
== count
- 1 )
4430 tries
= 1; // Everything fits, no second try required.
4434 GetListCtrl()->SetScrollbars
4438 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4440 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
4449 // FIXME: why should we call it from here?
4456 void wxListMainWindow::RefreshAll()
4461 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4462 if ( headerWin
&& headerWin
->m_dirty
)
4464 headerWin
->m_dirty
= false;
4465 headerWin
->Refresh();
4469 void wxListMainWindow::UpdateCurrent()
4471 if ( !HasCurrent() && !IsEmpty() )
4475 long wxListMainWindow::GetNextItem( long item
,
4476 int WXUNUSED(geometry
),
4480 max
= GetItemCount();
4481 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4482 _T("invalid listctrl index in GetNextItem()") );
4484 // notice that we start with the next item (or the first one if item == -1)
4485 // and this is intentional to allow writing a simple loop to iterate over
4486 // all selected items
4489 // this is not an error because the index was OK initially,
4490 // just no such item
4497 size_t count
= GetItemCount();
4498 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4500 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4503 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4510 // ----------------------------------------------------------------------------
4512 // ----------------------------------------------------------------------------
4514 void wxListMainWindow::DeleteItem( long lindex
)
4516 size_t count
= GetItemCount();
4518 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4519 _T("invalid item index in DeleteItem") );
4521 size_t index
= (size_t)lindex
;
4523 // we don't need to adjust the index for the previous items
4524 if ( HasCurrent() && m_current
>= index
)
4526 // if the current item is being deleted, we want the next one to
4527 // become selected - unless there is no next one - so don't adjust
4528 // m_current in this case
4529 if ( m_current
!= index
|| m_current
== count
- 1 )
4533 if ( InReportView() )
4535 // mark the Column Max Width cache as dirty if the items in the line
4536 // we're deleting contain the Max Column Width
4537 wxListLineData
* const line
= GetLine(index
);
4538 wxListItemDataList::compatibility_iterator n
;
4539 wxListItemData
*itemData
;
4543 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4545 n
= line
->m_items
.Item( i
);
4546 itemData
= n
->GetData();
4547 itemData
->GetItem(item
);
4549 itemWidth
= GetItemWidthWithImage(&item
);
4551 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4552 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4555 ResetVisibleLinesRange();
4558 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4563 m_selStore
.OnItemDelete(index
);
4567 m_lines
.RemoveAt( index
);
4570 // we need to refresh the (vert) scrollbar as the number of items changed
4573 RefreshAfter(index
);
4576 void wxListMainWindow::DeleteColumn( int col
)
4578 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4580 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4583 delete node
->GetData();
4584 m_columns
.Erase( node
);
4588 // update all the items
4589 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4591 wxListLineData
* const line
= GetLine(i
);
4592 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4593 delete n
->GetData();
4594 line
->m_items
.Erase(n
);
4598 if ( InReportView() ) // we only cache max widths when in Report View
4600 delete m_aColWidths
.Item(col
);
4601 m_aColWidths
.RemoveAt(col
);
4604 // invalidate it as it has to be recalculated
4608 void wxListMainWindow::DoDeleteAllItems()
4611 // nothing to do - in particular, don't send the event
4616 // to make the deletion of all items faster, we don't send the
4617 // notifications for each item deletion in this case but only one event
4618 // for all of them: this is compatible with wxMSW and documented in
4619 // DeleteAllItems() description
4621 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4622 event
.SetEventObject( GetParent() );
4623 GetParent()->GetEventHandler()->ProcessEvent( event
);
4631 if ( InReportView() )
4633 ResetVisibleLinesRange();
4634 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4636 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4643 void wxListMainWindow::DeleteAllItems()
4647 RecalculatePositions();
4650 void wxListMainWindow::DeleteEverything()
4652 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4653 WX_CLEAR_ARRAY(m_aColWidths
);
4658 // ----------------------------------------------------------------------------
4659 // scanning for an item
4660 // ----------------------------------------------------------------------------
4662 void wxListMainWindow::EnsureVisible( long index
)
4664 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4665 _T("invalid index in EnsureVisible") );
4667 // We have to call this here because the label in question might just have
4668 // been added and its position is not known yet
4670 RecalculatePositions(true /* no refresh */);
4672 MoveToItem((size_t)index
);
4675 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4681 wxString str_upper
= str
.Upper();
4685 size_t count
= GetItemCount();
4686 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4688 wxListLineData
*line
= GetLine(i
);
4689 wxString line_upper
= line
->GetText(0).Upper();
4692 if (line_upper
== str_upper
)
4697 if (line_upper
.find(str_upper
) == 0)
4705 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4711 size_t count
= GetItemCount();
4712 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4714 wxListLineData
*line
= GetLine(i
);
4716 line
->GetItem( 0, item
);
4717 if (item
.m_data
== data
)
4724 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4727 GetVisibleLinesRange( &topItem
, NULL
);
4730 GetItemPosition( GetItemCount() - 1, p
);
4734 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4735 if ( id
>= 0 && id
< (long)GetItemCount() )
4741 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4743 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
4745 size_t count
= GetItemCount();
4747 if ( InReportView() )
4749 size_t current
= y
/ GetLineHeight();
4750 if ( current
< count
)
4752 flags
= HitTestLine(current
, x
, y
);
4759 // TODO: optimize it too! this is less simple than for report view but
4760 // enumerating all items is still not a way to do it!!
4761 for ( size_t current
= 0; current
< count
; current
++ )
4763 flags
= HitTestLine(current
, x
, y
);
4772 // ----------------------------------------------------------------------------
4774 // ----------------------------------------------------------------------------
4776 void wxListMainWindow::InsertItem( wxListItem
&item
)
4778 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4780 int count
= GetItemCount();
4781 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4783 if (item
.m_itemId
> count
)
4784 item
.m_itemId
= count
;
4786 size_t id
= item
.m_itemId
;
4790 if ( InReportView() )
4792 ResetVisibleLinesRange();
4794 // calculate the width of the item and adjust the max column width
4795 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4796 int width
= GetItemWidthWithImage(&item
);
4797 item
.SetWidth(width
);
4798 if (width
> pWidthInfo
->nMaxWidth
)
4799 pWidthInfo
->nMaxWidth
= width
;
4802 wxListLineData
*line
= new wxListLineData(this);
4804 line
->SetItem( item
.m_col
, item
);
4806 m_lines
.Insert( line
, id
);
4810 // If an item is selected at or below the point of insertion, we need to
4811 // increment the member variables because the current row's index has gone
4813 if ( HasCurrent() && m_current
>= id
)
4816 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4818 RefreshLines(id
, GetItemCount() - 1);
4821 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4824 if ( InReportView() )
4826 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4827 item
.m_width
= GetTextLength( item
.m_text
);
4829 wxListHeaderData
*column
= new wxListHeaderData( item
);
4830 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4832 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4835 wxListHeaderDataList::compatibility_iterator
4836 node
= m_columns
.Item( col
);
4837 m_columns
.Insert( node
, column
);
4838 m_aColWidths
.Insert( colWidthInfo
, col
);
4842 m_columns
.Append( column
);
4843 m_aColWidths
.Add( colWidthInfo
);
4848 // update all the items
4849 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4851 wxListLineData
* const line
= GetLine(i
);
4852 wxListItemData
* const data
= new wxListItemData(this);
4854 line
->m_items
.Insert(col
, data
);
4856 line
->m_items
.Append(data
);
4860 // invalidate it as it has to be recalculated
4865 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4868 wxClientDC
dc(this);
4870 dc
.SetFont( GetFont() );
4872 if (item
->GetImage() != -1)
4875 GetImageSize( item
->GetImage(), ix
, iy
);
4879 if (!item
->GetText().empty())
4882 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4889 // ----------------------------------------------------------------------------
4891 // ----------------------------------------------------------------------------
4893 static wxListCtrlCompare list_ctrl_compare_func_2
;
4894 static long list_ctrl_compare_data
;
4896 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4898 wxListLineData
*line1
= *arg1
;
4899 wxListLineData
*line2
= *arg2
;
4901 line1
->GetItem( 0, item
);
4902 wxUIntPtr data1
= item
.m_data
;
4903 line2
->GetItem( 0, item
);
4904 wxUIntPtr data2
= item
.m_data
;
4905 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4908 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4910 // selections won't make sense any more after sorting the items so reset
4912 HighlightAll(false);
4915 list_ctrl_compare_func_2
= fn
;
4916 list_ctrl_compare_data
= data
;
4917 m_lines
.Sort( list_ctrl_compare_func_1
);
4921 // ----------------------------------------------------------------------------
4923 // ----------------------------------------------------------------------------
4925 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4927 wxPrintf( "wxListMainWindow::OnScroll\n" );
4929 // HandleOnScroll( event );
4931 // update our idea of which lines are shown when we redraw the window the
4933 ResetVisibleLinesRange();
4935 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4937 wxGenericListCtrl
* lc
= GetListCtrl();
4938 wxCHECK_RET( lc
, _T("no listctrl window?") );
4940 lc
->m_headerWin
->Refresh();
4941 lc
->m_headerWin
->Update();
4945 int wxListMainWindow::GetCountPerPage() const
4947 if ( !m_linesPerPage
)
4949 wxConstCast(this, wxListMainWindow
)->
4950 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4953 return m_linesPerPage
;
4956 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4958 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4960 if ( m_lineFrom
== (size_t)-1 )
4962 size_t count
= GetItemCount();
4965 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4967 // this may happen if SetScrollbars() hadn't been called yet
4968 if ( m_lineFrom
>= count
)
4969 m_lineFrom
= count
- 1;
4971 // we redraw one extra line but this is needed to make the redrawing
4972 // logic work when there is a fractional number of lines on screen
4973 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4974 if ( m_lineTo
>= count
)
4975 m_lineTo
= count
- 1;
4977 else // empty control
4980 m_lineTo
= (size_t)-1;
4984 wxASSERT_MSG( IsEmpty() ||
4985 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4986 _T("GetVisibleLinesRange() returns incorrect result") );
4994 // -------------------------------------------------------------------------------------
4995 // wxGenericListCtrl
4996 // -------------------------------------------------------------------------------------
4998 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
5000 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
5001 EVT_SIZE(wxGenericListCtrl::OnSize
)
5002 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
5005 void wxGenericListCtrl::Init()
5007 m_imageListNormal
= NULL
;
5008 m_imageListSmall
= NULL
;
5009 m_imageListState
= NULL
;
5011 m_ownsImageListNormal
=
5012 m_ownsImageListSmall
=
5013 m_ownsImageListState
= false;
5017 m_headerHeight
= wxRendererNative::Get().GetHeaderButtonHeight(this);
5020 wxGenericListCtrl::~wxGenericListCtrl()
5022 if (m_ownsImageListNormal
)
5023 delete m_imageListNormal
;
5024 if (m_ownsImageListSmall
)
5025 delete m_imageListSmall
;
5026 if (m_ownsImageListState
)
5027 delete m_imageListState
;
5030 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
5032 bool needs_header
= HasHeader();
5033 bool has_header
= (m_headerWin
!= NULL
);
5035 if (needs_header
== has_header
)
5040 m_headerWin
= new wxListHeaderWindow
5042 this, wxID_ANY
, m_mainWin
,
5044 wxSize(GetClientSize().x
, m_headerHeight
),
5048 #if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON
5050 #if wxOSX_USE_ATSU_TEXT
5051 font
.MacCreateFromThemeFont( kThemeSmallSystemFont
);
5053 font
.MacCreateFromUIFont( kCTFontSystemFontType
);
5055 m_headerWin
->SetFont( font
);
5058 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
5062 GetSizer()->Detach( m_headerWin
);
5070 bool wxGenericListCtrl::Create(wxWindow
*parent
,
5075 const wxValidator
&validator
,
5076 const wxString
&name
)
5080 // just like in other ports, an assert will fail if the user doesn't give any type style:
5081 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
5082 _T("wxListCtrl style should have exactly one mode bit set") );
5084 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
5088 style
&= ~wxBORDER_MASK
;
5089 style
|= wxBORDER_THEME
;
5092 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
5094 SetTargetWindow( m_mainWin
);
5096 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
5097 sizer
->Add( m_mainWin
, 1, wxGROW
);
5100 CreateOrDestroyHeaderWindowAsNeeded();
5102 SetInitialSize(size
);
5107 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
5109 return wxBORDER_THEME
;
5113 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
5117 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
5120 // we need to process arrows ourselves for scrolling
5121 if ( nMsg
== WM_GETDLGCODE
)
5123 rc
|= DLGC_WANTARROWS
;
5131 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
5133 wxSize newsize
= size
;
5135 newsize
.y
-= m_headerWin
->GetSize().y
;
5140 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
5142 // update our idea of which lines are shown when we redraw
5143 // the window the next time
5144 m_mainWin
->ResetVisibleLinesRange();
5146 HandleOnScroll( event
);
5148 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
5150 m_headerWin
->Refresh();
5151 m_headerWin
->Update();
5155 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
5157 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
5158 _T("wxLC_VIRTUAL can't be [un]set") );
5160 long flag
= GetWindowStyle();
5164 if (style
& wxLC_MASK_TYPE
)
5165 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5166 if (style
& wxLC_MASK_ALIGN
)
5167 flag
&= ~wxLC_MASK_ALIGN
;
5168 if (style
& wxLC_MASK_SORT
)
5169 flag
&= ~wxLC_MASK_SORT
;
5177 // some styles can be set without recreating everything (as happens in
5178 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
5179 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
5182 wxWindow::SetWindowStyleFlag(flag
);
5186 SetWindowStyleFlag( flag
);
5190 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5194 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
5196 CreateOrDestroyHeaderWindowAsNeeded();
5198 GetSizer()->Layout();
5201 wxWindow::SetWindowStyleFlag( flag
);
5204 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5206 m_mainWin
->GetColumn( col
, item
);
5210 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5212 m_mainWin
->SetColumn( col
, item
);
5216 int wxGenericListCtrl::GetColumnWidth( int col
) const
5218 return m_mainWin
->GetColumnWidth( col
);
5221 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5223 m_mainWin
->SetColumnWidth( col
, width
);
5227 int wxGenericListCtrl::GetCountPerPage() const
5229 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5232 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5234 m_mainWin
->GetItem( info
);
5238 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5240 m_mainWin
->SetItem( info
);
5244 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5247 info
.m_text
= label
;
5248 info
.m_mask
= wxLIST_MASK_TEXT
;
5249 info
.m_itemId
= index
;
5253 info
.m_image
= imageId
;
5254 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5257 m_mainWin
->SetItem(info
);
5261 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5263 return m_mainWin
->GetItemState( item
, stateMask
);
5266 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5268 m_mainWin
->SetItemState( item
, state
, stateMask
);
5273 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5275 return SetItemColumnImage(item
, 0, image
);
5279 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5282 info
.m_image
= image
;
5283 info
.m_mask
= wxLIST_MASK_IMAGE
;
5284 info
.m_itemId
= item
;
5285 info
.m_col
= column
;
5286 m_mainWin
->SetItem( info
);
5290 wxString
wxGenericListCtrl::GetItemText( long item
) const
5292 return m_mainWin
->GetItemText(item
);
5295 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5297 m_mainWin
->SetItemText(item
, str
);
5300 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5303 info
.m_mask
= wxLIST_MASK_DATA
;
5304 info
.m_itemId
= item
;
5305 m_mainWin
->GetItem( info
);
5309 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
5312 info
.m_mask
= wxLIST_MASK_DATA
;
5313 info
.m_itemId
= item
;
5315 m_mainWin
->SetItem( info
);
5319 wxRect
wxGenericListCtrl::GetViewRect() const
5321 return m_mainWin
->GetViewRect();
5324 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
5326 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
5329 bool wxGenericListCtrl::GetSubItemRect(long item
,
5332 int WXUNUSED(code
)) const
5334 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
5337 if ( m_mainWin
->HasHeader() )
5338 rect
.y
+= m_headerHeight
+ 1;
5343 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5345 m_mainWin
->GetItemPosition( item
, pos
);
5349 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5354 int wxGenericListCtrl::GetItemCount() const
5356 return m_mainWin
->GetItemCount();
5359 int wxGenericListCtrl::GetColumnCount() const
5361 return m_mainWin
->GetColumnCount();
5364 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5366 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5369 wxSize
wxGenericListCtrl::GetItemSpacing() const
5371 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5373 return wxSize(spacing
, spacing
);
5376 #if WXWIN_COMPATIBILITY_2_6
5377 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5379 return m_mainWin
->GetItemSpacing( isSmall
);
5381 #endif // WXWIN_COMPATIBILITY_2_6
5383 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5386 info
.m_itemId
= item
;
5387 info
.SetTextColour( col
);
5388 m_mainWin
->SetItem( info
);
5391 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5394 info
.m_itemId
= item
;
5395 m_mainWin
->GetItem( info
);
5396 return info
.GetTextColour();
5399 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5402 info
.m_itemId
= item
;
5403 info
.SetBackgroundColour( col
);
5404 m_mainWin
->SetItem( info
);
5407 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5410 info
.m_itemId
= item
;
5411 m_mainWin
->GetItem( info
);
5412 return info
.GetBackgroundColour();
5415 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5418 info
.m_itemId
= item
;
5420 m_mainWin
->SetItem( info
);
5423 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5426 info
.m_itemId
= item
;
5427 m_mainWin
->GetItem( info
);
5428 return info
.GetFont();
5431 int wxGenericListCtrl::GetSelectedItemCount() const
5433 return m_mainWin
->GetSelectedItemCount();
5436 wxColour
wxGenericListCtrl::GetTextColour() const
5438 return GetForegroundColour();
5441 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5443 SetForegroundColour(col
);
5446 long wxGenericListCtrl::GetTopItem() const
5449 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5453 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5455 return m_mainWin
->GetNextItem( item
, geom
, state
);
5458 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5460 if (which
== wxIMAGE_LIST_NORMAL
)
5461 return m_imageListNormal
;
5462 else if (which
== wxIMAGE_LIST_SMALL
)
5463 return m_imageListSmall
;
5464 else if (which
== wxIMAGE_LIST_STATE
)
5465 return m_imageListState
;
5470 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5472 if ( which
== wxIMAGE_LIST_NORMAL
)
5474 if (m_ownsImageListNormal
)
5475 delete m_imageListNormal
;
5476 m_imageListNormal
= imageList
;
5477 m_ownsImageListNormal
= false;
5479 else if ( which
== wxIMAGE_LIST_SMALL
)
5481 if (m_ownsImageListSmall
)
5482 delete m_imageListSmall
;
5483 m_imageListSmall
= imageList
;
5484 m_ownsImageListSmall
= false;
5486 else if ( which
== wxIMAGE_LIST_STATE
)
5488 if (m_ownsImageListState
)
5489 delete m_imageListState
;
5490 m_imageListState
= imageList
;
5491 m_ownsImageListState
= false;
5494 m_mainWin
->SetImageList( imageList
, which
);
5497 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5499 SetImageList(imageList
, which
);
5500 if ( which
== wxIMAGE_LIST_NORMAL
)
5501 m_ownsImageListNormal
= true;
5502 else if ( which
== wxIMAGE_LIST_SMALL
)
5503 m_ownsImageListSmall
= true;
5504 else if ( which
== wxIMAGE_LIST_STATE
)
5505 m_ownsImageListState
= true;
5508 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5513 bool wxGenericListCtrl::DeleteItem( long item
)
5515 m_mainWin
->DeleteItem( item
);
5519 bool wxGenericListCtrl::DeleteAllItems()
5521 m_mainWin
->DeleteAllItems();
5525 bool wxGenericListCtrl::DeleteAllColumns()
5527 size_t count
= m_mainWin
->m_columns
.GetCount();
5528 for ( size_t n
= 0; n
< count
; n
++ )
5533 void wxGenericListCtrl::ClearAll()
5535 m_mainWin
->DeleteEverything();
5538 bool wxGenericListCtrl::DeleteColumn( int col
)
5540 m_mainWin
->DeleteColumn( col
);
5542 // if we don't have the header any longer, we need to relayout the window
5543 // if ( !GetColumnCount() )
5548 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5549 wxClassInfo
* textControlClass
)
5551 return m_mainWin
->EditLabel( item
, textControlClass
);
5554 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5556 return m_mainWin
->GetEditControl();
5559 bool wxGenericListCtrl::EnsureVisible( long item
)
5561 m_mainWin
->EnsureVisible( item
);
5565 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5567 return m_mainWin
->FindItem( start
, str
, partial
);
5570 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5572 return m_mainWin
->FindItem( start
, data
);
5575 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5576 int WXUNUSED(direction
))
5578 return m_mainWin
->FindItem( pt
);
5581 // TODO: sub item hit testing
5582 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5584 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5587 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5589 m_mainWin
->InsertItem( info
);
5590 return info
.m_itemId
;
5593 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5596 info
.m_text
= label
;
5597 info
.m_mask
= wxLIST_MASK_TEXT
;
5598 info
.m_itemId
= index
;
5599 return InsertItem( info
);
5602 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5605 info
.m_mask
= wxLIST_MASK_IMAGE
;
5606 info
.m_image
= imageIndex
;
5607 info
.m_itemId
= index
;
5608 return InsertItem( info
);
5611 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5614 info
.m_text
= label
;
5615 info
.m_image
= imageIndex
;
5616 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5617 info
.m_itemId
= index
;
5618 return InsertItem( info
);
5621 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5623 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5625 m_mainWin
->InsertColumn( col
, item
);
5627 // if we hadn't had a header before but have one now
5628 // then we need to relayout the window
5629 // if ( GetColumnCount() == 1 && m_mainWin->HasHeader() )
5631 m_headerWin
->Refresh();
5636 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5637 int format
, int width
)
5640 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5641 item
.m_text
= heading
;
5644 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5645 item
.m_width
= width
;
5648 item
.m_format
= format
;
5650 return InsertColumn( col
, item
);
5653 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
5655 return m_mainWin
->ScrollList(dx
, dy
);
5659 // fn is a function which takes 3 long arguments: item1, item2, data.
5660 // item1 is the long data associated with a first item (NOT the index).
5661 // item2 is the long data associated with a second item (NOT the index).
5662 // data is the same value as passed to SortItems.
5663 // The return value is a negative number if the first item should precede the second
5664 // item, a positive number of the second item should precede the first,
5665 // or zero if the two items are equivalent.
5666 // data is arbitrary data to be passed to the sort function.
5668 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5670 m_mainWin
->SortItems( fn
, data
);
5674 // ----------------------------------------------------------------------------
5676 // ----------------------------------------------------------------------------
5678 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5680 if (!m_mainWin
) return;
5682 // We need to override OnSize so that our scrolled
5683 // window a) does call Layout() to use sizers for
5684 // positioning the controls but b) does not query
5685 // the sizer for their size and use that for setting
5686 // the scrollable area as set that ourselves by
5687 // calling SetScrollbar() further down.
5691 m_mainWin
->RecalculatePositions();
5696 void wxGenericListCtrl::OnInternalIdle()
5698 wxWindow::OnInternalIdle();
5700 if (m_mainWin
->m_dirty
)
5701 m_mainWin
->RecalculatePositions();
5704 // ----------------------------------------------------------------------------
5706 // ----------------------------------------------------------------------------
5708 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5712 m_mainWin
->SetBackgroundColour( colour
);
5713 m_mainWin
->m_dirty
= true;
5719 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5721 if ( !wxWindow::SetForegroundColour( colour
) )
5726 m_mainWin
->SetForegroundColour( colour
);
5727 m_mainWin
->m_dirty
= true;
5731 m_headerWin
->SetForegroundColour( colour
);
5736 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5738 if ( !wxWindow::SetFont( font
) )
5743 m_mainWin
->SetFont( font
);
5744 m_mainWin
->m_dirty
= true;
5749 m_headerWin
->SetFont( font
);
5750 // CalculateAndSetHeaderHeight();
5760 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5763 // Use the same color scheme as wxListBox
5764 return wxListBox::GetClassDefaultAttributes(variant
);
5766 wxUnusedVar(variant
);
5767 wxVisualAttributes attr
;
5768 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5769 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5770 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5775 // ----------------------------------------------------------------------------
5776 // methods forwarded to m_mainWin
5777 // ----------------------------------------------------------------------------
5779 #if wxUSE_DRAG_AND_DROP
5781 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5783 m_mainWin
->SetDropTarget( dropTarget
);
5786 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5788 return m_mainWin
->GetDropTarget();
5793 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5795 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5798 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5800 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5803 wxColour
wxGenericListCtrl::GetForegroundColour() const
5805 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5808 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5811 return m_mainWin
->PopupMenu( menu
, x
, y
);
5817 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5819 m_mainWin
->DoClientToScreen(x
, y
);
5822 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5824 m_mainWin
->DoScreenToClient(x
, y
);
5827 void wxGenericListCtrl::SetFocus()
5829 // The test in window.cpp fails as we are a composite
5830 // window, so it checks against "this", but not m_mainWin.
5831 if ( DoFindFocus() != this )
5832 m_mainWin
->SetFocus();
5835 wxSize
wxGenericListCtrl::DoGetBestSize() const
5837 // Something is better than nothing...
5838 // 100x80 is what the MSW version will get from the default
5839 // wxControl::DoGetBestSize
5840 return wxSize(100, 80);
5843 // ----------------------------------------------------------------------------
5844 // virtual list control support
5845 // ----------------------------------------------------------------------------
5847 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5849 // this is a pure virtual function, in fact - which is not really pure
5850 // because the controls which are not virtual don't need to implement it
5851 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5853 return wxEmptyString
;
5856 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5858 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5860 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5864 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5867 return OnGetItemImage(item
);
5873 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5875 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5876 _T("invalid item index in OnGetItemAttr()") );
5878 // no attributes by default
5882 void wxGenericListCtrl::SetItemCount(long count
)
5884 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5886 m_mainWin
->SetItemCount(count
);
5889 void wxGenericListCtrl::RefreshItem(long item
)
5891 m_mainWin
->RefreshLine(item
);
5894 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5896 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5899 // Generic wxListCtrl is more or less a container for two other
5900 // windows which drawings are done upon. These are namely
5901 // 'm_headerWin' and 'm_mainWin'.
5902 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5903 // behaviour wxListCtrl has under wxMSW.
5905 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5909 // The easy case, no rectangle specified.
5911 m_headerWin
->Refresh(eraseBackground
);
5914 m_mainWin
->Refresh(eraseBackground
);
5918 // Refresh the header window
5921 wxRect rectHeader
= m_headerWin
->GetRect();
5922 rectHeader
.Intersect(*rect
);
5923 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5926 m_headerWin
->GetPosition(&x
, &y
);
5927 rectHeader
.Offset(-x
, -y
);
5928 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5932 // Refresh the main window
5935 wxRect rectMain
= m_mainWin
->GetRect();
5936 rectMain
.Intersect(*rect
);
5937 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5940 m_mainWin
->GetPosition(&x
, &y
);
5941 rectMain
.Offset(-x
, -y
);
5942 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5948 #endif // wxUSE_LISTCTRL