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"
48 #include "wx/imaglist.h"
49 #include "wx/selstore.h"
50 #include "wx/renderer.h"
53 #include "wx/osx/private.h"
54 // for themeing support
55 #include <Carbon/Carbon.h>
59 // NOTE: If using the wxListBox visual attributes works everywhere then this can
60 // be removed, as well as the #else case below.
61 #define _USE_VISATTR 0
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // // the height of the header window (FIXME: should depend on its font!)
69 // static const int HEADER_HEIGHT = 23;
71 static const int SCROLL_UNIT_X
= 15;
73 // the spacing between the lines (in report mode)
74 static const int LINE_SPACING
= 0;
76 // extra margins around the text label
78 static const int EXTRA_WIDTH
= 6;
80 static const int EXTRA_WIDTH
= 4;
84 static const int EXTRA_HEIGHT
= 6;
86 static const int EXTRA_HEIGHT
= 4;
89 // margin between the window and the items
90 static const int EXTRA_BORDER_X
= 2;
91 static const int EXTRA_BORDER_Y
= 2;
93 // offset for the header window
94 static const int HEADER_OFFSET_X
= 0;
95 static const int HEADER_OFFSET_Y
= 0;
97 // margin between rows of icons in [small] icon view
98 static const int MARGIN_BETWEEN_ROWS
= 6;
100 // when autosizing the columns, add some slack
101 static const int AUTOSIZE_COL_MARGIN
= 10;
103 // default width for the header columns
104 static const int WIDTH_COL_DEFAULT
= 80;
106 // the space between the image and the text in the report mode
107 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
109 // the space between the image and the text in the report mode in header
110 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
112 // ============================================================================
114 // ============================================================================
116 //-----------------------------------------------------------------------------
117 // wxColWidthInfo (internal)
118 //-----------------------------------------------------------------------------
120 struct wxColWidthInfo
123 bool bNeedsUpdate
; // only set to true when an item whose
124 // width == nMaxWidth is removed
126 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
129 bNeedsUpdate
= needsUpdate
;
133 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
135 //-----------------------------------------------------------------------------
136 // wxListItemData (internal)
137 //-----------------------------------------------------------------------------
142 wxListItemData(wxListMainWindow
*owner
);
145 void SetItem( const wxListItem
&info
);
146 void SetImage( int image
) { m_image
= image
; }
147 void SetData( wxUIntPtr data
) { m_data
= data
; }
148 void SetPosition( int x
, int y
);
149 void SetSize( int width
, int height
);
151 bool HasText() const { return !m_text
.empty(); }
152 const wxString
& GetText() const { return m_text
; }
153 void SetText(const wxString
& text
) { m_text
= text
; }
155 // we can't use empty string for measuring the string width/height, so
156 // always return something
157 wxString
GetTextForMeasuring() const
159 wxString s
= GetText();
166 bool IsHit( int x
, int y
) const;
170 int GetWidth() const;
171 int GetHeight() const;
173 int GetImage() const { return m_image
; }
174 bool HasImage() const { return GetImage() != -1; }
176 void GetItem( wxListItem
&info
) const;
178 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
179 wxListItemAttr
*GetAttr() const { return m_attr
; }
182 // the item image or -1
185 // user data associated with the item
188 // the item coordinates are not used in report mode; instead this pointer is
189 // NULL and the owner window is used to retrieve the item position and size
192 // the list ctrl we are in
193 wxListMainWindow
*m_owner
;
195 // custom attributes or NULL
196 wxListItemAttr
*m_attr
;
199 // common part of all ctors
205 //-----------------------------------------------------------------------------
206 // wxListHeaderData (internal)
207 //-----------------------------------------------------------------------------
209 class wxListHeaderData
: public wxObject
213 wxListHeaderData( const wxListItem
&info
);
214 void SetItem( const wxListItem
&item
);
215 void SetPosition( int x
, int y
);
216 void SetWidth( int w
);
217 void SetState( int state
);
218 void SetFormat( int format
);
219 void SetHeight( int h
);
220 bool HasImage() const;
222 bool HasText() const { return !m_text
.empty(); }
223 const wxString
& GetText() const { return m_text
; }
224 void SetText(const wxString
& text
) { m_text
= text
; }
226 void GetItem( wxListItem
&item
);
228 bool IsHit( int x
, int y
) const;
229 int GetImage() const;
230 int GetWidth() const;
231 int GetFormat() const;
232 int GetState() const;
249 //-----------------------------------------------------------------------------
250 // wxListLineData (internal)
251 //-----------------------------------------------------------------------------
253 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
254 #include "wx/listimpl.cpp"
255 WX_DEFINE_LIST(wxListItemDataList
)
260 // the list of subitems: only may have more than one item in report mode
261 wxListItemDataList m_items
;
263 // this is not used in report view
275 // the part to be highlighted
276 wxRect m_rectHighlight
;
278 // extend all our rects to be centered inside the one of given width
279 void ExtendWidth(wxCoord w
)
281 wxASSERT_MSG( m_rectAll
.width
<= w
,
282 _T("width can only be increased") );
285 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
286 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
287 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
292 // is this item selected? [NB: not used in virtual mode]
295 // back pointer to the list ctrl
296 wxListMainWindow
*m_owner
;
299 wxListLineData(wxListMainWindow
*owner
);
303 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
307 // are we in report mode?
308 inline bool InReportView() const;
310 // are we in virtual report mode?
311 inline bool IsVirtual() const;
313 // these 2 methods shouldn't be called for report view controls, in that
314 // case we determine our position/size ourselves
316 // calculate the size of the line
317 void CalculateSize( wxDC
*dc
, int spacing
);
319 // remember the position this line appears at
320 void SetPosition( int x
, int y
, int spacing
);
324 void SetImage( int image
) { SetImage(0, image
); }
325 int GetImage() const { return GetImage(0); }
326 void SetImage( int index
, int image
);
327 int GetImage( int index
) const;
329 bool HasImage() const { return GetImage() != -1; }
330 bool HasText() const { return !GetText(0).empty(); }
332 void SetItem( int index
, const wxListItem
&info
);
333 void GetItem( int index
, wxListItem
&info
);
335 wxString
GetText(int index
) const;
336 void SetText( int index
, const wxString
& s
);
338 wxListItemAttr
*GetAttr() const;
339 void SetAttr(wxListItemAttr
*attr
);
341 // return true if the highlighting really changed
342 bool Highlight( bool on
);
344 void ReverseHighlight();
346 bool IsHighlighted() const
348 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
350 return m_highlighted
;
353 // draw the line on the given DC in icon/list mode
354 void Draw( wxDC
*dc
);
356 // the same in report mode
357 void DrawInReportMode( wxDC
*dc
,
359 const wxRect
& rectHL
,
363 // set the line to contain num items (only can be > 1 in report mode)
364 void InitItems( int num
);
366 // get the mode (i.e. style) of the list control
367 inline int GetMode() const;
369 // prepare the DC for drawing with these item's attributes, return true if
370 // we need to draw the items background to highlight it, false otherwise
371 bool SetAttributes(wxDC
*dc
,
372 const wxListItemAttr
*attr
,
375 // draw the text on the DC with the correct justification; also add an
376 // ellipsis if the text is too large to fit in the current width
377 void DrawTextFormatted(wxDC
*dc
,
378 const wxString
&text
,
381 int yMid
, // this is middle, not top, of the text
385 WX_DECLARE_OBJARRAY(wxListLineData
, wxListLineDataArray
);
386 #include "wx/arrimpl.cpp"
387 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
389 //-----------------------------------------------------------------------------
390 // wxListHeaderWindow (internal)
391 //-----------------------------------------------------------------------------
393 class wxListHeaderWindow
: public wxWindow
396 wxListMainWindow
*m_owner
;
397 const wxCursor
*m_currentCursor
;
398 wxCursor
*m_resizeCursor
;
401 // column being resized or -1
404 // divider line position in logical (unscrolled) coords
407 // minimal position beyond which the divider line
408 // can't be dragged in logical coords
412 wxListHeaderWindow();
414 wxListHeaderWindow( wxWindow
*win
,
416 wxListMainWindow
*owner
,
417 const wxPoint
&pos
= wxDefaultPosition
,
418 const wxSize
&size
= wxDefaultSize
,
420 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
422 virtual ~wxListHeaderWindow();
425 void AdjustDC( wxDC
& dc
);
427 void OnPaint( wxPaintEvent
&event
);
428 void OnMouse( wxMouseEvent
&event
);
429 void OnSetFocus( wxFocusEvent
&event
);
434 // Update main window's column later
435 bool m_sendSetColumnWidth
;
439 virtual void OnInternalIdle();
442 // common part of all ctors
445 // generate and process the list event of the given type, return true if
446 // it wasn't vetoed, i.e. if we should proceed
447 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
449 DECLARE_EVENT_TABLE()
452 //-----------------------------------------------------------------------------
453 // wxListRenameTimer (internal)
454 //-----------------------------------------------------------------------------
456 class wxListRenameTimer
: public wxTimer
459 wxListMainWindow
*m_owner
;
462 wxListRenameTimer( wxListMainWindow
*owner
);
466 //-----------------------------------------------------------------------------
467 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
468 //-----------------------------------------------------------------------------
470 class wxListTextCtrlWrapper
: public wxEvtHandler
473 // NB: text must be a valid object but not Create()d yet
474 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
478 wxTextCtrl
*GetText() const { return m_text
; }
480 void EndEdit( bool discardChanges
);
483 void OnChar( wxKeyEvent
&event
);
484 void OnKeyUp( wxKeyEvent
&event
);
485 void OnKillFocus( wxFocusEvent
&event
);
487 bool AcceptChanges();
488 void Finish( bool setfocus
);
491 wxListMainWindow
*m_owner
;
493 wxString m_startValue
;
495 bool m_aboutToFinish
;
497 DECLARE_EVENT_TABLE()
500 //-----------------------------------------------------------------------------
501 // wxListMainWindow (internal)
502 //-----------------------------------------------------------------------------
504 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
505 #include "wx/listimpl.cpp"
506 WX_DEFINE_LIST(wxListHeaderDataList
)
508 class wxListMainWindow
: public wxWindow
512 wxListMainWindow( wxWindow
*parent
,
514 const wxPoint
& pos
= wxDefaultPosition
,
515 const wxSize
& size
= wxDefaultSize
,
517 const wxString
&name
= _T("listctrlmainwindow") );
519 virtual ~wxListMainWindow();
521 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
523 // return true if this is a virtual list control
524 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
526 // return true if the control is in report mode
527 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
529 // return true if we are in single selection mode, false if multi sel
530 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
532 // do we have a header window?
533 bool HasHeader() const
534 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
536 void HighlightAll( bool on
);
538 // all these functions only do something if the line is currently visible
540 // change the line "selected" state, return true if it really changed
541 bool HighlightLine( size_t line
, bool highlight
= true);
543 // as HighlightLine() but do it for the range of lines: this is incredibly
544 // more efficient for virtual list controls!
546 // NB: unlike HighlightLine() this one does refresh the lines on screen
547 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
549 // toggle the line state and refresh it
550 void ReverseHighlight( size_t line
)
551 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
553 // return true if the line is highlighted
554 bool IsHighlighted(size_t line
) const;
556 // refresh one or several lines at once
557 void RefreshLine( size_t line
);
558 void RefreshLines( size_t lineFrom
, size_t lineTo
);
560 // refresh all selected items
561 void RefreshSelected();
563 // refresh all lines below the given one: the difference with
564 // RefreshLines() is that the index here might not be a valid one (happens
565 // when the last line is deleted)
566 void RefreshAfter( size_t lineFrom
);
568 // the methods which are forwarded to wxListLineData itself in list/icon
569 // modes but are here because the lines don't store their positions in the
572 // get the bound rect for the entire line
573 wxRect
GetLineRect(size_t line
) const;
575 // get the bound rect of the label
576 wxRect
GetLineLabelRect(size_t line
) const;
578 // get the bound rect of the items icon (only may be called if we do have
580 wxRect
GetLineIconRect(size_t line
) const;
582 // get the rect to be highlighted when the item has focus
583 wxRect
GetLineHighlightRect(size_t line
) const;
585 // get the size of the total line rect
586 wxSize
GetLineSize(size_t line
) const
587 { return GetLineRect(line
).GetSize(); }
589 // return the hit code for the corresponding position (in this line)
590 long HitTestLine(size_t line
, int x
, int y
) const;
592 // bring the selected item into view, scrolling to it if necessary
593 void MoveToItem(size_t item
);
595 bool ScrollList( int WXUNUSED(dx
), int dy
);
597 // bring the current item into view
598 void MoveToFocus() { MoveToItem(m_current
); }
600 // start editing the label of the given item
601 wxTextCtrl
*EditLabel(long item
,
602 wxClassInfo
* textControlClass
= CLASSINFO(wxTextCtrl
));
603 wxTextCtrl
*GetEditControl() const
605 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
608 void ResetTextControl(wxTextCtrl
*text
)
611 m_textctrlWrapper
= NULL
;
614 void OnRenameTimer();
615 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
616 void OnRenameCancelled(size_t itemEdit
);
618 void OnMouse( wxMouseEvent
&event
);
620 // called to switch the selection from the current item to newCurrent,
621 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
623 void OnChar( wxKeyEvent
&event
);
624 void OnKeyDown( wxKeyEvent
&event
);
625 void OnKeyUp( wxKeyEvent
&event
);
626 void OnSetFocus( wxFocusEvent
&event
);
627 void OnKillFocus( wxFocusEvent
&event
);
628 void OnScroll( wxScrollWinEvent
& event
);
630 void OnPaint( wxPaintEvent
&event
);
632 void OnChildFocus(wxChildFocusEvent
& event
);
634 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
635 void GetImageSize( int index
, int &width
, int &height
) const;
636 int GetTextLength( const wxString
&s
) const;
638 void SetImageList( wxImageList
*imageList
, int which
);
639 void SetItemSpacing( int spacing
, bool isSmall
= false );
640 int GetItemSpacing( bool isSmall
= false );
642 void SetColumn( int col
, wxListItem
&item
);
643 void SetColumnWidth( int col
, int width
);
644 void GetColumn( int col
, wxListItem
&item
) const;
645 int GetColumnWidth( int col
) const;
646 int GetColumnCount() const { return m_columns
.GetCount(); }
648 // returns the sum of the heights of all columns
649 int GetHeaderWidth() const;
651 int GetCountPerPage() const;
653 void SetItem( wxListItem
&item
);
654 void GetItem( wxListItem
&item
) const;
655 void SetItemState( long item
, long state
, long stateMask
);
656 void SetItemStateAll( long state
, long stateMask
);
657 int GetItemState( long item
, long stateMask
) const;
658 bool GetItemRect( long item
, wxRect
&rect
) const
660 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
);
662 bool GetSubItemRect( long item
, long subItem
, wxRect
& rect
) const;
663 wxRect
GetViewRect() const;
664 bool GetItemPosition( long item
, wxPoint
& pos
) const;
665 int GetSelectedItemCount() const;
667 wxString
GetItemText(long item
) const
670 info
.m_mask
= wxLIST_MASK_TEXT
;
671 info
.m_itemId
= item
;
676 void SetItemText(long item
, const wxString
& value
)
679 info
.m_mask
= wxLIST_MASK_TEXT
;
680 info
.m_itemId
= item
;
685 // set the scrollbars and update the positions of the items
686 void RecalculatePositions(bool noRefresh
= false);
688 // refresh the window and the header
691 long GetNextItem( long item
, int geometry
, int state
) const;
692 void DeleteItem( long index
);
693 void DeleteAllItems();
694 void DeleteColumn( int col
);
695 void DeleteEverything();
696 void EnsureVisible( long index
);
697 long FindItem( long start
, const wxString
& str
, bool partial
= false );
698 long FindItem( long start
, wxUIntPtr data
);
699 long FindItem( const wxPoint
& pt
);
700 long HitTest( int x
, int y
, int &flags
) const;
701 void InsertItem( wxListItem
&item
);
702 void InsertColumn( long col
, wxListItem
&item
);
703 int GetItemWidthWithImage(wxListItem
* item
);
704 void SortItems( wxListCtrlCompare fn
, long data
);
706 size_t GetItemCount() const;
707 bool IsEmpty() const { return GetItemCount() == 0; }
708 void SetItemCount(long count
);
710 // change the current (== focused) item, send a notification event
711 void ChangeCurrent(size_t current
);
712 void ResetCurrent() { ChangeCurrent((size_t)-1); }
713 bool HasCurrent() const { return m_current
!= (size_t)-1; }
715 // send out a wxListEvent
716 void SendNotify( size_t line
,
718 const wxPoint
& point
= wxDefaultPosition
);
720 // override base class virtual to reset m_lineHeight when the font changes
721 virtual bool SetFont(const wxFont
& font
)
723 if ( !wxWindow::SetFont(font
) )
731 // these are for wxListLineData usage only
733 // get the backpointer to the list ctrl
734 wxGenericListCtrl
*GetListCtrl() const
736 return wxStaticCast(GetParent(), wxGenericListCtrl
);
739 // get the height of all lines (assuming they all do have the same height)
740 wxCoord
GetLineHeight() const;
742 // get the y position of the given line (only for report view)
743 wxCoord
GetLineY(size_t line
) const;
745 // get the brush to use for the item highlighting
746 wxBrush
*GetHighlightBrush() const
748 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
751 bool HasFocus() const
757 // the array of all line objects for a non virtual list control (for the
758 // virtual list control we only ever use m_lines[0])
759 wxListLineDataArray m_lines
;
761 // the list of column objects
762 wxListHeaderDataList m_columns
;
764 // currently focused item or -1
767 // the number of lines per page
770 // this flag is set when something which should result in the window
771 // redrawing happens (i.e. an item was added or deleted, or its appearance
772 // changed) and OnPaint() doesn't redraw the window while it is set which
773 // allows to minimize the number of repaintings when a lot of items are
774 // being added. The real repainting occurs only after the next OnIdle()
778 wxColour
*m_highlightColour
;
779 wxImageList
*m_small_image_list
;
780 wxImageList
*m_normal_image_list
;
782 int m_normal_spacing
;
786 wxTimer
*m_renameTimer
;
790 ColWidthArray m_aColWidths
;
792 // for double click logic
793 size_t m_lineLastClicked
,
794 m_lineBeforeLastClicked
,
795 m_lineSelectSingleOnUp
;
798 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
800 // the total count of items in a virtual list control
803 // the object maintaining the items selection state, only used in virtual
805 wxSelectionStore m_selStore
;
807 // common part of all ctors
810 // get the line data for the given index
811 wxListLineData
*GetLine(size_t n
) const
813 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
817 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
824 // get a dummy line which can be used for geometry calculations and such:
825 // you must use GetLine() if you want to really draw the line
826 wxListLineData
*GetDummyLine() const;
828 // cache the line data of the n-th line in m_lines[0]
829 void CacheLineData(size_t line
);
831 // get the range of visible lines
832 void GetVisibleLinesRange(size_t *from
, size_t *to
);
834 // force us to recalculate the range of visible lines
835 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
837 // get the colour to be used for drawing the rules
838 wxColour
GetRuleColour() const
840 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
844 // initialize the current item if needed
845 void UpdateCurrent();
847 // delete all items but don't refresh: called from dtor
848 void DoDeleteAllItems();
850 // the height of one line using the current font
851 wxCoord m_lineHeight
;
853 // the total header width or 0 if not calculated yet
854 wxCoord m_headerWidth
;
856 // the first and last lines being shown on screen right now (inclusive),
857 // both may be -1 if they must be calculated so never access them directly:
858 // use GetVisibleLinesRange() above instead
862 // the brushes to use for item highlighting when we do/don't have focus
863 wxBrush
*m_highlightBrush
,
864 *m_highlightUnfocusedBrush
;
866 // wrapper around the text control currently used for in place editing or
867 // NULL if no item is being edited
868 wxListTextCtrlWrapper
*m_textctrlWrapper
;
871 DECLARE_EVENT_TABLE()
873 friend class wxGenericListCtrl
;
877 wxListItemData::~wxListItemData()
879 // in the virtual list control the attributes are managed by the main
880 // program, so don't delete them
881 if ( !m_owner
->IsVirtual() )
887 void wxListItemData::Init()
895 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
901 if ( owner
->InReportView() )
907 void wxListItemData::SetItem( const wxListItem
&info
)
909 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
910 SetText(info
.m_text
);
911 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
912 m_image
= info
.m_image
;
913 if ( info
.m_mask
& wxLIST_MASK_DATA
)
914 m_data
= info
.m_data
;
916 if ( info
.HasAttributes() )
919 m_attr
->AssignFrom(*info
.GetAttributes());
921 m_attr
= new wxListItemAttr(*info
.GetAttributes());
929 m_rect
->width
= info
.m_width
;
933 void wxListItemData::SetPosition( int x
, int y
)
935 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
941 void wxListItemData::SetSize( int width
, int height
)
943 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
946 m_rect
->width
= width
;
948 m_rect
->height
= height
;
951 bool wxListItemData::IsHit( int x
, int y
) const
953 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
955 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
958 int wxListItemData::GetX() const
960 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
965 int wxListItemData::GetY() const
967 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
972 int wxListItemData::GetWidth() const
974 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
976 return m_rect
->width
;
979 int wxListItemData::GetHeight() const
981 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
983 return m_rect
->height
;
986 void wxListItemData::GetItem( wxListItem
&info
) const
988 long mask
= info
.m_mask
;
990 // by default, get everything for backwards compatibility
993 if ( mask
& wxLIST_MASK_TEXT
)
994 info
.m_text
= m_text
;
995 if ( mask
& wxLIST_MASK_IMAGE
)
996 info
.m_image
= m_image
;
997 if ( mask
& wxLIST_MASK_DATA
)
998 info
.m_data
= m_data
;
1002 if ( m_attr
->HasTextColour() )
1003 info
.SetTextColour(m_attr
->GetTextColour());
1004 if ( m_attr
->HasBackgroundColour() )
1005 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1006 if ( m_attr
->HasFont() )
1007 info
.SetFont(m_attr
->GetFont());
1011 //-----------------------------------------------------------------------------
1013 //-----------------------------------------------------------------------------
1015 void wxListHeaderData::Init()
1027 wxListHeaderData::wxListHeaderData()
1032 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1039 void wxListHeaderData::SetItem( const wxListItem
&item
)
1041 m_mask
= item
.m_mask
;
1043 if ( m_mask
& wxLIST_MASK_TEXT
)
1044 m_text
= item
.m_text
;
1046 if ( m_mask
& wxLIST_MASK_IMAGE
)
1047 m_image
= item
.m_image
;
1049 if ( m_mask
& wxLIST_MASK_FORMAT
)
1050 m_format
= item
.m_format
;
1052 if ( m_mask
& wxLIST_MASK_WIDTH
)
1053 SetWidth(item
.m_width
);
1055 if ( m_mask
& wxLIST_MASK_STATE
)
1056 SetState(item
.m_state
);
1059 void wxListHeaderData::SetPosition( int x
, int y
)
1065 void wxListHeaderData::SetHeight( int h
)
1070 void wxListHeaderData::SetWidth( int w
)
1072 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1075 void wxListHeaderData::SetState( int flag
)
1080 void wxListHeaderData::SetFormat( int format
)
1085 bool wxListHeaderData::HasImage() const
1087 return m_image
!= -1;
1090 bool wxListHeaderData::IsHit( int x
, int y
) const
1092 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1095 void wxListHeaderData::GetItem( wxListItem
& item
)
1097 item
.m_mask
= m_mask
;
1098 item
.m_text
= m_text
;
1099 item
.m_image
= m_image
;
1100 item
.m_format
= m_format
;
1101 item
.m_width
= m_width
;
1102 item
.m_state
= m_state
;
1105 int wxListHeaderData::GetImage() const
1110 int wxListHeaderData::GetWidth() const
1115 int wxListHeaderData::GetFormat() const
1120 int wxListHeaderData::GetState() const
1125 //-----------------------------------------------------------------------------
1127 //-----------------------------------------------------------------------------
1129 inline int wxListLineData::GetMode() const
1131 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1134 inline bool wxListLineData::InReportView() const
1136 return m_owner
->HasFlag(wxLC_REPORT
);
1139 inline bool wxListLineData::IsVirtual() const
1141 return m_owner
->IsVirtual();
1144 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1148 if ( InReportView() )
1151 m_gi
= new GeometryInfo
;
1153 m_highlighted
= false;
1155 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1158 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1160 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1161 wxCHECK_RET( node
, _T("no subitems at all??") );
1163 wxListItemData
*item
= node
->GetData();
1168 switch ( GetMode() )
1171 case wxLC_SMALL_ICON
:
1172 m_gi
->m_rectAll
.width
= spacing
;
1174 s
= item
->GetText();
1179 m_gi
->m_rectLabel
.width
=
1180 m_gi
->m_rectLabel
.height
= 0;
1184 dc
->GetTextExtent( s
, &lw
, &lh
);
1188 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1190 m_gi
->m_rectAll
.width
= lw
;
1192 m_gi
->m_rectLabel
.width
= lw
;
1193 m_gi
->m_rectLabel
.height
= lh
;
1196 if (item
->HasImage())
1199 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1200 m_gi
->m_rectIcon
.width
= w
+ 8;
1201 m_gi
->m_rectIcon
.height
= h
+ 8;
1203 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1204 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1205 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1206 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1209 if ( item
->HasText() )
1211 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1212 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1214 else // no text, highlight the icon
1216 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1217 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1222 s
= item
->GetTextForMeasuring();
1224 dc
->GetTextExtent( s
, &lw
, &lh
);
1228 m_gi
->m_rectLabel
.width
= lw
;
1229 m_gi
->m_rectLabel
.height
= lh
;
1231 m_gi
->m_rectAll
.width
= lw
;
1232 m_gi
->m_rectAll
.height
= lh
;
1234 if (item
->HasImage())
1237 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1238 m_gi
->m_rectIcon
.width
= w
;
1239 m_gi
->m_rectIcon
.height
= h
;
1241 m_gi
->m_rectAll
.width
+= 4 + w
;
1242 if (h
> m_gi
->m_rectAll
.height
)
1243 m_gi
->m_rectAll
.height
= h
;
1246 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1247 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1251 wxFAIL_MSG( _T("unexpected call to SetSize") );
1255 wxFAIL_MSG( _T("unknown mode") );
1260 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1262 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1263 wxCHECK_RET( node
, _T("no subitems at all??") );
1265 wxListItemData
*item
= node
->GetData();
1267 switch ( GetMode() )
1270 case wxLC_SMALL_ICON
:
1271 m_gi
->m_rectAll
.x
= x
;
1272 m_gi
->m_rectAll
.y
= y
;
1274 if ( item
->HasImage() )
1276 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1277 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1278 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1281 if ( item
->HasText() )
1283 if (m_gi
->m_rectAll
.width
> spacing
)
1284 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1286 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1287 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1288 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1289 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1291 else // no text, highlight the icon
1293 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1294 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1299 m_gi
->m_rectAll
.x
= x
;
1300 m_gi
->m_rectAll
.y
= y
;
1302 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1303 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1304 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1306 if (item
->HasImage())
1308 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1309 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1310 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1314 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1319 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1323 wxFAIL_MSG( _T("unknown mode") );
1328 void wxListLineData::InitItems( int num
)
1330 for (int i
= 0; i
< num
; i
++)
1331 m_items
.Append( new wxListItemData(m_owner
) );
1334 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1336 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1337 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1339 wxListItemData
*item
= node
->GetData();
1340 item
->SetItem( info
);
1343 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1345 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1348 wxListItemData
*item
= node
->GetData();
1349 item
->GetItem( info
);
1353 wxString
wxListLineData::GetText(int index
) const
1357 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1360 wxListItemData
*item
= node
->GetData();
1361 s
= item
->GetText();
1367 void wxListLineData::SetText( int index
, const wxString
& s
)
1369 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1372 wxListItemData
*item
= node
->GetData();
1377 void wxListLineData::SetImage( int index
, int image
)
1379 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1380 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1382 wxListItemData
*item
= node
->GetData();
1383 item
->SetImage(image
);
1386 int wxListLineData::GetImage( int index
) const
1388 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1389 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1391 wxListItemData
*item
= node
->GetData();
1392 return item
->GetImage();
1395 wxListItemAttr
*wxListLineData::GetAttr() const
1397 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1398 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1400 wxListItemData
*item
= node
->GetData();
1401 return item
->GetAttr();
1404 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1406 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1407 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1409 wxListItemData
*item
= node
->GetData();
1410 item
->SetAttr(attr
);
1413 bool wxListLineData::SetAttributes(wxDC
*dc
,
1414 const wxListItemAttr
*attr
,
1417 wxWindow
*listctrl
= m_owner
->GetParent();
1421 // don't use foreground colour for drawing highlighted items - this might
1422 // make them completely invisible (and there is no way to do bit
1423 // arithmetics on wxColour, unfortunately)
1428 if (m_owner
->HasFocus()
1429 #if !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
1430 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1438 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1440 else if ( attr
&& attr
->HasTextColour() )
1441 colText
= attr
->GetTextColour();
1443 colText
= listctrl
->GetForegroundColour();
1445 dc
->SetTextForeground(colText
);
1449 if ( attr
&& attr
->HasFont() )
1450 font
= attr
->GetFont();
1452 font
= listctrl
->GetFont();
1457 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1458 if ( highlighted
|| hasBgCol
)
1461 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1463 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
1465 dc
->SetPen( *wxTRANSPARENT_PEN
);
1473 void wxListLineData::Draw( wxDC
*dc
)
1475 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1476 wxCHECK_RET( node
, _T("no subitems at all??") );
1478 bool highlighted
= IsHighlighted();
1480 wxListItemAttr
*attr
= GetAttr();
1482 if ( SetAttributes(dc
, attr
, highlighted
) )
1483 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1485 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1491 int flags
= wxCONTROL_SELECTED
;
1492 if (m_owner
->HasFocus()
1493 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
1494 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1497 flags
|= wxCONTROL_FOCUSED
;
1498 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1503 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1508 // just for debugging to better see where the items are
1510 dc
->SetPen(*wxRED_PEN
);
1511 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1512 dc
->DrawRectangle( m_gi
->m_rectAll
);
1513 dc
->SetPen(*wxGREEN_PEN
);
1514 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1517 wxListItemData
*item
= node
->GetData();
1518 if (item
->HasImage())
1520 // centre the image inside our rectangle, this looks nicer when items
1521 // ae aligned in a row
1522 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1524 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1527 if (item
->HasText())
1529 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1531 wxDCClipper
clipper(*dc
, rectLabel
);
1532 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1536 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1538 const wxRect
& rectHL
,
1541 // TODO: later we should support setting different attributes for
1542 // different columns - to do it, just add "col" argument to
1543 // GetAttr() and move these lines into the loop below
1544 wxListItemAttr
*attr
= GetAttr();
1545 if ( SetAttributes(dc
, attr
, highlighted
) )
1546 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1548 dc
->DrawRectangle( rectHL
);
1554 int flags
= wxCONTROL_SELECTED
;
1555 if (m_owner
->HasFocus())
1556 flags
|= wxCONTROL_FOCUSED
;
1557 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1561 dc
->DrawRectangle( rectHL
);
1566 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1567 yMid
= rect
.y
+ rect
.height
/2;
1569 // This probably needs to be done
1570 // on all platforms as the icons
1571 // otherwise nearly touch the border
1576 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1578 node
= node
->GetNext(), col
++ )
1580 wxListItemData
*item
= node
->GetData();
1582 int width
= m_owner
->GetColumnWidth(col
);
1586 const int wText
= width
- 8;
1587 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
1589 if ( item
->HasImage() )
1592 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1593 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1595 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1601 if ( item
->HasText() )
1602 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, wText
);
1606 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1607 const wxString
& textOrig
,
1613 // we don't support displaying multiple lines currently (and neither does
1614 // wxMSW FWIW) so just merge all the lines
1615 wxString
text(textOrig
);
1616 text
.Replace(_T("\n"), _T(" "));
1619 dc
->GetTextExtent(text
, &w
, &h
);
1621 const wxCoord y
= yMid
- (h
+ 1)/2;
1623 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1625 // determine if the string can fit inside the current width
1628 // it can, draw it using the items alignment
1630 m_owner
->GetColumn(col
, item
);
1631 switch ( item
.GetAlign() )
1633 case wxLIST_FORMAT_LEFT
:
1637 case wxLIST_FORMAT_RIGHT
:
1641 case wxLIST_FORMAT_CENTER
:
1642 x
+= (width
- w
) / 2;
1646 wxFAIL_MSG( _T("unknown list item format") );
1650 dc
->DrawText(text
, x
, y
);
1652 else // otherwise, truncate and add an ellipsis if possible
1654 // determine the base width
1655 wxString
ellipsis(wxT("..."));
1657 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1659 // continue until we have enough space or only one character left
1661 size_t len
= text
.length();
1662 wxString drawntext
= text
.Left(len
);
1665 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1666 drawntext
.RemoveLast();
1669 if (w
+ base_w
<= width
)
1673 // if still not enough space, remove ellipsis characters
1674 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1676 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1677 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1680 // now draw the text
1681 dc
->DrawText(drawntext
, x
, y
);
1682 dc
->DrawText(ellipsis
, x
+ w
, y
);
1686 bool wxListLineData::Highlight( bool on
)
1688 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1690 if ( on
== m_highlighted
)
1698 void wxListLineData::ReverseHighlight( void )
1700 Highlight(!IsHighlighted());
1703 //-----------------------------------------------------------------------------
1704 // wxListHeaderWindow
1705 //-----------------------------------------------------------------------------
1707 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1708 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1709 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1710 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1713 void wxListHeaderWindow::Init()
1715 m_currentCursor
= NULL
;
1716 m_isDragging
= false;
1718 m_sendSetColumnWidth
= false;
1721 wxListHeaderWindow::wxListHeaderWindow()
1726 m_resizeCursor
= NULL
;
1729 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1731 wxListMainWindow
*owner
,
1735 const wxString
&name
)
1736 : wxWindow( win
, id
, pos
, size
, style
, name
)
1741 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1744 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1745 SetOwnForegroundColour( attr
.colFg
);
1746 SetOwnBackgroundColour( attr
.colBg
);
1748 SetOwnFont( attr
.font
);
1750 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1751 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1753 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1757 wxListHeaderWindow::~wxListHeaderWindow()
1759 delete m_resizeCursor
;
1762 #ifdef __WXUNIVERSAL__
1763 #include "wx/univ/renderer.h"
1764 #include "wx/univ/theme.h"
1767 // shift the DC origin to match the position of the main window horz
1768 // scrollbar: this allows us to always use logical coords
1769 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1771 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1774 parent
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1777 parent
->GetViewStart( &view_start
, NULL
);
1782 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1784 // account for the horz scrollbar offset
1786 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1788 // Maybe we just have to check for m_signX
1789 // in the DC, but I leave the #ifdef __WXGTK__
1791 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1795 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1798 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1800 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1802 wxPaintDC
dc( this );
1806 dc
.SetFont( GetFont() );
1808 // width and height of the entire header window
1810 GetClientSize( &w
, &h
);
1811 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1813 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1814 dc
.SetTextForeground(GetForegroundColour());
1816 int x
= HEADER_OFFSET_X
;
1817 int numColumns
= m_owner
->GetColumnCount();
1819 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1821 m_owner
->GetColumn( i
, item
);
1822 int wCol
= item
.m_width
;
1828 if (!m_parent
->IsEnabled())
1829 flags
|= wxCONTROL_DISABLED
;
1831 // NB: The code below is not really Mac-specific, but since we are close
1832 // to 2.8 release and I don't have time to test on other platforms, I
1833 // defined this only for wxMac. If this behavior is desired on
1834 // other platforms, please go ahead and revise or remove the #ifdef.
1836 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1837 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1838 flags
|= wxCONTROL_SELECTED
;
1842 flags
|= wxCONTROL_SPECIAL
; // mark as first column
1844 wxRendererNative::Get().DrawHeaderButton
1848 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1852 // see if we have enough space for the column label
1854 // for this we need the width of the text
1857 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1858 wLabel
+= 2 * EXTRA_WIDTH
;
1860 // and the width of the icon, if any
1861 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1862 const int image
= item
.m_image
;
1863 wxImageList
*imageList
;
1866 imageList
= m_owner
->m_small_image_list
;
1869 imageList
->GetSize(image
, ix
, iy
);
1870 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1878 // ignore alignment if there is not enough space anyhow
1880 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1883 wxFAIL_MSG( _T("unknown list item format") );
1886 case wxLIST_FORMAT_LEFT
:
1890 case wxLIST_FORMAT_RIGHT
:
1891 xAligned
= x
+ cw
- wLabel
;
1894 case wxLIST_FORMAT_CENTER
:
1895 xAligned
= x
+ (cw
- wLabel
) / 2;
1899 // draw the text and image clipping them so that they
1900 // don't overwrite the column boundary
1901 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1903 // if we have an image, draw it on the right of the label
1910 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1911 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1912 wxIMAGELIST_DRAW_TRANSPARENT
1916 dc
.DrawText( item
.GetText(),
1917 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1922 // Fill in what's missing to the right of the columns, otherwise we will
1923 // leave an unpainted area when columns are removed (and it looks better)
1926 wxRendererNative::Get().DrawHeaderButton
1930 wxRect(x
, HEADER_OFFSET_Y
, w
- x
, h
),
1931 wxCONTROL_DIRTY
// mark as last column
1936 void wxListHeaderWindow::OnInternalIdle()
1938 wxWindow::OnInternalIdle();
1940 if (m_sendSetColumnWidth
)
1942 m_owner
->SetColumnWidth( m_colToSend
, m_widthToSend
);
1943 m_sendSetColumnWidth
= false;
1947 void wxListHeaderWindow::DrawCurrent()
1950 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1951 m_sendSetColumnWidth
= true;
1952 m_colToSend
= m_column
;
1953 m_widthToSend
= m_currentX
- m_minX
;
1955 int x1
= m_currentX
;
1957 m_owner
->ClientToScreen( &x1
, &y1
);
1959 int x2
= m_currentX
;
1961 m_owner
->GetClientSize( NULL
, &y2
);
1962 m_owner
->ClientToScreen( &x2
, &y2
);
1965 dc
.SetLogicalFunction( wxINVERT
);
1966 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1967 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1971 dc
.DrawLine( x1
, y1
, x2
, y2
);
1973 dc
.SetLogicalFunction( wxCOPY
);
1975 dc
.SetPen( wxNullPen
);
1976 dc
.SetBrush( wxNullBrush
);
1980 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1982 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1984 // we want to work with logical coords
1986 parent
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1987 int y
= event
.GetY();
1991 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1993 // we don't draw the line beyond our window, but we allow dragging it
1996 GetClientSize( &w
, NULL
);
1997 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
2000 // erase the line if it was drawn
2001 if ( m_currentX
< w
)
2004 if (event
.ButtonUp())
2007 m_isDragging
= false;
2009 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
2010 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
2017 m_currentX
= m_minX
+ 7;
2019 // draw in the new location
2020 if ( m_currentX
< w
)
2024 else // not dragging
2027 bool hit_border
= false;
2029 // end of the current column
2032 // find the column where this event occurred
2034 countCol
= m_owner
->GetColumnCount();
2035 for (col
= 0; col
< countCol
; col
++)
2037 xpos
+= m_owner
->GetColumnWidth( col
);
2040 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2042 // near the column border
2049 // inside the column
2056 if ( col
== countCol
)
2059 if (event
.LeftDown() || event
.RightUp())
2061 if (hit_border
&& event
.LeftDown())
2063 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2064 event
.GetPosition()) )
2066 m_isDragging
= true;
2071 //else: column resizing was vetoed by the user code
2073 else // click on a column
2075 // record the selected state of the columns
2076 if (event
.LeftDown())
2078 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2081 m_owner
->GetColumn(i
, colItem
);
2082 long state
= colItem
.GetState();
2084 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2086 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2087 m_owner
->SetColumn(i
, colItem
);
2091 SendListEvent( event
.LeftDown()
2092 ? wxEVT_COMMAND_LIST_COL_CLICK
2093 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2094 event
.GetPosition());
2097 else if (event
.Moving())
2102 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2103 m_currentCursor
= m_resizeCursor
;
2107 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2108 m_currentCursor
= wxSTANDARD_CURSOR
;
2112 SetCursor(*m_currentCursor
);
2117 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2119 m_owner
->SetFocus();
2123 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2125 wxWindow
*parent
= GetParent();
2126 wxListEvent
le( type
, parent
->GetId() );
2127 le
.SetEventObject( parent
);
2128 le
.m_pointDrag
= pos
;
2130 // the position should be relative to the parent window, not
2131 // this one for compatibility with MSW and common sense: the
2132 // user code doesn't know anything at all about this header
2133 // window, so why should it get positions relative to it?
2134 le
.m_pointDrag
.y
-= GetSize().y
;
2136 le
.m_col
= m_column
;
2137 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2140 //-----------------------------------------------------------------------------
2141 // wxListRenameTimer (internal)
2142 //-----------------------------------------------------------------------------
2144 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2149 void wxListRenameTimer::Notify()
2151 m_owner
->OnRenameTimer();
2154 //-----------------------------------------------------------------------------
2155 // wxListTextCtrlWrapper (internal)
2156 //-----------------------------------------------------------------------------
2158 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2159 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2160 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2161 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2164 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2167 : m_startValue(owner
->GetItemText(itemEdit
)),
2168 m_itemEdited(itemEdit
)
2172 m_aboutToFinish
= false;
2174 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
2176 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2178 parent
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2179 &rectLabel
.x
, &rectLabel
.y
);
2181 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2182 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2183 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2186 m_text
->PushEventHandler(this);
2189 void wxListTextCtrlWrapper::EndEdit(bool discardChanges
)
2191 m_aboutToFinish
= true;
2193 if ( discardChanges
)
2195 m_owner
->OnRenameCancelled(m_itemEdited
);
2201 // Notify the owner about the changes
2204 // Even if vetoed, close the control (consistent with MSW)
2209 void wxListTextCtrlWrapper::Finish( bool setfocus
)
2211 m_text
->RemoveEventHandler(this);
2212 m_owner
->ResetTextControl( m_text
);
2214 wxPendingDelete
.Append( this );
2217 m_owner
->SetFocus();
2220 bool wxListTextCtrlWrapper::AcceptChanges()
2222 const wxString value
= m_text
->GetValue();
2224 // notice that we should always call OnRenameAccept() to generate the "end
2225 // label editing" event, even if the user hasn't really changed anything
2226 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2228 // vetoed by the user
2232 // accepted, do rename the item (unless nothing changed)
2233 if ( value
!= m_startValue
)
2234 m_owner
->SetItemText(m_itemEdited
, value
);
2239 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2241 switch ( event
.m_keyCode
)
2256 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2258 if (m_aboutToFinish
)
2260 // auto-grow the textctrl:
2261 wxSize parentSize
= m_owner
->GetSize();
2262 wxPoint myPos
= m_text
->GetPosition();
2263 wxSize mySize
= m_text
->GetSize();
2265 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2266 if (myPos
.x
+ sx
> parentSize
.x
)
2267 sx
= parentSize
.x
- myPos
.x
;
2270 m_text
->SetSize(sx
, wxDefaultCoord
);
2276 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2278 if ( !m_aboutToFinish
)
2280 if ( !AcceptChanges() )
2281 m_owner
->OnRenameCancelled( m_itemEdited
);
2286 // We must let the native text control handle focus
2290 //-----------------------------------------------------------------------------
2292 //-----------------------------------------------------------------------------
2294 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledCanvas
)
2295 EVT_PAINT (wxListMainWindow::OnPaint
)
2296 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2297 EVT_CHAR (wxListMainWindow::OnChar
)
2298 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2299 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
2300 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2301 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2302 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2303 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus
)
2306 void wxListMainWindow::Init()
2311 m_lineTo
= (size_t)-1;
2317 m_small_image_list
= NULL
;
2318 m_normal_image_list
= NULL
;
2320 m_small_spacing
= 30;
2321 m_normal_spacing
= 40;
2325 m_isCreated
= false;
2327 m_lastOnSame
= false;
2328 m_renameTimer
= new wxListRenameTimer( this );
2329 m_textctrlWrapper
= NULL
;
2333 m_lineSelectSingleOnUp
=
2334 m_lineBeforeLastClicked
= (size_t)-1;
2337 wxListMainWindow::wxListMainWindow()
2342 m_highlightUnfocusedBrush
= NULL
;
2345 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2350 const wxString
&name
)
2351 : wxWindow( parent
, id
, pos
, size
, style
, name
)
2355 m_highlightBrush
= new wxBrush
2357 wxSystemSettings::GetColour
2359 wxSYS_COLOUR_HIGHLIGHT
2364 m_highlightUnfocusedBrush
= new wxBrush
2366 wxSystemSettings::GetColour
2368 wxSYS_COLOUR_BTNSHADOW
2373 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2374 SetOwnForegroundColour( attr
.colFg
);
2375 SetOwnBackgroundColour( attr
.colBg
);
2377 SetOwnFont( attr
.font
);
2380 wxListMainWindow::~wxListMainWindow()
2383 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2384 WX_CLEAR_ARRAY(m_aColWidths
);
2386 delete m_highlightBrush
;
2387 delete m_highlightUnfocusedBrush
;
2388 delete m_renameTimer
;
2391 void wxListMainWindow::CacheLineData(size_t line
)
2393 wxGenericListCtrl
*listctrl
= GetListCtrl();
2395 wxListLineData
*ld
= GetDummyLine();
2397 size_t countCol
= GetColumnCount();
2398 for ( size_t col
= 0; col
< countCol
; col
++ )
2400 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2401 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2404 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2407 wxListLineData
*wxListMainWindow::GetDummyLine() const
2409 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2410 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2412 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2414 // we need to recreate the dummy line if the number of columns in the
2415 // control changed as it would have the incorrect number of fields
2417 if ( !m_lines
.IsEmpty() &&
2418 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2420 self
->m_lines
.Clear();
2423 if ( m_lines
.IsEmpty() )
2425 wxListLineData
*line
= new wxListLineData(self
);
2426 self
->m_lines
.Add(line
);
2428 // don't waste extra memory -- there never going to be anything
2429 // else/more in this array
2430 self
->m_lines
.Shrink();
2436 // ----------------------------------------------------------------------------
2437 // line geometry (report mode only)
2438 // ----------------------------------------------------------------------------
2440 wxCoord
wxListMainWindow::GetLineHeight() const
2442 // we cache the line height as calling GetTextExtent() is slow
2443 if ( !m_lineHeight
)
2445 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2447 wxClientDC
dc( self
);
2448 dc
.SetFont( GetFont() );
2451 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2453 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2456 m_small_image_list
->GetSize(0, iw
, ih
);
2461 self
->m_lineHeight
= y
+ LINE_SPACING
;
2464 return m_lineHeight
;
2467 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2469 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2471 return LINE_SPACING
+ line
* GetLineHeight();
2474 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2476 if ( !InReportView() )
2477 return GetLine(line
)->m_gi
->m_rectAll
;
2480 rect
.x
= HEADER_OFFSET_X
;
2481 rect
.y
= GetLineY(line
);
2482 rect
.width
= GetHeaderWidth();
2483 rect
.height
= GetLineHeight();
2488 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2490 if ( !InReportView() )
2491 return GetLine(line
)->m_gi
->m_rectLabel
;
2494 wxListLineData
*data
= GetLine(line
);
2495 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
2498 wxListItemData
*item
= node
->GetData();
2499 if ( item
->HasImage() )
2502 GetImageSize( item
->GetImage(), ix
, iy
);
2503 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
2508 rect
.x
= image_x
+ HEADER_OFFSET_X
;
2509 rect
.y
= GetLineY(line
);
2510 rect
.width
= GetColumnWidth(0) - image_x
;
2511 rect
.height
= GetLineHeight();
2516 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2518 if ( !InReportView() )
2519 return GetLine(line
)->m_gi
->m_rectIcon
;
2521 wxListLineData
*ld
= GetLine(line
);
2522 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2525 rect
.x
= HEADER_OFFSET_X
;
2526 rect
.y
= GetLineY(line
);
2527 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2532 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2534 return InReportView() ? GetLineRect(line
)
2535 : GetLine(line
)->m_gi
->m_rectHighlight
;
2538 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2540 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2542 wxListLineData
*ld
= GetLine(line
);
2544 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2545 return wxLIST_HITTEST_ONITEMICON
;
2547 // VS: Testing for "ld->HasText() || InReportView()" instead of
2548 // "ld->HasText()" is needed to make empty lines in report view
2550 if ( ld
->HasText() || InReportView() )
2552 wxRect rect
= InReportView() ? GetLineRect(line
)
2553 : GetLineLabelRect(line
);
2555 if ( rect
.Contains(x
, y
) )
2556 return wxLIST_HITTEST_ONITEMLABEL
;
2562 // ----------------------------------------------------------------------------
2563 // highlight (selection) handling
2564 // ----------------------------------------------------------------------------
2566 bool wxListMainWindow::IsHighlighted(size_t line
) const
2570 return m_selStore
.IsSelected(line
);
2574 wxListLineData
*ld
= GetLine(line
);
2575 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2577 return ld
->IsHighlighted();
2581 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2587 wxArrayInt linesChanged
;
2588 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2591 // meny items changed state, refresh everything
2592 RefreshLines(lineFrom
, lineTo
);
2594 else // only a few items changed state, refresh only them
2596 size_t count
= linesChanged
.GetCount();
2597 for ( size_t n
= 0; n
< count
; n
++ )
2599 RefreshLine(linesChanged
[n
]);
2603 else // iterate over all items in non report view
2605 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2607 if ( HighlightLine(line
, highlight
) )
2613 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2619 changed
= m_selStore
.SelectItem(line
, highlight
);
2623 wxListLineData
*ld
= GetLine(line
);
2624 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2626 changed
= ld
->Highlight(highlight
);
2631 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2632 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2638 void wxListMainWindow::RefreshLine( size_t line
)
2640 if ( InReportView() )
2642 size_t visibleFrom
, visibleTo
;
2643 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2645 if ( line
< visibleFrom
|| line
> visibleTo
)
2649 wxRect rect
= GetLineRect(line
);
2651 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2652 RefreshRect( rect
);
2655 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2657 // we suppose that they are ordered by caller
2658 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2660 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2662 if ( InReportView() )
2664 size_t visibleFrom
, visibleTo
;
2665 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2667 if ( lineFrom
< visibleFrom
)
2668 lineFrom
= visibleFrom
;
2669 if ( lineTo
> visibleTo
)
2674 rect
.y
= GetLineY(lineFrom
);
2675 rect
.width
= GetClientSize().x
;
2676 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2678 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2679 RefreshRect( rect
);
2683 // TODO: this should be optimized...
2684 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2691 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2693 if ( InReportView() )
2695 size_t visibleFrom
, visibleTo
;
2696 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2698 if ( lineFrom
< visibleFrom
)
2699 lineFrom
= visibleFrom
;
2700 else if ( lineFrom
> visibleTo
)
2705 rect
.y
= GetLineY(lineFrom
);
2706 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2708 wxSize size
= GetClientSize();
2709 rect
.width
= size
.x
;
2711 // refresh till the bottom of the window
2712 rect
.height
= size
.y
- rect
.y
;
2714 RefreshRect( rect
);
2718 // TODO: how to do it more efficiently?
2723 void wxListMainWindow::RefreshSelected()
2729 if ( InReportView() )
2731 GetVisibleLinesRange(&from
, &to
);
2736 to
= GetItemCount() - 1;
2739 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2740 RefreshLine(m_current
);
2742 for ( size_t line
= from
; line
<= to
; line
++ )
2744 // NB: the test works as expected even if m_current == -1
2745 if ( line
!= m_current
&& IsHighlighted(line
) )
2750 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2752 // Note: a wxPaintDC must be constructed even if no drawing is
2753 // done (a Windows requirement).
2754 wxPaintDC
dc( this );
2758 // nothing to draw or not the moment to draw it
2763 RecalculatePositions( false );
2765 GetListCtrl()->PrepareDC( dc
);
2768 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2770 dc
.SetFont( GetFont() );
2772 if ( InReportView() )
2774 int lineHeight
= GetLineHeight();
2776 size_t visibleFrom
, visibleTo
;
2777 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2780 int xOrig
= dc
.LogicalToDeviceX( 0 );
2781 int yOrig
= dc
.LogicalToDeviceY( 0 );
2783 // tell the caller cache to cache the data
2786 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2787 GetParent()->GetId());
2788 evCache
.SetEventObject( GetParent() );
2789 evCache
.m_oldItemIndex
= visibleFrom
;
2790 evCache
.m_itemIndex
= visibleTo
;
2791 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2794 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2796 rectLine
= GetLineRect(line
);
2799 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2800 rectLine
.width
, rectLine
.height
) )
2802 // don't redraw unaffected lines to avoid flicker
2806 GetLine(line
)->DrawInReportMode( &dc
,
2808 GetLineHighlightRect(line
),
2809 IsHighlighted(line
) );
2812 if ( HasFlag(wxLC_HRULES
) )
2814 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2815 wxSize clientSize
= GetClientSize();
2817 size_t i
= visibleFrom
;
2818 if (i
== 0) i
= 1; // Don't draw the first one
2819 for ( ; i
<= visibleTo
; i
++ )
2822 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2823 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2824 clientSize
.x
- dev_x
, i
* lineHeight
);
2827 // Draw last horizontal rule
2828 if ( visibleTo
== GetItemCount() - 1 )
2831 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2832 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2833 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2837 // Draw vertical rules if required
2838 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2840 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2841 wxRect firstItemRect
, lastItemRect
;
2843 GetItemRect(visibleFrom
, firstItemRect
);
2844 GetItemRect(visibleTo
, lastItemRect
);
2845 int x
= firstItemRect
.GetX();
2847 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2849 for (int col
= 0; col
< GetColumnCount(); col
++)
2851 int colWidth
= GetColumnWidth(col
);
2853 int x_pos
= x
- dev_x
;
2854 if (col
< GetColumnCount()-1) x_pos
-= 2;
2855 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2856 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2862 size_t count
= GetItemCount();
2863 for ( size_t i
= 0; i
< count
; i
++ )
2865 GetLine(i
)->Draw( &dc
);
2870 // Don't draw rect outline under Mac at all.
2875 wxRect
rect( GetLineHighlightRect( m_current
) );
2877 dc
.SetPen( *wxBLACK_PEN
);
2878 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2879 dc
.DrawRectangle( rect
);
2881 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2889 void wxListMainWindow::HighlightAll( bool on
)
2891 if ( IsSingleSel() )
2893 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2895 // we just have one item to turn off
2896 if ( HasCurrent() && IsHighlighted(m_current
) )
2898 HighlightLine(m_current
, false);
2899 RefreshLine(m_current
);
2902 else // multi selection
2905 HighlightLines(0, GetItemCount() - 1, on
);
2909 void wxListMainWindow::OnChildFocus(wxChildFocusEvent
& WXUNUSED(event
))
2911 // Do nothing here. This prevents the default handler in wxScrolledWindow
2912 // from needlessly scrolling the window when the edit control is
2913 // dismissed. See ticket #9563.
2916 void wxListMainWindow::SendNotify( size_t line
,
2917 wxEventType command
,
2918 const wxPoint
& point
)
2920 wxListEvent
le( command
, GetParent()->GetId() );
2921 le
.SetEventObject( GetParent() );
2923 le
.m_itemIndex
= line
;
2925 // set only for events which have position
2926 if ( point
!= wxDefaultPosition
)
2927 le
.m_pointDrag
= point
;
2929 // don't try to get the line info for virtual list controls: the main
2930 // program has it anyhow and if we did it would result in accessing all
2931 // the lines, even those which are not visible now and this is precisely
2932 // what we're trying to avoid
2935 if ( line
!= (size_t)-1 )
2937 GetLine(line
)->GetItem( 0, le
.m_item
);
2939 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2941 //else: there may be no more such item
2943 GetParent()->GetEventHandler()->ProcessEvent( le
);
2946 void wxListMainWindow::ChangeCurrent(size_t current
)
2948 m_current
= current
;
2950 // as the current item changed, we shouldn't start editing it when the
2951 // "slow click" timer expires as the click happened on another item
2952 if ( m_renameTimer
->IsRunning() )
2953 m_renameTimer
->Stop();
2955 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2958 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2960 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2961 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2963 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2964 wxT("EditLabel() needs a text control") );
2966 size_t itemEdit
= (size_t)item
;
2968 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2969 le
.SetEventObject( GetParent() );
2970 le
.m_itemIndex
= item
;
2971 wxListLineData
*data
= GetLine(itemEdit
);
2972 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2973 data
->GetItem( 0, le
.m_item
);
2975 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2977 // vetoed by user code
2981 // We have to call this here because the label in question might just have
2982 // been added and no screen update taken place.
2987 // Pending events dispatched by wxSafeYield might have changed the item
2989 if ( (size_t)item
>= GetItemCount() )
2993 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2994 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2995 return m_textctrlWrapper
->GetText();
2998 void wxListMainWindow::OnRenameTimer()
3000 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
3002 EditLabel( m_current
);
3005 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
3007 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
3008 le
.SetEventObject( GetParent() );
3009 le
.m_itemIndex
= itemEdit
;
3011 wxListLineData
*data
= GetLine(itemEdit
);
3013 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
3015 data
->GetItem( 0, le
.m_item
);
3016 le
.m_item
.m_text
= value
;
3017 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
3021 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
3023 // let owner know that the edit was cancelled
3024 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
3026 le
.SetEditCanceled(true);
3028 le
.SetEventObject( GetParent() );
3029 le
.m_itemIndex
= itemEdit
;
3031 wxListLineData
*data
= GetLine(itemEdit
);
3032 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
3034 data
->GetItem( 0, le
.m_item
);
3035 GetEventHandler()->ProcessEvent( le
);
3038 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
3042 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
3043 // shutdown the edit control when the mouse is clicked elsewhere on the
3044 // listctrl because the order of events is different (or something like
3045 // that), so explicitly end the edit if it is active.
3046 if ( event
.LeftDown() && m_textctrlWrapper
)
3047 m_textctrlWrapper
->EndEdit( false );
3050 if ( event
.LeftDown() )
3053 event
.SetEventObject( GetParent() );
3054 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3057 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3059 // let the base handle mouse wheel events.
3064 if ( !HasCurrent() || IsEmpty() )
3066 if (event
.RightDown())
3068 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3070 wxContextMenuEvent
evtCtx(
3072 GetParent()->GetId(),
3073 ClientToScreen(event
.GetPosition()));
3074 evtCtx
.SetEventObject(GetParent());
3075 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3083 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3084 event
.ButtonDClick()) )
3087 int x
= event
.GetX();
3088 int y
= event
.GetY();
3089 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3091 // where did we hit it (if we did)?
3094 size_t count
= GetItemCount(),
3097 if ( InReportView() )
3099 current
= y
/ GetLineHeight();
3100 if ( current
< count
)
3101 hitResult
= HitTestLine(current
, x
, y
);
3105 // TODO: optimize it too! this is less simple than for report view but
3106 // enumerating all items is still not a way to do it!!
3107 for ( current
= 0; current
< count
; current
++ )
3109 hitResult
= HitTestLine(current
, x
, y
);
3115 if (event
.Dragging())
3117 if (m_dragCount
== 0)
3119 // we have to report the raw, physical coords as we want to be
3120 // able to call HitTest(event.m_pointDrag) from the user code to
3121 // get the item being dragged
3122 m_dragStart
= event
.GetPosition();
3127 if (m_dragCount
!= 3)
3130 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3131 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3133 wxListEvent
le( command
, GetParent()->GetId() );
3134 le
.SetEventObject( GetParent() );
3135 le
.m_itemIndex
= m_lineLastClicked
;
3136 le
.m_pointDrag
= m_dragStart
;
3137 GetParent()->GetEventHandler()->ProcessEvent( le
);
3148 // outside of any item
3149 if (event
.RightDown())
3151 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3153 wxContextMenuEvent
evtCtx(
3155 GetParent()->GetId(),
3156 ClientToScreen(event
.GetPosition()));
3157 evtCtx
.SetEventObject(GetParent());
3158 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3162 // reset the selection and bail out
3163 HighlightAll(false);
3169 bool forceClick
= false;
3170 if (event
.ButtonDClick())
3172 if ( m_renameTimer
->IsRunning() )
3173 m_renameTimer
->Stop();
3175 m_lastOnSame
= false;
3177 if ( current
== m_lineLastClicked
)
3179 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3185 // The first click was on another item, so don't interpret this as
3186 // a double click, but as a simple click instead
3193 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3195 // select single line
3196 HighlightAll( false );
3197 ReverseHighlight(m_lineSelectSingleOnUp
);
3202 if ((current
== m_current
) &&
3203 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3204 HasFlag(wxLC_EDIT_LABELS
) )
3206 if ( !InReportView() ||
3207 GetLineLabelRect(current
).Contains(x
, y
) )
3209 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
3210 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
3215 m_lastOnSame
= false;
3216 m_lineSelectSingleOnUp
= (size_t)-1;
3220 // This is necessary, because after a DnD operation in
3221 // from and to ourself, the up event is swallowed by the
3222 // DnD code. So on next non-up event (which means here and
3223 // now) m_lineSelectSingleOnUp should be reset.
3224 m_lineSelectSingleOnUp
= (size_t)-1;
3226 if (event
.RightDown())
3228 m_lineBeforeLastClicked
= m_lineLastClicked
;
3229 m_lineLastClicked
= current
;
3231 // If the item is already selected, do not update the selection.
3232 // Multi-selections should not be cleared if a selected item is clicked.
3233 if (!IsHighlighted(current
))
3235 HighlightAll(false);
3236 ChangeCurrent(current
);
3237 ReverseHighlight(m_current
);
3240 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3242 // Allow generation of context menu event
3245 else if (event
.MiddleDown())
3247 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3249 else if ( event
.LeftDown() || forceClick
)
3251 m_lineBeforeLastClicked
= m_lineLastClicked
;
3252 m_lineLastClicked
= current
;
3254 size_t oldCurrent
= m_current
;
3255 bool oldWasSelected
= IsHighlighted(m_current
);
3257 bool cmdModifierDown
= event
.CmdDown();
3258 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3260 if ( IsSingleSel() || !IsHighlighted(current
) )
3262 HighlightAll( false );
3264 ChangeCurrent(current
);
3266 ReverseHighlight(m_current
);
3268 else // multi sel & current is highlighted & no mod keys
3270 m_lineSelectSingleOnUp
= current
;
3271 ChangeCurrent(current
); // change focus
3274 else // multi sel & either ctrl or shift is down
3276 if (cmdModifierDown
)
3278 ChangeCurrent(current
);
3280 ReverseHighlight(m_current
);
3282 else if (event
.ShiftDown())
3284 ChangeCurrent(current
);
3286 size_t lineFrom
= oldCurrent
,
3289 if ( lineTo
< lineFrom
)
3292 lineFrom
= m_current
;
3295 HighlightLines(lineFrom
, lineTo
);
3297 else // !ctrl, !shift
3299 // test in the enclosing if should make it impossible
3300 wxFAIL_MSG( _T("how did we get here?") );
3304 if (m_current
!= oldCurrent
)
3305 RefreshLine( oldCurrent
);
3307 // forceClick is only set if the previous click was on another item
3308 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3312 void wxListMainWindow::MoveToItem(size_t item
)
3314 if ( item
== (size_t)-1 )
3317 wxRect rect
= GetLineRect(item
);
3319 int client_w
, client_h
;
3320 GetClientSize( &client_w
, &client_h
);
3322 const int hLine
= GetLineHeight();
3324 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3325 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3327 if ( InReportView() )
3329 // the next we need the range of lines shown it might be different,
3330 // so recalculate it
3331 ResetVisibleLinesRange();
3333 if (rect
.y
< view_y
)
3334 GetListCtrl()->Scroll( -1, rect
.y
/ hLine
);
3335 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3336 GetListCtrl()->Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3339 // At least on Mac the visible lines value will get reset inside of
3340 // Scroll *before* it actually scrolls the window because of the
3341 // Update() that happens there, so it will still have the wrong value.
3342 // So let's reset it again and wait for it to be recalculated in the
3343 // next paint event. I would expect this problem to show up in wxGTK
3344 // too but couldn't duplicate it there. Perhaps the order of events
3345 // is different... --Robin
3346 ResetVisibleLinesRange();
3354 if (rect
.x
-view_x
< 5)
3355 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
3356 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3357 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
3359 if (rect
.y
-view_y
< 5)
3360 sy
= (rect
.y
- 5) / hLine
;
3361 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
3362 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
3364 GetListCtrl()->Scroll(sx
, sy
);
3368 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
3370 if ( !InReportView() )
3372 // TODO: this should work in all views but is not implemented now
3377 GetVisibleLinesRange(&top
, &bottom
);
3379 if ( bottom
== (size_t)-1 )
3382 ResetVisibleLinesRange();
3384 int hLine
= GetLineHeight();
3386 GetListCtrl()->Scroll(-1, top
+ dy
/ hLine
);
3389 // see comment in MoveToItem() for why we do this
3390 ResetVisibleLinesRange();
3396 // ----------------------------------------------------------------------------
3397 // keyboard handling
3398 // ----------------------------------------------------------------------------
3400 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3402 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3403 _T("invalid item index in OnArrowChar()") );
3405 size_t oldCurrent
= m_current
;
3407 // in single selection we just ignore Shift as we can't select several
3409 if ( event
.ShiftDown() && !IsSingleSel() )
3411 ChangeCurrent(newCurrent
);
3413 // refresh the old focus to remove it
3414 RefreshLine( oldCurrent
);
3416 // select all the items between the old and the new one
3417 if ( oldCurrent
> newCurrent
)
3419 newCurrent
= oldCurrent
;
3420 oldCurrent
= m_current
;
3423 HighlightLines(oldCurrent
, newCurrent
);
3427 // all previously selected items are unselected unless ctrl is held
3428 // in a multiselection control
3429 if ( !event
.ControlDown() || IsSingleSel() )
3430 HighlightAll(false);
3432 ChangeCurrent(newCurrent
);
3434 // refresh the old focus to remove it
3435 RefreshLine( oldCurrent
);
3437 // in single selection mode we must always have a selected item
3438 if ( !event
.ControlDown() || IsSingleSel() )
3439 HighlightLine( m_current
, true );
3442 RefreshLine( m_current
);
3447 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3449 wxWindow
*parent
= GetParent();
3451 // propagate the key event upwards
3452 wxKeyEvent
ke(event
);
3453 ke
.SetEventObject( parent
);
3454 if (parent
->GetEventHandler()->ProcessEvent( ke
))
3460 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3462 wxWindow
*parent
= GetParent();
3464 // propagate the key event upwards
3465 wxKeyEvent
ke(event
);
3466 if (parent
->GetEventHandler()->ProcessEvent( ke
))
3472 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3474 wxWindow
*parent
= GetParent();
3476 // send a list_key event up
3479 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3480 le
.m_itemIndex
= m_current
;
3481 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3482 le
.m_code
= event
.GetKeyCode();
3483 le
.SetEventObject( parent
);
3484 parent
->GetEventHandler()->ProcessEvent( le
);
3487 if ( (event
.GetKeyCode() != WXK_UP
) &&
3488 (event
.GetKeyCode() != WXK_DOWN
) &&
3489 (event
.GetKeyCode() != WXK_RIGHT
) &&
3490 (event
.GetKeyCode() != WXK_LEFT
) &&
3491 (event
.GetKeyCode() != WXK_PAGEUP
) &&
3492 (event
.GetKeyCode() != WXK_PAGEDOWN
) &&
3493 (event
.GetKeyCode() != WXK_END
) &&
3494 (event
.GetKeyCode() != WXK_HOME
) )
3496 // propagate the char event upwards
3497 wxKeyEvent
ke(event
);
3498 ke
.SetEventObject( parent
);
3499 if (parent
->GetEventHandler()->ProcessEvent( ke
))
3503 if ( HandleAsNavigationKey(event
) )
3506 // no item -> nothing to do
3513 // don't use m_linesPerPage directly as it might not be computed yet
3514 const int pageSize
= GetCountPerPage();
3515 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3517 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3519 if (event
.GetKeyCode() == WXK_RIGHT
)
3520 event
.m_keyCode
= WXK_LEFT
;
3521 else if (event
.GetKeyCode() == WXK_LEFT
)
3522 event
.m_keyCode
= WXK_RIGHT
;
3525 switch ( event
.GetKeyCode() )
3528 if ( m_current
> 0 )
3529 OnArrowChar( m_current
- 1, event
);
3533 if ( m_current
< (size_t)GetItemCount() - 1 )
3534 OnArrowChar( m_current
+ 1, event
);
3539 OnArrowChar( GetItemCount() - 1, event
);
3544 OnArrowChar( 0, event
);
3549 int steps
= InReportView() ? pageSize
- 1
3550 : m_current
% pageSize
;
3552 int index
= m_current
- steps
;
3556 OnArrowChar( index
, event
);
3562 int steps
= InReportView()
3564 : pageSize
- (m_current
% pageSize
) - 1;
3566 size_t index
= m_current
+ steps
;
3567 size_t count
= GetItemCount();
3568 if ( index
>= count
)
3571 OnArrowChar( index
, event
);
3576 if ( !InReportView() )
3578 int index
= m_current
- pageSize
;
3582 OnArrowChar( index
, event
);
3587 if ( !InReportView() )
3589 size_t index
= m_current
+ pageSize
;
3591 size_t count
= GetItemCount();
3592 if ( index
>= count
)
3595 OnArrowChar( index
, event
);
3600 if ( IsSingleSel() )
3602 if ( event
.ControlDown() )
3604 ReverseHighlight(m_current
);
3606 else // normal space press
3608 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3611 else // multiple selection
3613 ReverseHighlight(m_current
);
3619 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3627 // ----------------------------------------------------------------------------
3629 // ----------------------------------------------------------------------------
3631 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3635 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3636 event
.SetEventObject( GetParent() );
3637 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3641 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3642 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3643 // which are already drawn correctly resulting in horrible flicker - avoid
3653 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3657 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3658 event
.SetEventObject( GetParent() );
3659 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3667 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3669 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3671 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3673 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3675 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3677 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3679 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3681 else if ( InReportView() && (m_small_image_list
))
3683 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3687 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3689 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3691 m_normal_image_list
->GetSize( index
, width
, height
);
3693 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3695 m_small_image_list
->GetSize( index
, width
, height
);
3697 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3699 m_small_image_list
->GetSize( index
, width
, height
);
3701 else if ( InReportView() && m_small_image_list
)
3703 m_small_image_list
->GetSize( index
, width
, height
);
3712 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3714 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3715 dc
.SetFont( GetFont() );
3718 dc
.GetTextExtent( s
, &lw
, NULL
);
3720 return lw
+ AUTOSIZE_COL_MARGIN
;
3723 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3727 // calc the spacing from the icon size
3728 int width
= 0, height
= 0;
3730 if ((imageList
) && (imageList
->GetImageCount()) )
3731 imageList
->GetSize(0, width
, height
);
3733 if (which
== wxIMAGE_LIST_NORMAL
)
3735 m_normal_image_list
= imageList
;
3736 m_normal_spacing
= width
+ 8;
3739 if (which
== wxIMAGE_LIST_SMALL
)
3741 m_small_image_list
= imageList
;
3742 m_small_spacing
= width
+ 14;
3743 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3747 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3751 m_small_spacing
= spacing
;
3753 m_normal_spacing
= spacing
;
3756 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3758 return isSmall
? m_small_spacing
: m_normal_spacing
;
3761 // ----------------------------------------------------------------------------
3763 // ----------------------------------------------------------------------------
3765 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3767 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3769 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3771 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3772 item
.m_width
= GetTextLength( item
.m_text
);
3774 wxListHeaderData
*column
= node
->GetData();
3775 column
->SetItem( item
);
3777 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3779 headerWin
->m_dirty
= true;
3783 // invalidate it as it has to be recalculated
3787 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3789 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3790 _T("invalid column index") );
3792 wxCHECK_RET( InReportView(),
3793 _T("SetColumnWidth() can only be called in report mode.") );
3797 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3799 headerWin
->m_dirty
= true;
3801 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3802 wxCHECK_RET( node
, _T("no column?") );
3804 wxListHeaderData
*column
= node
->GetData();
3806 size_t count
= GetItemCount();
3808 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3810 width
= GetTextLength(column
->GetText());
3811 width
+= 2*EXTRA_WIDTH
;
3813 // check for column header's image availability
3814 const int image
= column
->GetImage();
3817 if ( m_small_image_list
)
3820 m_small_image_list
->GetSize(image
, ix
, iy
);
3821 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3825 else if ( width
== wxLIST_AUTOSIZE
)
3829 // TODO: determine the max width somehow...
3830 width
= WIDTH_COL_DEFAULT
;
3834 wxClientDC
dc(this);
3835 dc
.SetFont( GetFont() );
3837 int max
= AUTOSIZE_COL_MARGIN
;
3839 // if the cached column width isn't valid then recalculate it
3840 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3842 for (size_t i
= 0; i
< count
; i
++)
3844 wxListLineData
*line
= GetLine( i
);
3845 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3847 wxCHECK_RET( n
, _T("no subitem?") );
3849 wxListItemData
*itemData
= n
->GetData();
3852 itemData
->GetItem(item
);
3853 int itemWidth
= GetItemWidthWithImage(&item
);
3854 if (itemWidth
> max
)
3858 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3859 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3862 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3863 width
= max
+ AUTOSIZE_COL_MARGIN
;
3867 column
->SetWidth( width
);
3869 // invalidate it as it has to be recalculated
3873 int wxListMainWindow::GetHeaderWidth() const
3875 if ( !m_headerWidth
)
3877 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3879 size_t count
= GetColumnCount();
3880 for ( size_t col
= 0; col
< count
; col
++ )
3882 self
->m_headerWidth
+= GetColumnWidth(col
);
3886 return m_headerWidth
;
3889 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3891 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3892 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3894 wxListHeaderData
*column
= node
->GetData();
3895 column
->GetItem( item
);
3898 int wxListMainWindow::GetColumnWidth( int col
) const
3900 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3901 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3903 wxListHeaderData
*column
= node
->GetData();
3904 return column
->GetWidth();
3907 // ----------------------------------------------------------------------------
3909 // ----------------------------------------------------------------------------
3911 void wxListMainWindow::SetItem( wxListItem
&item
)
3913 long id
= item
.m_itemId
;
3914 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3915 _T("invalid item index in SetItem") );
3919 wxListLineData
*line
= GetLine((size_t)id
);
3920 line
->SetItem( item
.m_col
, item
);
3922 // Set item state if user wants
3923 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3924 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3928 // update the Max Width Cache if needed
3929 int width
= GetItemWidthWithImage(&item
);
3931 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3932 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3936 // update the item on screen
3938 GetItemRect(id
, rectItem
);
3939 RefreshRect(rectItem
);
3942 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3947 // first deal with selection
3948 if ( stateMask
& wxLIST_STATE_SELECTED
)
3950 // set/clear select state
3953 // optimized version for virtual listctrl.
3954 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3957 else if ( state
& wxLIST_STATE_SELECTED
)
3959 const long count
= GetItemCount();
3960 for( long i
= 0; i
< count
; i
++ )
3962 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3968 // clear for non virtual (somewhat optimized by using GetNextItem())
3970 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3972 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3977 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3979 // unfocus all: only one item can be focussed, so clearing focus for
3980 // all items is simply clearing focus of the focussed item.
3981 SetItemState(m_current
, state
, stateMask
);
3983 //(setting focus to all items makes no sense, so it is not handled here.)
3986 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3990 SetItemStateAll(state
, stateMask
);
3994 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3995 _T("invalid list ctrl item index in SetItem") );
3997 size_t oldCurrent
= m_current
;
3998 size_t item
= (size_t)litem
; // safe because of the check above
4000 // do we need to change the focus?
4001 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4003 if ( state
& wxLIST_STATE_FOCUSED
)
4005 // don't do anything if this item is already focused
4006 if ( item
!= m_current
)
4008 ChangeCurrent(item
);
4010 if ( oldCurrent
!= (size_t)-1 )
4012 if ( IsSingleSel() )
4014 HighlightLine(oldCurrent
, false);
4017 RefreshLine(oldCurrent
);
4020 RefreshLine( m_current
);
4025 // don't do anything if this item is not focused
4026 if ( item
== m_current
)
4030 if ( IsSingleSel() )
4032 // we must unselect the old current item as well or we
4033 // might end up with more than one selected item in a
4034 // single selection control
4035 HighlightLine(oldCurrent
, false);
4038 RefreshLine( oldCurrent
);
4043 // do we need to change the selection state?
4044 if ( stateMask
& wxLIST_STATE_SELECTED
)
4046 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
4048 if ( IsSingleSel() )
4052 // selecting the item also makes it the focused one in the
4054 if ( m_current
!= item
)
4056 ChangeCurrent(item
);
4058 if ( oldCurrent
!= (size_t)-1 )
4060 HighlightLine( oldCurrent
, false );
4061 RefreshLine( oldCurrent
);
4067 // only the current item may be selected anyhow
4068 if ( item
!= m_current
)
4073 if ( HighlightLine(item
, on
) )
4080 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
4082 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
4083 _T("invalid list ctrl item index in GetItemState()") );
4085 int ret
= wxLIST_STATE_DONTCARE
;
4087 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4089 if ( (size_t)item
== m_current
)
4090 ret
|= wxLIST_STATE_FOCUSED
;
4093 if ( stateMask
& wxLIST_STATE_SELECTED
)
4095 if ( IsHighlighted(item
) )
4096 ret
|= wxLIST_STATE_SELECTED
;
4102 void wxListMainWindow::GetItem( wxListItem
&item
) const
4104 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
4105 _T("invalid item index in GetItem") );
4107 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4108 line
->GetItem( item
.m_col
, item
);
4110 // Get item state if user wants it
4111 if ( item
.m_mask
& wxLIST_MASK_STATE
)
4112 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
4113 wxLIST_STATE_FOCUSED
);
4116 // ----------------------------------------------------------------------------
4118 // ----------------------------------------------------------------------------
4120 size_t wxListMainWindow::GetItemCount() const
4122 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4125 void wxListMainWindow::SetItemCount(long count
)
4127 m_selStore
.SetItemCount(count
);
4128 m_countVirt
= count
;
4130 ResetVisibleLinesRange();
4132 // scrollbars must be reset
4136 int wxListMainWindow::GetSelectedItemCount() const
4138 // deal with the quick case first
4139 if ( IsSingleSel() )
4140 return HasCurrent() ? IsHighlighted(m_current
) : false;
4142 // virtual controls remmebers all its selections itself
4144 return m_selStore
.GetSelectedCount();
4146 // TODO: we probably should maintain the number of items selected even for
4147 // non virtual controls as enumerating all lines is really slow...
4148 size_t countSel
= 0;
4149 size_t count
= GetItemCount();
4150 for ( size_t line
= 0; line
< count
; line
++ )
4152 if ( GetLine(line
)->IsHighlighted() )
4159 // ----------------------------------------------------------------------------
4160 // item position/size
4161 // ----------------------------------------------------------------------------
4163 wxRect
wxListMainWindow::GetViewRect() const
4165 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
4167 // we need to find the longest/tallest label
4168 wxCoord xMax
= 0, yMax
= 0;
4169 const int count
= GetItemCount();
4172 for ( int i
= 0; i
< count
; i
++ )
4174 // we need logical, not physical, coordinates here, so use
4175 // GetLineRect() instead of GetItemRect()
4176 wxRect r
= GetLineRect(i
);
4178 wxCoord x
= r
.GetRight(),
4188 // some fudge needed to make it look prettier
4189 xMax
+= 2 * EXTRA_BORDER_X
;
4190 yMax
+= 2 * EXTRA_BORDER_Y
;
4192 // account for the scrollbars if necessary
4193 const wxSize sizeAll
= GetClientSize();
4194 if ( xMax
> sizeAll
.x
)
4195 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4196 if ( yMax
> sizeAll
.y
)
4197 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4199 return wxRect(0, 0, xMax
, yMax
);
4203 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
4205 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
4207 _T("GetSubItemRect only meaningful in report view") );
4208 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
4209 _T("invalid item in GetSubItemRect") );
4211 // ensure that we're laid out, otherwise we could return nonsense
4214 wxConstCast(this, wxListMainWindow
)->
4215 RecalculatePositions(true /* no refresh */);
4218 rect
= GetLineRect((size_t)item
);
4220 // Adjust rect to specified column
4221 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
4223 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
4224 _T("invalid subItem in GetSubItemRect") );
4226 for (int i
= 0; i
< subItem
; i
++)
4228 rect
.x
+= GetColumnWidth(i
);
4230 rect
.width
= GetColumnWidth(subItem
);
4233 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4238 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4241 GetItemRect(item
, rect
);
4249 // ----------------------------------------------------------------------------
4250 // geometry calculation
4251 // ----------------------------------------------------------------------------
4253 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4255 const int lineHeight
= GetLineHeight();
4257 wxClientDC
dc( this );
4258 dc
.SetFont( GetFont() );
4260 const size_t count
= GetItemCount();
4263 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
4264 iconSpacing
= m_normal_spacing
;
4265 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
4266 iconSpacing
= m_small_spacing
;
4270 // Note that we do not call GetClientSize() here but
4271 // GetSize() and subtract the border size for sunken
4272 // borders manually. This is technically incorrect,
4273 // but we need to know the client area's size WITHOUT
4274 // scrollbars here. Since we don't know if there are
4275 // any scrollbars, we use GetSize() instead. Another
4276 // solution would be to call SetScrollbars() here to
4277 // remove the scrollbars and call GetClientSize() then,
4278 // but this might result in flicker and - worse - will
4279 // reset the scrollbars to 0 which is not good at all
4280 // if you resize a dialog/window, but don't want to
4281 // reset the window scrolling. RR.
4282 // Furthermore, we actually do NOT subtract the border
4283 // width as 2 pixels is just the extra space which we
4284 // need around the actual content in the window. Other-
4285 // wise the text would e.g. touch the upper border. RR.
4288 GetSize( &clientWidth
, &clientHeight
);
4290 if ( InReportView() )
4292 // all lines have the same height and we scroll one line per step
4293 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4295 m_linesPerPage
= clientHeight
/ lineHeight
;
4297 ResetVisibleLinesRange();
4299 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4300 GetHeaderWidth() / SCROLL_UNIT_X
,
4301 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4302 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
4303 GetListCtrl()->GetScrollPos(wxVERTICAL
),
4308 // we have 3 different layout strategies: either layout all items
4309 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4310 // to arrange them in top to bottom, left to right (don't ask me why
4311 // not the other way round...) order
4312 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4314 int x
= EXTRA_BORDER_X
;
4315 int y
= EXTRA_BORDER_Y
;
4317 wxCoord widthMax
= 0;
4320 for ( i
= 0; i
< count
; i
++ )
4322 wxListLineData
*line
= GetLine(i
);
4323 line
->CalculateSize( &dc
, iconSpacing
);
4324 line
->SetPosition( x
, y
, iconSpacing
);
4326 wxSize sizeLine
= GetLineSize(i
);
4328 if ( HasFlag(wxLC_ALIGN_TOP
) )
4330 if ( sizeLine
.x
> widthMax
)
4331 widthMax
= sizeLine
.x
;
4335 else // wxLC_ALIGN_LEFT
4337 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4341 if ( HasFlag(wxLC_ALIGN_TOP
) )
4343 // traverse the items again and tweak their sizes so that they are
4344 // all the same in a row
4345 for ( i
= 0; i
< count
; i
++ )
4347 wxListLineData
*line
= GetLine(i
);
4348 line
->m_gi
->ExtendWidth(widthMax
);
4352 GetListCtrl()->SetScrollbars
4356 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4357 (y
+ lineHeight
) / lineHeight
,
4358 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
4359 GetListCtrl()->GetScrollPos( wxVERTICAL
),
4363 else // "flowed" arrangement, the most complicated case
4365 // at first we try without any scrollbars, if the items don't fit into
4366 // the window, we recalculate after subtracting the space taken by the
4369 int entireWidth
= 0;
4371 for (int tries
= 0; tries
< 2; tries
++)
4373 entireWidth
= 2 * EXTRA_BORDER_X
;
4377 // Now we have decided that the items do not fit into the
4378 // client area, so we need a scrollbar
4379 entireWidth
+= SCROLL_UNIT_X
;
4382 int x
= EXTRA_BORDER_X
;
4383 int y
= EXTRA_BORDER_Y
;
4384 int maxWidthInThisRow
= 0;
4387 int currentlyVisibleLines
= 0;
4389 for (size_t i
= 0; i
< count
; i
++)
4391 currentlyVisibleLines
++;
4392 wxListLineData
*line
= GetLine( i
);
4393 line
->CalculateSize( &dc
, iconSpacing
);
4394 line
->SetPosition( x
, y
, iconSpacing
);
4396 wxSize sizeLine
= GetLineSize( i
);
4398 if ( maxWidthInThisRow
< sizeLine
.x
)
4399 maxWidthInThisRow
= sizeLine
.x
;
4402 if (currentlyVisibleLines
> m_linesPerPage
)
4403 m_linesPerPage
= currentlyVisibleLines
;
4405 if ( y
+ sizeLine
.y
>= clientHeight
)
4407 currentlyVisibleLines
= 0;
4409 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4410 x
+= maxWidthInThisRow
;
4411 entireWidth
+= maxWidthInThisRow
;
4412 maxWidthInThisRow
= 0;
4415 // We have reached the last item.
4416 if ( i
== count
- 1 )
4417 entireWidth
+= maxWidthInThisRow
;
4419 if ( (tries
== 0) &&
4420 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4422 clientHeight
-= wxSystemSettings::
4423 GetMetric(wxSYS_HSCROLL_Y
);
4428 if ( i
== count
- 1 )
4429 tries
= 1; // Everything fits, no second try required.
4433 GetListCtrl()->SetScrollbars
4437 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4439 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
4448 // FIXME: why should we call it from here?
4455 void wxListMainWindow::RefreshAll()
4460 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4461 if ( headerWin
&& headerWin
->m_dirty
)
4463 headerWin
->m_dirty
= false;
4464 headerWin
->Refresh();
4468 void wxListMainWindow::UpdateCurrent()
4470 if ( !HasCurrent() && !IsEmpty() )
4474 long wxListMainWindow::GetNextItem( long item
,
4475 int WXUNUSED(geometry
),
4479 max
= GetItemCount();
4480 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4481 _T("invalid listctrl index in GetNextItem()") );
4483 // notice that we start with the next item (or the first one if item == -1)
4484 // and this is intentional to allow writing a simple loop to iterate over
4485 // all selected items
4488 // this is not an error because the index was OK initially,
4489 // just no such item
4496 size_t count
= GetItemCount();
4497 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4499 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4502 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4509 // ----------------------------------------------------------------------------
4511 // ----------------------------------------------------------------------------
4513 void wxListMainWindow::DeleteItem( long lindex
)
4515 size_t count
= GetItemCount();
4517 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4518 _T("invalid item index in DeleteItem") );
4520 size_t index
= (size_t)lindex
;
4522 // we don't need to adjust the index for the previous items
4523 if ( HasCurrent() && m_current
>= index
)
4525 // if the current item is being deleted, we want the next one to
4526 // become selected - unless there is no next one - so don't adjust
4527 // m_current in this case
4528 if ( m_current
!= index
|| m_current
== count
- 1 )
4532 if ( InReportView() )
4534 // mark the Column Max Width cache as dirty if the items in the line
4535 // we're deleting contain the Max Column Width
4536 wxListLineData
* const line
= GetLine(index
);
4537 wxListItemDataList::compatibility_iterator n
;
4538 wxListItemData
*itemData
;
4542 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4544 n
= line
->m_items
.Item( i
);
4545 itemData
= n
->GetData();
4546 itemData
->GetItem(item
);
4548 itemWidth
= GetItemWidthWithImage(&item
);
4550 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4551 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4554 ResetVisibleLinesRange();
4557 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4562 m_selStore
.OnItemDelete(index
);
4566 m_lines
.RemoveAt( index
);
4569 // we need to refresh the (vert) scrollbar as the number of items changed
4572 RefreshAfter(index
);
4575 void wxListMainWindow::DeleteColumn( int col
)
4577 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4579 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4582 delete node
->GetData();
4583 m_columns
.Erase( node
);
4587 // update all the items
4588 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4590 wxListLineData
* const line
= GetLine(i
);
4591 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4592 delete n
->GetData();
4593 line
->m_items
.Erase(n
);
4597 if ( InReportView() ) // we only cache max widths when in Report View
4599 delete m_aColWidths
.Item(col
);
4600 m_aColWidths
.RemoveAt(col
);
4603 // invalidate it as it has to be recalculated
4607 void wxListMainWindow::DoDeleteAllItems()
4610 // nothing to do - in particular, don't send the event
4615 // to make the deletion of all items faster, we don't send the
4616 // notifications for each item deletion in this case but only one event
4617 // for all of them: this is compatible with wxMSW and documented in
4618 // DeleteAllItems() description
4620 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4621 event
.SetEventObject( GetParent() );
4622 GetParent()->GetEventHandler()->ProcessEvent( event
);
4630 if ( InReportView() )
4632 ResetVisibleLinesRange();
4633 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4635 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4642 void wxListMainWindow::DeleteAllItems()
4646 RecalculatePositions();
4649 void wxListMainWindow::DeleteEverything()
4651 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4652 WX_CLEAR_ARRAY(m_aColWidths
);
4657 // ----------------------------------------------------------------------------
4658 // scanning for an item
4659 // ----------------------------------------------------------------------------
4661 void wxListMainWindow::EnsureVisible( long index
)
4663 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4664 _T("invalid index in EnsureVisible") );
4666 // We have to call this here because the label in question might just have
4667 // been added and its position is not known yet
4669 RecalculatePositions(true /* no refresh */);
4671 MoveToItem((size_t)index
);
4674 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4680 wxString str_upper
= str
.Upper();
4684 size_t count
= GetItemCount();
4685 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4687 wxListLineData
*line
= GetLine(i
);
4688 wxString line_upper
= line
->GetText(0).Upper();
4691 if (line_upper
== str_upper
)
4696 if (line_upper
.find(str_upper
) == 0)
4704 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4710 size_t count
= GetItemCount();
4711 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4713 wxListLineData
*line
= GetLine(i
);
4715 line
->GetItem( 0, item
);
4716 if (item
.m_data
== data
)
4723 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4726 GetVisibleLinesRange( &topItem
, NULL
);
4729 GetItemPosition( GetItemCount() - 1, p
);
4733 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4734 if ( id
>= 0 && id
< (long)GetItemCount() )
4740 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4742 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
4744 size_t count
= GetItemCount();
4746 if ( InReportView() )
4748 size_t current
= y
/ GetLineHeight();
4749 if ( current
< count
)
4751 flags
= HitTestLine(current
, x
, y
);
4758 // TODO: optimize it too! this is less simple than for report view but
4759 // enumerating all items is still not a way to do it!!
4760 for ( size_t current
= 0; current
< count
; current
++ )
4762 flags
= HitTestLine(current
, x
, y
);
4771 // ----------------------------------------------------------------------------
4773 // ----------------------------------------------------------------------------
4775 void wxListMainWindow::InsertItem( wxListItem
&item
)
4777 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4779 int count
= GetItemCount();
4780 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4782 if (item
.m_itemId
> count
)
4783 item
.m_itemId
= count
;
4785 size_t id
= item
.m_itemId
;
4789 if ( InReportView() )
4791 ResetVisibleLinesRange();
4793 // calculate the width of the item and adjust the max column width
4794 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4795 int width
= GetItemWidthWithImage(&item
);
4796 item
.SetWidth(width
);
4797 if (width
> pWidthInfo
->nMaxWidth
)
4798 pWidthInfo
->nMaxWidth
= width
;
4801 wxListLineData
*line
= new wxListLineData(this);
4803 line
->SetItem( item
.m_col
, item
);
4805 m_lines
.Insert( line
, id
);
4809 // If an item is selected at or below the point of insertion, we need to
4810 // increment the member variables because the current row's index has gone
4812 if ( HasCurrent() && m_current
>= id
)
4815 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4817 RefreshLines(id
, GetItemCount() - 1);
4820 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4823 if ( InReportView() )
4825 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4826 item
.m_width
= GetTextLength( item
.m_text
);
4828 wxListHeaderData
*column
= new wxListHeaderData( item
);
4829 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4831 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4834 wxListHeaderDataList::compatibility_iterator
4835 node
= m_columns
.Item( col
);
4836 m_columns
.Insert( node
, column
);
4837 m_aColWidths
.Insert( colWidthInfo
, col
);
4841 m_columns
.Append( column
);
4842 m_aColWidths
.Add( colWidthInfo
);
4847 // update all the items
4848 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4850 wxListLineData
* const line
= GetLine(i
);
4851 wxListItemData
* const data
= new wxListItemData(this);
4853 line
->m_items
.Insert(col
, data
);
4855 line
->m_items
.Append(data
);
4859 // invalidate it as it has to be recalculated
4864 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4867 wxClientDC
dc(this);
4869 dc
.SetFont( GetFont() );
4871 if (item
->GetImage() != -1)
4874 GetImageSize( item
->GetImage(), ix
, iy
);
4878 if (!item
->GetText().empty())
4881 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4888 // ----------------------------------------------------------------------------
4890 // ----------------------------------------------------------------------------
4892 static wxListCtrlCompare list_ctrl_compare_func_2
;
4893 static long list_ctrl_compare_data
;
4895 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4897 wxListLineData
*line1
= *arg1
;
4898 wxListLineData
*line2
= *arg2
;
4900 line1
->GetItem( 0, item
);
4901 wxUIntPtr data1
= item
.m_data
;
4902 line2
->GetItem( 0, item
);
4903 wxUIntPtr data2
= item
.m_data
;
4904 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4907 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4909 // selections won't make sense any more after sorting the items so reset
4911 HighlightAll(false);
4914 list_ctrl_compare_func_2
= fn
;
4915 list_ctrl_compare_data
= data
;
4916 m_lines
.Sort( list_ctrl_compare_func_1
);
4920 // ----------------------------------------------------------------------------
4922 // ----------------------------------------------------------------------------
4924 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4926 wxPrintf( "wxListMainWindow::OnScroll\n" );
4928 // HandleOnScroll( event );
4930 // update our idea of which lines are shown when we redraw the window the
4932 ResetVisibleLinesRange();
4934 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4936 wxGenericListCtrl
* lc
= GetListCtrl();
4937 wxCHECK_RET( lc
, _T("no listctrl window?") );
4939 lc
->m_headerWin
->Refresh();
4940 lc
->m_headerWin
->Update();
4944 int wxListMainWindow::GetCountPerPage() const
4946 if ( !m_linesPerPage
)
4948 wxConstCast(this, wxListMainWindow
)->
4949 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4952 return m_linesPerPage
;
4955 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4957 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4959 if ( m_lineFrom
== (size_t)-1 )
4961 size_t count
= GetItemCount();
4964 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4966 // this may happen if SetScrollbars() hadn't been called yet
4967 if ( m_lineFrom
>= count
)
4968 m_lineFrom
= count
- 1;
4970 // we redraw one extra line but this is needed to make the redrawing
4971 // logic work when there is a fractional number of lines on screen
4972 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4973 if ( m_lineTo
>= count
)
4974 m_lineTo
= count
- 1;
4976 else // empty control
4979 m_lineTo
= (size_t)-1;
4983 wxASSERT_MSG( IsEmpty() ||
4984 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4985 _T("GetVisibleLinesRange() returns incorrect result") );
4993 // -------------------------------------------------------------------------------------
4994 // wxGenericListCtrl
4995 // -------------------------------------------------------------------------------------
4997 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4999 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
5000 EVT_SIZE(wxGenericListCtrl::OnSize
)
5001 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
5004 void wxGenericListCtrl::Init()
5006 m_imageListNormal
= NULL
;
5007 m_imageListSmall
= NULL
;
5008 m_imageListState
= NULL
;
5010 m_ownsImageListNormal
=
5011 m_ownsImageListSmall
=
5012 m_ownsImageListState
= false;
5016 m_headerHeight
= wxRendererNative::Get().GetHeaderButtonHeight(this);
5019 wxGenericListCtrl::~wxGenericListCtrl()
5021 if (m_ownsImageListNormal
)
5022 delete m_imageListNormal
;
5023 if (m_ownsImageListSmall
)
5024 delete m_imageListSmall
;
5025 if (m_ownsImageListState
)
5026 delete m_imageListState
;
5029 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
5031 bool needs_header
= HasHeader();
5032 bool has_header
= (m_headerWin
!= NULL
);
5034 if (needs_header
== has_header
)
5039 m_headerWin
= new wxListHeaderWindow
5041 this, wxID_ANY
, m_mainWin
,
5043 wxSize(GetClientSize().x
, m_headerHeight
),
5047 #if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON
5049 #if wxOSX_USE_ATSU_TEXT
5050 font
.MacCreateFromThemeFont( kThemeSmallSystemFont
);
5052 font
.MacCreateFromUIFont( kCTFontSystemFontType
);
5054 m_headerWin
->SetFont( font
);
5057 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
5061 GetSizer()->Detach( m_headerWin
);
5069 bool wxGenericListCtrl::Create(wxWindow
*parent
,
5074 const wxValidator
&validator
,
5075 const wxString
&name
)
5079 // just like in other ports, an assert will fail if the user doesn't give any type style:
5080 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
5081 _T("wxListCtrl style should have exactly one mode bit set") );
5083 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
5087 style
&= ~wxBORDER_MASK
;
5088 style
|= wxBORDER_THEME
;
5091 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
5093 SetTargetWindow( m_mainWin
);
5095 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
5096 sizer
->Add( m_mainWin
, 1, wxGROW
);
5099 CreateOrDestroyHeaderWindowAsNeeded();
5101 SetInitialSize(size
);
5106 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
5108 return wxBORDER_THEME
;
5112 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
5116 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
5119 // we need to process arrows ourselves for scrolling
5120 if ( nMsg
== WM_GETDLGCODE
)
5122 rc
|= DLGC_WANTARROWS
;
5130 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
5132 wxSize newsize
= size
;
5134 newsize
.y
-= m_headerWin
->GetSize().y
;
5139 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
5141 // update our idea of which lines are shown when we redraw
5142 // the window the next time
5143 m_mainWin
->ResetVisibleLinesRange();
5145 HandleOnScroll( event
);
5147 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
5149 m_headerWin
->Refresh();
5150 m_headerWin
->Update();
5154 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
5156 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
5157 _T("wxLC_VIRTUAL can't be [un]set") );
5159 long flag
= GetWindowStyle();
5163 if (style
& wxLC_MASK_TYPE
)
5164 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5165 if (style
& wxLC_MASK_ALIGN
)
5166 flag
&= ~wxLC_MASK_ALIGN
;
5167 if (style
& wxLC_MASK_SORT
)
5168 flag
&= ~wxLC_MASK_SORT
;
5176 // some styles can be set without recreating everything (as happens in
5177 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
5178 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
5181 wxWindow::SetWindowStyleFlag(flag
);
5185 SetWindowStyleFlag( flag
);
5189 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5193 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
5195 CreateOrDestroyHeaderWindowAsNeeded();
5197 GetSizer()->Layout();
5200 wxWindow::SetWindowStyleFlag( flag
);
5203 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5205 m_mainWin
->GetColumn( col
, item
);
5209 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5211 m_mainWin
->SetColumn( col
, item
);
5215 int wxGenericListCtrl::GetColumnWidth( int col
) const
5217 return m_mainWin
->GetColumnWidth( col
);
5220 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5222 m_mainWin
->SetColumnWidth( col
, width
);
5226 int wxGenericListCtrl::GetCountPerPage() const
5228 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5231 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5233 m_mainWin
->GetItem( info
);
5237 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5239 m_mainWin
->SetItem( info
);
5243 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5246 info
.m_text
= label
;
5247 info
.m_mask
= wxLIST_MASK_TEXT
;
5248 info
.m_itemId
= index
;
5252 info
.m_image
= imageId
;
5253 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5256 m_mainWin
->SetItem(info
);
5260 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5262 return m_mainWin
->GetItemState( item
, stateMask
);
5265 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5267 m_mainWin
->SetItemState( item
, state
, stateMask
);
5272 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5274 return SetItemColumnImage(item
, 0, image
);
5278 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5281 info
.m_image
= image
;
5282 info
.m_mask
= wxLIST_MASK_IMAGE
;
5283 info
.m_itemId
= item
;
5284 info
.m_col
= column
;
5285 m_mainWin
->SetItem( info
);
5289 wxString
wxGenericListCtrl::GetItemText( long item
) const
5291 return m_mainWin
->GetItemText(item
);
5294 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5296 m_mainWin
->SetItemText(item
, str
);
5299 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5302 info
.m_mask
= wxLIST_MASK_DATA
;
5303 info
.m_itemId
= item
;
5304 m_mainWin
->GetItem( info
);
5308 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
5311 info
.m_mask
= wxLIST_MASK_DATA
;
5312 info
.m_itemId
= item
;
5314 m_mainWin
->SetItem( info
);
5318 wxRect
wxGenericListCtrl::GetViewRect() const
5320 return m_mainWin
->GetViewRect();
5323 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
5325 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
5328 bool wxGenericListCtrl::GetSubItemRect(long item
,
5331 int WXUNUSED(code
)) const
5333 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
5336 if ( m_mainWin
->HasHeader() )
5337 rect
.y
+= m_headerHeight
+ 1;
5342 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5344 m_mainWin
->GetItemPosition( item
, pos
);
5348 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5353 int wxGenericListCtrl::GetItemCount() const
5355 return m_mainWin
->GetItemCount();
5358 int wxGenericListCtrl::GetColumnCount() const
5360 return m_mainWin
->GetColumnCount();
5363 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5365 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5368 wxSize
wxGenericListCtrl::GetItemSpacing() const
5370 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5372 return wxSize(spacing
, spacing
);
5375 #if WXWIN_COMPATIBILITY_2_6
5376 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5378 return m_mainWin
->GetItemSpacing( isSmall
);
5380 #endif // WXWIN_COMPATIBILITY_2_6
5382 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5385 info
.m_itemId
= item
;
5386 info
.SetTextColour( col
);
5387 m_mainWin
->SetItem( info
);
5390 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5393 info
.m_itemId
= item
;
5394 m_mainWin
->GetItem( info
);
5395 return info
.GetTextColour();
5398 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5401 info
.m_itemId
= item
;
5402 info
.SetBackgroundColour( col
);
5403 m_mainWin
->SetItem( info
);
5406 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5409 info
.m_itemId
= item
;
5410 m_mainWin
->GetItem( info
);
5411 return info
.GetBackgroundColour();
5414 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5417 info
.m_itemId
= item
;
5419 m_mainWin
->SetItem( info
);
5422 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5425 info
.m_itemId
= item
;
5426 m_mainWin
->GetItem( info
);
5427 return info
.GetFont();
5430 int wxGenericListCtrl::GetSelectedItemCount() const
5432 return m_mainWin
->GetSelectedItemCount();
5435 wxColour
wxGenericListCtrl::GetTextColour() const
5437 return GetForegroundColour();
5440 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5442 SetForegroundColour(col
);
5445 long wxGenericListCtrl::GetTopItem() const
5448 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5452 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5454 return m_mainWin
->GetNextItem( item
, geom
, state
);
5457 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5459 if (which
== wxIMAGE_LIST_NORMAL
)
5460 return m_imageListNormal
;
5461 else if (which
== wxIMAGE_LIST_SMALL
)
5462 return m_imageListSmall
;
5463 else if (which
== wxIMAGE_LIST_STATE
)
5464 return m_imageListState
;
5469 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5471 if ( which
== wxIMAGE_LIST_NORMAL
)
5473 if (m_ownsImageListNormal
)
5474 delete m_imageListNormal
;
5475 m_imageListNormal
= imageList
;
5476 m_ownsImageListNormal
= false;
5478 else if ( which
== wxIMAGE_LIST_SMALL
)
5480 if (m_ownsImageListSmall
)
5481 delete m_imageListSmall
;
5482 m_imageListSmall
= imageList
;
5483 m_ownsImageListSmall
= false;
5485 else if ( which
== wxIMAGE_LIST_STATE
)
5487 if (m_ownsImageListState
)
5488 delete m_imageListState
;
5489 m_imageListState
= imageList
;
5490 m_ownsImageListState
= false;
5493 m_mainWin
->SetImageList( imageList
, which
);
5496 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5498 SetImageList(imageList
, which
);
5499 if ( which
== wxIMAGE_LIST_NORMAL
)
5500 m_ownsImageListNormal
= true;
5501 else if ( which
== wxIMAGE_LIST_SMALL
)
5502 m_ownsImageListSmall
= true;
5503 else if ( which
== wxIMAGE_LIST_STATE
)
5504 m_ownsImageListState
= true;
5507 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5512 bool wxGenericListCtrl::DeleteItem( long item
)
5514 m_mainWin
->DeleteItem( item
);
5518 bool wxGenericListCtrl::DeleteAllItems()
5520 m_mainWin
->DeleteAllItems();
5524 bool wxGenericListCtrl::DeleteAllColumns()
5526 size_t count
= m_mainWin
->m_columns
.GetCount();
5527 for ( size_t n
= 0; n
< count
; n
++ )
5532 void wxGenericListCtrl::ClearAll()
5534 m_mainWin
->DeleteEverything();
5537 bool wxGenericListCtrl::DeleteColumn( int col
)
5539 m_mainWin
->DeleteColumn( col
);
5541 // if we don't have the header any longer, we need to relayout the window
5542 // if ( !GetColumnCount() )
5547 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5548 wxClassInfo
* textControlClass
)
5550 return m_mainWin
->EditLabel( item
, textControlClass
);
5553 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5555 return m_mainWin
->GetEditControl();
5558 bool wxGenericListCtrl::EnsureVisible( long item
)
5560 m_mainWin
->EnsureVisible( item
);
5564 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5566 return m_mainWin
->FindItem( start
, str
, partial
);
5569 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5571 return m_mainWin
->FindItem( start
, data
);
5574 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5575 int WXUNUSED(direction
))
5577 return m_mainWin
->FindItem( pt
);
5580 // TODO: sub item hit testing
5581 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5583 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5586 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5588 m_mainWin
->InsertItem( info
);
5589 return info
.m_itemId
;
5592 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5595 info
.m_text
= label
;
5596 info
.m_mask
= wxLIST_MASK_TEXT
;
5597 info
.m_itemId
= index
;
5598 return InsertItem( info
);
5601 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5604 info
.m_mask
= wxLIST_MASK_IMAGE
;
5605 info
.m_image
= imageIndex
;
5606 info
.m_itemId
= index
;
5607 return InsertItem( info
);
5610 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5613 info
.m_text
= label
;
5614 info
.m_image
= imageIndex
;
5615 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5616 info
.m_itemId
= index
;
5617 return InsertItem( info
);
5620 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5622 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5624 m_mainWin
->InsertColumn( col
, item
);
5626 // if we hadn't had a header before but have one now
5627 // then we need to relayout the window
5628 // if ( GetColumnCount() == 1 && m_mainWin->HasHeader() )
5630 m_headerWin
->Refresh();
5635 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5636 int format
, int width
)
5639 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5640 item
.m_text
= heading
;
5643 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5644 item
.m_width
= width
;
5647 item
.m_format
= format
;
5649 return InsertColumn( col
, item
);
5652 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
5654 return m_mainWin
->ScrollList(dx
, dy
);
5658 // fn is a function which takes 3 long arguments: item1, item2, data.
5659 // item1 is the long data associated with a first item (NOT the index).
5660 // item2 is the long data associated with a second item (NOT the index).
5661 // data is the same value as passed to SortItems.
5662 // The return value is a negative number if the first item should precede the second
5663 // item, a positive number of the second item should precede the first,
5664 // or zero if the two items are equivalent.
5665 // data is arbitrary data to be passed to the sort function.
5667 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5669 m_mainWin
->SortItems( fn
, data
);
5673 // ----------------------------------------------------------------------------
5675 // ----------------------------------------------------------------------------
5677 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5679 if (!m_mainWin
) return;
5681 // We need to override OnSize so that our scrolled
5682 // window a) does call Layout() to use sizers for
5683 // positioning the controls but b) does not query
5684 // the sizer for their size and use that for setting
5685 // the scrollable area as set that ourselves by
5686 // calling SetScrollbar() further down.
5690 m_mainWin
->RecalculatePositions();
5695 void wxGenericListCtrl::OnInternalIdle()
5697 wxWindow::OnInternalIdle();
5699 if (m_mainWin
->m_dirty
)
5700 m_mainWin
->RecalculatePositions();
5703 // ----------------------------------------------------------------------------
5705 // ----------------------------------------------------------------------------
5707 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5711 m_mainWin
->SetBackgroundColour( colour
);
5712 m_mainWin
->m_dirty
= true;
5718 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5720 if ( !wxWindow::SetForegroundColour( colour
) )
5725 m_mainWin
->SetForegroundColour( colour
);
5726 m_mainWin
->m_dirty
= true;
5730 m_headerWin
->SetForegroundColour( colour
);
5735 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5737 if ( !wxWindow::SetFont( font
) )
5742 m_mainWin
->SetFont( font
);
5743 m_mainWin
->m_dirty
= true;
5748 m_headerWin
->SetFont( font
);
5749 // CalculateAndSetHeaderHeight();
5759 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5762 // Use the same color scheme as wxListBox
5763 return wxListBox::GetClassDefaultAttributes(variant
);
5765 wxUnusedVar(variant
);
5766 wxVisualAttributes attr
;
5767 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5768 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5769 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5774 // ----------------------------------------------------------------------------
5775 // methods forwarded to m_mainWin
5776 // ----------------------------------------------------------------------------
5778 #if wxUSE_DRAG_AND_DROP
5780 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5782 m_mainWin
->SetDropTarget( dropTarget
);
5785 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5787 return m_mainWin
->GetDropTarget();
5792 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5794 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5797 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5799 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5802 wxColour
wxGenericListCtrl::GetForegroundColour() const
5804 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5807 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5810 return m_mainWin
->PopupMenu( menu
, x
, y
);
5816 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5818 m_mainWin
->DoClientToScreen(x
, y
);
5821 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5823 m_mainWin
->DoScreenToClient(x
, y
);
5826 void wxGenericListCtrl::SetFocus()
5828 // The test in window.cpp fails as we are a composite
5829 // window, so it checks against "this", but not m_mainWin.
5830 if ( DoFindFocus() != this )
5831 m_mainWin
->SetFocus();
5834 wxSize
wxGenericListCtrl::DoGetBestSize() const
5836 // Something is better than nothing...
5837 // 100x80 is what the MSW version will get from the default
5838 // wxControl::DoGetBestSize
5839 return wxSize(100, 80);
5842 // ----------------------------------------------------------------------------
5843 // virtual list control support
5844 // ----------------------------------------------------------------------------
5846 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5848 // this is a pure virtual function, in fact - which is not really pure
5849 // because the controls which are not virtual don't need to implement it
5850 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5852 return wxEmptyString
;
5855 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5857 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5859 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5863 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5866 return OnGetItemImage(item
);
5872 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5874 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5875 _T("invalid item index in OnGetItemAttr()") );
5877 // no attributes by default
5881 void wxGenericListCtrl::SetItemCount(long count
)
5883 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5885 m_mainWin
->SetItemCount(count
);
5888 void wxGenericListCtrl::RefreshItem(long item
)
5890 m_mainWin
->RefreshLine(item
);
5893 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5895 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5898 // Generic wxListCtrl is more or less a container for two other
5899 // windows which drawings are done upon. These are namely
5900 // 'm_headerWin' and 'm_mainWin'.
5901 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5902 // behaviour wxListCtrl has under wxMSW.
5904 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5908 // The easy case, no rectangle specified.
5910 m_headerWin
->Refresh(eraseBackground
);
5913 m_mainWin
->Refresh(eraseBackground
);
5917 // Refresh the header window
5920 wxRect rectHeader
= m_headerWin
->GetRect();
5921 rectHeader
.Intersect(*rect
);
5922 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5925 m_headerWin
->GetPosition(&x
, &y
);
5926 rectHeader
.Offset(-x
, -y
);
5927 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5931 // Refresh the main window
5934 wxRect rectMain
= m_mainWin
->GetRect();
5935 rectMain
.Intersect(*rect
);
5936 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5939 m_mainWin
->GetPosition(&x
, &y
);
5940 rectMain
.Offset(-x
, -y
);
5941 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5947 #endif // wxUSE_LISTCTRL