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