1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "listctrl.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
27 #if defined(__WIN95__)
29 #include "wx/listctrl.h"
31 #include "wx/msw/private.h"
38 #include "wx/msw/gnuwin32/extra.h"
41 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
);
42 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
, HWND getFullInfo
= 0);
44 #if !USE_SHARED_LIBRARY
45 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
46 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
50 wxListCtrl::wxListCtrl(void)
52 m_imageListNormal
= NULL
;
53 m_imageListSmall
= NULL
;
54 m_imageListState
= NULL
;
59 bool wxListCtrl::Create(wxWindow
*parent
, const wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
60 const long style
, const wxValidator
& validator
, const wxString
& name
)
62 m_imageListNormal
= NULL
;
63 m_imageListSmall
= NULL
;
64 m_imageListState
= NULL
;
67 wxSystemSettings settings
;
68 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
69 SetForegroundColour(parent
->GetDefaultForegroundColour());
71 SetValidator(validator
);
79 m_windowStyle
= style
;
92 m_windowId
= (id
== -1) ? NewControlId() : id
;
94 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
;
97 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
99 // Even with extended styles, need to combine with WS_BORDER
100 // for them to look right.
101 if (want3D
|| (m_windowStyle
& wxSIMPLE_BORDER
) || (m_windowStyle
& wxRAISED_BORDER
) ||
102 (m_windowStyle
& wxSUNKEN_BORDER
) || (m_windowStyle
& wxDOUBLE_BORDER
))
105 wstyle
|= LVS_SHAREIMAGELISTS
;
106 m_baseStyle
= wstyle
;
108 long oldStyle
= 0; // Dummy
109 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
111 // Create the ListView control.
112 HWND hWndListControl
= CreateWindowEx(exStyle
,
117 (HWND
) parent
->GetHWND(),
122 m_hWnd
= (WXHWND
) hWndListControl
;
123 if (parent
) parent
->AddChild(this);
125 SubclassWin((WXHWND
) m_hWnd
);
130 wxListCtrl::~wxListCtrl(void)
132 m_textCtrl
.SetHWND((WXHWND
) NULL
);
135 // Add or remove a single window style
136 void wxListCtrl::SetSingleStyle(const long style
, const bool add
)
138 long flag
= GetWindowStyleFlag();
140 // Get rid of conflicting styles
143 if ( style
& wxLC_MASK_TYPE
)
144 flag
= flag
& ~wxLC_MASK_TYPE
;
145 if ( style
& wxLC_MASK_ALIGN
)
146 flag
= flag
& ~wxLC_MASK_ALIGN
;
147 if ( style
& wxLC_MASK_SORT
)
148 flag
= flag
& ~wxLC_MASK_SORT
;
164 m_windowStyle
= flag
;
169 // Set the whole window style
170 void wxListCtrl::SetWindowStyleFlag(const long flag
)
172 m_windowStyle
= flag
;
177 void wxListCtrl::RecreateWindow(void)
182 long style
= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
183 style
|= m_baseStyle
;
184 // ::SetWindowLong((HWND) GetHWND(), GWL_STYLE, style);
186 // The following recreation of the window appears to be necessary
187 // because SetWindowLong doesn't seem to do it.
189 int x
, y
, width
, height
;
191 GetSize(&width
, &height
);
194 ::DestroyWindow((HWND
) GetHWND());
197 // Recreate the ListView control: unfortunately I can't
198 // make it work by using SetWindowLong.
200 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
201 HWND hWndListControl
= CreateWindowEx(exStyle
,
206 (HWND
) GetParent()->GetHWND(),
211 m_hWnd
= (WXHWND
) hWndListControl
;
212 SubclassWin((WXHWND
) m_hWnd
);
214 if ( m_imageListNormal
)
215 SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
216 if ( m_imageListSmall
)
217 SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
218 if ( m_imageListState
)
219 SetImageList(m_imageListState
, wxIMAGE_LIST_STATE
);
223 // Can be just a single style, or a bitlist
224 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, const long style
) const
227 if ( style
& wxLC_ICON
)
229 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
230 oldStyle
-= LVS_SMALLICON
;
231 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
232 oldStyle
-= LVS_REPORT
;
233 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
234 oldStyle
-= LVS_LIST
;
238 if ( style
& wxLC_SMALL_ICON
)
240 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
241 oldStyle
-= LVS_ICON
;
242 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
243 oldStyle
-= LVS_REPORT
;
244 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
245 oldStyle
-= LVS_LIST
;
246 wstyle
|= LVS_SMALLICON
;
249 if ( style
& wxLC_LIST
)
251 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
252 oldStyle
-= LVS_ICON
;
253 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
254 oldStyle
-= LVS_REPORT
;
255 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
256 oldStyle
-= LVS_SMALLICON
;
260 if ( style
& wxLC_REPORT
)
262 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
263 oldStyle
-= LVS_ICON
;
264 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
265 oldStyle
-= LVS_LIST
;
266 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
267 oldStyle
-= LVS_SMALLICON
;
268 wstyle
|= LVS_REPORT
;
271 if ( style
& wxLC_ALIGN_LEFT
)
273 if ( oldStyle
& LVS_ALIGNTOP
)
274 oldStyle
-= LVS_ALIGNTOP
;
275 wstyle
|= LVS_ALIGNLEFT
;
278 if ( style
& wxLC_ALIGN_TOP
)
280 if ( oldStyle
& LVS_ALIGNLEFT
)
281 oldStyle
-= LVS_ALIGNLEFT
;
282 wstyle
|= LVS_ALIGNTOP
;
285 if ( style
& wxLC_AUTOARRANGE
)
286 wstyle
|= LVS_AUTOARRANGE
;
288 // Apparently, no such style (documentation wrong?)
290 if ( style & wxLC_BUTTON )
291 wstyle |= LVS_BUTTON;
294 if ( style
& wxLC_NO_SORT_HEADER
)
295 wstyle
|= LVS_NOSORTHEADER
;
297 if ( style
& wxLC_NO_HEADER
)
298 wstyle
|= LVS_NOCOLUMNHEADER
;
300 if ( style
& wxLC_EDIT_LABELS
)
301 wstyle
|= LVS_EDITLABELS
;
303 if ( style
& wxLC_SINGLE_SEL
)
304 wstyle
|= LVS_SINGLESEL
;
306 if ( style
& wxLC_SORT_ASCENDING
)
308 if ( oldStyle
& LVS_SORTDESCENDING
)
309 oldStyle
-= LVS_SORTDESCENDING
;
310 wstyle
|= LVS_SORTASCENDING
;
313 if ( style
& wxLC_SORT_DESCENDING
)
315 if ( oldStyle
& LVS_SORTASCENDING
)
316 oldStyle
-= LVS_SORTASCENDING
;
317 wstyle
|= LVS_SORTDESCENDING
;
323 // Sets the background colour (GetBackgroundColour already implicit in
325 void wxListCtrl::SetBackgroundColour(const wxColour
& col
)
327 wxWindow::SetBackgroundColour(col
);
329 ListView_SetBkColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
332 // Gets information about this column
333 bool wxListCtrl::GetColumn(const int col
, wxListItem
& item
) const
338 lvCol
.pszText
= NULL
;
340 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
342 lvCol
.mask
|= LVCF_TEXT
;
343 lvCol
.pszText
= new char[513];
344 lvCol
.cchTextMax
= 512;
347 bool success
= (ListView_GetColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
349 // item.m_subItem = lvCol.iSubItem;
350 item
.m_width
= lvCol
.cx
;
352 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
354 item
.m_text
= lvCol
.pszText
;
355 delete[] lvCol
.pszText
;
358 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
360 if (lvCol
.fmt
== LVCFMT_LEFT
)
361 item
.m_format
= wxLIST_FORMAT_LEFT
;
362 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
363 item
.m_format
= wxLIST_FORMAT_RIGHT
;
364 else if (lvCol
.fmt
== LVCFMT_CENTER
)
365 item
.m_format
= wxLIST_FORMAT_CENTRE
;
371 // Sets information about this column
372 bool wxListCtrl::SetColumn(const int col
, wxListItem
& item
)
377 lvCol
.pszText
= NULL
;
379 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
381 lvCol
.mask
|= LVCF_TEXT
;
382 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
383 lvCol
.cchTextMax
= 0; // Ignored
385 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
387 lvCol
.mask
|= LVCF_FMT
;
389 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
390 lvCol
.fmt
= LVCFMT_LEFT
;
391 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
392 lvCol
.fmt
= LVCFMT_RIGHT
;
393 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
394 lvCol
.fmt
= LVCFMT_CENTER
;
397 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
399 lvCol
.mask
|= LVCF_WIDTH
;
400 lvCol
.cx
= item
.m_width
;
402 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
403 lvCol
.cx
= LVSCW_AUTOSIZE
;
404 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
405 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
407 lvCol
.mask
|= LVCF_SUBITEM
;
408 lvCol
.iSubItem
= col
;
409 return (ListView_SetColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
412 // Gets the column width
413 int wxListCtrl::GetColumnWidth(const int col
) const
415 return ListView_GetColumnWidth((HWND
) GetHWND(), col
);
418 // Sets the column width
419 bool wxListCtrl::SetColumnWidth(const int col
, const int width
)
422 if ( m_windowStyle
& wxLC_LIST
)
426 if ( width2
== wxLIST_AUTOSIZE
)
427 width2
= LVSCW_AUTOSIZE
;
428 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
429 width2
= LVSCW_AUTOSIZE_USEHEADER
;
431 return (ListView_SetColumnWidth((HWND
) GetHWND(), col2
, width2
) != 0);
434 // Gets the number of items that can fit vertically in the
435 // visible area of the list control (list or report view)
436 // or the total number of items in the list control (icon
437 // or small icon view)
438 int wxListCtrl::GetCountPerPage(void) const
440 return ListView_GetCountPerPage((HWND
) GetHWND());
443 // Gets the edit control for editing labels.
444 wxTextCtrl
& wxListCtrl::GetEditControl(void) const
446 HWND hWnd
= (HWND
) ListView_GetEditControl((HWND
) GetHWND());
447 ((wxListCtrl
*)this)->m_textCtrl
.SetHWND((WXHWND
) hWnd
);
448 return (wxTextCtrl
&)m_textCtrl
;
451 // Gets information about the item
452 bool wxListCtrl::GetItem(wxListItem
& info
) const
455 lvItem
.pszText
= NULL
;
456 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
458 lvItem
.pszText
= new char[513];
459 lvItem
.cchTextMax
= 512;
461 bool success
= (::SendMessage((HWND
) GetHWND(), LVM_GETITEM
, 0, (LPARAM
)& lvItem
) != 0);
466 delete[] lvItem
.pszText
;
471 wxConvertFromMSWListItem(this, info
, lvItem
);
474 delete[] lvItem
.pszText
;
479 // Sets information about the item
480 bool wxListCtrl::SetItem(wxListItem
& info
)
483 wxConvertToMSWListItem(this, info
, item
);
485 return (ListView_SetItem((HWND
) GetHWND(), &item
) != 0);
488 long wxListCtrl::SetItem(const long index
, const int col
, const wxString
& label
, const int imageId
)
492 info
.m_mask
= wxLIST_MASK_TEXT
;
493 info
.m_itemId
= index
;
497 info
.m_image
= imageId
;
498 info
.m_mask
|= wxLIST_MASK_IMAGE
;
500 return SetItem(info
);
504 // Gets the item state
505 int wxListCtrl::GetItemState(const long item
, const long stateMask
) const
509 info
.m_mask
= wxLIST_MASK_STATE
;
510 info
.m_stateMask
= stateMask
;
511 info
.m_itemId
= item
;
519 // Sets the item state
520 bool wxListCtrl::SetItemState(const long item
, const long state
, const long stateMask
)
524 info
.m_mask
= wxLIST_MASK_STATE
;
525 info
.m_state
= state
;
526 info
.m_stateMask
= stateMask
;
527 info
.m_itemId
= item
;
529 return SetItem(info
);
532 // Sets the item image
533 bool wxListCtrl::SetItemImage(const long item
, const int image
, const int selImage
)
537 info
.m_mask
= wxLIST_MASK_IMAGE
;
538 info
.m_image
= image
;
539 info
.m_itemId
= item
;
541 return SetItem(info
);
544 // Gets the item text
545 wxString
wxListCtrl::GetItemText(const long item
) const
549 info
.m_mask
= wxLIST_MASK_TEXT
;
550 info
.m_itemId
= item
;
557 // Sets the item text
558 void wxListCtrl::SetItemText(const long item
, const wxString
& str
)
562 info
.m_mask
= wxLIST_MASK_TEXT
;
563 info
.m_itemId
= item
;
569 // Gets the item data
570 long wxListCtrl::GetItemData(const long item
) const
574 info
.m_mask
= wxLIST_MASK_DATA
;
575 info
.m_itemId
= item
;
582 // Sets the item data
583 bool wxListCtrl::SetItemData(const long item
, long data
)
587 info
.m_mask
= wxLIST_MASK_DATA
;
588 info
.m_itemId
= item
;
591 return SetItem(info
);
594 // Gets the item rectangle
595 bool wxListCtrl::GetItemRect(const long item
, wxRectangle
& rect
, const int code
) const
599 int code2
= LVIR_BOUNDS
;
600 if ( code
== wxLIST_RECT_BOUNDS
)
602 else if ( code
== wxLIST_RECT_ICON
)
604 else if ( code
== wxLIST_RECT_LABEL
)
607 bool success
= (ListView_GetItemRect((HWND
) GetHWND(), (int) item
, &rect2
, code2
) != 0);
611 rect
.width
= rect2
.right
- rect2
.left
;
612 rect
.height
= rect2
.bottom
- rect2
.left
;
616 // Gets the item position
617 bool wxListCtrl::GetItemPosition(const long item
, wxPoint
& pos
) const
621 bool success
= (ListView_GetItemPosition((HWND
) GetHWND(), (int) item
, &pt
) != 0);
623 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
627 // Sets the item position.
628 bool wxListCtrl::SetItemPosition(const long item
, const wxPoint
& pos
)
630 return (ListView_SetItemPosition((HWND
) GetHWND(), (int) item
, pos
.x
, pos
.y
) != 0);
633 // Gets the number of items in the list control
634 int wxListCtrl::GetItemCount(void) const
636 return ListView_GetItemCount((HWND
) GetHWND());
639 // Retrieves the spacing between icons in pixels.
640 // If small is TRUE, gets the spacing for the small icon
641 // view, otherwise the large icon view.
642 int wxListCtrl::GetItemSpacing(bool isSmall
) const
644 return ListView_GetItemSpacing((HWND
) GetHWND(), (BOOL
) isSmall
);
647 // Gets the number of selected items in the list control
648 int wxListCtrl::GetSelectedItemCount(void) const
650 return ListView_GetSelectedCount((HWND
) GetHWND());
653 // Gets the text colour of the listview
654 wxColour
wxListCtrl::GetTextColour(void) const
656 COLORREF ref
= ListView_GetTextColor((HWND
) GetHWND());
657 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
661 // Sets the text colour of the listview
662 void wxListCtrl::SetTextColour(const wxColour
& col
)
664 ListView_SetTextColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Blue(), col
.Green()));
667 // Gets the index of the topmost visible item when in
668 // list or report view
669 long wxListCtrl::GetTopItem(void) const
671 return (long) ListView_GetTopIndex((HWND
) GetHWND());
674 // Searches for an item, starting from 'item'.
675 // 'geometry' is one of
676 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
677 // 'state' is a state bit flag, one or more of
678 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
679 // item can be -1 to find the first item that matches the
681 // Returns the item or -1 if unsuccessful.
682 long wxListCtrl::GetNextItem(const long item
, int geom
, int state
) const
686 if ( geom
== wxLIST_NEXT_ABOVE
)
688 if ( geom
== wxLIST_NEXT_ALL
)
690 if ( geom
== wxLIST_NEXT_BELOW
)
692 if ( geom
== wxLIST_NEXT_LEFT
)
693 flags
|= LVNI_TOLEFT
;
694 if ( geom
== wxLIST_NEXT_RIGHT
)
695 flags
|= LVNI_TORIGHT
;
697 if ( state
& wxLIST_STATE_CUT
)
699 if ( state
& wxLIST_STATE_DROPHILITED
)
700 flags
|= LVNI_DROPHILITED
;
701 if ( state
& wxLIST_STATE_FOCUSED
)
702 flags
|= LVNI_FOCUSED
;
703 if ( state
& wxLIST_STATE_SELECTED
)
704 flags
|= LVNI_SELECTED
;
706 return (long) ListView_GetNextItem((HWND
) GetHWND(), item
, flags
);
710 wxImageList
*wxListCtrl::GetImageList(const int which
) const
712 if ( which
== wxIMAGE_LIST_NORMAL
)
714 return m_imageListNormal
;
716 else if ( which
== wxIMAGE_LIST_SMALL
)
718 return m_imageListSmall
;
720 else if ( which
== wxIMAGE_LIST_STATE
)
722 return m_imageListState
;
727 void wxListCtrl::SetImageList(wxImageList
*imageList
, const int which
)
730 if ( which
== wxIMAGE_LIST_NORMAL
)
732 flags
= LVSIL_NORMAL
;
733 m_imageListNormal
= imageList
;
735 else if ( which
== wxIMAGE_LIST_SMALL
)
738 m_imageListSmall
= imageList
;
740 else if ( which
== wxIMAGE_LIST_STATE
)
743 m_imageListState
= imageList
;
745 ListView_SetImageList((HWND
) GetHWND(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
749 ////////////////////////////////////////////////////////////////////////////
751 // Arranges the items
752 bool wxListCtrl::Arrange(const int flag
)
755 if ( flag
== wxLIST_ALIGN_LEFT
)
756 code
= LVA_ALIGNLEFT
;
757 else if ( flag
== wxLIST_ALIGN_TOP
)
759 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
761 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
762 code
= LVA_SNAPTOGRID
;
764 return (ListView_Arrange((HWND
) GetHWND(), code
) != 0);
768 bool wxListCtrl::DeleteItem(const long item
)
770 return (ListView_DeleteItem((HWND
) GetHWND(), (int) item
) != 0);
774 bool wxListCtrl::DeleteAllItems(void)
776 return (ListView_DeleteAllItems((HWND
) GetHWND()) != 0);
780 bool wxListCtrl::DeleteAllColumns(void)
783 for ( i
= 0; i
< m_colCount
; i
++)
785 if (ListView_DeleteColumn((HWND
) GetHWND(), 0) != 0)
788 return (m_colCount
== 0);
792 bool wxListCtrl::DeleteColumn(const int col
)
794 bool success
= (ListView_DeleteColumn((HWND
) GetHWND(), col
) != 0);
796 if ( success
&& (m_colCount
> 0) )
801 // Clears items, and columns if there are any.
802 void wxListCtrl::ClearAll(void)
805 if ( m_colCount
> 0 )
810 wxTextCtrl
& wxListCtrl::Edit(const long item
)
812 HWND hWnd
= (HWND
) ListView_EditLabel((HWND
) GetHWND(), (int) item
);
813 m_textCtrl
.SetHWND((WXHWND
) hWnd
);
817 // Ensures this item is visible
818 bool wxListCtrl::EnsureVisible(const long item
)
820 return (ListView_EnsureVisible((HWND
) GetHWND(), (int) item
, FALSE
) != 0);
823 // Find an item whose label matches this string, starting from the item after 'start'
824 // or the beginning if 'start' is -1.
825 long wxListCtrl::FindItem(const long start
, const wxString
& str
, const bool partial
)
827 LV_FINDINFO findInfo
;
829 findInfo
.flags
= LVFI_STRING
;
831 findInfo
.flags
|= LVFI_STRING
;
832 findInfo
.psz
= WXSTRINGCAST str
;
834 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
837 // Find an item whose data matches this data, starting from the item after 'start'
838 // or the beginning if 'start' is -1.
839 long wxListCtrl::FindItem(const long start
, const long data
)
841 LV_FINDINFO findInfo
;
843 findInfo
.flags
= LVFI_PARAM
;
844 findInfo
.lParam
= data
;
846 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
849 // Find an item nearest this position in the specified direction, starting from
850 // the item after 'start' or the beginning if 'start' is -1.
851 long wxListCtrl::FindItem(const long start
, const wxPoint
& pt
, const int direction
)
853 LV_FINDINFO findInfo
;
855 findInfo
.flags
= LVFI_NEARESTXY
;
856 findInfo
.pt
.x
= pt
.x
;
857 findInfo
.pt
.y
= pt
.y
;
858 findInfo
.vkDirection
= VK_RIGHT
;
860 if ( direction
== wxLIST_FIND_UP
)
861 findInfo
.vkDirection
= VK_UP
;
862 else if ( direction
== wxLIST_FIND_DOWN
)
863 findInfo
.vkDirection
= VK_DOWN
;
864 else if ( direction
== wxLIST_FIND_LEFT
)
865 findInfo
.vkDirection
= VK_LEFT
;
866 else if ( direction
== wxLIST_FIND_RIGHT
)
867 findInfo
.vkDirection
= VK_RIGHT
;
869 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
872 // Determines which item (if any) is at the specified point,
873 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
874 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
876 LV_HITTESTINFO hitTestInfo
;
877 hitTestInfo
.pt
.x
= (int) point
.x
;
878 hitTestInfo
.pt
.y
= (int) point
.y
;
880 ListView_HitTest((HWND
) GetHWND(), & hitTestInfo
);
883 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
884 flags
|= wxLIST_HITTEST_ABOVE
;
885 if ( hitTestInfo
.flags
& LVHT_BELOW
)
886 flags
|= wxLIST_HITTEST_BELOW
;
887 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
888 flags
|= wxLIST_HITTEST_NOWHERE
;
889 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
890 flags
|= wxLIST_HITTEST_ONITEMICON
;
891 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
892 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
893 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
894 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
895 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
896 flags
|= wxLIST_HITTEST_TOLEFT
;
897 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
898 flags
|= wxLIST_HITTEST_TORIGHT
;
900 return (long) hitTestInfo
.iItem
;
903 // Inserts an item, returning the index of the new item if successful,
905 long wxListCtrl::InsertItem(wxListItem
& info
)
908 wxConvertToMSWListItem(this, info
, item
);
910 return (long) ListView_InsertItem((HWND
) GetHWND(), & item
);
913 long wxListCtrl::InsertItem(const long index
, const wxString
& label
)
917 info
.m_mask
= wxLIST_MASK_TEXT
;
918 info
.m_itemId
= index
;
919 return InsertItem(info
);
922 // Inserts an image item
923 long wxListCtrl::InsertItem(const long index
, const int imageIndex
)
926 info
.m_image
= imageIndex
;
927 info
.m_mask
= wxLIST_MASK_IMAGE
;
928 info
.m_itemId
= index
;
929 return InsertItem(info
);
932 // Inserts an image/string item
933 long wxListCtrl::InsertItem(const long index
, const wxString
& label
, const int imageIndex
)
936 info
.m_image
= imageIndex
;
938 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
939 info
.m_itemId
= index
;
940 return InsertItem(info
);
943 // For list view mode (only), inserts a column.
944 long wxListCtrl::InsertColumn(const long col
, wxListItem
& item
)
949 lvCol
.pszText
= NULL
;
951 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
953 lvCol
.mask
|= LVCF_TEXT
;
954 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
955 lvCol
.cchTextMax
= 0; // Ignored
957 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
959 lvCol
.mask
|= LVCF_FMT
;
961 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
962 lvCol
.fmt
= LVCFMT_LEFT
;
963 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
964 lvCol
.fmt
= LVCFMT_RIGHT
;
965 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
966 lvCol
.fmt
= LVCFMT_CENTER
;
969 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
971 lvCol
.mask
|= LVCF_WIDTH
;
972 lvCol
.cx
= item
.m_width
;
974 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
975 lvCol
.cx
= LVSCW_AUTOSIZE
;
976 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
977 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
979 lvCol
.mask
|= LVCF_SUBITEM
;
980 lvCol
.iSubItem
= col
;
982 bool success
= (ListView_InsertColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
988 long wxListCtrl::InsertColumn(const long col
, const wxString
& heading
, const int format
,
992 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
993 item
.m_text
= heading
;
996 item
.m_mask
|= wxLIST_MASK_WIDTH
;
997 item
.m_width
= width
;
999 item
.m_format
= format
;
1001 return InsertColumn(col
, item
);
1004 // Scrolls the list control. If in icon, small icon or report view mode,
1005 // x specifies the number of pixels to scroll. If in list view mode, x
1006 // specifies the number of columns to scroll.
1007 // If in icon, small icon or list view mode, y specifies the number of pixels
1008 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1009 bool wxListCtrl::ScrollList(const int dx
, const int dy
)
1011 return (ListView_Scroll((HWND
) GetHWND(), dx
, dy
) != 0);
1016 // fn is a function which takes 3 long arguments: item1, item2, data.
1017 // item1 is the long data associated with a first item (NOT the index).
1018 // item2 is the long data associated with a second item (NOT the index).
1019 // data is the same value as passed to SortItems.
1020 // The return value is a negative number if the first item should precede the second
1021 // item, a positive number of the second item should precede the first,
1022 // or zero if the two items are equivalent.
1024 // data is arbitrary data to be passed to the sort function.
1025 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1027 return (ListView_SortItems((HWND
) GetHWND(), (PFNLVCOMPARE
) fn
, data
) != 0);
1030 bool wxListCtrl::MSWCommand(const WXUINT cmd
, const WXWORD id
)
1032 if (cmd
== EN_UPDATE
)
1034 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1035 event
.SetEventObject( this );
1036 ProcessCommand(event
);
1039 else if (cmd
== EN_KILLFOCUS
)
1041 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1042 event
.SetEventObject( this );
1043 ProcessCommand(event
);
1049 bool wxListCtrl::MSWNotify(const WXWPARAM wParam
, const WXLPARAM lParam
)
1051 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1052 wxEventType eventType
= wxEVT_NULL
;
1053 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1054 switch ( hdr1
->code
)
1058 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1059 NM_LISTVIEW
*hdr
= (NM_LISTVIEW
*)lParam
;
1060 event
.m_itemIndex
= hdr
->iItem
;
1061 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1062 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1065 case LVN_BEGINLABELEDIT
:
1067 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1068 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1069 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1072 case LVN_BEGINRDRAG
:
1074 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1075 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1076 event
.m_itemIndex
= hdr
->iItem
;
1077 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1078 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1081 case LVN_COLUMNCLICK
:
1083 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1084 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1085 event
.m_itemIndex
= -1;
1086 event
.m_col
= hdr
->iSubItem
;
1089 case LVN_DELETEALLITEMS
:
1091 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1092 // NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1093 event
.m_itemIndex
= -1;
1096 case LVN_DELETEITEM
:
1098 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1099 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1100 event
.m_itemIndex
= hdr
->iItem
;
1103 case LVN_ENDLABELEDIT
:
1105 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1106 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1107 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1108 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1109 event
.m_cancelled
= TRUE
;
1112 case LVN_GETDISPINFO
:
1115 // TODO: some text buffering here, I think
1116 // TODO: API for getting Windows to retrieve values
1118 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1119 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1120 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1123 case LVN_INSERTITEM
:
1125 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1126 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1127 event
.m_itemIndex
= hdr
->iItem
;
1130 case LVN_ITEMCHANGED
:
1132 // This needs to be sent to wxListCtrl as a rather more
1133 // concrete event. For now, just detect a selection
1135 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1136 if ( (hdr
->uNewState
& LVIS_SELECTED
) && !(hdr
->uOldState
& LVIS_SELECTED
) )
1138 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1139 event
.m_itemIndex
= hdr
->iItem
;
1141 else if ( !(hdr
->uNewState
& LVIS_SELECTED
) && (hdr
->uOldState
& LVIS_SELECTED
) )
1143 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1144 event
.m_itemIndex
= hdr
->iItem
;
1152 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1153 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1154 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1157 case LVN_SETDISPINFO
:
1159 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1160 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1161 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1166 return wxControl::MSWNotify(wParam
, lParam
);
1170 event
.SetEventObject( this );
1171 event
.SetEventType(eventType
);
1173 if ( !GetEventHandler()->ProcessEvent(event
) )
1176 if (hdr1
->code
== LVN_GETDISPINFO
)
1178 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1179 if ( info
->item
.mask
& LVIF_TEXT
)
1181 if ( !event
.m_item
.m_text
.IsNull() )
1183 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1184 info
->item
.cchTextMax
= strlen(info
->item
.pszText
) + 1;
1187 // wxConvertToMSWListItem(this, event.m_item, info->item);
1193 char *wxListCtrl::AddPool(const wxString
& str
)
1195 // Remove the first element if 3 strings exist
1196 if ( m_stringPool
.Number() == 3 )
1198 wxNode
*node
= m_stringPool
.First();
1199 delete[] (char *)node
->Data();
1202 wxNode
*node
= m_stringPool
.Add((char *) (const char *)str
);
1203 return (char *)node
->Data();
1206 // List item structure
1207 wxListItem::wxListItem(void)
1217 m_format
= wxLIST_FORMAT_CENTRE
;
1221 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1223 info
.m_data
= lvItem
.lParam
;
1226 info
.m_stateMask
= 0;
1227 info
.m_itemId
= lvItem
.iItem
;
1229 long oldMask
= lvItem
.mask
;
1231 bool needText
= FALSE
;
1232 if (getFullInfo
!= 0)
1234 if ( lvItem
.mask
& LVIF_TEXT
)
1241 lvItem
.pszText
= new char[513];
1242 lvItem
.cchTextMax
= 512;
1244 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ;
1245 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1246 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
) ;
1249 if ( lvItem
.mask
& LVIF_STATE
)
1251 info
.m_mask
|= wxLIST_MASK_STATE
;
1253 if ( lvItem
.stateMask
& LVIS_CUT
)
1255 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1256 if ( lvItem
.state
& LVIS_CUT
)
1257 info
.m_state
|= wxLIST_STATE_CUT
;
1259 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1261 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1262 if ( lvItem
.state
& LVIS_DROPHILITED
)
1263 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1265 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1267 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1268 if ( lvItem
.state
& LVIS_FOCUSED
)
1269 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1271 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1273 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1274 if ( lvItem
.state
& LVIS_SELECTED
)
1275 info
.m_state
|= wxLIST_STATE_SELECTED
;
1279 if ( lvItem
.mask
& LVIF_TEXT
)
1281 info
.m_mask
|= wxLIST_MASK_TEXT
;
1282 info
.m_text
= lvItem
.pszText
;
1284 if ( lvItem
.mask
& LVIF_IMAGE
)
1286 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1287 info
.m_image
= lvItem
.iImage
;
1289 if ( lvItem
.mask
& LVIF_PARAM
)
1290 info
.m_mask
|= wxLIST_MASK_DATA
;
1291 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1292 info
.m_mask
|= wxLIST_SET_ITEM
;
1293 info
.m_col
= lvItem
.iSubItem
;
1298 delete[] lvItem
.pszText
;
1300 lvItem
.mask
= oldMask
;
1303 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1305 lvItem
.iItem
= (int) info
.m_itemId
;
1307 lvItem
.iImage
= info
.m_image
;
1308 lvItem
.lParam
= info
.m_data
;
1309 lvItem
.stateMask
= 0;
1312 lvItem
.iSubItem
= info
.m_col
;
1314 if (info
.m_mask
& wxLIST_MASK_STATE
)
1316 lvItem
.mask
|= LVIF_STATE
;
1317 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1319 lvItem
.stateMask
|= LVIS_CUT
;
1320 if (info
.m_state
& wxLIST_STATE_CUT
)
1321 lvItem
.state
|= LVIS_CUT
;
1323 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1325 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1326 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1327 lvItem
.state
|= LVIS_DROPHILITED
;
1329 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1331 lvItem
.stateMask
|= LVIS_FOCUSED
;
1332 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1333 lvItem
.state
|= LVIS_FOCUSED
;
1335 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1337 lvItem
.stateMask
|= LVIS_SELECTED
;
1338 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1339 lvItem
.state
|= LVIS_SELECTED
;
1343 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1345 lvItem
.mask
|= LVIF_TEXT
;
1346 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1348 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1352 lvItem
.pszText
= (char *) (const char *)info
.m_text
;
1353 if ( lvItem
.pszText
)
1354 lvItem
.cchTextMax
= info
.m_text
.Length();
1356 lvItem
.cchTextMax
= 0;
1359 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
1360 lvItem
.mask
|= LVIF_IMAGE
;
1361 if (info
.m_mask
& wxLIST_MASK_DATA
)
1362 lvItem
.mask
|= LVIF_PARAM
;
1366 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxCommandEvent
)
1368 wxListEvent::wxListEvent(wxEventType commandType
, int id
):
1369 wxCommandEvent(commandType
, id
)
1374 m_cancelled
= FALSE
;