/[pcsx2_0.9.7]/trunk/3rdparty/wxWidgets/src/generic/listctrl.cpp
ViewVC logotype

Contents of /trunk/3rdparty/wxWidgets/src/generic/listctrl.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (10 years, 1 month ago) by william
File size: 166848 byte(s)
committing r3113 initial commit again...
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: listctrl.cpp 57542 2008-12-25 13:03:24Z VZ $
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 #include "wx/listctrl.h"
27
28 #if (!defined(__WXMSW__) || defined(__WXUNIVERSAL__)) && (!defined(__WXMAC__)|| defined(__WXUNIVERSAL__))
29 // if we have a native version, its implementation file does all this
30 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
31 IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl)
32 IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
33
34 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxGenericListCtrl)
35 #endif
36
37 #ifndef WX_PRECOMP
38 #include "wx/scrolwin.h"
39 #include "wx/timer.h"
40 #include "wx/settings.h"
41 #include "wx/dynarray.h"
42 #include "wx/dcclient.h"
43 #include "wx/dcscreen.h"
44 #include "wx/math.h"
45 #endif
46
47 #include "wx/imaglist.h"
48 #include "wx/selstore.h"
49 #include "wx/renderer.h"
50
51 #ifdef __WXMAC__
52 #include "wx/mac/private.h"
53 #endif
54
55
56 // NOTE: If using the wxListBox visual attributes works everywhere then this can
57 // be removed, as well as the #else case below.
58 #define _USE_VISATTR 0
59
60
61 // ----------------------------------------------------------------------------
62 // constants
63 // ----------------------------------------------------------------------------
64
65 // // the height of the header window (FIXME: should depend on its font!)
66 // static const int HEADER_HEIGHT = 23;
67
68 static const int SCROLL_UNIT_X = 15;
69
70 // the spacing between the lines (in report mode)
71 static const int LINE_SPACING = 0;
72
73 // extra margins around the text label
74 #ifdef __WXGTK__
75 static const int EXTRA_WIDTH = 6;
76 #else
77 static const int EXTRA_WIDTH = 4;
78 #endif
79 static const int EXTRA_HEIGHT = 4;
80
81 // margin between the window and the items
82 static const int EXTRA_BORDER_X = 2;
83 static const int EXTRA_BORDER_Y = 2;
84
85 // offset for the header window
86 static const int HEADER_OFFSET_X = 0;
87 static const int HEADER_OFFSET_Y = 0;
88
89 // margin between rows of icons in [small] icon view
90 static const int MARGIN_BETWEEN_ROWS = 6;
91
92 // when autosizing the columns, add some slack
93 static const int AUTOSIZE_COL_MARGIN = 10;
94
95 // default width for the header columns
96 static const int WIDTH_COL_DEFAULT = 80;
97
98 // the space between the image and the text in the report mode
99 static const int IMAGE_MARGIN_IN_REPORT_MODE = 5;
100
101 // the space between the image and the text in the report mode in header
102 static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE = 2;
103
104 // ============================================================================
105 // private classes
106 // ============================================================================
107
108 //-----------------------------------------------------------------------------
109 // wxColWidthInfo (internal)
110 //-----------------------------------------------------------------------------
111
112 struct wxColWidthInfo
113 {
114 int nMaxWidth;
115 bool bNeedsUpdate; // only set to true when an item whose
116 // width == nMaxWidth is removed
117
118 wxColWidthInfo(int w = 0, bool needsUpdate = false)
119 {
120 nMaxWidth = w;
121 bNeedsUpdate = needsUpdate;
122 }
123 };
124
125 WX_DEFINE_ARRAY_PTR(wxColWidthInfo *, ColWidthArray);
126
127 //-----------------------------------------------------------------------------
128 // wxListItemData (internal)
129 //-----------------------------------------------------------------------------
130
131 class wxListItemData
132 {
133 public:
134 wxListItemData(wxListMainWindow *owner);
135 ~wxListItemData();
136
137 void SetItem( const wxListItem &info );
138 void SetImage( int image ) { m_image = image; }
139 void SetData( wxUIntPtr data ) { m_data = data; }
140 void SetPosition( int x, int y );
141 void SetSize( int width, int height );
142
143 bool HasText() const { return !m_text.empty(); }
144 const wxString& GetText() const { return m_text; }
145 void SetText(const wxString& text) { m_text = text; }
146
147 // we can't use empty string for measuring the string width/height, so
148 // always return something
149 wxString GetTextForMeasuring() const
150 {
151 wxString s = GetText();
152 if ( s.empty() )
153 s = _T('H');
154
155 return s;
156 }
157
158 bool IsHit( int x, int y ) const;
159
160 int GetX() const;
161 int GetY() const;
162 int GetWidth() const;
163 int GetHeight() const;
164
165 int GetImage() const { return m_image; }
166 bool HasImage() const { return GetImage() != -1; }
167
168 void GetItem( wxListItem &info ) const;
169
170 void SetAttr(wxListItemAttr *attr) { m_attr = attr; }
171 wxListItemAttr *GetAttr() const { return m_attr; }
172
173 public:
174 // the item image or -1
175 int m_image;
176
177 // user data associated with the item
178 wxUIntPtr m_data;
179
180 // the item coordinates are not used in report mode; instead this pointer is
181 // NULL and the owner window is used to retrieve the item position and size
182 wxRect *m_rect;
183
184 // the list ctrl we are in
185 wxListMainWindow *m_owner;
186
187 // custom attributes or NULL
188 wxListItemAttr *m_attr;
189
190 protected:
191 // common part of all ctors
192 void Init();
193
194 wxString m_text;
195 };
196
197 //-----------------------------------------------------------------------------
198 // wxListHeaderData (internal)
199 //-----------------------------------------------------------------------------
200
201 class wxListHeaderData : public wxObject
202 {
203 public:
204 wxListHeaderData();
205 wxListHeaderData( const wxListItem &info );
206 void SetItem( const wxListItem &item );
207 void SetPosition( int x, int y );
208 void SetWidth( int w );
209 void SetState( int state );
210 void SetFormat( int format );
211 void SetHeight( int h );
212 bool HasImage() const;
213
214 bool HasText() const { return !m_text.empty(); }
215 const wxString& GetText() const { return m_text; }
216 void SetText(const wxString& text) { m_text = text; }
217
218 void GetItem( wxListItem &item );
219
220 bool IsHit( int x, int y ) const;
221 int GetImage() const;
222 int GetWidth() const;
223 int GetFormat() const;
224 int GetState() const;
225
226 protected:
227 long m_mask;
228 int m_image;
229 wxString m_text;
230 int m_format;
231 int m_width;
232 int m_xpos,
233 m_ypos;
234 int m_height;
235 int m_state;
236
237 private:
238 void Init();
239 };
240
241 //-----------------------------------------------------------------------------
242 // wxListLineData (internal)
243 //-----------------------------------------------------------------------------
244
245 WX_DECLARE_EXPORTED_LIST(wxListItemData, wxListItemDataList);
246 #include "wx/listimpl.cpp"
247 WX_DEFINE_LIST(wxListItemDataList)
248
249 class wxListLineData
250 {
251 public:
252 // the list of subitems: only may have more than one item in report mode
253 wxListItemDataList m_items;
254
255 // this is not used in report view
256 struct GeometryInfo
257 {
258 // total item rect
259 wxRect m_rectAll;
260
261 // label only
262 wxRect m_rectLabel;
263
264 // icon only
265 wxRect m_rectIcon;
266
267 // the part to be highlighted
268 wxRect m_rectHighlight;
269
270 // extend all our rects to be centered inside the one of given width
271 void ExtendWidth(wxCoord w)
272 {
273 wxASSERT_MSG( m_rectAll.width <= w,
274 _T("width can only be increased") );
275
276 m_rectAll.width = w;
277 m_rectLabel.x = m_rectAll.x + (w - m_rectLabel.width) / 2;
278 m_rectIcon.x = m_rectAll.x + (w - m_rectIcon.width) / 2;
279 m_rectHighlight.x = m_rectAll.x + (w - m_rectHighlight.width) / 2;
280 }
281 }
282 *m_gi;
283
284 // is this item selected? [NB: not used in virtual mode]
285 bool m_highlighted;
286
287 // back pointer to the list ctrl
288 wxListMainWindow *m_owner;
289
290 public:
291 wxListLineData(wxListMainWindow *owner);
292
293 ~wxListLineData()
294 {
295 WX_CLEAR_LIST(wxListItemDataList, m_items);
296 delete m_gi;
297 }
298
299 // are we in report mode?
300 inline bool InReportView() const;
301
302 // are we in virtual report mode?
303 inline bool IsVirtual() const;
304
305 // these 2 methods shouldn't be called for report view controls, in that
306 // case we determine our position/size ourselves
307
308 // calculate the size of the line
309 void CalculateSize( wxDC *dc, int spacing );
310
311 // remember the position this line appears at
312 void SetPosition( int x, int y, int spacing );
313
314 // wxListCtrl API
315
316 void SetImage( int image ) { SetImage(0, image); }
317 int GetImage() const { return GetImage(0); }
318 void SetImage( int index, int image );
319 int GetImage( int index ) const;
320
321 bool HasImage() const { return GetImage() != -1; }
322 bool HasText() const { return !GetText(0).empty(); }
323
324 void SetItem( int index, const wxListItem &info );
325 void GetItem( int index, wxListItem &info );
326
327 wxString GetText(int index) const;
328 void SetText( int index, const wxString& s );
329
330 wxListItemAttr *GetAttr() const;
331 void SetAttr(wxListItemAttr *attr);
332
333 // return true if the highlighting really changed
334 bool Highlight( bool on );
335
336 void ReverseHighlight();
337
338 bool IsHighlighted() const
339 {
340 wxASSERT_MSG( !IsVirtual(), _T("unexpected call to IsHighlighted") );
341
342 return m_highlighted;
343 }
344
345 // draw the line on the given DC in icon/list mode
346 void Draw( wxDC *dc );
347
348 // the same in report mode
349 void DrawInReportMode( wxDC *dc,
350 const wxRect& rect,
351 const wxRect& rectHL,
352 bool highlighted );
353
354 private:
355 // set the line to contain num items (only can be > 1 in report mode)
356 void InitItems( int num );
357
358 // get the mode (i.e. style) of the list control
359 inline int GetMode() const;
360
361 // prepare the DC for drawing with these item's attributes, return true if
362 // we need to draw the items background to highlight it, false otherwise
363 bool SetAttributes(wxDC *dc,
364 const wxListItemAttr *attr,
365 bool highlight);
366
367 // draw the text on the DC with the correct justification; also add an
368 // ellipsis if the text is too large to fit in the current width
369 void DrawTextFormatted(wxDC *dc,
370 const wxString &text,
371 int col,
372 int x,
373 int yMid, // this is middle, not top, of the text
374 int width);
375 };
376
377 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData, wxListLineDataArray);
378 #include "wx/arrimpl.cpp"
379 WX_DEFINE_OBJARRAY(wxListLineDataArray)
380
381 //-----------------------------------------------------------------------------
382 // wxListHeaderWindow (internal)
383 //-----------------------------------------------------------------------------
384
385 class wxListHeaderWindow : public wxWindow
386 {
387 protected:
388 wxListMainWindow *m_owner;
389 const wxCursor *m_currentCursor;
390 wxCursor *m_resizeCursor;
391 bool m_isDragging;
392
393 // column being resized or -1
394 int m_column;
395
396 // divider line position in logical (unscrolled) coords
397 int m_currentX;
398
399 // minimal position beyond which the divider line
400 // can't be dragged in logical coords
401 int m_minX;
402
403 public:
404 wxListHeaderWindow();
405
406 wxListHeaderWindow( wxWindow *win,
407 wxWindowID id,
408 wxListMainWindow *owner,
409 const wxPoint &pos = wxDefaultPosition,
410 const wxSize &size = wxDefaultSize,
411 long style = 0,
412 const wxString &name = wxT("wxlistctrlcolumntitles") );
413
414 virtual ~wxListHeaderWindow();
415
416 void DrawCurrent();
417 void AdjustDC( wxDC& dc );
418
419 void OnPaint( wxPaintEvent &event );
420 void OnMouse( wxMouseEvent &event );
421 void OnSetFocus( wxFocusEvent &event );
422
423 // needs refresh
424 bool m_dirty;
425
426 private:
427 // common part of all ctors
428 void Init();
429
430 // generate and process the list event of the given type, return true if
431 // it wasn't vetoed, i.e. if we should proceed
432 bool SendListEvent(wxEventType type, const wxPoint& pos);
433
434 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow)
435 DECLARE_EVENT_TABLE()
436 };
437
438 //-----------------------------------------------------------------------------
439 // wxListRenameTimer (internal)
440 //-----------------------------------------------------------------------------
441
442 class wxListRenameTimer: public wxTimer
443 {
444 private:
445 wxListMainWindow *m_owner;
446
447 public:
448 wxListRenameTimer( wxListMainWindow *owner );
449 void Notify();
450 };
451
452 //-----------------------------------------------------------------------------
453 // wxListTextCtrlWrapper: wraps a wxTextCtrl to make it work for inline editing
454 //-----------------------------------------------------------------------------
455
456 class wxListTextCtrlWrapper : public wxEvtHandler
457 {
458 public:
459 // NB: text must be a valid object but not Create()d yet
460 wxListTextCtrlWrapper(wxListMainWindow *owner,
461 wxTextCtrl *text,
462 size_t itemEdit);
463
464 wxTextCtrl *GetText() const { return m_text; }
465
466 void AcceptChangesAndFinish();
467
468 protected:
469 void OnChar( wxKeyEvent &event );
470 void OnKeyUp( wxKeyEvent &event );
471 void OnKillFocus( wxFocusEvent &event );
472
473 bool AcceptChanges();
474 void Finish();
475
476 private:
477 wxListMainWindow *m_owner;
478 wxTextCtrl *m_text;
479 wxString m_startValue;
480 size_t m_itemEdited;
481 bool m_finished;
482 bool m_aboutToFinish;
483
484 DECLARE_EVENT_TABLE()
485 };
486
487 //-----------------------------------------------------------------------------
488 // wxListMainWindow (internal)
489 //-----------------------------------------------------------------------------
490
491 WX_DECLARE_EXPORTED_LIST(wxListHeaderData, wxListHeaderDataList);
492 #include "wx/listimpl.cpp"
493 WX_DEFINE_LIST(wxListHeaderDataList)
494
495 class wxListMainWindow : public wxScrolledWindow
496 {
497 public:
498 wxListMainWindow();
499 wxListMainWindow( wxWindow *parent,
500 wxWindowID id,
501 const wxPoint& pos = wxDefaultPosition,
502 const wxSize& size = wxDefaultSize,
503 long style = 0,
504 const wxString &name = _T("listctrlmainwindow") );
505
506 virtual ~wxListMainWindow();
507
508 bool HasFlag(int flag) const { return m_parent->HasFlag(flag); }
509
510 // return true if this is a virtual list control
511 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL); }
512
513 // return true if the control is in report mode
514 bool InReportView() const { return HasFlag(wxLC_REPORT); }
515
516 // return true if we are in single selection mode, false if multi sel
517 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL); }
518
519 // do we have a header window?
520 bool HasHeader() const
521 { return InReportView() && !HasFlag(wxLC_NO_HEADER); }
522
523 void HighlightAll( bool on );
524
525 // all these functions only do something if the line is currently visible
526
527 // change the line "selected" state, return true if it really changed
528 bool HighlightLine( size_t line, bool highlight = true);
529
530 // as HighlightLine() but do it for the range of lines: this is incredibly
531 // more efficient for virtual list controls!
532 //
533 // NB: unlike HighlightLine() this one does refresh the lines on screen
534 void HighlightLines( size_t lineFrom, size_t lineTo, bool on = true );
535
536 // toggle the line state and refresh it
537 void ReverseHighlight( size_t line )
538 { HighlightLine(line, !IsHighlighted(line)); RefreshLine(line); }
539
540 // return true if the line is highlighted
541 bool IsHighlighted(size_t line) const;
542
543 // refresh one or several lines at once
544 void RefreshLine( size_t line );
545 void RefreshLines( size_t lineFrom, size_t lineTo );
546
547 // refresh all selected items
548 void RefreshSelected();
549
550 // refresh all lines below the given one: the difference with
551 // RefreshLines() is that the index here might not be a valid one (happens
552 // when the last line is deleted)
553 void RefreshAfter( size_t lineFrom );
554
555 // the methods which are forwarded to wxListLineData itself in list/icon
556 // modes but are here because the lines don't store their positions in the
557 // report mode
558
559 // get the bound rect for the entire line
560 wxRect GetLineRect(size_t line) const;
561
562 // get the bound rect of the label
563 wxRect GetLineLabelRect(size_t line) const;
564
565 // get the bound rect of the items icon (only may be called if we do have
566 // an icon!)
567 wxRect GetLineIconRect(size_t line) const;
568
569 // get the rect to be highlighted when the item has focus
570 wxRect GetLineHighlightRect(size_t line) const;
571
572 // get the size of the total line rect
573 wxSize GetLineSize(size_t line) const
574 { return GetLineRect(line).GetSize(); }
575
576 // return the hit code for the corresponding position (in this line)
577 long HitTestLine(size_t line, int x, int y) const;
578
579 // bring the selected item into view, scrolling to it if necessary
580 void MoveToItem(size_t item);
581
582 bool ScrollList( int WXUNUSED(dx), int dy );
583
584 // bring the current item into view
585 void MoveToFocus() { MoveToItem(m_current); }
586
587 // start editing the label of the given item
588 wxTextCtrl *EditLabel(long item,
589 wxClassInfo* textControlClass = CLASSINFO(wxTextCtrl));
590 wxTextCtrl *GetEditControl() const
591 {
592 return m_textctrlWrapper ? m_textctrlWrapper->GetText() : NULL;
593 }
594
595 void FinishEditing(wxTextCtrl *text)
596 {
597 delete text;
598 m_textctrlWrapper = NULL;
599 SetFocusIgnoringChildren();
600 }
601
602 // suspend/resume redrawing the control
603 void Freeze();
604 void Thaw();
605
606 void OnRenameTimer();
607 bool OnRenameAccept(size_t itemEdit, const wxString& value);
608 void OnRenameCancelled(size_t itemEdit);
609
610 void OnMouse( wxMouseEvent &event );
611
612 // called to switch the selection from the current item to newCurrent,
613 void OnArrowChar( size_t newCurrent, const wxKeyEvent& event );
614
615 void OnChar( wxKeyEvent &event );
616 void OnKeyDown( wxKeyEvent &event );
617 void OnKeyUp( wxKeyEvent &event );
618 void OnSetFocus( wxFocusEvent &event );
619 void OnKillFocus( wxFocusEvent &event );
620 void OnScroll( wxScrollWinEvent& event );
621
622 void OnPaint( wxPaintEvent &event );
623
624 void OnChildFocus(wxChildFocusEvent& event);
625
626 void DrawImage( int index, wxDC *dc, int x, int y );
627 void GetImageSize( int index, int &width, int &height ) const;
628 int GetTextLength( const wxString &s ) const;
629
630 void SetImageList( wxImageList *imageList, int which );
631 void SetItemSpacing( int spacing, bool isSmall = false );
632 int GetItemSpacing( bool isSmall = false );
633
634 void SetColumn( int col, wxListItem &item );
635 void SetColumnWidth( int col, int width );
636 void GetColumn( int col, wxListItem &item ) const;
637 int GetColumnWidth( int col ) const;
638 int GetColumnCount() const { return m_columns.GetCount(); }
639
640 // returns the sum of the heights of all columns
641 int GetHeaderWidth() const;
642
643 int GetCountPerPage() const;
644
645 void SetItem( wxListItem &item );
646 void GetItem( wxListItem &item ) const;
647 void SetItemState( long item, long state, long stateMask );
648 void SetItemStateAll( long state, long stateMask );
649 int GetItemState( long item, long stateMask ) const;
650 void GetItemRect( long index, wxRect &rect ) const;
651 wxRect GetViewRect() const;
652 bool GetItemPosition( long item, wxPoint& pos ) const;
653 int GetSelectedItemCount() const;
654
655 wxString GetItemText(long item) const
656 {
657 wxListItem info;
658 info.m_mask = wxLIST_MASK_TEXT;
659 info.m_itemId = item;
660 GetItem( info );
661 return info.m_text;
662 }
663
664 void SetItemText(long item, const wxString& value)
665 {
666 wxListItem info;
667 info.m_mask = wxLIST_MASK_TEXT;
668 info.m_itemId = item;
669 info.m_text = value;
670 SetItem( info );
671 }
672
673 // set the scrollbars and update the positions of the items
674 void RecalculatePositions(bool noRefresh = false);
675
676 // refresh the window and the header
677 void RefreshAll();
678
679 long GetNextItem( long item, int geometry, int state ) const;
680 void DeleteItem( long index );
681 void DeleteAllItems();
682 void DeleteColumn( int col );
683 void DeleteEverything();
684 void EnsureVisible( long index );
685 long FindItem( long start, const wxString& str, bool partial = false );
686 long FindItem( long start, wxUIntPtr data);
687 long FindItem( const wxPoint& pt );
688 long HitTest( int x, int y, int &flags ) const;
689 void InsertItem( wxListItem &item );
690 void InsertColumn( long col, wxListItem &item );
691 int GetItemWidthWithImage(wxListItem * item);
692 void SortItems( wxListCtrlCompare fn, long data );
693
694 size_t GetItemCount() const;
695 bool IsEmpty() const { return GetItemCount() == 0; }
696 void SetItemCount(long count);
697
698 // change the current (== focused) item, send a notification event
699 void ChangeCurrent(size_t current);
700 void ResetCurrent() { ChangeCurrent((size_t)-1); }
701 bool HasCurrent() const { return m_current != (size_t)-1; }
702
703 // send out a wxListEvent
704 void SendNotify( size_t line,
705 wxEventType command,
706 const wxPoint& point = wxDefaultPosition );
707
708 // override base class virtual to reset m_lineHeight when the font changes
709 virtual bool SetFont(const wxFont& font)
710 {
711 if ( !wxScrolledWindow::SetFont(font) )
712 return false;
713
714 m_lineHeight = 0;
715
716 return true;
717 }
718
719 // these are for wxListLineData usage only
720
721 // get the backpointer to the list ctrl
722 wxGenericListCtrl *GetListCtrl() const
723 {
724 return wxStaticCast(GetParent(), wxGenericListCtrl);
725 }
726
727 // get the height of all lines (assuming they all do have the same height)
728 wxCoord GetLineHeight() const;
729
730 // get the y position of the given line (only for report view)
731 wxCoord GetLineY(size_t line) const;
732
733 // get the brush to use for the item highlighting
734 wxBrush *GetHighlightBrush() const
735 {
736 return m_hasFocus ? m_highlightBrush : m_highlightUnfocusedBrush;
737 }
738
739 bool HasFocus() const
740 {
741 return m_hasFocus;
742 }
743
744 //protected:
745 // the array of all line objects for a non virtual list control (for the
746 // virtual list control we only ever use m_lines[0])
747 wxListLineDataArray m_lines;
748
749 // the list of column objects
750 wxListHeaderDataList m_columns;
751
752 // currently focused item or -1
753 size_t m_current;
754
755 // the number of lines per page
756 int m_linesPerPage;
757
758 // this flag is set when something which should result in the window
759 // redrawing happens (i.e. an item was added or deleted, or its appearance
760 // changed) and OnPaint() doesn't redraw the window while it is set which
761 // allows to minimize the number of repaintings when a lot of items are
762 // being added. The real repainting occurs only after the next OnIdle()
763 // call
764 bool m_dirty;
765
766 wxColour *m_highlightColour;
767 wxImageList *m_small_image_list;
768 wxImageList *m_normal_image_list;
769 int m_small_spacing;
770 int m_normal_spacing;
771 bool m_hasFocus;
772
773 bool m_lastOnSame;
774 wxTimer *m_renameTimer;
775 bool m_isCreated;
776 int m_dragCount;
777 wxPoint m_dragStart;
778 ColWidthArray m_aColWidths;
779
780 // for double click logic
781 size_t m_lineLastClicked,
782 m_lineBeforeLastClicked,
783 m_lineSelectSingleOnUp;
784
785 protected:
786 wxWindow *GetMainWindowOfCompositeControl() { return GetParent(); }
787
788 // the total count of items in a virtual list control
789 size_t m_countVirt;
790
791 // the object maintaining the items selection state, only used in virtual
792 // controls
793 wxSelectionStore m_selStore;
794
795 // common part of all ctors
796 void Init();
797
798 // get the line data for the given index
799 wxListLineData *GetLine(size_t n) const
800 {
801 wxASSERT_MSG( n != (size_t)-1, _T("invalid line index") );
802
803 if ( IsVirtual() )
804 {
805 wxConstCast(this, wxListMainWindow)->CacheLineData(n);
806 n = 0;
807 }
808
809 return &m_lines[n];
810 }
811
812 // get a dummy line which can be used for geometry calculations and such:
813 // you must use GetLine() if you want to really draw the line
814 wxListLineData *GetDummyLine() const;
815
816 // cache the line data of the n-th line in m_lines[0]
817 void CacheLineData(size_t line);
818
819 // get the range of visible lines
820 void GetVisibleLinesRange(size_t *from, size_t *to);
821
822 // force us to recalculate the range of visible lines
823 void ResetVisibleLinesRange() { m_lineFrom = (size_t)-1; }
824
825 // get the colour to be used for drawing the rules
826 wxColour GetRuleColour() const
827 {
828 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT);
829 }
830
831 private:
832 // initialize the current item if needed
833 void UpdateCurrent();
834
835 // delete all items but don't refresh: called from dtor
836 void DoDeleteAllItems();
837
838 // the height of one line using the current font
839 wxCoord m_lineHeight;
840
841 // the total header width or 0 if not calculated yet
842 wxCoord m_headerWidth;
843
844 // the first and last lines being shown on screen right now (inclusive),
845 // both may be -1 if they must be calculated so never access them directly:
846 // use GetVisibleLinesRange() above instead
847 size_t m_lineFrom,
848 m_lineTo;
849
850 // the brushes to use for item highlighting when we do/don't have focus
851 wxBrush *m_highlightBrush,
852 *m_highlightUnfocusedBrush;
853
854 // if this is > 0, the control is frozen and doesn't redraw itself
855 size_t m_freezeCount;
856
857 // wrapper around the text control currently used for in place editing or
858 // NULL if no item is being edited
859 wxListTextCtrlWrapper *m_textctrlWrapper;
860
861
862 DECLARE_DYNAMIC_CLASS(wxListMainWindow)
863 DECLARE_EVENT_TABLE()
864
865 friend class wxGenericListCtrl;
866 };
867
868
869 wxListItemData::~wxListItemData()
870 {
871 // in the virtual list control the attributes are managed by the main
872 // program, so don't delete them
873 if ( !m_owner->IsVirtual() )
874 delete m_attr;
875
876 delete m_rect;
877 }
878
879 void wxListItemData::Init()
880 {
881 m_image = -1;
882 m_data = 0;
883
884 m_attr = NULL;
885 }
886
887 wxListItemData::wxListItemData(wxListMainWindow *owner)
888 {
889 Init();
890
891 m_owner = owner;
892
893 if ( owner->InReportView() )
894 m_rect = NULL;
895 else
896 m_rect = new wxRect;
897 }
898
899 void wxListItemData::SetItem( const wxListItem &info )
900 {
901 if ( info.m_mask & wxLIST_MASK_TEXT )
902 SetText(info.m_text);
903 if ( info.m_mask & wxLIST_MASK_IMAGE )
904 m_image = info.m_image;
905 if ( info.m_mask & wxLIST_MASK_DATA )
906 m_data = info.m_data;
907
908 if ( info.HasAttributes() )
909 {
910 if ( m_attr )
911 m_attr->AssignFrom(*info.GetAttributes());
912 else
913 m_attr = new wxListItemAttr(*info.GetAttributes());
914 }
915
916 if ( m_rect )
917 {
918 m_rect->x =
919 m_rect->y =
920 m_rect->height = 0;
921 m_rect->width = info.m_width;
922 }
923 }
924
925 void wxListItemData::SetPosition( int x, int y )
926 {
927 wxCHECK_RET( m_rect, _T("unexpected SetPosition() call") );
928
929 m_rect->x = x;
930 m_rect->y = y;
931 }
932
933 void wxListItemData::SetSize( int width, int height )
934 {
935 wxCHECK_RET( m_rect, _T("unexpected SetSize() call") );
936
937 if ( width != -1 )
938 m_rect->width = width;
939 if ( height != -1 )
940 m_rect->height = height;
941 }
942
943 bool wxListItemData::IsHit( int x, int y ) const
944 {
945 wxCHECK_MSG( m_rect, false, _T("can't be called in this mode") );
946
947 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x, y);
948 }
949
950 int wxListItemData::GetX() const
951 {
952 wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
953
954 return m_rect->x;
955 }
956
957 int wxListItemData::GetY() const
958 {
959 wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
960
961 return m_rect->y;
962 }
963
964 int wxListItemData::GetWidth() const
965 {
966 wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
967
968 return m_rect->width;
969 }
970
971 int wxListItemData::GetHeight() const
972 {
973 wxCHECK_MSG( m_rect, 0, _T("can't be called in this mode") );
974
975 return m_rect->height;
976 }
977
978 void wxListItemData::GetItem( wxListItem &info ) const
979 {
980 long mask = info.m_mask;
981 if ( !mask )
982 // by default, get everything for backwards compatibility
983 mask = -1;
984
985 if ( mask & wxLIST_MASK_TEXT )
986 info.m_text = m_text;
987 if ( mask & wxLIST_MASK_IMAGE )
988 info.m_image = m_image;
989 if ( mask & wxLIST_MASK_DATA )
990 info.m_data = m_data;
991
992 if ( m_attr )
993 {
994 if ( m_attr->HasTextColour() )
995 info.SetTextColour(m_attr->GetTextColour());
996 if ( m_attr->HasBackgroundColour() )
997 info.SetBackgroundColour(m_attr->GetBackgroundColour());
998 if ( m_attr->HasFont() )
999 info.SetFont(m_attr->GetFont());
1000 }
1001 }
1002
1003 //-----------------------------------------------------------------------------
1004 // wxListHeaderData
1005 //-----------------------------------------------------------------------------
1006
1007 void wxListHeaderData::Init()
1008 {
1009 m_mask = 0;
1010 m_image = -1;
1011 m_format = 0;
1012 m_width = 0;
1013 m_xpos = 0;
1014 m_ypos = 0;
1015 m_height = 0;
1016 m_state = 0;
1017 }
1018
1019 wxListHeaderData::wxListHeaderData()
1020 {
1021 Init();
1022 }
1023
1024 wxListHeaderData::wxListHeaderData( const wxListItem &item )
1025 {
1026 Init();
1027
1028 SetItem( item );
1029 }
1030
1031 void wxListHeaderData::SetItem( const wxListItem &item )
1032 {
1033 m_mask = item.m_mask;
1034
1035 if ( m_mask & wxLIST_MASK_TEXT )
1036 m_text = item.m_text;
1037
1038 if ( m_mask & wxLIST_MASK_IMAGE )
1039 m_image = item.m_image;
1040
1041 if ( m_mask & wxLIST_MASK_FORMAT )
1042 m_format = item.m_format;
1043
1044 if ( m_mask & wxLIST_MASK_WIDTH )
1045 SetWidth(item.m_width);
1046
1047 if ( m_mask & wxLIST_MASK_STATE )
1048 SetState(item.m_state);
1049 }
1050
1051 void wxListHeaderData::SetPosition( int x, int y )
1052 {
1053 m_xpos = x;
1054 m_ypos = y;
1055 }
1056
1057 void wxListHeaderData::SetHeight( int h )
1058 {
1059 m_height = h;
1060 }
1061
1062 void wxListHeaderData::SetWidth( int w )
1063 {
1064 m_width = w < 0 ? WIDTH_COL_DEFAULT : w;
1065 }
1066
1067 void wxListHeaderData::SetState( int flag )
1068 {
1069 m_state = flag;
1070 }
1071
1072 void wxListHeaderData::SetFormat( int format )
1073 {
1074 m_format = format;
1075 }
1076
1077 bool wxListHeaderData::HasImage() const
1078 {
1079 return m_image != -1;
1080 }
1081
1082 bool wxListHeaderData::IsHit( int x, int y ) const
1083 {
1084 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
1085 }
1086
1087 void wxListHeaderData::GetItem( wxListItem& item )
1088 {
1089 item.m_mask = m_mask;
1090 item.m_text = m_text;
1091 item.m_image = m_image;
1092 item.m_format = m_format;
1093 item.m_width = m_width;
1094 item.m_state = m_state;
1095 }
1096
1097 int wxListHeaderData::GetImage() const
1098 {
1099 return m_image;
1100 }
1101
1102 int wxListHeaderData::GetWidth() const
1103 {
1104 return m_width;
1105 }
1106
1107 int wxListHeaderData::GetFormat() const
1108 {
1109 return m_format;
1110 }
1111
1112 int wxListHeaderData::GetState() const
1113 {
1114 return m_state;
1115 }
1116
1117 //-----------------------------------------------------------------------------
1118 // wxListLineData
1119 //-----------------------------------------------------------------------------
1120
1121 inline int wxListLineData::GetMode() const
1122 {
1123 return m_owner->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE;
1124 }
1125
1126 inline bool wxListLineData::InReportView() const
1127 {
1128 return m_owner->HasFlag(wxLC_REPORT);
1129 }
1130
1131 inline bool wxListLineData::IsVirtual() const
1132 {
1133 return m_owner->IsVirtual();
1134 }
1135
1136 wxListLineData::wxListLineData( wxListMainWindow *owner )
1137 {
1138 m_owner = owner;
1139
1140 if ( InReportView() )
1141 m_gi = NULL;
1142 else // !report
1143 m_gi = new GeometryInfo;
1144
1145 m_highlighted = false;
1146
1147 InitItems( GetMode() == wxLC_REPORT ? m_owner->GetColumnCount() : 1 );
1148 }
1149
1150 void wxListLineData::CalculateSize( wxDC *dc, int spacing )
1151 {
1152 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1153 wxCHECK_RET( node, _T("no subitems at all??") );
1154
1155 wxListItemData *item = node->GetData();
1156
1157 wxString s;
1158 wxCoord lw, lh;
1159
1160 switch ( GetMode() )
1161 {
1162 case wxLC_ICON:
1163 case wxLC_SMALL_ICON:
1164 m_gi->m_rectAll.width = spacing;
1165
1166 s = item->GetText();
1167
1168 if ( s.empty() )
1169 {
1170 lh =
1171 m_gi->m_rectLabel.width =
1172 m_gi->m_rectLabel.height = 0;
1173 }
1174 else // has label
1175 {
1176 dc->GetTextExtent( s, &lw, &lh );
1177 lw += EXTRA_WIDTH;
1178 lh += EXTRA_HEIGHT;
1179
1180 m_gi->m_rectAll.height = spacing + lh;
1181 if (lw > spacing)
1182 m_gi->m_rectAll.width = lw;
1183
1184 m_gi->m_rectLabel.width = lw;
1185 m_gi->m_rectLabel.height = lh;
1186 }
1187
1188 if (item->HasImage())
1189 {
1190 int w, h;
1191 m_owner->GetImageSize( item->GetImage(), w, h );
1192 m_gi->m_rectIcon.width = w + 8;
1193 m_gi->m_rectIcon.height = h + 8;
1194
1195 if ( m_gi->m_rectIcon.width > m_gi->m_rectAll.width )
1196 m_gi->m_rectAll.width = m_gi->m_rectIcon.width;
1197 if ( m_gi->m_rectIcon.height + lh > m_gi->m_rectAll.height - 4 )
1198 m_gi->m_rectAll.height = m_gi->m_rectIcon.height + lh + 4;
1199 }
1200
1201 if ( item->HasText() )
1202 {
1203 m_gi->m_rectHighlight.width = m_gi->m_rectLabel.width;
1204 m_gi->m_rectHighlight.height = m_gi->m_rectLabel.height;
1205 }
1206 else // no text, highlight the icon
1207 {
1208 m_gi->m_rectHighlight.width = m_gi->m_rectIcon.width;
1209 m_gi->m_rectHighlight.height = m_gi->m_rectIcon.height;
1210 }
1211 break;
1212
1213 case wxLC_LIST:
1214 s = item->GetTextForMeasuring();
1215
1216 dc->GetTextExtent( s, &lw, &lh );
1217 lw += EXTRA_WIDTH;
1218 lh += EXTRA_HEIGHT;
1219
1220 m_gi->m_rectLabel.width = lw;
1221 m_gi->m_rectLabel.height = lh;
1222
1223 m_gi->m_rectAll.width = lw;
1224 m_gi->m_rectAll.height = lh;
1225
1226 if (item->HasImage())
1227 {
1228 int w, h;
1229 m_owner->GetImageSize( item->GetImage(), w, h );
1230 m_gi->m_rectIcon.width = w;
1231 m_gi->m_rectIcon.height = h;
1232
1233 m_gi->m_rectAll.width += 4 + w;
1234 if (h > m_gi->m_rectAll.height)
1235 m_gi->m_rectAll.height = h;
1236 }
1237
1238 m_gi->m_rectHighlight.width = m_gi->m_rectAll.width;
1239 m_gi->m_rectHighlight.height = m_gi->m_rectAll.height;
1240 break;
1241
1242 case wxLC_REPORT:
1243 wxFAIL_MSG( _T("unexpected call to SetSize") );
1244 break;
1245
1246 default:
1247 wxFAIL_MSG( _T("unknown mode") );
1248 break;
1249 }
1250 }
1251
1252 void wxListLineData::SetPosition( int x, int y, int spacing )
1253 {
1254 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1255 wxCHECK_RET( node, _T("no subitems at all??") );
1256
1257 wxListItemData *item = node->GetData();
1258
1259 switch ( GetMode() )
1260 {
1261 case wxLC_ICON:
1262 case wxLC_SMALL_ICON:
1263 m_gi->m_rectAll.x = x;
1264 m_gi->m_rectAll.y = y;
1265
1266 if ( item->HasImage() )
1267 {
1268 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 4 +
1269 (m_gi->m_rectAll.width - m_gi->m_rectIcon.width) / 2;
1270 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 4;
1271 }
1272
1273 if ( item->HasText() )
1274 {
1275 if (m_gi->m_rectAll.width > spacing)
1276 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
1277 else
1278 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2) + (spacing / 2) - (m_gi->m_rectLabel.width / 2);
1279 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + m_gi->m_rectAll.height + 2 - m_gi->m_rectLabel.height;
1280 m_gi->m_rectHighlight.x = m_gi->m_rectLabel.x - 2;
1281 m_gi->m_rectHighlight.y = m_gi->m_rectLabel.y - 2;
1282 }
1283 else // no text, highlight the icon
1284 {
1285 m_gi->m_rectHighlight.x = m_gi->m_rectIcon.x - 4;
1286 m_gi->m_rectHighlight.y = m_gi->m_rectIcon.y - 4;
1287 }
1288 break;
1289
1290 case wxLC_LIST:
1291 m_gi->m_rectAll.x = x;
1292 m_gi->m_rectAll.y = y;
1293
1294 m_gi->m_rectHighlight.x = m_gi->m_rectAll.x;
1295 m_gi->m_rectHighlight.y = m_gi->m_rectAll.y;
1296 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + 2;
1297
1298 if (item->HasImage())
1299 {
1300 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 2;
1301 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 2;
1302 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 4 + (EXTRA_WIDTH/2) + m_gi->m_rectIcon.width;
1303 }
1304 else
1305 {
1306 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
1307 }
1308 break;
1309
1310 case wxLC_REPORT:
1311 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1312 break;
1313
1314 default:
1315 wxFAIL_MSG( _T("unknown mode") );
1316 break;
1317 }
1318 }
1319
1320 void wxListLineData::InitItems( int num )
1321 {
1322 for (int i = 0; i < num; i++)
1323 m_items.Append( new wxListItemData(m_owner) );
1324 }
1325
1326 void wxListLineData::SetItem( int index, const wxListItem &info )
1327 {
1328 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1329 wxCHECK_RET( node, _T("invalid column index in SetItem") );
1330
1331 wxListItemData *item = node->GetData();
1332 item->SetItem( info );
1333 }
1334
1335 void wxListLineData::GetItem( int index, wxListItem &info )
1336 {
1337 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1338 if (node)
1339 {
1340 wxListItemData *item = node->GetData();
1341 item->GetItem( info );
1342 }
1343 }
1344
1345 wxString wxListLineData::GetText(int index) const
1346 {
1347 wxString s;
1348
1349 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1350 if (node)
1351 {
1352 wxListItemData *item = node->GetData();
1353 s = item->GetText();
1354 }
1355
1356 return s;
1357 }
1358
1359 void wxListLineData::SetText( int index, const wxString& s )
1360 {
1361 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1362 if (node)
1363 {
1364 wxListItemData *item = node->GetData();
1365 item->SetText( s );
1366 }
1367 }
1368
1369 void wxListLineData::SetImage( int index, int image )
1370 {
1371 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1372 wxCHECK_RET( node, _T("invalid column index in SetImage()") );
1373
1374 wxListItemData *item = node->GetData();
1375 item->SetImage(image);
1376 }
1377
1378 int wxListLineData::GetImage( int index ) const
1379 {
1380 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
1381 wxCHECK_MSG( node, -1, _T("invalid column index in GetImage()") );
1382
1383 wxListItemData *item = node->GetData();
1384 return item->GetImage();
1385 }
1386
1387 wxListItemAttr *wxListLineData::GetAttr() const
1388 {
1389 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1390 wxCHECK_MSG( node, NULL, _T("invalid column index in GetAttr()") );
1391
1392 wxListItemData *item = node->GetData();
1393 return item->GetAttr();
1394 }
1395
1396 void wxListLineData::SetAttr(wxListItemAttr *attr)
1397 {
1398 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1399 wxCHECK_RET( node, _T("invalid column index in SetAttr()") );
1400
1401 wxListItemData *item = node->GetData();
1402 item->SetAttr(attr);
1403 }
1404
1405 bool wxListLineData::SetAttributes(wxDC *dc,
1406 const wxListItemAttr *attr,
1407 bool highlighted)
1408 {
1409 wxWindow *listctrl = m_owner->GetParent();
1410
1411 // fg colour
1412
1413 // don't use foreground colour for drawing highlighted items - this might
1414 // make them completely invisible (and there is no way to do bit
1415 // arithmetics on wxColour, unfortunately)
1416 wxColour colText;
1417 if ( highlighted )
1418 #ifdef __WXMAC__
1419 {
1420 if (m_owner->HasFocus()
1421 #ifdef __WXMAC__
1422 && IsControlActive( (ControlRef)m_owner->GetHandle() )
1423 #endif
1424 )
1425 colText = *wxWHITE;
1426 else
1427 colText = *wxBLACK;
1428 }
1429 #else
1430 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
1431 #endif
1432 else if ( attr && attr->HasTextColour() )
1433 colText = attr->GetTextColour();
1434 else
1435 colText = listctrl->GetForegroundColour();
1436
1437 dc->SetTextForeground(colText);
1438
1439 // font
1440 wxFont font;
1441 if ( attr && attr->HasFont() )
1442 font = attr->GetFont();
1443 else
1444 font = listctrl->GetFont();
1445
1446 dc->SetFont(font);
1447
1448 // bg colour
1449 bool hasBgCol = attr && attr->HasBackgroundColour();
1450 if ( highlighted || hasBgCol )
1451 {
1452 if ( highlighted )
1453 dc->SetBrush( *m_owner->GetHighlightBrush() );
1454 else
1455 dc->SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
1456
1457 dc->SetPen( *wxTRANSPARENT_PEN );
1458
1459 return true;
1460 }
1461
1462 return false;
1463 }
1464
1465 void wxListLineData::Draw( wxDC *dc )
1466 {
1467 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1468 wxCHECK_RET( node, _T("no subitems at all??") );
1469
1470 bool highlighted = IsHighlighted();
1471
1472 wxListItemAttr *attr = GetAttr();
1473
1474 if ( SetAttributes(dc, attr, highlighted) )
1475 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1476 {
1477 dc->DrawRectangle( m_gi->m_rectHighlight );
1478 }
1479 #else
1480 {
1481 if (highlighted)
1482 {
1483 int flags = wxCONTROL_SELECTED;
1484 if (m_owner->HasFocus()
1485 #ifdef __WXMAC__
1486 && IsControlActive( (ControlRef)m_owner->GetHandle() )
1487 #endif
1488 )
1489 flags |= wxCONTROL_FOCUSED;
1490 wxRendererNative::Get().DrawItemSelectionRect( m_owner, *dc, m_gi->m_rectHighlight, flags );
1491
1492 }
1493 else
1494 {
1495 dc->DrawRectangle( m_gi->m_rectHighlight );
1496 }
1497 }
1498 #endif
1499
1500 // just for debugging to better see where the items are
1501 #if 0
1502 dc->SetPen(*wxRED_PEN);
1503 dc->SetBrush(*wxTRANSPARENT_BRUSH);
1504 dc->DrawRectangle( m_gi->m_rectAll );
1505 dc->SetPen(*wxGREEN_PEN);
1506 dc->DrawRectangle( m_gi->m_rectIcon );
1507 #endif
1508
1509 wxListItemData *item = node->GetData();
1510 if (item->HasImage())
1511 {
1512 // centre the image inside our rectangle, this looks nicer when items
1513 // ae aligned in a row
1514 const wxRect& rectIcon = m_gi->m_rectIcon;
1515
1516 m_owner->DrawImage(item->GetImage(), dc, rectIcon.x, rectIcon.y);
1517 }
1518
1519 if (item->HasText())
1520 {
1521 const wxRect& rectLabel = m_gi->m_rectLabel;
1522
1523 wxDCClipper clipper(*dc, rectLabel);
1524 dc->DrawText(item->GetText(), rectLabel.x, rectLabel.y);
1525 }
1526 }
1527
1528 void wxListLineData::DrawInReportMode( wxDC *dc,
1529 const wxRect& rect,
1530 const wxRect& rectHL,
1531 bool highlighted )
1532 {
1533 // TODO: later we should support setting different attributes for
1534 // different columns - to do it, just add "col" argument to
1535 // GetAttr() and move these lines into the loop below
1536 wxListItemAttr *attr = GetAttr();
1537 if ( SetAttributes(dc, attr, highlighted) )
1538 #if ( !defined(__WXGTK20__) && !defined(__WXMAC__) )
1539 {
1540 dc->DrawRectangle( rectHL );
1541 }
1542 #else
1543 {
1544 if (highlighted)
1545 {
1546 int flags = wxCONTROL_SELECTED;
1547 if (m_owner->HasFocus()
1548 #ifdef __WXMAC__
1549 && IsControlActive( (ControlRef)m_owner->GetHandle() )
1550 #endif
1551 )
1552 flags |= wxCONTROL_FOCUSED;
1553 wxRendererNative::Get().DrawItemSelectionRect( m_owner, *dc, rectHL, flags );
1554 }
1555 else
1556 {
1557 dc->DrawRectangle( rectHL );
1558 }
1559 }
1560 #endif
1561
1562 wxCoord x = rect.x + HEADER_OFFSET_X,
1563 yMid = rect.y + rect.height/2;
1564 #ifdef __WXGTK__
1565 // This probably needs to be done
1566 // on all platforms as the icons
1567 // otherwise nearly touch the border
1568 x += 2;
1569 #endif
1570
1571 size_t col = 0;
1572 for ( wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
1573 node;
1574 node = node->GetNext(), col++ )
1575 {
1576 wxListItemData *item = node->GetData();
1577
1578 int width = m_owner->GetColumnWidth(col);
1579 int xOld = x;
1580 x += width;
1581
1582 if ( item->HasImage() )
1583 {
1584 int ix, iy;
1585 m_owner->GetImageSize( item->GetImage(), ix, iy );
1586 m_owner->DrawImage( item->GetImage(), dc, xOld, yMid - iy/2 );
1587
1588 ix += IMAGE_MARGIN_IN_REPORT_MODE;
1589
1590 xOld += ix;
1591 width -= ix;
1592 }
1593
1594 if ( item->HasText() )
1595 DrawTextFormatted(dc, item->GetText(), col, xOld, yMid, width - 8);
1596 }
1597 }
1598
1599 void wxListLineData::DrawTextFormatted(wxDC *dc,
1600 const wxString& textOrig,
1601 int col,
1602 int x,
1603 int yMid,
1604 int width)
1605 {
1606 // we don't support displaying multiple lines currently (and neither does
1607 // wxMSW FWIW) so just merge all the lines
1608 wxString text(textOrig);
1609 text.Replace(_T("\n"), _T(" "));
1610
1611 wxCoord w, h;
1612 dc->GetTextExtent(text, &w, &h);
1613
1614 const wxCoord y = yMid - (h + 1)/2;
1615
1616 wxDCClipper clipper(*dc, x, y, width, h);
1617
1618 // determine if the string can fit inside the current width
1619 if (w <= width)
1620 {
1621 // it can, draw it using the items alignment
1622 wxListItem item;
1623 m_owner->GetColumn(col, item);
1624 switch ( item.GetAlign() )
1625 {
1626 case wxLIST_FORMAT_LEFT:
1627 // nothing to do
1628 break;
1629
1630 case wxLIST_FORMAT_RIGHT:
1631 x += width - w;
1632 break;
1633
1634 case wxLIST_FORMAT_CENTER:
1635 x += (width - w) / 2;
1636 break;
1637
1638 default:
1639 wxFAIL_MSG( _T("unknown list item format") );
1640 break;
1641 }
1642
1643 dc->DrawText(text, x, y);
1644 }
1645 else // otherwise, truncate and add an ellipsis if possible
1646 {
1647 // determine the base width
1648 wxString ellipsis(wxT("..."));
1649 wxCoord base_w;
1650 dc->GetTextExtent(ellipsis, &base_w, &h);
1651
1652 // continue until we have enough space or only one character left
1653 wxCoord w_c, h_c;
1654 size_t len = text.length();
1655 wxString drawntext = text.Left(len);
1656 while (len > 1)
1657 {
1658 dc->GetTextExtent(drawntext.Last(), &w_c, &h_c);
1659 drawntext.RemoveLast();
1660 len--;
1661 w -= w_c;
1662 if (w + base_w <= width)
1663 break;
1664 }
1665
1666 // if still not enough space, remove ellipsis characters
1667 while (ellipsis.length() > 0 && w + base_w > width)
1668 {
1669 ellipsis = ellipsis.Left(ellipsis.length() - 1);
1670 dc->GetTextExtent(ellipsis, &base_w, &h);
1671 }
1672
1673 // now draw the text
1674 dc->DrawText(drawntext, x, y);
1675 dc->DrawText(ellipsis, x + w, y);
1676 }
1677 }
1678
1679 bool wxListLineData::Highlight( bool on )
1680 {
1681 wxCHECK_MSG( !IsVirtual(), false, _T("unexpected call to Highlight") );
1682
1683 if ( on == m_highlighted )
1684 return false;
1685
1686 m_highlighted = on;
1687
1688 return true;
1689 }
1690
1691 void wxListLineData::ReverseHighlight( void )
1692 {
1693 Highlight(!IsHighlighted());
1694 }
1695
1696 //-----------------------------------------------------------------------------
1697 // wxListHeaderWindow
1698 //-----------------------------------------------------------------------------
1699
1700 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow,wxWindow)
1701
1702 BEGIN_EVENT_TABLE(wxListHeaderWindow,wxWindow)
1703 EVT_PAINT (wxListHeaderWindow::OnPaint)
1704 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse)
1705 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus)
1706 END_EVENT_TABLE()
1707
1708 void wxListHeaderWindow::Init()
1709 {
1710 m_currentCursor = (wxCursor *) NULL;
1711 m_isDragging = false;
1712 m_dirty = false;
1713 }
1714
1715 wxListHeaderWindow::wxListHeaderWindow()
1716 {
1717 Init();
1718
1719 m_owner = (wxListMainWindow *) NULL;
1720 m_resizeCursor = (wxCursor *) NULL;
1721 }
1722
1723 wxListHeaderWindow::wxListHeaderWindow( wxWindow *win,
1724 wxWindowID id,
1725 wxListMainWindow *owner,
1726 const wxPoint& pos,
1727 const wxSize& size,
1728 long style,
1729 const wxString &name )
1730 : wxWindow( win, id, pos, size, style, name )
1731 {
1732 Init();
1733
1734 m_owner = owner;
1735 m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
1736
1737 #if _USE_VISATTR
1738 wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes();
1739 SetOwnForegroundColour( attr.colFg );
1740 SetOwnBackgroundColour( attr.colBg );
1741 if (!m_hasFont)
1742 SetOwnFont( attr.font );
1743 #else
1744 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
1745 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
1746 if (!m_hasFont)
1747 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT ));
1748 #endif
1749 }
1750
1751 wxListHeaderWindow::~wxListHeaderWindow()
1752 {
1753 delete m_resizeCursor;
1754 }
1755
1756 #ifdef __WXUNIVERSAL__
1757 #include "wx/univ/renderer.h"
1758 #include "wx/univ/theme.h"
1759 #endif
1760
1761 // shift the DC origin to match the position of the main window horz
1762 // scrollbar: this allows us to always use logical coords
1763 void wxListHeaderWindow::AdjustDC(wxDC& dc)
1764 {
1765 int xpix;
1766 m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
1767
1768 int view_start;
1769 m_owner->GetViewStart( &view_start, NULL );
1770
1771
1772 int org_x = 0;
1773 int org_y = 0;
1774 dc.GetDeviceOrigin( &org_x, &org_y );
1775
1776 // account for the horz scrollbar offset
1777 #ifdef __WXGTK__
1778 if (GetLayoutDirection() == wxLayout_RightToLeft)
1779 {
1780 // Maybe we just have to check for m_signX
1781 // in the DC, but I leave the #ifdef __WXGTK__
1782 // for now
1783 dc.SetDeviceOrigin( org_x + (view_start * xpix), org_y );
1784 }
1785 else
1786 #endif
1787 dc.SetDeviceOrigin( org_x - (view_start * xpix), org_y );
1788 }
1789
1790 void wxListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1791 {
1792 wxPaintDC dc( this );
1793
1794 PrepareDC( dc );
1795 AdjustDC( dc );
1796
1797 dc.SetFont( GetFont() );
1798
1799 // width and height of the entire header window
1800 int w, h;
1801 GetClientSize( &w, &h );
1802 m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
1803
1804 dc.SetBackgroundMode(wxTRANSPARENT);
1805 dc.SetTextForeground(GetForegroundColour());
1806
1807 int x = HEADER_OFFSET_X;
1808 int numColumns = m_owner->GetColumnCount();
1809 wxListItem item;
1810 for ( int i = 0; i < numColumns && x < w; i++ )
1811 {
1812 m_owner->GetColumn( i, item );
1813 int wCol = item.m_width;
1814
1815 int cw = wCol;
1816 int ch = h;
1817
1818 int flags = 0;
1819 if (!m_parent->IsEnabled())
1820 flags |= wxCONTROL_DISABLED;
1821
1822 // NB: The code below is not really Mac-specific, but since we are close
1823 // to 2.8 release and I don't have time to test on other platforms, I
1824 // defined this only for wxMac. If this behavior is desired on
1825 // other platforms, please go ahead and revise or remove the #ifdef.
1826 #ifdef __WXMAC__
1827 if ( !m_owner->IsVirtual() && (item.m_mask & wxLIST_MASK_STATE) &&
1828 (item.m_state & wxLIST_STATE_SELECTED) )
1829 flags |= wxCONTROL_SELECTED;
1830 #endif
1831
1832 wxRendererNative::Get().DrawHeaderButton
1833 (
1834 this,
1835 dc,
1836 wxRect(x, HEADER_OFFSET_Y, cw, ch),
1837 flags
1838 );
1839
1840 // see if we have enough space for the column label
1841
1842 // for this we need the width of the text
1843 wxCoord wLabel;
1844 wxCoord hLabel;
1845 dc.GetTextExtent(item.GetText(), &wLabel, &hLabel);
1846 wLabel += 2 * EXTRA_WIDTH;
1847
1848 // and the width of the icon, if any
1849 int ix = 0, iy = 0; // init them just to suppress the compiler warnings
1850 const int image = item.m_image;
1851 wxImageList *imageList;
1852 if ( image != -1 )
1853 {
1854 imageList = m_owner->m_small_image_list;
1855 if ( imageList )
1856 {
1857 imageList->GetSize(image, ix, iy);
1858 wLabel += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
1859 }
1860 }
1861 else
1862 {
1863 imageList = NULL;
1864 }
1865
1866 // ignore alignment if there is not enough space anyhow
1867 int xAligned;
1868 switch ( wLabel < cw ? item.GetAlign() : wxLIST_FORMAT_LEFT )
1869 {
1870 default:
1871 wxFAIL_MSG( _T("unknown list item format") );
1872 // fall through
1873
1874 case wxLIST_FORMAT_LEFT:
1875 xAligned = x;
1876 break;
1877
1878 case wxLIST_FORMAT_RIGHT:
1879 xAligned = x + cw - wLabel;
1880 break;
1881
1882 case wxLIST_FORMAT_CENTER:
1883 xAligned = x + (cw - wLabel) / 2;
1884 break;
1885 }
1886
1887 // draw the text and image clipping them so that they
1888 // don't overwrite the column boundary
1889 wxDCClipper clipper(dc, x, HEADER_OFFSET_Y, cw, h );
1890
1891 // if we have an image, draw it on the right of the label
1892 if ( imageList )
1893 {
1894 imageList->Draw
1895 (
1896 image,
1897 dc,
1898 xAligned + wLabel - ix - HEADER_IMAGE_MARGIN_IN_REPORT_MODE,
1899 HEADER_OFFSET_Y + (h - 4 - iy)/2,
1900 wxIMAGELIST_DRAW_TRANSPARENT
1901 );
1902 }
1903
1904 dc.DrawText( item.GetText(),
1905 xAligned + EXTRA_WIDTH, h / 2 - hLabel / 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
1906
1907 x += wCol;
1908 }
1909
1910 // Fill in what's missing to the right of the columns, otherwise we will
1911 // leave an unpainted area when columns are removed (and it looks better)
1912 if ( x < w )
1913 {
1914 wxRendererNative::Get().DrawHeaderButton
1915 (
1916 this,
1917 dc,
1918 wxRect(x, HEADER_OFFSET_Y, w - x, h),
1919 0
1920 );
1921 }
1922 }
1923
1924 void wxListHeaderWindow::DrawCurrent()
1925 {
1926 #if 1
1927 m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1928 #else
1929 int x1 = m_currentX;
1930 int y1 = 0;
1931 m_owner->ClientToScreen( &x1, &y1 );
1932
1933 int x2 = m_currentX;
1934 int y2 = 0;
1935 m_owner->GetClientSize( NULL, &y2 );
1936 m_owner->ClientToScreen( &x2, &y2 );
1937
1938 wxScreenDC dc;
1939 dc.SetLogicalFunction( wxINVERT );
1940 dc.SetPen( wxPen( *wxBLACK, 2, wxSOLID ) );
1941 dc.SetBrush( *wxTRANSPARENT_BRUSH );
1942
1943 AdjustDC(dc);
1944
1945 dc.DrawLine( x1, y1, x2, y2 );
1946
1947 dc.SetLogicalFunction( wxCOPY );
1948
1949 dc.SetPen( wxNullPen );
1950 dc.SetBrush( wxNullBrush );
1951 #endif
1952 }
1953
1954 void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
1955 {
1956 // we want to work with logical coords
1957 int x;
1958 m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
1959 int y = event.GetY();
1960
1961 if (m_isDragging)
1962 {
1963 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING, event.GetPosition());
1964
1965 // we don't draw the line beyond our window, but we allow dragging it
1966 // there
1967 int w = 0;
1968 GetClientSize( &w, NULL );
1969 m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
1970 w -= 6;
1971
1972 // erase the line if it was drawn
1973 if ( m_currentX < w )
1974 DrawCurrent();
1975
1976 if (event.ButtonUp())
1977 {
1978 ReleaseMouse();
1979 m_isDragging = false;
1980 m_dirty = true;
1981 m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1982 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG, event.GetPosition());
1983 }
1984 else
1985 {
1986 if (x > m_minX + 7)
1987 m_currentX = x;
1988 else
1989 m_currentX = m_minX + 7;
1990
1991 // draw in the new location
1992 if ( m_currentX < w )
1993 DrawCurrent();
1994 }
1995 }
1996 else // not dragging
1997 {
1998 m_minX = 0;
1999 bool hit_border = false;
2000
2001 // end of the current column
2002 int xpos = 0;
2003
2004 // find the column where this event occurred
2005 int col,
2006 countCol = m_owner->GetColumnCount();
2007 for (col = 0; col < countCol; col++)
2008 {
2009 xpos += m_owner->GetColumnWidth( col );
2010 m_column = col;
2011
2012 if ( (abs(x-xpos) < 3) && (y < 22) )
2013 {
2014 // near the column border
2015 hit_border = true;
2016 break;
2017 }
2018
2019 if ( x < xpos )
2020 {
2021 // inside the column
2022 break;
2023 }
2024
2025 m_minX = xpos;
2026 }
2027
2028 if ( col == countCol )
2029 m_column = -1;
2030
2031 if (event.LeftDown() || event.RightUp())
2032 {
2033 if (hit_border && event.LeftDown())
2034 {
2035 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG,
2036 event.GetPosition()) )
2037 {
2038 m_isDragging = true;
2039 m_currentX = x;
2040 CaptureMouse();
2041 DrawCurrent();
2042 }
2043 //else: column resizing was vetoed by the user code
2044 }
2045 else // click on a column
2046 {
2047 // record the selected state of the columns
2048 if (event.LeftDown())
2049 {
2050 for (int i=0; i < m_owner->GetColumnCount(); i++)
2051 {
2052 wxListItem colItem;
2053 m_owner->GetColumn(i, colItem);
2054 long state = colItem.GetState();
2055 if (i == m_column)
2056 colItem.SetState(state | wxLIST_STATE_SELECTED);
2057 else
2058 colItem.SetState(state & ~wxLIST_STATE_SELECTED);
2059 m_owner->SetColumn(i, colItem);
2060 }
2061 }
2062
2063 SendListEvent( event.LeftDown()
2064 ? wxEVT_COMMAND_LIST_COL_CLICK
2065 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK,
2066 event.GetPosition());
2067 }
2068 }
2069 else if (event.Moving())
2070 {
2071 bool setCursor;
2072 if (hit_border)
2073 {
2074 setCursor = m_currentCursor == wxSTANDARD_CURSOR;
2075 m_currentCursor = m_resizeCursor;
2076 }
2077 else
2078 {
2079 setCursor = m_currentCursor != wxSTANDARD_CURSOR;
2080 m_currentCursor = wxSTANDARD_CURSOR;
2081 }
2082
2083 if ( setCursor )
2084 SetCursor(*m_currentCursor);
2085 }
2086 }
2087 }
2088
2089 void wxListHeaderWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
2090 {
2091 m_owner->SetFocus();
2092 m_owner->Update();
2093 }
2094
2095 bool wxListHeaderWindow::SendListEvent(wxEventType type, const wxPoint& pos)
2096 {
2097 wxWindow *parent = GetParent();
2098 wxListEvent le( type, parent->GetId() );
2099 le.SetEventObject( parent );
2100 le.m_pointDrag = pos;
2101
2102 // the position should be relative to the parent window, not
2103 // this one for compatibility with MSW and common sense: the
2104 // user code doesn't know anything at all about this header
2105 // window, so why should it get positions relative to it?
2106 le.m_pointDrag.y -= GetSize().y;
2107
2108 le.m_col = m_column;
2109 return !parent->GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
2110 }
2111
2112 //-----------------------------------------------------------------------------
2113 // wxListRenameTimer (internal)
2114 //-----------------------------------------------------------------------------
2115
2116 wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
2117 {
2118 m_owner = owner;
2119 }
2120
2121 void wxListRenameTimer::Notify()
2122 {
2123 m_owner->OnRenameTimer();
2124 }
2125
2126 //-----------------------------------------------------------------------------
2127 // wxListTextCtrlWrapper (internal)
2128 //-----------------------------------------------------------------------------
2129
2130 BEGIN_EVENT_TABLE(wxListTextCtrlWrapper, wxEvtHandler)
2131 EVT_CHAR (wxListTextCtrlWrapper::OnChar)
2132 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp)
2133 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus)
2134 END_EVENT_TABLE()
2135
2136 wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow *owner,
2137 wxTextCtrl *text,
2138 size_t itemEdit)
2139 : m_startValue(owner->GetItemText(itemEdit)),
2140 m_itemEdited(itemEdit)
2141 {
2142 m_owner = owner;
2143 m_text = text;
2144 m_finished = false;
2145 m_aboutToFinish = false;
2146
2147 wxRect rectLabel = owner->GetLineLabelRect(itemEdit);
2148
2149 m_owner->CalcScrolledPosition(rectLabel.x, rectLabel.y,
2150 &rectLabel.x, &rectLabel.y);
2151
2152 m_text->Create(owner, wxID_ANY, m_startValue,
2153 wxPoint(rectLabel.x-4,rectLabel.y-4),
2154 wxSize(rectLabel.width+11,rectLabel.height+8));
2155 m_text->SetFocus();
2156
2157 m_text->PushEventHandler(this);
2158 }
2159
2160 void wxListTextCtrlWrapper::Finish()
2161 {
2162 if ( !m_finished )
2163 {
2164 m_finished = true;
2165
2166 m_text->RemoveEventHandler(this);
2167 m_owner->FinishEditing(m_text);
2168
2169 wxPendingDelete.Append( this );
2170 }
2171 }
2172
2173 bool wxListTextCtrlWrapper::AcceptChanges()
2174 {
2175 const wxString value = m_text->GetValue();
2176
2177 // notice that we should always call OnRenameAccept() to generate the "end
2178 // label editing" event, even if the user hasn't really changed anything
2179 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
2180 {
2181 // vetoed by the user
2182 return false;
2183 }
2184
2185 // accepted, do rename the item (unless nothing changed)
2186 if ( value != m_startValue )
2187 m_owner->SetItemText(m_itemEdited, value);
2188
2189 return true;
2190 }
2191
2192 void wxListTextCtrlWrapper::AcceptChangesAndFinish()
2193 {
2194 m_aboutToFinish = true;
2195
2196 // Notify the owner about the changes
2197 AcceptChanges();
2198
2199 // Even if vetoed, close the control (consistent with MSW)
2200 Finish();
2201 }
2202
2203 void wxListTextCtrlWrapper::OnChar( wxKeyEvent &event )
2204 {
2205 switch ( event.m_keyCode )
2206 {
2207 case WXK_RETURN:
2208 AcceptChangesAndFinish();
2209 break;
2210
2211 case WXK_ESCAPE:
2212 m_owner->OnRenameCancelled( m_itemEdited );
2213 Finish();
2214 break;
2215
2216 default:
2217 event.Skip();
2218 }
2219 }
2220
2221 void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
2222 {
2223 if (m_finished)
2224 {
2225 event.Skip();
2226 return;
2227 }
2228
2229 // auto-grow the textctrl:
2230 wxSize parentSize = m_owner->GetSize();
2231 wxPoint myPos = m_text->GetPosition();
2232 wxSize mySize = m_text->GetSize();
2233 int sx, sy;
2234 m_text->GetTextExtent(m_text->GetValue() + _T("MM"), &sx, &sy);
2235 if (myPos.x + sx > parentSize.x)
2236 sx = parentSize.x - myPos.x;
2237 if (mySize.x > sx)
2238 sx = mySize.x;
2239 m_text->SetSize(sx, wxDefaultCoord);
2240
2241 event.Skip();
2242 }
2243
2244 void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
2245 {
2246 if ( !m_finished && !m_aboutToFinish )
2247 {
2248 if ( !AcceptChanges() )
2249 m_owner->OnRenameCancelled( m_itemEdited );
2250
2251 Finish();
2252 }
2253
2254 // We must let the native text control handle focus
2255 event.Skip();
2256 }
2257
2258 //-----------------------------------------------------------------------------
2259 // wxListMainWindow
2260 //-----------------------------------------------------------------------------
2261
2262 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow,wxScrolledWindow)
2263
2264 BEGIN_EVENT_TABLE(wxListMainWindow,wxScrolledWindow)
2265 EVT_PAINT (wxListMainWindow::OnPaint)
2266 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse)
2267 EVT_CHAR (wxListMainWindow::OnChar)
2268 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown)
2269 EVT_KEY_UP (wxListMainWindow::OnKeyUp)
2270 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus)
2271 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus)
2272 EVT_SCROLLWIN (wxListMainWindow::OnScroll)
2273 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus)
2274 END_EVENT_TABLE()
2275
2276 void wxListMainWindow::Init()
2277 {
2278 m_dirty = true;
2279 m_countVirt = 0;
2280 m_lineFrom =
2281 m_lineTo = (size_t)-1;
2282 m_linesPerPage = 0;
2283
2284 m_headerWidth =
2285 m_lineHeight = 0;
2286
2287 m_small_image_list = (wxImageList *) NULL;
2288 m_normal_image_list = (wxImageList *) NULL;
2289
2290 m_small_spacing = 30;
2291 m_normal_spacing = 40;
2292
2293 m_hasFocus = false;
2294 m_dragCount = 0;
2295 m_isCreated = false;
2296
2297 m_lastOnSame = false;
2298 m_renameTimer = new wxListRenameTimer( this );
2299 m_textctrlWrapper = NULL;
2300
2301 m_current =
2302 m_lineLastClicked =
2303 m_lineSelectSingleOnUp =
2304 m_lineBeforeLastClicked = (size_t)-1;
2305
2306 m_freezeCount = 0;
2307 }
2308
2309 wxListMainWindow::wxListMainWindow()
2310 {
2311 Init();
2312
2313 m_highlightBrush =
2314 m_highlightUnfocusedBrush = (wxBrush *) NULL;
2315 }
2316
2317 wxListMainWindow::wxListMainWindow( wxWindow *parent,
2318 wxWindowID id,
2319 const wxPoint& pos,
2320 const wxSize& size,
2321 long style,
2322 const wxString &name )
2323 : wxScrolledWindow( parent, id, pos, size,
2324 style | wxHSCROLL | wxVSCROLL, name )
2325 {
2326 Init();
2327
2328 m_highlightBrush = new wxBrush
2329 (
2330 wxSystemSettings::GetColour
2331 (
2332 wxSYS_COLOUR_HIGHLIGHT
2333 ),
2334 wxSOLID
2335 );
2336
2337 m_highlightUnfocusedBrush = new wxBrush
2338 (
2339 wxSystemSettings::GetColour
2340 (
2341 wxSYS_COLOUR_BTNSHADOW
2342 ),
2343 wxSOLID
2344 );
2345
2346 SetScrollbars( 0, 0, 0, 0, 0, 0 );
2347
2348 wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes();
2349 SetOwnForegroundColour( attr.colFg );
2350 SetOwnBackgroundColour( attr.colBg );
2351 if (!m_hasFont)
2352 SetOwnFont( attr.font );
2353 }
2354
2355 wxListMainWindow::~wxListMainWindow()
2356 {
2357 DoDeleteAllItems();
2358 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
2359 WX_CLEAR_ARRAY(m_aColWidths);
2360
2361 delete m_highlightBrush;
2362 delete m_highlightUnfocusedBrush;
2363 delete m_renameTimer;
2364 }
2365
2366 void wxListMainWindow::CacheLineData(size_t line)
2367 {
2368 wxGenericListCtrl *listctrl = GetListCtrl();
2369
2370 wxListLineData *ld = GetDummyLine();
2371
2372 size_t countCol = GetColumnCount();
2373 for ( size_t col = 0; col < countCol; col++ )
2374 {
2375 ld->SetText(col, listctrl->OnGetItemText(line, col));
2376 ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col));
2377 }
2378
2379 ld->SetAttr(listctrl->OnGetItemAttr(line));
2380 }
2381
2382 wxListLineData *wxListMainWindow::GetDummyLine() const
2383 {
2384 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
2385 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2386
2387 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
2388
2389 // we need to recreate the dummy line if the number of columns in the
2390 // control changed as it would have the incorrect number of fields
2391 // otherwise
2392 if ( !m_lines.IsEmpty() &&
2393 m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() )
2394 {
2395 self->m_lines.Clear();
2396 }
2397
2398 if ( m_lines.IsEmpty() )
2399 {
2400 wxListLineData *line = new wxListLineData(self);
2401 self->m_lines.Add(line);
2402
2403 // don't waste extra memory -- there never going to be anything
2404 // else/more in this array
2405 self->m_lines.Shrink();
2406 }
2407
2408 return &m_lines[0];
2409 }
2410
2411 // ----------------------------------------------------------------------------
2412 // line geometry (report mode only)
2413 // ----------------------------------------------------------------------------
2414
2415 wxCoord wxListMainWindow::GetLineHeight() const
2416 {
2417 // we cache the line height as calling GetTextExtent() is slow
2418 if ( !m_lineHeight )
2419 {
2420 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
2421
2422 wxClientDC dc( self );
2423 dc.SetFont( GetFont() );
2424
2425 wxCoord y;
2426 dc.GetTextExtent(_T("H"), NULL, &y);
2427
2428 if ( m_small_image_list && m_small_image_list->GetImageCount() )
2429 {
2430 int iw = 0, ih = 0;
2431 m_small_image_list->GetSize(0, iw, ih);
2432 y = wxMax(y, ih);
2433 }
2434
2435 y += EXTRA_HEIGHT;
2436 self->m_lineHeight = y + LINE_SPACING;
2437 }
2438
2439 return m_lineHeight;
2440 }
2441
2442 wxCoord wxListMainWindow::GetLineY(size_t line) const
2443 {
2444 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
2445
2446 return LINE_SPACING + line * GetLineHeight();
2447 }
2448
2449 wxRect wxListMainWindow::GetLineRect(size_t line) const
2450 {
2451 if ( !InReportView() )
2452 return GetLine(line)->m_gi->m_rectAll;
2453
2454 wxRect rect;
2455 rect.x = HEADER_OFFSET_X;
2456 rect.y = GetLineY(line);
2457 rect.width = GetHeaderWidth();
2458 rect.height = GetLineHeight();
2459
2460 return rect;
2461 }
2462
2463 wxRect wxListMainWindow::GetLineLabelRect(size_t line) const
2464 {
2465 if ( !InReportView() )
2466 return GetLine(line)->m_gi->m_rectLabel;
2467
2468 int image_x = 0;
2469 wxListLineData *data = GetLine(line);
2470 wxListItemDataList::compatibility_iterator node = data->m_items.GetFirst();
2471 if (node)
2472 {
2473 wxListItemData *item = node->GetData();
2474 if ( item->HasImage() )
2475 {
2476 int ix, iy;
2477 GetImageSize( item->GetImage(), ix, iy );
2478 image_x = 3 + ix + IMAGE_MARGIN_IN_REPORT_MODE;
2479 }
2480 }
2481
2482 wxRect rect;
2483 rect.x = image_x + HEADER_OFFSET_X;
2484 rect.y = GetLineY(line);
2485 rect.width = GetColumnWidth(0) - image_x;
2486 rect.height = GetLineHeight();
2487
2488 return rect;
2489 }
2490
2491 wxRect wxListMainWindow::GetLineIconRect(size_t line) const
2492 {
2493 if ( !InReportView() )
2494 return GetLine(line)->m_gi->m_rectIcon;
2495
2496 wxListLineData *ld = GetLine(line);
2497 wxASSERT_MSG( ld->HasImage(), _T("should have an image") );
2498
2499 wxRect rect;
2500 rect.x = HEADER_OFFSET_X;
2501 rect.y = GetLineY(line);
2502 GetImageSize(ld->GetImage(), rect.width, rect.height);
2503
2504 return rect;
2505 }
2506
2507 wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const
2508 {
2509 return InReportView() ? GetLineRect(line)
2510 : GetLine(line)->m_gi->m_rectHighlight;
2511 }
2512
2513 long wxListMainWindow::HitTestLine(size_t line, int x, int y) const
2514 {
2515 wxASSERT_MSG( line < GetItemCount(), _T("invalid line in HitTestLine") );
2516
2517 wxListLineData *ld = GetLine(line);
2518
2519 if ( ld->HasImage() && GetLineIconRect(line).Contains(x, y) )
2520 return wxLIST_HITTEST_ONITEMICON;
2521
2522 // VS: Testing for "ld->HasText() || InReportView()" instead of
2523 // "ld->HasText()" is needed to make empty lines in report view
2524 // possible
2525 if ( ld->HasText() || InReportView() )
2526 {
2527 wxRect rect = InReportView() ? GetLineRect(line)
2528 : GetLineLabelRect(line);
2529
2530 if ( rect.Contains(x, y) )
2531 return wxLIST_HITTEST_ONITEMLABEL;
2532 }
2533
2534 return 0;
2535 }
2536
2537 // ----------------------------------------------------------------------------
2538 // highlight (selection) handling
2539 // ----------------------------------------------------------------------------
2540
2541 bool wxListMainWindow::IsHighlighted(size_t line) const
2542 {
2543 if ( IsVirtual() )
2544 {
2545 return m_selStore.IsSelected(line);
2546 }
2547 else // !virtual
2548 {
2549 wxListLineData *ld = GetLine(line);
2550 wxCHECK_MSG( ld, false, _T("invalid index in IsHighlighted") );
2551
2552 return ld->IsHighlighted();
2553 }
2554 }
2555
2556 void wxListMainWindow::HighlightLines( size_t lineFrom,
2557 size_t lineTo,
2558 bool highlight )
2559 {
2560 if ( IsVirtual() )
2561 {
2562 wxArrayInt linesChanged;
2563 if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight,
2564 &linesChanged) )
2565 {
2566 // meny items changed state, refresh everything
2567 RefreshLines(lineFrom, lineTo);
2568 }
2569 else // only a few items changed state, refresh only them
2570 {
2571 size_t count = linesChanged.GetCount();
2572 for ( size_t n = 0; n < count; n++ )
2573 {
2574 RefreshLine(linesChanged[n]);
2575 }
2576 }
2577 }
2578 else // iterate over all items in non report view
2579 {
2580 for ( size_t line = lineFrom; line <= lineTo; line++ )
2581 {
2582 if ( HighlightLine(line, highlight) )
2583 RefreshLine(line);
2584 }
2585 }
2586 }
2587
2588 bool wxListMainWindow::HighlightLine( size_t line, bool highlight )
2589 {
2590 bool changed;
2591
2592 if ( IsVirtual() )
2593 {
2594 changed = m_selStore.SelectItem(line, highlight);
2595 }
2596 else // !virtual
2597 {
2598 wxListLineData *ld = GetLine(line);
2599 wxCHECK_MSG( ld, false, _T("invalid index in HighlightLine") );
2600
2601 changed = ld->Highlight(highlight);
2602 }
2603
2604 if ( changed )
2605 {
2606 SendNotify( line, highlight ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2607 : wxEVT_COMMAND_LIST_ITEM_DESELECTED );
2608 }
2609
2610 return changed;
2611 }
2612
2613 void wxListMainWindow::RefreshLine( size_t line )
2614 {
2615 if ( InReportView() )
2616 {
2617 size_t visibleFrom, visibleTo;
2618 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2619
2620 if ( line < visibleFrom || line > visibleTo )
2621 return;
2622 }
2623
2624 wxRect rect = GetLineRect(line);
2625
2626 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2627 RefreshRect( rect );
2628 }
2629
2630 void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo )
2631 {
2632 // we suppose that they are ordered by caller
2633 wxASSERT_MSG( lineFrom <= lineTo, _T("indices in disorder") );
2634
2635 wxASSERT_MSG( lineTo < GetItemCount(), _T("invalid line range") );
2636
2637 if ( InReportView() )
2638 {
2639 size_t visibleFrom, visibleTo;
2640 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2641
2642 if ( lineFrom < visibleFrom )
2643 lineFrom = visibleFrom;
2644 if ( lineTo > visibleTo )
2645 lineTo = visibleTo;
2646
2647 wxRect rect;
2648 rect.x = 0;
2649 rect.y = GetLineY(lineFrom);
2650 rect.width = GetClientSize().x;
2651 rect.height = GetLineY(lineTo) - rect.y + GetLineHeight();
2652
2653 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2654 RefreshRect( rect );
2655 }
2656 else // !report
2657 {
2658 // TODO: this should be optimized...
2659 for ( size_t line = lineFrom; line <= lineTo; line++ )
2660 {
2661 RefreshLine(line);
2662 }
2663 }
2664 }
2665
2666 void wxListMainWindow::RefreshAfter( size_t lineFrom )
2667 {
2668 if ( InReportView() )
2669 {
2670 size_t visibleFrom, visibleTo;
2671 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2672
2673 if ( lineFrom < visibleFrom )
2674 lineFrom = visibleFrom;
2675 else if ( lineFrom > visibleTo )
2676 return;
2677
2678 wxRect rect;
2679 rect.x = 0;
2680 rect.y = GetLineY(lineFrom);
2681 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2682
2683 wxSize size = GetClientSize();
2684 rect.width = size.x;
2685
2686 // refresh till the bottom of the window
2687 rect.height = size.y - rect.y;
2688
2689 RefreshRect( rect );
2690 }
2691 else // !report
2692 {
2693 // TODO: how to do it more efficiently?
2694 m_dirty = true;
2695 }
2696 }
2697
2698 void wxListMainWindow::RefreshSelected()
2699 {
2700 if ( IsEmpty() )
2701 return;
2702
2703 size_t from, to;
2704 if ( InReportView() )
2705 {
2706 GetVisibleLinesRange(&from, &to);
2707 }
2708 else // !virtual
2709 {
2710 from = 0;
2711 to = GetItemCount() - 1;
2712 }
2713
2714 if ( HasCurrent() && m_current >= from && m_current <= to )
2715 RefreshLine(m_current);
2716
2717 for ( size_t line = from; line <= to; line++ )
2718 {
2719 // NB: the test works as expected even if m_current == -1
2720 if ( line != m_current && IsHighlighted(line) )
2721 RefreshLine(line);
2722 }
2723 }
2724
2725 void wxListMainWindow::Freeze()
2726 {
2727 m_freezeCount++;
2728 }
2729
2730 void wxListMainWindow::Thaw()
2731 {
2732 wxCHECK_RET( m_freezeCount > 0, _T("thawing unfrozen list control?") );
2733
2734 if ( --m_freezeCount == 0 )
2735 Refresh();
2736 }
2737
2738 void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2739 {
2740 // Note: a wxPaintDC must be constructed even if no drawing is
2741 // done (a Windows requirement).
2742 wxPaintDC dc( this );
2743
2744 if ( IsEmpty() || m_freezeCount )
2745 // nothing to draw or not the moment to draw it
2746 return;
2747
2748 if ( m_dirty )
2749 // delay the repainting until we calculate all the items positions
2750 return;
2751
2752 PrepareDC( dc );
2753
2754 int dev_x, dev_y;
2755 CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
2756
2757 dc.SetFont( GetFont() );
2758
2759 if ( InReportView() )
2760 {
2761 int lineHeight = GetLineHeight();
2762
2763 size_t visibleFrom, visibleTo;
2764 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2765
2766 wxRect rectLine;
2767 int xOrig = dc.LogicalToDeviceX( 0 );
2768 int yOrig = dc.LogicalToDeviceY( 0 );
2769
2770 // tell the caller cache to cache the data
2771 if ( IsVirtual() )
2772 {
2773 wxListEvent evCache(wxEVT_COMMAND_LIST_CACHE_HINT,
2774 GetParent()->GetId());
2775 evCache.SetEventObject( GetParent() );
2776 evCache.m_oldItemIndex = visibleFrom;
2777 evCache.m_itemIndex = visibleTo;
2778 GetParent()->GetEventHandler()->ProcessEvent( evCache );
2779 }
2780
2781 for ( size_t line = visibleFrom; line <= visibleTo; line++ )
2782 {
2783 rectLine = GetLineRect(line);
2784
2785
2786 if ( !IsExposed(rectLine.x + xOrig, rectLine.y + yOrig,
2787 rectLine.width, rectLine.height) )
2788 {
2789 // don't redraw unaffected lines to avoid flicker
2790 continue;
2791 }
2792
2793 GetLine(line)->DrawInReportMode( &dc,
2794 rectLine,
2795 GetLineHighlightRect(line),
2796 IsHighlighted(line) );
2797 }
2798
2799 if ( HasFlag(wxLC_HRULES) )
2800 {
2801 wxPen pen(GetRuleColour(), 1, wxSOLID);
2802 wxSize clientSize = GetClientSize();
2803
2804 size_t i = visibleFrom;
2805 if (i == 0) i = 1; // Don't draw the first one
2806 for ( ; i <= visibleTo; i++ )
2807 {
2808 dc.SetPen(pen);
2809 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2810 dc.DrawLine(0 - dev_x, i * lineHeight,
2811 clientSize.x - dev_x, i * lineHeight);
2812 }
2813
2814 // Draw last horizontal rule
2815 if ( visibleTo == GetItemCount() - 1 )
2816 {
2817 dc.SetPen( pen );
2818 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2819 dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight,
2820 clientSize.x - dev_x , (m_lineTo + 1) * lineHeight );
2821 }
2822 }
2823
2824 // Draw vertical rules if required
2825 if ( HasFlag(wxLC_VRULES) && !IsEmpty() )
2826 {
2827 wxPen pen(GetRuleColour(), 1, wxSOLID);
2828 wxRect firstItemRect, lastItemRect;
2829
2830 GetItemRect(visibleFrom, firstItemRect);
2831 GetItemRect(visibleTo, lastItemRect);
2832 int x = firstItemRect.GetX();
2833 dc.SetPen(pen);
2834 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2835
2836 for (int col = 0; col < GetColumnCount(); col++)
2837 {
2838 int colWidth = GetColumnWidth(col);
2839 x += colWidth;
2840 int x_pos = x - dev_x;
2841 if (col < GetColumnCount()-1) x_pos -= 2;
2842 dc.DrawLine(x_pos, firstItemRect.GetY() - 1 - dev_y,
2843 x_pos, lastItemRect.GetBottom() + 1 - dev_y);
2844 }
2845 }
2846 }
2847 else // !report
2848 {
2849 size_t count = GetItemCount();
2850 for ( size_t i = 0; i < count; i++ )
2851 {
2852 GetLine(i)->Draw( &dc );
2853 }
2854 }
2855
2856 #ifndef __WXMAC__
2857 // Don't draw rect outline under Mac at all.
2858 if ( HasCurrent() )
2859 {
2860 if ( m_hasFocus )
2861 {
2862 wxRect rect( GetLineHighlightRect( m_current ) );
2863 #ifndef __WXGTK20__
2864 dc.SetPen( *wxBLACK_PEN );
2865 dc.SetBrush( *wxTRANSPARENT_BRUSH );
2866 dc.DrawRectangle( rect );
2867 #else
2868 wxRendererNative::Get().DrawItemSelectionRect( this, dc, rect, wxCONTROL_CURRENT|wxCONTROL_FOCUSED );
2869
2870 #endif
2871 }
2872 }
2873 #endif
2874 }
2875
2876 void wxListMainWindow::HighlightAll( bool on )
2877 {
2878 if ( IsSingleSel() )
2879 {
2880 wxASSERT_MSG( !on, _T("can't do this in a single selection control") );
2881
2882 // we just have one item to turn off
2883 if ( HasCurrent() && IsHighlighted(m_current) )
2884 {
2885 HighlightLine(m_current, false);
2886 RefreshLine(m_current);
2887 }
2888 }
2889 else // multi selection
2890 {
2891 if ( !IsEmpty() )
2892 HighlightLines(0, GetItemCount() - 1, on);
2893 }
2894 }
2895
2896 void wxListMainWindow::OnChildFocus(wxChildFocusEvent& WXUNUSED(event))
2897 {
2898 // Do nothing here. This prevents the default handler in wxScrolledWindow
2899 // from needlessly scrolling the window when the edit control is
2900 // dismissed. See ticket #9563.
2901 }
2902
2903 void wxListMainWindow::SendNotify( size_t line,
2904 wxEventType command,
2905 const wxPoint& point )
2906 {
2907 wxListEvent le( command, GetParent()->GetId() );
2908 le.SetEventObject( GetParent() );
2909
2910 le.m_itemIndex = line;
2911
2912 // set only for events which have position
2913 if ( point != wxDefaultPosition )
2914 le.m_pointDrag = point;
2915
2916 // don't try to get the line info for virtual list controls: the main
2917 // program has it anyhow and if we did it would result in accessing all
2918 // the lines, even those which are not visible now and this is precisely
2919 // what we're trying to avoid
2920 if ( !IsVirtual() )
2921 {
2922 if ( line != (size_t)-1 )
2923 {
2924 GetLine(line)->GetItem( 0, le.m_item );
2925 }
2926 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
2927 }
2928 //else: there may be no more such item
2929
2930 GetParent()->GetEventHandler()->ProcessEvent( le );
2931 }
2932
2933 void wxListMainWindow::ChangeCurrent(size_t current)
2934 {
2935 m_current = current;
2936
2937 // as the current item changed, we shouldn't start editing it when the
2938 // "slow click" timer expires as the click happened on another item
2939 if ( m_renameTimer->IsRunning() )
2940 m_renameTimer->Stop();
2941
2942 SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
2943 }
2944
2945 wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
2946 {
2947 wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
2948 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
2949
2950 wxASSERT_MSG( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)),
2951 wxT("EditLabel() needs a text control") );
2952
2953 size_t itemEdit = (size_t)item;
2954
2955 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
2956 le.SetEventObject( GetParent() );
2957 le.m_itemIndex = item;
2958 wxListLineData *data = GetLine(itemEdit);
2959 wxCHECK_MSG( data, NULL, _T("invalid index in EditLabel()") );
2960 data->GetItem( 0, le.m_item );
2961
2962 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
2963 {
2964 // vetoed by user code
2965 return NULL;
2966 }
2967
2968 // We have to call this here because the label in question might just have
2969 // been added and no screen update taken place.
2970 if ( m_dirty )
2971 {
2972 wxSafeYield();
2973
2974 // Pending events dispatched by wxSafeYield might have changed the item
2975 // count
2976 if ( (size_t)item >= GetItemCount() )
2977 return NULL;
2978 }
2979
2980 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
2981 m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
2982 return m_textctrlWrapper->GetText();
2983 }
2984
2985 void wxListMainWindow::OnRenameTimer()
2986 {
2987 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2988
2989 EditLabel( m_current );
2990 }
2991
2992 bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
2993 {
2994 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2995 le.SetEventObject( GetParent() );
2996 le.m_itemIndex = itemEdit;
2997
2998 wxListLineData *data = GetLine(itemEdit);
2999
3000 wxCHECK_MSG( data, false, _T("invalid index in OnRenameAccept()") );
3001
3002 data->GetItem( 0, le.m_item );
3003 le.m_item.m_text = value;
3004 return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
3005 le.IsAllowed();
3006 }
3007
3008 void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
3009 {
3010 // let owner know that the edit was cancelled
3011 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
3012
3013 le.SetEditCanceled(true);
3014
3015 le.SetEventObject( GetParent() );
3016 le.m_itemIndex = itemEdit;
3017
3018 wxListLineData *data = GetLine(itemEdit);
3019 wxCHECK_RET( data, _T("invalid index in OnRenameCancelled()") );
3020
3021 data->GetItem( 0, le.m_item );
3022 GetEventHandler()->ProcessEvent( le );
3023 }
3024
3025 void wxListMainWindow::OnMouse( wxMouseEvent &event )
3026 {
3027
3028 #ifdef __WXMAC__
3029 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
3030 // shutdown the edit control when the mouse is clicked elsewhere on the
3031 // listctrl because the order of events is different (or something like
3032 // that), so explicitly end the edit if it is active.
3033 if ( event.LeftDown() && m_textctrlWrapper )
3034 m_textctrlWrapper->AcceptChangesAndFinish();
3035 #endif // __WXMAC__
3036
3037 if ( event.LeftDown() )
3038 SetFocusIgnoringChildren();
3039
3040 event.SetEventObject( GetParent() );
3041 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3042 return;
3043
3044 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
3045 {
3046 // let the base handle mouse wheel events.
3047 event.Skip();
3048 return;
3049 }
3050
3051 if ( !HasCurrent() || IsEmpty() )
3052 {
3053 if (event.RightDown())
3054 {
3055 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3056 // Allow generation of context menu event
3057 event.Skip();
3058 }
3059 return;
3060 }
3061
3062 if (m_dirty)
3063 return;
3064
3065 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
3066 event.ButtonDClick()) )
3067 return;
3068
3069 int x = event.GetX();
3070 int y = event.GetY();
3071 CalcUnscrolledPosition( x, y, &x, &y );
3072
3073 // where did we hit it (if we did)?
3074 long hitResult = 0;
3075
3076 size_t count = GetItemCount(),
3077 current;
3078
3079 if ( InReportView() )
3080 {
3081 current = y / GetLineHeight();
3082 if ( current < count )
3083 hitResult = HitTestLine(current, x, y);
3084 }
3085 else // !report
3086 {
3087 // TODO: optimize it too! this is less simple than for report view but
3088 // enumerating all items is still not a way to do it!!
3089 for ( current = 0; current < count; current++ )
3090 {
3091 hitResult = HitTestLine(current, x, y);
3092 if ( hitResult )
3093 break;
3094 }
3095 }
3096
3097 if (event.Dragging())
3098 {
3099 if (m_dragCount == 0)
3100 {
3101 // we have to report the raw, physical coords as we want to be
3102 // able to call HitTest(event.m_pointDrag) from the user code to
3103 // get the item being dragged
3104 m_dragStart = event.GetPosition();
3105 }
3106
3107 m_dragCount++;
3108
3109 if (m_dragCount != 3)
3110 return;
3111
3112 int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
3113 : wxEVT_COMMAND_LIST_BEGIN_DRAG;
3114
3115 wxListEvent le( command, GetParent()->GetId() );
3116 le.SetEventObject( GetParent() );
3117 le.m_itemIndex = m_lineLastClicked;
3118 le.m_pointDrag = m_dragStart;
3119 GetParent()->GetEventHandler()->ProcessEvent( le );
3120
3121 return;
3122 }
3123 else
3124 {
3125 m_dragCount = 0;
3126 }
3127
3128 if ( !hitResult )
3129 {
3130 // outside of any item
3131 if (event.RightDown())
3132 {
3133 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3134
3135 wxContextMenuEvent evtCtx(
3136 wxEVT_CONTEXT_MENU,
3137 GetParent()->GetId(),
3138 ClientToScreen(event.GetPosition()));
3139 evtCtx.SetEventObject(GetParent());
3140 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
3141 }
3142 else
3143 {
3144 // reset the selection and bail out
3145 HighlightAll(false);
3146 }
3147
3148 return;
3149 }
3150
3151 bool forceClick = false;
3152 if (event.ButtonDClick())
3153 {
3154 if ( m_renameTimer->IsRunning() )
3155 m_renameTimer->Stop();
3156
3157 m_lastOnSame = false;
3158
3159 if ( current == m_lineLastClicked )
3160 {
3161 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3162
3163 return;
3164 }
3165 else
3166 {
3167 // The first click was on another item, so don't interpret this as
3168 // a double click, but as a simple click instead
3169 forceClick = true;
3170 }
3171 }
3172
3173 if (event.LeftUp())
3174 {
3175 if (m_lineSelectSingleOnUp != (size_t)-1)
3176 {
3177 // select single line
3178 HighlightAll( false );
3179 ReverseHighlight(m_lineSelectSingleOnUp);
3180 }
3181
3182 if (m_lastOnSame)
3183 {
3184 if ((current == m_current) &&
3185 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
3186 HasFlag(wxLC_EDIT_LABELS) )
3187 {
3188 if (InReportView())
3189 {
3190 wxRect label = GetLineLabelRect( current );
3191 if (label.Contains( x, y ))
3192 m_renameTimer->Start( 250, true );
3193
3194 }
3195 else
3196 m_renameTimer->Start( 250, true );
3197 }
3198 }
3199
3200 m_lastOnSame = false;
3201 m_lineSelectSingleOnUp = (size_t)-1;
3202 }
3203 else
3204 {
3205 // This is necessary, because after a DnD operation in
3206 // from and to ourself, the up event is swallowed by the
3207 // DnD code. So on next non-up event (which means here and
3208 // now) m_lineSelectSingleOnUp should be reset.
3209 m_lineSelectSingleOnUp = (size_t)-1;
3210 }
3211 if (event.RightDown())
3212 {
3213 m_lineBeforeLastClicked = m_lineLastClicked;
3214 m_lineLastClicked = current;
3215
3216 // If the item is already selected, do not update the selection.
3217 // Multi-selections should not be cleared if a selected item is clicked.
3218 if (!IsHighlighted(current))
3219 {
3220 HighlightAll(false);
3221 ChangeCurrent(current);
3222 ReverseHighlight(m_current);
3223 }
3224
3225 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
3226
3227 wxContextMenuEvent evtCtx(
3228 wxEVT_CONTEXT_MENU,
3229 GetParent()->GetId(),
3230 ClientToScreen(event.GetPosition()));
3231 evtCtx.SetEventObject(GetParent());
3232 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
3233 }
3234 else if (event.MiddleDown())
3235 {
3236 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
3237 }
3238 else if ( event.LeftDown() || forceClick )
3239 {
3240 m_lineBeforeLastClicked = m_lineLastClicked;
3241 m_lineLastClicked = current;
3242
3243 size_t oldCurrent = m_current;
3244 bool oldWasSelected = IsHighlighted(m_current);
3245
3246 bool cmdModifierDown = event.CmdDown();
3247 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
3248 {
3249 if ( IsSingleSel() || !IsHighlighted(current) )
3250 {
3251 HighlightAll( false );
3252
3253 ChangeCurrent(current);
3254
3255 ReverseHighlight(m_current);
3256 }
3257 else // multi sel & current is highlighted & no mod keys
3258 {
3259 m_lineSelectSingleOnUp = current;
3260 ChangeCurrent(current); // change focus
3261 }
3262 }
3263 else // multi sel & either ctrl or shift is down
3264 {
3265 if (cmdModifierDown)
3266 {
3267 ChangeCurrent(current);
3268
3269 ReverseHighlight(m_current);
3270 }
3271 else if (event.ShiftDown())
3272 {
3273 ChangeCurrent(current);
3274
3275 size_t lineFrom = oldCurrent,
3276 lineTo = current;
3277
3278 if ( lineTo < lineFrom )
3279 {
3280 lineTo = lineFrom;
3281 lineFrom = m_current;
3282 }
3283
3284 HighlightLines(lineFrom, lineTo);
3285 }
3286 else // !ctrl, !shift
3287 {
3288 // test in the enclosing if should make it impossible
3289 wxFAIL_MSG( _T("how did we get here?") );
3290 }
3291 }
3292
3293 if (m_current != oldCurrent)
3294 RefreshLine( oldCurrent );
3295
3296 // forceClick is only set if the previous click was on another item
3297 m_lastOnSame = !forceClick && (m_current == oldCurrent) && oldWasSelected;
3298 }
3299 }
3300
3301 void wxListMainWindow::MoveToItem(size_t item)
3302 {
3303 if ( item == (size_t)-1 )
3304 return;
3305
3306 wxRect rect = GetLineRect(item);
3307
3308 int client_w, client_h;
3309 GetClientSize( &client_w, &client_h );
3310
3311 const int hLine = GetLineHeight();
3312
3313 int view_x = SCROLL_UNIT_X * GetScrollPos( wxHORIZONTAL );
3314 int view_y = hLine * GetScrollPos( wxVERTICAL );
3315
3316 if ( InReportView() )
3317 {
3318 // the next we need the range of lines shown it might be different,
3319 // so recalculate it
3320 ResetVisibleLinesRange();
3321
3322 if (rect.y < view_y)
3323 Scroll( -1, rect.y / hLine );
3324 if (rect.y + rect.height + 5 > view_y + client_h)
3325 Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
3326
3327 #ifdef __WXMAC__
3328 // At least on Mac the visible lines value will get reset inside of
3329 // Scroll *before* it actually scrolls the window because of the
3330 // Update() that happens there, so it will still have the wrong value.
3331 // So let's reset it again and wait for it to be recalculated in the
3332 // next paint event. I would expect this problem to show up in wxGTK
3333 // too but couldn't duplicate it there. Perhaps the order of events
3334 // is different... --Robin
3335 ResetVisibleLinesRange();
3336 #endif
3337 }
3338 else // !report
3339 {
3340 int sx = -1,
3341 sy = -1;
3342
3343 if (rect.x-view_x < 5)
3344 sx = (rect.x - 5) / SCROLL_UNIT_X;
3345 if (rect.x + rect.width - 5 > view_x + client_w)
3346 sx = (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X;
3347
3348 if (rect.y-view_y < 5)
3349 sy = (rect.y - 5) / hLine;
3350 if (rect.y + rect.height - 5 > view_y + client_h)
3351 sy = (rect.y + rect.height - client_h + hLine) / hLine;
3352
3353 Scroll(sx, sy);
3354 }
3355 }
3356
3357 bool wxListMainWindow::ScrollList(int WXUNUSED(dx), int dy)
3358 {
3359 if ( !InReportView() )
3360 {
3361 // TODO: this should work in all views but is not implemented now
3362 return false;
3363 }
3364
3365 size_t top, bottom;
3366 GetVisibleLinesRange(&top, &bottom);
3367
3368 if ( bottom == (size_t)-1 )
3369 return 0;
3370
3371 ResetVisibleLinesRange();
3372
3373 int hLine = GetLineHeight();
3374
3375 Scroll(-1, top + dy / hLine);
3376
3377 #ifdef __WXMAC__
3378 // see comment in MoveToItem() for why we do this
3379 ResetVisibleLinesRange();
3380 #endif
3381
3382 return true;
3383 }
3384
3385 // ----------------------------------------------------------------------------
3386 // keyboard handling
3387 // ----------------------------------------------------------------------------
3388
3389 void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
3390 {
3391 wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
3392 _T("invalid item index in OnArrowChar()") );
3393
3394 size_t oldCurrent = m_current;
3395
3396 // in single selection we just ignore Shift as we can't select several
3397 // items anyhow
3398 if ( event.ShiftDown() && !IsSingleSel() )
3399 {
3400 ChangeCurrent(newCurrent);
3401
3402 // refresh the old focus to remove it
3403 RefreshLine( oldCurrent );
3404
3405 // select all the items between the old and the new one
3406 if ( oldCurrent > newCurrent )
3407 {
3408 newCurrent = oldCurrent;
3409 oldCurrent = m_current;
3410 }
3411
3412 HighlightLines(oldCurrent, newCurrent);
3413 }
3414 else // !shift
3415 {
3416 // all previously selected items are unselected unless ctrl is held
3417 // in a multiselection control
3418 if ( !event.ControlDown() || IsSingleSel() )
3419 HighlightAll(false);
3420
3421 ChangeCurrent(newCurrent);
3422
3423 // refresh the old focus to remove it
3424 RefreshLine( oldCurrent );
3425
3426 // in single selection mode we must always have a selected item
3427 if ( !event.ControlDown() || IsSingleSel() )
3428 HighlightLine( m_current, true );
3429 }
3430
3431 RefreshLine( m_current );
3432
3433 MoveToFocus();
3434 }
3435
3436 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
3437 {
3438 wxWindow *parent = GetParent();
3439
3440 // propagate the key event upwards
3441 wxKeyEvent ke(event);
3442 if (parent->GetEventHandler()->ProcessEvent( ke ))
3443 return;
3444
3445 event.Skip();
3446 }
3447
3448 void wxListMainWindow::OnKeyUp( wxKeyEvent &event )
3449 {
3450 wxWindow *parent = GetParent();
3451
3452 // propagate the key event upwards
3453 wxKeyEvent ke(event);
3454 ke.SetEventObject( parent );
3455 if (parent->GetEventHandler()->ProcessEvent( ke ))
3456 return;
3457
3458 event.Skip();
3459 }
3460
3461 void wxListMainWindow::OnChar( wxKeyEvent &event )
3462 {
3463 wxWindow *parent = GetParent();
3464
3465 // send a list_key event up
3466 if ( HasCurrent() )
3467 {
3468 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() );
3469 le.m_itemIndex = m_current;
3470 GetLine(m_current)->GetItem( 0, le.m_item );
3471 le.m_code = event.GetKeyCode();
3472 le.SetEventObject( parent );
3473 parent->GetEventHandler()->ProcessEvent( le );
3474 }
3475
3476 // propagate the char event upwards
3477 wxKeyEvent ke(event);
3478 if (parent->GetEventHandler()->ProcessEvent( ke ))
3479 return;
3480
3481 if (event.GetKeyCode() == WXK_TAB)
3482 {
3483 wxNavigationKeyEvent nevent;
3484 nevent.SetWindowChange( event.ControlDown() );
3485 nevent.SetDirection( !event.ShiftDown() );
3486 nevent.SetEventObject( GetParent()->GetParent() );
3487 nevent.SetCurrentFocus( m_parent );
3488 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent ))
3489 return;
3490 }
3491
3492 // no item -> nothing to do
3493 if (!HasCurrent())
3494 {
3495 event.Skip();
3496 return;
3497 }
3498
3499 // don't use m_linesPerPage directly as it might not be computed yet
3500 const int pageSize = GetCountPerPage();
3501 wxCHECK_RET( pageSize, _T("should have non zero page size") );
3502
3503 if (GetLayoutDirection() == wxLayout_RightToLeft)
3504 {
3505 if (event.GetKeyCode() == WXK_RIGHT)
3506 event.m_keyCode = WXK_LEFT;
3507 else if (event.GetKeyCode() == WXK_LEFT)
3508 event.m_keyCode = WXK_RIGHT;
3509 }
3510
3511 switch ( event.GetKeyCode() )
3512 {
3513 case WXK_UP:
3514 if ( m_current > 0 )
3515 OnArrowChar( m_current - 1, event );
3516 break;
3517
3518 case WXK_DOWN:
3519 if ( m_current < (size_t)GetItemCount() - 1 )
3520 OnArrowChar( m_current + 1, event );
3521 break;
3522
3523 case WXK_END:
3524 if (!IsEmpty())
3525 OnArrowChar( GetItemCount() - 1, event );
3526 break;
3527
3528 case WXK_HOME:
3529 if (!IsEmpty())
3530 OnArrowChar( 0, event );
3531 break;
3532
3533 case WXK_PAGEUP:
3534 {
3535 int steps = InReportView() ? pageSize - 1
3536 : m_current % pageSize;
3537
3538 int index = m_current - steps;
3539 if (index < 0)
3540 index = 0;
3541
3542 OnArrowChar( index, event );
3543 }
3544 break;
3545
3546 case WXK_PAGEDOWN:
3547 {
3548 int steps = InReportView()
3549 ? pageSize - 1
3550 : pageSize - (m_current % pageSize) - 1;
3551
3552 size_t index = m_current + steps;
3553 size_t count = GetItemCount();
3554 if ( index >= count )
3555 index = count - 1;
3556
3557 OnArrowChar( index, event );
3558 }
3559 break;
3560
3561 case WXK_LEFT:
3562 if ( !InReportView() )
3563 {
3564 int index = m_current - pageSize;
3565 if (index < 0)
3566 index = 0;
3567
3568 OnArrowChar( index, event );
3569 }
3570 break;
3571
3572 case WXK_RIGHT:
3573 if ( !InReportView() )
3574 {
3575 size_t index = m_current + pageSize;
3576
3577 size_t count = GetItemCount();
3578 if ( index >= count )
3579 index = count - 1;
3580
3581 OnArrowChar( index, event );
3582 }
3583 break;
3584
3585 case WXK_SPACE:
3586 if ( IsSingleSel() )
3587 {
3588 if ( event.ControlDown() )
3589 {
3590 ReverseHighlight(m_current);
3591 }
3592 else // normal space press
3593 {
3594 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
3595 }
3596 }
3597 else // multiple selection
3598 {