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