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