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