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