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