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