1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "listctrl.h"
22 #pragma implementation "listctrlbase.h"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
40 #include "wx/settings.h"
43 #include "wx/textctrl.h"
44 #include "wx/imaglist.h"
45 #include "wx/listctrl.h"
46 #include "wx/dcclient.h"
48 #include "wx/msw/private.h"
50 #if ((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
51 #include "wx/msw/gnuwin32/extra.h"
58 (LVHT_ONITEMICON | LVHT_ONITEMLABEL | LVHT_ONITEMSTATEICON)
61 #ifndef LVM_SETEXTENDEDLISTVIEWSTYLE
62 #define LVM_SETEXTENDEDLISTVIEWSTYLE (0x1000 + 54)
65 #ifndef LVS_EX_FULLROWSELECT
66 #define LVS_EX_FULLROWSELECT 0x00000020
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
);
74 static void wxConvertFromMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& tvItem
, HWND getFullInfo
= 0);
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG
)
81 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG
)
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT
)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM
)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO
)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO
)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED
)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED
)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN
)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM
)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK
)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED
)
97 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl
, wxControl
)
98 IMPLEMENT_DYNAMIC_CLASS(wxListItem
, wxObject
)
100 BEGIN_EVENT_TABLE(wxListCtrl
, wxControl
)
101 EVT_PAINT(wxListCtrl::OnPaint
)
104 // ============================================================================
106 // ============================================================================
108 // ----------------------------------------------------------------------------
110 // ----------------------------------------------------------------------------
112 void wxListEvent::CopyObject(wxObject
& object_dest
) const
114 wxListEvent
*obj
= (wxListEvent
*)&object_dest
;
116 wxNotifyEvent::CopyObject(object_dest
);
118 obj
->m_code
= m_code
;
119 obj
->m_itemIndex
= m_itemIndex
;
120 obj
->m_oldItemIndex
= m_oldItemIndex
;
122 obj
->m_cancelled
= m_cancelled
;
123 obj
->m_pointDrag
= m_pointDrag
;
124 obj
->m_item
.m_mask
= m_item
.m_mask
;
125 obj
->m_item
.m_itemId
= m_item
.m_itemId
;
126 obj
->m_item
.m_col
= m_item
.m_col
;
127 obj
->m_item
.m_state
= m_item
.m_state
;
128 obj
->m_item
.m_stateMask
= m_item
.m_stateMask
;
129 obj
->m_item
.m_text
= m_item
.m_text
;
130 obj
->m_item
.m_image
= m_item
.m_image
;
131 obj
->m_item
.m_data
= m_item
.m_data
;
132 obj
->m_item
.m_format
= m_item
.m_format
;
133 obj
->m_item
.m_width
= m_item
.m_width
;
135 if ( m_item
.HasAttributes() )
137 obj
->m_item
.SetTextColour(m_item
.GetTextColour());
138 obj
->m_item
.SetBackgroundColour(m_item
.GetBackgroundColour());
139 obj
->m_item
.SetFont(m_item
.GetFont());
143 // ----------------------------------------------------------------------------
144 // wxListCtrl construction
145 // ----------------------------------------------------------------------------
147 void wxListCtrl::Init()
149 m_imageListNormal
= NULL
;
150 m_imageListSmall
= NULL
;
151 m_imageListState
= NULL
;
152 m_ownsImageListNormal
= m_ownsImageListSmall
= m_ownsImageListState
= FALSE
;
156 m_hasAnyAttr
= FALSE
;
159 bool wxListCtrl::Create(wxWindow
*parent
,
164 const wxValidator
& validator
,
165 const wxString
& name
)
168 SetValidator(validator
);
169 #endif // wxUSE_VALIDATORS
178 m_windowStyle
= style
;
191 m_windowId
= (id
== -1) ? NewControlId() : id
;
193 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
194 LVS_SHAREIMAGELISTS
| LVS_SHOWSELALWAYS
;
196 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
197 wstyle
|= WS_CLIPSIBLINGS
;
199 if ( wxStyleHasBorder(m_windowStyle
) )
201 m_baseStyle
= wstyle
;
203 if ( !DoCreateControl(x
, y
, width
, height
) )
207 parent
->AddChild(this);
212 bool wxListCtrl::DoCreateControl(int x
, int y
, int w
, int h
)
214 DWORD wstyle
= m_baseStyle
;
217 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
);
219 // Even with extended styles, need to combine with WS_BORDER
220 // for them to look right.
224 long oldStyle
= 0; // Dummy
225 wstyle
|= ConvertToMSWStyle(oldStyle
, m_windowStyle
);
227 // Create the ListView control.
228 m_hWnd
= (WXHWND
)CreateWindowEx(exStyle
,
233 GetWinHwnd(GetParent()),
240 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
245 // for comctl32.dll v 4.70+ we want to have this attribute because it's
246 // prettier (and also because wxGTK does it like this)
247 if ( (wstyle
& LVS_REPORT
) && wxTheApp
->GetComCtl32Version() >= 470 )
249 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE
,
250 0, LVS_EX_FULLROWSELECT
);
253 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
254 SetForegroundColour(GetParent()->GetForegroundColour());
261 void wxListCtrl::UpdateStyle()
265 // The new window view style
267 DWORD dwStyleNew
= ConvertToMSWStyle(dummy
, m_windowStyle
);
268 dwStyleNew
|= m_baseStyle
;
270 // Get the current window style.
271 DWORD dwStyleOld
= ::GetWindowLong(GetHwnd(), GWL_STYLE
);
273 // Only set the window style if the view bits have changed.
274 if ( dwStyleOld
!= dwStyleNew
)
276 ::SetWindowLong(GetHwnd(), GWL_STYLE
, dwStyleNew
);
281 void wxListCtrl::FreeAllAttrs(bool dontRecreate
)
285 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
287 delete (wxListItemAttr
*)node
->Data();
293 m_attrs
.Create(wxKEY_INTEGER
, 1000); // just as def ctor
296 m_hasAnyAttr
= FALSE
;
300 wxListCtrl::~wxListCtrl()
302 FreeAllAttrs(TRUE
/* no need to recreate hash any more */);
306 m_textCtrl
->UnsubclassWin();
307 m_textCtrl
->SetHWND(0);
312 if (m_ownsImageListNormal
) delete m_imageListNormal
;
313 if (m_ownsImageListSmall
) delete m_imageListSmall
;
314 if (m_ownsImageListState
) delete m_imageListState
;
317 // ----------------------------------------------------------------------------
318 // set/get/change style
319 // ----------------------------------------------------------------------------
321 // Add or remove a single window style
322 void wxListCtrl::SetSingleStyle(long style
, bool add
)
324 long flag
= GetWindowStyleFlag();
326 // Get rid of conflicting styles
329 if ( style
& wxLC_MASK_TYPE
)
330 flag
= flag
& ~wxLC_MASK_TYPE
;
331 if ( style
& wxLC_MASK_ALIGN
)
332 flag
= flag
& ~wxLC_MASK_ALIGN
;
333 if ( style
& wxLC_MASK_SORT
)
334 flag
= flag
& ~wxLC_MASK_SORT
;
350 m_windowStyle
= flag
;
355 // Set the whole window style
356 void wxListCtrl::SetWindowStyleFlag(long flag
)
358 m_windowStyle
= flag
;
363 // Can be just a single style, or a bitlist
364 long wxListCtrl::ConvertToMSWStyle(long& oldStyle
, long style
) const
367 if ( style
& wxLC_ICON
)
369 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
370 oldStyle
-= LVS_SMALLICON
;
371 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
372 oldStyle
-= LVS_REPORT
;
373 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
374 oldStyle
-= LVS_LIST
;
378 if ( style
& wxLC_SMALL_ICON
)
380 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
381 oldStyle
-= LVS_ICON
;
382 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
383 oldStyle
-= LVS_REPORT
;
384 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
385 oldStyle
-= LVS_LIST
;
386 wstyle
|= LVS_SMALLICON
;
389 if ( style
& wxLC_LIST
)
391 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
392 oldStyle
-= LVS_ICON
;
393 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_REPORT
)
394 oldStyle
-= LVS_REPORT
;
395 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
396 oldStyle
-= LVS_SMALLICON
;
400 if ( style
& wxLC_REPORT
)
402 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_ICON
)
403 oldStyle
-= LVS_ICON
;
404 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_LIST
)
405 oldStyle
-= LVS_LIST
;
406 if ( (oldStyle
& LVS_TYPEMASK
) == LVS_SMALLICON
)
407 oldStyle
-= LVS_SMALLICON
;
409 wstyle
|= LVS_REPORT
;
412 if ( style
& wxLC_ALIGN_LEFT
)
414 if ( oldStyle
& LVS_ALIGNTOP
)
415 oldStyle
-= LVS_ALIGNTOP
;
416 wstyle
|= LVS_ALIGNLEFT
;
419 if ( style
& wxLC_ALIGN_TOP
)
421 if ( oldStyle
& LVS_ALIGNLEFT
)
422 oldStyle
-= LVS_ALIGNLEFT
;
423 wstyle
|= LVS_ALIGNTOP
;
426 if ( style
& wxLC_AUTOARRANGE
)
427 wstyle
|= LVS_AUTOARRANGE
;
429 // Apparently, no such style (documentation wrong?)
431 if ( style & wxLC_BUTTON )
432 wstyle |= LVS_BUTTON;
435 if ( style
& wxLC_NO_SORT_HEADER
)
436 wstyle
|= LVS_NOSORTHEADER
;
438 if ( style
& wxLC_NO_HEADER
)
439 wstyle
|= LVS_NOCOLUMNHEADER
;
441 if ( style
& wxLC_EDIT_LABELS
)
442 wstyle
|= LVS_EDITLABELS
;
444 if ( style
& wxLC_SINGLE_SEL
)
445 wstyle
|= LVS_SINGLESEL
;
447 if ( style
& wxLC_SORT_ASCENDING
)
449 if ( oldStyle
& LVS_SORTDESCENDING
)
450 oldStyle
-= LVS_SORTDESCENDING
;
451 wstyle
|= LVS_SORTASCENDING
;
454 if ( style
& wxLC_SORT_DESCENDING
)
456 if ( oldStyle
& LVS_SORTASCENDING
)
457 oldStyle
-= LVS_SORTASCENDING
;
458 wstyle
|= LVS_SORTDESCENDING
;
464 // ----------------------------------------------------------------------------
466 // ----------------------------------------------------------------------------
468 // Sets the foreground, i.e. text, colour
469 bool wxListCtrl::SetForegroundColour(const wxColour
& col
)
471 if ( !wxWindow::SetForegroundColour(col
) )
474 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col
));
479 // Sets the background colour
480 bool wxListCtrl::SetBackgroundColour(const wxColour
& col
)
482 if ( !wxWindow::SetBackgroundColour(col
) )
485 // we set the same colour for both the "empty" background and the items
487 COLORREF color
= wxColourToRGB(col
);
488 ListView_SetBkColor(GetHwnd(), color
);
489 ListView_SetTextBkColor(GetHwnd(), color
);
494 // Gets information about this column
495 bool wxListCtrl::GetColumn(int col
, wxListItem
& item
) const
500 lvCol
.pszText
= NULL
;
502 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
504 lvCol
.mask
|= LVCF_TEXT
;
505 lvCol
.pszText
= new wxChar
[513];
506 lvCol
.cchTextMax
= 512;
509 bool success
= (ListView_GetColumn(GetHwnd(), col
, & lvCol
) != 0);
511 // item.m_subItem = lvCol.iSubItem;
512 item
.m_width
= lvCol
.cx
;
514 if ( (item
.m_mask
& wxLIST_MASK_TEXT
) && lvCol
.pszText
)
516 item
.m_text
= lvCol
.pszText
;
517 delete[] lvCol
.pszText
;
520 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
522 if (lvCol
.fmt
== LVCFMT_LEFT
)
523 item
.m_format
= wxLIST_FORMAT_LEFT
;
524 else if (lvCol
.fmt
== LVCFMT_RIGHT
)
525 item
.m_format
= wxLIST_FORMAT_RIGHT
;
526 else if (lvCol
.fmt
== LVCFMT_CENTER
)
527 item
.m_format
= wxLIST_FORMAT_CENTRE
;
533 // Sets information about this column
534 bool wxListCtrl::SetColumn(int col
, wxListItem
& item
)
539 lvCol
.pszText
= NULL
;
541 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
543 lvCol
.mask
|= LVCF_TEXT
;
544 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
545 lvCol
.cchTextMax
= 0; // Ignored
547 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
549 lvCol
.mask
|= LVCF_FMT
;
551 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
552 lvCol
.fmt
= LVCFMT_LEFT
;
553 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
554 lvCol
.fmt
= LVCFMT_RIGHT
;
555 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
556 lvCol
.fmt
= LVCFMT_CENTER
;
559 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
561 lvCol
.mask
|= LVCF_WIDTH
;
562 lvCol
.cx
= item
.m_width
;
564 if ( lvCol
.cx
== wxLIST_AUTOSIZE
)
565 lvCol
.cx
= LVSCW_AUTOSIZE
;
566 else if ( lvCol
.cx
== wxLIST_AUTOSIZE_USEHEADER
)
567 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
569 lvCol
.mask
|= LVCF_SUBITEM
;
570 lvCol
.iSubItem
= col
;
571 return (ListView_SetColumn(GetHwnd(), col
, & lvCol
) != 0);
574 // Gets the column width
575 int wxListCtrl::GetColumnWidth(int col
) const
577 return ListView_GetColumnWidth(GetHwnd(), col
);
580 // Sets the column width
581 bool wxListCtrl::SetColumnWidth(int col
, int width
)
584 if ( m_windowStyle
& wxLC_LIST
)
588 if ( width2
== wxLIST_AUTOSIZE
)
589 width2
= LVSCW_AUTOSIZE
;
590 else if ( width2
== wxLIST_AUTOSIZE_USEHEADER
)
591 width2
= LVSCW_AUTOSIZE_USEHEADER
;
593 return (ListView_SetColumnWidth(GetHwnd(), col2
, width2
) != 0);
596 // Gets the number of items that can fit vertically in the
597 // visible area of the list control (list or report view)
598 // or the total number of items in the list control (icon
599 // or small icon view)
600 int wxListCtrl::GetCountPerPage() const
602 return ListView_GetCountPerPage(GetHwnd());
605 // Gets the edit control for editing labels.
606 wxTextCtrl
* wxListCtrl::GetEditControl() const
611 // Gets information about the item
612 bool wxListCtrl::GetItem(wxListItem
& info
) const
615 wxZeroMemory(lvItem
);
617 lvItem
.iItem
= info
.m_itemId
;
618 lvItem
.iSubItem
= info
.m_col
;
620 if ( info
.m_mask
& wxLIST_MASK_TEXT
)
622 lvItem
.mask
|= LVIF_TEXT
;
623 lvItem
.pszText
= new wxChar
[513];
624 lvItem
.cchTextMax
= 512;
628 lvItem
.pszText
= NULL
;
631 if (info
.m_mask
& wxLIST_MASK_DATA
)
632 lvItem
.mask
|= LVIF_PARAM
;
634 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
635 lvItem
.mask
|= LVIF_IMAGE
;
637 if ( info
.m_mask
& wxLIST_MASK_STATE
)
639 lvItem
.mask
|= LVIF_STATE
;
640 // the other bits are hardly interesting anyhow
641 lvItem
.stateMask
= LVIS_SELECTED
| LVIS_FOCUSED
;
644 bool success
= ListView_GetItem((HWND
)GetHWND(), &lvItem
) != 0;
647 wxLogError(_("Couldn't retrieve information about list control item %d."),
652 wxConvertFromMSWListItem(this, info
, lvItem
);
656 delete[] lvItem
.pszText
;
661 // Sets information about the item
662 bool wxListCtrl::SetItem(wxListItem
& info
)
665 wxConvertToMSWListItem(this, info
, item
);
668 if ( !ListView_SetItem(GetHwnd(), &item
) )
670 wxLogDebug(_T("ListView_SetItem() failed"));
675 // we need to update the item immediately to show the new image
676 bool updateNow
= (info
.m_mask
& wxLIST_MASK_IMAGE
) != 0;
678 // check whether it has any custom attributes
679 if ( info
.HasAttributes() )
681 wxListItemAttr
*attr
= (wxListItemAttr
*)m_attrs
.Get(item
.iItem
);
684 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
686 *attr
= *info
.GetAttributes();
690 // if the colour has changed, we must redraw the item
696 // we need this to make the change visible right now
697 ListView_Update(GetHwnd(), item
.iItem
);
703 long wxListCtrl::SetItem(long index
, int col
, const wxString
& label
, int imageId
)
707 info
.m_mask
= wxLIST_MASK_TEXT
;
708 info
.m_itemId
= index
;
712 info
.m_image
= imageId
;
713 info
.m_mask
|= wxLIST_MASK_IMAGE
;
715 return SetItem(info
);
719 // Gets the item state
720 int wxListCtrl::GetItemState(long item
, long stateMask
) const
724 info
.m_mask
= wxLIST_MASK_STATE
;
725 info
.m_stateMask
= stateMask
;
726 info
.m_itemId
= item
;
734 // Sets the item state
735 bool wxListCtrl::SetItemState(long item
, long state
, long stateMask
)
739 info
.m_mask
= wxLIST_MASK_STATE
;
740 info
.m_state
= state
;
741 info
.m_stateMask
= stateMask
;
742 info
.m_itemId
= item
;
744 return SetItem(info
);
747 // Sets the item image
748 bool wxListCtrl::SetItemImage(long item
, int image
, int WXUNUSED(selImage
))
752 info
.m_mask
= wxLIST_MASK_IMAGE
;
753 info
.m_image
= image
;
754 info
.m_itemId
= item
;
756 return SetItem(info
);
759 // Gets the item text
760 wxString
wxListCtrl::GetItemText(long item
) const
764 info
.m_mask
= wxLIST_MASK_TEXT
;
765 info
.m_itemId
= item
;
772 // Sets the item text
773 void wxListCtrl::SetItemText(long item
, const wxString
& str
)
777 info
.m_mask
= wxLIST_MASK_TEXT
;
778 info
.m_itemId
= item
;
784 // Gets the item data
785 long wxListCtrl::GetItemData(long item
) const
789 info
.m_mask
= wxLIST_MASK_DATA
;
790 info
.m_itemId
= item
;
797 // Sets the item data
798 bool wxListCtrl::SetItemData(long item
, long data
)
802 info
.m_mask
= wxLIST_MASK_DATA
;
803 info
.m_itemId
= item
;
806 return SetItem(info
);
809 // Gets the item rectangle
810 bool wxListCtrl::GetItemRect(long item
, wxRect
& rect
, int code
) const
814 int code2
= LVIR_BOUNDS
;
815 if ( code
== wxLIST_RECT_BOUNDS
)
817 else if ( code
== wxLIST_RECT_ICON
)
819 else if ( code
== wxLIST_RECT_LABEL
)
823 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
) != 0);
825 bool success
= (ListView_GetItemRect(GetHwnd(), (int) item
, &rect2
, code2
) != 0);
830 rect
.width
= rect2
.right
- rect2
.left
;
831 rect
.height
= rect2
.bottom
- rect2
.top
;
835 // Gets the item position
836 bool wxListCtrl::GetItemPosition(long item
, wxPoint
& pos
) const
840 bool success
= (ListView_GetItemPosition(GetHwnd(), (int) item
, &pt
) != 0);
842 pos
.x
= pt
.x
; pos
.y
= pt
.y
;
846 // Sets the item position.
847 bool wxListCtrl::SetItemPosition(long item
, const wxPoint
& pos
)
849 return (ListView_SetItemPosition(GetHwnd(), (int) item
, pos
.x
, pos
.y
) != 0);
852 // Gets the number of items in the list control
853 int wxListCtrl::GetItemCount() const
855 return ListView_GetItemCount(GetHwnd());
858 // Retrieves the spacing between icons in pixels.
859 // If small is TRUE, gets the spacing for the small icon
860 // view, otherwise the large icon view.
861 int wxListCtrl::GetItemSpacing(bool isSmall
) const
863 return ListView_GetItemSpacing(GetHwnd(), (BOOL
) isSmall
);
866 // Gets the number of selected items in the list control
867 int wxListCtrl::GetSelectedItemCount() const
869 return ListView_GetSelectedCount(GetHwnd());
872 // Gets the text colour of the listview
873 wxColour
wxListCtrl::GetTextColour() const
875 COLORREF ref
= ListView_GetTextColor(GetHwnd());
876 wxColour
col(GetRValue(ref
), GetGValue(ref
), GetBValue(ref
));
880 // Sets the text colour of the listview
881 void wxListCtrl::SetTextColour(const wxColour
& col
)
883 ListView_SetTextColor(GetHwnd(), PALETTERGB(col
.Red(), col
.Green(), col
.Blue()));
886 // Gets the index of the topmost visible item when in
887 // list or report view
888 long wxListCtrl::GetTopItem() const
890 return (long) ListView_GetTopIndex(GetHwnd());
893 // Searches for an item, starting from 'item'.
894 // 'geometry' is one of
895 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
896 // 'state' is a state bit flag, one or more of
897 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
898 // item can be -1 to find the first item that matches the
900 // Returns the item or -1 if unsuccessful.
901 long wxListCtrl::GetNextItem(long item
, int geom
, int state
) const
905 if ( geom
== wxLIST_NEXT_ABOVE
)
907 if ( geom
== wxLIST_NEXT_ALL
)
909 if ( geom
== wxLIST_NEXT_BELOW
)
911 if ( geom
== wxLIST_NEXT_LEFT
)
912 flags
|= LVNI_TOLEFT
;
913 if ( geom
== wxLIST_NEXT_RIGHT
)
914 flags
|= LVNI_TORIGHT
;
916 if ( state
& wxLIST_STATE_CUT
)
918 if ( state
& wxLIST_STATE_DROPHILITED
)
919 flags
|= LVNI_DROPHILITED
;
920 if ( state
& wxLIST_STATE_FOCUSED
)
921 flags
|= LVNI_FOCUSED
;
922 if ( state
& wxLIST_STATE_SELECTED
)
923 flags
|= LVNI_SELECTED
;
925 return (long) ListView_GetNextItem(GetHwnd(), item
, flags
);
929 wxImageList
*wxListCtrl::GetImageList(int which
) const
931 if ( which
== wxIMAGE_LIST_NORMAL
)
933 return m_imageListNormal
;
935 else if ( which
== wxIMAGE_LIST_SMALL
)
937 return m_imageListSmall
;
939 else if ( which
== wxIMAGE_LIST_STATE
)
941 return m_imageListState
;
946 void wxListCtrl::SetImageList(wxImageList
*imageList
, int which
)
949 if ( which
== wxIMAGE_LIST_NORMAL
)
951 flags
= LVSIL_NORMAL
;
952 if (m_ownsImageListNormal
) delete m_imageListNormal
;
953 m_imageListNormal
= imageList
;
954 m_ownsImageListNormal
= FALSE
;
956 else if ( which
== wxIMAGE_LIST_SMALL
)
959 if (m_ownsImageListSmall
) delete m_imageListSmall
;
960 m_imageListSmall
= imageList
;
961 m_ownsImageListSmall
= FALSE
;
963 else if ( which
== wxIMAGE_LIST_STATE
)
966 if (m_ownsImageListState
) delete m_imageListState
;
967 m_imageListState
= imageList
;
968 m_ownsImageListState
= FALSE
;
970 ListView_SetImageList(GetHwnd(), (HIMAGELIST
) imageList
? imageList
->GetHIMAGELIST() : 0, flags
);
973 void wxListCtrl::AssignImageList(wxImageList
*imageList
, int which
)
975 SetImageList(imageList
, which
);
976 if ( which
== wxIMAGE_LIST_NORMAL
)
977 m_ownsImageListNormal
= TRUE
;
978 else if ( which
== wxIMAGE_LIST_SMALL
)
979 m_ownsImageListSmall
= TRUE
;
980 else if ( which
== wxIMAGE_LIST_STATE
)
981 m_ownsImageListState
= TRUE
;
984 // ----------------------------------------------------------------------------
986 // ----------------------------------------------------------------------------
988 // Arranges the items
989 bool wxListCtrl::Arrange(int flag
)
992 if ( flag
== wxLIST_ALIGN_LEFT
)
993 code
= LVA_ALIGNLEFT
;
994 else if ( flag
== wxLIST_ALIGN_TOP
)
996 else if ( flag
== wxLIST_ALIGN_DEFAULT
)
998 else if ( flag
== wxLIST_ALIGN_SNAP_TO_GRID
)
999 code
= LVA_SNAPTOGRID
;
1001 return (ListView_Arrange(GetHwnd(), code
) != 0);
1005 bool wxListCtrl::DeleteItem(long item
)
1007 return (ListView_DeleteItem(GetHwnd(), (int) item
) != 0);
1010 // Deletes all items
1011 bool wxListCtrl::DeleteAllItems()
1013 return (ListView_DeleteAllItems(GetHwnd()) != 0);
1016 // Deletes all items
1017 bool wxListCtrl::DeleteAllColumns()
1019 while ( m_colCount
> 0 )
1021 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1023 wxLogLastError(wxT("ListView_DeleteColumn"));
1031 wxASSERT_MSG( m_colCount
== 0, wxT("no columns should be left") );
1037 bool wxListCtrl::DeleteColumn(int col
)
1039 bool success
= (ListView_DeleteColumn(GetHwnd(), col
) != 0);
1041 if ( success
&& (m_colCount
> 0) )
1046 // Clears items, and columns if there are any.
1047 void wxListCtrl::ClearAll()
1050 if ( m_colCount
> 0 )
1054 wxTextCtrl
* wxListCtrl::EditLabel(long item
, wxClassInfo
* textControlClass
)
1056 wxASSERT( (textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
))) );
1058 // VS: ListView_EditLabel requires that the list has focus.
1060 HWND hWnd
= (HWND
) ListView_EditLabel(GetHwnd(), item
);
1064 m_textCtrl
->UnsubclassWin();
1065 m_textCtrl
->SetHWND(0);
1070 m_textCtrl
= (wxTextCtrl
*) textControlClass
->CreateObject();
1071 m_textCtrl
->SetHWND((WXHWND
) hWnd
);
1072 m_textCtrl
->SubclassWin((WXHWND
) hWnd
);
1077 // End label editing, optionally cancelling the edit
1078 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel
))
1082 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
1084 bool success = (ListView_EndEditLabelNow(GetHwnd(), cancel) != 0);
1088 m_textCtrl->UnsubclassWin();
1089 m_textCtrl->SetHWND(0);
1099 // Ensures this item is visible
1100 bool wxListCtrl::EnsureVisible(long item
)
1102 return (ListView_EnsureVisible(GetHwnd(), (int) item
, FALSE
) != 0);
1105 // Find an item whose label matches this string, starting from the item after 'start'
1106 // or the beginning if 'start' is -1.
1107 long wxListCtrl::FindItem(long start
, const wxString
& str
, bool partial
)
1109 LV_FINDINFO findInfo
;
1111 findInfo
.flags
= LVFI_STRING
;
1113 findInfo
.flags
|= LVFI_PARTIAL
;
1116 // ListView_FindItem() excludes the first item from search and to look
1117 // through all the items you need to start from -1 which is unnatural and
1118 // inconsistent with the generic version - so we adjust the index
1121 return ListView_FindItem(GetHwnd(), (int) start
, &findInfo
);
1124 // Find an item whose data matches this data, starting from the item after 'start'
1125 // or the beginning if 'start' is -1.
1126 long wxListCtrl::FindItem(long start
, long data
)
1128 LV_FINDINFO findInfo
;
1130 findInfo
.flags
= LVFI_PARAM
;
1131 findInfo
.lParam
= data
;
1133 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1136 // Find an item nearest this position in the specified direction, starting from
1137 // the item after 'start' or the beginning if 'start' is -1.
1138 long wxListCtrl::FindItem(long start
, const wxPoint
& pt
, int direction
)
1140 LV_FINDINFO findInfo
;
1142 findInfo
.flags
= LVFI_NEARESTXY
;
1143 findInfo
.pt
.x
= pt
.x
;
1144 findInfo
.pt
.y
= pt
.y
;
1145 findInfo
.vkDirection
= VK_RIGHT
;
1147 if ( direction
== wxLIST_FIND_UP
)
1148 findInfo
.vkDirection
= VK_UP
;
1149 else if ( direction
== wxLIST_FIND_DOWN
)
1150 findInfo
.vkDirection
= VK_DOWN
;
1151 else if ( direction
== wxLIST_FIND_LEFT
)
1152 findInfo
.vkDirection
= VK_LEFT
;
1153 else if ( direction
== wxLIST_FIND_RIGHT
)
1154 findInfo
.vkDirection
= VK_RIGHT
;
1156 return ListView_FindItem(GetHwnd(), (int) start
, & findInfo
);
1159 // Determines which item (if any) is at the specified point,
1160 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1161 long wxListCtrl::HitTest(const wxPoint
& point
, int& flags
)
1163 LV_HITTESTINFO hitTestInfo
;
1164 hitTestInfo
.pt
.x
= (int) point
.x
;
1165 hitTestInfo
.pt
.y
= (int) point
.y
;
1167 ListView_HitTest(GetHwnd(), & hitTestInfo
);
1170 if ( hitTestInfo
.flags
& LVHT_ABOVE
)
1171 flags
|= wxLIST_HITTEST_ABOVE
;
1172 if ( hitTestInfo
.flags
& LVHT_BELOW
)
1173 flags
|= wxLIST_HITTEST_BELOW
;
1174 if ( hitTestInfo
.flags
& LVHT_NOWHERE
)
1175 flags
|= wxLIST_HITTEST_NOWHERE
;
1176 if ( hitTestInfo
.flags
& LVHT_ONITEMICON
)
1177 flags
|= wxLIST_HITTEST_ONITEMICON
;
1178 if ( hitTestInfo
.flags
& LVHT_ONITEMLABEL
)
1179 flags
|= wxLIST_HITTEST_ONITEMLABEL
;
1180 if ( hitTestInfo
.flags
& LVHT_ONITEMSTATEICON
)
1181 flags
|= wxLIST_HITTEST_ONITEMSTATEICON
;
1182 if ( hitTestInfo
.flags
& LVHT_TOLEFT
)
1183 flags
|= wxLIST_HITTEST_TOLEFT
;
1184 if ( hitTestInfo
.flags
& LVHT_TORIGHT
)
1185 flags
|= wxLIST_HITTEST_TORIGHT
;
1187 return (long) hitTestInfo
.iItem
;
1190 // Inserts an item, returning the index of the new item if successful,
1192 long wxListCtrl::InsertItem(wxListItem
& info
)
1195 wxConvertToMSWListItem(this, info
, item
);
1197 // check whether it has any custom attributes
1198 if ( info
.HasAttributes() )
1201 wxListItemAttr
*attr
;
1202 attr
= (wxListItemAttr
*) m_attrs
.Get(item
.iItem
);
1206 m_attrs
.Put(item
.iItem
, (wxObject
*)new wxListItemAttr(*info
.GetAttributes()));
1208 else *attr
= *info
.GetAttributes();
1210 m_hasAnyAttr
= TRUE
;
1213 return (long) ListView_InsertItem(GetHwnd(), & item
);
1216 long wxListCtrl::InsertItem(long index
, const wxString
& label
)
1219 info
.m_text
= label
;
1220 info
.m_mask
= wxLIST_MASK_TEXT
;
1221 info
.m_itemId
= index
;
1222 return InsertItem(info
);
1225 // Inserts an image item
1226 long wxListCtrl::InsertItem(long index
, int imageIndex
)
1229 info
.m_image
= imageIndex
;
1230 info
.m_mask
= wxLIST_MASK_IMAGE
;
1231 info
.m_itemId
= index
;
1232 return InsertItem(info
);
1235 // Inserts an image/string item
1236 long wxListCtrl::InsertItem(long index
, const wxString
& label
, int imageIndex
)
1239 info
.m_image
= imageIndex
;
1240 info
.m_text
= label
;
1241 info
.m_mask
= wxLIST_MASK_IMAGE
| wxLIST_MASK_TEXT
;
1242 info
.m_itemId
= index
;
1243 return InsertItem(info
);
1246 // For list view mode (only), inserts a column.
1247 long wxListCtrl::InsertColumn(long col
, wxListItem
& item
)
1252 lvCol
.pszText
= NULL
;
1254 if ( item
.m_mask
& wxLIST_MASK_TEXT
)
1256 lvCol
.mask
|= LVCF_TEXT
;
1257 lvCol
.pszText
= WXSTRINGCAST item
.m_text
;
1258 lvCol
.cchTextMax
= 0; // Ignored
1260 if ( item
.m_mask
& wxLIST_MASK_FORMAT
)
1262 lvCol
.mask
|= LVCF_FMT
;
1264 if ( item
.m_format
== wxLIST_FORMAT_LEFT
)
1265 lvCol
.fmt
= LVCFMT_LEFT
;
1266 if ( item
.m_format
== wxLIST_FORMAT_RIGHT
)
1267 lvCol
.fmt
= LVCFMT_RIGHT
;
1268 if ( item
.m_format
== wxLIST_FORMAT_CENTRE
)
1269 lvCol
.fmt
= LVCFMT_CENTER
;
1272 lvCol
.mask
|= LVCF_WIDTH
;
1273 if ( item
.m_mask
& wxLIST_MASK_WIDTH
)
1275 if ( item
.m_width
== wxLIST_AUTOSIZE
)
1276 lvCol
.cx
= LVSCW_AUTOSIZE
;
1277 else if ( item
.m_width
== wxLIST_AUTOSIZE_USEHEADER
)
1278 lvCol
.cx
= LVSCW_AUTOSIZE_USEHEADER
;
1280 lvCol
.cx
= item
.m_width
;
1284 // always give some width to the new column: this one is compatible
1289 lvCol
.mask
|= LVCF_SUBITEM
;
1290 lvCol
.iSubItem
= col
;
1292 bool success
= ListView_InsertColumn(GetHwnd(), col
, & lvCol
) != -1;
1299 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1306 long wxListCtrl::InsertColumn(long col
,
1307 const wxString
& heading
,
1312 item
.m_mask
= wxLIST_MASK_TEXT
| wxLIST_MASK_FORMAT
;
1313 item
.m_text
= heading
;
1316 item
.m_mask
|= wxLIST_MASK_WIDTH
;
1317 item
.m_width
= width
;
1319 item
.m_format
= format
;
1321 return InsertColumn(col
, item
);
1324 // Scrolls the list control. If in icon, small icon or report view mode,
1325 // x specifies the number of pixels to scroll. If in list view mode, x
1326 // specifies the number of columns to scroll.
1327 // If in icon, small icon or list view mode, y specifies the number of pixels
1328 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1329 bool wxListCtrl::ScrollList(int dx
, int dy
)
1331 return (ListView_Scroll(GetHwnd(), dx
, dy
) != 0);
1336 // fn is a function which takes 3 long arguments: item1, item2, data.
1337 // item1 is the long data associated with a first item (NOT the index).
1338 // item2 is the long data associated with a second item (NOT the index).
1339 // data is the same value as passed to SortItems.
1340 // The return value is a negative number if the first item should precede the second
1341 // item, a positive number of the second item should precede the first,
1342 // or zero if the two items are equivalent.
1344 // data is arbitrary data to be passed to the sort function.
1345 bool wxListCtrl::SortItems(wxListCtrlCompare fn
, long data
)
1347 return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE
) fn
, data
) != 0);
1350 // ----------------------------------------------------------------------------
1351 // message processing
1352 // ----------------------------------------------------------------------------
1354 bool wxListCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1356 if (cmd
== EN_UPDATE
)
1358 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1359 event
.SetEventObject( this );
1360 ProcessCommand(event
);
1363 else if (cmd
== EN_KILLFOCUS
)
1365 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1366 event
.SetEventObject( this );
1367 ProcessCommand(event
);
1374 bool wxListCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1376 // prepare the event
1377 // -----------------
1379 wxListEvent
event(wxEVT_NULL
, m_windowId
);
1380 wxEventType eventType
= wxEVT_NULL
;
1382 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1384 // almost all messages use NM_LISTVIEW
1385 NM_LISTVIEW
*nmLV
= (NM_LISTVIEW
*)nmhdr
;
1387 // this is true for almost all events
1388 event
.m_item
.m_data
= nmLV
->lParam
;
1390 switch ( nmhdr
->code
)
1392 case LVN_BEGINRDRAG
:
1393 eventType
= wxEVT_COMMAND_LIST_BEGIN_RDRAG
;
1397 if ( eventType
== wxEVT_NULL
)
1399 eventType
= wxEVT_COMMAND_LIST_BEGIN_DRAG
;
1402 event
.m_itemIndex
= nmLV
->iItem
;
1403 event
.m_pointDrag
.x
= nmLV
->ptAction
.x
;
1404 event
.m_pointDrag
.y
= nmLV
->ptAction
.y
;
1407 case LVN_BEGINLABELEDIT
:
1409 eventType
= wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT
;
1410 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1411 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1415 case LVN_COLUMNCLICK
:
1416 eventType
= wxEVT_COMMAND_LIST_COL_CLICK
;
1417 event
.m_itemIndex
= -1;
1418 event
.m_col
= nmLV
->iSubItem
;
1421 case LVN_DELETEALLITEMS
:
1422 eventType
= wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS
;
1423 event
.m_itemIndex
= -1;
1429 case LVN_DELETEITEM
:
1430 eventType
= wxEVT_COMMAND_LIST_DELETE_ITEM
;
1431 event
.m_itemIndex
= nmLV
->iItem
;
1435 delete (wxListItemAttr
*)m_attrs
.Delete(nmLV
->iItem
);
1439 case LVN_ENDLABELEDIT
:
1441 eventType
= wxEVT_COMMAND_LIST_END_LABEL_EDIT
;
1442 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1443 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
);
1444 if ( info
->item
.pszText
== NULL
|| info
->item
.iItem
== -1 )
1449 case LVN_SETDISPINFO
:
1451 eventType
= wxEVT_COMMAND_LIST_SET_INFO
;
1452 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1453 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1457 case LVN_GETDISPINFO
:
1458 // this provokes stack overflow: indeed, wxConvertFromMSWListItem()
1459 // sends us WM_NOTIFY! As it doesn't do anything for now, just leave
1463 // TODO: some text buffering here, I think
1464 // TODO: API for getting Windows to retrieve values
1466 eventType
= wxEVT_COMMAND_LIST_GET_INFO
;
1467 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1468 wxConvertFromMSWListItem(this, event
.m_item
, info
->item
, GetHwnd());
1475 case LVN_INSERTITEM
:
1476 eventType
= wxEVT_COMMAND_LIST_INSERT_ITEM
;
1477 event
.m_itemIndex
= nmLV
->iItem
;
1480 case LVN_ITEMCHANGED
:
1481 // This needs to be sent to wxListCtrl as a rather more concrete
1482 // event. For now, just detect a selection or deselection.
1483 if ( (nmLV
->uNewState
& LVIS_SELECTED
) && !(nmLV
->uOldState
& LVIS_SELECTED
) )
1485 eventType
= wxEVT_COMMAND_LIST_ITEM_SELECTED
;
1486 event
.m_itemIndex
= nmLV
->iItem
;
1488 else if ( !(nmLV
->uNewState
& LVIS_SELECTED
) && (nmLV
->uOldState
& LVIS_SELECTED
) )
1490 eventType
= wxEVT_COMMAND_LIST_ITEM_DESELECTED
;
1491 event
.m_itemIndex
= nmLV
->iItem
;
1501 LV_KEYDOWN
*info
= (LV_KEYDOWN
*)lParam
;
1502 WORD wVKey
= info
->wVKey
;
1504 // get the current selection
1505 long lItem
= GetNextItem(-1,
1507 wxLIST_STATE_SELECTED
);
1509 // <Enter> or <Space> activate the selected item if any (but
1510 // not with Shift and/or Ctrl as then they have a predefined
1511 // meaning for the list view)
1513 (wVKey
== VK_RETURN
|| wVKey
== VK_SPACE
) &&
1514 !(wxIsShiftDown() || wxIsCtrlDown()) )
1516 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1517 event
.m_itemIndex
= lItem
;
1521 eventType
= wxEVT_COMMAND_LIST_KEY_DOWN
;
1522 event
.m_code
= wxCharCodeMSWToWX(wVKey
);
1527 // fill the other fields too
1528 event
.m_item
.m_text
= GetItemText(lItem
);
1529 event
.m_item
.m_data
= GetItemData(lItem
);
1535 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1537 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1542 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1543 // if it happened on an item (and not on empty place)
1544 if ( nmLV
->iItem
== -1 )
1550 eventType
= wxEVT_COMMAND_LIST_ITEM_ACTIVATED
;
1551 event
.m_itemIndex
= nmLV
->iItem
;
1552 event
.m_item
.m_text
= GetItemText(nmLV
->iItem
);
1553 event
.m_item
.m_data
= GetItemData(nmLV
->iItem
);
1557 /* TECH NOTE: NM_RCLICK isn't really good enough here. We want to
1558 subclass and check for the actual WM_RBUTTONDOWN message,
1559 because NM_RCLICK waits for the WM_RBUTTONUP message as well
1560 before firing off. We want to have notify events for both down
1563 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1564 // don't do anything else
1565 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1570 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1571 LV_HITTESTINFO lvhti
;
1572 wxZeroMemory(lvhti
);
1574 ::GetCursorPos(&(lvhti
.pt
));
1575 ::ScreenToClient(GetHwnd(),&(lvhti
.pt
));
1576 if ( ListView_HitTest(GetHwnd(),&lvhti
) != -1 )
1578 if ( lvhti
.flags
& LVHT_ONITEM
)
1580 eventType
= wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK
;
1581 event
.m_itemIndex
= lvhti
.iItem
;
1582 event
.m_pointDrag
.x
= lvhti
.pt
.x
;
1583 event
.m_pointDrag
.y
= lvhti
.pt
.y
;
1590 case NM_MCLICK
: // ***** THERE IS NO NM_MCLICK. Subclass anyone? ******
1592 // if the user processes it in wxEVT_COMMAND_MIDDLE_CLICK(), don't do
1594 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1599 // else translate it into wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK event
1600 eventType
= wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK
;
1601 NMITEMACTIVATE
* hdr
= (NMITEMACTIVATE
*)lParam
;
1602 event
.m_itemIndex
= hdr
->iItem
;
1607 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1610 LPNMLVCUSTOMDRAW lplvcd
= (LPNMLVCUSTOMDRAW
)lParam
;
1611 NMCUSTOMDRAW
& nmcd
= lplvcd
->nmcd
;
1612 switch( nmcd
.dwDrawStage
)
1615 // if we've got any items with non standard attributes,
1616 // notify us before painting each item
1617 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1621 case CDDS_ITEMPREPAINT
:
1623 wxListItemAttr
*attr
=
1624 (wxListItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
1628 // nothing to do for this item
1629 return CDRF_DODEFAULT
;
1633 wxColour colText
, colBack
;
1634 if ( attr
->HasFont() )
1636 wxFont font
= attr
->GetFont();
1637 hFont
= (HFONT
)font
.GetResourceHandle();
1644 if ( attr
->HasTextColour() )
1646 colText
= attr
->GetTextColour();
1650 colText
= GetTextColour();
1653 if ( attr
->HasBackgroundColour() )
1655 colBack
= attr
->GetBackgroundColour();
1659 colBack
= GetBackgroundColour();
1662 // note that if we wanted to set colours for
1663 // individual columns (subitems), we would have
1664 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1667 ::SelectObject(nmcd
.hdc
, hFont
);
1669 *result
= CDRF_NEWFONT
;
1673 *result
= CDRF_DODEFAULT
;
1676 lplvcd
->clrText
= wxColourToRGB(colText
);
1677 lplvcd
->clrTextBk
= wxColourToRGB(colBack
);
1683 *result
= CDRF_DODEFAULT
;
1687 // break; // can never be reached
1688 #endif // _WIN32_IE >= 0x300
1691 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1694 // process the event
1695 // -----------------
1697 event
.SetEventObject( this );
1698 event
.SetEventType(eventType
);
1700 if ( !GetEventHandler()->ProcessEvent(event
) )
1706 switch ( nmhdr
->code
)
1708 case LVN_DELETEALLITEMS
:
1709 // always return TRUE to suppress all additional LVN_DELETEITEM
1710 // notifications - this makes deleting all items from a list ctrl
1716 case LVN_GETDISPINFO
:
1718 LV_DISPINFO
*info
= (LV_DISPINFO
*)lParam
;
1719 if ( info
->item
.mask
& LVIF_TEXT
)
1721 if ( !event
.m_item
.m_text
.IsNull() )
1723 info
->item
.pszText
= AddPool(event
.m_item
.m_text
);
1724 info
->item
.cchTextMax
= wxStrlen(info
->item
.pszText
) + 1;
1727 // wxConvertToMSWListItem(this, event.m_item, info->item);
1730 case LVN_ENDLABELEDIT
:
1732 *result
= event
.IsAllowed();
1737 *result
= !event
.IsAllowed();
1742 wxChar
*wxListCtrl::AddPool(const wxString
& str
)
1744 // Remove the first element if 3 strings exist
1745 if ( m_stringPool
.Number() == 3 )
1747 wxNode
*node
= m_stringPool
.First();
1748 delete[] (char *)node
->Data();
1751 wxNode
*node
= m_stringPool
.Add(WXSTRINGCAST str
);
1752 return (wxChar
*)node
->Data();
1755 // Necessary for drawing hrules and vrules, if specified
1756 void wxListCtrl::OnPaint(wxPaintEvent
& event
)
1760 wxControl::OnPaint(event
);
1762 // Reset the device origin since it may have been set
1763 dc
.SetDeviceOrigin(0, 0);
1765 bool drawHRules
= ((GetWindowStyle() & wxLC_HRULES
) != 0);
1766 bool drawVRules
= ((GetWindowStyle() & wxLC_VRULES
) != 0);
1768 if (!drawHRules
&& !drawVRules
)
1770 if ((GetWindowStyle() & wxLC_REPORT
) == 0)
1773 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
1775 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
1777 wxSize clientSize
= GetClientSize();
1781 int itemCount
= GetItemCount();
1783 for (i
= 0; i
< itemCount
; i
++)
1785 if (GetItemRect(i
, itemRect
))
1787 cy
= itemRect
.GetTop();
1788 if (i
!= 0) // Don't draw the first one
1790 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1793 if (i
== (GetItemCount() - 1))
1795 cy
= itemRect
.GetBottom();
1796 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
1800 i
= (GetItemCount() - 1);
1801 if (drawVRules
&& (i
> -1))
1803 wxRect firstItemRect
;
1804 GetItemRect(0, firstItemRect
);
1806 if (GetItemRect(i
, itemRect
))
1809 int x
= itemRect
.GetX();
1810 for (col
= 0; col
< GetColumnCount(); col
++)
1812 int colWidth
= GetColumnWidth(col
);
1814 dc
.DrawLine(x
, firstItemRect
.GetY() - 2, x
, itemRect
.GetBottom());
1820 // ----------------------------------------------------------------------------
1822 // ----------------------------------------------------------------------------
1824 // List item structure
1825 wxListItem::wxListItem()
1835 m_format
= wxLIST_FORMAT_CENTRE
;
1841 void wxListItem::Clear()
1850 m_format
= wxLIST_FORMAT_CENTRE
;
1852 m_text
= wxEmptyString
;
1854 if (m_attr
) delete m_attr
;
1858 void wxListItem::ClearAttributes()
1860 if (m_attr
) delete m_attr
;
1864 static void wxConvertFromMSWListItem(const wxListCtrl
*WXUNUSED(ctrl
), wxListItem
& info
, LV_ITEM
& lvItem
, HWND getFullInfo
)
1866 info
.m_data
= lvItem
.lParam
;
1869 info
.m_stateMask
= 0;
1870 info
.m_itemId
= lvItem
.iItem
;
1872 long oldMask
= lvItem
.mask
;
1874 bool needText
= FALSE
;
1875 if (getFullInfo
!= 0)
1877 if ( lvItem
.mask
& LVIF_TEXT
)
1884 lvItem
.pszText
= new wxChar
[513];
1885 lvItem
.cchTextMax
= 512;
1887 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
1888 lvItem
.mask
|= LVIF_TEXT
| LVIF_IMAGE
| LVIF_PARAM
;
1889 ::SendMessage(getFullInfo
, LVM_GETITEM
, 0, (LPARAM
)& lvItem
);
1892 if ( lvItem
.mask
& LVIF_STATE
)
1894 info
.m_mask
|= wxLIST_MASK_STATE
;
1896 if ( lvItem
.stateMask
& LVIS_CUT
)
1898 info
.m_stateMask
|= wxLIST_STATE_CUT
;
1899 if ( lvItem
.state
& LVIS_CUT
)
1900 info
.m_state
|= wxLIST_STATE_CUT
;
1902 if ( lvItem
.stateMask
& LVIS_DROPHILITED
)
1904 info
.m_stateMask
|= wxLIST_STATE_DROPHILITED
;
1905 if ( lvItem
.state
& LVIS_DROPHILITED
)
1906 info
.m_state
|= wxLIST_STATE_DROPHILITED
;
1908 if ( lvItem
.stateMask
& LVIS_FOCUSED
)
1910 info
.m_stateMask
|= wxLIST_STATE_FOCUSED
;
1911 if ( lvItem
.state
& LVIS_FOCUSED
)
1912 info
.m_state
|= wxLIST_STATE_FOCUSED
;
1914 if ( lvItem
.stateMask
& LVIS_SELECTED
)
1916 info
.m_stateMask
|= wxLIST_STATE_SELECTED
;
1917 if ( lvItem
.state
& LVIS_SELECTED
)
1918 info
.m_state
|= wxLIST_STATE_SELECTED
;
1922 if ( lvItem
.mask
& LVIF_TEXT
)
1924 info
.m_mask
|= wxLIST_MASK_TEXT
;
1925 info
.m_text
= lvItem
.pszText
;
1927 if ( lvItem
.mask
& LVIF_IMAGE
)
1929 info
.m_mask
|= wxLIST_MASK_IMAGE
;
1930 info
.m_image
= lvItem
.iImage
;
1932 if ( lvItem
.mask
& LVIF_PARAM
)
1933 info
.m_mask
|= wxLIST_MASK_DATA
;
1934 if ( lvItem
.mask
& LVIF_DI_SETITEM
)
1935 info
.m_mask
|= wxLIST_SET_ITEM
;
1936 info
.m_col
= lvItem
.iSubItem
;
1941 delete[] lvItem
.pszText
;
1943 lvItem
.mask
= oldMask
;
1946 static void wxConvertToMSWListItem(const wxListCtrl
*ctrl
, wxListItem
& info
, LV_ITEM
& lvItem
)
1948 lvItem
.iItem
= (int) info
.m_itemId
;
1950 lvItem
.iImage
= info
.m_image
;
1951 lvItem
.lParam
= info
.m_data
;
1952 lvItem
.stateMask
= 0;
1955 lvItem
.iSubItem
= info
.m_col
;
1957 if (info
.m_mask
& wxLIST_MASK_STATE
)
1959 lvItem
.mask
|= LVIF_STATE
;
1960 if (info
.m_stateMask
& wxLIST_STATE_CUT
)
1962 lvItem
.stateMask
|= LVIS_CUT
;
1963 if (info
.m_state
& wxLIST_STATE_CUT
)
1964 lvItem
.state
|= LVIS_CUT
;
1966 if (info
.m_stateMask
& wxLIST_STATE_DROPHILITED
)
1968 lvItem
.stateMask
|= LVIS_DROPHILITED
;
1969 if (info
.m_state
& wxLIST_STATE_DROPHILITED
)
1970 lvItem
.state
|= LVIS_DROPHILITED
;
1972 if (info
.m_stateMask
& wxLIST_STATE_FOCUSED
)
1974 lvItem
.stateMask
|= LVIS_FOCUSED
;
1975 if (info
.m_state
& wxLIST_STATE_FOCUSED
)
1976 lvItem
.state
|= LVIS_FOCUSED
;
1978 if (info
.m_stateMask
& wxLIST_STATE_SELECTED
)
1980 lvItem
.stateMask
|= LVIS_SELECTED
;
1981 if (info
.m_state
& wxLIST_STATE_SELECTED
)
1982 lvItem
.state
|= LVIS_SELECTED
;
1986 if (info
.m_mask
& wxLIST_MASK_TEXT
)
1988 lvItem
.mask
|= LVIF_TEXT
;
1989 if ( ctrl
->GetWindowStyleFlag() & wxLC_USER_TEXT
)
1991 lvItem
.pszText
= LPSTR_TEXTCALLBACK
;
1995 // pszText is not const, hence the cast
1996 lvItem
.pszText
= (wxChar
*)info
.m_text
.c_str();
1997 if ( lvItem
.pszText
)
1998 lvItem
.cchTextMax
= info
.m_text
.Length();
2000 lvItem
.cchTextMax
= 0;
2003 if (info
.m_mask
& wxLIST_MASK_IMAGE
)
2004 lvItem
.mask
|= LVIF_IMAGE
;
2005 if (info
.m_mask
& wxLIST_MASK_DATA
)
2006 lvItem
.mask
|= LVIF_PARAM
;
2009 // ----------------------------------------------------------------------------
2011 // ----------------------------------------------------------------------------
2013 IMPLEMENT_DYNAMIC_CLASS(wxListEvent
, wxNotifyEvent
)
2015 wxListEvent::wxListEvent(wxEventType commandType
, int id
)
2016 : wxNotifyEvent(commandType
, id
)
2022 m_cancelled
= FALSE
;
2027 #endif // wxUSE_LISTCTRL