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"
47 #include "wx/imaglist.h"
48 #include "wx/selstore.h"
49 #include "wx/renderer.h"
52 #include "wx/mac/private.h"
56 // NOTE: If using the wxListBox visual attributes works everywhere then this can
57 // be removed, as well as the #else case below.
58 #define _USE_VISATTR 0
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 // // the height of the header window (FIXME: should depend on its font!)
66 // static const int HEADER_HEIGHT = 23;
68 static const int SCROLL_UNIT_X
= 15;
70 // the spacing between the lines (in report mode)
71 static const int LINE_SPACING
= 0;
73 // extra margins around the text label
75 static const int EXTRA_WIDTH
= 6;
77 static const int EXTRA_WIDTH
= 4;
79 static const int EXTRA_HEIGHT
= 4;
81 // margin between the window and the items
82 static const int EXTRA_BORDER_X
= 2;
83 static const int EXTRA_BORDER_Y
= 2;
85 // offset for the header window
86 static const int HEADER_OFFSET_X
= 1;
87 static const int HEADER_OFFSET_Y
= 1;
89 // margin between rows of icons in [small] icon view
90 static const int MARGIN_BETWEEN_ROWS
= 6;
92 // when autosizing the columns, add some slack
93 static const int AUTOSIZE_COL_MARGIN
= 10;
95 // default width for the header columns
96 static const int WIDTH_COL_DEFAULT
= 80;
98 // the space between the image and the text in the report mode
99 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
101 // the space between the image and the text in the report mode in header
102 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE
= 2;
104 // ============================================================================
106 // ============================================================================
108 //-----------------------------------------------------------------------------
109 // wxColWidthInfo (internal)
110 //-----------------------------------------------------------------------------
112 struct wxColWidthInfo
115 bool bNeedsUpdate
; // only set to true when an item whose
116 // width == nMaxWidth is removed
118 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
121 bNeedsUpdate
= needsUpdate
;
125 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
127 //-----------------------------------------------------------------------------
128 // wxListItemData (internal)
129 //-----------------------------------------------------------------------------
134 wxListItemData(wxListMainWindow
*owner
);
137 void SetItem( const wxListItem
&info
);
138 void SetImage( int image
) { m_image
= image
; }
139 void SetData( wxUIntPtr data
) { m_data
= data
; }
140 void SetPosition( int x
, int y
);
141 void SetSize( int width
, int height
);
143 bool HasText() const { return !m_text
.empty(); }
144 const wxString
& GetText() const { return m_text
; }
145 void SetText(const wxString
& text
) { m_text
= text
; }
147 // we can't use empty string for measuring the string width/height, so
148 // always return something
149 wxString
GetTextForMeasuring() const
151 wxString s
= GetText();
158 bool IsHit( int x
, int y
) const;
162 int GetWidth() const;
163 int GetHeight() const;
165 int GetImage() const { return m_image
; }
166 bool HasImage() const { return GetImage() != -1; }
168 void GetItem( wxListItem
&info
) const;
170 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
171 wxListItemAttr
*GetAttr() const { return m_attr
; }
174 // the item image or -1
177 // user data associated with the item
180 // the item coordinates are not used in report mode; instead this pointer is
181 // NULL and the owner window is used to retrieve the item position and size
184 // the list ctrl we are in
185 wxListMainWindow
*m_owner
;
187 // custom attributes or NULL
188 wxListItemAttr
*m_attr
;
191 // common part of all ctors
197 //-----------------------------------------------------------------------------
198 // wxListHeaderData (internal)
199 //-----------------------------------------------------------------------------
201 class wxListHeaderData
: public wxObject
205 wxListHeaderData( const wxListItem
&info
);
206 void SetItem( const wxListItem
&item
);
207 void SetPosition( int x
, int y
);
208 void SetWidth( int w
);
209 void SetState( int state
);
210 void SetFormat( int format
);
211 void SetHeight( int h
);
212 bool HasImage() const;
214 bool HasText() const { return !m_text
.empty(); }
215 const wxString
& GetText() const { return m_text
; }
216 void SetText(const wxString
& text
) { m_text
= text
; }
218 void GetItem( wxListItem
&item
);
220 bool IsHit( int x
, int y
) const;
221 int GetImage() const;
222 int GetWidth() const;
223 int GetFormat() const;
224 int GetState() const;
241 //-----------------------------------------------------------------------------
242 // wxListLineData (internal)
243 //-----------------------------------------------------------------------------
245 WX_DECLARE_EXPORTED_LIST(wxListItemData
, wxListItemDataList
);
246 #include "wx/listimpl.cpp"
247 WX_DEFINE_LIST(wxListItemDataList
)
252 // the list of subitems: only may have more than one item in report mode
253 wxListItemDataList m_items
;
255 // this is not used in report view
267 // the part to be highlighted
268 wxRect m_rectHighlight
;
270 // extend all our rects to be centered inside the one of given width
271 void ExtendWidth(wxCoord w
)
273 wxASSERT_MSG( m_rectAll
.width
<= w
,
274 _T("width can only be increased") );
277 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
278 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
279 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
284 // is this item selected? [NB: not used in virtual mode]
287 // back pointer to the list ctrl
288 wxListMainWindow
*m_owner
;
291 wxListLineData(wxListMainWindow
*owner
);
295 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
299 // are we in report mode?
300 inline bool InReportView() const;
302 // are we in virtual report mode?
303 inline bool IsVirtual() const;
305 // these 2 methods shouldn't be called for report view controls, in that
306 // case we determine our position/size ourselves
308 // calculate the size of the line
309 void CalculateSize( wxDC
*dc
, int spacing
);
311 // remember the position this line appears at
312 void SetPosition( int x
, int y
, int spacing
);
316 void SetImage( int image
) { SetImage(0, image
); }
317 int GetImage() const { return GetImage(0); }
318 void SetImage( int index
, int image
);
319 int GetImage( int index
) const;
321 bool HasImage() const { return GetImage() != -1; }
322 bool HasText() const { return !GetText(0).empty(); }
324 void SetItem( int index
, const wxListItem
&info
);
325 void GetItem( int index
, wxListItem
&info
);
327 wxString
GetText(int index
) const;
328 void SetText( int index
, const wxString
& s
);
330 wxListItemAttr
*GetAttr() const;
331 void SetAttr(wxListItemAttr
*attr
);
333 // return true if the highlighting really changed
334 bool Highlight( bool on
);
336 void ReverseHighlight();
338 bool IsHighlighted() const
340 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
342 return m_highlighted
;
345 // draw the line on the given DC in icon/list mode
346 void Draw( wxDC
*dc
);
348 // the same in report mode
349 void DrawInReportMode( wxDC
*dc
,
351 const wxRect
& rectHL
,
355 // set the line to contain num items (only can be > 1 in report mode)
356 void InitItems( int num
);
358 // get the mode (i.e. style) of the list control
359 inline int GetMode() const;
361 // prepare the DC for drawing with these item's attributes, return true if
362 // we need to draw the items background to highlight it, false otherwise
363 bool SetAttributes(wxDC
*dc
,
364 const wxListItemAttr
*attr
,
367 // draw the text on the DC with the correct justification; also add an
368 // ellipsis if the text is too large to fit in the current width
369 void DrawTextFormatted(wxDC
*dc
,
370 const wxString
&text
,
373 int yMid
, // this is middle, not top, of the text
377 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
378 #include "wx/arrimpl.cpp"
379 WX_DEFINE_OBJARRAY(wxListLineDataArray
)
381 //-----------------------------------------------------------------------------
382 // wxListHeaderWindow (internal)
383 //-----------------------------------------------------------------------------
385 class wxListHeaderWindow
: public wxWindow
388 wxListMainWindow
*m_owner
;
389 const wxCursor
*m_currentCursor
;
390 wxCursor
*m_resizeCursor
;
393 // column being resized or -1
396 // divider line position in logical (unscrolled) coords
399 // minimal position beyond which the divider line
400 // can't be dragged in logical coords
404 wxListHeaderWindow();
406 wxListHeaderWindow( wxWindow
*win
,
408 wxListMainWindow
*owner
,
409 const wxPoint
&pos
= wxDefaultPosition
,
410 const wxSize
&size
= wxDefaultSize
,
412 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
414 virtual ~wxListHeaderWindow();
417 void AdjustDC( wxDC
& dc
);
419 void OnPaint( wxPaintEvent
&event
);
420 void OnMouse( wxMouseEvent
&event
);
421 void OnSetFocus( wxFocusEvent
&event
);
427 // common part of all ctors
430 // generate and process the list event of the given type, return true if
431 // it wasn't vetoed, i.e. if we should proceed
432 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
434 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
435 DECLARE_EVENT_TABLE()
438 //-----------------------------------------------------------------------------
439 // wxListRenameTimer (internal)
440 //-----------------------------------------------------------------------------
442 class wxListRenameTimer
: public wxTimer
445 wxListMainWindow
*m_owner
;
448 wxListRenameTimer( wxListMainWindow
*owner
);
452 //-----------------------------------------------------------------------------
453 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
454 //-----------------------------------------------------------------------------
456 class wxListTextCtrlWrapper
: public wxEvtHandler
459 // NB: text must be a valid object but not Create()d yet
460 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
464 wxTextCtrl
*GetText() const { return m_text
; }
466 void AcceptChangesAndFinish();
469 void OnChar( wxKeyEvent
&event
);
470 void OnKeyUp( wxKeyEvent
&event
);
471 void OnKillFocus( wxFocusEvent
&event
);
473 bool AcceptChanges();
477 wxListMainWindow
*m_owner
;
479 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 // bring the current item into view
583 void MoveToFocus() { MoveToItem(m_current
); }
585 // start editing the label of the given item
586 wxTextCtrl
*EditLabel(long item
,
587 wxClassInfo
* textControlClass
= CLASSINFO(wxTextCtrl
));
588 wxTextCtrl
*GetEditControl() const
590 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
593 void FinishEditing(wxTextCtrl
*text
)
596 m_textctrlWrapper
= NULL
;
597 SetFocusIgnoringChildren();
600 // suspend/resume redrawing the control
604 void OnRenameTimer();
605 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
606 void OnRenameCancelled(size_t itemEdit
);
608 void OnMouse( wxMouseEvent
&event
);
610 // called to switch the selection from the current item to newCurrent,
611 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
613 void OnChar( wxKeyEvent
&event
);
614 void OnKeyDown( wxKeyEvent
&event
);
615 void OnKeyUp( wxKeyEvent
&event
);
616 void OnSetFocus( wxFocusEvent
&event
);
617 void OnKillFocus( wxFocusEvent
&event
);
618 void OnScroll( wxScrollWinEvent
& event
);
620 void OnPaint( wxPaintEvent
&event
);
622 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
623 void GetImageSize( int index
, int &width
, int &height
) const;
624 int GetTextLength( const wxString
&s
) const;
626 void SetImageList( wxImageList
*imageList
, int which
);
627 void SetItemSpacing( int spacing
, bool isSmall
= false );
628 int GetItemSpacing( bool isSmall
= false );
630 void SetColumn( int col
, wxListItem
&item
);
631 void SetColumnWidth( int col
, int width
);
632 void GetColumn( int col
, wxListItem
&item
) const;
633 int GetColumnWidth( int col
) const;
634 int GetColumnCount() const { return m_columns
.GetCount(); }
636 // returns the sum of the heights of all columns
637 int GetHeaderWidth() const;
639 int GetCountPerPage() const;
641 void SetItem( wxListItem
&item
);
642 void GetItem( wxListItem
&item
) const;
643 void SetItemState( long item
, long state
, long stateMask
);
644 void SetItemStateAll( long state
, long stateMask
);
645 int GetItemState( long item
, long stateMask
) const;
646 void GetItemRect( long index
, wxRect
&rect
) const;
647 wxRect
GetViewRect() const;
648 bool GetItemPosition( long item
, wxPoint
& pos
) const;
649 int GetSelectedItemCount() const;
651 wxString
GetItemText(long item
) const
654 info
.m_mask
= wxLIST_MASK_TEXT
;
655 info
.m_itemId
= item
;
660 void SetItemText(long item
, const wxString
& value
)
663 info
.m_mask
= wxLIST_MASK_TEXT
;
664 info
.m_itemId
= item
;
669 // set the scrollbars and update the positions of the items
670 void RecalculatePositions(bool noRefresh
= false);
672 // refresh the window and the header
675 long GetNextItem( long item
, int geometry
, int state
) const;
676 void DeleteItem( long index
);
677 void DeleteAllItems();
678 void DeleteColumn( int col
);
679 void DeleteEverything();
680 void EnsureVisible( long index
);
681 long FindItem( long start
, const wxString
& str
, bool partial
= false );
682 long FindItem( long start
, wxUIntPtr data
);
683 long FindItem( const wxPoint
& pt
);
684 long HitTest( int x
, int y
, int &flags
) const;
685 void InsertItem( wxListItem
&item
);
686 void InsertColumn( long col
, wxListItem
&item
);
687 int GetItemWidthWithImage(wxListItem
* item
);
688 void SortItems( wxListCtrlCompare fn
, long data
);
690 size_t GetItemCount() const;
691 bool IsEmpty() const { return GetItemCount() == 0; }
692 void SetItemCount(long count
);
694 // change the current (== focused) item, send a notification event
695 void ChangeCurrent(size_t current
);
696 void ResetCurrent() { ChangeCurrent((size_t)-1); }
697 bool HasCurrent() const { return m_current
!= (size_t)-1; }
699 // send out a wxListEvent
700 void SendNotify( size_t line
,
702 const wxPoint
& point
= wxDefaultPosition
);
704 // override base class virtual to reset m_lineHeight when the font changes
705 virtual bool SetFont(const wxFont
& font
)
707 if ( !wxScrolledWindow::SetFont(font
) )
715 // these are for wxListLineData usage only
717 // get the backpointer to the list ctrl
718 wxGenericListCtrl
*GetListCtrl() const
720 return wxStaticCast(GetParent(), wxGenericListCtrl
);
723 // get the height of all lines (assuming they all do have the same height)
724 wxCoord
GetLineHeight() const;
726 // get the y position of the given line (only for report view)
727 wxCoord
GetLineY(size_t line
) const;
729 // get the brush to use for the item highlighting
730 wxBrush
*GetHighlightBrush() const
732 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
735 bool HasFocus() const
741 // the array of all line objects for a non virtual list control (for the
742 // virtual list control we only ever use m_lines[0])
743 wxListLineDataArray m_lines
;
745 // the list of column objects
746 wxListHeaderDataList m_columns
;
748 // currently focused item or -1
751 // the number of lines per page
754 // this flag is set when something which should result in the window
755 // redrawing happens (i.e. an item was added or deleted, or its appearance
756 // changed) and OnPaint() doesn't redraw the window while it is set which
757 // allows to minimize the number of repaintings when a lot of items are
758 // being added. The real repainting occurs only after the next OnIdle()
762 wxColour
*m_highlightColour
;
763 wxImageList
*m_small_image_list
;
764 wxImageList
*m_normal_image_list
;
766 int m_normal_spacing
;
770 wxTimer
*m_renameTimer
;
774 ColWidthArray m_aColWidths
;
776 // for double click logic
777 size_t m_lineLastClicked
,
778 m_lineBeforeLastClicked
,
779 m_lineSelectSingleOnUp
;
782 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
784 // the total count of items in a virtual list control
787 // the object maintaining the items selection state, only used in virtual
789 wxSelectionStore m_selStore
;
791 // common part of all ctors
794 // get the line data for the given index
795 wxListLineData
*GetLine(size_t n
) const
797 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
801 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
808 // get a dummy line which can be used for geometry calculations and such:
809 // you must use GetLine() if you want to really draw the line
810 wxListLineData
*GetDummyLine() const;
812 // cache the line data of the n-th line in m_lines[0]
813 void CacheLineData(size_t line
);
815 // get the range of visible lines
816 void GetVisibleLinesRange(size_t *from
, size_t *to
);
818 // force us to recalculate the range of visible lines
819 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
821 // get the colour to be used for drawing the rules
822 wxColour
GetRuleColour() const
824 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
828 // initialize the current item if needed
829 void UpdateCurrent();
831 // delete all items but don't refresh: called from dtor
832 void DoDeleteAllItems();
834 // the height of one line using the current font
835 wxCoord m_lineHeight
;
837 // the total header width or 0 if not calculated yet
838 wxCoord m_headerWidth
;
840 // the first and last lines being shown on screen right now (inclusive),
841 // both may be -1 if they must be calculated so never access them directly:
842 // use GetVisibleLinesRange() above instead
846 // the brushes to use for item highlighting when we do/don't have focus
847 wxBrush
*m_highlightBrush
,
848 *m_highlightUnfocusedBrush
;
850 // if this is > 0, the control is frozen and doesn't redraw itself
851 size_t m_freezeCount
;
853 // wrapper around the text control currently used for in place editing or
854 // NULL if no item is being edited
855 wxListTextCtrlWrapper
*m_textctrlWrapper
;
858 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
859 DECLARE_EVENT_TABLE()
861 friend class wxGenericListCtrl
;
865 wxListItemData::~wxListItemData()
867 // in the virtual list control the attributes are managed by the main
868 // program, so don't delete them
869 if ( !m_owner
->IsVirtual() )
875 void wxListItemData::Init()
883 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
889 if ( owner
->InReportView() )
895 void wxListItemData::SetItem( const wxListItem
&info
)
897 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
898 SetText(info
.m_text
);
899 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
900 m_image
= info
.m_image
;
901 if ( info
.m_mask
& wxLIST_MASK_DATA
)
902 m_data
= info
.m_data
;
904 if ( info
.HasAttributes() )
907 m_attr
->AssignFrom(*info
.GetAttributes());
909 m_attr
= new wxListItemAttr(*info
.GetAttributes());
917 m_rect
->width
= info
.m_width
;
921 void wxListItemData::SetPosition( int x
, int y
)
923 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
929 void wxListItemData::SetSize( int width
, int height
)
931 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
934 m_rect
->width
= width
;
936 m_rect
->height
= height
;
939 bool wxListItemData::IsHit( int x
, int y
) const
941 wxCHECK_MSG( m_rect
, false, _T("can't be called in this mode") );
943 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x
, y
);
946 int wxListItemData::GetX() const
948 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
953 int wxListItemData::GetY() const
955 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
960 int wxListItemData::GetWidth() const
962 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
964 return m_rect
->width
;
967 int wxListItemData::GetHeight() const
969 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
971 return m_rect
->height
;
974 void wxListItemData::GetItem( wxListItem
&info
) const
976 long mask
= info
.m_mask
;
978 // by default, get everything for backwards compatibility
981 if ( mask
& wxLIST_MASK_TEXT
)
982 info
.m_text
= m_text
;
983 if ( mask
& wxLIST_MASK_IMAGE
)
984 info
.m_image
= m_image
;
985 if ( mask
& wxLIST_MASK_DATA
)
986 info
.m_data
= m_data
;
990 if ( m_attr
->HasTextColour() )
991 info
.SetTextColour(m_attr
->GetTextColour());
992 if ( m_attr
->HasBackgroundColour() )
993 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
994 if ( m_attr
->HasFont() )
995 info
.SetFont(m_attr
->GetFont());
999 //-----------------------------------------------------------------------------
1001 //-----------------------------------------------------------------------------
1003 void wxListHeaderData::Init()
1015 wxListHeaderData::wxListHeaderData()
1020 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1027 void wxListHeaderData::SetItem( const wxListItem
&item
)
1029 m_mask
= item
.m_mask
;
1031 if ( m_mask
& wxLIST_MASK_TEXT
)
1032 m_text
= item
.m_text
;
1034 if ( m_mask
& wxLIST_MASK_IMAGE
)
1035 m_image
= item
.m_image
;
1037 if ( m_mask
& wxLIST_MASK_FORMAT
)
1038 m_format
= item
.m_format
;
1040 if ( m_mask
& wxLIST_MASK_WIDTH
)
1041 SetWidth(item
.m_width
);
1043 if ( m_mask
& wxLIST_MASK_STATE
)
1044 SetState(item
.m_state
);
1047 void wxListHeaderData::SetPosition( int x
, int y
)
1053 void wxListHeaderData::SetHeight( int h
)
1058 void wxListHeaderData::SetWidth( int w
)
1060 m_width
= w
< 0 ? WIDTH_COL_DEFAULT
: w
;
1063 void wxListHeaderData::SetState( int flag
)
1068 void wxListHeaderData::SetFormat( int format
)
1073 bool wxListHeaderData::HasImage() const
1075 return m_image
!= -1;
1078 bool wxListHeaderData::IsHit( int x
, int y
) const
1080 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1083 void wxListHeaderData::GetItem( wxListItem
& item
)
1085 item
.m_mask
= m_mask
;
1086 item
.m_text
= m_text
;
1087 item
.m_image
= m_image
;
1088 item
.m_format
= m_format
;
1089 item
.m_width
= m_width
;
1090 item
.m_state
= m_state
;
1093 int wxListHeaderData::GetImage() const
1098 int wxListHeaderData::GetWidth() const
1103 int wxListHeaderData::GetFormat() const
1108 int wxListHeaderData::GetState() const
1113 //-----------------------------------------------------------------------------
1115 //-----------------------------------------------------------------------------
1117 inline int wxListLineData::GetMode() const
1119 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1122 inline bool wxListLineData::InReportView() const
1124 return m_owner
->HasFlag(wxLC_REPORT
);
1127 inline bool wxListLineData::IsVirtual() const
1129 return m_owner
->IsVirtual();
1132 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1136 if ( InReportView() )
1139 m_gi
= new GeometryInfo
;
1141 m_highlighted
= false;
1143 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1146 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1148 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1149 wxCHECK_RET( node
, _T("no subitems at all??") );
1151 wxListItemData
*item
= node
->GetData();
1156 switch ( GetMode() )
1159 case wxLC_SMALL_ICON
:
1160 m_gi
->m_rectAll
.width
= spacing
;
1162 s
= item
->GetText();
1167 m_gi
->m_rectLabel
.width
=
1168 m_gi
->m_rectLabel
.height
= 0;
1172 dc
->GetTextExtent( s
, &lw
, &lh
);
1176 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1178 m_gi
->m_rectAll
.width
= lw
;
1180 m_gi
->m_rectLabel
.width
= lw
;
1181 m_gi
->m_rectLabel
.height
= lh
;
1184 if (item
->HasImage())
1187 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1188 m_gi
->m_rectIcon
.width
= w
+ 8;
1189 m_gi
->m_rectIcon
.height
= h
+ 8;
1191 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1192 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1193 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1194 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1197 if ( item
->HasText() )
1199 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1200 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1202 else // no text, highlight the icon
1204 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1205 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1210 s
= item
->GetTextForMeasuring();
1212 dc
->GetTextExtent( s
, &lw
, &lh
);
1216 m_gi
->m_rectLabel
.width
= lw
;
1217 m_gi
->m_rectLabel
.height
= lh
;
1219 m_gi
->m_rectAll
.width
= lw
;
1220 m_gi
->m_rectAll
.height
= lh
;
1222 if (item
->HasImage())
1225 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1226 m_gi
->m_rectIcon
.width
= w
;
1227 m_gi
->m_rectIcon
.height
= h
;
1229 m_gi
->m_rectAll
.width
+= 4 + w
;
1230 if (h
> m_gi
->m_rectAll
.height
)
1231 m_gi
->m_rectAll
.height
= h
;
1234 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1235 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1239 wxFAIL_MSG( _T("unexpected call to SetSize") );
1243 wxFAIL_MSG( _T("unknown mode") );
1248 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1250 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1251 wxCHECK_RET( node
, _T("no subitems at all??") );
1253 wxListItemData
*item
= node
->GetData();
1255 switch ( GetMode() )
1258 case wxLC_SMALL_ICON
:
1259 m_gi
->m_rectAll
.x
= x
;
1260 m_gi
->m_rectAll
.y
= y
;
1262 if ( item
->HasImage() )
1264 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1265 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1266 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1269 if ( item
->HasText() )
1271 if (m_gi
->m_rectAll
.width
> spacing
)
1272 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1274 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2) + (spacing
/ 2) - (m_gi
->m_rectLabel
.width
/ 2);
1275 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1276 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1277 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1279 else // no text, highlight the icon
1281 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1282 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1287 m_gi
->m_rectAll
.x
= x
;
1288 m_gi
->m_rectAll
.y
= y
;
1290 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1291 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1292 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1294 if (item
->HasImage())
1296 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1297 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1298 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 4 + (EXTRA_WIDTH
/2) + m_gi
->m_rectIcon
.width
;
1302 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ (EXTRA_WIDTH
/2);
1307 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1311 wxFAIL_MSG( _T("unknown mode") );
1316 void wxListLineData::InitItems( int num
)
1318 for (int i
= 0; i
< num
; i
++)
1319 m_items
.Append( new wxListItemData(m_owner
) );
1322 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1324 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1325 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1327 wxListItemData
*item
= node
->GetData();
1328 item
->SetItem( info
);
1331 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1333 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1336 wxListItemData
*item
= node
->GetData();
1337 item
->GetItem( info
);
1341 wxString
wxListLineData::GetText(int index
) const
1345 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1348 wxListItemData
*item
= node
->GetData();
1349 s
= item
->GetText();
1355 void wxListLineData::SetText( int index
, const wxString
& s
)
1357 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1360 wxListItemData
*item
= node
->GetData();
1365 void wxListLineData::SetImage( int index
, int image
)
1367 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1368 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1370 wxListItemData
*item
= node
->GetData();
1371 item
->SetImage(image
);
1374 int wxListLineData::GetImage( int index
) const
1376 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1377 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1379 wxListItemData
*item
= node
->GetData();
1380 return item
->GetImage();
1383 wxListItemAttr
*wxListLineData::GetAttr() const
1385 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1386 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1388 wxListItemData
*item
= node
->GetData();
1389 return item
->GetAttr();
1392 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1394 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1395 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1397 wxListItemData
*item
= node
->GetData();
1398 item
->SetAttr(attr
);
1401 bool wxListLineData::SetAttributes(wxDC
*dc
,
1402 const wxListItemAttr
*attr
,
1405 wxWindow
*listctrl
= m_owner
->GetParent();
1409 // don't use foreground colour for drawing highlighted items - this might
1410 // make them completely invisible (and there is no way to do bit
1411 // arithmetics on wxColour, unfortunately)
1416 if (m_owner
->HasFocus()
1418 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1426 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1428 else if ( attr
&& attr
->HasTextColour() )
1429 colText
= attr
->GetTextColour();
1431 colText
= listctrl
->GetForegroundColour();
1433 dc
->SetTextForeground(colText
);
1437 if ( attr
&& attr
->HasFont() )
1438 font
= attr
->GetFont();
1440 font
= listctrl
->GetFont();
1445 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1446 if ( highlighted
|| hasBgCol
)
1449 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1451 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1453 dc
->SetPen( *wxTRANSPARENT_PEN
);
1461 void wxListLineData::Draw( wxDC
*dc
)
1463 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1464 wxCHECK_RET( node
, _T("no subitems at all??") );
1466 bool highlighted
= IsHighlighted();
1468 wxListItemAttr
*attr
= GetAttr();
1470 if ( SetAttributes(dc
, attr
, highlighted
) )
1471 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1473 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1479 int flags
= wxCONTROL_SELECTED
;
1480 if (m_owner
->HasFocus()
1482 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1485 flags
|= wxCONTROL_FOCUSED
;
1486 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, m_gi
->m_rectHighlight
, flags
);
1491 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1496 // just for debugging to better see where the items are
1498 dc
->SetPen(*wxRED_PEN
);
1499 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1500 dc
->DrawRectangle( m_gi
->m_rectAll
);
1501 dc
->SetPen(*wxGREEN_PEN
);
1502 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1505 wxListItemData
*item
= node
->GetData();
1506 if (item
->HasImage())
1508 // centre the image inside our rectangle, this looks nicer when items
1509 // ae aligned in a row
1510 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1512 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1515 if (item
->HasText())
1517 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1519 wxDCClipper
clipper(*dc
, rectLabel
);
1520 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1524 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1526 const wxRect
& rectHL
,
1529 // TODO: later we should support setting different attributes for
1530 // different columns - to do it, just add "col" argument to
1531 // GetAttr() and move these lines into the loop below
1532 wxListItemAttr
*attr
= GetAttr();
1533 if ( SetAttributes(dc
, attr
, highlighted
) )
1534 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1536 dc
->DrawRectangle( rectHL
);
1542 int flags
= wxCONTROL_SELECTED
;
1543 if (m_owner
->HasFocus()
1545 && IsControlActive( (ControlRef
)m_owner
->GetHandle() )
1548 flags
|= wxCONTROL_FOCUSED
;
1549 wxRendererNative::Get().DrawItemSelectionRect( m_owner
, *dc
, rectHL
, flags
);
1553 dc
->DrawRectangle( rectHL
);
1558 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1559 yMid
= rect
.y
+ rect
.height
/2;
1561 // This probably needs to be done
1562 // on all platforms as the icons
1563 // otherwise nearly touch the border
1568 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1570 node
= node
->GetNext(), col
++ )
1572 wxListItemData
*item
= node
->GetData();
1574 int width
= m_owner
->GetColumnWidth(col
);
1578 if ( item
->HasImage() )
1581 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1582 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, yMid
- iy
/2 );
1584 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1590 if ( item
->HasText() )
1591 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, yMid
, width
- 8);
1595 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1596 const wxString
& textOrig
,
1602 // we don't support displaying multiple lines currently (and neither does
1603 // wxMSW FWIW) so just merge all the lines
1604 wxString
text(textOrig
);
1605 text
.Replace(_T("\n"), _T(" "));
1608 dc
->GetTextExtent(text
, &w
, &h
);
1610 const wxCoord y
= yMid
- (h
+ 1)/2;
1612 wxDCClipper
clipper(*dc
, x
, y
, width
, h
);
1614 // determine if the string can fit inside the current width
1617 // it can, draw it using the items alignment
1619 m_owner
->GetColumn(col
, item
);
1620 switch ( item
.GetAlign() )
1622 case wxLIST_FORMAT_LEFT
:
1626 case wxLIST_FORMAT_RIGHT
:
1630 case wxLIST_FORMAT_CENTER
:
1631 x
+= (width
- w
) / 2;
1635 wxFAIL_MSG( _T("unknown list item format") );
1639 dc
->DrawText(text
, x
, y
);
1641 else // otherwise, truncate and add an ellipsis if possible
1643 // determine the base width
1644 wxString
ellipsis(wxT("..."));
1646 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1648 // continue until we have enough space or only one character left
1650 size_t len
= text
.length();
1651 wxString drawntext
= text
.Left(len
);
1654 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1655 drawntext
.RemoveLast();
1658 if (w
+ base_w
<= width
)
1662 // if still not enough space, remove ellipsis characters
1663 while (ellipsis
.length() > 0 && w
+ base_w
> width
)
1665 ellipsis
= ellipsis
.Left(ellipsis
.length() - 1);
1666 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1669 // now draw the text
1670 dc
->DrawText(drawntext
, x
, y
);
1671 dc
->DrawText(ellipsis
, x
+ w
, y
);
1675 bool wxListLineData::Highlight( bool on
)
1677 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1679 if ( on
== m_highlighted
)
1687 void wxListLineData::ReverseHighlight( void )
1689 Highlight(!IsHighlighted());
1692 //-----------------------------------------------------------------------------
1693 // wxListHeaderWindow
1694 //-----------------------------------------------------------------------------
1696 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1698 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1699 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1700 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1701 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1704 void wxListHeaderWindow::Init()
1706 m_currentCursor
= (wxCursor
*) NULL
;
1707 m_isDragging
= false;
1711 wxListHeaderWindow::wxListHeaderWindow()
1715 m_owner
= (wxListMainWindow
*) NULL
;
1716 m_resizeCursor
= (wxCursor
*) NULL
;
1719 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1721 wxListMainWindow
*owner
,
1725 const wxString
&name
)
1726 : wxWindow( win
, id
, pos
, size
, style
, name
)
1731 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1734 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1735 SetOwnForegroundColour( attr
.colFg
);
1736 SetOwnBackgroundColour( attr
.colBg
);
1738 SetOwnFont( attr
.font
);
1740 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1741 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1743 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1747 wxListHeaderWindow::~wxListHeaderWindow()
1749 delete m_resizeCursor
;
1752 #ifdef __WXUNIVERSAL__
1753 #include "wx/univ/renderer.h"
1754 #include "wx/univ/theme.h"
1757 // shift the DC origin to match the position of the main window horz
1758 // scrollbar: this allows us to always use logical coords
1759 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1762 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1765 m_owner
->GetViewStart( &view_start
, NULL
);
1770 dc
.GetDeviceOrigin( &org_x
, &org_y
);
1772 // account for the horz scrollbar offset
1774 if (GetLayoutDirection() == wxLayout_RightToLeft
)
1776 // Maybe we just have to check for m_signX
1777 // in the DC, but I leave the #ifdef __WXGTK__
1779 dc
.SetDeviceOrigin( org_x
+ (view_start
* xpix
), org_y
);
1783 dc
.SetDeviceOrigin( org_x
- (view_start
* xpix
), org_y
);
1786 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1788 wxPaintDC
dc( this );
1793 dc
.SetFont( GetFont() );
1795 // width and height of the entire header window
1797 GetClientSize( &w
, &h
);
1798 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1800 dc
.SetBackgroundMode(wxTRANSPARENT
);
1801 dc
.SetTextForeground(GetForegroundColour());
1803 int x
= HEADER_OFFSET_X
;
1804 int numColumns
= m_owner
->GetColumnCount();
1806 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1808 m_owner
->GetColumn( i
, item
);
1809 int wCol
= item
.m_width
;
1811 // the width of the rect to draw: make it smaller to fit entirely
1812 // inside the column rect
1822 if (!m_parent
->IsEnabled())
1823 flags
|= wxCONTROL_DISABLED
;
1825 // NB: The code below is not really Mac-specific, but since we are close
1826 // to 2.8 release and I don't have time to test on other platforms, I
1827 // defined this only for wxMac. If this behavior is desired on
1828 // other platforms, please go ahead and revise or remove the #ifdef.
1830 if ( !m_owner
->IsVirtual() && (item
.m_mask
& wxLIST_MASK_STATE
) &&
1831 (item
.m_state
& wxLIST_STATE_SELECTED
) )
1832 flags
|= wxCONTROL_SELECTED
;
1835 wxRendererNative::Get().DrawHeaderButton
1839 wxRect(x
, HEADER_OFFSET_Y
, cw
, ch
),
1843 // see if we have enough space for the column label
1845 // for this we need the width of the text
1848 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1849 wLabel
+= 2 * EXTRA_WIDTH
;
1851 // and the width of the icon, if any
1852 int ix
= 0, iy
= 0; // init them just to suppress the compiler warnings
1853 const int image
= item
.m_image
;
1854 wxImageList
*imageList
;
1857 imageList
= m_owner
->m_small_image_list
;
1860 imageList
->GetSize(image
, ix
, iy
);
1861 wLabel
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
1869 // ignore alignment if there is not enough space anyhow
1871 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1874 wxFAIL_MSG( _T("unknown list item format") );
1877 case wxLIST_FORMAT_LEFT
:
1881 case wxLIST_FORMAT_RIGHT
:
1882 xAligned
= x
+ cw
- wLabel
;
1885 case wxLIST_FORMAT_CENTER
:
1886 xAligned
= x
+ (cw
- wLabel
) / 2;
1890 // draw the text and image clipping them so that they
1891 // don't overwrite the column boundary
1892 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1894 // if we have an image, draw it on the right of the label
1901 xAligned
+ wLabel
- ix
- HEADER_IMAGE_MARGIN_IN_REPORT_MODE
,
1902 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1903 wxIMAGELIST_DRAW_TRANSPARENT
1907 dc
.DrawText( item
.GetText(),
1908 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1914 void wxListHeaderWindow::DrawCurrent()
1916 int x1
= m_currentX
;
1918 m_owner
->ClientToScreen( &x1
, &y1
);
1920 int x2
= m_currentX
;
1922 m_owner
->GetClientSize( NULL
, &y2
);
1923 m_owner
->ClientToScreen( &x2
, &y2
);
1926 dc
.SetLogicalFunction( wxINVERT
);
1927 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1928 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1932 dc
.DrawLine( x1
, y1
, x2
, y2
);
1934 dc
.SetLogicalFunction( wxCOPY
);
1936 dc
.SetPen( wxNullPen
);
1937 dc
.SetBrush( wxNullBrush
);
1940 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1942 // we want to work with logical coords
1944 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1945 int y
= event
.GetY();
1949 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1951 // we don't draw the line beyond our window, but we allow dragging it
1954 GetClientSize( &w
, NULL
);
1955 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1958 // erase the line if it was drawn
1959 if ( m_currentX
< w
)
1962 if (event
.ButtonUp())
1965 m_isDragging
= false;
1967 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1968 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1975 m_currentX
= m_minX
+ 7;
1977 // draw in the new location
1978 if ( m_currentX
< w
)
1982 else // not dragging
1985 bool hit_border
= false;
1987 // end of the current column
1990 // find the column where this event occurred
1992 countCol
= m_owner
->GetColumnCount();
1993 for (col
= 0; col
< countCol
; col
++)
1995 xpos
+= m_owner
->GetColumnWidth( col
);
1998 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2000 // near the column border
2007 // inside the column
2014 if ( col
== countCol
)
2017 if (event
.LeftDown() || event
.RightUp())
2019 if (hit_border
&& event
.LeftDown())
2021 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2022 event
.GetPosition()) )
2024 m_isDragging
= true;
2029 //else: column resizing was vetoed by the user code
2031 else // click on a column
2033 // record the selected state of the columns
2034 if (event
.LeftDown())
2036 for (int i
=0; i
< m_owner
->GetColumnCount(); i
++)
2039 m_owner
->GetColumn(i
, colItem
);
2040 long state
= colItem
.GetState();
2042 colItem
.SetState(state
| wxLIST_STATE_SELECTED
);
2044 colItem
.SetState(state
& ~wxLIST_STATE_SELECTED
);
2045 m_owner
->SetColumn(i
, colItem
);
2049 SendListEvent( event
.LeftDown()
2050 ? wxEVT_COMMAND_LIST_COL_CLICK
2051 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2052 event
.GetPosition());
2055 else if (event
.Moving())
2060 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2061 m_currentCursor
= m_resizeCursor
;
2065 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2066 m_currentCursor
= wxSTANDARD_CURSOR
;
2070 SetCursor(*m_currentCursor
);
2075 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2077 m_owner
->SetFocus();
2081 bool wxListHeaderWindow::SendListEvent(wxEventType type
, const wxPoint
& pos
)
2083 wxWindow
*parent
= GetParent();
2084 wxListEvent
le( type
, parent
->GetId() );
2085 le
.SetEventObject( parent
);
2086 le
.m_pointDrag
= pos
;
2088 // the position should be relative to the parent window, not
2089 // this one for compatibility with MSW and common sense: the
2090 // user code doesn't know anything at all about this header
2091 // window, so why should it get positions relative to it?
2092 le
.m_pointDrag
.y
-= GetSize().y
;
2094 le
.m_col
= m_column
;
2095 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2098 //-----------------------------------------------------------------------------
2099 // wxListRenameTimer (internal)
2100 //-----------------------------------------------------------------------------
2102 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2107 void wxListRenameTimer::Notify()
2109 m_owner
->OnRenameTimer();
2112 //-----------------------------------------------------------------------------
2113 // wxListTextCtrlWrapper (internal)
2114 //-----------------------------------------------------------------------------
2116 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper
, wxEvtHandler
)
2117 EVT_CHAR (wxListTextCtrlWrapper::OnChar
)
2118 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp
)
2119 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus
)
2122 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow
*owner
,
2125 : m_startValue(owner
->GetItemText(itemEdit
)),
2126 m_itemEdited(itemEdit
)
2131 m_aboutToFinish
= false;
2133 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2135 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2136 &rectLabel
.x
, &rectLabel
.y
);
2138 m_text
->Create(owner
, wxID_ANY
, m_startValue
,
2139 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2140 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2143 m_text
->PushEventHandler(this);
2146 void wxListTextCtrlWrapper::Finish()
2152 m_text
->RemoveEventHandler(this);
2153 m_owner
->FinishEditing(m_text
);
2155 wxPendingDelete
.Append( this );
2159 bool wxListTextCtrlWrapper::AcceptChanges()
2161 const wxString value
= m_text
->GetValue();
2163 // notice that we should always call OnRenameAccept() to generate the "end
2164 // label editing" event, even if the user hasn't really changed anything
2165 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2167 // vetoed by the user
2171 // accepted, do rename the item (unless nothing changed)
2172 if ( value
!= m_startValue
)
2173 m_owner
->SetItemText(m_itemEdited
, value
);
2178 void wxListTextCtrlWrapper::AcceptChangesAndFinish()
2180 m_aboutToFinish
= true;
2182 // Notify the owner about the changes
2185 // Even if vetoed, close the control (consistent with MSW)
2189 void wxListTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
2191 switch ( event
.m_keyCode
)
2194 AcceptChangesAndFinish();
2198 m_owner
->OnRenameCancelled( m_itemEdited
);
2207 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
2215 // auto-grow the textctrl:
2216 wxSize parentSize
= m_owner
->GetSize();
2217 wxPoint myPos
= m_text
->GetPosition();
2218 wxSize mySize
= m_text
->GetSize();
2220 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
2221 if (myPos
.x
+ sx
> parentSize
.x
)
2222 sx
= parentSize
.x
- myPos
.x
;
2225 m_text
->SetSize(sx
, wxDefaultCoord
);
2230 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
2232 if ( !m_finished
&& !m_aboutToFinish
)
2234 if ( !AcceptChanges() )
2235 m_owner
->OnRenameCancelled( m_itemEdited
);
2240 // We must let the native text control handle focus
2244 //-----------------------------------------------------------------------------
2246 //-----------------------------------------------------------------------------
2248 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2250 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2251 EVT_PAINT (wxListMainWindow::OnPaint
)
2252 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2253 EVT_CHAR (wxListMainWindow::OnChar
)
2254 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2255 EVT_KEY_UP (wxListMainWindow::OnKeyUp
)
2256 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2257 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2258 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2261 void wxListMainWindow::Init()
2266 m_lineTo
= (size_t)-1;
2272 m_small_image_list
= (wxImageList
*) NULL
;
2273 m_normal_image_list
= (wxImageList
*) NULL
;
2275 m_small_spacing
= 30;
2276 m_normal_spacing
= 40;
2280 m_isCreated
= false;
2282 m_lastOnSame
= false;
2283 m_renameTimer
= new wxListRenameTimer( this );
2284 m_textctrlWrapper
= NULL
;
2288 m_lineSelectSingleOnUp
=
2289 m_lineBeforeLastClicked
= (size_t)-1;
2294 wxListMainWindow::wxListMainWindow()
2299 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2302 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2307 const wxString
&name
)
2308 : wxScrolledWindow( parent
, id
, pos
, size
,
2309 style
| wxHSCROLL
| wxVSCROLL
, name
)
2313 m_highlightBrush
= new wxBrush
2315 wxSystemSettings::GetColour
2317 wxSYS_COLOUR_HIGHLIGHT
2322 m_highlightUnfocusedBrush
= new wxBrush
2324 wxSystemSettings::GetColour
2326 wxSYS_COLOUR_BTNSHADOW
2331 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2333 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2334 SetOwnForegroundColour( attr
.colFg
);
2335 SetOwnBackgroundColour( attr
.colBg
);
2337 SetOwnFont( attr
.font
);
2340 wxListMainWindow::~wxListMainWindow()
2343 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2344 WX_CLEAR_ARRAY(m_aColWidths
);
2346 delete m_highlightBrush
;
2347 delete m_highlightUnfocusedBrush
;
2348 delete m_renameTimer
;
2351 void wxListMainWindow::CacheLineData(size_t line
)
2353 wxGenericListCtrl
*listctrl
= GetListCtrl();
2355 wxListLineData
*ld
= GetDummyLine();
2357 size_t countCol
= GetColumnCount();
2358 for ( size_t col
= 0; col
< countCol
; col
++ )
2360 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2361 ld
->SetImage(col
, listctrl
->OnGetItemColumnImage(line
, col
));
2364 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2367 wxListLineData
*wxListMainWindow::GetDummyLine() const
2369 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2370 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2372 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2374 // we need to recreate the dummy line if the number of columns in the
2375 // control changed as it would have the incorrect number of fields
2377 if ( !m_lines
.IsEmpty() &&
2378 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2380 self
->m_lines
.Clear();
2383 if ( m_lines
.IsEmpty() )
2385 wxListLineData
*line
= new wxListLineData(self
);
2386 self
->m_lines
.Add(line
);
2388 // don't waste extra memory -- there never going to be anything
2389 // else/more in this array
2390 self
->m_lines
.Shrink();
2396 // ----------------------------------------------------------------------------
2397 // line geometry (report mode only)
2398 // ----------------------------------------------------------------------------
2400 wxCoord
wxListMainWindow::GetLineHeight() const
2402 // we cache the line height as calling GetTextExtent() is slow
2403 if ( !m_lineHeight
)
2405 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2407 wxClientDC
dc( self
);
2408 dc
.SetFont( GetFont() );
2411 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2413 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2416 m_small_image_list
->GetSize(0, iw
, ih
);
2421 self
->m_lineHeight
= y
+ LINE_SPACING
;
2424 return m_lineHeight
;
2427 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2429 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2431 return LINE_SPACING
+ line
* GetLineHeight();
2434 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2436 if ( !InReportView() )
2437 return GetLine(line
)->m_gi
->m_rectAll
;
2440 rect
.x
= HEADER_OFFSET_X
;
2441 rect
.y
= GetLineY(line
);
2442 rect
.width
= GetHeaderWidth();
2443 rect
.height
= GetLineHeight();
2448 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2450 if ( !InReportView() )
2451 return GetLine(line
)->m_gi
->m_rectLabel
;
2454 wxListLineData
*data
= GetLine(line
);
2455 wxListItemDataList::compatibility_iterator node
= data
->m_items
.GetFirst();
2458 wxListItemData
*item
= node
->GetData();
2459 if ( item
->HasImage() )
2462 GetImageSize( item
->GetImage(), ix
, iy
);
2463 image_x
= 3 + ix
+ IMAGE_MARGIN_IN_REPORT_MODE
;
2468 rect
.x
= HEADER_OFFSET_X
;
2469 rect
.y
= GetLineY(line
);
2470 rect
.width
= GetColumnWidth(0);
2471 rect
.height
= GetLineHeight();
2476 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2478 if ( !InReportView() )
2479 return GetLine(line
)->m_gi
->m_rectIcon
;
2481 wxListLineData
*ld
= GetLine(line
);
2482 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2485 rect
.x
= HEADER_OFFSET_X
;
2486 rect
.y
= GetLineY(line
);
2487 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2492 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2494 return InReportView() ? GetLineRect(line
)
2495 : GetLine(line
)->m_gi
->m_rectHighlight
;
2498 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2500 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2502 wxListLineData
*ld
= GetLine(line
);
2504 if ( ld
->HasImage() && GetLineIconRect(line
).Contains(x
, y
) )
2505 return wxLIST_HITTEST_ONITEMICON
;
2507 // VS: Testing for "ld->HasText() || InReportView()" instead of
2508 // "ld->HasText()" is needed to make empty lines in report view
2510 if ( ld
->HasText() || InReportView() )
2512 wxRect rect
= InReportView() ? GetLineRect(line
)
2513 : GetLineLabelRect(line
);
2515 if ( rect
.Contains(x
, y
) )
2516 return wxLIST_HITTEST_ONITEMLABEL
;
2522 // ----------------------------------------------------------------------------
2523 // highlight (selection) handling
2524 // ----------------------------------------------------------------------------
2526 bool wxListMainWindow::IsHighlighted(size_t line
) const
2530 return m_selStore
.IsSelected(line
);
2534 wxListLineData
*ld
= GetLine(line
);
2535 wxCHECK_MSG( ld
, false, _T("invalid index in IsHighlighted") );
2537 return ld
->IsHighlighted();
2541 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2547 wxArrayInt linesChanged
;
2548 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2551 // meny items changed state, refresh everything
2552 RefreshLines(lineFrom
, lineTo
);
2554 else // only a few items changed state, refresh only them
2556 size_t count
= linesChanged
.GetCount();
2557 for ( size_t n
= 0; n
< count
; n
++ )
2559 RefreshLine(linesChanged
[n
]);
2563 else // iterate over all items in non report view
2565 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2567 if ( HighlightLine(line
, highlight
) )
2573 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2579 changed
= m_selStore
.SelectItem(line
, highlight
);
2583 wxListLineData
*ld
= GetLine(line
);
2584 wxCHECK_MSG( ld
, false, _T("invalid index in HighlightLine") );
2586 changed
= ld
->Highlight(highlight
);
2591 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2592 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2598 void wxListMainWindow::RefreshLine( size_t line
)
2600 if ( InReportView() )
2602 size_t visibleFrom
, visibleTo
;
2603 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2605 if ( line
< visibleFrom
|| line
> visibleTo
)
2609 wxRect rect
= GetLineRect(line
);
2611 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2612 RefreshRect( rect
);
2615 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2617 // we suppose that they are ordered by caller
2618 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2620 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2622 if ( InReportView() )
2624 size_t visibleFrom
, visibleTo
;
2625 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2627 if ( lineFrom
< visibleFrom
)
2628 lineFrom
= visibleFrom
;
2629 if ( lineTo
> visibleTo
)
2634 rect
.y
= GetLineY(lineFrom
);
2635 rect
.width
= GetClientSize().x
;
2636 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2638 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2639 RefreshRect( rect
);
2643 // TODO: this should be optimized...
2644 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2651 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2653 if ( InReportView() )
2655 size_t visibleFrom
, visibleTo
;
2656 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2658 if ( lineFrom
< visibleFrom
)
2659 lineFrom
= visibleFrom
;
2660 else if ( lineFrom
> visibleTo
)
2665 rect
.y
= GetLineY(lineFrom
);
2666 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2668 wxSize size
= GetClientSize();
2669 rect
.width
= size
.x
;
2671 // refresh till the bottom of the window
2672 rect
.height
= size
.y
- rect
.y
;
2674 RefreshRect( rect
);
2678 // TODO: how to do it more efficiently?
2683 void wxListMainWindow::RefreshSelected()
2689 if ( InReportView() )
2691 GetVisibleLinesRange(&from
, &to
);
2696 to
= GetItemCount() - 1;
2699 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2700 RefreshLine(m_current
);
2702 for ( size_t line
= from
; line
<= to
; line
++ )
2704 // NB: the test works as expected even if m_current == -1
2705 if ( line
!= m_current
&& IsHighlighted(line
) )
2710 void wxListMainWindow::Freeze()
2715 void wxListMainWindow::Thaw()
2717 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2719 if ( --m_freezeCount
== 0 )
2723 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2725 // Note: a wxPaintDC must be constructed even if no drawing is
2726 // done (a Windows requirement).
2727 wxPaintDC
dc( this );
2729 if ( IsEmpty() || m_freezeCount
)
2730 // nothing to draw or not the moment to draw it
2734 // delay the repainting until we calculate all the items positions
2740 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2742 dc
.SetFont( GetFont() );
2744 if ( InReportView() )
2746 int lineHeight
= GetLineHeight();
2748 size_t visibleFrom
, visibleTo
;
2749 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2752 int xOrig
= dc
.LogicalToDeviceX( 0 );
2753 int yOrig
= dc
.LogicalToDeviceY( 0 );
2755 // tell the caller cache to cache the data
2758 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2759 GetParent()->GetId());
2760 evCache
.SetEventObject( GetParent() );
2761 evCache
.m_oldItemIndex
= visibleFrom
;
2762 evCache
.m_itemIndex
= visibleTo
;
2763 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2766 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2768 rectLine
= GetLineRect(line
);
2771 if ( !IsExposed(rectLine
.x
+ xOrig
, rectLine
.y
+ yOrig
,
2772 rectLine
.width
, rectLine
.height
) )
2774 // don't redraw unaffected lines to avoid flicker
2778 GetLine(line
)->DrawInReportMode( &dc
,
2780 GetLineHighlightRect(line
),
2781 IsHighlighted(line
) );
2784 if ( HasFlag(wxLC_HRULES
) )
2786 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2787 wxSize clientSize
= GetClientSize();
2789 size_t i
= visibleFrom
;
2790 if (i
== 0) i
= 1; // Don't draw the first one
2791 for ( ; i
<= visibleTo
; i
++ )
2794 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2795 dc
.DrawLine(0 - dev_x
, i
* lineHeight
,
2796 clientSize
.x
- dev_x
, i
* lineHeight
);
2799 // Draw last horizontal rule
2800 if ( visibleTo
== GetItemCount() - 1 )
2803 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2804 dc
.DrawLine(0 - dev_x
, (m_lineTo
+ 1) * lineHeight
,
2805 clientSize
.x
- dev_x
, (m_lineTo
+ 1) * lineHeight
);
2809 // Draw vertical rules if required
2810 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2812 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2813 wxRect firstItemRect
, lastItemRect
;
2815 GetItemRect(visibleFrom
, firstItemRect
);
2816 GetItemRect(visibleTo
, lastItemRect
);
2817 int x
= firstItemRect
.GetX();
2819 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2821 for (int col
= 0; col
< GetColumnCount(); col
++)
2823 int colWidth
= GetColumnWidth(col
);
2825 int x_pos
= x
- dev_x
;
2826 if (col
< GetColumnCount()-1) x_pos
-= 2;
2827 dc
.DrawLine(x_pos
, firstItemRect
.GetY() - 1 - dev_y
,
2828 x_pos
, lastItemRect
.GetBottom() + 1 - dev_y
);
2834 size_t count
= GetItemCount();
2835 for ( size_t i
= 0; i
< count
; i
++ )
2837 GetLine(i
)->Draw( &dc
);
2842 // Don't draw rect outline under Mac at all.
2847 wxRect
rect( GetLineHighlightRect( m_current
) );
2849 dc
.SetPen( *wxBLACK_PEN
);
2850 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2851 dc
.DrawRectangle( rect
);
2853 wxRendererNative::Get().DrawItemSelectionRect( this, dc
, rect
, wxCONTROL_CURRENT
|wxCONTROL_FOCUSED
);
2861 void wxListMainWindow::HighlightAll( bool on
)
2863 if ( IsSingleSel() )
2865 wxASSERT_MSG( !on
, _T("can't do this in a single selection control") );
2867 // we just have one item to turn off
2868 if ( HasCurrent() && IsHighlighted(m_current
) )
2870 HighlightLine(m_current
, false);
2871 RefreshLine(m_current
);
2876 HighlightLines(0, GetItemCount() - 1, on
);
2880 void wxListMainWindow::SendNotify( size_t line
,
2881 wxEventType command
,
2882 const wxPoint
& point
)
2884 wxListEvent
le( command
, GetParent()->GetId() );
2885 le
.SetEventObject( GetParent() );
2887 le
.m_itemIndex
= line
;
2889 // set only for events which have position
2890 if ( point
!= wxDefaultPosition
)
2891 le
.m_pointDrag
= point
;
2893 // don't try to get the line info for virtual list controls: the main
2894 // program has it anyhow and if we did it would result in accessing all
2895 // the lines, even those which are not visible now and this is precisely
2896 // what we're trying to avoid
2899 if ( line
!= (size_t)-1 )
2901 GetLine(line
)->GetItem( 0, le
.m_item
);
2903 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2905 //else: there may be no more such item
2907 GetParent()->GetEventHandler()->ProcessEvent( le
);
2910 void wxListMainWindow::ChangeCurrent(size_t current
)
2912 m_current
= current
;
2914 // as the current item changed, we shouldn't start editing it when the
2915 // "slow click" timer expires as the click happened on another item
2916 if ( m_renameTimer
->IsRunning() )
2917 m_renameTimer
->Stop();
2919 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2922 wxTextCtrl
*wxListMainWindow::EditLabel(long item
, wxClassInfo
* textControlClass
)
2924 wxCHECK_MSG( (item
>= 0) && ((size_t)item
< GetItemCount()), NULL
,
2925 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2927 wxASSERT_MSG( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)),
2928 wxT("EditLabel() needs a text control") );
2930 size_t itemEdit
= (size_t)item
;
2932 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2933 le
.SetEventObject( GetParent() );
2934 le
.m_itemIndex
= item
;
2935 wxListLineData
*data
= GetLine(itemEdit
);
2936 wxCHECK_MSG( data
, NULL
, _T("invalid index in EditLabel()") );
2937 data
->GetItem( 0, le
.m_item
);
2939 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2941 // vetoed by user code
2945 // We have to call this here because the label in question might just have
2946 // been added and no screen update taken place.
2951 // Pending events dispatched by wxSafeYield might have changed the item
2953 if ( (size_t)item
>= GetItemCount() )
2957 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
2958 m_textctrlWrapper
= new wxListTextCtrlWrapper(this, text
, item
);
2959 return m_textctrlWrapper
->GetText();
2962 void wxListMainWindow::OnRenameTimer()
2964 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2966 EditLabel( m_current
);
2969 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2971 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2972 le
.SetEventObject( GetParent() );
2973 le
.m_itemIndex
= itemEdit
;
2975 wxListLineData
*data
= GetLine(itemEdit
);
2977 wxCHECK_MSG( data
, false, _T("invalid index in OnRenameAccept()") );
2979 data
->GetItem( 0, le
.m_item
);
2980 le
.m_item
.m_text
= value
;
2981 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2985 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2987 // let owner know that the edit was cancelled
2988 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2990 le
.SetEditCanceled(true);
2992 le
.SetEventObject( GetParent() );
2993 le
.m_itemIndex
= itemEdit
;
2995 wxListLineData
*data
= GetLine(itemEdit
);
2996 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2998 data
->GetItem( 0, le
.m_item
);
2999 GetEventHandler()->ProcessEvent( le
);
3002 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
3006 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
3007 // shutdown the edit control when the mouse is clicked elsewhere on the
3008 // listctrl because the order of events is different (or something like
3009 // that), so explicitly end the edit if it is active.
3010 if ( event
.LeftDown() && m_textctrlWrapper
)
3011 m_textctrlWrapper
->AcceptChangesAndFinish();
3014 if ( event
.LeftDown() )
3017 event
.SetEventObject( GetParent() );
3018 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3021 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3023 // let the base handle mouse wheel events.
3028 if ( !HasCurrent() || IsEmpty() )
3030 if (event
.RightDown())
3032 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3033 // Allow generation of context menu event
3042 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3043 event
.ButtonDClick()) )
3046 int x
= event
.GetX();
3047 int y
= event
.GetY();
3048 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3050 // where did we hit it (if we did)?
3053 size_t count
= GetItemCount(),
3056 if ( InReportView() )
3058 current
= y
/ GetLineHeight();
3059 if ( current
< count
)
3060 hitResult
= HitTestLine(current
, x
, y
);
3064 // TODO: optimize it too! this is less simple than for report view but
3065 // enumerating all items is still not a way to do it!!
3066 for ( current
= 0; current
< count
; current
++ )
3068 hitResult
= HitTestLine(current
, x
, y
);
3074 if (event
.Dragging())
3076 if (m_dragCount
== 0)
3078 // we have to report the raw, physical coords as we want to be
3079 // able to call HitTest(event.m_pointDrag) from the user code to
3080 // get the item being dragged
3081 m_dragStart
= event
.GetPosition();
3086 if (m_dragCount
!= 3)
3089 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3090 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3092 wxListEvent
le( command
, GetParent()->GetId() );
3093 le
.SetEventObject( GetParent() );
3094 le
.m_itemIndex
= m_lineLastClicked
;
3095 le
.m_pointDrag
= m_dragStart
;
3096 GetParent()->GetEventHandler()->ProcessEvent( le
);
3107 // outside of any item
3108 if (event
.RightDown())
3110 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3111 // Allow generation of context menu event
3116 // reset the selection and bail out
3117 HighlightAll(false);
3123 bool forceClick
= false;
3124 if (event
.ButtonDClick())
3126 if ( m_renameTimer
->IsRunning() )
3127 m_renameTimer
->Stop();
3129 m_lastOnSame
= false;
3131 if ( current
== m_lineLastClicked
)
3133 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3139 // The first click was on another item, so don't interpret this as
3140 // a double click, but as a simple click instead
3147 if (m_lineSelectSingleOnUp
!= (size_t)-1)
3149 // select single line
3150 HighlightAll( false );
3151 ReverseHighlight(m_lineSelectSingleOnUp
);
3156 if ((current
== m_current
) &&
3157 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3158 HasFlag(wxLC_EDIT_LABELS
) )
3162 wxRect label
= GetLineLabelRect( current
);
3163 if (label
.Contains( x
, y
))
3164 m_renameTimer
->Start( 250, true );
3168 m_renameTimer
->Start( 250, true );
3172 m_lastOnSame
= false;
3173 m_lineSelectSingleOnUp
= (size_t)-1;
3177 // This is necessary, because after a DnD operation in
3178 // from and to ourself, the up event is swallowed by the
3179 // DnD code. So on next non-up event (which means here and
3180 // now) m_lineSelectSingleOnUp should be reset.
3181 m_lineSelectSingleOnUp
= (size_t)-1;
3183 if (event
.RightDown())
3185 m_lineBeforeLastClicked
= m_lineLastClicked
;
3186 m_lineLastClicked
= current
;
3188 // If the item is already selected, do not update the selection.
3189 // Multi-selections should not be cleared if a selected item is clicked.
3190 if (!IsHighlighted(current
))
3192 HighlightAll(false);
3193 ChangeCurrent(current
);
3194 ReverseHighlight(m_current
);
3197 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
, event
.GetPosition() );
3199 // Allow generation of context menu event
3202 else if (event
.MiddleDown())
3204 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3206 else if ( event
.LeftDown() || forceClick
)
3208 m_lineBeforeLastClicked
= m_lineLastClicked
;
3209 m_lineLastClicked
= current
;
3211 size_t oldCurrent
= m_current
;
3212 bool oldWasSelected
= IsHighlighted(m_current
);
3214 bool cmdModifierDown
= event
.CmdDown();
3215 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3217 if ( IsSingleSel() || !IsHighlighted(current
) )
3219 HighlightAll( false );
3221 ChangeCurrent(current
);
3223 ReverseHighlight(m_current
);
3225 else // multi sel & current is highlighted & no mod keys
3227 m_lineSelectSingleOnUp
= current
;
3228 ChangeCurrent(current
); // change focus
3231 else // multi sel & either ctrl or shift is down
3233 if (cmdModifierDown
)
3235 ChangeCurrent(current
);
3237 ReverseHighlight(m_current
);
3239 else if (event
.ShiftDown())
3241 ChangeCurrent(current
);
3243 size_t lineFrom
= oldCurrent
,
3246 if ( lineTo
< lineFrom
)
3249 lineFrom
= m_current
;
3252 HighlightLines(lineFrom
, lineTo
);
3254 else // !ctrl, !shift
3256 // test in the enclosing if should make it impossible
3257 wxFAIL_MSG( _T("how did we get here?") );
3261 if (m_current
!= oldCurrent
)
3262 RefreshLine( oldCurrent
);
3264 // forceClick is only set if the previous click was on another item
3265 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
) && oldWasSelected
;
3269 void wxListMainWindow::MoveToItem(size_t item
)
3271 if ( item
== (size_t)-1 )
3274 wxRect rect
= GetLineRect(item
);
3276 int client_w
, client_h
;
3277 GetClientSize( &client_w
, &client_h
);
3279 const int hLine
= GetLineHeight();
3281 int view_x
= SCROLL_UNIT_X
* GetScrollPos( wxHORIZONTAL
);
3282 int view_y
= hLine
* GetScrollPos( wxVERTICAL
);
3284 if ( InReportView() )
3286 // the next we need the range of lines shown it might be different,
3287 // so recalculate it
3288 ResetVisibleLinesRange();
3290 if (rect
.y
< view_y
)
3291 Scroll( -1, rect
.y
/ hLine
);
3292 if (rect
.y
+ rect
.height
+ 5 > view_y
+ client_h
)
3293 Scroll( -1, (rect
.y
+ rect
.height
- client_h
+ hLine
) / hLine
);
3297 if (rect
.x
-view_x
< 5)
3298 Scroll( (rect
.x
- 5) / SCROLL_UNIT_X
, -1 );
3299 if (rect
.x
+ rect
.width
- 5 > view_x
+ client_w
)
3300 Scroll( (rect
.x
+ rect
.width
- client_w
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
, -1 );
3304 // ----------------------------------------------------------------------------
3305 // keyboard handling
3306 // ----------------------------------------------------------------------------
3308 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3310 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3311 _T("invalid item index in OnArrowChar()") );
3313 size_t oldCurrent
= m_current
;
3315 // in single selection we just ignore Shift as we can't select several
3317 if ( event
.ShiftDown() && !IsSingleSel() )
3319 ChangeCurrent(newCurrent
);
3321 // refresh the old focus to remove it
3322 RefreshLine( oldCurrent
);
3324 // select all the items between the old and the new one
3325 if ( oldCurrent
> newCurrent
)
3327 newCurrent
= oldCurrent
;
3328 oldCurrent
= m_current
;
3331 HighlightLines(oldCurrent
, newCurrent
);
3335 // all previously selected items are unselected unless ctrl is held
3336 // in a multiselection control
3337 if ( !event
.ControlDown() || IsSingleSel() )
3338 HighlightAll(false);
3340 ChangeCurrent(newCurrent
);
3342 // refresh the old focus to remove it
3343 RefreshLine( oldCurrent
);
3345 // in single selection mode we must always have a selected item
3346 if ( !event
.ControlDown() || IsSingleSel() )
3347 HighlightLine( m_current
, true );
3350 RefreshLine( m_current
);
3355 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3357 wxWindow
*parent
= GetParent();
3359 // propagate the key event upwards
3360 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3361 ke
.m_shiftDown
= event
.m_shiftDown
;
3362 ke
.m_controlDown
= event
.m_controlDown
;
3363 ke
.m_altDown
= event
.m_altDown
;
3364 ke
.m_metaDown
= event
.m_metaDown
;
3365 ke
.m_keyCode
= event
.m_keyCode
;
3368 ke
.SetEventObject( parent
);
3369 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3374 void wxListMainWindow::OnKeyUp( wxKeyEvent
&event
)
3376 wxWindow
*parent
= GetParent();
3378 // propagate the key event upwards
3379 wxKeyEvent
ke( wxEVT_KEY_UP
);
3380 ke
.m_shiftDown
= event
.m_shiftDown
;
3381 ke
.m_controlDown
= event
.m_controlDown
;
3382 ke
.m_altDown
= event
.m_altDown
;
3383 ke
.m_metaDown
= event
.m_metaDown
;
3384 ke
.m_keyCode
= event
.m_keyCode
;
3387 ke
.SetEventObject( parent
);
3388 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3393 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3395 wxWindow
*parent
= GetParent();
3397 // send a list_key event up
3400 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3401 le
.m_itemIndex
= m_current
;
3402 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3403 le
.m_code
= event
.GetKeyCode();
3404 le
.SetEventObject( parent
);
3405 parent
->GetEventHandler()->ProcessEvent( le
);
3408 // propagate the char event upwards
3409 wxKeyEvent
ke( wxEVT_CHAR
);
3410 ke
.m_shiftDown
= event
.m_shiftDown
;
3411 ke
.m_controlDown
= event
.m_controlDown
;
3412 ke
.m_altDown
= event
.m_altDown
;
3413 ke
.m_metaDown
= event
.m_metaDown
;
3414 ke
.m_keyCode
= event
.m_keyCode
;
3417 ke
.SetEventObject( parent
);
3418 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3420 if (event
.GetKeyCode() == WXK_TAB
)
3422 wxNavigationKeyEvent nevent
;
3423 nevent
.SetWindowChange( event
.ControlDown() );
3424 nevent
.SetDirection( !event
.ShiftDown() );
3425 nevent
.SetEventObject( GetParent()->GetParent() );
3426 nevent
.SetCurrentFocus( m_parent
);
3427 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3431 // no item -> nothing to do
3438 // don't use m_linesPerPage directly as it might not be computed yet
3439 const int pageSize
= GetCountPerPage();
3440 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3442 if (GetLayoutDirection() == wxLayout_RightToLeft
)
3444 if (event
.GetKeyCode() == WXK_RIGHT
)
3445 event
.m_keyCode
= WXK_LEFT
;
3446 else if (event
.GetKeyCode() == WXK_LEFT
)
3447 event
.m_keyCode
= WXK_RIGHT
;
3450 switch ( event
.GetKeyCode() )
3453 if ( m_current
> 0 )
3454 OnArrowChar( m_current
- 1, event
);
3458 if ( m_current
< (size_t)GetItemCount() - 1 )
3459 OnArrowChar( m_current
+ 1, event
);
3464 OnArrowChar( GetItemCount() - 1, event
);
3469 OnArrowChar( 0, event
);
3474 int steps
= InReportView() ? pageSize
- 1
3475 : m_current
% pageSize
;
3477 int index
= m_current
- steps
;
3481 OnArrowChar( index
, event
);
3487 int steps
= InReportView()
3489 : pageSize
- (m_current
% pageSize
) - 1;
3491 size_t index
= m_current
+ steps
;
3492 size_t count
= GetItemCount();
3493 if ( index
>= count
)
3496 OnArrowChar( index
, event
);
3501 if ( !InReportView() )
3503 int index
= m_current
- pageSize
;
3507 OnArrowChar( index
, event
);
3512 if ( !InReportView() )
3514 size_t index
= m_current
+ pageSize
;
3516 size_t count
= GetItemCount();
3517 if ( index
>= count
)
3520 OnArrowChar( index
, event
);
3525 if ( IsSingleSel() )
3527 if ( event
.ControlDown() )
3529 ReverseHighlight(m_current
);
3531 else // normal space press
3533 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3536 else // multiple selection
3538 ReverseHighlight(m_current
);
3544 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3552 // ----------------------------------------------------------------------------
3554 // ----------------------------------------------------------------------------
3556 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3560 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3561 event
.SetEventObject( GetParent() );
3562 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3566 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3567 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3568 // which are already drawn correctly resulting in horrible flicker - avoid
3578 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3582 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3583 event
.SetEventObject( GetParent() );
3584 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3592 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3594 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3596 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3598 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3600 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3602 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3604 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3606 else if ( InReportView() && (m_small_image_list
))
3608 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3612 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3614 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3616 m_normal_image_list
->GetSize( index
, width
, height
);
3618 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3620 m_small_image_list
->GetSize( index
, width
, height
);
3622 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3624 m_small_image_list
->GetSize( index
, width
, height
);
3626 else if ( InReportView() && m_small_image_list
)
3628 m_small_image_list
->GetSize( index
, width
, height
);
3637 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3639 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3640 dc
.SetFont( GetFont() );
3643 dc
.GetTextExtent( s
, &lw
, NULL
);
3645 return lw
+ AUTOSIZE_COL_MARGIN
;
3648 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3652 // calc the spacing from the icon size
3653 int width
= 0, height
= 0;
3655 if ((imageList
) && (imageList
->GetImageCount()) )
3656 imageList
->GetSize(0, width
, height
);
3658 if (which
== wxIMAGE_LIST_NORMAL
)
3660 m_normal_image_list
= imageList
;
3661 m_normal_spacing
= width
+ 8;
3664 if (which
== wxIMAGE_LIST_SMALL
)
3666 m_small_image_list
= imageList
;
3667 m_small_spacing
= width
+ 14;
3668 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3672 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3676 m_small_spacing
= spacing
;
3678 m_normal_spacing
= spacing
;
3681 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3683 return isSmall
? m_small_spacing
: m_normal_spacing
;
3686 // ----------------------------------------------------------------------------
3688 // ----------------------------------------------------------------------------
3690 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3692 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3694 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3696 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3697 item
.m_width
= GetTextLength( item
.m_text
);
3699 wxListHeaderData
*column
= node
->GetData();
3700 column
->SetItem( item
);
3702 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3704 headerWin
->m_dirty
= true;
3708 // invalidate it as it has to be recalculated
3712 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3714 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3715 _T("invalid column index") );
3717 wxCHECK_RET( InReportView(),
3718 _T("SetColumnWidth() can only be called in report mode.") );
3721 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3723 headerWin
->m_dirty
= true;
3725 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3726 wxCHECK_RET( node
, _T("no column?") );
3728 wxListHeaderData
*column
= node
->GetData();
3730 size_t count
= GetItemCount();
3732 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3734 width
= GetTextLength(column
->GetText());
3735 width
+= 2*EXTRA_WIDTH
;
3737 // check for column header's image availability
3738 const int image
= column
->GetImage();
3741 if ( m_small_image_list
)
3744 m_small_image_list
->GetSize(image
, ix
, iy
);
3745 width
+= ix
+ HEADER_IMAGE_MARGIN_IN_REPORT_MODE
;
3749 else if ( width
== wxLIST_AUTOSIZE
)
3753 // TODO: determine the max width somehow...
3754 width
= WIDTH_COL_DEFAULT
;
3758 wxClientDC
dc(this);
3759 dc
.SetFont( GetFont() );
3761 int max
= AUTOSIZE_COL_MARGIN
;
3763 // if the cached column width isn't valid then recalculate it
3764 if (m_aColWidths
.Item(col
)->bNeedsUpdate
)
3766 for (size_t i
= 0; i
< count
; i
++)
3768 wxListLineData
*line
= GetLine( i
);
3769 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3771 wxCHECK_RET( n
, _T("no subitem?") );
3773 wxListItemData
*itemData
= n
->GetData();
3776 itemData
->GetItem(item
);
3777 int itemWidth
= GetItemWidthWithImage(&item
);
3778 if (itemWidth
> max
)
3782 m_aColWidths
.Item(col
)->bNeedsUpdate
= false;
3783 m_aColWidths
.Item(col
)->nMaxWidth
= max
;
3786 max
= m_aColWidths
.Item(col
)->nMaxWidth
;
3787 width
= max
+ AUTOSIZE_COL_MARGIN
;
3791 column
->SetWidth( width
);
3793 // invalidate it as it has to be recalculated
3797 int wxListMainWindow::GetHeaderWidth() const
3799 if ( !m_headerWidth
)
3801 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3803 size_t count
= GetColumnCount();
3804 for ( size_t col
= 0; col
< count
; col
++ )
3806 self
->m_headerWidth
+= GetColumnWidth(col
);
3810 return m_headerWidth
;
3813 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3815 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3816 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3818 wxListHeaderData
*column
= node
->GetData();
3819 column
->GetItem( item
);
3822 int wxListMainWindow::GetColumnWidth( int col
) const
3824 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3825 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3827 wxListHeaderData
*column
= node
->GetData();
3828 return column
->GetWidth();
3831 // ----------------------------------------------------------------------------
3833 // ----------------------------------------------------------------------------
3835 void wxListMainWindow::SetItem( wxListItem
&item
)
3837 long id
= item
.m_itemId
;
3838 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3839 _T("invalid item index in SetItem") );
3843 wxListLineData
*line
= GetLine((size_t)id
);
3844 line
->SetItem( item
.m_col
, item
);
3846 // Set item state if user wants
3847 if ( item
.m_mask
& wxLIST_MASK_STATE
)
3848 SetItemState( item
.m_itemId
, item
.m_state
, item
.m_state
);
3852 // update the Max Width Cache if needed
3853 int width
= GetItemWidthWithImage(&item
);
3855 if (width
> m_aColWidths
.Item(item
.m_col
)->nMaxWidth
)
3856 m_aColWidths
.Item(item
.m_col
)->nMaxWidth
= width
;
3860 // update the item on screen
3862 GetItemRect(id
, rectItem
);
3863 RefreshRect(rectItem
);
3866 void wxListMainWindow::SetItemStateAll(long state
, long stateMask
)
3871 // first deal with selection
3872 if ( stateMask
& wxLIST_STATE_SELECTED
)
3874 // set/clear select state
3877 // optimized version for virtual listctrl.
3878 m_selStore
.SelectRange(0, GetItemCount() - 1, state
== wxLIST_STATE_SELECTED
);
3881 else if ( state
& wxLIST_STATE_SELECTED
)
3883 const long count
= GetItemCount();
3884 for( long i
= 0; i
< count
; i
++ )
3886 SetItemState( i
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
3892 // clear for non virtual (somewhat optimized by using GetNextItem())
3894 while ( (i
= GetNextItem(i
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
)) != -1 )
3896 SetItemState( i
, 0, wxLIST_STATE_SELECTED
);
3901 if ( HasCurrent() && (state
== 0) && (stateMask
& wxLIST_STATE_FOCUSED
) )
3903 // unfocus all: only one item can be focussed, so clearing focus for
3904 // all items is simply clearing focus of the focussed item.
3905 SetItemState(m_current
, state
, stateMask
);
3907 //(setting focus to all items makes no sense, so it is not handled here.)
3910 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3914 SetItemStateAll(state
, stateMask
);
3918 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3919 _T("invalid list ctrl item index in SetItem") );
3921 size_t oldCurrent
= m_current
;
3922 size_t item
= (size_t)litem
; // safe because of the check above
3924 // do we need to change the focus?
3925 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3927 if ( state
& wxLIST_STATE_FOCUSED
)
3929 // don't do anything if this item is already focused
3930 if ( item
!= m_current
)
3932 ChangeCurrent(item
);
3934 if ( oldCurrent
!= (size_t)-1 )
3936 if ( IsSingleSel() )
3938 HighlightLine(oldCurrent
, false);
3941 RefreshLine(oldCurrent
);
3944 RefreshLine( m_current
);
3949 // don't do anything if this item is not focused
3950 if ( item
== m_current
)
3954 if ( IsSingleSel() )
3956 // we must unselect the old current item as well or we
3957 // might end up with more than one selected item in a
3958 // single selection control
3959 HighlightLine(oldCurrent
, false);
3962 RefreshLine( oldCurrent
);
3967 // do we need to change the selection state?
3968 if ( stateMask
& wxLIST_STATE_SELECTED
)
3970 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3972 if ( IsSingleSel() )
3976 // selecting the item also makes it the focused one in the
3978 if ( m_current
!= item
)
3980 ChangeCurrent(item
);
3982 if ( oldCurrent
!= (size_t)-1 )
3984 HighlightLine( oldCurrent
, false );
3985 RefreshLine( oldCurrent
);
3991 // only the current item may be selected anyhow
3992 if ( item
!= m_current
)
3997 if ( HighlightLine(item
, on
) )
4004 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
4006 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
4007 _T("invalid list ctrl item index in GetItemState()") );
4009 int ret
= wxLIST_STATE_DONTCARE
;
4011 if ( stateMask
& wxLIST_STATE_FOCUSED
)
4013 if ( (size_t)item
== m_current
)
4014 ret
|= wxLIST_STATE_FOCUSED
;
4017 if ( stateMask
& wxLIST_STATE_SELECTED
)
4019 if ( IsHighlighted(item
) )
4020 ret
|= wxLIST_STATE_SELECTED
;
4026 void wxListMainWindow::GetItem( wxListItem
&item
) const
4028 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
4029 _T("invalid item index in GetItem") );
4031 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
4032 line
->GetItem( item
.m_col
, item
);
4034 // Get item state if user wants it
4035 if ( item
.m_mask
& wxLIST_MASK_STATE
)
4036 item
.m_state
= GetItemState( item
.m_itemId
, wxLIST_STATE_SELECTED
|
4037 wxLIST_STATE_FOCUSED
);
4040 // ----------------------------------------------------------------------------
4042 // ----------------------------------------------------------------------------
4044 size_t wxListMainWindow::GetItemCount() const
4046 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
4049 void wxListMainWindow::SetItemCount(long count
)
4051 m_selStore
.SetItemCount(count
);
4052 m_countVirt
= count
;
4054 ResetVisibleLinesRange();
4056 // scrollbars must be reset
4060 int wxListMainWindow::GetSelectedItemCount() const
4062 // deal with the quick case first
4063 if ( IsSingleSel() )
4064 return HasCurrent() ? IsHighlighted(m_current
) : false;
4066 // virtual controls remmebers all its selections itself
4068 return m_selStore
.GetSelectedCount();
4070 // TODO: we probably should maintain the number of items selected even for
4071 // non virtual controls as enumerating all lines is really slow...
4072 size_t countSel
= 0;
4073 size_t count
= GetItemCount();
4074 for ( size_t line
= 0; line
< count
; line
++ )
4076 if ( GetLine(line
)->IsHighlighted() )
4083 // ----------------------------------------------------------------------------
4084 // item position/size
4085 // ----------------------------------------------------------------------------
4087 wxRect
wxListMainWindow::GetViewRect() const
4089 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
4090 _T("wxListCtrl::GetViewRect() only works in icon mode") );
4092 // we need to find the longest/tallest label
4093 wxCoord xMax
= 0, yMax
= 0;
4094 const int count
= GetItemCount();
4097 for ( int i
= 0; i
< count
; i
++ )
4102 wxCoord x
= r
.GetRight(),
4112 // some fudge needed to make it look prettier
4113 xMax
+= 2 * EXTRA_BORDER_X
;
4114 yMax
+= 2 * EXTRA_BORDER_Y
;
4116 // account for the scrollbars if necessary
4117 const wxSize sizeAll
= GetClientSize();
4118 if ( xMax
> sizeAll
.x
)
4119 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
4120 if ( yMax
> sizeAll
.y
)
4121 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
4123 return wxRect(0, 0, xMax
, yMax
);
4126 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
4128 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4129 _T("invalid index in GetItemRect") );
4131 // ensure that we're laid out, otherwise we could return nonsense
4134 wxConstCast(this, wxListMainWindow
)->
4135 RecalculatePositions(true /* no refresh */);
4138 rect
= GetLineRect((size_t)index
);
4140 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
4143 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
4146 GetItemRect(item
, rect
);
4154 // ----------------------------------------------------------------------------
4155 // geometry calculation
4156 // ----------------------------------------------------------------------------
4158 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
4160 const int lineHeight
= GetLineHeight();
4162 wxClientDC
dc( this );
4163 dc
.SetFont( GetFont() );
4165 const size_t count
= GetItemCount();
4168 if ( HasFlag(wxLC_ICON
) )
4169 iconSpacing
= m_normal_spacing
;
4170 else if ( HasFlag(wxLC_SMALL_ICON
) )
4171 iconSpacing
= m_small_spacing
;
4175 // Note that we do not call GetClientSize() here but
4176 // GetSize() and subtract the border size for sunken
4177 // borders manually. This is technically incorrect,
4178 // but we need to know the client area's size WITHOUT
4179 // scrollbars here. Since we don't know if there are
4180 // any scrollbars, we use GetSize() instead. Another
4181 // solution would be to call SetScrollbars() here to
4182 // remove the scrollbars and call GetClientSize() then,
4183 // but this might result in flicker and - worse - will
4184 // reset the scrollbars to 0 which is not good at all
4185 // if you resize a dialog/window, but don't want to
4186 // reset the window scrolling. RR.
4187 // Furthermore, we actually do NOT subtract the border
4188 // width as 2 pixels is just the extra space which we
4189 // need around the actual content in the window. Other-
4190 // wise the text would e.g. touch the upper border. RR.
4193 GetSize( &clientWidth
, &clientHeight
);
4195 if ( InReportView() )
4197 // all lines have the same height and we scroll one line per step
4198 int entireHeight
= count
* lineHeight
+ LINE_SPACING
;
4200 m_linesPerPage
= clientHeight
/ lineHeight
;
4202 ResetVisibleLinesRange();
4204 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
4205 GetHeaderWidth() / SCROLL_UNIT_X
,
4206 (entireHeight
+ lineHeight
- 1) / lineHeight
,
4207 GetScrollPos(wxHORIZONTAL
),
4208 GetScrollPos(wxVERTICAL
),
4213 // we have 3 different layout strategies: either layout all items
4214 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4215 // to arrange them in top to bottom, left to right (don't ask me why
4216 // not the other way round...) order
4217 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
4219 int x
= EXTRA_BORDER_X
;
4220 int y
= EXTRA_BORDER_Y
;
4222 wxCoord widthMax
= 0;
4225 for ( i
= 0; i
< count
; i
++ )
4227 wxListLineData
*line
= GetLine(i
);
4228 line
->CalculateSize( &dc
, iconSpacing
);
4229 line
->SetPosition( x
, y
, iconSpacing
);
4231 wxSize sizeLine
= GetLineSize(i
);
4233 if ( HasFlag(wxLC_ALIGN_TOP
) )
4235 if ( sizeLine
.x
> widthMax
)
4236 widthMax
= sizeLine
.x
;
4240 else // wxLC_ALIGN_LEFT
4242 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
4246 if ( HasFlag(wxLC_ALIGN_TOP
) )
4248 // traverse the items again and tweak their sizes so that they are
4249 // all the same in a row
4250 for ( i
= 0; i
< count
; i
++ )
4252 wxListLineData
*line
= GetLine(i
);
4253 line
->m_gi
->ExtendWidth(widthMax
);
4261 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4262 (y
+ lineHeight
) / lineHeight
,
4263 GetScrollPos( wxHORIZONTAL
),
4264 GetScrollPos( wxVERTICAL
),
4268 else // "flowed" arrangement, the most complicated case
4270 // at first we try without any scrollbars, if the items don't fit into
4271 // the window, we recalculate after subtracting the space taken by the
4274 int entireWidth
= 0;
4276 for (int tries
= 0; tries
< 2; tries
++)
4278 entireWidth
= 2 * EXTRA_BORDER_X
;
4282 // Now we have decided that the items do not fit into the
4283 // client area, so we need a scrollbar
4284 entireWidth
+= SCROLL_UNIT_X
;
4287 int x
= EXTRA_BORDER_X
;
4288 int y
= EXTRA_BORDER_Y
;
4289 int maxWidthInThisRow
= 0;
4292 int currentlyVisibleLines
= 0;
4294 for (size_t i
= 0; i
< count
; i
++)
4296 currentlyVisibleLines
++;
4297 wxListLineData
*line
= GetLine( i
);
4298 line
->CalculateSize( &dc
, iconSpacing
);
4299 line
->SetPosition( x
, y
, iconSpacing
);
4301 wxSize sizeLine
= GetLineSize( i
);
4303 if ( maxWidthInThisRow
< sizeLine
.x
)
4304 maxWidthInThisRow
= sizeLine
.x
;
4307 if (currentlyVisibleLines
> m_linesPerPage
)
4308 m_linesPerPage
= currentlyVisibleLines
;
4310 if ( y
+ sizeLine
.y
>= clientHeight
)
4312 currentlyVisibleLines
= 0;
4314 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
4315 x
+= maxWidthInThisRow
;
4316 entireWidth
+= maxWidthInThisRow
;
4317 maxWidthInThisRow
= 0;
4320 // We have reached the last item.
4321 if ( i
== count
- 1 )
4322 entireWidth
+= maxWidthInThisRow
;
4324 if ( (tries
== 0) &&
4325 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4327 clientHeight
-= wxSystemSettings::
4328 GetMetric(wxSYS_HSCROLL_Y
);
4333 if ( i
== count
- 1 )
4334 tries
= 1; // Everything fits, no second try required.
4342 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4344 GetScrollPos( wxHORIZONTAL
),
4353 // FIXME: why should we call it from here?
4360 void wxListMainWindow::RefreshAll()
4365 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4366 if ( headerWin
&& headerWin
->m_dirty
)
4368 headerWin
->m_dirty
= false;
4369 headerWin
->Refresh();
4373 void wxListMainWindow::UpdateCurrent()
4375 if ( !HasCurrent() && !IsEmpty() )
4379 long wxListMainWindow::GetNextItem( long item
,
4380 int WXUNUSED(geometry
),
4384 max
= GetItemCount();
4385 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4386 _T("invalid listctrl index in GetNextItem()") );
4388 // notice that we start with the next item (or the first one if item == -1)
4389 // and this is intentional to allow writing a simple loop to iterate over
4390 // all selected items
4393 // this is not an error because the index was OK initially,
4394 // just no such item
4401 size_t count
= GetItemCount();
4402 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4404 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4407 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4414 // ----------------------------------------------------------------------------
4416 // ----------------------------------------------------------------------------
4418 void wxListMainWindow::DeleteItem( long lindex
)
4420 size_t count
= GetItemCount();
4422 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4423 _T("invalid item index in DeleteItem") );
4425 size_t index
= (size_t)lindex
;
4427 // we don't need to adjust the index for the previous items
4428 if ( HasCurrent() && m_current
>= index
)
4430 // if the current item is being deleted, we want the next one to
4431 // become selected - unless there is no next one - so don't adjust
4432 // m_current in this case
4433 if ( m_current
!= index
|| m_current
== count
- 1 )
4437 if ( InReportView() )
4439 // mark the Column Max Width cache as dirty if the items in the line
4440 // we're deleting contain the Max Column Width
4441 wxListLineData
* const line
= GetLine(index
);
4442 wxListItemDataList::compatibility_iterator n
;
4443 wxListItemData
*itemData
;
4447 for (size_t i
= 0; i
< m_columns
.GetCount(); i
++)
4449 n
= line
->m_items
.Item( i
);
4450 itemData
= n
->GetData();
4451 itemData
->GetItem(item
);
4453 itemWidth
= GetItemWidthWithImage(&item
);
4455 if (itemWidth
>= m_aColWidths
.Item(i
)->nMaxWidth
)
4456 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4459 ResetVisibleLinesRange();
4462 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
, wxDefaultPosition
);
4467 m_selStore
.OnItemDelete(index
);
4471 m_lines
.RemoveAt( index
);
4474 // we need to refresh the (vert) scrollbar as the number of items changed
4477 RefreshAfter(index
);
4480 void wxListMainWindow::DeleteColumn( int col
)
4482 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4484 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4487 delete node
->GetData();
4488 m_columns
.Erase( node
);
4492 // update all the items
4493 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4495 wxListLineData
* const line
= GetLine(i
);
4496 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4497 delete n
->GetData();
4498 line
->m_items
.Erase(n
);
4502 if ( InReportView() ) // we only cache max widths when in Report View
4504 delete m_aColWidths
.Item(col
);
4505 m_aColWidths
.RemoveAt(col
);
4508 // invalidate it as it has to be recalculated
4512 void wxListMainWindow::DoDeleteAllItems()
4515 // nothing to do - in particular, don't send the event
4520 // to make the deletion of all items faster, we don't send the
4521 // notifications for each item deletion in this case but only one event
4522 // for all of them: this is compatible with wxMSW and documented in
4523 // DeleteAllItems() description
4525 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4526 event
.SetEventObject( GetParent() );
4527 GetParent()->GetEventHandler()->ProcessEvent( event
);
4535 if ( InReportView() )
4537 ResetVisibleLinesRange();
4538 for (size_t i
= 0; i
< m_aColWidths
.GetCount(); i
++)
4540 m_aColWidths
.Item(i
)->bNeedsUpdate
= true;
4547 void wxListMainWindow::DeleteAllItems()
4551 RecalculatePositions();
4554 void wxListMainWindow::DeleteEverything()
4556 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4557 WX_CLEAR_ARRAY(m_aColWidths
);
4562 // ----------------------------------------------------------------------------
4563 // scanning for an item
4564 // ----------------------------------------------------------------------------
4566 void wxListMainWindow::EnsureVisible( long index
)
4568 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4569 _T("invalid index in EnsureVisible") );
4571 // We have to call this here because the label in question might just have
4572 // been added and its position is not known yet
4574 RecalculatePositions(true /* no refresh */);
4576 MoveToItem((size_t)index
);
4579 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool partial
)
4585 wxString str_upper
= str
.Upper();
4589 size_t count
= GetItemCount();
4590 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4592 wxListLineData
*line
= GetLine(i
);
4593 wxString line_upper
= line
->GetText(0).Upper();
4596 if (line_upper
== str_upper
)
4601 if (line_upper
.find(str_upper
) == 0)
4609 long wxListMainWindow::FindItem(long start
, wxUIntPtr data
)
4615 size_t count
= GetItemCount();
4616 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4618 wxListLineData
*line
= GetLine(i
);
4620 line
->GetItem( 0, item
);
4621 if (item
.m_data
== data
)
4628 long wxListMainWindow::FindItem( const wxPoint
& pt
)
4631 GetVisibleLinesRange( &topItem
, NULL
);
4634 GetItemPosition( GetItemCount() - 1, p
);
4638 long id
= (long)floor( pt
.y
* double(GetItemCount() - topItem
- 1) / p
.y
+ topItem
);
4639 if ( id
>= 0 && id
< (long)GetItemCount() )
4645 long wxListMainWindow::HitTest( int x
, int y
, int &flags
) const
4647 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4649 size_t count
= GetItemCount();
4651 if ( InReportView() )
4653 size_t current
= y
/ GetLineHeight();
4654 if ( current
< count
)
4656 flags
= HitTestLine(current
, x
, y
);
4663 // TODO: optimize it too! this is less simple than for report view but
4664 // enumerating all items is still not a way to do it!!
4665 for ( size_t current
= 0; current
< count
; current
++ )
4667 flags
= HitTestLine(current
, x
, y
);
4676 // ----------------------------------------------------------------------------
4678 // ----------------------------------------------------------------------------
4680 void wxListMainWindow::InsertItem( wxListItem
&item
)
4682 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4684 int count
= GetItemCount();
4685 wxCHECK_RET( item
.m_itemId
>= 0, _T("invalid item index") );
4687 if (item
.m_itemId
> count
)
4688 item
.m_itemId
= count
;
4690 size_t id
= item
.m_itemId
;
4694 if ( InReportView() )
4696 ResetVisibleLinesRange();
4698 // calculate the width of the item and adjust the max column width
4699 wxColWidthInfo
*pWidthInfo
= m_aColWidths
.Item(item
.GetColumn());
4700 int width
= GetItemWidthWithImage(&item
);
4701 item
.SetWidth(width
);
4702 if (width
> pWidthInfo
->nMaxWidth
)
4703 pWidthInfo
->nMaxWidth
= width
;
4706 wxListLineData
*line
= new wxListLineData(this);
4708 line
->SetItem( item
.m_col
, item
);
4710 m_lines
.Insert( line
, id
);
4714 // If an item is selected at or below the point of insertion, we need to
4715 // increment the member variables because the current row's index has gone
4717 if ( HasCurrent() && m_current
>= id
)
4720 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4722 RefreshLines(id
, GetItemCount() - 1);
4725 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4728 if ( InReportView() )
4730 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4731 item
.m_width
= GetTextLength( item
.m_text
);
4733 wxListHeaderData
*column
= new wxListHeaderData( item
);
4734 wxColWidthInfo
*colWidthInfo
= new wxColWidthInfo();
4736 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4739 wxListHeaderDataList::compatibility_iterator
4740 node
= m_columns
.Item( col
);
4741 m_columns
.Insert( node
, column
);
4742 m_aColWidths
.Insert( colWidthInfo
, col
);
4746 m_columns
.Append( column
);
4747 m_aColWidths
.Add( colWidthInfo
);
4752 // update all the items
4753 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4755 wxListLineData
* const line
= GetLine(i
);
4756 wxListItemData
* const data
= new wxListItemData(this);
4758 line
->m_items
.Insert(col
, data
);
4760 line
->m_items
.Append(data
);
4764 // invalidate it as it has to be recalculated
4769 int wxListMainWindow::GetItemWidthWithImage(wxListItem
* item
)
4772 wxClientDC
dc(this);
4774 dc
.SetFont( GetFont() );
4776 if (item
->GetImage() != -1)
4779 GetImageSize( item
->GetImage(), ix
, iy
);
4783 if (!item
->GetText().empty())
4786 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
4793 // ----------------------------------------------------------------------------
4795 // ----------------------------------------------------------------------------
4797 wxListCtrlCompare list_ctrl_compare_func_2
;
4798 long list_ctrl_compare_data
;
4800 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4802 wxListLineData
*line1
= *arg1
;
4803 wxListLineData
*line2
= *arg2
;
4805 line1
->GetItem( 0, item
);
4806 wxUIntPtr data1
= item
.m_data
;
4807 line2
->GetItem( 0, item
);
4808 wxUIntPtr data2
= item
.m_data
;
4809 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4812 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4814 // selections won't make sense any more after sorting the items so reset
4816 HighlightAll(false);
4819 list_ctrl_compare_func_2
= fn
;
4820 list_ctrl_compare_data
= data
;
4821 m_lines
.Sort( list_ctrl_compare_func_1
);
4825 // ----------------------------------------------------------------------------
4827 // ----------------------------------------------------------------------------
4829 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4832 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4833 wxScrolledWindow::OnScroll(event
);
4835 HandleOnScroll( event
);
4838 // update our idea of which lines are shown when we redraw the window the
4840 ResetVisibleLinesRange();
4842 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4844 wxGenericListCtrl
* lc
= GetListCtrl();
4845 wxCHECK_RET( lc
, _T("no listctrl window?") );
4847 lc
->m_headerWin
->Refresh();
4848 lc
->m_headerWin
->Update();
4852 int wxListMainWindow::GetCountPerPage() const
4854 if ( !m_linesPerPage
)
4856 wxConstCast(this, wxListMainWindow
)->
4857 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4860 return m_linesPerPage
;
4863 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4865 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4867 if ( m_lineFrom
== (size_t)-1 )
4869 size_t count
= GetItemCount();
4872 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4874 // this may happen if SetScrollbars() hadn't been called yet
4875 if ( m_lineFrom
>= count
)
4876 m_lineFrom
= count
- 1;
4878 // we redraw one extra line but this is needed to make the redrawing
4879 // logic work when there is a fractional number of lines on screen
4880 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4881 if ( m_lineTo
>= count
)
4882 m_lineTo
= count
- 1;
4884 else // empty control
4887 m_lineTo
= (size_t)-1;
4891 wxASSERT_MSG( IsEmpty() ||
4892 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4893 _T("GetVisibleLinesRange() returns incorrect result") );
4901 // -------------------------------------------------------------------------------------
4902 // wxGenericListCtrl
4903 // -------------------------------------------------------------------------------------
4905 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4907 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4908 EVT_SIZE(wxGenericListCtrl::OnSize
)
4911 wxGenericListCtrl::wxGenericListCtrl()
4913 m_imageListNormal
= (wxImageList
*) NULL
;
4914 m_imageListSmall
= (wxImageList
*) NULL
;
4915 m_imageListState
= (wxImageList
*) NULL
;
4917 m_ownsImageListNormal
=
4918 m_ownsImageListSmall
=
4919 m_ownsImageListState
= false;
4921 m_mainWin
= (wxListMainWindow
*) NULL
;
4922 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4926 wxGenericListCtrl::~wxGenericListCtrl()
4928 if (m_ownsImageListNormal
)
4929 delete m_imageListNormal
;
4930 if (m_ownsImageListSmall
)
4931 delete m_imageListSmall
;
4932 if (m_ownsImageListState
)
4933 delete m_imageListState
;
4936 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4942 GetThemeMetric( kThemeMetricListHeaderHeight
, &h
);
4944 // we use 'g' to get the descent, too
4946 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4947 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4950 // only update if changed
4951 if ( h
!= m_headerHeight
)
4956 ResizeReportView(true);
4957 else //why is this needed if it doesn't have a header?
4958 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4963 void wxGenericListCtrl::CreateHeaderWindow()
4965 m_headerWin
= new wxListHeaderWindow
4967 this, wxID_ANY
, m_mainWin
,
4969 wxSize(GetClientSize().x
, m_headerHeight
),
4972 CalculateAndSetHeaderHeight();
4975 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4980 const wxValidator
&validator
,
4981 const wxString
&name
)
4985 m_imageListState
= (wxImageList
*) NULL
;
4986 m_ownsImageListNormal
=
4987 m_ownsImageListSmall
=
4988 m_ownsImageListState
= false;
4990 m_mainWin
= (wxListMainWindow
*) NULL
;
4991 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4995 if ( !(style
& wxLC_MASK_TYPE
) )
4997 style
= style
| wxLC_LIST
;
5000 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
5003 // this window itself shouldn't get the focus, only m_mainWin should
5006 // don't create the inner window with the border
5007 style
&= ~wxBORDER_MASK
;
5009 m_mainWin
= new wxListMainWindow( this, wxID_ANY
, wxPoint(0, 0), size
, style
);
5011 #ifdef __WXMAC_CARBON__
5012 // Human Interface Guidelines ask us for a special font in this case
5013 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL
)
5016 font
.MacCreateThemeFont( kThemeViewsFont
);
5021 if ( InReportView() )
5023 CreateHeaderWindow();
5025 #ifdef __WXMAC_CARBON__
5029 font
.MacCreateThemeFont( kThemeSmallSystemFont
);
5030 m_headerWin
->SetFont( font
);
5031 CalculateAndSetHeaderHeight();
5035 if ( HasFlag(wxLC_NO_HEADER
) )
5036 // VZ: why do we create it at all then?
5037 m_headerWin
->Show( false );
5040 SetInitialSize(size
);
5045 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
5047 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
5048 _T("wxLC_VIRTUAL can't be [un]set") );
5050 long flag
= GetWindowStyle();
5054 if (style
& wxLC_MASK_TYPE
)
5055 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
5056 if (style
& wxLC_MASK_ALIGN
)
5057 flag
&= ~wxLC_MASK_ALIGN
;
5058 if (style
& wxLC_MASK_SORT
)
5059 flag
&= ~wxLC_MASK_SORT
;
5067 SetWindowStyleFlag( flag
);
5070 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
5074 m_mainWin
->DeleteEverything();
5076 // has the header visibility changed?
5077 bool hasHeader
= HasHeader();
5078 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
5080 if ( hasHeader
!= willHaveHeader
)
5087 // don't delete, just hide, as we can reuse it later
5088 m_headerWin
->Show(false);
5090 //else: nothing to do
5092 else // must show header
5096 CreateHeaderWindow();
5098 else // already have it, just show
5100 m_headerWin
->Show( true );
5104 ResizeReportView(willHaveHeader
);
5108 wxWindow::SetWindowStyleFlag( flag
);
5111 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
5113 m_mainWin
->GetColumn( col
, item
);
5117 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
5119 m_mainWin
->SetColumn( col
, item
);
5123 int wxGenericListCtrl::GetColumnWidth( int col
) const
5125 return m_mainWin
->GetColumnWidth( col
);
5128 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
5130 m_mainWin
->SetColumnWidth( col
, width
);
5134 int wxGenericListCtrl::GetCountPerPage() const
5136 return m_mainWin
->GetCountPerPage(); // different from Windows ?
5139 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
5141 m_mainWin
->GetItem( info
);
5145 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
5147 m_mainWin
->SetItem( info
);
5151 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
5154 info
.m_text
= label
;
5155 info
.m_mask
= wxLIST_MASK_TEXT
;
5156 info
.m_itemId
= index
;
5160 info
.m_image
= imageId
;
5161 info
.m_mask
|= wxLIST_MASK_IMAGE
;
5164 m_mainWin
->SetItem(info
);
5168 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
5170 return m_mainWin
->GetItemState( item
, stateMask
);
5173 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
5175 m_mainWin
->SetItemState( item
, state
, stateMask
);
5180 wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
5182 return SetItemColumnImage(item
, 0, image
);
5186 wxGenericListCtrl::SetItemColumnImage( long item
, long column
, int image
)
5189 info
.m_image
= image
;
5190 info
.m_mask
= wxLIST_MASK_IMAGE
;
5191 info
.m_itemId
= item
;
5192 info
.m_col
= column
;
5193 m_mainWin
->SetItem( info
);
5197 wxString
wxGenericListCtrl::GetItemText( long item
) const
5199 return m_mainWin
->GetItemText(item
);
5202 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
5204 m_mainWin
->SetItemText(item
, str
);
5207 wxUIntPtr
wxGenericListCtrl::GetItemData( long item
) const
5210 info
.m_mask
= wxLIST_MASK_DATA
;
5211 info
.m_itemId
= item
;
5212 m_mainWin
->GetItem( info
);
5216 bool wxGenericListCtrl::SetItemPtrData( long item
, wxUIntPtr data
)
5219 info
.m_mask
= wxLIST_MASK_DATA
;
5220 info
.m_itemId
= item
;
5222 m_mainWin
->SetItem( info
);
5226 wxRect
wxGenericListCtrl::GetViewRect() const
5228 return m_mainWin
->GetViewRect();
5231 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
5233 m_mainWin
->GetItemRect( item
, rect
);
5234 if ( m_mainWin
->HasHeader() )
5235 rect
.y
+= m_headerHeight
+ 1;
5239 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
5241 m_mainWin
->GetItemPosition( item
, pos
);
5245 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
5250 int wxGenericListCtrl::GetItemCount() const
5252 return m_mainWin
->GetItemCount();
5255 int wxGenericListCtrl::GetColumnCount() const
5257 return m_mainWin
->GetColumnCount();
5260 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
5262 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
5265 wxSize
wxGenericListCtrl::GetItemSpacing() const
5267 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
5269 return wxSize(spacing
, spacing
);
5272 #if WXWIN_COMPATIBILITY_2_6
5273 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
5275 return m_mainWin
->GetItemSpacing( isSmall
);
5277 #endif // WXWIN_COMPATIBILITY_2_6
5279 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
5282 info
.m_itemId
= item
;
5283 info
.SetTextColour( col
);
5284 m_mainWin
->SetItem( info
);
5287 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
5290 info
.m_itemId
= item
;
5291 m_mainWin
->GetItem( info
);
5292 return info
.GetTextColour();
5295 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
5298 info
.m_itemId
= item
;
5299 info
.SetBackgroundColour( col
);
5300 m_mainWin
->SetItem( info
);
5303 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
5306 info
.m_itemId
= item
;
5307 m_mainWin
->GetItem( info
);
5308 return info
.GetBackgroundColour();
5311 int wxGenericListCtrl::GetScrollPos( int orient
) const
5313 return m_mainWin
->GetScrollPos( orient
);
5316 void wxGenericListCtrl::SetScrollPos( int orient
, int pos
, bool refresh
)
5318 m_mainWin
->SetScrollPos( orient
, pos
, refresh
);
5321 void wxGenericListCtrl::SetItemFont( long item
, const wxFont
&f
)
5324 info
.m_itemId
= item
;
5326 m_mainWin
->SetItem( info
);
5329 wxFont
wxGenericListCtrl::GetItemFont( long item
) const
5332 info
.m_itemId
= item
;
5333 m_mainWin
->GetItem( info
);
5334 return info
.GetFont();
5337 int wxGenericListCtrl::GetSelectedItemCount() const
5339 return m_mainWin
->GetSelectedItemCount();
5342 wxColour
wxGenericListCtrl::GetTextColour() const
5344 return GetForegroundColour();
5347 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
5349 SetForegroundColour(col
);
5352 long wxGenericListCtrl::GetTopItem() const
5355 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
5359 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
5361 return m_mainWin
->GetNextItem( item
, geom
, state
);
5364 wxImageList
*wxGenericListCtrl::GetImageList(int which
) const
5366 if (which
== wxIMAGE_LIST_NORMAL
)
5367 return m_imageListNormal
;
5368 else if (which
== wxIMAGE_LIST_SMALL
)
5369 return m_imageListSmall
;
5370 else if (which
== wxIMAGE_LIST_STATE
)
5371 return m_imageListState
;
5373 return (wxImageList
*) NULL
;
5376 void wxGenericListCtrl::SetImageList( wxImageList
*imageList
, int which
)
5378 if ( which
== wxIMAGE_LIST_NORMAL
)
5380 if (m_ownsImageListNormal
)
5381 delete m_imageListNormal
;
5382 m_imageListNormal
= imageList
;
5383 m_ownsImageListNormal
= false;
5385 else if ( which
== wxIMAGE_LIST_SMALL
)
5387 if (m_ownsImageListSmall
)
5388 delete m_imageListSmall
;
5389 m_imageListSmall
= imageList
;
5390 m_ownsImageListSmall
= false;
5392 else if ( which
== wxIMAGE_LIST_STATE
)
5394 if (m_ownsImageListState
)
5395 delete m_imageListState
;
5396 m_imageListState
= imageList
;
5397 m_ownsImageListState
= false;
5400 m_mainWin
->SetImageList( imageList
, which
);
5403 void wxGenericListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
5405 SetImageList(imageList
, which
);
5406 if ( which
== wxIMAGE_LIST_NORMAL
)
5407 m_ownsImageListNormal
= true;
5408 else if ( which
== wxIMAGE_LIST_SMALL
)
5409 m_ownsImageListSmall
= true;
5410 else if ( which
== wxIMAGE_LIST_STATE
)
5411 m_ownsImageListState
= true;
5414 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
5419 bool wxGenericListCtrl::DeleteItem( long item
)
5421 m_mainWin
->DeleteItem( item
);
5425 bool wxGenericListCtrl::DeleteAllItems()
5427 m_mainWin
->DeleteAllItems();
5431 bool wxGenericListCtrl::DeleteAllColumns()
5433 size_t count
= m_mainWin
->m_columns
.GetCount();
5434 for ( size_t n
= 0; n
< count
; n
++ )
5439 void wxGenericListCtrl::ClearAll()
5441 m_mainWin
->DeleteEverything();
5444 bool wxGenericListCtrl::DeleteColumn( int col
)
5446 m_mainWin
->DeleteColumn( col
);
5448 // if we don't have the header any longer, we need to relayout the window
5449 if ( !GetColumnCount() )
5450 ResizeReportView(false /* no header */);
5454 wxTextCtrl
*wxGenericListCtrl::EditLabel(long item
,
5455 wxClassInfo
* textControlClass
)
5457 return m_mainWin
->EditLabel( item
, textControlClass
);
5460 wxTextCtrl
*wxGenericListCtrl::GetEditControl() const
5462 return m_mainWin
->GetEditControl();
5465 bool wxGenericListCtrl::EnsureVisible( long item
)
5467 m_mainWin
->EnsureVisible( item
);
5471 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5473 return m_mainWin
->FindItem( start
, str
, partial
);
5476 long wxGenericListCtrl::FindItem( long start
, wxUIntPtr data
)
5478 return m_mainWin
->FindItem( start
, data
);
5481 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& pt
,
5482 int WXUNUSED(direction
))
5484 return m_mainWin
->FindItem( pt
);
5487 // TODO: sub item hit testing
5488 long wxGenericListCtrl::HitTest(const wxPoint
& point
, int& flags
, long *) const
5490 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5493 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5495 m_mainWin
->InsertItem( info
);
5496 return info
.m_itemId
;
5499 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5502 info
.m_text
= label
;
5503 info
.m_mask
= wxLIST_MASK_TEXT
;
5504 info
.m_itemId
= index
;
5505 return InsertItem( info
);
5508 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5511 info
.m_mask
= wxLIST_MASK_IMAGE
;
5512 info
.m_image
= imageIndex
;
5513 info
.m_itemId
= index
;
5514 return InsertItem( info
);
5517 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5520 info
.m_text
= label
;
5521 info
.m_image
= imageIndex
;
5522 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5523 info
.m_itemId
= index
;
5524 return InsertItem( info
);
5527 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5529 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5531 m_mainWin
->InsertColumn( col
, item
);
5533 // if we hadn't had a header before but have one now
5534 // then we need to relayout the window
5535 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5536 ResizeReportView(true /* have header */);
5538 m_headerWin
->Refresh();
5543 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5544 int format
, int width
)
5547 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5548 item
.m_text
= heading
;
5551 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5552 item
.m_width
= width
;
5555 item
.m_format
= format
;
5557 return InsertColumn( col
, item
);
5560 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5566 // fn is a function which takes 3 long arguments: item1, item2, data.
5567 // item1 is the long data associated with a first item (NOT the index).
5568 // item2 is the long data associated with a second item (NOT the index).
5569 // data is the same value as passed to SortItems.
5570 // The return value is a negative number if the first item should precede the second
5571 // item, a positive number of the second item should precede the first,
5572 // or zero if the two items are equivalent.
5573 // data is arbitrary data to be passed to the sort function.
5575 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5577 m_mainWin
->SortItems( fn
, data
);
5581 // ----------------------------------------------------------------------------
5583 // ----------------------------------------------------------------------------
5585 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5590 ResizeReportView(m_mainWin
->HasHeader());
5591 m_mainWin
->RecalculatePositions();
5594 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5597 GetClientSize( &cw
, &ch
);
5601 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5602 if(ch
> m_headerHeight
)
5603 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5604 cw
, ch
- m_headerHeight
- 1 );
5606 m_mainWin
->SetSize( 0, m_headerHeight
+ 1,
5609 else // no header window
5611 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5615 void wxGenericListCtrl::OnInternalIdle()
5617 wxWindow::OnInternalIdle();
5619 // do it only if needed
5620 if ( !m_mainWin
->m_dirty
)
5623 m_mainWin
->RecalculatePositions();
5626 // ----------------------------------------------------------------------------
5628 // ----------------------------------------------------------------------------
5630 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5634 m_mainWin
->SetBackgroundColour( colour
);
5635 m_mainWin
->m_dirty
= true;
5641 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5643 if ( !wxWindow::SetForegroundColour( colour
) )
5648 m_mainWin
->SetForegroundColour( colour
);
5649 m_mainWin
->m_dirty
= true;
5653 m_headerWin
->SetForegroundColour( colour
);
5658 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5660 if ( !wxWindow::SetFont( font
) )
5665 m_mainWin
->SetFont( font
);
5666 m_mainWin
->m_dirty
= true;
5671 m_headerWin
->SetFont( font
);
5672 CalculateAndSetHeaderHeight();
5682 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5685 // Use the same color scheme as wxListBox
5686 return wxListBox::GetClassDefaultAttributes(variant
);
5688 wxUnusedVar(variant
);
5689 wxVisualAttributes attr
;
5690 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5691 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5692 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5697 // ----------------------------------------------------------------------------
5698 // methods forwarded to m_mainWin
5699 // ----------------------------------------------------------------------------
5701 #if wxUSE_DRAG_AND_DROP
5703 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5705 m_mainWin
->SetDropTarget( dropTarget
);
5708 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5710 return m_mainWin
->GetDropTarget();
5715 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5717 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : false;
5720 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5722 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5725 wxColour
wxGenericListCtrl::GetForegroundColour() const
5727 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5730 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5733 return m_mainWin
->PopupMenu( menu
, x
, y
);
5739 void wxGenericListCtrl::DoClientToScreen( int *x
, int *y
) const
5741 m_mainWin
->DoClientToScreen(x
, y
);
5744 void wxGenericListCtrl::DoScreenToClient( int *x
, int *y
) const
5746 m_mainWin
->DoScreenToClient(x
, y
);
5749 void wxGenericListCtrl::SetFocus()
5751 // The test in window.cpp fails as we are a composite
5752 // window, so it checks against "this", but not m_mainWin.
5753 if ( DoFindFocus() != this )
5754 m_mainWin
->SetFocus();
5757 wxSize
wxGenericListCtrl::DoGetBestSize() const
5759 // Something is better than nothing...
5760 // 100x80 is what the MSW version will get from the default
5761 // wxControl::DoGetBestSize
5762 return wxSize(100, 80);
5765 // ----------------------------------------------------------------------------
5766 // virtual list control support
5767 // ----------------------------------------------------------------------------
5769 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5771 // this is a pure virtual function, in fact - which is not really pure
5772 // because the controls which are not virtual don't need to implement it
5773 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5775 return wxEmptyString
;
5778 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5780 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL
),
5782 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5786 int wxGenericListCtrl::OnGetItemColumnImage(long item
, long column
) const
5789 return OnGetItemImage(item
);
5795 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5797 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5798 _T("invalid item index in OnGetItemAttr()") );
5800 // no attributes by default
5804 void wxGenericListCtrl::SetItemCount(long count
)
5806 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5808 m_mainWin
->SetItemCount(count
);
5811 void wxGenericListCtrl::RefreshItem(long item
)
5813 m_mainWin
->RefreshLine(item
);
5816 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5818 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5821 // Generic wxListCtrl is more or less a container for two other
5822 // windows which drawings are done upon. These are namely
5823 // 'm_headerWin' and 'm_mainWin'.
5824 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5825 // behaviour wxListCtrl has under wxMSW.
5827 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5831 // The easy case, no rectangle specified.
5833 m_headerWin
->Refresh(eraseBackground
);
5836 m_mainWin
->Refresh(eraseBackground
);
5840 // Refresh the header window
5843 wxRect rectHeader
= m_headerWin
->GetRect();
5844 rectHeader
.Intersect(*rect
);
5845 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5848 m_headerWin
->GetPosition(&x
, &y
);
5849 rectHeader
.Offset(-x
, -y
);
5850 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5854 // Refresh the main window
5857 wxRect rectMain
= m_mainWin
->GetRect();
5858 rectMain
.Intersect(*rect
);
5859 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5862 m_mainWin
->GetPosition(&x
, &y
);
5863 rectMain
.Offset(-x
, -y
);
5864 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5870 void wxGenericListCtrl::Freeze()
5872 m_mainWin
->Freeze();
5875 void wxGenericListCtrl::Thaw()
5880 #endif // wxUSE_LISTCTRL