1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "listctrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/listctrl.h"
40 #include "wx/msw/private.h"
43 #include "wx/msw/gnuwin32/extra.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
);
53 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
, HWND getFullInfo
= 0);
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 #if !USE_SHARED_LIBRARY
60 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
61 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
62 #endif // USE_SHARED_LIBRARY
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxListCtrl construction
70 // ----------------------------------------------------------------------------
72 wxListCtrl::wxListCtrl()
74 m_imageListNormal
= NULL
;
75 m_imageListSmall
= NULL
;
76 m_imageListState
= NULL
;
82 bool wxListCtrl::Create(wxWindow
*parent
,
87 const wxValidator
& validator
,
90 m_imageListNormal
= NULL
;
91 m_imageListSmall
= NULL
;
92 m_imageListState
= NULL
;
96 SetValidator(validator
);
104 m_windowStyle
= style
;
117 m_windowId
= (id
== -1) ? NewControlId() : id
;
119 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
120 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
121 if ( wxStyleHasBorder(m_windowStyle
) )
123 m_baseStyle
= wstyle
;
125 if ( !DoCreateControl(x
, y
, width
, height
) )
129 parent
->AddChild(this);
134 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
136 DWORD wstyle
= m_baseStyle
;
139 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
141 // Even with extended styles, need to combine with WS_BORDER
142 // for them to look right.
146 long oldStyle
= 0; // Dummy
147 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
149 // Create the ListView control.
150 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
155 GetWinHwnd(GetParent()),
162 wxLogError(_T("Can't create list control window."));
167 // for comctl32.dll v 4.70+ we want to have this attribute because it's
168 // prettier (and also because wxGTK does it like this)
169 #ifdef ListView_SetExtendedListViewStyle
170 if ( wstyle
& LVS_REPORT
)
172 ListView_SetExtendedListViewStyle(GetHwnd(),
173 LVS_EX_FULLROWSELECT
);
175 #endif // ListView_SetExtendedListViewStyle
177 wxSystemSettings settings
;
178 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
179 SetForegroundColour(GetParent()->GetForegroundColour());
186 void wxListCtrl::UpdateStyle()
190 // The new window view style
192 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
193 dwStyleNew
|= m_baseStyle
;
195 // Get the current window style.
196 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
198 // Only set the window style if the view bits have changed.
199 if ( dwStyleOld
!= dwStyleNew
)
201 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
206 wxListCtrl::~wxListCtrl()
210 m_textCtrl
->UnsubclassWin();
211 m_textCtrl
->SetHWND(0);
217 // ----------------------------------------------------------------------------
218 // set/get/change style
219 // ----------------------------------------------------------------------------
221 // Add or remove a single window style
222 void wxListCtrl::SetSingleStyle(long style
, bool add
)
224 long flag
= GetWindowStyleFlag();
226 // Get rid of conflicting styles
229 if ( style
& wxLC_MASK_TYPE
)
230 flag
= flag
& ~wxLC_MASK_TYPE
;
231 if ( style
& wxLC_MASK_ALIGN
)
232 flag
= flag
& ~wxLC_MASK_ALIGN
;
233 if ( style
& wxLC_MASK_SORT
)
234 flag
= flag
& ~wxLC_MASK_SORT
;
250 m_windowStyle
= flag
;
255 // Set the whole window style
256 void wxListCtrl::SetWindowStyleFlag(long flag
)
258 m_windowStyle
= flag
;
263 // Can be just a single style, or a bitlist
264 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
267 if ( style
& wxLC_ICON
)
269 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
270 oldStyle
-= LVS_SMALLICON
;
271 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
272 oldStyle
-= LVS_REPORT
;
273 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
274 oldStyle
-= LVS_LIST
;
278 if ( style
& wxLC_SMALL_ICON
)
280 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
281 oldStyle
-= LVS_ICON
;
282 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
283 oldStyle
-= LVS_REPORT
;
284 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
285 oldStyle
-= LVS_LIST
;
286 wstyle
|= LVS_SMALLICON
;
289 if ( style
& wxLC_LIST
)
291 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
292 oldStyle
-= LVS_ICON
;
293 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
294 oldStyle
-= LVS_REPORT
;
295 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
296 oldStyle
-= LVS_SMALLICON
;
300 if ( style
& wxLC_REPORT
)
302 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
303 oldStyle
-= LVS_ICON
;
304 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
305 oldStyle
-= LVS_LIST
;
306 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
307 oldStyle
-= LVS_SMALLICON
;
309 wstyle
|= LVS_REPORT
;
312 if ( style
& wxLC_ALIGN_LEFT
)
314 if ( oldStyle
& LVS_ALIGNTOP
)
315 oldStyle
-= LVS_ALIGNTOP
;
316 wstyle
|= LVS_ALIGNLEFT
;
319 if ( style
& wxLC_ALIGN_TOP
)
321 if ( oldStyle
& LVS_ALIGNLEFT
)
322 oldStyle
-= LVS_ALIGNLEFT
;
323 wstyle
|= LVS_ALIGNTOP
;
326 if ( style
& wxLC_AUTOARRANGE
)
327 wstyle
|= LVS_AUTOARRANGE
;
329 // Apparently, no such style (documentation wrong?)
331 if ( style & wxLC_BUTTON )
332 wstyle |= LVS_BUTTON;
335 if ( style
& wxLC_NO_SORT_HEADER
)
336 wstyle
|= LVS_NOSORTHEADER
;
338 if ( style
& wxLC_NO_HEADER
)
339 wstyle
|= LVS_NOCOLUMNHEADER
;
341 if ( style
& wxLC_EDIT_LABELS
)
342 wstyle
|= LVS_EDITLABELS
;
344 if ( style
& wxLC_SINGLE_SEL
)
345 wstyle
|= LVS_SINGLESEL
;
347 if ( style
& wxLC_SORT_ASCENDING
)
349 if ( oldStyle
& LVS_SORTDESCENDING
)
350 oldStyle
-= LVS_SORTDESCENDING
;
351 wstyle
|= LVS_SORTASCENDING
;
354 if ( style
& wxLC_SORT_DESCENDING
)
356 if ( oldStyle
& LVS_SORTASCENDING
)
357 oldStyle
-= LVS_SORTASCENDING
;
358 wstyle
|= LVS_SORTDESCENDING
;
364 // ----------------------------------------------------------------------------
366 // ----------------------------------------------------------------------------
368 // Sets the background colour (GetBackgroundColour already implicit in
370 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
372 if ( !wxWindow::SetBackgroundColour(col
) )
375 ListView_SetBkColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
380 // Gets information about this column
381 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
386 lvCol
.pszText
= NULL
;
388 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
390 lvCol
.mask
|= LVCF_TEXT
;
391 lvCol
.pszText
= new wxChar
[513];
392 lvCol
.cchTextMax
= 512;
395 bool success
= (ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0);
397 // item.m_subItem = lvCol.iSubItem;
398 item
.m_width
= lvCol
.cx
;
400 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
402 item
.m_text
= lvCol
.pszText
;
403 delete[] lvCol
.pszText
;
406 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
408 if (lvCol
.fmt
== LVCFMT_LEFT
)
409 item
.m_format
= wxLIST_FORMAT_LEFT
;
410 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
411 item
.m_format
= wxLIST_FORMAT_RIGHT
;
412 else if (lvCol
.fmt
== LVCFMT_CENTER
)
413 item
.m_format
= wxLIST_FORMAT_CENTRE
;
419 // Sets information about this column
420 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
425 lvCol
.pszText
= NULL
;
427 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
429 lvCol
.mask
|= LVCF_TEXT
;
430 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
431 lvCol
.cchTextMax
= 0; // Ignored
433 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
435 lvCol
.mask
|= LVCF_FMT
;
437 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
438 lvCol
.fmt
= LVCFMT_LEFT
;
439 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
440 lvCol
.fmt
= LVCFMT_RIGHT
;
441 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
442 lvCol
.fmt
= LVCFMT_CENTER
;
445 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
447 lvCol
.mask
|= LVCF_WIDTH
;
448 lvCol
.cx
= item
.m_width
;
450 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
451 lvCol
.cx
= LVSCW_AUTOSIZE
;
452 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
453 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
455 lvCol
.mask
|= LVCF_SUBITEM
;
456 lvCol
.iSubItem
= col
;
457 return (ListView_SetColumn(GetHwnd(), col
, & lvCol
) != 0);
460 // Gets the column width
461 int wxListCtrl::GetColumnWidth(int col
) const
463 return ListView_GetColumnWidth(GetHwnd(), col
);
466 // Sets the column width
467 bool wxListCtrl::SetColumnWidth(int col
, int width
)
470 if ( m_windowStyle
& wxLC_LIST
)
474 if ( width2
== wxLIST_AUTOSIZE
)
475 width2
= LVSCW_AUTOSIZE
;
476 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
477 width2
= LVSCW_AUTOSIZE_USEHEADER
;
479 return (ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0);
482 // Gets the number of items that can fit vertically in the
483 // visible area of the list control (list or report view)
484 // or the total number of items in the list control (icon
485 // or small icon view)
486 int wxListCtrl::GetCountPerPage(void) const
488 return ListView_GetCountPerPage(GetHwnd());
491 // Gets the edit control for editing labels.
492 wxTextCtrl
* wxListCtrl::GetEditControl(void) const
497 // Gets information about the item
498 bool wxListCtrl::GetItem(wxListItem
& info
) const
502 memset(&lvItem
, 0, sizeof(lvItem
));
504 ZeroMemory(&lvItem
, sizeof(lvItem
)); // must set all fields to 0
507 lvItem
.iItem
= info
.m_itemId
;
508 lvItem
.iSubItem
= info
.m_col
;
510 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
512 lvItem
.mask
|= LVIF_TEXT
;
513 lvItem
.pszText
= new wxChar
[513];
514 lvItem
.cchTextMax
= 512;
518 lvItem
.pszText
= NULL
;
521 if (info
.m_mask
& wxLIST_MASK_DATA
)
522 lvItem
.mask
|= LVIF_PARAM
;
524 if ( info
.m_mask
& wxLIST_MASK_STATE
)
526 lvItem
.mask
|= LVIF_STATE
;
527 // the other bits are hardly interesting anyhow
528 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
531 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
534 wxLogError(_("Couldn't retrieve information about list control item %d."),
539 wxConvertFromMSWListItem(this, info
, lvItem
);
543 delete[] lvItem
.pszText
;
548 // Sets information about the item
549 bool wxListCtrl::SetItem(wxListItem
& info
)
552 wxConvertToMSWListItem(this, info
, item
);
554 bool ok
= ListView_SetItem(GetHwnd(), &item
) != 0;
555 if ( ok
&& (info
.m_mask
& wxLIST_MASK_IMAGE
) )
557 // make the change visible
558 ListView_Update(GetHwnd(), item
.iItem
);
564 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
568 info
.m_mask
= wxLIST_MASK_TEXT
;
569 info
.m_itemId
= index
;
573 info
.m_image
= imageId
;
574 info
.m_mask
|= wxLIST_MASK_IMAGE
;
576 return SetItem(info
);
580 // Gets the item state
581 int wxListCtrl::GetItemState(long item
, long stateMask
) const
585 info
.m_mask
= wxLIST_MASK_STATE
;
586 info
.m_stateMask
= stateMask
;
587 info
.m_itemId
= item
;
595 // Sets the item state
596 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
600 info
.m_mask
= wxLIST_MASK_STATE
;
601 info
.m_state
= state
;
602 info
.m_stateMask
= stateMask
;
603 info
.m_itemId
= item
;
605 return SetItem(info
);
608 // Sets the item image
609 bool wxListCtrl::SetItemImage(long item
, int image
, int selImage
)
613 info
.m_mask
= wxLIST_MASK_IMAGE
;
614 info
.m_image
= image
;
615 info
.m_itemId
= item
;
617 return SetItem(info
);
620 // Gets the item text
621 wxString
wxListCtrl::GetItemText(long item
) const
625 info
.m_mask
= wxLIST_MASK_TEXT
;
626 info
.m_itemId
= item
;
633 // Sets the item text
634 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
638 info
.m_mask
= wxLIST_MASK_TEXT
;
639 info
.m_itemId
= item
;
645 // Gets the item data
646 long wxListCtrl::GetItemData(long item
) const
650 info
.m_mask
= wxLIST_MASK_DATA
;
651 info
.m_itemId
= item
;
658 // Sets the item data
659 bool wxListCtrl::SetItemData(long item
, long data
)
663 info
.m_mask
= wxLIST_MASK_DATA
;
664 info
.m_itemId
= item
;
667 return SetItem(info
);
670 // Gets the item rectangle
671 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
675 int code2
= LVIR_BOUNDS
;
676 if ( code
== wxLIST_RECT_BOUNDS
)
678 else if ( code
== wxLIST_RECT_ICON
)
680 else if ( code
== wxLIST_RECT_LABEL
)
684 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
) != 0);
686 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
, code2
) != 0);
691 rect
.width
= rect2
.right
- rect2
.left
;
692 rect
.height
= rect2
.bottom
- rect2
.left
;
696 // Gets the item position
697 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
701 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
703 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
707 // Sets the item position.
708 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
710 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
713 // Gets the number of items in the list control
714 int wxListCtrl::GetItemCount(void) const
716 return ListView_GetItemCount(GetHwnd());
719 // Retrieves the spacing between icons in pixels.
720 // If small is TRUE, gets the spacing for the small icon
721 // view, otherwise the large icon view.
722 int wxListCtrl::GetItemSpacing(bool isSmall
) const
724 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
727 // Gets the number of selected items in the list control
728 int wxListCtrl::GetSelectedItemCount(void) const
730 return ListView_GetSelectedCount(GetHwnd());
733 // Gets the text colour of the listview
734 wxColour
wxListCtrl::GetTextColour(void) const
736 COLORREF ref
= ListView_GetTextColor(GetHwnd());
737 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
741 // Sets the text colour of the listview
742 void wxListCtrl::SetTextColour(const wxColour
& col
)
744 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
747 // Gets the index of the topmost visible item when in
748 // list or report view
749 long wxListCtrl::GetTopItem(void) const
751 return (long) ListView_GetTopIndex(GetHwnd());
754 // Searches for an item, starting from 'item'.
755 // 'geometry' is one of
756 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
757 // 'state' is a state bit flag, one or more of
758 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
759 // item can be -1 to find the first item that matches the
761 // Returns the item or -1 if unsuccessful.
762 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
766 if ( geom
== wxLIST_NEXT_ABOVE
)
768 if ( geom
== wxLIST_NEXT_ALL
)
770 if ( geom
== wxLIST_NEXT_BELOW
)
772 if ( geom
== wxLIST_NEXT_LEFT
)
773 flags
|= LVNI_TOLEFT
;
774 if ( geom
== wxLIST_NEXT_RIGHT
)
775 flags
|= LVNI_TORIGHT
;
777 if ( state
& wxLIST_STATE_CUT
)
779 if ( state
& wxLIST_STATE_DROPHILITED
)
780 flags
|= LVNI_DROPHILITED
;
781 if ( state
& wxLIST_STATE_FOCUSED
)
782 flags
|= LVNI_FOCUSED
;
783 if ( state
& wxLIST_STATE_SELECTED
)
784 flags
|= LVNI_SELECTED
;
786 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
790 wxImageList
*wxListCtrl::GetImageList(int which
) const
792 if ( which
== wxIMAGE_LIST_NORMAL
)
794 return m_imageListNormal
;
796 else if ( which
== wxIMAGE_LIST_SMALL
)
798 return m_imageListSmall
;
800 else if ( which
== wxIMAGE_LIST_STATE
)
802 return m_imageListState
;
807 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
810 if ( which
== wxIMAGE_LIST_NORMAL
)
812 flags
= LVSIL_NORMAL
;
813 m_imageListNormal
= imageList
;
815 else if ( which
== wxIMAGE_LIST_SMALL
)
818 m_imageListSmall
= imageList
;
820 else if ( which
== wxIMAGE_LIST_STATE
)
823 m_imageListState
= imageList
;
825 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
828 // ----------------------------------------------------------------------------
830 // ----------------------------------------------------------------------------
832 // Arranges the items
833 bool wxListCtrl::Arrange(int flag
)
836 if ( flag
== wxLIST_ALIGN_LEFT
)
837 code
= LVA_ALIGNLEFT
;
838 else if ( flag
== wxLIST_ALIGN_TOP
)
840 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
842 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
843 code
= LVA_SNAPTOGRID
;
845 return (ListView_Arrange(GetHwnd(), code
) != 0);
849 bool wxListCtrl::DeleteItem(long item
)
851 return (ListView_DeleteItem(GetHwnd(), (int) item
) != 0);
855 bool wxListCtrl::DeleteAllItems()
857 return (ListView_DeleteAllItems(GetHwnd()) != 0);
861 bool wxListCtrl::DeleteAllColumns()
863 while ( m_colCount
> 0 )
865 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
867 wxLogLastError("ListView_DeleteColumn");
875 wxASSERT_MSG( m_colCount
== 0, _T("no columns should be left") );
881 bool wxListCtrl::DeleteColumn(int col
)
883 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
885 if ( success
&& (m_colCount
> 0) )
890 // Clears items, and columns if there are any.
891 void wxListCtrl::ClearAll()
894 if ( m_colCount
> 0 )
898 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
900 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
902 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
906 m_textCtrl
->UnsubclassWin();
907 m_textCtrl
->SetHWND(0);
912 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
913 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
914 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
919 // End label editing, optionally cancelling the edit
920 bool wxListCtrl::EndEditLabel(bool cancel
)
924 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
926 bool success = (ListView_EndEditLabelNow(GetHwnd(), cancel) != 0);
930 m_textCtrl->UnsubclassWin();
931 m_textCtrl->SetHWND(0);
941 // Ensures this item is visible
942 bool wxListCtrl::EnsureVisible(long item
)
944 return (ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0);
947 // Find an item whose label matches this string, starting from the item after 'start'
948 // or the beginning if 'start' is -1.
949 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
951 LV_FINDINFO findInfo
;
953 findInfo
.flags
= LVFI_STRING
;
955 findInfo
.flags
|= LVFI_STRING
;
956 findInfo
.psz
= WXSTRINGCAST str
;
958 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
961 // Find an item whose data matches this data, starting from the item after 'start'
962 // or the beginning if 'start' is -1.
963 long wxListCtrl::FindItem(long start
, long data
)
965 LV_FINDINFO findInfo
;
967 findInfo
.flags
= LVFI_PARAM
;
968 findInfo
.lParam
= data
;
970 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
973 // Find an item nearest this position in the specified direction, starting from
974 // the item after 'start' or the beginning if 'start' is -1.
975 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
977 LV_FINDINFO findInfo
;
979 findInfo
.flags
= LVFI_NEARESTXY
;
980 findInfo
.pt
.x
= pt
.x
;
981 findInfo
.pt
.y
= pt
.y
;
982 findInfo
.vkDirection
= VK_RIGHT
;
984 if ( direction
== wxLIST_FIND_UP
)
985 findInfo
.vkDirection
= VK_UP
;
986 else if ( direction
== wxLIST_FIND_DOWN
)
987 findInfo
.vkDirection
= VK_DOWN
;
988 else if ( direction
== wxLIST_FIND_LEFT
)
989 findInfo
.vkDirection
= VK_LEFT
;
990 else if ( direction
== wxLIST_FIND_RIGHT
)
991 findInfo
.vkDirection
= VK_RIGHT
;
993 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
996 // Determines which item (if any) is at the specified point,
997 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
998 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1000 LV_HITTESTINFO hitTestInfo
;
1001 hitTestInfo
.pt
.x
= (int) point
.x
;
1002 hitTestInfo
.pt
.y
= (int) point
.y
;
1004 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1007 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1008 flags
|= wxLIST_HITTEST_ABOVE
;
1009 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1010 flags
|= wxLIST_HITTEST_BELOW
;
1011 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1012 flags
|= wxLIST_HITTEST_NOWHERE
;
1013 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1014 flags
|= wxLIST_HITTEST_ONITEMICON
;
1015 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1016 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1017 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1018 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1019 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1020 flags
|= wxLIST_HITTEST_TOLEFT
;
1021 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1022 flags
|= wxLIST_HITTEST_TORIGHT
;
1024 return (long) hitTestInfo
.iItem
;
1027 // Inserts an item, returning the index of the new item if successful,
1029 long wxListCtrl::InsertItem(wxListItem
& info
)
1032 wxConvertToMSWListItem(this, info
, item
);
1034 return (long) ListView_InsertItem(GetHwnd(), & item
);
1037 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1040 info
.m_text
= label
;
1041 info
.m_mask
= wxLIST_MASK_TEXT
;
1042 info
.m_itemId
= index
;
1043 return InsertItem(info
);
1046 // Inserts an image item
1047 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1050 info
.m_image
= imageIndex
;
1051 info
.m_mask
= wxLIST_MASK_IMAGE
;
1052 info
.m_itemId
= index
;
1053 return InsertItem(info
);
1056 // Inserts an image/string item
1057 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1060 info
.m_image
= imageIndex
;
1061 info
.m_text
= label
;
1062 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1063 info
.m_itemId
= index
;
1064 return InsertItem(info
);
1067 // For list view mode (only), inserts a column.
1068 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1073 lvCol
.pszText
= NULL
;
1075 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1077 lvCol
.mask
|= LVCF_TEXT
;
1078 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1079 lvCol
.cchTextMax
= 0; // Ignored
1081 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1083 lvCol
.mask
|= LVCF_FMT
;
1085 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1086 lvCol
.fmt
= LVCFMT_LEFT
;
1087 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1088 lvCol
.fmt
= LVCFMT_RIGHT
;
1089 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1090 lvCol
.fmt
= LVCFMT_CENTER
;
1093 lvCol
.mask
|= LVCF_WIDTH
;
1094 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1096 if ( item
.m_width
== wxLIST_AUTOSIZE
)
1097 lvCol
.cx
= LVSCW_AUTOSIZE
;
1098 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
1099 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1101 lvCol
.cx
= item
.m_width
;
1105 // always give some width to the new column: this one is compatible
1110 lvCol
.mask
|= LVCF_SUBITEM
;
1111 lvCol
.iSubItem
= col
;
1113 bool success
= ListView_InsertColumn(GetHwnd(), col
, & lvCol
) != -1;
1120 wxLogDebug(_T("Failed to insert the column '%s' into listview!"),
1127 long wxListCtrl::InsertColumn(long col
, const wxString
& heading
, int format
,
1131 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1132 item
.m_text
= heading
;
1135 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1136 item
.m_width
= width
;
1138 item
.m_format
= format
;
1140 return InsertColumn(col
, item
);
1143 // Scrolls the list control. If in icon, small icon or report view mode,
1144 // x specifies the number of pixels to scroll. If in list view mode, x
1145 // specifies the number of columns to scroll.
1146 // If in icon, small icon or list view mode, y specifies the number of pixels
1147 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1148 bool wxListCtrl::ScrollList(int dx
, int dy
)
1150 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1155 // fn is a function which takes 3 long arguments: item1, item2, data.
1156 // item1 is the long data associated with a first item (NOT the index).
1157 // item2 is the long data associated with a second item (NOT the index).
1158 // data is the same value as passed to SortItems.
1159 // The return value is a negative number if the first item should precede the second
1160 // item, a positive number of the second item should precede the first,
1161 // or zero if the two items are equivalent.
1163 // data is arbitrary data to be passed to the sort function.
1164 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1166 return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
) fn
, data
) != 0);
1169 // ----------------------------------------------------------------------------
1170 // message processing
1171 // ----------------------------------------------------------------------------
1173 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1175 if (cmd
== EN_UPDATE
)
1177 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1178 event
.SetEventObject( this );
1179 ProcessCommand(event
);
1182 else if (cmd
== EN_KILLFOCUS
)
1184 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1185 event
.SetEventObject( this );
1186 ProcessCommand(event
);
1193 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1195 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1196 wxEventType eventType
= wxEVT_NULL
;
1197 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1198 switch ( hdr1
->code
)
1200 case LVN_BEGINRDRAG
:
1201 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1205 if ( eventType
== wxEVT_NULL
)
1207 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1211 NM_LISTVIEW
*hdr
= (NM_LISTVIEW
*)lParam
;
1212 event
.m_itemIndex
= hdr
->iItem
;
1213 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1214 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1218 case LVN_BEGINLABELEDIT
:
1220 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1221 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1222 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1226 case LVN_COLUMNCLICK
:
1228 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1229 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1230 event
.m_itemIndex
= -1;
1231 event
.m_col
= hdr
->iSubItem
;
1235 case LVN_DELETEALLITEMS
:
1236 // what's the sense of generating a wxWin event for this when
1237 // it's absolutely not portable?
1239 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1240 event
.m_itemIndex
= -1;
1243 // return TRUE to suppress all additional LVN_DELETEITEM
1244 // notifications - this makes deleting all items from a list ctrl
1249 case LVN_DELETEITEM
:
1251 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1252 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1253 event
.m_itemIndex
= hdr
->iItem
;
1256 case LVN_ENDLABELEDIT
:
1258 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1259 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1260 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1261 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1262 event
.m_cancelled
= TRUE
;
1265 case LVN_GETDISPINFO
:
1268 // this provokes stack overflow: indeed, wxConvertFromMSWListItem()
1269 // sends us WM_NOTIFY! As it doesn't do anything for now, just leave
1273 // TODO: some text buffering here, I think
1274 // TODO: API for getting Windows to retrieve values
1276 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1277 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1278 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1283 case LVN_INSERTITEM
:
1285 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1286 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1287 event
.m_itemIndex
= hdr
->iItem
;
1290 case LVN_ITEMCHANGED
:
1292 // This needs to be sent to wxListCtrl as a rather more
1293 // concrete event. For now, just detect a selection
1295 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1296 if ( (hdr
->uNewState
& LVIS_SELECTED
) && !(hdr
->uOldState
& LVIS_SELECTED
) )
1298 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1299 event
.m_itemIndex
= hdr
->iItem
;
1301 else if ( !(hdr
->uNewState
& LVIS_SELECTED
) && (hdr
->uOldState
& LVIS_SELECTED
) )
1303 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1304 event
.m_itemIndex
= hdr
->iItem
;
1313 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1314 WORD wVKey
= info
->wVKey
;
1316 // get the current selection
1317 long lItem
= GetNextItem(-1,
1319 wxLIST_STATE_SELECTED
);
1321 // <Enter> or <Space> activate the selected item if any
1322 if ( lItem
!= -1 && (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) )
1324 // TODO this behaviour probably should be optional
1325 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1326 event
.m_itemIndex
= lItem
;
1330 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1331 event
.m_code
= wxCharCodeMSWToWX(wVKey
);
1337 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1339 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1344 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1345 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1348 case LVN_SETDISPINFO
:
1350 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1351 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1352 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1357 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1360 event
.SetEventObject( this );
1361 event
.SetEventType(eventType
);
1363 if ( !GetEventHandler()->ProcessEvent(event
) )
1366 if (hdr1
->code
== LVN_GETDISPINFO
)
1368 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1369 if ( info
->item
.mask
& LVIF_TEXT
)
1371 if ( !event
.m_item
.m_text
.IsNull() )
1373 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1374 info
->item
.cchTextMax
= wxStrlen(info
->item
.pszText
) + 1;
1377 // wxConvertToMSWListItem(this, event.m_item, info->item);
1380 *result
= !event
.IsAllowed();
1385 wxChar
*wxListCtrl::AddPool(const wxString
& str
)
1387 // Remove the first element if 3 strings exist
1388 if ( m_stringPool
.Number() == 3 )
1390 wxNode
*node
= m_stringPool
.First();
1391 delete[] (char *)node
->Data();
1394 wxNode
*node
= m_stringPool
.Add(WXSTRINGCAST str
);
1395 return (wxChar
*)node
->Data();
1398 // ----------------------------------------------------------------------------
1400 // ----------------------------------------------------------------------------
1402 // List item structure
1403 wxListItem::wxListItem()
1413 m_format
= wxLIST_FORMAT_CENTRE
;
1417 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1419 info
.m_data
= lvItem
.lParam
;
1422 info
.m_stateMask
= 0;
1423 info
.m_itemId
= lvItem
.iItem
;
1425 long oldMask
= lvItem
.mask
;
1427 bool needText
= FALSE
;
1428 if (getFullInfo
!= 0)
1430 if ( lvItem
.mask
& LVIF_TEXT
)
1437 lvItem
.pszText
= new wxChar
[513];
1438 lvItem
.cchTextMax
= 512;
1440 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
1441 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1442 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
1445 if ( lvItem
.mask
& LVIF_STATE
)
1447 info
.m_mask
|= wxLIST_MASK_STATE
;
1449 if ( lvItem
.stateMask
& LVIS_CUT
)
1451 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1452 if ( lvItem
.state
& LVIS_CUT
)
1453 info
.m_state
|= wxLIST_STATE_CUT
;
1455 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1457 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1458 if ( lvItem
.state
& LVIS_DROPHILITED
)
1459 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1461 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1463 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1464 if ( lvItem
.state
& LVIS_FOCUSED
)
1465 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1467 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1469 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1470 if ( lvItem
.state
& LVIS_SELECTED
)
1471 info
.m_state
|= wxLIST_STATE_SELECTED
;
1475 if ( lvItem
.mask
& LVIF_TEXT
)
1477 info
.m_mask
|= wxLIST_MASK_TEXT
;
1478 info
.m_text
= lvItem
.pszText
;
1480 if ( lvItem
.mask
& LVIF_IMAGE
)
1482 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1483 info
.m_image
= lvItem
.iImage
;
1485 if ( lvItem
.mask
& LVIF_PARAM
)
1486 info
.m_mask
|= wxLIST_MASK_DATA
;
1487 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1488 info
.m_mask
|= wxLIST_SET_ITEM
;
1489 info
.m_col
= lvItem
.iSubItem
;
1494 delete[] lvItem
.pszText
;
1496 lvItem
.mask
= oldMask
;
1499 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1501 lvItem
.iItem
= (int) info
.m_itemId
;
1503 lvItem
.iImage
= info
.m_image
;
1504 lvItem
.lParam
= info
.m_data
;
1505 lvItem
.stateMask
= 0;
1508 lvItem
.iSubItem
= info
.m_col
;
1510 if (info
.m_mask
& wxLIST_MASK_STATE
)
1512 lvItem
.mask
|= LVIF_STATE
;
1513 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1515 lvItem
.stateMask
|= LVIS_CUT
;
1516 if (info
.m_state
& wxLIST_STATE_CUT
)
1517 lvItem
.state
|= LVIS_CUT
;
1519 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1521 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1522 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1523 lvItem
.state
|= LVIS_DROPHILITED
;
1525 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1527 lvItem
.stateMask
|= LVIS_FOCUSED
;
1528 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1529 lvItem
.state
|= LVIS_FOCUSED
;
1531 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1533 lvItem
.stateMask
|= LVIS_SELECTED
;
1534 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1535 lvItem
.state
|= LVIS_SELECTED
;
1539 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1541 lvItem
.mask
|= LVIF_TEXT
;
1542 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1544 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1548 lvItem
.pszText
= WXSTRINGCAST info
.m_text
;
1549 if ( lvItem
.pszText
)
1550 lvItem
.cchTextMax
= info
.m_text
.Length();
1552 lvItem
.cchTextMax
= 0;
1555 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
1556 lvItem
.mask
|= LVIF_IMAGE
;
1557 if (info
.m_mask
& wxLIST_MASK_DATA
)
1558 lvItem
.mask
|= LVIF_PARAM
;
1561 // ----------------------------------------------------------------------------
1563 // ----------------------------------------------------------------------------
1565 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
1567 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
1568 : wxNotifyEvent(commandType
, id
)
1573 m_cancelled
= FALSE
;