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