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;
82 static const int EXTRA_HEIGHT
= 4;
84 // margin between the window and the items
85 static const int EXTRA_BORDER_X
= 2;
86 static const int EXTRA_BORDER_Y
= 2;
88 // offset for the header window
89 static const int HEADER_OFFSET_X
= 0;
90 static const int HEADER_OFFSET_Y
= 0;
92 // margin between rows of icons in [small] icon view
93 static const int MARGIN_BETWEEN_ROWS
= 6;
95 // when autosizing the columns, add some slack
96 static const int AUTOSIZE_COL_MARGIN
= 10;
98 // default width for the header columns
99 static const int WIDTH_COL_DEFAULT
= 80;
101 // the space between the image and the text in the report mode
102 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
104 // the space between the image and the text in the report mode in header
105 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
107 // ============================================================================
109 // ============================================================================
111 //-----------------------------------------------------------------------------
112 // wxColWidthInfo (internal)
113 //-----------------------------------------------------------------------------
115 struct wxColWidthInfo
118 bool bNeedsUpdate
; // only set to true when an item whose
119 // width == nMaxWidth is removed
121 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
124 bNeedsUpdate
= needsUpdate
;
128 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
130 //-----------------------------------------------------------------------------
131 // wxListItemData (internal)
132 //-----------------------------------------------------------------------------
137 wxListItemData(wxListMainWindow
*owner
);
140 void SetItem( const wxListItem
&info
);
141 void SetImage( int image
) { m_image
= image
; }
142 void SetData( wxUIntPtr data
) { m_data
= data
; }
143 void SetPosition( int x
, int y
);
144 void SetSize( int width
, int height
);
146 bool HasText() const { return !m_text
.empty(); }
147 const wxString
& GetText() const { return m_text
; }
148 void SetText(const wxString
& text
) { m_text
= text
; }
150 // we can't use empty string for measuring the string width/height, so
151 // always return something
152 wxString
GetTextForMeasuring() const
154 wxString s
= GetText();
161 bool IsHit( int x
, int y
) const;
165 int GetWidth() const;
166 int GetHeight() const;
168 int GetImage() const { return m_image
; }
169 bool HasImage() const { return GetImage() != -1; }
171 void GetItem( wxListItem
&info
) const;
173 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
174 wxListItemAttr
*GetAttr() const { return m_attr
; }
177 // the item image or -1
180 // user data associated with the item
183 // the item coordinates are not used in report mode; instead this pointer is
184 // NULL and the owner window is used to retrieve the item position and size
187 // the list ctrl we are in
188 wxListMainWindow
*m_owner
;
190 // custom attributes or NULL
191 wxListItemAttr
*m_attr
;
194 // common part of all ctors
200 //-----------------------------------------------------------------------------
201 // wxListHeaderData (internal)
202 //-----------------------------------------------------------------------------
204 class wxListHeaderData
: public wxObject
208 wxListHeaderData( const wxListItem
&info
);
209 void SetItem( const wxListItem
&item
);
210 void SetPosition( int x
, int y
);
211 void SetWidth( int w
);
212 void SetState( int state
);
213 void SetFormat( int format
);
214 void SetHeight( int h
);
215 bool HasImage() const;
217 bool HasText() const { return !m_text
.empty(); }
218 const wxString
& GetText() const { return m_text
; }
219 void SetText(const wxString
& text
) { m_text
= text
; }
221 void GetItem( wxListItem
&item
);
223 bool IsHit( int x
, int y
) const;
224 int GetImage() const;
225 int GetWidth() const;
226 int GetFormat() const;
227 int GetState() const;
244 //-----------------------------------------------------------------------------
245 // wxListLineData (internal)
246 //-----------------------------------------------------------------------------
248 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
249 #include "wx/listimpl.cpp"
250 WX_DEFINE_LIST(wxListItemDataList
)
255 // the list of subitems: only may have more than one item in report mode
256 wxListItemDataList m_items
;
258 // this is not used in report view
270 // the part to be highlighted
271 wxRect m_rectHighlight
;
273 // extend all our rects to be centered inside the one of given width
274 void ExtendWidth(wxCoord w
)
276 wxASSERT_MSG( m_rectAll
.width
<= w
,
277 _T("width can only be increased") );
280 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
281 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
282 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
287 // is this item selected? [NB: not used in virtual mode]
290 // back pointer to the list ctrl
291 wxListMainWindow
*m_owner
;
294 wxListLineData(wxListMainWindow
*owner
);
298 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
302 // are we in report mode?
303 inline bool InReportView() const;
305 // are we in virtual report mode?
306 inline bool IsVirtual() const;
308 // these 2 methods shouldn't be called for report view controls, in that
309 // case we determine our position/size ourselves
311 // calculate the size of the line
312 void CalculateSize( wxDC
*dc
, int spacing
);
314 // remember the position this line appears at
315 void SetPosition( int x
, int y
, int spacing
);
319 void SetImage( int image
) { SetImage(0, image
); }
320 int GetImage() const { return GetImage(0); }
321 void SetImage( int index
, int image
);
322 int GetImage( int index
) const;
324 bool HasImage() const { return GetImage() != -1; }
325 bool HasText() const { return !GetText(0).empty(); }
327 void SetItem( int index
, const wxListItem
&info
);
328 void GetItem( int index
, wxListItem
&info
);
330 wxString
GetText(int index
) const;
331 void SetText( int index
, const wxString
& s
);
333 wxListItemAttr
*GetAttr() const;
334 void SetAttr(wxListItemAttr
*attr
);
336 // return true if the highlighting really changed
337 bool Highlight( bool on
);
339 void ReverseHighlight();
341 bool IsHighlighted() const
343 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
345 return m_highlighted
;
348 // draw the line on the given DC in icon/list mode
349 void Draw( wxDC
*dc
);
351 // the same in report mode
352 void DrawInReportMode( wxDC
*dc
,
354 const wxRect
& rectHL
,
358 // set the line to contain num items (only can be > 1 in report mode)
359 void InitItems( int num
);
361 // get the mode (i.e. style) of the list control
362 inline int GetMode() const;
364 // prepare the DC for drawing with these item's attributes, return true if
365 // we need to draw the items background to highlight it, false otherwise
366 bool SetAttributes(wxDC
*dc
,
367 const wxListItemAttr
*attr
,
370 // draw the text on the DC with the correct justification; also add an
371 // ellipsis if the text is too large to fit in the current width
372 void DrawTextFormatted(wxDC
*dc
,
373 const wxString
&text
,
376 int yMid
, // this is middle, not top, of the text
380 WX_DECLARE_OBJARRAY(wxListLineData
, wxListLineDataArray
);
381 #include "wx/arrimpl.cpp"
382 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
384 //-----------------------------------------------------------------------------
385 // wxListHeaderWindow (internal)
386 //-----------------------------------------------------------------------------
388 class wxListHeaderWindow
: public wxWindow
391 wxListMainWindow
*m_owner
;
392 const wxCursor
*m_currentCursor
;
393 wxCursor
*m_resizeCursor
;
396 // column being resized or -1
399 // divider line position in logical (unscrolled) coords
402 // minimal position beyond which the divider line
403 // can't be dragged in logical coords
407 wxListHeaderWindow();
409 wxListHeaderWindow( wxWindow
*win
,
411 wxListMainWindow
*owner
,
412 const wxPoint
&pos
= wxDefaultPosition
,
413 const wxSize
&size
= wxDefaultSize
,
415 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
417 virtual ~wxListHeaderWindow();
420 void AdjustDC( wxDC
& dc
);
422 void OnPaint( wxPaintEvent
&event
);
423 void OnMouse( wxMouseEvent
&event
);
424 void OnSetFocus( wxFocusEvent
&event
);
429 // Update main window's column later
430 bool m_sendSetColumnWidth
;
434 virtual void OnInternalIdle();
437 // common part of all ctors
440 // generate and process the list event of the given type, return true if
441 // it wasn't vetoed, i.e. if we should proceed
442 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
444 DECLARE_EVENT_TABLE()
447 //-----------------------------------------------------------------------------
448 // wxListRenameTimer (internal)
449 //-----------------------------------------------------------------------------
451 class wxListRenameTimer
: public wxTimer
454 wxListMainWindow
*m_owner
;
457 wxListRenameTimer( wxListMainWindow
*owner
);
461 //-----------------------------------------------------------------------------
462 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
463 //-----------------------------------------------------------------------------
465 class wxListTextCtrlWrapper
: public wxEvtHandler
468 // NB: text must be a valid object but not Create()d yet
469 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
473 wxTextCtrl
*GetText() const { return m_text
; }
475 void EndEdit( bool discardChanges
);
478 void OnChar( wxKeyEvent
&event
);
479 void OnKeyUp( wxKeyEvent
&event
);
480 void OnKillFocus( wxFocusEvent
&event
);
482 bool AcceptChanges();
483 void Finish( bool setfocus
);
486 wxListMainWindow
*m_owner
;
488 wxString m_startValue
;
490 bool m_aboutToFinish
;
492 DECLARE_EVENT_TABLE()
495 //-----------------------------------------------------------------------------
496 // wxListMainWindow (internal)
497 //-----------------------------------------------------------------------------
499 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
500 #include "wx/listimpl.cpp"
501 WX_DEFINE_LIST(wxListHeaderDataList
)
503 class wxListMainWindow
: public wxWindow
507 wxListMainWindow( wxWindow
*parent
,
509 const wxPoint
& pos
= wxDefaultPosition
,
510 const wxSize
& size
= wxDefaultSize
,
512 const wxString
&name
= _T("listctrlmainwindow") );
514 virtual ~wxListMainWindow();
516 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
518 // return true if this is a virtual list control
519 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
521 // return true if the control is in report mode
522 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
524 // return true if we are in single selection mode, false if multi sel
525 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
527 // do we have a header window?
528 bool HasHeader() const
529 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
531 void HighlightAll( bool on
);
533 // all these functions only do something if the line is currently visible
535 // change the line "selected" state, return true if it really changed
536 bool HighlightLine( size_t line
, bool highlight
= true);
538 // as HighlightLine() but do it for the range of lines: this is incredibly
539 // more efficient for virtual list controls!
541 // NB: unlike HighlightLine() this one does refresh the lines on screen
542 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
544 // toggle the line state and refresh it
545 void ReverseHighlight( size_t line
)
546 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
548 // return true if the line is highlighted
549 bool IsHighlighted(size_t line
) const;
551 // refresh one or several lines at once
552 void RefreshLine( size_t line
);
553 void RefreshLines( size_t lineFrom
, size_t lineTo
);
555 // refresh all selected items
556 void RefreshSelected();
558 // refresh all lines below the given one: the difference with
559 // RefreshLines() is that the index here might not be a valid one (happens
560 // when the last line is deleted)
561 void RefreshAfter( size_t lineFrom
);
563 // the methods which are forwarded to wxListLineData itself in list/icon
564 // modes but are here because the lines don't store their positions in the
567 // get the bound rect for the entire line
568 wxRect
GetLineRect(size_t line
) const;
570 // get the bound rect of the label
571 wxRect
GetLineLabelRect(size_t line
) const;
573 // get the bound rect of the items icon (only may be called if we do have
575 wxRect
GetLineIconRect(size_t line
) const;
577 // get the rect to be highlighted when the item has focus
578 wxRect
GetLineHighlightRect(size_t line
) const;
580 // get the size of the total line rect
581 wxSize
GetLineSize(size_t line
) const
582 { return GetLineRect(line
).GetSize(); }
584 // return the hit code for the corresponding position (in this line)
585 long HitTestLine(size_t line
, int x
, int y
) const;
587 // bring the selected item into view, scrolling to it if necessary
588 void MoveToItem(size_t item
);
590 bool ScrollList( int WXUNUSED(dx
), int dy
);
592 // bring the current item into view
593 void MoveToFocus() { MoveToItem(m_current
); }
595 // start editing the label of the given item
596 wxTextCtrl
*EditLabel(long item
,
597 wxClassInfo
* textControlClass
= CLASSINFO(wxTextCtrl
));
598 wxTextCtrl
*GetEditControl() const
600 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
603 void ResetTextControl(wxTextCtrl
*text
)
606 m_textctrlWrapper
= NULL
;
609 void OnRenameTimer();
610 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
611 void OnRenameCancelled(size_t itemEdit
);
613 void OnMouse( wxMouseEvent
&event
);
615 // called to switch the selection from the current item to newCurrent,
616 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
618 void OnChar( wxKeyEvent
&event
);
619 void OnKeyDown( wxKeyEvent
&event
);
620 void OnKeyUp( wxKeyEvent
&event
);
621 void OnSetFocus( wxFocusEvent
&event
);
622 void OnKillFocus( wxFocusEvent
&event
);
623 void OnScroll( wxScrollWinEvent
& event
);
625 void OnPaint( wxPaintEvent
&event
);
627 void OnChildFocus(wxChildFocusEvent
& event
);
629 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
630 void GetImageSize( int index
, int &width
, int &height
) const;
631 int GetTextLength( const wxString
&s
) const;
633 void SetImageList( wxImageList
*imageList
, int which
);
634 void SetItemSpacing( int spacing
, bool isSmall
= false );
635 int GetItemSpacing( bool isSmall
= false );
637 void SetColumn( int col
, wxListItem
&item
);
638 void SetColumnWidth( int col
, int width
);
639 void GetColumn( int col
, wxListItem
&item
) const;
640 int GetColumnWidth( int col
) const;
641 int GetColumnCount() const { return m_columns
.GetCount(); }
643 // returns the sum of the heights of all columns
644 int GetHeaderWidth() const;
646 int GetCountPerPage() const;
648 void SetItem( wxListItem
&item
);
649 void GetItem( wxListItem
&item
) const;
650 void SetItemState( long item
, long state
, long stateMask
);
651 void SetItemStateAll( long state
, long stateMask
);
652 int GetItemState( long item
, long stateMask
) const;
653 bool GetItemRect( long item
, wxRect
&rect
) const
655 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
);
657 bool GetSubItemRect( long item
, long subItem
, wxRect
& rect
) const;
658 wxRect
GetViewRect() const;
659 bool GetItemPosition( long item
, wxPoint
& pos
) const;
660 int GetSelectedItemCount() const;
662 wxString
GetItemText(long item
) const
665 info
.m_mask
= wxLIST_MASK_TEXT
;
666 info
.m_itemId
= item
;
671 void SetItemText(long item
, const wxString
& value
)
674 info
.m_mask
= wxLIST_MASK_TEXT
;
675 info
.m_itemId
= item
;
680 // set the scrollbars and update the positions of the items
681 void RecalculatePositions(bool noRefresh
= false);
683 // refresh the window and the header
686 long GetNextItem( long item
, int geometry
, int state
) const;
687 void DeleteItem( long index
);
688 void DeleteAllItems();
689 void DeleteColumn( int col
);
690 void DeleteEverything();
691 void EnsureVisible( long index
);
692 long FindItem( long start
, const wxString
& str
, bool partial
= false );
693 long FindItem( long start
, wxUIntPtr data
);
694 long FindItem( const wxPoint
& pt
);
695 long HitTest( int x
, int y
, int &flags
) const;
696 void InsertItem( wxListItem
&item
);
697 void InsertColumn( long col
, wxListItem
&item
);
698 int GetItemWidthWithImage(wxListItem
* item
);
699 void SortItems( wxListCtrlCompare fn
, long data
);
701 size_t GetItemCount() const;
702 bool IsEmpty() const { return GetItemCount() == 0; }
703 void SetItemCount(long count
);
705 // change the current (== focused) item, send a notification event
706 void ChangeCurrent(size_t current
);
707 void ResetCurrent() { ChangeCurrent((size_t)-1); }
708 bool HasCurrent() const { return m_current
!= (size_t)-1; }
710 // send out a wxListEvent
711 void SendNotify( size_t line
,
713 const wxPoint
& point
= wxDefaultPosition
);
715 // override base class virtual to reset m_lineHeight when the font changes
716 virtual bool SetFont(const wxFont
& font
)
718 if ( !wxWindow::SetFont(font
) )
726 // these are for wxListLineData usage only
728 // get the backpointer to the list ctrl
729 wxGenericListCtrl
*GetListCtrl() const
731 return wxStaticCast(GetParent(), wxGenericListCtrl
);
734 // get the height of all lines (assuming they all do have the same height)
735 wxCoord
GetLineHeight() const;
737 // get the y position of the given line (only for report view)
738 wxCoord
GetLineY(size_t line
) const;
740 // get the brush to use for the item highlighting
741 wxBrush
*GetHighlightBrush() const
743 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
746 bool HasFocus() const
752 // the array of all line objects for a non virtual list control (for the
753 // virtual list control we only ever use m_lines[0])
754 wxListLineDataArray m_lines
;
756 // the list of column objects
757 wxListHeaderDataList m_columns
;
759 // currently focused item or -1
762 // the number of lines per page
765 // this flag is set when something which should result in the window
766 // redrawing happens (i.e. an item was added or deleted, or its appearance
767 // changed) and OnPaint() doesn't redraw the window while it is set which
768 // allows to minimize the number of repaintings when a lot of items are
769 // being added. The real repainting occurs only after the next OnIdle()
773 wxColour
*m_highlightColour
;
774 wxImageList
*m_small_image_list
;
775 wxImageList
*m_normal_image_list
;
777 int m_normal_spacing
;
781 wxTimer
*m_renameTimer
;
785 ColWidthArray m_aColWidths
;
787 // for double click logic
788 size_t m_lineLastClicked
,
789 m_lineBeforeLastClicked
,
790 m_lineSelectSingleOnUp
;
793 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
795 // the total count of items in a virtual list control
798 // the object maintaining the items selection state, only used in virtual
800 wxSelectionStore m_selStore
;
802 // common part of all ctors
805 // get the line data for the given index
806 wxListLineData
*GetLine(size_t n
) const
808 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
812 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
819 // get a dummy line which can be used for geometry calculations and such:
820 // you must use GetLine() if you want to really draw the line
821 wxListLineData
*GetDummyLine() const;
823 // cache the line data of the n-th line in m_lines[0]
824 void CacheLineData(size_t line
);
826 // get the range of visible lines
827 void GetVisibleLinesRange(size_t *from
, size_t *to
);
829 // force us to recalculate the range of visible lines
830 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
832 // get the colour to be used for drawing the rules
833 wxColour
GetRuleColour() const
835 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
839 // initialize the current item if needed
840 void UpdateCurrent();
842 // delete all items but don't refresh: called from dtor
843 void DoDeleteAllItems();
845 // the height of one line using the current font
846 wxCoord m_lineHeight
;
848 // the total header width or 0 if not calculated yet
849 wxCoord m_headerWidth
;
851 // the first and last lines being shown on screen right now (inclusive),
852 // both may be -1 if they must be calculated so never access them directly:
853 // use GetVisibleLinesRange() above instead
857 // the brushes to use for item highlighting when we do/don't have focus
858 wxBrush
*m_highlightBrush
,
859 *m_highlightUnfocusedBrush
;
861 // wrapper around the text control currently used for in place editing or
862 // NULL if no item is being edited
863 wxListTextCtrlWrapper
*m_textctrlWrapper
;
866 DECLARE_EVENT_TABLE()
868 friend class wxGenericListCtrl
;
872 wxListItemData::~wxListItemData()
874 // in the virtual list control the attributes are managed by the main
875 // program, so don't delete them
876 if ( !m_owner
->IsVirtual() )
882 void wxListItemData::Init()
890 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
896 if ( owner
->InReportView() )
902 void wxListItemData::SetItem( const wxListItem
&info
)
904 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
905 SetText(info
.m_text
);
906 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
907 m_image
= info
.m_image
;
908 if ( info
.m_mask
& wxLIST_MASK_DATA
)
909 m_data
= info
.m_data
;
911 if ( info
.HasAttributes() )
914 m_attr
->AssignFrom(*info
.GetAttributes());
916 m_attr
= new wxListItemAttr(*info
.GetAttributes());
924 m_rect
->width
= info
.m_width
;
928 void wxListItemData::SetPosition( int x
, int y
)
930 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
936 void wxListItemData::SetSize( int width
, int height
)
938 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
941 m_rect
->width
= width
;
943 m_rect
->height
= height
;
946 bool wxListItemData::IsHit( int x
, int y
) const
948 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
950 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
953 int wxListItemData::GetX() const
955 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
960 int wxListItemData::GetY() const
962 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
967 int wxListItemData::GetWidth() const
969 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
971 return m_rect
->width
;
974 int wxListItemData::GetHeight() const
976 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
978 return m_rect
->height
;
981 void wxListItemData::GetItem( wxListItem
&info
) const
983 long mask
= info
.m_mask
;
985 // by default, get everything for backwards compatibility
988 if ( mask
& wxLIST_MASK_TEXT
)
989 info
.m_text
= m_text
;
990 if ( mask
& wxLIST_MASK_IMAGE
)
991 info
.m_image
= m_image
;
992 if ( mask
& wxLIST_MASK_DATA
)
993 info
.m_data
= m_data
;
997 if ( m_attr
->HasTextColour() )
998 info
.SetTextColour(m_attr
->GetTextColour());
999 if ( m_attr
->HasBackgroundColour() )
1000 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1001 if ( m_attr
->HasFont() )
1002 info
.SetFont(m_attr
->GetFont());
1006 //-----------------------------------------------------------------------------
1008 //-----------------------------------------------------------------------------
1010 void wxListHeaderData::Init()
1022 wxListHeaderData::wxListHeaderData()
1027 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1034 void wxListHeaderData::SetItem( const wxListItem
&item
)
1036 m_mask
= item
.m_mask
;
1038 if ( m_mask
& wxLIST_MASK_TEXT
)
1039 m_text
= item
.m_text
;
1041 if ( m_mask
& wxLIST_MASK_IMAGE
)
1042 m_image
= item
.m_image
;
1044 if ( m_mask
& wxLIST_MASK_FORMAT
)
1045 m_format
= item
.m_format
;
1047 if ( m_mask
& wxLIST_MASK_WIDTH
)
1048 SetWidth(item
.m_width
);
1050 if ( m_mask
& wxLIST_MASK_STATE
)
1051 SetState(item
.m_state
);
1054 void wxListHeaderData::SetPosition( int x
, int y
)
1060 void wxListHeaderData::SetHeight( int h
)
1065 void wxListHeaderData::SetWidth( int w
)
1067 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1070 void wxListHeaderData::SetState( int flag
)
1075 void wxListHeaderData::SetFormat( int format
)
1080 bool wxListHeaderData::HasImage() const
1082 return m_image
!= -1;
1085 bool wxListHeaderData::IsHit( int x
, int y
) const
1087 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1090 void wxListHeaderData::GetItem( wxListItem
& item
)
1092 item
.m_mask
= m_mask
;
1093 item
.m_text
= m_text
;
1094 item
.m_image
= m_image
;
1095 item
.m_format
= m_format
;
1096 item
.m_width
= m_width
;
1097 item
.m_state
= m_state
;
1100 int wxListHeaderData::GetImage() const
1105 int wxListHeaderData::GetWidth() const
1110 int wxListHeaderData::GetFormat() const
1115 int wxListHeaderData::GetState() const
1120 //-----------------------------------------------------------------------------
1122 //-----------------------------------------------------------------------------
1124 inline int wxListLineData::GetMode() const
1126 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1129 inline bool wxListLineData::InReportView() const
1131 return m_owner
->HasFlag(wxLC_REPORT
);
1134 inline bool wxListLineData::IsVirtual() const
1136 return m_owner
->IsVirtual();
1139 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1143 if ( InReportView() )
1146 m_gi
= new GeometryInfo
;
1148 m_highlighted
= false;
1150 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1153 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1155 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1156 wxCHECK_RET( node
, _T("no subitems at all??") );
1158 wxListItemData
*item
= node
->GetData();
1163 switch ( GetMode() )
1166 case wxLC_SMALL_ICON
:
1167 m_gi
->m_rectAll
.width
= spacing
;
1169 s
= item
->GetText();
1174 m_gi
->m_rectLabel
.width
=
1175 m_gi
->m_rectLabel
.height
= 0;
1179 dc
->GetTextExtent( s
, &lw
, &lh
);
1183 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1185 m_gi
->m_rectAll
.width
= lw
;
1187 m_gi
->m_rectLabel
.width
= lw
;
1188 m_gi
->m_rectLabel
.height
= lh
;
1191 if (item
->HasImage())
1194 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1195 m_gi
->m_rectIcon
.width
= w
+ 8;
1196 m_gi
->m_rectIcon
.height
= h
+ 8;
1198 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1199 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1200 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1201 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1204 if ( item
->HasText() )
1206 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1207 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1209 else // no text, highlight the icon
1211 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1212 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1217 s
= item
->GetTextForMeasuring();
1219 dc
->GetTextExtent( s
, &lw
, &lh
);
1223 m_gi
->m_rectLabel
.width
= lw
;
1224 m_gi
->m_rectLabel
.height
= lh
;
1226 m_gi
->m_rectAll
.width
= lw
;
1227 m_gi
->m_rectAll
.height
= lh
;
1229 if (item
->HasImage())
1232 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1233 m_gi
->m_rectIcon
.width
= w
;
1234 m_gi
->m_rectIcon
.height
= h
;
1236 m_gi
->m_rectAll
.width
+= 4 + w
;
1237 if (h
> m_gi
->m_rectAll
.height
)
1238 m_gi
->m_rectAll
.height
= h
;
1241 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1242 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1246 wxFAIL_MSG( _T("unexpected call to SetSize") );
1250 wxFAIL_MSG( _T("unknown mode") );
1255 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1257 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1258 wxCHECK_RET( node
, _T("no subitems at all??") );
1260 wxListItemData
*item
= node
->GetData();
1262 switch ( GetMode() )
1265 case wxLC_SMALL_ICON
:
1266 m_gi
->m_rectAll
.x
= x
;
1267 m_gi
->m_rectAll
.y
= y
;
1269 if ( item
->HasImage() )
1271 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1272 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1273 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1276 if ( item
->HasText() )
1278 if (m_gi
->m_rectAll
.width
> spacing
)
1279 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1281 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1282 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1283 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1284 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1286 else // no text, highlight the icon
1288 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1289 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1294 m_gi
->m_rectAll
.x
= x
;
1295 m_gi
->m_rectAll
.y
= y
;
1297 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1298 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1299 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1301 if (item
->HasImage())
1303 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1304 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1305 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1309 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1314 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1318 wxFAIL_MSG( _T("unknown mode") );
1323 void wxListLineData::InitItems( int num
)
1325 for (int i
= 0; i
< num
; i
++)
1326 m_items
.Append( new wxListItemData(m_owner
) );
1329 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1331 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1332 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1334 wxListItemData
*item
= node
->GetData();
1335 item
->SetItem( info
);
1338 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1340 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1343 wxListItemData
*item
= node
->GetData();
1344 item
->GetItem( info
);
1348 wxString
wxListLineData::GetText(int index
) const
1352 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1355 wxListItemData
*item
= node
->GetData();
1356 s
= item
->GetText();
1362 void wxListLineData::SetText( int index
, const wxString
& s
)
1364 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1367 wxListItemData
*item
= node
->GetData();
1372 void wxListLineData::SetImage( int index
, int image
)
1374 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1375 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1377 wxListItemData
*item
= node
->GetData();
1378 item
->SetImage(image
);
1381 int wxListLineData::GetImage( int index
) const
1383 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1384 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1386 wxListItemData
*item
= node
->GetData();
1387 return item
->GetImage();
1390 wxListItemAttr
*wxListLineData::GetAttr() const
1392 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1393 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1395 wxListItemData
*item
= node
->GetData();
1396 return item
->GetAttr();
1399 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1401 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1402 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1404 wxListItemData
*item
= node
->GetData();
1405 item
->SetAttr(attr
);
1408 bool wxListLineData::SetAttributes(wxDC
*dc
,
1409 const wxListItemAttr
*attr
,
1412 wxWindow
*listctrl
= m_owner
->GetParent();
1416 // don't use foreground colour for drawing highlighted items - this might
1417 // make them completely invisible (and there is no way to do bit
1418 // arithmetics on wxColour, unfortunately)
1423 if (m_owner
->HasFocus()
1424 #if !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
1425 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1433 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1435 else if ( attr
&& attr
->HasTextColour() )
1436 colText
= attr
->GetTextColour();
1438 colText
= listctrl
->GetForegroundColour();
1440 dc
->SetTextForeground(colText
);
1444 if ( attr
&& attr
->HasFont() )
1445 font
= attr
->GetFont();
1447 font
= listctrl
->GetFont();
1452 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1453 if ( highlighted
|| hasBgCol
)
1456 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1458 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxBRUSHSTYLE_SOLID
));
1460 dc
->SetPen( *wxTRANSPARENT_PEN
);
1468 void wxListLineData::Draw( wxDC
*dc
)
1470 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1471 wxCHECK_RET( node
, _T("no subitems at all??") );
1473 bool highlighted
= IsHighlighted();
1475 wxListItemAttr
*attr
= GetAttr();
1477 if ( SetAttributes(dc
, attr
, highlighted
) )
1478 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1480 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1486 int flags
= wxCONTROL_SELECTED
;
1487 if (m_owner
->HasFocus()
1488 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
1489 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1492 flags
|= wxCONTROL_FOCUSED
;
1493 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1498 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1503 // just for debugging to better see where the items are
1505 dc
->SetPen(*wxRED_PEN
);
1506 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1507 dc
->DrawRectangle( m_gi
->m_rectAll
);
1508 dc
->SetPen(*wxGREEN_PEN
);
1509 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1512 wxListItemData
*item
= node
->GetData();
1513 if (item
->HasImage())
1515 // centre the image inside our rectangle, this looks nicer when items
1516 // ae aligned in a row
1517 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1519 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1522 if (item
->HasText())
1524 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1526 wxDCClipper
clipper(*dc
, rectLabel
);
1527 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1531 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1533 const wxRect
& rectHL
,
1536 // TODO: later we should support setting different attributes for
1537 // different columns - to do it, just add "col" argument to
1538 // GetAttr() and move these lines into the loop below
1539 wxListItemAttr
*attr
= GetAttr();
1540 if ( SetAttributes(dc
, attr
, highlighted
) )
1541 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1543 dc
->DrawRectangle( rectHL
);
1549 int flags
= wxCONTROL_SELECTED
;
1550 if (m_owner
->HasFocus())
1551 flags
|= wxCONTROL_FOCUSED
;
1552 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1556 dc
->DrawRectangle( rectHL
);
1561 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1562 yMid
= rect
.y
+ rect
.height
/2;
1564 // This probably needs to be done
1565 // on all platforms as the icons
1566 // otherwise nearly touch the border
1571 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1573 node
= node
->GetNext(), col
++ )
1575 wxListItemData
*item
= node
->GetData();
1577 int width
= m_owner
->GetColumnWidth(col
);
1581 const int wText
= width
- 8;
1582 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
1584 if ( item
->HasImage() )
1587 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1588 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1590 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1596 if ( item
->HasText() )
1597 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, wText
);
1601 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1602 const wxString
& textOrig
,
1608 // we don't support displaying multiple lines currently (and neither does
1609 // wxMSW FWIW) so just merge all the lines
1610 wxString
text(textOrig
);
1611 text
.Replace(_T("\n"), _T(" "));
1614 dc
->GetTextExtent(text
, &w
, &h
);
1616 const wxCoord y
= yMid
- (h
+ 1)/2;
1618 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1620 // determine if the string can fit inside the current width
1623 // it can, draw it using the items alignment
1625 m_owner
->GetColumn(col
, item
);
1626 switch ( item
.GetAlign() )
1628 case wxLIST_FORMAT_LEFT
:
1632 case wxLIST_FORMAT_RIGHT
:
1636 case wxLIST_FORMAT_CENTER
:
1637 x
+= (width
- w
) / 2;
1641 wxFAIL_MSG( _T("unknown list item format") );
1645 dc
->DrawText(text
, x
, y
);
1647 else // otherwise, truncate and add an ellipsis if possible
1649 // determine the base width
1650 wxString
ellipsis(wxT("..."));
1652 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1654 // continue until we have enough space or only one character left
1656 size_t len
= text
.length();
1657 wxString drawntext
= text
.Left(len
);
1660 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1661 drawntext
.RemoveLast();
1664 if (w
+ base_w
<= width
)
1668 // if still not enough space, remove ellipsis characters
1669 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1671 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1672 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1675 // now draw the text
1676 dc
->DrawText(drawntext
, x
, y
);
1677 dc
->DrawText(ellipsis
, x
+ w
, y
);
1681 bool wxListLineData::Highlight( bool on
)
1683 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1685 if ( on
== m_highlighted
)
1693 void wxListLineData::ReverseHighlight( void )
1695 Highlight(!IsHighlighted());
1698 //-----------------------------------------------------------------------------
1699 // wxListHeaderWindow
1700 //-----------------------------------------------------------------------------
1702 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1703 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1704 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1705 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1708 void wxListHeaderWindow::Init()
1710 m_currentCursor
= NULL
;
1711 m_isDragging
= false;
1713 m_sendSetColumnWidth
= false;
1716 wxListHeaderWindow::wxListHeaderWindow()
1721 m_resizeCursor
= NULL
;
1724 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1726 wxListMainWindow
*owner
,
1730 const wxString
&name
)
1731 : wxWindow( win
, id
, pos
, size
, style
, name
)
1736 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1739 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1740 SetOwnForegroundColour( attr
.colFg
);
1741 SetOwnBackgroundColour( attr
.colBg
);
1743 SetOwnFont( attr
.font
);
1745 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1746 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1748 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1752 wxListHeaderWindow::~wxListHeaderWindow()
1754 delete m_resizeCursor
;
1757 #ifdef __WXUNIVERSAL__
1758 #include "wx/univ/renderer.h"
1759 #include "wx/univ/theme.h"
1762 // shift the DC origin to match the position of the main window horz
1763 // scrollbar: this allows us to always use logical coords
1764 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1766 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1769 parent
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1772 parent
->GetViewStart( &view_start
, NULL
);
1777 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1779 // account for the horz scrollbar offset
1781 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1783 // Maybe we just have to check for m_signX
1784 // in the DC, but I leave the #ifdef __WXGTK__
1786 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1790 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1793 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1795 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1797 wxPaintDC
dc( this );
1801 dc
.SetFont( GetFont() );
1803 // width and height of the entire header window
1805 GetClientSize( &w
, &h
);
1806 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1808 dc
.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT
);
1809 dc
.SetTextForeground(GetForegroundColour());
1811 int x
= HEADER_OFFSET_X
;
1812 int numColumns
= m_owner
->GetColumnCount();
1814 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1816 m_owner
->GetColumn( i
, item
);
1817 int wCol
= item
.m_width
;
1823 if (!m_parent
->IsEnabled())
1824 flags
|= wxCONTROL_DISABLED
;
1826 // NB: The code below is not really Mac-specific, but since we are close
1827 // to 2.8 release and I don't have time to test on other platforms, I
1828 // defined this only for wxMac. If this behavior is desired on
1829 // other platforms, please go ahead and revise or remove the #ifdef.
1831 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1832 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1833 flags
|= wxCONTROL_SELECTED
;
1837 flags
|= wxCONTROL_SPECIAL
; // mark as first column
1839 wxRendererNative::Get().DrawHeaderButton
1843 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1847 // see if we have enough space for the column label
1849 // for this we need the width of the text
1852 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1853 wLabel
+= 2 * EXTRA_WIDTH
;
1855 // and the width of the icon, if any
1856 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1857 const int image
= item
.m_image
;
1858 wxImageList
*imageList
;
1861 imageList
= m_owner
->m_small_image_list
;
1864 imageList
->GetSize(image
, ix
, iy
);
1865 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1873 // ignore alignment if there is not enough space anyhow
1875 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1878 wxFAIL_MSG( _T("unknown list item format") );
1881 case wxLIST_FORMAT_LEFT
:
1885 case wxLIST_FORMAT_RIGHT
:
1886 xAligned
= x
+ cw
- wLabel
;
1889 case wxLIST_FORMAT_CENTER
:
1890 xAligned
= x
+ (cw
- wLabel
) / 2;
1894 // draw the text and image clipping them so that they
1895 // don't overwrite the column boundary
1896 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1898 // if we have an image, draw it on the right of the label
1905 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1906 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1907 wxIMAGELIST_DRAW_TRANSPARENT
1911 dc
.DrawText( item
.GetText(),
1912 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1917 // Fill in what's missing to the right of the columns, otherwise we will
1918 // leave an unpainted area when columns are removed (and it looks better)
1921 wxRendererNative::Get().DrawHeaderButton
1925 wxRect(x
, HEADER_OFFSET_Y
, w
- x
, h
),
1926 wxCONTROL_DIRTY
// mark as last column
1931 void wxListHeaderWindow::OnInternalIdle()
1933 wxWindow::OnInternalIdle();
1935 if (m_sendSetColumnWidth
)
1937 m_owner
->SetColumnWidth( m_colToSend
, m_widthToSend
);
1938 m_sendSetColumnWidth
= false;
1942 void wxListHeaderWindow::DrawCurrent()
1945 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1946 m_sendSetColumnWidth
= true;
1947 m_colToSend
= m_column
;
1948 m_widthToSend
= m_currentX
- m_minX
;
1950 int x1
= m_currentX
;
1952 m_owner
->ClientToScreen( &x1
, &y1
);
1954 int x2
= m_currentX
;
1956 m_owner
->GetClientSize( NULL
, &y2
);
1957 m_owner
->ClientToScreen( &x2
, &y2
);
1960 dc
.SetLogicalFunction( wxINVERT
);
1961 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1962 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1966 dc
.DrawLine( x1
, y1
, x2
, y2
);
1968 dc
.SetLogicalFunction( wxCOPY
);
1970 dc
.SetPen( wxNullPen
);
1971 dc
.SetBrush( wxNullBrush
);
1975 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1977 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
1979 // we want to work with logical coords
1981 parent
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1982 int y
= event
.GetY();
1986 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1988 // we don't draw the line beyond our window, but we allow dragging it
1991 GetClientSize( &w
, NULL
);
1992 parent
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1995 // erase the line if it was drawn
1996 if ( m_currentX
< w
)
1999 if (event
.ButtonUp())
2002 m_isDragging
= false;
2004 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
2005 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
2012 m_currentX
= m_minX
+ 7;
2014 // draw in the new location
2015 if ( m_currentX
< w
)
2019 else // not dragging
2022 bool hit_border
= false;
2024 // end of the current column
2027 // find the column where this event occurred
2029 countCol
= m_owner
->GetColumnCount();
2030 for (col
= 0; col
< countCol
; col
++)
2032 xpos
+= m_owner
->GetColumnWidth( col
);
2035 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2037 // near the column border
2044 // inside the column
2051 if ( col
== countCol
)
2054 if (event
.LeftDown() || event
.RightUp())
2056 if (hit_border
&& event
.LeftDown())
2058 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2059 event
.GetPosition()) )
2061 m_isDragging
= true;
2066 //else: column resizing was vetoed by the user code
2068 else // click on a column
2070 // record the selected state of the columns
2071 if (event
.LeftDown())
2073 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2076 m_owner
->GetColumn(i
, colItem
);
2077 long state
= colItem
.GetState();
2079 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2081 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2082 m_owner
->SetColumn(i
, colItem
);
2086 SendListEvent( event
.LeftDown()
2087 ? wxEVT_COMMAND_LIST_COL_CLICK
2088 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2089 event
.GetPosition());
2092 else if (event
.Moving())
2097 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2098 m_currentCursor
= m_resizeCursor
;
2102 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2103 m_currentCursor
= wxSTANDARD_CURSOR
;
2107 SetCursor(*m_currentCursor
);
2112 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2114 m_owner
->SetFocus();
2118 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2120 wxWindow
*parent
= GetParent();
2121 wxListEvent
le( type
, parent
->GetId() );
2122 le
.SetEventObject( parent
);
2123 le
.m_pointDrag
= pos
;
2125 // the position should be relative to the parent window, not
2126 // this one for compatibility with MSW and common sense: the
2127 // user code doesn't know anything at all about this header
2128 // window, so why should it get positions relative to it?
2129 le
.m_pointDrag
.y
-= GetSize().y
;
2131 le
.m_col
= m_column
;
2132 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2135 //-----------------------------------------------------------------------------
2136 // wxListRenameTimer (internal)
2137 //-----------------------------------------------------------------------------
2139 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2144 void wxListRenameTimer::Notify()
2146 m_owner
->OnRenameTimer();
2149 //-----------------------------------------------------------------------------
2150 // wxListTextCtrlWrapper (internal)
2151 //-----------------------------------------------------------------------------
2153 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2154 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2155 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2156 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2159 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2162 : m_startValue(owner
->GetItemText(itemEdit
)),
2163 m_itemEdited(itemEdit
)
2167 m_aboutToFinish
= false;
2169 wxGenericListCtrl
*parent
= m_owner
->GetListCtrl();
2171 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2173 parent
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2174 &rectLabel
.x
, &rectLabel
.y
);
2176 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2177 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2178 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2181 m_text
->PushEventHandler(this);
2184 void wxListTextCtrlWrapper::EndEdit(bool discardChanges
)
2186 m_aboutToFinish
= true;
2188 if ( discardChanges
)
2190 m_owner
->OnRenameCancelled(m_itemEdited
);
2196 // Notify the owner about the changes
2199 // Even if vetoed, close the control (consistent with MSW)
2204 void wxListTextCtrlWrapper::Finish( bool setfocus
)
2206 m_text
->RemoveEventHandler(this);
2207 m_owner
->ResetTextControl( m_text
);
2209 wxPendingDelete
.Append( this );
2212 m_owner
->SetFocus();
2215 bool wxListTextCtrlWrapper::AcceptChanges()
2217 const wxString value
= m_text
->GetValue();
2219 // notice that we should always call OnRenameAccept() to generate the "end
2220 // label editing" event, even if the user hasn't really changed anything
2221 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2223 // vetoed by the user
2227 // accepted, do rename the item (unless nothing changed)
2228 if ( value
!= m_startValue
)
2229 m_owner
->SetItemText(m_itemEdited
, value
);
2234 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2236 switch ( event
.m_keyCode
)
2251 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2253 if (m_aboutToFinish
)
2255 // auto-grow the textctrl:
2256 wxSize parentSize
= m_owner
->GetSize();
2257 wxPoint myPos
= m_text
->GetPosition();
2258 wxSize mySize
= m_text
->GetSize();
2260 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2261 if (myPos
.x
+ sx
> parentSize
.x
)
2262 sx
= parentSize
.x
- myPos
.x
;
2265 m_text
->SetSize(sx
, wxDefaultCoord
);
2271 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2273 if ( !m_aboutToFinish
)
2275 if ( !AcceptChanges() )
2276 m_owner
->OnRenameCancelled( m_itemEdited
);
2281 // We must let the native text control handle focus
2285 //-----------------------------------------------------------------------------
2287 //-----------------------------------------------------------------------------
2289 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledCanvas
)
2290 EVT_PAINT (wxListMainWindow::OnPaint
)
2291 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2292 EVT_CHAR (wxListMainWindow::OnChar
)
2293 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2294 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
2295 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2296 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2297 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2298 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus
)
2301 void wxListMainWindow::Init()
2306 m_lineTo
= (size_t)-1;
2312 m_small_image_list
= NULL
;
2313 m_normal_image_list
= NULL
;
2315 m_small_spacing
= 30;
2316 m_normal_spacing
= 40;
2320 m_isCreated
= false;
2322 m_lastOnSame
= false;
2323 m_renameTimer
= new wxListRenameTimer( this );
2324 m_textctrlWrapper
= NULL
;
2328 m_lineSelectSingleOnUp
=
2329 m_lineBeforeLastClicked
= (size_t)-1;
2332 wxListMainWindow::wxListMainWindow()
2337 m_highlightUnfocusedBrush
= NULL
;
2340 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2345 const wxString
&name
)
2346 : wxWindow( parent
, id
, pos
, size
, style
, name
)
2350 m_highlightBrush
= new wxBrush
2352 wxSystemSettings::GetColour
2354 wxSYS_COLOUR_HIGHLIGHT
2359 m_highlightUnfocusedBrush
= new wxBrush
2361 wxSystemSettings::GetColour
2363 wxSYS_COLOUR_BTNSHADOW
2368 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2369 SetOwnForegroundColour( attr
.colFg
);
2370 SetOwnBackgroundColour( attr
.colBg
);
2372 SetOwnFont( attr
.font
);
2375 wxListMainWindow::~wxListMainWindow()
2378 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2379 WX_CLEAR_ARRAY(m_aColWidths
);
2381 delete m_highlightBrush
;
2382 delete m_highlightUnfocusedBrush
;
2383 delete m_renameTimer
;
2386 void wxListMainWindow::CacheLineData(size_t line
)
2388 wxGenericListCtrl
*listctrl
= GetListCtrl();
2390 wxListLineData
*ld
= GetDummyLine();
2392 size_t countCol
= GetColumnCount();
2393 for ( size_t col
= 0; col
< countCol
; col
++ )
2395 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2396 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2399 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2402 wxListLineData
*wxListMainWindow::GetDummyLine() const
2404 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2405 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2407 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2409 // we need to recreate the dummy line if the number of columns in the
2410 // control changed as it would have the incorrect number of fields
2412 if ( !m_lines
.IsEmpty() &&
2413 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2415 self
->m_lines
.Clear();
2418 if ( m_lines
.IsEmpty() )
2420 wxListLineData
*line
= new wxListLineData(self
);
2421 self
->m_lines
.Add(line
);
2423 // don't waste extra memory -- there never going to be anything
2424 // else/more in this array
2425 self
->m_lines
.Shrink();
2431 // ----------------------------------------------------------------------------
2432 // line geometry (report mode only)
2433 // ----------------------------------------------------------------------------
2435 wxCoord
wxListMainWindow::GetLineHeight() const
2437 // we cache the line height as calling GetTextExtent() is slow
2438 if ( !m_lineHeight
)
2440 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2442 wxClientDC
dc( self
);
2443 dc
.SetFont( GetFont() );
2446 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2448 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2451 m_small_image_list
->GetSize(0, iw
, ih
);
2456 self
->m_lineHeight
= y
+ LINE_SPACING
;
2459 return m_lineHeight
;
2462 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2464 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2466 return LINE_SPACING
+ line
* GetLineHeight();
2469 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2471 if ( !InReportView() )
2472 return GetLine(line
)->m_gi
->m_rectAll
;
2475 rect
.x
= HEADER_OFFSET_X
;
2476 rect
.y
= GetLineY(line
);
2477 rect
.width
= GetHeaderWidth();
2478 rect
.height
= GetLineHeight();
2483 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2485 if ( !InReportView() )
2486 return GetLine(line
)->m_gi
->m_rectLabel
;
2489 wxListLineData
*data
= GetLine(line
);
2490 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
2493 wxListItemData
*item
= node
->GetData();
2494 if ( item
->HasImage() )
2497 GetImageSize( item
->GetImage(), ix
, iy
);
2498 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
2503 rect
.x
= image_x
+ HEADER_OFFSET_X
;
2504 rect
.y
= GetLineY(line
);
2505 rect
.width
= GetColumnWidth(0) - image_x
;
2506 rect
.height
= GetLineHeight();
2511 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2513 if ( !InReportView() )
2514 return GetLine(line
)->m_gi
->m_rectIcon
;
2516 wxListLineData
*ld
= GetLine(line
);
2517 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2520 rect
.x
= HEADER_OFFSET_X
;
2521 rect
.y
= GetLineY(line
);
2522 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2527 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2529 return InReportView() ? GetLineRect(line
)
2530 : GetLine(line
)->m_gi
->m_rectHighlight
;
2533 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2535 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2537 wxListLineData
*ld
= GetLine(line
);
2539 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2540 return wxLIST_HITTEST_ONITEMICON
;
2542 // VS: Testing for "ld->HasText() || InReportView()" instead of
2543 // "ld->HasText()" is needed to make empty lines in report view
2545 if ( ld
->HasText() || InReportView() )
2547 wxRect rect
= InReportView() ? GetLineRect(line
)
2548 : GetLineLabelRect(line
);
2550 if ( rect
.Contains(x
, y
) )
2551 return wxLIST_HITTEST_ONITEMLABEL
;
2557 // ----------------------------------------------------------------------------
2558 // highlight (selection) handling
2559 // ----------------------------------------------------------------------------
2561 bool wxListMainWindow::IsHighlighted(size_t line
) const
2565 return m_selStore
.IsSelected(line
);
2569 wxListLineData
*ld
= GetLine(line
);
2570 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2572 return ld
->IsHighlighted();
2576 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2582 wxArrayInt linesChanged
;
2583 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2586 // meny items changed state, refresh everything
2587 RefreshLines(lineFrom
, lineTo
);
2589 else // only a few items changed state, refresh only them
2591 size_t count
= linesChanged
.GetCount();
2592 for ( size_t n
= 0; n
< count
; n
++ )
2594 RefreshLine(linesChanged
[n
]);
2598 else // iterate over all items in non report view
2600 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2602 if ( HighlightLine(line
, highlight
) )
2608 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2614 changed
= m_selStore
.SelectItem(line
, highlight
);
2618 wxListLineData
*ld
= GetLine(line
);
2619 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2621 changed
= ld
->Highlight(highlight
);
2626 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2627 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2633 void wxListMainWindow::RefreshLine( size_t line
)
2635 if ( InReportView() )
2637 size_t visibleFrom
, visibleTo
;
2638 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2640 if ( line
< visibleFrom
|| line
> visibleTo
)
2644 wxRect rect
= GetLineRect(line
);
2646 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2647 RefreshRect( rect
);
2650 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2652 // we suppose that they are ordered by caller
2653 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2655 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2657 if ( InReportView() )
2659 size_t visibleFrom
, visibleTo
;
2660 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2662 if ( lineFrom
< visibleFrom
)
2663 lineFrom
= visibleFrom
;
2664 if ( lineTo
> visibleTo
)
2669 rect
.y
= GetLineY(lineFrom
);
2670 rect
.width
= GetClientSize().x
;
2671 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2673 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2674 RefreshRect( rect
);
2678 // TODO: this should be optimized...
2679 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2686 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2688 if ( InReportView() )
2690 size_t visibleFrom
, visibleTo
;
2691 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2693 if ( lineFrom
< visibleFrom
)
2694 lineFrom
= visibleFrom
;
2695 else if ( lineFrom
> visibleTo
)
2700 rect
.y
= GetLineY(lineFrom
);
2701 GetListCtrl()->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2703 wxSize size
= GetClientSize();
2704 rect
.width
= size
.x
;
2706 // refresh till the bottom of the window
2707 rect
.height
= size
.y
- rect
.y
;
2709 RefreshRect( rect
);
2713 // TODO: how to do it more efficiently?
2718 void wxListMainWindow::RefreshSelected()
2724 if ( InReportView() )
2726 GetVisibleLinesRange(&from
, &to
);
2731 to
= GetItemCount() - 1;
2734 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2735 RefreshLine(m_current
);
2737 for ( size_t line
= from
; line
<= to
; line
++ )
2739 // NB: the test works as expected even if m_current == -1
2740 if ( line
!= m_current
&& IsHighlighted(line
) )
2745 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2747 // Note: a wxPaintDC must be constructed even if no drawing is
2748 // done (a Windows requirement).
2749 wxPaintDC
dc( this );
2753 // nothing to draw or not the moment to draw it
2758 RecalculatePositions( false );
2760 GetListCtrl()->PrepareDC( dc
);
2763 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2765 dc
.SetFont( GetFont() );
2767 if ( InReportView() )
2769 int lineHeight
= GetLineHeight();
2771 size_t visibleFrom
, visibleTo
;
2772 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2775 int xOrig
= dc
.LogicalToDeviceX( 0 );
2776 int yOrig
= dc
.LogicalToDeviceY( 0 );
2778 // tell the caller cache to cache the data
2781 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2782 GetParent()->GetId());
2783 evCache
.SetEventObject( GetParent() );
2784 evCache
.m_oldItemIndex
= visibleFrom
;
2785 evCache
.m_itemIndex
= visibleTo
;
2786 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2789 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2791 rectLine
= GetLineRect(line
);
2794 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2795 rectLine
.width
, rectLine
.height
) )
2797 // don't redraw unaffected lines to avoid flicker
2801 GetLine(line
)->DrawInReportMode( &dc
,
2803 GetLineHighlightRect(line
),
2804 IsHighlighted(line
) );
2807 if ( HasFlag(wxLC_HRULES
) )
2809 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2810 wxSize clientSize
= GetClientSize();
2812 size_t i
= visibleFrom
;
2813 if (i
== 0) i
= 1; // Don't draw the first one
2814 for ( ; i
<= visibleTo
; i
++ )
2817 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2818 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2819 clientSize
.x
- dev_x
, i
* lineHeight
);
2822 // Draw last horizontal rule
2823 if ( visibleTo
== GetItemCount() - 1 )
2826 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2827 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2828 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2832 // Draw vertical rules if required
2833 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2835 wxPen
pen(GetRuleColour(), 1, wxPENSTYLE_SOLID
);
2836 wxRect firstItemRect
, lastItemRect
;
2838 GetItemRect(visibleFrom
, firstItemRect
);
2839 GetItemRect(visibleTo
, lastItemRect
);
2840 int x
= firstItemRect
.GetX();
2842 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2844 for (int col
= 0; col
< GetColumnCount(); col
++)
2846 int colWidth
= GetColumnWidth(col
);
2848 int x_pos
= x
- dev_x
;
2849 if (col
< GetColumnCount()-1) x_pos
-= 2;
2850 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2851 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2857 size_t count
= GetItemCount();
2858 for ( size_t i
= 0; i
< count
; i
++ )
2860 GetLine(i
)->Draw( &dc
);
2865 // Don't draw rect outline under Mac at all.
2870 wxRect
rect( GetLineHighlightRect( m_current
) );
2872 dc
.SetPen( *wxBLACK_PEN
);
2873 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2874 dc
.DrawRectangle( rect
);
2876 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2884 void wxListMainWindow::HighlightAll( bool on
)
2886 if ( IsSingleSel() )
2888 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2890 // we just have one item to turn off
2891 if ( HasCurrent() && IsHighlighted(m_current
) )
2893 HighlightLine(m_current
, false);
2894 RefreshLine(m_current
);
2897 else // multi selection
2900 HighlightLines(0, GetItemCount() - 1, on
);
2904 void wxListMainWindow::OnChildFocus(wxChildFocusEvent
& WXUNUSED(event
))
2906 // Do nothing here. This prevents the default handler in wxScrolledWindow
2907 // from needlessly scrolling the window when the edit control is
2908 // dismissed. See ticket #9563.
2911 void wxListMainWindow::SendNotify( size_t line
,
2912 wxEventType command
,
2913 const wxPoint
& point
)
2915 wxListEvent
le( command
, GetParent()->GetId() );
2916 le
.SetEventObject( GetParent() );
2918 le
.m_itemIndex
= line
;
2920 // set only for events which have position
2921 if ( point
!= wxDefaultPosition
)
2922 le
.m_pointDrag
= point
;
2924 // don't try to get the line info for virtual list controls: the main
2925 // program has it anyhow and if we did it would result in accessing all
2926 // the lines, even those which are not visible now and this is precisely
2927 // what we're trying to avoid
2930 if ( line
!= (size_t)-1 )
2932 GetLine(line
)->GetItem( 0, le
.m_item
);
2934 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2936 //else: there may be no more such item
2938 GetParent()->GetEventHandler()->ProcessEvent( le
);
2941 void wxListMainWindow::ChangeCurrent(size_t current
)
2943 m_current
= current
;
2945 // as the current item changed, we shouldn't start editing it when the
2946 // "slow click" timer expires as the click happened on another item
2947 if ( m_renameTimer
->IsRunning() )
2948 m_renameTimer
->Stop();
2950 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2953 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2955 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2956 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2958 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2959 wxT("EditLabel() needs a text control") );
2961 size_t itemEdit
= (size_t)item
;
2963 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2964 le
.SetEventObject( GetParent() );
2965 le
.m_itemIndex
= item
;
2966 wxListLineData
*data
= GetLine(itemEdit
);
2967 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2968 data
->GetItem( 0, le
.m_item
);
2970 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2972 // vetoed by user code
2976 // We have to call this here because the label in question might just have
2977 // been added and no screen update taken place.
2982 // Pending events dispatched by wxSafeYield might have changed the item
2984 if ( (size_t)item
>= GetItemCount() )
2988 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2989 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2990 return m_textctrlWrapper
->GetText();
2993 void wxListMainWindow::OnRenameTimer()
2995 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2997 EditLabel( m_current
);
3000 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
3002 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
3003 le
.SetEventObject( GetParent() );
3004 le
.m_itemIndex
= itemEdit
;
3006 wxListLineData
*data
= GetLine(itemEdit
);
3008 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
3010 data
->GetItem( 0, le
.m_item
);
3011 le
.m_item
.m_text
= value
;
3012 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
3016 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
3018 // let owner know that the edit was cancelled
3019 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
3021 le
.SetEditCanceled(true);
3023 le
.SetEventObject( GetParent() );
3024 le
.m_itemIndex
= itemEdit
;
3026 wxListLineData
*data
= GetLine(itemEdit
);
3027 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
3029 data
->GetItem( 0, le
.m_item
);
3030 GetEventHandler()->ProcessEvent( le
);
3033 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
3037 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
3038 // shutdown the edit control when the mouse is clicked elsewhere on the
3039 // listctrl because the order of events is different (or something like
3040 // that), so explicitly end the edit if it is active.
3041 if ( event
.LeftDown() && m_textctrlWrapper
)
3042 m_textctrlWrapper
->EndEdit( false );
3045 if ( event
.LeftDown() )
3048 event
.SetEventObject( GetParent() );
3049 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3052 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3054 // let the base handle mouse wheel events.
3059 if ( !HasCurrent() || IsEmpty() )
3061 if (event
.RightDown())
3063 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3065 wxContextMenuEvent
evtCtx(
3067 GetParent()->GetId(),
3068 ClientToScreen(event
.GetPosition()));
3069 evtCtx
.SetEventObject(GetParent());
3070 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3078 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3079 event
.ButtonDClick()) )
3082 int x
= event
.GetX();
3083 int y
= event
.GetY();
3084 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3086 // where did we hit it (if we did)?
3089 size_t count
= GetItemCount(),
3092 if ( InReportView() )
3094 current
= y
/ GetLineHeight();
3095 if ( current
< count
)
3096 hitResult
= HitTestLine(current
, x
, y
);
3100 // TODO: optimize it too! this is less simple than for report view but
3101 // enumerating all items is still not a way to do it!!
3102 for ( current
= 0; current
< count
; current
++ )
3104 hitResult
= HitTestLine(current
, x
, y
);
3110 if (event
.Dragging())
3112 if (m_dragCount
== 0)
3114 // we have to report the raw, physical coords as we want to be
3115 // able to call HitTest(event.m_pointDrag) from the user code to
3116 // get the item being dragged
3117 m_dragStart
= event
.GetPosition();
3122 if (m_dragCount
!= 3)
3125 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3126 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3128 wxListEvent
le( command
, GetParent()->GetId() );
3129 le
.SetEventObject( GetParent() );
3130 le
.m_itemIndex
= m_lineLastClicked
;
3131 le
.m_pointDrag
= m_dragStart
;
3132 GetParent()->GetEventHandler()->ProcessEvent( le
);
3143 // outside of any item
3144 if (event
.RightDown())
3146 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3148 wxContextMenuEvent
evtCtx(
3150 GetParent()->GetId(),
3151 ClientToScreen(event
.GetPosition()));
3152 evtCtx
.SetEventObject(GetParent());
3153 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3157 // reset the selection and bail out
3158 HighlightAll(false);
3164 bool forceClick
= false;
3165 if (event
.ButtonDClick())
3167 if ( m_renameTimer
->IsRunning() )
3168 m_renameTimer
->Stop();
3170 m_lastOnSame
= false;
3172 if ( current
== m_lineLastClicked
)
3174 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3180 // The first click was on another item, so don't interpret this as
3181 // a double click, but as a simple click instead
3188 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3190 // select single line
3191 HighlightAll( false );
3192 ReverseHighlight(m_lineSelectSingleOnUp
);
3197 if ((current
== m_current
) &&
3198 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3199 HasFlag(wxLC_EDIT_LABELS
) )
3201 if ( !InReportView() ||
3202 GetLineLabelRect(current
).Contains(x
, y
) )
3204 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
3205 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
3210 m_lastOnSame
= false;
3211 m_lineSelectSingleOnUp
= (size_t)-1;
3215 // This is necessary, because after a DnD operation in
3216 // from and to ourself, the up event is swallowed by the
3217 // DnD code. So on next non-up event (which means here and
3218 // now) m_lineSelectSingleOnUp should be reset.
3219 m_lineSelectSingleOnUp
= (size_t)-1;
3221 if (event
.RightDown())
3223 m_lineBeforeLastClicked
= m_lineLastClicked
;
3224 m_lineLastClicked
= current
;
3226 // If the item is already selected, do not update the selection.
3227 // Multi-selections should not be cleared if a selected item is clicked.
3228 if (!IsHighlighted(current
))
3230 HighlightAll(false);
3231 ChangeCurrent(current
);
3232 ReverseHighlight(m_current
);
3235 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3237 // Allow generation of context menu event
3240 else if (event
.MiddleDown())
3242 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3244 else if ( event
.LeftDown() || forceClick
)
3246 m_lineBeforeLastClicked
= m_lineLastClicked
;
3247 m_lineLastClicked
= current
;
3249 size_t oldCurrent
= m_current
;
3250 bool oldWasSelected
= IsHighlighted(m_current
);
3252 bool cmdModifierDown
= event
.CmdDown();
3253 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3255 if ( IsSingleSel() || !IsHighlighted(current
) )
3257 HighlightAll( false );
3259 ChangeCurrent(current
);
3261 ReverseHighlight(m_current
);
3263 else // multi sel & current is highlighted & no mod keys
3265 m_lineSelectSingleOnUp
= current
;
3266 ChangeCurrent(current
); // change focus
3269 else // multi sel & either ctrl or shift is down
3271 if (cmdModifierDown
)
3273 ChangeCurrent(current
);
3275 ReverseHighlight(m_current
);
3277 else if (event
.ShiftDown())
3279 ChangeCurrent(current
);
3281 size_t lineFrom
= oldCurrent
,
3284 if ( lineTo
< lineFrom
)
3287 lineFrom
= m_current
;
3290 HighlightLines(lineFrom
, lineTo
);
3292 else // !ctrl, !shift
3294 // test in the enclosing if should make it impossible
3295 wxFAIL_MSG( _T("how did we get here?") );
3299 if (m_current
!= oldCurrent
)
3300 RefreshLine( oldCurrent
);
3302 // forceClick is only set if the previous click was on another item
3303 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3307 void wxListMainWindow::MoveToItem(size_t item
)
3309 if ( item
== (size_t)-1 )
3312 wxRect rect
= GetLineRect(item
);
3314 int client_w
, client_h
;
3315 GetClientSize( &client_w
, &client_h
);
3317 const int hLine
= GetLineHeight();
3319 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3320 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3322 if ( InReportView() )
3324 // the next we need the range of lines shown it might be different,
3325 // so recalculate it
3326 ResetVisibleLinesRange();
3328 if (rect
.y
< view_y
)
3329 GetListCtrl()->Scroll( -1, rect
.y
/ hLine
);
3330 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3331 GetListCtrl()->Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3334 // At least on Mac the visible lines value will get reset inside of
3335 // Scroll *before* it actually scrolls the window because of the
3336 // Update() that happens there, so it will still have the wrong value.
3337 // So let's reset it again and wait for it to be recalculated in the
3338 // next paint event. I would expect this problem to show up in wxGTK
3339 // too but couldn't duplicate it there. Perhaps the order of events
3340 // is different... --Robin
3341 ResetVisibleLinesRange();
3349 if (rect
.x
-view_x
< 5)
3350 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
3351 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3352 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
3354 if (rect
.y
-view_y
< 5)
3355 sy
= (rect
.y
- 5) / hLine
;
3356 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
3357 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
3359 GetListCtrl()->Scroll(sx
, sy
);
3363 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
3365 if ( !InReportView() )
3367 // TODO: this should work in all views but is not implemented now
3372 GetVisibleLinesRange(&top
, &bottom
);
3374 if ( bottom
== (size_t)-1 )
3377 ResetVisibleLinesRange();
3379 int hLine
= GetLineHeight();
3381 GetListCtrl()->Scroll(-1, top
+ dy
/ hLine
);
3384 // see comment in MoveToItem() for why we do this
3385 ResetVisibleLinesRange();
3391 // ----------------------------------------------------------------------------
3392 // keyboard handling
3393 // ----------------------------------------------------------------------------
3395 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3397 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3398 _T("invalid item index in OnArrowChar()") );
3400 size_t oldCurrent
= m_current
;
3402 // in single selection we just ignore Shift as we can't select several
3404 if ( event
.ShiftDown() && !IsSingleSel() )
3406 ChangeCurrent(newCurrent
);
3408 // refresh the old focus to remove it
3409 RefreshLine( oldCurrent
);
3411 // select all the items between the old and the new one
3412 if ( oldCurrent
> newCurrent
)
3414 newCurrent
= oldCurrent
;
3415 oldCurrent
= m_current
;
3418 HighlightLines(oldCurrent
, newCurrent
);
3422 // all previously selected items are unselected unless ctrl is held
3423 // in a multiselection control
3424 if ( !event
.ControlDown() || IsSingleSel() )
3425 HighlightAll(false);
3427 ChangeCurrent(newCurrent
);
3429 // refresh the old focus to remove it
3430 RefreshLine( oldCurrent
);
3432 // in single selection mode we must always have a selected item
3433 if ( !event
.ControlDown() || IsSingleSel() )
3434 HighlightLine( m_current
, true );
3437 RefreshLine( m_current
);
3442 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3444 wxWindow
*parent
= GetParent();
3446 // propagate the key event upwards
3447 wxKeyEvent
ke(event
);
3448 ke
.SetEventObject( parent
);
3449 if (parent
->GetEventHandler()->ProcessEvent( ke
))
3455 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3457 wxWindow
*parent
= GetParent();
3459 // propagate the key event upwards
3460 wxKeyEvent
ke(event
);
3461 if (parent
->GetEventHandler()->ProcessEvent( ke
))
3467 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3469 wxWindow
*parent
= GetParent();
3471 // send a list_key event up
3474 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3475 le
.m_itemIndex
= m_current
;
3476 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3477 le
.m_code
= event
.GetKeyCode();
3478 le
.SetEventObject( parent
);
3479 parent
->GetEventHandler()->ProcessEvent( le
);
3482 // propagate the char event upwards
3483 wxKeyEvent
ke(event
);
3484 ke
.SetEventObject( parent
);
3485 if (parent
->GetEventHandler()->ProcessEvent( ke
))
3488 if ( HandleAsNavigationKey(event
) )
3491 // no item -> nothing to do
3498 // don't use m_linesPerPage directly as it might not be computed yet
3499 const int pageSize
= GetCountPerPage();
3500 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3502 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3504 if (event
.GetKeyCode() == WXK_RIGHT
)
3505 event
.m_keyCode
= WXK_LEFT
;
3506 else if (event
.GetKeyCode() == WXK_LEFT
)
3507 event
.m_keyCode
= WXK_RIGHT
;
3510 switch ( event
.GetKeyCode() )
3513 if ( m_current
> 0 )
3514 OnArrowChar( m_current
- 1, event
);
3518 if ( m_current
< (size_t)GetItemCount() - 1 )
3519 OnArrowChar( m_current
+ 1, event
);
3524 OnArrowChar( GetItemCount() - 1, event
);
3529 OnArrowChar( 0, event
);
3534 int steps
= InReportView() ? pageSize
- 1
3535 : m_current
% pageSize
;
3537 int index
= m_current
- steps
;
3541 OnArrowChar( index
, event
);
3547 int steps
= InReportView()
3549 : pageSize
- (m_current
% pageSize
) - 1;
3551 size_t index
= m_current
+ steps
;
3552 size_t count
= GetItemCount();
3553 if ( index
>= count
)
3556 OnArrowChar( index
, event
);
3561 if ( !InReportView() )
3563 int index
= m_current
- pageSize
;
3567 OnArrowChar( index
, event
);
3572 if ( !InReportView() )
3574 size_t index
= m_current
+ pageSize
;
3576 size_t count
= GetItemCount();
3577 if ( index
>= count
)
3580 OnArrowChar( index
, event
);
3585 if ( IsSingleSel() )
3587 if ( event
.ControlDown() )
3589 ReverseHighlight(m_current
);
3591 else // normal space press
3593 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3596 else // multiple selection
3598 ReverseHighlight(m_current
);
3604 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3612 // ----------------------------------------------------------------------------
3614 // ----------------------------------------------------------------------------
3616 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3620 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3621 event
.SetEventObject( GetParent() );
3622 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3626 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3627 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3628 // which are already drawn correctly resulting in horrible flicker - avoid
3638 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3642 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3643 event
.SetEventObject( GetParent() );
3644 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3652 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3654 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3656 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3658 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3660 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3662 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3664 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3666 else if ( InReportView() && (m_small_image_list
))
3668 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3672 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3674 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3676 m_normal_image_list
->GetSize( index
, width
, height
);
3678 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3680 m_small_image_list
->GetSize( index
, width
, height
);
3682 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3684 m_small_image_list
->GetSize( index
, width
, height
);
3686 else if ( InReportView() && m_small_image_list
)
3688 m_small_image_list
->GetSize( index
, width
, height
);
3697 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3699 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3700 dc
.SetFont( GetFont() );
3703 dc
.GetTextExtent( s
, &lw
, NULL
);
3705 return lw
+ AUTOSIZE_COL_MARGIN
;
3708 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3712 // calc the spacing from the icon size
3713 int width
= 0, height
= 0;
3715 if ((imageList
) && (imageList
->GetImageCount()) )
3716 imageList
->GetSize(0, width
, height
);
3718 if (which
== wxIMAGE_LIST_NORMAL
)
3720 m_normal_image_list
= imageList
;
3721 m_normal_spacing
= width
+ 8;
3724 if (which
== wxIMAGE_LIST_SMALL
)
3726 m_small_image_list
= imageList
;
3727 m_small_spacing
= width
+ 14;
3728 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3732 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3736 m_small_spacing
= spacing
;
3738 m_normal_spacing
= spacing
;
3741 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3743 return isSmall
? m_small_spacing
: m_normal_spacing
;
3746 // ----------------------------------------------------------------------------
3748 // ----------------------------------------------------------------------------
3750 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3752 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3754 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3756 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3757 item
.m_width
= GetTextLength( item
.m_text
);
3759 wxListHeaderData
*column
= node
->GetData();
3760 column
->SetItem( item
);
3762 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3764 headerWin
->m_dirty
= true;
3768 // invalidate it as it has to be recalculated
3772 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3774 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3775 _T("invalid column index") );
3777 wxCHECK_RET( InReportView(),
3778 _T("SetColumnWidth() can only be called in report mode.") );
3782 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3784 headerWin
->m_dirty
= true;
3786 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3787 wxCHECK_RET( node
, _T("no column?") );
3789 wxListHeaderData
*column
= node
->GetData();
3791 size_t count
= GetItemCount();
3793 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3795 width
= GetTextLength(column
->GetText());
3796 width
+= 2*EXTRA_WIDTH
;
3798 // check for column header's image availability
3799 const int image
= column
->GetImage();
3802 if ( m_small_image_list
)
3805 m_small_image_list
->GetSize(image
, ix
, iy
);
3806 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3810 else if ( width
== wxLIST_AUTOSIZE
)
3814 // TODO: determine the max width somehow...
3815 width
= WIDTH_COL_DEFAULT
;
3819 wxClientDC
dc(this);
3820 dc
.SetFont( GetFont() );
3822 int max
= AUTOSIZE_COL_MARGIN
;
3824 // if the cached column width isn't valid then recalculate it
3825 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3827 for (size_t i
= 0; i
< count
; i
++)
3829 wxListLineData
*line
= GetLine( i
);
3830 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3832 wxCHECK_RET( n
, _T("no subitem?") );
3834 wxListItemData
*itemData
= n
->GetData();
3837 itemData
->GetItem(item
);
3838 int itemWidth
= GetItemWidthWithImage(&item
);
3839 if (itemWidth
> max
)
3843 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3844 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3847 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3848 width
= max
+ AUTOSIZE_COL_MARGIN
;
3852 column
->SetWidth( width
);
3854 // invalidate it as it has to be recalculated
3858 int wxListMainWindow::GetHeaderWidth() const
3860 if ( !m_headerWidth
)
3862 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3864 size_t count
= GetColumnCount();
3865 for ( size_t col
= 0; col
< count
; col
++ )
3867 self
->m_headerWidth
+= GetColumnWidth(col
);
3871 return m_headerWidth
;
3874 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3876 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3877 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3879 wxListHeaderData
*column
= node
->GetData();
3880 column
->GetItem( item
);
3883 int wxListMainWindow::GetColumnWidth( int col
) const
3885 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3886 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3888 wxListHeaderData
*column
= node
->GetData();
3889 return column
->GetWidth();
3892 // ----------------------------------------------------------------------------
3894 // ----------------------------------------------------------------------------
3896 void wxListMainWindow::SetItem( wxListItem
&item
)
3898 long id
= item
.m_itemId
;
3899 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3900 _T("invalid item index in SetItem") );
3904 wxListLineData
*line
= GetLine((size_t)id
);
3905 line
->SetItem( item
.m_col
, item
);
3907 // Set item state if user wants
3908 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3909 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3913 // update the Max Width Cache if needed
3914 int width
= GetItemWidthWithImage(&item
);
3916 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3917 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3921 // update the item on screen
3923 GetItemRect(id
, rectItem
);
3924 RefreshRect(rectItem
);
3927 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3932 // first deal with selection
3933 if ( stateMask
& wxLIST_STATE_SELECTED
)
3935 // set/clear select state
3938 // optimized version for virtual listctrl.
3939 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3942 else if ( state
& wxLIST_STATE_SELECTED
)
3944 const long count
= GetItemCount();
3945 for( long i
= 0; i
< count
; i
++ )
3947 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3953 // clear for non virtual (somewhat optimized by using GetNextItem())
3955 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3957 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3962 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3964 // unfocus all: only one item can be focussed, so clearing focus for
3965 // all items is simply clearing focus of the focussed item.
3966 SetItemState(m_current
, state
, stateMask
);
3968 //(setting focus to all items makes no sense, so it is not handled here.)
3971 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3975 SetItemStateAll(state
, stateMask
);
3979 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3980 _T("invalid list ctrl item index in SetItem") );
3982 size_t oldCurrent
= m_current
;
3983 size_t item
= (size_t)litem
; // safe because of the check above
3985 // do we need to change the focus?
3986 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3988 if ( state
& wxLIST_STATE_FOCUSED
)
3990 // don't do anything if this item is already focused
3991 if ( item
!= m_current
)
3993 ChangeCurrent(item
);
3995 if ( oldCurrent
!= (size_t)-1 )
3997 if ( IsSingleSel() )
3999 HighlightLine(oldCurrent
, false);
4002 RefreshLine(oldCurrent
);
4005 RefreshLine( m_current
);
4010 // don't do anything if this item is not focused
4011 if ( item
== m_current
)
4015 if ( IsSingleSel() )
4017 // we must unselect the old current item as well or we
4018 // might end up with more than one selected item in a
4019 // single selection control
4020 HighlightLine(oldCurrent
, false);
4023 RefreshLine( oldCurrent
);
4028 // do we need to change the selection state?
4029 if ( stateMask
& wxLIST_STATE_SELECTED
)
4031 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
4033 if ( IsSingleSel() )
4037 // selecting the item also makes it the focused one in the
4039 if ( m_current
!= item
)
4041 ChangeCurrent(item
);
4043 if ( oldCurrent
!= (size_t)-1 )
4045 HighlightLine( oldCurrent
, false );
4046 RefreshLine( oldCurrent
);
4052 // only the current item may be selected anyhow
4053 if ( item
!= m_current
)
4058 if ( HighlightLine(item
, on
) )
4065 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
4067 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
4068 _T("invalid list ctrl item index in GetItemState()") );
4070 int ret
= wxLIST_STATE_DONTCARE
;
4072 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4074 if ( (size_t)item
== m_current
)
4075 ret
|= wxLIST_STATE_FOCUSED
;
4078 if ( stateMask
& wxLIST_STATE_SELECTED
)
4080 if ( IsHighlighted(item
) )
4081 ret
|= wxLIST_STATE_SELECTED
;
4087 void wxListMainWindow::GetItem( wxListItem
&item
) const
4089 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
4090 _T("invalid item index in GetItem") );
4092 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4093 line
->GetItem( item
.m_col
, item
);
4095 // Get item state if user wants it
4096 if ( item
.m_mask
& wxLIST_MASK_STATE
)
4097 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
4098 wxLIST_STATE_FOCUSED
);
4101 // ----------------------------------------------------------------------------
4103 // ----------------------------------------------------------------------------
4105 size_t wxListMainWindow::GetItemCount() const
4107 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4110 void wxListMainWindow::SetItemCount(long count
)
4112 m_selStore
.SetItemCount(count
);
4113 m_countVirt
= count
;
4115 ResetVisibleLinesRange();
4117 // scrollbars must be reset
4121 int wxListMainWindow::GetSelectedItemCount() const
4123 // deal with the quick case first
4124 if ( IsSingleSel() )
4125 return HasCurrent() ? IsHighlighted(m_current
) : false;
4127 // virtual controls remmebers all its selections itself
4129 return m_selStore
.GetSelectedCount();
4131 // TODO: we probably should maintain the number of items selected even for
4132 // non virtual controls as enumerating all lines is really slow...
4133 size_t countSel
= 0;
4134 size_t count
= GetItemCount();
4135 for ( size_t line
= 0; line
< count
; line
++ )
4137 if ( GetLine(line
)->IsHighlighted() )
4144 // ----------------------------------------------------------------------------
4145 // item position/size
4146 // ----------------------------------------------------------------------------
4148 wxRect
wxListMainWindow::GetViewRect() const
4150 wxASSERT_MSG( !HasFlag(wxLC_LIST
), "not implemented for list view" );
4152 // we need to find the longest/tallest label
4153 wxCoord xMax
= 0, yMax
= 0;
4154 const int count
= GetItemCount();
4157 for ( int i
= 0; i
< count
; i
++ )
4159 // we need logical, not physical, coordinates here, so use
4160 // GetLineRect() instead of GetItemRect()
4161 wxRect r
= GetLineRect(i
);
4163 wxCoord x
= r
.GetRight(),
4173 // some fudge needed to make it look prettier
4174 xMax
+= 2 * EXTRA_BORDER_X
;
4175 yMax
+= 2 * EXTRA_BORDER_Y
;
4177 // account for the scrollbars if necessary
4178 const wxSize sizeAll
= GetClientSize();
4179 if ( xMax
> sizeAll
.x
)
4180 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4181 if ( yMax
> sizeAll
.y
)
4182 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4184 return wxRect(0, 0, xMax
, yMax
);
4188 wxListMainWindow::GetSubItemRect(long item
, long subItem
, wxRect
& rect
) const
4190 wxCHECK_MSG( subItem
== wxLIST_GETSUBITEMRECT_WHOLEITEM
|| InReportView(),
4192 _T("GetSubItemRect only meaningful in report view") );
4193 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), false,
4194 _T("invalid item in GetSubItemRect") );
4196 // ensure that we're laid out, otherwise we could return nonsense
4199 wxConstCast(this, wxListMainWindow
)->
4200 RecalculatePositions(true /* no refresh */);
4203 rect
= GetLineRect((size_t)item
);
4205 // Adjust rect to specified column
4206 if ( subItem
!= wxLIST_GETSUBITEMRECT_WHOLEITEM
)
4208 wxCHECK_MSG( subItem
>= 0 && subItem
< GetColumnCount(), false,
4209 _T("invalid subItem in GetSubItemRect") );
4211 for (int i
= 0; i
< subItem
; i
++)
4213 rect
.x
+= GetColumnWidth(i
);
4215 rect
.width
= GetColumnWidth(subItem
);
4218 GetListCtrl()->CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4223 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4226 GetItemRect(item
, rect
);
4234 // ----------------------------------------------------------------------------
4235 // geometry calculation
4236 // ----------------------------------------------------------------------------
4238 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4240 const int lineHeight
= GetLineHeight();
4242 wxClientDC
dc( this );
4243 dc
.SetFont( GetFont() );
4245 const size_t count
= GetItemCount();
4248 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
4249 iconSpacing
= m_normal_spacing
;
4250 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
4251 iconSpacing
= m_small_spacing
;
4255 // Note that we do not call GetClientSize() here but
4256 // GetSize() and subtract the border size for sunken
4257 // borders manually. This is technically incorrect,
4258 // but we need to know the client area's size WITHOUT
4259 // scrollbars here. Since we don't know if there are
4260 // any scrollbars, we use GetSize() instead. Another
4261 // solution would be to call SetScrollbars() here to
4262 // remove the scrollbars and call GetClientSize() then,
4263 // but this might result in flicker and - worse - will
4264 // reset the scrollbars to 0 which is not good at all
4265 // if you resize a dialog/window, but don't want to
4266 // reset the window scrolling. RR.
4267 // Furthermore, we actually do NOT subtract the border
4268 // width as 2 pixels is just the extra space which we
4269 // need around the actual content in the window. Other-
4270 // wise the text would e.g. touch the upper border. RR.
4273 GetSize( &clientWidth
, &clientHeight
);
4275 if ( InReportView() )
4277 // all lines have the same height and we scroll one line per step
4278 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4280 m_linesPerPage
= clientHeight
/ lineHeight
;
4282 ResetVisibleLinesRange();
4284 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4285 GetHeaderWidth() / SCROLL_UNIT_X
,
4286 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4287 GetListCtrl()->GetScrollPos(wxHORIZONTAL
),
4288 GetListCtrl()->GetScrollPos(wxVERTICAL
),
4293 // we have 3 different layout strategies: either layout all items
4294 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4295 // to arrange them in top to bottom, left to right (don't ask me why
4296 // not the other way round...) order
4297 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4299 int x
= EXTRA_BORDER_X
;
4300 int y
= EXTRA_BORDER_Y
;
4302 wxCoord widthMax
= 0;
4305 for ( i
= 0; i
< count
; i
++ )
4307 wxListLineData
*line
= GetLine(i
);
4308 line
->CalculateSize( &dc
, iconSpacing
);
4309 line
->SetPosition( x
, y
, iconSpacing
);
4311 wxSize sizeLine
= GetLineSize(i
);
4313 if ( HasFlag(wxLC_ALIGN_TOP
) )
4315 if ( sizeLine
.x
> widthMax
)
4316 widthMax
= sizeLine
.x
;
4320 else // wxLC_ALIGN_LEFT
4322 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4326 if ( HasFlag(wxLC_ALIGN_TOP
) )
4328 // traverse the items again and tweak their sizes so that they are
4329 // all the same in a row
4330 for ( i
= 0; i
< count
; i
++ )
4332 wxListLineData
*line
= GetLine(i
);
4333 line
->m_gi
->ExtendWidth(widthMax
);
4337 GetListCtrl()->SetScrollbars
4341 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4342 (y
+ lineHeight
) / lineHeight
,
4343 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
4344 GetListCtrl()->GetScrollPos( wxVERTICAL
),
4348 else // "flowed" arrangement, the most complicated case
4350 // at first we try without any scrollbars, if the items don't fit into
4351 // the window, we recalculate after subtracting the space taken by the
4354 int entireWidth
= 0;
4356 for (int tries
= 0; tries
< 2; tries
++)
4358 entireWidth
= 2 * EXTRA_BORDER_X
;
4362 // Now we have decided that the items do not fit into the
4363 // client area, so we need a scrollbar
4364 entireWidth
+= SCROLL_UNIT_X
;
4367 int x
= EXTRA_BORDER_X
;
4368 int y
= EXTRA_BORDER_Y
;
4369 int maxWidthInThisRow
= 0;
4372 int currentlyVisibleLines
= 0;
4374 for (size_t i
= 0; i
< count
; i
++)
4376 currentlyVisibleLines
++;
4377 wxListLineData
*line
= GetLine( i
);
4378 line
->CalculateSize( &dc
, iconSpacing
);
4379 line
->SetPosition( x
, y
, iconSpacing
);
4381 wxSize sizeLine
= GetLineSize( i
);
4383 if ( maxWidthInThisRow
< sizeLine
.x
)
4384 maxWidthInThisRow
= sizeLine
.x
;
4387 if (currentlyVisibleLines
> m_linesPerPage
)
4388 m_linesPerPage
= currentlyVisibleLines
;
4390 if ( y
+ sizeLine
.y
>= clientHeight
)
4392 currentlyVisibleLines
= 0;
4394 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4395 x
+= maxWidthInThisRow
;
4396 entireWidth
+= maxWidthInThisRow
;
4397 maxWidthInThisRow
= 0;
4400 // We have reached the last item.
4401 if ( i
== count
- 1 )
4402 entireWidth
+= maxWidthInThisRow
;
4404 if ( (tries
== 0) &&
4405 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4407 clientHeight
-= wxSystemSettings::
4408 GetMetric(wxSYS_HSCROLL_Y
);
4413 if ( i
== count
- 1 )
4414 tries
= 1; // Everything fits, no second try required.
4418 GetListCtrl()->SetScrollbars
4422 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4424 GetListCtrl()->GetScrollPos( wxHORIZONTAL
),
4433 // FIXME: why should we call it from here?
4440 void wxListMainWindow::RefreshAll()
4445 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4446 if ( headerWin
&& headerWin
->m_dirty
)
4448 headerWin
->m_dirty
= false;
4449 headerWin
->Refresh();
4453 void wxListMainWindow::UpdateCurrent()
4455 if ( !HasCurrent() && !IsEmpty() )
4459 long wxListMainWindow::GetNextItem( long item
,
4460 int WXUNUSED(geometry
),
4464 max
= GetItemCount();
4465 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4466 _T("invalid listctrl index in GetNextItem()") );
4468 // notice that we start with the next item (or the first one if item == -1)
4469 // and this is intentional to allow writing a simple loop to iterate over
4470 // all selected items
4473 // this is not an error because the index was OK initially,
4474 // just no such item
4481 size_t count
= GetItemCount();
4482 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4484 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4487 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4494 // ----------------------------------------------------------------------------
4496 // ----------------------------------------------------------------------------
4498 void wxListMainWindow::DeleteItem( long lindex
)
4500 size_t count
= GetItemCount();
4502 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4503 _T("invalid item index in DeleteItem") );
4505 size_t index
= (size_t)lindex
;
4507 // we don't need to adjust the index for the previous items
4508 if ( HasCurrent() && m_current
>= index
)
4510 // if the current item is being deleted, we want the next one to
4511 // become selected - unless there is no next one - so don't adjust
4512 // m_current in this case
4513 if ( m_current
!= index
|| m_current
== count
- 1 )
4517 if ( InReportView() )
4519 // mark the Column Max Width cache as dirty if the items in the line
4520 // we're deleting contain the Max Column Width
4521 wxListLineData
* const line
= GetLine(index
);
4522 wxListItemDataList::compatibility_iterator n
;
4523 wxListItemData
*itemData
;
4527 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4529 n
= line
->m_items
.Item( i
);
4530 itemData
= n
->GetData();
4531 itemData
->GetItem(item
);
4533 itemWidth
= GetItemWidthWithImage(&item
);
4535 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4536 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4539 ResetVisibleLinesRange();
4542 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4547 m_selStore
.OnItemDelete(index
);
4551 m_lines
.RemoveAt( index
);
4554 // we need to refresh the (vert) scrollbar as the number of items changed
4557 RefreshAfter(index
);
4560 void wxListMainWindow::DeleteColumn( int col
)
4562 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4564 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4567 delete node
->GetData();
4568 m_columns
.Erase( node
);
4572 // update all the items
4573 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4575 wxListLineData
* const line
= GetLine(i
);
4576 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4577 delete n
->GetData();
4578 line
->m_items
.Erase(n
);
4582 if ( InReportView() ) // we only cache max widths when in Report View
4584 delete m_aColWidths
.Item(col
);
4585 m_aColWidths
.RemoveAt(col
);
4588 // invalidate it as it has to be recalculated
4592 void wxListMainWindow::DoDeleteAllItems()
4595 // nothing to do - in particular, don't send the event
4600 // to make the deletion of all items faster, we don't send the
4601 // notifications for each item deletion in this case but only one event
4602 // for all of them: this is compatible with wxMSW and documented in
4603 // DeleteAllItems() description
4605 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4606 event
.SetEventObject( GetParent() );
4607 GetParent()->GetEventHandler()->ProcessEvent( event
);
4615 if ( InReportView() )
4617 ResetVisibleLinesRange();
4618 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4620 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4627 void wxListMainWindow::DeleteAllItems()
4631 RecalculatePositions();
4634 void wxListMainWindow::DeleteEverything()
4636 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4637 WX_CLEAR_ARRAY(m_aColWidths
);
4642 // ----------------------------------------------------------------------------
4643 // scanning for an item
4644 // ----------------------------------------------------------------------------
4646 void wxListMainWindow::EnsureVisible( long index
)
4648 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4649 _T("invalid index in EnsureVisible") );
4651 // We have to call this here because the label in question might just have
4652 // been added and its position is not known yet
4654 RecalculatePositions(true /* no refresh */);
4656 MoveToItem((size_t)index
);
4659 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4665 wxString str_upper
= str
.Upper();
4669 size_t count
= GetItemCount();
4670 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4672 wxListLineData
*line
= GetLine(i
);
4673 wxString line_upper
= line
->GetText(0).Upper();
4676 if (line_upper
== str_upper
)
4681 if (line_upper
.find(str_upper
) == 0)
4689 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4695 size_t count
= GetItemCount();
4696 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4698 wxListLineData
*line
= GetLine(i
);
4700 line
->GetItem( 0, item
);
4701 if (item
.m_data
== data
)
4708 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4711 GetVisibleLinesRange( &topItem
, NULL
);
4714 GetItemPosition( GetItemCount() - 1, p
);
4718 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4719 if ( id
>= 0 && id
< (long)GetItemCount() )
4725 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4727 GetListCtrl()->CalcUnscrolledPosition( x
, y
, &x
, &y
);
4729 size_t count
= GetItemCount();
4731 if ( InReportView() )
4733 size_t current
= y
/ GetLineHeight();
4734 if ( current
< count
)
4736 flags
= HitTestLine(current
, x
, y
);
4743 // TODO: optimize it too! this is less simple than for report view but
4744 // enumerating all items is still not a way to do it!!
4745 for ( size_t current
= 0; current
< count
; current
++ )
4747 flags
= HitTestLine(current
, x
, y
);
4756 // ----------------------------------------------------------------------------
4758 // ----------------------------------------------------------------------------
4760 void wxListMainWindow::InsertItem( wxListItem
&item
)
4762 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4764 int count
= GetItemCount();
4765 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4767 if (item
.m_itemId
> count
)
4768 item
.m_itemId
= count
;
4770 size_t id
= item
.m_itemId
;
4774 if ( InReportView() )
4776 ResetVisibleLinesRange();
4778 // calculate the width of the item and adjust the max column width
4779 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4780 int width
= GetItemWidthWithImage(&item
);
4781 item
.SetWidth(width
);
4782 if (width
> pWidthInfo
->nMaxWidth
)
4783 pWidthInfo
->nMaxWidth
= width
;
4786 wxListLineData
*line
= new wxListLineData(this);
4788 line
->SetItem( item
.m_col
, item
);
4790 m_lines
.Insert( line
, id
);
4794 // If an item is selected at or below the point of insertion, we need to
4795 // increment the member variables because the current row's index has gone
4797 if ( HasCurrent() && m_current
>= id
)
4800 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4802 RefreshLines(id
, GetItemCount() - 1);
4805 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4808 if ( InReportView() )
4810 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4811 item
.m_width
= GetTextLength( item
.m_text
);
4813 wxListHeaderData
*column
= new wxListHeaderData( item
);
4814 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4816 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4819 wxListHeaderDataList::compatibility_iterator
4820 node
= m_columns
.Item( col
);
4821 m_columns
.Insert( node
, column
);
4822 m_aColWidths
.Insert( colWidthInfo
, col
);
4826 m_columns
.Append( column
);
4827 m_aColWidths
.Add( colWidthInfo
);
4832 // update all the items
4833 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4835 wxListLineData
* const line
= GetLine(i
);
4836 wxListItemData
* const data
= new wxListItemData(this);
4838 line
->m_items
.Insert(col
, data
);
4840 line
->m_items
.Append(data
);
4844 // invalidate it as it has to be recalculated
4849 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4852 wxClientDC
dc(this);
4854 dc
.SetFont( GetFont() );
4856 if (item
->GetImage() != -1)
4859 GetImageSize( item
->GetImage(), ix
, iy
);
4863 if (!item
->GetText().empty())
4866 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4873 // ----------------------------------------------------------------------------
4875 // ----------------------------------------------------------------------------
4877 wxListCtrlCompare list_ctrl_compare_func_2
;
4878 long list_ctrl_compare_data
;
4880 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4882 wxListLineData
*line1
= *arg1
;
4883 wxListLineData
*line2
= *arg2
;
4885 line1
->GetItem( 0, item
);
4886 wxUIntPtr data1
= item
.m_data
;
4887 line2
->GetItem( 0, item
);
4888 wxUIntPtr data2
= item
.m_data
;
4889 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4892 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4894 // selections won't make sense any more after sorting the items so reset
4896 HighlightAll(false);
4899 list_ctrl_compare_func_2
= fn
;
4900 list_ctrl_compare_data
= data
;
4901 m_lines
.Sort( list_ctrl_compare_func_1
);
4905 // ----------------------------------------------------------------------------
4907 // ----------------------------------------------------------------------------
4909 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4911 wxPrintf( "wxListMainWindow::OnScroll\n" );
4913 // HandleOnScroll( event );
4915 // update our idea of which lines are shown when we redraw the window the
4917 ResetVisibleLinesRange();
4919 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4921 wxGenericListCtrl
* lc
= GetListCtrl();
4922 wxCHECK_RET( lc
, _T("no listctrl window?") );
4924 lc
->m_headerWin
->Refresh();
4925 lc
->m_headerWin
->Update();
4929 int wxListMainWindow::GetCountPerPage() const
4931 if ( !m_linesPerPage
)
4933 wxConstCast(this, wxListMainWindow
)->
4934 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4937 return m_linesPerPage
;
4940 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4942 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4944 if ( m_lineFrom
== (size_t)-1 )
4946 size_t count
= GetItemCount();
4949 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4951 // this may happen if SetScrollbars() hadn't been called yet
4952 if ( m_lineFrom
>= count
)
4953 m_lineFrom
= count
- 1;
4955 // we redraw one extra line but this is needed to make the redrawing
4956 // logic work when there is a fractional number of lines on screen
4957 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4958 if ( m_lineTo
>= count
)
4959 m_lineTo
= count
- 1;
4961 else // empty control
4964 m_lineTo
= (size_t)-1;
4968 wxASSERT_MSG( IsEmpty() ||
4969 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4970 _T("GetVisibleLinesRange() returns incorrect result") );
4978 // -------------------------------------------------------------------------------------
4979 // wxGenericListCtrl
4980 // -------------------------------------------------------------------------------------
4982 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4984 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4985 EVT_SIZE(wxGenericListCtrl::OnSize
)
4986 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll
)
4989 void wxGenericListCtrl::Init()
4991 m_imageListNormal
= NULL
;
4992 m_imageListSmall
= NULL
;
4993 m_imageListState
= NULL
;
4995 m_ownsImageListNormal
=
4996 m_ownsImageListSmall
=
4997 m_ownsImageListState
= false;
5001 m_headerHeight
= wxRendererNative::Get().GetHeaderButtonHeight(this);
5004 wxGenericListCtrl::~wxGenericListCtrl()
5006 if (m_ownsImageListNormal
)
5007 delete m_imageListNormal
;
5008 if (m_ownsImageListSmall
)
5009 delete m_imageListSmall
;
5010 if (m_ownsImageListState
)
5011 delete m_imageListState
;
5014 void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
5016 bool needs_header
= HasHeader();
5017 bool has_header
= (m_headerWin
!= NULL
);
5019 if (needs_header
== has_header
)
5024 m_headerWin
= new wxListHeaderWindow
5026 this, wxID_ANY
, m_mainWin
,
5028 wxSize(GetClientSize().x
, m_headerHeight
),
5032 #if defined( __WXMAC__ ) && wxOSX_USE_COCOA_OR_CARBON
5034 #if wxOSX_USE_ATSU_TEXT
5035 font
.MacCreateFromThemeFont( kThemeSmallSystemFont
);
5037 font
.MacCreateFromUIFont( kCTFontSystemFontType
);
5039 m_headerWin
->SetFont( font
);
5042 GetSizer()->Prepend( m_headerWin
, 0, wxGROW
);
5046 GetSizer()->Detach( m_headerWin
);
5054 bool wxGenericListCtrl::Create(wxWindow
*parent
,
5059 const wxValidator
&validator
,
5060 const wxString
&name
)
5064 // just like in other ports, an assert will fail if the user doesn't give any type style:
5065 wxASSERT_MSG( (style
& wxLC_MASK_TYPE
),
5066 _T("wxListCtrl style should have exactly one mode bit set") );
5068 if ( !wxControl::Create( parent
, id
, pos
, size
, style
|wxVSCROLL
|wxHSCROLL
, validator
, name
) )
5072 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
5074 SetTargetWindow( m_mainWin
);
5076 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
5077 sizer
->Add( m_mainWin
, 1, wxGROW
);
5080 CreateOrDestroyHeaderWindowAsNeeded();
5082 SetInitialSize(size
);
5087 wxBorder
wxGenericListCtrl::GetDefaultBorder() const
5089 return wxBORDER_THEME
;
5093 WXLRESULT
wxGenericListCtrl::MSWWindowProc(WXUINT nMsg
,
5097 WXLRESULT rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
5100 // we need to process arrows ourselves for scrolling
5101 if ( nMsg
== WM_GETDLGCODE
)
5103 rc
|= DLGC_WANTARROWS
;
5111 wxSize
wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
5113 wxSize newsize
= size
;
5115 newsize
.y
-= m_headerWin
->GetSize().y
;
5120 void wxGenericListCtrl::OnScroll(wxScrollWinEvent
& event
)
5122 // update our idea of which lines are shown when we redraw
5123 // the window the next time
5124 m_mainWin
->ResetVisibleLinesRange();
5126 HandleOnScroll( event
);
5128 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
5130 m_headerWin
->Refresh();
5131 m_headerWin
->Update();
5135 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
5137 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
5138 _T("wxLC_VIRTUAL can't be [un]set") );
5140 long flag
= GetWindowStyle();
5144 if (style
& wxLC_MASK_TYPE
)
5145 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5146 if (style
& wxLC_MASK_ALIGN
)
5147 flag
&= ~wxLC_MASK_ALIGN
;
5148 if (style
& wxLC_MASK_SORT
)
5149 flag
&= ~wxLC_MASK_SORT
;
5157 // some styles can be set without recreating everything (as happens in
5158 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
5159 if ( !(style
& ~(wxLC_HRULES
| wxLC_VRULES
)) )
5162 wxWindow::SetWindowStyleFlag(flag
);
5166 SetWindowStyleFlag( flag
);
5170 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5174 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
5176 CreateOrDestroyHeaderWindowAsNeeded();
5178 GetSizer()->Layout();
5181 wxWindow::SetWindowStyleFlag( flag
);
5184 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5186 m_mainWin
->GetColumn( col
, item
);
5190 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5192 m_mainWin
->SetColumn( col
, item
);
5196 int wxGenericListCtrl::GetColumnWidth( int col
) const
5198 return m_mainWin
->GetColumnWidth( col
);
5201 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5203 m_mainWin
->SetColumnWidth( col
, width
);
5207 int wxGenericListCtrl::GetCountPerPage() const
5209 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5212 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5214 m_mainWin
->GetItem( info
);
5218 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5220 m_mainWin
->SetItem( info
);
5224 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5227 info
.m_text
= label
;
5228 info
.m_mask
= wxLIST_MASK_TEXT
;
5229 info
.m_itemId
= index
;
5233 info
.m_image
= imageId
;
5234 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5237 m_mainWin
->SetItem(info
);
5241 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5243 return m_mainWin
->GetItemState( item
, stateMask
);
5246 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5248 m_mainWin
->SetItemState( item
, state
, stateMask
);
5253 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5255 return SetItemColumnImage(item
, 0, image
);
5259 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5262 info
.m_image
= image
;
5263 info
.m_mask
= wxLIST_MASK_IMAGE
;
5264 info
.m_itemId
= item
;
5265 info
.m_col
= column
;
5266 m_mainWin
->SetItem( info
);
5270 wxString
wxGenericListCtrl::GetItemText( long item
) const
5272 return m_mainWin
->GetItemText(item
);
5275 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5277 m_mainWin
->SetItemText(item
, str
);
5280 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5283 info
.m_mask
= wxLIST_MASK_DATA
;
5284 info
.m_itemId
= item
;
5285 m_mainWin
->GetItem( info
);
5289 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
5292 info
.m_mask
= wxLIST_MASK_DATA
;
5293 info
.m_itemId
= item
;
5295 m_mainWin
->SetItem( info
);
5299 wxRect
wxGenericListCtrl::GetViewRect() const
5301 return m_mainWin
->GetViewRect();
5304 bool wxGenericListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
5306 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
, code
);
5309 bool wxGenericListCtrl::GetSubItemRect(long item
,
5312 int WXUNUSED(code
)) const
5314 if ( !m_mainWin
->GetSubItemRect( item
, subItem
, rect
) )
5317 if ( m_mainWin
->HasHeader() )
5318 rect
.y
+= m_headerHeight
+ 1;
5323 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5325 m_mainWin
->GetItemPosition( item
, pos
);
5329 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5334 int wxGenericListCtrl::GetItemCount() const
5336 return m_mainWin
->GetItemCount();
5339 int wxGenericListCtrl::GetColumnCount() const
5341 return m_mainWin
->GetColumnCount();
5344 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5346 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5349 wxSize
wxGenericListCtrl::GetItemSpacing() const
5351 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5353 return wxSize(spacing
, spacing
);
5356 #if WXWIN_COMPATIBILITY_2_6
5357 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5359 return m_mainWin
->GetItemSpacing( isSmall
);
5361 #endif // WXWIN_COMPATIBILITY_2_6
5363 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5366 info
.m_itemId
= item
;
5367 info
.SetTextColour( col
);
5368 m_mainWin
->SetItem( info
);
5371 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5374 info
.m_itemId
= item
;
5375 m_mainWin
->GetItem( info
);
5376 return info
.GetTextColour();
5379 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5382 info
.m_itemId
= item
;
5383 info
.SetBackgroundColour( col
);
5384 m_mainWin
->SetItem( info
);
5387 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5390 info
.m_itemId
= item
;
5391 m_mainWin
->GetItem( info
);
5392 return info
.GetBackgroundColour();
5395 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5398 info
.m_itemId
= item
;
5400 m_mainWin
->SetItem( info
);
5403 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5406 info
.m_itemId
= item
;
5407 m_mainWin
->GetItem( info
);
5408 return info
.GetFont();
5411 int wxGenericListCtrl::GetSelectedItemCount() const
5413 return m_mainWin
->GetSelectedItemCount();
5416 wxColour
wxGenericListCtrl::GetTextColour() const
5418 return GetForegroundColour();
5421 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5423 SetForegroundColour(col
);
5426 long wxGenericListCtrl::GetTopItem() const
5429 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5433 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5435 return m_mainWin
->GetNextItem( item
, geom
, state
);
5438 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5440 if (which
== wxIMAGE_LIST_NORMAL
)
5441 return m_imageListNormal
;
5442 else if (which
== wxIMAGE_LIST_SMALL
)
5443 return m_imageListSmall
;
5444 else if (which
== wxIMAGE_LIST_STATE
)
5445 return m_imageListState
;
5450 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5452 if ( which
== wxIMAGE_LIST_NORMAL
)
5454 if (m_ownsImageListNormal
)
5455 delete m_imageListNormal
;
5456 m_imageListNormal
= imageList
;
5457 m_ownsImageListNormal
= false;
5459 else if ( which
== wxIMAGE_LIST_SMALL
)
5461 if (m_ownsImageListSmall
)
5462 delete m_imageListSmall
;
5463 m_imageListSmall
= imageList
;
5464 m_ownsImageListSmall
= false;
5466 else if ( which
== wxIMAGE_LIST_STATE
)
5468 if (m_ownsImageListState
)
5469 delete m_imageListState
;
5470 m_imageListState
= imageList
;
5471 m_ownsImageListState
= false;
5474 m_mainWin
->SetImageList( imageList
, which
);
5477 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5479 SetImageList(imageList
, which
);
5480 if ( which
== wxIMAGE_LIST_NORMAL
)
5481 m_ownsImageListNormal
= true;
5482 else if ( which
== wxIMAGE_LIST_SMALL
)
5483 m_ownsImageListSmall
= true;
5484 else if ( which
== wxIMAGE_LIST_STATE
)
5485 m_ownsImageListState
= true;
5488 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5493 bool wxGenericListCtrl::DeleteItem( long item
)
5495 m_mainWin
->DeleteItem( item
);
5499 bool wxGenericListCtrl::DeleteAllItems()
5501 m_mainWin
->DeleteAllItems();
5505 bool wxGenericListCtrl::DeleteAllColumns()
5507 size_t count
= m_mainWin
->m_columns
.GetCount();
5508 for ( size_t n
= 0; n
< count
; n
++ )
5513 void wxGenericListCtrl::ClearAll()
5515 m_mainWin
->DeleteEverything();
5518 bool wxGenericListCtrl::DeleteColumn( int col
)
5520 m_mainWin
->DeleteColumn( col
);
5522 // if we don't have the header any longer, we need to relayout the window
5523 // if ( !GetColumnCount() )
5528 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5529 wxClassInfo
* textControlClass
)
5531 return m_mainWin
->EditLabel( item
, textControlClass
);
5534 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5536 return m_mainWin
->GetEditControl();
5539 bool wxGenericListCtrl::EnsureVisible( long item
)
5541 m_mainWin
->EnsureVisible( item
);
5545 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5547 return m_mainWin
->FindItem( start
, str
, partial
);
5550 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5552 return m_mainWin
->FindItem( start
, data
);
5555 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5556 int WXUNUSED(direction
))
5558 return m_mainWin
->FindItem( pt
);
5561 // TODO: sub item hit testing
5562 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5564 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5567 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5569 m_mainWin
->InsertItem( info
);
5570 return info
.m_itemId
;
5573 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5576 info
.m_text
= label
;
5577 info
.m_mask
= wxLIST_MASK_TEXT
;
5578 info
.m_itemId
= index
;
5579 return InsertItem( info
);
5582 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5585 info
.m_mask
= wxLIST_MASK_IMAGE
;
5586 info
.m_image
= imageIndex
;
5587 info
.m_itemId
= index
;
5588 return InsertItem( info
);
5591 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5594 info
.m_text
= label
;
5595 info
.m_image
= imageIndex
;
5596 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5597 info
.m_itemId
= index
;
5598 return InsertItem( info
);
5601 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5603 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5605 m_mainWin
->InsertColumn( col
, item
);
5607 // if we hadn't had a header before but have one now
5608 // then we need to relayout the window
5609 // if ( GetColumnCount() == 1 && m_mainWin->HasHeader() )
5611 m_headerWin
->Refresh();
5616 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5617 int format
, int width
)
5620 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5621 item
.m_text
= heading
;
5624 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5625 item
.m_width
= width
;
5628 item
.m_format
= format
;
5630 return InsertColumn( col
, item
);
5633 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
5635 return m_mainWin
->ScrollList(dx
, dy
);
5639 // fn is a function which takes 3 long arguments: item1, item2, data.
5640 // item1 is the long data associated with a first item (NOT the index).
5641 // item2 is the long data associated with a second item (NOT the index).
5642 // data is the same value as passed to SortItems.
5643 // The return value is a negative number if the first item should precede the second
5644 // item, a positive number of the second item should precede the first,
5645 // or zero if the two items are equivalent.
5646 // data is arbitrary data to be passed to the sort function.
5648 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5650 m_mainWin
->SortItems( fn
, data
);
5654 // ----------------------------------------------------------------------------
5656 // ----------------------------------------------------------------------------
5658 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5660 if (!m_mainWin
) return;
5662 // We need to override OnSize so that our scrolled
5663 // window a) does call Layout() to use sizers for
5664 // positioning the controls but b) does not query
5665 // the sizer for their size and use that for setting
5666 // the scrollable area as set that ourselves by
5667 // calling SetScrollbar() further down.
5671 m_mainWin
->RecalculatePositions();
5676 void wxGenericListCtrl::OnInternalIdle()
5678 wxWindow::OnInternalIdle();
5680 if (m_mainWin
->m_dirty
)
5681 m_mainWin
->RecalculatePositions();
5684 // ----------------------------------------------------------------------------
5686 // ----------------------------------------------------------------------------
5688 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5692 m_mainWin
->SetBackgroundColour( colour
);
5693 m_mainWin
->m_dirty
= true;
5699 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5701 if ( !wxWindow::SetForegroundColour( colour
) )
5706 m_mainWin
->SetForegroundColour( colour
);
5707 m_mainWin
->m_dirty
= true;
5711 m_headerWin
->SetForegroundColour( colour
);
5716 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5718 if ( !wxWindow::SetFont( font
) )
5723 m_mainWin
->SetFont( font
);
5724 m_mainWin
->m_dirty
= true;
5729 m_headerWin
->SetFont( font
);
5730 // CalculateAndSetHeaderHeight();
5740 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5743 // Use the same color scheme as wxListBox
5744 return wxListBox::GetClassDefaultAttributes(variant
);
5746 wxUnusedVar(variant
);
5747 wxVisualAttributes attr
;
5748 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT
);
5749 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5750 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5755 // ----------------------------------------------------------------------------
5756 // methods forwarded to m_mainWin
5757 // ----------------------------------------------------------------------------
5759 #if wxUSE_DRAG_AND_DROP
5761 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5763 m_mainWin
->SetDropTarget( dropTarget
);
5766 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5768 return m_mainWin
->GetDropTarget();
5773 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5775 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5778 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5780 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5783 wxColour
wxGenericListCtrl::GetForegroundColour() const
5785 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5788 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5791 return m_mainWin
->PopupMenu( menu
, x
, y
);
5797 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5799 m_mainWin
->DoClientToScreen(x
, y
);
5802 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5804 m_mainWin
->DoScreenToClient(x
, y
);
5807 void wxGenericListCtrl::SetFocus()
5809 // The test in window.cpp fails as we are a composite
5810 // window, so it checks against "this", but not m_mainWin.
5811 if ( DoFindFocus() != this )
5812 m_mainWin
->SetFocus();
5815 wxSize
wxGenericListCtrl::DoGetBestSize() const
5817 // Something is better than nothing...
5818 // 100x80 is what the MSW version will get from the default
5819 // wxControl::DoGetBestSize
5820 return wxSize(100, 80);
5823 // ----------------------------------------------------------------------------
5824 // virtual list control support
5825 // ----------------------------------------------------------------------------
5827 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5829 // this is a pure virtual function, in fact - which is not really pure
5830 // because the controls which are not virtual don't need to implement it
5831 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5833 return wxEmptyString
;
5836 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5838 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5840 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5844 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5847 return OnGetItemImage(item
);
5853 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5855 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5856 _T("invalid item index in OnGetItemAttr()") );
5858 // no attributes by default
5862 void wxGenericListCtrl::SetItemCount(long count
)
5864 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5866 m_mainWin
->SetItemCount(count
);
5869 void wxGenericListCtrl::RefreshItem(long item
)
5871 m_mainWin
->RefreshLine(item
);
5874 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5876 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5879 // Generic wxListCtrl is more or less a container for two other
5880 // windows which drawings are done upon. These are namely
5881 // 'm_headerWin' and 'm_mainWin'.
5882 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5883 // behaviour wxListCtrl has under wxMSW.
5885 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5889 // The easy case, no rectangle specified.
5891 m_headerWin
->Refresh(eraseBackground
);
5894 m_mainWin
->Refresh(eraseBackground
);
5898 // Refresh the header window
5901 wxRect rectHeader
= m_headerWin
->GetRect();
5902 rectHeader
.Intersect(*rect
);
5903 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5906 m_headerWin
->GetPosition(&x
, &y
);
5907 rectHeader
.Offset(-x
, -y
);
5908 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5912 // Refresh the main window
5915 wxRect rectMain
= m_mainWin
->GetRect();
5916 rectMain
.Intersect(*rect
);
5917 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5920 m_mainWin
->GetPosition(&x
, &y
);
5921 rectMain
.Offset(-x
, -y
);
5922 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5928 #endif // wxUSE_LISTCTRL