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