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 // TOD: Should also have some further convenience functions
906 // which don't require setting a wxListItem object
907 long wxListCtrl::InsertItem(wxListItem
& info
)
910 wxConvertToMSWListItem(this, info
, item
);
912 return (long) ListView_InsertItem((HWND
) GetHWND(), & item
);
915 long wxListCtrl::InsertItem(const long index
, const wxString
& label
)
919 info
.m_mask
= wxLIST_MASK_TEXT
;
920 info
.m_itemId
= index
;
921 return InsertItem(info
);
924 // Inserts an image item
925 long wxListCtrl::InsertItem(const long index
, const int imageIndex
)
928 info
.m_image
= imageIndex
;
929 info
.m_mask
= wxLIST_MASK_IMAGE
;
930 info
.m_itemId
= index
;
931 return InsertItem(info
);
934 // Inserts an image/string item
935 long wxListCtrl::InsertItem(const long index
, const wxString
& label
, const int imageIndex
)
938 info
.m_image
= imageIndex
;
940 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
941 info
.m_itemId
= index
;
942 return InsertItem(info
);
945 // For list view mode (only), inserts a column.
946 long wxListCtrl::InsertColumn(const long col
, wxListItem
& item
)
951 lvCol
.pszText
= NULL
;
953 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
955 lvCol
.mask
|= LVCF_TEXT
;
956 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
957 lvCol
.cchTextMax
= 0; // Ignored
959 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
961 lvCol
.mask
|= LVCF_FMT
;
963 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
964 lvCol
.fmt
= LVCFMT_LEFT
;
965 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
966 lvCol
.fmt
= LVCFMT_RIGHT
;
967 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
968 lvCol
.fmt
= LVCFMT_CENTER
;
971 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
973 lvCol
.mask
|= LVCF_WIDTH
;
974 lvCol
.cx
= item
.m_width
;
976 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
977 lvCol
.cx
= LVSCW_AUTOSIZE
;
978 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
979 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
981 lvCol
.mask
|= LVCF_SUBITEM
;
982 lvCol
.iSubItem
= col
;
984 bool success
= (ListView_InsertColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
990 long wxListCtrl::InsertColumn(const long col
, const wxString
& heading
, const int format
,
994 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
995 item
.m_text
= heading
;
998 item
.m_mask
|= wxLIST_MASK_WIDTH
;
999 item
.m_width
= width
;
1001 item
.m_format
= format
;
1003 return InsertColumn(col
, item
);
1006 // Scrolls the list control. If in icon, small icon or report view mode,
1007 // x specifies the number of pixels to scroll. If in list view mode, x
1008 // specifies the number of columns to scroll.
1009 // If in icon, small icon or list view mode, y specifies the number of pixels
1010 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1011 bool wxListCtrl::ScrollList(const int dx
, const int dy
)
1013 return (ListView_Scroll((HWND
) GetHWND(), dx
, dy
) != 0);
1018 // fn is a function which takes 3 long arguments: item1, item2, data.
1019 // item1 is the long data associated with a first item (NOT the index).
1020 // item2 is the long data associated with a second item (NOT the index).
1021 // data is the same value as passed to SortItems.
1022 // The return value is a negative number if the first item should precede the second
1023 // item, a positive number of the second item should precede the first,
1024 // or zero if the two items are equivalent.
1026 // data is arbitrary data to be passed to the sort function.
1027 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1029 return (ListView_SortItems((HWND
) GetHWND(), (PFNLVCOMPARE
) fn
, data
) != 0);
1032 bool wxListCtrl::MSWCommand(const WXUINT cmd
, const WXWORD id
)
1034 if (cmd
== EN_UPDATE
)
1036 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1037 event
.SetEventObject( this );
1038 ProcessCommand(event
);
1041 else if (cmd
== EN_KILLFOCUS
)
1043 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1044 event
.SetEventObject( this );
1045 ProcessCommand(event
);
1051 bool wxListCtrl::MSWNotify(const WXWPARAM wParam
, const WXLPARAM lParam
)
1053 wxListEvent
event(0, m_windowId
);
1055 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1056 switch ( hdr1
->code
)
1060 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1061 NM_LISTVIEW
*hdr
= (NM_LISTVIEW
*)lParam
;
1062 event
.m_itemIndex
= hdr
->iItem
;
1063 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1064 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1067 case LVN_BEGINLABELEDIT
:
1069 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1070 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1071 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1074 case LVN_BEGINRDRAG
:
1076 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1077 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1078 event
.m_itemIndex
= hdr
->iItem
;
1079 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1080 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1083 case LVN_COLUMNCLICK
:
1085 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1086 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1087 event
.m_itemIndex
= -1;
1088 event
.m_col
= hdr
->iSubItem
;
1091 case LVN_DELETEALLITEMS
:
1093 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1094 // NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1095 event
.m_itemIndex
= -1;
1098 case LVN_DELETEITEM
:
1100 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1101 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1102 event
.m_itemIndex
= hdr
->iItem
;
1105 case LVN_ENDLABELEDIT
:
1107 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1108 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1109 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1110 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1111 event
.m_cancelled
= TRUE
;
1114 case LVN_GETDISPINFO
:
1117 // TODO: some text buffering here, I think
1118 // TODO: API for getting Windows to retrieve values
1120 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1121 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1122 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1125 case LVN_INSERTITEM
:
1127 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1128 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1129 event
.m_itemIndex
= hdr
->iItem
;
1132 case LVN_ITEMCHANGED
:
1134 // This needs to be sent to wxListCtrl as a rather more
1135 // concrete event. For now, just detect a selection
1137 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1138 if ( (hdr
->uNewState
& LVIS_SELECTED
) && !(hdr
->uOldState
& LVIS_SELECTED
) )
1140 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1141 event
.m_itemIndex
= hdr
->iItem
;
1143 else if ( !(hdr
->uNewState
& LVIS_SELECTED
) && (hdr
->uOldState
& LVIS_SELECTED
) )
1145 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1146 event
.m_itemIndex
= hdr
->iItem
;
1154 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1155 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1156 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1159 case LVN_SETDISPINFO
:
1161 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1162 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1163 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1168 return wxControl::MSWNotify(wParam
, lParam
);
1172 event
.SetEventObject( this );
1173 event
.SetEventType(eventType
);
1175 if ( !GetEventHandler()->ProcessEvent(event
) )
1178 if (hdr1
->code
== LVN_GETDISPINFO
)
1180 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1181 if ( info
->item
.mask
& LVIF_TEXT
)
1183 if ( !event
.m_item
.m_text
.IsNull() )
1185 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1186 info
->item
.cchTextMax
= strlen(info
->item
.pszText
) + 1;
1189 // wxConvertToMSWListItem(this, event.m_item, info->item);
1195 char *wxListCtrl::AddPool(const wxString
& str
)
1197 // Remove the first element if 3 strings exist
1198 if ( m_stringPool
.Number() == 3 )
1200 wxNode
*node
= m_stringPool
.First();
1201 delete[] (char *)node
->Data();
1204 wxNode
*node
= m_stringPool
.Add((char *) (const char *)str
);
1205 return (char *)node
->Data();
1208 // List item structure
1209 wxListItem::wxListItem(void)
1219 m_format
= wxLIST_FORMAT_CENTRE
;
1223 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1225 info
.m_data
= lvItem
.lParam
;
1228 info
.m_stateMask
= 0;
1229 info
.m_itemId
= lvItem
.iItem
;
1231 long oldMask
= lvItem
.mask
;
1233 bool needText
= FALSE
;
1234 if (getFullInfo
!= 0)
1236 if ( lvItem
.mask
& LVIF_TEXT
)
1243 lvItem
.pszText
= new char[513];
1244 lvItem
.cchTextMax
= 512;
1246 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ;
1247 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1248 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
) ;
1251 if ( lvItem
.mask
& LVIF_STATE
)
1253 info
.m_mask
|= wxLIST_MASK_STATE
;
1255 if ( lvItem
.stateMask
& LVIS_CUT
)
1257 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1258 if ( lvItem
.state
& LVIS_CUT
)
1259 info
.m_state
|= wxLIST_STATE_CUT
;
1261 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1263 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1264 if ( lvItem
.state
& LVIS_DROPHILITED
)
1265 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1267 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1269 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1270 if ( lvItem
.state
& LVIS_FOCUSED
)
1271 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1273 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1275 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1276 if ( lvItem
.state
& LVIS_SELECTED
)
1277 info
.m_state
|= wxLIST_STATE_SELECTED
;
1281 if ( lvItem
.mask
& LVIF_TEXT
)
1283 info
.m_mask
|= wxLIST_MASK_TEXT
;
1284 info
.m_text
= lvItem
.pszText
;
1286 if ( lvItem
.mask
& LVIF_IMAGE
)
1288 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1289 info
.m_image
= lvItem
.iImage
;
1291 if ( lvItem
.mask
& LVIF_PARAM
)
1292 info
.m_mask
|= wxLIST_MASK_DATA
;
1293 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1294 info
.m_mask
|= wxLIST_SET_ITEM
;
1295 info
.m_col
= lvItem
.iSubItem
;
1300 delete[] lvItem
.pszText
;
1302 lvItem
.mask
= oldMask
;
1305 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1307 lvItem
.iItem
= (int) info
.m_itemId
;
1309 lvItem
.iImage
= info
.m_image
;
1310 lvItem
.lParam
= info
.m_data
;
1311 lvItem
.stateMask
= 0;
1314 lvItem
.iSubItem
= info
.m_col
;
1316 if (info
.m_mask
& wxLIST_MASK_STATE
)
1318 lvItem
.mask
|= LVIF_STATE
;
1319 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1321 lvItem
.stateMask
|= LVIS_CUT
;
1322 if (info
.m_state
& wxLIST_STATE_CUT
)
1323 lvItem
.state
|= LVIS_CUT
;
1325 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1327 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1328 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1329 lvItem
.state
|= LVIS_DROPHILITED
;
1331 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1333 lvItem
.stateMask
|= LVIS_FOCUSED
;
1334 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1335 lvItem
.state
|= LVIS_FOCUSED
;
1337 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1339 lvItem
.stateMask
|= LVIS_SELECTED
;
1340 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1341 lvItem
.state
|= LVIS_SELECTED
;
1345 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1347 lvItem
.mask
|= LVIF_TEXT
;
1348 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1350 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1354 lvItem
.pszText
= (char *) (const char *)info
.m_text
;
1355 if ( lvItem
.pszText
)
1356 lvItem
.cchTextMax
= info
.m_text
.Length();
1358 lvItem
.cchTextMax
= 0;
1361 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
1362 lvItem
.mask
|= LVIF_IMAGE
;
1363 if (info
.m_mask
& wxLIST_MASK_DATA
)
1364 lvItem
.mask
|= LVIF_PARAM
;
1368 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxCommandEvent
)
1370 wxListEvent::wxListEvent(WXTYPE commandType
, int id
):
1371 wxCommandEvent(commandType
, id
)
1376 m_cancelled
= FALSE
;