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