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