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