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