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