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