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