1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 // 1. we need to implement searching/sorting for virtual controls somehow
14 // 2. when changing selection the lines are refreshed twice
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #include "wx/listctrl.h"
28 #if (!defined(__WXMSW__) || defined(__WXUNIVERSAL__)) && !defined(__WXMAC__)
29 // if we have a native version, its implementation file does all this
30 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
31 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
32 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
34 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
38 #include "wx/scrolwin.h"
40 #include "wx/settings.h"
41 #include "wx/dynarray.h"
42 #include "wx/dcclient.h"
43 #include "wx/dcscreen.h"
45 #include "wx/settings.h"
48 #include "wx/imaglist.h"
49 #include "wx/selstore.h"
50 #include "wx/renderer.h"
53 #include "wx/mac/private.h"
57 // NOTE: If using the wxListBox visual attributes works everywhere then this can
58 // be removed, as well as the #else case below.
59 #define _USE_VISATTR 0
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // // the height of the header window (FIXME: should depend on its font!)
67 // static const int HEADER_HEIGHT = 23;
69 static const int SCROLL_UNIT_X
= 15;
71 // the spacing between the lines (in report mode)
72 static const int LINE_SPACING
= 0;
74 // extra margins around the text label
76 static const int EXTRA_WIDTH
= 6;
78 static const int EXTRA_WIDTH
= 4;
80 static const int EXTRA_HEIGHT
= 4;
82 // margin between the window and the items
83 static const int EXTRA_BORDER_X
= 2;
84 static const int EXTRA_BORDER_Y
= 2;
86 // offset for the header window
87 static const int HEADER_OFFSET_X
= 0;
88 static const int HEADER_OFFSET_Y
= 0;
90 // margin between rows of icons in [small] icon view
91 static const int MARGIN_BETWEEN_ROWS
= 6;
93 // when autosizing the columns, add some slack
94 static const int AUTOSIZE_COL_MARGIN
= 10;
96 // default width for the header columns
97 static const int WIDTH_COL_DEFAULT
= 80;
99 // the space between the image and the text in the report mode
100 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
102 // the space between the image and the text in the report mode in header
103 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
105 // ============================================================================
107 // ============================================================================
109 //-----------------------------------------------------------------------------
110 // wxColWidthInfo (internal)
111 //-----------------------------------------------------------------------------
113 struct wxColWidthInfo
116 bool bNeedsUpdate
; // only set to true when an item whose
117 // width == nMaxWidth is removed
119 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
122 bNeedsUpdate
= needsUpdate
;
126 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
128 //-----------------------------------------------------------------------------
129 // wxListItemData (internal)
130 //-----------------------------------------------------------------------------
135 wxListItemData(wxListMainWindow
*owner
);
138 void SetItem( const wxListItem
&info
);
139 void SetImage( int image
) { m_image
= image
; }
140 void SetData( wxUIntPtr data
) { m_data
= data
; }
141 void SetPosition( int x
, int y
);
142 void SetSize( int width
, int height
);
144 bool HasText() const { return !m_text
.empty(); }
145 const wxString
& GetText() const { return m_text
; }
146 void SetText(const wxString
& text
) { m_text
= text
; }
148 // we can't use empty string for measuring the string width/height, so
149 // always return something
150 wxString
GetTextForMeasuring() const
152 wxString s
= GetText();
159 bool IsHit( int x
, int y
) const;
163 int GetWidth() const;
164 int GetHeight() const;
166 int GetImage() const { return m_image
; }
167 bool HasImage() const { return GetImage() != -1; }
169 void GetItem( wxListItem
&info
) const;
171 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
172 wxListItemAttr
*GetAttr() const { return m_attr
; }
175 // the item image or -1
178 // user data associated with the item
181 // the item coordinates are not used in report mode; instead this pointer is
182 // NULL and the owner window is used to retrieve the item position and size
185 // the list ctrl we are in
186 wxListMainWindow
*m_owner
;
188 // custom attributes or NULL
189 wxListItemAttr
*m_attr
;
192 // common part of all ctors
198 //-----------------------------------------------------------------------------
199 // wxListHeaderData (internal)
200 //-----------------------------------------------------------------------------
202 class wxListHeaderData
: public wxObject
206 wxListHeaderData( const wxListItem
&info
);
207 void SetItem( const wxListItem
&item
);
208 void SetPosition( int x
, int y
);
209 void SetWidth( int w
);
210 void SetState( int state
);
211 void SetFormat( int format
);
212 void SetHeight( int h
);
213 bool HasImage() const;
215 bool HasText() const { return !m_text
.empty(); }
216 const wxString
& GetText() const { return m_text
; }
217 void SetText(const wxString
& text
) { m_text
= text
; }
219 void GetItem( wxListItem
&item
);
221 bool IsHit( int x
, int y
) const;
222 int GetImage() const;
223 int GetWidth() const;
224 int GetFormat() const;
225 int GetState() const;
242 //-----------------------------------------------------------------------------
243 // wxListLineData (internal)
244 //-----------------------------------------------------------------------------
246 WX_DECLARE_EXPORTED_LIST(wxListItemData
, wxListItemDataList
);
247 #include "wx/listimpl.cpp"
248 WX_DEFINE_LIST(wxListItemDataList
)
253 // the list of subitems: only may have more than one item in report mode
254 wxListItemDataList m_items
;
256 // this is not used in report view
268 // the part to be highlighted
269 wxRect m_rectHighlight
;
271 // extend all our rects to be centered inside the one of given width
272 void ExtendWidth(wxCoord w
)
274 wxASSERT_MSG( m_rectAll
.width
<= w
,
275 _T("width can only be increased") );
278 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
279 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
280 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
285 // is this item selected? [NB: not used in virtual mode]
288 // back pointer to the list ctrl
289 wxListMainWindow
*m_owner
;
292 wxListLineData(wxListMainWindow
*owner
);
296 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
300 // are we in report mode?
301 inline bool InReportView() const;
303 // are we in virtual report mode?
304 inline bool IsVirtual() const;
306 // these 2 methods shouldn't be called for report view controls, in that
307 // case we determine our position/size ourselves
309 // calculate the size of the line
310 void CalculateSize( wxDC
*dc
, int spacing
);
312 // remember the position this line appears at
313 void SetPosition( int x
, int y
, int spacing
);
317 void SetImage( int image
) { SetImage(0, image
); }
318 int GetImage() const { return GetImage(0); }
319 void SetImage( int index
, int image
);
320 int GetImage( int index
) const;
322 bool HasImage() const { return GetImage() != -1; }
323 bool HasText() const { return !GetText(0).empty(); }
325 void SetItem( int index
, const wxListItem
&info
);
326 void GetItem( int index
, wxListItem
&info
);
328 wxString
GetText(int index
) const;
329 void SetText( int index
, const wxString
& s
);
331 wxListItemAttr
*GetAttr() const;
332 void SetAttr(wxListItemAttr
*attr
);
334 // return true if the highlighting really changed
335 bool Highlight( bool on
);
337 void ReverseHighlight();
339 bool IsHighlighted() const
341 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
343 return m_highlighted
;
346 // draw the line on the given DC in icon/list mode
347 void Draw( wxDC
*dc
);
349 // the same in report mode
350 void DrawInReportMode( wxDC
*dc
,
352 const wxRect
& rectHL
,
356 // set the line to contain num items (only can be > 1 in report mode)
357 void InitItems( int num
);
359 // get the mode (i.e. style) of the list control
360 inline int GetMode() const;
362 // prepare the DC for drawing with these item's attributes, return true if
363 // we need to draw the items background to highlight it, false otherwise
364 bool SetAttributes(wxDC
*dc
,
365 const wxListItemAttr
*attr
,
368 // draw the text on the DC with the correct justification; also add an
369 // ellipsis if the text is too large to fit in the current width
370 void DrawTextFormatted(wxDC
*dc
,
371 const wxString
&text
,
374 int yMid
, // this is middle, not top, of the text
378 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
379 #include "wx/arrimpl.cpp"
380 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
382 //-----------------------------------------------------------------------------
383 // wxListHeaderWindow (internal)
384 //-----------------------------------------------------------------------------
386 class wxListHeaderWindow
: public wxWindow
389 wxListMainWindow
*m_owner
;
390 const wxCursor
*m_currentCursor
;
391 wxCursor
*m_resizeCursor
;
394 // column being resized or -1
397 // divider line position in logical (unscrolled) coords
400 // minimal position beyond which the divider line
401 // can't be dragged in logical coords
405 wxListHeaderWindow();
407 wxListHeaderWindow( wxWindow
*win
,
409 wxListMainWindow
*owner
,
410 const wxPoint
&pos
= wxDefaultPosition
,
411 const wxSize
&size
= wxDefaultSize
,
413 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
415 virtual ~wxListHeaderWindow();
418 void AdjustDC( wxDC
& dc
);
420 void OnPaint( wxPaintEvent
&event
);
421 void OnMouse( wxMouseEvent
&event
);
422 void OnSetFocus( wxFocusEvent
&event
);
428 // common part of all ctors
431 // generate and process the list event of the given type, return true if
432 // it wasn't vetoed, i.e. if we should proceed
433 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
435 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
436 DECLARE_EVENT_TABLE()
439 //-----------------------------------------------------------------------------
440 // wxListRenameTimer (internal)
441 //-----------------------------------------------------------------------------
443 class wxListRenameTimer
: public wxTimer
446 wxListMainWindow
*m_owner
;
449 wxListRenameTimer( wxListMainWindow
*owner
);
453 //-----------------------------------------------------------------------------
454 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
455 //-----------------------------------------------------------------------------
457 class wxListTextCtrlWrapper
: public wxEvtHandler
460 // NB: text must be a valid object but not Create()d yet
461 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
465 wxTextCtrl
*GetText() const { return m_text
; }
467 void EndEdit( bool discardChanges
);
470 void OnChar( wxKeyEvent
&event
);
471 void OnKeyUp( wxKeyEvent
&event
);
472 void OnKillFocus( wxFocusEvent
&event
);
474 bool AcceptChanges();
475 void Finish( bool setfocus
);
478 wxListMainWindow
*m_owner
;
480 wxString m_startValue
;
482 bool m_aboutToFinish
;
484 DECLARE_EVENT_TABLE()
487 //-----------------------------------------------------------------------------
488 // wxListMainWindow (internal)
489 //-----------------------------------------------------------------------------
491 WX_DECLARE_EXPORTED_LIST(wxListHeaderData
, wxListHeaderDataList
);
492 #include "wx/listimpl.cpp"
493 WX_DEFINE_LIST(wxListHeaderDataList
)
495 class wxListMainWindow
: public wxScrolledWindow
499 wxListMainWindow( wxWindow
*parent
,
501 const wxPoint
& pos
= wxDefaultPosition
,
502 const wxSize
& size
= wxDefaultSize
,
504 const wxString
&name
= _T("listctrlmainwindow") );
506 virtual ~wxListMainWindow();
508 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
510 // return true if this is a virtual list control
511 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
513 // return true if the control is in report mode
514 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
516 // return true if we are in single selection mode, false if multi sel
517 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
519 // do we have a header window?
520 bool HasHeader() const
521 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
523 void HighlightAll( bool on
);
525 // all these functions only do something if the line is currently visible
527 // change the line "selected" state, return true if it really changed
528 bool HighlightLine( size_t line
, bool highlight
= true);
530 // as HighlightLine() but do it for the range of lines: this is incredibly
531 // more efficient for virtual list controls!
533 // NB: unlike HighlightLine() this one does refresh the lines on screen
534 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
536 // toggle the line state and refresh it
537 void ReverseHighlight( size_t line
)
538 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
540 // return true if the line is highlighted
541 bool IsHighlighted(size_t line
) const;
543 // refresh one or several lines at once
544 void RefreshLine( size_t line
);
545 void RefreshLines( size_t lineFrom
, size_t lineTo
);
547 // refresh all selected items
548 void RefreshSelected();
550 // refresh all lines below the given one: the difference with
551 // RefreshLines() is that the index here might not be a valid one (happens
552 // when the last line is deleted)
553 void RefreshAfter( size_t lineFrom
);
555 // the methods which are forwarded to wxListLineData itself in list/icon
556 // modes but are here because the lines don't store their positions in the
559 // get the bound rect for the entire line
560 wxRect
GetLineRect(size_t line
) const;
562 // get the bound rect of the label
563 wxRect
GetLineLabelRect(size_t line
) const;
565 // get the bound rect of the items icon (only may be called if we do have
567 wxRect
GetLineIconRect(size_t line
) const;
569 // get the rect to be highlighted when the item has focus
570 wxRect
GetLineHighlightRect(size_t line
) const;
572 // get the size of the total line rect
573 wxSize
GetLineSize(size_t line
) const
574 { return GetLineRect(line
).GetSize(); }
576 // return the hit code for the corresponding position (in this line)
577 long HitTestLine(size_t line
, int x
, int y
) const;
579 // bring the selected item into view, scrolling to it if necessary
580 void MoveToItem(size_t item
);
582 bool ScrollList( int WXUNUSED(dx
), int dy
);
584 // bring the current item into view
585 void MoveToFocus() { MoveToItem(m_current
); }
587 // start editing the label of the given item
588 wxTextCtrl
*EditLabel(long item
,
589 wxClassInfo
* textControlClass
= CLASSINFO(wxTextCtrl
));
590 wxTextCtrl
*GetEditControl() const
592 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
595 void ResetTextControl(wxTextCtrl
*text
)
598 m_textctrlWrapper
= NULL
;
601 void OnRenameTimer();
602 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
603 void OnRenameCancelled(size_t itemEdit
);
605 void OnMouse( wxMouseEvent
&event
);
607 // called to switch the selection from the current item to newCurrent,
608 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
610 void OnChar( wxKeyEvent
&event
);
611 void OnKeyDown( wxKeyEvent
&event
);
612 void OnKeyUp( wxKeyEvent
&event
);
613 void OnSetFocus( wxFocusEvent
&event
);
614 void OnKillFocus( wxFocusEvent
&event
);
615 void OnScroll( wxScrollWinEvent
& event
);
617 void OnPaint( wxPaintEvent
&event
);
619 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
620 void GetImageSize( int index
, int &width
, int &height
) const;
621 int GetTextLength( const wxString
&s
) const;
623 void SetImageList( wxImageList
*imageList
, int which
);
624 void SetItemSpacing( int spacing
, bool isSmall
= false );
625 int GetItemSpacing( bool isSmall
= false );
627 void SetColumn( int col
, wxListItem
&item
);
628 void SetColumnWidth( int col
, int width
);
629 void GetColumn( int col
, wxListItem
&item
) const;
630 int GetColumnWidth( int col
) const;
631 int GetColumnCount() const { return m_columns
.GetCount(); }
633 // returns the sum of the heights of all columns
634 int GetHeaderWidth() const;
636 int GetCountPerPage() const;
638 void SetItem( wxListItem
&item
);
639 void GetItem( wxListItem
&item
) const;
640 void SetItemState( long item
, long state
, long stateMask
);
641 void SetItemStateAll( long state
, long stateMask
);
642 int GetItemState( long item
, long stateMask
) const;
643 void GetItemRect( long index
, wxRect
&rect
) const;
644 wxRect
GetViewRect() const;
645 bool GetItemPosition( long item
, wxPoint
& pos
) const;
646 int GetSelectedItemCount() const;
648 wxString
GetItemText(long item
) const
651 info
.m_mask
= wxLIST_MASK_TEXT
;
652 info
.m_itemId
= item
;
657 void SetItemText(long item
, const wxString
& value
)
660 info
.m_mask
= wxLIST_MASK_TEXT
;
661 info
.m_itemId
= item
;
666 // set the scrollbars and update the positions of the items
667 void RecalculatePositions(bool noRefresh
= false);
669 // refresh the window and the header
672 long GetNextItem( long item
, int geometry
, int state
) const;
673 void DeleteItem( long index
);
674 void DeleteAllItems();
675 void DeleteColumn( int col
);
676 void DeleteEverything();
677 void EnsureVisible( long index
);
678 long FindItem( long start
, const wxString
& str
, bool partial
= false );
679 long FindItem( long start
, wxUIntPtr data
);
680 long FindItem( const wxPoint
& pt
);
681 long HitTest( int x
, int y
, int &flags
) const;
682 void InsertItem( wxListItem
&item
);
683 void InsertColumn( long col
, wxListItem
&item
);
684 int GetItemWidthWithImage(wxListItem
* item
);
685 void SortItems( wxListCtrlCompare fn
, long data
);
687 size_t GetItemCount() const;
688 bool IsEmpty() const { return GetItemCount() == 0; }
689 void SetItemCount(long count
);
691 // change the current (== focused) item, send a notification event
692 void ChangeCurrent(size_t current
);
693 void ResetCurrent() { ChangeCurrent((size_t)-1); }
694 bool HasCurrent() const { return m_current
!= (size_t)-1; }
696 // send out a wxListEvent
697 void SendNotify( size_t line
,
699 const wxPoint
& point
= wxDefaultPosition
);
701 // override base class virtual to reset m_lineHeight when the font changes
702 virtual bool SetFont(const wxFont
& font
)
704 if ( !wxScrolledWindow::SetFont(font
) )
712 // these are for wxListLineData usage only
714 // get the backpointer to the list ctrl
715 wxGenericListCtrl
*GetListCtrl() const
717 return wxStaticCast(GetParent(), wxGenericListCtrl
);
720 // get the height of all lines (assuming they all do have the same height)
721 wxCoord
GetLineHeight() const;
723 // get the y position of the given line (only for report view)
724 wxCoord
GetLineY(size_t line
) const;
726 // get the brush to use for the item highlighting
727 wxBrush
*GetHighlightBrush() const
729 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
732 bool HasFocus() const
738 // the array of all line objects for a non virtual list control (for the
739 // virtual list control we only ever use m_lines[0])
740 wxListLineDataArray m_lines
;
742 // the list of column objects
743 wxListHeaderDataList m_columns
;
745 // currently focused item or -1
748 // the number of lines per page
751 // this flag is set when something which should result in the window
752 // redrawing happens (i.e. an item was added or deleted, or its appearance
753 // changed) and OnPaint() doesn't redraw the window while it is set which
754 // allows to minimize the number of repaintings when a lot of items are
755 // being added. The real repainting occurs only after the next OnIdle()
759 wxColour
*m_highlightColour
;
760 wxImageList
*m_small_image_list
;
761 wxImageList
*m_normal_image_list
;
763 int m_normal_spacing
;
767 wxTimer
*m_renameTimer
;
771 ColWidthArray m_aColWidths
;
773 // for double click logic
774 size_t m_lineLastClicked
,
775 m_lineBeforeLastClicked
,
776 m_lineSelectSingleOnUp
;
779 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
781 // the total count of items in a virtual list control
784 // the object maintaining the items selection state, only used in virtual
786 wxSelectionStore m_selStore
;
788 // common part of all ctors
791 // get the line data for the given index
792 wxListLineData
*GetLine(size_t n
) const
794 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
798 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
805 // get a dummy line which can be used for geometry calculations and such:
806 // you must use GetLine() if you want to really draw the line
807 wxListLineData
*GetDummyLine() const;
809 // cache the line data of the n-th line in m_lines[0]
810 void CacheLineData(size_t line
);
812 // get the range of visible lines
813 void GetVisibleLinesRange(size_t *from
, size_t *to
);
815 // force us to recalculate the range of visible lines
816 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
818 // get the colour to be used for drawing the rules
819 wxColour
GetRuleColour() const
821 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
825 // initialize the current item if needed
826 void UpdateCurrent();
828 // delete all items but don't refresh: called from dtor
829 void DoDeleteAllItems();
831 // the height of one line using the current font
832 wxCoord m_lineHeight
;
834 // the total header width or 0 if not calculated yet
835 wxCoord m_headerWidth
;
837 // the first and last lines being shown on screen right now (inclusive),
838 // both may be -1 if they must be calculated so never access them directly:
839 // use GetVisibleLinesRange() above instead
843 // the brushes to use for item highlighting when we do/don't have focus
844 wxBrush
*m_highlightBrush
,
845 *m_highlightUnfocusedBrush
;
847 // wrapper around the text control currently used for in place editing or
848 // NULL if no item is being edited
849 wxListTextCtrlWrapper
*m_textctrlWrapper
;
852 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
853 DECLARE_EVENT_TABLE()
855 friend class wxGenericListCtrl
;
859 wxListItemData::~wxListItemData()
861 // in the virtual list control the attributes are managed by the main
862 // program, so don't delete them
863 if ( !m_owner
->IsVirtual() )
869 void wxListItemData::Init()
877 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
883 if ( owner
->InReportView() )
889 void wxListItemData::SetItem( const wxListItem
&info
)
891 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
892 SetText(info
.m_text
);
893 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
894 m_image
= info
.m_image
;
895 if ( info
.m_mask
& wxLIST_MASK_DATA
)
896 m_data
= info
.m_data
;
898 if ( info
.HasAttributes() )
901 m_attr
->AssignFrom(*info
.GetAttributes());
903 m_attr
= new wxListItemAttr(*info
.GetAttributes());
911 m_rect
->width
= info
.m_width
;
915 void wxListItemData::SetPosition( int x
, int y
)
917 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
923 void wxListItemData::SetSize( int width
, int height
)
925 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
928 m_rect
->width
= width
;
930 m_rect
->height
= height
;
933 bool wxListItemData::IsHit( int x
, int y
) const
935 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
937 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
940 int wxListItemData::GetX() const
942 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
947 int wxListItemData::GetY() const
949 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
954 int wxListItemData::GetWidth() const
956 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
958 return m_rect
->width
;
961 int wxListItemData::GetHeight() const
963 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
965 return m_rect
->height
;
968 void wxListItemData::GetItem( wxListItem
&info
) const
970 long mask
= info
.m_mask
;
972 // by default, get everything for backwards compatibility
975 if ( mask
& wxLIST_MASK_TEXT
)
976 info
.m_text
= m_text
;
977 if ( mask
& wxLIST_MASK_IMAGE
)
978 info
.m_image
= m_image
;
979 if ( mask
& wxLIST_MASK_DATA
)
980 info
.m_data
= m_data
;
984 if ( m_attr
->HasTextColour() )
985 info
.SetTextColour(m_attr
->GetTextColour());
986 if ( m_attr
->HasBackgroundColour() )
987 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
988 if ( m_attr
->HasFont() )
989 info
.SetFont(m_attr
->GetFont());
993 //-----------------------------------------------------------------------------
995 //-----------------------------------------------------------------------------
997 void wxListHeaderData::Init()
1009 wxListHeaderData::wxListHeaderData()
1014 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1021 void wxListHeaderData::SetItem( const wxListItem
&item
)
1023 m_mask
= item
.m_mask
;
1025 if ( m_mask
& wxLIST_MASK_TEXT
)
1026 m_text
= item
.m_text
;
1028 if ( m_mask
& wxLIST_MASK_IMAGE
)
1029 m_image
= item
.m_image
;
1031 if ( m_mask
& wxLIST_MASK_FORMAT
)
1032 m_format
= item
.m_format
;
1034 if ( m_mask
& wxLIST_MASK_WIDTH
)
1035 SetWidth(item
.m_width
);
1037 if ( m_mask
& wxLIST_MASK_STATE
)
1038 SetState(item
.m_state
);
1041 void wxListHeaderData::SetPosition( int x
, int y
)
1047 void wxListHeaderData::SetHeight( int h
)
1052 void wxListHeaderData::SetWidth( int w
)
1054 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1057 void wxListHeaderData::SetState( int flag
)
1062 void wxListHeaderData::SetFormat( int format
)
1067 bool wxListHeaderData::HasImage() const
1069 return m_image
!= -1;
1072 bool wxListHeaderData::IsHit( int x
, int y
) const
1074 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1077 void wxListHeaderData::GetItem( wxListItem
& item
)
1079 item
.m_mask
= m_mask
;
1080 item
.m_text
= m_text
;
1081 item
.m_image
= m_image
;
1082 item
.m_format
= m_format
;
1083 item
.m_width
= m_width
;
1084 item
.m_state
= m_state
;
1087 int wxListHeaderData::GetImage() const
1092 int wxListHeaderData::GetWidth() const
1097 int wxListHeaderData::GetFormat() const
1102 int wxListHeaderData::GetState() const
1107 //-----------------------------------------------------------------------------
1109 //-----------------------------------------------------------------------------
1111 inline int wxListLineData::GetMode() const
1113 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1116 inline bool wxListLineData::InReportView() const
1118 return m_owner
->HasFlag(wxLC_REPORT
);
1121 inline bool wxListLineData::IsVirtual() const
1123 return m_owner
->IsVirtual();
1126 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1130 if ( InReportView() )
1133 m_gi
= new GeometryInfo
;
1135 m_highlighted
= false;
1137 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1140 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1142 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1143 wxCHECK_RET( node
, _T("no subitems at all??") );
1145 wxListItemData
*item
= node
->GetData();
1150 switch ( GetMode() )
1153 case wxLC_SMALL_ICON
:
1154 m_gi
->m_rectAll
.width
= spacing
;
1156 s
= item
->GetText();
1161 m_gi
->m_rectLabel
.width
=
1162 m_gi
->m_rectLabel
.height
= 0;
1166 dc
->GetTextExtent( s
, &lw
, &lh
);
1170 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1172 m_gi
->m_rectAll
.width
= lw
;
1174 m_gi
->m_rectLabel
.width
= lw
;
1175 m_gi
->m_rectLabel
.height
= lh
;
1178 if (item
->HasImage())
1181 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1182 m_gi
->m_rectIcon
.width
= w
+ 8;
1183 m_gi
->m_rectIcon
.height
= h
+ 8;
1185 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1186 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1187 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1188 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1191 if ( item
->HasText() )
1193 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1194 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1196 else // no text, highlight the icon
1198 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1199 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1204 s
= item
->GetTextForMeasuring();
1206 dc
->GetTextExtent( s
, &lw
, &lh
);
1210 m_gi
->m_rectLabel
.width
= lw
;
1211 m_gi
->m_rectLabel
.height
= lh
;
1213 m_gi
->m_rectAll
.width
= lw
;
1214 m_gi
->m_rectAll
.height
= lh
;
1216 if (item
->HasImage())
1219 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1220 m_gi
->m_rectIcon
.width
= w
;
1221 m_gi
->m_rectIcon
.height
= h
;
1223 m_gi
->m_rectAll
.width
+= 4 + w
;
1224 if (h
> m_gi
->m_rectAll
.height
)
1225 m_gi
->m_rectAll
.height
= h
;
1228 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1229 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1233 wxFAIL_MSG( _T("unexpected call to SetSize") );
1237 wxFAIL_MSG( _T("unknown mode") );
1242 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1244 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1245 wxCHECK_RET( node
, _T("no subitems at all??") );
1247 wxListItemData
*item
= node
->GetData();
1249 switch ( GetMode() )
1252 case wxLC_SMALL_ICON
:
1253 m_gi
->m_rectAll
.x
= x
;
1254 m_gi
->m_rectAll
.y
= y
;
1256 if ( item
->HasImage() )
1258 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1259 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1260 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1263 if ( item
->HasText() )
1265 if (m_gi
->m_rectAll
.width
> spacing
)
1266 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1268 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1269 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1270 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1271 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1273 else // no text, highlight the icon
1275 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1276 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1281 m_gi
->m_rectAll
.x
= x
;
1282 m_gi
->m_rectAll
.y
= y
;
1284 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1285 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1286 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1288 if (item
->HasImage())
1290 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1291 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1292 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1296 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1301 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1305 wxFAIL_MSG( _T("unknown mode") );
1310 void wxListLineData::InitItems( int num
)
1312 for (int i
= 0; i
< num
; i
++)
1313 m_items
.Append( new wxListItemData(m_owner
) );
1316 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1318 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1319 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1321 wxListItemData
*item
= node
->GetData();
1322 item
->SetItem( info
);
1325 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1327 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1330 wxListItemData
*item
= node
->GetData();
1331 item
->GetItem( info
);
1335 wxString
wxListLineData::GetText(int index
) const
1339 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1342 wxListItemData
*item
= node
->GetData();
1343 s
= item
->GetText();
1349 void wxListLineData::SetText( int index
, const wxString
& s
)
1351 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1354 wxListItemData
*item
= node
->GetData();
1359 void wxListLineData::SetImage( int index
, int image
)
1361 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1362 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1364 wxListItemData
*item
= node
->GetData();
1365 item
->SetImage(image
);
1368 int wxListLineData::GetImage( int index
) const
1370 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1371 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1373 wxListItemData
*item
= node
->GetData();
1374 return item
->GetImage();
1377 wxListItemAttr
*wxListLineData::GetAttr() const
1379 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1380 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1382 wxListItemData
*item
= node
->GetData();
1383 return item
->GetAttr();
1386 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1388 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1389 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1391 wxListItemData
*item
= node
->GetData();
1392 item
->SetAttr(attr
);
1395 bool wxListLineData::SetAttributes(wxDC
*dc
,
1396 const wxListItemAttr
*attr
,
1399 wxWindow
*listctrl
= m_owner
->GetParent();
1403 // don't use foreground colour for drawing highlighted items - this might
1404 // make them completely invisible (and there is no way to do bit
1405 // arithmetics on wxColour, unfortunately)
1410 if (m_owner
->HasFocus()
1411 #if !defined(__WXUNIVERSAL__)
1412 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1420 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1422 else if ( attr
&& attr
->HasTextColour() )
1423 colText
= attr
->GetTextColour();
1425 colText
= listctrl
->GetForegroundColour();
1427 dc
->SetTextForeground(colText
);
1431 if ( attr
&& attr
->HasFont() )
1432 font
= attr
->GetFont();
1434 font
= listctrl
->GetFont();
1439 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1440 if ( highlighted
|| hasBgCol
)
1443 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1445 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1447 dc
->SetPen( *wxTRANSPARENT_PEN
);
1455 void wxListLineData::Draw( wxDC
*dc
)
1457 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1458 wxCHECK_RET( node
, _T("no subitems at all??") );
1460 bool highlighted
= IsHighlighted();
1462 wxListItemAttr
*attr
= GetAttr();
1464 if ( SetAttributes(dc
, attr
, highlighted
) )
1465 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1467 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1473 int flags
= wxCONTROL_SELECTED
;
1474 if (m_owner
->HasFocus()
1475 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__)
1476 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1479 flags
|= wxCONTROL_FOCUSED
;
1480 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1485 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1490 // just for debugging to better see where the items are
1492 dc
->SetPen(*wxRED_PEN
);
1493 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1494 dc
->DrawRectangle( m_gi
->m_rectAll
);
1495 dc
->SetPen(*wxGREEN_PEN
);
1496 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1499 wxListItemData
*item
= node
->GetData();
1500 if (item
->HasImage())
1502 // centre the image inside our rectangle, this looks nicer when items
1503 // ae aligned in a row
1504 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1506 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1509 if (item
->HasText())
1511 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1513 wxDCClipper
clipper(*dc
, rectLabel
);
1514 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1518 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1520 const wxRect
& rectHL
,
1523 // TODO: later we should support setting different attributes for
1524 // different columns - to do it, just add "col" argument to
1525 // GetAttr() and move these lines into the loop below
1526 wxListItemAttr
*attr
= GetAttr();
1527 if ( SetAttributes(dc
, attr
, highlighted
) )
1528 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1530 dc
->DrawRectangle( rectHL
);
1536 int flags
= wxCONTROL_SELECTED
;
1537 if (m_owner
->HasFocus())
1538 flags
|= wxCONTROL_FOCUSED
;
1539 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1543 dc
->DrawRectangle( rectHL
);
1548 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1549 yMid
= rect
.y
+ rect
.height
/2;
1551 // This probably needs to be done
1552 // on all platforms as the icons
1553 // otherwise nearly touch the border
1558 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1560 node
= node
->GetNext(), col
++ )
1562 wxListItemData
*item
= node
->GetData();
1564 int width
= m_owner
->GetColumnWidth(col
);
1568 const int wText
= width
- 8;
1569 wxDCClipper
clipper(*dc
, xOld
, rect
.y
, wText
, rect
.height
);
1571 if ( item
->HasImage() )
1574 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1575 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1577 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1583 if ( item
->HasText() )
1584 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, wText
);
1588 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1589 const wxString
& textOrig
,
1595 // we don't support displaying multiple lines currently (and neither does
1596 // wxMSW FWIW) so just merge all the lines
1597 wxString
text(textOrig
);
1598 text
.Replace(_T("\n"), _T(" "));
1601 dc
->GetTextExtent(text
, &w
, &h
);
1603 const wxCoord y
= yMid
- (h
+ 1)/2;
1605 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1607 // determine if the string can fit inside the current width
1610 // it can, draw it using the items alignment
1612 m_owner
->GetColumn(col
, item
);
1613 switch ( item
.GetAlign() )
1615 case wxLIST_FORMAT_LEFT
:
1619 case wxLIST_FORMAT_RIGHT
:
1623 case wxLIST_FORMAT_CENTER
:
1624 x
+= (width
- w
) / 2;
1628 wxFAIL_MSG( _T("unknown list item format") );
1632 dc
->DrawText(text
, x
, y
);
1634 else // otherwise, truncate and add an ellipsis if possible
1636 // determine the base width
1637 wxString
ellipsis(wxT("..."));
1639 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1641 // continue until we have enough space or only one character left
1643 size_t len
= text
.length();
1644 wxString drawntext
= text
.Left(len
);
1647 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1648 drawntext
.RemoveLast();
1651 if (w
+ base_w
<= width
)
1655 // if still not enough space, remove ellipsis characters
1656 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1658 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1659 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1662 // now draw the text
1663 dc
->DrawText(drawntext
, x
, y
);
1664 dc
->DrawText(ellipsis
, x
+ w
, y
);
1668 bool wxListLineData::Highlight( bool on
)
1670 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1672 if ( on
== m_highlighted
)
1680 void wxListLineData::ReverseHighlight( void )
1682 Highlight(!IsHighlighted());
1685 //-----------------------------------------------------------------------------
1686 // wxListHeaderWindow
1687 //-----------------------------------------------------------------------------
1689 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1691 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1692 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1693 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1694 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1697 void wxListHeaderWindow::Init()
1699 m_currentCursor
= (wxCursor
*) NULL
;
1700 m_isDragging
= false;
1704 wxListHeaderWindow::wxListHeaderWindow()
1708 m_owner
= (wxListMainWindow
*) NULL
;
1709 m_resizeCursor
= (wxCursor
*) NULL
;
1712 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1714 wxListMainWindow
*owner
,
1718 const wxString
&name
)
1719 : wxWindow( win
, id
, pos
, size
, style
, name
)
1724 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1727 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1728 SetOwnForegroundColour( attr
.colFg
);
1729 SetOwnBackgroundColour( attr
.colBg
);
1731 SetOwnFont( attr
.font
);
1733 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1734 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1736 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1740 wxListHeaderWindow::~wxListHeaderWindow()
1742 delete m_resizeCursor
;
1745 #ifdef __WXUNIVERSAL__
1746 #include "wx/univ/renderer.h"
1747 #include "wx/univ/theme.h"
1750 // shift the DC origin to match the position of the main window horz
1751 // scrollbar: this allows us to always use logical coords
1752 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1755 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1758 m_owner
->GetViewStart( &view_start
, NULL
);
1763 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1765 // account for the horz scrollbar offset
1767 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1769 // Maybe we just have to check for m_signX
1770 // in the DC, but I leave the #ifdef __WXGTK__
1772 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1776 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1779 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1781 wxPaintDC
dc( this );
1786 dc
.SetFont( GetFont() );
1788 // width and height of the entire header window
1790 GetClientSize( &w
, &h
);
1791 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1793 dc
.SetBackgroundMode(wxTRANSPARENT
);
1794 dc
.SetTextForeground(GetForegroundColour());
1796 int x
= HEADER_OFFSET_X
;
1797 int numColumns
= m_owner
->GetColumnCount();
1799 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1801 m_owner
->GetColumn( i
, item
);
1802 int wCol
= item
.m_width
;
1808 if (!m_parent
->IsEnabled())
1809 flags
|= wxCONTROL_DISABLED
;
1811 // NB: The code below is not really Mac-specific, but since we are close
1812 // to 2.8 release and I don't have time to test on other platforms, I
1813 // defined this only for wxMac. If this behavior is desired on
1814 // other platforms, please go ahead and revise or remove the #ifdef.
1816 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1817 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1818 flags
|= wxCONTROL_SELECTED
;
1821 wxRendererNative::Get().DrawHeaderButton
1825 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1829 // see if we have enough space for the column label
1831 // for this we need the width of the text
1834 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1835 wLabel
+= 2 * EXTRA_WIDTH
;
1837 // and the width of the icon, if any
1838 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1839 const int image
= item
.m_image
;
1840 wxImageList
*imageList
;
1843 imageList
= m_owner
->m_small_image_list
;
1846 imageList
->GetSize(image
, ix
, iy
);
1847 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1855 // ignore alignment if there is not enough space anyhow
1857 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1860 wxFAIL_MSG( _T("unknown list item format") );
1863 case wxLIST_FORMAT_LEFT
:
1867 case wxLIST_FORMAT_RIGHT
:
1868 xAligned
= x
+ cw
- wLabel
;
1871 case wxLIST_FORMAT_CENTER
:
1872 xAligned
= x
+ (cw
- wLabel
) / 2;
1876 // draw the text and image clipping them so that they
1877 // don't overwrite the column boundary
1878 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1880 // if we have an image, draw it on the right of the label
1887 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1888 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1889 wxIMAGELIST_DRAW_TRANSPARENT
1893 dc
.DrawText( item
.GetText(),
1894 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1900 void wxListHeaderWindow::DrawCurrent()
1903 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1905 int x1
= m_currentX
;
1907 m_owner
->ClientToScreen( &x1
, &y1
);
1909 int x2
= m_currentX
;
1911 m_owner
->GetClientSize( NULL
, &y2
);
1912 m_owner
->ClientToScreen( &x2
, &y2
);
1915 dc
.SetLogicalFunction( wxINVERT
);
1916 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1917 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1921 dc
.DrawLine( x1
, y1
, x2
, y2
);
1923 dc
.SetLogicalFunction( wxCOPY
);
1925 dc
.SetPen( wxNullPen
);
1926 dc
.SetBrush( wxNullBrush
);
1930 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1932 // we want to work with logical coords
1934 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1935 int y
= event
.GetY();
1939 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1941 // we don't draw the line beyond our window, but we allow dragging it
1944 GetClientSize( &w
, NULL
);
1945 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1948 // erase the line if it was drawn
1949 if ( m_currentX
< w
)
1952 if (event
.ButtonUp())
1955 m_isDragging
= false;
1957 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1958 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1965 m_currentX
= m_minX
+ 7;
1967 // draw in the new location
1968 if ( m_currentX
< w
)
1972 else // not dragging
1975 bool hit_border
= false;
1977 // end of the current column
1980 // find the column where this event occurred
1982 countCol
= m_owner
->GetColumnCount();
1983 for (col
= 0; col
< countCol
; col
++)
1985 xpos
+= m_owner
->GetColumnWidth( col
);
1988 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1990 // near the column border
1997 // inside the column
2004 if ( col
== countCol
)
2007 if (event
.LeftDown() || event
.RightUp())
2009 if (hit_border
&& event
.LeftDown())
2011 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2012 event
.GetPosition()) )
2014 m_isDragging
= true;
2019 //else: column resizing was vetoed by the user code
2021 else // click on a column
2023 // record the selected state of the columns
2024 if (event
.LeftDown())
2026 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2029 m_owner
->GetColumn(i
, colItem
);
2030 long state
= colItem
.GetState();
2032 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2034 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2035 m_owner
->SetColumn(i
, colItem
);
2039 SendListEvent( event
.LeftDown()
2040 ? wxEVT_COMMAND_LIST_COL_CLICK
2041 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2042 event
.GetPosition());
2045 else if (event
.Moving())
2050 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2051 m_currentCursor
= m_resizeCursor
;
2055 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2056 m_currentCursor
= wxSTANDARD_CURSOR
;
2060 SetCursor(*m_currentCursor
);
2065 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2067 m_owner
->SetFocus();
2071 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2073 wxWindow
*parent
= GetParent();
2074 wxListEvent
le( type
, parent
->GetId() );
2075 le
.SetEventObject( parent
);
2076 le
.m_pointDrag
= pos
;
2078 // the position should be relative to the parent window, not
2079 // this one for compatibility with MSW and common sense: the
2080 // user code doesn't know anything at all about this header
2081 // window, so why should it get positions relative to it?
2082 le
.m_pointDrag
.y
-= GetSize().y
;
2084 le
.m_col
= m_column
;
2085 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2088 //-----------------------------------------------------------------------------
2089 // wxListRenameTimer (internal)
2090 //-----------------------------------------------------------------------------
2092 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2097 void wxListRenameTimer::Notify()
2099 m_owner
->OnRenameTimer();
2102 //-----------------------------------------------------------------------------
2103 // wxListTextCtrlWrapper (internal)
2104 //-----------------------------------------------------------------------------
2106 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2107 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2108 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2109 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2112 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2115 : m_startValue(owner
->GetItemText(itemEdit
)),
2116 m_itemEdited(itemEdit
)
2120 m_aboutToFinish
= false;
2122 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2124 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2125 &rectLabel
.x
, &rectLabel
.y
);
2127 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2128 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2129 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2132 m_text
->PushEventHandler(this);
2135 void wxListTextCtrlWrapper::EndEdit(bool discardChanges
)
2137 m_aboutToFinish
= true;
2139 if ( discardChanges
)
2141 m_owner
->OnRenameCancelled(m_itemEdited
);
2147 // Notify the owner about the changes
2150 // Even if vetoed, close the control (consistent with MSW)
2155 void wxListTextCtrlWrapper::Finish( bool setfocus
)
2157 m_text
->RemoveEventHandler(this);
2158 m_owner
->ResetTextControl( m_text
);
2160 wxPendingDelete
.Append( this );
2163 m_owner
->SetFocusIgnoringChildren();
2166 bool wxListTextCtrlWrapper::AcceptChanges()
2168 const wxString value
= m_text
->GetValue();
2170 // notice that we should always call OnRenameAccept() to generate the "end
2171 // label editing" event, even if the user hasn't really changed anything
2172 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2174 // vetoed by the user
2178 // accepted, do rename the item (unless nothing changed)
2179 if ( value
!= m_startValue
)
2180 m_owner
->SetItemText(m_itemEdited
, value
);
2185 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2187 switch ( event
.m_keyCode
)
2202 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2204 if (m_aboutToFinish
)
2206 // auto-grow the textctrl:
2207 wxSize parentSize
= m_owner
->GetSize();
2208 wxPoint myPos
= m_text
->GetPosition();
2209 wxSize mySize
= m_text
->GetSize();
2211 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2212 if (myPos
.x
+ sx
> parentSize
.x
)
2213 sx
= parentSize
.x
- myPos
.x
;
2216 m_text
->SetSize(sx
, wxDefaultCoord
);
2222 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2224 if ( !m_aboutToFinish
)
2226 if ( !AcceptChanges() )
2227 m_owner
->OnRenameCancelled( m_itemEdited
);
2232 // We must let the native text control handle focus
2236 //-----------------------------------------------------------------------------
2238 //-----------------------------------------------------------------------------
2240 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2242 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2243 EVT_PAINT (wxListMainWindow::OnPaint
)
2244 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2245 EVT_CHAR (wxListMainWindow::OnChar
)
2246 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2247 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
2248 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2249 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2250 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2253 void wxListMainWindow::Init()
2258 m_lineTo
= (size_t)-1;
2264 m_small_image_list
= (wxImageList
*) NULL
;
2265 m_normal_image_list
= (wxImageList
*) NULL
;
2267 m_small_spacing
= 30;
2268 m_normal_spacing
= 40;
2272 m_isCreated
= false;
2274 m_lastOnSame
= false;
2275 m_renameTimer
= new wxListRenameTimer( this );
2276 m_textctrlWrapper
= NULL
;
2280 m_lineSelectSingleOnUp
=
2281 m_lineBeforeLastClicked
= (size_t)-1;
2284 wxListMainWindow::wxListMainWindow()
2289 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2292 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2297 const wxString
&name
)
2298 : wxScrolledWindow( parent
, id
, pos
, size
,
2299 style
| wxHSCROLL
| wxVSCROLL
, name
)
2303 m_highlightBrush
= new wxBrush
2305 wxSystemSettings::GetColour
2307 wxSYS_COLOUR_HIGHLIGHT
2312 m_highlightUnfocusedBrush
= new wxBrush
2314 wxSystemSettings::GetColour
2316 wxSYS_COLOUR_BTNSHADOW
2321 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2323 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2324 SetOwnForegroundColour( attr
.colFg
);
2325 SetOwnBackgroundColour( attr
.colBg
);
2327 SetOwnFont( attr
.font
);
2330 wxListMainWindow::~wxListMainWindow()
2333 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2334 WX_CLEAR_ARRAY(m_aColWidths
);
2336 delete m_highlightBrush
;
2337 delete m_highlightUnfocusedBrush
;
2338 delete m_renameTimer
;
2341 void wxListMainWindow::CacheLineData(size_t line
)
2343 wxGenericListCtrl
*listctrl
= GetListCtrl();
2345 wxListLineData
*ld
= GetDummyLine();
2347 size_t countCol
= GetColumnCount();
2348 for ( size_t col
= 0; col
< countCol
; col
++ )
2350 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2351 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2354 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2357 wxListLineData
*wxListMainWindow::GetDummyLine() const
2359 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2360 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2362 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2364 // we need to recreate the dummy line if the number of columns in the
2365 // control changed as it would have the incorrect number of fields
2367 if ( !m_lines
.IsEmpty() &&
2368 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2370 self
->m_lines
.Clear();
2373 if ( m_lines
.IsEmpty() )
2375 wxListLineData
*line
= new wxListLineData(self
);
2376 self
->m_lines
.Add(line
);
2378 // don't waste extra memory -- there never going to be anything
2379 // else/more in this array
2380 self
->m_lines
.Shrink();
2386 // ----------------------------------------------------------------------------
2387 // line geometry (report mode only)
2388 // ----------------------------------------------------------------------------
2390 wxCoord
wxListMainWindow::GetLineHeight() const
2392 // we cache the line height as calling GetTextExtent() is slow
2393 if ( !m_lineHeight
)
2395 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2397 wxClientDC
dc( self
);
2398 dc
.SetFont( GetFont() );
2401 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2403 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2406 m_small_image_list
->GetSize(0, iw
, ih
);
2411 self
->m_lineHeight
= y
+ LINE_SPACING
;
2414 return m_lineHeight
;
2417 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2419 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2421 return LINE_SPACING
+ line
* GetLineHeight();
2424 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2426 if ( !InReportView() )
2427 return GetLine(line
)->m_gi
->m_rectAll
;
2430 rect
.x
= HEADER_OFFSET_X
;
2431 rect
.y
= GetLineY(line
);
2432 rect
.width
= GetHeaderWidth();
2433 rect
.height
= GetLineHeight();
2438 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2440 if ( !InReportView() )
2441 return GetLine(line
)->m_gi
->m_rectLabel
;
2444 wxListLineData
*data
= GetLine(line
);
2445 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
2448 wxListItemData
*item
= node
->GetData();
2449 if ( item
->HasImage() )
2452 GetImageSize( item
->GetImage(), ix
, iy
);
2453 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
2458 rect
.x
= image_x
+ HEADER_OFFSET_X
;
2459 rect
.y
= GetLineY(line
);
2460 rect
.width
= GetColumnWidth(0) - image_x
;
2461 rect
.height
= GetLineHeight();
2466 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2468 if ( !InReportView() )
2469 return GetLine(line
)->m_gi
->m_rectIcon
;
2471 wxListLineData
*ld
= GetLine(line
);
2472 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2475 rect
.x
= HEADER_OFFSET_X
;
2476 rect
.y
= GetLineY(line
);
2477 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2482 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2484 return InReportView() ? GetLineRect(line
)
2485 : GetLine(line
)->m_gi
->m_rectHighlight
;
2488 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2490 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2492 wxListLineData
*ld
= GetLine(line
);
2494 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2495 return wxLIST_HITTEST_ONITEMICON
;
2497 // VS: Testing for "ld->HasText() || InReportView()" instead of
2498 // "ld->HasText()" is needed to make empty lines in report view
2500 if ( ld
->HasText() || InReportView() )
2502 wxRect rect
= InReportView() ? GetLineRect(line
)
2503 : GetLineLabelRect(line
);
2505 if ( rect
.Contains(x
, y
) )
2506 return wxLIST_HITTEST_ONITEMLABEL
;
2512 // ----------------------------------------------------------------------------
2513 // highlight (selection) handling
2514 // ----------------------------------------------------------------------------
2516 bool wxListMainWindow::IsHighlighted(size_t line
) const
2520 return m_selStore
.IsSelected(line
);
2524 wxListLineData
*ld
= GetLine(line
);
2525 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2527 return ld
->IsHighlighted();
2531 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2537 wxArrayInt linesChanged
;
2538 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2541 // meny items changed state, refresh everything
2542 RefreshLines(lineFrom
, lineTo
);
2544 else // only a few items changed state, refresh only them
2546 size_t count
= linesChanged
.GetCount();
2547 for ( size_t n
= 0; n
< count
; n
++ )
2549 RefreshLine(linesChanged
[n
]);
2553 else // iterate over all items in non report view
2555 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2557 if ( HighlightLine(line
, highlight
) )
2563 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2569 changed
= m_selStore
.SelectItem(line
, highlight
);
2573 wxListLineData
*ld
= GetLine(line
);
2574 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2576 changed
= ld
->Highlight(highlight
);
2581 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2582 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2588 void wxListMainWindow::RefreshLine( size_t line
)
2590 if ( InReportView() )
2592 size_t visibleFrom
, visibleTo
;
2593 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2595 if ( line
< visibleFrom
|| line
> visibleTo
)
2599 wxRect rect
= GetLineRect(line
);
2601 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2602 RefreshRect( rect
);
2605 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2607 // we suppose that they are ordered by caller
2608 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2610 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2612 if ( InReportView() )
2614 size_t visibleFrom
, visibleTo
;
2615 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2617 if ( lineFrom
< visibleFrom
)
2618 lineFrom
= visibleFrom
;
2619 if ( lineTo
> visibleTo
)
2624 rect
.y
= GetLineY(lineFrom
);
2625 rect
.width
= GetClientSize().x
;
2626 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2628 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2629 RefreshRect( rect
);
2633 // TODO: this should be optimized...
2634 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2641 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2643 if ( InReportView() )
2645 size_t visibleFrom
, visibleTo
;
2646 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2648 if ( lineFrom
< visibleFrom
)
2649 lineFrom
= visibleFrom
;
2650 else if ( lineFrom
> visibleTo
)
2655 rect
.y
= GetLineY(lineFrom
);
2656 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2658 wxSize size
= GetClientSize();
2659 rect
.width
= size
.x
;
2661 // refresh till the bottom of the window
2662 rect
.height
= size
.y
- rect
.y
;
2664 RefreshRect( rect
);
2668 // TODO: how to do it more efficiently?
2673 void wxListMainWindow::RefreshSelected()
2679 if ( InReportView() )
2681 GetVisibleLinesRange(&from
, &to
);
2686 to
= GetItemCount() - 1;
2689 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2690 RefreshLine(m_current
);
2692 for ( size_t line
= from
; line
<= to
; line
++ )
2694 // NB: the test works as expected even if m_current == -1
2695 if ( line
!= m_current
&& IsHighlighted(line
) )
2700 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2702 // Note: a wxPaintDC must be constructed even if no drawing is
2703 // done (a Windows requirement).
2704 wxPaintDC
dc( this );
2708 // nothing to draw or not the moment to draw it
2714 // delay the repainting until we calculate all the items positions
2721 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2723 dc
.SetFont( GetFont() );
2725 if ( InReportView() )
2727 int lineHeight
= GetLineHeight();
2729 size_t visibleFrom
, visibleTo
;
2730 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2733 int xOrig
= dc
.LogicalToDeviceX( 0 );
2734 int yOrig
= dc
.LogicalToDeviceY( 0 );
2736 // tell the caller cache to cache the data
2739 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2740 GetParent()->GetId());
2741 evCache
.SetEventObject( GetParent() );
2742 evCache
.m_oldItemIndex
= visibleFrom
;
2743 evCache
.m_itemIndex
= visibleTo
;
2744 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2747 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2749 rectLine
= GetLineRect(line
);
2752 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2753 rectLine
.width
, rectLine
.height
) )
2755 // don't redraw unaffected lines to avoid flicker
2759 GetLine(line
)->DrawInReportMode( &dc
,
2761 GetLineHighlightRect(line
),
2762 IsHighlighted(line
) );
2765 if ( HasFlag(wxLC_HRULES
) )
2767 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2768 wxSize clientSize
= GetClientSize();
2770 size_t i
= visibleFrom
;
2771 if (i
== 0) i
= 1; // Don't draw the first one
2772 for ( ; i
<= visibleTo
; i
++ )
2775 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2776 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2777 clientSize
.x
- dev_x
, i
* lineHeight
);
2780 // Draw last horizontal rule
2781 if ( visibleTo
== GetItemCount() - 1 )
2784 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2785 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2786 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2790 // Draw vertical rules if required
2791 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2793 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2794 wxRect firstItemRect
, lastItemRect
;
2796 GetItemRect(visibleFrom
, firstItemRect
);
2797 GetItemRect(visibleTo
, lastItemRect
);
2798 int x
= firstItemRect
.GetX();
2800 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2802 for (int col
= 0; col
< GetColumnCount(); col
++)
2804 int colWidth
= GetColumnWidth(col
);
2806 int x_pos
= x
- dev_x
;
2807 if (col
< GetColumnCount()-1) x_pos
-= 2;
2808 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2809 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2815 size_t count
= GetItemCount();
2816 for ( size_t i
= 0; i
< count
; i
++ )
2818 GetLine(i
)->Draw( &dc
);
2823 // Don't draw rect outline under Mac at all.
2828 wxRect
rect( GetLineHighlightRect( m_current
) );
2830 dc
.SetPen( *wxBLACK_PEN
);
2831 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2832 dc
.DrawRectangle( rect
);
2834 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2842 void wxListMainWindow::HighlightAll( bool on
)
2844 if ( IsSingleSel() )
2846 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2848 // we just have one item to turn off
2849 if ( HasCurrent() && IsHighlighted(m_current
) )
2851 HighlightLine(m_current
, false);
2852 RefreshLine(m_current
);
2855 else // multi selection
2858 HighlightLines(0, GetItemCount() - 1, on
);
2862 void wxListMainWindow::SendNotify( size_t line
,
2863 wxEventType command
,
2864 const wxPoint
& point
)
2866 wxListEvent
le( command
, GetParent()->GetId() );
2867 le
.SetEventObject( GetParent() );
2869 le
.m_itemIndex
= line
;
2871 // set only for events which have position
2872 if ( point
!= wxDefaultPosition
)
2873 le
.m_pointDrag
= point
;
2875 // don't try to get the line info for virtual list controls: the main
2876 // program has it anyhow and if we did it would result in accessing all
2877 // the lines, even those which are not visible now and this is precisely
2878 // what we're trying to avoid
2881 if ( line
!= (size_t)-1 )
2883 GetLine(line
)->GetItem( 0, le
.m_item
);
2885 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2887 //else: there may be no more such item
2889 GetParent()->GetEventHandler()->ProcessEvent( le
);
2892 void wxListMainWindow::ChangeCurrent(size_t current
)
2894 m_current
= current
;
2896 // as the current item changed, we shouldn't start editing it when the
2897 // "slow click" timer expires as the click happened on another item
2898 if ( m_renameTimer
->IsRunning() )
2899 m_renameTimer
->Stop();
2901 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2904 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2906 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2907 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2909 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2910 wxT("EditLabel() needs a text control") );
2912 size_t itemEdit
= (size_t)item
;
2914 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2915 le
.SetEventObject( GetParent() );
2916 le
.m_itemIndex
= item
;
2917 wxListLineData
*data
= GetLine(itemEdit
);
2918 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2919 data
->GetItem( 0, le
.m_item
);
2921 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2923 // vetoed by user code
2927 // We have to call this here because the label in question might just have
2928 // been added and no screen update taken place.
2933 // Pending events dispatched by wxSafeYield might have changed the item
2935 if ( (size_t)item
>= GetItemCount() )
2939 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2940 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2941 return m_textctrlWrapper
->GetText();
2944 void wxListMainWindow::OnRenameTimer()
2946 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2948 EditLabel( m_current
);
2951 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2953 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2954 le
.SetEventObject( GetParent() );
2955 le
.m_itemIndex
= itemEdit
;
2957 wxListLineData
*data
= GetLine(itemEdit
);
2959 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2961 data
->GetItem( 0, le
.m_item
);
2962 le
.m_item
.m_text
= value
;
2963 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2967 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2969 // let owner know that the edit was cancelled
2970 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2972 le
.SetEditCanceled(true);
2974 le
.SetEventObject( GetParent() );
2975 le
.m_itemIndex
= itemEdit
;
2977 wxListLineData
*data
= GetLine(itemEdit
);
2978 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2980 data
->GetItem( 0, le
.m_item
);
2981 GetEventHandler()->ProcessEvent( le
);
2984 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2988 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2989 // shutdown the edit control when the mouse is clicked elsewhere on the
2990 // listctrl because the order of events is different (or something like
2991 // that), so explicitly end the edit if it is active.
2992 if ( event
.LeftDown() && m_textctrlWrapper
)
2993 m_textctrlWrapper
->EndEdit( false );
2996 if ( event
.LeftDown() )
2997 SetFocusIgnoringChildren();
2999 event
.SetEventObject( GetParent() );
3000 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3003 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3005 // let the base handle mouse wheel events.
3010 if ( !HasCurrent() || IsEmpty() )
3012 if (event
.RightDown())
3014 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3016 wxContextMenuEvent
evtCtx(
3018 GetParent()->GetId(),
3019 ClientToScreen(event
.GetPosition()));
3020 evtCtx
.SetEventObject(GetParent());
3021 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3029 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3030 event
.ButtonDClick()) )
3033 int x
= event
.GetX();
3034 int y
= event
.GetY();
3035 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3037 // where did we hit it (if we did)?
3040 size_t count
= GetItemCount(),
3043 if ( InReportView() )
3045 current
= y
/ GetLineHeight();
3046 if ( current
< count
)
3047 hitResult
= HitTestLine(current
, x
, y
);
3051 // TODO: optimize it too! this is less simple than for report view but
3052 // enumerating all items is still not a way to do it!!
3053 for ( current
= 0; current
< count
; current
++ )
3055 hitResult
= HitTestLine(current
, x
, y
);
3061 if (event
.Dragging())
3063 if (m_dragCount
== 0)
3065 // we have to report the raw, physical coords as we want to be
3066 // able to call HitTest(event.m_pointDrag) from the user code to
3067 // get the item being dragged
3068 m_dragStart
= event
.GetPosition();
3073 if (m_dragCount
!= 3)
3076 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3077 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3079 wxListEvent
le( command
, GetParent()->GetId() );
3080 le
.SetEventObject( GetParent() );
3081 le
.m_itemIndex
= m_lineLastClicked
;
3082 le
.m_pointDrag
= m_dragStart
;
3083 GetParent()->GetEventHandler()->ProcessEvent( le
);
3094 // outside of any item
3095 if (event
.RightDown())
3097 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3099 wxContextMenuEvent
evtCtx(
3101 GetParent()->GetId(),
3102 ClientToScreen(event
.GetPosition()));
3103 evtCtx
.SetEventObject(GetParent());
3104 GetParent()->GetEventHandler()->ProcessEvent(evtCtx
);
3108 // reset the selection and bail out
3109 HighlightAll(false);
3115 bool forceClick
= false;
3116 if (event
.ButtonDClick())
3118 if ( m_renameTimer
->IsRunning() )
3119 m_renameTimer
->Stop();
3121 m_lastOnSame
= false;
3123 if ( current
== m_lineLastClicked
)
3125 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3131 // The first click was on another item, so don't interpret this as
3132 // a double click, but as a simple click instead
3139 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3141 // select single line
3142 HighlightAll( false );
3143 ReverseHighlight(m_lineSelectSingleOnUp
);
3148 if ((current
== m_current
) &&
3149 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3150 HasFlag(wxLC_EDIT_LABELS
) )
3152 if ( !InReportView() ||
3153 GetLineLabelRect(current
).Contains(x
, y
) )
3155 int dclick
= wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC
);
3156 m_renameTimer
->Start(dclick
> 0 ? dclick
: 250, true);
3161 m_lastOnSame
= false;
3162 m_lineSelectSingleOnUp
= (size_t)-1;
3166 // This is necessary, because after a DnD operation in
3167 // from and to ourself, the up event is swallowed by the
3168 // DnD code. So on next non-up event (which means here and
3169 // now) m_lineSelectSingleOnUp should be reset.
3170 m_lineSelectSingleOnUp
= (size_t)-1;
3172 if (event
.RightDown())
3174 m_lineBeforeLastClicked
= m_lineLastClicked
;
3175 m_lineLastClicked
= current
;
3177 // If the item is already selected, do not update the selection.
3178 // Multi-selections should not be cleared if a selected item is clicked.
3179 if (!IsHighlighted(current
))
3181 HighlightAll(false);
3182 ChangeCurrent(current
);
3183 ReverseHighlight(m_current
);
3186 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3188 // Allow generation of context menu event
3191 else if (event
.MiddleDown())
3193 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3195 else if ( event
.LeftDown() || forceClick
)
3197 m_lineBeforeLastClicked
= m_lineLastClicked
;
3198 m_lineLastClicked
= current
;
3200 size_t oldCurrent
= m_current
;
3201 bool oldWasSelected
= IsHighlighted(m_current
);
3203 bool cmdModifierDown
= event
.CmdDown();
3204 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3206 if ( IsSingleSel() || !IsHighlighted(current
) )
3208 HighlightAll( false );
3210 ChangeCurrent(current
);
3212 ReverseHighlight(m_current
);
3214 else // multi sel & current is highlighted & no mod keys
3216 m_lineSelectSingleOnUp
= current
;
3217 ChangeCurrent(current
); // change focus
3220 else // multi sel & either ctrl or shift is down
3222 if (cmdModifierDown
)
3224 ChangeCurrent(current
);
3226 ReverseHighlight(m_current
);
3228 else if (event
.ShiftDown())
3230 ChangeCurrent(current
);
3232 size_t lineFrom
= oldCurrent
,
3235 if ( lineTo
< lineFrom
)
3238 lineFrom
= m_current
;
3241 HighlightLines(lineFrom
, lineTo
);
3243 else // !ctrl, !shift
3245 // test in the enclosing if should make it impossible
3246 wxFAIL_MSG( _T("how did we get here?") );
3250 if (m_current
!= oldCurrent
)
3251 RefreshLine( oldCurrent
);
3253 // forceClick is only set if the previous click was on another item
3254 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3258 void wxListMainWindow::MoveToItem(size_t item
)
3260 if ( item
== (size_t)-1 )
3263 wxRect rect
= GetLineRect(item
);
3265 int client_w
, client_h
;
3266 GetClientSize( &client_w
, &client_h
);
3268 const int hLine
= GetLineHeight();
3270 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3271 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3273 if ( InReportView() )
3275 // the next we need the range of lines shown it might be different,
3276 // so recalculate it
3277 ResetVisibleLinesRange();
3279 if (rect
.y
< view_y
)
3280 Scroll( -1, rect
.y
/ hLine
);
3281 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3282 Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3285 // At least on Mac the visible lines value will get reset inside of
3286 // Scroll *before* it actually scrolls the window because of the
3287 // Update() that happens there, so it will still have the wrong value.
3288 // So let's reset it again and wait for it to be recalculated in the
3289 // next paint event. I would expect this problem to show up in wxGTK
3290 // too but couldn't duplicate it there. Perhaps the order of events
3291 // is different... --Robin
3292 ResetVisibleLinesRange();
3300 if (rect
.x
-view_x
< 5)
3301 sx
= (rect
.x
- 5) / SCROLL_UNIT_X
;
3302 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3303 sx
= (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
;
3305 if (rect
.y
-view_y
< 5)
3306 sy
= (rect
.y
- 5) / hLine
;
3307 if (rect
.y
+ rect
.height
- 5 > view_y
+ client_h
)
3308 sy
= (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
;
3314 bool wxListMainWindow::ScrollList(int WXUNUSED(dx
), int dy
)
3316 if ( !InReportView() )
3318 // TODO: this should work in all views but is not implemented now
3323 GetVisibleLinesRange(&top
, &bottom
);
3325 if ( bottom
== (size_t)-1 )
3328 ResetVisibleLinesRange();
3330 int hLine
= GetLineHeight();
3332 Scroll(-1, top
+ dy
/ hLine
);
3335 // see comment in MoveToItem() for why we do this
3336 ResetVisibleLinesRange();
3342 // ----------------------------------------------------------------------------
3343 // keyboard handling
3344 // ----------------------------------------------------------------------------
3346 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3348 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3349 _T("invalid item index in OnArrowChar()") );
3351 size_t oldCurrent
= m_current
;
3353 // in single selection we just ignore Shift as we can't select several
3355 if ( event
.ShiftDown() && !IsSingleSel() )
3357 ChangeCurrent(newCurrent
);
3359 // refresh the old focus to remove it
3360 RefreshLine( oldCurrent
);
3362 // select all the items between the old and the new one
3363 if ( oldCurrent
> newCurrent
)
3365 newCurrent
= oldCurrent
;
3366 oldCurrent
= m_current
;
3369 HighlightLines(oldCurrent
, newCurrent
);
3373 // all previously selected items are unselected unless ctrl is held
3374 // in a multiselection control
3375 if ( !event
.ControlDown() || IsSingleSel() )
3376 HighlightAll(false);
3378 ChangeCurrent(newCurrent
);
3380 // refresh the old focus to remove it
3381 RefreshLine( oldCurrent
);
3383 // in single selection mode we must always have a selected item
3384 if ( !event
.ControlDown() || IsSingleSel() )
3385 HighlightLine( m_current
, true );
3388 RefreshLine( m_current
);
3393 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3395 wxWindow
*parent
= GetParent();
3397 // propagate the key event upwards
3398 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3399 ke
.m_shiftDown
= event
.m_shiftDown
;
3400 ke
.m_controlDown
= event
.m_controlDown
;
3401 ke
.m_altDown
= event
.m_altDown
;
3402 ke
.m_metaDown
= event
.m_metaDown
;
3403 ke
.m_keyCode
= event
.m_keyCode
;
3406 ke
.SetEventObject( parent
);
3407 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3412 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3414 wxWindow
*parent
= GetParent();
3416 // propagate the key event upwards
3417 wxKeyEvent
ke( wxEVT_KEY_UP
);
3418 ke
.m_shiftDown
= event
.m_shiftDown
;
3419 ke
.m_controlDown
= event
.m_controlDown
;
3420 ke
.m_altDown
= event
.m_altDown
;
3421 ke
.m_metaDown
= event
.m_metaDown
;
3422 ke
.m_keyCode
= event
.m_keyCode
;
3425 ke
.SetEventObject( parent
);
3426 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3431 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3433 wxWindow
*parent
= GetParent();
3435 // send a list_key event up
3438 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3439 le
.m_itemIndex
= m_current
;
3440 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3441 le
.m_code
= event
.GetKeyCode();
3442 le
.SetEventObject( parent
);
3443 parent
->GetEventHandler()->ProcessEvent( le
);
3446 // propagate the char event upwards
3447 wxKeyEvent
ke( wxEVT_CHAR
);
3448 ke
.m_shiftDown
= event
.m_shiftDown
;
3449 ke
.m_controlDown
= event
.m_controlDown
;
3450 ke
.m_altDown
= event
.m_altDown
;
3451 ke
.m_metaDown
= event
.m_metaDown
;
3452 ke
.m_keyCode
= event
.m_keyCode
;
3455 ke
.SetEventObject( parent
);
3456 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3458 if ( HandleAsNavigationKey(event
) )
3461 // no item -> nothing to do
3468 // don't use m_linesPerPage directly as it might not be computed yet
3469 const int pageSize
= GetCountPerPage();
3470 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3472 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3474 if (event
.GetKeyCode() == WXK_RIGHT
)
3475 event
.m_keyCode
= WXK_LEFT
;
3476 else if (event
.GetKeyCode() == WXK_LEFT
)
3477 event
.m_keyCode
= WXK_RIGHT
;
3480 switch ( event
.GetKeyCode() )
3483 if ( m_current
> 0 )
3484 OnArrowChar( m_current
- 1, event
);
3488 if ( m_current
< (size_t)GetItemCount() - 1 )
3489 OnArrowChar( m_current
+ 1, event
);
3494 OnArrowChar( GetItemCount() - 1, event
);
3499 OnArrowChar( 0, event
);
3504 int steps
= InReportView() ? pageSize
- 1
3505 : m_current
% pageSize
;
3507 int index
= m_current
- steps
;
3511 OnArrowChar( index
, event
);
3517 int steps
= InReportView()
3519 : pageSize
- (m_current
% pageSize
) - 1;
3521 size_t index
= m_current
+ steps
;
3522 size_t count
= GetItemCount();
3523 if ( index
>= count
)
3526 OnArrowChar( index
, event
);
3531 if ( !InReportView() )
3533 int index
= m_current
- pageSize
;
3537 OnArrowChar( index
, event
);
3542 if ( !InReportView() )
3544 size_t index
= m_current
+ pageSize
;
3546 size_t count
= GetItemCount();
3547 if ( index
>= count
)
3550 OnArrowChar( index
, event
);
3555 if ( IsSingleSel() )
3557 if ( event
.ControlDown() )
3559 ReverseHighlight(m_current
);
3561 else // normal space press
3563 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3566 else // multiple selection
3568 ReverseHighlight(m_current
);
3574 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3582 // ----------------------------------------------------------------------------
3584 // ----------------------------------------------------------------------------
3586 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3590 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3591 event
.SetEventObject( GetParent() );
3592 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3596 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3597 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3598 // which are already drawn correctly resulting in horrible flicker - avoid
3608 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3612 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3613 event
.SetEventObject( GetParent() );
3614 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3622 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3624 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3626 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3628 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3630 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3632 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3634 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3636 else if ( InReportView() && (m_small_image_list
))
3638 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3642 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3644 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3646 m_normal_image_list
->GetSize( index
, width
, height
);
3648 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3650 m_small_image_list
->GetSize( index
, width
, height
);
3652 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3654 m_small_image_list
->GetSize( index
, width
, height
);
3656 else if ( InReportView() && m_small_image_list
)
3658 m_small_image_list
->GetSize( index
, width
, height
);
3667 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3669 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3670 dc
.SetFont( GetFont() );
3673 dc
.GetTextExtent( s
, &lw
, NULL
);
3675 return lw
+ AUTOSIZE_COL_MARGIN
;
3678 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3682 // calc the spacing from the icon size
3683 int width
= 0, height
= 0;
3685 if ((imageList
) && (imageList
->GetImageCount()) )
3686 imageList
->GetSize(0, width
, height
);
3688 if (which
== wxIMAGE_LIST_NORMAL
)
3690 m_normal_image_list
= imageList
;
3691 m_normal_spacing
= width
+ 8;
3694 if (which
== wxIMAGE_LIST_SMALL
)
3696 m_small_image_list
= imageList
;
3697 m_small_spacing
= width
+ 14;
3698 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3702 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3706 m_small_spacing
= spacing
;
3708 m_normal_spacing
= spacing
;
3711 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3713 return isSmall
? m_small_spacing
: m_normal_spacing
;
3716 // ----------------------------------------------------------------------------
3718 // ----------------------------------------------------------------------------
3720 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3722 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3724 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3726 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3727 item
.m_width
= GetTextLength( item
.m_text
);
3729 wxListHeaderData
*column
= node
->GetData();
3730 column
->SetItem( item
);
3732 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3734 headerWin
->m_dirty
= true;
3738 // invalidate it as it has to be recalculated
3742 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3744 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3745 _T("invalid column index") );
3747 wxCHECK_RET( InReportView(),
3748 _T("SetColumnWidth() can only be called in report mode.") );
3751 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3753 headerWin
->m_dirty
= true;
3755 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3756 wxCHECK_RET( node
, _T("no column?") );
3758 wxListHeaderData
*column
= node
->GetData();
3760 size_t count
= GetItemCount();
3762 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3764 width
= GetTextLength(column
->GetText());
3765 width
+= 2*EXTRA_WIDTH
;
3767 // check for column header's image availability
3768 const int image
= column
->GetImage();
3771 if ( m_small_image_list
)
3774 m_small_image_list
->GetSize(image
, ix
, iy
);
3775 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3779 else if ( width
== wxLIST_AUTOSIZE
)
3783 // TODO: determine the max width somehow...
3784 width
= WIDTH_COL_DEFAULT
;
3788 wxClientDC
dc(this);
3789 dc
.SetFont( GetFont() );
3791 int max
= AUTOSIZE_COL_MARGIN
;
3793 // if the cached column width isn't valid then recalculate it
3794 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3796 for (size_t i
= 0; i
< count
; i
++)
3798 wxListLineData
*line
= GetLine( i
);
3799 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3801 wxCHECK_RET( n
, _T("no subitem?") );
3803 wxListItemData
*itemData
= n
->GetData();
3806 itemData
->GetItem(item
);
3807 int itemWidth
= GetItemWidthWithImage(&item
);
3808 if (itemWidth
> max
)
3812 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3813 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3816 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3817 width
= max
+ AUTOSIZE_COL_MARGIN
;
3821 column
->SetWidth( width
);
3823 // invalidate it as it has to be recalculated
3827 int wxListMainWindow::GetHeaderWidth() const
3829 if ( !m_headerWidth
)
3831 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3833 size_t count
= GetColumnCount();
3834 for ( size_t col
= 0; col
< count
; col
++ )
3836 self
->m_headerWidth
+= GetColumnWidth(col
);
3840 return m_headerWidth
;
3843 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3845 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3846 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3848 wxListHeaderData
*column
= node
->GetData();
3849 column
->GetItem( item
);
3852 int wxListMainWindow::GetColumnWidth( int col
) const
3854 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3855 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3857 wxListHeaderData
*column
= node
->GetData();
3858 return column
->GetWidth();
3861 // ----------------------------------------------------------------------------
3863 // ----------------------------------------------------------------------------
3865 void wxListMainWindow::SetItem( wxListItem
&item
)
3867 long id
= item
.m_itemId
;
3868 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3869 _T("invalid item index in SetItem") );
3873 wxListLineData
*line
= GetLine((size_t)id
);
3874 line
->SetItem( item
.m_col
, item
);
3876 // Set item state if user wants
3877 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3878 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3882 // update the Max Width Cache if needed
3883 int width
= GetItemWidthWithImage(&item
);
3885 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3886 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3890 // update the item on screen
3892 GetItemRect(id
, rectItem
);
3893 RefreshRect(rectItem
);
3896 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3901 // first deal with selection
3902 if ( stateMask
& wxLIST_STATE_SELECTED
)
3904 // set/clear select state
3907 // optimized version for virtual listctrl.
3908 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3911 else if ( state
& wxLIST_STATE_SELECTED
)
3913 const long count
= GetItemCount();
3914 for( long i
= 0; i
< count
; i
++ )
3916 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3922 // clear for non virtual (somewhat optimized by using GetNextItem())
3924 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3926 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3931 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3933 // unfocus all: only one item can be focussed, so clearing focus for
3934 // all items is simply clearing focus of the focussed item.
3935 SetItemState(m_current
, state
, stateMask
);
3937 //(setting focus to all items makes no sense, so it is not handled here.)
3940 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3944 SetItemStateAll(state
, stateMask
);
3948 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3949 _T("invalid list ctrl item index in SetItem") );
3951 size_t oldCurrent
= m_current
;
3952 size_t item
= (size_t)litem
; // safe because of the check above
3954 // do we need to change the focus?
3955 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3957 if ( state
& wxLIST_STATE_FOCUSED
)
3959 // don't do anything if this item is already focused
3960 if ( item
!= m_current
)
3962 ChangeCurrent(item
);
3964 if ( oldCurrent
!= (size_t)-1 )
3966 if ( IsSingleSel() )
3968 HighlightLine(oldCurrent
, false);
3971 RefreshLine(oldCurrent
);
3974 RefreshLine( m_current
);
3979 // don't do anything if this item is not focused
3980 if ( item
== m_current
)
3984 if ( IsSingleSel() )
3986 // we must unselect the old current item as well or we
3987 // might end up with more than one selected item in a
3988 // single selection control
3989 HighlightLine(oldCurrent
, false);
3992 RefreshLine( oldCurrent
);
3997 // do we need to change the selection state?
3998 if ( stateMask
& wxLIST_STATE_SELECTED
)
4000 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
4002 if ( IsSingleSel() )
4006 // selecting the item also makes it the focused one in the
4008 if ( m_current
!= item
)
4010 ChangeCurrent(item
);
4012 if ( oldCurrent
!= (size_t)-1 )
4014 HighlightLine( oldCurrent
, false );
4015 RefreshLine( oldCurrent
);
4021 // only the current item may be selected anyhow
4022 if ( item
!= m_current
)
4027 if ( HighlightLine(item
, on
) )
4034 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
4036 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
4037 _T("invalid list ctrl item index in GetItemState()") );
4039 int ret
= wxLIST_STATE_DONTCARE
;
4041 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4043 if ( (size_t)item
== m_current
)
4044 ret
|= wxLIST_STATE_FOCUSED
;
4047 if ( stateMask
& wxLIST_STATE_SELECTED
)
4049 if ( IsHighlighted(item
) )
4050 ret
|= wxLIST_STATE_SELECTED
;
4056 void wxListMainWindow::GetItem( wxListItem
&item
) const
4058 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
4059 _T("invalid item index in GetItem") );
4061 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4062 line
->GetItem( item
.m_col
, item
);
4064 // Get item state if user wants it
4065 if ( item
.m_mask
& wxLIST_MASK_STATE
)
4066 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
4067 wxLIST_STATE_FOCUSED
);
4070 // ----------------------------------------------------------------------------
4072 // ----------------------------------------------------------------------------
4074 size_t wxListMainWindow::GetItemCount() const
4076 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4079 void wxListMainWindow::SetItemCount(long count
)
4081 m_selStore
.SetItemCount(count
);
4082 m_countVirt
= count
;
4084 ResetVisibleLinesRange();
4086 // scrollbars must be reset
4090 int wxListMainWindow::GetSelectedItemCount() const
4092 // deal with the quick case first
4093 if ( IsSingleSel() )
4094 return HasCurrent() ? IsHighlighted(m_current
) : false;
4096 // virtual controls remmebers all its selections itself
4098 return m_selStore
.GetSelectedCount();
4100 // TODO: we probably should maintain the number of items selected even for
4101 // non virtual controls as enumerating all lines is really slow...
4102 size_t countSel
= 0;
4103 size_t count
= GetItemCount();
4104 for ( size_t line
= 0; line
< count
; line
++ )
4106 if ( GetLine(line
)->IsHighlighted() )
4113 // ----------------------------------------------------------------------------
4114 // item position/size
4115 // ----------------------------------------------------------------------------
4117 wxRect
wxListMainWindow::GetViewRect() const
4119 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
4120 _T("wxListCtrl::GetViewRect() only works in icon mode") );
4122 // we need to find the longest/tallest label
4123 wxCoord xMax
= 0, yMax
= 0;
4124 const int count
= GetItemCount();
4127 for ( int i
= 0; i
< count
; i
++ )
4132 wxCoord x
= r
.GetRight(),
4142 // some fudge needed to make it look prettier
4143 xMax
+= 2 * EXTRA_BORDER_X
;
4144 yMax
+= 2 * EXTRA_BORDER_Y
;
4146 // account for the scrollbars if necessary
4147 const wxSize sizeAll
= GetClientSize();
4148 if ( xMax
> sizeAll
.x
)
4149 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4150 if ( yMax
> sizeAll
.y
)
4151 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4153 return wxRect(0, 0, xMax
, yMax
);
4156 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
4158 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4159 _T("invalid index in GetItemRect") );
4161 // ensure that we're laid out, otherwise we could return nonsense
4164 wxConstCast(this, wxListMainWindow
)->
4165 RecalculatePositions(true /* no refresh */);
4168 rect
= GetLineRect((size_t)index
);
4170 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4173 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4176 GetItemRect(item
, rect
);
4184 // ----------------------------------------------------------------------------
4185 // geometry calculation
4186 // ----------------------------------------------------------------------------
4188 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4190 const int lineHeight
= GetLineHeight();
4192 wxClientDC
dc( this );
4193 dc
.SetFont( GetFont() );
4195 const size_t count
= GetItemCount();
4198 if ( HasFlag(wxLC_ICON
) )
4199 iconSpacing
= m_normal_spacing
;
4200 else if ( HasFlag(wxLC_SMALL_ICON
) )
4201 iconSpacing
= m_small_spacing
;
4205 // Note that we do not call GetClientSize() here but
4206 // GetSize() and subtract the border size for sunken
4207 // borders manually. This is technically incorrect,
4208 // but we need to know the client area's size WITHOUT
4209 // scrollbars here. Since we don't know if there are
4210 // any scrollbars, we use GetSize() instead. Another
4211 // solution would be to call SetScrollbars() here to
4212 // remove the scrollbars and call GetClientSize() then,
4213 // but this might result in flicker and - worse - will
4214 // reset the scrollbars to 0 which is not good at all
4215 // if you resize a dialog/window, but don't want to
4216 // reset the window scrolling. RR.
4217 // Furthermore, we actually do NOT subtract the border
4218 // width as 2 pixels is just the extra space which we
4219 // need around the actual content in the window. Other-
4220 // wise the text would e.g. touch the upper border. RR.
4223 GetSize( &clientWidth
, &clientHeight
);
4225 if ( InReportView() )
4227 // all lines have the same height and we scroll one line per step
4228 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4230 m_linesPerPage
= clientHeight
/ lineHeight
;
4232 ResetVisibleLinesRange();
4234 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4235 GetHeaderWidth() / SCROLL_UNIT_X
,
4236 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4237 GetScrollPos(wxHORIZONTAL
),
4238 GetScrollPos(wxVERTICAL
),
4243 // we have 3 different layout strategies: either layout all items
4244 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4245 // to arrange them in top to bottom, left to right (don't ask me why
4246 // not the other way round...) order
4247 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4249 int x
= EXTRA_BORDER_X
;
4250 int y
= EXTRA_BORDER_Y
;
4252 wxCoord widthMax
= 0;
4255 for ( i
= 0; i
< count
; i
++ )
4257 wxListLineData
*line
= GetLine(i
);
4258 line
->CalculateSize( &dc
, iconSpacing
);
4259 line
->SetPosition( x
, y
, iconSpacing
);
4261 wxSize sizeLine
= GetLineSize(i
);
4263 if ( HasFlag(wxLC_ALIGN_TOP
) )
4265 if ( sizeLine
.x
> widthMax
)
4266 widthMax
= sizeLine
.x
;
4270 else // wxLC_ALIGN_LEFT
4272 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4276 if ( HasFlag(wxLC_ALIGN_TOP
) )
4278 // traverse the items again and tweak their sizes so that they are
4279 // all the same in a row
4280 for ( i
= 0; i
< count
; i
++ )
4282 wxListLineData
*line
= GetLine(i
);
4283 line
->m_gi
->ExtendWidth(widthMax
);
4291 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4292 (y
+ lineHeight
) / lineHeight
,
4293 GetScrollPos( wxHORIZONTAL
),
4294 GetScrollPos( wxVERTICAL
),
4298 else // "flowed" arrangement, the most complicated case
4300 // at first we try without any scrollbars, if the items don't fit into
4301 // the window, we recalculate after subtracting the space taken by the
4304 int entireWidth
= 0;
4306 for (int tries
= 0; tries
< 2; tries
++)
4308 entireWidth
= 2 * EXTRA_BORDER_X
;
4312 // Now we have decided that the items do not fit into the
4313 // client area, so we need a scrollbar
4314 entireWidth
+= SCROLL_UNIT_X
;
4317 int x
= EXTRA_BORDER_X
;
4318 int y
= EXTRA_BORDER_Y
;
4319 int maxWidthInThisRow
= 0;
4322 int currentlyVisibleLines
= 0;
4324 for (size_t i
= 0; i
< count
; i
++)
4326 currentlyVisibleLines
++;
4327 wxListLineData
*line
= GetLine( i
);
4328 line
->CalculateSize( &dc
, iconSpacing
);
4329 line
->SetPosition( x
, y
, iconSpacing
);
4331 wxSize sizeLine
= GetLineSize( i
);
4333 if ( maxWidthInThisRow
< sizeLine
.x
)
4334 maxWidthInThisRow
= sizeLine
.x
;
4337 if (currentlyVisibleLines
> m_linesPerPage
)
4338 m_linesPerPage
= currentlyVisibleLines
;
4340 if ( y
+ sizeLine
.y
>= clientHeight
)
4342 currentlyVisibleLines
= 0;
4344 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4345 x
+= maxWidthInThisRow
;
4346 entireWidth
+= maxWidthInThisRow
;
4347 maxWidthInThisRow
= 0;
4350 // We have reached the last item.
4351 if ( i
== count
- 1 )
4352 entireWidth
+= maxWidthInThisRow
;
4354 if ( (tries
== 0) &&
4355 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4357 clientHeight
-= wxSystemSettings::
4358 GetMetric(wxSYS_HSCROLL_Y
);
4363 if ( i
== count
- 1 )
4364 tries
= 1; // Everything fits, no second try required.
4372 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4374 GetScrollPos( wxHORIZONTAL
),
4383 // FIXME: why should we call it from here?
4390 void wxListMainWindow::RefreshAll()
4395 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4396 if ( headerWin
&& headerWin
->m_dirty
)
4398 headerWin
->m_dirty
= false;
4399 headerWin
->Refresh();
4403 void wxListMainWindow::UpdateCurrent()
4405 if ( !HasCurrent() && !IsEmpty() )
4409 long wxListMainWindow::GetNextItem( long item
,
4410 int WXUNUSED(geometry
),
4414 max
= GetItemCount();
4415 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4416 _T("invalid listctrl index in GetNextItem()") );
4418 // notice that we start with the next item (or the first one if item == -1)
4419 // and this is intentional to allow writing a simple loop to iterate over
4420 // all selected items
4423 // this is not an error because the index was OK initially,
4424 // just no such item
4431 size_t count
= GetItemCount();
4432 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4434 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4437 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4444 // ----------------------------------------------------------------------------
4446 // ----------------------------------------------------------------------------
4448 void wxListMainWindow::DeleteItem( long lindex
)
4450 size_t count
= GetItemCount();
4452 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4453 _T("invalid item index in DeleteItem") );
4455 size_t index
= (size_t)lindex
;
4457 // we don't need to adjust the index for the previous items
4458 if ( HasCurrent() && m_current
>= index
)
4460 // if the current item is being deleted, we want the next one to
4461 // become selected - unless there is no next one - so don't adjust
4462 // m_current in this case
4463 if ( m_current
!= index
|| m_current
== count
- 1 )
4467 if ( InReportView() )
4469 // mark the Column Max Width cache as dirty if the items in the line
4470 // we're deleting contain the Max Column Width
4471 wxListLineData
* const line
= GetLine(index
);
4472 wxListItemDataList::compatibility_iterator n
;
4473 wxListItemData
*itemData
;
4477 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4479 n
= line
->m_items
.Item( i
);
4480 itemData
= n
->GetData();
4481 itemData
->GetItem(item
);
4483 itemWidth
= GetItemWidthWithImage(&item
);
4485 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4486 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4489 ResetVisibleLinesRange();
4492 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4497 m_selStore
.OnItemDelete(index
);
4501 m_lines
.RemoveAt( index
);
4504 // we need to refresh the (vert) scrollbar as the number of items changed
4507 RefreshAfter(index
);
4510 void wxListMainWindow::DeleteColumn( int col
)
4512 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4514 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4517 delete node
->GetData();
4518 m_columns
.Erase( node
);
4522 // update all the items
4523 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4525 wxListLineData
* const line
= GetLine(i
);
4526 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4527 delete n
->GetData();
4528 line
->m_items
.Erase(n
);
4532 if ( InReportView() ) // we only cache max widths when in Report View
4534 delete m_aColWidths
.Item(col
);
4535 m_aColWidths
.RemoveAt(col
);
4538 // invalidate it as it has to be recalculated
4542 void wxListMainWindow::DoDeleteAllItems()
4545 // nothing to do - in particular, don't send the event
4550 // to make the deletion of all items faster, we don't send the
4551 // notifications for each item deletion in this case but only one event
4552 // for all of them: this is compatible with wxMSW and documented in
4553 // DeleteAllItems() description
4555 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4556 event
.SetEventObject( GetParent() );
4557 GetParent()->GetEventHandler()->ProcessEvent( event
);
4565 if ( InReportView() )
4567 ResetVisibleLinesRange();
4568 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4570 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4577 void wxListMainWindow::DeleteAllItems()
4581 RecalculatePositions();
4584 void wxListMainWindow::DeleteEverything()
4586 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4587 WX_CLEAR_ARRAY(m_aColWidths
);
4592 // ----------------------------------------------------------------------------
4593 // scanning for an item
4594 // ----------------------------------------------------------------------------
4596 void wxListMainWindow::EnsureVisible( long index
)
4598 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4599 _T("invalid index in EnsureVisible") );
4601 // We have to call this here because the label in question might just have
4602 // been added and its position is not known yet
4604 RecalculatePositions(true /* no refresh */);
4606 MoveToItem((size_t)index
);
4609 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4615 wxString str_upper
= str
.Upper();
4619 size_t count
= GetItemCount();
4620 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4622 wxListLineData
*line
= GetLine(i
);
4623 wxString line_upper
= line
->GetText(0).Upper();
4626 if (line_upper
== str_upper
)
4631 if (line_upper
.find(str_upper
) == 0)
4639 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4645 size_t count
= GetItemCount();
4646 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4648 wxListLineData
*line
= GetLine(i
);
4650 line
->GetItem( 0, item
);
4651 if (item
.m_data
== data
)
4658 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4661 GetVisibleLinesRange( &topItem
, NULL
);
4664 GetItemPosition( GetItemCount() - 1, p
);
4668 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4669 if ( id
>= 0 && id
< (long)GetItemCount() )
4675 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4677 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4679 size_t count
= GetItemCount();
4681 if ( InReportView() )
4683 size_t current
= y
/ GetLineHeight();
4684 if ( current
< count
)
4686 flags
= HitTestLine(current
, x
, y
);
4693 // TODO: optimize it too! this is less simple than for report view but
4694 // enumerating all items is still not a way to do it!!
4695 for ( size_t current
= 0; current
< count
; current
++ )
4697 flags
= HitTestLine(current
, x
, y
);
4706 // ----------------------------------------------------------------------------
4708 // ----------------------------------------------------------------------------
4710 void wxListMainWindow::InsertItem( wxListItem
&item
)
4712 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4714 int count
= GetItemCount();
4715 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4717 if (item
.m_itemId
> count
)
4718 item
.m_itemId
= count
;
4720 size_t id
= item
.m_itemId
;
4724 if ( InReportView() )
4726 ResetVisibleLinesRange();
4728 // calculate the width of the item and adjust the max column width
4729 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4730 int width
= GetItemWidthWithImage(&item
);
4731 item
.SetWidth(width
);
4732 if (width
> pWidthInfo
->nMaxWidth
)
4733 pWidthInfo
->nMaxWidth
= width
;
4736 wxListLineData
*line
= new wxListLineData(this);
4738 line
->SetItem( item
.m_col
, item
);
4740 m_lines
.Insert( line
, id
);
4744 // If an item is selected at or below the point of insertion, we need to
4745 // increment the member variables because the current row's index has gone
4747 if ( HasCurrent() && m_current
>= id
)
4750 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4752 RefreshLines(id
, GetItemCount() - 1);
4755 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4758 if ( InReportView() )
4760 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4761 item
.m_width
= GetTextLength( item
.m_text
);
4763 wxListHeaderData
*column
= new wxListHeaderData( item
);
4764 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4766 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4769 wxListHeaderDataList::compatibility_iterator
4770 node
= m_columns
.Item( col
);
4771 m_columns
.Insert( node
, column
);
4772 m_aColWidths
.Insert( colWidthInfo
, col
);
4776 m_columns
.Append( column
);
4777 m_aColWidths
.Add( colWidthInfo
);
4782 // update all the items
4783 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4785 wxListLineData
* const line
= GetLine(i
);
4786 wxListItemData
* const data
= new wxListItemData(this);
4788 line
->m_items
.Insert(col
, data
);
4790 line
->m_items
.Append(data
);
4794 // invalidate it as it has to be recalculated
4799 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4802 wxClientDC
dc(this);
4804 dc
.SetFont( GetFont() );
4806 if (item
->GetImage() != -1)
4809 GetImageSize( item
->GetImage(), ix
, iy
);
4813 if (!item
->GetText().empty())
4816 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4823 // ----------------------------------------------------------------------------
4825 // ----------------------------------------------------------------------------
4827 wxListCtrlCompare list_ctrl_compare_func_2
;
4828 long list_ctrl_compare_data
;
4830 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4832 wxListLineData
*line1
= *arg1
;
4833 wxListLineData
*line2
= *arg2
;
4835 line1
->GetItem( 0, item
);
4836 wxUIntPtr data1
= item
.m_data
;
4837 line2
->GetItem( 0, item
);
4838 wxUIntPtr data2
= item
.m_data
;
4839 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4842 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4844 // selections won't make sense any more after sorting the items so reset
4846 HighlightAll(false);
4849 list_ctrl_compare_func_2
= fn
;
4850 list_ctrl_compare_data
= data
;
4851 m_lines
.Sort( list_ctrl_compare_func_1
);
4855 // ----------------------------------------------------------------------------
4857 // ----------------------------------------------------------------------------
4859 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4862 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4863 wxScrolledWindow::OnScroll(event
);
4865 HandleOnScroll( event
);
4868 // update our idea of which lines are shown when we redraw the window the
4870 ResetVisibleLinesRange();
4872 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4874 wxGenericListCtrl
* lc
= GetListCtrl();
4875 wxCHECK_RET( lc
, _T("no listctrl window?") );
4877 lc
->m_headerWin
->Refresh();
4878 lc
->m_headerWin
->Update();
4882 int wxListMainWindow::GetCountPerPage() const
4884 if ( !m_linesPerPage
)
4886 wxConstCast(this, wxListMainWindow
)->
4887 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4890 return m_linesPerPage
;
4893 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4895 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4897 if ( m_lineFrom
== (size_t)-1 )
4899 size_t count
= GetItemCount();
4902 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4904 // this may happen if SetScrollbars() hadn't been called yet
4905 if ( m_lineFrom
>= count
)
4906 m_lineFrom
= count
- 1;
4908 // we redraw one extra line but this is needed to make the redrawing
4909 // logic work when there is a fractional number of lines on screen
4910 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4911 if ( m_lineTo
>= count
)
4912 m_lineTo
= count
- 1;
4914 else // empty control
4917 m_lineTo
= (size_t)-1;
4921 wxASSERT_MSG( IsEmpty() ||
4922 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4923 _T("GetVisibleLinesRange() returns incorrect result") );
4931 // -------------------------------------------------------------------------------------
4932 // wxGenericListCtrl
4933 // -------------------------------------------------------------------------------------
4935 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4937 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4938 EVT_SIZE(wxGenericListCtrl::OnSize
)
4941 wxGenericListCtrl::wxGenericListCtrl()
4943 m_imageListNormal
= (wxImageList
*) NULL
;
4944 m_imageListSmall
= (wxImageList
*) NULL
;
4945 m_imageListState
= (wxImageList
*) NULL
;
4947 m_ownsImageListNormal
=
4948 m_ownsImageListSmall
=
4949 m_ownsImageListState
= false;
4951 m_mainWin
= (wxListMainWindow
*) NULL
;
4952 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4956 wxGenericListCtrl::~wxGenericListCtrl()
4958 if (m_ownsImageListNormal
)
4959 delete m_imageListNormal
;
4960 if (m_ownsImageListSmall
)
4961 delete m_imageListSmall
;
4962 if (m_ownsImageListState
)
4963 delete m_imageListState
;
4966 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4972 GetThemeMetric( kThemeMetricListHeaderHeight
, &h
);
4974 // we use 'g' to get the descent, too
4976 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4977 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4980 // only update if changed
4981 if ( h
!= m_headerHeight
)
4986 ResizeReportView(true);
4987 else //why is this needed if it doesn't have a header?
4988 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4993 void wxGenericListCtrl::CreateHeaderWindow()
4995 m_headerWin
= new wxListHeaderWindow
4997 this, wxID_ANY
, m_mainWin
,
4999 wxSize(GetClientSize().x
, m_headerHeight
),
5002 CalculateAndSetHeaderHeight();
5005 bool wxGenericListCtrl::Create(wxWindow
*parent
,
5010 const wxValidator
&validator
,
5011 const wxString
&name
)
5015 m_imageListState
= (wxImageList
*) NULL
;
5016 m_ownsImageListNormal
=
5017 m_ownsImageListSmall
=
5018 m_ownsImageListState
= false;
5020 m_mainWin
= (wxListMainWindow
*) NULL
;
5021 m_headerWin
= (wxListHeaderWindow
*) NULL
;
5025 if ( !(style
& wxLC_MASK_TYPE
) )
5027 style
= style
| wxLC_LIST
;
5030 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
5033 // this window itself shouldn't get the focus, only m_mainWin should
5036 // don't create the inner window with the border
5037 style
&= ~wxBORDER_MASK
;
5039 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
5042 // Human Interface Guidelines ask us for a special font in this case
5043 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
)
5046 font
.MacCreateFromThemeFont( kThemeViewsFont
);
5051 if ( InReportView() )
5053 CreateHeaderWindow();
5059 font
.MacCreateFromThemeFont( kThemeSmallSystemFont
);
5060 m_headerWin
->SetFont( font
);
5061 CalculateAndSetHeaderHeight();
5065 if ( HasFlag(wxLC_NO_HEADER
) )
5066 // VZ: why do we create it at all then?
5067 m_headerWin
->Show( false );
5070 SetInitialSize(size
);
5075 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
5077 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
5078 _T("wxLC_VIRTUAL can't be [un]set") );
5080 long flag
= GetWindowStyle();
5084 if (style
& wxLC_MASK_TYPE
)
5085 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5086 if (style
& wxLC_MASK_ALIGN
)
5087 flag
&= ~wxLC_MASK_ALIGN
;
5088 if (style
& wxLC_MASK_SORT
)
5089 flag
&= ~wxLC_MASK_SORT
;
5097 SetWindowStyleFlag( flag
);
5100 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5104 m_mainWin
->DeleteEverything();
5106 // has the header visibility changed?
5107 bool hasHeader
= HasHeader();
5108 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
5110 if ( hasHeader
!= willHaveHeader
)
5117 // don't delete, just hide, as we can reuse it later
5118 m_headerWin
->Show(false);
5120 //else: nothing to do
5122 else // must show header
5126 CreateHeaderWindow();
5128 else // already have it, just show
5130 m_headerWin
->Show( true );
5134 ResizeReportView(willHaveHeader
);
5138 wxWindow::SetWindowStyleFlag( flag
);
5141 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5143 m_mainWin
->GetColumn( col
, item
);
5147 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5149 m_mainWin
->SetColumn( col
, item
);
5153 int wxGenericListCtrl::GetColumnWidth( int col
) const
5155 return m_mainWin
->GetColumnWidth( col
);
5158 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5160 m_mainWin
->SetColumnWidth( col
, width
);
5164 int wxGenericListCtrl::GetCountPerPage() const
5166 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5169 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5171 m_mainWin
->GetItem( info
);
5175 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5177 m_mainWin
->SetItem( info
);
5181 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5184 info
.m_text
= label
;
5185 info
.m_mask
= wxLIST_MASK_TEXT
;
5186 info
.m_itemId
= index
;
5190 info
.m_image
= imageId
;
5191 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5194 m_mainWin
->SetItem(info
);
5198 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5200 return m_mainWin
->GetItemState( item
, stateMask
);
5203 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5205 m_mainWin
->SetItemState( item
, state
, stateMask
);
5210 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5212 return SetItemColumnImage(item
, 0, image
);
5216 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5219 info
.m_image
= image
;
5220 info
.m_mask
= wxLIST_MASK_IMAGE
;
5221 info
.m_itemId
= item
;
5222 info
.m_col
= column
;
5223 m_mainWin
->SetItem( info
);
5227 wxString
wxGenericListCtrl::GetItemText( long item
) const
5229 return m_mainWin
->GetItemText(item
);
5232 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5234 m_mainWin
->SetItemText(item
, str
);
5237 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5240 info
.m_mask
= wxLIST_MASK_DATA
;
5241 info
.m_itemId
= item
;
5242 m_mainWin
->GetItem( info
);
5246 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
5249 info
.m_mask
= wxLIST_MASK_DATA
;
5250 info
.m_itemId
= item
;
5252 m_mainWin
->SetItem( info
);
5256 wxRect
wxGenericListCtrl::GetViewRect() const
5258 return m_mainWin
->GetViewRect();
5261 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
5263 m_mainWin
->GetItemRect( item
, rect
);
5264 if ( m_mainWin
->HasHeader() )
5265 rect
.y
+= m_headerHeight
+ 1;
5269 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5271 m_mainWin
->GetItemPosition( item
, pos
);
5275 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5280 int wxGenericListCtrl::GetItemCount() const
5282 return m_mainWin
->GetItemCount();
5285 int wxGenericListCtrl::GetColumnCount() const
5287 return m_mainWin
->GetColumnCount();
5290 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5292 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5295 wxSize
wxGenericListCtrl::GetItemSpacing() const
5297 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5299 return wxSize(spacing
, spacing
);
5302 #if WXWIN_COMPATIBILITY_2_6
5303 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5305 return m_mainWin
->GetItemSpacing( isSmall
);
5307 #endif // WXWIN_COMPATIBILITY_2_6
5309 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5312 info
.m_itemId
= item
;
5313 info
.SetTextColour( col
);
5314 m_mainWin
->SetItem( info
);
5317 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5320 info
.m_itemId
= item
;
5321 m_mainWin
->GetItem( info
);
5322 return info
.GetTextColour();
5325 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5328 info
.m_itemId
= item
;
5329 info
.SetBackgroundColour( col
);
5330 m_mainWin
->SetItem( info
);
5333 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5336 info
.m_itemId
= item
;
5337 m_mainWin
->GetItem( info
);
5338 return info
.GetBackgroundColour();
5341 int wxGenericListCtrl::GetScrollPos( int orient
) const
5343 return m_mainWin
->GetScrollPos( orient
);
5346 void wxGenericListCtrl::SetScrollPos( int orient
, int pos
, bool refresh
)
5348 m_mainWin
->SetScrollPos( orient
, pos
, refresh
);
5351 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5354 info
.m_itemId
= item
;
5356 m_mainWin
->SetItem( info
);
5359 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5362 info
.m_itemId
= item
;
5363 m_mainWin
->GetItem( info
);
5364 return info
.GetFont();
5367 int wxGenericListCtrl::GetSelectedItemCount() const
5369 return m_mainWin
->GetSelectedItemCount();
5372 wxColour
wxGenericListCtrl::GetTextColour() const
5374 return GetForegroundColour();
5377 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5379 SetForegroundColour(col
);
5382 long wxGenericListCtrl::GetTopItem() const
5385 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5389 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5391 return m_mainWin
->GetNextItem( item
, geom
, state
);
5394 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5396 if (which
== wxIMAGE_LIST_NORMAL
)
5397 return m_imageListNormal
;
5398 else if (which
== wxIMAGE_LIST_SMALL
)
5399 return m_imageListSmall
;
5400 else if (which
== wxIMAGE_LIST_STATE
)
5401 return m_imageListState
;
5403 return (wxImageList
*) NULL
;
5406 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5408 if ( which
== wxIMAGE_LIST_NORMAL
)
5410 if (m_ownsImageListNormal
)
5411 delete m_imageListNormal
;
5412 m_imageListNormal
= imageList
;
5413 m_ownsImageListNormal
= false;
5415 else if ( which
== wxIMAGE_LIST_SMALL
)
5417 if (m_ownsImageListSmall
)
5418 delete m_imageListSmall
;
5419 m_imageListSmall
= imageList
;
5420 m_ownsImageListSmall
= false;
5422 else if ( which
== wxIMAGE_LIST_STATE
)
5424 if (m_ownsImageListState
)
5425 delete m_imageListState
;
5426 m_imageListState
= imageList
;
5427 m_ownsImageListState
= false;
5430 m_mainWin
->SetImageList( imageList
, which
);
5433 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5435 SetImageList(imageList
, which
);
5436 if ( which
== wxIMAGE_LIST_NORMAL
)
5437 m_ownsImageListNormal
= true;
5438 else if ( which
== wxIMAGE_LIST_SMALL
)
5439 m_ownsImageListSmall
= true;
5440 else if ( which
== wxIMAGE_LIST_STATE
)
5441 m_ownsImageListState
= true;
5444 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5449 bool wxGenericListCtrl::DeleteItem( long item
)
5451 m_mainWin
->DeleteItem( item
);
5455 bool wxGenericListCtrl::DeleteAllItems()
5457 m_mainWin
->DeleteAllItems();
5461 bool wxGenericListCtrl::DeleteAllColumns()
5463 size_t count
= m_mainWin
->m_columns
.GetCount();
5464 for ( size_t n
= 0; n
< count
; n
++ )
5469 void wxGenericListCtrl::ClearAll()
5471 m_mainWin
->DeleteEverything();
5474 bool wxGenericListCtrl::DeleteColumn( int col
)
5476 m_mainWin
->DeleteColumn( col
);
5478 // if we don't have the header any longer, we need to relayout the window
5479 if ( !GetColumnCount() )
5480 ResizeReportView(false /* no header */);
5484 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5485 wxClassInfo
* textControlClass
)
5487 return m_mainWin
->EditLabel( item
, textControlClass
);
5490 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5492 return m_mainWin
->GetEditControl();
5495 bool wxGenericListCtrl::EnsureVisible( long item
)
5497 m_mainWin
->EnsureVisible( item
);
5501 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5503 return m_mainWin
->FindItem( start
, str
, partial
);
5506 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5508 return m_mainWin
->FindItem( start
, data
);
5511 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5512 int WXUNUSED(direction
))
5514 return m_mainWin
->FindItem( pt
);
5517 // TODO: sub item hit testing
5518 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5520 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5523 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5525 m_mainWin
->InsertItem( info
);
5526 return info
.m_itemId
;
5529 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5532 info
.m_text
= label
;
5533 info
.m_mask
= wxLIST_MASK_TEXT
;
5534 info
.m_itemId
= index
;
5535 return InsertItem( info
);
5538 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5541 info
.m_mask
= wxLIST_MASK_IMAGE
;
5542 info
.m_image
= imageIndex
;
5543 info
.m_itemId
= index
;
5544 return InsertItem( info
);
5547 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5550 info
.m_text
= label
;
5551 info
.m_image
= imageIndex
;
5552 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5553 info
.m_itemId
= index
;
5554 return InsertItem( info
);
5557 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5559 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5561 m_mainWin
->InsertColumn( col
, item
);
5563 // if we hadn't had a header before but have one now
5564 // then we need to relayout the window
5565 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5566 ResizeReportView(true /* have header */);
5568 m_headerWin
->Refresh();
5573 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5574 int format
, int width
)
5577 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5578 item
.m_text
= heading
;
5581 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5582 item
.m_width
= width
;
5585 item
.m_format
= format
;
5587 return InsertColumn( col
, item
);
5590 bool wxGenericListCtrl::ScrollList( int dx
, int dy
)
5592 return m_mainWin
->ScrollList(dx
, dy
);
5596 // fn is a function which takes 3 long arguments: item1, item2, data.
5597 // item1 is the long data associated with a first item (NOT the index).
5598 // item2 is the long data associated with a second item (NOT the index).
5599 // data is the same value as passed to SortItems.
5600 // The return value is a negative number if the first item should precede the second
5601 // item, a positive number of the second item should precede the first,
5602 // or zero if the two items are equivalent.
5603 // data is arbitrary data to be passed to the sort function.
5605 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5607 m_mainWin
->SortItems( fn
, data
);
5611 // ----------------------------------------------------------------------------
5613 // ----------------------------------------------------------------------------
5615 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5620 ResizeReportView(m_mainWin
->HasHeader());
5621 m_mainWin
->RecalculatePositions();
5624 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5627 GetClientSize( &cw
, &ch
);
5631 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5632 if(ch
> m_headerHeight
)
5633 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5634 cw
, ch
- m_headerHeight
- 1 );
5636 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5639 else // no header window
5641 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5645 void wxGenericListCtrl::OnInternalIdle()
5647 wxWindow::OnInternalIdle();
5649 // do it only if needed
5650 if ( !m_mainWin
->m_dirty
)
5653 m_mainWin
->RecalculatePositions();
5656 // ----------------------------------------------------------------------------
5658 // ----------------------------------------------------------------------------
5660 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5664 m_mainWin
->SetBackgroundColour( colour
);
5665 m_mainWin
->m_dirty
= true;
5671 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5673 if ( !wxWindow::SetForegroundColour( colour
) )
5678 m_mainWin
->SetForegroundColour( colour
);
5679 m_mainWin
->m_dirty
= true;
5683 m_headerWin
->SetForegroundColour( colour
);
5688 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5690 if ( !wxWindow::SetFont( font
) )
5695 m_mainWin
->SetFont( font
);
5696 m_mainWin
->m_dirty
= true;
5701 m_headerWin
->SetFont( font
);
5702 CalculateAndSetHeaderHeight();
5712 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5715 // Use the same color scheme as wxListBox
5716 return wxListBox::GetClassDefaultAttributes(variant
);
5718 wxUnusedVar(variant
);
5719 wxVisualAttributes attr
;
5720 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5721 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5722 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5727 // ----------------------------------------------------------------------------
5728 // methods forwarded to m_mainWin
5729 // ----------------------------------------------------------------------------
5731 #if wxUSE_DRAG_AND_DROP
5733 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5735 m_mainWin
->SetDropTarget( dropTarget
);
5738 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5740 return m_mainWin
->GetDropTarget();
5745 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5747 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5750 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5752 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5755 wxColour
wxGenericListCtrl::GetForegroundColour() const
5757 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5760 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5763 return m_mainWin
->PopupMenu( menu
, x
, y
);
5769 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5771 m_mainWin
->DoClientToScreen(x
, y
);
5774 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5776 m_mainWin
->DoScreenToClient(x
, y
);
5779 void wxGenericListCtrl::SetFocus()
5781 // The test in window.cpp fails as we are a composite
5782 // window, so it checks against "this", but not m_mainWin.
5783 if ( DoFindFocus() != this )
5784 m_mainWin
->SetFocus();
5787 wxSize
wxGenericListCtrl::DoGetBestSize() const
5789 // Something is better than nothing...
5790 // 100x80 is what the MSW version will get from the default
5791 // wxControl::DoGetBestSize
5792 return wxSize(100, 80);
5795 // ----------------------------------------------------------------------------
5796 // virtual list control support
5797 // ----------------------------------------------------------------------------
5799 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5801 // this is a pure virtual function, in fact - which is not really pure
5802 // because the controls which are not virtual don't need to implement it
5803 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5805 return wxEmptyString
;
5808 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5810 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5812 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5816 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5819 return OnGetItemImage(item
);
5825 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5827 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5828 _T("invalid item index in OnGetItemAttr()") );
5830 // no attributes by default
5834 void wxGenericListCtrl::SetItemCount(long count
)
5836 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5838 m_mainWin
->SetItemCount(count
);
5841 void wxGenericListCtrl::RefreshItem(long item
)
5843 m_mainWin
->RefreshLine(item
);
5846 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5848 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5851 // Generic wxListCtrl is more or less a container for two other
5852 // windows which drawings are done upon. These are namely
5853 // 'm_headerWin' and 'm_mainWin'.
5854 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5855 // behaviour wxListCtrl has under wxMSW.
5857 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5861 // The easy case, no rectangle specified.
5863 m_headerWin
->Refresh(eraseBackground
);
5866 m_mainWin
->Refresh(eraseBackground
);
5870 // Refresh the header window
5873 wxRect rectHeader
= m_headerWin
->GetRect();
5874 rectHeader
.Intersect(*rect
);
5875 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5878 m_headerWin
->GetPosition(&x
, &y
);
5879 rectHeader
.Offset(-x
, -y
);
5880 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5884 // Refresh the main window
5887 wxRect rectMain
= m_mainWin
->GetRect();
5888 rectMain
.Intersect(*rect
);
5889 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5892 m_mainWin
->GetPosition(&x
, &y
);
5893 rectMain
.Offset(-x
, -y
);
5894 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5900 #endif // wxUSE_LISTCTRL