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