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