1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 /////////////////////////////////////////////////////////////////////////////
14 1. we need to implement searching/sorting for virtual controls somehow
15 ?2. when changing selection the lines are refreshed twice
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
27 #pragma implementation "listctrl.h"
28 #pragma implementation "listctrlbase.h"
31 // For compilers that support precompilation, includes "wx.h".
32 #include "wx/wxprec.h"
43 #include "wx/dynarray.h"
45 #include "wx/dcscreen.h"
47 #include "wx/textctrl.h"
50 // under Win32 we always use the native version and also may use the generic
51 // one, however some things should be done only if we use only the generic
53 #if defined(__WIN32__) && !defined(__WXUNIVERSAL__)
54 #define HAVE_NATIVE_LISTCTRL
57 // if we have the native control, wx/listctrl.h declares it and not this one
58 #ifdef HAVE_NATIVE_LISTCTRL
59 #include "wx/generic/listctrl.h"
60 #else // !HAVE_NATIVE_LISTCTRL
61 #include "wx/listctrl.h"
63 // if we have a native version, its implementation file does all this
64 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
66 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
68 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
69 #endif // HAVE_NATIVE_LISTCTRL/!HAVE_NATIVE_LISTCTRL
71 #if defined(__WXGTK__)
73 #include "wx/gtk/win_gtk.h"
76 #include "wx/selstore.h"
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
99 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
101 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
102 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
103 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 // the height of the header window (FIXME: should depend on its font!)
110 static const int HEADER_HEIGHT
= 23;
112 // the scrollbar units
113 static const int SCROLL_UNIT_X
= 15;
114 static const int SCROLL_UNIT_Y
= 15;
116 // the spacing between the lines (in report mode)
117 static const int LINE_SPACING
= 0;
119 // extra margins around the text label
120 static const int EXTRA_WIDTH
= 3;
121 static const int EXTRA_HEIGHT
= 4;
123 // offset for the header window
124 static const int HEADER_OFFSET_X
= 1;
125 static const int HEADER_OFFSET_Y
= 1;
127 // when autosizing the columns, add some slack
128 static const int AUTOSIZE_COL_MARGIN
= 10;
130 // default and minimal widths for the header columns
131 static const int WIDTH_COL_DEFAULT
= 80;
132 static const int WIDTH_COL_MIN
= 10;
134 // the space between the image and the text in the report mode
135 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
137 // ============================================================================
139 // ============================================================================
141 //-----------------------------------------------------------------------------
142 // wxListItemData (internal)
143 //-----------------------------------------------------------------------------
145 class WXDLLEXPORT wxListItemData
148 wxListItemData(wxListMainWindow
*owner
);
151 void SetItem( const wxListItem
&info
);
152 void SetImage( int image
) { m_image
= image
; }
153 void SetData( long data
) { m_data
= data
; }
154 void SetPosition( int x
, int y
);
155 void SetSize( int width
, int height
);
157 bool HasText() const { return !m_text
.empty(); }
158 const wxString
& GetText() const { return m_text
; }
159 void SetText(const wxString
& text
) { m_text
= text
; }
161 // we can't use empty string for measuring the string width/height, so
162 // always return something
163 wxString
GetTextForMeasuring() const
165 wxString s
= GetText();
172 bool IsHit( int x
, int y
) const;
176 int GetWidth() const;
177 int GetHeight() const;
179 int GetImage() const { return m_image
; }
180 bool HasImage() const { return GetImage() != -1; }
182 void GetItem( wxListItem
&info
) const;
184 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
185 wxListItemAttr
*GetAttr() const { return m_attr
; }
188 // the item image or -1
191 // user data associated with the item
194 // the item coordinates are not used in report mode, instead this pointer
195 // is NULL and the owner window is used to retrieve the item position and
199 // the list ctrl we are in
200 wxListMainWindow
*m_owner
;
202 // custom attributes or NULL
203 wxListItemAttr
*m_attr
;
206 // common part of all ctors
212 //-----------------------------------------------------------------------------
213 // wxListHeaderData (internal)
214 //-----------------------------------------------------------------------------
216 class WXDLLEXPORT wxListHeaderData
: public wxObject
220 wxListHeaderData( const wxListItem
&info
);
221 void SetItem( const wxListItem
&item
);
222 void SetPosition( int x
, int y
);
223 void SetWidth( int w
);
224 void SetFormat( int format
);
225 void SetHeight( int h
);
226 bool HasImage() const;
228 bool HasText() const { return !m_text
.empty(); }
229 const wxString
& GetText() const { return m_text
; }
230 void SetText(const wxString
& text
) { m_text
= text
; }
232 void GetItem( wxListItem
&item
);
234 bool IsHit( int x
, int y
) const;
235 int GetImage() const;
236 int GetWidth() const;
237 int GetFormat() const;
253 //-----------------------------------------------------------------------------
254 // wxListLineData (internal)
255 //-----------------------------------------------------------------------------
257 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
258 #include "wx/listimpl.cpp"
259 WX_DEFINE_LIST(wxListItemDataList
);
261 class WXDLLEXPORT wxListLineData
264 // the list of subitems: only may have more than one item in report mode
265 wxListItemDataList m_items
;
267 // this is not used in report view
279 // the part to be highlighted
280 wxRect m_rectHighlight
;
283 // is this item selected? [NB: not used in virtual mode]
286 // back pointer to the list ctrl
287 wxListMainWindow
*m_owner
;
290 wxListLineData(wxListMainWindow
*owner
);
294 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
298 // are we in report mode?
299 inline bool InReportView() const;
301 // are we in virtual report mode?
302 inline bool IsVirtual() const;
304 // these 2 methods shouldn't be called for report view controls, in that
305 // case we determine our position/size ourselves
307 // calculate the size of the line
308 void CalculateSize( wxDC
*dc
, int spacing
);
310 // remember the position this line appears at
311 void SetPosition( int x
, int y
, int window_width
, int spacing
);
315 void SetImage( int image
) { SetImage(0, image
); }
316 int GetImage() const { return GetImage(0); }
317 bool HasImage() const { return GetImage() != -1; }
318 bool HasText() const { return !GetText(0).empty(); }
320 void SetItem( int index
, const wxListItem
&info
);
321 void GetItem( int index
, wxListItem
&info
);
323 wxString
GetText(int index
) const;
324 void SetText( int index
, const wxString s
);
326 wxListItemAttr
*GetAttr() const;
327 void SetAttr(wxListItemAttr
*attr
);
329 // return true if the highlighting really changed
330 bool Highlight( bool on
);
332 void ReverseHighlight();
334 bool IsHighlighted() const
336 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
338 return m_highlighted
;
341 // draw the line on the given DC in icon/list mode
342 void Draw( wxDC
*dc
);
344 // the same in report mode
345 void DrawInReportMode( wxDC
*dc
,
347 const wxRect
& rectHL
,
351 // set the line to contain num items (only can be > 1 in report mode)
352 void InitItems( int num
);
354 // get the mode (i.e. style) of the list control
355 inline int GetMode() const;
357 // prepare the DC for drawing with these item's attributes, return true if
358 // we need to draw the items background to highlight it, false otherwise
359 bool SetAttributes(wxDC
*dc
,
360 const wxListItemAttr
*attr
,
363 // draw the text on the DC with the correct justification; also add an
364 // ellipsis if the text is too large to fit in the current width
365 void DrawTextFormatted(wxDC
*dc
, const wxString
&text
, int col
, int x
, int y
, int width
);
367 // these are only used by GetImage/SetImage above, we don't support images
368 // with subitems at the public API level yet
369 void SetImage( int index
, int image
);
370 int GetImage( int index
) const;
373 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
374 #include "wx/arrimpl.cpp"
375 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
377 //-----------------------------------------------------------------------------
378 // wxListHeaderWindow (internal)
379 //-----------------------------------------------------------------------------
381 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
384 wxListMainWindow
*m_owner
;
385 wxCursor
*m_currentCursor
;
386 wxCursor
*m_resizeCursor
;
389 // column being resized or -1
392 // divider line position in logical (unscrolled) coords
395 // minimal position beyond which the divider line can't be dragged in
400 wxListHeaderWindow();
402 wxListHeaderWindow( wxWindow
*win
,
404 wxListMainWindow
*owner
,
405 const wxPoint
&pos
= wxDefaultPosition
,
406 const wxSize
&size
= wxDefaultSize
,
408 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
410 virtual ~wxListHeaderWindow();
412 void DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
);
414 void AdjustDC(wxDC
& dc
);
416 void OnPaint( wxPaintEvent
&event
);
417 void OnMouse( wxMouseEvent
&event
);
418 void OnSetFocus( wxFocusEvent
&event
);
424 // common part of all ctors
427 // generate and process the list event of the given type, return true if
428 // it wasn't vetoed, i.e. if we should proceed
429 bool SendListEvent(wxEventType type
, wxPoint pos
);
431 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
432 DECLARE_EVENT_TABLE()
435 //-----------------------------------------------------------------------------
436 // wxListRenameTimer (internal)
437 //-----------------------------------------------------------------------------
439 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
442 wxListMainWindow
*m_owner
;
445 wxListRenameTimer( wxListMainWindow
*owner
);
449 //-----------------------------------------------------------------------------
450 // wxListTextCtrl (internal)
451 //-----------------------------------------------------------------------------
453 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
456 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
459 void OnChar( wxKeyEvent
&event
);
460 void OnKeyUp( wxKeyEvent
&event
);
461 void OnKillFocus( wxFocusEvent
&event
);
463 bool AcceptChanges();
467 wxListMainWindow
*m_owner
;
468 wxString m_startValue
;
472 DECLARE_EVENT_TABLE()
475 //-----------------------------------------------------------------------------
476 // wxListMainWindow (internal)
477 //-----------------------------------------------------------------------------
479 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
480 #include "wx/listimpl.cpp"
481 WX_DEFINE_LIST(wxListHeaderDataList
);
483 class WXDLLEXPORT wxListMainWindow
: public wxScrolledWindow
487 wxListMainWindow( wxWindow
*parent
,
489 const wxPoint
& pos
= wxDefaultPosition
,
490 const wxSize
& size
= wxDefaultSize
,
492 const wxString
&name
= _T("listctrlmainwindow") );
494 virtual ~wxListMainWindow();
496 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
498 // return true if this is a virtual list control
499 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
501 // return true if the control is in report mode
502 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
504 // return true if we are in single selection mode, false if multi sel
505 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
507 // do we have a header window?
508 bool HasHeader() const
509 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
511 void HighlightAll( bool on
);
513 // all these functions only do something if the line is currently visible
515 // change the line "selected" state, return TRUE if it really changed
516 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
518 // as HighlightLine() but do it for the range of lines: this is incredibly
519 // more efficient for virtual list controls!
521 // NB: unlike HighlightLine() this one does refresh the lines on screen
522 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
524 // toggle the line state and refresh it
525 void ReverseHighlight( size_t line
)
526 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
528 // return true if the line is highlighted
529 bool IsHighlighted(size_t line
) const;
531 // refresh one or several lines at once
532 void RefreshLine( size_t line
);
533 void RefreshLines( size_t lineFrom
, size_t lineTo
);
535 // refresh all selected items
536 void RefreshSelected();
538 // refresh all lines below the given one: the difference with
539 // RefreshLines() is that the index here might not be a valid one (happens
540 // when the last line is deleted)
541 void RefreshAfter( size_t lineFrom
);
543 // the methods which are forwarded to wxListLineData itself in list/icon
544 // modes but are here because the lines don't store their positions in the
547 // get the bound rect for the entire line
548 wxRect
GetLineRect(size_t line
) const;
550 // get the bound rect of the label
551 wxRect
GetLineLabelRect(size_t line
) const;
553 // get the bound rect of the items icon (only may be called if we do have
555 wxRect
GetLineIconRect(size_t line
) const;
557 // get the rect to be highlighted when the item has focus
558 wxRect
GetLineHighlightRect(size_t line
) const;
560 // get the size of the total line rect
561 wxSize
GetLineSize(size_t line
) const
562 { return GetLineRect(line
).GetSize(); }
564 // return the hit code for the corresponding position (in this line)
565 long HitTestLine(size_t line
, int x
, int y
) const;
567 // bring the selected item into view, scrolling to it if necessary
568 void MoveToItem(size_t item
);
570 // bring the current item into view
571 void MoveToFocus() { MoveToItem(m_current
); }
573 // start editing the label of the given item
574 void EditLabel( long item
);
576 // suspend/resume redrawing the control
582 void OnRenameTimer();
583 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
584 void OnRenameCancelled(size_t itemEdit
);
586 void OnMouse( wxMouseEvent
&event
);
588 // called to switch the selection from the current item to newCurrent,
589 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
591 void OnChar( wxKeyEvent
&event
);
592 void OnKeyDown( wxKeyEvent
&event
);
593 void OnSetFocus( wxFocusEvent
&event
);
594 void OnKillFocus( wxFocusEvent
&event
);
595 void OnScroll(wxScrollWinEvent
& event
) ;
597 void OnPaint( wxPaintEvent
&event
);
599 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
600 void GetImageSize( int index
, int &width
, int &height
) const;
601 int GetTextLength( const wxString
&s
) const;
603 void SetImageList( wxImageListType
*imageList
, int which
);
604 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
605 int GetItemSpacing( bool isSmall
= FALSE
);
607 void SetColumn( int col
, wxListItem
&item
);
608 void SetColumnWidth( int col
, int width
);
609 void GetColumn( int col
, wxListItem
&item
) const;
610 int GetColumnWidth( int col
) const;
611 int GetColumnCount() const { return m_columns
.GetCount(); }
613 // returns the sum of the heights of all columns
614 int GetHeaderWidth() const;
616 int GetCountPerPage() const;
618 void SetItem( wxListItem
&item
);
619 void GetItem( wxListItem
&item
) const;
620 void SetItemState( long item
, long state
, long stateMask
);
621 int GetItemState( long item
, long stateMask
) const;
622 void GetItemRect( long index
, wxRect
&rect
) const;
623 bool GetItemPosition( long item
, wxPoint
& pos
) const;
624 int GetSelectedItemCount() const;
626 wxString
GetItemText(long item
) const
629 info
.m_itemId
= item
;
634 void SetItemText(long item
, const wxString
& value
)
637 info
.m_mask
= wxLIST_MASK_TEXT
;
638 info
.m_itemId
= item
;
643 // set the scrollbars and update the positions of the items
644 void RecalculatePositions(bool noRefresh
= FALSE
);
646 // refresh the window and the header
649 long GetNextItem( long item
, int geometry
, int state
) const;
650 void DeleteItem( long index
);
651 void DeleteAllItems();
652 void DeleteColumn( int col
);
653 void DeleteEverything();
654 void EnsureVisible( long index
);
655 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
656 long FindItem( long start
, long data
);
657 long HitTest( int x
, int y
, int &flags
);
658 void InsertItem( wxListItem
&item
);
659 void InsertColumn( long col
, wxListItem
&item
);
660 void SortItems( wxListCtrlCompare fn
, long data
);
662 size_t GetItemCount() const;
663 bool IsEmpty() const { return GetItemCount() == 0; }
664 void SetItemCount(long count
);
666 // change the current (== focused) item, send a notification event
667 void ChangeCurrent(size_t current
);
668 void ResetCurrent() { ChangeCurrent((size_t)-1); }
669 bool HasCurrent() const { return m_current
!= (size_t)-1; }
671 // send out a wxListEvent
672 void SendNotify( size_t line
,
674 wxPoint point
= wxDefaultPosition
);
676 // override base class virtual to reset m_lineHeight when the font changes
677 virtual bool SetFont(const wxFont
& font
)
679 if ( !wxScrolledWindow::SetFont(font
) )
687 // these are for wxListLineData usage only
689 // get the backpointer to the list ctrl
690 wxGenericListCtrl
*GetListCtrl() const
692 return wxStaticCast(GetParent(), wxGenericListCtrl
);
695 // get the height of all lines (assuming they all do have the same height)
696 wxCoord
GetLineHeight() const;
698 // get the y position of the given line (only for report view)
699 wxCoord
GetLineY(size_t line
) const;
701 // get the brush to use for the item highlighting
702 wxBrush
*GetHighlightBrush() const
704 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
708 // the array of all line objects for a non virtual list control (for the
709 // virtual list control we only ever use m_lines[0])
710 wxListLineDataArray m_lines
;
712 // the list of column objects
713 wxListHeaderDataList m_columns
;
715 // currently focused item or -1
718 // the number of lines per page
721 // this flag is set when something which should result in the window
722 // redrawing happens (i.e. an item was added or deleted, or its appearance
723 // changed) and OnPaint() doesn't redraw the window while it is set which
724 // allows to minimize the number of repaintings when a lot of items are
725 // being added. The real repainting occurs only after the next OnIdle()
729 wxColour
*m_highlightColour
;
732 wxImageListType
*m_small_image_list
;
733 wxImageListType
*m_normal_image_list
;
735 int m_normal_spacing
;
739 wxTimer
*m_renameTimer
;
744 // for double click logic
745 size_t m_lineLastClicked
,
746 m_lineBeforeLastClicked
;
749 // the total count of items in a virtual list control
752 // the object maintaining the items selection state, only used in virtual
754 wxSelectionStore m_selStore
;
756 // common part of all ctors
759 // intiialize m_[xy]Scroll
760 void InitScrolling();
762 // get the line data for the given index
763 wxListLineData
*GetLine(size_t n
) const
765 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
769 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
777 // get a dummy line which can be used for geometry calculations and such:
778 // you must use GetLine() if you want to really draw the line
779 wxListLineData
*GetDummyLine() const;
781 // cache the line data of the n-th line in m_lines[0]
782 void CacheLineData(size_t line
);
784 // get the range of visible lines
785 void GetVisibleLinesRange(size_t *from
, size_t *to
);
787 // force us to recalculate the range of visible lines
788 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
790 // get the colour to be used for drawing the rules
791 wxColour
GetRuleColour() const
796 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
801 // initialize the current item if needed
802 void UpdateCurrent();
804 // delete all items but don't refresh: called from dtor
805 void DoDeleteAllItems();
807 // the height of one line using the current font
808 wxCoord m_lineHeight
;
810 // the total header width or 0 if not calculated yet
811 wxCoord m_headerWidth
;
813 // the first and last lines being shown on screen right now (inclusive),
814 // both may be -1 if they must be calculated so never access them directly:
815 // use GetVisibleLinesRange() above instead
819 // the brushes to use for item highlighting when we do/don't have focus
820 wxBrush
*m_highlightBrush
,
821 *m_highlightUnfocusedBrush
;
823 // if this is > 0, the control is frozen and doesn't redraw itself
824 size_t m_freezeCount
;
826 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
827 DECLARE_EVENT_TABLE()
829 friend class wxGenericListCtrl
;
832 // ============================================================================
834 // ============================================================================
836 //-----------------------------------------------------------------------------
838 //-----------------------------------------------------------------------------
840 wxListItemData::~wxListItemData()
842 // in the virtual list control the attributes are managed by the main
843 // program, so don't delete them
844 if ( !m_owner
->IsVirtual() )
852 void wxListItemData::Init()
860 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
866 if ( owner
->InReportView() )
876 void wxListItemData::SetItem( const wxListItem
&info
)
878 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
879 SetText(info
.m_text
);
880 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
881 m_image
= info
.m_image
;
882 if ( info
.m_mask
& wxLIST_MASK_DATA
)
883 m_data
= info
.m_data
;
885 if ( info
.HasAttributes() )
888 *m_attr
= *info
.GetAttributes();
890 m_attr
= new wxListItemAttr(*info
.GetAttributes());
898 m_rect
->width
= info
.m_width
;
902 void wxListItemData::SetPosition( int x
, int y
)
904 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
910 void wxListItemData::SetSize( int width
, int height
)
912 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
915 m_rect
->width
= width
;
917 m_rect
->height
= height
;
920 bool wxListItemData::IsHit( int x
, int y
) const
922 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
924 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
927 int wxListItemData::GetX() const
929 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
934 int wxListItemData::GetY() const
936 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
941 int wxListItemData::GetWidth() const
943 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
945 return m_rect
->width
;
948 int wxListItemData::GetHeight() const
950 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
952 return m_rect
->height
;
955 void wxListItemData::GetItem( wxListItem
&info
) const
957 info
.m_text
= m_text
;
958 info
.m_image
= m_image
;
959 info
.m_data
= m_data
;
963 if ( m_attr
->HasTextColour() )
964 info
.SetTextColour(m_attr
->GetTextColour());
965 if ( m_attr
->HasBackgroundColour() )
966 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
967 if ( m_attr
->HasFont() )
968 info
.SetFont(m_attr
->GetFont());
972 //-----------------------------------------------------------------------------
974 //-----------------------------------------------------------------------------
976 void wxListHeaderData::Init()
987 wxListHeaderData::wxListHeaderData()
992 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
999 void wxListHeaderData::SetItem( const wxListItem
&item
)
1001 m_mask
= item
.m_mask
;
1003 if ( m_mask
& wxLIST_MASK_TEXT
)
1004 m_text
= item
.m_text
;
1006 if ( m_mask
& wxLIST_MASK_IMAGE
)
1007 m_image
= item
.m_image
;
1009 if ( m_mask
& wxLIST_MASK_FORMAT
)
1010 m_format
= item
.m_format
;
1012 if ( m_mask
& wxLIST_MASK_WIDTH
)
1013 SetWidth(item
.m_width
);
1016 void wxListHeaderData::SetPosition( int x
, int y
)
1022 void wxListHeaderData::SetHeight( int h
)
1027 void wxListHeaderData::SetWidth( int w
)
1031 m_width
= WIDTH_COL_DEFAULT
;
1032 else if (m_width
< WIDTH_COL_MIN
)
1033 m_width
= WIDTH_COL_MIN
;
1036 void wxListHeaderData::SetFormat( int format
)
1041 bool wxListHeaderData::HasImage() const
1043 return m_image
!= -1;
1046 bool wxListHeaderData::IsHit( int x
, int y
) const
1048 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1051 void wxListHeaderData::GetItem( wxListItem
& item
)
1053 item
.m_mask
= m_mask
;
1054 item
.m_text
= m_text
;
1055 item
.m_image
= m_image
;
1056 item
.m_format
= m_format
;
1057 item
.m_width
= m_width
;
1060 int wxListHeaderData::GetImage() const
1065 int wxListHeaderData::GetWidth() const
1070 int wxListHeaderData::GetFormat() const
1075 //-----------------------------------------------------------------------------
1077 //-----------------------------------------------------------------------------
1079 inline int wxListLineData::GetMode() const
1081 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1084 inline bool wxListLineData::InReportView() const
1086 return m_owner
->HasFlag(wxLC_REPORT
);
1089 inline bool wxListLineData::IsVirtual() const
1091 return m_owner
->IsVirtual();
1094 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1098 if ( InReportView() )
1104 m_gi
= new GeometryInfo
;
1107 m_highlighted
= FALSE
;
1109 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1112 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1114 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1115 wxCHECK_RET( node
, _T("no subitems at all??") );
1117 wxListItemData
*item
= node
->GetData();
1119 switch ( GetMode() )
1122 case wxLC_SMALL_ICON
:
1124 m_gi
->m_rectAll
.width
= spacing
;
1126 wxString s
= item
->GetText();
1132 m_gi
->m_rectLabel
.width
=
1133 m_gi
->m_rectLabel
.height
= 0;
1137 dc
->GetTextExtent( s
, &lw
, &lh
);
1138 if (lh
< SCROLL_UNIT_Y
)
1143 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1145 m_gi
->m_rectAll
.width
= lw
;
1147 m_gi
->m_rectLabel
.width
= lw
;
1148 m_gi
->m_rectLabel
.height
= lh
;
1151 if (item
->HasImage())
1154 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1155 m_gi
->m_rectIcon
.width
= w
+ 8;
1156 m_gi
->m_rectIcon
.height
= h
+ 8;
1158 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1159 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1160 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1161 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1164 if ( item
->HasText() )
1166 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1167 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1169 else // no text, highlight the icon
1171 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1172 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1179 wxString s
= item
->GetTextForMeasuring();
1182 dc
->GetTextExtent( s
, &lw
, &lh
);
1183 if (lh
< SCROLL_UNIT_Y
)
1188 m_gi
->m_rectLabel
.width
= lw
;
1189 m_gi
->m_rectLabel
.height
= lh
;
1191 m_gi
->m_rectAll
.width
= lw
;
1192 m_gi
->m_rectAll
.height
= lh
;
1194 if (item
->HasImage())
1197 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1198 m_gi
->m_rectIcon
.width
= w
;
1199 m_gi
->m_rectIcon
.height
= h
;
1201 m_gi
->m_rectAll
.width
+= 4 + w
;
1202 if (h
> m_gi
->m_rectAll
.height
)
1203 m_gi
->m_rectAll
.height
= h
;
1206 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1207 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1212 wxFAIL_MSG( _T("unexpected call to SetSize") );
1216 wxFAIL_MSG( _T("unknown mode") );
1220 void wxListLineData::SetPosition( int x
, int y
,
1224 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1225 wxCHECK_RET( node
, _T("no subitems at all??") );
1227 wxListItemData
*item
= node
->GetData();
1229 switch ( GetMode() )
1232 case wxLC_SMALL_ICON
:
1233 m_gi
->m_rectAll
.x
= x
;
1234 m_gi
->m_rectAll
.y
= y
;
1236 if ( item
->HasImage() )
1238 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1239 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1240 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1243 if ( item
->HasText() )
1245 if (m_gi
->m_rectAll
.width
> spacing
)
1246 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1248 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1249 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1250 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1251 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1253 else // no text, highlight the icon
1255 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1256 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1261 m_gi
->m_rectAll
.x
= x
;
1262 m_gi
->m_rectAll
.y
= y
;
1264 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1265 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1266 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1268 if (item
->HasImage())
1270 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1271 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1272 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1276 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1281 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1285 wxFAIL_MSG( _T("unknown mode") );
1289 void wxListLineData::InitItems( int num
)
1291 for (int i
= 0; i
< num
; i
++)
1292 m_items
.Append( new wxListItemData(m_owner
) );
1295 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1297 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1298 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1300 wxListItemData
*item
= node
->GetData();
1301 item
->SetItem( info
);
1304 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1306 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1309 wxListItemData
*item
= node
->GetData();
1310 item
->GetItem( info
);
1314 wxString
wxListLineData::GetText(int index
) const
1318 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1321 wxListItemData
*item
= node
->GetData();
1322 s
= item
->GetText();
1328 void wxListLineData::SetText( int index
, const wxString s
)
1330 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1333 wxListItemData
*item
= node
->GetData();
1338 void wxListLineData::SetImage( int index
, int image
)
1340 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1341 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1343 wxListItemData
*item
= node
->GetData();
1344 item
->SetImage(image
);
1347 int wxListLineData::GetImage( int index
) const
1349 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1350 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1352 wxListItemData
*item
= node
->GetData();
1353 return item
->GetImage();
1356 wxListItemAttr
*wxListLineData::GetAttr() const
1358 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1359 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1361 wxListItemData
*item
= node
->GetData();
1362 return item
->GetAttr();
1365 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1367 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1368 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1370 wxListItemData
*item
= node
->GetData();
1371 item
->SetAttr(attr
);
1374 bool wxListLineData::SetAttributes(wxDC
*dc
,
1375 const wxListItemAttr
*attr
,
1378 wxWindow
*listctrl
= m_owner
->GetParent();
1382 // don't use foreground colour for drawing highlighted items - this might
1383 // make them completely invisible (and there is no way to do bit
1384 // arithmetics on wxColour, unfortunately)
1388 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1392 if ( attr
&& attr
->HasTextColour() )
1394 colText
= attr
->GetTextColour();
1398 colText
= listctrl
->GetForegroundColour();
1402 dc
->SetTextForeground(colText
);
1406 if ( attr
&& attr
->HasFont() )
1408 font
= attr
->GetFont();
1412 font
= listctrl
->GetFont();
1418 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1419 if ( highlighted
|| hasBgCol
)
1423 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1427 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1430 dc
->SetPen( *wxTRANSPARENT_PEN
);
1438 void wxListLineData::Draw( wxDC
*dc
)
1440 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1441 wxCHECK_RET( node
, _T("no subitems at all??") );
1443 bool highlighted
= IsHighlighted();
1445 wxListItemAttr
*attr
= GetAttr();
1447 if ( SetAttributes(dc
, attr
, highlighted
) )
1449 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1452 wxListItemData
*item
= node
->GetData();
1453 if (item
->HasImage())
1455 wxRect rectIcon
= m_gi
->m_rectIcon
;
1456 m_owner
->DrawImage( item
->GetImage(), dc
,
1457 rectIcon
.x
, rectIcon
.y
);
1460 if (item
->HasText())
1462 wxRect rectLabel
= m_gi
->m_rectLabel
;
1464 wxDCClipper
clipper(*dc
, rectLabel
);
1465 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1469 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1471 const wxRect
& rectHL
,
1474 // TODO: later we should support setting different attributes for
1475 // different columns - to do it, just add "col" argument to
1476 // GetAttr() and move these lines into the loop below
1477 wxListItemAttr
*attr
= GetAttr();
1478 if ( SetAttributes(dc
, attr
, highlighted
) )
1480 dc
->DrawRectangle( rectHL
);
1483 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1484 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1487 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1489 node
= node
->GetNext(), col
++ )
1491 wxListItemData
*item
= node
->GetData();
1493 int width
= m_owner
->GetColumnWidth(col
);
1497 if ( item
->HasImage() )
1500 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1501 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1503 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1509 wxDCClipper
clipper(*dc
, xOld
, y
, width
- 8, rect
.height
);
1511 if ( item
->HasText() )
1513 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, y
, width
- 8);
1518 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1519 const wxString
&text
,
1525 wxString drawntext
, ellipsis
;
1526 wxCoord w
, h
, base_w
;
1529 // determine if the string can fit inside the current width
1530 dc
->GetTextExtent(text
, &w
, &h
);
1533 // it can, draw it using the items alignment
1534 m_owner
->GetColumn(col
, item
);
1535 switch ( item
.GetAlign() )
1538 wxFAIL_MSG( _T("unknown list item format") );
1541 case wxLIST_FORMAT_LEFT
:
1545 case wxLIST_FORMAT_RIGHT
:
1549 case wxLIST_FORMAT_CENTER
:
1550 x
+= (width
- w
) / 2;
1554 dc
->DrawText(text
, x
, y
);
1556 else // otherwise, truncate and add an ellipsis if possible
1558 // determine the base width
1559 ellipsis
= wxString(wxT("..."));
1560 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1562 // continue until we have enough space or only one character left
1564 size_t len
= text
.Length();
1565 drawntext
= text
.Left(len
);
1568 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1569 drawntext
.RemoveLast();
1572 if (w
+ base_w
<= width
)
1576 // if still not enough space, remove ellipsis characters
1577 while (ellipsis
.Length() > 0 && w
+ base_w
> width
)
1579 ellipsis
= ellipsis
.Left(ellipsis
.Length() - 1);
1580 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1583 // now draw the text
1584 dc
->DrawText(drawntext
, x
, y
);
1585 dc
->DrawText(ellipsis
, x
+ w
, y
);
1589 bool wxListLineData::Highlight( bool on
)
1591 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1593 if ( on
== m_highlighted
)
1601 void wxListLineData::ReverseHighlight( void )
1603 Highlight(!IsHighlighted());
1606 //-----------------------------------------------------------------------------
1607 // wxListHeaderWindow
1608 //-----------------------------------------------------------------------------
1610 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1612 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1613 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1614 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1615 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1618 void wxListHeaderWindow::Init()
1620 m_currentCursor
= (wxCursor
*) NULL
;
1621 m_isDragging
= FALSE
;
1625 wxListHeaderWindow::wxListHeaderWindow()
1629 m_owner
= (wxListMainWindow
*) NULL
;
1630 m_resizeCursor
= (wxCursor
*) NULL
;
1633 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1635 wxListMainWindow
*owner
,
1639 const wxString
&name
)
1640 : wxWindow( win
, id
, pos
, size
, style
, name
)
1645 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1647 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1650 wxListHeaderWindow::~wxListHeaderWindow()
1652 delete m_resizeCursor
;
1655 #ifdef __WXUNIVERSAL__
1656 #include "wx/univ/renderer.h"
1657 #include "wx/univ/theme.h"
1660 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1662 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
1663 GtkStateType state
= m_parent
->IsEnabled() ? GTK_STATE_NORMAL
1664 : GTK_STATE_INSENSITIVE
;
1666 x
= dc
->XLOG2DEV( x
);
1668 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
,
1669 state
, GTK_SHADOW_OUT
,
1670 (GdkRectangle
*) NULL
, m_wxwindow
,
1671 (char *)"button", // const_cast
1672 x
-1, y
-1, w
+2, h
+2);
1673 #elif defined(__WXUNIVERSAL__)
1674 wxTheme
*theme
= wxTheme::Get();
1675 wxRenderer
*renderer
= theme
->GetRenderer();
1676 renderer
->DrawBorder( *dc
, wxBORDER_RAISED
, wxRect(x
,y
,w
,h
), 0 );
1677 #elif defined(__WXMAC__)
1678 const int m_corner
= 1;
1680 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1682 dc
->SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1683 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1684 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1686 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1689 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1690 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1692 dc
->SetPen( *wxWHITE_PEN
);
1693 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1694 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1695 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1696 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1698 const int m_corner
= 1;
1700 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1702 dc
->SetPen( *wxBLACK_PEN
);
1703 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1704 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1706 wxPen
pen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1709 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1710 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1712 dc
->SetPen( *wxWHITE_PEN
);
1713 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1714 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1715 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1716 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1720 // shift the DC origin to match the position of the main window horz
1721 // scrollbar: this allows us to always use logical coords
1722 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1725 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1728 m_owner
->GetViewStart( &x
, NULL
);
1730 // account for the horz scrollbar offset
1731 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1734 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1736 #if defined(__WXGTK__)
1737 wxClientDC
dc( this );
1739 wxPaintDC
dc( this );
1747 dc
.SetFont( GetFont() );
1749 // width and height of the entire header window
1751 GetClientSize( &w
, &h
);
1752 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1754 dc
.SetBackgroundMode(wxTRANSPARENT
);
1756 // do *not* use the listctrl colour for headers - one day we will have a
1757 // function to set it separately
1758 //dc.SetTextForeground( *wxBLACK );
1759 dc
.SetTextForeground(wxSystemSettings::
1760 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1762 int x
= HEADER_OFFSET_X
;
1764 int numColumns
= m_owner
->GetColumnCount();
1766 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1768 m_owner
->GetColumn( i
, item
);
1769 int wCol
= item
.m_width
;
1771 // the width of the rect to draw: make it smaller to fit entirely
1772 // inside the column rect
1775 dc
.SetPen( *wxWHITE_PEN
);
1777 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
1779 // see if we have enough space for the column label
1781 // for this we need the width of the text
1783 dc
.GetTextExtent(item
.GetText(), &wLabel
, NULL
);
1784 wLabel
+= 2*EXTRA_WIDTH
;
1786 // and the width of the icon, if any
1787 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
1788 int ix
= 0, // init them just to suppress the compiler warnings
1790 const int image
= item
.m_image
;
1791 wxImageListType
*imageList
;
1794 imageList
= m_owner
->m_small_image_list
;
1797 imageList
->GetSize(image
, ix
, iy
);
1798 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1806 // ignore alignment if there is not enough space anyhow
1808 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1811 wxFAIL_MSG( _T("unknown list item format") );
1814 case wxLIST_FORMAT_LEFT
:
1818 case wxLIST_FORMAT_RIGHT
:
1819 xAligned
= x
+ cw
- wLabel
;
1822 case wxLIST_FORMAT_CENTER
:
1823 xAligned
= x
+ (cw
- wLabel
) / 2;
1828 // if we have an image, draw it on the right of the label
1835 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
1836 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1837 wxIMAGELIST_DRAW_TRANSPARENT
1840 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1843 // draw the text clipping it so that it doesn't overwrite the column
1845 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1847 dc
.DrawText( item
.GetText(),
1848 xAligned
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1856 void wxListHeaderWindow::DrawCurrent()
1858 int x1
= m_currentX
;
1860 m_owner
->ClientToScreen( &x1
, &y1
);
1862 int x2
= m_currentX
;
1864 m_owner
->GetClientSize( NULL
, &y2
);
1865 m_owner
->ClientToScreen( &x2
, &y2
);
1868 dc
.SetLogicalFunction( wxINVERT
);
1869 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1870 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1874 dc
.DrawLine( x1
, y1
, x2
, y2
);
1876 dc
.SetLogicalFunction( wxCOPY
);
1878 dc
.SetPen( wxNullPen
);
1879 dc
.SetBrush( wxNullBrush
);
1882 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1884 // we want to work with logical coords
1886 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1887 int y
= event
.GetY();
1891 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1893 // we don't draw the line beyond our window, but we allow dragging it
1896 GetClientSize( &w
, NULL
);
1897 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1900 // erase the line if it was drawn
1901 if ( m_currentX
< w
)
1904 if (event
.ButtonUp())
1907 m_isDragging
= FALSE
;
1909 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1910 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1917 m_currentX
= m_minX
+ 7;
1919 // draw in the new location
1920 if ( m_currentX
< w
)
1924 else // not dragging
1927 bool hit_border
= FALSE
;
1929 // end of the current column
1932 // find the column where this event occured
1934 countCol
= m_owner
->GetColumnCount();
1935 for (col
= 0; col
< countCol
; col
++)
1937 xpos
+= m_owner
->GetColumnWidth( col
);
1940 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1942 // near the column border
1949 // inside the column
1956 if ( col
== countCol
)
1959 if (event
.LeftDown() || event
.RightUp())
1961 if (hit_border
&& event
.LeftDown())
1963 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1964 event
.GetPosition()) )
1966 m_isDragging
= TRUE
;
1971 //else: column resizing was vetoed by the user code
1973 else // click on a column
1975 SendListEvent( event
.LeftDown()
1976 ? wxEVT_COMMAND_LIST_COL_CLICK
1977 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1978 event
.GetPosition());
1981 else if (event
.Moving())
1986 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1987 m_currentCursor
= m_resizeCursor
;
1991 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1992 m_currentCursor
= wxSTANDARD_CURSOR
;
1996 SetCursor(*m_currentCursor
);
2001 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2003 m_owner
->SetFocus();
2006 bool wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
2008 wxWindow
*parent
= GetParent();
2009 wxListEvent
le( type
, parent
->GetId() );
2010 le
.SetEventObject( parent
);
2011 le
.m_pointDrag
= pos
;
2013 // the position should be relative to the parent window, not
2014 // this one for compatibility with MSW and common sense: the
2015 // user code doesn't know anything at all about this header
2016 // window, so why should it get positions relative to it?
2017 le
.m_pointDrag
.y
-= GetSize().y
;
2019 le
.m_col
= m_column
;
2020 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2023 //-----------------------------------------------------------------------------
2024 // wxListRenameTimer (internal)
2025 //-----------------------------------------------------------------------------
2027 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2032 void wxListRenameTimer::Notify()
2034 m_owner
->OnRenameTimer();
2037 //-----------------------------------------------------------------------------
2038 // wxListTextCtrl (internal)
2039 //-----------------------------------------------------------------------------
2041 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2042 EVT_CHAR (wxListTextCtrl::OnChar
)
2043 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2044 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2047 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
2048 : m_startValue(owner
->GetItemText(itemEdit
)),
2049 m_itemEdited(itemEdit
)
2054 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2056 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2057 &rectLabel
.x
, &rectLabel
.y
);
2059 (void)Create(owner
, wxID_ANY
, m_startValue
,
2060 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2061 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2064 void wxListTextCtrl::Finish()
2068 wxPendingDelete
.Append(this);
2072 m_owner
->SetFocus();
2076 bool wxListTextCtrl::AcceptChanges()
2078 const wxString value
= GetValue();
2080 if ( value
== m_startValue
)
2082 // nothing changed, always accept
2086 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2088 // vetoed by the user
2092 // accepted, do rename the item
2093 m_owner
->SetItemText(m_itemEdited
, value
);
2098 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2100 switch ( event
.m_keyCode
)
2103 if ( !AcceptChanges() )
2105 // vetoed by the user code
2108 //else: fall through
2112 m_owner
->OnRenameCancelled( m_itemEdited
);
2120 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2128 // auto-grow the textctrl:
2129 wxSize parentSize
= m_owner
->GetSize();
2130 wxPoint myPos
= GetPosition();
2131 wxSize mySize
= GetSize();
2133 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2134 if (myPos
.x
+ sx
> parentSize
.x
)
2135 sx
= parentSize
.x
- myPos
.x
;
2143 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2147 // We must finish regardless of success, otherwise we'll get focus problems
2150 if ( !AcceptChanges() )
2151 m_owner
->OnRenameCancelled( m_itemEdited
);
2157 //-----------------------------------------------------------------------------
2159 //-----------------------------------------------------------------------------
2161 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2163 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2164 EVT_PAINT (wxListMainWindow::OnPaint
)
2165 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2166 EVT_CHAR (wxListMainWindow::OnChar
)
2167 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2168 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2169 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2170 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2173 void wxListMainWindow::Init()
2178 m_lineTo
= (size_t)-1;
2184 m_small_image_list
= (wxImageListType
*) NULL
;
2185 m_normal_image_list
= (wxImageListType
*) NULL
;
2187 m_small_spacing
= 30;
2188 m_normal_spacing
= 40;
2192 m_isCreated
= FALSE
;
2194 m_lastOnSame
= FALSE
;
2195 m_renameTimer
= new wxListRenameTimer( this );
2199 m_lineBeforeLastClicked
= (size_t)-1;
2204 void wxListMainWindow::InitScrolling()
2206 if ( HasFlag(wxLC_REPORT
) )
2208 m_xScroll
= SCROLL_UNIT_X
;
2209 m_yScroll
= SCROLL_UNIT_Y
;
2213 m_xScroll
= SCROLL_UNIT_Y
;
2218 wxListMainWindow::wxListMainWindow()
2223 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2229 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2234 const wxString
&name
)
2235 : wxScrolledWindow( parent
, id
, pos
, size
,
2236 style
| wxHSCROLL
| wxVSCROLL
, name
)
2240 m_highlightBrush
= new wxBrush
2242 wxSystemSettings::GetColour
2244 wxSYS_COLOUR_HIGHLIGHT
2249 m_highlightUnfocusedBrush
= new wxBrush
2251 wxSystemSettings::GetColour
2253 wxSYS_COLOUR_BTNSHADOW
2262 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2264 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2267 wxListMainWindow::~wxListMainWindow()
2270 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2272 delete m_highlightBrush
;
2273 delete m_highlightUnfocusedBrush
;
2275 delete m_renameTimer
;
2278 void wxListMainWindow::CacheLineData(size_t line
)
2280 wxGenericListCtrl
*listctrl
= GetListCtrl();
2282 wxListLineData
*ld
= GetDummyLine();
2284 size_t countCol
= GetColumnCount();
2285 for ( size_t col
= 0; col
< countCol
; col
++ )
2287 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2290 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2291 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2294 wxListLineData
*wxListMainWindow::GetDummyLine() const
2296 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2298 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2300 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2302 // we need to recreate the dummy line if the number of columns in the
2303 // control changed as it would have the incorrect number of fields
2305 if ( !m_lines
.IsEmpty() &&
2306 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2308 self
->m_lines
.Clear();
2311 if ( m_lines
.IsEmpty() )
2313 wxListLineData
*line
= new wxListLineData(self
);
2314 self
->m_lines
.Add(line
);
2316 // don't waste extra memory -- there never going to be anything
2317 // else/more in this array
2318 self
->m_lines
.Shrink();
2324 // ----------------------------------------------------------------------------
2325 // line geometry (report mode only)
2326 // ----------------------------------------------------------------------------
2328 wxCoord
wxListMainWindow::GetLineHeight() const
2330 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2332 // we cache the line height as calling GetTextExtent() is slow
2333 if ( !m_lineHeight
)
2335 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2337 wxClientDC
dc( self
);
2338 dc
.SetFont( GetFont() );
2341 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2343 if ( y
< SCROLL_UNIT_Y
)
2346 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2350 m_small_image_list
->GetSize(0, iw
, ih
);
2355 self
->m_lineHeight
= y
+ LINE_SPACING
;
2358 return m_lineHeight
;
2361 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2363 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2365 return LINE_SPACING
+ line
*GetLineHeight();
2368 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2370 if ( !InReportView() )
2371 return GetLine(line
)->m_gi
->m_rectAll
;
2374 rect
.x
= HEADER_OFFSET_X
;
2375 rect
.y
= GetLineY(line
);
2376 rect
.width
= GetHeaderWidth();
2377 rect
.height
= GetLineHeight();
2382 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2384 if ( !InReportView() )
2385 return GetLine(line
)->m_gi
->m_rectLabel
;
2388 rect
.x
= HEADER_OFFSET_X
;
2389 rect
.y
= GetLineY(line
);
2390 rect
.width
= GetColumnWidth(0);
2391 rect
.height
= GetLineHeight();
2396 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2398 if ( !InReportView() )
2399 return GetLine(line
)->m_gi
->m_rectIcon
;
2401 wxListLineData
*ld
= GetLine(line
);
2402 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2405 rect
.x
= HEADER_OFFSET_X
;
2406 rect
.y
= GetLineY(line
);
2407 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2412 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2414 return InReportView() ? GetLineRect(line
)
2415 : GetLine(line
)->m_gi
->m_rectHighlight
;
2418 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2420 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2422 wxListLineData
*ld
= GetLine(line
);
2424 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2425 return wxLIST_HITTEST_ONITEMICON
;
2427 // VS: Testing for "ld->HasText() || InReportView()" instead of
2428 // "ld->HasText()" is needed to make empty lines in report view
2430 if ( ld
->HasText() || InReportView() )
2432 wxRect rect
= InReportView() ? GetLineRect(line
)
2433 : GetLineLabelRect(line
);
2435 if ( rect
.Inside(x
, y
) )
2436 return wxLIST_HITTEST_ONITEMLABEL
;
2442 // ----------------------------------------------------------------------------
2443 // highlight (selection) handling
2444 // ----------------------------------------------------------------------------
2446 bool wxListMainWindow::IsHighlighted(size_t line
) const
2450 return m_selStore
.IsSelected(line
);
2454 wxListLineData
*ld
= GetLine(line
);
2455 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2457 return ld
->IsHighlighted();
2461 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2467 wxArrayInt linesChanged
;
2468 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2471 // meny items changed state, refresh everything
2472 RefreshLines(lineFrom
, lineTo
);
2474 else // only a few items changed state, refresh only them
2476 size_t count
= linesChanged
.GetCount();
2477 for ( size_t n
= 0; n
< count
; n
++ )
2479 RefreshLine(linesChanged
[n
]);
2483 else // iterate over all items in non report view
2485 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2487 if ( HighlightLine(line
, highlight
) )
2495 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2501 changed
= m_selStore
.SelectItem(line
, highlight
);
2505 wxListLineData
*ld
= GetLine(line
);
2506 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2508 changed
= ld
->Highlight(highlight
);
2513 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2514 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2520 void wxListMainWindow::RefreshLine( size_t line
)
2522 if ( HasFlag(wxLC_REPORT
) )
2524 size_t visibleFrom
, visibleTo
;
2525 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2527 if ( line
< visibleFrom
|| line
> visibleTo
)
2531 wxRect rect
= GetLineRect(line
);
2533 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2534 RefreshRect( rect
);
2537 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2539 // we suppose that they are ordered by caller
2540 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2542 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2544 if ( HasFlag(wxLC_REPORT
) )
2546 size_t visibleFrom
, visibleTo
;
2547 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2549 if ( lineFrom
< visibleFrom
)
2550 lineFrom
= visibleFrom
;
2551 if ( lineTo
> visibleTo
)
2556 rect
.y
= GetLineY(lineFrom
);
2557 rect
.width
= GetClientSize().x
;
2558 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2560 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2561 RefreshRect( rect
);
2565 // TODO: this should be optimized...
2566 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2573 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2575 if ( HasFlag(wxLC_REPORT
) )
2577 size_t visibleFrom
, visibleTo
;
2578 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2580 if ( lineFrom
< visibleFrom
)
2581 lineFrom
= visibleFrom
;
2582 else if ( lineFrom
> visibleTo
)
2587 rect
.y
= GetLineY(lineFrom
);
2588 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2590 wxSize size
= GetClientSize();
2591 rect
.width
= size
.x
;
2592 // refresh till the bottom of the window
2593 rect
.height
= size
.y
- rect
.y
;
2595 RefreshRect( rect
);
2599 // TODO: how to do it more efficiently?
2604 void wxListMainWindow::RefreshSelected()
2610 if ( InReportView() )
2612 GetVisibleLinesRange(&from
, &to
);
2617 to
= GetItemCount() - 1;
2620 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2622 RefreshLine(m_current
);
2625 for ( size_t line
= from
; line
<= to
; line
++ )
2627 // NB: the test works as expected even if m_current == -1
2628 if ( line
!= m_current
&& IsHighlighted(line
) )
2635 void wxListMainWindow::Freeze()
2640 void wxListMainWindow::Thaw()
2642 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2644 if ( !--m_freezeCount
)
2650 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2652 // Note: a wxPaintDC must be constructed even if no drawing is
2653 // done (a Windows requirement).
2654 wxPaintDC
dc( this );
2656 if ( IsEmpty() || m_freezeCount
)
2658 // nothing to draw or not the moment to draw it
2664 // delay the repainting until we calculate all the items positions
2671 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2675 dc
.SetFont( GetFont() );
2677 if ( HasFlag(wxLC_REPORT
) )
2679 int lineHeight
= GetLineHeight();
2681 size_t visibleFrom
, visibleTo
;
2682 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2685 wxCoord xOrig
, yOrig
;
2686 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2688 // tell the caller cache to cache the data
2691 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2692 GetParent()->GetId());
2693 evCache
.SetEventObject( GetParent() );
2694 evCache
.m_oldItemIndex
= visibleFrom
;
2695 evCache
.m_itemIndex
= visibleTo
;
2696 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2699 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2701 rectLine
= GetLineRect(line
);
2703 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2704 rectLine
.width
, rectLine
.height
) )
2706 // don't redraw unaffected lines to avoid flicker
2710 GetLine(line
)->DrawInReportMode( &dc
,
2712 GetLineHighlightRect(line
),
2713 IsHighlighted(line
) );
2716 if ( HasFlag(wxLC_HRULES
) )
2718 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2719 wxSize clientSize
= GetClientSize();
2721 // Don't draw the first one
2722 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2725 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2726 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2727 clientSize
.x
- dev_x
, i
*lineHeight
);
2730 // Draw last horizontal rule
2731 if ( visibleTo
== GetItemCount() - 1 )
2734 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2735 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2736 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2740 // Draw vertical rules if required
2741 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2743 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2746 wxRect firstItemRect
;
2747 wxRect lastItemRect
;
2748 GetItemRect(visibleFrom
, firstItemRect
);
2749 GetItemRect(visibleTo
, lastItemRect
);
2750 int x
= firstItemRect
.GetX();
2752 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2753 for (col
= 0; col
< GetColumnCount(); col
++)
2755 int colWidth
= GetColumnWidth(col
);
2757 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2758 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2764 size_t count
= GetItemCount();
2765 for ( size_t i
= 0; i
< count
; i
++ )
2767 GetLine(i
)->Draw( &dc
);
2773 // don't draw rect outline under Max if we already have the background
2774 // color but under other platforms only draw it if we do: it is a bit
2775 // silly to draw "focus rect" if we don't have focus!
2780 #endif // __WXMAC__/!__WXMAC__
2782 dc
.SetPen( *wxBLACK_PEN
);
2783 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2784 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2791 void wxListMainWindow::HighlightAll( bool on
)
2793 if ( IsSingleSel() )
2795 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2797 // we just have one item to turn off
2798 if ( HasCurrent() && IsHighlighted(m_current
) )
2800 HighlightLine(m_current
, FALSE
);
2801 RefreshLine(m_current
);
2806 HighlightLines(0, GetItemCount() - 1, on
);
2810 void wxListMainWindow::SendNotify( size_t line
,
2811 wxEventType command
,
2814 wxListEvent
le( command
, GetParent()->GetId() );
2815 le
.SetEventObject( GetParent() );
2816 le
.m_itemIndex
= line
;
2818 // set only for events which have position
2819 if ( point
!= wxDefaultPosition
)
2820 le
.m_pointDrag
= point
;
2822 // don't try to get the line info for virtual list controls: the main
2823 // program has it anyhow and if we did it would result in accessing all
2824 // the lines, even those which are not visible now and this is precisely
2825 // what we're trying to avoid
2826 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2828 if ( line
!= (size_t)-1 )
2830 GetLine(line
)->GetItem( 0, le
.m_item
);
2832 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2834 //else: there may be no more such item
2836 GetParent()->GetEventHandler()->ProcessEvent( le
);
2839 void wxListMainWindow::ChangeCurrent(size_t current
)
2841 m_current
= current
;
2843 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2846 void wxListMainWindow::EditLabel( long item
)
2848 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2849 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2851 size_t itemEdit
= (size_t)item
;
2853 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2854 le
.SetEventObject( GetParent() );
2855 le
.m_itemIndex
= item
;
2856 wxListLineData
*data
= GetLine(itemEdit
);
2857 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2858 data
->GetItem( 0, le
.m_item
);
2859 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2861 // vetoed by user code
2865 // We have to call this here because the label in question might just have
2866 // been added and no screen update taken place.
2870 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2875 void wxListMainWindow::OnRenameTimer()
2877 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2879 EditLabel( m_current
);
2882 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2884 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2885 le
.SetEventObject( GetParent() );
2886 le
.m_itemIndex
= itemEdit
;
2888 wxListLineData
*data
= GetLine(itemEdit
);
2889 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
2891 data
->GetItem( 0, le
.m_item
);
2892 le
.m_item
.m_text
= value
;
2893 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2897 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2899 // wxMSW seems not to notify the program about
2900 // cancelled label edits.
2903 // let owner know that the edit was cancelled
2904 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2906 // These only exist for wxTreeCtrl, which should probably be changed
2907 // le.m_editCancelled = TRUE;
2908 // le.m_label = wxEmptyString;
2910 le
.SetEventObject( GetParent() );
2911 le
.m_itemIndex
= itemEdit
;
2913 wxListLineData
*data
= GetLine(itemEdit
);
2914 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2916 data
->GetItem( 0, le
.m_item
);
2918 GetEventHandler()->ProcessEvent( le
);
2921 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2923 event
.SetEventObject( GetParent() );
2924 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2927 if ( !HasCurrent() || IsEmpty() )
2933 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2934 event
.ButtonDClick()) )
2937 int x
= event
.GetX();
2938 int y
= event
.GetY();
2939 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2941 // where did we hit it (if we did)?
2944 size_t count
= GetItemCount(),
2947 if ( HasFlag(wxLC_REPORT
) )
2949 current
= y
/ GetLineHeight();
2950 if ( current
< count
)
2951 hitResult
= HitTestLine(current
, x
, y
);
2955 // TODO: optimize it too! this is less simple than for report view but
2956 // enumerating all items is still not a way to do it!!
2957 for ( current
= 0; current
< count
; current
++ )
2959 hitResult
= HitTestLine(current
, x
, y
);
2965 if (event
.Dragging())
2967 if (m_dragCount
== 0)
2969 // we have to report the raw, physical coords as we want to be
2970 // able to call HitTest(event.m_pointDrag) from the user code to
2971 // get the item being dragged
2972 m_dragStart
= event
.GetPosition();
2977 if (m_dragCount
!= 3)
2980 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2981 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2983 wxListEvent
le( command
, GetParent()->GetId() );
2984 le
.SetEventObject( GetParent() );
2985 le
.m_itemIndex
= current
;
2986 le
.m_pointDrag
= m_dragStart
;
2987 GetParent()->GetEventHandler()->ProcessEvent( le
);
2998 // outside of any item
3002 bool forceClick
= FALSE
;
3003 if (event
.ButtonDClick())
3005 m_renameTimer
->Stop();
3006 m_lastOnSame
= FALSE
;
3008 if ( current
== m_lineLastClicked
)
3010 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3016 // the first click was on another item, so don't interpret this as
3017 // a double click, but as a simple click instead
3022 if (event
.LeftUp() && m_lastOnSame
)
3024 if ((current
== m_current
) &&
3025 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3026 HasFlag(wxLC_EDIT_LABELS
) )
3028 m_renameTimer
->Start( 100, TRUE
);
3030 m_lastOnSame
= FALSE
;
3032 else if (event
.RightDown())
3034 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
3035 event
.GetPosition() );
3037 else if (event
.MiddleDown())
3039 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3041 else if ( event
.LeftDown() || forceClick
)
3043 m_lineBeforeLastClicked
= m_lineLastClicked
;
3044 m_lineLastClicked
= current
;
3046 size_t oldCurrent
= m_current
;
3048 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3050 HighlightAll( FALSE
);
3052 ChangeCurrent(current
);
3054 ReverseHighlight(m_current
);
3056 else // multi sel & either ctrl or shift is down
3058 if (event
.ControlDown())
3060 ChangeCurrent(current
);
3062 ReverseHighlight(m_current
);
3064 else if (event
.ShiftDown())
3066 ChangeCurrent(current
);
3068 size_t lineFrom
= oldCurrent
,
3071 if ( lineTo
< lineFrom
)
3074 lineFrom
= m_current
;
3077 HighlightLines(lineFrom
, lineTo
);
3079 else // !ctrl, !shift
3081 // test in the enclosing if should make it impossible
3082 wxFAIL_MSG( _T("how did we get here?") );
3086 if (m_current
!= oldCurrent
)
3088 RefreshLine( oldCurrent
);
3091 // forceClick is only set if the previous click was on another item
3092 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3096 void wxListMainWindow::MoveToItem(size_t item
)
3098 if ( item
== (size_t)-1 )
3101 wxRect rect
= GetLineRect(item
);
3103 int client_w
, client_h
;
3104 GetClientSize( &client_w
, &client_h
);
3106 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3107 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3109 if ( HasFlag(wxLC_REPORT
) )
3111 // the next we need the range of lines shown it might be different, so
3113 ResetVisibleLinesRange();
3115 if (rect
.y
< view_y
)
3116 Scroll( -1, rect
.y
/m_yScroll
);
3117 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3118 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3122 if (rect
.x
-view_x
< 5)
3123 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3124 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3125 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3129 // ----------------------------------------------------------------------------
3130 // keyboard handling
3131 // ----------------------------------------------------------------------------
3133 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3135 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3136 _T("invalid item index in OnArrowChar()") );
3138 size_t oldCurrent
= m_current
;
3140 // in single selection we just ignore Shift as we can't select several
3142 if ( event
.ShiftDown() && !IsSingleSel() )
3144 ChangeCurrent(newCurrent
);
3146 // select all the items between the old and the new one
3147 if ( oldCurrent
> newCurrent
)
3149 newCurrent
= oldCurrent
;
3150 oldCurrent
= m_current
;
3153 HighlightLines(oldCurrent
, newCurrent
);
3157 // all previously selected items are unselected unless ctrl is held
3158 if ( !event
.ControlDown() )
3159 HighlightAll(FALSE
);
3161 ChangeCurrent(newCurrent
);
3163 // refresh the old focus to remove it
3164 RefreshLine( oldCurrent
);
3166 if ( !event
.ControlDown() )
3168 HighlightLine( m_current
, TRUE
);
3172 RefreshLine( m_current
);
3177 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3179 wxWindow
*parent
= GetParent();
3181 /* we propagate the key event up */
3182 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3183 ke
.m_shiftDown
= event
.m_shiftDown
;
3184 ke
.m_controlDown
= event
.m_controlDown
;
3185 ke
.m_altDown
= event
.m_altDown
;
3186 ke
.m_metaDown
= event
.m_metaDown
;
3187 ke
.m_keyCode
= event
.m_keyCode
;
3190 ke
.SetEventObject( parent
);
3191 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3196 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3198 wxWindow
*parent
= GetParent();
3200 /* we send a list_key event up */
3203 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3204 le
.m_itemIndex
= m_current
;
3205 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3206 le
.m_code
= event
.GetKeyCode();
3207 le
.SetEventObject( parent
);
3208 parent
->GetEventHandler()->ProcessEvent( le
);
3211 /* we propagate the char event up */
3212 wxKeyEvent
ke( wxEVT_CHAR
);
3213 ke
.m_shiftDown
= event
.m_shiftDown
;
3214 ke
.m_controlDown
= event
.m_controlDown
;
3215 ke
.m_altDown
= event
.m_altDown
;
3216 ke
.m_metaDown
= event
.m_metaDown
;
3217 ke
.m_keyCode
= event
.m_keyCode
;
3220 ke
.SetEventObject( parent
);
3221 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3223 if (event
.GetKeyCode() == WXK_TAB
)
3225 wxNavigationKeyEvent nevent
;
3226 nevent
.SetWindowChange( event
.ControlDown() );
3227 nevent
.SetDirection( !event
.ShiftDown() );
3228 nevent
.SetEventObject( GetParent()->GetParent() );
3229 nevent
.SetCurrentFocus( m_parent
);
3230 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3234 /* no item -> nothing to do */
3241 switch (event
.GetKeyCode())
3244 if ( m_current
> 0 )
3245 OnArrowChar( m_current
- 1, event
);
3249 if ( m_current
< (size_t)GetItemCount() - 1 )
3250 OnArrowChar( m_current
+ 1, event
);
3255 OnArrowChar( GetItemCount() - 1, event
);
3260 OnArrowChar( 0, event
);
3266 if ( HasFlag(wxLC_REPORT
) )
3268 steps
= m_linesPerPage
- 1;
3272 steps
= m_current
% m_linesPerPage
;
3275 int index
= m_current
- steps
;
3279 OnArrowChar( index
, event
);
3286 if ( HasFlag(wxLC_REPORT
) )
3288 steps
= m_linesPerPage
- 1;
3292 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3295 size_t index
= m_current
+ steps
;
3296 size_t count
= GetItemCount();
3297 if ( index
>= count
)
3300 OnArrowChar( index
, event
);
3305 if ( !HasFlag(wxLC_REPORT
) )
3307 int index
= m_current
- m_linesPerPage
;
3311 OnArrowChar( index
, event
);
3316 if ( !HasFlag(wxLC_REPORT
) )
3318 size_t index
= m_current
+ m_linesPerPage
;
3320 size_t count
= GetItemCount();
3321 if ( index
>= count
)
3324 OnArrowChar( index
, event
);
3329 if ( IsSingleSel() )
3331 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3333 if ( IsHighlighted(m_current
) )
3335 // don't unselect the item in single selection mode
3338 //else: select it in ReverseHighlight() below if unselected
3341 ReverseHighlight(m_current
);
3346 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3354 // ----------------------------------------------------------------------------
3356 // ----------------------------------------------------------------------------
3358 void wxListMainWindow::SetFocus()
3360 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3361 // overrides SetFocus in such way that it does never change focus from
3362 // panel's child to the panel itself. Unfortunately, we must be able to change
3363 // focus to the panel from wxListTextCtrl because the text control should
3364 // disappear when the user clicks outside it.
3366 wxWindow
*oldFocus
= FindFocus();
3368 if ( oldFocus
&& oldFocus
->GetParent() == this )
3370 wxWindow::SetFocus();
3374 wxScrolledWindow::SetFocus();
3378 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3380 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3381 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3382 // which are already drawn correctly resulting in horrible flicker - avoid
3394 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3395 event
.SetEventObject( GetParent() );
3396 GetParent()->GetEventHandler()->ProcessEvent( event
);
3399 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3406 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3408 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3410 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3412 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3414 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3416 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3418 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3420 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3422 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3426 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3428 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3430 m_normal_image_list
->GetSize( index
, width
, height
);
3432 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3434 m_small_image_list
->GetSize( index
, width
, height
);
3436 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3438 m_small_image_list
->GetSize( index
, width
, height
);
3440 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3442 m_small_image_list
->GetSize( index
, width
, height
);
3451 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3453 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3454 dc
.SetFont( GetFont() );
3457 dc
.GetTextExtent( s
, &lw
, NULL
);
3459 return lw
+ AUTOSIZE_COL_MARGIN
;
3462 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3466 // calc the spacing from the icon size
3469 if ((imageList
) && (imageList
->GetImageCount()) )
3471 imageList
->GetSize(0, width
, height
);
3474 if (which
== wxIMAGE_LIST_NORMAL
)
3476 m_normal_image_list
= imageList
;
3477 m_normal_spacing
= width
+ 8;
3480 if (which
== wxIMAGE_LIST_SMALL
)
3482 m_small_image_list
= imageList
;
3483 m_small_spacing
= width
+ 14;
3484 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3488 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3493 m_small_spacing
= spacing
;
3497 m_normal_spacing
= spacing
;
3501 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3503 return isSmall
? m_small_spacing
: m_normal_spacing
;
3506 // ----------------------------------------------------------------------------
3508 // ----------------------------------------------------------------------------
3510 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3512 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3514 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3516 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3517 item
.m_width
= GetTextLength( item
.m_text
);
3519 wxListHeaderData
*column
= node
->GetData();
3520 column
->SetItem( item
);
3522 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3524 headerWin
->m_dirty
= TRUE
;
3528 // invalidate it as it has to be recalculated
3532 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3534 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3535 _T("invalid column index") );
3537 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3538 _T("SetColumnWidth() can only be called in report mode.") );
3541 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3543 headerWin
->m_dirty
= TRUE
;
3545 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3546 wxCHECK_RET( node
, _T("no column?") );
3548 wxListHeaderData
*column
= node
->GetData();
3550 size_t count
= GetItemCount();
3552 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3554 width
= GetTextLength(column
->GetText());
3556 else if ( width
== wxLIST_AUTOSIZE
)
3560 // TODO: determine the max width somehow...
3561 width
= WIDTH_COL_DEFAULT
;
3565 wxClientDC
dc(this);
3566 dc
.SetFont( GetFont() );
3568 int max
= AUTOSIZE_COL_MARGIN
;
3570 for ( size_t i
= 0; i
< count
; i
++ )
3572 wxListLineData
*line
= GetLine(i
);
3573 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3575 wxCHECK_RET( n
, _T("no subitem?") );
3577 wxListItemData
*item
= n
->GetData();
3580 if (item
->HasImage())
3583 GetImageSize( item
->GetImage(), ix
, iy
);
3587 if (item
->HasText())
3590 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3598 width
= max
+ AUTOSIZE_COL_MARGIN
;
3602 column
->SetWidth( width
);
3604 // invalidate it as it has to be recalculated
3608 int wxListMainWindow::GetHeaderWidth() const
3610 if ( !m_headerWidth
)
3612 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3614 size_t count
= GetColumnCount();
3615 for ( size_t col
= 0; col
< count
; col
++ )
3617 self
->m_headerWidth
+= GetColumnWidth(col
);
3621 return m_headerWidth
;
3624 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3626 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3627 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3629 wxListHeaderData
*column
= node
->GetData();
3630 column
->GetItem( item
);
3633 int wxListMainWindow::GetColumnWidth( int col
) const
3635 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3636 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3638 wxListHeaderData
*column
= node
->GetData();
3639 return column
->GetWidth();
3642 // ----------------------------------------------------------------------------
3644 // ----------------------------------------------------------------------------
3646 void wxListMainWindow::SetItem( wxListItem
&item
)
3648 long id
= item
.m_itemId
;
3649 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3650 _T("invalid item index in SetItem") );
3654 wxListLineData
*line
= GetLine((size_t)id
);
3655 line
->SetItem( item
.m_col
, item
);
3658 // update the item on screen
3660 GetItemRect(id
, rectItem
);
3661 RefreshRect(rectItem
);
3664 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3666 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3667 _T("invalid list ctrl item index in SetItem") );
3669 size_t oldCurrent
= m_current
;
3670 size_t item
= (size_t)litem
; // safe because of the check above
3672 // do we need to change the focus?
3673 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3675 if ( state
& wxLIST_STATE_FOCUSED
)
3677 // don't do anything if this item is already focused
3678 if ( item
!= m_current
)
3680 ChangeCurrent(item
);
3682 if ( oldCurrent
!= (size_t)-1 )
3684 if ( IsSingleSel() )
3686 HighlightLine(oldCurrent
, FALSE
);
3689 RefreshLine(oldCurrent
);
3692 RefreshLine( m_current
);
3697 // don't do anything if this item is not focused
3698 if ( item
== m_current
)
3702 if ( IsSingleSel() )
3704 // we must unselect the old current item as well or we
3705 // might end up with more than one selected item in a
3706 // single selection control
3707 HighlightLine(oldCurrent
, FALSE
);
3710 RefreshLine( oldCurrent
);
3715 // do we need to change the selection state?
3716 if ( stateMask
& wxLIST_STATE_SELECTED
)
3718 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3720 if ( IsSingleSel() )
3724 // selecting the item also makes it the focused one in the
3726 if ( m_current
!= item
)
3728 ChangeCurrent(item
);
3730 if ( oldCurrent
!= (size_t)-1 )
3732 HighlightLine( oldCurrent
, FALSE
);
3733 RefreshLine( oldCurrent
);
3739 // only the current item may be selected anyhow
3740 if ( item
!= m_current
)
3745 if ( HighlightLine(item
, on
) )
3752 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3754 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3755 _T("invalid list ctrl item index in GetItemState()") );
3757 int ret
= wxLIST_STATE_DONTCARE
;
3759 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3761 if ( (size_t)item
== m_current
)
3762 ret
|= wxLIST_STATE_FOCUSED
;
3765 if ( stateMask
& wxLIST_STATE_SELECTED
)
3767 if ( IsHighlighted(item
) )
3768 ret
|= wxLIST_STATE_SELECTED
;
3774 void wxListMainWindow::GetItem( wxListItem
&item
) const
3776 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3777 _T("invalid item index in GetItem") );
3779 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3780 line
->GetItem( item
.m_col
, item
);
3783 // ----------------------------------------------------------------------------
3785 // ----------------------------------------------------------------------------
3787 size_t wxListMainWindow::GetItemCount() const
3789 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3792 void wxListMainWindow::SetItemCount(long count
)
3794 m_selStore
.SetItemCount(count
);
3795 m_countVirt
= count
;
3797 ResetVisibleLinesRange();
3799 // scrollbars must be reset
3803 int wxListMainWindow::GetSelectedItemCount() const
3805 // deal with the quick case first
3806 if ( IsSingleSel() )
3808 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3811 // virtual controls remmebers all its selections itself
3813 return m_selStore
.GetSelectedCount();
3815 // TODO: we probably should maintain the number of items selected even for
3816 // non virtual controls as enumerating all lines is really slow...
3817 size_t countSel
= 0;
3818 size_t count
= GetItemCount();
3819 for ( size_t line
= 0; line
< count
; line
++ )
3821 if ( GetLine(line
)->IsHighlighted() )
3828 // ----------------------------------------------------------------------------
3829 // item position/size
3830 // ----------------------------------------------------------------------------
3832 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3834 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3835 _T("invalid index in GetItemRect") );
3837 rect
= GetLineRect((size_t)index
);
3839 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3842 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3845 GetItemRect(item
, rect
);
3853 // ----------------------------------------------------------------------------
3854 // geometry calculation
3855 // ----------------------------------------------------------------------------
3857 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3859 wxClientDC
dc( this );
3860 dc
.SetFont( GetFont() );
3863 if ( HasFlag(wxLC_ICON
) )
3864 iconSpacing
= m_normal_spacing
;
3865 else if ( HasFlag(wxLC_SMALL_ICON
) )
3866 iconSpacing
= m_small_spacing
;
3870 // Note that we do not call GetClientSize() here but
3871 // GetSize() and substract the border size for sunken
3872 // borders manually. This is technically incorrect,
3873 // but we need to know the client area's size WITHOUT
3874 // scrollbars here. Since we don't know if there are
3875 // any scrollbars, we use GetSize() instead. Another
3876 // solution would be to call SetScrollbars() here to
3877 // remove the scrollbars and call GetClientSize() then,
3878 // but this might result in flicker and - worse - will
3879 // reset the scrollbars to 0 which is not good at all
3880 // if you resize a dialog/window, but don't want to
3881 // reset the window scrolling. RR.
3882 // Furthermore, we actually do NOT subtract the border
3883 // width as 2 pixels is just the extra space which we
3884 // need around the actual content in the window. Other-
3885 // wise the text would e.g. touch the upper border. RR.
3888 GetSize( &clientWidth
, &clientHeight
);
3890 if ( HasFlag(wxLC_REPORT
) )
3892 // all lines have the same height
3893 int lineHeight
= GetLineHeight();
3895 // scroll one line per step
3896 m_yScroll
= lineHeight
;
3898 size_t lineCount
= GetItemCount();
3899 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3901 m_linesPerPage
= clientHeight
/ lineHeight
;
3903 ResetVisibleLinesRange();
3905 SetScrollbars( m_xScroll
, m_yScroll
,
3906 GetHeaderWidth() / m_xScroll
,
3907 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3908 GetScrollPos(wxHORIZONTAL
),
3909 GetScrollPos(wxVERTICAL
),
3914 // at first we try without any scrollbar. if the items don't
3915 // fit into the window, we recalculate after subtracting an
3916 // approximated 15 pt for the horizontal scrollbar
3918 int entireWidth
= 0;
3920 for (int tries
= 0; tries
< 2; tries
++)
3922 // We start with 4 for the border around all items
3927 // Now we have decided that the items do not fit into the
3928 // client area. Unfortunately, wxWindows sometimes thinks
3929 // that it does fit and therefore NO horizontal scrollbar
3930 // is inserted. This looks ugly, so we fudge here and make
3931 // the calculated width bigger than was actually has been
3932 // calculated. This ensures that wxScrolledWindows puts
3933 // a scrollbar at the bottom of its client area.
3934 entireWidth
+= SCROLL_UNIT_X
;
3937 // Start at 2,2 so the text does not touch the border
3942 int currentlyVisibleLines
= 0;
3944 size_t count
= GetItemCount();
3945 for (size_t i
= 0; i
< count
; i
++)
3947 currentlyVisibleLines
++;
3948 wxListLineData
*line
= GetLine(i
);
3949 line
->CalculateSize( &dc
, iconSpacing
);
3950 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
); // Why clientWidth? (FIXME)
3952 wxSize sizeLine
= GetLineSize(i
);
3954 if ( maxWidth
< sizeLine
.x
)
3955 maxWidth
= sizeLine
.x
;
3958 if (currentlyVisibleLines
> m_linesPerPage
)
3959 m_linesPerPage
= currentlyVisibleLines
;
3961 // Assume that the size of the next one is the same... (FIXME)
3962 if ( y
+ sizeLine
.y
>= clientHeight
)
3964 currentlyVisibleLines
= 0;
3967 entireWidth
+= maxWidth
+6;
3971 // We have reached the last item.
3972 if ( i
== count
- 1 )
3973 entireWidth
+= maxWidth
;
3975 if ( (tries
== 0) && (entireWidth
+SCROLL_UNIT_X
> clientWidth
) )
3977 clientHeight
-= 15; // We guess the scrollbar height. (FIXME)
3979 currentlyVisibleLines
= 0;
3983 if ( i
== count
- 1 )
3984 tries
= 1; // Everything fits, no second try required.
3988 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
3989 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
3994 // FIXME: why should we call it from here?
4001 void wxListMainWindow::RefreshAll()
4006 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4007 if ( headerWin
&& headerWin
->m_dirty
)
4009 headerWin
->m_dirty
= FALSE
;
4010 headerWin
->Refresh();
4014 void wxListMainWindow::UpdateCurrent()
4016 if ( !HasCurrent() && !IsEmpty() )
4022 long wxListMainWindow::GetNextItem( long item
,
4023 int WXUNUSED(geometry
),
4027 max
= GetItemCount();
4028 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4029 _T("invalid listctrl index in GetNextItem()") );
4031 // notice that we start with the next item (or the first one if item == -1)
4032 // and this is intentional to allow writing a simple loop to iterate over
4033 // all selected items
4037 // this is not an error because the index was ok initially, just no
4048 size_t count
= GetItemCount();
4049 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4051 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4054 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4061 // ----------------------------------------------------------------------------
4063 // ----------------------------------------------------------------------------
4065 void wxListMainWindow::DeleteItem( long lindex
)
4067 size_t count
= GetItemCount();
4069 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4070 _T("invalid item index in DeleteItem") );
4072 size_t index
= (size_t)lindex
;
4074 // we don't need to adjust the index for the previous items
4075 if ( HasCurrent() && m_current
>= index
)
4077 // if the current item is being deleted, we want the next one to
4078 // become selected - unless there is no next one - so don't adjust
4079 // m_current in this case
4080 if ( m_current
!= index
|| m_current
== count
- 1 )
4086 if ( InReportView() )
4088 ResetVisibleLinesRange();
4095 m_selStore
.OnItemDelete(index
);
4099 m_lines
.RemoveAt( index
);
4102 // we need to refresh the (vert) scrollbar as the number of items changed
4105 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4107 RefreshAfter(index
);
4110 void wxListMainWindow::DeleteColumn( int col
)
4112 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4114 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4117 delete node
->GetData();
4118 m_columns
.Erase( node
);
4122 // update all the items
4123 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4125 wxListLineData
* const line
= GetLine(i
);
4126 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4127 delete n
->GetData();
4128 line
->m_items
.Erase(n
);
4132 // invalidate it as it has to be recalculated
4136 void wxListMainWindow::DoDeleteAllItems()
4140 // nothing to do - in particular, don't send the event
4146 // to make the deletion of all items faster, we don't send the
4147 // notifications for each item deletion in this case but only one event
4148 // for all of them: this is compatible with wxMSW and documented in
4149 // DeleteAllItems() description
4151 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4152 event
.SetEventObject( GetParent() );
4153 GetParent()->GetEventHandler()->ProcessEvent( event
);
4162 if ( InReportView() )
4164 ResetVisibleLinesRange();
4170 void wxListMainWindow::DeleteAllItems()
4174 RecalculatePositions();
4177 void wxListMainWindow::DeleteEverything()
4179 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4184 // ----------------------------------------------------------------------------
4185 // scanning for an item
4186 // ----------------------------------------------------------------------------
4188 void wxListMainWindow::EnsureVisible( long index
)
4190 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4191 _T("invalid index in EnsureVisible") );
4193 // We have to call this here because the label in question might just have
4194 // been added and its position is not known yet
4197 RecalculatePositions(TRUE
/* no refresh */);
4200 MoveToItem((size_t)index
);
4203 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4210 size_t count
= GetItemCount();
4211 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4213 wxListLineData
*line
= GetLine(i
);
4214 if ( line
->GetText(0) == tmp
)
4221 long wxListMainWindow::FindItem(long start
, long data
)
4227 size_t count
= GetItemCount();
4228 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4230 wxListLineData
*line
= GetLine(i
);
4232 line
->GetItem( 0, item
);
4233 if (item
.m_data
== data
)
4240 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4242 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4244 size_t count
= GetItemCount();
4246 if ( HasFlag(wxLC_REPORT
) )
4248 size_t current
= y
/ GetLineHeight();
4249 if ( current
< count
)
4251 flags
= HitTestLine(current
, x
, y
);
4258 // TODO: optimize it too! this is less simple than for report view but
4259 // enumerating all items is still not a way to do it!!
4260 for ( size_t current
= 0; current
< count
; current
++ )
4262 flags
= HitTestLine(current
, x
, y
);
4271 // ----------------------------------------------------------------------------
4273 // ----------------------------------------------------------------------------
4275 void wxListMainWindow::InsertItem( wxListItem
&item
)
4277 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4279 size_t count
= GetItemCount();
4280 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4281 _T("invalid item index") );
4283 size_t id
= item
.m_itemId
;
4288 if ( HasFlag(wxLC_REPORT
) )
4291 ResetVisibleLinesRange();
4293 else if ( HasFlag(wxLC_LIST
) )
4295 else if ( HasFlag(wxLC_ICON
) )
4297 else if ( HasFlag(wxLC_SMALL_ICON
) )
4298 mode
= wxLC_ICON
; // no typo
4301 wxFAIL_MSG( _T("unknown mode") );
4304 wxListLineData
*line
= new wxListLineData(this);
4306 line
->SetItem( 0, item
);
4308 m_lines
.Insert( line
, id
);
4312 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4314 RefreshLines(id
, GetItemCount() - 1);
4317 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4320 if ( HasFlag(wxLC_REPORT
) )
4322 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4323 item
.m_width
= GetTextLength( item
.m_text
);
4325 wxListHeaderData
*column
= new wxListHeaderData( item
);
4326 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4329 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4330 m_columns
.Insert( node
, column
);
4334 m_columns
.Append( column
);
4339 // update all the items
4340 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4342 wxListLineData
* const line
= GetLine(i
);
4343 wxListItemData
* const data
= new wxListItemData(this);
4345 line
->m_items
.Insert(col
, data
);
4347 line
->m_items
.Append(data
);
4351 // invalidate it as it has to be recalculated
4356 // ----------------------------------------------------------------------------
4358 // ----------------------------------------------------------------------------
4360 wxListCtrlCompare list_ctrl_compare_func_2
;
4361 long list_ctrl_compare_data
;
4363 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4365 wxListLineData
*line1
= *arg1
;
4366 wxListLineData
*line2
= *arg2
;
4368 line1
->GetItem( 0, item
);
4369 long data1
= item
.m_data
;
4370 line2
->GetItem( 0, item
);
4371 long data2
= item
.m_data
;
4372 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4375 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4377 list_ctrl_compare_func_2
= fn
;
4378 list_ctrl_compare_data
= data
;
4379 m_lines
.Sort( list_ctrl_compare_func_1
);
4383 // ----------------------------------------------------------------------------
4385 // ----------------------------------------------------------------------------
4387 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4389 // update our idea of which lines are shown when we redraw the window the
4391 ResetVisibleLinesRange();
4394 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4395 wxScrolledWindow::OnScroll(event
);
4397 HandleOnScroll( event
);
4400 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4402 wxGenericListCtrl
* lc
= GetListCtrl();
4403 wxCHECK_RET( lc
, _T("no listctrl window?") );
4405 lc
->m_headerWin
->Refresh();
4406 lc
->m_headerWin
->Update();
4410 int wxListMainWindow::GetCountPerPage() const
4412 if ( !m_linesPerPage
)
4414 wxConstCast(this, wxListMainWindow
)->
4415 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4418 return m_linesPerPage
;
4421 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4423 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4425 if ( m_lineFrom
== (size_t)-1 )
4427 size_t count
= GetItemCount();
4430 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4432 // this may happen if SetScrollbars() hadn't been called yet
4433 if ( m_lineFrom
>= count
)
4434 m_lineFrom
= count
- 1;
4436 // we redraw one extra line but this is needed to make the redrawing
4437 // logic work when there is a fractional number of lines on screen
4438 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4439 if ( m_lineTo
>= count
)
4440 m_lineTo
= count
- 1;
4442 else // empty control
4445 m_lineTo
= (size_t)-1;
4449 wxASSERT_MSG( IsEmpty() ||
4450 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4451 _T("GetVisibleLinesRange() returns incorrect result") );
4459 // -------------------------------------------------------------------------------------
4460 // wxGenericListCtrl
4461 // -------------------------------------------------------------------------------------
4463 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4465 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4466 EVT_SIZE(wxGenericListCtrl::OnSize
)
4469 wxGenericListCtrl::wxGenericListCtrl()
4471 m_imageListNormal
= (wxImageListType
*) NULL
;
4472 m_imageListSmall
= (wxImageListType
*) NULL
;
4473 m_imageListState
= (wxImageListType
*) NULL
;
4475 m_ownsImageListNormal
=
4476 m_ownsImageListSmall
=
4477 m_ownsImageListState
= FALSE
;
4479 m_mainWin
= (wxListMainWindow
*) NULL
;
4480 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4483 wxGenericListCtrl::~wxGenericListCtrl()
4485 if (m_ownsImageListNormal
)
4486 delete m_imageListNormal
;
4487 if (m_ownsImageListSmall
)
4488 delete m_imageListSmall
;
4489 if (m_ownsImageListState
)
4490 delete m_imageListState
;
4493 void wxGenericListCtrl::CreateHeaderWindow()
4495 m_headerWin
= new wxListHeaderWindow
4497 this, -1, m_mainWin
,
4499 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
4504 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4509 const wxValidator
&validator
,
4510 const wxString
&name
)
4514 m_imageListState
= (wxImageListType
*) NULL
;
4515 m_ownsImageListNormal
=
4516 m_ownsImageListSmall
=
4517 m_ownsImageListState
= FALSE
;
4519 m_mainWin
= (wxListMainWindow
*) NULL
;
4520 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4522 if ( !(style
& wxLC_MASK_TYPE
) )
4524 style
= style
| wxLC_LIST
;
4527 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4530 // don't create the inner window with the border
4531 style
&= ~wxBORDER_MASK
;
4533 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4535 if ( HasFlag(wxLC_REPORT
) )
4537 CreateHeaderWindow();
4539 if ( HasFlag(wxLC_NO_HEADER
) )
4541 // VZ: why do we create it at all then?
4542 m_headerWin
->Show( FALSE
);
4549 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4551 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4552 _T("wxLC_VIRTUAL can't be [un]set") );
4554 long flag
= GetWindowStyle();
4558 if (style
& wxLC_MASK_TYPE
)
4559 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4560 if (style
& wxLC_MASK_ALIGN
)
4561 flag
&= ~wxLC_MASK_ALIGN
;
4562 if (style
& wxLC_MASK_SORT
)
4563 flag
&= ~wxLC_MASK_SORT
;
4575 SetWindowStyleFlag( flag
);
4578 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4582 m_mainWin
->DeleteEverything();
4584 // has the header visibility changed?
4585 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4586 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4588 if ( hasHeader
!= willHaveHeader
)
4595 // don't delete, just hide, as we can reuse it later
4596 m_headerWin
->Show(FALSE
);
4598 //else: nothing to do
4600 else // must show header
4604 CreateHeaderWindow();
4606 else // already have it, just show
4608 m_headerWin
->Show( TRUE
);
4612 ResizeReportView(willHaveHeader
);
4616 wxWindow::SetWindowStyleFlag( flag
);
4619 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4621 m_mainWin
->GetColumn( col
, item
);
4625 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4627 m_mainWin
->SetColumn( col
, item
);
4631 int wxGenericListCtrl::GetColumnWidth( int col
) const
4633 return m_mainWin
->GetColumnWidth( col
);
4636 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4638 m_mainWin
->SetColumnWidth( col
, width
);
4642 int wxGenericListCtrl::GetCountPerPage() const
4644 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4647 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4649 m_mainWin
->GetItem( info
);
4653 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4655 m_mainWin
->SetItem( info
);
4659 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4662 info
.m_text
= label
;
4663 info
.m_mask
= wxLIST_MASK_TEXT
;
4664 info
.m_itemId
= index
;
4668 info
.m_image
= imageId
;
4669 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4671 m_mainWin
->SetItem(info
);
4675 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4677 return m_mainWin
->GetItemState( item
, stateMask
);
4680 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4682 m_mainWin
->SetItemState( item
, state
, stateMask
);
4686 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4689 info
.m_image
= image
;
4690 info
.m_mask
= wxLIST_MASK_IMAGE
;
4691 info
.m_itemId
= item
;
4692 m_mainWin
->SetItem( info
);
4696 wxString
wxGenericListCtrl::GetItemText( long item
) const
4698 return m_mainWin
->GetItemText(item
);
4701 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4703 m_mainWin
->SetItemText(item
, str
);
4706 long wxGenericListCtrl::GetItemData( long item
) const
4709 info
.m_itemId
= item
;
4710 m_mainWin
->GetItem( info
);
4714 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4717 info
.m_mask
= wxLIST_MASK_DATA
;
4718 info
.m_itemId
= item
;
4720 m_mainWin
->SetItem( info
);
4724 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4726 m_mainWin
->GetItemRect( item
, rect
);
4727 if ( m_mainWin
->HasHeader() )
4728 rect
.y
+= HEADER_HEIGHT
+ 1;
4732 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4734 m_mainWin
->GetItemPosition( item
, pos
);
4738 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4743 int wxGenericListCtrl::GetItemCount() const
4745 return m_mainWin
->GetItemCount();
4748 int wxGenericListCtrl::GetColumnCount() const
4750 return m_mainWin
->GetColumnCount();
4753 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4755 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4758 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4760 return m_mainWin
->GetItemSpacing( isSmall
);
4763 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4766 info
.m_itemId
= item
;
4767 info
.SetTextColour( col
);
4768 m_mainWin
->SetItem( info
);
4771 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4774 info
.m_itemId
= item
;
4775 m_mainWin
->GetItem( info
);
4776 return info
.GetTextColour();
4779 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4782 info
.m_itemId
= item
;
4783 info
.SetBackgroundColour( col
);
4784 m_mainWin
->SetItem( info
);
4787 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4790 info
.m_itemId
= item
;
4791 m_mainWin
->GetItem( info
);
4792 return info
.GetBackgroundColour();
4795 int wxGenericListCtrl::GetSelectedItemCount() const
4797 return m_mainWin
->GetSelectedItemCount();
4800 wxColour
wxGenericListCtrl::GetTextColour() const
4802 return GetForegroundColour();
4805 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4807 SetForegroundColour(col
);
4810 long wxGenericListCtrl::GetTopItem() const
4813 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4817 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4819 return m_mainWin
->GetNextItem( item
, geom
, state
);
4822 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
4824 if (which
== wxIMAGE_LIST_NORMAL
)
4826 return m_imageListNormal
;
4828 else if (which
== wxIMAGE_LIST_SMALL
)
4830 return m_imageListSmall
;
4832 else if (which
== wxIMAGE_LIST_STATE
)
4834 return m_imageListState
;
4836 return (wxImageListType
*) NULL
;
4839 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
4841 if ( which
== wxIMAGE_LIST_NORMAL
)
4843 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4844 m_imageListNormal
= imageList
;
4845 m_ownsImageListNormal
= FALSE
;
4847 else if ( which
== wxIMAGE_LIST_SMALL
)
4849 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4850 m_imageListSmall
= imageList
;
4851 m_ownsImageListSmall
= FALSE
;
4853 else if ( which
== wxIMAGE_LIST_STATE
)
4855 if (m_ownsImageListState
) delete m_imageListState
;
4856 m_imageListState
= imageList
;
4857 m_ownsImageListState
= FALSE
;
4860 m_mainWin
->SetImageList( imageList
, which
);
4863 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
4865 SetImageList(imageList
, which
);
4866 if ( which
== wxIMAGE_LIST_NORMAL
)
4867 m_ownsImageListNormal
= TRUE
;
4868 else if ( which
== wxIMAGE_LIST_SMALL
)
4869 m_ownsImageListSmall
= TRUE
;
4870 else if ( which
== wxIMAGE_LIST_STATE
)
4871 m_ownsImageListState
= TRUE
;
4874 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4879 bool wxGenericListCtrl::DeleteItem( long item
)
4881 m_mainWin
->DeleteItem( item
);
4885 bool wxGenericListCtrl::DeleteAllItems()
4887 m_mainWin
->DeleteAllItems();
4891 bool wxGenericListCtrl::DeleteAllColumns()
4893 size_t count
= m_mainWin
->m_columns
.GetCount();
4894 for ( size_t n
= 0; n
< count
; n
++ )
4900 void wxGenericListCtrl::ClearAll()
4902 m_mainWin
->DeleteEverything();
4905 bool wxGenericListCtrl::DeleteColumn( int col
)
4907 m_mainWin
->DeleteColumn( col
);
4909 // if we don't have the header any longer, we need to relayout the window
4910 if ( !GetColumnCount() )
4912 ResizeReportView(FALSE
/* no header */);
4918 void wxGenericListCtrl::Edit( long item
)
4920 m_mainWin
->EditLabel( item
);
4923 bool wxGenericListCtrl::EnsureVisible( long item
)
4925 m_mainWin
->EnsureVisible( item
);
4929 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4931 return m_mainWin
->FindItem( start
, str
, partial
);
4934 long wxGenericListCtrl::FindItem( long start
, long data
)
4936 return m_mainWin
->FindItem( start
, data
);
4939 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4940 int WXUNUSED(direction
))
4945 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4947 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4950 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4952 m_mainWin
->InsertItem( info
);
4953 return info
.m_itemId
;
4956 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4959 info
.m_text
= label
;
4960 info
.m_mask
= wxLIST_MASK_TEXT
;
4961 info
.m_itemId
= index
;
4962 return InsertItem( info
);
4965 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4968 info
.m_mask
= wxLIST_MASK_IMAGE
;
4969 info
.m_image
= imageIndex
;
4970 info
.m_itemId
= index
;
4971 return InsertItem( info
);
4974 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4977 info
.m_text
= label
;
4978 info
.m_image
= imageIndex
;
4979 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4980 info
.m_itemId
= index
;
4981 return InsertItem( info
);
4984 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4986 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
4988 m_mainWin
->InsertColumn( col
, item
);
4990 // if we hadn't had header before and have it now we need to relayout the
4992 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
4994 ResizeReportView(TRUE
/* have header */);
4997 m_headerWin
->Refresh();
5002 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5003 int format
, int width
)
5006 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5007 item
.m_text
= heading
;
5010 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5011 item
.m_width
= width
;
5013 item
.m_format
= format
;
5015 return InsertColumn( col
, item
);
5018 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5024 // fn is a function which takes 3 long arguments: item1, item2, data.
5025 // item1 is the long data associated with a first item (NOT the index).
5026 // item2 is the long data associated with a second item (NOT the index).
5027 // data is the same value as passed to SortItems.
5028 // The return value is a negative number if the first item should precede the second
5029 // item, a positive number of the second item should precede the first,
5030 // or zero if the two items are equivalent.
5031 // data is arbitrary data to be passed to the sort function.
5033 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5035 m_mainWin
->SortItems( fn
, data
);
5039 // ----------------------------------------------------------------------------
5041 // ----------------------------------------------------------------------------
5043 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5048 ResizeReportView(m_mainWin
->HasHeader());
5050 m_mainWin
->RecalculatePositions();
5053 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5056 GetClientSize( &cw
, &ch
);
5060 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
5061 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
5063 else // no header window
5065 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5069 void wxGenericListCtrl::OnInternalIdle()
5071 wxWindow::OnInternalIdle();
5073 // do it only if needed
5074 if ( !m_mainWin
->m_dirty
)
5077 m_mainWin
->RecalculatePositions();
5080 // ----------------------------------------------------------------------------
5082 // ----------------------------------------------------------------------------
5084 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5088 m_mainWin
->SetBackgroundColour( colour
);
5089 m_mainWin
->m_dirty
= TRUE
;
5095 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5097 if ( !wxWindow::SetForegroundColour( colour
) )
5102 m_mainWin
->SetForegroundColour( colour
);
5103 m_mainWin
->m_dirty
= TRUE
;
5108 m_headerWin
->SetForegroundColour( colour
);
5114 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5116 if ( !wxWindow::SetFont( font
) )
5121 m_mainWin
->SetFont( font
);
5122 m_mainWin
->m_dirty
= TRUE
;
5127 m_headerWin
->SetFont( font
);
5133 // ----------------------------------------------------------------------------
5134 // methods forwarded to m_mainWin
5135 // ----------------------------------------------------------------------------
5137 #if wxUSE_DRAG_AND_DROP
5139 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5141 m_mainWin
->SetDropTarget( dropTarget
);
5144 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5146 return m_mainWin
->GetDropTarget();
5149 #endif // wxUSE_DRAG_AND_DROP
5151 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5153 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5156 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5158 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5161 wxColour
wxGenericListCtrl::GetForegroundColour() const
5163 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5166 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5169 return m_mainWin
->PopupMenu( menu
, x
, y
);
5172 #endif // wxUSE_MENUS
5175 void wxGenericListCtrl::SetFocus()
5177 /* The test in window.cpp fails as we are a composite
5178 window, so it checks against "this", but not m_mainWin. */
5179 if ( FindFocus() != this )
5180 m_mainWin
->SetFocus();
5183 // ----------------------------------------------------------------------------
5184 // virtual list control support
5185 // ----------------------------------------------------------------------------
5187 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5189 // this is a pure virtual function, in fact - which is not really pure
5190 // because the controls which are not virtual don't need to implement it
5191 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5193 return wxEmptyString
;
5196 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5199 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5204 wxListItemAttr
*wxGenericListCtrl::OnGetItemAttr(long item
) const
5206 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5207 _T("invalid item index in OnGetItemAttr()") );
5209 // no attributes by default
5213 void wxGenericListCtrl::SetItemCount(long count
)
5215 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5217 m_mainWin
->SetItemCount(count
);
5220 void wxGenericListCtrl::RefreshItem(long item
)
5222 m_mainWin
->RefreshLine(item
);
5225 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5227 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5231 * Generic wxListCtrl is more or less a container for two other
5232 * windows which drawings are done upon. These are namely
5233 * 'm_headerWin' and 'm_mainWin'.
5234 * Here we override 'virtual wxWindow::Refresh()' to mimic the
5235 * behaviour wxListCtrl has under wxMSW.
5237 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5241 // The easy case, no rectangle specified.
5243 m_headerWin
->Refresh(eraseBackground
);
5246 m_mainWin
->Refresh(eraseBackground
);
5250 // Refresh the header window
5253 wxRect rectHeader
= m_headerWin
->GetRect();
5254 rectHeader
.Intersect(*rect
);
5255 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5258 m_headerWin
->GetPosition(&x
, &y
);
5259 rectHeader
.Offset(-x
, -y
);
5260 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5265 // Refresh the main window
5268 wxRect rectMain
= m_mainWin
->GetRect();
5269 rectMain
.Intersect(*rect
);
5270 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5273 m_mainWin
->GetPosition(&x
, &y
);
5274 rectMain
.Offset(-x
, -y
);
5275 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5281 void wxGenericListCtrl::Freeze()
5283 m_mainWin
->Freeze();
5286 void wxGenericListCtrl::Thaw()
5291 #endif // wxUSE_LISTCTRL