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