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