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