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