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