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 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1064 lvCol
.mask
|= LVCF_WIDTH
;
1065 lvCol
.cx
= item
.m_width
;
1067 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
1068 lvCol
.cx
= LVSCW_AUTOSIZE
;
1069 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
1070 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1072 lvCol
.mask
|= LVCF_SUBITEM
;
1073 lvCol
.iSubItem
= col
;
1075 bool success
= (ListView_InsertColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
1081 long wxListCtrl::InsertColumn(long col
, const wxString
& heading
, int format
,
1085 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1086 item
.m_text
= heading
;
1089 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1090 item
.m_width
= width
;
1092 item
.m_format
= format
;
1094 return InsertColumn(col
, item
);
1097 // Scrolls the list control. If in icon, small icon or report view mode,
1098 // x specifies the number of pixels to scroll. If in list view mode, x
1099 // specifies the number of columns to scroll.
1100 // If in icon, small icon or list view mode, y specifies the number of pixels
1101 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1102 bool wxListCtrl::ScrollList(int dx
, int dy
)
1104 return (ListView_Scroll((HWND
) GetHWND(), dx
, dy
) != 0);
1109 // fn is a function which takes 3 long arguments: item1, item2, data.
1110 // item1 is the long data associated with a first item (NOT the index).
1111 // item2 is the long data associated with a second item (NOT the index).
1112 // data is the same value as passed to SortItems.
1113 // The return value is a negative number if the first item should precede the second
1114 // item, a positive number of the second item should precede the first,
1115 // or zero if the two items are equivalent.
1117 // data is arbitrary data to be passed to the sort function.
1118 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1120 return (ListView_SortItems((HWND
) GetHWND(), (PFNLVCOMPARE
) fn
, data
) != 0);
1123 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1125 if (cmd
== EN_UPDATE
)
1127 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1128 event
.SetEventObject( this );
1129 ProcessCommand(event
);
1132 else if (cmd
== EN_KILLFOCUS
)
1134 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1135 event
.SetEventObject( this );
1136 ProcessCommand(event
);
1142 bool wxListCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
*result
)
1144 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1145 wxEventType eventType
= wxEVT_NULL
;
1146 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1147 switch ( hdr1
->code
)
1149 case LVN_BEGINRDRAG
:
1150 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1154 if ( eventType
== wxEVT_NULL
)
1156 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1160 NM_LISTVIEW
*hdr
= (NM_LISTVIEW
*)lParam
;
1161 event
.m_itemIndex
= hdr
->iItem
;
1162 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1163 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1167 case LVN_BEGINLABELEDIT
:
1169 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1170 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1171 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1175 case LVN_COLUMNCLICK
:
1177 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1178 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1179 event
.m_itemIndex
= -1;
1180 event
.m_col
= hdr
->iSubItem
;
1183 case LVN_DELETEALLITEMS
:
1185 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1186 // NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1187 event
.m_itemIndex
= -1;
1190 case LVN_DELETEITEM
:
1192 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1193 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1194 event
.m_itemIndex
= hdr
->iItem
;
1197 case LVN_ENDLABELEDIT
:
1199 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1200 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1201 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1202 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1203 event
.m_cancelled
= TRUE
;
1206 case LVN_GETDISPINFO
:
1209 // TODO: some text buffering here, I think
1210 // TODO: API for getting Windows to retrieve values
1212 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1213 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1214 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1217 case LVN_INSERTITEM
:
1219 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1220 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1221 event
.m_itemIndex
= hdr
->iItem
;
1224 case LVN_ITEMCHANGED
:
1226 // This needs to be sent to wxListCtrl as a rather more
1227 // concrete event. For now, just detect a selection
1229 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1230 if ( (hdr
->uNewState
& LVIS_SELECTED
) && !(hdr
->uOldState
& LVIS_SELECTED
) )
1232 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1233 event
.m_itemIndex
= hdr
->iItem
;
1235 else if ( !(hdr
->uNewState
& LVIS_SELECTED
) && (hdr
->uOldState
& LVIS_SELECTED
) )
1237 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1238 event
.m_itemIndex
= hdr
->iItem
;
1246 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1247 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1248 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1251 case LVN_SETDISPINFO
:
1253 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1254 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1255 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1260 return wxControl::MSWNotify(wParam
, lParam
, result
);
1263 event
.SetEventObject( this );
1264 event
.SetEventType(eventType
);
1266 if ( !GetEventHandler()->ProcessEvent(event
) )
1269 if (hdr1
->code
== LVN_GETDISPINFO
)
1271 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1272 if ( info
->item
.mask
& LVIF_TEXT
)
1274 if ( !event
.m_item
.m_text
.IsNull() )
1276 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1277 info
->item
.cchTextMax
= strlen(info
->item
.pszText
) + 1;
1280 // wxConvertToMSWListItem(this, event.m_item, info->item);
1283 *result
= !event
.IsAllowed();
1288 char *wxListCtrl::AddPool(const wxString
& str
)
1290 // Remove the first element if 3 strings exist
1291 if ( m_stringPool
.Number() == 3 )
1293 wxNode
*node
= m_stringPool
.First();
1294 delete[] (char *)node
->Data();
1297 wxNode
*node
= m_stringPool
.Add((char *) (const char *)str
);
1298 return (char *)node
->Data();
1301 // List item structure
1302 wxListItem::wxListItem()
1312 m_format
= wxLIST_FORMAT_CENTRE
;
1316 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1318 info
.m_data
= lvItem
.lParam
;
1321 info
.m_stateMask
= 0;
1322 info
.m_itemId
= lvItem
.iItem
;
1324 long oldMask
= lvItem
.mask
;
1326 bool needText
= FALSE
;
1327 if (getFullInfo
!= 0)
1329 if ( lvItem
.mask
& LVIF_TEXT
)
1336 lvItem
.pszText
= new char[513];
1337 lvItem
.cchTextMax
= 512;
1339 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ;
1340 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1341 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
) ;
1344 if ( lvItem
.mask
& LVIF_STATE
)
1346 info
.m_mask
|= wxLIST_MASK_STATE
;
1348 if ( lvItem
.stateMask
& LVIS_CUT
)
1350 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1351 if ( lvItem
.state
& LVIS_CUT
)
1352 info
.m_state
|= wxLIST_STATE_CUT
;
1354 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1356 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1357 if ( lvItem
.state
& LVIS_DROPHILITED
)
1358 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1360 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1362 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1363 if ( lvItem
.state
& LVIS_FOCUSED
)
1364 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1366 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1368 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1369 if ( lvItem
.state
& LVIS_SELECTED
)
1370 info
.m_state
|= wxLIST_STATE_SELECTED
;
1374 if ( lvItem
.mask
& LVIF_TEXT
)
1376 info
.m_mask
|= wxLIST_MASK_TEXT
;
1377 info
.m_text
= lvItem
.pszText
;
1379 if ( lvItem
.mask
& LVIF_IMAGE
)
1381 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1382 info
.m_image
= lvItem
.iImage
;
1384 if ( lvItem
.mask
& LVIF_PARAM
)
1385 info
.m_mask
|= wxLIST_MASK_DATA
;
1386 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1387 info
.m_mask
|= wxLIST_SET_ITEM
;
1388 info
.m_col
= lvItem
.iSubItem
;
1393 delete[] lvItem
.pszText
;
1395 lvItem
.mask
= oldMask
;
1398 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1400 lvItem
.iItem
= (int) info
.m_itemId
;
1402 lvItem
.iImage
= info
.m_image
;
1403 lvItem
.lParam
= info
.m_data
;
1404 lvItem
.stateMask
= 0;
1407 lvItem
.iSubItem
= info
.m_col
;
1409 if (info
.m_mask
& wxLIST_MASK_STATE
)
1411 lvItem
.mask
|= LVIF_STATE
;
1412 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1414 lvItem
.stateMask
|= LVIS_CUT
;
1415 if (info
.m_state
& wxLIST_STATE_CUT
)
1416 lvItem
.state
|= LVIS_CUT
;
1418 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1420 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1421 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1422 lvItem
.state
|= LVIS_DROPHILITED
;
1424 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1426 lvItem
.stateMask
|= LVIS_FOCUSED
;
1427 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1428 lvItem
.state
|= LVIS_FOCUSED
;
1430 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1432 lvItem
.stateMask
|= LVIS_SELECTED
;
1433 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1434 lvItem
.state
|= LVIS_SELECTED
;
1438 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1440 lvItem
.mask
|= LVIF_TEXT
;
1441 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1443 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1447 lvItem
.pszText
= (char *) (const char *)info
.m_text
;
1448 if ( lvItem
.pszText
)
1449 lvItem
.cchTextMax
= info
.m_text
.Length();
1451 lvItem
.cchTextMax
= 0;
1454 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
1455 lvItem
.mask
|= LVIF_IMAGE
;
1456 if (info
.m_mask
& wxLIST_MASK_DATA
)
1457 lvItem
.mask
|= LVIF_PARAM
;
1461 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
1463 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
1464 : wxNotifyEvent(commandType
, id
)
1469 m_cancelled
= FALSE
;