]> git.saurik.com Git - wxWidgets.git/blame - src/generic/listctrl.cpp
Add benchmarks for wxImage and raw bitmap access to the graphics test.
[wxWidgets.git] / src / generic / listctrl.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
faa94f3e 2// Name: src/generic/listctrl.cpp
54442116 3// Purpose: generic implementation of wxListCtrl
c801d85f 4// Author: Robert Roebling
cf1dfa6b 5// Vadim Zeitlin (virtual list control support)
0208334d
RR
6// Id: $Id$
7// Copyright: (c) 1998 Robert Roebling
65571936 8// Licence: wxWindows licence
c801d85f
KB
9/////////////////////////////////////////////////////////////////////////////
10
277ea1b4
DS
11// TODO
12//
13// 1. we need to implement searching/sorting for virtual controls somehow
ad486020 14// 2. when changing selection the lines are refreshed twice
b54e41c5 15
1370703e 16
1e6d9499
JS
17// For compilers that support precompilation, includes "wx.h".
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
a2e5beee 21 #pragma hdrstop
1e6d9499
JS
22#endif
23
1e6feb95
VZ
24#if wxUSE_LISTCTRL
25
e2bc1d69 26#include "wx/listctrl.h"
2b5f62a0 27
2a673eb1 28#ifndef WX_PRECOMP
e409b62a
PC
29 #include "wx/scrolwin.h"
30 #include "wx/timer.h"
31 #include "wx/settings.h"
2a673eb1 32 #include "wx/dynarray.h"
f7354d92 33 #include "wx/dcclient.h"
2a673eb1 34 #include "wx/dcscreen.h"
18680f86 35 #include "wx/math.h"
5595181f 36 #include "wx/settings.h"
64374d1b 37 #include "wx/sizer.h"
2a673eb1
WS
38#endif
39
e409b62a 40#include "wx/imaglist.h"
9c7f49f5 41#include "wx/renderer.h"
74491934 42#include "wx/generic/private/listctrl.h"
9c7f49f5 43
f89d65ea 44#ifdef __WXMAC__
33ddeaba 45 #include "wx/osx/private.h"
f89d65ea 46#endif
e1d1f4bd 47
5d55031c
VZ
48#if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
49 #define "wx/msw/wrapwin.h"
50#endif
8158e0e1 51
e1d1f4bd
RD
52// NOTE: If using the wxListBox visual attributes works everywhere then this can
53// be removed, as well as the #else case below.
54#define _USE_VISATTR 0
55
56
cf1dfa6b
VZ
57// ----------------------------------------------------------------------------
58// constants
59// ----------------------------------------------------------------------------
60
f8252483
JS
61// // the height of the header window (FIXME: should depend on its font!)
62// static const int HEADER_HEIGHT = 23;
cf1dfa6b 63
cf1dfa6b 64static const int SCROLL_UNIT_X = 15;
cf1dfa6b
VZ
65
66// the spacing between the lines (in report mode)
b54e41c5 67static const int LINE_SPACING = 0;
cf1dfa6b
VZ
68
69// extra margins around the text label
ccdbdc89
RR
70#ifdef __WXGTK__
71static const int EXTRA_WIDTH = 6;
72#else
ae7cbd1a 73static const int EXTRA_WIDTH = 4;
ccdbdc89 74#endif
43cb7161
RR
75
76#ifdef __WXGTK__
77static const int EXTRA_HEIGHT = 6;
78#else
cf1dfa6b 79static const int EXTRA_HEIGHT = 4;
43cb7161 80#endif
cf1dfa6b 81
13602ebd
VZ
82// margin between the window and the items
83static const int EXTRA_BORDER_X = 2;
84static const int EXTRA_BORDER_Y = 2;
85
cf1dfa6b 86// offset for the header window
36a03ed8
RR
87static const int HEADER_OFFSET_X = 0;
88static const int HEADER_OFFSET_Y = 0;
cf1dfa6b 89
13602ebd
VZ
90// margin between rows of icons in [small] icon view
91static const int MARGIN_BETWEEN_ROWS = 6;
92
cf1dfa6b
VZ
93// when autosizing the columns, add some slack
94static const int AUTOSIZE_COL_MARGIN = 10;
95
98cb8dcb 96// default width for the header columns
cf1dfa6b 97static const int WIDTH_COL_DEFAULT = 80;
cf1dfa6b 98
06b781c7
VZ
99// the space between the image and the text in the report mode
100static const int IMAGE_MARGIN_IN_REPORT_MODE = 5;
101
936632d3
VZ
102// the space between the image and the text in the report mode in header
103static const int HEADER_IMAGE_MARGIN_IN_REPORT_MODE = 2;
104
efbb7287 105
cf1dfa6b 106
ad486020
FM
107// ----------------------------------------------------------------------------
108// arrays/list implementations
109// ----------------------------------------------------------------------------
efbb7287 110
2c1f73ee 111#include "wx/listimpl.cpp"
17a1ebd1 112WX_DEFINE_LIST(wxListItemDataList)
2c1f73ee 113
f6bcfd97 114#include "wx/arrimpl.cpp"
17a1ebd1 115WX_DEFINE_OBJARRAY(wxListLineDataArray)
f6bcfd97 116
24b9f055 117#include "wx/listimpl.cpp"
17a1ebd1 118WX_DEFINE_LIST(wxListHeaderDataList)
24b9f055 119
efbb7287 120
ad486020
FM
121// ----------------------------------------------------------------------------
122// wxListItemData
123// ----------------------------------------------------------------------------
c801d85f 124
850ed6e7
VZ
125wxListItemData::~wxListItemData()
126{
127 // in the virtual list control the attributes are managed by the main
128 // program, so don't delete them
129 if ( !m_owner->IsVirtual() )
850ed6e7 130 delete m_attr;
850ed6e7
VZ
131
132 delete m_rect;
133}
134
cf1dfa6b 135void wxListItemData::Init()
c801d85f 136{
92976ab6
RR
137 m_image = -1;
138 m_data = 0;
cf1dfa6b 139
0530737d 140 m_attr = NULL;
e1e955e1 141}
c801d85f 142
cf1dfa6b 143wxListItemData::wxListItemData(wxListMainWindow *owner)
c801d85f 144{
cf1dfa6b 145 Init();
0530737d 146
cf1dfa6b
VZ
147 m_owner = owner;
148
850ed6e7 149 if ( owner->InReportView() )
cf1dfa6b 150 m_rect = NULL;
cf1dfa6b 151 else
cf1dfa6b 152 m_rect = new wxRect;
e1e955e1 153}
c801d85f
KB
154
155void wxListItemData::SetItem( const wxListItem &info )
156{
cf1dfa6b 157 if ( info.m_mask & wxLIST_MASK_TEXT )
54442116 158 SetText(info.m_text);
cf1dfa6b 159 if ( info.m_mask & wxLIST_MASK_IMAGE )
54442116 160 m_image = info.m_image;
cf1dfa6b 161 if ( info.m_mask & wxLIST_MASK_DATA )
54442116 162 m_data = info.m_data;
0530737d
VZ
163
164 if ( info.HasAttributes() )
165 {
166 if ( m_attr )
c18fa7a4 167 m_attr->AssignFrom(*info.GetAttributes());
0530737d
VZ
168 else
169 m_attr = new wxListItemAttr(*info.GetAttributes());
170 }
171
cf1dfa6b
VZ
172 if ( m_rect )
173 {
174 m_rect->x =
175 m_rect->y =
176 m_rect->height = 0;
177 m_rect->width = info.m_width;
178 }
e1e955e1 179}
c801d85f 180
debe6624 181void wxListItemData::SetPosition( int x, int y )
c801d85f 182{
9a83f860 183 wxCHECK_RET( m_rect, wxT("unexpected SetPosition() call") );
cf1dfa6b
VZ
184
185 m_rect->x = x;
186 m_rect->y = y;
e1e955e1 187}
c801d85f 188
1e6d9499 189void wxListItemData::SetSize( int width, int height )
c801d85f 190{
9a83f860 191 wxCHECK_RET( m_rect, wxT("unexpected SetSize() call") );
c801d85f 192
cf1dfa6b
VZ
193 if ( width != -1 )
194 m_rect->width = width;
195 if ( height != -1 )
196 m_rect->height = height;
e1e955e1 197}
c801d85f 198
debe6624 199bool wxListItemData::IsHit( int x, int y ) const
c801d85f 200{
9a83f860 201 wxCHECK_MSG( m_rect, false, wxT("can't be called in this mode") );
cf1dfa6b 202
22a35096 203 return wxRect(GetX(), GetY(), GetWidth(), GetHeight()).Contains(x, y);
e1e955e1 204}
c801d85f 205
fd9811b1 206int wxListItemData::GetX() const
c801d85f 207{
9a83f860 208 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
cf1dfa6b
VZ
209
210 return m_rect->x;
e1e955e1 211}
c801d85f 212
fd9811b1 213int wxListItemData::GetY() const
c801d85f 214{
9a83f860 215 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
cf1dfa6b
VZ
216
217 return m_rect->y;
e1e955e1 218}
c801d85f 219
fd9811b1 220int wxListItemData::GetWidth() const
c801d85f 221{
9a83f860 222 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
cf1dfa6b
VZ
223
224 return m_rect->width;
e1e955e1 225}
c801d85f 226
fd9811b1 227int wxListItemData::GetHeight() const
c801d85f 228{
9a83f860 229 wxCHECK_MSG( m_rect, 0, wxT("can't be called in this mode") );
c801d85f 230
cf1dfa6b 231 return m_rect->height;
e1e955e1 232}
c801d85f 233
0530737d 234void wxListItemData::GetItem( wxListItem &info ) const
c801d85f 235{
39a7f085
VZ
236 long mask = info.m_mask;
237 if ( !mask )
39a7f085
VZ
238 // by default, get everything for backwards compatibility
239 mask = -1;
39a7f085
VZ
240
241 if ( mask & wxLIST_MASK_TEXT )
242 info.m_text = m_text;
243 if ( mask & wxLIST_MASK_IMAGE )
244 info.m_image = m_image;
245 if ( mask & wxLIST_MASK_DATA )
246 info.m_data = m_data;
c801d85f 247
0530737d
VZ
248 if ( m_attr )
249 {
250 if ( m_attr->HasTextColour() )
251 info.SetTextColour(m_attr->GetTextColour());
252 if ( m_attr->HasBackgroundColour() )
253 info.SetBackgroundColour(m_attr->GetBackgroundColour());
254 if ( m_attr->HasFont() )
255 info.SetFont(m_attr->GetFont());
256 }
e1e955e1 257}
c801d85f
KB
258
259//-----------------------------------------------------------------------------
260// wxListHeaderData
261//-----------------------------------------------------------------------------
262
0a816d95 263void wxListHeaderData::Init()
c801d85f 264{
92976ab6 265 m_mask = 0;
0a816d95 266 m_image = -1;
92976ab6
RR
267 m_format = 0;
268 m_width = 0;
269 m_xpos = 0;
270 m_ypos = 0;
271 m_height = 0;
df0edf44 272 m_state = 0;
e1e955e1 273}
c801d85f 274
0a816d95
VZ
275wxListHeaderData::wxListHeaderData()
276{
277 Init();
278}
279
c801d85f
KB
280wxListHeaderData::wxListHeaderData( const wxListItem &item )
281{
0a816d95
VZ
282 Init();
283
92976ab6 284 SetItem( item );
e1e955e1 285}
c801d85f
KB
286
287void wxListHeaderData::SetItem( const wxListItem &item )
288{
92976ab6 289 m_mask = item.m_mask;
cf1dfa6b 290
0a816d95
VZ
291 if ( m_mask & wxLIST_MASK_TEXT )
292 m_text = item.m_text;
293
294 if ( m_mask & wxLIST_MASK_IMAGE )
295 m_image = item.m_image;
296
297 if ( m_mask & wxLIST_MASK_FORMAT )
298 m_format = item.m_format;
299
300 if ( m_mask & wxLIST_MASK_WIDTH )
301 SetWidth(item.m_width);
6fef2483 302
df0edf44
KO
303 if ( m_mask & wxLIST_MASK_STATE )
304 SetState(item.m_state);
e1e955e1 305}
c801d85f 306
debe6624 307void wxListHeaderData::SetPosition( int x, int y )
c801d85f 308{
92976ab6
RR
309 m_xpos = x;
310 m_ypos = y;
e1e955e1 311}
c801d85f 312
debe6624 313void wxListHeaderData::SetHeight( int h )
c801d85f 314{
92976ab6 315 m_height = h;
e1e955e1 316}
c801d85f 317
debe6624 318void wxListHeaderData::SetWidth( int w )
c801d85f 319{
98cb8dcb 320 m_width = w < 0 ? WIDTH_COL_DEFAULT : w;
e1e955e1 321}
c801d85f 322
df0edf44
KO
323void wxListHeaderData::SetState( int flag )
324{
325 m_state = flag;
326}
327
debe6624 328void wxListHeaderData::SetFormat( int format )
c801d85f 329{
92976ab6 330 m_format = format;
e1e955e1 331}
c801d85f 332
fd9811b1 333bool wxListHeaderData::HasImage() const
c801d85f 334{
0a816d95 335 return m_image != -1;
e1e955e1 336}
c801d85f 337
c801d85f
KB
338bool wxListHeaderData::IsHit( int x, int y ) const
339{
92976ab6 340 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
e1e955e1 341}
c801d85f 342
0a816d95 343void wxListHeaderData::GetItem( wxListItem& item )
c801d85f 344{
82ab1ab1
VZ
345 long mask = item.m_mask;
346 if ( !mask )
347 {
348 // by default, get everything for backwards compatibility
349 mask = -1;
350 }
351
352 if ( mask & wxLIST_MASK_STATE )
353 item.m_state = m_state;
354 if ( mask & wxLIST_MASK_TEXT )
355 item.m_text = m_text;
356 if ( mask & wxLIST_MASK_IMAGE )
357 item.m_image = m_image;
358 if ( mask & wxLIST_MASK_WIDTH )
359 item.m_width = m_width;
360 if ( mask & wxLIST_MASK_FORMAT )
361 item.m_format = m_format;
e1e955e1 362}
c801d85f 363
fd9811b1 364int wxListHeaderData::GetImage() const
c801d85f 365{
92976ab6 366 return m_image;
e1e955e1 367}
c801d85f 368
fd9811b1 369int wxListHeaderData::GetWidth() const
c801d85f 370{
92976ab6 371 return m_width;
e1e955e1 372}
c801d85f 373
fd9811b1 374int wxListHeaderData::GetFormat() const
c801d85f 375{
92976ab6 376 return m_format;
e1e955e1 377}
c801d85f 378
df0edf44
KO
379int wxListHeaderData::GetState() const
380{
381 return m_state;
382}
383
c801d85f
KB
384//-----------------------------------------------------------------------------
385// wxListLineData
386//-----------------------------------------------------------------------------
387
cf1dfa6b
VZ
388inline int wxListLineData::GetMode() const
389{
b54e41c5 390 return m_owner->GetListCtrl()->GetWindowStyleFlag() & wxLC_MASK_TYPE;
cf1dfa6b 391}
c801d85f 392
cf1dfa6b 393inline bool wxListLineData::InReportView() const
c801d85f 394{
cf1dfa6b 395 return m_owner->HasFlag(wxLC_REPORT);
e1e955e1 396}
c801d85f 397
b54e41c5 398inline bool wxListLineData::IsVirtual() const
c801d85f 399{
cf1dfa6b
VZ
400 return m_owner->IsVirtual();
401}
2c1f73ee 402
5cd89174 403wxListLineData::wxListLineData( wxListMainWindow *owner )
cf1dfa6b
VZ
404{
405 m_owner = owner;
2c1f73ee 406
cf1dfa6b 407 if ( InReportView() )
cf1dfa6b 408 m_gi = NULL;
cf1dfa6b 409 else // !report
cf1dfa6b 410 m_gi = new GeometryInfo;
f6bcfd97 411
ca65c044 412 m_highlighted = false;
f6bcfd97 413
cf1dfa6b
VZ
414 InitItems( GetMode() == wxLC_REPORT ? m_owner->GetColumnCount() : 1 );
415}
f6bcfd97 416
cf1dfa6b
VZ
417void wxListLineData::CalculateSize( wxDC *dc, int spacing )
418{
222ed1d6 419 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
9a83f860 420 wxCHECK_RET( node, wxT("no subitems at all??") );
cf1dfa6b
VZ
421
422 wxListItemData *item = node->GetData();
423
94dd23ae
VZ
424 wxString s;
425 wxCoord lw, lh;
426
cf1dfa6b
VZ
427 switch ( GetMode() )
428 {
429 case wxLC_ICON:
430 case wxLC_SMALL_ICON:
94dd23ae 431 m_gi->m_rectAll.width = spacing;
cf1dfa6b 432
94dd23ae 433 s = item->GetText();
cf1dfa6b 434
94dd23ae
VZ
435 if ( s.empty() )
436 {
437 lh =
438 m_gi->m_rectLabel.width =
439 m_gi->m_rectLabel.height = 0;
92976ab6 440 }
94dd23ae 441 else // has label
92976ab6 442 {
92976ab6 443 dc->GetTextExtent( s, &lw, &lh );
cf1dfa6b
VZ
444 lw += EXTRA_WIDTH;
445 lh += EXTRA_HEIGHT;
2c1f73ee 446
94dd23ae
VZ
447 m_gi->m_rectAll.height = spacing + lh;
448 if (lw > spacing)
449 m_gi->m_rectAll.width = lw;
450
cf1dfa6b
VZ
451 m_gi->m_rectLabel.width = lw;
452 m_gi->m_rectLabel.height = lh;
94dd23ae 453 }
f6bcfd97 454
94dd23ae
VZ
455 if (item->HasImage())
456 {
457 int w, h;
458 m_owner->GetImageSize( item->GetImage(), w, h );
459 m_gi->m_rectIcon.width = w + 8;
460 m_gi->m_rectIcon.height = h + 8;
461
462 if ( m_gi->m_rectIcon.width > m_gi->m_rectAll.width )
463 m_gi->m_rectAll.width = m_gi->m_rectIcon.width;
464 if ( m_gi->m_rectIcon.height + lh > m_gi->m_rectAll.height - 4 )
465 m_gi->m_rectAll.height = m_gi->m_rectIcon.height + lh + 4;
466 }
f6bcfd97 467
94dd23ae
VZ
468 if ( item->HasText() )
469 {
470 m_gi->m_rectHighlight.width = m_gi->m_rectLabel.width;
471 m_gi->m_rectHighlight.height = m_gi->m_rectLabel.height;
472 }
473 else // no text, highlight the icon
474 {
475 m_gi->m_rectHighlight.width = m_gi->m_rectIcon.width;
476 m_gi->m_rectHighlight.height = m_gi->m_rectIcon.height;
477 }
478 break;
479
480 case wxLC_LIST:
481 s = item->GetTextForMeasuring();
482
483 dc->GetTextExtent( s, &lw, &lh );
94dd23ae
VZ
484 lw += EXTRA_WIDTH;
485 lh += EXTRA_HEIGHT;
486
487 m_gi->m_rectLabel.width = lw;
488 m_gi->m_rectLabel.height = lh;
489
490 m_gi->m_rectAll.width = lw;
491 m_gi->m_rectAll.height = lh;
f6bcfd97 492
94dd23ae
VZ
493 if (item->HasImage())
494 {
495 int w, h;
496 m_owner->GetImageSize( item->GetImage(), w, h );
497 m_gi->m_rectIcon.width = w;
498 m_gi->m_rectIcon.height = h;
499
500 m_gi->m_rectAll.width += 4 + w;
501 if (h > m_gi->m_rectAll.height)
502 m_gi->m_rectAll.height = h;
92976ab6 503 }
94dd23ae
VZ
504
505 m_gi->m_rectHighlight.width = m_gi->m_rectAll.width;
506 m_gi->m_rectHighlight.height = m_gi->m_rectAll.height;
92976ab6 507 break;
2c1f73ee 508
cf1dfa6b 509 case wxLC_REPORT:
9a83f860 510 wxFAIL_MSG( wxT("unexpected call to SetSize") );
92976ab6 511 break;
cf1dfa6b
VZ
512
513 default:
9a83f860 514 wxFAIL_MSG( wxT("unknown mode") );
277ea1b4 515 break;
e1e955e1 516 }
e1e955e1 517}
c801d85f 518
13602ebd 519void wxListLineData::SetPosition( int x, int y, int spacing )
c801d85f 520{
222ed1d6 521 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
9a83f860 522 wxCHECK_RET( node, wxT("no subitems at all??") );
2c1f73ee 523
cf1dfa6b 524 wxListItemData *item = node->GetData();
2c1f73ee 525
cf1dfa6b 526 switch ( GetMode() )
0b855868
RR
527 {
528 case wxLC_ICON:
cf1dfa6b
VZ
529 case wxLC_SMALL_ICON:
530 m_gi->m_rectAll.x = x;
531 m_gi->m_rectAll.y = y;
532
533 if ( item->HasImage() )
0b855868 534 {
c2569f41
VZ
535 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 4 +
536 (m_gi->m_rectAll.width - m_gi->m_rectIcon.width) / 2;
cf1dfa6b 537 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 4;
0b855868 538 }
cf1dfa6b
VZ
539
540 if ( item->HasText() )
0b855868 541 {
cf1dfa6b 542 if (m_gi->m_rectAll.width > spacing)
ccdbdc89 543 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
5d25c050 544 else
ccdbdc89 545 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2) + (spacing / 2) - (m_gi->m_rectLabel.width / 2);
cf1dfa6b 546 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + m_gi->m_rectAll.height + 2 - m_gi->m_rectLabel.height;
b54e41c5
VZ
547 m_gi->m_rectHighlight.x = m_gi->m_rectLabel.x - 2;
548 m_gi->m_rectHighlight.y = m_gi->m_rectLabel.y - 2;
bffa1c77 549 }
cf1dfa6b 550 else // no text, highlight the icon
0b855868 551 {
b54e41c5
VZ
552 m_gi->m_rectHighlight.x = m_gi->m_rectIcon.x - 4;
553 m_gi->m_rectHighlight.y = m_gi->m_rectIcon.y - 4;
bffa1c77 554 }
0b855868 555 break;
c801d85f 556
cf1dfa6b
VZ
557 case wxLC_LIST:
558 m_gi->m_rectAll.x = x;
559 m_gi->m_rectAll.y = y;
c801d85f 560
b54e41c5
VZ
561 m_gi->m_rectHighlight.x = m_gi->m_rectAll.x;
562 m_gi->m_rectHighlight.y = m_gi->m_rectAll.y;
cf1dfa6b 563 m_gi->m_rectLabel.y = m_gi->m_rectAll.y + 2;
c801d85f 564
cf1dfa6b
VZ
565 if (item->HasImage())
566 {
567 m_gi->m_rectIcon.x = m_gi->m_rectAll.x + 2;
568 m_gi->m_rectIcon.y = m_gi->m_rectAll.y + 2;
ccdbdc89 569 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + 4 + (EXTRA_WIDTH/2) + m_gi->m_rectIcon.width;
cf1dfa6b
VZ
570 }
571 else
572 {
ccdbdc89 573 m_gi->m_rectLabel.x = m_gi->m_rectAll.x + (EXTRA_WIDTH/2);
cf1dfa6b
VZ
574 }
575 break;
c801d85f 576
cf1dfa6b 577 case wxLC_REPORT:
9a83f860 578 wxFAIL_MSG( wxT("unexpected call to SetPosition") );
cf1dfa6b 579 break;
c801d85f 580
cf1dfa6b 581 default:
9a83f860 582 wxFAIL_MSG( wxT("unknown mode") );
277ea1b4 583 break;
cf1dfa6b 584 }
e1e955e1 585}
c801d85f 586
debe6624 587void wxListLineData::InitItems( int num )
c801d85f 588{
2c1f73ee 589 for (int i = 0; i < num; i++)
cf1dfa6b 590 m_items.Append( new wxListItemData(m_owner) );
e1e955e1 591}
c801d85f 592
debe6624 593void wxListLineData::SetItem( int index, const wxListItem &info )
c801d85f 594{
222ed1d6 595 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
9a83f860 596 wxCHECK_RET( node, wxT("invalid column index in SetItem") );
1370703e
VZ
597
598 wxListItemData *item = node->GetData();
599 item->SetItem( info );
e1e955e1 600}
c801d85f 601
1e6d9499 602void wxListLineData::GetItem( int index, wxListItem &info )
c801d85f 603{
222ed1d6 604 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
139adb6a
RR
605 if (node)
606 {
2c1f73ee 607 wxListItemData *item = node->GetData();
139adb6a
RR
608 item->GetItem( info );
609 }
e1e955e1 610}
c801d85f 611
54442116 612wxString wxListLineData::GetText(int index) const
c801d85f 613{
54442116
VZ
614 wxString s;
615
222ed1d6 616 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
139adb6a
RR
617 if (node)
618 {
2c1f73ee 619 wxListItemData *item = node->GetData();
54442116 620 s = item->GetText();
139adb6a 621 }
54442116
VZ
622
623 return s;
e1e955e1 624}
c801d85f 625
fbfb8bcc 626void wxListLineData::SetText( int index, const wxString& s )
c801d85f 627{
222ed1d6 628 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
139adb6a
RR
629 if (node)
630 {
2c1f73ee 631 wxListItemData *item = node->GetData();
139adb6a
RR
632 item->SetText( s );
633 }
e1e955e1 634}
c801d85f 635
cf1dfa6b 636void wxListLineData::SetImage( int index, int image )
c801d85f 637{
222ed1d6 638 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
9a83f860 639 wxCHECK_RET( node, wxT("invalid column index in SetImage()") );
cf1dfa6b
VZ
640
641 wxListItemData *item = node->GetData();
642 item->SetImage(image);
643}
644
645int wxListLineData::GetImage( int index ) const
646{
222ed1d6 647 wxListItemDataList::compatibility_iterator node = m_items.Item( index );
9a83f860 648 wxCHECK_MSG( node, -1, wxT("invalid column index in GetImage()") );
cf1dfa6b
VZ
649
650 wxListItemData *item = node->GetData();
651 return item->GetImage();
e1e955e1 652}
c801d85f 653
6c02c329
VZ
654wxListItemAttr *wxListLineData::GetAttr() const
655{
222ed1d6 656 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
9a83f860 657 wxCHECK_MSG( node, NULL, wxT("invalid column index in GetAttr()") );
6c02c329
VZ
658
659 wxListItemData *item = node->GetData();
660 return item->GetAttr();
661}
662
663void wxListLineData::SetAttr(wxListItemAttr *attr)
664{
222ed1d6 665 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
9a83f860 666 wxCHECK_RET( node, wxT("invalid column index in SetAttr()") );
6c02c329
VZ
667
668 wxListItemData *item = node->GetData();
669 item->SetAttr(attr);
670}
671
ed84dc74
VZ
672void wxListLineData::ApplyAttributes(wxDC *dc,
673 const wxRect& rectHL,
674 bool highlighted,
675 bool current)
0530737d 676{
ed84dc74
VZ
677 const wxListItemAttr * const attr = GetAttr();
678
679 wxWindow * const listctrl = m_owner->GetParent();
680
681 const bool hasFocus = listctrl->HasFocus()
682#if defined(__WXMAC__) && !defined(__WXUNIVERSAL__) && wxOSX_USE_CARBON
683 && IsControlActive( (ControlRef)listctrl->GetHandle() )
684#endif
685 ;
0e980f91
VZ
686
687 // fg colour
688
689 // don't use foreground colour for drawing highlighted items - this might
470caaf9
VZ
690 // make them completely invisible (and there is no way to do bit
691 // arithmetics on wxColour, unfortunately)
0e980f91
VZ
692 wxColour colText;
693 if ( highlighted )
d6d8b172 694 {
ed84dc74
VZ
695#ifdef __WXMAC__
696 if ( hasFocus )
d6d8b172
KO
697 colText = *wxWHITE;
698 else
699 colText = *wxBLACK;
4b8fa634 700#else
887b919b
JS
701 if ( hasFocus )
702 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT);
703 else
704 colText = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT);
4b8fa634 705#endif
ed84dc74 706 }
277ea1b4
DS
707 else if ( attr && attr->HasTextColour() )
708 colText = attr->GetTextColour();
0530737d 709 else
277ea1b4 710 colText = listctrl->GetForegroundColour();
0530737d 711
0e980f91
VZ
712 dc->SetTextForeground(colText);
713
714 // font
715 wxFont font;
0530737d 716 if ( attr && attr->HasFont() )
0e980f91 717 font = attr->GetFont();
0530737d 718 else
0e980f91 719 font = listctrl->GetFont();
0e980f91
VZ
720
721 dc->SetFont(font);
722
ed84dc74
VZ
723 // background
724 if ( highlighted )
ccdbdc89 725 {
ed84dc74
VZ
726 // Use the renderer method to ensure that the selected items use the
727 // native look.
728 int flags = wxCONTROL_SELECTED;
729 if ( hasFocus )
d67fc8b7 730 flags |= wxCONTROL_FOCUSED;
ed84dc74
VZ
731 if (current)
732 flags |= wxCONTROL_CURRENT;
d67fc8b7 733 wxRendererNative::Get().
ed84dc74
VZ
734 DrawItemSelectionRect( m_owner, *dc, rectHL, flags );
735 }
736 else if ( attr && attr->HasBackgroundColour() )
737 {
738 // Draw the background using the items custom background colour.
739 dc->SetBrush(attr->GetBackgroundColour());
740 dc->SetPen(*wxTRANSPARENT_PEN);
741 dc->DrawRectangle(rectHL);
ccdbdc89 742 }
0e980f91 743
94dd23ae
VZ
744 // just for debugging to better see where the items are
745#if 0
746 dc->SetPen(*wxRED_PEN);
747 dc->SetBrush(*wxTRANSPARENT_BRUSH);
748 dc->DrawRectangle( m_gi->m_rectAll );
749 dc->SetPen(*wxGREEN_PEN);
750 dc->DrawRectangle( m_gi->m_rectIcon );
277ea1b4 751#endif
ed84dc74
VZ
752}
753
754void wxListLineData::Draw(wxDC *dc, bool current)
755{
756 wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
757 wxCHECK_RET( node, wxT("no subitems at all??") );
758
759 ApplyAttributes(dc, m_gi->m_rectHighlight, IsHighlighted(), current);
94dd23ae 760
5cd89174
VZ
761 wxListItemData *item = node->GetData();
762 if (item->HasImage())
763 {
94dd23ae
VZ
764 // centre the image inside our rectangle, this looks nicer when items
765 // ae aligned in a row
766 const wxRect& rectIcon = m_gi->m_rectIcon;
767
768 m_owner->DrawImage(item->GetImage(), dc, rectIcon.x, rectIcon.y);
5cd89174
VZ
769 }
770
771 if (item->HasText())
772 {
94dd23ae 773 const wxRect& rectLabel = m_gi->m_rectLabel;
06b781c7
VZ
774
775 wxDCClipper clipper(*dc, rectLabel);
94dd23ae 776 dc->DrawText(item->GetText(), rectLabel.x, rectLabel.y);
5cd89174
VZ
777 }
778}
779
780void wxListLineData::DrawInReportMode( wxDC *dc,
938b652b 781 const wxRect& rect,
5cd89174 782 const wxRect& rectHL,
32fc355f
RR
783 bool highlighted,
784 bool current )
c801d85f 785{
6c02c329
VZ
786 // TODO: later we should support setting different attributes for
787 // different columns - to do it, just add "col" argument to
0e980f91 788 // GetAttr() and move these lines into the loop below
ed84dc74
VZ
789
790 ApplyAttributes(dc, rectHL, highlighted, current);
004fd0c8 791
938b652b 792 wxCoord x = rect.x + HEADER_OFFSET_X,
ebadbb76 793 yMid = rect.y + rect.height/2;
ccdbdc89
RR
794#ifdef __WXGTK__
795 // This probably needs to be done
796 // on all platforms as the icons
797 // otherwise nearly touch the border
798 x += 2;
799#endif
cf1dfa6b 800
904ccf52 801 size_t col = 0;
222ed1d6 802 for ( wxListItemDataList::compatibility_iterator node = m_items.GetFirst();
904ccf52
VZ
803 node;
804 node = node->GetNext(), col++ )
5cd89174
VZ
805 {
806 wxListItemData *item = node->GetData();
cf1dfa6b 807
904ccf52 808 int width = m_owner->GetColumnWidth(col);
5cd89174 809 int xOld = x;
06b781c7 810 x += width;
cf1dfa6b 811
cff48ba8
VZ
812 width -= 8;
813 const int wText = width;
f9f37ee2
VZ
814 wxDCClipper clipper(*dc, xOld, rect.y, wText, rect.height);
815
5cd89174
VZ
816 if ( item->HasImage() )
817 {
818 int ix, iy;
5cd89174 819 m_owner->GetImageSize( item->GetImage(), ix, iy );
ebadbb76 820 m_owner->DrawImage( item->GetImage(), dc, xOld, yMid - iy/2 );
cf1dfa6b 821
06b781c7 822 ix += IMAGE_MARGIN_IN_REPORT_MODE;
cf1dfa6b 823
06b781c7
VZ
824 xOld += ix;
825 width -= ix;
826 }
827
5cd89174 828 if ( item->HasText() )
cff48ba8 829 DrawTextFormatted(dc, item->GetText(), col, xOld, yMid, width);
2b5f62a0
VZ
830 }
831}
832
833void wxListLineData::DrawTextFormatted(wxDC *dc,
359fb29e 834 const wxString& textOrig,
2b5f62a0
VZ
835 int col,
836 int x,
ebadbb76 837 int yMid,
2b5f62a0
VZ
838 int width)
839{
359fb29e
VZ
840 // we don't support displaying multiple lines currently (and neither does
841 // wxMSW FWIW) so just merge all the lines
842 wxString text(textOrig);
9a83f860 843 text.Replace(wxT("\n"), wxT(" "));
359fb29e 844
ebadbb76
VZ
845 wxCoord w, h;
846 dc->GetTextExtent(text, &w, &h);
847
848 const wxCoord y = yMid - (h + 1)/2;
849
850 wxDCClipper clipper(*dc, x, y, width, h);
2b5f62a0
VZ
851
852 // determine if the string can fit inside the current width
2b5f62a0
VZ
853 if (w <= width)
854 {
e1e1272f 855 // it can, draw it using the items alignment
ebadbb76 856 wxListItem item;
2b5f62a0 857 m_owner->GetColumn(col, item);
e1e1272f
VZ
858 switch ( item.GetAlign() )
859 {
e1e1272f
VZ
860 case wxLIST_FORMAT_LEFT:
861 // nothing to do
862 break;
863
864 case wxLIST_FORMAT_RIGHT:
865 x += width - w;
866 break;
867
868 case wxLIST_FORMAT_CENTER:
869 x += (width - w) / 2;
870 break;
277ea1b4
DS
871
872 default:
9a83f860 873 wxFAIL_MSG( wxT("unknown list item format") );
277ea1b4 874 break;
e1e1272f
VZ
875 }
876
877 dc->DrawText(text, x, y);
2b5f62a0
VZ
878 }
879 else // otherwise, truncate and add an ellipsis if possible
880 {
881 // determine the base width
ebadbb76
VZ
882 wxString ellipsis(wxT("..."));
883 wxCoord base_w;
2b5f62a0
VZ
884 dc->GetTextExtent(ellipsis, &base_w, &h);
885
886 // continue until we have enough space or only one character left
5175dbbd 887 wxCoord w_c, h_c;
faa94f3e 888 size_t len = text.length();
ebadbb76 889 wxString drawntext = text.Left(len);
5175dbbd 890 while (len > 1)
2b5f62a0 891 {
5175dbbd
VS
892 dc->GetTextExtent(drawntext.Last(), &w_c, &h_c);
893 drawntext.RemoveLast();
894 len--;
895 w -= w_c;
2b5f62a0
VZ
896 if (w + base_w <= width)
897 break;
2b5f62a0
VZ
898 }
899
900 // if still not enough space, remove ellipsis characters
faa94f3e 901 while (ellipsis.length() > 0 && w + base_w > width)
2b5f62a0 902 {
faa94f3e 903 ellipsis = ellipsis.Left(ellipsis.length() - 1);
2b5f62a0 904 dc->GetTextExtent(ellipsis, &base_w, &h);
5cd89174 905 }
2b5f62a0
VZ
906
907 // now draw the text
908 dc->DrawText(drawntext, x, y);
909 dc->DrawText(ellipsis, x + w, y);
e1e955e1 910 }
e1e955e1 911}
c801d85f 912
b54e41c5 913bool wxListLineData::Highlight( bool on )
c801d85f 914{
9a83f860 915 wxCHECK_MSG( !IsVirtual(), false, wxT("unexpected call to Highlight") );
c801d85f 916
b54e41c5 917 if ( on == m_highlighted )
ca65c044 918 return false;
c801d85f 919
b54e41c5 920 m_highlighted = on;
c801d85f 921
ca65c044 922 return true;
e1e955e1 923}
c801d85f 924
b54e41c5 925void wxListLineData::ReverseHighlight( void )
c801d85f 926{
b54e41c5 927 Highlight(!IsHighlighted());
e1e955e1 928}
c801d85f
KB
929
930//-----------------------------------------------------------------------------
931// wxListHeaderWindow
932//-----------------------------------------------------------------------------
933
c801d85f 934BEGIN_EVENT_TABLE(wxListHeaderWindow,wxWindow)
63852e78
RR
935 EVT_PAINT (wxListHeaderWindow::OnPaint)
936 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse)
c801d85f
KB
937END_EVENT_TABLE()
938
0a816d95 939void wxListHeaderWindow::Init()
c801d85f 940{
d3b9f782 941 m_currentCursor = NULL;
ca65c044
WS
942 m_isDragging = false;
943 m_dirty = false;
06cd40a8 944 m_sendSetColumnWidth = false;
0a816d95
VZ
945}
946
947wxListHeaderWindow::wxListHeaderWindow()
948{
949 Init();
950
d3b9f782
VZ
951 m_owner = NULL;
952 m_resizeCursor = NULL;
e1e955e1 953}
c801d85f 954
0a816d95
VZ
955wxListHeaderWindow::wxListHeaderWindow( wxWindow *win,
956 wxWindowID id,
957 wxListMainWindow *owner,
958 const wxPoint& pos,
959 const wxSize& size,
960 long style,
961 const wxString &name )
962 : wxWindow( win, id, pos, size, style, name )
c801d85f 963{
0a816d95
VZ
964 Init();
965
63852e78 966 m_owner = owner;
63852e78 967 m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
f6bcfd97 968
35d4c967 969#if _USE_VISATTR
ca65c044 970 wxVisualAttributes attr = wxPanel::GetClassDefaultAttributes();
fa47d7a7
VS
971 SetOwnForegroundColour( attr.colFg );
972 SetOwnBackgroundColour( attr.colBg );
92a4e4de
VZ
973 if (!m_hasFont)
974 SetOwnFont( attr.font );
35d4c967 975#else
fa47d7a7
VS
976 SetOwnForegroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
977 SetOwnBackgroundColour( wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
92a4e4de
VZ
978 if (!m_hasFont)
979 SetOwnFont( wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT ));
35d4c967 980#endif
e1e955e1 981}
c801d85f 982
0a816d95 983wxListHeaderWindow::~wxListHeaderWindow()
a367b9b3 984{
63852e78 985 delete m_resizeCursor;
a367b9b3
JS
986}
987
2b5f62a0
VZ
988#ifdef __WXUNIVERSAL__
989#include "wx/univ/renderer.h"
990#include "wx/univ/theme.h"
991#endif
992
f6bcfd97
BP
993// shift the DC origin to match the position of the main window horz
994// scrollbar: this allows us to always use logical coords
995void wxListHeaderWindow::AdjustDC(wxDC& dc)
996{
06cd40a8
RR
997 wxGenericListCtrl *parent = m_owner->GetListCtrl();
998
f6bcfd97 999 int xpix;
06cd40a8 1000 parent->GetScrollPixelsPerUnit( &xpix, NULL );
f6bcfd97 1001
5eefe029 1002 int view_start;
06cd40a8 1003 parent->GetViewStart( &view_start, NULL );
5eefe029 1004
5eefe029
RR
1005
1006 int org_x = 0;
1007 int org_y = 0;
1008 dc.GetDeviceOrigin( &org_x, &org_y );
f6bcfd97
BP
1009
1010 // account for the horz scrollbar offset
807a572e
RR
1011#ifdef __WXGTK__
1012 if (GetLayoutDirection() == wxLayout_RightToLeft)
1013 {
1014 // Maybe we just have to check for m_signX
1015 // in the DC, but I leave the #ifdef __WXGTK__
1016 // for now
1017 dc.SetDeviceOrigin( org_x + (view_start * xpix), org_y );
1018 }
1019 else
1020#endif
1021 dc.SetDeviceOrigin( org_x - (view_start * xpix), org_y );
f6bcfd97
BP
1022}
1023
c801d85f
KB
1024void wxListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1025{
06cd40a8 1026 wxGenericListCtrl *parent = m_owner->GetListCtrl();
7d7b3f69 1027
63852e78 1028 wxPaintDC dc( this );
10bd0724 1029
f6bcfd97 1030 AdjustDC( dc );
bffa1c77 1031
63852e78 1032 dc.SetFont( GetFont() );
bd8289c1 1033
f6bcfd97
BP
1034 // width and height of the entire header window
1035 int w, h;
63852e78 1036 GetClientSize( &w, &h );
06cd40a8 1037 parent->CalcUnscrolledPosition(w, 0, &w, NULL);
c801d85f 1038
04ee05f9 1039 dc.SetBackgroundMode(wxBRUSHSTYLE_TRANSPARENT);
35d4c967 1040 dc.SetTextForeground(GetForegroundColour());
ca65c044 1041
cf1dfa6b 1042 int x = HEADER_OFFSET_X;
63852e78
RR
1043 int numColumns = m_owner->GetColumnCount();
1044 wxListItem item;
c5b7bb59 1045 for ( int i = 0; i < numColumns && x < w; i++ )
63852e78
RR
1046 {
1047 m_owner->GetColumn( i, item );
f6bcfd97 1048 int wCol = item.m_width;
f6bcfd97 1049
277ea1b4
DS
1050 int cw = wCol;
1051 int ch = h;
277ea1b4 1052
df0edf44
KO
1053 int flags = 0;
1054 if (!m_parent->IsEnabled())
1055 flags |= wxCONTROL_DISABLED;
1056
6fef2483 1057// NB: The code below is not really Mac-specific, but since we are close
df0edf44 1058// to 2.8 release and I don't have time to test on other platforms, I
4c51a665 1059// defined this only for wxMac. If this behaviour is desired on
df0edf44
KO
1060// other platforms, please go ahead and revise or remove the #ifdef.
1061#ifdef __WXMAC__
6fef2483 1062 if ( !m_owner->IsVirtual() && (item.m_mask & wxLIST_MASK_STATE) &&
df0edf44
KO
1063 (item.m_state & wxLIST_STATE_SELECTED) )
1064 flags |= wxCONTROL_SELECTED;
1065#endif
1066
06cd40a8
RR
1067 if (i == 0)
1068 flags |= wxCONTROL_SPECIAL; // mark as first column
1069
9c7f49f5
VZ
1070 wxRendererNative::Get().DrawHeaderButton
1071 (
1072 this,
1073 dc,
d108f236 1074 wxRect(x, HEADER_OFFSET_Y, cw, ch),
df0edf44 1075 flags
9c7f49f5 1076 );
0a816d95 1077
e1e1272f
VZ
1078 // see if we have enough space for the column label
1079
1080 // for this we need the width of the text
1081 wxCoord wLabel;
ca65c044 1082 wxCoord hLabel;
cabeffb0 1083 dc.GetTextExtent(item.GetText(), &wLabel, &hLabel);
277ea1b4 1084 wLabel += 2 * EXTRA_WIDTH;
e1e1272f
VZ
1085
1086 // and the width of the icon, if any
277ea1b4 1087 int ix = 0, iy = 0; // init them just to suppress the compiler warnings
e1e1272f 1088 const int image = item.m_image;
8a3e173a 1089 wxImageList *imageList;
0a816d95
VZ
1090 if ( image != -1 )
1091 {
ad486020 1092 imageList = m_owner->GetSmallImageList();
0a816d95
VZ
1093 if ( imageList )
1094 {
0a816d95 1095 imageList->GetSize(image, ix, iy);
936632d3 1096 wLabel += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
e1e1272f
VZ
1097 }
1098 }
1099 else
1100 {
1101 imageList = NULL;
1102 }
1103
1104 // ignore alignment if there is not enough space anyhow
1105 int xAligned;
1106 switch ( wLabel < cw ? item.GetAlign() : wxLIST_FORMAT_LEFT )
1107 {
1108 default:
9a83f860 1109 wxFAIL_MSG( wxT("unknown list item format") );
e1e1272f 1110 // fall through
0a816d95 1111
e1e1272f
VZ
1112 case wxLIST_FORMAT_LEFT:
1113 xAligned = x;
1114 break;
0a816d95 1115
e1e1272f
VZ
1116 case wxLIST_FORMAT_RIGHT:
1117 xAligned = x + cw - wLabel;
1118 break;
1119
1120 case wxLIST_FORMAT_CENTER:
1121 xAligned = x + (cw - wLabel) / 2;
1122 break;
1123 }
1124
936632d3
VZ
1125 // draw the text and image clipping them so that they
1126 // don't overwrite the column boundary
65782361 1127 wxDCClipper clipper(dc, x, HEADER_OFFSET_Y, cw, h);
936632d3 1128
e1e1272f
VZ
1129 // if we have an image, draw it on the right of the label
1130 if ( imageList )
1131 {
1132 imageList->Draw
1133 (
1134 image,
1135 dc,
936632d3 1136 xAligned + wLabel - ix - HEADER_IMAGE_MARGIN_IN_REPORT_MODE,
65782361 1137 HEADER_OFFSET_Y + (h - iy)/2,
e1e1272f
VZ
1138 wxIMAGELIST_DRAW_TRANSPARENT
1139 );
0a816d95
VZ
1140 }
1141
06b781c7 1142 dc.DrawText( item.GetText(),
65782361 1143 xAligned + EXTRA_WIDTH, (h - hLabel) / 2 );
06b781c7 1144
0a816d95 1145 x += wCol;
63852e78 1146 }
4a5ed5bf
VZ
1147
1148 // Fill in what's missing to the right of the columns, otherwise we will
1149 // leave an unpainted area when columns are removed (and it looks better)
1150 if ( x < w )
1151 {
1152 wxRendererNative::Get().DrawHeaderButton
1153 (
1154 this,
1155 dc,
1156 wxRect(x, HEADER_OFFSET_Y, w - x, h),
06cd40a8 1157 wxCONTROL_DIRTY // mark as last column
4a5ed5bf
VZ
1158 );
1159 }
e1e955e1 1160}
c801d85f 1161
06cd40a8
RR
1162void wxListHeaderWindow::OnInternalIdle()
1163{
1164 wxWindow::OnInternalIdle();
7d7b3f69 1165
06cd40a8
RR
1166 if (m_sendSetColumnWidth)
1167 {
1168 m_owner->SetColumnWidth( m_colToSend, m_widthToSend );
1169 m_sendSetColumnWidth = false;
1170 }
1171}
1172
0208334d
RR
1173void wxListHeaderWindow::DrawCurrent()
1174{
b46ea782 1175#if 1
06cd40a8
RR
1176 // m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1177 m_sendSetColumnWidth = true;
1178 m_colToSend = m_column;
1179 m_widthToSend = m_currentX - m_minX;
b46ea782 1180#else
63852e78
RR
1181 int x1 = m_currentX;
1182 int y1 = 0;
f16ba4e6 1183 m_owner->ClientToScreen( &x1, &y1 );
f6bcfd97 1184
f16ba4e6 1185 int x2 = m_currentX;
63852e78 1186 int y2 = 0;
f6bcfd97 1187 m_owner->GetClientSize( NULL, &y2 );
63852e78 1188 m_owner->ClientToScreen( &x2, &y2 );
0208334d 1189
63852e78 1190 wxScreenDC dc;
3c679789 1191 dc.SetLogicalFunction( wxINVERT );
fd1dc2a7 1192 dc.SetPen( wxPen(*wxBLACK, 2) );
63852e78 1193 dc.SetBrush( *wxTRANSPARENT_BRUSH );
0208334d 1194
f6bcfd97
BP
1195 AdjustDC(dc);
1196
63852e78 1197 dc.DrawLine( x1, y1, x2, y2 );
0208334d 1198
63852e78 1199 dc.SetLogicalFunction( wxCOPY );
0208334d 1200
63852e78
RR
1201 dc.SetPen( wxNullPen );
1202 dc.SetBrush( wxNullBrush );
b46ea782 1203#endif
0208334d
RR
1204}
1205
c801d85f
KB
1206void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
1207{
06cd40a8 1208 wxGenericListCtrl *parent = m_owner->GetListCtrl();
7d7b3f69 1209
f6bcfd97 1210 // we want to work with logical coords
3ca6a5f0 1211 int x;
06cd40a8 1212 parent->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
3ca6a5f0 1213 int y = event.GetY();
f6bcfd97 1214
cfb50f14 1215 if (m_isDragging)
0208334d 1216 {
355f2407 1217 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING, event.GetPosition());
a77ec46d 1218
f6bcfd97
BP
1219 // we don't draw the line beyond our window, but we allow dragging it
1220 // there
1221 int w = 0;
1222 GetClientSize( &w, NULL );
06cd40a8 1223 parent->CalcUnscrolledPosition(w, 0, &w, NULL);
f6bcfd97
BP
1224 w -= 6;
1225
1226 // erase the line if it was drawn
1227 if ( m_currentX < w )
1228 DrawCurrent();
1229
63852e78
RR
1230 if (event.ButtonUp())
1231 {
1232 ReleaseMouse();
ca65c044
WS
1233 m_isDragging = false;
1234 m_dirty = true;
f6bcfd97 1235 m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
355f2407 1236 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG, event.GetPosition());
63852e78
RR
1237 }
1238 else
1239 {
f6bcfd97 1240 if (x > m_minX + 7)
63852e78
RR
1241 m_currentX = x;
1242 else
f6bcfd97 1243 m_currentX = m_minX + 7;
bd8289c1 1244
f6bcfd97
BP
1245 // draw in the new location
1246 if ( m_currentX < w )
1247 DrawCurrent();
bffa1c77 1248 }
0208334d 1249 }
f6bcfd97 1250 else // not dragging
c801d85f 1251 {
f6bcfd97 1252 m_minX = 0;
ca65c044 1253 bool hit_border = false;
f6bcfd97
BP
1254
1255 // end of the current column
1256 int xpos = 0;
1257
3103e8a9 1258 // find the column where this event occurred
62313c27
VZ
1259 int col,
1260 countCol = m_owner->GetColumnCount();
1261 for (col = 0; col < countCol; col++)
bffa1c77 1262 {
cf1dfa6b
VZ
1263 xpos += m_owner->GetColumnWidth( col );
1264 m_column = col;
f6bcfd97
BP
1265
1266 if ( (abs(x-xpos) < 3) && (y < 22) )
1267 {
1268 // near the column border
ca65c044 1269 hit_border = true;
f6bcfd97
BP
1270 break;
1271 }
1272
1273 if ( x < xpos )
1274 {
1275 // inside the column
1276 break;
1277 }
1278
1279 m_minX = xpos;
bffa1c77 1280 }
63852e78 1281
62313c27
VZ
1282 if ( col == countCol )
1283 m_column = -1;
1284
11358d39 1285 if (event.LeftDown() || event.RightUp())
63852e78 1286 {
11358d39 1287 if (hit_border && event.LeftDown())
f6bcfd97 1288 {
355f2407
VZ
1289 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG,
1290 event.GetPosition()) )
1291 {
ca65c044 1292 m_isDragging = true;
355f2407 1293 m_currentX = x;
355f2407 1294 CaptureMouse();
03eccb83 1295 DrawCurrent();
355f2407
VZ
1296 }
1297 //else: column resizing was vetoed by the user code
f6bcfd97 1298 }
11358d39 1299 else // click on a column
f6bcfd97 1300 {
df0edf44
KO
1301 // record the selected state of the columns
1302 if (event.LeftDown())
1303 {
1304 for (int i=0; i < m_owner->GetColumnCount(); i++)
1305 {
1306 wxListItem colItem;
1307 m_owner->GetColumn(i, colItem);
6fef2483 1308 long state = colItem.GetState();
df0edf44
KO
1309 if (i == m_column)
1310 colItem.SetState(state | wxLIST_STATE_SELECTED);
1311 else
1312 colItem.SetState(state & ~wxLIST_STATE_SELECTED);
1313 m_owner->SetColumn(i, colItem);
1314 }
1315 }
6fef2483 1316
a77ec46d 1317 SendListEvent( event.LeftDown()
11358d39
VZ
1318 ? wxEVT_COMMAND_LIST_COL_CLICK
1319 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK,
a77ec46d 1320 event.GetPosition());
f6bcfd97 1321 }
63852e78 1322 }
f6bcfd97 1323 else if (event.Moving())
63852e78 1324 {
f6bcfd97
BP
1325 bool setCursor;
1326 if (hit_border)
1327 {
1328 setCursor = m_currentCursor == wxSTANDARD_CURSOR;
1329 m_currentCursor = m_resizeCursor;
1330 }
1331 else
1332 {
1333 setCursor = m_currentCursor != wxSTANDARD_CURSOR;
1334 m_currentCursor = wxSTANDARD_CURSOR;
1335 }
1336
1337 if ( setCursor )
1338 SetCursor(*m_currentCursor);
63852e78 1339 }
e1e955e1 1340 }
e1e955e1 1341}
c801d85f 1342
fbfb8bcc 1343bool wxListHeaderWindow::SendListEvent(wxEventType type, const wxPoint& pos)
a77ec46d
RD
1344{
1345 wxWindow *parent = GetParent();
1346 wxListEvent le( type, parent->GetId() );
1347 le.SetEventObject( parent );
1348 le.m_pointDrag = pos;
1349
1350 // the position should be relative to the parent window, not
1351 // this one for compatibility with MSW and common sense: the
1352 // user code doesn't know anything at all about this header
1353 // window, so why should it get positions relative to it?
1354 le.m_pointDrag.y -= GetSize().y;
1355
1356 le.m_col = m_column;
355f2407 1357 return !parent->GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
a77ec46d
RD
1358}
1359
c801d85f
KB
1360//-----------------------------------------------------------------------------
1361// wxListRenameTimer (internal)
1362//-----------------------------------------------------------------------------
1363
bd8289c1
VZ
1364wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
1365{
63852e78 1366 m_owner = owner;
e1e955e1 1367}
c801d85f 1368
bd8289c1
VZ
1369void wxListRenameTimer::Notify()
1370{
63852e78 1371 m_owner->OnRenameTimer();
e1e955e1 1372}
c801d85f 1373
d34d31f6
VZ
1374//-----------------------------------------------------------------------------
1375// wxListFindTimer (internal)
1376//-----------------------------------------------------------------------------
1377
1378void wxListFindTimer::Notify()
1379{
1380 m_owner->OnFindTimer();
1381}
1382
ee7ee469 1383//-----------------------------------------------------------------------------
26e8da41 1384// wxListTextCtrlWrapper (internal)
ee7ee469
RR
1385//-----------------------------------------------------------------------------
1386
26e8da41
VZ
1387BEGIN_EVENT_TABLE(wxListTextCtrlWrapper, wxEvtHandler)
1388 EVT_CHAR (wxListTextCtrlWrapper::OnChar)
1389 EVT_KEY_UP (wxListTextCtrlWrapper::OnKeyUp)
1390 EVT_KILL_FOCUS (wxListTextCtrlWrapper::OnKillFocus)
ee7ee469
RR
1391END_EVENT_TABLE()
1392
26e8da41
VZ
1393wxListTextCtrlWrapper::wxListTextCtrlWrapper(wxListMainWindow *owner,
1394 wxTextCtrl *text,
1395 size_t itemEdit)
62d89eb4
VZ
1396 : m_startValue(owner->GetItemText(itemEdit)),
1397 m_itemEdited(itemEdit)
1398{
63852e78 1399 m_owner = owner;
26e8da41 1400 m_text = text;
33fe475a 1401 m_aboutToFinish = false;
62d89eb4 1402
06cd40a8
RR
1403 wxGenericListCtrl *parent = m_owner->GetListCtrl();
1404
62d89eb4
VZ
1405 wxRect rectLabel = owner->GetLineLabelRect(itemEdit);
1406
06cd40a8 1407 parent->CalcScrolledPosition(rectLabel.x, rectLabel.y,
62d89eb4
VZ
1408 &rectLabel.x, &rectLabel.y);
1409
26e8da41
VZ
1410 m_text->Create(owner, wxID_ANY, m_startValue,
1411 wxPoint(rectLabel.x-4,rectLabel.y-4),
1412 wxSize(rectLabel.width+11,rectLabel.height+8));
3603a389 1413 m_text->SetFocus();
cf76cd4e 1414
26e8da41 1415 m_text->PushEventHandler(this);
ee7ee469
RR
1416}
1417
93f6e00d 1418void wxListTextCtrlWrapper::EndEdit(EndReason reason)
ee7ee469 1419{
a8a89154 1420 m_aboutToFinish = true;
3e6858cd 1421
93f6e00d 1422 switch ( reason )
63852e78 1423 {
93f6e00d
VZ
1424 case End_Accept:
1425 // Notify the owner about the changes
1426 AcceptChanges();
3e6858cd 1427
93f6e00d
VZ
1428 // Even if vetoed, close the control (consistent with MSW)
1429 Finish( true );
1430 break;
1431
1432 case End_Discard:
1433 m_owner->OnRenameCancelled(m_itemEdited);
26e8da41 1434
93f6e00d
VZ
1435 Finish( true );
1436 break;
1437
1438 case End_Destroy:
1439 // Don't generate any notifications for the control being destroyed
1440 // and don't set focus to it neither.
1441 Finish(false);
1442 break;
62d89eb4
VZ
1443 }
1444}
dd5a32cc 1445
a8a89154
RR
1446void wxListTextCtrlWrapper::Finish( bool setfocus )
1447{
1448 m_text->RemoveEventHandler(this);
1449 m_owner->ResetTextControl( m_text );
1450
1451 wxPendingDelete.Append( this );
3e6858cd 1452
a8a89154 1453 if (setfocus)
16361ec9 1454 m_owner->SetFocus();
a8a89154
RR
1455}
1456
26e8da41 1457bool wxListTextCtrlWrapper::AcceptChanges()
62d89eb4 1458{
26e8da41 1459 const wxString value = m_text->GetValue();
62d89eb4 1460
0a904ed2
VZ
1461 // notice that we should always call OnRenameAccept() to generate the "end
1462 // label editing" event, even if the user hasn't really changed anything
62d89eb4 1463 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
0a904ed2 1464 {
62d89eb4 1465 // vetoed by the user
ca65c044 1466 return false;
0a904ed2 1467 }
f6bcfd97 1468
0a904ed2
VZ
1469 // accepted, do rename the item (unless nothing changed)
1470 if ( value != m_startValue )
1471 m_owner->SetItemText(m_itemEdited, value);
f6bcfd97 1472
ca65c044 1473 return true;
62d89eb4 1474}
dd5a32cc 1475
26e8da41 1476void wxListTextCtrlWrapper::OnChar( wxKeyEvent &event )
64ac3db8
VZ
1477{
1478 if ( !CheckForEndEditKey(event) )
1479 event.Skip();
1480}
1481
1482bool wxListTextCtrlWrapper::CheckForEndEditKey(const wxKeyEvent& event)
62d89eb4
VZ
1483{
1484 switch ( event.m_keyCode )
1485 {
1486 case WXK_RETURN:
93f6e00d 1487 EndEdit( End_Accept );
768276f6 1488 break;
f6bcfd97 1489
62d89eb4 1490 case WXK_ESCAPE:
93f6e00d 1491 EndEdit( End_Discard );
62d89eb4
VZ
1492 break;
1493
1494 default:
64ac3db8 1495 return false;
62d89eb4 1496 }
64ac3db8
VZ
1497
1498 return true;
63852e78
RR
1499}
1500
26e8da41 1501void wxListTextCtrlWrapper::OnKeyUp( wxKeyEvent &event )
c13cace1 1502{
a8a89154
RR
1503 if (m_aboutToFinish)
1504 {
1505 // auto-grow the textctrl:
1506 wxSize parentSize = m_owner->GetSize();
1507 wxPoint myPos = m_text->GetPosition();
1508 wxSize mySize = m_text->GetSize();
1509 int sx, sy;
9a83f860 1510 m_text->GetTextExtent(m_text->GetValue() + wxT("MM"), &sx, &sy);
a8a89154
RR
1511 if (myPos.x + sx > parentSize.x)
1512 sx = parentSize.x - myPos.x;
1513 if (mySize.x > sx)
1514 sx = mySize.x;
1515 m_text->SetSize(sx, wxDefaultCoord);
1516 }
3e6858cd 1517
c13cace1
VS
1518 event.Skip();
1519}
1520
26e8da41 1521void wxListTextCtrlWrapper::OnKillFocus( wxFocusEvent &event )
63852e78 1522{
a8a89154 1523 if ( !m_aboutToFinish )
dd5a32cc 1524 {
86586459
RR
1525 if ( !AcceptChanges() )
1526 m_owner->OnRenameCancelled( m_itemEdited );
cf76cd4e 1527
a8a89154 1528 Finish( false );
62d89eb4 1529 }
86351e4a 1530
3603a389 1531 // We must let the native text control handle focus
62d89eb4 1532 event.Skip();
ee7ee469
RR
1533}
1534
c801d85f
KB
1535//-----------------------------------------------------------------------------
1536// wxListMainWindow
1537//-----------------------------------------------------------------------------
1538
fd1dc2a7 1539BEGIN_EVENT_TABLE(wxListMainWindow, wxWindow)
c801d85f 1540 EVT_PAINT (wxListMainWindow::OnPaint)
c801d85f 1541 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse)
64ac3db8 1542 EVT_CHAR_HOOK (wxListMainWindow::OnCharHook)
c801d85f 1543 EVT_CHAR (wxListMainWindow::OnChar)
3dfb93fd 1544 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown)
6fef2483 1545 EVT_KEY_UP (wxListMainWindow::OnKeyUp)
c801d85f
KB
1546 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus)
1547 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus)
2fa7c206 1548 EVT_SCROLLWIN (wxListMainWindow::OnScroll)
c2cbae6b 1549 EVT_CHILD_FOCUS (wxListMainWindow::OnChildFocus)
c801d85f
KB
1550END_EVENT_TABLE()
1551
1370703e 1552void wxListMainWindow::Init()
c801d85f 1553{
ca65c044 1554 m_dirty = true;
cf1dfa6b
VZ
1555 m_countVirt = 0;
1556 m_lineFrom =
b54e41c5 1557 m_lineTo = (size_t)-1;
cf1dfa6b
VZ
1558 m_linesPerPage = 0;
1559
1560 m_headerWidth =
1561 m_lineHeight = 0;
1370703e 1562
d3b9f782
VZ
1563 m_small_image_list = NULL;
1564 m_normal_image_list = NULL;
1370703e 1565
139adb6a
RR
1566 m_small_spacing = 30;
1567 m_normal_spacing = 40;
1370703e 1568
ca65c044 1569 m_hasFocus = false;
1370703e 1570 m_dragCount = 0;
ca65c044 1571 m_isCreated = false;
1370703e 1572
ca65c044 1573 m_lastOnSame = false;
139adb6a 1574 m_renameTimer = new wxListRenameTimer( this );
d34d31f6
VZ
1575 m_findTimer = NULL;
1576 m_findBell = 0; // default is to not ring bell at all
26e8da41 1577 m_textctrlWrapper = NULL;
277ea1b4 1578
cf1dfa6b 1579 m_current =
efbb7287 1580 m_lineLastClicked =
8df44392 1581 m_lineSelectSingleOnUp =
1370703e 1582 m_lineBeforeLastClicked = (size_t)-1;
e1e955e1 1583}
c801d85f 1584
1370703e 1585wxListMainWindow::wxListMainWindow()
c801d85f 1586{
1370703e
VZ
1587 Init();
1588
49ecb029 1589 m_highlightBrush =
d3b9f782 1590 m_highlightUnfocusedBrush = NULL;
1370703e
VZ
1591}
1592
1593wxListMainWindow::wxListMainWindow( wxWindow *parent,
1594 wxWindowID id,
1595 const wxPoint& pos,
1c0a0736
VZ
1596 const wxSize& size )
1597 : wxWindow( parent, id, pos, size,
1598 wxWANTS_CHARS | wxBORDER_NONE )
1370703e
VZ
1599{
1600 Init();
1601
49ecb029 1602 m_highlightBrush = new wxBrush
4b8fa634 1603 (
a756f210 1604 wxSystemSettings::GetColour
49ecb029
VZ
1605 (
1606 wxSYS_COLOUR_HIGHLIGHT
1607 ),
04ee05f9 1608 wxBRUSHSTYLE_SOLID
4b8fa634 1609 );
6fef2483 1610
49ecb029 1611 m_highlightUnfocusedBrush = new wxBrush
4b8fa634
KO
1612 (
1613 wxSystemSettings::GetColour
1614 (
1615 wxSYS_COLOUR_BTNSHADOW
1616 ),
04ee05f9 1617 wxBRUSHSTYLE_SOLID
4b8fa634 1618 );
49ecb029 1619
ca65c044 1620 wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes();
fa47d7a7
VS
1621 SetOwnForegroundColour( attr.colFg );
1622 SetOwnBackgroundColour( attr.colBg );
92a4e4de
VZ
1623 if (!m_hasFont)
1624 SetOwnFont( attr.font );
e1e955e1 1625}
c801d85f 1626
fd9811b1 1627wxListMainWindow::~wxListMainWindow()
c801d85f 1628{
93f6e00d
VZ
1629 if ( m_textctrlWrapper )
1630 m_textctrlWrapper->EndEdit(wxListTextCtrlWrapper::End_Destroy);
1631
5fe143df 1632 DoDeleteAllItems();
222ed1d6 1633 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
8d36b216 1634 WX_CLEAR_ARRAY(m_aColWidths);
12c1b46a 1635
b54e41c5 1636 delete m_highlightBrush;
49ecb029 1637 delete m_highlightUnfocusedBrush;
139adb6a 1638 delete m_renameTimer;
d34d31f6 1639 delete m_findTimer;
e1e955e1 1640}
c801d85f 1641
476de5ea
VZ
1642void wxListMainWindow::SetReportView(bool inReportView)
1643{
1644 const size_t count = m_lines.size();
1645 for ( size_t n = 0; n < count; n++ )
1646 {
1647 m_lines[n].SetReportView(inReportView);
1648 }
1649}
1650
cf1dfa6b 1651void wxListMainWindow::CacheLineData(size_t line)
c801d85f 1652{
3cd94a0d 1653 wxGenericListCtrl *listctrl = GetListCtrl();
25e3a937 1654
b54e41c5 1655 wxListLineData *ld = GetDummyLine();
f6bcfd97 1656
cf1dfa6b
VZ
1657 size_t countCol = GetColumnCount();
1658 for ( size_t col = 0; col < countCol; col++ )
1659 {
1660 ld->SetText(col, listctrl->OnGetItemText(line, col));
e062c6a4 1661 ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col));
cf1dfa6b
VZ
1662 }
1663
6c02c329 1664 ld->SetAttr(listctrl->OnGetItemAttr(line));
e1e955e1 1665}
c801d85f 1666
b54e41c5 1667wxListLineData *wxListMainWindow::GetDummyLine() const
c801d85f 1668{
9a83f860
VZ
1669 wxASSERT_MSG( !IsEmpty(), wxT("invalid line index") );
1670 wxASSERT_MSG( IsVirtual(), wxT("GetDummyLine() shouldn't be called") );
904ccf52
VZ
1671
1672 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
1673
1674 // we need to recreate the dummy line if the number of columns in the
1675 // control changed as it would have the incorrect number of fields
1676 // otherwise
1677 if ( !m_lines.IsEmpty() &&
1678 m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() )
2c1f73ee 1679 {
904ccf52
VZ
1680 self->m_lines.Clear();
1681 }
cf1dfa6b 1682
904ccf52
VZ
1683 if ( m_lines.IsEmpty() )
1684 {
5cd89174 1685 wxListLineData *line = new wxListLineData(self);
cf1dfa6b 1686 self->m_lines.Add(line);
904ccf52
VZ
1687
1688 // don't waste extra memory -- there never going to be anything
1689 // else/more in this array
1690 self->m_lines.Shrink();
2c1f73ee
VZ
1691 }
1692
cf1dfa6b
VZ
1693 return &m_lines[0];
1694}
1695
5cd89174
VZ
1696// ----------------------------------------------------------------------------
1697// line geometry (report mode only)
1698// ----------------------------------------------------------------------------
1699
cf1dfa6b
VZ
1700wxCoord wxListMainWindow::GetLineHeight() const
1701{
cf1dfa6b
VZ
1702 // we cache the line height as calling GetTextExtent() is slow
1703 if ( !m_lineHeight )
2c1f73ee 1704 {
cf1dfa6b 1705 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
bd8289c1 1706
cf1dfa6b
VZ
1707 wxClientDC dc( self );
1708 dc.SetFont( GetFont() );
c801d85f 1709
cf1dfa6b 1710 wxCoord y;
9a83f860 1711 dc.GetTextExtent(wxT("H"), NULL, &y);
004fd0c8 1712
ca7353de
RD
1713 if ( m_small_image_list && m_small_image_list->GetImageCount() )
1714 {
277ea1b4 1715 int iw = 0, ih = 0;
ca7353de
RD
1716 m_small_image_list->GetSize(0, iw, ih);
1717 y = wxMax(y, ih);
1718 }
1719
1720 y += EXTRA_HEIGHT;
cf1dfa6b
VZ
1721 self->m_lineHeight = y + LINE_SPACING;
1722 }
bffa1c77 1723
cf1dfa6b
VZ
1724 return m_lineHeight;
1725}
bffa1c77 1726
cf1dfa6b
VZ
1727wxCoord wxListMainWindow::GetLineY(size_t line) const
1728{
9a83f860 1729 wxASSERT_MSG( InReportView(), wxT("only works in report mode") );
1370703e 1730
277ea1b4 1731 return LINE_SPACING + line * GetLineHeight();
cf1dfa6b 1732}
206b0a67 1733
5cd89174
VZ
1734wxRect wxListMainWindow::GetLineRect(size_t line) const
1735{
1736 if ( !InReportView() )
1737 return GetLine(line)->m_gi->m_rectAll;
1738
1739 wxRect rect;
1740 rect.x = HEADER_OFFSET_X;
1741 rect.y = GetLineY(line);
1742 rect.width = GetHeaderWidth();
1743 rect.height = GetLineHeight();
1744
1745 return rect;
1746}
1747
1748wxRect wxListMainWindow::GetLineLabelRect(size_t line) const
1749{
1750 if ( !InReportView() )
1751 return GetLine(line)->m_gi->m_rectLabel;
1752
a70598d5
RR
1753 int image_x = 0;
1754 wxListLineData *data = GetLine(line);
1755 wxListItemDataList::compatibility_iterator node = data->m_items.GetFirst();
1756 if (node)
1757 {
1758 wxListItemData *item = node->GetData();
1759 if ( item->HasImage() )
1760 {
1761 int ix, iy;
1762 GetImageSize( item->GetImage(), ix, iy );
1763 image_x = 3 + ix + IMAGE_MARGIN_IN_REPORT_MODE;
1764 }
1765 }
a9aead31 1766
5cd89174 1767 wxRect rect;
edddffb5 1768 rect.x = image_x + HEADER_OFFSET_X;
5cd89174 1769 rect.y = GetLineY(line);
edddffb5 1770 rect.width = GetColumnWidth(0) - image_x;
5cd89174
VZ
1771 rect.height = GetLineHeight();
1772
1773 return rect;
1774}
1775
1776wxRect wxListMainWindow::GetLineIconRect(size_t line) const
1777{
1778 if ( !InReportView() )
1779 return GetLine(line)->m_gi->m_rectIcon;
1780
1781 wxListLineData *ld = GetLine(line);
9a83f860 1782 wxASSERT_MSG( ld->HasImage(), wxT("should have an image") );
5cd89174
VZ
1783
1784 wxRect rect;
1785 rect.x = HEADER_OFFSET_X;
1786 rect.y = GetLineY(line);
1787 GetImageSize(ld->GetImage(), rect.width, rect.height);
1788
1789 return rect;
1790}
1791
1792wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const
1793{
1794 return InReportView() ? GetLineRect(line)
1795 : GetLine(line)->m_gi->m_rectHighlight;
1796}
1797
1798long wxListMainWindow::HitTestLine(size_t line, int x, int y) const
1799{
9a83f860 1800 wxASSERT_MSG( line < GetItemCount(), wxT("invalid line in HitTestLine") );
fc4f1d5f 1801
5cd89174
VZ
1802 wxListLineData *ld = GetLine(line);
1803
22a35096 1804 if ( ld->HasImage() && GetLineIconRect(line).Contains(x, y) )
5cd89174
VZ
1805 return wxLIST_HITTEST_ONITEMICON;
1806
acf4d858
VS
1807 // VS: Testing for "ld->HasText() || InReportView()" instead of
1808 // "ld->HasText()" is needed to make empty lines in report view
1809 // possible
1810 if ( ld->HasText() || InReportView() )
5cd89174
VZ
1811 {
1812 wxRect rect = InReportView() ? GetLineRect(line)
1813 : GetLineLabelRect(line);
1814
22a35096 1815 if ( rect.Contains(x, y) )
5cd89174
VZ
1816 return wxLIST_HITTEST_ONITEMLABEL;
1817 }
1818
1819 return 0;
1820}
1821
1822// ----------------------------------------------------------------------------
1823// highlight (selection) handling
1824// ----------------------------------------------------------------------------
1825
b54e41c5 1826bool wxListMainWindow::IsHighlighted(size_t line) const
cf1dfa6b
VZ
1827{
1828 if ( IsVirtual() )
1829 {
b54e41c5 1830 return m_selStore.IsSelected(line);
cf1dfa6b
VZ
1831 }
1832 else // !virtual
1833 {
1834 wxListLineData *ld = GetLine(line);
9a83f860 1835 wxCHECK_MSG( ld, false, wxT("invalid index in IsHighlighted") );
cf1dfa6b 1836
b54e41c5 1837 return ld->IsHighlighted();
cf1dfa6b
VZ
1838 }
1839}
1840
68a9ef0e
VZ
1841void wxListMainWindow::HighlightLines( size_t lineFrom,
1842 size_t lineTo,
1843 bool highlight )
cf1dfa6b 1844{
cf1dfa6b
VZ
1845 if ( IsVirtual() )
1846 {
68a9ef0e
VZ
1847 wxArrayInt linesChanged;
1848 if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight,
1849 &linesChanged) )
1850 {
1851 // meny items changed state, refresh everything
1852 RefreshLines(lineFrom, lineTo);
1853 }
1854 else // only a few items changed state, refresh only them
1855 {
1856 size_t count = linesChanged.GetCount();
1857 for ( size_t n = 0; n < count; n++ )
1858 {
1859 RefreshLine(linesChanged[n]);
1860 }
1861 }
b54e41c5 1862 }
68a9ef0e 1863 else // iterate over all items in non report view
b54e41c5 1864 {
b54e41c5 1865 for ( size_t line = lineFrom; line <= lineTo; line++ )
cf1dfa6b 1866 {
b54e41c5 1867 if ( HighlightLine(line, highlight) )
68a9ef0e 1868 RefreshLine(line);
cf1dfa6b 1869 }
b54e41c5
VZ
1870 }
1871}
1872
1873bool wxListMainWindow::HighlightLine( size_t line, bool highlight )
1874{
1875 bool changed;
1876
1877 if ( IsVirtual() )
1878 {
1879 changed = m_selStore.SelectItem(line, highlight);
cf1dfa6b
VZ
1880 }
1881 else // !virtual
1882 {
1883 wxListLineData *ld = GetLine(line);
9a83f860 1884 wxCHECK_MSG( ld, false, wxT("invalid index in HighlightLine") );
cf1dfa6b 1885
b54e41c5 1886 changed = ld->Highlight(highlight);
cf1dfa6b
VZ
1887 }
1888
1889 if ( changed )
1890 {
b54e41c5 1891 SendNotify( line, highlight ? wxEVT_COMMAND_LIST_ITEM_SELECTED
5cd89174 1892 : wxEVT_COMMAND_LIST_ITEM_DESELECTED );
cf1dfa6b
VZ
1893 }
1894
1895 return changed;
1896}
1897
1898void wxListMainWindow::RefreshLine( size_t line )
1899{
b5d43d1d 1900 if ( InReportView() )
6ea1323a
VZ
1901 {
1902 size_t visibleFrom, visibleTo;
1903 GetVisibleLinesRange(&visibleFrom, &visibleTo);
c1c4c551 1904
6ea1323a
VZ
1905 if ( line < visibleFrom || line > visibleTo )
1906 return;
1907 }
c1c4c551 1908
5cd89174 1909 wxRect rect = GetLineRect(line);
cf1dfa6b 1910
06cd40a8 1911 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
cf1dfa6b
VZ
1912 RefreshRect( rect );
1913}
1914
1915void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo )
1916{
1917 // we suppose that they are ordered by caller
9a83f860 1918 wxASSERT_MSG( lineFrom <= lineTo, wxT("indices in disorder") );
cf1dfa6b 1919
9a83f860 1920 wxASSERT_MSG( lineTo < GetItemCount(), wxT("invalid line range") );
6b4a8d93 1921
b5d43d1d 1922 if ( InReportView() )
cf1dfa6b 1923 {
b54e41c5
VZ
1924 size_t visibleFrom, visibleTo;
1925 GetVisibleLinesRange(&visibleFrom, &visibleTo);
1926
1927 if ( lineFrom < visibleFrom )
1928 lineFrom = visibleFrom;
1929 if ( lineTo > visibleTo )
1930 lineTo = visibleTo;
cf1dfa6b
VZ
1931
1932 wxRect rect;
1933 rect.x = 0;
1934 rect.y = GetLineY(lineFrom);
1935 rect.width = GetClientSize().x;
91c6cc0e 1936 rect.height = GetLineY(lineTo) - rect.y + GetLineHeight();
cf1dfa6b 1937
06cd40a8 1938 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
cf1dfa6b
VZ
1939 RefreshRect( rect );
1940 }
1941 else // !report
1942 {
1943 // TODO: this should be optimized...
1944 for ( size_t line = lineFrom; line <= lineTo; line++ )
1945 {
1946 RefreshLine(line);
1947 }
1948 }
1949}
1950
6b4a8d93
VZ
1951void wxListMainWindow::RefreshAfter( size_t lineFrom )
1952{
b5d43d1d 1953 if ( InReportView() )
6b4a8d93 1954 {
c29582d0
JS
1955 size_t visibleFrom, visibleTo;
1956 GetVisibleLinesRange(&visibleFrom, &visibleTo);
6b4a8d93
VZ
1957
1958 if ( lineFrom < visibleFrom )
1959 lineFrom = visibleFrom;
c29582d0
JS
1960 else if ( lineFrom > visibleTo )
1961 return;
6b4a8d93
VZ
1962
1963 wxRect rect;
1964 rect.x = 0;
1965 rect.y = GetLineY(lineFrom);
06cd40a8 1966 GetListCtrl()->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
6b4a8d93
VZ
1967
1968 wxSize size = GetClientSize();
1969 rect.width = size.x;
277ea1b4 1970
6b4a8d93
VZ
1971 // refresh till the bottom of the window
1972 rect.height = size.y - rect.y;
1973
6b4a8d93 1974 RefreshRect( rect );
6b4a8d93
VZ
1975 }
1976 else // !report
1977 {
1978 // TODO: how to do it more efficiently?
ca65c044 1979 m_dirty = true;
6b4a8d93
VZ
1980 }
1981}
1982
49ecb029
VZ
1983void wxListMainWindow::RefreshSelected()
1984{
1985 if ( IsEmpty() )
1986 return;
1987
1988 size_t from, to;
1989 if ( InReportView() )
1990 {
1991 GetVisibleLinesRange(&from, &to);
1992 }
1993 else // !virtual
1994 {
1995 from = 0;
1996 to = GetItemCount() - 1;
1997 }
1998
cf30ae13 1999 if ( HasCurrent() && m_current >= from && m_current <= to )
49ecb029 2000 RefreshLine(m_current);
49ecb029
VZ
2001
2002 for ( size_t line = from; line <= to; line++ )
2003 {
2004 // NB: the test works as expected even if m_current == -1
2005 if ( line != m_current && IsHighlighted(line) )
49ecb029 2006 RefreshLine(line);
49ecb029
VZ
2007 }
2008}
2009
cf1dfa6b
VZ
2010void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2011{
2012 // Note: a wxPaintDC must be constructed even if no drawing is
2013 // done (a Windows requirement).
2014 wxPaintDC dc( this );
2015
03138b27 2016 if ( IsEmpty() )
17808a75 2017 {
c5c528fc 2018 // nothing to draw or not the moment to draw it
cf1dfa6b 2019 return;
17808a75 2020 }
cf1dfa6b 2021
1e4d446b 2022 if ( m_dirty )
06cd40a8 2023 RecalculatePositions( false );
1e4d446b 2024
06cd40a8 2025 GetListCtrl()->PrepareDC( dc );
cf1dfa6b
VZ
2026
2027 int dev_x, dev_y;
06cd40a8 2028 GetListCtrl()->CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
cf1dfa6b 2029
cf1dfa6b
VZ
2030 dc.SetFont( GetFont() );
2031
b5d43d1d 2032 if ( InReportView() )
cf1dfa6b 2033 {
b54e41c5 2034 int lineHeight = GetLineHeight();
cf1dfa6b 2035
b54e41c5
VZ
2036 size_t visibleFrom, visibleTo;
2037 GetVisibleLinesRange(&visibleFrom, &visibleTo);
b84839ae
VZ
2038
2039 wxRect rectLine;
5eefe029
RR
2040 int xOrig = dc.LogicalToDeviceX( 0 );
2041 int yOrig = dc.LogicalToDeviceY( 0 );
6fef2483 2042
ff3d11a0 2043 // tell the caller cache to cache the data
91c6cc0e
VZ
2044 if ( IsVirtual() )
2045 {
2046 wxListEvent evCache(wxEVT_COMMAND_LIST_CACHE_HINT,
2047 GetParent()->GetId());
2048 evCache.SetEventObject( GetParent() );
2049 evCache.m_oldItemIndex = visibleFrom;
18dd5378 2050 evCache.m_item.m_itemId =
91c6cc0e
VZ
2051 evCache.m_itemIndex = visibleTo;
2052 GetParent()->GetEventHandler()->ProcessEvent( evCache );
2053 }
ff3d11a0 2054
b54e41c5 2055 for ( size_t line = visibleFrom; line <= visibleTo; line++ )
cf1dfa6b 2056 {
b84839ae
VZ
2057 rectLine = GetLineRect(line);
2058
5eefe029
RR
2059
2060 if ( !IsExposed(rectLine.x + xOrig, rectLine.y + yOrig,
b84839ae
VZ
2061 rectLine.width, rectLine.height) )
2062 {
2063 // don't redraw unaffected lines to avoid flicker
2064 continue;
2065 }
2066
5cd89174 2067 GetLine(line)->DrawInReportMode( &dc,
b84839ae 2068 rectLine,
5cd89174 2069 GetLineHighlightRect(line),
ad486020 2070 IsHighlighted(line),
32fc355f 2071 line == m_current );
cf1dfa6b
VZ
2072 }
2073
2074 if ( HasFlag(wxLC_HRULES) )
2075 {
04ee05f9 2076 wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
cf1dfa6b
VZ
2077 wxSize clientSize = GetClientSize();
2078
696a18c7
RR
2079 size_t i = visibleFrom;
2080 if (i == 0) i = 1; // Don't draw the first one
2081 for ( ; i <= visibleTo; i++ )
cf1dfa6b
VZ
2082 {
2083 dc.SetPen(pen);
2084 dc.SetBrush( *wxTRANSPARENT_BRUSH );
277ea1b4
DS
2085 dc.DrawLine(0 - dev_x, i * lineHeight,
2086 clientSize.x - dev_x, i * lineHeight);
cf1dfa6b
VZ
2087 }
2088
2089 // Draw last horizontal rule
e7d073c3 2090 if ( visibleTo == GetItemCount() - 1 )
cf1dfa6b 2091 {
277ea1b4 2092 dc.SetPen( pen );
cf1dfa6b 2093 dc.SetBrush( *wxTRANSPARENT_BRUSH );
277ea1b4
DS
2094 dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight,
2095 clientSize.x - dev_x , (m_lineTo + 1) * lineHeight );
cf1dfa6b 2096 }
2c1f73ee 2097 }
206b0a67
JS
2098
2099 // Draw vertical rules if required
cf1dfa6b 2100 if ( HasFlag(wxLC_VRULES) && !IsEmpty() )
206b0a67 2101 {
04ee05f9 2102 wxPen pen(GetRuleColour(), 1, wxPENSTYLE_SOLID);
277ea1b4 2103 wxRect firstItemRect, lastItemRect;
cf1dfa6b 2104
e7d073c3
RD
2105 GetItemRect(visibleFrom, firstItemRect);
2106 GetItemRect(visibleTo, lastItemRect);
206b0a67 2107 int x = firstItemRect.GetX();
673dfcfa
JS
2108 dc.SetPen(pen);
2109 dc.SetBrush(* wxTRANSPARENT_BRUSH);
277ea1b4 2110
999836aa 2111 for (int col = 0; col < GetColumnCount(); col++)
206b0a67
JS
2112 {
2113 int colWidth = GetColumnWidth(col);
cf1dfa6b 2114 x += colWidth;
696a18c7
RR
2115 int x_pos = x - dev_x;
2116 if (col < GetColumnCount()-1) x_pos -= 2;
2117 dc.DrawLine(x_pos, firstItemRect.GetY() - 1 - dev_y,
2118 x_pos, lastItemRect.GetBottom() + 1 - dev_y);
206b0a67 2119 }
d786bf87 2120 }
139adb6a 2121 }
cf1dfa6b 2122 else // !report
139adb6a 2123 {
1370703e 2124 size_t count = GetItemCount();
cf1dfa6b
VZ
2125 for ( size_t i = 0; i < count; i++ )
2126 {
ed84dc74 2127 GetLine(i)->Draw( &dc, i == m_current );
cf1dfa6b 2128 }
139adb6a 2129 }
004fd0c8 2130
705c4d13
VZ
2131 // DrawFocusRect() is unusable under Mac, it draws outside of the highlight
2132 // rectangle somehow and so leaves traces when the item is not selected any
2133 // more, see #12229.
2134#ifndef __WXMAC__
c25f61f1 2135 if ( HasCurrent() )
cf1dfa6b 2136 {
d67fc8b7
VZ
2137 int flags = 0;
2138 if ( IsHighlighted(m_current) )
2139 flags |= wxCONTROL_SELECTED;
2140
2141 wxRendererNative::Get().
2142 DrawFocusRect(this, dc, GetLineHighlightRect(m_current), flags);
cf1dfa6b 2143 }
705c4d13 2144#endif // !__WXMAC__
e1e955e1 2145}
c801d85f 2146
b54e41c5 2147void wxListMainWindow::HighlightAll( bool on )
c801d85f 2148{
b54e41c5 2149 if ( IsSingleSel() )
c801d85f 2150 {
9a83f860 2151 wxASSERT_MSG( !on, wxT("can't do this in a single selection control") );
b54e41c5
VZ
2152
2153 // we just have one item to turn off
2154 if ( HasCurrent() && IsHighlighted(m_current) )
139adb6a 2155 {
ca65c044 2156 HighlightLine(m_current, false);
b54e41c5 2157 RefreshLine(m_current);
139adb6a 2158 }
e1e955e1 2159 }
b6423e8b 2160 else // multi selection
cf1dfa6b 2161 {
b6423e8b
VZ
2162 if ( !IsEmpty() )
2163 HighlightLines(0, GetItemCount() - 1, on);
cf1dfa6b 2164 }
e1e955e1 2165}
c801d85f 2166
78806f0c 2167void wxListMainWindow::OnChildFocus(wxChildFocusEvent& WXUNUSED(event))
c2cbae6b 2168{
78806f0c
RD
2169 // Do nothing here. This prevents the default handler in wxScrolledWindow
2170 // from needlessly scrolling the window when the edit control is
2171 // dismissed. See ticket #9563.
c2cbae6b
RD
2172}
2173
cf1dfa6b 2174void wxListMainWindow::SendNotify( size_t line,
05a7f61d 2175 wxEventType command,
fbfb8bcc 2176 const wxPoint& point )
c801d85f 2177{
139adb6a
RR
2178 wxListEvent le( command, GetParent()->GetId() );
2179 le.SetEventObject( GetParent() );
6fef2483 2180
18dd5378 2181 le.m_item.m_itemId =
cf1dfa6b 2182 le.m_itemIndex = line;
05a7f61d
VZ
2183
2184 // set only for events which have position
2185 if ( point != wxDefaultPosition )
2186 le.m_pointDrag = point;
2187
c1c4c551
VZ
2188 // don't try to get the line info for virtual list controls: the main
2189 // program has it anyhow and if we did it would result in accessing all
2190 // the lines, even those which are not visible now and this is precisely
2191 // what we're trying to avoid
a95d5751 2192 if ( !IsVirtual() )
91c6cc0e 2193 {
0ddefeb0
VZ
2194 if ( line != (size_t)-1 )
2195 {
2196 GetLine(line)->GetItem( 0, le.m_item );
2197 }
2198 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
91c6cc0e
VZ
2199 }
2200 //else: there may be no more such item
2201
6e228e42 2202 GetParent()->GetEventHandler()->ProcessEvent( le );
e1e955e1 2203}
c801d85f 2204
0ddefeb0 2205void wxListMainWindow::ChangeCurrent(size_t current)
c801d85f 2206{
0ddefeb0 2207 m_current = current;
c801d85f 2208
6fef2483
VZ
2209 // as the current item changed, we shouldn't start editing it when the
2210 // "slow click" timer expires as the click happened on another item
2211 if ( m_renameTimer->IsRunning() )
2212 m_renameTimer->Stop();
2213
0ddefeb0 2214 SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
e1e955e1 2215}
c801d85f 2216
26e8da41 2217wxTextCtrl *wxListMainWindow::EditLabel(long item, wxClassInfo* textControlClass)
c801d85f 2218{
26e8da41 2219 wxCHECK_MSG( (item >= 0) && ((size_t)item < GetItemCount()), NULL,
3cd94a0d 2220 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
004fd0c8 2221
80a46597 2222 wxASSERT_MSG( textControlClass->IsKindOf(wxCLASSINFO(wxTextCtrl)),
26e8da41
VZ
2223 wxT("EditLabel() needs a text control") );
2224
62d89eb4 2225 size_t itemEdit = (size_t)item;
e179bd65 2226
fd9811b1 2227 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
139adb6a 2228 le.SetEventObject( GetParent() );
18dd5378 2229 le.m_item.m_itemId =
1370703e 2230 le.m_itemIndex = item;
62d89eb4 2231 wxListLineData *data = GetLine(itemEdit);
9a83f860 2232 wxCHECK_MSG( data, NULL, wxT("invalid index in EditLabel()") );
1370703e 2233 data->GetItem( 0, le.m_item );
277ea1b4 2234
62d89eb4 2235 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
26e8da41 2236 {
62d89eb4 2237 // vetoed by user code
26e8da41
VZ
2238 return NULL;
2239 }
004fd0c8 2240
62d89eb4 2241 if ( m_dirty )
a502b284 2242 {
9db17727
VZ
2243 // Ensure the display is updated before we start editing.
2244 Update();
a502b284
VZ
2245 }
2246
26e8da41
VZ
2247 wxTextCtrl * const text = (wxTextCtrl *)textControlClass->CreateObject();
2248 m_textctrlWrapper = new wxListTextCtrlWrapper(this, text, item);
2249 return m_textctrlWrapper->GetText();
e1e955e1 2250}
c801d85f 2251
e179bd65
RR
2252void wxListMainWindow::OnRenameTimer()
2253{
cf1dfa6b 2254 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
004fd0c8 2255
cf1dfa6b 2256 EditLabel( m_current );
e179bd65
RR
2257}
2258
62d89eb4 2259bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
c801d85f 2260{
e179bd65
RR
2261 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2262 le.SetEventObject( GetParent() );
18dd5378 2263 le.m_item.m_itemId =
62d89eb4 2264 le.m_itemIndex = itemEdit;
1370703e 2265
62d89eb4 2266 wxListLineData *data = GetLine(itemEdit);
277ea1b4 2267
9a83f860 2268 wxCHECK_MSG( data, false, wxT("invalid index in OnRenameAccept()") );
1370703e
VZ
2269
2270 data->GetItem( 0, le.m_item );
62d89eb4
VZ
2271 le.m_item.m_text = value;
2272 return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
2273 le.IsAllowed();
e1e955e1 2274}
c801d85f 2275
18dd54e3 2276void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
86586459 2277{
86586459
RR
2278 // let owner know that the edit was cancelled
2279 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
86351e4a 2280
ca65c044 2281 le.SetEditCanceled(true);
86351e4a 2282
86586459 2283 le.SetEventObject( GetParent() );
18dd5378 2284 le.m_item.m_itemId =
86586459
RR
2285 le.m_itemIndex = itemEdit;
2286
2287 wxListLineData *data = GetLine(itemEdit);
9a83f860 2288 wxCHECK_RET( data, wxT("invalid index in OnRenameCancelled()") );
86586459
RR
2289
2290 data->GetItem( 0, le.m_item );
86586459
RR
2291 GetEventHandler()->ProcessEvent( le );
2292}
2293
d34d31f6
VZ
2294void wxListMainWindow::OnFindTimer()
2295{
2296 m_findPrefix.clear();
2297 if ( m_findBell )
2298 m_findBell = 1;
2299}
2300
2301void wxListMainWindow::EnableBellOnNoMatch( bool on )
2302{
2303 m_findBell = on;
2304}
2305
c801d85f
KB
2306void wxListMainWindow::OnMouse( wxMouseEvent &event )
2307{
b6bc47ef
RD
2308#ifdef __WXMAC__
2309 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2310 // shutdown the edit control when the mouse is clicked elsewhere on the
2311 // listctrl because the order of events is different (or something like
277ea1b4 2312 // that), so explicitly end the edit if it is active.
26e8da41 2313 if ( event.LeftDown() && m_textctrlWrapper )
93f6e00d 2314 m_textctrlWrapper->EndEdit(wxListTextCtrlWrapper::End_Accept);
26e8da41 2315#endif // __WXMAC__
277ea1b4 2316
8d4b048e 2317 if ( event.LeftDown() )
e55e1a5f
VZ
2318 {
2319 // Ensure we skip the event to let the system set focus to this window.
2320 event.Skip();
2321 }
6fef2483 2322
524a2c6e
VZ
2323 // Pretend that the event happened in wxListCtrl itself.
2324 wxMouseEvent me(event);
2325 me.SetEventObject( GetParent() );
2326 me.SetId(GetParent()->GetId());
2327 if ( GetParent()->GetEventHandler()->ProcessEvent( me ))
cf1dfa6b
VZ
2328 return;
2329
185f6036
RD
2330 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
2331 {
64894596 2332 // let the base class handle mouse wheel events.
185f6036
RD
2333 event.Skip();
2334 return;
2335 }
2ac08aeb 2336
cf1dfa6b 2337 if ( !HasCurrent() || IsEmpty() )
8b1464af
RR
2338 {
2339 if (event.RightDown())
2340 {
2341 SendNotify( (size_t)-1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
b6423e8b 2342
64894596
FM
2343 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU,
2344 GetParent()->GetId(),
2345 ClientToScreen(event.GetPosition()));
fc694caa
RR
2346 evtCtx.SetEventObject(GetParent());
2347 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
8b1464af 2348 }
cf1dfa6b 2349 return;
8b1464af 2350 }
cf1dfa6b
VZ
2351
2352 if (m_dirty)
2353 return;
e3e65dac 2354
cf1dfa6b
VZ
2355 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
2356 event.ButtonDClick()) )
2357 return;
c801d85f 2358
aaef15bf
RR
2359 int x = event.GetX();
2360 int y = event.GetY();
06cd40a8 2361 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
004fd0c8 2362
cc734310 2363 // where did we hit it (if we did)?
92976ab6 2364 long hitResult = 0;
1370703e 2365
cf1dfa6b
VZ
2366 size_t count = GetItemCount(),
2367 current;
2368
b5d43d1d 2369 if ( InReportView() )
92976ab6 2370 {
5cd89174 2371 current = y / GetLineHeight();
cc734310
VZ
2372 if ( current < count )
2373 hitResult = HitTestLine(current, x, y);
cf1dfa6b
VZ
2374 }
2375 else // !report
2376 {
2377 // TODO: optimize it too! this is less simple than for report view but
2378 // enumerating all items is still not a way to do it!!
4e3ace65 2379 for ( current = 0; current < count; current++ )
cf1dfa6b 2380 {
5cd89174 2381 hitResult = HitTestLine(current, x, y);
4e3ace65
VZ
2382 if ( hitResult )
2383 break;
cf1dfa6b 2384 }
92976ab6 2385 }
bd8289c1 2386
becc95d8
VZ
2387 // Update drag events counter first as we must do it even if the mouse is
2388 // not on any item right now as we must keep count in case we started
2389 // dragging from the empty control area but continued to do it over a valid
2390 // item -- in this situation we must not start dragging this item.
fd9811b1 2391 if (event.Dragging())
fd9811b1 2392 m_dragCount++;
fd9811b1 2393 else
fd9811b1 2394 m_dragCount = 0;
bd8289c1 2395
becc95d8
VZ
2396 // The only mouse event that can be generated without any valid item is
2397 // wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK as it can be useful to have a global
2398 // popup menu for the list control itself which should be shown even when
2399 // the user clicks outside of any item.
cf1dfa6b
VZ
2400 if ( !hitResult )
2401 {
8b1464af
RR
2402 // outside of any item
2403 if (event.RightDown())
2404 {
2405 SendNotify( (size_t) -1, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
b6423e8b 2406
fc694caa
RR
2407 wxContextMenuEvent evtCtx(
2408 wxEVT_CONTEXT_MENU,
2409 GetParent()->GetId(),
2410 ClientToScreen(event.GetPosition()));
2411 evtCtx.SetEventObject(GetParent());
2412 GetParent()->GetEventHandler()->ProcessEvent(evtCtx);
8b1464af
RR
2413 }
2414 else
2415 {
2416 // reset the selection and bail out
2417 HighlightAll(false);
2418 }
cf76cd4e 2419
cf1dfa6b
VZ
2420 return;
2421 }
bd8289c1 2422
becc95d8
VZ
2423 if ( event.Dragging() )
2424 {
2425 if (m_dragCount == 1)
2426 {
2427 // we have to report the raw, physical coords as we want to be
2428 // able to call HitTest(event.m_pointDrag) from the user code to
2429 // get the item being dragged
2430 m_dragStart = event.GetPosition();
2431 }
2432
2433 if (m_dragCount != 3)
2434 return;
2435
2436 int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2437 : wxEVT_COMMAND_LIST_BEGIN_DRAG;
2438
14841e5a 2439 SendNotify( m_lineLastClicked, command, m_dragStart );
becc95d8
VZ
2440
2441 return;
2442 }
2443
ca65c044 2444 bool forceClick = false;
92976ab6
RR
2445 if (event.ButtonDClick())
2446 {
6fef2483
VZ
2447 if ( m_renameTimer->IsRunning() )
2448 m_renameTimer->Stop();
2449
ca65c044 2450 m_lastOnSame = false;
efbb7287 2451
ddba340d 2452 if ( current == m_lineLastClicked )
efbb7287 2453 {
cf1dfa6b 2454 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
004fd0c8 2455
efbb7287
VZ
2456 return;
2457 }
2458 else
2459 {
8df44392 2460 // The first click was on another item, so don't interpret this as
efbb7287 2461 // a double click, but as a simple click instead
ca65c044 2462 forceClick = true;
efbb7287 2463 }
92976ab6 2464 }
bd8289c1 2465
8df44392 2466 if (event.LeftUp())
c801d85f 2467 {
277ea1b4 2468 if (m_lineSelectSingleOnUp != (size_t)-1)
92976ab6 2469 {
8df44392
RR
2470 // select single line
2471 HighlightAll( false );
2472 ReverseHighlight(m_lineSelectSingleOnUp);
2473 }
277ea1b4 2474
2cb1f3da 2475 if (m_lastOnSame)
8df44392
RR
2476 {
2477 if ((current == m_current) &&
2478 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
2cb1f3da 2479 HasFlag(wxLC_EDIT_LABELS) )
8df44392 2480 {
5595181f
VZ
2481 if ( !InReportView() ||
2482 GetLineLabelRect(current).Contains(x, y) )
a70598d5 2483 {
5595181f
VZ
2484 int dclick = wxSystemSettings::GetMetric(wxSYS_DCLICK_MSEC);
2485 m_renameTimer->Start(dclick > 0 ? dclick : 250, true);
a70598d5 2486 }
8df44392 2487 }
15ec266a
VZ
2488
2489 m_lastOnSame = false;
92976ab6 2490 }
277ea1b4 2491
277ea1b4 2492 m_lineSelectSingleOnUp = (size_t)-1;
8df44392
RR
2493 }
2494 else
2495 {
3103e8a9 2496 // This is necessary, because after a DnD operation in
2997ca30 2497 // from and to ourself, the up event is swallowed by the
8df44392
RR
2498 // DnD code. So on next non-up event (which means here and
2499 // now) m_lineSelectSingleOnUp should be reset.
277ea1b4 2500 m_lineSelectSingleOnUp = (size_t)-1;
e1e955e1 2501 }
8df44392 2502 if (event.RightDown())
b204641e 2503 {
8df44392
RR
2504 m_lineBeforeLastClicked = m_lineLastClicked;
2505 m_lineLastClicked = current;
277ea1b4 2506
eaefbb88
KH
2507 // If the item is already selected, do not update the selection.
2508 // Multi-selections should not be cleared if a selected item is clicked.
2509 if (!IsHighlighted(current))
2510 {
2511 HighlightAll(false);
2512 ChangeCurrent(current);
2513 ReverseHighlight(m_current);
2514 }
277ea1b4
DS
2515
2516 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2517
18216ae3
RR
2518 // Allow generation of context menu event
2519 event.Skip();
b204641e 2520 }
cf1dfa6b 2521 else if (event.MiddleDown())
b204641e 2522 {
cf1dfa6b 2523 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
92976ab6 2524 }
cf1dfa6b 2525 else if ( event.LeftDown() || forceClick )
92976ab6 2526 {
efbb7287 2527 m_lineBeforeLastClicked = m_lineLastClicked;
cf1dfa6b
VZ
2528 m_lineLastClicked = current;
2529
2530 size_t oldCurrent = m_current;
2cb1f3da
JS
2531 bool oldWasSelected = IsHighlighted(m_current);
2532
39e39d39 2533 bool cmdModifierDown = event.CmdDown();
2fe417e4 2534 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
8df44392 2535 {
277ea1b4 2536 if ( IsSingleSel() || !IsHighlighted(current) )
eb3d60a2
KH
2537 {
2538 HighlightAll( false );
0ddefeb0 2539
eb3d60a2 2540 ChangeCurrent(current);
cf1dfa6b 2541
eb3d60a2
KH
2542 ReverseHighlight(m_current);
2543 }
2544 else // multi sel & current is highlighted & no mod keys
8df44392 2545 {
2997ca30 2546 m_lineSelectSingleOnUp = current;
8df44392
RR
2547 ChangeCurrent(current); // change focus
2548 }
e1e955e1 2549 }
b54e41c5 2550 else // multi sel & either ctrl or shift is down
b204641e 2551 {
015fd9ef 2552 if (cmdModifierDown)
92976ab6 2553 {
0ddefeb0 2554 ChangeCurrent(current);
cf1dfa6b 2555
b54e41c5 2556 ReverseHighlight(m_current);
92976ab6 2557 }
473d087e 2558 else if (event.ShiftDown())
92976ab6 2559 {
0ddefeb0 2560 ChangeCurrent(current);
bffa1c77 2561
cf1dfa6b
VZ
2562 size_t lineFrom = oldCurrent,
2563 lineTo = current;
f6bcfd97 2564
cf1dfa6b 2565 if ( lineTo < lineFrom )
92976ab6 2566 {
cf1dfa6b
VZ
2567 lineTo = lineFrom;
2568 lineFrom = m_current;
92976ab6
RR
2569 }
2570
b54e41c5 2571 HighlightLines(lineFrom, lineTo);
92976ab6 2572 }
cf1dfa6b 2573 else // !ctrl, !shift
92976ab6 2574 {
cf1dfa6b 2575 // test in the enclosing if should make it impossible
9a83f860 2576 wxFAIL_MSG( wxT("how did we get here?") );
92976ab6 2577 }
e1e955e1 2578 }
cf1dfa6b 2579
92976ab6 2580 if (m_current != oldCurrent)
92976ab6 2581 RefreshLine( oldCurrent );
efbb7287 2582
15ec266a
VZ
2583 // Set the flag telling us whether the next click on this item should
2584 // start editing its label. This should happen if we clicked on the
2585 // current item and it was already selected, i.e. if this click was not
2586 // done to select it.
2587 //
2588 // It should not happen if this was a double click (forceClick is true)
2589 // nor if we hadn't had the focus before as then this click was used to
2590 // give focus to the control.
2591 m_lastOnSame = (m_current == oldCurrent) && oldWasSelected &&
2592 !forceClick && HasFocus();
e1e955e1 2593 }
e1e955e1 2594}
c801d85f 2595
34bbbc27 2596void wxListMainWindow::MoveToItem(size_t item)
c801d85f 2597{
34bbbc27 2598 if ( item == (size_t)-1 )
cf1dfa6b 2599 return;
004fd0c8 2600
34bbbc27 2601 wxRect rect = GetLineRect(item);
cf3da716 2602
cf1dfa6b 2603 int client_w, client_h;
cf3da716 2604 GetClientSize( &client_w, &client_h );
f6bcfd97 2605
6d78bbe6
VZ
2606 const int hLine = GetLineHeight();
2607
edea2c4a
RR
2608 int view_x = SCROLL_UNIT_X * GetListCtrl()->GetScrollPos( wxHORIZONTAL );
2609 int view_y = hLine * GetListCtrl()->GetScrollPos( wxVERTICAL );
004fd0c8 2610
b5d43d1d 2611 if ( InReportView() )
92976ab6 2612 {
277ea1b4
DS
2613 // the next we need the range of lines shown it might be different,
2614 // so recalculate it
b54e41c5
VZ
2615 ResetVisibleLinesRange();
2616
277ea1b4 2617 if (rect.y < view_y)
06cd40a8 2618 GetListCtrl()->Scroll( -1, rect.y / hLine );
277ea1b4 2619 if (rect.y + rect.height + 5 > view_y + client_h)
06cd40a8 2620 GetListCtrl()->Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
35a7f94b
RD
2621
2622#ifdef __WXMAC__
2623 // At least on Mac the visible lines value will get reset inside of
2624 // Scroll *before* it actually scrolls the window because of the
2625 // Update() that happens there, so it will still have the wrong value.
2626 // So let's reset it again and wait for it to be recalculated in the
2627 // next paint event. I would expect this problem to show up in wxGTK
2628 // too but couldn't duplicate it there. Perhaps the order of events
2629 // is different... --Robin
2630 ResetVisibleLinesRange();
2631#endif
92976ab6 2632 }
b54e41c5 2633 else // !report
92976ab6 2634 {
e81fa385
VZ
2635 int sx = -1,
2636 sy = -1;
2637
807a572e 2638 if (rect.x-view_x < 5)
e81fa385 2639 sx = (rect.x - 5) / SCROLL_UNIT_X;
807a572e 2640 if (rect.x + rect.width - 5 > view_x + client_w)
e81fa385
VZ
2641 sx = (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X;
2642
2643 if (rect.y-view_y < 5)
2644 sy = (rect.y - 5) / hLine;
2645 if (rect.y + rect.height - 5 > view_y + client_h)
2646 sy = (rect.y + rect.height - client_h + hLine) / hLine;
2647
06cd40a8 2648 GetListCtrl()->Scroll(sx, sy);
92976ab6 2649 }
e1e955e1 2650}
c801d85f 2651
66a9201d
VZ
2652bool wxListMainWindow::ScrollList(int WXUNUSED(dx), int dy)
2653{
2654 if ( !InReportView() )
2655 {
2656 // TODO: this should work in all views but is not implemented now
2657 return false;
2658 }
2659
2660 size_t top, bottom;
2661 GetVisibleLinesRange(&top, &bottom);
2662
2663 if ( bottom == (size_t)-1 )
2664 return 0;
2665
2666 ResetVisibleLinesRange();
2667
2668 int hLine = GetLineHeight();
2669
06cd40a8 2670 GetListCtrl()->Scroll(-1, top + dy / hLine);
66a9201d
VZ
2671
2672#ifdef __WXMAC__
2673 // see comment in MoveToItem() for why we do this
2674 ResetVisibleLinesRange();
2675#endif
2676
2677 return true;
2678}
2679
cf1dfa6b
VZ
2680// ----------------------------------------------------------------------------
2681// keyboard handling
2682// ----------------------------------------------------------------------------
2683
2684void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
c801d85f 2685{
cf1dfa6b 2686 wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
9a83f860 2687 wxT("invalid item index in OnArrowChar()") );
cf1dfa6b
VZ
2688
2689 size_t oldCurrent = m_current;
2690
2691 // in single selection we just ignore Shift as we can't select several
2692 // items anyhow
b54e41c5 2693 if ( event.ShiftDown() && !IsSingleSel() )
cf1dfa6b 2694 {
0ddefeb0 2695 ChangeCurrent(newCurrent);
cf1dfa6b 2696
65a1f350
RR
2697 // refresh the old focus to remove it
2698 RefreshLine( oldCurrent );
2699
cf1dfa6b
VZ
2700 // select all the items between the old and the new one
2701 if ( oldCurrent > newCurrent )
2702 {
2703 newCurrent = oldCurrent;
2704 oldCurrent = m_current;
2705 }
2706
b54e41c5 2707 HighlightLines(oldCurrent, newCurrent);
cf1dfa6b
VZ
2708 }
2709 else // !shift
2710 {
b54e41c5 2711 // all previously selected items are unselected unless ctrl is held
a9aead31
VZ
2712 // in a multiselection control
2713 if ( !event.ControlDown() || IsSingleSel() )
ca65c044 2714 HighlightAll(false);
b54e41c5 2715
0ddefeb0 2716 ChangeCurrent(newCurrent);
ca65c044 2717
65a1f350
RR
2718 // refresh the old focus to remove it
2719 RefreshLine( oldCurrent );
cf1dfa6b 2720
a9aead31
VZ
2721 // in single selection mode we must always have a selected item
2722 if ( !event.ControlDown() || IsSingleSel() )
ca65c044 2723 HighlightLine( m_current, true );
cf1dfa6b 2724 }
ca65c044 2725
92976ab6 2726 RefreshLine( m_current );
cf1dfa6b 2727
cf3da716 2728 MoveToFocus();
e1e955e1 2729}
c801d85f 2730
3dfb93fd
RR
2731void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
2732{
2733 wxWindow *parent = GetParent();
004fd0c8 2734
277ea1b4 2735 // propagate the key event upwards
101198ba 2736 wxKeyEvent ke(event);
3dfb93fd 2737 ke.SetEventObject( parent );
524a2c6e 2738 ke.SetId(GetParent()->GetId());
101198ba
VZ
2739 if (parent->GetEventHandler()->ProcessEvent( ke ))
2740 return;
004fd0c8 2741
92d9d10f
SL
2742 // send a list event
2743 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, parent->GetId() );
18dd5378 2744 le.m_item.m_itemId =
92d9d10f
SL
2745 le.m_itemIndex = m_current;
2746 if (HasCurrent())
2747 GetLine(m_current)->GetItem( 0, le.m_item );
2748 le.m_code = event.GetKeyCode();
2749 le.SetEventObject( parent );
2750 if (parent->GetEventHandler()->ProcessEvent( le ))
2751 return;
2752
3dfb93fd
RR
2753 event.Skip();
2754}
004fd0c8 2755
9fe25f13
KO
2756void wxListMainWindow::OnKeyUp( wxKeyEvent &event )
2757{
2758 wxWindow *parent = GetParent();
2759
2760 // propagate the key event upwards
101198ba 2761 wxKeyEvent ke(event);
524a2c6e
VZ
2762 ke.SetEventObject( parent );
2763 ke.SetId(GetParent()->GetId());
101198ba
VZ
2764 if (parent->GetEventHandler()->ProcessEvent( ke ))
2765 return;
9fe25f13
KO
2766
2767 event.Skip();
2768}
2769
64ac3db8
VZ
2770void wxListMainWindow::OnCharHook( wxKeyEvent &event )
2771{
2772 if ( m_textctrlWrapper )
2773 {
2774 // When an in-place editor is active we should ensure that it always
2775 // gets the key events that are special to it.
2776 if ( m_textctrlWrapper->CheckForEndEditKey(event) )
2777 {
2778 // Skip the call to wxEvent::Skip() below.
2779 return;
2780 }
2781 }
2782
2783 event.Skip();
2784}
2785
c801d85f
KB
2786void wxListMainWindow::OnChar( wxKeyEvent &event )
2787{
51cc4dad 2788 wxWindow *parent = GetParent();
004fd0c8 2789
d6a658ff
VZ
2790 // propagate the char event upwards
2791 wxKeyEvent ke(event);
2792 ke.SetEventObject( parent );
524a2c6e 2793 ke.SetId(GetParent()->GetId());
d6a658ff
VZ
2794 if (parent->GetEventHandler()->ProcessEvent( ke ))
2795 return;
004fd0c8 2796
f029f1d1
VS
2797 if ( HandleAsNavigationKey(event) )
2798 return;
004fd0c8 2799
277ea1b4 2800 // no item -> nothing to do
cf1dfa6b 2801 if (!HasCurrent())
c801d85f 2802 {
51cc4dad
RR
2803 event.Skip();
2804 return;
e1e955e1 2805 }
51cc4dad 2806
cf76cd4e
VZ
2807 // don't use m_linesPerPage directly as it might not be computed yet
2808 const int pageSize = GetCountPerPage();
9a83f860 2809 wxCHECK_RET( pageSize, wxT("should have non zero page size") );
cf76cd4e 2810
03703fc0
RR
2811 if (GetLayoutDirection() == wxLayout_RightToLeft)
2812 {
2813 if (event.GetKeyCode() == WXK_RIGHT)
2814 event.m_keyCode = WXK_LEFT;
2815 else if (event.GetKeyCode() == WXK_LEFT)
2816 event.m_keyCode = WXK_RIGHT;
2817 }
2818
d34d31f6
VZ
2819 int keyCode = event.GetKeyCode();
2820 switch ( keyCode )
c801d85f 2821 {
51cc4dad 2822 case WXK_UP:
cf1dfa6b
VZ
2823 if ( m_current > 0 )
2824 OnArrowChar( m_current - 1, event );
51cc4dad 2825 break;
cf1dfa6b 2826
51cc4dad 2827 case WXK_DOWN:
cf1dfa6b
VZ
2828 if ( m_current < (size_t)GetItemCount() - 1 )
2829 OnArrowChar( m_current + 1, event );
51cc4dad 2830 break;
cf1dfa6b 2831
51cc4dad 2832 case WXK_END:
1370703e 2833 if (!IsEmpty())
cf1dfa6b 2834 OnArrowChar( GetItemCount() - 1, event );
51cc4dad 2835 break;
cf1dfa6b 2836
51cc4dad 2837 case WXK_HOME:
1370703e 2838 if (!IsEmpty())
cf1dfa6b 2839 OnArrowChar( 0, event );
51cc4dad 2840 break;
cf1dfa6b 2841
faa94f3e 2842 case WXK_PAGEUP:
f6bcfd97 2843 {
cf76cd4e
VZ
2844 int steps = InReportView() ? pageSize - 1
2845 : m_current % pageSize;
cf1dfa6b
VZ
2846
2847 int index = m_current - steps;
2848 if (index < 0)
2849 index = 0;
2850
2851 OnArrowChar( index, event );
51cc4dad 2852 }
51cc4dad 2853 break;
cf1dfa6b 2854
faa94f3e 2855 case WXK_PAGEDOWN:
bffa1c77 2856 {
b5d43d1d 2857 int steps = InReportView()
cf76cd4e
VZ
2858 ? pageSize - 1
2859 : pageSize - (m_current % pageSize) - 1;
f6bcfd97 2860
cf1dfa6b
VZ
2861 size_t index = m_current + steps;
2862 size_t count = GetItemCount();
2863 if ( index >= count )
2864 index = count - 1;
2865
2866 OnArrowChar( index, event );
51cc4dad 2867 }
51cc4dad 2868 break;
cf1dfa6b 2869
51cc4dad 2870 case WXK_LEFT:
b5d43d1d 2871 if ( !InReportView() )
51cc4dad 2872 {
cf76cd4e 2873 int index = m_current - pageSize;
cf1dfa6b
VZ
2874 if (index < 0)
2875 index = 0;
2876
2877 OnArrowChar( index, event );
51cc4dad
RR
2878 }
2879 break;
cf1dfa6b 2880
51cc4dad 2881 case WXK_RIGHT:
b5d43d1d 2882 if ( !InReportView() )
51cc4dad 2883 {
cf76cd4e 2884 size_t index = m_current + pageSize;
cf1dfa6b
VZ
2885
2886 size_t count = GetItemCount();
2887 if ( index >= count )
2888 index = count - 1;
2889
2890 OnArrowChar( index, event );
51cc4dad
RR
2891 }
2892 break;
cf1dfa6b 2893
51cc4dad 2894 case WXK_SPACE:
b54e41c5 2895 if ( IsSingleSel() )
33d0e17c 2896 {
a9aead31
VZ
2897 if ( event.ControlDown() )
2898 {
2899 ReverseHighlight(m_current);
2900 }
2901 else // normal space press
70541533 2902 {
a9aead31 2903 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
70541533 2904 }
33d0e17c 2905 }
a9aead31
VZ
2906 else // multiple selection
2907 {
2908 ReverseHighlight(m_current);
2909 }
51cc4dad 2910 break;
cf1dfa6b 2911
51cc4dad
RR
2912 case WXK_RETURN:
2913 case WXK_EXECUTE:
87948f22 2914 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
51cc4dad 2915 break;
cf1dfa6b 2916
51cc4dad 2917 default:
d34d31f6
VZ
2918 if ( !event.HasModifiers() &&
2919 ((keyCode >= '0' && keyCode <= '9') ||
2920 (keyCode >= 'a' && keyCode <= 'z') ||
2921 (keyCode >= 'A' && keyCode <= 'Z') ||
2922 (keyCode == '_') ||
2923 (keyCode == '+') ||
2924 (keyCode == '*') ||
2925 (keyCode == '-')))
2926 {
2927 // find the next item starting with the given prefix
2928 wxChar ch = (wxChar)keyCode;
2929 size_t item;
2930
2931 // if the same character is typed multiple times then go to the
2932 // next entry starting with that character instead of searching
2933 // for an item starting with multiple copies of this character,
2934 // this is more useful and is how it works under Windows.
2935 if ( m_findPrefix.length() == 1 && m_findPrefix[0] == ch )
2936 {
2937 item = PrefixFindItem(m_current, ch);
2938 }
2939 else
2940 {
2941 const wxString newPrefix(m_findPrefix + ch);
2942 item = PrefixFindItem(m_current, newPrefix);
2943 if ( item != (size_t)-1 )
2944 m_findPrefix = newPrefix;
2945 }
2946
2947 // also start the timer to reset the current prefix if the user
2948 // doesn't press any more alnum keys soon -- we wouldn't want
2949 // to use this prefix for a new item search
2950 if ( !m_findTimer )
2951 {
2952 m_findTimer = new wxListFindTimer( this );
2953 }
2954
2955 // Notice that we should start the timer even if we didn't find
2956 // anything to make sure we reset the search state later.
2957 m_findTimer->Start(wxListFindTimer::DELAY, wxTIMER_ONE_SHOT);
2958
2959 // restart timer even when there's no match so bell get's reset
2960 if ( item != (size_t)-1 )
2961 {
2962 // Select the found item and go to it.
2963 HighlightAll(false);
2964 SetItemState(item,
2965 wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED,
2966 wxLIST_STATE_FOCUSED | wxLIST_STATE_SELECTED);
2967
2968 // Reset the bell flag if it had been temporarily disabled
2969 // before.
2970 if ( m_findBell )
2971 m_findBell = 1;
2972 }
2973 else // No such item
2974 {
2975 // Signal it with a bell if enabled.
2976 if ( m_findBell == 1 )
2977 {
2978 ::wxBell();
2979
2980 // Disable it for the next unsuccessful match, we only
2981 // beep once, this is usually enough and continuing to
2982 // do it would be annoying.
2983 m_findBell = -1;
2984 }
2985 }
2986 }
2987 else
2988 {
2989 event.Skip();
2990 }
e1e955e1 2991 }
e1e955e1 2992}
c801d85f 2993
cf1dfa6b
VZ
2994// ----------------------------------------------------------------------------
2995// focus handling
2996// ----------------------------------------------------------------------------
2997
c801d85f
KB
2998void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
2999{
dc76287d
RD
3000 if ( GetParent() )
3001 {
3002 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
3003 event.SetEventObject( GetParent() );
3004 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3005 return;
3006 }
3007
bde9072f
VZ
3008 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3009 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3010 // which are already drawn correctly resulting in horrible flicker - avoid
3011 // it
47724389
VZ
3012 if ( !m_hasFocus )
3013 {
ca65c044 3014 m_hasFocus = true;
bde9072f 3015
47724389
VZ
3016 RefreshSelected();
3017 }
e1e955e1 3018}
c801d85f
KB
3019
3020void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
3021{
dc76287d
RD
3022 if ( GetParent() )
3023 {
3024 wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
3025 event.SetEventObject( GetParent() );
3026 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3027 return;
3028 }
277ea1b4 3029
ca65c044 3030 m_hasFocus = false;
49ecb029 3031 RefreshSelected();
e1e955e1 3032}
c801d85f 3033
1e6d9499 3034void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
c801d85f 3035{
cf1dfa6b 3036 if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
63852e78
RR
3037 {
3038 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
63852e78 3039 }
cf1dfa6b 3040 else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
63852e78
RR
3041 {
3042 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3043 }
cf1dfa6b 3044 else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
0b855868
RR
3045 {
3046 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3047 }
b5d43d1d 3048 else if ( InReportView() && (m_small_image_list))
63852e78
RR
3049 {
3050 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
63852e78 3051 }
e1e955e1 3052}
c801d85f 3053
5cd89174 3054void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
c801d85f 3055{
cf1dfa6b 3056 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
63852e78
RR
3057 {
3058 m_normal_image_list->GetSize( index, width, height );
63852e78 3059 }
cf1dfa6b 3060 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
63852e78
RR
3061 {
3062 m_small_image_list->GetSize( index, width, height );
63852e78 3063 }
cf1dfa6b 3064 else if ( HasFlag(wxLC_LIST) && m_small_image_list )
0b855868
RR
3065 {
3066 m_small_image_list->GetSize( index, width, height );
0b855868 3067 }
b5d43d1d 3068 else if ( InReportView() && m_small_image_list )
63852e78
RR
3069 {
3070 m_small_image_list->GetSize( index, width, height );
63852e78 3071 }
cf1dfa6b
VZ
3072 else
3073 {
3074 width =
3075 height = 0;
3076 }
e1e955e1 3077}
c801d85f 3078
8a3e173a 3079void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
c801d85f 3080{
ca65c044 3081 m_dirty = true;
f6bcfd97
BP
3082
3083 // calc the spacing from the icon size
277ea1b4
DS
3084 int width = 0, height = 0;
3085
f6bcfd97 3086 if ((imageList) && (imageList->GetImageCount()) )
f6bcfd97 3087 imageList->GetSize(0, width, height);
f6bcfd97
BP
3088
3089 if (which == wxIMAGE_LIST_NORMAL)
3090 {
3091 m_normal_image_list = imageList;
3092 m_normal_spacing = width + 8;
3093 }
3094
3095 if (which == wxIMAGE_LIST_SMALL)
3096 {
3097 m_small_image_list = imageList;
3098 m_small_spacing = width + 14;
ca7353de 3099 m_lineHeight = 0; // ensure that the line height will be recalc'd
f6bcfd97 3100 }
e1e955e1 3101}
c801d85f 3102
debe6624 3103void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
c801d85f 3104{
ca65c044 3105 m_dirty = true;
139adb6a 3106 if (isSmall)
139adb6a 3107 m_small_spacing = spacing;
139adb6a 3108 else
139adb6a 3109 m_normal_spacing = spacing;
e1e955e1 3110}
c801d85f 3111
debe6624 3112int wxListMainWindow::GetItemSpacing( bool isSmall )
c801d85f 3113{
f6bcfd97 3114 return isSmall ? m_small_spacing : m_normal_spacing;
e1e955e1 3115}
c801d85f 3116
cf1dfa6b
VZ
3117// ----------------------------------------------------------------------------
3118// columns
3119// ----------------------------------------------------------------------------
3120
3d167c00
VZ
3121int
3122wxListMainWindow::ComputeMinHeaderWidth(const wxListHeaderData* column) const
3123{
3124 wxClientDC dc(const_cast<wxListMainWindow*>(this));
3125
3126 int width = dc.GetTextExtent(column->GetText()).x + AUTOSIZE_COL_MARGIN;
3127
3128 width += 2*EXTRA_WIDTH;
3129
3130 // check for column header's image availability
3131 const int image = column->GetImage();
3132 if ( image != -1 )
3133 {
3134 if ( m_small_image_list )
3135 {
3136 int ix = 0, iy = 0;
3137 m_small_image_list->GetSize(image, ix, iy);
3138 width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
3139 }
3140 }
3141
3142 return width;
3143}
3144
94248d2e 3145void wxListMainWindow::SetColumn( int col, const wxListItem &item )
c801d85f 3146{
222ed1d6 3147 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
f6bcfd97 3148
9a83f860 3149 wxCHECK_RET( node, wxT("invalid column index in SetColumn") );
cf1dfa6b 3150
cf1dfa6b
VZ
3151 wxListHeaderData *column = node->GetData();
3152 column->SetItem( item );
3153
94248d2e 3154 if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
3d167c00 3155 column->SetWidth(ComputeMinHeaderWidth(column));
94248d2e 3156
cf1dfa6b 3157 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
f6bcfd97 3158 if ( headerWin )
ca65c044 3159 headerWin->m_dirty = true;
cf1dfa6b 3160
ca65c044 3161 m_dirty = true;
cf1dfa6b
VZ
3162
3163 // invalidate it as it has to be recalculated
3164 m_headerWidth = 0;
e1e955e1 3165}
c801d85f 3166
debe6624 3167void wxListMainWindow::SetColumnWidth( int col, int width )
c801d85f 3168{
cf1dfa6b 3169 wxCHECK_RET( col >= 0 && col < GetColumnCount(),
9a83f860 3170 wxT("invalid column index") );
cf1dfa6b 3171
b5d43d1d 3172 wxCHECK_RET( InReportView(),
9a83f860 3173 wxT("SetColumnWidth() can only be called in report mode.") );
7d7b3f69 3174
ca65c044 3175 m_dirty = true;
06cd40a8 3176
abe4a4f1
VS
3177 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3178 if ( headerWin )
ca65c044 3179 headerWin->m_dirty = true;
bd8289c1 3180
222ed1d6 3181 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
9a83f860 3182 wxCHECK_RET( node, wxT("no column?") );
cf1dfa6b
VZ
3183
3184 wxListHeaderData *column = node->GetData();
3185
3186 size_t count = GetItemCount();
3187
f6bcfd97
BP
3188 if (width == wxLIST_AUTOSIZE_USEHEADER)
3189 {
3d167c00 3190 width = ComputeMinHeaderWidth(column);
f6bcfd97 3191 }
cf1dfa6b 3192 else if ( width == wxLIST_AUTOSIZE )
0180dad6 3193 {
3d167c00
VZ
3194 width = ComputeMinHeaderWidth(column);
3195
3196 if ( !IsVirtual() )
0180dad6 3197 {
cf1dfa6b
VZ
3198 wxClientDC dc(this);
3199 dc.SetFont( GetFont() );
3200
3201 int max = AUTOSIZE_COL_MARGIN;
3202
7d4813a0
VZ
3203 // if the cached column width isn't valid then recalculate it
3204 if (m_aColWidths.Item(col)->bNeedsUpdate)
0180dad6 3205 {
7d4813a0
VZ
3206 for (size_t i = 0; i < count; i++)
3207 {
277ea1b4 3208 wxListLineData *line = GetLine( i );
7d4813a0 3209 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
cf1dfa6b 3210
9a83f860 3211 wxCHECK_RET( n, wxT("no subitem?") );
cf1dfa6b 3212
7d4813a0
VZ
3213 wxListItemData *itemData = n->GetData();
3214 wxListItem item;
cf1dfa6b 3215
7d4813a0
VZ
3216 itemData->GetItem(item);
3217 int itemWidth = GetItemWidthWithImage(&item);
3218 if (itemWidth > max)
3219 max = itemWidth;
bffa1c77 3220 }
cf1dfa6b 3221
7d4813a0
VZ
3222 m_aColWidths.Item(col)->bNeedsUpdate = false;
3223 m_aColWidths.Item(col)->nMaxWidth = max;
0180dad6 3224 }
cf1dfa6b 3225
3d167c00
VZ
3226 max = m_aColWidths.Item(col)->nMaxWidth + AUTOSIZE_COL_MARGIN;
3227 if ( width < max )
3228 width = max;
0180dad6 3229 }
0180dad6
RR
3230 }
3231
cf1dfa6b 3232 column->SetWidth( width );
bd8289c1 3233
cf1dfa6b
VZ
3234 // invalidate it as it has to be recalculated
3235 m_headerWidth = 0;
3236}
3237
3238int wxListMainWindow::GetHeaderWidth() const
3239{
3240 if ( !m_headerWidth )
0208334d 3241 {
cf1dfa6b
VZ
3242 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
3243
3244 size_t count = GetColumnCount();
3245 for ( size_t col = 0; col < count; col++ )
63852e78 3246 {
cf1dfa6b 3247 self->m_headerWidth += GetColumnWidth(col);
63852e78 3248 }
0208334d 3249 }
bd8289c1 3250
cf1dfa6b 3251 return m_headerWidth;
e1e955e1 3252}
c801d85f 3253
cf1dfa6b 3254void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
c801d85f 3255{
222ed1d6 3256 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
9a83f860 3257 wxCHECK_RET( node, wxT("invalid column index in GetColumn") );
cf1dfa6b
VZ
3258
3259 wxListHeaderData *column = node->GetData();
3260 column->GetItem( item );
e1e955e1 3261}
c801d85f 3262
cf1dfa6b 3263int wxListMainWindow::GetColumnWidth( int col ) const
c801d85f 3264{
222ed1d6 3265 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
9a83f860 3266 wxCHECK_MSG( node, 0, wxT("invalid column index") );
24b9f055
VZ
3267
3268 wxListHeaderData *column = node->GetData();
3269 return column->GetWidth();
e1e955e1 3270}
c801d85f 3271
cf1dfa6b
VZ
3272// ----------------------------------------------------------------------------
3273// item state
3274// ----------------------------------------------------------------------------
c801d85f
KB
3275
3276void wxListMainWindow::SetItem( wxListItem &item )
3277{
cf1dfa6b
VZ
3278 long id = item.m_itemId;
3279 wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
9a83f860 3280 wxT("invalid item index in SetItem") );
cf1dfa6b 3281
6c02c329
VZ
3282 if ( !IsVirtual() )
3283 {
3284 wxListLineData *line = GetLine((size_t)id);
3285 line->SetItem( item.m_col, item );
7d4813a0 3286
39a7f085
VZ
3287 // Set item state if user wants
3288 if ( item.m_mask & wxLIST_MASK_STATE )
3289 SetItemState( item.m_itemId, item.m_state, item.m_state );
3290
7d4813a0
VZ
3291 if (InReportView())
3292 {
3293 // update the Max Width Cache if needed
3294 int width = GetItemWidthWithImage(&item);
3295
3296 if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
3297 m_aColWidths.Item(item.m_col)->nMaxWidth = width;
3298 }
6c02c329
VZ
3299 }
3300
b25278d8
VZ
3301 // update the item on screen unless we're going to update everything soon
3302 // anyhow
3303 if ( !m_dirty )
3304 {
3305 wxRect rectItem;
3306 GetItemRect(id, rectItem);
3307 RefreshRect(rectItem);
3308 }
e1e955e1 3309}
c801d85f 3310
0afa5859
VZ
3311void wxListMainWindow::SetItemStateAll(long state, long stateMask)
3312{
3313 if ( IsEmpty() )
3314 return;
3315
3316 // first deal with selection
3317 if ( stateMask & wxLIST_STATE_SELECTED )
3318 {
3319 // set/clear select state
3320 if ( IsVirtual() )
3321 {
3322 // optimized version for virtual listctrl.
3323 m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
3324 Refresh();
3325 }
3326 else if ( state & wxLIST_STATE_SELECTED )
3327 {
3328 const long count = GetItemCount();
3329 for( long i = 0; i < count; i++ )
3330 {
3331 SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
3332 }
3333
3334 }
3335 else
3336 {
3337 // clear for non virtual (somewhat optimized by using GetNextItem())
3338 long i = -1;
3339 while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
3340 {
3341 SetItemState( i, 0, wxLIST_STATE_SELECTED );
3342 }
3343 }
3344 }
3345
3346 if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
3347 {
3348 // unfocus all: only one item can be focussed, so clearing focus for
3349 // all items is simply clearing focus of the focussed item.
3350 SetItemState(m_current, state, stateMask);
3351 }
3352 //(setting focus to all items makes no sense, so it is not handled here.)
3353}
3354
cf1dfa6b 3355void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
c801d85f 3356{
0afa5859
VZ
3357 if ( litem == -1 )
3358 {
3359 SetItemStateAll(state, stateMask);
3360 return;
3361 }
3362
7448de8d 3363 wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
9a83f860 3364 wxT("invalid list ctrl item index in SetItem") );
54442116 3365
cf1dfa6b 3366 size_t oldCurrent = m_current;
88b792af 3367 size_t item = (size_t)litem; // safe because of the check above
bd8289c1 3368
88b792af 3369 // do we need to change the focus?
54442116 3370 if ( stateMask & wxLIST_STATE_FOCUSED )
c801d85f 3371 {
54442116 3372 if ( state & wxLIST_STATE_FOCUSED )
92976ab6 3373 {
54442116 3374 // don't do anything if this item is already focused
cf1dfa6b 3375 if ( item != m_current )
92976ab6 3376 {
0ddefeb0 3377 ChangeCurrent(item);
cf1dfa6b 3378
88b792af 3379 if ( oldCurrent != (size_t)-1 )
cf1dfa6b 3380 {
88b792af
VZ
3381 if ( IsSingleSel() )
3382 {
ca65c044 3383 HighlightLine(oldCurrent, false);
88b792af
VZ
3384 }
3385
cf1dfa6b
VZ
3386 RefreshLine(oldCurrent);
3387 }
54442116 3388
92976ab6 3389 RefreshLine( m_current );
92976ab6 3390 }
54442116
VZ
3391 }
3392 else // unfocus
3393 {
3394 // don't do anything if this item is not focused
cf1dfa6b 3395 if ( item == m_current )
bffa1c77 3396 {
0ddefeb0 3397 ResetCurrent();
88b792af 3398
943f6ad3
VZ
3399 if ( IsSingleSel() )
3400 {
3401 // we must unselect the old current item as well or we
3402 // might end up with more than one selected item in a
3403 // single selection control
ca65c044 3404 HighlightLine(oldCurrent, false);
943f6ad3
VZ
3405 }
3406
88b792af 3407 RefreshLine( oldCurrent );
bffa1c77 3408 }
92976ab6 3409 }
e1e955e1 3410 }
54442116 3411
88b792af 3412 // do we need to change the selection state?
54442116
VZ
3413 if ( stateMask & wxLIST_STATE_SELECTED )
3414 {
3415 bool on = (state & wxLIST_STATE_SELECTED) != 0;
54442116 3416
b54e41c5 3417 if ( IsSingleSel() )
54442116 3418 {
cf1dfa6b
VZ
3419 if ( on )
3420 {
3421 // selecting the item also makes it the focused one in the
3422 // single sel mode
3423 if ( m_current != item )
3424 {
0ddefeb0 3425 ChangeCurrent(item);
cf1dfa6b
VZ
3426
3427 if ( oldCurrent != (size_t)-1 )
3428 {
ca65c044 3429 HighlightLine( oldCurrent, false );
cf1dfa6b
VZ
3430 RefreshLine( oldCurrent );
3431 }
3432 }
3433 }
3434 else // off
3435 {
3436 // only the current item may be selected anyhow
3437 if ( item != m_current )
3438 return;
3439 }
54442116
VZ
3440 }
3441
b54e41c5 3442 if ( HighlightLine(item, on) )
54442116 3443 {
cf1dfa6b 3444 RefreshLine(item);
54442116
VZ
3445 }
3446 }
e1e955e1 3447}
c801d85f 3448
62d89eb4 3449int wxListMainWindow::GetItemState( long item, long stateMask ) const
c801d85f 3450{
cf1dfa6b 3451 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
9a83f860 3452 wxT("invalid list ctrl item index in GetItemState()") );
cf1dfa6b 3453
92976ab6 3454 int ret = wxLIST_STATE_DONTCARE;
cf1dfa6b
VZ
3455
3456 if ( stateMask & wxLIST_STATE_FOCUSED )
c801d85f 3457 {
cf1dfa6b
VZ
3458 if ( (size_t)item == m_current )
3459 ret |= wxLIST_STATE_FOCUSED;
e1e955e1 3460 }
cf1dfa6b
VZ
3461
3462 if ( stateMask & wxLIST_STATE_SELECTED )
c801d85f 3463 {
b54e41c5 3464 if ( IsHighlighted(item) )
cf1dfa6b 3465 ret |= wxLIST_STATE_SELECTED;
e1e955e1 3466 }
cf1dfa6b 3467
92976ab6 3468 return ret;
e1e955e1 3469}
c801d85f 3470
62d89eb4 3471void wxListMainWindow::GetItem( wxListItem &item ) const
c801d85f 3472{
cf1dfa6b 3473 wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
9a83f860 3474 wxT("invalid item index in GetItem") );
cf1dfa6b
VZ
3475
3476 wxListLineData *line = GetLine((size_t)item.m_itemId);
3477 line->GetItem( item.m_col, item );
39a7f085
VZ
3478
3479 // Get item state if user wants it
3480 if ( item.m_mask & wxLIST_MASK_STATE )
3481 item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
3482 wxLIST_STATE_FOCUSED );
e1e955e1 3483}
c801d85f 3484
cf1dfa6b
VZ
3485// ----------------------------------------------------------------------------
3486// item count
3487// ----------------------------------------------------------------------------
3488
3489size_t wxListMainWindow::GetItemCount() const
1370703e
VZ
3490{
3491 return IsVirtual() ? m_countVirt : m_lines.GetCount();
3492}
3493
3494void wxListMainWindow::SetItemCount(long count)
c801d85f 3495{
b54e41c5 3496 m_selStore.SetItemCount(count);
1370703e
VZ
3497 m_countVirt = count;
3498
850ed6e7
VZ
3499 ResetVisibleLinesRange();
3500
5fe143df 3501 // scrollbars must be reset
ca65c044 3502 m_dirty = true;
e1e955e1 3503}
c801d85f 3504
62d89eb4 3505int wxListMainWindow::GetSelectedItemCount() const
c801d85f 3506{
cf1dfa6b 3507 // deal with the quick case first
b54e41c5 3508 if ( IsSingleSel() )
ca65c044 3509 return HasCurrent() ? IsHighlighted(m_current) : false;
cf1dfa6b
VZ
3510
3511 // virtual controls remmebers all its selections itself
3512 if ( IsVirtual() )
b54e41c5 3513 return m_selStore.GetSelectedCount();
cf1dfa6b
VZ
3514
3515 // TODO: we probably should maintain the number of items selected even for
3516 // non virtual controls as enumerating all lines is really slow...
3517 size_t countSel = 0;
3518 size_t count = GetItemCount();
3519 for ( size_t line = 0; line < count; line++ )
92976ab6 3520 {
b54e41c5 3521 if ( GetLine(line)->IsHighlighted() )
cf1dfa6b 3522 countSel++;
92976ab6 3523 }
c801d85f 3524
cf1dfa6b 3525 return countSel;
e1e955e1 3526}
e3e65dac 3527
cf1dfa6b
VZ
3528// ----------------------------------------------------------------------------
3529// item position/size
3530// ----------------------------------------------------------------------------
3531
11ebea16
VZ
3532wxRect wxListMainWindow::GetViewRect() const
3533{
929b7901 3534 wxASSERT_MSG( !HasFlag(wxLC_LIST), "not implemented for list view" );
11ebea16
VZ
3535
3536 // we need to find the longest/tallest label
277ea1b4 3537 wxCoord xMax = 0, yMax = 0;
11ebea16
VZ
3538 const int count = GetItemCount();
3539 if ( count )
3540 {
3541 for ( int i = 0; i < count; i++ )
3542 {
ebf82d2b
VZ
3543 // we need logical, not physical, coordinates here, so use
3544 // GetLineRect() instead of GetItemRect()
3545 wxRect r = GetLineRect(i);
11ebea16
VZ
3546
3547 wxCoord x = r.GetRight(),
3548 y = r.GetBottom();
3549
3550 if ( x > xMax )
3551 xMax = x;
3552 if ( y > yMax )
3553 yMax = y;
3554 }
3555 }
3556
eb6c4508 3557 // some fudge needed to make it look prettier
277ea1b4
DS
3558 xMax += 2 * EXTRA_BORDER_X;
3559 yMax += 2 * EXTRA_BORDER_Y;
eb6c4508
VZ
3560
3561 // account for the scrollbars if necessary
3562 const wxSize sizeAll = GetClientSize();
3563 if ( xMax > sizeAll.x )
3564 yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
3565 if ( yMax > sizeAll.y )
3566 xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
11ebea16
VZ
3567
3568 return wxRect(0, 0, xMax, yMax);
3569}
3570
e974c5d2
VZ
3571bool
3572wxListMainWindow::GetSubItemRect(long item, long subItem, wxRect& rect) const
c801d85f 3573{
b8e57034
VZ
3574 wxCHECK_MSG( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM || InReportView(),
3575 false,
9a83f860 3576 wxT("GetSubItemRect only meaningful in report view") );
e974c5d2 3577 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), false,
9a83f860 3578 wxT("invalid item in GetSubItemRect") );
cf1dfa6b 3579
1e54be64
VZ
3580 // ensure that we're laid out, otherwise we could return nonsense
3581 if ( m_dirty )
3582 {
3583 wxConstCast(this, wxListMainWindow)->
ca65c044 3584 RecalculatePositions(true /* no refresh */);
1e54be64
VZ
3585 }
3586
e974c5d2
VZ
3587 rect = GetLineRect((size_t)item);
3588
3589 // Adjust rect to specified column
3590 if ( subItem != wxLIST_GETSUBITEMRECT_WHOLEITEM )
3591 {
3592 wxCHECK_MSG( subItem >= 0 && subItem < GetColumnCount(), false,
9a83f860 3593 wxT("invalid subItem in GetSubItemRect") );
e974c5d2
VZ
3594
3595 for (int i = 0; i < subItem; i++)
3596 {
3597 rect.x += GetColumnWidth(i);
3598 }
3599 rect.width = GetColumnWidth(subItem);
3600 }
b54e41c5 3601
06cd40a8 3602 GetListCtrl()->CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
e974c5d2
VZ
3603
3604 return true;
e1e955e1 3605}
c801d85f 3606
62d89eb4 3607bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
c801d85f 3608{
cf1dfa6b
VZ
3609 wxRect rect;
3610 GetItemRect(item, rect);
bd8289c1 3611
cf1dfa6b
VZ
3612 pos.x = rect.x;
3613 pos.y = rect.y;
bd8289c1 3614
ca65c044 3615 return true;
e1e955e1 3616}
c801d85f 3617
cf1dfa6b
VZ
3618// ----------------------------------------------------------------------------
3619// geometry calculation
3620// ----------------------------------------------------------------------------
3621
34bbbc27 3622void wxListMainWindow::RecalculatePositions(bool noRefresh)
c801d85f 3623{
6d6d86a6
RD
3624 const int lineHeight = GetLineHeight();
3625
1e6d9499 3626 wxClientDC dc( this );
92976ab6 3627 dc.SetFont( GetFont() );
c801d85f 3628
13602ebd
VZ
3629 const size_t count = GetItemCount();
3630
cf1dfa6b 3631 int iconSpacing;
026d7276 3632 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
cf1dfa6b 3633 iconSpacing = m_normal_spacing;
026d7276 3634 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
cf1dfa6b
VZ
3635 iconSpacing = m_small_spacing;
3636 else
3637 iconSpacing = 0;
004fd0c8 3638
bf632edd 3639 // Note that we do not call GetClientSize() here but
34621cc5 3640 // GetSize() and subtract the border size for sunken
bf632edd
RR
3641 // borders manually. This is technically incorrect,
3642 // but we need to know the client area's size WITHOUT
3643 // scrollbars here. Since we don't know if there are
3644 // any scrollbars, we use GetSize() instead. Another
3645 // solution would be to call SetScrollbars() here to
3646 // remove the scrollbars and call GetClientSize() then,
3647 // but this might result in flicker and - worse - will
3648 // reset the scrollbars to 0 which is not good at all
3649 // if you resize a dialog/window, but don't want to
3650 // reset the window scrolling. RR.
3651 // Furthermore, we actually do NOT subtract the border
3652 // width as 2 pixels is just the extra space which we
3653 // need around the actual content in the window. Other-
3654 // wise the text would e.g. touch the upper border. RR.
cf1dfa6b
VZ
3655 int clientWidth,
3656 clientHeight;
bf632edd 3657 GetSize( &clientWidth, &clientHeight );
a77ec46d 3658
b5d43d1d 3659 if ( InReportView() )
cf1dfa6b 3660 {
13602ebd 3661 // all lines have the same height and we scroll one line per step
277ea1b4 3662 int entireHeight = count * lineHeight + LINE_SPACING;
bd8289c1 3663
b54e41c5
VZ
3664 m_linesPerPage = clientHeight / lineHeight;
3665
3666 ResetVisibleLinesRange();
2c1f73ee 3667
06cd40a8 3668 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X, lineHeight,
13602ebd
VZ
3669 GetHeaderWidth() / SCROLL_UNIT_X,
3670 (entireHeight + lineHeight - 1) / lineHeight,
06cd40a8
RR
3671 GetListCtrl()->GetScrollPos(wxHORIZONTAL),
3672 GetListCtrl()->GetScrollPos(wxVERTICAL),
ca65c044 3673 true );
e1e955e1 3674 }
cf1dfa6b 3675 else // !report
92976ab6 3676 {
13602ebd
VZ
3677 // we have 3 different layout strategies: either layout all items
3678 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3679 // to arrange them in top to bottom, left to right (don't ask me why
3680 // not the other way round...) order
3681 if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) )
e487524e 3682 {
13602ebd
VZ
3683 int x = EXTRA_BORDER_X;
3684 int y = EXTRA_BORDER_Y;
cf1dfa6b 3685
94dd23ae
VZ
3686 wxCoord widthMax = 0;
3687
3688 size_t i;
3689 for ( i = 0; i < count; i++ )
92976ab6 3690 {
cf1dfa6b 3691 wxListLineData *line = GetLine(i);
92976ab6 3692 line->CalculateSize( &dc, iconSpacing );
13602ebd 3693 line->SetPosition( x, y, iconSpacing );
cf1dfa6b 3694
5cd89174 3695 wxSize sizeLine = GetLineSize(i);
cf1dfa6b 3696
13602ebd
VZ
3697 if ( HasFlag(wxLC_ALIGN_TOP) )
3698 {
94dd23ae
VZ
3699 if ( sizeLine.x > widthMax )
3700 widthMax = sizeLine.x;
3701
13602ebd
VZ
3702 y += sizeLine.y;
3703 }
3704 else // wxLC_ALIGN_LEFT
3705 {
3706 x += sizeLine.x + MARGIN_BETWEEN_ROWS;
3707 }
3708 }
3709
94dd23ae
VZ
3710 if ( HasFlag(wxLC_ALIGN_TOP) )
3711 {
3712 // traverse the items again and tweak their sizes so that they are
3713 // all the same in a row
3714 for ( i = 0; i < count; i++ )
3715 {
3716 wxListLineData *line = GetLine(i);
3717 line->m_gi->ExtendWidth(widthMax);
3718 }
3719 }
3720
06cd40a8 3721 GetListCtrl()->SetScrollbars
13602ebd
VZ
3722 (
3723 SCROLL_UNIT_X,
6d78bbe6 3724 lineHeight,
13602ebd 3725 (x + SCROLL_UNIT_X) / SCROLL_UNIT_X,
6d78bbe6 3726 (y + lineHeight) / lineHeight,
06cd40a8
RR
3727 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
3728 GetListCtrl()->GetScrollPos( wxVERTICAL ),
ca65c044 3729 true
13602ebd
VZ
3730 );
3731 }
3732 else // "flowed" arrangement, the most complicated case
3733 {
3734 // at first we try without any scrollbars, if the items don't fit into
3735 // the window, we recalculate after subtracting the space taken by the
3736 // scrollbar
3737
8a39593e 3738 int entireWidth = 0;
cf1dfa6b 3739
13602ebd
VZ
3740 for (int tries = 0; tries < 2; tries++)
3741 {
277ea1b4 3742 entireWidth = 2 * EXTRA_BORDER_X;
cf1dfa6b 3743
13602ebd 3744 if (tries == 1)
92976ab6 3745 {
13602ebd
VZ
3746 // Now we have decided that the items do not fit into the
3747 // client area, so we need a scrollbar
3748 entireWidth += SCROLL_UNIT_X;
92976ab6 3749 }
a77ec46d 3750
13602ebd
VZ
3751 int x = EXTRA_BORDER_X;
3752 int y = EXTRA_BORDER_Y;
4d0c2308
VZ
3753
3754 // Note that "row" here is vertical, i.e. what is called
3755 // "column" in many other places in wxWidgets.
13602ebd
VZ
3756 int maxWidthInThisRow = 0;
3757
3758 m_linesPerPage = 0;
3759 int currentlyVisibleLines = 0;
a77ec46d 3760
13602ebd 3761 for (size_t i = 0; i < count; i++)
92976ab6 3762 {
13602ebd 3763 currentlyVisibleLines++;
277ea1b4 3764 wxListLineData *line = GetLine( i );
13602ebd
VZ
3765 line->CalculateSize( &dc, iconSpacing );
3766 line->SetPosition( x, y, iconSpacing );
3767
277ea1b4 3768 wxSize sizeLine = GetLineSize( i );
a77ec46d 3769
13602ebd
VZ
3770 if ( maxWidthInThisRow < sizeLine.x )
3771 maxWidthInThisRow = sizeLine.x;
3772
3773 y += sizeLine.y;
3774 if (currentlyVisibleLines > m_linesPerPage)
3775 m_linesPerPage = currentlyVisibleLines;
3776
4d0c2308
VZ
3777 // Have we reached the end of the row either because no
3778 // more items would fit or because there are simply no more
3779 // items?
3780 if ( y + sizeLine.y >= clientHeight
3781 || i == count - 1)
13602ebd 3782 {
4d0c2308 3783 // Adjust all items in this row to have the same
08be21f5
VZ
3784 // width to ensure that they all align horizontally in
3785 // icon view.
3786 if ( HasFlag(wxLC_ICON) || HasFlag(wxLC_SMALL_ICON) )
4d0c2308 3787 {
08be21f5
VZ
3788 size_t firstRowLine = i - currentlyVisibleLines + 1;
3789 for (size_t j = firstRowLine; j <= i; j++)
3790 {
3791 GetLine(j)->m_gi->ExtendWidth(maxWidthInThisRow);
3792 }
4d0c2308
VZ
3793 }
3794
13602ebd
VZ
3795 currentlyVisibleLines = 0;
3796 y = EXTRA_BORDER_Y;
3797 maxWidthInThisRow += MARGIN_BETWEEN_ROWS;
3798 x += maxWidthInThisRow;
3799 entireWidth += maxWidthInThisRow;
3800 maxWidthInThisRow = 0;
3801 }
3802
13602ebd
VZ
3803 if ( (tries == 0) &&
3804 (entireWidth + SCROLL_UNIT_X > clientWidth) )
3805 {
3806 clientHeight -= wxSystemSettings::
3807 GetMetric(wxSYS_HSCROLL_Y);
3808 m_linesPerPage = 0;
13602ebd
VZ
3809 break;
3810 }
3811
3812 if ( i == count - 1 )
3813 tries = 1; // Everything fits, no second try required.
3814 }
92976ab6 3815 }
bffa1c77 3816
06cd40a8 3817 GetListCtrl()->SetScrollbars
13602ebd
VZ
3818 (
3819 SCROLL_UNIT_X,
6d78bbe6 3820 lineHeight,
13602ebd
VZ
3821 (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X,
3822 0,
06cd40a8 3823 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
13602ebd 3824 0,
ca65c044 3825 true
13602ebd
VZ
3826 );
3827 }
e1e955e1 3828 }
cf1dfa6b 3829
34bbbc27
VZ
3830 if ( !noRefresh )
3831 {
3832 // FIXME: why should we call it from here?
3833 UpdateCurrent();
b54e41c5 3834
34bbbc27
VZ
3835 RefreshAll();
3836 }
b54e41c5
VZ
3837}
3838
3839void wxListMainWindow::RefreshAll()
3840{
ca65c044 3841 m_dirty = false;
b54e41c5
VZ
3842 Refresh();
3843
3844 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
70541533 3845 if ( headerWin && headerWin->m_dirty )
b54e41c5 3846 {
ca65c044 3847 headerWin->m_dirty = false;
b54e41c5
VZ
3848 headerWin->Refresh();
3849 }
e1e955e1 3850}
c801d85f 3851
cf1dfa6b 3852void wxListMainWindow::UpdateCurrent()
c801d85f 3853{
b54e41c5 3854 if ( !HasCurrent() && !IsEmpty() )
0ddefeb0 3855 ChangeCurrent(0);
e1e955e1 3856}
c801d85f 3857
19695fbd
VZ
3858long wxListMainWindow::GetNextItem( long item,
3859 int WXUNUSED(geometry),
62d89eb4 3860 int state ) const
c801d85f 3861{
d1022fd6
VZ
3862 long ret = item,
3863 max = GetItemCount();
3864 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
9a83f860 3865 wxT("invalid listctrl index in GetNextItem()") );
19695fbd
VZ
3866
3867 // notice that we start with the next item (or the first one if item == -1)
3868 // and this is intentional to allow writing a simple loop to iterate over
3869 // all selected items
d1022fd6
VZ
3870 ret++;
3871 if ( ret == max )
277ea1b4
DS
3872 // this is not an error because the index was OK initially,
3873 // just no such item
d1022fd6 3874 return -1;
d1022fd6 3875
cf1dfa6b 3876 if ( !state )
cf1dfa6b
VZ
3877 // any will do
3878 return (size_t)ret;
cf1dfa6b
VZ
3879
3880 size_t count = GetItemCount();
3881 for ( size_t line = (size_t)ret; line < count; line++ )
63852e78 3882 {
cf1dfa6b
VZ
3883 if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
3884 return line;
3885
b54e41c5 3886 if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
cf1dfa6b 3887 return line;
63852e78 3888 }
19695fbd 3889
63852e78 3890 return -1;
e1e955e1 3891}
c801d85f 3892
cf1dfa6b
VZ
3893// ----------------------------------------------------------------------------
3894// deleting stuff
3895// ----------------------------------------------------------------------------
3896
b54e41c5 3897void wxListMainWindow::DeleteItem( long lindex )
c801d85f 3898{
cf1dfa6b
VZ
3899 size_t count = GetItemCount();
3900
b54e41c5 3901 wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
9a83f860 3902 wxT("invalid item index in DeleteItem") );
cf1dfa6b 3903
b54e41c5
VZ
3904 size_t index = (size_t)lindex;
3905
d6ddcd57
VZ
3906 // we don't need to adjust the index for the previous items
3907 if ( HasCurrent() && m_current >= index )
cf1dfa6b 3908 {
d6ddcd57
VZ
3909 // if the current item is being deleted, we want the next one to
3910 // become selected - unless there is no next one - so don't adjust
3911 // m_current in this case
3912 if ( m_current != index || m_current == count - 1 )
d6ddcd57 3913 m_current--;
cf1dfa6b 3914 }
6fef2483 3915
938b652b 3916 if ( InReportView() )
63852e78 3917 {
a95d5751
RR
3918 // mark the Column Max Width cache as dirty if the items in the line
3919 // we're deleting contain the Max Column Width
7d4813a0
VZ
3920 wxListLineData * const line = GetLine(index);
3921 wxListItemDataList::compatibility_iterator n;
3922 wxListItemData *itemData;
3923 wxListItem item;
3924 int itemWidth;
3925
3926 for (size_t i = 0; i < m_columns.GetCount(); i++)
3927 {
3928 n = line->m_items.Item( i );
3929 itemData = n->GetData();
3930 itemData->GetItem(item);
3931
3932 itemWidth = GetItemWidthWithImage(&item);
3933
3934 if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth)
3935 m_aColWidths.Item(i)->bNeedsUpdate = true;
3936 }
3937
6b4a8d93 3938 ResetVisibleLinesRange();
938b652b
VZ
3939 }
3940
a95d5751
RR
3941 SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM, wxDefaultPosition );
3942
938b652b
VZ
3943 if ( IsVirtual() )
3944 {
3945 m_countVirt--;
b54e41c5
VZ
3946 m_selStore.OnItemDelete(index);
3947 }
3948 else
3949 {
3950 m_lines.RemoveAt( index );
63852e78 3951 }
6b4a8d93 3952
70541533 3953 // we need to refresh the (vert) scrollbar as the number of items changed
ca65c044 3954 m_dirty = true;
91c6cc0e 3955
6b4a8d93 3956 RefreshAfter(index);
e1e955e1 3957}
c801d85f 3958
debe6624 3959void wxListMainWindow::DeleteColumn( int col )
c801d85f 3960{
222ed1d6 3961 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
24b9f055
VZ
3962
3963 wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
bd8289c1 3964
ca65c044 3965 m_dirty = true;
222ed1d6
MB
3966 delete node->GetData();
3967 m_columns.Erase( node );
ae409cb8 3968
df6f82f0
VZ
3969 if ( !IsVirtual() )
3970 {
3971 // update all the items
3972 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
3973 {
3974 wxListLineData * const line = GetLine(i);
7451b5c4
VZ
3975
3976 // In the following atypical but possible scenario it can be
3977 // legal to call DeleteColumn() but the items may not have any
3978 // values for it:
3979 // 1. In report view, insert a second column.
3980 // 2. Still in report view, add an item with 2 values.
3981 // 3. Switch to an icon (or list) view.
3982 // 4. Add an item -- necessarily with 1 value only.
3983 // 5. Switch back to report view.
3984 // 6. Call DeleteColumn().
3985 // So we need to check for this as otherwise we would simply crash
3986 // if this happens.
3987 if ( line->m_items.GetCount() <= static_cast<unsigned>(col) )
3988 continue;
3989
222ed1d6
MB
3990 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3991 delete n->GetData();
3992 line->m_items.Erase(n);
df6f82f0
VZ
3993 }
3994 }
3995
7d4813a0
VZ
3996 if ( InReportView() ) // we only cache max widths when in Report View
3997 {
3998 delete m_aColWidths.Item(col);
3999 m_aColWidths.RemoveAt(col);
4000 }
4001
ae409cb8
VZ
4002 // invalidate it as it has to be recalculated
4003 m_headerWidth = 0;
e1e955e1 4004}
c801d85f 4005
5fe143df 4006void wxListMainWindow::DoDeleteAllItems()
c801d85f 4007{
cf1dfa6b 4008 if ( IsEmpty() )
cf1dfa6b
VZ
4009 // nothing to do - in particular, don't send the event
4010 return;
cf1dfa6b 4011
b54e41c5 4012 ResetCurrent();
7c0ea335
VZ
4013
4014 // to make the deletion of all items faster, we don't send the
cf1dfa6b
VZ
4015 // notifications for each item deletion in this case but only one event
4016 // for all of them: this is compatible with wxMSW and documented in
4017 // DeleteAllItems() description
bffa1c77 4018
12c1b46a
RR
4019 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
4020 event.SetEventObject( GetParent() );
4021 GetParent()->GetEventHandler()->ProcessEvent( event );
7c0ea335 4022
b54e41c5
VZ
4023 if ( IsVirtual() )
4024 {
4025 m_countVirt = 0;
850ed6e7 4026 m_selStore.Clear();
b54e41c5
VZ
4027 }
4028
6b4a8d93
VZ
4029 if ( InReportView() )
4030 {
4031 ResetVisibleLinesRange();
7d4813a0
VZ
4032 for (size_t i = 0; i < m_aColWidths.GetCount(); i++)
4033 {
8d36b216 4034 m_aColWidths.Item(i)->bNeedsUpdate = true;
7d4813a0 4035 }
6b4a8d93
VZ
4036 }
4037
5b077d48 4038 m_lines.Clear();
5fe143df 4039}
cf1dfa6b 4040
5fe143df
VZ
4041void wxListMainWindow::DeleteAllItems()
4042{
4043 DoDeleteAllItems();
4044
4045 RecalculatePositions();
e1e955e1 4046}
c801d85f 4047
12c1b46a 4048void wxListMainWindow::DeleteEverything()
c801d85f 4049{
222ed1d6 4050 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
8d36b216 4051 WX_CLEAR_ARRAY(m_aColWidths);
904ccf52
VZ
4052
4053 DeleteAllItems();
e1e955e1 4054}
c801d85f 4055
cf1dfa6b
VZ
4056// ----------------------------------------------------------------------------
4057// scanning for an item
4058// ----------------------------------------------------------------------------
4059
debe6624 4060void wxListMainWindow::EnsureVisible( long index )
c801d85f 4061{
cf1dfa6b 4062 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
9a83f860 4063 wxT("invalid index in EnsureVisible") );
cf1dfa6b
VZ
4064
4065 // We have to call this here because the label in question might just have
34bbbc27
VZ
4066 // been added and its position is not known yet
4067 if ( m_dirty )
ca65c044 4068 RecalculatePositions(true /* no refresh */);
34bbbc27
VZ
4069
4070 MoveToItem((size_t)index);
e1e955e1 4071}
c801d85f 4072
23739465 4073long wxListMainWindow::FindItem(long start, const wxString& str, bool partial )
c801d85f 4074{
23739465
RR
4075 if (str.empty())
4076 return wxNOT_FOUND;
6fef2483 4077
5b077d48 4078 long pos = start;
23739465 4079 wxString str_upper = str.Upper();
cf1dfa6b
VZ
4080 if (pos < 0)
4081 pos = 0;
54442116 4082
cf1dfa6b
VZ
4083 size_t count = GetItemCount();
4084 for ( size_t i = (size_t)pos; i < count; i++ )
4085 {
4086 wxListLineData *line = GetLine(i);
23739465
RR
4087 wxString line_upper = line->GetText(0).Upper();
4088 if (!partial)
4089 {
4090 if (line_upper == str_upper )
4091 return i;
4092 }
4093 else
4094 {
4095 if (line_upper.find(str_upper) == 0)
4096 return i;
4097 }
5b077d48 4098 }
cf1dfa6b
VZ
4099
4100 return wxNOT_FOUND;
e1e955e1 4101}
c801d85f 4102
81ce36aa 4103long wxListMainWindow::FindItem(long start, wxUIntPtr data)
c801d85f 4104{
5b077d48 4105 long pos = start;
cf1dfa6b
VZ
4106 if (pos < 0)
4107 pos = 0;
4108
4109 size_t count = GetItemCount();
4110 for (size_t i = (size_t)pos; i < count; i++)
5b077d48 4111 {
cf1dfa6b 4112 wxListLineData *line = GetLine(i);
5b077d48
RR
4113 wxListItem item;
4114 line->GetItem( 0, item );
cf1dfa6b
VZ
4115 if (item.m_data == data)
4116 return i;
5b077d48 4117 }
cf1dfa6b
VZ
4118
4119 return wxNOT_FOUND;
e1e955e1 4120}
c801d85f 4121
8158e0e1
VZ
4122long wxListMainWindow::FindItem( const wxPoint& pt )
4123{
e6bf9573 4124 size_t topItem;
277ea1b4 4125 GetVisibleLinesRange( &topItem, NULL );
8158e0e1 4126
e6bf9573 4127 wxPoint p;
277ea1b4
DS
4128 GetItemPosition( GetItemCount() - 1, p );
4129 if ( p.y == 0 )
e6bf9573 4130 return topItem;
277ea1b4
DS
4131
4132 long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem );
4133 if ( id >= 0 && id < (long)GetItemCount() )
e6bf9573 4134 return id;
8158e0e1 4135
e6bf9573 4136 return wxNOT_FOUND;
8158e0e1
VZ
4137}
4138
be0e5d69 4139long wxListMainWindow::HitTest( int x, int y, int &flags ) const
c801d85f 4140{
06cd40a8 4141 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
e8741cca 4142
fc4f1d5f
VZ
4143 size_t count = GetItemCount();
4144
b5d43d1d 4145 if ( InReportView() )
c801d85f 4146 {
5cd89174 4147 size_t current = y / GetLineHeight();
fc4f1d5f
VZ
4148 if ( current < count )
4149 {
4150 flags = HitTestLine(current, x, y);
4151 if ( flags )
4152 return current;
4153 }
5cd89174
VZ
4154 }
4155 else // !report
4156 {
4157 // TODO: optimize it too! this is less simple than for report view but
4158 // enumerating all items is still not a way to do it!!
5cd89174 4159 for ( size_t current = 0; current < count; current++ )
5b077d48 4160 {
5cd89174
VZ
4161 flags = HitTestLine(current, x, y);
4162 if ( flags )
4163 return current;
5b077d48 4164 }
e1e955e1 4165 }
cf1dfa6b
VZ
4166
4167 return wxNOT_FOUND;
e1e955e1 4168}
c801d85f 4169
cf1dfa6b
VZ
4170// ----------------------------------------------------------------------------
4171// adding stuff
4172// ----------------------------------------------------------------------------
4173
c801d85f
KB
4174void wxListMainWindow::InsertItem( wxListItem &item )
4175{
9a83f860 4176 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
cf1dfa6b 4177
c7cf7a78 4178 int count = GetItemCount();
9a83f860 4179 wxCHECK_RET( item.m_itemId >= 0, wxT("invalid item index") );
cf1dfa6b 4180
33c956e6
RD
4181 if (item.m_itemId > count)
4182 item.m_itemId = count;
b713f891 4183
cf1dfa6b
VZ
4184 size_t id = item.m_itemId;
4185
ca65c044 4186 m_dirty = true;
cf1dfa6b 4187
b5d43d1d 4188 if ( InReportView() )
2b5f62a0 4189 {
2b5f62a0 4190 ResetVisibleLinesRange();
7d4813a0 4191
e612dec2
VZ
4192 const unsigned col = item.GetColumn();
4193 wxCHECK_RET( col < m_aColWidths.size(), "invalid item column" );
4194
7d4813a0 4195 // calculate the width of the item and adjust the max column width
e612dec2 4196 wxColWidthInfo *pWidthInfo = m_aColWidths.Item(col);
f5c479cc 4197 int width = GetItemWidthWithImage(&item);
7d4813a0
VZ
4198 item.SetWidth(width);
4199 if (width > pWidthInfo->nMaxWidth)
4200 pWidthInfo->nMaxWidth = width;
1370703e 4201 }
004fd0c8 4202
5cd89174 4203 wxListLineData *line = new wxListLineData(this);
004fd0c8 4204
1e6601c7 4205 line->SetItem( item.m_col, item );
7e132bdd 4206 if ( item.m_mask & wxLIST_MASK_IMAGE )
051b7b26
VZ
4207 {
4208 // Reset the buffered height if it's not big enough for the new image.
7e132bdd
VZ
4209 int image = item.GetImage();
4210 if ( m_small_image_list && image != -1 && InReportView() )
051b7b26
VZ
4211 {
4212 int imageWidth, imageHeight;
7e132bdd 4213 m_small_image_list->GetSize(image, imageWidth, imageHeight);
051b7b26
VZ
4214
4215 if ( imageHeight > m_lineHeight )
4216 m_lineHeight = 0;
4217 }
4218 }
cf1dfa6b
VZ
4219
4220 m_lines.Insert( line, id );
5cd89174 4221
ca65c044 4222 m_dirty = true;
a67e6e54 4223
ab96d680
VZ
4224 // If an item is selected at or below the point of insertion, we need to
4225 // increment the member variables because the current row's index has gone
4226 // up by one
4227 if ( HasCurrent() && m_current >= id )
ab96d680 4228 m_current++;
ab96d680 4229
a67e6e54
VZ
4230 SendNotify(id, wxEVT_COMMAND_LIST_INSERT_ITEM);
4231
5cd89174 4232 RefreshLines(id, GetItemCount() - 1);
e1e955e1 4233}
c801d85f 4234
57bf8a6f 4235long wxListMainWindow::InsertColumn( long col, const wxListItem &item )
c801d85f 4236{
57bf8a6f
VZ
4237 long idx = -1;
4238
ca65c044 4239 m_dirty = true;
b5d43d1d 4240 if ( InReportView() )
3db7be80 4241 {
94248d2e 4242 wxListHeaderData *column = new wxListHeaderData( item );
54442116 4243 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
3d167c00 4244 column->SetWidth(ComputeMinHeaderWidth(column));
df6f82f0 4245
7d4813a0
VZ
4246 wxColWidthInfo *colWidthInfo = new wxColWidthInfo();
4247
df6f82f0
VZ
4248 bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4249 if ( insert )
5b077d48 4250 {
7d4813a0
VZ
4251 wxListHeaderDataList::compatibility_iterator
4252 node = m_columns.Item( col );
24b9f055 4253 m_columns.Insert( node, column );
7d4813a0 4254 m_aColWidths.Insert( colWidthInfo, col );
57bf8a6f 4255 idx = col;
5b077d48
RR
4256 }
4257 else
4258 {
57bf8a6f 4259 idx = m_aColWidths.GetCount();
5b077d48 4260 m_columns.Append( column );
7d4813a0 4261 m_aColWidths.Add( colWidthInfo );
5b077d48 4262 }
ae409cb8 4263
df6f82f0
VZ
4264 if ( !IsVirtual() )
4265 {
4266 // update all the items
4267 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4268 {
4269 wxListLineData * const line = GetLine(i);
4270 wxListItemData * const data = new wxListItemData(this);
4271 if ( insert )
4272 line->m_items.Insert(col, data);
4273 else
4274 line->m_items.Append(data);
4275 }
4276 }
4277
ae409cb8
VZ
4278 // invalidate it as it has to be recalculated
4279 m_headerWidth = 0;
3db7be80 4280 }
57bf8a6f 4281 return idx;
e1e955e1 4282}
c801d85f 4283
7d4813a0
VZ
4284int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
4285{
4286 int width = 0;
4287 wxClientDC dc(this);
4288
4289 dc.SetFont( GetFont() );
4290
4291 if (item->GetImage() != -1)
4292 {
4293 int ix, iy;
4294 GetImageSize( item->GetImage(), ix, iy );
4295 width += ix + 5;
4296 }
4297
4298 if (!item->GetText().empty())
4299 {
4300 wxCoord w;
4301 dc.GetTextExtent( item->GetText(), &w, NULL );
4302 width += w;
4303 }
4304
4305 return width;
4306}
4307
cf1dfa6b
VZ
4308// ----------------------------------------------------------------------------
4309// sorting
4310// ----------------------------------------------------------------------------
4311
7d7b3f69 4312static wxListCtrlCompare list_ctrl_compare_func_2;
b18e2046 4313static wxIntPtr list_ctrl_compare_data;
c801d85f 4314
f6bcfd97 4315int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
c801d85f 4316{
f6bcfd97
BP
4317 wxListLineData *line1 = *arg1;
4318 wxListLineData *line2 = *arg2;
5b077d48
RR
4319 wxListItem item;
4320 line1->GetItem( 0, item );
81ce36aa 4321 wxUIntPtr data1 = item.m_data;
5b077d48 4322 line2->GetItem( 0, item );
81ce36aa 4323 wxUIntPtr data2 = item.m_data;
5b077d48 4324 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
e1e955e1 4325}
c801d85f 4326
b18e2046 4327void wxListMainWindow::SortItems( wxListCtrlCompare fn, wxIntPtr data )
c801d85f 4328{
6b06a727
VZ
4329 // selections won't make sense any more after sorting the items so reset
4330 // them
4331 HighlightAll(false);
4332 ResetCurrent();
4333
5b077d48
RR
4334 list_ctrl_compare_func_2 = fn;
4335 list_ctrl_compare_data = data;
4336 m_lines.Sort( list_ctrl_compare_func_1 );
ca65c044 4337 m_dirty = true;
e1e955e1 4338}
c801d85f 4339
cf1dfa6b
VZ
4340// ----------------------------------------------------------------------------
4341// scrolling
4342// ----------------------------------------------------------------------------
4343
7c74e7fe
SC
4344void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4345{
602e3365
RR
4346 // update our idea of which lines are shown when we redraw the window the
4347 // next time
4348 ResetVisibleLinesRange();
a9aead31 4349
cf1dfa6b 4350 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
7c74e7fe 4351 {
3cd94a0d 4352 wxGenericListCtrl* lc = GetListCtrl();
9a83f860 4353 wxCHECK_RET( lc, wxT("no listctrl window?") );
cf1dfa6b 4354
52d2a5e4
FM
4355 if (lc->m_headerWin) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4356 {
4357 lc->m_headerWin->Refresh();
4358 lc->m_headerWin->Update();
4359 }
7c74e7fe 4360 }
cf1dfa6b
VZ
4361}
4362
5cd89174
VZ
4363int wxListMainWindow::GetCountPerPage() const
4364{
4365 if ( !m_linesPerPage )
4366 {
4367 wxConstCast(this, wxListMainWindow)->
4368 m_linesPerPage = GetClientSize().y / GetLineHeight();
4369 }
4370
4371 return m_linesPerPage;
4372}
4373
b54e41c5 4374void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
cf1dfa6b 4375{
9a83f860 4376 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
cf1dfa6b 4377
b54e41c5
VZ
4378 if ( m_lineFrom == (size_t)-1 )
4379 {
b54e41c5 4380 size_t count = GetItemCount();
6522713c
VZ
4381 if ( count )
4382 {
e5d28ed4 4383 m_lineFrom = GetListCtrl()->GetScrollPos(wxVERTICAL);
cf1dfa6b 4384
152c57f2
VZ
4385 // this may happen if SetScrollbars() hadn't been called yet
4386 if ( m_lineFrom >= count )
bcc0da5c 4387 m_lineFrom = count - 1;
cf1dfa6b 4388
6522713c
VZ
4389 // we redraw one extra line but this is needed to make the redrawing
4390 // logic work when there is a fractional number of lines on screen
4391 m_lineTo = m_lineFrom + m_linesPerPage;
4392 if ( m_lineTo >= count )
4393 m_lineTo = count - 1;
4394 }
4395 else // empty control
4396 {
4397 m_lineFrom = 0;
4398 m_lineTo = (size_t)-1;
4399 }
cf1dfa6b 4400 }
b54e41c5 4401
ae167d2f
VZ
4402 wxASSERT_MSG( IsEmpty() ||
4403 (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
9a83f860 4404 wxT("GetVisibleLinesRange() returns incorrect result") );
6b4a8d93 4405
b54e41c5
VZ
4406 if ( from )
4407 *from = m_lineFrom;
4408 if ( to )
4409 *to = m_lineTo;
7c74e7fe
SC
4410}
4411
d34d31f6
VZ
4412size_t
4413wxListMainWindow::PrefixFindItem(size_t idParent,
4414 const wxString& prefixOrig) const
4415{
4416 // if no items then just return
4417 if ( idParent == (size_t)-1 )
4418 return idParent;
4419
4420 // match is case insensitive as this is more convenient to the user: having
4421 // to press Shift-letter to go to the item starting with a capital letter
4422 // would be too bothersome
4423 wxString prefix = prefixOrig.Lower();
4424
4425 // determine the starting point: we shouldn't take the current item (this
4426 // allows to switch between two items starting with the same letter just by
4427 // pressing it) but we shouldn't jump to the next one if the user is
4428 // continuing to type as otherwise he might easily skip the item he wanted
4429 size_t itemid = idParent;
4430 if ( prefix.length() == 1 )
4431 {
4432 itemid += 1;
4433 }
4434
4435 // look for the item starting with the given prefix after it
4436 while ( ( itemid < (size_t)GetItemCount() ) &&
4437 !GetLine(itemid)->GetText(0).Lower().StartsWith(prefix) )
4438 {
4439 itemid += 1;
4440 }
4441
4442 // if we haven't found anything...
4443 if ( !( itemid < (size_t)GetItemCount() ) )
4444 {
4445 // ... wrap to the beginning
4446 itemid = 0;
4447
4448 // and try all the items (stop when we get to the one we started from)
4449 while ( ( itemid < (size_t)GetItemCount() ) && itemid != idParent &&
4450 !GetLine(itemid)->GetText(0).Lower().StartsWith(prefix) )
4451 {
4452 itemid += 1;
4453 }
4454 // If we haven't found the item, id will be (size_t)-1, as per
4455 // documentation
4456 if ( !( itemid < (size_t)GetItemCount() ) ||
4457 ( ( itemid == idParent ) &&
4458 !GetLine(itemid)->GetText(0).Lower().StartsWith(prefix) ) )
4459 {
4460 itemid = (size_t)-1;
4461 }
4462 }
4463
4464 return itemid;
4465}
4466
c801d85f 4467// -------------------------------------------------------------------------------------
3cd94a0d 4468// wxGenericListCtrl
c801d85f
KB
4469// -------------------------------------------------------------------------------------
4470
3cd94a0d
JS
4471IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl, wxControl)
4472
26df5dd3 4473BEGIN_EVENT_TABLE(wxGenericListCtrl,wxListCtrlBase)
3cd94a0d 4474 EVT_SIZE(wxGenericListCtrl::OnSize)
06cd40a8 4475 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll)
c801d85f
KB
4476END_EVENT_TABLE()
4477
06cd40a8 4478void wxGenericListCtrl::Init()
c801d85f 4479{
d3b9f782
VZ
4480 m_imageListNormal = NULL;
4481 m_imageListSmall = NULL;
4482 m_imageListState = NULL;
cf1dfa6b
VZ
4483
4484 m_ownsImageListNormal =
4485 m_ownsImageListSmall =
ca65c044 4486 m_ownsImageListState = false;
cf1dfa6b 4487
d3b9f782
VZ
4488 m_mainWin = NULL;
4489 m_headerWin = NULL;
c801d85f
KB
4490}
4491
3cd94a0d 4492wxGenericListCtrl::~wxGenericListCtrl()
c801d85f 4493{
cf1dfa6b
VZ
4494 if (m_ownsImageListNormal)
4495 delete m_imageListNormal;
4496 if (m_ownsImageListSmall)
4497 delete m_imageListSmall;
4498 if (m_ownsImageListState)
4499 delete m_imageListState;
4500}
4501
06cd40a8 4502void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
f8252483 4503{
06cd40a8
RR
4504 bool needs_header = HasHeader();
4505 bool has_header = (m_headerWin != NULL);
7d7b3f69 4506
06cd40a8
RR
4507 if (needs_header == has_header)
4508 return;
f8252483 4509
06cd40a8
RR
4510 if (needs_header)
4511 {
4512 m_headerWin = new wxListHeaderWindow
cf1dfa6b 4513 (
ca65c044 4514 this, wxID_ANY, m_mainWin,
c47addef 4515 wxPoint(0,0),
c1fae0d0
VZ
4516 wxSize
4517 (
4518 GetClientSize().x,
4519 wxRendererNative::Get().GetHeaderButtonHeight(this)
4520 ),
cf1dfa6b
VZ
4521 wxTAB_TRAVERSAL
4522 );
7d7b3f69 4523
f1c40652 4524#if defined( __WXMAC__ )
7eb8aeb8 4525 static wxFont font( wxOSX_SYSTEM_FONT_SMALL );
06cd40a8
RR
4526 m_headerWin->SetFont( font );
4527#endif
7d7b3f69 4528
06cd40a8
RR
4529 GetSizer()->Prepend( m_headerWin, 0, wxGROW );
4530 }
4531 else
4532 {
4533 GetSizer()->Detach( m_headerWin );
7d7b3f69 4534
5276b0a5 4535 wxDELETE(m_headerWin);
06cd40a8 4536 }
c801d85f
KB
4537}
4538
3cd94a0d 4539bool wxGenericListCtrl::Create(wxWindow *parent,
25e3a937
VZ
4540 wxWindowID id,
4541 const wxPoint &pos,
4542 const wxSize &size,
4543 long style,
25e3a937 4544 const wxValidator &validator,
25e3a937 4545 const wxString &name)
c801d85f 4546{
06cd40a8 4547 Init();
3fc93ebd 4548
c615d649 4549 // just like in other ports, an assert will fail if the user doesn't give any type style:
5ac526c4 4550 wxASSERT_MSG( (style & wxLC_MASK_TYPE),
9a83f860 4551 wxT("wxListCtrl style should have exactly one mode bit set") );
f6bcfd97 4552
26df5dd3
VZ
4553 if ( !wxListCtrlBase::Create( parent, id, pos, size,
4554 style | wxVSCROLL | wxHSCROLL,
4555 validator, name ) )
ca65c044 4556 return false;
f6bcfd97 4557
1c0a0736 4558 m_mainWin = new wxListMainWindow(this, wxID_ANY, wxPoint(0, 0), size);
bd8289c1 4559
06cd40a8 4560 SetTargetWindow( m_mainWin );
7d7b3f69 4561
d6a658ff
VZ
4562 // We use the cursor keys for moving the selection, not scrolling, so call
4563 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4564 // keyboard events forwarded to us from wxListMainWindow.
4565 DisableKeyboardScrolling();
4566
06cd40a8
RR
4567 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
4568 sizer->Add( m_mainWin, 1, wxGROW );
4569 SetSizer( sizer );
7d7b3f69 4570
06cd40a8
RR
4571 CreateOrDestroyHeaderWindowAsNeeded();
4572
4573 SetInitialSize(size);
4574
4575 return true;
4576}
4577
4578wxBorder wxGenericListCtrl::GetDefaultBorder() const
4579{
4580 return wxBORDER_THEME;
4581}
4582
5d55031c 4583#if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
06cd40a8
RR
4584WXLRESULT wxGenericListCtrl::MSWWindowProc(WXUINT nMsg,
4585 WXWPARAM wParam,
4586 WXLPARAM lParam)
4587{
26df5dd3 4588 WXLRESULT rc = wxListCtrlBase::MSWWindowProc(nMsg, wParam, lParam);
06cd40a8 4589
06cd40a8
RR
4590 // we need to process arrows ourselves for scrolling
4591 if ( nMsg == WM_GETDLGCODE )
309e4c14 4592 {
06cd40a8 4593 rc |= DLGC_WANTARROWS;
309e4c14 4594 }
277ea1b4 4595
06cd40a8
RR
4596 return rc;
4597}
5d55031c 4598#endif // __WXMSW__
277ea1b4 4599
06cd40a8
RR
4600wxSize wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
4601{
4602 wxSize newsize = size;
4603 if (m_headerWin)
4604 newsize.y -= m_headerWin->GetSize().y;
bd8289c1 4605
06cd40a8
RR
4606 return newsize;
4607}
ca65c044 4608
06cd40a8
RR
4609void wxGenericListCtrl::OnScroll(wxScrollWinEvent& event)
4610{
4611 // update our idea of which lines are shown when we redraw
4612 // the window the next time
4613 m_mainWin->ResetVisibleLinesRange();
4614
06cd40a8
RR
4615 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4616 {
4617 m_headerWin->Refresh();
4618 m_headerWin->Update();
4619 }
a28882ab
VZ
4620
4621 // Let the window be scrolled as usual by the default handler.
4622 event.Skip();
e1e955e1 4623}
c801d85f 4624
3cd94a0d 4625void wxGenericListCtrl::SetSingleStyle( long style, bool add )
c801d85f 4626{
b54e41c5 4627 wxASSERT_MSG( !(style & wxLC_VIRTUAL),
9a83f860 4628 wxT("wxLC_VIRTUAL can't be [un]set") );
b54e41c5 4629
f03fc89f 4630 long flag = GetWindowStyle();
bd8289c1 4631
5b077d48
RR
4632 if (add)
4633 {
cf1dfa6b 4634 if (style & wxLC_MASK_TYPE)
b54e41c5 4635 flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
cf1dfa6b
VZ
4636 if (style & wxLC_MASK_ALIGN)
4637 flag &= ~wxLC_MASK_ALIGN;
4638 if (style & wxLC_MASK_SORT)
4639 flag &= ~wxLC_MASK_SORT;
5b077d48 4640 }
c801d85f 4641
5b077d48 4642 if (add)
5b077d48 4643 flag |= style;
5b077d48 4644 else
cf1dfa6b 4645 flag &= ~style;
bd8289c1 4646
b119ee87
VZ
4647 // some styles can be set without recreating everything (as happens in
4648 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4649 if ( !(style & ~(wxLC_HRULES | wxLC_VRULES)) )
4650 {
4651 Refresh();
4652 wxWindow::SetWindowStyleFlag(flag);
4653 }
4654 else
4655 {
4656 SetWindowStyleFlag( flag );
4657 }
e1e955e1 4658}
c801d85f 4659
3cd94a0d 4660void wxGenericListCtrl::SetWindowStyleFlag( long flag )
c801d85f 4661{
e61fedab
VZ
4662 // we add wxHSCROLL and wxVSCROLL in ctor unconditionally and it never
4663 // makes sense to remove them as we'll always add scrollbars anyhow when
4664 // needed
4665 flag |= wxHSCROLL | wxVSCROLL;
4666
476de5ea
VZ
4667 const bool wasInReportView = HasFlag(wxLC_REPORT);
4668
1bab3627
VZ
4669 // update the window style first so that the header is created or destroyed
4670 // corresponding to the new style
4671 wxWindow::SetWindowStyleFlag( flag );
4672
121a3581
RR
4673 if (m_mainWin)
4674 {
476de5ea
VZ
4675 const bool inReportView = (flag & wxLC_REPORT) != 0;
4676 if ( inReportView != wasInReportView )
4677 {
4678 // we need to notify the main window about this change as it must
4679 // update its data structures
4680 m_mainWin->SetReportView(inReportView);
4681 }
4682
06cd40a8 4683 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
a95cdab8 4684
06cd40a8 4685 CreateOrDestroyHeaderWindowAsNeeded();
7d7b3f69 4686
06cd40a8 4687 GetSizer()->Layout();
e1e955e1 4688 }
e1e955e1 4689}
c801d85f 4690
3cd94a0d 4691bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
c801d85f 4692{
5b077d48 4693 m_mainWin->GetColumn( col, item );
ca65c044 4694 return true;
e1e955e1 4695}
c801d85f 4696
94248d2e 4697bool wxGenericListCtrl::SetColumn( int col, const wxListItem& item )
c801d85f 4698{
5b077d48 4699 m_mainWin->SetColumn( col, item );
ca65c044 4700 return true;
e1e955e1 4701}
c801d85f 4702
3cd94a0d 4703int wxGenericListCtrl::GetColumnWidth( int col ) const
c801d85f 4704{
5b077d48 4705 return m_mainWin->GetColumnWidth( col );
e1e955e1 4706}
c801d85f 4707
3cd94a0d 4708bool wxGenericListCtrl::SetColumnWidth( int col, int width )
c801d85f 4709{
5b077d48 4710 m_mainWin->SetColumnWidth( col, width );
ca65c044 4711 return true;
e1e955e1 4712}
c801d85f 4713
3cd94a0d 4714int wxGenericListCtrl::GetCountPerPage() const
c801d85f
KB
4715{
4716 return m_mainWin->GetCountPerPage(); // different from Windows ?
e1e955e1 4717}
c801d85f 4718
3cd94a0d 4719bool wxGenericListCtrl::GetItem( wxListItem &info ) const
c801d85f 4720{
5b077d48 4721 m_mainWin->GetItem( info );
ca65c044 4722 return true;
e1e955e1 4723}
c801d85f 4724
3cd94a0d 4725bool wxGenericListCtrl::SetItem( wxListItem &info )
c801d85f 4726{
5b077d48 4727 m_mainWin->SetItem( info );
ca65c044 4728 return true;
e1e955e1 4729}
c801d85f 4730
3cd94a0d 4731long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
c801d85f 4732{
5b077d48
RR
4733 wxListItem info;
4734 info.m_text = label;
4735 info.m_mask = wxLIST_MASK_TEXT;
4736 info.m_itemId = index;
4737 info.m_col = col;
4738 if ( imageId > -1 )
4739 {
4740 info.m_image = imageId;
4741 info.m_mask |= wxLIST_MASK_IMAGE;
277ea1b4
DS
4742 }
4743
5b077d48 4744 m_mainWin->SetItem(info);
ca65c044 4745 return true;
e1e955e1 4746}
c801d85f 4747
3cd94a0d 4748int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
c801d85f 4749{
5b077d48 4750 return m_mainWin->GetItemState( item, stateMask );
e1e955e1 4751}
c801d85f 4752
3cd94a0d 4753bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
c801d85f 4754{
5b077d48 4755 m_mainWin->SetItemState( item, state, stateMask );
ca65c044 4756 return true;
e1e955e1 4757}
c801d85f 4758
aba3d35e
VZ
4759bool
4760wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
06db67bc
RD
4761{
4762 return SetItemColumnImage(item, 0, image);
4763}
4764
4765bool
4766wxGenericListCtrl::SetItemColumnImage( long item, long column, int image )
c3627a00
JS
4767{
4768 wxListItem info;
4769 info.m_image = image;
4770 info.m_mask = wxLIST_MASK_IMAGE;
4771 info.m_itemId = item;
06db67bc 4772 info.m_col = column;
c3627a00
JS
4773 m_mainWin->SetItem( info );
4774 return true;
4775}
c801d85f 4776
b6812a6f 4777wxString wxGenericListCtrl::GetItemText( long item, int col ) const
c801d85f 4778{
b6812a6f 4779 return m_mainWin->GetItemText(item, col);
e1e955e1 4780}
c801d85f 4781
3cd94a0d 4782void wxGenericListCtrl::SetItemText( long item, const wxString& str )
c801d85f 4783{
62d89eb4 4784 m_mainWin->SetItemText(item, str);
e1e955e1 4785}
c801d85f 4786
81ce36aa 4787wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const
c801d85f 4788{
5b077d48 4789 wxListItem info;
39a7f085 4790 info.m_mask = wxLIST_MASK_DATA;
5b077d48
RR
4791 info.m_itemId = item;
4792 m_mainWin->GetItem( info );
4793 return info.m_data;
e1e955e1 4794}
c801d85f 4795
9fcd0bf7 4796bool wxGenericListCtrl::SetItemPtrData( long item, wxUIntPtr data )
c801d85f 4797{
5b077d48
RR
4798 wxListItem info;
4799 info.m_mask = wxLIST_MASK_DATA;
4800 info.m_itemId = item;
4801 info.m_data = data;
4802 m_mainWin->SetItem( info );
ca65c044 4803 return true;
e1e955e1 4804}
c801d85f 4805
11ebea16
VZ
4806wxRect wxGenericListCtrl::GetViewRect() const
4807{
4808 return m_mainWin->GetViewRect();
4809}
4810
e974c5d2
VZ
4811bool wxGenericListCtrl::GetItemRect(long item, wxRect& rect, int code) const
4812{
4813 return GetSubItemRect(item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code);
4814}
4815
4816bool wxGenericListCtrl::GetSubItemRect(long item,
4817 long subItem,
4818 wxRect& rect,
4819 int WXUNUSED(code)) const
c801d85f 4820{
e974c5d2
VZ
4821 if ( !m_mainWin->GetSubItemRect( item, subItem, rect ) )
4822 return false;
4823
7e6874c0 4824 if ( m_mainWin->HasHeader() )
c1fae0d0 4825 rect.y += m_headerWin->GetSize().y + 1;
e974c5d2 4826
ca65c044 4827 return true;
e1e955e1 4828}
c801d85f 4829
3cd94a0d 4830bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
c801d85f 4831{
5b077d48 4832 m_mainWin->GetItemPosition( item, pos );
ca65c044 4833 return true;
e1e955e1 4834}
c801d85f 4835
3cd94a0d 4836bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
c801d85f 4837{
e974c5d2 4838 return false;
e1e955e1 4839}
c801d85f 4840
3cd94a0d 4841int wxGenericListCtrl::GetItemCount() const
c801d85f 4842{
5b077d48 4843 return m_mainWin->GetItemCount();
e1e955e1 4844}
c801d85f 4845
3cd94a0d 4846int wxGenericListCtrl::GetColumnCount() const
92976ab6 4847{
5b077d48 4848 return m_mainWin->GetColumnCount();
92976ab6
RR
4849}
4850
3cd94a0d 4851void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
33d0b396 4852{
5b077d48 4853 m_mainWin->SetItemSpacing( spacing, isSmall );
e1e955e1 4854}
33d0b396 4855
5db8d758
VZ
4856wxSize wxGenericListCtrl::GetItemSpacing() const
4857{
f58fc140 4858 const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON));
5db8d758
VZ
4859
4860 return wxSize(spacing, spacing);
4861}
4862
82d492f1 4863#if WXWIN_COMPATIBILITY_2_6
3cd94a0d 4864int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const
c801d85f 4865{
5b077d48 4866 return m_mainWin->GetItemSpacing( isSmall );
e1e955e1 4867}
82d492f1 4868#endif // WXWIN_COMPATIBILITY_2_6
c801d85f 4869
3cd94a0d 4870void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
5b98eb2f
RL
4871{
4872 wxListItem info;
4873 info.m_itemId = item;
4874 info.SetTextColour( col );
4875 m_mainWin->SetItem( info );
4876}
4877
3cd94a0d 4878wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
5b98eb2f
RL
4879{
4880 wxListItem info;
4881 info.m_itemId = item;
4882 m_mainWin->GetItem( info );
4883 return info.GetTextColour();
4884}
4885
3cd94a0d 4886void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
5b98eb2f
RL
4887{
4888 wxListItem info;
4889 info.m_itemId = item;
4890 info.SetBackgroundColour( col );
4891 m_mainWin->SetItem( info );
4892}
4893
3cd94a0d 4894wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
5b98eb2f
RL
4895{
4896 wxListItem info;
4897 info.m_itemId = item;
4898 m_mainWin->GetItem( info );
4899 return info.GetBackgroundColour();
4900}
4901
35c2acd4
MW
4902void wxGenericListCtrl::SetItemFont( long item, const wxFont &f )
4903{
4904 wxListItem info;
4905 info.m_itemId = item;
4906 info.SetFont( f );
4907 m_mainWin->SetItem( info );
4908}
4909
4910wxFont wxGenericListCtrl::GetItemFont( long item ) const
4911{
4912 wxListItem info;
4913 info.m_itemId = item;
4914 m_mainWin->GetItem( info );
4915 return info.GetFont();
4916}
4917
3cd94a0d 4918int wxGenericListCtrl::GetSelectedItemCount() const
c801d85f 4919{
5b077d48 4920 return m_mainWin->GetSelectedItemCount();
e1e955e1 4921}
c801d85f 4922
3cd94a0d 4923wxColour wxGenericListCtrl::GetTextColour() const
c801d85f 4924{
0530737d 4925 return GetForegroundColour();
e1e955e1 4926}
c801d85f 4927
3cd94a0d 4928void wxGenericListCtrl::SetTextColour(const wxColour& col)
c801d85f 4929{
0530737d 4930 SetForegroundColour(col);
e1e955e1 4931}
c801d85f 4932
3cd94a0d 4933long wxGenericListCtrl::GetTopItem() const
c801d85f 4934{
2b5f62a0
VZ
4935 size_t top;
4936 m_mainWin->GetVisibleLinesRange(&top, NULL);
4937 return (long)top;
e1e955e1 4938}
c801d85f 4939
3cd94a0d 4940long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
c801d85f 4941{
5b077d48 4942 return m_mainWin->GetNextItem( item, geom, state );
e1e955e1 4943}
c801d85f 4944
8a3e173a 4945wxImageList *wxGenericListCtrl::GetImageList(int which) const
c801d85f 4946{
5b077d48 4947 if (which == wxIMAGE_LIST_NORMAL)
5b077d48 4948 return m_imageListNormal;
5b077d48 4949 else if (which == wxIMAGE_LIST_SMALL)
5b077d48 4950 return m_imageListSmall;
5b077d48 4951 else if (which == wxIMAGE_LIST_STATE)
5b077d48 4952 return m_imageListState;
277ea1b4 4953
d3b9f782 4954 return NULL;
e1e955e1 4955}
c801d85f 4956
8a3e173a 4957void wxGenericListCtrl::SetImageList( wxImageList *imageList, int which )
c801d85f 4958{
2e12c11a
VS
4959 if ( which == wxIMAGE_LIST_NORMAL )
4960 {
277ea1b4
DS
4961 if (m_ownsImageListNormal)
4962 delete m_imageListNormal;
2e12c11a 4963 m_imageListNormal = imageList;
ca65c044 4964 m_ownsImageListNormal = false;
2e12c11a
VS
4965 }
4966 else if ( which == wxIMAGE_LIST_SMALL )
4967 {
277ea1b4
DS
4968 if (m_ownsImageListSmall)
4969 delete m_imageListSmall;
2e12c11a 4970 m_imageListSmall = imageList;
ca65c044 4971 m_ownsImageListSmall = false;
2e12c11a
VS
4972 }
4973 else if ( which == wxIMAGE_LIST_STATE )
4974 {
277ea1b4
DS
4975 if (m_ownsImageListState)
4976 delete m_imageListState;
2e12c11a 4977 m_imageListState = imageList;
ca65c044 4978 m_ownsImageListState = false;
2e12c11a
VS
4979 }
4980
5b077d48 4981 m_mainWin->SetImageList( imageList, which );
e1e955e1 4982}
c801d85f 4983
8a3e173a 4984void wxGenericListCtrl::AssignImageList(wxImageList *imageList, int which)
2e12c11a
VS
4985{
4986 SetImageList(imageList, which);
4987 if ( which == wxIMAGE_LIST_NORMAL )
ca65c044 4988 m_ownsImageListNormal = true;
2e12c11a 4989 else if ( which == wxIMAGE_LIST_SMALL )
ca65c044 4990 m_ownsImageListSmall = true;
2e12c11a 4991 else if ( which == wxIMAGE_LIST_STATE )
ca65c044 4992 m_ownsImageListState = true;
2e12c11a
VS
4993}
4994
3cd94a0d 4995bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
c801d85f 4996{
5b077d48 4997 return 0;
e1e955e1 4998}
c801d85f 4999
3cd94a0d 5000bool wxGenericListCtrl::DeleteItem( long item )
c801d85f 5001{
5b077d48 5002 m_mainWin->DeleteItem( item );
ca65c044 5003 return true;
e1e955e1 5004}
c801d85f 5005
3cd94a0d 5006bool wxGenericListCtrl::DeleteAllItems()
c801d85f 5007{
5b077d48 5008 m_mainWin->DeleteAllItems();
ca65c044 5009 return true;
e1e955e1 5010}
c801d85f 5011
3cd94a0d 5012bool wxGenericListCtrl::DeleteAllColumns()
bd8289c1 5013{
24b9f055
VZ
5014 size_t count = m_mainWin->m_columns.GetCount();
5015 for ( size_t n = 0; n < count; n++ )
277ea1b4 5016 DeleteColumn( 0 );
ca65c044 5017 return true;
4f22cf8d
RR
5018}
5019
3cd94a0d 5020void wxGenericListCtrl::ClearAll()
4f22cf8d 5021{
5b077d48 5022 m_mainWin->DeleteEverything();
bd8289c1
VZ
5023}
5024
3cd94a0d 5025bool wxGenericListCtrl::DeleteColumn( int col )
c801d85f 5026{
5b077d48 5027 m_mainWin->DeleteColumn( col );
40c08808
VZ
5028
5029 // if we don't have the header any longer, we need to relayout the window
06cd40a8 5030 // if ( !GetColumnCount() )
7d7b3f69 5031
53943b06
VZ
5032
5033 // Ensure that the non-existent columns are really removed from display.
5034 Refresh();
5035
ca65c044 5036 return true;
e1e955e1 5037}
c801d85f 5038
26e8da41
VZ
5039wxTextCtrl *wxGenericListCtrl::EditLabel(long item,
5040 wxClassInfo* textControlClass)
5041{
5042 return m_mainWin->EditLabel( item, textControlClass );
5043}
5044
5045wxTextCtrl *wxGenericListCtrl::GetEditControl() const
c801d85f 5046{
26e8da41 5047 return m_mainWin->GetEditControl();
e1e955e1 5048}
c801d85f 5049
3cd94a0d 5050bool wxGenericListCtrl::EnsureVisible( long item )
c801d85f 5051{
5b077d48 5052 m_mainWin->EnsureVisible( item );
ca65c044 5053 return true;
e1e955e1 5054}
c801d85f 5055
277ea1b4 5056long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
c801d85f 5057{
5b077d48 5058 return m_mainWin->FindItem( start, str, partial );
e1e955e1 5059}
c801d85f 5060
81ce36aa 5061long wxGenericListCtrl::FindItem( long start, wxUIntPtr data )
c801d85f 5062{
5b077d48 5063 return m_mainWin->FindItem( start, data );
e1e955e1 5064}
c801d85f 5065
8158e0e1 5066long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt,
debe6624 5067 int WXUNUSED(direction))
c801d85f 5068{
8158e0e1 5069 return m_mainWin->FindItem( pt );
e1e955e1 5070}
c801d85f 5071
164a7972 5072// TODO: sub item hit testing
be0e5d69 5073long wxGenericListCtrl::HitTest(const wxPoint& point, int& flags, long *) const
c801d85f 5074{
5b077d48 5075 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
e1e955e1 5076}
c801d85f 5077
3cd94a0d 5078long wxGenericListCtrl::InsertItem( wxListItem& info )
c801d85f 5079{
5b077d48 5080 m_mainWin->InsertItem( info );
2ebcd5f5 5081 return info.m_itemId;
e1e955e1 5082}
c801d85f 5083
3cd94a0d 5084long wxGenericListCtrl::InsertItem( long index, const wxString &label )
c801d85f 5085{
51cc4dad
RR
5086 wxListItem info;
5087 info.m_text = label;
5088 info.m_mask = wxLIST_MASK_TEXT;
5089 info.m_itemId = index;
5090 return InsertItem( info );
e1e955e1 5091}
c801d85f 5092
3cd94a0d 5093long wxGenericListCtrl::InsertItem( long index, int imageIndex )
c801d85f 5094{
51cc4dad
RR
5095 wxListItem info;
5096 info.m_mask = wxLIST_MASK_IMAGE;
5097 info.m_image = imageIndex;
5098 info.m_itemId = index;
5099 return InsertItem( info );
e1e955e1 5100}
c801d85f 5101
3cd94a0d 5102long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
c801d85f 5103{
51cc4dad
RR
5104 wxListItem info;
5105 info.m_text = label;
5106 info.m_image = imageIndex;
ec3f5752
RD
5107 info.m_mask = wxLIST_MASK_TEXT;
5108 if (imageIndex > -1)
5109 info.m_mask |= wxLIST_MASK_IMAGE;
51cc4dad
RR
5110 info.m_itemId = index;
5111 return InsertItem( info );
e1e955e1 5112}
c801d85f 5113
94248d2e 5114long wxGenericListCtrl::DoInsertColumn( long col, const wxListItem &item )
c801d85f 5115{
9a83f860 5116 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
40c08808 5117
57bf8a6f 5118 long idx = m_mainWin->InsertColumn( col, item );
40c08808 5119
52d2a5e4
FM
5120 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
5121 // still have m_headerWin==NULL
5122 if (m_headerWin)
5123 m_headerWin->Refresh();
25e3a937 5124
57bf8a6f 5125 return idx;
e1e955e1 5126}
c801d85f 5127
66a9201d 5128bool wxGenericListCtrl::ScrollList( int dx, int dy )
c801d85f 5129{
66a9201d 5130 return m_mainWin->ScrollList(dx, dy);
e1e955e1 5131}
c801d85f
KB
5132
5133// Sort items.
5134// fn is a function which takes 3 long arguments: item1, item2, data.
5135// item1 is the long data associated with a first item (NOT the index).
5136// item2 is the long data associated with a second item (NOT the index).
5137// data is the same value as passed to SortItems.
5138// The return value is a negative number if the first item should precede the second
5139// item, a positive number of the second item should precede the first,
5140// or zero if the two items are equivalent.
5141// data is arbitrary data to be passed to the sort function.
5142
b18e2046 5143bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn, wxIntPtr data )
c801d85f 5144{
51cc4dad 5145 m_mainWin->SortItems( fn, data );
ca65c044 5146 return true;
e1e955e1 5147}
c801d85f 5148
cf1dfa6b 5149// ----------------------------------------------------------------------------
b54e41c5 5150// event handlers
cf1dfa6b
VZ
5151// ----------------------------------------------------------------------------
5152
3cd94a0d 5153void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
53010e52 5154{
06cd40a8 5155 if (!m_mainWin) return;
53010e52 5156
06cd40a8
RR
5157 // We need to override OnSize so that our scrolled
5158 // window a) does call Layout() to use sizers for
5159 // positioning the controls but b) does not query
5160 // the sizer for their size and use that for setting
5161 // the scrollable area as set that ourselves by
5162 // calling SetScrollbar() further down.
a95cdab8 5163
06cd40a8 5164 Layout();
bd8289c1 5165
06cd40a8 5166 m_mainWin->RecalculatePositions();
7d7b3f69 5167
06cd40a8 5168 AdjustScrollbars();
b54e41c5 5169}
cf1dfa6b 5170
5180055b 5171void wxGenericListCtrl::OnInternalIdle()
b54e41c5 5172{
5180055b 5173 wxWindow::OnInternalIdle();
86351e4a 5174
06cd40a8
RR
5175 if (m_mainWin->m_dirty)
5176 m_mainWin->RecalculatePositions();
e1e955e1 5177}
53010e52 5178
cf1dfa6b
VZ
5179// ----------------------------------------------------------------------------
5180// font/colours
5181// ----------------------------------------------------------------------------
5182
3cd94a0d 5183bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
bd8289c1 5184{
51cc4dad
RR
5185 if (m_mainWin)
5186 {
5187 m_mainWin->SetBackgroundColour( colour );
ca65c044 5188 m_mainWin->m_dirty = true;
51cc4dad 5189 }
004fd0c8 5190
ca65c044 5191 return true;
e4d06860
RR
5192}
5193
3cd94a0d 5194bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
bd8289c1 5195{
f03fc89f 5196 if ( !wxWindow::SetForegroundColour( colour ) )
ca65c044 5197 return false;
004fd0c8 5198
51cc4dad
RR
5199 if (m_mainWin)
5200 {
5201 m_mainWin->SetForegroundColour( colour );
ca65c044 5202 m_mainWin->m_dirty = true;
51cc4dad 5203 }
004fd0c8 5204
51cc4dad 5205 if (m_headerWin)
51cc4dad 5206 m_headerWin->SetForegroundColour( colour );
f03fc89f 5207
ca65c044 5208 return true;
e4d06860 5209}
bd8289c1 5210
3cd94a0d 5211bool wxGenericListCtrl::SetFont( const wxFont &font )
bd8289c1 5212{
f03fc89f 5213 if ( !wxWindow::SetFont( font ) )
ca65c044 5214 return false;
004fd0c8 5215
51cc4dad
RR
5216 if (m_mainWin)
5217 {
5218 m_mainWin->SetFont( font );
ca65c044 5219 m_mainWin->m_dirty = true;
51cc4dad 5220 }
004fd0c8 5221
51cc4dad
RR
5222 if (m_headerWin)
5223 {
5224 m_headerWin->SetFont( font );
06cd40a8 5225 // CalculateAndSetHeaderHeight();
51cc4dad 5226 }
f03fc89f 5227
f8252483
JS
5228 Refresh();
5229
ca65c044 5230 return true;
e4d06860 5231}
c801d85f 5232
277ea1b4 5233// static
35d4c967
RD
5234wxVisualAttributes
5235wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
5236{
5237#if _USE_VISATTR
5238 // Use the same color scheme as wxListBox
5239 return wxListBox::GetClassDefaultAttributes(variant);
5240#else
e7045784 5241 wxUnusedVar(variant);
35d4c967 5242 wxVisualAttributes attr;
9f2968ad 5243 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
35d4c967
RD
5244 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
5245 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
5246 return attr;
5247#endif
5248}
5249
cf1dfa6b
VZ
5250// ----------------------------------------------------------------------------
5251// methods forwarded to m_mainWin
5252// ----------------------------------------------------------------------------
5253
efbb7287
VZ
5254#if wxUSE_DRAG_AND_DROP
5255
3cd94a0d 5256void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
efbb7287
VZ
5257{
5258 m_mainWin->SetDropTarget( dropTarget );
5259}
5260
3cd94a0d 5261wxDropTarget *wxGenericListCtrl::GetDropTarget() const
efbb7287
VZ
5262{
5263 return m_mainWin->GetDropTarget();
5264}
5265
277ea1b4 5266#endif
efbb7287 5267
3cd94a0d 5268bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
efbb7287 5269{
ca65c044 5270 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false;
efbb7287
VZ
5271}
5272
3cd94a0d 5273wxColour wxGenericListCtrl::GetBackgroundColour() const
efbb7287
VZ
5274{
5275 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5276}
5277
3cd94a0d 5278wxColour wxGenericListCtrl::GetForegroundColour() const
efbb7287
VZ
5279{
5280 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5281}
5282
3cd94a0d 5283bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
efbb7287 5284{
3a8c693a 5285#if wxUSE_MENUS
efbb7287 5286 return m_mainWin->PopupMenu( menu, x, y );
3a8c693a 5287#else
ca65c044 5288 return false;
277ea1b4 5289#endif
efbb7287
VZ
5290}
5291
7447bdd7
VZ
5292void wxGenericListCtrl::DoClientToScreen( int *x, int *y ) const
5293{
a9544d00
VZ
5294 // It's not clear whether this can be called before m_mainWin is created
5295 // but it seems better to be on the safe side and check.
5296 if ( m_mainWin )
5297 m_mainWin->DoClientToScreen(x, y);
5298 else
26df5dd3 5299 wxListCtrlBase::DoClientToScreen(x, y);
7447bdd7
VZ
5300}
5301
5302void wxGenericListCtrl::DoScreenToClient( int *x, int *y ) const
5303{
a9544d00
VZ
5304 // At least in wxGTK/Univ build this method can be called before m_mainWin
5305 // is created so avoid crashes in this case.
5306 if ( m_mainWin )
5307 m_mainWin->DoScreenToClient(x, y);
5308 else
26df5dd3 5309 wxListCtrlBase::DoScreenToClient(x, y);
7447bdd7
VZ
5310}
5311
3dea8ba7 5312wxSize wxGenericListCtrl::DoGetBestClientSize() const
3872d96d 5313{
902b679a
VZ
5314 // The base class version can compute the best size in report view only.
5315 wxSize sizeBest = wxListCtrlBase::DoGetBestClientSize();
3dea8ba7
VZ
5316
5317 if ( !InReportView() )
5318 {
5319 // Ensure that our minimal width is at least big enough to show all our
5320 // items. This is important for wxListbook to size itself correctly.
5321
5322 // Remember the offset of the first item: this corresponds to the
5323 // margins around the item so we will add it to the minimal size below
5324 // to ensure that we have equal margins on all sides.
5325 wxPoint ofs;
5326
5327 // We can iterate over all items as there shouldn't be too many of them
5328 // in non-report view. If it ever becomes a problem, we could examine
5329 // just the first few items probably, the determination of the best
5330 // size is less important if we will need scrollbars anyhow.
5331 for ( int n = 0; n < GetItemCount(); n++ )
5332 {
5333 const wxRect itemRect = m_mainWin->GetLineRect(n);
5334 if ( !n )
5335 {
5336 // Remember the position of the first item as all the rest are
5337 // offset by at least this number of pixels too.
5338 ofs = itemRect.GetPosition();
5339 }
5340
5341 sizeBest.IncTo(itemRect.GetSize());
5342 }
5343
5344 sizeBest.IncBy(2*ofs);
5345
5346
5347 // If we have the scrollbars we need to account for them too. And to
5348 // make sure the scrollbars status is up to date we need to call this
5349 // function to set them.
5350 m_mainWin->RecalculatePositions(true /* no refresh */);
5351
5352 // Unfortunately we can't use wxWindow::HasScrollbar() here as we need
5353 // to use m_mainWin client/virtual size for determination of whether we
5354 // use scrollbars and not the size of this window itself. Maybe that
5355 // function should be extended to work correctly in the case when our
5356 // scrollbars manage a different window from this one but currently it
5357 // doesn't work.
5358 const wxSize sizeClient = m_mainWin->GetClientSize();
5359 const wxSize sizeVirt = m_mainWin->GetVirtualSize();
5360
5361 if ( sizeVirt.x > sizeClient.x /* HasScrollbar(wxHORIZONTAL) */ )
5362 sizeBest.y += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
5363
5364 if ( sizeVirt.y > sizeClient.y /* HasScrollbar(wxVERTICAL) */ )
5365 sizeBest.x += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
5366 }
5367
5368 return sizeBest;
3872d96d
RD
5369}
5370
2c1f73ee
VZ
5371// ----------------------------------------------------------------------------
5372// virtual list control support
5373// ----------------------------------------------------------------------------
5374
3cd94a0d 5375wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2c1f73ee
VZ
5376{
5377 // this is a pure virtual function, in fact - which is not really pure
5378 // because the controls which are not virtual don't need to implement it
9a83f860 5379 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
2c1f73ee
VZ
5380
5381 return wxEmptyString;
5382}
5383
3cd94a0d 5384int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2c1f73ee 5385{
611a725e
RD
5386 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
5387 -1,
208458a7 5388 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
2c1f73ee
VZ
5389 return -1;
5390}
5391
208458a7
RD
5392int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const
5393{
5394 if (!column)
5395 return OnGetItemImage(item);
5396
5397 return -1;
08a175ce 5398}
208458a7 5399
3cd94a0d 5400void wxGenericListCtrl::SetItemCount(long count)
2c1f73ee 5401{
9a83f860 5402 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
2c1f73ee
VZ
5403
5404 m_mainWin->SetItemCount(count);
5405}
5406
3cd94a0d 5407void wxGenericListCtrl::RefreshItem(long item)
1a6cb56f
VZ
5408{
5409 m_mainWin->RefreshLine(item);
5410}
5411
3cd94a0d 5412void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
1a6cb56f
VZ
5413{
5414 m_mainWin->RefreshLines(itemFrom, itemTo);
5415}
5416
d34d31f6
VZ
5417void wxGenericListCtrl::EnableBellOnNoMatch( bool on )
5418{
5419 m_mainWin->EnableBellOnNoMatch(on);
5420}
5421
277ea1b4
DS
5422// Generic wxListCtrl is more or less a container for two other
5423// windows which drawings are done upon. These are namely
5424// 'm_headerWin' and 'm_mainWin'.
5425// Here we override 'virtual wxWindow::Refresh()' to mimic the
5426// behaviour wxListCtrl has under wxMSW.
5427//
2f1e3c46
VZ
5428void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5429{
5430 if (!rect)
5431 {
5432 // The easy case, no rectangle specified.
5433 if (m_headerWin)
5434 m_headerWin->Refresh(eraseBackground);
5435
5436 if (m_mainWin)
5437 m_mainWin->Refresh(eraseBackground);
5438 }
5439 else
5440 {
5441 // Refresh the header window
5442 if (m_headerWin)
5443 {
5444 wxRect rectHeader = m_headerWin->GetRect();
5445 rectHeader.Intersect(*rect);
5446 if (rectHeader.GetWidth() && rectHeader.GetHeight())
5447 {
5448 int x, y;
5449 m_headerWin->GetPosition(&x, &y);
5450 rectHeader.Offset(-x, -y);
5451 m_headerWin->Refresh(eraseBackground, &rectHeader);
5452 }
2f1e3c46
VZ
5453 }
5454
5455 // Refresh the main window
5456 if (m_mainWin)
5457 {
5458 wxRect rectMain = m_mainWin->GetRect();
5459 rectMain.Intersect(*rect);
5460 if (rectMain.GetWidth() && rectMain.GetHeight())
5461 {
5462 int x, y;
5463 m_mainWin->GetPosition(&x, &y);
5464 rectMain.Offset(-x, -y);
5465 m_mainWin->Refresh(eraseBackground, &rectMain);
5466 }
5467 }
5468 }
5469}
5470
e1983cad
VZ
5471void wxGenericListCtrl::Update()
5472{
5473 if ( m_mainWin )
5474 {
5475 if ( m_mainWin->m_dirty )
5476 m_mainWin->RecalculatePositions();
5477
5478 m_mainWin->Update();
5479 }
5480
5481 if ( m_headerWin )
5482 m_headerWin->Update();
5483}
5484
1e6feb95 5485#endif // wxUSE_LISTCTRL