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 // ----------------------------------------------------------------------------
26 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
27 #pragma implementation "listctrl.h"
28 #pragma implementation "listctrlbase.h"
31 // For compilers that support precompilation, includes "wx.h".
32 #include "wx/wxprec.h"
43 #include "wx/dynarray.h"
45 #include "wx/dcscreen.h"
47 #include "wx/textctrl.h"
50 // under Win32 we always use the native version and also may use the generic
51 // one, however some things should be done only if we use only the generic
53 #if defined(__WIN32__) && !defined(__WXUNIVERSAL__)
54 #define HAVE_NATIVE_LISTCTRL
57 // if we have the native control, wx/listctrl.h declares it and not this one
58 #ifdef HAVE_NATIVE_LISTCTRL
59 #include "wx/generic/listctrl.h"
60 #else // !HAVE_NATIVE_LISTCTRL
61 #include "wx/listctrl.h"
63 // if we have a native version, its implementation file does all this
64 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
66 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
68 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
69 #endif // HAVE_NATIVE_LISTCTRL/!HAVE_NATIVE_LISTCTRL
71 #include "wx/selstore.h"
73 #include "wx/renderer.h"
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
80 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
81 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
99 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 // // the height of the header window (FIXME: should depend on its font!)
107 // static const int HEADER_HEIGHT = 23;
109 // the scrollbar units
110 static const int SCROLL_UNIT_X
= 15;
111 static const int SCROLL_UNIT_Y
= 15;
113 // the spacing between the lines (in report mode)
114 static const int LINE_SPACING
= 0;
116 // extra margins around the text label
117 static const int EXTRA_WIDTH
= 3;
118 static const int EXTRA_HEIGHT
= 4;
120 // offset for the header window
121 static const int HEADER_OFFSET_X
= 1;
122 static const int HEADER_OFFSET_Y
= 1;
124 // when autosizing the columns, add some slack
125 static const int AUTOSIZE_COL_MARGIN
= 10;
127 // default and minimal widths for the header columns
128 static const int WIDTH_COL_DEFAULT
= 80;
129 static const int WIDTH_COL_MIN
= 10;
131 // the space between the image and the text in the report mode
132 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
134 // ============================================================================
136 // ============================================================================
138 //-----------------------------------------------------------------------------
139 // wxListItemData (internal)
140 //-----------------------------------------------------------------------------
142 class WXDLLEXPORT wxListItemData
145 wxListItemData(wxListMainWindow
*owner
);
148 void SetItem( const wxListItem
&info
);
149 void SetImage( int image
) { m_image
= image
; }
150 void SetData( long data
) { m_data
= data
; }
151 void SetPosition( int x
, int y
);
152 void SetSize( int width
, int height
);
154 bool HasText() const { return !m_text
.empty(); }
155 const wxString
& GetText() const { return m_text
; }
156 void SetText(const wxString
& text
) { m_text
= text
; }
158 // we can't use empty string for measuring the string width/height, so
159 // always return something
160 wxString
GetTextForMeasuring() const
162 wxString s
= GetText();
169 bool IsHit( int x
, int y
) const;
173 int GetWidth() const;
174 int GetHeight() const;
176 int GetImage() const { return m_image
; }
177 bool HasImage() const { return GetImage() != -1; }
179 void GetItem( wxListItem
&info
) const;
181 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
182 wxListItemAttr
*GetAttr() const { return m_attr
; }
185 // the item image or -1
188 // user data associated with the item
191 // the item coordinates are not used in report mode, instead this pointer
192 // is NULL and the owner window is used to retrieve the item position and
196 // the list ctrl we are in
197 wxListMainWindow
*m_owner
;
199 // custom attributes or NULL
200 wxListItemAttr
*m_attr
;
203 // common part of all ctors
209 //-----------------------------------------------------------------------------
210 // wxListHeaderData (internal)
211 //-----------------------------------------------------------------------------
213 class WXDLLEXPORT wxListHeaderData
: public wxObject
217 wxListHeaderData( const wxListItem
&info
);
218 void SetItem( const wxListItem
&item
);
219 void SetPosition( int x
, int y
);
220 void SetWidth( int w
);
221 void SetFormat( int format
);
222 void SetHeight( int h
);
223 bool HasImage() const;
225 bool HasText() const { return !m_text
.empty(); }
226 const wxString
& GetText() const { return m_text
; }
227 void SetText(const wxString
& text
) { m_text
= text
; }
229 void GetItem( wxListItem
&item
);
231 bool IsHit( int x
, int y
) const;
232 int GetImage() const;
233 int GetWidth() const;
234 int GetFormat() const;
250 //-----------------------------------------------------------------------------
251 // wxListLineData (internal)
252 //-----------------------------------------------------------------------------
254 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
255 #include "wx/listimpl.cpp"
256 WX_DEFINE_LIST(wxListItemDataList
);
261 // the list of subitems: only may have more than one item in report mode
262 wxListItemDataList m_items
;
264 // this is not used in report view
276 // the part to be highlighted
277 wxRect m_rectHighlight
;
280 // is this item selected? [NB: not used in virtual mode]
283 // back pointer to the list ctrl
284 wxListMainWindow
*m_owner
;
287 wxListLineData(wxListMainWindow
*owner
);
291 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
295 // are we in report mode?
296 inline bool InReportView() const;
298 // are we in virtual report mode?
299 inline bool IsVirtual() const;
301 // these 2 methods shouldn't be called for report view controls, in that
302 // case we determine our position/size ourselves
304 // calculate the size of the line
305 void CalculateSize( wxDC
*dc
, int spacing
);
307 // remember the position this line appears at
308 void SetPosition( int x
, int y
, int window_width
, int spacing
);
312 void SetImage( int image
) { SetImage(0, image
); }
313 int GetImage() const { return GetImage(0); }
314 bool HasImage() const { return GetImage() != -1; }
315 bool HasText() const { return !GetText(0).empty(); }
317 void SetItem( int index
, const wxListItem
&info
);
318 void GetItem( int index
, wxListItem
&info
);
320 wxString
GetText(int index
) const;
321 void SetText( int index
, const wxString s
);
323 wxListItemAttr
*GetAttr() const;
324 void SetAttr(wxListItemAttr
*attr
);
326 // return true if the highlighting really changed
327 bool Highlight( bool on
);
329 void ReverseHighlight();
331 bool IsHighlighted() const
333 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
335 return m_highlighted
;
338 // draw the line on the given DC in icon/list mode
339 void Draw( wxDC
*dc
);
341 // the same in report mode
342 void DrawInReportMode( wxDC
*dc
,
344 const wxRect
& rectHL
,
348 // set the line to contain num items (only can be > 1 in report mode)
349 void InitItems( int num
);
351 // get the mode (i.e. style) of the list control
352 inline int GetMode() const;
354 // prepare the DC for drawing with these item's attributes, return true if
355 // we need to draw the items background to highlight it, false otherwise
356 bool SetAttributes(wxDC
*dc
,
357 const wxListItemAttr
*attr
,
360 // draw the text on the DC with the correct justification; also add an
361 // ellipsis if the text is too large to fit in the current width
362 void DrawTextFormatted(wxDC
*dc
, const wxString
&text
, int col
, int x
, int y
, int width
);
364 // these are only used by GetImage/SetImage above, we don't support images
365 // with subitems at the public API level yet
366 void SetImage( int index
, int image
);
367 int GetImage( int index
) const;
370 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
371 #include "wx/arrimpl.cpp"
372 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
374 //-----------------------------------------------------------------------------
375 // wxListHeaderWindow (internal)
376 //-----------------------------------------------------------------------------
378 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
381 wxListMainWindow
*m_owner
;
382 wxCursor
*m_currentCursor
;
383 wxCursor
*m_resizeCursor
;
386 // column being resized or -1
389 // divider line position in logical (unscrolled) coords
392 // minimal position beyond which the divider line can't be dragged in
397 wxListHeaderWindow();
399 wxListHeaderWindow( wxWindow
*win
,
401 wxListMainWindow
*owner
,
402 const wxPoint
&pos
= wxDefaultPosition
,
403 const wxSize
&size
= wxDefaultSize
,
405 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
407 virtual ~wxListHeaderWindow();
410 void AdjustDC(wxDC
& dc
);
412 void OnPaint( wxPaintEvent
&event
);
413 void OnMouse( wxMouseEvent
&event
);
414 void OnSetFocus( wxFocusEvent
&event
);
420 // common part of all ctors
423 // generate and process the list event of the given type, return true if
424 // it wasn't vetoed, i.e. if we should proceed
425 bool SendListEvent(wxEventType type
, wxPoint pos
);
427 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
428 DECLARE_EVENT_TABLE()
431 //-----------------------------------------------------------------------------
432 // wxListRenameTimer (internal)
433 //-----------------------------------------------------------------------------
435 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
438 wxListMainWindow
*m_owner
;
441 wxListRenameTimer( wxListMainWindow
*owner
);
445 //-----------------------------------------------------------------------------
446 // wxListTextCtrl (internal)
447 //-----------------------------------------------------------------------------
449 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
452 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
455 void OnChar( wxKeyEvent
&event
);
456 void OnKeyUp( wxKeyEvent
&event
);
457 void OnKillFocus( wxFocusEvent
&event
);
459 bool AcceptChanges();
463 wxListMainWindow
*m_owner
;
464 wxString m_startValue
;
468 DECLARE_EVENT_TABLE()
471 //-----------------------------------------------------------------------------
472 // wxListMainWindow (internal)
473 //-----------------------------------------------------------------------------
475 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
476 #include "wx/listimpl.cpp"
477 WX_DEFINE_LIST(wxListHeaderDataList
);
479 class wxListMainWindow
: public wxScrolledWindow
483 wxListMainWindow( wxWindow
*parent
,
485 const wxPoint
& pos
= wxDefaultPosition
,
486 const wxSize
& size
= wxDefaultSize
,
488 const wxString
&name
= _T("listctrlmainwindow") );
490 virtual ~wxListMainWindow();
492 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
494 // return true if this is a virtual list control
495 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
497 // return true if the control is in report mode
498 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
500 // return true if we are in single selection mode, false if multi sel
501 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
503 // do we have a header window?
504 bool HasHeader() const
505 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
507 void HighlightAll( bool on
);
509 // all these functions only do something if the line is currently visible
511 // change the line "selected" state, return TRUE if it really changed
512 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
514 // as HighlightLine() but do it for the range of lines: this is incredibly
515 // more efficient for virtual list controls!
517 // NB: unlike HighlightLine() this one does refresh the lines on screen
518 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
520 // toggle the line state and refresh it
521 void ReverseHighlight( size_t line
)
522 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
524 // return true if the line is highlighted
525 bool IsHighlighted(size_t line
) const;
527 // refresh one or several lines at once
528 void RefreshLine( size_t line
);
529 void RefreshLines( size_t lineFrom
, size_t lineTo
);
531 // refresh all selected items
532 void RefreshSelected();
534 // refresh all lines below the given one: the difference with
535 // RefreshLines() is that the index here might not be a valid one (happens
536 // when the last line is deleted)
537 void RefreshAfter( size_t lineFrom
);
539 // the methods which are forwarded to wxListLineData itself in list/icon
540 // modes but are here because the lines don't store their positions in the
543 // get the bound rect for the entire line
544 wxRect
GetLineRect(size_t line
) const;
546 // get the bound rect of the label
547 wxRect
GetLineLabelRect(size_t line
) const;
549 // get the bound rect of the items icon (only may be called if we do have
551 wxRect
GetLineIconRect(size_t line
) const;
553 // get the rect to be highlighted when the item has focus
554 wxRect
GetLineHighlightRect(size_t line
) const;
556 // get the size of the total line rect
557 wxSize
GetLineSize(size_t line
) const
558 { return GetLineRect(line
).GetSize(); }
560 // return the hit code for the corresponding position (in this line)
561 long HitTestLine(size_t line
, int x
, int y
) const;
563 // bring the selected item into view, scrolling to it if necessary
564 void MoveToItem(size_t item
);
566 // bring the current item into view
567 void MoveToFocus() { MoveToItem(m_current
); }
569 // start editing the label of the given item
570 void EditLabel( long item
);
572 // suspend/resume redrawing the control
578 void OnRenameTimer();
579 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
580 void OnRenameCancelled(size_t itemEdit
);
582 void OnMouse( wxMouseEvent
&event
);
584 // called to switch the selection from the current item to newCurrent,
585 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
587 void OnChar( wxKeyEvent
&event
);
588 void OnKeyDown( wxKeyEvent
&event
);
589 void OnSetFocus( wxFocusEvent
&event
);
590 void OnKillFocus( wxFocusEvent
&event
);
591 void OnScroll(wxScrollWinEvent
& event
) ;
593 void OnPaint( wxPaintEvent
&event
);
595 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
596 void GetImageSize( int index
, int &width
, int &height
) const;
597 int GetTextLength( const wxString
&s
) const;
599 void SetImageList( wxImageListType
*imageList
, int which
);
600 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
601 int GetItemSpacing( bool isSmall
= FALSE
);
603 void SetColumn( int col
, wxListItem
&item
);
604 void SetColumnWidth( int col
, int width
);
605 void GetColumn( int col
, wxListItem
&item
) const;
606 int GetColumnWidth( int col
) const;
607 int GetColumnCount() const { return m_columns
.GetCount(); }
609 // returns the sum of the heights of all columns
610 int GetHeaderWidth() const;
612 int GetCountPerPage() const;
614 void SetItem( wxListItem
&item
);
615 void GetItem( wxListItem
&item
) const;
616 void SetItemState( long item
, long state
, long stateMask
);
617 int GetItemState( long item
, long stateMask
) const;
618 void GetItemRect( long index
, wxRect
&rect
) const;
619 bool GetItemPosition( long item
, wxPoint
& pos
) const;
620 int GetSelectedItemCount() const;
622 wxString
GetItemText(long item
) const
625 info
.m_itemId
= item
;
630 void SetItemText(long item
, const wxString
& value
)
633 info
.m_mask
= wxLIST_MASK_TEXT
;
634 info
.m_itemId
= item
;
639 // set the scrollbars and update the positions of the items
640 void RecalculatePositions(bool noRefresh
= FALSE
);
642 // refresh the window and the header
645 long GetNextItem( long item
, int geometry
, int state
) const;
646 void DeleteItem( long index
);
647 void DeleteAllItems();
648 void DeleteColumn( int col
);
649 void DeleteEverything();
650 void EnsureVisible( long index
);
651 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
652 long FindItem( long start
, long data
);
653 long HitTest( int x
, int y
, int &flags
);
654 void InsertItem( wxListItem
&item
);
655 void InsertColumn( long col
, wxListItem
&item
);
656 void SortItems( wxListCtrlCompare fn
, long data
);
658 size_t GetItemCount() const;
659 bool IsEmpty() const { return GetItemCount() == 0; }
660 void SetItemCount(long count
);
662 // change the current (== focused) item, send a notification event
663 void ChangeCurrent(size_t current
);
664 void ResetCurrent() { ChangeCurrent((size_t)-1); }
665 bool HasCurrent() const { return m_current
!= (size_t)-1; }
667 // send out a wxListEvent
668 void SendNotify( size_t line
,
670 wxPoint point
= wxDefaultPosition
);
672 // override base class virtual to reset m_lineHeight when the font changes
673 virtual bool SetFont(const wxFont
& font
)
675 if ( !wxScrolledWindow::SetFont(font
) )
683 // these are for wxListLineData usage only
685 // get the backpointer to the list ctrl
686 wxGenericListCtrl
*GetListCtrl() const
688 return wxStaticCast(GetParent(), wxGenericListCtrl
);
691 // get the height of all lines (assuming they all do have the same height)
692 wxCoord
GetLineHeight() const;
694 // get the y position of the given line (only for report view)
695 wxCoord
GetLineY(size_t line
) const;
697 // get the brush to use for the item highlighting
698 wxBrush
*GetHighlightBrush() const
700 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
704 // the array of all line objects for a non virtual list control (for the
705 // virtual list control we only ever use m_lines[0])
706 wxListLineDataArray m_lines
;
708 // the list of column objects
709 wxListHeaderDataList m_columns
;
711 // currently focused item or -1
714 // the number of lines per page
717 // this flag is set when something which should result in the window
718 // redrawing happens (i.e. an item was added or deleted, or its appearance
719 // changed) and OnPaint() doesn't redraw the window while it is set which
720 // allows to minimize the number of repaintings when a lot of items are
721 // being added. The real repainting occurs only after the next OnIdle()
725 wxColour
*m_highlightColour
;
728 wxImageListType
*m_small_image_list
;
729 wxImageListType
*m_normal_image_list
;
731 int m_normal_spacing
;
735 wxTimer
*m_renameTimer
;
740 // for double click logic
741 size_t m_lineLastClicked
,
742 m_lineBeforeLastClicked
;
745 // the total count of items in a virtual list control
748 // the object maintaining the items selection state, only used in virtual
750 wxSelectionStore m_selStore
;
752 // common part of all ctors
755 // intiialize m_[xy]Scroll
756 void InitScrolling();
758 // get the line data for the given index
759 wxListLineData
*GetLine(size_t n
) const
761 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
765 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
773 // get a dummy line which can be used for geometry calculations and such:
774 // you must use GetLine() if you want to really draw the line
775 wxListLineData
*GetDummyLine() const;
777 // cache the line data of the n-th line in m_lines[0]
778 void CacheLineData(size_t line
);
780 // get the range of visible lines
781 void GetVisibleLinesRange(size_t *from
, size_t *to
);
783 // force us to recalculate the range of visible lines
784 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
786 // get the colour to be used for drawing the rules
787 wxColour
GetRuleColour() const
792 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
797 // initialize the current item if needed
798 void UpdateCurrent();
800 // delete all items but don't refresh: called from dtor
801 void DoDeleteAllItems();
803 // the height of one line using the current font
804 wxCoord m_lineHeight
;
806 // the total header width or 0 if not calculated yet
807 wxCoord m_headerWidth
;
809 // the first and last lines being shown on screen right now (inclusive),
810 // both may be -1 if they must be calculated so never access them directly:
811 // use GetVisibleLinesRange() above instead
815 // the brushes to use for item highlighting when we do/don't have focus
816 wxBrush
*m_highlightBrush
,
817 *m_highlightUnfocusedBrush
;
819 // if this is > 0, the control is frozen and doesn't redraw itself
820 size_t m_freezeCount
;
822 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
823 DECLARE_EVENT_TABLE()
825 friend class wxGenericListCtrl
;
828 // ============================================================================
830 // ============================================================================
832 //-----------------------------------------------------------------------------
834 //-----------------------------------------------------------------------------
836 wxListItemData::~wxListItemData()
838 // in the virtual list control the attributes are managed by the main
839 // program, so don't delete them
840 if ( !m_owner
->IsVirtual() )
848 void wxListItemData::Init()
856 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
862 if ( owner
->InReportView() )
872 void wxListItemData::SetItem( const wxListItem
&info
)
874 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
875 SetText(info
.m_text
);
876 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
877 m_image
= info
.m_image
;
878 if ( info
.m_mask
& wxLIST_MASK_DATA
)
879 m_data
= info
.m_data
;
881 if ( info
.HasAttributes() )
884 *m_attr
= *info
.GetAttributes();
886 m_attr
= new wxListItemAttr(*info
.GetAttributes());
894 m_rect
->width
= info
.m_width
;
898 void wxListItemData::SetPosition( int x
, int y
)
900 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
906 void wxListItemData::SetSize( int width
, int height
)
908 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
911 m_rect
->width
= width
;
913 m_rect
->height
= height
;
916 bool wxListItemData::IsHit( int x
, int y
) const
918 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
920 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
923 int wxListItemData::GetX() const
925 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
930 int wxListItemData::GetY() const
932 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
937 int wxListItemData::GetWidth() const
939 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
941 return m_rect
->width
;
944 int wxListItemData::GetHeight() const
946 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
948 return m_rect
->height
;
951 void wxListItemData::GetItem( wxListItem
&info
) const
953 info
.m_text
= m_text
;
954 info
.m_image
= m_image
;
955 info
.m_data
= m_data
;
959 if ( m_attr
->HasTextColour() )
960 info
.SetTextColour(m_attr
->GetTextColour());
961 if ( m_attr
->HasBackgroundColour() )
962 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
963 if ( m_attr
->HasFont() )
964 info
.SetFont(m_attr
->GetFont());
968 //-----------------------------------------------------------------------------
970 //-----------------------------------------------------------------------------
972 void wxListHeaderData::Init()
983 wxListHeaderData::wxListHeaderData()
988 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
995 void wxListHeaderData::SetItem( const wxListItem
&item
)
997 m_mask
= item
.m_mask
;
999 if ( m_mask
& wxLIST_MASK_TEXT
)
1000 m_text
= item
.m_text
;
1002 if ( m_mask
& wxLIST_MASK_IMAGE
)
1003 m_image
= item
.m_image
;
1005 if ( m_mask
& wxLIST_MASK_FORMAT
)
1006 m_format
= item
.m_format
;
1008 if ( m_mask
& wxLIST_MASK_WIDTH
)
1009 SetWidth(item
.m_width
);
1012 void wxListHeaderData::SetPosition( int x
, int y
)
1018 void wxListHeaderData::SetHeight( int h
)
1023 void wxListHeaderData::SetWidth( int w
)
1027 m_width
= WIDTH_COL_DEFAULT
;
1028 else if (m_width
< WIDTH_COL_MIN
)
1029 m_width
= WIDTH_COL_MIN
;
1032 void wxListHeaderData::SetFormat( int format
)
1037 bool wxListHeaderData::HasImage() const
1039 return m_image
!= -1;
1042 bool wxListHeaderData::IsHit( int x
, int y
) const
1044 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1047 void wxListHeaderData::GetItem( wxListItem
& item
)
1049 item
.m_mask
= m_mask
;
1050 item
.m_text
= m_text
;
1051 item
.m_image
= m_image
;
1052 item
.m_format
= m_format
;
1053 item
.m_width
= m_width
;
1056 int wxListHeaderData::GetImage() const
1061 int wxListHeaderData::GetWidth() const
1066 int wxListHeaderData::GetFormat() const
1071 //-----------------------------------------------------------------------------
1073 //-----------------------------------------------------------------------------
1075 inline int wxListLineData::GetMode() const
1077 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1080 inline bool wxListLineData::InReportView() const
1082 return m_owner
->HasFlag(wxLC_REPORT
);
1085 inline bool wxListLineData::IsVirtual() const
1087 return m_owner
->IsVirtual();
1090 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1094 if ( InReportView() )
1100 m_gi
= new GeometryInfo
;
1103 m_highlighted
= FALSE
;
1105 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1108 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1110 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1111 wxCHECK_RET( node
, _T("no subitems at all??") );
1113 wxListItemData
*item
= node
->GetData();
1115 switch ( GetMode() )
1118 case wxLC_SMALL_ICON
:
1120 m_gi
->m_rectAll
.width
= spacing
;
1122 wxString s
= item
->GetText();
1128 m_gi
->m_rectLabel
.width
=
1129 m_gi
->m_rectLabel
.height
= 0;
1133 dc
->GetTextExtent( s
, &lw
, &lh
);
1134 if (lh
< SCROLL_UNIT_Y
)
1139 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1141 m_gi
->m_rectAll
.width
= lw
;
1143 m_gi
->m_rectLabel
.width
= lw
;
1144 m_gi
->m_rectLabel
.height
= lh
;
1147 if (item
->HasImage())
1150 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1151 m_gi
->m_rectIcon
.width
= w
+ 8;
1152 m_gi
->m_rectIcon
.height
= h
+ 8;
1154 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1155 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1156 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1157 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1160 if ( item
->HasText() )
1162 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1163 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1165 else // no text, highlight the icon
1167 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1168 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1175 wxString s
= item
->GetTextForMeasuring();
1178 dc
->GetTextExtent( s
, &lw
, &lh
);
1179 if (lh
< SCROLL_UNIT_Y
)
1184 m_gi
->m_rectLabel
.width
= lw
;
1185 m_gi
->m_rectLabel
.height
= lh
;
1187 m_gi
->m_rectAll
.width
= lw
;
1188 m_gi
->m_rectAll
.height
= lh
;
1190 if (item
->HasImage())
1193 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1194 m_gi
->m_rectIcon
.width
= w
;
1195 m_gi
->m_rectIcon
.height
= h
;
1197 m_gi
->m_rectAll
.width
+= 4 + w
;
1198 if (h
> m_gi
->m_rectAll
.height
)
1199 m_gi
->m_rectAll
.height
= h
;
1202 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1203 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1208 wxFAIL_MSG( _T("unexpected call to SetSize") );
1212 wxFAIL_MSG( _T("unknown mode") );
1216 void wxListLineData::SetPosition( int x
, int y
,
1217 int WXUNUSED(window_width
),
1220 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1221 wxCHECK_RET( node
, _T("no subitems at all??") );
1223 wxListItemData
*item
= node
->GetData();
1225 switch ( GetMode() )
1228 case wxLC_SMALL_ICON
:
1229 m_gi
->m_rectAll
.x
= x
;
1230 m_gi
->m_rectAll
.y
= y
;
1232 if ( item
->HasImage() )
1234 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1235 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1236 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1239 if ( item
->HasText() )
1241 if (m_gi
->m_rectAll
.width
> spacing
)
1242 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1244 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1245 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1246 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1247 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1249 else // no text, highlight the icon
1251 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1252 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1257 m_gi
->m_rectAll
.x
= x
;
1258 m_gi
->m_rectAll
.y
= y
;
1260 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1261 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1262 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1264 if (item
->HasImage())
1266 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1267 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1268 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1272 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1277 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1281 wxFAIL_MSG( _T("unknown mode") );
1285 void wxListLineData::InitItems( int num
)
1287 for (int i
= 0; i
< num
; i
++)
1288 m_items
.Append( new wxListItemData(m_owner
) );
1291 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1293 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1294 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1296 wxListItemData
*item
= node
->GetData();
1297 item
->SetItem( info
);
1300 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1302 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1305 wxListItemData
*item
= node
->GetData();
1306 item
->GetItem( info
);
1310 wxString
wxListLineData::GetText(int index
) const
1314 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1317 wxListItemData
*item
= node
->GetData();
1318 s
= item
->GetText();
1324 void wxListLineData::SetText( int index
, const wxString s
)
1326 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1329 wxListItemData
*item
= node
->GetData();
1334 void wxListLineData::SetImage( int index
, int image
)
1336 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1337 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1339 wxListItemData
*item
= node
->GetData();
1340 item
->SetImage(image
);
1343 int wxListLineData::GetImage( int index
) const
1345 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1346 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1348 wxListItemData
*item
= node
->GetData();
1349 return item
->GetImage();
1352 wxListItemAttr
*wxListLineData::GetAttr() const
1354 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1355 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1357 wxListItemData
*item
= node
->GetData();
1358 return item
->GetAttr();
1361 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1363 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1364 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1366 wxListItemData
*item
= node
->GetData();
1367 item
->SetAttr(attr
);
1370 bool wxListLineData::SetAttributes(wxDC
*dc
,
1371 const wxListItemAttr
*attr
,
1374 wxWindow
*listctrl
= m_owner
->GetParent();
1378 // don't use foreground colour for drawing highlighted items - this might
1379 // make them completely invisible (and there is no way to do bit
1380 // arithmetics on wxColour, unfortunately)
1384 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1388 if ( attr
&& attr
->HasTextColour() )
1390 colText
= attr
->GetTextColour();
1394 colText
= listctrl
->GetForegroundColour();
1398 dc
->SetTextForeground(colText
);
1402 if ( attr
&& attr
->HasFont() )
1404 font
= attr
->GetFont();
1408 font
= listctrl
->GetFont();
1414 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1415 if ( highlighted
|| hasBgCol
)
1419 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1423 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1426 dc
->SetPen( *wxTRANSPARENT_PEN
);
1434 void wxListLineData::Draw( wxDC
*dc
)
1436 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1437 wxCHECK_RET( node
, _T("no subitems at all??") );
1439 bool highlighted
= IsHighlighted();
1441 wxListItemAttr
*attr
= GetAttr();
1443 if ( SetAttributes(dc
, attr
, highlighted
) )
1445 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1448 wxListItemData
*item
= node
->GetData();
1449 if (item
->HasImage())
1451 wxRect rectIcon
= m_gi
->m_rectIcon
;
1452 m_owner
->DrawImage( item
->GetImage(), dc
,
1453 rectIcon
.x
, rectIcon
.y
);
1456 if (item
->HasText())
1458 wxRect rectLabel
= m_gi
->m_rectLabel
;
1460 wxDCClipper
clipper(*dc
, rectLabel
);
1461 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1465 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1467 const wxRect
& rectHL
,
1470 // TODO: later we should support setting different attributes for
1471 // different columns - to do it, just add "col" argument to
1472 // GetAttr() and move these lines into the loop below
1473 wxListItemAttr
*attr
= GetAttr();
1474 if ( SetAttributes(dc
, attr
, highlighted
) )
1476 dc
->DrawRectangle( rectHL
);
1479 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1480 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1483 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1485 node
= node
->GetNext(), col
++ )
1487 wxListItemData
*item
= node
->GetData();
1489 int width
= m_owner
->GetColumnWidth(col
);
1493 if ( item
->HasImage() )
1496 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1497 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1499 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1505 wxDCClipper
clipper(*dc
, xOld
, y
, width
- 8, rect
.height
);
1507 if ( item
->HasText() )
1509 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, y
, width
- 8);
1514 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1515 const wxString
&text
,
1521 wxString drawntext
, ellipsis
;
1522 wxCoord w
, h
, base_w
;
1525 // determine if the string can fit inside the current width
1526 dc
->GetTextExtent(text
, &w
, &h
);
1529 // it can, draw it using the items alignment
1530 m_owner
->GetColumn(col
, item
);
1531 switch ( item
.GetAlign() )
1534 wxFAIL_MSG( _T("unknown list item format") );
1537 case wxLIST_FORMAT_LEFT
:
1541 case wxLIST_FORMAT_RIGHT
:
1545 case wxLIST_FORMAT_CENTER
:
1546 x
+= (width
- w
) / 2;
1550 dc
->DrawText(text
, x
, y
);
1552 else // otherwise, truncate and add an ellipsis if possible
1554 // determine the base width
1555 ellipsis
= wxString(wxT("..."));
1556 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1558 // continue until we have enough space or only one character left
1560 size_t len
= text
.Length();
1561 drawntext
= text
.Left(len
);
1564 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1565 drawntext
.RemoveLast();
1568 if (w
+ base_w
<= width
)
1572 // if still not enough space, remove ellipsis characters
1573 while (ellipsis
.Length() > 0 && w
+ base_w
> width
)
1575 ellipsis
= ellipsis
.Left(ellipsis
.Length() - 1);
1576 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1579 // now draw the text
1580 dc
->DrawText(drawntext
, x
, y
);
1581 dc
->DrawText(ellipsis
, x
+ w
, y
);
1585 bool wxListLineData::Highlight( bool on
)
1587 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1589 if ( on
== m_highlighted
)
1597 void wxListLineData::ReverseHighlight( void )
1599 Highlight(!IsHighlighted());
1602 //-----------------------------------------------------------------------------
1603 // wxListHeaderWindow
1604 //-----------------------------------------------------------------------------
1606 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1608 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1609 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1610 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1611 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1614 void wxListHeaderWindow::Init()
1616 m_currentCursor
= (wxCursor
*) NULL
;
1617 m_isDragging
= FALSE
;
1621 wxListHeaderWindow::wxListHeaderWindow()
1625 m_owner
= (wxListMainWindow
*) NULL
;
1626 m_resizeCursor
= (wxCursor
*) NULL
;
1629 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1631 wxListMainWindow
*owner
,
1635 const wxString
&name
)
1636 : wxWindow( win
, id
, pos
, size
, style
, name
)
1641 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1643 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1646 wxListHeaderWindow::~wxListHeaderWindow()
1648 delete m_resizeCursor
;
1651 #ifdef __WXUNIVERSAL__
1652 #include "wx/univ/renderer.h"
1653 #include "wx/univ/theme.h"
1656 // shift the DC origin to match the position of the main window horz
1657 // scrollbar: this allows us to always use logical coords
1658 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1661 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1664 m_owner
->GetViewStart( &x
, NULL
);
1666 // account for the horz scrollbar offset
1667 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1670 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1672 wxPaintDC
dc( this );
1679 dc
.SetFont( GetFont() );
1681 // width and height of the entire header window
1683 GetClientSize( &w
, &h
);
1684 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1686 dc
.SetBackgroundMode(wxTRANSPARENT
);
1688 // do *not* use the listctrl colour for headers - one day we will have a
1689 // function to set it separately
1690 //dc.SetTextForeground( *wxBLACK );
1691 dc
.SetTextForeground(wxSystemSettings::
1692 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1694 int x
= HEADER_OFFSET_X
;
1696 int numColumns
= m_owner
->GetColumnCount();
1698 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1700 m_owner
->GetColumn( i
, item
);
1701 int wCol
= item
.m_width
;
1703 // the width of the rect to draw: make it smaller to fit entirely
1704 // inside the column rect
1707 wxRendererNative::Get().DrawHeaderButton
1711 wxRect(x
, HEADER_OFFSET_Y
, cw
, h
- 2),
1712 m_parent
->IsEnabled() ? 0
1713 : wxCONTROL_DISABLED
1716 // see if we have enough space for the column label
1718 // for this we need the width of the text
1720 dc
.GetTextExtent(item
.GetText(), &wLabel
, NULL
);
1721 wLabel
+= 2*EXTRA_WIDTH
;
1723 // and the width of the icon, if any
1724 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
1725 int ix
= 0, // init them just to suppress the compiler warnings
1727 const int image
= item
.m_image
;
1728 wxImageListType
*imageList
;
1731 imageList
= m_owner
->m_small_image_list
;
1734 imageList
->GetSize(image
, ix
, iy
);
1735 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1743 // ignore alignment if there is not enough space anyhow
1745 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1748 wxFAIL_MSG( _T("unknown list item format") );
1751 case wxLIST_FORMAT_LEFT
:
1755 case wxLIST_FORMAT_RIGHT
:
1756 xAligned
= x
+ cw
- wLabel
;
1759 case wxLIST_FORMAT_CENTER
:
1760 xAligned
= x
+ (cw
- wLabel
) / 2;
1765 // if we have an image, draw it on the right of the label
1772 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
1773 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1774 wxIMAGELIST_DRAW_TRANSPARENT
1777 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1780 // draw the text clipping it so that it doesn't overwrite the column
1782 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1784 dc
.DrawText( item
.GetText(),
1785 xAligned
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1793 void wxListHeaderWindow::DrawCurrent()
1795 int x1
= m_currentX
;
1797 m_owner
->ClientToScreen( &x1
, &y1
);
1799 int x2
= m_currentX
;
1801 m_owner
->GetClientSize( NULL
, &y2
);
1802 m_owner
->ClientToScreen( &x2
, &y2
);
1805 dc
.SetLogicalFunction( wxINVERT
);
1806 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1807 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1811 dc
.DrawLine( x1
, y1
, x2
, y2
);
1813 dc
.SetLogicalFunction( wxCOPY
);
1815 dc
.SetPen( wxNullPen
);
1816 dc
.SetBrush( wxNullBrush
);
1819 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1821 // we want to work with logical coords
1823 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1824 int y
= event
.GetY();
1828 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1830 // we don't draw the line beyond our window, but we allow dragging it
1833 GetClientSize( &w
, NULL
);
1834 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1837 // erase the line if it was drawn
1838 if ( m_currentX
< w
)
1841 if (event
.ButtonUp())
1844 m_isDragging
= FALSE
;
1846 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1847 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1854 m_currentX
= m_minX
+ 7;
1856 // draw in the new location
1857 if ( m_currentX
< w
)
1861 else // not dragging
1864 bool hit_border
= FALSE
;
1866 // end of the current column
1869 // find the column where this event occured
1871 countCol
= m_owner
->GetColumnCount();
1872 for (col
= 0; col
< countCol
; col
++)
1874 xpos
+= m_owner
->GetColumnWidth( col
);
1877 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1879 // near the column border
1886 // inside the column
1893 if ( col
== countCol
)
1896 if (event
.LeftDown() || event
.RightUp())
1898 if (hit_border
&& event
.LeftDown())
1900 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1901 event
.GetPosition()) )
1903 m_isDragging
= TRUE
;
1908 //else: column resizing was vetoed by the user code
1910 else // click on a column
1912 SendListEvent( event
.LeftDown()
1913 ? wxEVT_COMMAND_LIST_COL_CLICK
1914 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1915 event
.GetPosition());
1918 else if (event
.Moving())
1923 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1924 m_currentCursor
= m_resizeCursor
;
1928 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1929 m_currentCursor
= wxSTANDARD_CURSOR
;
1933 SetCursor(*m_currentCursor
);
1938 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1940 m_owner
->SetFocus();
1943 bool wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
1945 wxWindow
*parent
= GetParent();
1946 wxListEvent
le( type
, parent
->GetId() );
1947 le
.SetEventObject( parent
);
1948 le
.m_pointDrag
= pos
;
1950 // the position should be relative to the parent window, not
1951 // this one for compatibility with MSW and common sense: the
1952 // user code doesn't know anything at all about this header
1953 // window, so why should it get positions relative to it?
1954 le
.m_pointDrag
.y
-= GetSize().y
;
1956 le
.m_col
= m_column
;
1957 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1960 //-----------------------------------------------------------------------------
1961 // wxListRenameTimer (internal)
1962 //-----------------------------------------------------------------------------
1964 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1969 void wxListRenameTimer::Notify()
1971 m_owner
->OnRenameTimer();
1974 //-----------------------------------------------------------------------------
1975 // wxListTextCtrl (internal)
1976 //-----------------------------------------------------------------------------
1978 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
1979 EVT_CHAR (wxListTextCtrl::OnChar
)
1980 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
1981 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
1984 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
1985 : m_startValue(owner
->GetItemText(itemEdit
)),
1986 m_itemEdited(itemEdit
)
1991 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
1993 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
1994 &rectLabel
.x
, &rectLabel
.y
);
1996 (void)Create(owner
, wxID_ANY
, m_startValue
,
1997 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
1998 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2001 void wxListTextCtrl::Finish()
2005 wxPendingDelete
.Append(this);
2009 m_owner
->SetFocus();
2013 bool wxListTextCtrl::AcceptChanges()
2015 const wxString value
= GetValue();
2017 if ( value
== m_startValue
)
2019 // nothing changed, always accept
2023 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2025 // vetoed by the user
2029 // accepted, do rename the item
2030 m_owner
->SetItemText(m_itemEdited
, value
);
2035 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2037 switch ( event
.m_keyCode
)
2040 if ( !AcceptChanges() )
2042 // vetoed by the user code
2045 //else: fall through
2049 m_owner
->OnRenameCancelled( m_itemEdited
);
2057 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2065 // auto-grow the textctrl:
2066 wxSize parentSize
= m_owner
->GetSize();
2067 wxPoint myPos
= GetPosition();
2068 wxSize mySize
= GetSize();
2070 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2071 if (myPos
.x
+ sx
> parentSize
.x
)
2072 sx
= parentSize
.x
- myPos
.x
;
2080 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2084 // We must finish regardless of success, otherwise we'll get focus problems
2087 if ( !AcceptChanges() )
2088 m_owner
->OnRenameCancelled( m_itemEdited
);
2094 //-----------------------------------------------------------------------------
2096 //-----------------------------------------------------------------------------
2098 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2100 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2101 EVT_PAINT (wxListMainWindow::OnPaint
)
2102 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2103 EVT_CHAR (wxListMainWindow::OnChar
)
2104 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2105 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2106 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2107 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2110 void wxListMainWindow::Init()
2115 m_lineTo
= (size_t)-1;
2121 m_small_image_list
= (wxImageListType
*) NULL
;
2122 m_normal_image_list
= (wxImageListType
*) NULL
;
2124 m_small_spacing
= 30;
2125 m_normal_spacing
= 40;
2129 m_isCreated
= FALSE
;
2131 m_lastOnSame
= FALSE
;
2132 m_renameTimer
= new wxListRenameTimer( this );
2136 m_lineBeforeLastClicked
= (size_t)-1;
2141 void wxListMainWindow::InitScrolling()
2143 if ( HasFlag(wxLC_REPORT
) )
2145 m_xScroll
= SCROLL_UNIT_X
;
2146 m_yScroll
= SCROLL_UNIT_Y
;
2150 m_xScroll
= SCROLL_UNIT_Y
;
2155 wxListMainWindow::wxListMainWindow()
2160 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2166 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2171 const wxString
&name
)
2172 : wxScrolledWindow( parent
, id
, pos
, size
,
2173 style
| wxHSCROLL
| wxVSCROLL
, name
)
2177 m_highlightBrush
= new wxBrush
2179 wxSystemSettings::GetColour
2181 wxSYS_COLOUR_HIGHLIGHT
2186 m_highlightUnfocusedBrush
= new wxBrush
2188 wxSystemSettings::GetColour
2190 wxSYS_COLOUR_BTNSHADOW
2199 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2201 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2204 wxListMainWindow::~wxListMainWindow()
2207 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2209 delete m_highlightBrush
;
2210 delete m_highlightUnfocusedBrush
;
2212 delete m_renameTimer
;
2215 void wxListMainWindow::CacheLineData(size_t line
)
2217 wxGenericListCtrl
*listctrl
= GetListCtrl();
2219 wxListLineData
*ld
= GetDummyLine();
2221 size_t countCol
= GetColumnCount();
2222 for ( size_t col
= 0; col
< countCol
; col
++ )
2224 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2227 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2228 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2231 wxListLineData
*wxListMainWindow::GetDummyLine() const
2233 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2235 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2237 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2239 // we need to recreate the dummy line if the number of columns in the
2240 // control changed as it would have the incorrect number of fields
2242 if ( !m_lines
.IsEmpty() &&
2243 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2245 self
->m_lines
.Clear();
2248 if ( m_lines
.IsEmpty() )
2250 wxListLineData
*line
= new wxListLineData(self
);
2251 self
->m_lines
.Add(line
);
2253 // don't waste extra memory -- there never going to be anything
2254 // else/more in this array
2255 self
->m_lines
.Shrink();
2261 // ----------------------------------------------------------------------------
2262 // line geometry (report mode only)
2263 // ----------------------------------------------------------------------------
2265 wxCoord
wxListMainWindow::GetLineHeight() const
2267 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2269 // we cache the line height as calling GetTextExtent() is slow
2270 if ( !m_lineHeight
)
2272 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2274 wxClientDC
dc( self
);
2275 dc
.SetFont( GetFont() );
2278 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2280 if ( y
< SCROLL_UNIT_Y
)
2283 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2287 m_small_image_list
->GetSize(0, iw
, ih
);
2292 self
->m_lineHeight
= y
+ LINE_SPACING
;
2295 return m_lineHeight
;
2298 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2300 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2302 return LINE_SPACING
+ line
*GetLineHeight();
2305 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2307 if ( !InReportView() )
2308 return GetLine(line
)->m_gi
->m_rectAll
;
2311 rect
.x
= HEADER_OFFSET_X
;
2312 rect
.y
= GetLineY(line
);
2313 rect
.width
= GetHeaderWidth();
2314 rect
.height
= GetLineHeight();
2319 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2321 if ( !InReportView() )
2322 return GetLine(line
)->m_gi
->m_rectLabel
;
2325 rect
.x
= HEADER_OFFSET_X
;
2326 rect
.y
= GetLineY(line
);
2327 rect
.width
= GetColumnWidth(0);
2328 rect
.height
= GetLineHeight();
2333 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2335 if ( !InReportView() )
2336 return GetLine(line
)->m_gi
->m_rectIcon
;
2338 wxListLineData
*ld
= GetLine(line
);
2339 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2342 rect
.x
= HEADER_OFFSET_X
;
2343 rect
.y
= GetLineY(line
);
2344 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2349 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2351 return InReportView() ? GetLineRect(line
)
2352 : GetLine(line
)->m_gi
->m_rectHighlight
;
2355 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2357 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2359 wxListLineData
*ld
= GetLine(line
);
2361 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2362 return wxLIST_HITTEST_ONITEMICON
;
2364 // VS: Testing for "ld->HasText() || InReportView()" instead of
2365 // "ld->HasText()" is needed to make empty lines in report view
2367 if ( ld
->HasText() || InReportView() )
2369 wxRect rect
= InReportView() ? GetLineRect(line
)
2370 : GetLineLabelRect(line
);
2372 if ( rect
.Inside(x
, y
) )
2373 return wxLIST_HITTEST_ONITEMLABEL
;
2379 // ----------------------------------------------------------------------------
2380 // highlight (selection) handling
2381 // ----------------------------------------------------------------------------
2383 bool wxListMainWindow::IsHighlighted(size_t line
) const
2387 return m_selStore
.IsSelected(line
);
2391 wxListLineData
*ld
= GetLine(line
);
2392 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2394 return ld
->IsHighlighted();
2398 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2404 wxArrayInt linesChanged
;
2405 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2408 // meny items changed state, refresh everything
2409 RefreshLines(lineFrom
, lineTo
);
2411 else // only a few items changed state, refresh only them
2413 size_t count
= linesChanged
.GetCount();
2414 for ( size_t n
= 0; n
< count
; n
++ )
2416 RefreshLine(linesChanged
[n
]);
2420 else // iterate over all items in non report view
2422 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2424 if ( HighlightLine(line
, highlight
) )
2432 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2438 changed
= m_selStore
.SelectItem(line
, highlight
);
2442 wxListLineData
*ld
= GetLine(line
);
2443 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2445 changed
= ld
->Highlight(highlight
);
2450 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2451 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2457 void wxListMainWindow::RefreshLine( size_t line
)
2459 if ( HasFlag(wxLC_REPORT
) )
2461 size_t visibleFrom
, visibleTo
;
2462 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2464 if ( line
< visibleFrom
|| line
> visibleTo
)
2468 wxRect rect
= GetLineRect(line
);
2470 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2471 RefreshRect( rect
);
2474 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2476 // we suppose that they are ordered by caller
2477 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2479 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2481 if ( HasFlag(wxLC_REPORT
) )
2483 size_t visibleFrom
, visibleTo
;
2484 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2486 if ( lineFrom
< visibleFrom
)
2487 lineFrom
= visibleFrom
;
2488 if ( lineTo
> visibleTo
)
2493 rect
.y
= GetLineY(lineFrom
);
2494 rect
.width
= GetClientSize().x
;
2495 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2497 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2498 RefreshRect( rect
);
2502 // TODO: this should be optimized...
2503 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2510 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2512 if ( HasFlag(wxLC_REPORT
) )
2514 size_t visibleFrom
, visibleTo
;
2515 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2517 if ( lineFrom
< visibleFrom
)
2518 lineFrom
= visibleFrom
;
2519 else if ( lineFrom
> visibleTo
)
2524 rect
.y
= GetLineY(lineFrom
);
2525 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2527 wxSize size
= GetClientSize();
2528 rect
.width
= size
.x
;
2529 // refresh till the bottom of the window
2530 rect
.height
= size
.y
- rect
.y
;
2532 RefreshRect( rect
);
2536 // TODO: how to do it more efficiently?
2541 void wxListMainWindow::RefreshSelected()
2547 if ( InReportView() )
2549 GetVisibleLinesRange(&from
, &to
);
2554 to
= GetItemCount() - 1;
2557 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2559 RefreshLine(m_current
);
2562 for ( size_t line
= from
; line
<= to
; line
++ )
2564 // NB: the test works as expected even if m_current == -1
2565 if ( line
!= m_current
&& IsHighlighted(line
) )
2572 void wxListMainWindow::Freeze()
2577 void wxListMainWindow::Thaw()
2579 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2581 if ( !--m_freezeCount
)
2587 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2589 // Note: a wxPaintDC must be constructed even if no drawing is
2590 // done (a Windows requirement).
2591 wxPaintDC
dc( this );
2593 if ( IsEmpty() || m_freezeCount
)
2595 // nothing to draw or not the moment to draw it
2601 // delay the repainting until we calculate all the items positions
2608 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2612 dc
.SetFont( GetFont() );
2614 if ( HasFlag(wxLC_REPORT
) )
2616 int lineHeight
= GetLineHeight();
2618 size_t visibleFrom
, visibleTo
;
2619 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2622 wxCoord xOrig
, yOrig
;
2623 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2625 // tell the caller cache to cache the data
2628 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2629 GetParent()->GetId());
2630 evCache
.SetEventObject( GetParent() );
2631 evCache
.m_oldItemIndex
= visibleFrom
;
2632 evCache
.m_itemIndex
= visibleTo
;
2633 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2636 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2638 rectLine
= GetLineRect(line
);
2640 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2641 rectLine
.width
, rectLine
.height
) )
2643 // don't redraw unaffected lines to avoid flicker
2647 GetLine(line
)->DrawInReportMode( &dc
,
2649 GetLineHighlightRect(line
),
2650 IsHighlighted(line
) );
2653 if ( HasFlag(wxLC_HRULES
) )
2655 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2656 wxSize clientSize
= GetClientSize();
2658 // Don't draw the first one
2659 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2662 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2663 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2664 clientSize
.x
- dev_x
, i
*lineHeight
);
2667 // Draw last horizontal rule
2668 if ( visibleTo
== GetItemCount() - 1 )
2671 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2672 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2673 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2677 // Draw vertical rules if required
2678 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2680 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2683 wxRect firstItemRect
;
2684 wxRect lastItemRect
;
2685 GetItemRect(visibleFrom
, firstItemRect
);
2686 GetItemRect(visibleTo
, lastItemRect
);
2687 int x
= firstItemRect
.GetX();
2689 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2690 for (col
= 0; col
< GetColumnCount(); col
++)
2692 int colWidth
= GetColumnWidth(col
);
2694 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2695 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2701 size_t count
= GetItemCount();
2702 for ( size_t i
= 0; i
< count
; i
++ )
2704 GetLine(i
)->Draw( &dc
);
2710 // don't draw rect outline under Max if we already have the background
2711 // color but under other platforms only draw it if we do: it is a bit
2712 // silly to draw "focus rect" if we don't have focus!
2717 #endif // __WXMAC__/!__WXMAC__
2719 dc
.SetPen( *wxBLACK_PEN
);
2720 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2721 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2728 void wxListMainWindow::HighlightAll( bool on
)
2730 if ( IsSingleSel() )
2732 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2734 // we just have one item to turn off
2735 if ( HasCurrent() && IsHighlighted(m_current
) )
2737 HighlightLine(m_current
, FALSE
);
2738 RefreshLine(m_current
);
2743 HighlightLines(0, GetItemCount() - 1, on
);
2747 void wxListMainWindow::SendNotify( size_t line
,
2748 wxEventType command
,
2751 wxListEvent
le( command
, GetParent()->GetId() );
2752 le
.SetEventObject( GetParent() );
2753 le
.m_itemIndex
= line
;
2755 // set only for events which have position
2756 if ( point
!= wxDefaultPosition
)
2757 le
.m_pointDrag
= point
;
2759 // don't try to get the line info for virtual list controls: the main
2760 // program has it anyhow and if we did it would result in accessing all
2761 // the lines, even those which are not visible now and this is precisely
2762 // what we're trying to avoid
2763 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2765 if ( line
!= (size_t)-1 )
2767 GetLine(line
)->GetItem( 0, le
.m_item
);
2769 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2771 //else: there may be no more such item
2773 GetParent()->GetEventHandler()->ProcessEvent( le
);
2776 void wxListMainWindow::ChangeCurrent(size_t current
)
2778 m_current
= current
;
2780 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2783 void wxListMainWindow::EditLabel( long item
)
2785 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2786 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2788 size_t itemEdit
= (size_t)item
;
2790 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2791 le
.SetEventObject( GetParent() );
2792 le
.m_itemIndex
= item
;
2793 wxListLineData
*data
= GetLine(itemEdit
);
2794 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2795 data
->GetItem( 0, le
.m_item
);
2796 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2798 // vetoed by user code
2802 // We have to call this here because the label in question might just have
2803 // been added and no screen update taken place.
2807 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2812 void wxListMainWindow::OnRenameTimer()
2814 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2816 EditLabel( m_current
);
2819 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2821 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2822 le
.SetEventObject( GetParent() );
2823 le
.m_itemIndex
= itemEdit
;
2825 wxListLineData
*data
= GetLine(itemEdit
);
2826 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
2828 data
->GetItem( 0, le
.m_item
);
2829 le
.m_item
.m_text
= value
;
2830 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2834 #ifdef __VMS__ // Ignore unreacheable code
2835 # pragma message disable initnotreach
2838 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2840 // wxMSW seems not to notify the program about
2841 // cancelled label edits.
2844 // let owner know that the edit was cancelled
2845 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2847 // These only exist for wxTreeCtrl, which should probably be changed
2848 // le.m_editCancelled = TRUE;
2849 // le.m_label = wxEmptyString;
2851 le
.SetEventObject( GetParent() );
2852 le
.m_itemIndex
= itemEdit
;
2854 wxListLineData
*data
= GetLine(itemEdit
);
2855 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2857 data
->GetItem( 0, le
.m_item
);
2859 GetEventHandler()->ProcessEvent( le
);
2862 # pragma message enable initnotreach
2865 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2867 event
.SetEventObject( GetParent() );
2868 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2871 if ( !HasCurrent() || IsEmpty() )
2877 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2878 event
.ButtonDClick()) )
2881 int x
= event
.GetX();
2882 int y
= event
.GetY();
2883 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2885 // where did we hit it (if we did)?
2888 size_t count
= GetItemCount(),
2891 if ( HasFlag(wxLC_REPORT
) )
2893 current
= y
/ GetLineHeight();
2894 if ( current
< count
)
2895 hitResult
= HitTestLine(current
, x
, y
);
2899 // TODO: optimize it too! this is less simple than for report view but
2900 // enumerating all items is still not a way to do it!!
2901 for ( current
= 0; current
< count
; current
++ )
2903 hitResult
= HitTestLine(current
, x
, y
);
2909 if (event
.Dragging())
2911 if (m_dragCount
== 0)
2913 // we have to report the raw, physical coords as we want to be
2914 // able to call HitTest(event.m_pointDrag) from the user code to
2915 // get the item being dragged
2916 m_dragStart
= event
.GetPosition();
2921 if (m_dragCount
!= 3)
2924 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2925 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2927 wxListEvent
le( command
, GetParent()->GetId() );
2928 le
.SetEventObject( GetParent() );
2929 le
.m_itemIndex
= current
;
2930 le
.m_pointDrag
= m_dragStart
;
2931 GetParent()->GetEventHandler()->ProcessEvent( le
);
2942 // outside of any item
2946 bool forceClick
= FALSE
;
2947 if (event
.ButtonDClick())
2949 m_renameTimer
->Stop();
2950 m_lastOnSame
= FALSE
;
2952 if ( current
== m_lineLastClicked
)
2954 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2960 // the first click was on another item, so don't interpret this as
2961 // a double click, but as a simple click instead
2966 if (event
.LeftUp() && m_lastOnSame
)
2968 if ((current
== m_current
) &&
2969 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2970 HasFlag(wxLC_EDIT_LABELS
) )
2972 m_renameTimer
->Start( 100, TRUE
);
2974 m_lastOnSame
= FALSE
;
2976 else if (event
.RightDown())
2978 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
2979 event
.GetPosition() );
2981 else if (event
.MiddleDown())
2983 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2985 else if ( event
.LeftDown() || forceClick
)
2987 m_lineBeforeLastClicked
= m_lineLastClicked
;
2988 m_lineLastClicked
= current
;
2990 size_t oldCurrent
= m_current
;
2992 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
2994 HighlightAll( FALSE
);
2996 ChangeCurrent(current
);
2998 ReverseHighlight(m_current
);
3000 else // multi sel & either ctrl or shift is down
3002 if (event
.ControlDown())
3004 ChangeCurrent(current
);
3006 ReverseHighlight(m_current
);
3008 else if (event
.ShiftDown())
3010 ChangeCurrent(current
);
3012 size_t lineFrom
= oldCurrent
,
3015 if ( lineTo
< lineFrom
)
3018 lineFrom
= m_current
;
3021 HighlightLines(lineFrom
, lineTo
);
3023 else // !ctrl, !shift
3025 // test in the enclosing if should make it impossible
3026 wxFAIL_MSG( _T("how did we get here?") );
3030 if (m_current
!= oldCurrent
)
3032 RefreshLine( oldCurrent
);
3035 // forceClick is only set if the previous click was on another item
3036 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3040 void wxListMainWindow::MoveToItem(size_t item
)
3042 if ( item
== (size_t)-1 )
3045 wxRect rect
= GetLineRect(item
);
3047 int client_w
, client_h
;
3048 GetClientSize( &client_w
, &client_h
);
3050 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3051 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3053 if ( HasFlag(wxLC_REPORT
) )
3055 // the next we need the range of lines shown it might be different, so
3057 ResetVisibleLinesRange();
3059 if (rect
.y
< view_y
)
3060 Scroll( -1, rect
.y
/m_yScroll
);
3061 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3062 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3066 if (rect
.x
-view_x
< 5)
3067 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3068 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3069 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3073 // ----------------------------------------------------------------------------
3074 // keyboard handling
3075 // ----------------------------------------------------------------------------
3077 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3079 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3080 _T("invalid item index in OnArrowChar()") );
3082 size_t oldCurrent
= m_current
;
3084 // in single selection we just ignore Shift as we can't select several
3086 if ( event
.ShiftDown() && !IsSingleSel() )
3088 ChangeCurrent(newCurrent
);
3090 // select all the items between the old and the new one
3091 if ( oldCurrent
> newCurrent
)
3093 newCurrent
= oldCurrent
;
3094 oldCurrent
= m_current
;
3097 HighlightLines(oldCurrent
, newCurrent
);
3101 // all previously selected items are unselected unless ctrl is held
3102 if ( !event
.ControlDown() )
3103 HighlightAll(FALSE
);
3105 ChangeCurrent(newCurrent
);
3107 // refresh the old focus to remove it
3108 RefreshLine( oldCurrent
);
3110 if ( !event
.ControlDown() )
3112 HighlightLine( m_current
, TRUE
);
3116 RefreshLine( m_current
);
3121 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3123 wxWindow
*parent
= GetParent();
3125 /* we propagate the key event up */
3126 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3127 ke
.m_shiftDown
= event
.m_shiftDown
;
3128 ke
.m_controlDown
= event
.m_controlDown
;
3129 ke
.m_altDown
= event
.m_altDown
;
3130 ke
.m_metaDown
= event
.m_metaDown
;
3131 ke
.m_keyCode
= event
.m_keyCode
;
3134 ke
.SetEventObject( parent
);
3135 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3140 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3142 wxWindow
*parent
= GetParent();
3144 /* we send a list_key event up */
3147 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3148 le
.m_itemIndex
= m_current
;
3149 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3150 le
.m_code
= event
.GetKeyCode();
3151 le
.SetEventObject( parent
);
3152 parent
->GetEventHandler()->ProcessEvent( le
);
3155 /* we propagate the char event up */
3156 wxKeyEvent
ke( wxEVT_CHAR
);
3157 ke
.m_shiftDown
= event
.m_shiftDown
;
3158 ke
.m_controlDown
= event
.m_controlDown
;
3159 ke
.m_altDown
= event
.m_altDown
;
3160 ke
.m_metaDown
= event
.m_metaDown
;
3161 ke
.m_keyCode
= event
.m_keyCode
;
3164 ke
.SetEventObject( parent
);
3165 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3167 if (event
.GetKeyCode() == WXK_TAB
)
3169 wxNavigationKeyEvent nevent
;
3170 nevent
.SetWindowChange( event
.ControlDown() );
3171 nevent
.SetDirection( !event
.ShiftDown() );
3172 nevent
.SetEventObject( GetParent()->GetParent() );
3173 nevent
.SetCurrentFocus( m_parent
);
3174 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3178 /* no item -> nothing to do */
3185 switch (event
.GetKeyCode())
3188 if ( m_current
> 0 )
3189 OnArrowChar( m_current
- 1, event
);
3193 if ( m_current
< (size_t)GetItemCount() - 1 )
3194 OnArrowChar( m_current
+ 1, event
);
3199 OnArrowChar( GetItemCount() - 1, event
);
3204 OnArrowChar( 0, event
);
3210 if ( HasFlag(wxLC_REPORT
) )
3212 steps
= m_linesPerPage
- 1;
3216 steps
= m_current
% m_linesPerPage
;
3219 int index
= m_current
- steps
;
3223 OnArrowChar( index
, event
);
3230 if ( HasFlag(wxLC_REPORT
) )
3232 steps
= m_linesPerPage
- 1;
3236 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3239 size_t index
= m_current
+ steps
;
3240 size_t count
= GetItemCount();
3241 if ( index
>= count
)
3244 OnArrowChar( index
, event
);
3249 if ( !HasFlag(wxLC_REPORT
) )
3251 int index
= m_current
- m_linesPerPage
;
3255 OnArrowChar( index
, event
);
3260 if ( !HasFlag(wxLC_REPORT
) )
3262 size_t index
= m_current
+ m_linesPerPage
;
3264 size_t count
= GetItemCount();
3265 if ( index
>= count
)
3268 OnArrowChar( index
, event
);
3273 if ( IsSingleSel() )
3275 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3277 if ( IsHighlighted(m_current
) )
3279 // don't unselect the item in single selection mode
3282 //else: select it in ReverseHighlight() below if unselected
3285 ReverseHighlight(m_current
);
3290 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3298 // ----------------------------------------------------------------------------
3300 // ----------------------------------------------------------------------------
3302 void wxListMainWindow::SetFocus()
3304 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3305 // overrides SetFocus in such way that it does never change focus from
3306 // panel's child to the panel itself. Unfortunately, we must be able to change
3307 // focus to the panel from wxListTextCtrl because the text control should
3308 // disappear when the user clicks outside it.
3310 wxWindow
*oldFocus
= FindFocus();
3312 if ( oldFocus
&& oldFocus
->GetParent() == this )
3314 wxWindow::SetFocus();
3318 wxScrolledWindow::SetFocus();
3322 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3324 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3325 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3326 // which are already drawn correctly resulting in horrible flicker - avoid
3338 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3339 event
.SetEventObject( GetParent() );
3340 GetParent()->GetEventHandler()->ProcessEvent( event
);
3343 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3350 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3352 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3354 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3356 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3358 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3360 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3362 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3364 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3366 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3370 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3372 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3374 m_normal_image_list
->GetSize( index
, width
, height
);
3376 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3378 m_small_image_list
->GetSize( index
, width
, height
);
3380 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3382 m_small_image_list
->GetSize( index
, width
, height
);
3384 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3386 m_small_image_list
->GetSize( index
, width
, height
);
3395 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3397 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3398 dc
.SetFont( GetFont() );
3401 dc
.GetTextExtent( s
, &lw
, NULL
);
3403 return lw
+ AUTOSIZE_COL_MARGIN
;
3406 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3410 // calc the spacing from the icon size
3413 if ((imageList
) && (imageList
->GetImageCount()) )
3415 imageList
->GetSize(0, width
, height
);
3418 if (which
== wxIMAGE_LIST_NORMAL
)
3420 m_normal_image_list
= imageList
;
3421 m_normal_spacing
= width
+ 8;
3424 if (which
== wxIMAGE_LIST_SMALL
)
3426 m_small_image_list
= imageList
;
3427 m_small_spacing
= width
+ 14;
3428 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3432 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3437 m_small_spacing
= spacing
;
3441 m_normal_spacing
= spacing
;
3445 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3447 return isSmall
? m_small_spacing
: m_normal_spacing
;
3450 // ----------------------------------------------------------------------------
3452 // ----------------------------------------------------------------------------
3454 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3456 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3458 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3460 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3461 item
.m_width
= GetTextLength( item
.m_text
);
3463 wxListHeaderData
*column
= node
->GetData();
3464 column
->SetItem( item
);
3466 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3468 headerWin
->m_dirty
= TRUE
;
3472 // invalidate it as it has to be recalculated
3476 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3478 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3479 _T("invalid column index") );
3481 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3482 _T("SetColumnWidth() can only be called in report mode.") );
3485 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3487 headerWin
->m_dirty
= TRUE
;
3489 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3490 wxCHECK_RET( node
, _T("no column?") );
3492 wxListHeaderData
*column
= node
->GetData();
3494 size_t count
= GetItemCount();
3496 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3498 width
= GetTextLength(column
->GetText());
3500 else if ( width
== wxLIST_AUTOSIZE
)
3504 // TODO: determine the max width somehow...
3505 width
= WIDTH_COL_DEFAULT
;
3509 wxClientDC
dc(this);
3510 dc
.SetFont( GetFont() );
3512 int max
= AUTOSIZE_COL_MARGIN
;
3514 for ( size_t i
= 0; i
< count
; i
++ )
3516 wxListLineData
*line
= GetLine(i
);
3517 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3519 wxCHECK_RET( n
, _T("no subitem?") );
3521 wxListItemData
*item
= n
->GetData();
3524 if (item
->HasImage())
3527 GetImageSize( item
->GetImage(), ix
, iy
);
3531 if (item
->HasText())
3534 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3542 width
= max
+ AUTOSIZE_COL_MARGIN
;
3546 column
->SetWidth( width
);
3548 // invalidate it as it has to be recalculated
3552 int wxListMainWindow::GetHeaderWidth() const
3554 if ( !m_headerWidth
)
3556 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3558 size_t count
= GetColumnCount();
3559 for ( size_t col
= 0; col
< count
; col
++ )
3561 self
->m_headerWidth
+= GetColumnWidth(col
);
3565 return m_headerWidth
;
3568 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3570 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3571 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3573 wxListHeaderData
*column
= node
->GetData();
3574 column
->GetItem( item
);
3577 int wxListMainWindow::GetColumnWidth( int col
) const
3579 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3580 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3582 wxListHeaderData
*column
= node
->GetData();
3583 return column
->GetWidth();
3586 // ----------------------------------------------------------------------------
3588 // ----------------------------------------------------------------------------
3590 void wxListMainWindow::SetItem( wxListItem
&item
)
3592 long id
= item
.m_itemId
;
3593 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3594 _T("invalid item index in SetItem") );
3598 wxListLineData
*line
= GetLine((size_t)id
);
3599 line
->SetItem( item
.m_col
, item
);
3602 // update the item on screen
3604 GetItemRect(id
, rectItem
);
3605 RefreshRect(rectItem
);
3608 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3610 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3611 _T("invalid list ctrl item index in SetItem") );
3613 size_t oldCurrent
= m_current
;
3614 size_t item
= (size_t)litem
; // safe because of the check above
3616 // do we need to change the focus?
3617 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3619 if ( state
& wxLIST_STATE_FOCUSED
)
3621 // don't do anything if this item is already focused
3622 if ( item
!= m_current
)
3624 ChangeCurrent(item
);
3626 if ( oldCurrent
!= (size_t)-1 )
3628 if ( IsSingleSel() )
3630 HighlightLine(oldCurrent
, FALSE
);
3633 RefreshLine(oldCurrent
);
3636 RefreshLine( m_current
);
3641 // don't do anything if this item is not focused
3642 if ( item
== m_current
)
3646 if ( IsSingleSel() )
3648 // we must unselect the old current item as well or we
3649 // might end up with more than one selected item in a
3650 // single selection control
3651 HighlightLine(oldCurrent
, FALSE
);
3654 RefreshLine( oldCurrent
);
3659 // do we need to change the selection state?
3660 if ( stateMask
& wxLIST_STATE_SELECTED
)
3662 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3664 if ( IsSingleSel() )
3668 // selecting the item also makes it the focused one in the
3670 if ( m_current
!= item
)
3672 ChangeCurrent(item
);
3674 if ( oldCurrent
!= (size_t)-1 )
3676 HighlightLine( oldCurrent
, FALSE
);
3677 RefreshLine( oldCurrent
);
3683 // only the current item may be selected anyhow
3684 if ( item
!= m_current
)
3689 if ( HighlightLine(item
, on
) )
3696 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3698 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3699 _T("invalid list ctrl item index in GetItemState()") );
3701 int ret
= wxLIST_STATE_DONTCARE
;
3703 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3705 if ( (size_t)item
== m_current
)
3706 ret
|= wxLIST_STATE_FOCUSED
;
3709 if ( stateMask
& wxLIST_STATE_SELECTED
)
3711 if ( IsHighlighted(item
) )
3712 ret
|= wxLIST_STATE_SELECTED
;
3718 void wxListMainWindow::GetItem( wxListItem
&item
) const
3720 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3721 _T("invalid item index in GetItem") );
3723 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3724 line
->GetItem( item
.m_col
, item
);
3727 // ----------------------------------------------------------------------------
3729 // ----------------------------------------------------------------------------
3731 size_t wxListMainWindow::GetItemCount() const
3733 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3736 void wxListMainWindow::SetItemCount(long count
)
3738 m_selStore
.SetItemCount(count
);
3739 m_countVirt
= count
;
3741 ResetVisibleLinesRange();
3743 // scrollbars must be reset
3747 int wxListMainWindow::GetSelectedItemCount() const
3749 // deal with the quick case first
3750 if ( IsSingleSel() )
3752 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3755 // virtual controls remmebers all its selections itself
3757 return m_selStore
.GetSelectedCount();
3759 // TODO: we probably should maintain the number of items selected even for
3760 // non virtual controls as enumerating all lines is really slow...
3761 size_t countSel
= 0;
3762 size_t count
= GetItemCount();
3763 for ( size_t line
= 0; line
< count
; line
++ )
3765 if ( GetLine(line
)->IsHighlighted() )
3772 // ----------------------------------------------------------------------------
3773 // item position/size
3774 // ----------------------------------------------------------------------------
3776 wxRect
wxListMainWindow::GetViewRect() const
3778 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
3779 _T("wxListCtrl::GetViewRect() only works in icon mode") );
3781 // we need to find the longest/tallest label
3784 const int count
= GetItemCount();
3787 for ( int i
= 0; i
< count
; i
++ )
3792 wxCoord x
= r
.GetRight(),
3803 // account for the scrollbar
3804 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3805 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3808 return wxRect(0, 0, xMax
, yMax
);
3811 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3813 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3814 _T("invalid index in GetItemRect") );
3816 // ensure that we're laid out, otherwise we could return nonsense
3819 wxConstCast(this, wxListMainWindow
)->
3820 RecalculatePositions(TRUE
/* no refresh */);
3823 rect
= GetLineRect((size_t)index
);
3825 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3828 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3831 GetItemRect(item
, rect
);
3839 // ----------------------------------------------------------------------------
3840 // geometry calculation
3841 // ----------------------------------------------------------------------------
3843 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3845 wxClientDC
dc( this );
3846 dc
.SetFont( GetFont() );
3849 if ( HasFlag(wxLC_ICON
) )
3850 iconSpacing
= m_normal_spacing
;
3851 else if ( HasFlag(wxLC_SMALL_ICON
) )
3852 iconSpacing
= m_small_spacing
;
3856 // Note that we do not call GetClientSize() here but
3857 // GetSize() and substract the border size for sunken
3858 // borders manually. This is technically incorrect,
3859 // but we need to know the client area's size WITHOUT
3860 // scrollbars here. Since we don't know if there are
3861 // any scrollbars, we use GetSize() instead. Another
3862 // solution would be to call SetScrollbars() here to
3863 // remove the scrollbars and call GetClientSize() then,
3864 // but this might result in flicker and - worse - will
3865 // reset the scrollbars to 0 which is not good at all
3866 // if you resize a dialog/window, but don't want to
3867 // reset the window scrolling. RR.
3868 // Furthermore, we actually do NOT subtract the border
3869 // width as 2 pixels is just the extra space which we
3870 // need around the actual content in the window. Other-
3871 // wise the text would e.g. touch the upper border. RR.
3874 GetSize( &clientWidth
, &clientHeight
);
3876 if ( HasFlag(wxLC_REPORT
) )
3878 // all lines have the same height
3879 int lineHeight
= GetLineHeight();
3881 // scroll one line per step
3882 m_yScroll
= lineHeight
;
3884 size_t lineCount
= GetItemCount();
3885 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3887 m_linesPerPage
= clientHeight
/ lineHeight
;
3889 ResetVisibleLinesRange();
3891 SetScrollbars( m_xScroll
, m_yScroll
,
3892 GetHeaderWidth() / m_xScroll
,
3893 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3894 GetScrollPos(wxHORIZONTAL
),
3895 GetScrollPos(wxVERTICAL
),
3900 // at first we try without any scrollbar. if the items don't
3901 // fit into the window, we recalculate after subtracting an
3902 // approximated 15 pt for the horizontal scrollbar
3904 int entireWidth
= 0;
3906 for (int tries
= 0; tries
< 2; tries
++)
3908 // We start with 4 for the border around all items
3913 // Now we have decided that the items do not fit into the
3914 // client area. Unfortunately, wxWindows sometimes thinks
3915 // that it does fit and therefore NO horizontal scrollbar
3916 // is inserted. This looks ugly, so we fudge here and make
3917 // the calculated width bigger than was actually has been
3918 // calculated. This ensures that wxScrolledWindows puts
3919 // a scrollbar at the bottom of its client area.
3920 entireWidth
+= SCROLL_UNIT_X
;
3923 // Start at 2,2 so the text does not touch the border
3928 int currentlyVisibleLines
= 0;
3930 size_t count
= GetItemCount();
3931 for (size_t i
= 0; i
< count
; i
++)
3933 currentlyVisibleLines
++;
3934 wxListLineData
*line
= GetLine(i
);
3935 line
->CalculateSize( &dc
, iconSpacing
);
3936 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
); // Why clientWidth? (FIXME)
3938 wxSize sizeLine
= GetLineSize(i
);
3940 if ( maxWidth
< sizeLine
.x
)
3941 maxWidth
= sizeLine
.x
;
3944 if (currentlyVisibleLines
> m_linesPerPage
)
3945 m_linesPerPage
= currentlyVisibleLines
;
3947 // Assume that the size of the next one is the same... (FIXME)
3948 if ( y
+ sizeLine
.y
>= clientHeight
)
3950 currentlyVisibleLines
= 0;
3953 entireWidth
+= maxWidth
+6;
3957 // We have reached the last item.
3958 if ( i
== count
- 1 )
3959 entireWidth
+= maxWidth
;
3961 if ( (tries
== 0) && (entireWidth
+SCROLL_UNIT_X
> clientWidth
) )
3963 clientHeight
-= 15; // We guess the scrollbar height. (FIXME)
3965 currentlyVisibleLines
= 0;
3969 if ( i
== count
- 1 )
3970 tries
= 1; // Everything fits, no second try required.
3974 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
3975 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
3980 // FIXME: why should we call it from here?
3987 void wxListMainWindow::RefreshAll()
3992 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3993 if ( headerWin
&& headerWin
->m_dirty
)
3995 headerWin
->m_dirty
= FALSE
;
3996 headerWin
->Refresh();
4000 void wxListMainWindow::UpdateCurrent()
4002 if ( !HasCurrent() && !IsEmpty() )
4008 long wxListMainWindow::GetNextItem( long item
,
4009 int WXUNUSED(geometry
),
4013 max
= GetItemCount();
4014 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4015 _T("invalid listctrl index in GetNextItem()") );
4017 // notice that we start with the next item (or the first one if item == -1)
4018 // and this is intentional to allow writing a simple loop to iterate over
4019 // all selected items
4023 // this is not an error because the index was ok initially, just no
4034 size_t count
= GetItemCount();
4035 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4037 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4040 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4047 // ----------------------------------------------------------------------------
4049 // ----------------------------------------------------------------------------
4051 void wxListMainWindow::DeleteItem( long lindex
)
4053 size_t count
= GetItemCount();
4055 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4056 _T("invalid item index in DeleteItem") );
4058 size_t index
= (size_t)lindex
;
4060 // we don't need to adjust the index for the previous items
4061 if ( HasCurrent() && m_current
>= index
)
4063 // if the current item is being deleted, we want the next one to
4064 // become selected - unless there is no next one - so don't adjust
4065 // m_current in this case
4066 if ( m_current
!= index
|| m_current
== count
- 1 )
4072 if ( InReportView() )
4074 ResetVisibleLinesRange();
4081 m_selStore
.OnItemDelete(index
);
4085 m_lines
.RemoveAt( index
);
4088 // we need to refresh the (vert) scrollbar as the number of items changed
4091 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4093 RefreshAfter(index
);
4096 void wxListMainWindow::DeleteColumn( int col
)
4098 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4100 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4103 delete node
->GetData();
4104 m_columns
.Erase( node
);
4108 // update all the items
4109 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4111 wxListLineData
* const line
= GetLine(i
);
4112 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4113 delete n
->GetData();
4114 line
->m_items
.Erase(n
);
4118 // invalidate it as it has to be recalculated
4122 void wxListMainWindow::DoDeleteAllItems()
4126 // nothing to do - in particular, don't send the event
4132 // to make the deletion of all items faster, we don't send the
4133 // notifications for each item deletion in this case but only one event
4134 // for all of them: this is compatible with wxMSW and documented in
4135 // DeleteAllItems() description
4137 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4138 event
.SetEventObject( GetParent() );
4139 GetParent()->GetEventHandler()->ProcessEvent( event
);
4148 if ( InReportView() )
4150 ResetVisibleLinesRange();
4156 void wxListMainWindow::DeleteAllItems()
4160 RecalculatePositions();
4163 void wxListMainWindow::DeleteEverything()
4165 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4170 // ----------------------------------------------------------------------------
4171 // scanning for an item
4172 // ----------------------------------------------------------------------------
4174 void wxListMainWindow::EnsureVisible( long index
)
4176 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4177 _T("invalid index in EnsureVisible") );
4179 // We have to call this here because the label in question might just have
4180 // been added and its position is not known yet
4183 RecalculatePositions(TRUE
/* no refresh */);
4186 MoveToItem((size_t)index
);
4189 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4196 size_t count
= GetItemCount();
4197 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4199 wxListLineData
*line
= GetLine(i
);
4200 if ( line
->GetText(0) == tmp
)
4207 long wxListMainWindow::FindItem(long start
, long data
)
4213 size_t count
= GetItemCount();
4214 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4216 wxListLineData
*line
= GetLine(i
);
4218 line
->GetItem( 0, item
);
4219 if (item
.m_data
== data
)
4226 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4228 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4230 size_t count
= GetItemCount();
4232 if ( HasFlag(wxLC_REPORT
) )
4234 size_t current
= y
/ GetLineHeight();
4235 if ( current
< count
)
4237 flags
= HitTestLine(current
, x
, y
);
4244 // TODO: optimize it too! this is less simple than for report view but
4245 // enumerating all items is still not a way to do it!!
4246 for ( size_t current
= 0; current
< count
; current
++ )
4248 flags
= HitTestLine(current
, x
, y
);
4257 // ----------------------------------------------------------------------------
4259 // ----------------------------------------------------------------------------
4261 void wxListMainWindow::InsertItem( wxListItem
&item
)
4263 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4265 size_t count
= GetItemCount();
4266 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4267 _T("invalid item index") );
4269 size_t id
= item
.m_itemId
;
4274 if ( HasFlag(wxLC_REPORT
) )
4277 ResetVisibleLinesRange();
4279 else if ( HasFlag(wxLC_LIST
) )
4281 else if ( HasFlag(wxLC_ICON
) )
4283 else if ( HasFlag(wxLC_SMALL_ICON
) )
4284 mode
= wxLC_ICON
; // no typo
4287 wxFAIL_MSG( _T("unknown mode") );
4290 wxListLineData
*line
= new wxListLineData(this);
4292 line
->SetItem( 0, item
);
4294 m_lines
.Insert( line
, id
);
4298 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4300 RefreshLines(id
, GetItemCount() - 1);
4303 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4306 if ( HasFlag(wxLC_REPORT
) )
4308 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4309 item
.m_width
= GetTextLength( item
.m_text
);
4311 wxListHeaderData
*column
= new wxListHeaderData( item
);
4312 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4315 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4316 m_columns
.Insert( node
, column
);
4320 m_columns
.Append( column
);
4325 // update all the items
4326 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4328 wxListLineData
* const line
= GetLine(i
);
4329 wxListItemData
* const data
= new wxListItemData(this);
4331 line
->m_items
.Insert(col
, data
);
4333 line
->m_items
.Append(data
);
4337 // invalidate it as it has to be recalculated
4342 // ----------------------------------------------------------------------------
4344 // ----------------------------------------------------------------------------
4346 wxListCtrlCompare list_ctrl_compare_func_2
;
4347 long list_ctrl_compare_data
;
4349 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4351 wxListLineData
*line1
= *arg1
;
4352 wxListLineData
*line2
= *arg2
;
4354 line1
->GetItem( 0, item
);
4355 long data1
= item
.m_data
;
4356 line2
->GetItem( 0, item
);
4357 long data2
= item
.m_data
;
4358 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4361 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4363 list_ctrl_compare_func_2
= fn
;
4364 list_ctrl_compare_data
= data
;
4365 m_lines
.Sort( list_ctrl_compare_func_1
);
4369 // ----------------------------------------------------------------------------
4371 // ----------------------------------------------------------------------------
4373 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4375 // update our idea of which lines are shown when we redraw the window the
4377 ResetVisibleLinesRange();
4380 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4381 wxScrolledWindow::OnScroll(event
);
4383 HandleOnScroll( event
);
4386 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4388 wxGenericListCtrl
* lc
= GetListCtrl();
4389 wxCHECK_RET( lc
, _T("no listctrl window?") );
4391 lc
->m_headerWin
->Refresh();
4392 lc
->m_headerWin
->Update();
4396 int wxListMainWindow::GetCountPerPage() const
4398 if ( !m_linesPerPage
)
4400 wxConstCast(this, wxListMainWindow
)->
4401 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4404 return m_linesPerPage
;
4407 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4409 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4411 if ( m_lineFrom
== (size_t)-1 )
4413 size_t count
= GetItemCount();
4416 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4418 // this may happen if SetScrollbars() hadn't been called yet
4419 if ( m_lineFrom
>= count
)
4420 m_lineFrom
= count
- 1;
4422 // we redraw one extra line but this is needed to make the redrawing
4423 // logic work when there is a fractional number of lines on screen
4424 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4425 if ( m_lineTo
>= count
)
4426 m_lineTo
= count
- 1;
4428 else // empty control
4431 m_lineTo
= (size_t)-1;
4435 wxASSERT_MSG( IsEmpty() ||
4436 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4437 _T("GetVisibleLinesRange() returns incorrect result") );
4445 // -------------------------------------------------------------------------------------
4446 // wxGenericListCtrl
4447 // -------------------------------------------------------------------------------------
4449 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4451 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4452 EVT_SIZE(wxGenericListCtrl::OnSize
)
4455 wxGenericListCtrl::wxGenericListCtrl()
4457 m_imageListNormal
= (wxImageListType
*) NULL
;
4458 m_imageListSmall
= (wxImageListType
*) NULL
;
4459 m_imageListState
= (wxImageListType
*) NULL
;
4461 m_ownsImageListNormal
=
4462 m_ownsImageListSmall
=
4463 m_ownsImageListState
= FALSE
;
4465 m_mainWin
= (wxListMainWindow
*) NULL
;
4466 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4470 wxGenericListCtrl::~wxGenericListCtrl()
4472 if (m_ownsImageListNormal
)
4473 delete m_imageListNormal
;
4474 if (m_ownsImageListSmall
)
4475 delete m_imageListSmall
;
4476 if (m_ownsImageListState
)
4477 delete m_imageListState
;
4480 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4482 // we use the letter "H" for calculating the needed space, basing on the current font
4484 m_headerWin
->GetTextExtent(wxT("H"), &w
, &h
);
4485 m_headerHeight
= h
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4486 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4489 void wxGenericListCtrl::CreateHeaderWindow()
4491 m_headerWin
= new wxListHeaderWindow
4493 this, -1, m_mainWin
,
4495 wxSize(GetClientSize().x
, m_headerHeight
),
4498 CalculateAndSetHeaderHeight();
4501 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4506 const wxValidator
&validator
,
4507 const wxString
&name
)
4511 m_imageListState
= (wxImageListType
*) NULL
;
4512 m_ownsImageListNormal
=
4513 m_ownsImageListSmall
=
4514 m_ownsImageListState
= FALSE
;
4516 m_mainWin
= (wxListMainWindow
*) NULL
;
4517 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4519 if ( !(style
& wxLC_MASK_TYPE
) )
4521 style
= style
| wxLC_LIST
;
4524 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4527 // don't create the inner window with the border
4528 style
&= ~wxBORDER_MASK
;
4530 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4532 if ( HasFlag(wxLC_REPORT
) )
4534 CreateHeaderWindow();
4536 if ( HasFlag(wxLC_NO_HEADER
) )
4538 // VZ: why do we create it at all then?
4539 m_headerWin
->Show( FALSE
);
4546 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4548 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4549 _T("wxLC_VIRTUAL can't be [un]set") );
4551 long flag
= GetWindowStyle();
4555 if (style
& wxLC_MASK_TYPE
)
4556 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4557 if (style
& wxLC_MASK_ALIGN
)
4558 flag
&= ~wxLC_MASK_ALIGN
;
4559 if (style
& wxLC_MASK_SORT
)
4560 flag
&= ~wxLC_MASK_SORT
;
4572 SetWindowStyleFlag( flag
);
4575 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4579 m_mainWin
->DeleteEverything();
4581 // has the header visibility changed?
4582 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4583 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4585 if ( hasHeader
!= willHaveHeader
)
4592 // don't delete, just hide, as we can reuse it later
4593 m_headerWin
->Show(FALSE
);
4595 //else: nothing to do
4597 else // must show header
4601 CreateHeaderWindow();
4603 else // already have it, just show
4605 m_headerWin
->Show( TRUE
);
4609 ResizeReportView(willHaveHeader
);
4613 wxWindow::SetWindowStyleFlag( flag
);
4616 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4618 m_mainWin
->GetColumn( col
, item
);
4622 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4624 m_mainWin
->SetColumn( col
, item
);
4628 int wxGenericListCtrl::GetColumnWidth( int col
) const
4630 return m_mainWin
->GetColumnWidth( col
);
4633 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4635 m_mainWin
->SetColumnWidth( col
, width
);
4639 int wxGenericListCtrl::GetCountPerPage() const
4641 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4644 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4646 m_mainWin
->GetItem( info
);
4650 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4652 m_mainWin
->SetItem( info
);
4656 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4659 info
.m_text
= label
;
4660 info
.m_mask
= wxLIST_MASK_TEXT
;
4661 info
.m_itemId
= index
;
4665 info
.m_image
= imageId
;
4666 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4668 m_mainWin
->SetItem(info
);
4672 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4674 return m_mainWin
->GetItemState( item
, stateMask
);
4677 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4679 m_mainWin
->SetItemState( item
, state
, stateMask
);
4683 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4686 info
.m_image
= image
;
4687 info
.m_mask
= wxLIST_MASK_IMAGE
;
4688 info
.m_itemId
= item
;
4689 m_mainWin
->SetItem( info
);
4693 wxString
wxGenericListCtrl::GetItemText( long item
) const
4695 return m_mainWin
->GetItemText(item
);
4698 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4700 m_mainWin
->SetItemText(item
, str
);
4703 long wxGenericListCtrl::GetItemData( long item
) const
4706 info
.m_itemId
= item
;
4707 m_mainWin
->GetItem( info
);
4711 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4714 info
.m_mask
= wxLIST_MASK_DATA
;
4715 info
.m_itemId
= item
;
4717 m_mainWin
->SetItem( info
);
4721 wxRect
wxGenericListCtrl::GetViewRect() const
4723 return m_mainWin
->GetViewRect();
4726 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4728 m_mainWin
->GetItemRect( item
, rect
);
4729 if ( m_mainWin
->HasHeader() )
4730 rect
.y
+= m_headerHeight
+ 1;
4734 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4736 m_mainWin
->GetItemPosition( item
, pos
);
4740 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4745 int wxGenericListCtrl::GetItemCount() const
4747 return m_mainWin
->GetItemCount();
4750 int wxGenericListCtrl::GetColumnCount() const
4752 return m_mainWin
->GetColumnCount();
4755 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4757 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4760 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4762 return m_mainWin
->GetItemSpacing( isSmall
);
4765 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4768 info
.m_itemId
= item
;
4769 info
.SetTextColour( col
);
4770 m_mainWin
->SetItem( info
);
4773 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4776 info
.m_itemId
= item
;
4777 m_mainWin
->GetItem( info
);
4778 return info
.GetTextColour();
4781 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4784 info
.m_itemId
= item
;
4785 info
.SetBackgroundColour( col
);
4786 m_mainWin
->SetItem( info
);
4789 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4792 info
.m_itemId
= item
;
4793 m_mainWin
->GetItem( info
);
4794 return info
.GetBackgroundColour();
4797 int wxGenericListCtrl::GetSelectedItemCount() const
4799 return m_mainWin
->GetSelectedItemCount();
4802 wxColour
wxGenericListCtrl::GetTextColour() const
4804 return GetForegroundColour();
4807 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4809 SetForegroundColour(col
);
4812 long wxGenericListCtrl::GetTopItem() const
4815 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4819 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4821 return m_mainWin
->GetNextItem( item
, geom
, state
);
4824 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
4826 if (which
== wxIMAGE_LIST_NORMAL
)
4828 return m_imageListNormal
;
4830 else if (which
== wxIMAGE_LIST_SMALL
)
4832 return m_imageListSmall
;
4834 else if (which
== wxIMAGE_LIST_STATE
)
4836 return m_imageListState
;
4838 return (wxImageListType
*) NULL
;
4841 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
4843 if ( which
== wxIMAGE_LIST_NORMAL
)
4845 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4846 m_imageListNormal
= imageList
;
4847 m_ownsImageListNormal
= FALSE
;
4849 else if ( which
== wxIMAGE_LIST_SMALL
)
4851 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4852 m_imageListSmall
= imageList
;
4853 m_ownsImageListSmall
= FALSE
;
4855 else if ( which
== wxIMAGE_LIST_STATE
)
4857 if (m_ownsImageListState
) delete m_imageListState
;
4858 m_imageListState
= imageList
;
4859 m_ownsImageListState
= FALSE
;
4862 m_mainWin
->SetImageList( imageList
, which
);
4865 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
4867 SetImageList(imageList
, which
);
4868 if ( which
== wxIMAGE_LIST_NORMAL
)
4869 m_ownsImageListNormal
= TRUE
;
4870 else if ( which
== wxIMAGE_LIST_SMALL
)
4871 m_ownsImageListSmall
= TRUE
;
4872 else if ( which
== wxIMAGE_LIST_STATE
)
4873 m_ownsImageListState
= TRUE
;
4876 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4881 bool wxGenericListCtrl::DeleteItem( long item
)
4883 m_mainWin
->DeleteItem( item
);
4887 bool wxGenericListCtrl::DeleteAllItems()
4889 m_mainWin
->DeleteAllItems();
4893 bool wxGenericListCtrl::DeleteAllColumns()
4895 size_t count
= m_mainWin
->m_columns
.GetCount();
4896 for ( size_t n
= 0; n
< count
; n
++ )
4902 void wxGenericListCtrl::ClearAll()
4904 m_mainWin
->DeleteEverything();
4907 bool wxGenericListCtrl::DeleteColumn( int col
)
4909 m_mainWin
->DeleteColumn( col
);
4911 // if we don't have the header any longer, we need to relayout the window
4912 if ( !GetColumnCount() )
4914 ResizeReportView(FALSE
/* no header */);
4920 void wxGenericListCtrl::Edit( long item
)
4922 m_mainWin
->EditLabel( item
);
4925 bool wxGenericListCtrl::EnsureVisible( long item
)
4927 m_mainWin
->EnsureVisible( item
);
4931 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4933 return m_mainWin
->FindItem( start
, str
, partial
);
4936 long wxGenericListCtrl::FindItem( long start
, long data
)
4938 return m_mainWin
->FindItem( start
, data
);
4941 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4942 int WXUNUSED(direction
))
4947 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4949 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4952 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4954 m_mainWin
->InsertItem( info
);
4955 return info
.m_itemId
;
4958 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4961 info
.m_text
= label
;
4962 info
.m_mask
= wxLIST_MASK_TEXT
;
4963 info
.m_itemId
= index
;
4964 return InsertItem( info
);
4967 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4970 info
.m_mask
= wxLIST_MASK_IMAGE
;
4971 info
.m_image
= imageIndex
;
4972 info
.m_itemId
= index
;
4973 return InsertItem( info
);
4976 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4979 info
.m_text
= label
;
4980 info
.m_image
= imageIndex
;
4981 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4982 info
.m_itemId
= index
;
4983 return InsertItem( info
);
4986 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4988 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
4990 m_mainWin
->InsertColumn( col
, item
);
4992 // if we hadn't had header before and have it now we need to relayout the
4994 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
4996 ResizeReportView(TRUE
/* have header */);
4999 m_headerWin
->Refresh();
5004 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5005 int format
, int width
)
5008 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5009 item
.m_text
= heading
;
5012 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5013 item
.m_width
= width
;
5015 item
.m_format
= format
;
5017 return InsertColumn( col
, item
);
5020 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5026 // fn is a function which takes 3 long arguments: item1, item2, data.
5027 // item1 is the long data associated with a first item (NOT the index).
5028 // item2 is the long data associated with a second item (NOT the index).
5029 // data is the same value as passed to SortItems.
5030 // The return value is a negative number if the first item should precede the second
5031 // item, a positive number of the second item should precede the first,
5032 // or zero if the two items are equivalent.
5033 // data is arbitrary data to be passed to the sort function.
5035 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5037 m_mainWin
->SortItems( fn
, data
);
5041 // ----------------------------------------------------------------------------
5043 // ----------------------------------------------------------------------------
5045 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5050 ResizeReportView(m_mainWin
->HasHeader());
5052 m_mainWin
->RecalculatePositions();
5055 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5058 GetClientSize( &cw
, &ch
);
5062 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5063 m_mainWin
->SetSize( 0, m_headerHeight
+ 1, cw
, ch
- m_headerHeight
- 1 );
5065 else // no header window
5067 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5071 void wxGenericListCtrl::OnInternalIdle()
5073 wxWindow::OnInternalIdle();
5075 // do it only if needed
5076 if ( !m_mainWin
->m_dirty
)
5079 m_mainWin
->RecalculatePositions();
5082 // ----------------------------------------------------------------------------
5084 // ----------------------------------------------------------------------------
5086 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5090 m_mainWin
->SetBackgroundColour( colour
);
5091 m_mainWin
->m_dirty
= TRUE
;
5097 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5099 if ( !wxWindow::SetForegroundColour( colour
) )
5104 m_mainWin
->SetForegroundColour( colour
);
5105 m_mainWin
->m_dirty
= TRUE
;
5110 m_headerWin
->SetForegroundColour( colour
);
5116 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5118 if ( !wxWindow::SetFont( font
) )
5123 m_mainWin
->SetFont( font
);
5124 m_mainWin
->m_dirty
= TRUE
;
5129 m_headerWin
->SetFont( font
);
5130 CalculateAndSetHeaderHeight();
5138 // ----------------------------------------------------------------------------
5139 // methods forwarded to m_mainWin
5140 // ----------------------------------------------------------------------------
5142 #if wxUSE_DRAG_AND_DROP
5144 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5146 m_mainWin
->SetDropTarget( dropTarget
);
5149 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5151 return m_mainWin
->GetDropTarget();
5154 #endif // wxUSE_DRAG_AND_DROP
5156 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5158 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5161 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5163 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5166 wxColour
wxGenericListCtrl::GetForegroundColour() const
5168 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5171 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5174 return m_mainWin
->PopupMenu( menu
, x
, y
);
5177 #endif // wxUSE_MENUS
5180 void wxGenericListCtrl::SetFocus()
5182 /* The test in window.cpp fails as we are a composite
5183 window, so it checks against "this", but not m_mainWin. */
5184 if ( FindFocus() != this )
5185 m_mainWin
->SetFocus();
5188 // ----------------------------------------------------------------------------
5189 // virtual list control support
5190 // ----------------------------------------------------------------------------
5192 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5194 // this is a pure virtual function, in fact - which is not really pure
5195 // because the controls which are not virtual don't need to implement it
5196 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5198 return wxEmptyString
;
5201 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5204 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5209 wxListItemAttr
*wxGenericListCtrl::OnGetItemAttr(long item
) const
5211 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5212 _T("invalid item index in OnGetItemAttr()") );
5214 // no attributes by default
5218 void wxGenericListCtrl::SetItemCount(long count
)
5220 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5222 m_mainWin
->SetItemCount(count
);
5225 void wxGenericListCtrl::RefreshItem(long item
)
5227 m_mainWin
->RefreshLine(item
);
5230 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5232 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5236 * Generic wxListCtrl is more or less a container for two other
5237 * windows which drawings are done upon. These are namely
5238 * 'm_headerWin' and 'm_mainWin'.
5239 * Here we override 'virtual wxWindow::Refresh()' to mimic the
5240 * behaviour wxListCtrl has under wxMSW.
5242 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5246 // The easy case, no rectangle specified.
5248 m_headerWin
->Refresh(eraseBackground
);
5251 m_mainWin
->Refresh(eraseBackground
);
5255 // Refresh the header window
5258 wxRect rectHeader
= m_headerWin
->GetRect();
5259 rectHeader
.Intersect(*rect
);
5260 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5263 m_headerWin
->GetPosition(&x
, &y
);
5264 rectHeader
.Offset(-x
, -y
);
5265 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5270 // Refresh the main window
5273 wxRect rectMain
= m_mainWin
->GetRect();
5274 rectMain
.Intersect(*rect
);
5275 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5278 m_mainWin
->GetPosition(&x
, &y
);
5279 rectMain
.Offset(-x
, -y
);
5280 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5286 void wxGenericListCtrl::Freeze()
5288 m_mainWin
->Freeze();
5291 void wxGenericListCtrl::Thaw()
5296 #endif // wxUSE_LISTCTRL