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