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