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