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