]> git.saurik.com Git - wxWidgets.git/blob - src/msw/listctrl.cpp
draw solid focus rectangle in mono theme and don't do it at all for selected items...
[wxWidgets.git] / src / msw / listctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/listctrl.cpp
3 // Purpose: wxListCtrl
4 // Author: Julian Smart
5 // Modified by: Agron Selimaj
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_LISTCTRL
28
29 #ifndef WX_PRECOMP
30 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
31 #include "wx/app.h"
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #include "wx/settings.h"
35 #include "wx/dcclient.h"
36 #include "wx/textctrl.h"
37 #endif
38
39 #include "wx/imaglist.h"
40 #include "wx/listctrl.h"
41
42 #include "wx/msw/private.h"
43
44 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__)
45 #include <ole2.h>
46 #include <shellapi.h>
47 #if _WIN32_WCE < 400
48 #include <aygshell.h>
49 #endif
50 #endif
51
52 // Currently gcc and watcom don't define NMLVFINDITEM, and DMC only defines
53 // it by its old name NM_FINDTIEM.
54 //
55 #if defined(__VISUALC__) || defined(__BORLANDC__) || defined(NMLVFINDITEM)
56 #define HAVE_NMLVFINDITEM 1
57 #elif defined(__DMC__) || defined(NM_FINDITEM)
58 #define HAVE_NMLVFINDITEM 1
59 #define NMLVFINDITEM NM_FINDITEM
60 #endif
61
62 // ----------------------------------------------------------------------------
63 // private functions
64 // ----------------------------------------------------------------------------
65
66 // convert our state and mask flags to LV_ITEM constants
67 static void wxConvertToMSWFlags(long state, long mask, LV_ITEM& lvItem);
68
69 // convert wxListItem to LV_ITEM
70 static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
71 const wxListItem& info, LV_ITEM& lvItem);
72
73 // convert LV_ITEM to wxListItem
74 static void wxConvertFromMSWListItem(HWND hwndListCtrl,
75 wxListItem& info,
76 /* const */ LV_ITEM& lvItem);
77
78 // convert our wxListItem to LV_COLUMN
79 static void wxConvertToMSWListCol(HWND hwndList,
80 int col,
81 const wxListItem& item,
82 LV_COLUMN& lvCol);
83
84 // ----------------------------------------------------------------------------
85 // private helper classes
86 // ----------------------------------------------------------------------------
87
88 // We have to handle both fooW and fooA notifications in several cases
89 // because of broken comctl32.dll and/or unicows.dll. This class is used to
90 // convert LV_ITEMA and LV_ITEMW to LV_ITEM (which is either LV_ITEMA or
91 // LV_ITEMW depending on wxUSE_UNICODE setting), so that it can be processed
92 // by wxConvertToMSWListItem().
93 #if wxUSE_UNICODE
94 #define LV_ITEM_NATIVE LV_ITEMW
95 #define LV_ITEM_OTHER LV_ITEMA
96
97 #define LV_CONV_TO_WX cMB2WX
98 #define LV_CONV_BUF wxMB2WXbuf
99 #else // ANSI
100 #define LV_ITEM_NATIVE LV_ITEMA
101 #define LV_ITEM_OTHER LV_ITEMW
102
103 #define LV_CONV_TO_WX cWC2WX
104 #define LV_CONV_BUF wxWC2WXbuf
105 #endif // Unicode/ANSI
106
107 class wxLV_ITEM
108 {
109 public:
110 // default ctor, use Init() later
111 wxLV_ITEM() { m_buf = NULL; m_pItem = NULL; }
112
113 // init without conversion
114 void Init(LV_ITEM_NATIVE& item)
115 {
116 wxASSERT_MSG( !m_pItem, _T("Init() called twice?") );
117
118 m_pItem = &item;
119 }
120
121 // init with conversion
122 void Init(const LV_ITEM_OTHER& item)
123 {
124 // avoid unnecessary dynamic memory allocation, jjust make m_pItem
125 // point to our own m_item
126
127 // memcpy() can't work if the struct sizes are different
128 wxCOMPILE_TIME_ASSERT( sizeof(LV_ITEM_OTHER) == sizeof(LV_ITEM_NATIVE),
129 CodeCantWorkIfDiffSizes);
130
131 memcpy(&m_item, &item, sizeof(LV_ITEM_NATIVE));
132
133 // convert text from ANSI to Unicod if necessary
134 if ( (item.mask & LVIF_TEXT) && item.pszText )
135 {
136 m_buf = new LV_CONV_BUF(wxConvLocal.LV_CONV_TO_WX(item.pszText));
137 m_item.pszText = (wxChar *)m_buf->data();
138 }
139 }
140
141 // ctor without conversion
142 wxLV_ITEM(LV_ITEM_NATIVE& item) : m_buf(NULL), m_pItem(&item) { }
143
144 // ctor with conversion
145 wxLV_ITEM(LV_ITEM_OTHER& item) : m_buf(NULL)
146 {
147 Init(item);
148 }
149
150 ~wxLV_ITEM() { delete m_buf; }
151
152 // conversion to the real LV_ITEM
153 operator LV_ITEM_NATIVE&() const { return *m_pItem; }
154
155 private:
156 LV_CONV_BUF *m_buf;
157
158 LV_ITEM_NATIVE *m_pItem;
159 LV_ITEM_NATIVE m_item;
160
161 DECLARE_NO_COPY_CLASS(wxLV_ITEM)
162 };
163
164 ///////////////////////////////////////////////////////
165 // Problem:
166 // The MSW version had problems with SetTextColour() et
167 // al as the wxListItemAttr's were stored keyed on the
168 // item index. If a item was inserted anywhere but the end
169 // of the list the the text attributes (colour etc) for
170 // the following items were out of sync.
171 //
172 // Solution:
173 // Under MSW the only way to associate data with a List
174 // item independent of its position in the list is to
175 // store a pointer to it in its lParam attribute. However
176 // user programs are already using this (via the
177 // SetItemData() GetItemData() calls).
178 //
179 // However what we can do is store a pointer to a
180 // structure which contains the attributes we want *and*
181 // a lParam for the users data, e.g.
182 //
183 // class wxListItemInternalData
184 // {
185 // public:
186 // wxListItemAttr *attr;
187 // long lParam; // user data
188 // };
189 //
190 // To conserve memory, a wxListItemInternalData is
191 // only allocated for a LV_ITEM if text attributes or
192 // user data(lparam) are being set.
193
194
195 // class wxListItemInternalData
196 class wxListItemInternalData
197 {
198 public:
199 wxListItemAttr *attr;
200 LPARAM lParam; // user data
201
202 wxListItemInternalData() : attr(NULL), lParam(0) {}
203 ~wxListItemInternalData()
204 {
205 if (attr)
206 delete attr;
207 };
208
209 DECLARE_NO_COPY_CLASS(wxListItemInternalData)
210 };
211
212 // Get the internal data structure
213 static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId);
214 static wxListItemInternalData *wxGetInternalData(const wxListCtrl *ctl, long itemId);
215 static wxListItemAttr *wxGetInternalDataAttr(const wxListCtrl *ctl, long itemId);
216 static void wxDeleteInternalData(wxListCtrl* ctl, long itemId);
217
218
219 #if wxUSE_EXTENDED_RTTI
220 WX_DEFINE_FLAGS( wxListCtrlStyle )
221
222 wxBEGIN_FLAGS( wxListCtrlStyle )
223 // new style border flags, we put them first to
224 // use them for streaming out
225 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
226 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
227 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
228 wxFLAGS_MEMBER(wxBORDER_RAISED)
229 wxFLAGS_MEMBER(wxBORDER_STATIC)
230 wxFLAGS_MEMBER(wxBORDER_NONE)
231
232 // old style border flags
233 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
234 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
235 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
236 wxFLAGS_MEMBER(wxRAISED_BORDER)
237 wxFLAGS_MEMBER(wxSTATIC_BORDER)
238 wxFLAGS_MEMBER(wxBORDER)
239
240 // standard window styles
241 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
242 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
243 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
244 wxFLAGS_MEMBER(wxWANTS_CHARS)
245 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
246 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
247 wxFLAGS_MEMBER(wxVSCROLL)
248 wxFLAGS_MEMBER(wxHSCROLL)
249
250 wxFLAGS_MEMBER(wxLC_LIST)
251 wxFLAGS_MEMBER(wxLC_REPORT)
252 wxFLAGS_MEMBER(wxLC_ICON)
253 wxFLAGS_MEMBER(wxLC_SMALL_ICON)
254 wxFLAGS_MEMBER(wxLC_ALIGN_TOP)
255 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT)
256 wxFLAGS_MEMBER(wxLC_AUTOARRANGE)
257 wxFLAGS_MEMBER(wxLC_USER_TEXT)
258 wxFLAGS_MEMBER(wxLC_EDIT_LABELS)
259 wxFLAGS_MEMBER(wxLC_NO_HEADER)
260 wxFLAGS_MEMBER(wxLC_SINGLE_SEL)
261 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING)
262 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING)
263 wxFLAGS_MEMBER(wxLC_VIRTUAL)
264
265 wxEND_FLAGS( wxListCtrlStyle )
266
267 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl, wxControl,"wx/listctrl.h")
268
269 wxBEGIN_PROPERTIES_TABLE(wxListCtrl)
270 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
271
272 wxPROPERTY_FLAGS( WindowStyle , wxListCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
273 wxEND_PROPERTIES_TABLE()
274
275 wxBEGIN_HANDLERS_TABLE(wxListCtrl)
276 wxEND_HANDLERS_TABLE()
277
278 wxCONSTRUCTOR_5( wxListCtrl , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
279
280 /*
281 TODO : Expose more information of a list's layout etc. via appropriate objects (à la NotebookPageInfo)
282 */
283 #else
284 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
285 #endif
286
287 IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl)
288 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
289
290 IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
291
292 BEGIN_EVENT_TABLE(wxListCtrl, wxControl)
293 EVT_PAINT(wxListCtrl::OnPaint)
294 END_EVENT_TABLE()
295
296 // ============================================================================
297 // implementation
298 // ============================================================================
299
300 // ----------------------------------------------------------------------------
301 // wxListCtrl construction
302 // ----------------------------------------------------------------------------
303
304 void wxListCtrl::Init()
305 {
306 m_imageListNormal = NULL;
307 m_imageListSmall = NULL;
308 m_imageListState = NULL;
309 m_ownsImageListNormal = m_ownsImageListSmall = m_ownsImageListState = false;
310 m_colCount = 0;
311 m_count = 0;
312 m_ignoreChangeMessages = false;
313 m_textCtrl = NULL;
314 m_AnyInternalData = false;
315 m_hasAnyAttr = false;
316 }
317
318 bool wxListCtrl::Create(wxWindow *parent,
319 wxWindowID id,
320 const wxPoint& pos,
321 const wxSize& size,
322 long style,
323 const wxValidator& validator,
324 const wxString& name)
325 {
326 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
327 return false;
328
329 if ( !MSWCreateControl(WC_LISTVIEW, wxEmptyString, pos, size) )
330 return false;
331
332 // explicitly say that we want to use Unicode because otherwise we get ANSI
333 // versions of _some_ messages (notably LVN_GETDISPINFOA) in MSLU build
334 wxSetCCUnicodeFormat(GetHwnd());
335
336 // We must set the default text colour to the system/theme color, otherwise
337 // GetTextColour will always return black
338 SetTextColour(GetDefaultAttributes().colFg);
339
340 // for comctl32.dll v 4.70+ we want to have some non default extended
341 // styles because it's prettier (and also because wxGTK does it like this)
342 if ( InReportView() && wxApp::GetComCtl32Version() >= 470 )
343 {
344 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE,
345 0, LVS_EX_LABELTIP | LVS_EX_FULLROWSELECT | LVS_EX_SUBITEMIMAGES);
346 }
347
348 return true;
349 }
350
351 WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
352 {
353 WXDWORD wstyle = wxControl::MSWGetStyle(style, exstyle);
354
355 wstyle |= LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS;
356
357 #ifdef __WXDEBUG__
358 size_t nModes = 0;
359
360 #define MAP_MODE_STYLE(wx, ms) \
361 if ( style & (wx) ) { wstyle |= (ms); nModes++; }
362 #else // !__WXDEBUG__
363 #define MAP_MODE_STYLE(wx, ms) \
364 if ( style & (wx) ) wstyle |= (ms);
365 #endif // __WXDEBUG__
366
367 MAP_MODE_STYLE(wxLC_ICON, LVS_ICON)
368 MAP_MODE_STYLE(wxLC_SMALL_ICON, LVS_SMALLICON)
369 MAP_MODE_STYLE(wxLC_LIST, LVS_LIST)
370 MAP_MODE_STYLE(wxLC_REPORT, LVS_REPORT)
371
372 wxASSERT_MSG( nModes == 1,
373 _T("wxListCtrl style should have exactly one mode bit set") );
374
375 #undef MAP_MODE_STYLE
376
377 if ( style & wxLC_ALIGN_LEFT )
378 wstyle |= LVS_ALIGNLEFT;
379
380 if ( style & wxLC_ALIGN_TOP )
381 wstyle |= LVS_ALIGNTOP;
382
383 if ( style & wxLC_AUTOARRANGE )
384 wstyle |= LVS_AUTOARRANGE;
385
386 if ( style & wxLC_NO_SORT_HEADER )
387 wstyle |= LVS_NOSORTHEADER;
388
389 if ( style & wxLC_NO_HEADER )
390 wstyle |= LVS_NOCOLUMNHEADER;
391
392 if ( style & wxLC_EDIT_LABELS )
393 wstyle |= LVS_EDITLABELS;
394
395 if ( style & wxLC_SINGLE_SEL )
396 wstyle |= LVS_SINGLESEL;
397
398 if ( style & wxLC_SORT_ASCENDING )
399 {
400 wstyle |= LVS_SORTASCENDING;
401
402 wxASSERT_MSG( !(style & wxLC_SORT_DESCENDING),
403 _T("can't sort in ascending and descending orders at once") );
404 }
405 else if ( style & wxLC_SORT_DESCENDING )
406 wstyle |= LVS_SORTDESCENDING;
407
408 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
409 if ( style & wxLC_VIRTUAL )
410 {
411 int ver = wxApp::GetComCtl32Version();
412 if ( ver < 470 )
413 {
414 wxLogWarning(_("Please install a newer version of comctl32.dll\n(at least version 4.70 is required but you have %d.%02d)\nor this program won't operate correctly."),
415 ver / 100, ver % 100);
416 }
417
418 wstyle |= LVS_OWNERDATA;
419 }
420 #endif // ancient cygwin
421
422 return wstyle;
423 }
424
425 void wxListCtrl::UpdateStyle()
426 {
427 if ( GetHwnd() )
428 {
429 // The new window view style
430 DWORD dwStyleNew = MSWGetStyle(m_windowStyle, NULL);
431
432 // some styles are not returned by MSWGetStyle()
433 if ( IsShown() )
434 dwStyleNew |= WS_VISIBLE;
435
436 // Get the current window style.
437 DWORD dwStyleOld = ::GetWindowLong(GetHwnd(), GWL_STYLE);
438
439 // we don't have wxVSCROLL style, but the list control may have it,
440 // don't change it then
441 dwStyleNew |= dwStyleOld & (WS_HSCROLL | WS_VSCROLL);
442
443 // Only set the window style if the view bits have changed.
444 if ( dwStyleOld != dwStyleNew )
445 {
446 ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyleNew);
447 }
448 }
449 }
450
451 void wxListCtrl::FreeAllInternalData()
452 {
453 if (m_AnyInternalData)
454 {
455 int n = GetItemCount();
456
457 m_ignoreChangeMessages = true;
458 for (int i = 0; i < n; i++)
459 wxDeleteInternalData(this, i);
460 m_ignoreChangeMessages = false;
461
462 m_AnyInternalData = false;
463 }
464 }
465
466 wxListCtrl::~wxListCtrl()
467 {
468 FreeAllInternalData();
469
470 if ( m_textCtrl )
471 {
472 m_textCtrl->UnsubclassWin();
473 m_textCtrl->SetHWND(0);
474 delete m_textCtrl;
475 m_textCtrl = NULL;
476 }
477
478 if (m_ownsImageListNormal)
479 delete m_imageListNormal;
480 if (m_ownsImageListSmall)
481 delete m_imageListSmall;
482 if (m_ownsImageListState)
483 delete m_imageListState;
484 }
485
486 // ----------------------------------------------------------------------------
487 // set/get/change style
488 // ----------------------------------------------------------------------------
489
490 // Add or remove a single window style
491 void wxListCtrl::SetSingleStyle(long style, bool add)
492 {
493 long flag = GetWindowStyleFlag();
494
495 // Get rid of conflicting styles
496 if ( add )
497 {
498 if ( style & wxLC_MASK_TYPE)
499 flag = flag & ~wxLC_MASK_TYPE;
500 if ( style & wxLC_MASK_ALIGN )
501 flag = flag & ~wxLC_MASK_ALIGN;
502 if ( style & wxLC_MASK_SORT )
503 flag = flag & ~wxLC_MASK_SORT;
504 }
505
506 if ( add )
507 flag |= style;
508 else
509 flag &= ~style;
510
511 SetWindowStyleFlag(flag);
512 }
513
514 // Set the whole window style
515 void wxListCtrl::SetWindowStyleFlag(long flag)
516 {
517 if ( flag != m_windowStyle )
518 {
519 m_windowStyle = flag;
520
521 UpdateStyle();
522
523 Refresh();
524 }
525 }
526
527 // ----------------------------------------------------------------------------
528 // accessors
529 // ----------------------------------------------------------------------------
530
531 /* static */ wxVisualAttributes
532 wxListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
533 {
534 wxVisualAttributes attrs = GetCompositeControlsDefaultAttributes(variant);
535
536 // common controls have their own default font
537 attrs.font = wxGetCCDefaultFont();
538
539 return attrs;
540 }
541
542 // Sets the foreground, i.e. text, colour
543 bool wxListCtrl::SetForegroundColour(const wxColour& col)
544 {
545 if ( !wxWindow::SetForegroundColour(col) )
546 return false;
547
548 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col));
549
550 return true;
551 }
552
553 // Sets the background colour
554 bool wxListCtrl::SetBackgroundColour(const wxColour& col)
555 {
556 if ( !wxWindow::SetBackgroundColour(col) )
557 return false;
558
559 // we set the same colour for both the "empty" background and the items
560 // background
561 COLORREF color = wxColourToRGB(col);
562 ListView_SetBkColor(GetHwnd(), color);
563 ListView_SetTextBkColor(GetHwnd(), color);
564
565 return true;
566 }
567
568 // Gets information about this column
569 bool wxListCtrl::GetColumn(int col, wxListItem& item) const
570 {
571 LV_COLUMN lvCol;
572 wxZeroMemory(lvCol);
573
574 lvCol.mask = LVCF_WIDTH;
575
576 if ( item.m_mask & wxLIST_MASK_TEXT )
577 {
578 lvCol.mask |= LVCF_TEXT;
579 lvCol.pszText = new wxChar[513];
580 lvCol.cchTextMax = 512;
581 }
582
583 if ( item.m_mask & wxLIST_MASK_FORMAT )
584 {
585 lvCol.mask |= LVCF_FMT;
586 }
587
588 if ( item.m_mask & wxLIST_MASK_IMAGE )
589 {
590 lvCol.mask |= LVCF_IMAGE;
591 }
592
593 bool success = ListView_GetColumn(GetHwnd(), col, &lvCol) != 0;
594
595 // item.m_subItem = lvCol.iSubItem;
596 item.m_width = lvCol.cx;
597
598 if ( (item.m_mask & wxLIST_MASK_TEXT) && lvCol.pszText )
599 {
600 item.m_text = lvCol.pszText;
601 delete[] lvCol.pszText;
602 }
603
604 if ( item.m_mask & wxLIST_MASK_FORMAT )
605 {
606 switch (lvCol.fmt & LVCFMT_JUSTIFYMASK) {
607 case LVCFMT_LEFT:
608 item.m_format = wxLIST_FORMAT_LEFT;
609 break;
610 case LVCFMT_RIGHT:
611 item.m_format = wxLIST_FORMAT_RIGHT;
612 break;
613 case LVCFMT_CENTER:
614 item.m_format = wxLIST_FORMAT_CENTRE;
615 break;
616 default:
617 item.m_format = -1; // Unknown?
618 break;
619 }
620 }
621
622 // the column images were not supported in older versions but how to check
623 // for this? we can't use _WIN32_IE because we always define it to a very
624 // high value, so see if another symbol which is only defined starting from
625 // comctl32.dll 4.70 is available
626 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
627 if ( item.m_mask & wxLIST_MASK_IMAGE )
628 {
629 item.m_image = lvCol.iImage;
630 }
631 #endif // LVCOLUMN::iImage exists
632
633 return success;
634 }
635
636 // Sets information about this column
637 bool wxListCtrl::SetColumn(int col, const wxListItem& item)
638 {
639 LV_COLUMN lvCol;
640 wxConvertToMSWListCol(GetHwnd(), col, item, lvCol);
641
642 return ListView_SetColumn(GetHwnd(), col, &lvCol) != 0;
643 }
644
645 // Gets the column width
646 int wxListCtrl::GetColumnWidth(int col) const
647 {
648 return ListView_GetColumnWidth(GetHwnd(), col);
649 }
650
651 // Sets the column width
652 bool wxListCtrl::SetColumnWidth(int col, int width)
653 {
654 if ( m_windowStyle & wxLC_LIST )
655 col = 0;
656
657 if ( width == wxLIST_AUTOSIZE)
658 width = LVSCW_AUTOSIZE;
659 else if ( width == wxLIST_AUTOSIZE_USEHEADER)
660 width = LVSCW_AUTOSIZE_USEHEADER;
661
662 return ListView_SetColumnWidth(GetHwnd(), col, width) != 0;
663 }
664
665 // Gets the number of items that can fit vertically in the
666 // visible area of the list control (list or report view)
667 // or the total number of items in the list control (icon
668 // or small icon view)
669 int wxListCtrl::GetCountPerPage() const
670 {
671 return ListView_GetCountPerPage(GetHwnd());
672 }
673
674 // Gets the edit control for editing labels.
675 wxTextCtrl* wxListCtrl::GetEditControl() const
676 {
677 return m_textCtrl;
678 }
679
680 // Gets information about the item
681 bool wxListCtrl::GetItem(wxListItem& info) const
682 {
683 LV_ITEM lvItem;
684 wxZeroMemory(lvItem);
685
686 lvItem.iItem = info.m_itemId;
687 lvItem.iSubItem = info.m_col;
688
689 if ( info.m_mask & wxLIST_MASK_TEXT )
690 {
691 lvItem.mask |= LVIF_TEXT;
692 lvItem.pszText = new wxChar[513];
693 lvItem.cchTextMax = 512;
694 }
695 else
696 {
697 lvItem.pszText = NULL;
698 }
699
700 if (info.m_mask & wxLIST_MASK_DATA)
701 lvItem.mask |= LVIF_PARAM;
702
703 if (info.m_mask & wxLIST_MASK_IMAGE)
704 lvItem.mask |= LVIF_IMAGE;
705
706 if ( info.m_mask & wxLIST_MASK_STATE )
707 {
708 lvItem.mask |= LVIF_STATE;
709 wxConvertToMSWFlags(0, info.m_stateMask, lvItem);
710 }
711
712 bool success = ListView_GetItem((HWND)GetHWND(), &lvItem) != 0;
713 if ( !success )
714 {
715 wxLogError(_("Couldn't retrieve information about list control item %d."),
716 lvItem.iItem);
717 }
718 else
719 {
720 // give NULL as hwnd as we already have everything we need
721 wxConvertFromMSWListItem(NULL, info, lvItem);
722 }
723
724 if (lvItem.pszText)
725 delete[] lvItem.pszText;
726
727 return success;
728 }
729
730 // Sets information about the item
731 bool wxListCtrl::SetItem(wxListItem& info)
732 {
733 const long id = info.GetId();
734 wxCHECK_MSG( id >= 0 && id < GetItemCount(), false,
735 _T("invalid item index in SetItem") );
736
737 LV_ITEM item;
738 wxConvertToMSWListItem(this, info, item);
739
740 // we never update the lParam if it contains our pointer
741 // to the wxListItemInternalData structure
742 item.mask &= ~LVIF_PARAM;
743
744 // check if setting attributes or lParam
745 if (info.HasAttributes() || (info.m_mask & wxLIST_MASK_DATA))
746 {
747 // get internal item data
748 // perhaps a cache here ?
749 wxListItemInternalData *data = wxGetInternalData(this, id);
750
751 if (! data)
752 {
753 // need to set it
754 m_AnyInternalData = true;
755 data = new wxListItemInternalData();
756 item.lParam = (LPARAM) data;
757 item.mask |= LVIF_PARAM;
758 };
759
760
761 // user data
762 if (info.m_mask & wxLIST_MASK_DATA)
763 data->lParam = info.m_data;
764
765 // attributes
766 if ( info.HasAttributes() )
767 {
768 const wxListItemAttr& attrNew = *info.GetAttributes();
769
770 // don't overwrite the already set attributes if we have them
771 if ( data->attr )
772 data->attr->AssignFrom(attrNew);
773 else
774 data->attr = new wxListItemAttr(attrNew);
775 };
776 };
777
778
779 // we could be changing only the attribute in which case we don't need to
780 // call ListView_SetItem() at all
781 if ( item.mask )
782 {
783 item.cchTextMax = 0;
784 if ( !ListView_SetItem(GetHwnd(), &item) )
785 {
786 wxLogDebug(_T("ListView_SetItem() failed"));
787
788 return false;
789 }
790 }
791
792 // we need to update the item immediately to show the new image
793 bool updateNow = (info.m_mask & wxLIST_MASK_IMAGE) != 0;
794
795 // check whether it has any custom attributes
796 if ( info.HasAttributes() )
797 {
798 m_hasAnyAttr = true;
799
800 // if the colour has changed, we must redraw the item
801 updateNow = true;
802 }
803
804 if ( updateNow )
805 {
806 // we need this to make the change visible right now
807 RefreshItem(item.iItem);
808 }
809
810 return true;
811 }
812
813 long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId)
814 {
815 wxListItem info;
816 info.m_text = label;
817 info.m_mask = wxLIST_MASK_TEXT;
818 info.m_itemId = index;
819 info.m_col = col;
820 if ( imageId > -1 )
821 {
822 info.m_image = imageId;
823 info.m_mask |= wxLIST_MASK_IMAGE;
824 }
825 return SetItem(info);
826 }
827
828
829 // Gets the item state
830 int wxListCtrl::GetItemState(long item, long stateMask) const
831 {
832 wxListItem info;
833
834 info.m_mask = wxLIST_MASK_STATE;
835 info.m_stateMask = stateMask;
836 info.m_itemId = item;
837
838 if (!GetItem(info))
839 return 0;
840
841 return info.m_state;
842 }
843
844 // Sets the item state
845 bool wxListCtrl::SetItemState(long item, long state, long stateMask)
846 {
847 // NB: don't use SetItem() here as it doesn't work with the virtual list
848 // controls
849 LV_ITEM lvItem;
850 wxZeroMemory(lvItem);
851
852 wxConvertToMSWFlags(state, stateMask, lvItem);
853
854 // for the virtual list controls we need to refresh the previously focused
855 // item manually when changing focus without changing selection
856 // programmatically because otherwise it keeps its focus rectangle until
857 // next repaint (yet another comctl32 bug)
858 long focusOld;
859 if ( IsVirtual() &&
860 (stateMask & wxLIST_STATE_FOCUSED) &&
861 (state & wxLIST_STATE_FOCUSED) )
862 {
863 focusOld = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
864 }
865 else
866 {
867 focusOld = -1;
868 }
869
870 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE,
871 (WPARAM)item, (LPARAM)&lvItem) )
872 {
873 wxLogLastError(_T("ListView_SetItemState"));
874
875 return false;
876 }
877
878 if ( focusOld != -1 )
879 {
880 // no need to refresh the item if it was previously selected, it would
881 // only result in annoying flicker
882 if ( !(GetItemState(focusOld,
883 wxLIST_STATE_SELECTED) & wxLIST_STATE_SELECTED) )
884 {
885 RefreshItem(focusOld);
886 }
887 }
888
889 return true;
890 }
891
892 // Sets the item image
893 bool wxListCtrl::SetItemImage(long item, int image, int WXUNUSED(selImage))
894 {
895 return SetItemColumnImage(item, 0, image);
896 }
897
898 // Sets the item image
899 bool wxListCtrl::SetItemColumnImage(long item, long column, int image)
900 {
901 wxListItem info;
902
903 info.m_mask = wxLIST_MASK_IMAGE;
904 info.m_image = image;
905 info.m_itemId = item;
906 info.m_col = column;
907
908 return SetItem(info);
909 }
910
911 // Gets the item text
912 wxString wxListCtrl::GetItemText(long item) const
913 {
914 wxListItem info;
915
916 info.m_mask = wxLIST_MASK_TEXT;
917 info.m_itemId = item;
918
919 if (!GetItem(info))
920 return wxEmptyString;
921 return info.m_text;
922 }
923
924 // Sets the item text
925 void wxListCtrl::SetItemText(long item, const wxString& str)
926 {
927 wxListItem info;
928
929 info.m_mask = wxLIST_MASK_TEXT;
930 info.m_itemId = item;
931 info.m_text = str;
932
933 SetItem(info);
934 }
935
936 // Gets the item data
937 wxUIntPtr wxListCtrl::GetItemData(long item) const
938 {
939 wxListItem info;
940
941 info.m_mask = wxLIST_MASK_DATA;
942 info.m_itemId = item;
943
944 if (!GetItem(info))
945 return 0;
946 return info.m_data;
947 }
948
949 // Sets the item data
950 bool wxListCtrl::SetItemData(long item, long data)
951 {
952 wxListItem info;
953
954 info.m_mask = wxLIST_MASK_DATA;
955 info.m_itemId = item;
956 info.m_data = data;
957
958 return SetItem(info);
959 }
960
961 wxRect wxListCtrl::GetViewRect() const
962 {
963 wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
964 _T("wxListCtrl::GetViewRect() only works in icon mode") );
965
966 RECT rc;
967 if ( !ListView_GetViewRect(GetHwnd(), &rc) )
968 {
969 wxLogDebug(_T("ListView_GetViewRect() failed."));
970
971 wxZeroMemory(rc);
972 }
973
974 wxRect rect;
975 wxCopyRECTToRect(rc, rect);
976
977 return rect;
978 }
979
980 // Gets the item rectangle
981 bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
982 {
983 return GetSubItemRect( item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code) ;
984 }
985
986 /*!
987 * Retrieve coordinates and size of a specified subitem of a listview control.
988 * This function only works if the listview control is in the report mode.
989 *
990 * @param item : Item number
991 * @param subItem : Subitem or column number, use -1 for the whole row including
992 * all columns or subitems
993 * @param rect : A pointer to an allocated wxRect object
994 * @param code : Specify the part of the subitem coordinates you need. Choices are
995 * wxLIST_RECT_BOUNDS, wxLIST_RECT_ICON, wxLIST_RECT_LABEL
996 *
997 * @return bool : True if successful.
998 */
999 bool wxListCtrl::GetSubItemRect(long item, long subItem, wxRect& rect, int code) const
1000 {
1001 RECT rectWin;
1002
1003 int codeWin;
1004 if ( code == wxLIST_RECT_BOUNDS )
1005 codeWin = LVIR_BOUNDS;
1006 else if ( code == wxLIST_RECT_ICON )
1007 codeWin = LVIR_ICON;
1008 else if ( code == wxLIST_RECT_LABEL )
1009 codeWin = LVIR_LABEL;
1010 else
1011 {
1012 wxFAIL_MSG( _T("incorrect code in GetItemRect() / GetSubItemRect()") );
1013 codeWin = LVIR_BOUNDS;
1014 }
1015
1016 bool success;
1017 if( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM)
1018 {
1019 success = ListView_GetItemRect(GetHwnd(), (int) item, &rectWin, codeWin) != 0;
1020 }
1021 else if( subItem >= 0)
1022 {
1023 success = ListView_GetSubItemRect( GetHwnd(), (int) item, (int) subItem, codeWin, &rectWin) != 0;
1024 }
1025 else
1026 {
1027 wxFAIL_MSG( _T("incorrect subItem number in GetSubItemRect()") );
1028 return false;
1029 }
1030
1031 rect.x = rectWin.left;
1032 rect.y = rectWin.top;
1033 rect.width = rectWin.right - rectWin.left;
1034 rect.height = rectWin.bottom - rectWin.top;
1035
1036 return success;
1037 }
1038
1039
1040
1041
1042 // Gets the item position
1043 bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
1044 {
1045 POINT pt;
1046
1047 bool success = (ListView_GetItemPosition(GetHwnd(), (int) item, &pt) != 0);
1048
1049 pos.x = pt.x; pos.y = pt.y;
1050 return success;
1051 }
1052
1053 // Sets the item position.
1054 bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
1055 {
1056 return (ListView_SetItemPosition(GetHwnd(), (int) item, pos.x, pos.y) != 0);
1057 }
1058
1059 // Gets the number of items in the list control
1060 int wxListCtrl::GetItemCount() const
1061 {
1062 return m_count;
1063 }
1064
1065 wxSize wxListCtrl::GetItemSpacing() const
1066 {
1067 const int spacing = ListView_GetItemSpacing(GetHwnd(), (BOOL)HasFlag(wxLC_SMALL_ICON));
1068
1069 return wxSize(LOWORD(spacing), HIWORD(spacing));
1070 }
1071
1072 #if WXWIN_COMPATIBILITY_2_6
1073
1074 int wxListCtrl::GetItemSpacing(bool isSmall) const
1075 {
1076 return ListView_GetItemSpacing(GetHwnd(), (BOOL) isSmall);
1077 }
1078
1079 #endif // WXWIN_COMPATIBILITY_2_6
1080
1081 void wxListCtrl::SetItemTextColour( long item, const wxColour &col )
1082 {
1083 wxListItem info;
1084 info.m_itemId = item;
1085 info.SetTextColour( col );
1086 SetItem( info );
1087 }
1088
1089 wxColour wxListCtrl::GetItemTextColour( long item ) const
1090 {
1091 wxColour col;
1092 wxListItemInternalData *data = wxGetInternalData(this, item);
1093 if ( data && data->attr )
1094 col = data->attr->GetTextColour();
1095
1096 return col;
1097 }
1098
1099 void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
1100 {
1101 wxListItem info;
1102 info.m_itemId = item;
1103 info.SetBackgroundColour( col );
1104 SetItem( info );
1105 }
1106
1107 wxColour wxListCtrl::GetItemBackgroundColour( long item ) const
1108 {
1109 wxColour col;
1110 wxListItemInternalData *data = wxGetInternalData(this, item);
1111 if ( data && data->attr )
1112 col = data->attr->GetBackgroundColour();
1113
1114 return col;
1115 }
1116
1117 void wxListCtrl::SetItemFont( long item, const wxFont &f )
1118 {
1119 wxListItem info;
1120 info.m_itemId = item;
1121 info.SetFont( f );
1122 SetItem( info );
1123 }
1124
1125 wxFont wxListCtrl::GetItemFont( long item ) const
1126 {
1127 wxFont f;
1128 wxListItemInternalData *data = wxGetInternalData(this, item);
1129 if ( data && data->attr )
1130 f = data->attr->GetFont();
1131
1132 return f;
1133 }
1134
1135 // Gets the number of selected items in the list control
1136 int wxListCtrl::GetSelectedItemCount() const
1137 {
1138 return ListView_GetSelectedCount(GetHwnd());
1139 }
1140
1141 // Gets the text colour of the listview
1142 wxColour wxListCtrl::GetTextColour() const
1143 {
1144 COLORREF ref = ListView_GetTextColor(GetHwnd());
1145 wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
1146 return col;
1147 }
1148
1149 // Sets the text colour of the listview
1150 void wxListCtrl::SetTextColour(const wxColour& col)
1151 {
1152 ListView_SetTextColor(GetHwnd(), PALETTERGB(col.Red(), col.Green(), col.Blue()));
1153 }
1154
1155 // Gets the index of the topmost visible item when in
1156 // list or report view
1157 long wxListCtrl::GetTopItem() const
1158 {
1159 return (long) ListView_GetTopIndex(GetHwnd());
1160 }
1161
1162 // Searches for an item, starting from 'item'.
1163 // 'geometry' is one of
1164 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1165 // 'state' is a state bit flag, one or more of
1166 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1167 // item can be -1 to find the first item that matches the
1168 // specified flags.
1169 // Returns the item or -1 if unsuccessful.
1170 long wxListCtrl::GetNextItem(long item, int geom, int state) const
1171 {
1172 long flags = 0;
1173
1174 if ( geom == wxLIST_NEXT_ABOVE )
1175 flags |= LVNI_ABOVE;
1176 if ( geom == wxLIST_NEXT_ALL )
1177 flags |= LVNI_ALL;
1178 if ( geom == wxLIST_NEXT_BELOW )
1179 flags |= LVNI_BELOW;
1180 if ( geom == wxLIST_NEXT_LEFT )
1181 flags |= LVNI_TOLEFT;
1182 if ( geom == wxLIST_NEXT_RIGHT )
1183 flags |= LVNI_TORIGHT;
1184
1185 if ( state & wxLIST_STATE_CUT )
1186 flags |= LVNI_CUT;
1187 if ( state & wxLIST_STATE_DROPHILITED )
1188 flags |= LVNI_DROPHILITED;
1189 if ( state & wxLIST_STATE_FOCUSED )
1190 flags |= LVNI_FOCUSED;
1191 if ( state & wxLIST_STATE_SELECTED )
1192 flags |= LVNI_SELECTED;
1193
1194 return (long) ListView_GetNextItem(GetHwnd(), item, flags);
1195 }
1196
1197
1198 wxImageList *wxListCtrl::GetImageList(int which) const
1199 {
1200 if ( which == wxIMAGE_LIST_NORMAL )
1201 {
1202 return m_imageListNormal;
1203 }
1204 else if ( which == wxIMAGE_LIST_SMALL )
1205 {
1206 return m_imageListSmall;
1207 }
1208 else if ( which == wxIMAGE_LIST_STATE )
1209 {
1210 return m_imageListState;
1211 }
1212 return NULL;
1213 }
1214
1215 void wxListCtrl::SetImageList(wxImageList *imageList, int which)
1216 {
1217 int flags = 0;
1218 if ( which == wxIMAGE_LIST_NORMAL )
1219 {
1220 flags = LVSIL_NORMAL;
1221 if (m_ownsImageListNormal) delete m_imageListNormal;
1222 m_imageListNormal = imageList;
1223 m_ownsImageListNormal = false;
1224 }
1225 else if ( which == wxIMAGE_LIST_SMALL )
1226 {
1227 flags = LVSIL_SMALL;
1228 if (m_ownsImageListSmall) delete m_imageListSmall;
1229 m_imageListSmall = imageList;
1230 m_ownsImageListSmall = false;
1231 }
1232 else if ( which == wxIMAGE_LIST_STATE )
1233 {
1234 flags = LVSIL_STATE;
1235 if (m_ownsImageListState) delete m_imageListState;
1236 m_imageListState = imageList;
1237 m_ownsImageListState = false;
1238 }
1239 (void) ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags);
1240 }
1241
1242 void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
1243 {
1244 SetImageList(imageList, which);
1245 if ( which == wxIMAGE_LIST_NORMAL )
1246 m_ownsImageListNormal = true;
1247 else if ( which == wxIMAGE_LIST_SMALL )
1248 m_ownsImageListSmall = true;
1249 else if ( which == wxIMAGE_LIST_STATE )
1250 m_ownsImageListState = true;
1251 }
1252
1253 // ----------------------------------------------------------------------------
1254 // Operations
1255 // ----------------------------------------------------------------------------
1256
1257 // Arranges the items
1258 bool wxListCtrl::Arrange(int flag)
1259 {
1260 UINT code = 0;
1261 if ( flag == wxLIST_ALIGN_LEFT )
1262 code = LVA_ALIGNLEFT;
1263 else if ( flag == wxLIST_ALIGN_TOP )
1264 code = LVA_ALIGNTOP;
1265 else if ( flag == wxLIST_ALIGN_DEFAULT )
1266 code = LVA_DEFAULT;
1267 else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID )
1268 code = LVA_SNAPTOGRID;
1269
1270 return (ListView_Arrange(GetHwnd(), code) != 0);
1271 }
1272
1273 // Deletes an item
1274 bool wxListCtrl::DeleteItem(long item)
1275 {
1276 if ( !ListView_DeleteItem(GetHwnd(), (int) item) )
1277 {
1278 wxLogLastError(_T("ListView_DeleteItem"));
1279 return false;
1280 }
1281
1282 m_count--;
1283 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1284 wxT("m_count should match ListView_GetItemCount"));
1285
1286 // the virtual list control doesn't refresh itself correctly, help it
1287 if ( IsVirtual() )
1288 {
1289 // we need to refresh all the lines below the one which was deleted
1290 wxRect rectItem;
1291 if ( item > 0 && GetItemCount() )
1292 {
1293 GetItemRect(item - 1, rectItem);
1294 }
1295 else
1296 {
1297 rectItem.y =
1298 rectItem.height = 0;
1299 }
1300
1301 wxRect rectWin = GetRect();
1302 rectWin.height = rectWin.GetBottom() - rectItem.GetBottom();
1303 rectWin.y = rectItem.GetBottom();
1304
1305 RefreshRect(rectWin);
1306 }
1307
1308 return true;
1309 }
1310
1311 // Deletes all items
1312 bool wxListCtrl::DeleteAllItems()
1313 {
1314 return ListView_DeleteAllItems(GetHwnd()) != 0;
1315 }
1316
1317 // Deletes all items
1318 bool wxListCtrl::DeleteAllColumns()
1319 {
1320 while ( m_colCount > 0 )
1321 {
1322 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1323 {
1324 wxLogLastError(wxT("ListView_DeleteColumn"));
1325
1326 return false;
1327 }
1328
1329 m_colCount--;
1330 }
1331
1332 wxASSERT_MSG( m_colCount == 0, wxT("no columns should be left") );
1333
1334 return true;
1335 }
1336
1337 // Deletes a column
1338 bool wxListCtrl::DeleteColumn(int col)
1339 {
1340 bool success = (ListView_DeleteColumn(GetHwnd(), col) != 0);
1341
1342 if ( success && (m_colCount > 0) )
1343 m_colCount --;
1344 return success;
1345 }
1346
1347 // Clears items, and columns if there are any.
1348 void wxListCtrl::ClearAll()
1349 {
1350 DeleteAllItems();
1351 if ( m_colCount > 0 )
1352 DeleteAllColumns();
1353 }
1354
1355 wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1356 {
1357 wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) );
1358
1359 // ListView_EditLabel requires that the list has focus.
1360 SetFocus();
1361
1362 WXHWND hWnd = (WXHWND) ListView_EditLabel(GetHwnd(), item);
1363 if ( !hWnd )
1364 {
1365 // failed to start editing
1366 return NULL;
1367 }
1368
1369 // [re]create the text control wrapping the HWND we got
1370 if ( m_textCtrl )
1371 {
1372 m_textCtrl->UnsubclassWin();
1373 m_textCtrl->SetHWND(0);
1374 delete m_textCtrl;
1375 }
1376
1377 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1378 m_textCtrl->SetHWND(hWnd);
1379 m_textCtrl->SubclassWin(hWnd);
1380 m_textCtrl->SetParent(this);
1381
1382 // we must disallow TABbing away from the control while the edit contol is
1383 // shown because this leaves it in some strange state (just try removing
1384 // this line and then pressing TAB while editing an item in listctrl
1385 // inside a panel)
1386 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | wxTE_PROCESS_TAB);
1387
1388 return m_textCtrl;
1389 }
1390
1391 // End label editing, optionally cancelling the edit
1392 bool wxListCtrl::EndEditLabel(bool cancel)
1393 {
1394 // m_textCtrl is not always ready, ie. in EVT_LIST_BEGIN_LABEL_EDIT
1395 HWND hwnd = ListView_GetEditControl(GetHwnd());
1396 bool b = (hwnd != NULL);
1397 if (b)
1398 {
1399 if (cancel)
1400 ::SetWindowText(hwnd, wxEmptyString); // dubious but better than nothing
1401 if (m_textCtrl)
1402 {
1403 m_textCtrl->UnsubclassWin();
1404 m_textCtrl->SetHWND(0);
1405 delete m_textCtrl;
1406 m_textCtrl = NULL;
1407 }
1408 ::DestroyWindow(hwnd);
1409 }
1410 return b;
1411 }
1412
1413 // Ensures this item is visible
1414 bool wxListCtrl::EnsureVisible(long item)
1415 {
1416 return ListView_EnsureVisible(GetHwnd(), (int) item, FALSE) != FALSE;
1417 }
1418
1419 // Find an item whose label matches this string, starting from the item after 'start'
1420 // or the beginning if 'start' is -1.
1421 long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
1422 {
1423 LV_FINDINFO findInfo;
1424
1425 findInfo.flags = LVFI_STRING;
1426 if ( partial )
1427 findInfo.flags |= LVFI_PARTIAL;
1428 findInfo.psz = str;
1429
1430 // ListView_FindItem() excludes the first item from search and to look
1431 // through all the items you need to start from -1 which is unnatural and
1432 // inconsistent with the generic version - so we adjust the index
1433 if (start != -1)
1434 start --;
1435 return ListView_FindItem(GetHwnd(), (int) start, &findInfo);
1436 }
1437
1438 // Find an item whose data matches this data, starting from the item after 'start'
1439 // or the beginning if 'start' is -1.
1440 // NOTE : Lindsay Mathieson - 14-July-2002
1441 // No longer use ListView_FindItem as the data attribute is now stored
1442 // in a wxListItemInternalData structure refernced by the actual lParam
1443 long wxListCtrl::FindItem(long start, wxUIntPtr data)
1444 {
1445 long idx = start + 1;
1446 long count = GetItemCount();
1447
1448 while (idx < count)
1449 {
1450 if (GetItemData(idx) == data)
1451 return idx;
1452 idx++;
1453 };
1454
1455 return -1;
1456 }
1457
1458 // Find an item nearest this position in the specified direction, starting from
1459 // the item after 'start' or the beginning if 'start' is -1.
1460 long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
1461 {
1462 LV_FINDINFO findInfo;
1463
1464 findInfo.flags = LVFI_NEARESTXY;
1465 findInfo.pt.x = pt.x;
1466 findInfo.pt.y = pt.y;
1467 findInfo.vkDirection = VK_RIGHT;
1468
1469 if ( direction == wxLIST_FIND_UP )
1470 findInfo.vkDirection = VK_UP;
1471 else if ( direction == wxLIST_FIND_DOWN )
1472 findInfo.vkDirection = VK_DOWN;
1473 else if ( direction == wxLIST_FIND_LEFT )
1474 findInfo.vkDirection = VK_LEFT;
1475 else if ( direction == wxLIST_FIND_RIGHT )
1476 findInfo.vkDirection = VK_RIGHT;
1477
1478 return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
1479 }
1480
1481 // Determines which item (if any) is at the specified point,
1482 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1483 long
1484 wxListCtrl::HitTest(const wxPoint& point, int& flags, long *ptrSubItem) const
1485 {
1486 LV_HITTESTINFO hitTestInfo;
1487 hitTestInfo.pt.x = (int) point.x;
1488 hitTestInfo.pt.y = (int) point.y;
1489
1490 long item;
1491 #ifdef LVM_SUBITEMHITTEST
1492 if ( ptrSubItem && wxApp::GetComCtl32Version() >= 470 )
1493 {
1494 item = ListView_SubItemHitTest(GetHwnd(), &hitTestInfo);
1495 *ptrSubItem = hitTestInfo.iSubItem;
1496 }
1497 else
1498 #endif // LVM_SUBITEMHITTEST
1499 {
1500 item = ListView_HitTest(GetHwnd(), &hitTestInfo);
1501 }
1502
1503 flags = 0;
1504
1505 if ( hitTestInfo.flags & LVHT_ABOVE )
1506 flags |= wxLIST_HITTEST_ABOVE;
1507 if ( hitTestInfo.flags & LVHT_BELOW )
1508 flags |= wxLIST_HITTEST_BELOW;
1509 if ( hitTestInfo.flags & LVHT_TOLEFT )
1510 flags |= wxLIST_HITTEST_TOLEFT;
1511 if ( hitTestInfo.flags & LVHT_TORIGHT )
1512 flags |= wxLIST_HITTEST_TORIGHT;
1513
1514 if ( hitTestInfo.flags & LVHT_NOWHERE )
1515 flags |= wxLIST_HITTEST_NOWHERE;
1516
1517 // note a bug or at least a very strange feature of comtl32.dll (tested
1518 // with version 4.0 under Win95 and 6.0 under Win 2003): if you click to
1519 // the right of the item label, ListView_HitTest() returns a combination of
1520 // LVHT_ONITEMICON, LVHT_ONITEMLABEL and LVHT_ONITEMSTATEICON -- filter out
1521 // the bits which don't make sense
1522 if ( hitTestInfo.flags & LVHT_ONITEMLABEL )
1523 {
1524 flags |= wxLIST_HITTEST_ONITEMLABEL;
1525
1526 // do not translate LVHT_ONITEMICON here, as per above
1527 }
1528 else
1529 {
1530 if ( hitTestInfo.flags & LVHT_ONITEMICON )
1531 flags |= wxLIST_HITTEST_ONITEMICON;
1532 if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON )
1533 flags |= wxLIST_HITTEST_ONITEMSTATEICON;
1534 }
1535
1536 return item;
1537 }
1538
1539
1540 // Inserts an item, returning the index of the new item if successful,
1541 // -1 otherwise.
1542 long wxListCtrl::InsertItem(const wxListItem& info)
1543 {
1544 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1545
1546 LV_ITEM item;
1547 wxConvertToMSWListItem(this, info, item);
1548 item.mask &= ~LVIF_PARAM;
1549
1550 // check wether we need to allocate our internal data
1551 bool needInternalData = ((info.m_mask & wxLIST_MASK_DATA) || info.HasAttributes());
1552 if (needInternalData)
1553 {
1554 m_AnyInternalData = true;
1555 item.mask |= LVIF_PARAM;
1556
1557 // internal stucture that manages data
1558 wxListItemInternalData *data = new wxListItemInternalData();
1559 item.lParam = (LPARAM) data;
1560
1561 if (info.m_mask & wxLIST_MASK_DATA)
1562 data->lParam = info.m_data;
1563
1564 // check whether it has any custom attributes
1565 if ( info.HasAttributes() )
1566 {
1567 // take copy of attributes
1568 data->attr = new wxListItemAttr(*info.GetAttributes());
1569
1570 // and remember that we have some now...
1571 m_hasAnyAttr = true;
1572 }
1573 };
1574
1575 long rv = ListView_InsertItem(GetHwnd(), & item);
1576
1577 m_count++;
1578 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1579 wxT("m_count should match ListView_GetItemCount"));
1580
1581 return rv;
1582 }
1583
1584 long wxListCtrl::InsertItem(long index, const wxString& label)
1585 {
1586 wxListItem info;
1587 info.m_text = label;
1588 info.m_mask = wxLIST_MASK_TEXT;
1589 info.m_itemId = index;
1590 return InsertItem(info);
1591 }
1592
1593 // Inserts an image item
1594 long wxListCtrl::InsertItem(long index, int imageIndex)
1595 {
1596 wxListItem info;
1597 info.m_image = imageIndex;
1598 info.m_mask = wxLIST_MASK_IMAGE;
1599 info.m_itemId = index;
1600 return InsertItem(info);
1601 }
1602
1603 // Inserts an image/string item
1604 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1605 {
1606 wxListItem info;
1607 info.m_image = imageIndex;
1608 info.m_text = label;
1609 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1610 info.m_itemId = index;
1611 return InsertItem(info);
1612 }
1613
1614 // For list view mode (only), inserts a column.
1615 long wxListCtrl::InsertColumn(long col, const wxListItem& item)
1616 {
1617 LV_COLUMN lvCol;
1618 wxConvertToMSWListCol(GetHwnd(), col, item, lvCol);
1619
1620 if ( !(lvCol.mask & LVCF_WIDTH) )
1621 {
1622 // always give some width to the new column: this one is compatible
1623 // with the generic version
1624 lvCol.mask |= LVCF_WIDTH;
1625 lvCol.cx = 80;
1626 }
1627
1628 long n = ListView_InsertColumn(GetHwnd(), col, &lvCol);
1629 if ( n != -1 )
1630 {
1631 m_colCount++;
1632 }
1633 else // failed to insert?
1634 {
1635 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1636 lvCol.pszText);
1637 }
1638
1639 return n;
1640 }
1641
1642 long wxListCtrl::InsertColumn(long col,
1643 const wxString& heading,
1644 int format,
1645 int width)
1646 {
1647 wxListItem item;
1648 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1649 item.m_text = heading;
1650 if ( width > -1 )
1651 {
1652 item.m_mask |= wxLIST_MASK_WIDTH;
1653 item.m_width = width;
1654 }
1655 item.m_format = format;
1656
1657 return InsertColumn(col, item);
1658 }
1659
1660 // scroll the control by the given number of pixels (exception: in list view,
1661 // dx is interpreted as number of columns)
1662 bool wxListCtrl::ScrollList(int dx, int dy)
1663 {
1664 if ( !ListView_Scroll(GetHwnd(), dx, dy) )
1665 {
1666 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx, dy);
1667
1668 return false;
1669 }
1670
1671 return true;
1672 }
1673
1674 // Sort items.
1675
1676 // fn is a function which takes 3 long arguments: item1, item2, data.
1677 // item1 is the long data associated with a first item (NOT the index).
1678 // item2 is the long data associated with a second item (NOT the index).
1679 // data is the same value as passed to SortItems.
1680 // The return value is a negative number if the first item should precede the second
1681 // item, a positive number of the second item should precede the first,
1682 // or zero if the two items are equivalent.
1683
1684 // data is arbitrary data to be passed to the sort function.
1685
1686 // Internal structures for proxying the user compare function
1687 // so that we can pass it the *real* user data
1688
1689 // translate lParam data and call user func
1690 struct wxInternalDataSort
1691 {
1692 wxListCtrlCompare user_fn;
1693 long data;
1694 };
1695
1696 int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
1697 {
1698 struct wxInternalDataSort *internalData = (struct wxInternalDataSort *) lParamSort;
1699
1700 wxListItemInternalData *data1 = (wxListItemInternalData *) lParam1;
1701 wxListItemInternalData *data2 = (wxListItemInternalData *) lParam2;
1702
1703 long d1 = (data1 == NULL ? 0 : data1->lParam);
1704 long d2 = (data2 == NULL ? 0 : data2->lParam);
1705
1706 return internalData->user_fn(d1, d2, internalData->data);
1707
1708 }
1709
1710 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1711 {
1712 struct wxInternalDataSort internalData;
1713 internalData.user_fn = fn;
1714 internalData.data = data;
1715
1716 // WPARAM cast is needed for mingw/cygwin
1717 if ( !ListView_SortItems(GetHwnd(),
1718 wxInternalDataCompareFunc,
1719 (WPARAM) &internalData) )
1720 {
1721 wxLogDebug(_T("ListView_SortItems() failed"));
1722
1723 return false;
1724 }
1725
1726 return true;
1727 }
1728
1729
1730
1731 // ----------------------------------------------------------------------------
1732 // message processing
1733 // ----------------------------------------------------------------------------
1734
1735 bool wxListCtrl::MSWShouldPreProcessMessage(WXMSG* msg)
1736 {
1737 if ( msg->message == WM_KEYDOWN )
1738 {
1739 if ( msg->wParam == VK_RETURN )
1740 {
1741 // we need VK_RETURN to generate wxEVT_COMMAND_LIST_ITEM_ACTIVATED
1742 return false;
1743 }
1744 }
1745
1746 return wxControl::MSWShouldPreProcessMessage(msg);
1747 }
1748
1749 bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1750 {
1751 if (cmd == EN_UPDATE)
1752 {
1753 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1754 event.SetEventObject( this );
1755 ProcessCommand(event);
1756 return true;
1757 }
1758 else if (cmd == EN_KILLFOCUS)
1759 {
1760 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1761 event.SetEventObject( this );
1762 ProcessCommand(event);
1763 return true;
1764 }
1765 else
1766 return false;
1767 }
1768
1769 bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1770 {
1771
1772 // prepare the event
1773 // -----------------
1774
1775 wxListEvent event(wxEVT_NULL, m_windowId);
1776 event.SetEventObject(this);
1777
1778 wxEventType eventType = wxEVT_NULL;
1779
1780 NMHDR *nmhdr = (NMHDR *)lParam;
1781
1782 // if your compiler is as broken as this, you should really change it: this
1783 // code is needed for normal operation! #ifdef below is only useful for
1784 // automatic rebuilds which are done with a very old compiler version
1785 #ifdef HDN_BEGINTRACKA
1786
1787 // check for messages from the header (in report view)
1788 HWND hwndHdr = ListView_GetHeader(GetHwnd());
1789
1790 // is it a message from the header?
1791 if ( nmhdr->hwndFrom == hwndHdr )
1792 {
1793 HD_NOTIFY *nmHDR = (HD_NOTIFY *)nmhdr;
1794
1795 event.m_itemIndex = -1;
1796
1797 switch ( nmhdr->code )
1798 {
1799 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1800 // TRACK messages even to ANSI programs: on my system I get
1801 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1802 //
1803 // work around is to simply catch both versions and hope that it
1804 // works (why should this message exist in ANSI and Unicode is
1805 // beyond me as it doesn't deal with strings at all...)
1806 //
1807 // note that fr HDN_TRACK another possibility could be to use
1808 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1809 // something other than the item width changes so we'd have to
1810 // filter out the unwanted events then
1811 case HDN_BEGINTRACKA:
1812 case HDN_BEGINTRACKW:
1813 eventType = wxEVT_COMMAND_LIST_COL_BEGIN_DRAG;
1814 // fall through
1815
1816 case HDN_TRACKA:
1817 case HDN_TRACKW:
1818 if ( eventType == wxEVT_NULL )
1819 eventType = wxEVT_COMMAND_LIST_COL_DRAGGING;
1820 // fall through
1821
1822 case HDN_ENDTRACKA:
1823 case HDN_ENDTRACKW:
1824 if ( eventType == wxEVT_NULL )
1825 eventType = wxEVT_COMMAND_LIST_COL_END_DRAG;
1826
1827 event.m_item.m_width = nmHDR->pitem->cxy;
1828 event.m_col = nmHDR->iItem;
1829 break;
1830
1831 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1832 case GN_CONTEXTMENU:
1833 #endif //__WXWINCE__
1834 case NM_RCLICK:
1835 {
1836 eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK;
1837 event.m_col = -1;
1838
1839 // find the column clicked: we have to search for it
1840 // ourselves as the notification message doesn't provide
1841 // this info
1842
1843 // where did the click occur?
1844 POINT ptClick;
1845 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1846 if(nmhdr->code == GN_CONTEXTMENU) {
1847 ptClick = ((NMRGINFO*)nmhdr)->ptAction;
1848 } else
1849 #endif //__WXWINCE__
1850 if ( !::GetCursorPos(&ptClick) )
1851 {
1852 wxLogLastError(_T("GetCursorPos"));
1853 }
1854
1855 if ( !::ScreenToClient(GetHwnd(), &ptClick) )
1856 {
1857 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1858 }
1859
1860 event.m_pointDrag.x = ptClick.x;
1861 event.m_pointDrag.y = ptClick.y;
1862
1863 int colCount = Header_GetItemCount(hwndHdr);
1864
1865 RECT rect;
1866 for ( int col = 0; col < colCount; col++ )
1867 {
1868 if ( Header_GetItemRect(hwndHdr, col, &rect) )
1869 {
1870 if ( ::PtInRect(&rect, ptClick) )
1871 {
1872 event.m_col = col;
1873 break;
1874 }
1875 }
1876 }
1877 }
1878 break;
1879
1880 case HDN_GETDISPINFOW:
1881 // letting Windows XP handle this message results in mysterious
1882 // crashes in comctl32.dll seemingly because of bad message
1883 // parameters
1884 //
1885 // I have no idea what is the real cause of the bug (which is,
1886 // just to make things interesting, is impossible to reproduce
1887 // reliably) but ignoring all these messages does fix it and
1888 // doesn't seem to have any negative consequences
1889 return true;
1890
1891 default:
1892 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1893 }
1894 }
1895 else
1896 #endif // defined(HDN_BEGINTRACKA)
1897 if ( nmhdr->hwndFrom == GetHwnd() )
1898 {
1899 // almost all messages use NM_LISTVIEW
1900 NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr;
1901
1902 const int iItem = nmLV->iItem;
1903
1904
1905 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1906 // ignored for efficiency. It is done here because the internal data is in the
1907 // process of being deleted so we don't want to try and access it below.
1908 if ( m_ignoreChangeMessages &&
1909 ( (nmLV->hdr.code == LVN_ITEMCHANGED) ||
1910 (nmLV->hdr.code == LVN_ITEMCHANGING)) )
1911 {
1912 return true;
1913 }
1914
1915
1916 // If we have a valid item then check if there is a data value
1917 // associated with it and put it in the event.
1918 if ( iItem >= 0 && iItem < GetItemCount() )
1919 {
1920 wxListItemInternalData *internaldata =
1921 wxGetInternalData(GetHwnd(), iItem);
1922
1923 if ( internaldata )
1924 event.m_item.m_data = internaldata->lParam;
1925 }
1926
1927 bool processed = true;
1928 switch ( nmhdr->code )
1929 {
1930 case LVN_BEGINRDRAG:
1931 eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
1932 // fall through
1933
1934 case LVN_BEGINDRAG:
1935 if ( eventType == wxEVT_NULL )
1936 {
1937 eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG;
1938 }
1939
1940 event.m_itemIndex = iItem;
1941 event.m_pointDrag.x = nmLV->ptAction.x;
1942 event.m_pointDrag.y = nmLV->ptAction.y;
1943 break;
1944
1945 // NB: we have to handle both *A and *W versions here because some
1946 // versions of comctl32.dll send ANSI messages even to the
1947 // Unicode windows
1948 case LVN_BEGINLABELEDITA:
1949 case LVN_BEGINLABELEDITW:
1950 {
1951 wxLV_ITEM item;
1952 if ( nmhdr->code == LVN_BEGINLABELEDITA )
1953 {
1954 item.Init(((LV_DISPINFOA *)lParam)->item);
1955 }
1956 else // LVN_BEGINLABELEDITW
1957 {
1958 item.Init(((LV_DISPINFOW *)lParam)->item);
1959 }
1960
1961 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
1962 wxConvertFromMSWListItem(GetHwnd(), event.m_item, item);
1963 event.m_itemIndex = event.m_item.m_itemId;
1964 }
1965 break;
1966
1967 case LVN_ENDLABELEDITA:
1968 case LVN_ENDLABELEDITW:
1969 {
1970 wxLV_ITEM item;
1971 if ( nmhdr->code == LVN_ENDLABELEDITA )
1972 {
1973 item.Init(((LV_DISPINFOA *)lParam)->item);
1974 }
1975 else // LVN_ENDLABELEDITW
1976 {
1977 item.Init(((LV_DISPINFOW *)lParam)->item);
1978 }
1979
1980 // was editing cancelled?
1981 const LV_ITEM& lvi = (LV_ITEM)item;
1982 if ( !lvi.pszText || lvi.iItem == -1 )
1983 {
1984 // don't keep a stale wxTextCtrl around
1985 if ( m_textCtrl )
1986 {
1987 // EDIT control will be deleted by the list control itself so
1988 // prevent us from deleting it as well
1989 m_textCtrl->UnsubclassWin();
1990 m_textCtrl->SetHWND(0);
1991 delete m_textCtrl;
1992 m_textCtrl = NULL;
1993 }
1994
1995 event.SetEditCanceled(true);
1996 }
1997
1998 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
1999 wxConvertFromMSWListItem(NULL, event.m_item, item);
2000 event.m_itemIndex = event.m_item.m_itemId;
2001 }
2002 break;
2003
2004 case LVN_COLUMNCLICK:
2005 eventType = wxEVT_COMMAND_LIST_COL_CLICK;
2006 event.m_itemIndex = -1;
2007 event.m_col = nmLV->iSubItem;
2008 break;
2009
2010 case LVN_DELETEALLITEMS:
2011 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
2012 event.m_itemIndex = -1;
2013 break;
2014
2015 case LVN_DELETEITEM:
2016 if ( m_count == 0 )
2017 {
2018 // this should be prevented by the post-processing code
2019 // below, but "just in case"
2020 return false;
2021 }
2022
2023 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
2024 event.m_itemIndex = iItem;
2025 // delete the assoicated internal data
2026 wxDeleteInternalData(this, iItem);
2027 break;
2028
2029 #if WXWIN_COMPATIBILITY_2_4
2030 case LVN_SETDISPINFO:
2031 {
2032 eventType = wxEVT_COMMAND_LIST_SET_INFO;
2033 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
2034 wxConvertFromMSWListItem(GetHwnd(), event.m_item, info->item);
2035 }
2036 break;
2037 #endif
2038
2039 case LVN_INSERTITEM:
2040 eventType = wxEVT_COMMAND_LIST_INSERT_ITEM;
2041 event.m_itemIndex = iItem;
2042 break;
2043
2044 case LVN_ITEMCHANGED:
2045 // we translate this catch all message into more interesting
2046 // (and more easy to process) wxWidgets events
2047
2048 // first of all, we deal with the state change events only and
2049 // only for valid items (item == -1 for the virtual list
2050 // control)
2051 if ( nmLV->uChanged & LVIF_STATE && iItem != -1 )
2052 {
2053 // temp vars for readability
2054 const UINT stOld = nmLV->uOldState;
2055 const UINT stNew = nmLV->uNewState;
2056
2057 event.m_item.SetId(iItem);
2058 event.m_item.SetMask(wxLIST_MASK_TEXT |
2059 wxLIST_MASK_IMAGE |
2060 wxLIST_MASK_DATA);
2061 GetItem(event.m_item);
2062
2063 // has the focus changed?
2064 if ( !(stOld & LVIS_FOCUSED) && (stNew & LVIS_FOCUSED) )
2065 {
2066 eventType = wxEVT_COMMAND_LIST_ITEM_FOCUSED;
2067 event.m_itemIndex = iItem;
2068 }
2069
2070 if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) )
2071 {
2072 if ( eventType != wxEVT_NULL )
2073 {
2074 // focus and selection have both changed: send the
2075 // focus event from here and the selection one
2076 // below
2077 event.SetEventType(eventType);
2078 (void)GetEventHandler()->ProcessEvent(event);
2079 }
2080 else // no focus event to send
2081 {
2082 // then need to set m_itemIndex as it wasn't done
2083 // above
2084 event.m_itemIndex = iItem;
2085 }
2086
2087 eventType = stNew & LVIS_SELECTED
2088 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2089 : wxEVT_COMMAND_LIST_ITEM_DESELECTED;
2090 }
2091 }
2092
2093 if ( eventType == wxEVT_NULL )
2094 {
2095 // not an interesting event for us
2096 return false;
2097 }
2098
2099 break;
2100
2101 case LVN_KEYDOWN:
2102 {
2103 LV_KEYDOWN *info = (LV_KEYDOWN *)lParam;
2104 WORD wVKey = info->wVKey;
2105
2106 // get the current selection
2107 long lItem = GetNextItem(-1,
2108 wxLIST_NEXT_ALL,
2109 wxLIST_STATE_SELECTED);
2110
2111 // <Enter> or <Space> activate the selected item if any (but
2112 // not with Shift and/or Ctrl as then they have a predefined
2113 // meaning for the list view)
2114 if ( lItem != -1 &&
2115 (wVKey == VK_RETURN || wVKey == VK_SPACE) &&
2116 !(wxIsShiftDown() || wxIsCtrlDown()) )
2117 {
2118 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
2119 }
2120 else
2121 {
2122 eventType = wxEVT_COMMAND_LIST_KEY_DOWN;
2123
2124 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2125 // value which should be used as is
2126 int code = wxCharCodeMSWToWX(wVKey);
2127 event.m_code = code ? code : wVKey;
2128 }
2129
2130 event.m_itemIndex =
2131 event.m_item.m_itemId = lItem;
2132
2133 if ( lItem != -1 )
2134 {
2135 // fill the other fields too
2136 event.m_item.m_text = GetItemText(lItem);
2137 event.m_item.m_data = GetItemData(lItem);
2138 }
2139 }
2140 break;
2141
2142 case NM_DBLCLK:
2143 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2144 // anything else
2145 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
2146 {
2147 return true;
2148 }
2149
2150 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2151 // if it happened on an item (and not on empty place)
2152 if ( iItem == -1 )
2153 {
2154 // not on item
2155 return false;
2156 }
2157
2158 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
2159 event.m_itemIndex = iItem;
2160 event.m_item.m_text = GetItemText(iItem);
2161 event.m_item.m_data = GetItemData(iItem);
2162 break;
2163
2164 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2165 case GN_CONTEXTMENU:
2166 #endif //__WXWINCE__
2167 case NM_RCLICK:
2168 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2169 // don't do anything else
2170 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
2171 {
2172 return true;
2173 }
2174
2175 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2176 LV_HITTESTINFO lvhti;
2177 wxZeroMemory(lvhti);
2178
2179 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2180 if(nmhdr->code == GN_CONTEXTMENU) {
2181 lvhti.pt = ((NMRGINFO*)nmhdr)->ptAction;
2182 } else
2183 #endif //__WXWINCE__
2184 ::GetCursorPos(&(lvhti.pt));
2185 ::ScreenToClient(GetHwnd(),&(lvhti.pt));
2186 if ( ListView_HitTest(GetHwnd(),&lvhti) != -1 )
2187 {
2188 if ( lvhti.flags & LVHT_ONITEM )
2189 {
2190 eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK;
2191 event.m_itemIndex = lvhti.iItem;
2192 event.m_pointDrag.x = lvhti.pt.x;
2193 event.m_pointDrag.y = lvhti.pt.y;
2194 }
2195 }
2196 break;
2197
2198 #ifdef NM_CUSTOMDRAW
2199 case NM_CUSTOMDRAW:
2200 *result = OnCustomDraw(lParam);
2201
2202 return *result != CDRF_DODEFAULT;
2203 #endif // _WIN32_IE >= 0x300
2204
2205 case LVN_ODCACHEHINT:
2206 {
2207 const NM_CACHEHINT *cacheHint = (NM_CACHEHINT *)lParam;
2208
2209 eventType = wxEVT_COMMAND_LIST_CACHE_HINT;
2210
2211 // we get some really stupid cache hints like ones for
2212 // items in range 0..0 for an empty control or, after
2213 // deleting an item, for items in invalid range -- filter
2214 // this garbage out
2215 if ( cacheHint->iFrom > cacheHint->iTo )
2216 return false;
2217
2218 event.m_oldItemIndex = cacheHint->iFrom;
2219
2220 const long iMax = GetItemCount();
2221 event.m_itemIndex = cacheHint->iTo < iMax ? cacheHint->iTo
2222 : iMax - 1;
2223 }
2224 break;
2225
2226 #ifdef HAVE_NMLVFINDITEM
2227 case LVN_ODFINDITEM:
2228 // this message is only used with the virtual list control but
2229 // even there we don't want to always use it: in a control with
2230 // sufficiently big number of items (defined as > 1000 here),
2231 // accidentally pressing a key could result in hanging an
2232 // application waiting while it performs linear search
2233 if ( IsVirtual() && GetItemCount() <= 1000 )
2234 {
2235 NMLVFINDITEM* pFindInfo = (NMLVFINDITEM*)lParam;
2236
2237 // no match by default
2238 *result = -1;
2239
2240 // we only handle string-based searches here
2241 //
2242 // TODO: what about LVFI_PARTIAL, should we handle this?
2243 if ( !(pFindInfo->lvfi.flags & LVFI_STRING) )
2244 {
2245 return false;
2246 }
2247
2248 const wxChar * const searchstr = pFindInfo->lvfi.psz;
2249 const size_t len = wxStrlen(searchstr);
2250
2251 // this is the first item we should examine, search from it
2252 // wrapping if necessary
2253 const int startPos = pFindInfo->iStart;
2254 const int maxPos = GetItemCount();
2255 wxCHECK_MSG( startPos <= maxPos, false,
2256 _T("bad starting position in LVN_ODFINDITEM") );
2257
2258 int currentPos = startPos;
2259 do
2260 {
2261 // wrap to the beginning if necessary
2262 if ( currentPos == maxPos )
2263 {
2264 // somewhat surprizingly, LVFI_WRAP isn't set in
2265 // flags but we still should wrap
2266 currentPos = 0;
2267 }
2268
2269 // does this item begin with searchstr?
2270 if ( wxStrnicmp(searchstr,
2271 GetItemText(currentPos), len) == 0 )
2272 {
2273 *result = currentPos;
2274 break;
2275 }
2276 }
2277 while ( ++currentPos != startPos );
2278
2279 if ( *result == -1 )
2280 {
2281 // not found
2282 return false;
2283 }
2284
2285 SetItemState(*result,
2286 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED,
2287 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
2288 EnsureVisible(*result);
2289 return true;
2290 }
2291 else
2292 {
2293 processed = false;
2294 }
2295 break;
2296 #endif // HAVE_NMLVFINDITEM
2297
2298 case LVN_GETDISPINFO:
2299 if ( IsVirtual() )
2300 {
2301 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
2302
2303 LV_ITEM& lvi = info->item;
2304 long item = lvi.iItem;
2305
2306 if ( lvi.mask & LVIF_TEXT )
2307 {
2308 wxString text = OnGetItemText(item, lvi.iSubItem);
2309 wxStrncpy(lvi.pszText, text, lvi.cchTextMax);
2310 }
2311
2312 // see comment at the end of wxListCtrl::GetColumn()
2313 #ifdef NM_CUSTOMDRAW
2314 if ( lvi.mask & LVIF_IMAGE )
2315 {
2316 lvi.iImage = OnGetItemColumnImage(item, lvi.iSubItem);
2317 }
2318 #endif // NM_CUSTOMDRAW
2319
2320 // a little dose of healthy paranoia: as we never use
2321 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2322 wxASSERT_MSG( !(lvi.mask & LVIF_STATE),
2323 _T("we don't support state callbacks yet!") );
2324
2325 return true;
2326 }
2327 // fall through
2328
2329 default:
2330 processed = false;
2331 }
2332
2333 if ( !processed )
2334 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2335 }
2336 else
2337 {
2338 // where did this one come from?
2339 return false;
2340 }
2341
2342 // process the event
2343 // -----------------
2344
2345 event.SetEventType(eventType);
2346
2347 bool processed = GetEventHandler()->ProcessEvent(event);
2348
2349 // post processing
2350 // ---------------
2351 switch ( nmhdr->code )
2352 {
2353 case LVN_DELETEALLITEMS:
2354 // always return true to suppress all additional LVN_DELETEITEM
2355 // notifications - this makes deleting all items from a list ctrl
2356 // much faster
2357 *result = TRUE;
2358
2359 // also, we may free all user data now (couldn't do it before as
2360 // the user should have access to it in OnDeleteAllItems() handler)
2361 FreeAllInternalData();
2362
2363 // the control is empty now, synchronize the cached number of items
2364 // with the real one
2365 m_count = 0;
2366 return true;
2367
2368 case LVN_ENDLABELEDITA:
2369 case LVN_ENDLABELEDITW:
2370 // logic here is inverted compared to all the other messages
2371 *result = event.IsAllowed();
2372
2373 // don't keep a stale wxTextCtrl around
2374 if ( m_textCtrl )
2375 {
2376 // EDIT control will be deleted by the list control itself so
2377 // prevent us from deleting it as well
2378 m_textCtrl->UnsubclassWin();
2379 m_textCtrl->SetHWND(0);
2380 delete m_textCtrl;
2381 m_textCtrl = NULL;
2382 }
2383
2384 return true;
2385 }
2386
2387 if ( processed )
2388 *result = !event.IsAllowed();
2389
2390 return processed;
2391 }
2392
2393 // ----------------------------------------------------------------------------
2394 // custom draw stuff
2395 // ----------------------------------------------------------------------------
2396
2397 // see comment at the end of wxListCtrl::GetColumn()
2398 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2399
2400 static RECT GetCustomDrawnItemRect(const NMCUSTOMDRAW& nmcd)
2401 {
2402 RECT rc;
2403 ListView_GetItemRect(nmcd.hdr.hwndFrom, nmcd.dwItemSpec, &rc, LVIR_BOUNDS);
2404
2405 RECT rcIcon;
2406 ListView_GetItemRect(nmcd.hdr.hwndFrom, nmcd.dwItemSpec, &rcIcon, LVIR_ICON);
2407
2408 // exclude the icon part, neither the selection background nor focus rect
2409 // should cover it
2410 rc.left = rcIcon.right;
2411
2412 return rc;
2413 }
2414
2415 static void HandleSubItemPrepaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont)
2416 {
2417 NMCUSTOMDRAW& nmcd = pLVCD->nmcd;
2418
2419 HDC hdc = nmcd.hdc;
2420 HWND hwndList = nmcd.hdr.hwndFrom;
2421 const DWORD item = nmcd.dwItemSpec;
2422
2423
2424 // the font must be valid, otherwise we wouldn't be painting the item at all
2425 SelectInHDC selFont(hdc, hfont);
2426
2427 // get the rectangle to paint
2428 RECT rc;
2429 ListView_GetSubItemRect(hwndList, item, pLVCD->iSubItem, LVIR_BOUNDS, &rc);
2430 if ( !pLVCD->iSubItem )
2431 {
2432 // broken ListView_GetSubItemRect() returns the entire item rect for
2433 // 0th subitem while we really need just the part for this column
2434 RECT rc2;
2435 ListView_GetSubItemRect(hwndList, item, 1, LVIR_BOUNDS, &rc2);
2436
2437 rc.right = rc2.left;
2438 rc.left += 4;
2439 }
2440 else // not first subitem
2441 {
2442 rc.left += 6;
2443 }
2444
2445 // get the image and text to draw
2446 wxChar text[512];
2447 LV_ITEM it;
2448 wxZeroMemory(it);
2449 it.mask = LVIF_TEXT | LVIF_IMAGE;
2450 it.iItem = item;
2451 it.iSubItem = pLVCD->iSubItem;
2452 it.pszText = text;
2453 it.cchTextMax = WXSIZEOF(text);
2454 ListView_GetItem(hwndList, &it);
2455
2456 HIMAGELIST himl = ListView_GetImageList(hwndList, LVSIL_SMALL);
2457 if ( himl && ImageList_GetImageCount(himl) )
2458 {
2459 if ( it.iImage != -1 )
2460 {
2461 ImageList_Draw(himl, it.iImage, hdc, rc.left, rc.top,
2462 nmcd.uItemState & CDIS_SELECTED ? ILD_SELECTED
2463 : ILD_TRANSPARENT);
2464 }
2465
2466 // notice that even if this item doesn't have any image, the list
2467 // control still leaves space for the image in the first column if the
2468 // image list is not empty (presumably so that items with and without
2469 // images align?)
2470 if ( it.iImage != -1 || it.iSubItem == 0 )
2471 {
2472 int wImage, hImage;
2473 ImageList_GetIconSize(himl, &wImage, &hImage);
2474
2475 rc.left += wImage + 2;
2476 }
2477 }
2478
2479 ::SetBkMode(hdc, TRANSPARENT);
2480
2481 // TODO: support for centred/right aligned columns
2482 ::DrawText(hdc, text, -1, &rc,
2483 #ifndef __WXWINCE__
2484 DT_WORD_ELLIPSIS |
2485 #endif // __WXWINCE__
2486 DT_NOPREFIX | DT_SINGLELINE | DT_VCENTER);
2487 }
2488
2489 static void HandleItemPostpaint(NMCUSTOMDRAW nmcd)
2490 {
2491 if ( nmcd.uItemState & CDIS_FOCUS )
2492 {
2493 RECT rc = GetCustomDrawnItemRect(nmcd);
2494
2495 // don't use the provided HDC, it's in some strange state by now
2496 ::DrawFocusRect(WindowHDC(nmcd.hdr.hwndFrom), &rc);
2497 }
2498 }
2499
2500 // pLVCD->clrText and clrTextBk should contain the colours to use
2501 static void HandleItemPaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont)
2502 {
2503 NMCUSTOMDRAW& nmcd = pLVCD->nmcd; // just a shortcut
2504
2505 const HWND hwndList = nmcd.hdr.hwndFrom;
2506 const int item = nmcd.dwItemSpec;
2507
2508 // unfortunately we can't trust CDIS_SELECTED, it is often set even when
2509 // the item is not at all selected for some reason (comctl32 6), but we
2510 // also can't always trust ListView_GetItem() as it could return the old
2511 // item status if we're called just after the (de)selection, so remember
2512 // the last item to gain selection and also check for it here
2513 for ( int i = -1;; )
2514 {
2515 i = ListView_GetNextItem(hwndList, i, LVNI_SELECTED);
2516 if ( i == -1 )
2517 {
2518 nmcd.uItemState &= ~CDIS_SELECTED;
2519 break;
2520 }
2521
2522 if ( i == item )
2523 {
2524 nmcd.uItemState |= CDIS_SELECTED;
2525 break;
2526 }
2527 }
2528
2529 // same thing for CDIS_FOCUS (except simpler as there is only one of them)
2530 if ( ::GetFocus() == hwndList &&
2531 ListView_GetNextItem(hwndList, (WPARAM)-1, LVNI_FOCUSED) == item )
2532 {
2533 nmcd.uItemState |= CDIS_FOCUS;
2534 }
2535 else
2536 {
2537 nmcd.uItemState &= ~CDIS_FOCUS;
2538 }
2539
2540 if ( nmcd.uItemState & CDIS_SELECTED )
2541 {
2542 int syscolFg, syscolBg;
2543 if ( ::GetFocus() == hwndList )
2544 {
2545 syscolFg = COLOR_HIGHLIGHTTEXT;
2546 syscolBg = COLOR_HIGHLIGHT;
2547 }
2548 else // selected but unfocused
2549 {
2550 syscolFg = COLOR_WINDOWTEXT;
2551 syscolBg = COLOR_BTNFACE;
2552
2553 // don't grey out the icon in this case neither
2554 nmcd.uItemState &= ~CDIS_SELECTED;
2555 }
2556
2557 pLVCD->clrText = ::GetSysColor(syscolFg);
2558 pLVCD->clrTextBk = ::GetSysColor(syscolBg);
2559 }
2560 //else: not selected, use normal colours from pLVCD
2561
2562 HDC hdc = nmcd.hdc;
2563 RECT rc = GetCustomDrawnItemRect(nmcd);
2564
2565 ::SetTextColor(hdc, pLVCD->clrText);
2566 ::FillRect(hdc, &rc, AutoHBRUSH(pLVCD->clrTextBk));
2567
2568 // we could use CDRF_NOTIFYSUBITEMDRAW here but it results in weird repaint
2569 // problems so just draw everything except the focus rect from here instead
2570 const int colCount = Header_GetItemCount(ListView_GetHeader(hwndList));
2571 for ( int col = 0; col < colCount; col++ )
2572 {
2573 pLVCD->iSubItem = col;
2574 HandleSubItemPrepaint(pLVCD, hfont);
2575 }
2576
2577 HandleItemPostpaint(nmcd);
2578 }
2579
2580 static WXLPARAM HandleItemPrepaint(wxListCtrl *listctrl,
2581 LPNMLVCUSTOMDRAW pLVCD,
2582 wxListItemAttr *attr)
2583 {
2584 if ( !attr )
2585 {
2586 // nothing to do for this item
2587 return CDRF_DODEFAULT;
2588 }
2589
2590
2591 // set the colours to use for text drawing
2592 pLVCD->clrText = attr->HasTextColour()
2593 ? wxColourToRGB(attr->GetTextColour())
2594 : wxColourToRGB(listctrl->GetTextColour());
2595 pLVCD->clrTextBk = attr->HasBackgroundColour()
2596 ? wxColourToRGB(attr->GetBackgroundColour())
2597 : wxColourToRGB(listctrl->GetBackgroundColour());
2598
2599 // select the font if non default one is specified
2600 if ( attr->HasFont() )
2601 {
2602 wxFont font = attr->GetFont();
2603 if ( font.GetEncoding() != wxFONTENCODING_SYSTEM )
2604 {
2605 // the standard control ignores the font encoding/charset, at least
2606 // with recent comctl32.dll versions (5 and 6, it uses to work with
2607 // 4.something) so we have to draw the item entirely ourselves in
2608 // this case
2609 HandleItemPaint(pLVCD, GetHfontOf(font));
2610 return CDRF_SKIPDEFAULT;
2611 }
2612
2613 ::SelectObject(pLVCD->nmcd.hdc, GetHfontOf(font));
2614
2615 return CDRF_NEWFONT;
2616 }
2617
2618 return CDRF_DODEFAULT;
2619 }
2620
2621 WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam)
2622 {
2623 LPNMLVCUSTOMDRAW pLVCD = (LPNMLVCUSTOMDRAW)lParam;
2624 NMCUSTOMDRAW& nmcd = pLVCD->nmcd;
2625 switch ( nmcd.dwDrawStage )
2626 {
2627 case CDDS_PREPAINT:
2628 // if we've got any items with non standard attributes,
2629 // notify us before painting each item
2630 //
2631 // for virtual controls, always suppose that we have attributes as
2632 // there is no way to check for this
2633 if ( IsVirtual() || m_hasAnyAttr )
2634 return CDRF_NOTIFYITEMDRAW;
2635 break;
2636
2637 case CDDS_ITEMPREPAINT:
2638 const int item = nmcd.dwItemSpec;
2639
2640 // we get this message with item == 0 for an empty control, we
2641 // must ignore it as calling OnGetItemAttr() would be wrong
2642 if ( item < 0 || item >= GetItemCount() )
2643 break;
2644
2645 return HandleItemPrepaint(this, pLVCD, DoGetItemAttr(item));
2646 }
2647
2648 return CDRF_DODEFAULT;
2649 }
2650
2651 #endif // NM_CUSTOMDRAW supported
2652
2653 // Necessary for drawing hrules and vrules, if specified
2654 void wxListCtrl::OnPaint(wxPaintEvent& event)
2655 {
2656 bool drawHRules = HasFlag(wxLC_HRULES);
2657 bool drawVRules = HasFlag(wxLC_VRULES);
2658
2659 if (!InReportView() || !drawHRules && !drawVRules)
2660 {
2661 event.Skip();
2662 return;
2663 }
2664
2665 wxPaintDC dc(this);
2666
2667 wxControl::OnPaint(event);
2668
2669 // Reset the device origin since it may have been set
2670 dc.SetDeviceOrigin(0, 0);
2671
2672 wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
2673 dc.SetPen(pen);
2674 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2675
2676 wxSize clientSize = GetClientSize();
2677 wxRect itemRect;
2678
2679 int itemCount = GetItemCount();
2680 int i;
2681 if (drawHRules)
2682 {
2683 long top = GetTopItem();
2684 for (i = top; i < top + GetCountPerPage() + 1; i++)
2685 {
2686 if (GetItemRect(i, itemRect))
2687 {
2688 int cy = itemRect.GetTop();
2689 if (i != 0) // Don't draw the first one
2690 {
2691 dc.DrawLine(0, cy, clientSize.x, cy);
2692 }
2693 // Draw last line
2694 if (i == itemCount - 1)
2695 {
2696 cy = itemRect.GetBottom();
2697 dc.DrawLine(0, cy, clientSize.x, cy);
2698 }
2699 }
2700 }
2701 }
2702 i = itemCount - 1;
2703 if (drawVRules && (i > -1))
2704 {
2705 wxRect firstItemRect;
2706 GetItemRect(0, firstItemRect);
2707
2708 if (GetItemRect(i, itemRect))
2709 {
2710 // this is a fix for bug 673394: erase the pixels which we would
2711 // otherwise leave on the screen
2712 static const int gap = 2;
2713 dc.SetPen(*wxTRANSPARENT_PEN);
2714 dc.SetBrush(wxBrush(GetBackgroundColour()));
2715 dc.DrawRectangle(0, firstItemRect.GetY() - gap,
2716 clientSize.GetWidth(), gap);
2717
2718 dc.SetPen(pen);
2719 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2720 int x = itemRect.GetX();
2721 for (int col = 0; col < GetColumnCount(); col++)
2722 {
2723 int colWidth = GetColumnWidth(col);
2724 x += colWidth ;
2725 dc.DrawLine(x-1, firstItemRect.GetY() - gap,
2726 x-1, itemRect.GetBottom());
2727 }
2728 }
2729 }
2730 }
2731
2732 WXLRESULT
2733 wxListCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2734 {
2735 switch ( nMsg )
2736 {
2737 #ifdef WM_PRINT
2738 case WM_PRINT:
2739 // we should bypass our own WM_PRINT handling as we don't handle
2740 // PRF_CHILDREN flag, so leave it to the native control itself
2741 return MSWDefWindowProc(nMsg, wParam, lParam);
2742 #endif // WM_PRINT
2743
2744 case WM_CONTEXTMENU:
2745 // because this message is propagated upwards the child-parent
2746 // chain, we get it for the right clicks on the header window but
2747 // this is confusing in wx as right clicking there already
2748 // generates a separate wxEVT_COMMAND_LIST_COL_RIGHT_CLICK event
2749 // so just ignore them
2750 if ( (HWND)wParam == ListView_GetHeader(GetHwnd()) )
2751 return 0;
2752 //else: break
2753 }
2754
2755 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
2756 }
2757
2758 // ----------------------------------------------------------------------------
2759 // virtual list controls
2760 // ----------------------------------------------------------------------------
2761
2762 wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2763 {
2764 // this is a pure virtual function, in fact - which is not really pure
2765 // because the controls which are not virtual don't need to implement it
2766 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2767
2768 return wxEmptyString;
2769 }
2770
2771 int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2772 {
2773 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
2774 -1,
2775 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
2776 return -1;
2777 }
2778
2779 int wxListCtrl::OnGetItemColumnImage(long item, long column) const
2780 {
2781 if (!column)
2782 return OnGetItemImage(item);
2783
2784 return -1;
2785 }
2786
2787 wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
2788 {
2789 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
2790 _T("invalid item index in OnGetItemAttr()") );
2791
2792 // no attributes by default
2793 return NULL;
2794 }
2795
2796 wxListItemAttr *wxListCtrl::DoGetItemAttr(long item) const
2797 {
2798 return IsVirtual() ? OnGetItemAttr(item)
2799 : wxGetInternalDataAttr(this, item);
2800 }
2801
2802 void wxListCtrl::SetItemCount(long count)
2803 {
2804 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2805
2806 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count,
2807 LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL) )
2808 {
2809 wxLogLastError(_T("ListView_SetItemCount"));
2810 }
2811 m_count = count;
2812 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
2813 wxT("m_count should match ListView_GetItemCount"));
2814 }
2815
2816 void wxListCtrl::RefreshItem(long item)
2817 {
2818 // strangely enough, ListView_Update() results in much more flicker here
2819 // than a dumb Refresh() -- why?
2820 #if 0
2821 if ( !ListView_Update(GetHwnd(), item) )
2822 {
2823 wxLogLastError(_T("ListView_Update"));
2824 }
2825 #else // 1
2826 wxRect rect;
2827 GetItemRect(item, rect);
2828 RefreshRect(rect);
2829 #endif // 0/1
2830 }
2831
2832 void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
2833 {
2834 wxRect rect1, rect2;
2835 GetItemRect(itemFrom, rect1);
2836 GetItemRect(itemTo, rect2);
2837
2838 wxRect rect = rect1;
2839 rect.height = rect2.GetBottom() - rect1.GetTop();
2840
2841 RefreshRect(rect);
2842 }
2843
2844 // ----------------------------------------------------------------------------
2845 // internal data stuff
2846 // ----------------------------------------------------------------------------
2847
2848 static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId)
2849 {
2850 LV_ITEM it;
2851 it.mask = LVIF_PARAM;
2852 it.iItem = itemId;
2853
2854 if ( !ListView_GetItem(hwnd, &it) )
2855 return NULL;
2856
2857 return (wxListItemInternalData *) it.lParam;
2858 }
2859
2860 static
2861 wxListItemInternalData *wxGetInternalData(const wxListCtrl *ctl, long itemId)
2862 {
2863 return wxGetInternalData(GetHwndOf(ctl), itemId);
2864 }
2865
2866 static
2867 wxListItemAttr *wxGetInternalDataAttr(const wxListCtrl *ctl, long itemId)
2868 {
2869 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
2870
2871 return data ? data->attr : NULL;
2872 }
2873
2874 static void wxDeleteInternalData(wxListCtrl* ctl, long itemId)
2875 {
2876 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
2877 if (data)
2878 {
2879 LV_ITEM item;
2880 memset(&item, 0, sizeof(item));
2881 item.iItem = itemId;
2882 item.mask = LVIF_PARAM;
2883 item.lParam = (LPARAM) 0;
2884 ListView_SetItem((HWND)ctl->GetHWND(), &item);
2885 delete data;
2886 }
2887 }
2888
2889 // ----------------------------------------------------------------------------
2890 // wxWin <-> MSW items conversions
2891 // ----------------------------------------------------------------------------
2892
2893 static void wxConvertFromMSWListItem(HWND hwndListCtrl,
2894 wxListItem& info,
2895 LV_ITEM& lvItem)
2896 {
2897 wxListItemInternalData *internaldata =
2898 (wxListItemInternalData *) lvItem.lParam;
2899
2900 if (internaldata)
2901 info.m_data = internaldata->lParam;
2902
2903 info.m_mask = 0;
2904 info.m_state = 0;
2905 info.m_stateMask = 0;
2906 info.m_itemId = lvItem.iItem;
2907
2908 long oldMask = lvItem.mask;
2909
2910 bool needText = false;
2911 if (hwndListCtrl != 0)
2912 {
2913 if ( lvItem.mask & LVIF_TEXT )
2914 needText = false;
2915 else
2916 needText = true;
2917
2918 if ( needText )
2919 {
2920 lvItem.pszText = new wxChar[513];
2921 lvItem.cchTextMax = 512;
2922 }
2923 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
2924 ::SendMessage(hwndListCtrl, LVM_GETITEM, 0, (LPARAM)& lvItem);
2925 }
2926
2927 if ( lvItem.mask & LVIF_STATE )
2928 {
2929 info.m_mask |= wxLIST_MASK_STATE;
2930
2931 if ( lvItem.stateMask & LVIS_CUT)
2932 {
2933 info.m_stateMask |= wxLIST_STATE_CUT;
2934 if ( lvItem.state & LVIS_CUT )
2935 info.m_state |= wxLIST_STATE_CUT;
2936 }
2937 if ( lvItem.stateMask & LVIS_DROPHILITED)
2938 {
2939 info.m_stateMask |= wxLIST_STATE_DROPHILITED;
2940 if ( lvItem.state & LVIS_DROPHILITED )
2941 info.m_state |= wxLIST_STATE_DROPHILITED;
2942 }
2943 if ( lvItem.stateMask & LVIS_FOCUSED)
2944 {
2945 info.m_stateMask |= wxLIST_STATE_FOCUSED;
2946 if ( lvItem.state & LVIS_FOCUSED )
2947 info.m_state |= wxLIST_STATE_FOCUSED;
2948 }
2949 if ( lvItem.stateMask & LVIS_SELECTED)
2950 {
2951 info.m_stateMask |= wxLIST_STATE_SELECTED;
2952 if ( lvItem.state & LVIS_SELECTED )
2953 info.m_state |= wxLIST_STATE_SELECTED;
2954 }
2955 }
2956
2957 if ( lvItem.mask & LVIF_TEXT )
2958 {
2959 info.m_mask |= wxLIST_MASK_TEXT;
2960 info.m_text = lvItem.pszText;
2961 }
2962 if ( lvItem.mask & LVIF_IMAGE )
2963 {
2964 info.m_mask |= wxLIST_MASK_IMAGE;
2965 info.m_image = lvItem.iImage;
2966 }
2967 if ( lvItem.mask & LVIF_PARAM )
2968 info.m_mask |= wxLIST_MASK_DATA;
2969 if ( lvItem.mask & LVIF_DI_SETITEM )
2970 info.m_mask |= wxLIST_SET_ITEM;
2971 info.m_col = lvItem.iSubItem;
2972
2973 if (needText)
2974 {
2975 if (lvItem.pszText)
2976 delete[] lvItem.pszText;
2977 }
2978 lvItem.mask = oldMask;
2979 }
2980
2981 static void wxConvertToMSWFlags(long state, long stateMask, LV_ITEM& lvItem)
2982 {
2983 if (stateMask & wxLIST_STATE_CUT)
2984 {
2985 lvItem.stateMask |= LVIS_CUT;
2986 if (state & wxLIST_STATE_CUT)
2987 lvItem.state |= LVIS_CUT;
2988 }
2989 if (stateMask & wxLIST_STATE_DROPHILITED)
2990 {
2991 lvItem.stateMask |= LVIS_DROPHILITED;
2992 if (state & wxLIST_STATE_DROPHILITED)
2993 lvItem.state |= LVIS_DROPHILITED;
2994 }
2995 if (stateMask & wxLIST_STATE_FOCUSED)
2996 {
2997 lvItem.stateMask |= LVIS_FOCUSED;
2998 if (state & wxLIST_STATE_FOCUSED)
2999 lvItem.state |= LVIS_FOCUSED;
3000 }
3001 if (stateMask & wxLIST_STATE_SELECTED)
3002 {
3003 lvItem.stateMask |= LVIS_SELECTED;
3004 if (state & wxLIST_STATE_SELECTED)
3005 lvItem.state |= LVIS_SELECTED;
3006 }
3007 }
3008
3009 static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
3010 const wxListItem& info,
3011 LV_ITEM& lvItem)
3012 {
3013 lvItem.iItem = (int) info.m_itemId;
3014
3015 lvItem.iImage = info.m_image;
3016 lvItem.stateMask = 0;
3017 lvItem.state = 0;
3018 lvItem.mask = 0;
3019 lvItem.iSubItem = info.m_col;
3020
3021 if (info.m_mask & wxLIST_MASK_STATE)
3022 {
3023 lvItem.mask |= LVIF_STATE;
3024
3025 wxConvertToMSWFlags(info.m_state, info.m_stateMask, lvItem);
3026 }
3027
3028 if (info.m_mask & wxLIST_MASK_TEXT)
3029 {
3030 lvItem.mask |= LVIF_TEXT;
3031 if ( ctrl->HasFlag(wxLC_USER_TEXT) )
3032 {
3033 lvItem.pszText = LPSTR_TEXTCALLBACK;
3034 }
3035 else
3036 {
3037 // pszText is not const, hence the cast
3038 lvItem.pszText = (wxChar *)info.m_text.c_str();
3039 if ( lvItem.pszText )
3040 lvItem.cchTextMax = info.m_text.length();
3041 else
3042 lvItem.cchTextMax = 0;
3043 }
3044 }
3045 if (info.m_mask & wxLIST_MASK_IMAGE)
3046 lvItem.mask |= LVIF_IMAGE;
3047 }
3048
3049 static void wxConvertToMSWListCol(HWND hwndList,
3050 int col,
3051 const wxListItem& item,
3052 LV_COLUMN& lvCol)
3053 {
3054 wxZeroMemory(lvCol);
3055
3056 if ( item.m_mask & wxLIST_MASK_TEXT )
3057 {
3058 lvCol.mask |= LVCF_TEXT;
3059 lvCol.pszText = (wxChar *)item.m_text.c_str(); // cast is safe
3060 }
3061
3062 if ( item.m_mask & wxLIST_MASK_FORMAT )
3063 {
3064 lvCol.mask |= LVCF_FMT;
3065
3066 if ( item.m_format == wxLIST_FORMAT_LEFT )
3067 lvCol.fmt = LVCFMT_LEFT;
3068 else if ( item.m_format == wxLIST_FORMAT_RIGHT )
3069 lvCol.fmt = LVCFMT_RIGHT;
3070 else if ( item.m_format == wxLIST_FORMAT_CENTRE )
3071 lvCol.fmt = LVCFMT_CENTER;
3072 }
3073
3074 if ( item.m_mask & wxLIST_MASK_WIDTH )
3075 {
3076 lvCol.mask |= LVCF_WIDTH;
3077 if ( item.m_width == wxLIST_AUTOSIZE)
3078 lvCol.cx = LVSCW_AUTOSIZE;
3079 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
3080 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
3081 else
3082 lvCol.cx = item.m_width;
3083 }
3084
3085 // see comment at the end of wxListCtrl::GetColumn()
3086 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
3087 if ( item.m_mask & wxLIST_MASK_IMAGE )
3088 {
3089 if ( wxApp::GetComCtl32Version() >= 470 )
3090 {
3091 lvCol.mask |= LVCF_IMAGE;
3092
3093 // we use LVCFMT_BITMAP_ON_RIGHT because the images on the right
3094 // seem to be generally nicer than on the left and the generic
3095 // version only draws them on the right (we don't have a flag to
3096 // specify the image location anyhow)
3097 //
3098 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
3099 // make any difference in my tests -- but maybe we should?
3100 if ( item.m_image != -1 )
3101 {
3102 // as we're going to overwrite the format field, get its
3103 // current value first -- unless we want to overwrite it anyhow
3104 if ( !(lvCol.mask & LVCF_FMT) )
3105 {
3106 LV_COLUMN lvColOld;
3107 wxZeroMemory(lvColOld);
3108 lvColOld.mask = LVCF_FMT;
3109 if ( ListView_GetColumn(hwndList, col, &lvColOld) )
3110 {
3111 lvCol.fmt = lvColOld.fmt;
3112 }
3113
3114 lvCol.mask |= LVCF_FMT;
3115 }
3116
3117 lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE;
3118 }
3119
3120 lvCol.iImage = item.m_image;
3121 }
3122 //else: it doesn't support item images anyhow
3123 }
3124 #endif // _WIN32_IE >= 0x0300
3125 }
3126
3127 #endif // wxUSE_LISTCTRL