1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 1. we need to implement searching/sorting for virtual controls somehow
15 ?2. when changing selection the lines are refreshed twice
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
27 #pragma implementation "listctrl.h"
28 #pragma implementation "listctrlbase.h"
31 // For compilers that support precompilation, includes "wx.h".
32 #include "wx/wxprec.h"
43 #include "wx/dynarray.h"
45 #include "wx/dcscreen.h"
47 #include "wx/textctrl.h"
50 #include "wx/listctrl.h"
52 #if defined(__WXGTK__)
54 #include "wx/gtk/win_gtk.h"
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
62 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
63 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
64 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
65 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
66 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
67 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
68 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
69 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
70 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
71 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
72 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
73 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
74 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
75 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
76 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
77 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
78 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
79 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
80 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
81 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // the height of the header window (FIXME: should depend on its font!)
89 static const int HEADER_HEIGHT
= 23;
91 // the scrollbar units
92 static const int SCROLL_UNIT_X
= 15;
93 static const int SCROLL_UNIT_Y
= 15;
95 // the spacing between the lines (in report mode)
96 static const int LINE_SPACING
= 0;
98 // extra margins around the text label
99 static const int EXTRA_WIDTH
= 3;
100 static const int EXTRA_HEIGHT
= 4;
102 // offset for the header window
103 static const int HEADER_OFFSET_X
= 1;
104 static const int HEADER_OFFSET_Y
= 1;
106 // when autosizing the columns, add some slack
107 static const int AUTOSIZE_COL_MARGIN
= 10;
109 // default and minimal widths for the header columns
110 static const int WIDTH_COL_DEFAULT
= 80;
111 static const int WIDTH_COL_MIN
= 10;
113 // the space between the image and the text in the report mode
114 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
116 // ============================================================================
118 // ============================================================================
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 int CMPFUNC_CONV
wxSizeTCmpFn(size_t n1
, size_t n2
) { return n1
- n2
; }
126 WX_DEFINE_SORTED_EXPORTED_ARRAY_LONG(size_t, wxIndexArray
);
128 // this class is used to store the selected items in the virtual list control
129 // (but it is not tied to list control and so can be used with other controls
130 // such as wxListBox in wxUniv)
132 // the idea is to make it really smart later (i.e. store the selections as an
133 // array of ranes + individual items) but, as I don't have time to do it now
134 // (this would require writing code to merge/break ranges and much more) keep
135 // it simple but define a clean interface to it which allows it to be made
137 class WXDLLEXPORT wxSelectionStore
140 wxSelectionStore() : m_itemsSel(wxSizeTCmpFn
) { Init(); }
142 // set the total number of items we handle
143 void SetItemCount(size_t count
) { m_count
= count
; }
145 // special case of SetItemCount(0)
146 void Clear() { m_itemsSel
.Clear(); m_count
= 0; m_defaultState
= false; }
148 // must be called when a new item is inserted/added
149 void OnItemAdd(size_t item
) { wxFAIL_MSG( _T("TODO") ); }
151 // must be called when an item is deleted
152 void OnItemDelete(size_t item
);
154 // select one item, use SelectRange() insted if possible!
156 // returns true if the items selection really changed
157 bool SelectItem(size_t item
, bool select
= TRUE
);
159 // select the range of items
161 // return true and fill the itemsChanged array with the indices of items
162 // which have changed state if "few" of them did, otherwise return false
163 // (meaning that too many items changed state to bother counting them
165 bool SelectRange(size_t itemFrom
, size_t itemTo
,
167 wxArrayInt
*itemsChanged
= NULL
);
169 // return true if the given item is selected
170 bool IsSelected(size_t item
) const;
172 // return the total number of selected items
173 size_t GetSelectedCount() const
175 return m_defaultState
? m_count
- m_itemsSel
.GetCount()
176 : m_itemsSel
.GetCount();
181 void Init() { m_defaultState
= FALSE
; }
183 // the total number of items we handle
186 // the default state: normally, FALSE (i.e. off) but maybe set to TRUE if
187 // there are more selected items than non selected ones - this allows to
188 // handle selection of all items efficiently
191 // the array of items whose selection state is different from default
192 wxIndexArray m_itemsSel
;
194 DECLARE_NO_COPY_CLASS(wxSelectionStore
)
197 //-----------------------------------------------------------------------------
198 // wxListItemData (internal)
199 //-----------------------------------------------------------------------------
201 class WXDLLEXPORT wxListItemData
204 wxListItemData(wxListMainWindow
*owner
);
207 void SetItem( const wxListItem
&info
);
208 void SetImage( int image
) { m_image
= image
; }
209 void SetData( long data
) { m_data
= data
; }
210 void SetPosition( int x
, int y
);
211 void SetSize( int width
, int height
);
213 bool HasText() const { return !m_text
.empty(); }
214 const wxString
& GetText() const { return m_text
; }
215 void SetText(const wxString
& text
) { m_text
= text
; }
217 // we can't use empty string for measuring the string width/height, so
218 // always return something
219 wxString
GetTextForMeasuring() const
221 wxString s
= GetText();
228 bool IsHit( int x
, int y
) const;
232 int GetWidth() const;
233 int GetHeight() const;
235 int GetImage() const { return m_image
; }
236 bool HasImage() const { return GetImage() != -1; }
238 void GetItem( wxListItem
&info
) const;
240 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
241 wxListItemAttr
*GetAttr() const { return m_attr
; }
244 // the item image or -1
247 // user data associated with the item
250 // the item coordinates are not used in report mode, instead this pointer
251 // is NULL and the owner window is used to retrieve the item position and
255 // the list ctrl we are in
256 wxListMainWindow
*m_owner
;
258 // custom attributes or NULL
259 wxListItemAttr
*m_attr
;
262 // common part of all ctors
268 //-----------------------------------------------------------------------------
269 // wxListHeaderData (internal)
270 //-----------------------------------------------------------------------------
272 class WXDLLEXPORT wxListHeaderData
: public wxObject
276 wxListHeaderData( const wxListItem
&info
);
277 void SetItem( const wxListItem
&item
);
278 void SetPosition( int x
, int y
);
279 void SetWidth( int w
);
280 void SetFormat( int format
);
281 void SetHeight( int h
);
282 bool HasImage() const;
284 bool HasText() const { return !m_text
.empty(); }
285 const wxString
& GetText() const { return m_text
; }
286 void SetText(const wxString
& text
) { m_text
= text
; }
288 void GetItem( wxListItem
&item
);
290 bool IsHit( int x
, int y
) const;
291 int GetImage() const;
292 int GetWidth() const;
293 int GetFormat() const;
309 //-----------------------------------------------------------------------------
310 // wxListLineData (internal)
311 //-----------------------------------------------------------------------------
313 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
314 #include "wx/listimpl.cpp"
315 WX_DEFINE_LIST(wxListItemDataList
);
317 class WXDLLEXPORT wxListLineData
320 // the list of subitems: only may have more than one item in report mode
321 wxListItemDataList m_items
;
323 // this is not used in report view
335 // the part to be highlighted
336 wxRect m_rectHighlight
;
339 // is this item selected? [NB: not used in virtual mode]
342 // back pointer to the list ctrl
343 wxListMainWindow
*m_owner
;
346 wxListLineData(wxListMainWindow
*owner
);
348 ~wxListLineData() { delete m_gi
; }
350 // are we in report mode?
351 inline bool InReportView() const;
353 // are we in virtual report mode?
354 inline bool IsVirtual() const;
356 // these 2 methods shouldn't be called for report view controls, in that
357 // case we determine our position/size ourselves
359 // calculate the size of the line
360 void CalculateSize( wxDC
*dc
, int spacing
);
362 // remember the position this line appears at
363 void SetPosition( int x
, int y
, int window_width
, int spacing
);
367 void SetImage( int image
) { SetImage(0, image
); }
368 int GetImage() const { return GetImage(0); }
369 bool HasImage() const { return GetImage() != -1; }
370 bool HasText() const { return !GetText(0).empty(); }
372 void SetItem( int index
, const wxListItem
&info
);
373 void GetItem( int index
, wxListItem
&info
);
375 wxString
GetText(int index
) const;
376 void SetText( int index
, const wxString s
);
378 wxListItemAttr
*GetAttr() const;
379 void SetAttr(wxListItemAttr
*attr
);
381 // return true if the highlighting really changed
382 bool Highlight( bool on
);
384 void ReverseHighlight();
386 bool IsHighlighted() const
388 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
390 return m_highlighted
;
393 // draw the line on the given DC in icon/list mode
394 void Draw( wxDC
*dc
);
396 // the same in report mode
397 void DrawInReportMode( wxDC
*dc
,
399 const wxRect
& rectHL
,
403 // set the line to contain num items (only can be > 1 in report mode)
404 void InitItems( int num
);
406 // get the mode (i.e. style) of the list control
407 inline int GetMode() const;
409 // prepare the DC for drawing with these item's attributes, return true if
410 // we need to draw the items background to highlight it, false otherwise
411 bool SetAttributes(wxDC
*dc
,
412 const wxListItemAttr
*attr
,
415 // these are only used by GetImage/SetImage above, we don't support images
416 // with subitems at the public API level yet
417 void SetImage( int index
, int image
);
418 int GetImage( int index
) const;
421 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
422 #include "wx/arrimpl.cpp"
423 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
425 //-----------------------------------------------------------------------------
426 // wxListHeaderWindow (internal)
427 //-----------------------------------------------------------------------------
429 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
432 wxListMainWindow
*m_owner
;
433 wxCursor
*m_currentCursor
;
434 wxCursor
*m_resizeCursor
;
437 // column being resized or -1
440 // divider line position in logical (unscrolled) coords
443 // minimal position beyond which the divider line can't be dragged in
448 wxListHeaderWindow();
450 wxListHeaderWindow( wxWindow
*win
,
452 wxListMainWindow
*owner
,
453 const wxPoint
&pos
= wxDefaultPosition
,
454 const wxSize
&size
= wxDefaultSize
,
456 const wxString
&name
= "wxlistctrlcolumntitles" );
458 virtual ~wxListHeaderWindow();
460 void DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
);
462 void AdjustDC(wxDC
& dc
);
464 void OnPaint( wxPaintEvent
&event
);
465 void OnMouse( wxMouseEvent
&event
);
466 void OnSetFocus( wxFocusEvent
&event
);
472 // common part of all ctors
475 void SendListEvent(wxEventType type
, wxPoint pos
);
477 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
478 DECLARE_EVENT_TABLE()
481 //-----------------------------------------------------------------------------
482 // wxListRenameTimer (internal)
483 //-----------------------------------------------------------------------------
485 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
488 wxListMainWindow
*m_owner
;
491 wxListRenameTimer( wxListMainWindow
*owner
);
495 //-----------------------------------------------------------------------------
496 // wxListTextCtrl (internal)
497 //-----------------------------------------------------------------------------
499 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
502 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
505 void OnChar( wxKeyEvent
&event
);
506 void OnKeyUp( wxKeyEvent
&event
);
507 void OnKillFocus( wxFocusEvent
&event
);
509 bool AcceptChanges();
513 wxListMainWindow
*m_owner
;
514 wxString m_startValue
;
518 DECLARE_EVENT_TABLE()
521 //-----------------------------------------------------------------------------
522 // wxListMainWindow (internal)
523 //-----------------------------------------------------------------------------
525 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
526 #include "wx/listimpl.cpp"
527 WX_DEFINE_LIST(wxListHeaderDataList
);
529 class WXDLLEXPORT wxListMainWindow
: public wxScrolledWindow
533 wxListMainWindow( wxWindow
*parent
,
535 const wxPoint
& pos
= wxDefaultPosition
,
536 const wxSize
& size
= wxDefaultSize
,
538 const wxString
&name
= _T("listctrlmainwindow") );
540 virtual ~wxListMainWindow();
542 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
544 // return true if this is a virtual list control
545 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
547 // return true if the control is in report mode
548 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
550 // return true if we are in single selection mode, false if multi sel
551 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
553 // do we have a header window?
554 bool HasHeader() const
555 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
557 void HighlightAll( bool on
);
559 // all these functions only do something if the line is currently visible
561 // change the line "selected" state, return TRUE if it really changed
562 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
564 // as HighlightLine() but do it for the range of lines: this is incredibly
565 // more efficient for virtual list controls!
567 // NB: unlike HighlightLine() this one does refresh the lines on screen
568 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
570 // toggle the line state and refresh it
571 void ReverseHighlight( size_t line
)
572 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
574 // return true if the line is highlighted
575 bool IsHighlighted(size_t line
) const;
577 // refresh one or several lines at once
578 void RefreshLine( size_t line
);
579 void RefreshLines( size_t lineFrom
, size_t lineTo
);
581 // refresh all selected items
582 void RefreshSelected();
584 // refresh all lines below the given one: the difference with
585 // RefreshLines() is that the index here might not be a valid one (happens
586 // when the last line is deleted)
587 void RefreshAfter( size_t lineFrom
);
589 // the methods which are forwarded to wxListLineData itself in list/icon
590 // modes but are here because the lines don't store their positions in the
593 // get the bound rect for the entire line
594 wxRect
GetLineRect(size_t line
) const;
596 // get the bound rect of the label
597 wxRect
GetLineLabelRect(size_t line
) const;
599 // get the bound rect of the items icon (only may be called if we do have
601 wxRect
GetLineIconRect(size_t line
) const;
603 // get the rect to be highlighted when the item has focus
604 wxRect
GetLineHighlightRect(size_t line
) const;
606 // get the size of the total line rect
607 wxSize
GetLineSize(size_t line
) const
608 { return GetLineRect(line
).GetSize(); }
610 // return the hit code for the corresponding position (in this line)
611 long HitTestLine(size_t line
, int x
, int y
) const;
613 // bring the selected item into view, scrolling to it if necessary
614 void MoveToItem(size_t item
);
616 // bring the current item into view
617 void MoveToFocus() { MoveToItem(m_current
); }
619 // start editing the label of the given item
620 void EditLabel( long item
);
622 // suspend/resume redrawing the control
628 void OnRenameTimer();
629 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
631 void OnMouse( wxMouseEvent
&event
);
633 // called to switch the selection from the current item to newCurrent,
634 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
636 void OnChar( wxKeyEvent
&event
);
637 void OnKeyDown( wxKeyEvent
&event
);
638 void OnSetFocus( wxFocusEvent
&event
);
639 void OnKillFocus( wxFocusEvent
&event
);
640 void OnScroll(wxScrollWinEvent
& event
) ;
642 void OnPaint( wxPaintEvent
&event
);
644 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
645 void GetImageSize( int index
, int &width
, int &height
) const;
646 int GetTextLength( const wxString
&s
) const;
648 void SetImageList( wxGenericImageList
*imageList
, int which
);
649 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
650 int GetItemSpacing( bool isSmall
= FALSE
);
652 void SetColumn( int col
, wxListItem
&item
);
653 void SetColumnWidth( int col
, int width
);
654 void GetColumn( int col
, wxListItem
&item
) const;
655 int GetColumnWidth( int col
) const;
656 int GetColumnCount() const { return m_columns
.GetCount(); }
658 // returns the sum of the heights of all columns
659 int GetHeaderWidth() const;
661 int GetCountPerPage() const;
663 void SetItem( wxListItem
&item
);
664 void GetItem( wxListItem
&item
) const;
665 void SetItemState( long item
, long state
, long stateMask
);
666 int GetItemState( long item
, long stateMask
) const;
667 void GetItemRect( long index
, wxRect
&rect
) const;
668 bool GetItemPosition( long item
, wxPoint
& pos
) const;
669 int GetSelectedItemCount() const;
671 wxString
GetItemText(long item
) const
674 info
.m_itemId
= item
;
679 void SetItemText(long item
, const wxString
& value
)
682 info
.m_mask
= wxLIST_MASK_TEXT
;
683 info
.m_itemId
= item
;
688 // set the scrollbars and update the positions of the items
689 void RecalculatePositions(bool noRefresh
= FALSE
);
691 // refresh the window and the header
694 long GetNextItem( long item
, int geometry
, int state
) const;
695 void DeleteItem( long index
);
696 void DeleteAllItems();
697 void DeleteColumn( int col
);
698 void DeleteEverything();
699 void EnsureVisible( long index
);
700 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
701 long FindItem( long start
, long data
);
702 long HitTest( int x
, int y
, int &flags
);
703 void InsertItem( wxListItem
&item
);
704 void InsertColumn( long col
, wxListItem
&item
);
705 void SortItems( wxListCtrlCompare fn
, long data
);
707 size_t GetItemCount() const;
708 bool IsEmpty() const { return GetItemCount() == 0; }
709 void SetItemCount(long count
);
711 // change the current (== focused) item, send a notification event
712 void ChangeCurrent(size_t current
);
713 void ResetCurrent() { ChangeCurrent((size_t)-1); }
714 bool HasCurrent() const { return m_current
!= (size_t)-1; }
716 // send out a wxListEvent
717 void SendNotify( size_t line
,
719 wxPoint point
= wxDefaultPosition
);
721 // override base class virtual to reset m_lineHeight when the font changes
722 virtual bool SetFont(const wxFont
& font
)
724 if ( !wxScrolledWindow::SetFont(font
) )
732 // these are for wxListLineData usage only
734 // get the backpointer to the list ctrl
735 wxGenericListCtrl
*GetListCtrl() const
737 return wxStaticCast(GetParent(), wxGenericListCtrl
);
740 // get the height of all lines (assuming they all do have the same height)
741 wxCoord
GetLineHeight() const;
743 // get the y position of the given line (only for report view)
744 wxCoord
GetLineY(size_t line
) const;
746 // get the brush to use for the item highlighting
747 wxBrush
*GetHighlightBrush() const
749 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
753 // the array of all line objects for a non virtual list control (for the
754 // virtual list control we only ever use m_lines[0])
755 wxListLineDataArray m_lines
;
757 // the list of column objects
758 wxListHeaderDataList m_columns
;
760 // currently focused item or -1
763 // the number of lines per page
766 // this flag is set when something which should result in the window
767 // redrawing happens (i.e. an item was added or deleted, or its appearance
768 // changed) and OnPaint() doesn't redraw the window while it is set which
769 // allows to minimize the number of repaintings when a lot of items are
770 // being added. The real repainting occurs only after the next OnIdle()
774 wxColour
*m_highlightColour
;
777 wxGenericImageList
*m_small_image_list
;
778 wxGenericImageList
*m_normal_image_list
;
780 int m_normal_spacing
;
784 wxTimer
*m_renameTimer
;
789 // for double click logic
790 size_t m_lineLastClicked
,
791 m_lineBeforeLastClicked
;
794 // the total count of items in a virtual list control
797 // the object maintaining the items selection state, only used in virtual
799 wxSelectionStore m_selStore
;
801 // common part of all ctors
804 // intiialize m_[xy]Scroll
805 void InitScrolling();
807 // get the line data for the given index
808 wxListLineData
*GetLine(size_t n
) const
810 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
814 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
822 // get a dummy line which can be used for geometry calculations and such:
823 // you must use GetLine() if you want to really draw the line
824 wxListLineData
*GetDummyLine() const;
826 // cache the line data of the n-th line in m_lines[0]
827 void CacheLineData(size_t line
);
829 // get the range of visible lines
830 void GetVisibleLinesRange(size_t *from
, size_t *to
);
832 // force us to recalculate the range of visible lines
833 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
835 // get the colour to be used for drawing the rules
836 wxColour
GetRuleColour() const
841 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
846 // initialize the current item if needed
847 void UpdateCurrent();
849 // delete all items but don't refresh: called from dtor
850 void DoDeleteAllItems();
852 // the height of one line using the current font
853 wxCoord m_lineHeight
;
855 // the total header width or 0 if not calculated yet
856 wxCoord m_headerWidth
;
858 // the first and last lines being shown on screen right now (inclusive),
859 // both may be -1 if they must be calculated so never access them directly:
860 // use GetVisibleLinesRange() above instead
864 // the brushes to use for item highlighting when we do/don't have focus
865 wxBrush
*m_highlightBrush
,
866 *m_highlightUnfocusedBrush
;
868 // if this is > 0, the control is frozen and doesn't redraw itself
869 size_t m_freezeCount
;
871 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
872 DECLARE_EVENT_TABLE()
875 // ============================================================================
877 // ============================================================================
879 // ----------------------------------------------------------------------------
881 // ----------------------------------------------------------------------------
883 bool wxSelectionStore::IsSelected(size_t item
) const
885 bool isSel
= m_itemsSel
.Index(item
) != wxNOT_FOUND
;
887 // if the default state is to be selected, being in m_itemsSel means that
888 // the item is not selected, so we have to inverse the logic
889 return m_defaultState
? !isSel
: isSel
;
892 bool wxSelectionStore::SelectItem(size_t item
, bool select
)
894 // search for the item ourselves as like this we get the index where to
895 // insert it later if needed, so we do only one search in the array instead
896 // of two (adding item to a sorted array requires a search)
897 size_t index
= m_itemsSel
.IndexForInsert(item
);
898 bool isSel
= index
< m_itemsSel
.GetCount() && m_itemsSel
[index
] == item
;
900 if ( select
!= m_defaultState
)
904 m_itemsSel
.AddAt(item
, index
);
909 else // reset to default state
913 m_itemsSel
.RemoveAt(index
);
921 bool wxSelectionStore::SelectRange(size_t itemFrom
, size_t itemTo
,
923 wxArrayInt
*itemsChanged
)
925 // 100 is hardcoded but it shouldn't matter much: the important thing is
926 // that we don't refresh everything when really few (e.g. 1 or 2) items
928 static const size_t MANY_ITEMS
= 100;
930 wxASSERT_MSG( itemFrom
<= itemTo
, _T("should be in order") );
932 // are we going to have more [un]selected items than the other ones?
933 if ( itemTo
- itemFrom
> m_count
/2 )
935 if ( select
!= m_defaultState
)
937 // the default state now becomes the same as 'select'
938 m_defaultState
= select
;
940 // so all the old selections (which had state select) shouldn't be
941 // selected any more, but all the other ones should
942 wxIndexArray selOld
= m_itemsSel
;
945 // TODO: it should be possible to optimize the searches a bit
946 // knowing the possible range
949 for ( item
= 0; item
< itemFrom
; item
++ )
951 if ( selOld
.Index(item
) == wxNOT_FOUND
)
952 m_itemsSel
.Add(item
);
955 for ( item
= itemTo
+ 1; item
< m_count
; item
++ )
957 if ( selOld
.Index(item
) == wxNOT_FOUND
)
958 m_itemsSel
.Add(item
);
961 // many items (> half) changed state
964 else // select == m_defaultState
966 // get the inclusive range of items between itemFrom and itemTo
967 size_t count
= m_itemsSel
.GetCount(),
968 start
= m_itemsSel
.IndexForInsert(itemFrom
),
969 end
= m_itemsSel
.IndexForInsert(itemTo
);
971 if ( start
== count
|| m_itemsSel
[start
] < itemFrom
)
976 if ( end
== count
|| m_itemsSel
[end
] > itemTo
)
983 // delete all of them (from end to avoid changing indices)
984 for ( int i
= end
; i
>= (int)start
; i
-- )
988 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
990 // stop counting (see comment below)
995 itemsChanged
->Add(m_itemsSel
[i
]);
999 m_itemsSel
.RemoveAt(i
);
1004 else // "few" items change state
1008 itemsChanged
->Empty();
1011 // just add the items to the selection
1012 for ( size_t item
= itemFrom
; item
<= itemTo
; item
++ )
1014 if ( SelectItem(item
, select
) && itemsChanged
)
1016 itemsChanged
->Add(item
);
1018 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
1020 // stop counting them, we'll just eat gobs of memory
1021 // for nothing at all - faster to refresh everything in
1023 itemsChanged
= NULL
;
1029 // we set it to NULL if there are many items changing state
1030 return itemsChanged
!= NULL
;
1033 void wxSelectionStore::OnItemDelete(size_t item
)
1035 size_t count
= m_itemsSel
.GetCount(),
1036 i
= m_itemsSel
.IndexForInsert(item
);
1038 if ( i
< count
&& m_itemsSel
[i
] == item
)
1040 // this item itself was in m_itemsSel, remove it from there
1041 m_itemsSel
.RemoveAt(i
);
1046 // and adjust the index of all which follow it
1049 // all following elements must be greater than the one we deleted
1050 wxASSERT_MSG( m_itemsSel
[i
] > item
, _T("logic error") );
1056 //-----------------------------------------------------------------------------
1058 //-----------------------------------------------------------------------------
1060 wxListItemData::~wxListItemData()
1062 // in the virtual list control the attributes are managed by the main
1063 // program, so don't delete them
1064 if ( !m_owner
->IsVirtual() )
1072 void wxListItemData::Init()
1080 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
1086 if ( owner
->InReportView() )
1092 m_rect
= new wxRect
;
1096 void wxListItemData::SetItem( const wxListItem
&info
)
1098 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
1099 SetText(info
.m_text
);
1100 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
1101 m_image
= info
.m_image
;
1102 if ( info
.m_mask
& wxLIST_MASK_DATA
)
1103 m_data
= info
.m_data
;
1105 if ( info
.HasAttributes() )
1108 *m_attr
= *info
.GetAttributes();
1110 m_attr
= new wxListItemAttr(*info
.GetAttributes());
1118 m_rect
->width
= info
.m_width
;
1122 void wxListItemData::SetPosition( int x
, int y
)
1124 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
1130 void wxListItemData::SetSize( int width
, int height
)
1132 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
1135 m_rect
->width
= width
;
1137 m_rect
->height
= height
;
1140 bool wxListItemData::IsHit( int x
, int y
) const
1142 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
1144 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
1147 int wxListItemData::GetX() const
1149 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1154 int wxListItemData::GetY() const
1156 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1161 int wxListItemData::GetWidth() const
1163 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1165 return m_rect
->width
;
1168 int wxListItemData::GetHeight() const
1170 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1172 return m_rect
->height
;
1175 void wxListItemData::GetItem( wxListItem
&info
) const
1177 info
.m_text
= m_text
;
1178 info
.m_image
= m_image
;
1179 info
.m_data
= m_data
;
1183 if ( m_attr
->HasTextColour() )
1184 info
.SetTextColour(m_attr
->GetTextColour());
1185 if ( m_attr
->HasBackgroundColour() )
1186 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1187 if ( m_attr
->HasFont() )
1188 info
.SetFont(m_attr
->GetFont());
1192 //-----------------------------------------------------------------------------
1194 //-----------------------------------------------------------------------------
1196 void wxListHeaderData::Init()
1207 wxListHeaderData::wxListHeaderData()
1212 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1219 void wxListHeaderData::SetItem( const wxListItem
&item
)
1221 m_mask
= item
.m_mask
;
1223 if ( m_mask
& wxLIST_MASK_TEXT
)
1224 m_text
= item
.m_text
;
1226 if ( m_mask
& wxLIST_MASK_IMAGE
)
1227 m_image
= item
.m_image
;
1229 if ( m_mask
& wxLIST_MASK_FORMAT
)
1230 m_format
= item
.m_format
;
1232 if ( m_mask
& wxLIST_MASK_WIDTH
)
1233 SetWidth(item
.m_width
);
1236 void wxListHeaderData::SetPosition( int x
, int y
)
1242 void wxListHeaderData::SetHeight( int h
)
1247 void wxListHeaderData::SetWidth( int w
)
1251 m_width
= WIDTH_COL_DEFAULT
;
1252 else if (m_width
< WIDTH_COL_MIN
)
1253 m_width
= WIDTH_COL_MIN
;
1256 void wxListHeaderData::SetFormat( int format
)
1261 bool wxListHeaderData::HasImage() const
1263 return m_image
!= -1;
1266 bool wxListHeaderData::IsHit( int x
, int y
) const
1268 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1271 void wxListHeaderData::GetItem( wxListItem
& item
)
1273 item
.m_mask
= m_mask
;
1274 item
.m_text
= m_text
;
1275 item
.m_image
= m_image
;
1276 item
.m_format
= m_format
;
1277 item
.m_width
= m_width
;
1280 int wxListHeaderData::GetImage() const
1285 int wxListHeaderData::GetWidth() const
1290 int wxListHeaderData::GetFormat() const
1295 //-----------------------------------------------------------------------------
1297 //-----------------------------------------------------------------------------
1299 inline int wxListLineData::GetMode() const
1301 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1304 inline bool wxListLineData::InReportView() const
1306 return m_owner
->HasFlag(wxLC_REPORT
);
1309 inline bool wxListLineData::IsVirtual() const
1311 return m_owner
->IsVirtual();
1314 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1317 m_items
.DeleteContents( TRUE
);
1319 if ( InReportView() )
1325 m_gi
= new GeometryInfo
;
1328 m_highlighted
= FALSE
;
1330 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1333 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1335 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1336 wxCHECK_RET( node
, _T("no subitems at all??") );
1338 wxListItemData
*item
= node
->GetData();
1340 switch ( GetMode() )
1343 case wxLC_SMALL_ICON
:
1345 m_gi
->m_rectAll
.width
= spacing
;
1347 wxString s
= item
->GetText();
1353 m_gi
->m_rectLabel
.width
=
1354 m_gi
->m_rectLabel
.height
= 0;
1358 dc
->GetTextExtent( s
, &lw
, &lh
);
1359 if (lh
< SCROLL_UNIT_Y
)
1364 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1366 m_gi
->m_rectAll
.width
= lw
;
1368 m_gi
->m_rectLabel
.width
= lw
;
1369 m_gi
->m_rectLabel
.height
= lh
;
1372 if (item
->HasImage())
1375 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1376 m_gi
->m_rectIcon
.width
= w
+ 8;
1377 m_gi
->m_rectIcon
.height
= h
+ 8;
1379 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1380 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1381 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1382 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1385 if ( item
->HasText() )
1387 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1388 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1390 else // no text, highlight the icon
1392 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1393 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1400 wxString s
= item
->GetTextForMeasuring();
1403 dc
->GetTextExtent( s
, &lw
, &lh
);
1404 if (lh
< SCROLL_UNIT_Y
)
1409 m_gi
->m_rectLabel
.width
= lw
;
1410 m_gi
->m_rectLabel
.height
= lh
;
1412 m_gi
->m_rectAll
.width
= lw
;
1413 m_gi
->m_rectAll
.height
= lh
;
1415 if (item
->HasImage())
1418 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1419 m_gi
->m_rectIcon
.width
= w
;
1420 m_gi
->m_rectIcon
.height
= h
;
1422 m_gi
->m_rectAll
.width
+= 4 + w
;
1423 if (h
> m_gi
->m_rectAll
.height
)
1424 m_gi
->m_rectAll
.height
= h
;
1427 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1428 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1433 wxFAIL_MSG( _T("unexpected call to SetSize") );
1437 wxFAIL_MSG( _T("unknown mode") );
1441 void wxListLineData::SetPosition( int x
, int y
,
1445 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1446 wxCHECK_RET( node
, _T("no subitems at all??") );
1448 wxListItemData
*item
= node
->GetData();
1450 switch ( GetMode() )
1453 case wxLC_SMALL_ICON
:
1454 m_gi
->m_rectAll
.x
= x
;
1455 m_gi
->m_rectAll
.y
= y
;
1457 if ( item
->HasImage() )
1459 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1460 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1461 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1464 if ( item
->HasText() )
1466 if (m_gi
->m_rectAll
.width
> spacing
)
1467 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1469 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1470 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1471 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1472 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1474 else // no text, highlight the icon
1476 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1477 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1482 m_gi
->m_rectAll
.x
= x
;
1483 m_gi
->m_rectAll
.y
= y
;
1485 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1486 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1487 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1489 if (item
->HasImage())
1491 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1492 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1493 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1497 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1502 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1506 wxFAIL_MSG( _T("unknown mode") );
1510 void wxListLineData::InitItems( int num
)
1512 for (int i
= 0; i
< num
; i
++)
1513 m_items
.Append( new wxListItemData(m_owner
) );
1516 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1518 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1519 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1521 wxListItemData
*item
= node
->GetData();
1522 item
->SetItem( info
);
1525 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1527 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1530 wxListItemData
*item
= node
->GetData();
1531 item
->GetItem( info
);
1535 wxString
wxListLineData::GetText(int index
) const
1539 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1542 wxListItemData
*item
= node
->GetData();
1543 s
= item
->GetText();
1549 void wxListLineData::SetText( int index
, const wxString s
)
1551 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1554 wxListItemData
*item
= node
->GetData();
1559 void wxListLineData::SetImage( int index
, int image
)
1561 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1562 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1564 wxListItemData
*item
= node
->GetData();
1565 item
->SetImage(image
);
1568 int wxListLineData::GetImage( int index
) const
1570 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1571 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1573 wxListItemData
*item
= node
->GetData();
1574 return item
->GetImage();
1577 wxListItemAttr
*wxListLineData::GetAttr() const
1579 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1580 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1582 wxListItemData
*item
= node
->GetData();
1583 return item
->GetAttr();
1586 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1588 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1589 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1591 wxListItemData
*item
= node
->GetData();
1592 item
->SetAttr(attr
);
1595 bool wxListLineData::SetAttributes(wxDC
*dc
,
1596 const wxListItemAttr
*attr
,
1599 wxWindow
*listctrl
= m_owner
->GetParent();
1603 // don't use foreground colour for drawing highlighted items - this might
1604 // make them completely invisible (and there is no way to do bit
1605 // arithmetics on wxColour, unfortunately)
1609 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1613 if ( attr
&& attr
->HasTextColour() )
1615 colText
= attr
->GetTextColour();
1619 colText
= listctrl
->GetForegroundColour();
1623 dc
->SetTextForeground(colText
);
1627 if ( attr
&& attr
->HasFont() )
1629 font
= attr
->GetFont();
1633 font
= listctrl
->GetFont();
1639 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1640 if ( highlighted
|| hasBgCol
)
1644 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1648 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1651 dc
->SetPen( *wxTRANSPARENT_PEN
);
1659 void wxListLineData::Draw( wxDC
*dc
)
1661 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1662 wxCHECK_RET( node
, _T("no subitems at all??") );
1664 bool highlighted
= IsHighlighted();
1666 wxListItemAttr
*attr
= GetAttr();
1668 if ( SetAttributes(dc
, attr
, highlighted
) )
1670 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1673 wxListItemData
*item
= node
->GetData();
1674 if (item
->HasImage())
1676 wxRect rectIcon
= m_gi
->m_rectIcon
;
1677 m_owner
->DrawImage( item
->GetImage(), dc
,
1678 rectIcon
.x
, rectIcon
.y
);
1681 if (item
->HasText())
1683 wxRect rectLabel
= m_gi
->m_rectLabel
;
1685 wxDCClipper
clipper(*dc
, rectLabel
);
1686 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1690 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1692 const wxRect
& rectHL
,
1695 // TODO: later we should support setting different attributes for
1696 // different columns - to do it, just add "col" argument to
1697 // GetAttr() and move these lines into the loop below
1698 wxListItemAttr
*attr
= GetAttr();
1699 if ( SetAttributes(dc
, attr
, highlighted
) )
1701 dc
->DrawRectangle( rectHL
);
1704 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1705 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1708 for ( wxListItemDataList::Node
*node
= m_items
.GetFirst();
1710 node
= node
->GetNext(), col
++ )
1712 wxListItemData
*item
= node
->GetData();
1714 int width
= m_owner
->GetColumnWidth(col
);
1718 if ( item
->HasImage() )
1721 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1722 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1724 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1730 wxDCClipper
clipper(*dc
, xOld
, y
, width
, rect
.height
);
1732 if ( item
->HasText() )
1734 dc
->DrawText( item
->GetText(), xOld
, y
);
1739 bool wxListLineData::Highlight( bool on
)
1741 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1743 if ( on
== m_highlighted
)
1751 void wxListLineData::ReverseHighlight( void )
1753 Highlight(!IsHighlighted());
1756 //-----------------------------------------------------------------------------
1757 // wxListHeaderWindow
1758 //-----------------------------------------------------------------------------
1760 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1762 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1763 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1764 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1765 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1768 void wxListHeaderWindow::Init()
1770 m_currentCursor
= (wxCursor
*) NULL
;
1771 m_isDragging
= FALSE
;
1775 wxListHeaderWindow::wxListHeaderWindow()
1779 m_owner
= (wxListMainWindow
*) NULL
;
1780 m_resizeCursor
= (wxCursor
*) NULL
;
1783 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1785 wxListMainWindow
*owner
,
1789 const wxString
&name
)
1790 : wxWindow( win
, id
, pos
, size
, style
, name
)
1795 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1797 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1800 wxListHeaderWindow::~wxListHeaderWindow()
1802 delete m_resizeCursor
;
1805 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1807 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
1808 GtkStateType state
= m_parent
->IsEnabled() ? GTK_STATE_NORMAL
1809 : GTK_STATE_INSENSITIVE
;
1811 x
= dc
->XLOG2DEV( x
);
1813 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
,
1814 state
, GTK_SHADOW_OUT
,
1815 (GdkRectangle
*) NULL
, m_wxwindow
,
1816 (char *)"button", // const_cast
1817 x
-1, y
-1, w
+2, h
+2);
1818 #elif defined( __WXMAC__ )
1819 const int m_corner
= 1;
1821 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1823 dc
->SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1824 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1825 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1827 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1830 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1831 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1833 dc
->SetPen( *wxWHITE_PEN
);
1834 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1835 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1836 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1837 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1839 const int m_corner
= 1;
1841 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1843 dc
->SetPen( *wxBLACK_PEN
);
1844 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1845 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1847 wxPen
pen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1850 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1851 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1853 dc
->SetPen( *wxWHITE_PEN
);
1854 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1855 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1856 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1857 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1861 // shift the DC origin to match the position of the main window horz
1862 // scrollbar: this allows us to always use logical coords
1863 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1866 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1869 m_owner
->GetViewStart( &x
, NULL
);
1871 // account for the horz scrollbar offset
1872 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1875 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1877 #if defined(__WXGTK__)
1878 wxClientDC
dc( this );
1880 wxPaintDC
dc( this );
1888 dc
.SetFont( GetFont() );
1890 // width and height of the entire header window
1892 GetClientSize( &w
, &h
);
1893 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1895 dc
.SetBackgroundMode(wxTRANSPARENT
);
1897 // do *not* use the listctrl colour for headers - one day we will have a
1898 // function to set it separately
1899 //dc.SetTextForeground( *wxBLACK );
1900 dc
.SetTextForeground(wxSystemSettings::
1901 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1903 int x
= HEADER_OFFSET_X
;
1905 int numColumns
= m_owner
->GetColumnCount();
1907 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1909 m_owner
->GetColumn( i
, item
);
1910 int wCol
= item
.m_width
;
1912 // the width of the rect to draw: make it smaller to fit entirely
1913 // inside the column rect
1916 dc
.SetPen( *wxWHITE_PEN
);
1918 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
1920 // if we have an image, draw it on the right of the label
1921 int image
= item
.m_image
;
1924 wxGenericImageList
*imageList
= m_owner
->m_small_image_list
;
1928 imageList
->GetSize(image
, ix
, iy
);
1935 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1936 wxIMAGELIST_DRAW_TRANSPARENT
1941 //else: ignore the column image
1944 // draw the text clipping it so that it doesn't overwrite the column
1946 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1948 dc
.DrawText( item
.GetText(),
1949 x
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1957 void wxListHeaderWindow::DrawCurrent()
1959 int x1
= m_currentX
;
1961 m_owner
->ClientToScreen( &x1
, &y1
);
1963 int x2
= m_currentX
;
1965 m_owner
->GetClientSize( NULL
, &y2
);
1966 m_owner
->ClientToScreen( &x2
, &y2
);
1969 dc
.SetLogicalFunction( wxINVERT
);
1970 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1971 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1975 dc
.DrawLine( x1
, y1
, x2
, y2
);
1977 dc
.SetLogicalFunction( wxCOPY
);
1979 dc
.SetPen( wxNullPen
);
1980 dc
.SetBrush( wxNullBrush
);
1983 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1985 // we want to work with logical coords
1987 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1988 int y
= event
.GetY();
1992 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
,
1993 event
.GetPosition());
1995 // we don't draw the line beyond our window, but we allow dragging it
1998 GetClientSize( &w
, NULL
);
1999 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
2002 // erase the line if it was drawn
2003 if ( m_currentX
< w
)
2006 if (event
.ButtonUp())
2009 m_isDragging
= FALSE
;
2011 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
2012 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
,
2013 event
.GetPosition());
2020 m_currentX
= m_minX
+ 7;
2022 // draw in the new location
2023 if ( m_currentX
< w
)
2027 else // not dragging
2030 bool hit_border
= FALSE
;
2032 // end of the current column
2035 // find the column where this event occured
2037 countCol
= m_owner
->GetColumnCount();
2038 for (col
= 0; col
< countCol
; col
++)
2040 xpos
+= m_owner
->GetColumnWidth( col
);
2043 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2045 // near the column border
2052 // inside the column
2059 if ( col
== countCol
)
2062 if (event
.LeftDown() || event
.RightUp())
2064 if (hit_border
&& event
.LeftDown())
2066 m_isDragging
= TRUE
;
2070 SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2071 event
.GetPosition());
2073 else // click on a column
2075 SendListEvent( event
.LeftDown()
2076 ? wxEVT_COMMAND_LIST_COL_CLICK
2077 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2078 event
.GetPosition());
2081 else if (event
.Moving())
2086 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2087 m_currentCursor
= m_resizeCursor
;
2091 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2092 m_currentCursor
= wxSTANDARD_CURSOR
;
2096 SetCursor(*m_currentCursor
);
2101 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2103 m_owner
->SetFocus();
2106 void wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
2108 wxWindow
*parent
= GetParent();
2109 wxListEvent
le( type
, parent
->GetId() );
2110 le
.SetEventObject( parent
);
2111 le
.m_pointDrag
= pos
;
2113 // the position should be relative to the parent window, not
2114 // this one for compatibility with MSW and common sense: the
2115 // user code doesn't know anything at all about this header
2116 // window, so why should it get positions relative to it?
2117 le
.m_pointDrag
.y
-= GetSize().y
;
2119 le
.m_col
= m_column
;
2120 parent
->GetEventHandler()->ProcessEvent( le
);
2123 //-----------------------------------------------------------------------------
2124 // wxListRenameTimer (internal)
2125 //-----------------------------------------------------------------------------
2127 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2132 void wxListRenameTimer::Notify()
2134 m_owner
->OnRenameTimer();
2137 //-----------------------------------------------------------------------------
2138 // wxListTextCtrl (internal)
2139 //-----------------------------------------------------------------------------
2141 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2142 EVT_CHAR (wxListTextCtrl::OnChar
)
2143 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2144 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2147 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
2148 : m_startValue(owner
->GetItemText(itemEdit
)),
2149 m_itemEdited(itemEdit
)
2154 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2156 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2157 &rectLabel
.x
, &rectLabel
.y
);
2159 (void)Create(owner
, wxID_ANY
, m_startValue
,
2160 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2161 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2164 void wxListTextCtrl::Finish()
2168 wxPendingDelete
.Append(this);
2172 m_owner
->SetFocus();
2176 bool wxListTextCtrl::AcceptChanges()
2178 const wxString value
= GetValue();
2180 if ( value
== m_startValue
)
2182 // nothing changed, always accept
2186 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2188 // vetoed by the user
2192 // accepted, do rename the item
2193 m_owner
->SetItemText(m_itemEdited
, value
);
2198 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2200 switch ( event
.m_keyCode
)
2203 if ( !AcceptChanges() )
2205 // vetoed by the user code
2208 //else: fall through
2219 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2227 // auto-grow the textctrl:
2228 wxSize parentSize
= m_owner
->GetSize();
2229 wxPoint myPos
= GetPosition();
2230 wxSize mySize
= GetSize();
2232 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2233 if (myPos
.x
+ sx
> parentSize
.x
)
2234 sx
= parentSize
.x
- myPos
.x
;
2242 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2246 (void)AcceptChanges();
2254 //-----------------------------------------------------------------------------
2256 //-----------------------------------------------------------------------------
2258 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2260 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2261 EVT_PAINT (wxListMainWindow::OnPaint
)
2262 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2263 EVT_CHAR (wxListMainWindow::OnChar
)
2264 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2265 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2266 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2267 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2270 void wxListMainWindow::Init()
2272 m_columns
.DeleteContents( TRUE
);
2276 m_lineTo
= (size_t)-1;
2282 m_small_image_list
= (wxGenericImageList
*) NULL
;
2283 m_normal_image_list
= (wxGenericImageList
*) NULL
;
2285 m_small_spacing
= 30;
2286 m_normal_spacing
= 40;
2290 m_isCreated
= FALSE
;
2292 m_lastOnSame
= FALSE
;
2293 m_renameTimer
= new wxListRenameTimer( this );
2297 m_lineBeforeLastClicked
= (size_t)-1;
2302 void wxListMainWindow::InitScrolling()
2304 if ( HasFlag(wxLC_REPORT
) )
2306 m_xScroll
= SCROLL_UNIT_X
;
2307 m_yScroll
= SCROLL_UNIT_Y
;
2311 m_xScroll
= SCROLL_UNIT_Y
;
2316 wxListMainWindow::wxListMainWindow()
2321 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2327 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2332 const wxString
&name
)
2333 : wxScrolledWindow( parent
, id
, pos
, size
,
2334 style
| wxHSCROLL
| wxVSCROLL
, name
)
2338 m_highlightBrush
= new wxBrush
2340 wxSystemSettings::GetColour
2342 wxSYS_COLOUR_HIGHLIGHT
2347 m_highlightUnfocusedBrush
= new wxBrush
2349 wxSystemSettings::GetColour
2351 wxSYS_COLOUR_BTNSHADOW
2360 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2362 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2365 wxListMainWindow::~wxListMainWindow()
2369 delete m_highlightBrush
;
2370 delete m_highlightUnfocusedBrush
;
2372 delete m_renameTimer
;
2375 void wxListMainWindow::CacheLineData(size_t line
)
2377 wxGenericListCtrl
*listctrl
= GetListCtrl();
2379 wxListLineData
*ld
= GetDummyLine();
2381 size_t countCol
= GetColumnCount();
2382 for ( size_t col
= 0; col
< countCol
; col
++ )
2384 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2387 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2388 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2391 wxListLineData
*wxListMainWindow::GetDummyLine() const
2393 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2395 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2397 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2399 // we need to recreate the dummy line if the number of columns in the
2400 // control changed as it would have the incorrect number of fields
2402 if ( !m_lines
.IsEmpty() &&
2403 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2405 self
->m_lines
.Clear();
2408 if ( m_lines
.IsEmpty() )
2410 wxListLineData
*line
= new wxListLineData(self
);
2411 self
->m_lines
.Add(line
);
2413 // don't waste extra memory -- there never going to be anything
2414 // else/more in this array
2415 self
->m_lines
.Shrink();
2421 // ----------------------------------------------------------------------------
2422 // line geometry (report mode only)
2423 // ----------------------------------------------------------------------------
2425 wxCoord
wxListMainWindow::GetLineHeight() const
2427 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2429 // we cache the line height as calling GetTextExtent() is slow
2430 if ( !m_lineHeight
)
2432 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2434 wxClientDC
dc( self
);
2435 dc
.SetFont( GetFont() );
2438 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2440 if ( y
< SCROLL_UNIT_Y
)
2444 self
->m_lineHeight
= y
+ LINE_SPACING
;
2447 return m_lineHeight
;
2450 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2452 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2454 return LINE_SPACING
+ line
*GetLineHeight();
2457 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2459 if ( !InReportView() )
2460 return GetLine(line
)->m_gi
->m_rectAll
;
2463 rect
.x
= HEADER_OFFSET_X
;
2464 rect
.y
= GetLineY(line
);
2465 rect
.width
= GetHeaderWidth();
2466 rect
.height
= GetLineHeight();
2471 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2473 if ( !InReportView() )
2474 return GetLine(line
)->m_gi
->m_rectLabel
;
2477 rect
.x
= HEADER_OFFSET_X
;
2478 rect
.y
= GetLineY(line
);
2479 rect
.width
= GetColumnWidth(0);
2480 rect
.height
= GetLineHeight();
2485 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2487 if ( !InReportView() )
2488 return GetLine(line
)->m_gi
->m_rectIcon
;
2490 wxListLineData
*ld
= GetLine(line
);
2491 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2494 rect
.x
= HEADER_OFFSET_X
;
2495 rect
.y
= GetLineY(line
);
2496 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2501 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2503 return InReportView() ? GetLineRect(line
)
2504 : GetLine(line
)->m_gi
->m_rectHighlight
;
2507 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2509 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2511 wxListLineData
*ld
= GetLine(line
);
2513 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2514 return wxLIST_HITTEST_ONITEMICON
;
2516 // VS: Testing for "ld->HasText() || InReportView()" instead of
2517 // "ld->HasText()" is needed to make empty lines in report view
2519 if ( ld
->HasText() || InReportView() )
2521 wxRect rect
= InReportView() ? GetLineRect(line
)
2522 : GetLineLabelRect(line
);
2524 if ( rect
.Inside(x
, y
) )
2525 return wxLIST_HITTEST_ONITEMLABEL
;
2531 // ----------------------------------------------------------------------------
2532 // highlight (selection) handling
2533 // ----------------------------------------------------------------------------
2535 bool wxListMainWindow::IsHighlighted(size_t line
) const
2539 return m_selStore
.IsSelected(line
);
2543 wxListLineData
*ld
= GetLine(line
);
2544 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2546 return ld
->IsHighlighted();
2550 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2556 wxArrayInt linesChanged
;
2557 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2560 // meny items changed state, refresh everything
2561 RefreshLines(lineFrom
, lineTo
);
2563 else // only a few items changed state, refresh only them
2565 size_t count
= linesChanged
.GetCount();
2566 for ( size_t n
= 0; n
< count
; n
++ )
2568 RefreshLine(linesChanged
[n
]);
2572 else // iterate over all items in non report view
2574 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2576 if ( HighlightLine(line
, highlight
) )
2584 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2590 changed
= m_selStore
.SelectItem(line
, highlight
);
2594 wxListLineData
*ld
= GetLine(line
);
2595 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2597 changed
= ld
->Highlight(highlight
);
2602 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2603 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2609 void wxListMainWindow::RefreshLine( size_t line
)
2611 if ( HasFlag(wxLC_REPORT
) )
2613 size_t visibleFrom
, visibleTo
;
2614 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2616 if ( line
< visibleFrom
|| line
> visibleTo
)
2620 wxRect rect
= GetLineRect(line
);
2622 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2623 RefreshRect( rect
);
2626 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2628 // we suppose that they are ordered by caller
2629 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2631 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2633 if ( HasFlag(wxLC_REPORT
) )
2635 size_t visibleFrom
, visibleTo
;
2636 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2638 if ( lineFrom
< visibleFrom
)
2639 lineFrom
= visibleFrom
;
2640 if ( lineTo
> visibleTo
)
2645 rect
.y
= GetLineY(lineFrom
);
2646 rect
.width
= GetClientSize().x
;
2647 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2649 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2650 RefreshRect( rect
);
2654 // TODO: this should be optimized...
2655 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2662 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2664 if ( HasFlag(wxLC_REPORT
) )
2667 GetVisibleLinesRange(&visibleFrom
, NULL
);
2669 if ( lineFrom
< visibleFrom
)
2670 lineFrom
= visibleFrom
;
2674 rect
.y
= GetLineY(lineFrom
);
2676 wxSize size
= GetClientSize();
2677 rect
.width
= size
.x
;
2678 // refresh till the bottom of the window
2679 rect
.height
= size
.y
- rect
.y
;
2681 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2682 RefreshRect( rect
);
2686 // TODO: how to do it more efficiently?
2691 void wxListMainWindow::RefreshSelected()
2697 if ( InReportView() )
2699 GetVisibleLinesRange(&from
, &to
);
2704 to
= GetItemCount() - 1;
2707 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2709 RefreshLine(m_current
);
2712 for ( size_t line
= from
; line
<= to
; line
++ )
2714 // NB: the test works as expected even if m_current == -1
2715 if ( line
!= m_current
&& IsHighlighted(line
) )
2722 void wxListMainWindow::Freeze()
2727 void wxListMainWindow::Thaw()
2729 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2731 if ( !--m_freezeCount
)
2737 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2739 // Note: a wxPaintDC must be constructed even if no drawing is
2740 // done (a Windows requirement).
2741 wxPaintDC
dc( this );
2743 if ( IsEmpty() || m_freezeCount
)
2745 // nothing to draw or not the moment to draw it
2751 // delay the repainting until we calculate all the items positions
2758 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2762 dc
.SetFont( GetFont() );
2764 if ( HasFlag(wxLC_REPORT
) )
2766 int lineHeight
= GetLineHeight();
2768 size_t visibleFrom
, visibleTo
;
2769 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2772 wxCoord xOrig
, yOrig
;
2773 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2775 // tell the caller cache to cache the data
2778 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2779 GetParent()->GetId());
2780 evCache
.SetEventObject( GetParent() );
2781 evCache
.m_oldItemIndex
= visibleFrom
;
2782 evCache
.m_itemIndex
= visibleTo
;
2783 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2786 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2788 rectLine
= GetLineRect(line
);
2790 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2791 rectLine
.width
, rectLine
.height
) )
2793 // don't redraw unaffected lines to avoid flicker
2797 GetLine(line
)->DrawInReportMode( &dc
,
2799 GetLineHighlightRect(line
),
2800 IsHighlighted(line
) );
2803 if ( HasFlag(wxLC_HRULES
) )
2805 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2806 wxSize clientSize
= GetClientSize();
2808 for ( size_t i
= visibleFrom
; i
<= visibleTo
; i
++ )
2811 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2812 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2813 clientSize
.x
- dev_x
, i
*lineHeight
);
2816 // Draw last horizontal rule
2817 if ( visibleTo
> visibleFrom
)
2820 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2821 dc
.DrawLine(0 - dev_x
, m_lineTo
*lineHeight
,
2822 clientSize
.x
- dev_x
, m_lineTo
*lineHeight
);
2826 // Draw vertical rules if required
2827 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2829 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2832 wxRect firstItemRect
;
2833 wxRect lastItemRect
;
2834 GetItemRect(0, firstItemRect
);
2835 GetItemRect(GetItemCount() - 1, lastItemRect
);
2836 int x
= firstItemRect
.GetX();
2838 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2839 for (col
= 0; col
< GetColumnCount(); col
++)
2841 int colWidth
= GetColumnWidth(col
);
2843 dc
.DrawLine(x
- dev_x
, firstItemRect
.GetY() - 1 - dev_y
,
2844 x
- dev_x
, lastItemRect
.GetBottom() + 1 - dev_y
);
2850 size_t count
= GetItemCount();
2851 for ( size_t i
= 0; i
< count
; i
++ )
2853 GetLine(i
)->Draw( &dc
);
2859 // don't draw rect outline under Max if we already have the background
2860 // color but under other platforms only draw it if we do: it is a bit
2861 // silly to draw "focus rect" if we don't have focus!
2866 #endif // __WXMAC__/!__WXMAC__
2868 dc
.SetPen( *wxBLACK_PEN
);
2869 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2870 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2877 void wxListMainWindow::HighlightAll( bool on
)
2879 if ( IsSingleSel() )
2881 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2883 // we just have one item to turn off
2884 if ( HasCurrent() && IsHighlighted(m_current
) )
2886 HighlightLine(m_current
, FALSE
);
2887 RefreshLine(m_current
);
2892 HighlightLines(0, GetItemCount() - 1, on
);
2896 void wxListMainWindow::SendNotify( size_t line
,
2897 wxEventType command
,
2900 wxListEvent
le( command
, GetParent()->GetId() );
2901 le
.SetEventObject( GetParent() );
2902 le
.m_itemIndex
= line
;
2904 // set only for events which have position
2905 if ( point
!= wxDefaultPosition
)
2906 le
.m_pointDrag
= point
;
2908 // don't try to get the line info for virtual list controls: the main
2909 // program has it anyhow and if we did it would result in accessing all
2910 // the lines, even those which are not visible now and this is precisely
2911 // what we're trying to avoid
2912 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2914 if ( line
!= (size_t)-1 )
2916 GetLine(line
)->GetItem( 0, le
.m_item
);
2918 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2920 //else: there may be no more such item
2922 GetParent()->GetEventHandler()->ProcessEvent( le
);
2925 void wxListMainWindow::ChangeCurrent(size_t current
)
2927 m_current
= current
;
2929 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2932 void wxListMainWindow::EditLabel( long item
)
2934 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2935 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2937 size_t itemEdit
= (size_t)item
;
2939 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2940 le
.SetEventObject( GetParent() );
2941 le
.m_itemIndex
= item
;
2942 wxListLineData
*data
= GetLine(itemEdit
);
2943 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2944 data
->GetItem( 0, le
.m_item
);
2945 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2947 // vetoed by user code
2951 // We have to call this here because the label in question might just have
2952 // been added and no screen update taken place.
2956 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2961 void wxListMainWindow::OnRenameTimer()
2963 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2965 EditLabel( m_current
);
2968 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2970 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2971 le
.SetEventObject( GetParent() );
2972 le
.m_itemIndex
= itemEdit
;
2974 wxListLineData
*data
= GetLine(itemEdit
);
2975 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
2977 data
->GetItem( 0, le
.m_item
);
2978 le
.m_item
.m_text
= value
;
2979 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2983 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2985 event
.SetEventObject( GetParent() );
2986 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2989 if ( !HasCurrent() || IsEmpty() )
2995 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2996 event
.ButtonDClick()) )
2999 int x
= event
.GetX();
3000 int y
= event
.GetY();
3001 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3003 // where did we hit it (if we did)?
3006 size_t count
= GetItemCount(),
3009 if ( HasFlag(wxLC_REPORT
) )
3011 current
= y
/ GetLineHeight();
3012 if ( current
< count
)
3013 hitResult
= HitTestLine(current
, x
, y
);
3017 // TODO: optimize it too! this is less simple than for report view but
3018 // enumerating all items is still not a way to do it!!
3019 for ( current
= 0; current
< count
; current
++ )
3021 hitResult
= HitTestLine(current
, x
, y
);
3027 if (event
.Dragging())
3029 if (m_dragCount
== 0)
3031 // we have to report the raw, physical coords as we want to be
3032 // able to call HitTest(event.m_pointDrag) from the user code to
3033 // get the item being dragged
3034 m_dragStart
= event
.GetPosition();
3039 if (m_dragCount
!= 3)
3042 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3043 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3045 wxListEvent
le( command
, GetParent()->GetId() );
3046 le
.SetEventObject( GetParent() );
3047 le
.m_pointDrag
= m_dragStart
;
3048 GetParent()->GetEventHandler()->ProcessEvent( le
);
3059 // outside of any item
3063 bool forceClick
= FALSE
;
3064 if (event
.ButtonDClick())
3066 m_renameTimer
->Stop();
3067 m_lastOnSame
= FALSE
;
3070 // FIXME: wxGTK generates bad sequence of events prior to doubleclick
3071 // ("down, up, down, double, up" while other ports
3072 // do "down, up, double, up"). We have to have this hack
3073 // in place till somebody fixes wxGTK...
3074 if ( current
== m_lineBeforeLastClicked
)
3076 if ( current
== m_lineLastClicked
)
3079 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3085 // the first click was on another item, so don't interpret this as
3086 // a double click, but as a simple click instead
3091 if (event
.LeftUp() && m_lastOnSame
)
3093 if ((current
== m_current
) &&
3094 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3095 HasFlag(wxLC_EDIT_LABELS
) )
3097 m_renameTimer
->Start( 100, TRUE
);
3099 m_lastOnSame
= FALSE
;
3101 else if (event
.RightDown())
3103 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
3104 event
.GetPosition() );
3106 else if (event
.MiddleDown())
3108 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3110 else if ( event
.LeftDown() || forceClick
)
3112 m_lineBeforeLastClicked
= m_lineLastClicked
;
3113 m_lineLastClicked
= current
;
3115 size_t oldCurrent
= m_current
;
3117 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3119 HighlightAll( FALSE
);
3121 ChangeCurrent(current
);
3123 ReverseHighlight(m_current
);
3125 else // multi sel & either ctrl or shift is down
3127 if (event
.ControlDown())
3129 ChangeCurrent(current
);
3131 ReverseHighlight(m_current
);
3133 else if (event
.ShiftDown())
3135 ChangeCurrent(current
);
3137 size_t lineFrom
= oldCurrent
,
3140 if ( lineTo
< lineFrom
)
3143 lineFrom
= m_current
;
3146 HighlightLines(lineFrom
, lineTo
);
3148 else // !ctrl, !shift
3150 // test in the enclosing if should make it impossible
3151 wxFAIL_MSG( _T("how did we get here?") );
3155 if (m_current
!= oldCurrent
)
3157 RefreshLine( oldCurrent
);
3160 // forceClick is only set if the previous click was on another item
3161 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3165 void wxListMainWindow::MoveToItem(size_t item
)
3167 if ( item
== (size_t)-1 )
3170 wxRect rect
= GetLineRect(item
);
3172 int client_w
, client_h
;
3173 GetClientSize( &client_w
, &client_h
);
3175 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3176 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3178 if ( HasFlag(wxLC_REPORT
) )
3180 // the next we need the range of lines shown it might be different, so
3182 ResetVisibleLinesRange();
3184 if (rect
.y
< view_y
)
3185 Scroll( -1, rect
.y
/m_yScroll
);
3186 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3187 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3191 if (rect
.x
-view_x
< 5)
3192 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3193 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3194 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3198 // ----------------------------------------------------------------------------
3199 // keyboard handling
3200 // ----------------------------------------------------------------------------
3202 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3204 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3205 _T("invalid item index in OnArrowChar()") );
3207 size_t oldCurrent
= m_current
;
3209 // in single selection we just ignore Shift as we can't select several
3211 if ( event
.ShiftDown() && !IsSingleSel() )
3213 ChangeCurrent(newCurrent
);
3215 // select all the items between the old and the new one
3216 if ( oldCurrent
> newCurrent
)
3218 newCurrent
= oldCurrent
;
3219 oldCurrent
= m_current
;
3222 HighlightLines(oldCurrent
, newCurrent
);
3226 // all previously selected items are unselected unless ctrl is held
3227 if ( !event
.ControlDown() )
3228 HighlightAll(FALSE
);
3230 ChangeCurrent(newCurrent
);
3232 // refresh the old focus to remove it
3233 RefreshLine( oldCurrent
);
3235 if ( !event
.ControlDown() )
3237 HighlightLine( m_current
, TRUE
);
3241 RefreshLine( m_current
);
3246 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3248 wxWindow
*parent
= GetParent();
3250 /* we propagate the key event up */
3251 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3252 ke
.m_shiftDown
= event
.m_shiftDown
;
3253 ke
.m_controlDown
= event
.m_controlDown
;
3254 ke
.m_altDown
= event
.m_altDown
;
3255 ke
.m_metaDown
= event
.m_metaDown
;
3256 ke
.m_keyCode
= event
.m_keyCode
;
3259 ke
.SetEventObject( parent
);
3260 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3265 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3267 wxWindow
*parent
= GetParent();
3269 /* we send a list_key event up */
3272 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3273 le
.m_itemIndex
= m_current
;
3274 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3275 le
.m_code
= (int)event
.KeyCode();
3276 le
.SetEventObject( parent
);
3277 parent
->GetEventHandler()->ProcessEvent( le
);
3280 /* we propagate the char event up */
3281 wxKeyEvent
ke( wxEVT_CHAR
);
3282 ke
.m_shiftDown
= event
.m_shiftDown
;
3283 ke
.m_controlDown
= event
.m_controlDown
;
3284 ke
.m_altDown
= event
.m_altDown
;
3285 ke
.m_metaDown
= event
.m_metaDown
;
3286 ke
.m_keyCode
= event
.m_keyCode
;
3289 ke
.SetEventObject( parent
);
3290 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3292 if (event
.KeyCode() == WXK_TAB
)
3294 wxNavigationKeyEvent nevent
;
3295 nevent
.SetWindowChange( event
.ControlDown() );
3296 nevent
.SetDirection( !event
.ShiftDown() );
3297 nevent
.SetEventObject( GetParent()->GetParent() );
3298 nevent
.SetCurrentFocus( m_parent
);
3299 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3303 /* no item -> nothing to do */
3310 switch (event
.KeyCode())
3313 if ( m_current
> 0 )
3314 OnArrowChar( m_current
- 1, event
);
3318 if ( m_current
< (size_t)GetItemCount() - 1 )
3319 OnArrowChar( m_current
+ 1, event
);
3324 OnArrowChar( GetItemCount() - 1, event
);
3329 OnArrowChar( 0, event
);
3335 if ( HasFlag(wxLC_REPORT
) )
3337 steps
= m_linesPerPage
- 1;
3341 steps
= m_current
% m_linesPerPage
;
3344 int index
= m_current
- steps
;
3348 OnArrowChar( index
, event
);
3355 if ( HasFlag(wxLC_REPORT
) )
3357 steps
= m_linesPerPage
- 1;
3361 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3364 size_t index
= m_current
+ steps
;
3365 size_t count
= GetItemCount();
3366 if ( index
>= count
)
3369 OnArrowChar( index
, event
);
3374 if ( !HasFlag(wxLC_REPORT
) )
3376 int index
= m_current
- m_linesPerPage
;
3380 OnArrowChar( index
, event
);
3385 if ( !HasFlag(wxLC_REPORT
) )
3387 size_t index
= m_current
+ m_linesPerPage
;
3389 size_t count
= GetItemCount();
3390 if ( index
>= count
)
3393 OnArrowChar( index
, event
);
3398 if ( IsSingleSel() )
3400 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3402 if ( IsHighlighted(m_current
) )
3404 // don't unselect the item in single selection mode
3407 //else: select it in ReverseHighlight() below if unselected
3410 ReverseHighlight(m_current
);
3415 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3423 // ----------------------------------------------------------------------------
3425 // ----------------------------------------------------------------------------
3427 void wxListMainWindow::SetFocus()
3429 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3430 // overrides SetFocus in such way that it does never change focus from
3431 // panel's child to the panel itself. Unfortunately, we must be able to change
3432 // focus to the panel from wxListTextCtrl because the text control should
3433 // disappear when the user clicks outside it.
3435 wxWindow
*oldFocus
= FindFocus();
3437 if ( oldFocus
&& oldFocus
->GetParent() == this )
3439 wxWindow::SetFocus();
3443 wxScrolledWindow::SetFocus();
3447 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3449 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3450 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3451 // which are already drawn correctly resulting in horrible flicker - avoid
3463 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3464 event
.SetEventObject( GetParent() );
3465 GetParent()->GetEventHandler()->ProcessEvent( event
);
3468 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3475 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3477 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3479 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3481 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3483 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3485 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3487 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3489 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3491 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3495 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3497 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3499 m_normal_image_list
->GetSize( index
, width
, height
);
3501 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3503 m_small_image_list
->GetSize( index
, width
, height
);
3505 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3507 m_small_image_list
->GetSize( index
, width
, height
);
3509 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3511 m_small_image_list
->GetSize( index
, width
, height
);
3520 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3522 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3523 dc
.SetFont( GetFont() );
3526 dc
.GetTextExtent( s
, &lw
, NULL
);
3528 return lw
+ AUTOSIZE_COL_MARGIN
;
3531 void wxListMainWindow::SetImageList( wxGenericImageList
*imageList
, int which
)
3535 // calc the spacing from the icon size
3538 if ((imageList
) && (imageList
->GetImageCount()) )
3540 imageList
->GetSize(0, width
, height
);
3543 if (which
== wxIMAGE_LIST_NORMAL
)
3545 m_normal_image_list
= imageList
;
3546 m_normal_spacing
= width
+ 8;
3549 if (which
== wxIMAGE_LIST_SMALL
)
3551 m_small_image_list
= imageList
;
3552 m_small_spacing
= width
+ 14;
3556 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3561 m_small_spacing
= spacing
;
3565 m_normal_spacing
= spacing
;
3569 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3571 return isSmall
? m_small_spacing
: m_normal_spacing
;
3574 // ----------------------------------------------------------------------------
3576 // ----------------------------------------------------------------------------
3578 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3580 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3582 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3584 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3585 item
.m_width
= GetTextLength( item
.m_text
);
3587 wxListHeaderData
*column
= node
->GetData();
3588 column
->SetItem( item
);
3590 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3592 headerWin
->m_dirty
= TRUE
;
3596 // invalidate it as it has to be recalculated
3600 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3602 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3603 _T("invalid column index") );
3605 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3606 _T("SetColumnWidth() can only be called in report mode.") );
3609 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3611 headerWin
->m_dirty
= TRUE
;
3613 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3614 wxCHECK_RET( node
, _T("no column?") );
3616 wxListHeaderData
*column
= node
->GetData();
3618 size_t count
= GetItemCount();
3620 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3622 width
= GetTextLength(column
->GetText());
3624 else if ( width
== wxLIST_AUTOSIZE
)
3628 // TODO: determine the max width somehow...
3629 width
= WIDTH_COL_DEFAULT
;
3633 wxClientDC
dc(this);
3634 dc
.SetFont( GetFont() );
3636 int max
= AUTOSIZE_COL_MARGIN
;
3638 for ( size_t i
= 0; i
< count
; i
++ )
3640 wxListLineData
*line
= GetLine(i
);
3641 wxListItemDataList::Node
*n
= line
->m_items
.Item( col
);
3643 wxCHECK_RET( n
, _T("no subitem?") );
3645 wxListItemData
*item
= n
->GetData();
3648 if (item
->HasImage())
3651 GetImageSize( item
->GetImage(), ix
, iy
);
3655 if (item
->HasText())
3658 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3666 width
= max
+ AUTOSIZE_COL_MARGIN
;
3670 column
->SetWidth( width
);
3672 // invalidate it as it has to be recalculated
3676 int wxListMainWindow::GetHeaderWidth() const
3678 if ( !m_headerWidth
)
3680 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3682 size_t count
= GetColumnCount();
3683 for ( size_t col
= 0; col
< count
; col
++ )
3685 self
->m_headerWidth
+= GetColumnWidth(col
);
3689 return m_headerWidth
;
3692 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3694 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3695 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3697 wxListHeaderData
*column
= node
->GetData();
3698 column
->GetItem( item
);
3701 int wxListMainWindow::GetColumnWidth( int col
) const
3703 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3704 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3706 wxListHeaderData
*column
= node
->GetData();
3707 return column
->GetWidth();
3710 // ----------------------------------------------------------------------------
3712 // ----------------------------------------------------------------------------
3714 void wxListMainWindow::SetItem( wxListItem
&item
)
3716 long id
= item
.m_itemId
;
3717 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3718 _T("invalid item index in SetItem") );
3722 wxListLineData
*line
= GetLine((size_t)id
);
3723 line
->SetItem( item
.m_col
, item
);
3726 if ( InReportView() )
3728 // just refresh the line to show the new value of the text/image
3729 RefreshLine((size_t)id
);
3733 // refresh everything (resulting in horrible flicker - FIXME!)
3738 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3740 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3741 _T("invalid list ctrl item index in SetItem") );
3743 size_t oldCurrent
= m_current
;
3744 size_t item
= (size_t)litem
; // safe because of the check above
3746 // do we need to change the focus?
3747 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3749 if ( state
& wxLIST_STATE_FOCUSED
)
3751 // don't do anything if this item is already focused
3752 if ( item
!= m_current
)
3754 ChangeCurrent(item
);
3756 if ( oldCurrent
!= (size_t)-1 )
3758 if ( IsSingleSel() )
3760 HighlightLine(oldCurrent
, FALSE
);
3763 RefreshLine(oldCurrent
);
3766 RefreshLine( m_current
);
3771 // don't do anything if this item is not focused
3772 if ( item
== m_current
)
3776 if ( IsSingleSel() )
3778 // we must unselect the old current item as well or we
3779 // might end up with more than one selected item in a
3780 // single selection control
3781 HighlightLine(oldCurrent
, FALSE
);
3784 RefreshLine( oldCurrent
);
3789 // do we need to change the selection state?
3790 if ( stateMask
& wxLIST_STATE_SELECTED
)
3792 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3794 if ( IsSingleSel() )
3798 // selecting the item also makes it the focused one in the
3800 if ( m_current
!= item
)
3802 ChangeCurrent(item
);
3804 if ( oldCurrent
!= (size_t)-1 )
3806 HighlightLine( oldCurrent
, FALSE
);
3807 RefreshLine( oldCurrent
);
3813 // only the current item may be selected anyhow
3814 if ( item
!= m_current
)
3819 if ( HighlightLine(item
, on
) )
3826 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3828 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3829 _T("invalid list ctrl item index in GetItemState()") );
3831 int ret
= wxLIST_STATE_DONTCARE
;
3833 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3835 if ( (size_t)item
== m_current
)
3836 ret
|= wxLIST_STATE_FOCUSED
;
3839 if ( stateMask
& wxLIST_STATE_SELECTED
)
3841 if ( IsHighlighted(item
) )
3842 ret
|= wxLIST_STATE_SELECTED
;
3848 void wxListMainWindow::GetItem( wxListItem
&item
) const
3850 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3851 _T("invalid item index in GetItem") );
3853 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3854 line
->GetItem( item
.m_col
, item
);
3857 // ----------------------------------------------------------------------------
3859 // ----------------------------------------------------------------------------
3861 size_t wxListMainWindow::GetItemCount() const
3863 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3866 void wxListMainWindow::SetItemCount(long count
)
3868 m_selStore
.SetItemCount(count
);
3869 m_countVirt
= count
;
3871 ResetVisibleLinesRange();
3873 // scrollbars must be reset
3877 int wxListMainWindow::GetSelectedItemCount() const
3879 // deal with the quick case first
3880 if ( IsSingleSel() )
3882 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3885 // virtual controls remmebers all its selections itself
3887 return m_selStore
.GetSelectedCount();
3889 // TODO: we probably should maintain the number of items selected even for
3890 // non virtual controls as enumerating all lines is really slow...
3891 size_t countSel
= 0;
3892 size_t count
= GetItemCount();
3893 for ( size_t line
= 0; line
< count
; line
++ )
3895 if ( GetLine(line
)->IsHighlighted() )
3902 // ----------------------------------------------------------------------------
3903 // item position/size
3904 // ----------------------------------------------------------------------------
3906 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3908 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3909 _T("invalid index in GetItemRect") );
3911 rect
= GetLineRect((size_t)index
);
3913 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3916 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3919 GetItemRect(item
, rect
);
3927 // ----------------------------------------------------------------------------
3928 // geometry calculation
3929 // ----------------------------------------------------------------------------
3931 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3933 wxClientDC
dc( this );
3934 dc
.SetFont( GetFont() );
3937 if ( HasFlag(wxLC_ICON
) )
3938 iconSpacing
= m_normal_spacing
;
3939 else if ( HasFlag(wxLC_SMALL_ICON
) )
3940 iconSpacing
= m_small_spacing
;
3944 // Note that we do not call GetClientSize() here but
3945 // GetSize() and substract the border size for sunken
3946 // borders manually. This is technically incorrect,
3947 // but we need to know the client area's size WITHOUT
3948 // scrollbars here. Since we don't know if there are
3949 // any scrollbars, we use GetSize() instead. Another
3950 // solution would be to call SetScrollbars() here to
3951 // remove the scrollbars and call GetClientSize() then,
3952 // but this might result in flicker and - worse - will
3953 // reset the scrollbars to 0 which is not good at all
3954 // if you resize a dialog/window, but don't want to
3955 // reset the window scrolling. RR.
3956 // Furthermore, we actually do NOT subtract the border
3957 // width as 2 pixels is just the extra space which we
3958 // need around the actual content in the window. Other-
3959 // wise the text would e.g. touch the upper border. RR.
3962 GetSize( &clientWidth
, &clientHeight
);
3964 if ( HasFlag(wxLC_REPORT
) )
3966 // all lines have the same height
3967 int lineHeight
= GetLineHeight();
3969 // scroll one line per step
3970 m_yScroll
= lineHeight
;
3972 size_t lineCount
= GetItemCount();
3973 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3975 m_linesPerPage
= clientHeight
/ lineHeight
;
3977 ResetVisibleLinesRange();
3979 SetScrollbars( m_xScroll
, m_yScroll
,
3980 GetHeaderWidth() / m_xScroll
,
3981 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3982 GetScrollPos(wxHORIZONTAL
),
3983 GetScrollPos(wxVERTICAL
),
3988 // at first we try without any scrollbar. if the items don't
3989 // fit into the window, we recalculate after subtracting an
3990 // approximated 15 pt for the horizontal scrollbar
3992 int entireWidth
= 0;
3994 for (int tries
= 0; tries
< 2; tries
++)
3996 // We start with 4 for the border around all items
4001 // Now we have decided that the items do not fit into the
4002 // client area. Unfortunately, wxWindows sometimes thinks
4003 // that it does fit and therefore NO horizontal scrollbar
4004 // is inserted. This looks ugly, so we fudge here and make
4005 // the calculated width bigger than was actually has been
4006 // calculated. This ensures that wxScrolledWindows puts
4007 // a scrollbar at the bottom of its client area.
4008 entireWidth
+= SCROLL_UNIT_X
;
4011 // Start at 2,2 so the text does not touch the border
4016 int currentlyVisibleLines
= 0;
4018 size_t count
= GetItemCount();
4019 for (size_t i
= 0; i
< count
; i
++)
4021 currentlyVisibleLines
++;
4022 wxListLineData
*line
= GetLine(i
);
4023 line
->CalculateSize( &dc
, iconSpacing
);
4024 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
); // Why clientWidth? (FIXME)
4026 wxSize sizeLine
= GetLineSize(i
);
4028 if ( maxWidth
< sizeLine
.x
)
4029 maxWidth
= sizeLine
.x
;
4032 if (currentlyVisibleLines
> m_linesPerPage
)
4033 m_linesPerPage
= currentlyVisibleLines
;
4035 // Assume that the size of the next one is the same... (FIXME)
4036 if ( y
+ sizeLine
.y
>= clientHeight
)
4038 currentlyVisibleLines
= 0;
4041 entireWidth
+= maxWidth
+6;
4045 // We have reached the last item.
4046 if ( i
== count
- 1 )
4047 entireWidth
+= maxWidth
;
4049 if ( (tries
== 0) && (entireWidth
+SCROLL_UNIT_X
> clientWidth
) )
4051 clientHeight
-= 15; // We guess the scrollbar height. (FIXME)
4053 currentlyVisibleLines
= 0;
4057 if ( i
== count
- 1 )
4058 tries
= 1; // Everything fits, no second try required.
4062 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
4063 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
4068 // FIXME: why should we call it from here?
4075 void wxListMainWindow::RefreshAll()
4080 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4081 if ( headerWin
&& headerWin
->m_dirty
)
4083 headerWin
->m_dirty
= FALSE
;
4084 headerWin
->Refresh();
4088 void wxListMainWindow::UpdateCurrent()
4090 if ( !HasCurrent() && !IsEmpty() )
4096 long wxListMainWindow::GetNextItem( long item
,
4097 int WXUNUSED(geometry
),
4101 max
= GetItemCount();
4102 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4103 _T("invalid listctrl index in GetNextItem()") );
4105 // notice that we start with the next item (or the first one if item == -1)
4106 // and this is intentional to allow writing a simple loop to iterate over
4107 // all selected items
4111 // this is not an error because the index was ok initially, just no
4122 size_t count
= GetItemCount();
4123 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4125 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4128 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4135 // ----------------------------------------------------------------------------
4137 // ----------------------------------------------------------------------------
4139 void wxListMainWindow::DeleteItem( long lindex
)
4141 size_t count
= GetItemCount();
4143 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4144 _T("invalid item index in DeleteItem") );
4146 size_t index
= (size_t)lindex
;
4148 // we don't need to adjust the index for the previous items
4149 if ( HasCurrent() && m_current
>= index
)
4151 // if the current item is being deleted, we want the next one to
4152 // become selected - unless there is no next one - so don't adjust
4153 // m_current in this case
4154 if ( m_current
!= index
|| m_current
== count
- 1 )
4160 if ( InReportView() )
4162 ResetVisibleLinesRange();
4169 m_selStore
.OnItemDelete(index
);
4173 m_lines
.RemoveAt( index
);
4176 // we need to refresh the (vert) scrollbar as the number of items changed
4179 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4181 RefreshAfter(index
);
4184 void wxListMainWindow::DeleteColumn( int col
)
4186 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4188 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4191 m_columns
.DeleteNode( node
);
4193 // invalidate it as it has to be recalculated
4197 void wxListMainWindow::DoDeleteAllItems()
4201 // nothing to do - in particular, don't send the event
4207 // to make the deletion of all items faster, we don't send the
4208 // notifications for each item deletion in this case but only one event
4209 // for all of them: this is compatible with wxMSW and documented in
4210 // DeleteAllItems() description
4212 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4213 event
.SetEventObject( GetParent() );
4214 GetParent()->GetEventHandler()->ProcessEvent( event
);
4223 if ( InReportView() )
4225 ResetVisibleLinesRange();
4231 void wxListMainWindow::DeleteAllItems()
4235 RecalculatePositions();
4238 void wxListMainWindow::DeleteEverything()
4245 // ----------------------------------------------------------------------------
4246 // scanning for an item
4247 // ----------------------------------------------------------------------------
4249 void wxListMainWindow::EnsureVisible( long index
)
4251 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4252 _T("invalid index in EnsureVisible") );
4254 // We have to call this here because the label in question might just have
4255 // been added and its position is not known yet
4258 RecalculatePositions(TRUE
/* no refresh */);
4261 MoveToItem((size_t)index
);
4264 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4271 size_t count
= GetItemCount();
4272 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4274 wxListLineData
*line
= GetLine(i
);
4275 if ( line
->GetText(0) == tmp
)
4282 long wxListMainWindow::FindItem(long start
, long data
)
4288 size_t count
= GetItemCount();
4289 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4291 wxListLineData
*line
= GetLine(i
);
4293 line
->GetItem( 0, item
);
4294 if (item
.m_data
== data
)
4301 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4303 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4305 size_t count
= GetItemCount();
4307 if ( HasFlag(wxLC_REPORT
) )
4309 size_t current
= y
/ GetLineHeight();
4310 if ( current
< count
)
4312 flags
= HitTestLine(current
, x
, y
);
4319 // TODO: optimize it too! this is less simple than for report view but
4320 // enumerating all items is still not a way to do it!!
4321 for ( size_t current
= 0; current
< count
; current
++ )
4323 flags
= HitTestLine(current
, x
, y
);
4332 // ----------------------------------------------------------------------------
4334 // ----------------------------------------------------------------------------
4336 void wxListMainWindow::InsertItem( wxListItem
&item
)
4338 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4340 size_t count
= GetItemCount();
4341 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4342 _T("invalid item index") );
4344 size_t id
= item
.m_itemId
;
4349 if ( HasFlag(wxLC_REPORT
) )
4351 else if ( HasFlag(wxLC_LIST
) )
4353 else if ( HasFlag(wxLC_ICON
) )
4355 else if ( HasFlag(wxLC_SMALL_ICON
) )
4356 mode
= wxLC_ICON
; // no typo
4359 wxFAIL_MSG( _T("unknown mode") );
4362 wxListLineData
*line
= new wxListLineData(this);
4364 line
->SetItem( 0, item
);
4366 m_lines
.Insert( line
, id
);
4369 RefreshLines(id
, GetItemCount() - 1);
4372 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4375 if ( HasFlag(wxLC_REPORT
) )
4377 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4378 item
.m_width
= GetTextLength( item
.m_text
);
4379 wxListHeaderData
*column
= new wxListHeaderData( item
);
4380 if ((col
>= 0) && (col
< (int)m_columns
.GetCount()))
4382 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4383 m_columns
.Insert( node
, column
);
4387 m_columns
.Append( column
);
4390 // invalidate it as it has to be recalculated
4395 // ----------------------------------------------------------------------------
4397 // ----------------------------------------------------------------------------
4399 wxListCtrlCompare list_ctrl_compare_func_2
;
4400 long list_ctrl_compare_data
;
4402 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4404 wxListLineData
*line1
= *arg1
;
4405 wxListLineData
*line2
= *arg2
;
4407 line1
->GetItem( 0, item
);
4408 long data1
= item
.m_data
;
4409 line2
->GetItem( 0, item
);
4410 long data2
= item
.m_data
;
4411 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4414 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4416 list_ctrl_compare_func_2
= fn
;
4417 list_ctrl_compare_data
= data
;
4418 m_lines
.Sort( list_ctrl_compare_func_1
);
4422 // ----------------------------------------------------------------------------
4424 // ----------------------------------------------------------------------------
4426 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4428 // update our idea of which lines are shown when we redraw the window the
4430 ResetVisibleLinesRange();
4433 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4434 wxScrolledWindow::OnScroll(event
);
4436 HandleOnScroll( event
);
4439 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4441 wxGenericListCtrl
* lc
= GetListCtrl();
4442 wxCHECK_RET( lc
, _T("no listctrl window?") );
4444 lc
->m_headerWin
->Refresh();
4445 lc
->m_headerWin
->Update();
4449 int wxListMainWindow::GetCountPerPage() const
4451 if ( !m_linesPerPage
)
4453 wxConstCast(this, wxListMainWindow
)->
4454 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4457 return m_linesPerPage
;
4460 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4462 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4464 if ( m_lineFrom
== (size_t)-1 )
4466 size_t count
= GetItemCount();
4469 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4471 // this may happen if SetScrollbars() hadn't been called yet
4472 if ( m_lineFrom
>= count
)
4473 m_lineFrom
= count
- 1;
4475 // we redraw one extra line but this is needed to make the redrawing
4476 // logic work when there is a fractional number of lines on screen
4477 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4478 if ( m_lineTo
>= count
)
4479 m_lineTo
= count
- 1;
4481 else // empty control
4484 m_lineTo
= (size_t)-1;
4488 wxASSERT_MSG( IsEmpty() ||
4489 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4490 _T("GetVisibleLinesRange() returns incorrect result") );
4498 // -------------------------------------------------------------------------------------
4500 // -------------------------------------------------------------------------------------
4502 #if !defined(__WIN32__)
4503 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
4506 // -------------------------------------------------------------------------------------
4507 // wxGenericListCtrl
4508 // -------------------------------------------------------------------------------------
4510 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4512 #if !defined(__WIN32__)
4513 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
4515 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
4518 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4519 EVT_SIZE(wxGenericListCtrl::OnSize
)
4520 EVT_IDLE(wxGenericListCtrl::OnIdle
)
4523 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
4525 * wxListCtrl has to be a real class or we have problems with
4526 * the run-time information.
4529 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
4532 wxGenericListCtrl::wxGenericListCtrl()
4534 m_imageListNormal
= (wxGenericImageList
*) NULL
;
4535 m_imageListSmall
= (wxGenericImageList
*) NULL
;
4536 m_imageListState
= (wxGenericImageList
*) NULL
;
4538 m_ownsImageListNormal
=
4539 m_ownsImageListSmall
=
4540 m_ownsImageListState
= FALSE
;
4542 m_mainWin
= (wxListMainWindow
*) NULL
;
4543 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4546 wxGenericListCtrl::~wxGenericListCtrl()
4548 if (m_ownsImageListNormal
)
4549 delete m_imageListNormal
;
4550 if (m_ownsImageListSmall
)
4551 delete m_imageListSmall
;
4552 if (m_ownsImageListState
)
4553 delete m_imageListState
;
4556 void wxGenericListCtrl::CreateHeaderWindow()
4558 m_headerWin
= new wxListHeaderWindow
4560 this, -1, m_mainWin
,
4562 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
4567 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4572 const wxValidator
&validator
,
4573 const wxString
&name
)
4577 m_imageListState
= (wxGenericImageList
*) NULL
;
4578 m_ownsImageListNormal
=
4579 m_ownsImageListSmall
=
4580 m_ownsImageListState
= FALSE
;
4582 m_mainWin
= (wxListMainWindow
*) NULL
;
4583 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4585 if ( !(style
& wxLC_MASK_TYPE
) )
4587 style
= style
| wxLC_LIST
;
4590 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4593 // don't create the inner window with the border
4594 style
&= ~wxSUNKEN_BORDER
;
4596 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4598 if ( HasFlag(wxLC_REPORT
) )
4600 CreateHeaderWindow();
4602 if ( HasFlag(wxLC_NO_HEADER
) )
4604 // VZ: why do we create it at all then?
4605 m_headerWin
->Show( FALSE
);
4612 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4614 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4615 _T("wxLC_VIRTUAL can't be [un]set") );
4617 long flag
= GetWindowStyle();
4621 if (style
& wxLC_MASK_TYPE
)
4622 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4623 if (style
& wxLC_MASK_ALIGN
)
4624 flag
&= ~wxLC_MASK_ALIGN
;
4625 if (style
& wxLC_MASK_SORT
)
4626 flag
&= ~wxLC_MASK_SORT
;
4638 SetWindowStyleFlag( flag
);
4641 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4645 m_mainWin
->DeleteEverything();
4647 // has the header visibility changed?
4648 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4649 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4651 if ( hasHeader
!= willHaveHeader
)
4658 // don't delete, just hide, as we can reuse it later
4659 m_headerWin
->Show(FALSE
);
4661 //else: nothing to do
4663 else // must show header
4667 CreateHeaderWindow();
4669 else // already have it, just show
4671 m_headerWin
->Show( TRUE
);
4675 ResizeReportView(willHaveHeader
);
4679 wxWindow::SetWindowStyleFlag( flag
);
4682 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4684 m_mainWin
->GetColumn( col
, item
);
4688 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4690 m_mainWin
->SetColumn( col
, item
);
4694 int wxGenericListCtrl::GetColumnWidth( int col
) const
4696 return m_mainWin
->GetColumnWidth( col
);
4699 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4701 m_mainWin
->SetColumnWidth( col
, width
);
4705 int wxGenericListCtrl::GetCountPerPage() const
4707 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4710 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4712 m_mainWin
->GetItem( info
);
4716 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4718 m_mainWin
->SetItem( info
);
4722 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4725 info
.m_text
= label
;
4726 info
.m_mask
= wxLIST_MASK_TEXT
;
4727 info
.m_itemId
= index
;
4731 info
.m_image
= imageId
;
4732 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4734 m_mainWin
->SetItem(info
);
4738 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4740 return m_mainWin
->GetItemState( item
, stateMask
);
4743 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4745 m_mainWin
->SetItemState( item
, state
, stateMask
);
4749 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4752 info
.m_image
= image
;
4753 info
.m_mask
= wxLIST_MASK_IMAGE
;
4754 info
.m_itemId
= item
;
4755 m_mainWin
->SetItem( info
);
4759 wxString
wxGenericListCtrl::GetItemText( long item
) const
4761 return m_mainWin
->GetItemText(item
);
4764 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4766 m_mainWin
->SetItemText(item
, str
);
4769 long wxGenericListCtrl::GetItemData( long item
) const
4772 info
.m_itemId
= item
;
4773 m_mainWin
->GetItem( info
);
4777 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4780 info
.m_mask
= wxLIST_MASK_DATA
;
4781 info
.m_itemId
= item
;
4783 m_mainWin
->SetItem( info
);
4787 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4789 m_mainWin
->GetItemRect( item
, rect
);
4793 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4795 m_mainWin
->GetItemPosition( item
, pos
);
4799 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4804 int wxGenericListCtrl::GetItemCount() const
4806 return m_mainWin
->GetItemCount();
4809 int wxGenericListCtrl::GetColumnCount() const
4811 return m_mainWin
->GetColumnCount();
4814 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4816 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4819 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4821 return m_mainWin
->GetItemSpacing( isSmall
);
4824 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4827 info
.m_itemId
= item
;
4828 info
.SetTextColour( col
);
4829 m_mainWin
->SetItem( info
);
4832 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4835 info
.m_itemId
= item
;
4836 m_mainWin
->GetItem( info
);
4837 return info
.GetTextColour();
4840 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4843 info
.m_itemId
= item
;
4844 info
.SetBackgroundColour( col
);
4845 m_mainWin
->SetItem( info
);
4848 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4851 info
.m_itemId
= item
;
4852 m_mainWin
->GetItem( info
);
4853 return info
.GetBackgroundColour();
4856 int wxGenericListCtrl::GetSelectedItemCount() const
4858 return m_mainWin
->GetSelectedItemCount();
4861 wxColour
wxGenericListCtrl::GetTextColour() const
4863 return GetForegroundColour();
4866 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4868 SetForegroundColour(col
);
4871 long wxGenericListCtrl::GetTopItem() const
4876 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4878 return m_mainWin
->GetNextItem( item
, geom
, state
);
4881 wxGenericImageList
*wxGenericListCtrl::GetImageList(int which
) const
4883 if (which
== wxIMAGE_LIST_NORMAL
)
4885 return m_imageListNormal
;
4887 else if (which
== wxIMAGE_LIST_SMALL
)
4889 return m_imageListSmall
;
4891 else if (which
== wxIMAGE_LIST_STATE
)
4893 return m_imageListState
;
4895 return (wxGenericImageList
*) NULL
;
4898 void wxGenericListCtrl::SetImageList( wxGenericImageList
*imageList
, int which
)
4900 if ( which
== wxIMAGE_LIST_NORMAL
)
4902 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4903 m_imageListNormal
= imageList
;
4904 m_ownsImageListNormal
= FALSE
;
4906 else if ( which
== wxIMAGE_LIST_SMALL
)
4908 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4909 m_imageListSmall
= imageList
;
4910 m_ownsImageListSmall
= FALSE
;
4912 else if ( which
== wxIMAGE_LIST_STATE
)
4914 if (m_ownsImageListState
) delete m_imageListState
;
4915 m_imageListState
= imageList
;
4916 m_ownsImageListState
= FALSE
;
4919 m_mainWin
->SetImageList( imageList
, which
);
4922 void wxGenericListCtrl::AssignImageList(wxGenericImageList
*imageList
, int which
)
4924 SetImageList(imageList
, which
);
4925 if ( which
== wxIMAGE_LIST_NORMAL
)
4926 m_ownsImageListNormal
= TRUE
;
4927 else if ( which
== wxIMAGE_LIST_SMALL
)
4928 m_ownsImageListSmall
= TRUE
;
4929 else if ( which
== wxIMAGE_LIST_STATE
)
4930 m_ownsImageListState
= TRUE
;
4933 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4938 bool wxGenericListCtrl::DeleteItem( long item
)
4940 m_mainWin
->DeleteItem( item
);
4944 bool wxGenericListCtrl::DeleteAllItems()
4946 m_mainWin
->DeleteAllItems();
4950 bool wxGenericListCtrl::DeleteAllColumns()
4952 size_t count
= m_mainWin
->m_columns
.GetCount();
4953 for ( size_t n
= 0; n
< count
; n
++ )
4959 void wxGenericListCtrl::ClearAll()
4961 m_mainWin
->DeleteEverything();
4964 bool wxGenericListCtrl::DeleteColumn( int col
)
4966 m_mainWin
->DeleteColumn( col
);
4968 // if we don't have the header any longer, we need to relayout the window
4969 if ( !GetColumnCount() )
4971 ResizeReportView(FALSE
/* no header */);
4977 void wxGenericListCtrl::Edit( long item
)
4979 m_mainWin
->EditLabel( item
);
4982 bool wxGenericListCtrl::EnsureVisible( long item
)
4984 m_mainWin
->EnsureVisible( item
);
4988 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4990 return m_mainWin
->FindItem( start
, str
, partial
);
4993 long wxGenericListCtrl::FindItem( long start
, long data
)
4995 return m_mainWin
->FindItem( start
, data
);
4998 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4999 int WXUNUSED(direction
))
5004 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
5006 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5009 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5011 m_mainWin
->InsertItem( info
);
5012 return info
.m_itemId
;
5015 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5018 info
.m_text
= label
;
5019 info
.m_mask
= wxLIST_MASK_TEXT
;
5020 info
.m_itemId
= index
;
5021 return InsertItem( info
);
5024 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5027 info
.m_mask
= wxLIST_MASK_IMAGE
;
5028 info
.m_image
= imageIndex
;
5029 info
.m_itemId
= index
;
5030 return InsertItem( info
);
5033 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5036 info
.m_text
= label
;
5037 info
.m_image
= imageIndex
;
5038 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5039 info
.m_itemId
= index
;
5040 return InsertItem( info
);
5043 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5045 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5047 m_mainWin
->InsertColumn( col
, item
);
5049 // if we hadn't had header before and have it now we need to relayout the
5051 if ( GetColumnCount() == 1 )
5053 ResizeReportView(TRUE
/* have header */);
5056 m_headerWin
->Refresh();
5061 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5062 int format
, int width
)
5065 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5066 item
.m_text
= heading
;
5069 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5070 item
.m_width
= width
;
5072 item
.m_format
= format
;
5074 return InsertColumn( col
, item
);
5077 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5083 // fn is a function which takes 3 long arguments: item1, item2, data.
5084 // item1 is the long data associated with a first item (NOT the index).
5085 // item2 is the long data associated with a second item (NOT the index).
5086 // data is the same value as passed to SortItems.
5087 // The return value is a negative number if the first item should precede the second
5088 // item, a positive number of the second item should precede the first,
5089 // or zero if the two items are equivalent.
5090 // data is arbitrary data to be passed to the sort function.
5092 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5094 m_mainWin
->SortItems( fn
, data
);
5098 // ----------------------------------------------------------------------------
5100 // ----------------------------------------------------------------------------
5102 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5107 ResizeReportView(m_mainWin
->HasHeader());
5109 m_mainWin
->RecalculatePositions();
5112 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5115 GetClientSize( &cw
, &ch
);
5119 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
5120 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
5122 else // no header window
5124 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5128 void wxGenericListCtrl::OnIdle( wxIdleEvent
& event
)
5132 // do it only if needed
5133 if ( !m_mainWin
->m_dirty
)
5136 m_mainWin
->RecalculatePositions();
5139 // ----------------------------------------------------------------------------
5141 // ----------------------------------------------------------------------------
5143 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5147 m_mainWin
->SetBackgroundColour( colour
);
5148 m_mainWin
->m_dirty
= TRUE
;
5154 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5156 if ( !wxWindow::SetForegroundColour( colour
) )
5161 m_mainWin
->SetForegroundColour( colour
);
5162 m_mainWin
->m_dirty
= TRUE
;
5167 m_headerWin
->SetForegroundColour( colour
);
5173 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5175 if ( !wxWindow::SetFont( font
) )
5180 m_mainWin
->SetFont( font
);
5181 m_mainWin
->m_dirty
= TRUE
;
5186 m_headerWin
->SetFont( font
);
5192 // ----------------------------------------------------------------------------
5193 // methods forwarded to m_mainWin
5194 // ----------------------------------------------------------------------------
5196 #if wxUSE_DRAG_AND_DROP
5198 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5200 m_mainWin
->SetDropTarget( dropTarget
);
5203 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5205 return m_mainWin
->GetDropTarget();
5208 #endif // wxUSE_DRAG_AND_DROP
5210 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5212 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5215 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5217 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5220 wxColour
wxGenericListCtrl::GetForegroundColour() const
5222 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5225 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5228 return m_mainWin
->PopupMenu( menu
, x
, y
);
5231 #endif // wxUSE_MENUS
5234 void wxGenericListCtrl::SetFocus()
5236 /* The test in window.cpp fails as we are a composite
5237 window, so it checks against "this", but not m_mainWin. */
5238 if ( FindFocus() != this )
5239 m_mainWin
->SetFocus();
5242 // ----------------------------------------------------------------------------
5243 // virtual list control support
5244 // ----------------------------------------------------------------------------
5246 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5248 // this is a pure virtual function, in fact - which is not really pure
5249 // because the controls which are not virtual don't need to implement it
5250 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5252 return wxEmptyString
;
5255 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5258 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5263 wxListItemAttr
*wxGenericListCtrl::OnGetItemAttr(long item
) const
5265 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5266 _T("invalid item index in OnGetItemAttr()") );
5268 // no attributes by default
5272 void wxGenericListCtrl::SetItemCount(long count
)
5274 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5276 m_mainWin
->SetItemCount(count
);
5279 void wxGenericListCtrl::RefreshItem(long item
)
5281 m_mainWin
->RefreshLine(item
);
5284 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5286 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5289 void wxGenericListCtrl::Freeze()
5291 m_mainWin
->Freeze();
5294 void wxGenericListCtrl::Thaw()
5299 #endif // wxUSE_LISTCTRL