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