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
;
60 bool wxListCtrl::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
61 long style
, const wxValidator
& validator
, const wxString
& name
)
63 m_imageListNormal
= NULL
;
64 m_imageListSmall
= NULL
;
65 m_imageListState
= NULL
;
69 wxSystemSettings settings
;
70 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
71 SetForegroundColour(parent
->GetDefaultForegroundColour());
73 SetValidator(validator
);
81 m_windowStyle
= style
;
94 m_windowId
= (id
== -1) ? NewControlId() : id
;
96 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
;
99 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
101 // Even with extended styles, need to combine with WS_BORDER
102 // for them to look right.
103 if (want3D
|| (m_windowStyle
& wxSIMPLE_BORDER
) || (m_windowStyle
& wxRAISED_BORDER
) ||
104 (m_windowStyle
& wxSUNKEN_BORDER
) || (m_windowStyle
& wxDOUBLE_BORDER
))
107 wstyle
|= LVS_SHAREIMAGELISTS
;
108 m_baseStyle
= wstyle
;
110 long oldStyle
= 0; // Dummy
111 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
113 // Create the ListView control.
114 HWND hWndListControl
= CreateWindowEx(exStyle
,
119 (HWND
) parent
->GetHWND(),
124 m_hWnd
= (WXHWND
) hWndListControl
;
125 if (parent
) parent
->AddChild(this);
127 SubclassWin((WXHWND
) m_hWnd
);
132 wxListCtrl::~wxListCtrl(void)
136 m_textCtrl
->UnsubclassWin();
137 m_textCtrl
->SetHWND(0);
143 // Add or remove a single window style
144 void wxListCtrl::SetSingleStyle(long style
, bool add
)
146 long flag
= GetWindowStyleFlag();
148 // Get rid of conflicting styles
151 if ( style
& wxLC_MASK_TYPE
)
152 flag
= flag
& ~wxLC_MASK_TYPE
;
153 if ( style
& wxLC_MASK_ALIGN
)
154 flag
= flag
& ~wxLC_MASK_ALIGN
;
155 if ( style
& wxLC_MASK_SORT
)
156 flag
= flag
& ~wxLC_MASK_SORT
;
172 m_windowStyle
= flag
;
177 // Set the whole window style
178 void wxListCtrl::SetWindowStyleFlag(long flag
)
180 m_windowStyle
= flag
;
185 void wxListCtrl::RecreateWindow(void)
190 long style
= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
191 style
|= m_baseStyle
;
192 // ::SetWindowLong((HWND) GetHWND(), GWL_STYLE, style);
194 // The following recreation of the window appears to be necessary
195 // because SetWindowLong doesn't seem to do it.
197 int x
, y
, width
, height
;
199 GetSize(&width
, &height
);
202 ::DestroyWindow((HWND
) GetHWND());
205 // Recreate the ListView control: unfortunately I can't
206 // make it work by using SetWindowLong.
208 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
209 HWND hWndListControl
= CreateWindowEx(exStyle
,
214 (HWND
) GetParent()->GetHWND(),
219 m_hWnd
= (WXHWND
) hWndListControl
;
220 SubclassWin((WXHWND
) m_hWnd
);
222 if ( m_imageListNormal
)
223 SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
224 if ( m_imageListSmall
)
225 SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
226 if ( m_imageListState
)
227 SetImageList(m_imageListState
, wxIMAGE_LIST_STATE
);
231 // Can be just a single style, or a bitlist
232 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
235 if ( style
& wxLC_ICON
)
237 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
238 oldStyle
-= LVS_SMALLICON
;
239 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
240 oldStyle
-= LVS_REPORT
;
241 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
242 oldStyle
-= LVS_LIST
;
246 if ( style
& wxLC_SMALL_ICON
)
248 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
249 oldStyle
-= LVS_ICON
;
250 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
251 oldStyle
-= LVS_REPORT
;
252 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
253 oldStyle
-= LVS_LIST
;
254 wstyle
|= LVS_SMALLICON
;
257 if ( style
& wxLC_LIST
)
259 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
260 oldStyle
-= LVS_ICON
;
261 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
262 oldStyle
-= LVS_REPORT
;
263 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
264 oldStyle
-= LVS_SMALLICON
;
268 if ( style
& wxLC_REPORT
)
270 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
271 oldStyle
-= LVS_ICON
;
272 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
273 oldStyle
-= LVS_LIST
;
274 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
275 oldStyle
-= LVS_SMALLICON
;
276 wstyle
|= LVS_REPORT
;
279 if ( style
& wxLC_ALIGN_LEFT
)
281 if ( oldStyle
& LVS_ALIGNTOP
)
282 oldStyle
-= LVS_ALIGNTOP
;
283 wstyle
|= LVS_ALIGNLEFT
;
286 if ( style
& wxLC_ALIGN_TOP
)
288 if ( oldStyle
& LVS_ALIGNLEFT
)
289 oldStyle
-= LVS_ALIGNLEFT
;
290 wstyle
|= LVS_ALIGNTOP
;
293 if ( style
& wxLC_AUTOARRANGE
)
294 wstyle
|= LVS_AUTOARRANGE
;
296 // Apparently, no such style (documentation wrong?)
298 if ( style & wxLC_BUTTON )
299 wstyle |= LVS_BUTTON;
302 if ( style
& wxLC_NO_SORT_HEADER
)
303 wstyle
|= LVS_NOSORTHEADER
;
305 if ( style
& wxLC_NO_HEADER
)
306 wstyle
|= LVS_NOCOLUMNHEADER
;
308 if ( style
& wxLC_EDIT_LABELS
)
309 wstyle
|= LVS_EDITLABELS
;
311 if ( style
& wxLC_SINGLE_SEL
)
312 wstyle
|= LVS_SINGLESEL
;
314 if ( style
& wxLC_SORT_ASCENDING
)
316 if ( oldStyle
& LVS_SORTDESCENDING
)
317 oldStyle
-= LVS_SORTDESCENDING
;
318 wstyle
|= LVS_SORTASCENDING
;
321 if ( style
& wxLC_SORT_DESCENDING
)
323 if ( oldStyle
& LVS_SORTASCENDING
)
324 oldStyle
-= LVS_SORTASCENDING
;
325 wstyle
|= LVS_SORTDESCENDING
;
331 // Sets the background colour (GetBackgroundColour already implicit in
333 void wxListCtrl::SetBackgroundColour(const wxColour
& col
)
335 wxWindow::SetBackgroundColour(col
);
337 ListView_SetBkColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
340 // Gets information about this column
341 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
346 lvCol
.pszText
= NULL
;
348 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
350 lvCol
.mask
|= LVCF_TEXT
;
351 lvCol
.pszText
= new char[513];
352 lvCol
.cchTextMax
= 512;
355 bool success
= (ListView_GetColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
357 // item.m_subItem = lvCol.iSubItem;
358 item
.m_width
= lvCol
.cx
;
360 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
362 item
.m_text
= lvCol
.pszText
;
363 delete[] lvCol
.pszText
;
366 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
368 if (lvCol
.fmt
== LVCFMT_LEFT
)
369 item
.m_format
= wxLIST_FORMAT_LEFT
;
370 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
371 item
.m_format
= wxLIST_FORMAT_RIGHT
;
372 else if (lvCol
.fmt
== LVCFMT_CENTER
)
373 item
.m_format
= wxLIST_FORMAT_CENTRE
;
379 // Sets information about this column
380 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
385 lvCol
.pszText
= NULL
;
387 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
389 lvCol
.mask
|= LVCF_TEXT
;
390 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
391 lvCol
.cchTextMax
= 0; // Ignored
393 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
395 lvCol
.mask
|= LVCF_FMT
;
397 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
398 lvCol
.fmt
= LVCFMT_LEFT
;
399 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
400 lvCol
.fmt
= LVCFMT_RIGHT
;
401 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
402 lvCol
.fmt
= LVCFMT_CENTER
;
405 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
407 lvCol
.mask
|= LVCF_WIDTH
;
408 lvCol
.cx
= item
.m_width
;
410 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
411 lvCol
.cx
= LVSCW_AUTOSIZE
;
412 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
413 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
415 lvCol
.mask
|= LVCF_SUBITEM
;
416 lvCol
.iSubItem
= col
;
417 return (ListView_SetColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
420 // Gets the column width
421 int wxListCtrl::GetColumnWidth(int col
) const
423 return ListView_GetColumnWidth((HWND
) GetHWND(), col
);
426 // Sets the column width
427 bool wxListCtrl::SetColumnWidth(int col
, int width
)
430 if ( m_windowStyle
& wxLC_LIST
)
434 if ( width2
== wxLIST_AUTOSIZE
)
435 width2
= LVSCW_AUTOSIZE
;
436 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
437 width2
= LVSCW_AUTOSIZE_USEHEADER
;
439 return (ListView_SetColumnWidth((HWND
) GetHWND(), col2
, width2
) != 0);
442 // Gets the number of items that can fit vertically in the
443 // visible area of the list control (list or report view)
444 // or the total number of items in the list control (icon
445 // or small icon view)
446 int wxListCtrl::GetCountPerPage(void) const
448 return ListView_GetCountPerPage((HWND
) GetHWND());
451 // Gets the edit control for editing labels.
452 wxTextCtrl
* wxListCtrl::GetEditControl(void) const
457 // Gets information about the item
458 bool wxListCtrl::GetItem(wxListItem
& info
) const
461 lvItem
.pszText
= NULL
;
462 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
464 lvItem
.pszText
= new char[513];
465 lvItem
.cchTextMax
= 512;
467 bool success
= (::SendMessage((HWND
) GetHWND(), LVM_GETITEM
, 0, (LPARAM
)& lvItem
) != 0);
472 delete[] lvItem
.pszText
;
477 wxConvertFromMSWListItem(this, info
, lvItem
);
480 delete[] lvItem
.pszText
;
485 // Sets information about the item
486 bool wxListCtrl::SetItem(wxListItem
& info
)
489 wxConvertToMSWListItem(this, info
, item
);
491 return (ListView_SetItem((HWND
) GetHWND(), &item
) != 0);
494 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
498 info
.m_mask
= wxLIST_MASK_TEXT
;
499 info
.m_itemId
= index
;
503 info
.m_image
= imageId
;
504 info
.m_mask
|= wxLIST_MASK_IMAGE
;
506 return SetItem(info
);
510 // Gets the item state
511 int wxListCtrl::GetItemState(long item
, long stateMask
) const
515 info
.m_mask
= wxLIST_MASK_STATE
;
516 info
.m_stateMask
= stateMask
;
517 info
.m_itemId
= item
;
525 // Sets the item state
526 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
530 info
.m_mask
= wxLIST_MASK_STATE
;
531 info
.m_state
= state
;
532 info
.m_stateMask
= stateMask
;
533 info
.m_itemId
= item
;
535 return SetItem(info
);
538 // Sets the item image
539 bool wxListCtrl::SetItemImage(long item
, int image
, int selImage
)
543 info
.m_mask
= wxLIST_MASK_IMAGE
;
544 info
.m_image
= image
;
545 info
.m_itemId
= item
;
547 return SetItem(info
);
550 // Gets the item text
551 wxString
wxListCtrl::GetItemText(long item
) const
555 info
.m_mask
= wxLIST_MASK_TEXT
;
556 info
.m_itemId
= item
;
563 // Sets the item text
564 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
568 info
.m_mask
= wxLIST_MASK_TEXT
;
569 info
.m_itemId
= item
;
575 // Gets the item data
576 long wxListCtrl::GetItemData(long item
) const
580 info
.m_mask
= wxLIST_MASK_DATA
;
581 info
.m_itemId
= item
;
588 // Sets the item data
589 bool wxListCtrl::SetItemData(long item
, long data
)
593 info
.m_mask
= wxLIST_MASK_DATA
;
594 info
.m_itemId
= item
;
597 return SetItem(info
);
600 // Gets the item rectangle
601 bool wxListCtrl::GetItemRect(long item
, wxRectangle
& rect
, int code
) const
605 int code2
= LVIR_BOUNDS
;
606 if ( code
== wxLIST_RECT_BOUNDS
)
608 else if ( code
== wxLIST_RECT_ICON
)
610 else if ( code
== wxLIST_RECT_LABEL
)
613 bool success
= (ListView_GetItemRect((HWND
) GetHWND(), (int) item
, &rect2
, code2
) != 0);
617 rect
.width
= rect2
.right
- rect2
.left
;
618 rect
.height
= rect2
.bottom
- rect2
.left
;
622 // Gets the item position
623 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
627 bool success
= (ListView_GetItemPosition((HWND
) GetHWND(), (int) item
, &pt
) != 0);
629 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
633 // Sets the item position.
634 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
636 return (ListView_SetItemPosition((HWND
) GetHWND(), (int) item
, pos
.x
, pos
.y
) != 0);
639 // Gets the number of items in the list control
640 int wxListCtrl::GetItemCount(void) const
642 return ListView_GetItemCount((HWND
) GetHWND());
645 // Retrieves the spacing between icons in pixels.
646 // If small is TRUE, gets the spacing for the small icon
647 // view, otherwise the large icon view.
648 int wxListCtrl::GetItemSpacing(bool isSmall
) const
650 return ListView_GetItemSpacing((HWND
) GetHWND(), (BOOL
) isSmall
);
653 // Gets the number of selected items in the list control
654 int wxListCtrl::GetSelectedItemCount(void) const
656 return ListView_GetSelectedCount((HWND
) GetHWND());
659 // Gets the text colour of the listview
660 wxColour
wxListCtrl::GetTextColour(void) const
662 COLORREF ref
= ListView_GetTextColor((HWND
) GetHWND());
663 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
667 // Sets the text colour of the listview
668 void wxListCtrl::SetTextColour(const wxColour
& col
)
670 ListView_SetTextColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Blue(), col
.Green()));
673 // Gets the index of the topmost visible item when in
674 // list or report view
675 long wxListCtrl::GetTopItem(void) const
677 return (long) ListView_GetTopIndex((HWND
) GetHWND());
680 // Searches for an item, starting from 'item'.
681 // 'geometry' is one of
682 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
683 // 'state' is a state bit flag, one or more of
684 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
685 // item can be -1 to find the first item that matches the
687 // Returns the item or -1 if unsuccessful.
688 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
692 if ( geom
== wxLIST_NEXT_ABOVE
)
694 if ( geom
== wxLIST_NEXT_ALL
)
696 if ( geom
== wxLIST_NEXT_BELOW
)
698 if ( geom
== wxLIST_NEXT_LEFT
)
699 flags
|= LVNI_TOLEFT
;
700 if ( geom
== wxLIST_NEXT_RIGHT
)
701 flags
|= LVNI_TORIGHT
;
703 if ( state
& wxLIST_STATE_CUT
)
705 if ( state
& wxLIST_STATE_DROPHILITED
)
706 flags
|= LVNI_DROPHILITED
;
707 if ( state
& wxLIST_STATE_FOCUSED
)
708 flags
|= LVNI_FOCUSED
;
709 if ( state
& wxLIST_STATE_SELECTED
)
710 flags
|= LVNI_SELECTED
;
712 return (long) ListView_GetNextItem((HWND
) GetHWND(), item
, flags
);
716 wxImageList
*wxListCtrl::GetImageList(int which
) const
718 if ( which
== wxIMAGE_LIST_NORMAL
)
720 return m_imageListNormal
;
722 else if ( which
== wxIMAGE_LIST_SMALL
)
724 return m_imageListSmall
;
726 else if ( which
== wxIMAGE_LIST_STATE
)
728 return m_imageListState
;
733 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
736 if ( which
== wxIMAGE_LIST_NORMAL
)
738 flags
= LVSIL_NORMAL
;
739 m_imageListNormal
= imageList
;
741 else if ( which
== wxIMAGE_LIST_SMALL
)
744 m_imageListSmall
= imageList
;
746 else if ( which
== wxIMAGE_LIST_STATE
)
749 m_imageListState
= imageList
;
751 ListView_SetImageList((HWND
) GetHWND(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
755 ////////////////////////////////////////////////////////////////////////////
757 // Arranges the items
758 bool wxListCtrl::Arrange(int flag
)
761 if ( flag
== wxLIST_ALIGN_LEFT
)
762 code
= LVA_ALIGNLEFT
;
763 else if ( flag
== wxLIST_ALIGN_TOP
)
765 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
767 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
768 code
= LVA_SNAPTOGRID
;
770 return (ListView_Arrange((HWND
) GetHWND(), code
) != 0);
774 bool wxListCtrl::DeleteItem(long item
)
776 return (ListView_DeleteItem((HWND
) GetHWND(), (int) item
) != 0);
780 bool wxListCtrl::DeleteAllItems(void)
782 return (ListView_DeleteAllItems((HWND
) GetHWND()) != 0);
786 bool wxListCtrl::DeleteAllColumns(void)
789 for ( i
= 0; i
< m_colCount
; i
++)
791 if (ListView_DeleteColumn((HWND
) GetHWND(), 0) != 0)
794 return (m_colCount
== 0);
798 bool wxListCtrl::DeleteColumn(int col
)
800 bool success
= (ListView_DeleteColumn((HWND
) GetHWND(), col
) != 0);
802 if ( success
&& (m_colCount
> 0) )
807 // Clears items, and columns if there are any.
808 void wxListCtrl::ClearAll(void)
811 if ( m_colCount
> 0 )
815 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
817 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
819 HWND hWnd
= (HWND
) ListView_EditLabel((HWND
) GetHWND(), item
);
823 m_textCtrl
->UnsubclassWin();
824 m_textCtrl
->SetHWND(0);
829 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
830 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
831 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
836 // End label editing, optionally cancelling the edit
837 bool wxListCtrl::EndEditLabel(bool cancel
)
841 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
843 bool success = (ListView_EndEditLabelNow((HWND) GetHWND(), cancel) != 0);
847 m_textCtrl->UnsubclassWin();
848 m_textCtrl->SetHWND(0);
858 // Ensures this item is visible
859 bool wxListCtrl::EnsureVisible(long item
)
861 return (ListView_EnsureVisible((HWND
) GetHWND(), (int) item
, FALSE
) != 0);
864 // Find an item whose label matches this string, starting from the item after 'start'
865 // or the beginning if 'start' is -1.
866 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
868 LV_FINDINFO findInfo
;
870 findInfo
.flags
= LVFI_STRING
;
872 findInfo
.flags
|= LVFI_STRING
;
873 findInfo
.psz
= WXSTRINGCAST str
;
875 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
878 // Find an item whose data matches this data, starting from the item after 'start'
879 // or the beginning if 'start' is -1.
880 long wxListCtrl::FindItem(long start
, long data
)
882 LV_FINDINFO findInfo
;
884 findInfo
.flags
= LVFI_PARAM
;
885 findInfo
.lParam
= data
;
887 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
890 // Find an item nearest this position in the specified direction, starting from
891 // the item after 'start' or the beginning if 'start' is -1.
892 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
894 LV_FINDINFO findInfo
;
896 findInfo
.flags
= LVFI_NEARESTXY
;
897 findInfo
.pt
.x
= pt
.x
;
898 findInfo
.pt
.y
= pt
.y
;
899 findInfo
.vkDirection
= VK_RIGHT
;
901 if ( direction
== wxLIST_FIND_UP
)
902 findInfo
.vkDirection
= VK_UP
;
903 else if ( direction
== wxLIST_FIND_DOWN
)
904 findInfo
.vkDirection
= VK_DOWN
;
905 else if ( direction
== wxLIST_FIND_LEFT
)
906 findInfo
.vkDirection
= VK_LEFT
;
907 else if ( direction
== wxLIST_FIND_RIGHT
)
908 findInfo
.vkDirection
= VK_RIGHT
;
910 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
913 // Determines which item (if any) is at the specified point,
914 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
915 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
917 LV_HITTESTINFO hitTestInfo
;
918 hitTestInfo
.pt
.x
= (int) point
.x
;
919 hitTestInfo
.pt
.y
= (int) point
.y
;
921 ListView_HitTest((HWND
) GetHWND(), & hitTestInfo
);
924 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
925 flags
|= wxLIST_HITTEST_ABOVE
;
926 if ( hitTestInfo
.flags
& LVHT_BELOW
)
927 flags
|= wxLIST_HITTEST_BELOW
;
928 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
929 flags
|= wxLIST_HITTEST_NOWHERE
;
930 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
931 flags
|= wxLIST_HITTEST_ONITEMICON
;
932 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
933 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
934 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
935 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
936 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
937 flags
|= wxLIST_HITTEST_TOLEFT
;
938 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
939 flags
|= wxLIST_HITTEST_TORIGHT
;
941 return (long) hitTestInfo
.iItem
;
944 // Inserts an item, returning the index of the new item if successful,
946 long wxListCtrl::InsertItem(wxListItem
& info
)
949 wxConvertToMSWListItem(this, info
, item
);
951 return (long) ListView_InsertItem((HWND
) GetHWND(), & item
);
954 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
958 info
.m_mask
= wxLIST_MASK_TEXT
;
959 info
.m_itemId
= index
;
960 return InsertItem(info
);
963 // Inserts an image item
964 long wxListCtrl::InsertItem(long index
, int imageIndex
)
967 info
.m_image
= imageIndex
;
968 info
.m_mask
= wxLIST_MASK_IMAGE
;
969 info
.m_itemId
= index
;
970 return InsertItem(info
);
973 // Inserts an image/string item
974 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
977 info
.m_image
= imageIndex
;
979 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
980 info
.m_itemId
= index
;
981 return InsertItem(info
);
984 // For list view mode (only), inserts a column.
985 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
990 lvCol
.pszText
= NULL
;
992 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
994 lvCol
.mask
|= LVCF_TEXT
;
995 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
996 lvCol
.cchTextMax
= 0; // Ignored
998 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1000 lvCol
.mask
|= LVCF_FMT
;
1002 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1003 lvCol
.fmt
= LVCFMT_LEFT
;
1004 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1005 lvCol
.fmt
= LVCFMT_RIGHT
;
1006 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1007 lvCol
.fmt
= LVCFMT_CENTER
;
1010 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1012 lvCol
.mask
|= LVCF_WIDTH
;
1013 lvCol
.cx
= item
.m_width
;
1015 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
1016 lvCol
.cx
= LVSCW_AUTOSIZE
;
1017 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
1018 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1020 lvCol
.mask
|= LVCF_SUBITEM
;
1021 lvCol
.iSubItem
= col
;
1023 bool success
= (ListView_InsertColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
1029 long wxListCtrl::InsertColumn(long col
, const wxString
& heading
, int format
,
1033 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1034 item
.m_text
= heading
;
1037 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1038 item
.m_width
= width
;
1040 item
.m_format
= format
;
1042 return InsertColumn(col
, item
);
1045 // Scrolls the list control. If in icon, small icon or report view mode,
1046 // x specifies the number of pixels to scroll. If in list view mode, x
1047 // specifies the number of columns to scroll.
1048 // If in icon, small icon or list view mode, y specifies the number of pixels
1049 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1050 bool wxListCtrl::ScrollList(int dx
, int dy
)
1052 return (ListView_Scroll((HWND
) GetHWND(), dx
, dy
) != 0);
1057 // fn is a function which takes 3 long arguments: item1, item2, data.
1058 // item1 is the long data associated with a first item (NOT the index).
1059 // item2 is the long data associated with a second item (NOT the index).
1060 // data is the same value as passed to SortItems.
1061 // The return value is a negative number if the first item should precede the second
1062 // item, a positive number of the second item should precede the first,
1063 // or zero if the two items are equivalent.
1065 // data is arbitrary data to be passed to the sort function.
1066 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1068 return (ListView_SortItems((HWND
) GetHWND(), (PFNLVCOMPARE
) fn
, data
) != 0);
1071 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1073 if (cmd
== EN_UPDATE
)
1075 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1076 event
.SetEventObject( this );
1077 ProcessCommand(event
);
1080 else if (cmd
== EN_KILLFOCUS
)
1082 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1083 event
.SetEventObject( this );
1084 ProcessCommand(event
);
1090 bool wxListCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
)
1092 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1093 wxEventType eventType
= wxEVT_NULL
;
1094 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1095 switch ( hdr1
->code
)
1099 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1100 NM_LISTVIEW
*hdr
= (NM_LISTVIEW
*)lParam
;
1101 event
.m_itemIndex
= hdr
->iItem
;
1102 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1103 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1106 case LVN_BEGINLABELEDIT
:
1108 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1109 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1110 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1113 case LVN_BEGINRDRAG
:
1115 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1116 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1117 event
.m_itemIndex
= hdr
->iItem
;
1118 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1119 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1122 case LVN_COLUMNCLICK
:
1124 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1125 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1126 event
.m_itemIndex
= -1;
1127 event
.m_col
= hdr
->iSubItem
;
1130 case LVN_DELETEALLITEMS
:
1132 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1133 // NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1134 event
.m_itemIndex
= -1;
1137 case LVN_DELETEITEM
:
1139 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1140 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1141 event
.m_itemIndex
= hdr
->iItem
;
1144 case LVN_ENDLABELEDIT
:
1146 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1147 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1148 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1149 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1150 event
.m_cancelled
= TRUE
;
1153 case LVN_GETDISPINFO
:
1156 // TODO: some text buffering here, I think
1157 // TODO: API for getting Windows to retrieve values
1159 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1160 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1161 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1164 case LVN_INSERTITEM
:
1166 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1167 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1168 event
.m_itemIndex
= hdr
->iItem
;
1171 case LVN_ITEMCHANGED
:
1173 // This needs to be sent to wxListCtrl as a rather more
1174 // concrete event. For now, just detect a selection
1176 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1177 if ( (hdr
->uNewState
& LVIS_SELECTED
) && !(hdr
->uOldState
& LVIS_SELECTED
) )
1179 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1180 event
.m_itemIndex
= hdr
->iItem
;
1182 else if ( !(hdr
->uNewState
& LVIS_SELECTED
) && (hdr
->uOldState
& LVIS_SELECTED
) )
1184 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1185 event
.m_itemIndex
= hdr
->iItem
;
1193 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1194 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1195 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1198 case LVN_SETDISPINFO
:
1200 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1201 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1202 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1207 return wxControl::MSWNotify(wParam
, lParam
);
1211 event
.SetEventObject( this );
1212 event
.SetEventType(eventType
);
1214 if ( !GetEventHandler()->ProcessEvent(event
) )
1217 if (hdr1
->code
== LVN_GETDISPINFO
)
1219 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1220 if ( info
->item
.mask
& LVIF_TEXT
)
1222 if ( !event
.m_item
.m_text
.IsNull() )
1224 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1225 info
->item
.cchTextMax
= strlen(info
->item
.pszText
) + 1;
1228 // wxConvertToMSWListItem(this, event.m_item, info->item);
1234 char *wxListCtrl::AddPool(const wxString
& str
)
1236 // Remove the first element if 3 strings exist
1237 if ( m_stringPool
.Number() == 3 )
1239 wxNode
*node
= m_stringPool
.First();
1240 delete[] (char *)node
->Data();
1243 wxNode
*node
= m_stringPool
.Add((char *) (const char *)str
);
1244 return (char *)node
->Data();
1247 // List item structure
1248 wxListItem::wxListItem(void)
1258 m_format
= wxLIST_FORMAT_CENTRE
;
1262 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1264 info
.m_data
= lvItem
.lParam
;
1267 info
.m_stateMask
= 0;
1268 info
.m_itemId
= lvItem
.iItem
;
1270 long oldMask
= lvItem
.mask
;
1272 bool needText
= FALSE
;
1273 if (getFullInfo
!= 0)
1275 if ( lvItem
.mask
& LVIF_TEXT
)
1282 lvItem
.pszText
= new char[513];
1283 lvItem
.cchTextMax
= 512;
1285 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ;
1286 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1287 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
) ;
1290 if ( lvItem
.mask
& LVIF_STATE
)
1292 info
.m_mask
|= wxLIST_MASK_STATE
;
1294 if ( lvItem
.stateMask
& LVIS_CUT
)
1296 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1297 if ( lvItem
.state
& LVIS_CUT
)
1298 info
.m_state
|= wxLIST_STATE_CUT
;
1300 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1302 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1303 if ( lvItem
.state
& LVIS_DROPHILITED
)
1304 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1306 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1308 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1309 if ( lvItem
.state
& LVIS_FOCUSED
)
1310 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1312 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1314 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1315 if ( lvItem
.state
& LVIS_SELECTED
)
1316 info
.m_state
|= wxLIST_STATE_SELECTED
;
1320 if ( lvItem
.mask
& LVIF_TEXT
)
1322 info
.m_mask
|= wxLIST_MASK_TEXT
;
1323 info
.m_text
= lvItem
.pszText
;
1325 if ( lvItem
.mask
& LVIF_IMAGE
)
1327 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1328 info
.m_image
= lvItem
.iImage
;
1330 if ( lvItem
.mask
& LVIF_PARAM
)
1331 info
.m_mask
|= wxLIST_MASK_DATA
;
1332 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1333 info
.m_mask
|= wxLIST_SET_ITEM
;
1334 info
.m_col
= lvItem
.iSubItem
;
1339 delete[] lvItem
.pszText
;
1341 lvItem
.mask
= oldMask
;
1344 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1346 lvItem
.iItem
= (int) info
.m_itemId
;
1348 lvItem
.iImage
= info
.m_image
;
1349 lvItem
.lParam
= info
.m_data
;
1350 lvItem
.stateMask
= 0;
1353 lvItem
.iSubItem
= info
.m_col
;
1355 if (info
.m_mask
& wxLIST_MASK_STATE
)
1357 lvItem
.mask
|= LVIF_STATE
;
1358 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1360 lvItem
.stateMask
|= LVIS_CUT
;
1361 if (info
.m_state
& wxLIST_STATE_CUT
)
1362 lvItem
.state
|= LVIS_CUT
;
1364 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1366 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1367 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1368 lvItem
.state
|= LVIS_DROPHILITED
;
1370 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1372 lvItem
.stateMask
|= LVIS_FOCUSED
;
1373 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1374 lvItem
.state
|= LVIS_FOCUSED
;
1376 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1378 lvItem
.stateMask
|= LVIS_SELECTED
;
1379 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1380 lvItem
.state
|= LVIS_SELECTED
;
1384 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1386 lvItem
.mask
|= LVIF_TEXT
;
1387 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1389 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1393 lvItem
.pszText
= (char *) (const char *)info
.m_text
;
1394 if ( lvItem
.pszText
)
1395 lvItem
.cchTextMax
= info
.m_text
.Length();
1397 lvItem
.cchTextMax
= 0;
1400 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
1401 lvItem
.mask
|= LVIF_IMAGE
;
1402 if (info
.m_mask
& wxLIST_MASK_DATA
)
1403 lvItem
.mask
|= LVIF_PARAM
;
1407 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxCommandEvent
)
1409 wxListEvent::wxListEvent(wxEventType commandType
, int id
):
1410 wxCommandEvent(commandType
, id
)
1415 m_cancelled
= FALSE
;