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
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_STATE
)
487 lvItem
.mask
|= LVIF_STATE
;
488 // the other bits are hardly interesting anyhow
489 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
492 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
495 wxLogError(_("Couldn't retrieve information about list control item %d."),
500 wxConvertFromMSWListItem(this, info
, lvItem
);
504 delete[] lvItem
.pszText
;
509 // Sets information about the item
510 bool wxListCtrl::SetItem(wxListItem
& info
)
513 wxConvertToMSWListItem(this, info
, item
);
515 return (ListView_SetItem((HWND
) GetHWND(), &item
) != 0);
518 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
522 info
.m_mask
= wxLIST_MASK_TEXT
;
523 info
.m_itemId
= index
;
527 info
.m_image
= imageId
;
528 info
.m_mask
|= wxLIST_MASK_IMAGE
;
530 return SetItem(info
);
534 // Gets the item state
535 int wxListCtrl::GetItemState(long item
, long stateMask
) const
539 info
.m_mask
= wxLIST_MASK_STATE
;
540 info
.m_stateMask
= stateMask
;
541 info
.m_itemId
= item
;
549 // Sets the item state
550 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
554 info
.m_mask
= wxLIST_MASK_STATE
;
555 info
.m_state
= state
;
556 info
.m_stateMask
= stateMask
;
557 info
.m_itemId
= item
;
559 return SetItem(info
);
562 // Sets the item image
563 bool wxListCtrl::SetItemImage(long item
, int image
, int selImage
)
567 info
.m_mask
= wxLIST_MASK_IMAGE
;
568 info
.m_image
= image
;
569 info
.m_itemId
= item
;
571 return SetItem(info
);
574 // Gets the item text
575 wxString
wxListCtrl::GetItemText(long item
) const
579 info
.m_mask
= wxLIST_MASK_TEXT
;
580 info
.m_itemId
= item
;
587 // Sets the item text
588 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
592 info
.m_mask
= wxLIST_MASK_TEXT
;
593 info
.m_itemId
= item
;
599 // Gets the item data
600 long wxListCtrl::GetItemData(long item
) const
604 info
.m_mask
= wxLIST_MASK_DATA
;
605 info
.m_itemId
= item
;
612 // Sets the item data
613 bool wxListCtrl::SetItemData(long item
, long data
)
617 info
.m_mask
= wxLIST_MASK_DATA
;
618 info
.m_itemId
= item
;
621 return SetItem(info
);
624 // Gets the item rectangle
625 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
629 int code2
= LVIR_BOUNDS
;
630 if ( code
== wxLIST_RECT_BOUNDS
)
632 else if ( code
== wxLIST_RECT_ICON
)
634 else if ( code
== wxLIST_RECT_LABEL
)
637 bool success
= (ListView_GetItemRect((HWND
) GetHWND(), (int) item
, &rect2
, code2
) != 0);
641 rect
.width
= rect2
.right
- rect2
.left
;
642 rect
.height
= rect2
.bottom
- rect2
.left
;
646 // Gets the item position
647 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
651 bool success
= (ListView_GetItemPosition((HWND
) GetHWND(), (int) item
, &pt
) != 0);
653 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
657 // Sets the item position.
658 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
660 return (ListView_SetItemPosition((HWND
) GetHWND(), (int) item
, pos
.x
, pos
.y
) != 0);
663 // Gets the number of items in the list control
664 int wxListCtrl::GetItemCount(void) const
666 return ListView_GetItemCount((HWND
) GetHWND());
669 // Retrieves the spacing between icons in pixels.
670 // If small is TRUE, gets the spacing for the small icon
671 // view, otherwise the large icon view.
672 int wxListCtrl::GetItemSpacing(bool isSmall
) const
674 return ListView_GetItemSpacing((HWND
) GetHWND(), (BOOL
) isSmall
);
677 // Gets the number of selected items in the list control
678 int wxListCtrl::GetSelectedItemCount(void) const
680 return ListView_GetSelectedCount((HWND
) GetHWND());
683 // Gets the text colour of the listview
684 wxColour
wxListCtrl::GetTextColour(void) const
686 COLORREF ref
= ListView_GetTextColor((HWND
) GetHWND());
687 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
691 // Sets the text colour of the listview
692 void wxListCtrl::SetTextColour(const wxColour
& col
)
694 ListView_SetTextColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Blue(), col
.Green()));
697 // Gets the index of the topmost visible item when in
698 // list or report view
699 long wxListCtrl::GetTopItem(void) const
701 return (long) ListView_GetTopIndex((HWND
) GetHWND());
704 // Searches for an item, starting from 'item'.
705 // 'geometry' is one of
706 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
707 // 'state' is a state bit flag, one or more of
708 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
709 // item can be -1 to find the first item that matches the
711 // Returns the item or -1 if unsuccessful.
712 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
716 if ( geom
== wxLIST_NEXT_ABOVE
)
718 if ( geom
== wxLIST_NEXT_ALL
)
720 if ( geom
== wxLIST_NEXT_BELOW
)
722 if ( geom
== wxLIST_NEXT_LEFT
)
723 flags
|= LVNI_TOLEFT
;
724 if ( geom
== wxLIST_NEXT_RIGHT
)
725 flags
|= LVNI_TORIGHT
;
727 if ( state
& wxLIST_STATE_CUT
)
729 if ( state
& wxLIST_STATE_DROPHILITED
)
730 flags
|= LVNI_DROPHILITED
;
731 if ( state
& wxLIST_STATE_FOCUSED
)
732 flags
|= LVNI_FOCUSED
;
733 if ( state
& wxLIST_STATE_SELECTED
)
734 flags
|= LVNI_SELECTED
;
736 return (long) ListView_GetNextItem((HWND
) GetHWND(), item
, flags
);
740 wxImageList
*wxListCtrl::GetImageList(int which
) const
742 if ( which
== wxIMAGE_LIST_NORMAL
)
744 return m_imageListNormal
;
746 else if ( which
== wxIMAGE_LIST_SMALL
)
748 return m_imageListSmall
;
750 else if ( which
== wxIMAGE_LIST_STATE
)
752 return m_imageListState
;
757 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
760 if ( which
== wxIMAGE_LIST_NORMAL
)
762 flags
= LVSIL_NORMAL
;
763 m_imageListNormal
= imageList
;
765 else if ( which
== wxIMAGE_LIST_SMALL
)
768 m_imageListSmall
= imageList
;
770 else if ( which
== wxIMAGE_LIST_STATE
)
773 m_imageListState
= imageList
;
775 ListView_SetImageList((HWND
) GetHWND(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
779 ////////////////////////////////////////////////////////////////////////////
781 // Arranges the items
782 bool wxListCtrl::Arrange(int flag
)
785 if ( flag
== wxLIST_ALIGN_LEFT
)
786 code
= LVA_ALIGNLEFT
;
787 else if ( flag
== wxLIST_ALIGN_TOP
)
789 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
791 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
792 code
= LVA_SNAPTOGRID
;
794 return (ListView_Arrange((HWND
) GetHWND(), code
) != 0);
798 bool wxListCtrl::DeleteItem(long item
)
800 return (ListView_DeleteItem((HWND
) GetHWND(), (int) item
) != 0);
804 bool wxListCtrl::DeleteAllItems(void)
806 return (ListView_DeleteAllItems((HWND
) GetHWND()) != 0);
810 bool wxListCtrl::DeleteAllColumns(void)
813 for ( i
= 0; i
< m_colCount
; i
++)
815 if (ListView_DeleteColumn((HWND
) GetHWND(), 0) != 0)
818 return (m_colCount
== 0);
822 bool wxListCtrl::DeleteColumn(int col
)
824 bool success
= (ListView_DeleteColumn((HWND
) GetHWND(), col
) != 0);
826 if ( success
&& (m_colCount
> 0) )
831 // Clears items, and columns if there are any.
832 void wxListCtrl::ClearAll(void)
835 if ( m_colCount
> 0 )
839 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
841 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
843 HWND hWnd
= (HWND
) ListView_EditLabel((HWND
) GetHWND(), item
);
847 m_textCtrl
->UnsubclassWin();
848 m_textCtrl
->SetHWND(0);
853 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
854 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
855 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
860 // End label editing, optionally cancelling the edit
861 bool wxListCtrl::EndEditLabel(bool cancel
)
865 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
867 bool success = (ListView_EndEditLabelNow((HWND) GetHWND(), cancel) != 0);
871 m_textCtrl->UnsubclassWin();
872 m_textCtrl->SetHWND(0);
882 // Ensures this item is visible
883 bool wxListCtrl::EnsureVisible(long item
)
885 return (ListView_EnsureVisible((HWND
) GetHWND(), (int) item
, FALSE
) != 0);
888 // Find an item whose label matches this string, starting from the item after 'start'
889 // or the beginning if 'start' is -1.
890 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
892 LV_FINDINFO findInfo
;
894 findInfo
.flags
= LVFI_STRING
;
896 findInfo
.flags
|= LVFI_STRING
;
897 findInfo
.psz
= WXSTRINGCAST str
;
899 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
902 // Find an item whose data matches this data, starting from the item after 'start'
903 // or the beginning if 'start' is -1.
904 long wxListCtrl::FindItem(long start
, long data
)
906 LV_FINDINFO findInfo
;
908 findInfo
.flags
= LVFI_PARAM
;
909 findInfo
.lParam
= data
;
911 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
914 // Find an item nearest this position in the specified direction, starting from
915 // the item after 'start' or the beginning if 'start' is -1.
916 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
918 LV_FINDINFO findInfo
;
920 findInfo
.flags
= LVFI_NEARESTXY
;
921 findInfo
.pt
.x
= pt
.x
;
922 findInfo
.pt
.y
= pt
.y
;
923 findInfo
.vkDirection
= VK_RIGHT
;
925 if ( direction
== wxLIST_FIND_UP
)
926 findInfo
.vkDirection
= VK_UP
;
927 else if ( direction
== wxLIST_FIND_DOWN
)
928 findInfo
.vkDirection
= VK_DOWN
;
929 else if ( direction
== wxLIST_FIND_LEFT
)
930 findInfo
.vkDirection
= VK_LEFT
;
931 else if ( direction
== wxLIST_FIND_RIGHT
)
932 findInfo
.vkDirection
= VK_RIGHT
;
934 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
937 // Determines which item (if any) is at the specified point,
938 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
939 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
941 LV_HITTESTINFO hitTestInfo
;
942 hitTestInfo
.pt
.x
= (int) point
.x
;
943 hitTestInfo
.pt
.y
= (int) point
.y
;
945 ListView_HitTest((HWND
) GetHWND(), & hitTestInfo
);
948 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
949 flags
|= wxLIST_HITTEST_ABOVE
;
950 if ( hitTestInfo
.flags
& LVHT_BELOW
)
951 flags
|= wxLIST_HITTEST_BELOW
;
952 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
953 flags
|= wxLIST_HITTEST_NOWHERE
;
954 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
955 flags
|= wxLIST_HITTEST_ONITEMICON
;
956 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
957 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
958 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
959 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
960 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
961 flags
|= wxLIST_HITTEST_TOLEFT
;
962 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
963 flags
|= wxLIST_HITTEST_TORIGHT
;
965 return (long) hitTestInfo
.iItem
;
968 // Inserts an item, returning the index of the new item if successful,
970 long wxListCtrl::InsertItem(wxListItem
& info
)
973 wxConvertToMSWListItem(this, info
, item
);
975 return (long) ListView_InsertItem((HWND
) GetHWND(), & item
);
978 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
982 info
.m_mask
= wxLIST_MASK_TEXT
;
983 info
.m_itemId
= index
;
984 return InsertItem(info
);
987 // Inserts an image item
988 long wxListCtrl::InsertItem(long index
, int imageIndex
)
991 info
.m_image
= imageIndex
;
992 info
.m_mask
= wxLIST_MASK_IMAGE
;
993 info
.m_itemId
= index
;
994 return InsertItem(info
);
997 // Inserts an image/string item
998 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1001 info
.m_image
= imageIndex
;
1002 info
.m_text
= label
;
1003 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1004 info
.m_itemId
= index
;
1005 return InsertItem(info
);
1008 // For list view mode (only), inserts a column.
1009 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1014 lvCol
.pszText
= NULL
;
1016 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1018 lvCol
.mask
|= LVCF_TEXT
;
1019 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1020 lvCol
.cchTextMax
= 0; // Ignored
1022 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1024 lvCol
.mask
|= LVCF_FMT
;
1026 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1027 lvCol
.fmt
= LVCFMT_LEFT
;
1028 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1029 lvCol
.fmt
= LVCFMT_RIGHT
;
1030 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1031 lvCol
.fmt
= LVCFMT_CENTER
;
1034 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1036 lvCol
.mask
|= LVCF_WIDTH
;
1037 lvCol
.cx
= item
.m_width
;
1039 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
1040 lvCol
.cx
= LVSCW_AUTOSIZE
;
1041 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
1042 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1044 lvCol
.mask
|= LVCF_SUBITEM
;
1045 lvCol
.iSubItem
= col
;
1047 bool success
= (ListView_InsertColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
1053 long wxListCtrl::InsertColumn(long col
, const wxString
& heading
, int format
,
1057 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1058 item
.m_text
= heading
;
1061 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1062 item
.m_width
= width
;
1064 item
.m_format
= format
;
1066 return InsertColumn(col
, item
);
1069 // Scrolls the list control. If in icon, small icon or report view mode,
1070 // x specifies the number of pixels to scroll. If in list view mode, x
1071 // specifies the number of columns to scroll.
1072 // If in icon, small icon or list view mode, y specifies the number of pixels
1073 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1074 bool wxListCtrl::ScrollList(int dx
, int dy
)
1076 return (ListView_Scroll((HWND
) GetHWND(), dx
, dy
) != 0);
1081 // fn is a function which takes 3 long arguments: item1, item2, data.
1082 // item1 is the long data associated with a first item (NOT the index).
1083 // item2 is the long data associated with a second item (NOT the index).
1084 // data is the same value as passed to SortItems.
1085 // The return value is a negative number if the first item should precede the second
1086 // item, a positive number of the second item should precede the first,
1087 // or zero if the two items are equivalent.
1089 // data is arbitrary data to be passed to the sort function.
1090 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1092 return (ListView_SortItems((HWND
) GetHWND(), (PFNLVCOMPARE
) fn
, data
) != 0);
1095 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1097 if (cmd
== EN_UPDATE
)
1099 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1100 event
.SetEventObject( this );
1101 ProcessCommand(event
);
1104 else if (cmd
== EN_KILLFOCUS
)
1106 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1107 event
.SetEventObject( this );
1108 ProcessCommand(event
);
1114 bool wxListCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
*result
)
1116 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1117 wxEventType eventType
= wxEVT_NULL
;
1118 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1119 switch ( hdr1
->code
)
1121 case LVN_BEGINRDRAG
:
1122 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1126 if ( eventType
== wxEVT_NULL
)
1128 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1132 NM_LISTVIEW
*hdr
= (NM_LISTVIEW
*)lParam
;
1133 event
.m_itemIndex
= hdr
->iItem
;
1134 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1135 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1139 case LVN_BEGINLABELEDIT
:
1141 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1142 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1143 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1147 case LVN_COLUMNCLICK
:
1149 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1150 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1151 event
.m_itemIndex
= -1;
1152 event
.m_col
= hdr
->iSubItem
;
1155 case LVN_DELETEALLITEMS
:
1157 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1158 // NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1159 event
.m_itemIndex
= -1;
1162 case LVN_DELETEITEM
:
1164 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1165 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1166 event
.m_itemIndex
= hdr
->iItem
;
1169 case LVN_ENDLABELEDIT
:
1171 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1172 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1173 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1174 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1175 event
.m_cancelled
= TRUE
;
1178 case LVN_GETDISPINFO
:
1181 // TODO: some text buffering here, I think
1182 // TODO: API for getting Windows to retrieve values
1184 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1185 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1186 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1189 case LVN_INSERTITEM
:
1191 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1192 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1193 event
.m_itemIndex
= hdr
->iItem
;
1196 case LVN_ITEMCHANGED
:
1198 // This needs to be sent to wxListCtrl as a rather more
1199 // concrete event. For now, just detect a selection
1201 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1202 if ( (hdr
->uNewState
& LVIS_SELECTED
) && !(hdr
->uOldState
& LVIS_SELECTED
) )
1204 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1205 event
.m_itemIndex
= hdr
->iItem
;
1207 else if ( !(hdr
->uNewState
& LVIS_SELECTED
) && (hdr
->uOldState
& LVIS_SELECTED
) )
1209 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1210 event
.m_itemIndex
= hdr
->iItem
;
1218 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1219 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1220 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1223 case LVN_SETDISPINFO
:
1225 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1226 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1227 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1232 return wxControl::MSWNotify(wParam
, lParam
, result
);
1235 event
.SetEventObject( this );
1236 event
.SetEventType(eventType
);
1238 if ( !GetEventHandler()->ProcessEvent(event
) )
1241 if (hdr1
->code
== LVN_GETDISPINFO
)
1243 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1244 if ( info
->item
.mask
& LVIF_TEXT
)
1246 if ( !event
.m_item
.m_text
.IsNull() )
1248 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1249 info
->item
.cchTextMax
= strlen(info
->item
.pszText
) + 1;
1252 // wxConvertToMSWListItem(this, event.m_item, info->item);
1255 *result
= !event
.IsAllowed();
1260 char *wxListCtrl::AddPool(const wxString
& str
)
1262 // Remove the first element if 3 strings exist
1263 if ( m_stringPool
.Number() == 3 )
1265 wxNode
*node
= m_stringPool
.First();
1266 delete[] (char *)node
->Data();
1269 wxNode
*node
= m_stringPool
.Add((char *) (const char *)str
);
1270 return (char *)node
->Data();
1273 // List item structure
1274 wxListItem::wxListItem(void)
1284 m_format
= wxLIST_FORMAT_CENTRE
;
1288 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1290 info
.m_data
= lvItem
.lParam
;
1293 info
.m_stateMask
= 0;
1294 info
.m_itemId
= lvItem
.iItem
;
1296 long oldMask
= lvItem
.mask
;
1298 bool needText
= FALSE
;
1299 if (getFullInfo
!= 0)
1301 if ( lvItem
.mask
& LVIF_TEXT
)
1308 lvItem
.pszText
= new char[513];
1309 lvItem
.cchTextMax
= 512;
1311 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ;
1312 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1313 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
) ;
1316 if ( lvItem
.mask
& LVIF_STATE
)
1318 info
.m_mask
|= wxLIST_MASK_STATE
;
1320 if ( lvItem
.stateMask
& LVIS_CUT
)
1322 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1323 if ( lvItem
.state
& LVIS_CUT
)
1324 info
.m_state
|= wxLIST_STATE_CUT
;
1326 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1328 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1329 if ( lvItem
.state
& LVIS_DROPHILITED
)
1330 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1332 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1334 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1335 if ( lvItem
.state
& LVIS_FOCUSED
)
1336 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1338 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1340 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1341 if ( lvItem
.state
& LVIS_SELECTED
)
1342 info
.m_state
|= wxLIST_STATE_SELECTED
;
1346 if ( lvItem
.mask
& LVIF_TEXT
)
1348 info
.m_mask
|= wxLIST_MASK_TEXT
;
1349 info
.m_text
= lvItem
.pszText
;
1351 if ( lvItem
.mask
& LVIF_IMAGE
)
1353 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1354 info
.m_image
= lvItem
.iImage
;
1356 if ( lvItem
.mask
& LVIF_PARAM
)
1357 info
.m_mask
|= wxLIST_MASK_DATA
;
1358 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1359 info
.m_mask
|= wxLIST_SET_ITEM
;
1360 info
.m_col
= lvItem
.iSubItem
;
1365 delete[] lvItem
.pszText
;
1367 lvItem
.mask
= oldMask
;
1370 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1372 lvItem
.iItem
= (int) info
.m_itemId
;
1374 lvItem
.iImage
= info
.m_image
;
1375 lvItem
.lParam
= info
.m_data
;
1376 lvItem
.stateMask
= 0;
1379 lvItem
.iSubItem
= info
.m_col
;
1381 if (info
.m_mask
& wxLIST_MASK_STATE
)
1383 lvItem
.mask
|= LVIF_STATE
;
1384 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1386 lvItem
.stateMask
|= LVIS_CUT
;
1387 if (info
.m_state
& wxLIST_STATE_CUT
)
1388 lvItem
.state
|= LVIS_CUT
;
1390 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1392 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1393 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1394 lvItem
.state
|= LVIS_DROPHILITED
;
1396 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1398 lvItem
.stateMask
|= LVIS_FOCUSED
;
1399 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1400 lvItem
.state
|= LVIS_FOCUSED
;
1402 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1404 lvItem
.stateMask
|= LVIS_SELECTED
;
1405 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1406 lvItem
.state
|= LVIS_SELECTED
;
1410 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1412 lvItem
.mask
|= LVIF_TEXT
;
1413 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1415 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1419 lvItem
.pszText
= (char *) (const char *)info
.m_text
;
1420 if ( lvItem
.pszText
)
1421 lvItem
.cchTextMax
= info
.m_text
.Length();
1423 lvItem
.cchTextMax
= 0;
1426 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
1427 lvItem
.mask
|= LVIF_IMAGE
;
1428 if (info
.m_mask
& wxLIST_MASK_DATA
)
1429 lvItem
.mask
|= LVIF_PARAM
;
1433 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxCommandEvent
)
1435 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
1436 : wxNotifyEvent(commandType
, id
)
1441 m_cancelled
= FALSE
;