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