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