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