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