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