Fix -Wmissing-declarations warnings
[amule.git] / src / extern / wxWidgets / listctrl.cpp
blobb64af86250c9e5445e1f8e1d4af7eb5c415d850f
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$
7 // Copyright: Copyright (c) 1998-2011 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 #if wxCHECK_VERSION(2, 9, 0)
44 // wxWidgets 2.9+ does not include a mac/private anymore.
45 #else
46 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && (wxOSX_USE_CARBON || TARGET_CARBON)
47 #include <wx/mac/private.h>
48 #endif
49 #endif
52 // NOTE: If using the wxListBox visual attributes works everywhere then this can
53 // be removed, as well as the #else case below.
54 #define _USE_VISATTR 0
56 namespace MuleExtern {
58 // ----------------------------------------------------------------------------
59 // constants
60 // ----------------------------------------------------------------------------
62 // // the height of the header window (FIXME: should depend on its font!)
63 // static const int HEADER_HEIGHT = 23;
65 static const int SCROLL_UNIT_X = 15;
67 // the spacing between the lines (in report mode)
68 static const int LINE_SPACING = 0;
70 // extra margins around the text label
71 #ifdef __WXGTK__
72 static const int EXTRA_WIDTH = 6;
73 #else
74 static const int EXTRA_WIDTH = 4;
75 #endif
76 static const int EXTRA_HEIGHT = 4;
78 // margin between the window and the items
79 static const int EXTRA_BORDER_X = 2;
80 static const int EXTRA_BORDER_Y = 2;
82 // offset for the header window
83 static const int HEADER_OFFSET_X = 0;
84 static const int HEADER_OFFSET_Y = 0;
86 // margin between rows of icons in [small] icon view
87 static const int MARGIN_BETWEEN_ROWS = 6;
89 // when autosizing the columns, add some slack
90 static const int AUTOSIZE_COL_MARGIN = 10;
92 // default width for the header columns
93 static const int WIDTH_COL_DEFAULT = 80;
95 // the space between the image and the text in the report mode
96 static const int IMAGE_MARGIN_IN_REPORT_MODE = 5;
98 // the space between the image and the text in the report mode in header
99 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE = 2;
101 const wxChar wxListCtrlNameStr[] = wxT("listCtrl");
103 // ============================================================================
104 // private classes
105 // ============================================================================
107 //-----------------------------------------------------------------------------
108 // wxColWidthInfo (internal)
109 //-----------------------------------------------------------------------------
111 struct wxColWidthInfo
113 int nMaxWidth;
114 bool bNeedsUpdate; // only set to true when an item whose
115 // width == nMaxWidth is removed
117 wxColWidthInfo(int w = 0, bool needsUpdate = false)
119 nMaxWidth = w;
120 bNeedsUpdate = needsUpdate;
124 WX_DEFINE_ARRAY_PTR(wxColWidthInfo *, ColWidthArray);
126 //-----------------------------------------------------------------------------
127 // wxListItemData (internal)
128 //-----------------------------------------------------------------------------
130 class wxListItemData
132 public:
133 wxListItemData(wxListMainWindow *owner);
134 ~wxListItemData();
136 void SetItem( const wxListItem &info );
137 void SetImage( int image ) { m_image = image; }
138 void SetData( wxUIntPtr data ) { m_data = data; }
139 void SetPosition( int x, int y );
140 void SetSize( int width, int height );
142 bool HasText() const { return !m_text.empty(); }
143 const wxString& GetText() const { return m_text; }
144 void SetText(const wxString& text) { m_text = text; }
146 // we can't use empty string for measuring the string width/height, so
147 // always return something
148 wxString GetTextForMeasuring() const
150 wxString s = GetText();
151 if ( s.empty() )
152 s = _T('H');
154 return s;
157 bool IsHit( int x, int y ) const;
159 int GetX() const;
160 int GetY() const;
161 int GetWidth() const;
162 int GetHeight() const;
164 int GetImage() const { return m_image; }
165 bool HasImage() const { return GetImage() != -1; }
167 void GetItem( wxListItem &info ) const;
169 void SetAttr(wxListItemAttr *attr) { m_attr = attr; }
170 wxListItemAttr *GetAttr() const { return m_attr; }
172 public:
173 // the item image or -1
174 int m_image;
176 // user data associated with the item
177 wxUIntPtr m_data;
179 // the item coordinates are not used in report mode; instead this pointer is
180 // NULL and the owner window is used to retrieve the item position and size
181 wxRect *m_rect;
183 // the list ctrl we are in
184 wxListMainWindow *m_owner;
186 // custom attributes or NULL
187 wxListItemAttr *m_attr;
189 protected:
190 // common part of all ctors
191 void Init();
193 wxString m_text;
196 //-----------------------------------------------------------------------------
197 // wxListHeaderData (internal)
198 //-----------------------------------------------------------------------------
200 class wxListHeaderData : public wxObject
202 public:
203 wxListHeaderData();
204 wxListHeaderData( const wxListItem &info );
205 void SetItem( const wxListItem &item );
206 void SetPosition( int x, int y );
207 void SetWidth( int w );
208 void SetState( int state );
209 void SetFormat( int format );
210 void SetHeight( int h );
211 bool HasImage() const;
213 bool HasText() const { return !m_text.empty(); }
214 const wxString& GetText() const { return m_text; }
215 void SetText(const wxString& text) { m_text = text; }
217 void GetItem( wxListItem &item );
219 bool IsHit( int x, int y ) const;
220 int GetImage() const;
221 int GetWidth() const;
222 int GetFormat() const;
223 int GetState() const;
225 protected:
226 long m_mask;
227 int m_image;
228 wxString m_text;
229 int m_format;
230 int m_width;
231 int m_xpos,
232 m_ypos;
233 int m_height;
234 int m_state;
236 private:
237 void Init();
240 //-----------------------------------------------------------------------------
241 // wxListLineData (internal)
242 //-----------------------------------------------------------------------------
244 WX_DECLARE_LIST(wxListItemData, wxListItemDataList);
245 #include <wx/listimpl.cpp>
246 WX_DEFINE_LIST(wxListItemDataList)
248 class wxListLineData
250 public:
251 // the list of subitems: only may have more than one item in report mode
252 wxListItemDataList m_items;
254 // this is not used in report view
255 struct GeometryInfo
257 // total item rect
258 wxRect m_rectAll;
260 // label only
261 wxRect m_rectLabel;
263 // icon only
264 wxRect m_rectIcon;
266 // the part to be highlighted
267 wxRect m_rectHighlight;
269 // extend all our rects to be centered inside the one of given width
270 void ExtendWidth(wxCoord w)
272 wxASSERT_MSG( m_rectAll.width <= w,
273 _T("width can only be increased") );
275 m_rectAll.width = w;
276 m_rectLabel.x = m_rectAll.x + (w - m_rectLabel.width) / 2;
277 m_rectIcon.x = m_rectAll.x + (w - m_rectIcon.width) / 2;
278 m_rectHighlight.x = m_rectAll.x + (w - m_rectHighlight.width) / 2;
281 *m_gi;
283 // is this item selected? [NB: not used in virtual mode]
284 bool m_highlighted;
286 // back pointer to the list ctrl
287 wxListMainWindow *m_owner;
289 public:
290 wxListLineData(wxListMainWindow *owner);
292 ~wxListLineData()
294 WX_CLEAR_LIST(wxListItemDataList, m_items);
295 delete m_gi;
298 // are we in report mode?
299 inline bool InReportView() const;
301 // are we in virtual report mode?
302 inline bool IsVirtual() const;
304 // these 2 methods shouldn't be called for report view controls, in that
305 // case we determine our position/size ourselves
307 // calculate the size of the line
308 void CalculateSize( wxDC *dc, int spacing );
310 // remember the position this line appears at
311 void SetPosition( int x, int y, int spacing );
313 // wxListCtrl API
315 void SetImage( int image ) { SetImage(0, image); }
316 int GetImage() const { return GetImage(0); }
317 void SetImage( int index, int image );
318 int GetImage( int index ) const;
320 bool HasImage() const { return GetImage() != -1; }
321 bool HasText() const { return !GetText(0).empty(); }
323 void SetItem( int index, const wxListItem &info );
324 void GetItem( int index, wxListItem &info );
326 wxString GetText(int index) const;
327 void SetText( int index, const wxString& s );
329 wxListItemAttr *GetAttr() const;
330 void SetAttr(wxListItemAttr *attr);
332 // return true if the highlighting really changed
333 bool Highlight( bool on );
335 void ReverseHighlight();
337 bool IsHighlighted() const
339 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
341 return m_highlighted;
344 // draw the line on the given DC in icon/list mode
345 void Draw( wxDC *dc );
347 // the same in report mode
348 void DrawInReportMode( wxDC *dc,
349 const wxRect& rect,
350 const wxRect& rectHL,
351 bool highlighted );
353 private:
354 // set the line to contain num items (only can be > 1 in report mode)
355 void InitItems( int num );
357 // get the mode (i.e. style) of the list control
358 inline int GetMode() const;
360 // prepare the DC for drawing with these item's attributes, return true if
361 // we need to draw the items background to highlight it, false otherwise
362 bool SetAttributes(wxDC *dc,
363 const wxListItemAttr *attr,
364 bool highlight);
366 // draw the text on the DC with the correct justification; also add an
367 // ellipsis if the text is too large to fit in the current width
368 void DrawTextFormatted(wxDC *dc,
369 const wxString &text,
370 int col,
371 int x,
372 int yMid, // this is middle, not top, of the text
373 int width);
376 WX_DECLARE_OBJARRAY(wxListLineData, wxListLineDataArray);
377 #include <wx/arrimpl.cpp>
378 WX_DEFINE_OBJARRAY(wxListLineDataArray)
380 //-----------------------------------------------------------------------------
381 // wxListHeaderWindow (internal)
382 //-----------------------------------------------------------------------------
384 class wxListHeaderWindow : public wxWindow
386 protected:
387 wxListMainWindow *m_owner;
388 const wxCursor *m_currentCursor;
389 wxCursor *m_resizeCursor;
390 bool m_isDragging;
392 // column being resized or -1
393 int m_column;
395 // divider line position in logical (unscrolled) coords
396 int m_currentX;
398 // minimal position beyond which the divider line
399 // can't be dragged in logical coords
400 int m_minX;
402 public:
403 wxListHeaderWindow();
405 wxListHeaderWindow( wxWindow *win,
406 wxWindowID id,
407 wxListMainWindow *owner,
408 const wxPoint &pos = wxDefaultPosition,
409 const wxSize &size = wxDefaultSize,
410 long style = 0,
411 const wxString &name = wxT("wxlistctrlcolumntitles") );
413 virtual ~wxListHeaderWindow();
415 void DrawCurrent();
416 void AdjustDC( wxDC& dc );
418 void OnPaint( wxPaintEvent &event );
419 void OnMouse( wxMouseEvent &event );
420 void OnSetFocus( wxFocusEvent &event );
422 // needs refresh
423 bool m_dirty;
425 private:
426 // common part of all ctors
427 void Init();
429 // generate and process the list event of the given type, return true if
430 // it wasn't vetoed, i.e. if we should proceed
431 bool SendListEvent(wxEventType type, const wxPoint& pos);
433 DECLARE_EVENT_TABLE()
436 //-----------------------------------------------------------------------------
437 // wxListRenameTimer (internal)
438 //-----------------------------------------------------------------------------
440 class wxListRenameTimer: public wxTimer
442 private:
443 wxListMainWindow *m_owner;
445 public:
446 wxListRenameTimer( wxListMainWindow *owner );
447 void Notify();
450 //-----------------------------------------------------------------------------
451 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
452 //-----------------------------------------------------------------------------
454 class wxListTextCtrlWrapper : public wxEvtHandler
456 public:
457 // NB: text must be a valid object but not Create()d yet
458 wxListTextCtrlWrapper(wxListMainWindow *owner,
459 wxTextCtrl *text,
460 size_t itemEdit);
462 wxTextCtrl *GetText() const { return m_text; }
464 void AcceptChangesAndFinish();
466 protected:
467 void OnChar( wxKeyEvent &event );
468 void OnKeyUp( wxKeyEvent &event );
469 void OnKillFocus( wxFocusEvent &event );
471 bool AcceptChanges();
472 void Finish();
474 private:
475 wxListMainWindow *m_owner;
476 wxTextCtrl *m_text;
477 wxString m_startValue;
478 size_t m_itemEdited;
479 bool m_finished;
480 bool m_aboutToFinish;
482 DECLARE_EVENT_TABLE()
485 //-----------------------------------------------------------------------------
486 // wxListMainWindow (internal)
487 //-----------------------------------------------------------------------------
489 WX_DECLARE_LIST(wxListHeaderData, wxListHeaderDataList);
490 #include <wx/listimpl.cpp>
491 WX_DEFINE_LIST(wxListHeaderDataList)
493 class wxListMainWindow : public wxScrolledWindow
495 public:
496 wxListMainWindow();
497 wxListMainWindow( wxWindow *parent,
498 wxWindowID id,
499 const wxPoint& pos = wxDefaultPosition,
500 const wxSize& size = wxDefaultSize,
501 long style = 0,
502 const wxString &name = _T("listctrlmainwindow") );
504 virtual ~wxListMainWindow();
506 bool HasFlag(int flag) const { return m_parent->HasFlag(flag); }
508 // return true if this is a virtual list control
509 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL); }
511 // return true if the control is in report mode
512 bool InReportView() const { return HasFlag(wxLC_REPORT); }
514 // return true if we are in single selection mode, false if multi sel
515 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL); }
517 // do we have a header window?
518 bool HasHeader() const
519 { return InReportView() && !HasFlag(wxLC_NO_HEADER); }
521 void HighlightAll( bool on );
523 // all these functions only do something if the line is currently visible
525 // change the line "selected" state, return true if it really changed
526 bool HighlightLine( size_t line, bool highlight = true);
528 // as HighlightLine() but do it for the range of lines: this is incredibly
529 // more efficient for virtual list controls!
531 // NB: unlike HighlightLine() this one does refresh the lines on screen
532 void HighlightLines( size_t lineFrom, size_t lineTo, bool on = true );
534 // toggle the line state and refresh it
535 void ReverseHighlight( size_t line )
536 { HighlightLine(line, !IsHighlighted(line)); RefreshLine(line); }
538 // return true if the line is highlighted
539 bool IsHighlighted(size_t line) const;
541 // refresh one or several lines at once
542 void RefreshLine( size_t line );
543 void RefreshLines( size_t lineFrom, size_t lineTo );
545 // refresh all selected items
546 void RefreshSelected();
548 // refresh all lines below the given one: the difference with
549 // RefreshLines() is that the index here might not be a valid one (happens
550 // when the last line is deleted)
551 void RefreshAfter( size_t lineFrom );
553 // the methods which are forwarded to wxListLineData itself in list/icon
554 // modes but are here because the lines don't store their positions in the
555 // report mode
557 // get the bound rect for the entire line
558 wxRect GetLineRect(size_t line) const;
560 // get the bound rect of the label
561 wxRect GetLineLabelRect(size_t line) const;
563 // get the bound rect of the items icon (only may be called if we do have
564 // an icon!)
565 wxRect GetLineIconRect(size_t line) const;
567 // get the rect to be highlighted when the item has focus
568 wxRect GetLineHighlightRect(size_t line) const;
570 // get the size of the total line rect
571 wxSize GetLineSize(size_t line) const
572 { return GetLineRect(line).GetSize(); }
574 // return the hit code for the corresponding position (in this line)
575 long HitTestLine(size_t line, int x, int y) const;
577 // bring the selected item into view, scrolling to it if necessary
578 void MoveToItem(size_t item);
580 // bring the current item into view
581 void MoveToFocus() { MoveToItem(m_current); }
583 // start editing the label of the given item
584 wxTextCtrl *EditLabel(long item,
585 wxClassInfo* textControlClass = CLASSINFO(wxTextCtrl));
586 wxTextCtrl *GetEditControl() const
588 return m_textctrlWrapper ? m_textctrlWrapper->GetText() : NULL;
591 void FinishEditing(wxTextCtrl *text)
593 delete text;
594 m_textctrlWrapper = NULL;
595 SetFocusIgnoringChildren();
598 // suspend/resume redrawing the control
599 void Freeze();
600 void Thaw();
602 void OnRenameTimer();
603 bool OnRenameAccept(size_t itemEdit, const wxString& value);
604 void OnRenameCancelled(size_t itemEdit);
606 void OnMouse( wxMouseEvent &event );
608 // called to switch the selection from the current item to newCurrent,
609 void OnArrowChar( size_t newCurrent, const wxKeyEvent& event );
611 void OnChar( wxKeyEvent &event );
612 void OnKeyDown( wxKeyEvent &event );
613 void OnKeyUp( wxKeyEvent &event );
614 void OnSetFocus( wxFocusEvent &event );
615 void OnKillFocus( wxFocusEvent &event );
616 void OnScroll( wxScrollWinEvent& event );
618 void OnPaint( wxPaintEvent &event );
619 void OnErase( wxEraseEvent& event ) {
620 // This is needed to avoid garbage on empty lists.
621 if ( IsEmpty() ) {
622 event.Skip();
626 void OnChildFocus(wxChildFocusEvent& event);
628 void DrawImage( int index, wxDC *dc, int x, int y );
629 void GetImageSize( int index, int &width, int &height ) const;
630 int GetTextLength( const wxString &s ) const;
632 void SetImageList( wxImageList *imageList, int which );
633 void SetItemSpacing( int spacing, bool isSmall = false );
634 int GetItemSpacing( bool isSmall = false );
636 void SetColumn( int col, wxListItem &item );
637 void SetColumnWidth( int col, int width );
638 void GetColumn( int col, wxListItem &item ) const;
639 int GetColumnWidth( int col ) const;
640 int GetColumnCount() const { return m_columns.GetCount(); }
642 // returns the sum of the heights of all columns
643 int GetHeaderWidth() const;
645 int GetCountPerPage() const;
647 void SetItem( wxListItem &item );
648 void GetItem( wxListItem &item ) const;
649 void SetItemState( long item, long state, long stateMask );
650 void SetItemStateAll( long state, long stateMask );
651 int GetItemState( long item, long stateMask ) const;
652 void GetItemRect( long index, wxRect &rect ) const;
653 wxRect GetViewRect() const;
654 bool GetItemPosition( long item, wxPoint& pos ) const;
655 int GetSelectedItemCount() const;
657 wxString GetItemText(long item) const
659 wxListItem info;
660 info.m_mask = wxLIST_MASK_TEXT;
661 info.m_itemId = item;
662 GetItem( info );
663 return info.m_text;
666 void SetItemText(long item, const wxString& value)
668 wxListItem info;
669 info.m_mask = wxLIST_MASK_TEXT;
670 info.m_itemId = item;
671 info.m_text = value;
672 SetItem( info );
675 // set the scrollbars and update the positions of the items
676 void RecalculatePositions(bool noRefresh = false);
678 // refresh the window and the header
679 void RefreshAll();
681 long GetNextItem( long item, int geometry, int state ) const;
682 void DeleteItem( long index );
683 void DeleteAllItems();
684 void DeleteColumn( int col );
685 void DeleteEverything();
686 void EnsureVisible( long index );
687 long FindItem( long start, const wxString& str, bool partial = false );
688 long FindItem( long start, wxUIntPtr data);
689 long FindItem( const wxPoint& pt );
690 long HitTest( int x, int y, int &flags ) const;
691 void InsertItem( wxListItem &item );
692 void InsertColumn( long col, wxListItem &item );
693 int GetItemWidthWithImage(wxListItem * item);
694 void SortItems( MuleListCtrlCompare fn, long data );
696 size_t GetItemCount() const;
697 bool IsEmpty() const { return GetItemCount() == 0; }
698 void SetItemCount(long count);
700 // change the current (== focused) item, send a notification event
701 void ChangeCurrent(size_t current);
702 void ResetCurrent() { ChangeCurrent((size_t)-1); }
703 bool HasCurrent() const { return m_current != (size_t)-1; }
705 // send out a wxListEvent
706 void SendNotify( size_t line,
707 wxEventType command,
708 const wxPoint& point = wxDefaultPosition );
710 // override base class virtual to reset m_lineHeight when the font changes
711 virtual bool SetFont(const wxFont& font)
713 if ( !wxScrolledWindow::SetFont(font) )
714 return false;
716 m_lineHeight = 0;
718 return true;
721 // these are for wxListLineData usage only
723 // get the backpointer to the list ctrl
724 wxGenericListCtrl *GetListCtrl() const
726 return wxStaticCast(GetParent(), wxGenericListCtrl);
729 // get the height of all lines (assuming they all do have the same height)
730 wxCoord GetLineHeight() const;
732 // get the y position of the given line (only for report view)
733 wxCoord GetLineY(size_t line) const;
735 // get the brush to use for the item highlighting
736 const wxBrush& GetHighlightBrush() const
738 return m_hasFocus ? m_highlightBrush : m_highlightUnfocusedBrush;
741 bool HasFocus() const
743 return m_hasFocus;
746 //protected:
747 // the array of all line objects for a non virtual list control (for the
748 // virtual list control we only ever use m_lines[0])
749 wxListLineDataArray m_lines;
751 // the list of column objects
752 wxListHeaderDataList m_columns;
754 // currently focused item or -1
755 size_t m_current;
757 // the number of lines per page
758 int m_linesPerPage;
760 // this flag is set when something which should result in the window
761 // redrawing happens (i.e. an item was added or deleted, or its appearance
762 // changed) and OnPaint() doesn't redraw the window while it is set which
763 // allows to minimize the number of repaintings when a lot of items are
764 // being added. The real repainting occurs only after the next OnIdle()
765 // call
766 bool m_dirty;
768 wxColour *m_highlightColour;
769 wxImageList *m_small_image_list;
770 wxImageList *m_normal_image_list;
771 int m_small_spacing;
772 int m_normal_spacing;
773 bool m_hasFocus;
775 bool m_lastOnSame;
776 wxTimer *m_renameTimer;
777 bool m_isCreated;
778 int m_dragCount;
779 wxPoint m_dragStart;
780 ColWidthArray m_aColWidths;
782 // for double click logic
783 size_t m_lineLastClicked,
784 m_lineBeforeLastClicked,
785 m_lineSelectSingleOnUp;
787 protected:
788 wxWindow *GetMainWindowOfCompositeControl() { return GetParent(); }
790 // the total count of items in a virtual list control
791 size_t m_countVirt;
793 // the object maintaining the items selection state, only used in virtual
794 // controls
795 wxSelectionStore m_selStore;
797 // common part of all ctors
798 void Init();
800 // get the line data for the given index
801 wxListLineData *GetLine(size_t n) const
803 wxASSERT_MSG( n != (size_t)-1, _T("invalid line index") );
805 if ( IsVirtual() )
807 wxConstCast(this, wxListMainWindow)->CacheLineData(n);
808 n = 0;
811 return &m_lines[n];
814 // get a dummy line which can be used for geometry calculations and such:
815 // you must use GetLine() if you want to really draw the line
816 wxListLineData *GetDummyLine() const;
818 // cache the line data of the n-th line in m_lines[0]
819 void CacheLineData(size_t line);
821 // get the range of visible lines
822 void GetVisibleLinesRange(size_t *from, size_t *to);
824 // force us to recalculate the range of visible lines
825 void ResetVisibleLinesRange() { m_lineFrom = (size_t)-1; }
827 // get the colour to be used for drawing the rules
828 wxColour GetRuleColour() const
830 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
833 private:
834 // initialize the current item if needed
835 void UpdateCurrent();
837 // delete all items but don't refresh: called from dtor
838 void DoDeleteAllItems();
840 // the height of one line using the current font
841 wxCoord m_lineHeight;
843 // the total header width or 0 if not calculated yet
844 wxCoord m_headerWidth;
846 // the first and last lines being shown on screen right now (inclusive),
847 // both may be -1 if they must be calculated so never access them directly:
848 // use GetVisibleLinesRange() above instead
849 size_t m_lineFrom,
850 m_lineTo;
852 // the brushes to use for item highlighting when we do/don't have focus
853 wxBrush m_highlightBrush,
854 m_highlightUnfocusedBrush;
856 // if this is > 0, the control is frozen and doesn't redraw itself
857 size_t m_freezeCount;
859 // wrapper around the text control currently used for in place editing or
860 // NULL if no item is being edited
861 wxListTextCtrlWrapper *m_textctrlWrapper;
864 DECLARE_EVENT_TABLE()
866 friend class wxGenericListCtrl;
870 wxListItemData::~wxListItemData()
872 // in the virtual list control the attributes are managed by the main
873 // program, so don't delete them
874 if ( !m_owner->IsVirtual() )
875 delete m_attr;
877 delete m_rect;
880 void wxListItemData::Init()
882 m_image = -1;
883 m_data = 0;
885 m_attr = NULL;
888 wxListItemData::wxListItemData(wxListMainWindow *owner)
890 Init();
892 m_owner = owner;
894 if ( owner->InReportView() )
895 m_rect = NULL;
896 else
897 m_rect = new wxRect;
900 void wxListItemData::SetItem( const wxListItem &info )
902 if ( info.m_mask & wxLIST_MASK_TEXT )
903 SetText(info.m_text);
904 if ( info.m_mask & wxLIST_MASK_IMAGE )
905 m_image = info.m_image;
906 if ( info.m_mask & wxLIST_MASK_DATA )
907 m_data = info.m_data;
909 if ( info.HasAttributes() )
911 if ( m_attr )
912 m_attr->AssignFrom(*info.GetAttributes());
913 else
914 m_attr = new wxListItemAttr(*info.GetAttributes());
917 if ( m_rect )
919 m_rect->x =
920 m_rect->y =
921 m_rect->height = 0;
922 m_rect->width = info.m_width;
926 void wxListItemData::SetPosition( int x, int y )
928 wxCHECK_RET( m_rect, _T("unexpected SetPosition() call") );
930 m_rect->x = x;
931 m_rect->y = y;
934 void wxListItemData::SetSize( int width, int height )
936 wxCHECK_RET( m_rect, _T("unexpected SetSize() call") );
938 if ( width != -1 )
939 m_rect->width = width;
940 if ( height != -1 )
941 m_rect->height = height;
944 bool wxListItemData::IsHit( int x, int y ) const
946 wxCHECK_MSG( m_rect, false, _T("can't be called in this mode") );
948 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x, y);
951 int wxListItemData::GetX() const
953 wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
955 return m_rect->x;
958 int wxListItemData::GetY() const
960 wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
962 return m_rect->y;
965 int wxListItemData::GetWidth() const
967 wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
969 return m_rect->width;
972 int wxListItemData::GetHeight() const
974 wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
976 return m_rect->height;
979 void wxListItemData::GetItem( wxListItem &info ) const
981 long mask = info.m_mask;
982 if ( !mask )
983 // by default, get everything for backwards compatibility
984 mask = -1;
986 if ( mask & wxLIST_MASK_TEXT )
987 info.m_text = m_text;
988 if ( mask & wxLIST_MASK_IMAGE )
989 info.m_image = m_image;
990 if ( mask & wxLIST_MASK_DATA )
991 info.m_data = m_data;
993 if ( m_attr )
995 if ( m_attr->HasTextColour() )
996 info.SetTextColour(m_attr->GetTextColour());
997 if ( m_attr->HasBackgroundColour() )
998 info.SetBackgroundColour(m_attr->GetBackgroundColour());
999 if ( m_attr->HasFont() )
1000 info.SetFont(m_attr->GetFont());
1004 //-----------------------------------------------------------------------------
1005 // wxListHeaderData
1006 //-----------------------------------------------------------------------------
1008 void wxListHeaderData::Init()
1010 m_mask = 0;
1011 m_image = -1;
1012 m_format = 0;
1013 m_width = 0;
1014 m_xpos = 0;
1015 m_ypos = 0;
1016 m_height = 0;
1017 m_state = 0;
1020 wxListHeaderData::wxListHeaderData()
1022 Init();
1025 wxListHeaderData::wxListHeaderData( const wxListItem &item )
1027 Init();
1029 SetItem( item );
1032 void wxListHeaderData::SetItem( const wxListItem &item )
1034 m_mask = item.m_mask;
1036 if ( m_mask & wxLIST_MASK_TEXT )
1037 m_text = item.m_text;
1039 if ( m_mask & wxLIST_MASK_IMAGE )
1040 m_image = item.m_image;
1042 if ( m_mask & wxLIST_MASK_FORMAT )
1043 m_format = item.m_format;
1045 if ( m_mask & wxLIST_MASK_WIDTH )
1046 SetWidth(item.m_width);
1048 if ( m_mask & wxLIST_MASK_STATE )
1049 SetState(item.m_state);
1052 void wxListHeaderData::SetPosition( int x, int y )
1054 m_xpos = x;
1055 m_ypos = y;
1058 void wxListHeaderData::SetHeight( int h )
1060 m_height = h;
1063 void wxListHeaderData::SetWidth( int w )
1065 m_width = w < 0 ? WIDTH_COL_DEFAULT : w;
1068 void wxListHeaderData::SetState( int flag )
1070 m_state = flag;
1073 void wxListHeaderData::SetFormat( int format )
1075 m_format = format;
1078 bool wxListHeaderData::HasImage() const
1080 return m_image != -1;
1083 bool wxListHeaderData::IsHit( int x, int y ) const
1085 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
1088 void wxListHeaderData::GetItem( wxListItem& item )
1090 item.m_mask = m_mask;
1091 item.m_text = m_text;
1092 item.m_image = m_image;
1093 item.m_format = m_format;
1094 item.m_width = m_width;
1095 item.m_state = m_state;
1098 int wxListHeaderData::GetImage() const
1100 return m_image;
1103 int wxListHeaderData::GetWidth() const
1105 return m_width;
1108 int wxListHeaderData::GetFormat() const
1110 return m_format;
1113 int wxListHeaderData::GetState() const
1115 return m_state;
1118 //-----------------------------------------------------------------------------
1119 // wxListLineData
1120 //-----------------------------------------------------------------------------
1122 inline int wxListLineData::GetMode() const
1124 return m_owner->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE;
1127 inline bool wxListLineData::InReportView() const
1129 return m_owner->HasFlag(wxLC_REPORT);
1132 inline bool wxListLineData::IsVirtual() const
1134 return m_owner->IsVirtual();
1137 wxListLineData::wxListLineData( wxListMainWindow *owner )
1139 m_owner = owner;
1141 if ( InReportView() )
1142 m_gi = NULL;
1143 else // !report
1144 m_gi = new GeometryInfo;
1146 m_highlighted = false;
1148 InitItems( GetMode() == wxLC_REPORT ? m_owner->GetColumnCount() : 1 );
1151 void wxListLineData::CalculateSize( wxDC *dc, int spacing )
1153 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1154 wxCHECK_RET( node, _T("no subitems at all??") );
1156 wxListItemData *item = node->GetData();
1158 wxString s;
1159 wxCoord lw, lh;
1161 switch ( GetMode() )
1163 case wxLC_ICON:
1164 case wxLC_SMALL_ICON:
1165 m_gi->m_rectAll.width = spacing;
1167 s = item->GetText();
1169 if ( s.empty() )
1171 lh =
1172 m_gi->m_rectLabel.width =
1173 m_gi->m_rectLabel.height = 0;
1175 else // has label
1177 dc->GetTextExtent( s, &lw, &lh );
1178 lw += EXTRA_WIDTH;
1179 lh += EXTRA_HEIGHT;
1181 m_gi->m_rectAll.height = spacing + lh;
1182 if (lw > spacing)
1183 m_gi->m_rectAll.width = lw;
1185 m_gi->m_rectLabel.width = lw;
1186 m_gi->m_rectLabel.height = lh;
1189 if (item->HasImage())
1191 int w, h;
1192 m_owner->GetImageSize( item->GetImage(), w, h );
1193 m_gi->m_rectIcon.width = w + 8;
1194 m_gi->m_rectIcon.height = h + 8;
1196 if ( m_gi->m_rectIcon.width > m_gi->m_rectAll.width )
1197 m_gi->m_rectAll.width = m_gi->m_rectIcon.width;
1198 if ( m_gi->m_rectIcon.height + lh > m_gi->m_rectAll.height - 4 )
1199 m_gi->m_rectAll.height = m_gi->m_rectIcon.height + lh + 4;
1202 if ( item->HasText() )
1204 m_gi->m_rectHighlight.width = m_gi->m_rectLabel.width;
1205 m_gi->m_rectHighlight.height = m_gi->m_rectLabel.height;
1207 else // no text, highlight the icon
1209 m_gi->m_rectHighlight.width = m_gi->m_rectIcon.width;
1210 m_gi->m_rectHighlight.height = m_gi->m_rectIcon.height;
1212 break;
1214 case wxLC_LIST:
1215 s = item->GetTextForMeasuring();
1217 dc->GetTextExtent( s, &lw, &lh );
1218 lw += EXTRA_WIDTH;
1219 lh += EXTRA_HEIGHT;
1221 m_gi->m_rectLabel.width = lw;
1222 m_gi->m_rectLabel.height = lh;
1224 m_gi->m_rectAll.width = lw;
1225 m_gi->m_rectAll.height = lh;
1227 if (item->HasImage())
1229 int w, h;
1230 m_owner->GetImageSize( item->GetImage(), w, h );
1231 m_gi->m_rectIcon.width = w;
1232 m_gi->m_rectIcon.height = h;
1234 m_gi->m_rectAll.width += 4 + w;
1235 if (h > m_gi->m_rectAll.height)
1236 m_gi->m_rectAll.height = h;
1239 m_gi->m_rectHighlight.width = m_gi->m_rectAll.width;
1240 m_gi->m_rectHighlight.height = m_gi->m_rectAll.height;
1241 break;
1243 case wxLC_REPORT:
1244 wxFAIL_MSG( _T("unexpected call to SetSize") );
1245 break;
1247 default:
1248 wxFAIL_MSG( _T("unknown mode") );
1249 break;
1253 void wxListLineData::SetPosition( int x, int y, int spacing )
1255 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1256 wxCHECK_RET( node, _T("no subitems at all??") );
1258 wxListItemData *item = node->GetData();
1260 switch ( GetMode() )
1262 case wxLC_ICON:
1263 case wxLC_SMALL_ICON:
1264 m_gi->m_rectAll.x = x;
1265 m_gi->m_rectAll.y = y;
1267 if ( item->HasImage() )
1269 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 4 +
1270 (m_gi->m_rectAll.width - m_gi->m_rectIcon.width) / 2;
1271 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 4;
1274 if ( item->HasText() )
1276 if (m_gi->m_rectAll.width > spacing)
1277 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
1278 else
1279 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2) + (spacing / 2) - (m_gi->m_rectLabel.width / 2);
1280 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + m_gi->m_rectAll.height + 2 - m_gi->m_rectLabel.height;
1281 m_gi->m_rectHighlight.x = m_gi->m_rectLabel.x - 2;
1282 m_gi->m_rectHighlight.y = m_gi->m_rectLabel.y - 2;
1284 else // no text, highlight the icon
1286 m_gi->m_rectHighlight.x = m_gi->m_rectIcon.x - 4;
1287 m_gi->m_rectHighlight.y = m_gi->m_rectIcon.y - 4;
1289 break;
1291 case wxLC_LIST:
1292 m_gi->m_rectAll.x = x;
1293 m_gi->m_rectAll.y = y;
1295 m_gi->m_rectHighlight.x = m_gi->m_rectAll.x;
1296 m_gi->m_rectHighlight.y = m_gi->m_rectAll.y;
1297 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + 2;
1299 if (item->HasImage())
1301 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 2;
1302 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 2;
1303 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 4 + (EXTRA_WIDTH/2) + m_gi->m_rectIcon.width;
1305 else
1307 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
1309 break;
1311 case wxLC_REPORT:
1312 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1313 break;
1315 default:
1316 wxFAIL_MSG( _T("unknown mode") );
1317 break;
1321 void wxListLineData::InitItems( int num )
1323 for (int i = 0; i < num; i++)
1324 m_items.Append( new wxListItemData(m_owner) );
1327 void wxListLineData::SetItem( int index, const wxListItem &info )
1329 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1330 wxCHECK_RET( node, _T("invalid column index in SetItem") );
1332 wxListItemData *item = node->GetData();
1333 item->SetItem( info );
1336 void wxListLineData::GetItem( int index, wxListItem &info )
1338 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1339 if (node)
1341 wxListItemData *item = node->GetData();
1342 item->GetItem( info );
1346 wxString wxListLineData::GetText(int index) const
1348 wxString s;
1350 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1351 if (node)
1353 wxListItemData *item = node->GetData();
1354 s = item->GetText();
1357 return s;
1360 void wxListLineData::SetText( int index, const wxString& s )
1362 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1363 if (node)
1365 wxListItemData *item = node->GetData();
1366 item->SetText( s );
1370 void wxListLineData::SetImage( int index, int image )
1372 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1373 wxCHECK_RET( node, _T("invalid column index in SetImage()") );
1375 wxListItemData *item = node->GetData();
1376 item->SetImage(image);
1379 int wxListLineData::GetImage( int index ) const
1381 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1382 wxCHECK_MSG( node, -1, _T("invalid column index in GetImage()") );
1384 wxListItemData *item = node->GetData();
1385 return item->GetImage();
1388 wxListItemAttr *wxListLineData::GetAttr() const
1390 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1391 wxCHECK_MSG( node, NULL, _T("invalid column index in GetAttr()") );
1393 wxListItemData *item = node->GetData();
1394 return item->GetAttr();
1397 void wxListLineData::SetAttr(wxListItemAttr *attr)
1399 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1400 wxCHECK_RET( node, _T("invalid column index in SetAttr()") );
1402 wxListItemData *item = node->GetData();
1403 item->SetAttr(attr);
1406 bool wxListLineData::SetAttributes(wxDC *dc,
1407 const wxListItemAttr *attr,
1408 bool highlighted)
1410 wxWindow *listctrl = m_owner->GetParent();
1412 // fg colour
1414 // don't use foreground colour for drawing highlighted items - this might
1415 // make them completely invisible (and there is no way to do bit
1416 // arithmetics on wxColour, unfortunately)
1417 wxColour colText;
1418 if ( highlighted )
1419 #ifdef __WXMAC__
1421 if (m_owner->HasFocus()
1422 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
1423 && IsControlActive( (ControlRef)m_owner->GetHandle() )
1424 #endif
1426 colText = *wxWHITE;
1427 else
1428 colText = *wxBLACK;
1430 #else
1431 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
1432 #endif
1433 else if ( attr && attr->HasTextColour() )
1434 colText = attr->GetTextColour();
1435 else
1436 colText = listctrl->GetForegroundColour();
1438 dc->SetTextForeground(colText);
1440 // font
1441 wxFont font;
1442 if ( attr && attr->HasFont() )
1443 font = attr->GetFont();
1444 else
1445 font = listctrl->GetFont();
1447 dc->SetFont(font);
1449 // bg colour
1450 bool hasBgCol = attr && attr->HasBackgroundColour();
1451 if ( highlighted || hasBgCol )
1453 if ( highlighted )
1454 dc->SetBrush( m_owner->GetHighlightBrush() );
1455 else
1456 dc->SetBrush(*(wxTheBrushList->FindOrCreateBrush(attr->GetBackgroundColour(), wxSOLID)));
1458 dc->SetPen( *wxTRANSPARENT_PEN );
1460 return true;
1463 return false;
1466 void wxListLineData::Draw( wxDC *dc )
1468 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1469 wxCHECK_RET( node, _T("no subitems at all??") );
1471 bool highlighted = IsHighlighted();
1473 wxListItemAttr *attr = GetAttr();
1475 if ( SetAttributes(dc, attr, highlighted) )
1476 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1478 dc->DrawRectangle( m_gi->m_rectHighlight );
1480 #else
1482 if (highlighted)
1484 int flags = wxCONTROL_SELECTED;
1485 if (m_owner->HasFocus()
1486 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
1487 && IsControlActive( (ControlRef)m_owner->GetHandle() )
1488 #endif
1490 flags |= wxCONTROL_FOCUSED;
1491 wxRendererNative::Get().DrawItemSelectionRect( m_owner, *dc, m_gi->m_rectHighlight, flags );
1494 else
1496 dc->DrawRectangle( m_gi->m_rectHighlight );
1499 #endif
1501 // just for debugging to better see where the items are
1502 #if 0
1503 dc->SetPen(*wxRED_PEN);
1504 dc->SetBrush(*wxTRANSPARENT_BRUSH);
1505 dc->DrawRectangle( m_gi->m_rectAll );
1506 dc->SetPen(*wxGREEN_PEN);
1507 dc->DrawRectangle( m_gi->m_rectIcon );
1508 #endif
1510 wxListItemData *item = node->GetData();
1511 if (item->HasImage())
1513 // centre the image inside our rectangle, this looks nicer when items
1514 // ae aligned in a row
1515 const wxRect& rectIcon = m_gi->m_rectIcon;
1517 m_owner->DrawImage(item->GetImage(), dc, rectIcon.x, rectIcon.y);
1520 if (item->HasText())
1522 const wxRect& rectLabel = m_gi->m_rectLabel;
1524 wxDCClipper clipper(*dc, rectLabel);
1525 dc->DrawText(item->GetText(), rectLabel.x, rectLabel.y);
1529 void wxListLineData::DrawInReportMode( wxDC *dc,
1530 const wxRect& rect,
1531 const wxRect& rectHL,
1532 bool highlighted )
1534 // TODO: later we should support setting different attributes for
1535 // different columns - to do it, just add "col" argument to
1536 // GetAttr() and move these lines into the loop below
1537 wxListItemAttr *attr = GetAttr();
1538 if ( SetAttributes(dc, attr, highlighted) )
1539 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1541 dc->DrawRectangle( rectHL );
1543 #else
1545 if (highlighted)
1547 int flags = wxCONTROL_SELECTED;
1548 if (m_owner->HasFocus()
1549 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
1550 && IsControlActive( (ControlRef)m_owner->GetHandle() )
1551 #endif
1553 flags |= wxCONTROL_FOCUSED;
1554 wxRendererNative::Get().DrawItemSelectionRect( m_owner, *dc, rectHL, flags );
1556 else
1558 dc->DrawRectangle( rectHL );
1561 #endif
1563 wxCoord x = rect.x + HEADER_OFFSET_X,
1564 yMid = rect.y + rect.height/2;
1565 #ifdef __WXGTK__
1566 // This probably needs to be done
1567 // on all platforms as the icons
1568 // otherwise nearly touch the border
1569 x += 2;
1570 #endif
1572 size_t col = 0;
1573 for ( wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1574 node;
1575 node = node->GetNext(), col++ )
1577 wxListItemData *item = node->GetData();
1579 int width = m_owner->GetColumnWidth(col);
1580 int xOld = x;
1581 x += width;
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;
1909 // Fill in what's missing to the right of the columns, otherwise we will
1910 // leave an unpainted area when columns are removed (and it looks better)
1911 if ( x < w )
1913 wxRendererNative::Get().DrawHeaderButton
1915 this,
1917 wxRect(x, HEADER_OFFSET_Y, w - x, h),
1923 void wxListHeaderWindow::DrawCurrent()
1925 #if 1
1926 m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1927 #else
1928 int x1 = m_currentX;
1929 int y1 = 0;
1930 m_owner->ClientToScreen( &x1, &y1 );
1932 int x2 = m_currentX;
1933 int y2 = 0;
1934 m_owner->GetClientSize( NULL, &y2 );
1935 m_owner->ClientToScreen( &x2, &y2 );
1937 wxScreenDC dc;
1938 dc.SetLogicalFunction( wxINVERT );
1939 dc.SetPen( *(wxThePenList->FindOrCreatePen(*wxBLACK, 2, wxSOLID ) ));
1940 dc.SetBrush( *wxTRANSPARENT_BRUSH );
1942 AdjustDC(dc);
1944 dc.DrawLine( x1, y1, x2, y2 );
1946 dc.SetLogicalFunction( wxCOPY );
1948 dc.SetPen( wxNullPen );
1949 dc.SetBrush( wxNullBrush );
1950 #endif
1953 void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
1955 // we want to work with logical coords
1956 int x;
1957 m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
1958 int y = event.GetY();
1960 if (m_isDragging)
1962 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING, event.GetPosition());
1964 // we don't draw the line beyond our window, but we allow dragging it
1965 // there
1966 int w = 0;
1967 GetClientSize( &w, NULL );
1968 m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
1969 w -= 6;
1971 // erase the line if it was drawn
1972 if ( m_currentX < w )
1973 DrawCurrent();
1975 if (event.ButtonUp())
1977 ReleaseMouse();
1978 m_isDragging = false;
1979 m_dirty = true;
1980 m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1981 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG, event.GetPosition());
1983 else
1985 if (x > m_minX + 7)
1986 m_currentX = x;
1987 else
1988 m_currentX = m_minX + 7;
1990 // draw in the new location
1991 if ( m_currentX < w )
1992 DrawCurrent();
1995 else // not dragging
1997 m_minX = 0;
1998 bool hit_border = false;
2000 // end of the current column
2001 int xpos = 0;
2003 // find the column where this event occurred
2004 int col,
2005 countCol = m_owner->GetColumnCount();
2006 for (col = 0; col < countCol; col++)
2008 xpos += m_owner->GetColumnWidth( col );
2009 m_column = col;
2011 if ( (abs(x-xpos) < 3) && (y < 22) )
2013 // near the column border
2014 hit_border = true;
2015 break;
2018 if ( x < xpos )
2020 // inside the column
2021 break;
2024 m_minX = xpos;
2027 if ( col == countCol )
2028 m_column = -1;
2030 if (event.LeftDown() || event.RightUp())
2032 if (hit_border && event.LeftDown())
2034 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG,
2035 event.GetPosition()) )
2037 m_isDragging = true;
2038 m_currentX = x;
2039 CaptureMouse();
2040 DrawCurrent();
2042 //else: column resizing was vetoed by the user code
2044 else // click on a column
2046 // record the selected state of the columns
2047 if (event.LeftDown())
2049 for (int i=0; i < m_owner->GetColumnCount(); i++)
2051 wxListItem colItem;
2052 m_owner->GetColumn(i, colItem);
2053 long state = colItem.GetState();
2054 if (i == m_column)
2055 colItem.SetState(state | wxLIST_STATE_SELECTED);
2056 else
2057 colItem.SetState(state & ~wxLIST_STATE_SELECTED);
2058 m_owner->SetColumn(i, colItem);
2062 SendListEvent( event.LeftDown()
2063 ? wxEVT_COMMAND_LIST_COL_CLICK
2064 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK,
2065 event.GetPosition());
2068 else if (event.Moving())
2070 bool setCursor;
2071 if (hit_border)
2073 setCursor = m_currentCursor == wxSTANDARD_CURSOR;
2074 m_currentCursor = m_resizeCursor;
2076 else
2078 setCursor = m_currentCursor != wxSTANDARD_CURSOR;
2079 m_currentCursor = wxSTANDARD_CURSOR;
2082 if ( setCursor )
2083 SetCursor(*m_currentCursor);
2088 void wxListHeaderWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
2090 m_owner->SetFocus();
2091 m_owner->Update();
2094 bool wxListHeaderWindow::SendListEvent(wxEventType type, const wxPoint& pos)
2096 wxWindow *parent = GetParent();
2097 wxListEvent le( type, parent->GetId() );
2098 le.SetEventObject( parent );
2099 le.m_pointDrag = pos;
2101 // the position should be relative to the parent window, not
2102 // this one for compatibility with MSW and common sense: the
2103 // user code doesn't know anything at all about this header
2104 // window, so why should it get positions relative to it?
2105 le.m_pointDrag.y -= GetSize().y;
2107 le.m_col = m_column;
2108 return !parent->GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
2111 //-----------------------------------------------------------------------------
2112 // wxListRenameTimer (internal)
2113 //-----------------------------------------------------------------------------
2115 wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
2117 m_owner = owner;
2120 void wxListRenameTimer::Notify()
2122 m_owner->OnRenameTimer();
2125 //-----------------------------------------------------------------------------
2126 // wxListTextCtrlWrapper (internal)
2127 //-----------------------------------------------------------------------------
2129 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper, wxEvtHandler)
2130 EVT_CHAR (wxListTextCtrlWrapper::OnChar)
2131 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp)
2132 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus)
2133 END_EVENT_TABLE()
2135 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow *owner,
2136 wxTextCtrl *text,
2137 size_t itemEdit)
2138 : m_startValue(owner->GetItemText(itemEdit)),
2139 m_itemEdited(itemEdit)
2141 m_owner = owner;
2142 m_text = text;
2143 m_finished = false;
2144 m_aboutToFinish = false;
2146 wxRect rectLabel = owner->GetLineLabelRect(itemEdit);
2148 m_owner->CalcScrolledPosition(rectLabel.x, rectLabel.y,
2149 &rectLabel.x, &rectLabel.y);
2151 m_text->Create(owner, wxID_ANY, m_startValue,
2152 wxPoint(rectLabel.x-4,rectLabel.y-4),
2153 wxSize(rectLabel.width+11,rectLabel.height+8));
2154 m_text->SetFocus();
2156 m_text->PushEventHandler(this);
2159 void wxListTextCtrlWrapper::Finish()
2161 if ( !m_finished )
2163 m_finished = true;
2165 m_text->RemoveEventHandler(this);
2166 m_owner->FinishEditing(m_text);
2168 wxPendingDelete.Append( this );
2172 bool wxListTextCtrlWrapper::AcceptChanges()
2174 const wxString value = m_text->GetValue();
2176 // notice that we should always call OnRenameAccept() to generate the "end
2177 // label editing" event, even if the user hasn't really changed anything
2178 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
2180 // vetoed by the user
2181 return false;
2184 // accepted, do rename the item (unless nothing changed)
2185 if ( value != m_startValue )
2186 m_owner->SetItemText(m_itemEdited, value);
2188 return true;
2191 void wxListTextCtrlWrapper::AcceptChangesAndFinish()
2193 m_aboutToFinish = true;
2195 // Notify the owner about the changes
2196 AcceptChanges();
2198 // Even if vetoed, close the control (consistent with MSW)
2199 Finish();
2202 void wxListTextCtrlWrapper::OnChar( wxKeyEvent &event )
2204 switch ( event.m_keyCode )
2206 case WXK_RETURN:
2207 AcceptChangesAndFinish();
2208 break;
2210 case WXK_ESCAPE:
2211 m_owner->OnRenameCancelled( m_itemEdited );
2212 Finish();
2213 break;
2215 default:
2216 event.Skip();
2220 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
2222 if (m_finished)
2224 event.Skip();
2225 return;
2228 // auto-grow the textctrl:
2229 wxSize parentSize = m_owner->GetSize();
2230 wxPoint myPos = m_text->GetPosition();
2231 wxSize mySize = m_text->GetSize();
2232 int sx, sy;
2233 m_text->GetTextExtent(m_text->GetValue() + _T("MM"), &sx, &sy);
2234 if (myPos.x + sx > parentSize.x)
2235 sx = parentSize.x - myPos.x;
2236 if (mySize.x > sx)
2237 sx = mySize.x;
2238 m_text->SetSize(sx, wxDefaultCoord);
2240 event.Skip();
2243 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
2245 if ( !m_finished && !m_aboutToFinish )
2247 if ( !AcceptChanges() )
2248 m_owner->OnRenameCancelled( m_itemEdited );
2250 Finish();
2253 // We must let the native text control handle focus
2254 event.Skip();
2257 //-----------------------------------------------------------------------------
2258 // wxListMainWindow
2259 //-----------------------------------------------------------------------------
2261 BEGIN_EVENT_TABLE(wxListMainWindow,wxScrolledWindow)
2262 EVT_PAINT (wxListMainWindow::OnPaint)
2263 EVT_ERASE_BACKGROUND (wxListMainWindow::OnErase)
2264 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse)
2265 EVT_CHAR (wxListMainWindow::OnChar)
2266 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown)
2267 EVT_KEY_UP (wxListMainWindow::OnKeyUp)
2268 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus)
2269 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus)
2270 EVT_SCROLLWIN (wxListMainWindow::OnScroll)
2271 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus)
2272 END_EVENT_TABLE()
2274 void wxListMainWindow::Init()
2276 m_dirty = true;
2277 m_countVirt = 0;
2278 m_lineFrom =
2279 m_lineTo = (size_t)-1;
2280 m_linesPerPage = 0;
2282 m_headerWidth =
2283 m_lineHeight = 0;
2285 m_small_image_list = (wxImageList *) NULL;
2286 m_normal_image_list = (wxImageList *) NULL;
2288 m_small_spacing = 30;
2289 m_normal_spacing = 40;
2291 m_hasFocus = false;
2292 m_dragCount = 0;
2293 m_isCreated = false;
2295 m_lastOnSame = false;
2296 m_renameTimer = new wxListRenameTimer( this );
2297 m_textctrlWrapper = NULL;
2299 m_current =
2300 m_lineLastClicked =
2301 m_lineSelectSingleOnUp =
2302 m_lineBeforeLastClicked = (size_t)-1;
2304 m_freezeCount = 0;
2307 wxListMainWindow::wxListMainWindow()
2309 Init();
2312 wxListMainWindow::wxListMainWindow( wxWindow *parent,
2313 wxWindowID id,
2314 const wxPoint& pos,
2315 const wxSize& size,
2316 long style,
2317 const wxString &name )
2318 : wxScrolledWindow( parent, id, pos, size,
2319 style | wxHSCROLL | wxVSCROLL, name )
2321 Init();
2323 m_highlightBrush = *(wxTheBrushList->FindOrCreateBrush(
2324 wxSystemSettings::GetColour
2326 wxSYS_COLOUR_HIGHLIGHT
2328 wxSOLID
2331 m_highlightUnfocusedBrush = *(wxTheBrushList->FindOrCreateBrush(
2332 wxSystemSettings::GetColour
2334 wxSYS_COLOUR_BTNSHADOW
2336 wxSOLID
2339 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2341 wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes();
2342 SetOwnForegroundColour( attr.colFg );
2343 SetOwnBackgroundColour( attr.colBg );
2344 if (!m_hasFont)
2345 SetOwnFont( attr.font );
2348 wxListMainWindow::~wxListMainWindow()
2350 DoDeleteAllItems();
2351 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
2352 WX_CLEAR_ARRAY(m_aColWidths);
2354 delete m_renameTimer;
2357 void wxListMainWindow::CacheLineData(size_t line)
2359 wxGenericListCtrl *listctrl = GetListCtrl();
2361 wxListLineData *ld = GetDummyLine();
2363 size_t countCol = GetColumnCount();
2364 for ( size_t col = 0; col < countCol; col++ )
2366 ld->SetText(col, listctrl->OnGetItemText(line, col));
2367 ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col));
2370 ld->SetAttr(listctrl->OnGetItemAttr(line));
2373 wxListLineData *wxListMainWindow::GetDummyLine() const
2375 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2376 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2378 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
2380 // we need to recreate the dummy line if the number of columns in the
2381 // control changed as it would have the incorrect number of fields
2382 // otherwise
2383 if ( !m_lines.IsEmpty() &&
2384 m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() )
2386 self->m_lines.Clear();
2389 if ( m_lines.IsEmpty() )
2391 wxListLineData *line = new wxListLineData(self);
2392 self->m_lines.Add(line);
2394 // don't waste extra memory -- there never going to be anything
2395 // else/more in this array
2396 self->m_lines.Shrink();
2399 return &m_lines[0];
2402 // ----------------------------------------------------------------------------
2403 // line geometry (report mode only)
2404 // ----------------------------------------------------------------------------
2406 wxCoord wxListMainWindow::GetLineHeight() const
2408 // we cache the line height as calling GetTextExtent() is slow
2409 if ( !m_lineHeight )
2411 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
2413 wxClientDC dc( self );
2414 dc.SetFont( GetFont() );
2416 wxCoord y;
2417 dc.GetTextExtent(_T("H"), NULL, &y);
2419 if ( m_small_image_list && m_small_image_list->GetImageCount() )
2421 int iw = 0, ih = 0;
2422 m_small_image_list->GetSize(0, iw, ih);
2423 y = wxMax(y, ih);
2426 y += EXTRA_HEIGHT;
2427 self->m_lineHeight = y + LINE_SPACING;
2430 return m_lineHeight;
2433 wxCoord wxListMainWindow::GetLineY(size_t line) const
2435 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2437 return LINE_SPACING + line * GetLineHeight();
2440 wxRect wxListMainWindow::GetLineRect(size_t line) const
2442 if ( !InReportView() )
2443 return GetLine(line)->m_gi->m_rectAll;
2445 wxRect rect;
2446 rect.x = HEADER_OFFSET_X;
2447 rect.y = GetLineY(line);
2448 rect.width = GetHeaderWidth();
2449 rect.height = GetLineHeight();
2451 return rect;
2454 wxRect wxListMainWindow::GetLineLabelRect(size_t line) const
2456 if ( !InReportView() )
2457 return GetLine(line)->m_gi->m_rectLabel;
2459 int image_x = 0;
2460 wxListLineData *data = GetLine(line);
2461 wxListItemDataList::compatibility_iterator node = data->m_items.GetFirst();
2462 if (node)
2464 wxListItemData *item = node->GetData();
2465 if ( item->HasImage() )
2467 int ix, iy;
2468 GetImageSize( item->GetImage(), ix, iy );
2469 image_x = 3 + ix + IMAGE_MARGIN_IN_REPORT_MODE;
2473 wxRect rect;
2474 rect.x = image_x + HEADER_OFFSET_X;
2475 rect.y = GetLineY(line);
2476 rect.width = GetColumnWidth(0) - image_x;
2477 rect.height = GetLineHeight();
2479 return rect;
2482 wxRect wxListMainWindow::GetLineIconRect(size_t line) const
2484 if ( !InReportView() )
2485 return GetLine(line)->m_gi->m_rectIcon;
2487 wxListLineData *ld = GetLine(line);
2488 wxASSERT_MSG( ld->HasImage(), _T("should have an image") );
2490 wxRect rect;
2491 rect.x = HEADER_OFFSET_X;
2492 rect.y = GetLineY(line);
2493 GetImageSize(ld->GetImage(), rect.width, rect.height);
2495 return rect;
2498 wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const
2500 return InReportView() ? GetLineRect(line)
2501 : GetLine(line)->m_gi->m_rectHighlight;
2504 long wxListMainWindow::HitTestLine(size_t line, int x, int y) const
2506 wxASSERT_MSG( line < GetItemCount(), _T("invalid line in HitTestLine") );
2508 wxListLineData *ld = GetLine(line);
2510 if ( ld->HasImage() && GetLineIconRect(line).Contains(x, y) )
2511 return wxLIST_HITTEST_ONITEMICON;
2513 // VS: Testing for "ld->HasText() || InReportView()" instead of
2514 // "ld->HasText()" is needed to make empty lines in report view
2515 // possible
2516 if ( ld->HasText() || InReportView() )
2518 wxRect rect = InReportView() ? GetLineRect(line)
2519 : GetLineLabelRect(line);
2521 if ( rect.Contains(x, y) )
2522 return wxLIST_HITTEST_ONITEMLABEL;
2525 return 0;
2528 // ----------------------------------------------------------------------------
2529 // highlight (selection) handling
2530 // ----------------------------------------------------------------------------
2532 bool wxListMainWindow::IsHighlighted(size_t line) const
2534 if ( IsVirtual() )
2536 return m_selStore.IsSelected(line);
2538 else // !virtual
2540 wxListLineData *ld = GetLine(line);
2541 wxCHECK_MSG( ld, false, _T("invalid index in IsHighlighted") );
2543 return ld->IsHighlighted();
2547 void wxListMainWindow::HighlightLines( size_t lineFrom,
2548 size_t lineTo,
2549 bool highlight )
2551 if ( IsVirtual() )
2553 wxArrayInt linesChanged;
2554 if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight,
2555 &linesChanged) )
2557 // meny items changed state, refresh everything
2558 RefreshLines(lineFrom, lineTo);
2560 else // only a few items changed state, refresh only them
2562 size_t count = linesChanged.GetCount();
2563 for ( size_t n = 0; n < count; n++ )
2565 RefreshLine(linesChanged[n]);
2569 else // iterate over all items in non report view
2571 for ( size_t line = lineFrom; line <= lineTo; line++ )
2573 if ( HighlightLine(line, highlight) )
2574 RefreshLine(line);
2579 bool wxListMainWindow::HighlightLine( size_t line, bool highlight )
2581 bool changed;
2583 if ( IsVirtual() )
2585 changed = m_selStore.SelectItem(line, highlight);
2587 else // !virtual
2589 wxListLineData *ld = GetLine(line);
2590 wxCHECK_MSG( ld, false, _T("invalid index in HighlightLine") );
2592 changed = ld->Highlight(highlight);
2595 if ( changed )
2597 SendNotify( line, highlight ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2598 : wxEVT_COMMAND_LIST_ITEM_DESELECTED );
2601 return changed;
2604 void wxListMainWindow::RefreshLine( size_t line )
2606 if ( InReportView() )
2608 size_t visibleFrom, visibleTo;
2609 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2611 if ( line < visibleFrom || line > visibleTo )
2612 return;
2615 wxRect rect = GetLineRect(line);
2617 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2618 RefreshRect( rect );
2621 void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo )
2623 // we suppose that they are ordered by caller
2624 wxASSERT_MSG( lineFrom <= lineTo, _T("indices in disorder") );
2626 wxASSERT_MSG( lineTo < GetItemCount(), _T("invalid line range") );
2628 if ( InReportView() )
2630 size_t visibleFrom, visibleTo;
2631 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2633 if ( lineFrom < visibleFrom )
2634 lineFrom = visibleFrom;
2635 if ( lineTo > visibleTo )
2636 lineTo = visibleTo;
2638 wxRect rect;
2639 rect.x = 0;
2640 rect.y = GetLineY(lineFrom);
2641 rect.width = GetClientSize().x;
2642 rect.height = GetLineY(lineTo) - rect.y + GetLineHeight();
2644 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2645 RefreshRect( rect );
2647 else // !report
2649 // TODO: this should be optimized...
2650 for ( size_t line = lineFrom; line <= lineTo; line++ )
2652 RefreshLine(line);
2657 void wxListMainWindow::RefreshAfter( size_t lineFrom )
2659 if ( InReportView() )
2661 size_t visibleFrom, visibleTo;
2662 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2664 if ( lineFrom < visibleFrom )
2665 lineFrom = visibleFrom;
2666 else if ( lineFrom > visibleTo )
2667 return;
2669 wxRect rect;
2670 rect.x = 0;
2671 rect.y = GetLineY(lineFrom);
2672 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2674 wxSize size = GetClientSize();
2675 rect.width = size.x;
2677 // refresh till the bottom of the window
2678 rect.height = size.y - rect.y;
2680 RefreshRect( rect );
2682 else // !report
2684 // TODO: how to do it more efficiently?
2685 m_dirty = true;
2689 void wxListMainWindow::RefreshSelected()
2691 if ( IsEmpty() )
2692 return;
2694 size_t from, to;
2695 if ( InReportView() )
2697 GetVisibleLinesRange(&from, &to);
2699 else // !virtual
2701 from = 0;
2702 to = GetItemCount() - 1;
2705 if ( HasCurrent() && m_current >= from && m_current <= to )
2706 RefreshLine(m_current);
2708 for ( size_t line = from; line <= to; line++ )
2710 // NB: the test works as expected even if m_current == -1
2711 if ( line != m_current && IsHighlighted(line) )
2712 RefreshLine(line);
2716 void wxListMainWindow::Freeze()
2718 m_freezeCount++;
2721 void wxListMainWindow::Thaw()
2723 wxCHECK_RET( m_freezeCount > 0, _T("thawing unfrozen list control?") );
2725 if ( --m_freezeCount == 0 )
2726 Refresh();
2729 void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2731 // Note: a wxPaintDC must be constructed even if no drawing is
2732 // done (a Windows requirement).
2733 wxBufferedPaintDC dc( this );
2735 // Ensure an uniform background color, as to avoid differences between
2736 // the automatically cleared parts and the rest of the canvas.
2737 dc.SetBackground(*(wxTheBrushList->FindOrCreateBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX), wxSOLID)));
2739 // We need to clear the DC manually, since we intercept BG-erase events.
2740 // Clearing must be done first thing because caching of the double-buffering causes artifacts otherwise.
2741 dc.Clear();
2743 if ( m_freezeCount )
2744 return;
2746 if ( m_dirty )
2747 // delay the repainting until we calculate all the items positions
2748 return;
2750 PrepareDC( dc );
2752 // IsEmpty is checked now, after clearing, to avoid garbage on empty lists.
2753 if ( IsEmpty() ) {
2754 return;
2757 int dev_x, dev_y;
2758 CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
2760 dc.SetFont( GetFont() );
2762 if ( InReportView() )
2764 int lineHeight = GetLineHeight();
2766 size_t visibleFrom, visibleTo;
2767 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2769 wxRect rectLine;
2770 int xOrig = dc.LogicalToDeviceX( 0 );
2771 int yOrig = dc.LogicalToDeviceY( 0 );
2773 // tell the caller cache to cache the data
2774 if ( IsVirtual() )
2776 wxListEvent evCache(wxEVT_COMMAND_LIST_CACHE_HINT,
2777 GetParent()->GetId());
2778 evCache.SetEventObject( GetParent() );
2779 evCache.m_oldItemIndex = visibleFrom;
2780 evCache.m_itemIndex = visibleTo;
2781 GetParent()->GetEventHandler()->ProcessEvent( evCache );
2784 for ( size_t line = visibleFrom; line <= visibleTo; line++ )
2786 rectLine = GetLineRect(line);
2789 if ( !IsExposed(rectLine.x + xOrig, rectLine.y + yOrig,
2790 rectLine.width, rectLine.height) )
2792 // don't redraw unaffected lines to avoid flicker
2793 continue;
2796 if (HasFlag(wxLC_OWNERDRAW)) {
2797 ((wxGenericListCtrl*)m_parent)->OnDrawItem(line, &dc, rectLine, GetLineHighlightRect(line), IsHighlighted(line));
2798 } else {
2799 GetLine(line)->DrawInReportMode( &dc,
2800 rectLine,
2801 GetLineHighlightRect(line),
2802 IsHighlighted(line) );
2806 if ( HasFlag(wxLC_HRULES) )
2808 wxPen pen = *(wxThePenList->FindOrCreatePen(GetRuleColour(), 1, wxSOLID));
2809 wxSize clientSize = GetClientSize();
2811 size_t i = visibleFrom;
2812 if (i == 0) i = 1; // Don't draw the first one
2813 for ( ; i <= visibleTo; i++ )
2815 dc.SetPen(pen);
2816 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2817 dc.DrawLine(0 - dev_x, i * lineHeight,
2818 clientSize.x - dev_x, i * lineHeight);
2821 // Draw last horizontal rule
2822 if ( visibleTo == GetItemCount() - 1 )
2824 dc.SetPen( pen );
2825 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2826 dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight,
2827 clientSize.x - dev_x , (m_lineTo + 1) * lineHeight );
2831 // Draw vertical rules if required
2832 if ( HasFlag(wxLC_VRULES) && !IsEmpty() )
2834 wxPen pen = *(wxThePenList->FindOrCreatePen(GetRuleColour(), 1, wxSOLID));
2835 wxRect firstItemRect, lastItemRect;
2837 GetItemRect(visibleFrom, firstItemRect);
2838 GetItemRect(visibleTo, lastItemRect);
2839 int x = firstItemRect.GetX();
2840 dc.SetPen(pen);
2841 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2843 for (int col = 0; col < GetColumnCount(); col++)
2845 int colWidth = GetColumnWidth(col);
2846 x += colWidth;
2847 int x_pos = x - dev_x;
2848 if (col < GetColumnCount()-1) x_pos -= 2;
2849 dc.DrawLine(x_pos, firstItemRect.GetY() - 1 - dev_y,
2850 x_pos, lastItemRect.GetBottom() + 1 - dev_y);
2854 else // !report
2856 size_t count = GetItemCount();
2857 for ( size_t i = 0; i < count; i++ )
2859 GetLine(i)->Draw( &dc );
2863 #ifndef __WXMAC__
2864 // Don't draw rect outline under Mac at all.
2865 if ( HasCurrent() )
2867 if ( m_hasFocus )
2869 wxRect rect( GetLineHighlightRect( m_current ) );
2870 #ifndef __WXGTK20__
2871 dc.SetPen( *wxBLACK_PEN );
2872 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2873 dc.DrawRectangle( rect );
2874 #else
2875 wxRendererNative::Get().DrawItemSelectionRect( this, dc, rect, wxCONTROL_CURRENT|wxCONTROL_FOCUSED );
2877 #endif
2880 #endif
2883 void wxListMainWindow::HighlightAll( bool on )
2885 if ( IsSingleSel() )
2887 wxASSERT_MSG( !on, _T("can't do this in a single selection control") );
2889 // we just have one item to turn off
2890 if ( HasCurrent() && IsHighlighted(m_current) )
2892 HighlightLine(m_current, false);
2893 RefreshLine(m_current);
2896 else // multi selection
2898 if ( !IsEmpty() )
2899 HighlightLines(0, GetItemCount() - 1, on);
2903 void wxListMainWindow::OnChildFocus(wxChildFocusEvent& WXUNUSED(event))
2905 // Do nothing here. This prevents the default handler in wxScrolledWindow
2906 // from needlessly scrolling the window when the edit control is
2907 // dismissed. See ticket #9563.
2910 void wxListMainWindow::SendNotify( size_t line,
2911 wxEventType command,
2912 const wxPoint& point )
2914 wxListEvent le( command, GetParent()->GetId() );
2915 le.SetEventObject( GetParent() );
2917 le.m_itemIndex = line;
2919 // set only for events which have position
2920 if ( point != wxDefaultPosition )
2921 le.m_pointDrag = point;
2923 // don't try to get the line info for virtual list controls: the main
2924 // program has it anyhow and if we did it would result in accessing all
2925 // the lines, even those which are not visible now and this is precisely
2926 // what we're trying to avoid
2927 if ( !IsVirtual() )
2929 if ( line != (size_t)-1 )
2931 GetLine(line)->GetItem( 0, le.m_item );
2933 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2935 //else: there may be no more such item
2937 GetParent()->GetEventHandler()->ProcessEvent( le );
2940 void wxListMainWindow::ChangeCurrent(size_t current)
2942 m_current = current;
2944 // as the current item changed, we shouldn't start editing it when the
2945 // "slow click" timer expires as the click happened on another item
2946 if ( m_renameTimer->IsRunning() )
2947 m_renameTimer->Stop();
2949 SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
2952 wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
2954 wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
2955 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2957 wxASSERT_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)),
2958 wxT("EditLabel() needs a text control") );
2960 size_t itemEdit = (size_t)item;
2962 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
2963 le.SetEventObject( GetParent() );
2964 le.m_itemIndex = item;
2965 wxListLineData *data = GetLine(itemEdit);
2966 wxCHECK_MSG( data, NULL, _T("invalid index in EditLabel()") );
2967 data->GetItem( 0, le.m_item );
2969 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
2971 // vetoed by user code
2972 return NULL;
2975 // We have to call this here because the label in question might just have
2976 // been added and no screen update taken place.
2977 if ( m_dirty )
2979 wxSafeYield();
2981 // Pending events dispatched by wxSafeYield might have changed the item
2982 // count
2983 if ( (size_t)item >= GetItemCount() )
2984 return NULL;
2987 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
2988 m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
2989 return m_textctrlWrapper->GetText();
2992 void wxListMainWindow::OnRenameTimer()
2994 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2996 EditLabel( m_current );
2999 bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
3001 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
3002 le.SetEventObject( GetParent() );
3003 le.m_itemIndex = itemEdit;
3005 wxListLineData *data = GetLine(itemEdit);
3007 wxCHECK_MSG( data, false, _T("invalid index in OnRenameAccept()") );
3009 data->GetItem( 0, le.m_item );
3010 le.m_item.m_text = value;
3011 return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
3012 le.IsAllowed();
3015 void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
3017 // let owner know that the edit was cancelled
3018 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
3020 le.SetEditCanceled(true);
3022 le.SetEventObject( GetParent() );
3023 le.m_itemIndex = itemEdit;
3025 wxListLineData *data = GetLine(itemEdit);
3026 wxCHECK_RET( data, _T("invalid index in OnRenameCancelled()") );
3028 data->GetItem( 0, le.m_item );
3029 GetEventHandler()->ProcessEvent( le );
3032 void wxListMainWindow::OnMouse( wxMouseEvent &event )
3035 #ifdef __WXMAC__
3036 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
3037 // shutdown the edit control when the mouse is clicked elsewhere on the
3038 // listctrl because the order of events is different (or something like
3039 // that), so explicitly end the edit if it is active.
3040 if ( event.LeftDown() && m_textctrlWrapper )
3041 m_textctrlWrapper->AcceptChangesAndFinish();
3042 #endif // __WXMAC__
3044 if ( event.LeftDown() )
3045 SetFocusIgnoringChildren();
3047 event.SetEventObject( GetParent() );
3048 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3049 return;
3051 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
3053 // let the base handle mouse wheel events.
3054 event.Skip();
3055 return;
3058 if ( !HasCurrent() || IsEmpty() )
3060 if (event.RightDown())
3062 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3063 // Allow generation of context menu event
3064 event.Skip();
3066 return;
3069 if (m_dirty)
3070 return;
3072 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
3073 event.ButtonDClick()) )
3074 return;
3076 int x = event.GetX();
3077 int y = event.GetY();
3078 CalcUnscrolledPosition( x, y, &x, &y );
3080 // where did we hit it (if we did)?
3081 long hitResult = 0;
3083 size_t count = GetItemCount(),
3084 current;
3086 if ( InReportView() )
3088 current = y / GetLineHeight();
3089 if ( current < count )
3090 hitResult = HitTestLine(current, x, y);
3092 else // !report
3094 // TODO: optimize it too! this is less simple than for report view but
3095 // enumerating all items is still not a way to do it!!
3096 for ( current = 0; current < count; current++ )
3098 hitResult = HitTestLine(current, x, y);
3099 if ( hitResult )
3100 break;
3104 if (event.Dragging())
3106 if (m_dragCount == 0)
3108 // we have to report the raw, physical coords as we want to be
3109 // able to call HitTest(event.m_pointDrag) from the user code to
3110 // get the item being dragged
3111 m_dragStart = event.GetPosition();
3114 m_dragCount++;
3116 if (m_dragCount != 3)
3117 return;
3119 int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3120 : wxEVT_COMMAND_LIST_BEGIN_DRAG;
3122 wxListEvent le( command, GetParent()->GetId() );
3123 le.SetEventObject( GetParent() );
3124 le.m_itemIndex = m_lineLastClicked;
3125 le.m_pointDrag = m_dragStart;
3126 GetParent()->GetEventHandler()->ProcessEvent( le );
3128 return;
3130 else
3132 m_dragCount = 0;
3135 if ( !hitResult )
3137 // outside of any item
3138 if (event.RightDown())
3140 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3142 wxContextMenuEvent evtCtx(
3143 wxEVT_CONTEXT_MENU,
3144 GetParent()->GetId(),
3145 ClientToScreen(event.GetPosition()));
3146 evtCtx.SetEventObject(GetParent());
3147 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
3149 else
3151 // reset the selection and bail out
3152 HighlightAll(false);
3155 return;
3158 bool forceClick = false;
3159 if (event.ButtonDClick())
3161 if ( m_renameTimer->IsRunning() )
3162 m_renameTimer->Stop();
3164 m_lastOnSame = false;
3166 if ( current == m_lineLastClicked )
3168 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3170 return;
3172 else
3174 // The first click was on another item, so don't interpret this as
3175 // a double click, but as a simple click instead
3176 forceClick = true;
3180 if (event.LeftUp())
3182 if (m_lineSelectSingleOnUp != (size_t)-1)
3184 // select single line
3185 HighlightAll( false );
3186 ReverseHighlight(m_lineSelectSingleOnUp);
3189 if (m_lastOnSame)
3191 if ((current == m_current) &&
3192 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
3193 HasFlag(wxLC_EDIT_LABELS) )
3195 if (InReportView())
3197 wxRect label = GetLineLabelRect( current );
3198 if (label.Contains( x, y ))
3199 m_renameTimer->Start( 250, true );
3202 else
3203 m_renameTimer->Start( 250, true );
3207 m_lastOnSame = false;
3208 m_lineSelectSingleOnUp = (size_t)-1;
3210 else
3212 // This is necessary, because after a DnD operation in
3213 // from and to ourself, the up event is swallowed by the
3214 // DnD code. So on next non-up event (which means here and
3215 // now) m_lineSelectSingleOnUp should be reset.
3216 m_lineSelectSingleOnUp = (size_t)-1;
3218 if (event.RightDown())
3220 m_lineBeforeLastClicked = m_lineLastClicked;
3221 m_lineLastClicked = current;
3223 // If the item is already selected, do not update the selection.
3224 // Multi-selections should not be cleared if a selected item is clicked.
3225 if (!IsHighlighted(current))
3227 HighlightAll(false);
3228 ChangeCurrent(current);
3229 ReverseHighlight(m_current);
3232 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3234 wxContextMenuEvent evtCtx(
3235 wxEVT_CONTEXT_MENU,
3236 GetParent()->GetId(),
3237 ClientToScreen(event.GetPosition()));
3238 evtCtx.SetEventObject(GetParent());
3239 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
3241 else if (event.MiddleDown())
3243 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
3245 else if ( event.LeftDown() || forceClick )
3247 m_lineBeforeLastClicked = m_lineLastClicked;
3248 m_lineLastClicked = current;
3250 size_t oldCurrent = m_current;
3251 bool oldWasSelected = IsHighlighted(m_current);
3253 bool cmdModifierDown = event.CmdDown();
3254 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
3256 if ( IsSingleSel() || !IsHighlighted(current) )
3258 HighlightAll( false );
3260 ChangeCurrent(current);
3262 ReverseHighlight(m_current);
3264 else // multi sel & current is highlighted & no mod keys
3266 m_lineSelectSingleOnUp = current;
3267 ChangeCurrent(current); // change focus
3270 else // multi sel & either ctrl or shift is down
3272 if (cmdModifierDown)
3274 ChangeCurrent(current);
3276 ReverseHighlight(m_current);
3278 else if (event.ShiftDown())
3280 ChangeCurrent(current);
3282 size_t lineFrom = oldCurrent,
3283 lineTo = current;
3285 if ( lineTo < lineFrom )
3287 lineTo = lineFrom;
3288 lineFrom = m_current;
3291 HighlightLines(lineFrom, lineTo);
3293 else // !ctrl, !shift
3295 // test in the enclosing if should make it impossible
3296 wxFAIL_MSG( _T("how did we get here?") );
3300 if (m_current != oldCurrent)
3301 RefreshLine( oldCurrent );
3303 // forceClick is only set if the previous click was on another item
3304 m_lastOnSame = !forceClick && (m_current == oldCurrent) && oldWasSelected;
3308 void wxListMainWindow::MoveToItem(size_t item)
3310 if ( item == (size_t)-1 )
3311 return;
3313 wxRect rect = GetLineRect(item);
3315 int client_w, client_h;
3316 GetClientSize( &client_w, &client_h );
3318 const int hLine = GetLineHeight();
3320 int view_x = SCROLL_UNIT_X * GetScrollPos( wxHORIZONTAL );
3321 int view_y = hLine * GetScrollPos( wxVERTICAL );
3323 if ( InReportView() )
3325 // the next we need the range of lines shown it might be different,
3326 // so recalculate it
3327 ResetVisibleLinesRange();
3329 if (rect.y < view_y)
3330 Scroll( -1, rect.y / hLine );
3331 if (rect.y + rect.height + 5 > view_y + client_h)
3332 Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
3334 #ifdef __WXMAC__
3335 // At least on Mac the visible lines value will get reset inside of
3336 // Scroll *before* it actually scrolls the window because of the
3337 // Update() that happens there, so it will still have the wrong value.
3338 // So let's reset it again and wait for it to be recalculated in the
3339 // next paint event. I would expect this problem to show up in wxGTK
3340 // too but couldn't duplicate it there. Perhaps the order of events
3341 // is different... --Robin
3342 ResetVisibleLinesRange();
3343 #endif
3345 else // !report
3347 if (rect.x-view_x < 5)
3348 Scroll( (rect.x - 5) / SCROLL_UNIT_X, -1 );
3349 if (rect.x + rect.width - 5 > view_x + client_w)
3350 Scroll( (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X, -1 );
3354 // ----------------------------------------------------------------------------
3355 // keyboard handling
3356 // ----------------------------------------------------------------------------
3358 void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
3360 wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
3361 _T("invalid item index in OnArrowChar()") );
3363 size_t oldCurrent = m_current;
3365 // in single selection we just ignore Shift as we can't select several
3366 // items anyhow
3367 if ( event.ShiftDown() && !IsSingleSel() )
3369 ChangeCurrent(newCurrent);
3371 // refresh the old focus to remove it
3372 RefreshLine( oldCurrent );
3374 // select all the items between the old and the new one
3375 if ( oldCurrent > newCurrent )
3377 newCurrent = oldCurrent;
3378 oldCurrent = m_current;
3381 HighlightLines(oldCurrent, newCurrent);
3383 else // !shift
3385 // all previously selected items are unselected unless ctrl is held
3386 // in a multiselection control
3387 if ( !event.ControlDown() || IsSingleSel() )
3388 HighlightAll(false);
3390 ChangeCurrent(newCurrent);
3392 // refresh the old focus to remove it
3393 RefreshLine( oldCurrent );
3395 // in single selection mode we must always have a selected item
3396 if ( !event.ControlDown() || IsSingleSel() )
3397 HighlightLine( m_current, true );
3400 RefreshLine( m_current );
3402 MoveToFocus();
3405 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
3407 wxWindow *parent = GetParent();
3409 // propagate the key event upwards
3410 wxKeyEvent ke(event);
3411 if (parent->GetEventHandler()->ProcessEvent( ke ))
3412 return;
3414 event.Skip();
3417 void wxListMainWindow::OnKeyUp( wxKeyEvent &event )
3419 wxWindow *parent = GetParent();
3421 // propagate the key event upwards
3422 wxKeyEvent ke(event);
3423 ke.SetEventObject( parent );
3424 if (parent->GetEventHandler()->ProcessEvent( ke ))
3425 return;
3427 event.Skip();
3430 void wxListMainWindow::OnChar( wxKeyEvent &event )
3432 wxWindow *parent = GetParent();
3434 // send a list_key event up
3435 if ( HasCurrent() )
3437 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() );
3438 le.m_itemIndex = m_current;
3439 GetLine(m_current)->GetItem( 0, le.m_item );
3440 le.m_code = event.GetKeyCode();
3441 le.SetEventObject( parent );
3442 parent->GetEventHandler()->ProcessEvent( le );
3445 // propagate the char event upwards
3446 wxKeyEvent ke(event);
3447 if (parent->GetEventHandler()->ProcessEvent( ke ))
3448 return;
3450 if (event.GetKeyCode() == WXK_TAB)
3452 wxNavigationKeyEvent nevent;
3453 nevent.SetWindowChange( event.ControlDown() );
3454 nevent.SetDirection( !event.ShiftDown() );
3455 nevent.SetEventObject( GetParent()->GetParent() );
3456 nevent.SetCurrentFocus( m_parent );
3457 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent ))
3458 return;
3461 // no item -> nothing to do
3462 if (!HasCurrent())
3464 event.Skip();
3465 return;
3468 // don't use m_linesPerPage directly as it might not be computed yet
3469 const int pageSize = GetCountPerPage();
3470 wxCHECK_RET( pageSize, _T("should have non zero page size") );
3472 if (GetLayoutDirection() == wxLayout_RightToLeft)
3474 if (event.GetKeyCode() == WXK_RIGHT)
3475 event.m_keyCode = WXK_LEFT;
3476 else if (event.GetKeyCode() == WXK_LEFT)
3477 event.m_keyCode = WXK_RIGHT;
3480 switch ( event.GetKeyCode() )
3482 case WXK_UP:
3483 if ( m_current > 0 )
3484 OnArrowChar( m_current - 1, event );
3485 break;
3487 case WXK_DOWN:
3488 if ( m_current < (size_t)GetItemCount() - 1 )
3489 OnArrowChar( m_current + 1, event );
3490 break;
3492 case WXK_END:
3493 if (!IsEmpty())
3494 OnArrowChar( GetItemCount() - 1, event );
3495 break;
3497 case WXK_HOME:
3498 if (!IsEmpty())
3499 OnArrowChar( 0, event );
3500 break;
3502 case WXK_PAGEUP:
3504 int steps = InReportView() ? pageSize - 1
3505 : m_current % pageSize;
3507 int index = m_current - steps;
3508 if (index < 0)
3509 index = 0;
3511 OnArrowChar( index, event );
3513 break;
3515 case WXK_PAGEDOWN:
3517 int steps = InReportView()
3518 ? pageSize - 1
3519 : pageSize - (m_current % pageSize) - 1;
3521 size_t index = m_current + steps;
3522 size_t count = GetItemCount();
3523 if ( index >= count )
3524 index = count - 1;
3526 OnArrowChar( index, event );
3528 break;
3530 case WXK_LEFT:
3531 if ( !InReportView() )
3533 int index = m_current - pageSize;
3534 if (index < 0)
3535 index = 0;
3537 OnArrowChar( index, event );
3539 break;
3541 case WXK_RIGHT:
3542 if ( !InReportView() )
3544 size_t index = m_current + pageSize;
3546 size_t count = GetItemCount();
3547 if ( index >= count )
3548 index = count - 1;
3550 OnArrowChar( index, event );
3552 break;
3554 case WXK_SPACE:
3555 if ( IsSingleSel() )
3557 if ( event.ControlDown() )
3559 ReverseHighlight(m_current);
3561 else // normal space press
3563 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3566 else // multiple selection
3568 ReverseHighlight(m_current);
3570 break;
3572 case WXK_RETURN:
3573 case WXK_EXECUTE:
3574 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3575 break;
3577 default:
3578 event.Skip();
3582 // ----------------------------------------------------------------------------
3583 // focus handling
3584 // ----------------------------------------------------------------------------
3586 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
3588 if ( GetParent() )
3590 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
3591 event.SetEventObject( GetParent() );
3592 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3593 return;
3596 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3597 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3598 // which are already drawn correctly resulting in horrible flicker - avoid
3599 // it
3600 if ( !m_hasFocus )
3602 m_hasFocus = true;
3604 RefreshSelected();
3608 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
3610 if ( GetParent() )
3612 wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
3613 event.SetEventObject( GetParent() );
3614 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3615 return;
3618 m_hasFocus = false;
3619 RefreshSelected();
3622 void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
3624 if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
3626 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3628 else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
3630 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3632 else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
3634 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3636 else if ( InReportView() && (m_small_image_list))
3638 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3642 void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
3644 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
3646 m_normal_image_list->GetSize( index, width, height );
3648 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
3650 m_small_image_list->GetSize( index, width, height );
3652 else if ( HasFlag(wxLC_LIST) && m_small_image_list )
3654 m_small_image_list->GetSize( index, width, height );
3656 else if ( InReportView() && m_small_image_list )
3658 m_small_image_list->GetSize( index, width, height );
3660 else
3662 width =
3663 height = 0;
3667 int wxListMainWindow::GetTextLength( const wxString &s ) const
3669 wxClientDC dc( wxConstCast(this, wxListMainWindow) );
3670 dc.SetFont( GetFont() );
3672 wxCoord lw;
3673 dc.GetTextExtent( s, &lw, NULL );
3675 return lw + AUTOSIZE_COL_MARGIN;
3678 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
3680 m_dirty = true;
3682 // calc the spacing from the icon size
3683 int width = 0, height = 0;
3685 if ((imageList) && (imageList->GetImageCount()) )
3686 imageList->GetSize(0, width, height);
3688 if (which == wxIMAGE_LIST_NORMAL)
3690 m_normal_image_list = imageList;
3691 m_normal_spacing = width + 8;
3694 if (which == wxIMAGE_LIST_SMALL)
3696 m_small_image_list = imageList;
3697 m_small_spacing = width + 14;
3698 m_lineHeight = 0; // ensure that the line height will be recalc'd
3702 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
3704 m_dirty = true;
3705 if (isSmall)
3706 m_small_spacing = spacing;
3707 else
3708 m_normal_spacing = spacing;
3711 int wxListMainWindow::GetItemSpacing( bool isSmall )
3713 return isSmall ? m_small_spacing : m_normal_spacing;
3716 // ----------------------------------------------------------------------------
3717 // columns
3718 // ----------------------------------------------------------------------------
3720 void wxListMainWindow::SetColumn( int col, wxListItem &item )
3722 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3724 wxCHECK_RET( node, _T("invalid column index in SetColumn") );
3726 if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
3727 item.m_width = GetTextLength( item.m_text );
3729 wxListHeaderData *column = node->GetData();
3730 column->SetItem( item );
3732 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3733 if ( headerWin )
3734 headerWin->m_dirty = true;
3736 m_dirty = true;
3738 // invalidate it as it has to be recalculated
3739 m_headerWidth = 0;
3742 void wxListMainWindow::SetColumnWidth( int col, int width )
3744 { wxCHECK_RET( col >= 0 && col < GetColumnCount(),
3745 _T("invalid column index") ); }
3747 { wxCHECK_RET( InReportView(),
3748 _T("SetColumnWidth() can only be called in report mode.") ); }
3750 m_dirty = true;
3751 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3752 if ( headerWin )
3753 headerWin->m_dirty = true;
3755 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3756 { wxCHECK_RET( node, _T("no column?") ); }
3758 wxListHeaderData *column = node->GetData();
3760 size_t count = GetItemCount();
3762 if (width == wxLIST_AUTOSIZE_USEHEADER)
3764 width = GetTextLength(column->GetText());
3765 width += 2*EXTRA_WIDTH;
3767 // check for column header's image availability
3768 const int image = column->GetImage();
3769 if ( image != -1 )
3771 if ( m_small_image_list )
3773 int ix = 0, iy = 0;
3774 m_small_image_list->GetSize(image, ix, iy);
3775 width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
3779 else if ( width == wxLIST_AUTOSIZE )
3781 if ( IsVirtual() )
3783 // TODO: determine the max width somehow...
3784 width = WIDTH_COL_DEFAULT;
3786 else // !virtual
3788 wxClientDC dc(this);
3789 dc.SetFont( GetFont() );
3791 int max = AUTOSIZE_COL_MARGIN;
3793 // if the cached column width isn't valid then recalculate it
3794 if (m_aColWidths.Item(col)->bNeedsUpdate)
3796 for (size_t i = 0; i < count; i++)
3798 wxListLineData *line = GetLine( i );
3799 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3801 wxCHECK_RET( n, _T("no subitem?") );
3803 wxListItemData *itemData = n->GetData();
3804 wxListItem item;
3806 itemData->GetItem(item);
3807 int itemWidth = GetItemWidthWithImage(&item);
3808 if (itemWidth > max)
3809 max = itemWidth;
3812 m_aColWidths.Item(col)->bNeedsUpdate = false;
3813 m_aColWidths.Item(col)->nMaxWidth = max;
3816 max = m_aColWidths.Item(col)->nMaxWidth;
3817 width = max + AUTOSIZE_COL_MARGIN;
3821 column->SetWidth( width );
3823 // invalidate it as it has to be recalculated
3824 m_headerWidth = 0;
3827 int wxListMainWindow::GetHeaderWidth() const
3829 if ( !m_headerWidth )
3831 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
3833 size_t count = GetColumnCount();
3834 for ( size_t col = 0; col < count; col++ )
3836 self->m_headerWidth += GetColumnWidth(col);
3840 return m_headerWidth;
3843 void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
3845 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3846 wxCHECK_RET( node, _T("invalid column index in GetColumn") );
3848 wxListHeaderData *column = node->GetData();
3849 column->GetItem( item );
3852 int wxListMainWindow::GetColumnWidth( int col ) const
3854 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3855 wxCHECK_MSG( node, 0, _T("invalid column index") );
3857 wxListHeaderData *column = node->GetData();
3858 return column->GetWidth();
3861 // ----------------------------------------------------------------------------
3862 // item state
3863 // ----------------------------------------------------------------------------
3865 void wxListMainWindow::SetItem( wxListItem &item )
3867 long id = item.m_itemId;
3868 wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
3869 _T("invalid item index in SetItem") );
3871 if ( !IsVirtual() )
3873 wxListLineData *line = GetLine((size_t)id);
3874 line->SetItem( item.m_col, item );
3876 // Set item state if user wants
3877 if ( item.m_mask & wxLIST_MASK_STATE )
3878 SetItemState( item.m_itemId, item.m_state, item.m_state );
3880 if (InReportView())
3882 // update the Max Width Cache if needed
3883 int width = GetItemWidthWithImage(&item);
3885 if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
3886 m_aColWidths.Item(item.m_col)->nMaxWidth = width;
3890 // update the item on screen
3891 wxRect rectItem;
3892 GetItemRect(id, rectItem);
3893 RefreshRect(rectItem);
3896 void wxListMainWindow::SetItemStateAll(long state, long stateMask)
3898 if ( IsEmpty() )
3899 return;
3901 // first deal with selection
3902 if ( stateMask & wxLIST_STATE_SELECTED )
3904 // set/clear select state
3905 if ( IsVirtual() )
3907 // optimized version for virtual listctrl.
3908 m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
3909 Refresh();
3911 else if ( state & wxLIST_STATE_SELECTED )
3913 const long count = GetItemCount();
3914 for( long i = 0; i < count; i++ )
3916 SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
3920 else
3922 // clear for non virtual (somewhat optimized by using GetNextItem())
3923 long i = -1;
3924 while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
3926 SetItemState( i, 0, wxLIST_STATE_SELECTED );
3931 if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
3933 // unfocus all: only one item can be focussed, so clearing focus for
3934 // all items is simply clearing focus of the focussed item.
3935 SetItemState(m_current, state, stateMask);
3937 //(setting focus to all items makes no sense, so it is not handled here.)
3940 void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
3942 if ( litem == -1 )
3944 SetItemStateAll(state, stateMask);
3945 return;
3948 wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
3949 _T("invalid list ctrl item index in SetItem") );
3951 size_t oldCurrent = m_current;
3952 size_t item = (size_t)litem; // safe because of the check above
3954 // do we need to change the focus?
3955 if ( stateMask & wxLIST_STATE_FOCUSED )
3957 if ( state & wxLIST_STATE_FOCUSED )
3959 // don't do anything if this item is already focused
3960 if ( item != m_current )
3962 ChangeCurrent(item);
3964 if ( oldCurrent != (size_t)-1 )
3966 if ( IsSingleSel() )
3968 HighlightLine(oldCurrent, false);
3971 RefreshLine(oldCurrent);
3974 RefreshLine( m_current );
3977 else // unfocus
3979 // don't do anything if this item is not focused
3980 if ( item == m_current )
3982 ResetCurrent();
3984 if ( IsSingleSel() )
3986 // we must unselect the old current item as well or we
3987 // might end up with more than one selected item in a
3988 // single selection control
3989 HighlightLine(oldCurrent, false);
3992 RefreshLine( oldCurrent );
3997 // do we need to change the selection state?
3998 if ( stateMask & wxLIST_STATE_SELECTED )
4000 bool on = (state & wxLIST_STATE_SELECTED) != 0;
4002 if ( IsSingleSel() )
4004 if ( on )
4006 // selecting the item also makes it the focused one in the
4007 // single sel mode
4008 if ( m_current != item )
4010 ChangeCurrent(item);
4012 if ( oldCurrent != (size_t)-1 )
4014 HighlightLine( oldCurrent, false );
4015 RefreshLine( oldCurrent );
4019 else // off
4021 // only the current item may be selected anyhow
4022 if ( item != m_current )
4023 return;
4027 if ( HighlightLine(item, on) )
4029 RefreshLine(item);
4034 int wxListMainWindow::GetItemState( long item, long stateMask ) const
4036 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
4037 _T("invalid list ctrl item index in GetItemState()") );
4039 int ret = wxLIST_STATE_DONTCARE;
4041 if ( stateMask & wxLIST_STATE_FOCUSED )
4043 if ( (size_t)item == m_current )
4044 ret |= wxLIST_STATE_FOCUSED;
4047 if ( stateMask & wxLIST_STATE_SELECTED )
4049 if ( IsHighlighted(item) )
4050 ret |= wxLIST_STATE_SELECTED;
4053 return ret;
4056 void wxListMainWindow::GetItem( wxListItem &item ) const
4058 wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
4059 _T("invalid item index in GetItem") );
4061 wxListLineData *line = GetLine((size_t)item.m_itemId);
4062 line->GetItem( item.m_col, item );
4064 // Get item state if user wants it
4065 if ( item.m_mask & wxLIST_MASK_STATE )
4066 item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
4067 wxLIST_STATE_FOCUSED );
4070 // ----------------------------------------------------------------------------
4071 // item count
4072 // ----------------------------------------------------------------------------
4074 size_t wxListMainWindow::GetItemCount() const
4076 return IsVirtual() ? m_countVirt : m_lines.GetCount();
4079 void wxListMainWindow::SetItemCount(long count)
4081 m_selStore.SetItemCount(count);
4082 m_countVirt = count;
4084 ResetVisibleLinesRange();
4086 // scrollbars must be reset
4087 m_dirty = true;
4090 int wxListMainWindow::GetSelectedItemCount() const
4092 // deal with the quick case first
4093 if ( IsSingleSel() )
4094 return HasCurrent() ? IsHighlighted(m_current) : false;
4096 // virtual controls remmebers all its selections itself
4097 if ( IsVirtual() )
4098 return m_selStore.GetSelectedCount();
4100 // TODO: we probably should maintain the number of items selected even for
4101 // non virtual controls as enumerating all lines is really slow...
4102 size_t countSel = 0;
4103 size_t count = GetItemCount();
4104 for ( size_t line = 0; line < count; line++ )
4106 if ( GetLine(line)->IsHighlighted() )
4107 countSel++;
4110 return countSel;
4113 // ----------------------------------------------------------------------------
4114 // item position/size
4115 // ----------------------------------------------------------------------------
4117 wxRect wxListMainWindow::GetViewRect() const
4119 wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
4120 _T("wxListCtrl::GetViewRect() only works in icon mode") );
4122 // we need to find the longest/tallest label
4123 wxCoord xMax = 0, yMax = 0;
4124 const int count = GetItemCount();
4125 if ( count )
4127 for ( int i = 0; i < count; i++ )
4129 // we need logical, not physical, coordinates here, so use
4130 // GetLineRect() instead of GetItemRect()
4131 wxRect r = GetLineRect(i);
4133 wxCoord x = r.GetRight(),
4134 y = r.GetBottom();
4136 if ( x > xMax )
4137 xMax = x;
4138 if ( y > yMax )
4139 yMax = y;
4143 // some fudge needed to make it look prettier
4144 xMax += 2 * EXTRA_BORDER_X;
4145 yMax += 2 * EXTRA_BORDER_Y;
4147 // account for the scrollbars if necessary
4148 const wxSize sizeAll = GetClientSize();
4149 if ( xMax > sizeAll.x )
4150 yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
4151 if ( yMax > sizeAll.y )
4152 xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
4154 return wxRect(0, 0, xMax, yMax);
4157 void wxListMainWindow::GetItemRect( long index, wxRect &rect ) const
4159 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4160 _T("invalid index in GetItemRect") );
4162 // ensure that we're laid out, otherwise we could return nonsense
4163 if ( m_dirty )
4165 wxConstCast(this, wxListMainWindow)->
4166 RecalculatePositions(true /* no refresh */);
4169 rect = GetLineRect((size_t)index);
4171 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
4174 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
4176 wxRect rect;
4177 GetItemRect(item, rect);
4179 pos.x = rect.x;
4180 pos.y = rect.y;
4182 return true;
4185 // ----------------------------------------------------------------------------
4186 // geometry calculation
4187 // ----------------------------------------------------------------------------
4189 void wxListMainWindow::RecalculatePositions(bool noRefresh)
4191 const int lineHeight = GetLineHeight();
4193 wxClientDC dc( this );
4194 dc.SetFont( GetFont() );
4196 const size_t count = GetItemCount();
4198 int iconSpacing;
4199 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
4200 iconSpacing = m_normal_spacing;
4201 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
4202 iconSpacing = m_small_spacing;
4203 else
4204 iconSpacing = 0;
4206 // Note that we do not call GetClientSize() here but
4207 // GetSize() and subtract the border size for sunken
4208 // borders manually. This is technically incorrect,
4209 // but we need to know the client area's size WITHOUT
4210 // scrollbars here. Since we don't know if there are
4211 // any scrollbars, we use GetSize() instead. Another
4212 // solution would be to call SetScrollbars() here to
4213 // remove the scrollbars and call GetClientSize() then,
4214 // but this might result in flicker and - worse - will
4215 // reset the scrollbars to 0 which is not good at all
4216 // if you resize a dialog/window, but don't want to
4217 // reset the window scrolling. RR.
4218 // Furthermore, we actually do NOT subtract the border
4219 // width as 2 pixels is just the extra space which we
4220 // need around the actual content in the window. Other-
4221 // wise the text would e.g. touch the upper border. RR.
4222 int clientWidth,
4223 clientHeight;
4224 GetSize( &clientWidth, &clientHeight );
4226 if ( InReportView() )
4228 // all lines have the same height and we scroll one line per step
4229 int entireHeight = count * lineHeight + LINE_SPACING;
4231 m_linesPerPage = clientHeight / lineHeight;
4233 ResetVisibleLinesRange();
4235 SetScrollbars( SCROLL_UNIT_X, lineHeight,
4236 GetHeaderWidth() / SCROLL_UNIT_X,
4237 (entireHeight + lineHeight - 1) / lineHeight,
4238 GetScrollPos(wxHORIZONTAL),
4239 GetScrollPos(wxVERTICAL),
4240 true );
4242 else // !report
4244 // we have 3 different layout strategies: either layout all items
4245 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4246 // to arrange them in top to bottom, left to right (don't ask me why
4247 // not the other way round...) order
4248 if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) )
4250 int x = EXTRA_BORDER_X;
4251 int y = EXTRA_BORDER_Y;
4253 wxCoord widthMax = 0;
4255 size_t i;
4256 for ( i = 0; i < count; i++ )
4258 wxListLineData *line = GetLine(i);
4259 line->CalculateSize( &dc, iconSpacing );
4260 line->SetPosition( x, y, iconSpacing );
4262 wxSize sizeLine = GetLineSize(i);
4264 if ( HasFlag(wxLC_ALIGN_TOP) )
4266 if ( sizeLine.x > widthMax )
4267 widthMax = sizeLine.x;
4269 y += sizeLine.y;
4271 else // wxLC_ALIGN_LEFT
4273 x += sizeLine.x + MARGIN_BETWEEN_ROWS;
4277 if ( HasFlag(wxLC_ALIGN_TOP) )
4279 // traverse the items again and tweak their sizes so that they are
4280 // all the same in a row
4281 for ( i = 0; i < count; i++ )
4283 wxListLineData *line = GetLine(i);
4284 line->m_gi->ExtendWidth(widthMax);
4288 SetScrollbars
4290 SCROLL_UNIT_X,
4291 lineHeight,
4292 (x + SCROLL_UNIT_X) / SCROLL_UNIT_X,
4293 (y + lineHeight) / lineHeight,
4294 GetScrollPos( wxHORIZONTAL ),
4295 GetScrollPos( wxVERTICAL ),
4296 true
4299 else // "flowed" arrangement, the most complicated case
4301 // at first we try without any scrollbars, if the items don't fit into
4302 // the window, we recalculate after subtracting the space taken by the
4303 // scrollbar
4305 int entireWidth = 0;
4307 for (int tries = 0; tries < 2; tries++)
4309 entireWidth = 2 * EXTRA_BORDER_X;
4311 if (tries == 1)
4313 // Now we have decided that the items do not fit into the
4314 // client area, so we need a scrollbar
4315 entireWidth += SCROLL_UNIT_X;
4318 int x = EXTRA_BORDER_X;
4319 int y = EXTRA_BORDER_Y;
4320 int maxWidthInThisRow = 0;
4322 m_linesPerPage = 0;
4323 int currentlyVisibleLines = 0;
4325 for (size_t i = 0; i < count; i++)
4327 currentlyVisibleLines++;
4328 wxListLineData *line = GetLine( i );
4329 line->CalculateSize( &dc, iconSpacing );
4330 line->SetPosition( x, y, iconSpacing );
4332 wxSize sizeLine = GetLineSize( i );
4334 if ( maxWidthInThisRow < sizeLine.x )
4335 maxWidthInThisRow = sizeLine.x;
4337 y += sizeLine.y;
4338 if (currentlyVisibleLines > m_linesPerPage)
4339 m_linesPerPage = currentlyVisibleLines;
4341 if ( y + sizeLine.y >= clientHeight )
4343 currentlyVisibleLines = 0;
4344 y = EXTRA_BORDER_Y;
4345 maxWidthInThisRow += MARGIN_BETWEEN_ROWS;
4346 x += maxWidthInThisRow;
4347 entireWidth += maxWidthInThisRow;
4348 maxWidthInThisRow = 0;
4351 // We have reached the last item.
4352 if ( i == count - 1 )
4353 entireWidth += maxWidthInThisRow;
4355 if ( (tries == 0) &&
4356 (entireWidth + SCROLL_UNIT_X > clientWidth) )
4358 clientHeight -= wxSystemSettings::
4359 GetMetric(wxSYS_HSCROLL_Y);
4360 m_linesPerPage = 0;
4361 break;
4364 if ( i == count - 1 )
4365 tries = 1; // Everything fits, no second try required.
4369 SetScrollbars
4371 SCROLL_UNIT_X,
4372 lineHeight,
4373 (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X,
4375 GetScrollPos( wxHORIZONTAL ),
4377 true
4382 if ( !noRefresh )
4384 // FIXME: why should we call it from here?
4385 UpdateCurrent();
4387 RefreshAll();
4391 void wxListMainWindow::RefreshAll()
4393 m_dirty = false;
4394 Refresh();
4396 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
4397 if ( headerWin && headerWin->m_dirty )
4399 headerWin->m_dirty = false;
4400 headerWin->Refresh();
4404 void wxListMainWindow::UpdateCurrent()
4406 if ( !HasCurrent() && !IsEmpty() )
4407 ChangeCurrent(0);
4410 long wxListMainWindow::GetNextItem( long item,
4411 int WXUNUSED(geometry),
4412 int state ) const
4414 long ret = item,
4415 max = GetItemCount();
4416 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
4417 _T("invalid listctrl index in GetNextItem()") );
4419 // notice that we start with the next item (or the first one if item == -1)
4420 // and this is intentional to allow writing a simple loop to iterate over
4421 // all selected items
4422 ret++;
4423 if ( ret == max )
4424 // this is not an error because the index was OK initially,
4425 // just no such item
4426 return -1;
4428 if ( !state )
4429 // any will do
4430 return (size_t)ret;
4432 size_t count = GetItemCount();
4433 for ( size_t line = (size_t)ret; line < count; line++ )
4435 if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
4436 return line;
4438 if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
4439 return line;
4442 return -1;
4445 // ----------------------------------------------------------------------------
4446 // deleting stuff
4447 // ----------------------------------------------------------------------------
4449 void wxListMainWindow::DeleteItem( long lindex )
4451 size_t count = GetItemCount();
4453 wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
4454 _T("invalid item index in DeleteItem") );
4456 size_t index = (size_t)lindex;
4458 // we don't need to adjust the index for the previous items
4459 if ( HasCurrent() && m_current >= index )
4461 // if the current item is being deleted, we want the next one to
4462 // become selected - unless there is no next one - so don't adjust
4463 // m_current in this case
4464 if ( m_current != index || m_current == count - 1 )
4465 m_current--;
4468 if ( InReportView() )
4470 // mark the Column Max Width cache as dirty if the items in the line
4471 // we're deleting contain the Max Column Width
4472 wxListLineData * const line = GetLine(index);
4473 wxListItemDataList::compatibility_iterator n;
4474 wxListItemData *itemData;
4475 wxListItem item;
4476 int itemWidth;
4478 for (size_t i = 0; i < m_columns.GetCount(); i++)
4480 n = line->m_items.Item( i );
4481 itemData = n->GetData();
4482 itemData->GetItem(item);
4484 itemWidth = GetItemWidthWithImage(&item);
4486 if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth)
4487 m_aColWidths.Item(i)->bNeedsUpdate = true;
4490 ResetVisibleLinesRange();
4493 SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM, wxDefaultPosition );
4495 if ( IsVirtual() )
4497 m_countVirt--;
4498 m_selStore.OnItemDelete(index);
4500 else
4502 m_lines.RemoveAt( index );
4505 // we need to refresh the (vert) scrollbar as the number of items changed
4506 m_dirty = true;
4508 RefreshAfter(index);
4511 void wxListMainWindow::DeleteColumn( int col )
4513 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
4515 wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
4517 m_dirty = true;
4518 delete node->GetData();
4519 m_columns.Erase( node );
4521 if ( !IsVirtual() )
4523 // update all the items
4524 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4526 wxListLineData * const line = GetLine(i);
4527 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
4528 delete n->GetData();
4529 line->m_items.Erase(n);
4533 if ( InReportView() ) // we only cache max widths when in Report View
4535 delete m_aColWidths.Item(col);
4536 m_aColWidths.RemoveAt(col);
4539 // invalidate it as it has to be recalculated
4540 m_headerWidth = 0;
4543 void wxListMainWindow::DoDeleteAllItems()
4545 if ( IsEmpty() )
4546 // nothing to do - in particular, don't send the event
4547 return;
4549 ResetCurrent();
4551 // to make the deletion of all items faster, we don't send the
4552 // notifications for each item deletion in this case but only one event
4553 // for all of them: this is compatible with wxMSW and documented in
4554 // DeleteAllItems() description
4556 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
4557 event.SetEventObject( GetParent() );
4558 GetParent()->GetEventHandler()->ProcessEvent( event );
4560 if ( IsVirtual() )
4562 m_countVirt = 0;
4563 m_selStore.Clear();
4566 if ( InReportView() )
4568 ResetVisibleLinesRange();
4569 for (size_t i = 0; i < m_aColWidths.GetCount(); i++)
4571 m_aColWidths.Item(i)->bNeedsUpdate = true;
4575 m_lines.Clear();
4578 void wxListMainWindow::DeleteAllItems()
4580 DoDeleteAllItems();
4582 RecalculatePositions();
4585 void wxListMainWindow::DeleteEverything()
4587 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
4588 WX_CLEAR_ARRAY(m_aColWidths);
4590 DeleteAllItems();
4593 // ----------------------------------------------------------------------------
4594 // scanning for an item
4595 // ----------------------------------------------------------------------------
4597 void wxListMainWindow::EnsureVisible( long index )
4599 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4600 _T("invalid index in EnsureVisible") );
4602 // We have to call this here because the label in question might just have
4603 // been added and its position is not known yet
4604 if ( m_dirty )
4605 RecalculatePositions(true /* no refresh */);
4607 MoveToItem((size_t)index);
4610 long wxListMainWindow::FindItem(long start, const wxString& str, bool partial )
4612 if (str.empty())
4613 return wxNOT_FOUND;
4615 long pos = start;
4616 wxString str_upper = str.Upper();
4617 if (pos < 0)
4618 pos = 0;
4620 size_t count = GetItemCount();
4621 for ( size_t i = (size_t)pos; i < count; i++ )
4623 wxListLineData *line = GetLine(i);
4624 wxString line_upper = line->GetText(0).Upper();
4625 if (!partial)
4627 if (line_upper == str_upper )
4628 return i;
4630 else
4632 if (line_upper.find(str_upper) == 0)
4633 return i;
4637 return wxNOT_FOUND;
4640 long wxListMainWindow::FindItem(long start, wxUIntPtr data)
4642 long pos = start;
4643 if (pos < 0)
4644 pos = 0;
4646 size_t count = GetItemCount();
4647 for (size_t i = (size_t)pos; i < count; i++)
4649 wxListLineData *line = GetLine(i);
4650 wxListItem item;
4651 line->GetItem( 0, item );
4652 if (item.m_data == data)
4653 return i;
4656 return wxNOT_FOUND;
4659 long wxListMainWindow::FindItem( const wxPoint& pt )
4661 size_t topItem;
4662 GetVisibleLinesRange( &topItem, NULL );
4664 wxPoint p;
4665 GetItemPosition( GetItemCount() - 1, p );
4666 if ( p.y == 0 )
4667 return topItem;
4669 long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem );
4670 if ( id >= 0 && id < (long)GetItemCount() )
4671 return id;
4673 return wxNOT_FOUND;
4676 long wxListMainWindow::HitTest( int x, int y, int &flags ) const
4678 CalcUnscrolledPosition( x, y, &x, &y );
4680 size_t count = GetItemCount();
4682 if ( InReportView() )
4684 size_t current = y / GetLineHeight();
4685 if ( current < count )
4687 flags = HitTestLine(current, x, y);
4688 if ( flags )
4689 return current;
4692 else // !report
4694 // TODO: optimize it too! this is less simple than for report view but
4695 // enumerating all items is still not a way to do it!!
4696 for ( size_t current = 0; current < count; current++ )
4698 flags = HitTestLine(current, x, y);
4699 if ( flags )
4700 return current;
4704 return wxNOT_FOUND;
4707 // ----------------------------------------------------------------------------
4708 // adding stuff
4709 // ----------------------------------------------------------------------------
4711 void wxListMainWindow::InsertItem( wxListItem &item )
4713 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4715 int count = GetItemCount();
4716 wxCHECK_RET( item.m_itemId >= 0, _T("invalid item index") );
4718 if (item.m_itemId > count)
4719 item.m_itemId = count;
4721 size_t id = item.m_itemId;
4723 m_dirty = true;
4725 if ( InReportView() )
4727 ResetVisibleLinesRange();
4729 // calculate the width of the item and adjust the max column width
4730 wxColWidthInfo *pWidthInfo = m_aColWidths.Item(item.GetColumn());
4731 int width = GetItemWidthWithImage(&item);
4732 item.SetWidth(width);
4733 if (width > pWidthInfo->nMaxWidth)
4734 pWidthInfo->nMaxWidth = width;
4737 wxListLineData *line = new wxListLineData(this);
4739 line->SetItem( item.m_col, item );
4741 m_lines.Insert( line, id );
4743 m_dirty = true;
4745 // If an item is selected at or below the point of insertion, we need to
4746 // increment the member variables because the current row's index has gone
4747 // up by one
4748 if ( HasCurrent() && m_current >= id )
4749 m_current++;
4751 SendNotify(id, wxEVT_COMMAND_LIST_INSERT_ITEM);
4753 RefreshLines(id, GetItemCount() - 1);
4756 void wxListMainWindow::InsertColumn( long col, wxListItem &item )
4758 m_dirty = true;
4759 if ( InReportView() )
4761 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
4762 item.m_width = GetTextLength( item.m_text );
4764 wxListHeaderData *column = new wxListHeaderData( item );
4765 wxColWidthInfo *colWidthInfo = new wxColWidthInfo();
4767 bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4768 if ( insert )
4770 wxListHeaderDataList::compatibility_iterator
4771 node = m_columns.Item( col );
4772 m_columns.Insert( node, column );
4773 m_aColWidths.Insert( colWidthInfo, col );
4775 else
4777 m_columns.Append( column );
4778 m_aColWidths.Add( colWidthInfo );
4781 if ( !IsVirtual() )
4783 // update all the items
4784 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4786 wxListLineData * const line = GetLine(i);
4787 wxListItemData * const data = new wxListItemData(this);
4788 if ( insert )
4789 line->m_items.Insert(col, data);
4790 else
4791 line->m_items.Append(data);
4795 // invalidate it as it has to be recalculated
4796 m_headerWidth = 0;
4800 int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
4802 int width = 0;
4803 wxClientDC dc(this);
4805 dc.SetFont( GetFont() );
4807 if (item->GetImage() != -1)
4809 int ix, iy;
4810 GetImageSize( item->GetImage(), ix, iy );
4811 width += ix + 5;
4814 if (!item->GetText().empty())
4816 wxCoord w;
4817 dc.GetTextExtent( item->GetText(), &w, NULL );
4818 width += w;
4821 return width;
4824 // ----------------------------------------------------------------------------
4825 // sorting
4826 // ----------------------------------------------------------------------------
4828 MuleListCtrlCompare list_ctrl_compare_func_2;
4829 long list_ctrl_compare_data;
4831 static int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
4833 wxListLineData *line1 = *arg1;
4834 wxListLineData *line2 = *arg2;
4835 wxListItem item;
4836 line1->GetItem( 0, item );
4837 wxUIntPtr data1 = item.m_data;
4838 line2->GetItem( 0, item );
4839 wxUIntPtr data2 = item.m_data;
4840 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
4843 void wxListMainWindow::SortItems( MuleListCtrlCompare fn, long data )
4845 // selections won't make sense any more after sorting the items so reset
4846 // them
4847 HighlightAll(false);
4848 ResetCurrent();
4850 list_ctrl_compare_func_2 = fn;
4851 list_ctrl_compare_data = data;
4852 m_lines.Sort( list_ctrl_compare_func_1 );
4853 m_dirty = true;
4856 // ----------------------------------------------------------------------------
4857 // scrolling
4858 // ----------------------------------------------------------------------------
4860 void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4862 // FIXME
4863 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4864 wxScrolledWindow::OnScroll(event);
4865 #else
4866 HandleOnScroll( event );
4867 #endif
4869 // update our idea of which lines are shown when we redraw the window the
4870 // next time
4871 ResetVisibleLinesRange();
4873 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4875 wxGenericListCtrl* lc = GetListCtrl();
4876 wxCHECK_RET( lc, _T("no listctrl window?") );
4878 lc->m_headerWin->Refresh();
4879 lc->m_headerWin->Update();
4883 int wxListMainWindow::GetCountPerPage() const
4885 if ( !m_linesPerPage )
4887 wxConstCast(this, wxListMainWindow)->
4888 m_linesPerPage = GetClientSize().y / GetLineHeight();
4891 return m_linesPerPage;
4894 void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
4896 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4898 if ( m_lineFrom == (size_t)-1 )
4900 size_t count = GetItemCount();
4901 if ( count )
4903 m_lineFrom = GetScrollPos(wxVERTICAL);
4905 // this may happen if SetScrollbars() hadn't been called yet
4906 if ( m_lineFrom >= count )
4907 m_lineFrom = count - 1;
4909 // we redraw one extra line but this is needed to make the redrawing
4910 // logic work when there is a fractional number of lines on screen
4911 m_lineTo = m_lineFrom + m_linesPerPage;
4912 if ( m_lineTo >= count )
4913 m_lineTo = count - 1;
4915 else // empty control
4917 m_lineFrom = 0;
4918 m_lineTo = (size_t)-1;
4922 wxASSERT_MSG( IsEmpty() ||
4923 (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
4924 _T("GetVisibleLinesRange() returns incorrect result") );
4926 if ( from )
4927 *from = m_lineFrom;
4928 if ( to )
4929 *to = m_lineTo;
4932 // -------------------------------------------------------------------------------------
4933 // wxGenericListCtrl
4934 // -------------------------------------------------------------------------------------
4936 BEGIN_EVENT_TABLE(wxGenericListCtrl,wxControl)
4937 EVT_SIZE(wxGenericListCtrl::OnSize)
4938 END_EVENT_TABLE()
4940 wxGenericListCtrl::wxGenericListCtrl()
4942 m_imageListNormal = (wxImageList *) NULL;
4943 m_imageListSmall = (wxImageList *) NULL;
4944 m_imageListState = (wxImageList *) NULL;
4946 m_ownsImageListNormal =
4947 m_ownsImageListSmall =
4948 m_ownsImageListState = false;
4950 m_mainWin = (wxListMainWindow*) NULL;
4951 m_headerWin = (wxListHeaderWindow*) NULL;
4952 m_headerHeight = 0;
4955 wxGenericListCtrl::~wxGenericListCtrl()
4957 if (m_ownsImageListNormal)
4958 delete m_imageListNormal;
4959 if (m_ownsImageListSmall)
4960 delete m_imageListSmall;
4961 if (m_ownsImageListState)
4962 delete m_imageListState;
4965 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4967 if ( m_headerWin )
4969 #if defined( __WXMAC__ ) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
4970 SInt32 h;
4971 GetThemeMetric( kThemeMetricListHeaderHeight, &h );
4972 #else
4973 // we use 'g' to get the descent, too
4974 int w, h, d;
4975 m_headerWin->GetTextExtent(wxT("Hg"), &w, &h, &d);
4976 h += d + 2 * HEADER_OFFSET_Y + EXTRA_HEIGHT;
4977 #endif
4979 // only update if changed
4980 if ( h != m_headerHeight )
4982 m_headerHeight = h;
4984 if ( HasHeader() )
4985 ResizeReportView(true);
4986 else //why is this needed if it doesn't have a header?
4987 m_headerWin->SetSize(m_headerWin->GetSize().x, m_headerHeight);
4992 void wxGenericListCtrl::CreateHeaderWindow()
4994 m_headerWin = new wxListHeaderWindow
4996 this, wxID_ANY, m_mainWin,
4997 wxPoint(0,0),
4998 wxSize(GetClientSize().x, m_headerHeight),
4999 wxTAB_TRAVERSAL
5001 CalculateAndSetHeaderHeight();
5004 bool wxGenericListCtrl::GetFocus()
5006 return m_mainWin->m_hasFocus;
5009 bool wxGenericListCtrl::Create(wxWindow *parent,
5010 wxWindowID id,
5011 const wxPoint &pos,
5012 const wxSize &size,
5013 long style,
5014 const wxValidator &validator,
5015 const wxString &name)
5017 m_imageListNormal =
5018 m_imageListSmall =
5019 m_imageListState = (wxImageList *) NULL;
5020 m_ownsImageListNormal =
5021 m_ownsImageListSmall =
5022 m_ownsImageListState = false;
5024 m_mainWin = (wxListMainWindow*) NULL;
5025 m_headerWin = (wxListHeaderWindow*) NULL;
5027 m_headerHeight = 0;
5029 if ( !(style & wxLC_MASK_TYPE) )
5031 style = style | wxLC_LIST;
5034 // add more styles here that should only appear
5035 // in the main window
5036 unsigned long only_main_window_style = wxALWAYS_SHOW_SB;
5038 if ( !wxControl::Create( parent, id, pos, size, style & ~only_main_window_style, validator, name ) )
5039 return false;
5041 // don't create the inner window with the border
5042 style &= ~wxBORDER_MASK;
5044 m_mainWin = new wxListMainWindow( this, wxID_ANY, wxPoint(0, 0), size, style );
5046 #ifdef __WXMAC_CARBON__
5047 // Human Interface Guidelines ask us for a special font in this case
5048 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL )
5050 wxFont font;
5051 font.MacCreateThemeFont( kThemeViewsFont );
5052 SetFont( font );
5054 #endif
5056 if ( InReportView() )
5058 CreateHeaderWindow();
5060 #ifdef __WXMAC_CARBON__
5061 if (m_headerWin)
5063 wxFont font;
5064 font.MacCreateThemeFont( kThemeSmallSystemFont );
5065 m_headerWin->SetFont( font );
5066 CalculateAndSetHeaderHeight();
5068 #endif
5070 if ( HasFlag(wxLC_NO_HEADER) )
5071 // VZ: why do we create it at all then?
5072 m_headerWin->Show( false );
5075 SetInitialSize(size);
5077 return true;
5080 void wxGenericListCtrl::SetSingleStyle( long style, bool add )
5082 wxASSERT_MSG( !(style & wxLC_VIRTUAL),
5083 _T("wxLC_VIRTUAL can't be [un]set") );
5085 long flag = GetWindowStyle();
5087 if (add)
5089 if (style & wxLC_MASK_TYPE)
5090 flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
5091 if (style & wxLC_MASK_ALIGN)
5092 flag &= ~wxLC_MASK_ALIGN;
5093 if (style & wxLC_MASK_SORT)
5094 flag &= ~wxLC_MASK_SORT;
5097 if (add)
5098 flag |= style;
5099 else
5100 flag &= ~style;
5102 // some styles can be set without recreating everything (as happens in
5103 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
5104 if ( !(style & ~(wxLC_HRULES | wxLC_VRULES)) )
5106 Refresh();
5107 wxWindow::SetWindowStyleFlag(flag);
5109 else
5111 SetWindowStyleFlag( flag );
5115 void wxGenericListCtrl::SetWindowStyleFlag( long flag )
5117 if (m_mainWin)
5119 m_mainWin->DeleteEverything();
5121 // has the header visibility changed?
5122 bool hasHeader = HasHeader();
5123 bool willHaveHeader = (flag & wxLC_REPORT) && !(flag & wxLC_NO_HEADER);
5125 if ( hasHeader != willHaveHeader )
5127 // toggle it
5128 if ( hasHeader )
5130 if ( m_headerWin )
5132 // don't delete, just hide, as we can reuse it later
5133 m_headerWin->Show(false);
5135 //else: nothing to do
5137 else // must show header
5139 if (!m_headerWin)
5141 CreateHeaderWindow();
5143 else // already have it, just show
5145 m_headerWin->Show( true );
5149 ResizeReportView(willHaveHeader);
5153 wxWindow::SetWindowStyleFlag( flag );
5156 bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
5158 m_mainWin->GetColumn( col, item );
5159 return true;
5162 bool wxGenericListCtrl::SetColumn( int col, wxListItem& item )
5164 m_mainWin->SetColumn( col, item );
5165 return true;
5168 int wxGenericListCtrl::GetColumnWidth( int col ) const
5170 return m_mainWin->GetColumnWidth( col );
5173 bool wxGenericListCtrl::SetColumnWidth( int col, int width )
5175 m_mainWin->SetColumnWidth( col, width );
5176 return true;
5179 int wxGenericListCtrl::GetCountPerPage() const
5181 return m_mainWin->GetCountPerPage(); // different from Windows ?
5184 bool wxGenericListCtrl::GetItem( wxListItem &info ) const
5186 m_mainWin->GetItem( info );
5187 return true;
5190 bool wxGenericListCtrl::SetItem( wxListItem &info )
5192 m_mainWin->SetItem( info );
5193 return true;
5196 long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
5198 wxListItem info;
5199 info.m_text = label;
5200 info.m_mask = wxLIST_MASK_TEXT;
5201 info.m_itemId = index;
5202 info.m_col = col;
5203 if ( imageId > -1 )
5205 info.m_image = imageId;
5206 info.m_mask |= wxLIST_MASK_IMAGE;
5209 m_mainWin->SetItem(info);
5210 return true;
5213 int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
5215 return m_mainWin->GetItemState( item, stateMask );
5218 bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
5220 m_mainWin->SetItemState( item, state, stateMask );
5221 return true;
5224 bool
5225 wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
5227 return SetItemColumnImage(item, 0, image);
5230 bool
5231 wxGenericListCtrl::SetItemColumnImage( long item, long column, int image )
5233 wxListItem info;
5234 info.m_image = image;
5235 info.m_mask = wxLIST_MASK_IMAGE;
5236 info.m_itemId = item;
5237 info.m_col = column;
5238 m_mainWin->SetItem( info );
5239 return true;
5242 wxString wxGenericListCtrl::GetItemText( long item ) const
5244 return m_mainWin->GetItemText(item);
5247 void wxGenericListCtrl::SetItemText( long item, const wxString& str )
5249 m_mainWin->SetItemText(item, str);
5252 wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const
5254 wxListItem info;
5255 info.m_mask = wxLIST_MASK_DATA;
5256 info.m_itemId = item;
5257 m_mainWin->GetItem( info );
5258 return info.m_data;
5261 bool wxGenericListCtrl::SetItemPtrData( long item, wxUIntPtr data )
5263 wxListItem info;
5264 info.m_mask = wxLIST_MASK_DATA;
5265 info.m_itemId = item;
5266 info.m_data = data;
5267 m_mainWin->SetItem( info );
5268 return true;
5271 #if 0
5272 bool wxGenericListCtrl::SetItemData(long item, long data)
5274 return SetItemPtrData(item, data);
5276 #endif
5278 wxRect wxGenericListCtrl::GetViewRect() const
5280 return m_mainWin->GetViewRect();
5283 bool wxGenericListCtrl::GetItemRect( long item, wxRect &rect, int WXUNUSED(code) ) const
5285 m_mainWin->GetItemRect( item, rect );
5286 if ( m_mainWin->HasHeader() )
5287 rect.y += m_headerHeight + 1;
5288 return true;
5291 bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
5293 m_mainWin->GetItemPosition( item, pos );
5294 return true;
5297 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
5299 return 0;
5302 int wxGenericListCtrl::GetItemCount() const
5304 return m_mainWin->GetItemCount();
5307 int wxGenericListCtrl::GetColumnCount() const
5309 return m_mainWin->GetColumnCount();
5312 void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
5314 m_mainWin->SetItemSpacing( spacing, isSmall );
5317 wxSize wxGenericListCtrl::GetItemSpacing() const
5319 const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON));
5321 return wxSize(spacing, spacing);
5324 void wxGenericListCtrl::OnDrawItem(int WXUNUSED(item), wxDC* WXUNUSED(dc), const wxRect& WXUNUSED(rect), const wxRect& WXUNUSED(rectHL), bool WXUNUSED(highlighted))
5326 // do nothing here, this is just a stub
5329 void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
5331 wxListItem info;
5332 info.m_itemId = item;
5333 info.SetTextColour( col );
5334 m_mainWin->SetItem( info );
5337 wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
5339 wxListItem info;
5340 info.m_itemId = item;
5341 m_mainWin->GetItem( info );
5342 return info.GetTextColour();
5345 void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
5347 wxListItem info;
5348 info.m_itemId = item;
5349 info.SetBackgroundColour( col );
5350 m_mainWin->SetItem( info );
5353 wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
5355 wxListItem info;
5356 info.m_itemId = item;
5357 m_mainWin->GetItem( info );
5358 return info.GetBackgroundColour();
5361 int wxGenericListCtrl::GetScrollPos( int orient ) const
5363 return m_mainWin->GetScrollPos( orient );
5366 void wxGenericListCtrl::SetScrollPos( int orient, int pos, bool refresh )
5368 m_mainWin->SetScrollPos( orient, pos, refresh );
5371 void wxGenericListCtrl::SetItemFont( long item, const wxFont &f )
5373 wxListItem info;
5374 info.m_itemId = item;
5375 info.SetFont( f );
5376 m_mainWin->SetItem( info );
5379 wxFont wxGenericListCtrl::GetItemFont( long item ) const
5381 wxListItem info;
5382 info.m_itemId = item;
5383 m_mainWin->GetItem( info );
5384 return info.GetFont();
5387 int wxGenericListCtrl::GetSelectedItemCount() const
5389 return m_mainWin->GetSelectedItemCount();
5392 wxColour wxGenericListCtrl::GetTextColour() const
5394 return GetForegroundColour();
5397 void wxGenericListCtrl::SetTextColour(const wxColour& col)
5399 SetForegroundColour(col);
5402 long wxGenericListCtrl::GetTopItem() const
5404 size_t top;
5405 m_mainWin->GetVisibleLinesRange(&top, NULL);
5406 return (long)top;
5409 void wxGenericListCtrl::GetVisibleLines(long* first, long* last)
5411 size_t from, to;
5413 m_mainWin->GetVisibleLinesRange(&from, &to);
5415 if (first) *first = (long)from;
5416 if (last) *last = (long)to;
5419 long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
5421 return m_mainWin->GetNextItem( item, geom, state );
5424 wxImageList *wxGenericListCtrl::GetImageList(int which) const
5426 if (which == wxIMAGE_LIST_NORMAL)
5427 return m_imageListNormal;
5428 else if (which == wxIMAGE_LIST_SMALL)
5429 return m_imageListSmall;
5430 else if (which == wxIMAGE_LIST_STATE)
5431 return m_imageListState;
5433 return (wxImageList *) NULL;
5436 void wxGenericListCtrl::SetImageList( wxImageList *imageList, int which )
5438 if ( which == wxIMAGE_LIST_NORMAL )
5440 if (m_ownsImageListNormal)
5441 delete m_imageListNormal;
5442 m_imageListNormal = imageList;
5443 m_ownsImageListNormal = false;
5445 else if ( which == wxIMAGE_LIST_SMALL )
5447 if (m_ownsImageListSmall)
5448 delete m_imageListSmall;
5449 m_imageListSmall = imageList;
5450 m_ownsImageListSmall = false;
5452 else if ( which == wxIMAGE_LIST_STATE )
5454 if (m_ownsImageListState)
5455 delete m_imageListState;
5456 m_imageListState = imageList;
5457 m_ownsImageListState = false;
5460 m_mainWin->SetImageList( imageList, which );
5463 void wxGenericListCtrl::AssignImageList(wxImageList *imageList, int which)
5465 SetImageList(imageList, which);
5466 if ( which == wxIMAGE_LIST_NORMAL )
5467 m_ownsImageListNormal = true;
5468 else if ( which == wxIMAGE_LIST_SMALL )
5469 m_ownsImageListSmall = true;
5470 else if ( which == wxIMAGE_LIST_STATE )
5471 m_ownsImageListState = true;
5474 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
5476 return 0;
5479 bool wxGenericListCtrl::DeleteItem( long item )
5481 m_mainWin->DeleteItem( item );
5482 return true;
5485 bool wxGenericListCtrl::DeleteAllItems()
5487 m_mainWin->DeleteAllItems();
5488 return true;
5491 bool wxGenericListCtrl::DeleteAllColumns()
5493 size_t count = m_mainWin->m_columns.GetCount();
5494 for ( size_t n = 0; n < count; n++ )
5495 DeleteColumn( 0 );
5496 return true;
5499 void wxGenericListCtrl::ClearAll()
5501 m_mainWin->DeleteEverything();
5504 bool wxGenericListCtrl::DeleteColumn( int col )
5506 m_mainWin->DeleteColumn( col );
5508 // if we don't have the header any longer, we need to relayout the window
5509 if ( !GetColumnCount() )
5510 ResizeReportView(false /* no header */);
5511 return true;
5514 wxTextCtrl *wxGenericListCtrl::EditLabel(long item,
5515 wxClassInfo* textControlClass)
5517 return m_mainWin->EditLabel( item, textControlClass );
5520 wxTextCtrl *wxGenericListCtrl::GetEditControl() const
5522 return m_mainWin->GetEditControl();
5525 bool wxGenericListCtrl::EnsureVisible( long item )
5527 m_mainWin->EnsureVisible( item );
5528 return true;
5531 long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
5533 return m_mainWin->FindItem( start, str, partial );
5536 long wxGenericListCtrl::FindItem( long start, wxUIntPtr data )
5538 return m_mainWin->FindItem( start, data );
5541 long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt,
5542 int WXUNUSED(direction))
5544 return m_mainWin->FindItem( pt );
5547 // TODO: sub item hit testing
5548 long wxGenericListCtrl::HitTest(const wxPoint& point, int& flags, long *) const
5550 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
5553 long wxGenericListCtrl::InsertItem( wxListItem& info )
5555 m_mainWin->InsertItem( info );
5556 return info.m_itemId;
5559 long wxGenericListCtrl::InsertItem( long index, const wxString &label )
5561 wxListItem info;
5562 info.m_text = label;
5563 info.m_mask = wxLIST_MASK_TEXT;
5564 info.m_itemId = index;
5565 return InsertItem( info );
5568 long wxGenericListCtrl::InsertItem( long index, int imageIndex )
5570 wxListItem info;
5571 info.m_mask = wxLIST_MASK_IMAGE;
5572 info.m_image = imageIndex;
5573 info.m_itemId = index;
5574 return InsertItem( info );
5577 long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
5579 wxListItem info;
5580 info.m_text = label;
5581 info.m_image = imageIndex;
5582 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
5583 info.m_itemId = index;
5584 return InsertItem( info );
5587 long wxGenericListCtrl::InsertColumn( long col, wxListItem &item )
5589 wxCHECK_MSG( m_headerWin, -1, _T("can't add column in non report mode") );
5591 m_mainWin->InsertColumn( col, item );
5593 // if we hadn't had a header before but have one now
5594 // then we need to relayout the window
5595 if ( GetColumnCount() == 1 && m_mainWin->HasHeader() )
5596 ResizeReportView(true /* have header */);
5598 m_headerWin->Refresh();
5600 return 0;
5603 long wxGenericListCtrl::InsertColumn( long col, const wxString &heading,
5604 int format, int width )
5606 wxListItem item;
5607 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
5608 item.m_text = heading;
5609 if (width >= -2)
5611 item.m_mask |= wxLIST_MASK_WIDTH;
5612 item.m_width = width;
5615 item.m_format = format;
5617 return InsertColumn( col, item );
5620 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx), int WXUNUSED(dy) )
5622 return 0;
5625 // Sort items.
5626 // fn is a function which takes 3 long arguments: item1, item2, data.
5627 // item1 is the long data associated with a first item (NOT the index).
5628 // item2 is the long data associated with a second item (NOT the index).
5629 // data is the same value as passed to SortItems.
5630 // The return value is a negative number if the first item should precede the second
5631 // item, a positive number of the second item should precede the first,
5632 // or zero if the two items are equivalent.
5633 // data is arbitrary data to be passed to the sort function.
5635 bool wxGenericListCtrl::SortItems( MuleListCtrlCompare fn, long data )
5637 m_mainWin->SortItems( fn, data );
5638 return true;
5641 // ----------------------------------------------------------------------------
5642 // event handlers
5643 // ----------------------------------------------------------------------------
5645 void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
5647 if ( !m_mainWin )
5648 return;
5650 ResizeReportView(m_mainWin->HasHeader());
5651 m_mainWin->RecalculatePositions();
5654 void wxGenericListCtrl::ResizeReportView(bool showHeader)
5656 int cw, ch;
5657 GetClientSize( &cw, &ch );
5659 if ( showHeader )
5661 m_headerWin->SetSize( 0, 0, cw, m_headerHeight );
5662 if(ch > m_headerHeight)
5663 m_mainWin->SetSize( 0, m_headerHeight + 1,
5664 cw, ch - m_headerHeight - 1 );
5665 else
5666 m_mainWin->SetSize( 0, m_headerHeight + 1,
5667 cw, 0);
5669 else // no header window
5671 m_mainWin->SetSize( 0, 0, cw, ch );
5675 void wxGenericListCtrl::OnInternalIdle()
5677 wxWindow::OnInternalIdle();
5679 // do it only if needed
5680 if ( !m_mainWin->m_dirty )
5681 return;
5683 m_mainWin->RecalculatePositions();
5686 // ----------------------------------------------------------------------------
5687 // font/colours
5688 // ----------------------------------------------------------------------------
5690 bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
5692 if (m_mainWin)
5694 m_mainWin->SetBackgroundColour( colour );
5695 m_mainWin->m_dirty = true;
5698 return true;
5701 bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
5703 if ( !wxWindow::SetForegroundColour( colour ) )
5704 return false;
5706 if (m_mainWin)
5708 m_mainWin->SetForegroundColour( colour );
5709 m_mainWin->m_dirty = true;
5712 if (m_headerWin)
5713 m_headerWin->SetForegroundColour( colour );
5715 return true;
5718 bool wxGenericListCtrl::SetFont( const wxFont &font )
5720 if ( !wxWindow::SetFont( font ) )
5721 return false;
5723 if (m_mainWin)
5725 m_mainWin->SetFont( font );
5726 m_mainWin->m_dirty = true;
5729 if (m_headerWin)
5731 m_headerWin->SetFont( font );
5732 CalculateAndSetHeaderHeight();
5735 Refresh();
5737 return true;
5740 // static
5741 wxVisualAttributes
5742 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
5744 #if _USE_VISATTR
5745 // Use the same color scheme as wxListBox
5746 return wxListBox::GetClassDefaultAttributes(variant);
5747 #else
5748 wxUnusedVar(variant);
5749 wxVisualAttributes attr;
5750 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
5751 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
5752 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
5753 return attr;
5754 #endif
5757 // ----------------------------------------------------------------------------
5758 // methods forwarded to m_mainWin
5759 // ----------------------------------------------------------------------------
5761 #if wxUSE_DRAG_AND_DROP
5763 void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
5765 m_mainWin->SetDropTarget( dropTarget );
5768 wxDropTarget *wxGenericListCtrl::GetDropTarget() const
5770 return m_mainWin->GetDropTarget();
5773 #endif
5775 bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
5777 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false;
5780 wxColour wxGenericListCtrl::GetBackgroundColour() const
5782 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5785 wxColour wxGenericListCtrl::GetForegroundColour() const
5787 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5790 bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
5792 #if wxUSE_MENUS
5793 return m_mainWin->PopupMenu( menu, x, y );
5794 #else
5795 return false;
5796 #endif
5799 void wxGenericListCtrl::SetFocus()
5801 // The test in window.cpp fails as we are a composite
5802 // window, so it checks against "this", but not m_mainWin.
5803 if ( DoFindFocus() != this )
5804 m_mainWin->SetFocus();
5807 wxSize wxGenericListCtrl::DoGetBestSize() const
5809 // Something is better than nothing...
5810 // 100x80 is what the MSW version will get from the default
5811 // wxControl::DoGetBestSize
5812 return wxSize(100, 80);
5815 // ----------------------------------------------------------------------------
5816 // virtual list control support
5817 // ----------------------------------------------------------------------------
5819 wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
5821 // this is a pure virtual function, in fact - which is not really pure
5822 // because the controls which are not virtual don't need to implement it
5823 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5825 return wxEmptyString;
5828 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
5830 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
5832 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5833 return -1;
5836 int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const
5838 if (!column)
5839 return OnGetItemImage(item);
5841 return -1;
5844 wxListItemAttr *
5845 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
5847 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
5848 _T("invalid item index in OnGetItemAttr()") );
5850 // no attributes by default
5851 return NULL;
5854 void wxGenericListCtrl::SetItemCount(long count)
5856 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5858 m_mainWin->SetItemCount(count);
5861 void wxGenericListCtrl::RefreshItem(long item)
5863 m_mainWin->RefreshLine(item);
5866 void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
5868 m_mainWin->RefreshLines(itemFrom, itemTo);
5871 // Generic wxListCtrl is more or less a container for two other
5872 // windows which drawings are done upon. These are namely
5873 // 'm_headerWin' and 'm_mainWin'.
5874 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5875 // behaviour wxListCtrl has under wxMSW.
5877 void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5879 if (!rect)
5881 // The easy case, no rectangle specified.
5882 if (m_headerWin)
5883 m_headerWin->Refresh(eraseBackground);
5885 if (m_mainWin)
5886 m_mainWin->Refresh(eraseBackground);
5888 else
5890 // Refresh the header window
5891 if (m_headerWin)
5893 wxRect rectHeader = m_headerWin->GetRect();
5894 rectHeader.Intersect(*rect);
5895 if (rectHeader.GetWidth() && rectHeader.GetHeight())
5897 int x, y;
5898 m_headerWin->GetPosition(&x, &y);
5899 rectHeader.Offset(-x, -y);
5900 m_headerWin->Refresh(eraseBackground, &rectHeader);
5904 // Refresh the main window
5905 if (m_mainWin)
5907 wxRect rectMain = m_mainWin->GetRect();
5908 rectMain.Intersect(*rect);
5909 if (rectMain.GetWidth() && rectMain.GetHeight())
5911 int x, y;
5912 m_mainWin->GetPosition(&x, &y);
5913 rectMain.Offset(-x, -y);
5914 m_mainWin->Refresh(eraseBackground, &rectMain);
5920 void wxGenericListCtrl::Freeze()
5922 m_mainWin->Freeze();
5925 void wxGenericListCtrl::Thaw()
5927 m_mainWin->Thaw();
5930 } // namespace MuleExtern
5932 #endif // wxUSE_LISTCTRL