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