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