]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listctrl.cpp
Unicode conversion.
[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)
363void wxListCtrl::SetBackgroundColour(const wxColour& col)
364{
0b5efdc7 365 wxWindow::SetBackgroundColour(col);
2bda0e17 366
0b5efdc7 367 ListView_SetBkColor((HWND) GetHWND(), PALETTERGB(col.Red(), col.Green(), col.Blue()));
2bda0e17
KB
368}
369
370// Gets information about this column
debe6624 371bool wxListCtrl::GetColumn(int col, wxListItem& item) const
2bda0e17 372{
0b5efdc7
VZ
373 LV_COLUMN lvCol;
374 lvCol.mask = 0;
375 lvCol.fmt = 0;
376 lvCol.pszText = NULL;
377
378 if ( item.m_mask & wxLIST_MASK_TEXT )
379 {
380 lvCol.mask |= LVCF_TEXT;
381 lvCol.pszText = new char[513];
382 lvCol.cchTextMax = 512;
383 }
384
385 bool success = (ListView_GetColumn((HWND) GetHWND(), col, & lvCol) != 0);
386
387 // item.m_subItem = lvCol.iSubItem;
388 item.m_width = lvCol.cx;
389
390 if ( (item.m_mask & wxLIST_MASK_TEXT) && lvCol.pszText )
391 {
392 item.m_text = lvCol.pszText;
393 delete[] lvCol.pszText;
394 }
395
396 if ( item.m_mask & wxLIST_MASK_FORMAT )
397 {
398 if (lvCol.fmt == LVCFMT_LEFT)
399 item.m_format = wxLIST_FORMAT_LEFT;
400 else if (lvCol.fmt == LVCFMT_RIGHT)
401 item.m_format = wxLIST_FORMAT_RIGHT;
402 else if (lvCol.fmt == LVCFMT_CENTER)
403 item.m_format = wxLIST_FORMAT_CENTRE;
2bda0e17
KB
404 }
405
0b5efdc7 406 return success;
2bda0e17
KB
407}
408
409// Sets information about this column
debe6624 410bool wxListCtrl::SetColumn(int col, wxListItem& item)
2bda0e17 411{
0b5efdc7
VZ
412 LV_COLUMN lvCol;
413 lvCol.mask = 0;
414 lvCol.fmt = 0;
415 lvCol.pszText = NULL;
416
417 if ( item.m_mask & wxLIST_MASK_TEXT )
418 {
419 lvCol.mask |= LVCF_TEXT;
420 lvCol.pszText = WXSTRINGCAST item.m_text;
421 lvCol.cchTextMax = 0; // Ignored
422 }
423 if ( item.m_mask & wxLIST_MASK_FORMAT )
424 {
425 lvCol.mask |= LVCF_FMT;
426
427 if ( item.m_format == wxLIST_FORMAT_LEFT )
428 lvCol.fmt = LVCFMT_LEFT;
429 if ( item.m_format == wxLIST_FORMAT_RIGHT )
430 lvCol.fmt = LVCFMT_RIGHT;
431 if ( item.m_format == wxLIST_FORMAT_CENTRE )
432 lvCol.fmt = LVCFMT_CENTER;
2bda0e17
KB
433 }
434
0b5efdc7
VZ
435 if ( item.m_mask & wxLIST_MASK_WIDTH )
436 {
437 lvCol.mask |= LVCF_WIDTH;
438 lvCol.cx = item.m_width;
2bda0e17 439
0b5efdc7
VZ
440 if ( lvCol.cx == wxLIST_AUTOSIZE)
441 lvCol.cx = LVSCW_AUTOSIZE;
442 else if ( lvCol.cx == wxLIST_AUTOSIZE_USEHEADER)
443 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
444 }
445 lvCol.mask |= LVCF_SUBITEM;
446 lvCol.iSubItem = col;
447 return (ListView_SetColumn((HWND) GetHWND(), col, & lvCol) != 0);
2bda0e17
KB
448}
449
450// Gets the column width
debe6624 451int wxListCtrl::GetColumnWidth(int col) const
2bda0e17 452{
0b5efdc7 453 return ListView_GetColumnWidth((HWND) GetHWND(), col);
2bda0e17
KB
454}
455
456// Sets the column width
debe6624 457bool wxListCtrl::SetColumnWidth(int col, int width)
2bda0e17 458{
0b5efdc7
VZ
459 int col2 = col;
460 if ( m_windowStyle & wxLC_LIST )
461 col2 = -1;
2bda0e17 462
0b5efdc7
VZ
463 int width2 = width;
464 if ( width2 == wxLIST_AUTOSIZE)
465 width2 = LVSCW_AUTOSIZE;
466 else if ( width2 == wxLIST_AUTOSIZE_USEHEADER)
467 width2 = LVSCW_AUTOSIZE_USEHEADER;
2bda0e17 468
0b5efdc7 469 return (ListView_SetColumnWidth((HWND) GetHWND(), col2, width2) != 0);
2bda0e17
KB
470}
471
472// Gets the number of items that can fit vertically in the
473// visible area of the list control (list or report view)
474// or the total number of items in the list control (icon
475// or small icon view)
476int wxListCtrl::GetCountPerPage(void) const
477{
0b5efdc7 478 return ListView_GetCountPerPage((HWND) GetHWND());
2bda0e17
KB
479}
480
481// Gets the edit control for editing labels.
bbcdf8bc 482wxTextCtrl* wxListCtrl::GetEditControl(void) const
2bda0e17 483{
bbcdf8bc 484 return m_textCtrl;
2bda0e17
KB
485}
486
487// Gets information about the item
488bool wxListCtrl::GetItem(wxListItem& info) const
489{
0b5efdc7 490 LV_ITEM lvItem;
cf0b3979 491#ifdef __GNUWIN32__
0b5efdc7 492 memset(&lvItem, 0, sizeof(lvItem));
386af6a2 493#else
0b5efdc7 494 ZeroMemory(&lvItem, sizeof(lvItem)); // must set all fields to 0
386af6a2 495#endif
2bda0e17 496
0b5efdc7
VZ
497 lvItem.iItem = info.m_itemId;
498
499 if ( info.m_mask & wxLIST_MASK_TEXT )
500 {
501 lvItem.mask |= LVIF_TEXT;
502 lvItem.pszText = new char[513];
503 lvItem.cchTextMax = 512;
504 }
505 else
506 {
507 lvItem.pszText = NULL;
508 }
509
510 if (info.m_mask & wxLIST_MASK_DATA)
511 lvItem.mask |= LVIF_PARAM ;
512
513 if ( info.m_mask & wxLIST_MASK_STATE )
514 {
515 lvItem.mask |= LVIF_STATE;
516 // the other bits are hardly interesting anyhow
517 lvItem.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
518 }
519
520 bool success = ListView_GetItem((HWND)GetHWND(), &lvItem) != 0;
521 if ( !success )
522 {
523 wxLogError(_("Couldn't retrieve information about list control item %d."),
524 lvItem.iItem);
525 }
526 else
527 {
528 wxConvertFromMSWListItem(this, info, lvItem);
529 }
530
531 if (lvItem.pszText)
532 delete[] lvItem.pszText;
533
534 return success;
2bda0e17
KB
535}
536
537// Sets information about the item
538bool wxListCtrl::SetItem(wxListItem& info)
539{
0b5efdc7 540 LV_ITEM item;
2bda0e17 541 wxConvertToMSWListItem(this, info, item);
0b5efdc7
VZ
542 item.cchTextMax = 0;
543 return (ListView_SetItem((HWND) GetHWND(), &item) != 0);
2bda0e17
KB
544}
545
debe6624 546long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId)
2bda0e17 547{
0b5efdc7
VZ
548 wxListItem info;
549 info.m_text = label;
550 info.m_mask = wxLIST_MASK_TEXT;
551 info.m_itemId = index;
552 info.m_col = col;
553 if ( imageId > -1 )
554 {
555 info.m_image = imageId;
556 info.m_mask |= wxLIST_MASK_IMAGE;
557 }
558 return SetItem(info);
2bda0e17
KB
559}
560
561
562// Gets the item state
debe6624 563int wxListCtrl::GetItemState(long item, long stateMask) const
2bda0e17 564{
0b5efdc7 565 wxListItem info;
2bda0e17 566
0b5efdc7
VZ
567 info.m_mask = wxLIST_MASK_STATE ;
568 info.m_stateMask = stateMask;
569 info.m_itemId = item;
2bda0e17 570
0b5efdc7
VZ
571 if (!GetItem(info))
572 return 0;
2bda0e17 573
0b5efdc7 574 return info.m_state;
2bda0e17
KB
575}
576
577// Sets the item state
debe6624 578bool wxListCtrl::SetItemState(long item, long state, long stateMask)
2bda0e17 579{
0b5efdc7 580 wxListItem info;
2bda0e17 581
0b5efdc7
VZ
582 info.m_mask = wxLIST_MASK_STATE ;
583 info.m_state = state;
584 info.m_stateMask = stateMask;
585 info.m_itemId = item;
2bda0e17 586
0b5efdc7 587 return SetItem(info);
2bda0e17
KB
588}
589
590// Sets the item image
debe6624 591bool wxListCtrl::SetItemImage(long item, int image, int selImage)
2bda0e17 592{
0b5efdc7 593 wxListItem info;
2bda0e17 594
0b5efdc7
VZ
595 info.m_mask = wxLIST_MASK_IMAGE ;
596 info.m_image = image;
597 info.m_itemId = item;
2bda0e17 598
0b5efdc7 599 return SetItem(info);
2bda0e17
KB
600}
601
602// Gets the item text
debe6624 603wxString wxListCtrl::GetItemText(long item) const
2bda0e17 604{
0b5efdc7 605 wxListItem info;
2bda0e17 606
0b5efdc7
VZ
607 info.m_mask = wxLIST_MASK_TEXT ;
608 info.m_itemId = item;
2bda0e17 609
0b5efdc7
VZ
610 if (!GetItem(info))
611 return wxString("");
612 return info.m_text;
2bda0e17
KB
613}
614
615// Sets the item text
debe6624 616void wxListCtrl::SetItemText(long item, const wxString& str)
2bda0e17 617{
0b5efdc7 618 wxListItem info;
2bda0e17 619
0b5efdc7
VZ
620 info.m_mask = wxLIST_MASK_TEXT ;
621 info.m_itemId = item;
622 info.m_text = str;
2bda0e17 623
0b5efdc7 624 SetItem(info);
2bda0e17
KB
625}
626
627// Gets the item data
debe6624 628long wxListCtrl::GetItemData(long item) const
2bda0e17 629{
0b5efdc7 630 wxListItem info;
2bda0e17 631
0b5efdc7
VZ
632 info.m_mask = wxLIST_MASK_DATA ;
633 info.m_itemId = item;
2bda0e17 634
0b5efdc7
VZ
635 if (!GetItem(info))
636 return 0;
637 return info.m_data;
2bda0e17
KB
638}
639
640// Sets the item data
debe6624 641bool wxListCtrl::SetItemData(long item, long data)
2bda0e17 642{
0b5efdc7 643 wxListItem info;
2bda0e17 644
0b5efdc7
VZ
645 info.m_mask = wxLIST_MASK_DATA ;
646 info.m_itemId = item;
647 info.m_data = data;
2bda0e17 648
0b5efdc7 649 return SetItem(info);
2bda0e17
KB
650}
651
652// Gets the item rectangle
16e93305 653bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
2bda0e17 654{
0b5efdc7 655 RECT rect2;
2bda0e17 656
0b5efdc7
VZ
657 int code2 = LVIR_BOUNDS;
658 if ( code == wxLIST_RECT_BOUNDS )
659 code2 = LVIR_BOUNDS;
660 else if ( code == wxLIST_RECT_ICON )
661 code2 = LVIR_ICON;
662 else if ( code == wxLIST_RECT_LABEL )
663 code2 = LVIR_LABEL;
2bda0e17 664
0b5efdc7 665 bool success = (ListView_GetItemRect((HWND) GetHWND(), (int) item, &rect2, code2) != 0);
2bda0e17 666
0b5efdc7
VZ
667 rect.x = rect2.left;
668 rect.y = rect2.top;
669 rect.width = rect2.right - rect2.left;
670 rect.height = rect2.bottom - rect2.left;
671 return success;
2bda0e17
KB
672}
673
674// Gets the item position
debe6624 675bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
2bda0e17 676{
0b5efdc7 677 POINT pt;
2bda0e17 678
0b5efdc7 679 bool success = (ListView_GetItemPosition((HWND) GetHWND(), (int) item, &pt) != 0);
2bda0e17 680
0b5efdc7
VZ
681 pos.x = pt.x; pos.y = pt.y;
682 return success;
2bda0e17
KB
683}
684
685// Sets the item position.
debe6624 686bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
2bda0e17 687{
0b5efdc7 688 return (ListView_SetItemPosition((HWND) GetHWND(), (int) item, pos.x, pos.y) != 0);
2bda0e17
KB
689}
690
691// Gets the number of items in the list control
692int wxListCtrl::GetItemCount(void) const
693{
0b5efdc7 694 return ListView_GetItemCount((HWND) GetHWND());
2bda0e17
KB
695}
696
697// Retrieves the spacing between icons in pixels.
698// If small is TRUE, gets the spacing for the small icon
699// view, otherwise the large icon view.
700int wxListCtrl::GetItemSpacing(bool isSmall) const
701{
0b5efdc7 702 return ListView_GetItemSpacing((HWND) GetHWND(), (BOOL) isSmall);
2bda0e17
KB
703}
704
705// Gets the number of selected items in the list control
706int wxListCtrl::GetSelectedItemCount(void) const
707{
0b5efdc7 708 return ListView_GetSelectedCount((HWND) GetHWND());
2bda0e17
KB
709}
710
711// Gets the text colour of the listview
712wxColour wxListCtrl::GetTextColour(void) const
713{
0b5efdc7
VZ
714 COLORREF ref = ListView_GetTextColor((HWND) GetHWND());
715 wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
716 return col;
2bda0e17
KB
717}
718
719// Sets the text colour of the listview
720void wxListCtrl::SetTextColour(const wxColour& col)
721{
0b5efdc7 722 ListView_SetTextColor((HWND) GetHWND(), PALETTERGB(col.Red(), col.Blue(), col.Green()));
2bda0e17
KB
723}
724
725// Gets the index of the topmost visible item when in
726// list or report view
727long wxListCtrl::GetTopItem(void) const
728{
0b5efdc7 729 return (long) ListView_GetTopIndex((HWND) GetHWND());
2bda0e17
KB
730}
731
732// Searches for an item, starting from 'item'.
733// 'geometry' is one of
734// wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
735// 'state' is a state bit flag, one or more of
736// wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
737// item can be -1 to find the first item that matches the
738// specified flags.
739// Returns the item or -1 if unsuccessful.
debe6624 740long wxListCtrl::GetNextItem(long item, int geom, int state) const
2bda0e17 741{
0b5efdc7 742 long flags = 0;
2bda0e17 743
0b5efdc7
VZ
744 if ( geom == wxLIST_NEXT_ABOVE )
745 flags |= LVNI_ABOVE;
746 if ( geom == wxLIST_NEXT_ALL )
747 flags |= LVNI_ALL;
748 if ( geom == wxLIST_NEXT_BELOW )
749 flags |= LVNI_BELOW;
750 if ( geom == wxLIST_NEXT_LEFT )
751 flags |= LVNI_TOLEFT;
752 if ( geom == wxLIST_NEXT_RIGHT )
753 flags |= LVNI_TORIGHT;
2bda0e17 754
0b5efdc7
VZ
755 if ( state & wxLIST_STATE_CUT )
756 flags |= LVNI_CUT;
757 if ( state & wxLIST_STATE_DROPHILITED )
758 flags |= LVNI_DROPHILITED;
759 if ( state & wxLIST_STATE_FOCUSED )
760 flags |= LVNI_FOCUSED;
761 if ( state & wxLIST_STATE_SELECTED )
762 flags |= LVNI_SELECTED;
2bda0e17 763
0b5efdc7 764 return (long) ListView_GetNextItem((HWND) GetHWND(), item, flags);
2bda0e17
KB
765}
766
767
debe6624 768wxImageList *wxListCtrl::GetImageList(int which) const
2bda0e17 769{
0b5efdc7 770 if ( which == wxIMAGE_LIST_NORMAL )
2bda0e17 771 {
0b5efdc7
VZ
772 return m_imageListNormal;
773 }
774 else if ( which == wxIMAGE_LIST_SMALL )
2bda0e17 775 {
0b5efdc7
VZ
776 return m_imageListSmall;
777 }
778 else if ( which == wxIMAGE_LIST_STATE )
2bda0e17 779 {
0b5efdc7
VZ
780 return m_imageListState;
781 }
782 return NULL;
2bda0e17
KB
783}
784
debe6624 785void wxListCtrl::SetImageList(wxImageList *imageList, int which)
2bda0e17 786{
0b5efdc7
VZ
787 int flags = 0;
788 if ( which == wxIMAGE_LIST_NORMAL )
2bda0e17 789 {
0b5efdc7
VZ
790 flags = LVSIL_NORMAL;
791 m_imageListNormal = imageList;
792 }
793 else if ( which == wxIMAGE_LIST_SMALL )
2bda0e17 794 {
0b5efdc7
VZ
795 flags = LVSIL_SMALL;
796 m_imageListSmall = imageList;
797 }
798 else if ( which == wxIMAGE_LIST_STATE )
2bda0e17 799 {
0b5efdc7
VZ
800 flags = LVSIL_STATE;
801 m_imageListState = imageList;
802 }
803 ListView_SetImageList((HWND) GetHWND(), (HIMAGELIST) imageList ? imageList->GetHIMAGELIST() : 0, flags);
2bda0e17
KB
804}
805
806// Operations
807////////////////////////////////////////////////////////////////////////////
808
809// Arranges the items
debe6624 810bool wxListCtrl::Arrange(int flag)
2bda0e17 811{
0b5efdc7
VZ
812 UINT code = 0;
813 if ( flag == wxLIST_ALIGN_LEFT )
814 code = LVA_ALIGNLEFT;
815 else if ( flag == wxLIST_ALIGN_TOP )
816 code = LVA_ALIGNTOP;
817 else if ( flag == wxLIST_ALIGN_DEFAULT )
818 code = LVA_DEFAULT;
819 else if ( flag == wxLIST_ALIGN_SNAP_TO_GRID )
820 code = LVA_SNAPTOGRID;
2bda0e17 821
0b5efdc7 822 return (ListView_Arrange((HWND) GetHWND(), code) != 0);
2bda0e17
KB
823}
824
825// Deletes an item
debe6624 826bool wxListCtrl::DeleteItem(long item)
2bda0e17 827{
0b5efdc7 828 return (ListView_DeleteItem((HWND) GetHWND(), (int) item) != 0);
2bda0e17
KB
829}
830
831// Deletes all items
0b5efdc7 832bool wxListCtrl::DeleteAllItems()
2bda0e17 833{
0b5efdc7 834 return (ListView_DeleteAllItems((HWND) GetHWND()) != 0);
2bda0e17
KB
835}
836
837// Deletes all items
0b5efdc7 838bool wxListCtrl::DeleteAllColumns()
2bda0e17 839{
0b5efdc7 840 int i;
2bda0e17
KB
841 for ( i = 0; i < m_colCount; i++)
842 {
0b5efdc7
VZ
843 if (ListView_DeleteColumn((HWND) GetHWND(), 0) != 0)
844 m_colCount --;
2bda0e17
KB
845 }
846 return (m_colCount == 0);
847}
848
849// Deletes a column
debe6624 850bool wxListCtrl::DeleteColumn(int col)
2bda0e17 851{
0b5efdc7 852 bool success = (ListView_DeleteColumn((HWND) GetHWND(), col) != 0);
2bda0e17
KB
853
854 if ( success && (m_colCount > 0) )
855 m_colCount --;
856 return success;
857}
858
859// Clears items, and columns if there are any.
0b5efdc7 860void wxListCtrl::ClearAll()
2bda0e17
KB
861{
862 DeleteAllItems();
863 if ( m_colCount > 0 )
864 DeleteAllColumns();
865}
866
bbcdf8bc
JS
867wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
868{
869 wxASSERT( (textControlClass->IsKindOf(CLASSINFO(wxTextCtrl))) );
870
871 HWND hWnd = (HWND) ListView_EditLabel((HWND) GetHWND(), item);
872
873 if (m_textCtrl)
874 {
0b5efdc7
VZ
875 m_textCtrl->UnsubclassWin();
876 m_textCtrl->SetHWND(0);
877 delete m_textCtrl;
878 m_textCtrl = NULL;
bbcdf8bc
JS
879 }
880
881 m_textCtrl = (wxTextCtrl*) textControlClass->CreateObject();
882 m_textCtrl->SetHWND((WXHWND) hWnd);
883 m_textCtrl->SubclassWin((WXHWND) hWnd);
884
885 return m_textCtrl;
886}
887
888// End label editing, optionally cancelling the edit
889bool wxListCtrl::EndEditLabel(bool cancel)
2bda0e17 890{
341287bf 891 wxFAIL;
bbcdf8bc 892
0b5efdc7
VZ
893 /* I don't know how to implement this: there's no such macro as ListView_EndEditLabelNow.
894 * ???
895 bool success = (ListView_EndEditLabelNow((HWND) GetHWND(), cancel) != 0);
896
897 if (m_textCtrl)
898 {
899 m_textCtrl->UnsubclassWin();
900 m_textCtrl->SetHWND(0);
901 delete m_textCtrl;
902 m_textCtrl = NULL;
903 }
904 return success;
905 */
906 return FALSE;
2bda0e17
KB
907}
908
bbcdf8bc 909
2bda0e17 910// Ensures this item is visible
debe6624 911bool wxListCtrl::EnsureVisible(long item)
2bda0e17 912{
0b5efdc7 913 return (ListView_EnsureVisible((HWND) GetHWND(), (int) item, FALSE) != 0);
2bda0e17
KB
914}
915
916// Find an item whose label matches this string, starting from the item after 'start'
917// or the beginning if 'start' is -1.
debe6624 918long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
2bda0e17 919{
0b5efdc7 920 LV_FINDINFO findInfo;
2bda0e17 921
0b5efdc7
VZ
922 findInfo.flags = LVFI_STRING;
923 if ( partial )
924 findInfo.flags |= LVFI_STRING;
925 findInfo.psz = WXSTRINGCAST str;
2bda0e17 926
0b5efdc7 927 return ListView_FindItem((HWND) GetHWND(), (int) start, & findInfo);
2bda0e17
KB
928}
929
930// Find an item whose data matches this data, starting from the item after 'start'
931// or the beginning if 'start' is -1.
debe6624 932long wxListCtrl::FindItem(long start, long data)
2bda0e17 933{
0b5efdc7 934 LV_FINDINFO findInfo;
2bda0e17 935
0b5efdc7
VZ
936 findInfo.flags = LVFI_PARAM;
937 findInfo.lParam = data;
2bda0e17 938
0b5efdc7 939 return ListView_FindItem((HWND) GetHWND(), (int) start, & findInfo);
2bda0e17
KB
940}
941
942// Find an item nearest this position in the specified direction, starting from
943// the item after 'start' or the beginning if 'start' is -1.
debe6624 944long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
2bda0e17 945{
0b5efdc7 946 LV_FINDINFO findInfo;
2bda0e17 947
0b5efdc7
VZ
948 findInfo.flags = LVFI_NEARESTXY;
949 findInfo.pt.x = pt.x;
950 findInfo.pt.y = pt.y;
acb62b84 951 findInfo.vkDirection = VK_RIGHT;
2bda0e17 952
0b5efdc7
VZ
953 if ( direction == wxLIST_FIND_UP )
954 findInfo.vkDirection = VK_UP;
955 else if ( direction == wxLIST_FIND_DOWN )
956 findInfo.vkDirection = VK_DOWN;
957 else if ( direction == wxLIST_FIND_LEFT )
958 findInfo.vkDirection = VK_LEFT;
959 else if ( direction == wxLIST_FIND_RIGHT )
960 findInfo.vkDirection = VK_RIGHT;
961
962 return ListView_FindItem((HWND) GetHWND(), (int) start, & findInfo);
2bda0e17
KB
963}
964
965// Determines which item (if any) is at the specified point,
966// giving details in 'flags' (see wxLIST_HITTEST_... flags above)
967long wxListCtrl::HitTest(const wxPoint& point, int& flags)
968{
969 LV_HITTESTINFO hitTestInfo;
0b5efdc7
VZ
970 hitTestInfo.pt.x = (int) point.x;
971 hitTestInfo.pt.y = (int) point.y;
2bda0e17
KB
972
973 ListView_HitTest((HWND) GetHWND(), & hitTestInfo);
974
0b5efdc7
VZ
975 flags = 0;
976 if ( hitTestInfo.flags & LVHT_ABOVE )
977 flags |= wxLIST_HITTEST_ABOVE;
978 if ( hitTestInfo.flags & LVHT_BELOW )
979 flags |= wxLIST_HITTEST_BELOW;
980 if ( hitTestInfo.flags & LVHT_NOWHERE )
981 flags |= wxLIST_HITTEST_NOWHERE;
982 if ( hitTestInfo.flags & LVHT_ONITEMICON )
983 flags |= wxLIST_HITTEST_ONITEMICON;
984 if ( hitTestInfo.flags & LVHT_ONITEMLABEL )
985 flags |= wxLIST_HITTEST_ONITEMLABEL;
986 if ( hitTestInfo.flags & LVHT_ONITEMSTATEICON )
987 flags |= wxLIST_HITTEST_ONITEMSTATEICON;
988 if ( hitTestInfo.flags & LVHT_TOLEFT )
989 flags |= wxLIST_HITTEST_TOLEFT;
990 if ( hitTestInfo.flags & LVHT_TORIGHT )
991 flags |= wxLIST_HITTEST_TORIGHT;
992
993 return (long) hitTestInfo.iItem ;
2bda0e17
KB
994}
995
996// Inserts an item, returning the index of the new item if successful,
997// -1 otherwise.
2bda0e17
KB
998long wxListCtrl::InsertItem(wxListItem& info)
999{
0b5efdc7
VZ
1000 LV_ITEM item;
1001 wxConvertToMSWListItem(this, info, item);
2bda0e17 1002
0b5efdc7 1003 return (long) ListView_InsertItem((HWND) GetHWND(), & item);
2bda0e17
KB
1004}
1005
debe6624 1006long wxListCtrl::InsertItem(long index, const wxString& label)
2bda0e17 1007{
0b5efdc7
VZ
1008 wxListItem info;
1009 info.m_text = label;
1010 info.m_mask = wxLIST_MASK_TEXT;
1011 info.m_itemId = index;
1012 return InsertItem(info);
2bda0e17
KB
1013}
1014
1015// Inserts an image item
debe6624 1016long wxListCtrl::InsertItem(long index, int imageIndex)
2bda0e17 1017{
0b5efdc7
VZ
1018 wxListItem info;
1019 info.m_image = imageIndex;
1020 info.m_mask = wxLIST_MASK_IMAGE;
1021 info.m_itemId = index;
1022 return InsertItem(info);
2bda0e17
KB
1023}
1024
1025// Inserts an image/string item
debe6624 1026long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
2bda0e17 1027{
0b5efdc7
VZ
1028 wxListItem info;
1029 info.m_image = imageIndex;
1030 info.m_text = label;
1031 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1032 info.m_itemId = index;
1033 return InsertItem(info);
2bda0e17
KB
1034}
1035
1036// For list view mode (only), inserts a column.
debe6624 1037long wxListCtrl::InsertColumn(long col, wxListItem& item)
2bda0e17 1038{
0b5efdc7
VZ
1039 LV_COLUMN lvCol;
1040 lvCol.mask = 0;
1041 lvCol.fmt = 0;
1042 lvCol.pszText = NULL;
1043
1044 if ( item.m_mask & wxLIST_MASK_TEXT )
1045 {
1046 lvCol.mask |= LVCF_TEXT;
1047 lvCol.pszText = WXSTRINGCAST item.m_text;
1048 lvCol.cchTextMax = 0; // Ignored
1049 }
1050 if ( item.m_mask & wxLIST_MASK_FORMAT )
1051 {
1052 lvCol.mask |= LVCF_FMT;
1053
1054 if ( item.m_format == wxLIST_FORMAT_LEFT )
1055 lvCol.fmt = LVCFMT_LEFT;
1056 if ( item.m_format == wxLIST_FORMAT_RIGHT )
1057 lvCol.fmt = LVCFMT_RIGHT;
1058 if ( item.m_format == wxLIST_FORMAT_CENTRE )
1059 lvCol.fmt = LVCFMT_CENTER;
2bda0e17
KB
1060 }
1061
e373f51b 1062 lvCol.mask |= LVCF_WIDTH;
0b5efdc7
VZ
1063 if ( item.m_mask & wxLIST_MASK_WIDTH )
1064 {
e373f51b 1065 if ( item.m_width == wxLIST_AUTOSIZE)
0b5efdc7 1066 lvCol.cx = LVSCW_AUTOSIZE;
e373f51b 1067 else if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER)
0b5efdc7 1068 lvCol.cx = LVSCW_AUTOSIZE_USEHEADER;
e373f51b
VZ
1069 else
1070 lvCol.cx = item.m_width;
1071 }
1072 else
1073 {
1074 // always give some width to the new column: this one is compatible
1075 // with wxGTK
1076 lvCol.cx = 80;
0b5efdc7 1077 }
e373f51b 1078
0b5efdc7
VZ
1079 lvCol.mask |= LVCF_SUBITEM;
1080 lvCol.iSubItem = col;
2bda0e17 1081
e373f51b 1082 bool success = ListView_InsertColumn((HWND) GetHWND(), col, & lvCol) != -1;
2bda0e17 1083 if ( success )
e373f51b
VZ
1084 {
1085 m_colCount++;
1086 }
1087 else
1088 {
1089 wxLogDebug("Failed to insert the column '%s' into listview!",
1090 lvCol.pszText);
1091 }
1092
2bda0e17
KB
1093 return success;
1094}
1095
debe6624 1096long wxListCtrl::InsertColumn(long col, const wxString& heading, int format,
0b5efdc7 1097 int width)
2bda0e17 1098{
0b5efdc7
VZ
1099 wxListItem item;
1100 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1101 item.m_text = heading;
1102 if ( width > -1 )
1103 {
1104 item.m_mask |= wxLIST_MASK_WIDTH;
1105 item.m_width = width;
1106 }
1107 item.m_format = format;
2bda0e17 1108
0b5efdc7 1109 return InsertColumn(col, item);
2bda0e17
KB
1110}
1111
1112// Scrolls the list control. If in icon, small icon or report view mode,
1113// x specifies the number of pixels to scroll. If in list view mode, x
1114// specifies the number of columns to scroll.
1115// If in icon, small icon or list view mode, y specifies the number of pixels
1116// to scroll. If in report view mode, y specifies the number of lines to scroll.
debe6624 1117bool wxListCtrl::ScrollList(int dx, int dy)
2bda0e17 1118{
0b5efdc7 1119 return (ListView_Scroll((HWND) GetHWND(), dx, dy) != 0);
2bda0e17
KB
1120}
1121
1122// Sort items.
1123
1124// fn is a function which takes 3 long arguments: item1, item2, data.
1125// item1 is the long data associated with a first item (NOT the index).
1126// item2 is the long data associated with a second item (NOT the index).
1127// data is the same value as passed to SortItems.
1128// The return value is a negative number if the first item should precede the second
1129// item, a positive number of the second item should precede the first,
1130// or zero if the two items are equivalent.
1131
1132// data is arbitrary data to be passed to the sort function.
1133bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1134{
0b5efdc7 1135 return (ListView_SortItems((HWND) GetHWND(), (PFNLVCOMPARE) fn, data) != 0);
2bda0e17
KB
1136}
1137
debe6624 1138bool wxListCtrl::MSWCommand(WXUINT cmd, WXWORD id)
2bda0e17 1139{
0b5efdc7 1140 if (cmd == EN_UPDATE)
acb62b84 1141 {
0b5efdc7
VZ
1142 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1143 event.SetEventObject( this );
1144 ProcessCommand(event);
1145 return TRUE;
acb62b84 1146 }
0b5efdc7 1147 else if (cmd == EN_KILLFOCUS)
acb62b84 1148 {
0b5efdc7
VZ
1149 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1150 event.SetEventObject( this );
1151 ProcessCommand(event);
1152 return TRUE;
acb62b84 1153 }
0b5efdc7
VZ
1154 else return FALSE;
1155}
1156
1157bool wxListCtrl::MSWNotify(WXWPARAM wParam, WXLPARAM lParam, WXLPARAM *result)
1158{
1159 wxListEvent event(wxEVT_NULL, m_windowId);
1160 wxEventType eventType = wxEVT_NULL;
1161 NMHDR *hdr1 = (NMHDR *) lParam;
1162 switch ( hdr1->code )
acb62b84 1163 {
0b5efdc7
VZ
1164 case LVN_BEGINRDRAG:
1165 eventType = wxEVT_COMMAND_LIST_BEGIN_RDRAG;
1166 // fall through
1167
1168 case LVN_BEGINDRAG:
1169 if ( eventType == wxEVT_NULL )
1170 {
1171 eventType = wxEVT_COMMAND_LIST_BEGIN_DRAG;
1172 }
1173
1174 {
1175 NM_LISTVIEW *hdr = (NM_LISTVIEW *)lParam;
1176 event.m_itemIndex = hdr->iItem;
1177 event.m_pointDrag.x = hdr->ptAction.x;
1178 event.m_pointDrag.y = hdr->ptAction.y;
1179 }
1180 break;
1181
1182 case LVN_BEGINLABELEDIT:
1183 {
1184 eventType = wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT;
1185 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1186 wxConvertFromMSWListItem(this, event.m_item, info->item, (HWND) GetHWND());
1187 break;
1188 }
1189
1190 case LVN_COLUMNCLICK:
1191 {
1192 eventType = wxEVT_COMMAND_LIST_COL_CLICK;
1193 NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1194 event.m_itemIndex = -1;
1195 event.m_col = hdr->iSubItem;
1196 break;
1197 }
1198 case LVN_DELETEALLITEMS:
1199 {
1200 eventType = wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS;
1201 // NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1202 event.m_itemIndex = -1;
1203 break;
1204 }
1205 case LVN_DELETEITEM:
1206 {
1207 eventType = wxEVT_COMMAND_LIST_DELETE_ITEM;
1208 NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1209 event.m_itemIndex = hdr->iItem;
1210 break;
1211 }
1212 case LVN_ENDLABELEDIT:
1213 {
1214 eventType = wxEVT_COMMAND_LIST_END_LABEL_EDIT;
1215 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1216 wxConvertFromMSWListItem(this, event.m_item, info->item, (HWND) GetHWND());
1217 if ( info->item.pszText == NULL || info->item.iItem == -1 )
1218 event.m_cancelled = TRUE;
1219 break;
1220 }
1221 case LVN_GETDISPINFO:
1222 {
1223 // return FALSE;
1224 // TODO: some text buffering here, I think
1225 // TODO: API for getting Windows to retrieve values
1226 // on demand.
1227 eventType = wxEVT_COMMAND_LIST_GET_INFO;
1228 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1229 wxConvertFromMSWListItem(this, event.m_item, info->item, (HWND) GetHWND());
1230 break;
1231 }
1232 case LVN_INSERTITEM:
1233 {
1234 eventType = wxEVT_COMMAND_LIST_INSERT_ITEM;
1235 NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1236 event.m_itemIndex = hdr->iItem;
1237 break;
1238 }
1239 case LVN_ITEMCHANGED:
1240 {
1241 // This needs to be sent to wxListCtrl as a rather more
1242 // concrete event. For now, just detect a selection
1243 // or deselection.
1244 NM_LISTVIEW* hdr = (NM_LISTVIEW*)lParam;
1245 if ( (hdr->uNewState & LVIS_SELECTED) && !(hdr->uOldState & LVIS_SELECTED) )
1246 {
1247 eventType = wxEVT_COMMAND_LIST_ITEM_SELECTED;
1248 event.m_itemIndex = hdr->iItem;
1249 }
1250 else if ( !(hdr->uNewState & LVIS_SELECTED) && (hdr->uOldState & LVIS_SELECTED) )
1251 {
1252 eventType = wxEVT_COMMAND_LIST_ITEM_DESELECTED;
1253 event.m_itemIndex = hdr->iItem;
1254 }
1255 else
1256 return FALSE;
1257 break;
1258 }
1259 case LVN_KEYDOWN:
1260 {
1261 eventType = wxEVT_COMMAND_LIST_KEY_DOWN;
1262 LV_KEYDOWN *info = (LV_KEYDOWN *)lParam;
1263 event.m_code = wxCharCodeMSWToWX(info->wVKey);
1264 break;
1265 }
1266 case LVN_SETDISPINFO:
1267 {
1268 eventType = wxEVT_COMMAND_LIST_SET_INFO;
1269 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1270 wxConvertFromMSWListItem(this, event.m_item, info->item, (HWND) GetHWND());
1271 break;
1272 }
1273
1274 default :
1275 return wxControl::MSWNotify(wParam, lParam, result);
acb62b84
VZ
1276 }
1277
0b5efdc7
VZ
1278 event.SetEventObject( this );
1279 event.SetEventType(eventType);
acb62b84 1280
0b5efdc7
VZ
1281 if ( !GetEventHandler()->ProcessEvent(event) )
1282 return FALSE;
acb62b84 1283
0b5efdc7 1284 if (hdr1->code == LVN_GETDISPINFO)
acb62b84 1285 {
0b5efdc7
VZ
1286 LV_DISPINFO *info = (LV_DISPINFO *)lParam;
1287 if ( info->item.mask & LVIF_TEXT )
1288 {
1289 if ( !event.m_item.m_text.IsNull() )
1290 {
1291 info->item.pszText = AddPool(event.m_item.m_text);
1292 info->item.cchTextMax = strlen(info->item.pszText) + 1;
1293 }
1294 }
1295 // wxConvertToMSWListItem(this, event.m_item, info->item);
acb62b84 1296 }
acb62b84 1297
0b5efdc7 1298 *result = !event.IsAllowed();
fd3f686c 1299
0b5efdc7 1300 return TRUE;
2bda0e17
KB
1301}
1302
1303char *wxListCtrl::AddPool(const wxString& str)
1304{
0b5efdc7
VZ
1305 // Remove the first element if 3 strings exist
1306 if ( m_stringPool.Number() == 3 )
1307 {
1308 wxNode *node = m_stringPool.First();
1309 delete[] (char *)node->Data();
1310 delete node;
1311 }
1312 wxNode *node = m_stringPool.Add((char *) (const char *)str);
1313 return (char *)node->Data();
2bda0e17
KB
1314}
1315
1316// List item structure
0b5efdc7 1317wxListItem::wxListItem()
2bda0e17
KB
1318{
1319 m_mask = 0;
1320 m_itemId = 0;
1321 m_col = 0;
1322 m_state = 0;
1323 m_stateMask = 0;
1324 m_image = 0;
0b5efdc7 1325 m_data = 0;
2bda0e17 1326
0b5efdc7
VZ
1327 m_format = wxLIST_FORMAT_CENTRE;
1328 m_width = 0;
2bda0e17
KB
1329}
1330
1331static void wxConvertFromMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& lvItem, HWND getFullInfo)
1332{
0b5efdc7
VZ
1333 info.m_data = lvItem.lParam;
1334 info.m_mask = 0;
1335 info.m_state = 0;
1336 info.m_stateMask = 0;
1337 info.m_itemId = lvItem.iItem;
acb62b84 1338
0b5efdc7 1339 long oldMask = lvItem.mask;
acb62b84 1340
0b5efdc7
VZ
1341 bool needText = FALSE;
1342 if (getFullInfo != 0)
acb62b84 1343 {
0b5efdc7
VZ
1344 if ( lvItem.mask & LVIF_TEXT )
1345 needText = FALSE;
1346 else
1347 needText = TRUE;
acb62b84 1348
0b5efdc7
VZ
1349 if ( needText )
1350 {
1351 lvItem.pszText = new char[513];
1352 lvItem.cchTextMax = 512;
1353 }
1354 // lvItem.mask |= TVIF_HANDLE | TVIF_STATE | TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_CHILDREN | TVIF_PARAM ;
1355 lvItem.mask |= LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM ;
1356 ::SendMessage(getFullInfo, LVM_GETITEM, 0, (LPARAM)& lvItem) ;
1357 }
acb62b84 1358
0b5efdc7 1359 if ( lvItem.mask & LVIF_STATE )
acb62b84 1360 {
0b5efdc7
VZ
1361 info.m_mask |= wxLIST_MASK_STATE;
1362
1363 if ( lvItem.stateMask & LVIS_CUT)
1364 {
1365 info.m_stateMask |= wxLIST_STATE_CUT ;
1366 if ( lvItem.state & LVIS_CUT )
1367 info.m_state |= wxLIST_STATE_CUT ;
1368 }
1369 if ( lvItem.stateMask & LVIS_DROPHILITED)
1370 {
1371 info.m_stateMask |= wxLIST_STATE_DROPHILITED ;
1372 if ( lvItem.state & LVIS_DROPHILITED )
1373 info.m_state |= wxLIST_STATE_DROPHILITED ;
1374 }
1375 if ( lvItem.stateMask & LVIS_FOCUSED)
1376 {
1377 info.m_stateMask |= wxLIST_STATE_FOCUSED ;
1378 if ( lvItem.state & LVIS_FOCUSED )
1379 info.m_state |= wxLIST_STATE_FOCUSED ;
1380 }
1381 if ( lvItem.stateMask & LVIS_SELECTED)
1382 {
1383 info.m_stateMask |= wxLIST_STATE_SELECTED ;
1384 if ( lvItem.state & LVIS_SELECTED )
1385 info.m_state |= wxLIST_STATE_SELECTED ;
1386 }
acb62b84 1387 }
0b5efdc7
VZ
1388
1389 if ( lvItem.mask & LVIF_TEXT )
acb62b84 1390 {
0b5efdc7
VZ
1391 info.m_mask |= wxLIST_MASK_TEXT;
1392 info.m_text = lvItem.pszText;
acb62b84 1393 }
0b5efdc7 1394 if ( lvItem.mask & LVIF_IMAGE )
acb62b84 1395 {
0b5efdc7
VZ
1396 info.m_mask |= wxLIST_MASK_IMAGE;
1397 info.m_image = lvItem.iImage;
acb62b84 1398 }
0b5efdc7
VZ
1399 if ( lvItem.mask & LVIF_PARAM )
1400 info.m_mask |= wxLIST_MASK_DATA;
1401 if ( lvItem.mask & LVIF_DI_SETITEM )
1402 info.m_mask |= wxLIST_SET_ITEM;
1403 info.m_col = lvItem.iSubItem;
1404
1405 if (needText)
acb62b84 1406 {
0b5efdc7
VZ
1407 if (lvItem.pszText)
1408 delete[] lvItem.pszText;
acb62b84 1409 }
0b5efdc7 1410 lvItem.mask = oldMask ;
2bda0e17
KB
1411}
1412
1413static void wxConvertToMSWListItem(const wxListCtrl *ctrl, wxListItem& info, LV_ITEM& lvItem)
1414{
0b5efdc7 1415 lvItem.iItem = (int) info.m_itemId ;
acb62b84 1416
0b5efdc7
VZ
1417 lvItem.iImage = info.m_image ;
1418 lvItem.lParam = info.m_data;
1419 lvItem.stateMask = 0;
1420 lvItem.state = 0;
1421 lvItem.mask = 0;
1422 lvItem.iSubItem = info.m_col;
acb62b84 1423
0b5efdc7 1424 if (info.m_mask & wxLIST_MASK_STATE)
acb62b84 1425 {
0b5efdc7
VZ
1426 lvItem.mask |= LVIF_STATE ;
1427 if (info.m_stateMask & wxLIST_STATE_CUT)
1428 {
1429 lvItem.stateMask |= LVIS_CUT ;
1430 if (info.m_state & wxLIST_STATE_CUT)
1431 lvItem.state |= LVIS_CUT;
1432 }
1433 if (info.m_stateMask & wxLIST_STATE_DROPHILITED)
1434 {
1435 lvItem.stateMask |= LVIS_DROPHILITED;
1436 if (info.m_state & wxLIST_STATE_DROPHILITED)
1437 lvItem.state |= LVIS_DROPHILITED;
1438 }
1439 if (info.m_stateMask & wxLIST_STATE_FOCUSED)
1440 {
1441 lvItem.stateMask |= LVIS_FOCUSED;
1442 if (info.m_state & wxLIST_STATE_FOCUSED)
1443 lvItem.state |= LVIS_FOCUSED;
1444 }
1445 if (info.m_stateMask & wxLIST_STATE_SELECTED)
1446 {
1447 lvItem.stateMask |= LVIS_SELECTED;
1448 if (info.m_state & wxLIST_STATE_SELECTED)
1449 lvItem.state |= LVIS_SELECTED;
1450 }
acb62b84 1451 }
acb62b84 1452
0b5efdc7 1453 if (info.m_mask & wxLIST_MASK_TEXT)
acb62b84 1454 {
0b5efdc7
VZ
1455 lvItem.mask |= LVIF_TEXT ;
1456 if ( ctrl->GetWindowStyleFlag() & wxLC_USER_TEXT )
1457 {
1458 lvItem.pszText = LPSTR_TEXTCALLBACK;
1459 }
1460 else
1461 {
1462 lvItem.pszText = (char *) (const char *)info.m_text ;
1463 if ( lvItem.pszText )
1464 lvItem.cchTextMax = info.m_text.Length();
1465 else
1466 lvItem.cchTextMax = 0;
1467 }
acb62b84 1468 }
0b5efdc7
VZ
1469 if (info.m_mask & wxLIST_MASK_IMAGE)
1470 lvItem.mask |= LVIF_IMAGE ;
1471 if (info.m_mask & wxLIST_MASK_DATA)
1472 lvItem.mask |= LVIF_PARAM ;
2bda0e17
KB
1473}
1474
1475// List event
92976ab6 1476IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
2bda0e17 1477
fd3f686c 1478wxListEvent::wxListEvent(wxEventType commandType, int id)
0b5efdc7 1479: wxNotifyEvent(commandType, id)
2bda0e17 1480{
0b5efdc7
VZ
1481 m_code = 0;
1482 m_itemIndex = 0;
1483 m_col = 0;
1484 m_cancelled = FALSE;
2bda0e17
KB
1485}
1486
1487#endif
1488