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