+#if wxUSE_LISTCTRL
+
+#ifndef WX_PRECOMP
+ #include "wx/app.h"
+
+ #include "wx/dynarray.h"
+
+ #include "wx/dcscreen.h"
+
+ #include "wx/textctrl.h"
+#endif
+
+// under Win32 we always use the native version and also may use the generic
+// one, however some things should be done only if we use only the generic
+// version
+#if defined(__WIN32__) && !defined(__WXUNIVERSAL__)
+ #define HAVE_NATIVE_LISTCTRL
+#endif
+
+// if we have the native control, wx/listctrl.h declares it and not this one
+#ifdef HAVE_NATIVE_LISTCTRL
+ #include "wx/generic/listctrl.h"
+#else // !HAVE_NATIVE_LISTCTRL
+ #include "wx/listctrl.h"
+
+ // if we have a native version, its implementation file does all this
+ IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
+ IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl)
+ IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
+
+ IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxGenericListCtrl)
+#endif // HAVE_NATIVE_LISTCTRL/!HAVE_NATIVE_LISTCTRL
+
+#include "wx/selstore.h"
+
+#include "wx/renderer.h"
+
+#ifdef __WXMAC__
+ #include "wx/mac/private.h"
+#endif
+
+#include <math.h>
+
+
+// NOTE: If using the wxListBox visual attributes works everywhere then this can
+// be removed, as well as the #else case below.
+#define _USE_VISATTR 0
+
+
+// ----------------------------------------------------------------------------
+// events
+// ----------------------------------------------------------------------------
+
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS)
+#if WXWIN_COMPATIBILITY_2_4
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO)
+#endif
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_RIGHT_CLICK)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_DRAGGING)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_END_DRAG)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_FOCUSED)
+DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_CACHE_HINT)
+
+// ----------------------------------------------------------------------------
+// constants
+// ----------------------------------------------------------------------------
+
+// // the height of the header window (FIXME: should depend on its font!)
+// static const int HEADER_HEIGHT = 23;
+
+static const int SCROLL_UNIT_X = 15;
+
+// the spacing between the lines (in report mode)
+static const int LINE_SPACING = 0;
+
+// extra margins around the text label
+static const int EXTRA_WIDTH = 4;
+static const int EXTRA_HEIGHT = 4;
+
+// margin between the window and the items
+static const int EXTRA_BORDER_X = 2;
+static const int EXTRA_BORDER_Y = 2;
+
+// offset for the header window
+static const int HEADER_OFFSET_X = 1;
+static const int HEADER_OFFSET_Y = 1;
+
+// margin between rows of icons in [small] icon view
+static const int MARGIN_BETWEEN_ROWS = 6;
+
+// when autosizing the columns, add some slack
+static const int AUTOSIZE_COL_MARGIN = 10;
+
+// default and minimal widths for the header columns
+static const int WIDTH_COL_DEFAULT = 80;
+static const int WIDTH_COL_MIN = 10;
+
+// the space between the image and the text in the report mode
+static const int IMAGE_MARGIN_IN_REPORT_MODE = 5;
+
+// ============================================================================
+// private classes
+// ============================================================================
+
+//-----------------------------------------------------------------------------
+// wxListItemData (internal)
+//-----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxListItemData
+{
+public:
+ wxListItemData(wxListMainWindow *owner);
+ ~wxListItemData();
+
+ void SetItem( const wxListItem &info );
+ void SetImage( int image ) { m_image = image; }
+ void SetData( wxUIntPtr data ) { m_data = data; }
+ void SetPosition( int x, int y );
+ void SetSize( int width, int height );
+
+ bool HasText() const { return !m_text.empty(); }
+ const wxString& GetText() const { return m_text; }
+ void SetText(const wxString& text) { m_text = text; }
+
+ // we can't use empty string for measuring the string width/height, so
+ // always return something
+ wxString GetTextForMeasuring() const
+ {
+ wxString s = GetText();
+ if ( s.empty() )
+ s = _T('H');
+
+ return s;
+ }
+
+ bool IsHit( int x, int y ) const;
+
+ int GetX() const;
+ int GetY() const;
+ int GetWidth() const;
+ int GetHeight() const;
+
+ int GetImage() const { return m_image; }
+ bool HasImage() const { return GetImage() != -1; }
+
+ void GetItem( wxListItem &info ) const;
+
+ void SetAttr(wxListItemAttr *attr) { m_attr = attr; }
+ wxListItemAttr *GetAttr() const { return m_attr; }
+
+public:
+ // the item image or -1
+ int m_image;
+
+ // user data associated with the item
+ wxUIntPtr m_data;
+
+ // the item coordinates are not used in report mode, instead this pointer
+ // is NULL and the owner window is used to retrieve the item position and
+ // size
+ wxRect *m_rect;
+
+ // the list ctrl we are in
+ wxListMainWindow *m_owner;
+
+ // custom attributes or NULL
+ wxListItemAttr *m_attr;
+
+protected:
+ // common part of all ctors
+ void Init();
+
+ wxString m_text;
+};
+
+//-----------------------------------------------------------------------------
+// wxListHeaderData (internal)
+//-----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxListHeaderData : public wxObject
+{
+public:
+ wxListHeaderData();
+ wxListHeaderData( const wxListItem &info );
+ void SetItem( const wxListItem &item );
+ void SetPosition( int x, int y );
+ void SetWidth( int w );
+ void SetFormat( int format );
+ void SetHeight( int h );
+ bool HasImage() const;
+
+ bool HasText() const { return !m_text.empty(); }
+ const wxString& GetText() const { return m_text; }
+ void SetText(const wxString& text) { m_text = text; }
+
+ void GetItem( wxListItem &item );
+
+ bool IsHit( int x, int y ) const;
+ int GetImage() const;
+ int GetWidth() const;
+ int GetFormat() const;
+
+protected:
+ long m_mask;
+ int m_image;
+ wxString m_text;
+ int m_format;
+ int m_width;
+ int m_xpos,
+ m_ypos;
+ int m_height;
+
+private:
+ void Init();
+};
+
+//-----------------------------------------------------------------------------
+// wxListLineData (internal)
+//-----------------------------------------------------------------------------
+
+WX_DECLARE_LIST(wxListItemData, wxListItemDataList);
+#include "wx/listimpl.cpp"
+WX_DEFINE_LIST(wxListItemDataList);
+
+class wxListLineData
+{
+public:
+ // the list of subitems: only may have more than one item in report mode
+ wxListItemDataList m_items;
+
+ // this is not used in report view
+ struct GeometryInfo
+ {
+ // total item rect
+ wxRect m_rectAll;
+
+ // label only
+ wxRect m_rectLabel;
+
+ // icon only
+ wxRect m_rectIcon;
+
+ // the part to be highlighted
+ wxRect m_rectHighlight;
+
+ // extend all our rects to be centered inside theo ne of given width
+ void ExtendWidth(wxCoord w)
+ {
+ wxASSERT_MSG( m_rectAll.width <= w,
+ _T("width can only be increased") );
+
+ m_rectAll.width = w;
+ m_rectLabel.x = m_rectAll.x + (w - m_rectLabel.width)/2;
+ m_rectIcon.x = m_rectAll.x + (w - m_rectIcon.width)/2;
+ m_rectHighlight.x = m_rectAll.x + (w - m_rectHighlight.width)/2;
+ }
+ } *m_gi;
+
+ // is this item selected? [NB: not used in virtual mode]
+ bool m_highlighted;
+
+ // back pointer to the list ctrl
+ wxListMainWindow *m_owner;
+
+public:
+ wxListLineData(wxListMainWindow *owner);
+
+ ~wxListLineData()
+ {
+ WX_CLEAR_LIST(wxListItemDataList, m_items);
+ delete m_gi;
+ }
+
+ // are we in report mode?
+ inline bool InReportView() const;
+
+ // are we in virtual report mode?
+ inline bool IsVirtual() const;
+
+ // these 2 methods shouldn't be called for report view controls, in that
+ // case we determine our position/size ourselves
+
+ // calculate the size of the line
+ void CalculateSize( wxDC *dc, int spacing );
+
+ // remember the position this line appears at
+ void SetPosition( int x, int y, int spacing );
+
+ // wxListCtrl API
+
+ void SetImage( int image ) { SetImage(0, image); }
+ int GetImage() const { return GetImage(0); }
+ bool HasImage() const { return GetImage() != -1; }
+ bool HasText() const { return !GetText(0).empty(); }
+
+ void SetItem( int index, const wxListItem &info );
+ void GetItem( int index, wxListItem &info );
+
+ wxString GetText(int index) const;
+ void SetText( int index, const wxString s );
+
+ wxListItemAttr *GetAttr() const;
+ void SetAttr(wxListItemAttr *attr);
+
+ // return true if the highlighting really changed
+ bool Highlight( bool on );
+
+ void ReverseHighlight();
+
+ bool IsHighlighted() const
+ {
+ wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
+
+ return m_highlighted;
+ }
+
+ // draw the line on the given DC in icon/list mode
+ void Draw( wxDC *dc );
+
+ // the same in report mode
+ void DrawInReportMode( wxDC *dc,
+ const wxRect& rect,
+ const wxRect& rectHL,
+ bool highlighted );
+
+private:
+ // set the line to contain num items (only can be > 1 in report mode)
+ void InitItems( int num );
+
+ // get the mode (i.e. style) of the list control
+ inline int GetMode() const;
+
+ // prepare the DC for drawing with these item's attributes, return true if
+ // we need to draw the items background to highlight it, false otherwise
+ bool SetAttributes(wxDC *dc,
+ const wxListItemAttr *attr,
+ bool highlight);
+
+ // draw the text on the DC with the correct justification; also add an
+ // ellipsis if the text is too large to fit in the current width
+ void DrawTextFormatted(wxDC *dc, const wxString &text, int col, int x, int y, int width);
+
+ // these are only used by GetImage/SetImage above, we don't support images
+ // with subitems at the public API level yet
+ void SetImage( int index, int image );
+ int GetImage( int index ) const;
+};
+
+WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData, wxListLineDataArray);
+#include "wx/arrimpl.cpp"
+WX_DEFINE_OBJARRAY(wxListLineDataArray);
+
+//-----------------------------------------------------------------------------
+// wxListHeaderWindow (internal)
+//-----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxListHeaderWindow : public wxWindow
+{
+protected:
+ wxListMainWindow *m_owner;
+ wxCursor *m_currentCursor;
+ wxCursor *m_resizeCursor;
+ bool m_isDragging;
+
+ // column being resized or -1
+ int m_column;
+
+ // divider line position in logical (unscrolled) coords
+ int m_currentX;
+
+ // minimal position beyond which the divider line can't be dragged in
+ // logical coords
+ int m_minX;
+
+public:
+ wxListHeaderWindow();
+
+ wxListHeaderWindow( wxWindow *win,
+ wxWindowID id,
+ wxListMainWindow *owner,
+ const wxPoint &pos = wxDefaultPosition,
+ const wxSize &size = wxDefaultSize,
+ long style = 0,
+ const wxString &name = wxT("wxlistctrlcolumntitles") );
+
+ virtual ~wxListHeaderWindow();
+
+ void DrawCurrent();
+ void AdjustDC(wxDC& dc);
+
+ void OnPaint( wxPaintEvent &event );
+ void OnMouse( wxMouseEvent &event );
+ void OnSetFocus( wxFocusEvent &event );
+
+ // needs refresh
+ bool m_dirty;
+
+private:
+ // common part of all ctors
+ void Init();
+
+ // generate and process the list event of the given type, return true if
+ // it wasn't vetoed, i.e. if we should proceed
+ bool SendListEvent(wxEventType type, wxPoint pos);
+
+ DECLARE_DYNAMIC_CLASS(wxListHeaderWindow)
+ DECLARE_EVENT_TABLE()
+};
+
+//-----------------------------------------------------------------------------
+// wxListRenameTimer (internal)
+//-----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxListRenameTimer: public wxTimer
+{
+private:
+ wxListMainWindow *m_owner;
+
+public:
+ wxListRenameTimer( wxListMainWindow *owner );
+ void Notify();
+};
+
+//-----------------------------------------------------------------------------
+// wxListTextCtrl (internal)
+//-----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxListTextCtrl: public wxTextCtrl
+{
+public:
+ wxListTextCtrl(wxListMainWindow *owner, size_t itemEdit);
+
+protected:
+ void OnChar( wxKeyEvent &event );
+ void OnKeyUp( wxKeyEvent &event );
+ void OnKillFocus( wxFocusEvent &event );
+
+ bool AcceptChanges();
+ void Finish();
+
+private:
+ wxListMainWindow *m_owner;
+ wxString m_startValue;
+ size_t m_itemEdited;
+ bool m_finished;
+
+ DECLARE_EVENT_TABLE()
+};
+
+//-----------------------------------------------------------------------------
+// wxListMainWindow (internal)
+//-----------------------------------------------------------------------------
+
+WX_DECLARE_LIST(wxListHeaderData, wxListHeaderDataList);
+#include "wx/listimpl.cpp"
+WX_DEFINE_LIST(wxListHeaderDataList);
+
+class wxListMainWindow : public wxScrolledWindow
+{
+public:
+ wxListMainWindow();
+ wxListMainWindow( wxWindow *parent,
+ wxWindowID id,
+ const wxPoint& pos = wxDefaultPosition,
+ const wxSize& size = wxDefaultSize,
+ long style = 0,
+ const wxString &name = _T("listctrlmainwindow") );
+
+ virtual ~wxListMainWindow();
+
+ wxWindow *GetMainWindowOfCompositeControl() { return GetParent(); }
+
+ bool HasFlag(int flag) const { return m_parent->HasFlag(flag); }
+
+ // return true if this is a virtual list control
+ bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL); }
+
+ // return true if the control is in report mode
+ bool InReportView() const { return HasFlag(wxLC_REPORT); }
+
+ // return true if we are in single selection mode, false if multi sel
+ bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL); }
+
+ // do we have a header window?
+ bool HasHeader() const
+ { return InReportView() && !HasFlag(wxLC_NO_HEADER); }
+
+ void HighlightAll( bool on );
+
+ // all these functions only do something if the line is currently visible
+
+ // change the line "selected" state, return true if it really changed
+ bool HighlightLine( size_t line, bool highlight = true);
+
+ // as HighlightLine() but do it for the range of lines: this is incredibly
+ // more efficient for virtual list controls!
+ //
+ // NB: unlike HighlightLine() this one does refresh the lines on screen
+ void HighlightLines( size_t lineFrom, size_t lineTo, bool on = true );
+
+ // toggle the line state and refresh it
+ void ReverseHighlight( size_t line )
+ { HighlightLine(line, !IsHighlighted(line)); RefreshLine(line); }
+
+ // return true if the line is highlighted
+ bool IsHighlighted(size_t line) const;
+
+ // refresh one or several lines at once
+ void RefreshLine( size_t line );
+ void RefreshLines( size_t lineFrom, size_t lineTo );
+
+ // refresh all selected items
+ void RefreshSelected();
+
+ // refresh all lines below the given one: the difference with
+ // RefreshLines() is that the index here might not be a valid one (happens
+ // when the last line is deleted)
+ void RefreshAfter( size_t lineFrom );
+
+ // the methods which are forwarded to wxListLineData itself in list/icon
+ // modes but are here because the lines don't store their positions in the
+ // report mode
+
+ // get the bound rect for the entire line
+ wxRect GetLineRect(size_t line) const;
+
+ // get the bound rect of the label
+ wxRect GetLineLabelRect(size_t line) const;
+
+ // get the bound rect of the items icon (only may be called if we do have
+ // an icon!)
+ wxRect GetLineIconRect(size_t line) const;
+
+ // get the rect to be highlighted when the item has focus
+ wxRect GetLineHighlightRect(size_t line) const;
+
+ // get the size of the total line rect
+ wxSize GetLineSize(size_t line) const
+ { return GetLineRect(line).GetSize(); }
+
+ // return the hit code for the corresponding position (in this line)
+ long HitTestLine(size_t line, int x, int y) const;
+
+ // bring the selected item into view, scrolling to it if necessary
+ void MoveToItem(size_t item);
+
+ // bring the current item into view
+ void MoveToFocus() { MoveToItem(m_current); }
+
+ // start editing the label of the given item
+ void EditLabel( long item );
+
+ // suspend/resume redrawing the control
+ void Freeze();
+ void Thaw();
+
+ void SetFocus();
+
+ void OnRenameTimer();
+ bool OnRenameAccept(size_t itemEdit, const wxString& value);
+ void OnRenameCancelled(size_t itemEdit);
+
+ void OnMouse( wxMouseEvent &event );
+
+ // called to switch the selection from the current item to newCurrent,
+ void OnArrowChar( size_t newCurrent, const wxKeyEvent& event );
+
+ void OnChar( wxKeyEvent &event );
+ void OnKeyDown( wxKeyEvent &event );
+ void OnSetFocus( wxFocusEvent &event );
+ void OnKillFocus( wxFocusEvent &event );
+ void OnScroll(wxScrollWinEvent& event) ;
+
+ void OnPaint( wxPaintEvent &event );
+
+ void DrawImage( int index, wxDC *dc, int x, int y );
+ void GetImageSize( int index, int &width, int &height ) const;
+ int GetTextLength( const wxString &s ) const;
+
+ void SetImageList( wxImageListType *imageList, int which );
+ void SetItemSpacing( int spacing, bool isSmall = false );
+ int GetItemSpacing( bool isSmall = false );
+
+ void SetColumn( int col, wxListItem &item );
+ void SetColumnWidth( int col, int width );
+ void GetColumn( int col, wxListItem &item ) const;
+ int GetColumnWidth( int col ) const;
+ int GetColumnCount() const { return m_columns.GetCount(); }
+
+ // returns the sum of the heights of all columns
+ int GetHeaderWidth() const;
+
+ int GetCountPerPage() const;
+
+ void SetItem( wxListItem &item );
+ void GetItem( wxListItem &item ) const;
+ void SetItemState( long item, long state, long stateMask );
+ int GetItemState( long item, long stateMask ) const;
+ void GetItemRect( long index, wxRect &rect ) const;
+ wxRect GetViewRect() const;
+ bool GetItemPosition( long item, wxPoint& pos ) const;
+ int GetSelectedItemCount() const;
+
+ wxString GetItemText(long item) const
+ {
+ wxListItem info;
+ info.m_itemId = item;
+ GetItem( info );
+ return info.m_text;
+ }
+
+ void SetItemText(long item, const wxString& value)
+ {
+ wxListItem info;
+ info.m_mask = wxLIST_MASK_TEXT;
+ info.m_itemId = item;
+ info.m_text = value;
+ SetItem( info );
+ }
+
+ // set the scrollbars and update the positions of the items
+ void RecalculatePositions(bool noRefresh = false);
+
+ // refresh the window and the header
+ void RefreshAll();
+
+ long GetNextItem( long item, int geometry, int state ) const;
+ void DeleteItem( long index );
+ void DeleteAllItems();
+ void DeleteColumn( int col );
+ void DeleteEverything();
+ void EnsureVisible( long index );
+ long FindItem( long start, const wxString& str, bool partial = false );
+ long FindItem( long start, wxUIntPtr data);
+ long FindItem( const wxPoint& pt );
+ long HitTest( int x, int y, int &flags );
+ void InsertItem( wxListItem &item );
+ void InsertColumn( long col, wxListItem &item );
+ void SortItems( wxListCtrlCompare fn, long data );
+
+ size_t GetItemCount() const;
+ bool IsEmpty() const { return GetItemCount() == 0; }
+ void SetItemCount(long count);
+
+ // change the current (== focused) item, send a notification event
+ void ChangeCurrent(size_t current);
+ void ResetCurrent() { ChangeCurrent((size_t)-1); }
+ bool HasCurrent() const { return m_current != (size_t)-1; }
+
+ // send out a wxListEvent
+ void SendNotify( size_t line,
+ wxEventType command,
+ wxPoint point = wxDefaultPosition );
+
+ // override base class virtual to reset m_lineHeight when the font changes
+ virtual bool SetFont(const wxFont& font)
+ {
+ if ( !wxScrolledWindow::SetFont(font) )
+ return false;
+
+ m_lineHeight = 0;
+
+ return true;
+ }
+
+ // these are for wxListLineData usage only
+
+ // get the backpointer to the list ctrl
+ wxGenericListCtrl *GetListCtrl() const
+ {
+ return wxStaticCast(GetParent(), wxGenericListCtrl);
+ }
+
+ // get the height of all lines (assuming they all do have the same height)
+ wxCoord GetLineHeight() const;
+
+ // get the y position of the given line (only for report view)
+ wxCoord GetLineY(size_t line) const;
+
+ // get the brush to use for the item highlighting
+ wxBrush *GetHighlightBrush() const
+ {
+ return m_hasFocus ? m_highlightBrush : m_highlightUnfocusedBrush;
+ }
+
+//protected:
+ // the array of all line objects for a non virtual list control (for the
+ // virtual list control we only ever use m_lines[0])
+ wxListLineDataArray m_lines;
+
+ // the list of column objects
+ wxListHeaderDataList m_columns;
+
+ // currently focused item or -1
+ size_t m_current;
+
+ // the number of lines per page
+ int m_linesPerPage;
+
+ // this flag is set when something which should result in the window
+ // redrawing happens (i.e. an item was added or deleted, or its appearance
+ // changed) and OnPaint() doesn't redraw the window while it is set which
+ // allows to minimize the number of repaintings when a lot of items are
+ // being added. The real repainting occurs only after the next OnIdle()
+ // call
+ bool m_dirty;
+
+ wxColour *m_highlightColour;
+ wxImageListType *m_small_image_list;
+ wxImageListType *m_normal_image_list;
+ int m_small_spacing;
+ int m_normal_spacing;
+ bool m_hasFocus;
+
+ bool m_lastOnSame;
+ wxTimer *m_renameTimer;
+ bool m_isCreated;
+ int m_dragCount;
+ wxPoint m_dragStart;
+
+ // for double click logic
+ size_t m_lineLastClicked,
+ m_lineBeforeLastClicked;
+
+protected:
+ // the total count of items in a virtual list control
+ size_t m_countVirt;
+
+ // the object maintaining the items selection state, only used in virtual
+ // controls
+ wxSelectionStore m_selStore;
+
+ // common part of all ctors
+ void Init();
+
+ // get the line data for the given index
+ wxListLineData *GetLine(size_t n) const
+ {
+ wxASSERT_MSG( n != (size_t)-1, _T("invalid line index") );
+
+ if ( IsVirtual() )
+ {
+ wxConstCast(this, wxListMainWindow)->CacheLineData(n);
+
+ n = 0;
+ }
+
+ return &m_lines[n];
+ }
+
+ // get a dummy line which can be used for geometry calculations and such:
+ // you must use GetLine() if you want to really draw the line
+ wxListLineData *GetDummyLine() const;
+
+ // cache the line data of the n-th line in m_lines[0]
+ void CacheLineData(size_t line);
+
+ // get the range of visible lines
+ void GetVisibleLinesRange(size_t *from, size_t *to);
+
+ // force us to recalculate the range of visible lines
+ void ResetVisibleLinesRange() { m_lineFrom = (size_t)-1; }
+
+ // get the colour to be used for drawing the rules
+ wxColour GetRuleColour() const
+ {
+#ifdef __WXMAC__
+ return *wxWHITE;
+#else
+ return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
+#endif
+ }
+
+private:
+ // initialize the current item if needed
+ void UpdateCurrent();
+
+ // delete all items but don't refresh: called from dtor
+ void DoDeleteAllItems();
+
+ // the height of one line using the current font
+ wxCoord m_lineHeight;
+
+ // the total header width or 0 if not calculated yet
+ wxCoord m_headerWidth;
+
+ // the first and last lines being shown on screen right now (inclusive),
+ // both may be -1 if they must be calculated so never access them directly:
+ // use GetVisibleLinesRange() above instead
+ size_t m_lineFrom,
+ m_lineTo;
+
+ // the brushes to use for item highlighting when we do/don't have focus
+ wxBrush *m_highlightBrush,
+ *m_highlightUnfocusedBrush;
+
+ // if this is > 0, the control is frozen and doesn't redraw itself
+ size_t m_freezeCount;
+
+ DECLARE_DYNAMIC_CLASS(wxListMainWindow)
+ DECLARE_EVENT_TABLE()
+
+ friend class wxGenericListCtrl;
+};
+
+// ============================================================================
+// implementation
+// ============================================================================
+
+//-----------------------------------------------------------------------------
+// wxListItemData
+//-----------------------------------------------------------------------------
+
+wxListItemData::~wxListItemData()
+{
+ // in the virtual list control the attributes are managed by the main
+ // program, so don't delete them
+ if ( !m_owner->IsVirtual() )
+ {
+ delete m_attr;
+ }
+
+ delete m_rect;
+}
+
+void wxListItemData::Init()
+{
+ m_image = -1;
+ m_data = 0;
+
+ m_attr = NULL;
+}
+
+wxListItemData::wxListItemData(wxListMainWindow *owner)
+{
+ Init();
+
+ m_owner = owner;
+
+ if ( owner->InReportView() )
+ {
+ m_rect = NULL;
+ }
+ else
+ {
+ m_rect = new wxRect;
+ }
+}
+
+void wxListItemData::SetItem( const wxListItem &info )
+{
+ if ( info.m_mask & wxLIST_MASK_TEXT )
+ SetText(info.m_text);
+ if ( info.m_mask & wxLIST_MASK_IMAGE )
+ m_image = info.m_image;
+ if ( info.m_mask & wxLIST_MASK_DATA )
+ m_data = info.m_data;
+
+ if ( info.HasAttributes() )
+ {
+ if ( m_attr )
+ *m_attr = *info.GetAttributes();
+ else
+ m_attr = new wxListItemAttr(*info.GetAttributes());
+ }
+
+ if ( m_rect )
+ {
+ m_rect->x =
+ m_rect->y =
+ m_rect->height = 0;
+ m_rect->width = info.m_width;
+ }
+}
+
+void wxListItemData::SetPosition( int x, int y )
+{
+ wxCHECK_RET( m_rect, _T("unexpected SetPosition() call") );
+
+ m_rect->x = x;
+ m_rect->y = y;
+}
+
+void wxListItemData::SetSize( int width, int height )
+{
+ wxCHECK_RET( m_rect, _T("unexpected SetSize() call") );
+
+ if ( width != -1 )
+ m_rect->width = width;
+ if ( height != -1 )
+ m_rect->height = height;
+}
+
+bool wxListItemData::IsHit( int x, int y ) const
+{
+ wxCHECK_MSG( m_rect, false, _T("can't be called in this mode") );
+
+ return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x, y);
+}
+
+int wxListItemData::GetX() const
+{
+ wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
+
+ return m_rect->x;
+}
+
+int wxListItemData::GetY() const
+{
+ wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
+
+ return m_rect->y;
+}
+
+int wxListItemData::GetWidth() const
+{
+ wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
+
+ return m_rect->width;
+}
+
+int wxListItemData::GetHeight() const
+{
+ wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
+
+ return m_rect->height;
+}
+
+void wxListItemData::GetItem( wxListItem &info ) const
+{
+ info.m_text = m_text;
+ info.m_image = m_image;
+ info.m_data = m_data;
+
+ if ( m_attr )
+ {
+ if ( m_attr->HasTextColour() )
+ info.SetTextColour(m_attr->GetTextColour());
+ if ( m_attr->HasBackgroundColour() )
+ info.SetBackgroundColour(m_attr->GetBackgroundColour());
+ if ( m_attr->HasFont() )
+ info.SetFont(m_attr->GetFont());
+ }
+}
+
+//-----------------------------------------------------------------------------
+// wxListHeaderData
+//-----------------------------------------------------------------------------
+
+void wxListHeaderData::Init()
+{
+ m_mask = 0;
+ m_image = -1;
+ m_format = 0;
+ m_width = 0;
+ m_xpos = 0;
+ m_ypos = 0;
+ m_height = 0;
+}
+
+wxListHeaderData::wxListHeaderData()
+{
+ Init();
+}
+
+wxListHeaderData::wxListHeaderData( const wxListItem &item )
+{
+ Init();
+
+ SetItem( item );
+}
+
+void wxListHeaderData::SetItem( const wxListItem &item )
+{
+ m_mask = item.m_mask;
+
+ if ( m_mask & wxLIST_MASK_TEXT )
+ m_text = item.m_text;
+
+ if ( m_mask & wxLIST_MASK_IMAGE )
+ m_image = item.m_image;
+
+ if ( m_mask & wxLIST_MASK_FORMAT )
+ m_format = item.m_format;
+
+ if ( m_mask & wxLIST_MASK_WIDTH )
+ SetWidth(item.m_width);
+}
+
+void wxListHeaderData::SetPosition( int x, int y )
+{
+ m_xpos = x;
+ m_ypos = y;
+}
+
+void wxListHeaderData::SetHeight( int h )
+{
+ m_height = h;
+}
+
+void wxListHeaderData::SetWidth( int w )
+{
+ m_width = w;
+ if (m_width < 0)
+ m_width = WIDTH_COL_DEFAULT;
+ else if (m_width < WIDTH_COL_MIN)
+ m_width = WIDTH_COL_MIN;
+}
+
+void wxListHeaderData::SetFormat( int format )
+{
+ m_format = format;
+}
+
+bool wxListHeaderData::HasImage() const
+{
+ return m_image != -1;
+}
+
+bool wxListHeaderData::IsHit( int x, int y ) const
+{
+ return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
+}
+
+void wxListHeaderData::GetItem( wxListItem& item )
+{
+ item.m_mask = m_mask;
+ item.m_text = m_text;
+ item.m_image = m_image;
+ item.m_format = m_format;
+ item.m_width = m_width;
+}
+
+int wxListHeaderData::GetImage() const
+{
+ return m_image;
+}
+
+int wxListHeaderData::GetWidth() const
+{
+ return m_width;
+}
+
+int wxListHeaderData::GetFormat() const
+{
+ return m_format;
+}
+
+//-----------------------------------------------------------------------------
+// wxListLineData
+//-----------------------------------------------------------------------------
+
+inline int wxListLineData::GetMode() const
+{
+ return m_owner->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE;
+}
+
+inline bool wxListLineData::InReportView() const
+{
+ return m_owner->HasFlag(wxLC_REPORT);
+}
+
+inline bool wxListLineData::IsVirtual() const
+{
+ return m_owner->IsVirtual();
+}
+
+wxListLineData::wxListLineData( wxListMainWindow *owner )
+{
+ m_owner = owner;
+
+ if ( InReportView() )
+ {
+ m_gi = NULL;
+ }
+ else // !report
+ {
+ m_gi = new GeometryInfo;
+ }
+
+ m_highlighted = false;
+
+ InitItems( GetMode() == wxLC_REPORT ? m_owner->GetColumnCount() : 1 );
+}
+
+void wxListLineData::CalculateSize( wxDC *dc, int spacing )
+{
+ wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
+ wxCHECK_RET( node, _T("no subitems at all??") );
+
+ wxListItemData *item = node->GetData();
+
+ wxString s;
+ wxCoord lw, lh;
+
+ switch ( GetMode() )
+ {
+ case wxLC_ICON:
+ case wxLC_SMALL_ICON:
+ m_gi->m_rectAll.width = spacing;
+
+ s = item->GetText();
+
+ if ( s.empty() )
+ {
+ lh =
+ m_gi->m_rectLabel.width =
+ m_gi->m_rectLabel.height = 0;
+ }
+ else // has label
+ {
+ dc->GetTextExtent( s, &lw, &lh );
+ lw += EXTRA_WIDTH;
+ lh += EXTRA_HEIGHT;
+
+ m_gi->m_rectAll.height = spacing + lh;
+ if (lw > spacing)
+ m_gi->m_rectAll.width = lw;
+
+ m_gi->m_rectLabel.width = lw;
+ m_gi->m_rectLabel.height = lh;
+ }
+
+ if (item->HasImage())
+ {
+ int w, h;
+ m_owner->GetImageSize( item->GetImage(), w, h );
+ m_gi->m_rectIcon.width = w + 8;
+ m_gi->m_rectIcon.height = h + 8;
+
+ if ( m_gi->m_rectIcon.width > m_gi->m_rectAll.width )
+ m_gi->m_rectAll.width = m_gi->m_rectIcon.width;
+ if ( m_gi->m_rectIcon.height + lh > m_gi->m_rectAll.height - 4 )
+ m_gi->m_rectAll.height = m_gi->m_rectIcon.height + lh + 4;
+ }
+
+ if ( item->HasText() )
+ {
+ m_gi->m_rectHighlight.width = m_gi->m_rectLabel.width;
+ m_gi->m_rectHighlight.height = m_gi->m_rectLabel.height;
+ }
+ else // no text, highlight the icon
+ {
+ m_gi->m_rectHighlight.width = m_gi->m_rectIcon.width;
+ m_gi->m_rectHighlight.height = m_gi->m_rectIcon.height;
+ }
+ break;
+
+ case wxLC_LIST:
+ s = item->GetTextForMeasuring();
+
+ dc->GetTextExtent( s, &lw, &lh );
+ lw += EXTRA_WIDTH;
+ lh += EXTRA_HEIGHT;
+
+ m_gi->m_rectLabel.width = lw;
+ m_gi->m_rectLabel.height = lh;
+
+ m_gi->m_rectAll.width = lw;
+ m_gi->m_rectAll.height = lh;
+
+ if (item->HasImage())
+ {
+ int w, h;
+ m_owner->GetImageSize( item->GetImage(), w, h );
+ m_gi->m_rectIcon.width = w;
+ m_gi->m_rectIcon.height = h;
+
+ m_gi->m_rectAll.width += 4 + w;
+ if (h > m_gi->m_rectAll.height)
+ m_gi->m_rectAll.height = h;
+ }
+
+ m_gi->m_rectHighlight.width = m_gi->m_rectAll.width;
+ m_gi->m_rectHighlight.height = m_gi->m_rectAll.height;
+ break;
+
+ case wxLC_REPORT:
+ wxFAIL_MSG( _T("unexpected call to SetSize") );
+ break;
+
+ default:
+ wxFAIL_MSG( _T("unknown mode") );
+ }
+}
+
+void wxListLineData::SetPosition( int x, int y, int spacing )
+{
+ wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
+ wxCHECK_RET( node, _T("no subitems at all??") );
+
+ wxListItemData *item = node->GetData();
+
+ switch ( GetMode() )
+ {
+ case wxLC_ICON:
+ case wxLC_SMALL_ICON:
+ m_gi->m_rectAll.x = x;
+ m_gi->m_rectAll.y = y;
+
+ if ( item->HasImage() )
+ {
+ m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 4 +
+ (m_gi->m_rectAll.width - m_gi->m_rectIcon.width) / 2;
+ m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 4;
+ }
+
+ if ( item->HasText() )
+ {
+ if (m_gi->m_rectAll.width > spacing)
+ m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 2;
+ else
+ m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 2 + (spacing/2) - (m_gi->m_rectLabel.width/2);
+ m_gi->m_rectLabel.y = m_gi->m_rectAll.y + m_gi->m_rectAll.height + 2 - m_gi->m_rectLabel.height;
+ m_gi->m_rectHighlight.x = m_gi->m_rectLabel.x - 2;
+ m_gi->m_rectHighlight.y = m_gi->m_rectLabel.y - 2;
+ }
+ else // no text, highlight the icon
+ {
+ m_gi->m_rectHighlight.x = m_gi->m_rectIcon.x - 4;
+ m_gi->m_rectHighlight.y = m_gi->m_rectIcon.y - 4;
+ }
+ break;
+
+ case wxLC_LIST:
+ m_gi->m_rectAll.x = x;
+ m_gi->m_rectAll.y = y;
+
+ m_gi->m_rectHighlight.x = m_gi->m_rectAll.x;
+ m_gi->m_rectHighlight.y = m_gi->m_rectAll.y;
+ m_gi->m_rectLabel.y = m_gi->m_rectAll.y + 2;
+
+ if (item->HasImage())
+ {
+ m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 2;
+ m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 2;
+ m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 6 + m_gi->m_rectIcon.width;
+ }
+ else
+ {
+ m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 2;
+ }
+ break;
+
+ case wxLC_REPORT:
+ wxFAIL_MSG( _T("unexpected call to SetPosition") );
+ break;
+
+ default:
+ wxFAIL_MSG( _T("unknown mode") );
+ }
+}
+
+void wxListLineData::InitItems( int num )
+{
+ for (int i = 0; i < num; i++)
+ m_items.Append( new wxListItemData(m_owner) );
+}
+
+void wxListLineData::SetItem( int index, const wxListItem &info )
+{
+ wxListItemDataList::compatibility_iterator node = m_items.Item( index );
+ wxCHECK_RET( node, _T("invalid column index in SetItem") );
+
+ wxListItemData *item = node->GetData();
+ item->SetItem( info );
+}
+
+void wxListLineData::GetItem( int index, wxListItem &info )
+{
+ wxListItemDataList::compatibility_iterator node = m_items.Item( index );
+ if (node)
+ {
+ wxListItemData *item = node->GetData();
+ item->GetItem( info );
+ }
+}
+
+wxString wxListLineData::GetText(int index) const
+{
+ wxString s;
+
+ wxListItemDataList::compatibility_iterator node = m_items.Item( index );
+ if (node)
+ {
+ wxListItemData *item = node->GetData();
+ s = item->GetText();
+ }
+
+ return s;
+}
+
+void wxListLineData::SetText( int index, const wxString s )
+{
+ wxListItemDataList::compatibility_iterator node = m_items.Item( index );
+ if (node)
+ {
+ wxListItemData *item = node->GetData();
+ item->SetText( s );
+ }
+}
+
+void wxListLineData::SetImage( int index, int image )
+{
+ wxListItemDataList::compatibility_iterator node = m_items.Item( index );
+ wxCHECK_RET( node, _T("invalid column index in SetImage()") );
+
+ wxListItemData *item = node->GetData();
+ item->SetImage(image);
+}
+
+int wxListLineData::GetImage( int index ) const
+{
+ wxListItemDataList::compatibility_iterator node = m_items.Item( index );
+ wxCHECK_MSG( node, -1, _T("invalid column index in GetImage()") );
+
+ wxListItemData *item = node->GetData();
+ return item->GetImage();
+}
+
+wxListItemAttr *wxListLineData::GetAttr() const
+{
+ wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
+ wxCHECK_MSG( node, NULL, _T("invalid column index in GetAttr()") );
+
+ wxListItemData *item = node->GetData();
+ return item->GetAttr();
+}
+
+void wxListLineData::SetAttr(wxListItemAttr *attr)
+{
+ wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
+ wxCHECK_RET( node, _T("invalid column index in SetAttr()") );
+
+ wxListItemData *item = node->GetData();
+ item->SetAttr(attr);
+}
+
+bool wxListLineData::SetAttributes(wxDC *dc,
+ const wxListItemAttr *attr,
+ bool highlighted)
+{
+ wxWindow *listctrl = m_owner->GetParent();
+
+ // fg colour
+
+ // don't use foreground colour for drawing highlighted items - this might
+ // make them completely invisible (and there is no way to do bit
+ // arithmetics on wxColour, unfortunately)
+ wxColour colText;
+ if ( highlighted )
+ {
+ colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
+ }
+ else
+ {
+ if ( attr && attr->HasTextColour() )
+ {
+ colText = attr->GetTextColour();
+ }
+ else
+ {
+ colText = listctrl->GetForegroundColour();
+ }
+ }
+
+ dc->SetTextForeground(colText);
+
+ // font
+ wxFont font;
+ if ( attr && attr->HasFont() )
+ {
+ font = attr->GetFont();
+ }
+ else
+ {
+ font = listctrl->GetFont();
+ }
+
+ dc->SetFont(font);
+
+ // bg colour
+ bool hasBgCol = attr && attr->HasBackgroundColour();
+ if ( highlighted || hasBgCol )
+ {
+ if ( highlighted )
+ {
+ dc->SetBrush( *m_owner->GetHighlightBrush() );
+ }
+ else
+ {
+ dc->SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
+ }
+
+ dc->SetPen( *wxTRANSPARENT_PEN );
+
+ return true;
+ }
+
+ return false;
+}
+
+void wxListLineData::Draw( wxDC *dc )
+{
+ wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
+ wxCHECK_RET( node, _T("no subitems at all??") );
+
+ bool highlighted = IsHighlighted();
+
+ wxListItemAttr *attr = GetAttr();
+
+ if ( SetAttributes(dc, attr, highlighted) )
+ {
+ dc->DrawRectangle( m_gi->m_rectHighlight );
+ }
+
+ // just for debugging to better see where the items are
+#if 0
+ dc->SetPen(*wxRED_PEN);
+ dc->SetBrush(*wxTRANSPARENT_BRUSH);
+ dc->DrawRectangle( m_gi->m_rectAll );
+ dc->SetPen(*wxGREEN_PEN);
+ dc->DrawRectangle( m_gi->m_rectIcon );
+#endif // 0
+
+ wxListItemData *item = node->GetData();
+ if (item->HasImage())
+ {
+ // centre the image inside our rectangle, this looks nicer when items
+ // ae aligned in a row
+ const wxRect& rectIcon = m_gi->m_rectIcon;
+
+ m_owner->DrawImage(item->GetImage(), dc, rectIcon.x, rectIcon.y);
+ }
+
+ if (item->HasText())
+ {
+ const wxRect& rectLabel = m_gi->m_rectLabel;
+
+ wxDCClipper clipper(*dc, rectLabel);
+ dc->DrawText(item->GetText(), rectLabel.x, rectLabel.y);
+ }
+}
+
+void wxListLineData::DrawInReportMode( wxDC *dc,
+ const wxRect& rect,
+ const wxRect& rectHL,
+ bool highlighted )
+{
+ // TODO: later we should support setting different attributes for
+ // different columns - to do it, just add "col" argument to
+ // GetAttr() and move these lines into the loop below
+ wxListItemAttr *attr = GetAttr();
+ if ( SetAttributes(dc, attr, highlighted) )
+ {
+ dc->DrawRectangle( rectHL );
+ }
+
+ wxCoord x = rect.x + HEADER_OFFSET_X,
+ y = rect.y + (LINE_SPACING + EXTRA_HEIGHT) / 2;
+
+ size_t col = 0;
+ for ( wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
+ node;
+ node = node->GetNext(), col++ )
+ {
+ wxListItemData *item = node->GetData();
+
+ int width = m_owner->GetColumnWidth(col);
+ int xOld = x;
+ x += width;
+
+ if ( item->HasImage() )
+ {
+ int ix, iy;
+ m_owner->DrawImage( item->GetImage(), dc, xOld, y );
+ m_owner->GetImageSize( item->GetImage(), ix, iy );
+
+ ix += IMAGE_MARGIN_IN_REPORT_MODE;
+
+ xOld += ix;
+ width -= ix;
+ }
+
+ wxDCClipper clipper(*dc, xOld, y, width - 8, rect.height);
+
+ if ( item->HasText() )
+ {
+ DrawTextFormatted(dc, item->GetText(), col, xOld, y, width - 8);
+ }
+ }
+}
+
+void wxListLineData::DrawTextFormatted(wxDC *dc,
+ const wxString &text,
+ int col,
+ int x,
+ int y,
+ int width)
+{
+ wxString drawntext, ellipsis;
+ wxCoord w, h, base_w;
+ wxListItem item;
+
+ // determine if the string can fit inside the current width
+ dc->GetTextExtent(text, &w, &h);
+ if (w <= width)
+ {
+ // it can, draw it using the items alignment
+ m_owner->GetColumn(col, item);
+ switch ( item.GetAlign() )
+ {
+ default:
+ wxFAIL_MSG( _T("unknown list item format") );
+ // fall through
+
+ case wxLIST_FORMAT_LEFT:
+ // nothing to do
+ break;
+
+ case wxLIST_FORMAT_RIGHT:
+ x += width - w;
+ break;
+
+ case wxLIST_FORMAT_CENTER:
+ x += (width - w) / 2;
+ break;
+ }
+
+ dc->DrawText(text, x, y);
+ }
+ else // otherwise, truncate and add an ellipsis if possible
+ {
+ // determine the base width
+ ellipsis = wxString(wxT("..."));
+ dc->GetTextExtent(ellipsis, &base_w, &h);
+
+ // continue until we have enough space or only one character left
+ wxCoord w_c, h_c;
+ size_t len = text.Length();
+ drawntext = text.Left(len);
+ while (len > 1)
+ {
+ dc->GetTextExtent(drawntext.Last(), &w_c, &h_c);
+ drawntext.RemoveLast();
+ len--;
+ w -= w_c;
+ if (w + base_w <= width)
+ break;
+ }
+
+ // if still not enough space, remove ellipsis characters
+ while (ellipsis.Length() > 0 && w + base_w > width)
+ {
+ ellipsis = ellipsis.Left(ellipsis.Length() - 1);
+ dc->GetTextExtent(ellipsis, &base_w, &h);
+ }
+
+ // now draw the text
+ dc->DrawText(drawntext, x, y);
+ dc->DrawText(ellipsis, x + w, y);
+ }
+}
+
+bool wxListLineData::Highlight( bool on )
+{
+ wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
+
+ if ( on == m_highlighted )
+ return false;
+
+ m_highlighted = on;
+
+ return true;
+}
+
+void wxListLineData::ReverseHighlight( void )
+{
+ Highlight(!IsHighlighted());
+}
+
+//-----------------------------------------------------------------------------
+// wxListHeaderWindow
+//-----------------------------------------------------------------------------
+
+IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow,wxWindow)
+
+BEGIN_EVENT_TABLE(wxListHeaderWindow,wxWindow)
+ EVT_PAINT (wxListHeaderWindow::OnPaint)
+ EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse)
+ EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus)
+END_EVENT_TABLE()
+
+void wxListHeaderWindow::Init()
+{
+ m_currentCursor = (wxCursor *) NULL;
+ m_isDragging = false;
+ m_dirty = false;
+}
+
+wxListHeaderWindow::wxListHeaderWindow()
+{
+ Init();
+
+ m_owner = (wxListMainWindow *) NULL;
+ m_resizeCursor = (wxCursor *) NULL;
+}
+
+wxListHeaderWindow::wxListHeaderWindow( wxWindow *win,
+ wxWindowID id,
+ wxListMainWindow *owner,
+ const wxPoint& pos,
+ const wxSize& size,
+ long style,
+ const wxString &name )
+ : wxWindow( win, id, pos, size, style, name )
+{
+ Init();
+
+ m_owner = owner;
+ m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
+
+#if _USE_VISATTR
+ wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes();
+ SetOwnForegroundColour( attr.colFg );
+ SetOwnBackgroundColour( attr.colBg );
+ if (!m_hasFont)
+ SetOwnFont( attr.font );
+#else
+ SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
+ SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
+ if (!m_hasFont)
+ SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT ));
+#endif
+}
+
+wxListHeaderWindow::~wxListHeaderWindow()
+{
+ delete m_resizeCursor;
+}
+
+#ifdef __WXUNIVERSAL__
+#include "wx/univ/renderer.h"
+#include "wx/univ/theme.h"
+#endif
+
+// shift the DC origin to match the position of the main window horz
+// scrollbar: this allows us to always use logical coords
+void wxListHeaderWindow::AdjustDC(wxDC& dc)
+{
+ int xpix;
+ m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
+
+ int x;
+ m_owner->GetViewStart( &x, NULL );
+
+ // account for the horz scrollbar offset
+ dc.SetDeviceOrigin( -x * xpix, 0 );
+}
+
+void wxListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
+{
+ wxPaintDC dc( this );
+
+ PrepareDC( dc );
+ AdjustDC( dc );
+
+ dc.BeginDrawing();
+
+ dc.SetFont( GetFont() );
+
+ // width and height of the entire header window
+ int w, h;
+ GetClientSize( &w, &h );
+ m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
+
+ dc.SetBackgroundMode(wxTRANSPARENT);
+
+ dc.SetTextForeground(GetForegroundColour());
+
+ int x = HEADER_OFFSET_X;
+
+ int numColumns = m_owner->GetColumnCount();
+ wxListItem item;
+ for ( int i = 0; i < numColumns && x < w; i++ )
+ {
+ m_owner->GetColumn( i, item );
+ int wCol = item.m_width;
+
+ // the width of the rect to draw: make it smaller to fit entirely
+ // inside the column rect
+ int cw = wCol - 2;
+
+ wxRendererNative::Get().DrawHeaderButton
+ (
+ this,
+ dc,
+ wxRect(x, HEADER_OFFSET_Y, cw, h - 2),
+ m_parent->IsEnabled() ? 0
+ : wxCONTROL_DISABLED
+ );
+
+ // see if we have enough space for the column label
+
+ // for this we need the width of the text
+ wxCoord wLabel;
+ wxCoord hLabel;
+ dc.GetTextExtent(item.GetText(), &wLabel, &hLabel);
+ wLabel += 2*EXTRA_WIDTH;
+
+ // and the width of the icon, if any
+ static const int MARGIN_BETWEEN_TEXT_AND_ICON = 2;
+ int ix = 0, // init them just to suppress the compiler warnings
+ iy = 0;
+ const int image = item.m_image;
+ wxImageListType *imageList;
+ if ( image != -1 )
+ {
+ imageList = m_owner->m_small_image_list;
+ if ( imageList )
+ {
+ imageList->GetSize(image, ix, iy);
+ wLabel += ix + MARGIN_BETWEEN_TEXT_AND_ICON;
+ }
+ }
+ else
+ {
+ imageList = NULL;
+ }
+
+ // ignore alignment if there is not enough space anyhow
+ int xAligned;
+ switch ( wLabel < cw ? item.GetAlign() : wxLIST_FORMAT_LEFT )
+ {
+ default:
+ wxFAIL_MSG( _T("unknown list item format") );
+ // fall through
+
+ case wxLIST_FORMAT_LEFT:
+ xAligned = x;
+ break;
+
+ case wxLIST_FORMAT_RIGHT:
+ xAligned = x + cw - wLabel;
+ break;
+
+ case wxLIST_FORMAT_CENTER:
+ xAligned = x + (cw - wLabel) / 2;
+ break;
+ }
+
+
+ // if we have an image, draw it on the right of the label
+ if ( imageList )
+ {
+ imageList->Draw
+ (
+ image,
+ dc,
+ xAligned + wLabel - ix - MARGIN_BETWEEN_TEXT_AND_ICON,
+ HEADER_OFFSET_Y + (h - 4 - iy)/2,
+ wxIMAGELIST_DRAW_TRANSPARENT
+ );
+
+ cw -= ix + MARGIN_BETWEEN_TEXT_AND_ICON;
+ }
+
+ // draw the text clipping it so that it doesn't overwrite the column
+ // boundary
+ wxDCClipper clipper(dc, x, HEADER_OFFSET_Y, cw, h - 4 );
+
+ dc.DrawText( item.GetText(),
+ xAligned + EXTRA_WIDTH, h / 2 - hLabel / 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
+
+ x += wCol;
+ }
+
+ dc.EndDrawing();
+}
+
+void wxListHeaderWindow::DrawCurrent()
+{
+ int x1 = m_currentX;
+ int y1 = 0;
+ m_owner->ClientToScreen( &x1, &y1 );
+
+ int x2 = m_currentX;
+ int y2 = 0;
+ m_owner->GetClientSize( NULL, &y2 );
+ m_owner->ClientToScreen( &x2, &y2 );
+
+ wxScreenDC dc;
+ dc.SetLogicalFunction( wxINVERT );
+ dc.SetPen( wxPen( *wxBLACK, 2, wxSOLID ) );
+ dc.SetBrush( *wxTRANSPARENT_BRUSH );
+
+ AdjustDC(dc);
+
+ dc.DrawLine( x1, y1, x2, y2 );
+
+ dc.SetLogicalFunction( wxCOPY );
+
+ dc.SetPen( wxNullPen );
+ dc.SetBrush( wxNullBrush );
+}
+
+void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
+{
+ // we want to work with logical coords
+ int x;
+ m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
+ int y = event.GetY();
+
+ if (m_isDragging)
+ {
+ SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING, event.GetPosition());
+
+ // we don't draw the line beyond our window, but we allow dragging it
+ // there
+ int w = 0;
+ GetClientSize( &w, NULL );
+ m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
+ w -= 6;
+
+ // erase the line if it was drawn
+ if ( m_currentX < w )
+ DrawCurrent();
+
+ if (event.ButtonUp())
+ {
+ ReleaseMouse();
+ m_isDragging = false;
+ m_dirty = true;
+ m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
+ SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG, event.GetPosition());
+ }
+ else
+ {
+ if (x > m_minX + 7)
+ m_currentX = x;
+ else
+ m_currentX = m_minX + 7;
+
+ // draw in the new location
+ if ( m_currentX < w )
+ DrawCurrent();
+ }
+ }
+ else // not dragging
+ {
+ m_minX = 0;
+ bool hit_border = false;
+
+ // end of the current column
+ int xpos = 0;
+
+ // find the column where this event occured
+ int col,
+ countCol = m_owner->GetColumnCount();
+ for (col = 0; col < countCol; col++)
+ {
+ xpos += m_owner->GetColumnWidth( col );
+ m_column = col;
+
+ if ( (abs(x-xpos) < 3) && (y < 22) )
+ {
+ // near the column border
+ hit_border = true;
+ break;
+ }
+
+ if ( x < xpos )
+ {
+ // inside the column
+ break;
+ }
+
+ m_minX = xpos;
+ }
+
+ if ( col == countCol )
+ m_column = -1;
+
+ if (event.LeftDown() || event.RightUp())
+ {
+ if (hit_border && event.LeftDown())
+ {
+ if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG,
+ event.GetPosition()) )
+ {
+ m_isDragging = true;
+ m_currentX = x;
+ CaptureMouse();
+ DrawCurrent();
+ }
+ //else: column resizing was vetoed by the user code
+ }
+ else // click on a column
+ {
+ SendListEvent( event.LeftDown()
+ ? wxEVT_COMMAND_LIST_COL_CLICK
+ : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK,
+ event.GetPosition());
+ }
+ }
+ else if (event.Moving())
+ {
+ bool setCursor;
+ if (hit_border)
+ {
+ setCursor = m_currentCursor == wxSTANDARD_CURSOR;
+ m_currentCursor = m_resizeCursor;
+ }
+ else
+ {
+ setCursor = m_currentCursor != wxSTANDARD_CURSOR;
+ m_currentCursor = wxSTANDARD_CURSOR;
+ }
+
+ if ( setCursor )
+ SetCursor(*m_currentCursor);
+ }
+ }
+}
+
+void wxListHeaderWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
+{
+ m_owner->SetFocus();
+ m_owner->Update();
+}
+
+bool wxListHeaderWindow::SendListEvent(wxEventType type, wxPoint pos)
+{
+ wxWindow *parent = GetParent();
+ wxListEvent le( type, parent->GetId() );
+ le.SetEventObject( parent );
+ le.m_pointDrag = pos;
+
+ // the position should be relative to the parent window, not
+ // this one for compatibility with MSW and common sense: the
+ // user code doesn't know anything at all about this header
+ // window, so why should it get positions relative to it?
+ le.m_pointDrag.y -= GetSize().y;
+
+ le.m_col = m_column;
+ return !parent->GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
+}
+
+//-----------------------------------------------------------------------------
+// wxListRenameTimer (internal)
+//-----------------------------------------------------------------------------
+
+wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
+{
+ m_owner = owner;
+}
+
+void wxListRenameTimer::Notify()
+{
+ m_owner->OnRenameTimer();
+}
+
+//-----------------------------------------------------------------------------
+// wxListTextCtrl (internal)
+//-----------------------------------------------------------------------------
+
+BEGIN_EVENT_TABLE(wxListTextCtrl,wxTextCtrl)
+ EVT_CHAR (wxListTextCtrl::OnChar)
+ EVT_KEY_UP (wxListTextCtrl::OnKeyUp)
+ EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus)
+END_EVENT_TABLE()
+
+wxListTextCtrl::wxListTextCtrl(wxListMainWindow *owner, size_t itemEdit)
+ : m_startValue(owner->GetItemText(itemEdit)),
+ m_itemEdited(itemEdit)
+{
+ m_owner = owner;
+ m_finished = false;
+
+ wxRect rectLabel = owner->GetLineLabelRect(itemEdit);
+
+ m_owner->CalcScrolledPosition(rectLabel.x, rectLabel.y,
+ &rectLabel.x, &rectLabel.y);
+
+ (void)Create(owner, wxID_ANY, m_startValue,
+ wxPoint(rectLabel.x-4,rectLabel.y-4),
+ wxSize(rectLabel.width+11,rectLabel.height+8));
+}
+
+void wxListTextCtrl::Finish()
+{
+ if ( !m_finished )
+ {
+ wxPendingDelete.Append(this);
+
+ m_finished = true;
+
+ m_owner->SetFocus();
+ }
+}
+
+bool wxListTextCtrl::AcceptChanges()
+{
+ const wxString value = GetValue();
+
+ if ( value == m_startValue )
+ {
+ // nothing changed, always accept
+ return true;
+ }
+
+ if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
+ {
+ // vetoed by the user
+ return false;
+ }
+
+ // accepted, do rename the item
+ m_owner->SetItemText(m_itemEdited, value);
+
+ return true;
+}
+
+void wxListTextCtrl::OnChar( wxKeyEvent &event )
+{
+ switch ( event.m_keyCode )
+ {
+ case WXK_RETURN:
+ if ( AcceptChanges() )
+ {
+ // Close the text control, changes were accepted
+ Finish();
+ }
+ // else do nothing, do not accept and do not close
+
+ break;
+
+ case WXK_ESCAPE:
+ Finish();
+ m_owner->OnRenameCancelled( m_itemEdited );
+ break;
+
+ default:
+ event.Skip();
+ }
+}
+
+void wxListTextCtrl::OnKeyUp( wxKeyEvent &event )
+{
+ if (m_finished)
+ {
+ event.Skip();
+ return;
+ }
+
+ // auto-grow the textctrl:
+ wxSize parentSize = m_owner->GetSize();
+ wxPoint myPos = GetPosition();
+ wxSize mySize = GetSize();
+ int sx, sy;
+ GetTextExtent(GetValue() + _T("MM"), &sx, &sy);
+ if (myPos.x + sx > parentSize.x)
+ sx = parentSize.x - myPos.x;
+ if (mySize.x > sx)
+ sx = mySize.x;
+ SetSize(sx, wxDefaultCoord);
+
+ event.Skip();
+}
+
+void wxListTextCtrl::OnKillFocus( wxFocusEvent &event )
+{
+ if ( !m_finished )
+ {
+ // We must finish regardless of success, otherwise we'll get
+ // focus problems:
+ Finish();
+
+ if ( !AcceptChanges() )
+ m_owner->OnRenameCancelled( m_itemEdited );
+ }
+
+ // We must let the native text control handle focus, too, otherwise
+ // it could have problems with the cursor (e.g., in wxGTK):
+ event.Skip();
+}