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