1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/listctrl.h
3 // Purpose: private definitions of wxListCtrl helpers
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_GENERIC_LISTCTRL_PRIVATE_H_
12 #define _WX_GENERIC_LISTCTRL_PRIVATE_H_
18 #include "wx/listctrl.h"
19 #include "wx/selstore.h"
21 #include "wx/settings.h"
23 // ============================================================================
25 // ============================================================================
27 //-----------------------------------------------------------------------------
28 // wxColWidthInfo (internal)
29 //-----------------------------------------------------------------------------
34 bool bNeedsUpdate
; // only set to true when an item whose
35 // width == nMaxWidth is removed
37 wxColWidthInfo(int w
= 0, bool needsUpdate
= false)
40 bNeedsUpdate
= needsUpdate
;
44 WX_DEFINE_ARRAY_PTR(wxColWidthInfo
*, ColWidthArray
);
46 //-----------------------------------------------------------------------------
47 // wxListItemData (internal)
48 //-----------------------------------------------------------------------------
53 wxListItemData(wxListMainWindow
*owner
);
56 void SetItem( const wxListItem
&info
);
57 void SetImage( int image
) { m_image
= image
; }
58 void SetData( wxUIntPtr data
) { m_data
= data
; }
59 void SetPosition( int x
, int y
);
60 void SetSize( int width
, int height
);
62 bool HasText() const { return !m_text
.empty(); }
63 const wxString
& GetText() const { return m_text
; }
64 void SetText(const wxString
& text
) { m_text
= text
; }
66 // we can't use empty string for measuring the string width/height, so
67 // always return something
68 wxString
GetTextForMeasuring() const
70 wxString s
= GetText();
77 bool IsHit( int x
, int y
) const;
82 int GetHeight() const;
84 int GetImage() const { return m_image
; }
85 bool HasImage() const { return GetImage() != -1; }
87 void GetItem( wxListItem
&info
) const;
89 void SetAttr(wxListItemAttr
*attr
) { m_attr
= attr
; }
90 wxListItemAttr
*GetAttr() const { return m_attr
; }
93 // the item image or -1
96 // user data associated with the item
99 // the item coordinates are not used in report mode; instead this pointer is
100 // NULL and the owner window is used to retrieve the item position and size
103 // the list ctrl we are in
104 wxListMainWindow
*m_owner
;
106 // custom attributes or NULL
107 wxListItemAttr
*m_attr
;
110 // common part of all ctors
116 //-----------------------------------------------------------------------------
117 // wxListHeaderData (internal)
118 //-----------------------------------------------------------------------------
120 class wxListHeaderData
: public wxObject
124 wxListHeaderData( const wxListItem
&info
);
125 void SetItem( const wxListItem
&item
);
126 void SetPosition( int x
, int y
);
127 void SetWidth( int w
);
128 void SetState( int state
);
129 void SetFormat( int format
);
130 void SetHeight( int h
);
131 bool HasImage() const;
133 bool HasText() const { return !m_text
.empty(); }
134 const wxString
& GetText() const { return m_text
; }
135 void SetText(const wxString
& text
) { m_text
= text
; }
137 void GetItem( wxListItem
&item
);
139 bool IsHit( int x
, int y
) const;
140 int GetImage() const;
141 int GetWidth() const;
142 int GetFormat() const;
143 int GetState() const;
160 //-----------------------------------------------------------------------------
161 // wxListLineData (internal)
162 //-----------------------------------------------------------------------------
164 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
169 // the list of subitems: only may have more than one item in report mode
170 wxListItemDataList m_items
;
172 // this is not used in report view
184 // the part to be highlighted
185 wxRect m_rectHighlight
;
187 // extend all our rects to be centered inside the one of given width
188 void ExtendWidth(wxCoord w
)
190 wxASSERT_MSG( m_rectAll
.width
<= w
,
191 wxT("width can only be increased") );
194 m_rectLabel
.x
= m_rectAll
.x
+ (w
- m_rectLabel
.width
) / 2;
195 m_rectIcon
.x
= m_rectAll
.x
+ (w
- m_rectIcon
.width
) / 2;
196 m_rectHighlight
.x
= m_rectAll
.x
+ (w
- m_rectHighlight
.width
) / 2;
201 // is this item selected? [NB: not used in virtual mode]
204 // back pointer to the list ctrl
205 wxListMainWindow
*m_owner
;
208 wxListLineData(wxListMainWindow
*owner
);
212 WX_CLEAR_LIST(wxListItemDataList
, m_items
);
216 // called by the owner when it toggles report view
217 void SetReportView(bool inReportView
)
219 // we only need m_gi when we're not in report view so update as needed
227 m_gi
= new GeometryInfo
;
231 // are we in report mode?
232 inline bool InReportView() const;
234 // are we in virtual report mode?
235 inline bool IsVirtual() const;
237 // these 2 methods shouldn't be called for report view controls, in that
238 // case we determine our position/size ourselves
240 // calculate the size of the line
241 void CalculateSize( wxDC
*dc
, int spacing
);
243 // remember the position this line appears at
244 void SetPosition( int x
, int y
, int spacing
);
248 void SetImage( int image
) { SetImage(0, image
); }
249 int GetImage() const { return GetImage(0); }
250 void SetImage( int index
, int image
);
251 int GetImage( int index
) const;
253 bool HasImage() const { return GetImage() != -1; }
254 bool HasText() const { return !GetText(0).empty(); }
256 void SetItem( int index
, const wxListItem
&info
);
257 void GetItem( int index
, wxListItem
&info
);
259 wxString
GetText(int index
) const;
260 void SetText( int index
, const wxString
& s
);
262 wxListItemAttr
*GetAttr() const;
263 void SetAttr(wxListItemAttr
*attr
);
265 // return true if the highlighting really changed
266 bool Highlight( bool on
);
268 void ReverseHighlight();
270 bool IsHighlighted() const
272 wxASSERT_MSG( !IsVirtual(), wxT("unexpected call to IsHighlighted") );
274 return m_highlighted
;
277 // draw the line on the given DC in icon/list mode
278 void Draw( wxDC
*dc
, bool current
);
280 // the same in report mode: it needs more parameters as we don't store
281 // everything in the item in report mode
282 void DrawInReportMode( wxDC
*dc
,
284 const wxRect
& rectHL
,
289 // set the line to contain num items (only can be > 1 in report mode)
290 void InitItems( int num
);
292 // get the mode (i.e. style) of the list control
293 inline int GetMode() const;
295 // Apply this item attributes to the given DC: set the text font and colour
296 // and also erase the background appropriately.
297 void ApplyAttributes(wxDC
*dc
,
298 const wxRect
& rectHL
,
302 // draw the text on the DC with the correct justification; also add an
303 // ellipsis if the text is too large to fit in the current width
304 void DrawTextFormatted(wxDC
*dc
,
305 const wxString
&text
,
308 int yMid
, // this is middle, not top, of the text
312 WX_DECLARE_OBJARRAY(wxListLineData
, wxListLineDataArray
);
314 //-----------------------------------------------------------------------------
315 // wxListHeaderWindow (internal)
316 //-----------------------------------------------------------------------------
318 class wxListHeaderWindow
: public wxWindow
321 wxListMainWindow
*m_owner
;
322 const wxCursor
*m_currentCursor
;
323 wxCursor
*m_resizeCursor
;
326 // column being resized or -1
329 // divider line position in logical (unscrolled) coords
332 // minimal position beyond which the divider line
333 // can't be dragged in logical coords
337 wxListHeaderWindow();
339 wxListHeaderWindow( wxWindow
*win
,
341 wxListMainWindow
*owner
,
342 const wxPoint
&pos
= wxDefaultPosition
,
343 const wxSize
&size
= wxDefaultSize
,
345 const wxString
&name
= wxT("wxlistctrlcolumntitles") );
347 virtual ~wxListHeaderWindow();
350 void AdjustDC( wxDC
& dc
);
352 void OnPaint( wxPaintEvent
&event
);
353 void OnMouse( wxMouseEvent
&event
);
354 void OnSetFocus( wxFocusEvent
&event
);
359 // Update main window's column later
360 bool m_sendSetColumnWidth
;
364 virtual void OnInternalIdle();
367 // common part of all ctors
370 // generate and process the list event of the given type, return true if
371 // it wasn't vetoed, i.e. if we should proceed
372 bool SendListEvent(wxEventType type
, const wxPoint
& pos
);
374 DECLARE_EVENT_TABLE()
377 //-----------------------------------------------------------------------------
378 // wxListRenameTimer (internal)
379 //-----------------------------------------------------------------------------
381 class wxListRenameTimer
: public wxTimer
384 wxListMainWindow
*m_owner
;
387 wxListRenameTimer( wxListMainWindow
*owner
);
391 //-----------------------------------------------------------------------------
392 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
393 //-----------------------------------------------------------------------------
395 class wxListTextCtrlWrapper
: public wxEvtHandler
398 // NB: text must be a valid object but not Create()d yet
399 wxListTextCtrlWrapper(wxListMainWindow
*owner
,
403 wxTextCtrl
*GetText() const { return m_text
; }
405 // Different reasons for calling EndEdit():
407 // It was called because:
410 End_Accept
, // user has accepted the changes.
411 End_Discard
, // user has cancelled editing.
412 End_Destroy
// the entire control is being destroyed.
415 void EndEdit(EndReason reason
);
418 void OnChar( wxKeyEvent
&event
);
419 void OnKeyUp( wxKeyEvent
&event
);
420 void OnKillFocus( wxFocusEvent
&event
);
422 bool AcceptChanges();
423 void Finish( bool setfocus
);
426 wxListMainWindow
*m_owner
;
428 wxString m_startValue
;
430 bool m_aboutToFinish
;
432 DECLARE_EVENT_TABLE()
435 //-----------------------------------------------------------------------------
436 // wxListMainWindow (internal)
437 //-----------------------------------------------------------------------------
439 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
441 class wxListMainWindow
: public wxWindow
445 wxListMainWindow( wxWindow
*parent
,
447 const wxPoint
& pos
= wxDefaultPosition
,
448 const wxSize
& size
= wxDefaultSize
,
450 const wxString
&name
= wxT("listctrlmainwindow") );
452 virtual ~wxListMainWindow();
454 // called by the main control when its mode changes
455 void SetReportView(bool inReportView
);
457 // helper to simplify testing for wxLC_XXX flags
458 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
460 // return true if this is a virtual list control
461 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
463 // return true if the control is in report mode
464 bool InReportView() const { return HasFlag(wxLC_REPORT
); }
466 // return true if we are in single selection mode, false if multi sel
467 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL
); }
469 // do we have a header window?
470 bool HasHeader() const
471 { return InReportView() && !HasFlag(wxLC_NO_HEADER
); }
473 void HighlightAll( bool on
);
475 // all these functions only do something if the line is currently visible
477 // change the line "selected" state, return true if it really changed
478 bool HighlightLine( size_t line
, bool highlight
= true);
480 // as HighlightLine() but do it for the range of lines: this is incredibly
481 // more efficient for virtual list controls!
483 // NB: unlike HighlightLine() this one does refresh the lines on screen
484 void HighlightLines( size_t lineFrom
, size_t lineTo
, bool on
= true );
486 // toggle the line state and refresh it
487 void ReverseHighlight( size_t line
)
488 { HighlightLine(line
, !IsHighlighted(line
)); RefreshLine(line
); }
490 // return true if the line is highlighted
491 bool IsHighlighted(size_t line
) const;
493 // refresh one or several lines at once
494 void RefreshLine( size_t line
);
495 void RefreshLines( size_t lineFrom
, size_t lineTo
);
497 // refresh all selected items
498 void RefreshSelected();
500 // refresh all lines below the given one: the difference with
501 // RefreshLines() is that the index here might not be a valid one (happens
502 // when the last line is deleted)
503 void RefreshAfter( size_t lineFrom
);
505 // the methods which are forwarded to wxListLineData itself in list/icon
506 // modes but are here because the lines don't store their positions in the
509 // get the bound rect for the entire line
510 wxRect
GetLineRect(size_t line
) const;
512 // get the bound rect of the label
513 wxRect
GetLineLabelRect(size_t line
) const;
515 // get the bound rect of the items icon (only may be called if we do have
517 wxRect
GetLineIconRect(size_t line
) const;
519 // get the rect to be highlighted when the item has focus
520 wxRect
GetLineHighlightRect(size_t line
) const;
522 // get the size of the total line rect
523 wxSize
GetLineSize(size_t line
) const
524 { return GetLineRect(line
).GetSize(); }
526 // return the hit code for the corresponding position (in this line)
527 long HitTestLine(size_t line
, int x
, int y
) const;
529 // bring the selected item into view, scrolling to it if necessary
530 void MoveToItem(size_t item
);
532 bool ScrollList( int WXUNUSED(dx
), int dy
);
534 // bring the current item into view
535 void MoveToFocus() { MoveToItem(m_current
); }
537 // start editing the label of the given item
538 wxTextCtrl
*EditLabel(long item
,
539 wxClassInfo
* textControlClass
= wxCLASSINFO(wxTextCtrl
));
540 wxTextCtrl
*GetEditControl() const
542 return m_textctrlWrapper
? m_textctrlWrapper
->GetText() : NULL
;
545 void ResetTextControl(wxTextCtrl
*text
)
548 m_textctrlWrapper
= NULL
;
551 void OnRenameTimer();
552 bool OnRenameAccept(size_t itemEdit
, const wxString
& value
);
553 void OnRenameCancelled(size_t itemEdit
);
555 void OnMouse( wxMouseEvent
&event
);
557 // called to switch the selection from the current item to newCurrent,
558 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
560 void OnChar( wxKeyEvent
&event
);
561 void OnKeyDown( wxKeyEvent
&event
);
562 void OnKeyUp( wxKeyEvent
&event
);
563 void OnSetFocus( wxFocusEvent
&event
);
564 void OnKillFocus( wxFocusEvent
&event
);
565 void OnScroll( wxScrollWinEvent
& event
);
567 void OnPaint( wxPaintEvent
&event
);
569 void OnChildFocus(wxChildFocusEvent
& event
);
571 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
572 void GetImageSize( int index
, int &width
, int &height
) const;
573 int GetTextLength( const wxString
&s
) const;
575 void SetImageList( wxImageList
*imageList
, int which
);
576 void SetItemSpacing( int spacing
, bool isSmall
= false );
577 int GetItemSpacing( bool isSmall
= false );
579 void SetColumn( int col
, wxListItem
&item
);
580 void SetColumnWidth( int col
, int width
);
581 void GetColumn( int col
, wxListItem
&item
) const;
582 int GetColumnWidth( int col
) const;
583 int GetColumnCount() const { return m_columns
.GetCount(); }
585 // returns the sum of the heights of all columns
586 int GetHeaderWidth() const;
588 int GetCountPerPage() const;
590 void SetItem( wxListItem
&item
);
591 void GetItem( wxListItem
&item
) const;
592 void SetItemState( long item
, long state
, long stateMask
);
593 void SetItemStateAll( long state
, long stateMask
);
594 int GetItemState( long item
, long stateMask
) const;
595 bool GetItemRect( long item
, wxRect
&rect
) const
597 return GetSubItemRect(item
, wxLIST_GETSUBITEMRECT_WHOLEITEM
, rect
);
599 bool GetSubItemRect( long item
, long subItem
, wxRect
& rect
) const;
600 wxRect
GetViewRect() const;
601 bool GetItemPosition( long item
, wxPoint
& pos
) const;
602 int GetSelectedItemCount() const;
604 wxString
GetItemText(long item
, int col
= 0) const
607 info
.m_mask
= wxLIST_MASK_TEXT
;
608 info
.m_itemId
= item
;
614 void SetItemText(long item
, const wxString
& value
)
617 info
.m_mask
= wxLIST_MASK_TEXT
;
618 info
.m_itemId
= item
;
623 wxImageList
* GetSmallImageList() const
624 { return m_small_image_list
; }
626 // set the scrollbars and update the positions of the items
627 void RecalculatePositions(bool noRefresh
= false);
629 // refresh the window and the header
632 long GetNextItem( long item
, int geometry
, int state
) const;
633 void DeleteItem( long index
);
634 void DeleteAllItems();
635 void DeleteColumn( int col
);
636 void DeleteEverything();
637 void EnsureVisible( long index
);
638 long FindItem( long start
, const wxString
& str
, bool partial
= false );
639 long FindItem( long start
, wxUIntPtr data
);
640 long FindItem( const wxPoint
& pt
);
641 long HitTest( int x
, int y
, int &flags
) const;
642 void InsertItem( wxListItem
&item
);
643 void InsertColumn( long col
, wxListItem
&item
);
644 int GetItemWidthWithImage(wxListItem
* item
);
645 void SortItems( wxListCtrlCompare fn
, wxIntPtr data
);
647 size_t GetItemCount() const;
648 bool IsEmpty() const { return GetItemCount() == 0; }
649 void SetItemCount(long count
);
651 // change the current (== focused) item, send a notification event
652 void ChangeCurrent(size_t current
);
653 void ResetCurrent() { ChangeCurrent((size_t)-1); }
654 bool HasCurrent() const { return m_current
!= (size_t)-1; }
656 // send out a wxListEvent
657 void SendNotify( size_t line
,
659 const wxPoint
& point
= wxDefaultPosition
);
661 // override base class virtual to reset m_lineHeight when the font changes
662 virtual bool SetFont(const wxFont
& font
)
664 if ( !wxWindow::SetFont(font
) )
672 // these are for wxListLineData usage only
674 // get the backpointer to the list ctrl
675 wxGenericListCtrl
*GetListCtrl() const
677 return wxStaticCast(GetParent(), wxGenericListCtrl
);
680 // get the height of all lines (assuming they all do have the same height)
681 wxCoord
GetLineHeight() const;
683 // get the y position of the given line (only for report view)
684 wxCoord
GetLineY(size_t line
) const;
686 // get the brush to use for the item highlighting
687 wxBrush
*GetHighlightBrush() const
689 return m_hasFocus
? m_highlightBrush
: m_highlightUnfocusedBrush
;
692 bool HasFocus() const
698 // the array of all line objects for a non virtual list control (for the
699 // virtual list control we only ever use m_lines[0])
700 wxListLineDataArray m_lines
;
702 // the list of column objects
703 wxListHeaderDataList m_columns
;
705 // currently focused item or -1
708 // the number of lines per page
711 // this flag is set when something which should result in the window
712 // redrawing happens (i.e. an item was added or deleted, or its appearance
713 // changed) and OnPaint() doesn't redraw the window while it is set which
714 // allows to minimize the number of repaintings when a lot of items are
715 // being added. The real repainting occurs only after the next OnIdle()
719 wxColour
*m_highlightColour
;
720 wxImageList
*m_small_image_list
;
721 wxImageList
*m_normal_image_list
;
723 int m_normal_spacing
;
727 wxTimer
*m_renameTimer
;
731 ColWidthArray m_aColWidths
;
733 // for double click logic
734 size_t m_lineLastClicked
,
735 m_lineBeforeLastClicked
,
736 m_lineSelectSingleOnUp
;
739 wxWindow
*GetMainWindowOfCompositeControl() { return GetParent(); }
741 // the total count of items in a virtual list control
744 // the object maintaining the items selection state, only used in virtual
746 wxSelectionStore m_selStore
;
748 // common part of all ctors
751 // get the line data for the given index
752 wxListLineData
*GetLine(size_t n
) const
754 wxASSERT_MSG( n
!= (size_t)-1, wxT("invalid line index") );
758 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
765 // get a dummy line which can be used for geometry calculations and such:
766 // you must use GetLine() if you want to really draw the line
767 wxListLineData
*GetDummyLine() const;
769 // cache the line data of the n-th line in m_lines[0]
770 void CacheLineData(size_t line
);
772 // get the range of visible lines
773 void GetVisibleLinesRange(size_t *from
, size_t *to
);
775 // force us to recalculate the range of visible lines
776 void ResetVisibleLinesRange() { m_lineFrom
= (size_t)-1; }
778 // get the colour to be used for drawing the rules
779 wxColour
GetRuleColour() const
781 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
785 // initialize the current item if needed
786 void UpdateCurrent();
788 // delete all items but don't refresh: called from dtor
789 void DoDeleteAllItems();
791 // the height of one line using the current font
792 wxCoord m_lineHeight
;
794 // the total header width or 0 if not calculated yet
795 wxCoord m_headerWidth
;
797 // the first and last lines being shown on screen right now (inclusive),
798 // both may be -1 if they must be calculated so never access them directly:
799 // use GetVisibleLinesRange() above instead
803 // the brushes to use for item highlighting when we do/don't have focus
804 wxBrush
*m_highlightBrush
,
805 *m_highlightUnfocusedBrush
;
807 // wrapper around the text control currently used for in place editing or
808 // NULL if no item is being edited
809 wxListTextCtrlWrapper
*m_textctrlWrapper
;
812 DECLARE_EVENT_TABLE()
814 friend class wxGenericListCtrl
;
817 #endif // wxUSE_LISTCTRL
818 #endif // _WX_GENERIC_LISTCTRL_PRIVATE_H_