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