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