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"
34 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
40 #include "wx/msw/gnuwin32/extra.h"
44 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
);
45 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
, HWND getFullInfo
= 0);
47 #if !USE_SHARED_LIBRARY
48 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
49 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
50 #endif // USE_SHARED_LIBRARY
52 wxListCtrl::wxListCtrl()
54 m_imageListNormal
= NULL
;
55 m_imageListSmall
= NULL
;
56 m_imageListState
= NULL
;
62 bool wxListCtrl::Create(wxWindow
*parent
,
67 const wxValidator
& validator
,
70 m_imageListNormal
= NULL
;
71 m_imageListSmall
= NULL
;
72 m_imageListState
= NULL
;
76 SetValidator(validator
);
84 m_windowStyle
= style
;
97 m_windowId
= (id
== -1) ? NewControlId() : id
;
99 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
;
102 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
104 // Even with extended styles, need to combine with WS_BORDER
105 // for them to look right.
106 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
109 wstyle
|= LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
110 m_baseStyle
= wstyle
;
112 long oldStyle
= 0; // Dummy
113 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
115 // Create the ListView control.
116 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
121 (HWND
) parent
->GetHWND(),
127 wxLogError("Can't create list control window.");
132 // for comctl32.dll v 4.70+ we want to have this attribute because it's
133 // prettier (and also because wxGTK does it like this)
134 #ifdef ListView_SetExtendedListViewStyle
135 if ( wstyle
& LVS_REPORT
)
137 ListView_SetExtendedListViewStyle((HWND
)GetHWND(),
138 LVS_EX_FULLROWSELECT
);
140 #endif // ListView_SetExtendedListViewStyle
142 wxSystemSettings settings
;
143 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
144 SetForegroundColour(parent
->GetForegroundColour());
146 if (parent
) parent
->AddChild(this);
148 SubclassWin((WXHWND
) m_hWnd
);
153 wxListCtrl::~wxListCtrl()
157 m_textCtrl
->UnsubclassWin();
158 m_textCtrl
->SetHWND(0);
164 // Add or remove a single window style
165 void wxListCtrl::SetSingleStyle(long style
, bool add
)
167 long flag
= GetWindowStyleFlag();
169 // Get rid of conflicting styles
172 if ( style
& wxLC_MASK_TYPE
)
173 flag
= flag
& ~wxLC_MASK_TYPE
;
174 if ( style
& wxLC_MASK_ALIGN
)
175 flag
= flag
& ~wxLC_MASK_ALIGN
;
176 if ( style
& wxLC_MASK_SORT
)
177 flag
= flag
& ~wxLC_MASK_SORT
;
193 m_windowStyle
= flag
;
198 // Set the whole window style
199 void wxListCtrl::SetWindowStyleFlag(long flag
)
201 m_windowStyle
= flag
;
206 void wxListCtrl::RecreateWindow()
211 long style
= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
212 style
|= m_baseStyle
;
213 // ::SetWindowLong((HWND) GetHWND(), GWL_STYLE, style);
215 // The following recreation of the window appears to be necessary
216 // because SetWindowLong doesn't seem to do it.
218 int x
, y
, width
, height
;
220 GetSize(&width
, &height
);
223 ::DestroyWindow((HWND
) GetHWND());
226 // Recreate the ListView control: unfortunately I can't
227 // make it work by using SetWindowLong.
229 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
230 HWND hWndListControl
= CreateWindowEx(exStyle
,
235 (HWND
) GetParent()->GetHWND(),
240 m_hWnd
= (WXHWND
) hWndListControl
;
241 SubclassWin((WXHWND
) m_hWnd
);
243 #ifdef ListView_SetExtendedListViewStyle
244 if ( style
& LVS_REPORT
)
246 ListView_SetExtendedListViewStyle((HWND
)GetHWND(),
247 LVS_EX_FULLROWSELECT
);
249 #endif // ListView_SetExtendedListViewStyle
251 if ( m_imageListNormal
)
252 SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
253 if ( m_imageListSmall
)
254 SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
255 if ( m_imageListState
)
256 SetImageList(m_imageListState
, wxIMAGE_LIST_STATE
);
260 // Can be just a single style, or a bitlist
261 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
264 if ( style
& wxLC_ICON
)
266 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
267 oldStyle
-= LVS_SMALLICON
;
268 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
269 oldStyle
-= LVS_REPORT
;
270 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
271 oldStyle
-= LVS_LIST
;
275 if ( style
& wxLC_SMALL_ICON
)
277 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
278 oldStyle
-= LVS_ICON
;
279 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
280 oldStyle
-= LVS_REPORT
;
281 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
282 oldStyle
-= LVS_LIST
;
283 wstyle
|= LVS_SMALLICON
;
286 if ( style
& wxLC_LIST
)
288 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
289 oldStyle
-= LVS_ICON
;
290 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
291 oldStyle
-= LVS_REPORT
;
292 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
293 oldStyle
-= LVS_SMALLICON
;
297 if ( style
& wxLC_REPORT
)
299 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
300 oldStyle
-= LVS_ICON
;
301 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
302 oldStyle
-= LVS_LIST
;
303 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
304 oldStyle
-= LVS_SMALLICON
;
306 wstyle
|= LVS_REPORT
;
309 if ( style
& wxLC_ALIGN_LEFT
)
311 if ( oldStyle
& LVS_ALIGNTOP
)
312 oldStyle
-= LVS_ALIGNTOP
;
313 wstyle
|= LVS_ALIGNLEFT
;
316 if ( style
& wxLC_ALIGN_TOP
)
318 if ( oldStyle
& LVS_ALIGNLEFT
)
319 oldStyle
-= LVS_ALIGNLEFT
;
320 wstyle
|= LVS_ALIGNTOP
;
323 if ( style
& wxLC_AUTOARRANGE
)
324 wstyle
|= LVS_AUTOARRANGE
;
326 // Apparently, no such style (documentation wrong?)
328 if ( style & wxLC_BUTTON )
329 wstyle |= LVS_BUTTON;
332 if ( style
& wxLC_NO_SORT_HEADER
)
333 wstyle
|= LVS_NOSORTHEADER
;
335 if ( style
& wxLC_NO_HEADER
)
336 wstyle
|= LVS_NOCOLUMNHEADER
;
338 if ( style
& wxLC_EDIT_LABELS
)
339 wstyle
|= LVS_EDITLABELS
;
341 if ( style
& wxLC_SINGLE_SEL
)
342 wstyle
|= LVS_SINGLESEL
;
344 if ( style
& wxLC_SORT_ASCENDING
)
346 if ( oldStyle
& LVS_SORTDESCENDING
)
347 oldStyle
-= LVS_SORTDESCENDING
;
348 wstyle
|= LVS_SORTASCENDING
;
351 if ( style
& wxLC_SORT_DESCENDING
)
353 if ( oldStyle
& LVS_SORTASCENDING
)
354 oldStyle
-= LVS_SORTASCENDING
;
355 wstyle
|= LVS_SORTDESCENDING
;
361 // Sets the background colour (GetBackgroundColour already implicit in
363 void wxListCtrl::SetBackgroundColour(const wxColour
& col
)
365 wxWindow::SetBackgroundColour(col
);
367 ListView_SetBkColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
370 // Gets information about this column
371 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
376 lvCol
.pszText
= NULL
;
378 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
380 lvCol
.mask
|= LVCF_TEXT
;
381 lvCol
.pszText
= new char[513];
382 lvCol
.cchTextMax
= 512;
385 bool success
= (ListView_GetColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
387 // item.m_subItem = lvCol.iSubItem;
388 item
.m_width
= lvCol
.cx
;
390 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
392 item
.m_text
= lvCol
.pszText
;
393 delete[] lvCol
.pszText
;
396 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
398 if (lvCol
.fmt
== LVCFMT_LEFT
)
399 item
.m_format
= wxLIST_FORMAT_LEFT
;
400 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
401 item
.m_format
= wxLIST_FORMAT_RIGHT
;
402 else if (lvCol
.fmt
== LVCFMT_CENTER
)
403 item
.m_format
= wxLIST_FORMAT_CENTRE
;
409 // Sets information about this column
410 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
415 lvCol
.pszText
= NULL
;
417 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
419 lvCol
.mask
|= LVCF_TEXT
;
420 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
421 lvCol
.cchTextMax
= 0; // Ignored
423 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
425 lvCol
.mask
|= LVCF_FMT
;
427 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
428 lvCol
.fmt
= LVCFMT_LEFT
;
429 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
430 lvCol
.fmt
= LVCFMT_RIGHT
;
431 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
432 lvCol
.fmt
= LVCFMT_CENTER
;
435 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
437 lvCol
.mask
|= LVCF_WIDTH
;
438 lvCol
.cx
= item
.m_width
;
440 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
441 lvCol
.cx
= LVSCW_AUTOSIZE
;
442 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
443 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
445 lvCol
.mask
|= LVCF_SUBITEM
;
446 lvCol
.iSubItem
= col
;
447 return (ListView_SetColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
450 // Gets the column width
451 int wxListCtrl::GetColumnWidth(int col
) const
453 return ListView_GetColumnWidth((HWND
) GetHWND(), col
);
456 // Sets the column width
457 bool wxListCtrl::SetColumnWidth(int col
, int width
)
460 if ( m_windowStyle
& wxLC_LIST
)
464 if ( width2
== wxLIST_AUTOSIZE
)
465 width2
= LVSCW_AUTOSIZE
;
466 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
467 width2
= LVSCW_AUTOSIZE_USEHEADER
;
469 return (ListView_SetColumnWidth((HWND
) GetHWND(), col2
, width2
) != 0);
472 // Gets the number of items that can fit vertically in the
473 // visible area of the list control (list or report view)
474 // or the total number of items in the list control (icon
475 // or small icon view)
476 int wxListCtrl::GetCountPerPage(void) const
478 return ListView_GetCountPerPage((HWND
) GetHWND());
481 // Gets the edit control for editing labels.
482 wxTextCtrl
* wxListCtrl::GetEditControl(void) const
487 // Gets information about the item
488 bool wxListCtrl::GetItem(wxListItem
& info
) const
492 memset(&lvItem
, 0, sizeof(lvItem
));
494 ZeroMemory(&lvItem
, sizeof(lvItem
)); // must set all fields to 0
497 lvItem
.iItem
= info
.m_itemId
;
499 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
501 lvItem
.mask
|= LVIF_TEXT
;
502 lvItem
.pszText
= new char[513];
503 lvItem
.cchTextMax
= 512;
507 lvItem
.pszText
= NULL
;
510 if (info
.m_mask
& wxLIST_MASK_DATA
)
511 lvItem
.mask
|= LVIF_PARAM
;
513 if ( info
.m_mask
& wxLIST_MASK_STATE
)
515 lvItem
.mask
|= LVIF_STATE
;
516 // the other bits are hardly interesting anyhow
517 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
520 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
523 wxLogError(_("Couldn't retrieve information about list control item %d."),
528 wxConvertFromMSWListItem(this, info
, lvItem
);
532 delete[] lvItem
.pszText
;
537 // Sets information about the item
538 bool wxListCtrl::SetItem(wxListItem
& info
)
541 wxConvertToMSWListItem(this, info
, item
);
543 return (ListView_SetItem((HWND
) GetHWND(), &item
) != 0);
546 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
550 info
.m_mask
= wxLIST_MASK_TEXT
;
551 info
.m_itemId
= index
;
555 info
.m_image
= imageId
;
556 info
.m_mask
|= wxLIST_MASK_IMAGE
;
558 return SetItem(info
);
562 // Gets the item state
563 int wxListCtrl::GetItemState(long item
, long stateMask
) const
567 info
.m_mask
= wxLIST_MASK_STATE
;
568 info
.m_stateMask
= stateMask
;
569 info
.m_itemId
= item
;
577 // Sets the item state
578 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
582 info
.m_mask
= wxLIST_MASK_STATE
;
583 info
.m_state
= state
;
584 info
.m_stateMask
= stateMask
;
585 info
.m_itemId
= item
;
587 return SetItem(info
);
590 // Sets the item image
591 bool wxListCtrl::SetItemImage(long item
, int image
, int selImage
)
595 info
.m_mask
= wxLIST_MASK_IMAGE
;
596 info
.m_image
= image
;
597 info
.m_itemId
= item
;
599 return SetItem(info
);
602 // Gets the item text
603 wxString
wxListCtrl::GetItemText(long item
) const
607 info
.m_mask
= wxLIST_MASK_TEXT
;
608 info
.m_itemId
= item
;
615 // Sets the item text
616 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
620 info
.m_mask
= wxLIST_MASK_TEXT
;
621 info
.m_itemId
= item
;
627 // Gets the item data
628 long wxListCtrl::GetItemData(long item
) const
632 info
.m_mask
= wxLIST_MASK_DATA
;
633 info
.m_itemId
= item
;
640 // Sets the item data
641 bool wxListCtrl::SetItemData(long item
, long data
)
645 info
.m_mask
= wxLIST_MASK_DATA
;
646 info
.m_itemId
= item
;
649 return SetItem(info
);
652 // Gets the item rectangle
653 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
657 int code2
= LVIR_BOUNDS
;
658 if ( code
== wxLIST_RECT_BOUNDS
)
660 else if ( code
== wxLIST_RECT_ICON
)
662 else if ( code
== wxLIST_RECT_LABEL
)
665 bool success
= (ListView_GetItemRect((HWND
) GetHWND(), (int) item
, &rect2
, code2
) != 0);
669 rect
.width
= rect2
.right
- rect2
.left
;
670 rect
.height
= rect2
.bottom
- rect2
.left
;
674 // Gets the item position
675 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
679 bool success
= (ListView_GetItemPosition((HWND
) GetHWND(), (int) item
, &pt
) != 0);
681 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
685 // Sets the item position.
686 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
688 return (ListView_SetItemPosition((HWND
) GetHWND(), (int) item
, pos
.x
, pos
.y
) != 0);
691 // Gets the number of items in the list control
692 int wxListCtrl::GetItemCount(void) const
694 return ListView_GetItemCount((HWND
) GetHWND());
697 // Retrieves the spacing between icons in pixels.
698 // If small is TRUE, gets the spacing for the small icon
699 // view, otherwise the large icon view.
700 int wxListCtrl::GetItemSpacing(bool isSmall
) const
702 return ListView_GetItemSpacing((HWND
) GetHWND(), (BOOL
) isSmall
);
705 // Gets the number of selected items in the list control
706 int wxListCtrl::GetSelectedItemCount(void) const
708 return ListView_GetSelectedCount((HWND
) GetHWND());
711 // Gets the text colour of the listview
712 wxColour
wxListCtrl::GetTextColour(void) const
714 COLORREF ref
= ListView_GetTextColor((HWND
) GetHWND());
715 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
719 // Sets the text colour of the listview
720 void wxListCtrl::SetTextColour(const wxColour
& col
)
722 ListView_SetTextColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Blue(), col
.Green()));
725 // Gets the index of the topmost visible item when in
726 // list or report view
727 long wxListCtrl::GetTopItem(void) const
729 return (long) ListView_GetTopIndex((HWND
) GetHWND());
732 // Searches for an item, starting from 'item'.
733 // 'geometry' is one of
734 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
735 // 'state' is a state bit flag, one or more of
736 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
737 // item can be -1 to find the first item that matches the
739 // Returns the item or -1 if unsuccessful.
740 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
744 if ( geom
== wxLIST_NEXT_ABOVE
)
746 if ( geom
== wxLIST_NEXT_ALL
)
748 if ( geom
== wxLIST_NEXT_BELOW
)
750 if ( geom
== wxLIST_NEXT_LEFT
)
751 flags
|= LVNI_TOLEFT
;
752 if ( geom
== wxLIST_NEXT_RIGHT
)
753 flags
|= LVNI_TORIGHT
;
755 if ( state
& wxLIST_STATE_CUT
)
757 if ( state
& wxLIST_STATE_DROPHILITED
)
758 flags
|= LVNI_DROPHILITED
;
759 if ( state
& wxLIST_STATE_FOCUSED
)
760 flags
|= LVNI_FOCUSED
;
761 if ( state
& wxLIST_STATE_SELECTED
)
762 flags
|= LVNI_SELECTED
;
764 return (long) ListView_GetNextItem((HWND
) GetHWND(), item
, flags
);
768 wxImageList
*wxListCtrl::GetImageList(int which
) const
770 if ( which
== wxIMAGE_LIST_NORMAL
)
772 return m_imageListNormal
;
774 else if ( which
== wxIMAGE_LIST_SMALL
)
776 return m_imageListSmall
;
778 else if ( which
== wxIMAGE_LIST_STATE
)
780 return m_imageListState
;
785 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
788 if ( which
== wxIMAGE_LIST_NORMAL
)
790 flags
= LVSIL_NORMAL
;
791 m_imageListNormal
= imageList
;
793 else if ( which
== wxIMAGE_LIST_SMALL
)
796 m_imageListSmall
= imageList
;
798 else if ( which
== wxIMAGE_LIST_STATE
)
801 m_imageListState
= imageList
;
803 ListView_SetImageList((HWND
) GetHWND(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
807 ////////////////////////////////////////////////////////////////////////////
809 // Arranges the items
810 bool wxListCtrl::Arrange(int flag
)
813 if ( flag
== wxLIST_ALIGN_LEFT
)
814 code
= LVA_ALIGNLEFT
;
815 else if ( flag
== wxLIST_ALIGN_TOP
)
817 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
819 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
820 code
= LVA_SNAPTOGRID
;
822 return (ListView_Arrange((HWND
) GetHWND(), code
) != 0);
826 bool wxListCtrl::DeleteItem(long item
)
828 return (ListView_DeleteItem((HWND
) GetHWND(), (int) item
) != 0);
832 bool wxListCtrl::DeleteAllItems()
834 return (ListView_DeleteAllItems((HWND
) GetHWND()) != 0);
838 bool wxListCtrl::DeleteAllColumns()
841 for ( i
= 0; i
< m_colCount
; i
++)
843 if (ListView_DeleteColumn((HWND
) GetHWND(), 0) != 0)
846 return (m_colCount
== 0);
850 bool wxListCtrl::DeleteColumn(int col
)
852 bool success
= (ListView_DeleteColumn((HWND
) GetHWND(), col
) != 0);
854 if ( success
&& (m_colCount
> 0) )
859 // Clears items, and columns if there are any.
860 void wxListCtrl::ClearAll()
863 if ( m_colCount
> 0 )
867 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
869 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
871 HWND hWnd
= (HWND
) ListView_EditLabel((HWND
) GetHWND(), item
);
875 m_textCtrl
->UnsubclassWin();
876 m_textCtrl
->SetHWND(0);
881 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
882 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
883 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
888 // End label editing, optionally cancelling the edit
889 bool wxListCtrl::EndEditLabel(bool cancel
)
893 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
895 bool success = (ListView_EndEditLabelNow((HWND) GetHWND(), cancel) != 0);
899 m_textCtrl->UnsubclassWin();
900 m_textCtrl->SetHWND(0);
910 // Ensures this item is visible
911 bool wxListCtrl::EnsureVisible(long item
)
913 return (ListView_EnsureVisible((HWND
) GetHWND(), (int) item
, FALSE
) != 0);
916 // Find an item whose label matches this string, starting from the item after 'start'
917 // or the beginning if 'start' is -1.
918 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
920 LV_FINDINFO findInfo
;
922 findInfo
.flags
= LVFI_STRING
;
924 findInfo
.flags
|= LVFI_STRING
;
925 findInfo
.psz
= WXSTRINGCAST str
;
927 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
930 // Find an item whose data matches this data, starting from the item after 'start'
931 // or the beginning if 'start' is -1.
932 long wxListCtrl::FindItem(long start
, long data
)
934 LV_FINDINFO findInfo
;
936 findInfo
.flags
= LVFI_PARAM
;
937 findInfo
.lParam
= data
;
939 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
942 // Find an item nearest this position in the specified direction, starting from
943 // the item after 'start' or the beginning if 'start' is -1.
944 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
946 LV_FINDINFO findInfo
;
948 findInfo
.flags
= LVFI_NEARESTXY
;
949 findInfo
.pt
.x
= pt
.x
;
950 findInfo
.pt
.y
= pt
.y
;
951 findInfo
.vkDirection
= VK_RIGHT
;
953 if ( direction
== wxLIST_FIND_UP
)
954 findInfo
.vkDirection
= VK_UP
;
955 else if ( direction
== wxLIST_FIND_DOWN
)
956 findInfo
.vkDirection
= VK_DOWN
;
957 else if ( direction
== wxLIST_FIND_LEFT
)
958 findInfo
.vkDirection
= VK_LEFT
;
959 else if ( direction
== wxLIST_FIND_RIGHT
)
960 findInfo
.vkDirection
= VK_RIGHT
;
962 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
965 // Determines which item (if any) is at the specified point,
966 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
967 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
969 LV_HITTESTINFO hitTestInfo
;
970 hitTestInfo
.pt
.x
= (int) point
.x
;
971 hitTestInfo
.pt
.y
= (int) point
.y
;
973 ListView_HitTest((HWND
) GetHWND(), & hitTestInfo
);
976 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
977 flags
|= wxLIST_HITTEST_ABOVE
;
978 if ( hitTestInfo
.flags
& LVHT_BELOW
)
979 flags
|= wxLIST_HITTEST_BELOW
;
980 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
981 flags
|= wxLIST_HITTEST_NOWHERE
;
982 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
983 flags
|= wxLIST_HITTEST_ONITEMICON
;
984 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
985 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
986 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
987 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
988 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
989 flags
|= wxLIST_HITTEST_TOLEFT
;
990 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
991 flags
|= wxLIST_HITTEST_TORIGHT
;
993 return (long) hitTestInfo
.iItem
;
996 // Inserts an item, returning the index of the new item if successful,
998 long wxListCtrl::InsertItem(wxListItem
& info
)
1001 wxConvertToMSWListItem(this, info
, item
);
1003 return (long) ListView_InsertItem((HWND
) GetHWND(), & item
);
1006 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1009 info
.m_text
= label
;
1010 info
.m_mask
= wxLIST_MASK_TEXT
;
1011 info
.m_itemId
= index
;
1012 return InsertItem(info
);
1015 // Inserts an image item
1016 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1019 info
.m_image
= imageIndex
;
1020 info
.m_mask
= wxLIST_MASK_IMAGE
;
1021 info
.m_itemId
= index
;
1022 return InsertItem(info
);
1025 // Inserts an image/string item
1026 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1029 info
.m_image
= imageIndex
;
1030 info
.m_text
= label
;
1031 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1032 info
.m_itemId
= index
;
1033 return InsertItem(info
);
1036 // For list view mode (only), inserts a column.
1037 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1042 lvCol
.pszText
= NULL
;
1044 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1046 lvCol
.mask
|= LVCF_TEXT
;
1047 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1048 lvCol
.cchTextMax
= 0; // Ignored
1050 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1052 lvCol
.mask
|= LVCF_FMT
;
1054 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1055 lvCol
.fmt
= LVCFMT_LEFT
;
1056 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1057 lvCol
.fmt
= LVCFMT_RIGHT
;
1058 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1059 lvCol
.fmt
= LVCFMT_CENTER
;
1062 lvCol
.mask
|= LVCF_WIDTH
;
1063 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1065 if ( item
.m_width
== wxLIST_AUTOSIZE
)
1066 lvCol
.cx
= LVSCW_AUTOSIZE
;
1067 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
1068 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1070 lvCol
.cx
= item
.m_width
;
1074 // always give some width to the new column: this one is compatible
1079 lvCol
.mask
|= LVCF_SUBITEM
;
1080 lvCol
.iSubItem
= col
;
1082 bool success
= ListView_InsertColumn((HWND
) GetHWND(), col
, & lvCol
) != -1;
1089 wxLogDebug("Failed to insert the column '%s' into listview!",
1096 long wxListCtrl::InsertColumn(long col
, const wxString
& heading
, int format
,
1100 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1101 item
.m_text
= heading
;
1104 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1105 item
.m_width
= width
;
1107 item
.m_format
= format
;
1109 return InsertColumn(col
, item
);
1112 // Scrolls the list control. If in icon, small icon or report view mode,
1113 // x specifies the number of pixels to scroll. If in list view mode, x
1114 // specifies the number of columns to scroll.
1115 // If in icon, small icon or list view mode, y specifies the number of pixels
1116 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1117 bool wxListCtrl::ScrollList(int dx
, int dy
)
1119 return (ListView_Scroll((HWND
) GetHWND(), dx
, dy
) != 0);
1124 // fn is a function which takes 3 long arguments: item1, item2, data.
1125 // item1 is the long data associated with a first item (NOT the index).
1126 // item2 is the long data associated with a second item (NOT the index).
1127 // data is the same value as passed to SortItems.
1128 // The return value is a negative number if the first item should precede the second
1129 // item, a positive number of the second item should precede the first,
1130 // or zero if the two items are equivalent.
1132 // data is arbitrary data to be passed to the sort function.
1133 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1135 return (ListView_SortItems((HWND
) GetHWND(), (PFNLVCOMPARE
) fn
, data
) != 0);
1138 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1140 if (cmd
== EN_UPDATE
)
1142 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1143 event
.SetEventObject( this );
1144 ProcessCommand(event
);
1147 else if (cmd
== EN_KILLFOCUS
)
1149 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1150 event
.SetEventObject( this );
1151 ProcessCommand(event
);
1157 bool wxListCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
*result
)
1159 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1160 wxEventType eventType
= wxEVT_NULL
;
1161 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1162 switch ( hdr1
->code
)
1164 case LVN_BEGINRDRAG
:
1165 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1169 if ( eventType
== wxEVT_NULL
)
1171 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1175 NM_LISTVIEW
*hdr
= (NM_LISTVIEW
*)lParam
;
1176 event
.m_itemIndex
= hdr
->iItem
;
1177 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1178 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1182 case LVN_BEGINLABELEDIT
:
1184 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1185 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1186 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1190 case LVN_COLUMNCLICK
:
1192 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1193 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1194 event
.m_itemIndex
= -1;
1195 event
.m_col
= hdr
->iSubItem
;
1198 case LVN_DELETEALLITEMS
:
1200 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1201 // NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1202 event
.m_itemIndex
= -1;
1205 case LVN_DELETEITEM
:
1207 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1208 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1209 event
.m_itemIndex
= hdr
->iItem
;
1212 case LVN_ENDLABELEDIT
:
1214 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1215 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1216 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1217 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1218 event
.m_cancelled
= TRUE
;
1221 case LVN_GETDISPINFO
:
1224 // TODO: some text buffering here, I think
1225 // TODO: API for getting Windows to retrieve values
1227 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1228 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1229 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1232 case LVN_INSERTITEM
:
1234 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1235 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1236 event
.m_itemIndex
= hdr
->iItem
;
1239 case LVN_ITEMCHANGED
:
1241 // This needs to be sent to wxListCtrl as a rather more
1242 // concrete event. For now, just detect a selection
1244 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1245 if ( (hdr
->uNewState
& LVIS_SELECTED
) && !(hdr
->uOldState
& LVIS_SELECTED
) )
1247 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1248 event
.m_itemIndex
= hdr
->iItem
;
1250 else if ( !(hdr
->uNewState
& LVIS_SELECTED
) && (hdr
->uOldState
& LVIS_SELECTED
) )
1252 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1253 event
.m_itemIndex
= hdr
->iItem
;
1261 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1262 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1263 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1266 case LVN_SETDISPINFO
:
1268 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1269 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1270 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1275 return wxControl::MSWNotify(wParam
, lParam
, result
);
1278 event
.SetEventObject( this );
1279 event
.SetEventType(eventType
);
1281 if ( !GetEventHandler()->ProcessEvent(event
) )
1284 if (hdr1
->code
== LVN_GETDISPINFO
)
1286 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1287 if ( info
->item
.mask
& LVIF_TEXT
)
1289 if ( !event
.m_item
.m_text
.IsNull() )
1291 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1292 info
->item
.cchTextMax
= strlen(info
->item
.pszText
) + 1;
1295 // wxConvertToMSWListItem(this, event.m_item, info->item);
1298 *result
= !event
.IsAllowed();
1303 char *wxListCtrl::AddPool(const wxString
& str
)
1305 // Remove the first element if 3 strings exist
1306 if ( m_stringPool
.Number() == 3 )
1308 wxNode
*node
= m_stringPool
.First();
1309 delete[] (char *)node
->Data();
1312 wxNode
*node
= m_stringPool
.Add((char *) (const char *)str
);
1313 return (char *)node
->Data();
1316 // List item structure
1317 wxListItem::wxListItem()
1327 m_format
= wxLIST_FORMAT_CENTRE
;
1331 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1333 info
.m_data
= lvItem
.lParam
;
1336 info
.m_stateMask
= 0;
1337 info
.m_itemId
= lvItem
.iItem
;
1339 long oldMask
= lvItem
.mask
;
1341 bool needText
= FALSE
;
1342 if (getFullInfo
!= 0)
1344 if ( lvItem
.mask
& LVIF_TEXT
)
1351 lvItem
.pszText
= new char[513];
1352 lvItem
.cchTextMax
= 512;
1354 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ;
1355 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1356 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
) ;
1359 if ( lvItem
.mask
& LVIF_STATE
)
1361 info
.m_mask
|= wxLIST_MASK_STATE
;
1363 if ( lvItem
.stateMask
& LVIS_CUT
)
1365 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1366 if ( lvItem
.state
& LVIS_CUT
)
1367 info
.m_state
|= wxLIST_STATE_CUT
;
1369 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1371 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1372 if ( lvItem
.state
& LVIS_DROPHILITED
)
1373 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1375 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1377 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1378 if ( lvItem
.state
& LVIS_FOCUSED
)
1379 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1381 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1383 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1384 if ( lvItem
.state
& LVIS_SELECTED
)
1385 info
.m_state
|= wxLIST_STATE_SELECTED
;
1389 if ( lvItem
.mask
& LVIF_TEXT
)
1391 info
.m_mask
|= wxLIST_MASK_TEXT
;
1392 info
.m_text
= lvItem
.pszText
;
1394 if ( lvItem
.mask
& LVIF_IMAGE
)
1396 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1397 info
.m_image
= lvItem
.iImage
;
1399 if ( lvItem
.mask
& LVIF_PARAM
)
1400 info
.m_mask
|= wxLIST_MASK_DATA
;
1401 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1402 info
.m_mask
|= wxLIST_SET_ITEM
;
1403 info
.m_col
= lvItem
.iSubItem
;
1408 delete[] lvItem
.pszText
;
1410 lvItem
.mask
= oldMask
;
1413 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1415 lvItem
.iItem
= (int) info
.m_itemId
;
1417 lvItem
.iImage
= info
.m_image
;
1418 lvItem
.lParam
= info
.m_data
;
1419 lvItem
.stateMask
= 0;
1422 lvItem
.iSubItem
= info
.m_col
;
1424 if (info
.m_mask
& wxLIST_MASK_STATE
)
1426 lvItem
.mask
|= LVIF_STATE
;
1427 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1429 lvItem
.stateMask
|= LVIS_CUT
;
1430 if (info
.m_state
& wxLIST_STATE_CUT
)
1431 lvItem
.state
|= LVIS_CUT
;
1433 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1435 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1436 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1437 lvItem
.state
|= LVIS_DROPHILITED
;
1439 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1441 lvItem
.stateMask
|= LVIS_FOCUSED
;
1442 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1443 lvItem
.state
|= LVIS_FOCUSED
;
1445 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1447 lvItem
.stateMask
|= LVIS_SELECTED
;
1448 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1449 lvItem
.state
|= LVIS_SELECTED
;
1453 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1455 lvItem
.mask
|= LVIF_TEXT
;
1456 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1458 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1462 lvItem
.pszText
= (char *) (const char *)info
.m_text
;
1463 if ( lvItem
.pszText
)
1464 lvItem
.cchTextMax
= info
.m_text
.Length();
1466 lvItem
.cchTextMax
= 0;
1469 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
1470 lvItem
.mask
|= LVIF_IMAGE
;
1471 if (info
.m_mask
& wxLIST_MASK_DATA
)
1472 lvItem
.mask
|= LVIF_PARAM
;
1476 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
1478 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
1479 : wxNotifyEvent(commandType
, id
)
1484 m_cancelled
= FALSE
;