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