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