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