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