1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 1. we need to implement searching/sorting for virtual controls somehow
15 ?2. when changing selection the lines are refreshed twice
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
27 #pragma implementation "listctrl.h"
28 #pragma implementation "listctrlbase.h"
31 // For compilers that support precompilation, includes "wx.h".
32 #include "wx/wxprec.h"
43 #include "wx/dynarray.h"
45 #include "wx/dcscreen.h"
47 #include "wx/textctrl.h"
50 // under Win32 we always use the native version and also may use the generic
51 // one, however some things should be done only if we use only the generic
53 #if defined(__WIN32__) && !defined(__WXUNIVERSAL__)
54 #define HAVE_NATIVE_LISTCTRL
57 // if we have the native control, wx/listctrl.h declares it and not this one
58 #ifdef HAVE_NATIVE_LISTCTRL
59 #include "wx/generic/listctrl.h"
60 #else // !HAVE_NATIVE_LISTCTRL
61 #include "wx/listctrl.h"
63 // if we have a native version, its implementation file does all this
64 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
66 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
68 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
69 #endif // HAVE_NATIVE_LISTCTRL/!HAVE_NATIVE_LISTCTRL
71 #if defined(__WXGTK__)
73 #include "wx/gtk/win_gtk.h"
76 #include "wx/selstore.h"
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING
)
98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG
)
99 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
101 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
102 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED
)
103 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT
)
105 // ----------------------------------------------------------------------------
107 // ----------------------------------------------------------------------------
109 // the height of the header window (FIXME: should depend on its font!)
110 static const int HEADER_HEIGHT
= 23;
112 // the scrollbar units
113 static const int SCROLL_UNIT_X
= 15;
114 static const int SCROLL_UNIT_Y
= 15;
116 // the spacing between the lines (in report mode)
117 static const int LINE_SPACING
= 0;
119 // extra margins around the text label
120 static const int EXTRA_WIDTH
= 3;
121 static const int EXTRA_HEIGHT
= 4;
123 // offset for the header window
124 static const int HEADER_OFFSET_X
= 1;
125 static const int HEADER_OFFSET_Y
= 1;
127 // when autosizing the columns, add some slack
128 static const int AUTOSIZE_COL_MARGIN
= 10;
130 // default and minimal widths for the header columns
131 static const int WIDTH_COL_DEFAULT
= 80;
132 static const int WIDTH_COL_MIN
= 10;
134 // the space between the image and the text in the report mode
135 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
137 // ============================================================================
139 // ============================================================================
141 //-----------------------------------------------------------------------------
142 // wxListItemData (internal)
143 //-----------------------------------------------------------------------------
145 class WXDLLEXPORT wxListItemData
148 wxListItemData(wxListMainWindow
*owner
);
151 void SetItem( const wxListItem
&info
);
152 void SetImage( int image
) { m_image
= image
; }
153 void SetData( long data
) { m_data
= data
; }
154 void SetPosition( int x
, int y
);
155 void SetSize( int width
, int height
);
157 bool HasText() const { return !m_text
.empty(); }
158 const wxString
& GetText() const { return m_text
; }
159 void SetText(const wxString
& text
) { m_text
= text
; }
161 // we can't use empty string for measuring the string width/height, so
162 // always return something
163 wxString
GetTextForMeasuring() const
165 wxString s
= GetText();
172 bool IsHit( int x
, int y
) const;
176 int GetWidth() const;
177 int GetHeight() const;
179 int GetImage() const { return m_image
; }
180 bool HasImage() const { return GetImage() != -1; }
182 void GetItem( wxListItem
&info
) const;
184 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
185 wxListItemAttr
*GetAttr() const { return m_attr
; }
188 // the item image or -1
191 // user data associated with the item
194 // the item coordinates are not used in report mode, instead this pointer
195 // is NULL and the owner window is used to retrieve the item position and
199 // the list ctrl we are in
200 wxListMainWindow
*m_owner
;
202 // custom attributes or NULL
203 wxListItemAttr
*m_attr
;
206 // common part of all ctors
212 //-----------------------------------------------------------------------------
213 // wxListHeaderData (internal)
214 //-----------------------------------------------------------------------------
216 class WXDLLEXPORT wxListHeaderData
: public wxObject
220 wxListHeaderData( const wxListItem
&info
);
221 void SetItem( const wxListItem
&item
);
222 void SetPosition( int x
, int y
);
223 void SetWidth( int w
);
224 void SetFormat( int format
);
225 void SetHeight( int h
);
226 bool HasImage() const;
228 bool HasText() const { return !m_text
.empty(); }
229 const wxString
& GetText() const { return m_text
; }
230 void SetText(const wxString
& text
) { m_text
= text
; }
232 void GetItem( wxListItem
&item
);
234 bool IsHit( int x
, int y
) const;
235 int GetImage() const;
236 int GetWidth() const;
237 int GetFormat() const;
253 //-----------------------------------------------------------------------------
254 // wxListLineData (internal)
255 //-----------------------------------------------------------------------------
257 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
258 #include "wx/listimpl.cpp"
259 WX_DEFINE_LIST(wxListItemDataList
);
261 class WXDLLEXPORT wxListLineData
264 // the list of subitems: only may have more than one item in report mode
265 wxListItemDataList m_items
;
267 // this is not used in report view
279 // the part to be highlighted
280 wxRect m_rectHighlight
;
283 // is this item selected? [NB: not used in virtual mode]
286 // back pointer to the list ctrl
287 wxListMainWindow
*m_owner
;
290 wxListLineData(wxListMainWindow
*owner
);
292 ~wxListLineData() { delete m_gi
; }
294 // are we in report mode?
295 inline bool InReportView() const;
297 // are we in virtual report mode?
298 inline bool IsVirtual() const;
300 // these 2 methods shouldn't be called for report view controls, in that
301 // case we determine our position/size ourselves
303 // calculate the size of the line
304 void CalculateSize( wxDC
*dc
, int spacing
);
306 // remember the position this line appears at
307 void SetPosition( int x
, int y
, int window_width
, int spacing
);
311 void SetImage( int image
) { SetImage(0, image
); }
312 int GetImage() const { return GetImage(0); }
313 bool HasImage() const { return GetImage() != -1; }
314 bool HasText() const { return !GetText(0).empty(); }
316 void SetItem( int index
, const wxListItem
&info
);
317 void GetItem( int index
, wxListItem
&info
);
319 wxString
GetText(int index
) const;
320 void SetText( int index
, const wxString s
);
322 wxListItemAttr
*GetAttr() const;
323 void SetAttr(wxListItemAttr
*attr
);
325 // return true if the highlighting really changed
326 bool Highlight( bool on
);
328 void ReverseHighlight();
330 bool IsHighlighted() const
332 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
334 return m_highlighted
;
337 // draw the line on the given DC in icon/list mode
338 void Draw( wxDC
*dc
);
340 // the same in report mode
341 void DrawInReportMode( wxDC
*dc
,
343 const wxRect
& rectHL
,
347 // set the line to contain num items (only can be > 1 in report mode)
348 void InitItems( int num
);
350 // get the mode (i.e. style) of the list control
351 inline int GetMode() const;
353 // prepare the DC for drawing with these item's attributes, return true if
354 // we need to draw the items background to highlight it, false otherwise
355 bool SetAttributes(wxDC
*dc
,
356 const wxListItemAttr
*attr
,
359 // draw the text on the DC with the correct justification; also add an
360 // ellipsis if the text is too large to fit in the current width
361 void DrawTextFormatted(wxDC
*dc
, const wxString
&text
, int col
, int x
, int y
, int width
);
363 // these are only used by GetImage/SetImage above, we don't support images
364 // with subitems at the public API level yet
365 void SetImage( int index
, int image
);
366 int GetImage( int index
) const;
369 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
370 #include "wx/arrimpl.cpp"
371 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
373 //-----------------------------------------------------------------------------
374 // wxListHeaderWindow (internal)
375 //-----------------------------------------------------------------------------
377 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
380 wxListMainWindow
*m_owner
;
381 wxCursor
*m_currentCursor
;
382 wxCursor
*m_resizeCursor
;
385 // column being resized or -1
388 // divider line position in logical (unscrolled) coords
391 // minimal position beyond which the divider line can't be dragged in
396 wxListHeaderWindow();
398 wxListHeaderWindow( wxWindow
*win
,
400 wxListMainWindow
*owner
,
401 const wxPoint
&pos
= wxDefaultPosition
,
402 const wxSize
&size
= wxDefaultSize
,
404 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
406 virtual ~wxListHeaderWindow();
408 void DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
);
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 WXDLLEXPORT 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
)
1093 m_items
.DeleteContents( TRUE
);
1095 if ( InReportView() )
1101 m_gi
= new GeometryInfo
;
1104 m_highlighted
= FALSE
;
1106 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1109 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1111 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1112 wxCHECK_RET( node
, _T("no subitems at all??") );
1114 wxListItemData
*item
= node
->GetData();
1116 switch ( GetMode() )
1119 case wxLC_SMALL_ICON
:
1121 m_gi
->m_rectAll
.width
= spacing
;
1123 wxString s
= item
->GetText();
1129 m_gi
->m_rectLabel
.width
=
1130 m_gi
->m_rectLabel
.height
= 0;
1134 dc
->GetTextExtent( s
, &lw
, &lh
);
1135 if (lh
< SCROLL_UNIT_Y
)
1140 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1142 m_gi
->m_rectAll
.width
= lw
;
1144 m_gi
->m_rectLabel
.width
= lw
;
1145 m_gi
->m_rectLabel
.height
= lh
;
1148 if (item
->HasImage())
1151 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1152 m_gi
->m_rectIcon
.width
= w
+ 8;
1153 m_gi
->m_rectIcon
.height
= h
+ 8;
1155 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1156 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1157 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1158 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1161 if ( item
->HasText() )
1163 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1164 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1166 else // no text, highlight the icon
1168 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1169 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1176 wxString s
= item
->GetTextForMeasuring();
1179 dc
->GetTextExtent( s
, &lw
, &lh
);
1180 if (lh
< SCROLL_UNIT_Y
)
1185 m_gi
->m_rectLabel
.width
= lw
;
1186 m_gi
->m_rectLabel
.height
= lh
;
1188 m_gi
->m_rectAll
.width
= lw
;
1189 m_gi
->m_rectAll
.height
= lh
;
1191 if (item
->HasImage())
1194 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1195 m_gi
->m_rectIcon
.width
= w
;
1196 m_gi
->m_rectIcon
.height
= h
;
1198 m_gi
->m_rectAll
.width
+= 4 + w
;
1199 if (h
> m_gi
->m_rectAll
.height
)
1200 m_gi
->m_rectAll
.height
= h
;
1203 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1204 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1209 wxFAIL_MSG( _T("unexpected call to SetSize") );
1213 wxFAIL_MSG( _T("unknown mode") );
1217 void wxListLineData::SetPosition( int x
, int y
,
1221 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1222 wxCHECK_RET( node
, _T("no subitems at all??") );
1224 wxListItemData
*item
= node
->GetData();
1226 switch ( GetMode() )
1229 case wxLC_SMALL_ICON
:
1230 m_gi
->m_rectAll
.x
= x
;
1231 m_gi
->m_rectAll
.y
= y
;
1233 if ( item
->HasImage() )
1235 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1236 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1237 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1240 if ( item
->HasText() )
1242 if (m_gi
->m_rectAll
.width
> spacing
)
1243 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1245 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1246 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1247 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1248 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1250 else // no text, highlight the icon
1252 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1253 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1258 m_gi
->m_rectAll
.x
= x
;
1259 m_gi
->m_rectAll
.y
= y
;
1261 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1262 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1263 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1265 if (item
->HasImage())
1267 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1268 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1269 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1273 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1278 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1282 wxFAIL_MSG( _T("unknown mode") );
1286 void wxListLineData::InitItems( int num
)
1288 for (int i
= 0; i
< num
; i
++)
1289 m_items
.Append( new wxListItemData(m_owner
) );
1292 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1294 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1295 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1297 wxListItemData
*item
= node
->GetData();
1298 item
->SetItem( info
);
1301 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1303 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1306 wxListItemData
*item
= node
->GetData();
1307 item
->GetItem( info
);
1311 wxString
wxListLineData::GetText(int index
) const
1315 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1318 wxListItemData
*item
= node
->GetData();
1319 s
= item
->GetText();
1325 void wxListLineData::SetText( int index
, const wxString s
)
1327 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1330 wxListItemData
*item
= node
->GetData();
1335 void wxListLineData::SetImage( int index
, int image
)
1337 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1338 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1340 wxListItemData
*item
= node
->GetData();
1341 item
->SetImage(image
);
1344 int wxListLineData::GetImage( int index
) const
1346 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1347 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1349 wxListItemData
*item
= node
->GetData();
1350 return item
->GetImage();
1353 wxListItemAttr
*wxListLineData::GetAttr() const
1355 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1356 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1358 wxListItemData
*item
= node
->GetData();
1359 return item
->GetAttr();
1362 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1364 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1365 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1367 wxListItemData
*item
= node
->GetData();
1368 item
->SetAttr(attr
);
1371 bool wxListLineData::SetAttributes(wxDC
*dc
,
1372 const wxListItemAttr
*attr
,
1375 wxWindow
*listctrl
= m_owner
->GetParent();
1379 // don't use foreground colour for drawing highlighted items - this might
1380 // make them completely invisible (and there is no way to do bit
1381 // arithmetics on wxColour, unfortunately)
1385 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1389 if ( attr
&& attr
->HasTextColour() )
1391 colText
= attr
->GetTextColour();
1395 colText
= listctrl
->GetForegroundColour();
1399 dc
->SetTextForeground(colText
);
1403 if ( attr
&& attr
->HasFont() )
1405 font
= attr
->GetFont();
1409 font
= listctrl
->GetFont();
1415 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1416 if ( highlighted
|| hasBgCol
)
1420 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1424 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1427 dc
->SetPen( *wxTRANSPARENT_PEN
);
1435 void wxListLineData::Draw( wxDC
*dc
)
1437 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1438 wxCHECK_RET( node
, _T("no subitems at all??") );
1440 bool highlighted
= IsHighlighted();
1442 wxListItemAttr
*attr
= GetAttr();
1444 if ( SetAttributes(dc
, attr
, highlighted
) )
1446 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1449 wxListItemData
*item
= node
->GetData();
1450 if (item
->HasImage())
1452 wxRect rectIcon
= m_gi
->m_rectIcon
;
1453 m_owner
->DrawImage( item
->GetImage(), dc
,
1454 rectIcon
.x
, rectIcon
.y
);
1457 if (item
->HasText())
1459 wxRect rectLabel
= m_gi
->m_rectLabel
;
1461 wxDCClipper
clipper(*dc
, rectLabel
);
1462 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1466 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1468 const wxRect
& rectHL
,
1471 // TODO: later we should support setting different attributes for
1472 // different columns - to do it, just add "col" argument to
1473 // GetAttr() and move these lines into the loop below
1474 wxListItemAttr
*attr
= GetAttr();
1475 if ( SetAttributes(dc
, attr
, highlighted
) )
1477 dc
->DrawRectangle( rectHL
);
1480 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1481 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1484 for ( wxListItemDataList::Node
*node
= m_items
.GetFirst();
1486 node
= node
->GetNext(), col
++ )
1488 wxListItemData
*item
= node
->GetData();
1490 int width
= m_owner
->GetColumnWidth(col
);
1494 if ( item
->HasImage() )
1497 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1498 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1500 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1506 wxDCClipper
clipper(*dc
, xOld
, y
, width
- 8, rect
.height
);
1508 if ( item
->HasText() )
1510 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, y
, width
- 8);
1515 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1516 const wxString
&text
,
1522 wxString drawntext
, ellipsis
;
1523 wxCoord w
, h
, base_w
;
1526 // determine if the string can fit inside the current width
1527 dc
->GetTextExtent(text
, &w
, &h
);
1530 // it can, draw it using the items alignment
1531 m_owner
->GetColumn(col
, item
);
1532 switch ( item
.GetAlign() )
1535 wxFAIL_MSG( _T("unknown list item format") );
1538 case wxLIST_FORMAT_LEFT
:
1542 case wxLIST_FORMAT_RIGHT
:
1546 case wxLIST_FORMAT_CENTER
:
1547 x
+= (width
- w
) / 2;
1551 dc
->DrawText(text
, x
, y
);
1553 else // otherwise, truncate and add an ellipsis if possible
1555 // determine the base width
1556 ellipsis
= wxString(wxT("..."));
1557 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1559 // continue until we have enough space or only one character left
1561 size_t len
= text
.Length();
1562 drawntext
= text
.Left(len
);
1565 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1566 drawntext
.RemoveLast();
1569 if (w
+ base_w
<= width
)
1573 // if still not enough space, remove ellipsis characters
1574 while (ellipsis
.Length() > 0 && w
+ base_w
> width
)
1576 ellipsis
= ellipsis
.Left(ellipsis
.Length() - 1);
1577 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1580 // now draw the text
1581 dc
->DrawText(drawntext
, x
, y
);
1582 dc
->DrawText(ellipsis
, x
+ w
, y
);
1586 bool wxListLineData::Highlight( bool on
)
1588 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1590 if ( on
== m_highlighted
)
1598 void wxListLineData::ReverseHighlight( void )
1600 Highlight(!IsHighlighted());
1603 //-----------------------------------------------------------------------------
1604 // wxListHeaderWindow
1605 //-----------------------------------------------------------------------------
1607 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1609 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1610 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1611 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1612 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1615 void wxListHeaderWindow::Init()
1617 m_currentCursor
= (wxCursor
*) NULL
;
1618 m_isDragging
= FALSE
;
1622 wxListHeaderWindow::wxListHeaderWindow()
1626 m_owner
= (wxListMainWindow
*) NULL
;
1627 m_resizeCursor
= (wxCursor
*) NULL
;
1630 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1632 wxListMainWindow
*owner
,
1636 const wxString
&name
)
1637 : wxWindow( win
, id
, pos
, size
, style
, name
)
1642 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1644 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE
) );
1647 wxListHeaderWindow::~wxListHeaderWindow()
1649 delete m_resizeCursor
;
1652 #ifdef __WXUNIVERSAL__
1653 #include "wx/univ/renderer.h"
1654 #include "wx/univ/theme.h"
1657 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1659 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
1660 GtkStateType state
= m_parent
->IsEnabled() ? GTK_STATE_NORMAL
1661 : GTK_STATE_INSENSITIVE
;
1663 x
= dc
->XLOG2DEV( x
);
1665 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
,
1666 state
, GTK_SHADOW_OUT
,
1667 (GdkRectangle
*) NULL
, m_wxwindow
,
1668 (char *)"button", // const_cast
1669 x
-1, y
-1, w
+2, h
+2);
1670 #elif defined(__WXUNIVERSAL__)
1671 wxTheme
*theme
= wxTheme::Get();
1672 wxRenderer
*renderer
= theme
->GetRenderer();
1673 renderer
->DrawBorder( *dc
, wxBORDER_RAISED
, wxRect(x
,y
,w
,h
), 0 );
1674 #elif defined(__WXMAC__)
1675 const int m_corner
= 1;
1677 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1679 dc
->SetPen( wxPen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1680 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1681 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1683 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1686 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1687 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1689 dc
->SetPen( *wxWHITE_PEN
);
1690 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1691 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1692 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1693 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1695 const int m_corner
= 1;
1697 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1699 dc
->SetPen( *wxBLACK_PEN
);
1700 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1701 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1703 wxPen
pen( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1706 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1707 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1709 dc
->SetPen( *wxWHITE_PEN
);
1710 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1711 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1712 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1713 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1717 // shift the DC origin to match the position of the main window horz
1718 // scrollbar: this allows us to always use logical coords
1719 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1722 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1725 m_owner
->GetViewStart( &x
, NULL
);
1727 // account for the horz scrollbar offset
1728 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1731 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1733 #if defined(__WXGTK__)
1734 wxClientDC
dc( this );
1736 wxPaintDC
dc( this );
1744 dc
.SetFont( GetFont() );
1746 // width and height of the entire header window
1748 GetClientSize( &w
, &h
);
1749 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1751 dc
.SetBackgroundMode(wxTRANSPARENT
);
1753 // do *not* use the listctrl colour for headers - one day we will have a
1754 // function to set it separately
1755 //dc.SetTextForeground( *wxBLACK );
1756 dc
.SetTextForeground(wxSystemSettings::
1757 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1759 int x
= HEADER_OFFSET_X
;
1761 int numColumns
= m_owner
->GetColumnCount();
1763 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1765 m_owner
->GetColumn( i
, item
);
1766 int wCol
= item
.m_width
;
1768 // the width of the rect to draw: make it smaller to fit entirely
1769 // inside the column rect
1772 dc
.SetPen( *wxWHITE_PEN
);
1774 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
1776 // see if we have enough space for the column label
1778 // for this we need the width of the text
1780 dc
.GetTextExtent(item
.GetText(), &wLabel
, NULL
);
1781 wLabel
+= 2*EXTRA_WIDTH
;
1783 // and the width of the icon, if any
1784 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
1785 int ix
= 0, // init them just to suppress the compiler warnings
1787 const int image
= item
.m_image
;
1788 wxImageListType
*imageList
;
1791 imageList
= m_owner
->m_small_image_list
;
1794 imageList
->GetSize(image
, ix
, iy
);
1795 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1803 // ignore alignment if there is not enough space anyhow
1805 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1808 wxFAIL_MSG( _T("unknown list item format") );
1811 case wxLIST_FORMAT_LEFT
:
1815 case wxLIST_FORMAT_RIGHT
:
1816 xAligned
= x
+ cw
- wLabel
;
1819 case wxLIST_FORMAT_CENTER
:
1820 xAligned
= x
+ (cw
- wLabel
) / 2;
1825 // if we have an image, draw it on the right of the label
1832 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
1833 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1834 wxIMAGELIST_DRAW_TRANSPARENT
1837 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1840 // draw the text clipping it so that it doesn't overwrite the column
1842 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1844 dc
.DrawText( item
.GetText(),
1845 xAligned
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1853 void wxListHeaderWindow::DrawCurrent()
1855 int x1
= m_currentX
;
1857 m_owner
->ClientToScreen( &x1
, &y1
);
1859 int x2
= m_currentX
;
1861 m_owner
->GetClientSize( NULL
, &y2
);
1862 m_owner
->ClientToScreen( &x2
, &y2
);
1865 dc
.SetLogicalFunction( wxINVERT
);
1866 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1867 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1871 dc
.DrawLine( x1
, y1
, x2
, y2
);
1873 dc
.SetLogicalFunction( wxCOPY
);
1875 dc
.SetPen( wxNullPen
);
1876 dc
.SetBrush( wxNullBrush
);
1879 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1881 // we want to work with logical coords
1883 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1884 int y
= event
.GetY();
1888 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1890 // we don't draw the line beyond our window, but we allow dragging it
1893 GetClientSize( &w
, NULL
);
1894 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1897 // erase the line if it was drawn
1898 if ( m_currentX
< w
)
1901 if (event
.ButtonUp())
1904 m_isDragging
= FALSE
;
1906 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1907 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1914 m_currentX
= m_minX
+ 7;
1916 // draw in the new location
1917 if ( m_currentX
< w
)
1921 else // not dragging
1924 bool hit_border
= FALSE
;
1926 // end of the current column
1929 // find the column where this event occured
1931 countCol
= m_owner
->GetColumnCount();
1932 for (col
= 0; col
< countCol
; col
++)
1934 xpos
+= m_owner
->GetColumnWidth( col
);
1937 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1939 // near the column border
1946 // inside the column
1953 if ( col
== countCol
)
1956 if (event
.LeftDown() || event
.RightUp())
1958 if (hit_border
&& event
.LeftDown())
1960 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1961 event
.GetPosition()) )
1963 m_isDragging
= TRUE
;
1968 //else: column resizing was vetoed by the user code
1970 else // click on a column
1972 SendListEvent( event
.LeftDown()
1973 ? wxEVT_COMMAND_LIST_COL_CLICK
1974 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1975 event
.GetPosition());
1978 else if (event
.Moving())
1983 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1984 m_currentCursor
= m_resizeCursor
;
1988 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1989 m_currentCursor
= wxSTANDARD_CURSOR
;
1993 SetCursor(*m_currentCursor
);
1998 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2000 m_owner
->SetFocus();
2003 bool wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
2005 wxWindow
*parent
= GetParent();
2006 wxListEvent
le( type
, parent
->GetId() );
2007 le
.SetEventObject( parent
);
2008 le
.m_pointDrag
= pos
;
2010 // the position should be relative to the parent window, not
2011 // this one for compatibility with MSW and common sense: the
2012 // user code doesn't know anything at all about this header
2013 // window, so why should it get positions relative to it?
2014 le
.m_pointDrag
.y
-= GetSize().y
;
2016 le
.m_col
= m_column
;
2017 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
2020 //-----------------------------------------------------------------------------
2021 // wxListRenameTimer (internal)
2022 //-----------------------------------------------------------------------------
2024 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
2029 void wxListRenameTimer::Notify()
2031 m_owner
->OnRenameTimer();
2034 //-----------------------------------------------------------------------------
2035 // wxListTextCtrl (internal)
2036 //-----------------------------------------------------------------------------
2038 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2039 EVT_CHAR (wxListTextCtrl::OnChar
)
2040 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2041 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2044 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
2045 : m_startValue(owner
->GetItemText(itemEdit
)),
2046 m_itemEdited(itemEdit
)
2051 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2053 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2054 &rectLabel
.x
, &rectLabel
.y
);
2056 (void)Create(owner
, wxID_ANY
, m_startValue
,
2057 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2058 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2061 void wxListTextCtrl::Finish()
2065 wxPendingDelete
.Append(this);
2069 m_owner
->SetFocus();
2073 bool wxListTextCtrl::AcceptChanges()
2075 const wxString value
= GetValue();
2077 if ( value
== m_startValue
)
2079 // nothing changed, always accept
2083 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2085 // vetoed by the user
2089 // accepted, do rename the item
2090 m_owner
->SetItemText(m_itemEdited
, value
);
2095 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2097 switch ( event
.m_keyCode
)
2100 if ( !AcceptChanges() )
2102 // vetoed by the user code
2105 //else: fall through
2109 m_owner
->OnRenameCancelled( m_itemEdited
);
2117 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2125 // auto-grow the textctrl:
2126 wxSize parentSize
= m_owner
->GetSize();
2127 wxPoint myPos
= GetPosition();
2128 wxSize mySize
= GetSize();
2130 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2131 if (myPos
.x
+ sx
> parentSize
.x
)
2132 sx
= parentSize
.x
- myPos
.x
;
2140 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2144 // We must finish regardless of success, otherwise we'll get focus problems
2147 if ( !AcceptChanges() )
2148 m_owner
->OnRenameCancelled( m_itemEdited
);
2154 //-----------------------------------------------------------------------------
2156 //-----------------------------------------------------------------------------
2158 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2160 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2161 EVT_PAINT (wxListMainWindow::OnPaint
)
2162 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2163 EVT_CHAR (wxListMainWindow::OnChar
)
2164 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2165 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2166 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2167 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2170 void wxListMainWindow::Init()
2172 m_columns
.DeleteContents( TRUE
);
2176 m_lineTo
= (size_t)-1;
2182 m_small_image_list
= (wxImageListType
*) NULL
;
2183 m_normal_image_list
= (wxImageListType
*) NULL
;
2185 m_small_spacing
= 30;
2186 m_normal_spacing
= 40;
2190 m_isCreated
= FALSE
;
2192 m_lastOnSame
= FALSE
;
2193 m_renameTimer
= new wxListRenameTimer( this );
2197 m_lineBeforeLastClicked
= (size_t)-1;
2202 void wxListMainWindow::InitScrolling()
2204 if ( HasFlag(wxLC_REPORT
) )
2206 m_xScroll
= SCROLL_UNIT_X
;
2207 m_yScroll
= SCROLL_UNIT_Y
;
2211 m_xScroll
= SCROLL_UNIT_Y
;
2216 wxListMainWindow::wxListMainWindow()
2221 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2227 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2232 const wxString
&name
)
2233 : wxScrolledWindow( parent
, id
, pos
, size
,
2234 style
| wxHSCROLL
| wxVSCROLL
, name
)
2238 m_highlightBrush
= new wxBrush
2240 wxSystemSettings::GetColour
2242 wxSYS_COLOUR_HIGHLIGHT
2247 m_highlightUnfocusedBrush
= new wxBrush
2249 wxSystemSettings::GetColour
2251 wxSYS_COLOUR_BTNSHADOW
2260 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
2262 SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX
) );
2265 wxListMainWindow::~wxListMainWindow()
2269 delete m_highlightBrush
;
2270 delete m_highlightUnfocusedBrush
;
2272 delete m_renameTimer
;
2275 void wxListMainWindow::CacheLineData(size_t line
)
2277 wxGenericListCtrl
*listctrl
= GetListCtrl();
2279 wxListLineData
*ld
= GetDummyLine();
2281 size_t countCol
= GetColumnCount();
2282 for ( size_t col
= 0; col
< countCol
; col
++ )
2284 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2287 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2288 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2291 wxListLineData
*wxListMainWindow::GetDummyLine() const
2293 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2295 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2297 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2299 // we need to recreate the dummy line if the number of columns in the
2300 // control changed as it would have the incorrect number of fields
2302 if ( !m_lines
.IsEmpty() &&
2303 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2305 self
->m_lines
.Clear();
2308 if ( m_lines
.IsEmpty() )
2310 wxListLineData
*line
= new wxListLineData(self
);
2311 self
->m_lines
.Add(line
);
2313 // don't waste extra memory -- there never going to be anything
2314 // else/more in this array
2315 self
->m_lines
.Shrink();
2321 // ----------------------------------------------------------------------------
2322 // line geometry (report mode only)
2323 // ----------------------------------------------------------------------------
2325 wxCoord
wxListMainWindow::GetLineHeight() const
2327 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2329 // we cache the line height as calling GetTextExtent() is slow
2330 if ( !m_lineHeight
)
2332 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2334 wxClientDC
dc( self
);
2335 dc
.SetFont( GetFont() );
2338 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2340 if ( y
< SCROLL_UNIT_Y
)
2343 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2347 m_small_image_list
->GetSize(0, iw
, ih
);
2352 self
->m_lineHeight
= y
+ LINE_SPACING
;
2355 return m_lineHeight
;
2358 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2360 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
2362 return LINE_SPACING
+ line
*GetLineHeight();
2365 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2367 if ( !InReportView() )
2368 return GetLine(line
)->m_gi
->m_rectAll
;
2371 rect
.x
= HEADER_OFFSET_X
;
2372 rect
.y
= GetLineY(line
);
2373 rect
.width
= GetHeaderWidth();
2374 rect
.height
= GetLineHeight();
2379 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2381 if ( !InReportView() )
2382 return GetLine(line
)->m_gi
->m_rectLabel
;
2385 rect
.x
= HEADER_OFFSET_X
;
2386 rect
.y
= GetLineY(line
);
2387 rect
.width
= GetColumnWidth(0);
2388 rect
.height
= GetLineHeight();
2393 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2395 if ( !InReportView() )
2396 return GetLine(line
)->m_gi
->m_rectIcon
;
2398 wxListLineData
*ld
= GetLine(line
);
2399 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2402 rect
.x
= HEADER_OFFSET_X
;
2403 rect
.y
= GetLineY(line
);
2404 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2409 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2411 return InReportView() ? GetLineRect(line
)
2412 : GetLine(line
)->m_gi
->m_rectHighlight
;
2415 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2417 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2419 wxListLineData
*ld
= GetLine(line
);
2421 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2422 return wxLIST_HITTEST_ONITEMICON
;
2424 // VS: Testing for "ld->HasText() || InReportView()" instead of
2425 // "ld->HasText()" is needed to make empty lines in report view
2427 if ( ld
->HasText() || InReportView() )
2429 wxRect rect
= InReportView() ? GetLineRect(line
)
2430 : GetLineLabelRect(line
);
2432 if ( rect
.Inside(x
, y
) )
2433 return wxLIST_HITTEST_ONITEMLABEL
;
2439 // ----------------------------------------------------------------------------
2440 // highlight (selection) handling
2441 // ----------------------------------------------------------------------------
2443 bool wxListMainWindow::IsHighlighted(size_t line
) const
2447 return m_selStore
.IsSelected(line
);
2451 wxListLineData
*ld
= GetLine(line
);
2452 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2454 return ld
->IsHighlighted();
2458 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2464 wxArrayInt linesChanged
;
2465 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2468 // meny items changed state, refresh everything
2469 RefreshLines(lineFrom
, lineTo
);
2471 else // only a few items changed state, refresh only them
2473 size_t count
= linesChanged
.GetCount();
2474 for ( size_t n
= 0; n
< count
; n
++ )
2476 RefreshLine(linesChanged
[n
]);
2480 else // iterate over all items in non report view
2482 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2484 if ( HighlightLine(line
, highlight
) )
2492 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2498 changed
= m_selStore
.SelectItem(line
, highlight
);
2502 wxListLineData
*ld
= GetLine(line
);
2503 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2505 changed
= ld
->Highlight(highlight
);
2510 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2511 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2517 void wxListMainWindow::RefreshLine( size_t line
)
2519 if ( HasFlag(wxLC_REPORT
) )
2521 size_t visibleFrom
, visibleTo
;
2522 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2524 if ( line
< visibleFrom
|| line
> visibleTo
)
2528 wxRect rect
= GetLineRect(line
);
2530 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2531 RefreshRect( rect
);
2534 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2536 // we suppose that they are ordered by caller
2537 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2539 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2541 if ( HasFlag(wxLC_REPORT
) )
2543 size_t visibleFrom
, visibleTo
;
2544 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2546 if ( lineFrom
< visibleFrom
)
2547 lineFrom
= visibleFrom
;
2548 if ( lineTo
> visibleTo
)
2553 rect
.y
= GetLineY(lineFrom
);
2554 rect
.width
= GetClientSize().x
;
2555 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2557 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2558 RefreshRect( rect
);
2562 // TODO: this should be optimized...
2563 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2570 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2572 if ( HasFlag(wxLC_REPORT
) )
2574 size_t visibleFrom
, visibleTo
;
2575 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2577 if ( lineFrom
< visibleFrom
)
2578 lineFrom
= visibleFrom
;
2579 else if ( lineFrom
> visibleTo
)
2584 rect
.y
= GetLineY(lineFrom
);
2585 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2587 wxSize size
= GetClientSize();
2588 rect
.width
= size
.x
;
2589 // refresh till the bottom of the window
2590 rect
.height
= size
.y
- rect
.y
;
2592 RefreshRect( rect
);
2596 // TODO: how to do it more efficiently?
2601 void wxListMainWindow::RefreshSelected()
2607 if ( InReportView() )
2609 GetVisibleLinesRange(&from
, &to
);
2614 to
= GetItemCount() - 1;
2617 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2619 RefreshLine(m_current
);
2622 for ( size_t line
= from
; line
<= to
; line
++ )
2624 // NB: the test works as expected even if m_current == -1
2625 if ( line
!= m_current
&& IsHighlighted(line
) )
2632 void wxListMainWindow::Freeze()
2637 void wxListMainWindow::Thaw()
2639 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2641 if ( !--m_freezeCount
)
2647 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2649 // Note: a wxPaintDC must be constructed even if no drawing is
2650 // done (a Windows requirement).
2651 wxPaintDC
dc( this );
2653 if ( IsEmpty() || m_freezeCount
)
2655 // nothing to draw or not the moment to draw it
2661 // delay the repainting until we calculate all the items positions
2668 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2672 dc
.SetFont( GetFont() );
2674 if ( HasFlag(wxLC_REPORT
) )
2676 int lineHeight
= GetLineHeight();
2678 size_t visibleFrom
, visibleTo
;
2679 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2682 wxCoord xOrig
, yOrig
;
2683 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2685 // tell the caller cache to cache the data
2688 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2689 GetParent()->GetId());
2690 evCache
.SetEventObject( GetParent() );
2691 evCache
.m_oldItemIndex
= visibleFrom
;
2692 evCache
.m_itemIndex
= visibleTo
;
2693 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2696 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2698 rectLine
= GetLineRect(line
);
2700 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2701 rectLine
.width
, rectLine
.height
) )
2703 // don't redraw unaffected lines to avoid flicker
2707 GetLine(line
)->DrawInReportMode( &dc
,
2709 GetLineHighlightRect(line
),
2710 IsHighlighted(line
) );
2713 if ( HasFlag(wxLC_HRULES
) )
2715 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2716 wxSize clientSize
= GetClientSize();
2718 // Don't draw the first one
2719 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2722 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2723 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2724 clientSize
.x
- dev_x
, i
*lineHeight
);
2727 // Draw last horizontal rule
2728 if ( visibleTo
== GetItemCount() - 1 )
2731 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2732 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2733 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2737 // Draw vertical rules if required
2738 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2740 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2743 wxRect firstItemRect
;
2744 wxRect lastItemRect
;
2745 GetItemRect(visibleFrom
, firstItemRect
);
2746 GetItemRect(visibleTo
, lastItemRect
);
2747 int x
= firstItemRect
.GetX();
2749 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2750 for (col
= 0; col
< GetColumnCount(); col
++)
2752 int colWidth
= GetColumnWidth(col
);
2754 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2755 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2761 size_t count
= GetItemCount();
2762 for ( size_t i
= 0; i
< count
; i
++ )
2764 GetLine(i
)->Draw( &dc
);
2770 // don't draw rect outline under Max if we already have the background
2771 // color but under other platforms only draw it if we do: it is a bit
2772 // silly to draw "focus rect" if we don't have focus!
2777 #endif // __WXMAC__/!__WXMAC__
2779 dc
.SetPen( *wxBLACK_PEN
);
2780 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2781 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2788 void wxListMainWindow::HighlightAll( bool on
)
2790 if ( IsSingleSel() )
2792 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2794 // we just have one item to turn off
2795 if ( HasCurrent() && IsHighlighted(m_current
) )
2797 HighlightLine(m_current
, FALSE
);
2798 RefreshLine(m_current
);
2803 HighlightLines(0, GetItemCount() - 1, on
);
2807 void wxListMainWindow::SendNotify( size_t line
,
2808 wxEventType command
,
2811 wxListEvent
le( command
, GetParent()->GetId() );
2812 le
.SetEventObject( GetParent() );
2813 le
.m_itemIndex
= line
;
2815 // set only for events which have position
2816 if ( point
!= wxDefaultPosition
)
2817 le
.m_pointDrag
= point
;
2819 // don't try to get the line info for virtual list controls: the main
2820 // program has it anyhow and if we did it would result in accessing all
2821 // the lines, even those which are not visible now and this is precisely
2822 // what we're trying to avoid
2823 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2825 if ( line
!= (size_t)-1 )
2827 GetLine(line
)->GetItem( 0, le
.m_item
);
2829 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2831 //else: there may be no more such item
2833 GetParent()->GetEventHandler()->ProcessEvent( le
);
2836 void wxListMainWindow::ChangeCurrent(size_t current
)
2838 m_current
= current
;
2840 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2843 void wxListMainWindow::EditLabel( long item
)
2845 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2846 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2848 size_t itemEdit
= (size_t)item
;
2850 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2851 le
.SetEventObject( GetParent() );
2852 le
.m_itemIndex
= item
;
2853 wxListLineData
*data
= GetLine(itemEdit
);
2854 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2855 data
->GetItem( 0, le
.m_item
);
2856 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2858 // vetoed by user code
2862 // We have to call this here because the label in question might just have
2863 // been added and no screen update taken place.
2867 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2872 void wxListMainWindow::OnRenameTimer()
2874 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2876 EditLabel( m_current
);
2879 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2881 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2882 le
.SetEventObject( GetParent() );
2883 le
.m_itemIndex
= itemEdit
;
2885 wxListLineData
*data
= GetLine(itemEdit
);
2886 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
2888 data
->GetItem( 0, le
.m_item
);
2889 le
.m_item
.m_text
= value
;
2890 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2894 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2896 // wxMSW seems not to notify the program about
2897 // cancelled label edits.
2900 // let owner know that the edit was cancelled
2901 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2903 // These only exist for wxTreeCtrl, which should probably be changed
2904 // le.m_editCancelled = TRUE;
2905 // le.m_label = wxEmptyString;
2907 le
.SetEventObject( GetParent() );
2908 le
.m_itemIndex
= itemEdit
;
2910 wxListLineData
*data
= GetLine(itemEdit
);
2911 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2913 data
->GetItem( 0, le
.m_item
);
2915 GetEventHandler()->ProcessEvent( le
);
2918 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2920 event
.SetEventObject( GetParent() );
2921 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2924 if ( !HasCurrent() || IsEmpty() )
2930 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2931 event
.ButtonDClick()) )
2934 int x
= event
.GetX();
2935 int y
= event
.GetY();
2936 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2938 // where did we hit it (if we did)?
2941 size_t count
= GetItemCount(),
2944 if ( HasFlag(wxLC_REPORT
) )
2946 current
= y
/ GetLineHeight();
2947 if ( current
< count
)
2948 hitResult
= HitTestLine(current
, x
, y
);
2952 // TODO: optimize it too! this is less simple than for report view but
2953 // enumerating all items is still not a way to do it!!
2954 for ( current
= 0; current
< count
; current
++ )
2956 hitResult
= HitTestLine(current
, x
, y
);
2962 if (event
.Dragging())
2964 if (m_dragCount
== 0)
2966 // we have to report the raw, physical coords as we want to be
2967 // able to call HitTest(event.m_pointDrag) from the user code to
2968 // get the item being dragged
2969 m_dragStart
= event
.GetPosition();
2974 if (m_dragCount
!= 3)
2977 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2978 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2980 wxListEvent
le( command
, GetParent()->GetId() );
2981 le
.SetEventObject( GetParent() );
2982 le
.m_itemIndex
= current
;
2983 le
.m_pointDrag
= m_dragStart
;
2984 GetParent()->GetEventHandler()->ProcessEvent( le
);
2995 // outside of any item
2999 bool forceClick
= FALSE
;
3000 if (event
.ButtonDClick())
3002 m_renameTimer
->Stop();
3003 m_lastOnSame
= FALSE
;
3005 if ( current
== m_lineLastClicked
)
3007 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3013 // the first click was on another item, so don't interpret this as
3014 // a double click, but as a simple click instead
3019 if (event
.LeftUp() && m_lastOnSame
)
3021 if ((current
== m_current
) &&
3022 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
3023 HasFlag(wxLC_EDIT_LABELS
) )
3025 m_renameTimer
->Start( 100, TRUE
);
3027 m_lastOnSame
= FALSE
;
3029 else if (event
.RightDown())
3031 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
3032 event
.GetPosition() );
3034 else if (event
.MiddleDown())
3036 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
3038 else if ( event
.LeftDown() || forceClick
)
3040 m_lineBeforeLastClicked
= m_lineLastClicked
;
3041 m_lineLastClicked
= current
;
3043 size_t oldCurrent
= m_current
;
3045 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
3047 HighlightAll( FALSE
);
3049 ChangeCurrent(current
);
3051 ReverseHighlight(m_current
);
3053 else // multi sel & either ctrl or shift is down
3055 if (event
.ControlDown())
3057 ChangeCurrent(current
);
3059 ReverseHighlight(m_current
);
3061 else if (event
.ShiftDown())
3063 ChangeCurrent(current
);
3065 size_t lineFrom
= oldCurrent
,
3068 if ( lineTo
< lineFrom
)
3071 lineFrom
= m_current
;
3074 HighlightLines(lineFrom
, lineTo
);
3076 else // !ctrl, !shift
3078 // test in the enclosing if should make it impossible
3079 wxFAIL_MSG( _T("how did we get here?") );
3083 if (m_current
!= oldCurrent
)
3085 RefreshLine( oldCurrent
);
3088 // forceClick is only set if the previous click was on another item
3089 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3093 void wxListMainWindow::MoveToItem(size_t item
)
3095 if ( item
== (size_t)-1 )
3098 wxRect rect
= GetLineRect(item
);
3100 int client_w
, client_h
;
3101 GetClientSize( &client_w
, &client_h
);
3103 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
3104 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
3106 if ( HasFlag(wxLC_REPORT
) )
3108 // the next we need the range of lines shown it might be different, so
3110 ResetVisibleLinesRange();
3112 if (rect
.y
< view_y
)
3113 Scroll( -1, rect
.y
/m_yScroll
);
3114 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3115 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
3119 if (rect
.x
-view_x
< 5)
3120 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
3121 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3122 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
3126 // ----------------------------------------------------------------------------
3127 // keyboard handling
3128 // ----------------------------------------------------------------------------
3130 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3132 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3133 _T("invalid item index in OnArrowChar()") );
3135 size_t oldCurrent
= m_current
;
3137 // in single selection we just ignore Shift as we can't select several
3139 if ( event
.ShiftDown() && !IsSingleSel() )
3141 ChangeCurrent(newCurrent
);
3143 // select all the items between the old and the new one
3144 if ( oldCurrent
> newCurrent
)
3146 newCurrent
= oldCurrent
;
3147 oldCurrent
= m_current
;
3150 HighlightLines(oldCurrent
, newCurrent
);
3154 // all previously selected items are unselected unless ctrl is held
3155 if ( !event
.ControlDown() )
3156 HighlightAll(FALSE
);
3158 ChangeCurrent(newCurrent
);
3160 // refresh the old focus to remove it
3161 RefreshLine( oldCurrent
);
3163 if ( !event
.ControlDown() )
3165 HighlightLine( m_current
, TRUE
);
3169 RefreshLine( m_current
);
3174 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3176 wxWindow
*parent
= GetParent();
3178 /* we propagate the key event up */
3179 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3180 ke
.m_shiftDown
= event
.m_shiftDown
;
3181 ke
.m_controlDown
= event
.m_controlDown
;
3182 ke
.m_altDown
= event
.m_altDown
;
3183 ke
.m_metaDown
= event
.m_metaDown
;
3184 ke
.m_keyCode
= event
.m_keyCode
;
3187 ke
.SetEventObject( parent
);
3188 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3193 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3195 wxWindow
*parent
= GetParent();
3197 /* we send a list_key event up */
3200 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3201 le
.m_itemIndex
= m_current
;
3202 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3203 le
.m_code
= event
.GetKeyCode();
3204 le
.SetEventObject( parent
);
3205 parent
->GetEventHandler()->ProcessEvent( le
);
3208 /* we propagate the char event up */
3209 wxKeyEvent
ke( wxEVT_CHAR
);
3210 ke
.m_shiftDown
= event
.m_shiftDown
;
3211 ke
.m_controlDown
= event
.m_controlDown
;
3212 ke
.m_altDown
= event
.m_altDown
;
3213 ke
.m_metaDown
= event
.m_metaDown
;
3214 ke
.m_keyCode
= event
.m_keyCode
;
3217 ke
.SetEventObject( parent
);
3218 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3220 if (event
.GetKeyCode() == WXK_TAB
)
3222 wxNavigationKeyEvent nevent
;
3223 nevent
.SetWindowChange( event
.ControlDown() );
3224 nevent
.SetDirection( !event
.ShiftDown() );
3225 nevent
.SetEventObject( GetParent()->GetParent() );
3226 nevent
.SetCurrentFocus( m_parent
);
3227 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3231 /* no item -> nothing to do */
3238 switch (event
.GetKeyCode())
3241 if ( m_current
> 0 )
3242 OnArrowChar( m_current
- 1, event
);
3246 if ( m_current
< (size_t)GetItemCount() - 1 )
3247 OnArrowChar( m_current
+ 1, event
);
3252 OnArrowChar( GetItemCount() - 1, event
);
3257 OnArrowChar( 0, event
);
3263 if ( HasFlag(wxLC_REPORT
) )
3265 steps
= m_linesPerPage
- 1;
3269 steps
= m_current
% m_linesPerPage
;
3272 int index
= m_current
- steps
;
3276 OnArrowChar( index
, event
);
3283 if ( HasFlag(wxLC_REPORT
) )
3285 steps
= m_linesPerPage
- 1;
3289 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3292 size_t index
= m_current
+ steps
;
3293 size_t count
= GetItemCount();
3294 if ( index
>= count
)
3297 OnArrowChar( index
, event
);
3302 if ( !HasFlag(wxLC_REPORT
) )
3304 int index
= m_current
- m_linesPerPage
;
3308 OnArrowChar( index
, event
);
3313 if ( !HasFlag(wxLC_REPORT
) )
3315 size_t index
= m_current
+ m_linesPerPage
;
3317 size_t count
= GetItemCount();
3318 if ( index
>= count
)
3321 OnArrowChar( index
, event
);
3326 if ( IsSingleSel() )
3328 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3330 if ( IsHighlighted(m_current
) )
3332 // don't unselect the item in single selection mode
3335 //else: select it in ReverseHighlight() below if unselected
3338 ReverseHighlight(m_current
);
3343 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3351 // ----------------------------------------------------------------------------
3353 // ----------------------------------------------------------------------------
3355 void wxListMainWindow::SetFocus()
3357 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3358 // overrides SetFocus in such way that it does never change focus from
3359 // panel's child to the panel itself. Unfortunately, we must be able to change
3360 // focus to the panel from wxListTextCtrl because the text control should
3361 // disappear when the user clicks outside it.
3363 wxWindow
*oldFocus
= FindFocus();
3365 if ( oldFocus
&& oldFocus
->GetParent() == this )
3367 wxWindow::SetFocus();
3371 wxScrolledWindow::SetFocus();
3375 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3377 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3378 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3379 // which are already drawn correctly resulting in horrible flicker - avoid
3391 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3392 event
.SetEventObject( GetParent() );
3393 GetParent()->GetEventHandler()->ProcessEvent( event
);
3396 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3403 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3405 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3407 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3409 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3411 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3413 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3415 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3417 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
3419 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3423 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3425 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3427 m_normal_image_list
->GetSize( index
, width
, height
);
3429 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3431 m_small_image_list
->GetSize( index
, width
, height
);
3433 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3435 m_small_image_list
->GetSize( index
, width
, height
);
3437 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
3439 m_small_image_list
->GetSize( index
, width
, height
);
3448 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3450 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3451 dc
.SetFont( GetFont() );
3454 dc
.GetTextExtent( s
, &lw
, NULL
);
3456 return lw
+ AUTOSIZE_COL_MARGIN
;
3459 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3463 // calc the spacing from the icon size
3466 if ((imageList
) && (imageList
->GetImageCount()) )
3468 imageList
->GetSize(0, width
, height
);
3471 if (which
== wxIMAGE_LIST_NORMAL
)
3473 m_normal_image_list
= imageList
;
3474 m_normal_spacing
= width
+ 8;
3477 if (which
== wxIMAGE_LIST_SMALL
)
3479 m_small_image_list
= imageList
;
3480 m_small_spacing
= width
+ 14;
3481 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3485 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3490 m_small_spacing
= spacing
;
3494 m_normal_spacing
= spacing
;
3498 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3500 return isSmall
? m_small_spacing
: m_normal_spacing
;
3503 // ----------------------------------------------------------------------------
3505 // ----------------------------------------------------------------------------
3507 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3509 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3511 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3513 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3514 item
.m_width
= GetTextLength( item
.m_text
);
3516 wxListHeaderData
*column
= node
->GetData();
3517 column
->SetItem( item
);
3519 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3521 headerWin
->m_dirty
= TRUE
;
3525 // invalidate it as it has to be recalculated
3529 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3531 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3532 _T("invalid column index") );
3534 wxCHECK_RET( HasFlag(wxLC_REPORT
),
3535 _T("SetColumnWidth() can only be called in report mode.") );
3538 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3540 headerWin
->m_dirty
= TRUE
;
3542 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3543 wxCHECK_RET( node
, _T("no column?") );
3545 wxListHeaderData
*column
= node
->GetData();
3547 size_t count
= GetItemCount();
3549 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3551 width
= GetTextLength(column
->GetText());
3553 else if ( width
== wxLIST_AUTOSIZE
)
3557 // TODO: determine the max width somehow...
3558 width
= WIDTH_COL_DEFAULT
;
3562 wxClientDC
dc(this);
3563 dc
.SetFont( GetFont() );
3565 int max
= AUTOSIZE_COL_MARGIN
;
3567 for ( size_t i
= 0; i
< count
; i
++ )
3569 wxListLineData
*line
= GetLine(i
);
3570 wxListItemDataList::Node
*n
= line
->m_items
.Item( col
);
3572 wxCHECK_RET( n
, _T("no subitem?") );
3574 wxListItemData
*item
= n
->GetData();
3577 if (item
->HasImage())
3580 GetImageSize( item
->GetImage(), ix
, iy
);
3584 if (item
->HasText())
3587 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3595 width
= max
+ AUTOSIZE_COL_MARGIN
;
3599 column
->SetWidth( width
);
3601 // invalidate it as it has to be recalculated
3605 int wxListMainWindow::GetHeaderWidth() const
3607 if ( !m_headerWidth
)
3609 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3611 size_t count
= GetColumnCount();
3612 for ( size_t col
= 0; col
< count
; col
++ )
3614 self
->m_headerWidth
+= GetColumnWidth(col
);
3618 return m_headerWidth
;
3621 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3623 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3624 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3626 wxListHeaderData
*column
= node
->GetData();
3627 column
->GetItem( item
);
3630 int wxListMainWindow::GetColumnWidth( int col
) const
3632 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3633 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3635 wxListHeaderData
*column
= node
->GetData();
3636 return column
->GetWidth();
3639 // ----------------------------------------------------------------------------
3641 // ----------------------------------------------------------------------------
3643 void wxListMainWindow::SetItem( wxListItem
&item
)
3645 long id
= item
.m_itemId
;
3646 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3647 _T("invalid item index in SetItem") );
3651 wxListLineData
*line
= GetLine((size_t)id
);
3652 line
->SetItem( item
.m_col
, item
);
3655 // update the item on screen
3657 GetItemRect(id
, rectItem
);
3658 RefreshRect(rectItem
);
3661 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3663 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3664 _T("invalid list ctrl item index in SetItem") );
3666 size_t oldCurrent
= m_current
;
3667 size_t item
= (size_t)litem
; // safe because of the check above
3669 // do we need to change the focus?
3670 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3672 if ( state
& wxLIST_STATE_FOCUSED
)
3674 // don't do anything if this item is already focused
3675 if ( item
!= m_current
)
3677 ChangeCurrent(item
);
3679 if ( oldCurrent
!= (size_t)-1 )
3681 if ( IsSingleSel() )
3683 HighlightLine(oldCurrent
, FALSE
);
3686 RefreshLine(oldCurrent
);
3689 RefreshLine( m_current
);
3694 // don't do anything if this item is not focused
3695 if ( item
== m_current
)
3699 if ( IsSingleSel() )
3701 // we must unselect the old current item as well or we
3702 // might end up with more than one selected item in a
3703 // single selection control
3704 HighlightLine(oldCurrent
, FALSE
);
3707 RefreshLine( oldCurrent
);
3712 // do we need to change the selection state?
3713 if ( stateMask
& wxLIST_STATE_SELECTED
)
3715 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3717 if ( IsSingleSel() )
3721 // selecting the item also makes it the focused one in the
3723 if ( m_current
!= item
)
3725 ChangeCurrent(item
);
3727 if ( oldCurrent
!= (size_t)-1 )
3729 HighlightLine( oldCurrent
, FALSE
);
3730 RefreshLine( oldCurrent
);
3736 // only the current item may be selected anyhow
3737 if ( item
!= m_current
)
3742 if ( HighlightLine(item
, on
) )
3749 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3751 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3752 _T("invalid list ctrl item index in GetItemState()") );
3754 int ret
= wxLIST_STATE_DONTCARE
;
3756 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3758 if ( (size_t)item
== m_current
)
3759 ret
|= wxLIST_STATE_FOCUSED
;
3762 if ( stateMask
& wxLIST_STATE_SELECTED
)
3764 if ( IsHighlighted(item
) )
3765 ret
|= wxLIST_STATE_SELECTED
;
3771 void wxListMainWindow::GetItem( wxListItem
&item
) const
3773 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3774 _T("invalid item index in GetItem") );
3776 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3777 line
->GetItem( item
.m_col
, item
);
3780 // ----------------------------------------------------------------------------
3782 // ----------------------------------------------------------------------------
3784 size_t wxListMainWindow::GetItemCount() const
3786 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3789 void wxListMainWindow::SetItemCount(long count
)
3791 m_selStore
.SetItemCount(count
);
3792 m_countVirt
= count
;
3794 ResetVisibleLinesRange();
3796 // scrollbars must be reset
3800 int wxListMainWindow::GetSelectedItemCount() const
3802 // deal with the quick case first
3803 if ( IsSingleSel() )
3805 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3808 // virtual controls remmebers all its selections itself
3810 return m_selStore
.GetSelectedCount();
3812 // TODO: we probably should maintain the number of items selected even for
3813 // non virtual controls as enumerating all lines is really slow...
3814 size_t countSel
= 0;
3815 size_t count
= GetItemCount();
3816 for ( size_t line
= 0; line
< count
; line
++ )
3818 if ( GetLine(line
)->IsHighlighted() )
3825 // ----------------------------------------------------------------------------
3826 // item position/size
3827 // ----------------------------------------------------------------------------
3829 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3831 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3832 _T("invalid index in GetItemRect") );
3834 rect
= GetLineRect((size_t)index
);
3836 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3839 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3842 GetItemRect(item
, rect
);
3850 // ----------------------------------------------------------------------------
3851 // geometry calculation
3852 // ----------------------------------------------------------------------------
3854 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3856 wxClientDC
dc( this );
3857 dc
.SetFont( GetFont() );
3860 if ( HasFlag(wxLC_ICON
) )
3861 iconSpacing
= m_normal_spacing
;
3862 else if ( HasFlag(wxLC_SMALL_ICON
) )
3863 iconSpacing
= m_small_spacing
;
3867 // Note that we do not call GetClientSize() here but
3868 // GetSize() and substract the border size for sunken
3869 // borders manually. This is technically incorrect,
3870 // but we need to know the client area's size WITHOUT
3871 // scrollbars here. Since we don't know if there are
3872 // any scrollbars, we use GetSize() instead. Another
3873 // solution would be to call SetScrollbars() here to
3874 // remove the scrollbars and call GetClientSize() then,
3875 // but this might result in flicker and - worse - will
3876 // reset the scrollbars to 0 which is not good at all
3877 // if you resize a dialog/window, but don't want to
3878 // reset the window scrolling. RR.
3879 // Furthermore, we actually do NOT subtract the border
3880 // width as 2 pixels is just the extra space which we
3881 // need around the actual content in the window. Other-
3882 // wise the text would e.g. touch the upper border. RR.
3885 GetSize( &clientWidth
, &clientHeight
);
3887 if ( HasFlag(wxLC_REPORT
) )
3889 // all lines have the same height
3890 int lineHeight
= GetLineHeight();
3892 // scroll one line per step
3893 m_yScroll
= lineHeight
;
3895 size_t lineCount
= GetItemCount();
3896 int entireHeight
= lineCount
*lineHeight
+ LINE_SPACING
;
3898 m_linesPerPage
= clientHeight
/ lineHeight
;
3900 ResetVisibleLinesRange();
3902 SetScrollbars( m_xScroll
, m_yScroll
,
3903 GetHeaderWidth() / m_xScroll
,
3904 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3905 GetScrollPos(wxHORIZONTAL
),
3906 GetScrollPos(wxVERTICAL
),
3911 // at first we try without any scrollbar. if the items don't
3912 // fit into the window, we recalculate after subtracting an
3913 // approximated 15 pt for the horizontal scrollbar
3915 int entireWidth
= 0;
3917 for (int tries
= 0; tries
< 2; tries
++)
3919 // We start with 4 for the border around all items
3924 // Now we have decided that the items do not fit into the
3925 // client area. Unfortunately, wxWindows sometimes thinks
3926 // that it does fit and therefore NO horizontal scrollbar
3927 // is inserted. This looks ugly, so we fudge here and make
3928 // the calculated width bigger than was actually has been
3929 // calculated. This ensures that wxScrolledWindows puts
3930 // a scrollbar at the bottom of its client area.
3931 entireWidth
+= SCROLL_UNIT_X
;
3934 // Start at 2,2 so the text does not touch the border
3939 int currentlyVisibleLines
= 0;
3941 size_t count
= GetItemCount();
3942 for (size_t i
= 0; i
< count
; i
++)
3944 currentlyVisibleLines
++;
3945 wxListLineData
*line
= GetLine(i
);
3946 line
->CalculateSize( &dc
, iconSpacing
);
3947 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
); // Why clientWidth? (FIXME)
3949 wxSize sizeLine
= GetLineSize(i
);
3951 if ( maxWidth
< sizeLine
.x
)
3952 maxWidth
= sizeLine
.x
;
3955 if (currentlyVisibleLines
> m_linesPerPage
)
3956 m_linesPerPage
= currentlyVisibleLines
;
3958 // Assume that the size of the next one is the same... (FIXME)
3959 if ( y
+ sizeLine
.y
>= clientHeight
)
3961 currentlyVisibleLines
= 0;
3964 entireWidth
+= maxWidth
+6;
3968 // We have reached the last item.
3969 if ( i
== count
- 1 )
3970 entireWidth
+= maxWidth
;
3972 if ( (tries
== 0) && (entireWidth
+SCROLL_UNIT_X
> clientWidth
) )
3974 clientHeight
-= 15; // We guess the scrollbar height. (FIXME)
3976 currentlyVisibleLines
= 0;
3980 if ( i
== count
- 1 )
3981 tries
= 1; // Everything fits, no second try required.
3985 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
3986 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
3991 // FIXME: why should we call it from here?
3998 void wxListMainWindow::RefreshAll()
4003 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4004 if ( headerWin
&& headerWin
->m_dirty
)
4006 headerWin
->m_dirty
= FALSE
;
4007 headerWin
->Refresh();
4011 void wxListMainWindow::UpdateCurrent()
4013 if ( !HasCurrent() && !IsEmpty() )
4019 long wxListMainWindow::GetNextItem( long item
,
4020 int WXUNUSED(geometry
),
4024 max
= GetItemCount();
4025 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4026 _T("invalid listctrl index in GetNextItem()") );
4028 // notice that we start with the next item (or the first one if item == -1)
4029 // and this is intentional to allow writing a simple loop to iterate over
4030 // all selected items
4034 // this is not an error because the index was ok initially, just no
4045 size_t count
= GetItemCount();
4046 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4048 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4051 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4058 // ----------------------------------------------------------------------------
4060 // ----------------------------------------------------------------------------
4062 void wxListMainWindow::DeleteItem( long lindex
)
4064 size_t count
= GetItemCount();
4066 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4067 _T("invalid item index in DeleteItem") );
4069 size_t index
= (size_t)lindex
;
4071 // we don't need to adjust the index for the previous items
4072 if ( HasCurrent() && m_current
>= index
)
4074 // if the current item is being deleted, we want the next one to
4075 // become selected - unless there is no next one - so don't adjust
4076 // m_current in this case
4077 if ( m_current
!= index
|| m_current
== count
- 1 )
4083 if ( InReportView() )
4085 ResetVisibleLinesRange();
4092 m_selStore
.OnItemDelete(index
);
4096 m_lines
.RemoveAt( index
);
4099 // we need to refresh the (vert) scrollbar as the number of items changed
4102 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4104 RefreshAfter(index
);
4107 void wxListMainWindow::DeleteColumn( int col
)
4109 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4111 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4114 m_columns
.DeleteNode( node
);
4116 // invalidate it as it has to be recalculated
4120 void wxListMainWindow::DoDeleteAllItems()
4124 // nothing to do - in particular, don't send the event
4130 // to make the deletion of all items faster, we don't send the
4131 // notifications for each item deletion in this case but only one event
4132 // for all of them: this is compatible with wxMSW and documented in
4133 // DeleteAllItems() description
4135 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4136 event
.SetEventObject( GetParent() );
4137 GetParent()->GetEventHandler()->ProcessEvent( event
);
4146 if ( InReportView() )
4148 ResetVisibleLinesRange();
4154 void wxListMainWindow::DeleteAllItems()
4158 RecalculatePositions();
4161 void wxListMainWindow::DeleteEverything()
4168 // ----------------------------------------------------------------------------
4169 // scanning for an item
4170 // ----------------------------------------------------------------------------
4172 void wxListMainWindow::EnsureVisible( long index
)
4174 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4175 _T("invalid index in EnsureVisible") );
4177 // We have to call this here because the label in question might just have
4178 // been added and its position is not known yet
4181 RecalculatePositions(TRUE
/* no refresh */);
4184 MoveToItem((size_t)index
);
4187 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4194 size_t count
= GetItemCount();
4195 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4197 wxListLineData
*line
= GetLine(i
);
4198 if ( line
->GetText(0) == tmp
)
4205 long wxListMainWindow::FindItem(long start
, long data
)
4211 size_t count
= GetItemCount();
4212 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4214 wxListLineData
*line
= GetLine(i
);
4216 line
->GetItem( 0, item
);
4217 if (item
.m_data
== data
)
4224 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4226 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4228 size_t count
= GetItemCount();
4230 if ( HasFlag(wxLC_REPORT
) )
4232 size_t current
= y
/ GetLineHeight();
4233 if ( current
< count
)
4235 flags
= HitTestLine(current
, x
, y
);
4242 // TODO: optimize it too! this is less simple than for report view but
4243 // enumerating all items is still not a way to do it!!
4244 for ( size_t current
= 0; current
< count
; current
++ )
4246 flags
= HitTestLine(current
, x
, y
);
4255 // ----------------------------------------------------------------------------
4257 // ----------------------------------------------------------------------------
4259 void wxListMainWindow::InsertItem( wxListItem
&item
)
4261 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4263 size_t count
= GetItemCount();
4264 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4265 _T("invalid item index") );
4267 size_t id
= item
.m_itemId
;
4272 if ( HasFlag(wxLC_REPORT
) )
4275 ResetVisibleLinesRange();
4277 else if ( HasFlag(wxLC_LIST
) )
4279 else if ( HasFlag(wxLC_ICON
) )
4281 else if ( HasFlag(wxLC_SMALL_ICON
) )
4282 mode
= wxLC_ICON
; // no typo
4285 wxFAIL_MSG( _T("unknown mode") );
4288 wxListLineData
*line
= new wxListLineData(this);
4290 line
->SetItem( 0, item
);
4292 m_lines
.Insert( line
, id
);
4296 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4298 RefreshLines(id
, GetItemCount() - 1);
4301 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4304 if ( HasFlag(wxLC_REPORT
) )
4306 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4307 item
.m_width
= GetTextLength( item
.m_text
);
4308 wxListHeaderData
*column
= new wxListHeaderData( item
);
4309 if ((col
>= 0) && (col
< (int)m_columns
.GetCount()))
4311 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
4312 m_columns
.Insert( node
, column
);
4316 m_columns
.Append( column
);
4319 // invalidate it as it has to be recalculated
4324 // ----------------------------------------------------------------------------
4326 // ----------------------------------------------------------------------------
4328 wxListCtrlCompare list_ctrl_compare_func_2
;
4329 long list_ctrl_compare_data
;
4331 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4333 wxListLineData
*line1
= *arg1
;
4334 wxListLineData
*line2
= *arg2
;
4336 line1
->GetItem( 0, item
);
4337 long data1
= item
.m_data
;
4338 line2
->GetItem( 0, item
);
4339 long data2
= item
.m_data
;
4340 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4343 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4345 list_ctrl_compare_func_2
= fn
;
4346 list_ctrl_compare_data
= data
;
4347 m_lines
.Sort( list_ctrl_compare_func_1
);
4351 // ----------------------------------------------------------------------------
4353 // ----------------------------------------------------------------------------
4355 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4357 // update our idea of which lines are shown when we redraw the window the
4359 ResetVisibleLinesRange();
4362 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4363 wxScrolledWindow::OnScroll(event
);
4365 HandleOnScroll( event
);
4368 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4370 wxGenericListCtrl
* lc
= GetListCtrl();
4371 wxCHECK_RET( lc
, _T("no listctrl window?") );
4373 lc
->m_headerWin
->Refresh();
4374 lc
->m_headerWin
->Update();
4378 int wxListMainWindow::GetCountPerPage() const
4380 if ( !m_linesPerPage
)
4382 wxConstCast(this, wxListMainWindow
)->
4383 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4386 return m_linesPerPage
;
4389 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4391 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("this is for report mode only") );
4393 if ( m_lineFrom
== (size_t)-1 )
4395 size_t count
= GetItemCount();
4398 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4400 // this may happen if SetScrollbars() hadn't been called yet
4401 if ( m_lineFrom
>= count
)
4402 m_lineFrom
= count
- 1;
4404 // we redraw one extra line but this is needed to make the redrawing
4405 // logic work when there is a fractional number of lines on screen
4406 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4407 if ( m_lineTo
>= count
)
4408 m_lineTo
= count
- 1;
4410 else // empty control
4413 m_lineTo
= (size_t)-1;
4417 wxASSERT_MSG( IsEmpty() ||
4418 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4419 _T("GetVisibleLinesRange() returns incorrect result") );
4427 // -------------------------------------------------------------------------------------
4428 // wxGenericListCtrl
4429 // -------------------------------------------------------------------------------------
4431 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4433 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4434 EVT_SIZE(wxGenericListCtrl::OnSize
)
4437 wxGenericListCtrl::wxGenericListCtrl()
4439 m_imageListNormal
= (wxImageListType
*) NULL
;
4440 m_imageListSmall
= (wxImageListType
*) NULL
;
4441 m_imageListState
= (wxImageListType
*) NULL
;
4443 m_ownsImageListNormal
=
4444 m_ownsImageListSmall
=
4445 m_ownsImageListState
= FALSE
;
4447 m_mainWin
= (wxListMainWindow
*) NULL
;
4448 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4451 wxGenericListCtrl::~wxGenericListCtrl()
4453 if (m_ownsImageListNormal
)
4454 delete m_imageListNormal
;
4455 if (m_ownsImageListSmall
)
4456 delete m_imageListSmall
;
4457 if (m_ownsImageListState
)
4458 delete m_imageListState
;
4461 void wxGenericListCtrl::CreateHeaderWindow()
4463 m_headerWin
= new wxListHeaderWindow
4465 this, -1, m_mainWin
,
4467 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
4472 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4477 const wxValidator
&validator
,
4478 const wxString
&name
)
4482 m_imageListState
= (wxImageListType
*) NULL
;
4483 m_ownsImageListNormal
=
4484 m_ownsImageListSmall
=
4485 m_ownsImageListState
= FALSE
;
4487 m_mainWin
= (wxListMainWindow
*) NULL
;
4488 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4490 if ( !(style
& wxLC_MASK_TYPE
) )
4492 style
= style
| wxLC_LIST
;
4495 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4498 // don't create the inner window with the border
4499 style
&= ~wxBORDER_MASK
;
4501 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4503 if ( HasFlag(wxLC_REPORT
) )
4505 CreateHeaderWindow();
4507 if ( HasFlag(wxLC_NO_HEADER
) )
4509 // VZ: why do we create it at all then?
4510 m_headerWin
->Show( FALSE
);
4517 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4519 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4520 _T("wxLC_VIRTUAL can't be [un]set") );
4522 long flag
= GetWindowStyle();
4526 if (style
& wxLC_MASK_TYPE
)
4527 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4528 if (style
& wxLC_MASK_ALIGN
)
4529 flag
&= ~wxLC_MASK_ALIGN
;
4530 if (style
& wxLC_MASK_SORT
)
4531 flag
&= ~wxLC_MASK_SORT
;
4543 SetWindowStyleFlag( flag
);
4546 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4550 m_mainWin
->DeleteEverything();
4552 // has the header visibility changed?
4553 bool hasHeader
= HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
),
4554 willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4556 if ( hasHeader
!= willHaveHeader
)
4563 // don't delete, just hide, as we can reuse it later
4564 m_headerWin
->Show(FALSE
);
4566 //else: nothing to do
4568 else // must show header
4572 CreateHeaderWindow();
4574 else // already have it, just show
4576 m_headerWin
->Show( TRUE
);
4580 ResizeReportView(willHaveHeader
);
4584 wxWindow::SetWindowStyleFlag( flag
);
4587 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4589 m_mainWin
->GetColumn( col
, item
);
4593 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4595 m_mainWin
->SetColumn( col
, item
);
4599 int wxGenericListCtrl::GetColumnWidth( int col
) const
4601 return m_mainWin
->GetColumnWidth( col
);
4604 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4606 m_mainWin
->SetColumnWidth( col
, width
);
4610 int wxGenericListCtrl::GetCountPerPage() const
4612 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4615 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4617 m_mainWin
->GetItem( info
);
4621 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4623 m_mainWin
->SetItem( info
);
4627 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4630 info
.m_text
= label
;
4631 info
.m_mask
= wxLIST_MASK_TEXT
;
4632 info
.m_itemId
= index
;
4636 info
.m_image
= imageId
;
4637 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4639 m_mainWin
->SetItem(info
);
4643 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4645 return m_mainWin
->GetItemState( item
, stateMask
);
4648 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4650 m_mainWin
->SetItemState( item
, state
, stateMask
);
4654 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4657 info
.m_image
= image
;
4658 info
.m_mask
= wxLIST_MASK_IMAGE
;
4659 info
.m_itemId
= item
;
4660 m_mainWin
->SetItem( info
);
4664 wxString
wxGenericListCtrl::GetItemText( long item
) const
4666 return m_mainWin
->GetItemText(item
);
4669 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4671 m_mainWin
->SetItemText(item
, str
);
4674 long wxGenericListCtrl::GetItemData( long item
) const
4677 info
.m_itemId
= item
;
4678 m_mainWin
->GetItem( info
);
4682 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4685 info
.m_mask
= wxLIST_MASK_DATA
;
4686 info
.m_itemId
= item
;
4688 m_mainWin
->SetItem( info
);
4692 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4694 m_mainWin
->GetItemRect( item
, rect
);
4695 if ( m_mainWin
->HasHeader() )
4696 rect
.y
+= HEADER_HEIGHT
+ 1;
4700 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4702 m_mainWin
->GetItemPosition( item
, pos
);
4706 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4711 int wxGenericListCtrl::GetItemCount() const
4713 return m_mainWin
->GetItemCount();
4716 int wxGenericListCtrl::GetColumnCount() const
4718 return m_mainWin
->GetColumnCount();
4721 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4723 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4726 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4728 return m_mainWin
->GetItemSpacing( isSmall
);
4731 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4734 info
.m_itemId
= item
;
4735 info
.SetTextColour( col
);
4736 m_mainWin
->SetItem( info
);
4739 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4742 info
.m_itemId
= item
;
4743 m_mainWin
->GetItem( info
);
4744 return info
.GetTextColour();
4747 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4750 info
.m_itemId
= item
;
4751 info
.SetBackgroundColour( col
);
4752 m_mainWin
->SetItem( info
);
4755 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4758 info
.m_itemId
= item
;
4759 m_mainWin
->GetItem( info
);
4760 return info
.GetBackgroundColour();
4763 int wxGenericListCtrl::GetSelectedItemCount() const
4765 return m_mainWin
->GetSelectedItemCount();
4768 wxColour
wxGenericListCtrl::GetTextColour() const
4770 return GetForegroundColour();
4773 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4775 SetForegroundColour(col
);
4778 long wxGenericListCtrl::GetTopItem() const
4781 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4785 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4787 return m_mainWin
->GetNextItem( item
, geom
, state
);
4790 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
4792 if (which
== wxIMAGE_LIST_NORMAL
)
4794 return m_imageListNormal
;
4796 else if (which
== wxIMAGE_LIST_SMALL
)
4798 return m_imageListSmall
;
4800 else if (which
== wxIMAGE_LIST_STATE
)
4802 return m_imageListState
;
4804 return (wxImageListType
*) NULL
;
4807 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
4809 if ( which
== wxIMAGE_LIST_NORMAL
)
4811 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4812 m_imageListNormal
= imageList
;
4813 m_ownsImageListNormal
= FALSE
;
4815 else if ( which
== wxIMAGE_LIST_SMALL
)
4817 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4818 m_imageListSmall
= imageList
;
4819 m_ownsImageListSmall
= FALSE
;
4821 else if ( which
== wxIMAGE_LIST_STATE
)
4823 if (m_ownsImageListState
) delete m_imageListState
;
4824 m_imageListState
= imageList
;
4825 m_ownsImageListState
= FALSE
;
4828 m_mainWin
->SetImageList( imageList
, which
);
4831 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
4833 SetImageList(imageList
, which
);
4834 if ( which
== wxIMAGE_LIST_NORMAL
)
4835 m_ownsImageListNormal
= TRUE
;
4836 else if ( which
== wxIMAGE_LIST_SMALL
)
4837 m_ownsImageListSmall
= TRUE
;
4838 else if ( which
== wxIMAGE_LIST_STATE
)
4839 m_ownsImageListState
= TRUE
;
4842 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4847 bool wxGenericListCtrl::DeleteItem( long item
)
4849 m_mainWin
->DeleteItem( item
);
4853 bool wxGenericListCtrl::DeleteAllItems()
4855 m_mainWin
->DeleteAllItems();
4859 bool wxGenericListCtrl::DeleteAllColumns()
4861 size_t count
= m_mainWin
->m_columns
.GetCount();
4862 for ( size_t n
= 0; n
< count
; n
++ )
4868 void wxGenericListCtrl::ClearAll()
4870 m_mainWin
->DeleteEverything();
4873 bool wxGenericListCtrl::DeleteColumn( int col
)
4875 m_mainWin
->DeleteColumn( col
);
4877 // if we don't have the header any longer, we need to relayout the window
4878 if ( !GetColumnCount() )
4880 ResizeReportView(FALSE
/* no header */);
4886 void wxGenericListCtrl::Edit( long item
)
4888 m_mainWin
->EditLabel( item
);
4891 bool wxGenericListCtrl::EnsureVisible( long item
)
4893 m_mainWin
->EnsureVisible( item
);
4897 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4899 return m_mainWin
->FindItem( start
, str
, partial
);
4902 long wxGenericListCtrl::FindItem( long start
, long data
)
4904 return m_mainWin
->FindItem( start
, data
);
4907 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4908 int WXUNUSED(direction
))
4913 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4915 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4918 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
4920 m_mainWin
->InsertItem( info
);
4921 return info
.m_itemId
;
4924 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
4927 info
.m_text
= label
;
4928 info
.m_mask
= wxLIST_MASK_TEXT
;
4929 info
.m_itemId
= index
;
4930 return InsertItem( info
);
4933 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
4936 info
.m_mask
= wxLIST_MASK_IMAGE
;
4937 info
.m_image
= imageIndex
;
4938 info
.m_itemId
= index
;
4939 return InsertItem( info
);
4942 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4945 info
.m_text
= label
;
4946 info
.m_image
= imageIndex
;
4947 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4948 info
.m_itemId
= index
;
4949 return InsertItem( info
);
4952 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
4954 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
4956 m_mainWin
->InsertColumn( col
, item
);
4958 // if we hadn't had header before and have it now we need to relayout the
4960 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
4962 ResizeReportView(TRUE
/* have header */);
4965 m_headerWin
->Refresh();
4970 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
4971 int format
, int width
)
4974 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4975 item
.m_text
= heading
;
4978 item
.m_mask
|= wxLIST_MASK_WIDTH
;
4979 item
.m_width
= width
;
4981 item
.m_format
= format
;
4983 return InsertColumn( col
, item
);
4986 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
4992 // fn is a function which takes 3 long arguments: item1, item2, data.
4993 // item1 is the long data associated with a first item (NOT the index).
4994 // item2 is the long data associated with a second item (NOT the index).
4995 // data is the same value as passed to SortItems.
4996 // The return value is a negative number if the first item should precede the second
4997 // item, a positive number of the second item should precede the first,
4998 // or zero if the two items are equivalent.
4999 // data is arbitrary data to be passed to the sort function.
5001 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5003 m_mainWin
->SortItems( fn
, data
);
5007 // ----------------------------------------------------------------------------
5009 // ----------------------------------------------------------------------------
5011 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5016 ResizeReportView(m_mainWin
->HasHeader());
5018 m_mainWin
->RecalculatePositions();
5021 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5024 GetClientSize( &cw
, &ch
);
5028 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
5029 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
5031 else // no header window
5033 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5037 void wxGenericListCtrl::OnInternalIdle()
5039 wxWindow::OnInternalIdle();
5041 // do it only if needed
5042 if ( !m_mainWin
->m_dirty
)
5045 m_mainWin
->RecalculatePositions();
5048 // ----------------------------------------------------------------------------
5050 // ----------------------------------------------------------------------------
5052 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5056 m_mainWin
->SetBackgroundColour( colour
);
5057 m_mainWin
->m_dirty
= TRUE
;
5063 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5065 if ( !wxWindow::SetForegroundColour( colour
) )
5070 m_mainWin
->SetForegroundColour( colour
);
5071 m_mainWin
->m_dirty
= TRUE
;
5076 m_headerWin
->SetForegroundColour( colour
);
5082 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5084 if ( !wxWindow::SetFont( font
) )
5089 m_mainWin
->SetFont( font
);
5090 m_mainWin
->m_dirty
= TRUE
;
5095 m_headerWin
->SetFont( font
);
5101 // ----------------------------------------------------------------------------
5102 // methods forwarded to m_mainWin
5103 // ----------------------------------------------------------------------------
5105 #if wxUSE_DRAG_AND_DROP
5107 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5109 m_mainWin
->SetDropTarget( dropTarget
);
5112 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5114 return m_mainWin
->GetDropTarget();
5117 #endif // wxUSE_DRAG_AND_DROP
5119 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5121 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5124 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5126 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5129 wxColour
wxGenericListCtrl::GetForegroundColour() const
5131 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5134 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5137 return m_mainWin
->PopupMenu( menu
, x
, y
);
5140 #endif // wxUSE_MENUS
5143 void wxGenericListCtrl::SetFocus()
5145 /* The test in window.cpp fails as we are a composite
5146 window, so it checks against "this", but not m_mainWin. */
5147 if ( FindFocus() != this )
5148 m_mainWin
->SetFocus();
5151 // ----------------------------------------------------------------------------
5152 // virtual list control support
5153 // ----------------------------------------------------------------------------
5155 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5157 // this is a pure virtual function, in fact - which is not really pure
5158 // because the controls which are not virtual don't need to implement it
5159 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5161 return wxEmptyString
;
5164 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5167 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5172 wxListItemAttr
*wxGenericListCtrl::OnGetItemAttr(long item
) const
5174 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5175 _T("invalid item index in OnGetItemAttr()") );
5177 // no attributes by default
5181 void wxGenericListCtrl::SetItemCount(long count
)
5183 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5185 m_mainWin
->SetItemCount(count
);
5188 void wxGenericListCtrl::RefreshItem(long item
)
5190 m_mainWin
->RefreshLine(item
);
5193 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5195 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5199 * Generic wxListCtrl is more or less a container for two other
5200 * windows which drawings are done upon. These are namely
5201 * 'm_headerWin' and 'm_mainWin'.
5202 * Here we override 'virtual wxWindow::Refresh()' to mimic the
5203 * behaviour wxListCtrl has under wxMSW.
5205 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5209 // The easy case, no rectangle specified.
5211 m_headerWin
->Refresh(eraseBackground
);
5214 m_mainWin
->Refresh(eraseBackground
);
5218 // Refresh the header window
5221 wxRect rectHeader
= m_headerWin
->GetRect();
5222 rectHeader
.Intersect(*rect
);
5223 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5226 m_headerWin
->GetPosition(&x
, &y
);
5227 rectHeader
.Offset(-x
, -y
);
5228 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5233 // Refresh the main window
5236 wxRect rectMain
= m_mainWin
->GetRect();
5237 rectMain
.Intersect(*rect
);
5238 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5241 m_mainWin
->GetPosition(&x
, &y
);
5242 rectMain
.Offset(-x
, -y
);
5243 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5249 void wxGenericListCtrl::Freeze()
5251 m_mainWin
->Freeze();
5254 void wxGenericListCtrl::Thaw()
5259 #endif // wxUSE_LISTCTRL