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