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