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