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"
40 #include "wx/dcscreen.h"
42 #include "wx/listctrl.h"
43 #include "wx/imaglist.h"
44 #include "wx/dynarray.h"
48 #include "wx/gtk/win_gtk.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
58 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
59 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
60 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
61 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
62 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
63 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
64 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
65 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
66 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
67 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
68 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
69 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
70 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
71 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
72 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
73 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
74 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
75 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
76 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 // the height of the header window (FIXME: should depend on its font!)
83 static const int HEADER_HEIGHT
= 23;
85 // the scrollbar units
86 static const int SCROLL_UNIT_X
= 15;
87 static const int SCROLL_UNIT_Y
= 15;
89 // the spacing between the lines (in report mode)
90 static const int LINE_SPACING
= 0;
92 // extra margins around the text label
93 static const int EXTRA_WIDTH
= 3;
94 static const int EXTRA_HEIGHT
= 4;
96 // offset for the header window
97 static const int HEADER_OFFSET_X
= 1;
98 static const int HEADER_OFFSET_Y
= 1;
100 // when autosizing the columns, add some slack
101 static const int AUTOSIZE_COL_MARGIN
= 10;
103 // default and minimal widths for the header columns
104 static const int WIDTH_COL_DEFAULT
= 80;
105 static const int WIDTH_COL_MIN
= 10;
107 // the space between the image and the text in the report mode
108 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
110 // ============================================================================
112 // ============================================================================
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 int CMPFUNC_CONV
wxSizeTCmpFn(size_t n1
, size_t n2
) { return n1
- n2
; }
120 WX_DEFINE_SORTED_EXPORTED_ARRAY(size_t, wxIndexArray
);
122 // this class is used to store the selected items in the virtual list control
123 // (but it is not tied to list control and so can be used with other controls
124 // such as wxListBox in wxUniv)
126 // the idea is to make it really smart later (i.e. store the selections as an
127 // array of ranes + individual items) but, as I don't have time to do it now
128 // (this would require writing code to merge/break ranges and much more) keep
129 // it simple but define a clean interface to it which allows it to be made
131 class WXDLLEXPORT wxSelectionStore
134 wxSelectionStore() : m_itemsSel(wxSizeTCmpFn
) { Init(); }
136 // set the total number of items we handle
137 void SetItemCount(size_t count
) { m_count
= count
; }
139 // special case of SetItemCount(0)
140 void Clear() { m_itemsSel
.Clear(); m_count
= 0; }
142 // must be called when a new item is inserted/added
143 void OnItemAdd(size_t item
) { wxFAIL_MSG( _T("TODO") ); }
145 // must be called when an item is deleted
146 void OnItemDelete(size_t item
);
148 // select one item, use SelectRange() insted if possible!
150 // returns true if the items selection really changed
151 bool SelectItem(size_t item
, bool select
= TRUE
);
153 // select the range of items
155 // return true and fill the itemsChanged array with the indices of items
156 // which have changed state if "few" of them did, otherwise return false
157 // (meaning that too many items changed state to bother counting them
159 bool SelectRange(size_t itemFrom
, size_t itemTo
,
161 wxArrayInt
*itemsChanged
= NULL
);
163 // return true if the given item is selected
164 bool IsSelected(size_t item
) const;
166 // return the total number of selected items
167 size_t GetSelectedCount() const
169 return m_defaultState
? m_count
- m_itemsSel
.GetCount()
170 : m_itemsSel
.GetCount();
175 void Init() { m_defaultState
= FALSE
; }
177 // the total number of items we handle
180 // the default state: normally, FALSE (i.e. off) but maybe set to TRUE if
181 // there are more selected items than non selected ones - this allows to
182 // handle selection of all items efficiently
185 // the array of items whose selection state is different from default
186 wxIndexArray m_itemsSel
;
188 DECLARE_NO_COPY_CLASS(wxSelectionStore
)
191 //-----------------------------------------------------------------------------
192 // wxListItemData (internal)
193 //-----------------------------------------------------------------------------
195 class WXDLLEXPORT wxListItemData
198 wxListItemData(wxListMainWindow
*owner
);
201 void SetItem( const wxListItem
&info
);
202 void SetImage( int image
) { m_image
= image
; }
203 void SetData( long data
) { m_data
= data
; }
204 void SetPosition( int x
, int y
);
205 void SetSize( int width
, int height
);
207 bool HasText() const { return !m_text
.empty(); }
208 const wxString
& GetText() const { return m_text
; }
209 void SetText(const wxString
& text
) { m_text
= text
; }
211 // we can't use empty string for measuring the string width/height, so
212 // always return something
213 wxString
GetTextForMeasuring() const
215 wxString s
= GetText();
222 bool IsHit( int x
, int y
) const;
226 int GetWidth() const;
227 int GetHeight() const;
229 int GetImage() const { return m_image
; }
230 bool HasImage() const { return GetImage() != -1; }
232 void GetItem( wxListItem
&info
) const;
234 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
235 wxListItemAttr
*GetAttr() const { return m_attr
; }
238 // the item image or -1
241 // user data associated with the item
244 // the item coordinates are not used in report mode, instead this pointer
245 // is NULL and the owner window is used to retrieve the item position and
249 // the list ctrl we are in
250 wxListMainWindow
*m_owner
;
252 // custom attributes or NULL
253 wxListItemAttr
*m_attr
;
256 // common part of all ctors
262 //-----------------------------------------------------------------------------
263 // wxListHeaderData (internal)
264 //-----------------------------------------------------------------------------
266 class WXDLLEXPORT wxListHeaderData
: public wxObject
270 wxListHeaderData( const wxListItem
&info
);
271 void SetItem( const wxListItem
&item
);
272 void SetPosition( int x
, int y
);
273 void SetWidth( int w
);
274 void SetFormat( int format
);
275 void SetHeight( int h
);
276 bool HasImage() const;
278 bool HasText() const { return !m_text
.empty(); }
279 const wxString
& GetText() const { return m_text
; }
280 void SetText(const wxString
& text
) { m_text
= text
; }
282 void GetItem( wxListItem
&item
);
284 bool IsHit( int x
, int y
) const;
285 int GetImage() const;
286 int GetWidth() const;
287 int GetFormat() const;
303 //-----------------------------------------------------------------------------
304 // wxListLineData (internal)
305 //-----------------------------------------------------------------------------
307 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
308 #include "wx/listimpl.cpp"
309 WX_DEFINE_LIST(wxListItemDataList
);
311 class WXDLLEXPORT wxListLineData
314 // the list of subitems: only may have more than one item in report mode
315 wxListItemDataList m_items
;
317 // this is not used in report view
329 // the part to be highlighted
330 wxRect m_rectHighlight
;
333 // is this item selected? [NB: not used in virtual mode]
336 // back pointer to the list ctrl
337 wxListMainWindow
*m_owner
;
340 wxListLineData(wxListMainWindow
*owner
);
342 ~wxListLineData() { delete m_gi
; }
344 // are we in report mode?
345 inline bool InReportView() const;
347 // are we in virtual report mode?
348 inline bool IsVirtual() const;
350 // these 2 methods shouldn't be called for report view controls, in that
351 // case we determine our position/size ourselves
353 // calculate the size of the line
354 void CalculateSize( wxDC
*dc
, int spacing
);
356 // remember the position this line appears at
357 void SetPosition( int x
, int y
, int window_width
, int spacing
);
361 void SetImage( int image
) { SetImage(0, image
); }
362 int GetImage() const { return GetImage(0); }
363 bool HasImage() const { return GetImage() != -1; }
364 bool HasText() const { return !GetText(0).empty(); }
366 void SetItem( int index
, const wxListItem
&info
);
367 void GetItem( int index
, wxListItem
&info
);
369 wxString
GetText(int index
) const;
370 void SetText( int index
, const wxString s
);
372 wxListItemAttr
*GetAttr() const;
373 void SetAttr(wxListItemAttr
*attr
);
375 // return true if the highlighting really changed
376 bool Highlight( bool on
);
378 void ReverseHighlight();
380 bool IsHighlighted() const
382 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
384 return m_highlighted
;
387 // draw the line on the given DC in icon/list mode
388 void Draw( wxDC
*dc
);
390 // the same in report mode
391 void DrawInReportMode( wxDC
*dc
,
393 const wxRect
& rectHL
,
397 // set the line to contain num items (only can be > 1 in report mode)
398 void InitItems( int num
);
400 // get the mode (i.e. style) of the list control
401 inline int GetMode() const;
403 // prepare the DC for drawing with these item's attributes, return true if
404 // we need to draw the items background to highlight it, false otherwise
405 bool SetAttributes(wxDC
*dc
,
406 const wxListItemAttr
*attr
,
409 // these are only used by GetImage/SetImage above, we don't support images
410 // with subitems at the public API level yet
411 void SetImage( int index
, int image
);
412 int GetImage( int index
) const;
415 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
416 #include "wx/arrimpl.cpp"
417 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
419 //-----------------------------------------------------------------------------
420 // wxListHeaderWindow (internal)
421 //-----------------------------------------------------------------------------
423 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
426 wxListMainWindow
*m_owner
;
427 wxCursor
*m_currentCursor
;
428 wxCursor
*m_resizeCursor
;
431 // column being resized
434 // divider line position in logical (unscrolled) coords
437 // minimal position beyond which the divider line can't be dragged in
442 wxListHeaderWindow();
444 wxListHeaderWindow( wxWindow
*win
,
446 wxListMainWindow
*owner
,
447 const wxPoint
&pos
= wxDefaultPosition
,
448 const wxSize
&size
= wxDefaultSize
,
450 const wxString
&name
= "wxlistctrlcolumntitles" );
452 virtual ~wxListHeaderWindow();
454 void DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
);
456 void AdjustDC(wxDC
& dc
);
458 void OnPaint( wxPaintEvent
&event
);
459 void OnMouse( wxMouseEvent
&event
);
460 void OnSetFocus( wxFocusEvent
&event
);
466 // common part of all ctors
469 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
470 DECLARE_EVENT_TABLE()
473 //-----------------------------------------------------------------------------
474 // wxListRenameTimer (internal)
475 //-----------------------------------------------------------------------------
477 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
480 wxListMainWindow
*m_owner
;
483 wxListRenameTimer( wxListMainWindow
*owner
);
487 //-----------------------------------------------------------------------------
488 // wxListTextCtrl (internal)
489 //-----------------------------------------------------------------------------
491 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
496 wxListMainWindow
*m_owner
;
497 wxString m_startValue
;
501 wxListTextCtrl( wxWindow
*parent
, const wxWindowID id
,
502 bool *accept
, wxString
*res
, wxListMainWindow
*owner
,
503 const wxString
&value
= "",
504 const wxPoint
&pos
= wxDefaultPosition
, const wxSize
&size
= wxDefaultSize
,
506 const wxValidator
& validator
= wxDefaultValidator
,
507 const wxString
&name
= "listctrltextctrl" );
508 void OnChar( wxKeyEvent
&event
);
509 void OnKeyUp( wxKeyEvent
&event
);
510 void OnKillFocus( wxFocusEvent
&event
);
513 DECLARE_DYNAMIC_CLASS(wxListTextCtrl
);
514 DECLARE_EVENT_TABLE()
517 //-----------------------------------------------------------------------------
518 // wxListMainWindow (internal)
519 //-----------------------------------------------------------------------------
521 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
522 #include "wx/listimpl.cpp"
523 WX_DEFINE_LIST(wxListHeaderDataList
);
525 class WXDLLEXPORT wxListMainWindow
: public wxScrolledWindow
529 wxListMainWindow( wxWindow
*parent
,
531 const wxPoint
& pos
= wxDefaultPosition
,
532 const wxSize
& size
= wxDefaultSize
,
534 const wxString
&name
= _T("listctrlmainwindow") );
536 virtual ~wxListMainWindow();
538 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
540 // return true if this is a virtual list control
541 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
543 // return true if the control is in report mode
544 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
546 // return true if we are in single selection mode, false if multi sel
547 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
549 // do we have a header window?
550 bool HasHeader() const
551 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
553 void HighlightAll( bool on
);
555 // all these functions only do something if the line is currently visible
557 // change the line "selected" state, return TRUE if it really changed
558 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
560 // as HighlightLine() but do it for the range of lines: this is incredibly
561 // more efficient for virtual list controls!
563 // NB: unlike HighlightLine() this one does refresh the lines on screen
564 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
566 // toggle the line state and refresh it
567 void ReverseHighlight( size_t line
)
568 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
570 // return true if the line is highlighted
571 bool IsHighlighted(size_t line
) const;
573 // refresh one or several lines at once
574 void RefreshLine( size_t line
);
575 void RefreshLines( size_t lineFrom
, size_t lineTo
);
577 // refresh all selected items
578 void RefreshSelected();
580 // refresh all lines below the given one: the difference with
581 // RefreshLines() is that the index here might not be a valid one (happens
582 // when the last line is deleted)
583 void RefreshAfter( size_t lineFrom
);
585 // the methods which are forwarded to wxListLineData itself in list/icon
586 // modes but are here because the lines don't store their positions in the
589 // get the bound rect for the entire line
590 wxRect
GetLineRect(size_t line
) const;
592 // get the bound rect of the label
593 wxRect
GetLineLabelRect(size_t line
) const;
595 // get the bound rect of the items icon (only may be called if we do have
597 wxRect
GetLineIconRect(size_t line
) const;
599 // get the rect to be highlighted when the item has focus
600 wxRect
GetLineHighlightRect(size_t line
) const;
602 // get the size of the total line rect
603 wxSize
GetLineSize(size_t line
) const
604 { return GetLineRect(line
).GetSize(); }
606 // return the hit code for the corresponding position (in this line)
607 long HitTestLine(size_t line
, int x
, int y
) const;
609 // bring the selected item into view, scrolling to it if necessary
610 void MoveToItem(size_t item
);
612 // bring the current item into view
613 void MoveToFocus() { MoveToItem(m_current
); }
615 // start editing the label of the given item
616 void EditLabel( long item
);
618 // suspend/resume redrawing the control
622 void OnRenameTimer();
623 void OnRenameAccept();
625 void OnMouse( wxMouseEvent
&event
);
627 // called to switch the selection from the current item to newCurrent,
628 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
630 void OnChar( wxKeyEvent
&event
);
631 void OnKeyDown( wxKeyEvent
&event
);
632 void OnSetFocus( wxFocusEvent
&event
);
633 void OnKillFocus( wxFocusEvent
&event
);
634 void OnScroll(wxScrollWinEvent
& event
) ;
636 void OnPaint( wxPaintEvent
&event
);
638 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
639 void GetImageSize( int index
, int &width
, int &height
) const;
640 int GetTextLength( const wxString
&s
) const;
642 void SetImageList( wxImageList
*imageList
, int which
);
643 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
644 int GetItemSpacing( bool isSmall
= FALSE
);
646 void SetColumn( int col
, wxListItem
&item
);
647 void SetColumnWidth( int col
, int width
);
648 void GetColumn( int col
, wxListItem
&item
) const;
649 int GetColumnWidth( int col
) const;
650 int GetColumnCount() const { return m_columns
.GetCount(); }
652 // returns the sum of the heights of all columns
653 int GetHeaderWidth() const;
655 int GetCountPerPage() const;
657 void SetItem( wxListItem
&item
);
658 void GetItem( wxListItem
&item
);
659 void SetItemState( long item
, long state
, long stateMask
);
660 int GetItemState( long item
, long stateMask
);
661 void GetItemRect( long index
, wxRect
&rect
);
662 bool GetItemPosition( long item
, wxPoint
& pos
);
663 int GetSelectedItemCount();
665 // set the scrollbars and update the positions of the items
666 void RecalculatePositions(bool noRefresh
= FALSE
);
668 // refresh the window and the header
671 long GetNextItem( long item
, int geometry
, int state
);
672 void DeleteItem( long index
);
673 void DeleteAllItems();
674 void DeleteColumn( int col
);
675 void DeleteEverything();
676 void EnsureVisible( long index
);
677 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
678 long FindItem( long start
, long data
);
679 long HitTest( int x
, int y
, int &flags
);
680 void InsertItem( wxListItem
&item
);
681 void InsertColumn( long col
, wxListItem
&item
);
682 void SortItems( wxListCtrlCompare fn
, long data
);
684 size_t GetItemCount() const;
685 bool IsEmpty() const { return GetItemCount() == 0; }
686 void SetItemCount(long count
);
688 // change the current (== focused) item, send a notification event
689 void ChangeCurrent(size_t current
);
690 void ResetCurrent() { ChangeCurrent((size_t)-1); }
691 bool HasCurrent() const { return m_current
!= (size_t)-1; }
693 // send out a wxListEvent
694 void SendNotify( size_t line
,
696 wxPoint point
= wxDefaultPosition
);
698 // override base class virtual to reset m_lineHeight when the font changes
699 virtual bool SetFont(const wxFont
& font
)
701 if ( !wxScrolledWindow::SetFont(font
) )
709 // these are for wxListLineData usage only
711 // get the backpointer to the list ctrl
712 wxListCtrl
*GetListCtrl() const
714 return wxStaticCast(GetParent(), wxListCtrl
);
717 // get the height of all lines (assuming they all do have the same height)
718 wxCoord
GetLineHeight() const;
720 // get the y position of the given line (only for report view)
721 wxCoord
GetLineY(size_t line
) const;
723 // get the brush to use for the item highlighting
724 wxBrush
*GetHighlightBrush() const
726 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
730 // the array of all line objects for a non virtual list control
731 wxListLineDataArray m_lines
;
733 // the list of column objects
734 wxListHeaderDataList m_columns
;
736 // currently focused item or -1
739 // the item currently being edited or -1
740 size_t m_currentEdit
;
742 // the number of lines per page
745 // this flag is set when something which should result in the window
746 // redrawing happens (i.e. an item was added or deleted, or its appearance
747 // changed) and OnPaint() doesn't redraw the window while it is set which
748 // allows to minimize the number of repaintings when a lot of items are
749 // being added. The real repainting occurs only after the next OnIdle()
753 wxColour
*m_highlightColour
;
756 wxImageList
*m_small_image_list
;
757 wxImageList
*m_normal_image_list
;
759 int m_normal_spacing
;
763 wxTimer
*m_renameTimer
;
765 wxString m_renameRes
;
770 // for double click logic
771 size_t m_lineLastClicked
,
772 m_lineBeforeLastClicked
;
775 // the total count of items in a virtual list control
778 // the object maintaining the items selection state, only used in virtual
780 wxSelectionStore m_selStore
;
782 // common part of all ctors
785 // intiialize m_[xy]Scroll
786 void InitScrolling();
788 // get the line data for the given index
789 wxListLineData
*GetLine(size_t n
) const
791 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
795 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
803 // get a dummy line which can be used for geometry calculations and such:
804 // you must use GetLine() if you want to really draw the line
805 wxListLineData
*GetDummyLine() const;
807 // cache the line data of the n-th line in m_lines[0]
808 void CacheLineData(size_t line
);
810 // get the range of visible lines
811 void GetVisibleLinesRange(size_t *from
, size_t *to
);
813 // force us to recalculate the range of visible lines
814 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
816 // get the colour to be used for drawing the rules
817 wxColour
GetRuleColour() const
822 return wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
);
827 // initialize the current item if needed
828 void UpdateCurrent();
830 // delete all items but don't refresh: called from dtor
831 void DoDeleteAllItems();
833 // the height of one line using the current font
834 wxCoord m_lineHeight
;
836 // the total header width or 0 if not calculated yet
837 wxCoord m_headerWidth
;
839 // the first and last lines being shown on screen right now (inclusive),
840 // both may be -1 if they must be calculated so never access them directly:
841 // use GetVisibleLinesRange() above instead
845 // the brushes to use for item highlighting when we do/don't have focus
846 wxBrush
*m_highlightBrush
,
847 *m_highlightUnfocusedBrush
;
849 // if this is > 0, the control is frozen and doesn't redraw itself
850 size_t m_freezeCount
;
852 DECLARE_DYNAMIC_CLASS(wxListMainWindow
);
853 DECLARE_EVENT_TABLE()
856 // ============================================================================
858 // ============================================================================
860 // ----------------------------------------------------------------------------
862 // ----------------------------------------------------------------------------
864 bool wxSelectionStore::IsSelected(size_t item
) const
866 bool isSel
= m_itemsSel
.Index(item
) != wxNOT_FOUND
;
868 // if the default state is to be selected, being in m_itemsSel means that
869 // the item is not selected, so we have to inverse the logic
870 return m_defaultState
? !isSel
: isSel
;
873 bool wxSelectionStore::SelectItem(size_t item
, bool select
)
875 // search for the item ourselves as like this we get the index where to
876 // insert it later if needed, so we do only one search in the array instead
877 // of two (adding item to a sorted array requires a search)
878 size_t index
= m_itemsSel
.IndexForInsert(item
);
879 bool isSel
= index
< m_itemsSel
.GetCount() && m_itemsSel
[index
] == item
;
881 if ( select
!= m_defaultState
)
885 m_itemsSel
.AddAt(item
, index
);
890 else // reset to default state
894 m_itemsSel
.RemoveAt(index
);
902 bool wxSelectionStore::SelectRange(size_t itemFrom
, size_t itemTo
,
904 wxArrayInt
*itemsChanged
)
906 // 100 is hardcoded but it shouldn't matter much: the important thing is
907 // that we don't refresh everything when really few (e.g. 1 or 2) items
909 static const size_t MANY_ITEMS
= 100;
911 wxASSERT_MSG( itemFrom
<= itemTo
, _T("should be in order") );
913 // are we going to have more [un]selected items than the other ones?
914 if ( itemTo
- itemFrom
> m_count
/2 )
916 if ( select
!= m_defaultState
)
918 // the default state now becomes the same as 'select'
919 m_defaultState
= select
;
921 // so all the old selections (which had state select) shouldn't be
922 // selected any more, but all the other ones should
923 wxIndexArray selOld
= m_itemsSel
;
926 // TODO: it should be possible to optimize the searches a bit
927 // knowing the possible range
930 for ( item
= 0; item
< itemFrom
; item
++ )
932 if ( selOld
.Index(item
) == wxNOT_FOUND
)
933 m_itemsSel
.Add(item
);
936 for ( item
= itemTo
+ 1; item
< m_count
; item
++ )
938 if ( selOld
.Index(item
) == wxNOT_FOUND
)
939 m_itemsSel
.Add(item
);
942 // many items (> half) changed state
945 else // select == m_defaultState
947 // get the inclusive range of items between itemFrom and itemTo
948 size_t count
= m_itemsSel
.GetCount(),
949 start
= m_itemsSel
.IndexForInsert(itemFrom
),
950 end
= m_itemsSel
.IndexForInsert(itemTo
);
952 if ( start
== count
|| m_itemsSel
[start
] < itemFrom
)
957 if ( end
== count
|| m_itemsSel
[end
] > itemTo
)
964 // delete all of them (from end to avoid changing indices)
965 for ( int i
= end
; i
>= (int)start
; i
-- )
969 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
971 // stop counting (see comment below)
975 itemsChanged
->Add(m_itemsSel
[i
]);
978 m_itemsSel
.RemoveAt(i
);
983 else // "few" items change state
987 itemsChanged
->Empty();
990 // just add the items to the selection
991 for ( size_t item
= itemFrom
; item
<= itemTo
; item
++ )
993 if ( SelectItem(item
, select
) && itemsChanged
)
995 itemsChanged
->Add(item
);
997 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
999 // stop counting them, we'll just eat gobs of memory
1000 // for nothing at all - faster to refresh everything in
1002 itemsChanged
= NULL
;
1008 // we set it to NULL if there are many items changing state
1009 return itemsChanged
!= NULL
;
1012 void wxSelectionStore::OnItemDelete(size_t item
)
1014 size_t count
= m_itemsSel
.GetCount(),
1015 i
= m_itemsSel
.IndexForInsert(item
);
1017 if ( i
< count
&& m_itemsSel
[i
] == item
)
1019 // this item itself was in m_itemsSel, remove it from there
1020 m_itemsSel
.RemoveAt(i
);
1025 // and adjust the index of all which follow it
1028 // all following elements must be greater than the one we deleted
1029 wxASSERT_MSG( m_itemsSel
[i
] > item
, _T("logic error") );
1035 //-----------------------------------------------------------------------------
1037 //-----------------------------------------------------------------------------
1039 wxListItemData::~wxListItemData()
1041 // in the virtual list control the attributes are managed by the main
1042 // program, so don't delete them
1043 if ( !m_owner
->IsVirtual() )
1051 void wxListItemData::Init()
1059 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
1065 if ( owner
->InReportView() )
1071 m_rect
= new wxRect
;
1075 void wxListItemData::SetItem( const wxListItem
&info
)
1077 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
1078 SetText(info
.m_text
);
1079 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
1080 m_image
= info
.m_image
;
1081 if ( info
.m_mask
& wxLIST_MASK_DATA
)
1082 m_data
= info
.m_data
;
1084 if ( info
.HasAttributes() )
1087 *m_attr
= *info
.GetAttributes();
1089 m_attr
= new wxListItemAttr(*info
.GetAttributes());
1097 m_rect
->width
= info
.m_width
;
1101 void wxListItemData::SetPosition( int x
, int y
)
1103 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
1109 void wxListItemData::SetSize( int width
, int height
)
1111 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
1114 m_rect
->width
= width
;
1116 m_rect
->height
= height
;
1119 bool wxListItemData::IsHit( int x
, int y
) const
1121 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
1123 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
1126 int wxListItemData::GetX() const
1128 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1133 int wxListItemData::GetY() const
1135 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1140 int wxListItemData::GetWidth() const
1142 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1144 return m_rect
->width
;
1147 int wxListItemData::GetHeight() const
1149 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1151 return m_rect
->height
;
1154 void wxListItemData::GetItem( wxListItem
&info
) const
1156 info
.m_text
= m_text
;
1157 info
.m_image
= m_image
;
1158 info
.m_data
= m_data
;
1162 if ( m_attr
->HasTextColour() )
1163 info
.SetTextColour(m_attr
->GetTextColour());
1164 if ( m_attr
->HasBackgroundColour() )
1165 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1166 if ( m_attr
->HasFont() )
1167 info
.SetFont(m_attr
->GetFont());
1171 //-----------------------------------------------------------------------------
1173 //-----------------------------------------------------------------------------
1175 void wxListHeaderData::Init()
1186 wxListHeaderData::wxListHeaderData()
1191 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1198 void wxListHeaderData::SetItem( const wxListItem
&item
)
1200 m_mask
= item
.m_mask
;
1202 if ( m_mask
& wxLIST_MASK_TEXT
)
1203 m_text
= item
.m_text
;
1205 if ( m_mask
& wxLIST_MASK_IMAGE
)
1206 m_image
= item
.m_image
;
1208 if ( m_mask
& wxLIST_MASK_FORMAT
)
1209 m_format
= item
.m_format
;
1211 if ( m_mask
& wxLIST_MASK_WIDTH
)
1212 SetWidth(item
.m_width
);
1215 void wxListHeaderData::SetPosition( int x
, int y
)
1221 void wxListHeaderData::SetHeight( int h
)
1226 void wxListHeaderData::SetWidth( int w
)
1230 m_width
= WIDTH_COL_DEFAULT
;
1231 else if (m_width
< WIDTH_COL_MIN
)
1232 m_width
= WIDTH_COL_MIN
;
1235 void wxListHeaderData::SetFormat( int format
)
1240 bool wxListHeaderData::HasImage() const
1242 return m_image
!= -1;
1245 bool wxListHeaderData::IsHit( int x
, int y
) const
1247 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1250 void wxListHeaderData::GetItem( wxListItem
& item
)
1252 item
.m_mask
= m_mask
;
1253 item
.m_text
= m_text
;
1254 item
.m_image
= m_image
;
1255 item
.m_format
= m_format
;
1256 item
.m_width
= m_width
;
1259 int wxListHeaderData::GetImage() const
1264 int wxListHeaderData::GetWidth() const
1269 int wxListHeaderData::GetFormat() const
1274 //-----------------------------------------------------------------------------
1276 //-----------------------------------------------------------------------------
1278 inline int wxListLineData::GetMode() const
1280 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1283 inline bool wxListLineData::InReportView() const
1285 return m_owner
->HasFlag(wxLC_REPORT
);
1288 inline bool wxListLineData::IsVirtual() const
1290 return m_owner
->IsVirtual();
1293 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1296 m_items
.DeleteContents( TRUE
);
1298 if ( InReportView() )
1304 m_gi
= new GeometryInfo
;
1307 m_highlighted
= FALSE
;
1309 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1312 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1314 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1315 wxCHECK_RET( node
, _T("no subitems at all??") );
1317 wxListItemData
*item
= node
->GetData();
1319 switch ( GetMode() )
1322 case wxLC_SMALL_ICON
:
1324 m_gi
->m_rectAll
.width
= spacing
;
1326 wxString s
= item
->GetText();
1332 m_gi
->m_rectLabel
.width
=
1333 m_gi
->m_rectLabel
.height
= 0;
1337 dc
->GetTextExtent( s
, &lw
, &lh
);
1338 if (lh
< SCROLL_UNIT_Y
)
1343 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1345 m_gi
->m_rectAll
.width
= lw
;
1347 m_gi
->m_rectLabel
.width
= lw
;
1348 m_gi
->m_rectLabel
.height
= lh
;
1351 if (item
->HasImage())
1354 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1355 m_gi
->m_rectIcon
.width
= w
+ 8;
1356 m_gi
->m_rectIcon
.height
= h
+ 8;
1358 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1359 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1360 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1361 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1364 if ( item
->HasText() )
1366 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1367 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1369 else // no text, highlight the icon
1371 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1372 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1379 wxString s
= item
->GetTextForMeasuring();
1382 dc
->GetTextExtent( s
, &lw
, &lh
);
1383 if (lh
< SCROLL_UNIT_Y
)
1388 m_gi
->m_rectLabel
.width
= lw
;
1389 m_gi
->m_rectLabel
.height
= lh
;
1391 m_gi
->m_rectAll
.width
= lw
;
1392 m_gi
->m_rectAll
.height
= lh
;
1394 if (item
->HasImage())
1397 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1398 m_gi
->m_rectIcon
.width
= w
;
1399 m_gi
->m_rectIcon
.height
= h
;
1401 m_gi
->m_rectAll
.width
+= 4 + w
;
1402 if (h
> m_gi
->m_rectAll
.height
)
1403 m_gi
->m_rectAll
.height
= h
;
1406 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1407 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1412 wxFAIL_MSG( _T("unexpected call to SetSize") );
1416 wxFAIL_MSG( _T("unknown mode") );
1420 void wxListLineData::SetPosition( int x
, int y
,
1424 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1425 wxCHECK_RET( node
, _T("no subitems at all??") );
1427 wxListItemData
*item
= node
->GetData();
1429 switch ( GetMode() )
1432 case wxLC_SMALL_ICON
:
1433 m_gi
->m_rectAll
.x
= x
;
1434 m_gi
->m_rectAll
.y
= y
;
1436 if ( item
->HasImage() )
1438 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4
1439 + (spacing
- m_gi
->m_rectIcon
.width
)/2;
1440 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1443 if ( item
->HasText() )
1445 if (m_gi
->m_rectAll
.width
> spacing
)
1446 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1448 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1449 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1450 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1451 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1453 else // no text, highlight the icon
1455 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1456 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1461 m_gi
->m_rectAll
.x
= x
;
1462 m_gi
->m_rectAll
.y
= y
;
1464 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1465 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1466 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1468 if (item
->HasImage())
1470 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1471 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1472 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1476 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1481 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1485 wxFAIL_MSG( _T("unknown mode") );
1489 void wxListLineData::InitItems( int num
)
1491 for (int i
= 0; i
< num
; i
++)
1492 m_items
.Append( new wxListItemData(m_owner
) );
1495 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1497 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1498 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1500 wxListItemData
*item
= node
->GetData();
1501 item
->SetItem( info
);
1504 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1506 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1509 wxListItemData
*item
= node
->GetData();
1510 item
->GetItem( info
);
1514 wxString
wxListLineData::GetText(int index
) const
1518 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1521 wxListItemData
*item
= node
->GetData();
1522 s
= item
->GetText();
1528 void wxListLineData::SetText( int index
, const wxString s
)
1530 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1533 wxListItemData
*item
= node
->GetData();
1538 void wxListLineData::SetImage( int index
, int image
)
1540 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1541 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1543 wxListItemData
*item
= node
->GetData();
1544 item
->SetImage(image
);
1547 int wxListLineData::GetImage( int index
) const
1549 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1550 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1552 wxListItemData
*item
= node
->GetData();
1553 return item
->GetImage();
1556 wxListItemAttr
*wxListLineData::GetAttr() const
1558 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1559 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1561 wxListItemData
*item
= node
->GetData();
1562 return item
->GetAttr();
1565 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1567 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1568 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1570 wxListItemData
*item
= node
->GetData();
1571 item
->SetAttr(attr
);
1574 bool wxListLineData::SetAttributes(wxDC
*dc
,
1575 const wxListItemAttr
*attr
,
1578 wxWindow
*listctrl
= m_owner
->GetParent();
1582 // don't use foreground colour for drawing highlighted items - this might
1583 // make them completely invisible (and there is no way to do bit
1584 // arithmetics on wxColour, unfortunately)
1588 colText
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1592 if ( attr
&& attr
->HasTextColour() )
1594 colText
= attr
->GetTextColour();
1598 colText
= listctrl
->GetForegroundColour();
1602 dc
->SetTextForeground(colText
);
1606 if ( attr
&& attr
->HasFont() )
1608 font
= attr
->GetFont();
1612 font
= listctrl
->GetFont();
1618 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1619 if ( highlighted
|| hasBgCol
)
1623 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1627 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1630 dc
->SetPen( *wxTRANSPARENT_PEN
);
1638 void wxListLineData::Draw( wxDC
*dc
)
1640 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1641 wxCHECK_RET( node
, _T("no subitems at all??") );
1643 bool highlighted
= IsHighlighted();
1645 wxListItemAttr
*attr
= GetAttr();
1647 if ( SetAttributes(dc
, attr
, highlighted
) )
1649 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1652 wxListItemData
*item
= node
->GetData();
1653 if (item
->HasImage())
1655 wxRect rectIcon
= m_gi
->m_rectIcon
;
1656 m_owner
->DrawImage( item
->GetImage(), dc
,
1657 rectIcon
.x
, rectIcon
.y
);
1660 if (item
->HasText())
1662 wxRect rectLabel
= m_gi
->m_rectLabel
;
1664 wxDCClipper
clipper(*dc
, rectLabel
);
1665 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1669 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1671 const wxRect
& rectHL
,
1674 // TODO: later we should support setting different attributes for
1675 // different columns - to do it, just add "col" argument to
1676 // GetAttr() and move these lines into the loop below
1677 wxListItemAttr
*attr
= GetAttr();
1678 if ( SetAttributes(dc
, attr
, highlighted
) )
1680 dc
->DrawRectangle( rectHL
);
1683 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1684 wxCHECK_RET( node
, _T("no subitems at all??") );
1687 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1688 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1692 wxListItemData
*item
= node
->GetData();
1694 int width
= m_owner
->GetColumnWidth(col
++);
1698 if ( item
->HasImage() )
1701 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1702 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1704 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1710 wxDCClipper
clipper(*dc
, xOld
, y
, width
, rect
.height
);
1712 if ( item
->HasText() )
1714 dc
->DrawText( item
->GetText(), xOld
, y
);
1717 node
= node
->GetNext();
1721 bool wxListLineData::Highlight( bool on
)
1723 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1725 if ( on
== m_highlighted
)
1733 void wxListLineData::ReverseHighlight( void )
1735 Highlight(!IsHighlighted());
1738 //-----------------------------------------------------------------------------
1739 // wxListHeaderWindow
1740 //-----------------------------------------------------------------------------
1742 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
);
1744 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1745 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1746 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1747 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1750 void wxListHeaderWindow::Init()
1752 m_currentCursor
= (wxCursor
*) NULL
;
1753 m_isDragging
= FALSE
;
1757 wxListHeaderWindow::wxListHeaderWindow()
1761 m_owner
= (wxListMainWindow
*) NULL
;
1762 m_resizeCursor
= (wxCursor
*) NULL
;
1765 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1767 wxListMainWindow
*owner
,
1771 const wxString
&name
)
1772 : wxWindow( win
, id
, pos
, size
, style
, name
)
1777 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1779 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
) );
1782 wxListHeaderWindow::~wxListHeaderWindow()
1784 delete m_resizeCursor
;
1787 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1790 GtkStateType state
= m_parent
->IsEnabled() ? GTK_STATE_NORMAL
1791 : GTK_STATE_INSENSITIVE
;
1793 x
= dc
->XLOG2DEV( x
);
1795 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
,
1796 state
, GTK_SHADOW_OUT
,
1797 (GdkRectangle
*) NULL
, m_wxwindow
, "button",
1798 x
-1, y
-1, w
+2, h
+2);
1799 #elif defined( __WXMAC__ )
1800 const int m_corner
= 1;
1802 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1804 dc
->SetPen( wxPen( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1805 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1806 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1808 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1811 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1812 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1814 dc
->SetPen( *wxWHITE_PEN
);
1815 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1816 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1817 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1818 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1820 const int m_corner
= 1;
1822 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1824 dc
->SetPen( *wxBLACK_PEN
);
1825 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1826 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1828 wxPen
pen( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1831 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1832 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1834 dc
->SetPen( *wxWHITE_PEN
);
1835 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1836 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1837 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1838 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1842 // shift the DC origin to match the position of the main window horz
1843 // scrollbar: this allows us to always use logical coords
1844 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1847 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1850 m_owner
->GetViewStart( &x
, NULL
);
1852 // account for the horz scrollbar offset
1853 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1856 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1859 wxClientDC
dc( this );
1861 wxPaintDC
dc( this );
1869 dc
.SetFont( GetFont() );
1871 // width and height of the entire header window
1873 GetClientSize( &w
, &h
);
1874 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1876 dc
.SetBackgroundMode(wxTRANSPARENT
);
1878 // do *not* use the listctrl colour for headers - one day we will have a
1879 // function to set it separately
1880 //dc.SetTextForeground( *wxBLACK );
1881 dc
.SetTextForeground(wxSystemSettings::
1882 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1884 int x
= HEADER_OFFSET_X
;
1886 int numColumns
= m_owner
->GetColumnCount();
1888 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1890 m_owner
->GetColumn( i
, item
);
1891 int wCol
= item
.m_width
;
1893 // the width of the rect to draw: make it smaller to fit entirely
1894 // inside the column rect
1897 dc
.SetPen( *wxWHITE_PEN
);
1899 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
1901 // if we have an image, draw it on the right of the label
1902 int image
= item
.m_image
;
1905 wxImageList
*imageList
= m_owner
->m_small_image_list
;
1909 imageList
->GetSize(image
, ix
, iy
);
1916 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1917 wxIMAGELIST_DRAW_TRANSPARENT
1922 //else: ignore the column image
1925 // draw the text clipping it so that it doesn't overwrite the column
1927 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1929 dc
.DrawText( item
.GetText(),
1930 x
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1938 void wxListHeaderWindow::DrawCurrent()
1940 int x1
= m_currentX
;
1942 ClientToScreen( &x1
, &y1
);
1944 int x2
= m_currentX
-1;
1946 m_owner
->GetClientSize( NULL
, &y2
);
1947 m_owner
->ClientToScreen( &x2
, &y2
);
1950 dc
.SetLogicalFunction( wxINVERT
);
1951 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1952 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1956 dc
.DrawLine( x1
, y1
, x2
, y2
);
1958 dc
.SetLogicalFunction( wxCOPY
);
1960 dc
.SetPen( wxNullPen
);
1961 dc
.SetBrush( wxNullBrush
);
1964 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1966 // we want to work with logical coords
1968 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1969 int y
= event
.GetY();
1973 // we don't draw the line beyond our window, but we allow dragging it
1976 GetClientSize( &w
, NULL
);
1977 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1980 // erase the line if it was drawn
1981 if ( m_currentX
< w
)
1984 if (event
.ButtonUp())
1987 m_isDragging
= FALSE
;
1989 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1996 m_currentX
= m_minX
+ 7;
1998 // draw in the new location
1999 if ( m_currentX
< w
)
2003 else // not dragging
2006 bool hit_border
= FALSE
;
2008 // end of the current column
2011 // find the column where this event occured
2012 int countCol
= m_owner
->GetColumnCount();
2013 for (int col
= 0; col
< countCol
; col
++)
2015 xpos
+= m_owner
->GetColumnWidth( col
);
2018 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2020 // near the column border
2027 // inside the column
2034 if (event
.LeftDown() || event
.RightUp())
2036 if (hit_border
&& event
.LeftDown())
2038 m_isDragging
= TRUE
;
2043 else // click on a column
2045 wxWindow
*parent
= GetParent();
2046 wxListEvent
le( event
.LeftDown()
2047 ? wxEVT_COMMAND_LIST_COL_CLICK
2048 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2050 le
.SetEventObject( parent
);
2051 le
.m_pointDrag
= event
.GetPosition();
2053 // the position should be relative to the parent window, not
2054 // this one for compatibility with MSW and common sense: the
2055 // user code doesn't know anything at all about this header
2056 // window, so why should it get positions relative to it?
2057 le
.m_pointDrag
.y
-= GetSize().y
;
2059 le
.m_col
= m_column
;
2060 parent
->GetEventHandler()->ProcessEvent( le
);
2063 else if (event
.Moving())
2068 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2069 m_currentCursor
= m_resizeCursor
;
2073 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2074 m_currentCursor
= wxSTANDARD_CURSOR
;
2078 SetCursor(*m_currentCursor
);
2083 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2085 m_owner
->SetFocus();
2088 //-----------------------------------------------------------------------------
2089 // wxListRenameTimer (internal)
2090 //-----------------------------------------------------------------------------
2092 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2097 void wxListRenameTimer::Notify()
2099 m_owner
->OnRenameTimer();
2102 //-----------------------------------------------------------------------------
2103 // wxListTextCtrl (internal)
2104 //-----------------------------------------------------------------------------
2106 IMPLEMENT_DYNAMIC_CLASS(wxListTextCtrl
,wxTextCtrl
);
2108 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2109 EVT_CHAR (wxListTextCtrl::OnChar
)
2110 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2111 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2114 wxListTextCtrl::wxListTextCtrl( wxWindow
*parent
,
2115 const wxWindowID id
,
2118 wxListMainWindow
*owner
,
2119 const wxString
&value
,
2123 const wxValidator
& validator
,
2124 const wxString
&name
)
2125 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
2130 (*m_accept
) = FALSE
;
2132 m_startValue
= value
;
2135 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2137 if (event
.m_keyCode
== WXK_RETURN
)
2140 (*m_res
) = GetValue();
2142 if (!wxPendingDelete
.Member(this))
2143 wxPendingDelete
.Append(this);
2145 if ((*m_accept
) && ((*m_res
) != m_startValue
))
2146 m_owner
->OnRenameAccept();
2150 if (event
.m_keyCode
== WXK_ESCAPE
)
2152 (*m_accept
) = FALSE
;
2155 if (!wxPendingDelete
.Member(this))
2156 wxPendingDelete
.Append(this);
2164 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2166 // auto-grow the textctrl:
2167 wxSize parentSize
= m_owner
->GetSize();
2168 wxPoint myPos
= GetPosition();
2169 wxSize mySize
= GetSize();
2171 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
); // FIXME: MM??
2172 if (myPos
.x
+ sx
> parentSize
.x
)
2173 sx
= parentSize
.x
- myPos
.x
;
2181 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2183 if (!wxPendingDelete
.Member(this))
2184 wxPendingDelete
.Append(this);
2186 if ((*m_accept
) && ((*m_res
) != m_startValue
))
2187 m_owner
->OnRenameAccept();
2190 //-----------------------------------------------------------------------------
2192 //-----------------------------------------------------------------------------
2194 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
);
2196 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2197 EVT_PAINT (wxListMainWindow::OnPaint
)
2198 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2199 EVT_CHAR (wxListMainWindow::OnChar
)
2200 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2201 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2202 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2203 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2206 void wxListMainWindow::Init()
2208 m_columns
.DeleteContents( TRUE
);
2212 m_lineTo
= (size_t)-1;
2218 m_small_image_list
= (wxImageList
*) NULL
;
2219 m_normal_image_list
= (wxImageList
*) NULL
;
2221 m_small_spacing
= 30;
2222 m_normal_spacing
= 40;
2226 m_isCreated
= FALSE
;
2228 m_lastOnSame
= FALSE
;
2229 m_renameTimer
= new wxListRenameTimer( this );
2230 m_renameAccept
= FALSE
;
2235 m_lineBeforeLastClicked
= (size_t)-1;
2240 void wxListMainWindow::InitScrolling()
2242 if ( HasFlag(wxLC_REPORT
) )
2244 m_xScroll
= SCROLL_UNIT_X
;
2245 m_yScroll
= SCROLL_UNIT_Y
;
2249 m_xScroll
= SCROLL_UNIT_Y
;
2254 wxListMainWindow::wxListMainWindow()
2259 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2265 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2270 const wxString
&name
)
2271 : wxScrolledWindow( parent
, id
, pos
, size
,
2272 style
| wxHSCROLL
| wxVSCROLL
, name
)
2276 m_highlightBrush
= new wxBrush
2278 wxSystemSettings::GetSystemColour
2280 wxSYS_COLOUR_HIGHLIGHT
2285 m_highlightUnfocusedBrush
= new wxBrush
2287 wxSystemSettings::GetSystemColour
2289 wxSYS_COLOUR_BTNSHADOW
2298 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2300 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
2303 wxListMainWindow::~wxListMainWindow()
2307 delete m_highlightBrush
;
2308 delete m_highlightUnfocusedBrush
;
2310 delete m_renameTimer
;
2313 void wxListMainWindow::CacheLineData(size_t line
)
2315 wxListCtrl
*listctrl
= GetListCtrl();
2317 wxListLineData
*ld
= GetDummyLine();
2319 size_t countCol
= GetColumnCount();
2320 for ( size_t col
= 0; col
< countCol
; col
++ )
2322 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2325 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2326 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2329 wxListLineData
*wxListMainWindow::GetDummyLine() const
2331 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2333 if ( m_lines
.IsEmpty() )
2335 // normal controls are supposed to have something in m_lines
2336 // already if it's not empty
2337 wxASSERT_MSG( IsVirtual(), _T("logic error") );
2339 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2340 wxListLineData
*line
= new wxListLineData(self
);
2341 self
->m_lines
.Add(line
);
2347 // ----------------------------------------------------------------------------
2348 // line geometry (report mode only)
2349 // ----------------------------------------------------------------------------
2351 wxCoord
wxListMainWindow::GetLineHeight() const
2353 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2355 // we cache the line height as calling GetTextExtent() is slow
2356 if ( !m_lineHeight
)
2358 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2360 wxClientDC
dc( self
);
2361 dc
.SetFont( GetFont() );
2364 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2366 if ( y
< SCROLL_UNIT_Y
)
2370 self
->m_lineHeight
= y
+ LINE_SPACING
;
2373 return m_lineHeight
;
2376 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2378 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2380 return LINE_SPACING
+ line
*GetLineHeight();
2383 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2385 if ( !InReportView() )
2386 return GetLine(line
)->m_gi
->m_rectAll
;
2389 rect
.x
= HEADER_OFFSET_X
;
2390 rect
.y
= GetLineY(line
);
2391 rect
.width
= GetHeaderWidth();
2392 rect
.height
= GetLineHeight();
2397 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2399 if ( !InReportView() )
2400 return GetLine(line
)->m_gi
->m_rectLabel
;
2403 rect
.x
= HEADER_OFFSET_X
;
2404 rect
.y
= GetLineY(line
);
2405 rect
.width
= GetColumnWidth(0);
2406 rect
.height
= GetLineHeight();
2411 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2413 if ( !InReportView() )
2414 return GetLine(line
)->m_gi
->m_rectIcon
;
2416 wxListLineData
*ld
= GetLine(line
);
2417 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2420 rect
.x
= HEADER_OFFSET_X
;
2421 rect
.y
= GetLineY(line
);
2422 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2427 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2429 return InReportView() ? GetLineRect(line
)
2430 : GetLine(line
)->m_gi
->m_rectHighlight
;
2433 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2435 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2437 wxListLineData
*ld
= GetLine(line
);
2439 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2440 return wxLIST_HITTEST_ONITEMICON
;
2442 if ( ld
->HasText() )
2444 wxRect rect
= InReportView() ? GetLineRect(line
)
2445 : GetLineLabelRect(line
);
2447 if ( rect
.Inside(x
, y
) )
2448 return wxLIST_HITTEST_ONITEMLABEL
;
2454 // ----------------------------------------------------------------------------
2455 // highlight (selection) handling
2456 // ----------------------------------------------------------------------------
2458 bool wxListMainWindow::IsHighlighted(size_t line
) const
2462 return m_selStore
.IsSelected(line
);
2466 wxListLineData
*ld
= GetLine(line
);
2467 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2469 return ld
->IsHighlighted();
2473 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2479 wxArrayInt linesChanged
;
2480 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2483 // meny items changed state, refresh everything
2484 RefreshLines(lineFrom
, lineTo
);
2486 else // only a few items changed state, refresh only them
2488 size_t count
= linesChanged
.GetCount();
2489 for ( size_t n
= 0; n
< count
; n
++ )
2491 RefreshLine(linesChanged
[n
]);
2495 else // iterate over all items in non report view
2497 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2499 if ( HighlightLine(line
, highlight
) )
2507 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2513 changed
= m_selStore
.SelectItem(line
, highlight
);
2517 wxListLineData
*ld
= GetLine(line
);
2518 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2520 changed
= ld
->Highlight(highlight
);
2525 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2526 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2532 void wxListMainWindow::RefreshLine( size_t line
)
2534 if ( HasFlag(wxLC_REPORT
) )
2536 size_t visibleFrom
, visibleTo
;
2537 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2539 if ( line
< visibleFrom
|| line
> visibleTo
)
2543 wxRect rect
= GetLineRect(line
);
2545 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2546 RefreshRect( rect
);
2549 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2551 // we suppose that they are ordered by caller
2552 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2554 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2556 if ( HasFlag(wxLC_REPORT
) )
2558 size_t visibleFrom
, visibleTo
;
2559 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2561 if ( lineFrom
< visibleFrom
)
2562 lineFrom
= visibleFrom
;
2563 if ( lineTo
> visibleTo
)
2568 rect
.y
= GetLineY(lineFrom
);
2569 rect
.width
= GetClientSize().x
;
2570 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2572 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2573 RefreshRect( rect
);
2577 // TODO: this should be optimized...
2578 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2585 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2587 if ( HasFlag(wxLC_REPORT
) )
2590 GetVisibleLinesRange(&visibleFrom
, NULL
);
2592 if ( lineFrom
< visibleFrom
)
2593 lineFrom
= visibleFrom
;
2597 rect
.y
= GetLineY(lineFrom
);
2599 wxSize size
= GetClientSize();
2600 rect
.width
= size
.x
;
2601 // refresh till the bottom of the window
2602 rect
.height
= size
.y
- rect
.y
;
2604 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2605 RefreshRect( rect
);
2609 // TODO: how to do it more efficiently?
2614 void wxListMainWindow::RefreshSelected()
2620 if ( InReportView() )
2622 GetVisibleLinesRange(&from
, &to
);
2627 to
= GetItemCount() - 1;
2630 // VZ: this code would work fine if wxGTK wxWindow::Refresh() were
2631 // reasonable, i.e. if it only generated one expose event for
2632 // several calls to it - as it is, each Refresh() results in a
2633 // repaint which provokes flicker too horrible to be seen
2635 // when/if wxGTK is fixed, this code should be restored as normally it
2636 // should generate _less_ flicker than the version below
2638 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2640 RefreshLine(m_current
);
2643 for ( size_t line
= from
; line
<= to
; line
++ )
2645 // NB: the test works as expected even if m_current == -1
2646 if ( line
!= m_current
&& IsHighlighted(line
) )
2652 size_t selMin
= (size_t)-1,
2655 for ( size_t line
= from
; line
<= to
; line
++ )
2657 if ( IsHighlighted(line
) || (line
== m_current
) )
2659 if ( line
< selMin
)
2661 if ( line
> selMax
)
2666 if ( selMin
!= (size_t)-1 )
2668 RefreshLines(selMin
, selMax
);
2670 #endif // !__WXGTK__/__WXGTK__
2673 void wxListMainWindow::Freeze()
2678 void wxListMainWindow::Thaw()
2680 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2682 if ( !--m_freezeCount
)
2688 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2690 // Note: a wxPaintDC must be constructed even if no drawing is
2691 // done (a Windows requirement).
2692 wxPaintDC
dc( this );
2694 if ( IsEmpty() || m_freezeCount
)
2696 // nothing to draw or not the moment to draw it
2702 // delay the repainting until we calculate all the items positions
2709 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2713 dc
.SetFont( GetFont() );
2715 if ( HasFlag(wxLC_REPORT
) )
2717 int lineHeight
= GetLineHeight();
2719 size_t visibleFrom
, visibleTo
;
2720 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2723 wxCoord xOrig
, yOrig
;
2724 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2726 // tell the caller cache to cache the data
2729 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2730 GetParent()->GetId());
2731 evCache
.SetEventObject( GetParent() );
2732 evCache
.m_oldItemIndex
= visibleFrom
;
2733 evCache
.m_itemIndex
= visibleTo
;
2734 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2737 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2739 rectLine
= GetLineRect(line
);
2741 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2742 rectLine
.width
, rectLine
.height
) )
2744 // don't redraw unaffected lines to avoid flicker
2748 GetLine(line
)->DrawInReportMode( &dc
,
2750 GetLineHighlightRect(line
),
2751 IsHighlighted(line
) );
2754 if ( HasFlag(wxLC_HRULES
) )
2756 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2757 wxSize clientSize
= GetClientSize();
2759 for ( size_t i
= visibleFrom
; i
<= visibleTo
; i
++ )
2762 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2763 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2764 clientSize
.x
- dev_x
, i
*lineHeight
);
2767 // Draw last horizontal rule
2768 if ( visibleTo
> visibleFrom
)
2771 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2772 dc
.DrawLine(0 - dev_x
, m_lineTo
*lineHeight
,
2773 clientSize
.x
- dev_x
, m_lineTo
*lineHeight
);
2777 // Draw vertical rules if required
2778 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2780 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2783 wxRect firstItemRect
;
2784 wxRect lastItemRect
;
2785 GetItemRect(0, firstItemRect
);
2786 GetItemRect(GetItemCount() - 1, lastItemRect
);
2787 int x
= firstItemRect
.GetX();
2789 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2790 for (col
= 0; col
< GetColumnCount(); col
++)
2792 int colWidth
= GetColumnWidth(col
);
2794 dc
.DrawLine(x
- dev_x
, firstItemRect
.GetY() - 1 - dev_y
,
2795 x
- dev_x
, lastItemRect
.GetBottom() + 1 - dev_y
);
2801 size_t count
= GetItemCount();
2802 for ( size_t i
= 0; i
< count
; i
++ )
2804 GetLine(i
)->Draw( &dc
);
2810 // don't draw rect outline under Max if we already have the background
2811 // color but under other platforms only draw it if we do: it is a bit
2812 // silly to draw "focus rect" if we don't have focus!
2817 #endif // __WXMAC__/!__WXMAC__
2819 dc
.SetPen( *wxBLACK_PEN
);
2820 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2821 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2828 void wxListMainWindow::HighlightAll( bool on
)
2830 if ( IsSingleSel() )
2832 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2834 // we just have one item to turn off
2835 if ( HasCurrent() && IsHighlighted(m_current
) )
2837 HighlightLine(m_current
, FALSE
);
2838 RefreshLine(m_current
);
2843 HighlightLines(0, GetItemCount() - 1, on
);
2847 void wxListMainWindow::SendNotify( size_t line
,
2848 wxEventType command
,
2851 wxListEvent
le( command
, GetParent()->GetId() );
2852 le
.SetEventObject( GetParent() );
2853 le
.m_itemIndex
= line
;
2855 // set only for events which have position
2856 if ( point
!= wxDefaultPosition
)
2857 le
.m_pointDrag
= point
;
2859 // don't try to get the line info for virtual list controls: the main
2860 // program has it anyhow and if we did it would result in accessing all
2861 // the lines, even those which are not visible now and this is precisely
2862 // what we're trying to avoid
2863 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2865 if ( line
!= (size_t)-1 )
2867 GetLine(line
)->GetItem( 0, le
.m_item
);
2869 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2871 //else: there may be no more such item
2873 GetParent()->GetEventHandler()->ProcessEvent( le
);
2876 void wxListMainWindow::ChangeCurrent(size_t current
)
2878 m_current
= current
;
2880 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2883 void wxListMainWindow::EditLabel( long item
)
2885 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2886 wxT("wrong index in wxListCtrl::EditLabel()") );
2888 m_currentEdit
= (size_t)item
;
2890 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2891 le
.SetEventObject( GetParent() );
2892 le
.m_itemIndex
= item
;
2893 wxListLineData
*data
= GetLine(m_currentEdit
);
2894 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2895 data
->GetItem( 0, le
.m_item
);
2896 GetParent()->GetEventHandler()->ProcessEvent( le
);
2898 if (!le
.IsAllowed())
2901 // We have to call this here because the label in question might just have
2902 // been added and no screen update taken place.
2906 wxClientDC
dc(this);
2909 wxString s
= data
->GetText(0);
2910 wxRect rectLabel
= GetLineLabelRect(m_currentEdit
);
2912 rectLabel
.x
= dc
.LogicalToDeviceX( rectLabel
.x
);
2913 rectLabel
.y
= dc
.LogicalToDeviceY( rectLabel
.y
);
2915 wxListTextCtrl
*text
= new wxListTextCtrl
2922 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2923 wxSize(rectLabel
.width
+11,rectLabel
.height
+8)
2928 void wxListMainWindow::OnRenameTimer()
2930 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2932 EditLabel( m_current
);
2935 void wxListMainWindow::OnRenameAccept()
2937 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2938 le
.SetEventObject( GetParent() );
2939 le
.m_itemIndex
= m_currentEdit
;
2941 wxListLineData
*data
= GetLine(m_currentEdit
);
2942 wxCHECK_RET( data
, _T("invalid index in OnRenameAccept()") );
2944 data
->GetItem( 0, le
.m_item
);
2945 le
.m_item
.m_text
= m_renameRes
;
2946 GetParent()->GetEventHandler()->ProcessEvent( le
);
2948 if (!le
.IsAllowed()) return;
2951 info
.m_mask
= wxLIST_MASK_TEXT
;
2952 info
.m_itemId
= le
.m_itemIndex
;
2953 info
.m_text
= m_renameRes
;
2954 info
.SetTextColour(le
.m_item
.GetTextColour());
2958 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2960 event
.SetEventObject( GetParent() );
2961 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2964 if ( !HasCurrent() || IsEmpty() )
2970 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2971 event
.ButtonDClick()) )
2974 int x
= event
.GetX();
2975 int y
= event
.GetY();
2976 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2978 // where did we hit it (if we did)?
2981 size_t count
= GetItemCount(),
2984 if ( HasFlag(wxLC_REPORT
) )
2986 current
= y
/ GetLineHeight();
2987 if ( current
< count
)
2988 hitResult
= HitTestLine(current
, x
, y
);
2992 // TODO: optimize it too! this is less simple than for report view but
2993 // enumerating all items is still not a way to do it!!
2994 for ( current
= 0; current
< count
; current
++ )
2996 hitResult
= HitTestLine(current
, x
, y
);
3002 if (event
.Dragging())
3004 if (m_dragCount
== 0)
3006 // we have to report the raw, physical coords as we want to be
3007 // able to call HitTest(event.m_pointDrag) from the user code to
3008 // get the item being dragged
3009 m_dragStart
= event
.GetPosition();
3014 if (m_dragCount
!= 3)
3017 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3018 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3020 wxListEvent
le( command
, GetParent()->GetId() );
3021 le
.SetEventObject( GetParent() );
3022 le
.m_pointDrag
= m_dragStart
;
3023 GetParent()->GetEventHandler()->ProcessEvent( le
);
3034 // outside of any item
3038 bool forceClick
= FALSE
;
3039 if (event
.ButtonDClick())
3041 m_renameTimer
->Stop();
3042 m_lastOnSame
= FALSE
;
3044 if ( current
== m_lineBeforeLastClicked
)
3046 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3052 // the first click was on another item, so don't interpret this as
3053 // a double click, but as a simple click instead
3058 if (event
.LeftUp() && m_lastOnSame
)
3060 if ((current
== m_current
) &&
3061 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3062 HasFlag(wxLC_EDIT_LABELS
) )
3064 m_renameTimer
->Start( 100, TRUE
);
3066 m_lastOnSame
= FALSE
;
3068 else if (event
.RightDown())
3070 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
3071 event
.GetPosition() );
3073 else if (event
.MiddleDown())
3075 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3077 else if ( event
.LeftDown() || forceClick
)
3079 m_lineBeforeLastClicked
= m_lineLastClicked
;
3080 m_lineLastClicked
= current
;
3082 size_t oldCurrent
= m_current
;
3084 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3086 HighlightAll( FALSE
);
3088 ChangeCurrent(current
);
3090 ReverseHighlight(m_current
);
3092 else // multi sel & either ctrl or shift is down
3094 if (event
.ControlDown())
3096 ChangeCurrent(current
);
3098 ReverseHighlight(m_current
);
3100 else if (event
.ShiftDown())
3102 ChangeCurrent(current
);
3104 size_t lineFrom
= oldCurrent
,
3107 if ( lineTo
< lineFrom
)
3110 lineFrom
= m_current
;
3113 HighlightLines(lineFrom
, lineTo
);
3115 else // !ctrl, !shift
3117 // test in the enclosing if should make it impossible
3118 wxFAIL_MSG( _T("how did we get here?") );
3122 if (m_current
!= oldCurrent
)
3124 RefreshLine( oldCurrent
);
3127 // forceClick is only set if the previous click was on another item
3128 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3132 void wxListMainWindow::MoveToItem(size_t item
)
3134 if ( item
== (size_t)-1 )
3137 wxRect rect
= GetLineRect(item
);
3139 int client_w
, client_h
;
3140 GetClientSize( &client_w
, &client_h
);
3142 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3143 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3145 if ( HasFlag(wxLC_REPORT
) )
3147 // the next we need the range of lines shown it might be different, so
3149 ResetVisibleLinesRange();
3151 if (rect
.y
< view_y
)
3152 Scroll( -1, rect
.y
/m_yScroll
);
3153 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3154 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3158 if (rect
.x
-view_x
< 5)
3159 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3160 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3161 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3165 // ----------------------------------------------------------------------------
3166 // keyboard handling
3167 // ----------------------------------------------------------------------------
3169 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3171 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3172 _T("invalid item index in OnArrowChar()") );
3174 size_t oldCurrent
= m_current
;
3176 // in single selection we just ignore Shift as we can't select several
3178 if ( event
.ShiftDown() && !IsSingleSel() )
3180 ChangeCurrent(newCurrent
);
3182 // select all the items between the old and the new one
3183 if ( oldCurrent
> newCurrent
)
3185 newCurrent
= oldCurrent
;
3186 oldCurrent
= m_current
;
3189 HighlightLines(oldCurrent
, newCurrent
);
3193 // all previously selected items are unselected unless ctrl is held
3194 if ( !event
.ControlDown() )
3195 HighlightAll(FALSE
);
3197 ChangeCurrent(newCurrent
);
3199 HighlightLine( oldCurrent
, FALSE
);
3200 RefreshLine( oldCurrent
);
3202 if ( !event
.ControlDown() )
3204 HighlightLine( m_current
, TRUE
);
3208 RefreshLine( m_current
);
3213 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3215 wxWindow
*parent
= GetParent();
3217 /* we propagate the key event up */
3218 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3219 ke
.m_shiftDown
= event
.m_shiftDown
;
3220 ke
.m_controlDown
= event
.m_controlDown
;
3221 ke
.m_altDown
= event
.m_altDown
;
3222 ke
.m_metaDown
= event
.m_metaDown
;
3223 ke
.m_keyCode
= event
.m_keyCode
;
3226 ke
.SetEventObject( parent
);
3227 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3232 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3234 wxWindow
*parent
= GetParent();
3236 /* we send a list_key event up */
3239 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3240 le
.m_itemIndex
= m_current
;
3241 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3242 le
.m_code
= (int)event
.KeyCode();
3243 le
.SetEventObject( parent
);
3244 parent
->GetEventHandler()->ProcessEvent( le
);
3247 /* we propagate the char event up */
3248 wxKeyEvent
ke( wxEVT_CHAR
);
3249 ke
.m_shiftDown
= event
.m_shiftDown
;
3250 ke
.m_controlDown
= event
.m_controlDown
;
3251 ke
.m_altDown
= event
.m_altDown
;
3252 ke
.m_metaDown
= event
.m_metaDown
;
3253 ke
.m_keyCode
= event
.m_keyCode
;
3256 ke
.SetEventObject( parent
);
3257 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3259 if (event
.KeyCode() == WXK_TAB
)
3261 wxNavigationKeyEvent nevent
;
3262 nevent
.SetWindowChange( event
.ControlDown() );
3263 nevent
.SetDirection( !event
.ShiftDown() );
3264 nevent
.SetEventObject( GetParent()->GetParent() );
3265 nevent
.SetCurrentFocus( m_parent
);
3266 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3270 /* no item -> nothing to do */
3277 switch (event
.KeyCode())
3280 if ( m_current
> 0 )
3281 OnArrowChar( m_current
- 1, event
);
3285 if ( m_current
< (size_t)GetItemCount() - 1 )
3286 OnArrowChar( m_current
+ 1, event
);
3291 OnArrowChar( GetItemCount() - 1, event
);
3296 OnArrowChar( 0, event
);
3302 if ( HasFlag(wxLC_REPORT
) )
3304 steps
= m_linesPerPage
- 1;
3308 steps
= m_current
% m_linesPerPage
;
3311 int index
= m_current
- steps
;
3315 OnArrowChar( index
, event
);
3322 if ( HasFlag(wxLC_REPORT
) )
3324 steps
= m_linesPerPage
- 1;
3328 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3331 size_t index
= m_current
+ steps
;
3332 size_t count
= GetItemCount();
3333 if ( index
>= count
)
3336 OnArrowChar( index
, event
);
3341 if ( !HasFlag(wxLC_REPORT
) )
3343 int index
= m_current
- m_linesPerPage
;
3347 OnArrowChar( index
, event
);
3352 if ( !HasFlag(wxLC_REPORT
) )
3354 size_t index
= m_current
+ m_linesPerPage
;
3356 size_t count
= GetItemCount();
3357 if ( index
>= count
)
3360 OnArrowChar( index
, event
);
3365 if ( IsSingleSel() )
3367 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3369 if ( IsHighlighted(m_current
) )
3371 // don't unselect the item in single selection mode
3374 //else: select it in ReverseHighlight() below if unselected
3377 ReverseHighlight(m_current
);
3382 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3390 // ----------------------------------------------------------------------------
3392 // ----------------------------------------------------------------------------
3395 extern wxWindow
*g_focusWindow
;
3398 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3400 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3401 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3402 // which are already drawn correctly resulting in horrible flicker - avoid
3415 g_focusWindow
= GetParent();
3418 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3419 event
.SetEventObject( GetParent() );
3420 GetParent()->GetEventHandler()->ProcessEvent( event
);
3423 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3430 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3432 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3434 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3436 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3438 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3440 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3442 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3444 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3446 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3450 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3452 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3454 m_normal_image_list
->GetSize( index
, width
, height
);
3456 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3458 m_small_image_list
->GetSize( index
, width
, height
);
3460 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3462 m_small_image_list
->GetSize( index
, width
, height
);
3464 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3466 m_small_image_list
->GetSize( index
, width
, height
);
3475 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3477 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3478 dc
.SetFont( GetFont() );
3481 dc
.GetTextExtent( s
, &lw
, NULL
);
3483 return lw
+ AUTOSIZE_COL_MARGIN
;
3486 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
3490 // calc the spacing from the icon size
3493 if ((imageList
) && (imageList
->GetImageCount()) )
3495 imageList
->GetSize(0, width
, height
);
3498 if (which
== wxIMAGE_LIST_NORMAL
)
3500 m_normal_image_list
= imageList
;
3501 m_normal_spacing
= width
+ 8;
3504 if (which
== wxIMAGE_LIST_SMALL
)
3506 m_small_image_list
= imageList
;
3507 m_small_spacing
= width
+ 14;
3511 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3516 m_small_spacing
= spacing
;
3520 m_normal_spacing
= spacing
;
3524 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3526 return isSmall
? m_small_spacing
: m_normal_spacing
;
3529 // ----------------------------------------------------------------------------
3531 // ----------------------------------------------------------------------------
3533 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3535 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3537 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3539 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3540 item
.m_width
= GetTextLength( item
.m_text
);
3542 wxListHeaderData
*column
= node
->GetData();
3543 column
->SetItem( item
);
3545 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3547 headerWin
->m_dirty
= TRUE
;
3551 // invalidate it as it has to be recalculated
3555 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3557 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3558 _T("invalid column index") );
3560 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3561 _T("SetColumnWidth() can only be called in report mode.") );
3564 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3566 headerWin
->m_dirty
= TRUE
;
3568 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3569 wxCHECK_RET( node
, _T("no column?") );
3571 wxListHeaderData
*column
= node
->GetData();
3573 size_t count
= GetItemCount();
3575 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3577 width
= GetTextLength(column
->GetText());
3579 else if ( width
== wxLIST_AUTOSIZE
)
3583 // TODO: determine the max width somehow...
3584 width
= WIDTH_COL_DEFAULT
;
3588 wxClientDC
dc(this);
3589 dc
.SetFont( GetFont() );
3591 int max
= AUTOSIZE_COL_MARGIN
;
3593 for ( size_t i
= 0; i
< count
; i
++ )
3595 wxListLineData
*line
= GetLine(i
);
3596 wxListItemDataList::Node
*n
= line
->m_items
.Item( col
);
3598 wxCHECK_RET( n
, _T("no subitem?") );
3600 wxListItemData
*item
= n
->GetData();
3603 if (item
->HasImage())
3606 GetImageSize( item
->GetImage(), ix
, iy
);
3610 if (item
->HasText())
3613 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3621 width
= max
+ AUTOSIZE_COL_MARGIN
;
3625 column
->SetWidth( width
);
3627 // invalidate it as it has to be recalculated
3631 int wxListMainWindow::GetHeaderWidth() const
3633 if ( !m_headerWidth
)
3635 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3637 size_t count
= GetColumnCount();
3638 for ( size_t col
= 0; col
< count
; col
++ )
3640 self
->m_headerWidth
+= GetColumnWidth(col
);
3644 return m_headerWidth
;
3647 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3649 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3650 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3652 wxListHeaderData
*column
= node
->GetData();
3653 column
->GetItem( item
);
3656 int wxListMainWindow::GetColumnWidth( int col
) const
3658 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3659 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3661 wxListHeaderData
*column
= node
->GetData();
3662 return column
->GetWidth();
3665 // ----------------------------------------------------------------------------
3667 // ----------------------------------------------------------------------------
3669 void wxListMainWindow::SetItem( wxListItem
&item
)
3671 long id
= item
.m_itemId
;
3672 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3673 _T("invalid item index in SetItem") );
3677 wxListLineData
*line
= GetLine((size_t)id
);
3678 line
->SetItem( item
.m_col
, item
);
3681 if ( InReportView() )
3683 // just refresh the line to show the new value of the text/image
3684 RefreshLine((size_t)id
);
3688 // refresh everything (resulting in horrible flicker - FIXME!)
3693 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3695 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3696 _T("invalid list ctrl item index in SetItem") );
3698 size_t oldCurrent
= m_current
;
3699 size_t item
= (size_t)litem
; // safe because of the check above
3701 // do we need to change the focus?
3702 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3704 if ( state
& wxLIST_STATE_FOCUSED
)
3706 // don't do anything if this item is already focused
3707 if ( item
!= m_current
)
3709 ChangeCurrent(item
);
3711 if ( oldCurrent
!= (size_t)-1 )
3713 if ( IsSingleSel() )
3715 HighlightLine(oldCurrent
, FALSE
);
3718 RefreshLine(oldCurrent
);
3721 RefreshLine( m_current
);
3726 // don't do anything if this item is not focused
3727 if ( item
== m_current
)
3731 RefreshLine( oldCurrent
);
3736 // do we need to change the selection state?
3737 if ( stateMask
& wxLIST_STATE_SELECTED
)
3739 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3741 if ( IsSingleSel() )
3745 // selecting the item also makes it the focused one in the
3747 if ( m_current
!= item
)
3749 ChangeCurrent(item
);
3751 if ( oldCurrent
!= (size_t)-1 )
3753 HighlightLine( oldCurrent
, FALSE
);
3754 RefreshLine( oldCurrent
);
3760 // only the current item may be selected anyhow
3761 if ( item
!= m_current
)
3766 if ( HighlightLine(item
, on
) )
3773 int wxListMainWindow::GetItemState( long item
, long stateMask
)
3775 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3776 _T("invalid list ctrl item index in GetItemState()") );
3778 int ret
= wxLIST_STATE_DONTCARE
;
3780 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3782 if ( (size_t)item
== m_current
)
3783 ret
|= wxLIST_STATE_FOCUSED
;
3786 if ( stateMask
& wxLIST_STATE_SELECTED
)
3788 if ( IsHighlighted(item
) )
3789 ret
|= wxLIST_STATE_SELECTED
;
3795 void wxListMainWindow::GetItem( wxListItem
&item
)
3797 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3798 _T("invalid item index in GetItem") );
3800 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3801 line
->GetItem( item
.m_col
, item
);
3804 // ----------------------------------------------------------------------------
3806 // ----------------------------------------------------------------------------
3808 size_t wxListMainWindow::GetItemCount() const
3810 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3813 void wxListMainWindow::SetItemCount(long count
)
3815 m_selStore
.SetItemCount(count
);
3816 m_countVirt
= count
;
3818 ResetVisibleLinesRange();
3820 // scrollbars must be reset
3824 int wxListMainWindow::GetSelectedItemCount()
3826 // deal with the quick case first
3827 if ( IsSingleSel() )
3829 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3832 // virtual controls remmebers all its selections itself
3834 return m_selStore
.GetSelectedCount();
3836 // TODO: we probably should maintain the number of items selected even for
3837 // non virtual controls as enumerating all lines is really slow...
3838 size_t countSel
= 0;
3839 size_t count
= GetItemCount();
3840 for ( size_t line
= 0; line
< count
; line
++ )
3842 if ( GetLine(line
)->IsHighlighted() )
3849 // ----------------------------------------------------------------------------
3850 // item position/size
3851 // ----------------------------------------------------------------------------
3853 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
)
3855 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3856 _T("invalid index in GetItemRect") );
3858 rect
= GetLineRect((size_t)index
);
3860 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3863 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
)
3866 GetItemRect(item
, rect
);
3874 // ----------------------------------------------------------------------------
3875 // geometry calculation
3876 // ----------------------------------------------------------------------------
3878 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3880 wxClientDC
dc( this );
3881 dc
.SetFont( GetFont() );
3884 if ( HasFlag(wxLC_ICON
) )
3885 iconSpacing
= m_normal_spacing
;
3886 else if ( HasFlag(wxLC_SMALL_ICON
) )
3887 iconSpacing
= m_small_spacing
;
3893 GetClientSize( &clientWidth
, &clientHeight
);
3895 if ( HasFlag(wxLC_REPORT
) )
3897 // all lines have the same height
3898 int lineHeight
= GetLineHeight();
3900 // scroll one line per step
3901 m_yScroll
= lineHeight
;
3903 size_t lineCount
= GetItemCount();
3904 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3906 m_linesPerPage
= clientHeight
/ lineHeight
;
3908 ResetVisibleLinesRange();
3910 SetScrollbars( m_xScroll
, m_yScroll
,
3911 (GetHeaderWidth() + m_xScroll
- 1)/m_xScroll
,
3912 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3913 GetScrollPos(wxHORIZONTAL
),
3914 GetScrollPos(wxVERTICAL
),
3919 // at first we try without any scrollbar. if the items don't
3920 // fit into the window, we recalculate after subtracting an
3921 // approximated 15 pt for the horizontal scrollbar
3923 clientHeight
-= 4; // sunken frame
3925 int entireWidth
= 0;
3927 for (int tries
= 0; tries
< 2; tries
++)
3934 int currentlyVisibleLines
= 0;
3936 size_t count
= GetItemCount();
3937 for (size_t i
= 0; i
< count
; i
++)
3939 currentlyVisibleLines
++;
3940 wxListLineData
*line
= GetLine(i
);
3941 line
->CalculateSize( &dc
, iconSpacing
);
3942 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
);
3944 wxSize sizeLine
= GetLineSize(i
);
3946 if ( maxWidth
< sizeLine
.x
)
3947 maxWidth
= sizeLine
.x
;
3950 if (currentlyVisibleLines
> m_linesPerPage
)
3951 m_linesPerPage
= currentlyVisibleLines
;
3953 // assume that the size of the next one is the same... (FIXME)
3954 if ( y
+ sizeLine
.y
- 6 >= clientHeight
)
3956 currentlyVisibleLines
= 0;
3959 entireWidth
+= maxWidth
+6;
3962 if ( i
== count
- 1 )
3963 entireWidth
+= maxWidth
;
3964 if ((tries
== 0) && (entireWidth
> clientWidth
))
3966 clientHeight
-= 15; // scrollbar height
3968 currentlyVisibleLines
= 0;
3971 if ( i
== count
- 1 )
3972 tries
= 1; // everything fits, no second try required
3976 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
3977 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
3982 // FIXME: why should we call it from here?
3989 void wxListMainWindow::RefreshAll()
3994 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3995 if ( headerWin
&& headerWin
->m_dirty
)
3997 headerWin
->m_dirty
= FALSE
;
3998 headerWin
->Refresh();
4002 void wxListMainWindow::UpdateCurrent()
4004 if ( !HasCurrent() && !IsEmpty() )
4010 long wxListMainWindow::GetNextItem( long item
,
4011 int WXUNUSED(geometry
),
4015 max
= GetItemCount();
4016 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4017 _T("invalid listctrl index in GetNextItem()") );
4019 // notice that we start with the next item (or the first one if item == -1)
4020 // and this is intentional to allow writing a simple loop to iterate over
4021 // all selected items
4025 // this is not an error because the index was ok initially, just no
4036 size_t count
= GetItemCount();
4037 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4039 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4042 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4049 // ----------------------------------------------------------------------------
4051 // ----------------------------------------------------------------------------
4053 void wxListMainWindow::DeleteItem( long lindex
)
4055 size_t count
= GetItemCount();
4057 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4058 _T("invalid item index in DeleteItem") );
4060 size_t index
= (size_t)lindex
;
4062 // we don't need to adjust the index for the previous items
4063 if ( HasCurrent() && m_current
>= index
)
4065 // if the current item is being deleted, we want the next one to
4066 // become selected - unless there is no next one - so don't adjust
4067 // m_current in this case
4068 if ( m_current
!= index
|| m_current
== count
- 1 )
4074 if ( InReportView() )
4076 ResetVisibleLinesRange();
4083 m_selStore
.OnItemDelete(index
);
4087 m_lines
.RemoveAt( index
);
4090 // we need to refresh the (vert) scrollbar as the number of items changed
4093 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4095 RefreshAfter(index
);
4098 void wxListMainWindow::DeleteColumn( int col
)
4100 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4102 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4105 m_columns
.DeleteNode( node
);
4108 void wxListMainWindow::DoDeleteAllItems()
4112 // nothing to do - in particular, don't send the event
4118 // to make the deletion of all items faster, we don't send the
4119 // notifications for each item deletion in this case but only one event
4120 // for all of them: this is compatible with wxMSW and documented in
4121 // DeleteAllItems() description
4123 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4124 event
.SetEventObject( GetParent() );
4125 GetParent()->GetEventHandler()->ProcessEvent( event
);
4134 if ( InReportView() )
4136 ResetVisibleLinesRange();
4142 void wxListMainWindow::DeleteAllItems()
4146 RecalculatePositions();
4149 void wxListMainWindow::DeleteEverything()
4156 // ----------------------------------------------------------------------------
4157 // scanning for an item
4158 // ----------------------------------------------------------------------------
4160 void wxListMainWindow::EnsureVisible( long index
)
4162 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4163 _T("invalid index in EnsureVisible") );
4165 // We have to call this here because the label in question might just have
4166 // been added and its position is not known yet
4169 RecalculatePositions(TRUE
/* no refresh */);
4172 MoveToItem((size_t)index
);
4175 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4182 size_t count
= GetItemCount();
4183 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4185 wxListLineData
*line
= GetLine(i
);
4186 if ( line
->GetText(0) == tmp
)
4193 long wxListMainWindow::FindItem(long start
, long data
)
4199 size_t count
= GetItemCount();
4200 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4202 wxListLineData
*line
= GetLine(i
);
4204 line
->GetItem( 0, item
);
4205 if (item
.m_data
== data
)
4212 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4214 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4216 size_t count
= GetItemCount();
4218 if ( HasFlag(wxLC_REPORT
) )
4220 size_t current
= y
/ GetLineHeight();
4221 if ( current
< count
)
4223 flags
= HitTestLine(current
, x
, y
);
4230 // TODO: optimize it too! this is less simple than for report view but
4231 // enumerating all items is still not a way to do it!!
4232 for ( size_t current
= 0; current
< count
; current
++ )
4234 flags
= HitTestLine(current
, x
, y
);
4243 // ----------------------------------------------------------------------------
4245 // ----------------------------------------------------------------------------
4247 void wxListMainWindow::InsertItem( wxListItem
&item
)
4249 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4251 size_t count
= GetItemCount();
4252 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4253 _T("invalid item index") );
4255 size_t id
= item
.m_itemId
;
4260 if ( HasFlag(wxLC_REPORT
) )
4262 else if ( HasFlag(wxLC_LIST
) )
4264 else if ( HasFlag(wxLC_ICON
) )
4266 else if ( HasFlag(wxLC_SMALL_ICON
) )
4267 mode
= wxLC_ICON
; // no typo
4270 wxFAIL_MSG( _T("unknown mode") );
4273 wxListLineData
*line
= new wxListLineData(this);
4275 line
->SetItem( 0, item
);
4277 m_lines
.Insert( line
, id
);
4280 RefreshLines(id
, GetItemCount() - 1);
4283 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4286 if ( HasFlag(wxLC_REPORT
) )
4288 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4289 item
.m_width
= GetTextLength( item
.m_text
);
4290 wxListHeaderData
*column
= new wxListHeaderData( item
);
4291 if ((col
>= 0) && (col
< (int)m_columns
.GetCount()))
4293 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4294 m_columns
.Insert( node
, column
);
4298 m_columns
.Append( column
);
4303 // ----------------------------------------------------------------------------
4305 // ----------------------------------------------------------------------------
4307 wxListCtrlCompare list_ctrl_compare_func_2
;
4308 long list_ctrl_compare_data
;
4310 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4312 wxListLineData
*line1
= *arg1
;
4313 wxListLineData
*line2
= *arg2
;
4315 line1
->GetItem( 0, item
);
4316 long data1
= item
.m_data
;
4317 line2
->GetItem( 0, item
);
4318 long data2
= item
.m_data
;
4319 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4322 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4324 list_ctrl_compare_func_2
= fn
;
4325 list_ctrl_compare_data
= data
;
4326 m_lines
.Sort( list_ctrl_compare_func_1
);
4330 // ----------------------------------------------------------------------------
4332 // ----------------------------------------------------------------------------
4334 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4336 // update our idea of which lines are shown when we redraw the window the
4338 ResetVisibleLinesRange();
4341 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4342 wxScrolledWindow::OnScroll(event
);
4344 HandleOnScroll( event
);
4347 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4349 wxListCtrl
* lc
= GetListCtrl();
4350 wxCHECK_RET( lc
, _T("no listctrl window?") );
4352 lc
->m_headerWin
->Refresh() ;
4354 lc
->m_headerWin
->MacUpdateImmediately() ;
4359 int wxListMainWindow::GetCountPerPage() const
4361 if ( !m_linesPerPage
)
4363 wxConstCast(this, wxListMainWindow
)->
4364 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4367 return m_linesPerPage
;
4370 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4372 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4374 if ( m_lineFrom
== (size_t)-1 )
4376 size_t count
= GetItemCount();
4379 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4381 // this may happen if SetScrollbars() hadn't been called yet
4382 if ( m_lineFrom
>= count
)
4383 m_lineFrom
= count
- 1;
4385 // we redraw one extra line but this is needed to make the redrawing
4386 // logic work when there is a fractional number of lines on screen
4387 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4388 if ( m_lineTo
>= count
)
4389 m_lineTo
= count
- 1;
4391 else // empty control
4394 m_lineTo
= (size_t)-1;
4398 wxASSERT_MSG( IsEmpty() ||
4399 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4400 _T("GetVisibleLinesRange() returns incorrect result") );
4408 // -------------------------------------------------------------------------------------
4410 // -------------------------------------------------------------------------------------
4412 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
4414 wxListItem::wxListItem()
4421 void wxListItem::Clear()
4430 m_format
= wxLIST_FORMAT_CENTRE
;
4437 void wxListItem::ClearAttributes()
4446 // -------------------------------------------------------------------------------------
4448 // -------------------------------------------------------------------------------------
4450 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
4452 wxListEvent::wxListEvent( wxEventType commandType
, int id
)
4453 : wxNotifyEvent( commandType
, id
)
4459 m_cancelled
= FALSE
;
4464 void wxListEvent::CopyObject(wxObject
& object_dest
) const
4466 wxListEvent
*obj
= (wxListEvent
*)&object_dest
;
4468 wxNotifyEvent::CopyObject(object_dest
);
4470 obj
->m_code
= m_code
;
4471 obj
->m_itemIndex
= m_itemIndex
;
4472 obj
->m_oldItemIndex
= m_oldItemIndex
;
4474 obj
->m_cancelled
= m_cancelled
;
4475 obj
->m_pointDrag
= m_pointDrag
;
4476 obj
->m_item
.m_mask
= m_item
.m_mask
;
4477 obj
->m_item
.m_itemId
= m_item
.m_itemId
;
4478 obj
->m_item
.m_col
= m_item
.m_col
;
4479 obj
->m_item
.m_state
= m_item
.m_state
;
4480 obj
->m_item
.m_stateMask
= m_item
.m_stateMask
;
4481 obj
->m_item
.m_text
= m_item
.m_text
;
4482 obj
->m_item
.m_image
= m_item
.m_image
;
4483 obj
->m_item
.m_data
= m_item
.m_data
;
4484 obj
->m_item
.m_format
= m_item
.m_format
;
4485 obj
->m_item
.m_width
= m_item
.m_width
;
4487 if ( m_item
.HasAttributes() )
4489 obj
->m_item
.SetTextColour(m_item
.GetTextColour());
4493 // -------------------------------------------------------------------------------------
4495 // -------------------------------------------------------------------------------------
4497 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
4498 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
4500 BEGIN_EVENT_TABLE(wxListCtrl
,wxControl
)
4501 EVT_SIZE(wxListCtrl::OnSize
)
4502 EVT_IDLE(wxListCtrl::OnIdle
)
4505 wxListCtrl::wxListCtrl()
4507 m_imageListNormal
= (wxImageList
*) NULL
;
4508 m_imageListSmall
= (wxImageList
*) NULL
;
4509 m_imageListState
= (wxImageList
*) NULL
;
4511 m_ownsImageListNormal
=
4512 m_ownsImageListSmall
=
4513 m_ownsImageListState
= FALSE
;
4515 m_mainWin
= (wxListMainWindow
*) NULL
;
4516 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4519 wxListCtrl::~wxListCtrl()
4521 if (m_ownsImageListNormal
)
4522 delete m_imageListNormal
;
4523 if (m_ownsImageListSmall
)
4524 delete m_imageListSmall
;
4525 if (m_ownsImageListState
)
4526 delete m_imageListState
;
4529 void wxListCtrl::CreateHeaderWindow()
4531 m_headerWin
= new wxListHeaderWindow
4533 this, -1, m_mainWin
,
4535 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
4540 bool wxListCtrl::Create(wxWindow
*parent
,
4545 const wxValidator
&validator
,
4546 const wxString
&name
)
4550 m_imageListState
= (wxImageList
*) NULL
;
4551 m_ownsImageListNormal
=
4552 m_ownsImageListSmall
=
4553 m_ownsImageListState
= FALSE
;
4555 m_mainWin
= (wxListMainWindow
*) NULL
;
4556 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4558 if ( !(style
& wxLC_MASK_TYPE
) )
4560 style
= style
| wxLC_LIST
;
4563 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4566 // don't create the inner window with the border
4567 style
&= ~wxSUNKEN_BORDER
;
4569 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4571 if ( HasFlag(wxLC_REPORT
) )
4573 CreateHeaderWindow();
4575 if ( HasFlag(wxLC_NO_HEADER
) )
4577 // VZ: why do we create it at all then?
4578 m_headerWin
->Show( FALSE
);
4585 void wxListCtrl::SetSingleStyle( long style
, bool add
)
4587 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4588 _T("wxLC_VIRTUAL can't be [un]set") );
4590 long flag
= GetWindowStyle();
4594 if (style
& wxLC_MASK_TYPE
)
4595 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4596 if (style
& wxLC_MASK_ALIGN
)
4597 flag
&= ~wxLC_MASK_ALIGN
;
4598 if (style
& wxLC_MASK_SORT
)
4599 flag
&= ~wxLC_MASK_SORT
;
4611 SetWindowStyleFlag( flag
);
4614 void wxListCtrl::SetWindowStyleFlag( long flag
)
4618 m_mainWin
->DeleteEverything();
4620 // has the header visibility changed?
4621 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4622 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4624 if ( hasHeader
!= willHaveHeader
)
4631 // don't delete, just hide, as we can reuse it later
4632 m_headerWin
->Show(FALSE
);
4634 //else: nothing to do
4636 else // must show header
4640 CreateHeaderWindow();
4642 else // already have it, just show
4644 m_headerWin
->Show( TRUE
);
4648 ResizeReportView(willHaveHeader
);
4652 wxWindow::SetWindowStyleFlag( flag
);
4655 bool wxListCtrl::GetColumn(int col
, wxListItem
&item
) const
4657 m_mainWin
->GetColumn( col
, item
);
4661 bool wxListCtrl::SetColumn( int col
, wxListItem
& item
)
4663 m_mainWin
->SetColumn( col
, item
);
4667 int wxListCtrl::GetColumnWidth( int col
) const
4669 return m_mainWin
->GetColumnWidth( col
);
4672 bool wxListCtrl::SetColumnWidth( int col
, int width
)
4674 m_mainWin
->SetColumnWidth( col
, width
);
4678 int wxListCtrl::GetCountPerPage() const
4680 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4683 bool wxListCtrl::GetItem( wxListItem
&info
) const
4685 m_mainWin
->GetItem( info
);
4689 bool wxListCtrl::SetItem( wxListItem
&info
)
4691 m_mainWin
->SetItem( info
);
4695 long wxListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4698 info
.m_text
= label
;
4699 info
.m_mask
= wxLIST_MASK_TEXT
;
4700 info
.m_itemId
= index
;
4704 info
.m_image
= imageId
;
4705 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4707 m_mainWin
->SetItem(info
);
4711 int wxListCtrl::GetItemState( long item
, long stateMask
) const
4713 return m_mainWin
->GetItemState( item
, stateMask
);
4716 bool wxListCtrl::SetItemState( long item
, long state
, long stateMask
)
4718 m_mainWin
->SetItemState( item
, state
, stateMask
);
4722 bool wxListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4725 info
.m_image
= image
;
4726 info
.m_mask
= wxLIST_MASK_IMAGE
;
4727 info
.m_itemId
= item
;
4728 m_mainWin
->SetItem( info
);
4732 wxString
wxListCtrl::GetItemText( long item
) const
4735 info
.m_itemId
= item
;
4736 m_mainWin
->GetItem( info
);
4740 void wxListCtrl::SetItemText( long item
, const wxString
&str
)
4743 info
.m_mask
= wxLIST_MASK_TEXT
;
4744 info
.m_itemId
= item
;
4746 m_mainWin
->SetItem( info
);
4749 long wxListCtrl::GetItemData( long item
) const
4752 info
.m_itemId
= item
;
4753 m_mainWin
->GetItem( info
);
4757 bool wxListCtrl::SetItemData( long item
, long data
)
4760 info
.m_mask
= wxLIST_MASK_DATA
;
4761 info
.m_itemId
= item
;
4763 m_mainWin
->SetItem( info
);
4767 bool wxListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4769 m_mainWin
->GetItemRect( item
, rect
);
4773 bool wxListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4775 m_mainWin
->GetItemPosition( item
, pos
);
4779 bool wxListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4784 int wxListCtrl::GetItemCount() const
4786 return m_mainWin
->GetItemCount();
4789 int wxListCtrl::GetColumnCount() const
4791 return m_mainWin
->GetColumnCount();
4794 void wxListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4796 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4799 int wxListCtrl::GetItemSpacing( bool isSmall
) const
4801 return m_mainWin
->GetItemSpacing( isSmall
);
4804 int wxListCtrl::GetSelectedItemCount() const
4806 return m_mainWin
->GetSelectedItemCount();
4809 wxColour
wxListCtrl::GetTextColour() const
4811 return GetForegroundColour();
4814 void wxListCtrl::SetTextColour(const wxColour
& col
)
4816 SetForegroundColour(col
);
4819 long wxListCtrl::GetTopItem() const
4824 long wxListCtrl::GetNextItem( long item
, int geom
, int state
) const
4826 return m_mainWin
->GetNextItem( item
, geom
, state
);
4829 wxImageList
*wxListCtrl::GetImageList(int which
) const
4831 if (which
== wxIMAGE_LIST_NORMAL
)
4833 return m_imageListNormal
;
4835 else if (which
== wxIMAGE_LIST_SMALL
)
4837 return m_imageListSmall
;
4839 else if (which
== wxIMAGE_LIST_STATE
)
4841 return m_imageListState
;
4843 return (wxImageList
*) NULL
;
4846 void wxListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4848 if ( which
== wxIMAGE_LIST_NORMAL
)
4850 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4851 m_imageListNormal
= imageList
;
4852 m_ownsImageListNormal
= FALSE
;
4854 else if ( which
== wxIMAGE_LIST_SMALL
)
4856 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4857 m_imageListSmall
= imageList
;
4858 m_ownsImageListSmall
= FALSE
;
4860 else if ( which
== wxIMAGE_LIST_STATE
)
4862 if (m_ownsImageListState
) delete m_imageListState
;
4863 m_imageListState
= imageList
;
4864 m_ownsImageListState
= FALSE
;
4867 m_mainWin
->SetImageList( imageList
, which
);
4870 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4872 SetImageList(imageList
, which
);
4873 if ( which
== wxIMAGE_LIST_NORMAL
)
4874 m_ownsImageListNormal
= TRUE
;
4875 else if ( which
== wxIMAGE_LIST_SMALL
)
4876 m_ownsImageListSmall
= TRUE
;
4877 else if ( which
== wxIMAGE_LIST_STATE
)
4878 m_ownsImageListState
= TRUE
;
4881 bool wxListCtrl::Arrange( int WXUNUSED(flag
) )
4886 bool wxListCtrl::DeleteItem( long item
)
4888 m_mainWin
->DeleteItem( item
);
4892 bool wxListCtrl::DeleteAllItems()
4894 m_mainWin
->DeleteAllItems();
4898 bool wxListCtrl::DeleteAllColumns()
4900 size_t count
= m_mainWin
->m_columns
.GetCount();
4901 for ( size_t n
= 0; n
< count
; n
++ )
4907 void wxListCtrl::ClearAll()
4909 m_mainWin
->DeleteEverything();
4912 bool wxListCtrl::DeleteColumn( int col
)
4914 m_mainWin
->DeleteColumn( col
);
4918 void wxListCtrl::Edit( long item
)
4920 m_mainWin
->EditLabel( item
);
4923 bool wxListCtrl::EnsureVisible( long item
)
4925 m_mainWin
->EnsureVisible( item
);
4929 long wxListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4931 return m_mainWin
->FindItem( start
, str
, partial
);
4934 long wxListCtrl::FindItem( long start
, long data
)
4936 return m_mainWin
->FindItem( start
, data
);
4939 long wxListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4940 int WXUNUSED(direction
))
4945 long wxListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4947 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4950 long wxListCtrl::InsertItem( wxListItem
& info
)
4952 m_mainWin
->InsertItem( info
);
4953 return info
.m_itemId
;
4956 long wxListCtrl::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 wxListCtrl::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 wxListCtrl::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 wxListCtrl::InsertColumn( long col
, wxListItem
&item
)
4986 wxASSERT( m_headerWin
);
4987 m_mainWin
->InsertColumn( col
, item
);
4988 m_headerWin
->Refresh();
4993 long wxListCtrl::InsertColumn( long col
, const wxString
&heading
,
4994 int format
, int width
)
4997 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4998 item
.m_text
= heading
;
5001 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5002 item
.m_width
= width
;
5004 item
.m_format
= format
;
5006 return InsertColumn( col
, item
);
5009 bool wxListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5015 // fn is a function which takes 3 long arguments: item1, item2, data.
5016 // item1 is the long data associated with a first item (NOT the index).
5017 // item2 is the long data associated with a second item (NOT the index).
5018 // data is the same value as passed to SortItems.
5019 // The return value is a negative number if the first item should precede the second
5020 // item, a positive number of the second item should precede the first,
5021 // or zero if the two items are equivalent.
5022 // data is arbitrary data to be passed to the sort function.
5024 bool wxListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5026 m_mainWin
->SortItems( fn
, data
);
5030 // ----------------------------------------------------------------------------
5032 // ----------------------------------------------------------------------------
5034 void wxListCtrl::OnSize(wxSizeEvent
& event
)
5039 ResizeReportView(m_mainWin
->HasHeader());
5041 m_mainWin
->RecalculatePositions();
5044 void wxListCtrl::ResizeReportView(bool showHeader
)
5047 GetClientSize( &cw
, &ch
);
5051 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
5052 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
5054 else // no header window
5056 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5060 void wxListCtrl::OnIdle( wxIdleEvent
& event
)
5064 // do it only if needed
5065 if ( !m_mainWin
->m_dirty
)
5068 m_mainWin
->RecalculatePositions();
5071 // ----------------------------------------------------------------------------
5073 // ----------------------------------------------------------------------------
5075 bool wxListCtrl::SetBackgroundColour( const wxColour
&colour
)
5079 m_mainWin
->SetBackgroundColour( colour
);
5080 m_mainWin
->m_dirty
= TRUE
;
5086 bool wxListCtrl::SetForegroundColour( const wxColour
&colour
)
5088 if ( !wxWindow::SetForegroundColour( colour
) )
5093 m_mainWin
->SetForegroundColour( colour
);
5094 m_mainWin
->m_dirty
= TRUE
;
5099 m_headerWin
->SetForegroundColour( colour
);
5105 bool wxListCtrl::SetFont( const wxFont
&font
)
5107 if ( !wxWindow::SetFont( font
) )
5112 m_mainWin
->SetFont( font
);
5113 m_mainWin
->m_dirty
= TRUE
;
5118 m_headerWin
->SetFont( font
);
5124 // ----------------------------------------------------------------------------
5125 // methods forwarded to m_mainWin
5126 // ----------------------------------------------------------------------------
5128 #if wxUSE_DRAG_AND_DROP
5130 void wxListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5132 m_mainWin
->SetDropTarget( dropTarget
);
5135 wxDropTarget
*wxListCtrl::GetDropTarget() const
5137 return m_mainWin
->GetDropTarget();
5140 #endif // wxUSE_DRAG_AND_DROP
5142 bool wxListCtrl::SetCursor( const wxCursor
&cursor
)
5144 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5147 wxColour
wxListCtrl::GetBackgroundColour() const
5149 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5152 wxColour
wxListCtrl::GetForegroundColour() const
5154 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5157 bool wxListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5160 return m_mainWin
->PopupMenu( menu
, x
, y
);
5163 #endif // wxUSE_MENUS
5166 void wxListCtrl::SetFocus()
5168 /* The test in window.cpp fails as we are a composite
5169 window, so it checks against "this", but not m_mainWin. */
5170 if ( FindFocus() != this )
5171 m_mainWin
->SetFocus();
5174 // ----------------------------------------------------------------------------
5175 // virtual list control support
5176 // ----------------------------------------------------------------------------
5178 wxString
wxListCtrl::OnGetItemText(long item
, long col
) const
5180 // this is a pure virtual function, in fact - which is not really pure
5181 // because the controls which are not virtual don't need to implement it
5182 wxFAIL_MSG( _T("not supposed to be called") );
5184 return wxEmptyString
;
5187 int wxListCtrl::OnGetItemImage(long item
) const
5190 wxFAIL_MSG( _T("not supposed to be called") );
5195 wxListItemAttr
*wxListCtrl::OnGetItemAttr(long item
) const
5197 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5198 _T("invalid item index in OnGetItemAttr()") );
5200 // no attributes by default
5204 void wxListCtrl::SetItemCount(long count
)
5206 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5208 m_mainWin
->SetItemCount(count
);
5211 void wxListCtrl::RefreshItem(long item
)
5213 m_mainWin
->RefreshLine(item
);
5216 void wxListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5218 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5221 void wxListCtrl::Freeze()
5223 m_mainWin
->Freeze();
5226 void wxListCtrl::Thaw()
5231 #endif // wxUSE_LISTCTRL