call static functions directly
[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(const 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 return SetItemColumnImage(item, 0, image);
923 }
924
925 // Sets the item image
926 bool wxListCtrl::SetItemColumnImage(long item, long column, int image)
927 {
928 wxListItem info;
929
930 info.m_mask = wxLIST_MASK_IMAGE;
931 info.m_image = image;
932 info.m_itemId = item;
933 info.m_col = column;
934
935 return SetItem(info);
936 }
937
938 // Gets the item text
939 wxString wxListCtrl::GetItemText(long item) const
940 {
941 wxListItem info;
942
943 info.m_mask = wxLIST_MASK_TEXT;
944 info.m_itemId = item;
945
946 if (!GetItem(info))
947 return wxEmptyString;
948 return info.m_text;
949 }
950
951 // Sets the item text
952 void wxListCtrl::SetItemText(long item, const wxString& str)
953 {
954 wxListItem info;
955
956 info.m_mask = wxLIST_MASK_TEXT;
957 info.m_itemId = item;
958 info.m_text = str;
959
960 SetItem(info);
961 }
962
963 // Gets the item data
964 wxUIntPtr wxListCtrl::GetItemData(long item) const
965 {
966 wxListItem info;
967
968 info.m_mask = wxLIST_MASK_DATA;
969 info.m_itemId = item;
970
971 if (!GetItem(info))
972 return 0;
973 return info.m_data;
974 }
975
976 // Sets the item data
977 bool wxListCtrl::SetItemData(long item, long data)
978 {
979 wxListItem info;
980
981 info.m_mask = wxLIST_MASK_DATA;
982 info.m_itemId = item;
983 info.m_data = data;
984
985 return SetItem(info);
986 }
987
988 wxRect wxListCtrl::GetViewRect() const
989 {
990 wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
991 _T("wxListCtrl::GetViewRect() only works in icon mode") );
992
993 RECT rc;
994 if ( !ListView_GetViewRect(GetHwnd(), &rc) )
995 {
996 wxLogDebug(_T("ListView_GetViewRect() failed."));
997
998 wxZeroMemory(rc);
999 }
1000
1001 wxRect rect;
1002 wxCopyRECTToRect(rc, rect);
1003
1004 return rect;
1005 }
1006
1007 // Gets the item rectangle
1008 bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
1009 {
1010 RECT rectWin;
1011
1012 int codeWin;
1013 if ( code == wxLIST_RECT_BOUNDS )
1014 codeWin = LVIR_BOUNDS;
1015 else if ( code == wxLIST_RECT_ICON )
1016 codeWin = LVIR_ICON;
1017 else if ( code == wxLIST_RECT_LABEL )
1018 codeWin = LVIR_LABEL;
1019 else
1020 {
1021 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
1022
1023 codeWin = LVIR_BOUNDS;
1024 }
1025
1026 bool success = ListView_GetItemRect(GetHwnd(), (int) item, &rectWin, codeWin) != 0;
1027
1028 rect.x = rectWin.left;
1029 rect.y = rectWin.top;
1030 rect.width = rectWin.right - rectWin.left;
1031 rect.height = rectWin.bottom - rectWin.top;
1032
1033 return success;
1034 }
1035
1036 // Gets the item position
1037 bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
1038 {
1039 POINT pt;
1040
1041 bool success = (ListView_GetItemPosition(GetHwnd(), (int) item, &pt) != 0);
1042
1043 pos.x = pt.x; pos.y = pt.y;
1044 return success;
1045 }
1046
1047 // Sets the item position.
1048 bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
1049 {
1050 return (ListView_SetItemPosition(GetHwnd(), (int) item, pos.x, pos.y) != 0);
1051 }
1052
1053 // Gets the number of items in the list control
1054 int wxListCtrl::GetItemCount() const
1055 {
1056 return m_count;
1057 }
1058
1059 wxSize wxListCtrl::GetItemSpacing() const
1060 {
1061 const int spacing = ListView_GetItemSpacing(GetHwnd(), (BOOL)HasFlag(wxLC_SMALL_ICON));
1062
1063 return wxSize(LOWORD(spacing), HIWORD(spacing));
1064 }
1065
1066 #if WXWIN_COMPATIBILITY_2_6
1067
1068 int wxListCtrl::GetItemSpacing(bool isSmall) const
1069 {
1070 return ListView_GetItemSpacing(GetHwnd(), (BOOL) isSmall);
1071 }
1072
1073 #endif // WXWIN_COMPATIBILITY_2_6
1074
1075 void wxListCtrl::SetItemTextColour( long item, const wxColour &col )
1076 {
1077 wxListItem info;
1078 info.m_itemId = item;
1079 info.SetTextColour( col );
1080 SetItem( info );
1081 }
1082
1083 wxColour wxListCtrl::GetItemTextColour( long item ) const
1084 {
1085 wxColour col;
1086 wxListItemInternalData *data = wxGetInternalData(this, item);
1087 if ( data && data->attr )
1088 col = data->attr->GetTextColour();
1089
1090 return col;
1091 }
1092
1093 void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
1094 {
1095 wxListItem info;
1096 info.m_itemId = item;
1097 info.SetBackgroundColour( col );
1098 SetItem( info );
1099 }
1100
1101 wxColour wxListCtrl::GetItemBackgroundColour( long item ) const
1102 {
1103 wxColour col;
1104 wxListItemInternalData *data = wxGetInternalData(this, item);
1105 if ( data && data->attr )
1106 col = data->attr->GetBackgroundColour();
1107
1108 return col;
1109 }
1110
1111 void wxListCtrl::SetItemFont( long item, const wxFont &f )
1112 {
1113 wxListItem info;
1114 info.m_itemId = item;
1115 info.SetFont( f );
1116 SetItem( info );
1117 }
1118
1119 wxFont wxListCtrl::GetItemFont( long item ) const
1120 {
1121 wxFont f;
1122 wxListItemInternalData *data = wxGetInternalData(this, item);
1123 if ( data && data->attr )
1124 f = data->attr->GetFont();
1125
1126 return f;
1127 }
1128
1129 // Gets the number of selected items in the list control
1130 int wxListCtrl::GetSelectedItemCount() const
1131 {
1132 return ListView_GetSelectedCount(GetHwnd());
1133 }
1134
1135 // Gets the text colour of the listview
1136 wxColour wxListCtrl::GetTextColour() const
1137 {
1138 COLORREF ref = ListView_GetTextColor(GetHwnd());
1139 wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
1140 return col;
1141 }
1142
1143 // Sets the text colour of the listview
1144 void wxListCtrl::SetTextColour(const wxColour& col)
1145 {
1146 ListView_SetTextColor(GetHwnd(), PALETTERGB(col.Red(), col.Green(), col.Blue()));
1147 }
1148
1149 // Gets the index of the topmost visible item when in
1150 // list or report view
1151 long wxListCtrl::GetTopItem() const
1152 {
1153 return (long) ListView_GetTopIndex(GetHwnd());
1154 }
1155
1156 // Searches for an item, starting from 'item'.
1157 // 'geometry' is one of
1158 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1159 // 'state' is a state bit flag, one or more of
1160 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1161 // item can be -1 to find the first item that matches the
1162 // specified flags.
1163 // Returns the item or -1 if unsuccessful.
1164 long wxListCtrl::GetNextItem(long item, int geom, int state) const
1165 {
1166 long flags = 0;
1167
1168 if ( geom == wxLIST_NEXT_ABOVE )
1169 flags |= LVNI_ABOVE;
1170 if ( geom == wxLIST_NEXT_ALL )
1171 flags |= LVNI_ALL;
1172 if ( geom == wxLIST_NEXT_BELOW )
1173 flags |= LVNI_BELOW;
1174 if ( geom == wxLIST_NEXT_LEFT )
1175 flags |= LVNI_TOLEFT;
1176 if ( geom == wxLIST_NEXT_RIGHT )
1177 flags |= LVNI_TORIGHT;
1178
1179 if ( state & wxLIST_STATE_CUT )
1180 flags |= LVNI_CUT;
1181 if ( state & wxLIST_STATE_DROPHILITED )
1182 flags |= LVNI_DROPHILITED;
1183 if ( state & wxLIST_STATE_FOCUSED )
1184 flags |= LVNI_FOCUSED;
1185 if ( state & wxLIST_STATE_SELECTED )
1186 flags |= LVNI_SELECTED;
1187
1188 return (long) ListView_GetNextItem(GetHwnd(), item, flags);
1189 }
1190
1191
1192 wxImageList *wxListCtrl::GetImageList(int which) const
1193 {
1194 if ( which == wxIMAGE_LIST_NORMAL )
1195 {
1196 return m_imageListNormal;
1197 }
1198 else if ( which == wxIMAGE_LIST_SMALL )
1199 {
1200 return m_imageListSmall;
1201 }
1202 else if ( which == wxIMAGE_LIST_STATE )
1203 {
1204 return m_imageListState;
1205 }
1206 return NULL;
1207 }
1208
1209 void wxListCtrl::SetImageList(wxImageList *imageList, int which)
1210 {
1211 int flags = 0;
1212 if ( which == wxIMAGE_LIST_NORMAL )
1213 {
1214 flags = LVSIL_NORMAL;
1215 if (m_ownsImageListNormal) delete m_imageListNormal;
1216 m_imageListNormal = imageList;
1217 m_ownsImageListNormal = false;
1218 }
1219 else if ( which == wxIMAGE_LIST_SMALL )
1220 {
1221 flags = LVSIL_SMALL;
1222 if (m_ownsImageListSmall) delete m_imageListSmall;
1223 m_imageListSmall = imageList;
1224 m_ownsImageListSmall = false;
1225 }
1226 else if ( which == wxIMAGE_LIST_STATE )
1227 {
1228 flags = LVSIL_STATE;
1229 if (m_ownsImageListState) delete m_imageListState;
1230 m_imageListState = imageList;
1231 m_ownsImageListState = false;
1232 }
1233 (void) ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags);
1234 }
1235
1236 void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
1237 {
1238 SetImageList(imageList, which);
1239 if ( which == wxIMAGE_LIST_NORMAL )
1240 m_ownsImageListNormal = true;
1241 else if ( which == wxIMAGE_LIST_SMALL )
1242 m_ownsImageListSmall = true;
1243 else if ( which == wxIMAGE_LIST_STATE )
1244 m_ownsImageListState = true;
1245 }
1246
1247 // ----------------------------------------------------------------------------
1248 // Operations
1249 // ----------------------------------------------------------------------------
1250
1251 // Arranges the items
1252 bool wxListCtrl::Arrange(int flag)
1253 {
1254 UINT code = 0;
1255 if ( flag == wxLIST_ALIGN_LEFT )
1256 code = LVA_ALIGNLEFT;
1257 else if ( flag == wxLIST_ALIGN_TOP )
1258 code = LVA_ALIGNTOP;
1259 else if ( flag == wxLIST_ALIGN_DEFAULT )
1260 code = LVA_DEFAULT;
1261 else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID )
1262 code = LVA_SNAPTOGRID;
1263
1264 return (ListView_Arrange(GetHwnd(), code) != 0);
1265 }
1266
1267 // Deletes an item
1268 bool wxListCtrl::DeleteItem(long item)
1269 {
1270 if ( !ListView_DeleteItem(GetHwnd(), (int) item) )
1271 {
1272 wxLogLastError(_T("ListView_DeleteItem"));
1273 return false;
1274 }
1275
1276 m_count--;
1277 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1278 wxT("m_count should match ListView_GetItemCount"));
1279
1280 // the virtual list control doesn't refresh itself correctly, help it
1281 if ( IsVirtual() )
1282 {
1283 // we need to refresh all the lines below the one which was deleted
1284 wxRect rectItem;
1285 if ( item > 0 && GetItemCount() )
1286 {
1287 GetItemRect(item - 1, rectItem);
1288 }
1289 else
1290 {
1291 rectItem.y =
1292 rectItem.height = 0;
1293 }
1294
1295 wxRect rectWin = GetRect();
1296 rectWin.height = rectWin.GetBottom() - rectItem.GetBottom();
1297 rectWin.y = rectItem.GetBottom();
1298
1299 RefreshRect(rectWin);
1300 }
1301
1302 return true;
1303 }
1304
1305 // Deletes all items
1306 bool wxListCtrl::DeleteAllItems()
1307 {
1308 return ListView_DeleteAllItems(GetHwnd()) != 0;
1309 }
1310
1311 // Deletes all items
1312 bool wxListCtrl::DeleteAllColumns()
1313 {
1314 while ( m_colCount > 0 )
1315 {
1316 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1317 {
1318 wxLogLastError(wxT("ListView_DeleteColumn"));
1319
1320 return false;
1321 }
1322
1323 m_colCount--;
1324 }
1325
1326 wxASSERT_MSG( m_colCount == 0, wxT("no columns should be left") );
1327
1328 return true;
1329 }
1330
1331 // Deletes a column
1332 bool wxListCtrl::DeleteColumn(int col)
1333 {
1334 bool success = (ListView_DeleteColumn(GetHwnd(), col) != 0);
1335
1336 if ( success && (m_colCount > 0) )
1337 m_colCount --;
1338 return success;
1339 }
1340
1341 // Clears items, and columns if there are any.
1342 void wxListCtrl::ClearAll()
1343 {
1344 DeleteAllItems();
1345 if ( m_colCount > 0 )
1346 DeleteAllColumns();
1347 }
1348
1349 wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1350 {
1351 wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) );
1352
1353 // ListView_EditLabel requires that the list has focus.
1354 SetFocus();
1355
1356 WXHWND hWnd = (WXHWND) ListView_EditLabel(GetHwnd(), item);
1357 if ( !hWnd )
1358 {
1359 // failed to start editing
1360 return NULL;
1361 }
1362
1363 // [re]create the text control wrapping the HWND we got
1364 if ( m_textCtrl )
1365 {
1366 m_textCtrl->UnsubclassWin();
1367 m_textCtrl->SetHWND(0);
1368 delete m_textCtrl;
1369 }
1370
1371 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1372 m_textCtrl->SetHWND(hWnd);
1373 m_textCtrl->SubclassWin(hWnd);
1374 m_textCtrl->SetParent(this);
1375
1376 // we must disallow TABbing away from the control while the edit contol is
1377 // shown because this leaves it in some strange state (just try removing
1378 // this line and then pressing TAB while editing an item in listctrl
1379 // inside a panel)
1380 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | wxTE_PROCESS_TAB);
1381
1382 return m_textCtrl;
1383 }
1384
1385 // End label editing, optionally cancelling the edit
1386 bool wxListCtrl::EndEditLabel(bool cancel)
1387 {
1388 // m_textCtrl is not always ready, ie. in EVT_LIST_BEGIN_LABEL_EDIT
1389 HWND hwnd = ListView_GetEditControl(GetHwnd());
1390 bool b = (hwnd != NULL);
1391 if (b)
1392 {
1393 if (cancel)
1394 ::SetWindowText(hwnd, wxEmptyString); // dubious but better than nothing
1395 if (m_textCtrl)
1396 {
1397 m_textCtrl->UnsubclassWin();
1398 m_textCtrl->SetHWND(0);
1399 delete m_textCtrl;
1400 m_textCtrl = NULL;
1401 }
1402 ::DestroyWindow(hwnd);
1403 }
1404 return b;
1405 }
1406
1407 // Ensures this item is visible
1408 bool wxListCtrl::EnsureVisible(long item)
1409 {
1410 return ListView_EnsureVisible(GetHwnd(), (int) item, FALSE) != FALSE;
1411 }
1412
1413 // Find an item whose label matches this string, starting from the item after 'start'
1414 // or the beginning if 'start' is -1.
1415 long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
1416 {
1417 LV_FINDINFO findInfo;
1418
1419 findInfo.flags = LVFI_STRING;
1420 if ( partial )
1421 findInfo.flags |= LVFI_PARTIAL;
1422 findInfo.psz = str;
1423
1424 // ListView_FindItem() excludes the first item from search and to look
1425 // through all the items you need to start from -1 which is unnatural and
1426 // inconsistent with the generic version - so we adjust the index
1427 if (start != -1)
1428 start --;
1429 return ListView_FindItem(GetHwnd(), (int) start, &findInfo);
1430 }
1431
1432 // Find an item whose data matches this data, starting from the item after 'start'
1433 // or the beginning if 'start' is -1.
1434 // NOTE : Lindsay Mathieson - 14-July-2002
1435 // No longer use ListView_FindItem as the data attribute is now stored
1436 // in a wxListItemInternalData structure refernced by the actual lParam
1437 long wxListCtrl::FindItem(long start, wxUIntPtr data)
1438 {
1439 long idx = start + 1;
1440 long count = GetItemCount();
1441
1442 while (idx < count)
1443 {
1444 if (GetItemData(idx) == data)
1445 return idx;
1446 idx++;
1447 };
1448
1449 return -1;
1450 }
1451
1452 // Find an item nearest this position in the specified direction, starting from
1453 // the item after 'start' or the beginning if 'start' is -1.
1454 long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
1455 {
1456 LV_FINDINFO findInfo;
1457
1458 findInfo.flags = LVFI_NEARESTXY;
1459 findInfo.pt.x = pt.x;
1460 findInfo.pt.y = pt.y;
1461 findInfo.vkDirection = VK_RIGHT;
1462
1463 if ( direction == wxLIST_FIND_UP )
1464 findInfo.vkDirection = VK_UP;
1465 else if ( direction == wxLIST_FIND_DOWN )
1466 findInfo.vkDirection = VK_DOWN;
1467 else if ( direction == wxLIST_FIND_LEFT )
1468 findInfo.vkDirection = VK_LEFT;
1469 else if ( direction == wxLIST_FIND_RIGHT )
1470 findInfo.vkDirection = VK_RIGHT;
1471
1472 return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
1473 }
1474
1475 // Determines which item (if any) is at the specified point,
1476 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1477 long wxListCtrl::HitTest(const wxPoint& point, int& flags)
1478 {
1479 LV_HITTESTINFO hitTestInfo;
1480 hitTestInfo.pt.x = (int) point.x;
1481 hitTestInfo.pt.y = (int) point.y;
1482
1483 ListView_HitTest(GetHwnd(), & hitTestInfo);
1484
1485 flags = 0;
1486
1487 if ( hitTestInfo.flags & LVHT_ABOVE )
1488 flags |= wxLIST_HITTEST_ABOVE;
1489 if ( hitTestInfo.flags & LVHT_BELOW )
1490 flags |= wxLIST_HITTEST_BELOW;
1491 if ( hitTestInfo.flags & LVHT_TOLEFT )
1492 flags |= wxLIST_HITTEST_TOLEFT;
1493 if ( hitTestInfo.flags & LVHT_TORIGHT )
1494 flags |= wxLIST_HITTEST_TORIGHT;
1495
1496 if ( hitTestInfo.flags & LVHT_NOWHERE )
1497 flags |= wxLIST_HITTEST_NOWHERE;
1498
1499 // note a bug or at least a very strange feature of comtl32.dll (tested
1500 // with version 4.0 under Win95 and 6.0 under Win 2003): if you click to
1501 // the right of the item label, ListView_HitTest() returns a combination of
1502 // LVHT_ONITEMICON, LVHT_ONITEMLABEL and LVHT_ONITEMSTATEICON -- filter out
1503 // the bits which don't make sense
1504 if ( hitTestInfo.flags & LVHT_ONITEMLABEL )
1505 {
1506 flags |= wxLIST_HITTEST_ONITEMLABEL;
1507
1508 // do not translate LVHT_ONITEMICON here, as per above
1509 }
1510 else
1511 {
1512 if ( hitTestInfo.flags & LVHT_ONITEMICON )
1513 flags |= wxLIST_HITTEST_ONITEMICON;
1514 if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON )
1515 flags |= wxLIST_HITTEST_ONITEMSTATEICON;
1516 }
1517
1518 return (long) hitTestInfo.iItem;
1519 }
1520
1521 // Inserts an item, returning the index of the new item if successful,
1522 // -1 otherwise.
1523 long wxListCtrl::InsertItem(const wxListItem& info)
1524 {
1525 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1526
1527 LV_ITEM item;
1528 wxConvertToMSWListItem(this, info, item);
1529 item.mask &= ~LVIF_PARAM;
1530
1531 // check wether we need to allocate our internal data
1532 bool needInternalData = ((info.m_mask & wxLIST_MASK_DATA) || info.HasAttributes());
1533 if (needInternalData)
1534 {
1535 m_AnyInternalData = true;
1536 item.mask |= LVIF_PARAM;
1537
1538 // internal stucture that manages data
1539 wxListItemInternalData *data = new wxListItemInternalData();
1540 item.lParam = (LPARAM) data;
1541
1542 if (info.m_mask & wxLIST_MASK_DATA)
1543 data->lParam = info.m_data;
1544
1545 // check whether it has any custom attributes
1546 if ( info.HasAttributes() )
1547 {
1548 // take copy of attributes
1549 data->attr = new wxListItemAttr(*info.GetAttributes());
1550
1551 // and remember that we have some now...
1552 m_hasAnyAttr = true;
1553 }
1554 };
1555
1556 long rv = ListView_InsertItem(GetHwnd(), & item);
1557
1558 m_count++;
1559 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1560 wxT("m_count should match ListView_GetItemCount"));
1561
1562 return rv;
1563 }
1564
1565 long wxListCtrl::InsertItem(long index, const wxString& label)
1566 {
1567 wxListItem info;
1568 info.m_text = label;
1569 info.m_mask = wxLIST_MASK_TEXT;
1570 info.m_itemId = index;
1571 return InsertItem(info);
1572 }
1573
1574 // Inserts an image item
1575 long wxListCtrl::InsertItem(long index, int imageIndex)
1576 {
1577 wxListItem info;
1578 info.m_image = imageIndex;
1579 info.m_mask = wxLIST_MASK_IMAGE;
1580 info.m_itemId = index;
1581 return InsertItem(info);
1582 }
1583
1584 // Inserts an image/string item
1585 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1586 {
1587 wxListItem info;
1588 info.m_image = imageIndex;
1589 info.m_text = label;
1590 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1591 info.m_itemId = index;
1592 return InsertItem(info);
1593 }
1594
1595 // For list view mode (only), inserts a column.
1596 long wxListCtrl::InsertColumn(long col, const wxListItem& item)
1597 {
1598 LV_COLUMN lvCol;
1599 wxConvertToMSWListCol(GetHwnd(), col, item, lvCol);
1600
1601 if ( !(lvCol.mask & LVCF_WIDTH) )
1602 {
1603 // always give some width to the new column: this one is compatible
1604 // with the generic version
1605 lvCol.mask |= LVCF_WIDTH;
1606 lvCol.cx = 80;
1607 }
1608
1609 long n = ListView_InsertColumn(GetHwnd(), col, &lvCol);
1610 if ( n != -1 )
1611 {
1612 m_colCount++;
1613 }
1614 else // failed to insert?
1615 {
1616 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1617 lvCol.pszText);
1618 }
1619
1620 return n;
1621 }
1622
1623 long wxListCtrl::InsertColumn(long col,
1624 const wxString& heading,
1625 int format,
1626 int width)
1627 {
1628 wxListItem item;
1629 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1630 item.m_text = heading;
1631 if ( width > -1 )
1632 {
1633 item.m_mask |= wxLIST_MASK_WIDTH;
1634 item.m_width = width;
1635 }
1636 item.m_format = format;
1637
1638 return InsertColumn(col, item);
1639 }
1640
1641 // scroll the control by the given number of pixels (exception: in list view,
1642 // dx is interpreted as number of columns)
1643 bool wxListCtrl::ScrollList(int dx, int dy)
1644 {
1645 if ( !ListView_Scroll(GetHwnd(), dx, dy) )
1646 {
1647 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx, dy);
1648
1649 return false;
1650 }
1651
1652 return true;
1653 }
1654
1655 // Sort items.
1656
1657 // fn is a function which takes 3 long arguments: item1, item2, data.
1658 // item1 is the long data associated with a first item (NOT the index).
1659 // item2 is the long data associated with a second item (NOT the index).
1660 // data is the same value as passed to SortItems.
1661 // The return value is a negative number if the first item should precede the second
1662 // item, a positive number of the second item should precede the first,
1663 // or zero if the two items are equivalent.
1664
1665 // data is arbitrary data to be passed to the sort function.
1666
1667 // Internal structures for proxying the user compare function
1668 // so that we can pass it the *real* user data
1669
1670 // translate lParam data and call user func
1671 struct wxInternalDataSort
1672 {
1673 wxListCtrlCompare user_fn;
1674 long data;
1675 };
1676
1677 int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
1678 {
1679 struct wxInternalDataSort *internalData = (struct wxInternalDataSort *) lParamSort;
1680
1681 wxListItemInternalData *data1 = (wxListItemInternalData *) lParam1;
1682 wxListItemInternalData *data2 = (wxListItemInternalData *) lParam2;
1683
1684 long d1 = (data1 == NULL ? 0 : data1->lParam);
1685 long d2 = (data2 == NULL ? 0 : data2->lParam);
1686
1687 return internalData->user_fn(d1, d2, internalData->data);
1688
1689 }
1690
1691 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1692 {
1693 struct wxInternalDataSort internalData;
1694 internalData.user_fn = fn;
1695 internalData.data = data;
1696
1697 // WPARAM cast is needed for mingw/cygwin
1698 if ( !ListView_SortItems(GetHwnd(),
1699 wxInternalDataCompareFunc,
1700 (WPARAM) &internalData) )
1701 {
1702 wxLogDebug(_T("ListView_SortItems() failed"));
1703
1704 return false;
1705 }
1706
1707 return true;
1708 }
1709
1710
1711
1712 // ----------------------------------------------------------------------------
1713 // message processing
1714 // ----------------------------------------------------------------------------
1715
1716 bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1717 {
1718 if (cmd == EN_UPDATE)
1719 {
1720 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1721 event.SetEventObject( this );
1722 ProcessCommand(event);
1723 return true;
1724 }
1725 else if (cmd == EN_KILLFOCUS)
1726 {
1727 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1728 event.SetEventObject( this );
1729 ProcessCommand(event);
1730 return true;
1731 }
1732 else
1733 return false;
1734 }
1735
1736 bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1737 {
1738
1739 // prepare the event
1740 // -----------------
1741
1742 wxListEvent event(wxEVT_NULL, m_windowId);
1743 event.SetEventObject(this);
1744
1745 wxEventType eventType = wxEVT_NULL;
1746
1747 NMHDR *nmhdr = (NMHDR *)lParam;
1748
1749 // if your compiler is as broken as this, you should really change it: this
1750 // code is needed for normal operation! #ifdef below is only useful for
1751 // automatic rebuilds which are done with a very old compiler version
1752 #ifdef HDN_BEGINTRACKA
1753
1754 // check for messages from the header (in report view)
1755 HWND hwndHdr = ListView_GetHeader(GetHwnd());
1756
1757 // is it a message from the header?
1758 if ( nmhdr->hwndFrom == hwndHdr )
1759 {
1760 HD_NOTIFY *nmHDR = (HD_NOTIFY *)nmhdr;
1761
1762 event.m_itemIndex = -1;
1763
1764 switch ( nmhdr->code )
1765 {
1766 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1767 // TRACK messages even to ANSI programs: on my system I get
1768 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1769 //
1770 // work around is to simply catch both versions and hope that it
1771 // works (why should this message exist in ANSI and Unicode is
1772 // beyond me as it doesn't deal with strings at all...)
1773 //
1774 // note that fr HDN_TRACK another possibility could be to use
1775 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1776 // something other than the item width changes so we'd have to
1777 // filter out the unwanted events then
1778 case HDN_BEGINTRACKA:
1779 case HDN_BEGINTRACKW:
1780 eventType = wxEVT_COMMAND_LIST_COL_BEGIN_DRAG;
1781 // fall through
1782
1783 case HDN_TRACKA:
1784 case HDN_TRACKW:
1785 if ( eventType == wxEVT_NULL )
1786 eventType = wxEVT_COMMAND_LIST_COL_DRAGGING;
1787 // fall through
1788
1789 case HDN_ENDTRACKA:
1790 case HDN_ENDTRACKW:
1791 if ( eventType == wxEVT_NULL )
1792 eventType = wxEVT_COMMAND_LIST_COL_END_DRAG;
1793
1794 event.m_item.m_width = nmHDR->pitem->cxy;
1795 event.m_col = nmHDR->iItem;
1796 break;
1797
1798 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1799 case GN_CONTEXTMENU:
1800 #endif //__WXWINCE__
1801 case NM_RCLICK:
1802 {
1803 eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK;
1804 event.m_col = -1;
1805
1806 // find the column clicked: we have to search for it
1807 // ourselves as the notification message doesn't provide
1808 // this info
1809
1810 // where did the click occur?
1811 POINT ptClick;
1812 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
1813 if(nmhdr->code == GN_CONTEXTMENU) {
1814 ptClick = ((NMRGINFO*)nmhdr)->ptAction;
1815 } else
1816 #endif //__WXWINCE__
1817 if ( !::GetCursorPos(&ptClick) )
1818 {
1819 wxLogLastError(_T("GetCursorPos"));
1820 }
1821
1822 if ( !::ScreenToClient(GetHwnd(), &ptClick) )
1823 {
1824 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1825 }
1826
1827 event.m_pointDrag.x = ptClick.x;
1828 event.m_pointDrag.y = ptClick.y;
1829
1830 int colCount = Header_GetItemCount(hwndHdr);
1831
1832 RECT rect;
1833 for ( int col = 0; col < colCount; col++ )
1834 {
1835 if ( Header_GetItemRect(hwndHdr, col, &rect) )
1836 {
1837 if ( ::PtInRect(&rect, ptClick) )
1838 {
1839 event.m_col = col;
1840 break;
1841 }
1842 }
1843 }
1844 }
1845 break;
1846
1847 case HDN_GETDISPINFOW:
1848 // letting Windows XP handle this message results in mysterious
1849 // crashes in comctl32.dll seemingly because of bad message
1850 // parameters
1851 //
1852 // I have no idea what is the real cause of the bug (which is,
1853 // just to make things interesting, is impossible to reproduce
1854 // reliably) but ignoring all these messages does fix it and
1855 // doesn't seem to have any negative consequences
1856 return true;
1857
1858 default:
1859 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1860 }
1861 }
1862 else
1863 #endif // defined(HDN_BEGINTRACKA)
1864 if ( nmhdr->hwndFrom == GetHwnd() )
1865 {
1866 // almost all messages use NM_LISTVIEW
1867 NM_LISTVIEW *nmLV = (NM_LISTVIEW *)nmhdr;
1868
1869 const int iItem = nmLV->iItem;
1870
1871
1872 // FreeAllInternalData will cause LVN_ITEMCHANG* messages, which can be
1873 // ignored for efficiency. It is done here because the internal data is in the
1874 // process of being deleted so we don't want to try and access it below.
1875 if ( m_ignoreChangeMessages &&
1876 ( (nmLV->hdr.code == LVN_ITEMCHANGED) ||
1877 (nmLV->hdr.code == LVN_ITEMCHANGING)) )
1878 {
1879 return true;
1880 }
1881
1882
1883 // If we have a valid item then check if there is a data value
1884 // associated with it and put it in the event.
1885 if ( iItem >= 0 && iItem < GetItemCount() )
1886 {
1887 wxListItemInternalData *internaldata =
1888 wxGetInternalData(GetHwnd(), iItem);
1889
1890 if ( internaldata )
1891 event.m_item.m_data = internaldata->lParam;
1892 }
1893
1894 bool processed = true;
1895 switch ( nmhdr->code )
1896 {
1897 case LVN_BEGINRDRAG:
1898 eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
1899 // fall through
1900
1901 case LVN_BEGINDRAG:
1902 if ( eventType == wxEVT_NULL )
1903 {
1904 eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG;
1905 }
1906
1907 event.m_itemIndex = iItem;
1908 event.m_pointDrag.x = nmLV->ptAction.x;
1909 event.m_pointDrag.y = nmLV->ptAction.y;
1910 break;
1911
1912 // NB: we have to handle both *A and *W versions here because some
1913 // versions of comctl32.dll send ANSI messages even to the
1914 // Unicode windows
1915 case LVN_BEGINLABELEDITA:
1916 case LVN_BEGINLABELEDITW:
1917 {
1918 wxLV_ITEM item;
1919 if ( nmhdr->code == LVN_BEGINLABELEDITA )
1920 {
1921 item.Init(((LV_DISPINFOA *)lParam)->item);
1922 }
1923 else // LVN_BEGINLABELEDITW
1924 {
1925 item.Init(((LV_DISPINFOW *)lParam)->item);
1926 }
1927
1928 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
1929 wxConvertFromMSWListItem(GetHwnd(), event.m_item, item);
1930 event.m_itemIndex = event.m_item.m_itemId;
1931 }
1932 break;
1933
1934 case LVN_ENDLABELEDITA:
1935 case LVN_ENDLABELEDITW:
1936 {
1937 wxLV_ITEM item;
1938 if ( nmhdr->code == LVN_ENDLABELEDITA )
1939 {
1940 item.Init(((LV_DISPINFOA *)lParam)->item);
1941 }
1942 else // LVN_ENDLABELEDITW
1943 {
1944 item.Init(((LV_DISPINFOW *)lParam)->item);
1945 }
1946
1947 // was editing cancelled?
1948 const LV_ITEM& lvi = (LV_ITEM)item;
1949 if ( !lvi.pszText || lvi.iItem == -1 )
1950 {
1951 // don't keep a stale wxTextCtrl around
1952 if ( m_textCtrl )
1953 {
1954 // EDIT control will be deleted by the list control itself so
1955 // prevent us from deleting it as well
1956 m_textCtrl->UnsubclassWin();
1957 m_textCtrl->SetHWND(0);
1958 delete m_textCtrl;
1959 m_textCtrl = NULL;
1960 }
1961
1962 event.SetEditCanceled(true);
1963 }
1964
1965 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
1966 wxConvertFromMSWListItem(NULL, event.m_item, item);
1967 event.m_itemIndex = event.m_item.m_itemId;
1968 }
1969 break;
1970
1971 case LVN_COLUMNCLICK:
1972 eventType = wxEVT_COMMAND_LIST_COL_CLICK;
1973 event.m_itemIndex = -1;
1974 event.m_col = nmLV->iSubItem;
1975 break;
1976
1977 case LVN_DELETEALLITEMS:
1978 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
1979 event.m_itemIndex = -1;
1980 break;
1981
1982 case LVN_DELETEITEM:
1983 if ( m_count == 0 )
1984 {
1985 // this should be prevented by the post-processing code
1986 // below, but "just in case"
1987 return false;
1988 }
1989
1990 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
1991 event.m_itemIndex = iItem;
1992 // delete the assoicated internal data
1993 wxDeleteInternalData(this, iItem);
1994 break;
1995
1996 #if WXWIN_COMPATIBILITY_2_4
1997 case LVN_SETDISPINFO:
1998 {
1999 eventType = wxEVT_COMMAND_LIST_SET_INFO;
2000 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
2001 wxConvertFromMSWListItem(GetHwnd(), event.m_item, info->item);
2002 }
2003 break;
2004 #endif
2005
2006 case LVN_INSERTITEM:
2007 eventType = wxEVT_COMMAND_LIST_INSERT_ITEM;
2008 event.m_itemIndex = iItem;
2009 break;
2010
2011 case LVN_ITEMCHANGED:
2012 // we translate this catch all message into more interesting
2013 // (and more easy to process) wxWidgets events
2014
2015 // first of all, we deal with the state change events only and
2016 // only for valid items (item == -1 for the virtual list
2017 // control)
2018 if ( nmLV->uChanged & LVIF_STATE && iItem != -1 )
2019 {
2020 // temp vars for readability
2021 const UINT stOld = nmLV->uOldState;
2022 const UINT stNew = nmLV->uNewState;
2023
2024 event.m_item.SetId(iItem);
2025 event.m_item.SetMask(wxLIST_MASK_TEXT |
2026 wxLIST_MASK_IMAGE |
2027 wxLIST_MASK_DATA);
2028 GetItem(event.m_item);
2029
2030 // has the focus changed?
2031 if ( !(stOld & LVIS_FOCUSED) && (stNew & LVIS_FOCUSED) )
2032 {
2033 eventType = wxEVT_COMMAND_LIST_ITEM_FOCUSED;
2034 event.m_itemIndex = iItem;
2035 }
2036
2037 if ( (stNew & LVIS_SELECTED) != (stOld & LVIS_SELECTED) )
2038 {
2039 if ( eventType != wxEVT_NULL )
2040 {
2041 // focus and selection have both changed: send the
2042 // focus event from here and the selection one
2043 // below
2044 event.SetEventType(eventType);
2045 (void)GetEventHandler()->ProcessEvent(event);
2046 }
2047 else // no focus event to send
2048 {
2049 // then need to set m_itemIndex as it wasn't done
2050 // above
2051 event.m_itemIndex = iItem;
2052 }
2053
2054 eventType = stNew & LVIS_SELECTED
2055 ? wxEVT_COMMAND_LIST_ITEM_SELECTED
2056 : wxEVT_COMMAND_LIST_ITEM_DESELECTED;
2057 }
2058 }
2059
2060 if ( eventType == wxEVT_NULL )
2061 {
2062 // not an interesting event for us
2063 return false;
2064 }
2065
2066 break;
2067
2068 case LVN_KEYDOWN:
2069 {
2070 LV_KEYDOWN *info = (LV_KEYDOWN *)lParam;
2071 WORD wVKey = info->wVKey;
2072
2073 // get the current selection
2074 long lItem = GetNextItem(-1,
2075 wxLIST_NEXT_ALL,
2076 wxLIST_STATE_SELECTED);
2077
2078 // <Enter> or <Space> activate the selected item if any (but
2079 // not with Shift and/or Ctrl as then they have a predefined
2080 // meaning for the list view)
2081 if ( lItem != -1 &&
2082 (wVKey == VK_RETURN || wVKey == VK_SPACE) &&
2083 !(wxIsShiftDown() || wxIsCtrlDown()) )
2084 {
2085 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
2086 }
2087 else
2088 {
2089 eventType = wxEVT_COMMAND_LIST_KEY_DOWN;
2090
2091 // wxCharCodeMSWToWX() returns 0 if the key is an ASCII
2092 // value which should be used as is
2093 int code = wxCharCodeMSWToWX(wVKey);
2094 event.m_code = code ? code : wVKey;
2095 }
2096
2097 event.m_itemIndex =
2098 event.m_item.m_itemId = lItem;
2099
2100 if ( lItem != -1 )
2101 {
2102 // fill the other fields too
2103 event.m_item.m_text = GetItemText(lItem);
2104 event.m_item.m_data = GetItemData(lItem);
2105 }
2106 }
2107 break;
2108
2109 case NM_DBLCLK:
2110 // if the user processes it in wxEVT_COMMAND_LEFT_CLICK(), don't do
2111 // anything else
2112 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
2113 {
2114 return true;
2115 }
2116
2117 // else translate it into wxEVT_COMMAND_LIST_ITEM_ACTIVATED event
2118 // if it happened on an item (and not on empty place)
2119 if ( iItem == -1 )
2120 {
2121 // not on item
2122 return false;
2123 }
2124
2125 eventType = wxEVT_COMMAND_LIST_ITEM_ACTIVATED;
2126 event.m_itemIndex = iItem;
2127 event.m_item.m_text = GetItemText(iItem);
2128 event.m_item.m_data = GetItemData(iItem);
2129 break;
2130
2131 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2132 case GN_CONTEXTMENU:
2133 #endif //__WXWINCE__
2134 case NM_RCLICK:
2135 // if the user processes it in wxEVT_COMMAND_RIGHT_CLICK(),
2136 // don't do anything else
2137 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
2138 {
2139 return true;
2140 }
2141
2142 // else translate it into wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK event
2143 LV_HITTESTINFO lvhti;
2144 wxZeroMemory(lvhti);
2145
2146 #if defined(__WXWINCE__) && !defined(__HANDHELDPC__) && _WIN32_WCE < 400
2147 if(nmhdr->code == GN_CONTEXTMENU) {
2148 lvhti.pt = ((NMRGINFO*)nmhdr)->ptAction;
2149 } else
2150 #endif //__WXWINCE__
2151 ::GetCursorPos(&(lvhti.pt));
2152 ::ScreenToClient(GetHwnd(),&(lvhti.pt));
2153 if ( ListView_HitTest(GetHwnd(),&lvhti) != -1 )
2154 {
2155 if ( lvhti.flags & LVHT_ONITEM )
2156 {
2157 eventType = wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK;
2158 event.m_itemIndex = lvhti.iItem;
2159 event.m_pointDrag.x = lvhti.pt.x;
2160 event.m_pointDrag.y = lvhti.pt.y;
2161 }
2162 }
2163 break;
2164
2165 #ifdef NM_CUSTOMDRAW
2166 case NM_CUSTOMDRAW:
2167 *result = OnCustomDraw(lParam);
2168
2169 return *result != CDRF_DODEFAULT;
2170 #endif // _WIN32_IE >= 0x300
2171
2172 case LVN_ODCACHEHINT:
2173 {
2174 const NM_CACHEHINT *cacheHint = (NM_CACHEHINT *)lParam;
2175
2176 eventType = wxEVT_COMMAND_LIST_CACHE_HINT;
2177
2178 // we get some really stupid cache hints like ones for
2179 // items in range 0..0 for an empty control or, after
2180 // deleting an item, for items in invalid range -- filter
2181 // this garbage out
2182 if ( cacheHint->iFrom > cacheHint->iTo )
2183 return false;
2184
2185 event.m_oldItemIndex = cacheHint->iFrom;
2186
2187 const long iMax = GetItemCount();
2188 event.m_itemIndex = cacheHint->iTo < iMax ? cacheHint->iTo
2189 : iMax - 1;
2190 }
2191 break;
2192
2193 #ifdef HAVE_NMLVFINDITEM
2194 case LVN_ODFINDITEM:
2195 // this message is only used with the virtual list control but
2196 // even there we don't want to always use it: in a control with
2197 // sufficiently big number of items (defined as > 1000 here),
2198 // accidentally pressing a key could result in hanging an
2199 // application waiting while it performs linear search
2200 if ( IsVirtual() && GetItemCount() <= 1000 )
2201 {
2202 NMLVFINDITEM* pFindInfo = (NMLVFINDITEM*)lParam;
2203
2204 // no match by default
2205 *result = -1;
2206
2207 // we only handle string-based searches here
2208 //
2209 // TODO: what about LVFI_PARTIAL, should we handle this?
2210 if ( !(pFindInfo->lvfi.flags & LVFI_STRING) )
2211 {
2212 return false;
2213 }
2214
2215 const wxChar * const searchstr = pFindInfo->lvfi.psz;
2216 const size_t len = wxStrlen(searchstr);
2217
2218 // this is the first item we should examine, search from it
2219 // wrapping if necessary
2220 const int startPos = pFindInfo->iStart;
2221 const int maxPos = GetItemCount();
2222 wxCHECK_MSG( startPos <= maxPos, false,
2223 _T("bad starting position in LVN_ODFINDITEM") );
2224
2225 int currentPos = startPos;
2226 do
2227 {
2228 // wrap to the beginning if necessary
2229 if ( currentPos == maxPos )
2230 {
2231 // somewhat surprizingly, LVFI_WRAP isn't set in
2232 // flags but we still should wrap
2233 currentPos = 0;
2234 }
2235
2236 // does this item begin with searchstr?
2237 if ( wxStrnicmp(searchstr,
2238 GetItemText(currentPos), len) == 0 )
2239 {
2240 *result = currentPos;
2241 break;
2242 }
2243 }
2244 while ( ++currentPos != startPos );
2245
2246 if ( *result == -1 )
2247 {
2248 // not found
2249 return false;
2250 }
2251
2252 SetItemState(*result,
2253 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED,
2254 wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
2255 EnsureVisible(*result);
2256 return true;
2257 }
2258 else
2259 {
2260 processed = false;
2261 }
2262 break;
2263 #endif // HAVE_NMLVFINDITEM
2264
2265 case LVN_GETDISPINFO:
2266 if ( IsVirtual() )
2267 {
2268 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
2269
2270 LV_ITEM& lvi = info->item;
2271 long item = lvi.iItem;
2272
2273 if ( lvi.mask & LVIF_TEXT )
2274 {
2275 wxString text = OnGetItemText(item, lvi.iSubItem);
2276 wxStrncpy(lvi.pszText, text, lvi.cchTextMax);
2277 }
2278
2279 // see comment at the end of wxListCtrl::GetColumn()
2280 #ifdef NM_CUSTOMDRAW
2281 if ( lvi.mask & LVIF_IMAGE )
2282 {
2283 lvi.iImage = OnGetItemColumnImage(item, lvi.iSubItem);
2284 }
2285 #endif // NM_CUSTOMDRAW
2286
2287 // a little dose of healthy paranoia: as we never use
2288 // LVM_SETCALLBACKMASK we're not supposed to get these ones
2289 wxASSERT_MSG( !(lvi.mask & LVIF_STATE),
2290 _T("we don't support state callbacks yet!") );
2291
2292 return true;
2293 }
2294 // fall through
2295
2296 default:
2297 processed = false;
2298 }
2299
2300 if ( !processed )
2301 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2302 }
2303 else
2304 {
2305 // where did this one come from?
2306 return false;
2307 }
2308
2309 // process the event
2310 // -----------------
2311
2312 event.SetEventType(eventType);
2313
2314 bool processed = GetEventHandler()->ProcessEvent(event);
2315
2316 // post processing
2317 // ---------------
2318 switch ( nmhdr->code )
2319 {
2320 case LVN_DELETEALLITEMS:
2321 // always return true to suppress all additional LVN_DELETEITEM
2322 // notifications - this makes deleting all items from a list ctrl
2323 // much faster
2324 *result = TRUE;
2325
2326 // also, we may free all user data now (couldn't do it before as
2327 // the user should have access to it in OnDeleteAllItems() handler)
2328 FreeAllInternalData();
2329
2330 // the control is empty now, synchronize the cached number of items
2331 // with the real one
2332 m_count = 0;
2333 return true;
2334
2335 case LVN_ENDLABELEDITA:
2336 case LVN_ENDLABELEDITW:
2337 // logic here is inverted compared to all the other messages
2338 *result = event.IsAllowed();
2339
2340 // don't keep a stale wxTextCtrl around
2341 if ( m_textCtrl )
2342 {
2343 // EDIT control will be deleted by the list control itself so
2344 // prevent us from deleting it as well
2345 m_textCtrl->UnsubclassWin();
2346 m_textCtrl->SetHWND(0);
2347 delete m_textCtrl;
2348 m_textCtrl = NULL;
2349 }
2350
2351 return true;
2352 }
2353
2354 if ( processed )
2355 *result = !event.IsAllowed();
2356
2357 return processed;
2358 }
2359
2360 // ----------------------------------------------------------------------------
2361 // custom draw stuff
2362 // ----------------------------------------------------------------------------
2363
2364 // see comment at the end of wxListCtrl::GetColumn()
2365 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2366
2367 static RECT GetCustomDrawnItemRect(const NMCUSTOMDRAW& nmcd)
2368 {
2369 RECT rc;
2370 ListView_GetItemRect(nmcd.hdr.hwndFrom, nmcd.dwItemSpec, &rc, LVIR_BOUNDS);
2371
2372 RECT rcIcon;
2373 ListView_GetItemRect(nmcd.hdr.hwndFrom, nmcd.dwItemSpec, &rcIcon, LVIR_ICON);
2374
2375 // exclude the icon part, neither the selection background nor focus rect
2376 // should cover it
2377 rc.left = rcIcon.right;
2378
2379 return rc;
2380 }
2381
2382 static void HandleSubItemPrepaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont)
2383 {
2384 NMCUSTOMDRAW& nmcd = pLVCD->nmcd;
2385
2386 HDC hdc = nmcd.hdc;
2387 HWND hwndList = nmcd.hdr.hwndFrom;
2388 const DWORD item = nmcd.dwItemSpec;
2389
2390
2391 // the font must be valid, otherwise we wouldn't be painting the item at all
2392 SelectInHDC selFont(hdc, hfont);
2393
2394 // get the rectangle to paint
2395 RECT rc;
2396 ListView_GetSubItemRect(hwndList, item, pLVCD->iSubItem, LVIR_BOUNDS, &rc);
2397 if ( !pLVCD->iSubItem )
2398 {
2399 // broken ListView_GetSubItemRect() returns the entire item rect for
2400 // 0th subitem while we really need just the part for this column
2401 RECT rc2;
2402 ListView_GetSubItemRect(hwndList, item, 1, LVIR_BOUNDS, &rc2);
2403
2404 rc.right = rc2.left;
2405 rc.left += 4;
2406 }
2407 else // not first subitem
2408 {
2409 rc.left += 6;
2410 }
2411
2412 // get the image and text to draw
2413 wxChar text[512];
2414 LV_ITEM it;
2415 wxZeroMemory(it);
2416 it.mask = LVIF_TEXT | LVIF_IMAGE;
2417 it.iItem = item;
2418 it.iSubItem = pLVCD->iSubItem;
2419 it.pszText = text;
2420 it.cchTextMax = WXSIZEOF(text);
2421 ListView_GetItem(hwndList, &it);
2422
2423 HIMAGELIST himl = ListView_GetImageList(hwndList, LVSIL_SMALL);
2424 if ( himl && ImageList_GetImageCount(himl) )
2425 {
2426 if ( it.iImage != -1 )
2427 {
2428 ImageList_Draw(himl, it.iImage, hdc, rc.left, rc.top,
2429 nmcd.uItemState & CDIS_SELECTED ? ILD_SELECTED
2430 : ILD_TRANSPARENT);
2431 }
2432
2433 // notice that even if this item doesn't have any image, the list
2434 // control still leaves space for the image in the first column if the
2435 // image list is not empty (presumably so that items with and without
2436 // images align?)
2437 if ( it.iImage != -1 || it.iSubItem == 0 )
2438 {
2439 int wImage, hImage;
2440 ImageList_GetIconSize(himl, &wImage, &hImage);
2441
2442 rc.left += wImage + 2;
2443 }
2444 }
2445
2446 ::SetBkMode(hdc, TRANSPARENT);
2447
2448 // TODO: support for centred/right aligned columns
2449 ::DrawText(hdc, text, -1, &rc,
2450 #ifndef __WXWINCE__
2451 DT_WORD_ELLIPSIS |
2452 #endif // __WXWINCE__
2453 DT_NOPREFIX | DT_SINGLELINE | DT_VCENTER);
2454 }
2455
2456 static void HandleItemPostpaint(NMCUSTOMDRAW nmcd)
2457 {
2458 if ( nmcd.uItemState & CDIS_FOCUS )
2459 {
2460 RECT rc = GetCustomDrawnItemRect(nmcd);
2461
2462 // don't use the provided HDC, it's in some strange state by now
2463 ::DrawFocusRect(WindowHDC(nmcd.hdr.hwndFrom), &rc);
2464 }
2465 }
2466
2467 // pLVCD->clrText and clrTextBk should contain the colours to use
2468 static void HandleItemPaint(LPNMLVCUSTOMDRAW pLVCD, HFONT hfont)
2469 {
2470 NMCUSTOMDRAW& nmcd = pLVCD->nmcd; // just a shortcut
2471
2472 const HWND hwndList = nmcd.hdr.hwndFrom;
2473 const int item = nmcd.dwItemSpec;
2474
2475 // unfortunately we can't trust CDIS_SELECTED, it is often set even when
2476 // the item is not at all selected for some reason (comctl32 6), but we
2477 // also can't always trust ListView_GetItem() as it could return the old
2478 // item status if we're called just after the (de)selection, so remember
2479 // the last item to gain selection and also check for it here
2480 for ( int i = -1;; )
2481 {
2482 i = ListView_GetNextItem(hwndList, i, LVNI_SELECTED);
2483 if ( i == -1 )
2484 {
2485 nmcd.uItemState &= ~CDIS_SELECTED;
2486 break;
2487 }
2488
2489 if ( i == item )
2490 {
2491 nmcd.uItemState |= CDIS_SELECTED;
2492 break;
2493 }
2494 }
2495
2496 // same thing for CDIS_FOCUS (except simpler as there is only one of them)
2497 if ( ::GetFocus() == hwndList &&
2498 ListView_GetNextItem(hwndList, (WPARAM)-1, LVNI_FOCUSED) == item )
2499 {
2500 nmcd.uItemState |= CDIS_FOCUS;
2501 }
2502 else
2503 {
2504 nmcd.uItemState &= ~CDIS_FOCUS;
2505 }
2506
2507 if ( nmcd.uItemState & CDIS_SELECTED )
2508 {
2509 int syscolFg, syscolBg;
2510 if ( ::GetFocus() == hwndList )
2511 {
2512 syscolFg = COLOR_HIGHLIGHTTEXT;
2513 syscolBg = COLOR_HIGHLIGHT;
2514 }
2515 else // selected but unfocused
2516 {
2517 syscolFg = COLOR_WINDOWTEXT;
2518 syscolBg = COLOR_BTNFACE;
2519
2520 // don't grey out the icon in this case neither
2521 nmcd.uItemState &= ~CDIS_SELECTED;
2522 }
2523
2524 pLVCD->clrText = ::GetSysColor(syscolFg);
2525 pLVCD->clrTextBk = ::GetSysColor(syscolBg);
2526 }
2527 //else: not selected, use normal colours from pLVCD
2528
2529 HDC hdc = nmcd.hdc;
2530 RECT rc = GetCustomDrawnItemRect(nmcd);
2531
2532 ::SetTextColor(hdc, pLVCD->clrText);
2533 ::FillRect(hdc, &rc, AutoHBRUSH(pLVCD->clrTextBk));
2534
2535 // we could use CDRF_NOTIFYSUBITEMDRAW here but it results in weird repaint
2536 // problems so just draw everything except the focus rect from here instead
2537 const int colCount = Header_GetItemCount(ListView_GetHeader(hwndList));
2538 for ( int col = 0; col < colCount; col++ )
2539 {
2540 pLVCD->iSubItem = col;
2541 HandleSubItemPrepaint(pLVCD, hfont);
2542 }
2543
2544 HandleItemPostpaint(nmcd);
2545 }
2546
2547 static WXLPARAM HandleItemPrepaint(wxListCtrl *listctrl,
2548 LPNMLVCUSTOMDRAW pLVCD,
2549 wxListItemAttr *attr)
2550 {
2551 if ( !attr )
2552 {
2553 // nothing to do for this item
2554 return CDRF_DODEFAULT;
2555 }
2556
2557
2558 // set the colours to use for text drawing
2559 pLVCD->clrText = attr->HasTextColour()
2560 ? wxColourToRGB(attr->GetTextColour())
2561 : wxColourToRGB(listctrl->GetTextColour());
2562 pLVCD->clrTextBk = attr->HasBackgroundColour()
2563 ? wxColourToRGB(attr->GetBackgroundColour())
2564 : wxColourToRGB(listctrl->GetBackgroundColour());
2565
2566 // select the font if non default one is specified
2567 if ( attr->HasFont() )
2568 {
2569 wxFont font = attr->GetFont();
2570 if ( font.GetEncoding() != wxFONTENCODING_SYSTEM )
2571 {
2572 // the standard control ignores the font encoding/charset, at least
2573 // with recent comctl32.dll versions (5 and 6, it uses to work with
2574 // 4.something) so we have to draw the item entirely ourselves in
2575 // this case
2576 HandleItemPaint(pLVCD, GetHfontOf(font));
2577 return CDRF_SKIPDEFAULT;
2578 }
2579
2580 ::SelectObject(pLVCD->nmcd.hdc, GetHfontOf(font));
2581
2582 return CDRF_NEWFONT;
2583 }
2584
2585 return CDRF_DODEFAULT;
2586 }
2587
2588 WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam)
2589 {
2590 LPNMLVCUSTOMDRAW pLVCD = (LPNMLVCUSTOMDRAW)lParam;
2591 NMCUSTOMDRAW& nmcd = pLVCD->nmcd;
2592 switch ( nmcd.dwDrawStage )
2593 {
2594 case CDDS_PREPAINT:
2595 // if we've got any items with non standard attributes,
2596 // notify us before painting each item
2597 //
2598 // for virtual controls, always suppose that we have attributes as
2599 // there is no way to check for this
2600 if ( IsVirtual() || m_hasAnyAttr )
2601 return CDRF_NOTIFYITEMDRAW;
2602 break;
2603
2604 case CDDS_ITEMPREPAINT:
2605 const int item = nmcd.dwItemSpec;
2606
2607 // we get this message with item == 0 for an empty control, we
2608 // must ignore it as calling OnGetItemAttr() would be wrong
2609 if ( item < 0 || item >= GetItemCount() )
2610 break;
2611
2612 return HandleItemPrepaint(this, pLVCD, DoGetItemAttr(item));
2613 }
2614
2615 return CDRF_DODEFAULT;
2616 }
2617
2618 #endif // NM_CUSTOMDRAW supported
2619
2620 // Necessary for drawing hrules and vrules, if specified
2621 void wxListCtrl::OnPaint(wxPaintEvent& event)
2622 {
2623 bool drawHRules = HasFlag(wxLC_HRULES);
2624 bool drawVRules = HasFlag(wxLC_VRULES);
2625
2626 if (!InReportView() || !drawHRules && !drawVRules)
2627 {
2628 event.Skip();
2629 return;
2630 }
2631
2632 wxPaintDC dc(this);
2633
2634 wxControl::OnPaint(event);
2635
2636 // Reset the device origin since it may have been set
2637 dc.SetDeviceOrigin(0, 0);
2638
2639 wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
2640 dc.SetPen(pen);
2641 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2642
2643 wxSize clientSize = GetClientSize();
2644 wxRect itemRect;
2645
2646 int itemCount = GetItemCount();
2647 int i;
2648 if (drawHRules)
2649 {
2650 long top = GetTopItem();
2651 for (i = top; i < top + GetCountPerPage() + 1; i++)
2652 {
2653 if (GetItemRect(i, itemRect))
2654 {
2655 int cy = itemRect.GetTop();
2656 if (i != 0) // Don't draw the first one
2657 {
2658 dc.DrawLine(0, cy, clientSize.x, cy);
2659 }
2660 // Draw last line
2661 if (i == itemCount - 1)
2662 {
2663 cy = itemRect.GetBottom();
2664 dc.DrawLine(0, cy, clientSize.x, cy);
2665 }
2666 }
2667 }
2668 }
2669 i = itemCount - 1;
2670 if (drawVRules && (i > -1))
2671 {
2672 wxRect firstItemRect;
2673 GetItemRect(0, firstItemRect);
2674
2675 if (GetItemRect(i, itemRect))
2676 {
2677 // this is a fix for bug 673394: erase the pixels which we would
2678 // otherwise leave on the screen
2679 static const int gap = 2;
2680 dc.SetPen(*wxTRANSPARENT_PEN);
2681 dc.SetBrush(wxBrush(GetBackgroundColour()));
2682 dc.DrawRectangle(0, firstItemRect.GetY() - gap,
2683 clientSize.GetWidth(), gap);
2684
2685 dc.SetPen(pen);
2686 dc.SetBrush(*wxTRANSPARENT_BRUSH);
2687 int x = itemRect.GetX();
2688 for (int col = 0; col < GetColumnCount(); col++)
2689 {
2690 int colWidth = GetColumnWidth(col);
2691 x += colWidth ;
2692 dc.DrawLine(x-1, firstItemRect.GetY() - gap,
2693 x-1, itemRect.GetBottom());
2694 }
2695 }
2696 }
2697 }
2698
2699 WXLRESULT
2700 wxListCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2701 {
2702 switch ( nMsg )
2703 {
2704 #ifdef WM_PRINT
2705 case WM_PRINT:
2706 // we should bypass our own WM_PRINT handling as we don't handle
2707 // PRF_CHILDREN flag, so leave it to the native control itself
2708 return MSWDefWindowProc(nMsg, wParam, lParam);
2709 #endif // WM_PRINT
2710
2711 case WM_CONTEXTMENU:
2712 // because this message is propagated upwards the child-parent
2713 // chain, we get it for the right clicks on the header window but
2714 // this is confusing in wx as right clicking there already
2715 // generates a separate wxEVT_COMMAND_LIST_COL_RIGHT_CLICK event
2716 // so just ignore them
2717 if ( (HWND)wParam == ListView_GetHeader(GetHwnd()) )
2718 return 0;
2719 //else: break
2720 }
2721
2722 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
2723 }
2724
2725 // ----------------------------------------------------------------------------
2726 // virtual list controls
2727 // ----------------------------------------------------------------------------
2728
2729 wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2730 {
2731 // this is a pure virtual function, in fact - which is not really pure
2732 // because the controls which are not virtual don't need to implement it
2733 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2734
2735 return wxEmptyString;
2736 }
2737
2738 int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2739 {
2740 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
2741 -1,
2742 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
2743 return -1;
2744 }
2745
2746 int wxListCtrl::OnGetItemColumnImage(long item, long column) const
2747 {
2748 if (!column)
2749 return OnGetItemImage(item);
2750
2751 return -1;
2752 }
2753
2754 wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
2755 {
2756 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
2757 _T("invalid item index in OnGetItemAttr()") );
2758
2759 // no attributes by default
2760 return NULL;
2761 }
2762
2763 wxListItemAttr *wxListCtrl::DoGetItemAttr(long item) const
2764 {
2765 return IsVirtual() ? OnGetItemAttr(item)
2766 : wxGetInternalDataAttr(this, item);
2767 }
2768
2769 void wxListCtrl::SetItemCount(long count)
2770 {
2771 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2772
2773 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count,
2774 LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL) )
2775 {
2776 wxLogLastError(_T("ListView_SetItemCount"));
2777 }
2778 m_count = count;
2779 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
2780 wxT("m_count should match ListView_GetItemCount"));
2781 }
2782
2783 void wxListCtrl::RefreshItem(long item)
2784 {
2785 // strangely enough, ListView_Update() results in much more flicker here
2786 // than a dumb Refresh() -- why?
2787 #if 0
2788 if ( !ListView_Update(GetHwnd(), item) )
2789 {
2790 wxLogLastError(_T("ListView_Update"));
2791 }
2792 #else // 1
2793 wxRect rect;
2794 GetItemRect(item, rect);
2795 RefreshRect(rect);
2796 #endif // 0/1
2797 }
2798
2799 void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
2800 {
2801 wxRect rect1, rect2;
2802 GetItemRect(itemFrom, rect1);
2803 GetItemRect(itemTo, rect2);
2804
2805 wxRect rect = rect1;
2806 rect.height = rect2.GetBottom() - rect1.GetTop();
2807
2808 RefreshRect(rect);
2809 }
2810
2811 // ----------------------------------------------------------------------------
2812 // internal data stuff
2813 // ----------------------------------------------------------------------------
2814
2815 static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId)
2816 {
2817 LV_ITEM it;
2818 it.mask = LVIF_PARAM;
2819 it.iItem = itemId;
2820
2821 if ( !ListView_GetItem(hwnd, &it) )
2822 return NULL;
2823
2824 return (wxListItemInternalData *) it.lParam;
2825 }
2826
2827 static
2828 wxListItemInternalData *wxGetInternalData(const wxListCtrl *ctl, long itemId)
2829 {
2830 return wxGetInternalData(GetHwndOf(ctl), itemId);
2831 }
2832
2833 static
2834 wxListItemAttr *wxGetInternalDataAttr(const wxListCtrl *ctl, long itemId)
2835 {
2836 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
2837
2838 return data ? data->attr : NULL;
2839 }
2840
2841 static void wxDeleteInternalData(wxListCtrl* ctl, long itemId)
2842 {
2843 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
2844 if (data)
2845 {
2846 LV_ITEM item;
2847 memset(&item, 0, sizeof(item));
2848 item.iItem = itemId;
2849 item.mask = LVIF_PARAM;
2850 item.lParam = (LPARAM) 0;
2851 ListView_SetItem((HWND)ctl->GetHWND(), &item);
2852 delete data;
2853 }
2854 }
2855
2856 // ----------------------------------------------------------------------------
2857 // wxWin <-> MSW items conversions
2858 // ----------------------------------------------------------------------------
2859
2860 static void wxConvertFromMSWListItem(HWND hwndListCtrl,
2861 wxListItem& info,
2862 LV_ITEM& lvItem)
2863 {
2864 wxListItemInternalData *internaldata =
2865 (wxListItemInternalData *) lvItem.lParam;
2866
2867 if (internaldata)
2868 info.m_data = internaldata->lParam;
2869
2870 info.m_mask = 0;
2871 info.m_state = 0;
2872 info.m_stateMask = 0;
2873 info.m_itemId = lvItem.iItem;
2874
2875 long oldMask = lvItem.mask;
2876
2877 bool needText = false;
2878 if (hwndListCtrl != 0)
2879 {
2880 if ( lvItem.mask & LVIF_TEXT )
2881 needText = false;
2882 else
2883 needText = true;
2884
2885 if ( needText )
2886 {
2887 lvItem.pszText = new wxChar[513];
2888 lvItem.cchTextMax = 512;
2889 }
2890 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
2891 ::SendMessage(hwndListCtrl, LVM_GETITEM, 0, (LPARAM)& lvItem);
2892 }
2893
2894 if ( lvItem.mask & LVIF_STATE )
2895 {
2896 info.m_mask |= wxLIST_MASK_STATE;
2897
2898 if ( lvItem.stateMask & LVIS_CUT)
2899 {
2900 info.m_stateMask |= wxLIST_STATE_CUT;
2901 if ( lvItem.state & LVIS_CUT )
2902 info.m_state |= wxLIST_STATE_CUT;
2903 }
2904 if ( lvItem.stateMask & LVIS_DROPHILITED)
2905 {
2906 info.m_stateMask |= wxLIST_STATE_DROPHILITED;
2907 if ( lvItem.state & LVIS_DROPHILITED )
2908 info.m_state |= wxLIST_STATE_DROPHILITED;
2909 }
2910 if ( lvItem.stateMask & LVIS_FOCUSED)
2911 {
2912 info.m_stateMask |= wxLIST_STATE_FOCUSED;
2913 if ( lvItem.state & LVIS_FOCUSED )
2914 info.m_state |= wxLIST_STATE_FOCUSED;
2915 }
2916 if ( lvItem.stateMask & LVIS_SELECTED)
2917 {
2918 info.m_stateMask |= wxLIST_STATE_SELECTED;
2919 if ( lvItem.state & LVIS_SELECTED )
2920 info.m_state |= wxLIST_STATE_SELECTED;
2921 }
2922 }
2923
2924 if ( lvItem.mask & LVIF_TEXT )
2925 {
2926 info.m_mask |= wxLIST_MASK_TEXT;
2927 info.m_text = lvItem.pszText;
2928 }
2929 if ( lvItem.mask & LVIF_IMAGE )
2930 {
2931 info.m_mask |= wxLIST_MASK_IMAGE;
2932 info.m_image = lvItem.iImage;
2933 }
2934 if ( lvItem.mask & LVIF_PARAM )
2935 info.m_mask |= wxLIST_MASK_DATA;
2936 if ( lvItem.mask & LVIF_DI_SETITEM )
2937 info.m_mask |= wxLIST_SET_ITEM;
2938 info.m_col = lvItem.iSubItem;
2939
2940 if (needText)
2941 {
2942 if (lvItem.pszText)
2943 delete[] lvItem.pszText;
2944 }
2945 lvItem.mask = oldMask;
2946 }
2947
2948 static void wxConvertToMSWFlags(long state, long stateMask, LV_ITEM& lvItem)
2949 {
2950 if (stateMask & wxLIST_STATE_CUT)
2951 {
2952 lvItem.stateMask |= LVIS_CUT;
2953 if (state & wxLIST_STATE_CUT)
2954 lvItem.state |= LVIS_CUT;
2955 }
2956 if (stateMask & wxLIST_STATE_DROPHILITED)
2957 {
2958 lvItem.stateMask |= LVIS_DROPHILITED;
2959 if (state & wxLIST_STATE_DROPHILITED)
2960 lvItem.state |= LVIS_DROPHILITED;
2961 }
2962 if (stateMask & wxLIST_STATE_FOCUSED)
2963 {
2964 lvItem.stateMask |= LVIS_FOCUSED;
2965 if (state & wxLIST_STATE_FOCUSED)
2966 lvItem.state |= LVIS_FOCUSED;
2967 }
2968 if (stateMask & wxLIST_STATE_SELECTED)
2969 {
2970 lvItem.stateMask |= LVIS_SELECTED;
2971 if (state & wxLIST_STATE_SELECTED)
2972 lvItem.state |= LVIS_SELECTED;
2973 }
2974 }
2975
2976 static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
2977 const wxListItem& info,
2978 LV_ITEM& lvItem)
2979 {
2980 lvItem.iItem = (int) info.m_itemId;
2981
2982 lvItem.iImage = info.m_image;
2983 lvItem.stateMask = 0;
2984 lvItem.state = 0;
2985 lvItem.mask = 0;
2986 lvItem.iSubItem = info.m_col;
2987
2988 if (info.m_mask & wxLIST_MASK_STATE)
2989 {
2990 lvItem.mask |= LVIF_STATE;
2991
2992 wxConvertToMSWFlags(info.m_state, info.m_stateMask, lvItem);
2993 }
2994
2995 if (info.m_mask & wxLIST_MASK_TEXT)
2996 {
2997 lvItem.mask |= LVIF_TEXT;
2998 if ( ctrl->HasFlag(wxLC_USER_TEXT) )
2999 {
3000 lvItem.pszText = LPSTR_TEXTCALLBACK;
3001 }
3002 else
3003 {
3004 // pszText is not const, hence the cast
3005 lvItem.pszText = (wxChar *)info.m_text.c_str();
3006 if ( lvItem.pszText )
3007 lvItem.cchTextMax = info.m_text.length();
3008 else
3009 lvItem.cchTextMax = 0;
3010 }
3011 }
3012 if (info.m_mask & wxLIST_MASK_IMAGE)
3013 lvItem.mask |= LVIF_IMAGE;
3014 }
3015
3016 static void wxConvertToMSWListCol(HWND hwndList,
3017 int col,
3018 const wxListItem& item,
3019 LV_COLUMN& lvCol)
3020 {
3021 wxZeroMemory(lvCol);
3022
3023 if ( item.m_mask & wxLIST_MASK_TEXT )
3024 {
3025 lvCol.mask |= LVCF_TEXT;
3026 lvCol.pszText = (wxChar *)item.m_text.c_str(); // cast is safe
3027 }
3028
3029 if ( item.m_mask & wxLIST_MASK_FORMAT )
3030 {
3031 lvCol.mask |= LVCF_FMT;
3032
3033 if ( item.m_format == wxLIST_FORMAT_LEFT )
3034 lvCol.fmt = LVCFMT_LEFT;
3035 else if ( item.m_format == wxLIST_FORMAT_RIGHT )
3036 lvCol.fmt = LVCFMT_RIGHT;
3037 else if ( item.m_format == wxLIST_FORMAT_CENTRE )
3038 lvCol.fmt = LVCFMT_CENTER;
3039 }
3040
3041 if ( item.m_mask & wxLIST_MASK_WIDTH )
3042 {
3043 lvCol.mask |= LVCF_WIDTH;
3044 if ( item.m_width == wxLIST_AUTOSIZE)
3045 lvCol.cx = LVSCW_AUTOSIZE;
3046 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
3047 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
3048 else
3049 lvCol.cx = item.m_width;
3050 }
3051
3052 // see comment at the end of wxListCtrl::GetColumn()
3053 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
3054 if ( item.m_mask & wxLIST_MASK_IMAGE )
3055 {
3056 if ( wxApp::GetComCtl32Version() >= 470 )
3057 {
3058 lvCol.mask |= LVCF_IMAGE;
3059
3060 // we use LVCFMT_BITMAP_ON_RIGHT because the images on the right
3061 // seem to be generally nicer than on the left and the generic
3062 // version only draws them on the right (we don't have a flag to
3063 // specify the image location anyhow)
3064 //
3065 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
3066 // make any difference in my tests -- but maybe we should?
3067 if ( item.m_image != -1 )
3068 {
3069 // as we're going to overwrite the format field, get its
3070 // current value first -- unless we want to overwrite it anyhow
3071 if ( !(lvCol.mask & LVCF_FMT) )
3072 {
3073 LV_COLUMN lvColOld;
3074 wxZeroMemory(lvColOld);
3075 lvColOld.mask = LVCF_FMT;
3076 if ( ListView_GetColumn(hwndList, col, &lvColOld) )
3077 {
3078 lvCol.fmt = lvColOld.fmt;
3079 }
3080
3081 lvCol.mask |= LVCF_FMT;
3082 }
3083
3084 lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE;
3085 }
3086
3087 lvCol.iImage = item.m_image;
3088 }
3089 //else: it doesn't support item images anyhow
3090 }
3091 #endif // _WIN32_IE >= 0x0300
3092 }
3093
3094 #endif // wxUSE_LISTCTRL