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