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