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