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