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
|| wxStyleHasBorder(m_windowStyle
) )
103 wstyle
|= LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
104 m_baseStyle
= wstyle
;
106 long oldStyle
= 0; // Dummy
107 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
109 // Create the ListView control.
110 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
115 (HWND
) parent
->GetHWND(),
121 wxLogError("Can't create list control window.");
126 wxSystemSettings settings
;
127 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
128 SetForegroundColour(parent
->GetForegroundColour());
130 if (parent
) parent
->AddChild(this);
132 SubclassWin((WXHWND
) m_hWnd
);
137 wxListCtrl::~wxListCtrl(void)
141 m_textCtrl
->UnsubclassWin();
142 m_textCtrl
->SetHWND(0);
148 // Add or remove a single window style
149 void wxListCtrl::SetSingleStyle(long style
, bool add
)
151 long flag
= GetWindowStyleFlag();
153 // Get rid of conflicting styles
156 if ( style
& wxLC_MASK_TYPE
)
157 flag
= flag
& ~wxLC_MASK_TYPE
;
158 if ( style
& wxLC_MASK_ALIGN
)
159 flag
= flag
& ~wxLC_MASK_ALIGN
;
160 if ( style
& wxLC_MASK_SORT
)
161 flag
= flag
& ~wxLC_MASK_SORT
;
177 m_windowStyle
= flag
;
182 // Set the whole window style
183 void wxListCtrl::SetWindowStyleFlag(long flag
)
185 m_windowStyle
= flag
;
190 void wxListCtrl::RecreateWindow(void)
195 long style
= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
196 style
|= m_baseStyle
;
197 // ::SetWindowLong((HWND) GetHWND(), GWL_STYLE, style);
199 // The following recreation of the window appears to be necessary
200 // because SetWindowLong doesn't seem to do it.
202 int x
, y
, width
, height
;
204 GetSize(&width
, &height
);
207 ::DestroyWindow((HWND
) GetHWND());
210 // Recreate the ListView control: unfortunately I can't
211 // make it work by using SetWindowLong.
213 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
214 HWND hWndListControl
= CreateWindowEx(exStyle
,
219 (HWND
) GetParent()->GetHWND(),
224 m_hWnd
= (WXHWND
) hWndListControl
;
225 SubclassWin((WXHWND
) m_hWnd
);
227 if ( m_imageListNormal
)
228 SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
229 if ( m_imageListSmall
)
230 SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
231 if ( m_imageListState
)
232 SetImageList(m_imageListState
, wxIMAGE_LIST_STATE
);
236 // Can be just a single style, or a bitlist
237 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
240 if ( style
& wxLC_ICON
)
242 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
243 oldStyle
-= LVS_SMALLICON
;
244 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
245 oldStyle
-= LVS_REPORT
;
246 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
247 oldStyle
-= LVS_LIST
;
251 if ( style
& wxLC_SMALL_ICON
)
253 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
254 oldStyle
-= LVS_ICON
;
255 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
256 oldStyle
-= LVS_REPORT
;
257 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
258 oldStyle
-= LVS_LIST
;
259 wstyle
|= LVS_SMALLICON
;
262 if ( style
& wxLC_LIST
)
264 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
265 oldStyle
-= LVS_ICON
;
266 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
267 oldStyle
-= LVS_REPORT
;
268 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
269 oldStyle
-= LVS_SMALLICON
;
273 if ( style
& wxLC_REPORT
)
275 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
276 oldStyle
-= LVS_ICON
;
277 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
278 oldStyle
-= LVS_LIST
;
279 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
280 oldStyle
-= LVS_SMALLICON
;
281 wstyle
|= LVS_REPORT
;
284 if ( style
& wxLC_ALIGN_LEFT
)
286 if ( oldStyle
& LVS_ALIGNTOP
)
287 oldStyle
-= LVS_ALIGNTOP
;
288 wstyle
|= LVS_ALIGNLEFT
;
291 if ( style
& wxLC_ALIGN_TOP
)
293 if ( oldStyle
& LVS_ALIGNLEFT
)
294 oldStyle
-= LVS_ALIGNLEFT
;
295 wstyle
|= LVS_ALIGNTOP
;
298 if ( style
& wxLC_AUTOARRANGE
)
299 wstyle
|= LVS_AUTOARRANGE
;
301 // Apparently, no such style (documentation wrong?)
303 if ( style & wxLC_BUTTON )
304 wstyle |= LVS_BUTTON;
307 if ( style
& wxLC_NO_SORT_HEADER
)
308 wstyle
|= LVS_NOSORTHEADER
;
310 if ( style
& wxLC_NO_HEADER
)
311 wstyle
|= LVS_NOCOLUMNHEADER
;
313 if ( style
& wxLC_EDIT_LABELS
)
314 wstyle
|= LVS_EDITLABELS
;
316 if ( style
& wxLC_SINGLE_SEL
)
317 wstyle
|= LVS_SINGLESEL
;
319 if ( style
& wxLC_SORT_ASCENDING
)
321 if ( oldStyle
& LVS_SORTDESCENDING
)
322 oldStyle
-= LVS_SORTDESCENDING
;
323 wstyle
|= LVS_SORTASCENDING
;
326 if ( style
& wxLC_SORT_DESCENDING
)
328 if ( oldStyle
& LVS_SORTASCENDING
)
329 oldStyle
-= LVS_SORTASCENDING
;
330 wstyle
|= LVS_SORTDESCENDING
;
336 // Sets the background colour (GetBackgroundColour already implicit in
338 void wxListCtrl::SetBackgroundColour(const wxColour
& col
)
340 wxWindow::SetBackgroundColour(col
);
342 ListView_SetBkColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
345 // Gets information about this column
346 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
351 lvCol
.pszText
= NULL
;
353 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
355 lvCol
.mask
|= LVCF_TEXT
;
356 lvCol
.pszText
= new char[513];
357 lvCol
.cchTextMax
= 512;
360 bool success
= (ListView_GetColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
362 // item.m_subItem = lvCol.iSubItem;
363 item
.m_width
= lvCol
.cx
;
365 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
367 item
.m_text
= lvCol
.pszText
;
368 delete[] lvCol
.pszText
;
371 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
373 if (lvCol
.fmt
== LVCFMT_LEFT
)
374 item
.m_format
= wxLIST_FORMAT_LEFT
;
375 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
376 item
.m_format
= wxLIST_FORMAT_RIGHT
;
377 else if (lvCol
.fmt
== LVCFMT_CENTER
)
378 item
.m_format
= wxLIST_FORMAT_CENTRE
;
384 // Sets information about this column
385 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
390 lvCol
.pszText
= NULL
;
392 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
394 lvCol
.mask
|= LVCF_TEXT
;
395 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
396 lvCol
.cchTextMax
= 0; // Ignored
398 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
400 lvCol
.mask
|= LVCF_FMT
;
402 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
403 lvCol
.fmt
= LVCFMT_LEFT
;
404 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
405 lvCol
.fmt
= LVCFMT_RIGHT
;
406 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
407 lvCol
.fmt
= LVCFMT_CENTER
;
410 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
412 lvCol
.mask
|= LVCF_WIDTH
;
413 lvCol
.cx
= item
.m_width
;
415 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
416 lvCol
.cx
= LVSCW_AUTOSIZE
;
417 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
418 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
420 lvCol
.mask
|= LVCF_SUBITEM
;
421 lvCol
.iSubItem
= col
;
422 return (ListView_SetColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
425 // Gets the column width
426 int wxListCtrl::GetColumnWidth(int col
) const
428 return ListView_GetColumnWidth((HWND
) GetHWND(), col
);
431 // Sets the column width
432 bool wxListCtrl::SetColumnWidth(int col
, int width
)
435 if ( m_windowStyle
& wxLC_LIST
)
439 if ( width2
== wxLIST_AUTOSIZE
)
440 width2
= LVSCW_AUTOSIZE
;
441 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
442 width2
= LVSCW_AUTOSIZE_USEHEADER
;
444 return (ListView_SetColumnWidth((HWND
) GetHWND(), col2
, width2
) != 0);
447 // Gets the number of items that can fit vertically in the
448 // visible area of the list control (list or report view)
449 // or the total number of items in the list control (icon
450 // or small icon view)
451 int wxListCtrl::GetCountPerPage(void) const
453 return ListView_GetCountPerPage((HWND
) GetHWND());
456 // Gets the edit control for editing labels.
457 wxTextCtrl
* wxListCtrl::GetEditControl(void) const
462 // Gets information about the item
463 bool wxListCtrl::GetItem(wxListItem
& info
) const
467 memset(&lvItem
, 0, sizeof(lvItem
));
469 ZeroMemory(&lvItem
, sizeof(lvItem
)); // must set all fields to 0
472 lvItem
.iItem
= info
.m_itemId
;
474 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
476 lvItem
.mask
|= LVIF_TEXT
;
477 lvItem
.pszText
= new char[513];
478 lvItem
.cchTextMax
= 512;
482 lvItem
.pszText
= NULL
;
485 if (info
.m_mask
& wxLIST_MASK_DATA
)
486 lvItem
.mask
|= LVIF_PARAM
;
488 if ( info
.m_mask
& wxLIST_MASK_STATE
)
490 lvItem
.mask
|= LVIF_STATE
;
491 // the other bits are hardly interesting anyhow
492 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
495 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
498 wxLogError(_("Couldn't retrieve information about list control item %d."),
503 wxConvertFromMSWListItem(this, info
, lvItem
);
507 delete[] lvItem
.pszText
;
512 // Sets information about the item
513 bool wxListCtrl::SetItem(wxListItem
& info
)
516 wxConvertToMSWListItem(this, info
, item
);
518 return (ListView_SetItem((HWND
) GetHWND(), &item
) != 0);
521 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
525 info
.m_mask
= wxLIST_MASK_TEXT
;
526 info
.m_itemId
= index
;
530 info
.m_image
= imageId
;
531 info
.m_mask
|= wxLIST_MASK_IMAGE
;
533 return SetItem(info
);
537 // Gets the item state
538 int wxListCtrl::GetItemState(long item
, long stateMask
) const
542 info
.m_mask
= wxLIST_MASK_STATE
;
543 info
.m_stateMask
= stateMask
;
544 info
.m_itemId
= item
;
552 // Sets the item state
553 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
557 info
.m_mask
= wxLIST_MASK_STATE
;
558 info
.m_state
= state
;
559 info
.m_stateMask
= stateMask
;
560 info
.m_itemId
= item
;
562 return SetItem(info
);
565 // Sets the item image
566 bool wxListCtrl::SetItemImage(long item
, int image
, int selImage
)
570 info
.m_mask
= wxLIST_MASK_IMAGE
;
571 info
.m_image
= image
;
572 info
.m_itemId
= item
;
574 return SetItem(info
);
577 // Gets the item text
578 wxString
wxListCtrl::GetItemText(long item
) const
582 info
.m_mask
= wxLIST_MASK_TEXT
;
583 info
.m_itemId
= item
;
590 // Sets the item text
591 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
595 info
.m_mask
= wxLIST_MASK_TEXT
;
596 info
.m_itemId
= item
;
602 // Gets the item data
603 long wxListCtrl::GetItemData(long item
) const
607 info
.m_mask
= wxLIST_MASK_DATA
;
608 info
.m_itemId
= item
;
615 // Sets the item data
616 bool wxListCtrl::SetItemData(long item
, long data
)
620 info
.m_mask
= wxLIST_MASK_DATA
;
621 info
.m_itemId
= item
;
624 return SetItem(info
);
627 // Gets the item rectangle
628 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
632 int code2
= LVIR_BOUNDS
;
633 if ( code
== wxLIST_RECT_BOUNDS
)
635 else if ( code
== wxLIST_RECT_ICON
)
637 else if ( code
== wxLIST_RECT_LABEL
)
640 bool success
= (ListView_GetItemRect((HWND
) GetHWND(), (int) item
, &rect2
, code2
) != 0);
644 rect
.width
= rect2
.right
- rect2
.left
;
645 rect
.height
= rect2
.bottom
- rect2
.left
;
649 // Gets the item position
650 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
654 bool success
= (ListView_GetItemPosition((HWND
) GetHWND(), (int) item
, &pt
) != 0);
656 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
660 // Sets the item position.
661 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
663 return (ListView_SetItemPosition((HWND
) GetHWND(), (int) item
, pos
.x
, pos
.y
) != 0);
666 // Gets the number of items in the list control
667 int wxListCtrl::GetItemCount(void) const
669 return ListView_GetItemCount((HWND
) GetHWND());
672 // Retrieves the spacing between icons in pixels.
673 // If small is TRUE, gets the spacing for the small icon
674 // view, otherwise the large icon view.
675 int wxListCtrl::GetItemSpacing(bool isSmall
) const
677 return ListView_GetItemSpacing((HWND
) GetHWND(), (BOOL
) isSmall
);
680 // Gets the number of selected items in the list control
681 int wxListCtrl::GetSelectedItemCount(void) const
683 return ListView_GetSelectedCount((HWND
) GetHWND());
686 // Gets the text colour of the listview
687 wxColour
wxListCtrl::GetTextColour(void) const
689 COLORREF ref
= ListView_GetTextColor((HWND
) GetHWND());
690 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
694 // Sets the text colour of the listview
695 void wxListCtrl::SetTextColour(const wxColour
& col
)
697 ListView_SetTextColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Blue(), col
.Green()));
700 // Gets the index of the topmost visible item when in
701 // list or report view
702 long wxListCtrl::GetTopItem(void) const
704 return (long) ListView_GetTopIndex((HWND
) GetHWND());
707 // Searches for an item, starting from 'item'.
708 // 'geometry' is one of
709 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
710 // 'state' is a state bit flag, one or more of
711 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
712 // item can be -1 to find the first item that matches the
714 // Returns the item or -1 if unsuccessful.
715 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
719 if ( geom
== wxLIST_NEXT_ABOVE
)
721 if ( geom
== wxLIST_NEXT_ALL
)
723 if ( geom
== wxLIST_NEXT_BELOW
)
725 if ( geom
== wxLIST_NEXT_LEFT
)
726 flags
|= LVNI_TOLEFT
;
727 if ( geom
== wxLIST_NEXT_RIGHT
)
728 flags
|= LVNI_TORIGHT
;
730 if ( state
& wxLIST_STATE_CUT
)
732 if ( state
& wxLIST_STATE_DROPHILITED
)
733 flags
|= LVNI_DROPHILITED
;
734 if ( state
& wxLIST_STATE_FOCUSED
)
735 flags
|= LVNI_FOCUSED
;
736 if ( state
& wxLIST_STATE_SELECTED
)
737 flags
|= LVNI_SELECTED
;
739 return (long) ListView_GetNextItem((HWND
) GetHWND(), item
, flags
);
743 wxImageList
*wxListCtrl::GetImageList(int which
) const
745 if ( which
== wxIMAGE_LIST_NORMAL
)
747 return m_imageListNormal
;
749 else if ( which
== wxIMAGE_LIST_SMALL
)
751 return m_imageListSmall
;
753 else if ( which
== wxIMAGE_LIST_STATE
)
755 return m_imageListState
;
760 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
763 if ( which
== wxIMAGE_LIST_NORMAL
)
765 flags
= LVSIL_NORMAL
;
766 m_imageListNormal
= imageList
;
768 else if ( which
== wxIMAGE_LIST_SMALL
)
771 m_imageListSmall
= imageList
;
773 else if ( which
== wxIMAGE_LIST_STATE
)
776 m_imageListState
= imageList
;
778 ListView_SetImageList((HWND
) GetHWND(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
782 ////////////////////////////////////////////////////////////////////////////
784 // Arranges the items
785 bool wxListCtrl::Arrange(int flag
)
788 if ( flag
== wxLIST_ALIGN_LEFT
)
789 code
= LVA_ALIGNLEFT
;
790 else if ( flag
== wxLIST_ALIGN_TOP
)
792 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
794 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
795 code
= LVA_SNAPTOGRID
;
797 return (ListView_Arrange((HWND
) GetHWND(), code
) != 0);
801 bool wxListCtrl::DeleteItem(long item
)
803 return (ListView_DeleteItem((HWND
) GetHWND(), (int) item
) != 0);
807 bool wxListCtrl::DeleteAllItems(void)
809 return (ListView_DeleteAllItems((HWND
) GetHWND()) != 0);
813 bool wxListCtrl::DeleteAllColumns(void)
816 for ( i
= 0; i
< m_colCount
; i
++)
818 if (ListView_DeleteColumn((HWND
) GetHWND(), 0) != 0)
821 return (m_colCount
== 0);
825 bool wxListCtrl::DeleteColumn(int col
)
827 bool success
= (ListView_DeleteColumn((HWND
) GetHWND(), col
) != 0);
829 if ( success
&& (m_colCount
> 0) )
834 // Clears items, and columns if there are any.
835 void wxListCtrl::ClearAll(void)
838 if ( m_colCount
> 0 )
842 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
844 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
846 HWND hWnd
= (HWND
) ListView_EditLabel((HWND
) GetHWND(), item
);
850 m_textCtrl
->UnsubclassWin();
851 m_textCtrl
->SetHWND(0);
856 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
857 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
858 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
863 // End label editing, optionally cancelling the edit
864 bool wxListCtrl::EndEditLabel(bool cancel
)
868 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
870 bool success = (ListView_EndEditLabelNow((HWND) GetHWND(), cancel) != 0);
874 m_textCtrl->UnsubclassWin();
875 m_textCtrl->SetHWND(0);
885 // Ensures this item is visible
886 bool wxListCtrl::EnsureVisible(long item
)
888 return (ListView_EnsureVisible((HWND
) GetHWND(), (int) item
, FALSE
) != 0);
891 // Find an item whose label matches this string, starting from the item after 'start'
892 // or the beginning if 'start' is -1.
893 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
895 LV_FINDINFO findInfo
;
897 findInfo
.flags
= LVFI_STRING
;
899 findInfo
.flags
|= LVFI_STRING
;
900 findInfo
.psz
= WXSTRINGCAST str
;
902 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
905 // Find an item whose data matches this data, starting from the item after 'start'
906 // or the beginning if 'start' is -1.
907 long wxListCtrl::FindItem(long start
, long data
)
909 LV_FINDINFO findInfo
;
911 findInfo
.flags
= LVFI_PARAM
;
912 findInfo
.lParam
= data
;
914 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
917 // Find an item nearest this position in the specified direction, starting from
918 // the item after 'start' or the beginning if 'start' is -1.
919 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
921 LV_FINDINFO findInfo
;
923 findInfo
.flags
= LVFI_NEARESTXY
;
924 findInfo
.pt
.x
= pt
.x
;
925 findInfo
.pt
.y
= pt
.y
;
926 findInfo
.vkDirection
= VK_RIGHT
;
928 if ( direction
== wxLIST_FIND_UP
)
929 findInfo
.vkDirection
= VK_UP
;
930 else if ( direction
== wxLIST_FIND_DOWN
)
931 findInfo
.vkDirection
= VK_DOWN
;
932 else if ( direction
== wxLIST_FIND_LEFT
)
933 findInfo
.vkDirection
= VK_LEFT
;
934 else if ( direction
== wxLIST_FIND_RIGHT
)
935 findInfo
.vkDirection
= VK_RIGHT
;
937 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
940 // Determines which item (if any) is at the specified point,
941 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
942 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
944 LV_HITTESTINFO hitTestInfo
;
945 hitTestInfo
.pt
.x
= (int) point
.x
;
946 hitTestInfo
.pt
.y
= (int) point
.y
;
948 ListView_HitTest((HWND
) GetHWND(), & hitTestInfo
);
951 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
952 flags
|= wxLIST_HITTEST_ABOVE
;
953 if ( hitTestInfo
.flags
& LVHT_BELOW
)
954 flags
|= wxLIST_HITTEST_BELOW
;
955 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
956 flags
|= wxLIST_HITTEST_NOWHERE
;
957 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
958 flags
|= wxLIST_HITTEST_ONITEMICON
;
959 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
960 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
961 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
962 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
963 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
964 flags
|= wxLIST_HITTEST_TOLEFT
;
965 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
966 flags
|= wxLIST_HITTEST_TORIGHT
;
968 return (long) hitTestInfo
.iItem
;
971 // Inserts an item, returning the index of the new item if successful,
973 long wxListCtrl::InsertItem(wxListItem
& info
)
976 wxConvertToMSWListItem(this, info
, item
);
978 return (long) ListView_InsertItem((HWND
) GetHWND(), & item
);
981 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
985 info
.m_mask
= wxLIST_MASK_TEXT
;
986 info
.m_itemId
= index
;
987 return InsertItem(info
);
990 // Inserts an image item
991 long wxListCtrl::InsertItem(long index
, int imageIndex
)
994 info
.m_image
= imageIndex
;
995 info
.m_mask
= wxLIST_MASK_IMAGE
;
996 info
.m_itemId
= index
;
997 return InsertItem(info
);
1000 // Inserts an image/string item
1001 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1004 info
.m_image
= imageIndex
;
1005 info
.m_text
= label
;
1006 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1007 info
.m_itemId
= index
;
1008 return InsertItem(info
);
1011 // For list view mode (only), inserts a column.
1012 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1017 lvCol
.pszText
= NULL
;
1019 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1021 lvCol
.mask
|= LVCF_TEXT
;
1022 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1023 lvCol
.cchTextMax
= 0; // Ignored
1025 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1027 lvCol
.mask
|= LVCF_FMT
;
1029 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1030 lvCol
.fmt
= LVCFMT_LEFT
;
1031 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1032 lvCol
.fmt
= LVCFMT_RIGHT
;
1033 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1034 lvCol
.fmt
= LVCFMT_CENTER
;
1037 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1039 lvCol
.mask
|= LVCF_WIDTH
;
1040 lvCol
.cx
= item
.m_width
;
1042 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
1043 lvCol
.cx
= LVSCW_AUTOSIZE
;
1044 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
1045 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1047 lvCol
.mask
|= LVCF_SUBITEM
;
1048 lvCol
.iSubItem
= col
;
1050 bool success
= (ListView_InsertColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
1056 long wxListCtrl::InsertColumn(long col
, const wxString
& heading
, int format
,
1060 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1061 item
.m_text
= heading
;
1064 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1065 item
.m_width
= width
;
1067 item
.m_format
= format
;
1069 return InsertColumn(col
, item
);
1072 // Scrolls the list control. If in icon, small icon or report view mode,
1073 // x specifies the number of pixels to scroll. If in list view mode, x
1074 // specifies the number of columns to scroll.
1075 // If in icon, small icon or list view mode, y specifies the number of pixels
1076 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1077 bool wxListCtrl::ScrollList(int dx
, int dy
)
1079 return (ListView_Scroll((HWND
) GetHWND(), dx
, dy
) != 0);
1084 // fn is a function which takes 3 long arguments: item1, item2, data.
1085 // item1 is the long data associated with a first item (NOT the index).
1086 // item2 is the long data associated with a second item (NOT the index).
1087 // data is the same value as passed to SortItems.
1088 // The return value is a negative number if the first item should precede the second
1089 // item, a positive number of the second item should precede the first,
1090 // or zero if the two items are equivalent.
1092 // data is arbitrary data to be passed to the sort function.
1093 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1095 return (ListView_SortItems((HWND
) GetHWND(), (PFNLVCOMPARE
) fn
, data
) != 0);
1098 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1100 if (cmd
== EN_UPDATE
)
1102 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1103 event
.SetEventObject( this );
1104 ProcessCommand(event
);
1107 else if (cmd
== EN_KILLFOCUS
)
1109 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1110 event
.SetEventObject( this );
1111 ProcessCommand(event
);
1117 bool wxListCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
*result
)
1119 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1120 wxEventType eventType
= wxEVT_NULL
;
1121 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1122 switch ( hdr1
->code
)
1124 case LVN_BEGINRDRAG
:
1125 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1129 if ( eventType
== wxEVT_NULL
)
1131 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1135 NM_LISTVIEW
*hdr
= (NM_LISTVIEW
*)lParam
;
1136 event
.m_itemIndex
= hdr
->iItem
;
1137 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1138 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1142 case LVN_BEGINLABELEDIT
:
1144 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1145 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1146 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1150 case LVN_COLUMNCLICK
:
1152 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1153 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1154 event
.m_itemIndex
= -1;
1155 event
.m_col
= hdr
->iSubItem
;
1158 case LVN_DELETEALLITEMS
:
1160 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1161 // NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1162 event
.m_itemIndex
= -1;
1165 case LVN_DELETEITEM
:
1167 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1168 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1169 event
.m_itemIndex
= hdr
->iItem
;
1172 case LVN_ENDLABELEDIT
:
1174 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1175 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1176 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1177 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1178 event
.m_cancelled
= TRUE
;
1181 case LVN_GETDISPINFO
:
1184 // TODO: some text buffering here, I think
1185 // TODO: API for getting Windows to retrieve values
1187 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1188 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1189 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1192 case LVN_INSERTITEM
:
1194 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1195 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1196 event
.m_itemIndex
= hdr
->iItem
;
1199 case LVN_ITEMCHANGED
:
1201 // This needs to be sent to wxListCtrl as a rather more
1202 // concrete event. For now, just detect a selection
1204 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1205 if ( (hdr
->uNewState
& LVIS_SELECTED
) && !(hdr
->uOldState
& LVIS_SELECTED
) )
1207 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1208 event
.m_itemIndex
= hdr
->iItem
;
1210 else if ( !(hdr
->uNewState
& LVIS_SELECTED
) && (hdr
->uOldState
& LVIS_SELECTED
) )
1212 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1213 event
.m_itemIndex
= hdr
->iItem
;
1221 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1222 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1223 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1226 case LVN_SETDISPINFO
:
1228 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1229 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1230 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1235 return wxControl::MSWNotify(wParam
, lParam
, result
);
1238 event
.SetEventObject( this );
1239 event
.SetEventType(eventType
);
1241 if ( !GetEventHandler()->ProcessEvent(event
) )
1244 if (hdr1
->code
== LVN_GETDISPINFO
)
1246 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1247 if ( info
->item
.mask
& LVIF_TEXT
)
1249 if ( !event
.m_item
.m_text
.IsNull() )
1251 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1252 info
->item
.cchTextMax
= strlen(info
->item
.pszText
) + 1;
1255 // wxConvertToMSWListItem(this, event.m_item, info->item);
1258 *result
= !event
.IsAllowed();
1263 char *wxListCtrl::AddPool(const wxString
& str
)
1265 // Remove the first element if 3 strings exist
1266 if ( m_stringPool
.Number() == 3 )
1268 wxNode
*node
= m_stringPool
.First();
1269 delete[] (char *)node
->Data();
1272 wxNode
*node
= m_stringPool
.Add((char *) (const char *)str
);
1273 return (char *)node
->Data();
1276 // List item structure
1277 wxListItem::wxListItem(void)
1287 m_format
= wxLIST_FORMAT_CENTRE
;
1291 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1293 info
.m_data
= lvItem
.lParam
;
1296 info
.m_stateMask
= 0;
1297 info
.m_itemId
= lvItem
.iItem
;
1299 long oldMask
= lvItem
.mask
;
1301 bool needText
= FALSE
;
1302 if (getFullInfo
!= 0)
1304 if ( lvItem
.mask
& LVIF_TEXT
)
1311 lvItem
.pszText
= new char[513];
1312 lvItem
.cchTextMax
= 512;
1314 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ;
1315 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1316 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
) ;
1319 if ( lvItem
.mask
& LVIF_STATE
)
1321 info
.m_mask
|= wxLIST_MASK_STATE
;
1323 if ( lvItem
.stateMask
& LVIS_CUT
)
1325 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1326 if ( lvItem
.state
& LVIS_CUT
)
1327 info
.m_state
|= wxLIST_STATE_CUT
;
1329 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1331 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1332 if ( lvItem
.state
& LVIS_DROPHILITED
)
1333 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1335 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1337 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1338 if ( lvItem
.state
& LVIS_FOCUSED
)
1339 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1341 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1343 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1344 if ( lvItem
.state
& LVIS_SELECTED
)
1345 info
.m_state
|= wxLIST_STATE_SELECTED
;
1349 if ( lvItem
.mask
& LVIF_TEXT
)
1351 info
.m_mask
|= wxLIST_MASK_TEXT
;
1352 info
.m_text
= lvItem
.pszText
;
1354 if ( lvItem
.mask
& LVIF_IMAGE
)
1356 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1357 info
.m_image
= lvItem
.iImage
;
1359 if ( lvItem
.mask
& LVIF_PARAM
)
1360 info
.m_mask
|= wxLIST_MASK_DATA
;
1361 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1362 info
.m_mask
|= wxLIST_SET_ITEM
;
1363 info
.m_col
= lvItem
.iSubItem
;
1368 delete[] lvItem
.pszText
;
1370 lvItem
.mask
= oldMask
;
1373 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1375 lvItem
.iItem
= (int) info
.m_itemId
;
1377 lvItem
.iImage
= info
.m_image
;
1378 lvItem
.lParam
= info
.m_data
;
1379 lvItem
.stateMask
= 0;
1382 lvItem
.iSubItem
= info
.m_col
;
1384 if (info
.m_mask
& wxLIST_MASK_STATE
)
1386 lvItem
.mask
|= LVIF_STATE
;
1387 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1389 lvItem
.stateMask
|= LVIS_CUT
;
1390 if (info
.m_state
& wxLIST_STATE_CUT
)
1391 lvItem
.state
|= LVIS_CUT
;
1393 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1395 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1396 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1397 lvItem
.state
|= LVIS_DROPHILITED
;
1399 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1401 lvItem
.stateMask
|= LVIS_FOCUSED
;
1402 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1403 lvItem
.state
|= LVIS_FOCUSED
;
1405 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1407 lvItem
.stateMask
|= LVIS_SELECTED
;
1408 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1409 lvItem
.state
|= LVIS_SELECTED
;
1413 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1415 lvItem
.mask
|= LVIF_TEXT
;
1416 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1418 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1422 lvItem
.pszText
= (char *) (const char *)info
.m_text
;
1423 if ( lvItem
.pszText
)
1424 lvItem
.cchTextMax
= info
.m_text
.Length();
1426 lvItem
.cchTextMax
= 0;
1429 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
1430 lvItem
.mask
|= LVIF_IMAGE
;
1431 if (info
.m_mask
& wxLIST_MASK_DATA
)
1432 lvItem
.mask
|= LVIF_PARAM
;
1436 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
1438 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
1439 : wxNotifyEvent(commandType
, id
)
1444 m_cancelled
= FALSE
;