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