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