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