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