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