1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 1. we need to implement searching/sorting for virtual controls somehow
15 ?2. when changing selection the lines are refreshed twice
18 // ============================================================================
20 // ============================================================================
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
27 #pragma implementation "listctrl.h"
28 #pragma implementation "listctrlbase.h"
31 // For compilers that support precompilation, includes "wx.h".
32 #include "wx/wxprec.h"
43 #include "wx/dynarray.h"
45 #include "wx/dcscreen.h"
47 #include "wx/textctrl.h"
50 // under Win32 we always use the native version and also may use the generic
51 // one, however some things should be done only if we use only the generic
53 #if defined(__WIN32__) && !defined(__WXUNIVERSAL__)
54 #define HAVE_NATIVE_LISTCTRL
57 // if we have the native control, wx/listctrl.h declares it and not this one
58 #ifdef HAVE_NATIVE_LISTCTRL
59 #include "wx/generic/listctrl.h"
60 #else // !HAVE_NATIVE_LISTCTRL
61 #include "wx/listctrl.h"
63 // if we have a native version, its implementation file does all this
64 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
65 IMPLEMENT_DYNAMIC_CLASS(wxListView
, wxListCtrl
)
66 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
68 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxGenericListCtrl
)
69 #endif // HAVE_NATIVE_LISTCTRL/!HAVE_NATIVE_LISTCTRL
71 #include "wx/selstore.h"
73 #include "wx/renderer.h"
76 #include "wx/mac/private.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 static const int SCROLL_UNIT_X
= 15;
114 // the spacing between the lines (in report mode)
115 static const int LINE_SPACING
= 0;
117 // extra margins around the text label
118 static const int EXTRA_WIDTH
= 4;
119 static const int EXTRA_HEIGHT
= 4;
121 // margin between the window and the items
122 static const int EXTRA_BORDER_X
= 2;
123 static const int EXTRA_BORDER_Y
= 2;
125 // offset for the header window
126 static const int HEADER_OFFSET_X
= 1;
127 static const int HEADER_OFFSET_Y
= 1;
129 // margin between rows of icons in [small] icon view
130 static const int MARGIN_BETWEEN_ROWS
= 6;
132 // when autosizing the columns, add some slack
133 static const int AUTOSIZE_COL_MARGIN
= 10;
135 // default and minimal widths for the header columns
136 static const int WIDTH_COL_DEFAULT
= 80;
137 static const int WIDTH_COL_MIN
= 10;
139 // the space between the image and the text in the report mode
140 static const int IMAGE_MARGIN_IN_REPORT_MODE
= 5;
142 // ============================================================================
144 // ============================================================================
146 //-----------------------------------------------------------------------------
147 // wxListItemData (internal)
148 //-----------------------------------------------------------------------------
150 class WXDLLEXPORT wxListItemData
153 wxListItemData(wxListMainWindow
*owner
);
156 void SetItem( const wxListItem
&info
);
157 void SetImage( int image
) { m_image
= image
; }
158 void SetData( long data
) { m_data
= data
; }
159 void SetPosition( int x
, int y
);
160 void SetSize( int width
, int height
);
162 bool HasText() const { return !m_text
.empty(); }
163 const wxString
& GetText() const { return m_text
; }
164 void SetText(const wxString
& text
) { m_text
= text
; }
166 // we can't use empty string for measuring the string width/height, so
167 // always return something
168 wxString
GetTextForMeasuring() const
170 wxString s
= GetText();
177 bool IsHit( int x
, int y
) const;
181 int GetWidth() const;
182 int GetHeight() const;
184 int GetImage() const { return m_image
; }
185 bool HasImage() const { return GetImage() != -1; }
187 void GetItem( wxListItem
&info
) const;
189 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
190 wxListItemAttr
*GetAttr() const { return m_attr
; }
193 // the item image or -1
196 // user data associated with the item
199 // the item coordinates are not used in report mode, instead this pointer
200 // is NULL and the owner window is used to retrieve the item position and
204 // the list ctrl we are in
205 wxListMainWindow
*m_owner
;
207 // custom attributes or NULL
208 wxListItemAttr
*m_attr
;
211 // common part of all ctors
217 //-----------------------------------------------------------------------------
218 // wxListHeaderData (internal)
219 //-----------------------------------------------------------------------------
221 class WXDLLEXPORT wxListHeaderData
: public wxObject
225 wxListHeaderData( const wxListItem
&info
);
226 void SetItem( const wxListItem
&item
);
227 void SetPosition( int x
, int y
);
228 void SetWidth( int w
);
229 void SetFormat( int format
);
230 void SetHeight( int h
);
231 bool HasImage() const;
233 bool HasText() const { return !m_text
.empty(); }
234 const wxString
& GetText() const { return m_text
; }
235 void SetText(const wxString
& text
) { m_text
= text
; }
237 void GetItem( wxListItem
&item
);
239 bool IsHit( int x
, int y
) const;
240 int GetImage() const;
241 int GetWidth() const;
242 int GetFormat() const;
258 //-----------------------------------------------------------------------------
259 // wxListLineData (internal)
260 //-----------------------------------------------------------------------------
262 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
263 #include "wx/listimpl.cpp"
264 WX_DEFINE_LIST(wxListItemDataList
);
269 // the list of subitems: only may have more than one item in report mode
270 wxListItemDataList m_items
;
272 // this is not used in report view
284 // the part to be highlighted
285 wxRect m_rectHighlight
;
287 // extend all our rects to be centered inside theo ne of given width
288 void ExtendWidth(wxCoord w
)
290 wxASSERT_MSG( m_rectAll
.width
<= w
,
291 _T("width can only be increased") );
294 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
)/2;
295 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
)/2;
296 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
)/2;
300 // is this item selected? [NB: not used in virtual mode]
303 // back pointer to the list ctrl
304 wxListMainWindow
*m_owner
;
307 wxListLineData(wxListMainWindow
*owner
);
311 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
315 // are we in report mode?
316 inline bool InReportView() const;
318 // are we in virtual report mode?
319 inline bool IsVirtual() const;
321 // these 2 methods shouldn't be called for report view controls, in that
322 // case we determine our position/size ourselves
324 // calculate the size of the line
325 void CalculateSize( wxDC
*dc
, int spacing
);
327 // remember the position this line appears at
328 void SetPosition( int x
, int y
, int spacing
);
332 void SetImage( int image
) { SetImage(0, image
); }
333 int GetImage() const { return GetImage(0); }
334 bool HasImage() const { return GetImage() != -1; }
335 bool HasText() const { return !GetText(0).empty(); }
337 void SetItem( int index
, const wxListItem
&info
);
338 void GetItem( int index
, wxListItem
&info
);
340 wxString
GetText(int index
) const;
341 void SetText( int index
, const wxString s
);
343 wxListItemAttr
*GetAttr() const;
344 void SetAttr(wxListItemAttr
*attr
);
346 // return true if the highlighting really changed
347 bool Highlight( bool on
);
349 void ReverseHighlight();
351 bool IsHighlighted() const
353 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
355 return m_highlighted
;
358 // draw the line on the given DC in icon/list mode
359 void Draw( wxDC
*dc
);
361 // the same in report mode
362 void DrawInReportMode( wxDC
*dc
,
364 const wxRect
& rectHL
,
368 // set the line to contain num items (only can be > 1 in report mode)
369 void InitItems( int num
);
371 // get the mode (i.e. style) of the list control
372 inline int GetMode() const;
374 // prepare the DC for drawing with these item's attributes, return true if
375 // we need to draw the items background to highlight it, false otherwise
376 bool SetAttributes(wxDC
*dc
,
377 const wxListItemAttr
*attr
,
380 // draw the text on the DC with the correct justification; also add an
381 // ellipsis if the text is too large to fit in the current width
382 void DrawTextFormatted(wxDC
*dc
, const wxString
&text
, int col
, int x
, int y
, int width
);
384 // these are only used by GetImage/SetImage above, we don't support images
385 // with subitems at the public API level yet
386 void SetImage( int index
, int image
);
387 int GetImage( int index
) const;
390 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
391 #include "wx/arrimpl.cpp"
392 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
394 //-----------------------------------------------------------------------------
395 // wxListHeaderWindow (internal)
396 //-----------------------------------------------------------------------------
398 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
401 wxListMainWindow
*m_owner
;
402 wxCursor
*m_currentCursor
;
403 wxCursor
*m_resizeCursor
;
406 // column being resized or -1
409 // divider line position in logical (unscrolled) coords
412 // minimal position beyond which the divider line can't be dragged in
417 wxListHeaderWindow();
419 wxListHeaderWindow( wxWindow
*win
,
421 wxListMainWindow
*owner
,
422 const wxPoint
&pos
= wxDefaultPosition
,
423 const wxSize
&size
= wxDefaultSize
,
425 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
427 virtual ~wxListHeaderWindow();
430 void AdjustDC(wxDC
& dc
);
432 void OnPaint( wxPaintEvent
&event
);
433 void OnMouse( wxMouseEvent
&event
);
434 void OnSetFocus( wxFocusEvent
&event
);
440 // common part of all ctors
443 // generate and process the list event of the given type, return true if
444 // it wasn't vetoed, i.e. if we should proceed
445 bool SendListEvent(wxEventType type
, wxPoint pos
);
447 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
448 DECLARE_EVENT_TABLE()
451 //-----------------------------------------------------------------------------
452 // wxListRenameTimer (internal)
453 //-----------------------------------------------------------------------------
455 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
458 wxListMainWindow
*m_owner
;
461 wxListRenameTimer( wxListMainWindow
*owner
);
465 //-----------------------------------------------------------------------------
466 // wxListTextCtrl (internal)
467 //-----------------------------------------------------------------------------
469 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
472 wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
);
475 void OnChar( wxKeyEvent
&event
);
476 void OnKeyUp( wxKeyEvent
&event
);
477 void OnKillFocus( wxFocusEvent
&event
);
479 bool AcceptChanges();
483 wxListMainWindow
*m_owner
;
484 wxString m_startValue
;
488 DECLARE_EVENT_TABLE()
491 //-----------------------------------------------------------------------------
492 // wxListMainWindow (internal)
493 //-----------------------------------------------------------------------------
495 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
496 #include "wx/listimpl.cpp"
497 WX_DEFINE_LIST(wxListHeaderDataList
);
499 class wxListMainWindow
: public wxScrolledWindow
503 wxListMainWindow( wxWindow
*parent
,
505 const wxPoint
& pos
= wxDefaultPosition
,
506 const wxSize
& size
= wxDefaultSize
,
508 const wxString
&name
= _T("listctrlmainwindow") );
510 virtual ~wxListMainWindow();
512 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
514 // return true if this is a virtual list control
515 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
517 // return true if the control is in report mode
518 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
520 // return true if we are in single selection mode, false if multi sel
521 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
523 // do we have a header window?
524 bool HasHeader() const
525 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
527 void HighlightAll( bool on
);
529 // all these functions only do something if the line is currently visible
531 // change the line "selected" state, return TRUE if it really changed
532 bool HighlightLine( size_t line
, bool highlight
= TRUE
);
534 // as HighlightLine() but do it for the range of lines: this is incredibly
535 // more efficient for virtual list controls!
537 // NB: unlike HighlightLine() this one does refresh the lines on screen
538 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= TRUE
);
540 // toggle the line state and refresh it
541 void ReverseHighlight( size_t line
)
542 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
544 // return true if the line is highlighted
545 bool IsHighlighted(size_t line
) const;
547 // refresh one or several lines at once
548 void RefreshLine( size_t line
);
549 void RefreshLines( size_t lineFrom
, size_t lineTo
);
551 // refresh all selected items
552 void RefreshSelected();
554 // refresh all lines below the given one: the difference with
555 // RefreshLines() is that the index here might not be a valid one (happens
556 // when the last line is deleted)
557 void RefreshAfter( size_t lineFrom
);
559 // the methods which are forwarded to wxListLineData itself in list/icon
560 // modes but are here because the lines don't store their positions in the
563 // get the bound rect for the entire line
564 wxRect
GetLineRect(size_t line
) const;
566 // get the bound rect of the label
567 wxRect
GetLineLabelRect(size_t line
) const;
569 // get the bound rect of the items icon (only may be called if we do have
571 wxRect
GetLineIconRect(size_t line
) const;
573 // get the rect to be highlighted when the item has focus
574 wxRect
GetLineHighlightRect(size_t line
) const;
576 // get the size of the total line rect
577 wxSize
GetLineSize(size_t line
) const
578 { return GetLineRect(line
).GetSize(); }
580 // return the hit code for the corresponding position (in this line)
581 long HitTestLine(size_t line
, int x
, int y
) const;
583 // bring the selected item into view, scrolling to it if necessary
584 void MoveToItem(size_t item
);
586 // bring the current item into view
587 void MoveToFocus() { MoveToItem(m_current
); }
589 // start editing the label of the given item
590 void EditLabel( long item
);
592 // suspend/resume redrawing the control
598 void OnRenameTimer();
599 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
600 void OnRenameCancelled(size_t itemEdit
);
602 void OnMouse( wxMouseEvent
&event
);
604 // called to switch the selection from the current item to newCurrent,
605 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
607 void OnChar( wxKeyEvent
&event
);
608 void OnKeyDown( wxKeyEvent
&event
);
609 void OnSetFocus( wxFocusEvent
&event
);
610 void OnKillFocus( wxFocusEvent
&event
);
611 void OnScroll(wxScrollWinEvent
& event
) ;
613 void OnPaint( wxPaintEvent
&event
);
615 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
616 void GetImageSize( int index
, int &width
, int &height
) const;
617 int GetTextLength( const wxString
&s
) const;
619 void SetImageList( wxImageListType
*imageList
, int which
);
620 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
621 int GetItemSpacing( bool isSmall
= FALSE
);
623 void SetColumn( int col
, wxListItem
&item
);
624 void SetColumnWidth( int col
, int width
);
625 void GetColumn( int col
, wxListItem
&item
) const;
626 int GetColumnWidth( int col
) const;
627 int GetColumnCount() const { return m_columns
.GetCount(); }
629 // returns the sum of the heights of all columns
630 int GetHeaderWidth() const;
632 int GetCountPerPage() const;
634 void SetItem( wxListItem
&item
);
635 void GetItem( wxListItem
&item
) const;
636 void SetItemState( long item
, long state
, long stateMask
);
637 int GetItemState( long item
, long stateMask
) const;
638 void GetItemRect( long index
, wxRect
&rect
) const;
639 wxRect
GetViewRect() const;
640 bool GetItemPosition( long item
, wxPoint
& pos
) const;
641 int GetSelectedItemCount() const;
643 wxString
GetItemText(long item
) const
646 info
.m_itemId
= item
;
651 void SetItemText(long item
, const wxString
& value
)
654 info
.m_mask
= wxLIST_MASK_TEXT
;
655 info
.m_itemId
= item
;
660 // set the scrollbars and update the positions of the items
661 void RecalculatePositions(bool noRefresh
= FALSE
);
663 // refresh the window and the header
666 long GetNextItem( long item
, int geometry
, int state
) const;
667 void DeleteItem( long index
);
668 void DeleteAllItems();
669 void DeleteColumn( int col
);
670 void DeleteEverything();
671 void EnsureVisible( long index
);
672 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
673 long FindItem( long start
, long data
);
674 long HitTest( int x
, int y
, int &flags
);
675 void InsertItem( wxListItem
&item
);
676 void InsertColumn( long col
, wxListItem
&item
);
677 void SortItems( wxListCtrlCompare fn
, long data
);
679 size_t GetItemCount() const;
680 bool IsEmpty() const { return GetItemCount() == 0; }
681 void SetItemCount(long count
);
683 // change the current (== focused) item, send a notification event
684 void ChangeCurrent(size_t current
);
685 void ResetCurrent() { ChangeCurrent((size_t)-1); }
686 bool HasCurrent() const { return m_current
!= (size_t)-1; }
688 // send out a wxListEvent
689 void SendNotify( size_t line
,
691 wxPoint point
= wxDefaultPosition
);
693 // override base class virtual to reset m_lineHeight when the font changes
694 virtual bool SetFont(const wxFont
& font
)
696 if ( !wxScrolledWindow::SetFont(font
) )
704 // these are for wxListLineData usage only
706 // get the backpointer to the list ctrl
707 wxGenericListCtrl
*GetListCtrl() const
709 return wxStaticCast(GetParent(), wxGenericListCtrl
);
712 // get the height of all lines (assuming they all do have the same height)
713 wxCoord
GetLineHeight() const;
715 // get the y position of the given line (only for report view)
716 wxCoord
GetLineY(size_t line
) const;
718 // get the brush to use for the item highlighting
719 wxBrush
*GetHighlightBrush() const
721 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
725 // the array of all line objects for a non virtual list control (for the
726 // virtual list control we only ever use m_lines[0])
727 wxListLineDataArray m_lines
;
729 // the list of column objects
730 wxListHeaderDataList m_columns
;
732 // currently focused item or -1
735 // the number of lines per page
738 // this flag is set when something which should result in the window
739 // redrawing happens (i.e. an item was added or deleted, or its appearance
740 // changed) and OnPaint() doesn't redraw the window while it is set which
741 // allows to minimize the number of repaintings when a lot of items are
742 // being added. The real repainting occurs only after the next OnIdle()
746 wxColour
*m_highlightColour
;
747 wxImageListType
*m_small_image_list
;
748 wxImageListType
*m_normal_image_list
;
750 int m_normal_spacing
;
754 wxTimer
*m_renameTimer
;
759 // for double click logic
760 size_t m_lineLastClicked
,
761 m_lineBeforeLastClicked
;
764 // the total count of items in a virtual list control
767 // the object maintaining the items selection state, only used in virtual
769 wxSelectionStore m_selStore
;
771 // common part of all ctors
774 // get the line data for the given index
775 wxListLineData
*GetLine(size_t n
) const
777 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
781 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
789 // get a dummy line which can be used for geometry calculations and such:
790 // you must use GetLine() if you want to really draw the line
791 wxListLineData
*GetDummyLine() const;
793 // cache the line data of the n-th line in m_lines[0]
794 void CacheLineData(size_t line
);
796 // get the range of visible lines
797 void GetVisibleLinesRange(size_t *from
, size_t *to
);
799 // force us to recalculate the range of visible lines
800 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
802 // get the colour to be used for drawing the rules
803 wxColour
GetRuleColour() const
808 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
813 // initialize the current item if needed
814 void UpdateCurrent();
816 // delete all items but don't refresh: called from dtor
817 void DoDeleteAllItems();
819 // the height of one line using the current font
820 wxCoord m_lineHeight
;
822 // the total header width or 0 if not calculated yet
823 wxCoord m_headerWidth
;
825 // the first and last lines being shown on screen right now (inclusive),
826 // both may be -1 if they must be calculated so never access them directly:
827 // use GetVisibleLinesRange() above instead
831 // the brushes to use for item highlighting when we do/don't have focus
832 wxBrush
*m_highlightBrush
,
833 *m_highlightUnfocusedBrush
;
835 // if this is > 0, the control is frozen and doesn't redraw itself
836 size_t m_freezeCount
;
838 DECLARE_DYNAMIC_CLASS(wxListMainWindow
)
839 DECLARE_EVENT_TABLE()
841 friend class wxGenericListCtrl
;
844 // ============================================================================
846 // ============================================================================
848 //-----------------------------------------------------------------------------
850 //-----------------------------------------------------------------------------
852 wxListItemData::~wxListItemData()
854 // in the virtual list control the attributes are managed by the main
855 // program, so don't delete them
856 if ( !m_owner
->IsVirtual() )
864 void wxListItemData::Init()
872 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
878 if ( owner
->InReportView() )
888 void wxListItemData::SetItem( const wxListItem
&info
)
890 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
891 SetText(info
.m_text
);
892 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
893 m_image
= info
.m_image
;
894 if ( info
.m_mask
& wxLIST_MASK_DATA
)
895 m_data
= info
.m_data
;
897 if ( info
.HasAttributes() )
900 *m_attr
= *info
.GetAttributes();
902 m_attr
= new wxListItemAttr(*info
.GetAttributes());
910 m_rect
->width
= info
.m_width
;
914 void wxListItemData::SetPosition( int x
, int y
)
916 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
922 void wxListItemData::SetSize( int width
, int height
)
924 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
927 m_rect
->width
= width
;
929 m_rect
->height
= height
;
932 bool wxListItemData::IsHit( int x
, int y
) const
934 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
936 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
939 int wxListItemData::GetX() const
941 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
946 int wxListItemData::GetY() const
948 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
953 int wxListItemData::GetWidth() const
955 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
957 return m_rect
->width
;
960 int wxListItemData::GetHeight() const
962 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
964 return m_rect
->height
;
967 void wxListItemData::GetItem( wxListItem
&info
) const
969 info
.m_text
= m_text
;
970 info
.m_image
= m_image
;
971 info
.m_data
= m_data
;
975 if ( m_attr
->HasTextColour() )
976 info
.SetTextColour(m_attr
->GetTextColour());
977 if ( m_attr
->HasBackgroundColour() )
978 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
979 if ( m_attr
->HasFont() )
980 info
.SetFont(m_attr
->GetFont());
984 //-----------------------------------------------------------------------------
986 //-----------------------------------------------------------------------------
988 void wxListHeaderData::Init()
999 wxListHeaderData::wxListHeaderData()
1004 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
1011 void wxListHeaderData::SetItem( const wxListItem
&item
)
1013 m_mask
= item
.m_mask
;
1015 if ( m_mask
& wxLIST_MASK_TEXT
)
1016 m_text
= item
.m_text
;
1018 if ( m_mask
& wxLIST_MASK_IMAGE
)
1019 m_image
= item
.m_image
;
1021 if ( m_mask
& wxLIST_MASK_FORMAT
)
1022 m_format
= item
.m_format
;
1024 if ( m_mask
& wxLIST_MASK_WIDTH
)
1025 SetWidth(item
.m_width
);
1028 void wxListHeaderData::SetPosition( int x
, int y
)
1034 void wxListHeaderData::SetHeight( int h
)
1039 void wxListHeaderData::SetWidth( int w
)
1043 m_width
= WIDTH_COL_DEFAULT
;
1044 else if (m_width
< WIDTH_COL_MIN
)
1045 m_width
= WIDTH_COL_MIN
;
1048 void wxListHeaderData::SetFormat( int format
)
1053 bool wxListHeaderData::HasImage() const
1055 return m_image
!= -1;
1058 bool wxListHeaderData::IsHit( int x
, int y
) const
1060 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
1063 void wxListHeaderData::GetItem( wxListItem
& item
)
1065 item
.m_mask
= m_mask
;
1066 item
.m_text
= m_text
;
1067 item
.m_image
= m_image
;
1068 item
.m_format
= m_format
;
1069 item
.m_width
= m_width
;
1072 int wxListHeaderData::GetImage() const
1077 int wxListHeaderData::GetWidth() const
1082 int wxListHeaderData::GetFormat() const
1087 //-----------------------------------------------------------------------------
1089 //-----------------------------------------------------------------------------
1091 inline int wxListLineData::GetMode() const
1093 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE
;
1096 inline bool wxListLineData::InReportView() const
1098 return m_owner
->HasFlag(wxLC_REPORT
);
1101 inline bool wxListLineData::IsVirtual() const
1103 return m_owner
->IsVirtual();
1106 wxListLineData::wxListLineData( wxListMainWindow
*owner
)
1110 if ( InReportView() )
1116 m_gi
= new GeometryInfo
;
1119 m_highlighted
= FALSE
;
1121 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
1124 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
1126 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1127 wxCHECK_RET( node
, _T("no subitems at all??") );
1129 wxListItemData
*item
= node
->GetData();
1134 switch ( GetMode() )
1137 case wxLC_SMALL_ICON
:
1138 m_gi
->m_rectAll
.width
= spacing
;
1140 s
= item
->GetText();
1145 m_gi
->m_rectLabel
.width
=
1146 m_gi
->m_rectLabel
.height
= 0;
1150 dc
->GetTextExtent( s
, &lw
, &lh
);
1154 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1156 m_gi
->m_rectAll
.width
= lw
;
1158 m_gi
->m_rectLabel
.width
= lw
;
1159 m_gi
->m_rectLabel
.height
= lh
;
1162 if (item
->HasImage())
1165 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1166 m_gi
->m_rectIcon
.width
= w
+ 8;
1167 m_gi
->m_rectIcon
.height
= h
+ 8;
1169 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1170 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1171 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1172 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1175 if ( item
->HasText() )
1177 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectLabel
.width
;
1178 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectLabel
.height
;
1180 else // no text, highlight the icon
1182 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectIcon
.width
;
1183 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectIcon
.height
;
1188 s
= item
->GetTextForMeasuring();
1190 dc
->GetTextExtent( s
, &lw
, &lh
);
1194 m_gi
->m_rectLabel
.width
= lw
;
1195 m_gi
->m_rectLabel
.height
= lh
;
1197 m_gi
->m_rectAll
.width
= lw
;
1198 m_gi
->m_rectAll
.height
= lh
;
1200 if (item
->HasImage())
1203 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1204 m_gi
->m_rectIcon
.width
= w
;
1205 m_gi
->m_rectIcon
.height
= h
;
1207 m_gi
->m_rectAll
.width
+= 4 + w
;
1208 if (h
> m_gi
->m_rectAll
.height
)
1209 m_gi
->m_rectAll
.height
= h
;
1212 m_gi
->m_rectHighlight
.width
= m_gi
->m_rectAll
.width
;
1213 m_gi
->m_rectHighlight
.height
= m_gi
->m_rectAll
.height
;
1217 wxFAIL_MSG( _T("unexpected call to SetSize") );
1221 wxFAIL_MSG( _T("unknown mode") );
1225 void wxListLineData::SetPosition( int x
, int y
, int spacing
)
1227 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1228 wxCHECK_RET( node
, _T("no subitems at all??") );
1230 wxListItemData
*item
= node
->GetData();
1232 switch ( GetMode() )
1235 case wxLC_SMALL_ICON
:
1236 m_gi
->m_rectAll
.x
= x
;
1237 m_gi
->m_rectAll
.y
= y
;
1239 if ( item
->HasImage() )
1241 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4 +
1242 (m_gi
->m_rectAll
.width
- m_gi
->m_rectIcon
.width
) / 2;
1243 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1246 if ( item
->HasText() )
1248 if (m_gi
->m_rectAll
.width
> spacing
)
1249 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1251 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1252 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1253 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectLabel
.x
- 2;
1254 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectLabel
.y
- 2;
1256 else // no text, highlight the icon
1258 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectIcon
.x
- 4;
1259 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectIcon
.y
- 4;
1264 m_gi
->m_rectAll
.x
= x
;
1265 m_gi
->m_rectAll
.y
= y
;
1267 m_gi
->m_rectHighlight
.x
= m_gi
->m_rectAll
.x
;
1268 m_gi
->m_rectHighlight
.y
= m_gi
->m_rectAll
.y
;
1269 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1271 if (item
->HasImage())
1273 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1274 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1275 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1279 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1284 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1288 wxFAIL_MSG( _T("unknown mode") );
1292 void wxListLineData::InitItems( int num
)
1294 for (int i
= 0; i
< num
; i
++)
1295 m_items
.Append( new wxListItemData(m_owner
) );
1298 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1300 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1301 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1303 wxListItemData
*item
= node
->GetData();
1304 item
->SetItem( info
);
1307 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1309 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1312 wxListItemData
*item
= node
->GetData();
1313 item
->GetItem( info
);
1317 wxString
wxListLineData::GetText(int index
) const
1321 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1324 wxListItemData
*item
= node
->GetData();
1325 s
= item
->GetText();
1331 void wxListLineData::SetText( int index
, const wxString s
)
1333 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1336 wxListItemData
*item
= node
->GetData();
1341 void wxListLineData::SetImage( int index
, int image
)
1343 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1344 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1346 wxListItemData
*item
= node
->GetData();
1347 item
->SetImage(image
);
1350 int wxListLineData::GetImage( int index
) const
1352 wxListItemDataList::compatibility_iterator node
= m_items
.Item( index
);
1353 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1355 wxListItemData
*item
= node
->GetData();
1356 return item
->GetImage();
1359 wxListItemAttr
*wxListLineData::GetAttr() const
1361 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1362 wxCHECK_MSG( node
, NULL
, _T("invalid column index in GetAttr()") );
1364 wxListItemData
*item
= node
->GetData();
1365 return item
->GetAttr();
1368 void wxListLineData::SetAttr(wxListItemAttr
*attr
)
1370 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1371 wxCHECK_RET( node
, _T("invalid column index in SetAttr()") );
1373 wxListItemData
*item
= node
->GetData();
1374 item
->SetAttr(attr
);
1377 bool wxListLineData::SetAttributes(wxDC
*dc
,
1378 const wxListItemAttr
*attr
,
1381 wxWindow
*listctrl
= m_owner
->GetParent();
1385 // don't use foreground colour for drawing highlighted items - this might
1386 // make them completely invisible (and there is no way to do bit
1387 // arithmetics on wxColour, unfortunately)
1391 colText
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
1395 if ( attr
&& attr
->HasTextColour() )
1397 colText
= attr
->GetTextColour();
1401 colText
= listctrl
->GetForegroundColour();
1405 dc
->SetTextForeground(colText
);
1409 if ( attr
&& attr
->HasFont() )
1411 font
= attr
->GetFont();
1415 font
= listctrl
->GetFont();
1421 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1422 if ( highlighted
|| hasBgCol
)
1426 dc
->SetBrush( *m_owner
->GetHighlightBrush() );
1430 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1433 dc
->SetPen( *wxTRANSPARENT_PEN
);
1441 void wxListLineData::Draw( wxDC
*dc
)
1443 wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1444 wxCHECK_RET( node
, _T("no subitems at all??") );
1446 bool highlighted
= IsHighlighted();
1448 wxListItemAttr
*attr
= GetAttr();
1450 if ( SetAttributes(dc
, attr
, highlighted
) )
1452 dc
->DrawRectangle( m_gi
->m_rectHighlight
);
1455 // just for debugging to better see where the items are
1457 dc
->SetPen(*wxRED_PEN
);
1458 dc
->SetBrush(*wxTRANSPARENT_BRUSH
);
1459 dc
->DrawRectangle( m_gi
->m_rectAll
);
1460 dc
->SetPen(*wxGREEN_PEN
);
1461 dc
->DrawRectangle( m_gi
->m_rectIcon
);
1464 wxListItemData
*item
= node
->GetData();
1465 if (item
->HasImage())
1467 // centre the image inside our rectangle, this looks nicer when items
1468 // ae aligned in a row
1469 const wxRect
& rectIcon
= m_gi
->m_rectIcon
;
1471 m_owner
->DrawImage(item
->GetImage(), dc
, rectIcon
.x
, rectIcon
.y
);
1474 if (item
->HasText())
1476 const wxRect
& rectLabel
= m_gi
->m_rectLabel
;
1478 wxDCClipper
clipper(*dc
, rectLabel
);
1479 dc
->DrawText(item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1483 void wxListLineData::DrawInReportMode( wxDC
*dc
,
1485 const wxRect
& rectHL
,
1488 // TODO: later we should support setting different attributes for
1489 // different columns - to do it, just add "col" argument to
1490 // GetAttr() and move these lines into the loop below
1491 wxListItemAttr
*attr
= GetAttr();
1492 if ( SetAttributes(dc
, attr
, highlighted
) )
1494 dc
->DrawRectangle( rectHL
);
1497 wxCoord x
= rect
.x
+ HEADER_OFFSET_X
,
1498 y
= rect
.y
+ (LINE_SPACING
+ EXTRA_HEIGHT
) / 2;
1501 for ( wxListItemDataList::compatibility_iterator node
= m_items
.GetFirst();
1503 node
= node
->GetNext(), col
++ )
1505 wxListItemData
*item
= node
->GetData();
1507 int width
= m_owner
->GetColumnWidth(col
);
1511 if ( item
->HasImage() )
1514 m_owner
->DrawImage( item
->GetImage(), dc
, xOld
, y
);
1515 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1517 ix
+= IMAGE_MARGIN_IN_REPORT_MODE
;
1523 wxDCClipper
clipper(*dc
, xOld
, y
, width
- 8, rect
.height
);
1525 if ( item
->HasText() )
1527 DrawTextFormatted(dc
, item
->GetText(), col
, xOld
, y
, width
- 8);
1532 void wxListLineData::DrawTextFormatted(wxDC
*dc
,
1533 const wxString
&text
,
1539 wxString drawntext
, ellipsis
;
1540 wxCoord w
, h
, base_w
;
1543 // determine if the string can fit inside the current width
1544 dc
->GetTextExtent(text
, &w
, &h
);
1547 // it can, draw it using the items alignment
1548 m_owner
->GetColumn(col
, item
);
1549 switch ( item
.GetAlign() )
1552 wxFAIL_MSG( _T("unknown list item format") );
1555 case wxLIST_FORMAT_LEFT
:
1559 case wxLIST_FORMAT_RIGHT
:
1563 case wxLIST_FORMAT_CENTER
:
1564 x
+= (width
- w
) / 2;
1568 dc
->DrawText(text
, x
, y
);
1570 else // otherwise, truncate and add an ellipsis if possible
1572 // determine the base width
1573 ellipsis
= wxString(wxT("..."));
1574 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1576 // continue until we have enough space or only one character left
1578 size_t len
= text
.Length();
1579 drawntext
= text
.Left(len
);
1582 dc
->GetTextExtent(drawntext
.Last(), &w_c
, &h_c
);
1583 drawntext
.RemoveLast();
1586 if (w
+ base_w
<= width
)
1590 // if still not enough space, remove ellipsis characters
1591 while (ellipsis
.Length() > 0 && w
+ base_w
> width
)
1593 ellipsis
= ellipsis
.Left(ellipsis
.Length() - 1);
1594 dc
->GetTextExtent(ellipsis
, &base_w
, &h
);
1597 // now draw the text
1598 dc
->DrawText(drawntext
, x
, y
);
1599 dc
->DrawText(ellipsis
, x
+ w
, y
);
1603 bool wxListLineData::Highlight( bool on
)
1605 wxCHECK_MSG( !IsVirtual(), FALSE
, _T("unexpected call to Highlight") );
1607 if ( on
== m_highlighted
)
1615 void wxListLineData::ReverseHighlight( void )
1617 Highlight(!IsHighlighted());
1620 //-----------------------------------------------------------------------------
1621 // wxListHeaderWindow
1622 //-----------------------------------------------------------------------------
1624 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
)
1626 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1627 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1628 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1629 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1632 void wxListHeaderWindow::Init()
1634 m_currentCursor
= (wxCursor
*) NULL
;
1635 m_isDragging
= FALSE
;
1639 wxListHeaderWindow::wxListHeaderWindow()
1643 m_owner
= (wxListMainWindow
*) NULL
;
1644 m_resizeCursor
= (wxCursor
*) NULL
;
1647 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
,
1649 wxListMainWindow
*owner
,
1653 const wxString
&name
)
1654 : wxWindow( win
, id
, pos
, size
, style
, name
)
1659 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1662 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1663 SetDefaultForegroundColour( attr
.colFg
);
1664 SetDefaultBackgroundColour( attr
.colBg
);
1665 SetDefaultFont( attr
.font
);
1667 SetDefaultForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
1668 SetDefaultBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
));
1669 SetDefaultFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
1673 wxListHeaderWindow::~wxListHeaderWindow()
1675 delete m_resizeCursor
;
1678 #ifdef __WXUNIVERSAL__
1679 #include "wx/univ/renderer.h"
1680 #include "wx/univ/theme.h"
1683 // shift the DC origin to match the position of the main window horz
1684 // scrollbar: this allows us to always use logical coords
1685 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1688 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1691 m_owner
->GetViewStart( &x
, NULL
);
1693 // account for the horz scrollbar offset
1694 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1697 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1699 wxPaintDC
dc( this );
1706 dc
.SetFont( GetFont() );
1708 // width and height of the entire header window
1710 GetClientSize( &w
, &h
);
1711 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1713 dc
.SetBackgroundMode(wxTRANSPARENT
);
1715 dc
.SetTextForeground(GetForegroundColour());
1717 int x
= HEADER_OFFSET_X
;
1719 int numColumns
= m_owner
->GetColumnCount();
1721 for ( int i
= 0; i
< numColumns
&& x
< w
; i
++ )
1723 m_owner
->GetColumn( i
, item
);
1724 int wCol
= item
.m_width
;
1726 // the width of the rect to draw: make it smaller to fit entirely
1727 // inside the column rect
1730 wxRendererNative::Get().DrawHeaderButton
1734 wxRect(x
, HEADER_OFFSET_Y
, cw
, h
- 2),
1735 m_parent
->IsEnabled() ? 0
1736 : wxCONTROL_DISABLED
1739 // see if we have enough space for the column label
1741 // for this we need the width of the text
1744 dc
.GetTextExtent(item
.GetText(), &wLabel
, &hLabel
);
1745 wLabel
+= 2*EXTRA_WIDTH
;
1747 // and the width of the icon, if any
1748 static const int MARGIN_BETWEEN_TEXT_AND_ICON
= 2;
1749 int ix
= 0, // init them just to suppress the compiler warnings
1751 const int image
= item
.m_image
;
1752 wxImageListType
*imageList
;
1755 imageList
= m_owner
->m_small_image_list
;
1758 imageList
->GetSize(image
, ix
, iy
);
1759 wLabel
+= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1767 // ignore alignment if there is not enough space anyhow
1769 switch ( wLabel
< cw
? item
.GetAlign() : wxLIST_FORMAT_LEFT
)
1772 wxFAIL_MSG( _T("unknown list item format") );
1775 case wxLIST_FORMAT_LEFT
:
1779 case wxLIST_FORMAT_RIGHT
:
1780 xAligned
= x
+ cw
- wLabel
;
1783 case wxLIST_FORMAT_CENTER
:
1784 xAligned
= x
+ (cw
- wLabel
) / 2;
1789 // if we have an image, draw it on the right of the label
1796 xAligned
+ wLabel
- ix
- MARGIN_BETWEEN_TEXT_AND_ICON
,
1797 HEADER_OFFSET_Y
+ (h
- 4 - iy
)/2,
1798 wxIMAGELIST_DRAW_TRANSPARENT
1801 cw
-= ix
+ MARGIN_BETWEEN_TEXT_AND_ICON
;
1804 // draw the text clipping it so that it doesn't overwrite the column
1806 wxDCClipper
clipper(dc
, x
, HEADER_OFFSET_Y
, cw
, h
- 4 );
1808 dc
.DrawText( item
.GetText(),
1809 xAligned
+ EXTRA_WIDTH
, h
/ 2 - hLabel
/ 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1817 void wxListHeaderWindow::DrawCurrent()
1819 int x1
= m_currentX
;
1821 m_owner
->ClientToScreen( &x1
, &y1
);
1823 int x2
= m_currentX
;
1825 m_owner
->GetClientSize( NULL
, &y2
);
1826 m_owner
->ClientToScreen( &x2
, &y2
);
1829 dc
.SetLogicalFunction( wxINVERT
);
1830 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1831 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1835 dc
.DrawLine( x1
, y1
, x2
, y2
);
1837 dc
.SetLogicalFunction( wxCOPY
);
1839 dc
.SetPen( wxNullPen
);
1840 dc
.SetBrush( wxNullBrush
);
1843 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1845 // we want to work with logical coords
1847 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1848 int y
= event
.GetY();
1852 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING
, event
.GetPosition());
1854 // we don't draw the line beyond our window, but we allow dragging it
1857 GetClientSize( &w
, NULL
);
1858 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1861 // erase the line if it was drawn
1862 if ( m_currentX
< w
)
1865 if (event
.ButtonUp())
1868 m_isDragging
= FALSE
;
1870 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1871 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG
, event
.GetPosition());
1878 m_currentX
= m_minX
+ 7;
1880 // draw in the new location
1881 if ( m_currentX
< w
)
1885 else // not dragging
1888 bool hit_border
= FALSE
;
1890 // end of the current column
1893 // find the column where this event occured
1895 countCol
= m_owner
->GetColumnCount();
1896 for (col
= 0; col
< countCol
; col
++)
1898 xpos
+= m_owner
->GetColumnWidth( col
);
1901 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1903 // near the column border
1910 // inside the column
1917 if ( col
== countCol
)
1920 if (event
.LeftDown() || event
.RightUp())
1922 if (hit_border
&& event
.LeftDown())
1924 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG
,
1925 event
.GetPosition()) )
1927 m_isDragging
= TRUE
;
1932 //else: column resizing was vetoed by the user code
1934 else // click on a column
1936 SendListEvent( event
.LeftDown()
1937 ? wxEVT_COMMAND_LIST_COL_CLICK
1938 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK
,
1939 event
.GetPosition());
1942 else if (event
.Moving())
1947 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1948 m_currentCursor
= m_resizeCursor
;
1952 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1953 m_currentCursor
= wxSTANDARD_CURSOR
;
1957 SetCursor(*m_currentCursor
);
1962 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1964 m_owner
->SetFocus();
1968 bool wxListHeaderWindow::SendListEvent(wxEventType type
, wxPoint pos
)
1970 wxWindow
*parent
= GetParent();
1971 wxListEvent
le( type
, parent
->GetId() );
1972 le
.SetEventObject( parent
);
1973 le
.m_pointDrag
= pos
;
1975 // the position should be relative to the parent window, not
1976 // this one for compatibility with MSW and common sense: the
1977 // user code doesn't know anything at all about this header
1978 // window, so why should it get positions relative to it?
1979 le
.m_pointDrag
.y
-= GetSize().y
;
1981 le
.m_col
= m_column
;
1982 return !parent
->GetEventHandler()->ProcessEvent( le
) || le
.IsAllowed();
1985 //-----------------------------------------------------------------------------
1986 // wxListRenameTimer (internal)
1987 //-----------------------------------------------------------------------------
1989 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1994 void wxListRenameTimer::Notify()
1996 m_owner
->OnRenameTimer();
1999 //-----------------------------------------------------------------------------
2000 // wxListTextCtrl (internal)
2001 //-----------------------------------------------------------------------------
2003 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
2004 EVT_CHAR (wxListTextCtrl::OnChar
)
2005 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
2006 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
2009 wxListTextCtrl::wxListTextCtrl(wxListMainWindow
*owner
, size_t itemEdit
)
2010 : m_startValue(owner
->GetItemText(itemEdit
)),
2011 m_itemEdited(itemEdit
)
2016 wxRect rectLabel
= owner
->GetLineLabelRect(itemEdit
);
2018 m_owner
->CalcScrolledPosition(rectLabel
.x
, rectLabel
.y
,
2019 &rectLabel
.x
, &rectLabel
.y
);
2021 (void)Create(owner
, wxID_ANY
, m_startValue
,
2022 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2023 wxSize(rectLabel
.width
+11,rectLabel
.height
+8));
2026 void wxListTextCtrl::Finish()
2030 wxPendingDelete
.Append(this);
2034 m_owner
->SetFocus();
2038 bool wxListTextCtrl::AcceptChanges()
2040 const wxString value
= GetValue();
2042 if ( value
== m_startValue
)
2044 // nothing changed, always accept
2048 if ( !m_owner
->OnRenameAccept(m_itemEdited
, value
) )
2050 // vetoed by the user
2054 // accepted, do rename the item
2055 m_owner
->SetItemText(m_itemEdited
, value
);
2060 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
2062 switch ( event
.m_keyCode
)
2065 if ( !AcceptChanges() )
2067 // vetoed by the user code
2070 //else: fall through
2074 m_owner
->OnRenameCancelled( m_itemEdited
);
2082 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
2090 // auto-grow the textctrl:
2091 wxSize parentSize
= m_owner
->GetSize();
2092 wxPoint myPos
= GetPosition();
2093 wxSize mySize
= GetSize();
2095 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
);
2096 if (myPos
.x
+ sx
> parentSize
.x
)
2097 sx
= parentSize
.x
- myPos
.x
;
2105 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&event
)
2109 // We must finish regardless of success, otherwise we'll get focus problems
2112 if ( !AcceptChanges() )
2113 m_owner
->OnRenameCancelled( m_itemEdited
);
2119 //-----------------------------------------------------------------------------
2121 //-----------------------------------------------------------------------------
2123 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
)
2125 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
2126 EVT_PAINT (wxListMainWindow::OnPaint
)
2127 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
2128 EVT_CHAR (wxListMainWindow::OnChar
)
2129 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
2130 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
2131 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
2132 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
2135 void wxListMainWindow::Init()
2140 m_lineTo
= (size_t)-1;
2146 m_small_image_list
= (wxImageListType
*) NULL
;
2147 m_normal_image_list
= (wxImageListType
*) NULL
;
2149 m_small_spacing
= 30;
2150 m_normal_spacing
= 40;
2154 m_isCreated
= FALSE
;
2156 m_lastOnSame
= FALSE
;
2157 m_renameTimer
= new wxListRenameTimer( this );
2161 m_lineBeforeLastClicked
= (size_t)-1;
2166 wxListMainWindow::wxListMainWindow()
2171 m_highlightUnfocusedBrush
= (wxBrush
*) NULL
;
2174 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
2179 const wxString
&name
)
2180 : wxScrolledWindow( parent
, id
, pos
, size
,
2181 style
| wxHSCROLL
| wxVSCROLL
, name
)
2185 m_highlightBrush
= new wxBrush
2187 wxSystemSettings::GetColour
2189 wxSYS_COLOUR_HIGHLIGHT
2194 m_highlightUnfocusedBrush
= new wxBrush
2196 wxSystemSettings::GetColour
2198 wxSYS_COLOUR_BTNSHADOW
2206 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2208 wxVisualAttributes attr
= wxGenericListCtrl::GetClassDefaultAttributes();
2209 SetDefaultForegroundColour( attr
.colFg
);
2210 SetDefaultBackgroundColour( attr
.colBg
);
2211 SetDefaultFont( attr
.font
);
2214 wxListMainWindow::~wxListMainWindow()
2217 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
2219 delete m_highlightBrush
;
2220 delete m_highlightUnfocusedBrush
;
2222 delete m_renameTimer
;
2225 void wxListMainWindow::CacheLineData(size_t line
)
2227 wxGenericListCtrl
*listctrl
= GetListCtrl();
2229 wxListLineData
*ld
= GetDummyLine();
2231 size_t countCol
= GetColumnCount();
2232 for ( size_t col
= 0; col
< countCol
; col
++ )
2234 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
2237 ld
->SetImage(listctrl
->OnGetItemImage(line
));
2238 ld
->SetAttr(listctrl
->OnGetItemAttr(line
));
2241 wxListLineData
*wxListMainWindow::GetDummyLine() const
2243 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2245 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2247 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2249 // we need to recreate the dummy line if the number of columns in the
2250 // control changed as it would have the incorrect number of fields
2252 if ( !m_lines
.IsEmpty() &&
2253 m_lines
[0].m_items
.GetCount() != (size_t)GetColumnCount() )
2255 self
->m_lines
.Clear();
2258 if ( m_lines
.IsEmpty() )
2260 wxListLineData
*line
= new wxListLineData(self
);
2261 self
->m_lines
.Add(line
);
2263 // don't waste extra memory -- there never going to be anything
2264 // else/more in this array
2265 self
->m_lines
.Shrink();
2271 // ----------------------------------------------------------------------------
2272 // line geometry (report mode only)
2273 // ----------------------------------------------------------------------------
2275 wxCoord
wxListMainWindow::GetLineHeight() const
2277 // we cache the line height as calling GetTextExtent() is slow
2278 if ( !m_lineHeight
)
2280 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
2282 wxClientDC
dc( self
);
2283 dc
.SetFont( GetFont() );
2286 dc
.GetTextExtent(_T("H"), NULL
, &y
);
2288 if ( m_small_image_list
&& m_small_image_list
->GetImageCount() )
2292 m_small_image_list
->GetSize(0, iw
, ih
);
2297 self
->m_lineHeight
= y
+ LINE_SPACING
;
2300 return m_lineHeight
;
2303 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
2305 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2307 return LINE_SPACING
+ line
*GetLineHeight();
2310 wxRect
wxListMainWindow::GetLineRect(size_t line
) const
2312 if ( !InReportView() )
2313 return GetLine(line
)->m_gi
->m_rectAll
;
2316 rect
.x
= HEADER_OFFSET_X
;
2317 rect
.y
= GetLineY(line
);
2318 rect
.width
= GetHeaderWidth();
2319 rect
.height
= GetLineHeight();
2324 wxRect
wxListMainWindow::GetLineLabelRect(size_t line
) const
2326 if ( !InReportView() )
2327 return GetLine(line
)->m_gi
->m_rectLabel
;
2330 rect
.x
= HEADER_OFFSET_X
;
2331 rect
.y
= GetLineY(line
);
2332 rect
.width
= GetColumnWidth(0);
2333 rect
.height
= GetLineHeight();
2338 wxRect
wxListMainWindow::GetLineIconRect(size_t line
) const
2340 if ( !InReportView() )
2341 return GetLine(line
)->m_gi
->m_rectIcon
;
2343 wxListLineData
*ld
= GetLine(line
);
2344 wxASSERT_MSG( ld
->HasImage(), _T("should have an image") );
2347 rect
.x
= HEADER_OFFSET_X
;
2348 rect
.y
= GetLineY(line
);
2349 GetImageSize(ld
->GetImage(), rect
.width
, rect
.height
);
2354 wxRect
wxListMainWindow::GetLineHighlightRect(size_t line
) const
2356 return InReportView() ? GetLineRect(line
)
2357 : GetLine(line
)->m_gi
->m_rectHighlight
;
2360 long wxListMainWindow::HitTestLine(size_t line
, int x
, int y
) const
2362 wxASSERT_MSG( line
< GetItemCount(), _T("invalid line in HitTestLine") );
2364 wxListLineData
*ld
= GetLine(line
);
2366 if ( ld
->HasImage() && GetLineIconRect(line
).Inside(x
, y
) )
2367 return wxLIST_HITTEST_ONITEMICON
;
2369 // VS: Testing for "ld->HasText() || InReportView()" instead of
2370 // "ld->HasText()" is needed to make empty lines in report view
2372 if ( ld
->HasText() || InReportView() )
2374 wxRect rect
= InReportView() ? GetLineRect(line
)
2375 : GetLineLabelRect(line
);
2377 if ( rect
.Inside(x
, y
) )
2378 return wxLIST_HITTEST_ONITEMLABEL
;
2384 // ----------------------------------------------------------------------------
2385 // highlight (selection) handling
2386 // ----------------------------------------------------------------------------
2388 bool wxListMainWindow::IsHighlighted(size_t line
) const
2392 return m_selStore
.IsSelected(line
);
2396 wxListLineData
*ld
= GetLine(line
);
2397 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHighlighted") );
2399 return ld
->IsHighlighted();
2403 void wxListMainWindow::HighlightLines( size_t lineFrom
,
2409 wxArrayInt linesChanged
;
2410 if ( !m_selStore
.SelectRange(lineFrom
, lineTo
, highlight
,
2413 // meny items changed state, refresh everything
2414 RefreshLines(lineFrom
, lineTo
);
2416 else // only a few items changed state, refresh only them
2418 size_t count
= linesChanged
.GetCount();
2419 for ( size_t n
= 0; n
< count
; n
++ )
2421 RefreshLine(linesChanged
[n
]);
2425 else // iterate over all items in non report view
2427 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2429 if ( HighlightLine(line
, highlight
) )
2437 bool wxListMainWindow::HighlightLine( size_t line
, bool highlight
)
2443 changed
= m_selStore
.SelectItem(line
, highlight
);
2447 wxListLineData
*ld
= GetLine(line
);
2448 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in HighlightLine") );
2450 changed
= ld
->Highlight(highlight
);
2455 SendNotify( line
, highlight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2456 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2462 void wxListMainWindow::RefreshLine( size_t line
)
2464 if ( InReportView() )
2466 size_t visibleFrom
, visibleTo
;
2467 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2469 if ( line
< visibleFrom
|| line
> visibleTo
)
2473 wxRect rect
= GetLineRect(line
);
2475 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2476 RefreshRect( rect
);
2479 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2481 // we suppose that they are ordered by caller
2482 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2484 wxASSERT_MSG( lineTo
< GetItemCount(), _T("invalid line range") );
2486 if ( InReportView() )
2488 size_t visibleFrom
, visibleTo
;
2489 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2491 if ( lineFrom
< visibleFrom
)
2492 lineFrom
= visibleFrom
;
2493 if ( lineTo
> visibleTo
)
2498 rect
.y
= GetLineY(lineFrom
);
2499 rect
.width
= GetClientSize().x
;
2500 rect
.height
= GetLineY(lineTo
) - rect
.y
+ GetLineHeight();
2502 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2503 RefreshRect( rect
);
2507 // TODO: this should be optimized...
2508 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2515 void wxListMainWindow::RefreshAfter( size_t lineFrom
)
2517 if ( InReportView() )
2519 size_t visibleFrom
, visibleTo
;
2520 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2522 if ( lineFrom
< visibleFrom
)
2523 lineFrom
= visibleFrom
;
2524 else if ( lineFrom
> visibleTo
)
2529 rect
.y
= GetLineY(lineFrom
);
2530 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2532 wxSize size
= GetClientSize();
2533 rect
.width
= size
.x
;
2534 // refresh till the bottom of the window
2535 rect
.height
= size
.y
- rect
.y
;
2537 RefreshRect( rect
);
2541 // TODO: how to do it more efficiently?
2546 void wxListMainWindow::RefreshSelected()
2552 if ( InReportView() )
2554 GetVisibleLinesRange(&from
, &to
);
2559 to
= GetItemCount() - 1;
2562 if ( HasCurrent() && m_current
>= from
&& m_current
<= to
)
2564 RefreshLine(m_current
);
2567 for ( size_t line
= from
; line
<= to
; line
++ )
2569 // NB: the test works as expected even if m_current == -1
2570 if ( line
!= m_current
&& IsHighlighted(line
) )
2577 void wxListMainWindow::Freeze()
2582 void wxListMainWindow::Thaw()
2584 wxCHECK_RET( m_freezeCount
> 0, _T("thawing unfrozen list control?") );
2586 if ( !--m_freezeCount
)
2592 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2594 // Note: a wxPaintDC must be constructed even if no drawing is
2595 // done (a Windows requirement).
2596 wxPaintDC
dc( this );
2598 if ( IsEmpty() || m_freezeCount
)
2600 // nothing to draw or not the moment to draw it
2606 // delay the repainting until we calculate all the items positions
2613 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2617 dc
.SetFont( GetFont() );
2619 if ( InReportView() )
2621 int lineHeight
= GetLineHeight();
2623 size_t visibleFrom
, visibleTo
;
2624 GetVisibleLinesRange(&visibleFrom
, &visibleTo
);
2627 wxCoord xOrig
, yOrig
;
2628 CalcUnscrolledPosition(0, 0, &xOrig
, &yOrig
);
2630 // tell the caller cache to cache the data
2633 wxListEvent
evCache(wxEVT_COMMAND_LIST_CACHE_HINT
,
2634 GetParent()->GetId());
2635 evCache
.SetEventObject( GetParent() );
2636 evCache
.m_oldItemIndex
= visibleFrom
;
2637 evCache
.m_itemIndex
= visibleTo
;
2638 GetParent()->GetEventHandler()->ProcessEvent( evCache
);
2641 for ( size_t line
= visibleFrom
; line
<= visibleTo
; line
++ )
2643 rectLine
= GetLineRect(line
);
2645 if ( !IsExposed(rectLine
.x
- xOrig
, rectLine
.y
- yOrig
,
2646 rectLine
.width
, rectLine
.height
) )
2648 // don't redraw unaffected lines to avoid flicker
2652 GetLine(line
)->DrawInReportMode( &dc
,
2654 GetLineHighlightRect(line
),
2655 IsHighlighted(line
) );
2658 if ( HasFlag(wxLC_HRULES
) )
2660 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2661 wxSize clientSize
= GetClientSize();
2663 // Don't draw the first one
2664 for ( size_t i
= visibleFrom
+1; i
<= visibleTo
; i
++ )
2667 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2668 dc
.DrawLine(0 - dev_x
, i
*lineHeight
,
2669 clientSize
.x
- dev_x
, i
*lineHeight
);
2672 // Draw last horizontal rule
2673 if ( visibleTo
== GetItemCount() - 1 )
2676 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2677 dc
.DrawLine(0 - dev_x
, (m_lineTo
+1)*lineHeight
,
2678 clientSize
.x
- dev_x
, (m_lineTo
+1)*lineHeight
);
2682 // Draw vertical rules if required
2683 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2685 wxPen
pen(GetRuleColour(), 1, wxSOLID
);
2687 wxRect firstItemRect
;
2688 wxRect lastItemRect
;
2689 GetItemRect(visibleFrom
, firstItemRect
);
2690 GetItemRect(visibleTo
, lastItemRect
);
2691 int x
= firstItemRect
.GetX();
2693 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2694 for (int col
= 0; col
< GetColumnCount(); col
++)
2696 int colWidth
= GetColumnWidth(col
);
2698 dc
.DrawLine(x
- dev_x
- 2, firstItemRect
.GetY() - 1 - dev_y
,
2699 x
- dev_x
- 2, lastItemRect
.GetBottom() + 1 - dev_y
);
2705 size_t count
= GetItemCount();
2706 for ( size_t i
= 0; i
< count
; i
++ )
2708 GetLine(i
)->Draw( &dc
);
2713 // Don't draw rect outline under Mac at all.
2718 dc
.SetPen( *wxBLACK_PEN
);
2719 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2720 dc
.DrawRectangle( GetLineHighlightRect(m_current
) );
2728 void wxListMainWindow::HighlightAll( bool on
)
2730 if ( IsSingleSel() )
2732 wxASSERT_MSG( !on
, _T("can't do this in a single sel control") );
2734 // we just have one item to turn off
2735 if ( HasCurrent() && IsHighlighted(m_current
) )
2737 HighlightLine(m_current
, FALSE
);
2738 RefreshLine(m_current
);
2743 HighlightLines(0, GetItemCount() - 1, on
);
2747 void wxListMainWindow::SendNotify( size_t line
,
2748 wxEventType command
,
2751 wxListEvent
le( command
, GetParent()->GetId() );
2752 le
.SetEventObject( GetParent() );
2753 le
.m_itemIndex
= line
;
2755 // set only for events which have position
2756 if ( point
!= wxDefaultPosition
)
2757 le
.m_pointDrag
= point
;
2759 // don't try to get the line info for virtual list controls: the main
2760 // program has it anyhow and if we did it would result in accessing all
2761 // the lines, even those which are not visible now and this is precisely
2762 // what we're trying to avoid
2763 if ( !IsVirtual() && (command
!= wxEVT_COMMAND_LIST_DELETE_ITEM
) )
2765 if ( line
!= (size_t)-1 )
2767 GetLine(line
)->GetItem( 0, le
.m_item
);
2769 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2771 //else: there may be no more such item
2773 GetParent()->GetEventHandler()->ProcessEvent( le
);
2776 void wxListMainWindow::ChangeCurrent(size_t current
)
2778 m_current
= current
;
2780 SendNotify(current
, wxEVT_COMMAND_LIST_ITEM_FOCUSED
);
2783 void wxListMainWindow::EditLabel( long item
)
2785 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2786 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2788 size_t itemEdit
= (size_t)item
;
2790 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2791 le
.SetEventObject( GetParent() );
2792 le
.m_itemIndex
= item
;
2793 wxListLineData
*data
= GetLine(itemEdit
);
2794 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2795 data
->GetItem( 0, le
.m_item
);
2796 if ( GetParent()->GetEventHandler()->ProcessEvent( le
) && !le
.IsAllowed() )
2798 // vetoed by user code
2802 // We have to call this here because the label in question might just have
2803 // been added and no screen update taken place.
2807 wxListTextCtrl
*text
= new wxListTextCtrl(this, itemEdit
);
2812 void wxListMainWindow::OnRenameTimer()
2814 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2816 EditLabel( m_current
);
2819 bool wxListMainWindow::OnRenameAccept(size_t itemEdit
, const wxString
& value
)
2821 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2822 le
.SetEventObject( GetParent() );
2823 le
.m_itemIndex
= itemEdit
;
2825 wxListLineData
*data
= GetLine(itemEdit
);
2826 wxCHECK_MSG( data
, FALSE
, _T("invalid index in OnRenameAccept()") );
2828 data
->GetItem( 0, le
.m_item
);
2829 le
.m_item
.m_text
= value
;
2830 return !GetParent()->GetEventHandler()->ProcessEvent( le
) ||
2834 void wxListMainWindow::OnRenameCancelled(size_t itemEdit
)
2836 // let owner know that the edit was cancelled
2837 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2839 le
.SetEditCanceled(TRUE
);
2841 le
.SetEventObject( GetParent() );
2842 le
.m_itemIndex
= itemEdit
;
2844 wxListLineData
*data
= GetLine(itemEdit
);
2845 wxCHECK_RET( data
, _T("invalid index in OnRenameCancelled()") );
2847 data
->GetItem( 0, le
.m_item
);
2849 GetEventHandler()->ProcessEvent( le
);
2852 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2854 event
.SetEventObject( GetParent() );
2855 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2858 if ( !HasCurrent() || IsEmpty() )
2864 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2865 event
.ButtonDClick()) )
2868 int x
= event
.GetX();
2869 int y
= event
.GetY();
2870 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2872 // where did we hit it (if we did)?
2875 size_t count
= GetItemCount(),
2878 if ( InReportView() )
2880 current
= y
/ GetLineHeight();
2881 if ( current
< count
)
2882 hitResult
= HitTestLine(current
, x
, y
);
2886 // TODO: optimize it too! this is less simple than for report view but
2887 // enumerating all items is still not a way to do it!!
2888 for ( current
= 0; current
< count
; current
++ )
2890 hitResult
= HitTestLine(current
, x
, y
);
2896 if (event
.Dragging())
2898 if (m_dragCount
== 0)
2900 // we have to report the raw, physical coords as we want to be
2901 // able to call HitTest(event.m_pointDrag) from the user code to
2902 // get the item being dragged
2903 m_dragStart
= event
.GetPosition();
2908 if (m_dragCount
!= 3)
2911 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2912 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2914 wxListEvent
le( command
, GetParent()->GetId() );
2915 le
.SetEventObject( GetParent() );
2916 le
.m_itemIndex
= current
;
2917 le
.m_pointDrag
= m_dragStart
;
2918 GetParent()->GetEventHandler()->ProcessEvent( le
);
2929 // outside of any item
2933 bool forceClick
= FALSE
;
2934 if (event
.ButtonDClick())
2936 m_renameTimer
->Stop();
2937 m_lastOnSame
= FALSE
;
2939 if ( current
== m_lineLastClicked
)
2941 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2947 // the first click was on another item, so don't interpret this as
2948 // a double click, but as a simple click instead
2953 if (event
.LeftUp() && m_lastOnSame
)
2955 if ((current
== m_current
) &&
2956 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2957 HasFlag(wxLC_EDIT_LABELS
) )
2959 m_renameTimer
->Start( 100, TRUE
);
2961 m_lastOnSame
= FALSE
;
2963 else if (event
.RightDown())
2965 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
2966 event
.GetPosition() );
2968 else if (event
.MiddleDown())
2970 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2972 else if ( event
.LeftDown() || forceClick
)
2974 m_lineBeforeLastClicked
= m_lineLastClicked
;
2975 m_lineLastClicked
= current
;
2977 size_t oldCurrent
= m_current
;
2979 if ( IsSingleSel() || !(event
.ControlDown() || event
.ShiftDown()) )
2981 HighlightAll( FALSE
);
2983 ChangeCurrent(current
);
2985 ReverseHighlight(m_current
);
2987 else // multi sel & either ctrl or shift is down
2989 if (event
.ControlDown())
2991 ChangeCurrent(current
);
2993 ReverseHighlight(m_current
);
2995 else if (event
.ShiftDown())
2997 ChangeCurrent(current
);
2999 size_t lineFrom
= oldCurrent
,
3002 if ( lineTo
< lineFrom
)
3005 lineFrom
= m_current
;
3008 HighlightLines(lineFrom
, lineTo
);
3010 else // !ctrl, !shift
3012 // test in the enclosing if should make it impossible
3013 wxFAIL_MSG( _T("how did we get here?") );
3017 if (m_current
!= oldCurrent
)
3019 RefreshLine( oldCurrent
);
3022 // forceClick is only set if the previous click was on another item
3023 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
3027 void wxListMainWindow::MoveToItem(size_t item
)
3029 if ( item
== (size_t)-1 )
3032 wxRect rect
= GetLineRect(item
);
3034 int client_w
, client_h
;
3035 GetClientSize( &client_w
, &client_h
);
3037 const int hLine
= GetLineHeight();
3039 int view_x
= SCROLL_UNIT_X
*GetScrollPos( wxHORIZONTAL
);
3040 int view_y
= hLine
*GetScrollPos( wxVERTICAL
);
3042 if ( InReportView() )
3044 // the next we need the range of lines shown it might be different, so
3046 ResetVisibleLinesRange();
3048 if (rect
.y
< view_y
)
3049 Scroll( -1, rect
.y
/hLine
);
3050 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
3051 Scroll( -1, (rect
.y
+rect
.height
-client_h
+hLine
)/hLine
);
3055 if (rect
.x
-view_x
< 5)
3056 Scroll( (rect
.x
-5)/SCROLL_UNIT_X
, -1 );
3057 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
3058 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/SCROLL_UNIT_X
, -1 );
3062 // ----------------------------------------------------------------------------
3063 // keyboard handling
3064 // ----------------------------------------------------------------------------
3066 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
3068 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
3069 _T("invalid item index in OnArrowChar()") );
3071 size_t oldCurrent
= m_current
;
3073 // in single selection we just ignore Shift as we can't select several
3075 if ( event
.ShiftDown() && !IsSingleSel() )
3077 ChangeCurrent(newCurrent
);
3079 // refresh the old focus to remove it
3080 RefreshLine( oldCurrent
);
3082 // select all the items between the old and the new one
3083 if ( oldCurrent
> newCurrent
)
3085 newCurrent
= oldCurrent
;
3086 oldCurrent
= m_current
;
3089 HighlightLines(oldCurrent
, newCurrent
);
3093 // all previously selected items are unselected unless ctrl is held
3094 if ( !event
.ControlDown() )
3095 HighlightAll(FALSE
);
3097 ChangeCurrent(newCurrent
);
3099 // refresh the old focus to remove it
3100 RefreshLine( oldCurrent
);
3102 if ( !event
.ControlDown() )
3104 HighlightLine( m_current
, TRUE
);
3108 RefreshLine( m_current
);
3113 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
3115 wxWindow
*parent
= GetParent();
3117 /* we propagate the key event up */
3118 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
3119 ke
.m_shiftDown
= event
.m_shiftDown
;
3120 ke
.m_controlDown
= event
.m_controlDown
;
3121 ke
.m_altDown
= event
.m_altDown
;
3122 ke
.m_metaDown
= event
.m_metaDown
;
3123 ke
.m_keyCode
= event
.m_keyCode
;
3126 ke
.SetEventObject( parent
);
3127 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3132 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
3134 wxWindow
*parent
= GetParent();
3136 /* we send a list_key event up */
3139 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
3140 le
.m_itemIndex
= m_current
;
3141 GetLine(m_current
)->GetItem( 0, le
.m_item
);
3142 le
.m_code
= event
.GetKeyCode();
3143 le
.SetEventObject( parent
);
3144 parent
->GetEventHandler()->ProcessEvent( le
);
3147 /* we propagate the char event up */
3148 wxKeyEvent
ke( wxEVT_CHAR
);
3149 ke
.m_shiftDown
= event
.m_shiftDown
;
3150 ke
.m_controlDown
= event
.m_controlDown
;
3151 ke
.m_altDown
= event
.m_altDown
;
3152 ke
.m_metaDown
= event
.m_metaDown
;
3153 ke
.m_keyCode
= event
.m_keyCode
;
3156 ke
.SetEventObject( parent
);
3157 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
3159 if (event
.GetKeyCode() == WXK_TAB
)
3161 wxNavigationKeyEvent nevent
;
3162 nevent
.SetWindowChange( event
.ControlDown() );
3163 nevent
.SetDirection( !event
.ShiftDown() );
3164 nevent
.SetEventObject( GetParent()->GetParent() );
3165 nevent
.SetCurrentFocus( m_parent
);
3166 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3170 /* no item -> nothing to do */
3177 switch (event
.GetKeyCode())
3180 if ( m_current
> 0 )
3181 OnArrowChar( m_current
- 1, event
);
3185 if ( m_current
< (size_t)GetItemCount() - 1 )
3186 OnArrowChar( m_current
+ 1, event
);
3191 OnArrowChar( GetItemCount() - 1, event
);
3196 OnArrowChar( 0, event
);
3201 int steps
= InReportView() ? m_linesPerPage
- 1 : m_current
% m_linesPerPage
;
3203 int index
= m_current
- steps
;
3207 OnArrowChar( index
, event
);
3213 int steps
= InReportView()
3214 ? m_linesPerPage
- 1
3215 : m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
3217 size_t index
= m_current
+ steps
;
3218 size_t count
= GetItemCount();
3219 if ( index
>= count
)
3222 OnArrowChar( index
, event
);
3227 if ( !InReportView() )
3229 int index
= m_current
- m_linesPerPage
;
3233 OnArrowChar( index
, event
);
3238 if ( !InReportView() )
3240 size_t index
= m_current
+ m_linesPerPage
;
3242 size_t count
= GetItemCount();
3243 if ( index
>= count
)
3246 OnArrowChar( index
, event
);
3251 if ( IsSingleSel() )
3253 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3255 if ( IsHighlighted(m_current
) )
3257 // don't unselect the item in single selection mode
3260 //else: select it in ReverseHighlight() below if unselected
3263 ReverseHighlight(m_current
);
3268 SendNotify( m_current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
3276 // ----------------------------------------------------------------------------
3278 // ----------------------------------------------------------------------------
3280 void wxListMainWindow::SetFocus()
3282 // VS: wxListMainWindow derives from wxPanel (via wxScrolledWindow) and wxPanel
3283 // overrides SetFocus in such way that it does never change focus from
3284 // panel's child to the panel itself. Unfortunately, we must be able to change
3285 // focus to the panel from wxListTextCtrl because the text control should
3286 // disappear when the user clicks outside it.
3288 wxWindow
*oldFocus
= FindFocus();
3290 if ( oldFocus
&& oldFocus
->GetParent() == this )
3292 wxWindow::SetFocus();
3296 wxScrolledWindow::SetFocus();
3300 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
3304 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
3305 event
.SetEventObject( GetParent() );
3306 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3310 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3311 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3312 // which are already drawn correctly resulting in horrible flicker - avoid
3322 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
3326 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetParent()->GetId() );
3327 event
.SetEventObject( GetParent() );
3328 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
3335 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
3337 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
3339 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3341 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
3343 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3345 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
3347 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3349 else if ( InReportView() && (m_small_image_list
))
3351 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
3355 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
) const
3357 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
3359 m_normal_image_list
->GetSize( index
, width
, height
);
3361 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
3363 m_small_image_list
->GetSize( index
, width
, height
);
3365 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
3367 m_small_image_list
->GetSize( index
, width
, height
);
3369 else if ( InReportView() && m_small_image_list
)
3371 m_small_image_list
->GetSize( index
, width
, height
);
3380 int wxListMainWindow::GetTextLength( const wxString
&s
) const
3382 wxClientDC
dc( wxConstCast(this, wxListMainWindow
) );
3383 dc
.SetFont( GetFont() );
3386 dc
.GetTextExtent( s
, &lw
, NULL
);
3388 return lw
+ AUTOSIZE_COL_MARGIN
;
3391 void wxListMainWindow::SetImageList( wxImageListType
*imageList
, int which
)
3395 // calc the spacing from the icon size
3398 if ((imageList
) && (imageList
->GetImageCount()) )
3400 imageList
->GetSize(0, width
, height
);
3403 if (which
== wxIMAGE_LIST_NORMAL
)
3405 m_normal_image_list
= imageList
;
3406 m_normal_spacing
= width
+ 8;
3409 if (which
== wxIMAGE_LIST_SMALL
)
3411 m_small_image_list
= imageList
;
3412 m_small_spacing
= width
+ 14;
3413 m_lineHeight
= 0; // ensure that the line height will be recalc'd
3417 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
3422 m_small_spacing
= spacing
;
3426 m_normal_spacing
= spacing
;
3430 int wxListMainWindow::GetItemSpacing( bool isSmall
)
3432 return isSmall
? m_small_spacing
: m_normal_spacing
;
3435 // ----------------------------------------------------------------------------
3437 // ----------------------------------------------------------------------------
3439 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
3441 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3443 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
3445 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3446 item
.m_width
= GetTextLength( item
.m_text
);
3448 wxListHeaderData
*column
= node
->GetData();
3449 column
->SetItem( item
);
3451 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3453 headerWin
->m_dirty
= TRUE
;
3457 // invalidate it as it has to be recalculated
3461 void wxListMainWindow::SetColumnWidth( int col
, int width
)
3463 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
3464 _T("invalid column index") );
3466 wxCHECK_RET( InReportView(),
3467 _T("SetColumnWidth() can only be called in report mode.") );
3470 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
3472 headerWin
->m_dirty
= TRUE
;
3474 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3475 wxCHECK_RET( node
, _T("no column?") );
3477 wxListHeaderData
*column
= node
->GetData();
3479 size_t count
= GetItemCount();
3481 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
3483 width
= GetTextLength(column
->GetText());
3485 else if ( width
== wxLIST_AUTOSIZE
)
3489 // TODO: determine the max width somehow...
3490 width
= WIDTH_COL_DEFAULT
;
3494 wxClientDC
dc(this);
3495 dc
.SetFont( GetFont() );
3497 int max
= AUTOSIZE_COL_MARGIN
;
3499 for ( size_t i
= 0; i
< count
; i
++ )
3501 wxListLineData
*line
= GetLine(i
);
3502 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
3504 wxCHECK_RET( n
, _T("no subitem?") );
3506 wxListItemData
*item
= n
->GetData();
3509 if (item
->HasImage())
3512 GetImageSize( item
->GetImage(), ix
, iy
);
3516 if (item
->HasText())
3519 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3527 width
= max
+ AUTOSIZE_COL_MARGIN
;
3531 column
->SetWidth( width
);
3533 // invalidate it as it has to be recalculated
3537 int wxListMainWindow::GetHeaderWidth() const
3539 if ( !m_headerWidth
)
3541 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3543 size_t count
= GetColumnCount();
3544 for ( size_t col
= 0; col
< count
; col
++ )
3546 self
->m_headerWidth
+= GetColumnWidth(col
);
3550 return m_headerWidth
;
3553 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3555 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3556 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3558 wxListHeaderData
*column
= node
->GetData();
3559 column
->GetItem( item
);
3562 int wxListMainWindow::GetColumnWidth( int col
) const
3564 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
3565 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3567 wxListHeaderData
*column
= node
->GetData();
3568 return column
->GetWidth();
3571 // ----------------------------------------------------------------------------
3573 // ----------------------------------------------------------------------------
3575 void wxListMainWindow::SetItem( wxListItem
&item
)
3577 long id
= item
.m_itemId
;
3578 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3579 _T("invalid item index in SetItem") );
3583 wxListLineData
*line
= GetLine((size_t)id
);
3584 line
->SetItem( item
.m_col
, item
);
3587 // update the item on screen
3589 GetItemRect(id
, rectItem
);
3590 RefreshRect(rectItem
);
3593 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3595 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3596 _T("invalid list ctrl item index in SetItem") );
3598 size_t oldCurrent
= m_current
;
3599 size_t item
= (size_t)litem
; // safe because of the check above
3601 // do we need to change the focus?
3602 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3604 if ( state
& wxLIST_STATE_FOCUSED
)
3606 // don't do anything if this item is already focused
3607 if ( item
!= m_current
)
3609 ChangeCurrent(item
);
3611 if ( oldCurrent
!= (size_t)-1 )
3613 if ( IsSingleSel() )
3615 HighlightLine(oldCurrent
, FALSE
);
3618 RefreshLine(oldCurrent
);
3621 RefreshLine( m_current
);
3626 // don't do anything if this item is not focused
3627 if ( item
== m_current
)
3631 if ( IsSingleSel() )
3633 // we must unselect the old current item as well or we
3634 // might end up with more than one selected item in a
3635 // single selection control
3636 HighlightLine(oldCurrent
, FALSE
);
3639 RefreshLine( oldCurrent
);
3644 // do we need to change the selection state?
3645 if ( stateMask
& wxLIST_STATE_SELECTED
)
3647 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3649 if ( IsSingleSel() )
3653 // selecting the item also makes it the focused one in the
3655 if ( m_current
!= item
)
3657 ChangeCurrent(item
);
3659 if ( oldCurrent
!= (size_t)-1 )
3661 HighlightLine( oldCurrent
, FALSE
);
3662 RefreshLine( oldCurrent
);
3668 // only the current item may be selected anyhow
3669 if ( item
!= m_current
)
3674 if ( HighlightLine(item
, on
) )
3681 int wxListMainWindow::GetItemState( long item
, long stateMask
) const
3683 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3684 _T("invalid list ctrl item index in GetItemState()") );
3686 int ret
= wxLIST_STATE_DONTCARE
;
3688 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3690 if ( (size_t)item
== m_current
)
3691 ret
|= wxLIST_STATE_FOCUSED
;
3694 if ( stateMask
& wxLIST_STATE_SELECTED
)
3696 if ( IsHighlighted(item
) )
3697 ret
|= wxLIST_STATE_SELECTED
;
3703 void wxListMainWindow::GetItem( wxListItem
&item
) const
3705 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3706 _T("invalid item index in GetItem") );
3708 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3709 line
->GetItem( item
.m_col
, item
);
3712 // ----------------------------------------------------------------------------
3714 // ----------------------------------------------------------------------------
3716 size_t wxListMainWindow::GetItemCount() const
3718 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3721 void wxListMainWindow::SetItemCount(long count
)
3723 m_selStore
.SetItemCount(count
);
3724 m_countVirt
= count
;
3726 ResetVisibleLinesRange();
3728 // scrollbars must be reset
3732 int wxListMainWindow::GetSelectedItemCount() const
3734 // deal with the quick case first
3735 if ( IsSingleSel() )
3737 return HasCurrent() ? IsHighlighted(m_current
) : FALSE
;
3740 // virtual controls remmebers all its selections itself
3742 return m_selStore
.GetSelectedCount();
3744 // TODO: we probably should maintain the number of items selected even for
3745 // non virtual controls as enumerating all lines is really slow...
3746 size_t countSel
= 0;
3747 size_t count
= GetItemCount();
3748 for ( size_t line
= 0; line
< count
; line
++ )
3750 if ( GetLine(line
)->IsHighlighted() )
3757 // ----------------------------------------------------------------------------
3758 // item position/size
3759 // ----------------------------------------------------------------------------
3761 wxRect
wxListMainWindow::GetViewRect() const
3763 wxASSERT_MSG( !HasFlag(wxLC_REPORT
| wxLC_LIST
),
3764 _T("wxListCtrl::GetViewRect() only works in icon mode") );
3766 // we need to find the longest/tallest label
3769 const int count
= GetItemCount();
3772 for ( int i
= 0; i
< count
; i
++ )
3777 wxCoord x
= r
.GetRight(),
3787 // some fudge needed to make it look prettier
3788 xMax
+= 2*EXTRA_BORDER_X
;
3789 yMax
+= 2*EXTRA_BORDER_Y
;
3791 // account for the scrollbars if necessary
3792 const wxSize sizeAll
= GetClientSize();
3793 if ( xMax
> sizeAll
.x
)
3794 yMax
+= wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y
);
3795 if ( yMax
> sizeAll
.y
)
3796 xMax
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
3798 return wxRect(0, 0, xMax
, yMax
);
3801 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
) const
3803 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3804 _T("invalid index in GetItemRect") );
3806 // ensure that we're laid out, otherwise we could return nonsense
3809 wxConstCast(this, wxListMainWindow
)->
3810 RecalculatePositions(TRUE
/* no refresh */);
3813 rect
= GetLineRect((size_t)index
);
3815 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3818 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
) const
3821 GetItemRect(item
, rect
);
3829 // ----------------------------------------------------------------------------
3830 // geometry calculation
3831 // ----------------------------------------------------------------------------
3833 void wxListMainWindow::RecalculatePositions(bool noRefresh
)
3835 wxClientDC
dc( this );
3836 dc
.SetFont( GetFont() );
3838 const size_t count
= GetItemCount();
3841 if ( HasFlag(wxLC_ICON
) )
3842 iconSpacing
= m_normal_spacing
;
3843 else if ( HasFlag(wxLC_SMALL_ICON
) )
3844 iconSpacing
= m_small_spacing
;
3848 // Note that we do not call GetClientSize() here but
3849 // GetSize() and substract the border size for sunken
3850 // borders manually. This is technically incorrect,
3851 // but we need to know the client area's size WITHOUT
3852 // scrollbars here. Since we don't know if there are
3853 // any scrollbars, we use GetSize() instead. Another
3854 // solution would be to call SetScrollbars() here to
3855 // remove the scrollbars and call GetClientSize() then,
3856 // but this might result in flicker and - worse - will
3857 // reset the scrollbars to 0 which is not good at all
3858 // if you resize a dialog/window, but don't want to
3859 // reset the window scrolling. RR.
3860 // Furthermore, we actually do NOT subtract the border
3861 // width as 2 pixels is just the extra space which we
3862 // need around the actual content in the window. Other-
3863 // wise the text would e.g. touch the upper border. RR.
3866 GetSize( &clientWidth
, &clientHeight
);
3868 const int lineHeight
= GetLineHeight();
3870 if ( InReportView() )
3872 // all lines have the same height and we scroll one line per step
3873 int entireHeight
= count
*lineHeight
+ LINE_SPACING
;
3875 m_linesPerPage
= clientHeight
/ lineHeight
;
3877 ResetVisibleLinesRange();
3879 SetScrollbars( SCROLL_UNIT_X
, lineHeight
,
3880 GetHeaderWidth() / SCROLL_UNIT_X
,
3881 (entireHeight
+ lineHeight
- 1) / lineHeight
,
3882 GetScrollPos(wxHORIZONTAL
),
3883 GetScrollPos(wxVERTICAL
),
3888 // we have 3 different layout strategies: either layout all items
3889 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3890 // to arrange them in top to bottom, left to right (don't ask me why
3891 // not the other way round...) order
3892 if ( HasFlag(wxLC_ALIGN_LEFT
| wxLC_ALIGN_TOP
) )
3894 int x
= EXTRA_BORDER_X
;
3895 int y
= EXTRA_BORDER_Y
;
3897 wxCoord widthMax
= 0;
3900 for ( i
= 0; i
< count
; i
++ )
3902 wxListLineData
*line
= GetLine(i
);
3903 line
->CalculateSize( &dc
, iconSpacing
);
3904 line
->SetPosition( x
, y
, iconSpacing
);
3906 wxSize sizeLine
= GetLineSize(i
);
3908 if ( HasFlag(wxLC_ALIGN_TOP
) )
3910 if ( sizeLine
.x
> widthMax
)
3911 widthMax
= sizeLine
.x
;
3915 else // wxLC_ALIGN_LEFT
3917 x
+= sizeLine
.x
+ MARGIN_BETWEEN_ROWS
;
3921 if ( HasFlag(wxLC_ALIGN_TOP
) )
3923 // traverse the items again and tweak their sizes so that they are
3924 // all the same in a row
3925 for ( i
= 0; i
< count
; i
++ )
3927 wxListLineData
*line
= GetLine(i
);
3928 line
->m_gi
->ExtendWidth(widthMax
);
3937 (x
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
3938 (y
+ lineHeight
) / lineHeight
,
3939 GetScrollPos( wxHORIZONTAL
),
3940 GetScrollPos( wxVERTICAL
),
3944 else // "flowed" arrangement, the most complicated case
3946 // at first we try without any scrollbars, if the items don't fit into
3947 // the window, we recalculate after subtracting the space taken by the
3950 int entireWidth
= 0;
3952 for (int tries
= 0; tries
< 2; tries
++)
3954 entireWidth
= 2*EXTRA_BORDER_X
;
3958 // Now we have decided that the items do not fit into the
3959 // client area, so we need a scrollbar
3960 entireWidth
+= SCROLL_UNIT_X
;
3963 int x
= EXTRA_BORDER_X
;
3964 int y
= EXTRA_BORDER_Y
;
3965 int maxWidthInThisRow
= 0;
3968 int currentlyVisibleLines
= 0;
3970 for (size_t i
= 0; i
< count
; i
++)
3972 currentlyVisibleLines
++;
3973 wxListLineData
*line
= GetLine(i
);
3974 line
->CalculateSize( &dc
, iconSpacing
);
3975 line
->SetPosition( x
, y
, iconSpacing
);
3977 wxSize sizeLine
= GetLineSize(i
);
3979 if ( maxWidthInThisRow
< sizeLine
.x
)
3980 maxWidthInThisRow
= sizeLine
.x
;
3983 if (currentlyVisibleLines
> m_linesPerPage
)
3984 m_linesPerPage
= currentlyVisibleLines
;
3986 if ( y
+ sizeLine
.y
>= clientHeight
)
3988 currentlyVisibleLines
= 0;
3990 maxWidthInThisRow
+= MARGIN_BETWEEN_ROWS
;
3991 x
+= maxWidthInThisRow
;
3992 entireWidth
+= maxWidthInThisRow
;
3993 maxWidthInThisRow
= 0;
3996 // We have reached the last item.
3997 if ( i
== count
- 1 )
3998 entireWidth
+= maxWidthInThisRow
;
4000 if ( (tries
== 0) &&
4001 (entireWidth
+ SCROLL_UNIT_X
> clientWidth
) )
4003 clientHeight
-= wxSystemSettings::
4004 GetMetric(wxSYS_HSCROLL_Y
);
4009 if ( i
== count
- 1 )
4010 tries
= 1; // Everything fits, no second try required.
4018 (entireWidth
+ SCROLL_UNIT_X
) / SCROLL_UNIT_X
,
4020 GetScrollPos( wxHORIZONTAL
),
4029 // FIXME: why should we call it from here?
4036 void wxListMainWindow::RefreshAll()
4041 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
4042 if ( headerWin
&& headerWin
->m_dirty
)
4044 headerWin
->m_dirty
= FALSE
;
4045 headerWin
->Refresh();
4049 void wxListMainWindow::UpdateCurrent()
4051 if ( !HasCurrent() && !IsEmpty() )
4057 long wxListMainWindow::GetNextItem( long item
,
4058 int WXUNUSED(geometry
),
4062 max
= GetItemCount();
4063 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
4064 _T("invalid listctrl index in GetNextItem()") );
4066 // notice that we start with the next item (or the first one if item == -1)
4067 // and this is intentional to allow writing a simple loop to iterate over
4068 // all selected items
4072 // this is not an error because the index was ok initially, just no
4083 size_t count
= GetItemCount();
4084 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
4086 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
4089 if ( (state
& wxLIST_STATE_SELECTED
) && IsHighlighted(line
) )
4096 // ----------------------------------------------------------------------------
4098 // ----------------------------------------------------------------------------
4100 void wxListMainWindow::DeleteItem( long lindex
)
4102 size_t count
= GetItemCount();
4104 wxCHECK_RET( (lindex
>= 0) && ((size_t)lindex
< count
),
4105 _T("invalid item index in DeleteItem") );
4107 size_t index
= (size_t)lindex
;
4109 // we don't need to adjust the index for the previous items
4110 if ( HasCurrent() && m_current
>= index
)
4112 // if the current item is being deleted, we want the next one to
4113 // become selected - unless there is no next one - so don't adjust
4114 // m_current in this case
4115 if ( m_current
!= index
|| m_current
== count
- 1 )
4121 if ( InReportView() )
4123 ResetVisibleLinesRange();
4130 m_selStore
.OnItemDelete(index
);
4134 m_lines
.RemoveAt( index
);
4137 // we need to refresh the (vert) scrollbar as the number of items changed
4140 SendNotify( index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
4142 RefreshAfter(index
);
4145 void wxListMainWindow::DeleteColumn( int col
)
4147 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4149 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
4152 delete node
->GetData();
4153 m_columns
.Erase( node
);
4157 // update all the items
4158 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4160 wxListLineData
* const line
= GetLine(i
);
4161 wxListItemDataList::compatibility_iterator n
= line
->m_items
.Item( col
);
4162 delete n
->GetData();
4163 line
->m_items
.Erase(n
);
4167 // invalidate it as it has to be recalculated
4171 void wxListMainWindow::DoDeleteAllItems()
4175 // nothing to do - in particular, don't send the event
4181 // to make the deletion of all items faster, we don't send the
4182 // notifications for each item deletion in this case but only one event
4183 // for all of them: this is compatible with wxMSW and documented in
4184 // DeleteAllItems() description
4186 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
4187 event
.SetEventObject( GetParent() );
4188 GetParent()->GetEventHandler()->ProcessEvent( event
);
4197 if ( InReportView() )
4199 ResetVisibleLinesRange();
4205 void wxListMainWindow::DeleteAllItems()
4209 RecalculatePositions();
4212 void wxListMainWindow::DeleteEverything()
4214 WX_CLEAR_LIST(wxListHeaderDataList
, m_columns
);
4219 // ----------------------------------------------------------------------------
4220 // scanning for an item
4221 // ----------------------------------------------------------------------------
4223 void wxListMainWindow::EnsureVisible( long index
)
4225 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
4226 _T("invalid index in EnsureVisible") );
4228 // We have to call this here because the label in question might just have
4229 // been added and its position is not known yet
4232 RecalculatePositions(TRUE
/* no refresh */);
4235 MoveToItem((size_t)index
);
4238 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
4245 size_t count
= GetItemCount();
4246 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
4248 wxListLineData
*line
= GetLine(i
);
4249 if ( line
->GetText(0) == tmp
)
4256 long wxListMainWindow::FindItem(long start
, long data
)
4262 size_t count
= GetItemCount();
4263 for (size_t i
= (size_t)pos
; i
< count
; i
++)
4265 wxListLineData
*line
= GetLine(i
);
4267 line
->GetItem( 0, item
);
4268 if (item
.m_data
== data
)
4275 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
4277 CalcUnscrolledPosition( x
, y
, &x
, &y
);
4279 size_t count
= GetItemCount();
4281 if ( InReportView() )
4283 size_t current
= y
/ GetLineHeight();
4284 if ( current
< count
)
4286 flags
= HitTestLine(current
, x
, y
);
4293 // TODO: optimize it too! this is less simple than for report view but
4294 // enumerating all items is still not a way to do it!!
4295 for ( size_t current
= 0; current
< count
; current
++ )
4297 flags
= HitTestLine(current
, x
, y
);
4306 // ----------------------------------------------------------------------------
4308 // ----------------------------------------------------------------------------
4310 void wxListMainWindow::InsertItem( wxListItem
&item
)
4312 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4314 size_t count
= GetItemCount();
4315 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
4316 _T("invalid item index") );
4318 size_t id
= item
.m_itemId
;
4323 // this is unused variable
4326 if ( InReportView() )
4329 // this is unused variable
4332 ResetVisibleLinesRange();
4334 else if ( HasFlag(wxLC_LIST
) )
4336 // this is unused variable
4341 else if ( HasFlag(wxLC_ICON
) )
4343 // this is unused variable
4348 else if ( HasFlag(wxLC_SMALL_ICON
) )
4350 // this is unused variable
4351 mode
= wxLC_ICON
; // no typo
4357 wxFAIL_MSG( _T("unknown mode") );
4360 wxListLineData
*line
= new wxListLineData(this);
4362 line
->SetItem( 0, item
);
4364 m_lines
.Insert( line
, id
);
4368 // If an item is selected at or below the point of insertion, we need to
4369 // increment the member variables because the current row's index has gone
4371 if ( HasCurrent() && m_current
>= id
)
4376 SendNotify(id
, wxEVT_COMMAND_LIST_INSERT_ITEM
);
4378 RefreshLines(id
, GetItemCount() - 1);
4381 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
4384 if ( InReportView() )
4386 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
4387 item
.m_width
= GetTextLength( item
.m_text
);
4389 wxListHeaderData
*column
= new wxListHeaderData( item
);
4390 bool insert
= (col
>= 0) && ((size_t)col
< m_columns
.GetCount());
4393 wxListHeaderDataList::compatibility_iterator node
= m_columns
.Item( col
);
4394 m_columns
.Insert( node
, column
);
4398 m_columns
.Append( column
);
4403 // update all the items
4404 for ( size_t i
= 0; i
< m_lines
.GetCount(); i
++ )
4406 wxListLineData
* const line
= GetLine(i
);
4407 wxListItemData
* const data
= new wxListItemData(this);
4409 line
->m_items
.Insert(col
, data
);
4411 line
->m_items
.Append(data
);
4415 // invalidate it as it has to be recalculated
4420 // ----------------------------------------------------------------------------
4422 // ----------------------------------------------------------------------------
4424 wxListCtrlCompare list_ctrl_compare_func_2
;
4425 long list_ctrl_compare_data
;
4427 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
4429 wxListLineData
*line1
= *arg1
;
4430 wxListLineData
*line2
= *arg2
;
4432 line1
->GetItem( 0, item
);
4433 long data1
= item
.m_data
;
4434 line2
->GetItem( 0, item
);
4435 long data2
= item
.m_data
;
4436 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
4439 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
4441 list_ctrl_compare_func_2
= fn
;
4442 list_ctrl_compare_data
= data
;
4443 m_lines
.Sort( list_ctrl_compare_func_1
);
4447 // ----------------------------------------------------------------------------
4449 // ----------------------------------------------------------------------------
4451 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
4453 // update our idea of which lines are shown when we redraw the window the
4455 ResetVisibleLinesRange();
4458 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
4459 wxScrolledWindow::OnScroll(event
);
4461 HandleOnScroll( event
);
4464 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
4466 wxGenericListCtrl
* lc
= GetListCtrl();
4467 wxCHECK_RET( lc
, _T("no listctrl window?") );
4469 lc
->m_headerWin
->Refresh();
4470 lc
->m_headerWin
->Update();
4474 int wxListMainWindow::GetCountPerPage() const
4476 if ( !m_linesPerPage
)
4478 wxConstCast(this, wxListMainWindow
)->
4479 m_linesPerPage
= GetClientSize().y
/ GetLineHeight();
4482 return m_linesPerPage
;
4485 void wxListMainWindow::GetVisibleLinesRange(size_t *from
, size_t *to
)
4487 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4489 if ( m_lineFrom
== (size_t)-1 )
4491 size_t count
= GetItemCount();
4494 m_lineFrom
= GetScrollPos(wxVERTICAL
);
4496 // this may happen if SetScrollbars() hadn't been called yet
4497 if ( m_lineFrom
>= count
)
4498 m_lineFrom
= count
- 1;
4500 // we redraw one extra line but this is needed to make the redrawing
4501 // logic work when there is a fractional number of lines on screen
4502 m_lineTo
= m_lineFrom
+ m_linesPerPage
;
4503 if ( m_lineTo
>= count
)
4504 m_lineTo
= count
- 1;
4506 else // empty control
4509 m_lineTo
= (size_t)-1;
4513 wxASSERT_MSG( IsEmpty() ||
4514 (m_lineFrom
<= m_lineTo
&& m_lineTo
< GetItemCount()),
4515 _T("GetVisibleLinesRange() returns incorrect result") );
4523 // -------------------------------------------------------------------------------------
4524 // wxGenericListCtrl
4525 // -------------------------------------------------------------------------------------
4527 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl
, wxControl
)
4529 BEGIN_EVENT_TABLE(wxGenericListCtrl
,wxControl
)
4530 EVT_SIZE(wxGenericListCtrl::OnSize
)
4533 wxGenericListCtrl::wxGenericListCtrl()
4535 m_imageListNormal
= (wxImageListType
*) NULL
;
4536 m_imageListSmall
= (wxImageListType
*) NULL
;
4537 m_imageListState
= (wxImageListType
*) NULL
;
4539 m_ownsImageListNormal
=
4540 m_ownsImageListSmall
=
4541 m_ownsImageListState
= FALSE
;
4543 m_mainWin
= (wxListMainWindow
*) NULL
;
4544 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4548 wxGenericListCtrl::~wxGenericListCtrl()
4550 if (m_ownsImageListNormal
)
4551 delete m_imageListNormal
;
4552 if (m_ownsImageListSmall
)
4553 delete m_imageListSmall
;
4554 if (m_ownsImageListState
)
4555 delete m_imageListState
;
4558 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4562 // we use 'g' to get the descent, too
4564 m_headerWin
->GetTextExtent(wxT("Hg"), &w
, &h
, &d
);
4565 h
+= d
+ 2 * HEADER_OFFSET_Y
+ EXTRA_HEIGHT
;
4567 // only update if changed
4568 if ( h
!= m_headerHeight
)
4572 m_headerWin
->SetSize(m_headerWin
->GetSize().x
, m_headerHeight
);
4575 ResizeReportView(TRUE
);
4580 void wxGenericListCtrl::CreateHeaderWindow()
4582 m_headerWin
= new wxListHeaderWindow
4584 this, -1, m_mainWin
,
4586 wxSize(GetClientSize().x
, m_headerHeight
),
4589 CalculateAndSetHeaderHeight();
4592 bool wxGenericListCtrl::Create(wxWindow
*parent
,
4597 const wxValidator
&validator
,
4598 const wxString
&name
)
4602 m_imageListState
= (wxImageListType
*) NULL
;
4603 m_ownsImageListNormal
=
4604 m_ownsImageListSmall
=
4605 m_ownsImageListState
= FALSE
;
4607 m_mainWin
= (wxListMainWindow
*) NULL
;
4608 m_headerWin
= (wxListHeaderWindow
*) NULL
;
4612 if ( !(style
& wxLC_MASK_TYPE
) )
4614 style
= style
| wxLC_LIST
;
4617 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
4620 // don't create the inner window with the border
4621 style
&= ~wxBORDER_MASK
;
4623 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
4625 #if defined( __WXMAC__ ) && __WXMAC_CARBON__
4627 font
.MacCreateThemeFont( kThemeViewsFont
) ;
4630 if ( InReportView() )
4632 CreateHeaderWindow();
4634 if ( HasFlag(wxLC_NO_HEADER
) )
4636 // VZ: why do we create it at all then?
4637 m_headerWin
->Show( FALSE
);
4646 void wxGenericListCtrl::SetSingleStyle( long style
, bool add
)
4648 wxASSERT_MSG( !(style
& wxLC_VIRTUAL
),
4649 _T("wxLC_VIRTUAL can't be [un]set") );
4651 long flag
= GetWindowStyle();
4655 if (style
& wxLC_MASK_TYPE
)
4656 flag
&= ~(wxLC_MASK_TYPE
| wxLC_VIRTUAL
);
4657 if (style
& wxLC_MASK_ALIGN
)
4658 flag
&= ~wxLC_MASK_ALIGN
;
4659 if (style
& wxLC_MASK_SORT
)
4660 flag
&= ~wxLC_MASK_SORT
;
4672 SetWindowStyleFlag( flag
);
4675 void wxGenericListCtrl::SetWindowStyleFlag( long flag
)
4679 m_mainWin
->DeleteEverything();
4681 // has the header visibility changed?
4682 bool hasHeader
= HasHeader();
4683 bool willHaveHeader
= (flag
& wxLC_REPORT
) && !(flag
& wxLC_NO_HEADER
);
4685 if ( hasHeader
!= willHaveHeader
)
4692 // don't delete, just hide, as we can reuse it later
4693 m_headerWin
->Show(FALSE
);
4695 //else: nothing to do
4697 else // must show header
4701 CreateHeaderWindow();
4703 else // already have it, just show
4705 m_headerWin
->Show( TRUE
);
4709 ResizeReportView(willHaveHeader
);
4713 wxWindow::SetWindowStyleFlag( flag
);
4716 bool wxGenericListCtrl::GetColumn(int col
, wxListItem
&item
) const
4718 m_mainWin
->GetColumn( col
, item
);
4722 bool wxGenericListCtrl::SetColumn( int col
, wxListItem
& item
)
4724 m_mainWin
->SetColumn( col
, item
);
4728 int wxGenericListCtrl::GetColumnWidth( int col
) const
4730 return m_mainWin
->GetColumnWidth( col
);
4733 bool wxGenericListCtrl::SetColumnWidth( int col
, int width
)
4735 m_mainWin
->SetColumnWidth( col
, width
);
4739 int wxGenericListCtrl::GetCountPerPage() const
4741 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4744 bool wxGenericListCtrl::GetItem( wxListItem
&info
) const
4746 m_mainWin
->GetItem( info
);
4750 bool wxGenericListCtrl::SetItem( wxListItem
&info
)
4752 m_mainWin
->SetItem( info
);
4756 long wxGenericListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4759 info
.m_text
= label
;
4760 info
.m_mask
= wxLIST_MASK_TEXT
;
4761 info
.m_itemId
= index
;
4765 info
.m_image
= imageId
;
4766 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4768 m_mainWin
->SetItem(info
);
4772 int wxGenericListCtrl::GetItemState( long item
, long stateMask
) const
4774 return m_mainWin
->GetItemState( item
, stateMask
);
4777 bool wxGenericListCtrl::SetItemState( long item
, long state
, long stateMask
)
4779 m_mainWin
->SetItemState( item
, state
, stateMask
);
4783 bool wxGenericListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4786 info
.m_image
= image
;
4787 info
.m_mask
= wxLIST_MASK_IMAGE
;
4788 info
.m_itemId
= item
;
4789 m_mainWin
->SetItem( info
);
4793 wxString
wxGenericListCtrl::GetItemText( long item
) const
4795 return m_mainWin
->GetItemText(item
);
4798 void wxGenericListCtrl::SetItemText( long item
, const wxString
& str
)
4800 m_mainWin
->SetItemText(item
, str
);
4803 long wxGenericListCtrl::GetItemData( long item
) const
4806 info
.m_itemId
= item
;
4807 m_mainWin
->GetItem( info
);
4811 bool wxGenericListCtrl::SetItemData( long item
, long data
)
4814 info
.m_mask
= wxLIST_MASK_DATA
;
4815 info
.m_itemId
= item
;
4817 m_mainWin
->SetItem( info
);
4821 wxRect
wxGenericListCtrl::GetViewRect() const
4823 return m_mainWin
->GetViewRect();
4826 bool wxGenericListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4828 m_mainWin
->GetItemRect( item
, rect
);
4829 if ( m_mainWin
->HasHeader() )
4830 rect
.y
+= m_headerHeight
+ 1;
4834 bool wxGenericListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4836 m_mainWin
->GetItemPosition( item
, pos
);
4840 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4845 int wxGenericListCtrl::GetItemCount() const
4847 return m_mainWin
->GetItemCount();
4850 int wxGenericListCtrl::GetColumnCount() const
4852 return m_mainWin
->GetColumnCount();
4855 void wxGenericListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4857 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4860 wxSize
wxGenericListCtrl::GetItemSpacing() const
4862 const int spacing
= m_mainWin
->GetItemSpacing(HasFlag(wxLC_SMALL_ICON
));
4864 return wxSize(spacing
, spacing
);
4867 int wxGenericListCtrl::GetItemSpacing( bool isSmall
) const
4869 return m_mainWin
->GetItemSpacing( isSmall
);
4872 void wxGenericListCtrl::SetItemTextColour( long item
, const wxColour
&col
)
4875 info
.m_itemId
= item
;
4876 info
.SetTextColour( col
);
4877 m_mainWin
->SetItem( info
);
4880 wxColour
wxGenericListCtrl::GetItemTextColour( long item
) const
4883 info
.m_itemId
= item
;
4884 m_mainWin
->GetItem( info
);
4885 return info
.GetTextColour();
4888 void wxGenericListCtrl::SetItemBackgroundColour( long item
, const wxColour
&col
)
4891 info
.m_itemId
= item
;
4892 info
.SetBackgroundColour( col
);
4893 m_mainWin
->SetItem( info
);
4896 wxColour
wxGenericListCtrl::GetItemBackgroundColour( long item
) const
4899 info
.m_itemId
= item
;
4900 m_mainWin
->GetItem( info
);
4901 return info
.GetBackgroundColour();
4904 int wxGenericListCtrl::GetSelectedItemCount() const
4906 return m_mainWin
->GetSelectedItemCount();
4909 wxColour
wxGenericListCtrl::GetTextColour() const
4911 return GetForegroundColour();
4914 void wxGenericListCtrl::SetTextColour(const wxColour
& col
)
4916 SetForegroundColour(col
);
4919 long wxGenericListCtrl::GetTopItem() const
4922 m_mainWin
->GetVisibleLinesRange(&top
, NULL
);
4926 long wxGenericListCtrl::GetNextItem( long item
, int geom
, int state
) const
4928 return m_mainWin
->GetNextItem( item
, geom
, state
);
4931 wxImageListType
*wxGenericListCtrl::GetImageList(int which
) const
4933 if (which
== wxIMAGE_LIST_NORMAL
)
4935 return m_imageListNormal
;
4937 else if (which
== wxIMAGE_LIST_SMALL
)
4939 return m_imageListSmall
;
4941 else if (which
== wxIMAGE_LIST_STATE
)
4943 return m_imageListState
;
4945 return (wxImageListType
*) NULL
;
4948 void wxGenericListCtrl::SetImageList( wxImageListType
*imageList
, int which
)
4950 if ( which
== wxIMAGE_LIST_NORMAL
)
4952 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4953 m_imageListNormal
= imageList
;
4954 m_ownsImageListNormal
= FALSE
;
4956 else if ( which
== wxIMAGE_LIST_SMALL
)
4958 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4959 m_imageListSmall
= imageList
;
4960 m_ownsImageListSmall
= FALSE
;
4962 else if ( which
== wxIMAGE_LIST_STATE
)
4964 if (m_ownsImageListState
) delete m_imageListState
;
4965 m_imageListState
= imageList
;
4966 m_ownsImageListState
= FALSE
;
4969 m_mainWin
->SetImageList( imageList
, which
);
4972 void wxGenericListCtrl::AssignImageList(wxImageListType
*imageList
, int which
)
4974 SetImageList(imageList
, which
);
4975 if ( which
== wxIMAGE_LIST_NORMAL
)
4976 m_ownsImageListNormal
= TRUE
;
4977 else if ( which
== wxIMAGE_LIST_SMALL
)
4978 m_ownsImageListSmall
= TRUE
;
4979 else if ( which
== wxIMAGE_LIST_STATE
)
4980 m_ownsImageListState
= TRUE
;
4983 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag
) )
4988 bool wxGenericListCtrl::DeleteItem( long item
)
4990 m_mainWin
->DeleteItem( item
);
4994 bool wxGenericListCtrl::DeleteAllItems()
4996 m_mainWin
->DeleteAllItems();
5000 bool wxGenericListCtrl::DeleteAllColumns()
5002 size_t count
= m_mainWin
->m_columns
.GetCount();
5003 for ( size_t n
= 0; n
< count
; n
++ )
5009 void wxGenericListCtrl::ClearAll()
5011 m_mainWin
->DeleteEverything();
5014 bool wxGenericListCtrl::DeleteColumn( int col
)
5016 m_mainWin
->DeleteColumn( col
);
5018 // if we don't have the header any longer, we need to relayout the window
5019 if ( !GetColumnCount() )
5021 ResizeReportView(FALSE
/* no header */);
5027 void wxGenericListCtrl::Edit( long item
)
5029 m_mainWin
->EditLabel( item
);
5032 bool wxGenericListCtrl::EnsureVisible( long item
)
5034 m_mainWin
->EnsureVisible( item
);
5038 long wxGenericListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
5040 return m_mainWin
->FindItem( start
, str
, partial
);
5043 long wxGenericListCtrl::FindItem( long start
, long data
)
5045 return m_mainWin
->FindItem( start
, data
);
5048 long wxGenericListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
5049 int WXUNUSED(direction
))
5054 long wxGenericListCtrl::HitTest( const wxPoint
&point
, int &flags
)
5056 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
5059 long wxGenericListCtrl::InsertItem( wxListItem
& info
)
5061 m_mainWin
->InsertItem( info
);
5062 return info
.m_itemId
;
5065 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
)
5068 info
.m_text
= label
;
5069 info
.m_mask
= wxLIST_MASK_TEXT
;
5070 info
.m_itemId
= index
;
5071 return InsertItem( info
);
5074 long wxGenericListCtrl::InsertItem( long index
, int imageIndex
)
5077 info
.m_mask
= wxLIST_MASK_IMAGE
;
5078 info
.m_image
= imageIndex
;
5079 info
.m_itemId
= index
;
5080 return InsertItem( info
);
5083 long wxGenericListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
5086 info
.m_text
= label
;
5087 info
.m_image
= imageIndex
;
5088 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
5089 info
.m_itemId
= index
;
5090 return InsertItem( info
);
5093 long wxGenericListCtrl::InsertColumn( long col
, wxListItem
&item
)
5095 wxCHECK_MSG( m_headerWin
, -1, _T("can't add column in non report mode") );
5097 m_mainWin
->InsertColumn( col
, item
);
5099 // if we hadn't had header before and have it now we need to relayout the
5101 if ( GetColumnCount() == 1 && m_mainWin
->HasHeader() )
5103 ResizeReportView(TRUE
/* have header */);
5106 m_headerWin
->Refresh();
5111 long wxGenericListCtrl::InsertColumn( long col
, const wxString
&heading
,
5112 int format
, int width
)
5115 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
5116 item
.m_text
= heading
;
5119 item
.m_mask
|= wxLIST_MASK_WIDTH
;
5120 item
.m_width
= width
;
5122 item
.m_format
= format
;
5124 return InsertColumn( col
, item
);
5127 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
5133 // fn is a function which takes 3 long arguments: item1, item2, data.
5134 // item1 is the long data associated with a first item (NOT the index).
5135 // item2 is the long data associated with a second item (NOT the index).
5136 // data is the same value as passed to SortItems.
5137 // The return value is a negative number if the first item should precede the second
5138 // item, a positive number of the second item should precede the first,
5139 // or zero if the two items are equivalent.
5140 // data is arbitrary data to be passed to the sort function.
5142 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
5144 m_mainWin
->SortItems( fn
, data
);
5148 // ----------------------------------------------------------------------------
5150 // ----------------------------------------------------------------------------
5152 void wxGenericListCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
5157 ResizeReportView(m_mainWin
->HasHeader());
5159 m_mainWin
->RecalculatePositions();
5162 void wxGenericListCtrl::ResizeReportView(bool showHeader
)
5165 GetClientSize( &cw
, &ch
);
5169 m_headerWin
->SetSize( 0, 0, cw
, m_headerHeight
);
5170 m_mainWin
->SetSize( 0, m_headerHeight
+ 1, cw
, ch
- m_headerHeight
- 1 );
5172 else // no header window
5174 m_mainWin
->SetSize( 0, 0, cw
, ch
);
5178 void wxGenericListCtrl::OnInternalIdle()
5180 wxWindow::OnInternalIdle();
5182 // do it only if needed
5183 if ( !m_mainWin
->m_dirty
)
5186 m_mainWin
->RecalculatePositions();
5189 // ----------------------------------------------------------------------------
5191 // ----------------------------------------------------------------------------
5193 bool wxGenericListCtrl::SetBackgroundColour( const wxColour
&colour
)
5197 m_mainWin
->SetBackgroundColour( colour
);
5198 m_mainWin
->m_dirty
= TRUE
;
5204 bool wxGenericListCtrl::SetForegroundColour( const wxColour
&colour
)
5206 if ( !wxWindow::SetForegroundColour( colour
) )
5211 m_mainWin
->SetForegroundColour( colour
);
5212 m_mainWin
->m_dirty
= TRUE
;
5217 m_headerWin
->SetForegroundColour( colour
);
5223 bool wxGenericListCtrl::SetFont( const wxFont
&font
)
5225 if ( !wxWindow::SetFont( font
) )
5230 m_mainWin
->SetFont( font
);
5231 m_mainWin
->m_dirty
= TRUE
;
5236 m_headerWin
->SetFont( font
);
5237 CalculateAndSetHeaderHeight();
5246 // NOTE: If using the wxListBox visual attributes works everywhere then this can
5247 // be removed, as well as the #else case below.
5248 #define _USE_VISATTR 0
5250 #include "wx/listbox.h"
5254 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
5257 // Use the same color scheme as wxListBox
5258 return wxListBox::GetClassDefaultAttributes(variant
);
5260 wxVisualAttributes attr
;
5261 attr
.colFg
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
5262 attr
.colBg
= wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX
);
5263 attr
.font
= wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
);
5268 // ----------------------------------------------------------------------------
5269 // methods forwarded to m_mainWin
5270 // ----------------------------------------------------------------------------
5272 #if wxUSE_DRAG_AND_DROP
5274 void wxGenericListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
5276 m_mainWin
->SetDropTarget( dropTarget
);
5279 wxDropTarget
*wxGenericListCtrl::GetDropTarget() const
5281 return m_mainWin
->GetDropTarget();
5284 #endif // wxUSE_DRAG_AND_DROP
5286 bool wxGenericListCtrl::SetCursor( const wxCursor
&cursor
)
5288 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
5291 wxColour
wxGenericListCtrl::GetBackgroundColour() const
5293 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
5296 wxColour
wxGenericListCtrl::GetForegroundColour() const
5298 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
5301 bool wxGenericListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
5304 return m_mainWin
->PopupMenu( menu
, x
, y
);
5307 #endif // wxUSE_MENUS
5310 void wxGenericListCtrl::SetFocus()
5312 /* The test in window.cpp fails as we are a composite
5313 window, so it checks against "this", but not m_mainWin. */
5314 if ( FindFocus() != this )
5315 m_mainWin
->SetFocus();
5318 // ----------------------------------------------------------------------------
5319 // virtual list control support
5320 // ----------------------------------------------------------------------------
5322 wxString
wxGenericListCtrl::OnGetItemText(long WXUNUSED(item
), long WXUNUSED(col
)) const
5324 // this is a pure virtual function, in fact - which is not really pure
5325 // because the controls which are not virtual don't need to implement it
5326 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5328 return wxEmptyString
;
5331 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item
)) const
5334 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
5340 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item
)) const
5342 wxASSERT_MSG( item
>= 0 && item
< GetItemCount(),
5343 _T("invalid item index in OnGetItemAttr()") );
5345 // no attributes by default
5349 void wxGenericListCtrl::SetItemCount(long count
)
5351 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5353 m_mainWin
->SetItemCount(count
);
5356 void wxGenericListCtrl::RefreshItem(long item
)
5358 m_mainWin
->RefreshLine(item
);
5361 void wxGenericListCtrl::RefreshItems(long itemFrom
, long itemTo
)
5363 m_mainWin
->RefreshLines(itemFrom
, itemTo
);
5367 * Generic wxListCtrl is more or less a container for two other
5368 * windows which drawings are done upon. These are namely
5369 * 'm_headerWin' and 'm_mainWin'.
5370 * Here we override 'virtual wxWindow::Refresh()' to mimic the
5371 * behaviour wxListCtrl has under wxMSW.
5373 void wxGenericListCtrl::Refresh(bool eraseBackground
, const wxRect
*rect
)
5377 // The easy case, no rectangle specified.
5379 m_headerWin
->Refresh(eraseBackground
);
5382 m_mainWin
->Refresh(eraseBackground
);
5386 // Refresh the header window
5389 wxRect rectHeader
= m_headerWin
->GetRect();
5390 rectHeader
.Intersect(*rect
);
5391 if (rectHeader
.GetWidth() && rectHeader
.GetHeight())
5394 m_headerWin
->GetPosition(&x
, &y
);
5395 rectHeader
.Offset(-x
, -y
);
5396 m_headerWin
->Refresh(eraseBackground
, &rectHeader
);
5401 // Refresh the main window
5404 wxRect rectMain
= m_mainWin
->GetRect();
5405 rectMain
.Intersect(*rect
);
5406 if (rectMain
.GetWidth() && rectMain
.GetHeight())
5409 m_mainWin
->GetPosition(&x
, &y
);
5410 rectMain
.Offset(-x
, -y
);
5411 m_mainWin
->Refresh(eraseBackground
, &rectMain
);
5417 void wxGenericListCtrl::Freeze()
5419 m_mainWin
->Freeze();
5422 void wxGenericListCtrl::Thaw()
5427 #endif // wxUSE_LISTCTRL