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