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