1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/generic/listctrl.cpp
3 // Purpose:     generic implementation of wxListCtrl
4 // Author:      Robert Roebling
5 //              Vadim Zeitlin (virtual list control support)
6 // Id:          $Id: listctrl.cpp 67017 2011-02-25 09:37:28Z JS $
7 // Copyright:   (c) 1998 Robert Roebling
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 // TODO
12 //
13 //   1. we need to implement searching/sorting for virtual controls somehow
14 // 2. when changing selection the lines are refreshed twice
15 
16 
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
19 
20 #ifdef __BORLANDC__
21     #pragma hdrstop
22 #endif
23 
24 #if wxUSE_LISTCTRL
25 
26 #include "wx/listctrl.h"
27 
28 #if (!defined(__WXMSW__) || defined(__WXUNIVERSAL__)) && (!defined(__WXMAC__)|| defined(__WXUNIVERSAL__))
29     // if we have a native version, its implementation file does all this
30     IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
31     IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl)
32     IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
33 
34     IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxGenericListCtrl)
35 #endif
36 
37 #ifndef WX_PRECOMP
38     #include "wx/scrolwin.h"
39     #include "wx/timer.h"
40     #include "wx/settings.h"
41     #include "wx/dynarray.h"
42     #include "wx/dcclient.h"
43     #include "wx/dcscreen.h"
44     #include "wx/math.h"
45 #endif
46 
47 #include "wx/imaglist.h"
48 #include "wx/selstore.h"
49 #include "wx/renderer.h"
50 
51 #ifdef __WXMAC__
52     #include "wx/mac/private.h"
53 #endif
54 
55 
56 // NOTE: If using the wxListBox visual attributes works everywhere then this can
57 // be removed, as well as the #else case below.
58 #define _USE_VISATTR 0
59 
60 
61 // ----------------------------------------------------------------------------
62 // constants
63 // ----------------------------------------------------------------------------
64 
65 // // the height of the header window (FIXME: should depend on its font!)
66 // static const int HEADER_HEIGHT = 23;
67 
68 static const int SCROLL_UNIT_X = 15;
69 
70 // the spacing between the lines (in report mode)
71 static const int LINE_SPACING = 0;
72 
73 // extra margins around the text label
74 #ifdef __WXGTK__
75 static const int EXTRA_WIDTH = 6;
76 #else
77 static const int EXTRA_WIDTH = 4;
78 #endif
79 static const int EXTRA_HEIGHT = 4;
80 
81 // margin between the window and the items
82 static const int EXTRA_BORDER_X = 2;
83 static const int EXTRA_BORDER_Y = 2;
84 
85 // offset for the header window
86 static const int HEADER_OFFSET_X = 0;
87 static const int HEADER_OFFSET_Y = 0;
88 
89 // margin between rows of icons in [small] icon view
90 static const int MARGIN_BETWEEN_ROWS = 6;
91 
92 // when autosizing the columns, add some slack
93 static const int AUTOSIZE_COL_MARGIN = 10;
94 
95 // default width for the header columns
96 static const int WIDTH_COL_DEFAULT = 80;
97 
98 // the space between the image and the text in the report mode
99 static const int IMAGE_MARGIN_IN_REPORT_MODE = 5;
100 
101 // the space between the image and the text in the report mode in header
102 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE = 2;
103 
104 // ============================================================================
105 // private classes
106 // ============================================================================
107 
108 //-----------------------------------------------------------------------------
109 //  wxColWidthInfo (internal)
110 //-----------------------------------------------------------------------------
111 
112 struct wxColWidthInfo
113 {
114     int     nMaxWidth;
115     bool    bNeedsUpdate;   //  only set to true when an item whose
116                             //  width == nMaxWidth is removed
117 
wxColWidthInfowxColWidthInfo118     wxColWidthInfo(int w = 0, bool needsUpdate = false)
119     {
120         nMaxWidth = w;
121         bNeedsUpdate = needsUpdate;
122     }
123 };
124 
125 WX_DEFINE_ARRAY_PTR(wxColWidthInfo *, ColWidthArray);
126 
127 //-----------------------------------------------------------------------------
128 //  wxListItemData (internal)
129 //-----------------------------------------------------------------------------
130 
131 class wxListItemData
132 {
133 public:
134     wxListItemData(wxListMainWindow *owner);
135     ~wxListItemData();
136 
137     void SetItem( const wxListItem &info );
SetImage(int image)138     void SetImage( int image ) { m_image = image; }
SetData(wxUIntPtr data)139     void SetData( wxUIntPtr data ) { m_data = data; }
140     void SetPosition( int x, int y );
141     void SetSize( int width, int height );
142 
HasText() const143     bool HasText() const { return !m_text.empty(); }
GetText() const144     const wxString& GetText() const { return m_text; }
SetText(const wxString & text)145     void SetText(const wxString& text) { m_text = text; }
146 
147     // we can't use empty string for measuring the string width/height, so
148     // always return something
GetTextForMeasuring() const149     wxString GetTextForMeasuring() const
150     {
151         wxString s = GetText();
152         if ( s.empty() )
153             s = _T('H');
154 
155         return s;
156     }
157 
158     bool IsHit( int x, int y ) const;
159 
160     int GetX() const;
161     int GetY() const;
162     int GetWidth() const;
163     int GetHeight() const;
164 
GetImage() const165     int GetImage() const { return m_image; }
HasImage() const166     bool HasImage() const { return GetImage() != -1; }
167 
168     void GetItem( wxListItem &info ) const;
169 
SetAttr(wxListItemAttr * attr)170     void SetAttr(wxListItemAttr *attr) { m_attr = attr; }
GetAttr() const171     wxListItemAttr *GetAttr() const { return m_attr; }
172 
173 public:
174     // the item image or -1
175     int m_image;
176 
177     // user data associated with the item
178     wxUIntPtr m_data;
179 
180     // the item coordinates are not used in report mode; instead this pointer is
181     // NULL and the owner window is used to retrieve the item position and size
182     wxRect *m_rect;
183 
184     // the list ctrl we are in
185     wxListMainWindow *m_owner;
186 
187     // custom attributes or NULL
188     wxListItemAttr *m_attr;
189 
190 protected:
191     // common part of all ctors
192     void Init();
193 
194     wxString m_text;
195 };
196 
197 //-----------------------------------------------------------------------------
198 //  wxListHeaderData (internal)
199 //-----------------------------------------------------------------------------
200 
201 class wxListHeaderData : public wxObject
202 {
203 public:
204     wxListHeaderData();
205     wxListHeaderData( const wxListItem &info );
206     void SetItem( const wxListItem &item );
207     void SetPosition( int x, int y );
208     void SetWidth( int w );
209     void SetState( int state );
210     void SetFormat( int format );
211     void SetHeight( int h );
212     bool HasImage() const;
213 
HasText() const214     bool HasText() const { return !m_text.empty(); }
GetText() const215     const wxString& GetText() const { return m_text; }
SetText(const wxString & text)216     void SetText(const wxString& text) { m_text = text; }
217 
218     void GetItem( wxListItem &item );
219 
220     bool IsHit( int x, int y ) const;
221     int GetImage() const;
222     int GetWidth() const;
223     int GetFormat() const;
224     int GetState() const;
225 
226 protected:
227     long      m_mask;
228     int       m_image;
229     wxString  m_text;
230     int       m_format;
231     int       m_width;
232     int       m_xpos,
233               m_ypos;
234     int       m_height;
235     int       m_state;
236 
237 private:
238     void Init();
239 };
240 
241 //-----------------------------------------------------------------------------
242 //  wxListLineData (internal)
243 //-----------------------------------------------------------------------------
244 
245 WX_DECLARE_EXPORTED_LIST(wxListItemData, wxListItemDataList);
246 #include "wx/listimpl.cpp"
247 WX_DEFINE_LIST(wxListItemDataList)
248 
249 class wxListLineData
250 {
251 public:
252     // the list of subitems: only may have more than one item in report mode
253     wxListItemDataList m_items;
254 
255     // this is not used in report view
256     struct GeometryInfo
257     {
258         // total item rect
259         wxRect m_rectAll;
260 
261         // label only
262         wxRect m_rectLabel;
263 
264         // icon only
265         wxRect m_rectIcon;
266 
267         // the part to be highlighted
268         wxRect m_rectHighlight;
269 
270         // extend all our rects to be centered inside the one of given width
ExtendWidthwxListLineData::GeometryInfo271         void ExtendWidth(wxCoord w)
272         {
273             wxASSERT_MSG( m_rectAll.width <= w,
274                             _T("width can only be increased") );
275 
276             m_rectAll.width = w;
277             m_rectLabel.x = m_rectAll.x + (w - m_rectLabel.width) / 2;
278             m_rectIcon.x = m_rectAll.x + (w - m_rectIcon.width) / 2;
279             m_rectHighlight.x = m_rectAll.x + (w - m_rectHighlight.width) / 2;
280         }
281     }
282     *m_gi;
283 
284     // is this item selected? [NB: not used in virtual mode]
285     bool m_highlighted;
286 
287     // back pointer to the list ctrl
288     wxListMainWindow *m_owner;
289 
290 public:
291     wxListLineData(wxListMainWindow *owner);
292 
~wxListLineData()293     ~wxListLineData()
294     {
295         WX_CLEAR_LIST(wxListItemDataList, m_items);
296         delete m_gi;
297     }
298 
299     // are we in report mode?
300     inline bool InReportView() const;
301 
302     // are we in virtual report mode?
303     inline bool IsVirtual() const;
304 
305     // these 2 methods shouldn't be called for report view controls, in that
306     // case we determine our position/size ourselves
307 
308     // calculate the size of the line
309     void CalculateSize( wxDC *dc, int spacing );
310 
311     // remember the position this line appears at
312     void SetPosition( int x, int y, int spacing );
313 
314     // wxListCtrl API
315 
SetImage(int image)316     void SetImage( int image ) { SetImage(0, image); }
GetImage() const317     int GetImage() const { return GetImage(0); }
318     void SetImage( int index, int image );
319     int GetImage( int index ) const;
320 
HasImage() const321     bool HasImage() const { return GetImage() != -1; }
HasText() const322     bool HasText() const { return !GetText(0).empty(); }
323 
324     void SetItem( int index, const wxListItem &info );
325     void GetItem( int index, wxListItem &info );
326 
327     wxString GetText(int index) const;
328     void SetText( int index, const wxString& s );
329 
330     wxListItemAttr *GetAttr() const;
331     void SetAttr(wxListItemAttr *attr);
332 
333     // return true if the highlighting really changed
334     bool Highlight( bool on );
335 
336     void ReverseHighlight();
337 
IsHighlighted() const338     bool IsHighlighted() const
339     {
340         wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
341 
342         return m_highlighted;
343     }
344 
345     // draw the line on the given DC in icon/list mode
346     void Draw( wxDC *dc );
347 
348     // the same in report mode
349     void DrawInReportMode( wxDC *dc,
350                            const wxRect& rect,
351                            const wxRect& rectHL,
352                            bool highlighted );
353 
354 private:
355     // set the line to contain num items (only can be > 1 in report mode)
356     void InitItems( int num );
357 
358     // get the mode (i.e. style)  of the list control
359     inline int GetMode() const;
360 
361     // prepare the DC for drawing with these item's attributes, return true if
362     // we need to draw the items background to highlight it, false otherwise
363     bool SetAttributes(wxDC *dc,
364                        const wxListItemAttr *attr,
365                        bool highlight);
366 
367     // draw the text on the DC with the correct justification; also add an
368     // ellipsis if the text is too large to fit in the current width
369     void DrawTextFormatted(wxDC *dc,
370                            const wxString &text,
371                            int col,
372                            int x,
373                            int yMid,    // this is middle, not top, of the text
374                            int width);
375 };
376 
377 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData, wxListLineDataArray);
378 #include "wx/arrimpl.cpp"
379 WX_DEFINE_OBJARRAY(wxListLineDataArray)
380 
381 //-----------------------------------------------------------------------------
382 //  wxListHeaderWindow (internal)
383 //-----------------------------------------------------------------------------
384 
385 class wxListHeaderWindow : public wxWindow
386 {
387 protected:
388     wxListMainWindow  *m_owner;
389     const wxCursor    *m_currentCursor;
390     wxCursor          *m_resizeCursor;
391     bool               m_isDragging;
392 
393     // column being resized or -1
394     int m_column;
395 
396     // divider line position in logical (unscrolled) coords
397     int m_currentX;
398 
399     // minimal position beyond which the divider line
400     // can't be dragged in logical coords
401     int m_minX;
402 
403 public:
404     wxListHeaderWindow();
405 
406     wxListHeaderWindow( wxWindow *win,
407                         wxWindowID id,
408                         wxListMainWindow *owner,
409                         const wxPoint &pos = wxDefaultPosition,
410                         const wxSize &size = wxDefaultSize,
411                         long style = 0,
412                         const wxString &name = wxT("wxlistctrlcolumntitles") );
413 
414     virtual ~wxListHeaderWindow();
415 
416     void DrawCurrent();
417     void AdjustDC( wxDC& dc );
418 
419     void OnPaint( wxPaintEvent &event );
420     void OnMouse( wxMouseEvent &event );
421     void OnSetFocus( wxFocusEvent &event );
422 
423     // needs refresh
424     bool m_dirty;
425 
426 private:
427     // common part of all ctors
428     void Init();
429 
430     // generate and process the list event of the given type, return true if
431     // it wasn't vetoed, i.e. if we should proceed
432     bool SendListEvent(wxEventType type, const wxPoint& pos);
433 
434     DECLARE_DYNAMIC_CLASS(wxListHeaderWindow)
435     DECLARE_EVENT_TABLE()
436 };
437 
438 //-----------------------------------------------------------------------------
439 // wxListRenameTimer (internal)
440 //-----------------------------------------------------------------------------
441 
442 class wxListRenameTimer: public wxTimer
443 {
444 private:
445     wxListMainWindow *m_owner;
446 
447 public:
448     wxListRenameTimer( wxListMainWindow *owner );
449     void Notify();
450 };
451 
452 //-----------------------------------------------------------------------------
453 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
454 //-----------------------------------------------------------------------------
455 
456 class wxListTextCtrlWrapper : public wxEvtHandler
457 {
458 public:
459     // NB: text must be a valid object but not Create()d yet
460     wxListTextCtrlWrapper(wxListMainWindow *owner,
461                           wxTextCtrl *text,
462                           size_t itemEdit);
463 
GetText() const464     wxTextCtrl *GetText() const { return m_text; }
465 
466     void AcceptChangesAndFinish();
467 
468 protected:
469     void OnChar( wxKeyEvent &event );
470     void OnKeyUp( wxKeyEvent &event );
471     void OnKillFocus( wxFocusEvent &event );
472 
473     bool AcceptChanges();
474     void Finish();
475 
476 private:
477     wxListMainWindow   *m_owner;
478     wxTextCtrl         *m_text;
479     wxString            m_startValue;
480     size_t              m_itemEdited;
481     bool                m_finished;
482     bool                m_aboutToFinish;
483 
484     DECLARE_EVENT_TABLE()
485 };
486 
487 //-----------------------------------------------------------------------------
488 //  wxListMainWindow (internal)
489 //-----------------------------------------------------------------------------
490 
491 WX_DECLARE_EXPORTED_LIST(wxListHeaderData, wxListHeaderDataList);
492 #include "wx/listimpl.cpp"
493 WX_DEFINE_LIST(wxListHeaderDataList)
494 
495 class wxListMainWindow : public wxScrolledWindow
496 {
497 public:
498     wxListMainWindow();
499     wxListMainWindow( wxWindow *parent,
500                       wxWindowID id,
501                       const wxPoint& pos = wxDefaultPosition,
502                       const wxSize& size = wxDefaultSize,
503                       long style = 0,
504                       const wxString &name = _T("listctrlmainwindow") );
505 
506     virtual ~wxListMainWindow();
507 
HasFlag(int flag) const508     bool HasFlag(int flag) const { return m_parent->HasFlag(flag); }
509 
510     // return true if this is a virtual list control
IsVirtual() const511     bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL); }
512 
513     // return true if the control is in report mode
InReportView() const514     bool InReportView() const { return HasFlag(wxLC_REPORT); }
515 
516     // return true if we are in single selection mode, false if multi sel
IsSingleSel() const517     bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL); }
518 
519     // do we have a header window?
HasHeader() const520     bool HasHeader() const
521         { return InReportView() && !HasFlag(wxLC_NO_HEADER); }
522 
523     void HighlightAll( bool on );
524 
525     // all these functions only do something if the line is currently visible
526 
527     // change the line "selected" state, return true if it really changed
528     bool HighlightLine( size_t line, bool highlight = true);
529 
530     // as HighlightLine() but do it for the range of lines: this is incredibly
531     // more efficient for virtual list controls!
532     //
533     // NB: unlike HighlightLine() this one does refresh the lines on screen
534     void HighlightLines( size_t lineFrom, size_t lineTo, bool on = true );
535 
536     // toggle the line state and refresh it
ReverseHighlight(size_t line)537     void ReverseHighlight( size_t line )
538         { HighlightLine(line, !IsHighlighted(line)); RefreshLine(line); }
539 
540     // return true if the line is highlighted
541     bool IsHighlighted(size_t line) const;
542 
543     // refresh one or several lines at once
544     void RefreshLine( size_t line );
545     void RefreshLines( size_t lineFrom, size_t lineTo );
546 
547     // refresh all selected items
548     void RefreshSelected();
549 
550     // refresh all lines below the given one: the difference with
551     // RefreshLines() is that the index here might not be a valid one (happens
552     // when the last line is deleted)
553     void RefreshAfter( size_t lineFrom );
554 
555     // the methods which are forwarded to wxListLineData itself in list/icon
556     // modes but are here because the lines don't store their positions in the
557     // report mode
558 
559     // get the bound rect for the entire line
560     wxRect GetLineRect(size_t line) const;
561 
562     // get the bound rect of the label
563     wxRect GetLineLabelRect(size_t line) const;
564 
565     // get the bound rect of the items icon (only may be called if we do have
566     // an icon!)
567     wxRect GetLineIconRect(size_t line) const;
568 
569     // get the rect to be highlighted when the item has focus
570     wxRect GetLineHighlightRect(size_t line) const;
571 
572     // get the size of the total line rect
GetLineSize(size_t line) const573     wxSize GetLineSize(size_t line) const
574         { return GetLineRect(line).GetSize(); }
575 
576     // return the hit code for the corresponding position (in this line)
577     long HitTestLine(size_t line, int x, int y) const;
578 
579     // bring the selected item into view, scrolling to it if necessary
580     void MoveToItem(size_t item);
581 
582     bool ScrollList( int WXUNUSED(dx), int dy );
583 
584     // bring the current item into view
MoveToFocus()585     void MoveToFocus() { MoveToItem(m_current); }
586 
587     // start editing the label of the given item
588     wxTextCtrl *EditLabel(long item,
589                           wxClassInfo* textControlClass = CLASSINFO(wxTextCtrl));
GetEditControl() const590     wxTextCtrl *GetEditControl() const
591     {
592         return m_textctrlWrapper ? m_textctrlWrapper->GetText() : NULL;
593     }
594 
FinishEditing(wxTextCtrl * text)595     void FinishEditing(wxTextCtrl *text)
596     {
597         delete text;
598         m_textctrlWrapper = NULL;
599         SetFocusIgnoringChildren();
600     }
601 
602     // suspend/resume redrawing the control
603     void Freeze();
604     void Thaw();
605 
606     void OnRenameTimer();
607     bool OnRenameAccept(size_t itemEdit, const wxString& value);
608     void OnRenameCancelled(size_t itemEdit);
609 
610     void OnMouse( wxMouseEvent &event );
611 
612     // called to switch the selection from the current item to newCurrent,
613     void OnArrowChar( size_t newCurrent, const wxKeyEvent& event );
614 
615     void OnChar( wxKeyEvent &event );
616     void OnKeyDown( wxKeyEvent &event );
617     void OnKeyUp( wxKeyEvent &event );
618     void OnSetFocus( wxFocusEvent &event );
619     void OnKillFocus( wxFocusEvent &event );
620     void OnScroll( wxScrollWinEvent& event );
621 
622     void OnPaint( wxPaintEvent &event );
623 
624     void OnChildFocus(wxChildFocusEvent& event);
625 
626     void DrawImage( int index, wxDC *dc, int x, int y );
627     void GetImageSize( int index, int &width, int &height ) const;
628     int GetTextLength( const wxString &s ) const;
629 
630     void SetImageList( wxImageList *imageList, int which );
631     void SetItemSpacing( int spacing, bool isSmall = false );
632     int GetItemSpacing( bool isSmall = false );
633 
634     void SetColumn( int col, wxListItem &item );
635     void SetColumnWidth( int col, int width );
636     void GetColumn( int col, wxListItem &item ) const;
637     int GetColumnWidth( int col ) const;
GetColumnCount() const638     int GetColumnCount() const { return m_columns.GetCount(); }
639 
640     // returns the sum of the heights of all columns
641     int GetHeaderWidth() const;
642 
643     int GetCountPerPage() const;
644 
645     void SetItem( wxListItem &item );
646     void GetItem( wxListItem &item ) const;
647     void SetItemState( long item, long state, long stateMask );
648     void SetItemStateAll( long state, long stateMask );
649     int GetItemState( long item, long stateMask ) const;
650     void GetItemRect( long index, wxRect &rect ) const;
651     wxRect GetViewRect() const;
652     bool GetItemPosition( long item, wxPoint& pos ) const;
653     int GetSelectedItemCount() const;
654 
GetItemText(long item) const655     wxString GetItemText(long item) const
656     {
657         wxListItem info;
658         info.m_mask = wxLIST_MASK_TEXT;
659         info.m_itemId = item;
660         GetItem( info );
661         return info.m_text;
662     }
663 
SetItemText(long item,const wxString & value)664     void SetItemText(long item, const wxString& value)
665     {
666         wxListItem info;
667         info.m_mask = wxLIST_MASK_TEXT;
668         info.m_itemId = item;
669         info.m_text = value;
670         SetItem( info );
671     }
672 
673     // set the scrollbars and update the positions of the items
674     void RecalculatePositions(bool noRefresh = false);
675 
676     // refresh the window and the header
677     void RefreshAll();
678 
679     long GetNextItem( long item, int geometry, int state ) const;
680     void DeleteItem( long index );
681     void DeleteAllItems();
682     void DeleteColumn( int col );
683     void DeleteEverything();
684     void EnsureVisible( long index );
685     long FindItem( long start, const wxString& str, bool partial = false );
686     long FindItem( long start, wxUIntPtr data);
687     long FindItem( const wxPoint& pt );
688     long HitTest( int x, int y, int &flags ) const;
689     void InsertItem( wxListItem &item );
690     void InsertColumn( long col, wxListItem &item );
691     int GetItemWidthWithImage(wxListItem * item);
692     void SortItems( wxListCtrlCompare fn, long data );
693 
694     size_t GetItemCount() const;
IsEmpty() const695     bool IsEmpty() const { return GetItemCount() == 0; }
696     void SetItemCount(long count);
697 
698     // change the current (== focused) item, send a notification event
699     void ChangeCurrent(size_t current);
ResetCurrent()700     void ResetCurrent() { ChangeCurrent((size_t)-1); }
HasCurrent() const701     bool HasCurrent() const { return m_current != (size_t)-1; }
702 
703     // send out a wxListEvent
704     void SendNotify( size_t line,
705                      wxEventType command,
706                      const wxPoint& point = wxDefaultPosition );
707 
708     // override base class virtual to reset m_lineHeight when the font changes
SetFont(const wxFont & font)709     virtual bool SetFont(const wxFont& font)
710     {
711         if ( !wxScrolledWindow::SetFont(font) )
712             return false;
713 
714         m_lineHeight = 0;
715 
716         return true;
717     }
718 
719     // these are for wxListLineData usage only
720 
721     // get the backpointer to the list ctrl
GetListCtrl() const722     wxGenericListCtrl *GetListCtrl() const
723     {
724         return wxStaticCast(GetParent(), wxGenericListCtrl);
725     }
726 
727     // get the height of all lines (assuming they all do have the same height)
728     wxCoord GetLineHeight() const;
729 
730     // get the y position of the given line (only for report view)
731     wxCoord GetLineY(size_t line) const;
732 
733     // get the brush to use for the item highlighting
GetHighlightBrush() const734     wxBrush *GetHighlightBrush() const
735     {
736         return m_hasFocus ? m_highlightBrush : m_highlightUnfocusedBrush;
737     }
738 
HasFocus() const739     bool HasFocus() const
740     {
741         return m_hasFocus;
742     }
743 
744 //protected:
745     // the array of all line objects for a non virtual list control (for the
746     // virtual list control we only ever use m_lines[0])
747     wxListLineDataArray  m_lines;
748 
749     // the list of column objects
750     wxListHeaderDataList m_columns;
751 
752     // currently focused item or -1
753     size_t               m_current;
754 
755     // the number of lines per page
756     int                  m_linesPerPage;
757 
758     // this flag is set when something which should result in the window
759     // redrawing happens (i.e. an item was added or deleted, or its appearance
760     // changed) and OnPaint() doesn't redraw the window while it is set which
761     // allows to minimize the number of repaintings when a lot of items are
762     // being added. The real repainting occurs only after the next OnIdle()
763     // call
764     bool                 m_dirty;
765 
766     wxColour            *m_highlightColour;
767     wxImageList         *m_small_image_list;
768     wxImageList         *m_normal_image_list;
769     int                  m_small_spacing;
770     int                  m_normal_spacing;
771     bool                 m_hasFocus;
772 
773     bool                 m_lastOnSame;
774     wxTimer             *m_renameTimer;
775     bool                 m_isCreated;
776     int                  m_dragCount;
777     wxPoint              m_dragStart;
778     ColWidthArray        m_aColWidths;
779 
780     // for double click logic
781     size_t m_lineLastClicked,
782            m_lineBeforeLastClicked,
783            m_lineSelectSingleOnUp;
784 
785 protected:
GetMainWindowOfCompositeControl()786     wxWindow *GetMainWindowOfCompositeControl() { return GetParent(); }
787 
788     // the total count of items in a virtual list control
789     size_t m_countVirt;
790 
791     // the object maintaining the items selection state, only used in virtual
792     // controls
793     wxSelectionStore m_selStore;
794 
795     // common part of all ctors
796     void Init();
797 
798     // get the line data for the given index
GetLine(size_t n) const799     wxListLineData *GetLine(size_t n) const
800     {
801         wxASSERT_MSG( n != (size_t)-1, _T("invalid line index") );
802 
803         if ( IsVirtual() )
804         {
805             wxConstCast(this, wxListMainWindow)->CacheLineData(n);
806             n = 0;
807         }
808 
809         return &m_lines[n];
810     }
811 
812     // get a dummy line which can be used for geometry calculations and such:
813     // you must use GetLine() if you want to really draw the line
814     wxListLineData *GetDummyLine() const;
815 
816     // cache the line data of the n-th line in m_lines[0]
817     void CacheLineData(size_t line);
818 
819     // get the range of visible lines
820     void GetVisibleLinesRange(size_t *from, size_t *to);
821 
822     // force us to recalculate the range of visible lines
ResetVisibleLinesRange()823     void ResetVisibleLinesRange() { m_lineFrom = (size_t)-1; }
824 
825     // get the colour to be used for drawing the rules
GetRuleColour() const826     wxColour GetRuleColour() const
827     {
828         return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
829     }
830 
831 private:
832     // initialize the current item if needed
833     void UpdateCurrent();
834 
835     // delete all items but don't refresh: called from dtor
836     void DoDeleteAllItems();
837 
838     // the height of one line using the current font
839     wxCoord m_lineHeight;
840 
841     // the total header width or 0 if not calculated yet
842     wxCoord m_headerWidth;
843 
844     // the first and last lines being shown on screen right now (inclusive),
845     // both may be -1 if they must be calculated so never access them directly:
846     // use GetVisibleLinesRange() above instead
847     size_t m_lineFrom,
848            m_lineTo;
849 
850     // the brushes to use for item highlighting when we do/don't have focus
851     wxBrush *m_highlightBrush,
852             *m_highlightUnfocusedBrush;
853 
854     // if this is > 0, the control is frozen and doesn't redraw itself
855     size_t m_freezeCount;
856 
857     // wrapper around the text control currently used for in place editing or
858     // NULL if no item is being edited
859     wxListTextCtrlWrapper *m_textctrlWrapper;
860 
861 
862     DECLARE_DYNAMIC_CLASS(wxListMainWindow)
863     DECLARE_EVENT_TABLE()
864 
865     friend class wxGenericListCtrl;
866 };
867 
868 
~wxListItemData()869 wxListItemData::~wxListItemData()
870 {
871     // in the virtual list control the attributes are managed by the main
872     // program, so don't delete them
873     if ( !m_owner->IsVirtual() )
874         delete m_attr;
875 
876     delete m_rect;
877 }
878 
Init()879 void wxListItemData::Init()
880 {
881     m_image = -1;
882     m_data = 0;
883 
884     m_attr = NULL;
885 }
886 
wxListItemData(wxListMainWindow * owner)887 wxListItemData::wxListItemData(wxListMainWindow *owner)
888 {
889     Init();
890 
891     m_owner = owner;
892 
893     if ( owner->InReportView() )
894         m_rect = NULL;
895     else
896         m_rect = new wxRect;
897 }
898 
SetItem(const wxListItem & info)899 void wxListItemData::SetItem( const wxListItem &info )
900 {
901     if ( info.m_mask & wxLIST_MASK_TEXT )
902         SetText(info.m_text);
903     if ( info.m_mask & wxLIST_MASK_IMAGE )
904         m_image = info.m_image;
905     if ( info.m_mask & wxLIST_MASK_DATA )
906         m_data = info.m_data;
907 
908     if ( info.HasAttributes() )
909     {
910         if ( m_attr )
911             m_attr->AssignFrom(*info.GetAttributes());
912         else
913             m_attr = new wxListItemAttr(*info.GetAttributes());
914     }
915 
916     if ( m_rect )
917     {
918         m_rect->x =
919         m_rect->y =
920         m_rect->height = 0;
921         m_rect->width = info.m_width;
922     }
923 }
924 
SetPosition(int x,int y)925 void wxListItemData::SetPosition( int x, int y )
926 {
927     wxCHECK_RET( m_rect, _T("unexpected SetPosition() call") );
928 
929     m_rect->x = x;
930     m_rect->y = y;
931 }
932 
SetSize(int width,int height)933 void wxListItemData::SetSize( int width, int height )
934 {
935     wxCHECK_RET( m_rect, _T("unexpected SetSize() call") );
936 
937     if ( width != -1 )
938         m_rect->width = width;
939     if ( height != -1 )
940         m_rect->height = height;
941 }
942 
IsHit(int x,int y) const943 bool wxListItemData::IsHit( int x, int y ) const
944 {
945     wxCHECK_MSG( m_rect, false, _T("can't be called in this mode") );
946 
947     return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x, y);
948 }
949 
GetX() const950 int wxListItemData::GetX() const
951 {
952     wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
953 
954     return m_rect->x;
955 }
956 
GetY() const957 int wxListItemData::GetY() const
958 {
959     wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
960 
961     return m_rect->y;
962 }
963 
GetWidth() const964 int wxListItemData::GetWidth() const
965 {
966     wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
967 
968     return m_rect->width;
969 }
970 
GetHeight() const971 int wxListItemData::GetHeight() const
972 {
973     wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
974 
975     return m_rect->height;
976 }
977 
GetItem(wxListItem & info) const978 void wxListItemData::GetItem( wxListItem &info ) const
979 {
980     long mask = info.m_mask;
981     if ( !mask )
982         // by default, get everything for backwards compatibility
983         mask = -1;
984 
985     if ( mask & wxLIST_MASK_TEXT )
986         info.m_text = m_text;
987     if ( mask & wxLIST_MASK_IMAGE )
988         info.m_image = m_image;
989     if ( mask & wxLIST_MASK_DATA )
990         info.m_data = m_data;
991 
992     if ( m_attr )
993     {
994         if ( m_attr->HasTextColour() )
995             info.SetTextColour(m_attr->GetTextColour());
996         if ( m_attr->HasBackgroundColour() )
997             info.SetBackgroundColour(m_attr->GetBackgroundColour());
998         if ( m_attr->HasFont() )
999             info.SetFont(m_attr->GetFont());
1000     }
1001 }
1002 
1003 //-----------------------------------------------------------------------------
1004 //  wxListHeaderData
1005 //-----------------------------------------------------------------------------
1006 
Init()1007 void wxListHeaderData::Init()
1008 {
1009     m_mask = 0;
1010     m_image = -1;
1011     m_format = 0;
1012     m_width = 0;
1013     m_xpos = 0;
1014     m_ypos = 0;
1015     m_height = 0;
1016     m_state = 0;
1017 }
1018 
wxListHeaderData()1019 wxListHeaderData::wxListHeaderData()
1020 {
1021     Init();
1022 }
1023 
wxListHeaderData(const wxListItem & item)1024 wxListHeaderData::wxListHeaderData( const wxListItem &item )
1025 {
1026     Init();
1027 
1028     SetItem( item );
1029 }
1030 
SetItem(const wxListItem & item)1031 void wxListHeaderData::SetItem( const wxListItem &item )
1032 {
1033     m_mask = item.m_mask;
1034 
1035     if ( m_mask & wxLIST_MASK_TEXT )
1036         m_text = item.m_text;
1037 
1038     if ( m_mask & wxLIST_MASK_IMAGE )
1039         m_image = item.m_image;
1040 
1041     if ( m_mask & wxLIST_MASK_FORMAT )
1042         m_format = item.m_format;
1043 
1044     if ( m_mask & wxLIST_MASK_WIDTH )
1045         SetWidth(item.m_width);
1046 
1047     if ( m_mask & wxLIST_MASK_STATE )
1048         SetState(item.m_state);
1049 }
1050 
SetPosition(int x,int y)1051 void wxListHeaderData::SetPosition( int x, int y )
1052 {
1053     m_xpos = x;
1054     m_ypos = y;
1055 }
1056 
SetHeight(int h)1057 void wxListHeaderData::SetHeight( int h )
1058 {
1059     m_height = h;
1060 }
1061 
SetWidth(int w)1062 void wxListHeaderData::SetWidth( int w )
1063 {
1064     m_width = w < 0 ? WIDTH_COL_DEFAULT : w;
1065 }
1066 
SetState(int flag)1067 void wxListHeaderData::SetState( int flag )
1068 {
1069     m_state = flag;
1070 }
1071 
SetFormat(int format)1072 void wxListHeaderData::SetFormat( int format )
1073 {
1074     m_format = format;
1075 }
1076 
HasImage() const1077 bool wxListHeaderData::HasImage() const
1078 {
1079     return m_image != -1;
1080 }
1081 
IsHit(int x,int y) const1082 bool wxListHeaderData::IsHit( int x, int y ) const
1083 {
1084     return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
1085 }
1086 
GetItem(wxListItem & item)1087 void wxListHeaderData::GetItem( wxListItem& item )
1088 {
1089     item.m_mask = m_mask;
1090     item.m_text = m_text;
1091     item.m_image = m_image;
1092     item.m_format = m_format;
1093     item.m_width = m_width;
1094     item.m_state = m_state;
1095 }
1096 
GetImage() const1097 int wxListHeaderData::GetImage() const
1098 {
1099     return m_image;
1100 }
1101 
GetWidth() const1102 int wxListHeaderData::GetWidth() const
1103 {
1104     return m_width;
1105 }
1106 
GetFormat() const1107 int wxListHeaderData::GetFormat() const
1108 {
1109     return m_format;
1110 }
1111 
GetState() const1112 int wxListHeaderData::GetState() const
1113 {
1114     return m_state;
1115 }
1116 
1117 //-----------------------------------------------------------------------------
1118 //  wxListLineData
1119 //-----------------------------------------------------------------------------
1120 
GetMode() const1121 inline int wxListLineData::GetMode() const
1122 {
1123     return m_owner->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE;
1124 }
1125 
InReportView() const1126 inline bool wxListLineData::InReportView() const
1127 {
1128     return m_owner->HasFlag(wxLC_REPORT);
1129 }
1130 
IsVirtual() const1131 inline bool wxListLineData::IsVirtual() const
1132 {
1133     return m_owner->IsVirtual();
1134 }
1135 
wxListLineData(wxListMainWindow * owner)1136 wxListLineData::wxListLineData( wxListMainWindow *owner )
1137 {
1138     m_owner = owner;
1139 
1140     if ( InReportView() )
1141         m_gi = NULL;
1142     else // !report
1143         m_gi = new GeometryInfo;
1144 
1145     m_highlighted = false;
1146 
1147     InitItems( GetMode() == wxLC_REPORT ? m_owner->GetColumnCount() : 1 );
1148 }
1149 
CalculateSize(wxDC * dc,int spacing)1150 void wxListLineData::CalculateSize( wxDC *dc, int spacing )
1151 {
1152     wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1153     wxCHECK_RET( node, _T("no subitems at all??") );
1154 
1155     wxListItemData *item = node->GetData();
1156 
1157     wxString s;
1158     wxCoord lw, lh;
1159 
1160     switch ( GetMode() )
1161     {
1162         case wxLC_ICON:
1163         case wxLC_SMALL_ICON:
1164             m_gi->m_rectAll.width = spacing;
1165 
1166             s = item->GetText();
1167 
1168             if ( s.empty() )
1169             {
1170                 lh =
1171                 m_gi->m_rectLabel.width =
1172                 m_gi->m_rectLabel.height = 0;
1173             }
1174             else // has label
1175             {
1176                 dc->GetTextExtent( s, &lw, &lh );
1177                 lw += EXTRA_WIDTH;
1178                 lh += EXTRA_HEIGHT;
1179 
1180                 m_gi->m_rectAll.height = spacing + lh;
1181                 if (lw > spacing)
1182                     m_gi->m_rectAll.width = lw;
1183 
1184                 m_gi->m_rectLabel.width = lw;
1185                 m_gi->m_rectLabel.height = lh;
1186             }
1187 
1188             if (item->HasImage())
1189             {
1190                 int w, h;
1191                 m_owner->GetImageSize( item->GetImage(), w, h );
1192                 m_gi->m_rectIcon.width = w + 8;
1193                 m_gi->m_rectIcon.height = h + 8;
1194 
1195                 if ( m_gi->m_rectIcon.width > m_gi->m_rectAll.width )
1196                     m_gi->m_rectAll.width = m_gi->m_rectIcon.width;
1197                 if ( m_gi->m_rectIcon.height + lh > m_gi->m_rectAll.height - 4 )
1198                     m_gi->m_rectAll.height = m_gi->m_rectIcon.height + lh + 4;
1199             }
1200 
1201             if ( item->HasText() )
1202             {
1203                 m_gi->m_rectHighlight.width = m_gi->m_rectLabel.width;
1204                 m_gi->m_rectHighlight.height = m_gi->m_rectLabel.height;
1205             }
1206             else // no text, highlight the icon
1207             {
1208                 m_gi->m_rectHighlight.width = m_gi->m_rectIcon.width;
1209                 m_gi->m_rectHighlight.height = m_gi->m_rectIcon.height;
1210             }
1211             break;
1212 
1213         case wxLC_LIST:
1214             s = item->GetTextForMeasuring();
1215 
1216             dc->GetTextExtent( s, &lw, &lh );
1217             lw += EXTRA_WIDTH;
1218             lh += EXTRA_HEIGHT;
1219 
1220             m_gi->m_rectLabel.width = lw;
1221             m_gi->m_rectLabel.height = lh;
1222 
1223             m_gi->m_rectAll.width = lw;
1224             m_gi->m_rectAll.height = lh;
1225 
1226             if (item->HasImage())
1227             {
1228                 int w, h;
1229                 m_owner->GetImageSize( item->GetImage(), w, h );
1230                 m_gi->m_rectIcon.width = w;
1231                 m_gi->m_rectIcon.height = h;
1232 
1233                 m_gi->m_rectAll.width += 4 + w;
1234                 if (h > m_gi->m_rectAll.height)
1235                     m_gi->m_rectAll.height = h;
1236             }
1237 
1238             m_gi->m_rectHighlight.width = m_gi->m_rectAll.width;
1239             m_gi->m_rectHighlight.height = m_gi->m_rectAll.height;
1240             break;
1241 
1242         case wxLC_REPORT:
1243             wxFAIL_MSG( _T("unexpected call to SetSize") );
1244             break;
1245 
1246         default:
1247             wxFAIL_MSG( _T("unknown mode") );
1248             break;
1249     }
1250 }
1251 
SetPosition(int x,int y,int spacing)1252 void wxListLineData::SetPosition( int x, int y, int spacing )
1253 {
1254     wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1255     wxCHECK_RET( node, _T("no subitems at all??") );
1256 
1257     wxListItemData *item = node->GetData();
1258 
1259     switch ( GetMode() )
1260     {
1261         case wxLC_ICON:
1262         case wxLC_SMALL_ICON:
1263             m_gi->m_rectAll.x = x;
1264             m_gi->m_rectAll.y = y;
1265 
1266             if ( item->HasImage() )
1267             {
1268                 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 4 +
1269                     (m_gi->m_rectAll.width - m_gi->m_rectIcon.width) / 2;
1270                 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 4;
1271             }
1272 
1273             if ( item->HasText() )
1274             {
1275                 if (m_gi->m_rectAll.width > spacing)
1276                     m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
1277                 else
1278                     m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2) + (spacing / 2) - (m_gi->m_rectLabel.width / 2);
1279                 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + m_gi->m_rectAll.height + 2 - m_gi->m_rectLabel.height;
1280                 m_gi->m_rectHighlight.x = m_gi->m_rectLabel.x - 2;
1281                 m_gi->m_rectHighlight.y = m_gi->m_rectLabel.y - 2;
1282             }
1283             else // no text, highlight the icon
1284             {
1285                 m_gi->m_rectHighlight.x = m_gi->m_rectIcon.x - 4;
1286                 m_gi->m_rectHighlight.y = m_gi->m_rectIcon.y - 4;
1287             }
1288             break;
1289 
1290         case wxLC_LIST:
1291             m_gi->m_rectAll.x = x;
1292             m_gi->m_rectAll.y = y;
1293 
1294             m_gi->m_rectHighlight.x = m_gi->m_rectAll.x;
1295             m_gi->m_rectHighlight.y = m_gi->m_rectAll.y;
1296             m_gi->m_rectLabel.y = m_gi->m_rectAll.y + 2;
1297 
1298             if (item->HasImage())
1299             {
1300                 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 2;
1301                 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 2;
1302                 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 4 + (EXTRA_WIDTH/2) + m_gi->m_rectIcon.width;
1303             }
1304             else
1305             {
1306                 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
1307             }
1308             break;
1309 
1310         case wxLC_REPORT:
1311             wxFAIL_MSG( _T("unexpected call to SetPosition") );
1312             break;
1313 
1314         default:
1315             wxFAIL_MSG( _T("unknown mode") );
1316             break;
1317     }
1318 }
1319 
InitItems(int num)1320 void wxListLineData::InitItems( int num )
1321 {
1322     for (int i = 0; i < num; i++)
1323         m_items.Append( new wxListItemData(m_owner) );
1324 }
1325 
SetItem(int index,const wxListItem & info)1326 void wxListLineData::SetItem( int index, const wxListItem &info )
1327 {
1328     wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1329     wxCHECK_RET( node, _T("invalid column index in SetItem") );
1330 
1331     wxListItemData *item = node->GetData();
1332     item->SetItem( info );
1333 }
1334 
GetItem(int index,wxListItem & info)1335 void wxListLineData::GetItem( int index, wxListItem &info )
1336 {
1337     wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1338     if (node)
1339     {
1340         wxListItemData *item = node->GetData();
1341         item->GetItem( info );
1342     }
1343 }
1344 
GetText(int index) const1345 wxString wxListLineData::GetText(int index) const
1346 {
1347     wxString s;
1348 
1349     wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1350     if (node)
1351     {
1352         wxListItemData *item = node->GetData();
1353         s = item->GetText();
1354     }
1355 
1356     return s;
1357 }
1358 
SetText(int index,const wxString & s)1359 void wxListLineData::SetText( int index, const wxString& s )
1360 {
1361     wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1362     if (node)
1363     {
1364         wxListItemData *item = node->GetData();
1365         item->SetText( s );
1366     }
1367 }
1368 
SetImage(int index,int image)1369 void wxListLineData::SetImage( int index, int image )
1370 {
1371     wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1372     wxCHECK_RET( node, _T("invalid column index in SetImage()") );
1373 
1374     wxListItemData *item = node->GetData();
1375     item->SetImage(image);
1376 }
1377 
GetImage(int index) const1378 int wxListLineData::GetImage( int index ) const
1379 {
1380     wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1381     wxCHECK_MSG( node, -1, _T("invalid column index in GetImage()") );
1382 
1383     wxListItemData *item = node->GetData();
1384     return item->GetImage();
1385 }
1386 
GetAttr() const1387 wxListItemAttr *wxListLineData::GetAttr() const
1388 {
1389     wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1390     wxCHECK_MSG( node, NULL, _T("invalid column index in GetAttr()") );
1391 
1392     wxListItemData *item = node->GetData();
1393     return item->GetAttr();
1394 }
1395 
SetAttr(wxListItemAttr * attr)1396 void wxListLineData::SetAttr(wxListItemAttr *attr)
1397 {
1398     wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1399     wxCHECK_RET( node, _T("invalid column index in SetAttr()") );
1400 
1401     wxListItemData *item = node->GetData();
1402     item->SetAttr(attr);
1403 }
1404 
SetAttributes(wxDC * dc,const wxListItemAttr * attr,bool highlighted)1405 bool wxListLineData::SetAttributes(wxDC *dc,
1406                                    const wxListItemAttr *attr,
1407                                    bool highlighted)
1408 {
1409     wxWindow *listctrl = m_owner->GetParent();
1410 
1411     // fg colour
1412 
1413     // don't use foreground colour for drawing highlighted items - this might
1414     // make them completely invisible (and there is no way to do bit
1415     // arithmetics on wxColour, unfortunately)
1416     wxColour colText;
1417     if ( highlighted )
1418 #ifdef __WXMAC__
1419     {
1420         if (m_owner->HasFocus()
1421 #ifdef __WXMAC__
1422                 && IsControlActive( (ControlRef)m_owner->GetHandle() )
1423 #endif
1424         )
1425             colText = *wxWHITE;
1426         else
1427             colText = *wxBLACK;
1428     }
1429 #else
1430     {
1431       if (m_owner->HasFocus())
1432           colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
1433       else
1434           colText = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT);
1435     }
1436 #endif
1437     else if ( attr && attr->HasTextColour() )
1438         colText = attr->GetTextColour();
1439     else
1440         colText = listctrl->GetForegroundColour();
1441 
1442     dc->SetTextForeground(colText);
1443 
1444     // font
1445     wxFont font;
1446     if ( attr && attr->HasFont() )
1447         font = attr->GetFont();
1448     else
1449         font = listctrl->GetFont();
1450 
1451     dc->SetFont(font);
1452 
1453     // bg colour
1454     bool hasBgCol = attr && attr->HasBackgroundColour();
1455     if ( highlighted || hasBgCol )
1456     {
1457         if ( highlighted )
1458             dc->SetBrush( *m_owner->GetHighlightBrush() );
1459         else
1460             dc->SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
1461 
1462         dc->SetPen( *wxTRANSPARENT_PEN );
1463 
1464         return true;
1465     }
1466 
1467     return false;
1468 }
1469 
Draw(wxDC * dc)1470 void wxListLineData::Draw( wxDC *dc )
1471 {
1472     wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1473     wxCHECK_RET( node, _T("no subitems at all??") );
1474 
1475     bool highlighted = IsHighlighted();
1476 
1477     wxListItemAttr *attr = GetAttr();
1478 
1479     if ( SetAttributes(dc, attr, highlighted) )
1480 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1481     {
1482         dc->DrawRectangle( m_gi->m_rectHighlight );
1483     }
1484 #else
1485     {
1486         if (highlighted)
1487         {
1488             int flags = wxCONTROL_SELECTED;
1489             if (m_owner->HasFocus()
1490 #ifdef __WXMAC__
1491                 && IsControlActive( (ControlRef)m_owner->GetHandle() )
1492 #endif
1493             )
1494                 flags |= wxCONTROL_FOCUSED;
1495             wxRendererNative::Get().DrawItemSelectionRect( m_owner, *dc, m_gi->m_rectHighlight, flags );
1496 
1497         }
1498         else
1499         {
1500             dc->DrawRectangle( m_gi->m_rectHighlight );
1501         }
1502     }
1503 #endif
1504 
1505     // just for debugging to better see where the items are
1506 #if 0
1507     dc->SetPen(*wxRED_PEN);
1508     dc->SetBrush(*wxTRANSPARENT_BRUSH);
1509     dc->DrawRectangle( m_gi->m_rectAll );
1510     dc->SetPen(*wxGREEN_PEN);
1511     dc->DrawRectangle( m_gi->m_rectIcon );
1512 #endif
1513 
1514     wxListItemData *item = node->GetData();
1515     if (item->HasImage())
1516     {
1517         // centre the image inside our rectangle, this looks nicer when items
1518         // ae aligned in a row
1519         const wxRect& rectIcon = m_gi->m_rectIcon;
1520 
1521         m_owner->DrawImage(item->GetImage(), dc, rectIcon.x, rectIcon.y);
1522     }
1523 
1524     if (item->HasText())
1525     {
1526         const wxRect& rectLabel = m_gi->m_rectLabel;
1527 
1528         wxDCClipper clipper(*dc, rectLabel);
1529         dc->DrawText(item->GetText(), rectLabel.x, rectLabel.y);
1530     }
1531 }
1532 
DrawInReportMode(wxDC * dc,const wxRect & rect,const wxRect & rectHL,bool highlighted)1533 void wxListLineData::DrawInReportMode( wxDC *dc,
1534                                        const wxRect& rect,
1535                                        const wxRect& rectHL,
1536                                        bool highlighted )
1537 {
1538     // TODO: later we should support setting different attributes for
1539     //       different columns - to do it, just add "col" argument to
1540     //       GetAttr() and move these lines into the loop below
1541     wxListItemAttr *attr = GetAttr();
1542     if ( SetAttributes(dc, attr, highlighted) )
1543 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1544     {
1545         dc->DrawRectangle( rectHL );
1546     }
1547 #else
1548     {
1549         if (highlighted)
1550         {
1551             int flags = wxCONTROL_SELECTED;
1552             if (m_owner->HasFocus()
1553 #ifdef __WXMAC__
1554                 && IsControlActive( (ControlRef)m_owner->GetHandle() )
1555 #endif
1556             )
1557                 flags |= wxCONTROL_FOCUSED;
1558             wxRendererNative::Get().DrawItemSelectionRect( m_owner, *dc, rectHL, flags );
1559         }
1560         else
1561         {
1562             dc->DrawRectangle( rectHL );
1563         }
1564     }
1565 #endif
1566 
1567     wxCoord x = rect.x + HEADER_OFFSET_X,
1568             yMid = rect.y + rect.height/2;
1569 #ifdef __WXGTK__
1570     // This probably needs to be done
1571     // on all platforms as the icons
1572     // otherwise nearly touch the border
1573     x += 2;
1574 #endif
1575 
1576     size_t col = 0;
1577     for ( wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1578           node;
1579           node = node->GetNext(), col++ )
1580     {
1581         wxListItemData *item = node->GetData();
1582 
1583         int width = m_owner->GetColumnWidth(col);
1584         int xOld = x;
1585         x += width;
1586 
1587         if ( item->HasImage() )
1588         {
1589             int ix, iy;
1590             m_owner->GetImageSize( item->GetImage(), ix, iy );
1591             m_owner->DrawImage( item->GetImage(), dc, xOld, yMid - iy/2 );
1592 
1593             ix += IMAGE_MARGIN_IN_REPORT_MODE;
1594 
1595             xOld += ix;
1596             width -= ix;
1597         }
1598 
1599         if ( item->HasText() )
1600             DrawTextFormatted(dc, item->GetText(), col, xOld, yMid, width - 8);
1601     }
1602 }
1603 
DrawTextFormatted(wxDC * dc,const wxString & textOrig,int col,int x,int yMid,int width)1604 void wxListLineData::DrawTextFormatted(wxDC *dc,
1605                                        const wxString& textOrig,
1606                                        int col,
1607                                        int x,
1608                                        int yMid,
1609                                        int width)
1610 {
1611     // we don't support displaying multiple lines currently (and neither does
1612     // wxMSW FWIW) so just merge all the lines
1613     wxString text(textOrig);
1614     text.Replace(_T("\n"), _T(" "));
1615 
1616     wxCoord w, h;
1617     dc->GetTextExtent(text, &w, &h);
1618 
1619     const wxCoord y = yMid - (h + 1)/2;
1620 
1621     wxDCClipper clipper(*dc, x, y, width, h);
1622 
1623     // determine if the string can fit inside the current width
1624     if (w <= width)
1625     {
1626         // it can, draw it using the items alignment
1627         wxListItem item;
1628         m_owner->GetColumn(col, item);
1629         switch ( item.GetAlign() )
1630         {
1631             case wxLIST_FORMAT_LEFT:
1632                 // nothing to do
1633                 break;
1634 
1635             case wxLIST_FORMAT_RIGHT:
1636                 x += width - w;
1637                 break;
1638 
1639             case wxLIST_FORMAT_CENTER:
1640                 x += (width - w) / 2;
1641                 break;
1642 
1643             default:
1644                 wxFAIL_MSG( _T("unknown list item format") );
1645                 break;
1646         }
1647 
1648         dc->DrawText(text, x, y);
1649     }
1650     else // otherwise, truncate and add an ellipsis if possible
1651     {
1652         // determine the base width
1653         wxString ellipsis(wxT("..."));
1654         wxCoord base_w;
1655         dc->GetTextExtent(ellipsis, &base_w, &h);
1656 
1657         // continue until we have enough space or only one character left
1658         wxCoord w_c, h_c;
1659         size_t len = text.length();
1660         wxString drawntext = text.Left(len);
1661         while (len > 1)
1662         {
1663             dc->GetTextExtent(drawntext.Last(), &w_c, &h_c);
1664             drawntext.RemoveLast();
1665             len--;
1666             w -= w_c;
1667             if (w + base_w <= width)
1668                 break;
1669         }
1670 
1671         // if still not enough space, remove ellipsis characters
1672         while (ellipsis.length() > 0 && w + base_w > width)
1673         {
1674             ellipsis = ellipsis.Left(ellipsis.length() - 1);
1675             dc->GetTextExtent(ellipsis, &base_w, &h);
1676         }
1677 
1678         // now draw the text
1679         dc->DrawText(drawntext, x, y);
1680         dc->DrawText(ellipsis, x + w, y);
1681     }
1682 }
1683 
Highlight(bool on)1684 bool wxListLineData::Highlight( bool on )
1685 {
1686     wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1687 
1688     if ( on == m_highlighted )
1689         return false;
1690 
1691     m_highlighted = on;
1692 
1693     return true;
1694 }
1695 
ReverseHighlight(void)1696 void wxListLineData::ReverseHighlight( void )
1697 {
1698     Highlight(!IsHighlighted());
1699 }
1700 
1701 //-----------------------------------------------------------------------------
1702 //  wxListHeaderWindow
1703 //-----------------------------------------------------------------------------
1704 
IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow,wxWindow)1705 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow,wxWindow)
1706 
1707 BEGIN_EVENT_TABLE(wxListHeaderWindow,wxWindow)
1708     EVT_PAINT         (wxListHeaderWindow::OnPaint)
1709     EVT_MOUSE_EVENTS  (wxListHeaderWindow::OnMouse)
1710     EVT_SET_FOCUS     (wxListHeaderWindow::OnSetFocus)
1711 END_EVENT_TABLE()
1712 
1713 void wxListHeaderWindow::Init()
1714 {
1715     m_currentCursor = (wxCursor *) NULL;
1716     m_isDragging = false;
1717     m_dirty = false;
1718 }
1719 
wxListHeaderWindow()1720 wxListHeaderWindow::wxListHeaderWindow()
1721 {
1722     Init();
1723 
1724     m_owner = (wxListMainWindow *) NULL;
1725     m_resizeCursor = (wxCursor *) NULL;
1726 }
1727 
wxListHeaderWindow(wxWindow * win,wxWindowID id,wxListMainWindow * owner,const wxPoint & pos,const wxSize & size,long style,const wxString & name)1728 wxListHeaderWindow::wxListHeaderWindow( wxWindow *win,
1729                                         wxWindowID id,
1730                                         wxListMainWindow *owner,
1731                                         const wxPoint& pos,
1732                                         const wxSize& size,
1733                                         long style,
1734                                         const wxString &name )
1735                   : wxWindow( win, id, pos, size, style, name )
1736 {
1737     Init();
1738 
1739     m_owner = owner;
1740     m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
1741 
1742 #if _USE_VISATTR
1743     wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes();
1744     SetOwnForegroundColour( attr.colFg );
1745     SetOwnBackgroundColour( attr.colBg );
1746     if (!m_hasFont)
1747         SetOwnFont( attr.font );
1748 #else
1749     SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
1750     SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
1751     if (!m_hasFont)
1752         SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT ));
1753 #endif
1754 }
1755 
~wxListHeaderWindow()1756 wxListHeaderWindow::~wxListHeaderWindow()
1757 {
1758     delete m_resizeCursor;
1759 }
1760 
1761 #ifdef __WXUNIVERSAL__
1762 #include "wx/univ/renderer.h"
1763 #include "wx/univ/theme.h"
1764 #endif
1765 
1766 // shift the DC origin to match the position of the main window horz
1767 // scrollbar: this allows us to always use logical coords
AdjustDC(wxDC & dc)1768 void wxListHeaderWindow::AdjustDC(wxDC& dc)
1769 {
1770     int xpix;
1771     m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
1772 
1773     int view_start;
1774     m_owner->GetViewStart( &view_start, NULL );
1775 
1776 
1777     int org_x = 0;
1778     int org_y = 0;
1779     dc.GetDeviceOrigin( &org_x, &org_y );
1780 
1781     // account for the horz scrollbar offset
1782 #ifdef __WXGTK__
1783     if (GetLayoutDirection() == wxLayout_RightToLeft)
1784     {
1785         // Maybe we just have to check for m_signX
1786         // in the DC, but I leave the #ifdef __WXGTK__
1787         // for now
1788         dc.SetDeviceOrigin( org_x + (view_start * xpix), org_y );
1789     }
1790     else
1791 #endif
1792         dc.SetDeviceOrigin( org_x - (view_start * xpix), org_y );
1793 }
1794 
OnPaint(wxPaintEvent & WXUNUSED (event))1795 void wxListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1796 {
1797     wxPaintDC dc( this );
1798 
1799     PrepareDC( dc );
1800     AdjustDC( dc );
1801 
1802     dc.SetFont( GetFont() );
1803 
1804     // width and height of the entire header window
1805     int w, h;
1806     GetClientSize( &w, &h );
1807     m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
1808 
1809     dc.SetBackgroundMode(wxTRANSPARENT);
1810     dc.SetTextForeground(GetForegroundColour());
1811 
1812     int x = HEADER_OFFSET_X;
1813     int numColumns = m_owner->GetColumnCount();
1814     wxListItem item;
1815     for ( int i = 0; i < numColumns && x < w; i++ )
1816     {
1817         m_owner->GetColumn( i, item );
1818         int wCol = item.m_width;
1819 
1820         int cw = wCol;
1821         int ch = h;
1822 
1823         int flags = 0;
1824         if (!m_parent->IsEnabled())
1825             flags |= wxCONTROL_DISABLED;
1826 
1827 // NB: The code below is not really Mac-specific, but since we are close
1828 // to 2.8 release and I don't have time to test on other platforms, I
1829 // defined this only for wxMac. If this behavior is desired on
1830 // other platforms, please go ahead and revise or remove the #ifdef.
1831 #ifdef __WXMAC__
1832         if ( !m_owner->IsVirtual() && (item.m_mask & wxLIST_MASK_STATE) &&
1833                 (item.m_state & wxLIST_STATE_SELECTED) )
1834             flags |= wxCONTROL_SELECTED;
1835 #endif
1836 
1837         wxRendererNative::Get().DrawHeaderButton
1838                                 (
1839                                     this,
1840                                     dc,
1841                                     wxRect(x, HEADER_OFFSET_Y, cw, ch),
1842                                     flags
1843                                 );
1844 
1845         // see if we have enough space for the column label
1846 
1847         // for this we need the width of the text
1848         wxCoord wLabel;
1849         wxCoord hLabel;
1850         dc.GetTextExtent(item.GetText(), &wLabel, &hLabel);
1851         wLabel += 2 * EXTRA_WIDTH;
1852 
1853         // and the width of the icon, if any
1854         int ix = 0, iy = 0;    // init them just to suppress the compiler warnings
1855         const int image = item.m_image;
1856         wxImageList *imageList;
1857         if ( image != -1 )
1858         {
1859             imageList = m_owner->m_small_image_list;
1860             if ( imageList )
1861             {
1862                 imageList->GetSize(image, ix, iy);
1863                 wLabel += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
1864             }
1865         }
1866         else
1867         {
1868             imageList = NULL;
1869         }
1870 
1871         // ignore alignment if there is not enough space anyhow
1872         int xAligned;
1873         switch ( wLabel < cw ? item.GetAlign() : wxLIST_FORMAT_LEFT )
1874         {
1875             default:
1876                 wxFAIL_MSG( _T("unknown list item format") );
1877                 // fall through
1878 
1879             case wxLIST_FORMAT_LEFT:
1880                 xAligned = x;
1881                 break;
1882 
1883             case wxLIST_FORMAT_RIGHT:
1884                 xAligned = x + cw - wLabel;
1885                 break;
1886 
1887             case wxLIST_FORMAT_CENTER:
1888                 xAligned = x + (cw - wLabel) / 2;
1889                 break;
1890         }
1891 
1892         // draw the text and image clipping them so that they
1893         // don't overwrite the column boundary
1894         wxDCClipper clipper(dc, x, HEADER_OFFSET_Y, cw, h  );
1895 
1896         // if we have an image, draw it on the right of the label
1897         if ( imageList )
1898         {
1899             imageList->Draw
1900                        (
1901                         image,
1902                         dc,
1903                         xAligned + wLabel - ix - HEADER_IMAGE_MARGIN_IN_REPORT_MODE,
1904                         HEADER_OFFSET_Y + (h - 4 - iy)/2,
1905                         wxIMAGELIST_DRAW_TRANSPARENT
1906                        );
1907         }
1908 
1909         dc.DrawText( item.GetText(),
1910                      xAligned + EXTRA_WIDTH, h / 2 - hLabel / 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1911 
1912         x += wCol;
1913     }
1914 
1915     // Fill in what's missing to the right of the columns, otherwise we will
1916     // leave an unpainted area when columns are removed (and it looks better)
1917     if ( x < w )
1918     {
1919         wxRendererNative::Get().DrawHeaderButton
1920                                 (
1921                                     this,
1922                                     dc,
1923                                     wxRect(x, HEADER_OFFSET_Y, w - x, h),
1924                                     0
1925                                 );
1926     }
1927 }
1928 
DrawCurrent()1929 void wxListHeaderWindow::DrawCurrent()
1930 {
1931 #if 1
1932     m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1933 #else
1934     int x1 = m_currentX;
1935     int y1 = 0;
1936     m_owner->ClientToScreen( &x1, &y1 );
1937 
1938     int x2 = m_currentX;
1939     int y2 = 0;
1940     m_owner->GetClientSize( NULL, &y2 );
1941     m_owner->ClientToScreen( &x2, &y2 );
1942 
1943     wxScreenDC dc;
1944     dc.SetLogicalFunction( wxINVERT );
1945     dc.SetPen( wxPen( *wxBLACK, 2, wxSOLID ) );
1946     dc.SetBrush( *wxTRANSPARENT_BRUSH );
1947 
1948     AdjustDC(dc);
1949 
1950     dc.DrawLine( x1, y1, x2, y2 );
1951 
1952     dc.SetLogicalFunction( wxCOPY );
1953 
1954     dc.SetPen( wxNullPen );
1955     dc.SetBrush( wxNullBrush );
1956 #endif
1957 }
1958 
OnMouse(wxMouseEvent & event)1959 void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
1960 {
1961     // we want to work with logical coords
1962     int x;
1963     m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
1964     int y = event.GetY();
1965 
1966     if (m_isDragging)
1967     {
1968         SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING, event.GetPosition());
1969 
1970         // we don't draw the line beyond our window, but we allow dragging it
1971         // there
1972         int w = 0;
1973         GetClientSize( &w, NULL );
1974         m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
1975         w -= 6;
1976 
1977         // erase the line if it was drawn
1978         if ( m_currentX < w )
1979             DrawCurrent();
1980 
1981         if (event.ButtonUp())
1982         {
1983             ReleaseMouse();
1984             m_isDragging = false;
1985             m_dirty = true;
1986             m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1987             SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG, event.GetPosition());
1988         }
1989         else
1990         {
1991             if (x > m_minX + 7)
1992                 m_currentX = x;
1993             else
1994                 m_currentX = m_minX + 7;
1995 
1996             // draw in the new location
1997             if ( m_currentX < w )
1998                 DrawCurrent();
1999         }
2000     }
2001     else // not dragging
2002     {
2003         m_minX = 0;
2004         bool hit_border = false;
2005 
2006         // end of the current column
2007         int xpos = 0;
2008 
2009         // find the column where this event occurred
2010         int col,
2011             countCol = m_owner->GetColumnCount();
2012         for (col = 0; col < countCol; col++)
2013         {
2014             xpos += m_owner->GetColumnWidth( col );
2015             m_column = col;
2016 
2017             if ( (abs(x-xpos) < 3) && (y < 22) )
2018             {
2019                 // near the column border
2020                 hit_border = true;
2021                 break;
2022             }
2023 
2024             if ( x < xpos )
2025             {
2026                 // inside the column
2027                 break;
2028             }
2029 
2030             m_minX = xpos;
2031         }
2032 
2033         if ( col == countCol )
2034             m_column = -1;
2035 
2036         if (event.LeftDown() || event.RightUp())
2037         {
2038             if (hit_border && event.LeftDown())
2039             {
2040                 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG,
2041                                    event.GetPosition()) )
2042                 {
2043                     m_isDragging = true;
2044                     m_currentX = x;
2045                     CaptureMouse();
2046                     DrawCurrent();
2047                 }
2048                 //else: column resizing was vetoed by the user code
2049             }
2050             else // click on a column
2051             {
2052                 // record the selected state of the columns
2053                 if (event.LeftDown())
2054                 {
2055                     for (int i=0; i < m_owner->GetColumnCount(); i++)
2056                     {
2057                         wxListItem colItem;
2058                         m_owner->GetColumn(i, colItem);
2059                         long state = colItem.GetState();
2060                         if (i == m_column)
2061                             colItem.SetState(state | wxLIST_STATE_SELECTED);
2062                         else
2063                             colItem.SetState(state & ~wxLIST_STATE_SELECTED);
2064                         m_owner->SetColumn(i, colItem);
2065                     }
2066                 }
2067 
2068                 SendListEvent( event.LeftDown()
2069                                     ? wxEVT_COMMAND_LIST_COL_CLICK
2070                                     : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK,
2071                                 event.GetPosition());
2072             }
2073         }
2074         else if (event.Moving())
2075         {
2076             bool setCursor;
2077             if (hit_border)
2078             {
2079                 setCursor = m_currentCursor == wxSTANDARD_CURSOR;
2080                 m_currentCursor = m_resizeCursor;
2081             }
2082             else
2083             {
2084                 setCursor = m_currentCursor != wxSTANDARD_CURSOR;
2085                 m_currentCursor = wxSTANDARD_CURSOR;
2086             }
2087 
2088             if ( setCursor )
2089                 SetCursor(*m_currentCursor);
2090         }
2091     }
2092 }
2093 
OnSetFocus(wxFocusEvent & WXUNUSED (event))2094 void wxListHeaderWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
2095 {
2096     m_owner->SetFocus();
2097     m_owner->Update();
2098 }
2099 
SendListEvent(wxEventType type,const wxPoint & pos)2100 bool wxListHeaderWindow::SendListEvent(wxEventType type, const wxPoint& pos)
2101 {
2102     wxWindow *parent = GetParent();
2103     wxListEvent le( type, parent->GetId() );
2104     le.SetEventObject( parent );
2105     le.m_pointDrag = pos;
2106 
2107     // the position should be relative to the parent window, not
2108     // this one for compatibility with MSW and common sense: the
2109     // user code doesn't know anything at all about this header
2110     // window, so why should it get positions relative to it?
2111     le.m_pointDrag.y -= GetSize().y;
2112 
2113     le.m_col = m_column;
2114     return !parent->GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
2115 }
2116 
2117 //-----------------------------------------------------------------------------
2118 // wxListRenameTimer (internal)
2119 //-----------------------------------------------------------------------------
2120 
wxListRenameTimer(wxListMainWindow * owner)2121 wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
2122 {
2123     m_owner = owner;
2124 }
2125 
Notify()2126 void wxListRenameTimer::Notify()
2127 {
2128     m_owner->OnRenameTimer();
2129 }
2130 
2131 //-----------------------------------------------------------------------------
2132 // wxListTextCtrlWrapper (internal)
2133 //-----------------------------------------------------------------------------
2134 
BEGIN_EVENT_TABLE(wxListTextCtrlWrapper,wxEvtHandler)2135 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper, wxEvtHandler)
2136     EVT_CHAR           (wxListTextCtrlWrapper::OnChar)
2137     EVT_KEY_UP         (wxListTextCtrlWrapper::OnKeyUp)
2138     EVT_KILL_FOCUS     (wxListTextCtrlWrapper::OnKillFocus)
2139 END_EVENT_TABLE()
2140 
2141 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow *owner,
2142                                              wxTextCtrl *text,
2143                                              size_t itemEdit)
2144               : m_startValue(owner->GetItemText(itemEdit)),
2145                 m_itemEdited(itemEdit)
2146 {
2147     m_owner = owner;
2148     m_text = text;
2149     m_finished = false;
2150     m_aboutToFinish = false;
2151 
2152     wxRect rectLabel = owner->GetLineLabelRect(itemEdit);
2153 
2154     m_owner->CalcScrolledPosition(rectLabel.x, rectLabel.y,
2155                                   &rectLabel.x, &rectLabel.y);
2156 
2157     m_text->Create(owner, wxID_ANY, m_startValue,
2158                    wxPoint(rectLabel.x-4,rectLabel.y-4),
2159                    wxSize(rectLabel.width+11,rectLabel.height+8));
2160     m_text->SetFocus();
2161 
2162     m_text->PushEventHandler(this);
2163 }
2164 
Finish()2165 void wxListTextCtrlWrapper::Finish()
2166 {
2167     if ( !m_finished )
2168     {
2169         m_finished = true;
2170 
2171         m_text->RemoveEventHandler(this);
2172         m_owner->FinishEditing(m_text);
2173 
2174         wxPendingDelete.Append( this );
2175     }
2176 }
2177 
AcceptChanges()2178 bool wxListTextCtrlWrapper::AcceptChanges()
2179 {
2180     const wxString value = m_text->GetValue();
2181 
2182     // notice that we should always call OnRenameAccept() to generate the "end
2183     // label editing" event, even if the user hasn't really changed anything
2184     if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
2185     {
2186         // vetoed by the user
2187         return false;
2188     }
2189 
2190     // accepted, do rename the item (unless nothing changed)
2191     if ( value != m_startValue )
2192         m_owner->SetItemText(m_itemEdited, value);
2193 
2194     return true;
2195 }
2196 
AcceptChangesAndFinish()2197 void wxListTextCtrlWrapper::AcceptChangesAndFinish()
2198 {
2199     m_aboutToFinish = true;
2200 
2201     // Notify the owner about the changes
2202     AcceptChanges();
2203 
2204     // Even if vetoed, close the control (consistent with MSW)
2205     Finish();
2206 }
2207 
OnChar(wxKeyEvent & event)2208 void wxListTextCtrlWrapper::OnChar( wxKeyEvent &event )
2209 {
2210     switch ( event.m_keyCode )
2211     {
2212         case WXK_RETURN:
2213             AcceptChangesAndFinish();
2214             break;
2215 
2216         case WXK_ESCAPE:
2217             m_owner->OnRenameCancelled( m_itemEdited );
2218             Finish();
2219             break;
2220 
2221         default:
2222             event.Skip();
2223     }
2224 }
2225 
OnKeyUp(wxKeyEvent & event)2226 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
2227 {
2228     if (m_finished)
2229     {
2230         event.Skip();
2231         return;
2232     }
2233 
2234     // auto-grow the textctrl:
2235     wxSize parentSize = m_owner->GetSize();
2236     wxPoint myPos = m_text->GetPosition();
2237     wxSize mySize = m_text->GetSize();
2238     int sx, sy;
2239     m_text->GetTextExtent(m_text->GetValue() + _T("MM"), &sx, &sy);
2240     if (myPos.x + sx > parentSize.x)
2241         sx = parentSize.x - myPos.x;
2242     if (mySize.x > sx)
2243         sx = mySize.x;
2244     m_text->SetSize(sx, wxDefaultCoord);
2245 
2246     event.Skip();
2247 }
2248 
OnKillFocus(wxFocusEvent & event)2249 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
2250 {
2251     if ( !m_finished && !m_aboutToFinish )
2252     {
2253         if ( !AcceptChanges() )
2254             m_owner->OnRenameCancelled( m_itemEdited );
2255 
2256         Finish();
2257     }
2258 
2259     // We must let the native text control handle focus
2260     event.Skip();
2261 }
2262 
2263 //-----------------------------------------------------------------------------
2264 //  wxListMainWindow
2265 //-----------------------------------------------------------------------------
2266 
IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow,wxScrolledWindow)2267 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow,wxScrolledWindow)
2268 
2269 BEGIN_EVENT_TABLE(wxListMainWindow,wxScrolledWindow)
2270   EVT_PAINT          (wxListMainWindow::OnPaint)
2271   EVT_MOUSE_EVENTS   (wxListMainWindow::OnMouse)
2272   EVT_CHAR           (wxListMainWindow::OnChar)
2273   EVT_KEY_DOWN       (wxListMainWindow::OnKeyDown)
2274   EVT_KEY_UP         (wxListMainWindow::OnKeyUp)
2275   EVT_SET_FOCUS      (wxListMainWindow::OnSetFocus)
2276   EVT_KILL_FOCUS     (wxListMainWindow::OnKillFocus)
2277   EVT_SCROLLWIN      (wxListMainWindow::OnScroll)
2278   EVT_CHILD_FOCUS    (wxListMainWindow::OnChildFocus)
2279 END_EVENT_TABLE()
2280 
2281 void wxListMainWindow::Init()
2282 {
2283     m_dirty = true;
2284     m_countVirt = 0;
2285     m_lineFrom =
2286     m_lineTo = (size_t)-1;
2287     m_linesPerPage = 0;
2288 
2289     m_headerWidth =
2290     m_lineHeight = 0;
2291 
2292     m_small_image_list = (wxImageList *) NULL;
2293     m_normal_image_list = (wxImageList *) NULL;
2294 
2295     m_small_spacing = 30;
2296     m_normal_spacing = 40;
2297 
2298     m_hasFocus = false;
2299     m_dragCount = 0;
2300     m_isCreated = false;
2301 
2302     m_lastOnSame = false;
2303     m_renameTimer = new wxListRenameTimer( this );
2304     m_textctrlWrapper = NULL;
2305 
2306     m_current =
2307     m_lineLastClicked =
2308     m_lineSelectSingleOnUp =
2309     m_lineBeforeLastClicked = (size_t)-1;
2310 
2311     m_freezeCount = 0;
2312 }
2313 
wxListMainWindow()2314 wxListMainWindow::wxListMainWindow()
2315 {
2316     Init();
2317 
2318     m_highlightBrush =
2319     m_highlightUnfocusedBrush = (wxBrush *) NULL;
2320 }
2321 
wxListMainWindow(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,long style,const wxString & name)2322 wxListMainWindow::wxListMainWindow( wxWindow *parent,
2323                                     wxWindowID id,
2324                                     const wxPoint& pos,
2325                                     const wxSize& size,
2326                                     long style,
2327                                     const wxString &name )
2328                 : wxScrolledWindow( parent, id, pos, size,
2329                                     style | wxHSCROLL | wxVSCROLL, name )
2330 {
2331     Init();
2332 
2333     m_highlightBrush = new wxBrush
2334                          (
2335                             wxSystemSettings::GetColour
2336                             (
2337                                 wxSYS_COLOUR_HIGHLIGHT
2338                             ),
2339                             wxSOLID
2340                          );
2341 
2342     m_highlightUnfocusedBrush = new wxBrush
2343                               (
2344                                  wxSystemSettings::GetColour
2345                                  (
2346                                      wxSYS_COLOUR_BTNSHADOW
2347                                  ),
2348                                  wxSOLID
2349                               );
2350 
2351     SetScrollbars( 0, 0, 0, 0, 0, 0 );
2352 
2353     wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes();
2354     SetOwnForegroundColour( attr.colFg );
2355     SetOwnBackgroundColour( attr.colBg );
2356     if (!m_hasFont)
2357         SetOwnFont( attr.font );
2358 }
2359 
~wxListMainWindow()2360 wxListMainWindow::~wxListMainWindow()
2361 {
2362     DoDeleteAllItems();
2363     WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
2364     WX_CLEAR_ARRAY(m_aColWidths);
2365 
2366     delete m_highlightBrush;
2367     delete m_highlightUnfocusedBrush;
2368     delete m_renameTimer;
2369 }
2370 
CacheLineData(size_t line)2371 void wxListMainWindow::CacheLineData(size_t line)
2372 {
2373     wxGenericListCtrl *listctrl = GetListCtrl();
2374 
2375     wxListLineData *ld = GetDummyLine();
2376 
2377     size_t countCol = GetColumnCount();
2378     for ( size_t col = 0; col < countCol; col++ )
2379     {
2380         ld->SetText(col, listctrl->OnGetItemText(line, col));
2381         ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col));
2382     }
2383 
2384     ld->SetAttr(listctrl->OnGetItemAttr(line));
2385 }
2386 
GetDummyLine() const2387 wxListLineData *wxListMainWindow::GetDummyLine() const
2388 {
2389     wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2390     wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2391 
2392     wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
2393 
2394     // we need to recreate the dummy line if the number of columns in the
2395     // control changed as it would have the incorrect number of fields
2396     // otherwise
2397     if ( !m_lines.IsEmpty() &&
2398             m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() )
2399     {
2400         self->m_lines.Clear();
2401     }
2402 
2403     if ( m_lines.IsEmpty() )
2404     {
2405         wxListLineData *line = new wxListLineData(self);
2406         self->m_lines.Add(line);
2407 
2408         // don't waste extra memory -- there never going to be anything
2409         // else/more in this array
2410         self->m_lines.Shrink();
2411     }
2412 
2413     return &m_lines[0];
2414 }
2415 
2416 // ----------------------------------------------------------------------------
2417 // line geometry (report mode only)
2418 // ----------------------------------------------------------------------------
2419 
GetLineHeight() const2420 wxCoord wxListMainWindow::GetLineHeight() const
2421 {
2422     // we cache the line height as calling GetTextExtent() is slow
2423     if ( !m_lineHeight )
2424     {
2425         wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
2426 
2427         wxClientDC dc( self );
2428         dc.SetFont( GetFont() );
2429 
2430         wxCoord y;
2431         dc.GetTextExtent(_T("H"), NULL, &y);
2432 
2433         if ( m_small_image_list && m_small_image_list->GetImageCount() )
2434         {
2435             int iw = 0, ih = 0;
2436             m_small_image_list->GetSize(0, iw, ih);
2437             y = wxMax(y, ih);
2438         }
2439 
2440         y += EXTRA_HEIGHT;
2441         self->m_lineHeight = y + LINE_SPACING;
2442     }
2443 
2444     return m_lineHeight;
2445 }
2446 
GetLineY(size_t line) const2447 wxCoord wxListMainWindow::GetLineY(size_t line) const
2448 {
2449     wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2450 
2451     return LINE_SPACING + line * GetLineHeight();
2452 }
2453 
GetLineRect(size_t line) const2454 wxRect wxListMainWindow::GetLineRect(size_t line) const
2455 {
2456     if ( !InReportView() )
2457         return GetLine(line)->m_gi->m_rectAll;
2458 
2459     wxRect rect;
2460     rect.x = HEADER_OFFSET_X;
2461     rect.y = GetLineY(line);
2462     rect.width = GetHeaderWidth();
2463     rect.height = GetLineHeight();
2464 
2465     return rect;
2466 }
2467 
GetLineLabelRect(size_t line) const2468 wxRect wxListMainWindow::GetLineLabelRect(size_t line) const
2469 {
2470     if ( !InReportView() )
2471         return GetLine(line)->m_gi->m_rectLabel;
2472 
2473     int image_x = 0;
2474     wxListLineData *data = GetLine(line);
2475     wxListItemDataList::compatibility_iterator node = data->m_items.GetFirst();
2476     if (node)
2477     {
2478         wxListItemData *item = node->GetData();
2479         if ( item->HasImage() )
2480         {
2481             int ix, iy;
2482             GetImageSize( item->GetImage(), ix, iy );
2483             image_x = 3 + ix + IMAGE_MARGIN_IN_REPORT_MODE;
2484         }
2485     }
2486 
2487     wxRect rect;
2488     rect.x = image_x + HEADER_OFFSET_X;
2489     rect.y = GetLineY(line);
2490     rect.width = GetColumnWidth(0) - image_x;
2491     rect.height = GetLineHeight();
2492 
2493     return rect;
2494 }
2495 
GetLineIconRect(size_t line) const2496 wxRect wxListMainWindow::GetLineIconRect(size_t line) const
2497 {
2498     if ( !InReportView() )
2499         return GetLine(line)->m_gi->m_rectIcon;
2500 
2501     wxListLineData *ld = GetLine(line);
2502     wxASSERT_MSG( ld->HasImage(), _T("should have an image") );
2503 
2504     wxRect rect;
2505     rect.x = HEADER_OFFSET_X;
2506     rect.y = GetLineY(line);
2507     GetImageSize(ld->GetImage(), rect.width, rect.height);
2508 
2509     return rect;
2510 }
2511 
GetLineHighlightRect(size_t line) const2512 wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const
2513 {
2514     return InReportView() ? GetLineRect(line)
2515                           : GetLine(line)->m_gi->m_rectHighlight;
2516 }
2517 
HitTestLine(size_t line,int x,int y) const2518 long wxListMainWindow::HitTestLine(size_t line, int x, int y) const
2519 {
2520     wxASSERT_MSG( line < GetItemCount(), _T("invalid line in HitTestLine") );
2521 
2522     wxListLineData *ld = GetLine(line);
2523 
2524     if ( ld->HasImage() && GetLineIconRect(line).Contains(x, y) )
2525         return wxLIST_HITTEST_ONITEMICON;
2526 
2527     // VS: Testing for "ld->HasText() || InReportView()" instead of
2528     //     "ld->HasText()" is needed to make empty lines in report view
2529     //     possible
2530     if ( ld->HasText() || InReportView() )
2531     {
2532         wxRect rect = InReportView() ? GetLineRect(line)
2533                                      : GetLineLabelRect(line);
2534 
2535         if ( rect.Contains(x, y) )
2536             return wxLIST_HITTEST_ONITEMLABEL;
2537     }
2538 
2539     return 0;
2540 }
2541 
2542 // ----------------------------------------------------------------------------
2543 // highlight (selection) handling
2544 // ----------------------------------------------------------------------------
2545 
IsHighlighted(size_t line) const2546 bool wxListMainWindow::IsHighlighted(size_t line) const
2547 {
2548     if ( IsVirtual() )
2549     {
2550         return m_selStore.IsSelected(line);
2551     }
2552     else // !virtual
2553     {
2554         wxListLineData *ld = GetLine(line);
2555         wxCHECK_MSG( ld, false, _T("invalid index in IsHighlighted") );
2556 
2557         return ld->IsHighlighted();
2558     }
2559 }
2560 
HighlightLines(size_t lineFrom,size_t lineTo,bool highlight)2561 void wxListMainWindow::HighlightLines( size_t lineFrom,
2562                                        size_t lineTo,
2563                                        bool highlight )
2564 {
2565     if ( IsVirtual() )
2566     {
2567         wxArrayInt linesChanged;
2568         if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight,
2569                                      &linesChanged) )
2570         {
2571             // meny items changed state, refresh everything
2572             RefreshLines(lineFrom, lineTo);
2573         }
2574         else // only a few items changed state, refresh only them
2575         {
2576             size_t count = linesChanged.GetCount();
2577             for ( size_t n = 0; n < count; n++ )
2578             {
2579                 RefreshLine(linesChanged[n]);
2580             }
2581         }
2582     }
2583     else // iterate over all items in non report view
2584     {
2585         for ( size_t line = lineFrom; line <= lineTo; line++ )
2586         {
2587             if ( HighlightLine(line, highlight) )
2588                 RefreshLine(line);
2589         }
2590     }
2591 }
2592 
HighlightLine(size_t line,bool highlight)2593 bool wxListMainWindow::HighlightLine( size_t line, bool highlight )
2594 {
2595     bool changed;
2596 
2597     if ( IsVirtual() )
2598     {
2599         changed = m_selStore.SelectItem(line, highlight);
2600     }
2601     else // !virtual
2602     {
2603         wxListLineData *ld = GetLine(line);
2604         wxCHECK_MSG( ld, false, _T("invalid index in HighlightLine") );
2605 
2606         changed = ld->Highlight(highlight);
2607     }
2608 
2609     if ( changed )
2610     {
2611         SendNotify( line, highlight ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2612                                     : wxEVT_COMMAND_LIST_ITEM_DESELECTED );
2613     }
2614 
2615     return changed;
2616 }
2617 
RefreshLine(size_t line)2618 void wxListMainWindow::RefreshLine( size_t line )
2619 {
2620     if ( InReportView() )
2621     {
2622         size_t visibleFrom, visibleTo;
2623         GetVisibleLinesRange(&visibleFrom, &visibleTo);
2624 
2625         if ( line < visibleFrom || line > visibleTo )
2626             return;
2627     }
2628 
2629     wxRect rect = GetLineRect(line);
2630 
2631     CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2632     RefreshRect( rect );
2633 }
2634 
RefreshLines(size_t lineFrom,size_t lineTo)2635 void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo )
2636 {
2637     // we suppose that they are ordered by caller
2638     wxASSERT_MSG( lineFrom <= lineTo, _T("indices in disorder") );
2639 
2640     wxASSERT_MSG( lineTo < GetItemCount(), _T("invalid line range") );
2641 
2642     if ( InReportView() )
2643     {
2644         size_t visibleFrom, visibleTo;
2645         GetVisibleLinesRange(&visibleFrom, &visibleTo);
2646 
2647         if ( lineFrom < visibleFrom )
2648             lineFrom = visibleFrom;
2649         if ( lineTo > visibleTo )
2650             lineTo = visibleTo;
2651 
2652         wxRect rect;
2653         rect.x = 0;
2654         rect.y = GetLineY(lineFrom);
2655         rect.width = GetClientSize().x;
2656         rect.height = GetLineY(lineTo) - rect.y + GetLineHeight();
2657 
2658         CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2659         RefreshRect( rect );
2660     }
2661     else // !report
2662     {
2663         // TODO: this should be optimized...
2664         for ( size_t line = lineFrom; line <= lineTo; line++ )
2665         {
2666             RefreshLine(line);
2667         }
2668     }
2669 }
2670 
RefreshAfter(size_t lineFrom)2671 void wxListMainWindow::RefreshAfter( size_t lineFrom )
2672 {
2673     if ( InReportView() )
2674     {
2675         size_t visibleFrom, visibleTo;
2676         GetVisibleLinesRange(&visibleFrom, &visibleTo);
2677 
2678         if ( lineFrom < visibleFrom )
2679             lineFrom = visibleFrom;
2680         else if ( lineFrom > visibleTo )
2681             return;
2682 
2683         wxRect rect;
2684         rect.x = 0;
2685         rect.y = GetLineY(lineFrom);
2686         CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2687 
2688         wxSize size = GetClientSize();
2689         rect.width = size.x;
2690 
2691         // refresh till the bottom of the window
2692         rect.height = size.y - rect.y;
2693 
2694         RefreshRect( rect );
2695     }
2696     else // !report
2697     {
2698         // TODO: how to do it more efficiently?
2699         m_dirty = true;
2700     }
2701 }
2702 
RefreshSelected()2703 void wxListMainWindow::RefreshSelected()
2704 {
2705     if ( IsEmpty() )
2706         return;
2707 
2708     size_t from, to;
2709     if ( InReportView() )
2710     {
2711         GetVisibleLinesRange(&from, &to);
2712     }
2713     else // !virtual
2714     {
2715         from = 0;
2716         to = GetItemCount() - 1;
2717     }
2718 
2719     if ( HasCurrent() && m_current >= from && m_current <= to )
2720         RefreshLine(m_current);
2721 
2722     for ( size_t line = from; line <= to; line++ )
2723     {
2724         // NB: the test works as expected even if m_current == -1
2725         if ( line != m_current && IsHighlighted(line) )
2726             RefreshLine(line);
2727     }
2728 }
2729 
Freeze()2730 void wxListMainWindow::Freeze()
2731 {
2732     m_freezeCount++;
2733 }
2734 
Thaw()2735 void wxListMainWindow::Thaw()
2736 {
2737     wxCHECK_RET( m_freezeCount > 0, _T("thawing unfrozen list control?") );
2738 
2739     if ( --m_freezeCount == 0 )
2740     {
2741         if (m_dirty)
2742             RecalculatePositions();
2743         else
2744             Refresh();
2745     }
2746 }
2747 
OnPaint(wxPaintEvent & WXUNUSED (event))2748 void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2749 {
2750     // Note: a wxPaintDC must be constructed even if no drawing is
2751     // done (a Windows requirement).
2752     wxPaintDC dc( this );
2753 
2754     if ( IsEmpty() || m_freezeCount )
2755         // nothing to draw or not the moment to draw it
2756         return;
2757 
2758     if ( m_dirty )
2759         // delay the repainting until we calculate all the items positions
2760         return;
2761 
2762     PrepareDC( dc );
2763 
2764     int dev_x, dev_y;
2765     CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
2766 
2767     dc.SetFont( GetFont() );
2768 
2769     if ( InReportView() )
2770     {
2771         int lineHeight = GetLineHeight();
2772 
2773         size_t visibleFrom, visibleTo;
2774         GetVisibleLinesRange(&visibleFrom, &visibleTo);
2775 
2776         wxRect rectLine;
2777         int xOrig = dc.LogicalToDeviceX( 0 );
2778         int yOrig = dc.LogicalToDeviceY( 0 );
2779 
2780         // tell the caller cache to cache the data
2781         if ( IsVirtual() )
2782         {
2783             wxListEvent evCache(wxEVT_COMMAND_LIST_CACHE_HINT,
2784                                 GetParent()->GetId());
2785             evCache.SetEventObject( GetParent() );
2786             evCache.m_oldItemIndex = visibleFrom;
2787             evCache.m_itemIndex = visibleTo;
2788             GetParent()->GetEventHandler()->ProcessEvent( evCache );
2789         }
2790 
2791         for ( size_t line = visibleFrom; line <= visibleTo; line++ )
2792         {
2793             rectLine = GetLineRect(line);
2794 
2795 
2796             if ( !IsExposed(rectLine.x + xOrig, rectLine.y + yOrig,
2797                             rectLine.width, rectLine.height) )
2798             {
2799                 // don't redraw unaffected lines to avoid flicker
2800                 continue;
2801             }
2802 
2803             GetLine(line)->DrawInReportMode( &dc,
2804                                              rectLine,
2805                                              GetLineHighlightRect(line),
2806                                              IsHighlighted(line) );
2807         }
2808 
2809         if ( HasFlag(wxLC_HRULES) )
2810         {
2811             wxPen pen(GetRuleColour(), 1, wxSOLID);
2812             wxSize clientSize = GetClientSize();
2813 
2814             size_t i = visibleFrom;
2815             if (i == 0) i = 1; // Don't draw the first one
2816             for ( ; i <= visibleTo; i++ )
2817             {
2818                 dc.SetPen(pen);
2819                 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2820                 dc.DrawLine(0 - dev_x, i * lineHeight,
2821                             clientSize.x - dev_x, i * lineHeight);
2822             }
2823 
2824             // Draw last horizontal rule
2825             if ( visibleTo == GetItemCount() - 1 )
2826             {
2827                 dc.SetPen( pen );
2828                 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2829                 dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight,
2830                             clientSize.x - dev_x , (m_lineTo + 1) * lineHeight );
2831             }
2832         }
2833 
2834         // Draw vertical rules if required
2835         if ( HasFlag(wxLC_VRULES) && !IsEmpty() )
2836         {
2837             wxPen pen(GetRuleColour(), 1, wxSOLID);
2838             wxRect firstItemRect, lastItemRect;
2839 
2840             GetItemRect(visibleFrom, firstItemRect);
2841             GetItemRect(visibleTo, lastItemRect);
2842             int x = firstItemRect.GetX();
2843             dc.SetPen(pen);
2844             dc.SetBrush(* wxTRANSPARENT_BRUSH);
2845 
2846             for (int col = 0; col < GetColumnCount(); col++)
2847             {
2848                 int colWidth = GetColumnWidth(col);
2849                 x += colWidth;
2850                 int x_pos = x - dev_x;
2851                 if (col < GetColumnCount()-1) x_pos -= 2;
2852                 dc.DrawLine(x_pos, firstItemRect.GetY() - 1 - dev_y,
2853                             x_pos, lastItemRect.GetBottom() + 1 - dev_y);
2854             }
2855         }
2856     }
2857     else // !report
2858     {
2859         size_t count = GetItemCount();
2860         for ( size_t i = 0; i < count; i++ )
2861         {
2862             GetLine(i)->Draw( &dc );
2863         }
2864     }
2865 
2866 #ifndef __WXMAC__
2867     // Don't draw rect outline under Mac at all.
2868     if ( HasCurrent() )
2869     {
2870         if ( m_hasFocus )
2871         {
2872             wxRect rect( GetLineHighlightRect( m_current ) );
2873 #ifndef __WXGTK20__
2874             dc.SetPen( *wxBLACK_PEN );
2875             dc.SetBrush( *wxTRANSPARENT_BRUSH );
2876             dc.DrawRectangle( rect );
2877 #else
2878             wxRendererNative::Get().DrawItemSelectionRect( this, dc, rect, wxCONTROL_CURRENT|wxCONTROL_FOCUSED );
2879 
2880 #endif
2881         }
2882     }
2883 #endif
2884 }
2885 
HighlightAll(bool on)2886 void wxListMainWindow::HighlightAll( bool on )
2887 {
2888     if ( IsSingleSel() )
2889     {
2890         wxASSERT_MSG( !on, _T("can't do this in a single selection control") );
2891 
2892         // we just have one item to turn off
2893         if ( HasCurrent() && IsHighlighted(m_current) )
2894         {
2895             HighlightLine(m_current, false);
2896             RefreshLine(m_current);
2897         }
2898     }
2899     else // multi selection
2900     {
2901         if ( !IsEmpty() )
2902             HighlightLines(0, GetItemCount() - 1, on);
2903     }
2904 }
2905 
OnChildFocus(wxChildFocusEvent & WXUNUSED (event))2906 void wxListMainWindow::OnChildFocus(wxChildFocusEvent& WXUNUSED(event))
2907 {
2908     // Do nothing here.  This prevents the default handler in wxScrolledWindow
2909     // from needlessly scrolling the window when the edit control is
2910     // dismissed.  See ticket #9563.
2911 }
2912 
SendNotify(size_t line,wxEventType command,const wxPoint & point)2913 void wxListMainWindow::SendNotify( size_t line,
2914                                    wxEventType command,
2915                                    const wxPoint& point )
2916 {
2917     wxListEvent le( command, GetParent()->GetId() );
2918     le.SetEventObject( GetParent() );
2919 
2920     le.m_itemIndex = line;
2921 
2922     // set only for events which have position
2923     if ( point != wxDefaultPosition )
2924         le.m_pointDrag = point;
2925 
2926     // don't try to get the line info for virtual list controls: the main
2927     // program has it anyhow and if we did it would result in accessing all
2928     // the lines, even those which are not visible now and this is precisely
2929     // what we're trying to avoid
2930     if ( !IsVirtual() )
2931     {
2932         if ( line != (size_t)-1 )
2933         {
2934             GetLine(line)->GetItem( 0, le.m_item );
2935         }
2936         //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2937     }
2938     //else: there may be no more such item
2939 
2940     GetParent()->GetEventHandler()->ProcessEvent( le );
2941 }
2942 
ChangeCurrent(size_t current)2943 void wxListMainWindow::ChangeCurrent(size_t current)
2944 {
2945     m_current = current;
2946 
2947     // as the current item changed, we shouldn't start editing it when the
2948     // "slow click" timer expires as the click happened on another item
2949     if ( m_renameTimer->IsRunning() )
2950         m_renameTimer->Stop();
2951 
2952     SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
2953 }
2954 
EditLabel(long item,wxClassInfo * textControlClass)2955 wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
2956 {
2957     wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
2958                  wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2959 
2960     wxASSERT_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)),
2961                  wxT("EditLabel() needs a text control") );
2962 
2963     size_t itemEdit = (size_t)item;
2964 
2965     wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
2966     le.SetEventObject( GetParent() );
2967     le.m_itemIndex = item;
2968     wxListLineData *data = GetLine(itemEdit);
2969     wxCHECK_MSG( data, NULL, _T("invalid index in EditLabel()") );
2970     data->GetItem( 0, le.m_item );
2971 
2972     if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
2973     {
2974         // vetoed by user code
2975         return NULL;
2976     }
2977 
2978     // We have to call this here because the label in question might just have
2979     // been added and no screen update taken place.
2980     if ( m_dirty )
2981     {
2982         wxSafeYield();
2983 
2984         // Pending events dispatched by wxSafeYield might have changed the item
2985         // count
2986         if ( (size_t)item >= GetItemCount() )
2987             return NULL;
2988     }
2989 
2990     wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
2991     m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
2992     return m_textctrlWrapper->GetText();
2993 }
2994 
OnRenameTimer()2995 void wxListMainWindow::OnRenameTimer()
2996 {
2997     wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2998 
2999     EditLabel( m_current );
3000 }
3001 
OnRenameAccept(size_t itemEdit,const wxString & value)3002 bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
3003 {
3004     wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
3005     le.SetEventObject( GetParent() );
3006     le.m_itemIndex = itemEdit;
3007 
3008     wxListLineData *data = GetLine(itemEdit);
3009 
3010     wxCHECK_MSG( data, false, _T("invalid index in OnRenameAccept()") );
3011 
3012     data->GetItem( 0, le.m_item );
3013     le.m_item.m_text = value;
3014     return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
3015                 le.IsAllowed();
3016 }
3017 
OnRenameCancelled(size_t itemEdit)3018 void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
3019 {
3020     // let owner know that the edit was cancelled
3021     wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
3022 
3023     le.SetEditCanceled(true);
3024 
3025     le.SetEventObject( GetParent() );
3026     le.m_itemIndex = itemEdit;
3027 
3028     wxListLineData *data = GetLine(itemEdit);
3029     wxCHECK_RET( data, _T("invalid index in OnRenameCancelled()") );
3030 
3031     data->GetItem( 0, le.m_item );
3032     GetEventHandler()->ProcessEvent( le );
3033 }
3034 
OnMouse(wxMouseEvent & event)3035 void wxListMainWindow::OnMouse( wxMouseEvent &event )
3036 {
3037 
3038 #ifdef __WXMAC__
3039     // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
3040     // shutdown the edit control when the mouse is clicked elsewhere on the
3041     // listctrl because the order of events is different (or something like
3042     // that), so explicitly end the edit if it is active.
3043     if ( event.LeftDown() && m_textctrlWrapper )
3044         m_textctrlWrapper->AcceptChangesAndFinish();
3045 #endif // __WXMAC__
3046 
3047     if ( event.LeftDown() )
3048         SetFocusIgnoringChildren();
3049 
3050     event.SetEventObject( GetParent() );
3051     if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3052         return;
3053 
3054     if (event.GetEventType() == wxEVT_MOUSEWHEEL)
3055     {
3056         // let the base handle mouse wheel events.
3057         event.Skip();
3058         return;
3059     }
3060 
3061     if ( !HasCurrent() || IsEmpty() )
3062     {
3063         if (event.RightDown())
3064         {
3065             SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3066             // Allow generation of context menu event
3067             event.Skip();
3068         }
3069         return;
3070     }
3071 
3072     if (m_dirty)
3073         return;
3074 
3075     if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
3076         event.ButtonDClick()) )
3077         return;
3078 
3079     int x = event.GetX();
3080     int y = event.GetY();
3081     CalcUnscrolledPosition( x, y, &x, &y );
3082 
3083     // where did we hit it (if we did)?
3084     long hitResult = 0;
3085 
3086     size_t count = GetItemCount(),
3087            current;
3088 
3089     if ( InReportView() )
3090     {
3091         current = y / GetLineHeight();
3092         if ( current < count )
3093             hitResult = HitTestLine(current, x, y);
3094     }
3095     else // !report
3096     {
3097         // TODO: optimize it too! this is less simple than for report view but
3098         //       enumerating all items is still not a way to do it!!
3099         for ( current = 0; current < count; current++ )
3100         {
3101             hitResult = HitTestLine(current, x, y);
3102             if ( hitResult )
3103                 break;
3104         }
3105     }
3106 
3107     if (event.Dragging())
3108     {
3109         if (m_dragCount == 0)
3110         {
3111             // we have to report the raw, physical coords as we want to be
3112             // able to call HitTest(event.m_pointDrag) from the user code to
3113             // get the item being dragged
3114             m_dragStart = event.GetPosition();
3115         }
3116 
3117         m_dragCount++;
3118 
3119         if (m_dragCount != 3)
3120             return;
3121 
3122         int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3123                                           : wxEVT_COMMAND_LIST_BEGIN_DRAG;
3124 
3125         wxListEvent le( command, GetParent()->GetId() );
3126         le.SetEventObject( GetParent() );
3127         le.m_itemIndex = m_lineLastClicked;
3128         le.m_pointDrag = m_dragStart;
3129         GetParent()->GetEventHandler()->ProcessEvent( le );
3130 
3131         return;
3132     }
3133     else
3134     {
3135         m_dragCount = 0;
3136     }
3137 
3138     if ( !hitResult )
3139     {
3140         // outside of any item
3141         if (event.RightDown())
3142         {
3143             SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3144 
3145             wxContextMenuEvent evtCtx(
3146                 wxEVT_CONTEXT_MENU,
3147                 GetParent()->GetId(),
3148                 ClientToScreen(event.GetPosition()));
3149             evtCtx.SetEventObject(GetParent());
3150             GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
3151         }
3152         else
3153         {
3154             // reset the selection and bail out
3155             HighlightAll(false);
3156         }
3157 
3158         return;
3159     }
3160 
3161     bool forceClick = false;
3162     if (event.ButtonDClick())
3163     {
3164         if ( m_renameTimer->IsRunning() )
3165             m_renameTimer->Stop();
3166 
3167         m_lastOnSame = false;
3168 
3169         if ( current == m_lineLastClicked )
3170         {
3171             SendNotify( current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3172 
3173             return;
3174         }
3175         else
3176         {
3177             // The first click was on another item, so don't interpret this as
3178             // a double click, but as a simple click instead
3179             forceClick = true;
3180         }
3181     }
3182 
3183     if (event.LeftUp())
3184     {
3185         if (m_lineSelectSingleOnUp != (size_t)-1)
3186         {
3187             // select single line
3188             HighlightAll( false );
3189             ReverseHighlight(m_lineSelectSingleOnUp);
3190         }
3191 
3192         if (m_lastOnSame)
3193         {
3194             if ((current == m_current) &&
3195                 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
3196                 HasFlag(wxLC_EDIT_LABELS) )
3197             {
3198                 if (InReportView())
3199                 {
3200                     wxRect label = GetLineLabelRect( current );
3201                     if (label.Contains( x, y ))
3202                         m_renameTimer->Start( 250, true );
3203 
3204                 }
3205                 else
3206                     m_renameTimer->Start( 250, true );
3207             }
3208         }
3209 
3210         m_lastOnSame = false;
3211         m_lineSelectSingleOnUp = (size_t)-1;
3212     }
3213     else
3214     {
3215         // This is necessary, because after a DnD operation in
3216         // from and to ourself, the up event is swallowed by the
3217         // DnD code. So on next non-up event (which means here and
3218         // now) m_lineSelectSingleOnUp should be reset.
3219         m_lineSelectSingleOnUp = (size_t)-1;
3220     }
3221     if (event.RightDown())
3222     {
3223         m_lineBeforeLastClicked = m_lineLastClicked;
3224         m_lineLastClicked = current;
3225 
3226         // If the item is already selected, do not update the selection.
3227         // Multi-selections should not be cleared if a selected item is clicked.
3228         if (!IsHighlighted(current))
3229         {
3230             HighlightAll(false);
3231             ChangeCurrent(current);
3232             ReverseHighlight(m_current);
3233         }
3234 
3235         SendNotify( current, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3236 
3237         wxContextMenuEvent evtCtx(
3238                 wxEVT_CONTEXT_MENU,
3239                 GetParent()->GetId(),
3240                 ClientToScreen(event.GetPosition()));
3241         evtCtx.SetEventObject(GetParent());
3242         GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
3243     }
3244     else if (event.MiddleDown())
3245     {
3246         SendNotify( current, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
3247     }
3248     else if ( event.LeftDown() || forceClick )
3249     {
3250         m_lineBeforeLastClicked = m_lineLastClicked;
3251         m_lineLastClicked = current;
3252 
3253         size_t oldCurrent = m_current;
3254         bool oldWasSelected = IsHighlighted(m_current);
3255 
3256         bool cmdModifierDown = event.CmdDown();
3257         if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
3258         {
3259             if ( IsSingleSel() || !IsHighlighted(current) )
3260             {
3261                 HighlightAll( false );
3262 
3263                 ChangeCurrent(current);
3264 
3265                 ReverseHighlight(m_current);
3266             }
3267             else // multi sel & current is highlighted & no mod keys
3268             {
3269                 m_lineSelectSingleOnUp = current;
3270                 ChangeCurrent(current); // change focus
3271             }
3272         }
3273         else // multi sel & either ctrl or shift is down
3274         {
3275             if (cmdModifierDown)
3276             {
3277                 ChangeCurrent(current);
3278 
3279                 ReverseHighlight(m_current);
3280             }
3281             else if (event.ShiftDown())
3282             {
3283                 ChangeCurrent(current);
3284 
3285                 size_t lineFrom = oldCurrent,
3286                        lineTo = current;
3287 
3288                 if ( lineTo < lineFrom )
3289                 {
3290                     lineTo = lineFrom;
3291                     lineFrom = m_current;
3292                 }
3293 
3294                 HighlightLines(lineFrom, lineTo);
3295             }
3296             else // !ctrl, !shift
3297             {
3298                 // test in the enclosing if should make it impossible
3299                 wxFAIL_MSG( _T("how did we get here?") );
3300             }
3301         }
3302 
3303         if (m_current != oldCurrent)
3304             RefreshLine( oldCurrent );
3305 
3306         // forceClick is only set if the previous click was on another item
3307         m_lastOnSame = !forceClick && (m_current == oldCurrent) && oldWasSelected;
3308     }
3309 }
3310 
MoveToItem(size_t item)3311 void wxListMainWindow::MoveToItem(size_t item)
3312 {
3313     if ( item == (size_t)-1 )
3314         return;
3315 
3316     wxRect rect = GetLineRect(item);
3317 
3318     int client_w, client_h;
3319     GetClientSize( &client_w, &client_h );
3320 
3321     const int hLine = GetLineHeight();
3322 
3323     int view_x = SCROLL_UNIT_X * GetScrollPos( wxHORIZONTAL );
3324     int view_y = hLine * GetScrollPos( wxVERTICAL );
3325 
3326     if ( InReportView() )
3327     {
3328         // the next we need the range of lines shown it might be different,
3329         // so recalculate it
3330         ResetVisibleLinesRange();
3331 
3332         if (rect.y < view_y)
3333             Scroll( -1, rect.y / hLine );
3334         if (rect.y + rect.height + 5 > view_y + client_h)
3335             Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
3336 
3337 #ifdef __WXMAC__
3338         // At least on Mac the visible lines value will get reset inside of
3339         // Scroll *before* it actually scrolls the window because of the
3340         // Update() that happens there, so it will still have the wrong value.
3341         // So let's reset it again and wait for it to be recalculated in the
3342         // next paint event.  I would expect this problem to show up in wxGTK
3343         // too but couldn't duplicate it there.  Perhaps the order of events
3344         // is different...  --Robin
3345         ResetVisibleLinesRange();
3346 #endif
3347     }
3348     else // !report
3349     {
3350         int sx = -1,
3351             sy = -1;
3352 
3353         if (rect.x-view_x < 5)
3354             sx = (rect.x - 5) / SCROLL_UNIT_X;
3355         if (rect.x + rect.width - 5 > view_x + client_w)
3356             sx = (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X;
3357 
3358         if (rect.y-view_y < 5)
3359             sy = (rect.y - 5) / hLine;
3360         if (rect.y + rect.height - 5 > view_y + client_h)
3361             sy = (rect.y + rect.height - client_h + hLine) / hLine;
3362 
3363         Scroll(sx, sy);
3364     }
3365 }
3366 
ScrollList(int WXUNUSED (dx),int dy)3367 bool wxListMainWindow::ScrollList(int WXUNUSED(dx), int dy)
3368 {
3369     if ( !InReportView() )
3370     {
3371         // TODO: this should work in all views but is not implemented now
3372         return false;
3373     }
3374 
3375     size_t top, bottom;
3376     GetVisibleLinesRange(&top, &bottom);
3377 
3378     if ( bottom == (size_t)-1 )
3379         return 0;
3380 
3381     ResetVisibleLinesRange();
3382 
3383     int hLine = GetLineHeight();
3384 
3385     Scroll(-1, top + dy / hLine);
3386 
3387 #ifdef __WXMAC__
3388     // see comment in MoveToItem() for why we do this
3389     ResetVisibleLinesRange();
3390 #endif
3391 
3392     return true;
3393 }
3394 
3395 // ----------------------------------------------------------------------------
3396 // keyboard handling
3397 // ----------------------------------------------------------------------------
3398 
OnArrowChar(size_t newCurrent,const wxKeyEvent & event)3399 void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
3400 {
3401     wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
3402                  _T("invalid item index in OnArrowChar()") );
3403 
3404     size_t oldCurrent = m_current;
3405 
3406     // in single selection we just ignore Shift as we can't select several
3407     // items anyhow
3408     if ( event.ShiftDown() && !IsSingleSel() )
3409     {
3410         ChangeCurrent(newCurrent);
3411 
3412         // refresh the old focus to remove it
3413         RefreshLine( oldCurrent );
3414 
3415         // select all the items between the old and the new one
3416         if ( oldCurrent > newCurrent )
3417         {
3418             newCurrent = oldCurrent;
3419             oldCurrent = m_current;
3420         }
3421 
3422         HighlightLines(oldCurrent, newCurrent);
3423     }
3424     else // !shift
3425     {
3426         // all previously selected items are unselected unless ctrl is held
3427         // in a multiselection control
3428         if ( !event.ControlDown() || IsSingleSel() )
3429             HighlightAll(false);
3430 
3431         ChangeCurrent(newCurrent);
3432 
3433         // refresh the old focus to remove it
3434         RefreshLine( oldCurrent );
3435 
3436         // in single selection mode we must always have a selected item
3437         if ( !event.ControlDown() || IsSingleSel() )
3438             HighlightLine( m_current, true );
3439     }
3440 
3441     RefreshLine( m_current );
3442 
3443     MoveToFocus();
3444 }
3445 
OnKeyDown(wxKeyEvent & event)3446 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
3447 {
3448     wxWindow *parent = GetParent();
3449 
3450     // propagate the key event upwards
3451     wxKeyEvent ke(event);
3452     if (parent->GetEventHandler()->ProcessEvent( ke ))
3453         return;
3454 
3455     event.Skip();
3456 }
3457 
OnKeyUp(wxKeyEvent & event)3458 void wxListMainWindow::OnKeyUp( wxKeyEvent &event )
3459 {
3460     wxWindow *parent = GetParent();
3461 
3462     // propagate the key event upwards
3463     wxKeyEvent ke(event);
3464     ke.SetEventObject( parent );
3465     if (parent->GetEventHandler()->ProcessEvent( ke ))
3466         return;
3467 
3468     event.Skip();
3469 }
3470 
OnChar(wxKeyEvent & event)3471 void wxListMainWindow::OnChar( wxKeyEvent &event )
3472 {
3473     wxWindow *parent = GetParent();
3474 
3475     // send a list_key event up
3476     if ( HasCurrent() )
3477     {
3478         wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() );
3479         le.m_itemIndex = m_current;
3480         GetLine(m_current)->GetItem( 0, le.m_item );
3481         le.m_code = event.GetKeyCode();
3482         le.SetEventObject( parent );
3483         parent->GetEventHandler()->ProcessEvent( le );
3484     }
3485 
3486     // propagate the char event upwards
3487     wxKeyEvent ke(event);
3488     if (parent->GetEventHandler()->ProcessEvent( ke ))
3489         return;
3490 
3491     if (event.GetKeyCode() == WXK_TAB)
3492     {
3493         wxNavigationKeyEvent nevent;
3494         nevent.SetWindowChange( event.ControlDown() );
3495         nevent.SetDirection( !event.ShiftDown() );
3496         nevent.SetEventObject( GetParent()->GetParent() );
3497         nevent.SetCurrentFocus( m_parent );
3498         if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent ))
3499             return;
3500     }
3501 
3502     // no item -> nothing to do
3503     if (!HasCurrent())
3504     {
3505         event.Skip();
3506         return;
3507     }
3508 
3509     // don't use m_linesPerPage directly as it might not be computed yet
3510     const int pageSize = GetCountPerPage();
3511     wxCHECK_RET( pageSize, _T("should have non zero page size") );
3512 
3513     if (GetLayoutDirection() == wxLayout_RightToLeft)
3514     {
3515         if (event.GetKeyCode() == WXK_RIGHT)
3516             event.m_keyCode = WXK_LEFT;
3517         else if (event.GetKeyCode() == WXK_LEFT)
3518             event.m_keyCode = WXK_RIGHT;
3519     }
3520 
3521     switch ( event.GetKeyCode() )
3522     {
3523         case WXK_UP:
3524             if ( m_current > 0 )
3525                 OnArrowChar( m_current - 1, event );
3526             break;
3527 
3528         case WXK_DOWN:
3529             if ( m_current < (size_t)GetItemCount() - 1 )
3530                 OnArrowChar( m_current + 1, event );
3531             break;
3532 
3533         case WXK_END:
3534             if (!IsEmpty())
3535                 OnArrowChar( GetItemCount() - 1, event );
3536             break;
3537 
3538         case WXK_HOME:
3539             if (!IsEmpty())
3540                 OnArrowChar( 0, event );
3541             break;
3542 
3543         case WXK_PAGEUP:
3544             {
3545                 int steps = InReportView() ? pageSize - 1
3546                                            : m_current % pageSize;
3547 
3548                 int index = m_current - steps;
3549                 if (index < 0)
3550                     index = 0;
3551 
3552                 OnArrowChar( index, event );
3553             }
3554             break;
3555 
3556         case WXK_PAGEDOWN:
3557             {
3558                 int steps = InReportView()
3559                                 ? pageSize - 1
3560                                 : pageSize - (m_current % pageSize) - 1;
3561 
3562                 size_t index = m_current + steps;
3563                 size_t count = GetItemCount();
3564                 if ( index >= count )
3565                     index = count - 1;
3566 
3567                 OnArrowChar( index, event );
3568             }
3569             break;
3570 
3571         case WXK_LEFT:
3572             if ( !InReportView() )
3573             {
3574                 int index = m_current - pageSize;
3575                 if (index < 0)
3576                     index = 0;
3577 
3578                 OnArrowChar( index, event );
3579             }
3580             break;
3581 
3582         case WXK_RIGHT:
3583             if ( !InReportView() )
3584             {
3585                 size_t index = m_current + pageSize;
3586 
3587                 size_t count = GetItemCount();
3588                 if ( index >= count )
3589                     index = count - 1;
3590 
3591                 OnArrowChar( index, event );
3592             }
3593             break;
3594 
3595         case WXK_SPACE:
3596             if ( IsSingleSel() )
3597             {
3598                 if ( event.ControlDown() )
3599                 {
3600                     ReverseHighlight(m_current);
3601                 }
3602                 else // normal space press
3603                 {
3604                     SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3605                 }
3606             }
3607             else // multiple selection
3608             {
3609                 ReverseHighlight(m_current);
3610             }
3611             break;
3612 
3613         case WXK_RETURN:
3614         case WXK_EXECUTE:
3615             SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3616             break;
3617 
3618         default:
3619             event.Skip();
3620     }
3621 }
3622 
3623 // ----------------------------------------------------------------------------
3624 // focus handling
3625 // ----------------------------------------------------------------------------
3626 
OnSetFocus(wxFocusEvent & WXUNUSED (event))3627 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
3628 {
3629     if ( GetParent() )
3630     {
3631         wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
3632         event.SetEventObject( GetParent() );
3633         if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3634             return;
3635     }
3636 
3637     // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3638     // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3639     // which are already drawn correctly resulting in horrible flicker - avoid
3640     // it
3641     if ( !m_hasFocus )
3642     {
3643         m_hasFocus = true;
3644 
3645         RefreshSelected();
3646     }
3647 }
3648 
OnKillFocus(wxFocusEvent & WXUNUSED (event))3649 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
3650 {
3651     if ( GetParent() )
3652     {
3653         wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
3654         event.SetEventObject( GetParent() );
3655         if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3656             return;
3657     }
3658 
3659     m_hasFocus = false;
3660     RefreshSelected();
3661 }
3662 
DrawImage(int index,wxDC * dc,int x,int y)3663 void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
3664 {
3665     if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
3666     {
3667         m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3668     }
3669     else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
3670     {
3671         m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3672     }
3673     else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
3674     {
3675         m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3676     }
3677     else if ( InReportView() && (m_small_image_list))
3678     {
3679         m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3680     }
3681 }
3682 
GetImageSize(int index,int & width,int & height) const3683 void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
3684 {
3685     if ( HasFlag(wxLC_ICON) && m_normal_image_list )
3686     {
3687         m_normal_image_list->GetSize( index, width, height );
3688     }
3689     else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
3690     {
3691         m_small_image_list->GetSize( index, width, height );
3692     }
3693     else if ( HasFlag(wxLC_LIST) && m_small_image_list )
3694     {
3695         m_small_image_list->GetSize( index, width, height );
3696     }
3697     else if ( InReportView() && m_small_image_list )
3698     {
3699         m_small_image_list->GetSize( index, width, height );
3700     }
3701     else
3702     {
3703         width =
3704         height = 0;
3705     }
3706 }
3707 
GetTextLength(const wxString & s) const3708 int wxListMainWindow::GetTextLength( const wxString &s ) const
3709 {
3710     wxClientDC dc( wxConstCast(this, wxListMainWindow) );
3711     dc.SetFont( GetFont() );
3712 
3713     wxCoord lw;
3714     dc.GetTextExtent( s, &lw, NULL );
3715 
3716     return lw + AUTOSIZE_COL_MARGIN;
3717 }
3718 
SetImageList(wxImageList * imageList,int which)3719 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
3720 {
3721     m_dirty = true;
3722 
3723     // calc the spacing from the icon size
3724     int width = 0, height = 0;
3725 
3726     if ((imageList) && (imageList->GetImageCount()) )
3727         imageList->GetSize(0, width, height);
3728 
3729     if (which == wxIMAGE_LIST_NORMAL)
3730     {
3731         m_normal_image_list = imageList;
3732         m_normal_spacing = width + 8;
3733     }
3734 
3735     if (which == wxIMAGE_LIST_SMALL)
3736     {
3737         m_small_image_list = imageList;
3738         m_small_spacing = width + 14;
3739         m_lineHeight = 0;  // ensure that the line height will be recalc'd
3740     }
3741 }
3742 
SetItemSpacing(int spacing,bool isSmall)3743 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
3744 {
3745     m_dirty = true;
3746     if (isSmall)
3747         m_small_spacing = spacing;
3748     else
3749         m_normal_spacing = spacing;
3750 }
3751 
GetItemSpacing(bool isSmall)3752 int wxListMainWindow::GetItemSpacing( bool isSmall )
3753 {
3754     return isSmall ? m_small_spacing : m_normal_spacing;
3755 }
3756 
3757 // ----------------------------------------------------------------------------
3758 // columns
3759 // ----------------------------------------------------------------------------
3760 
SetColumn(int col,wxListItem & item)3761 void wxListMainWindow::SetColumn( int col, wxListItem &item )
3762 {
3763     wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3764 
3765     wxCHECK_RET( node, _T("invalid column index in SetColumn") );
3766 
3767     if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
3768         item.m_width = GetTextLength( item.m_text );
3769 
3770     wxListHeaderData *column = node->GetData();
3771     column->SetItem( item );
3772 
3773     wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3774     if ( headerWin )
3775         headerWin->m_dirty = true;
3776 
3777     m_dirty = true;
3778 
3779     // invalidate it as it has to be recalculated
3780     m_headerWidth = 0;
3781 }
3782 
SetColumnWidth(int col,int width)3783 void wxListMainWindow::SetColumnWidth( int col, int width )
3784 {
3785     wxCHECK_RET( col >= 0 && col < GetColumnCount(),
3786                  _T("invalid column index") );
3787 
3788     wxCHECK_RET( InReportView(),
3789                  _T("SetColumnWidth() can only be called in report mode.") );
3790 
3791     m_dirty = true;
3792     wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3793     if ( headerWin )
3794         headerWin->m_dirty = true;
3795 
3796     wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3797     wxCHECK_RET( node, _T("no column?") );
3798 
3799     wxListHeaderData *column = node->GetData();
3800 
3801     size_t count = GetItemCount();
3802 
3803     if (width == wxLIST_AUTOSIZE_USEHEADER)
3804     {
3805         width = GetTextLength(column->GetText());
3806         width += 2*EXTRA_WIDTH;
3807 
3808         // check for column header's image availability
3809         const int image = column->GetImage();
3810         if ( image != -1 )
3811         {
3812             if ( m_small_image_list )
3813             {
3814                 int ix = 0, iy = 0;
3815                 m_small_image_list->GetSize(image, ix, iy);
3816                 width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
3817             }
3818         }
3819     }
3820     else if ( width == wxLIST_AUTOSIZE )
3821     {
3822         if ( IsVirtual() )
3823         {
3824             // TODO: determine the max width somehow...
3825             width = WIDTH_COL_DEFAULT;
3826         }
3827         else // !virtual
3828         {
3829             wxClientDC dc(this);
3830             dc.SetFont( GetFont() );
3831 
3832             int max = AUTOSIZE_COL_MARGIN;
3833 
3834             //  if the cached column width isn't valid then recalculate it
3835             if (m_aColWidths.Item(col)->bNeedsUpdate)
3836             {
3837                 for (size_t i = 0; i < count; i++)
3838                 {
3839                     wxListLineData *line = GetLine( i );
3840                     wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3841 
3842                     wxCHECK_RET( n, _T("no subitem?") );
3843 
3844                     wxListItemData *itemData = n->GetData();
3845                     wxListItem      item;
3846 
3847                     itemData->GetItem(item);
3848                     int itemWidth = GetItemWidthWithImage(&item);
3849                     if (itemWidth > max)
3850                         max = itemWidth;
3851                 }
3852 
3853                 m_aColWidths.Item(col)->bNeedsUpdate = false;
3854                 m_aColWidths.Item(col)->nMaxWidth = max;
3855             }
3856 
3857             max = m_aColWidths.Item(col)->nMaxWidth;
3858             width = max + AUTOSIZE_COL_MARGIN;
3859         }
3860     }
3861 
3862     column->SetWidth( width );
3863 
3864     // invalidate it as it has to be recalculated
3865     m_headerWidth = 0;
3866 }
3867 
GetHeaderWidth() const3868 int wxListMainWindow::GetHeaderWidth() const
3869 {
3870     if ( !m_headerWidth )
3871     {
3872         wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
3873 
3874         size_t count = GetColumnCount();
3875         for ( size_t col = 0; col < count; col++ )
3876         {
3877             self->m_headerWidth += GetColumnWidth(col);
3878         }
3879     }
3880 
3881     return m_headerWidth;
3882 }
3883 
GetColumn(int col,wxListItem & item) const3884 void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
3885 {
3886     wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3887     wxCHECK_RET( node, _T("invalid column index in GetColumn") );
3888 
3889     wxListHeaderData *column = node->GetData();
3890     column->GetItem( item );
3891 }
3892 
GetColumnWidth(int col) const3893 int wxListMainWindow::GetColumnWidth( int col ) const
3894 {
3895     wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3896     wxCHECK_MSG( node, 0, _T("invalid column index") );
3897 
3898     wxListHeaderData *column = node->GetData();
3899     return column->GetWidth();
3900 }
3901 
3902 // ----------------------------------------------------------------------------
3903 // item state
3904 // ----------------------------------------------------------------------------
3905 
SetItem(wxListItem & item)3906 void wxListMainWindow::SetItem( wxListItem &item )
3907 {
3908     long id = item.m_itemId;
3909     wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
3910                  _T("invalid item index in SetItem") );
3911 
3912     if ( !IsVirtual() )
3913     {
3914         wxListLineData *line = GetLine((size_t)id);
3915         line->SetItem( item.m_col, item );
3916 
3917         // Set item state if user wants
3918         if ( item.m_mask & wxLIST_MASK_STATE )
3919             SetItemState( item.m_itemId, item.m_state, item.m_state );
3920 
3921         if (InReportView())
3922         {
3923             //  update the Max Width Cache if needed
3924             int width = GetItemWidthWithImage(&item);
3925 
3926             if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
3927                 m_aColWidths.Item(item.m_col)->nMaxWidth = width;
3928         }
3929     }
3930 
3931     // update the item on screen
3932     wxRect rectItem;
3933     GetItemRect(id, rectItem);
3934     RefreshRect(rectItem);
3935 }
3936 
SetItemStateAll(long state,long stateMask)3937 void wxListMainWindow::SetItemStateAll(long state, long stateMask)
3938 {
3939     if ( IsEmpty() )
3940         return;
3941 
3942     // first deal with selection
3943     if ( stateMask & wxLIST_STATE_SELECTED )
3944     {
3945         // set/clear select state
3946         if ( IsVirtual() )
3947         {
3948             // optimized version for virtual listctrl.
3949             m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
3950             Refresh();
3951         }
3952         else if ( state & wxLIST_STATE_SELECTED )
3953         {
3954             const long count = GetItemCount();
3955             for( long i = 0; i <  count; i++ )
3956             {
3957                 SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
3958             }
3959 
3960         }
3961         else
3962         {
3963             // clear for non virtual (somewhat optimized by using GetNextItem())
3964             long i = -1;
3965             while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
3966             {
3967                 SetItemState( i, 0, wxLIST_STATE_SELECTED );
3968             }
3969         }
3970     }
3971 
3972     if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
3973     {
3974         // unfocus all: only one item can be focussed, so clearing focus for
3975         // all items is simply clearing focus of the focussed item.
3976         SetItemState(m_current, state, stateMask);
3977     }
3978     //(setting focus to all items makes no sense, so it is not handled here.)
3979 }
3980 
SetItemState(long litem,long state,long stateMask)3981 void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
3982 {
3983     if ( litem == -1 )
3984     {
3985         SetItemStateAll(state, stateMask);
3986         return;
3987     }
3988 
3989     wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
3990                  _T("invalid list ctrl item index in SetItem") );
3991 
3992     size_t oldCurrent = m_current;
3993     size_t item = (size_t)litem;    // safe because of the check above
3994 
3995     // do we need to change the focus?
3996     if ( stateMask & wxLIST_STATE_FOCUSED )
3997     {
3998         if ( state & wxLIST_STATE_FOCUSED )
3999         {
4000             // don't do anything if this item is already focused
4001             if ( item != m_current )
4002             {
4003                 ChangeCurrent(item);
4004 
4005                 if ( oldCurrent != (size_t)-1 )
4006                 {
4007                     if ( IsSingleSel() )
4008                     {
4009                         HighlightLine(oldCurrent, false);
4010                     }
4011 
4012                     RefreshLine(oldCurrent);
4013                 }
4014 
4015                 RefreshLine( m_current );
4016             }
4017         }
4018         else // unfocus
4019         {
4020             // don't do anything if this item is not focused
4021             if ( item == m_current )
4022             {
4023                 ResetCurrent();
4024 
4025                 if ( IsSingleSel() )
4026                 {
4027                     // we must unselect the old current item as well or we
4028                     // might end up with more than one selected item in a
4029                     // single selection control
4030                     HighlightLine(oldCurrent, false);
4031                 }
4032 
4033                 RefreshLine( oldCurrent );
4034             }
4035         }
4036     }
4037 
4038     // do we need to change the selection state?
4039     if ( stateMask & wxLIST_STATE_SELECTED )
4040     {
4041         bool on = (state & wxLIST_STATE_SELECTED) != 0;
4042 
4043         if ( IsSingleSel() )
4044         {
4045             if ( on )
4046             {
4047                 // selecting the item also makes it the focused one in the
4048                 // single sel mode
4049                 if ( m_current != item )
4050                 {
4051                     ChangeCurrent(item);
4052 
4053                     if ( oldCurrent != (size_t)-1 )
4054                     {
4055                         HighlightLine( oldCurrent, false );
4056                         RefreshLine( oldCurrent );
4057                     }
4058                 }
4059             }
4060             else // off
4061             {
4062                 // only the current item may be selected anyhow
4063                 if ( item != m_current )
4064                     return;
4065             }
4066         }
4067 
4068         if ( HighlightLine(item, on) )
4069         {
4070             RefreshLine(item);
4071         }
4072     }
4073 }
4074 
GetItemState(long item,long stateMask) const4075 int wxListMainWindow::GetItemState( long item, long stateMask ) const
4076 {
4077     wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
4078                  _T("invalid list ctrl item index in GetItemState()") );
4079 
4080     int ret = wxLIST_STATE_DONTCARE;
4081 
4082     if ( stateMask & wxLIST_STATE_FOCUSED )
4083     {
4084         if ( (size_t)item == m_current )
4085             ret |= wxLIST_STATE_FOCUSED;
4086     }
4087 
4088     if ( stateMask & wxLIST_STATE_SELECTED )
4089     {
4090         if ( IsHighlighted(item) )
4091             ret |= wxLIST_STATE_SELECTED;
4092     }
4093 
4094     return ret;
4095 }
4096 
GetItem(wxListItem & item) const4097 void wxListMainWindow::GetItem( wxListItem &item ) const
4098 {
4099     wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
4100                  _T("invalid item index in GetItem") );
4101 
4102     wxListLineData *line = GetLine((size_t)item.m_itemId);
4103     line->GetItem( item.m_col, item );
4104 
4105     // Get item state if user wants it
4106     if ( item.m_mask & wxLIST_MASK_STATE )
4107         item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
4108                                                  wxLIST_STATE_FOCUSED );
4109 }
4110 
4111 // ----------------------------------------------------------------------------
4112 // item count
4113 // ----------------------------------------------------------------------------
4114 
GetItemCount() const4115 size_t wxListMainWindow::GetItemCount() const
4116 {
4117     return IsVirtual() ? m_countVirt : m_lines.GetCount();
4118 }
4119 
SetItemCount(long count)4120 void wxListMainWindow::SetItemCount(long count)
4121 {
4122     m_selStore.SetItemCount(count);
4123     m_countVirt = count;
4124 
4125     ResetVisibleLinesRange();
4126 
4127     // scrollbars must be reset
4128     m_dirty = true;
4129 }
4130 
GetSelectedItemCount() const4131 int wxListMainWindow::GetSelectedItemCount() const
4132 {
4133     // deal with the quick case first
4134     if ( IsSingleSel() )
4135         return HasCurrent() ? IsHighlighted(m_current) : false;
4136 
4137     // virtual controls remmebers all its selections itself
4138     if ( IsVirtual() )
4139         return m_selStore.GetSelectedCount();
4140 
4141     // TODO: we probably should maintain the number of items selected even for
4142     //       non virtual controls as enumerating all lines is really slow...
4143     size_t countSel = 0;
4144     size_t count = GetItemCount();
4145     for ( size_t line = 0; line < count; line++ )
4146     {
4147         if ( GetLine(line)->IsHighlighted() )
4148             countSel++;
4149     }
4150 
4151     return countSel;
4152 }
4153 
4154 // ----------------------------------------------------------------------------
4155 // item position/size
4156 // ----------------------------------------------------------------------------
4157 
GetViewRect() const4158 wxRect wxListMainWindow::GetViewRect() const
4159 {
4160     wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
4161                     _T("wxListCtrl::GetViewRect() only works in icon mode") );
4162 
4163     // we need to find the longest/tallest label
4164     wxCoord xMax = 0, yMax = 0;
4165     const int count = GetItemCount();
4166     if ( count )
4167     {
4168         for ( int i = 0; i < count; i++ )
4169         {
4170             // we need logical, not physical, coordinates here, so use
4171             // GetLineRect() instead of GetItemRect()
4172             wxRect r = GetLineRect(i);
4173 
4174             wxCoord x = r.GetRight(),
4175                     y = r.GetBottom();
4176 
4177             if ( x > xMax )
4178                 xMax = x;
4179             if ( y > yMax )
4180                 yMax = y;
4181         }
4182     }
4183 
4184     // some fudge needed to make it look prettier
4185     xMax += 2 * EXTRA_BORDER_X;
4186     yMax += 2 * EXTRA_BORDER_Y;
4187 
4188     // account for the scrollbars if necessary
4189     const wxSize sizeAll = GetClientSize();
4190     if ( xMax > sizeAll.x )
4191         yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
4192     if ( yMax > sizeAll.y )
4193         xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
4194 
4195     return wxRect(0, 0, xMax, yMax);
4196 }
4197 
GetItemRect(long index,wxRect & rect) const4198 void wxListMainWindow::GetItemRect( long index, wxRect &rect ) const
4199 {
4200     wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4201                  _T("invalid index in GetItemRect") );
4202 
4203     // ensure that we're laid out, otherwise we could return nonsense
4204     if ( m_dirty )
4205     {
4206         wxConstCast(this, wxListMainWindow)->
4207             RecalculatePositions(true /* no refresh */);
4208     }
4209 
4210     rect = GetLineRect((size_t)index);
4211 
4212     CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
4213 }
4214 
GetItemPosition(long item,wxPoint & pos) const4215 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
4216 {
4217     wxRect rect;
4218     GetItemRect(item, rect);
4219 
4220     pos.x = rect.x;
4221     pos.y = rect.y;
4222 
4223     return true;
4224 }
4225 
4226 // ----------------------------------------------------------------------------
4227 // geometry calculation
4228 // ----------------------------------------------------------------------------
4229 
RecalculatePositions(bool noRefresh)4230 void wxListMainWindow::RecalculatePositions(bool noRefresh)
4231 {
4232     const int lineHeight = GetLineHeight();
4233 
4234     wxClientDC dc( this );
4235     dc.SetFont( GetFont() );
4236 
4237     const size_t count = GetItemCount();
4238 
4239     int iconSpacing;
4240     if ( HasFlag(wxLC_ICON) && m_normal_image_list )
4241         iconSpacing = m_normal_spacing;
4242     else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
4243         iconSpacing = m_small_spacing;
4244     else
4245         iconSpacing = 0;
4246 
4247     // Note that we do not call GetClientSize() here but
4248     // GetSize() and subtract the border size for sunken
4249     // borders manually. This is technically incorrect,
4250     // but we need to know the client area's size WITHOUT
4251     // scrollbars here. Since we don't know if there are
4252     // any scrollbars, we use GetSize() instead. Another
4253     // solution would be to call SetScrollbars() here to
4254     // remove the scrollbars and call GetClientSize() then,
4255     // but this might result in flicker and - worse - will
4256     // reset the scrollbars to 0 which is not good at all
4257     // if you resize a dialog/window, but don't want to
4258     // reset the window scrolling. RR.
4259     // Furthermore, we actually do NOT subtract the border
4260     // width as 2 pixels is just the extra space which we
4261     // need around the actual content in the window. Other-
4262     // wise the text would e.g. touch the upper border. RR.
4263     int clientWidth,
4264         clientHeight;
4265     GetSize( &clientWidth, &clientHeight );
4266 
4267     if ( InReportView() )
4268     {
4269         // all lines have the same height and we scroll one line per step
4270         int entireHeight = count * lineHeight + LINE_SPACING;
4271 
4272         m_linesPerPage = clientHeight / lineHeight;
4273 
4274         ResetVisibleLinesRange();
4275 
4276         SetScrollbars( SCROLL_UNIT_X, lineHeight,
4277                        GetHeaderWidth() / SCROLL_UNIT_X,
4278                        (entireHeight + lineHeight - 1) / lineHeight,
4279                        GetScrollPos(wxHORIZONTAL),
4280                        GetScrollPos(wxVERTICAL),
4281                        true );
4282     }
4283     else // !report
4284     {
4285         // we have 3 different layout strategies: either layout all items
4286         // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4287         // to arrange them in top to bottom, left to right (don't ask me why
4288         // not the other way round...) order
4289         if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) )
4290         {
4291             int x = EXTRA_BORDER_X;
4292             int y = EXTRA_BORDER_Y;
4293 
4294             wxCoord widthMax = 0;
4295 
4296             size_t i;
4297             for ( i = 0; i < count; i++ )
4298             {
4299                 wxListLineData *line = GetLine(i);
4300                 line->CalculateSize( &dc, iconSpacing );
4301                 line->SetPosition( x, y, iconSpacing );
4302 
4303                 wxSize sizeLine = GetLineSize(i);
4304 
4305                 if ( HasFlag(wxLC_ALIGN_TOP) )
4306                 {
4307                     if ( sizeLine.x > widthMax )
4308                         widthMax = sizeLine.x;
4309 
4310                     y += sizeLine.y;
4311                 }
4312                 else // wxLC_ALIGN_LEFT
4313                 {
4314                     x += sizeLine.x + MARGIN_BETWEEN_ROWS;
4315                 }
4316             }
4317 
4318             if ( HasFlag(wxLC_ALIGN_TOP) )
4319             {
4320                 // traverse the items again and tweak their sizes so that they are
4321                 // all the same in a row
4322                 for ( i = 0; i < count; i++ )
4323                 {
4324                     wxListLineData *line = GetLine(i);
4325                     line->m_gi->ExtendWidth(widthMax);
4326                 }
4327             }
4328 
4329             SetScrollbars
4330             (
4331                 SCROLL_UNIT_X,
4332                 lineHeight,
4333                 (x + SCROLL_UNIT_X) / SCROLL_UNIT_X,
4334                 (y + lineHeight) / lineHeight,
4335                 GetScrollPos( wxHORIZONTAL ),
4336                 GetScrollPos( wxVERTICAL ),
4337                 true
4338             );
4339         }
4340         else // "flowed" arrangement, the most complicated case
4341         {
4342             // at first we try without any scrollbars, if the items don't fit into
4343             // the window, we recalculate after subtracting the space taken by the
4344             // scrollbar
4345 
4346             int entireWidth = 0;
4347 
4348             for (int tries = 0; tries < 2; tries++)
4349             {
4350                 entireWidth = 2 * EXTRA_BORDER_X;
4351 
4352                 if (tries == 1)
4353                 {
4354                     // Now we have decided that the items do not fit into the
4355                     // client area, so we need a scrollbar
4356                     entireWidth += SCROLL_UNIT_X;
4357                 }
4358 
4359                 int x = EXTRA_BORDER_X;
4360                 int y = EXTRA_BORDER_Y;
4361                 int maxWidthInThisRow = 0;
4362 
4363                 m_linesPerPage = 0;
4364                 int currentlyVisibleLines = 0;
4365 
4366                 for (size_t i = 0; i < count; i++)
4367                 {
4368                     currentlyVisibleLines++;
4369                     wxListLineData *line = GetLine( i );
4370                     line->CalculateSize( &dc, iconSpacing );
4371                     line->SetPosition( x, y, iconSpacing );
4372 
4373                     wxSize sizeLine = GetLineSize( i );
4374 
4375                     if ( maxWidthInThisRow < sizeLine.x )
4376                         maxWidthInThisRow = sizeLine.x;
4377 
4378                     y += sizeLine.y;
4379                     if (currentlyVisibleLines > m_linesPerPage)
4380                         m_linesPerPage = currentlyVisibleLines;
4381 
4382                     if ( y + sizeLine.y >= clientHeight )
4383                     {
4384                         currentlyVisibleLines = 0;
4385                         y = EXTRA_BORDER_Y;
4386                         maxWidthInThisRow += MARGIN_BETWEEN_ROWS;
4387                         x += maxWidthInThisRow;
4388                         entireWidth += maxWidthInThisRow;
4389                         maxWidthInThisRow = 0;
4390                     }
4391 
4392                     // We have reached the last item.
4393                     if ( i == count - 1 )
4394                         entireWidth += maxWidthInThisRow;
4395 
4396                     if ( (tries == 0) &&
4397                             (entireWidth + SCROLL_UNIT_X > clientWidth) )
4398                     {
4399                         clientHeight -= wxSystemSettings::
4400                                             GetMetric(wxSYS_HSCROLL_Y);
4401                         m_linesPerPage = 0;
4402                         break;
4403                     }
4404 
4405                     if ( i == count - 1 )
4406                         tries = 1;  // Everything fits, no second try required.
4407                 }
4408             }
4409 
4410             SetScrollbars
4411             (
4412                 SCROLL_UNIT_X,
4413                 lineHeight,
4414                 (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X,
4415                 0,
4416                 GetScrollPos( wxHORIZONTAL ),
4417                 0,
4418                 true
4419             );
4420         }
4421     }
4422 
4423     if ( !noRefresh )
4424     {
4425         // FIXME: why should we call it from here?
4426         UpdateCurrent();
4427 
4428         RefreshAll();
4429     }
4430 }
4431 
RefreshAll()4432 void wxListMainWindow::RefreshAll()
4433 {
4434     m_dirty = false;
4435     Refresh();
4436 
4437     wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
4438     if ( headerWin && headerWin->m_dirty )
4439     {
4440         headerWin->m_dirty = false;
4441         headerWin->Refresh();
4442     }
4443 }
4444 
UpdateCurrent()4445 void wxListMainWindow::UpdateCurrent()
4446 {
4447     if ( !HasCurrent() && !IsEmpty() )
4448         ChangeCurrent(0);
4449 }
4450 
GetNextItem(long item,int WXUNUSED (geometry),int state) const4451 long wxListMainWindow::GetNextItem( long item,
4452                                     int WXUNUSED(geometry),
4453                                     int state ) const
4454 {
4455     long ret = item,
4456          max = GetItemCount();
4457     wxCHECK_MSG( (ret == -1) || (ret < max), -1,
4458                  _T("invalid listctrl index in GetNextItem()") );
4459 
4460     // notice that we start with the next item (or the first one if item == -1)
4461     // and this is intentional to allow writing a simple loop to iterate over
4462     // all selected items
4463     ret++;
4464     if ( ret == max )
4465         // this is not an error because the index was OK initially,
4466         // just no such item
4467         return -1;
4468 
4469     if ( !state )
4470         // any will do
4471         return (size_t)ret;
4472 
4473     size_t count = GetItemCount();
4474     for ( size_t line = (size_t)ret; line < count; line++ )
4475     {
4476         if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
4477             return line;
4478 
4479         if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
4480             return line;
4481     }
4482 
4483     return -1;
4484 }
4485 
4486 // ----------------------------------------------------------------------------
4487 // deleting stuff
4488 // ----------------------------------------------------------------------------
4489 
DeleteItem(long lindex)4490 void wxListMainWindow::DeleteItem( long lindex )
4491 {
4492     size_t count = GetItemCount();
4493 
4494     wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
4495                  _T("invalid item index in DeleteItem") );
4496 
4497     size_t index = (size_t)lindex;
4498 
4499     // we don't need to adjust the index for the previous items
4500     if ( HasCurrent() && m_current >= index )
4501     {
4502         // if the current item is being deleted, we want the next one to
4503         // become selected - unless there is no next one - so don't adjust
4504         // m_current in this case
4505         if ( m_current != index || m_current == count - 1 )
4506             m_current--;
4507     }
4508 
4509     if ( InReportView() )
4510     {
4511         //  mark the Column Max Width cache as dirty if the items in the line
4512         //  we're deleting contain the Max Column Width
4513         wxListLineData * const line = GetLine(index);
4514         wxListItemDataList::compatibility_iterator n;
4515         wxListItemData *itemData;
4516         wxListItem      item;
4517         int             itemWidth;
4518 
4519         for (size_t i = 0; i < m_columns.GetCount(); i++)
4520         {
4521             n = line->m_items.Item( i );
4522             itemData = n->GetData();
4523             itemData->GetItem(item);
4524 
4525             itemWidth = GetItemWidthWithImage(&item);
4526 
4527             if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth)
4528                 m_aColWidths.Item(i)->bNeedsUpdate = true;
4529         }
4530 
4531         ResetVisibleLinesRange();
4532     }
4533 
4534     SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM, wxDefaultPosition );
4535 
4536     if ( IsVirtual() )
4537     {
4538         m_countVirt--;
4539         m_selStore.OnItemDelete(index);
4540     }
4541     else
4542     {
4543         m_lines.RemoveAt( index );
4544     }
4545 
4546     // we need to refresh the (vert) scrollbar as the number of items changed
4547     m_dirty = true;
4548 
4549     RefreshAfter(index);
4550 }
4551 
DeleteColumn(int col)4552 void wxListMainWindow::DeleteColumn( int col )
4553 {
4554     wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
4555 
4556     wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
4557 
4558     m_dirty = true;
4559     delete node->GetData();
4560     m_columns.Erase( node );
4561 
4562     if ( !IsVirtual() )
4563     {
4564         // update all the items
4565         for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4566         {
4567             wxListLineData * const line = GetLine(i);
4568             wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
4569             delete n->GetData();
4570             line->m_items.Erase(n);
4571         }
4572     }
4573 
4574     if ( InReportView() )   //  we only cache max widths when in Report View
4575     {
4576         delete m_aColWidths.Item(col);
4577         m_aColWidths.RemoveAt(col);
4578     }
4579 
4580     // invalidate it as it has to be recalculated
4581     m_headerWidth = 0;
4582 }
4583 
DoDeleteAllItems()4584 void wxListMainWindow::DoDeleteAllItems()
4585 {
4586     if ( IsEmpty() )
4587         // nothing to do - in particular, don't send the event
4588         return;
4589 
4590     ResetCurrent();
4591 
4592     // to make the deletion of all items faster, we don't send the
4593     // notifications for each item deletion in this case but only one event
4594     // for all of them: this is compatible with wxMSW and documented in
4595     // DeleteAllItems() description
4596 
4597     wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
4598     event.SetEventObject( GetParent() );
4599     GetParent()->GetEventHandler()->ProcessEvent( event );
4600 
4601     if ( IsVirtual() )
4602     {
4603         m_countVirt = 0;
4604         m_selStore.Clear();
4605     }
4606 
4607     if ( InReportView() )
4608     {
4609         ResetVisibleLinesRange();
4610         for (size_t i = 0; i < m_aColWidths.GetCount(); i++)
4611         {
4612             m_aColWidths.Item(i)->bNeedsUpdate = true;
4613         }
4614     }
4615 
4616     m_lines.Clear();
4617 }
4618 
DeleteAllItems()4619 void wxListMainWindow::DeleteAllItems()
4620 {
4621     DoDeleteAllItems();
4622 
4623     RecalculatePositions();
4624 }
4625 
DeleteEverything()4626 void wxListMainWindow::DeleteEverything()
4627 {
4628     WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
4629     WX_CLEAR_ARRAY(m_aColWidths);
4630 
4631     DeleteAllItems();
4632 }
4633 
4634 // ----------------------------------------------------------------------------
4635 // scanning for an item
4636 // ----------------------------------------------------------------------------
4637 
EnsureVisible(long index)4638 void wxListMainWindow::EnsureVisible( long index )
4639 {
4640     wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4641                  _T("invalid index in EnsureVisible") );
4642 
4643     // We have to call this here because the label in question might just have
4644     // been added and its position is not known yet
4645     if ( m_dirty )
4646         RecalculatePositions(true /* no refresh */);
4647 
4648     MoveToItem((size_t)index);
4649 }
4650 
FindItem(long start,const wxString & str,bool partial)4651 long wxListMainWindow::FindItem(long start, const wxString& str, bool partial )
4652 {
4653     if (str.empty())
4654         return wxNOT_FOUND;
4655 
4656     long pos = start;
4657     wxString str_upper = str.Upper();
4658     if (pos < 0)
4659         pos = 0;
4660 
4661     size_t count = GetItemCount();
4662     for ( size_t i = (size_t)pos; i < count; i++ )
4663     {
4664         wxListLineData *line = GetLine(i);
4665         wxString line_upper = line->GetText(0).Upper();
4666         if (!partial)
4667         {
4668             if (line_upper == str_upper )
4669                 return i;
4670         }
4671         else
4672         {
4673             if (line_upper.find(str_upper) == 0)
4674                 return i;
4675         }
4676     }
4677 
4678     return wxNOT_FOUND;
4679 }
4680 
FindItem(long start,wxUIntPtr data)4681 long wxListMainWindow::FindItem(long start, wxUIntPtr data)
4682 {
4683     long pos = start;
4684     if (pos < 0)
4685         pos = 0;
4686 
4687     size_t count = GetItemCount();
4688     for (size_t i = (size_t)pos; i < count; i++)
4689     {
4690         wxListLineData *line = GetLine(i);
4691         wxListItem item;
4692         line->GetItem( 0, item );
4693         if (item.m_data == data)
4694             return i;
4695     }
4696 
4697     return wxNOT_FOUND;
4698 }
4699 
FindItem(const wxPoint & pt)4700 long wxListMainWindow::FindItem( const wxPoint& pt )
4701 {
4702     size_t topItem;
4703     GetVisibleLinesRange( &topItem, NULL );
4704 
4705     wxPoint p;
4706     GetItemPosition( GetItemCount() - 1, p );
4707     if ( p.y == 0 )
4708         return topItem;
4709 
4710     long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem );
4711     if ( id >= 0 && id < (long)GetItemCount() )
4712         return id;
4713 
4714     return wxNOT_FOUND;
4715 }
4716 
HitTest(int x,int y,int & flags) const4717 long wxListMainWindow::HitTest( int x, int y, int &flags ) const
4718 {
4719     CalcUnscrolledPosition( x, y, &x, &y );
4720 
4721     size_t count = GetItemCount();
4722 
4723     if ( InReportView() )
4724     {
4725         size_t current = y / GetLineHeight();
4726         if ( current < count )
4727         {
4728             flags = HitTestLine(current, x, y);
4729             if ( flags )
4730                 return current;
4731         }
4732     }
4733     else // !report
4734     {
4735         // TODO: optimize it too! this is less simple than for report view but
4736         //       enumerating all items is still not a way to do it!!
4737         for ( size_t current = 0; current < count; current++ )
4738         {
4739             flags = HitTestLine(current, x, y);
4740             if ( flags )
4741                 return current;
4742         }
4743     }
4744 
4745     return wxNOT_FOUND;
4746 }
4747 
4748 // ----------------------------------------------------------------------------
4749 // adding stuff
4750 // ----------------------------------------------------------------------------
4751 
InsertItem(wxListItem & item)4752 void wxListMainWindow::InsertItem( wxListItem &item )
4753 {
4754     wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4755 
4756     int count = GetItemCount();
4757     wxCHECK_RET( item.m_itemId >= 0, _T("invalid item index") );
4758 
4759     if (item.m_itemId > count)
4760         item.m_itemId = count;
4761 
4762     size_t id = item.m_itemId;
4763 
4764     m_dirty = true;
4765 
4766     if ( InReportView() )
4767     {
4768         ResetVisibleLinesRange();
4769 
4770         // calculate the width of the item and adjust the max column width
4771         wxColWidthInfo *pWidthInfo = m_aColWidths.Item(item.GetColumn());
4772         int width = GetItemWidthWithImage(&item);
4773         item.SetWidth(width);
4774         if (width > pWidthInfo->nMaxWidth)
4775             pWidthInfo->nMaxWidth = width;
4776     }
4777 
4778     wxListLineData *line = new wxListLineData(this);
4779 
4780     line->SetItem( item.m_col, item );
4781 
4782     m_lines.Insert( line, id );
4783 
4784     m_dirty = true;
4785 
4786     // If an item is selected at or below the point of insertion, we need to
4787     // increment the member variables because the current row's index has gone
4788     // up by one
4789     if ( HasCurrent() && m_current >= id )
4790         m_current++;
4791 
4792     SendNotify(id, wxEVT_COMMAND_LIST_INSERT_ITEM);
4793 
4794     RefreshLines(id, GetItemCount() - 1);
4795 }
4796 
InsertColumn(long col,wxListItem & item)4797 void wxListMainWindow::InsertColumn( long col, wxListItem &item )
4798 {
4799     m_dirty = true;
4800     if ( InReportView() )
4801     {
4802         if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
4803             item.m_width = GetTextLength( item.m_text );
4804 
4805         wxListHeaderData *column = new wxListHeaderData( item );
4806         wxColWidthInfo *colWidthInfo = new wxColWidthInfo();
4807 
4808         bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4809         if ( insert )
4810         {
4811             wxListHeaderDataList::compatibility_iterator
4812                 node = m_columns.Item( col );
4813             m_columns.Insert( node, column );
4814             m_aColWidths.Insert( colWidthInfo, col );
4815         }
4816         else
4817         {
4818             m_columns.Append( column );
4819             m_aColWidths.Add( colWidthInfo );
4820         }
4821 
4822         if ( !IsVirtual() )
4823         {
4824             // update all the items
4825             for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4826             {
4827                 wxListLineData * const line = GetLine(i);
4828                 wxListItemData * const data = new wxListItemData(this);
4829                 if ( insert )
4830                     line->m_items.Insert(col, data);
4831                 else
4832                     line->m_items.Append(data);
4833             }
4834         }
4835 
4836         // invalidate it as it has to be recalculated
4837         m_headerWidth = 0;
4838     }
4839 }
4840 
GetItemWidthWithImage(wxListItem * item)4841 int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
4842 {
4843     int width = 0;
4844     wxClientDC dc(this);
4845 
4846     dc.SetFont( GetFont() );
4847 
4848     if (item->GetImage() != -1)
4849     {
4850         int ix, iy;
4851         GetImageSize( item->GetImage(), ix, iy );
4852         width += ix + 5;
4853     }
4854 
4855     if (!item->GetText().empty())
4856     {
4857         wxCoord w;
4858         dc.GetTextExtent( item->GetText(), &w, NULL );
4859         width += w;
4860     }
4861 
4862     return width;
4863 }
4864 
4865 // ----------------------------------------------------------------------------
4866 // sorting
4867 // ----------------------------------------------------------------------------
4868 
4869 wxListCtrlCompare list_ctrl_compare_func_2;
4870 long              list_ctrl_compare_data;
4871 
list_ctrl_compare_func_1(wxListLineData ** arg1,wxListLineData ** arg2)4872 int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
4873 {
4874     wxListLineData *line1 = *arg1;
4875     wxListLineData *line2 = *arg2;
4876     wxListItem item;
4877     line1->GetItem( 0, item );
4878     wxUIntPtr data1 = item.m_data;
4879     line2->GetItem( 0, item );
4880     wxUIntPtr data2 = item.m_data;
4881     return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
4882 }
4883 
SortItems(wxListCtrlCompare fn,long data)4884 void wxListMainWindow::SortItems( wxListCtrlCompare fn, long data )
4885 {
4886     // selections won't make sense any more after sorting the items so reset
4887     // them
4888     HighlightAll(false);
4889     ResetCurrent();
4890 
4891     list_ctrl_compare_func_2 = fn;
4892     list_ctrl_compare_data = data;
4893     m_lines.Sort( list_ctrl_compare_func_1 );
4894     m_dirty = true;
4895 }
4896 
4897 // ----------------------------------------------------------------------------
4898 // scrolling
4899 // ----------------------------------------------------------------------------
4900 
OnScroll(wxScrollWinEvent & event)4901 void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4902 {
4903     // FIXME
4904 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4905     wxScrolledWindow::OnScroll(event);
4906 #else
4907     HandleOnScroll( event );
4908 #endif
4909 
4910     // update our idea of which lines are shown when we redraw the window the
4911     // next time
4912     ResetVisibleLinesRange();
4913 
4914     if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4915     {
4916         wxGenericListCtrl* lc = GetListCtrl();
4917         wxCHECK_RET( lc, _T("no listctrl window?") );
4918 
4919         lc->m_headerWin->Refresh();
4920         lc->m_headerWin->Update();
4921     }
4922 }
4923 
GetCountPerPage() const4924 int wxListMainWindow::GetCountPerPage() const
4925 {
4926     if ( !m_linesPerPage )
4927     {
4928         wxConstCast(this, wxListMainWindow)->
4929             m_linesPerPage = GetClientSize().y / GetLineHeight();
4930     }
4931 
4932     return m_linesPerPage;
4933 }
4934 
GetVisibleLinesRange(size_t * from,size_t * to)4935 void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
4936 {
4937     wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4938 
4939     if ( m_lineFrom == (size_t)-1 )
4940     {
4941         size_t count = GetItemCount();
4942         if ( count )
4943         {
4944             m_lineFrom = GetScrollPos(wxVERTICAL);
4945 
4946             // this may happen if SetScrollbars() hadn't been called yet
4947             if ( m_lineFrom >= count )
4948                 m_lineFrom = count - 1;
4949 
4950             // we redraw one extra line but this is needed to make the redrawing
4951             // logic work when there is a fractional number of lines on screen
4952             m_lineTo = m_lineFrom + m_linesPerPage;
4953             if ( m_lineTo >= count )
4954                 m_lineTo = count - 1;
4955         }
4956         else // empty control
4957         {
4958             m_lineFrom = 0;
4959             m_lineTo = (size_t)-1;
4960         }
4961     }
4962 
4963     wxASSERT_MSG( IsEmpty() ||
4964                   (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
4965                   _T("GetVisibleLinesRange() returns incorrect result") );
4966 
4967     if ( from )
4968         *from = m_lineFrom;
4969     if ( to )
4970         *to = m_lineTo;
4971 }
4972 
4973 // -------------------------------------------------------------------------------------
4974 // wxGenericListCtrl
4975 // -------------------------------------------------------------------------------------
4976 
IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl,wxControl)4977 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl, wxControl)
4978 
4979 BEGIN_EVENT_TABLE(wxGenericListCtrl,wxControl)
4980   EVT_SIZE(wxGenericListCtrl::OnSize)
4981 END_EVENT_TABLE()
4982 
4983 wxGenericListCtrl::wxGenericListCtrl()
4984 {
4985     m_imageListNormal = (wxImageList *) NULL;
4986     m_imageListSmall = (wxImageList *) NULL;
4987     m_imageListState = (wxImageList *) NULL;
4988 
4989     m_ownsImageListNormal =
4990     m_ownsImageListSmall =
4991     m_ownsImageListState = false;
4992 
4993     m_mainWin = (wxListMainWindow*) NULL;
4994     m_headerWin = (wxListHeaderWindow*) NULL;
4995     m_headerHeight = 0;
4996 }
4997 
~wxGenericListCtrl()4998 wxGenericListCtrl::~wxGenericListCtrl()
4999 {
5000     if (m_ownsImageListNormal)
5001         delete m_imageListNormal;
5002     if (m_ownsImageListSmall)
5003         delete m_imageListSmall;
5004     if (m_ownsImageListState)
5005         delete m_imageListState;
5006 }
5007 
CalculateAndSetHeaderHeight()5008 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
5009 {
5010     if ( m_headerWin )
5011     {
5012 #ifdef __WXMAC__
5013         SInt32 h;
5014         GetThemeMetric( kThemeMetricListHeaderHeight, &h );
5015 #else
5016         // we use 'g' to get the descent, too
5017         int w, h, d;
5018         m_headerWin->GetTextExtent(wxT("Hg"), &w, &h, &d);
5019         h += d + 2 * HEADER_OFFSET_Y + EXTRA_HEIGHT;
5020 #endif
5021 
5022         // only update if changed
5023         if ( h != m_headerHeight )
5024         {
5025             m_headerHeight = h;
5026 
5027             if ( HasHeader() )
5028                 ResizeReportView(true);
5029             else    //why is this needed if it doesn't have a header?
5030                 m_headerWin->SetSize(m_headerWin->GetSize().x, m_headerHeight);
5031         }
5032     }
5033 }
5034 
CreateHeaderWindow()5035 void wxGenericListCtrl::CreateHeaderWindow()
5036 {
5037     m_headerWin = new wxListHeaderWindow
5038                       (
5039                         this, wxID_ANY, m_mainWin,
5040                         wxPoint(0,0),
5041                         wxSize(GetClientSize().x, m_headerHeight),
5042                         wxTAB_TRAVERSAL
5043                       );
5044     CalculateAndSetHeaderHeight();
5045 }
5046 
Create(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,long style,const wxValidator & validator,const wxString & name)5047 bool wxGenericListCtrl::Create(wxWindow *parent,
5048                         wxWindowID id,
5049                         const wxPoint &pos,
5050                         const wxSize &size,
5051                         long style,
5052                         const wxValidator &validator,
5053                         const wxString &name)
5054 {
5055     m_imageListNormal =
5056     m_imageListSmall =
5057     m_imageListState = (wxImageList *) NULL;
5058     m_ownsImageListNormal =
5059     m_ownsImageListSmall =
5060     m_ownsImageListState = false;
5061 
5062     m_mainWin = (wxListMainWindow*) NULL;
5063     m_headerWin = (wxListHeaderWindow*) NULL;
5064 
5065     m_headerHeight = 0;
5066 
5067     if ( !(style & wxLC_MASK_TYPE) )
5068     {
5069         style = style | wxLC_LIST;
5070     }
5071 
5072     // add more styles here that should only appear
5073     // in the main window
5074     unsigned long only_main_window_style = wxALWAYS_SHOW_SB;
5075 
5076     if ( !wxControl::Create( parent, id, pos, size, style & ~only_main_window_style, validator, name ) )
5077         return false;
5078 
5079     // don't create the inner window with the border
5080     style &= ~wxBORDER_MASK;
5081 
5082     m_mainWin = new wxListMainWindow( this, wxID_ANY, wxPoint(0, 0), size, style );
5083 
5084 #ifdef  __WXMAC_CARBON__
5085     // Human Interface Guidelines ask us for a special font in this case
5086     if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL )
5087     {
5088         wxFont font;
5089         font.MacCreateThemeFont( kThemeViewsFont );
5090         SetFont( font );
5091     }
5092 #endif
5093 
5094     if ( InReportView() )
5095     {
5096         CreateHeaderWindow();
5097 
5098 #ifdef  __WXMAC_CARBON__
5099         if (m_headerWin)
5100         {
5101             wxFont font;
5102             font.MacCreateThemeFont( kThemeSmallSystemFont );
5103             m_headerWin->SetFont( font );
5104             CalculateAndSetHeaderHeight();
5105         }
5106 #endif
5107 
5108         if ( HasFlag(wxLC_NO_HEADER) )
5109             // VZ: why do we create it at all then?
5110             m_headerWin->Show( false );
5111     }
5112 
5113     SetInitialSize(size);
5114 
5115     return true;
5116 }
5117 
SetSingleStyle(long style,bool add)5118 void wxGenericListCtrl::SetSingleStyle( long style, bool add )
5119 {
5120     wxASSERT_MSG( !(style & wxLC_VIRTUAL),
5121                   _T("wxLC_VIRTUAL can't be [un]set") );
5122 
5123     long flag = GetWindowStyle();
5124 
5125     if (add)
5126     {
5127         if (style & wxLC_MASK_TYPE)
5128             flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
5129         if (style & wxLC_MASK_ALIGN)
5130             flag &= ~wxLC_MASK_ALIGN;
5131         if (style & wxLC_MASK_SORT)
5132             flag &= ~wxLC_MASK_SORT;
5133     }
5134 
5135     if (add)
5136         flag |= style;
5137     else
5138         flag &= ~style;
5139 
5140     // some styles can be set without recreating everything (as happens in
5141     // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
5142     if ( !(style & ~(wxLC_HRULES | wxLC_VRULES)) )
5143     {
5144         Refresh();
5145         wxWindow::SetWindowStyleFlag(flag);
5146     }
5147     else
5148     {
5149         SetWindowStyleFlag( flag );
5150     }
5151 }
5152 
SetWindowStyleFlag(long flag)5153 void wxGenericListCtrl::SetWindowStyleFlag( long flag )
5154 {
5155     if (m_mainWin)
5156     {
5157         m_mainWin->DeleteEverything();
5158 
5159         // has the header visibility changed?
5160         bool hasHeader = HasHeader();
5161         bool willHaveHeader = (flag & wxLC_REPORT) && !(flag & wxLC_NO_HEADER);
5162 
5163         if ( hasHeader != willHaveHeader )
5164         {
5165             // toggle it
5166             if ( hasHeader )
5167             {
5168                 if ( m_headerWin )
5169                 {
5170                     // don't delete, just hide, as we can reuse it later
5171                     m_headerWin->Show(false);
5172                 }
5173                 //else: nothing to do
5174             }
5175             else // must show header
5176             {
5177                 if (!m_headerWin)
5178                 {
5179                     CreateHeaderWindow();
5180                 }
5181                 else // already have it, just show
5182                 {
5183                     m_headerWin->Show( true );
5184                 }
5185             }
5186 
5187             ResizeReportView(willHaveHeader);
5188         }
5189     }
5190 
5191     wxWindow::SetWindowStyleFlag( flag );
5192 }
5193 
GetColumn(int col,wxListItem & item) const5194 bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
5195 {
5196     m_mainWin->GetColumn( col, item );
5197     return true;
5198 }
5199 
SetColumn(int col,wxListItem & item)5200 bool wxGenericListCtrl::SetColumn( int col, wxListItem& item )
5201 {
5202     m_mainWin->SetColumn( col, item );
5203     return true;
5204 }
5205 
GetColumnWidth(int col) const5206 int wxGenericListCtrl::GetColumnWidth( int col ) const
5207 {
5208     return m_mainWin->GetColumnWidth( col );
5209 }
5210 
SetColumnWidth(int col,int width)5211 bool wxGenericListCtrl::SetColumnWidth( int col, int width )
5212 {
5213     m_mainWin->SetColumnWidth( col, width );
5214     return true;
5215 }
5216 
GetCountPerPage() const5217 int wxGenericListCtrl::GetCountPerPage() const
5218 {
5219   return m_mainWin->GetCountPerPage();  // different from Windows ?
5220 }
5221 
GetItem(wxListItem & info) const5222 bool wxGenericListCtrl::GetItem( wxListItem &info ) const
5223 {
5224     m_mainWin->GetItem( info );
5225     return true;
5226 }
5227 
SetItem(wxListItem & info)5228 bool wxGenericListCtrl::SetItem( wxListItem &info )
5229 {
5230     m_mainWin->SetItem( info );
5231     return true;
5232 }
5233 
SetItem(long index,int col,const wxString & label,int imageId)5234 long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
5235 {
5236     wxListItem info;
5237     info.m_text = label;
5238     info.m_mask = wxLIST_MASK_TEXT;
5239     info.m_itemId = index;
5240     info.m_col = col;
5241     if ( imageId > -1 )
5242     {
5243         info.m_image = imageId;
5244         info.m_mask |= wxLIST_MASK_IMAGE;
5245     }
5246 
5247     m_mainWin->SetItem(info);
5248     return true;
5249 }
5250 
GetItemState(long item,long stateMask) const5251 int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
5252 {
5253     return m_mainWin->GetItemState( item, stateMask );
5254 }
5255 
SetItemState(long item,long state,long stateMask)5256 bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
5257 {
5258     m_mainWin->SetItemState( item, state, stateMask );
5259     return true;
5260 }
5261 
5262 bool
SetItemImage(long item,int image,int WXUNUSED (selImage))5263 wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
5264 {
5265     return SetItemColumnImage(item, 0, image);
5266 }
5267 
5268 bool
SetItemColumnImage(long item,long column,int image)5269 wxGenericListCtrl::SetItemColumnImage( long item, long column, int image )
5270 {
5271     wxListItem info;
5272     info.m_image = image;
5273     info.m_mask = wxLIST_MASK_IMAGE;
5274     info.m_itemId = item;
5275     info.m_col = column;
5276     m_mainWin->SetItem( info );
5277     return true;
5278 }
5279 
GetItemText(long item) const5280 wxString wxGenericListCtrl::GetItemText( long item ) const
5281 {
5282     return m_mainWin->GetItemText(item);
5283 }
5284 
SetItemText(long item,const wxString & str)5285 void wxGenericListCtrl::SetItemText( long item, const wxString& str )
5286 {
5287     m_mainWin->SetItemText(item, str);
5288 }
5289 
GetItemData(long item) const5290 wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const
5291 {
5292     wxListItem info;
5293     info.m_mask = wxLIST_MASK_DATA;
5294     info.m_itemId = item;
5295     m_mainWin->GetItem( info );
5296     return info.m_data;
5297 }
5298 
SetItemPtrData(long item,wxUIntPtr data)5299 bool wxGenericListCtrl::SetItemPtrData( long item, wxUIntPtr data )
5300 {
5301     wxListItem info;
5302     info.m_mask = wxLIST_MASK_DATA;
5303     info.m_itemId = item;
5304     info.m_data = data;
5305     m_mainWin->SetItem( info );
5306     return true;
5307 }
5308 
SetItemData(long item,long data)5309 bool wxGenericListCtrl::SetItemData(long item, long data)
5310 {
5311     return SetItemPtrData(item, data);
5312 }
5313 
GetViewRect() const5314 wxRect wxGenericListCtrl::GetViewRect() const
5315 {
5316     return m_mainWin->GetViewRect();
5317 }
5318 
GetItemRect(long item,wxRect & rect,int WXUNUSED (code)) const5319 bool wxGenericListCtrl::GetItemRect( long item, wxRect &rect, int WXUNUSED(code) ) const
5320 {
5321     m_mainWin->GetItemRect( item, rect );
5322     if ( m_mainWin->HasHeader() )
5323         rect.y += m_headerHeight + 1;
5324     return true;
5325 }
5326 
GetItemPosition(long item,wxPoint & pos) const5327 bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
5328 {
5329     m_mainWin->GetItemPosition( item, pos );
5330     return true;
5331 }
5332 
SetItemPosition(long WXUNUSED (item),const wxPoint & WXUNUSED (pos))5333 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
5334 {
5335     return 0;
5336 }
5337 
GetItemCount() const5338 int wxGenericListCtrl::GetItemCount() const
5339 {
5340     return m_mainWin->GetItemCount();
5341 }
5342 
GetColumnCount() const5343 int wxGenericListCtrl::GetColumnCount() const
5344 {
5345     return m_mainWin->GetColumnCount();
5346 }
5347 
SetItemSpacing(int spacing,bool isSmall)5348 void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
5349 {
5350     m_mainWin->SetItemSpacing( spacing, isSmall );
5351 }
5352 
GetItemSpacing() const5353 wxSize wxGenericListCtrl::GetItemSpacing() const
5354 {
5355     const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON));
5356 
5357     return wxSize(spacing, spacing);
5358 }
5359 
5360 #if WXWIN_COMPATIBILITY_2_6
GetItemSpacing(bool isSmall) const5361 int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const
5362 {
5363     return m_mainWin->GetItemSpacing( isSmall );
5364 }
5365 #endif // WXWIN_COMPATIBILITY_2_6
5366 
SetItemTextColour(long item,const wxColour & col)5367 void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
5368 {
5369     wxListItem info;
5370     info.m_itemId = item;
5371     info.SetTextColour( col );
5372     m_mainWin->SetItem( info );
5373 }
5374 
GetItemTextColour(long item) const5375 wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
5376 {
5377     wxListItem info;
5378     info.m_itemId = item;
5379     m_mainWin->GetItem( info );
5380     return info.GetTextColour();
5381 }
5382 
SetItemBackgroundColour(long item,const wxColour & col)5383 void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
5384 {
5385     wxListItem info;
5386     info.m_itemId = item;
5387     info.SetBackgroundColour( col );
5388     m_mainWin->SetItem( info );
5389 }
5390 
GetItemBackgroundColour(long item) const5391 wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
5392 {
5393     wxListItem info;
5394     info.m_itemId = item;
5395     m_mainWin->GetItem( info );
5396     return info.GetBackgroundColour();
5397 }
5398 
GetScrollPos(int orient) const5399 int wxGenericListCtrl::GetScrollPos( int orient ) const
5400 {
5401     return m_mainWin->GetScrollPos( orient );
5402 }
5403 
SetScrollPos(int orient,int pos,bool refresh)5404 void wxGenericListCtrl::SetScrollPos( int orient, int pos, bool refresh )
5405 {
5406     m_mainWin->SetScrollPos( orient, pos, refresh );
5407 }
5408 
SetItemFont(long item,const wxFont & f)5409 void wxGenericListCtrl::SetItemFont( long item, const wxFont &f )
5410 {
5411     wxListItem info;
5412     info.m_itemId = item;
5413     info.SetFont( f );
5414     m_mainWin->SetItem( info );
5415 }
5416 
GetItemFont(long item) const5417 wxFont wxGenericListCtrl::GetItemFont( long item ) const
5418 {
5419     wxListItem info;
5420     info.m_itemId = item;
5421     m_mainWin->GetItem( info );
5422     return info.GetFont();
5423 }
5424 
GetSelectedItemCount() const5425 int wxGenericListCtrl::GetSelectedItemCount() const
5426 {
5427     return m_mainWin->GetSelectedItemCount();
5428 }
5429 
GetTextColour() const5430 wxColour wxGenericListCtrl::GetTextColour() const
5431 {
5432     return GetForegroundColour();
5433 }
5434 
SetTextColour(const wxColour & col)5435 void wxGenericListCtrl::SetTextColour(const wxColour& col)
5436 {
5437     SetForegroundColour(col);
5438 }
5439 
GetTopItem() const5440 long wxGenericListCtrl::GetTopItem() const
5441 {
5442     size_t top;
5443     m_mainWin->GetVisibleLinesRange(&top, NULL);
5444     return (long)top;
5445 }
5446 
GetNextItem(long item,int geom,int state) const5447 long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
5448 {
5449     return m_mainWin->GetNextItem( item, geom, state );
5450 }
5451 
GetImageList(int which) const5452 wxImageList *wxGenericListCtrl::GetImageList(int which) const
5453 {
5454     if (which == wxIMAGE_LIST_NORMAL)
5455         return m_imageListNormal;
5456     else if (which == wxIMAGE_LIST_SMALL)
5457         return m_imageListSmall;
5458     else if (which == wxIMAGE_LIST_STATE)
5459         return m_imageListState;
5460 
5461     return (wxImageList *) NULL;
5462 }
5463 
SetImageList(wxImageList * imageList,int which)5464 void wxGenericListCtrl::SetImageList( wxImageList *imageList, int which )
5465 {
5466     if ( which == wxIMAGE_LIST_NORMAL )
5467     {
5468         if (m_ownsImageListNormal)
5469             delete m_imageListNormal;
5470         m_imageListNormal = imageList;
5471         m_ownsImageListNormal = false;
5472     }
5473     else if ( which == wxIMAGE_LIST_SMALL )
5474     {
5475         if (m_ownsImageListSmall)
5476             delete m_imageListSmall;
5477         m_imageListSmall = imageList;
5478         m_ownsImageListSmall = false;
5479     }
5480     else if ( which == wxIMAGE_LIST_STATE )
5481     {
5482         if (m_ownsImageListState)
5483             delete m_imageListState;
5484         m_imageListState = imageList;
5485         m_ownsImageListState = false;
5486     }
5487 
5488     m_mainWin->SetImageList( imageList, which );
5489 }
5490 
AssignImageList(wxImageList * imageList,int which)5491 void wxGenericListCtrl::AssignImageList(wxImageList *imageList, int which)
5492 {
5493     SetImageList(imageList, which);
5494     if ( which == wxIMAGE_LIST_NORMAL )
5495         m_ownsImageListNormal = true;
5496     else if ( which == wxIMAGE_LIST_SMALL )
5497         m_ownsImageListSmall = true;
5498     else if ( which == wxIMAGE_LIST_STATE )
5499         m_ownsImageListState = true;
5500 }
5501 
Arrange(int WXUNUSED (flag))5502 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
5503 {
5504     return 0;
5505 }
5506 
DeleteItem(long item)5507 bool wxGenericListCtrl::DeleteItem( long item )
5508 {
5509     m_mainWin->DeleteItem( item );
5510     return true;
5511 }
5512 
DeleteAllItems()5513 bool wxGenericListCtrl::DeleteAllItems()
5514 {
5515     m_mainWin->DeleteAllItems();
5516     return true;
5517 }
5518 
DeleteAllColumns()5519 bool wxGenericListCtrl::DeleteAllColumns()
5520 {
5521     size_t count = m_mainWin->m_columns.GetCount();
5522     for ( size_t n = 0; n < count; n++ )
5523         DeleteColumn( 0 );
5524     return true;
5525 }
5526 
ClearAll()5527 void wxGenericListCtrl::ClearAll()
5528 {
5529     m_mainWin->DeleteEverything();
5530 }
5531 
DeleteColumn(int col)5532 bool wxGenericListCtrl::DeleteColumn( int col )
5533 {
5534     m_mainWin->DeleteColumn( col );
5535 
5536     // if we don't have the header any longer, we need to relayout the window
5537     if ( !GetColumnCount() )
5538         ResizeReportView(false /* no header */);
5539     return true;
5540 }
5541 
EditLabel(long item,wxClassInfo * textControlClass)5542 wxTextCtrl *wxGenericListCtrl::EditLabel(long item,
5543                                          wxClassInfo* textControlClass)
5544 {
5545     return m_mainWin->EditLabel( item, textControlClass );
5546 }
5547 
GetEditControl() const5548 wxTextCtrl *wxGenericListCtrl::GetEditControl() const
5549 {
5550     return m_mainWin->GetEditControl();
5551 }
5552 
EnsureVisible(long item)5553 bool wxGenericListCtrl::EnsureVisible( long item )
5554 {
5555     m_mainWin->EnsureVisible( item );
5556     return true;
5557 }
5558 
FindItem(long start,const wxString & str,bool partial)5559 long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
5560 {
5561     return m_mainWin->FindItem( start, str, partial );
5562 }
5563 
FindItem(long start,wxUIntPtr data)5564 long wxGenericListCtrl::FindItem( long start, wxUIntPtr data )
5565 {
5566     return m_mainWin->FindItem( start, data );
5567 }
5568 
FindItem(long WXUNUSED (start),const wxPoint & pt,int WXUNUSED (direction))5569 long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt,
5570                            int WXUNUSED(direction))
5571 {
5572     return m_mainWin->FindItem( pt );
5573 }
5574 
5575 // TODO: sub item hit testing
HitTest(const wxPoint & point,int & flags,long *) const5576 long wxGenericListCtrl::HitTest(const wxPoint& point, int& flags, long *) const
5577 {
5578     return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
5579 }
5580 
InsertItem(wxListItem & info)5581 long wxGenericListCtrl::InsertItem( wxListItem& info )
5582 {
5583     m_mainWin->InsertItem( info );
5584     return info.m_itemId;
5585 }
5586 
InsertItem(long index,const wxString & label)5587 long wxGenericListCtrl::InsertItem( long index, const wxString &label )
5588 {
5589     wxListItem info;
5590     info.m_text = label;
5591     info.m_mask = wxLIST_MASK_TEXT;
5592     info.m_itemId = index;
5593     return InsertItem( info );
5594 }
5595 
InsertItem(long index,int imageIndex)5596 long wxGenericListCtrl::InsertItem( long index, int imageIndex )
5597 {
5598     wxListItem info;
5599     info.m_mask = wxLIST_MASK_IMAGE;
5600     info.m_image = imageIndex;
5601     info.m_itemId = index;
5602     return InsertItem( info );
5603 }
5604 
InsertItem(long index,const wxString & label,int imageIndex)5605 long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
5606 {
5607     wxListItem info;
5608     info.m_text = label;
5609     info.m_image = imageIndex;
5610     info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
5611     info.m_itemId = index;
5612     return InsertItem( info );
5613 }
5614 
InsertColumn(long col,wxListItem & item)5615 long wxGenericListCtrl::InsertColumn( long col, wxListItem &item )
5616 {
5617     wxCHECK_MSG( m_headerWin, -1, _T("can't add column in non report mode") );
5618 
5619     m_mainWin->InsertColumn( col, item );
5620 
5621     // if we hadn't had a header before but have one now
5622     // then we need to relayout the window
5623     if ( GetColumnCount() == 1 && m_mainWin->HasHeader() )
5624         ResizeReportView(true /* have header */);
5625 
5626     m_headerWin->Refresh();
5627 
5628     return 0;
5629 }
5630 
InsertColumn(long col,const wxString & heading,int format,int width)5631 long wxGenericListCtrl::InsertColumn( long col, const wxString &heading,
5632                                int format, int width )
5633 {
5634     wxListItem item;
5635     item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
5636     item.m_text = heading;
5637     if (width >= -2)
5638     {
5639         item.m_mask |= wxLIST_MASK_WIDTH;
5640         item.m_width = width;
5641     }
5642 
5643     item.m_format = format;
5644 
5645     return InsertColumn( col, item );
5646 }
5647 
ScrollList(int dx,int dy)5648 bool wxGenericListCtrl::ScrollList( int dx, int dy )
5649 {
5650     return m_mainWin->ScrollList(dx, dy);
5651 }
5652 
5653 // Sort items.
5654 // fn is a function which takes 3 long arguments: item1, item2, data.
5655 // item1 is the long data associated with a first item (NOT the index).
5656 // item2 is the long data associated with a second item (NOT the index).
5657 // data is the same value as passed to SortItems.
5658 // The return value is a negative number if the first item should precede the second
5659 // item, a positive number of the second item should precede the first,
5660 // or zero if the two items are equivalent.
5661 // data is arbitrary data to be passed to the sort function.
5662 
SortItems(wxListCtrlCompare fn,long data)5663 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn, long data )
5664 {
5665     m_mainWin->SortItems( fn, data );
5666     return true;
5667 }
5668 
5669 // ----------------------------------------------------------------------------
5670 // event handlers
5671 // ----------------------------------------------------------------------------
5672 
OnSize(wxSizeEvent & WXUNUSED (event))5673 void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
5674 {
5675     if ( !m_mainWin )
5676         return;
5677 
5678     ResizeReportView(m_mainWin->HasHeader());
5679     m_mainWin->RecalculatePositions();
5680 }
5681 
ResizeReportView(bool showHeader)5682 void wxGenericListCtrl::ResizeReportView(bool showHeader)
5683 {
5684     int cw, ch;
5685     GetClientSize( &cw, &ch );
5686 
5687     if ( showHeader )
5688     {
5689         m_headerWin->SetSize( 0, 0, cw, m_headerHeight );
5690         if(ch > m_headerHeight)
5691             m_mainWin->SetSize( 0, m_headerHeight + 1,
5692                                    cw, ch - m_headerHeight - 1 );
5693         else
5694             m_mainWin->SetSize( 0, m_headerHeight + 1,
5695                                    cw, 0);
5696     }
5697     else // no header window
5698     {
5699         m_mainWin->SetSize( 0, 0, cw, ch );
5700     }
5701 }
5702 
OnInternalIdle()5703 void wxGenericListCtrl::OnInternalIdle()
5704 {
5705     wxWindow::OnInternalIdle();
5706 
5707     // do it only if needed
5708     if ( !m_mainWin->m_dirty )
5709         return;
5710 
5711     m_mainWin->RecalculatePositions();
5712 }
5713 
5714 // ----------------------------------------------------------------------------
5715 // font/colours
5716 // ----------------------------------------------------------------------------
5717 
SetBackgroundColour(const wxColour & colour)5718 bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
5719 {
5720     if (m_mainWin)
5721     {
5722         m_mainWin->SetBackgroundColour( colour );
5723         m_mainWin->m_dirty = true;
5724     }
5725 
5726     return true;
5727 }
5728 
SetForegroundColour(const wxColour & colour)5729 bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
5730 {
5731     if ( !wxWindow::SetForegroundColour( colour ) )
5732         return false;
5733 
5734     if (m_mainWin)
5735     {
5736         m_mainWin->SetForegroundColour( colour );
5737         m_mainWin->m_dirty = true;
5738     }
5739 
5740     if (m_headerWin)
5741         m_headerWin->SetForegroundColour( colour );
5742 
5743     return true;
5744 }
5745 
SetFont(const wxFont & font)5746 bool wxGenericListCtrl::SetFont( const wxFont &font )
5747 {
5748     if ( !wxWindow::SetFont( font ) )
5749         return false;
5750 
5751     if (m_mainWin)
5752     {
5753         m_mainWin->SetFont( font );
5754         m_mainWin->m_dirty = true;
5755     }
5756 
5757     if (m_headerWin)
5758     {
5759         m_headerWin->SetFont( font );
5760         CalculateAndSetHeaderHeight();
5761     }
5762 
5763     Refresh();
5764 
5765     return true;
5766 }
5767 
5768 // static
5769 wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant variant)5770 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
5771 {
5772 #if _USE_VISATTR
5773     // Use the same color scheme as wxListBox
5774     return wxListBox::GetClassDefaultAttributes(variant);
5775 #else
5776     wxUnusedVar(variant);
5777     wxVisualAttributes attr;
5778     attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
5779     attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
5780     attr.font  = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
5781     return attr;
5782 #endif
5783 }
5784 
5785 // ----------------------------------------------------------------------------
5786 // methods forwarded to m_mainWin
5787 // ----------------------------------------------------------------------------
5788 
5789 #if wxUSE_DRAG_AND_DROP
5790 
SetDropTarget(wxDropTarget * dropTarget)5791 void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
5792 {
5793     m_mainWin->SetDropTarget( dropTarget );
5794 }
5795 
GetDropTarget() const5796 wxDropTarget *wxGenericListCtrl::GetDropTarget() const
5797 {
5798     return m_mainWin->GetDropTarget();
5799 }
5800 
5801 #endif
5802 
SetCursor(const wxCursor & cursor)5803 bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
5804 {
5805     return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false;
5806 }
5807 
GetBackgroundColour() const5808 wxColour wxGenericListCtrl::GetBackgroundColour() const
5809 {
5810     return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5811 }
5812 
GetForegroundColour() const5813 wxColour wxGenericListCtrl::GetForegroundColour() const
5814 {
5815     return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5816 }
5817 
DoPopupMenu(wxMenu * menu,int x,int y)5818 bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
5819 {
5820 #if wxUSE_MENUS
5821     return m_mainWin->PopupMenu( menu, x, y );
5822 #else
5823     return false;
5824 #endif
5825 }
5826 
DoClientToScreen(int * x,int * y) const5827 void wxGenericListCtrl::DoClientToScreen( int *x, int *y ) const
5828 {
5829     m_mainWin->DoClientToScreen(x, y);
5830 }
5831 
DoScreenToClient(int * x,int * y) const5832 void wxGenericListCtrl::DoScreenToClient( int *x, int *y ) const
5833 {
5834     m_mainWin->DoScreenToClient(x, y);
5835 }
5836 
SetFocus()5837 void wxGenericListCtrl::SetFocus()
5838 {
5839     // The test in window.cpp fails as we are a composite
5840     // window, so it checks against "this", but not m_mainWin.
5841     if ( DoFindFocus() != this )
5842         m_mainWin->SetFocus();
5843 }
5844 
DoGetBestSize() const5845 wxSize wxGenericListCtrl::DoGetBestSize() const
5846 {
5847     // Something is better than nothing...
5848     // 100x80 is what the MSW version will get from the default
5849     // wxControl::DoGetBestSize
5850     return wxSize(100, 80);
5851 }
5852 
5853 // ----------------------------------------------------------------------------
5854 // virtual list control support
5855 // ----------------------------------------------------------------------------
5856 
OnGetItemText(long WXUNUSED (item),long WXUNUSED (col)) const5857 wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
5858 {
5859     // this is a pure virtual function, in fact - which is not really pure
5860     // because the controls which are not virtual don't need to implement it
5861     wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5862 
5863     return wxEmptyString;
5864 }
5865 
OnGetItemImage(long WXUNUSED (item)) const5866 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
5867 {
5868     wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
5869                 -1,
5870                 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5871     return -1;
5872 }
5873 
OnGetItemColumnImage(long item,long column) const5874 int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const
5875 {
5876     if (!column)
5877         return OnGetItemImage(item);
5878 
5879    return -1;
5880 }
5881 
5882 wxListItemAttr *
OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG (item)) const5883 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
5884 {
5885     wxASSERT_MSG( item >= 0 && item < GetItemCount(),
5886                   _T("invalid item index in OnGetItemAttr()") );
5887 
5888     // no attributes by default
5889     return NULL;
5890 }
5891 
SetItemCount(long count)5892 void wxGenericListCtrl::SetItemCount(long count)
5893 {
5894     wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5895 
5896     m_mainWin->SetItemCount(count);
5897 }
5898 
RefreshItem(long item)5899 void wxGenericListCtrl::RefreshItem(long item)
5900 {
5901     m_mainWin->RefreshLine(item);
5902 }
5903 
RefreshItems(long itemFrom,long itemTo)5904 void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
5905 {
5906     m_mainWin->RefreshLines(itemFrom, itemTo);
5907 }
5908 
5909 // Generic wxListCtrl is more or less a container for two other
5910 // windows which drawings are done upon. These are namely
5911 // 'm_headerWin' and 'm_mainWin'.
5912 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5913 // behaviour wxListCtrl has under wxMSW.
5914 //
Refresh(bool eraseBackground,const wxRect * rect)5915 void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5916 {
5917     if (!rect)
5918     {
5919         // The easy case, no rectangle specified.
5920         if (m_headerWin)
5921             m_headerWin->Refresh(eraseBackground);
5922 
5923         if (m_mainWin)
5924             m_mainWin->Refresh(eraseBackground);
5925     }
5926     else
5927     {
5928         // Refresh the header window
5929         if (m_headerWin)
5930         {
5931             wxRect rectHeader = m_headerWin->GetRect();
5932             rectHeader.Intersect(*rect);
5933             if (rectHeader.GetWidth() && rectHeader.GetHeight())
5934             {
5935                 int x, y;
5936                 m_headerWin->GetPosition(&x, &y);
5937                 rectHeader.Offset(-x, -y);
5938                 m_headerWin->Refresh(eraseBackground, &rectHeader);
5939             }
5940         }
5941 
5942         // Refresh the main window
5943         if (m_mainWin)
5944         {
5945             wxRect rectMain = m_mainWin->GetRect();
5946             rectMain.Intersect(*rect);
5947             if (rectMain.GetWidth() && rectMain.GetHeight())
5948             {
5949                 int x, y;
5950                 m_mainWin->GetPosition(&x, &y);
5951                 rectMain.Offset(-x, -y);
5952                 m_mainWin->Refresh(eraseBackground, &rectMain);
5953             }
5954         }
5955     }
5956 }
5957 
Update()5958 void wxGenericListCtrl::Update()
5959 {
5960     if (m_mainWin && m_mainWin->m_dirty)
5961         m_mainWin->RecalculatePositions();
5962 
5963     wxControl::Update();
5964 }
5965 
Freeze()5966 void wxGenericListCtrl::Freeze()
5967 {
5968     m_mainWin->Freeze();
5969 }
5970 
Thaw()5971 void wxGenericListCtrl::Thaw()
5972 {
5973     m_mainWin->Thaw();
5974 }
5975 
5976 #endif // wxUSE_LISTCTRL
5977