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