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