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