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