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
)
53 wxListCtrl::wxListCtrl(void)
55 m_imageListNormal
= NULL
;
56 m_imageListSmall
= NULL
;
57 m_imageListState
= NULL
;
63 bool wxListCtrl::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
64 long style
, const wxValidator
& validator
, const wxString
& name
)
66 m_imageListNormal
= NULL
;
67 m_imageListSmall
= NULL
;
68 m_imageListState
= NULL
;
72 SetValidator(validator
);
80 m_windowStyle
= style
;
93 m_windowId
= (id
== -1) ? NewControlId() : id
;
95 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
;
98 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
100 // Even with extended styles, need to combine with WS_BORDER
101 // for them to look right.
102 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
105 wstyle
|= LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
106 m_baseStyle
= wstyle
;
108 long oldStyle
= 0; // Dummy
109 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
111 // Create the ListView control.
112 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
117 (HWND
) parent
->GetHWND(),
123 wxLogError("Can't create list control window.");
128 wxSystemSettings settings
;
129 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
130 SetForegroundColour(parent
->GetForegroundColour());
132 if (parent
) parent
->AddChild(this);
134 SubclassWin((WXHWND
) m_hWnd
);
139 wxListCtrl::~wxListCtrl(void)
143 m_textCtrl
->UnsubclassWin();
144 m_textCtrl
->SetHWND(0);
150 // Add or remove a single window style
151 void wxListCtrl::SetSingleStyle(long style
, bool add
)
153 long flag
= GetWindowStyleFlag();
155 // Get rid of conflicting styles
158 if ( style
& wxLC_MASK_TYPE
)
159 flag
= flag
& ~wxLC_MASK_TYPE
;
160 if ( style
& wxLC_MASK_ALIGN
)
161 flag
= flag
& ~wxLC_MASK_ALIGN
;
162 if ( style
& wxLC_MASK_SORT
)
163 flag
= flag
& ~wxLC_MASK_SORT
;
179 m_windowStyle
= flag
;
184 // Set the whole window style
185 void wxListCtrl::SetWindowStyleFlag(long flag
)
187 m_windowStyle
= flag
;
192 void wxListCtrl::RecreateWindow(void)
197 long style
= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
198 style
|= m_baseStyle
;
199 // ::SetWindowLong((HWND) GetHWND(), GWL_STYLE, style);
201 // The following recreation of the window appears to be necessary
202 // because SetWindowLong doesn't seem to do it.
204 int x
, y
, width
, height
;
206 GetSize(&width
, &height
);
209 ::DestroyWindow((HWND
) GetHWND());
212 // Recreate the ListView control: unfortunately I can't
213 // make it work by using SetWindowLong.
215 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
216 HWND hWndListControl
= CreateWindowEx(exStyle
,
221 (HWND
) GetParent()->GetHWND(),
226 m_hWnd
= (WXHWND
) hWndListControl
;
227 SubclassWin((WXHWND
) m_hWnd
);
229 if ( m_imageListNormal
)
230 SetImageList(m_imageListNormal
, wxIMAGE_LIST_NORMAL
);
231 if ( m_imageListSmall
)
232 SetImageList(m_imageListSmall
, wxIMAGE_LIST_SMALL
);
233 if ( m_imageListState
)
234 SetImageList(m_imageListState
, wxIMAGE_LIST_STATE
);
238 // Can be just a single style, or a bitlist
239 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
242 if ( style
& wxLC_ICON
)
244 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
245 oldStyle
-= LVS_SMALLICON
;
246 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
247 oldStyle
-= LVS_REPORT
;
248 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
249 oldStyle
-= LVS_LIST
;
253 if ( style
& wxLC_SMALL_ICON
)
255 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
256 oldStyle
-= LVS_ICON
;
257 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
258 oldStyle
-= LVS_REPORT
;
259 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
260 oldStyle
-= LVS_LIST
;
261 wstyle
|= LVS_SMALLICON
;
264 if ( style
& wxLC_LIST
)
266 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
267 oldStyle
-= LVS_ICON
;
268 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
269 oldStyle
-= LVS_REPORT
;
270 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
271 oldStyle
-= LVS_SMALLICON
;
275 if ( style
& wxLC_REPORT
)
277 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
278 oldStyle
-= LVS_ICON
;
279 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
280 oldStyle
-= LVS_LIST
;
281 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
282 oldStyle
-= LVS_SMALLICON
;
283 wstyle
|= LVS_REPORT
;
286 if ( style
& wxLC_ALIGN_LEFT
)
288 if ( oldStyle
& LVS_ALIGNTOP
)
289 oldStyle
-= LVS_ALIGNTOP
;
290 wstyle
|= LVS_ALIGNLEFT
;
293 if ( style
& wxLC_ALIGN_TOP
)
295 if ( oldStyle
& LVS_ALIGNLEFT
)
296 oldStyle
-= LVS_ALIGNLEFT
;
297 wstyle
|= LVS_ALIGNTOP
;
300 if ( style
& wxLC_AUTOARRANGE
)
301 wstyle
|= LVS_AUTOARRANGE
;
303 // Apparently, no such style (documentation wrong?)
305 if ( style & wxLC_BUTTON )
306 wstyle |= LVS_BUTTON;
309 if ( style
& wxLC_NO_SORT_HEADER
)
310 wstyle
|= LVS_NOSORTHEADER
;
312 if ( style
& wxLC_NO_HEADER
)
313 wstyle
|= LVS_NOCOLUMNHEADER
;
315 if ( style
& wxLC_EDIT_LABELS
)
316 wstyle
|= LVS_EDITLABELS
;
318 if ( style
& wxLC_SINGLE_SEL
)
319 wstyle
|= LVS_SINGLESEL
;
321 if ( style
& wxLC_SORT_ASCENDING
)
323 if ( oldStyle
& LVS_SORTDESCENDING
)
324 oldStyle
-= LVS_SORTDESCENDING
;
325 wstyle
|= LVS_SORTASCENDING
;
328 if ( style
& wxLC_SORT_DESCENDING
)
330 if ( oldStyle
& LVS_SORTASCENDING
)
331 oldStyle
-= LVS_SORTASCENDING
;
332 wstyle
|= LVS_SORTDESCENDING
;
338 // Sets the background colour (GetBackgroundColour already implicit in
340 void wxListCtrl::SetBackgroundColour(const wxColour
& col
)
342 wxWindow::SetBackgroundColour(col
);
344 ListView_SetBkColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
347 // Gets information about this column
348 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
353 lvCol
.pszText
= NULL
;
355 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
357 lvCol
.mask
|= LVCF_TEXT
;
358 lvCol
.pszText
= new char[513];
359 lvCol
.cchTextMax
= 512;
362 bool success
= (ListView_GetColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
364 // item.m_subItem = lvCol.iSubItem;
365 item
.m_width
= lvCol
.cx
;
367 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
369 item
.m_text
= lvCol
.pszText
;
370 delete[] lvCol
.pszText
;
373 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
375 if (lvCol
.fmt
== LVCFMT_LEFT
)
376 item
.m_format
= wxLIST_FORMAT_LEFT
;
377 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
378 item
.m_format
= wxLIST_FORMAT_RIGHT
;
379 else if (lvCol
.fmt
== LVCFMT_CENTER
)
380 item
.m_format
= wxLIST_FORMAT_CENTRE
;
386 // Sets information about this column
387 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
392 lvCol
.pszText
= NULL
;
394 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
396 lvCol
.mask
|= LVCF_TEXT
;
397 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
398 lvCol
.cchTextMax
= 0; // Ignored
400 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
402 lvCol
.mask
|= LVCF_FMT
;
404 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
405 lvCol
.fmt
= LVCFMT_LEFT
;
406 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
407 lvCol
.fmt
= LVCFMT_RIGHT
;
408 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
409 lvCol
.fmt
= LVCFMT_CENTER
;
412 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
414 lvCol
.mask
|= LVCF_WIDTH
;
415 lvCol
.cx
= item
.m_width
;
417 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
418 lvCol
.cx
= LVSCW_AUTOSIZE
;
419 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
420 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
422 lvCol
.mask
|= LVCF_SUBITEM
;
423 lvCol
.iSubItem
= col
;
424 return (ListView_SetColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
427 // Gets the column width
428 int wxListCtrl::GetColumnWidth(int col
) const
430 return ListView_GetColumnWidth((HWND
) GetHWND(), col
);
433 // Sets the column width
434 bool wxListCtrl::SetColumnWidth(int col
, int width
)
437 if ( m_windowStyle
& wxLC_LIST
)
441 if ( width2
== wxLIST_AUTOSIZE
)
442 width2
= LVSCW_AUTOSIZE
;
443 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
444 width2
= LVSCW_AUTOSIZE_USEHEADER
;
446 return (ListView_SetColumnWidth((HWND
) GetHWND(), col2
, width2
) != 0);
449 // Gets the number of items that can fit vertically in the
450 // visible area of the list control (list or report view)
451 // or the total number of items in the list control (icon
452 // or small icon view)
453 int wxListCtrl::GetCountPerPage(void) const
455 return ListView_GetCountPerPage((HWND
) GetHWND());
458 // Gets the edit control for editing labels.
459 wxTextCtrl
* wxListCtrl::GetEditControl(void) const
464 // Gets information about the item
465 bool wxListCtrl::GetItem(wxListItem
& info
) const
469 memset(&lvItem
, 0, sizeof(lvItem
));
471 ZeroMemory(&lvItem
, sizeof(lvItem
)); // must set all fields to 0
474 lvItem
.iItem
= info
.m_itemId
;
476 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
478 lvItem
.mask
|= LVIF_TEXT
;
479 lvItem
.pszText
= new char[513];
480 lvItem
.cchTextMax
= 512;
484 lvItem
.pszText
= NULL
;
487 if (info
.m_mask
& wxLIST_MASK_DATA
)
488 lvItem
.mask
|= LVIF_PARAM
;
490 if ( info
.m_mask
& wxLIST_MASK_STATE
)
492 lvItem
.mask
|= LVIF_STATE
;
493 // the other bits are hardly interesting anyhow
494 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
497 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
500 wxLogError(_("Couldn't retrieve information about list control item %d."),
505 wxConvertFromMSWListItem(this, info
, lvItem
);
509 delete[] lvItem
.pszText
;
514 // Sets information about the item
515 bool wxListCtrl::SetItem(wxListItem
& info
)
518 wxConvertToMSWListItem(this, info
, item
);
520 return (ListView_SetItem((HWND
) GetHWND(), &item
) != 0);
523 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
527 info
.m_mask
= wxLIST_MASK_TEXT
;
528 info
.m_itemId
= index
;
532 info
.m_image
= imageId
;
533 info
.m_mask
|= wxLIST_MASK_IMAGE
;
535 return SetItem(info
);
539 // Gets the item state
540 int wxListCtrl::GetItemState(long item
, long stateMask
) const
544 info
.m_mask
= wxLIST_MASK_STATE
;
545 info
.m_stateMask
= stateMask
;
546 info
.m_itemId
= item
;
554 // Sets the item state
555 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
559 info
.m_mask
= wxLIST_MASK_STATE
;
560 info
.m_state
= state
;
561 info
.m_stateMask
= stateMask
;
562 info
.m_itemId
= item
;
564 return SetItem(info
);
567 // Sets the item image
568 bool wxListCtrl::SetItemImage(long item
, int image
, int selImage
)
572 info
.m_mask
= wxLIST_MASK_IMAGE
;
573 info
.m_image
= image
;
574 info
.m_itemId
= item
;
576 return SetItem(info
);
579 // Gets the item text
580 wxString
wxListCtrl::GetItemText(long item
) const
584 info
.m_mask
= wxLIST_MASK_TEXT
;
585 info
.m_itemId
= item
;
592 // Sets the item text
593 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
597 info
.m_mask
= wxLIST_MASK_TEXT
;
598 info
.m_itemId
= item
;
604 // Gets the item data
605 long wxListCtrl::GetItemData(long item
) const
609 info
.m_mask
= wxLIST_MASK_DATA
;
610 info
.m_itemId
= item
;
617 // Sets the item data
618 bool wxListCtrl::SetItemData(long item
, long data
)
622 info
.m_mask
= wxLIST_MASK_DATA
;
623 info
.m_itemId
= item
;
626 return SetItem(info
);
629 // Gets the item rectangle
630 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
634 int code2
= LVIR_BOUNDS
;
635 if ( code
== wxLIST_RECT_BOUNDS
)
637 else if ( code
== wxLIST_RECT_ICON
)
639 else if ( code
== wxLIST_RECT_LABEL
)
642 bool success
= (ListView_GetItemRect((HWND
) GetHWND(), (int) item
, &rect2
, code2
) != 0);
646 rect
.width
= rect2
.right
- rect2
.left
;
647 rect
.height
= rect2
.bottom
- rect2
.left
;
651 // Gets the item position
652 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
656 bool success
= (ListView_GetItemPosition((HWND
) GetHWND(), (int) item
, &pt
) != 0);
658 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
662 // Sets the item position.
663 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
665 return (ListView_SetItemPosition((HWND
) GetHWND(), (int) item
, pos
.x
, pos
.y
) != 0);
668 // Gets the number of items in the list control
669 int wxListCtrl::GetItemCount(void) const
671 return ListView_GetItemCount((HWND
) GetHWND());
674 // Retrieves the spacing between icons in pixels.
675 // If small is TRUE, gets the spacing for the small icon
676 // view, otherwise the large icon view.
677 int wxListCtrl::GetItemSpacing(bool isSmall
) const
679 return ListView_GetItemSpacing((HWND
) GetHWND(), (BOOL
) isSmall
);
682 // Gets the number of selected items in the list control
683 int wxListCtrl::GetSelectedItemCount(void) const
685 return ListView_GetSelectedCount((HWND
) GetHWND());
688 // Gets the text colour of the listview
689 wxColour
wxListCtrl::GetTextColour(void) const
691 COLORREF ref
= ListView_GetTextColor((HWND
) GetHWND());
692 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
696 // Sets the text colour of the listview
697 void wxListCtrl::SetTextColour(const wxColour
& col
)
699 ListView_SetTextColor((HWND
) GetHWND(), PALETTERGB(col
.Red(), col
.Blue(), col
.Green()));
702 // Gets the index of the topmost visible item when in
703 // list or report view
704 long wxListCtrl::GetTopItem(void) const
706 return (long) ListView_GetTopIndex((HWND
) GetHWND());
709 // Searches for an item, starting from 'item'.
710 // 'geometry' is one of
711 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
712 // 'state' is a state bit flag, one or more of
713 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
714 // item can be -1 to find the first item that matches the
716 // Returns the item or -1 if unsuccessful.
717 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
721 if ( geom
== wxLIST_NEXT_ABOVE
)
723 if ( geom
== wxLIST_NEXT_ALL
)
725 if ( geom
== wxLIST_NEXT_BELOW
)
727 if ( geom
== wxLIST_NEXT_LEFT
)
728 flags
|= LVNI_TOLEFT
;
729 if ( geom
== wxLIST_NEXT_RIGHT
)
730 flags
|= LVNI_TORIGHT
;
732 if ( state
& wxLIST_STATE_CUT
)
734 if ( state
& wxLIST_STATE_DROPHILITED
)
735 flags
|= LVNI_DROPHILITED
;
736 if ( state
& wxLIST_STATE_FOCUSED
)
737 flags
|= LVNI_FOCUSED
;
738 if ( state
& wxLIST_STATE_SELECTED
)
739 flags
|= LVNI_SELECTED
;
741 return (long) ListView_GetNextItem((HWND
) GetHWND(), item
, flags
);
745 wxImageList
*wxListCtrl::GetImageList(int which
) const
747 if ( which
== wxIMAGE_LIST_NORMAL
)
749 return m_imageListNormal
;
751 else if ( which
== wxIMAGE_LIST_SMALL
)
753 return m_imageListSmall
;
755 else if ( which
== wxIMAGE_LIST_STATE
)
757 return m_imageListState
;
762 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
765 if ( which
== wxIMAGE_LIST_NORMAL
)
767 flags
= LVSIL_NORMAL
;
768 m_imageListNormal
= imageList
;
770 else if ( which
== wxIMAGE_LIST_SMALL
)
773 m_imageListSmall
= imageList
;
775 else if ( which
== wxIMAGE_LIST_STATE
)
778 m_imageListState
= imageList
;
780 ListView_SetImageList((HWND
) GetHWND(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
784 ////////////////////////////////////////////////////////////////////////////
786 // Arranges the items
787 bool wxListCtrl::Arrange(int flag
)
790 if ( flag
== wxLIST_ALIGN_LEFT
)
791 code
= LVA_ALIGNLEFT
;
792 else if ( flag
== wxLIST_ALIGN_TOP
)
794 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
796 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
797 code
= LVA_SNAPTOGRID
;
799 return (ListView_Arrange((HWND
) GetHWND(), code
) != 0);
803 bool wxListCtrl::DeleteItem(long item
)
805 return (ListView_DeleteItem((HWND
) GetHWND(), (int) item
) != 0);
809 bool wxListCtrl::DeleteAllItems(void)
811 return (ListView_DeleteAllItems((HWND
) GetHWND()) != 0);
815 bool wxListCtrl::DeleteAllColumns(void)
818 for ( i
= 0; i
< m_colCount
; i
++)
820 if (ListView_DeleteColumn((HWND
) GetHWND(), 0) != 0)
823 return (m_colCount
== 0);
827 bool wxListCtrl::DeleteColumn(int col
)
829 bool success
= (ListView_DeleteColumn((HWND
) GetHWND(), col
) != 0);
831 if ( success
&& (m_colCount
> 0) )
836 // Clears items, and columns if there are any.
837 void wxListCtrl::ClearAll(void)
840 if ( m_colCount
> 0 )
844 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
846 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
848 HWND hWnd
= (HWND
) ListView_EditLabel((HWND
) GetHWND(), item
);
852 m_textCtrl
->UnsubclassWin();
853 m_textCtrl
->SetHWND(0);
858 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
859 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
860 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
865 // End label editing, optionally cancelling the edit
866 bool wxListCtrl::EndEditLabel(bool cancel
)
870 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
872 bool success = (ListView_EndEditLabelNow((HWND) GetHWND(), cancel) != 0);
876 m_textCtrl->UnsubclassWin();
877 m_textCtrl->SetHWND(0);
887 // Ensures this item is visible
888 bool wxListCtrl::EnsureVisible(long item
)
890 return (ListView_EnsureVisible((HWND
) GetHWND(), (int) item
, FALSE
) != 0);
893 // Find an item whose label matches this string, starting from the item after 'start'
894 // or the beginning if 'start' is -1.
895 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
897 LV_FINDINFO findInfo
;
899 findInfo
.flags
= LVFI_STRING
;
901 findInfo
.flags
|= LVFI_STRING
;
902 findInfo
.psz
= WXSTRINGCAST str
;
904 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
907 // Find an item whose data matches this data, starting from the item after 'start'
908 // or the beginning if 'start' is -1.
909 long wxListCtrl::FindItem(long start
, long data
)
911 LV_FINDINFO findInfo
;
913 findInfo
.flags
= LVFI_PARAM
;
914 findInfo
.lParam
= data
;
916 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
919 // Find an item nearest this position in the specified direction, starting from
920 // the item after 'start' or the beginning if 'start' is -1.
921 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
923 LV_FINDINFO findInfo
;
925 findInfo
.flags
= LVFI_NEARESTXY
;
926 findInfo
.pt
.x
= pt
.x
;
927 findInfo
.pt
.y
= pt
.y
;
928 findInfo
.vkDirection
= VK_RIGHT
;
930 if ( direction
== wxLIST_FIND_UP
)
931 findInfo
.vkDirection
= VK_UP
;
932 else if ( direction
== wxLIST_FIND_DOWN
)
933 findInfo
.vkDirection
= VK_DOWN
;
934 else if ( direction
== wxLIST_FIND_LEFT
)
935 findInfo
.vkDirection
= VK_LEFT
;
936 else if ( direction
== wxLIST_FIND_RIGHT
)
937 findInfo
.vkDirection
= VK_RIGHT
;
939 return ListView_FindItem((HWND
) GetHWND(), (int) start
, & findInfo
);
942 // Determines which item (if any) is at the specified point,
943 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
944 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
946 LV_HITTESTINFO hitTestInfo
;
947 hitTestInfo
.pt
.x
= (int) point
.x
;
948 hitTestInfo
.pt
.y
= (int) point
.y
;
950 ListView_HitTest((HWND
) GetHWND(), & hitTestInfo
);
953 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
954 flags
|= wxLIST_HITTEST_ABOVE
;
955 if ( hitTestInfo
.flags
& LVHT_BELOW
)
956 flags
|= wxLIST_HITTEST_BELOW
;
957 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
958 flags
|= wxLIST_HITTEST_NOWHERE
;
959 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
960 flags
|= wxLIST_HITTEST_ONITEMICON
;
961 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
962 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
963 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
964 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
965 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
966 flags
|= wxLIST_HITTEST_TOLEFT
;
967 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
968 flags
|= wxLIST_HITTEST_TORIGHT
;
970 return (long) hitTestInfo
.iItem
;
973 // Inserts an item, returning the index of the new item if successful,
975 long wxListCtrl::InsertItem(wxListItem
& info
)
978 wxConvertToMSWListItem(this, info
, item
);
980 return (long) ListView_InsertItem((HWND
) GetHWND(), & item
);
983 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
987 info
.m_mask
= wxLIST_MASK_TEXT
;
988 info
.m_itemId
= index
;
989 return InsertItem(info
);
992 // Inserts an image item
993 long wxListCtrl::InsertItem(long index
, int imageIndex
)
996 info
.m_image
= imageIndex
;
997 info
.m_mask
= wxLIST_MASK_IMAGE
;
998 info
.m_itemId
= index
;
999 return InsertItem(info
);
1002 // Inserts an image/string item
1003 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1006 info
.m_image
= imageIndex
;
1007 info
.m_text
= label
;
1008 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1009 info
.m_itemId
= index
;
1010 return InsertItem(info
);
1013 // For list view mode (only), inserts a column.
1014 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1019 lvCol
.pszText
= NULL
;
1021 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1023 lvCol
.mask
|= LVCF_TEXT
;
1024 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1025 lvCol
.cchTextMax
= 0; // Ignored
1027 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1029 lvCol
.mask
|= LVCF_FMT
;
1031 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1032 lvCol
.fmt
= LVCFMT_LEFT
;
1033 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1034 lvCol
.fmt
= LVCFMT_RIGHT
;
1035 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1036 lvCol
.fmt
= LVCFMT_CENTER
;
1039 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1041 lvCol
.mask
|= LVCF_WIDTH
;
1042 lvCol
.cx
= item
.m_width
;
1044 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
1045 lvCol
.cx
= LVSCW_AUTOSIZE
;
1046 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
1047 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1049 lvCol
.mask
|= LVCF_SUBITEM
;
1050 lvCol
.iSubItem
= col
;
1052 bool success
= (ListView_InsertColumn((HWND
) GetHWND(), col
, & lvCol
) != 0);
1058 long wxListCtrl::InsertColumn(long col
, const wxString
& heading
, int format
,
1062 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1063 item
.m_text
= heading
;
1066 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1067 item
.m_width
= width
;
1069 item
.m_format
= format
;
1071 return InsertColumn(col
, item
);
1074 // Scrolls the list control. If in icon, small icon or report view mode,
1075 // x specifies the number of pixels to scroll. If in list view mode, x
1076 // specifies the number of columns to scroll.
1077 // If in icon, small icon or list view mode, y specifies the number of pixels
1078 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1079 bool wxListCtrl::ScrollList(int dx
, int dy
)
1081 return (ListView_Scroll((HWND
) GetHWND(), dx
, dy
) != 0);
1086 // fn is a function which takes 3 long arguments: item1, item2, data.
1087 // item1 is the long data associated with a first item (NOT the index).
1088 // item2 is the long data associated with a second item (NOT the index).
1089 // data is the same value as passed to SortItems.
1090 // The return value is a negative number if the first item should precede the second
1091 // item, a positive number of the second item should precede the first,
1092 // or zero if the two items are equivalent.
1094 // data is arbitrary data to be passed to the sort function.
1095 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1097 return (ListView_SortItems((HWND
) GetHWND(), (PFNLVCOMPARE
) fn
, data
) != 0);
1100 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1102 if (cmd
== EN_UPDATE
)
1104 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1105 event
.SetEventObject( this );
1106 ProcessCommand(event
);
1109 else if (cmd
== EN_KILLFOCUS
)
1111 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1112 event
.SetEventObject( this );
1113 ProcessCommand(event
);
1119 bool wxListCtrl::MSWNotify(WXWPARAM wParam
, WXLPARAM lParam
, WXLPARAM
*result
)
1121 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1122 wxEventType eventType
= wxEVT_NULL
;
1123 NMHDR
*hdr1
= (NMHDR
*) lParam
;
1124 switch ( hdr1
->code
)
1126 case LVN_BEGINRDRAG
:
1127 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1131 if ( eventType
== wxEVT_NULL
)
1133 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1137 NM_LISTVIEW
*hdr
= (NM_LISTVIEW
*)lParam
;
1138 event
.m_itemIndex
= hdr
->iItem
;
1139 event
.m_pointDrag
.x
= hdr
->ptAction
.x
;
1140 event
.m_pointDrag
.y
= hdr
->ptAction
.y
;
1144 case LVN_BEGINLABELEDIT
:
1146 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1147 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1148 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1152 case LVN_COLUMNCLICK
:
1154 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1155 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1156 event
.m_itemIndex
= -1;
1157 event
.m_col
= hdr
->iSubItem
;
1160 case LVN_DELETEALLITEMS
:
1162 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1163 // NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1164 event
.m_itemIndex
= -1;
1167 case LVN_DELETEITEM
:
1169 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1170 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1171 event
.m_itemIndex
= hdr
->iItem
;
1174 case LVN_ENDLABELEDIT
:
1176 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1177 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1178 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1179 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1180 event
.m_cancelled
= TRUE
;
1183 case LVN_GETDISPINFO
:
1186 // TODO: some text buffering here, I think
1187 // TODO: API for getting Windows to retrieve values
1189 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1190 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1191 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1194 case LVN_INSERTITEM
:
1196 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1197 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1198 event
.m_itemIndex
= hdr
->iItem
;
1201 case LVN_ITEMCHANGED
:
1203 // This needs to be sent to wxListCtrl as a rather more
1204 // concrete event. For now, just detect a selection
1206 NM_LISTVIEW
* hdr
= (NM_LISTVIEW
*)lParam
;
1207 if ( (hdr
->uNewState
& LVIS_SELECTED
) && !(hdr
->uOldState
& LVIS_SELECTED
) )
1209 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1210 event
.m_itemIndex
= hdr
->iItem
;
1212 else if ( !(hdr
->uNewState
& LVIS_SELECTED
) && (hdr
->uOldState
& LVIS_SELECTED
) )
1214 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1215 event
.m_itemIndex
= hdr
->iItem
;
1223 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1224 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1225 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1228 case LVN_SETDISPINFO
:
1230 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1231 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1232 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, (HWND
) GetHWND());
1237 return wxControl::MSWNotify(wParam
, lParam
, result
);
1240 event
.SetEventObject( this );
1241 event
.SetEventType(eventType
);
1243 if ( !GetEventHandler()->ProcessEvent(event
) )
1246 if (hdr1
->code
== LVN_GETDISPINFO
)
1248 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1249 if ( info
->item
.mask
& LVIF_TEXT
)
1251 if ( !event
.m_item
.m_text
.IsNull() )
1253 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1254 info
->item
.cchTextMax
= strlen(info
->item
.pszText
) + 1;
1257 // wxConvertToMSWListItem(this, event.m_item, info->item);
1260 *result
= !event
.IsAllowed();
1265 char *wxListCtrl::AddPool(const wxString
& str
)
1267 // Remove the first element if 3 strings exist
1268 if ( m_stringPool
.Number() == 3 )
1270 wxNode
*node
= m_stringPool
.First();
1271 delete[] (char *)node
->Data();
1274 wxNode
*node
= m_stringPool
.Add((char *) (const char *)str
);
1275 return (char *)node
->Data();
1278 // List item structure
1279 wxListItem::wxListItem(void)
1289 m_format
= wxLIST_FORMAT_CENTRE
;
1293 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1295 info
.m_data
= lvItem
.lParam
;
1298 info
.m_stateMask
= 0;
1299 info
.m_itemId
= lvItem
.iItem
;
1301 long oldMask
= lvItem
.mask
;
1303 bool needText
= FALSE
;
1304 if (getFullInfo
!= 0)
1306 if ( lvItem
.mask
& LVIF_TEXT
)
1313 lvItem
.pszText
= new char[513];
1314 lvItem
.cchTextMax
= 512;
1316 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ;
1317 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1318 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
) ;
1321 if ( lvItem
.mask
& LVIF_STATE
)
1323 info
.m_mask
|= wxLIST_MASK_STATE
;
1325 if ( lvItem
.stateMask
& LVIS_CUT
)
1327 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1328 if ( lvItem
.state
& LVIS_CUT
)
1329 info
.m_state
|= wxLIST_STATE_CUT
;
1331 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1333 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1334 if ( lvItem
.state
& LVIS_DROPHILITED
)
1335 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1337 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1339 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1340 if ( lvItem
.state
& LVIS_FOCUSED
)
1341 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1343 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1345 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1346 if ( lvItem
.state
& LVIS_SELECTED
)
1347 info
.m_state
|= wxLIST_STATE_SELECTED
;
1351 if ( lvItem
.mask
& LVIF_TEXT
)
1353 info
.m_mask
|= wxLIST_MASK_TEXT
;
1354 info
.m_text
= lvItem
.pszText
;
1356 if ( lvItem
.mask
& LVIF_IMAGE
)
1358 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1359 info
.m_image
= lvItem
.iImage
;
1361 if ( lvItem
.mask
& LVIF_PARAM
)
1362 info
.m_mask
|= wxLIST_MASK_DATA
;
1363 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1364 info
.m_mask
|= wxLIST_SET_ITEM
;
1365 info
.m_col
= lvItem
.iSubItem
;
1370 delete[] lvItem
.pszText
;
1372 lvItem
.mask
= oldMask
;
1375 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1377 lvItem
.iItem
= (int) info
.m_itemId
;
1379 lvItem
.iImage
= info
.m_image
;
1380 lvItem
.lParam
= info
.m_data
;
1381 lvItem
.stateMask
= 0;
1384 lvItem
.iSubItem
= info
.m_col
;
1386 if (info
.m_mask
& wxLIST_MASK_STATE
)
1388 lvItem
.mask
|= LVIF_STATE
;
1389 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1391 lvItem
.stateMask
|= LVIS_CUT
;
1392 if (info
.m_state
& wxLIST_STATE_CUT
)
1393 lvItem
.state
|= LVIS_CUT
;
1395 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1397 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1398 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1399 lvItem
.state
|= LVIS_DROPHILITED
;
1401 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1403 lvItem
.stateMask
|= LVIS_FOCUSED
;
1404 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1405 lvItem
.state
|= LVIS_FOCUSED
;
1407 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1409 lvItem
.stateMask
|= LVIS_SELECTED
;
1410 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1411 lvItem
.state
|= LVIS_SELECTED
;
1415 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1417 lvItem
.mask
|= LVIF_TEXT
;
1418 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1420 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1424 lvItem
.pszText
= (char *) (const char *)info
.m_text
;
1425 if ( lvItem
.pszText
)
1426 lvItem
.cchTextMax
= info
.m_text
.Length();
1428 lvItem
.cchTextMax
= 0;
1431 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
1432 lvItem
.mask
|= LVIF_IMAGE
;
1433 if (info
.m_mask
& wxLIST_MASK_DATA
)
1434 lvItem
.mask
|= LVIF_PARAM
;
1438 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
1440 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
1441 : wxNotifyEvent(commandType
, id
)
1446 m_cancelled
= FALSE
;