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