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