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