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