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