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