]> git.saurik.com Git - wxWidgets.git/blob - src/msw/listctrl.cpp
simplified RTTI chain: wxFrame base class is now directly wxWindow, not wxFrameMSW
[wxWidgets.git] / src / msw / listctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: listctrl.cpp
3 // Purpose: wxListCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "listctrl.h"
22 #pragma implementation "listctrlbase.h"
23 #endif
24
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #if wxUSE_LISTCTRL && defined(__WIN95__)
33
34 #ifndef WX_PRECOMP
35 #include "wx/app.h"
36 #include "wx/intl.h"
37 #include "wx/log.h"
38 #include "wx/settings.h"
39 #endif
40
41 #include "wx/textctrl.h"
42 #include "wx/imaglist.h"
43 #include "wx/listctrl.h"
44 #include "wx/dcclient.h"
45
46 #include "wx/msw/private.h"
47
48 #if ((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
49 #include "wx/msw/gnuwin32/extra.h"
50 #else
51 #include <commctrl.h>
52 #endif
53
54 #ifndef LVHT_ONITEM
55 #define LVHT_ONITEM \
56 (LVHT_ONITEMICON | LVHT_ONITEMLABEL | LVHT_ONITEMSTATEICON)
57 #endif
58
59 #ifndef LVM_SETEXTENDEDLISTVIEWSTYLE
60 #define LVM_SETEXTENDEDLISTVIEWSTYLE (0x1000 + 54)
61 #endif
62
63 #ifndef LVS_EX_FULLROWSELECT
64 #define LVS_EX_FULLROWSELECT 0x00000020
65 #endif
66
67 #ifndef LVS_OWNERDATA
68 #define LVS_OWNERDATA 0x1000
69 #endif
70
71 // ----------------------------------------------------------------------------
72 // private functions
73 // ----------------------------------------------------------------------------
74
75 static void wxConvertToMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& tvItem);
76 static void wxConvertFromMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& tvItem, HWND getFullInfo = 0);
77
78 // ----------------------------------------------------------------------------
79 // events
80 // ----------------------------------------------------------------------------
81
82 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG)
83 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG)
84 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT)
85 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT)
86 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM)
87 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS)
88 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO)
89 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO)
90 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED)
91 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED)
92 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN)
93 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM)
94 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK)
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED)
98
99 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
100 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
101
102 BEGIN_EVENT_TABLE(wxListCtrl, wxControl)
103 EVT_PAINT(wxListCtrl::OnPaint)
104 END_EVENT_TABLE()
105
106 // ============================================================================
107 // implementation
108 // ============================================================================
109
110 // ----------------------------------------------------------------------------
111 // wxListEvent
112 // ----------------------------------------------------------------------------
113
114 void wxListEvent::CopyObject(wxObject& object_dest) const
115 {
116 wxListEvent *obj = (wxListEvent *)&object_dest;
117
118 wxNotifyEvent::CopyObject(object_dest);
119
120 obj->m_code = m_code;
121 obj->m_itemIndex = m_itemIndex;
122 obj->m_oldItemIndex = m_oldItemIndex;
123 obj->m_col = m_col;
124 obj->m_cancelled = m_cancelled;
125 obj->m_pointDrag = m_pointDrag;
126 obj->m_item.m_mask = m_item.m_mask;
127 obj->m_item.m_itemId = m_item.m_itemId;
128 obj->m_item.m_col = m_item.m_col;
129 obj->m_item.m_state = m_item.m_state;
130 obj->m_item.m_stateMask = m_item.m_stateMask;
131 obj->m_item.m_text = m_item.m_text;
132 obj->m_item.m_image = m_item.m_image;
133 obj->m_item.m_data = m_item.m_data;
134 obj->m_item.m_format = m_item.m_format;
135 obj->m_item.m_width = m_item.m_width;
136
137 if ( m_item.HasAttributes() )
138 {
139 obj->m_item.SetTextColour(m_item.GetTextColour());
140 obj->m_item.SetBackgroundColour(m_item.GetBackgroundColour());
141 obj->m_item.SetFont(m_item.GetFont());
142 }
143 }
144
145 // ----------------------------------------------------------------------------
146 // wxListCtrl construction
147 // ----------------------------------------------------------------------------
148
149 void wxListCtrl::Init()
150 {
151 m_imageListNormal = NULL;
152 m_imageListSmall = NULL;
153 m_imageListState = NULL;
154 m_ownsImageListNormal = m_ownsImageListSmall = m_ownsImageListState = FALSE;
155 m_baseStyle = 0;
156 m_colCount = 0;
157 m_textCtrl = NULL;
158 m_hasAnyAttr = FALSE;
159 }
160
161 bool wxListCtrl::Create(wxWindow *parent,
162 wxWindowID id,
163 const wxPoint& pos,
164 const wxSize& size,
165 long style,
166 const wxValidator& validator,
167 const wxString& name)
168 {
169 #if wxUSE_VALIDATORS
170 SetValidator(validator);
171 #endif // wxUSE_VALIDATORS
172
173 SetName(name);
174
175 int x = pos.x;
176 int y = pos.y;
177 int width = size.x;
178 int height = size.y;
179
180 m_windowStyle = style;
181
182 SetParent(parent);
183
184 if (width <= 0)
185 width = 100;
186 if (height <= 0)
187 height = 30;
188 if (x < 0)
189 x = 0;
190 if (y < 0)
191 y = 0;
192
193 m_windowId = (id == -1) ? NewControlId() : id;
194
195 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP |
196 LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS;
197
198 if ( m_windowStyle & wxCLIP_SIBLINGS )
199 wstyle |= WS_CLIPSIBLINGS;
200
201 if ( wxStyleHasBorder(m_windowStyle) )
202 wstyle |= WS_BORDER;
203 m_baseStyle = wstyle;
204
205 if ( !DoCreateControl(x, y, width, height) )
206 return FALSE;
207
208 if (parent)
209 parent->AddChild(this);
210
211 return TRUE;
212 }
213
214 bool wxListCtrl::DoCreateControl(int x, int y, int w, int h)
215 {
216 DWORD wstyle = m_baseStyle;
217
218 bool want3D;
219 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
220
221 // Even with extended styles, need to combine with WS_BORDER
222 // for them to look right.
223 if ( want3D )
224 wstyle |= WS_BORDER;
225
226 long oldStyle = 0; // Dummy
227 wstyle |= ConvertToMSWStyle(oldStyle, m_windowStyle);
228
229 // Create the ListView control.
230 m_hWnd = (WXHWND)CreateWindowEx(exStyle,
231 WC_LISTVIEW,
232 wxT(""),
233 wstyle,
234 x, y, w, h,
235 GetWinHwnd(GetParent()),
236 (HMENU)m_windowId,
237 wxGetInstance(),
238 NULL);
239
240 if ( !m_hWnd )
241 {
242 wxLogError(_("Can't create list control window, check that comctl32.dll is installed."));
243
244 return FALSE;
245 }
246
247 // for comctl32.dll v 4.70+ we want to have this attribute because it's
248 // prettier (and also because wxGTK does it like this)
249 if ( (wstyle & LVS_REPORT) && wxTheApp->GetComCtl32Version() >= 470 )
250 {
251 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE,
252 0, LVS_EX_FULLROWSELECT);
253 }
254
255 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
256 SetForegroundColour(GetParent()->GetForegroundColour());
257
258 SubclassWin(m_hWnd);
259
260 return TRUE;
261 }
262
263 void wxListCtrl::UpdateStyle()
264 {
265 if ( GetHWND() )
266 {
267 // The new window view style
268 long dummy;
269 DWORD dwStyleNew = ConvertToMSWStyle(dummy, m_windowStyle);
270 dwStyleNew |= m_baseStyle;
271
272 // Get the current window style.
273 DWORD dwStyleOld = ::GetWindowLong(GetHwnd(), GWL_STYLE);
274
275 // Only set the window style if the view bits have changed.
276 if ( dwStyleOld != dwStyleNew )
277 {
278 ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyleNew);
279 }
280 }
281 }
282
283 void wxListCtrl::FreeAllAttrs(bool dontRecreate)
284 {
285 if ( m_hasAnyAttr )
286 {
287 for ( wxNode *node = m_attrs.Next(); node; node = m_attrs.Next() )
288 {
289 delete (wxListItemAttr *)node->Data();
290 }
291
292 m_attrs.Destroy();
293 if ( !dontRecreate )
294 {
295 m_attrs.Create(wxKEY_INTEGER, 1000); // just as def ctor
296 }
297
298 m_hasAnyAttr = FALSE;
299 }
300 }
301
302 wxListCtrl::~wxListCtrl()
303 {
304 FreeAllAttrs(TRUE /* no need to recreate hash any more */);
305
306 if ( m_textCtrl )
307 {
308 m_textCtrl->SetHWND(0);
309 m_textCtrl->UnsubclassWin();
310 delete m_textCtrl;
311 m_textCtrl = NULL;
312 }
313
314 if (m_ownsImageListNormal) delete m_imageListNormal;
315 if (m_ownsImageListSmall) delete m_imageListSmall;
316 if (m_ownsImageListState) delete m_imageListState;
317 }
318
319 // ----------------------------------------------------------------------------
320 // set/get/change style
321 // ----------------------------------------------------------------------------
322
323 // Add or remove a single window style
324 void wxListCtrl::SetSingleStyle(long style, bool add)
325 {
326 long flag = GetWindowStyleFlag();
327
328 // Get rid of conflicting styles
329 if ( add )
330 {
331 if ( style & wxLC_MASK_TYPE)
332 flag = flag & ~wxLC_MASK_TYPE;
333 if ( style & wxLC_MASK_ALIGN )
334 flag = flag & ~wxLC_MASK_ALIGN;
335 if ( style & wxLC_MASK_SORT )
336 flag = flag & ~wxLC_MASK_SORT;
337 }
338
339 if ( flag & style )
340 {
341 if ( !add )
342 flag -= style;
343 }
344 else
345 {
346 if ( add )
347 {
348 flag |= style;
349 }
350 }
351
352 m_windowStyle = flag;
353
354 UpdateStyle();
355 }
356
357 // Set the whole window style
358 void wxListCtrl::SetWindowStyleFlag(long flag)
359 {
360 m_windowStyle = flag;
361
362 UpdateStyle();
363 }
364
365 // Can be just a single style, or a bitlist
366 long wxListCtrl::ConvertToMSWStyle(long& oldStyle, long style) const
367 {
368 long wstyle = 0;
369 if ( style & wxLC_ICON )
370 {
371 if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON )
372 oldStyle -= LVS_SMALLICON;
373 if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT )
374 oldStyle -= LVS_REPORT;
375 if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST )
376 oldStyle -= LVS_LIST;
377 wstyle |= LVS_ICON;
378 }
379
380 if ( style & wxLC_SMALL_ICON )
381 {
382 if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON )
383 oldStyle -= LVS_ICON;
384 if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT )
385 oldStyle -= LVS_REPORT;
386 if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST )
387 oldStyle -= LVS_LIST;
388 wstyle |= LVS_SMALLICON;
389 }
390
391 if ( style & wxLC_LIST )
392 {
393 if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON )
394 oldStyle -= LVS_ICON;
395 if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT )
396 oldStyle -= LVS_REPORT;
397 if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON )
398 oldStyle -= LVS_SMALLICON;
399 wstyle |= LVS_LIST;
400 }
401
402 if ( style & wxLC_REPORT )
403 {
404 if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON )
405 oldStyle -= LVS_ICON;
406 if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST )
407 oldStyle -= LVS_LIST;
408 if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON )
409 oldStyle -= LVS_SMALLICON;
410
411 wstyle |= LVS_REPORT;
412 }
413
414 if ( style & wxLC_ALIGN_LEFT )
415 {
416 if ( oldStyle & LVS_ALIGNTOP )
417 oldStyle -= LVS_ALIGNTOP;
418 wstyle |= LVS_ALIGNLEFT;
419 }
420
421 if ( style & wxLC_ALIGN_TOP )
422 {
423 if ( oldStyle & LVS_ALIGNLEFT )
424 oldStyle -= LVS_ALIGNLEFT;
425 wstyle |= LVS_ALIGNTOP;
426 }
427
428 if ( style & wxLC_AUTOARRANGE )
429 wstyle |= LVS_AUTOARRANGE;
430
431 if ( style & wxLC_NO_SORT_HEADER )
432 wstyle |= LVS_NOSORTHEADER;
433
434 if ( style & wxLC_NO_HEADER )
435 wstyle |= LVS_NOCOLUMNHEADER;
436
437 if ( style & wxLC_EDIT_LABELS )
438 wstyle |= LVS_EDITLABELS;
439
440 if ( style & wxLC_SINGLE_SEL )
441 wstyle |= LVS_SINGLESEL;
442
443 if ( style & wxLC_SORT_ASCENDING )
444 {
445 if ( oldStyle & LVS_SORTDESCENDING )
446 oldStyle -= LVS_SORTDESCENDING;
447 wstyle |= LVS_SORTASCENDING;
448 }
449
450 if ( style & wxLC_SORT_DESCENDING )
451 {
452 if ( oldStyle & LVS_SORTASCENDING )
453 oldStyle -= LVS_SORTASCENDING;
454 wstyle |= LVS_SORTDESCENDING;
455 }
456
457 if ( style & wxLC_VIRTUAL )
458 {
459 int ver = wxTheApp->GetComCtl32Version();
460 if ( ver < 470 )
461 {
462 wxLogWarning(_("Please install a newer version of comctl32.dll\n"
463 "(at least version 4.70 is required but you have "
464 "%d.%02d)\n"
465 "or this program won't operate correctly."),
466 ver / 100, ver % 100);
467 }
468
469 wstyle |= LVS_OWNERDATA;
470 }
471
472 return wstyle;
473 }
474
475 // ----------------------------------------------------------------------------
476 // accessors
477 // ----------------------------------------------------------------------------
478
479 // Sets the foreground, i.e. text, colour
480 bool wxListCtrl::SetForegroundColour(const wxColour& col)
481 {
482 if ( !wxWindow::SetForegroundColour(col) )
483 return FALSE;
484
485 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col));
486
487 return TRUE;
488 }
489
490 // Sets the background colour
491 bool wxListCtrl::SetBackgroundColour(const wxColour& col)
492 {
493 if ( !wxWindow::SetBackgroundColour(col) )
494 return FALSE;
495
496 // we set the same colour for both the "empty" background and the items
497 // background
498 COLORREF color = wxColourToRGB(col);
499 ListView_SetBkColor(GetHwnd(), color);
500 ListView_SetTextBkColor(GetHwnd(), color);
501
502 return TRUE;
503 }
504
505 // Gets information about this column
506 bool wxListCtrl::GetColumn(int col, wxListItem& item) const
507 {
508 LV_COLUMN lvCol;
509 lvCol.mask = 0;
510 lvCol.fmt = 0;
511 lvCol.pszText = NULL;
512
513 if ( item.m_mask & wxLIST_MASK_TEXT )
514 {
515 lvCol.mask |= LVCF_TEXT;
516 lvCol.pszText = new wxChar[513];
517 lvCol.cchTextMax = 512;
518 }
519
520 bool success = (ListView_GetColumn(GetHwnd(), col, & lvCol) != 0);
521
522 // item.m_subItem = lvCol.iSubItem;
523 item.m_width = lvCol.cx;
524
525 if ( (item.m_mask & wxLIST_MASK_TEXT) && lvCol.pszText )
526 {
527 item.m_text = lvCol.pszText;
528 delete[] lvCol.pszText;
529 }
530
531 if ( item.m_mask & wxLIST_MASK_FORMAT )
532 {
533 if (lvCol.fmt == LVCFMT_LEFT)
534 item.m_format = wxLIST_FORMAT_LEFT;
535 else if (lvCol.fmt == LVCFMT_RIGHT)
536 item.m_format = wxLIST_FORMAT_RIGHT;
537 else if (lvCol.fmt == LVCFMT_CENTER)
538 item.m_format = wxLIST_FORMAT_CENTRE;
539 }
540
541 return success;
542 }
543
544 // Sets information about this column
545 bool wxListCtrl::SetColumn(int col, wxListItem& item)
546 {
547 LV_COLUMN lvCol;
548 lvCol.mask = 0;
549 lvCol.fmt = 0;
550 lvCol.pszText = NULL;
551
552 if ( item.m_mask & wxLIST_MASK_TEXT )
553 {
554 lvCol.mask |= LVCF_TEXT;
555 lvCol.pszText = WXSTRINGCAST item.m_text;
556 lvCol.cchTextMax = 0; // Ignored
557 }
558 if ( item.m_mask & wxLIST_MASK_FORMAT )
559 {
560 lvCol.mask |= LVCF_FMT;
561
562 if ( item.m_format == wxLIST_FORMAT_LEFT )
563 lvCol.fmt = LVCFMT_LEFT;
564 if ( item.m_format == wxLIST_FORMAT_RIGHT )
565 lvCol.fmt = LVCFMT_RIGHT;
566 if ( item.m_format == wxLIST_FORMAT_CENTRE )
567 lvCol.fmt = LVCFMT_CENTER;
568 }
569
570 if ( item.m_mask & wxLIST_MASK_WIDTH )
571 {
572 lvCol.mask |= LVCF_WIDTH;
573 lvCol.cx = item.m_width;
574
575 if ( lvCol.cx == wxLIST_AUTOSIZE)
576 lvCol.cx = LVSCW_AUTOSIZE;
577 else if ( lvCol.cx == wxLIST_AUTOSIZE_USEHEADER)
578 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
579 }
580 lvCol.mask |= LVCF_SUBITEM;
581 lvCol.iSubItem = col;
582 return (ListView_SetColumn(GetHwnd(), col, & lvCol) != 0);
583 }
584
585 // Gets the column width
586 int wxListCtrl::GetColumnWidth(int col) const
587 {
588 return ListView_GetColumnWidth(GetHwnd(), col);
589 }
590
591 // Sets the column width
592 bool wxListCtrl::SetColumnWidth(int col, int width)
593 {
594 int col2 = col;
595 if ( m_windowStyle & wxLC_LIST )
596 col2 = -1;
597
598 int width2 = width;
599 if ( width2 == wxLIST_AUTOSIZE)
600 width2 = LVSCW_AUTOSIZE;
601 else if ( width2 == wxLIST_AUTOSIZE_USEHEADER)
602 width2 = LVSCW_AUTOSIZE_USEHEADER;
603
604 return (ListView_SetColumnWidth(GetHwnd(), col2, width2) != 0);
605 }
606
607 // Gets the number of items that can fit vertically in the
608 // visible area of the list control (list or report view)
609 // or the total number of items in the list control (icon
610 // or small icon view)
611 int wxListCtrl::GetCountPerPage() const
612 {
613 return ListView_GetCountPerPage(GetHwnd());
614 }
615
616 // Gets the edit control for editing labels.
617 wxTextCtrl* wxListCtrl::GetEditControl() const
618 {
619 return m_textCtrl;
620 }
621
622 // Gets information about the item
623 bool wxListCtrl::GetItem(wxListItem& info) const
624 {
625 LV_ITEM lvItem;
626 wxZeroMemory(lvItem);
627
628 lvItem.iItem = info.m_itemId;
629 lvItem.iSubItem = info.m_col;
630
631 if ( info.m_mask & wxLIST_MASK_TEXT )
632 {
633 lvItem.mask |= LVIF_TEXT;
634 lvItem.pszText = new wxChar[513];
635 lvItem.cchTextMax = 512;
636 }
637 else
638 {
639 lvItem.pszText = NULL;
640 }
641
642 if (info.m_mask & wxLIST_MASK_DATA)
643 lvItem.mask |= LVIF_PARAM;
644
645 if (info.m_mask & wxLIST_MASK_IMAGE)
646 lvItem.mask |= LVIF_IMAGE;
647
648 if ( info.m_mask & wxLIST_MASK_STATE )
649 {
650 lvItem.mask |= LVIF_STATE;
651 // the other bits are hardly interesting anyhow
652 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
653 }
654
655 bool success = ListView_GetItem((HWND)GetHWND(), &lvItem) != 0;
656 if ( !success )
657 {
658 wxLogError(_("Couldn't retrieve information about list control item %d."),
659 lvItem.iItem);
660 }
661 else
662 {
663 wxConvertFromMSWListItem(this, info, lvItem);
664 }
665
666 if (lvItem.pszText)
667 delete[] lvItem.pszText;
668
669 return success;
670 }
671
672 // Sets information about the item
673 bool wxListCtrl::SetItem(wxListItem& info)
674 {
675 LV_ITEM item;
676 wxConvertToMSWListItem(this, info, item);
677
678 item.cchTextMax = 0;
679 if ( !ListView_SetItem(GetHwnd(), &item) )
680 {
681 wxLogDebug(_T("ListView_SetItem() failed"));
682
683 return FALSE;
684 }
685
686 // we need to update the item immediately to show the new image
687 bool updateNow = (info.m_mask & wxLIST_MASK_IMAGE) != 0;
688
689 // check whether it has any custom attributes
690 if ( info.HasAttributes() )
691 {
692 wxListItemAttr *attr = (wxListItemAttr *)m_attrs.Get(item.iItem);
693
694 if ( attr == NULL )
695 m_attrs.Put(item.iItem, (wxObject *)new wxListItemAttr(*info.GetAttributes()));
696 else
697 *attr = *info.GetAttributes();
698
699 m_hasAnyAttr = TRUE;
700
701 // if the colour has changed, we must redraw the item
702 updateNow = TRUE;
703 }
704
705 if ( updateNow )
706 {
707 // we need this to make the change visible right now
708 ListView_Update(GetHwnd(), item.iItem);
709 }
710
711 return TRUE;
712 }
713
714 long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId)
715 {
716 wxListItem info;
717 info.m_text = label;
718 info.m_mask = wxLIST_MASK_TEXT;
719 info.m_itemId = index;
720 info.m_col = col;
721 if ( imageId > -1 )
722 {
723 info.m_image = imageId;
724 info.m_mask |= wxLIST_MASK_IMAGE;
725 }
726 return SetItem(info);
727 }
728
729
730 // Gets the item state
731 int wxListCtrl::GetItemState(long item, long stateMask) const
732 {
733 wxListItem info;
734
735 info.m_mask = wxLIST_MASK_STATE;
736 info.m_stateMask = stateMask;
737 info.m_itemId = item;
738
739 if (!GetItem(info))
740 return 0;
741
742 return info.m_state;
743 }
744
745 // Sets the item state
746 bool wxListCtrl::SetItemState(long item, long state, long stateMask)
747 {
748 wxListItem info;
749
750 info.m_mask = wxLIST_MASK_STATE;
751 info.m_state = state;
752 info.m_stateMask = stateMask;
753 info.m_itemId = item;
754
755 return SetItem(info);
756 }
757
758 // Sets the item image
759 bool wxListCtrl::SetItemImage(long item, int image, int WXUNUSED(selImage))
760 {
761 wxListItem info;
762
763 info.m_mask = wxLIST_MASK_IMAGE;
764 info.m_image = image;
765 info.m_itemId = item;
766
767 return SetItem(info);
768 }
769
770 // Gets the item text
771 wxString wxListCtrl::GetItemText(long item) const
772 {
773 wxListItem info;
774
775 info.m_mask = wxLIST_MASK_TEXT;
776 info.m_itemId = item;
777
778 if (!GetItem(info))
779 return wxString("");
780 return info.m_text;
781 }
782
783 // Sets the item text
784 void wxListCtrl::SetItemText(long item, const wxString& str)
785 {
786 wxListItem info;
787
788 info.m_mask = wxLIST_MASK_TEXT;
789 info.m_itemId = item;
790 info.m_text = str;
791
792 SetItem(info);
793 }
794
795 // Gets the item data
796 long wxListCtrl::GetItemData(long item) const
797 {
798 wxListItem info;
799
800 info.m_mask = wxLIST_MASK_DATA;
801 info.m_itemId = item;
802
803 if (!GetItem(info))
804 return 0;
805 return info.m_data;
806 }
807
808 // Sets the item data
809 bool wxListCtrl::SetItemData(long item, long data)
810 {
811 wxListItem info;
812
813 info.m_mask = wxLIST_MASK_DATA;
814 info.m_itemId = item;
815 info.m_data = data;
816
817 return SetItem(info);
818 }
819
820 // Gets the item rectangle
821 bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
822 {
823 RECT rect2;
824
825 int code2 = LVIR_BOUNDS;
826 if ( code == wxLIST_RECT_BOUNDS )
827 code2 = LVIR_BOUNDS;
828 else if ( code == wxLIST_RECT_ICON )
829 code2 = LVIR_ICON;
830 else if ( code == wxLIST_RECT_LABEL )
831 code2 = LVIR_LABEL;
832
833 #ifdef __WXWINE__
834 bool success = (ListView_GetItemRect(GetHwnd(), (int) item, &rect2 ) != 0);
835 #else
836 bool success = (ListView_GetItemRect(GetHwnd(), (int) item, &rect2, code2) != 0);
837 #endif
838
839 rect.x = rect2.left;
840 rect.y = rect2.top;
841 rect.width = rect2.right - rect2.left;
842 rect.height = rect2.bottom - rect2.top;
843 return success;
844 }
845
846 // Gets the item position
847 bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
848 {
849 POINT pt;
850
851 bool success = (ListView_GetItemPosition(GetHwnd(), (int) item, &pt) != 0);
852
853 pos.x = pt.x; pos.y = pt.y;
854 return success;
855 }
856
857 // Sets the item position.
858 bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
859 {
860 return (ListView_SetItemPosition(GetHwnd(), (int) item, pos.x, pos.y) != 0);
861 }
862
863 // Gets the number of items in the list control
864 int wxListCtrl::GetItemCount() const
865 {
866 return ListView_GetItemCount(GetHwnd());
867 }
868
869 // Retrieves the spacing between icons in pixels.
870 // If small is TRUE, gets the spacing for the small icon
871 // view, otherwise the large icon view.
872 int wxListCtrl::GetItemSpacing(bool isSmall) const
873 {
874 return ListView_GetItemSpacing(GetHwnd(), (BOOL) isSmall);
875 }
876
877 // Gets the number of selected items in the list control
878 int wxListCtrl::GetSelectedItemCount() const
879 {
880 return ListView_GetSelectedCount(GetHwnd());
881 }
882
883 // Gets the text colour of the listview
884 wxColour wxListCtrl::GetTextColour() const
885 {
886 COLORREF ref = ListView_GetTextColor(GetHwnd());
887 wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
888 return col;
889 }
890
891 // Sets the text colour of the listview
892 void wxListCtrl::SetTextColour(const wxColour& col)
893 {
894 ListView_SetTextColor(GetHwnd(), PALETTERGB(col.Red(), col.Green(), col.Blue()));
895 }
896
897 // Gets the index of the topmost visible item when in
898 // list or report view
899 long wxListCtrl::GetTopItem() const
900 {
901 return (long) ListView_GetTopIndex(GetHwnd());
902 }
903
904 // Searches for an item, starting from 'item'.
905 // 'geometry' is one of
906 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
907 // 'state' is a state bit flag, one or more of
908 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
909 // item can be -1 to find the first item that matches the
910 // specified flags.
911 // Returns the item or -1 if unsuccessful.
912 long wxListCtrl::GetNextItem(long item, int geom, int state) const
913 {
914 long flags = 0;
915
916 if ( geom == wxLIST_NEXT_ABOVE )
917 flags |= LVNI_ABOVE;
918 if ( geom == wxLIST_NEXT_ALL )
919 flags |= LVNI_ALL;
920 if ( geom == wxLIST_NEXT_BELOW )
921 flags |= LVNI_BELOW;
922 if ( geom == wxLIST_NEXT_LEFT )
923 flags |= LVNI_TOLEFT;
924 if ( geom == wxLIST_NEXT_RIGHT )
925 flags |= LVNI_TORIGHT;
926
927 if ( state & wxLIST_STATE_CUT )
928 flags |= LVNI_CUT;
929 if ( state & wxLIST_STATE_DROPHILITED )
930 flags |= LVNI_DROPHILITED;
931 if ( state & wxLIST_STATE_FOCUSED )
932 flags |= LVNI_FOCUSED;
933 if ( state & wxLIST_STATE_SELECTED )
934 flags |= LVNI_SELECTED;
935
936 return (long) ListView_GetNextItem(GetHwnd(), item, flags);
937 }
938
939
940 wxImageList *wxListCtrl::GetImageList(int which) const
941 {
942 if ( which == wxIMAGE_LIST_NORMAL )
943 {
944 return m_imageListNormal;
945 }
946 else if ( which == wxIMAGE_LIST_SMALL )
947 {
948 return m_imageListSmall;
949 }
950 else if ( which == wxIMAGE_LIST_STATE )
951 {
952 return m_imageListState;
953 }
954 return NULL;
955 }
956
957 void wxListCtrl::SetImageList(wxImageList *imageList, int which)
958 {
959 int flags = 0;
960 if ( which == wxIMAGE_LIST_NORMAL )
961 {
962 flags = LVSIL_NORMAL;
963 if (m_ownsImageListNormal) delete m_imageListNormal;
964 m_imageListNormal = imageList;
965 m_ownsImageListNormal = FALSE;
966 }
967 else if ( which == wxIMAGE_LIST_SMALL )
968 {
969 flags = LVSIL_SMALL;
970 if (m_ownsImageListSmall) delete m_imageListSmall;
971 m_imageListSmall = imageList;
972 m_ownsImageListSmall = FALSE;
973 }
974 else if ( which == wxIMAGE_LIST_STATE )
975 {
976 flags = LVSIL_STATE;
977 if (m_ownsImageListState) delete m_imageListState;
978 m_imageListState = imageList;
979 m_ownsImageListState = FALSE;
980 }
981 ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags);
982 }
983
984 void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
985 {
986 SetImageList(imageList, which);
987 if ( which == wxIMAGE_LIST_NORMAL )
988 m_ownsImageListNormal = TRUE;
989 else if ( which == wxIMAGE_LIST_SMALL )
990 m_ownsImageListSmall = TRUE;
991 else if ( which == wxIMAGE_LIST_STATE )
992 m_ownsImageListState = TRUE;
993 }
994
995 // ----------------------------------------------------------------------------
996 // Operations
997 // ----------------------------------------------------------------------------
998
999 // Arranges the items
1000 bool wxListCtrl::Arrange(int flag)
1001 {
1002 UINT code = 0;
1003 if ( flag == wxLIST_ALIGN_LEFT )
1004 code = LVA_ALIGNLEFT;
1005 else if ( flag == wxLIST_ALIGN_TOP )
1006 code = LVA_ALIGNTOP;
1007 else if ( flag == wxLIST_ALIGN_DEFAULT )
1008 code = LVA_DEFAULT;
1009 else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID )
1010 code = LVA_SNAPTOGRID;
1011
1012 return (ListView_Arrange(GetHwnd(), code) != 0);
1013 }
1014
1015 // Deletes an item
1016 bool wxListCtrl::DeleteItem(long item)
1017 {
1018 return (ListView_DeleteItem(GetHwnd(), (int) item) != 0);
1019 }
1020
1021 // Deletes all items
1022 bool wxListCtrl::DeleteAllItems()
1023 {
1024 return (ListView_DeleteAllItems(GetHwnd()) != 0);
1025 }
1026
1027 // Deletes all items
1028 bool wxListCtrl::DeleteAllColumns()
1029 {
1030 while ( m_colCount > 0 )
1031 {
1032 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1033 {
1034 wxLogLastError(wxT("ListView_DeleteColumn"));
1035
1036 return FALSE;
1037 }
1038
1039 m_colCount--;
1040 }
1041
1042 wxASSERT_MSG( m_colCount == 0, wxT("no columns should be left") );
1043
1044 return TRUE;
1045 }
1046
1047 // Deletes a column
1048 bool wxListCtrl::DeleteColumn(int col)
1049 {
1050 bool success = (ListView_DeleteColumn(GetHwnd(), col) != 0);
1051
1052 if ( success && (m_colCount > 0) )
1053 m_colCount --;
1054 return success;
1055 }
1056
1057 // Clears items, and columns if there are any.
1058 void wxListCtrl::ClearAll()
1059 {
1060 DeleteAllItems();
1061 if ( m_colCount > 0 )
1062 DeleteAllColumns();
1063 }
1064
1065 wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1066 {
1067 wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) );
1068
1069 // VS: ListView_EditLabel requires that the list has focus.
1070 SetFocus();
1071 HWND hWnd = (HWND) ListView_EditLabel(GetHwnd(), item);
1072
1073 if (m_textCtrl)
1074 {
1075 m_textCtrl->SetHWND(0);
1076 m_textCtrl->UnsubclassWin();
1077 delete m_textCtrl;
1078 m_textCtrl = NULL;
1079 }
1080
1081 m_textCtrl = (wxTextCtrl*) textControlClass->CreateObject();
1082 m_textCtrl->SetHWND((WXHWND) hWnd);
1083 m_textCtrl->SubclassWin((WXHWND) hWnd);
1084
1085 return m_textCtrl;
1086 }
1087
1088 // End label editing, optionally cancelling the edit
1089 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel))
1090 {
1091 wxFAIL_MSG( _T("not implemented") );
1092
1093 return FALSE;
1094 }
1095
1096 // Ensures this item is visible
1097 bool wxListCtrl::EnsureVisible(long item)
1098 {
1099 return ListView_EnsureVisible(GetHwnd(), (int) item, FALSE) != 0;
1100 }
1101
1102 // Find an item whose label matches this string, starting from the item after 'start'
1103 // or the beginning if 'start' is -1.
1104 long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
1105 {
1106 LV_FINDINFO findInfo;
1107
1108 findInfo.flags = LVFI_STRING;
1109 if ( partial )
1110 findInfo.flags |= LVFI_PARTIAL;
1111 findInfo.psz = str;
1112
1113 // ListView_FindItem() excludes the first item from search and to look
1114 // through all the items you need to start from -1 which is unnatural and
1115 // inconsistent with the generic version - so we adjust the index
1116 if (start != -1)
1117 start --;
1118 return ListView_FindItem(GetHwnd(), (int) start, &findInfo);
1119 }
1120
1121 // Find an item whose data matches this data, starting from the item after 'start'
1122 // or the beginning if 'start' is -1.
1123 long wxListCtrl::FindItem(long start, long data)
1124 {
1125 LV_FINDINFO findInfo;
1126
1127 findInfo.flags = LVFI_PARAM;
1128 findInfo.lParam = data;
1129
1130 return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
1131 }
1132
1133 // Find an item nearest this position in the specified direction, starting from
1134 // the item after 'start' or the beginning if 'start' is -1.
1135 long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
1136 {
1137 LV_FINDINFO findInfo;
1138
1139 findInfo.flags = LVFI_NEARESTXY;
1140 findInfo.pt.x = pt.x;
1141 findInfo.pt.y = pt.y;
1142 findInfo.vkDirection = VK_RIGHT;
1143
1144 if ( direction == wxLIST_FIND_UP )
1145 findInfo.vkDirection = VK_UP;
1146 else if ( direction == wxLIST_FIND_DOWN )
1147 findInfo.vkDirection = VK_DOWN;
1148 else if ( direction == wxLIST_FIND_LEFT )
1149 findInfo.vkDirection = VK_LEFT;
1150 else if ( direction == wxLIST_FIND_RIGHT )
1151 findInfo.vkDirection = VK_RIGHT;
1152
1153 return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
1154 }
1155
1156 // Determines which item (if any) is at the specified point,
1157 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1158 long wxListCtrl::HitTest(const wxPoint& point, int& flags)
1159 {
1160 LV_HITTESTINFO hitTestInfo;
1161 hitTestInfo.pt.x = (int) point.x;
1162 hitTestInfo.pt.y = (int) point.y;
1163
1164 ListView_HitTest(GetHwnd(), & hitTestInfo);
1165
1166 flags = 0;
1167 if ( hitTestInfo.flags & LVHT_ABOVE )
1168 flags |= wxLIST_HITTEST_ABOVE;
1169 if ( hitTestInfo.flags & LVHT_BELOW )
1170 flags |= wxLIST_HITTEST_BELOW;
1171 if ( hitTestInfo.flags & LVHT_NOWHERE )
1172 flags |= wxLIST_HITTEST_NOWHERE;
1173 if ( hitTestInfo.flags & LVHT_ONITEMICON )
1174 flags |= wxLIST_HITTEST_ONITEMICON;
1175 if ( hitTestInfo.flags & LVHT_ONITEMLABEL )
1176 flags |= wxLIST_HITTEST_ONITEMLABEL;
1177 if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON )
1178 flags |= wxLIST_HITTEST_ONITEMSTATEICON;
1179 if ( hitTestInfo.flags & LVHT_TOLEFT )
1180 flags |= wxLIST_HITTEST_TOLEFT;
1181 if ( hitTestInfo.flags & LVHT_TORIGHT )
1182 flags |= wxLIST_HITTEST_TORIGHT;
1183
1184 return (long) hitTestInfo.iItem;
1185 }
1186
1187 // Inserts an item, returning the index of the new item if successful,
1188 // -1 otherwise.
1189 long wxListCtrl::InsertItem(wxListItem& info)
1190 {
1191 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1192
1193 LV_ITEM item;
1194 wxConvertToMSWListItem(this, info, item);
1195
1196 // check whether it has any custom attributes
1197 if ( info.HasAttributes() )
1198 {
1199
1200 wxListItemAttr *attr;
1201 attr = (wxListItemAttr*) m_attrs.Get(item.iItem);
1202
1203 if (attr == NULL)
1204
1205 m_attrs.Put(item.iItem, (wxObject *)new wxListItemAttr(*info.GetAttributes()));
1206
1207 else *attr = *info.GetAttributes();
1208
1209 m_hasAnyAttr = TRUE;
1210 }
1211
1212 return (long) ListView_InsertItem(GetHwnd(), & item);
1213 }
1214
1215 long wxListCtrl::InsertItem(long index, const wxString& label)
1216 {
1217 wxListItem info;
1218 info.m_text = label;
1219 info.m_mask = wxLIST_MASK_TEXT;
1220 info.m_itemId = index;
1221 return InsertItem(info);
1222 }
1223
1224 // Inserts an image item
1225 long wxListCtrl::InsertItem(long index, int imageIndex)
1226 {
1227 wxListItem info;
1228 info.m_image = imageIndex;
1229 info.m_mask = wxLIST_MASK_IMAGE;
1230 info.m_itemId = index;
1231 return InsertItem(info);
1232 }
1233
1234 // Inserts an image/string item
1235 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1236 {
1237 wxListItem info;
1238 info.m_image = imageIndex;
1239 info.m_text = label;
1240 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1241 info.m_itemId = index;
1242 return InsertItem(info);
1243 }
1244
1245 // For list view mode (only), inserts a column.
1246 long wxListCtrl::InsertColumn(long col, wxListItem& item)
1247 {
1248 LV_COLUMN lvCol;
1249 lvCol.mask = 0;
1250 lvCol.fmt = 0;
1251 lvCol.pszText = NULL;
1252
1253 if ( item.m_mask & wxLIST_MASK_TEXT )
1254 {
1255 lvCol.mask |= LVCF_TEXT;
1256 lvCol.pszText = WXSTRINGCAST item.m_text;
1257 lvCol.cchTextMax = 0; // Ignored
1258 }
1259 if ( item.m_mask & wxLIST_MASK_FORMAT )
1260 {
1261 lvCol.mask |= LVCF_FMT;
1262
1263 if ( item.m_format == wxLIST_FORMAT_LEFT )
1264 lvCol.fmt = LVCFMT_LEFT;
1265 if ( item.m_format == wxLIST_FORMAT_RIGHT )
1266 lvCol.fmt = LVCFMT_RIGHT;
1267 if ( item.m_format == wxLIST_FORMAT_CENTRE )
1268 lvCol.fmt = LVCFMT_CENTER;
1269 }
1270
1271 lvCol.mask |= LVCF_WIDTH;
1272 if ( item.m_mask & wxLIST_MASK_WIDTH )
1273 {
1274 if ( item.m_width == wxLIST_AUTOSIZE)
1275 lvCol.cx = LVSCW_AUTOSIZE;
1276 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
1277 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
1278 else
1279 lvCol.cx = item.m_width;
1280 }
1281 else
1282 {
1283 // always give some width to the new column: this one is compatible
1284 // with wxGTK
1285 lvCol.cx = 80;
1286 }
1287
1288 lvCol.mask |= LVCF_SUBITEM;
1289 lvCol.iSubItem = col;
1290
1291 bool success = ListView_InsertColumn(GetHwnd(), col, & lvCol) != -1;
1292 if ( success )
1293 {
1294 m_colCount++;
1295 }
1296 else
1297 {
1298 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1299 lvCol.pszText);
1300 }
1301
1302 return success;
1303 }
1304
1305 long wxListCtrl::InsertColumn(long col,
1306 const wxString& heading,
1307 int format,
1308 int width)
1309 {
1310 wxListItem item;
1311 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1312 item.m_text = heading;
1313 if ( width > -1 )
1314 {
1315 item.m_mask |= wxLIST_MASK_WIDTH;
1316 item.m_width = width;
1317 }
1318 item.m_format = format;
1319
1320 return InsertColumn(col, item);
1321 }
1322
1323 // Scrolls the list control. If in icon, small icon or report view mode,
1324 // x specifies the number of pixels to scroll. If in list view mode, x
1325 // specifies the number of columns to scroll.
1326 // If in icon, small icon or list view mode, y specifies the number of pixels
1327 // to scroll. If in report view mode, y specifies the number of lines to scroll.
1328 bool wxListCtrl::ScrollList(int dx, int dy)
1329 {
1330 return (ListView_Scroll(GetHwnd(), dx, dy) != 0);
1331 }
1332
1333 // Sort items.
1334
1335 // fn is a function which takes 3 long arguments: item1, item2, data.
1336 // item1 is the long data associated with a first item (NOT the index).
1337 // item2 is the long data associated with a second item (NOT the index).
1338 // data is the same value as passed to SortItems.
1339 // The return value is a negative number if the first item should precede the second
1340 // item, a positive number of the second item should precede the first,
1341 // or zero if the two items are equivalent.
1342
1343 // data is arbitrary data to be passed to the sort function.
1344 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1345 {
1346 return (ListView_SortItems(GetHwnd(), (PFNLVCOMPARE) fn, data) != 0);
1347 }
1348
1349 // ----------------------------------------------------------------------------
1350 // message processing
1351 // ----------------------------------------------------------------------------
1352
1353 bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1354 {
1355 if (cmd == EN_UPDATE)
1356 {
1357 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1358 event.SetEventObject( this );
1359 ProcessCommand(event);
1360 return TRUE;
1361 }
1362 else if (cmd == EN_KILLFOCUS)
1363 {
1364 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1365 event.SetEventObject( this );
1366 ProcessCommand(event);
1367 return TRUE;
1368 }
1369 else
1370 return FALSE;
1371 }
1372
1373 bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1374 {
1375 // prepare the event
1376 // -----------------
1377
1378 wxListEvent event(wxEVT_NULL, m_windowId);
1379 wxEventType eventType = wxEVT_NULL;
1380
1381 NMHDR *nmhdr = (NMHDR *)lParam;
1382
1383 // almost all messages use NM_LISTVIEW
1384 NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr;
1385
1386 // this is true for almost all events
1387 event.m_item.m_data = nmLV->lParam;
1388
1389 switch ( nmhdr->code )
1390 {
1391 case LVN_BEGINRDRAG:
1392 eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
1393 // fall through
1394
1395 case LVN_BEGINDRAG:
1396 if ( eventType == wxEVT_NULL )
1397 {
1398 eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG;
1399 }
1400
1401 event.m_itemIndex = nmLV->iItem;
1402 event.m_pointDrag.x = nmLV->ptAction.x;
1403 event.m_pointDrag.y = nmLV->ptAction.y;
1404 break;
1405
1406 case LVN_BEGINLABELEDIT:
1407 {
1408 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
1409 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1410 wxConvertFromMSWListItem(this, event.m_item, info->item, GetHwnd());
1411 event.m_itemIndex = event.m_item.m_itemId;
1412 }
1413 break;
1414
1415 case LVN_COLUMNCLICK:
1416 eventType = wxEVT_COMMAND_LIST_COL_CLICK;
1417 event.m_itemIndex = -1;
1418 event.m_col = nmLV->iSubItem;
1419 break;
1420
1421 case LVN_DELETEALLITEMS:
1422 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
1423 event.m_itemIndex = -1;
1424
1425 FreeAllAttrs();
1426
1427 break;
1428
1429 case LVN_DELETEITEM:
1430 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
1431 event.m_itemIndex = nmLV->iItem;
1432
1433 if ( m_hasAnyAttr )
1434 {
1435 delete (wxListItemAttr *)m_attrs.Delete(nmLV->iItem);
1436 }
1437 break;
1438
1439 case LVN_ENDLABELEDIT:
1440 {
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 )
1445 return FALSE;
1446
1447 event.m_itemIndex = event.m_item.m_itemId;
1448 }
1449 break;
1450
1451 case LVN_SETDISPINFO:
1452 {
1453 eventType = wxEVT_COMMAND_LIST_SET_INFO;
1454 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1455 wxConvertFromMSWListItem(this, event.m_item, info->item, GetHwnd());
1456 }
1457 break;
1458
1459 case LVN_INSERTITEM:
1460 eventType = wxEVT_COMMAND_LIST_INSERT_ITEM;
1461 event.m_itemIndex = nmLV->iItem;
1462 break;
1463
1464 case LVN_ITEMCHANGED:
1465 // This needs to be sent to wxListCtrl as a rather more concrete
1466 // event. For now, just detect a selection or deselection.
1467 if ( (nmLV->uNewState & LVIS_SELECTED) && !(nmLV->uOldState & LVIS_SELECTED) )
1468 {
1469 eventType = wxEVT_COMMAND_LIST_ITEM_SELECTED;
1470 event.m_itemIndex = nmLV->iItem;
1471 }
1472 else if ( !(nmLV->uNewState & LVIS_SELECTED) && (nmLV->uOldState & LVIS_SELECTED) )
1473 {
1474 eventType = wxEVT_COMMAND_LIST_ITEM_DESELECTED;
1475 event.m_itemIndex = nmLV->iItem;
1476 }
1477 else
1478 {
1479 return FALSE;
1480 }
1481 break;
1482
1483 case LVN_KEYDOWN:
1484 {
1485 LV_KEYDOWN *info = (LV_KEYDOWN *)lParam;
1486 WORD wVKey = info->wVKey;
1487
1488 // get the current selection
1489 long lItem = GetNextItem(-1,
1490 wxLIST_NEXT_ALL,
1491 wxLIST_STATE_SELECTED);
1492
1493 // <Enter> or <Space> activate the selected item if any (but
1494 // not with Shift and/or Ctrl as then they have a predefined
1495 // meaning for the list view)
1496 if ( lItem != -1 &&
1497 (wVKey == VK_RETURN || wVKey == VK_SPACE) &&
1498 !(wxIsShiftDown() || wxIsCtrlDown()) )
1499 {
1500 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
1501 event.m_itemIndex = lItem;
1502 }
1503 else
1504 {
1505 eventType = wxEVT_COMMAND_LIST_KEY_DOWN;
1506 event.m_code = wxCharCodeMSWToWX(wVKey);
1507 }
1508
1509 if ( lItem != -1 )
1510 {
1511 // fill the other fields too
1512 event.m_item.m_text = GetItemText(lItem);
1513 event.m_item.m_data = GetItemData(lItem);
1514 }
1515 }
1516 break;
1517
1518 case NM_DBLCLK:
1519 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
1520 // anything else
1521 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1522 {
1523 return TRUE;
1524 }
1525
1526 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
1527 // if it happened on an item (and not on empty place)
1528 if ( nmLV->iItem == -1 )
1529 {
1530 // not on item
1531 return FALSE;
1532 }
1533
1534 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
1535 event.m_itemIndex = nmLV->iItem;
1536 event.m_item.m_text = GetItemText(nmLV->iItem);
1537 event.m_item.m_data = GetItemData(nmLV->iItem);
1538 break;
1539
1540 case NM_RCLICK:
1541 /* TECH NOTE: NM_RCLICK isn't really good enough here. We want to
1542 subclass and check for the actual WM_RBUTTONDOWN message,
1543 because NM_RCLICK waits for the WM_RBUTTONUP message as well
1544 before firing off. We want to have notify events for both down
1545 -and- up. */
1546 {
1547 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
1548 // don't do anything else
1549 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1550 {
1551 return TRUE;
1552 }
1553
1554 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
1555 LV_HITTESTINFO lvhti;
1556 wxZeroMemory(lvhti);
1557
1558 ::GetCursorPos(&(lvhti.pt));
1559 ::ScreenToClient(GetHwnd(),&(lvhti.pt));
1560 if ( ListView_HitTest(GetHwnd(),&lvhti) != -1 )
1561 {
1562 if ( lvhti.flags & LVHT_ONITEM )
1563 {
1564 eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK;
1565 event.m_itemIndex = lvhti.iItem;
1566 event.m_pointDrag.x = lvhti.pt.x;
1567 event.m_pointDrag.y = lvhti.pt.y;
1568 }
1569 }
1570 }
1571 break;
1572
1573 #if 0
1574 case NM_MCLICK: // ***** THERE IS NO NM_MCLICK. Subclass anyone? ******
1575 {
1576 // if the user processes it in wxEVT_COMMAND_MIDDLE_CLICK(), don't do
1577 // anything else
1578 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1579 {
1580 return TRUE;
1581 }
1582
1583 // else translate it into wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK event
1584 eventType = wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK;
1585 NMITEMACTIVATE* hdr = (NMITEMACTIVATE*)lParam;
1586 event.m_itemIndex = hdr->iItem;
1587 }
1588 break;
1589 #endif // 0
1590
1591 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1592 case NM_CUSTOMDRAW:
1593 {
1594 LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
1595 NMCUSTOMDRAW& nmcd = lplvcd->nmcd;
1596 switch( nmcd.dwDrawStage )
1597 {
1598 case CDDS_PREPAINT:
1599 // if we've got any items with non standard attributes,
1600 // notify us before painting each item
1601 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
1602 : CDRF_DODEFAULT;
1603 return TRUE;
1604
1605 case CDDS_ITEMPREPAINT:
1606 {
1607 wxListItemAttr *attr =
1608 (wxListItemAttr *)m_attrs.Get(nmcd.dwItemSpec);
1609
1610 if ( !attr )
1611 {
1612 // nothing to do for this item
1613 return CDRF_DODEFAULT;
1614 }
1615
1616 HFONT hFont;
1617 wxColour colText, colBack;
1618 if ( attr->HasFont() )
1619 {
1620 wxFont font = attr->GetFont();
1621 hFont = (HFONT)font.GetResourceHandle();
1622 }
1623 else
1624 {
1625 hFont = 0;
1626 }
1627
1628 if ( attr->HasTextColour() )
1629 {
1630 colText = attr->GetTextColour();
1631 }
1632 else
1633 {
1634 colText = GetTextColour();
1635 }
1636
1637 if ( attr->HasBackgroundColour() )
1638 {
1639 colBack = attr->GetBackgroundColour();
1640 }
1641 else
1642 {
1643 colBack = GetBackgroundColour();
1644 }
1645
1646 // note that if we wanted to set colours for
1647 // individual columns (subitems), we would have
1648 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1649 if ( hFont )
1650 {
1651 ::SelectObject(nmcd.hdc, hFont);
1652
1653 *result = CDRF_NEWFONT;
1654 }
1655 else
1656 {
1657 *result = CDRF_DODEFAULT;
1658 }
1659
1660 lplvcd->clrText = wxColourToRGB(colText);
1661 lplvcd->clrTextBk = wxColourToRGB(colBack);
1662
1663 return TRUE;
1664 }
1665
1666 default:
1667 *result = CDRF_DODEFAULT;
1668 return TRUE;
1669 }
1670 }
1671 // break; // can never be reached
1672 #endif // _WIN32_IE >= 0x300
1673
1674 case LVN_GETDISPINFO:
1675 if ( IsVirtual() )
1676 {
1677 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1678
1679 LV_ITEM& lvi = info->item;
1680 long item = lvi.iItem;
1681
1682 if ( lvi.mask & LVIF_TEXT )
1683 {
1684 wxString text = OnGetItemText(item, lvi.iSubItem);
1685 wxStrncpy(lvi.pszText, text, lvi.cchTextMax);
1686 }
1687
1688 if ( lvi.mask & LVIF_IMAGE )
1689 {
1690 lvi.iImage = OnGetItemImage(item);
1691 }
1692
1693 // a little dose of healthy paranoia: as we never use
1694 // LVM_SETCALLBACKMASK we're not supposed to get these ones
1695 wxASSERT_MSG( !(lvi.mask & LVIF_STATE),
1696 _T("we don't support state callbacks yet!") );
1697
1698 return TRUE;
1699 }
1700 // fall through
1701
1702 default:
1703 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1704 }
1705
1706 // process the event
1707 // -----------------
1708
1709 event.SetEventObject( this );
1710 event.SetEventType(eventType);
1711
1712 if ( !GetEventHandler()->ProcessEvent(event) )
1713 return FALSE;
1714
1715 // post processing
1716 // ---------------
1717
1718 switch ( nmhdr->code )
1719 {
1720 case LVN_DELETEALLITEMS:
1721 // always return TRUE to suppress all additional LVN_DELETEITEM
1722 // notifications - this makes deleting all items from a list ctrl
1723 // much faster
1724 *result = TRUE;
1725
1726 return TRUE;
1727
1728 case LVN_ENDLABELEDIT:
1729 {
1730 *result = event.IsAllowed();
1731 return TRUE;
1732 }
1733 }
1734
1735 *result = !event.IsAllowed();
1736
1737 return TRUE;
1738 }
1739
1740 // Necessary for drawing hrules and vrules, if specified
1741 void wxListCtrl::OnPaint(wxPaintEvent& event)
1742 {
1743 wxPaintDC dc(this);
1744
1745 wxControl::OnPaint(event);
1746
1747 // Reset the device origin since it may have been set
1748 dc.SetDeviceOrigin(0, 0);
1749
1750 bool drawHRules = ((GetWindowStyle() & wxLC_HRULES) != 0);
1751 bool drawVRules = ((GetWindowStyle() & wxLC_VRULES) != 0);
1752
1753 if (!drawHRules && !drawVRules)
1754 return;
1755 if ((GetWindowStyle() & wxLC_REPORT) == 0)
1756 return;
1757
1758 wxPen pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
1759 dc.SetPen(pen);
1760 dc.SetBrush(* wxTRANSPARENT_BRUSH);
1761
1762 wxSize clientSize = GetClientSize();
1763 wxRect itemRect;
1764 int cy=0;
1765
1766 int itemCount = GetItemCount();
1767 int i;
1768 for (i = 0; i < itemCount; i++)
1769 {
1770 if (GetItemRect(i, itemRect))
1771 {
1772 cy = itemRect.GetTop();
1773 if (i != 0) // Don't draw the first one
1774 {
1775 dc.DrawLine(0, cy, clientSize.x, cy);
1776 }
1777 // Draw last line
1778 if (i == (GetItemCount() - 1))
1779 {
1780 cy = itemRect.GetBottom();
1781 dc.DrawLine(0, cy, clientSize.x, cy);
1782 }
1783 }
1784 }
1785 i = (GetItemCount() - 1);
1786 if (drawVRules && (i > -1))
1787 {
1788 wxRect firstItemRect;
1789 GetItemRect(0, firstItemRect);
1790
1791 if (GetItemRect(i, itemRect))
1792 {
1793 int col;
1794 int x = itemRect.GetX();
1795 for (col = 0; col < GetColumnCount(); col++)
1796 {
1797 int colWidth = GetColumnWidth(col);
1798 x += colWidth ;
1799 dc.DrawLine(x, firstItemRect.GetY() - 2, x, itemRect.GetBottom());
1800 }
1801 }
1802 }
1803 }
1804
1805 // ----------------------------------------------------------------------------
1806 // virtual list controls
1807 // ----------------------------------------------------------------------------
1808
1809 wxString wxListCtrl::OnGetItemText(long item, long col) const
1810 {
1811 // this is a pure virtual function, in fact - which is not really pure
1812 // because the controls which are not virtual don't need to implement it
1813 wxFAIL_MSG( _T("not supposed to be called") );
1814
1815 return wxEmptyString;
1816 }
1817
1818 int wxListCtrl::OnGetItemImage(long item) const
1819 {
1820 // same as above
1821 wxFAIL_MSG( _T("not supposed to be called") );
1822
1823 return -1;
1824 }
1825
1826 void wxListCtrl::SetItemCount(long count)
1827 {
1828 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
1829
1830 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count, 0) )
1831 {
1832 wxLogLastError(_T("ListView_SetItemCount"));
1833 }
1834 }
1835
1836 // ----------------------------------------------------------------------------
1837 // wxListItem
1838 // ----------------------------------------------------------------------------
1839
1840 // List item structure
1841 wxListItem::wxListItem()
1842 {
1843 m_mask = 0;
1844 m_itemId = 0;
1845 m_col = 0;
1846 m_state = 0;
1847 m_stateMask = 0;
1848 m_image = 0;
1849 m_data = 0;
1850
1851 m_format = wxLIST_FORMAT_CENTRE;
1852 m_width = 0;
1853
1854 m_attr = NULL;
1855 }
1856
1857 void wxListItem::Clear()
1858 {
1859 m_mask = 0;
1860 m_itemId = 0;
1861 m_col = 0;
1862 m_state = 0;
1863 m_stateMask = 0;
1864 m_image = 0;
1865 m_data = 0;
1866 m_format = wxLIST_FORMAT_CENTRE;
1867 m_width = 0;
1868 m_text = wxEmptyString;
1869
1870 if (m_attr) delete m_attr;
1871 m_attr = NULL;
1872 }
1873
1874 void wxListItem::ClearAttributes()
1875 {
1876 if (m_attr) delete m_attr;
1877 m_attr = NULL;
1878 }
1879
1880 static void wxConvertFromMSWListItem(const wxListCtrl *WXUNUSED(ctrl), wxListItem& info, LV_ITEM& lvItem, HWND getFullInfo)
1881 {
1882 info.m_data = lvItem.lParam;
1883 info.m_mask = 0;
1884 info.m_state = 0;
1885 info.m_stateMask = 0;
1886 info.m_itemId = lvItem.iItem;
1887
1888 long oldMask = lvItem.mask;
1889
1890 bool needText = FALSE;
1891 if (getFullInfo != 0)
1892 {
1893 if ( lvItem.mask & LVIF_TEXT )
1894 needText = FALSE;
1895 else
1896 needText = TRUE;
1897
1898 if ( needText )
1899 {
1900 lvItem.pszText = new wxChar[513];
1901 lvItem.cchTextMax = 512;
1902 }
1903 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM;
1904 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
1905 ::SendMessage(getFullInfo, LVM_GETITEM, 0, (LPARAM)& lvItem);
1906 }
1907
1908 if ( lvItem.mask & LVIF_STATE )
1909 {
1910 info.m_mask |= wxLIST_MASK_STATE;
1911
1912 if ( lvItem.stateMask & LVIS_CUT)
1913 {
1914 info.m_stateMask |= wxLIST_STATE_CUT;
1915 if ( lvItem.state & LVIS_CUT )
1916 info.m_state |= wxLIST_STATE_CUT;
1917 }
1918 if ( lvItem.stateMask & LVIS_DROPHILITED)
1919 {
1920 info.m_stateMask |= wxLIST_STATE_DROPHILITED;
1921 if ( lvItem.state & LVIS_DROPHILITED )
1922 info.m_state |= wxLIST_STATE_DROPHILITED;
1923 }
1924 if ( lvItem.stateMask & LVIS_FOCUSED)
1925 {
1926 info.m_stateMask |= wxLIST_STATE_FOCUSED;
1927 if ( lvItem.state & LVIS_FOCUSED )
1928 info.m_state |= wxLIST_STATE_FOCUSED;
1929 }
1930 if ( lvItem.stateMask & LVIS_SELECTED)
1931 {
1932 info.m_stateMask |= wxLIST_STATE_SELECTED;
1933 if ( lvItem.state & LVIS_SELECTED )
1934 info.m_state |= wxLIST_STATE_SELECTED;
1935 }
1936 }
1937
1938 if ( lvItem.mask & LVIF_TEXT )
1939 {
1940 info.m_mask |= wxLIST_MASK_TEXT;
1941 info.m_text = lvItem.pszText;
1942 }
1943 if ( lvItem.mask & LVIF_IMAGE )
1944 {
1945 info.m_mask |= wxLIST_MASK_IMAGE;
1946 info.m_image = lvItem.iImage;
1947 }
1948 if ( lvItem.mask & LVIF_PARAM )
1949 info.m_mask |= wxLIST_MASK_DATA;
1950 if ( lvItem.mask & LVIF_DI_SETITEM )
1951 info.m_mask |= wxLIST_SET_ITEM;
1952 info.m_col = lvItem.iSubItem;
1953
1954 if (needText)
1955 {
1956 if (lvItem.pszText)
1957 delete[] lvItem.pszText;
1958 }
1959 lvItem.mask = oldMask;
1960 }
1961
1962 static void wxConvertToMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& lvItem)
1963 {
1964 lvItem.iItem = (int) info.m_itemId;
1965
1966 lvItem.iImage = info.m_image;
1967 lvItem.lParam = info.m_data;
1968 lvItem.stateMask = 0;
1969 lvItem.state = 0;
1970 lvItem.mask = 0;
1971 lvItem.iSubItem = info.m_col;
1972
1973 if (info.m_mask & wxLIST_MASK_STATE)
1974 {
1975 lvItem.mask |= LVIF_STATE;
1976 if (info.m_stateMask & wxLIST_STATE_CUT)
1977 {
1978 lvItem.stateMask |= LVIS_CUT;
1979 if (info.m_state & wxLIST_STATE_CUT)
1980 lvItem.state |= LVIS_CUT;
1981 }
1982 if (info.m_stateMask & wxLIST_STATE_DROPHILITED)
1983 {
1984 lvItem.stateMask |= LVIS_DROPHILITED;
1985 if (info.m_state & wxLIST_STATE_DROPHILITED)
1986 lvItem.state |= LVIS_DROPHILITED;
1987 }
1988 if (info.m_stateMask & wxLIST_STATE_FOCUSED)
1989 {
1990 lvItem.stateMask |= LVIS_FOCUSED;
1991 if (info.m_state & wxLIST_STATE_FOCUSED)
1992 lvItem.state |= LVIS_FOCUSED;
1993 }
1994 if (info.m_stateMask & wxLIST_STATE_SELECTED)
1995 {
1996 lvItem.stateMask |= LVIS_SELECTED;
1997 if (info.m_state & wxLIST_STATE_SELECTED)
1998 lvItem.state |= LVIS_SELECTED;
1999 }
2000 }
2001
2002 if (info.m_mask & wxLIST_MASK_TEXT)
2003 {
2004 lvItem.mask |= LVIF_TEXT;
2005 if ( ctrl->GetWindowStyleFlag() & wxLC_USER_TEXT )
2006 {
2007 lvItem.pszText = LPSTR_TEXTCALLBACK;
2008 }
2009 else
2010 {
2011 // pszText is not const, hence the cast
2012 lvItem.pszText = (wxChar *)info.m_text.c_str();
2013 if ( lvItem.pszText )
2014 lvItem.cchTextMax = info.m_text.Length();
2015 else
2016 lvItem.cchTextMax = 0;
2017 }
2018 }
2019 if (info.m_mask & wxLIST_MASK_IMAGE)
2020 lvItem.mask |= LVIF_IMAGE;
2021 if (info.m_mask & wxLIST_MASK_DATA)
2022 lvItem.mask |= LVIF_PARAM;
2023 }
2024
2025 // ----------------------------------------------------------------------------
2026 // List event
2027 // ----------------------------------------------------------------------------
2028
2029 IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
2030
2031 wxListEvent::wxListEvent(wxEventType commandType, int id)
2032 : wxNotifyEvent(commandType, id)
2033 {
2034 m_code = 0;
2035 m_itemIndex = 0;
2036 m_oldItemIndex = 0;
2037 m_col = 0;
2038 m_cancelled = FALSE;
2039 }
2040
2041 #endif // wxUSE_LISTCTRL