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