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 (with wxListView declaration)
51 // only when wxGenericListCtrl is the only
52 // implementation, and therefore wxListView needs
53 // to be derived from the 'generic' version.
55 #if defined(__WIN32__) && !defined(__WXUNIVERSAL__)
56 #include "wx/generic/listctrl.h"
58 #include "wx/listctrl.h"
61 #if defined(__WXGTK__)
63 #include "wx/gtk/win_gtk.h"
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
71 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
72 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
73 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
74 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
75 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
76 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
77 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
78 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
79 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
80 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
81 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 // the height of the header window (FIXME: should depend on its font!)
98 static const int HEADER_HEIGHT
= 23;
100 // the scrollbar units
101 static const int SCROLL_UNIT_X
= 15;
102 static const int SCROLL_UNIT_Y
= 15;
104 // the spacing between the lines (in report mode)
105 static const int LINE_SPACING
= 0;
107 // extra margins around the text label
108 static const int EXTRA_WIDTH
= 3;
109 static const int EXTRA_HEIGHT
= 4;
111 // offset for the header window
112 static const int HEADER_OFFSET_X
= 1;
113 static const int HEADER_OFFSET_Y
= 1;
115 // when autosizing the columns, add some slack
116 static const int AUTOSIZE_COL_MARGIN
= 10;
118 // default and minimal widths for the header columns
119 static const int WIDTH_COL_DEFAULT
= 80;
120 static const int WIDTH_COL_MIN
= 10;
122 // the space between the image and the text in the report mode
123 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
125 // ============================================================================
127 // ============================================================================
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 int CMPFUNC_CONV
wxSizeTCmpFn(size_t n1
, size_t n2
) { return n1
- n2
; }
135 WX_DEFINE_SORTED_EXPORTED_ARRAY_LONG(size_t, wxIndexArray
);
137 // this class is used to store the selected items in the virtual list control
138 // (but it is not tied to list control and so can be used with other controls
139 // such as wxListBox in wxUniv)
141 // the idea is to make it really smart later (i.e. store the selections as an
142 // array of ranes + individual items) but, as I don't have time to do it now
143 // (this would require writing code to merge/break ranges and much more) keep
144 // it simple but define a clean interface to it which allows it to be made
146 class WXDLLEXPORT wxSelectionStore
149 wxSelectionStore() : m_itemsSel(wxSizeTCmpFn
) { Init(); }
151 // set the total number of items we handle
152 void SetItemCount(size_t count
) { m_count
= count
; }
154 // special case of SetItemCount(0)
155 void Clear() { m_itemsSel
.Clear(); m_count
= 0; m_defaultState
= FALSE
; }
157 // must be called when a new item is inserted/added
158 void OnItemAdd(size_t item
) { wxFAIL_MSG( _T("TODO") ); }
160 // must be called when an item is deleted
161 void OnItemDelete(size_t item
);
163 // select one item, use SelectRange() insted if possible!
165 // returns true if the items selection really changed
166 bool SelectItem(size_t item
, bool select
= TRUE
);
168 // select the range of items
170 // return true and fill the itemsChanged array with the indices of items
171 // which have changed state if "few" of them did, otherwise return false
172 // (meaning that too many items changed state to bother counting them
174 bool SelectRange(size_t itemFrom
, size_t itemTo
,
176 wxArrayInt
*itemsChanged
= NULL
);
178 // return true if the given item is selected
179 bool IsSelected(size_t item
) const;
181 // return the total number of selected items
182 size_t GetSelectedCount() const
184 return m_defaultState
? m_count
- m_itemsSel
.GetCount()
185 : m_itemsSel
.GetCount();
190 void Init() { m_defaultState
= FALSE
; }
192 // the total number of items we handle
195 // the default state: normally, FALSE (i.e. off) but maybe set to TRUE if
196 // there are more selected items than non selected ones - this allows to
197 // handle selection of all items efficiently
200 // the array of items whose selection state is different from default
201 wxIndexArray m_itemsSel
;
203 DECLARE_NO_COPY_CLASS(wxSelectionStore
)
206 //-----------------------------------------------------------------------------
207 // wxListItemData (internal)
208 //-----------------------------------------------------------------------------
210 class WXDLLEXPORT wxListItemData
213 wxListItemData(wxListMainWindow
*owner
);
216 void SetItem( const wxListItem
&info
);
217 void SetImage( int image
) { m_image
= image
; }
218 void SetData( long data
) { m_data
= data
; }
219 void SetPosition( int x
, int y
);
220 void SetSize( int width
, int height
);
222 bool HasText() const { return !m_text
.empty(); }
223 const wxString
& GetText() const { return m_text
; }
224 void SetText(const wxString
& text
) { m_text
= text
; }
226 // we can't use empty string for measuring the string width/height, so
227 // always return something
228 wxString
GetTextForMeasuring() const
230 wxString s
= GetText();
237 bool IsHit( int x
, int y
) const;
241 int GetWidth() const;
242 int GetHeight() const;
244 int GetImage() const { return m_image
; }
245 bool HasImage() const { return GetImage() != -1; }
247 void GetItem( wxListItem
&info
) const;
249 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
250 wxListItemAttr
*GetAttr() const { return m_attr
; }
253 // the item image or -1
256 // user data associated with the item
259 // the item coordinates are not used in report mode, instead this pointer
260 // is NULL and the owner window is used to retrieve the item position and
264 // the list ctrl we are in
265 wxListMainWindow
*m_owner
;
267 // custom attributes or NULL
268 wxListItemAttr
*m_attr
;
271 // common part of all ctors
277 //-----------------------------------------------------------------------------
278 // wxListHeaderData (internal)
279 //-----------------------------------------------------------------------------
281 class WXDLLEXPORT wxListHeaderData
: public wxObject
285 wxListHeaderData( const wxListItem
&info
);
286 void SetItem( const wxListItem
&item
);
287 void SetPosition( int x
, int y
);
288 void SetWidth( int w
);
289 void SetFormat( int format
);
290 void SetHeight( int h
);
291 bool HasImage() const;
293 bool HasText() const { return !m_text
.empty(); }
294 const wxString
& GetText() const { return m_text
; }
295 void SetText(const wxString
& text
) { m_text
= text
; }
297 void GetItem( wxListItem
&item
);
299 bool IsHit( int x
, int y
) const;
300 int GetImage() const;
301 int GetWidth() const;
302 int GetFormat() const;
318 //-----------------------------------------------------------------------------
319 // wxListLineData (internal)
320 //-----------------------------------------------------------------------------
322 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
323 #include "wx/listimpl.cpp"
324 WX_DEFINE_LIST(wxListItemDataList
);
326 class WXDLLEXPORT wxListLineData
329 // the list of subitems: only may have more than one item in report mode
330 wxListItemDataList m_items
;
332 // this is not used in report view
344 // the part to be highlighted
345 wxRect m_rectHighlight
;
348 // is this item selected? [NB: not used in virtual mode]
351 // back pointer to the list ctrl
352 wxListMainWindow
*m_owner
;
355 wxListLineData(wxListMainWindow
*owner
);
357 ~wxListLineData() { delete m_gi
; }
359 // are we in report mode?
360 inline bool InReportView() const;
362 // are we in virtual report mode?
363 inline bool IsVirtual() const;
365 // these 2 methods shouldn't be called for report view controls, in that
366 // case we determine our position/size ourselves
368 // calculate the size of the line
369 void CalculateSize( wxDC
*dc
, int spacing
);
371 // remember the position this line appears at
372 void SetPosition( int x
, int y
, int window_width
, int spacing
);
376 void SetImage( int image
) { SetImage(0, image
); }
377 int GetImage() const { return GetImage(0); }
378 bool HasImage() const { return GetImage() != -1; }
379 bool HasText() const { return !GetText(0).empty(); }
381 void SetItem( int index
, const wxListItem
&info
);
382 void GetItem( int index
, wxListItem
&info
);
384 wxString
GetText(int index
) const;
385 void SetText( int index
, const wxString s
);
387 wxListItemAttr
*GetAttr() const;
388 void SetAttr(wxListItemAttr
*attr
);
390 // return true if the highlighting really changed
391 bool Highlight( bool on
);
393 void ReverseHighlight();
395 bool IsHighlighted() const
397 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
399 return m_highlighted
;
402 // draw the line on the given DC in icon/list mode
403 void Draw( wxDC
*dc
);
405 // the same in report mode
406 void DrawInReportMode( wxDC
*dc
,
408 const wxRect
& rectHL
,
412 // set the line to contain num items (only can be > 1 in report mode)
413 void InitItems( int num
);
415 // get the mode (i.e. style) of the list control
416 inline int GetMode() const;
418 // prepare the DC for drawing with these item's attributes, return true if
419 // we need to draw the items background to highlight it, false otherwise
420 bool SetAttributes(wxDC
*dc
,
421 const wxListItemAttr
*attr
,
424 // these are only used by GetImage/SetImage above, we don't support images
425 // with subitems at the public API level yet
426 void SetImage( int index
, int image
);
427 int GetImage( int index
) const;
430 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
431 #include "wx/arrimpl.cpp"
432 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
434 //-----------------------------------------------------------------------------
435 // wxListHeaderWindow (internal)
436 //-----------------------------------------------------------------------------
438 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
441 wxListMainWindow
*m_owner
;
442 wxCursor
*m_currentCursor
;
443 wxCursor
*m_resizeCursor
;
446 // column being resized or -1
449 // divider line position in logical (unscrolled) coords
452 // minimal position beyond which the divider line can't be dragged in
457 wxListHeaderWindow();
459 wxListHeaderWindow( wxWindow
*win
,
461 wxListMainWindow
*owner
,
462 const wxPoint
&pos
= wxDefaultPosition
,
463 const wxSize
&size
= wxDefaultSize
,
465 const wxString
&name
= "wxlistctrlcolumntitles" );
467 virtual ~wxListHeaderWindow();
469 void DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
);
471 void AdjustDC(wxDC
& dc
);
473 void OnPaint( wxPaintEvent
&event
);
474 void OnMouse( wxMouseEvent
&event
);
475 void OnSetFocus( wxFocusEvent
&event
);
481 // common part of all ctors
484 void SendListEvent(wxEventType type
, wxPoint pos
);
486 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
487 DECLARE_EVENT_TABLE()
490 //-----------------------------------------------------------------------------
491 // wxListRenameTimer (internal)
492 //-----------------------------------------------------------------------------
494 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
497 wxListMainWindow
*m_owner
;
500 wxListRenameTimer( wxListMainWindow
*owner
);
504 //-----------------------------------------------------------------------------
505 // wxListTextCtrl (internal)
506 //-----------------------------------------------------------------------------
508 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
511 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
514 void OnChar( wxKeyEvent
&event
);
515 void OnKeyUp( wxKeyEvent
&event
);
516 void OnKillFocus( wxFocusEvent
&event
);
518 bool AcceptChanges();
522 wxListMainWindow
*m_owner
;
523 wxString m_startValue
;
527 DECLARE_EVENT_TABLE()
530 //-----------------------------------------------------------------------------
531 // wxListMainWindow (internal)
532 //-----------------------------------------------------------------------------
534 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
535 #include "wx/listimpl.cpp"
536 WX_DEFINE_LIST(wxListHeaderDataList
);
538 class WXDLLEXPORT wxListMainWindow
: public wxScrolledWindow
542 wxListMainWindow( wxWindow
*parent
,
544 const wxPoint
& pos
= wxDefaultPosition
,
545 const wxSize
& size
= wxDefaultSize
,
547 const wxString
&name
= _T("listctrlmainwindow") );
549 virtual ~wxListMainWindow();
551 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
553 // return true if this is a virtual list control
554 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
556 // return true if the control is in report mode
557 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
559 // return true if we are in single selection mode, false if multi sel
560 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
562 // do we have a header window?
563 bool HasHeader() const
564 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
566 void HighlightAll( bool on
);
568 // all these functions only do something if the line is currently visible
570 // change the line "selected" state, return TRUE if it really changed
571 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
573 // as HighlightLine() but do it for the range of lines: this is incredibly
574 // more efficient for virtual list controls!
576 // NB: unlike HighlightLine() this one does refresh the lines on screen
577 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
579 // toggle the line state and refresh it
580 void ReverseHighlight( size_t line
)
581 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
583 // return true if the line is highlighted
584 bool IsHighlighted(size_t line
) const;
586 // refresh one or several lines at once
587 void RefreshLine( size_t line
);
588 void RefreshLines( size_t lineFrom
, size_t lineTo
);
590 // refresh all selected items
591 void RefreshSelected();
593 // refresh all lines below the given one: the difference with
594 // RefreshLines() is that the index here might not be a valid one (happens
595 // when the last line is deleted)
596 void RefreshAfter( size_t lineFrom
);
598 // the methods which are forwarded to wxListLineData itself in list/icon
599 // modes but are here because the lines don't store their positions in the
602 // get the bound rect for the entire line
603 wxRect
GetLineRect(size_t line
) const;
605 // get the bound rect of the label
606 wxRect
GetLineLabelRect(size_t line
) const;
608 // get the bound rect of the items icon (only may be called if we do have
610 wxRect
GetLineIconRect(size_t line
) const;
612 // get the rect to be highlighted when the item has focus
613 wxRect
GetLineHighlightRect(size_t line
) const;
615 // get the size of the total line rect
616 wxSize
GetLineSize(size_t line
) const
617 { return GetLineRect(line
).GetSize(); }
619 // return the hit code for the corresponding position (in this line)
620 long HitTestLine(size_t line
, int x
, int y
) const;
622 // bring the selected item into view, scrolling to it if necessary
623 void MoveToItem(size_t item
);
625 // bring the current item into view
626 void MoveToFocus() { MoveToItem(m_current
); }
628 // start editing the label of the given item
629 void EditLabel( long item
);
631 // suspend/resume redrawing the control
637 void OnRenameTimer();
638 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
640 void OnMouse( wxMouseEvent
&event
);
642 // called to switch the selection from the current item to newCurrent,
643 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
645 void OnChar( wxKeyEvent
&event
);
646 void OnKeyDown( wxKeyEvent
&event
);
647 void OnSetFocus( wxFocusEvent
&event
);
648 void OnKillFocus( wxFocusEvent
&event
);
649 void OnScroll(wxScrollWinEvent
& event
) ;
651 void OnPaint( wxPaintEvent
&event
);
653 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
654 void GetImageSize( int index
, int &width
, int &height
) const;
655 int GetTextLength( const wxString
&s
) const;
657 void SetImageList( wxImageListType
*imageList
, int which
);
658 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
659 int GetItemSpacing( bool isSmall
= FALSE
);
661 void SetColumn( int col
, wxListItem
&item
);
662 void SetColumnWidth( int col
, int width
);
663 void GetColumn( int col
, wxListItem
&item
) const;
664 int GetColumnWidth( int col
) const;
665 int GetColumnCount() const { return m_columns
.GetCount(); }
667 // returns the sum of the heights of all columns
668 int GetHeaderWidth() const;
670 int GetCountPerPage() const;
672 void SetItem( wxListItem
&item
);
673 void GetItem( wxListItem
&item
) const;
674 void SetItemState( long item
, long state
, long stateMask
);
675 int GetItemState( long item
, long stateMask
) const;
676 void GetItemRect( long index
, wxRect
&rect
) const;
677 bool GetItemPosition( long item
, wxPoint
& pos
) const;
678 int GetSelectedItemCount() const;
680 wxString
GetItemText(long item
) const
683 info
.m_itemId
= item
;
688 void SetItemText(long item
, const wxString
& value
)
691 info
.m_mask
= wxLIST_MASK_TEXT
;
692 info
.m_itemId
= item
;
697 // set the scrollbars and update the positions of the items
698 void RecalculatePositions(bool noRefresh
= FALSE
);
700 // refresh the window and the header
703 long GetNextItem( long item
, int geometry
, int state
) const;
704 void DeleteItem( long index
);
705 void DeleteAllItems();
706 void DeleteColumn( int col
);
707 void DeleteEverything();
708 void EnsureVisible( long index
);
709 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
710 long FindItem( long start
, long data
);
711 long HitTest( int x
, int y
, int &flags
);
712 void InsertItem( wxListItem
&item
);
713 void InsertColumn( long col
, wxListItem
&item
);
714 void SortItems( wxListCtrlCompare fn
, long data
);
716 size_t GetItemCount() const;
717 bool IsEmpty() const { return GetItemCount() == 0; }
718 void SetItemCount(long count
);
720 // change the current (== focused) item, send a notification event
721 void ChangeCurrent(size_t current
);
722 void ResetCurrent() { ChangeCurrent((size_t)-1); }
723 bool HasCurrent() const { return m_current
!= (size_t)-1; }
725 // send out a wxListEvent
726 void SendNotify( size_t line
,
728 wxPoint point
= wxDefaultPosition
);
730 // override base class virtual to reset m_lineHeight when the font changes
731 virtual bool SetFont(const wxFont
& font
)
733 if ( !wxScrolledWindow::SetFont(font
) )
741 // these are for wxListLineData usage only
743 // get the backpointer to the list ctrl
744 wxGenericListCtrl
*GetListCtrl() const
746 return wxStaticCast(GetParent(), wxGenericListCtrl
);
749 // get the height of all lines (assuming they all do have the same height)
750 wxCoord
GetLineHeight() const;
752 // get the y position of the given line (only for report view)
753 wxCoord
GetLineY(size_t line
) const;
755 // get the brush to use for the item highlighting
756 wxBrush
*GetHighlightBrush() const
758 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
762 // the array of all line objects for a non virtual list control (for the
763 // virtual list control we only ever use m_lines[0])
764 wxListLineDataArray m_lines
;
766 // the list of column objects
767 wxListHeaderDataList m_columns
;
769 // currently focused item or -1
772 // the number of lines per page
775 // this flag is set when something which should result in the window
776 // redrawing happens (i.e. an item was added or deleted, or its appearance
777 // changed) and OnPaint() doesn't redraw the window while it is set which
778 // allows to minimize the number of repaintings when a lot of items are
779 // being added. The real repainting occurs only after the next OnIdle()
783 wxColour
*m_highlightColour
;
786 wxImageListType
*m_small_image_list
;
787 wxImageListType
*m_normal_image_list
;
789 int m_normal_spacing
;
793 wxTimer
*m_renameTimer
;
798 // for double click logic
799 size_t m_lineLastClicked
,
800 m_lineBeforeLastClicked
;
803 // the total count of items in a virtual list control
806 // the object maintaining the items selection state, only used in virtual
808 wxSelectionStore m_selStore
;
810 // common part of all ctors
813 // intiialize m_[xy]Scroll
814 void InitScrolling();
816 // get the line data for the given index
817 wxListLineData
*GetLine(size_t n
) const
819 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
823 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
831 // get a dummy line which can be used for geometry calculations and such:
832 // you must use GetLine() if you want to really draw the line
833 wxListLineData
*GetDummyLine() const;
835 // cache the line data of the n-th line in m_lines[0]
836 void CacheLineData(size_t line
);
838 // get the range of visible lines
839 void GetVisibleLinesRange(size_t *from
, size_t *to
);
841 // force us to recalculate the range of visible lines
842 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
844 // get the colour to be used for drawing the rules
845 wxColour
GetRuleColour() const
850 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
855 // initialize the current item if needed
856 void UpdateCurrent();
858 // delete all items but don't refresh: called from dtor
859 void DoDeleteAllItems();
861 // the height of one line using the current font
862 wxCoord m_lineHeight
;
864 // the total header width or 0 if not calculated yet
865 wxCoord m_headerWidth
;
867 // the first and last lines being shown on screen right now (inclusive),
868 // both may be -1 if they must be calculated so never access them directly:
869 // use GetVisibleLinesRange() above instead
873 // the brushes to use for item highlighting when we do/don't have focus
874 wxBrush
*m_highlightBrush
,
875 *m_highlightUnfocusedBrush
;
877 // if this is > 0, the control is frozen and doesn't redraw itself
878 size_t m_freezeCount
;
880 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
881 DECLARE_EVENT_TABLE()
884 // ============================================================================
886 // ============================================================================
888 // ----------------------------------------------------------------------------
890 // ----------------------------------------------------------------------------
892 bool wxSelectionStore::IsSelected(size_t item
) const
894 bool isSel
= m_itemsSel
.Index(item
) != wxNOT_FOUND
;
896 // if the default state is to be selected, being in m_itemsSel means that
897 // the item is not selected, so we have to inverse the logic
898 return m_defaultState
? !isSel
: isSel
;
901 bool wxSelectionStore::SelectItem(size_t item
, bool select
)
903 // search for the item ourselves as like this we get the index where to
904 // insert it later if needed, so we do only one search in the array instead
905 // of two (adding item to a sorted array requires a search)
906 size_t index
= m_itemsSel
.IndexForInsert(item
);
907 bool isSel
= index
< m_itemsSel
.GetCount() && m_itemsSel
[index
] == item
;
909 if ( select
!= m_defaultState
)
913 m_itemsSel
.AddAt(item
, index
);
918 else // reset to default state
922 m_itemsSel
.RemoveAt(index
);
930 bool wxSelectionStore::SelectRange(size_t itemFrom
, size_t itemTo
,
932 wxArrayInt
*itemsChanged
)
934 // 100 is hardcoded but it shouldn't matter much: the important thing is
935 // that we don't refresh everything when really few (e.g. 1 or 2) items
937 static const size_t MANY_ITEMS
= 100;
939 wxASSERT_MSG( itemFrom
<= itemTo
, _T("should be in order") );
941 // are we going to have more [un]selected items than the other ones?
942 if ( itemTo
- itemFrom
> m_count
/2 )
944 if ( select
!= m_defaultState
)
946 // the default state now becomes the same as 'select'
947 m_defaultState
= select
;
949 // so all the old selections (which had state select) shouldn't be
950 // selected any more, but all the other ones should
951 wxIndexArray selOld
= m_itemsSel
;
954 // TODO: it should be possible to optimize the searches a bit
955 // knowing the possible range
958 for ( item
= 0; item
< itemFrom
; item
++ )
960 if ( selOld
.Index(item
) == wxNOT_FOUND
)
961 m_itemsSel
.Add(item
);
964 for ( item
= itemTo
+ 1; item
< m_count
; item
++ )
966 if ( selOld
.Index(item
) == wxNOT_FOUND
)
967 m_itemsSel
.Add(item
);
970 // many items (> half) changed state
973 else // select == m_defaultState
975 // get the inclusive range of items between itemFrom and itemTo
976 size_t count
= m_itemsSel
.GetCount(),
977 start
= m_itemsSel
.IndexForInsert(itemFrom
),
978 end
= m_itemsSel
.IndexForInsert(itemTo
);
980 if ( start
== count
|| m_itemsSel
[start
] < itemFrom
)
985 if ( end
== count
|| m_itemsSel
[end
] > itemTo
)
992 // delete all of them (from end to avoid changing indices)
993 for ( int i
= end
; i
>= (int)start
; i
-- )
997 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
999 // stop counting (see comment below)
1000 itemsChanged
= NULL
;
1004 itemsChanged
->Add(m_itemsSel
[i
]);
1008 m_itemsSel
.RemoveAt(i
);
1013 else // "few" items change state
1017 itemsChanged
->Empty();
1020 // just add the items to the selection
1021 for ( size_t item
= itemFrom
; item
<= itemTo
; item
++ )
1023 if ( SelectItem(item
, select
) && itemsChanged
)
1025 itemsChanged
->Add(item
);
1027 if ( itemsChanged
->GetCount() > MANY_ITEMS
)
1029 // stop counting them, we'll just eat gobs of memory
1030 // for nothing at all - faster to refresh everything in
1032 itemsChanged
= NULL
;
1038 // we set it to NULL if there are many items changing state
1039 return itemsChanged
!= NULL
;
1042 void wxSelectionStore::OnItemDelete(size_t item
)
1044 size_t count
= m_itemsSel
.GetCount(),
1045 i
= m_itemsSel
.IndexForInsert(item
);
1047 if ( i
< count
&& m_itemsSel
[i
] == item
)
1049 // this item itself was in m_itemsSel, remove it from there
1050 m_itemsSel
.RemoveAt(i
);
1055 // and adjust the index of all which follow it
1058 // all following elements must be greater than the one we deleted
1059 wxASSERT_MSG( m_itemsSel
[i
] > item
, _T("logic error") );
1065 //-----------------------------------------------------------------------------
1067 //-----------------------------------------------------------------------------
1069 wxListItemData::~wxListItemData()
1071 // in the virtual list control the attributes are managed by the main
1072 // program, so don't delete them
1073 if ( !m_owner
->IsVirtual() )
1081 void wxListItemData::Init()
1089 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
1095 if ( owner
->InReportView() )
1101 m_rect
= new wxRect
;
1105 void wxListItemData::SetItem( const wxListItem
&info
)
1107 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
1108 SetText(info
.m_text
);
1109 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
1110 m_image
= info
.m_image
;
1111 if ( info
.m_mask
& wxLIST_MASK_DATA
)
1112 m_data
= info
.m_data
;
1114 if ( info
.HasAttributes() )
1117 *m_attr
= *info
.GetAttributes();
1119 m_attr
= new wxListItemAttr(*info
.GetAttributes());
1127 m_rect
->width
= info
.m_width
;
1131 void wxListItemData::SetPosition( int x
, int y
)
1133 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
1139 void wxListItemData::SetSize( int width
, int height
)
1141 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
1144 m_rect
->width
= width
;
1146 m_rect
->height
= height
;
1149 bool wxListItemData::IsHit( int x
, int y
) const
1151 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
1153 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
1156 int wxListItemData::GetX() const
1158 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1163 int wxListItemData::GetY() const
1165 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1170 int wxListItemData::GetWidth() const
1172 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1174 return m_rect
->width
;
1177 int wxListItemData::GetHeight() const
1179 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
1181 return m_rect
->height
;
1184 void wxListItemData::GetItem( wxListItem
&info
) const
1186 info
.m_text
= m_text
;
1187 info
.m_image
= m_image
;
1188 info
.m_data
= m_data
;
1192 if ( m_attr
->HasTextColour() )
1193 info
.SetTextColour(m_attr
->GetTextColour());
1194 if ( m_attr
->HasBackgroundColour() )
1195 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
1196 if ( m_attr
->HasFont() )
1197 info
.SetFont(m_attr
->GetFont());
1201 //-----------------------------------------------------------------------------
1203 //-----------------------------------------------------------------------------
1205 void wxListHeaderData::Init()
1216 wxListHeaderData::wxListHeaderData()
1221 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1228 void wxListHeaderData::SetItem( const wxListItem
&item
)
1230 m_mask
= item
.m_mask
;
1232 if ( m_mask
& wxLIST_MASK_TEXT
)
1233 m_text
= item
.m_text
;
1235 if ( m_mask
& wxLIST_MASK_IMAGE
)
1236 m_image
= item
.m_image
;
1238 if ( m_mask
& wxLIST_MASK_FORMAT
)
1239 m_format
= item
.m_format
;
1241 if ( m_mask
& wxLIST_MASK_WIDTH
)
1242 SetWidth(item
.m_width
);
1245 void wxListHeaderData::SetPosition( int x
, int y
)
1251 void wxListHeaderData::SetHeight( int h
)
1256 void wxListHeaderData::SetWidth( int w
)
1260 m_width
= WIDTH_COL_DEFAULT
;
1261 else if (m_width
< WIDTH_COL_MIN
)
1262 m_width
= WIDTH_COL_MIN
;
1265 void wxListHeaderData::SetFormat( int format
)
1270 bool wxListHeaderData::HasImage() const
1272 return m_image
!= -1;
1275 bool wxListHeaderData::IsHit( int x
, int y
) const
1277 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1280 void wxListHeaderData::GetItem( wxListItem
& item
)
1282 item
.m_mask
= m_mask
;
1283 item
.m_text
= m_text
;
1284 item
.m_image
= m_image
;
1285 item
.m_format
= m_format
;
1286 item
.m_width
= m_width
;
1289 int wxListHeaderData::GetImage() const
1294 int wxListHeaderData::GetWidth() const
1299 int wxListHeaderData::GetFormat() const
1304 //-----------------------------------------------------------------------------
1306 //-----------------------------------------------------------------------------
1308 inline int wxListLineData::GetMode() const
1310 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1313 inline bool wxListLineData::InReportView() const
1315 return m_owner
->HasFlag(wxLC_REPORT
);
1318 inline bool wxListLineData::IsVirtual() const
1320 return m_owner
->IsVirtual();
1323 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1326 m_items
.DeleteContents( TRUE
);
1328 if ( InReportView() )
1334 m_gi
= new GeometryInfo
;
1337 m_highlighted
= FALSE
;
1339 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1342 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1344 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1345 wxCHECK_RET( node
, _T("no subitems at all??") );
1347 wxListItemData
*item
= node
->GetData();
1349 switch ( GetMode() )
1352 case wxLC_SMALL_ICON
:
1354 m_gi
->m_rectAll
.width
= spacing
;
1356 wxString s
= item
->GetText();
1362 m_gi
->m_rectLabel
.width
=
1363 m_gi
->m_rectLabel
.height
= 0;
1367 dc
->GetTextExtent( s
, &lw
, &lh
);
1368 if (lh
< SCROLL_UNIT_Y
)
1373 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1375 m_gi
->m_rectAll
.width
= lw
;
1377 m_gi
->m_rectLabel
.width
= lw
;
1378 m_gi
->m_rectLabel
.height
= lh
;
1381 if (item
->HasImage())
1384 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1385 m_gi
->m_rectIcon
.width
= w
+ 8;
1386 m_gi
->m_rectIcon
.height
= h
+ 8;
1388 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1389 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1390 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1391 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1394 if ( item
->HasText() )
1396 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1397 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1399 else // no text, highlight the icon
1401 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1402 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1409 wxString s
= item
->GetTextForMeasuring();
1412 dc
->GetTextExtent( s
, &lw
, &lh
);
1413 if (lh
< SCROLL_UNIT_Y
)
1418 m_gi
->m_rectLabel
.width
= lw
;
1419 m_gi
->m_rectLabel
.height
= lh
;
1421 m_gi
->m_rectAll
.width
= lw
;
1422 m_gi
->m_rectAll
.height
= lh
;
1424 if (item
->HasImage())
1427 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1428 m_gi
->m_rectIcon
.width
= w
;
1429 m_gi
->m_rectIcon
.height
= h
;
1431 m_gi
->m_rectAll
.width
+= 4 + w
;
1432 if (h
> m_gi
->m_rectAll
.height
)
1433 m_gi
->m_rectAll
.height
= h
;
1436 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1437 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1442 wxFAIL_MSG( _T("unexpected call to SetSize") );
1446 wxFAIL_MSG( _T("unknown mode") );
1450 void wxListLineData::SetPosition( int x
, int y
,
1454 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1455 wxCHECK_RET( node
, _T("no subitems at all??") );
1457 wxListItemData
*item
= node
->GetData();
1459 switch ( GetMode() )
1462 case wxLC_SMALL_ICON
:
1463 m_gi
->m_rectAll
.x
= x
;
1464 m_gi
->m_rectAll
.y
= y
;
1466 if ( item
->HasImage() )
1468 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1469 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1470 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1473 if ( item
->HasText() )
1475 if (m_gi
->m_rectAll
.width
> spacing
)
1476 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1478 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1479 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1480 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1481 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1483 else // no text, highlight the icon
1485 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1486 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1491 m_gi
->m_rectAll
.x
= x
;
1492 m_gi
->m_rectAll
.y
= y
;
1494 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1495 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1496 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1498 if (item
->HasImage())
1500 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1501 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1502 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1506 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1511 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1515 wxFAIL_MSG( _T("unknown mode") );
1519 void wxListLineData::InitItems( int num
)
1521 for (int i
= 0; i
< num
; i
++)
1522 m_items
.Append( new wxListItemData(m_owner
) );
1525 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1527 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1528 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1530 wxListItemData
*item
= node
->GetData();
1531 item
->SetItem( info
);
1534 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1536 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1539 wxListItemData
*item
= node
->GetData();
1540 item
->GetItem( info
);
1544 wxString
wxListLineData::GetText(int index
) const
1548 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1551 wxListItemData
*item
= node
->GetData();
1552 s
= item
->GetText();
1558 void wxListLineData::SetText( int index
, const wxString s
)
1560 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1563 wxListItemData
*item
= node
->GetData();
1568 void wxListLineData::SetImage( int index
, int image
)
1570 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1571 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1573 wxListItemData
*item
= node
->GetData();
1574 item
->SetImage(image
);
1577 int wxListLineData::GetImage( int index
) const
1579 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1580 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1582 wxListItemData
*item
= node
->GetData();
1583 return item
->GetImage();
1586 wxListItemAttr
*wxListLineData::GetAttr() const
1588 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1589 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1591 wxListItemData
*item
= node
->GetData();
1592 return item
->GetAttr();
1595 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1597 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1598 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1600 wxListItemData
*item
= node
->GetData();
1601 item
->SetAttr(attr
);
1604 bool wxListLineData::SetAttributes(wxDC
*dc
,
1605 const wxListItemAttr
*attr
,
1608 wxWindow
*listctrl
= m_owner
->GetParent();
1612 // don't use foreground colour for drawing highlighted items - this might
1613 // make them completely invisible (and there is no way to do bit
1614 // arithmetics on wxColour, unfortunately)
1618 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1622 if ( attr
&& attr
->HasTextColour() )
1624 colText
= attr
->GetTextColour();
1628 colText
= listctrl
->GetForegroundColour();
1632 dc
->SetTextForeground(colText
);
1636 if ( attr
&& attr
->HasFont() )
1638 font
= attr
->GetFont();
1642 font
= listctrl
->GetFont();
1648 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1649 if ( highlighted
|| hasBgCol
)
1653 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1657 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1660 dc
->SetPen( *wxTRANSPARENT_PEN
);
1668 void wxListLineData::Draw( wxDC
*dc
)
1670 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1671 wxCHECK_RET( node
, _T("no subitems at all??") );
1673 bool highlighted
= IsHighlighted();
1675 wxListItemAttr
*attr
= GetAttr();
1677 if ( SetAttributes(dc
, attr
, highlighted
) )
1679 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1682 wxListItemData
*item
= node
->GetData();
1683 if (item
->HasImage())
1685 wxRect rectIcon
= m_gi
->m_rectIcon
;
1686 m_owner
->DrawImage( item
->GetImage(), dc
,
1687 rectIcon
.x
, rectIcon
.y
);
1690 if (item
->HasText())
1692 wxRect rectLabel
= m_gi
->m_rectLabel
;
1694 wxDCClipper
clipper(*dc
, rectLabel
);
1695 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1699 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1701 const wxRect
& rectHL
,
1704 // TODO: later we should support setting different attributes for
1705 // different columns - to do it, just add "col" argument to
1706 // GetAttr() and move these lines into the loop below
1707 wxListItemAttr
*attr
= GetAttr();
1708 if ( SetAttributes(dc
, attr
, highlighted
) )
1710 dc
->DrawRectangle( rectHL
);
1713 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1714 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1717 for ( wxListItemDataList::Node
*node
= m_items
.GetFirst();
1719 node
= node
->GetNext(), col
++ )
1721 wxListItemData
*item
= node
->GetData();
1723 int width
= m_owner
->GetColumnWidth(col
);
1727 if ( item
->HasImage() )
1730 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1731 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1733 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1739 wxDCClipper
clipper(*dc
, xOld
, y
, width
, rect
.height
);
1741 if ( item
->HasText() )
1743 dc
->DrawText( item
->GetText(), xOld
, y
);
1748 bool wxListLineData::Highlight( bool on
)
1750 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1752 if ( on
== m_highlighted
)
1760 void wxListLineData::ReverseHighlight( void )
1762 Highlight(!IsHighlighted());
1765 //-----------------------------------------------------------------------------
1766 // wxListHeaderWindow
1767 //-----------------------------------------------------------------------------
1769 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1771 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1772 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1773 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1774 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1777 void wxListHeaderWindow::Init()
1779 m_currentCursor
= (wxCursor
*) NULL
;
1780 m_isDragging
= FALSE
;
1784 wxListHeaderWindow::wxListHeaderWindow()
1788 m_owner
= (wxListMainWindow
*) NULL
;
1789 m_resizeCursor
= (wxCursor
*) NULL
;
1792 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1794 wxListMainWindow
*owner
,
1798 const wxString
&name
)
1799 : wxWindow( win
, id
, pos
, size
, style
, name
)
1804 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1806 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1809 wxListHeaderWindow::~wxListHeaderWindow()
1811 delete m_resizeCursor
;
1814 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1816 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
1817 GtkStateType state
= m_parent
->IsEnabled() ? GTK_STATE_NORMAL
1818 : GTK_STATE_INSENSITIVE
;
1820 x
= dc
->XLOG2DEV( x
);
1822 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
,
1823 state
, GTK_SHADOW_OUT
,
1824 (GdkRectangle
*) NULL
, m_wxwindow
,
1825 (char *)"button", // const_cast
1826 x
-1, y
-1, w
+2, h
+2);
1827 #elif defined( __WXMAC__ )
1828 const int m_corner
= 1;
1830 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1832 dc
->SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1833 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1834 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1836 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1839 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1840 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1842 dc
->SetPen( *wxWHITE_PEN
);
1843 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1844 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1845 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1846 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1848 const int m_corner
= 1;
1850 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1852 dc
->SetPen( *wxBLACK_PEN
);
1853 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1854 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1856 wxPen
pen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1859 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1860 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1862 dc
->SetPen( *wxWHITE_PEN
);
1863 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1864 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1865 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1866 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1870 // shift the DC origin to match the position of the main window horz
1871 // scrollbar: this allows us to always use logical coords
1872 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1875 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1878 m_owner
->GetViewStart( &x
, NULL
);
1880 // account for the horz scrollbar offset
1881 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1884 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1886 #if defined(__WXGTK__)
1887 wxClientDC
dc( this );
1889 wxPaintDC
dc( this );
1897 dc
.SetFont( GetFont() );
1899 // width and height of the entire header window
1901 GetClientSize( &w
, &h
);
1902 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1904 dc
.SetBackgroundMode(wxTRANSPARENT
);
1906 // do *not* use the listctrl colour for headers - one day we will have a
1907 // function to set it separately
1908 //dc.SetTextForeground( *wxBLACK );
1909 dc
.SetTextForeground(wxSystemSettings::
1910 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1912 int x
= HEADER_OFFSET_X
;
1914 int numColumns
= m_owner
->GetColumnCount();
1916 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1918 m_owner
->GetColumn( i
, item
);
1919 int wCol
= item
.m_width
;
1921 // the width of the rect to draw: make it smaller to fit entirely
1922 // inside the column rect
1925 dc
.SetPen( *wxWHITE_PEN
);
1927 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
1929 // if we have an image, draw it on the right of the label
1930 int image
= item
.m_image
;
1933 wxImageListType
*imageList
= m_owner
->m_small_image_list
;
1937 imageList
->GetSize(image
, ix
, iy
);
1944 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1945 wxIMAGELIST_DRAW_TRANSPARENT
1950 //else: ignore the column image
1953 // draw the text clipping it so that it doesn't overwrite the column
1955 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1957 dc
.DrawText( item
.GetText(),
1958 x
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1966 void wxListHeaderWindow::DrawCurrent()
1968 int x1
= m_currentX
;
1970 m_owner
->ClientToScreen( &x1
, &y1
);
1972 int x2
= m_currentX
;
1974 m_owner
->GetClientSize( NULL
, &y2
);
1975 m_owner
->ClientToScreen( &x2
, &y2
);
1978 dc
.SetLogicalFunction( wxINVERT
);
1979 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1980 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1984 dc
.DrawLine( x1
, y1
, x2
, y2
);
1986 dc
.SetLogicalFunction( wxCOPY
);
1988 dc
.SetPen( wxNullPen
);
1989 dc
.SetBrush( wxNullBrush
);
1992 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1994 // we want to work with logical coords
1996 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1997 int y
= event
.GetY();
2001 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
,
2002 event
.GetPosition());
2004 // we don't draw the line beyond our window, but we allow dragging it
2007 GetClientSize( &w
, NULL
);
2008 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
2011 // erase the line if it was drawn
2012 if ( m_currentX
< w
)
2015 if (event
.ButtonUp())
2018 m_isDragging
= FALSE
;
2020 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
2021 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
,
2022 event
.GetPosition());
2029 m_currentX
= m_minX
+ 7;
2031 // draw in the new location
2032 if ( m_currentX
< w
)
2036 else // not dragging
2039 bool hit_border
= FALSE
;
2041 // end of the current column
2044 // find the column where this event occured
2046 countCol
= m_owner
->GetColumnCount();
2047 for (col
= 0; col
< countCol
; col
++)
2049 xpos
+= m_owner
->GetColumnWidth( col
);
2052 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
2054 // near the column border
2061 // inside the column
2068 if ( col
== countCol
)
2071 if (event
.LeftDown() || event
.RightUp())
2073 if (hit_border
&& event
.LeftDown())
2075 m_isDragging
= TRUE
;
2079 SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
2080 event
.GetPosition());
2082 else // click on a column
2084 SendListEvent( event
.LeftDown()
2085 ? wxEVT_COMMAND_LIST_COL_CLICK
2086 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
2087 event
.GetPosition());
2090 else if (event
.Moving())
2095 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
2096 m_currentCursor
= m_resizeCursor
;
2100 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
2101 m_currentCursor
= wxSTANDARD_CURSOR
;
2105 SetCursor(*m_currentCursor
);
2110 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2112 m_owner
->SetFocus();
2115 void wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
2117 wxWindow
*parent
= GetParent();
2118 wxListEvent
le( type
, parent
->GetId() );
2119 le
.SetEventObject( parent
);
2120 le
.m_pointDrag
= pos
;
2122 // the position should be relative to the parent window, not
2123 // this one for compatibility with MSW and common sense: the
2124 // user code doesn't know anything at all about this header
2125 // window, so why should it get positions relative to it?
2126 le
.m_pointDrag
.y
-= GetSize().y
;
2128 le
.m_col
= m_column
;
2129 parent
->GetEventHandler()->ProcessEvent( le
);
2132 //-----------------------------------------------------------------------------
2133 // wxListRenameTimer (internal)
2134 //-----------------------------------------------------------------------------
2136 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2141 void wxListRenameTimer::Notify()
2143 m_owner
->OnRenameTimer();
2146 //-----------------------------------------------------------------------------
2147 // wxListTextCtrl (internal)
2148 //-----------------------------------------------------------------------------
2150 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2151 EVT_CHAR (wxListTextCtrl::OnChar
)
2152 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2153 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2156 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
2157 : m_startValue(owner
->GetItemText(itemEdit
)),
2158 m_itemEdited(itemEdit
)
2163 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2165 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2166 &rectLabel
.x
, &rectLabel
.y
);
2168 (void)Create(owner
, wxID_ANY
, m_startValue
,
2169 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2170 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2173 void wxListTextCtrl::Finish()
2177 wxPendingDelete
.Append(this);
2181 m_owner
->SetFocus();
2185 bool wxListTextCtrl::AcceptChanges()
2187 const wxString value
= GetValue();
2189 if ( value
== m_startValue
)
2191 // nothing changed, always accept
2195 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2197 // vetoed by the user
2201 // accepted, do rename the item
2202 m_owner
->SetItemText(m_itemEdited
, value
);
2207 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2209 switch ( event
.m_keyCode
)
2212 if ( !AcceptChanges() )
2214 // vetoed by the user code
2217 //else: fall through
2228 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2236 // auto-grow the textctrl:
2237 wxSize parentSize
= m_owner
->GetSize();
2238 wxPoint myPos
= GetPosition();
2239 wxSize mySize
= GetSize();
2241 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2242 if (myPos
.x
+ sx
> parentSize
.x
)
2243 sx
= parentSize
.x
- myPos
.x
;
2251 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2255 (void)AcceptChanges();
2263 //-----------------------------------------------------------------------------
2265 //-----------------------------------------------------------------------------
2267 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2269 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2270 EVT_PAINT (wxListMainWindow::OnPaint
)
2271 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2272 EVT_CHAR (wxListMainWindow::OnChar
)
2273 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2274 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2275 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2276 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2279 void wxListMainWindow::Init()
2281 m_columns
.DeleteContents( TRUE
);
2285 m_lineTo
= (size_t)-1;
2291 m_small_image_list
= (wxImageListType
*) NULL
;
2292 m_normal_image_list
= (wxImageListType
*) NULL
;
2294 m_small_spacing
= 30;
2295 m_normal_spacing
= 40;
2299 m_isCreated
= FALSE
;
2301 m_lastOnSame
= FALSE
;
2302 m_renameTimer
= new wxListRenameTimer( this );
2306 m_lineBeforeLastClicked
= (size_t)-1;
2311 void wxListMainWindow::InitScrolling()
2313 if ( HasFlag(wxLC_REPORT
) )
2315 m_xScroll
= SCROLL_UNIT_X
;
2316 m_yScroll
= SCROLL_UNIT_Y
;
2320 m_xScroll
= SCROLL_UNIT_Y
;
2325 wxListMainWindow::wxListMainWindow()
2330 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2336 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2341 const wxString
&name
)
2342 : wxScrolledWindow( parent
, id
, pos
, size
,
2343 style
| wxHSCROLL
| wxVSCROLL
, name
)
2347 m_highlightBrush
= new wxBrush
2349 wxSystemSettings::GetColour
2351 wxSYS_COLOUR_HIGHLIGHT
2356 m_highlightUnfocusedBrush
= new wxBrush
2358 wxSystemSettings::GetColour
2360 wxSYS_COLOUR_BTNSHADOW
2369 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2371 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2374 wxListMainWindow::~wxListMainWindow()
2378 delete m_highlightBrush
;
2379 delete m_highlightUnfocusedBrush
;
2381 delete m_renameTimer
;
2384 void wxListMainWindow::CacheLineData(size_t line
)
2386 wxGenericListCtrl
*listctrl
= GetListCtrl();
2388 wxListLineData
*ld
= GetDummyLine();
2390 size_t countCol
= GetColumnCount();
2391 for ( size_t col
= 0; col
< countCol
; col
++ )
2393 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2396 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2397 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2400 wxListLineData
*wxListMainWindow::GetDummyLine() const
2402 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2404 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2406 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2408 // we need to recreate the dummy line if the number of columns in the
2409 // control changed as it would have the incorrect number of fields
2411 if ( !m_lines
.IsEmpty() &&
2412 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2414 self
->m_lines
.Clear();
2417 if ( m_lines
.IsEmpty() )
2419 wxListLineData
*line
= new wxListLineData(self
);
2420 self
->m_lines
.Add(line
);
2422 // don't waste extra memory -- there never going to be anything
2423 // else/more in this array
2424 self
->m_lines
.Shrink();
2430 // ----------------------------------------------------------------------------
2431 // line geometry (report mode only)
2432 // ----------------------------------------------------------------------------
2434 wxCoord
wxListMainWindow::GetLineHeight() const
2436 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2438 // we cache the line height as calling GetTextExtent() is slow
2439 if ( !m_lineHeight
)
2441 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2443 wxClientDC
dc( self
);
2444 dc
.SetFont( GetFont() );
2447 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2449 if ( y
< SCROLL_UNIT_Y
)
2453 self
->m_lineHeight
= y
+ LINE_SPACING
;
2456 return m_lineHeight
;
2459 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2461 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2463 return LINE_SPACING
+ line
*GetLineHeight();
2466 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2468 if ( !InReportView() )
2469 return GetLine(line
)->m_gi
->m_rectAll
;
2472 rect
.x
= HEADER_OFFSET_X
;
2473 rect
.y
= GetLineY(line
);
2474 rect
.width
= GetHeaderWidth();
2475 rect
.height
= GetLineHeight();
2480 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2482 if ( !InReportView() )
2483 return GetLine(line
)->m_gi
->m_rectLabel
;
2486 rect
.x
= HEADER_OFFSET_X
;
2487 rect
.y
= GetLineY(line
);
2488 rect
.width
= GetColumnWidth(0);
2489 rect
.height
= GetLineHeight();
2494 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2496 if ( !InReportView() )
2497 return GetLine(line
)->m_gi
->m_rectIcon
;
2499 wxListLineData
*ld
= GetLine(line
);
2500 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2503 rect
.x
= HEADER_OFFSET_X
;
2504 rect
.y
= GetLineY(line
);
2505 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2510 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2512 return InReportView() ? GetLineRect(line
)
2513 : GetLine(line
)->m_gi
->m_rectHighlight
;
2516 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2518 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2520 wxListLineData
*ld
= GetLine(line
);
2522 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2523 return wxLIST_HITTEST_ONITEMICON
;
2525 // VS: Testing for "ld->HasText() || InReportView()" instead of
2526 // "ld->HasText()" is needed to make empty lines in report view
2528 if ( ld
->HasText() || InReportView() )
2530 wxRect rect
= InReportView() ? GetLineRect(line
)
2531 : GetLineLabelRect(line
);
2533 if ( rect
.Inside(x
, y
) )
2534 return wxLIST_HITTEST_ONITEMLABEL
;
2540 // ----------------------------------------------------------------------------
2541 // highlight (selection) handling
2542 // ----------------------------------------------------------------------------
2544 bool wxListMainWindow::IsHighlighted(size_t line
) const
2548 return m_selStore
.IsSelected(line
);
2552 wxListLineData
*ld
= GetLine(line
);
2553 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2555 return ld
->IsHighlighted();
2559 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2565 wxArrayInt linesChanged
;
2566 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2569 // meny items changed state, refresh everything
2570 RefreshLines(lineFrom
, lineTo
);
2572 else // only a few items changed state, refresh only them
2574 size_t count
= linesChanged
.GetCount();
2575 for ( size_t n
= 0; n
< count
; n
++ )
2577 RefreshLine(linesChanged
[n
]);
2581 else // iterate over all items in non report view
2583 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2585 if ( HighlightLine(line
, highlight
) )
2593 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2599 changed
= m_selStore
.SelectItem(line
, highlight
);
2603 wxListLineData
*ld
= GetLine(line
);
2604 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2606 changed
= ld
->Highlight(highlight
);
2611 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2612 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2618 void wxListMainWindow::RefreshLine( size_t line
)
2620 if ( HasFlag(wxLC_REPORT
) )
2622 size_t visibleFrom
, visibleTo
;
2623 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2625 if ( line
< visibleFrom
|| line
> visibleTo
)
2629 wxRect rect
= GetLineRect(line
);
2631 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2632 RefreshRect( rect
);
2635 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2637 // we suppose that they are ordered by caller
2638 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2640 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2642 if ( HasFlag(wxLC_REPORT
) )
2644 size_t visibleFrom
, visibleTo
;
2645 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2647 if ( lineFrom
< visibleFrom
)
2648 lineFrom
= visibleFrom
;
2649 if ( lineTo
> visibleTo
)
2654 rect
.y
= GetLineY(lineFrom
);
2655 rect
.width
= GetClientSize().x
;
2656 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2658 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2659 RefreshRect( rect
);
2663 // TODO: this should be optimized...
2664 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2671 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2673 if ( HasFlag(wxLC_REPORT
) )
2675 size_t visibleFrom
, visibleTo
;
2676 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2678 if ( lineFrom
< visibleFrom
)
2679 lineFrom
= visibleFrom
;
2680 else if ( lineFrom
> visibleTo
)
2685 rect
.y
= GetLineY(lineFrom
);
2686 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2688 wxSize size
= GetClientSize();
2689 rect
.width
= size
.x
;
2690 // refresh till the bottom of the window
2691 rect
.height
= size
.y
- rect
.y
;
2693 RefreshRect( rect
);
2697 // TODO: how to do it more efficiently?
2702 void wxListMainWindow::RefreshSelected()
2708 if ( InReportView() )
2710 GetVisibleLinesRange(&from
, &to
);
2715 to
= GetItemCount() - 1;
2718 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2720 RefreshLine(m_current
);
2723 for ( size_t line
= from
; line
<= to
; line
++ )
2725 // NB: the test works as expected even if m_current == -1
2726 if ( line
!= m_current
&& IsHighlighted(line
) )
2733 void wxListMainWindow::Freeze()
2738 void wxListMainWindow::Thaw()
2740 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2742 if ( !--m_freezeCount
)
2748 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2750 // Note: a wxPaintDC must be constructed even if no drawing is
2751 // done (a Windows requirement).
2752 wxPaintDC
dc( this );
2754 if ( IsEmpty() || m_freezeCount
)
2756 // nothing to draw or not the moment to draw it
2762 // delay the repainting until we calculate all the items positions
2769 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2773 dc
.SetFont( GetFont() );
2775 if ( HasFlag(wxLC_REPORT
) )
2777 int lineHeight
= GetLineHeight();
2779 size_t visibleFrom
, visibleTo
;
2780 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2783 wxCoord xOrig
, yOrig
;
2784 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2786 // tell the caller cache to cache the data
2789 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2790 GetParent()->GetId());
2791 evCache
.SetEventObject( GetParent() );
2792 evCache
.m_oldItemIndex
= visibleFrom
;
2793 evCache
.m_itemIndex
= visibleTo
;
2794 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2797 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2799 rectLine
= GetLineRect(line
);
2801 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2802 rectLine
.width
, rectLine
.height
) )
2804 // don't redraw unaffected lines to avoid flicker
2808 GetLine(line
)->DrawInReportMode( &dc
,
2810 GetLineHighlightRect(line
),
2811 IsHighlighted(line
) );
2814 if ( HasFlag(wxLC_HRULES
) )
2816 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2817 wxSize clientSize
= GetClientSize();
2819 // Don't draw the first one
2820 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2823 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2824 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2825 clientSize
.x
- dev_x
, i
*lineHeight
);
2828 // Draw last horizontal rule
2829 if ( visibleTo
== GetItemCount() - 1 )
2832 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2833 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2834 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2838 // Draw vertical rules if required
2839 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2841 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2844 wxRect firstItemRect
;
2845 wxRect lastItemRect
;
2846 GetItemRect(visibleFrom
, firstItemRect
);
2847 GetItemRect(visibleTo
, lastItemRect
);
2848 int x
= firstItemRect
.GetX();
2850 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2851 for (col
= 0; col
< GetColumnCount(); col
++)
2853 int colWidth
= GetColumnWidth(col
);
2855 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2856 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2862 size_t count
= GetItemCount();
2863 for ( size_t i
= 0; i
< count
; i
++ )
2865 GetLine(i
)->Draw( &dc
);
2871 // don't draw rect outline under Max if we already have the background
2872 // color but under other platforms only draw it if we do: it is a bit
2873 // silly to draw "focus rect" if we don't have focus!
2878 #endif // __WXMAC__/!__WXMAC__
2880 dc
.SetPen( *wxBLACK_PEN
);
2881 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2882 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2889 void wxListMainWindow::HighlightAll( bool on
)
2891 if ( IsSingleSel() )
2893 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2895 // we just have one item to turn off
2896 if ( HasCurrent() && IsHighlighted(m_current
) )
2898 HighlightLine(m_current
, FALSE
);
2899 RefreshLine(m_current
);
2904 HighlightLines(0, GetItemCount() - 1, on
);
2908 void wxListMainWindow::SendNotify( size_t line
,
2909 wxEventType command
,
2912 wxListEvent
le( command
, GetParent()->GetId() );
2913 le
.SetEventObject( GetParent() );
2914 le
.m_itemIndex
= line
;
2916 // set only for events which have position
2917 if ( point
!= wxDefaultPosition
)
2918 le
.m_pointDrag
= point
;
2920 // don't try to get the line info for virtual list controls: the main
2921 // program has it anyhow and if we did it would result in accessing all
2922 // the lines, even those which are not visible now and this is precisely
2923 // what we're trying to avoid
2924 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2926 if ( line
!= (size_t)-1 )
2928 GetLine(line
)->GetItem( 0, le
.m_item
);
2930 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2932 //else: there may be no more such item
2934 GetParent()->GetEventHandler()->ProcessEvent( le
);
2937 void wxListMainWindow::ChangeCurrent(size_t current
)
2939 m_current
= current
;
2941 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2944 void wxListMainWindow::EditLabel( long item
)
2946 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2947 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2949 size_t itemEdit
= (size_t)item
;
2951 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2952 le
.SetEventObject( GetParent() );
2953 le
.m_itemIndex
= item
;
2954 wxListLineData
*data
= GetLine(itemEdit
);
2955 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2956 data
->GetItem( 0, le
.m_item
);
2957 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2959 // vetoed by user code
2963 // We have to call this here because the label in question might just have
2964 // been added and no screen update taken place.
2968 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2973 void wxListMainWindow::OnRenameTimer()
2975 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2977 EditLabel( m_current
);
2980 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2982 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2983 le
.SetEventObject( GetParent() );
2984 le
.m_itemIndex
= itemEdit
;
2986 wxListLineData
*data
= GetLine(itemEdit
);
2987 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
2989 data
->GetItem( 0, le
.m_item
);
2990 le
.m_item
.m_text
= value
;
2991 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2995 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2997 event
.SetEventObject( GetParent() );
2998 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3001 if ( !HasCurrent() || IsEmpty() )
3007 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
3008 event
.ButtonDClick()) )
3011 int x
= event
.GetX();
3012 int y
= event
.GetY();
3013 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3015 // where did we hit it (if we did)?
3018 size_t count
= GetItemCount(),
3021 if ( HasFlag(wxLC_REPORT
) )
3023 current
= y
/ GetLineHeight();
3024 if ( current
< count
)
3025 hitResult
= HitTestLine(current
, x
, y
);
3029 // TODO: optimize it too! this is less simple than for report view but
3030 // enumerating all items is still not a way to do it!!
3031 for ( current
= 0; current
< count
; current
++ )
3033 hitResult
= HitTestLine(current
, x
, y
);
3039 if (event
.Dragging())
3041 if (m_dragCount
== 0)
3043 // we have to report the raw, physical coords as we want to be
3044 // able to call HitTest(event.m_pointDrag) from the user code to
3045 // get the item being dragged
3046 m_dragStart
= event
.GetPosition();
3051 if (m_dragCount
!= 3)
3054 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3055 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
3057 wxListEvent
le( command
, GetParent()->GetId() );
3058 le
.SetEventObject( GetParent() );
3059 le
.m_pointDrag
= m_dragStart
;
3060 GetParent()->GetEventHandler()->ProcessEvent( le
);
3071 // outside of any item
3075 bool forceClick
= FALSE
;
3076 if (event
.ButtonDClick())
3078 m_renameTimer
->Stop();
3079 m_lastOnSame
= FALSE
;
3082 // FIXME: wxGTK generates bad sequence of events prior to doubleclick
3083 // ("down, up, down, double, up" while other ports
3084 // do "down, up, double, up"). We have to have this hack
3085 // in place till somebody fixes wxGTK...
3086 if ( current
== m_lineBeforeLastClicked
)
3088 if ( current
== m_lineLastClicked
)
3091 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3097 // the first click was on another item, so don't interpret this as
3098 // a double click, but as a simple click instead
3103 if (event
.LeftUp() && m_lastOnSame
)
3105 if ((current
== m_current
) &&
3106 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3107 HasFlag(wxLC_EDIT_LABELS
) )
3109 m_renameTimer
->Start( 100, TRUE
);
3111 m_lastOnSame
= FALSE
;
3113 else if (event
.RightDown())
3115 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
3116 event
.GetPosition() );
3118 else if (event
.MiddleDown())
3120 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3122 else if ( event
.LeftDown() || forceClick
)
3124 m_lineBeforeLastClicked
= m_lineLastClicked
;
3125 m_lineLastClicked
= current
;
3127 size_t oldCurrent
= m_current
;
3129 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3131 HighlightAll( FALSE
);
3133 ChangeCurrent(current
);
3135 ReverseHighlight(m_current
);
3137 else // multi sel & either ctrl or shift is down
3139 if (event
.ControlDown())
3141 ChangeCurrent(current
);
3143 ReverseHighlight(m_current
);
3145 else if (event
.ShiftDown())
3147 ChangeCurrent(current
);
3149 size_t lineFrom
= oldCurrent
,
3152 if ( lineTo
< lineFrom
)
3155 lineFrom
= m_current
;
3158 HighlightLines(lineFrom
, lineTo
);
3160 else // !ctrl, !shift
3162 // test in the enclosing if should make it impossible
3163 wxFAIL_MSG( _T("how did we get here?") );
3167 if (m_current
!= oldCurrent
)
3169 RefreshLine( oldCurrent
);
3172 // forceClick is only set if the previous click was on another item
3173 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3177 void wxListMainWindow::MoveToItem(size_t item
)
3179 if ( item
== (size_t)-1 )
3182 wxRect rect
= GetLineRect(item
);
3184 int client_w
, client_h
;
3185 GetClientSize( &client_w
, &client_h
);
3187 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3188 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3190 if ( HasFlag(wxLC_REPORT
) )
3192 // the next we need the range of lines shown it might be different, so
3194 ResetVisibleLinesRange();
3196 if (rect
.y
< view_y
)
3197 Scroll( -1, rect
.y
/m_yScroll
);
3198 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3199 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3203 if (rect
.x
-view_x
< 5)
3204 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3205 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3206 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3210 // ----------------------------------------------------------------------------
3211 // keyboard handling
3212 // ----------------------------------------------------------------------------
3214 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3216 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3217 _T("invalid item index in OnArrowChar()") );
3219 size_t oldCurrent
= m_current
;
3221 // in single selection we just ignore Shift as we can't select several
3223 if ( event
.ShiftDown() && !IsSingleSel() )
3225 ChangeCurrent(newCurrent
);
3227 // select all the items between the old and the new one
3228 if ( oldCurrent
> newCurrent
)
3230 newCurrent
= oldCurrent
;
3231 oldCurrent
= m_current
;
3234 HighlightLines(oldCurrent
, newCurrent
);
3238 // all previously selected items are unselected unless ctrl is held
3239 if ( !event
.ControlDown() )
3240 HighlightAll(FALSE
);
3242 ChangeCurrent(newCurrent
);
3244 // refresh the old focus to remove it
3245 RefreshLine( oldCurrent
);
3247 if ( !event
.ControlDown() )
3249 HighlightLine( m_current
, TRUE
);
3253 RefreshLine( m_current
);
3258 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3260 wxWindow
*parent
= GetParent();
3262 /* we propagate the key event up */
3263 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3264 ke
.m_shiftDown
= event
.m_shiftDown
;
3265 ke
.m_controlDown
= event
.m_controlDown
;
3266 ke
.m_altDown
= event
.m_altDown
;
3267 ke
.m_metaDown
= event
.m_metaDown
;
3268 ke
.m_keyCode
= event
.m_keyCode
;
3271 ke
.SetEventObject( parent
);
3272 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3277 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3279 wxWindow
*parent
= GetParent();
3281 /* we send a list_key event up */
3284 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3285 le
.m_itemIndex
= m_current
;
3286 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3287 le
.m_code
= (int)event
.KeyCode();
3288 le
.SetEventObject( parent
);
3289 parent
->GetEventHandler()->ProcessEvent( le
);
3292 /* we propagate the char event up */
3293 wxKeyEvent
ke( wxEVT_CHAR
);
3294 ke
.m_shiftDown
= event
.m_shiftDown
;
3295 ke
.m_controlDown
= event
.m_controlDown
;
3296 ke
.m_altDown
= event
.m_altDown
;
3297 ke
.m_metaDown
= event
.m_metaDown
;
3298 ke
.m_keyCode
= event
.m_keyCode
;
3301 ke
.SetEventObject( parent
);
3302 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3304 if (event
.KeyCode() == WXK_TAB
)
3306 wxNavigationKeyEvent nevent
;
3307 nevent
.SetWindowChange( event
.ControlDown() );
3308 nevent
.SetDirection( !event
.ShiftDown() );
3309 nevent
.SetEventObject( GetParent()->GetParent() );
3310 nevent
.SetCurrentFocus( m_parent
);
3311 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3315 /* no item -> nothing to do */
3322 switch (event
.KeyCode())
3325 if ( m_current
> 0 )
3326 OnArrowChar( m_current
- 1, event
);
3330 if ( m_current
< (size_t)GetItemCount() - 1 )
3331 OnArrowChar( m_current
+ 1, event
);
3336 OnArrowChar( GetItemCount() - 1, event
);
3341 OnArrowChar( 0, event
);
3347 if ( HasFlag(wxLC_REPORT
) )
3349 steps
= m_linesPerPage
- 1;
3353 steps
= m_current
% m_linesPerPage
;
3356 int index
= m_current
- steps
;
3360 OnArrowChar( index
, event
);
3367 if ( HasFlag(wxLC_REPORT
) )
3369 steps
= m_linesPerPage
- 1;
3373 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3376 size_t index
= m_current
+ steps
;
3377 size_t count
= GetItemCount();
3378 if ( index
>= count
)
3381 OnArrowChar( index
, event
);
3386 if ( !HasFlag(wxLC_REPORT
) )
3388 int index
= m_current
- m_linesPerPage
;
3392 OnArrowChar( index
, event
);
3397 if ( !HasFlag(wxLC_REPORT
) )
3399 size_t index
= m_current
+ m_linesPerPage
;
3401 size_t count
= GetItemCount();
3402 if ( index
>= count
)
3405 OnArrowChar( index
, event
);
3410 if ( IsSingleSel() )
3412 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3414 if ( IsHighlighted(m_current
) )
3416 // don't unselect the item in single selection mode
3419 //else: select it in ReverseHighlight() below if unselected
3422 ReverseHighlight(m_current
);
3427 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3435 // ----------------------------------------------------------------------------
3437 // ----------------------------------------------------------------------------
3439 void wxListMainWindow::SetFocus()
3441 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3442 // overrides SetFocus in such way that it does never change focus from
3443 // panel's child to the panel itself. Unfortunately, we must be able to change
3444 // focus to the panel from wxListTextCtrl because the text control should
3445 // disappear when the user clicks outside it.
3447 wxWindow
*oldFocus
= FindFocus();
3449 if ( oldFocus
&& oldFocus
->GetParent() == this )
3451 wxWindow::SetFocus();
3455 wxScrolledWindow::SetFocus();
3459 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3461 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3462 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3463 // which are already drawn correctly resulting in horrible flicker - avoid
3475 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3476 event
.SetEventObject( GetParent() );
3477 GetParent()->GetEventHandler()->ProcessEvent( event
);
3480 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3487 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3489 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3491 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3493 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3495 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3497 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3499 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3501 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3503 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3507 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3509 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3511 m_normal_image_list
->GetSize( index
, width
, height
);
3513 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3515 m_small_image_list
->GetSize( index
, width
, height
);
3517 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3519 m_small_image_list
->GetSize( index
, width
, height
);
3521 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3523 m_small_image_list
->GetSize( index
, width
, height
);
3532 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3534 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3535 dc
.SetFont( GetFont() );
3538 dc
.GetTextExtent( s
, &lw
, NULL
);
3540 return lw
+ AUTOSIZE_COL_MARGIN
;
3543 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3547 // calc the spacing from the icon size
3550 if ((imageList
) && (imageList
->GetImageCount()) )
3552 imageList
->GetSize(0, width
, height
);
3555 if (which
== wxIMAGE_LIST_NORMAL
)
3557 m_normal_image_list
= imageList
;
3558 m_normal_spacing
= width
+ 8;
3561 if (which
== wxIMAGE_LIST_SMALL
)
3563 m_small_image_list
= imageList
;
3564 m_small_spacing
= width
+ 14;
3568 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3573 m_small_spacing
= spacing
;
3577 m_normal_spacing
= spacing
;
3581 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3583 return isSmall
? m_small_spacing
: m_normal_spacing
;
3586 // ----------------------------------------------------------------------------
3588 // ----------------------------------------------------------------------------
3590 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3592 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3594 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3596 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3597 item
.m_width
= GetTextLength( item
.m_text
);
3599 wxListHeaderData
*column
= node
->GetData();
3600 column
->SetItem( item
);
3602 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3604 headerWin
->m_dirty
= TRUE
;
3608 // invalidate it as it has to be recalculated
3612 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3614 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3615 _T("invalid column index") );
3617 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3618 _T("SetColumnWidth() can only be called in report mode.") );
3621 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3623 headerWin
->m_dirty
= TRUE
;
3625 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3626 wxCHECK_RET( node
, _T("no column?") );
3628 wxListHeaderData
*column
= node
->GetData();
3630 size_t count
= GetItemCount();
3632 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3634 width
= GetTextLength(column
->GetText());
3636 else if ( width
== wxLIST_AUTOSIZE
)
3640 // TODO: determine the max width somehow...
3641 width
= WIDTH_COL_DEFAULT
;
3645 wxClientDC
dc(this);
3646 dc
.SetFont( GetFont() );
3648 int max
= AUTOSIZE_COL_MARGIN
;
3650 for ( size_t i
= 0; i
< count
; i
++ )
3652 wxListLineData
*line
= GetLine(i
);
3653 wxListItemDataList::Node
*n
= line
->m_items
.Item( col
);
3655 wxCHECK_RET( n
, _T("no subitem?") );
3657 wxListItemData
*item
= n
->GetData();
3660 if (item
->HasImage())
3663 GetImageSize( item
->GetImage(), ix
, iy
);
3667 if (item
->HasText())
3670 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3678 width
= max
+ AUTOSIZE_COL_MARGIN
;
3682 column
->SetWidth( width
);
3684 // invalidate it as it has to be recalculated
3688 int wxListMainWindow::GetHeaderWidth() const
3690 if ( !m_headerWidth
)
3692 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3694 size_t count
= GetColumnCount();
3695 for ( size_t col
= 0; col
< count
; col
++ )
3697 self
->m_headerWidth
+= GetColumnWidth(col
);
3701 return m_headerWidth
;
3704 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3706 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3707 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3709 wxListHeaderData
*column
= node
->GetData();
3710 column
->GetItem( item
);
3713 int wxListMainWindow::GetColumnWidth( int col
) const
3715 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3716 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3718 wxListHeaderData
*column
= node
->GetData();
3719 return column
->GetWidth();
3722 // ----------------------------------------------------------------------------
3724 // ----------------------------------------------------------------------------
3726 void wxListMainWindow::SetItem( wxListItem
&item
)
3728 long id
= item
.m_itemId
;
3729 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3730 _T("invalid item index in SetItem") );
3734 wxListLineData
*line
= GetLine((size_t)id
);
3735 line
->SetItem( item
.m_col
, item
);
3738 if ( InReportView() )
3740 // just refresh the line to show the new value of the text/image
3741 RefreshLine((size_t)id
);
3745 // refresh everything (resulting in horrible flicker - FIXME!)
3750 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3752 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3753 _T("invalid list ctrl item index in SetItem") );
3755 size_t oldCurrent
= m_current
;
3756 size_t item
= (size_t)litem
; // safe because of the check above
3758 // do we need to change the focus?
3759 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3761 if ( state
& wxLIST_STATE_FOCUSED
)
3763 // don't do anything if this item is already focused
3764 if ( item
!= m_current
)
3766 ChangeCurrent(item
);
3768 if ( oldCurrent
!= (size_t)-1 )
3770 if ( IsSingleSel() )
3772 HighlightLine(oldCurrent
, FALSE
);
3775 RefreshLine(oldCurrent
);
3778 RefreshLine( m_current
);
3783 // don't do anything if this item is not focused
3784 if ( item
== m_current
)
3788 if ( IsSingleSel() )
3790 // we must unselect the old current item as well or we
3791 // might end up with more than one selected item in a
3792 // single selection control
3793 HighlightLine(oldCurrent
, FALSE
);
3796 RefreshLine( oldCurrent
);
3801 // do we need to change the selection state?
3802 if ( stateMask
& wxLIST_STATE_SELECTED
)
3804 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3806 if ( IsSingleSel() )
3810 // selecting the item also makes it the focused one in the
3812 if ( m_current
!= item
)
3814 ChangeCurrent(item
);
3816 if ( oldCurrent
!= (size_t)-1 )
3818 HighlightLine( oldCurrent
, FALSE
);
3819 RefreshLine( oldCurrent
);
3825 // only the current item may be selected anyhow
3826 if ( item
!= m_current
)
3831 if ( HighlightLine(item
, on
) )
3838 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3840 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3841 _T("invalid list ctrl item index in GetItemState()") );
3843 int ret
= wxLIST_STATE_DONTCARE
;
3845 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3847 if ( (size_t)item
== m_current
)
3848 ret
|= wxLIST_STATE_FOCUSED
;
3851 if ( stateMask
& wxLIST_STATE_SELECTED
)
3853 if ( IsHighlighted(item
) )
3854 ret
|= wxLIST_STATE_SELECTED
;
3860 void wxListMainWindow::GetItem( wxListItem
&item
) const
3862 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3863 _T("invalid item index in GetItem") );
3865 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3866 line
->GetItem( item
.m_col
, item
);
3869 // ----------------------------------------------------------------------------
3871 // ----------------------------------------------------------------------------
3873 size_t wxListMainWindow::GetItemCount() const
3875 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3878 void wxListMainWindow::SetItemCount(long count
)
3880 m_selStore
.SetItemCount(count
);
3881 m_countVirt
= count
;
3883 ResetVisibleLinesRange();
3885 // scrollbars must be reset
3889 int wxListMainWindow::GetSelectedItemCount() const
3891 // deal with the quick case first
3892 if ( IsSingleSel() )
3894 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3897 // virtual controls remmebers all its selections itself
3899 return m_selStore
.GetSelectedCount();
3901 // TODO: we probably should maintain the number of items selected even for
3902 // non virtual controls as enumerating all lines is really slow...
3903 size_t countSel
= 0;
3904 size_t count
= GetItemCount();
3905 for ( size_t line
= 0; line
< count
; line
++ )
3907 if ( GetLine(line
)->IsHighlighted() )
3914 // ----------------------------------------------------------------------------
3915 // item position/size
3916 // ----------------------------------------------------------------------------
3918 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3920 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3921 _T("invalid index in GetItemRect") );
3923 rect
= GetLineRect((size_t)index
);
3925 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3928 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3931 GetItemRect(item
, rect
);
3939 // ----------------------------------------------------------------------------
3940 // geometry calculation
3941 // ----------------------------------------------------------------------------
3943 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3945 wxClientDC
dc( this );
3946 dc
.SetFont( GetFont() );
3949 if ( HasFlag(wxLC_ICON
) )
3950 iconSpacing
= m_normal_spacing
;
3951 else if ( HasFlag(wxLC_SMALL_ICON
) )
3952 iconSpacing
= m_small_spacing
;
3956 // Note that we do not call GetClientSize() here but
3957 // GetSize() and substract the border size for sunken
3958 // borders manually. This is technically incorrect,
3959 // but we need to know the client area's size WITHOUT
3960 // scrollbars here. Since we don't know if there are
3961 // any scrollbars, we use GetSize() instead. Another
3962 // solution would be to call SetScrollbars() here to
3963 // remove the scrollbars and call GetClientSize() then,
3964 // but this might result in flicker and - worse - will
3965 // reset the scrollbars to 0 which is not good at all
3966 // if you resize a dialog/window, but don't want to
3967 // reset the window scrolling. RR.
3968 // Furthermore, we actually do NOT subtract the border
3969 // width as 2 pixels is just the extra space which we
3970 // need around the actual content in the window. Other-
3971 // wise the text would e.g. touch the upper border. RR.
3974 GetSize( &clientWidth
, &clientHeight
);
3976 if ( HasFlag(wxLC_REPORT
) )
3978 // all lines have the same height
3979 int lineHeight
= GetLineHeight();
3981 // scroll one line per step
3982 m_yScroll
= lineHeight
;
3984 size_t lineCount
= GetItemCount();
3985 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3987 m_linesPerPage
= clientHeight
/ lineHeight
;
3989 ResetVisibleLinesRange();
3991 SetScrollbars( m_xScroll
, m_yScroll
,
3992 GetHeaderWidth() / m_xScroll
,
3993 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3994 GetScrollPos(wxHORIZONTAL
),
3995 GetScrollPos(wxVERTICAL
),
4000 // at first we try without any scrollbar. if the items don't
4001 // fit into the window, we recalculate after subtracting an
4002 // approximated 15 pt for the horizontal scrollbar
4004 int entireWidth
= 0;
4006 for (int tries
= 0; tries
< 2; tries
++)
4008 // We start with 4 for the border around all items
4013 // Now we have decided that the items do not fit into the
4014 // client area. Unfortunately, wxWindows sometimes thinks
4015 // that it does fit and therefore NO horizontal scrollbar
4016 // is inserted. This looks ugly, so we fudge here and make
4017 // the calculated width bigger than was actually has been
4018 // calculated. This ensures that wxScrolledWindows puts
4019 // a scrollbar at the bottom of its client area.
4020 entireWidth
+= SCROLL_UNIT_X
;
4023 // Start at 2,2 so the text does not touch the border
4028 int currentlyVisibleLines
= 0;
4030 size_t count
= GetItemCount();
4031 for (size_t i
= 0; i
< count
; i
++)
4033 currentlyVisibleLines
++;
4034 wxListLineData
*line
= GetLine(i
);
4035 line
->CalculateSize( &dc
, iconSpacing
);
4036 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
); // Why clientWidth? (FIXME)
4038 wxSize sizeLine
= GetLineSize(i
);
4040 if ( maxWidth
< sizeLine
.x
)
4041 maxWidth
= sizeLine
.x
;
4044 if (currentlyVisibleLines
> m_linesPerPage
)
4045 m_linesPerPage
= currentlyVisibleLines
;
4047 // Assume that the size of the next one is the same... (FIXME)
4048 if ( y
+ sizeLine
.y
>= clientHeight
)
4050 currentlyVisibleLines
= 0;
4053 entireWidth
+= maxWidth
+6;
4057 // We have reached the last item.
4058 if ( i
== count
- 1 )
4059 entireWidth
+= maxWidth
;
4061 if ( (tries
== 0) && (entireWidth
+SCROLL_UNIT_X
> clientWidth
) )
4063 clientHeight
-= 15; // We guess the scrollbar height. (FIXME)
4065 currentlyVisibleLines
= 0;
4069 if ( i
== count
- 1 )
4070 tries
= 1; // Everything fits, no second try required.
4074 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
4075 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
4080 // FIXME: why should we call it from here?
4087 void wxListMainWindow::RefreshAll()
4092 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4093 if ( headerWin
&& headerWin
->m_dirty
)
4095 headerWin
->m_dirty
= FALSE
;
4096 headerWin
->Refresh();
4100 void wxListMainWindow::UpdateCurrent()
4102 if ( !HasCurrent() && !IsEmpty() )
4108 long wxListMainWindow::GetNextItem( long item
,
4109 int WXUNUSED(geometry
),
4113 max
= GetItemCount();
4114 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4115 _T("invalid listctrl index in GetNextItem()") );
4117 // notice that we start with the next item (or the first one if item == -1)
4118 // and this is intentional to allow writing a simple loop to iterate over
4119 // all selected items
4123 // this is not an error because the index was ok initially, just no
4134 size_t count
= GetItemCount();
4135 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4137 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4140 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4147 // ----------------------------------------------------------------------------
4149 // ----------------------------------------------------------------------------
4151 void wxListMainWindow::DeleteItem( long lindex
)
4153 size_t count
= GetItemCount();
4155 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4156 _T("invalid item index in DeleteItem") );
4158 size_t index
= (size_t)lindex
;
4160 // we don't need to adjust the index for the previous items
4161 if ( HasCurrent() && m_current
>= index
)
4163 // if the current item is being deleted, we want the next one to
4164 // become selected - unless there is no next one - so don't adjust
4165 // m_current in this case
4166 if ( m_current
!= index
|| m_current
== count
- 1 )
4172 if ( InReportView() )
4174 ResetVisibleLinesRange();
4181 m_selStore
.OnItemDelete(index
);
4185 m_lines
.RemoveAt( index
);
4188 // we need to refresh the (vert) scrollbar as the number of items changed
4191 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4193 RefreshAfter(index
);
4196 void wxListMainWindow::DeleteColumn( int col
)
4198 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4200 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4203 m_columns
.DeleteNode( node
);
4205 // invalidate it as it has to be recalculated
4209 void wxListMainWindow::DoDeleteAllItems()
4213 // nothing to do - in particular, don't send the event
4219 // to make the deletion of all items faster, we don't send the
4220 // notifications for each item deletion in this case but only one event
4221 // for all of them: this is compatible with wxMSW and documented in
4222 // DeleteAllItems() description
4224 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4225 event
.SetEventObject( GetParent() );
4226 GetParent()->GetEventHandler()->ProcessEvent( event
);
4235 if ( InReportView() )
4237 ResetVisibleLinesRange();
4243 void wxListMainWindow::DeleteAllItems()
4247 RecalculatePositions();
4250 void wxListMainWindow::DeleteEverything()
4257 // ----------------------------------------------------------------------------
4258 // scanning for an item
4259 // ----------------------------------------------------------------------------
4261 void wxListMainWindow::EnsureVisible( long index
)
4263 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4264 _T("invalid index in EnsureVisible") );
4266 // We have to call this here because the label in question might just have
4267 // been added and its position is not known yet
4270 RecalculatePositions(TRUE
/* no refresh */);
4273 MoveToItem((size_t)index
);
4276 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4283 size_t count
= GetItemCount();
4284 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4286 wxListLineData
*line
= GetLine(i
);
4287 if ( line
->GetText(0) == tmp
)
4294 long wxListMainWindow::FindItem(long start
, long data
)
4300 size_t count
= GetItemCount();
4301 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4303 wxListLineData
*line
= GetLine(i
);
4305 line
->GetItem( 0, item
);
4306 if (item
.m_data
== data
)
4313 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4315 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4317 size_t count
= GetItemCount();
4319 if ( HasFlag(wxLC_REPORT
) )
4321 size_t current
= y
/ GetLineHeight();
4322 if ( current
< count
)
4324 flags
= HitTestLine(current
, x
, y
);
4331 // TODO: optimize it too! this is less simple than for report view but
4332 // enumerating all items is still not a way to do it!!
4333 for ( size_t current
= 0; current
< count
; current
++ )
4335 flags
= HitTestLine(current
, x
, y
);
4344 // ----------------------------------------------------------------------------
4346 // ----------------------------------------------------------------------------
4348 void wxListMainWindow::InsertItem( wxListItem
&item
)
4350 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4352 size_t count
= GetItemCount();
4353 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4354 _T("invalid item index") );
4356 size_t id
= item
.m_itemId
;
4361 if ( HasFlag(wxLC_REPORT
) )
4363 else if ( HasFlag(wxLC_LIST
) )
4365 else if ( HasFlag(wxLC_ICON
) )
4367 else if ( HasFlag(wxLC_SMALL_ICON
) )
4368 mode
= wxLC_ICON
; // no typo
4371 wxFAIL_MSG( _T("unknown mode") );
4374 wxListLineData
*line
= new wxListLineData(this);
4376 line
->SetItem( 0, item
);
4378 m_lines
.Insert( line
, id
);
4381 RefreshLines(id
, GetItemCount() - 1);
4384 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4387 if ( HasFlag(wxLC_REPORT
) )
4389 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4390 item
.m_width
= GetTextLength( item
.m_text
);
4391 wxListHeaderData
*column
= new wxListHeaderData( item
);
4392 if ((col
>= 0) && (col
< (int)m_columns
.GetCount()))
4394 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4395 m_columns
.Insert( node
, column
);
4399 m_columns
.Append( column
);
4402 // invalidate it as it has to be recalculated
4407 // ----------------------------------------------------------------------------
4409 // ----------------------------------------------------------------------------
4411 wxListCtrlCompare list_ctrl_compare_func_2
;
4412 long list_ctrl_compare_data
;
4414 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4416 wxListLineData
*line1
= *arg1
;
4417 wxListLineData
*line2
= *arg2
;
4419 line1
->GetItem( 0, item
);
4420 long data1
= item
.m_data
;
4421 line2
->GetItem( 0, item
);
4422 long data2
= item
.m_data
;
4423 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4426 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4428 list_ctrl_compare_func_2
= fn
;
4429 list_ctrl_compare_data
= data
;
4430 m_lines
.Sort( list_ctrl_compare_func_1
);
4434 // ----------------------------------------------------------------------------
4436 // ----------------------------------------------------------------------------
4438 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4440 // update our idea of which lines are shown when we redraw the window the
4442 ResetVisibleLinesRange();
4445 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4446 wxScrolledWindow::OnScroll(event
);
4448 HandleOnScroll( event
);
4451 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4453 wxGenericListCtrl
* lc
= GetListCtrl();
4454 wxCHECK_RET( lc
, _T("no listctrl window?") );
4456 lc
->m_headerWin
->Refresh();
4457 lc
->m_headerWin
->Update();
4461 int wxListMainWindow::GetCountPerPage() const
4463 if ( !m_linesPerPage
)
4465 wxConstCast(this, wxListMainWindow
)->
4466 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4469 return m_linesPerPage
;
4472 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4474 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4476 if ( m_lineFrom
== (size_t)-1 )
4478 size_t count
= GetItemCount();
4481 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4483 // this may happen if SetScrollbars() hadn't been called yet
4484 if ( m_lineFrom
>= count
)
4485 m_lineFrom
= count
- 1;
4487 // we redraw one extra line but this is needed to make the redrawing
4488 // logic work when there is a fractional number of lines on screen
4489 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4490 if ( m_lineTo
>= count
)
4491 m_lineTo
= count
- 1;
4493 else // empty control
4496 m_lineTo
= (size_t)-1;
4500 wxASSERT_MSG( IsEmpty() ||
4501 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4502 _T("GetVisibleLinesRange() returns incorrect result") );
4510 // -------------------------------------------------------------------------------------
4512 // -------------------------------------------------------------------------------------
4514 #if !defined(__WIN32__)
4515 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
4518 // -------------------------------------------------------------------------------------
4519 // wxGenericListCtrl
4520 // -------------------------------------------------------------------------------------
4522 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4524 #if !defined(__WIN32__)
4525 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
4527 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
4530 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4531 EVT_SIZE(wxGenericListCtrl::OnSize
)
4532 EVT_IDLE(wxGenericListCtrl::OnIdle
)
4535 #if !defined(__WXMSW__) || defined(__WIN16__) || defined(__WXUNIVERSAL__)
4537 * wxListCtrl has to be a real class or we have problems with
4538 * the run-time information.
4541 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
4544 wxGenericListCtrl::wxGenericListCtrl()
4546 m_imageListNormal
= (wxImageListType
*) NULL
;
4547 m_imageListSmall
= (wxImageListType
*) NULL
;
4548 m_imageListState
= (wxImageListType
*) NULL
;
4550 m_ownsImageListNormal
=
4551 m_ownsImageListSmall
=
4552 m_ownsImageListState
= FALSE
;
4554 m_mainWin
= (wxListMainWindow
*) NULL
;
4555 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4558 wxGenericListCtrl::~wxGenericListCtrl()
4560 if (m_ownsImageListNormal
)
4561 delete m_imageListNormal
;
4562 if (m_ownsImageListSmall
)
4563 delete m_imageListSmall
;
4564 if (m_ownsImageListState
)
4565 delete m_imageListState
;
4568 void wxGenericListCtrl::CreateHeaderWindow()
4570 m_headerWin
= new wxListHeaderWindow
4572 this, -1, m_mainWin
,
4574 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
4579 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4584 const wxValidator
&validator
,
4585 const wxString
&name
)
4589 m_imageListState
= (wxImageListType
*) NULL
;
4590 m_ownsImageListNormal
=
4591 m_ownsImageListSmall
=
4592 m_ownsImageListState
= FALSE
;
4594 m_mainWin
= (wxListMainWindow
*) NULL
;
4595 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4597 if ( !(style
& wxLC_MASK_TYPE
) )
4599 style
= style
| wxLC_LIST
;
4602 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4605 // don't create the inner window with the border
4606 style
&= ~wxSUNKEN_BORDER
;
4608 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4610 if ( HasFlag(wxLC_REPORT
) )
4612 CreateHeaderWindow();
4614 if ( HasFlag(wxLC_NO_HEADER
) )
4616 // VZ: why do we create it at all then?
4617 m_headerWin
->Show( FALSE
);
4624 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4626 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4627 _T("wxLC_VIRTUAL can't be [un]set") );
4629 long flag
= GetWindowStyle();
4633 if (style
& wxLC_MASK_TYPE
)
4634 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4635 if (style
& wxLC_MASK_ALIGN
)
4636 flag
&= ~wxLC_MASK_ALIGN
;
4637 if (style
& wxLC_MASK_SORT
)
4638 flag
&= ~wxLC_MASK_SORT
;
4650 SetWindowStyleFlag( flag
);
4653 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4657 m_mainWin
->DeleteEverything();
4659 // has the header visibility changed?
4660 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4661 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4663 if ( hasHeader
!= willHaveHeader
)
4670 // don't delete, just hide, as we can reuse it later
4671 m_headerWin
->Show(FALSE
);
4673 //else: nothing to do
4675 else // must show header
4679 CreateHeaderWindow();
4681 else // already have it, just show
4683 m_headerWin
->Show( TRUE
);
4687 ResizeReportView(willHaveHeader
);
4691 wxWindow::SetWindowStyleFlag( flag
);
4694 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4696 m_mainWin
->GetColumn( col
, item
);
4700 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4702 m_mainWin
->SetColumn( col
, item
);
4706 int wxGenericListCtrl::GetColumnWidth( int col
) const
4708 return m_mainWin
->GetColumnWidth( col
);
4711 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4713 m_mainWin
->SetColumnWidth( col
, width
);
4717 int wxGenericListCtrl::GetCountPerPage() const
4719 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4722 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4724 m_mainWin
->GetItem( info
);
4728 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4730 m_mainWin
->SetItem( info
);
4734 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4737 info
.m_text
= label
;
4738 info
.m_mask
= wxLIST_MASK_TEXT
;
4739 info
.m_itemId
= index
;
4743 info
.m_image
= imageId
;
4744 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4746 m_mainWin
->SetItem(info
);
4750 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4752 return m_mainWin
->GetItemState( item
, stateMask
);
4755 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4757 m_mainWin
->SetItemState( item
, state
, stateMask
);
4761 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4764 info
.m_image
= image
;
4765 info
.m_mask
= wxLIST_MASK_IMAGE
;
4766 info
.m_itemId
= item
;
4767 m_mainWin
->SetItem( info
);
4771 wxString
wxGenericListCtrl::GetItemText( long item
) const
4773 return m_mainWin
->GetItemText(item
);
4776 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4778 m_mainWin
->SetItemText(item
, str
);
4781 long wxGenericListCtrl::GetItemData( long item
) const
4784 info
.m_itemId
= item
;
4785 m_mainWin
->GetItem( info
);
4789 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4792 info
.m_mask
= wxLIST_MASK_DATA
;
4793 info
.m_itemId
= item
;
4795 m_mainWin
->SetItem( info
);
4799 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4801 m_mainWin
->GetItemRect( item
, rect
);
4805 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4807 m_mainWin
->GetItemPosition( item
, pos
);
4811 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4816 int wxGenericListCtrl::GetItemCount() const
4818 return m_mainWin
->GetItemCount();
4821 int wxGenericListCtrl::GetColumnCount() const
4823 return m_mainWin
->GetColumnCount();
4826 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4828 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4831 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4833 return m_mainWin
->GetItemSpacing( isSmall
);
4836 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4839 info
.m_itemId
= item
;
4840 info
.SetTextColour( col
);
4841 m_mainWin
->SetItem( info
);
4844 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4847 info
.m_itemId
= item
;
4848 m_mainWin
->GetItem( info
);
4849 return info
.GetTextColour();
4852 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4855 info
.m_itemId
= item
;
4856 info
.SetBackgroundColour( col
);
4857 m_mainWin
->SetItem( info
);
4860 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4863 info
.m_itemId
= item
;
4864 m_mainWin
->GetItem( info
);
4865 return info
.GetBackgroundColour();
4868 int wxGenericListCtrl::GetSelectedItemCount() const
4870 return m_mainWin
->GetSelectedItemCount();
4873 wxColour
wxGenericListCtrl::GetTextColour() const
4875 return GetForegroundColour();
4878 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4880 SetForegroundColour(col
);
4883 long wxGenericListCtrl::GetTopItem() const
4888 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4890 return m_mainWin
->GetNextItem( item
, geom
, state
);
4893 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
4895 if (which
== wxIMAGE_LIST_NORMAL
)
4897 return m_imageListNormal
;
4899 else if (which
== wxIMAGE_LIST_SMALL
)
4901 return m_imageListSmall
;
4903 else if (which
== wxIMAGE_LIST_STATE
)
4905 return m_imageListState
;
4907 return (wxImageListType
*) NULL
;
4910 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
4912 if ( which
== wxIMAGE_LIST_NORMAL
)
4914 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4915 m_imageListNormal
= imageList
;
4916 m_ownsImageListNormal
= FALSE
;
4918 else if ( which
== wxIMAGE_LIST_SMALL
)
4920 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4921 m_imageListSmall
= imageList
;
4922 m_ownsImageListSmall
= FALSE
;
4924 else if ( which
== wxIMAGE_LIST_STATE
)
4926 if (m_ownsImageListState
) delete m_imageListState
;
4927 m_imageListState
= imageList
;
4928 m_ownsImageListState
= FALSE
;
4931 m_mainWin
->SetImageList( imageList
, which
);
4934 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
4936 SetImageList(imageList
, which
);
4937 if ( which
== wxIMAGE_LIST_NORMAL
)
4938 m_ownsImageListNormal
= TRUE
;
4939 else if ( which
== wxIMAGE_LIST_SMALL
)
4940 m_ownsImageListSmall
= TRUE
;
4941 else if ( which
== wxIMAGE_LIST_STATE
)
4942 m_ownsImageListState
= TRUE
;
4945 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4950 bool wxGenericListCtrl::DeleteItem( long item
)
4952 m_mainWin
->DeleteItem( item
);
4956 bool wxGenericListCtrl::DeleteAllItems()
4958 m_mainWin
->DeleteAllItems();
4962 bool wxGenericListCtrl::DeleteAllColumns()
4964 size_t count
= m_mainWin
->m_columns
.GetCount();
4965 for ( size_t n
= 0; n
< count
; n
++ )
4971 void wxGenericListCtrl::ClearAll()
4973 m_mainWin
->DeleteEverything();
4976 bool wxGenericListCtrl::DeleteColumn( int col
)
4978 m_mainWin
->DeleteColumn( col
);
4980 // if we don't have the header any longer, we need to relayout the window
4981 if ( !GetColumnCount() )
4983 ResizeReportView(FALSE
/* no header */);
4989 void wxGenericListCtrl::Edit( long item
)
4991 m_mainWin
->EditLabel( item
);
4994 bool wxGenericListCtrl::EnsureVisible( long item
)
4996 m_mainWin
->EnsureVisible( item
);
5000 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5002 return m_mainWin
->FindItem( start
, str
, partial
);
5005 long wxGenericListCtrl::FindItem( long start
, long data
)
5007 return m_mainWin
->FindItem( start
, data
);
5010 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
5011 int WXUNUSED(direction
))
5016 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
5018 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5021 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5023 m_mainWin
->InsertItem( info
);
5024 return info
.m_itemId
;
5027 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5030 info
.m_text
= label
;
5031 info
.m_mask
= wxLIST_MASK_TEXT
;
5032 info
.m_itemId
= index
;
5033 return InsertItem( info
);
5036 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5039 info
.m_mask
= wxLIST_MASK_IMAGE
;
5040 info
.m_image
= imageIndex
;
5041 info
.m_itemId
= index
;
5042 return InsertItem( info
);
5045 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5048 info
.m_text
= label
;
5049 info
.m_image
= imageIndex
;
5050 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5051 info
.m_itemId
= index
;
5052 return InsertItem( info
);
5055 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5057 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5059 m_mainWin
->InsertColumn( col
, item
);
5061 // if we hadn't had header before and have it now we need to relayout the
5063 if ( GetColumnCount() == 1 )
5065 ResizeReportView(TRUE
/* have header */);
5068 m_headerWin
->Refresh();
5073 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5074 int format
, int width
)
5077 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5078 item
.m_text
= heading
;
5081 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5082 item
.m_width
= width
;
5084 item
.m_format
= format
;
5086 return InsertColumn( col
, item
);
5089 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5095 // fn is a function which takes 3 long arguments: item1, item2, data.
5096 // item1 is the long data associated with a first item (NOT the index).
5097 // item2 is the long data associated with a second item (NOT the index).
5098 // data is the same value as passed to SortItems.
5099 // The return value is a negative number if the first item should precede the second
5100 // item, a positive number of the second item should precede the first,
5101 // or zero if the two items are equivalent.
5102 // data is arbitrary data to be passed to the sort function.
5104 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5106 m_mainWin
->SortItems( fn
, data
);
5110 // ----------------------------------------------------------------------------
5112 // ----------------------------------------------------------------------------
5114 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5119 ResizeReportView(m_mainWin
->HasHeader());
5121 m_mainWin
->RecalculatePositions();
5124 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5127 GetClientSize( &cw
, &ch
);
5131 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
5132 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
5134 else // no header window
5136 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5140 void wxGenericListCtrl::OnIdle( wxIdleEvent
& event
)
5144 // do it only if needed
5145 if ( !m_mainWin
->m_dirty
)
5148 m_mainWin
->RecalculatePositions();
5151 // ----------------------------------------------------------------------------
5153 // ----------------------------------------------------------------------------
5155 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5159 m_mainWin
->SetBackgroundColour( colour
);
5160 m_mainWin
->m_dirty
= TRUE
;
5166 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5168 if ( !wxWindow::SetForegroundColour( colour
) )
5173 m_mainWin
->SetForegroundColour( colour
);
5174 m_mainWin
->m_dirty
= TRUE
;
5179 m_headerWin
->SetForegroundColour( colour
);
5185 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5187 if ( !wxWindow::SetFont( font
) )
5192 m_mainWin
->SetFont( font
);
5193 m_mainWin
->m_dirty
= TRUE
;
5198 m_headerWin
->SetFont( font
);
5204 // ----------------------------------------------------------------------------
5205 // methods forwarded to m_mainWin
5206 // ----------------------------------------------------------------------------
5208 #if wxUSE_DRAG_AND_DROP
5210 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5212 m_mainWin
->SetDropTarget( dropTarget
);
5215 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5217 return m_mainWin
->GetDropTarget();
5220 #endif // wxUSE_DRAG_AND_DROP
5222 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5224 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5227 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5229 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5232 wxColour
wxGenericListCtrl::GetForegroundColour() const
5234 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5237 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5240 return m_mainWin
->PopupMenu( menu
, x
, y
);
5243 #endif // wxUSE_MENUS
5246 void wxGenericListCtrl::SetFocus()
5248 /* The test in window.cpp fails as we are a composite
5249 window, so it checks against "this", but not m_mainWin. */
5250 if ( FindFocus() != this )
5251 m_mainWin
->SetFocus();
5254 // ----------------------------------------------------------------------------
5255 // virtual list control support
5256 // ----------------------------------------------------------------------------
5258 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5260 // this is a pure virtual function, in fact - which is not really pure
5261 // because the controls which are not virtual don't need to implement it
5262 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5264 return wxEmptyString
;
5267 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5270 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5275 wxListItemAttr
*wxGenericListCtrl::OnGetItemAttr(long item
) const
5277 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5278 _T("invalid item index in OnGetItemAttr()") );
5280 // no attributes by default
5284 void wxGenericListCtrl::SetItemCount(long count
)
5286 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5288 m_mainWin
->SetItemCount(count
);
5291 void wxGenericListCtrl::RefreshItem(long item
)
5293 m_mainWin
->RefreshLine(item
);
5296 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5298 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5301 void wxGenericListCtrl::Freeze()
5303 m_mainWin
->Freeze();
5306 void wxGenericListCtrl::Thaw()
5311 #endif // wxUSE_LISTCTRL