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