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