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