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