1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Vadim Zeitlin (virtual list control support)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
20 #pragma implementation "listctrl.h"
21 #pragma implementation "listctrlbase.h"
25 #include "listctrl.old.cpp"
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
37 #include "wx/dcscreen.h"
39 #include "wx/listctrl.h"
40 #include "wx/generic/imaglist.h"
41 #include "wx/dynarray.h"
45 #include "wx/gtk/win_gtk.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
58 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
59 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
60 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
61 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
62 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
63 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
64 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
65 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
66 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
67 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // the height of the header window (FIXME: should depend on its font!)
74 static const int HEADER_HEIGHT
= 23;
76 // the scrollbar units
77 static const int SCROLL_UNIT_X
= 15;
78 static const int SCROLL_UNIT_Y
= 15;
80 // the spacing between the lines (in report mode)
81 static const int LINE_SPACING
= 1;
83 // extra margins around the text label
84 static const int EXTRA_WIDTH
= 3;
85 static const int EXTRA_HEIGHT
= 4;
87 // offset for the header window
88 static const int HEADER_OFFSET_X
= 1;
89 static const int HEADER_OFFSET_Y
= 1;
91 // when autosizing the columns, add some slack
92 static const int AUTOSIZE_COL_MARGIN
= 10;
94 // default and minimal widths for the header columns
95 static const int WIDTH_COL_DEFAULT
= 80;
96 static const int WIDTH_COL_MIN
= 10;
98 // ============================================================================
100 // ============================================================================
102 //-----------------------------------------------------------------------------
103 // wxListItemData (internal)
104 //-----------------------------------------------------------------------------
106 class WXDLLEXPORT wxListItemData
109 wxListItemData(wxListMainWindow
*owner
);
110 ~wxListItemData() { delete m_attr
; delete m_rect
; }
112 void SetItem( const wxListItem
&info
);
113 void SetImage( int image
) { m_image
= image
; }
114 void SetData( long data
) { m_data
= data
; }
115 void SetPosition( int x
, int y
);
116 void SetSize( int width
, int height
);
118 bool HasText() const { return !m_text
.empty(); }
119 const wxString
& GetText() const { return m_text
; }
120 void SetText(const wxString
& text
) { m_text
= text
; }
122 // we can't use empty string for measuring the string width/height, so
123 // always return something
124 wxString
GetTextForMeasuring() const
126 wxString s
= GetText();
133 bool IsHit( int x
, int y
) const;
137 int GetWidth() const;
138 int GetHeight() const;
140 int GetImage() const { return m_image
; }
141 bool HasImage() const { return GetImage() != -1; }
143 void GetItem( wxListItem
&info
) const;
145 wxListItemAttr
*GetAttributes() const { return m_attr
; }
148 // the item image or -1
151 // user data associated with the item
154 // the item coordinates are not used in report mode, instead this pointer
155 // is NULL and the owner window is used to retrieve the item position and
159 // the list ctrl we are in
160 wxListMainWindow
*m_owner
;
162 // custom attributes or NULL
163 wxListItemAttr
*m_attr
;
166 // common part of all ctors
172 //-----------------------------------------------------------------------------
173 // wxListHeaderData (internal)
174 //-----------------------------------------------------------------------------
176 class WXDLLEXPORT wxListHeaderData
: public wxObject
190 wxListHeaderData( const wxListItem
&info
);
191 void SetItem( const wxListItem
&item
);
192 void SetPosition( int x
, int y
);
193 void SetWidth( int w
);
194 void SetFormat( int format
);
195 void SetHeight( int h
);
196 bool HasImage() const;
198 bool HasText() const { return !m_text
.empty(); }
199 const wxString
& GetText() const { return m_text
; }
200 void SetText(const wxString
& text
) { m_text
= text
; }
202 void GetItem( wxListItem
&item
);
204 bool IsHit( int x
, int y
) const;
205 int GetImage() const;
206 int GetWidth() const;
207 int GetFormat() const;
210 DECLARE_DYNAMIC_CLASS(wxListHeaderData
);
213 //-----------------------------------------------------------------------------
214 // wxListLineData (internal)
215 //-----------------------------------------------------------------------------
217 WX_DECLARE_LIST(wxListItemData
, wxListItemDataList
);
218 #include "wx/listimpl.cpp"
219 WX_DEFINE_LIST(wxListItemDataList
);
221 class WXDLLEXPORT wxListLineData
224 // the list of subitems: only may have more than one item in report mode
225 wxListItemDataList m_items
;
227 // this is not used in report view
239 // the part to be highlighted
240 wxRect m_rectHilight
;
243 // is this item selected? [NB: not used in virtual mode]
246 // back pointer to the list ctrl
247 wxListMainWindow
*m_owner
;
250 wxListLineData( wxListMainWindow
*owner
, size_t line
);
252 ~wxListLineData() { delete m_gi
; }
254 // are we in report mode?
255 inline bool InReportView() const;
257 // are we in virtual report mode?
258 inline bool IsVirtal() const;
260 // these 2 methods shouldn't be called for report view controls, in that
261 // case we determine our position/size ourselves
263 // calculate the size of the line
264 void CalculateSize( wxDC
*dc
, int spacing
);
266 // remember the position this line appears at
267 void SetPosition( int x
, int y
, int window_width
, int spacing
);
269 long IsHit( int x
, int y
);
271 void SetItem( int index
, const wxListItem
&info
);
272 void GetItem( int index
, wxListItem
&info
);
274 wxString
GetText(int index
) const;
275 void SetText( int index
, const wxString s
);
277 void SetImage( int index
, int image
);
278 int GetImage( int index
) const;
280 // get the bound rect of this line
281 wxRect
GetRect() const;
283 // get the bound rect of the label
284 wxRect
GetLabelRect() const;
286 // get the bound rect of the items icon (only may be called if we do have
288 wxRect
GetIconRect() const;
290 // get the rect to be highlighted when the item has focus
291 wxRect
GetHighlightRect() const;
293 // get the size of the total line rect
294 wxSize
GetSize() const { return GetRect().GetSize(); }
296 // return true if the highlighting really changed
297 bool Hilight( bool on
);
299 void ReverseHilight();
301 // draw the line on the given DC
302 void Draw( wxDC
*dc
, int y
= 0, int height
= 0, bool highlighted
= FALSE
);
304 bool IsHilighted() const
306 wxASSERT_MSG( !IsVirtal(), _T("unexpected call to IsHilighted") );
311 // only for wxListMainWindow::CacheLineData()
312 void SetLineIndex(size_t line
) { m_lineIndex
= line
; }
315 // set the line to contain num items (only can be > 1 in report mode)
316 void InitItems( int num
);
318 // get the mode (i.e. style) of the list control
319 inline int GetMode() const;
321 void SetAttributes(wxDC
*dc
,
322 const wxListItemAttr
*attr
,
323 const wxColour
& colText
,
327 // the index of this line (only used in report mode)
332 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData
, wxListLineDataArray
);
333 #include "wx/arrimpl.cpp"
334 WX_DEFINE_OBJARRAY(wxListLineDataArray
);
336 //-----------------------------------------------------------------------------
337 // wxListHeaderWindow (internal)
338 //-----------------------------------------------------------------------------
340 class WXDLLEXPORT wxListHeaderWindow
: public wxWindow
343 wxListMainWindow
*m_owner
;
344 wxCursor
*m_currentCursor
;
345 wxCursor
*m_resizeCursor
;
348 // column being resized
351 // divider line position in logical (unscrolled) coords
354 // minimal position beyond which the divider line can't be dragged in
359 wxListHeaderWindow();
360 virtual ~wxListHeaderWindow();
362 wxListHeaderWindow( wxWindow
*win
,
364 wxListMainWindow
*owner
,
365 const wxPoint
&pos
= wxDefaultPosition
,
366 const wxSize
&size
= wxDefaultSize
,
368 const wxString
&name
= "wxlistctrlcolumntitles" );
370 void DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
);
372 void AdjustDC(wxDC
& dc
);
374 void OnPaint( wxPaintEvent
&event
);
375 void OnMouse( wxMouseEvent
&event
);
376 void OnSetFocus( wxFocusEvent
&event
);
382 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow
)
383 DECLARE_EVENT_TABLE()
386 //-----------------------------------------------------------------------------
387 // wxListRenameTimer (internal)
388 //-----------------------------------------------------------------------------
390 class WXDLLEXPORT wxListRenameTimer
: public wxTimer
393 wxListMainWindow
*m_owner
;
396 wxListRenameTimer( wxListMainWindow
*owner
);
400 //-----------------------------------------------------------------------------
401 // wxListTextCtrl (internal)
402 //-----------------------------------------------------------------------------
404 class WXDLLEXPORT wxListTextCtrl
: public wxTextCtrl
409 wxListMainWindow
*m_owner
;
410 wxString m_startValue
;
414 wxListTextCtrl( wxWindow
*parent
, const wxWindowID id
,
415 bool *accept
, wxString
*res
, wxListMainWindow
*owner
,
416 const wxString
&value
= "",
417 const wxPoint
&pos
= wxDefaultPosition
, const wxSize
&size
= wxDefaultSize
,
419 const wxValidator
& validator
= wxDefaultValidator
,
420 const wxString
&name
= "listctrltextctrl" );
421 void OnChar( wxKeyEvent
&event
);
422 void OnKeyUp( wxKeyEvent
&event
);
423 void OnKillFocus( wxFocusEvent
&event
);
426 DECLARE_DYNAMIC_CLASS(wxListTextCtrl
);
427 DECLARE_EVENT_TABLE()
430 //-----------------------------------------------------------------------------
431 // wxListMainWindow (internal)
432 //-----------------------------------------------------------------------------
434 WX_DECLARE_LIST(wxListHeaderData
, wxListHeaderDataList
);
435 #include "wx/listimpl.cpp"
436 WX_DEFINE_LIST(wxListHeaderDataList
);
438 class WXDLLEXPORT wxListMainWindow
: public wxScrolledWindow
442 wxListMainWindow( wxWindow
*parent
,
444 const wxPoint
& pos
= wxDefaultPosition
,
445 const wxSize
& size
= wxDefaultSize
,
447 const wxString
&name
= _T("listctrlmainwindow") );
449 virtual ~wxListMainWindow();
451 bool HasFlag(int flag
) const { return m_parent
->HasFlag(flag
); }
453 // return true if this is a virtual list control
454 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL
); }
456 // do we have a header window?
457 bool HasHeader() const
458 { return HasFlag(wxLC_REPORT
) && !HasFlag(wxLC_NO_HEADER
); }
460 void HilightAll( bool on
);
462 // all these functions only do something if the line is currently visible
464 // change the line "selected" state, return TRUE if it really changed
465 bool HilightLine( size_t line
, bool hilight
= TRUE
);
467 // toggle the line state and refresh it
468 void ReverseHilight( size_t line
)
469 { HilightLine(line
, !IsHilighted(line
)); RefreshLine(line
); }
471 // refresh one or several lines at once
472 void RefreshLine( size_t line
);
473 void RefreshLines( size_t lineFrom
, size_t lineTo
);
475 // return true if the line is highlighted
476 bool IsHilighted(size_t line
) const;
478 void EditLabel( long item
);
479 void OnRenameTimer();
480 void OnRenameAccept();
482 void OnMouse( wxMouseEvent
&event
);
485 // called to switch the selection from the current item to newCurrent,
486 void OnArrowChar( size_t newCurrent
, const wxKeyEvent
& event
);
488 void OnChar( wxKeyEvent
&event
);
489 void OnKeyDown( wxKeyEvent
&event
);
490 void OnSetFocus( wxFocusEvent
&event
);
491 void OnKillFocus( wxFocusEvent
&event
);
492 void OnSize( wxSizeEvent
&event
);
493 void OnScroll(wxScrollWinEvent
& event
) ;
495 void OnPaint( wxPaintEvent
&event
);
497 void DrawImage( int index
, wxDC
*dc
, int x
, int y
);
498 void GetImageSize( int index
, int &width
, int &height
);
499 int GetTextLength( const wxString
&s
);
501 void SetImageList( wxImageList
*imageList
, int which
);
502 void SetItemSpacing( int spacing
, bool isSmall
= FALSE
);
503 int GetItemSpacing( bool isSmall
= FALSE
);
505 void SetColumn( int col
, wxListItem
&item
);
506 void SetColumnWidth( int col
, int width
);
507 void GetColumn( int col
, wxListItem
&item
) const;
508 int GetColumnWidth( int col
) const;
509 int GetColumnCount() const { return m_columns
.GetCount(); }
511 // returns the sum of the heights of all columns
512 int GetHeaderWidth() const;
514 int GetCountPerPage() { return m_linesPerPage
; }
516 void SetItem( wxListItem
&item
);
517 void GetItem( wxListItem
&item
);
518 void SetItemState( long item
, long state
, long stateMask
);
519 int GetItemState( long item
, long stateMask
);
520 void GetItemRect( long index
, wxRect
&rect
);
521 bool GetItemPosition( long item
, wxPoint
& pos
);
522 int GetSelectedItemCount();
524 // set the scrollbars and update the positions of the items
525 void CalculatePositions();
527 long GetNextItem( long item
, int geometry
, int state
);
528 void DeleteItem( long index
);
529 void DeleteAllItems();
530 void DeleteColumn( int col
);
531 void DeleteEverything();
532 void EnsureVisible( long index
);
533 long FindItem( long start
, const wxString
& str
, bool partial
= FALSE
);
534 long FindItem( long start
, long data
);
535 long HitTest( int x
, int y
, int &flags
);
536 void InsertItem( wxListItem
&item
);
537 void InsertColumn( long col
, wxListItem
&item
);
538 void SortItems( wxListCtrlCompare fn
, long data
);
540 size_t GetItemCount() const;
541 bool IsEmpty() const { return GetItemCount() == 0; }
542 void SetItemCount(long count
);
544 bool HasCurrent() const { return m_current
!= (size_t)-1; }
546 // send out a wxListEvent
547 void SendNotify( size_t line
,
549 wxPoint point
= wxDefaultPosition
);
551 // called by wxListCtrl when its font changes
552 void OnFontChange() { m_lineHeight
= 0; }
554 // these are for wxListLineData usage only
556 // get the backpointer to the list ctrl
557 wxListCtrl
*GetListCtrl() const
559 return wxStaticCast(GetParent(), wxListCtrl
);
562 // get the height of all lines (assuming they all do have the same height)
563 wxCoord
GetLineHeight() const;
565 // get the y position of the given line (only for report view)
566 wxCoord
GetLineY(size_t line
) const;
569 // the array of all line objects for a non virtual list control
570 wxListLineDataArray m_lines
;
572 // the list of column objects
573 wxListHeaderDataList m_columns
;
575 // currently focused item or -1
578 // the item currently being edited or -1
579 size_t m_currentEdit
;
581 // the number of lines per page
584 // this flag is set when something which should result in the window
585 // redrawing happens (i.e. an item was added or deleted, or its appearance
586 // changed) and OnPaint() doesn't redraw the window while it is set which
587 // allows to minimize the number of repaintings when a lot of items are
588 // being added. The real repainting occurs only after the next OnIdle()
592 wxBrush
*m_hilightBrush
;
593 wxColour
*m_hilightColour
;
596 wxImageList
*m_small_image_list
;
597 wxImageList
*m_normal_image_list
;
599 int m_normal_spacing
;
603 wxTimer
*m_renameTimer
;
605 wxString m_renameRes
;
610 // for double click logic
611 size_t m_lineLastClicked
,
612 m_lineBeforeLastClicked
;
615 // the total count of items in a virtual list control
618 // the first and last lines being shown on screen right now (inclusive)
622 // the array containing the indices of all selected items, only used in
624 wxArrayInt m_selections
;
626 // common part of all ctors
629 // intiialize m_[xy]Scroll
630 void InitScrolling();
632 // get the line data for the given index
633 wxListLineData
*GetLine(size_t n
) const
635 wxASSERT_MSG( n
!= (size_t)-1, _T("invalid line index") );
639 wxConstCast(this, wxListMainWindow
)->CacheLineData(n
);
647 // get the first line: this one is special as we have it even in virtual
648 // list control (it is useful to cache it as we use it for measuring, hit
650 wxListLineData
*GetFirstLine() const;
652 // cache the line data of the n-th line in m_lines[0]
653 void CacheLineData(size_t line
);
655 // update m_lineFrom/To
656 void UpdateShownLinesRange();
659 // initialize the current item if needed
660 void UpdateCurrent();
662 // called when an item is [un]focuded, i.e. becomes [not] current
665 void OnFocusLine( size_t line
);
666 void OnUnfocusLine( size_t line
);
668 // the height of one line using the current font
669 wxCoord m_lineHeight
;
671 // the total header width or 0 if not calculated yet
672 wxCoord m_headerWidth
;
674 DECLARE_DYNAMIC_CLASS(wxListMainWindow
);
675 DECLARE_EVENT_TABLE()
678 // ============================================================================
680 // ============================================================================
682 //-----------------------------------------------------------------------------
684 //-----------------------------------------------------------------------------
686 void wxListItemData::Init()
694 wxListItemData::wxListItemData(wxListMainWindow
*owner
)
700 if ( owner
->HasFlag(wxLC_REPORT
) )
710 void wxListItemData::SetItem( const wxListItem
&info
)
712 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
713 SetText(info
.m_text
);
714 if ( info
.m_mask
& wxLIST_MASK_IMAGE
)
715 m_image
= info
.m_image
;
716 if ( info
.m_mask
& wxLIST_MASK_DATA
)
717 m_data
= info
.m_data
;
719 if ( info
.HasAttributes() )
722 *m_attr
= *info
.GetAttributes();
724 m_attr
= new wxListItemAttr(*info
.GetAttributes());
732 m_rect
->width
= info
.m_width
;
736 void wxListItemData::SetPosition( int x
, int y
)
738 wxCHECK_RET( m_rect
, _T("unexpected SetPosition() call") );
744 void wxListItemData::SetSize( int width
, int height
)
746 wxCHECK_RET( m_rect
, _T("unexpected SetSize() call") );
749 m_rect
->width
= width
;
751 m_rect
->height
= height
;
754 bool wxListItemData::IsHit( int x
, int y
) const
756 wxCHECK_MSG( m_rect
, FALSE
, _T("can't be called in this mode") );
758 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Inside(x
, y
);
761 int wxListItemData::GetX() const
763 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
768 int wxListItemData::GetY() const
770 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
775 int wxListItemData::GetWidth() const
777 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
779 return m_rect
->width
;
782 int wxListItemData::GetHeight() const
784 wxCHECK_MSG( m_rect
, 0, _T("can't be called in this mode") );
786 return m_rect
->height
;
789 void wxListItemData::GetItem( wxListItem
&info
) const
791 info
.m_text
= m_text
;
792 info
.m_image
= m_image
;
793 info
.m_data
= m_data
;
797 if ( m_attr
->HasTextColour() )
798 info
.SetTextColour(m_attr
->GetTextColour());
799 if ( m_attr
->HasBackgroundColour() )
800 info
.SetBackgroundColour(m_attr
->GetBackgroundColour());
801 if ( m_attr
->HasFont() )
802 info
.SetFont(m_attr
->GetFont());
806 //-----------------------------------------------------------------------------
808 //-----------------------------------------------------------------------------
810 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderData
,wxObject
);
812 wxListHeaderData::wxListHeaderData()
823 wxListHeaderData::wxListHeaderData( const wxListItem
&item
)
831 void wxListHeaderData::SetItem( const wxListItem
&item
)
833 m_mask
= item
.m_mask
;
834 m_text
= item
.m_text
;
835 m_image
= item
.m_image
;
836 m_format
= item
.m_format
;
838 SetWidth(item
.m_width
);
841 void wxListHeaderData::SetPosition( int x
, int y
)
847 void wxListHeaderData::SetHeight( int h
)
852 void wxListHeaderData::SetWidth( int w
)
856 m_width
= WIDTH_COL_DEFAULT
;
857 if (m_width
< WIDTH_COL_MIN
)
858 m_width
= WIDTH_COL_MIN
;
861 void wxListHeaderData::SetFormat( int format
)
866 bool wxListHeaderData::HasImage() const
868 return (m_image
!= 0);
871 bool wxListHeaderData::IsHit( int x
, int y
) const
873 return ((x
>= m_xpos
) && (x
<= m_xpos
+m_width
) && (y
>= m_ypos
) && (y
<= m_ypos
+m_height
));
876 void wxListHeaderData::GetItem( wxListItem
&item
)
878 item
.m_mask
= m_mask
;
879 item
.m_text
= m_text
;
880 item
.m_image
= m_image
;
881 item
.m_format
= m_format
;
882 item
.m_width
= m_width
;
885 int wxListHeaderData::GetImage() const
890 int wxListHeaderData::GetWidth() const
895 int wxListHeaderData::GetFormat() const
900 //-----------------------------------------------------------------------------
902 //-----------------------------------------------------------------------------
904 inline int wxListLineData::GetMode() const
906 return m_owner
->GetListCtrl()->GetWindowStyleFlag() & wxLC_MODE_MASK
;
909 inline bool wxListLineData::InReportView() const
911 return m_owner
->HasFlag(wxLC_REPORT
);
914 inline bool wxListLineData::IsVirtal() const
916 return m_owner
->IsVirtual();
919 wxListLineData::wxListLineData( wxListMainWindow
*owner
, size_t line
)
922 m_items
.DeleteContents( TRUE
);
926 if ( InReportView() )
932 m_gi
= new GeometryInfo
;
937 InitItems( GetMode() == wxLC_REPORT
? m_owner
->GetColumnCount() : 1 );
940 wxRect
wxListLineData::GetRect() const
942 if ( !InReportView() )
943 return m_gi
->m_rectAll
;
946 rect
.x
= HEADER_OFFSET_X
;
947 rect
.y
= m_owner
->GetLineY(m_lineIndex
);
948 rect
.width
= m_owner
->GetHeaderWidth();
949 rect
.height
= m_owner
->GetLineHeight();
954 wxRect
wxListLineData::GetLabelRect() const
956 if ( !InReportView() )
957 return m_gi
->m_rectLabel
;
960 rect
.x
= HEADER_OFFSET_X
;
961 rect
.y
= m_owner
->GetLineY(m_lineIndex
);
962 rect
.width
= m_owner
->GetColumnWidth(0);
963 rect
.height
= m_owner
->GetLineHeight();
968 wxRect
wxListLineData::GetIconRect() const
970 if ( !InReportView() )
971 return m_gi
->m_rectIcon
;
975 wxListItemDataList::Node
*node
= m_items
.GetFirst();
976 wxCHECK_MSG( node
, rect
, _T("no subitems at all??") );
978 wxListItemData
*item
= node
->GetData();
979 wxASSERT_MSG( item
->HasImage(), _T("GetIconRect() called but no image") );
981 rect
.x
= HEADER_OFFSET_X
;
982 rect
.y
= m_owner
->GetLineY(m_lineIndex
);
983 m_owner
->GetImageSize(item
->GetImage(), rect
.width
, rect
.height
);
988 wxRect
wxListLineData::GetHighlightRect() const
990 return InReportView() ? GetRect() : m_gi
->m_rectHilight
;
993 void wxListLineData::CalculateSize( wxDC
*dc
, int spacing
)
995 wxListItemDataList::Node
*node
= m_items
.GetFirst();
996 wxCHECK_RET( node
, _T("no subitems at all??") );
998 wxListItemData
*item
= node
->GetData();
1000 switch ( GetMode() )
1003 case wxLC_SMALL_ICON
:
1005 m_gi
->m_rectAll
.width
= spacing
;
1007 wxString s
= item
->GetText();
1013 m_gi
->m_rectLabel
.width
=
1014 m_gi
->m_rectLabel
.height
= 0;
1018 dc
->GetTextExtent( s
, &lw
, &lh
);
1019 if (lh
< SCROLL_UNIT_Y
)
1024 m_gi
->m_rectAll
.height
= spacing
+ lh
;
1026 m_gi
->m_rectAll
.width
= lw
;
1028 m_gi
->m_rectLabel
.width
= lw
;
1029 m_gi
->m_rectLabel
.height
= lh
;
1032 if (item
->HasImage())
1035 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1036 m_gi
->m_rectIcon
.width
= w
+ 8;
1037 m_gi
->m_rectIcon
.height
= h
+ 8;
1039 if ( m_gi
->m_rectIcon
.width
> m_gi
->m_rectAll
.width
)
1040 m_gi
->m_rectAll
.width
= m_gi
->m_rectIcon
.width
;
1041 if ( m_gi
->m_rectIcon
.height
+ lh
> m_gi
->m_rectAll
.height
- 4 )
1042 m_gi
->m_rectAll
.height
= m_gi
->m_rectIcon
.height
+ lh
+ 4;
1045 if ( item
->HasText() )
1047 m_gi
->m_rectHilight
.width
= m_gi
->m_rectLabel
.width
;
1048 m_gi
->m_rectHilight
.height
= m_gi
->m_rectLabel
.height
;
1050 else // no text, highlight the icon
1052 m_gi
->m_rectHilight
.width
= m_gi
->m_rectIcon
.width
;
1053 m_gi
->m_rectHilight
.height
= m_gi
->m_rectIcon
.height
;
1060 wxString s
= item
->GetTextForMeasuring();
1063 dc
->GetTextExtent( s
, &lw
, &lh
);
1064 if (lh
< SCROLL_UNIT_Y
)
1069 m_gi
->m_rectLabel
.width
= lw
;
1070 m_gi
->m_rectLabel
.height
= lh
;
1072 m_gi
->m_rectAll
.width
= lw
;
1073 m_gi
->m_rectAll
.height
= lh
;
1075 if (item
->HasImage())
1078 m_owner
->GetImageSize( item
->GetImage(), w
, h
);
1079 m_gi
->m_rectIcon
.width
= w
;
1080 m_gi
->m_rectIcon
.height
= h
;
1082 m_gi
->m_rectAll
.width
+= 4 + w
;
1083 if (h
> m_gi
->m_rectAll
.height
)
1084 m_gi
->m_rectAll
.height
= h
;
1087 m_gi
->m_rectHilight
.width
= m_gi
->m_rectAll
.width
;
1088 m_gi
->m_rectHilight
.height
= m_gi
->m_rectAll
.height
;
1093 wxFAIL_MSG( _T("unexpected call to SetSize") );
1097 wxFAIL_MSG( _T("unknown mode") );
1101 void wxListLineData::SetPosition( int x
, int y
,
1105 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1106 wxCHECK_RET( node
, _T("no subitems at all??") );
1108 wxListItemData
*item
= node
->GetData();
1110 switch ( GetMode() )
1113 case wxLC_SMALL_ICON
:
1114 m_gi
->m_rectAll
.x
= x
;
1115 m_gi
->m_rectAll
.y
= y
;
1117 if ( item
->HasImage() )
1119 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 4
1120 + (spacing
- m_gi
->m_rectIcon
.width
)/2;
1121 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 4;
1124 if ( item
->HasText() )
1126 if (m_gi
->m_rectAll
.width
> spacing
)
1127 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1129 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2 + (spacing
/2) - (m_gi
->m_rectLabel
.width
/2);
1130 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ m_gi
->m_rectAll
.height
+ 2 - m_gi
->m_rectLabel
.height
;
1131 m_gi
->m_rectHilight
.x
= m_gi
->m_rectLabel
.x
- 2;
1132 m_gi
->m_rectHilight
.y
= m_gi
->m_rectLabel
.y
- 2;
1134 else // no text, highlight the icon
1136 m_gi
->m_rectHilight
.x
= m_gi
->m_rectIcon
.x
- 4;
1137 m_gi
->m_rectHilight
.y
= m_gi
->m_rectIcon
.y
- 4;
1142 m_gi
->m_rectAll
.x
= x
;
1143 m_gi
->m_rectAll
.y
= y
;
1145 m_gi
->m_rectHilight
.x
= m_gi
->m_rectAll
.x
;
1146 m_gi
->m_rectHilight
.y
= m_gi
->m_rectAll
.y
;
1147 m_gi
->m_rectLabel
.y
= m_gi
->m_rectAll
.y
+ 2;
1149 if (item
->HasImage())
1151 m_gi
->m_rectIcon
.x
= m_gi
->m_rectAll
.x
+ 2;
1152 m_gi
->m_rectIcon
.y
= m_gi
->m_rectAll
.y
+ 2;
1153 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 6 + m_gi
->m_rectIcon
.width
;
1157 m_gi
->m_rectLabel
.x
= m_gi
->m_rectAll
.x
+ 2;
1162 wxFAIL_MSG( _T("unexpected call to SetPosition") );
1166 wxFAIL_MSG( _T("unknown mode") );
1170 long wxListLineData::IsHit( int x
, int y
)
1172 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1173 wxCHECK_MSG( node
, 0, _T("no subitems at all??") );
1175 wxListItemData
*item
= node
->GetData();
1176 if ( item
->HasImage() && GetIconRect().Inside(x
, y
) )
1177 return wxLIST_HITTEST_ONITEMICON
;
1179 if ( item
->HasText() )
1181 wxRect rect
= InReportView() ? GetRect() : GetLabelRect();
1182 if ( rect
.Inside(x
, y
) )
1183 return wxLIST_HITTEST_ONITEMLABEL
;
1189 void wxListLineData::InitItems( int num
)
1191 for (int i
= 0; i
< num
; i
++)
1192 m_items
.Append( new wxListItemData(m_owner
) );
1195 void wxListLineData::SetItem( int index
, const wxListItem
&info
)
1197 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1198 wxCHECK_RET( node
, _T("invalid column index in SetItem") );
1200 wxListItemData
*item
= node
->GetData();
1201 item
->SetItem( info
);
1204 void wxListLineData::GetItem( int index
, wxListItem
&info
)
1206 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1209 wxListItemData
*item
= node
->GetData();
1210 item
->GetItem( info
);
1214 wxString
wxListLineData::GetText(int index
) const
1218 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1221 wxListItemData
*item
= node
->GetData();
1222 s
= item
->GetText();
1228 void wxListLineData::SetText( int index
, const wxString s
)
1230 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1233 wxListItemData
*item
= node
->GetData();
1238 void wxListLineData::SetImage( int index
, int image
)
1240 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1241 wxCHECK_RET( node
, _T("invalid column index in SetImage()") );
1243 wxListItemData
*item
= node
->GetData();
1244 item
->SetImage(image
);
1247 int wxListLineData::GetImage( int index
) const
1249 wxListItemDataList::Node
*node
= m_items
.Item( index
);
1250 wxCHECK_MSG( node
, -1, _T("invalid column index in GetImage()") );
1252 wxListItemData
*item
= node
->GetData();
1253 return item
->GetImage();
1256 void wxListLineData::SetAttributes(wxDC
*dc
,
1257 const wxListItemAttr
*attr
,
1258 const wxColour
& colText
,
1262 // don't use foregroud colour for drawing highlighted items - this might
1263 // make them completely invisible (and there is no way to do bit
1264 // arithmetics on wxColour, unfortunately)
1265 if ( !hilight
&& attr
&& attr
->HasTextColour() )
1267 dc
->SetTextForeground(attr
->GetTextColour());
1271 dc
->SetTextForeground(colText
);
1274 if ( attr
&& attr
->HasFont() )
1276 dc
->SetFont(attr
->GetFont());
1284 void wxListLineData::Draw( wxDC
*dc
, int y
, int height
, bool hilighted
)
1286 wxRect rect
= GetRect();
1287 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1289 if ( !m_owner
->IsExposed( rect
) )
1292 wxWindow
*listctrl
= m_owner
->GetParent();
1294 // use our own flag if we maintain it
1295 if ( !m_owner
->IsVirtual() )
1296 hilighted
= m_hilighted
;
1298 // default foreground colour
1302 colText
= wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT
);
1306 colText
= listctrl
->GetForegroundColour();
1310 wxFont font
= listctrl
->GetFont();
1312 // VZ: currently we set the colours/fonts only once, but like this (i.e.
1313 // using SetAttributes() inside the loop), it will be trivial to
1314 // customize the subitems (in report mode) too.
1315 wxListItemData
*item
= m_items
.GetFirst()->GetData();
1316 wxListItemAttr
*attr
= item
->GetAttributes();
1317 SetAttributes(dc
, attr
, colText
, font
, hilighted
);
1319 bool hasBgCol
= attr
&& attr
->HasBackgroundColour();
1320 if ( hilighted
|| hasBgCol
)
1324 dc
->SetBrush( *m_owner
->m_hilightBrush
);
1329 dc
->SetBrush(wxBrush(attr
->GetBackgroundColour(), wxSOLID
));
1331 dc
->SetBrush( * wxWHITE_BRUSH
);
1334 dc
->SetPen( * wxTRANSPARENT_PEN
);
1335 dc
->DrawRectangle( GetHighlightRect() );
1338 wxListItemDataList::Node
*node
= m_items
.GetFirst();
1340 if ( GetMode() == wxLC_REPORT
)
1343 int x
= HEADER_OFFSET_X
;
1345 y
+= EXTRA_HEIGHT
/ 2;
1349 wxListItemData
*item
= node
->GetData();
1353 if ( item
->HasImage() )
1356 m_owner
->DrawImage( item
->GetImage(), dc
, x
, y
);
1357 m_owner
->GetImageSize( item
->GetImage(), ix
, iy
);
1358 x
+= ix
+ 5; // FIXME: what is "5"?
1361 int width
= m_owner
->GetColumnWidth(col
++);
1363 dc
->SetClippingRegion(x
, y
, width
, height
);
1365 if ( item
->HasText() )
1367 dc
->DrawText( item
->GetText(), x
, y
+ 1 );
1370 dc
->DestroyClippingRegion();
1374 node
= node
->GetNext();
1381 wxListItemData
*item
= node
->GetData();
1382 if (item
->HasImage())
1384 wxRect rectIcon
= GetIconRect();
1385 m_owner
->DrawImage( item
->GetImage(), dc
,
1386 rectIcon
.x
, rectIcon
.y
);
1389 if (item
->HasText())
1391 wxRect rectLabel
= GetLabelRect();
1392 dc
->DrawText( item
->GetText(), rectLabel
.x
, rectLabel
.y
);
1398 bool wxListLineData::Hilight( bool on
)
1400 wxCHECK_MSG( !m_owner
->IsVirtual(), FALSE
, _T("unexpected call to Hilight") );
1402 if ( on
== m_hilighted
)
1410 void wxListLineData::ReverseHilight( void )
1412 Hilight(!IsHilighted());
1415 //-----------------------------------------------------------------------------
1416 // wxListHeaderWindow
1417 //-----------------------------------------------------------------------------
1419 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow
,wxWindow
);
1421 BEGIN_EVENT_TABLE(wxListHeaderWindow
,wxWindow
)
1422 EVT_PAINT (wxListHeaderWindow::OnPaint
)
1423 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse
)
1424 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus
)
1427 wxListHeaderWindow::wxListHeaderWindow( void )
1429 m_owner
= (wxListMainWindow
*) NULL
;
1430 m_currentCursor
= (wxCursor
*) NULL
;
1431 m_resizeCursor
= (wxCursor
*) NULL
;
1432 m_isDragging
= FALSE
;
1435 wxListHeaderWindow::wxListHeaderWindow( wxWindow
*win
, wxWindowID id
, wxListMainWindow
*owner
,
1436 const wxPoint
&pos
, const wxSize
&size
,
1437 long style
, const wxString
&name
) :
1438 wxWindow( win
, id
, pos
, size
, style
, name
)
1441 // m_currentCursor = wxSTANDARD_CURSOR;
1442 m_currentCursor
= (wxCursor
*) NULL
;
1443 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
1444 m_isDragging
= FALSE
;
1447 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE
) );
1450 wxListHeaderWindow::~wxListHeaderWindow( void )
1452 delete m_resizeCursor
;
1455 void wxListHeaderWindow::DoDrawRect( wxDC
*dc
, int x
, int y
, int w
, int h
)
1458 GtkStateType state
= GTK_STATE_NORMAL
;
1459 if (!m_parent
->IsEnabled()) state
= GTK_STATE_INSENSITIVE
;
1461 x
= dc
->XLOG2DEV( x
);
1463 gtk_paint_box (m_wxwindow
->style
, GTK_PIZZA(m_wxwindow
)->bin_window
, state
, GTK_SHADOW_OUT
,
1464 (GdkRectangle
*) NULL
, m_wxwindow
, "button", x
-1, y
-1, w
+2, h
+2);
1465 #elif defined( __WXMAC__ )
1466 const int m_corner
= 1;
1468 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1470 dc
->SetPen( wxPen( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNSHADOW
) , 1 , wxSOLID
) );
1471 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1472 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1474 wxPen
pen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID
);
1477 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1478 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1480 dc
->SetPen( *wxWHITE_PEN
);
1481 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1482 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1483 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1484 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1486 const int m_corner
= 1;
1488 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
1490 dc
->SetPen( *wxBLACK_PEN
);
1491 dc
->DrawLine( x
+w
-m_corner
+1, y
, x
+w
, y
+h
); // right (outer)
1492 dc
->DrawRectangle( x
, y
+h
, w
+1, 1 ); // bottom (outer)
1494 wxPen
pen( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNSHADOW
), 1, wxSOLID
);
1497 dc
->DrawLine( x
+w
-m_corner
, y
, x
+w
-1, y
+h
); // right (inner)
1498 dc
->DrawRectangle( x
+1, y
+h
-1, w
-2, 1 ); // bottom (inner)
1500 dc
->SetPen( *wxWHITE_PEN
);
1501 dc
->DrawRectangle( x
, y
, w
-m_corner
+1, 1 ); // top (outer)
1502 dc
->DrawRectangle( x
, y
, 1, h
); // left (outer)
1503 dc
->DrawLine( x
, y
+h
-1, x
+1, y
+h
-1 );
1504 dc
->DrawLine( x
+w
-1, y
, x
+w
-1, y
+1 );
1508 // shift the DC origin to match the position of the main window horz
1509 // scrollbar: this allows us to always use logical coords
1510 void wxListHeaderWindow::AdjustDC(wxDC
& dc
)
1513 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1516 m_owner
->GetViewStart( &x
, NULL
);
1518 // account for the horz scrollbar offset
1519 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1522 void wxListHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1525 wxClientDC
dc( this );
1527 wxPaintDC
dc( this );
1535 dc
.SetFont( GetFont() );
1537 // width and height of the entire header window
1539 GetClientSize( &w
, &h
);
1540 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1542 dc
.SetBackgroundMode(wxTRANSPARENT
);
1544 // do *not* use the listctrl colour for headers - one day we will have a
1545 // function to set it separately
1546 //dc.SetTextForeground( *wxBLACK );
1547 dc
.SetTextForeground(wxSystemSettings::
1548 GetSystemColour( wxSYS_COLOUR_WINDOWTEXT
));
1550 int x
= HEADER_OFFSET_X
;
1552 int numColumns
= m_owner
->GetColumnCount();
1554 for (int i
= 0; i
< numColumns
; i
++)
1556 m_owner
->GetColumn( i
, item
);
1557 int wCol
= item
.m_width
;
1558 int cw
= wCol
- 2; // the width of the rect to draw
1560 int xEnd
= x
+ wCol
;
1562 dc
.SetPen( *wxWHITE_PEN
);
1564 DoDrawRect( &dc
, x
, HEADER_OFFSET_Y
, cw
, h
-2 );
1565 dc
.SetClippingRegion( x
, HEADER_OFFSET_Y
, cw
-5, h
-4 );
1566 dc
.DrawText( item
.GetText(), x
+ EXTRA_WIDTH
, HEADER_OFFSET_Y
+ EXTRA_HEIGHT
);
1567 dc
.DestroyClippingRegion();
1576 void wxListHeaderWindow::DrawCurrent()
1578 int x1
= m_currentX
;
1580 ClientToScreen( &x1
, &y1
);
1582 int x2
= m_currentX
-1;
1584 m_owner
->GetClientSize( NULL
, &y2
);
1585 m_owner
->ClientToScreen( &x2
, &y2
);
1588 dc
.SetLogicalFunction( wxINVERT
);
1589 dc
.SetPen( wxPen( *wxBLACK
, 2, wxSOLID
) );
1590 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1594 dc
.DrawLine( x1
, y1
, x2
, y2
);
1596 dc
.SetLogicalFunction( wxCOPY
);
1598 dc
.SetPen( wxNullPen
);
1599 dc
.SetBrush( wxNullBrush
);
1602 void wxListHeaderWindow::OnMouse( wxMouseEvent
&event
)
1604 // we want to work with logical coords
1606 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1607 int y
= event
.GetY();
1611 // we don't draw the line beyond our window, but we allow dragging it
1614 GetClientSize( &w
, NULL
);
1615 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1618 // erase the line if it was drawn
1619 if ( m_currentX
< w
)
1622 if (event
.ButtonUp())
1625 m_isDragging
= FALSE
;
1627 m_owner
->SetColumnWidth( m_column
, m_currentX
- m_minX
);
1634 m_currentX
= m_minX
+ 7;
1636 // draw in the new location
1637 if ( m_currentX
< w
)
1641 else // not dragging
1644 bool hit_border
= FALSE
;
1646 // end of the current column
1649 // find the column where this event occured
1650 int countCol
= m_owner
->GetColumnCount();
1651 for (int col
= 0; col
< countCol
; col
++)
1653 xpos
+= m_owner
->GetColumnWidth( col
);
1656 if ( (abs(x
-xpos
) < 3) && (y
< 22) )
1658 // near the column border
1665 // inside the column
1672 if (event
.LeftDown())
1676 m_isDragging
= TRUE
;
1683 wxWindow
*parent
= GetParent();
1684 wxListEvent
le( wxEVT_COMMAND_LIST_COL_CLICK
, parent
->GetId() );
1685 le
.SetEventObject( parent
);
1686 le
.m_col
= m_column
;
1687 parent
->GetEventHandler()->ProcessEvent( le
);
1690 else if (event
.Moving())
1695 setCursor
= m_currentCursor
== wxSTANDARD_CURSOR
;
1696 m_currentCursor
= m_resizeCursor
;
1700 setCursor
= m_currentCursor
!= wxSTANDARD_CURSOR
;
1701 m_currentCursor
= wxSTANDARD_CURSOR
;
1705 SetCursor(*m_currentCursor
);
1710 void wxListHeaderWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
1712 m_owner
->SetFocus();
1715 //-----------------------------------------------------------------------------
1716 // wxListRenameTimer (internal)
1717 //-----------------------------------------------------------------------------
1719 wxListRenameTimer::wxListRenameTimer( wxListMainWindow
*owner
)
1724 void wxListRenameTimer::Notify()
1726 m_owner
->OnRenameTimer();
1729 //-----------------------------------------------------------------------------
1730 // wxListTextCtrl (internal)
1731 //-----------------------------------------------------------------------------
1733 IMPLEMENT_DYNAMIC_CLASS(wxListTextCtrl
,wxTextCtrl
);
1735 BEGIN_EVENT_TABLE(wxListTextCtrl
,wxTextCtrl
)
1736 EVT_CHAR (wxListTextCtrl::OnChar
)
1737 EVT_KEY_UP (wxListTextCtrl::OnKeyUp
)
1738 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus
)
1741 wxListTextCtrl::wxListTextCtrl( wxWindow
*parent
,
1742 const wxWindowID id
,
1745 wxListMainWindow
*owner
,
1746 const wxString
&value
,
1750 const wxValidator
& validator
,
1751 const wxString
&name
)
1752 : wxTextCtrl( parent
, id
, value
, pos
, size
, style
, validator
, name
)
1757 (*m_accept
) = FALSE
;
1759 m_startValue
= value
;
1762 void wxListTextCtrl::OnChar( wxKeyEvent
&event
)
1764 if (event
.m_keyCode
== WXK_RETURN
)
1767 (*m_res
) = GetValue();
1769 if (!wxPendingDelete
.Member(this))
1770 wxPendingDelete
.Append(this);
1772 if ((*m_accept
) && ((*m_res
) != m_startValue
))
1773 m_owner
->OnRenameAccept();
1777 if (event
.m_keyCode
== WXK_ESCAPE
)
1779 (*m_accept
) = FALSE
;
1782 if (!wxPendingDelete
.Member(this))
1783 wxPendingDelete
.Append(this);
1791 void wxListTextCtrl::OnKeyUp( wxKeyEvent
&event
)
1793 // auto-grow the textctrl:
1794 wxSize parentSize
= m_owner
->GetSize();
1795 wxPoint myPos
= GetPosition();
1796 wxSize mySize
= GetSize();
1798 GetTextExtent(GetValue() + _T("MM"), &sx
, &sy
); // FIXME: MM??
1799 if (myPos
.x
+ sx
> parentSize
.x
)
1800 sx
= parentSize
.x
- myPos
.x
;
1808 void wxListTextCtrl::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
1810 if (!wxPendingDelete
.Member(this))
1811 wxPendingDelete
.Append(this);
1813 if ((*m_accept
) && ((*m_res
) != m_startValue
))
1814 m_owner
->OnRenameAccept();
1817 //-----------------------------------------------------------------------------
1819 //-----------------------------------------------------------------------------
1821 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow
,wxScrolledWindow
);
1823 BEGIN_EVENT_TABLE(wxListMainWindow
,wxScrolledWindow
)
1824 EVT_PAINT (wxListMainWindow::OnPaint
)
1825 EVT_SIZE (wxListMainWindow::OnSize
)
1826 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse
)
1827 EVT_CHAR (wxListMainWindow::OnChar
)
1828 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown
)
1829 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus
)
1830 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus
)
1831 EVT_SCROLLWIN (wxListMainWindow::OnScroll
)
1834 void wxListMainWindow::Init()
1836 m_columns
.DeleteContents( TRUE
);
1846 m_small_image_list
= (wxImageList
*) NULL
;
1847 m_normal_image_list
= (wxImageList
*) NULL
;
1849 m_small_spacing
= 30;
1850 m_normal_spacing
= 40;
1854 m_isCreated
= FALSE
;
1856 m_lastOnSame
= FALSE
;
1857 m_renameTimer
= new wxListRenameTimer( this );
1858 m_renameAccept
= FALSE
;
1863 m_lineBeforeLastClicked
= (size_t)-1;
1866 void wxListMainWindow::InitScrolling()
1868 if ( HasFlag(wxLC_REPORT
) )
1870 m_xScroll
= SCROLL_UNIT_X
;
1871 m_yScroll
= SCROLL_UNIT_Y
;
1875 m_xScroll
= SCROLL_UNIT_Y
;
1880 wxListMainWindow::wxListMainWindow()
1884 m_hilightBrush
= (wxBrush
*) NULL
;
1890 wxListMainWindow::wxListMainWindow( wxWindow
*parent
,
1895 const wxString
&name
)
1896 : wxScrolledWindow( parent
, id
, pos
, size
,
1897 style
| wxHSCROLL
| wxVSCROLL
, name
)
1901 m_hilightBrush
= new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT
), wxSOLID
);
1906 SetScrollbars( m_xScroll
, m_yScroll
, 0, 0, 0, 0 );
1908 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX
) );
1911 wxListMainWindow::~wxListMainWindow()
1915 delete m_hilightBrush
;
1917 delete m_renameTimer
;
1920 void wxListMainWindow::CacheLineData(size_t line
)
1922 wxListCtrl
*listctrl
= GetListCtrl();
1924 wxListLineData
*ld
= GetFirstLine();
1926 size_t countCol
= GetColumnCount();
1927 for ( size_t col
= 0; col
< countCol
; col
++ )
1929 ld
->SetText(col
, listctrl
->OnGetItemText(line
, col
));
1932 ld
->SetImage(0, listctrl
->OnGetItemImage(line
));
1933 ld
->SetLineIndex(line
);
1936 wxListLineData
*wxListMainWindow::GetFirstLine() const
1938 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
1940 if ( m_lines
.IsEmpty() )
1942 // normal controls are supposed to have something in m_lines
1943 // already if it's not empty
1944 wxASSERT_MSG( IsVirtual(), _T("logic error") );
1946 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1947 wxListLineData
*line
= new wxListLineData( self
, 0 );
1948 self
->m_lines
.Add(line
);
1951 m_lines
[0].SetLineIndex(0);
1956 wxCoord
wxListMainWindow::GetLineHeight() const
1958 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
1960 // we cache the line height as calling GetTextExtent() is slow
1961 if ( !m_lineHeight
)
1963 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
1965 wxClientDC
dc( self
);
1966 dc
.SetFont( GetFont() );
1969 dc
.GetTextExtent(_T("H"), NULL
, &y
);
1971 if ( y
< SCROLL_UNIT_Y
)
1975 self
->m_lineHeight
= y
+ LINE_SPACING
;
1978 return m_lineHeight
;
1981 wxCoord
wxListMainWindow::GetLineY(size_t line
) const
1983 wxASSERT_MSG( HasFlag(wxLC_REPORT
), _T("only works in report mode") );
1985 return LINE_SPACING
+ line
*GetLineHeight();
1988 bool wxListMainWindow::IsHilighted(size_t line
) const
1992 return m_selections
.Index(line
) != wxNOT_FOUND
;
1996 wxListLineData
*ld
= GetLine(line
);
1997 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHilighted") );
1999 return ld
->IsHilighted();
2003 bool wxListMainWindow::HilightLine( size_t line
, bool hilight
)
2011 int index
= m_selections
.Index(line
);
2014 if ( index
== wxNOT_FOUND
)
2016 m_selections
.Add(line
);
2022 if ( index
!= wxNOT_FOUND
)
2024 m_selections
.RemoveAt((size_t)index
);
2031 wxListLineData
*ld
= GetLine(line
);
2032 wxCHECK_MSG( ld
, FALSE
, _T("invalid index in IsHilighted") );
2034 changed
= ld
->Hilight(hilight
);
2039 SendNotify( line
, hilight
? wxEVT_COMMAND_LIST_ITEM_SELECTED
2040 : wxEVT_COMMAND_LIST_ITEM_DESELECTED
);
2046 void wxListMainWindow::RefreshLine( size_t line
)
2048 wxRect rect
= GetLine(line
)->GetRect();
2050 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2051 RefreshRect( rect
);
2054 void wxListMainWindow::RefreshLines( size_t lineFrom
, size_t lineTo
)
2056 // we suppose that they are ordered by caller
2057 wxASSERT_MSG( lineFrom
<= lineTo
, _T("indices in disorder") );
2059 if ( HasFlag(wxLC_REPORT
) )
2061 if ( lineFrom
< m_lineFrom
)
2062 lineFrom
= m_lineFrom
;
2063 if ( lineTo
> m_lineTo
)
2068 rect
.y
= GetLineY(lineFrom
);
2069 rect
.width
= GetClientSize().x
;
2070 rect
.height
= GetLineY(lineTo
) - rect
.y
;
2072 CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2073 RefreshRect( rect
);
2077 // TODO: this should be optimized...
2078 for ( size_t line
= lineFrom
; line
<= lineTo
; line
++ )
2085 void wxListMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2087 // Note: a wxPaintDC must be constructed even if no drawing is
2088 // done (a Windows requirement).
2089 wxPaintDC
dc( this );
2093 // postpone redrawing until the next OnIdle() call to minimize flicker
2099 // empty control. nothing to draw
2106 CalcScrolledPosition( 0, 0, &dev_x
, &dev_y
);
2110 dc
.SetFont( GetFont() );
2112 if ( HasFlag(wxLC_REPORT
) )
2114 int lineSpacing
= GetLineHeight();
2116 for ( size_t line
= m_lineFrom
; line
<= m_lineTo
; line
++ )
2118 GetLine(line
)->Draw( &dc
,
2121 IsHilighted(line
) );
2124 if ( HasFlag(wxLC_HRULES
) )
2127 wxPen
pen(wxWHITE
, 1, wxSOLID
);
2129 wxPen
pen(wxSystemSettings::
2130 GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2132 wxSize clientSize
= GetClientSize();
2134 for ( size_t i
= m_lineFrom
; i
<= m_lineTo
; i
++ )
2137 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2138 dc
.DrawLine(0 - dev_x
, i
*lineSpacing
,
2139 clientSize
.x
- dev_x
, i
*lineSpacing
);
2142 // Draw last horizontal rule
2143 if ( m_lineTo
> m_lineFrom
)
2146 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2147 dc
.DrawLine(0 - dev_x
, m_lineTo
*lineSpacing
,
2148 clientSize
.x
- dev_x
, m_lineTo
*lineSpacing
);
2152 // Draw vertical rules if required
2153 if ( HasFlag(wxLC_VRULES
) && !IsEmpty() )
2156 wxPen
pen(wxWHITE
, 1, wxSOLID
);
2158 wxPen
pen(wxSystemSettings::
2159 GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
2163 wxRect firstItemRect
;
2164 wxRect lastItemRect
;
2165 GetItemRect(0, firstItemRect
);
2166 GetItemRect(GetItemCount() - 1, lastItemRect
);
2167 int x
= firstItemRect
.GetX();
2169 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
2170 for (col
= 0; col
< GetColumnCount(); col
++)
2172 int colWidth
= GetColumnWidth(col
);
2174 dc
.DrawLine(x
- dev_x
, firstItemRect
.GetY() - 1 - dev_y
,
2175 x
- dev_x
, lastItemRect
.GetBottom() + 1 - dev_y
);
2181 size_t count
= GetItemCount();
2182 for ( size_t i
= 0; i
< count
; i
++ )
2184 GetLine(i
)->Draw( &dc
);
2188 if ( HasCurrent() && m_hasFocus
)
2194 // just offset the rect of the first line to position it correctly
2195 wxListLineData
*line
= GetFirstLine();
2196 rect
= line
->GetHighlightRect();
2197 rect
.y
= GetLineY(m_current
);
2201 rect
= GetLine(m_current
)->GetHighlightRect();
2204 // no rect outline, we already have the background color
2206 dc
.SetPen( *wxBLACK_PEN
);
2207 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
2208 dc
.DrawRectangle( rect
);
2215 void wxListMainWindow::HilightAll( bool on
)
2217 bool needsRefresh
= FALSE
;
2219 size_t count
= GetItemCount();
2220 for ( size_t line
= 0; line
< count
; line
++ )
2222 if ( HilightLine( line
, on
) )
2224 if ( HasFlag(wxLC_REPORT
) )
2226 needsRefresh
= TRUE
;
2237 RefreshLines( 0, count
- 1 );
2241 void wxListMainWindow::SendNotify( size_t line
,
2242 wxEventType command
,
2245 wxListEvent
le( command
, GetParent()->GetId() );
2246 le
.SetEventObject( GetParent() );
2247 le
.m_itemIndex
= line
;
2249 // set only for events which have position
2250 if ( point
!= wxDefaultPosition
)
2251 le
.m_pointDrag
= point
;
2253 GetLine(line
)->GetItem( 0, le
.m_item
);
2254 GetParent()->GetEventHandler()->ProcessEvent( le
);
2257 void wxListMainWindow::OnFocusLine( size_t WXUNUSED(line
) )
2259 // SendNotify( line, wxEVT_COMMAND_LIST_ITEM_FOCUSSED );
2262 void wxListMainWindow::OnUnfocusLine( size_t WXUNUSED(line
) )
2264 // SendNotify( line, wxEVT_COMMAND_LIST_ITEM_UNFOCUSSED );
2267 void wxListMainWindow::EditLabel( long item
)
2269 wxCHECK_RET( (item
>= 0) && ((size_t)item
< GetItemCount()),
2270 wxT("wrong index in wxListCtrl::EditLabel()") );
2272 m_currentEdit
= (size_t)item
;
2274 wxListEvent
le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
, GetParent()->GetId() );
2275 le
.SetEventObject( GetParent() );
2276 le
.m_itemIndex
= item
;
2277 wxListLineData
*data
= GetLine(m_currentEdit
);
2278 wxCHECK_RET( data
, _T("invalid index in EditLabel()") );
2279 data
->GetItem( 0, le
.m_item
);
2280 GetParent()->GetEventHandler()->ProcessEvent( le
);
2282 if (!le
.IsAllowed())
2285 // We have to call this here because the label in question might just have
2286 // been added and no screen update taken place.
2290 wxClientDC
dc(this);
2293 wxString s
= data
->GetText(0);
2294 wxRect rectLabel
= data
->GetLabelRect();
2296 rectLabel
.x
= dc
.LogicalToDeviceX( rectLabel
.x
);
2297 rectLabel
.y
= dc
.LogicalToDeviceY( rectLabel
.y
);
2299 wxListTextCtrl
*text
= new wxListTextCtrl
2306 wxPoint(rectLabel
.x
-4,rectLabel
.y
-4),
2307 wxSize(rectLabel
.width
+11,rectLabel
.height
+8)
2312 void wxListMainWindow::OnRenameTimer()
2314 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
2316 EditLabel( m_current
);
2319 void wxListMainWindow::OnRenameAccept()
2321 wxListEvent
le( wxEVT_COMMAND_LIST_END_LABEL_EDIT
, GetParent()->GetId() );
2322 le
.SetEventObject( GetParent() );
2323 le
.m_itemIndex
= m_currentEdit
;
2325 wxListLineData
*data
= GetLine(m_currentEdit
);
2326 wxCHECK_RET( data
, _T("invalid index in OnRenameAccept()") );
2328 data
->GetItem( 0, le
.m_item
);
2329 le
.m_item
.m_text
= m_renameRes
;
2330 GetParent()->GetEventHandler()->ProcessEvent( le
);
2332 if (!le
.IsAllowed()) return;
2335 info
.m_mask
= wxLIST_MASK_TEXT
;
2336 info
.m_itemId
= le
.m_itemIndex
;
2337 info
.m_text
= m_renameRes
;
2338 info
.SetTextColour(le
.m_item
.GetTextColour());
2342 void wxListMainWindow::OnMouse( wxMouseEvent
&event
)
2344 event
.SetEventObject( GetParent() );
2345 if ( GetParent()->GetEventHandler()->ProcessEvent( event
) )
2348 if ( !HasCurrent() || IsEmpty() )
2354 if ( !(event
.Dragging() || event
.ButtonDown() || event
.LeftUp() ||
2355 event
.ButtonDClick()) )
2358 int x
= event
.GetX();
2359 int y
= event
.GetY();
2360 CalcUnscrolledPosition( x
, y
, &x
, &y
);
2362 /* Did we actually hit an item ? */
2365 size_t count
= GetItemCount(),
2368 if ( HasFlag(wxLC_REPORT
) )
2370 wxCoord lineHeight
= GetLineHeight();
2372 current
= y
/ lineHeight
;
2373 hitResult
= GetFirstLine()->IsHit( x
, y
% lineHeight
);
2377 // TODO: optimize it too! this is less simple than for report view but
2378 // enumerating all items is still not a way to do it!!
2379 for ( current
= 0; current
< count
; current
++ )
2381 wxListLineData
*line
= (wxListLineData
*) NULL
;
2382 line
= GetLine(current
);
2383 hitResult
= line
->IsHit( x
, y
);
2389 if (event
.Dragging())
2391 if (m_dragCount
== 0)
2392 m_dragStart
= wxPoint(x
,y
);
2396 if (m_dragCount
!= 3)
2399 int command
= event
.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2400 : wxEVT_COMMAND_LIST_BEGIN_DRAG
;
2402 wxListEvent
le( command
, GetParent()->GetId() );
2403 le
.SetEventObject( GetParent() );
2404 le
.m_pointDrag
= m_dragStart
;
2405 GetParent()->GetEventHandler()->ProcessEvent( le
);
2416 // outside of any item
2420 bool forceClick
= FALSE
;
2421 if (event
.ButtonDClick())
2423 m_renameTimer
->Stop();
2424 m_lastOnSame
= FALSE
;
2426 if ( current
== m_lineBeforeLastClicked
)
2428 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_ACTIVATED
);
2434 // the first click was on another item, so don't interpret this as
2435 // a double click, but as a simple click instead
2440 if (event
.LeftUp() && m_lastOnSame
)
2442 if ((current
== m_current
) &&
2443 (hitResult
== wxLIST_HITTEST_ONITEMLABEL
) &&
2444 HasFlag(wxLC_EDIT_LABELS
) )
2446 m_renameTimer
->Start( 100, TRUE
);
2448 m_lastOnSame
= FALSE
;
2450 else if (event
.RightDown())
2452 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
,
2453 event
.GetPosition() );
2455 else if (event
.MiddleDown())
2457 SendNotify( current
, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
);
2459 else if ( event
.LeftDown() || forceClick
)
2461 m_lineBeforeLastClicked
= m_lineLastClicked
;
2462 m_lineLastClicked
= current
;
2464 size_t oldCurrent
= m_current
;
2466 if ( HasFlag(wxLC_SINGLE_SEL
) ||
2467 !(event
.ControlDown() || event
.ShiftDown()) )
2469 m_current
= current
;
2470 HilightAll( FALSE
);
2472 ReverseHilight(m_current
);
2476 if (event
.ControlDown())
2478 m_current
= current
;
2480 ReverseHilight(m_current
);
2482 else if (event
.ShiftDown())
2484 m_current
= current
;
2486 size_t lineFrom
= oldCurrent
,
2489 if ( lineTo
< lineFrom
)
2492 lineFrom
= m_current
;
2495 bool needsRefresh
= FALSE
;
2496 for ( size_t i
= lineFrom
; i
<= lineTo
; i
++ )
2498 if ( HilightLine(i
, TRUE
) )
2499 needsRefresh
= TRUE
;
2504 RefreshLines(lineFrom
, lineTo
);
2507 else // !ctrl, !shift
2509 // test in the enclosing if should make it impossible
2510 wxFAIL_MSG( _T("how did we get here?") );
2514 if (m_current
!= oldCurrent
)
2516 RefreshLine( oldCurrent
);
2517 OnUnfocusLine( oldCurrent
);
2518 OnFocusLine( m_current
);
2521 // forceClick is only set if the previous click was on another item
2522 m_lastOnSame
= !forceClick
&& (m_current
== oldCurrent
);
2526 void wxListMainWindow::MoveToFocus()
2531 wxListLineData
*data
= IsVirtual() ? GetFirstLine() : GetLine(m_current
);
2532 wxRect rect
= data
->GetRect();
2534 int client_w
, client_h
;
2535 GetClientSize( &client_w
, &client_h
);
2537 int view_x
= m_xScroll
*GetScrollPos( wxHORIZONTAL
);
2538 int view_y
= m_yScroll
*GetScrollPos( wxVERTICAL
);
2540 if ( HasFlag(wxLC_REPORT
) )
2542 if (rect
.y
< view_y
)
2543 Scroll( -1, rect
.y
/m_yScroll
);
2544 if (rect
.y
+rect
.height
+5 > view_y
+client_h
)
2545 Scroll( -1, (rect
.y
+rect
.height
-client_h
+SCROLL_UNIT_Y
)/m_yScroll
);
2547 UpdateShownLinesRange();
2551 if (rect
.x
-view_x
< 5)
2552 Scroll( (rect
.x
-5)/m_xScroll
, -1 );
2553 if (rect
.x
+rect
.width
-5 > view_x
+client_w
)
2554 Scroll( (rect
.x
+rect
.width
-client_w
+SCROLL_UNIT_X
)/m_xScroll
, -1 );
2558 // ----------------------------------------------------------------------------
2559 // keyboard handling
2560 // ----------------------------------------------------------------------------
2562 void wxListMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
2564 wxCHECK_RET( newCurrent
< (size_t)GetItemCount(),
2565 _T("invalid item index in OnArrowChar()") );
2567 size_t oldCurrent
= m_current
;
2569 // in single selection we just ignore Shift as we can't select several
2571 if ( event
.ShiftDown() && !HasFlag(wxLC_SINGLE_SEL
) )
2573 m_current
= newCurrent
;
2575 // select all the items between the old and the new one
2576 if ( oldCurrent
> newCurrent
)
2578 newCurrent
= oldCurrent
;
2579 oldCurrent
= m_current
;
2582 bool needsRefresh
= FALSE
;
2583 for ( size_t line
= oldCurrent
; line
<= newCurrent
; line
++ )
2585 if ( HilightLine( line
) )
2587 needsRefresh
= TRUE
;
2592 RefreshLines( oldCurrent
, newCurrent
);
2596 m_current
= newCurrent
;
2598 HilightLine( oldCurrent
, FALSE
);
2599 RefreshLine( oldCurrent
);
2601 if ( !event
.ControlDown() )
2603 HilightLine( m_current
, TRUE
);
2607 OnUnfocusLine( oldCurrent
);
2608 OnFocusLine( m_current
);
2609 RefreshLine( m_current
);
2614 void wxListMainWindow::OnKeyDown( wxKeyEvent
&event
)
2616 wxWindow
*parent
= GetParent();
2618 /* we propagate the key event up */
2619 wxKeyEvent
ke( wxEVT_KEY_DOWN
);
2620 ke
.m_shiftDown
= event
.m_shiftDown
;
2621 ke
.m_controlDown
= event
.m_controlDown
;
2622 ke
.m_altDown
= event
.m_altDown
;
2623 ke
.m_metaDown
= event
.m_metaDown
;
2624 ke
.m_keyCode
= event
.m_keyCode
;
2627 ke
.SetEventObject( parent
);
2628 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
2633 void wxListMainWindow::OnChar( wxKeyEvent
&event
)
2635 wxWindow
*parent
= GetParent();
2637 /* we send a list_key event up */
2640 wxListEvent
le( wxEVT_COMMAND_LIST_KEY_DOWN
, GetParent()->GetId() );
2641 le
.m_itemIndex
= m_current
;
2642 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2643 le
.m_code
= (int)event
.KeyCode();
2644 le
.SetEventObject( parent
);
2645 parent
->GetEventHandler()->ProcessEvent( le
);
2648 /* we propagate the char event up */
2649 wxKeyEvent
ke( wxEVT_CHAR
);
2650 ke
.m_shiftDown
= event
.m_shiftDown
;
2651 ke
.m_controlDown
= event
.m_controlDown
;
2652 ke
.m_altDown
= event
.m_altDown
;
2653 ke
.m_metaDown
= event
.m_metaDown
;
2654 ke
.m_keyCode
= event
.m_keyCode
;
2657 ke
.SetEventObject( parent
);
2658 if (parent
->GetEventHandler()->ProcessEvent( ke
)) return;
2660 if (event
.KeyCode() == WXK_TAB
)
2662 wxNavigationKeyEvent nevent
;
2663 nevent
.SetWindowChange( event
.ControlDown() );
2664 nevent
.SetDirection( !event
.ShiftDown() );
2665 nevent
.SetEventObject( GetParent()->GetParent() );
2666 nevent
.SetCurrentFocus( m_parent
);
2667 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
)) return;
2670 /* no item -> nothing to do */
2677 switch (event
.KeyCode())
2680 if ( m_current
> 0 )
2681 OnArrowChar( m_current
- 1, event
);
2685 if ( m_current
< (size_t)GetItemCount() - 1 )
2686 OnArrowChar( m_current
+ 1, event
);
2691 OnArrowChar( GetItemCount() - 1, event
);
2696 OnArrowChar( 0, event
);
2702 if ( HasFlag(wxLC_REPORT
) )
2704 steps
= m_linesPerPage
- 1;
2708 steps
= m_current
% m_linesPerPage
;
2711 int index
= m_current
- steps
;
2715 OnArrowChar( index
, event
);
2722 if ( HasFlag(wxLC_REPORT
) )
2724 steps
= m_linesPerPage
- 1;
2728 steps
= m_linesPerPage
- (m_current
% m_linesPerPage
) - 1;
2731 size_t index
= m_current
+ steps
;
2732 size_t count
= GetItemCount();
2733 if ( index
>= count
)
2736 OnArrowChar( index
, event
);
2741 if ( !HasFlag(wxLC_REPORT
) )
2743 int index
= m_current
- m_linesPerPage
;
2747 OnArrowChar( index
, event
);
2752 if ( !HasFlag(wxLC_REPORT
) )
2754 size_t index
= m_current
+ m_linesPerPage
;
2756 size_t count
= GetItemCount();
2757 if ( index
>= count
)
2760 OnArrowChar( index
, event
);
2765 if ( HasFlag(wxLC_SINGLE_SEL
) )
2767 wxListEvent
le( wxEVT_COMMAND_LIST_ITEM_ACTIVATED
,
2768 GetParent()->GetId() );
2769 le
.SetEventObject( GetParent() );
2770 le
.m_itemIndex
= m_current
;
2771 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2772 GetParent()->GetEventHandler()->ProcessEvent( le
);
2776 ReverseHilight(m_current
);
2783 wxListEvent
le( wxEVT_COMMAND_LIST_ITEM_ACTIVATED
,
2784 GetParent()->GetId() );
2785 le
.SetEventObject( GetParent() );
2786 le
.m_itemIndex
= m_current
;
2787 GetLine(m_current
)->GetItem( 0, le
.m_item
);
2788 GetParent()->GetEventHandler()->ProcessEvent( le
);
2797 // ----------------------------------------------------------------------------
2799 // ----------------------------------------------------------------------------
2802 extern wxWindow
*g_focusWindow
;
2805 void wxListMainWindow::OnSetFocus( wxFocusEvent
&WXUNUSED(event
) )
2810 RefreshLine( m_current
);
2816 g_focusWindow
= GetParent();
2819 wxFocusEvent
event( wxEVT_SET_FOCUS
, GetParent()->GetId() );
2820 event
.SetEventObject( GetParent() );
2821 GetParent()->GetEventHandler()->ProcessEvent( event
);
2824 void wxListMainWindow::OnKillFocus( wxFocusEvent
&WXUNUSED(event
) )
2829 RefreshLine( m_current
);
2832 void wxListMainWindow::DrawImage( int index
, wxDC
*dc
, int x
, int y
)
2834 if ( HasFlag(wxLC_ICON
) && (m_normal_image_list
))
2836 m_normal_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2838 else if ( HasFlag(wxLC_SMALL_ICON
) && (m_small_image_list
))
2840 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2842 else if ( HasFlag(wxLC_LIST
) && (m_small_image_list
))
2844 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2846 else if ( HasFlag(wxLC_REPORT
) && (m_small_image_list
))
2848 m_small_image_list
->Draw( index
, *dc
, x
, y
, wxIMAGELIST_DRAW_TRANSPARENT
);
2852 void wxListMainWindow::GetImageSize( int index
, int &width
, int &height
)
2854 if ( HasFlag(wxLC_ICON
) && m_normal_image_list
)
2856 m_normal_image_list
->GetSize( index
, width
, height
);
2858 else if ( HasFlag(wxLC_SMALL_ICON
) && m_small_image_list
)
2860 m_small_image_list
->GetSize( index
, width
, height
);
2862 else if ( HasFlag(wxLC_LIST
) && m_small_image_list
)
2864 m_small_image_list
->GetSize( index
, width
, height
);
2866 else if ( HasFlag(wxLC_REPORT
) && m_small_image_list
)
2868 m_small_image_list
->GetSize( index
, width
, height
);
2877 int wxListMainWindow::GetTextLength( const wxString
&s
)
2879 wxClientDC
dc( this );
2880 dc
.SetFont( GetFont() );
2883 dc
.GetTextExtent( s
, &lw
, NULL
);
2885 return lw
+ AUTOSIZE_COL_MARGIN
;
2888 void wxListMainWindow::SetImageList( wxImageList
*imageList
, int which
)
2892 // calc the spacing from the icon size
2895 if ((imageList
) && (imageList
->GetImageCount()) )
2897 imageList
->GetSize(0, width
, height
);
2900 if (which
== wxIMAGE_LIST_NORMAL
)
2902 m_normal_image_list
= imageList
;
2903 m_normal_spacing
= width
+ 8;
2906 if (which
== wxIMAGE_LIST_SMALL
)
2908 m_small_image_list
= imageList
;
2909 m_small_spacing
= width
+ 14;
2913 void wxListMainWindow::SetItemSpacing( int spacing
, bool isSmall
)
2918 m_small_spacing
= spacing
;
2922 m_normal_spacing
= spacing
;
2926 int wxListMainWindow::GetItemSpacing( bool isSmall
)
2928 return isSmall
? m_small_spacing
: m_normal_spacing
;
2931 // ----------------------------------------------------------------------------
2933 // ----------------------------------------------------------------------------
2935 void wxListMainWindow::SetColumn( int col
, wxListItem
&item
)
2937 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
2939 wxCHECK_RET( node
, _T("invalid column index in SetColumn") );
2941 if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
2942 item
.m_width
= GetTextLength( item
.m_text
);
2944 wxListHeaderData
*column
= node
->GetData();
2945 column
->SetItem( item
);
2947 wxListHeaderWindow
*headerWin
= GetListCtrl()->m_headerWin
;
2949 headerWin
->m_dirty
= TRUE
;
2953 // invalidate it as it has to be recalculated
2957 void wxListMainWindow::SetColumnWidth( int col
, int width
)
2959 wxCHECK_RET( col
>= 0 && col
< GetColumnCount(),
2960 _T("invalid column index") );
2962 wxCHECK_RET( HasFlag(wxLC_REPORT
),
2963 _T("SetColumnWidth() can only be called in report mode.") );
2967 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
2968 wxCHECK_RET( node
, _T("no column?") );
2970 wxListHeaderData
*column
= node
->GetData();
2972 size_t count
= GetItemCount();
2974 if (width
== wxLIST_AUTOSIZE_USEHEADER
)
2976 width
= GetTextLength(column
->GetText());
2978 else if ( width
== wxLIST_AUTOSIZE
)
2982 // TODO: determine the max width somehow...
2983 width
= WIDTH_COL_DEFAULT
;
2987 wxClientDC
dc(this);
2988 dc
.SetFont( GetFont() );
2990 int max
= AUTOSIZE_COL_MARGIN
;
2992 for ( size_t i
= 0; i
< count
; i
++ )
2994 wxListLineData
*line
= GetLine(i
);
2995 wxListItemDataList::Node
*n
= line
->m_items
.Item( col
);
2997 wxCHECK_RET( n
, _T("no subitem?") );
2999 wxListItemData
*item
= n
->GetData();
3002 if (item
->HasImage())
3005 GetImageSize( item
->GetImage(), ix
, iy
);
3009 if (item
->HasText())
3012 dc
.GetTextExtent( item
->GetText(), &w
, NULL
);
3020 width
= max
+ AUTOSIZE_COL_MARGIN
;
3024 column
->SetWidth( width
);
3026 // invalidate it as it has to be recalculated
3030 int wxListMainWindow::GetHeaderWidth() const
3032 if ( !m_headerWidth
)
3034 wxListMainWindow
*self
= wxConstCast(this, wxListMainWindow
);
3036 size_t count
= GetColumnCount();
3037 for ( size_t col
= 0; col
< count
; col
++ )
3039 self
->m_headerWidth
+= GetColumnWidth(col
);
3043 return m_headerWidth
;
3046 void wxListMainWindow::GetColumn( int col
, wxListItem
&item
) const
3048 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3049 wxCHECK_RET( node
, _T("invalid column index in GetColumn") );
3051 wxListHeaderData
*column
= node
->GetData();
3052 column
->GetItem( item
);
3055 int wxListMainWindow::GetColumnWidth( int col
) const
3057 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3058 wxCHECK_MSG( node
, 0, _T("invalid column index") );
3060 wxListHeaderData
*column
= node
->GetData();
3061 return column
->GetWidth();
3064 // ----------------------------------------------------------------------------
3066 // ----------------------------------------------------------------------------
3068 void wxListMainWindow::SetItem( wxListItem
&item
)
3070 long id
= item
.m_itemId
;
3071 wxCHECK_RET( id
>= 0 && (size_t)id
< GetItemCount(),
3072 _T("invalid item index in SetItem") );
3076 // just refresh the line to show the new value of the text/image
3077 RefreshLine((size_t)id
);
3083 wxListLineData
*line
= GetLine((size_t)id
);
3084 if ( HasFlag(wxLC_REPORT
) )
3085 item
.m_width
= GetColumnWidth( item
.m_col
);
3086 line
->SetItem( item
.m_col
, item
);
3090 void wxListMainWindow::SetItemState( long litem
, long state
, long stateMask
)
3092 wxCHECK_RET( litem
>= 0 && (size_t)litem
< GetItemCount(),
3093 _T("invalid list ctrl item index in SetItem") );
3095 size_t oldCurrent
= m_current
;
3096 size_t item
= (size_t)litem
; // sdafe because of the check above
3098 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3100 if ( state
& wxLIST_STATE_FOCUSED
)
3102 // don't do anything if this item is already focused
3103 if ( item
!= m_current
)
3105 OnUnfocusLine( m_current
);
3107 OnFocusLine( m_current
);
3109 if ( HasFlag(wxLC_SINGLE_SEL
) && (oldCurrent
!= (size_t)-1) )
3111 HilightLine(oldCurrent
, FALSE
);
3112 RefreshLine(oldCurrent
);
3115 RefreshLine( m_current
);
3120 // don't do anything if this item is not focused
3121 if ( item
== m_current
)
3123 OnUnfocusLine( m_current
);
3124 m_current
= (size_t)-1;
3129 if ( stateMask
& wxLIST_STATE_SELECTED
)
3131 bool on
= (state
& wxLIST_STATE_SELECTED
) != 0;
3133 if ( HasFlag(wxLC_SINGLE_SEL
) )
3137 // selecting the item also makes it the focused one in the
3139 if ( m_current
!= item
)
3141 OnUnfocusLine( m_current
);
3143 OnFocusLine( m_current
);
3145 if ( oldCurrent
!= (size_t)-1 )
3147 HilightLine( oldCurrent
, FALSE
);
3148 RefreshLine( oldCurrent
);
3154 // only the current item may be selected anyhow
3155 if ( item
!= m_current
)
3160 if ( HilightLine(item
, on
) )
3167 int wxListMainWindow::GetItemState( long item
, long stateMask
)
3169 wxCHECK_MSG( item
>= 0 && (size_t)item
< GetItemCount(), 0,
3170 _T("invalid list ctrl item index in GetItemState()") );
3172 int ret
= wxLIST_STATE_DONTCARE
;
3174 if ( stateMask
& wxLIST_STATE_FOCUSED
)
3176 if ( (size_t)item
== m_current
)
3177 ret
|= wxLIST_STATE_FOCUSED
;
3180 if ( stateMask
& wxLIST_STATE_SELECTED
)
3182 if ( IsHilighted(item
) )
3183 ret
|= wxLIST_STATE_SELECTED
;
3189 void wxListMainWindow::GetItem( wxListItem
&item
)
3191 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
< GetItemCount(),
3192 _T("invalid item index in GetItem") );
3194 wxListLineData
*line
= GetLine((size_t)item
.m_itemId
);
3195 line
->GetItem( item
.m_col
, item
);
3198 // ----------------------------------------------------------------------------
3200 // ----------------------------------------------------------------------------
3202 size_t wxListMainWindow::GetItemCount() const
3204 return IsVirtual() ? m_countVirt
: m_lines
.GetCount();
3207 void wxListMainWindow::SetItemCount(long count
)
3209 m_countVirt
= count
;
3214 int wxListMainWindow::GetSelectedItemCount()
3216 // deal with the quick case first
3217 if ( HasFlag(wxLC_SINGLE_SEL
) )
3219 return m_current
== (size_t)-1 ? FALSE
: IsHilighted(m_current
);
3222 // virtual controls remmebers all its selections itself
3224 return m_selections
.GetCount();
3226 // TODO: we probably should maintain the number of items selected even for
3227 // non virtual controls as enumerating all lines is really slow...
3228 size_t countSel
= 0;
3229 size_t count
= GetItemCount();
3230 for ( size_t line
= 0; line
< count
; line
++ )
3232 if ( GetLine(line
)->IsHilighted() )
3239 // ----------------------------------------------------------------------------
3240 // item position/size
3241 // ----------------------------------------------------------------------------
3243 void wxListMainWindow::GetItemRect( long index
, wxRect
&rect
)
3245 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3246 _T("invalid index in GetItemRect") );
3248 rect
= GetLine((size_t)index
)->GetRect();
3249 CalcScrolledPosition(rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
3252 bool wxListMainWindow::GetItemPosition(long item
, wxPoint
& pos
)
3255 GetItemRect(item
, rect
);
3263 // ----------------------------------------------------------------------------
3264 // geometry calculation
3265 // ----------------------------------------------------------------------------
3267 void wxListMainWindow::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3269 // wait for the next OnIdle() with geometry recalculation
3273 void wxListMainWindow::CalculatePositions()
3278 wxClientDC
dc( this );
3279 dc
.SetFont( GetFont() );
3282 if ( HasFlag(wxLC_ICON
) )
3283 iconSpacing
= m_normal_spacing
;
3284 else if ( HasFlag(wxLC_SMALL_ICON
) )
3285 iconSpacing
= m_small_spacing
;
3291 GetClientSize( &clientWidth
, &clientHeight
);
3293 if ( HasFlag(wxLC_REPORT
) )
3295 // all lines have the same height
3296 int lineSpacing
= GetLineHeight();
3298 // scroll one line per step
3299 m_yScroll
= lineSpacing
;
3301 size_t lineCount
= GetItemCount();
3302 int entireHeight
= lineCount
*lineSpacing
+ 2*LINE_SPACING
;
3304 m_linesPerPage
= clientHeight
/ lineSpacing
;
3306 SetScrollbars( m_xScroll
, m_yScroll
,
3307 (GetHeaderWidth() + m_xScroll
- 1)/m_xScroll
,
3308 (entireHeight
+ m_yScroll
- 1)/m_yScroll
,
3309 GetScrollPos(wxHORIZONTAL
),
3310 GetScrollPos(wxVERTICAL
),
3313 UpdateShownLinesRange();
3317 // at first we try without any scrollbar. if the items don't
3318 // fit into the window, we recalculate after subtracting an
3319 // approximated 15 pt for the horizontal scrollbar
3321 clientHeight
-= 4; // sunken frame
3325 for (int tries
= 0; tries
< 2; tries
++)
3332 int currentlyVisibleLines
= 0;
3334 size_t count
= GetItemCount();
3335 for (size_t i
= 0; i
< count
; i
++)
3337 currentlyVisibleLines
++;
3338 wxListLineData
*line
= GetLine(i
);
3339 line
->CalculateSize( &dc
, iconSpacing
);
3340 line
->SetPosition( x
, y
, clientWidth
, iconSpacing
);
3342 wxSize sizeLine
= line
->GetSize();
3344 if ( maxWidth
< sizeLine
.x
)
3345 maxWidth
= sizeLine
.x
;
3348 if (currentlyVisibleLines
> m_linesPerPage
)
3349 m_linesPerPage
= currentlyVisibleLines
;
3351 // assume that the size of the next one is the same... (FIXME)
3352 if ( y
+ sizeLine
.y
- 6 >= clientHeight
)
3354 currentlyVisibleLines
= 0;
3357 entireWidth
+= maxWidth
+6;
3360 if ( i
== count
- 1 )
3361 entireWidth
+= maxWidth
;
3362 if ((tries
== 0) && (entireWidth
> clientWidth
))
3364 clientHeight
-= 15; // scrollbar height
3366 currentlyVisibleLines
= 0;
3369 if ( i
== count
- 1 )
3370 tries
= 1; // everything fits, no second try required
3374 int scroll_pos
= GetScrollPos( wxHORIZONTAL
);
3375 SetScrollbars( m_xScroll
, m_yScroll
, (entireWidth
+SCROLL_UNIT_X
) / m_xScroll
, 0, scroll_pos
, 0, TRUE
);
3378 // FIXME: why should we call it from here?
3382 void wxListMainWindow::UpdateCurrent()
3384 if ( (m_current
== (size_t)-1) && !IsEmpty() )
3389 if ( m_current
!= (size_t)-1 )
3391 OnFocusLine( m_current
);
3395 long wxListMainWindow::GetNextItem( long item
,
3396 int WXUNUSED(geometry
),
3400 max
= GetItemCount();
3401 wxCHECK_MSG( (ret
== -1) || (ret
< max
), -1,
3402 _T("invalid listctrl index in GetNextItem()") );
3404 // notice that we start with the next item (or the first one if item == -1)
3405 // and this is intentional to allow writing a simple loop to iterate over
3406 // all selected items
3410 // this is not an error because the index was ok initially, just no
3421 size_t count
= GetItemCount();
3422 for ( size_t line
= (size_t)ret
; line
< count
; line
++ )
3424 if ( (state
& wxLIST_STATE_FOCUSED
) && (line
== m_current
) )
3427 if ( (state
& wxLIST_STATE_SELECTED
) && IsHilighted(line
) )
3434 // ----------------------------------------------------------------------------
3436 // ----------------------------------------------------------------------------
3438 void wxListMainWindow::DeleteItem( long index
)
3440 size_t count
= GetItemCount();
3442 wxCHECK_RET( (index
>= 0) && ((size_t)index
< count
),
3443 _T("invalid item index in DeleteItem") );
3447 // select the next item when the selected one is deleted
3448 if ( m_current
== (size_t)index
)
3450 // the last valid index after deleting the item will be count-2
3451 if ( ++m_current
>= count
- 2 )
3453 m_current
= count
- 2;
3457 SendNotify( (size_t)index
, wxEVT_COMMAND_LIST_DELETE_ITEM
);
3461 m_lines
.RemoveAt( (size_t)index
);
3465 void wxListMainWindow::DeleteColumn( int col
)
3467 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3469 wxCHECK_RET( node
, wxT("invalid column index in DeleteColumn()") );
3472 m_columns
.DeleteNode( node
);
3475 void wxListMainWindow::DeleteAllItems()
3479 // nothing to do - in particular, don't send the event
3484 m_current
= (size_t)-1;
3486 // to make the deletion of all items faster, we don't send the
3487 // notifications for each item deletion in this case but only one event
3488 // for all of them: this is compatible with wxMSW and documented in
3489 // DeleteAllItems() description
3491 wxListEvent
event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
, GetParent()->GetId() );
3492 event
.SetEventObject( GetParent() );
3493 GetParent()->GetEventHandler()->ProcessEvent( event
);
3497 m_selections
.Clear();
3500 void wxListMainWindow::DeleteEverything()
3507 // ----------------------------------------------------------------------------
3508 // scanning for an item
3509 // ----------------------------------------------------------------------------
3511 void wxListMainWindow::EnsureVisible( long index
)
3513 wxCHECK_RET( index
>= 0 && (size_t)index
< GetItemCount(),
3514 _T("invalid index in EnsureVisible") );
3516 // We have to call this here because the label in question might just have
3517 // been added and no screen update taken place.
3521 size_t oldCurrent
= m_current
;
3522 m_current
= (size_t)index
;
3524 m_current
= oldCurrent
;
3527 long wxListMainWindow::FindItem(long start
, const wxString
& str
, bool WXUNUSED(partial
) )
3534 size_t count
= GetItemCount();
3535 for ( size_t i
= (size_t)pos
; i
< count
; i
++ )
3537 wxListLineData
*line
= GetLine(i
);
3538 if ( line
->GetText(0) == tmp
)
3545 long wxListMainWindow::FindItem(long start
, long data
)
3551 size_t count
= GetItemCount();
3552 for (size_t i
= (size_t)pos
; i
< count
; i
++)
3554 wxListLineData
*line
= GetLine(i
);
3556 line
->GetItem( 0, item
);
3557 if (item
.m_data
== data
)
3564 long wxListMainWindow::HitTest( int x
, int y
, int &flags
)
3566 CalcUnscrolledPosition( x
, y
, &x
, &y
);
3568 size_t count
= GetItemCount();
3569 for (size_t i
= 0; i
< count
; i
++)
3571 wxListLineData
*line
= GetLine(i
);
3572 long ret
= line
->IsHit( x
, y
);
3583 // ----------------------------------------------------------------------------
3585 // ----------------------------------------------------------------------------
3587 void wxListMainWindow::InsertItem( wxListItem
&item
)
3589 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
3591 size_t count
= GetItemCount();
3592 wxCHECK_RET( item
.m_itemId
>= 0 && (size_t)item
.m_itemId
<= count
,
3593 _T("invalid item index") );
3595 size_t id
= item
.m_itemId
;
3600 if ( HasFlag(wxLC_REPORT
) )
3602 else if ( HasFlag(wxLC_LIST
) )
3604 else if ( HasFlag(wxLC_ICON
) )
3606 else if ( HasFlag(wxLC_SMALL_ICON
) )
3607 mode
= wxLC_ICON
; // no typo
3610 wxFAIL_MSG( _T("unknown mode") );
3613 wxListLineData
*line
= new wxListLineData( this, id
);
3615 line
->SetItem( 0, item
);
3617 m_lines
.Insert( line
, id
);
3620 void wxListMainWindow::InsertColumn( long col
, wxListItem
&item
)
3623 if ( HasFlag(wxLC_REPORT
) )
3625 if (item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
3626 item
.m_width
= GetTextLength( item
.m_text
);
3627 wxListHeaderData
*column
= new wxListHeaderData( item
);
3628 if ((col
>= 0) && (col
< (int)m_columns
.GetCount()))
3630 wxListHeaderDataList::Node
*node
= m_columns
.Item( col
);
3631 m_columns
.Insert( node
, column
);
3635 m_columns
.Append( column
);
3640 // ----------------------------------------------------------------------------
3642 // ----------------------------------------------------------------------------
3644 wxListCtrlCompare list_ctrl_compare_func_2
;
3645 long list_ctrl_compare_data
;
3647 int LINKAGEMODE
list_ctrl_compare_func_1( wxListLineData
**arg1
, wxListLineData
**arg2
)
3649 wxListLineData
*line1
= *arg1
;
3650 wxListLineData
*line2
= *arg2
;
3652 line1
->GetItem( 0, item
);
3653 long data1
= item
.m_data
;
3654 line2
->GetItem( 0, item
);
3655 long data2
= item
.m_data
;
3656 return list_ctrl_compare_func_2( data1
, data2
, list_ctrl_compare_data
);
3659 void wxListMainWindow::SortItems( wxListCtrlCompare fn
, long data
)
3661 list_ctrl_compare_func_2
= fn
;
3662 list_ctrl_compare_data
= data
;
3663 m_lines
.Sort( list_ctrl_compare_func_1
);
3667 // ----------------------------------------------------------------------------
3669 // ----------------------------------------------------------------------------
3671 void wxListMainWindow::OnScroll(wxScrollWinEvent
& event
)
3673 // update our idea of which lines are shown before scrolling the window
3674 UpdateShownLinesRange();
3677 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
3678 wxScrolledWindow::OnScroll(event
);
3680 HandleOnScroll( event
);
3683 if ( event
.GetOrientation() == wxHORIZONTAL
&& HasHeader() )
3685 wxListCtrl
* lc
= GetListCtrl();
3686 wxCHECK_RET( lc
, _T("no listctrl window?") );
3688 lc
->m_headerWin
->Refresh() ;
3690 lc
->m_headerWin
->MacUpdateImmediately() ;
3695 void wxListMainWindow::UpdateShownLinesRange()
3697 // only optimize redrawing for the report view using m_lineFrom/To
3698 if ( !HasFlag(wxLC_REPORT
) )
3701 size_t lineFromOld
= m_lineFrom
,
3702 lineToOld
= m_lineTo
;
3704 m_lineFrom
= GetScrollPos(wxVERTICAL
);
3706 size_t count
= GetItemCount();
3708 wxASSERT_MSG( m_lineFrom
< count
, _T("invalid scroll position?") );
3710 m_lineTo
= m_lineFrom
+ m_linesPerPage
- 1;
3711 if ( m_lineTo
>= count
)
3712 m_lineTo
= count
- 1;
3714 if ( m_lineFrom
!= lineFromOld
|| m_lineTo
!= lineToOld
)
3720 // -------------------------------------------------------------------------------------
3722 // -------------------------------------------------------------------------------------
3724 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
3726 wxListItem::wxListItem()
3735 m_format
= wxLIST_FORMAT_CENTRE
;
3741 void wxListItem::Clear()
3750 m_format
= wxLIST_FORMAT_CENTRE
;
3757 void wxListItem::ClearAttributes()
3766 // -------------------------------------------------------------------------------------
3768 // -------------------------------------------------------------------------------------
3770 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
3772 wxListEvent::wxListEvent( wxEventType commandType
, int id
)
3773 : wxNotifyEvent( commandType
, id
)
3779 m_cancelled
= FALSE
;
3784 void wxListEvent::CopyObject(wxObject
& object_dest
) const
3786 wxListEvent
*obj
= (wxListEvent
*)&object_dest
;
3788 wxNotifyEvent::CopyObject(object_dest
);
3790 obj
->m_code
= m_code
;
3791 obj
->m_itemIndex
= m_itemIndex
;
3792 obj
->m_oldItemIndex
= m_oldItemIndex
;
3794 obj
->m_cancelled
= m_cancelled
;
3795 obj
->m_pointDrag
= m_pointDrag
;
3796 obj
->m_item
.m_mask
= m_item
.m_mask
;
3797 obj
->m_item
.m_itemId
= m_item
.m_itemId
;
3798 obj
->m_item
.m_col
= m_item
.m_col
;
3799 obj
->m_item
.m_state
= m_item
.m_state
;
3800 obj
->m_item
.m_stateMask
= m_item
.m_stateMask
;
3801 obj
->m_item
.m_text
= m_item
.m_text
;
3802 obj
->m_item
.m_image
= m_item
.m_image
;
3803 obj
->m_item
.m_data
= m_item
.m_data
;
3804 obj
->m_item
.m_format
= m_item
.m_format
;
3805 obj
->m_item
.m_width
= m_item
.m_width
;
3807 if ( m_item
.HasAttributes() )
3809 obj
->m_item
.SetTextColour(m_item
.GetTextColour());
3813 // -------------------------------------------------------------------------------------
3815 // -------------------------------------------------------------------------------------
3817 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
3819 BEGIN_EVENT_TABLE(wxListCtrl
,wxControl
)
3820 EVT_SIZE(wxListCtrl::OnSize
)
3821 EVT_IDLE(wxListCtrl::OnIdle
)
3824 wxListCtrl::wxListCtrl()
3826 m_imageListNormal
= (wxImageList
*) NULL
;
3827 m_imageListSmall
= (wxImageList
*) NULL
;
3828 m_imageListState
= (wxImageList
*) NULL
;
3830 m_ownsImageListNormal
=
3831 m_ownsImageListSmall
=
3832 m_ownsImageListState
= FALSE
;
3834 m_mainWin
= (wxListMainWindow
*) NULL
;
3835 m_headerWin
= (wxListHeaderWindow
*) NULL
;
3838 wxListCtrl::~wxListCtrl()
3840 if (m_ownsImageListNormal
)
3841 delete m_imageListNormal
;
3842 if (m_ownsImageListSmall
)
3843 delete m_imageListSmall
;
3844 if (m_ownsImageListState
)
3845 delete m_imageListState
;
3848 void wxListCtrl::CreateHeaderWindow()
3850 m_headerWin
= new wxListHeaderWindow
3852 this, -1, m_mainWin
,
3854 wxSize(GetClientSize().x
, HEADER_HEIGHT
),
3859 bool wxListCtrl::Create(wxWindow
*parent
,
3864 const wxValidator
&validator
,
3865 const wxString
&name
)
3869 m_imageListState
= (wxImageList
*) NULL
;
3870 m_ownsImageListNormal
=
3871 m_ownsImageListSmall
=
3872 m_ownsImageListState
= FALSE
;
3874 m_mainWin
= (wxListMainWindow
*) NULL
;
3875 m_headerWin
= (wxListHeaderWindow
*) NULL
;
3877 if ( !(style
& (wxLC_REPORT
| wxLC_LIST
| wxLC_ICON
)) )
3879 style
= style
| wxLC_LIST
;
3882 if ( !wxControl::Create( parent
, id
, pos
, size
, style
, validator
, name
) )
3885 if ( style
& wxSUNKEN_BORDER
)
3886 style
-= wxSUNKEN_BORDER
;
3888 m_mainWin
= new wxListMainWindow( this, -1, wxPoint(0,0), size
, style
);
3890 if (HasFlag(wxLC_REPORT
))
3892 CreateHeaderWindow();
3894 if (HasFlag(wxLC_NO_HEADER
))
3896 // VZ: why do we create it at all then?
3897 m_headerWin
->Show( FALSE
);
3904 void wxListCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3906 /* handled in OnIdle */
3908 if (m_mainWin
) m_mainWin
->m_dirty
= TRUE
;
3911 void wxListCtrl::SetSingleStyle( long style
, bool add
)
3913 long flag
= GetWindowStyle();
3917 if (style
& wxLC_MASK_TYPE
)
3918 flag
&= ~wxLC_MASK_TYPE
;
3919 if (style
& wxLC_MASK_ALIGN
)
3920 flag
&= ~wxLC_MASK_ALIGN
;
3921 if (style
& wxLC_MASK_SORT
)
3922 flag
&= ~wxLC_MASK_SORT
;
3934 SetWindowStyleFlag( flag
);
3937 void wxListCtrl::SetWindowStyleFlag( long flag
)
3941 m_mainWin
->DeleteEverything();
3945 GetClientSize( &width
, &height
);
3947 if (flag
& wxLC_REPORT
)
3949 if (!HasFlag(wxLC_REPORT
))
3953 CreateHeaderWindow();
3955 if (HasFlag(wxLC_NO_HEADER
))
3956 m_headerWin
->Show( FALSE
);
3960 if (flag
& wxLC_NO_HEADER
)
3961 m_headerWin
->Show( FALSE
);
3963 m_headerWin
->Show( TRUE
);
3969 if ( m_mainWin
->HasHeader() )
3971 m_headerWin
->Show( FALSE
);
3976 wxWindow::SetWindowStyleFlag( flag
);
3979 bool wxListCtrl::GetColumn(int col
, wxListItem
&item
) const
3981 m_mainWin
->GetColumn( col
, item
);
3985 bool wxListCtrl::SetColumn( int col
, wxListItem
& item
)
3987 m_mainWin
->SetColumn( col
, item
);
3991 int wxListCtrl::GetColumnWidth( int col
) const
3993 return m_mainWin
->GetColumnWidth( col
);
3996 bool wxListCtrl::SetColumnWidth( int col
, int width
)
3998 m_mainWin
->SetColumnWidth( col
, width
);
4002 int wxListCtrl::GetCountPerPage() const
4004 return m_mainWin
->GetCountPerPage(); // different from Windows ?
4007 bool wxListCtrl::GetItem( wxListItem
&info
) const
4009 m_mainWin
->GetItem( info
);
4013 bool wxListCtrl::SetItem( wxListItem
&info
)
4015 m_mainWin
->SetItem( info
);
4019 long wxListCtrl::SetItem( long index
, int col
, const wxString
& label
, int imageId
)
4022 info
.m_text
= label
;
4023 info
.m_mask
= wxLIST_MASK_TEXT
;
4024 info
.m_itemId
= index
;
4028 info
.m_image
= imageId
;
4029 info
.m_mask
|= wxLIST_MASK_IMAGE
;
4031 m_mainWin
->SetItem(info
);
4035 int wxListCtrl::GetItemState( long item
, long stateMask
) const
4037 return m_mainWin
->GetItemState( item
, stateMask
);
4040 bool wxListCtrl::SetItemState( long item
, long state
, long stateMask
)
4042 m_mainWin
->SetItemState( item
, state
, stateMask
);
4046 bool wxListCtrl::SetItemImage( long item
, int image
, int WXUNUSED(selImage
) )
4049 info
.m_image
= image
;
4050 info
.m_mask
= wxLIST_MASK_IMAGE
;
4051 info
.m_itemId
= item
;
4052 m_mainWin
->SetItem( info
);
4056 wxString
wxListCtrl::GetItemText( long item
) const
4059 info
.m_itemId
= item
;
4060 m_mainWin
->GetItem( info
);
4064 void wxListCtrl::SetItemText( long item
, const wxString
&str
)
4067 info
.m_mask
= wxLIST_MASK_TEXT
;
4068 info
.m_itemId
= item
;
4070 m_mainWin
->SetItem( info
);
4073 long wxListCtrl::GetItemData( long item
) const
4076 info
.m_itemId
= item
;
4077 m_mainWin
->GetItem( info
);
4081 bool wxListCtrl::SetItemData( long item
, long data
)
4084 info
.m_mask
= wxLIST_MASK_DATA
;
4085 info
.m_itemId
= item
;
4087 m_mainWin
->SetItem( info
);
4091 bool wxListCtrl::GetItemRect( long item
, wxRect
&rect
, int WXUNUSED(code
) ) const
4093 m_mainWin
->GetItemRect( item
, rect
);
4097 bool wxListCtrl::GetItemPosition( long item
, wxPoint
& pos
) const
4099 m_mainWin
->GetItemPosition( item
, pos
);
4103 bool wxListCtrl::SetItemPosition( long WXUNUSED(item
), const wxPoint
& WXUNUSED(pos
) )
4108 int wxListCtrl::GetItemCount() const
4110 return m_mainWin
->GetItemCount();
4113 int wxListCtrl::GetColumnCount() const
4115 return m_mainWin
->GetColumnCount();
4118 void wxListCtrl::SetItemSpacing( int spacing
, bool isSmall
)
4120 m_mainWin
->SetItemSpacing( spacing
, isSmall
);
4123 int wxListCtrl::GetItemSpacing( bool isSmall
) const
4125 return m_mainWin
->GetItemSpacing( isSmall
);
4128 int wxListCtrl::GetSelectedItemCount() const
4130 return m_mainWin
->GetSelectedItemCount();
4133 wxColour
wxListCtrl::GetTextColour() const
4135 return GetForegroundColour();
4138 void wxListCtrl::SetTextColour(const wxColour
& col
)
4140 SetForegroundColour(col
);
4143 long wxListCtrl::GetTopItem() const
4148 long wxListCtrl::GetNextItem( long item
, int geom
, int state
) const
4150 return m_mainWin
->GetNextItem( item
, geom
, state
);
4153 wxImageList
*wxListCtrl::GetImageList(int which
) const
4155 if (which
== wxIMAGE_LIST_NORMAL
)
4157 return m_imageListNormal
;
4159 else if (which
== wxIMAGE_LIST_SMALL
)
4161 return m_imageListSmall
;
4163 else if (which
== wxIMAGE_LIST_STATE
)
4165 return m_imageListState
;
4167 return (wxImageList
*) NULL
;
4170 void wxListCtrl::SetImageList( wxImageList
*imageList
, int which
)
4172 if ( which
== wxIMAGE_LIST_NORMAL
)
4174 if (m_ownsImageListNormal
) delete m_imageListNormal
;
4175 m_imageListNormal
= imageList
;
4176 m_ownsImageListNormal
= FALSE
;
4178 else if ( which
== wxIMAGE_LIST_SMALL
)
4180 if (m_ownsImageListSmall
) delete m_imageListSmall
;
4181 m_imageListSmall
= imageList
;
4182 m_ownsImageListSmall
= FALSE
;
4184 else if ( which
== wxIMAGE_LIST_STATE
)
4186 if (m_ownsImageListState
) delete m_imageListState
;
4187 m_imageListState
= imageList
;
4188 m_ownsImageListState
= FALSE
;
4191 m_mainWin
->SetImageList( imageList
, which
);
4194 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
4196 SetImageList(imageList
, which
);
4197 if ( which
== wxIMAGE_LIST_NORMAL
)
4198 m_ownsImageListNormal
= TRUE
;
4199 else if ( which
== wxIMAGE_LIST_SMALL
)
4200 m_ownsImageListSmall
= TRUE
;
4201 else if ( which
== wxIMAGE_LIST_STATE
)
4202 m_ownsImageListState
= TRUE
;
4205 bool wxListCtrl::Arrange( int WXUNUSED(flag
) )
4210 bool wxListCtrl::DeleteItem( long item
)
4212 m_mainWin
->DeleteItem( item
);
4216 bool wxListCtrl::DeleteAllItems()
4218 m_mainWin
->DeleteAllItems();
4222 bool wxListCtrl::DeleteAllColumns()
4224 size_t count
= m_mainWin
->m_columns
.GetCount();
4225 for ( size_t n
= 0; n
< count
; n
++ )
4231 void wxListCtrl::ClearAll()
4233 m_mainWin
->DeleteEverything();
4236 bool wxListCtrl::DeleteColumn( int col
)
4238 m_mainWin
->DeleteColumn( col
);
4242 void wxListCtrl::Edit( long item
)
4244 m_mainWin
->EditLabel( item
);
4247 bool wxListCtrl::EnsureVisible( long item
)
4249 m_mainWin
->EnsureVisible( item
);
4253 long wxListCtrl::FindItem( long start
, const wxString
& str
, bool partial
)
4255 return m_mainWin
->FindItem( start
, str
, partial
);
4258 long wxListCtrl::FindItem( long start
, long data
)
4260 return m_mainWin
->FindItem( start
, data
);
4263 long wxListCtrl::FindItem( long WXUNUSED(start
), const wxPoint
& WXUNUSED(pt
),
4264 int WXUNUSED(direction
))
4269 long wxListCtrl::HitTest( const wxPoint
&point
, int &flags
)
4271 return m_mainWin
->HitTest( (int)point
.x
, (int)point
.y
, flags
);
4274 long wxListCtrl::InsertItem( wxListItem
& info
)
4276 m_mainWin
->InsertItem( info
);
4277 return info
.m_itemId
;
4280 long wxListCtrl::InsertItem( long index
, const wxString
&label
)
4283 info
.m_text
= label
;
4284 info
.m_mask
= wxLIST_MASK_TEXT
;
4285 info
.m_itemId
= index
;
4286 return InsertItem( info
);
4289 long wxListCtrl::InsertItem( long index
, int imageIndex
)
4292 info
.m_mask
= wxLIST_MASK_IMAGE
;
4293 info
.m_image
= imageIndex
;
4294 info
.m_itemId
= index
;
4295 return InsertItem( info
);
4298 long wxListCtrl::InsertItem( long index
, const wxString
&label
, int imageIndex
)
4301 info
.m_text
= label
;
4302 info
.m_image
= imageIndex
;
4303 info
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_IMAGE
;
4304 info
.m_itemId
= index
;
4305 return InsertItem( info
);
4308 long wxListCtrl::InsertColumn( long col
, wxListItem
&item
)
4310 wxASSERT( m_headerWin
);
4311 m_mainWin
->InsertColumn( col
, item
);
4312 m_headerWin
->Refresh();
4317 long wxListCtrl::InsertColumn( long col
, const wxString
&heading
,
4318 int format
, int width
)
4321 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
4322 item
.m_text
= heading
;
4325 item
.m_mask
|= wxLIST_MASK_WIDTH
;
4326 item
.m_width
= width
;
4328 item
.m_format
= format
;
4330 return InsertColumn( col
, item
);
4333 bool wxListCtrl::ScrollList( int WXUNUSED(dx
), int WXUNUSED(dy
) )
4339 // fn is a function which takes 3 long arguments: item1, item2, data.
4340 // item1 is the long data associated with a first item (NOT the index).
4341 // item2 is the long data associated with a second item (NOT the index).
4342 // data is the same value as passed to SortItems.
4343 // The return value is a negative number if the first item should precede the second
4344 // item, a positive number of the second item should precede the first,
4345 // or zero if the two items are equivalent.
4346 // data is arbitrary data to be passed to the sort function.
4348 bool wxListCtrl::SortItems( wxListCtrlCompare fn
, long data
)
4350 m_mainWin
->SortItems( fn
, data
);
4354 // ----------------------------------------------------------------------------
4355 // OnIdle() handler: this is the place where we redraw the control
4356 // ----------------------------------------------------------------------------
4358 void wxListCtrl::OnIdle( wxIdleEvent
& WXUNUSED(event
) )
4360 // do it only if needed
4361 if ( !m_mainWin
->m_dirty
)
4366 GetClientSize( &cw
, &ch
);
4368 if ( m_mainWin
->HasHeader() )
4370 m_headerWin
->SetSize( 0, 0, cw
, HEADER_HEIGHT
);
4371 m_mainWin
->SetSize( 0, HEADER_HEIGHT
+ 1, cw
, ch
- HEADER_HEIGHT
- 1 );
4373 else // no header window
4375 m_mainWin
->SetSize( 0, 0, cw
, ch
);
4378 m_mainWin
->CalculatePositions();
4380 m_mainWin
->m_dirty
= FALSE
;
4381 m_mainWin
->Refresh();
4383 if ( m_headerWin
&& m_headerWin
->m_dirty
)
4385 m_headerWin
->m_dirty
= FALSE
;
4386 m_headerWin
->Refresh();
4390 // ----------------------------------------------------------------------------
4392 // ----------------------------------------------------------------------------
4394 bool wxListCtrl::SetBackgroundColour( const wxColour
&colour
)
4398 m_mainWin
->SetBackgroundColour( colour
);
4399 m_mainWin
->m_dirty
= TRUE
;
4405 bool wxListCtrl::SetForegroundColour( const wxColour
&colour
)
4407 if ( !wxWindow::SetForegroundColour( colour
) )
4412 m_mainWin
->SetForegroundColour( colour
);
4413 m_mainWin
->m_dirty
= TRUE
;
4418 m_headerWin
->SetForegroundColour( colour
);
4424 bool wxListCtrl::SetFont( const wxFont
&font
)
4426 if ( !wxWindow::SetFont( font
) )
4431 m_mainWin
->SetFont( font
);
4432 m_mainWin
->m_dirty
= TRUE
;
4437 m_headerWin
->SetFont( font
);
4440 // invalidate it as the font changed
4441 m_mainWin
->OnFontChange();
4446 // ----------------------------------------------------------------------------
4447 // methods forwarded to m_mainWin
4448 // ----------------------------------------------------------------------------
4450 #if wxUSE_DRAG_AND_DROP
4452 void wxListCtrl::SetDropTarget( wxDropTarget
*dropTarget
)
4454 m_mainWin
->SetDropTarget( dropTarget
);
4457 wxDropTarget
*wxListCtrl::GetDropTarget() const
4459 return m_mainWin
->GetDropTarget();
4462 #endif // wxUSE_DRAG_AND_DROP
4464 bool wxListCtrl::SetCursor( const wxCursor
&cursor
)
4466 return m_mainWin
? m_mainWin
->wxWindow::SetCursor(cursor
) : FALSE
;
4469 wxColour
wxListCtrl::GetBackgroundColour() const
4471 return m_mainWin
? m_mainWin
->GetBackgroundColour() : wxColour();
4474 wxColour
wxListCtrl::GetForegroundColour() const
4476 return m_mainWin
? m_mainWin
->GetForegroundColour() : wxColour();
4479 bool wxListCtrl::DoPopupMenu( wxMenu
*menu
, int x
, int y
)
4482 return m_mainWin
->PopupMenu( menu
, x
, y
);
4485 #endif // wxUSE_MENUS
4488 void wxListCtrl::SetFocus()
4490 /* The test in window.cpp fails as we are a composite
4491 window, so it checks against "this", but not m_mainWin. */
4492 if ( FindFocus() != this )
4493 m_mainWin
->SetFocus();
4496 // ----------------------------------------------------------------------------
4497 // virtual list control support
4498 // ----------------------------------------------------------------------------
4500 wxString
wxListCtrl::OnGetItemText(long item
, long col
) const
4502 // this is a pure virtual function, in fact - which is not really pure
4503 // because the controls which are not virtual don't need to implement it
4504 wxFAIL_MSG( _T("not supposed to be called") );
4506 return wxEmptyString
;
4509 int wxListCtrl::OnGetItemImage(long item
) const
4512 wxFAIL_MSG( _T("not supposed to be called") );
4517 void wxListCtrl::SetItemCount(long count
)
4519 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
4521 m_mainWin
->SetItemCount(count
);
4524 #endif // wxUSE_LISTCTRL