]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listctrl.cpp
Added native selection rectangle drawing.
[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 __WXGTK__
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 __WXGTK__
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 __WXGTK__
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 __WXGTK__
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 le.m_itemIndex = line;
2846
2847 // set only for events which have position
2848 if ( point != wxDefaultPosition )
2849 le.m_pointDrag = point;
2850
2851 // don't try to get the line info for virtual list controls: the main
2852 // program has it anyhow and if we did it would result in accessing all
2853 // the lines, even those which are not visible now and this is precisely
2854 // what we're trying to avoid
2855 if ( !IsVirtual() && (command != wxEVT_COMMAND_LIST_DELETE_ITEM) )
2856 {
2857 if ( line != (size_t)-1 )
2858 {
2859 GetLine(line)->GetItem( 0, le.m_item );
2860 }
2861 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2862 }
2863 //else: there may be no more such item
2864
2865 GetParent()->GetEventHandler()->ProcessEvent( le );
2866 }
2867
2868 void wxListMainWindow::ChangeCurrent(size_t current)
2869 {
2870 m_current = current;
2871
2872 SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
2873 }
2874
2875 wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
2876 {
2877 wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
2878 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2879
2880 wxASSERT_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)),
2881 wxT("EditLabel() needs a text control") );
2882
2883 size_t itemEdit = (size_t)item;
2884
2885 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
2886 le.SetEventObject( GetParent() );
2887 le.m_itemIndex = item;
2888 wxListLineData *data = GetLine(itemEdit);
2889 wxCHECK_MSG( data, NULL, _T("invalid index in EditLabel()") );
2890 data->GetItem( 0, le.m_item );
2891
2892 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
2893 {
2894 // vetoed by user code
2895 return NULL;
2896 }
2897
2898 // We have to call this here because the label in question might just have
2899 // been added and no screen update taken place.
2900 if ( m_dirty )
2901 wxSafeYield();
2902
2903 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
2904 m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
2905 return m_textctrlWrapper->GetText();
2906 }
2907
2908 void wxListMainWindow::OnRenameTimer()
2909 {
2910 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2911
2912 EditLabel( m_current );
2913 }
2914
2915 bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
2916 {
2917 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2918 le.SetEventObject( GetParent() );
2919 le.m_itemIndex = itemEdit;
2920
2921 wxListLineData *data = GetLine(itemEdit);
2922
2923 wxCHECK_MSG( data, false, _T("invalid index in OnRenameAccept()") );
2924
2925 data->GetItem( 0, le.m_item );
2926 le.m_item.m_text = value;
2927 return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
2928 le.IsAllowed();
2929 }
2930
2931 void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
2932 {
2933 // let owner know that the edit was cancelled
2934 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2935
2936 le.SetEditCanceled(true);
2937
2938 le.SetEventObject( GetParent() );
2939 le.m_itemIndex = itemEdit;
2940
2941 wxListLineData *data = GetLine(itemEdit);
2942 wxCHECK_RET( data, _T("invalid index in OnRenameCancelled()") );
2943
2944 data->GetItem( 0, le.m_item );
2945 GetEventHandler()->ProcessEvent( le );
2946 }
2947
2948 void wxListMainWindow::OnMouse( wxMouseEvent &event )
2949 {
2950
2951 #ifdef __WXMAC__
2952 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2953 // shutdown the edit control when the mouse is clicked elsewhere on the
2954 // listctrl because the order of events is different (or something like
2955 // that), so explicitly end the edit if it is active.
2956 if ( event.LeftDown() && m_textctrlWrapper )
2957 m_textctrlWrapper->AcceptChangesAndFinish();
2958 #endif // __WXMAC__
2959
2960 event.SetEventObject( GetParent() );
2961 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
2962 return;
2963
2964 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
2965 {
2966 // let the base handle mouse wheel events.
2967 event.Skip();
2968 return;
2969 }
2970
2971 if ( !HasCurrent() || IsEmpty() )
2972 {
2973 if (event.RightDown())
2974 {
2975 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2976 // Allow generation of context menu event
2977 event.Skip();
2978 }
2979 return;
2980 }
2981
2982 if (m_dirty)
2983 return;
2984
2985 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
2986 event.ButtonDClick()) )
2987 return;
2988
2989 int x = event.GetX();
2990 int y = event.GetY();
2991 CalcUnscrolledPosition( x, y, &x, &y );
2992
2993 // where did we hit it (if we did)?
2994 long hitResult = 0;
2995
2996 size_t count = GetItemCount(),
2997 current;
2998
2999 if ( InReportView() )
3000 {
3001 current = y / GetLineHeight();
3002 if ( current < count )
3003 hitResult = HitTestLine(current, x, y);
3004 }
3005 else // !report
3006 {
3007 // TODO: optimize it too! this is less simple than for report view but
3008 // enumerating all items is still not a way to do it!!
3009 for ( current = 0; current < count; current++ )
3010 {
3011 hitResult = HitTestLine(current, x, y);
3012 if ( hitResult )
3013 break;
3014 }
3015 }
3016
3017 if (event.Dragging())
3018 {
3019 if (m_dragCount == 0)
3020 {
3021 // we have to report the raw, physical coords as we want to be
3022 // able to call HitTest(event.m_pointDrag) from the user code to
3023 // get the item being dragged
3024 m_dragStart = event.GetPosition();
3025 }
3026
3027 m_dragCount++;
3028
3029 if (m_dragCount != 3)
3030 return;
3031
3032 int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3033 : wxEVT_COMMAND_LIST_BEGIN_DRAG;
3034
3035 wxListEvent le( command, GetParent()->GetId() );
3036 le.SetEventObject( GetParent() );
3037 le.m_itemIndex = m_lineLastClicked;
3038 le.m_pointDrag = m_dragStart;
3039 GetParent()->GetEventHandler()->ProcessEvent( le );
3040
3041 return;
3042 }
3043 else
3044 {
3045 m_dragCount = 0;
3046 }
3047
3048 if ( !hitResult )
3049 {
3050 // outside of any item
3051 if (event.RightDown())
3052 {
3053 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3054 // Allow generation of context menu event
3055 event.Skip();
3056 }
3057 else
3058 {
3059 // reset the selection and bail out
3060 HighlightAll(false);
3061 }
3062
3063 return;
3064 }
3065
3066 bool forceClick = false;
3067 if (event.ButtonDClick())
3068 {
3069 m_renameTimer->Stop();
3070 m_lastOnSame = false;
3071
3072 if ( current == m_lineLastClicked )
3073 {
3074 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3075
3076 return;
3077 }
3078 else
3079 {
3080 // The first click was on another item, so don't interpret this as
3081 // a double click, but as a simple click instead
3082 forceClick = true;
3083 }
3084 }
3085
3086 if (event.LeftUp())
3087 {
3088 if (m_lineSelectSingleOnUp != (size_t)-1)
3089 {
3090 // select single line
3091 HighlightAll( false );
3092 ReverseHighlight(m_lineSelectSingleOnUp);
3093 }
3094
3095 if (m_lastOnSame)
3096 {
3097 if ((current == m_current) &&
3098 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
3099 HasFlag(wxLC_EDIT_LABELS) )
3100 {
3101 m_renameTimer->Start( 100, true );
3102 }
3103 }
3104
3105 m_lastOnSame = false;
3106 m_lineSelectSingleOnUp = (size_t)-1;
3107 }
3108 else
3109 {
3110 // This is necessary, because after a DnD operation in
3111 // from and to ourself, the up event is swallowed by the
3112 // DnD code. So on next non-up event (which means here and
3113 // now) m_lineSelectSingleOnUp should be reset.
3114 m_lineSelectSingleOnUp = (size_t)-1;
3115 }
3116 if (event.RightDown())
3117 {
3118 m_lineBeforeLastClicked = m_lineLastClicked;
3119 m_lineLastClicked = current;
3120
3121 // If the item is already selected, do not update the selection.
3122 // Multi-selections should not be cleared if a selected item is clicked.
3123 if (!IsHighlighted(current))
3124 {
3125 HighlightAll(false);
3126 ChangeCurrent(current);
3127 ReverseHighlight(m_current);
3128 }
3129
3130 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3131
3132 // Allow generation of context menu event
3133 event.Skip();
3134 }
3135 else if (event.MiddleDown())
3136 {
3137 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
3138 }
3139 else if ( event.LeftDown() || forceClick )
3140 {
3141 m_lineBeforeLastClicked = m_lineLastClicked;
3142 m_lineLastClicked = current;
3143
3144 size_t oldCurrent = m_current;
3145 bool oldWasSelected = IsHighlighted(m_current);
3146
3147 bool cmdModifierDown = event.CmdDown();
3148 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
3149 {
3150 if ( IsSingleSel() || !IsHighlighted(current) )
3151 {
3152 HighlightAll( false );
3153
3154 ChangeCurrent(current);
3155
3156 ReverseHighlight(m_current);
3157 }
3158 else // multi sel & current is highlighted & no mod keys
3159 {
3160 m_lineSelectSingleOnUp = current;
3161 ChangeCurrent(current); // change focus
3162 }
3163 }
3164 else // multi sel & either ctrl or shift is down
3165 {
3166 if (cmdModifierDown)
3167 {
3168 ChangeCurrent(current);
3169
3170 ReverseHighlight(m_current);
3171 }
3172 else if (event.ShiftDown())
3173 {
3174 ChangeCurrent(current);
3175
3176 size_t lineFrom = oldCurrent,
3177 lineTo = current;
3178
3179 if ( lineTo < lineFrom )
3180 {
3181 lineTo = lineFrom;
3182 lineFrom = m_current;
3183 }
3184
3185 HighlightLines(lineFrom, lineTo);
3186 }
3187 else // !ctrl, !shift
3188 {
3189 // test in the enclosing if should make it impossible
3190 wxFAIL_MSG( _T("how did we get here?") );
3191 }
3192 }
3193
3194 if (m_current != oldCurrent)
3195 RefreshLine( oldCurrent );
3196
3197 // forceClick is only set if the previous click was on another item
3198 m_lastOnSame = !forceClick && (m_current == oldCurrent) && oldWasSelected;
3199 }
3200 }
3201
3202 void wxListMainWindow::MoveToItem(size_t item)
3203 {
3204 if ( item == (size_t)-1 )
3205 return;
3206
3207 wxRect rect = GetLineRect(item);
3208
3209 int client_w, client_h;
3210 GetClientSize( &client_w, &client_h );
3211
3212 const int hLine = GetLineHeight();
3213
3214 int view_x = SCROLL_UNIT_X * GetScrollPos( wxHORIZONTAL );
3215 int view_y = hLine * GetScrollPos( wxVERTICAL );
3216
3217 if ( InReportView() )
3218 {
3219 // the next we need the range of lines shown it might be different,
3220 // so recalculate it
3221 ResetVisibleLinesRange();
3222
3223 if (rect.y < view_y)
3224 Scroll( -1, rect.y / hLine );
3225 if (rect.y + rect.height + 5 > view_y + client_h)
3226 Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
3227 }
3228 else // !report
3229 {
3230 if (rect.x-view_x < 5)
3231 Scroll( (rect.x - 5) / SCROLL_UNIT_X, -1 );
3232 if (rect.x + rect.width - 5 > view_x + client_w)
3233 Scroll( (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X, -1 );
3234 }
3235 }
3236
3237 // ----------------------------------------------------------------------------
3238 // keyboard handling
3239 // ----------------------------------------------------------------------------
3240
3241 void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
3242 {
3243 wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
3244 _T("invalid item index in OnArrowChar()") );
3245
3246 size_t oldCurrent = m_current;
3247
3248 // in single selection we just ignore Shift as we can't select several
3249 // items anyhow
3250 if ( event.ShiftDown() && !IsSingleSel() )
3251 {
3252 ChangeCurrent(newCurrent);
3253
3254 // refresh the old focus to remove it
3255 RefreshLine( oldCurrent );
3256
3257 // select all the items between the old and the new one
3258 if ( oldCurrent > newCurrent )
3259 {
3260 newCurrent = oldCurrent;
3261 oldCurrent = m_current;
3262 }
3263
3264 HighlightLines(oldCurrent, newCurrent);
3265 }
3266 else // !shift
3267 {
3268 // all previously selected items are unselected unless ctrl is held
3269 if ( !event.ControlDown() )
3270 HighlightAll(false);
3271
3272 ChangeCurrent(newCurrent);
3273
3274 // refresh the old focus to remove it
3275 RefreshLine( oldCurrent );
3276
3277 if ( !event.ControlDown() )
3278 {
3279 HighlightLine( m_current, true );
3280 }
3281 }
3282
3283 RefreshLine( m_current );
3284
3285 MoveToFocus();
3286 }
3287
3288 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
3289 {
3290 wxWindow *parent = GetParent();
3291
3292 // propagate the key event upwards
3293 wxKeyEvent ke( wxEVT_KEY_DOWN );
3294 ke.m_shiftDown = event.m_shiftDown;
3295 ke.m_controlDown = event.m_controlDown;
3296 ke.m_altDown = event.m_altDown;
3297 ke.m_metaDown = event.m_metaDown;
3298 ke.m_keyCode = event.m_keyCode;
3299 ke.m_x = event.m_x;
3300 ke.m_y = event.m_y;
3301 ke.SetEventObject( parent );
3302 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
3303
3304 event.Skip();
3305 }
3306
3307 void wxListMainWindow::OnChar( wxKeyEvent &event )
3308 {
3309 wxWindow *parent = GetParent();
3310
3311 // send a list_key event up
3312 if ( HasCurrent() )
3313 {
3314 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() );
3315 le.m_itemIndex = m_current;
3316 GetLine(m_current)->GetItem( 0, le.m_item );
3317 le.m_code = event.GetKeyCode();
3318 le.SetEventObject( parent );
3319 parent->GetEventHandler()->ProcessEvent( le );
3320 }
3321
3322 // propagate the char event upwards
3323 wxKeyEvent ke( wxEVT_CHAR );
3324 ke.m_shiftDown = event.m_shiftDown;
3325 ke.m_controlDown = event.m_controlDown;
3326 ke.m_altDown = event.m_altDown;
3327 ke.m_metaDown = event.m_metaDown;
3328 ke.m_keyCode = event.m_keyCode;
3329 ke.m_x = event.m_x;
3330 ke.m_y = event.m_y;
3331 ke.SetEventObject( parent );
3332 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
3333
3334 if (event.GetKeyCode() == WXK_TAB)
3335 {
3336 wxNavigationKeyEvent nevent;
3337 nevent.SetWindowChange( event.ControlDown() );
3338 nevent.SetDirection( !event.ShiftDown() );
3339 nevent.SetEventObject( GetParent()->GetParent() );
3340 nevent.SetCurrentFocus( m_parent );
3341 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent ))
3342 return;
3343 }
3344
3345 // no item -> nothing to do
3346 if (!HasCurrent())
3347 {
3348 event.Skip();
3349 return;
3350 }
3351
3352 // don't use m_linesPerPage directly as it might not be computed yet
3353 const int pageSize = GetCountPerPage();
3354 wxCHECK_RET( pageSize, _T("should have non zero page size") );
3355
3356 if (GetLayoutDirection() == wxLayout_RightToLeft)
3357 {
3358 if (event.GetKeyCode() == WXK_RIGHT)
3359 event.m_keyCode = WXK_LEFT;
3360 else if (event.GetKeyCode() == WXK_LEFT)
3361 event.m_keyCode = WXK_RIGHT;
3362 }
3363
3364 switch ( event.GetKeyCode() )
3365 {
3366 case WXK_UP:
3367 if ( m_current > 0 )
3368 OnArrowChar( m_current - 1, event );
3369 break;
3370
3371 case WXK_DOWN:
3372 if ( m_current < (size_t)GetItemCount() - 1 )
3373 OnArrowChar( m_current + 1, event );
3374 break;
3375
3376 case WXK_END:
3377 if (!IsEmpty())
3378 OnArrowChar( GetItemCount() - 1, event );
3379 break;
3380
3381 case WXK_HOME:
3382 if (!IsEmpty())
3383 OnArrowChar( 0, event );
3384 break;
3385
3386 case WXK_PAGEUP:
3387 {
3388 int steps = InReportView() ? pageSize - 1
3389 : m_current % pageSize;
3390
3391 int index = m_current - steps;
3392 if (index < 0)
3393 index = 0;
3394
3395 OnArrowChar( index, event );
3396 }
3397 break;
3398
3399 case WXK_PAGEDOWN:
3400 {
3401 int steps = InReportView()
3402 ? pageSize - 1
3403 : pageSize - (m_current % pageSize) - 1;
3404
3405 size_t index = m_current + steps;
3406 size_t count = GetItemCount();
3407 if ( index >= count )
3408 index = count - 1;
3409
3410 OnArrowChar( index, event );
3411 }
3412 break;
3413
3414 case WXK_LEFT:
3415 if ( !InReportView() )
3416 {
3417 int index = m_current - pageSize;
3418 if (index < 0)
3419 index = 0;
3420
3421 OnArrowChar( index, event );
3422 }
3423 break;
3424
3425 case WXK_RIGHT:
3426 if ( !InReportView() )
3427 {
3428 size_t index = m_current + pageSize;
3429
3430 size_t count = GetItemCount();
3431 if ( index >= count )
3432 index = count - 1;
3433
3434 OnArrowChar( index, event );
3435 }
3436 break;
3437
3438 case WXK_SPACE:
3439 if ( IsSingleSel() )
3440 {
3441 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3442
3443 if ( IsHighlighted(m_current) )
3444 {
3445 // don't unselect the item in single selection mode
3446 break;
3447 }
3448 //else: select it in ReverseHighlight() below if unselected
3449 }
3450
3451 ReverseHighlight(m_current);
3452 break;
3453
3454 case WXK_RETURN:
3455 case WXK_EXECUTE:
3456 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3457 break;
3458
3459 default:
3460 event.Skip();
3461 }
3462 }
3463
3464 // ----------------------------------------------------------------------------
3465 // focus handling
3466 // ----------------------------------------------------------------------------
3467
3468 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
3469 {
3470 if ( GetParent() )
3471 {
3472 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
3473 event.SetEventObject( GetParent() );
3474 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3475 return;
3476 }
3477
3478 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3479 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3480 // which are already drawn correctly resulting in horrible flicker - avoid
3481 // it
3482 if ( !m_hasFocus )
3483 {
3484 m_hasFocus = true;
3485
3486 RefreshSelected();
3487 }
3488 }
3489
3490 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
3491 {
3492 if ( GetParent() )
3493 {
3494 wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
3495 event.SetEventObject( GetParent() );
3496 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3497 return;
3498 }
3499
3500 m_hasFocus = false;
3501 RefreshSelected();
3502 }
3503
3504 void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
3505 {
3506 if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
3507 {
3508 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3509 }
3510 else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
3511 {
3512 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3513 }
3514 else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
3515 {
3516 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3517 }
3518 else if ( InReportView() && (m_small_image_list))
3519 {
3520 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3521 }
3522 }
3523
3524 void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
3525 {
3526 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
3527 {
3528 m_normal_image_list->GetSize( index, width, height );
3529 }
3530 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
3531 {
3532 m_small_image_list->GetSize( index, width, height );
3533 }
3534 else if ( HasFlag(wxLC_LIST) && m_small_image_list )
3535 {
3536 m_small_image_list->GetSize( index, width, height );
3537 }
3538 else if ( InReportView() && m_small_image_list )
3539 {
3540 m_small_image_list->GetSize( index, width, height );
3541 }
3542 else
3543 {
3544 width =
3545 height = 0;
3546 }
3547 }
3548
3549 int wxListMainWindow::GetTextLength( const wxString &s ) const
3550 {
3551 wxClientDC dc( wxConstCast(this, wxListMainWindow) );
3552 dc.SetFont( GetFont() );
3553
3554 wxCoord lw;
3555 dc.GetTextExtent( s, &lw, NULL );
3556
3557 return lw + AUTOSIZE_COL_MARGIN;
3558 }
3559
3560 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
3561 {
3562 m_dirty = true;
3563
3564 // calc the spacing from the icon size
3565 int width = 0, height = 0;
3566
3567 if ((imageList) && (imageList->GetImageCount()) )
3568 imageList->GetSize(0, width, height);
3569
3570 if (which == wxIMAGE_LIST_NORMAL)
3571 {
3572 m_normal_image_list = imageList;
3573 m_normal_spacing = width + 8;
3574 }
3575
3576 if (which == wxIMAGE_LIST_SMALL)
3577 {
3578 m_small_image_list = imageList;
3579 m_small_spacing = width + 14;
3580 m_lineHeight = 0; // ensure that the line height will be recalc'd
3581 }
3582 }
3583
3584 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
3585 {
3586 m_dirty = true;
3587 if (isSmall)
3588 m_small_spacing = spacing;
3589 else
3590 m_normal_spacing = spacing;
3591 }
3592
3593 int wxListMainWindow::GetItemSpacing( bool isSmall )
3594 {
3595 return isSmall ? m_small_spacing : m_normal_spacing;
3596 }
3597
3598 // ----------------------------------------------------------------------------
3599 // columns
3600 // ----------------------------------------------------------------------------
3601
3602 void wxListMainWindow::SetColumn( int col, wxListItem &item )
3603 {
3604 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3605
3606 wxCHECK_RET( node, _T("invalid column index in SetColumn") );
3607
3608 if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
3609 item.m_width = GetTextLength( item.m_text );
3610
3611 wxListHeaderData *column = node->GetData();
3612 column->SetItem( item );
3613
3614 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3615 if ( headerWin )
3616 headerWin->m_dirty = true;
3617
3618 m_dirty = true;
3619
3620 // invalidate it as it has to be recalculated
3621 m_headerWidth = 0;
3622 }
3623
3624 void wxListMainWindow::SetColumnWidth( int col, int width )
3625 {
3626 wxCHECK_RET( col >= 0 && col < GetColumnCount(),
3627 _T("invalid column index") );
3628
3629 wxCHECK_RET( InReportView(),
3630 _T("SetColumnWidth() can only be called in report mode.") );
3631
3632 m_dirty = true;
3633 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3634 if ( headerWin )
3635 headerWin->m_dirty = true;
3636
3637 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3638 wxCHECK_RET( node, _T("no column?") );
3639
3640 wxListHeaderData *column = node->GetData();
3641
3642 size_t count = GetItemCount();
3643
3644 if (width == wxLIST_AUTOSIZE_USEHEADER)
3645 {
3646 width = GetTextLength(column->GetText());
3647 }
3648 else if ( width == wxLIST_AUTOSIZE )
3649 {
3650 if ( IsVirtual() )
3651 {
3652 // TODO: determine the max width somehow...
3653 width = WIDTH_COL_DEFAULT;
3654 }
3655 else // !virtual
3656 {
3657 wxClientDC dc(this);
3658 dc.SetFont( GetFont() );
3659
3660 int max = AUTOSIZE_COL_MARGIN;
3661
3662 // if the cached column width isn't valid then recalculate it
3663 if (m_aColWidths.Item(col)->bNeedsUpdate)
3664 {
3665 for (size_t i = 0; i < count; i++)
3666 {
3667 wxListLineData *line = GetLine( i );
3668 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3669
3670 wxCHECK_RET( n, _T("no subitem?") );
3671
3672 wxListItemData *itemData = n->GetData();
3673 wxListItem item;
3674
3675 itemData->GetItem(item);
3676 int itemWidth = GetItemWidthWithImage(&item);
3677 if (itemWidth > max)
3678 max = itemWidth;
3679 }
3680
3681 m_aColWidths.Item(col)->bNeedsUpdate = false;
3682 m_aColWidths.Item(col)->nMaxWidth = max;
3683 }
3684
3685 max = m_aColWidths.Item(col)->nMaxWidth;
3686 width = max + AUTOSIZE_COL_MARGIN;
3687 }
3688 }
3689
3690 column->SetWidth( width );
3691
3692 // invalidate it as it has to be recalculated
3693 m_headerWidth = 0;
3694 }
3695
3696 int wxListMainWindow::GetHeaderWidth() const
3697 {
3698 if ( !m_headerWidth )
3699 {
3700 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
3701
3702 size_t count = GetColumnCount();
3703 for ( size_t col = 0; col < count; col++ )
3704 {
3705 self->m_headerWidth += GetColumnWidth(col);
3706 }
3707 }
3708
3709 return m_headerWidth;
3710 }
3711
3712 void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
3713 {
3714 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3715 wxCHECK_RET( node, _T("invalid column index in GetColumn") );
3716
3717 wxListHeaderData *column = node->GetData();
3718 column->GetItem( item );
3719 }
3720
3721 int wxListMainWindow::GetColumnWidth( int col ) const
3722 {
3723 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
3724 wxCHECK_MSG( node, 0, _T("invalid column index") );
3725
3726 wxListHeaderData *column = node->GetData();
3727 return column->GetWidth();
3728 }
3729
3730 // ----------------------------------------------------------------------------
3731 // item state
3732 // ----------------------------------------------------------------------------
3733
3734 void wxListMainWindow::SetItem( wxListItem &item )
3735 {
3736 long id = item.m_itemId;
3737 wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
3738 _T("invalid item index in SetItem") );
3739
3740 if ( !IsVirtual() )
3741 {
3742 wxListLineData *line = GetLine((size_t)id);
3743 line->SetItem( item.m_col, item );
3744
3745 // Set item state if user wants
3746 if ( item.m_mask & wxLIST_MASK_STATE )
3747 SetItemState( item.m_itemId, item.m_state, item.m_state );
3748
3749 if (InReportView())
3750 {
3751 // update the Max Width Cache if needed
3752 int width = GetItemWidthWithImage(&item);
3753
3754 if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
3755 m_aColWidths.Item(item.m_col)->nMaxWidth = width;
3756 }
3757 }
3758
3759 // update the item on screen
3760 wxRect rectItem;
3761 GetItemRect(id, rectItem);
3762 RefreshRect(rectItem);
3763 }
3764
3765 void wxListMainWindow::SetItemStateAll(long state, long stateMask)
3766 {
3767 if ( IsEmpty() )
3768 return;
3769
3770 // first deal with selection
3771 if ( stateMask & wxLIST_STATE_SELECTED )
3772 {
3773 // set/clear select state
3774 if ( IsVirtual() )
3775 {
3776 // optimized version for virtual listctrl.
3777 m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
3778 Refresh();
3779 }
3780 else if ( state & wxLIST_STATE_SELECTED )
3781 {
3782 const long count = GetItemCount();
3783 for( long i = 0; i < count; i++ )
3784 {
3785 SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
3786 }
3787
3788 }
3789 else
3790 {
3791 // clear for non virtual (somewhat optimized by using GetNextItem())
3792 long i = -1;
3793 while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
3794 {
3795 SetItemState( i, 0, wxLIST_STATE_SELECTED );
3796 }
3797 }
3798 }
3799
3800 if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
3801 {
3802 // unfocus all: only one item can be focussed, so clearing focus for
3803 // all items is simply clearing focus of the focussed item.
3804 SetItemState(m_current, state, stateMask);
3805 }
3806 //(setting focus to all items makes no sense, so it is not handled here.)
3807 }
3808
3809 void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
3810 {
3811 if ( litem == -1 )
3812 {
3813 SetItemStateAll(state, stateMask);
3814 return;
3815 }
3816
3817 wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
3818 _T("invalid list ctrl item index in SetItem") );
3819
3820 size_t oldCurrent = m_current;
3821 size_t item = (size_t)litem; // safe because of the check above
3822
3823 // do we need to change the focus?
3824 if ( stateMask & wxLIST_STATE_FOCUSED )
3825 {
3826 if ( state & wxLIST_STATE_FOCUSED )
3827 {
3828 // don't do anything if this item is already focused
3829 if ( item != m_current )
3830 {
3831 ChangeCurrent(item);
3832
3833 if ( oldCurrent != (size_t)-1 )
3834 {
3835 if ( IsSingleSel() )
3836 {
3837 HighlightLine(oldCurrent, false);
3838 }
3839
3840 RefreshLine(oldCurrent);
3841 }
3842
3843 RefreshLine( m_current );
3844 }
3845 }
3846 else // unfocus
3847 {
3848 // don't do anything if this item is not focused
3849 if ( item == m_current )
3850 {
3851 ResetCurrent();
3852
3853 if ( IsSingleSel() )
3854 {
3855 // we must unselect the old current item as well or we
3856 // might end up with more than one selected item in a
3857 // single selection control
3858 HighlightLine(oldCurrent, false);
3859 }
3860
3861 RefreshLine( oldCurrent );
3862 }
3863 }
3864 }
3865
3866 // do we need to change the selection state?
3867 if ( stateMask & wxLIST_STATE_SELECTED )
3868 {
3869 bool on = (state & wxLIST_STATE_SELECTED) != 0;
3870
3871 if ( IsSingleSel() )
3872 {
3873 if ( on )
3874 {
3875 // selecting the item also makes it the focused one in the
3876 // single sel mode
3877 if ( m_current != item )
3878 {
3879 ChangeCurrent(item);
3880
3881 if ( oldCurrent != (size_t)-1 )
3882 {
3883 HighlightLine( oldCurrent, false );
3884 RefreshLine( oldCurrent );
3885 }
3886 }
3887 }
3888 else // off
3889 {
3890 // only the current item may be selected anyhow
3891 if ( item != m_current )
3892 return;
3893 }
3894 }
3895
3896 if ( HighlightLine(item, on) )
3897 {
3898 RefreshLine(item);
3899 }
3900 }
3901 }
3902
3903 int wxListMainWindow::GetItemState( long item, long stateMask ) const
3904 {
3905 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
3906 _T("invalid list ctrl item index in GetItemState()") );
3907
3908 int ret = wxLIST_STATE_DONTCARE;
3909
3910 if ( stateMask & wxLIST_STATE_FOCUSED )
3911 {
3912 if ( (size_t)item == m_current )
3913 ret |= wxLIST_STATE_FOCUSED;
3914 }
3915
3916 if ( stateMask & wxLIST_STATE_SELECTED )
3917 {
3918 if ( IsHighlighted(item) )
3919 ret |= wxLIST_STATE_SELECTED;
3920 }
3921
3922 return ret;
3923 }
3924
3925 void wxListMainWindow::GetItem( wxListItem &item ) const
3926 {
3927 wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
3928 _T("invalid item index in GetItem") );
3929
3930 wxListLineData *line = GetLine((size_t)item.m_itemId);
3931 line->GetItem( item.m_col, item );
3932
3933 // Get item state if user wants it
3934 if ( item.m_mask & wxLIST_MASK_STATE )
3935 item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
3936 wxLIST_STATE_FOCUSED );
3937 }
3938
3939 // ----------------------------------------------------------------------------
3940 // item count
3941 // ----------------------------------------------------------------------------
3942
3943 size_t wxListMainWindow::GetItemCount() const
3944 {
3945 return IsVirtual() ? m_countVirt : m_lines.GetCount();
3946 }
3947
3948 void wxListMainWindow::SetItemCount(long count)
3949 {
3950 m_selStore.SetItemCount(count);
3951 m_countVirt = count;
3952
3953 ResetVisibleLinesRange();
3954
3955 // scrollbars must be reset
3956 m_dirty = true;
3957 }
3958
3959 int wxListMainWindow::GetSelectedItemCount() const
3960 {
3961 // deal with the quick case first
3962 if ( IsSingleSel() )
3963 return HasCurrent() ? IsHighlighted(m_current) : false;
3964
3965 // virtual controls remmebers all its selections itself
3966 if ( IsVirtual() )
3967 return m_selStore.GetSelectedCount();
3968
3969 // TODO: we probably should maintain the number of items selected even for
3970 // non virtual controls as enumerating all lines is really slow...
3971 size_t countSel = 0;
3972 size_t count = GetItemCount();
3973 for ( size_t line = 0; line < count; line++ )
3974 {
3975 if ( GetLine(line)->IsHighlighted() )
3976 countSel++;
3977 }
3978
3979 return countSel;
3980 }
3981
3982 // ----------------------------------------------------------------------------
3983 // item position/size
3984 // ----------------------------------------------------------------------------
3985
3986 wxRect wxListMainWindow::GetViewRect() const
3987 {
3988 wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
3989 _T("wxListCtrl::GetViewRect() only works in icon mode") );
3990
3991 // we need to find the longest/tallest label
3992 wxCoord xMax = 0, yMax = 0;
3993 const int count = GetItemCount();
3994 if ( count )
3995 {
3996 for ( int i = 0; i < count; i++ )
3997 {
3998 wxRect r;
3999 GetItemRect(i, r);
4000
4001 wxCoord x = r.GetRight(),
4002 y = r.GetBottom();
4003
4004 if ( x > xMax )
4005 xMax = x;
4006 if ( y > yMax )
4007 yMax = y;
4008 }
4009 }
4010
4011 // some fudge needed to make it look prettier
4012 xMax += 2 * EXTRA_BORDER_X;
4013 yMax += 2 * EXTRA_BORDER_Y;
4014
4015 // account for the scrollbars if necessary
4016 const wxSize sizeAll = GetClientSize();
4017 if ( xMax > sizeAll.x )
4018 yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
4019 if ( yMax > sizeAll.y )
4020 xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
4021
4022 return wxRect(0, 0, xMax, yMax);
4023 }
4024
4025 void wxListMainWindow::GetItemRect( long index, wxRect &rect ) const
4026 {
4027 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4028 _T("invalid index in GetItemRect") );
4029
4030 // ensure that we're laid out, otherwise we could return nonsense
4031 if ( m_dirty )
4032 {
4033 wxConstCast(this, wxListMainWindow)->
4034 RecalculatePositions(true /* no refresh */);
4035 }
4036
4037 rect = GetLineRect((size_t)index);
4038
4039 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
4040 }
4041
4042 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
4043 {
4044 wxRect rect;
4045 GetItemRect(item, rect);
4046
4047 pos.x = rect.x;
4048 pos.y = rect.y;
4049
4050 return true;
4051 }
4052
4053 // ----------------------------------------------------------------------------
4054 // geometry calculation
4055 // ----------------------------------------------------------------------------
4056
4057 void wxListMainWindow::RecalculatePositions(bool noRefresh)
4058 {
4059 const int lineHeight = GetLineHeight();
4060
4061 wxClientDC dc( this );
4062 dc.SetFont( GetFont() );
4063
4064 const size_t count = GetItemCount();
4065
4066 int iconSpacing;
4067 if ( HasFlag(wxLC_ICON) )
4068 iconSpacing = m_normal_spacing;
4069 else if ( HasFlag(wxLC_SMALL_ICON) )
4070 iconSpacing = m_small_spacing;
4071 else
4072 iconSpacing = 0;
4073
4074 // Note that we do not call GetClientSize() here but
4075 // GetSize() and subtract the border size for sunken
4076 // borders manually. This is technically incorrect,
4077 // but we need to know the client area's size WITHOUT
4078 // scrollbars here. Since we don't know if there are
4079 // any scrollbars, we use GetSize() instead. Another
4080 // solution would be to call SetScrollbars() here to
4081 // remove the scrollbars and call GetClientSize() then,
4082 // but this might result in flicker and - worse - will
4083 // reset the scrollbars to 0 which is not good at all
4084 // if you resize a dialog/window, but don't want to
4085 // reset the window scrolling. RR.
4086 // Furthermore, we actually do NOT subtract the border
4087 // width as 2 pixels is just the extra space which we
4088 // need around the actual content in the window. Other-
4089 // wise the text would e.g. touch the upper border. RR.
4090 int clientWidth,
4091 clientHeight;
4092 GetSize( &clientWidth, &clientHeight );
4093
4094 if ( InReportView() )
4095 {
4096 // all lines have the same height and we scroll one line per step
4097 int entireHeight = count * lineHeight + LINE_SPACING;
4098
4099 m_linesPerPage = clientHeight / lineHeight;
4100
4101 ResetVisibleLinesRange();
4102
4103 SetScrollbars( SCROLL_UNIT_X, lineHeight,
4104 GetHeaderWidth() / SCROLL_UNIT_X,
4105 (entireHeight + lineHeight - 1) / lineHeight,
4106 GetScrollPos(wxHORIZONTAL),
4107 GetScrollPos(wxVERTICAL),
4108 true );
4109 }
4110 else // !report
4111 {
4112 // we have 3 different layout strategies: either layout all items
4113 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
4114 // to arrange them in top to bottom, left to right (don't ask me why
4115 // not the other way round...) order
4116 if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) )
4117 {
4118 int x = EXTRA_BORDER_X;
4119 int y = EXTRA_BORDER_Y;
4120
4121 wxCoord widthMax = 0;
4122
4123 size_t i;
4124 for ( i = 0; i < count; i++ )
4125 {
4126 wxListLineData *line = GetLine(i);
4127 line->CalculateSize( &dc, iconSpacing );
4128 line->SetPosition( x, y, iconSpacing );
4129
4130 wxSize sizeLine = GetLineSize(i);
4131
4132 if ( HasFlag(wxLC_ALIGN_TOP) )
4133 {
4134 if ( sizeLine.x > widthMax )
4135 widthMax = sizeLine.x;
4136
4137 y += sizeLine.y;
4138 }
4139 else // wxLC_ALIGN_LEFT
4140 {
4141 x += sizeLine.x + MARGIN_BETWEEN_ROWS;
4142 }
4143 }
4144
4145 if ( HasFlag(wxLC_ALIGN_TOP) )
4146 {
4147 // traverse the items again and tweak their sizes so that they are
4148 // all the same in a row
4149 for ( i = 0; i < count; i++ )
4150 {
4151 wxListLineData *line = GetLine(i);
4152 line->m_gi->ExtendWidth(widthMax);
4153 }
4154 }
4155
4156 SetScrollbars
4157 (
4158 SCROLL_UNIT_X,
4159 lineHeight,
4160 (x + SCROLL_UNIT_X) / SCROLL_UNIT_X,
4161 (y + lineHeight) / lineHeight,
4162 GetScrollPos( wxHORIZONTAL ),
4163 GetScrollPos( wxVERTICAL ),
4164 true
4165 );
4166 }
4167 else // "flowed" arrangement, the most complicated case
4168 {
4169 // at first we try without any scrollbars, if the items don't fit into
4170 // the window, we recalculate after subtracting the space taken by the
4171 // scrollbar
4172
4173 int entireWidth = 0;
4174
4175 for (int tries = 0; tries < 2; tries++)
4176 {
4177 entireWidth = 2 * EXTRA_BORDER_X;
4178
4179 if (tries == 1)
4180 {
4181 // Now we have decided that the items do not fit into the
4182 // client area, so we need a scrollbar
4183 entireWidth += SCROLL_UNIT_X;
4184 }
4185
4186 int x = EXTRA_BORDER_X;
4187 int y = EXTRA_BORDER_Y;
4188 int maxWidthInThisRow = 0;
4189
4190 m_linesPerPage = 0;
4191 int currentlyVisibleLines = 0;
4192
4193 for (size_t i = 0; i < count; i++)
4194 {
4195 currentlyVisibleLines++;
4196 wxListLineData *line = GetLine( i );
4197 line->CalculateSize( &dc, iconSpacing );
4198 line->SetPosition( x, y, iconSpacing );
4199
4200 wxSize sizeLine = GetLineSize( i );
4201
4202 if ( maxWidthInThisRow < sizeLine.x )
4203 maxWidthInThisRow = sizeLine.x;
4204
4205 y += sizeLine.y;
4206 if (currentlyVisibleLines > m_linesPerPage)
4207 m_linesPerPage = currentlyVisibleLines;
4208
4209 if ( y + sizeLine.y >= clientHeight )
4210 {
4211 currentlyVisibleLines = 0;
4212 y = EXTRA_BORDER_Y;
4213 maxWidthInThisRow += MARGIN_BETWEEN_ROWS;
4214 x += maxWidthInThisRow;
4215 entireWidth += maxWidthInThisRow;
4216 maxWidthInThisRow = 0;
4217 }
4218
4219 // We have reached the last item.
4220 if ( i == count - 1 )
4221 entireWidth += maxWidthInThisRow;
4222
4223 if ( (tries == 0) &&
4224 (entireWidth + SCROLL_UNIT_X > clientWidth) )
4225 {
4226 clientHeight -= wxSystemSettings::
4227 GetMetric(wxSYS_HSCROLL_Y);
4228 m_linesPerPage = 0;
4229 break;
4230 }
4231
4232 if ( i == count - 1 )
4233 tries = 1; // Everything fits, no second try required.
4234 }
4235 }
4236
4237 SetScrollbars
4238 (
4239 SCROLL_UNIT_X,
4240 lineHeight,
4241 (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X,
4242 0,
4243 GetScrollPos( wxHORIZONTAL ),
4244 0,
4245 true
4246 );
4247 }
4248 }
4249
4250 if ( !noRefresh )
4251 {
4252 // FIXME: why should we call it from here?
4253 UpdateCurrent();
4254
4255 RefreshAll();
4256 }
4257 }
4258
4259 void wxListMainWindow::RefreshAll()
4260 {
4261 m_dirty = false;
4262 Refresh();
4263
4264 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
4265 if ( headerWin && headerWin->m_dirty )
4266 {
4267 headerWin->m_dirty = false;
4268 headerWin->Refresh();
4269 }
4270 }
4271
4272 void wxListMainWindow::UpdateCurrent()
4273 {
4274 if ( !HasCurrent() && !IsEmpty() )
4275 ChangeCurrent(0);
4276 }
4277
4278 long wxListMainWindow::GetNextItem( long item,
4279 int WXUNUSED(geometry),
4280 int state ) const
4281 {
4282 long ret = item,
4283 max = GetItemCount();
4284 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
4285 _T("invalid listctrl index in GetNextItem()") );
4286
4287 // notice that we start with the next item (or the first one if item == -1)
4288 // and this is intentional to allow writing a simple loop to iterate over
4289 // all selected items
4290 ret++;
4291 if ( ret == max )
4292 // this is not an error because the index was OK initially,
4293 // just no such item
4294 return -1;
4295
4296 if ( !state )
4297 // any will do
4298 return (size_t)ret;
4299
4300 size_t count = GetItemCount();
4301 for ( size_t line = (size_t)ret; line < count; line++ )
4302 {
4303 if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
4304 return line;
4305
4306 if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
4307 return line;
4308 }
4309
4310 return -1;
4311 }
4312
4313 // ----------------------------------------------------------------------------
4314 // deleting stuff
4315 // ----------------------------------------------------------------------------
4316
4317 void wxListMainWindow::DeleteItem( long lindex )
4318 {
4319 size_t count = GetItemCount();
4320
4321 wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
4322 _T("invalid item index in DeleteItem") );
4323
4324 size_t index = (size_t)lindex;
4325
4326 // we don't need to adjust the index for the previous items
4327 if ( HasCurrent() && m_current >= index )
4328 {
4329 // if the current item is being deleted, we want the next one to
4330 // become selected - unless there is no next one - so don't adjust
4331 // m_current in this case
4332 if ( m_current != index || m_current == count - 1 )
4333 m_current--;
4334 }
4335
4336 if ( InReportView() )
4337 {
4338 // mark the Column Max Width cache as dirty if the items in the line
4339 // we're deleting contain the Max Column Width
4340 wxListLineData * const line = GetLine(index);
4341 wxListItemDataList::compatibility_iterator n;
4342 wxListItemData *itemData;
4343 wxListItem item;
4344 int itemWidth;
4345
4346 for (size_t i = 0; i < m_columns.GetCount(); i++)
4347 {
4348 n = line->m_items.Item( i );
4349 itemData = n->GetData();
4350 itemData->GetItem(item);
4351
4352 itemWidth = GetItemWidthWithImage(&item);
4353
4354 if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth)
4355 m_aColWidths.Item(i)->bNeedsUpdate = true;
4356 }
4357
4358 ResetVisibleLinesRange();
4359 }
4360
4361 if ( IsVirtual() )
4362 {
4363 m_countVirt--;
4364 m_selStore.OnItemDelete(index);
4365 }
4366 else
4367 {
4368 m_lines.RemoveAt( index );
4369 }
4370
4371 // we need to refresh the (vert) scrollbar as the number of items changed
4372 m_dirty = true;
4373
4374 SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM );
4375
4376 RefreshAfter(index);
4377 }
4378
4379 void wxListMainWindow::DeleteColumn( int col )
4380 {
4381 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
4382
4383 wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
4384
4385 m_dirty = true;
4386 delete node->GetData();
4387 m_columns.Erase( node );
4388
4389 if ( !IsVirtual() )
4390 {
4391 // update all the items
4392 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4393 {
4394 wxListLineData * const line = GetLine(i);
4395 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
4396 delete n->GetData();
4397 line->m_items.Erase(n);
4398 }
4399 }
4400
4401 if ( InReportView() ) // we only cache max widths when in Report View
4402 {
4403 delete m_aColWidths.Item(col);
4404 m_aColWidths.RemoveAt(col);
4405 }
4406
4407 // invalidate it as it has to be recalculated
4408 m_headerWidth = 0;
4409 }
4410
4411 void wxListMainWindow::DoDeleteAllItems()
4412 {
4413 if ( IsEmpty() )
4414 // nothing to do - in particular, don't send the event
4415 return;
4416
4417 ResetCurrent();
4418
4419 // to make the deletion of all items faster, we don't send the
4420 // notifications for each item deletion in this case but only one event
4421 // for all of them: this is compatible with wxMSW and documented in
4422 // DeleteAllItems() description
4423
4424 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
4425 event.SetEventObject( GetParent() );
4426 GetParent()->GetEventHandler()->ProcessEvent( event );
4427
4428 if ( IsVirtual() )
4429 {
4430 m_countVirt = 0;
4431 m_selStore.Clear();
4432 }
4433
4434 if ( InReportView() )
4435 {
4436 ResetVisibleLinesRange();
4437 for (size_t i = 0; i < m_aColWidths.GetCount(); i++)
4438 {
4439 m_aColWidths.Item(i)->bNeedsUpdate = true;
4440 }
4441 }
4442
4443 m_lines.Clear();
4444 }
4445
4446 void wxListMainWindow::DeleteAllItems()
4447 {
4448 DoDeleteAllItems();
4449
4450 RecalculatePositions();
4451 }
4452
4453 void wxListMainWindow::DeleteEverything()
4454 {
4455 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
4456 WX_CLEAR_ARRAY(m_aColWidths);
4457
4458 DeleteAllItems();
4459 }
4460
4461 // ----------------------------------------------------------------------------
4462 // scanning for an item
4463 // ----------------------------------------------------------------------------
4464
4465 void wxListMainWindow::EnsureVisible( long index )
4466 {
4467 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4468 _T("invalid index in EnsureVisible") );
4469
4470 // We have to call this here because the label in question might just have
4471 // been added and its position is not known yet
4472 if ( m_dirty )
4473 RecalculatePositions(true /* no refresh */);
4474
4475 MoveToItem((size_t)index);
4476 }
4477
4478 long wxListMainWindow::FindItem(long start, const wxString& str, bool WXUNUSED(partial) )
4479 {
4480 long pos = start;
4481 wxString tmp = str;
4482 if (pos < 0)
4483 pos = 0;
4484
4485 size_t count = GetItemCount();
4486 for ( size_t i = (size_t)pos; i < count; i++ )
4487 {
4488 wxListLineData *line = GetLine(i);
4489 if ( line->GetText(0) == tmp )
4490 return i;
4491 }
4492
4493 return wxNOT_FOUND;
4494 }
4495
4496 long wxListMainWindow::FindItem(long start, wxUIntPtr data)
4497 {
4498 long pos = start;
4499 if (pos < 0)
4500 pos = 0;
4501
4502 size_t count = GetItemCount();
4503 for (size_t i = (size_t)pos; i < count; i++)
4504 {
4505 wxListLineData *line = GetLine(i);
4506 wxListItem item;
4507 line->GetItem( 0, item );
4508 if (item.m_data == data)
4509 return i;
4510 }
4511
4512 return wxNOT_FOUND;
4513 }
4514
4515 long wxListMainWindow::FindItem( const wxPoint& pt )
4516 {
4517 size_t topItem;
4518 GetVisibleLinesRange( &topItem, NULL );
4519
4520 wxPoint p;
4521 GetItemPosition( GetItemCount() - 1, p );
4522 if ( p.y == 0 )
4523 return topItem;
4524
4525 long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem );
4526 if ( id >= 0 && id < (long)GetItemCount() )
4527 return id;
4528
4529 return wxNOT_FOUND;
4530 }
4531
4532 long wxListMainWindow::HitTest( int x, int y, int &flags ) const
4533 {
4534 CalcUnscrolledPosition( x, y, &x, &y );
4535
4536 size_t count = GetItemCount();
4537
4538 if ( InReportView() )
4539 {
4540 size_t current = y / GetLineHeight();
4541 if ( current < count )
4542 {
4543 flags = HitTestLine(current, x, y);
4544 if ( flags )
4545 return current;
4546 }
4547 }
4548 else // !report
4549 {
4550 // TODO: optimize it too! this is less simple than for report view but
4551 // enumerating all items is still not a way to do it!!
4552 for ( size_t current = 0; current < count; current++ )
4553 {
4554 flags = HitTestLine(current, x, y);
4555 if ( flags )
4556 return current;
4557 }
4558 }
4559
4560 return wxNOT_FOUND;
4561 }
4562
4563 // ----------------------------------------------------------------------------
4564 // adding stuff
4565 // ----------------------------------------------------------------------------
4566
4567 void wxListMainWindow::InsertItem( wxListItem &item )
4568 {
4569 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4570
4571 int count = GetItemCount();
4572 wxCHECK_RET( item.m_itemId >= 0, _T("invalid item index") );
4573
4574 if (item.m_itemId > count)
4575 item.m_itemId = count;
4576
4577 size_t id = item.m_itemId;
4578
4579 m_dirty = true;
4580
4581 if ( InReportView() )
4582 {
4583 ResetVisibleLinesRange();
4584
4585 // calculate the width of the item and adjust the max column width
4586 wxColWidthInfo *pWidthInfo = m_aColWidths.Item(item.GetColumn());
4587 int width = GetItemWidthWithImage(&item);
4588 item.SetWidth(width);
4589 if (width > pWidthInfo->nMaxWidth)
4590 pWidthInfo->nMaxWidth = width;
4591 }
4592
4593 wxListLineData *line = new wxListLineData(this);
4594
4595 line->SetItem( item.m_col, item );
4596
4597 m_lines.Insert( line, id );
4598
4599 m_dirty = true;
4600
4601 // If an item is selected at or below the point of insertion, we need to
4602 // increment the member variables because the current row's index has gone
4603 // up by one
4604 if ( HasCurrent() && m_current >= id )
4605 m_current++;
4606
4607 SendNotify(id, wxEVT_COMMAND_LIST_INSERT_ITEM);
4608
4609 RefreshLines(id, GetItemCount() - 1);
4610 }
4611
4612 void wxListMainWindow::InsertColumn( long col, wxListItem &item )
4613 {
4614 m_dirty = true;
4615 if ( InReportView() )
4616 {
4617 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
4618 item.m_width = GetTextLength( item.m_text );
4619
4620 wxListHeaderData *column = new wxListHeaderData( item );
4621 wxColWidthInfo *colWidthInfo = new wxColWidthInfo();
4622
4623 bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4624 if ( insert )
4625 {
4626 wxListHeaderDataList::compatibility_iterator
4627 node = m_columns.Item( col );
4628 m_columns.Insert( node, column );
4629 m_aColWidths.Insert( colWidthInfo, col );
4630 }
4631 else
4632 {
4633 m_columns.Append( column );
4634 m_aColWidths.Add( colWidthInfo );
4635 }
4636
4637 if ( !IsVirtual() )
4638 {
4639 // update all the items
4640 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4641 {
4642 wxListLineData * const line = GetLine(i);
4643 wxListItemData * const data = new wxListItemData(this);
4644 if ( insert )
4645 line->m_items.Insert(col, data);
4646 else
4647 line->m_items.Append(data);
4648 }
4649 }
4650
4651 // invalidate it as it has to be recalculated
4652 m_headerWidth = 0;
4653 }
4654 }
4655
4656 int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
4657 {
4658 int width = 0;
4659 wxClientDC dc(this);
4660
4661 dc.SetFont( GetFont() );
4662
4663 if (item->GetImage() != -1)
4664 {
4665 int ix, iy;
4666 GetImageSize( item->GetImage(), ix, iy );
4667 width += ix + 5;
4668 }
4669
4670 if (!item->GetText().empty())
4671 {
4672 wxCoord w;
4673 dc.GetTextExtent( item->GetText(), &w, NULL );
4674 width += w;
4675 }
4676
4677 return width;
4678 }
4679
4680 // ----------------------------------------------------------------------------
4681 // sorting
4682 // ----------------------------------------------------------------------------
4683
4684 wxListCtrlCompare list_ctrl_compare_func_2;
4685 long list_ctrl_compare_data;
4686
4687 int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
4688 {
4689 wxListLineData *line1 = *arg1;
4690 wxListLineData *line2 = *arg2;
4691 wxListItem item;
4692 line1->GetItem( 0, item );
4693 wxUIntPtr data1 = item.m_data;
4694 line2->GetItem( 0, item );
4695 wxUIntPtr data2 = item.m_data;
4696 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
4697 }
4698
4699 void wxListMainWindow::SortItems( wxListCtrlCompare fn, long data )
4700 {
4701 list_ctrl_compare_func_2 = fn;
4702 list_ctrl_compare_data = data;
4703 m_lines.Sort( list_ctrl_compare_func_1 );
4704 m_dirty = true;
4705 }
4706
4707 // ----------------------------------------------------------------------------
4708 // scrolling
4709 // ----------------------------------------------------------------------------
4710
4711 void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4712 {
4713 // update our idea of which lines are shown when we redraw the window the
4714 // next time
4715 ResetVisibleLinesRange();
4716
4717 // FIXME
4718 #if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
4719 wxScrolledWindow::OnScroll(event);
4720 #else
4721 HandleOnScroll( event );
4722 #endif
4723
4724 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4725 {
4726 wxGenericListCtrl* lc = GetListCtrl();
4727 wxCHECK_RET( lc, _T("no listctrl window?") );
4728
4729 lc->m_headerWin->Refresh();
4730 lc->m_headerWin->Update();
4731 }
4732 }
4733
4734 int wxListMainWindow::GetCountPerPage() const
4735 {
4736 if ( !m_linesPerPage )
4737 {
4738 wxConstCast(this, wxListMainWindow)->
4739 m_linesPerPage = GetClientSize().y / GetLineHeight();
4740 }
4741
4742 return m_linesPerPage;
4743 }
4744
4745 void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
4746 {
4747 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
4748
4749 if ( m_lineFrom == (size_t)-1 )
4750 {
4751 size_t count = GetItemCount();
4752 if ( count )
4753 {
4754 m_lineFrom = GetScrollPos(wxVERTICAL);
4755
4756 // this may happen if SetScrollbars() hadn't been called yet
4757 if ( m_lineFrom >= count )
4758 m_lineFrom = count - 1;
4759
4760 // we redraw one extra line but this is needed to make the redrawing
4761 // logic work when there is a fractional number of lines on screen
4762 m_lineTo = m_lineFrom + m_linesPerPage;
4763 if ( m_lineTo >= count )
4764 m_lineTo = count - 1;
4765 }
4766 else // empty control
4767 {
4768 m_lineFrom = 0;
4769 m_lineTo = (size_t)-1;
4770 }
4771 }
4772
4773 wxASSERT_MSG( IsEmpty() ||
4774 (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
4775 _T("GetVisibleLinesRange() returns incorrect result") );
4776
4777 if ( from )
4778 *from = m_lineFrom;
4779 if ( to )
4780 *to = m_lineTo;
4781 }
4782
4783 // -------------------------------------------------------------------------------------
4784 // wxGenericListCtrl
4785 // -------------------------------------------------------------------------------------
4786
4787 IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl, wxControl)
4788
4789 BEGIN_EVENT_TABLE(wxGenericListCtrl,wxControl)
4790 EVT_SIZE(wxGenericListCtrl::OnSize)
4791 END_EVENT_TABLE()
4792
4793 wxGenericListCtrl::wxGenericListCtrl()
4794 {
4795 m_imageListNormal = (wxImageList *) NULL;
4796 m_imageListSmall = (wxImageList *) NULL;
4797 m_imageListState = (wxImageList *) NULL;
4798
4799 m_ownsImageListNormal =
4800 m_ownsImageListSmall =
4801 m_ownsImageListState = false;
4802
4803 m_mainWin = (wxListMainWindow*) NULL;
4804 m_headerWin = (wxListHeaderWindow*) NULL;
4805 m_headerHeight = 0;
4806 }
4807
4808 wxGenericListCtrl::~wxGenericListCtrl()
4809 {
4810 if (m_ownsImageListNormal)
4811 delete m_imageListNormal;
4812 if (m_ownsImageListSmall)
4813 delete m_imageListSmall;
4814 if (m_ownsImageListState)
4815 delete m_imageListState;
4816 }
4817
4818 void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4819 {
4820 if ( m_headerWin )
4821 {
4822 #ifdef __WXMAC__
4823 SInt32 h;
4824 GetThemeMetric( kThemeMetricListHeaderHeight, &h );
4825 #else
4826 // we use 'g' to get the descent, too
4827 int w, h, d;
4828 m_headerWin->GetTextExtent(wxT("Hg"), &w, &h, &d);
4829 h += d + 2 * HEADER_OFFSET_Y + EXTRA_HEIGHT;
4830 #endif
4831
4832 // only update if changed
4833 if ( h != m_headerHeight )
4834 {
4835 m_headerHeight = h;
4836
4837 if ( HasHeader() )
4838 ResizeReportView(true);
4839 else //why is this needed if it doesn't have a header?
4840 m_headerWin->SetSize(m_headerWin->GetSize().x, m_headerHeight);
4841 }
4842 }
4843 }
4844
4845 void wxGenericListCtrl::CreateHeaderWindow()
4846 {
4847 m_headerWin = new wxListHeaderWindow
4848 (
4849 this, wxID_ANY, m_mainWin,
4850 wxPoint(0,0),
4851 wxSize(GetClientSize().x, m_headerHeight),
4852 wxTAB_TRAVERSAL
4853 );
4854 CalculateAndSetHeaderHeight();
4855 }
4856
4857 bool wxGenericListCtrl::Create(wxWindow *parent,
4858 wxWindowID id,
4859 const wxPoint &pos,
4860 const wxSize &size,
4861 long style,
4862 const wxValidator &validator,
4863 const wxString &name)
4864 {
4865 m_imageListNormal =
4866 m_imageListSmall =
4867 m_imageListState = (wxImageList *) NULL;
4868 m_ownsImageListNormal =
4869 m_ownsImageListSmall =
4870 m_ownsImageListState = false;
4871
4872 m_mainWin = (wxListMainWindow*) NULL;
4873 m_headerWin = (wxListHeaderWindow*) NULL;
4874
4875 m_headerHeight = 0;
4876
4877 if ( !(style & wxLC_MASK_TYPE) )
4878 {
4879 style = style | wxLC_LIST;
4880 }
4881
4882 if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) )
4883 return false;
4884
4885 // don't create the inner window with the border
4886 style &= ~wxBORDER_MASK;
4887
4888 m_mainWin = new wxListMainWindow( this, wxID_ANY, wxPoint(0, 0), size, style );
4889
4890 #ifdef __WXMAC_CARBON__
4891 // Human Interface Guidelines ask us for a special font in this case
4892 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL )
4893 {
4894 wxFont font;
4895 font.MacCreateThemeFont( kThemeViewsFont );
4896 SetFont( font );
4897 }
4898 #endif
4899
4900 if ( InReportView() )
4901 {
4902 CreateHeaderWindow();
4903
4904 #ifdef __WXMAC_CARBON__
4905 if (m_headerWin)
4906 {
4907 wxFont font;
4908 font.MacCreateThemeFont( kThemeSmallSystemFont );
4909 m_headerWin->SetFont( font );
4910 CalculateAndSetHeaderHeight();
4911 }
4912 #endif
4913
4914 if ( HasFlag(wxLC_NO_HEADER) )
4915 // VZ: why do we create it at all then?
4916 m_headerWin->Show( false );
4917 }
4918
4919 SetInitialSize(size);
4920
4921 return true;
4922 }
4923
4924 void wxGenericListCtrl::SetSingleStyle( long style, bool add )
4925 {
4926 wxASSERT_MSG( !(style & wxLC_VIRTUAL),
4927 _T("wxLC_VIRTUAL can't be [un]set") );
4928
4929 long flag = GetWindowStyle();
4930
4931 if (add)
4932 {
4933 if (style & wxLC_MASK_TYPE)
4934 flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
4935 if (style & wxLC_MASK_ALIGN)
4936 flag &= ~wxLC_MASK_ALIGN;
4937 if (style & wxLC_MASK_SORT)
4938 flag &= ~wxLC_MASK_SORT;
4939 }
4940
4941 if (add)
4942 flag |= style;
4943 else
4944 flag &= ~style;
4945
4946 SetWindowStyleFlag( flag );
4947 }
4948
4949 void wxGenericListCtrl::SetWindowStyleFlag( long flag )
4950 {
4951 if (m_mainWin)
4952 {
4953 m_mainWin->DeleteEverything();
4954
4955 // has the header visibility changed?
4956 bool hasHeader = HasHeader();
4957 bool willHaveHeader = (flag & wxLC_REPORT) && !(flag & wxLC_NO_HEADER);
4958
4959 if ( hasHeader != willHaveHeader )
4960 {
4961 // toggle it
4962 if ( hasHeader )
4963 {
4964 if ( m_headerWin )
4965 {
4966 // don't delete, just hide, as we can reuse it later
4967 m_headerWin->Show(false);
4968 }
4969 //else: nothing to do
4970 }
4971 else // must show header
4972 {
4973 if (!m_headerWin)
4974 {
4975 CreateHeaderWindow();
4976 }
4977 else // already have it, just show
4978 {
4979 m_headerWin->Show( true );
4980 }
4981 }
4982
4983 ResizeReportView(willHaveHeader);
4984 }
4985 }
4986
4987 wxWindow::SetWindowStyleFlag( flag );
4988 }
4989
4990 bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
4991 {
4992 m_mainWin->GetColumn( col, item );
4993 return true;
4994 }
4995
4996 bool wxGenericListCtrl::SetColumn( int col, wxListItem& item )
4997 {
4998 m_mainWin->SetColumn( col, item );
4999 return true;
5000 }
5001
5002 int wxGenericListCtrl::GetColumnWidth( int col ) const
5003 {
5004 return m_mainWin->GetColumnWidth( col );
5005 }
5006
5007 bool wxGenericListCtrl::SetColumnWidth( int col, int width )
5008 {
5009 m_mainWin->SetColumnWidth( col, width );
5010 return true;
5011 }
5012
5013 int wxGenericListCtrl::GetCountPerPage() const
5014 {
5015 return m_mainWin->GetCountPerPage(); // different from Windows ?
5016 }
5017
5018 bool wxGenericListCtrl::GetItem( wxListItem &info ) const
5019 {
5020 m_mainWin->GetItem( info );
5021 return true;
5022 }
5023
5024 bool wxGenericListCtrl::SetItem( wxListItem &info )
5025 {
5026 m_mainWin->SetItem( info );
5027 return true;
5028 }
5029
5030 long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
5031 {
5032 wxListItem info;
5033 info.m_text = label;
5034 info.m_mask = wxLIST_MASK_TEXT;
5035 info.m_itemId = index;
5036 info.m_col = col;
5037 if ( imageId > -1 )
5038 {
5039 info.m_image = imageId;
5040 info.m_mask |= wxLIST_MASK_IMAGE;
5041 }
5042
5043 m_mainWin->SetItem(info);
5044 return true;
5045 }
5046
5047 int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
5048 {
5049 return m_mainWin->GetItemState( item, stateMask );
5050 }
5051
5052 bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
5053 {
5054 m_mainWin->SetItemState( item, state, stateMask );
5055 return true;
5056 }
5057
5058 bool
5059 wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
5060 {
5061 return SetItemColumnImage(item, 0, image);
5062 }
5063
5064 bool
5065 wxGenericListCtrl::SetItemColumnImage( long item, long column, int image )
5066 {
5067 wxListItem info;
5068 info.m_image = image;
5069 info.m_mask = wxLIST_MASK_IMAGE;
5070 info.m_itemId = item;
5071 info.m_col = column;
5072 m_mainWin->SetItem( info );
5073 return true;
5074 }
5075
5076 wxString wxGenericListCtrl::GetItemText( long item ) const
5077 {
5078 return m_mainWin->GetItemText(item);
5079 }
5080
5081 void wxGenericListCtrl::SetItemText( long item, const wxString& str )
5082 {
5083 m_mainWin->SetItemText(item, str);
5084 }
5085
5086 wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const
5087 {
5088 wxListItem info;
5089 info.m_mask = wxLIST_MASK_DATA;
5090 info.m_itemId = item;
5091 m_mainWin->GetItem( info );
5092 return info.m_data;
5093 }
5094
5095 bool wxGenericListCtrl::SetItemData( long item, long data )
5096 {
5097 wxListItem info;
5098 info.m_mask = wxLIST_MASK_DATA;
5099 info.m_itemId = item;
5100 info.m_data = data;
5101 m_mainWin->SetItem( info );
5102 return true;
5103 }
5104
5105 wxRect wxGenericListCtrl::GetViewRect() const
5106 {
5107 return m_mainWin->GetViewRect();
5108 }
5109
5110 bool wxGenericListCtrl::GetItemRect( long item, wxRect &rect, int WXUNUSED(code) ) const
5111 {
5112 m_mainWin->GetItemRect( item, rect );
5113 if ( m_mainWin->HasHeader() )
5114 rect.y += m_headerHeight + 1;
5115 return true;
5116 }
5117
5118 bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
5119 {
5120 m_mainWin->GetItemPosition( item, pos );
5121 return true;
5122 }
5123
5124 bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
5125 {
5126 return 0;
5127 }
5128
5129 int wxGenericListCtrl::GetItemCount() const
5130 {
5131 return m_mainWin->GetItemCount();
5132 }
5133
5134 int wxGenericListCtrl::GetColumnCount() const
5135 {
5136 return m_mainWin->GetColumnCount();
5137 }
5138
5139 void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
5140 {
5141 m_mainWin->SetItemSpacing( spacing, isSmall );
5142 }
5143
5144 wxSize wxGenericListCtrl::GetItemSpacing() const
5145 {
5146 const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON));
5147
5148 return wxSize(spacing, spacing);
5149 }
5150
5151 #if WXWIN_COMPATIBILITY_2_6
5152 int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const
5153 {
5154 return m_mainWin->GetItemSpacing( isSmall );
5155 }
5156 #endif // WXWIN_COMPATIBILITY_2_6
5157
5158 void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
5159 {
5160 wxListItem info;
5161 info.m_itemId = item;
5162 info.SetTextColour( col );
5163 m_mainWin->SetItem( info );
5164 }
5165
5166 wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
5167 {
5168 wxListItem info;
5169 info.m_itemId = item;
5170 m_mainWin->GetItem( info );
5171 return info.GetTextColour();
5172 }
5173
5174 void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
5175 {
5176 wxListItem info;
5177 info.m_itemId = item;
5178 info.SetBackgroundColour( col );
5179 m_mainWin->SetItem( info );
5180 }
5181
5182 wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
5183 {
5184 wxListItem info;
5185 info.m_itemId = item;
5186 m_mainWin->GetItem( info );
5187 return info.GetBackgroundColour();
5188 }
5189
5190 void wxGenericListCtrl::SetItemFont( long item, const wxFont &f )
5191 {
5192 wxListItem info;
5193 info.m_itemId = item;
5194 info.SetFont( f );
5195 m_mainWin->SetItem( info );
5196 }
5197
5198 wxFont wxGenericListCtrl::GetItemFont( long item ) const
5199 {
5200 wxListItem info;
5201 info.m_itemId = item;
5202 m_mainWin->GetItem( info );
5203 return info.GetFont();
5204 }
5205
5206 int wxGenericListCtrl::GetSelectedItemCount() const
5207 {
5208 return m_mainWin->GetSelectedItemCount();
5209 }
5210
5211 wxColour wxGenericListCtrl::GetTextColour() const
5212 {
5213 return GetForegroundColour();
5214 }
5215
5216 void wxGenericListCtrl::SetTextColour(const wxColour& col)
5217 {
5218 SetForegroundColour(col);
5219 }
5220
5221 long wxGenericListCtrl::GetTopItem() const
5222 {
5223 size_t top;
5224 m_mainWin->GetVisibleLinesRange(&top, NULL);
5225 return (long)top;
5226 }
5227
5228 long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
5229 {
5230 return m_mainWin->GetNextItem( item, geom, state );
5231 }
5232
5233 wxImageList *wxGenericListCtrl::GetImageList(int which) const
5234 {
5235 if (which == wxIMAGE_LIST_NORMAL)
5236 return m_imageListNormal;
5237 else if (which == wxIMAGE_LIST_SMALL)
5238 return m_imageListSmall;
5239 else if (which == wxIMAGE_LIST_STATE)
5240 return m_imageListState;
5241
5242 return (wxImageList *) NULL;
5243 }
5244
5245 void wxGenericListCtrl::SetImageList( wxImageList *imageList, int which )
5246 {
5247 if ( which == wxIMAGE_LIST_NORMAL )
5248 {
5249 if (m_ownsImageListNormal)
5250 delete m_imageListNormal;
5251 m_imageListNormal = imageList;
5252 m_ownsImageListNormal = false;
5253 }
5254 else if ( which == wxIMAGE_LIST_SMALL )
5255 {
5256 if (m_ownsImageListSmall)
5257 delete m_imageListSmall;
5258 m_imageListSmall = imageList;
5259 m_ownsImageListSmall = false;
5260 }
5261 else if ( which == wxIMAGE_LIST_STATE )
5262 {
5263 if (m_ownsImageListState)
5264 delete m_imageListState;
5265 m_imageListState = imageList;
5266 m_ownsImageListState = false;
5267 }
5268
5269 m_mainWin->SetImageList( imageList, which );
5270 }
5271
5272 void wxGenericListCtrl::AssignImageList(wxImageList *imageList, int which)
5273 {
5274 SetImageList(imageList, which);
5275 if ( which == wxIMAGE_LIST_NORMAL )
5276 m_ownsImageListNormal = true;
5277 else if ( which == wxIMAGE_LIST_SMALL )
5278 m_ownsImageListSmall = true;
5279 else if ( which == wxIMAGE_LIST_STATE )
5280 m_ownsImageListState = true;
5281 }
5282
5283 bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
5284 {
5285 return 0;
5286 }
5287
5288 bool wxGenericListCtrl::DeleteItem( long item )
5289 {
5290 m_mainWin->DeleteItem( item );
5291 return true;
5292 }
5293
5294 bool wxGenericListCtrl::DeleteAllItems()
5295 {
5296 m_mainWin->DeleteAllItems();
5297 return true;
5298 }
5299
5300 bool wxGenericListCtrl::DeleteAllColumns()
5301 {
5302 size_t count = m_mainWin->m_columns.GetCount();
5303 for ( size_t n = 0; n < count; n++ )
5304 DeleteColumn( 0 );
5305 return true;
5306 }
5307
5308 void wxGenericListCtrl::ClearAll()
5309 {
5310 m_mainWin->DeleteEverything();
5311 }
5312
5313 bool wxGenericListCtrl::DeleteColumn( int col )
5314 {
5315 m_mainWin->DeleteColumn( col );
5316
5317 // if we don't have the header any longer, we need to relayout the window
5318 if ( !GetColumnCount() )
5319 ResizeReportView(false /* no header */);
5320 return true;
5321 }
5322
5323 wxTextCtrl *wxGenericListCtrl::EditLabel(long item,
5324 wxClassInfo* textControlClass)
5325 {
5326 return m_mainWin->EditLabel( item, textControlClass );
5327 }
5328
5329 wxTextCtrl *wxGenericListCtrl::GetEditControl() const
5330 {
5331 return m_mainWin->GetEditControl();
5332 }
5333
5334 bool wxGenericListCtrl::EnsureVisible( long item )
5335 {
5336 m_mainWin->EnsureVisible( item );
5337 return true;
5338 }
5339
5340 long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
5341 {
5342 return m_mainWin->FindItem( start, str, partial );
5343 }
5344
5345 long wxGenericListCtrl::FindItem( long start, wxUIntPtr data )
5346 {
5347 return m_mainWin->FindItem( start, data );
5348 }
5349
5350 long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt,
5351 int WXUNUSED(direction))
5352 {
5353 return m_mainWin->FindItem( pt );
5354 }
5355
5356 // TODO: sub item hit testing
5357 long wxGenericListCtrl::HitTest(const wxPoint& point, int& flags, long *) const
5358 {
5359 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
5360 }
5361
5362 long wxGenericListCtrl::InsertItem( wxListItem& info )
5363 {
5364 m_mainWin->InsertItem( info );
5365 return info.m_itemId;
5366 }
5367
5368 long wxGenericListCtrl::InsertItem( long index, const wxString &label )
5369 {
5370 wxListItem info;
5371 info.m_text = label;
5372 info.m_mask = wxLIST_MASK_TEXT;
5373 info.m_itemId = index;
5374 return InsertItem( info );
5375 }
5376
5377 long wxGenericListCtrl::InsertItem( long index, int imageIndex )
5378 {
5379 wxListItem info;
5380 info.m_mask = wxLIST_MASK_IMAGE;
5381 info.m_image = imageIndex;
5382 info.m_itemId = index;
5383 return InsertItem( info );
5384 }
5385
5386 long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
5387 {
5388 wxListItem info;
5389 info.m_text = label;
5390 info.m_image = imageIndex;
5391 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
5392 info.m_itemId = index;
5393 return InsertItem( info );
5394 }
5395
5396 long wxGenericListCtrl::InsertColumn( long col, wxListItem &item )
5397 {
5398 wxCHECK_MSG( m_headerWin, -1, _T("can't add column in non report mode") );
5399
5400 m_mainWin->InsertColumn( col, item );
5401
5402 // if we hadn't had a header before but have one now
5403 // then we need to relayout the window
5404 if ( GetColumnCount() == 1 && m_mainWin->HasHeader() )
5405 ResizeReportView(true /* have header */);
5406
5407 m_headerWin->Refresh();
5408
5409 return 0;
5410 }
5411
5412 long wxGenericListCtrl::InsertColumn( long col, const wxString &heading,
5413 int format, int width )
5414 {
5415 wxListItem item;
5416 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
5417 item.m_text = heading;
5418 if (width >= -2)
5419 {
5420 item.m_mask |= wxLIST_MASK_WIDTH;
5421 item.m_width = width;
5422 }
5423
5424 item.m_format = format;
5425
5426 return InsertColumn( col, item );
5427 }
5428
5429 bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx), int WXUNUSED(dy) )
5430 {
5431 return 0;
5432 }
5433
5434 // Sort items.
5435 // fn is a function which takes 3 long arguments: item1, item2, data.
5436 // item1 is the long data associated with a first item (NOT the index).
5437 // item2 is the long data associated with a second item (NOT the index).
5438 // data is the same value as passed to SortItems.
5439 // The return value is a negative number if the first item should precede the second
5440 // item, a positive number of the second item should precede the first,
5441 // or zero if the two items are equivalent.
5442 // data is arbitrary data to be passed to the sort function.
5443
5444 bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn, long data )
5445 {
5446 m_mainWin->SortItems( fn, data );
5447 return true;
5448 }
5449
5450 // ----------------------------------------------------------------------------
5451 // event handlers
5452 // ----------------------------------------------------------------------------
5453
5454 void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
5455 {
5456 if ( !m_mainWin )
5457 return;
5458
5459 ResizeReportView(m_mainWin->HasHeader());
5460 m_mainWin->RecalculatePositions();
5461 }
5462
5463 void wxGenericListCtrl::ResizeReportView(bool showHeader)
5464 {
5465 int cw, ch;
5466 GetClientSize( &cw, &ch );
5467
5468 if ( showHeader )
5469 {
5470 m_headerWin->SetSize( 0, 0, cw, m_headerHeight );
5471 if(ch > m_headerHeight)
5472 m_mainWin->SetSize( 0, m_headerHeight + 1,
5473 cw, ch - m_headerHeight - 1 );
5474 else
5475 m_mainWin->SetSize( 0, m_headerHeight + 1,
5476 cw, 0);
5477 }
5478 else // no header window
5479 {
5480 m_mainWin->SetSize( 0, 0, cw, ch );
5481 }
5482 }
5483
5484 void wxGenericListCtrl::OnInternalIdle()
5485 {
5486 wxWindow::OnInternalIdle();
5487
5488 // do it only if needed
5489 if ( !m_mainWin->m_dirty )
5490 return;
5491
5492 m_mainWin->RecalculatePositions();
5493 }
5494
5495 // ----------------------------------------------------------------------------
5496 // font/colours
5497 // ----------------------------------------------------------------------------
5498
5499 bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
5500 {
5501 if (m_mainWin)
5502 {
5503 m_mainWin->SetBackgroundColour( colour );
5504 m_mainWin->m_dirty = true;
5505 }
5506
5507 return true;
5508 }
5509
5510 bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
5511 {
5512 if ( !wxWindow::SetForegroundColour( colour ) )
5513 return false;
5514
5515 if (m_mainWin)
5516 {
5517 m_mainWin->SetForegroundColour( colour );
5518 m_mainWin->m_dirty = true;
5519 }
5520
5521 if (m_headerWin)
5522 m_headerWin->SetForegroundColour( colour );
5523
5524 return true;
5525 }
5526
5527 bool wxGenericListCtrl::SetFont( const wxFont &font )
5528 {
5529 if ( !wxWindow::SetFont( font ) )
5530 return false;
5531
5532 if (m_mainWin)
5533 {
5534 m_mainWin->SetFont( font );
5535 m_mainWin->m_dirty = true;
5536 }
5537
5538 if (m_headerWin)
5539 {
5540 m_headerWin->SetFont( font );
5541 CalculateAndSetHeaderHeight();
5542 }
5543
5544 Refresh();
5545
5546 return true;
5547 }
5548
5549 // static
5550 wxVisualAttributes
5551 wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
5552 {
5553 #if _USE_VISATTR
5554 // Use the same color scheme as wxListBox
5555 return wxListBox::GetClassDefaultAttributes(variant);
5556 #else
5557 wxUnusedVar(variant);
5558 wxVisualAttributes attr;
5559 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
5560 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
5561 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
5562 return attr;
5563 #endif
5564 }
5565
5566 // ----------------------------------------------------------------------------
5567 // methods forwarded to m_mainWin
5568 // ----------------------------------------------------------------------------
5569
5570 #if wxUSE_DRAG_AND_DROP
5571
5572 void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
5573 {
5574 m_mainWin->SetDropTarget( dropTarget );
5575 }
5576
5577 wxDropTarget *wxGenericListCtrl::GetDropTarget() const
5578 {
5579 return m_mainWin->GetDropTarget();
5580 }
5581
5582 #endif
5583
5584 bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
5585 {
5586 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false;
5587 }
5588
5589 wxColour wxGenericListCtrl::GetBackgroundColour() const
5590 {
5591 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5592 }
5593
5594 wxColour wxGenericListCtrl::GetForegroundColour() const
5595 {
5596 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5597 }
5598
5599 bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
5600 {
5601 #if wxUSE_MENUS
5602 return m_mainWin->PopupMenu( menu, x, y );
5603 #else
5604 return false;
5605 #endif
5606 }
5607
5608 void wxGenericListCtrl::DoClientToScreen( int *x, int *y ) const
5609 {
5610 m_mainWin->DoClientToScreen(x, y);
5611 }
5612
5613 void wxGenericListCtrl::DoScreenToClient( int *x, int *y ) const
5614 {
5615 m_mainWin->DoScreenToClient(x, y);
5616 }
5617
5618 void wxGenericListCtrl::SetFocus()
5619 {
5620 // The test in window.cpp fails as we are a composite
5621 // window, so it checks against "this", but not m_mainWin.
5622 if ( DoFindFocus() != this )
5623 m_mainWin->SetFocus();
5624 }
5625
5626 wxSize wxGenericListCtrl::DoGetBestSize() const
5627 {
5628 // Something is better than nothing...
5629 // 100x80 is what the MSW version will get from the default
5630 // wxControl::DoGetBestSize
5631 return wxSize(100, 80);
5632 }
5633
5634 // ----------------------------------------------------------------------------
5635 // virtual list control support
5636 // ----------------------------------------------------------------------------
5637
5638 wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
5639 {
5640 // this is a pure virtual function, in fact - which is not really pure
5641 // because the controls which are not virtual don't need to implement it
5642 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
5643
5644 return wxEmptyString;
5645 }
5646
5647 int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
5648 {
5649 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
5650 -1,
5651 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
5652 return -1;
5653 }
5654
5655 int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const
5656 {
5657 if (!column)
5658 return OnGetItemImage(item);
5659
5660 return -1;
5661 }
5662
5663 wxListItemAttr *
5664 wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
5665 {
5666 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
5667 _T("invalid item index in OnGetItemAttr()") );
5668
5669 // no attributes by default
5670 return NULL;
5671 }
5672
5673 void wxGenericListCtrl::SetItemCount(long count)
5674 {
5675 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5676
5677 m_mainWin->SetItemCount(count);
5678 }
5679
5680 void wxGenericListCtrl::RefreshItem(long item)
5681 {
5682 m_mainWin->RefreshLine(item);
5683 }
5684
5685 void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
5686 {
5687 m_mainWin->RefreshLines(itemFrom, itemTo);
5688 }
5689
5690 // Generic wxListCtrl is more or less a container for two other
5691 // windows which drawings are done upon. These are namely
5692 // 'm_headerWin' and 'm_mainWin'.
5693 // Here we override 'virtual wxWindow::Refresh()' to mimic the
5694 // behaviour wxListCtrl has under wxMSW.
5695 //
5696 void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5697 {
5698 if (!rect)
5699 {
5700 // The easy case, no rectangle specified.
5701 if (m_headerWin)
5702 m_headerWin->Refresh(eraseBackground);
5703
5704 if (m_mainWin)
5705 m_mainWin->Refresh(eraseBackground);
5706 }
5707 else
5708 {
5709 // Refresh the header window
5710 if (m_headerWin)
5711 {
5712 wxRect rectHeader = m_headerWin->GetRect();
5713 rectHeader.Intersect(*rect);
5714 if (rectHeader.GetWidth() && rectHeader.GetHeight())
5715 {
5716 int x, y;
5717 m_headerWin->GetPosition(&x, &y);
5718 rectHeader.Offset(-x, -y);
5719 m_headerWin->Refresh(eraseBackground, &rectHeader);
5720 }
5721 }
5722
5723 // Refresh the main window
5724 if (m_mainWin)
5725 {
5726 wxRect rectMain = m_mainWin->GetRect();
5727 rectMain.Intersect(*rect);
5728 if (rectMain.GetWidth() && rectMain.GetHeight())
5729 {
5730 int x, y;
5731 m_mainWin->GetPosition(&x, &y);
5732 rectMain.Offset(-x, -y);
5733 m_mainWin->Refresh(eraseBackground, &rectMain);
5734 }
5735 }
5736 }
5737 }
5738
5739 void wxGenericListCtrl::Freeze()
5740 {
5741 m_mainWin->Freeze();
5742 }
5743
5744 void wxGenericListCtrl::Thaw()
5745 {
5746 m_mainWin->Thaw();
5747 }
5748
5749 #endif // wxUSE_LISTCTRL