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