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