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