Upstream tarball 20080304
[amule.git] / src / extern / wxWidgets / listctrl.cpp
bloba33d132c85af38082eb2ebc979c8208de362a29f
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
6 // Id: $Id: listctrl.cpp 8201 2008-03-01 22:39:21Z xaignar $
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // TODO
13 // 1. we need to implement searching/sorting for virtual controls somehow
14 // 2. when changing selection the lines are refreshed twice
17 // For compilers that support precompilation, includes "wx.h".
18 #include <wx/wxprec.h>
20 #ifdef __BORLANDC__
21 #pragma hdrstop
22 #endif
24 #if wxUSE_LISTCTRL
26 #include "listctrl.h"
28 #ifndef WX_PRECOMP
29 #include <wx/scrolwin.h>
30 #include <wx/timer.h>
31 #include <wx/settings.h>
32 #include <wx/dynarray.h>
33 #include <wx/dcclient.h>
34 #include <wx/dcscreen.h>
35 #include <wx/math.h>
36 #endif
38 #include <wx/imaglist.h>
39 #include <wx/selstore.h>
40 #include <wx/renderer.h>
41 #include <wx/dcbuffer.h>
43 #ifdef __WXMAC__
44 #include <wx/mac/private.h>
45 #endif
48 // NOTE: If using the wxListBox visual attributes works everywhere then this can
49 // be removed, as well as the #else case below.
50 #define _USE_VISATTR 0
52 namespace MuleExtern {
54 // ----------------------------------------------------------------------------
55 // constants
56 // ----------------------------------------------------------------------------
58 // // the height of the header window (FIXME: should depend on its font!)
59 // static const int HEADER_HEIGHT = 23;
61 static const int SCROLL_UNIT_X = 15;
63 // the spacing between the lines (in report mode)
64 static const int LINE_SPACING = 0;
66 // extra margins around the text label
67 #ifdef __WXGTK__
68 static const int EXTRA_WIDTH = 6;
69 #else
70 static const int EXTRA_WIDTH = 4;
71 #endif
72 static const int EXTRA_HEIGHT = 4;
74 // margin between the window and the items
75 static const int EXTRA_BORDER_X = 2;
76 static const int EXTRA_BORDER_Y = 2;
78 // offset for the header window
79 static const int HEADER_OFFSET_X = 0;
80 static const int HEADER_OFFSET_Y = 0;
82 // margin between rows of icons in [small] icon view
83 static const int MARGIN_BETWEEN_ROWS = 6;
85 // when autosizing the columns, add some slack
86 static const int AUTOSIZE_COL_MARGIN = 10;
88 // default width for the header columns
89 static const int WIDTH_COL_DEFAULT = 80;
91 // the space between the image and the text in the report mode
92 static const int IMAGE_MARGIN_IN_REPORT_MODE = 5;
94 // the space between the image and the text in the report mode in header
95 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE = 2;
97 // ============================================================================
98 // private classes
99 // ============================================================================
101 //-----------------------------------------------------------------------------
102 // wxColWidthInfo (internal)
103 //-----------------------------------------------------------------------------
105 struct wxColWidthInfo
107 int nMaxWidth;
108 bool bNeedsUpdate; // only set to true when an item whose
109 // width == nMaxWidth is removed
111 wxColWidthInfo(int w = 0, bool needsUpdate = false)
113 nMaxWidth = w;
114 bNeedsUpdate = needsUpdate;
118 WX_DEFINE_ARRAY_PTR(wxColWidthInfo *, ColWidthArray);
120 //-----------------------------------------------------------------------------
121 // wxListItemData (internal)
122 //-----------------------------------------------------------------------------
124 class wxListItemData
126 public:
127 wxListItemData(wxListMainWindow *owner);
128 ~wxListItemData();
130 void SetItem( const wxListItem &info );
131 void SetImage( int image ) { m_image = image; }
132 void SetData( wxUIntPtr data ) { m_data = data; }
133 void SetPosition( int x, int y );
134 void SetSize( int width, int height );
136 bool HasText() const { return !m_text.empty(); }
137 const wxString& GetText() const { return m_text; }
138 void SetText(const wxString& text) { m_text = text; }
140 // we can't use empty string for measuring the string width/height, so
141 // always return something
142 wxString GetTextForMeasuring() const
144 wxString s = GetText();
145 if ( s.empty() )
146 s = _T('H');
148 return s;
151 bool IsHit( int x, int y ) const;
153 int GetX() const;
154 int GetY() const;
155 int GetWidth() const;
156 int GetHeight() const;
158 int GetImage() const { return m_image; }
159 bool HasImage() const { return GetImage() != -1; }
161 void GetItem( wxListItem &info ) const;
163 void SetAttr(wxListItemAttr *attr) { m_attr = attr; }
164 wxListItemAttr *GetAttr() const { return m_attr; }
166 public:
167 // the item image or -1
168 int m_image;
170 // user data associated with the item
171 wxUIntPtr m_data;
173 // the item coordinates are not used in report mode; instead this pointer is
174 // NULL and the owner window is used to retrieve the item position and size
175 wxRect *m_rect;
177 // the list ctrl we are in
178 wxListMainWindow *m_owner;
180 // custom attributes or NULL
181 wxListItemAttr *m_attr;
183 protected:
184 // common part of all ctors
185 void Init();
187 wxString m_text;
190 //-----------------------------------------------------------------------------
191 // wxListHeaderData (internal)
192 //-----------------------------------------------------------------------------
194 class wxListHeaderData : public wxObject
196 public:
197 wxListHeaderData();
198 wxListHeaderData( const wxListItem &info );
199 void SetItem( const wxListItem &item );
200 void SetPosition( int x, int y );
201 void SetWidth( int w );
202 void SetState( int state );
203 void SetFormat( int format );
204 void SetHeight( int h );
205 bool HasImage() const;
207 bool HasText() const { return !m_text.empty(); }
208 const wxString& GetText() const { return m_text; }
209 void SetText(const wxString& text) { m_text = text; }
211 void GetItem( wxListItem &item );
213 bool IsHit( int x, int y ) const;
214 int GetImage() const;
215 int GetWidth() const;
216 int GetFormat() const;
217 int GetState() const;
219 protected:
220 long m_mask;
221 int m_image;
222 wxString m_text;
223 int m_format;
224 int m_width;
225 int m_xpos,
226 m_ypos;
227 int m_height;
228 int m_state;
230 private:
231 void Init();
234 //-----------------------------------------------------------------------------
235 // wxListLineData (internal)
236 //-----------------------------------------------------------------------------
238 WX_DECLARE_EXPORTED_LIST(wxListItemData, wxListItemDataList);
239 #include <wx/listimpl.cpp>
240 WX_DEFINE_LIST(wxListItemDataList)
242 class wxListLineData
244 public:
245 // the list of subitems: only may have more than one item in report mode
246 wxListItemDataList m_items;
248 // this is not used in report view
249 struct GeometryInfo
251 // total item rect
252 wxRect m_rectAll;
254 // label only
255 wxRect m_rectLabel;
257 // icon only
258 wxRect m_rectIcon;
260 // the part to be highlighted
261 wxRect m_rectHighlight;
263 // extend all our rects to be centered inside the one of given width
264 void ExtendWidth(wxCoord w)
266 wxASSERT_MSG( m_rectAll.width <= w,
267 _T("width can only be increased") );
269 m_rectAll.width = w;
270 m_rectLabel.x = m_rectAll.x + (w - m_rectLabel.width) / 2;
271 m_rectIcon.x = m_rectAll.x + (w - m_rectIcon.width) / 2;
272 m_rectHighlight.x = m_rectAll.x + (w - m_rectHighlight.width) / 2;
275 *m_gi;
277 // is this item selected? [NB: not used in virtual mode]
278 bool m_highlighted;
280 // back pointer to the list ctrl
281 wxListMainWindow *m_owner;
283 public:
284 wxListLineData(wxListMainWindow *owner);
286 ~wxListLineData()
288 WX_CLEAR_LIST(wxListItemDataList, m_items);
289 delete m_gi;
292 // are we in report mode?
293 inline bool InReportView() const;
295 // are we in virtual report mode?
296 inline bool IsVirtual() const;
298 // these 2 methods shouldn't be called for report view controls, in that
299 // case we determine our position/size ourselves
301 // calculate the size of the line
302 void CalculateSize( wxDC *dc, int spacing );
304 // remember the position this line appears at
305 void SetPosition( int x, int y, int spacing );
307 // wxListCtrl API
309 void SetImage( int image ) { SetImage(0, image); }
310 int GetImage() const { return GetImage(0); }
311 void SetImage( int index, int image );
312 int GetImage( int index ) const;
314 bool HasImage() const { return GetImage() != -1; }
315 bool HasText() const { return !GetText(0).empty(); }
317 void SetItem( int index, const wxListItem &info );
318 void GetItem( int index, wxListItem &info );
320 wxString GetText(int index) const;
321 void SetText( int index, const wxString& s );
323 wxListItemAttr *GetAttr() const;
324 void SetAttr(wxListItemAttr *attr);
326 // return true if the highlighting really changed
327 bool Highlight( bool on );
329 void ReverseHighlight();
331 bool IsHighlighted() const
333 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
335 return m_highlighted;
338 // draw the line on the given DC in icon/list mode
339 void Draw( wxDC *dc );
341 // the same in report mode
342 void DrawInReportMode( wxDC *dc,
343 const wxRect& rect,
344 const wxRect& rectHL,
345 bool highlighted );
347 private:
348 // set the line to contain num items (only can be > 1 in report mode)
349 void InitItems( int num );
351 // get the mode (i.e. style) of the list control
352 inline int GetMode() const;
354 // prepare the DC for drawing with these item's attributes, return true if
355 // we need to draw the items background to highlight it, false otherwise
356 bool SetAttributes(wxDC *dc,
357 const wxListItemAttr *attr,
358 bool highlight);
360 // draw the text on the DC with the correct justification; also add an
361 // ellipsis if the text is too large to fit in the current width
362 void DrawTextFormatted(wxDC *dc,
363 const wxString &text,
364 int col,
365 int x,
366 int yMid, // this is middle, not top, of the text
367 int width);
370 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData, wxListLineDataArray);
371 #include <wx/arrimpl.cpp>
372 WX_DEFINE_OBJARRAY(wxListLineDataArray)
374 //-----------------------------------------------------------------------------
375 // wxListHeaderWindow (internal)
376 //-----------------------------------------------------------------------------
378 class wxListHeaderWindow : public wxWindow
380 protected:
381 wxListMainWindow *m_owner;
382 const wxCursor *m_currentCursor;
383 wxCursor *m_resizeCursor;
384 bool m_isDragging;
386 // column being resized or -1
387 int m_column;
389 // divider line position in logical (unscrolled) coords
390 int m_currentX;
392 // minimal position beyond which the divider line
393 // can't be dragged in logical coords
394 int m_minX;
396 public:
397 wxListHeaderWindow();
399 wxListHeaderWindow( wxWindow *win,
400 wxWindowID id,
401 wxListMainWindow *owner,
402 const wxPoint &pos = wxDefaultPosition,
403 const wxSize &size = wxDefaultSize,
404 long style = 0,
405 const wxString &name = wxT("wxlistctrlcolumntitles") );
407 virtual ~wxListHeaderWindow();
409 void DrawCurrent();
410 void AdjustDC( wxDC& dc );
412 void OnPaint( wxPaintEvent &event );
413 void OnMouse( wxMouseEvent &event );
414 void OnSetFocus( wxFocusEvent &event );
416 // needs refresh
417 bool m_dirty;
419 private:
420 // common part of all ctors
421 void Init();
423 // generate and process the list event of the given type, return true if
424 // it wasn't vetoed, i.e. if we should proceed
425 bool SendListEvent(wxEventType type, const wxPoint& pos);
427 DECLARE_EVENT_TABLE()
430 //-----------------------------------------------------------------------------
431 // wxListRenameTimer (internal)
432 //-----------------------------------------------------------------------------
434 class wxListRenameTimer: public wxTimer
436 private:
437 wxListMainWindow *m_owner;
439 public:
440 wxListRenameTimer( wxListMainWindow *owner );
441 void Notify();
444 //-----------------------------------------------------------------------------
445 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
446 //-----------------------------------------------------------------------------
448 class wxListTextCtrlWrapper : public wxEvtHandler
450 public:
451 // NB: text must be a valid object but not Create()d yet
452 wxListTextCtrlWrapper(wxListMainWindow *owner,
453 wxTextCtrl *text,
454 size_t itemEdit);
456 wxTextCtrl *GetText() const { return m_text; }
458 void AcceptChangesAndFinish();
460 protected:
461 void OnChar( wxKeyEvent &event );
462 void OnKeyUp( wxKeyEvent &event );
463 void OnKillFocus( wxFocusEvent &event );
465 bool AcceptChanges();
466 void Finish();
468 private:
469 wxListMainWindow *m_owner;
470 wxTextCtrl *m_text;
471 wxString m_startValue;
472 size_t m_itemEdited;
473 bool m_finished;
474 bool m_aboutToFinish;
476 DECLARE_EVENT_TABLE()
479 //-----------------------------------------------------------------------------
480 // wxListMainWindow (internal)
481 //-----------------------------------------------------------------------------
483 WX_DECLARE_EXPORTED_LIST(wxListHeaderData, wxListHeaderDataList);
484 #include <wx/listimpl.cpp>
485 WX_DEFINE_LIST(wxListHeaderDataList)
487 class wxListMainWindow : public wxScrolledWindow
489 public:
490 wxListMainWindow();
491 wxListMainWindow( wxWindow *parent,
492 wxWindowID id,
493 const wxPoint& pos = wxDefaultPosition,
494 const wxSize& size = wxDefaultSize,
495 long style = 0,
496 const wxString &name = _T("listctrlmainwindow") );
498 virtual ~wxListMainWindow();
500 bool HasFlag(int flag) const { return m_parent->HasFlag(flag); }
502 // return true if this is a virtual list control
503 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL); }
505 // return true if the control is in report mode
506 bool InReportView() const { return HasFlag(wxLC_REPORT); }
508 // return true if we are in single selection mode, false if multi sel
509 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL); }
511 // do we have a header window?
512 bool HasHeader() const
513 { return InReportView() && !HasFlag(wxLC_NO_HEADER); }
515 void HighlightAll( bool on );
517 // all these functions only do something if the line is currently visible
519 // change the line "selected" state, return true if it really changed
520 bool HighlightLine( size_t line, bool highlight = true);
522 // as HighlightLine() but do it for the range of lines: this is incredibly
523 // more efficient for virtual list controls!
525 // NB: unlike HighlightLine() this one does refresh the lines on screen
526 void HighlightLines( size_t lineFrom, size_t lineTo, bool on = true );
528 // toggle the line state and refresh it
529 void ReverseHighlight( size_t line )
530 { HighlightLine(line, !IsHighlighted(line)); RefreshLine(line); }
532 // return true if the line is highlighted
533 bool IsHighlighted(size_t line) const;
535 // refresh one or several lines at once
536 void RefreshLine( size_t line );
537 void RefreshLines( size_t lineFrom, size_t lineTo );
539 // refresh all selected items
540 void RefreshSelected();
542 // refresh all lines below the given one: the difference with
543 // RefreshLines() is that the index here might not be a valid one (happens
544 // when the last line is deleted)
545 void RefreshAfter( size_t lineFrom );
547 // the methods which are forwarded to wxListLineData itself in list/icon
548 // modes but are here because the lines don't store their positions in the
549 // report mode
551 // get the bound rect for the entire line
552 wxRect GetLineRect(size_t line) const;
554 // get the bound rect of the label
555 wxRect GetLineLabelRect(size_t line) const;
557 // get the bound rect of the items icon (only may be called if we do have
558 // an icon!)
559 wxRect GetLineIconRect(size_t line) const;
561 // get the rect to be highlighted when the item has focus
562 wxRect GetLineHighlightRect(size_t line) const;
564 // get the size of the total line rect
565 wxSize GetLineSize(size_t line) const
566 { return GetLineRect(line).GetSize(); }
568 // return the hit code for the corresponding position (in this line)
569 long HitTestLine(size_t line, int x, int y) const;
571 // bring the selected item into view, scrolling to it if necessary
572 void MoveToItem(size_t item);
574 // bring the current item into view
575 void MoveToFocus() { MoveToItem(m_current); }
577 // start editing the label of the given item
578 wxTextCtrl *EditLabel(long item,
579 wxClassInfo* textControlClass = CLASSINFO(wxTextCtrl));
580 wxTextCtrl *GetEditControl() const
582 return m_textctrlWrapper ? m_textctrlWrapper->GetText() : NULL;
585 void FinishEditing(wxTextCtrl *text)
587 delete text;
588 m_textctrlWrapper = NULL;
589 SetFocusIgnoringChildren();
592 // suspend/resume redrawing the control
593 void Freeze();
594 void Thaw();
596 void OnRenameTimer();
597 bool OnRenameAccept(size_t itemEdit, const wxString& value);
598 void OnRenameCancelled(size_t itemEdit);
600 void OnMouse( wxMouseEvent &event );
602 // called to switch the selection from the current item to newCurrent,
603 void OnArrowChar( size_t newCurrent, const wxKeyEvent& event );
605 void OnChar( wxKeyEvent &event );
606 void OnKeyDown( wxKeyEvent &event );
607 void OnKeyUp( wxKeyEvent &event );
608 void OnSetFocus( wxFocusEvent &event );
609 void OnKillFocus( wxFocusEvent &event );
610 void OnScroll( wxScrollWinEvent& event );
612 void OnPaint( wxPaintEvent &event );
613 void OnErase( wxEraseEvent& event ) {
614 // This is needed to avoid garbage on empty lists.
615 if ( IsEmpty() ) {
616 event.Skip();
620 void DrawImage( int index, wxDC *dc, int x, int y );
621 void GetImageSize( int index, int &width, int &height ) const;
622 int GetTextLength( const wxString &s ) const;
624 void SetImageList( wxImageList *imageList, int which );
625 void SetItemSpacing( int spacing, bool isSmall = false );
626 int GetItemSpacing( bool isSmall = false );
628 void SetColumn( int col, wxListItem &item );
629 void SetColumnWidth( int col, int width );
630 void GetColumn( int col, wxListItem &item ) const;
631 int GetColumnWidth( int col ) const;
632 int GetColumnCount() const { return m_columns.GetCount(); }
634 // returns the sum of the heights of all columns
635 int GetHeaderWidth() const;
637 int GetCountPerPage() const;
639 void SetItem( wxListItem &item );
640 void GetItem( wxListItem &item ) const;
641 void SetItemState( long item, long state, long stateMask );
642 void SetItemStateAll( long state, long stateMask );
643 int GetItemState( long item, long stateMask ) const;
644 void GetItemRect( long index, wxRect &rect ) const;
645 wxRect GetViewRect() const;
646 bool GetItemPosition( long item, wxPoint& pos ) const;
647 int GetSelectedItemCount() const;
649 wxString GetItemText(long item) const
651 wxListItem info;
652 info.m_mask = wxLIST_MASK_TEXT;
653 info.m_itemId = item;
654 GetItem( info );
655 return info.m_text;
658 void SetItemText(long item, const wxString& value)
660 wxListItem info;
661 info.m_mask = wxLIST_MASK_TEXT;
662 info.m_itemId = item;
663 info.m_text = value;
664 SetItem( info );
667 // set the scrollbars and update the positions of the items
668 void RecalculatePositions(bool noRefresh = false);
670 // refresh the window and the header
671 void RefreshAll();
673 long GetNextItem( long item, int geometry, int state ) const;
674 void DeleteItem( long index );
675 void DeleteAllItems();
676 void DeleteColumn( int col );
677 void DeleteEverything();
678 void EnsureVisible( long index );
679 long FindItem( long start, const wxString& str, bool partial = false );
680 long FindItem( long start, wxUIntPtr data);
681 long FindItem( const wxPoint& pt );
682 long HitTest( int x, int y, int &flags ) const;
683 void InsertItem( wxListItem &item );
684 void InsertColumn( long col, wxListItem &item );
685 int GetItemWidthWithImage(wxListItem * item);
686 void SortItems( MuleListCtrlCompare fn, long data );
688 size_t GetItemCount() const;
689 bool IsEmpty() const { return GetItemCount() == 0; }
690 void SetItemCount(long count);
692 // change the current (== focused) item, send a notification event
693 void ChangeCurrent(size_t current);
694 void ResetCurrent() { ChangeCurrent((size_t)-1); }
695 bool HasCurrent() const { return m_current != (size_t)-1; }
697 // send out a wxListEvent
698 void SendNotify( size_t line,
699 wxEventType command,
700 const wxPoint& point = wxDefaultPosition );
702 // override base class virtual to reset m_lineHeight when the font changes
703 virtual bool SetFont(const wxFont& font)
705 if ( !wxScrolledWindow::SetFont(font) )
706 return false;
708 m_lineHeight = 0;
710 return true;
713 // these are for wxListLineData usage only
715 // get the backpointer to the list ctrl
716 wxGenericListCtrl *GetListCtrl() const
718 return wxStaticCast(GetParent(), wxGenericListCtrl);
721 // get the height of all lines (assuming they all do have the same height)
722 wxCoord GetLineHeight() const;
724 // get the y position of the given line (only for report view)
725 wxCoord GetLineY(size_t line) const;
727 // get the brush to use for the item highlighting
728 wxBrush *GetHighlightBrush() const
730 return m_hasFocus ? m_highlightBrush : m_highlightUnfocusedBrush;
733 bool HasFocus() const
735 return m_hasFocus;
738 //protected:
739 // the array of all line objects for a non virtual list control (for the
740 // virtual list control we only ever use m_lines[0])
741 wxListLineDataArray m_lines;
743 // the list of column objects
744 wxListHeaderDataList m_columns;
746 // currently focused item or -1
747 size_t m_current;
749 // the number of lines per page
750 int m_linesPerPage;
752 // this flag is set when something which should result in the window
753 // redrawing happens (i.e. an item was added or deleted, or its appearance
754 // changed) and OnPaint() doesn't redraw the window while it is set which
755 // allows to minimize the number of repaintings when a lot of items are
756 // being added. The real repainting occurs only after the next OnIdle()
757 // call
758 bool m_dirty;
760 wxColour *m_highlightColour;
761 wxImageList *m_small_image_list;
762 wxImageList *m_normal_image_list;
763 int m_small_spacing;
764 int m_normal_spacing;
765 bool m_hasFocus;
767 bool m_lastOnSame;
768 wxTimer *m_renameTimer;
769 bool m_isCreated;
770 int m_dragCount;
771 wxPoint m_dragStart;
772 ColWidthArray m_aColWidths;
774 // for double click logic
775 size_t m_lineLastClicked,
776 m_lineBeforeLastClicked,
777 m_lineSelectSingleOnUp;
779 protected:
780 wxWindow *GetMainWindowOfCompositeControl() { return GetParent(); }
782 // the total count of items in a virtual list control
783 size_t m_countVirt;
785 // the object maintaining the items selection state, only used in virtual
786 // controls
787 wxSelectionStore m_selStore;
789 // common part of all ctors
790 void Init();
792 // get the line data for the given index
793 wxListLineData *GetLine(size_t n) const
795 wxASSERT_MSG( n != (size_t)-1, _T("invalid line index") );
797 if ( IsVirtual() )
799 wxConstCast(this, wxListMainWindow)->CacheLineData(n);
800 n = 0;
803 return &m_lines[n];
806 // get a dummy line which can be used for geometry calculations and such:
807 // you must use GetLine() if you want to really draw the line
808 wxListLineData *GetDummyLine() const;
810 // cache the line data of the n-th line in m_lines[0]
811 void CacheLineData(size_t line);
813 // get the range of visible lines
814 void GetVisibleLinesRange(size_t *from, size_t *to);
816 // force us to recalculate the range of visible lines
817 void ResetVisibleLinesRange() { m_lineFrom = (size_t)-1; }
819 // get the colour to be used for drawing the rules
820 wxColour GetRuleColour() const
822 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
825 private:
826 // initialize the current item if needed
827 void UpdateCurrent();
829 // delete all items but don't refresh: called from dtor
830 void DoDeleteAllItems();
832 // the height of one line using the current font
833 wxCoord m_lineHeight;
835 // the total header width or 0 if not calculated yet
836 wxCoord m_headerWidth;
838 // the first and last lines being shown on screen right now (inclusive),
839 // both may be -1 if they must be calculated so never access them directly:
840 // use GetVisibleLinesRange() above instead
841 size_t m_lineFrom,
842 m_lineTo;
844 // the brushes to use for item highlighting when we do/don't have focus
845 wxBrush *m_highlightBrush,
846 *m_highlightUnfocusedBrush;
848 // if this is > 0, the control is frozen and doesn't redraw itself
849 size_t m_freezeCount;
851 // wrapper around the text control currently used for in place editing or
852 // NULL if no item is being edited
853 wxListTextCtrlWrapper *m_textctrlWrapper;
856 DECLARE_EVENT_TABLE()
858 friend class wxGenericListCtrl;
862 wxListItemData::~wxListItemData()
864 // in the virtual list control the attributes are managed by the main
865 // program, so don't delete them
866 if ( !m_owner->IsVirtual() )
867 delete m_attr;
869 delete m_rect;
872 void wxListItemData::Init()
874 m_image = -1;
875 m_data = 0;
877 m_attr = NULL;
880 wxListItemData::wxListItemData(wxListMainWindow *owner)
882 Init();
884 m_owner = owner;
886 if ( owner->InReportView() )
887 m_rect = NULL;
888 else
889 m_rect = new wxRect;
892 void wxListItemData::SetItem( const wxListItem &info )
894 if ( info.m_mask & wxLIST_MASK_TEXT )
895 SetText(info.m_text);
896 if ( info.m_mask & wxLIST_MASK_IMAGE )
897 m_image = info.m_image;
898 if ( info.m_mask & wxLIST_MASK_DATA )
899 m_data = info.m_data;
901 if ( info.HasAttributes() )
903 if ( m_attr )
904 m_attr->AssignFrom(*info.GetAttributes());
905 else
906 m_attr = new wxListItemAttr(*info.GetAttributes());
909 if ( m_rect )
911 m_rect->x =
912 m_rect->y =
913 m_rect->height = 0;
914 m_rect->width = info.m_width;
918 void wxListItemData::SetPosition( int x, int y )
920 wxCHECK_RET( m_rect, _T("unexpected SetPosition() call") );
922 m_rect->x = x;
923 m_rect->y = y;
926 void wxListItemData::SetSize( int width, int height )
928 wxCHECK_RET( m_rect, _T("unexpected SetSize() call") );
930 if ( width != -1 )
931 m_rect->width = width;
932 if ( height != -1 )
933 m_rect->height = height;
936 bool wxListItemData::IsHit( int x, int y ) const
938 wxCHECK_MSG( m_rect, false, _T("can't be called in this mode") );
940 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x, y);
943 int wxListItemData::GetX() const
945 wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
947 return m_rect->x;
950 int wxListItemData::GetY() const
952 wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
954 return m_rect->y;
957 int wxListItemData::GetWidth() const
959 wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
961 return m_rect->width;
964 int wxListItemData::GetHeight() const
966 wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
968 return m_rect->height;
971 void wxListItemData::GetItem( wxListItem &info ) const
973 long mask = info.m_mask;
974 if ( !mask )
975 // by default, get everything for backwards compatibility
976 mask = -1;
978 if ( mask & wxLIST_MASK_TEXT )
979 info.m_text = m_text;
980 if ( mask & wxLIST_MASK_IMAGE )
981 info.m_image = m_image;
982 if ( mask & wxLIST_MASK_DATA )
983 info.m_data = m_data;
985 if ( m_attr )
987 if ( m_attr->HasTextColour() )
988 info.SetTextColour(m_attr->GetTextColour());
989 if ( m_attr->HasBackgroundColour() )
990 info.SetBackgroundColour(m_attr->GetBackgroundColour());
991 if ( m_attr->HasFont() )
992 info.SetFont(m_attr->GetFont());
996 //-----------------------------------------------------------------------------
997 // wxListHeaderData
998 //-----------------------------------------------------------------------------
1000 void wxListHeaderData::Init()
1002 m_mask = 0;
1003 m_image = -1;
1004 m_format = 0;
1005 m_width = 0;
1006 m_xpos = 0;
1007 m_ypos = 0;
1008 m_height = 0;
1009 m_state = 0;
1012 wxListHeaderData::wxListHeaderData()
1014 Init();
1017 wxListHeaderData::wxListHeaderData( const wxListItem &item )
1019 Init();
1021 SetItem( item );
1024 void wxListHeaderData::SetItem( const wxListItem &item )
1026 m_mask = item.m_mask;
1028 if ( m_mask & wxLIST_MASK_TEXT )
1029 m_text = item.m_text;
1031 if ( m_mask & wxLIST_MASK_IMAGE )
1032 m_image = item.m_image;
1034 if ( m_mask & wxLIST_MASK_FORMAT )
1035 m_format = item.m_format;
1037 if ( m_mask & wxLIST_MASK_WIDTH )
1038 SetWidth(item.m_width);
1040 if ( m_mask & wxLIST_MASK_STATE )
1041 SetState(item.m_state);
1044 void wxListHeaderData::SetPosition( int x, int y )
1046 m_xpos = x;
1047 m_ypos = y;
1050 void wxListHeaderData::SetHeight( int h )
1052 m_height = h;
1055 void wxListHeaderData::SetWidth( int w )
1057 m_width = w < 0 ? WIDTH_COL_DEFAULT : w;
1060 void wxListHeaderData::SetState( int flag )
1062 m_state = flag;
1065 void wxListHeaderData::SetFormat( int format )
1067 m_format = format;
1070 bool wxListHeaderData::HasImage() const
1072 return m_image != -1;
1075 bool wxListHeaderData::IsHit( int x, int y ) const
1077 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
1080 void wxListHeaderData::GetItem( wxListItem& item )
1082 item.m_mask = m_mask;
1083 item.m_text = m_text;
1084 item.m_image = m_image;
1085 item.m_format = m_format;
1086 item.m_width = m_width;
1087 item.m_state = m_state;
1090 int wxListHeaderData::GetImage() const
1092 return m_image;
1095 int wxListHeaderData::GetWidth() const
1097 return m_width;
1100 int wxListHeaderData::GetFormat() const
1102 return m_format;
1105 int wxListHeaderData::GetState() const
1107 return m_state;
1110 //-----------------------------------------------------------------------------
1111 // wxListLineData
1112 //-----------------------------------------------------------------------------
1114 inline int wxListLineData::GetMode() const
1116 return m_owner->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE;
1119 inline bool wxListLineData::InReportView() const
1121 return m_owner->HasFlag(wxLC_REPORT);
1124 inline bool wxListLineData::IsVirtual() const
1126 return m_owner->IsVirtual();
1129 wxListLineData::wxListLineData( wxListMainWindow *owner )
1131 m_owner = owner;
1133 if ( InReportView() )
1134 m_gi = NULL;
1135 else // !report
1136 m_gi = new GeometryInfo;
1138 m_highlighted = false;
1140 InitItems( GetMode() == wxLC_REPORT ? m_owner->GetColumnCount() : 1 );
1143 void wxListLineData::CalculateSize( wxDC *dc, int spacing )
1145 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1146 wxCHECK_RET( node, _T("no subitems at all??") );
1148 wxListItemData *item = node->GetData();
1150 wxString s;
1151 wxCoord lw, lh;
1153 switch ( GetMode() )
1155 case wxLC_ICON:
1156 case wxLC_SMALL_ICON:
1157 m_gi->m_rectAll.width = spacing;
1159 s = item->GetText();
1161 if ( s.empty() )
1163 lh =
1164 m_gi->m_rectLabel.width =
1165 m_gi->m_rectLabel.height = 0;
1167 else // has label
1169 dc->GetTextExtent( s, &lw, &lh );
1170 lw += EXTRA_WIDTH;
1171 lh += EXTRA_HEIGHT;
1173 m_gi->m_rectAll.height = spacing + lh;
1174 if (lw > spacing)
1175 m_gi->m_rectAll.width = lw;
1177 m_gi->m_rectLabel.width = lw;
1178 m_gi->m_rectLabel.height = lh;
1181 if (item->HasImage())
1183 int w, h;
1184 m_owner->GetImageSize( item->GetImage(), w, h );
1185 m_gi->m_rectIcon.width = w + 8;
1186 m_gi->m_rectIcon.height = h + 8;
1188 if ( m_gi->m_rectIcon.width > m_gi->m_rectAll.width )
1189 m_gi->m_rectAll.width = m_gi->m_rectIcon.width;
1190 if ( m_gi->m_rectIcon.height + lh > m_gi->m_rectAll.height - 4 )
1191 m_gi->m_rectAll.height = m_gi->m_rectIcon.height + lh + 4;
1194 if ( item->HasText() )
1196 m_gi->m_rectHighlight.width = m_gi->m_rectLabel.width;
1197 m_gi->m_rectHighlight.height = m_gi->m_rectLabel.height;
1199 else // no text, highlight the icon
1201 m_gi->m_rectHighlight.width = m_gi->m_rectIcon.width;
1202 m_gi->m_rectHighlight.height = m_gi->m_rectIcon.height;
1204 break;
1206 case wxLC_LIST:
1207 s = item->GetTextForMeasuring();
1209 dc->GetTextExtent( s, &lw, &lh );
1210 lw += EXTRA_WIDTH;
1211 lh += EXTRA_HEIGHT;
1213 m_gi->m_rectLabel.width = lw;
1214 m_gi->m_rectLabel.height = lh;
1216 m_gi->m_rectAll.width = lw;
1217 m_gi->m_rectAll.height = lh;
1219 if (item->HasImage())
1221 int w, h;
1222 m_owner->GetImageSize( item->GetImage(), w, h );
1223 m_gi->m_rectIcon.width = w;
1224 m_gi->m_rectIcon.height = h;
1226 m_gi->m_rectAll.width += 4 + w;
1227 if (h > m_gi->m_rectAll.height)
1228 m_gi->m_rectAll.height = h;
1231 m_gi->m_rectHighlight.width = m_gi->m_rectAll.width;
1232 m_gi->m_rectHighlight.height = m_gi->m_rectAll.height;
1233 break;
1235 case wxLC_REPORT:
1236 wxFAIL_MSG( _T("unexpected call to SetSize") );
1237 break;
1239 default:
1240 wxFAIL_MSG( _T("unknown mode") );
1241 break;
1245 void wxListLineData::SetPosition( int x, int y, int spacing )
1247 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1248 wxCHECK_RET( node, _T("no subitems at all??") );
1250 wxListItemData *item = node->GetData();
1252 switch ( GetMode() )
1254 case wxLC_ICON:
1255 case wxLC_SMALL_ICON:
1256 m_gi->m_rectAll.x = x;
1257 m_gi->m_rectAll.y = y;
1259 if ( item->HasImage() )
1261 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 4 +
1262 (m_gi->m_rectAll.width - m_gi->m_rectIcon.width) / 2;
1263 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 4;
1266 if ( item->HasText() )
1268 if (m_gi->m_rectAll.width > spacing)
1269 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
1270 else
1271 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2) + (spacing / 2) - (m_gi->m_rectLabel.width / 2);
1272 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + m_gi->m_rectAll.height + 2 - m_gi->m_rectLabel.height;
1273 m_gi->m_rectHighlight.x = m_gi->m_rectLabel.x - 2;
1274 m_gi->m_rectHighlight.y = m_gi->m_rectLabel.y - 2;
1276 else // no text, highlight the icon
1278 m_gi->m_rectHighlight.x = m_gi->m_rectIcon.x - 4;
1279 m_gi->m_rectHighlight.y = m_gi->m_rectIcon.y - 4;
1281 break;
1283 case wxLC_LIST:
1284 m_gi->m_rectAll.x = x;
1285 m_gi->m_rectAll.y = y;
1287 m_gi->m_rectHighlight.x = m_gi->m_rectAll.x;
1288 m_gi->m_rectHighlight.y = m_gi->m_rectAll.y;
1289 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + 2;
1291 if (item->HasImage())
1293 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 2;
1294 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 2;
1295 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 4 + (EXTRA_WIDTH/2) + m_gi->m_rectIcon.width;
1297 else
1299 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
1301 break;
1303 case wxLC_REPORT:
1304 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1305 break;
1307 default:
1308 wxFAIL_MSG( _T("unknown mode") );
1309 break;
1313 void wxListLineData::InitItems( int num )
1315 for (int i = 0; i < num; i++)
1316 m_items.Append( new wxListItemData(m_owner) );
1319 void wxListLineData::SetItem( int index, const wxListItem &info )
1321 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1322 wxCHECK_RET( node, _T("invalid column index in SetItem") );
1324 wxListItemData *item = node->GetData();
1325 item->SetItem( info );
1328 void wxListLineData::GetItem( int index, wxListItem &info )
1330 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1331 if (node)
1333 wxListItemData *item = node->GetData();
1334 item->GetItem( info );
1338 wxString wxListLineData::GetText(int index) const
1340 wxString s;
1342 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1343 if (node)
1345 wxListItemData *item = node->GetData();
1346 s = item->GetText();
1349 return s;
1352 void wxListLineData::SetText( int index, const wxString& s )
1354 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1355 if (node)
1357 wxListItemData *item = node->GetData();
1358 item->SetText( s );
1362 void wxListLineData::SetImage( int index, int image )
1364 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1365 wxCHECK_RET( node, _T("invalid column index in SetImage()") );
1367 wxListItemData *item = node->GetData();
1368 item->SetImage(image);
1371 int wxListLineData::GetImage( int index ) const
1373 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1374 wxCHECK_MSG( node, -1, _T("invalid column index in GetImage()") );
1376 wxListItemData *item = node->GetData();
1377 return item->GetImage();
1380 wxListItemAttr *wxListLineData::GetAttr() const
1382 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1383 wxCHECK_MSG( node, NULL, _T("invalid column index in GetAttr()") );
1385 wxListItemData *item = node->GetData();
1386 return item->GetAttr();
1389 void wxListLineData::SetAttr(wxListItemAttr *attr)
1391 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1392 wxCHECK_RET( node, _T("invalid column index in SetAttr()") );
1394 wxListItemData *item = node->GetData();
1395 item->SetAttr(attr);
1398 bool wxListLineData::SetAttributes(wxDC *dc,
1399 const wxListItemAttr *attr,
1400 bool highlighted)
1402 wxWindow *listctrl = m_owner->GetParent();
1404 // fg colour
1406 // don't use foreground colour for drawing highlighted items - this might
1407 // make them completely invisible (and there is no way to do bit
1408 // arithmetics on wxColour, unfortunately)
1409 wxColour colText;
1410 if ( highlighted )
1411 #ifdef __WXMAC__
1413 if (m_owner->HasFocus()
1414 #ifdef __WXMAC__
1415 && IsControlActive( (ControlRef)m_owner->GetHandle() )
1416 #endif
1418 colText = *wxWHITE;
1419 else
1420 colText = *wxBLACK;
1422 #else
1423 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
1424 #endif
1425 else if ( attr && attr->HasTextColour() )
1426 colText = attr->GetTextColour();
1427 else
1428 colText = listctrl->GetForegroundColour();
1430 dc->SetTextForeground(colText);
1432 // font
1433 wxFont font;
1434 if ( attr && attr->HasFont() )
1435 font = attr->GetFont();
1436 else
1437 font = listctrl->GetFont();
1439 dc->SetFont(font);
1441 // bg colour
1442 bool hasBgCol = attr && attr->HasBackgroundColour();
1443 if ( highlighted || hasBgCol )
1445 if ( highlighted )
1446 dc->SetBrush( *m_owner->GetHighlightBrush() );
1447 else
1448 dc->SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
1450 dc->SetPen( *wxTRANSPARENT_PEN );
1452 return true;
1455 return false;
1458 void wxListLineData::Draw( wxDC *dc )
1460 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1461 wxCHECK_RET( node, _T("no subitems at all??") );
1463 bool highlighted = IsHighlighted();
1465 wxListItemAttr *attr = GetAttr();
1467 if ( SetAttributes(dc, attr, highlighted) )
1468 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1470 dc->DrawRectangle( m_gi->m_rectHighlight );
1472 #else
1474 if (highlighted)
1476 int flags = wxCONTROL_SELECTED;
1477 if (m_owner->HasFocus()
1478 #ifdef __WXMAC__
1479 && IsControlActive( (ControlRef)m_owner->GetHandle() )
1480 #endif
1482 flags |= wxCONTROL_FOCUSED;
1483 wxRendererNative::Get().DrawItemSelectionRect( m_owner, *dc, m_gi->m_rectHighlight, flags );
1486 else
1488 dc->DrawRectangle( m_gi->m_rectHighlight );
1491 #endif
1493 // just for debugging to better see where the items are
1494 #if 0
1495 dc->SetPen(*wxRED_PEN);
1496 dc->SetBrush(*wxTRANSPARENT_BRUSH);
1497 dc->DrawRectangle( m_gi->m_rectAll );
1498 dc->SetPen(*wxGREEN_PEN);
1499 dc->DrawRectangle( m_gi->m_rectIcon );
1500 #endif
1502 wxListItemData *item = node->GetData();
1503 if (item->HasImage())
1505 // centre the image inside our rectangle, this looks nicer when items
1506 // ae aligned in a row
1507 const wxRect& rectIcon = m_gi->m_rectIcon;
1509 m_owner->DrawImage(item->GetImage(), dc, rectIcon.x, rectIcon.y);
1512 if (item->HasText())
1514 const wxRect& rectLabel = m_gi->m_rectLabel;
1516 wxDCClipper clipper(*dc, rectLabel);
1517 dc->DrawText(item->GetText(), rectLabel.x, rectLabel.y);
1521 void wxListLineData::DrawInReportMode( wxDC *dc,
1522 const wxRect& rect,
1523 const wxRect& rectHL,
1524 bool highlighted )
1526 // TODO: later we should support setting different attributes for
1527 // different columns - to do it, just add "col" argument to
1528 // GetAttr() and move these lines into the loop below
1529 wxListItemAttr *attr = GetAttr();
1530 if ( SetAttributes(dc, attr, highlighted) )
1531 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1533 dc->DrawRectangle( rectHL );
1535 #else
1537 if (highlighted)
1539 int flags = wxCONTROL_SELECTED;
1540 if (m_owner->HasFocus()
1541 #ifdef __WXMAC__
1542 && IsControlActive( (ControlRef)m_owner->GetHandle() )
1543 #endif
1545 flags |= wxCONTROL_FOCUSED;
1546 wxRendererNative::Get().DrawItemSelectionRect( m_owner, *dc, rectHL, flags );
1548 else
1550 dc->DrawRectangle( rectHL );
1553 #endif
1555 wxCoord x = rect.x + HEADER_OFFSET_X,
1556 yMid = rect.y + rect.height/2;
1557 #ifdef __WXGTK__
1558 // This probably needs to be done
1559 // on all platforms as the icons
1560 // otherwise nearly touch the border
1561 x += 2;
1562 #endif
1564 size_t col = 0;
1565 for ( wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1566 node;
1567 node = node->GetNext(), col++ )
1569 wxListItemData *item = node->GetData();
1571 int width = m_owner->GetColumnWidth(col);
1572 int xOld = x;
1573 x += width;
1575 // Fix for a bug in wxWidgets.
1576 // This has been reported as patch 1898914:
1577 // http://sourceforge.net/tracker/index.php?func=detail&aid=1898914&group_id=9863&atid=309863
1579 // Prevents the drawing of images into the
1580 // next collumn, in case of small widths.
1581 wxDCClipper clipper(*dc, xOld, rect.y, width - 8, rect.height);
1583 if ( item->HasImage() )
1585 int ix, iy;
1586 m_owner->GetImageSize( item->GetImage(), ix, iy );
1587 m_owner->DrawImage( item->GetImage(), dc, xOld, yMid - iy/2 );
1589 ix += IMAGE_MARGIN_IN_REPORT_MODE;
1591 xOld += ix;
1592 width -= ix;
1595 if ( item->HasText() )
1596 DrawTextFormatted(dc, item->GetText(), col, xOld, yMid, width - 8);
1600 void wxListLineData::DrawTextFormatted(wxDC *dc,
1601 const wxString& textOrig,
1602 int col,
1603 int x,
1604 int yMid,
1605 int width)
1607 // we don't support displaying multiple lines currently (and neither does
1608 // wxMSW FWIW) so just merge all the lines
1609 wxString text(textOrig);
1610 text.Replace(_T("\n"), _T(" "));
1612 wxCoord w, h;
1613 dc->GetTextExtent(text, &w, &h);
1615 const wxCoord y = yMid - (h + 1)/2;
1617 wxDCClipper clipper(*dc, x, y, width, h);
1619 // determine if the string can fit inside the current width
1620 if (w <= width)
1622 // it can, draw it using the items alignment
1623 wxListItem item;
1624 m_owner->GetColumn(col, item);
1625 switch ( item.GetAlign() )
1627 case wxLIST_FORMAT_LEFT:
1628 // nothing to do
1629 break;
1631 case wxLIST_FORMAT_RIGHT:
1632 x += width - w;
1633 break;
1635 case wxLIST_FORMAT_CENTER:
1636 x += (width - w) / 2;
1637 break;
1639 default:
1640 wxFAIL_MSG( _T("unknown list item format") );
1641 break;
1644 dc->DrawText(text, x, y);
1646 else // otherwise, truncate and add an ellipsis if possible
1648 // determine the base width
1649 wxString ellipsis(wxT("..."));
1650 wxCoord base_w;
1651 dc->GetTextExtent(ellipsis, &base_w, &h);
1653 // continue until we have enough space or only one character left
1654 wxCoord w_c, h_c;
1655 size_t len = text.length();
1656 wxString drawntext = text.Left(len);
1657 while (len > 1)
1659 dc->GetTextExtent(drawntext.Last(), &w_c, &h_c);
1660 drawntext.RemoveLast();
1661 len--;
1662 w -= w_c;
1663 if (w + base_w <= width)
1664 break;
1667 // if still not enough space, remove ellipsis characters
1668 while (ellipsis.length() > 0 && w + base_w > width)
1670 ellipsis = ellipsis.Left(ellipsis.length() - 1);
1671 dc->GetTextExtent(ellipsis, &base_w, &h);
1674 // now draw the text
1675 dc->DrawText(drawntext, x, y);
1676 dc->DrawText(ellipsis, x + w, y);
1680 bool wxListLineData::Highlight( bool on )
1682 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1684 if ( on == m_highlighted )
1685 return false;
1687 m_highlighted = on;
1689 return true;
1692 void wxListLineData::ReverseHighlight( void )
1694 Highlight(!IsHighlighted());
1697 //-----------------------------------------------------------------------------
1698 // wxListHeaderWindow
1699 //-----------------------------------------------------------------------------
1701 BEGIN_EVENT_TABLE(wxListHeaderWindow,wxWindow)
1702 EVT_PAINT (wxListHeaderWindow::OnPaint)
1703 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse)
1704 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus)
1705 END_EVENT_TABLE()
1707 void wxListHeaderWindow::Init()
1709 m_currentCursor = (wxCursor *) NULL;
1710 m_isDragging = false;
1711 m_dirty = false;
1714 wxListHeaderWindow::wxListHeaderWindow()
1716 Init();
1718 m_owner = (wxListMainWindow *) NULL;
1719 m_resizeCursor = (wxCursor *) NULL;
1722 wxListHeaderWindow::wxListHeaderWindow( wxWindow *win,
1723 wxWindowID id,
1724 wxListMainWindow *owner,
1725 const wxPoint& pos,
1726 const wxSize& size,
1727 long style,
1728 const wxString &name )
1729 : wxWindow( win, id, pos, size, style, name )
1731 Init();
1733 m_owner = owner;
1734 m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
1736 #if _USE_VISATTR
1737 wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes();
1738 SetOwnForegroundColour( attr.colFg );
1739 SetOwnBackgroundColour( attr.colBg );
1740 if (!m_hasFont)
1741 SetOwnFont( attr.font );
1742 #else
1743 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
1744 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
1745 if (!m_hasFont)
1746 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT ));
1747 #endif
1750 wxListHeaderWindow::~wxListHeaderWindow()
1752 delete m_resizeCursor;
1755 #ifdef __WXUNIVERSAL__
1756 #include <wx/univ/renderer.h>
1757 #include <wx/univ/theme.h>
1758 #endif
1760 // shift the DC origin to match the position of the main window horz
1761 // scrollbar: this allows us to always use logical coords
1762 void wxListHeaderWindow::AdjustDC(wxDC& dc)
1764 int xpix;
1765 m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
1767 int view_start;
1768 m_owner->GetViewStart( &view_start, NULL );
1771 int org_x = 0;
1772 int org_y = 0;
1773 dc.GetDeviceOrigin( &org_x, &org_y );
1775 // account for the horz scrollbar offset
1776 #ifdef __WXGTK__
1777 if (GetLayoutDirection() == wxLayout_RightToLeft)
1779 // Maybe we just have to check for m_signX
1780 // in the DC, but I leave the #ifdef __WXGTK__
1781 // for now
1782 dc.SetDeviceOrigin( org_x + (view_start * xpix), org_y );
1784 else
1785 #endif
1786 dc.SetDeviceOrigin( org_x - (view_start * xpix), org_y );
1789 void wxListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1791 wxPaintDC dc( this );
1793 PrepareDC( dc );
1794 AdjustDC( dc );
1796 dc.SetFont( GetFont() );
1798 // width and height of the entire header window
1799 int w, h;
1800 GetClientSize( &w, &h );
1801 m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
1803 dc.SetBackgroundMode(wxTRANSPARENT);
1804 dc.SetTextForeground(GetForegroundColour());
1806 int x = HEADER_OFFSET_X;
1807 int numColumns = m_owner->GetColumnCount();
1808 wxListItem item;
1809 for ( int i = 0; i < numColumns && x < w; i++ )
1811 m_owner->GetColumn( i, item );
1812 int wCol = item.m_width;
1814 int cw = wCol;
1815 int ch = h;
1817 int flags = 0;
1818 if (!m_parent->IsEnabled())
1819 flags |= wxCONTROL_DISABLED;
1821 // NB: The code below is not really Mac-specific, but since we are close
1822 // to 2.8 release and I don't have time to test on other platforms, I
1823 // defined this only for wxMac. If this behavior is desired on
1824 // other platforms, please go ahead and revise or remove the #ifdef.
1825 #ifdef __WXMAC__
1826 if ( !m_owner->IsVirtual() && (item.m_mask & wxLIST_MASK_STATE) &&
1827 (item.m_state & wxLIST_STATE_SELECTED) )
1828 flags |= wxCONTROL_SELECTED;
1829 #endif
1831 wxRendererNative::Get().DrawHeaderButton
1833 this,
1835 wxRect(x, HEADER_OFFSET_Y, cw, ch),
1836 flags
1839 // see if we have enough space for the column label
1841 // for this we need the width of the text
1842 wxCoord wLabel;
1843 wxCoord hLabel;
1844 dc.GetTextExtent(item.GetText(), &wLabel, &hLabel);
1845 wLabel += 2 * EXTRA_WIDTH;
1847 // and the width of the icon, if any
1848 int ix = 0, iy = 0; // init them just to suppress the compiler warnings
1849 const int image = item.m_image;
1850 wxImageList *imageList;
1851 if ( image != -1 )
1853 imageList = m_owner->m_small_image_list;
1854 if ( imageList )
1856 imageList->GetSize(image, ix, iy);
1857 wLabel += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
1860 else
1862 imageList = NULL;
1865 // ignore alignment if there is not enough space anyhow
1866 int xAligned;
1867 switch ( wLabel < cw ? item.GetAlign() : wxLIST_FORMAT_LEFT )
1869 default:
1870 wxFAIL_MSG( _T("unknown list item format") );
1871 // fall through
1873 case wxLIST_FORMAT_LEFT:
1874 xAligned = x;
1875 break;
1877 case wxLIST_FORMAT_RIGHT:
1878 xAligned = x + cw - wLabel;
1879 break;
1881 case wxLIST_FORMAT_CENTER:
1882 xAligned = x + (cw - wLabel) / 2;
1883 break;
1886 // draw the text and image clipping them so that they
1887 // don't overwrite the column boundary
1888 wxDCClipper clipper(dc, x, HEADER_OFFSET_Y, cw, h );
1890 // if we have an image, draw it on the right of the label
1891 if ( imageList )
1893 imageList->Draw
1895 image,
1897 xAligned + wLabel - ix - HEADER_IMAGE_MARGIN_IN_REPORT_MODE,
1898 HEADER_OFFSET_Y + (h - 4 - iy)/2,
1899 wxIMAGELIST_DRAW_TRANSPARENT
1903 dc.DrawText( item.GetText(),
1904 xAligned + EXTRA_WIDTH, h / 2 - hLabel / 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1906 x += wCol;
1910 void wxListHeaderWindow::DrawCurrent()
1912 #if 1
1913 m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1914 #else
1915 int x1 = m_currentX;
1916 int y1 = 0;
1917 m_owner->ClientToScreen( &x1, &y1 );
1919 int x2 = m_currentX;
1920 int y2 = 0;
1921 m_owner->GetClientSize( NULL, &y2 );
1922 m_owner->ClientToScreen( &x2, &y2 );
1924 wxScreenDC dc;
1925 dc.SetLogicalFunction( wxINVERT );
1926 dc.SetPen( wxPen( *wxBLACK, 2, wxSOLID ) );
1927 dc.SetBrush( *wxTRANSPARENT_BRUSH );
1929 AdjustDC(dc);
1931 dc.DrawLine( x1, y1, x2, y2 );
1933 dc.SetLogicalFunction( wxCOPY );
1935 dc.SetPen( wxNullPen );
1936 dc.SetBrush( wxNullBrush );
1937 #endif
1940 void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
1942 // we want to work with logical coords
1943 int x;
1944 m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
1945 int y = event.GetY();
1947 if (m_isDragging)
1949 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING, event.GetPosition());
1951 // we don't draw the line beyond our window, but we allow dragging it
1952 // there
1953 int w = 0;
1954 GetClientSize( &w, NULL );
1955 m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
1956 w -= 6;
1958 // erase the line if it was drawn
1959 if ( m_currentX < w )
1960 DrawCurrent();
1962 if (event.ButtonUp())
1964 ReleaseMouse();
1965 m_isDragging = false;
1966 m_dirty = true;
1967 m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1968 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG, event.GetPosition());
1970 else
1972 if (x > m_minX + 7)
1973 m_currentX = x;
1974 else
1975 m_currentX = m_minX + 7;
1977 // draw in the new location
1978 if ( m_currentX < w )
1979 DrawCurrent();
1982 else // not dragging
1984 m_minX = 0;
1985 bool hit_border = false;
1987 // end of the current column
1988 int xpos = 0;
1990 // find the column where this event occurred
1991 int col,
1992 countCol = m_owner->GetColumnCount();
1993 for (col = 0; col < countCol; col++)
1995 xpos += m_owner->GetColumnWidth( col );
1996 m_column = col;
1998 if ( (abs(x-xpos) < 3) && (y < 22) )
2000 // near the column border
2001 hit_border = true;
2002 break;
2005 if ( x < xpos )
2007 // inside the column
2008 break;
2011 m_minX = xpos;
2014 if ( col == countCol )
2015 m_column = -1;
2017 if (event.LeftDown() || event.RightUp())
2019 if (hit_border && event.LeftDown())
2021 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG,
2022 event.GetPosition()) )
2024 m_isDragging = true;
2025 m_currentX = x;
2026 CaptureMouse();
2027 DrawCurrent();
2029 //else: column resizing was vetoed by the user code
2031 else // click on a column
2033 // record the selected state of the columns
2034 if (event.LeftDown())
2036 for (int i=0; i < m_owner->GetColumnCount(); i++)
2038 wxListItem colItem;
2039 m_owner->GetColumn(i, colItem);
2040 long state = colItem.GetState();
2041 if (i == m_column)
2042 colItem.SetState(state | wxLIST_STATE_SELECTED);
2043 else
2044 colItem.SetState(state & ~wxLIST_STATE_SELECTED);
2045 m_owner->SetColumn(i, colItem);
2049 SendListEvent( event.LeftDown()
2050 ? wxEVT_COMMAND_LIST_COL_CLICK
2051 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK,
2052 event.GetPosition());
2055 else if (event.Moving())
2057 bool setCursor;
2058 if (hit_border)
2060 setCursor = m_currentCursor == wxSTANDARD_CURSOR;
2061 m_currentCursor = m_resizeCursor;
2063 else
2065 setCursor = m_currentCursor != wxSTANDARD_CURSOR;
2066 m_currentCursor = wxSTANDARD_CURSOR;
2069 if ( setCursor )
2070 SetCursor(*m_currentCursor);
2075 void wxListHeaderWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
2077 m_owner->SetFocus();
2078 m_owner->Update();
2081 bool wxListHeaderWindow::SendListEvent(wxEventType type, const wxPoint& pos)
2083 wxWindow *parent = GetParent();
2084 wxListEvent le( type, parent->GetId() );
2085 le.SetEventObject( parent );
2086 le.m_pointDrag = pos;
2088 // the position should be relative to the parent window, not
2089 // this one for compatibility with MSW and common sense: the
2090 // user code doesn't know anything at all about this header
2091 // window, so why should it get positions relative to it?
2092 le.m_pointDrag.y -= GetSize().y;
2094 le.m_col = m_column;
2095 return !parent->GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
2098 //-----------------------------------------------------------------------------
2099 // wxListRenameTimer (internal)
2100 //-----------------------------------------------------------------------------
2102 wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
2104 m_owner = owner;
2107 void wxListRenameTimer::Notify()
2109 m_owner->OnRenameTimer();
2112 //-----------------------------------------------------------------------------
2113 // wxListTextCtrlWrapper (internal)
2114 //-----------------------------------------------------------------------------
2116 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper, wxEvtHandler)
2117 EVT_CHAR (wxListTextCtrlWrapper::OnChar)
2118 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp)
2119 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus)
2120 END_EVENT_TABLE()
2122 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow *owner,
2123 wxTextCtrl *text,
2124 size_t itemEdit)
2125 : m_startValue(owner->GetItemText(itemEdit)),
2126 m_itemEdited(itemEdit)
2128 m_owner = owner;
2129 m_text = text;
2130 m_finished = false;
2131 m_aboutToFinish = false;
2133 wxRect rectLabel = owner->GetLineLabelRect(itemEdit);
2135 m_owner->CalcScrolledPosition(rectLabel.x, rectLabel.y,
2136 &rectLabel.x, &rectLabel.y);
2138 m_text->Create(owner, wxID_ANY, m_startValue,
2139 wxPoint(rectLabel.x-4,rectLabel.y-4),
2140 wxSize(rectLabel.width+11,rectLabel.height+8));
2141 m_text->SetFocus();
2143 m_text->PushEventHandler(this);
2146 void wxListTextCtrlWrapper::Finish()
2148 if ( !m_finished )
2150 m_finished = true;
2152 m_text->RemoveEventHandler(this);
2153 m_owner->FinishEditing(m_text);
2155 wxPendingDelete.Append( this );
2159 bool wxListTextCtrlWrapper::AcceptChanges()
2161 const wxString value = m_text->GetValue();
2163 // notice that we should always call OnRenameAccept() to generate the "end
2164 // label editing" event, even if the user hasn't really changed anything
2165 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
2167 // vetoed by the user
2168 return false;
2171 // accepted, do rename the item (unless nothing changed)
2172 if ( value != m_startValue )
2173 m_owner->SetItemText(m_itemEdited, value);
2175 return true;
2178 void wxListTextCtrlWrapper::AcceptChangesAndFinish()
2180 m_aboutToFinish = true;
2182 // Notify the owner about the changes
2183 AcceptChanges();
2185 // Even if vetoed, close the control (consistent with MSW)
2186 Finish();
2189 void wxListTextCtrlWrapper::OnChar( wxKeyEvent &event )
2191 switch ( event.m_keyCode )
2193 case WXK_RETURN:
2194 AcceptChangesAndFinish();
2195 break;
2197 case WXK_ESCAPE:
2198 m_owner->OnRenameCancelled( m_itemEdited );
2199 Finish();
2200 break;
2202 default:
2203 event.Skip();
2207 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
2209 if (m_finished)
2211 event.Skip();
2212 return;
2215 // auto-grow the textctrl:
2216 wxSize parentSize = m_owner->GetSize();
2217 wxPoint myPos = m_text->GetPosition();
2218 wxSize mySize = m_text->GetSize();
2219 int sx, sy;
2220 m_text->GetTextExtent(m_text->GetValue() + _T("MM"), &sx, &sy);
2221 if (myPos.x + sx > parentSize.x)
2222 sx = parentSize.x - myPos.x;
2223 if (mySize.x > sx)
2224 sx = mySize.x;
2225 m_text->SetSize(sx, wxDefaultCoord);
2227 event.Skip();
2230 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
2232 if ( !m_finished && !m_aboutToFinish )
2234 if ( !AcceptChanges() )
2235 m_owner->OnRenameCancelled( m_itemEdited );
2237 Finish();
2240 // We must let the native text control handle focus
2241 event.Skip();
2244 //-----------------------------------------------------------------------------
2245 // wxListMainWindow
2246 //-----------------------------------------------------------------------------
2248 BEGIN_EVENT_TABLE(wxListMainWindow,wxScrolledWindow)
2249 EVT_PAINT (wxListMainWindow::OnPaint)
2250 EVT_ERASE_BACKGROUND (wxListMainWindow::OnErase)
2251 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse)
2252 EVT_CHAR (wxListMainWindow::OnChar)
2253 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown)
2254 EVT_KEY_UP (wxListMainWindow::OnKeyUp)
2255 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus)
2256 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus)
2257 EVT_SCROLLWIN (wxListMainWindow::OnScroll)
2258 END_EVENT_TABLE()
2260 void wxListMainWindow::Init()
2262 m_dirty = true;
2263 m_countVirt = 0;
2264 m_lineFrom =
2265 m_lineTo = (size_t)-1;
2266 m_linesPerPage = 0;
2268 m_headerWidth =
2269 m_lineHeight = 0;
2271 m_small_image_list = (wxImageList *) NULL;
2272 m_normal_image_list = (wxImageList *) NULL;
2274 m_small_spacing = 30;
2275 m_normal_spacing = 40;
2277 m_hasFocus = false;
2278 m_dragCount = 0;
2279 m_isCreated = false;
2281 m_lastOnSame = false;
2282 m_renameTimer = new wxListRenameTimer( this );
2283 m_textctrlWrapper = NULL;
2285 m_current =
2286 m_lineLastClicked =
2287 m_lineSelectSingleOnUp =
2288 m_lineBeforeLastClicked = (size_t)-1;
2290 m_freezeCount = 0;
2293 wxListMainWindow::wxListMainWindow()
2295 Init();
2297 m_highlightBrush =
2298 m_highlightUnfocusedBrush = (wxBrush *) NULL;
2301 wxListMainWindow::wxListMainWindow( wxWindow *parent,
2302 wxWindowID id,
2303 const wxPoint& pos,
2304 const wxSize& size,
2305 long style,
2306 const wxString &name )
2307 : wxScrolledWindow( parent, id, pos, size,
2308 style | wxHSCROLL | wxVSCROLL, name )
2310 Init();
2312 m_highlightBrush = new wxBrush
2314 wxSystemSettings::GetColour
2316 wxSYS_COLOUR_HIGHLIGHT
2318 wxSOLID
2321 m_highlightUnfocusedBrush = new wxBrush
2323 wxSystemSettings::GetColour
2325 wxSYS_COLOUR_BTNSHADOW
2327 wxSOLID
2330 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2332 wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes();
2333 SetOwnForegroundColour( attr.colFg );
2334 SetOwnBackgroundColour( attr.colBg );
2335 if (!m_hasFont)
2336 SetOwnFont( attr.font );
2339 wxListMainWindow::~wxListMainWindow()
2341 DoDeleteAllItems();
2342 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
2343 WX_CLEAR_ARRAY(m_aColWidths);
2345 delete m_highlightBrush;
2346 delete m_highlightUnfocusedBrush;
2347 delete m_renameTimer;
2350 void wxListMainWindow::CacheLineData(size_t line)
2352 wxGenericListCtrl *listctrl = GetListCtrl();
2354 wxListLineData *ld = GetDummyLine();
2356 size_t countCol = GetColumnCount();
2357 for ( size_t col = 0; col < countCol; col++ )
2359 ld->SetText(col, listctrl->OnGetItemText(line, col));
2360 ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col));
2363 ld->SetAttr(listctrl->OnGetItemAttr(line));
2366 wxListLineData *wxListMainWindow::GetDummyLine() const
2368 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2369 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2371 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
2373 // we need to recreate the dummy line if the number of columns in the
2374 // control changed as it would have the incorrect number of fields
2375 // otherwise
2376 if ( !m_lines.IsEmpty() &&
2377 m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() )
2379 self->m_lines.Clear();
2382 if ( m_lines.IsEmpty() )
2384 wxListLineData *line = new wxListLineData(self);
2385 self->m_lines.Add(line);
2387 // don't waste extra memory -- there never going to be anything
2388 // else/more in this array
2389 self->m_lines.Shrink();
2392 return &m_lines[0];
2395 // ----------------------------------------------------------------------------
2396 // line geometry (report mode only)
2397 // ----------------------------------------------------------------------------
2399 wxCoord wxListMainWindow::GetLineHeight() const
2401 // we cache the line height as calling GetTextExtent() is slow
2402 if ( !m_lineHeight )
2404 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
2406 wxClientDC dc( self );
2407 dc.SetFont( GetFont() );
2409 wxCoord y;
2410 dc.GetTextExtent(_T("H"), NULL, &y);
2412 if ( m_small_image_list && m_small_image_list->GetImageCount() )
2414 int iw = 0, ih = 0;
2415 m_small_image_list->GetSize(0, iw, ih);
2416 y = wxMax(y, ih);
2419 y += EXTRA_HEIGHT;
2420 self->m_lineHeight = y + LINE_SPACING;
2423 return m_lineHeight;
2426 wxCoord wxListMainWindow::GetLineY(size_t line) const
2428 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2430 return LINE_SPACING + line * GetLineHeight();
2433 wxRect wxListMainWindow::GetLineRect(size_t line) const
2435 if ( !InReportView() )
2436 return GetLine(line)->m_gi->m_rectAll;
2438 wxRect rect;
2439 rect.x = HEADER_OFFSET_X;
2440 rect.y = GetLineY(line);
2441 rect.width = GetHeaderWidth();
2442 rect.height = GetLineHeight();
2444 return rect;
2447 wxRect wxListMainWindow::GetLineLabelRect(size_t line) const
2449 if ( !InReportView() )
2450 return GetLine(line)->m_gi->m_rectLabel;
2452 int image_x = 0;
2453 wxListLineData *data = GetLine(line);
2454 wxListItemDataList::compatibility_iterator node = data->m_items.GetFirst();
2455 if (node)
2457 wxListItemData *item = node->GetData();
2458 if ( item->HasImage() )
2460 int ix, iy;
2461 GetImageSize( item->GetImage(), ix, iy );
2462 image_x = 3 + ix + IMAGE_MARGIN_IN_REPORT_MODE;
2466 wxRect rect;
2467 rect.x = image_x + HEADER_OFFSET_X;
2468 rect.y = GetLineY(line);
2469 rect.width = GetColumnWidth(0) - image_x;
2470 rect.height = GetLineHeight();
2472 return rect;
2475 wxRect wxListMainWindow::GetLineIconRect(size_t line) const
2477 if ( !InReportView() )
2478 return GetLine(line)->m_gi->m_rectIcon;
2480 wxListLineData *ld = GetLine(line);
2481 wxASSERT_MSG( ld->HasImage(), _T("should have an image") );
2483 wxRect rect;
2484 rect.x = HEADER_OFFSET_X;
2485 rect.y = GetLineY(line);
2486 GetImageSize(ld->GetImage(), rect.width, rect.height);
2488 return rect;
2491 wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const
2493 return InReportView() ? GetLineRect(line)
2494 : GetLine(line)->m_gi->m_rectHighlight;
2497 long wxListMainWindow::HitTestLine(size_t line, int x, int y) const
2499 wxASSERT_MSG( line < GetItemCount(), _T("invalid line in HitTestLine") );
2501 wxListLineData *ld = GetLine(line);
2503 if ( ld->HasImage() && GetLineIconRect(line).Contains(x, y) )
2504 return wxLIST_HITTEST_ONITEMICON;
2506 // VS: Testing for "ld->HasText() || InReportView()" instead of
2507 // "ld->HasText()" is needed to make empty lines in report view
2508 // possible
2509 if ( ld->HasText() || InReportView() )
2511 wxRect rect = InReportView() ? GetLineRect(line)
2512 : GetLineLabelRect(line);
2514 if ( rect.Contains(x, y) )
2515 return wxLIST_HITTEST_ONITEMLABEL;
2518 return 0;
2521 // ----------------------------------------------------------------------------
2522 // highlight (selection) handling
2523 // ----------------------------------------------------------------------------
2525 bool wxListMainWindow::IsHighlighted(size_t line) const
2527 if ( IsVirtual() )
2529 return m_selStore.IsSelected(line);
2531 else // !virtual
2533 wxListLineData *ld = GetLine(line);
2534 wxCHECK_MSG( ld, false, _T("invalid index in IsHighlighted") );
2536 return ld->IsHighlighted();
2540 void wxListMainWindow::HighlightLines( size_t lineFrom,
2541 size_t lineTo,
2542 bool highlight )
2544 if ( IsVirtual() )
2546 wxArrayInt linesChanged;
2547 if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight,
2548 &linesChanged) )
2550 // meny items changed state, refresh everything
2551 RefreshLines(lineFrom, lineTo);
2553 else // only a few items changed state, refresh only them
2555 size_t count = linesChanged.GetCount();
2556 for ( size_t n = 0; n < count; n++ )
2558 RefreshLine(linesChanged[n]);
2562 else // iterate over all items in non report view
2564 for ( size_t line = lineFrom; line <= lineTo; line++ )
2566 if ( HighlightLine(line, highlight) )
2567 RefreshLine(line);
2572 bool wxListMainWindow::HighlightLine( size_t line, bool highlight )
2574 bool changed;
2576 if ( IsVirtual() )
2578 changed = m_selStore.SelectItem(line, highlight);
2580 else // !virtual
2582 wxListLineData *ld = GetLine(line);
2583 wxCHECK_MSG( ld, false, _T("invalid index in HighlightLine") );
2585 changed = ld->Highlight(highlight);
2588 if ( changed )
2590 SendNotify( line, highlight ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2591 : wxEVT_COMMAND_LIST_ITEM_DESELECTED );
2594 return changed;
2597 void wxListMainWindow::RefreshLine( size_t line )
2599 if ( InReportView() )
2601 size_t visibleFrom, visibleTo;
2602 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2604 if ( line < visibleFrom || line > visibleTo )
2605 return;
2608 wxRect rect = GetLineRect(line);
2610 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2611 RefreshRect( rect );
2614 void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo )
2616 // we suppose that they are ordered by caller
2617 wxASSERT_MSG( lineFrom <= lineTo, _T("indices in disorder") );
2619 wxASSERT_MSG( lineTo < GetItemCount(), _T("invalid line range") );
2621 if ( InReportView() )
2623 size_t visibleFrom, visibleTo;
2624 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2626 if ( lineFrom < visibleFrom )
2627 lineFrom = visibleFrom;
2628 if ( lineTo > visibleTo )
2629 lineTo = visibleTo;
2631 wxRect rect;
2632 rect.x = 0;
2633 rect.y = GetLineY(lineFrom);
2634 rect.width = GetClientSize().x;
2635 rect.height = GetLineY(lineTo) - rect.y + GetLineHeight();
2637 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2638 RefreshRect( rect );
2640 else // !report
2642 // TODO: this should be optimized...
2643 for ( size_t line = lineFrom; line <= lineTo; line++ )
2645 RefreshLine(line);
2650 void wxListMainWindow::RefreshAfter( size_t lineFrom )
2652 if ( InReportView() )
2654 size_t visibleFrom, visibleTo;
2655 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2657 if ( lineFrom < visibleFrom )
2658 lineFrom = visibleFrom;
2659 else if ( lineFrom > visibleTo )
2660 return;
2662 wxRect rect;
2663 rect.x = 0;
2664 rect.y = GetLineY(lineFrom);
2665 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2667 wxSize size = GetClientSize();
2668 rect.width = size.x;
2670 // refresh till the bottom of the window
2671 rect.height = size.y - rect.y;
2673 RefreshRect( rect );
2675 else // !report
2677 // TODO: how to do it more efficiently?
2678 m_dirty = true;
2682 void wxListMainWindow::RefreshSelected()
2684 if ( IsEmpty() )
2685 return;
2687 size_t from, to;
2688 if ( InReportView() )
2690 GetVisibleLinesRange(&from, &to);
2692 else // !virtual
2694 from = 0;
2695 to = GetItemCount() - 1;
2698 if ( HasCurrent() && m_current >= from && m_current <= to )
2699 RefreshLine(m_current);
2701 for ( size_t line = from; line <= to; line++ )
2703 // NB: the test works as expected even if m_current == -1
2704 if ( line != m_current && IsHighlighted(line) )
2705 RefreshLine(line);
2709 void wxListMainWindow::Freeze()
2711 m_freezeCount++;
2714 void wxListMainWindow::Thaw()
2716 wxCHECK_RET( m_freezeCount > 0, _T("thawing unfrozen list control?") );
2718 if ( --m_freezeCount == 0 )
2719 Refresh();
2722 void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2724 // wxBufferedPaintDC falls over in the case of dimensions that
2725 // are zero, as cann happen when a splitter is moved so that
2726 // the entire window is hidden (e.g. on the transfers window).
2728 // This has been reported as patch #1899643:
2729 // http://sourceforge.net/tracker/index.php?func=detail&aid=1899643&group_id=9863&atid=309863
2730 wxSize size = GetClientSize();
2731 if ((size.x <= 0) || (size.y <= 0)) {
2732 wxPaintDC dc(this);
2733 return;
2736 // Note: a wxPaintDC must be constructed even if no drawing is
2737 // done (a Windows requirement).
2738 wxBufferedPaintDC dc( this );
2740 // Ensure an uniform background color, as to avoid differences between
2741 // the automatically cleared parts and the rest of the canvas.
2742 dc.SetBackground(*(wxTheBrushList->FindOrCreateBrush(
2743 wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX), wxSOLID)));
2745 if ( m_freezeCount )
2746 return;
2748 if ( m_dirty )
2749 // delay the repainting until we calculate all the items positions
2750 return;
2752 PrepareDC( dc );
2754 // We need to clear the DC manually, since we intercept BG-erase events.
2755 dc.Clear();
2757 // IsEmpty is checked now, after clearing, to avoid garbage on empty lists.
2758 if ( IsEmpty() ) {
2759 return;
2762 int dev_x, dev_y;
2763 CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
2765 dc.SetFont( GetFont() );
2767 if ( InReportView() )
2769 int lineHeight = GetLineHeight();
2771 size_t visibleFrom, visibleTo;
2772 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2774 wxRect rectLine;
2775 int xOrig = dc.LogicalToDeviceX( 0 );
2776 int yOrig = dc.LogicalToDeviceY( 0 );
2778 // tell the caller cache to cache the data
2779 if ( IsVirtual() )
2781 wxListEvent evCache(wxEVT_COMMAND_LIST_CACHE_HINT,
2782 GetParent()->GetId());
2783 evCache.SetEventObject( GetParent() );
2784 evCache.m_oldItemIndex = visibleFrom;
2785 evCache.m_itemIndex = visibleTo;
2786 GetParent()->GetEventHandler()->ProcessEvent( evCache );
2789 for ( size_t line = visibleFrom; line <= visibleTo; line++ )
2791 rectLine = GetLineRect(line);
2794 if ( !IsExposed(rectLine.x + xOrig, rectLine.y + yOrig,
2795 rectLine.width, rectLine.height) )
2797 // don't redraw unaffected lines to avoid flicker
2798 continue;
2801 if (HasFlag(wxLC_OWNERDRAW)) {
2802 ((wxGenericListCtrl*)m_parent)->OnDrawItem(line, &dc, rectLine, GetLineHighlightRect(line), IsHighlighted(line));
2803 } else {
2804 GetLine(line)->DrawInReportMode( &dc,
2805 rectLine,
2806 GetLineHighlightRect(line),
2807 IsHighlighted(line) );
2811 if ( HasFlag(wxLC_HRULES) )
2813 wxPen pen(GetRuleColour(), 1, wxSOLID);
2814 wxSize clientSize = GetClientSize();
2816 size_t i = visibleFrom;
2817 if (i == 0) i = 1; // Don't draw the first one
2818 for ( ; i <= visibleTo; i++ )
2820 dc.SetPen(pen);
2821 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2822 dc.DrawLine(0 - dev_x, i * lineHeight,
2823 clientSize.x - dev_x, i * lineHeight);
2826 // Draw last horizontal rule
2827 if ( visibleTo == GetItemCount() - 1 )
2829 dc.SetPen( pen );
2830 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2831 dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight,
2832 clientSize.x - dev_x , (m_lineTo + 1) * lineHeight );
2836 // Draw vertical rules if required
2837 if ( HasFlag(wxLC_VRULES) && !IsEmpty() )
2839 wxPen pen(GetRuleColour(), 1, wxSOLID);
2840 wxRect firstItemRect, lastItemRect;
2842 GetItemRect(visibleFrom, firstItemRect);
2843 GetItemRect(visibleTo, lastItemRect);
2844 int x = firstItemRect.GetX();
2845 dc.SetPen(pen);
2846 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2848 for (int col = 0; col < GetColumnCount(); col++)
2850 int colWidth = GetColumnWidth(col);
2851 x += colWidth;
2852 int x_pos = x - dev_x;
2853 if (col < GetColumnCount()-1) x_pos -= 2;
2854 dc.DrawLine(x_pos, firstItemRect.GetY() - 1 - dev_y,
2855 x_pos, lastItemRect.GetBottom() + 1 - dev_y);
2859 else // !report
2861 size_t count = GetItemCount();
2862 for ( size_t i = 0; i < count; i++ )
2864 GetLine(i)->Draw( &dc );
2868 #ifndef __WXMAC__
2869 // Don't draw rect outline under Mac at all.
2870 if ( HasCurrent() )
2872 if ( m_hasFocus )
2874 wxRect rect( GetLineHighlightRect( m_current ) );
2875 #ifndef __WXGTK20__
2876 dc.SetPen( *wxBLACK_PEN );
2877 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2878 dc.DrawRectangle( rect );
2879 #else
2880 wxRendererNative::Get().DrawItemSelectionRect( this, dc, rect, wxCONTROL_CURRENT|wxCONTROL_FOCUSED );
2882 #endif
2885 #endif
2888 void wxListMainWindow::HighlightAll( bool on )
2890 if ( IsSingleSel() )
2892 wxASSERT_MSG( !on, _T("can't do this in a single selection control") );
2894 // we just have one item to turn off
2895 if ( HasCurrent() && IsHighlighted(m_current) )
2897 HighlightLine(m_current, false);
2898 RefreshLine(m_current);
2901 else // multi selection
2903 if ( !IsEmpty() )
2904 HighlightLines(0, GetItemCount() - 1, on);
2908 void wxListMainWindow::SendNotify( size_t line,
2909 wxEventType command,
2910 const wxPoint& point )
2912 wxListEvent le( command, GetParent()->GetId() );
2913 le.SetEventObject( GetParent() );
2915 le.m_itemIndex = line;
2917 // set only for events which have position
2918 if ( point != wxDefaultPosition )
2919 le.m_pointDrag = point;
2921 // don't try to get the line info for virtual list controls: the main
2922 // program has it anyhow and if we did it would result in accessing all
2923 // the lines, even those which are not visible now and this is precisely
2924 // what we're trying to avoid
2925 if ( !IsVirtual() )
2927 if ( line != (size_t)-1 )
2929 GetLine(line)->GetItem( 0, le.m_item );
2931 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2933 //else: there may be no more such item
2935 GetParent()->GetEventHandler()->ProcessEvent( le );
2938 void wxListMainWindow::ChangeCurrent(size_t current)
2940 m_current = current;
2942 // as the current item changed, we shouldn't start editing it when the
2943 // "slow click" timer expires as the click happened on another item
2944 if ( m_renameTimer->IsRunning() )
2945 m_renameTimer->Stop();
2947 SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
2950 wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
2952 wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
2953 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2955 wxASSERT_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)),
2956 wxT("EditLabel() needs a text control") );
2958 size_t itemEdit = (size_t)item;
2960 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
2961 le.SetEventObject( GetParent() );
2962 le.m_itemIndex = item;
2963 wxListLineData *data = GetLine(itemEdit);
2964 wxCHECK_MSG( data, NULL, _T("invalid index in EditLabel()") );
2965 data->GetItem( 0, le.m_item );
2967 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
2969 // vetoed by user code
2970 return NULL;
2973 // We have to call this here because the label in question might just have
2974 // been added and no screen update taken place.
2975 if ( m_dirty )
2977 wxSafeYield();
2979 // Pending events dispatched by wxSafeYield might have changed the item
2980 // count
2981 if ( (size_t)item >= GetItemCount() )
2982 return NULL;
2985 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
2986 m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
2987 return m_textctrlWrapper->GetText();
2990 void wxListMainWindow::OnRenameTimer()
2992 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2994 EditLabel( m_current );
2997 bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
2999 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
3000 le.SetEventObject( GetParent() );
3001 le.m_itemIndex = itemEdit;
3003 wxListLineData *data = GetLine(itemEdit);
3005 wxCHECK_MSG( data, false, _T("invalid index in OnRenameAccept()") );
3007 data->GetItem( 0, le.m_item );
3008 le.m_item.m_text = value;
3009 return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
3010 le.IsAllowed();
3013 void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
3015 // let owner know that the edit was cancelled
3016 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
3018 le.SetEditCanceled(true);
3020 le.SetEventObject( GetParent() );
3021 le.m_itemIndex = itemEdit;
3023 wxListLineData *data = GetLine(itemEdit);
3024 wxCHECK_RET( data, _T("invalid index in OnRenameCancelled()") );
3026 data->GetItem( 0, le.m_item );
3027 GetEventHandler()->ProcessEvent( le );
3030 void wxListMainWindow::OnMouse( wxMouseEvent &event )
3033 #ifdef __WXMAC__
3034 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
3035 // shutdown the edit control when the mouse is clicked elsewhere on the
3036 // listctrl because the order of events is different (or something like
3037 // that), so explicitly end the edit if it is active.
3038 if ( event.LeftDown() && m_textctrlWrapper )
3039 m_textctrlWrapper->AcceptChangesAndFinish();
3040 #endif // __WXMAC__
3042 if ( event.LeftDown() )
3043 SetFocusIgnoringChildren();
3045 event.SetEventObject( GetParent() );
3046 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3047 return;
3049 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
3051 // let the base handle mouse wheel events.
3052 event.Skip();
3053 return;
3056 if ( !HasCurrent() || IsEmpty() )
3058 if (event.RightDown())
3060 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3061 // Allow generation of context menu event
3062 event.Skip();
3064 return;
3067 if (m_dirty)
3068 return;
3070 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
3071 event.ButtonDClick()) )
3072 return;
3074 int x = event.GetX();
3075 int y = event.GetY();
3076 CalcUnscrolledPosition( x, y, &x, &y );
3078 // where did we hit it (if we did)?
3079 long hitResult = 0;
3081 size_t count = GetItemCount(),
3082 current;
3084 if ( InReportView() )
3086 current = y / GetLineHeight();
3087 if ( current < count )
3088 hitResult = HitTestLine(current, x, y);
3090 else // !report
3092 // TODO: optimize it too! this is less simple than for report view but
3093 // enumerating all items is still not a way to do it!!
3094 for ( current = 0; current < count; current++ )
3096 hitResult = HitTestLine(current, x, y);
3097 if ( hitResult )
3098 break;
3102 if (event.Dragging())
3104 if (m_dragCount == 0)
3106 // we have to report the raw, physical coords as we want to be
3107 // able to call HitTest(event.m_pointDrag) from the user code to
3108 // get the item being dragged
3109 m_dragStart = event.GetPosition();
3112 m_dragCount++;
3114 if (m_dragCount != 3)
3115 return;
3117 int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3118 : wxEVT_COMMAND_LIST_BEGIN_DRAG;
3120 wxListEvent le( command, GetParent()->GetId() );
3121 le.SetEventObject( GetParent() );
3122 le.m_itemIndex = m_lineLastClicked;
3123 le.m_pointDrag = m_dragStart;
3124 GetParent()->GetEventHandler()->ProcessEvent( le );
3126 return;
3128 else
3130 m_dragCount = 0;
3133 if ( !hitResult )
3135 // outside of any item
3136 if (event.RightDown())
3138 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3140 wxContextMenuEvent evtCtx(
3141 wxEVT_CONTEXT_MENU,
3142 GetParent()->GetId(),
3143 ClientToScreen(event.GetPosition()));
3144 evtCtx.SetEventObject(GetParent());
3145 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
3147 else
3149 // reset the selection and bail out
3150 HighlightAll(false);
3153 return;
3156 bool forceClick = false;
3157 if (event.ButtonDClick())
3159 if ( m_renameTimer->IsRunning() )
3160 m_renameTimer->Stop();
3162 m_lastOnSame = false;
3164 if ( current == m_lineLastClicked )
3166 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3168 return;
3170 else
3172 // The first click was on another item, so don't interpret this as
3173 // a double click, but as a simple click instead
3174 forceClick = true;
3178 if (event.LeftUp())
3180 if (m_lineSelectSingleOnUp != (size_t)-1)
3182 // select single line
3183 HighlightAll( false );
3184 ReverseHighlight(m_lineSelectSingleOnUp);
3187 if (m_lastOnSame)
3189 if ((current == m_current) &&
3190 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
3191 HasFlag(wxLC_EDIT_LABELS) )
3193 if (InReportView())
3195 wxRect label = GetLineLabelRect( current );
3196 if (label.Contains( x, y ))
3197 m_renameTimer->Start( 250, true );
3200 else
3201 m_renameTimer->Start( 250, true );
3205 m_lastOnSame = false;
3206 m_lineSelectSingleOnUp = (size_t)-1;
3208 else
3210 // This is necessary, because after a DnD operation in
3211 // from and to ourself, the up event is swallowed by the
3212 // DnD code. So on next non-up event (which means here and
3213 // now) m_lineSelectSingleOnUp should be reset.
3214 m_lineSelectSingleOnUp = (size_t)-1;
3216 if (event.RightDown())
3218 m_lineBeforeLastClicked = m_lineLastClicked;
3219 m_lineLastClicked = current;
3221 // If the item is already selected, do not update the selection.
3222 // Multi-selections should not be cleared if a selected item is clicked.
3223 if (!IsHighlighted(current))
3225 HighlightAll(false);
3226 ChangeCurrent(current);
3227 ReverseHighlight(m_current);
3230 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3232 wxContextMenuEvent evtCtx(
3233 wxEVT_CONTEXT_MENU,
3234 GetParent()->GetId(),
3235 ClientToScreen(event.GetPosition()));
3236 evtCtx.SetEventObject(GetParent());
3237 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
3239 else if (event.MiddleDown())
3241 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
3243 else if ( event.LeftDown() || forceClick )
3245 m_lineBeforeLastClicked = m_lineLastClicked;
3246 m_lineLastClicked = current;
3248 size_t oldCurrent = m_current;
3249 bool oldWasSelected = IsHighlighted(m_current);
3251 bool cmdModifierDown = event.CmdDown();
3252 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
3254 if ( IsSingleSel() || !IsHighlighted(current) )
3256 HighlightAll( false );
3258 ChangeCurrent(current);
3260 ReverseHighlight(m_current);
3262 else // multi sel & current is highlighted & no mod keys
3264 m_lineSelectSingleOnUp = current;
3265 ChangeCurrent(current); // change focus
3268 else // multi sel & either ctrl or shift is down
3270 if (cmdModifierDown)
3272 ChangeCurrent(current);
3274 ReverseHighlight(m_current);
3276 else if (event.ShiftDown())
3278 ChangeCurrent(current);
3280 size_t lineFrom = oldCurrent,
3281 lineTo = current;
3283 if ( lineTo < lineFrom )
3285 lineTo = lineFrom;
3286 lineFrom = m_current;
3289 HighlightLines(lineFrom, lineTo);
3291 else // !ctrl, !shift
3293 // test in the enclosing if should make it impossible
3294 wxFAIL_MSG( _T("how did we get here?") );
3298 if (m_current != oldCurrent)
3299 RefreshLine( oldCurrent );
3301 // forceClick is only set if the previous click was on another item
3302 m_lastOnSame = !forceClick && (m_current == oldCurrent) && oldWasSelected;
3306 void wxListMainWindow::MoveToItem(size_t item)
3308 if ( item == (size_t)-1 )
3309 return;
3311 wxRect rect = GetLineRect(item);
3313 int client_w, client_h;
3314 GetClientSize( &client_w, &client_h );
3316 const int hLine = GetLineHeight();
3318 int view_x = SCROLL_UNIT_X * GetScrollPos( wxHORIZONTAL );
3319 int view_y = hLine * GetScrollPos( wxVERTICAL );
3321 if ( InReportView() )
3323 // the next we need the range of lines shown it might be different,
3324 // so recalculate it
3325 ResetVisibleLinesRange();
3327 if (rect.y < view_y)
3328 Scroll( -1, rect.y / hLine );
3329 if (rect.y + rect.height + 5 > view_y + client_h)
3330 Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
3332 #ifdef __WXMAC__
3333 // At least on Mac the visible lines value will get reset inside of
3334 // Scroll *before* it actually scrolls the window because of the
3335 // Update() that happens there, so it will still have the wrong value.
3336 // So let's reset it again and wait for it to be recalculated in the
3337 // next paint event. I would expect this problem to show up in wxGTK
3338 // too but couldn't duplicate it there. Perhaps the order of events
3339 // is different... --Robin
3340 ResetVisibleLinesRange();
3341 #endif
3343 else // !report
3345 if (rect.x-view_x < 5)
3346 Scroll( (rect.x - 5) / SCROLL_UNIT_X, -1 );
3347 if (rect.x + rect.width - 5 > view_x + client_w)
3348 Scroll( (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X, -1 );
3352 // ----------------------------------------------------------------------------
3353 // keyboard handling
3354 // ----------------------------------------------------------------------------
3356 void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
3358 wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
3359 _T("invalid item index in OnArrowChar()") );
3361 size_t oldCurrent = m_current;
3363 // in single selection we just ignore Shift as we can't select several
3364 // items anyhow
3365 if ( event.ShiftDown() && !IsSingleSel() )
3367 ChangeCurrent(newCurrent);
3369 // refresh the old focus to remove it
3370 RefreshLine( oldCurrent );
3372 // select all the items between the old and the new one
3373 if ( oldCurrent > newCurrent )
3375 newCurrent = oldCurrent;
3376 oldCurrent = m_current;
3379 HighlightLines(oldCurrent, newCurrent);
3381 else // !shift
3383 // all previously selected items are unselected unless ctrl is held
3384 // in a multiselection control
3385 if ( !event.ControlDown() || IsSingleSel() )
3386 HighlightAll(false);
3388 ChangeCurrent(newCurrent);
3390 // refresh the old focus to remove it
3391 RefreshLine( oldCurrent );
3393 // in single selection mode we must always have a selected item
3394 if ( !event.ControlDown() || IsSingleSel() )
3395 HighlightLine( m_current, true );
3398 RefreshLine( m_current );
3400 MoveToFocus();
3403 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
3405 wxWindow *parent = GetParent();
3407 // propagate the key event upwards
3408 wxKeyEvent ke( wxEVT_KEY_DOWN );
3409 #if 0
3410 ke.m_shiftDown = event.m_shiftDown;
3411 ke.m_controlDown = event.m_controlDown;
3412 ke.m_altDown = event.m_altDown;
3413 ke.m_metaDown = event.m_metaDown;
3414 ke.m_keyCode = event.m_keyCode;
3415 ke.m_x = event.m_x;
3416 ke.m_y = event.m_y;
3417 #else
3418 // This is a fix for a bug in wxWidgets, where m_uniChar isn't
3419 // set in the new event object, thus breaking GetUnicodeKey()
3420 // http://sourceforge.net/tracker/index.php?func=detail&aid=1863312&group_id=9863&atid=109863
3421 ke = event;
3422 #endif
3423 ke.SetEventObject( parent );
3424 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
3426 event.Skip();
3429 void wxListMainWindow::OnKeyUp( wxKeyEvent &event )
3431 wxWindow *parent = GetParent();
3433 // propagate the key event upwards
3434 wxKeyEvent ke( wxEVT_KEY_UP );
3435 #if 0
3436 ke.m_shiftDown = event.m_shiftDown;
3437 ke.m_controlDown = event.m_controlDown;
3438 ke.m_altDown = event.m_altDown;
3439 ke.m_metaDown = event.m_metaDown;
3440 ke.m_keyCode = event.m_keyCode;
3441 ke.m_x = event.m_x;
3442 ke.m_y = event.m_y;
3443 #else
3444 // This is a fix for a bug in wxWidgets, where m_uniChar isn't
3445 // set in the new event object, thus breaking GetUnicodeKey()
3446 // http://sourceforge.net/tracker/index.php?func=detail&aid=1863312&group_id=9863&atid=109863
3447 ke = event;
3448 #endif
3449 ke.SetEventObject( parent );
3450 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
3452 event.Skip();
3455 void wxListMainWindow::OnChar( wxKeyEvent &event )
3457 wxWindow *parent = GetParent();
3459 // send a list_key event up
3460 if ( HasCurrent() )
3462 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() );
3463 le.m_itemIndex = m_current;
3464 GetLine(m_current)->GetItem( 0, le.m_item );
3465 le.m_code = event.GetKeyCode();
3466 le.SetEventObject( parent );
3467 parent->GetEventHandler()->ProcessEvent( le );
3470 // propagate the char event upwards
3471 wxKeyEvent ke( wxEVT_CHAR );
3472 #if 0
3473 ke.m_shiftDown = event.m_shiftDown;
3474 ke.m_controlDown = event.m_controlDown;
3475 ke.m_altDown = event.m_altDown;
3476 ke.m_metaDown = event.m_metaDown;
3477 ke.m_keyCode = event.m_keyCode;
3478 ke.m_x = event.m_x;
3479 ke.m_y = event.m_y;
3480 #else
3481 // This is a fix for a bug in wxWidgets, where m_uniChar isn't
3482 // set in the new event object, thus breaking GetUnicodeKey()
3483 // http://sourceforge.net/tracker/index.php?func=detail&aid=1863312&group_id=9863&atid=109863
3484 ke = event;
3485 #endif
3486 ke.SetEventObject( parent );
3487 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
3489 if (event.GetKeyCode() == WXK_TAB)
3491 wxNavigationKeyEvent nevent;
3492 nevent.SetWindowChange( event.ControlDown() );
3493 nevent.SetDirection( !event.ShiftDown() );
3494 nevent.SetEventObject( GetParent()->GetParent() );
3495 nevent.SetCurrentFocus( m_parent );
3496 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent ))
3497 return;
3500 // no item -> nothing to do
3501 if (!HasCurrent())
3503 event.Skip();
3504 return;
3507 // don't use m_linesPerPage directly as it might not be computed yet
3508 const int pageSize = GetCountPerPage();
3509 wxCHECK_RET( pageSize, _T("should have non zero page size") );
3511 if (GetLayoutDirection() == wxLayout_RightToLeft)
3513 if (event.GetKeyCode() == WXK_RIGHT)
3514 event.m_keyCode = WXK_LEFT;
3515 else if (event.GetKeyCode() == WXK_LEFT)
3516 event.m_keyCode = WXK_RIGHT;
3519 switch ( event.GetKeyCode() )
3521 case WXK_UP:
3522 if ( m_current > 0 )
3523 OnArrowChar( m_current - 1, event );
3524 break;
3526 case WXK_DOWN:
3527 if ( m_current < (size_t)GetItemCount() - 1 )
3528 OnArrowChar( m_current + 1, event );
3529 break;
3531 case WXK_END:
3532 if (!IsEmpty())
3533 OnArrowChar( GetItemCount() - 1, event );
3534 break;
3536 case WXK_HOME:
3537 if (!IsEmpty())
3538 OnArrowChar( 0, event );
3539 break;
3541 case WXK_PAGEUP:
3543 int steps = InReportView() ? pageSize - 1
3544 : m_current % pageSize;
3546 int index = m_current - steps;
3547 if (index < 0)
3548 index = 0;
3550 OnArrowChar( index, event );
3552 break;
3554 case WXK_PAGEDOWN:
3556 int steps = InReportView()
3557 ? pageSize - 1
3558 : pageSize - (m_current % pageSize) - 1;
3560 size_t index = m_current + steps;
3561 size_t count = GetItemCount();
3562 if ( index >= count )
3563 index = count - 1;
3565 OnArrowChar( index, event );
3567 break;
3569 case WXK_LEFT:
3570 if ( !InReportView() )
3572 int index = m_current - pageSize;
3573 if (index < 0)
3574 index = 0;
3576 OnArrowChar( index, event );
3578 break;
3580 case WXK_RIGHT:
3581 if ( !InReportView() )
3583 size_t index = m_current + pageSize;
3585 size_t count = GetItemCount();
3586 if ( index >= count )
3587 index = count - 1;
3589 OnArrowChar( index, event );
3591 break;
3593 case WXK_SPACE:
3594 if ( IsSingleSel() )
3596 if ( event.ControlDown() )
3598 ReverseHighlight(m_current);
3600 else // normal space press
3602 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3605 else // multiple selection
3607 ReverseHighlight(m_current);
3609 break;
3611 case WXK_RETURN:
3612 case WXK_EXECUTE:
3613 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3614 break;
3616 default:
3617 event.Skip();
3621 // ----------------------------------------------------------------------------
3622 // focus handling
3623 // ----------------------------------------------------------------------------
3625 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
3627 if ( GetParent() )
3629 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
3630 event.SetEventObject( GetParent() );
3631 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3632 return;
3635 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3636 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3637 // which are already drawn correctly resulting in horrible flicker - avoid
3638 // it
3639 if ( !m_hasFocus )
3641 m_hasFocus = true;
3643 RefreshSelected();
3647 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
3649 if ( GetParent() )
3651 wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
3652 event.SetEventObject( GetParent() );
3653 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3654 return;
3657 m_hasFocus = false;
3658 RefreshSelected();
3661 void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
3663 if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
3665 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3667 else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
3669 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3671 else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
3673 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3675 else if ( InReportView() && (m_small_image_list))
3677 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3681 void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
3683 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
3685 m_normal_image_list->GetSize( index, width, height );
3687 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
3689 m_small_image_list->GetSize( index, width, height );
3691 else if ( HasFlag(wxLC_LIST) && m_small_image_list )
3693 m_small_image_list->GetSize( index, width, height );
3695 else if ( InReportView() && m_small_image_list )
3697 m_small_image_list->GetSize( index, width, height );
3699 else
3701 width =
3702 height = 0;
3706 int wxListMainWindow::GetTextLength( const wxString &s ) const
3708 wxClientDC dc( wxConstCast(this, wxListMainWindow) );
3709 dc.SetFont( GetFont() );
3711 wxCoord lw;
3712 dc.GetTextExtent( s, &lw, NULL );
3714 return lw + AUTOSIZE_COL_MARGIN;
3717 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
3719 m_dirty = true;
3721 // calc the spacing from the icon size
3722 int width = 0, height = 0;
3724 if ((imageList) && (imageList->GetImageCount()) )
3725 imageList->GetSize(0, width, height);
3727 if (which == wxIMAGE_LIST_NORMAL)
3729 m_normal_image_list = imageList;
3730 m_normal_spacing = width + 8;
3733 if (which == wxIMAGE_LIST_SMALL)
3735 m_small_image_list = imageList;
3736 m_small_spacing = width + 14;
3737 m_lineHeight = 0; // ensure that the line height will be recalc'd
3741 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
3743 m_dirty = true;
3744 if (isSmall)
3745 m_small_spacing = spacing;
3746 else
3747 m_normal_spacing = spacing;
3750 int wxListMainWindow::GetItemSpacing( bool isSmall )
3752 return isSmall ? m_small_spacing : m_normal_spacing;
3755 // ----------------------------------------------------------------------------
3756 // columns
3757 // ----------------------------------------------------------------------------
3759 void wxListMainWindow::SetColumn( int col, wxListItem &item )
3761 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3763 wxCHECK_RET( node, _T("invalid column index in SetColumn") );
3765 if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
3766 item.m_width = GetTextLength( item.m_text );
3768 wxListHeaderData *column = node->GetData();
3769 column->SetItem( item );
3771 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3772 if ( headerWin )
3773 headerWin->m_dirty = true;
3775 m_dirty = true;
3777 // invalidate it as it has to be recalculated
3778 m_headerWidth = 0;
3781 void wxListMainWindow::SetColumnWidth( int col, int width )
3783 wxCHECK_RET( col >= 0 && col < GetColumnCount(),
3784 _T("invalid column index") );
3786 wxCHECK_RET( InReportView(),
3787 _T("SetColumnWidth() can only be called in report mode.") );
3789 m_dirty = true;
3790 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3791 if ( headerWin )
3792 headerWin->m_dirty = true;
3794 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3795 wxCHECK_RET( node, _T("no column?") );
3797 wxListHeaderData *column = node->GetData();
3799 size_t count = GetItemCount();
3801 if (width == wxLIST_AUTOSIZE_USEHEADER)
3803 width = GetTextLength(column->GetText());
3804 width += 2*EXTRA_WIDTH;
3806 // check for column header's image availability
3807 const int image = column->GetImage();
3808 if ( image != -1 )
3810 if ( m_small_image_list )
3812 int ix = 0, iy = 0;
3813 m_small_image_list->GetSize(image, ix, iy);
3814 width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
3818 else if ( width == wxLIST_AUTOSIZE )
3820 if ( IsVirtual() )
3822 // TODO: determine the max width somehow...
3823 width = WIDTH_COL_DEFAULT;
3825 else // !virtual
3827 wxClientDC dc(this);
3828 dc.SetFont( GetFont() );
3830 int max = AUTOSIZE_COL_MARGIN;
3832 // if the cached column width isn't valid then recalculate it
3833 if (m_aColWidths.Item(col)->bNeedsUpdate)
3835 for (size_t i = 0; i < count; i++)
3837 wxListLineData *line = GetLine( i );
3838 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3840 wxCHECK_RET( n, _T("no subitem?") );
3842 wxListItemData *itemData = n->GetData();
3843 wxListItem item;
3845 itemData->GetItem(item);
3846 int itemWidth = GetItemWidthWithImage(&item);
3847 if (itemWidth > max)
3848 max = itemWidth;
3851 m_aColWidths.Item(col)->bNeedsUpdate = false;
3852 m_aColWidths.Item(col)->nMaxWidth = max;
3855 max = m_aColWidths.Item(col)->nMaxWidth;
3856 width = max + AUTOSIZE_COL_MARGIN;
3860 column->SetWidth( width );
3862 // invalidate it as it has to be recalculated
3863 m_headerWidth = 0;
3866 int wxListMainWindow::GetHeaderWidth() const
3868 if ( !m_headerWidth )
3870 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
3872 size_t count = GetColumnCount();
3873 for ( size_t col = 0; col < count; col++ )
3875 self->m_headerWidth += GetColumnWidth(col);
3879 return m_headerWidth;
3882 void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
3884 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3885 wxCHECK_RET( node, _T("invalid column index in GetColumn") );
3887 wxListHeaderData *column = node->GetData();
3888 column->GetItem( item );
3891 int wxListMainWindow::GetColumnWidth( int col ) const
3893 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3894 wxCHECK_MSG( node, 0, _T("invalid column index") );
3896 wxListHeaderData *column = node->GetData();
3897 return column->GetWidth();
3900 // ----------------------------------------------------------------------------
3901 // item state
3902 // ----------------------------------------------------------------------------
3904 void wxListMainWindow::SetItem( wxListItem &item )
3906 long id = item.m_itemId;
3907 wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
3908 _T("invalid item index in SetItem") );
3910 if ( !IsVirtual() )
3912 wxListLineData *line = GetLine((size_t)id);
3913 line->SetItem( item.m_col, item );
3915 // Set item state if user wants
3916 if ( item.m_mask & wxLIST_MASK_STATE )
3917 SetItemState( item.m_itemId, item.m_state, item.m_state );
3919 if (InReportView())
3921 // update the Max Width Cache if needed
3922 int width = GetItemWidthWithImage(&item);
3924 if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
3925 m_aColWidths.Item(item.m_col)->nMaxWidth = width;
3929 // update the item on screen
3930 wxRect rectItem;
3931 GetItemRect(id, rectItem);
3932 RefreshRect(rectItem);
3935 void wxListMainWindow::SetItemStateAll(long state, long stateMask)
3937 if ( IsEmpty() )
3938 return;
3940 // first deal with selection
3941 if ( stateMask & wxLIST_STATE_SELECTED )
3943 // set/clear select state
3944 if ( IsVirtual() )
3946 // optimized version for virtual listctrl.
3947 m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
3948 Refresh();
3950 else if ( state & wxLIST_STATE_SELECTED )
3952 const long count = GetItemCount();
3953 for( long i = 0; i < count; i++ )
3955 SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
3959 else
3961 // clear for non virtual (somewhat optimized by using GetNextItem())
3962 long i = -1;
3963 while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
3965 SetItemState( i, 0, wxLIST_STATE_SELECTED );
3970 if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
3972 // unfocus all: only one item can be focussed, so clearing focus for
3973 // all items is simply clearing focus of the focussed item.
3974 SetItemState(m_current, state, stateMask);
3976 //(setting focus to all items makes no sense, so it is not handled here.)
3979 void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
3981 if ( litem == -1 )
3983 SetItemStateAll(state, stateMask);
3984 return;
3987 wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
3988 _T("invalid list ctrl item index in SetItem") );
3990 size_t oldCurrent = m_current;
3991 size_t item = (size_t)litem; // safe because of the check above
3993 // do we need to change the focus?
3994 if ( stateMask & wxLIST_STATE_FOCUSED )
3996 if ( state & wxLIST_STATE_FOCUSED )
3998 // don't do anything if this item is already focused
3999 if ( item != m_current )
4001 ChangeCurrent(item);
4003 if ( oldCurrent != (size_t)-1 )
4005 if ( IsSingleSel() )
4007 HighlightLine(oldCurrent, false);
4010 RefreshLine(oldCurrent);
4013 RefreshLine( m_current );
4016 else // unfocus
4018 // don't do anything if this item is not focused
4019 if ( item == m_current )
4021 ResetCurrent();
4023 if ( IsSingleSel() )
4025 // we must unselect the old current item as well or we
4026 // might end up with more than one selected item in a
4027 // single selection control
4028 HighlightLine(oldCurrent, false);
4031 RefreshLine( oldCurrent );
4036 // do we need to change the selection state?
4037 if ( stateMask & wxLIST_STATE_SELECTED )
4039 bool on = (state & wxLIST_STATE_SELECTED) != 0;
4041 if ( IsSingleSel() )
4043 if ( on )
4045 // selecting the item also makes it the focused one in the
4046 // single sel mode
4047 if ( m_current != item )
4049 ChangeCurrent(item);
4051 if ( oldCurrent != (size_t)-1 )
4053 HighlightLine( oldCurrent, false );
4054 RefreshLine( oldCurrent );
4058 else // off
4060 // only the current item may be selected anyhow
4061 if ( item != m_current )
4062 return;
4066 if ( HighlightLine(item, on) )
4068 RefreshLine(item);
4073 int wxListMainWindow::GetItemState( long item, long stateMask ) const
4075 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
4076 _T("invalid list ctrl item index in GetItemState()") );
4078 int ret = wxLIST_STATE_DONTCARE;
4080 if ( stateMask & wxLIST_STATE_FOCUSED )
4082 if ( (size_t)item == m_current )
4083 ret |= wxLIST_STATE_FOCUSED;
4086 if ( stateMask & wxLIST_STATE_SELECTED )
4088 if ( IsHighlighted(item) )
4089 ret |= wxLIST_STATE_SELECTED;
4092 return ret;
4095 void wxListMainWindow::GetItem( wxListItem &item ) const
4097 wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
4098 _T("invalid item index in GetItem") );
4100 wxListLineData *line = GetLine((size_t)item.m_itemId);
4101 line->GetItem( item.m_col, item );
4103 // Get item state if user wants it
4104 if ( item.m_mask & wxLIST_MASK_STATE )
4105 item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
4106 wxLIST_STATE_FOCUSED );
4109 // ----------------------------------------------------------------------------
4110 // item count
4111 // ----------------------------------------------------------------------------
4113 size_t wxListMainWindow::GetItemCount() const
4115 return IsVirtual() ? m_countVirt : m_lines.GetCount();
4118 void wxListMainWindow::SetItemCount(long count)
4120 m_selStore.SetItemCount(count);
4121 m_countVirt = count;
4123 ResetVisibleLinesRange();
4125 // scrollbars must be reset
4126 m_dirty = true;
4129 int wxListMainWindow::GetSelectedItemCount() const
4131 // deal with the quick case first
4132 if ( IsSingleSel() )
4133 return HasCurrent() ? IsHighlighted(m_current) : false;
4135 // virtual controls remmebers all its selections itself
4136 if ( IsVirtual() )
4137 return m_selStore.GetSelectedCount();
4139 // TODO: we probably should maintain the number of items selected even for
4140 // non virtual controls as enumerating all lines is really slow...
4141 size_t countSel = 0;
4142 size_t count = GetItemCount();
4143 for ( size_t line = 0; line < count; line++ )
4145 if ( GetLine(line)->IsHighlighted() )
4146 countSel++;
4149 return countSel;
4152 // ----------------------------------------------------------------------------
4153 // item position/size
4154 // ----------------------------------------------------------------------------
4156 wxRect wxListMainWindow::GetViewRect() const
4158 wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
4159 _T("wxListCtrl::GetViewRect() only works in icon mode") );
4161 // we need to find the longest/tallest label
4162 wxCoord xMax = 0, yMax = 0;
4163 const int count = GetItemCount();
4164 if ( count )
4166 for ( int i = 0; i < count; i++ )
4168 wxRect r;
4169 GetItemRect(i, r);
4171 wxCoord x = r.GetRight(),
4172 y = r.GetBottom();
4174 if ( x > xMax )
4175 xMax = x;
4176 if ( y > yMax )
4177 yMax = y;
4181 // some fudge needed to make it look prettier
4182 xMax += 2 * EXTRA_BORDER_X;
4183 yMax += 2 * EXTRA_BORDER_Y;
4185 // account for the scrollbars if necessary
4186 const wxSize sizeAll = GetClientSize();
4187 if ( xMax > sizeAll.x )
4188 yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
4189 if ( yMax > sizeAll.y )
4190 xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
4192 return wxRect(0, 0, xMax, yMax);
4195 void wxListMainWindow::GetItemRect( long index, wxRect &rect ) const
4197 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4198 _T("invalid index in GetItemRect") );
4200 // ensure that we're laid out, otherwise we could return nonsense
4201 if ( m_dirty )
4203 wxConstCast(this, wxListMainWindow)->
4204 RecalculatePositions(true /* no refresh */);
4207 rect = GetLineRect((size_t)index);
4209 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
4212 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
4214 wxRect rect;
4215 GetItemRect(item, rect);
4217 pos.x = rect.x;
4218 pos.y = rect.y;
4220 return true;
4223 // ----------------------------------------------------------------------------
4224 // geometry calculation
4225 // ----------------------------------------------------------------------------
4227 void wxListMainWindow::RecalculatePositions(bool noRefresh)
4229 const int lineHeight = GetLineHeight();
4231 wxClientDC dc( this );
4232 dc.SetFont( GetFont() );
4234 const size_t count = GetItemCount();
4236 int iconSpacing;
4237 if ( HasFlag(wxLC_ICON) )
4238 iconSpacing = m_normal_spacing;
4239 else if ( HasFlag(wxLC_SMALL_ICON) )
4240 iconSpacing = m_small_spacing;
4241 else
4242 iconSpacing = 0;
4244 // Note that we do not call GetClientSize() here but
4245 // GetSize() and subtract the border size for sunken
4246 // borders manually. This is technically incorrect,
4247 // but we need to know the client area's size WITHOUT
4248 // scrollbars here. Since we don't know if there are
4249 // any scrollbars, we use GetSize() instead. Another
4250 // solution would be to call SetScrollbars() here to
4251 // remove the scrollbars and call GetClientSize() then,
4252 // but this might result in flicker and - worse - will
4253 // reset the scrollbars to 0 which is not good at all
4254 // if you resize a dialog/window, but don't want to
4255 // reset the window scrolling. RR.
4256 // Furthermore, we actually do NOT subtract the border
4257 // width as 2 pixels is just the extra space which we
4258 // need around the actual content in the window. Other-
4259 // wise the text would e.g. touch the upper border. RR.
4260 int clientWidth,
4261 clientHeight;
4262 GetSize( &clientWidth, &clientHeight );
4264 if ( InReportView() )
4266 // all lines have the same height and we scroll one line per step
4267 int entireHeight = count * lineHeight + LINE_SPACING;
4269 m_linesPerPage = clientHeight / lineHeight;
4271 ResetVisibleLinesRange();
4273 SetScrollbars( SCROLL_UNIT_X, lineHeight,
4274 GetHeaderWidth() / SCROLL_UNIT_X,
4275 (entireHeight + lineHeight - 1) / lineHeight,
4276 GetScrollPos(wxHORIZONTAL),
4277 GetScrollPos(wxVERTICAL),
4278 true );
4280 else // !report
4282 // we have 3 different layout strategies: either layout all items
4283 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4284 // to arrange them in top to bottom, left to right (don't ask me why
4285 // not the other way round...) order
4286 if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) )
4288 int x = EXTRA_BORDER_X;
4289 int y = EXTRA_BORDER_Y;
4291 wxCoord widthMax = 0;
4293 size_t i;
4294 for ( i = 0; i < count; i++ )
4296 wxListLineData *line = GetLine(i);
4297 line->CalculateSize( &dc, iconSpacing );
4298 line->SetPosition( x, y, iconSpacing );
4300 wxSize sizeLine = GetLineSize(i);
4302 if ( HasFlag(wxLC_ALIGN_TOP) )
4304 if ( sizeLine.x > widthMax )
4305 widthMax = sizeLine.x;
4307 y += sizeLine.y;
4309 else // wxLC_ALIGN_LEFT
4311 x += sizeLine.x + MARGIN_BETWEEN_ROWS;
4315 if ( HasFlag(wxLC_ALIGN_TOP) )
4317 // traverse the items again and tweak their sizes so that they are
4318 // all the same in a row
4319 for ( i = 0; i < count; i++ )
4321 wxListLineData *line = GetLine(i);
4322 line->m_gi->ExtendWidth(widthMax);
4326 SetScrollbars
4328 SCROLL_UNIT_X,
4329 lineHeight,
4330 (x + SCROLL_UNIT_X) / SCROLL_UNIT_X,
4331 (y + lineHeight) / lineHeight,
4332 GetScrollPos( wxHORIZONTAL ),
4333 GetScrollPos( wxVERTICAL ),
4334 true
4337 else // "flowed" arrangement, the most complicated case
4339 // at first we try without any scrollbars, if the items don't fit into
4340 // the window, we recalculate after subtracting the space taken by the
4341 // scrollbar
4343 int entireWidth = 0;
4345 for (int tries = 0; tries < 2; tries++)
4347 entireWidth = 2 * EXTRA_BORDER_X;
4349 if (tries == 1)
4351 // Now we have decided that the items do not fit into the
4352 // client area, so we need a scrollbar
4353 entireWidth += SCROLL_UNIT_X;
4356 int x = EXTRA_BORDER_X;
4357 int y = EXTRA_BORDER_Y;
4358 int maxWidthInThisRow = 0;
4360 m_linesPerPage = 0;
4361 int currentlyVisibleLines = 0;
4363 for (size_t i = 0; i < count; i++)
4365 currentlyVisibleLines++;
4366 wxListLineData *line = GetLine( i );
4367 line->CalculateSize( &dc, iconSpacing );
4368 line->SetPosition( x, y, iconSpacing );
4370 wxSize sizeLine = GetLineSize( i );
4372 if ( maxWidthInThisRow < sizeLine.x )
4373 maxWidthInThisRow = sizeLine.x;
4375 y += sizeLine.y;
4376 if (currentlyVisibleLines > m_linesPerPage)
4377 m_linesPerPage = currentlyVisibleLines;
4379 if ( y + sizeLine.y >= clientHeight )
4381 currentlyVisibleLines = 0;
4382 y = EXTRA_BORDER_Y;
4383 maxWidthInThisRow += MARGIN_BETWEEN_ROWS;
4384 x += maxWidthInThisRow;
4385 entireWidth += maxWidthInThisRow;
4386 maxWidthInThisRow = 0;
4389 // We have reached the last item.
4390 if ( i == count - 1 )
4391 entireWidth += maxWidthInThisRow;
4393 if ( (tries == 0) &&
4394 (entireWidth + SCROLL_UNIT_X > clientWidth) )
4396 clientHeight -= wxSystemSettings::
4397 GetMetric(wxSYS_HSCROLL_Y);
4398 m_linesPerPage = 0;
4399 break;
4402 if ( i == count - 1 )
4403 tries = 1; // Everything fits, no second try required.
4407 SetScrollbars
4409 SCROLL_UNIT_X,
4410 lineHeight,
4411 (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X,
4413 GetScrollPos( wxHORIZONTAL ),
4415 true
4420 if ( !noRefresh )
4422 // FIXME: why should we call it from here?
4423 UpdateCurrent();
4425 RefreshAll();
4429 void wxListMainWindow::RefreshAll()
4431 m_dirty = false;
4432 Refresh();
4434 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
4435 if ( headerWin && headerWin->m_dirty )
4437 headerWin->m_dirty = false;
4438 headerWin->Refresh();
4442 void wxListMainWindow::UpdateCurrent()
4444 if ( !HasCurrent() && !IsEmpty() )
4445 ChangeCurrent(0);
4448 long wxListMainWindow::GetNextItem( long item,
4449 int WXUNUSED(geometry),
4450 int state ) const
4452 long ret = item,
4453 max = GetItemCount();
4454 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
4455 _T("invalid listctrl index in GetNextItem()") );
4457 // notice that we start with the next item (or the first one if item == -1)
4458 // and this is intentional to allow writing a simple loop to iterate over
4459 // all selected items
4460 ret++;
4461 if ( ret == max )
4462 // this is not an error because the index was OK initially,
4463 // just no such item
4464 return -1;
4466 if ( !state )
4467 // any will do
4468 return (size_t)ret;
4470 size_t count = GetItemCount();
4471 for ( size_t line = (size_t)ret; line < count; line++ )
4473 if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
4474 return line;
4476 if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
4477 return line;
4480 return -1;
4483 // ----------------------------------------------------------------------------
4484 // deleting stuff
4485 // ----------------------------------------------------------------------------
4487 void wxListMainWindow::DeleteItem( long lindex )
4489 size_t count = GetItemCount();
4491 wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
4492 _T("invalid item index in DeleteItem") );
4494 size_t index = (size_t)lindex;
4496 // we don't need to adjust the index for the previous items
4497 if ( HasCurrent() && m_current >= index )
4499 // if the current item is being deleted, we want the next one to
4500 // become selected - unless there is no next one - so don't adjust
4501 // m_current in this case
4502 if ( m_current != index || m_current == count - 1 )
4503 m_current--;
4506 if ( InReportView() )
4508 // mark the Column Max Width cache as dirty if the items in the line
4509 // we're deleting contain the Max Column Width
4510 wxListLineData * const line = GetLine(index);
4511 wxListItemDataList::compatibility_iterator n;
4512 wxListItemData *itemData;
4513 wxListItem item;
4514 int itemWidth;
4516 for (size_t i = 0; i < m_columns.GetCount(); i++)
4518 n = line->m_items.Item( i );
4519 itemData = n->GetData();
4520 itemData->GetItem(item);
4522 itemWidth = GetItemWidthWithImage(&item);
4524 if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth)
4525 m_aColWidths.Item(i)->bNeedsUpdate = true;
4528 ResetVisibleLinesRange();
4531 SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM, wxDefaultPosition );
4533 if ( IsVirtual() )
4535 m_countVirt--;
4536 m_selStore.OnItemDelete(index);
4538 else
4540 m_lines.RemoveAt( index );
4543 // we need to refresh the (vert) scrollbar as the number of items changed
4544 m_dirty = true;
4546 RefreshAfter(index);
4549 void wxListMainWindow::DeleteColumn( int col )
4551 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
4553 wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
4555 m_dirty = true;
4556 delete node->GetData();
4557 m_columns.Erase( node );
4559 if ( !IsVirtual() )
4561 // update all the items
4562 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4564 wxListLineData * const line = GetLine(i);
4565 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
4566 delete n->GetData();
4567 line->m_items.Erase(n);
4571 if ( InReportView() ) // we only cache max widths when in Report View
4573 delete m_aColWidths.Item(col);
4574 m_aColWidths.RemoveAt(col);
4577 // invalidate it as it has to be recalculated
4578 m_headerWidth = 0;
4581 void wxListMainWindow::DoDeleteAllItems()
4583 if ( IsEmpty() )
4584 // nothing to do - in particular, don't send the event
4585 return;
4587 ResetCurrent();
4589 // to make the deletion of all items faster, we don't send the
4590 // notifications for each item deletion in this case but only one event
4591 // for all of them: this is compatible with wxMSW and documented in
4592 // DeleteAllItems() description
4594 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
4595 event.SetEventObject( GetParent() );
4596 GetParent()->GetEventHandler()->ProcessEvent( event );
4598 if ( IsVirtual() )
4600 m_countVirt = 0;
4601 m_selStore.Clear();
4604 if ( InReportView() )
4606 ResetVisibleLinesRange();
4607 for (size_t i = 0; i < m_aColWidths.GetCount(); i++)
4609 m_aColWidths.Item(i)->bNeedsUpdate = true;
4613 m_lines.Clear();
4616 void wxListMainWindow::DeleteAllItems()
4618 DoDeleteAllItems();
4620 RecalculatePositions();
4623 void wxListMainWindow::DeleteEverything()
4625 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
4626 WX_CLEAR_ARRAY(m_aColWidths);
4628 DeleteAllItems();
4631 // ----------------------------------------------------------------------------
4632 // scanning for an item
4633 // ----------------------------------------------------------------------------
4635 void wxListMainWindow::EnsureVisible( long index )
4637 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4638 _T("invalid index in EnsureVisible") );
4640 // We have to call this here because the label in question might just have
4641 // been added and its position is not known yet
4642 if ( m_dirty )
4643 RecalculatePositions(true /* no refresh */);
4645 MoveToItem((size_t)index);
4648 long wxListMainWindow::FindItem(long start, const wxString& str, bool partial )
4650 if (str.empty())
4651 return wxNOT_FOUND;
4653 long pos = start;
4654 wxString str_upper = str.Upper();
4655 if (pos < 0)
4656 pos = 0;
4658 size_t count = GetItemCount();
4659 for ( size_t i = (size_t)pos; i < count; i++ )
4661 wxListLineData *line = GetLine(i);
4662 wxString line_upper = line->GetText(0).Upper();
4663 if (!partial)
4665 if (line_upper == str_upper )
4666 return i;
4668 else
4670 if (line_upper.find(str_upper) == 0)
4671 return i;
4675 return wxNOT_FOUND;
4678 long wxListMainWindow::FindItem(long start, wxUIntPtr data)
4680 long pos = start;
4681 if (pos < 0)
4682 pos = 0;
4684 size_t count = GetItemCount();
4685 for (size_t i = (size_t)pos; i < count; i++)
4687 wxListLineData *line = GetLine(i);
4688 wxListItem item;
4689 line->GetItem( 0, item );
4690 if (item.m_data == data)
4691 return i;
4694 return wxNOT_FOUND;
4697 long wxListMainWindow::FindItem( const wxPoint& pt )
4699 size_t topItem;
4700 GetVisibleLinesRange( &topItem, NULL );
4702 wxPoint p;
4703 GetItemPosition( GetItemCount() - 1, p );
4704 if ( p.y == 0 )
4705 return topItem;
4707 long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem );
4708 if ( id >= 0 && id < (long)GetItemCount() )
4709 return id;
4711 return wxNOT_FOUND;
4714 long wxListMainWindow::HitTest( int x, int y, int &flags ) const
4716 CalcUnscrolledPosition( x, y, &x, &y );
4718 size_t count = GetItemCount();
4720 if ( InReportView() )
4722 size_t current = y / GetLineHeight();
4723 if ( current < count )
4725 flags = HitTestLine(current, x, y);
4726 if ( flags )
4727 return current;
4730 else // !report
4732 // TODO: optimize it too! this is less simple than for report view but
4733 // enumerating all items is still not a way to do it!!
4734 for ( size_t current = 0; current < count; current++ )
4736 flags = HitTestLine(current, x, y);
4737 if ( flags )
4738 return current;
4742 return wxNOT_FOUND;
4745 // ----------------------------------------------------------------------------
4746 // adding stuff
4747 // ----------------------------------------------------------------------------
4749 void wxListMainWindow::InsertItem( wxListItem &item )
4751 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4753 int count = GetItemCount();
4754 wxCHECK_RET( item.m_itemId >= 0, _T("invalid item index") );
4756 if (item.m_itemId > count)
4757 item.m_itemId = count;
4759 size_t id = item.m_itemId;
4761 m_dirty = true;
4763 if ( InReportView() )
4765 ResetVisibleLinesRange();
4767 // calculate the width of the item and adjust the max column width
4768 wxColWidthInfo *pWidthInfo = m_aColWidths.Item(item.GetColumn());
4769 int width = GetItemWidthWithImage(&item);
4770 item.SetWidth(width);
4771 if (width > pWidthInfo->nMaxWidth)
4772 pWidthInfo->nMaxWidth = width;
4775 wxListLineData *line = new wxListLineData(this);
4777 line->SetItem( item.m_col, item );
4779 m_lines.Insert( line, id );
4781 m_dirty = true;
4783 // If an item is selected at or below the point of insertion, we need to
4784 // increment the member variables because the current row's index has gone
4785 // up by one
4786 if ( HasCurrent() && m_current >= id )
4787 m_current++;
4789 SendNotify(id, wxEVT_COMMAND_LIST_INSERT_ITEM);
4791 RefreshLines(id, GetItemCount() - 1);
4794 void wxListMainWindow::InsertColumn( long col, wxListItem &item )
4796 m_dirty = true;
4797 if ( InReportView() )
4799 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
4800 item.m_width = GetTextLength( item.m_text );
4802 wxListHeaderData *column = new wxListHeaderData( item );
4803 wxColWidthInfo *colWidthInfo = new wxColWidthInfo();
4805 bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4806 if ( insert )
4808 wxListHeaderDataList::compatibility_iterator
4809 node = m_columns.Item( col );
4810 m_columns.Insert( node, column );
4811 m_aColWidths.Insert( colWidthInfo, col );
4813 else
4815 m_columns.Append( column );
4816 m_aColWidths.Add( colWidthInfo );
4819 if ( !IsVirtual() )
4821 // update all the items
4822 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4824 wxListLineData * const line = GetLine(i);
4825 wxListItemData * const data = new wxListItemData(this);
4826 if ( insert )
4827 line->m_items.Insert(col, data);
4828 else
4829 line->m_items.Append(data);
4833 // invalidate it as it has to be recalculated
4834 m_headerWidth = 0;
4838 int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
4840 int width = 0;
4841 wxClientDC dc(this);
4843 dc.SetFont( GetFont() );
4845 if (item->GetImage() != -1)
4847 int ix, iy;
4848 GetImageSize( item->GetImage(), ix, iy );
4849 width += ix + 5;
4852 if (!item->GetText().empty())
4854 wxCoord w;
4855 dc.GetTextExtent( item->GetText(), &w, NULL );
4856 width += w;
4859 return width;
4862 // ----------------------------------------------------------------------------
4863 // sorting
4864 // ----------------------------------------------------------------------------
4866 MuleListCtrlCompare list_ctrl_compare_func_2;
4867 long list_ctrl_compare_data;
4869 int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
4871 wxListLineData *line1 = *arg1;
4872 wxListLineData *line2 = *arg2;
4873 wxListItem item;
4874 line1->GetItem( 0, item );
4875 wxUIntPtr data1 = item.m_data;
4876 line2->GetItem( 0, item );
4877 wxUIntPtr data2 = item.m_data;
4878 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
4881 void wxListMainWindow::SortItems( MuleListCtrlCompare fn, long data )
4883 // selections won't make sense any more after sorting the items so reset
4884 // them
4885 HighlightAll(false);
4886 ResetCurrent();
4888 list_ctrl_compare_func_2 = fn;
4889 list_ctrl_compare_data = data;
4890 m_lines.Sort( list_ctrl_compare_func_1 );
4891 m_dirty = true;
4894 // ----------------------------------------------------------------------------
4895 // scrolling
4896 // ----------------------------------------------------------------------------
4898 void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4900 // FIXME
4901 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4902 wxScrolledWindow::OnScroll(event);
4903 #else
4904 HandleOnScroll( event );
4905 #endif
4907 // update our idea of which lines are shown when we redraw the window the
4908 // next time
4909 ResetVisibleLinesRange();
4911 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4913 wxGenericListCtrl* lc = GetListCtrl();
4914 wxCHECK_RET( lc, _T("no listctrl window?") );
4916 lc->m_headerWin->Refresh();
4917 lc->m_headerWin->Update();
4921 int wxListMainWindow::GetCountPerPage() const
4923 if ( !m_linesPerPage )
4925 wxConstCast(this, wxListMainWindow)->
4926 m_linesPerPage = GetClientSize().y / GetLineHeight();
4929 return m_linesPerPage;
4932 void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
4934 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4936 if ( m_lineFrom == (size_t)-1 )
4938 size_t count = GetItemCount();
4939 if ( count )
4941 m_lineFrom = GetScrollPos(wxVERTICAL);
4943 // this may happen if SetScrollbars() hadn't been called yet
4944 if ( m_lineFrom >= count )
4945 m_lineFrom = count - 1;
4947 // we redraw one extra line but this is needed to make the redrawing
4948 // logic work when there is a fractional number of lines on screen
4949 m_lineTo = m_lineFrom + m_linesPerPage;
4950 if ( m_lineTo >= count )
4951 m_lineTo = count - 1;
4953 else // empty control
4955 m_lineFrom = 0;
4956 m_lineTo = (size_t)-1;
4960 wxASSERT_MSG( IsEmpty() ||
4961 (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
4962 _T("GetVisibleLinesRange() returns incorrect result") );
4964 if ( from )
4965 *from = m_lineFrom;
4966 if ( to )
4967 *to = m_lineTo;
4970 // -------------------------------------------------------------------------------------
4971 // wxGenericListCtrl
4972 // -------------------------------------------------------------------------------------
4974 BEGIN_EVENT_TABLE(wxGenericListCtrl,wxControl)
4975 EVT_SIZE(wxGenericListCtrl::OnSize)
4976 END_EVENT_TABLE()
4978 wxGenericListCtrl::wxGenericListCtrl()
4980 m_imageListNormal = (wxImageList *) NULL;
4981 m_imageListSmall = (wxImageList *) NULL;
4982 m_imageListState = (wxImageList *) NULL;
4984 m_ownsImageListNormal =
4985 m_ownsImageListSmall =
4986 m_ownsImageListState = false;
4988 m_mainWin = (wxListMainWindow*) NULL;
4989 m_headerWin = (wxListHeaderWindow*) NULL;
4990 m_headerHeight = 0;
4993 wxGenericListCtrl::~wxGenericListCtrl()
4995 if (m_ownsImageListNormal)
4996 delete m_imageListNormal;
4997 if (m_ownsImageListSmall)
4998 delete m_imageListSmall;
4999 if (m_ownsImageListState)
5000 delete m_imageListState;
5003 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
5005 if ( m_headerWin )
5007 #ifdef __WXMAC__
5008 SInt32 h;
5009 GetThemeMetric( kThemeMetricListHeaderHeight, &h );
5010 #else
5011 // we use 'g' to get the descent, too
5012 int w, h, d;
5013 m_headerWin->GetTextExtent(wxT("Hg"), &w, &h, &d);
5014 h += d + 2 * HEADER_OFFSET_Y + EXTRA_HEIGHT;
5015 #endif
5017 // only update if changed
5018 if ( h != m_headerHeight )
5020 m_headerHeight = h;
5022 if ( HasHeader() )
5023 ResizeReportView(true);
5024 else //why is this needed if it doesn't have a header?
5025 m_headerWin->SetSize(m_headerWin->GetSize().x, m_headerHeight);
5030 void wxGenericListCtrl::CreateHeaderWindow()
5032 m_headerWin = new wxListHeaderWindow
5034 this, wxID_ANY, m_mainWin,
5035 wxPoint(0,0),
5036 wxSize(GetClientSize().x, m_headerHeight),
5037 wxTAB_TRAVERSAL
5039 CalculateAndSetHeaderHeight();
5042 bool wxGenericListCtrl::GetFocus()
5044 return m_mainWin->m_hasFocus;
5047 bool wxGenericListCtrl::Create(wxWindow *parent,
5048 wxWindowID id,
5049 const wxPoint &pos,
5050 const wxSize &size,
5051 long style,
5052 const wxValidator &validator,
5053 const wxString &name)
5055 m_imageListNormal =
5056 m_imageListSmall =
5057 m_imageListState = (wxImageList *) NULL;
5058 m_ownsImageListNormal =
5059 m_ownsImageListSmall =
5060 m_ownsImageListState = false;
5062 m_mainWin = (wxListMainWindow*) NULL;
5063 m_headerWin = (wxListHeaderWindow*) NULL;
5065 m_headerHeight = 0;
5067 if ( !(style & wxLC_MASK_TYPE) )
5069 style = style | wxLC_LIST;
5072 // add more styles here that should only appear
5073 // in the main window
5074 unsigned long only_main_window_style = wxALWAYS_SHOW_SB;
5076 if ( !wxControl::Create( parent, id, pos, size, style & ~only_main_window_style, validator, name ) )
5077 return false;
5079 // don't create the inner window with the border
5080 style &= ~wxBORDER_MASK;
5082 m_mainWin = new wxListMainWindow( this, wxID_ANY, wxPoint(0, 0), size, style );
5084 #ifdef __WXMAC_CARBON__
5085 // Human Interface Guidelines ask us for a special font in this case
5086 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL )
5088 wxFont font;
5089 font.MacCreateThemeFont( kThemeViewsFont );
5090 SetFont( font );
5092 #endif
5094 if ( InReportView() )
5096 CreateHeaderWindow();
5098 #ifdef __WXMAC_CARBON__
5099 if (m_headerWin)
5101 wxFont font;
5102 font.MacCreateThemeFont( kThemeSmallSystemFont );
5103 m_headerWin->SetFont( font );
5104 CalculateAndSetHeaderHeight();
5106 #endif
5108 if ( HasFlag(wxLC_NO_HEADER) )
5109 // VZ: why do we create it at all then?
5110 m_headerWin->Show( false );
5113 SetInitialSize(size);
5115 return true;
5118 void wxGenericListCtrl::SetSingleStyle( long style, bool add )
5120 wxASSERT_MSG( !(style & wxLC_VIRTUAL),
5121 _T("wxLC_VIRTUAL can't be [un]set") );
5123 long flag = GetWindowStyle();
5125 if (add)
5127 if (style & wxLC_MASK_TYPE)
5128 flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
5129 if (style & wxLC_MASK_ALIGN)
5130 flag &= ~wxLC_MASK_ALIGN;
5131 if (style & wxLC_MASK_SORT)
5132 flag &= ~wxLC_MASK_SORT;
5135 if (add)
5136 flag |= style;
5137 else
5138 flag &= ~style;
5140 SetWindowStyleFlag( flag );
5143 void wxGenericListCtrl::SetWindowStyleFlag( long flag )
5145 if (m_mainWin)
5147 m_mainWin->DeleteEverything();
5149 // has the header visibility changed?
5150 bool hasHeader = HasHeader();
5151 bool willHaveHeader = (flag & wxLC_REPORT) && !(flag & wxLC_NO_HEADER);
5153 if ( hasHeader != willHaveHeader )
5155 // toggle it
5156 if ( hasHeader )
5158 if ( m_headerWin )
5160 // don't delete, just hide, as we can reuse it later
5161 m_headerWin->Show(false);
5163 //else: nothing to do
5165 else // must show header
5167 if (!m_headerWin)
5169 CreateHeaderWindow();
5171 else // already have it, just show
5173 m_headerWin->Show( true );
5177 ResizeReportView(willHaveHeader);
5181 wxWindow::SetWindowStyleFlag( flag );
5184 bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
5186 m_mainWin->GetColumn( col, item );
5187 return true;
5190 bool wxGenericListCtrl::SetColumn( int col, wxListItem& item )
5192 m_mainWin->SetColumn( col, item );
5193 return true;
5196 int wxGenericListCtrl::GetColumnWidth( int col ) const
5198 return m_mainWin->GetColumnWidth( col );
5201 bool wxGenericListCtrl::SetColumnWidth( int col, int width )
5203 m_mainWin->SetColumnWidth( col, width );
5204 return true;
5207 int wxGenericListCtrl::GetCountPerPage() const
5209 return m_mainWin->GetCountPerPage(); // different from Windows ?
5212 bool wxGenericListCtrl::GetItem( wxListItem &info ) const
5214 m_mainWin->GetItem( info );
5215 return true;
5218 bool wxGenericListCtrl::SetItem( wxListItem &info )
5220 m_mainWin->SetItem( info );
5221 return true;
5224 long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
5226 wxListItem info;
5227 info.m_text = label;
5228 info.m_mask = wxLIST_MASK_TEXT;
5229 info.m_itemId = index;
5230 info.m_col = col;
5231 if ( imageId > -1 )
5233 info.m_image = imageId;
5234 info.m_mask |= wxLIST_MASK_IMAGE;
5237 m_mainWin->SetItem(info);
5238 return true;
5241 int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
5243 return m_mainWin->GetItemState( item, stateMask );
5246 bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
5248 m_mainWin->SetItemState( item, state, stateMask );
5249 return true;
5252 bool
5253 wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
5255 return SetItemColumnImage(item, 0, image);
5258 bool
5259 wxGenericListCtrl::SetItemColumnImage( long item, long column, int image )
5261 wxListItem info;
5262 info.m_image = image;
5263 info.m_mask = wxLIST_MASK_IMAGE;
5264 info.m_itemId = item;
5265 info.m_col = column;
5266 m_mainWin->SetItem( info );
5267 return true;
5270 wxString wxGenericListCtrl::GetItemText( long item ) const
5272 return m_mainWin->GetItemText(item);
5275 void wxGenericListCtrl::SetItemText( long item, const wxString& str )
5277 m_mainWin->SetItemText(item, str);
5280 wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const
5282 wxListItem info;
5283 info.m_mask = wxLIST_MASK_DATA;
5284 info.m_itemId = item;
5285 m_mainWin->GetItem( info );
5286 return info.m_data;
5289 bool wxGenericListCtrl::SetItemPtrData( long item, wxUIntPtr data )
5291 wxListItem info;
5292 info.m_mask = wxLIST_MASK_DATA;
5293 info.m_itemId = item;
5294 info.m_data = data;
5295 m_mainWin->SetItem( info );
5296 return true;
5299 #if 0
5300 bool wxGenericListCtrl::SetItemData(long item, long data)
5302 return SetItemPtrData(item, data);
5304 #endif
5306 wxRect wxGenericListCtrl::GetViewRect() const
5308 return m_mainWin->GetViewRect();
5311 bool wxGenericListCtrl::GetItemRect( long item, wxRect &rect, int WXUNUSED(code) ) const
5313 m_mainWin->GetItemRect( item, rect );
5314 if ( m_mainWin->HasHeader() )
5315 rect.y += m_headerHeight + 1;
5316 return true;
5319 bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
5321 m_mainWin->GetItemPosition( item, pos );
5322 return true;
5325 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
5327 return 0;
5330 int wxGenericListCtrl::GetItemCount() const
5332 return m_mainWin->GetItemCount();
5335 int wxGenericListCtrl::GetColumnCount() const
5337 return m_mainWin->GetColumnCount();
5340 void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
5342 m_mainWin->SetItemSpacing( spacing, isSmall );
5345 wxSize wxGenericListCtrl::GetItemSpacing() const
5347 const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON));
5349 return wxSize(spacing, spacing);
5352 #if WXWIN_COMPATIBILITY_2_6
5353 int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const
5355 return m_mainWin->GetItemSpacing( isSmall );
5357 #endif // WXWIN_COMPATIBILITY_2_6
5359 void wxGenericListCtrl::OnDrawItem(int WXUNUSED(item), wxDC* WXUNUSED(dc), const wxRect& WXUNUSED(rect), const wxRect& WXUNUSED(rectHL), bool WXUNUSED(highlighted))
5361 // do nothing here, this is just a stub
5364 void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
5366 wxListItem info;
5367 info.m_itemId = item;
5368 info.SetTextColour( col );
5369 m_mainWin->SetItem( info );
5372 wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
5374 wxListItem info;
5375 info.m_itemId = item;
5376 m_mainWin->GetItem( info );
5377 return info.GetTextColour();
5380 void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
5382 wxListItem info;
5383 info.m_itemId = item;
5384 info.SetBackgroundColour( col );
5385 m_mainWin->SetItem( info );
5388 wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
5390 wxListItem info;
5391 info.m_itemId = item;
5392 m_mainWin->GetItem( info );
5393 return info.GetBackgroundColour();
5396 int wxGenericListCtrl::GetScrollPos( int orient ) const
5398 return m_mainWin->GetScrollPos( orient );
5401 void wxGenericListCtrl::SetScrollPos( int orient, int pos, bool refresh )
5403 m_mainWin->SetScrollPos( orient, pos, refresh );
5406 void wxGenericListCtrl::SetItemFont( long item, const wxFont &f )
5408 wxListItem info;
5409 info.m_itemId = item;
5410 info.SetFont( f );
5411 m_mainWin->SetItem( info );
5414 wxFont wxGenericListCtrl::GetItemFont( long item ) const
5416 wxListItem info;
5417 info.m_itemId = item;
5418 m_mainWin->GetItem( info );
5419 return info.GetFont();
5422 int wxGenericListCtrl::GetSelectedItemCount() const
5424 return m_mainWin->GetSelectedItemCount();
5427 wxColour wxGenericListCtrl::GetTextColour() const
5429 return GetForegroundColour();
5432 void wxGenericListCtrl::SetTextColour(const wxColour& col)
5434 SetForegroundColour(col);
5437 long wxGenericListCtrl::GetTopItem() const
5439 size_t top;
5440 m_mainWin->GetVisibleLinesRange(&top, NULL);
5441 return (long)top;
5444 void wxGenericListCtrl::GetVisibleLines(long* first, long* last)
5446 size_t from, to;
5448 m_mainWin->GetVisibleLinesRange(&from, &to);
5450 if (first) *first = (long)from;
5451 if (last) *last = (long)to;
5454 long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
5456 return m_mainWin->GetNextItem( item, geom, state );
5459 wxImageList *wxGenericListCtrl::GetImageList(int which) const
5461 if (which == wxIMAGE_LIST_NORMAL)
5462 return m_imageListNormal;
5463 else if (which == wxIMAGE_LIST_SMALL)
5464 return m_imageListSmall;
5465 else if (which == wxIMAGE_LIST_STATE)
5466 return m_imageListState;
5468 return (wxImageList *) NULL;
5471 void wxGenericListCtrl::SetImageList( wxImageList *imageList, int which )
5473 if ( which == wxIMAGE_LIST_NORMAL )
5475 if (m_ownsImageListNormal)
5476 delete m_imageListNormal;
5477 m_imageListNormal = imageList;
5478 m_ownsImageListNormal = false;
5480 else if ( which == wxIMAGE_LIST_SMALL )
5482 if (m_ownsImageListSmall)
5483 delete m_imageListSmall;
5484 m_imageListSmall = imageList;
5485 m_ownsImageListSmall = false;
5487 else if ( which == wxIMAGE_LIST_STATE )
5489 if (m_ownsImageListState)
5490 delete m_imageListState;
5491 m_imageListState = imageList;
5492 m_ownsImageListState = false;
5495 m_mainWin->SetImageList( imageList, which );
5498 void wxGenericListCtrl::AssignImageList(wxImageList *imageList, int which)
5500 SetImageList(imageList, which);
5501 if ( which == wxIMAGE_LIST_NORMAL )
5502 m_ownsImageListNormal = true;
5503 else if ( which == wxIMAGE_LIST_SMALL )
5504 m_ownsImageListSmall = true;
5505 else if ( which == wxIMAGE_LIST_STATE )
5506 m_ownsImageListState = true;
5509 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
5511 return 0;
5514 bool wxGenericListCtrl::DeleteItem( long item )
5516 m_mainWin->DeleteItem( item );
5517 return true;
5520 bool wxGenericListCtrl::DeleteAllItems()
5522 m_mainWin->DeleteAllItems();
5523 return true;
5526 bool wxGenericListCtrl::DeleteAllColumns()
5528 size_t count = m_mainWin->m_columns.GetCount();
5529 for ( size_t n = 0; n < count; n++ )
5530 DeleteColumn( 0 );
5531 return true;
5534 void wxGenericListCtrl::ClearAll()
5536 m_mainWin->DeleteEverything();
5539 bool wxGenericListCtrl::DeleteColumn( int col )
5541 m_mainWin->DeleteColumn( col );
5543 // if we don't have the header any longer, we need to relayout the window
5544 if ( !GetColumnCount() )
5545 ResizeReportView(false /* no header */);
5546 return true;
5549 wxTextCtrl *wxGenericListCtrl::EditLabel(long item,
5550 wxClassInfo* textControlClass)
5552 return m_mainWin->EditLabel( item, textControlClass );
5555 wxTextCtrl *wxGenericListCtrl::GetEditControl() const
5557 return m_mainWin->GetEditControl();
5560 bool wxGenericListCtrl::EnsureVisible( long item )
5562 m_mainWin->EnsureVisible( item );
5563 return true;
5566 long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
5568 return m_mainWin->FindItem( start, str, partial );
5571 long wxGenericListCtrl::FindItem( long start, wxUIntPtr data )
5573 return m_mainWin->FindItem( start, data );
5576 long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt,
5577 int WXUNUSED(direction))
5579 return m_mainWin->FindItem( pt );
5582 // TODO: sub item hit testing
5583 long wxGenericListCtrl::HitTest(const wxPoint& point, int& flags, long *) const
5585 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
5588 long wxGenericListCtrl::InsertItem( wxListItem& info )
5590 m_mainWin->InsertItem( info );
5591 return info.m_itemId;
5594 long wxGenericListCtrl::InsertItem( long index, const wxString &label )
5596 wxListItem info;
5597 info.m_text = label;
5598 info.m_mask = wxLIST_MASK_TEXT;
5599 info.m_itemId = index;
5600 return InsertItem( info );
5603 long wxGenericListCtrl::InsertItem( long index, int imageIndex )
5605 wxListItem info;
5606 info.m_mask = wxLIST_MASK_IMAGE;
5607 info.m_image = imageIndex;
5608 info.m_itemId = index;
5609 return InsertItem( info );
5612 long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
5614 wxListItem info;
5615 info.m_text = label;
5616 info.m_image = imageIndex;
5617 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
5618 info.m_itemId = index;
5619 return InsertItem( info );
5622 long wxGenericListCtrl::InsertColumn( long col, wxListItem &item )
5624 wxCHECK_MSG( m_headerWin, -1, _T("can't add column in non report mode") );
5626 m_mainWin->InsertColumn( col, item );
5628 // if we hadn't had a header before but have one now
5629 // then we need to relayout the window
5630 if ( GetColumnCount() == 1 && m_mainWin->HasHeader() )
5631 ResizeReportView(true /* have header */);
5633 m_headerWin->Refresh();
5635 return 0;
5638 long wxGenericListCtrl::InsertColumn( long col, const wxString &heading,
5639 int format, int width )
5641 wxListItem item;
5642 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
5643 item.m_text = heading;
5644 if (width >= -2)
5646 item.m_mask |= wxLIST_MASK_WIDTH;
5647 item.m_width = width;
5650 item.m_format = format;
5652 return InsertColumn( col, item );
5655 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx), int WXUNUSED(dy) )
5657 return 0;
5660 // Sort items.
5661 // fn is a function which takes 3 long arguments: item1, item2, data.
5662 // item1 is the long data associated with a first item (NOT the index).
5663 // item2 is the long data associated with a second item (NOT the index).
5664 // data is the same value as passed to SortItems.
5665 // The return value is a negative number if the first item should precede the second
5666 // item, a positive number of the second item should precede the first,
5667 // or zero if the two items are equivalent.
5668 // data is arbitrary data to be passed to the sort function.
5670 bool wxGenericListCtrl::SortItems( MuleListCtrlCompare fn, long data )
5672 m_mainWin->SortItems( fn, data );
5673 return true;
5676 // ----------------------------------------------------------------------------
5677 // event handlers
5678 // ----------------------------------------------------------------------------
5680 void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
5682 if ( !m_mainWin )
5683 return;
5685 ResizeReportView(m_mainWin->HasHeader());
5686 m_mainWin->RecalculatePositions();
5689 void wxGenericListCtrl::ResizeReportView(bool showHeader)
5691 int cw, ch;
5692 GetClientSize( &cw, &ch );
5694 if ( showHeader )
5696 m_headerWin->SetSize( 0, 0, cw, m_headerHeight );
5697 if(ch > m_headerHeight)
5698 m_mainWin->SetSize( 0, m_headerHeight + 1,
5699 cw, ch - m_headerHeight - 1 );
5700 else
5701 m_mainWin->SetSize( 0, m_headerHeight + 1,
5702 cw, 0);
5704 else // no header window
5706 m_mainWin->SetSize( 0, 0, cw, ch );
5710 void wxGenericListCtrl::OnInternalIdle()
5712 wxWindow::OnInternalIdle();
5714 // do it only if needed
5715 if ( !m_mainWin->m_dirty )
5716 return;
5718 m_mainWin->RecalculatePositions();
5721 // ----------------------------------------------------------------------------
5722 // font/colours
5723 // ----------------------------------------------------------------------------
5725 bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
5727 if (m_mainWin)
5729 m_mainWin->SetBackgroundColour( colour );
5730 m_mainWin->m_dirty = true;
5733 return true;
5736 bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
5738 if ( !wxWindow::SetForegroundColour( colour ) )
5739 return false;
5741 if (m_mainWin)
5743 m_mainWin->SetForegroundColour( colour );
5744 m_mainWin->m_dirty = true;
5747 if (m_headerWin)
5748 m_headerWin->SetForegroundColour( colour );
5750 return true;
5753 bool wxGenericListCtrl::SetFont( const wxFont &font )
5755 if ( !wxWindow::SetFont( font ) )
5756 return false;
5758 if (m_mainWin)
5760 m_mainWin->SetFont( font );
5761 m_mainWin->m_dirty = true;
5764 if (m_headerWin)
5766 m_headerWin->SetFont( font );
5767 CalculateAndSetHeaderHeight();
5770 Refresh();
5772 return true;
5775 // static
5776 wxVisualAttributes
5777 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
5779 #if _USE_VISATTR
5780 // Use the same color scheme as wxListBox
5781 return wxListBox::GetClassDefaultAttributes(variant);
5782 #else
5783 wxUnusedVar(variant);
5784 wxVisualAttributes attr;
5785 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
5786 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
5787 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
5788 return attr;
5789 #endif
5792 // ----------------------------------------------------------------------------
5793 // methods forwarded to m_mainWin
5794 // ----------------------------------------------------------------------------
5796 #if wxUSE_DRAG_AND_DROP
5798 void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
5800 m_mainWin->SetDropTarget( dropTarget );
5803 wxDropTarget *wxGenericListCtrl::GetDropTarget() const
5805 return m_mainWin->GetDropTarget();
5808 #endif
5810 bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
5812 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false;
5815 wxColour wxGenericListCtrl::GetBackgroundColour() const
5817 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5820 wxColour wxGenericListCtrl::GetForegroundColour() const
5822 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5825 bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
5827 #if wxUSE_MENUS
5828 return m_mainWin->PopupMenu( menu, x, y );
5829 #else
5830 return false;
5831 #endif
5834 void wxGenericListCtrl::DoClientToScreen( int *x, int *y ) const
5836 m_mainWin->DoClientToScreen(x, y);
5839 void wxGenericListCtrl::DoScreenToClient( int *x, int *y ) const
5841 m_mainWin->DoScreenToClient(x, y);
5844 void wxGenericListCtrl::SetFocus()
5846 // The test in window.cpp fails as we are a composite
5847 // window, so it checks against "this", but not m_mainWin.
5848 if ( DoFindFocus() != this )
5849 m_mainWin->SetFocus();
5852 wxSize wxGenericListCtrl::DoGetBestSize() const
5854 // Something is better than nothing...
5855 // 100x80 is what the MSW version will get from the default
5856 // wxControl::DoGetBestSize
5857 return wxSize(100, 80);
5860 // ----------------------------------------------------------------------------
5861 // virtual list control support
5862 // ----------------------------------------------------------------------------
5864 wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
5866 // this is a pure virtual function, in fact - which is not really pure
5867 // because the controls which are not virtual don't need to implement it
5868 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5870 return wxEmptyString;
5873 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
5875 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
5877 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5878 return -1;
5881 int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const
5883 if (!column)
5884 return OnGetItemImage(item);
5886 return -1;
5889 wxListItemAttr *
5890 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
5892 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
5893 _T("invalid item index in OnGetItemAttr()") );
5895 // no attributes by default
5896 return NULL;
5899 void wxGenericListCtrl::SetItemCount(long count)
5901 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5903 m_mainWin->SetItemCount(count);
5906 void wxGenericListCtrl::RefreshItem(long item)
5908 m_mainWin->RefreshLine(item);
5911 void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
5913 m_mainWin->RefreshLines(itemFrom, itemTo);
5916 // Generic wxListCtrl is more or less a container for two other
5917 // windows which drawings are done upon. These are namely
5918 // 'm_headerWin' and 'm_mainWin'.
5919 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5920 // behaviour wxListCtrl has under wxMSW.
5922 void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5924 if (!rect)
5926 // The easy case, no rectangle specified.
5927 if (m_headerWin)
5928 m_headerWin->Refresh(eraseBackground);
5930 if (m_mainWin)
5931 m_mainWin->Refresh(eraseBackground);
5933 else
5935 // Refresh the header window
5936 if (m_headerWin)
5938 wxRect rectHeader = m_headerWin->GetRect();
5939 rectHeader.Intersect(*rect);
5940 if (rectHeader.GetWidth() && rectHeader.GetHeight())
5942 int x, y;
5943 m_headerWin->GetPosition(&x, &y);
5944 rectHeader.Offset(-x, -y);
5945 m_headerWin->Refresh(eraseBackground, &rectHeader);
5949 // Refresh the main window
5950 if (m_mainWin)
5952 wxRect rectMain = m_mainWin->GetRect();
5953 rectMain.Intersect(*rect);
5954 if (rectMain.GetWidth() && rectMain.GetHeight())
5956 int x, y;
5957 m_mainWin->GetPosition(&x, &y);
5958 rectMain.Offset(-x, -y);
5959 m_mainWin->Refresh(eraseBackground, &rectMain);
5965 void wxGenericListCtrl::Freeze()
5967 m_mainWin->Freeze();
5970 void wxGenericListCtrl::Thaw()
5972 m_mainWin->Thaw();
5975 } // namespace MuleExtern
5977 #endif // wxUSE_LISTCTRL