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"
32 #include "wx/msw/private.h"
39 #include "wx/msw/gnuwin32/extra.h"
42 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
);
43 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
, HWND getFullInfo
= 0);
45 #if !USE_SHARED_LIBRARY
46 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
47 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
51 wxListCtrl::wxListCtrl(void)
53 m_imageListNormal
= NULL
;
54 m_imageListSmall
= NULL
;
55 m_imageListState
= NULL
;
61 bool wxListCtrl::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
62 long style
, const wxValidator
& validator
, const wxString
& name
)
64 m_imageListNormal
= NULL
;
65 m_imageListSmall
= NULL
;
66 m_imageListState
= NULL
;
70 SetValidator(validator
);
78 m_windowStyle
= style
;
91 m_windowId
= (id
== -1) ? NewControlId() : id
;
93 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
;
96 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
98 // Even with extended styles, need to combine with WS_BORDER
99 // for them to look right.
100 if (want3D
|| (m_windowStyle
& wxSIMPLE_BORDER
) || (m_windowStyle
& wxRAISED_BORDER
) ||
101 (m_windowStyle
& wxSUNKEN_BORDER
) || (m_windowStyle
& wxDOUBLE_BORDER
))
104 wstyle
|= LVS_SHAREIMAGELISTS
;
105 m_baseStyle
= wstyle
;
107 long oldStyle
= 0; // Dummy
108 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
110 // Create the ListView control.
111 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
116 (HWND
) parent
->GetHWND(),
122 wxLogError("Can't create list control window.");
127 wxSystemSettings settings
;
128 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
129 SetForegroundColour(parent
->GetDefaultForegroundColour());
131 if (parent
) parent
->AddChild(this);
133 SubclassWin((WXHWND
) m_hWnd
);
138 wxListCtrl::~wxListCtrl(void)
142 m_textCtrl
->UnsubclassWin();
143 m_textCtrl
->SetHWND(0);
149 // Add or remove a single window style
150 void wxListCtrl::SetSingleStyle(long style
, bool add
)
152 long flag
= GetWindowStyleFlag();
154 // Get rid of conflicting styles
157 if ( style
& wxLC_MASK_TYPE
)
158 flag
= flag
& ~wxLC_MASK_TYPE
;
159 if ( style
& wxLC_MASK_ALIGN
)
160 flag
= flag
& ~wxLC_MASK_ALIGN
;
161 if ( style
& wxLC_MASK_SORT
)
162 flag
= flag
& ~wxLC_MASK_SORT
;
178 m_windowStyle
= flag
;
183 // Set the whole window style
184 void wxListCtrl::SetWindowStyleFlag(long flag
)
186 m_windowStyle
= flag
;
191 void wxListCtrl::RecreateWindow(void)
196 long style
= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
197 style
|= m_baseStyle
;
198 // ::SetWindowLong((HWND) GetHWND(), GWL_STYLE, style);
200 // The following recreation of the window appears to be necessary
201 // because SetWindowLong doesn't seem to do it.
203 int x
, y
, width
, height
;
205 GetSize(&width
, &height
);
208 ::DestroyWindow((HWND
) GetHWND());
211 // Recreate the ListView control: unfortunately I can't
212 // make it work by using SetWindowLong.
214 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
215 HWND hWndListControl
= CreateWindowEx(exStyle
,
220 (HWND
) GetParent()->GetHWND(),
225 m_hWnd
= (WXHWND
) hWndListControl
;
226 SubclassWin((WXHWND
) m_hWnd
);
228 if ( m_imageListNormal
)
229 SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
230 if ( m_imageListSmall
)
231 SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
232 if ( m_imageListState
)
233 SetImageList(m_imageListState
, wxIMAGE_LIST_STATE
);
237 // Can be just a single style, or a bitlist
238 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
241 if ( style
& wxLC_ICON
)
243 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
244 oldStyle
-= LVS_SMALLICON
;
245 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
246 oldStyle
-= LVS_REPORT
;
247 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
248 oldStyle
-= LVS_LIST
;
252 if ( style
& wxLC_SMALL_ICON
)
254 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
255 oldStyle
-= LVS_ICON
;
256 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
257 oldStyle
-= LVS_REPORT
;
258 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
259 oldStyle
-= LVS_LIST
;
260 wstyle
|= LVS_SMALLICON
;
263 if ( style
& wxLC_LIST
)
265 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
266 oldStyle
-= LVS_ICON
;
267 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
268 oldStyle
-= LVS_REPORT
;
269 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
270 oldStyle
-= LVS_SMALLICON
;
274 if ( style
& wxLC_REPORT
)
276 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
277 oldStyle
-= LVS_ICON
;
278 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
279 oldStyle
-= LVS_LIST
;
280 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
281 oldStyle
-= LVS_SMALLICON
;
282 wstyle
|= LVS_REPORT
;
285 if ( style
& wxLC_ALIGN_LEFT
)
287 if ( oldStyle
& LVS_ALIGNTOP
)
288 oldStyle
-= LVS_ALIGNTOP
;
289 wstyle
|= LVS_ALIGNLEFT
;
292 if ( style
& wxLC_ALIGN_TOP
)
294 if ( oldStyle
& LVS_ALIGNLEFT
)
295 oldStyle
-= LVS_ALIGNLEFT
;
296 wstyle
|= LVS_ALIGNTOP
;
299 if ( style
& wxLC_AUTOARRANGE
)
300 wstyle
|= LVS_AUTOARRANGE
;
302 // Apparently, no such style (documentation wrong?)
304 if ( style & wxLC_BUTTON )
305 wstyle |= LVS_BUTTON;
308 if ( style
& wxLC_NO_SORT_HEADER
)
309 wstyle
|= LVS_NOSORTHEADER
;
311 if ( style
& wxLC_NO_HEADER
)
312 wstyle
|= LVS_NOCOLUMNHEADER
;
314 if ( style
& wxLC_EDIT_LABELS
)
315 wstyle
|= LVS_EDITLABELS
;
317 if ( style
& wxLC_SINGLE_SEL
)
318 wstyle
|= LVS_SINGLESEL
;
320 if ( style
& wxLC_SORT_ASCENDING
)
322 if ( oldStyle
& LVS_SORTDESCENDING
)
323 oldStyle
-= LVS_SORTDESCENDING
;
324 wstyle
|= LVS_SORTASCENDING
;
327 if ( style
& wxLC_SORT_DESCENDING
)
329 if ( oldStyle
& LVS_SORTASCENDING
)
330 oldStyle
-= LVS_SORTASCENDING
;
331 wstyle
|= LVS_SORTDESCENDING
;
337 // Sets the background colour (GetBackgroundColour already implicit in
339 void wxListCtrl::SetBackgroundColour(const wxColour
& col
)
341 wxWindow::SetBackgroundColour(col
);
343 ListView_SetBkColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
346 // Gets information about this column
347 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
352 lvCol
.pszText
= NULL
;
354 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
356 lvCol
.mask
|= LVCF_TEXT
;
357 lvCol
.pszText
= new char[513];
358 lvCol
.cchTextMax
= 512;
361 bool success
= (ListView_GetColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
363 // item.m_subItem = lvCol.iSubItem;
364 item
.m_width
= lvCol
.cx
;
366 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
368 item
.m_text
= lvCol
.pszText
;
369 delete[] lvCol
.pszText
;
372 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
374 if (lvCol
.fmt
== LVCFMT_LEFT
)
375 item
.m_format
= wxLIST_FORMAT_LEFT
;
376 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
377 item
.m_format
= wxLIST_FORMAT_RIGHT
;
378 else if (lvCol
.fmt
== LVCFMT_CENTER
)
379 item
.m_format
= wxLIST_FORMAT_CENTRE
;
385 // Sets information about this column
386 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
391 lvCol
.pszText
= NULL
;
393 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
395 lvCol
.mask
|= LVCF_TEXT
;
396 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
397 lvCol
.cchTextMax
= 0; // Ignored
399 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
401 lvCol
.mask
|= LVCF_FMT
;
403 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
404 lvCol
.fmt
= LVCFMT_LEFT
;
405 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
406 lvCol
.fmt
= LVCFMT_RIGHT
;
407 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
408 lvCol
.fmt
= LVCFMT_CENTER
;
411 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
413 lvCol
.mask
|= LVCF_WIDTH
;
414 lvCol
.cx
= item
.m_width
;
416 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
417 lvCol
.cx
= LVSCW_AUTOSIZE
;
418 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
419 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
421 lvCol
.mask
|= LVCF_SUBITEM
;
422 lvCol
.iSubItem
= col
;
423 return (ListView_SetColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
426 // Gets the column width
427 int wxListCtrl::GetColumnWidth(int col
) const
429 return ListView_GetColumnWidth((HWND
) GetHWND(), col
);
432 // Sets the column width
433 bool wxListCtrl::SetColumnWidth(int col
, int width
)
436 if ( m_windowStyle
& wxLC_LIST
)
440 if ( width2
== wxLIST_AUTOSIZE
)
441 width2
= LVSCW_AUTOSIZE
;
442 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
443 width2
= LVSCW_AUTOSIZE_USEHEADER
;
445 return (ListView_SetColumnWidth((HWND
) GetHWND(), col2
, width2
) != 0);
448 // Gets the number of items that can fit vertically in the
449 // visible area of the list control (list or report view)
450 // or the total number of items in the list control (icon
451 // or small icon view)
452 int wxListCtrl::GetCountPerPage(void) const
454 return ListView_GetCountPerPage((HWND
) GetHWND());
457 // Gets the edit control for editing labels.
458 wxTextCtrl
* wxListCtrl::GetEditControl(void) const
463 // Gets information about the item
464 bool wxListCtrl::GetItem(wxListItem
& info
) const
467 lvItem
.pszText
= NULL
;
468 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
470 lvItem
.pszText
= new char[513];
471 lvItem
.cchTextMax
= 512;
473 bool success
= (::SendMessage((HWND
) GetHWND(), LVM_GETITEM
, 0, (LPARAM
)& lvItem
) != 0);
478 delete[] lvItem
.pszText
;
483 wxConvertFromMSWListItem(this, info
, lvItem
);
486 delete[] lvItem
.pszText
;
491 // Sets information about the item
492 bool wxListCtrl::SetItem(wxListItem
& info
)
495 wxConvertToMSWListItem(this, info
, item
);
497 return (ListView_SetItem((HWND
) GetHWND(), &item
) != 0);
500 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
504 info
.m_mask
= wxLIST_MASK_TEXT
;
505 info
.m_itemId
= index
;
509 info
.m_image
= imageId
;
510 info
.m_mask
|= wxLIST_MASK_IMAGE
;
512 return SetItem(info
);
516 // Gets the item state
517 int wxListCtrl::GetItemState(long item
, long stateMask
) const
521 info
.m_mask
= wxLIST_MASK_STATE
;
522 info
.m_stateMask
= stateMask
;
523 info
.m_itemId
= item
;
531 // Sets the item state
532 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
536 info
.m_mask
= wxLIST_MASK_STATE
;
537 info
.m_state
= state
;
538 info
.m_stateMask
= stateMask
;
539 info
.m_itemId
= item
;
541 return SetItem(info
);
544 // Sets the item image
545 bool wxListCtrl::SetItemImage(long item
, int image
, int selImage
)
549 info
.m_mask
= wxLIST_MASK_IMAGE
;
550 info
.m_image
= image
;
551 info
.m_itemId
= item
;
553 return SetItem(info
);
556 // Gets the item text
557 wxString
wxListCtrl::GetItemText(long item
) const
561 info
.m_mask
= wxLIST_MASK_TEXT
;
562 info
.m_itemId
= item
;
569 // Sets the item text
570 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
574 info
.m_mask
= wxLIST_MASK_TEXT
;
575 info
.m_itemId
= item
;
581 // Gets the item data
582 long wxListCtrl::GetItemData(long item
) const
586 info
.m_mask
= wxLIST_MASK_DATA
;
587 info
.m_itemId
= item
;
594 // Sets the item data
595 bool wxListCtrl::SetItemData(long item
, long data
)
599 info
.m_mask
= wxLIST_MASK_DATA
;
600 info
.m_itemId
= item
;
603 return SetItem(info
);
606 // Gets the item rectangle
607 bool wxListCtrl::GetItemRect(long item
, wxRectangle
& rect
, int code
) const
611 int code2
= LVIR_BOUNDS
;
612 if ( code
== wxLIST_RECT_BOUNDS
)
614 else if ( code
== wxLIST_RECT_ICON
)
616 else if ( code
== wxLIST_RECT_LABEL
)
619 bool success
= (ListView_GetItemRect((HWND
) GetHWND(), (int) item
, &rect2
, code2
) != 0);
623 rect
.width
= rect2
.right
- rect2
.left
;
624 rect
.height
= rect2
.bottom
- rect2
.left
;
628 // Gets the item position
629 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
633 bool success
= (ListView_GetItemPosition((HWND
) GetHWND(), (int) item
, &pt
) != 0);
635 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
639 // Sets the item position.
640 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
642 return (ListView_SetItemPosition((HWND
) GetHWND(), (int) item
, pos
.x
, pos
.y
) != 0);
645 // Gets the number of items in the list control
646 int wxListCtrl::GetItemCount(void) const
648 return ListView_GetItemCount((HWND
) GetHWND());
651 // Retrieves the spacing between icons in pixels.
652 // If small is TRUE, gets the spacing for the small icon
653 // view, otherwise the large icon view.
654 int wxListCtrl::GetItemSpacing(bool isSmall
) const
656 return ListView_GetItemSpacing((HWND
) GetHWND(), (BOOL
) isSmall
);
659 // Gets the number of selected items in the list control
660 int wxListCtrl::GetSelectedItemCount(void) const
662 return ListView_GetSelectedCount((HWND
) GetHWND());
665 // Gets the text colour of the listview
666 wxColour
wxListCtrl::GetTextColour(void) const
668 COLORREF ref
= ListView_GetTextColor((HWND
) GetHWND());
669 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
673 // Sets the text colour of the listview
674 void wxListCtrl::SetTextColour(const wxColour
& col
)
676 ListView_SetTextColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Blue(), col
.Green()));
679 // Gets the index of the topmost visible item when in
680 // list or report view
681 long wxListCtrl::GetTopItem(void) const
683 return (long) ListView_GetTopIndex((HWND
) GetHWND());
686 // Searches for an item, starting from 'item'.
687 // 'geometry' is one of
688 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
689 // 'state' is a state bit flag, one or more of
690 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
691 // item can be -1 to find the first item that matches the
693 // Returns the item or -1 if unsuccessful.
694 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
698 if ( geom
== wxLIST_NEXT_ABOVE
)
700 if ( geom
== wxLIST_NEXT_ALL
)
702 if ( geom
== wxLIST_NEXT_BELOW
)
704 if ( geom
== wxLIST_NEXT_LEFT
)
705 flags
|= LVNI_TOLEFT
;
706 if ( geom
== wxLIST_NEXT_RIGHT
)
707 flags
|= LVNI_TORIGHT
;
709 if ( state
& wxLIST_STATE_CUT
)
711 if ( state
& wxLIST_STATE_DROPHILITED
)
712 flags
|= LVNI_DROPHILITED
;
713 if ( state
& wxLIST_STATE_FOCUSED
)
714 flags
|= LVNI_FOCUSED
;
715 if ( state
& wxLIST_STATE_SELECTED
)
716 flags
|= LVNI_SELECTED
;
718 return (long) ListView_GetNextItem((HWND
) GetHWND(), item
, flags
);
722 wxImageList
*wxListCtrl::GetImageList(int which
) const
724 if ( which
== wxIMAGE_LIST_NORMAL
)
726 return m_imageListNormal
;
728 else if ( which
== wxIMAGE_LIST_SMALL
)
730 return m_imageListSmall
;
732 else if ( which
== wxIMAGE_LIST_STATE
)
734 return m_imageListState
;
739 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
742 if ( which
== wxIMAGE_LIST_NORMAL
)
744 flags
= LVSIL_NORMAL
;
745 m_imageListNormal
= imageList
;
747 else if ( which
== wxIMAGE_LIST_SMALL
)
750 m_imageListSmall
= imageList
;
752 else if ( which
== wxIMAGE_LIST_STATE
)
755 m_imageListState
= imageList
;
757 ListView_SetImageList((HWND
) GetHWND(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
761 ////////////////////////////////////////////////////////////////////////////
763 // Arranges the items
764 bool wxListCtrl::Arrange(int flag
)
767 if ( flag
== wxLIST_ALIGN_LEFT
)
768 code
= LVA_ALIGNLEFT
;
769 else if ( flag
== wxLIST_ALIGN_TOP
)
771 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
773 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
774 code
= LVA_SNAPTOGRID
;
776 return (ListView_Arrange((HWND
) GetHWND(), code
) != 0);
780 bool wxListCtrl::DeleteItem(long item
)
782 return (ListView_DeleteItem((HWND
) GetHWND(), (int) item
) != 0);
786 bool wxListCtrl::DeleteAllItems(void)
788 return (ListView_DeleteAllItems((HWND
) GetHWND()) != 0);
792 bool wxListCtrl::DeleteAllColumns(void)
795 for ( i
= 0; i
< m_colCount
; i
++)
797 if (ListView_DeleteColumn((HWND
) GetHWND(), 0) != 0)
800 return (m_colCount
== 0);
804 bool wxListCtrl::DeleteColumn(int col
)
806 bool success
= (ListView_DeleteColumn((HWND
) GetHWND(), col
) != 0);
808 if ( success
&& (m_colCount
> 0) )
813 // Clears items, and columns if there are any.
814 void wxListCtrl::ClearAll(void)
817 if ( m_colCount
> 0 )
821 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
823 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
825 HWND hWnd
= (HWND
) ListView_EditLabel((HWND
) GetHWND(), item
);
829 m_textCtrl
->UnsubclassWin();
830 m_textCtrl
->SetHWND(0);
835 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
836 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
837 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
842 // End label editing, optionally cancelling the edit
843 bool wxListCtrl::EndEditLabel(bool cancel
)
847 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
849 bool success = (ListView_EndEditLabelNow((HWND) GetHWND(), cancel) != 0);
853 m_textCtrl->UnsubclassWin();
854 m_textCtrl->SetHWND(0);
864 // Ensures this item is visible
865 bool wxListCtrl::EnsureVisible(long item
)
867 return (ListView_EnsureVisible((HWND
) GetHWND(), (int) item
, FALSE
) != 0);
870 // Find an item whose label matches this string, starting from the item after 'start'
871 // or the beginning if 'start' is -1.
872 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
874 LV_FINDINFO findInfo
;
876 findInfo
.flags
= LVFI_STRING
;
878 findInfo
.flags
|= LVFI_STRING
;
879 findInfo
.psz
= WXSTRINGCAST str
;
881 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
884 // Find an item whose data matches this data, starting from the item after 'start'
885 // or the beginning if 'start' is -1.
886 long wxListCtrl::FindItem(long start
, long data
)
888 LV_FINDINFO findInfo
;
890 findInfo
.flags
= LVFI_PARAM
;
891 findInfo
.lParam
= data
;
893 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
896 // Find an item nearest this position in the specified direction, starting from
897 // the item after 'start' or the beginning if 'start' is -1.
898 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
900 LV_FINDINFO findInfo
;
902 findInfo
.flags
= LVFI_NEARESTXY
;
903 findInfo
.pt
.x
= pt
.x
;
904 findInfo
.pt
.y
= pt
.y
;
905 findInfo
.vkDirection
= VK_RIGHT
;
907 if ( direction
== wxLIST_FIND_UP
)
908 findInfo
.vkDirection
= VK_UP
;
909 else if ( direction
== wxLIST_FIND_DOWN
)
910 findInfo
.vkDirection
= VK_DOWN
;
911 else if ( direction
== wxLIST_FIND_LEFT
)
912 findInfo
.vkDirection
= VK_LEFT
;
913 else if ( direction
== wxLIST_FIND_RIGHT
)
914 findInfo
.vkDirection
= VK_RIGHT
;
916 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
919 // Determines which item (if any) is at the specified point,
920 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
921 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
923 LV_HITTESTINFO hitTestInfo
;
924 hitTestInfo
.pt
.x
= (int) point
.x
;
925 hitTestInfo
.pt
.y
= (int) point
.y
;
927 ListView_HitTest((HWND
) GetHWND(), & hitTestInfo
);
930 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
931 flags
|= wxLIST_HITTEST_ABOVE
;
932 if ( hitTestInfo
.flags
& LVHT_BELOW
)
933 flags
|= wxLIST_HITTEST_BELOW
;
934 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
935 flags
|= wxLIST_HITTEST_NOWHERE
;
936 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
937 flags
|= wxLIST_HITTEST_ONITEMICON
;
938 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
939 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
940 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
941 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
942 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
943 flags
|= wxLIST_HITTEST_TOLEFT
;
944 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
945 flags
|= wxLIST_HITTEST_TORIGHT
;
947 return (long) hitTestInfo
.iItem
;
950 // Inserts an item, returning the index of the new item if successful,
952 long wxListCtrl::InsertItem(wxListItem
& info
)
955 wxConvertToMSWListItem(this, info
, item
);
957 return (long) ListView_InsertItem((HWND
) GetHWND(), & item
);
960 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
964 info
.m_mask
= wxLIST_MASK_TEXT
;
965 info
.m_itemId
= index
;
966 return InsertItem(info
);
969 // Inserts an image item
970 long wxListCtrl::InsertItem(long index
, int imageIndex
)
973 info
.m_image
= imageIndex
;
974 info
.m_mask
= wxLIST_MASK_IMAGE
;
975 info
.m_itemId
= index
;
976 return InsertItem(info
);
979 // Inserts an image/string item
980 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
983 info
.m_image
= imageIndex
;
985 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
986 info
.m_itemId
= index
;
987 return InsertItem(info
);
990 // For list view mode (only), inserts a column.
991 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
996 lvCol
.pszText
= NULL
;
998 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1000 lvCol
.mask
|= LVCF_TEXT
;
1001 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1002 lvCol
.cchTextMax
= 0; // Ignored
1004 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1006 lvCol
.mask
|= LVCF_FMT
;
1008 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1009 lvCol
.fmt
= LVCFMT_LEFT
;
1010 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1011 lvCol
.fmt
= LVCFMT_RIGHT
;
1012 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1013 lvCol
.fmt
= LVCFMT_CENTER
;
1016 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1018 lvCol
.mask
|= LVCF_WIDTH
;
1019 lvCol
.cx
= item
.m_width
;
1021 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
1022 lvCol
.cx
= LVSCW_AUTOSIZE
;
1023 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
1024 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1026 lvCol
.mask
|= LVCF_SUBITEM
;
1027 lvCol
.iSubItem
= col
;
1029 bool success
= (ListView_InsertColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
1035 long wxListCtrl::InsertColumn(long col
, const wxString
& heading
, int format
,
1039 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1040 item
.m_text
= heading
;
1043 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1044 item
.m_width
= width
;
1046 item
.m_format
= format
;
1048 return InsertColumn(col
, item
);
1051 // Scrolls the list control. If in icon, small icon or report view mode,
1052 // x specifies the number of pixels to scroll. If in list view mode, x
1053 // specifies the number of columns to scroll.
1054 // If in icon, small icon or list view mode, y specifies the number of pixels
1055 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1056 bool wxListCtrl::ScrollList(int dx
, int dy
)
1058 return (ListView_Scroll((HWND
) GetHWND(), dx
, dy
) != 0);
1063 // fn is a function which takes 3 long arguments: item1, item2, data.
1064 // item1 is the long data associated with a first item (NOT the index).
1065 // item2 is the long data associated with a second item (NOT the index).
1066 // data is the same value as passed to SortItems.
1067 // The return value is a negative number if the first item should precede the second
1068 // item, a positive number of the second item should precede the first,
1069 // or zero if the two items are equivalent.
1071 // data is arbitrary data to be passed to the sort function.
1072 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1074 return (ListView_SortItems((HWND
) GetHWND(), (PFNLVCOMPARE
) fn
, data
) != 0);
1077 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1079 if (cmd
== EN_UPDATE
)
1081 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1082 event
.SetEventObject( this );
1083 ProcessCommand(event
);
1086 else if (cmd
== EN_KILLFOCUS
)
1088 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1089 event
.SetEventObject( this );
1090 ProcessCommand(event
);
1096 bool wxListCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
)
1098 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1099 wxEventType eventType
= wxEVT_NULL
;
1100 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1101 switch ( hdr1
->code
)
1105 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1106 NM_LISTVIEW
*hdr
= (NM_LISTVIEW
*)lParam
;
1107 event
.m_itemIndex
= hdr
->iItem
;
1108 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1109 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1112 case LVN_BEGINLABELEDIT
:
1114 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1115 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1116 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1119 case LVN_BEGINRDRAG
:
1121 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1122 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1123 event
.m_itemIndex
= hdr
->iItem
;
1124 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1125 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1128 case LVN_COLUMNCLICK
:
1130 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1131 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1132 event
.m_itemIndex
= -1;
1133 event
.m_col
= hdr
->iSubItem
;
1136 case LVN_DELETEALLITEMS
:
1138 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1139 // NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1140 event
.m_itemIndex
= -1;
1143 case LVN_DELETEITEM
:
1145 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1146 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1147 event
.m_itemIndex
= hdr
->iItem
;
1150 case LVN_ENDLABELEDIT
:
1152 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1153 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1154 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1155 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1156 event
.m_cancelled
= TRUE
;
1159 case LVN_GETDISPINFO
:
1162 // TODO: some text buffering here, I think
1163 // TODO: API for getting Windows to retrieve values
1165 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1166 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1167 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1170 case LVN_INSERTITEM
:
1172 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1173 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1174 event
.m_itemIndex
= hdr
->iItem
;
1177 case LVN_ITEMCHANGED
:
1179 // This needs to be sent to wxListCtrl as a rather more
1180 // concrete event. For now, just detect a selection
1182 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1183 if ( (hdr
->uNewState
& LVIS_SELECTED
) && !(hdr
->uOldState
& LVIS_SELECTED
) )
1185 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1186 event
.m_itemIndex
= hdr
->iItem
;
1188 else if ( !(hdr
->uNewState
& LVIS_SELECTED
) && (hdr
->uOldState
& LVIS_SELECTED
) )
1190 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1191 event
.m_itemIndex
= hdr
->iItem
;
1199 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1200 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1201 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1204 case LVN_SETDISPINFO
:
1206 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1207 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1208 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1213 return wxControl::MSWNotify(wParam
, lParam
);
1217 event
.SetEventObject( this );
1218 event
.SetEventType(eventType
);
1220 if ( !GetEventHandler()->ProcessEvent(event
) )
1223 if (hdr1
->code
== LVN_GETDISPINFO
)
1225 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1226 if ( info
->item
.mask
& LVIF_TEXT
)
1228 if ( !event
.m_item
.m_text
.IsNull() )
1230 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1231 info
->item
.cchTextMax
= strlen(info
->item
.pszText
) + 1;
1234 // wxConvertToMSWListItem(this, event.m_item, info->item);
1240 char *wxListCtrl::AddPool(const wxString
& str
)
1242 // Remove the first element if 3 strings exist
1243 if ( m_stringPool
.Number() == 3 )
1245 wxNode
*node
= m_stringPool
.First();
1246 delete[] (char *)node
->Data();
1249 wxNode
*node
= m_stringPool
.Add((char *) (const char *)str
);
1250 return (char *)node
->Data();
1253 // List item structure
1254 wxListItem::wxListItem(void)
1264 m_format
= wxLIST_FORMAT_CENTRE
;
1268 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1270 info
.m_data
= lvItem
.lParam
;
1273 info
.m_stateMask
= 0;
1274 info
.m_itemId
= lvItem
.iItem
;
1276 long oldMask
= lvItem
.mask
;
1278 bool needText
= FALSE
;
1279 if (getFullInfo
!= 0)
1281 if ( lvItem
.mask
& LVIF_TEXT
)
1288 lvItem
.pszText
= new char[513];
1289 lvItem
.cchTextMax
= 512;
1291 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ;
1292 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1293 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
) ;
1296 if ( lvItem
.mask
& LVIF_STATE
)
1298 info
.m_mask
|= wxLIST_MASK_STATE
;
1300 if ( lvItem
.stateMask
& LVIS_CUT
)
1302 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1303 if ( lvItem
.state
& LVIS_CUT
)
1304 info
.m_state
|= wxLIST_STATE_CUT
;
1306 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1308 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1309 if ( lvItem
.state
& LVIS_DROPHILITED
)
1310 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1312 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1314 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1315 if ( lvItem
.state
& LVIS_FOCUSED
)
1316 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1318 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1320 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1321 if ( lvItem
.state
& LVIS_SELECTED
)
1322 info
.m_state
|= wxLIST_STATE_SELECTED
;
1326 if ( lvItem
.mask
& LVIF_TEXT
)
1328 info
.m_mask
|= wxLIST_MASK_TEXT
;
1329 info
.m_text
= lvItem
.pszText
;
1331 if ( lvItem
.mask
& LVIF_IMAGE
)
1333 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1334 info
.m_image
= lvItem
.iImage
;
1336 if ( lvItem
.mask
& LVIF_PARAM
)
1337 info
.m_mask
|= wxLIST_MASK_DATA
;
1338 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1339 info
.m_mask
|= wxLIST_SET_ITEM
;
1340 info
.m_col
= lvItem
.iSubItem
;
1345 delete[] lvItem
.pszText
;
1347 lvItem
.mask
= oldMask
;
1350 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1352 lvItem
.iItem
= (int) info
.m_itemId
;
1354 lvItem
.iImage
= info
.m_image
;
1355 lvItem
.lParam
= info
.m_data
;
1356 lvItem
.stateMask
= 0;
1359 lvItem
.iSubItem
= info
.m_col
;
1361 if (info
.m_mask
& wxLIST_MASK_STATE
)
1363 lvItem
.mask
|= LVIF_STATE
;
1364 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1366 lvItem
.stateMask
|= LVIS_CUT
;
1367 if (info
.m_state
& wxLIST_STATE_CUT
)
1368 lvItem
.state
|= LVIS_CUT
;
1370 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1372 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1373 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1374 lvItem
.state
|= LVIS_DROPHILITED
;
1376 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1378 lvItem
.stateMask
|= LVIS_FOCUSED
;
1379 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1380 lvItem
.state
|= LVIS_FOCUSED
;
1382 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1384 lvItem
.stateMask
|= LVIS_SELECTED
;
1385 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1386 lvItem
.state
|= LVIS_SELECTED
;
1390 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1392 lvItem
.mask
|= LVIF_TEXT
;
1393 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1395 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1399 lvItem
.pszText
= (char *) (const char *)info
.m_text
;
1400 if ( lvItem
.pszText
)
1401 lvItem
.cchTextMax
= info
.m_text
.Length();
1403 lvItem
.cchTextMax
= 0;
1406 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
1407 lvItem
.mask
|= LVIF_IMAGE
;
1408 if (info
.m_mask
& wxLIST_MASK_DATA
)
1409 lvItem
.mask
|= LVIF_PARAM
;
1413 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxCommandEvent
)
1415 wxListEvent::wxListEvent(wxEventType commandType
, int id
):
1416 wxCommandEvent(commandType
, id
)
1421 m_cancelled
= FALSE
;