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