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