]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listctrl.cpp
Changed [] parameters to be unsigned.
[wxWidgets.git] / src / msw / listctrl.cpp
CommitLineData
2bda0e17
KB
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
acb62b84 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
0b5efdc7 13 #pragma implementation "listctrl.h"
2bda0e17
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
0b5efdc7 20 #pragma hdrstop
2bda0e17
KB
21#endif
22
23#ifndef WX_PRECOMP
0b5efdc7 24 #include "wx/wx.h"
2bda0e17
KB
25#endif
26
27#if defined(__WIN95__)
28
29#include "wx/listctrl.h"
f8352807 30#include "wx/log.h"
2bda0e17
KB
31
32#include "wx/msw/private.h"
33
57c208c5 34#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
0b5efdc7 35 #include <commctrl.h>
2bda0e17
KB
36#endif
37
57c208c5 38#ifndef __TWIN32__
0b5efdc7
VZ
39 #ifdef __GNUWIN32__
40 #include "wx/msw/gnuwin32/extra.h"
41 #endif
57c208c5 42#endif
2bda0e17
KB
43
44static void wxConvertToMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& tvItem);
45static void wxConvertFromMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& tvItem, HWND getFullInfo = 0);
46
47#if !USE_SHARED_LIBRARY
0b5efdc7
VZ
48 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
49 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
50#endif // USE_SHARED_LIBRARY
2bda0e17 51
0b5efdc7 52wxListCtrl::wxListCtrl()
2bda0e17 53{
0b5efdc7
VZ
54 m_imageListNormal = NULL;
55 m_imageListSmall = NULL;
56 m_imageListState = NULL;
57 m_baseStyle = 0;
2bda0e17 58 m_colCount = 0;
bbcdf8bc 59 m_textCtrl = NULL;
2bda0e17
KB
60}
61
0b5efdc7
VZ
62bool wxListCtrl::Create(wxWindow *parent,
63 wxWindowID id,
64 const wxPoint& pos,
65 const wxSize& size,
66 long style,
67 const wxValidator& validator,
68 const wxString& name)
2bda0e17 69{
0b5efdc7
VZ
70 m_imageListNormal = NULL;
71 m_imageListSmall = NULL;
72 m_imageListState = NULL;
73 m_textCtrl = NULL;
74 m_colCount = 0;
75
76 SetValidator(validator);
77 SetName(name);
2bda0e17 78
0b5efdc7
VZ
79 int x = pos.x;
80 int y = pos.y;
81 int width = size.x;
82 int height = size.y;
2bda0e17 83
0b5efdc7 84 m_windowStyle = style;
2bda0e17 85
0b5efdc7 86 SetParent(parent);
2bda0e17 87
0b5efdc7
VZ
88 if (width <= 0)
89 width = 100;
90 if (height <= 0)
91 height = 30;
92 if (x < 0)
93 x = 0;
94 if (y < 0)
95 y = 0;
2bda0e17 96
0b5efdc7 97 m_windowId = (id == -1) ? NewControlId() : id;
2bda0e17 98
0b5efdc7 99 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP;
2bda0e17 100
0b5efdc7
VZ
101 bool want3D;
102 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
2bda0e17 103
0b5efdc7
VZ
104 // Even with extended styles, need to combine with WS_BORDER
105 // for them to look right.
106 if ( want3D || wxStyleHasBorder(m_windowStyle) )
107 wstyle |= WS_BORDER;
2bda0e17 108
0b5efdc7
VZ
109 wstyle |= LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS ;
110 m_baseStyle = wstyle;
2bda0e17 111
0b5efdc7
VZ
112 long oldStyle = 0; // Dummy
113 wstyle |= ConvertToMSWStyle(oldStyle, m_windowStyle);
2bda0e17 114
0b5efdc7
VZ
115 // Create the ListView control.
116 m_hWnd = (WXHWND)CreateWindowEx(exStyle,
117 WC_LISTVIEW,
118 "",
119 wstyle,
120 x, y, width, height,
121 (HWND) parent->GetHWND(),
122 (HMENU)m_windowId,
123 wxGetInstance(),
124 NULL);
f8352807 125
0b5efdc7
VZ
126 if ( !m_hWnd ) {
127 wxLogError("Can't create list control window.");
128
129 return FALSE;
130 }
131
132 // for comctl32.dll v 4.70+ we want to have this attribute because it's
133 // prettier (and also because wxGTK does it like this)
134#ifdef ListView_SetExtendedListViewStyle
135 if ( wstyle & LVS_REPORT )
136 {
137 ListView_SetExtendedListViewStyle((HWND)GetHWND(),
138 LVS_EX_FULLROWSELECT);
139 }
140#endif // ListView_SetExtendedListViewStyle
f8352807 141
0b5efdc7
VZ
142 wxSystemSettings settings;
143 SetBackgroundColour(settings.GetSystemColour(wxSYS_COLOUR_WINDOW));
144 SetForegroundColour(parent->GetForegroundColour());
f8352807 145
0b5efdc7 146 if (parent) parent->AddChild(this);
2bda0e17 147
0b5efdc7 148 SubclassWin((WXHWND) m_hWnd);
2bda0e17 149
0b5efdc7 150 return TRUE;
2bda0e17
KB
151}
152
0b5efdc7 153wxListCtrl::~wxListCtrl()
2bda0e17 154{
bbcdf8bc
JS
155 if (m_textCtrl)
156 {
0b5efdc7
VZ
157 m_textCtrl->UnsubclassWin();
158 m_textCtrl->SetHWND(0);
159 delete m_textCtrl;
160 m_textCtrl = NULL;
bbcdf8bc 161 }
2bda0e17
KB
162}
163
164// Add or remove a single window style
debe6624 165void wxListCtrl::SetSingleStyle(long style, bool add)
2bda0e17 166{
0b5efdc7 167 long flag = GetWindowStyleFlag();
2bda0e17 168
0b5efdc7 169 // Get rid of conflicting styles
acb62b84
VZ
170 if ( add )
171 {
0b5efdc7
VZ
172 if ( style & wxLC_MASK_TYPE)
173 flag = flag & ~wxLC_MASK_TYPE ;
174 if ( style & wxLC_MASK_ALIGN )
175 flag = flag & ~wxLC_MASK_ALIGN ;
176 if ( style & wxLC_MASK_SORT )
177 flag = flag & ~wxLC_MASK_SORT ;
acb62b84 178 }
2bda0e17 179
0b5efdc7
VZ
180 if ( flag & style )
181 {
182 if ( !add )
183 flag -= style;
184 }
185 else
186 {
187 if ( add )
188 {
189 flag |= style;
190 }
191 }
192
193 m_windowStyle = flag;
2bda0e17 194
0b5efdc7 195 RecreateWindow();
2bda0e17
KB
196}
197
198// Set the whole window style
debe6624 199void wxListCtrl::SetWindowStyleFlag(long flag)
2bda0e17 200{
0b5efdc7 201 m_windowStyle = flag;
2bda0e17 202
0b5efdc7
VZ
203 RecreateWindow();
204}
205
206void wxListCtrl::RecreateWindow()
207{
208 if ( GetHWND() )
209 {
210 long oldStyle = 0;
211 long style = ConvertToMSWStyle(oldStyle, m_windowStyle);
212 style |= m_baseStyle;
213 // ::SetWindowLong((HWND) GetHWND(), GWL_STYLE, style);
214
215 // The following recreation of the window appears to be necessary
216 // because SetWindowLong doesn't seem to do it.
217
218 int x, y, width, height;
219 GetPosition(&x, &y);
220 GetSize(&width, &height);
221
222 UnsubclassWin();
223 ::DestroyWindow((HWND) GetHWND());
224
225 // Experimental
226 // Recreate the ListView control: unfortunately I can't
227 // make it work by using SetWindowLong.
228 bool want3D;
229 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D) ;
230 HWND hWndListControl = CreateWindowEx(exStyle,
231 WC_LISTVIEW,
232 "",
233 style,
234 x, y, width, height,
235 (HWND) GetParent()->GetHWND(),
236 (HMENU)m_windowId,
237 wxGetInstance(),
238 NULL );
239
240 m_hWnd = (WXHWND) hWndListControl;
241 SubclassWin((WXHWND) m_hWnd);
242
243#ifdef ListView_SetExtendedListViewStyle
244 if ( style & LVS_REPORT )
245 {
246 ListView_SetExtendedListViewStyle((HWND)GetHWND(),
247 LVS_EX_FULLROWSELECT);
248 }
249#endif // ListView_SetExtendedListViewStyle
250
251 if ( m_imageListNormal )
252 SetImageList(m_imageListNormal, wxIMAGE_LIST_NORMAL);
253 if ( m_imageListSmall )
254 SetImageList(m_imageListSmall, wxIMAGE_LIST_SMALL);
255 if ( m_imageListState )
256 SetImageList(m_imageListState, wxIMAGE_LIST_STATE);
257 }
2bda0e17
KB
258}
259
0b5efdc7
VZ
260// Can be just a single style, or a bitlist
261long wxListCtrl::ConvertToMSWStyle(long& oldStyle, long style) const
2bda0e17 262{
0b5efdc7
VZ
263 long wstyle = 0;
264 if ( style & wxLC_ICON )
265 {
266 if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON )
267 oldStyle -= LVS_SMALLICON;
268 if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT )
269 oldStyle -= LVS_REPORT;
270 if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST )
271 oldStyle -= LVS_LIST;
272 wstyle |= LVS_ICON;
273 }
274
275 if ( style & wxLC_SMALL_ICON )
276 {
277 if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON )
278 oldStyle -= LVS_ICON;
279 if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT )
280 oldStyle -= LVS_REPORT;
281 if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST )
282 oldStyle -= LVS_LIST;
283 wstyle |= LVS_SMALLICON;
284 }
285
286 if ( style & wxLC_LIST )
287 {
288 if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON )
289 oldStyle -= LVS_ICON;
290 if ( (oldStyle & LVS_TYPEMASK) == LVS_REPORT )
291 oldStyle -= LVS_REPORT;
292 if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON )
293 oldStyle -= LVS_SMALLICON;
294 wstyle |= LVS_LIST;
295 }
2bda0e17 296
0b5efdc7
VZ
297 if ( style & wxLC_REPORT )
298 {
299 if ( (oldStyle & LVS_TYPEMASK) == LVS_ICON )
300 oldStyle -= LVS_ICON;
301 if ( (oldStyle & LVS_TYPEMASK) == LVS_LIST )
302 oldStyle -= LVS_LIST;
303 if ( (oldStyle & LVS_TYPEMASK) == LVS_SMALLICON )
304 oldStyle -= LVS_SMALLICON;
305
306 wstyle |= LVS_REPORT;
307 }
2bda0e17 308
0b5efdc7
VZ
309 if ( style & wxLC_ALIGN_LEFT )
310 {
311 if ( oldStyle & LVS_ALIGNTOP )
312 oldStyle -= LVS_ALIGNTOP;
313 wstyle |= LVS_ALIGNLEFT;
314 }
2bda0e17 315
0b5efdc7
VZ
316 if ( style & wxLC_ALIGN_TOP )
317 {
318 if ( oldStyle & LVS_ALIGNLEFT )
319 oldStyle -= LVS_ALIGNLEFT;
320 wstyle |= LVS_ALIGNTOP;
321 }
2bda0e17 322
0b5efdc7
VZ
323 if ( style & wxLC_AUTOARRANGE )
324 wstyle |= LVS_AUTOARRANGE;
2bda0e17 325
0b5efdc7
VZ
326 // Apparently, no such style (documentation wrong?)
327 /*
328 if ( style & wxLC_BUTTON )
329 wstyle |= LVS_BUTTON;
330 */
2bda0e17 331
0b5efdc7
VZ
332 if ( style & wxLC_NO_SORT_HEADER )
333 wstyle |= LVS_NOSORTHEADER;
2bda0e17 334
0b5efdc7
VZ
335 if ( style & wxLC_NO_HEADER )
336 wstyle |= LVS_NOCOLUMNHEADER;
337
338 if ( style & wxLC_EDIT_LABELS )
339 wstyle |= LVS_EDITLABELS;
340
341 if ( style & wxLC_SINGLE_SEL )
342 wstyle |= LVS_SINGLESEL;
343
344 if ( style & wxLC_SORT_ASCENDING )
345 {
346 if ( oldStyle & LVS_SORTDESCENDING )
347 oldStyle -= LVS_SORTDESCENDING;
348 wstyle |= LVS_SORTASCENDING;
349 }
350
351 if ( style & wxLC_SORT_DESCENDING )
352 {
353 if ( oldStyle & LVS_SORTASCENDING )
354 oldStyle -= LVS_SORTASCENDING;
355 wstyle |= LVS_SORTDESCENDING;
356 }
357
358 return wstyle;
2bda0e17
KB
359}
360
361// Sets the background colour (GetBackgroundColour already implicit in
362// wxWindow class)
cc2b7472 363bool wxListCtrl::SetBackgroundColour(const wxColour& col)
2bda0e17 364{
cc2b7472
VZ
365 if ( !wxWindow::SetBackgroundColour(col) )
366 return FALSE;
2bda0e17 367
0b5efdc7 368 ListView_SetBkColor((HWND) GetHWND(), PALETTERGB(col.Red(), col.Green(), col.Blue()));
cc2b7472
VZ
369
370 return TRUE;
2bda0e17
KB
371}
372
373// Gets information about this column
debe6624 374bool wxListCtrl::GetColumn(int col, wxListItem& item) const
2bda0e17 375{
0b5efdc7
VZ
376 LV_COLUMN lvCol;
377 lvCol.mask = 0;
378 lvCol.fmt = 0;
379 lvCol.pszText = NULL;
380
381 if ( item.m_mask & wxLIST_MASK_TEXT )
382 {
383 lvCol.mask |= LVCF_TEXT;
384 lvCol.pszText = new char[513];
385 lvCol.cchTextMax = 512;
386 }
387
388 bool success = (ListView_GetColumn((HWND) GetHWND(), col, & lvCol) != 0);
389
390 // item.m_subItem = lvCol.iSubItem;
391 item.m_width = lvCol.cx;
392
393 if ( (item.m_mask & wxLIST_MASK_TEXT) && lvCol.pszText )
394 {
395 item.m_text = lvCol.pszText;
396 delete[] lvCol.pszText;
397 }
398
399 if ( item.m_mask & wxLIST_MASK_FORMAT )
400 {
401 if (lvCol.fmt == LVCFMT_LEFT)
402 item.m_format = wxLIST_FORMAT_LEFT;
403 else if (lvCol.fmt == LVCFMT_RIGHT)
404 item.m_format = wxLIST_FORMAT_RIGHT;
405 else if (lvCol.fmt == LVCFMT_CENTER)
406 item.m_format = wxLIST_FORMAT_CENTRE;
2bda0e17
KB
407 }
408
0b5efdc7 409 return success;
2bda0e17
KB
410}
411
412// Sets information about this column
debe6624 413bool wxListCtrl::SetColumn(int col, wxListItem& item)
2bda0e17 414{
0b5efdc7
VZ
415 LV_COLUMN lvCol;
416 lvCol.mask = 0;
417 lvCol.fmt = 0;
418 lvCol.pszText = NULL;
419
420 if ( item.m_mask & wxLIST_MASK_TEXT )
421 {
422 lvCol.mask |= LVCF_TEXT;
423 lvCol.pszText = WXSTRINGCAST item.m_text;
424 lvCol.cchTextMax = 0; // Ignored
425 }
426 if ( item.m_mask & wxLIST_MASK_FORMAT )
427 {
428 lvCol.mask |= LVCF_FMT;
429
430 if ( item.m_format == wxLIST_FORMAT_LEFT )
431 lvCol.fmt = LVCFMT_LEFT;
432 if ( item.m_format == wxLIST_FORMAT_RIGHT )
433 lvCol.fmt = LVCFMT_RIGHT;
434 if ( item.m_format == wxLIST_FORMAT_CENTRE )
435 lvCol.fmt = LVCFMT_CENTER;
2bda0e17
KB
436 }
437
0b5efdc7
VZ
438 if ( item.m_mask & wxLIST_MASK_WIDTH )
439 {
440 lvCol.mask |= LVCF_WIDTH;
441 lvCol.cx = item.m_width;
2bda0e17 442
0b5efdc7
VZ
443 if ( lvCol.cx == wxLIST_AUTOSIZE)
444 lvCol.cx = LVSCW_AUTOSIZE;
445 else if ( lvCol.cx == wxLIST_AUTOSIZE_USEHEADER)
446 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
447 }
448 lvCol.mask |= LVCF_SUBITEM;
449 lvCol.iSubItem = col;
450 return (ListView_SetColumn((HWND) GetHWND(), col, & lvCol) != 0);
2bda0e17
KB
451}
452
453// Gets the column width
debe6624 454int wxListCtrl::GetColumnWidth(int col) const
2bda0e17 455{
0b5efdc7 456 return ListView_GetColumnWidth((HWND) GetHWND(), col);
2bda0e17
KB
457}
458
459// Sets the column width
debe6624 460bool wxListCtrl::SetColumnWidth(int col, int width)
2bda0e17 461{
0b5efdc7
VZ
462 int col2 = col;
463 if ( m_windowStyle & wxLC_LIST )
464 col2 = -1;
2bda0e17 465
0b5efdc7
VZ
466 int width2 = width;
467 if ( width2 == wxLIST_AUTOSIZE)
468 width2 = LVSCW_AUTOSIZE;
469 else if ( width2 == wxLIST_AUTOSIZE_USEHEADER)
470 width2 = LVSCW_AUTOSIZE_USEHEADER;
2bda0e17 471
0b5efdc7 472 return (ListView_SetColumnWidth((HWND) GetHWND(), col2, width2) != 0);
2bda0e17
KB
473}
474
475// Gets the number of items that can fit vertically in the
476// visible area of the list control (list or report view)
477// or the total number of items in the list control (icon
478// or small icon view)
479int wxListCtrl::GetCountPerPage(void) const
480{
0b5efdc7 481 return ListView_GetCountPerPage((HWND) GetHWND());
2bda0e17
KB
482}
483
484// Gets the edit control for editing labels.
bbcdf8bc 485wxTextCtrl* wxListCtrl::GetEditControl(void) const
2bda0e17 486{
bbcdf8bc 487 return m_textCtrl;
2bda0e17
KB
488}
489
490// Gets information about the item
491bool wxListCtrl::GetItem(wxListItem& info) const
492{
0b5efdc7 493 LV_ITEM lvItem;
cf0b3979 494#ifdef __GNUWIN32__
0b5efdc7 495 memset(&lvItem, 0, sizeof(lvItem));
386af6a2 496#else
0b5efdc7 497 ZeroMemory(&lvItem, sizeof(lvItem)); // must set all fields to 0
386af6a2 498#endif
2bda0e17 499
0b5efdc7
VZ
500 lvItem.iItem = info.m_itemId;
501
502 if ( info.m_mask & wxLIST_MASK_TEXT )
503 {
504 lvItem.mask |= LVIF_TEXT;
505 lvItem.pszText = new char[513];
506 lvItem.cchTextMax = 512;
507 }
508 else
509 {
510 lvItem.pszText = NULL;
511 }
512
513 if (info.m_mask & wxLIST_MASK_DATA)
514 lvItem.mask |= LVIF_PARAM ;
515
516 if ( info.m_mask & wxLIST_MASK_STATE )
517 {
518 lvItem.mask |= LVIF_STATE;
519 // the other bits are hardly interesting anyhow
520 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
521 }
522
523 bool success = ListView_GetItem((HWND)GetHWND(), &lvItem) != 0;
524 if ( !success )
525 {
526 wxLogError(_("Couldn't retrieve information about list control item %d."),
527 lvItem.iItem);
528 }
529 else
530 {
531 wxConvertFromMSWListItem(this, info, lvItem);
532 }
533
534 if (lvItem.pszText)
535 delete[] lvItem.pszText;
536
537 return success;
2bda0e17
KB
538}
539
540// Sets information about the item
541bool wxListCtrl::SetItem(wxListItem& info)
542{
0b5efdc7 543 LV_ITEM item;
2bda0e17 544 wxConvertToMSWListItem(this, info, item);
0b5efdc7
VZ
545 item.cchTextMax = 0;
546 return (ListView_SetItem((HWND) GetHWND(), &item) != 0);
2bda0e17
KB
547}
548
debe6624 549long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId)
2bda0e17 550{
0b5efdc7
VZ
551 wxListItem info;
552 info.m_text = label;
553 info.m_mask = wxLIST_MASK_TEXT;
554 info.m_itemId = index;
555 info.m_col = col;
556 if ( imageId > -1 )
557 {
558 info.m_image = imageId;
559 info.m_mask |= wxLIST_MASK_IMAGE;
560 }
561 return SetItem(info);
2bda0e17
KB
562}
563
564
565// Gets the item state
debe6624 566int wxListCtrl::GetItemState(long item, long stateMask) const
2bda0e17 567{
0b5efdc7 568 wxListItem info;
2bda0e17 569
0b5efdc7
VZ
570 info.m_mask = wxLIST_MASK_STATE ;
571 info.m_stateMask = stateMask;
572 info.m_itemId = item;
2bda0e17 573
0b5efdc7
VZ
574 if (!GetItem(info))
575 return 0;
2bda0e17 576
0b5efdc7 577 return info.m_state;
2bda0e17
KB
578}
579
580// Sets the item state
debe6624 581bool wxListCtrl::SetItemState(long item, long state, long stateMask)
2bda0e17 582{
0b5efdc7 583 wxListItem info;
2bda0e17 584
0b5efdc7
VZ
585 info.m_mask = wxLIST_MASK_STATE ;
586 info.m_state = state;
587 info.m_stateMask = stateMask;
588 info.m_itemId = item;
2bda0e17 589
0b5efdc7 590 return SetItem(info);
2bda0e17
KB
591}
592
593// Sets the item image
debe6624 594bool wxListCtrl::SetItemImage(long item, int image, int selImage)
2bda0e17 595{
0b5efdc7 596 wxListItem info;
2bda0e17 597
0b5efdc7
VZ
598 info.m_mask = wxLIST_MASK_IMAGE ;
599 info.m_image = image;
600 info.m_itemId = item;
2bda0e17 601
0b5efdc7 602 return SetItem(info);
2bda0e17
KB
603}
604
605// Gets the item text
debe6624 606wxString wxListCtrl::GetItemText(long item) const
2bda0e17 607{
0b5efdc7 608 wxListItem info;
2bda0e17 609
0b5efdc7
VZ
610 info.m_mask = wxLIST_MASK_TEXT ;
611 info.m_itemId = item;
2bda0e17 612
0b5efdc7
VZ
613 if (!GetItem(info))
614 return wxString("");
615 return info.m_text;
2bda0e17
KB
616}
617
618// Sets the item text
debe6624 619void wxListCtrl::SetItemText(long item, const wxString& str)
2bda0e17 620{
0b5efdc7 621 wxListItem info;
2bda0e17 622
0b5efdc7
VZ
623 info.m_mask = wxLIST_MASK_TEXT ;
624 info.m_itemId = item;
625 info.m_text = str;
2bda0e17 626
0b5efdc7 627 SetItem(info);
2bda0e17
KB
628}
629
630// Gets the item data
debe6624 631long wxListCtrl::GetItemData(long item) const
2bda0e17 632{
0b5efdc7 633 wxListItem info;
2bda0e17 634
0b5efdc7
VZ
635 info.m_mask = wxLIST_MASK_DATA ;
636 info.m_itemId = item;
2bda0e17 637
0b5efdc7
VZ
638 if (!GetItem(info))
639 return 0;
640 return info.m_data;
2bda0e17
KB
641}
642
643// Sets the item data
debe6624 644bool wxListCtrl::SetItemData(long item, long data)
2bda0e17 645{
0b5efdc7 646 wxListItem info;
2bda0e17 647
0b5efdc7
VZ
648 info.m_mask = wxLIST_MASK_DATA ;
649 info.m_itemId = item;
650 info.m_data = data;
2bda0e17 651
0b5efdc7 652 return SetItem(info);
2bda0e17
KB
653}
654
655// Gets the item rectangle
16e93305 656bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
2bda0e17 657{
0b5efdc7 658 RECT rect2;
2bda0e17 659
0b5efdc7
VZ
660 int code2 = LVIR_BOUNDS;
661 if ( code == wxLIST_RECT_BOUNDS )
662 code2 = LVIR_BOUNDS;
663 else if ( code == wxLIST_RECT_ICON )
664 code2 = LVIR_ICON;
665 else if ( code == wxLIST_RECT_LABEL )
666 code2 = LVIR_LABEL;
2bda0e17 667
0b5efdc7 668 bool success = (ListView_GetItemRect((HWND) GetHWND(), (int) item, &rect2, code2) != 0);
2bda0e17 669
0b5efdc7
VZ
670 rect.x = rect2.left;
671 rect.y = rect2.top;
672 rect.width = rect2.right - rect2.left;
673 rect.height = rect2.bottom - rect2.left;
674 return success;
2bda0e17
KB
675}
676
677// Gets the item position
debe6624 678bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
2bda0e17 679{
0b5efdc7 680 POINT pt;
2bda0e17 681
0b5efdc7 682 bool success = (ListView_GetItemPosition((HWND) GetHWND(), (int) item, &pt) != 0);
2bda0e17 683
0b5efdc7
VZ
684 pos.x = pt.x; pos.y = pt.y;
685 return success;
2bda0e17
KB
686}
687
688// Sets the item position.
debe6624 689bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
2bda0e17 690{
0b5efdc7 691 return (ListView_SetItemPosition((HWND) GetHWND(), (int) item, pos.x, pos.y) != 0);
2bda0e17
KB
692}
693
694// Gets the number of items in the list control
695int wxListCtrl::GetItemCount(void) const
696{
0b5efdc7 697 return ListView_GetItemCount((HWND) GetHWND());
2bda0e17
KB
698}
699
700// Retrieves the spacing between icons in pixels.
701// If small is TRUE, gets the spacing for the small icon
702// view, otherwise the large icon view.
703int wxListCtrl::GetItemSpacing(bool isSmall) const
704{
0b5efdc7 705 return ListView_GetItemSpacing((HWND) GetHWND(), (BOOL) isSmall);
2bda0e17
KB
706}
707
708// Gets the number of selected items in the list control
709int wxListCtrl::GetSelectedItemCount(void) const
710{
0b5efdc7 711 return ListView_GetSelectedCount((HWND) GetHWND());
2bda0e17
KB
712}
713
714// Gets the text colour of the listview
715wxColour wxListCtrl::GetTextColour(void) const
716{
0b5efdc7
VZ
717 COLORREF ref = ListView_GetTextColor((HWND) GetHWND());
718 wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
719 return col;
2bda0e17
KB
720}
721
722// Sets the text colour of the listview
723void wxListCtrl::SetTextColour(const wxColour& col)
724{
0b5efdc7 725 ListView_SetTextColor((HWND) GetHWND(), PALETTERGB(col.Red(), col.Blue(), col.Green()));
2bda0e17
KB
726}
727
728// Gets the index of the topmost visible item when in
729// list or report view
730long wxListCtrl::GetTopItem(void) const
731{
0b5efdc7 732 return (long) ListView_GetTopIndex((HWND) GetHWND());
2bda0e17
KB
733}
734
735// Searches for an item, starting from 'item'.
736// 'geometry' is one of
737// wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
738// 'state' is a state bit flag, one or more of
739// wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
740// item can be -1 to find the first item that matches the
741// specified flags.
742// Returns the item or -1 if unsuccessful.
debe6624 743long wxListCtrl::GetNextItem(long item, int geom, int state) const
2bda0e17 744{
0b5efdc7 745 long flags = 0;
2bda0e17 746
0b5efdc7
VZ
747 if ( geom == wxLIST_NEXT_ABOVE )
748 flags |= LVNI_ABOVE;
749 if ( geom == wxLIST_NEXT_ALL )
750 flags |= LVNI_ALL;
751 if ( geom == wxLIST_NEXT_BELOW )
752 flags |= LVNI_BELOW;
753 if ( geom == wxLIST_NEXT_LEFT )
754 flags |= LVNI_TOLEFT;
755 if ( geom == wxLIST_NEXT_RIGHT )
756 flags |= LVNI_TORIGHT;
2bda0e17 757
0b5efdc7
VZ
758 if ( state & wxLIST_STATE_CUT )
759 flags |= LVNI_CUT;
760 if ( state & wxLIST_STATE_DROPHILITED )
761 flags |= LVNI_DROPHILITED;
762 if ( state & wxLIST_STATE_FOCUSED )
763 flags |= LVNI_FOCUSED;
764 if ( state & wxLIST_STATE_SELECTED )
765 flags |= LVNI_SELECTED;
2bda0e17 766
0b5efdc7 767 return (long) ListView_GetNextItem((HWND) GetHWND(), item, flags);
2bda0e17
KB
768}
769
770
debe6624 771wxImageList *wxListCtrl::GetImageList(int which) const
2bda0e17 772{
0b5efdc7 773 if ( which == wxIMAGE_LIST_NORMAL )
2bda0e17 774 {
0b5efdc7
VZ
775 return m_imageListNormal;
776 }
777 else if ( which == wxIMAGE_LIST_SMALL )
2bda0e17 778 {
0b5efdc7
VZ
779 return m_imageListSmall;
780 }
781 else if ( which == wxIMAGE_LIST_STATE )
2bda0e17 782 {
0b5efdc7
VZ
783 return m_imageListState;
784 }
785 return NULL;
2bda0e17
KB
786}
787
debe6624 788void wxListCtrl::SetImageList(wxImageList *imageList, int which)
2bda0e17 789{
0b5efdc7
VZ
790 int flags = 0;
791 if ( which == wxIMAGE_LIST_NORMAL )
2bda0e17 792 {
0b5efdc7
VZ
793 flags = LVSIL_NORMAL;
794 m_imageListNormal = imageList;
795 }
796 else if ( which == wxIMAGE_LIST_SMALL )
2bda0e17 797 {
0b5efdc7
VZ
798 flags = LVSIL_SMALL;
799 m_imageListSmall = imageList;
800 }
801 else if ( which == wxIMAGE_LIST_STATE )
2bda0e17 802 {
0b5efdc7
VZ
803 flags = LVSIL_STATE;
804 m_imageListState = imageList;
805 }
806 ListView_SetImageList((HWND) GetHWND(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags);
2bda0e17
KB
807}
808
809// Operations
810////////////////////////////////////////////////////////////////////////////
811
812// Arranges the items
debe6624 813bool wxListCtrl::Arrange(int flag)
2bda0e17 814{
0b5efdc7
VZ
815 UINT code = 0;
816 if ( flag == wxLIST_ALIGN_LEFT )
817 code = LVA_ALIGNLEFT;
818 else if ( flag == wxLIST_ALIGN_TOP )
819 code = LVA_ALIGNTOP;
820 else if ( flag == wxLIST_ALIGN_DEFAULT )
821 code = LVA_DEFAULT;
822 else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID )
823 code = LVA_SNAPTOGRID;
2bda0e17 824
0b5efdc7 825 return (ListView_Arrange((HWND) GetHWND(), code) != 0);
2bda0e17
KB
826}
827
828// Deletes an item
debe6624 829bool wxListCtrl::DeleteItem(long item)
2bda0e17 830{
0b5efdc7 831 return (ListView_DeleteItem((HWND) GetHWND(), (int) item) != 0);
2bda0e17
KB
832}
833
834// Deletes all items
0b5efdc7 835bool wxListCtrl::DeleteAllItems()
2bda0e17 836{
0b5efdc7 837 return (ListView_DeleteAllItems((HWND) GetHWND()) != 0);
2bda0e17
KB
838}
839
840// Deletes all items
0b5efdc7 841bool wxListCtrl::DeleteAllColumns()
2bda0e17 842{
0b5efdc7 843 int i;
2bda0e17
KB
844 for ( i = 0; i < m_colCount; i++)
845 {
0b5efdc7
VZ
846 if (ListView_DeleteColumn((HWND) GetHWND(), 0) != 0)
847 m_colCount --;
2bda0e17
KB
848 }
849 return (m_colCount == 0);
850}
851
852// Deletes a column
debe6624 853bool wxListCtrl::DeleteColumn(int col)
2bda0e17 854{
0b5efdc7 855 bool success = (ListView_DeleteColumn((HWND) GetHWND(), col) != 0);
2bda0e17
KB
856
857 if ( success && (m_colCount > 0) )
858 m_colCount --;
859 return success;
860}
861
862// Clears items, and columns if there are any.
0b5efdc7 863void wxListCtrl::ClearAll()
2bda0e17
KB
864{
865 DeleteAllItems();
866 if ( m_colCount > 0 )
867 DeleteAllColumns();
868}
869
bbcdf8bc
JS
870wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
871{
872 wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) );
873
874 HWND hWnd = (HWND) ListView_EditLabel((HWND) GetHWND(), item);
875
876 if (m_textCtrl)
877 {
0b5efdc7
VZ
878 m_textCtrl->UnsubclassWin();
879 m_textCtrl->SetHWND(0);
880 delete m_textCtrl;
881 m_textCtrl = NULL;
bbcdf8bc
JS
882 }
883
884 m_textCtrl = (wxTextCtrl*) textControlClass->CreateObject();
885 m_textCtrl->SetHWND((WXHWND) hWnd);
886 m_textCtrl->SubclassWin((WXHWND) hWnd);
887
888 return m_textCtrl;
889}
890
891// End label editing, optionally cancelling the edit
892bool wxListCtrl::EndEditLabel(bool cancel)
2bda0e17 893{
341287bf 894 wxFAIL;
bbcdf8bc 895
0b5efdc7
VZ
896 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
897 * ???
898 bool success = (ListView_EndEditLabelNow((HWND) GetHWND(), cancel) != 0);
899
900 if (m_textCtrl)
901 {
902 m_textCtrl->UnsubclassWin();
903 m_textCtrl->SetHWND(0);
904 delete m_textCtrl;
905 m_textCtrl = NULL;
906 }
907 return success;
908 */
909 return FALSE;
2bda0e17
KB
910}
911
bbcdf8bc 912
2bda0e17 913// Ensures this item is visible
debe6624 914bool wxListCtrl::EnsureVisible(long item)
2bda0e17 915{
0b5efdc7 916 return (ListView_EnsureVisible((HWND) GetHWND(), (int) item, FALSE) != 0);
2bda0e17
KB
917}
918
919// Find an item whose label matches this string, starting from the item after 'start'
920// or the beginning if 'start' is -1.
debe6624 921long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
2bda0e17 922{
0b5efdc7 923 LV_FINDINFO findInfo;
2bda0e17 924
0b5efdc7
VZ
925 findInfo.flags = LVFI_STRING;
926 if ( partial )
927 findInfo.flags |= LVFI_STRING;
928 findInfo.psz = WXSTRINGCAST str;
2bda0e17 929
0b5efdc7 930 return ListView_FindItem((HWND) GetHWND(), (int) start, & findInfo);
2bda0e17
KB
931}
932
933// Find an item whose data matches this data, starting from the item after 'start'
934// or the beginning if 'start' is -1.
debe6624 935long wxListCtrl::FindItem(long start, long data)
2bda0e17 936{
0b5efdc7 937 LV_FINDINFO findInfo;
2bda0e17 938
0b5efdc7
VZ
939 findInfo.flags = LVFI_PARAM;
940 findInfo.lParam = data;
2bda0e17 941
0b5efdc7 942 return ListView_FindItem((HWND) GetHWND(), (int) start, & findInfo);
2bda0e17
KB
943}
944
945// Find an item nearest this position in the specified direction, starting from
946// the item after 'start' or the beginning if 'start' is -1.
debe6624 947long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
2bda0e17 948{
0b5efdc7 949 LV_FINDINFO findInfo;
2bda0e17 950
0b5efdc7
VZ
951 findInfo.flags = LVFI_NEARESTXY;
952 findInfo.pt.x = pt.x;
953 findInfo.pt.y = pt.y;
acb62b84 954 findInfo.vkDirection = VK_RIGHT;
2bda0e17 955
0b5efdc7
VZ
956 if ( direction == wxLIST_FIND_UP )
957 findInfo.vkDirection = VK_UP;
958 else if ( direction == wxLIST_FIND_DOWN )
959 findInfo.vkDirection = VK_DOWN;
960 else if ( direction == wxLIST_FIND_LEFT )
961 findInfo.vkDirection = VK_LEFT;
962 else if ( direction == wxLIST_FIND_RIGHT )
963 findInfo.vkDirection = VK_RIGHT;
964
965 return ListView_FindItem((HWND) GetHWND(), (int) start, & findInfo);
2bda0e17
KB
966}
967
968// Determines which item (if any) is at the specified point,
969// giving details in 'flags' (see wxLIST_HITTEST_... flags above)
970long wxListCtrl::HitTest(const wxPoint& point, int& flags)
971{
972 LV_HITTESTINFO hitTestInfo;
0b5efdc7
VZ
973 hitTestInfo.pt.x = (int) point.x;
974 hitTestInfo.pt.y = (int) point.y;
2bda0e17
KB
975
976 ListView_HitTest((HWND) GetHWND(), & hitTestInfo);
977
0b5efdc7
VZ
978 flags = 0;
979 if ( hitTestInfo.flags & LVHT_ABOVE )
980 flags |= wxLIST_HITTEST_ABOVE;
981 if ( hitTestInfo.flags & LVHT_BELOW )
982 flags |= wxLIST_HITTEST_BELOW;
983 if ( hitTestInfo.flags & LVHT_NOWHERE )
984 flags |= wxLIST_HITTEST_NOWHERE;
985 if ( hitTestInfo.flags & LVHT_ONITEMICON )
986 flags |= wxLIST_HITTEST_ONITEMICON;
987 if ( hitTestInfo.flags & LVHT_ONITEMLABEL )
988 flags |= wxLIST_HITTEST_ONITEMLABEL;
989 if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON )
990 flags |= wxLIST_HITTEST_ONITEMSTATEICON;
991 if ( hitTestInfo.flags & LVHT_TOLEFT )
992 flags |= wxLIST_HITTEST_TOLEFT;
993 if ( hitTestInfo.flags & LVHT_TORIGHT )
994 flags |= wxLIST_HITTEST_TORIGHT;
995
996 return (long) hitTestInfo.iItem ;
2bda0e17
KB
997}
998
999// Inserts an item, returning the index of the new item if successful,
1000// -1 otherwise.
2bda0e17
KB
1001long wxListCtrl::InsertItem(wxListItem& info)
1002{
0b5efdc7
VZ
1003 LV_ITEM item;
1004 wxConvertToMSWListItem(this, info, item);
2bda0e17 1005
0b5efdc7 1006 return (long) ListView_InsertItem((HWND) GetHWND(), & item);
2bda0e17
KB
1007}
1008
debe6624 1009long wxListCtrl::InsertItem(long index, const wxString& label)
2bda0e17 1010{
0b5efdc7
VZ
1011 wxListItem info;
1012 info.m_text = label;
1013 info.m_mask = wxLIST_MASK_TEXT;
1014 info.m_itemId = index;
1015 return InsertItem(info);
2bda0e17
KB
1016}
1017
1018// Inserts an image item
debe6624 1019long wxListCtrl::InsertItem(long index, int imageIndex)
2bda0e17 1020{
0b5efdc7
VZ
1021 wxListItem info;
1022 info.m_image = imageIndex;
1023 info.m_mask = wxLIST_MASK_IMAGE;
1024 info.m_itemId = index;
1025 return InsertItem(info);
2bda0e17
KB
1026}
1027
1028// Inserts an image/string item
debe6624 1029long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
2bda0e17 1030{
0b5efdc7
VZ
1031 wxListItem info;
1032 info.m_image = imageIndex;
1033 info.m_text = label;
1034 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1035 info.m_itemId = index;
1036 return InsertItem(info);
2bda0e17
KB
1037}
1038
1039// For list view mode (only), inserts a column.
debe6624 1040long wxListCtrl::InsertColumn(long col, wxListItem& item)
2bda0e17 1041{
0b5efdc7
VZ
1042 LV_COLUMN lvCol;
1043 lvCol.mask = 0;
1044 lvCol.fmt = 0;
1045 lvCol.pszText = NULL;
1046
1047 if ( item.m_mask & wxLIST_MASK_TEXT )
1048 {
1049 lvCol.mask |= LVCF_TEXT;
1050 lvCol.pszText = WXSTRINGCAST item.m_text;
1051 lvCol.cchTextMax = 0; // Ignored
1052 }
1053 if ( item.m_mask & wxLIST_MASK_FORMAT )
1054 {
1055 lvCol.mask |= LVCF_FMT;
1056
1057 if ( item.m_format == wxLIST_FORMAT_LEFT )
1058 lvCol.fmt = LVCFMT_LEFT;
1059 if ( item.m_format == wxLIST_FORMAT_RIGHT )
1060 lvCol.fmt = LVCFMT_RIGHT;
1061 if ( item.m_format == wxLIST_FORMAT_CENTRE )
1062 lvCol.fmt = LVCFMT_CENTER;
2bda0e17
KB
1063 }
1064
e373f51b 1065 lvCol.mask |= LVCF_WIDTH;
0b5efdc7
VZ
1066 if ( item.m_mask & wxLIST_MASK_WIDTH )
1067 {
e373f51b 1068 if ( item.m_width == wxLIST_AUTOSIZE)
0b5efdc7 1069 lvCol.cx = LVSCW_AUTOSIZE;
e373f51b 1070 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
0b5efdc7 1071 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
e373f51b
VZ
1072 else
1073 lvCol.cx = item.m_width;
1074 }
1075 else
1076 {
1077 // always give some width to the new column: this one is compatible
1078 // with wxGTK
1079 lvCol.cx = 80;
0b5efdc7 1080 }
e373f51b 1081
0b5efdc7
VZ
1082 lvCol.mask |= LVCF_SUBITEM;
1083 lvCol.iSubItem = col;
2bda0e17 1084
e373f51b 1085 bool success = ListView_InsertColumn((HWND) GetHWND(), col, & lvCol) != -1;
2bda0e17 1086 if ( success )
e373f51b
VZ
1087 {
1088 m_colCount++;
1089 }
1090 else
1091 {
1092 wxLogDebug("Failed to insert the column '%s' into listview!",
1093 lvCol.pszText);
1094 }
1095
2bda0e17
KB
1096 return success;
1097}
1098
debe6624 1099long wxListCtrl::InsertColumn(long col, const wxString& heading, int format,
0b5efdc7 1100 int width)
2bda0e17 1101{
0b5efdc7
VZ
1102 wxListItem item;
1103 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1104 item.m_text = heading;
1105 if ( width > -1 )
1106 {
1107 item.m_mask |= wxLIST_MASK_WIDTH;
1108 item.m_width = width;
1109 }
1110 item.m_format = format;
2bda0e17 1111
0b5efdc7 1112 return InsertColumn(col, item);
2bda0e17
KB
1113}
1114
1115// Scrolls the list control. If in icon, small icon or report view mode,
1116// x specifies the number of pixels to scroll. If in list view mode, x
1117// specifies the number of columns to scroll.
1118// If in icon, small icon or list view mode, y specifies the number of pixels
1119// to scroll. If in report view mode, y specifies the number of lines to scroll.
debe6624 1120bool wxListCtrl::ScrollList(int dx, int dy)
2bda0e17 1121{
0b5efdc7 1122 return (ListView_Scroll((HWND) GetHWND(), dx, dy) != 0);
2bda0e17
KB
1123}
1124
1125// Sort items.
1126
1127// fn is a function which takes 3 long arguments: item1, item2, data.
1128// item1 is the long data associated with a first item (NOT the index).
1129// item2 is the long data associated with a second item (NOT the index).
1130// data is the same value as passed to SortItems.
1131// The return value is a negative number if the first item should precede the second
1132// item, a positive number of the second item should precede the first,
1133// or zero if the two items are equivalent.
1134
1135// data is arbitrary data to be passed to the sort function.
1136bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1137{
0b5efdc7 1138 return (ListView_SortItems((HWND) GetHWND(), (PFNLVCOMPARE) fn, data) != 0);
2bda0e17
KB
1139}
1140
debe6624 1141bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id)
2bda0e17 1142{
0b5efdc7 1143 if (cmd == EN_UPDATE)
acb62b84 1144 {
0b5efdc7
VZ
1145 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1146 event.SetEventObject( this );
1147 ProcessCommand(event);
1148 return TRUE;
acb62b84 1149 }
0b5efdc7 1150 else if (cmd == EN_KILLFOCUS)
acb62b84 1151 {
0b5efdc7
VZ
1152 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1153 event.SetEventObject( this );
1154 ProcessCommand(event);
1155 return TRUE;
acb62b84 1156 }
0b5efdc7
VZ
1157 else return FALSE;
1158}
1159
a23fd0e1 1160bool wxListCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
0b5efdc7
VZ
1161{
1162 wxListEvent event(wxEVT_NULL, m_windowId);
1163 wxEventType eventType = wxEVT_NULL;
1164 NMHDR *hdr1 = (NMHDR *) lParam;
1165 switch ( hdr1->code )
acb62b84 1166 {
0b5efdc7
VZ
1167 case LVN_BEGINRDRAG:
1168 eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
1169 // fall through
1170
1171 case LVN_BEGINDRAG:
1172 if ( eventType == wxEVT_NULL )
1173 {
1174 eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG;
1175 }
1176
1177 {
1178 NM_LISTVIEW *hdr = (NM_LISTVIEW *)lParam;
1179 event.m_itemIndex = hdr->iItem;
1180 event.m_pointDrag.x = hdr->ptAction.x;
1181 event.m_pointDrag.y = hdr->ptAction.y;
1182 }
1183 break;
1184
1185 case LVN_BEGINLABELEDIT:
1186 {
1187 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
1188 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1189 wxConvertFromMSWListItem(this, event.m_item, info->item, (HWND) GetHWND());
1190 break;
1191 }
1192
1193 case LVN_COLUMNCLICK:
1194 {
1195 eventType = wxEVT_COMMAND_LIST_COL_CLICK;
1196 NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1197 event.m_itemIndex = -1;
1198 event.m_col = hdr->iSubItem;
1199 break;
1200 }
1201 case LVN_DELETEALLITEMS:
1202 {
1203 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
1204 // NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1205 event.m_itemIndex = -1;
1206 break;
1207 }
1208 case LVN_DELETEITEM:
1209 {
1210 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
1211 NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1212 event.m_itemIndex = hdr->iItem;
1213 break;
1214 }
1215 case LVN_ENDLABELEDIT:
1216 {
1217 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
1218 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1219 wxConvertFromMSWListItem(this, event.m_item, info->item, (HWND) GetHWND());
1220 if ( info->item.pszText == NULL || info->item.iItem == -1 )
1221 event.m_cancelled = TRUE;
1222 break;
1223 }
1224 case LVN_GETDISPINFO:
1225 {
1226 // return FALSE;
1227 // TODO: some text buffering here, I think
1228 // TODO: API for getting Windows to retrieve values
1229 // on demand.
1230 eventType = wxEVT_COMMAND_LIST_GET_INFO;
1231 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1232 wxConvertFromMSWListItem(this, event.m_item, info->item, (HWND) GetHWND());
1233 break;
1234 }
1235 case LVN_INSERTITEM:
1236 {
1237 eventType = wxEVT_COMMAND_LIST_INSERT_ITEM;
1238 NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1239 event.m_itemIndex = hdr->iItem;
1240 break;
1241 }
1242 case LVN_ITEMCHANGED:
1243 {
1244 // This needs to be sent to wxListCtrl as a rather more
1245 // concrete event. For now, just detect a selection
1246 // or deselection.
1247 NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1248 if ( (hdr->uNewState & LVIS_SELECTED) && !(hdr->uOldState & LVIS_SELECTED) )
1249 {
1250 eventType = wxEVT_COMMAND_LIST_ITEM_SELECTED;
1251 event.m_itemIndex = hdr->iItem;
1252 }
1253 else if ( !(hdr->uNewState & LVIS_SELECTED) && (hdr->uOldState & LVIS_SELECTED) )
1254 {
1255 eventType = wxEVT_COMMAND_LIST_ITEM_DESELECTED;
1256 event.m_itemIndex = hdr->iItem;
1257 }
1258 else
1259 return FALSE;
1260 break;
1261 }
1262 case LVN_KEYDOWN:
1263 {
1264 eventType = wxEVT_COMMAND_LIST_KEY_DOWN;
1265 LV_KEYDOWN *info = (LV_KEYDOWN *)lParam;
1266 event.m_code = wxCharCodeMSWToWX(info->wVKey);
1267 break;
1268 }
1269 case LVN_SETDISPINFO:
1270 {
1271 eventType = wxEVT_COMMAND_LIST_SET_INFO;
1272 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1273 wxConvertFromMSWListItem(this, event.m_item, info->item, (HWND) GetHWND());
1274 break;
1275 }
1276
1277 default :
a23fd0e1 1278 return wxControl::MSWOnNotify(idCtrl, lParam, result);
acb62b84
VZ
1279 }
1280
0b5efdc7
VZ
1281 event.SetEventObject( this );
1282 event.SetEventType(eventType);
acb62b84 1283
0b5efdc7
VZ
1284 if ( !GetEventHandler()->ProcessEvent(event) )
1285 return FALSE;
acb62b84 1286
0b5efdc7 1287 if (hdr1->code == LVN_GETDISPINFO)
acb62b84 1288 {
0b5efdc7
VZ
1289 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1290 if ( info->item.mask & LVIF_TEXT )
1291 {
1292 if ( !event.m_item.m_text.IsNull() )
1293 {
1294 info->item.pszText = AddPool(event.m_item.m_text);
1295 info->item.cchTextMax = strlen(info->item.pszText) + 1;
1296 }
1297 }
1298 // wxConvertToMSWListItem(this, event.m_item, info->item);
acb62b84 1299 }
acb62b84 1300
0b5efdc7 1301 *result = !event.IsAllowed();
fd3f686c 1302
0b5efdc7 1303 return TRUE;
2bda0e17
KB
1304}
1305
1306char *wxListCtrl::AddPool(const wxString& str)
1307{
0b5efdc7
VZ
1308 // Remove the first element if 3 strings exist
1309 if ( m_stringPool.Number() == 3 )
1310 {
1311 wxNode *node = m_stringPool.First();
1312 delete[] (char *)node->Data();
1313 delete node;
1314 }
1315 wxNode *node = m_stringPool.Add((char *) (const char *)str);
1316 return (char *)node->Data();
2bda0e17
KB
1317}
1318
1319// List item structure
0b5efdc7 1320wxListItem::wxListItem()
2bda0e17
KB
1321{
1322 m_mask = 0;
1323 m_itemId = 0;
1324 m_col = 0;
1325 m_state = 0;
1326 m_stateMask = 0;
1327 m_image = 0;
0b5efdc7 1328 m_data = 0;
2bda0e17 1329
0b5efdc7
VZ
1330 m_format = wxLIST_FORMAT_CENTRE;
1331 m_width = 0;
2bda0e17
KB
1332}
1333
1334static void wxConvertFromMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& lvItem, HWND getFullInfo)
1335{
0b5efdc7
VZ
1336 info.m_data = lvItem.lParam;
1337 info.m_mask = 0;
1338 info.m_state = 0;
1339 info.m_stateMask = 0;
1340 info.m_itemId = lvItem.iItem;
acb62b84 1341
0b5efdc7 1342 long oldMask = lvItem.mask;
acb62b84 1343
0b5efdc7
VZ
1344 bool needText = FALSE;
1345 if (getFullInfo != 0)
acb62b84 1346 {
0b5efdc7
VZ
1347 if ( lvItem.mask & LVIF_TEXT )
1348 needText = FALSE;
1349 else
1350 needText = TRUE;
acb62b84 1351
0b5efdc7
VZ
1352 if ( needText )
1353 {
1354 lvItem.pszText = new char[513];
1355 lvItem.cchTextMax = 512;
1356 }
1357 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ;
1358 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM ;
1359 ::SendMessage(getFullInfo, LVM_GETITEM, 0, (LPARAM)& lvItem) ;
1360 }
acb62b84 1361
0b5efdc7 1362 if ( lvItem.mask & LVIF_STATE )
acb62b84 1363 {
0b5efdc7
VZ
1364 info.m_mask |= wxLIST_MASK_STATE;
1365
1366 if ( lvItem.stateMask & LVIS_CUT)
1367 {
1368 info.m_stateMask |= wxLIST_STATE_CUT ;
1369 if ( lvItem.state & LVIS_CUT )
1370 info.m_state |= wxLIST_STATE_CUT ;
1371 }
1372 if ( lvItem.stateMask & LVIS_DROPHILITED)
1373 {
1374 info.m_stateMask |= wxLIST_STATE_DROPHILITED ;
1375 if ( lvItem.state & LVIS_DROPHILITED )
1376 info.m_state |= wxLIST_STATE_DROPHILITED ;
1377 }
1378 if ( lvItem.stateMask & LVIS_FOCUSED)
1379 {
1380 info.m_stateMask |= wxLIST_STATE_FOCUSED ;
1381 if ( lvItem.state & LVIS_FOCUSED )
1382 info.m_state |= wxLIST_STATE_FOCUSED ;
1383 }
1384 if ( lvItem.stateMask & LVIS_SELECTED)
1385 {
1386 info.m_stateMask |= wxLIST_STATE_SELECTED ;
1387 if ( lvItem.state & LVIS_SELECTED )
1388 info.m_state |= wxLIST_STATE_SELECTED ;
1389 }
acb62b84 1390 }
0b5efdc7
VZ
1391
1392 if ( lvItem.mask & LVIF_TEXT )
acb62b84 1393 {
0b5efdc7
VZ
1394 info.m_mask |= wxLIST_MASK_TEXT;
1395 info.m_text = lvItem.pszText;
acb62b84 1396 }
0b5efdc7 1397 if ( lvItem.mask & LVIF_IMAGE )
acb62b84 1398 {
0b5efdc7
VZ
1399 info.m_mask |= wxLIST_MASK_IMAGE;
1400 info.m_image = lvItem.iImage;
acb62b84 1401 }
0b5efdc7
VZ
1402 if ( lvItem.mask & LVIF_PARAM )
1403 info.m_mask |= wxLIST_MASK_DATA;
1404 if ( lvItem.mask & LVIF_DI_SETITEM )
1405 info.m_mask |= wxLIST_SET_ITEM;
1406 info.m_col = lvItem.iSubItem;
1407
1408 if (needText)
acb62b84 1409 {
0b5efdc7
VZ
1410 if (lvItem.pszText)
1411 delete[] lvItem.pszText;
acb62b84 1412 }
0b5efdc7 1413 lvItem.mask = oldMask ;
2bda0e17
KB
1414}
1415
1416static void wxConvertToMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& lvItem)
1417{
0b5efdc7 1418 lvItem.iItem = (int) info.m_itemId ;
acb62b84 1419
0b5efdc7
VZ
1420 lvItem.iImage = info.m_image ;
1421 lvItem.lParam = info.m_data;
1422 lvItem.stateMask = 0;
1423 lvItem.state = 0;
1424 lvItem.mask = 0;
1425 lvItem.iSubItem = info.m_col;
acb62b84 1426
0b5efdc7 1427 if (info.m_mask & wxLIST_MASK_STATE)
acb62b84 1428 {
0b5efdc7
VZ
1429 lvItem.mask |= LVIF_STATE ;
1430 if (info.m_stateMask & wxLIST_STATE_CUT)
1431 {
1432 lvItem.stateMask |= LVIS_CUT ;
1433 if (info.m_state & wxLIST_STATE_CUT)
1434 lvItem.state |= LVIS_CUT;
1435 }
1436 if (info.m_stateMask & wxLIST_STATE_DROPHILITED)
1437 {
1438 lvItem.stateMask |= LVIS_DROPHILITED;
1439 if (info.m_state & wxLIST_STATE_DROPHILITED)
1440 lvItem.state |= LVIS_DROPHILITED;
1441 }
1442 if (info.m_stateMask & wxLIST_STATE_FOCUSED)
1443 {
1444 lvItem.stateMask |= LVIS_FOCUSED;
1445 if (info.m_state & wxLIST_STATE_FOCUSED)
1446 lvItem.state |= LVIS_FOCUSED;
1447 }
1448 if (info.m_stateMask & wxLIST_STATE_SELECTED)
1449 {
1450 lvItem.stateMask |= LVIS_SELECTED;
1451 if (info.m_state & wxLIST_STATE_SELECTED)
1452 lvItem.state |= LVIS_SELECTED;
1453 }
acb62b84 1454 }
acb62b84 1455
0b5efdc7 1456 if (info.m_mask & wxLIST_MASK_TEXT)
acb62b84 1457 {
0b5efdc7
VZ
1458 lvItem.mask |= LVIF_TEXT ;
1459 if ( ctrl->GetWindowStyleFlag() & wxLC_USER_TEXT )
1460 {
1461 lvItem.pszText = LPSTR_TEXTCALLBACK;
1462 }
1463 else
1464 {
1465 lvItem.pszText = (char *) (const char *)info.m_text ;
1466 if ( lvItem.pszText )
1467 lvItem.cchTextMax = info.m_text.Length();
1468 else
1469 lvItem.cchTextMax = 0;
1470 }
acb62b84 1471 }
0b5efdc7
VZ
1472 if (info.m_mask & wxLIST_MASK_IMAGE)
1473 lvItem.mask |= LVIF_IMAGE ;
1474 if (info.m_mask & wxLIST_MASK_DATA)
1475 lvItem.mask |= LVIF_PARAM ;
2bda0e17
KB
1476}
1477
1478// List event
92976ab6 1479IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
2bda0e17 1480
fd3f686c 1481wxListEvent::wxListEvent(wxEventType commandType, int id)
0b5efdc7 1482: wxNotifyEvent(commandType, id)
2bda0e17 1483{
0b5efdc7
VZ
1484 m_code = 0;
1485 m_itemIndex = 0;
1486 m_col = 0;
1487 m_cancelled = FALSE;
2bda0e17
KB
1488}
1489
1490#endif
1491