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
;
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
466 ZeroMemory(&lvItem
, sizeof(lvItem
)); // must set all fields to 0
468 lvItem
.iItem
= info
.m_itemId
;
470 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
472 lvItem
.mask
|= LVIF_TEXT
;
473 lvItem
.pszText
= new char[513];
474 lvItem
.cchTextMax
= 512;
478 lvItem
.pszText
= NULL
;
481 if ( info
.m_mask
& wxLIST_MASK_STATE
)
483 lvItem
.mask
|= LVIF_STATE
;
484 // the other bits are hardly interesting anyhow
485 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
488 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
491 wxLogError(_("Couldn't retrieve information about list control item %d."),
496 wxConvertFromMSWListItem(this, info
, lvItem
);
500 delete[] lvItem
.pszText
;
505 // Sets information about the item
506 bool wxListCtrl::SetItem(wxListItem
& info
)
509 wxConvertToMSWListItem(this, info
, item
);
511 return (ListView_SetItem((HWND
) GetHWND(), &item
) != 0);
514 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
518 info
.m_mask
= wxLIST_MASK_TEXT
;
519 info
.m_itemId
= index
;
523 info
.m_image
= imageId
;
524 info
.m_mask
|= wxLIST_MASK_IMAGE
;
526 return SetItem(info
);
530 // Gets the item state
531 int wxListCtrl::GetItemState(long item
, long stateMask
) const
535 info
.m_mask
= wxLIST_MASK_STATE
;
536 info
.m_stateMask
= stateMask
;
537 info
.m_itemId
= item
;
545 // Sets the item state
546 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
550 info
.m_mask
= wxLIST_MASK_STATE
;
551 info
.m_state
= state
;
552 info
.m_stateMask
= stateMask
;
553 info
.m_itemId
= item
;
555 return SetItem(info
);
558 // Sets the item image
559 bool wxListCtrl::SetItemImage(long item
, int image
, int selImage
)
563 info
.m_mask
= wxLIST_MASK_IMAGE
;
564 info
.m_image
= image
;
565 info
.m_itemId
= item
;
567 return SetItem(info
);
570 // Gets the item text
571 wxString
wxListCtrl::GetItemText(long item
) const
575 info
.m_mask
= wxLIST_MASK_TEXT
;
576 info
.m_itemId
= item
;
583 // Sets the item text
584 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
588 info
.m_mask
= wxLIST_MASK_TEXT
;
589 info
.m_itemId
= item
;
595 // Gets the item data
596 long wxListCtrl::GetItemData(long item
) const
600 info
.m_mask
= wxLIST_MASK_DATA
;
601 info
.m_itemId
= item
;
608 // Sets the item data
609 bool wxListCtrl::SetItemData(long item
, long data
)
613 info
.m_mask
= wxLIST_MASK_DATA
;
614 info
.m_itemId
= item
;
617 return SetItem(info
);
620 // Gets the item rectangle
621 bool wxListCtrl::GetItemRect(long item
, wxRectangle
& rect
, int code
) const
625 int code2
= LVIR_BOUNDS
;
626 if ( code
== wxLIST_RECT_BOUNDS
)
628 else if ( code
== wxLIST_RECT_ICON
)
630 else if ( code
== wxLIST_RECT_LABEL
)
633 bool success
= (ListView_GetItemRect((HWND
) GetHWND(), (int) item
, &rect2
, code2
) != 0);
637 rect
.width
= rect2
.right
- rect2
.left
;
638 rect
.height
= rect2
.bottom
- rect2
.left
;
642 // Gets the item position
643 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
647 bool success
= (ListView_GetItemPosition((HWND
) GetHWND(), (int) item
, &pt
) != 0);
649 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
653 // Sets the item position.
654 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
656 return (ListView_SetItemPosition((HWND
) GetHWND(), (int) item
, pos
.x
, pos
.y
) != 0);
659 // Gets the number of items in the list control
660 int wxListCtrl::GetItemCount(void) const
662 return ListView_GetItemCount((HWND
) GetHWND());
665 // Retrieves the spacing between icons in pixels.
666 // If small is TRUE, gets the spacing for the small icon
667 // view, otherwise the large icon view.
668 int wxListCtrl::GetItemSpacing(bool isSmall
) const
670 return ListView_GetItemSpacing((HWND
) GetHWND(), (BOOL
) isSmall
);
673 // Gets the number of selected items in the list control
674 int wxListCtrl::GetSelectedItemCount(void) const
676 return ListView_GetSelectedCount((HWND
) GetHWND());
679 // Gets the text colour of the listview
680 wxColour
wxListCtrl::GetTextColour(void) const
682 COLORREF ref
= ListView_GetTextColor((HWND
) GetHWND());
683 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
687 // Sets the text colour of the listview
688 void wxListCtrl::SetTextColour(const wxColour
& col
)
690 ListView_SetTextColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Blue(), col
.Green()));
693 // Gets the index of the topmost visible item when in
694 // list or report view
695 long wxListCtrl::GetTopItem(void) const
697 return (long) ListView_GetTopIndex((HWND
) GetHWND());
700 // Searches for an item, starting from 'item'.
701 // 'geometry' is one of
702 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
703 // 'state' is a state bit flag, one or more of
704 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
705 // item can be -1 to find the first item that matches the
707 // Returns the item or -1 if unsuccessful.
708 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
712 if ( geom
== wxLIST_NEXT_ABOVE
)
714 if ( geom
== wxLIST_NEXT_ALL
)
716 if ( geom
== wxLIST_NEXT_BELOW
)
718 if ( geom
== wxLIST_NEXT_LEFT
)
719 flags
|= LVNI_TOLEFT
;
720 if ( geom
== wxLIST_NEXT_RIGHT
)
721 flags
|= LVNI_TORIGHT
;
723 if ( state
& wxLIST_STATE_CUT
)
725 if ( state
& wxLIST_STATE_DROPHILITED
)
726 flags
|= LVNI_DROPHILITED
;
727 if ( state
& wxLIST_STATE_FOCUSED
)
728 flags
|= LVNI_FOCUSED
;
729 if ( state
& wxLIST_STATE_SELECTED
)
730 flags
|= LVNI_SELECTED
;
732 return (long) ListView_GetNextItem((HWND
) GetHWND(), item
, flags
);
736 wxImageList
*wxListCtrl::GetImageList(int which
) const
738 if ( which
== wxIMAGE_LIST_NORMAL
)
740 return m_imageListNormal
;
742 else if ( which
== wxIMAGE_LIST_SMALL
)
744 return m_imageListSmall
;
746 else if ( which
== wxIMAGE_LIST_STATE
)
748 return m_imageListState
;
753 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
756 if ( which
== wxIMAGE_LIST_NORMAL
)
758 flags
= LVSIL_NORMAL
;
759 m_imageListNormal
= imageList
;
761 else if ( which
== wxIMAGE_LIST_SMALL
)
764 m_imageListSmall
= imageList
;
766 else if ( which
== wxIMAGE_LIST_STATE
)
769 m_imageListState
= imageList
;
771 ListView_SetImageList((HWND
) GetHWND(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
775 ////////////////////////////////////////////////////////////////////////////
777 // Arranges the items
778 bool wxListCtrl::Arrange(int flag
)
781 if ( flag
== wxLIST_ALIGN_LEFT
)
782 code
= LVA_ALIGNLEFT
;
783 else if ( flag
== wxLIST_ALIGN_TOP
)
785 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
787 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
788 code
= LVA_SNAPTOGRID
;
790 return (ListView_Arrange((HWND
) GetHWND(), code
) != 0);
794 bool wxListCtrl::DeleteItem(long item
)
796 return (ListView_DeleteItem((HWND
) GetHWND(), (int) item
) != 0);
800 bool wxListCtrl::DeleteAllItems(void)
802 return (ListView_DeleteAllItems((HWND
) GetHWND()) != 0);
806 bool wxListCtrl::DeleteAllColumns(void)
809 for ( i
= 0; i
< m_colCount
; i
++)
811 if (ListView_DeleteColumn((HWND
) GetHWND(), 0) != 0)
814 return (m_colCount
== 0);
818 bool wxListCtrl::DeleteColumn(int col
)
820 bool success
= (ListView_DeleteColumn((HWND
) GetHWND(), col
) != 0);
822 if ( success
&& (m_colCount
> 0) )
827 // Clears items, and columns if there are any.
828 void wxListCtrl::ClearAll(void)
831 if ( m_colCount
> 0 )
835 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
837 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
839 HWND hWnd
= (HWND
) ListView_EditLabel((HWND
) GetHWND(), item
);
843 m_textCtrl
->UnsubclassWin();
844 m_textCtrl
->SetHWND(0);
849 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
850 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
851 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
856 // End label editing, optionally cancelling the edit
857 bool wxListCtrl::EndEditLabel(bool cancel
)
861 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
863 bool success = (ListView_EndEditLabelNow((HWND) GetHWND(), cancel) != 0);
867 m_textCtrl->UnsubclassWin();
868 m_textCtrl->SetHWND(0);
878 // Ensures this item is visible
879 bool wxListCtrl::EnsureVisible(long item
)
881 return (ListView_EnsureVisible((HWND
) GetHWND(), (int) item
, FALSE
) != 0);
884 // Find an item whose label matches this string, starting from the item after 'start'
885 // or the beginning if 'start' is -1.
886 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
888 LV_FINDINFO findInfo
;
890 findInfo
.flags
= LVFI_STRING
;
892 findInfo
.flags
|= LVFI_STRING
;
893 findInfo
.psz
= WXSTRINGCAST str
;
895 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
898 // Find an item whose data matches this data, starting from the item after 'start'
899 // or the beginning if 'start' is -1.
900 long wxListCtrl::FindItem(long start
, long data
)
902 LV_FINDINFO findInfo
;
904 findInfo
.flags
= LVFI_PARAM
;
905 findInfo
.lParam
= data
;
907 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
910 // Find an item nearest this position in the specified direction, starting from
911 // the item after 'start' or the beginning if 'start' is -1.
912 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
914 LV_FINDINFO findInfo
;
916 findInfo
.flags
= LVFI_NEARESTXY
;
917 findInfo
.pt
.x
= pt
.x
;
918 findInfo
.pt
.y
= pt
.y
;
919 findInfo
.vkDirection
= VK_RIGHT
;
921 if ( direction
== wxLIST_FIND_UP
)
922 findInfo
.vkDirection
= VK_UP
;
923 else if ( direction
== wxLIST_FIND_DOWN
)
924 findInfo
.vkDirection
= VK_DOWN
;
925 else if ( direction
== wxLIST_FIND_LEFT
)
926 findInfo
.vkDirection
= VK_LEFT
;
927 else if ( direction
== wxLIST_FIND_RIGHT
)
928 findInfo
.vkDirection
= VK_RIGHT
;
930 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
933 // Determines which item (if any) is at the specified point,
934 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
935 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
937 LV_HITTESTINFO hitTestInfo
;
938 hitTestInfo
.pt
.x
= (int) point
.x
;
939 hitTestInfo
.pt
.y
= (int) point
.y
;
941 ListView_HitTest((HWND
) GetHWND(), & hitTestInfo
);
944 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
945 flags
|= wxLIST_HITTEST_ABOVE
;
946 if ( hitTestInfo
.flags
& LVHT_BELOW
)
947 flags
|= wxLIST_HITTEST_BELOW
;
948 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
949 flags
|= wxLIST_HITTEST_NOWHERE
;
950 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
951 flags
|= wxLIST_HITTEST_ONITEMICON
;
952 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
953 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
954 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
955 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
956 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
957 flags
|= wxLIST_HITTEST_TOLEFT
;
958 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
959 flags
|= wxLIST_HITTEST_TORIGHT
;
961 return (long) hitTestInfo
.iItem
;
964 // Inserts an item, returning the index of the new item if successful,
966 long wxListCtrl::InsertItem(wxListItem
& info
)
969 wxConvertToMSWListItem(this, info
, item
);
971 return (long) ListView_InsertItem((HWND
) GetHWND(), & item
);
974 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
978 info
.m_mask
= wxLIST_MASK_TEXT
;
979 info
.m_itemId
= index
;
980 return InsertItem(info
);
983 // Inserts an image item
984 long wxListCtrl::InsertItem(long index
, int imageIndex
)
987 info
.m_image
= imageIndex
;
988 info
.m_mask
= wxLIST_MASK_IMAGE
;
989 info
.m_itemId
= index
;
990 return InsertItem(info
);
993 // Inserts an image/string item
994 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
997 info
.m_image
= imageIndex
;
999 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1000 info
.m_itemId
= index
;
1001 return InsertItem(info
);
1004 // For list view mode (only), inserts a column.
1005 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1010 lvCol
.pszText
= NULL
;
1012 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1014 lvCol
.mask
|= LVCF_TEXT
;
1015 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1016 lvCol
.cchTextMax
= 0; // Ignored
1018 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1020 lvCol
.mask
|= LVCF_FMT
;
1022 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1023 lvCol
.fmt
= LVCFMT_LEFT
;
1024 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1025 lvCol
.fmt
= LVCFMT_RIGHT
;
1026 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1027 lvCol
.fmt
= LVCFMT_CENTER
;
1030 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1032 lvCol
.mask
|= LVCF_WIDTH
;
1033 lvCol
.cx
= item
.m_width
;
1035 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
1036 lvCol
.cx
= LVSCW_AUTOSIZE
;
1037 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
1038 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1040 lvCol
.mask
|= LVCF_SUBITEM
;
1041 lvCol
.iSubItem
= col
;
1043 bool success
= (ListView_InsertColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
1049 long wxListCtrl::InsertColumn(long col
, const wxString
& heading
, int format
,
1053 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1054 item
.m_text
= heading
;
1057 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1058 item
.m_width
= width
;
1060 item
.m_format
= format
;
1062 return InsertColumn(col
, item
);
1065 // Scrolls the list control. If in icon, small icon or report view mode,
1066 // x specifies the number of pixels to scroll. If in list view mode, x
1067 // specifies the number of columns to scroll.
1068 // If in icon, small icon or list view mode, y specifies the number of pixels
1069 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1070 bool wxListCtrl::ScrollList(int dx
, int dy
)
1072 return (ListView_Scroll((HWND
) GetHWND(), dx
, dy
) != 0);
1077 // fn is a function which takes 3 long arguments: item1, item2, data.
1078 // item1 is the long data associated with a first item (NOT the index).
1079 // item2 is the long data associated with a second item (NOT the index).
1080 // data is the same value as passed to SortItems.
1081 // The return value is a negative number if the first item should precede the second
1082 // item, a positive number of the second item should precede the first,
1083 // or zero if the two items are equivalent.
1085 // data is arbitrary data to be passed to the sort function.
1086 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1088 return (ListView_SortItems((HWND
) GetHWND(), (PFNLVCOMPARE
) fn
, data
) != 0);
1091 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1093 if (cmd
== EN_UPDATE
)
1095 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1096 event
.SetEventObject( this );
1097 ProcessCommand(event
);
1100 else if (cmd
== EN_KILLFOCUS
)
1102 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1103 event
.SetEventObject( this );
1104 ProcessCommand(event
);
1110 bool wxListCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
)
1112 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1113 wxEventType eventType
= wxEVT_NULL
;
1114 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1115 switch ( hdr1
->code
)
1119 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1120 NM_LISTVIEW
*hdr
= (NM_LISTVIEW
*)lParam
;
1121 event
.m_itemIndex
= hdr
->iItem
;
1122 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1123 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1126 case LVN_BEGINLABELEDIT
:
1128 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1129 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1130 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1133 case LVN_BEGINRDRAG
:
1135 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1136 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1137 event
.m_itemIndex
= hdr
->iItem
;
1138 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1139 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1142 case LVN_COLUMNCLICK
:
1144 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1145 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1146 event
.m_itemIndex
= -1;
1147 event
.m_col
= hdr
->iSubItem
;
1150 case LVN_DELETEALLITEMS
:
1152 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1153 // NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1154 event
.m_itemIndex
= -1;
1157 case LVN_DELETEITEM
:
1159 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1160 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1161 event
.m_itemIndex
= hdr
->iItem
;
1164 case LVN_ENDLABELEDIT
:
1166 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1167 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1168 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1169 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1170 event
.m_cancelled
= TRUE
;
1173 case LVN_GETDISPINFO
:
1176 // TODO: some text buffering here, I think
1177 // TODO: API for getting Windows to retrieve values
1179 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1180 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1181 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1184 case LVN_INSERTITEM
:
1186 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1187 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1188 event
.m_itemIndex
= hdr
->iItem
;
1191 case LVN_ITEMCHANGED
:
1193 // This needs to be sent to wxListCtrl as a rather more
1194 // concrete event. For now, just detect a selection
1196 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1197 if ( (hdr
->uNewState
& LVIS_SELECTED
) && !(hdr
->uOldState
& LVIS_SELECTED
) )
1199 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1200 event
.m_itemIndex
= hdr
->iItem
;
1202 else if ( !(hdr
->uNewState
& LVIS_SELECTED
) && (hdr
->uOldState
& LVIS_SELECTED
) )
1204 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1205 event
.m_itemIndex
= hdr
->iItem
;
1213 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1214 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1215 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1218 case LVN_SETDISPINFO
:
1220 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1221 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1222 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1227 return wxControl::MSWNotify(wParam
, lParam
);
1231 event
.SetEventObject( this );
1232 event
.SetEventType(eventType
);
1234 if ( !GetEventHandler()->ProcessEvent(event
) )
1237 if (hdr1
->code
== LVN_GETDISPINFO
)
1239 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1240 if ( info
->item
.mask
& LVIF_TEXT
)
1242 if ( !event
.m_item
.m_text
.IsNull() )
1244 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1245 info
->item
.cchTextMax
= strlen(info
->item
.pszText
) + 1;
1248 // wxConvertToMSWListItem(this, event.m_item, info->item);
1254 char *wxListCtrl::AddPool(const wxString
& str
)
1256 // Remove the first element if 3 strings exist
1257 if ( m_stringPool
.Number() == 3 )
1259 wxNode
*node
= m_stringPool
.First();
1260 delete[] (char *)node
->Data();
1263 wxNode
*node
= m_stringPool
.Add((char *) (const char *)str
);
1264 return (char *)node
->Data();
1267 // List item structure
1268 wxListItem::wxListItem(void)
1278 m_format
= wxLIST_FORMAT_CENTRE
;
1282 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1284 info
.m_data
= lvItem
.lParam
;
1287 info
.m_stateMask
= 0;
1288 info
.m_itemId
= lvItem
.iItem
;
1290 long oldMask
= lvItem
.mask
;
1292 bool needText
= FALSE
;
1293 if (getFullInfo
!= 0)
1295 if ( lvItem
.mask
& LVIF_TEXT
)
1302 lvItem
.pszText
= new char[513];
1303 lvItem
.cchTextMax
= 512;
1305 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ;
1306 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1307 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
) ;
1310 if ( lvItem
.mask
& LVIF_STATE
)
1312 info
.m_mask
|= wxLIST_MASK_STATE
;
1314 if ( lvItem
.stateMask
& LVIS_CUT
)
1316 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1317 if ( lvItem
.state
& LVIS_CUT
)
1318 info
.m_state
|= wxLIST_STATE_CUT
;
1320 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1322 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1323 if ( lvItem
.state
& LVIS_DROPHILITED
)
1324 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1326 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1328 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1329 if ( lvItem
.state
& LVIS_FOCUSED
)
1330 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1332 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1334 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1335 if ( lvItem
.state
& LVIS_SELECTED
)
1336 info
.m_state
|= wxLIST_STATE_SELECTED
;
1340 if ( lvItem
.mask
& LVIF_TEXT
)
1342 info
.m_mask
|= wxLIST_MASK_TEXT
;
1343 info
.m_text
= lvItem
.pszText
;
1345 if ( lvItem
.mask
& LVIF_IMAGE
)
1347 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1348 info
.m_image
= lvItem
.iImage
;
1350 if ( lvItem
.mask
& LVIF_PARAM
)
1351 info
.m_mask
|= wxLIST_MASK_DATA
;
1352 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1353 info
.m_mask
|= wxLIST_SET_ITEM
;
1354 info
.m_col
= lvItem
.iSubItem
;
1359 delete[] lvItem
.pszText
;
1361 lvItem
.mask
= oldMask
;
1364 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1366 lvItem
.iItem
= (int) info
.m_itemId
;
1368 lvItem
.iImage
= info
.m_image
;
1369 lvItem
.lParam
= info
.m_data
;
1370 lvItem
.stateMask
= 0;
1373 lvItem
.iSubItem
= info
.m_col
;
1375 if (info
.m_mask
& wxLIST_MASK_STATE
)
1377 lvItem
.mask
|= LVIF_STATE
;
1378 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1380 lvItem
.stateMask
|= LVIS_CUT
;
1381 if (info
.m_state
& wxLIST_STATE_CUT
)
1382 lvItem
.state
|= LVIS_CUT
;
1384 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1386 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1387 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1388 lvItem
.state
|= LVIS_DROPHILITED
;
1390 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1392 lvItem
.stateMask
|= LVIS_FOCUSED
;
1393 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1394 lvItem
.state
|= LVIS_FOCUSED
;
1396 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1398 lvItem
.stateMask
|= LVIS_SELECTED
;
1399 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1400 lvItem
.state
|= LVIS_SELECTED
;
1404 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1406 lvItem
.mask
|= LVIF_TEXT
;
1407 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1409 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1413 lvItem
.pszText
= (char *) (const char *)info
.m_text
;
1414 if ( lvItem
.pszText
)
1415 lvItem
.cchTextMax
= info
.m_text
.Length();
1417 lvItem
.cchTextMax
= 0;
1420 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
1421 lvItem
.mask
|= LVIF_IMAGE
;
1422 if (info
.m_mask
& wxLIST_MASK_DATA
)
1423 lvItem
.mask
|= LVIF_PARAM
;
1427 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxCommandEvent
)
1429 wxListEvent::wxListEvent(wxEventType commandType
, int id
):
1430 wxCommandEvent(commandType
, id
)
1435 m_cancelled
= FALSE
;