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