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