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