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