]> git.saurik.com Git - wxWidgets.git/blob - src/msw/listctrl.cpp
Add --with-sdl to the build doc and RPM spec
[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__)
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 , , 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 #if wxUSE_UNICODE
357 ::SendMessage(GetHwnd(), LVM_SETUNICODEFORMAT, TRUE, 0);
358 #endif
359
360 // for comctl32.dll v 4.70+ we want to have this attribute because it's
361 // prettier (and also because wxGTK does it like this)
362 if ( InReportView() && wxTheApp->GetComCtl32Version() >= 470 )
363 {
364 ::SendMessage(GetHwnd(), LVM_SETEXTENDEDLISTVIEWSTYLE,
365 0, LVS_EX_FULLROWSELECT);
366 }
367
368 // inherit foreground colour but our background should be the same as for
369 // listboxes and other such "container" windows and not inherited
370 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
371 SetForegroundColour(GetParent()->GetForegroundColour());
372
373 return TRUE;
374 }
375
376 WXDWORD wxListCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const
377 {
378 WXDWORD wstyle = wxControl::MSWGetStyle(style, exstyle);
379
380 wstyle |= LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS;
381
382 #ifdef __WXDEBUG__
383 size_t nModes = 0;
384
385 #define MAP_MODE_STYLE(wx, ms) \
386 if ( style & (wx) ) { wstyle |= (ms); nModes++; }
387 #else // !__WXDEBUG__
388 #define MAP_MODE_STYLE(wx, ms) \
389 if ( style & (wx) ) wstyle |= (ms);
390 #endif // __WXDEBUG__
391
392 MAP_MODE_STYLE(wxLC_ICON, LVS_ICON)
393 MAP_MODE_STYLE(wxLC_SMALL_ICON, LVS_SMALLICON)
394 MAP_MODE_STYLE(wxLC_LIST, LVS_LIST)
395 MAP_MODE_STYLE(wxLC_REPORT, LVS_REPORT)
396
397 wxASSERT_MSG( nModes == 1,
398 _T("wxListCtrl style should have exactly one mode bit set") );
399
400 #undef MAP_MODE_STYLE
401
402 if ( style & wxLC_ALIGN_LEFT )
403 wstyle |= LVS_ALIGNLEFT;
404
405 if ( style & wxLC_ALIGN_TOP )
406 wstyle |= LVS_ALIGNTOP;
407
408 if ( style & wxLC_AUTOARRANGE )
409 wstyle |= LVS_AUTOARRANGE;
410
411 if ( style & wxLC_NO_SORT_HEADER )
412 wstyle |= LVS_NOSORTHEADER;
413
414 if ( style & wxLC_NO_HEADER )
415 wstyle |= LVS_NOCOLUMNHEADER;
416
417 if ( style & wxLC_EDIT_LABELS )
418 wstyle |= LVS_EDITLABELS;
419
420 if ( style & wxLC_SINGLE_SEL )
421 wstyle |= LVS_SINGLESEL;
422
423 if ( style & wxLC_SORT_ASCENDING )
424 {
425 wstyle |= LVS_SORTASCENDING;
426
427 wxASSERT_MSG( !(style & wxLC_SORT_DESCENDING),
428 _T("can't sort in ascending and descending orders at once") );
429 }
430 else if ( style & wxLC_SORT_DESCENDING )
431 wstyle |= LVS_SORTDESCENDING;
432
433 #if !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
434 if ( style & wxLC_VIRTUAL )
435 {
436 int ver = wxTheApp->GetComCtl32Version();
437 if ( ver < 470 )
438 {
439 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."),
440 ver / 100, ver % 100);
441 }
442
443 wstyle |= LVS_OWNERDATA;
444 }
445 #endif // ancient cygwin
446
447 return wstyle;
448 }
449
450 void wxListCtrl::UpdateStyle()
451 {
452 if ( GetHwnd() )
453 {
454 // The new window view style
455 DWORD dwStyleNew = MSWGetStyle(m_windowStyle, NULL);
456
457 // Get the current window style.
458 DWORD dwStyleOld = ::GetWindowLong(GetHwnd(), GWL_STYLE);
459
460 // Only set the window style if the view bits have changed.
461 if ( dwStyleOld != dwStyleNew )
462 {
463 ::SetWindowLong(GetHwnd(), GWL_STYLE, dwStyleNew);
464 }
465 }
466 }
467
468 void wxListCtrl::FreeAllInternalData()
469 {
470 if (m_AnyInternalData)
471 {
472 int n = GetItemCount();
473 int i = 0;
474
475 m_ignoreChangeMessages = TRUE;
476 for (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 ( flag & style )
522 {
523 if ( !add )
524 flag -= style;
525 }
526 else
527 {
528 if ( add )
529 {
530 flag |= style;
531 }
532 }
533
534 m_windowStyle = flag;
535
536 UpdateStyle();
537 }
538
539 // Set the whole window style
540 void wxListCtrl::SetWindowStyleFlag(long flag)
541 {
542 m_windowStyle = flag;
543
544 UpdateStyle();
545 }
546
547 // ----------------------------------------------------------------------------
548 // accessors
549 // ----------------------------------------------------------------------------
550
551 // Sets the foreground, i.e. text, colour
552 bool wxListCtrl::SetForegroundColour(const wxColour& col)
553 {
554 if ( !wxWindow::SetForegroundColour(col) )
555 return FALSE;
556
557 ListView_SetTextColor(GetHwnd(), wxColourToRGB(col));
558
559 return TRUE;
560 }
561
562 // Sets the background colour
563 bool wxListCtrl::SetBackgroundColour(const wxColour& col)
564 {
565 if ( !wxWindow::SetBackgroundColour(col) )
566 return FALSE;
567
568 // we set the same colour for both the "empty" background and the items
569 // background
570 COLORREF color = wxColourToRGB(col);
571 ListView_SetBkColor(GetHwnd(), color);
572 ListView_SetTextBkColor(GetHwnd(), color);
573
574 return TRUE;
575 }
576
577 // Gets information about this column
578 bool wxListCtrl::GetColumn(int col, wxListItem& item) const
579 {
580 LV_COLUMN lvCol;
581 wxZeroMemory(lvCol);
582
583 lvCol.mask = LVCF_WIDTH;
584
585 if ( item.m_mask & wxLIST_MASK_TEXT )
586 {
587 lvCol.mask |= LVCF_TEXT;
588 lvCol.pszText = new wxChar[513];
589 lvCol.cchTextMax = 512;
590 }
591
592 if ( item.m_mask & wxLIST_MASK_FORMAT )
593 {
594 lvCol.mask |= LVCF_FMT;
595 }
596
597 if ( item.m_mask & wxLIST_MASK_IMAGE )
598 {
599 lvCol.mask |= LVCF_IMAGE;
600 }
601
602 bool success = ListView_GetColumn(GetHwnd(), col, &lvCol) != 0;
603
604 // item.m_subItem = lvCol.iSubItem;
605 item.m_width = lvCol.cx;
606
607 if ( (item.m_mask & wxLIST_MASK_TEXT) && lvCol.pszText )
608 {
609 item.m_text = lvCol.pszText;
610 delete[] lvCol.pszText;
611 }
612
613 if ( item.m_mask & wxLIST_MASK_FORMAT )
614 {
615 switch (lvCol.fmt & LVCFMT_JUSTIFYMASK) {
616 case LVCFMT_LEFT:
617 item.m_format = wxLIST_FORMAT_LEFT;
618 break;
619 case LVCFMT_RIGHT:
620 item.m_format = wxLIST_FORMAT_RIGHT;
621 break;
622 case LVCFMT_CENTER:
623 item.m_format = wxLIST_FORMAT_CENTRE;
624 break;
625 default:
626 item.m_format = -1; // Unknown?
627 break;
628 }
629 }
630
631 // the column images were not supported in older versions but how to check
632 // for this? we can't use _WIN32_IE because we always define it to a very
633 // high value, so see if another symbol which is only defined starting from
634 // comctl32.dll 4.70 is available
635 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
636 if ( item.m_mask & wxLIST_MASK_IMAGE )
637 {
638 item.m_image = lvCol.iImage;
639 }
640 #endif // LVCOLUMN::iImage exists
641
642 return success;
643 }
644
645 // Sets information about this column
646 bool wxListCtrl::SetColumn(int col, wxListItem& item)
647 {
648 LV_COLUMN lvCol;
649 wxConvertToMSWListCol(col, item, lvCol);
650
651 return ListView_SetColumn(GetHwnd(), col, &lvCol) != 0;
652 }
653
654 // Gets the column width
655 int wxListCtrl::GetColumnWidth(int col) const
656 {
657 return ListView_GetColumnWidth(GetHwnd(), col);
658 }
659
660 // Sets the column width
661 bool wxListCtrl::SetColumnWidth(int col, int width)
662 {
663 int col2 = col;
664 if ( m_windowStyle & wxLC_LIST )
665 col2 = -1;
666
667 int width2 = width;
668 if ( width2 == wxLIST_AUTOSIZE)
669 width2 = LVSCW_AUTOSIZE;
670 else if ( width2 == wxLIST_AUTOSIZE_USEHEADER)
671 width2 = LVSCW_AUTOSIZE_USEHEADER;
672
673 return ListView_SetColumnWidth(GetHwnd(), col2, width2) != 0;
674 }
675
676 // Gets the number of items that can fit vertically in the
677 // visible area of the list control (list or report view)
678 // or the total number of items in the list control (icon
679 // or small icon view)
680 int wxListCtrl::GetCountPerPage() const
681 {
682 return ListView_GetCountPerPage(GetHwnd());
683 }
684
685 // Gets the edit control for editing labels.
686 wxTextCtrl* wxListCtrl::GetEditControl() const
687 {
688 return m_textCtrl;
689 }
690
691 // Gets information about the item
692 bool wxListCtrl::GetItem(wxListItem& info) const
693 {
694 LV_ITEM lvItem;
695 wxZeroMemory(lvItem);
696
697 lvItem.iItem = info.m_itemId;
698 lvItem.iSubItem = info.m_col;
699
700 if ( info.m_mask & wxLIST_MASK_TEXT )
701 {
702 lvItem.mask |= LVIF_TEXT;
703 lvItem.pszText = new wxChar[513];
704 lvItem.cchTextMax = 512;
705 }
706 else
707 {
708 lvItem.pszText = NULL;
709 }
710
711 if (info.m_mask & wxLIST_MASK_DATA)
712 lvItem.mask |= LVIF_PARAM;
713
714 if (info.m_mask & wxLIST_MASK_IMAGE)
715 lvItem.mask |= LVIF_IMAGE;
716
717 if ( info.m_mask & wxLIST_MASK_STATE )
718 {
719 lvItem.mask |= LVIF_STATE;
720 // the other bits are hardly interesting anyhow
721 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
722 }
723
724 bool success = ListView_GetItem((HWND)GetHWND(), &lvItem) != 0;
725 if ( !success )
726 {
727 wxLogError(_("Couldn't retrieve information about list control item %d."),
728 lvItem.iItem);
729 }
730 else
731 {
732 // give NULL as hwnd as we already have everything we need
733 wxConvertFromMSWListItem(NULL, info, lvItem);
734 }
735
736 if (lvItem.pszText)
737 delete[] lvItem.pszText;
738
739 return success;
740 }
741
742 // Sets information about the item
743 bool wxListCtrl::SetItem(wxListItem& info)
744 {
745 LV_ITEM item;
746 wxConvertToMSWListItem(this, info, item);
747
748 // we never update the lParam if it contains our pointer
749 // to the wxListItemInternalData structure
750 item.mask &= ~LVIF_PARAM;
751
752 // check if setting attributes or lParam
753 if (info.HasAttributes() || (info.m_mask & wxLIST_MASK_DATA))
754 {
755 // get internal item data
756 // perhaps a cache here ?
757 wxListItemInternalData *data = wxGetInternalData(this, info.m_itemId);
758
759 if (! data)
760 {
761 // need to set it
762 m_AnyInternalData = TRUE;
763 data = new wxListItemInternalData();
764 item.lParam = (LPARAM) data;
765 item.mask |= LVIF_PARAM;
766 };
767
768
769 // user data
770 if (info.m_mask & wxLIST_MASK_DATA)
771 data->lParam = info.m_data;
772
773 // attributes
774 if (info.HasAttributes())
775 {
776 if (data->attr)
777 *data->attr = *info.GetAttributes();
778 else
779 data->attr = new wxListItemAttr(*info.GetAttributes());
780 };
781 };
782
783
784 // we could be changing only the attribute in which case we don't need to
785 // call ListView_SetItem() at all
786 if ( item.mask )
787 {
788 item.cchTextMax = 0;
789 if ( !ListView_SetItem(GetHwnd(), &item) )
790 {
791 wxLogDebug(_T("ListView_SetItem() failed"));
792
793 return FALSE;
794 }
795 }
796
797 // we need to update the item immediately to show the new image
798 bool updateNow = (info.m_mask & wxLIST_MASK_IMAGE) != 0;
799
800 // check whether it has any custom attributes
801 if ( info.HasAttributes() )
802 {
803 m_hasAnyAttr = TRUE;
804
805 // if the colour has changed, we must redraw the item
806 updateNow = TRUE;
807 }
808
809 if ( updateNow )
810 {
811 // we need this to make the change visible right now
812 RefreshItem(item.iItem);
813 }
814
815 return TRUE;
816 }
817
818 long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId)
819 {
820 wxListItem info;
821 info.m_text = label;
822 info.m_mask = wxLIST_MASK_TEXT;
823 info.m_itemId = index;
824 info.m_col = col;
825 if ( imageId > -1 )
826 {
827 info.m_image = imageId;
828 info.m_mask |= wxLIST_MASK_IMAGE;
829 }
830 return SetItem(info);
831 }
832
833
834 // Gets the item state
835 int wxListCtrl::GetItemState(long item, long stateMask) const
836 {
837 wxListItem info;
838
839 info.m_mask = wxLIST_MASK_STATE;
840 info.m_stateMask = stateMask;
841 info.m_itemId = item;
842
843 if (!GetItem(info))
844 return 0;
845
846 return info.m_state;
847 }
848
849 // Sets the item state
850 bool wxListCtrl::SetItemState(long item, long state, long stateMask)
851 {
852 // NB: don't use SetItem() here as it doesn't work with the virtual list
853 // controls
854 LV_ITEM lvItem;
855 wxZeroMemory(lvItem);
856
857 wxConvertToMSWFlags(state, stateMask, lvItem);
858
859 // for the virtual list controls we need to refresh the previously focused
860 // item manually when changing focus without changing selection
861 // programmatically because otherwise it keeps its focus rectangle until
862 // next repaint (yet another comctl32 bug)
863 long focusOld;
864 if ( IsVirtual() &&
865 (stateMask & wxLIST_STATE_FOCUSED) &&
866 (state & wxLIST_STATE_FOCUSED) )
867 {
868 focusOld = GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_FOCUSED);
869 }
870 else
871 {
872 focusOld = -1;
873 }
874
875 if ( !::SendMessage(GetHwnd(), LVM_SETITEMSTATE,
876 (WPARAM)item, (LPARAM)&lvItem) )
877 {
878 wxLogLastError(_T("ListView_SetItemState"));
879
880 return FALSE;
881 }
882
883 if ( focusOld != -1 )
884 {
885 // no need to refresh the item if it was previously selected, it would
886 // only result in annoying flicker
887 if ( !(GetItemState(focusOld,
888 wxLIST_STATE_SELECTED) & wxLIST_STATE_SELECTED) )
889 {
890 RefreshItem(focusOld);
891 }
892 }
893
894 return TRUE;
895 }
896
897 // Sets the item image
898 bool wxListCtrl::SetItemImage(long item, int image, int WXUNUSED(selImage))
899 {
900 wxListItem info;
901
902 info.m_mask = wxLIST_MASK_IMAGE;
903 info.m_image = image;
904 info.m_itemId = item;
905
906 return SetItem(info);
907 }
908
909 // Gets the item text
910 wxString wxListCtrl::GetItemText(long item) const
911 {
912 wxListItem info;
913
914 info.m_mask = wxLIST_MASK_TEXT;
915 info.m_itemId = item;
916
917 if (!GetItem(info))
918 return wxEmptyString;
919 return info.m_text;
920 }
921
922 // Sets the item text
923 void wxListCtrl::SetItemText(long item, const wxString& str)
924 {
925 wxListItem info;
926
927 info.m_mask = wxLIST_MASK_TEXT;
928 info.m_itemId = item;
929 info.m_text = str;
930
931 SetItem(info);
932 }
933
934 // Gets the item data
935 long wxListCtrl::GetItemData(long item) const
936 {
937 wxListItem info;
938
939 info.m_mask = wxLIST_MASK_DATA;
940 info.m_itemId = item;
941
942 if (!GetItem(info))
943 return 0;
944 return info.m_data;
945 }
946
947 // Sets the item data
948 bool wxListCtrl::SetItemData(long item, long data)
949 {
950 wxListItem info;
951
952 info.m_mask = wxLIST_MASK_DATA;
953 info.m_itemId = item;
954 info.m_data = data;
955
956 return SetItem(info);
957 }
958
959 wxRect wxListCtrl::GetViewRect() const
960 {
961 wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
962 _T("wxListCtrl::GetViewRect() only works in icon mode") );
963
964 RECT rc;
965 if ( !ListView_GetViewRect(GetHwnd(), &rc) )
966 {
967 wxLogDebug(_T("ListView_GetViewRect() failed."));
968
969 wxZeroMemory(rc);
970 }
971
972 wxRect rect;
973 wxCopyRECTToRect(rc, rect);
974
975 return rect;
976 }
977
978 // Gets the item rectangle
979 bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
980 {
981 RECT rectWin;
982
983 int codeWin;
984 if ( code == wxLIST_RECT_BOUNDS )
985 codeWin = LVIR_BOUNDS;
986 else if ( code == wxLIST_RECT_ICON )
987 codeWin = LVIR_ICON;
988 else if ( code == wxLIST_RECT_LABEL )
989 codeWin = LVIR_LABEL;
990 else
991 {
992 wxFAIL_MSG( _T("incorrect code in GetItemRect()") );
993
994 codeWin = LVIR_BOUNDS;
995 }
996
997 bool success = ListView_GetItemRect(GetHwnd(), (int) item, &rectWin, codeWin) != 0;
998
999 rect.x = rectWin.left;
1000 rect.y = rectWin.top;
1001 rect.width = rectWin.right - rectWin.left;
1002 rect.height = rectWin.bottom - rectWin.top;
1003
1004 return success;
1005 }
1006
1007 // Gets the item position
1008 bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
1009 {
1010 POINT pt;
1011
1012 bool success = (ListView_GetItemPosition(GetHwnd(), (int) item, &pt) != 0);
1013
1014 pos.x = pt.x; pos.y = pt.y;
1015 return success;
1016 }
1017
1018 // Sets the item position.
1019 bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
1020 {
1021 return (ListView_SetItemPosition(GetHwnd(), (int) item, pos.x, pos.y) != 0);
1022 }
1023
1024 // Gets the number of items in the list control
1025 int wxListCtrl::GetItemCount() const
1026 {
1027 return m_count;
1028 }
1029
1030 wxSize wxListCtrl::GetItemSpacing() const
1031 {
1032 const int spacing = ListView_GetItemSpacing(GetHwnd(), (BOOL)HasFlag(wxLC_SMALL_ICON));
1033
1034 return wxSize(LOWORD(spacing), HIWORD(spacing));
1035 }
1036
1037 int wxListCtrl::GetItemSpacing(bool isSmall) const
1038 {
1039 return ListView_GetItemSpacing(GetHwnd(), (BOOL) isSmall);
1040 }
1041
1042 void wxListCtrl::SetItemTextColour( long item, const wxColour &col )
1043 {
1044 wxListItem info;
1045 info.m_itemId = item;
1046 info.SetTextColour( col );
1047 SetItem( info );
1048 }
1049
1050 wxColour wxListCtrl::GetItemTextColour( long item ) const
1051 {
1052 wxColour col;
1053 wxListItemInternalData *data = wxGetInternalData(this, item);
1054 if ( data && data->attr )
1055 col = data->attr->GetTextColour();
1056
1057 return col;
1058 }
1059
1060 void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
1061 {
1062 wxListItem info;
1063 info.m_itemId = item;
1064 info.SetBackgroundColour( col );
1065 SetItem( info );
1066 }
1067
1068 wxColour wxListCtrl::GetItemBackgroundColour( long item ) const
1069 {
1070 wxColour col;
1071 wxListItemInternalData *data = wxGetInternalData(this, item);
1072 if ( data && data->attr )
1073 col = data->attr->GetBackgroundColour();
1074
1075 return col;
1076 }
1077
1078 // Gets the number of selected items in the list control
1079 int wxListCtrl::GetSelectedItemCount() const
1080 {
1081 return ListView_GetSelectedCount(GetHwnd());
1082 }
1083
1084 // Gets the text colour of the listview
1085 wxColour wxListCtrl::GetTextColour() const
1086 {
1087 COLORREF ref = ListView_GetTextColor(GetHwnd());
1088 wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
1089 return col;
1090 }
1091
1092 // Sets the text colour of the listview
1093 void wxListCtrl::SetTextColour(const wxColour& col)
1094 {
1095 ListView_SetTextColor(GetHwnd(), PALETTERGB(col.Red(), col.Green(), col.Blue()));
1096 }
1097
1098 // Gets the index of the topmost visible item when in
1099 // list or report view
1100 long wxListCtrl::GetTopItem() const
1101 {
1102 return (long) ListView_GetTopIndex(GetHwnd());
1103 }
1104
1105 // Searches for an item, starting from 'item'.
1106 // 'geometry' is one of
1107 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
1108 // 'state' is a state bit flag, one or more of
1109 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
1110 // item can be -1 to find the first item that matches the
1111 // specified flags.
1112 // Returns the item or -1 if unsuccessful.
1113 long wxListCtrl::GetNextItem(long item, int geom, int state) const
1114 {
1115 long flags = 0;
1116
1117 if ( geom == wxLIST_NEXT_ABOVE )
1118 flags |= LVNI_ABOVE;
1119 if ( geom == wxLIST_NEXT_ALL )
1120 flags |= LVNI_ALL;
1121 if ( geom == wxLIST_NEXT_BELOW )
1122 flags |= LVNI_BELOW;
1123 if ( geom == wxLIST_NEXT_LEFT )
1124 flags |= LVNI_TOLEFT;
1125 if ( geom == wxLIST_NEXT_RIGHT )
1126 flags |= LVNI_TORIGHT;
1127
1128 if ( state & wxLIST_STATE_CUT )
1129 flags |= LVNI_CUT;
1130 if ( state & wxLIST_STATE_DROPHILITED )
1131 flags |= LVNI_DROPHILITED;
1132 if ( state & wxLIST_STATE_FOCUSED )
1133 flags |= LVNI_FOCUSED;
1134 if ( state & wxLIST_STATE_SELECTED )
1135 flags |= LVNI_SELECTED;
1136
1137 return (long) ListView_GetNextItem(GetHwnd(), item, flags);
1138 }
1139
1140
1141 wxImageList *wxListCtrl::GetImageList(int which) const
1142 {
1143 if ( which == wxIMAGE_LIST_NORMAL )
1144 {
1145 return m_imageListNormal;
1146 }
1147 else if ( which == wxIMAGE_LIST_SMALL )
1148 {
1149 return m_imageListSmall;
1150 }
1151 else if ( which == wxIMAGE_LIST_STATE )
1152 {
1153 return m_imageListState;
1154 }
1155 return NULL;
1156 }
1157
1158 void wxListCtrl::SetImageList(wxImageList *imageList, int which)
1159 {
1160 int flags = 0;
1161 if ( which == wxIMAGE_LIST_NORMAL )
1162 {
1163 flags = LVSIL_NORMAL;
1164 if (m_ownsImageListNormal) delete m_imageListNormal;
1165 m_imageListNormal = imageList;
1166 m_ownsImageListNormal = FALSE;
1167 }
1168 else if ( which == wxIMAGE_LIST_SMALL )
1169 {
1170 flags = LVSIL_SMALL;
1171 if (m_ownsImageListSmall) delete m_imageListSmall;
1172 m_imageListSmall = imageList;
1173 m_ownsImageListSmall = FALSE;
1174 }
1175 else if ( which == wxIMAGE_LIST_STATE )
1176 {
1177 flags = LVSIL_STATE;
1178 if (m_ownsImageListState) delete m_imageListState;
1179 m_imageListState = imageList;
1180 m_ownsImageListState = FALSE;
1181 }
1182 ListView_SetImageList(GetHwnd(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags);
1183 }
1184
1185 void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
1186 {
1187 SetImageList(imageList, which);
1188 if ( which == wxIMAGE_LIST_NORMAL )
1189 m_ownsImageListNormal = TRUE;
1190 else if ( which == wxIMAGE_LIST_SMALL )
1191 m_ownsImageListSmall = TRUE;
1192 else if ( which == wxIMAGE_LIST_STATE )
1193 m_ownsImageListState = TRUE;
1194 }
1195
1196 // ----------------------------------------------------------------------------
1197 // Operations
1198 // ----------------------------------------------------------------------------
1199
1200 // Arranges the items
1201 bool wxListCtrl::Arrange(int flag)
1202 {
1203 UINT code = 0;
1204 if ( flag == wxLIST_ALIGN_LEFT )
1205 code = LVA_ALIGNLEFT;
1206 else if ( flag == wxLIST_ALIGN_TOP )
1207 code = LVA_ALIGNTOP;
1208 else if ( flag == wxLIST_ALIGN_DEFAULT )
1209 code = LVA_DEFAULT;
1210 else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID )
1211 code = LVA_SNAPTOGRID;
1212
1213 return (ListView_Arrange(GetHwnd(), code) != 0);
1214 }
1215
1216 // Deletes an item
1217 bool wxListCtrl::DeleteItem(long item)
1218 {
1219 if ( !ListView_DeleteItem(GetHwnd(), (int) item) )
1220 {
1221 wxLogLastError(_T("ListView_DeleteItem"));
1222 return FALSE;
1223 }
1224
1225 m_count -= 1;
1226 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1227 wxT("m_count should match ListView_GetItemCount"));
1228
1229 // the virtual list control doesn't refresh itself correctly, help it
1230 if ( IsVirtual() )
1231 {
1232 // we need to refresh all the lines below the one which was deleted
1233 wxRect rectItem;
1234 if ( item > 0 && GetItemCount() )
1235 {
1236 GetItemRect(item - 1, rectItem);
1237 }
1238 else
1239 {
1240 rectItem.y =
1241 rectItem.height = 0;
1242 }
1243
1244 wxRect rectWin = GetRect();
1245 rectWin.height = rectWin.GetBottom() - rectItem.GetBottom();
1246 rectWin.y = rectItem.GetBottom();
1247
1248 RefreshRect(rectWin);
1249 }
1250
1251 return TRUE;
1252 }
1253
1254 // Deletes all items
1255 bool wxListCtrl::DeleteAllItems()
1256 {
1257 FreeAllInternalData();
1258 return ListView_DeleteAllItems(GetHwnd()) != 0;
1259 }
1260
1261 // Deletes all items
1262 bool wxListCtrl::DeleteAllColumns()
1263 {
1264 while ( m_colCount > 0 )
1265 {
1266 if ( ListView_DeleteColumn(GetHwnd(), 0) == 0 )
1267 {
1268 wxLogLastError(wxT("ListView_DeleteColumn"));
1269
1270 return FALSE;
1271 }
1272
1273 m_colCount--;
1274 }
1275
1276 wxASSERT_MSG( m_colCount == 0, wxT("no columns should be left") );
1277
1278 return TRUE;
1279 }
1280
1281 // Deletes a column
1282 bool wxListCtrl::DeleteColumn(int col)
1283 {
1284 bool success = (ListView_DeleteColumn(GetHwnd(), col) != 0);
1285
1286 if ( success && (m_colCount > 0) )
1287 m_colCount --;
1288 return success;
1289 }
1290
1291 // Clears items, and columns if there are any.
1292 void wxListCtrl::ClearAll()
1293 {
1294 DeleteAllItems();
1295 if ( m_colCount > 0 )
1296 DeleteAllColumns();
1297 }
1298
1299 wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1300 {
1301 wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) );
1302
1303 // ListView_EditLabel requires that the list has focus.
1304 SetFocus();
1305
1306 WXHWND hWnd = (WXHWND) ListView_EditLabel(GetHwnd(), item);
1307 if ( !hWnd )
1308 {
1309 // failed to start editing
1310 return NULL;
1311 }
1312
1313 // [re]create the text control wrapping the HWND we got
1314 if ( m_textCtrl )
1315 {
1316 m_textCtrl->UnsubclassWin();
1317 m_textCtrl->SetHWND(0);
1318 delete m_textCtrl;
1319 }
1320
1321 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1322 m_textCtrl->SetHWND(hWnd);
1323 m_textCtrl->SubclassWin(hWnd);
1324 m_textCtrl->SetParent(this);
1325
1326 // we must disallow TABbing away from the control while the edit contol is
1327 // shown because this leaves it in some strange state (just try removing
1328 // this line and then pressing TAB while editing an item in listctrl
1329 // inside a panel)
1330 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | wxTE_PROCESS_TAB);
1331
1332 return m_textCtrl;
1333 }
1334
1335 // End label editing, optionally cancelling the edit
1336 bool wxListCtrl::EndEditLabel(bool WXUNUSED(cancel))
1337 {
1338 wxFAIL_MSG( _T("not implemented") );
1339
1340 return FALSE;
1341 }
1342
1343 // Ensures this item is visible
1344 bool wxListCtrl::EnsureVisible(long item)
1345 {
1346 return ListView_EnsureVisible(GetHwnd(), (int) item, FALSE) != 0;
1347 }
1348
1349 // Find an item whose label matches this string, starting from the item after 'start'
1350 // or the beginning if 'start' is -1.
1351 long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
1352 {
1353 LV_FINDINFO findInfo;
1354
1355 findInfo.flags = LVFI_STRING;
1356 if ( partial )
1357 findInfo.flags |= LVFI_PARTIAL;
1358 findInfo.psz = str;
1359
1360 // ListView_FindItem() excludes the first item from search and to look
1361 // through all the items you need to start from -1 which is unnatural and
1362 // inconsistent with the generic version - so we adjust the index
1363 if (start != -1)
1364 start --;
1365 return ListView_FindItem(GetHwnd(), (int) start, &findInfo);
1366 }
1367
1368 // Find an item whose data matches this data, starting from the item after 'start'
1369 // or the beginning if 'start' is -1.
1370 // NOTE : Lindsay Mathieson - 14-July-2002
1371 // No longer use ListView_FindItem as the data attribute is now stored
1372 // in a wxListItemInternalData structure refernced by the actual lParam
1373 long wxListCtrl::FindItem(long start, long data)
1374 {
1375 long idx = start + 1;
1376 long count = GetItemCount();
1377
1378 while (idx < count)
1379 {
1380 if (GetItemData(idx) == data)
1381 return idx;
1382 idx++;
1383 };
1384
1385 return -1;
1386 }
1387
1388 // Find an item nearest this position in the specified direction, starting from
1389 // the item after 'start' or the beginning if 'start' is -1.
1390 long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
1391 {
1392 LV_FINDINFO findInfo;
1393
1394 findInfo.flags = LVFI_NEARESTXY;
1395 findInfo.pt.x = pt.x;
1396 findInfo.pt.y = pt.y;
1397 findInfo.vkDirection = VK_RIGHT;
1398
1399 if ( direction == wxLIST_FIND_UP )
1400 findInfo.vkDirection = VK_UP;
1401 else if ( direction == wxLIST_FIND_DOWN )
1402 findInfo.vkDirection = VK_DOWN;
1403 else if ( direction == wxLIST_FIND_LEFT )
1404 findInfo.vkDirection = VK_LEFT;
1405 else if ( direction == wxLIST_FIND_RIGHT )
1406 findInfo.vkDirection = VK_RIGHT;
1407
1408 return ListView_FindItem(GetHwnd(), (int) start, & findInfo);
1409 }
1410
1411 // Determines which item (if any) is at the specified point,
1412 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1413 long wxListCtrl::HitTest(const wxPoint& point, int& flags)
1414 {
1415 LV_HITTESTINFO hitTestInfo;
1416 hitTestInfo.pt.x = (int) point.x;
1417 hitTestInfo.pt.y = (int) point.y;
1418
1419 ListView_HitTest(GetHwnd(), & hitTestInfo);
1420
1421 flags = 0;
1422 if ( hitTestInfo.flags & LVHT_ABOVE )
1423 flags |= wxLIST_HITTEST_ABOVE;
1424 if ( hitTestInfo.flags & LVHT_BELOW )
1425 flags |= wxLIST_HITTEST_BELOW;
1426 if ( hitTestInfo.flags & LVHT_NOWHERE )
1427 flags |= wxLIST_HITTEST_NOWHERE;
1428 if ( hitTestInfo.flags & LVHT_ONITEMICON )
1429 flags |= wxLIST_HITTEST_ONITEMICON;
1430 if ( hitTestInfo.flags & LVHT_ONITEMLABEL )
1431 flags |= wxLIST_HITTEST_ONITEMLABEL;
1432 if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON )
1433 flags |= wxLIST_HITTEST_ONITEMSTATEICON;
1434 if ( hitTestInfo.flags & LVHT_TOLEFT )
1435 flags |= wxLIST_HITTEST_TOLEFT;
1436 if ( hitTestInfo.flags & LVHT_TORIGHT )
1437 flags |= wxLIST_HITTEST_TORIGHT;
1438
1439 return (long) hitTestInfo.iItem;
1440 }
1441
1442 // Inserts an item, returning the index of the new item if successful,
1443 // -1 otherwise.
1444 long wxListCtrl::InsertItem(wxListItem& info)
1445 {
1446 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1447
1448 LV_ITEM item;
1449 wxConvertToMSWListItem(this, info, item);
1450 item.mask &= ~LVIF_PARAM;
1451
1452 // check wether we need to allocate our internal data
1453 bool needInternalData = ((info.m_mask & wxLIST_MASK_DATA) || info.HasAttributes());
1454 if (needInternalData)
1455 {
1456 m_AnyInternalData = TRUE;
1457 item.mask |= LVIF_PARAM;
1458
1459 // internal stucture that manages data
1460 wxListItemInternalData *data = new wxListItemInternalData();
1461 item.lParam = (LPARAM) data;
1462
1463 if (info.m_mask & wxLIST_MASK_DATA)
1464 data->lParam = info.m_data;
1465
1466 // check whether it has any custom attributes
1467 if ( info.HasAttributes() )
1468 {
1469 // take copy of attributes
1470 data->attr = new wxListItemAttr(*info.GetAttributes());
1471
1472 // and remember that we have some now...
1473 m_hasAnyAttr = TRUE;
1474 }
1475 };
1476
1477 long rv = ListView_InsertItem(GetHwnd(), & item);
1478
1479 m_count++;
1480 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
1481 wxT("m_count should match ListView_GetItemCount"));
1482
1483 return rv;
1484 }
1485
1486 long wxListCtrl::InsertItem(long index, const wxString& label)
1487 {
1488 wxListItem info;
1489 info.m_text = label;
1490 info.m_mask = wxLIST_MASK_TEXT;
1491 info.m_itemId = index;
1492 return InsertItem(info);
1493 }
1494
1495 // Inserts an image item
1496 long wxListCtrl::InsertItem(long index, int imageIndex)
1497 {
1498 wxListItem info;
1499 info.m_image = imageIndex;
1500 info.m_mask = wxLIST_MASK_IMAGE;
1501 info.m_itemId = index;
1502 return InsertItem(info);
1503 }
1504
1505 // Inserts an image/string item
1506 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1507 {
1508 wxListItem info;
1509 info.m_image = imageIndex;
1510 info.m_text = label;
1511 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1512 info.m_itemId = index;
1513 return InsertItem(info);
1514 }
1515
1516 // For list view mode (only), inserts a column.
1517 long wxListCtrl::InsertColumn(long col, wxListItem& item)
1518 {
1519 LV_COLUMN lvCol;
1520 wxConvertToMSWListCol(col, item, lvCol);
1521
1522 if ( !(lvCol.mask & LVCF_WIDTH) )
1523 {
1524 // always give some width to the new column: this one is compatible
1525 // with the generic version
1526 lvCol.mask |= LVCF_WIDTH;
1527 lvCol.cx = 80;
1528 }
1529
1530 long n = ListView_InsertColumn(GetHwnd(), col, &lvCol);
1531 if ( n != -1 )
1532 {
1533 m_colCount++;
1534 }
1535 else // failed to insert?
1536 {
1537 wxLogDebug(wxT("Failed to insert the column '%s' into listview!"),
1538 lvCol.pszText);
1539 }
1540
1541 return n;
1542 }
1543
1544 long wxListCtrl::InsertColumn(long col,
1545 const wxString& heading,
1546 int format,
1547 int width)
1548 {
1549 wxListItem item;
1550 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1551 item.m_text = heading;
1552 if ( width > -1 )
1553 {
1554 item.m_mask |= wxLIST_MASK_WIDTH;
1555 item.m_width = width;
1556 }
1557 item.m_format = format;
1558
1559 return InsertColumn(col, item);
1560 }
1561
1562 // scroll the control by the given number of pixels (exception: in list view,
1563 // dx is interpreted as number of columns)
1564 bool wxListCtrl::ScrollList(int dx, int dy)
1565 {
1566 if ( !ListView_Scroll(GetHwnd(), dx, dy) )
1567 {
1568 wxLogDebug(_T("ListView_Scroll(%d, %d) failed"), dx, dy);
1569
1570 return FALSE;
1571 }
1572
1573 return TRUE;
1574 }
1575
1576 // Sort items.
1577
1578 // fn is a function which takes 3 long arguments: item1, item2, data.
1579 // item1 is the long data associated with a first item (NOT the index).
1580 // item2 is the long data associated with a second item (NOT the index).
1581 // data is the same value as passed to SortItems.
1582 // The return value is a negative number if the first item should precede the second
1583 // item, a positive number of the second item should precede the first,
1584 // or zero if the two items are equivalent.
1585
1586 // data is arbitrary data to be passed to the sort function.
1587
1588 // Internal structures for proxying the user compare function
1589 // so that we can pass it the *real* user data
1590
1591 // translate lParam data and call user func
1592 struct wxInternalDataSort
1593 {
1594 wxListCtrlCompare user_fn;
1595 long data;
1596 };
1597
1598 int CALLBACK wxInternalDataCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
1599 {
1600 struct wxInternalDataSort *internalData = (struct wxInternalDataSort *) lParamSort;
1601
1602 wxListItemInternalData *data1 = (wxListItemInternalData *) lParam1;
1603 wxListItemInternalData *data2 = (wxListItemInternalData *) lParam2;
1604
1605 long d1 = (data1 == NULL ? 0 : data1->lParam);
1606 long d2 = (data2 == NULL ? 0 : data2->lParam);
1607
1608 return internalData->user_fn(d1, d2, internalData->data);
1609
1610 };
1611
1612 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1613 {
1614 struct wxInternalDataSort internalData;
1615 internalData.user_fn = fn;
1616 internalData.data = data;
1617
1618 // WPARAM cast is needed for mingw/cygwin
1619 if ( !ListView_SortItems(GetHwnd(),
1620 wxInternalDataCompareFunc,
1621 (WPARAM) &internalData) )
1622 {
1623 wxLogDebug(_T("ListView_SortItems() failed"));
1624
1625 return FALSE;
1626 }
1627
1628 return TRUE;
1629 }
1630
1631
1632
1633 // ----------------------------------------------------------------------------
1634 // message processing
1635 // ----------------------------------------------------------------------------
1636
1637 bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1638 {
1639 if (cmd == EN_UPDATE)
1640 {
1641 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1642 event.SetEventObject( this );
1643 ProcessCommand(event);
1644 return TRUE;
1645 }
1646 else if (cmd == EN_KILLFOCUS)
1647 {
1648 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1649 event.SetEventObject( this );
1650 ProcessCommand(event);
1651 return TRUE;
1652 }
1653 else
1654 return FALSE;
1655 }
1656
1657 bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1658 {
1659
1660 // prepare the event
1661 // -----------------
1662
1663 wxListEvent event(wxEVT_NULL, m_windowId);
1664 event.SetEventObject(this);
1665
1666 wxEventType eventType = wxEVT_NULL;
1667
1668 NMHDR *nmhdr = (NMHDR *)lParam;
1669
1670 // if your compiler is as broken as this, you should really change it: this
1671 // code is needed for normal operation! #ifdef below is only useful for
1672 // automatic rebuilds which are done with a very old compiler version
1673 #ifdef HDN_BEGINTRACKA
1674
1675 // check for messages from the header (in report view)
1676 HWND hwndHdr = ListView_GetHeader(GetHwnd());
1677
1678 // is it a message from the header?
1679 if ( nmhdr->hwndFrom == hwndHdr )
1680 {
1681 HD_NOTIFY *nmHDR = (HD_NOTIFY *)nmhdr;
1682
1683 event.m_itemIndex = -1;
1684
1685 switch ( nmhdr->code )
1686 {
1687 // yet another comctl32.dll bug: under NT/W2K it sends Unicode
1688 // TRACK messages even to ANSI programs: on my system I get
1689 // HDN_BEGINTRACKW and HDN_ENDTRACKA and no HDN_TRACK at all!
1690 //
1691 // work around is to simply catch both versions and hope that it
1692 // works (why should this message exist in ANSI and Unicode is
1693 // beyond me as it doesn't deal with strings at all...)
1694 //
1695 // note that fr HDN_TRACK another possibility could be to use
1696 // HDN_ITEMCHANGING but it is sent even after HDN_ENDTRACK and when
1697 // something other than the item width changes so we'd have to
1698 // filter out the unwanted events then
1699 case HDN_BEGINTRACKA:
1700 case HDN_BEGINTRACKW:
1701 eventType = wxEVT_COMMAND_LIST_COL_BEGIN_DRAG;
1702 // fall through
1703
1704 case HDN_TRACKA:
1705 case HDN_TRACKW:
1706 if ( eventType == wxEVT_NULL )
1707 eventType = wxEVT_COMMAND_LIST_COL_DRAGGING;
1708 // fall through
1709
1710 case HDN_ENDTRACKA:
1711 case HDN_ENDTRACKW:
1712 if ( eventType == wxEVT_NULL )
1713 eventType = wxEVT_COMMAND_LIST_COL_END_DRAG;
1714
1715 event.m_item.m_width = nmHDR->pitem->cxy;
1716 event.m_col = nmHDR->iItem;
1717 break;
1718
1719 #if defined(__WXWINCE__) && _WIN32_WCE < 400
1720 case GN_CONTEXTMENU:
1721 #endif //__WXWINCE__
1722 case NM_RCLICK:
1723 {
1724 eventType = wxEVT_COMMAND_LIST_COL_RIGHT_CLICK;
1725 event.m_col = -1;
1726
1727 // find the column clicked: we have to search for it
1728 // ourselves as the notification message doesn't provide
1729 // this info
1730
1731 // where did the click occur?
1732 POINT ptClick;
1733 #if defined(__WXWINCE__) && _WIN32_WCE < 400
1734 if(nmhdr->code == GN_CONTEXTMENU) {
1735 ptClick = ((NMRGINFO*)nmhdr)->ptAction;
1736 } else
1737 #endif //__WXWINCE__
1738 if ( !::GetCursorPos(&ptClick) )
1739 {
1740 wxLogLastError(_T("GetCursorPos"));
1741 }
1742
1743 if ( !::ScreenToClient(hwndHdr, &ptClick) )
1744 {
1745 wxLogLastError(_T("ScreenToClient(listctrl header)"));
1746 }
1747
1748 event.m_pointDrag.x = ptClick.x;
1749 event.m_pointDrag.y = ptClick.y;
1750
1751 int colCount = Header_GetItemCount(hwndHdr);
1752
1753 RECT rect;
1754 for ( int col = 0; col < colCount; col++ )
1755 {
1756 if ( Header_GetItemRect(hwndHdr, col, &rect) )
1757 {
1758 if ( ::PtInRect(&rect, ptClick) )
1759 {
1760 event.m_col = col;
1761 break;
1762 }
1763 }
1764 }
1765 }
1766 break;
1767
1768 case HDN_GETDISPINFOW:
1769 {
1770 LPNMHDDISPINFOW info = (LPNMHDDISPINFOW) lParam;
1771 // This is a fix for a strange bug under XP.
1772 // Normally, info->iItem is a valid index, but
1773 // sometimes this is a silly (large) number
1774 // and when we return FALSE via wxControl::MSWOnNotify
1775 // to indicate that it hasn't yet been processed,
1776 // there's a GPF in Windows.
1777 // By returning TRUE here, we avoid further processing
1778 // of this strange message.
1779 if ( info->iItem >= GetColumnCount() )
1780 return TRUE;
1781 }
1782 // fall through
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) wxWindows 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__) && _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__) && _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 return TRUE;
2173
2174 case LVN_ENDLABELEDITA:
2175 case LVN_ENDLABELEDITW:
2176 // logic here is inversed compared to all the other messages
2177 *result = event.IsAllowed();
2178
2179 // don't keep a stale wxTextCtrl around
2180 if ( m_textCtrl )
2181 {
2182 // EDIT control will be deleted by the list control itself so
2183 // prevent us from deleting it as well
2184 m_textCtrl->UnsubclassWin();
2185 m_textCtrl->SetHWND(0);
2186 delete m_textCtrl;
2187 m_textCtrl = NULL;
2188 }
2189
2190 return TRUE;
2191 }
2192
2193 if ( processed )
2194 *result = !event.IsAllowed();
2195
2196 return processed;
2197 }
2198
2199 // see comment at the end of wxListCtrl::GetColumn()
2200 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2201
2202 WXLPARAM wxListCtrl::OnCustomDraw(WXLPARAM lParam)
2203 {
2204 LPNMLVCUSTOMDRAW lplvcd = (LPNMLVCUSTOMDRAW)lParam;
2205 NMCUSTOMDRAW& nmcd = lplvcd->nmcd;
2206 switch ( nmcd.dwDrawStage )
2207 {
2208 case CDDS_PREPAINT:
2209 // if we've got any items with non standard attributes,
2210 // notify us before painting each item
2211 //
2212 // for virtual controls, always suppose that we have attributes as
2213 // there is no way to check for this
2214 return IsVirtual() || m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2215 : CDRF_DODEFAULT;
2216
2217 case CDDS_ITEMPREPAINT:
2218 {
2219 size_t item = (size_t)nmcd.dwItemSpec;
2220 if ( item >= (size_t)GetItemCount() )
2221 {
2222 // we get this message with item == 0 for an empty control,
2223 // we must ignore it as calling OnGetItemAttr() would be
2224 // wrong
2225 return CDRF_DODEFAULT;
2226 }
2227
2228 wxListItemAttr *attr =
2229 IsVirtual() ? OnGetItemAttr(item)
2230 : wxGetInternalDataAttr(this, item);
2231
2232 if ( !attr )
2233 {
2234 // nothing to do for this item
2235 return CDRF_DODEFAULT;
2236 }
2237
2238 HFONT hFont;
2239 wxColour colText, colBack;
2240 if ( attr->HasFont() )
2241 {
2242 wxFont font = attr->GetFont();
2243 hFont = (HFONT)font.GetResourceHandle();
2244 }
2245 else
2246 {
2247 hFont = 0;
2248 }
2249
2250 if ( attr->HasTextColour() )
2251 {
2252 colText = attr->GetTextColour();
2253 }
2254 else
2255 {
2256 colText = GetTextColour();
2257 }
2258
2259 if ( attr->HasBackgroundColour() )
2260 {
2261 colBack = attr->GetBackgroundColour();
2262 }
2263 else
2264 {
2265 colBack = GetBackgroundColour();
2266 }
2267
2268 lplvcd->clrText = wxColourToRGB(colText);
2269 lplvcd->clrTextBk = wxColourToRGB(colBack);
2270
2271 // note that if we wanted to set colours for
2272 // individual columns (subitems), we would have
2273 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2274 if ( hFont )
2275 {
2276 ::SelectObject(nmcd.hdc, hFont);
2277
2278 return CDRF_NEWFONT;
2279 }
2280 }
2281 // fall through to return CDRF_DODEFAULT
2282
2283 default:
2284 return CDRF_DODEFAULT;
2285 }
2286 }
2287
2288 #endif // NM_CUSTOMDRAW supported
2289
2290 // Necessary for drawing hrules and vrules, if specified
2291 void wxListCtrl::OnPaint(wxPaintEvent& event)
2292 {
2293 wxPaintDC dc(this);
2294
2295 wxControl::OnPaint(event);
2296
2297 // Reset the device origin since it may have been set
2298 dc.SetDeviceOrigin(0, 0);
2299
2300 bool drawHRules = HasFlag(wxLC_HRULES);
2301 bool drawVRules = HasFlag(wxLC_VRULES);
2302
2303 if (!InReportView() || !drawHRules && !drawVRules)
2304 return;
2305
2306 wxPen pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
2307 dc.SetPen(pen);
2308 dc.SetBrush(* wxTRANSPARENT_BRUSH);
2309
2310 wxSize clientSize = GetClientSize();
2311 wxRect itemRect;
2312 int cy=0;
2313
2314 int itemCount = GetItemCount();
2315 int i;
2316 if (drawHRules)
2317 {
2318 long top = GetTopItem();
2319 for (i = top; i < top + GetCountPerPage() + 1; i++)
2320 {
2321 if (GetItemRect(i, itemRect))
2322 {
2323 cy = itemRect.GetTop();
2324 if (i != 0) // Don't draw the first one
2325 {
2326 dc.DrawLine(0, cy, clientSize.x, cy);
2327 }
2328 // Draw last line
2329 if (i == itemCount - 1)
2330 {
2331 cy = itemRect.GetBottom();
2332 dc.DrawLine(0, cy, clientSize.x, cy);
2333 }
2334 }
2335 }
2336 }
2337 i = itemCount - 1;
2338 if (drawVRules && (i > -1))
2339 {
2340 wxRect firstItemRect;
2341 GetItemRect(0, firstItemRect);
2342
2343 if (GetItemRect(i, itemRect))
2344 {
2345 int col;
2346 int x = itemRect.GetX();
2347 for (col = 0; col < GetColumnCount(); col++)
2348 {
2349 int colWidth = GetColumnWidth(col);
2350 x += colWidth ;
2351 dc.DrawLine(x-1, firstItemRect.GetY() - 2, x-1, itemRect.GetBottom());
2352 }
2353 }
2354 }
2355 }
2356
2357 // ----------------------------------------------------------------------------
2358 // virtual list controls
2359 // ----------------------------------------------------------------------------
2360
2361 wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2362 {
2363 // this is a pure virtual function, in fact - which is not really pure
2364 // because the controls which are not virtual don't need to implement it
2365 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
2366
2367 return wxEmptyString;
2368 }
2369
2370 int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2371 {
2372 // same as above
2373 wxFAIL_MSG( _T("wxListCtrl::OnGetItemImage not supposed to be called") );
2374
2375 return -1;
2376 }
2377
2378 wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
2379 {
2380 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
2381 _T("invalid item index in OnGetItemAttr()") );
2382
2383 // no attributes by default
2384 return NULL;
2385 }
2386
2387 void wxListCtrl::SetItemCount(long count)
2388 {
2389 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
2390
2391 if ( !::SendMessage(GetHwnd(), LVM_SETITEMCOUNT, (WPARAM)count,
2392 LVSICF_NOSCROLL | LVSICF_NOINVALIDATEALL) )
2393 {
2394 wxLogLastError(_T("ListView_SetItemCount"));
2395 }
2396 m_count = count;
2397 wxASSERT_MSG( m_count == ListView_GetItemCount(GetHwnd()),
2398 wxT("m_count should match ListView_GetItemCount"));
2399 }
2400
2401 void wxListCtrl::RefreshItem(long item)
2402 {
2403 // strangely enough, ListView_Update() results in much more flicker here
2404 // than a dumb Refresh() -- why?
2405 #if 0
2406 if ( !ListView_Update(GetHwnd(), item) )
2407 {
2408 wxLogLastError(_T("ListView_Update"));
2409 }
2410 #else // 1
2411 wxRect rect;
2412 GetItemRect(item, rect);
2413 RefreshRect(rect);
2414 #endif // 0/1
2415 }
2416
2417 void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
2418 {
2419 wxRect rect1, rect2;
2420 GetItemRect(itemFrom, rect1);
2421 GetItemRect(itemTo, rect2);
2422
2423 wxRect rect = rect1;
2424 rect.height = rect2.GetBottom() - rect1.GetTop();
2425
2426 RefreshRect(rect);
2427 }
2428
2429 // ----------------------------------------------------------------------------
2430 // internal data stuff
2431 // ----------------------------------------------------------------------------
2432
2433 static wxListItemInternalData *wxGetInternalData(HWND hwnd, long itemId)
2434 {
2435 LV_ITEM it;
2436 it.mask = LVIF_PARAM;
2437 it.iItem = itemId;
2438
2439 if ( !ListView_GetItem(hwnd, &it) )
2440 return NULL;
2441
2442 return (wxListItemInternalData *) it.lParam;
2443 };
2444
2445 static
2446 wxListItemInternalData *wxGetInternalData(const wxListCtrl *ctl, long itemId)
2447 {
2448 return wxGetInternalData(GetHwndOf(ctl), itemId);
2449 };
2450
2451 static wxListItemAttr *wxGetInternalDataAttr(wxListCtrl *ctl, long itemId)
2452 {
2453 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
2454
2455 return data ? data->attr : NULL;
2456 };
2457
2458 static void wxDeleteInternalData(wxListCtrl* ctl, long itemId)
2459 {
2460 wxListItemInternalData *data = wxGetInternalData(ctl, itemId);
2461 if (data)
2462 {
2463 LV_ITEM item;
2464 memset(&item, 0, sizeof(item));
2465 item.iItem = itemId;
2466 item.mask = LVIF_PARAM;
2467 item.lParam = (LPARAM) 0;
2468 ListView_SetItem((HWND)ctl->GetHWND(), &item);
2469 delete data;
2470 }
2471 }
2472
2473 // ----------------------------------------------------------------------------
2474 // wxWin <-> MSW items conversions
2475 // ----------------------------------------------------------------------------
2476
2477 static void wxConvertFromMSWListItem(HWND hwndListCtrl,
2478 wxListItem& info,
2479 LV_ITEM& lvItem)
2480 {
2481 wxListItemInternalData *internaldata =
2482 (wxListItemInternalData *) lvItem.lParam;
2483
2484 if (internaldata)
2485 info.m_data = internaldata->lParam;
2486
2487 info.m_mask = 0;
2488 info.m_state = 0;
2489 info.m_stateMask = 0;
2490 info.m_itemId = lvItem.iItem;
2491
2492 long oldMask = lvItem.mask;
2493
2494 bool needText = FALSE;
2495 if (hwndListCtrl != 0)
2496 {
2497 if ( lvItem.mask & LVIF_TEXT )
2498 needText = FALSE;
2499 else
2500 needText = TRUE;
2501
2502 if ( needText )
2503 {
2504 lvItem.pszText = new wxChar[513];
2505 lvItem.cchTextMax = 512;
2506 }
2507 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
2508 ::SendMessage(hwndListCtrl, LVM_GETITEM, 0, (LPARAM)& lvItem);
2509 }
2510
2511 if ( lvItem.mask & LVIF_STATE )
2512 {
2513 info.m_mask |= wxLIST_MASK_STATE;
2514
2515 if ( lvItem.stateMask & LVIS_CUT)
2516 {
2517 info.m_stateMask |= wxLIST_STATE_CUT;
2518 if ( lvItem.state & LVIS_CUT )
2519 info.m_state |= wxLIST_STATE_CUT;
2520 }
2521 if ( lvItem.stateMask & LVIS_DROPHILITED)
2522 {
2523 info.m_stateMask |= wxLIST_STATE_DROPHILITED;
2524 if ( lvItem.state & LVIS_DROPHILITED )
2525 info.m_state |= wxLIST_STATE_DROPHILITED;
2526 }
2527 if ( lvItem.stateMask & LVIS_FOCUSED)
2528 {
2529 info.m_stateMask |= wxLIST_STATE_FOCUSED;
2530 if ( lvItem.state & LVIS_FOCUSED )
2531 info.m_state |= wxLIST_STATE_FOCUSED;
2532 }
2533 if ( lvItem.stateMask & LVIS_SELECTED)
2534 {
2535 info.m_stateMask |= wxLIST_STATE_SELECTED;
2536 if ( lvItem.state & LVIS_SELECTED )
2537 info.m_state |= wxLIST_STATE_SELECTED;
2538 }
2539 }
2540
2541 if ( lvItem.mask & LVIF_TEXT )
2542 {
2543 info.m_mask |= wxLIST_MASK_TEXT;
2544 info.m_text = lvItem.pszText;
2545 }
2546 if ( lvItem.mask & LVIF_IMAGE )
2547 {
2548 info.m_mask |= wxLIST_MASK_IMAGE;
2549 info.m_image = lvItem.iImage;
2550 }
2551 if ( lvItem.mask & LVIF_PARAM )
2552 info.m_mask |= wxLIST_MASK_DATA;
2553 if ( lvItem.mask & LVIF_DI_SETITEM )
2554 info.m_mask |= wxLIST_SET_ITEM;
2555 info.m_col = lvItem.iSubItem;
2556
2557 if (needText)
2558 {
2559 if (lvItem.pszText)
2560 delete[] lvItem.pszText;
2561 }
2562 lvItem.mask = oldMask;
2563 }
2564
2565 static void wxConvertToMSWFlags(long state, long stateMask, LV_ITEM& lvItem)
2566 {
2567 if (stateMask & wxLIST_STATE_CUT)
2568 {
2569 lvItem.stateMask |= LVIS_CUT;
2570 if (state & wxLIST_STATE_CUT)
2571 lvItem.state |= LVIS_CUT;
2572 }
2573 if (stateMask & wxLIST_STATE_DROPHILITED)
2574 {
2575 lvItem.stateMask |= LVIS_DROPHILITED;
2576 if (state & wxLIST_STATE_DROPHILITED)
2577 lvItem.state |= LVIS_DROPHILITED;
2578 }
2579 if (stateMask & wxLIST_STATE_FOCUSED)
2580 {
2581 lvItem.stateMask |= LVIS_FOCUSED;
2582 if (state & wxLIST_STATE_FOCUSED)
2583 lvItem.state |= LVIS_FOCUSED;
2584 }
2585 if (stateMask & wxLIST_STATE_SELECTED)
2586 {
2587 lvItem.stateMask |= LVIS_SELECTED;
2588 if (state & wxLIST_STATE_SELECTED)
2589 lvItem.state |= LVIS_SELECTED;
2590 }
2591 }
2592
2593 static void wxConvertToMSWListItem(const wxListCtrl *ctrl,
2594 const wxListItem& info,
2595 LV_ITEM& lvItem)
2596 {
2597 lvItem.iItem = (int) info.m_itemId;
2598
2599 lvItem.iImage = info.m_image;
2600 lvItem.stateMask = 0;
2601 lvItem.state = 0;
2602 lvItem.mask = 0;
2603 lvItem.iSubItem = info.m_col;
2604
2605 if (info.m_mask & wxLIST_MASK_STATE)
2606 {
2607 lvItem.mask |= LVIF_STATE;
2608
2609 wxConvertToMSWFlags(info.m_state, info.m_stateMask, lvItem);
2610 }
2611
2612 if (info.m_mask & wxLIST_MASK_TEXT)
2613 {
2614 lvItem.mask |= LVIF_TEXT;
2615 if ( ctrl->HasFlag(wxLC_USER_TEXT) )
2616 {
2617 lvItem.pszText = LPSTR_TEXTCALLBACK;
2618 }
2619 else
2620 {
2621 // pszText is not const, hence the cast
2622 lvItem.pszText = (wxChar *)info.m_text.c_str();
2623 if ( lvItem.pszText )
2624 lvItem.cchTextMax = info.m_text.Length();
2625 else
2626 lvItem.cchTextMax = 0;
2627 }
2628 }
2629 if (info.m_mask & wxLIST_MASK_IMAGE)
2630 lvItem.mask |= LVIF_IMAGE;
2631 }
2632
2633 static void wxConvertToMSWListCol(int WXUNUSED(col), const wxListItem& item,
2634 LV_COLUMN& lvCol)
2635 {
2636 wxZeroMemory(lvCol);
2637
2638 if ( item.m_mask & wxLIST_MASK_TEXT )
2639 {
2640 lvCol.mask |= LVCF_TEXT;
2641 lvCol.pszText = (wxChar *)item.m_text.c_str(); // cast is safe
2642 }
2643
2644 if ( item.m_mask & wxLIST_MASK_FORMAT )
2645 {
2646 lvCol.mask |= LVCF_FMT;
2647
2648 if ( item.m_format == wxLIST_FORMAT_LEFT )
2649 lvCol.fmt = LVCFMT_LEFT;
2650 else if ( item.m_format == wxLIST_FORMAT_RIGHT )
2651 lvCol.fmt = LVCFMT_RIGHT;
2652 else if ( item.m_format == wxLIST_FORMAT_CENTRE )
2653 lvCol.fmt = LVCFMT_CENTER;
2654 }
2655
2656 if ( item.m_mask & wxLIST_MASK_WIDTH )
2657 {
2658 lvCol.mask |= LVCF_WIDTH;
2659 if ( item.m_width == wxLIST_AUTOSIZE)
2660 lvCol.cx = LVSCW_AUTOSIZE;
2661 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
2662 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
2663 else
2664 lvCol.cx = item.m_width;
2665 }
2666
2667 // see comment at the end of wxListCtrl::GetColumn()
2668 #ifdef NM_CUSTOMDRAW // _WIN32_IE >= 0x0300
2669 if ( item.m_mask & wxLIST_MASK_IMAGE )
2670 {
2671 if ( wxTheApp->GetComCtl32Version() >= 470 )
2672 {
2673 lvCol.mask |= LVCF_IMAGE | LVCF_FMT;
2674
2675 // we use LVCFMT_BITMAP_ON_RIGHT because thei mages on the right
2676 // seem to be generally nicer than on the left and the generic
2677 // version only draws them on the right (we don't have a flag to
2678 // specify the image location anyhow)
2679 //
2680 // we don't use LVCFMT_COL_HAS_IMAGES because it doesn't seem to
2681 // make any difference in my tests -- but maybe we should?
2682 lvCol.fmt |= LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE;
2683
2684 lvCol.iImage = item.m_image;
2685 }
2686 //else: it doesn't support item images anyhow
2687 }
2688 #endif // _WIN32_IE >= 0x0300
2689 }
2690
2691 #endif // wxUSE_LISTCTRL
2692