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