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