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