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