]> git.saurik.com Git - wxWidgets.git/blame - src/generic/listctrl.cpp
Don't use tree style to draw focus in DrawItemSelectionRect() in wxGTK.
[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
d6a658ff
VZ
2756 // propagate the char event upwards
2757 wxKeyEvent ke(event);
2758 ke.SetEventObject( parent );
2759 if (parent->GetEventHandler()->ProcessEvent( ke ))
2760 return;
004fd0c8 2761
f029f1d1
VS
2762 if ( HandleAsNavigationKey(event) )
2763 return;
004fd0c8 2764
277ea1b4 2765 // no item -> nothing to do
cf1dfa6b 2766 if (!HasCurrent())
c801d85f 2767 {
51cc4dad
RR
2768 event.Skip();
2769 return;
e1e955e1 2770 }
51cc4dad 2771
cf76cd4e
VZ
2772 // don't use m_linesPerPage directly as it might not be computed yet
2773 const int pageSize = GetCountPerPage();
9a83f860 2774 wxCHECK_RET( pageSize, wxT("should have non zero page size") );
cf76cd4e 2775
03703fc0
RR
2776 if (GetLayoutDirection() == wxLayout_RightToLeft)
2777 {
2778 if (event.GetKeyCode() == WXK_RIGHT)
2779 event.m_keyCode = WXK_LEFT;
2780 else if (event.GetKeyCode() == WXK_LEFT)
2781 event.m_keyCode = WXK_RIGHT;
2782 }
2783
cf76cd4e 2784 switch ( event.GetKeyCode() )
c801d85f 2785 {
51cc4dad 2786 case WXK_UP:
cf1dfa6b
VZ
2787 if ( m_current > 0 )
2788 OnArrowChar( m_current - 1, event );
51cc4dad 2789 break;
cf1dfa6b 2790
51cc4dad 2791 case WXK_DOWN:
cf1dfa6b
VZ
2792 if ( m_current < (size_t)GetItemCount() - 1 )
2793 OnArrowChar( m_current + 1, event );
51cc4dad 2794 break;
cf1dfa6b 2795
51cc4dad 2796 case WXK_END:
1370703e 2797 if (!IsEmpty())
cf1dfa6b 2798 OnArrowChar( GetItemCount() - 1, event );
51cc4dad 2799 break;
cf1dfa6b 2800
51cc4dad 2801 case WXK_HOME:
1370703e 2802 if (!IsEmpty())
cf1dfa6b 2803 OnArrowChar( 0, event );
51cc4dad 2804 break;
cf1dfa6b 2805
faa94f3e 2806 case WXK_PAGEUP:
f6bcfd97 2807 {
cf76cd4e
VZ
2808 int steps = InReportView() ? pageSize - 1
2809 : m_current % pageSize;
cf1dfa6b
VZ
2810
2811 int index = m_current - steps;
2812 if (index < 0)
2813 index = 0;
2814
2815 OnArrowChar( index, event );
51cc4dad 2816 }
51cc4dad 2817 break;
cf1dfa6b 2818
faa94f3e 2819 case WXK_PAGEDOWN:
bffa1c77 2820 {
b5d43d1d 2821 int steps = InReportView()
cf76cd4e
VZ
2822 ? pageSize - 1
2823 : pageSize - (m_current % pageSize) - 1;
f6bcfd97 2824
cf1dfa6b
VZ
2825 size_t index = m_current + steps;
2826 size_t count = GetItemCount();
2827 if ( index >= count )
2828 index = count - 1;
2829
2830 OnArrowChar( index, event );
51cc4dad 2831 }
51cc4dad 2832 break;
cf1dfa6b 2833
51cc4dad 2834 case WXK_LEFT:
b5d43d1d 2835 if ( !InReportView() )
51cc4dad 2836 {
cf76cd4e 2837 int index = m_current - pageSize;
cf1dfa6b
VZ
2838 if (index < 0)
2839 index = 0;
2840
2841 OnArrowChar( index, event );
51cc4dad
RR
2842 }
2843 break;
cf1dfa6b 2844
51cc4dad 2845 case WXK_RIGHT:
b5d43d1d 2846 if ( !InReportView() )
51cc4dad 2847 {
cf76cd4e 2848 size_t index = m_current + pageSize;
cf1dfa6b
VZ
2849
2850 size_t count = GetItemCount();
2851 if ( index >= count )
2852 index = count - 1;
2853
2854 OnArrowChar( index, event );
51cc4dad
RR
2855 }
2856 break;
cf1dfa6b 2857
51cc4dad 2858 case WXK_SPACE:
b54e41c5 2859 if ( IsSingleSel() )
33d0e17c 2860 {
a9aead31
VZ
2861 if ( event.ControlDown() )
2862 {
2863 ReverseHighlight(m_current);
2864 }
2865 else // normal space press
70541533 2866 {
a9aead31 2867 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
70541533 2868 }
33d0e17c 2869 }
a9aead31
VZ
2870 else // multiple selection
2871 {
2872 ReverseHighlight(m_current);
2873 }
51cc4dad 2874 break;
cf1dfa6b 2875
51cc4dad
RR
2876 case WXK_RETURN:
2877 case WXK_EXECUTE:
87948f22 2878 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
51cc4dad 2879 break;
cf1dfa6b 2880
51cc4dad 2881 default:
51cc4dad 2882 event.Skip();
e1e955e1 2883 }
e1e955e1 2884}
c801d85f 2885
cf1dfa6b
VZ
2886// ----------------------------------------------------------------------------
2887// focus handling
2888// ----------------------------------------------------------------------------
2889
c801d85f
KB
2890void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
2891{
dc76287d
RD
2892 if ( GetParent() )
2893 {
2894 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
2895 event.SetEventObject( GetParent() );
2896 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
2897 return;
2898 }
2899
bde9072f
VZ
2900 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
2901 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
2902 // which are already drawn correctly resulting in horrible flicker - avoid
2903 // it
47724389
VZ
2904 if ( !m_hasFocus )
2905 {
ca65c044 2906 m_hasFocus = true;
bde9072f 2907
47724389
VZ
2908 RefreshSelected();
2909 }
e1e955e1 2910}
c801d85f
KB
2911
2912void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
2913{
dc76287d
RD
2914 if ( GetParent() )
2915 {
2916 wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
2917 event.SetEventObject( GetParent() );
2918 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
2919 return;
2920 }
277ea1b4 2921
ca65c044 2922 m_hasFocus = false;
49ecb029 2923 RefreshSelected();
e1e955e1 2924}
c801d85f 2925
1e6d9499 2926void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
c801d85f 2927{
cf1dfa6b 2928 if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
63852e78
RR
2929 {
2930 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
63852e78 2931 }
cf1dfa6b 2932 else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
63852e78
RR
2933 {
2934 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2935 }
cf1dfa6b 2936 else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
0b855868
RR
2937 {
2938 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2939 }
b5d43d1d 2940 else if ( InReportView() && (m_small_image_list))
63852e78
RR
2941 {
2942 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
63852e78 2943 }
e1e955e1 2944}
c801d85f 2945
5cd89174 2946void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
c801d85f 2947{
cf1dfa6b 2948 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
63852e78
RR
2949 {
2950 m_normal_image_list->GetSize( index, width, height );
63852e78 2951 }
cf1dfa6b 2952 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
63852e78
RR
2953 {
2954 m_small_image_list->GetSize( index, width, height );
63852e78 2955 }
cf1dfa6b 2956 else if ( HasFlag(wxLC_LIST) && m_small_image_list )
0b855868
RR
2957 {
2958 m_small_image_list->GetSize( index, width, height );
0b855868 2959 }
b5d43d1d 2960 else if ( InReportView() && m_small_image_list )
63852e78
RR
2961 {
2962 m_small_image_list->GetSize( index, width, height );
63852e78 2963 }
cf1dfa6b
VZ
2964 else
2965 {
2966 width =
2967 height = 0;
2968 }
e1e955e1 2969}
c801d85f 2970
5cd89174 2971int wxListMainWindow::GetTextLength( const wxString &s ) const
c801d85f 2972{
5cd89174 2973 wxClientDC dc( wxConstCast(this, wxListMainWindow) );
cf1dfa6b 2974 dc.SetFont( GetFont() );
c801d85f 2975
cf1dfa6b
VZ
2976 wxCoord lw;
2977 dc.GetTextExtent( s, &lw, NULL );
2978
2979 return lw + AUTOSIZE_COL_MARGIN;
e1e955e1 2980}
c801d85f 2981
8a3e173a 2982void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
c801d85f 2983{
ca65c044 2984 m_dirty = true;
f6bcfd97
BP
2985
2986 // calc the spacing from the icon size
277ea1b4
DS
2987 int width = 0, height = 0;
2988
f6bcfd97 2989 if ((imageList) && (imageList->GetImageCount()) )
f6bcfd97 2990 imageList->GetSize(0, width, height);
f6bcfd97
BP
2991
2992 if (which == wxIMAGE_LIST_NORMAL)
2993 {
2994 m_normal_image_list = imageList;
2995 m_normal_spacing = width + 8;
2996 }
2997
2998 if (which == wxIMAGE_LIST_SMALL)
2999 {
3000 m_small_image_list = imageList;
3001 m_small_spacing = width + 14;
ca7353de 3002 m_lineHeight = 0; // ensure that the line height will be recalc'd
f6bcfd97 3003 }
e1e955e1 3004}
c801d85f 3005
debe6624 3006void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
c801d85f 3007{
ca65c044 3008 m_dirty = true;
139adb6a 3009 if (isSmall)
139adb6a 3010 m_small_spacing = spacing;
139adb6a 3011 else
139adb6a 3012 m_normal_spacing = spacing;
e1e955e1 3013}
c801d85f 3014
debe6624 3015int wxListMainWindow::GetItemSpacing( bool isSmall )
c801d85f 3016{
f6bcfd97 3017 return isSmall ? m_small_spacing : m_normal_spacing;
e1e955e1 3018}
c801d85f 3019
cf1dfa6b
VZ
3020// ----------------------------------------------------------------------------
3021// columns
3022// ----------------------------------------------------------------------------
3023
debe6624 3024void wxListMainWindow::SetColumn( int col, wxListItem &item )
c801d85f 3025{
222ed1d6 3026 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
f6bcfd97 3027
9a83f860 3028 wxCHECK_RET( node, wxT("invalid column index in SetColumn") );
cf1dfa6b
VZ
3029
3030 if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
3031 item.m_width = GetTextLength( item.m_text );
3032
3033 wxListHeaderData *column = node->GetData();
3034 column->SetItem( item );
3035
3036 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
f6bcfd97 3037 if ( headerWin )
ca65c044 3038 headerWin->m_dirty = true;
cf1dfa6b 3039
ca65c044 3040 m_dirty = true;
cf1dfa6b
VZ
3041
3042 // invalidate it as it has to be recalculated
3043 m_headerWidth = 0;
e1e955e1 3044}
c801d85f 3045
debe6624 3046void wxListMainWindow::SetColumnWidth( int col, int width )
c801d85f 3047{
cf1dfa6b 3048 wxCHECK_RET( col >= 0 && col < GetColumnCount(),
9a83f860 3049 wxT("invalid column index") );
cf1dfa6b 3050
b5d43d1d 3051 wxCHECK_RET( InReportView(),
9a83f860 3052 wxT("SetColumnWidth() can only be called in report mode.") );
7d7b3f69 3053
ca65c044 3054 m_dirty = true;
06cd40a8 3055
abe4a4f1
VS
3056 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3057 if ( headerWin )
ca65c044 3058 headerWin->m_dirty = true;
bd8289c1 3059
222ed1d6 3060 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
9a83f860 3061 wxCHECK_RET( node, wxT("no column?") );
cf1dfa6b
VZ
3062
3063 wxListHeaderData *column = node->GetData();
3064
3065 size_t count = GetItemCount();
3066
f6bcfd97
BP
3067 if (width == wxLIST_AUTOSIZE_USEHEADER)
3068 {
cf1dfa6b 3069 width = GetTextLength(column->GetText());
d63c9ecf
VZ
3070 width += 2*EXTRA_WIDTH;
3071
3072 // check for column header's image availability
3073 const int image = column->GetImage();
3074 if ( image != -1 )
3075 {
3076 if ( m_small_image_list )
3077 {
3078 int ix = 0, iy = 0;
3079 m_small_image_list->GetSize(image, ix, iy);
3080 width += ix + HEADER_IMAGE_MARGIN_IN_REPORT_MODE;
3081 }
3082 }
f6bcfd97 3083 }
cf1dfa6b 3084 else if ( width == wxLIST_AUTOSIZE )
0180dad6 3085 {
cf1dfa6b
VZ
3086 if ( IsVirtual() )
3087 {
3088 // TODO: determine the max width somehow...
3089 width = WIDTH_COL_DEFAULT;
3090 }
3091 else // !virtual
0180dad6 3092 {
cf1dfa6b
VZ
3093 wxClientDC dc(this);
3094 dc.SetFont( GetFont() );
3095
3096 int max = AUTOSIZE_COL_MARGIN;
3097
7d4813a0
VZ
3098 // if the cached column width isn't valid then recalculate it
3099 if (m_aColWidths.Item(col)->bNeedsUpdate)
0180dad6 3100 {
7d4813a0
VZ
3101 for (size_t i = 0; i < count; i++)
3102 {
277ea1b4 3103 wxListLineData *line = GetLine( i );
7d4813a0 3104 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
cf1dfa6b 3105
9a83f860 3106 wxCHECK_RET( n, wxT("no subitem?") );
cf1dfa6b 3107
7d4813a0
VZ
3108 wxListItemData *itemData = n->GetData();
3109 wxListItem item;
cf1dfa6b 3110
7d4813a0
VZ
3111 itemData->GetItem(item);
3112 int itemWidth = GetItemWidthWithImage(&item);
3113 if (itemWidth > max)
3114 max = itemWidth;
bffa1c77 3115 }
cf1dfa6b 3116
7d4813a0
VZ
3117 m_aColWidths.Item(col)->bNeedsUpdate = false;
3118 m_aColWidths.Item(col)->nMaxWidth = max;
0180dad6 3119 }
cf1dfa6b 3120
7d4813a0 3121 max = m_aColWidths.Item(col)->nMaxWidth;
cf1dfa6b 3122 width = max + AUTOSIZE_COL_MARGIN;
0180dad6 3123 }
0180dad6
RR
3124 }
3125
cf1dfa6b 3126 column->SetWidth( width );
bd8289c1 3127
cf1dfa6b
VZ
3128 // invalidate it as it has to be recalculated
3129 m_headerWidth = 0;
3130}
3131
3132int wxListMainWindow::GetHeaderWidth() const
3133{
3134 if ( !m_headerWidth )
0208334d 3135 {
cf1dfa6b
VZ
3136 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
3137
3138 size_t count = GetColumnCount();
3139 for ( size_t col = 0; col < count; col++ )
63852e78 3140 {
cf1dfa6b 3141 self->m_headerWidth += GetColumnWidth(col);
63852e78 3142 }
0208334d 3143 }
bd8289c1 3144
cf1dfa6b 3145 return m_headerWidth;
e1e955e1 3146}
c801d85f 3147
cf1dfa6b 3148void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
c801d85f 3149{
222ed1d6 3150 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
9a83f860 3151 wxCHECK_RET( node, wxT("invalid column index in GetColumn") );
cf1dfa6b
VZ
3152
3153 wxListHeaderData *column = node->GetData();
3154 column->GetItem( item );
e1e955e1 3155}
c801d85f 3156
cf1dfa6b 3157int wxListMainWindow::GetColumnWidth( int col ) const
c801d85f 3158{
222ed1d6 3159 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
9a83f860 3160 wxCHECK_MSG( node, 0, wxT("invalid column index") );
24b9f055
VZ
3161
3162 wxListHeaderData *column = node->GetData();
3163 return column->GetWidth();
e1e955e1 3164}
c801d85f 3165
cf1dfa6b
VZ
3166// ----------------------------------------------------------------------------
3167// item state
3168// ----------------------------------------------------------------------------
c801d85f
KB
3169
3170void wxListMainWindow::SetItem( wxListItem &item )
3171{
cf1dfa6b
VZ
3172 long id = item.m_itemId;
3173 wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
9a83f860 3174 wxT("invalid item index in SetItem") );
cf1dfa6b 3175
6c02c329
VZ
3176 if ( !IsVirtual() )
3177 {
3178 wxListLineData *line = GetLine((size_t)id);
3179 line->SetItem( item.m_col, item );
7d4813a0 3180
39a7f085
VZ
3181 // Set item state if user wants
3182 if ( item.m_mask & wxLIST_MASK_STATE )
3183 SetItemState( item.m_itemId, item.m_state, item.m_state );
3184
7d4813a0
VZ
3185 if (InReportView())
3186 {
3187 // update the Max Width Cache if needed
3188 int width = GetItemWidthWithImage(&item);
3189
3190 if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
3191 m_aColWidths.Item(item.m_col)->nMaxWidth = width;
3192 }
6c02c329
VZ
3193 }
3194
285013b3
VZ
3195 // update the item on screen
3196 wxRect rectItem;
3197 GetItemRect(id, rectItem);
3198 RefreshRect(rectItem);
e1e955e1 3199}
c801d85f 3200
0afa5859
VZ
3201void wxListMainWindow::SetItemStateAll(long state, long stateMask)
3202{
3203 if ( IsEmpty() )
3204 return;
3205
3206 // first deal with selection
3207 if ( stateMask & wxLIST_STATE_SELECTED )
3208 {
3209 // set/clear select state
3210 if ( IsVirtual() )
3211 {
3212 // optimized version for virtual listctrl.
3213 m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
3214 Refresh();
3215 }
3216 else if ( state & wxLIST_STATE_SELECTED )
3217 {
3218 const long count = GetItemCount();
3219 for( long i = 0; i < count; i++ )
3220 {
3221 SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
3222 }
3223
3224 }
3225 else
3226 {
3227 // clear for non virtual (somewhat optimized by using GetNextItem())
3228 long i = -1;
3229 while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
3230 {
3231 SetItemState( i, 0, wxLIST_STATE_SELECTED );
3232 }
3233 }
3234 }
3235
3236 if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
3237 {
3238 // unfocus all: only one item can be focussed, so clearing focus for
3239 // all items is simply clearing focus of the focussed item.
3240 SetItemState(m_current, state, stateMask);
3241 }
3242 //(setting focus to all items makes no sense, so it is not handled here.)
3243}
3244
cf1dfa6b 3245void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
c801d85f 3246{
0afa5859
VZ
3247 if ( litem == -1 )
3248 {
3249 SetItemStateAll(state, stateMask);
3250 return;
3251 }
3252
7448de8d 3253 wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
9a83f860 3254 wxT("invalid list ctrl item index in SetItem") );
54442116 3255
cf1dfa6b 3256 size_t oldCurrent = m_current;
88b792af 3257 size_t item = (size_t)litem; // safe because of the check above
bd8289c1 3258
88b792af 3259 // do we need to change the focus?
54442116 3260 if ( stateMask & wxLIST_STATE_FOCUSED )
c801d85f 3261 {
54442116 3262 if ( state & wxLIST_STATE_FOCUSED )
92976ab6 3263 {
54442116 3264 // don't do anything if this item is already focused
cf1dfa6b 3265 if ( item != m_current )
92976ab6 3266 {
0ddefeb0 3267 ChangeCurrent(item);
cf1dfa6b 3268
88b792af 3269 if ( oldCurrent != (size_t)-1 )
cf1dfa6b 3270 {
88b792af
VZ
3271 if ( IsSingleSel() )
3272 {
ca65c044 3273 HighlightLine(oldCurrent, false);
88b792af
VZ
3274 }
3275
cf1dfa6b
VZ
3276 RefreshLine(oldCurrent);
3277 }
54442116 3278
92976ab6 3279 RefreshLine( m_current );
92976ab6 3280 }
54442116
VZ
3281 }
3282 else // unfocus
3283 {
3284 // don't do anything if this item is not focused
cf1dfa6b 3285 if ( item == m_current )
bffa1c77 3286 {
0ddefeb0 3287 ResetCurrent();
88b792af 3288
943f6ad3
VZ
3289 if ( IsSingleSel() )
3290 {
3291 // we must unselect the old current item as well or we
3292 // might end up with more than one selected item in a
3293 // single selection control
ca65c044 3294 HighlightLine(oldCurrent, false);
943f6ad3
VZ
3295 }
3296
88b792af 3297 RefreshLine( oldCurrent );
bffa1c77 3298 }
92976ab6 3299 }
e1e955e1 3300 }
54442116 3301
88b792af 3302 // do we need to change the selection state?
54442116
VZ
3303 if ( stateMask & wxLIST_STATE_SELECTED )
3304 {
3305 bool on = (state & wxLIST_STATE_SELECTED) != 0;
54442116 3306
b54e41c5 3307 if ( IsSingleSel() )
54442116 3308 {
cf1dfa6b
VZ
3309 if ( on )
3310 {
3311 // selecting the item also makes it the focused one in the
3312 // single sel mode
3313 if ( m_current != item )
3314 {
0ddefeb0 3315 ChangeCurrent(item);
cf1dfa6b
VZ
3316
3317 if ( oldCurrent != (size_t)-1 )
3318 {
ca65c044 3319 HighlightLine( oldCurrent, false );
cf1dfa6b
VZ
3320 RefreshLine( oldCurrent );
3321 }
3322 }
3323 }
3324 else // off
3325 {
3326 // only the current item may be selected anyhow
3327 if ( item != m_current )
3328 return;
3329 }
54442116
VZ
3330 }
3331
b54e41c5 3332 if ( HighlightLine(item, on) )
54442116 3333 {
cf1dfa6b 3334 RefreshLine(item);
54442116
VZ
3335 }
3336 }
e1e955e1 3337}
c801d85f 3338
62d89eb4 3339int wxListMainWindow::GetItemState( long item, long stateMask ) const
c801d85f 3340{
cf1dfa6b 3341 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
9a83f860 3342 wxT("invalid list ctrl item index in GetItemState()") );
cf1dfa6b 3343
92976ab6 3344 int ret = wxLIST_STATE_DONTCARE;
cf1dfa6b
VZ
3345
3346 if ( stateMask & wxLIST_STATE_FOCUSED )
c801d85f 3347 {
cf1dfa6b
VZ
3348 if ( (size_t)item == m_current )
3349 ret |= wxLIST_STATE_FOCUSED;
e1e955e1 3350 }
cf1dfa6b
VZ
3351
3352 if ( stateMask & wxLIST_STATE_SELECTED )
c801d85f 3353 {
b54e41c5 3354 if ( IsHighlighted(item) )
cf1dfa6b 3355 ret |= wxLIST_STATE_SELECTED;
e1e955e1 3356 }
cf1dfa6b 3357
92976ab6 3358 return ret;
e1e955e1 3359}
c801d85f 3360
62d89eb4 3361void wxListMainWindow::GetItem( wxListItem &item ) const
c801d85f 3362{
cf1dfa6b 3363 wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
9a83f860 3364 wxT("invalid item index in GetItem") );
cf1dfa6b
VZ
3365
3366 wxListLineData *line = GetLine((size_t)item.m_itemId);
3367 line->GetItem( item.m_col, item );
39a7f085
VZ
3368
3369 // Get item state if user wants it
3370 if ( item.m_mask & wxLIST_MASK_STATE )
3371 item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
3372 wxLIST_STATE_FOCUSED );
e1e955e1 3373}
c801d85f 3374
cf1dfa6b
VZ
3375// ----------------------------------------------------------------------------
3376// item count
3377// ----------------------------------------------------------------------------
3378
3379size_t wxListMainWindow::GetItemCount() const
1370703e
VZ
3380{
3381 return IsVirtual() ? m_countVirt : m_lines.GetCount();
3382}
3383
3384void wxListMainWindow::SetItemCount(long count)
c801d85f 3385{
b54e41c5 3386 m_selStore.SetItemCount(count);
1370703e
VZ
3387 m_countVirt = count;
3388
850ed6e7
VZ
3389 ResetVisibleLinesRange();
3390
5fe143df 3391 // scrollbars must be reset
ca65c044 3392 m_dirty = true;
e1e955e1 3393}
c801d85f 3394
62d89eb4 3395int wxListMainWindow::GetSelectedItemCount() const
c801d85f 3396{
cf1dfa6b 3397 // deal with the quick case first
b54e41c5 3398 if ( IsSingleSel() )
ca65c044 3399 return HasCurrent() ? IsHighlighted(m_current) : false;
cf1dfa6b
VZ
3400
3401 // virtual controls remmebers all its selections itself
3402 if ( IsVirtual() )
b54e41c5 3403 return m_selStore.GetSelectedCount();
cf1dfa6b
VZ
3404
3405 // TODO: we probably should maintain the number of items selected even for
3406 // non virtual controls as enumerating all lines is really slow...
3407 size_t countSel = 0;
3408 size_t count = GetItemCount();
3409 for ( size_t line = 0; line < count; line++ )
92976ab6 3410 {
b54e41c5 3411 if ( GetLine(line)->IsHighlighted() )
cf1dfa6b 3412 countSel++;
92976ab6 3413 }
c801d85f 3414
cf1dfa6b 3415 return countSel;
e1e955e1 3416}
e3e65dac 3417
cf1dfa6b
VZ
3418// ----------------------------------------------------------------------------
3419// item position/size
3420// ----------------------------------------------------------------------------
3421
11ebea16
VZ
3422wxRect wxListMainWindow::GetViewRect() const
3423{
929b7901 3424 wxASSERT_MSG( !HasFlag(wxLC_LIST), "not implemented for list view" );
11ebea16
VZ
3425
3426 // we need to find the longest/tallest label
277ea1b4 3427 wxCoord xMax = 0, yMax = 0;
11ebea16
VZ
3428 const int count = GetItemCount();
3429 if ( count )
3430 {
3431 for ( int i = 0; i < count; i++ )
3432 {
ebf82d2b
VZ
3433 // we need logical, not physical, coordinates here, so use
3434 // GetLineRect() instead of GetItemRect()
3435 wxRect r = GetLineRect(i);
11ebea16
VZ
3436
3437 wxCoord x = r.GetRight(),
3438 y = r.GetBottom();
3439
3440 if ( x > xMax )
3441 xMax = x;
3442 if ( y > yMax )
3443 yMax = y;
3444 }
3445 }
3446
eb6c4508 3447 // some fudge needed to make it look prettier
277ea1b4
DS
3448 xMax += 2 * EXTRA_BORDER_X;
3449 yMax += 2 * EXTRA_BORDER_Y;
eb6c4508
VZ
3450
3451 // account for the scrollbars if necessary
3452 const wxSize sizeAll = GetClientSize();
3453 if ( xMax > sizeAll.x )
3454 yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
3455 if ( yMax > sizeAll.y )
3456 xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
11ebea16
VZ
3457
3458 return wxRect(0, 0, xMax, yMax);
3459}
3460
e974c5d2
VZ
3461bool
3462wxListMainWindow::GetSubItemRect(long item, long subItem, wxRect& rect) const
c801d85f 3463{
b8e57034
VZ
3464 wxCHECK_MSG( subItem == wxLIST_GETSUBITEMRECT_WHOLEITEM || InReportView(),
3465 false,
9a83f860 3466 wxT("GetSubItemRect only meaningful in report view") );
e974c5d2 3467 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), false,
9a83f860 3468 wxT("invalid item in GetSubItemRect") );
cf1dfa6b 3469
1e54be64
VZ
3470 // ensure that we're laid out, otherwise we could return nonsense
3471 if ( m_dirty )
3472 {
3473 wxConstCast(this, wxListMainWindow)->
ca65c044 3474 RecalculatePositions(true /* no refresh */);
1e54be64
VZ
3475 }
3476
e974c5d2
VZ
3477 rect = GetLineRect((size_t)item);
3478
3479 // Adjust rect to specified column
3480 if ( subItem != wxLIST_GETSUBITEMRECT_WHOLEITEM )
3481 {
3482 wxCHECK_MSG( subItem >= 0 && subItem < GetColumnCount(), false,
9a83f860 3483 wxT("invalid subItem in GetSubItemRect") );
e974c5d2
VZ
3484
3485 for (int i = 0; i < subItem; i++)
3486 {
3487 rect.x += GetColumnWidth(i);
3488 }
3489 rect.width = GetColumnWidth(subItem);
3490 }
b54e41c5 3491
06cd40a8 3492 GetListCtrl()->CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
e974c5d2
VZ
3493
3494 return true;
e1e955e1 3495}
c801d85f 3496
62d89eb4 3497bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
c801d85f 3498{
cf1dfa6b
VZ
3499 wxRect rect;
3500 GetItemRect(item, rect);
bd8289c1 3501
cf1dfa6b
VZ
3502 pos.x = rect.x;
3503 pos.y = rect.y;
bd8289c1 3504
ca65c044 3505 return true;
e1e955e1 3506}
c801d85f 3507
cf1dfa6b
VZ
3508// ----------------------------------------------------------------------------
3509// geometry calculation
3510// ----------------------------------------------------------------------------
3511
34bbbc27 3512void wxListMainWindow::RecalculatePositions(bool noRefresh)
c801d85f 3513{
6d6d86a6
RD
3514 const int lineHeight = GetLineHeight();
3515
1e6d9499 3516 wxClientDC dc( this );
92976ab6 3517 dc.SetFont( GetFont() );
c801d85f 3518
13602ebd
VZ
3519 const size_t count = GetItemCount();
3520
cf1dfa6b 3521 int iconSpacing;
026d7276 3522 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
cf1dfa6b 3523 iconSpacing = m_normal_spacing;
026d7276 3524 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
cf1dfa6b
VZ
3525 iconSpacing = m_small_spacing;
3526 else
3527 iconSpacing = 0;
004fd0c8 3528
bf632edd 3529 // Note that we do not call GetClientSize() here but
34621cc5 3530 // GetSize() and subtract the border size for sunken
bf632edd
RR
3531 // borders manually. This is technically incorrect,
3532 // but we need to know the client area's size WITHOUT
3533 // scrollbars here. Since we don't know if there are
3534 // any scrollbars, we use GetSize() instead. Another
3535 // solution would be to call SetScrollbars() here to
3536 // remove the scrollbars and call GetClientSize() then,
3537 // but this might result in flicker and - worse - will
3538 // reset the scrollbars to 0 which is not good at all
3539 // if you resize a dialog/window, but don't want to
3540 // reset the window scrolling. RR.
3541 // Furthermore, we actually do NOT subtract the border
3542 // width as 2 pixels is just the extra space which we
3543 // need around the actual content in the window. Other-
3544 // wise the text would e.g. touch the upper border. RR.
cf1dfa6b
VZ
3545 int clientWidth,
3546 clientHeight;
bf632edd 3547 GetSize( &clientWidth, &clientHeight );
a77ec46d 3548
b5d43d1d 3549 if ( InReportView() )
cf1dfa6b 3550 {
13602ebd 3551 // all lines have the same height and we scroll one line per step
277ea1b4 3552 int entireHeight = count * lineHeight + LINE_SPACING;
bd8289c1 3553
b54e41c5
VZ
3554 m_linesPerPage = clientHeight / lineHeight;
3555
3556 ResetVisibleLinesRange();
2c1f73ee 3557
06cd40a8 3558 GetListCtrl()->SetScrollbars( SCROLL_UNIT_X, lineHeight,
13602ebd
VZ
3559 GetHeaderWidth() / SCROLL_UNIT_X,
3560 (entireHeight + lineHeight - 1) / lineHeight,
06cd40a8
RR
3561 GetListCtrl()->GetScrollPos(wxHORIZONTAL),
3562 GetListCtrl()->GetScrollPos(wxVERTICAL),
ca65c044 3563 true );
e1e955e1 3564 }
cf1dfa6b 3565 else // !report
92976ab6 3566 {
13602ebd
VZ
3567 // we have 3 different layout strategies: either layout all items
3568 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3569 // to arrange them in top to bottom, left to right (don't ask me why
3570 // not the other way round...) order
3571 if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) )
e487524e 3572 {
13602ebd
VZ
3573 int x = EXTRA_BORDER_X;
3574 int y = EXTRA_BORDER_Y;
cf1dfa6b 3575
94dd23ae
VZ
3576 wxCoord widthMax = 0;
3577
3578 size_t i;
3579 for ( i = 0; i < count; i++ )
92976ab6 3580 {
cf1dfa6b 3581 wxListLineData *line = GetLine(i);
92976ab6 3582 line->CalculateSize( &dc, iconSpacing );
13602ebd 3583 line->SetPosition( x, y, iconSpacing );
cf1dfa6b 3584
5cd89174 3585 wxSize sizeLine = GetLineSize(i);
cf1dfa6b 3586
13602ebd
VZ
3587 if ( HasFlag(wxLC_ALIGN_TOP) )
3588 {
94dd23ae
VZ
3589 if ( sizeLine.x > widthMax )
3590 widthMax = sizeLine.x;
3591
13602ebd
VZ
3592 y += sizeLine.y;
3593 }
3594 else // wxLC_ALIGN_LEFT
3595 {
3596 x += sizeLine.x + MARGIN_BETWEEN_ROWS;
3597 }
3598 }
3599
94dd23ae
VZ
3600 if ( HasFlag(wxLC_ALIGN_TOP) )
3601 {
3602 // traverse the items again and tweak their sizes so that they are
3603 // all the same in a row
3604 for ( i = 0; i < count; i++ )
3605 {
3606 wxListLineData *line = GetLine(i);
3607 line->m_gi->ExtendWidth(widthMax);
3608 }
3609 }
3610
06cd40a8 3611 GetListCtrl()->SetScrollbars
13602ebd
VZ
3612 (
3613 SCROLL_UNIT_X,
6d78bbe6 3614 lineHeight,
13602ebd 3615 (x + SCROLL_UNIT_X) / SCROLL_UNIT_X,
6d78bbe6 3616 (y + lineHeight) / lineHeight,
06cd40a8
RR
3617 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
3618 GetListCtrl()->GetScrollPos( wxVERTICAL ),
ca65c044 3619 true
13602ebd
VZ
3620 );
3621 }
3622 else // "flowed" arrangement, the most complicated case
3623 {
3624 // at first we try without any scrollbars, if the items don't fit into
3625 // the window, we recalculate after subtracting the space taken by the
3626 // scrollbar
3627
8a39593e 3628 int entireWidth = 0;
cf1dfa6b 3629
13602ebd
VZ
3630 for (int tries = 0; tries < 2; tries++)
3631 {
277ea1b4 3632 entireWidth = 2 * EXTRA_BORDER_X;
cf1dfa6b 3633
13602ebd 3634 if (tries == 1)
92976ab6 3635 {
13602ebd
VZ
3636 // Now we have decided that the items do not fit into the
3637 // client area, so we need a scrollbar
3638 entireWidth += SCROLL_UNIT_X;
92976ab6 3639 }
a77ec46d 3640
13602ebd
VZ
3641 int x = EXTRA_BORDER_X;
3642 int y = EXTRA_BORDER_Y;
3643 int maxWidthInThisRow = 0;
3644
3645 m_linesPerPage = 0;
3646 int currentlyVisibleLines = 0;
a77ec46d 3647
13602ebd 3648 for (size_t i = 0; i < count; i++)
92976ab6 3649 {
13602ebd 3650 currentlyVisibleLines++;
277ea1b4 3651 wxListLineData *line = GetLine( i );
13602ebd
VZ
3652 line->CalculateSize( &dc, iconSpacing );
3653 line->SetPosition( x, y, iconSpacing );
3654
277ea1b4 3655 wxSize sizeLine = GetLineSize( i );
a77ec46d 3656
13602ebd
VZ
3657 if ( maxWidthInThisRow < sizeLine.x )
3658 maxWidthInThisRow = sizeLine.x;
3659
3660 y += sizeLine.y;
3661 if (currentlyVisibleLines > m_linesPerPage)
3662 m_linesPerPage = currentlyVisibleLines;
3663
3664 if ( y + sizeLine.y >= clientHeight )
3665 {
3666 currentlyVisibleLines = 0;
3667 y = EXTRA_BORDER_Y;
3668 maxWidthInThisRow += MARGIN_BETWEEN_ROWS;
3669 x += maxWidthInThisRow;
3670 entireWidth += maxWidthInThisRow;
3671 maxWidthInThisRow = 0;
3672 }
3673
3674 // We have reached the last item.
3675 if ( i == count - 1 )
3676 entireWidth += maxWidthInThisRow;
3677
3678 if ( (tries == 0) &&
3679 (entireWidth + SCROLL_UNIT_X > clientWidth) )
3680 {
3681 clientHeight -= wxSystemSettings::
3682 GetMetric(wxSYS_HSCROLL_Y);
3683 m_linesPerPage = 0;
13602ebd
VZ
3684 break;
3685 }
3686
3687 if ( i == count - 1 )
3688 tries = 1; // Everything fits, no second try required.
3689 }
92976ab6 3690 }
bffa1c77 3691
06cd40a8 3692 GetListCtrl()->SetScrollbars
13602ebd
VZ
3693 (
3694 SCROLL_UNIT_X,
6d78bbe6 3695 lineHeight,
13602ebd
VZ
3696 (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X,
3697 0,
06cd40a8 3698 GetListCtrl()->GetScrollPos( wxHORIZONTAL ),
13602ebd 3699 0,
ca65c044 3700 true
13602ebd
VZ
3701 );
3702 }
e1e955e1 3703 }
cf1dfa6b 3704
34bbbc27
VZ
3705 if ( !noRefresh )
3706 {
3707 // FIXME: why should we call it from here?
3708 UpdateCurrent();
b54e41c5 3709
34bbbc27
VZ
3710 RefreshAll();
3711 }
b54e41c5
VZ
3712}
3713
3714void wxListMainWindow::RefreshAll()
3715{
ca65c044 3716 m_dirty = false;
b54e41c5
VZ
3717 Refresh();
3718
3719 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
70541533 3720 if ( headerWin && headerWin->m_dirty )
b54e41c5 3721 {
ca65c044 3722 headerWin->m_dirty = false;
b54e41c5
VZ
3723 headerWin->Refresh();
3724 }
e1e955e1 3725}
c801d85f 3726
cf1dfa6b 3727void wxListMainWindow::UpdateCurrent()
c801d85f 3728{
b54e41c5 3729 if ( !HasCurrent() && !IsEmpty() )
0ddefeb0 3730 ChangeCurrent(0);
e1e955e1 3731}
c801d85f 3732
19695fbd
VZ
3733long wxListMainWindow::GetNextItem( long item,
3734 int WXUNUSED(geometry),
62d89eb4 3735 int state ) const
c801d85f 3736{
d1022fd6
VZ
3737 long ret = item,
3738 max = GetItemCount();
3739 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
9a83f860 3740 wxT("invalid listctrl index in GetNextItem()") );
19695fbd
VZ
3741
3742 // notice that we start with the next item (or the first one if item == -1)
3743 // and this is intentional to allow writing a simple loop to iterate over
3744 // all selected items
d1022fd6
VZ
3745 ret++;
3746 if ( ret == max )
277ea1b4
DS
3747 // this is not an error because the index was OK initially,
3748 // just no such item
d1022fd6 3749 return -1;
d1022fd6 3750
cf1dfa6b 3751 if ( !state )
cf1dfa6b
VZ
3752 // any will do
3753 return (size_t)ret;
cf1dfa6b
VZ
3754
3755 size_t count = GetItemCount();
3756 for ( size_t line = (size_t)ret; line < count; line++ )
63852e78 3757 {
cf1dfa6b
VZ
3758 if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
3759 return line;
3760
b54e41c5 3761 if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
cf1dfa6b 3762 return line;
63852e78 3763 }
19695fbd 3764
63852e78 3765 return -1;
e1e955e1 3766}
c801d85f 3767
cf1dfa6b
VZ
3768// ----------------------------------------------------------------------------
3769// deleting stuff
3770// ----------------------------------------------------------------------------
3771
b54e41c5 3772void wxListMainWindow::DeleteItem( long lindex )
c801d85f 3773{
cf1dfa6b
VZ
3774 size_t count = GetItemCount();
3775
b54e41c5 3776 wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
9a83f860 3777 wxT("invalid item index in DeleteItem") );
cf1dfa6b 3778
b54e41c5
VZ
3779 size_t index = (size_t)lindex;
3780
d6ddcd57
VZ
3781 // we don't need to adjust the index for the previous items
3782 if ( HasCurrent() && m_current >= index )
cf1dfa6b 3783 {
d6ddcd57
VZ
3784 // if the current item is being deleted, we want the next one to
3785 // become selected - unless there is no next one - so don't adjust
3786 // m_current in this case
3787 if ( m_current != index || m_current == count - 1 )
d6ddcd57 3788 m_current--;
cf1dfa6b 3789 }
6fef2483 3790
938b652b 3791 if ( InReportView() )
63852e78 3792 {
a95d5751
RR
3793 // mark the Column Max Width cache as dirty if the items in the line
3794 // we're deleting contain the Max Column Width
7d4813a0
VZ
3795 wxListLineData * const line = GetLine(index);
3796 wxListItemDataList::compatibility_iterator n;
3797 wxListItemData *itemData;
3798 wxListItem item;
3799 int itemWidth;
3800
3801 for (size_t i = 0; i < m_columns.GetCount(); i++)
3802 {
3803 n = line->m_items.Item( i );
3804 itemData = n->GetData();
3805 itemData->GetItem(item);
3806
3807 itemWidth = GetItemWidthWithImage(&item);
3808
3809 if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth)
3810 m_aColWidths.Item(i)->bNeedsUpdate = true;
3811 }
3812
6b4a8d93 3813 ResetVisibleLinesRange();
938b652b
VZ
3814 }
3815
a95d5751
RR
3816 SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM, wxDefaultPosition );
3817
938b652b
VZ
3818 if ( IsVirtual() )
3819 {
3820 m_countVirt--;
b54e41c5
VZ
3821 m_selStore.OnItemDelete(index);
3822 }
3823 else
3824 {
3825 m_lines.RemoveAt( index );
63852e78 3826 }
6b4a8d93 3827
70541533 3828 // we need to refresh the (vert) scrollbar as the number of items changed
ca65c044 3829 m_dirty = true;
91c6cc0e 3830
6b4a8d93 3831 RefreshAfter(index);
e1e955e1 3832}
c801d85f 3833
debe6624 3834void wxListMainWindow::DeleteColumn( int col )
c801d85f 3835{
222ed1d6 3836 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
24b9f055
VZ
3837
3838 wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
bd8289c1 3839
ca65c044 3840 m_dirty = true;
222ed1d6
MB
3841 delete node->GetData();
3842 m_columns.Erase( node );
ae409cb8 3843
df6f82f0
VZ
3844 if ( !IsVirtual() )
3845 {
3846 // update all the items
3847 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
3848 {
3849 wxListLineData * const line = GetLine(i);
222ed1d6
MB
3850 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
3851 delete n->GetData();
3852 line->m_items.Erase(n);
df6f82f0
VZ
3853 }
3854 }
3855
7d4813a0
VZ
3856 if ( InReportView() ) // we only cache max widths when in Report View
3857 {
3858 delete m_aColWidths.Item(col);
3859 m_aColWidths.RemoveAt(col);
3860 }
3861
ae409cb8
VZ
3862 // invalidate it as it has to be recalculated
3863 m_headerWidth = 0;
e1e955e1 3864}
c801d85f 3865
5fe143df 3866void wxListMainWindow::DoDeleteAllItems()
c801d85f 3867{
cf1dfa6b 3868 if ( IsEmpty() )
cf1dfa6b
VZ
3869 // nothing to do - in particular, don't send the event
3870 return;
cf1dfa6b 3871
b54e41c5 3872 ResetCurrent();
7c0ea335
VZ
3873
3874 // to make the deletion of all items faster, we don't send the
cf1dfa6b
VZ
3875 // notifications for each item deletion in this case but only one event
3876 // for all of them: this is compatible with wxMSW and documented in
3877 // DeleteAllItems() description
bffa1c77 3878
12c1b46a
RR
3879 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
3880 event.SetEventObject( GetParent() );
3881 GetParent()->GetEventHandler()->ProcessEvent( event );
7c0ea335 3882
b54e41c5
VZ
3883 if ( IsVirtual() )
3884 {
3885 m_countVirt = 0;
850ed6e7 3886 m_selStore.Clear();
b54e41c5
VZ
3887 }
3888
6b4a8d93
VZ
3889 if ( InReportView() )
3890 {
3891 ResetVisibleLinesRange();
7d4813a0
VZ
3892 for (size_t i = 0; i < m_aColWidths.GetCount(); i++)
3893 {
8d36b216 3894 m_aColWidths.Item(i)->bNeedsUpdate = true;
7d4813a0 3895 }
6b4a8d93
VZ
3896 }
3897
5b077d48 3898 m_lines.Clear();
5fe143df 3899}
cf1dfa6b 3900
5fe143df
VZ
3901void wxListMainWindow::DeleteAllItems()
3902{
3903 DoDeleteAllItems();
3904
3905 RecalculatePositions();
e1e955e1 3906}
c801d85f 3907
12c1b46a 3908void wxListMainWindow::DeleteEverything()
c801d85f 3909{
222ed1d6 3910 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
8d36b216 3911 WX_CLEAR_ARRAY(m_aColWidths);
904ccf52
VZ
3912
3913 DeleteAllItems();
e1e955e1 3914}
c801d85f 3915
cf1dfa6b
VZ
3916// ----------------------------------------------------------------------------
3917// scanning for an item
3918// ----------------------------------------------------------------------------
3919
debe6624 3920void wxListMainWindow::EnsureVisible( long index )
c801d85f 3921{
cf1dfa6b 3922 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
9a83f860 3923 wxT("invalid index in EnsureVisible") );
cf1dfa6b
VZ
3924
3925 // We have to call this here because the label in question might just have
34bbbc27
VZ
3926 // been added and its position is not known yet
3927 if ( m_dirty )
ca65c044 3928 RecalculatePositions(true /* no refresh */);
34bbbc27
VZ
3929
3930 MoveToItem((size_t)index);
e1e955e1 3931}
c801d85f 3932
23739465 3933long wxListMainWindow::FindItem(long start, const wxString& str, bool partial )
c801d85f 3934{
23739465
RR
3935 if (str.empty())
3936 return wxNOT_FOUND;
6fef2483 3937
5b077d48 3938 long pos = start;
23739465 3939 wxString str_upper = str.Upper();
cf1dfa6b
VZ
3940 if (pos < 0)
3941 pos = 0;
54442116 3942
cf1dfa6b
VZ
3943 size_t count = GetItemCount();
3944 for ( size_t i = (size_t)pos; i < count; i++ )
3945 {
3946 wxListLineData *line = GetLine(i);
23739465
RR
3947 wxString line_upper = line->GetText(0).Upper();
3948 if (!partial)
3949 {
3950 if (line_upper == str_upper )
3951 return i;
3952 }
3953 else
3954 {
3955 if (line_upper.find(str_upper) == 0)
3956 return i;
3957 }
5b077d48 3958 }
cf1dfa6b
VZ
3959
3960 return wxNOT_FOUND;
e1e955e1 3961}
c801d85f 3962
81ce36aa 3963long wxListMainWindow::FindItem(long start, wxUIntPtr data)
c801d85f 3964{
5b077d48 3965 long pos = start;
cf1dfa6b
VZ
3966 if (pos < 0)
3967 pos = 0;
3968
3969 size_t count = GetItemCount();
3970 for (size_t i = (size_t)pos; i < count; i++)
5b077d48 3971 {
cf1dfa6b 3972 wxListLineData *line = GetLine(i);
5b077d48
RR
3973 wxListItem item;
3974 line->GetItem( 0, item );
cf1dfa6b
VZ
3975 if (item.m_data == data)
3976 return i;
5b077d48 3977 }
cf1dfa6b
VZ
3978
3979 return wxNOT_FOUND;
e1e955e1 3980}
c801d85f 3981
8158e0e1
VZ
3982long wxListMainWindow::FindItem( const wxPoint& pt )
3983{
e6bf9573 3984 size_t topItem;
277ea1b4 3985 GetVisibleLinesRange( &topItem, NULL );
8158e0e1 3986
e6bf9573 3987 wxPoint p;
277ea1b4
DS
3988 GetItemPosition( GetItemCount() - 1, p );
3989 if ( p.y == 0 )
e6bf9573 3990 return topItem;
277ea1b4
DS
3991
3992 long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem );
3993 if ( id >= 0 && id < (long)GetItemCount() )
e6bf9573 3994 return id;
8158e0e1 3995
e6bf9573 3996 return wxNOT_FOUND;
8158e0e1
VZ
3997}
3998
be0e5d69 3999long wxListMainWindow::HitTest( int x, int y, int &flags ) const
c801d85f 4000{
06cd40a8 4001 GetListCtrl()->CalcUnscrolledPosition( x, y, &x, &y );
e8741cca 4002
fc4f1d5f
VZ
4003 size_t count = GetItemCount();
4004
b5d43d1d 4005 if ( InReportView() )
c801d85f 4006 {
5cd89174 4007 size_t current = y / GetLineHeight();
fc4f1d5f
VZ
4008 if ( current < count )
4009 {
4010 flags = HitTestLine(current, x, y);
4011 if ( flags )
4012 return current;
4013 }
5cd89174
VZ
4014 }
4015 else // !report
4016 {
4017 // TODO: optimize it too! this is less simple than for report view but
4018 // enumerating all items is still not a way to do it!!
5cd89174 4019 for ( size_t current = 0; current < count; current++ )
5b077d48 4020 {
5cd89174
VZ
4021 flags = HitTestLine(current, x, y);
4022 if ( flags )
4023 return current;
5b077d48 4024 }
e1e955e1 4025 }
cf1dfa6b
VZ
4026
4027 return wxNOT_FOUND;
e1e955e1 4028}
c801d85f 4029
cf1dfa6b
VZ
4030// ----------------------------------------------------------------------------
4031// adding stuff
4032// ----------------------------------------------------------------------------
4033
c801d85f
KB
4034void wxListMainWindow::InsertItem( wxListItem &item )
4035{
9a83f860 4036 wxASSERT_MSG( !IsVirtual(), wxT("can't be used with virtual control") );
cf1dfa6b 4037
c7cf7a78 4038 int count = GetItemCount();
9a83f860 4039 wxCHECK_RET( item.m_itemId >= 0, wxT("invalid item index") );
cf1dfa6b 4040
33c956e6
RD
4041 if (item.m_itemId > count)
4042 item.m_itemId = count;
b713f891 4043
cf1dfa6b
VZ
4044 size_t id = item.m_itemId;
4045
ca65c044 4046 m_dirty = true;
cf1dfa6b 4047
b5d43d1d 4048 if ( InReportView() )
2b5f62a0 4049 {
2b5f62a0 4050 ResetVisibleLinesRange();
7d4813a0 4051
e612dec2
VZ
4052 const unsigned col = item.GetColumn();
4053 wxCHECK_RET( col < m_aColWidths.size(), "invalid item column" );
4054
7d4813a0 4055 // calculate the width of the item and adjust the max column width
e612dec2 4056 wxColWidthInfo *pWidthInfo = m_aColWidths.Item(col);
f5c479cc 4057 int width = GetItemWidthWithImage(&item);
7d4813a0
VZ
4058 item.SetWidth(width);
4059 if (width > pWidthInfo->nMaxWidth)
4060 pWidthInfo->nMaxWidth = width;
1370703e 4061 }
004fd0c8 4062
5cd89174 4063 wxListLineData *line = new wxListLineData(this);
004fd0c8 4064
1e6601c7 4065 line->SetItem( item.m_col, item );
cf1dfa6b
VZ
4066
4067 m_lines.Insert( line, id );
5cd89174 4068
ca65c044 4069 m_dirty = true;
a67e6e54 4070
ab96d680
VZ
4071 // If an item is selected at or below the point of insertion, we need to
4072 // increment the member variables because the current row's index has gone
4073 // up by one
4074 if ( HasCurrent() && m_current >= id )
ab96d680 4075 m_current++;
ab96d680 4076
a67e6e54
VZ
4077 SendNotify(id, wxEVT_COMMAND_LIST_INSERT_ITEM);
4078
5cd89174 4079 RefreshLines(id, GetItemCount() - 1);
e1e955e1 4080}
c801d85f 4081
debe6624 4082void wxListMainWindow::InsertColumn( long col, wxListItem &item )
c801d85f 4083{
ca65c044 4084 m_dirty = true;
b5d43d1d 4085 if ( InReportView() )
3db7be80 4086 {
54442116
VZ
4087 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
4088 item.m_width = GetTextLength( item.m_text );
df6f82f0 4089
5b077d48 4090 wxListHeaderData *column = new wxListHeaderData( item );
7d4813a0
VZ
4091 wxColWidthInfo *colWidthInfo = new wxColWidthInfo();
4092
df6f82f0
VZ
4093 bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4094 if ( insert )
5b077d48 4095 {
7d4813a0
VZ
4096 wxListHeaderDataList::compatibility_iterator
4097 node = m_columns.Item( col );
24b9f055 4098 m_columns.Insert( node, column );
7d4813a0 4099 m_aColWidths.Insert( colWidthInfo, col );
5b077d48
RR
4100 }
4101 else
4102 {
4103 m_columns.Append( column );
7d4813a0 4104 m_aColWidths.Add( colWidthInfo );
5b077d48 4105 }
ae409cb8 4106
df6f82f0
VZ
4107 if ( !IsVirtual() )
4108 {
4109 // update all the items
4110 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4111 {
4112 wxListLineData * const line = GetLine(i);
4113 wxListItemData * const data = new wxListItemData(this);
4114 if ( insert )
4115 line->m_items.Insert(col, data);
4116 else
4117 line->m_items.Append(data);
4118 }
4119 }
4120
ae409cb8
VZ
4121 // invalidate it as it has to be recalculated
4122 m_headerWidth = 0;
3db7be80 4123 }
e1e955e1 4124}
c801d85f 4125
7d4813a0
VZ
4126int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
4127{
4128 int width = 0;
4129 wxClientDC dc(this);
4130
4131 dc.SetFont( GetFont() );
4132
4133 if (item->GetImage() != -1)
4134 {
4135 int ix, iy;
4136 GetImageSize( item->GetImage(), ix, iy );
4137 width += ix + 5;
4138 }
4139
4140 if (!item->GetText().empty())
4141 {
4142 wxCoord w;
4143 dc.GetTextExtent( item->GetText(), &w, NULL );
4144 width += w;
4145 }
4146
4147 return width;
4148}
4149
cf1dfa6b
VZ
4150// ----------------------------------------------------------------------------
4151// sorting
4152// ----------------------------------------------------------------------------
4153
7d7b3f69 4154static wxListCtrlCompare list_ctrl_compare_func_2;
b18e2046 4155static wxIntPtr list_ctrl_compare_data;
c801d85f 4156
f6bcfd97 4157int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
c801d85f 4158{
f6bcfd97
BP
4159 wxListLineData *line1 = *arg1;
4160 wxListLineData *line2 = *arg2;
5b077d48
RR
4161 wxListItem item;
4162 line1->GetItem( 0, item );
81ce36aa 4163 wxUIntPtr data1 = item.m_data;
5b077d48 4164 line2->GetItem( 0, item );
81ce36aa 4165 wxUIntPtr data2 = item.m_data;
5b077d48 4166 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
e1e955e1 4167}
c801d85f 4168
b18e2046 4169void wxListMainWindow::SortItems( wxListCtrlCompare fn, wxIntPtr data )
c801d85f 4170{
6b06a727
VZ
4171 // selections won't make sense any more after sorting the items so reset
4172 // them
4173 HighlightAll(false);
4174 ResetCurrent();
4175
5b077d48
RR
4176 list_ctrl_compare_func_2 = fn;
4177 list_ctrl_compare_data = data;
4178 m_lines.Sort( list_ctrl_compare_func_1 );
ca65c044 4179 m_dirty = true;
e1e955e1 4180}
c801d85f 4181
cf1dfa6b
VZ
4182// ----------------------------------------------------------------------------
4183// scrolling
4184// ----------------------------------------------------------------------------
4185
7c74e7fe
SC
4186void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4187{
602e3365
RR
4188 // update our idea of which lines are shown when we redraw the window the
4189 // next time
4190 ResetVisibleLinesRange();
a9aead31 4191
cf1dfa6b 4192 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
7c74e7fe 4193 {
3cd94a0d 4194 wxGenericListCtrl* lc = GetListCtrl();
9a83f860 4195 wxCHECK_RET( lc, wxT("no listctrl window?") );
cf1dfa6b 4196
52d2a5e4
FM
4197 if (lc->m_headerWin) // when we use wxLC_NO_HEADER, m_headerWin==NULL
4198 {
4199 lc->m_headerWin->Refresh();
4200 lc->m_headerWin->Update();
4201 }
7c74e7fe 4202 }
cf1dfa6b
VZ
4203}
4204
5cd89174
VZ
4205int wxListMainWindow::GetCountPerPage() const
4206{
4207 if ( !m_linesPerPage )
4208 {
4209 wxConstCast(this, wxListMainWindow)->
4210 m_linesPerPage = GetClientSize().y / GetLineHeight();
4211 }
4212
4213 return m_linesPerPage;
4214}
4215
b54e41c5 4216void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
cf1dfa6b 4217{
9a83f860 4218 wxASSERT_MSG( InReportView(), wxT("this is for report mode only") );
cf1dfa6b 4219
b54e41c5
VZ
4220 if ( m_lineFrom == (size_t)-1 )
4221 {
b54e41c5 4222 size_t count = GetItemCount();
6522713c
VZ
4223 if ( count )
4224 {
e5d28ed4 4225 m_lineFrom = GetListCtrl()->GetScrollPos(wxVERTICAL);
cf1dfa6b 4226
152c57f2
VZ
4227 // this may happen if SetScrollbars() hadn't been called yet
4228 if ( m_lineFrom >= count )
bcc0da5c 4229 m_lineFrom = count - 1;
cf1dfa6b 4230
6522713c
VZ
4231 // we redraw one extra line but this is needed to make the redrawing
4232 // logic work when there is a fractional number of lines on screen
4233 m_lineTo = m_lineFrom + m_linesPerPage;
4234 if ( m_lineTo >= count )
4235 m_lineTo = count - 1;
4236 }
4237 else // empty control
4238 {
4239 m_lineFrom = 0;
4240 m_lineTo = (size_t)-1;
4241 }
cf1dfa6b 4242 }
b54e41c5 4243
ae167d2f
VZ
4244 wxASSERT_MSG( IsEmpty() ||
4245 (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
9a83f860 4246 wxT("GetVisibleLinesRange() returns incorrect result") );
6b4a8d93 4247
b54e41c5
VZ
4248 if ( from )
4249 *from = m_lineFrom;
4250 if ( to )
4251 *to = m_lineTo;
7c74e7fe
SC
4252}
4253
c801d85f 4254// -------------------------------------------------------------------------------------
3cd94a0d 4255// wxGenericListCtrl
c801d85f
KB
4256// -------------------------------------------------------------------------------------
4257
3cd94a0d
JS
4258IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl, wxControl)
4259
3cd94a0d
JS
4260BEGIN_EVENT_TABLE(wxGenericListCtrl,wxControl)
4261 EVT_SIZE(wxGenericListCtrl::OnSize)
06cd40a8 4262 EVT_SCROLLWIN(wxGenericListCtrl::OnScroll)
c801d85f
KB
4263END_EVENT_TABLE()
4264
06cd40a8 4265void wxGenericListCtrl::Init()
c801d85f 4266{
d3b9f782
VZ
4267 m_imageListNormal = NULL;
4268 m_imageListSmall = NULL;
4269 m_imageListState = NULL;
cf1dfa6b
VZ
4270
4271 m_ownsImageListNormal =
4272 m_ownsImageListSmall =
ca65c044 4273 m_ownsImageListState = false;
cf1dfa6b 4274
d3b9f782
VZ
4275 m_mainWin = NULL;
4276 m_headerWin = NULL;
c801d85f
KB
4277}
4278
3cd94a0d 4279wxGenericListCtrl::~wxGenericListCtrl()
c801d85f 4280{
cf1dfa6b
VZ
4281 if (m_ownsImageListNormal)
4282 delete m_imageListNormal;
4283 if (m_ownsImageListSmall)
4284 delete m_imageListSmall;
4285 if (m_ownsImageListState)
4286 delete m_imageListState;
4287}
4288
06cd40a8 4289void wxGenericListCtrl::CreateOrDestroyHeaderWindowAsNeeded()
f8252483 4290{
06cd40a8
RR
4291 bool needs_header = HasHeader();
4292 bool has_header = (m_headerWin != NULL);
7d7b3f69 4293
06cd40a8
RR
4294 if (needs_header == has_header)
4295 return;
f8252483 4296
06cd40a8
RR
4297 if (needs_header)
4298 {
4299 m_headerWin = new wxListHeaderWindow
cf1dfa6b 4300 (
ca65c044 4301 this, wxID_ANY, m_mainWin,
c47addef 4302 wxPoint(0,0),
c1fae0d0
VZ
4303 wxSize
4304 (
4305 GetClientSize().x,
4306 wxRendererNative::Get().GetHeaderButtonHeight(this)
4307 ),
cf1dfa6b
VZ
4308 wxTAB_TRAVERSAL
4309 );
7d7b3f69 4310
f1c40652 4311#if defined( __WXMAC__ )
7eb8aeb8 4312 static wxFont font( wxOSX_SYSTEM_FONT_SMALL );
06cd40a8
RR
4313 m_headerWin->SetFont( font );
4314#endif
7d7b3f69 4315
06cd40a8
RR
4316 GetSizer()->Prepend( m_headerWin, 0, wxGROW );
4317 }
4318 else
4319 {
4320 GetSizer()->Detach( m_headerWin );
7d7b3f69 4321
5276b0a5 4322 wxDELETE(m_headerWin);
06cd40a8 4323 }
c801d85f
KB
4324}
4325
3cd94a0d 4326bool wxGenericListCtrl::Create(wxWindow *parent,
25e3a937
VZ
4327 wxWindowID id,
4328 const wxPoint &pos,
4329 const wxSize &size,
4330 long style,
25e3a937 4331 const wxValidator &validator,
25e3a937 4332 const wxString &name)
c801d85f 4333{
06cd40a8 4334 Init();
3fc93ebd 4335
c615d649 4336 // just like in other ports, an assert will fail if the user doesn't give any type style:
5ac526c4 4337 wxASSERT_MSG( (style & wxLC_MASK_TYPE),
9a83f860 4338 wxT("wxListCtrl style should have exactly one mode bit set") );
f6bcfd97 4339
06cd40a8 4340 if ( !wxControl::Create( parent, id, pos, size, style|wxVSCROLL|wxHSCROLL, validator, name ) )
ca65c044 4341 return false;
f6bcfd97 4342
dff750b4
RR
4343#ifdef __WXGTK__
4344 style &= ~wxBORDER_MASK;
4345 style |= wxBORDER_THEME;
7d7b3f69 4346#endif
bd8289c1 4347
277ea1b4 4348 m_mainWin = new wxListMainWindow( this, wxID_ANY, wxPoint(0, 0), size, style );
bd8289c1 4349
06cd40a8 4350 SetTargetWindow( m_mainWin );
7d7b3f69 4351
d6a658ff
VZ
4352 // We use the cursor keys for moving the selection, not scrolling, so call
4353 // this method to ensure wxScrollHelperEvtHandler doesn't catch all
4354 // keyboard events forwarded to us from wxListMainWindow.
4355 DisableKeyboardScrolling();
4356
06cd40a8
RR
4357 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
4358 sizer->Add( m_mainWin, 1, wxGROW );
4359 SetSizer( sizer );
7d7b3f69 4360
06cd40a8
RR
4361 CreateOrDestroyHeaderWindowAsNeeded();
4362
4363 SetInitialSize(size);
4364
4365 return true;
4366}
4367
4368wxBorder wxGenericListCtrl::GetDefaultBorder() const
4369{
4370 return wxBORDER_THEME;
4371}
4372
5d55031c 4373#if defined(__WXMSW__) && !defined(__WXWINCE__) && !defined(__WXUNIVERSAL__)
06cd40a8
RR
4374WXLRESULT wxGenericListCtrl::MSWWindowProc(WXUINT nMsg,
4375 WXWPARAM wParam,
4376 WXLPARAM lParam)
4377{
4378 WXLRESULT rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
4379
06cd40a8
RR
4380 // we need to process arrows ourselves for scrolling
4381 if ( nMsg == WM_GETDLGCODE )
309e4c14 4382 {
06cd40a8 4383 rc |= DLGC_WANTARROWS;
309e4c14 4384 }
277ea1b4 4385
06cd40a8
RR
4386 return rc;
4387}
5d55031c 4388#endif // __WXMSW__
277ea1b4 4389
06cd40a8
RR
4390wxSize wxGenericListCtrl::GetSizeAvailableForScrollTarget(const wxSize& size)
4391{
4392 wxSize newsize = size;
4393 if (m_headerWin)
4394 newsize.y -= m_headerWin->GetSize().y;
bd8289c1 4395
06cd40a8
RR
4396 return newsize;
4397}
ca65c044 4398
06cd40a8
RR
4399void wxGenericListCtrl::OnScroll(wxScrollWinEvent& event)
4400{
4401 // update our idea of which lines are shown when we redraw
4402 // the window the next time
4403 m_mainWin->ResetVisibleLinesRange();
4404
4405 HandleOnScroll( event );
4406
4407 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
4408 {
4409 m_headerWin->Refresh();
4410 m_headerWin->Update();
4411 }
e1e955e1 4412}
c801d85f 4413
3cd94a0d 4414void wxGenericListCtrl::SetSingleStyle( long style, bool add )
c801d85f 4415{
b54e41c5 4416 wxASSERT_MSG( !(style & wxLC_VIRTUAL),
9a83f860 4417 wxT("wxLC_VIRTUAL can't be [un]set") );
b54e41c5 4418
f03fc89f 4419 long flag = GetWindowStyle();
bd8289c1 4420
5b077d48
RR
4421 if (add)
4422 {
cf1dfa6b 4423 if (style & wxLC_MASK_TYPE)
b54e41c5 4424 flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
cf1dfa6b
VZ
4425 if (style & wxLC_MASK_ALIGN)
4426 flag &= ~wxLC_MASK_ALIGN;
4427 if (style & wxLC_MASK_SORT)
4428 flag &= ~wxLC_MASK_SORT;
5b077d48 4429 }
c801d85f 4430
5b077d48 4431 if (add)
5b077d48 4432 flag |= style;
5b077d48 4433 else
cf1dfa6b 4434 flag &= ~style;
bd8289c1 4435
b119ee87
VZ
4436 // some styles can be set without recreating everything (as happens in
4437 // SetWindowStyleFlag() which calls wxListMainWindow::DeleteEverything())
4438 if ( !(style & ~(wxLC_HRULES | wxLC_VRULES)) )
4439 {
4440 Refresh();
4441 wxWindow::SetWindowStyleFlag(flag);
4442 }
4443 else
4444 {
4445 SetWindowStyleFlag( flag );
4446 }
e1e955e1 4447}
c801d85f 4448
3cd94a0d 4449void wxGenericListCtrl::SetWindowStyleFlag( long flag )
c801d85f 4450{
476de5ea
VZ
4451 const bool wasInReportView = HasFlag(wxLC_REPORT);
4452
1bab3627
VZ
4453 // update the window style first so that the header is created or destroyed
4454 // corresponding to the new style
4455 wxWindow::SetWindowStyleFlag( flag );
4456
121a3581
RR
4457 if (m_mainWin)
4458 {
476de5ea
VZ
4459 const bool inReportView = (flag & wxLC_REPORT) != 0;
4460 if ( inReportView != wasInReportView )
4461 {
4462 // we need to notify the main window about this change as it must
4463 // update its data structures
4464 m_mainWin->SetReportView(inReportView);
4465 }
4466
06cd40a8 4467 // m_mainWin->DeleteEverything(); wxMSW doesn't do that
a95cdab8 4468
06cd40a8 4469 CreateOrDestroyHeaderWindowAsNeeded();
7d7b3f69 4470
06cd40a8 4471 GetSizer()->Layout();
e1e955e1 4472 }
e1e955e1 4473}
c801d85f 4474
3cd94a0d 4475bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
c801d85f 4476{
5b077d48 4477 m_mainWin->GetColumn( col, item );
ca65c044 4478 return true;
e1e955e1 4479}
c801d85f 4480
3cd94a0d 4481bool wxGenericListCtrl::SetColumn( int col, wxListItem& item )
c801d85f 4482{
5b077d48 4483 m_mainWin->SetColumn( col, item );
ca65c044 4484 return true;
e1e955e1 4485}
c801d85f 4486
3cd94a0d 4487int wxGenericListCtrl::GetColumnWidth( int col ) const
c801d85f 4488{
5b077d48 4489 return m_mainWin->GetColumnWidth( col );
e1e955e1 4490}
c801d85f 4491
3cd94a0d 4492bool wxGenericListCtrl::SetColumnWidth( int col, int width )
c801d85f 4493{
5b077d48 4494 m_mainWin->SetColumnWidth( col, width );
ca65c044 4495 return true;
e1e955e1 4496}
c801d85f 4497
3cd94a0d 4498int wxGenericListCtrl::GetCountPerPage() const
c801d85f
KB
4499{
4500 return m_mainWin->GetCountPerPage(); // different from Windows ?
e1e955e1 4501}
c801d85f 4502
3cd94a0d 4503bool wxGenericListCtrl::GetItem( wxListItem &info ) const
c801d85f 4504{
5b077d48 4505 m_mainWin->GetItem( info );
ca65c044 4506 return true;
e1e955e1 4507}
c801d85f 4508
3cd94a0d 4509bool wxGenericListCtrl::SetItem( wxListItem &info )
c801d85f 4510{
5b077d48 4511 m_mainWin->SetItem( info );
ca65c044 4512 return true;
e1e955e1 4513}
c801d85f 4514
3cd94a0d 4515long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
c801d85f 4516{
5b077d48
RR
4517 wxListItem info;
4518 info.m_text = label;
4519 info.m_mask = wxLIST_MASK_TEXT;
4520 info.m_itemId = index;
4521 info.m_col = col;
4522 if ( imageId > -1 )
4523 {
4524 info.m_image = imageId;
4525 info.m_mask |= wxLIST_MASK_IMAGE;
277ea1b4
DS
4526 }
4527
5b077d48 4528 m_mainWin->SetItem(info);
ca65c044 4529 return true;
e1e955e1 4530}
c801d85f 4531
3cd94a0d 4532int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
c801d85f 4533{
5b077d48 4534 return m_mainWin->GetItemState( item, stateMask );
e1e955e1 4535}
c801d85f 4536
3cd94a0d 4537bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
c801d85f 4538{
5b077d48 4539 m_mainWin->SetItemState( item, state, stateMask );
ca65c044 4540 return true;
e1e955e1 4541}
c801d85f 4542
aba3d35e
VZ
4543bool
4544wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
06db67bc
RD
4545{
4546 return SetItemColumnImage(item, 0, image);
4547}
4548
4549bool
4550wxGenericListCtrl::SetItemColumnImage( long item, long column, int image )
c3627a00
JS
4551{
4552 wxListItem info;
4553 info.m_image = image;
4554 info.m_mask = wxLIST_MASK_IMAGE;
4555 info.m_itemId = item;
06db67bc 4556 info.m_col = column;
c3627a00
JS
4557 m_mainWin->SetItem( info );
4558 return true;
4559}
c801d85f 4560
b6812a6f 4561wxString wxGenericListCtrl::GetItemText( long item, int col ) const
c801d85f 4562{
b6812a6f 4563 return m_mainWin->GetItemText(item, col);
e1e955e1 4564}
c801d85f 4565
3cd94a0d 4566void wxGenericListCtrl::SetItemText( long item, const wxString& str )
c801d85f 4567{
62d89eb4 4568 m_mainWin->SetItemText(item, str);
e1e955e1 4569}
c801d85f 4570
81ce36aa 4571wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const
c801d85f 4572{
5b077d48 4573 wxListItem info;
39a7f085 4574 info.m_mask = wxLIST_MASK_DATA;
5b077d48
RR
4575 info.m_itemId = item;
4576 m_mainWin->GetItem( info );
4577 return info.m_data;
e1e955e1 4578}
c801d85f 4579
9fcd0bf7 4580bool wxGenericListCtrl::SetItemPtrData( long item, wxUIntPtr data )
c801d85f 4581{
5b077d48
RR
4582 wxListItem info;
4583 info.m_mask = wxLIST_MASK_DATA;
4584 info.m_itemId = item;
4585 info.m_data = data;
4586 m_mainWin->SetItem( info );
ca65c044 4587 return true;
e1e955e1 4588}
c801d85f 4589
11ebea16
VZ
4590wxRect wxGenericListCtrl::GetViewRect() const
4591{
4592 return m_mainWin->GetViewRect();
4593}
4594
e974c5d2
VZ
4595bool wxGenericListCtrl::GetItemRect(long item, wxRect& rect, int code) const
4596{
4597 return GetSubItemRect(item, wxLIST_GETSUBITEMRECT_WHOLEITEM, rect, code);
4598}
4599
4600bool wxGenericListCtrl::GetSubItemRect(long item,
4601 long subItem,
4602 wxRect& rect,
4603 int WXUNUSED(code)) const
c801d85f 4604{
e974c5d2
VZ
4605 if ( !m_mainWin->GetSubItemRect( item, subItem, rect ) )
4606 return false;
4607
7e6874c0 4608 if ( m_mainWin->HasHeader() )
c1fae0d0 4609 rect.y += m_headerWin->GetSize().y + 1;
e974c5d2 4610
ca65c044 4611 return true;
e1e955e1 4612}
c801d85f 4613
3cd94a0d 4614bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
c801d85f 4615{
5b077d48 4616 m_mainWin->GetItemPosition( item, pos );
ca65c044 4617 return true;
e1e955e1 4618}
c801d85f 4619
3cd94a0d 4620bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
c801d85f 4621{
e974c5d2 4622 return false;
e1e955e1 4623}
c801d85f 4624
3cd94a0d 4625int wxGenericListCtrl::GetItemCount() const
c801d85f 4626{
5b077d48 4627 return m_mainWin->GetItemCount();
e1e955e1 4628}
c801d85f 4629
3cd94a0d 4630int wxGenericListCtrl::GetColumnCount() const
92976ab6 4631{
5b077d48 4632 return m_mainWin->GetColumnCount();
92976ab6
RR
4633}
4634
3cd94a0d 4635void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
33d0b396 4636{
5b077d48 4637 m_mainWin->SetItemSpacing( spacing, isSmall );
e1e955e1 4638}
33d0b396 4639
5db8d758
VZ
4640wxSize wxGenericListCtrl::GetItemSpacing() const
4641{
f58fc140 4642 const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON));
5db8d758
VZ
4643
4644 return wxSize(spacing, spacing);
4645}
4646
82d492f1 4647#if WXWIN_COMPATIBILITY_2_6
3cd94a0d 4648int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const
c801d85f 4649{
5b077d48 4650 return m_mainWin->GetItemSpacing( isSmall );
e1e955e1 4651}
82d492f1 4652#endif // WXWIN_COMPATIBILITY_2_6
c801d85f 4653
3cd94a0d 4654void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
5b98eb2f
RL
4655{
4656 wxListItem info;
4657 info.m_itemId = item;
4658 info.SetTextColour( col );
4659 m_mainWin->SetItem( info );
4660}
4661
3cd94a0d 4662wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
5b98eb2f
RL
4663{
4664 wxListItem info;
4665 info.m_itemId = item;
4666 m_mainWin->GetItem( info );
4667 return info.GetTextColour();
4668}
4669
3cd94a0d 4670void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
5b98eb2f
RL
4671{
4672 wxListItem info;
4673 info.m_itemId = item;
4674 info.SetBackgroundColour( col );
4675 m_mainWin->SetItem( info );
4676}
4677
3cd94a0d 4678wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
5b98eb2f
RL
4679{
4680 wxListItem info;
4681 info.m_itemId = item;
4682 m_mainWin->GetItem( info );
4683 return info.GetBackgroundColour();
4684}
4685
35c2acd4
MW
4686void wxGenericListCtrl::SetItemFont( long item, const wxFont &f )
4687{
4688 wxListItem info;
4689 info.m_itemId = item;
4690 info.SetFont( f );
4691 m_mainWin->SetItem( info );
4692}
4693
4694wxFont wxGenericListCtrl::GetItemFont( long item ) const
4695{
4696 wxListItem info;
4697 info.m_itemId = item;
4698 m_mainWin->GetItem( info );
4699 return info.GetFont();
4700}
4701
3cd94a0d 4702int wxGenericListCtrl::GetSelectedItemCount() const
c801d85f 4703{
5b077d48 4704 return m_mainWin->GetSelectedItemCount();
e1e955e1 4705}
c801d85f 4706
3cd94a0d 4707wxColour wxGenericListCtrl::GetTextColour() const
c801d85f 4708{
0530737d 4709 return GetForegroundColour();
e1e955e1 4710}
c801d85f 4711
3cd94a0d 4712void wxGenericListCtrl::SetTextColour(const wxColour& col)
c801d85f 4713{
0530737d 4714 SetForegroundColour(col);
e1e955e1 4715}
c801d85f 4716
3cd94a0d 4717long wxGenericListCtrl::GetTopItem() const
c801d85f 4718{
2b5f62a0
VZ
4719 size_t top;
4720 m_mainWin->GetVisibleLinesRange(&top, NULL);
4721 return (long)top;
e1e955e1 4722}
c801d85f 4723
3cd94a0d 4724long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
c801d85f 4725{
5b077d48 4726 return m_mainWin->GetNextItem( item, geom, state );
e1e955e1 4727}
c801d85f 4728
8a3e173a 4729wxImageList *wxGenericListCtrl::GetImageList(int which) const
c801d85f 4730{
5b077d48 4731 if (which == wxIMAGE_LIST_NORMAL)
5b077d48 4732 return m_imageListNormal;
5b077d48 4733 else if (which == wxIMAGE_LIST_SMALL)
5b077d48 4734 return m_imageListSmall;
5b077d48 4735 else if (which == wxIMAGE_LIST_STATE)
5b077d48 4736 return m_imageListState;
277ea1b4 4737
d3b9f782 4738 return NULL;
e1e955e1 4739}
c801d85f 4740
8a3e173a 4741void wxGenericListCtrl::SetImageList( wxImageList *imageList, int which )
c801d85f 4742{
2e12c11a
VS
4743 if ( which == wxIMAGE_LIST_NORMAL )
4744 {
277ea1b4
DS
4745 if (m_ownsImageListNormal)
4746 delete m_imageListNormal;
2e12c11a 4747 m_imageListNormal = imageList;
ca65c044 4748 m_ownsImageListNormal = false;
2e12c11a
VS
4749 }
4750 else if ( which == wxIMAGE_LIST_SMALL )
4751 {
277ea1b4
DS
4752 if (m_ownsImageListSmall)
4753 delete m_imageListSmall;
2e12c11a 4754 m_imageListSmall = imageList;
ca65c044 4755 m_ownsImageListSmall = false;
2e12c11a
VS
4756 }
4757 else if ( which == wxIMAGE_LIST_STATE )
4758 {
277ea1b4
DS
4759 if (m_ownsImageListState)
4760 delete m_imageListState;
2e12c11a 4761 m_imageListState = imageList;
ca65c044 4762 m_ownsImageListState = false;
2e12c11a
VS
4763 }
4764
5b077d48 4765 m_mainWin->SetImageList( imageList, which );
e1e955e1 4766}
c801d85f 4767
8a3e173a 4768void wxGenericListCtrl::AssignImageList(wxImageList *imageList, int which)
2e12c11a
VS
4769{
4770 SetImageList(imageList, which);
4771 if ( which == wxIMAGE_LIST_NORMAL )
ca65c044 4772 m_ownsImageListNormal = true;
2e12c11a 4773 else if ( which == wxIMAGE_LIST_SMALL )
ca65c044 4774 m_ownsImageListSmall = true;
2e12c11a 4775 else if ( which == wxIMAGE_LIST_STATE )
ca65c044 4776 m_ownsImageListState = true;
2e12c11a
VS
4777}
4778
3cd94a0d 4779bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
c801d85f 4780{
5b077d48 4781 return 0;
e1e955e1 4782}
c801d85f 4783
3cd94a0d 4784bool wxGenericListCtrl::DeleteItem( long item )
c801d85f 4785{
5b077d48 4786 m_mainWin->DeleteItem( item );
ca65c044 4787 return true;
e1e955e1 4788}
c801d85f 4789
3cd94a0d 4790bool wxGenericListCtrl::DeleteAllItems()
c801d85f 4791{
5b077d48 4792 m_mainWin->DeleteAllItems();
ca65c044 4793 return true;
e1e955e1 4794}
c801d85f 4795
3cd94a0d 4796bool wxGenericListCtrl::DeleteAllColumns()
bd8289c1 4797{
24b9f055
VZ
4798 size_t count = m_mainWin->m_columns.GetCount();
4799 for ( size_t n = 0; n < count; n++ )
277ea1b4 4800 DeleteColumn( 0 );
ca65c044 4801 return true;
4f22cf8d
RR
4802}
4803
3cd94a0d 4804void wxGenericListCtrl::ClearAll()
4f22cf8d 4805{
5b077d48 4806 m_mainWin->DeleteEverything();
bd8289c1
VZ
4807}
4808
3cd94a0d 4809bool wxGenericListCtrl::DeleteColumn( int col )
c801d85f 4810{
5b077d48 4811 m_mainWin->DeleteColumn( col );
40c08808
VZ
4812
4813 // if we don't have the header any longer, we need to relayout the window
06cd40a8 4814 // if ( !GetColumnCount() )
7d7b3f69 4815
ca65c044 4816 return true;
e1e955e1 4817}
c801d85f 4818
26e8da41
VZ
4819wxTextCtrl *wxGenericListCtrl::EditLabel(long item,
4820 wxClassInfo* textControlClass)
4821{
4822 return m_mainWin->EditLabel( item, textControlClass );
4823}
4824
4825wxTextCtrl *wxGenericListCtrl::GetEditControl() const
c801d85f 4826{
26e8da41 4827 return m_mainWin->GetEditControl();
e1e955e1 4828}
c801d85f 4829
3cd94a0d 4830bool wxGenericListCtrl::EnsureVisible( long item )
c801d85f 4831{
5b077d48 4832 m_mainWin->EnsureVisible( item );
ca65c044 4833 return true;
e1e955e1 4834}
c801d85f 4835
277ea1b4 4836long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
c801d85f 4837{
5b077d48 4838 return m_mainWin->FindItem( start, str, partial );
e1e955e1 4839}
c801d85f 4840
81ce36aa 4841long wxGenericListCtrl::FindItem( long start, wxUIntPtr data )
c801d85f 4842{
5b077d48 4843 return m_mainWin->FindItem( start, data );
e1e955e1 4844}
c801d85f 4845
8158e0e1 4846long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt,
debe6624 4847 int WXUNUSED(direction))
c801d85f 4848{
8158e0e1 4849 return m_mainWin->FindItem( pt );
e1e955e1 4850}
c801d85f 4851
164a7972 4852// TODO: sub item hit testing
be0e5d69 4853long wxGenericListCtrl::HitTest(const wxPoint& point, int& flags, long *) const
c801d85f 4854{
5b077d48 4855 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
e1e955e1 4856}
c801d85f 4857
3cd94a0d 4858long wxGenericListCtrl::InsertItem( wxListItem& info )
c801d85f 4859{
5b077d48 4860 m_mainWin->InsertItem( info );
2ebcd5f5 4861 return info.m_itemId;
e1e955e1 4862}
c801d85f 4863
3cd94a0d 4864long wxGenericListCtrl::InsertItem( long index, const wxString &label )
c801d85f 4865{
51cc4dad
RR
4866 wxListItem info;
4867 info.m_text = label;
4868 info.m_mask = wxLIST_MASK_TEXT;
4869 info.m_itemId = index;
4870 return InsertItem( info );
e1e955e1 4871}
c801d85f 4872
3cd94a0d 4873long wxGenericListCtrl::InsertItem( long index, int imageIndex )
c801d85f 4874{
51cc4dad
RR
4875 wxListItem info;
4876 info.m_mask = wxLIST_MASK_IMAGE;
4877 info.m_image = imageIndex;
4878 info.m_itemId = index;
4879 return InsertItem( info );
e1e955e1 4880}
c801d85f 4881
3cd94a0d 4882long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
c801d85f 4883{
51cc4dad
RR
4884 wxListItem info;
4885 info.m_text = label;
4886 info.m_image = imageIndex;
4887 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
4888 info.m_itemId = index;
4889 return InsertItem( info );
e1e955e1 4890}
c801d85f 4891
3cd94a0d 4892long wxGenericListCtrl::InsertColumn( long col, wxListItem &item )
c801d85f 4893{
9a83f860 4894 wxCHECK_MSG( InReportView(), -1, wxT("can't add column in non report mode") );
40c08808 4895
51cc4dad 4896 m_mainWin->InsertColumn( col, item );
40c08808 4897
52d2a5e4
FM
4898 // NOTE: if wxLC_NO_HEADER was given, then we are in report view mode but
4899 // still have m_headerWin==NULL
4900 if (m_headerWin)
4901 m_headerWin->Refresh();
25e3a937 4902
51cc4dad 4903 return 0;
e1e955e1 4904}
c801d85f 4905
3cd94a0d 4906long wxGenericListCtrl::InsertColumn( long col, const wxString &heading,
debe6624 4907 int format, int width )
c801d85f 4908{
51cc4dad
RR
4909 wxListItem item;
4910 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
4911 item.m_text = heading;
4912 if (width >= -2)
4913 {
4914 item.m_mask |= wxLIST_MASK_WIDTH;
4915 item.m_width = width;
4916 }
277ea1b4 4917
51cc4dad 4918 item.m_format = format;
c801d85f 4919
51cc4dad 4920 return InsertColumn( col, item );
e1e955e1 4921}
c801d85f 4922
66a9201d 4923bool wxGenericListCtrl::ScrollList( int dx, int dy )
c801d85f 4924{
66a9201d 4925 return m_mainWin->ScrollList(dx, dy);
e1e955e1 4926}
c801d85f
KB
4927
4928// Sort items.
4929// fn is a function which takes 3 long arguments: item1, item2, data.
4930// item1 is the long data associated with a first item (NOT the index).
4931// item2 is the long data associated with a second item (NOT the index).
4932// data is the same value as passed to SortItems.
4933// The return value is a negative number if the first item should precede the second
4934// item, a positive number of the second item should precede the first,
4935// or zero if the two items are equivalent.
4936// data is arbitrary data to be passed to the sort function.
4937
b18e2046 4938bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn, wxIntPtr data )
c801d85f 4939{
51cc4dad 4940 m_mainWin->SortItems( fn, data );
ca65c044 4941 return true;
e1e955e1 4942}
c801d85f 4943
cf1dfa6b 4944// ----------------------------------------------------------------------------
b54e41c5 4945// event handlers
cf1dfa6b
VZ
4946// ----------------------------------------------------------------------------
4947
3cd94a0d 4948void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
53010e52 4949{
06cd40a8 4950 if (!m_mainWin) return;
53010e52 4951
06cd40a8
RR
4952 // We need to override OnSize so that our scrolled
4953 // window a) does call Layout() to use sizers for
4954 // positioning the controls but b) does not query
4955 // the sizer for their size and use that for setting
4956 // the scrollable area as set that ourselves by
4957 // calling SetScrollbar() further down.
a95cdab8 4958
06cd40a8 4959 Layout();
bd8289c1 4960
06cd40a8 4961 m_mainWin->RecalculatePositions();
7d7b3f69 4962
06cd40a8 4963 AdjustScrollbars();
b54e41c5 4964}
cf1dfa6b 4965
5180055b 4966void wxGenericListCtrl::OnInternalIdle()
b54e41c5 4967{
5180055b 4968 wxWindow::OnInternalIdle();
86351e4a 4969
06cd40a8
RR
4970 if (m_mainWin->m_dirty)
4971 m_mainWin->RecalculatePositions();
e1e955e1 4972}
53010e52 4973
cf1dfa6b
VZ
4974// ----------------------------------------------------------------------------
4975// font/colours
4976// ----------------------------------------------------------------------------
4977
3cd94a0d 4978bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
bd8289c1 4979{
51cc4dad
RR
4980 if (m_mainWin)
4981 {
4982 m_mainWin->SetBackgroundColour( colour );
ca65c044 4983 m_mainWin->m_dirty = true;
51cc4dad 4984 }
004fd0c8 4985
ca65c044 4986 return true;
e4d06860
RR
4987}
4988
3cd94a0d 4989bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
bd8289c1 4990{
f03fc89f 4991 if ( !wxWindow::SetForegroundColour( colour ) )
ca65c044 4992 return false;
004fd0c8 4993
51cc4dad
RR
4994 if (m_mainWin)
4995 {
4996 m_mainWin->SetForegroundColour( colour );
ca65c044 4997 m_mainWin->m_dirty = true;
51cc4dad 4998 }
004fd0c8 4999
51cc4dad 5000 if (m_headerWin)
51cc4dad 5001 m_headerWin->SetForegroundColour( colour );
f03fc89f 5002
ca65c044 5003 return true;
e4d06860 5004}
bd8289c1 5005
3cd94a0d 5006bool wxGenericListCtrl::SetFont( const wxFont &font )
bd8289c1 5007{
f03fc89f 5008 if ( !wxWindow::SetFont( font ) )
ca65c044 5009 return false;
004fd0c8 5010
51cc4dad
RR
5011 if (m_mainWin)
5012 {
5013 m_mainWin->SetFont( font );
ca65c044 5014 m_mainWin->m_dirty = true;
51cc4dad 5015 }
004fd0c8 5016
51cc4dad
RR
5017 if (m_headerWin)
5018 {
5019 m_headerWin->SetFont( font );
06cd40a8 5020 // CalculateAndSetHeaderHeight();
51cc4dad 5021 }
f03fc89f 5022
f8252483
JS
5023 Refresh();
5024
ca65c044 5025 return true;
e4d06860 5026}
c801d85f 5027
277ea1b4 5028// static
35d4c967
RD
5029wxVisualAttributes
5030wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
5031{
5032#if _USE_VISATTR
5033 // Use the same color scheme as wxListBox
5034 return wxListBox::GetClassDefaultAttributes(variant);
5035#else
e7045784 5036 wxUnusedVar(variant);
35d4c967 5037 wxVisualAttributes attr;
9f2968ad 5038 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT);
35d4c967
RD
5039 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
5040 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
5041 return attr;
5042#endif
5043}
5044
cf1dfa6b
VZ
5045// ----------------------------------------------------------------------------
5046// methods forwarded to m_mainWin
5047// ----------------------------------------------------------------------------
5048
efbb7287
VZ
5049#if wxUSE_DRAG_AND_DROP
5050
3cd94a0d 5051void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
efbb7287
VZ
5052{
5053 m_mainWin->SetDropTarget( dropTarget );
5054}
5055
3cd94a0d 5056wxDropTarget *wxGenericListCtrl::GetDropTarget() const
efbb7287
VZ
5057{
5058 return m_mainWin->GetDropTarget();
5059}
5060
277ea1b4 5061#endif
efbb7287 5062
3cd94a0d 5063bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
efbb7287 5064{
ca65c044 5065 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false;
efbb7287
VZ
5066}
5067
3cd94a0d 5068wxColour wxGenericListCtrl::GetBackgroundColour() const
efbb7287
VZ
5069{
5070 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5071}
5072
3cd94a0d 5073wxColour wxGenericListCtrl::GetForegroundColour() const
efbb7287
VZ
5074{
5075 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5076}
5077
3cd94a0d 5078bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
efbb7287 5079{
3a8c693a 5080#if wxUSE_MENUS
efbb7287 5081 return m_mainWin->PopupMenu( menu, x, y );
3a8c693a 5082#else
ca65c044 5083 return false;
277ea1b4 5084#endif
efbb7287
VZ
5085}
5086
7447bdd7
VZ
5087void wxGenericListCtrl::DoClientToScreen( int *x, int *y ) const
5088{
5d584acb 5089 m_mainWin->DoClientToScreen(x, y);
7447bdd7
VZ
5090}
5091
5092void wxGenericListCtrl::DoScreenToClient( int *x, int *y ) const
5093{
5d584acb 5094 m_mainWin->DoScreenToClient(x, y);
7447bdd7
VZ
5095}
5096
3cd94a0d 5097void wxGenericListCtrl::SetFocus()
efbb7287 5098{
277ea1b4
DS
5099 // The test in window.cpp fails as we are a composite
5100 // window, so it checks against "this", but not m_mainWin.
66370e38 5101 if ( DoFindFocus() != this )
efbb7287
VZ
5102 m_mainWin->SetFocus();
5103}
1e6feb95 5104
3872d96d
RD
5105wxSize wxGenericListCtrl::DoGetBestSize() const
5106{
5107 // Something is better than nothing...
5108 // 100x80 is what the MSW version will get from the default
5109 // wxControl::DoGetBestSize
277ea1b4 5110 return wxSize(100, 80);
3872d96d
RD
5111}
5112
2c1f73ee
VZ
5113// ----------------------------------------------------------------------------
5114// virtual list control support
5115// ----------------------------------------------------------------------------
5116
3cd94a0d 5117wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2c1f73ee
VZ
5118{
5119 // this is a pure virtual function, in fact - which is not really pure
5120 // because the controls which are not virtual don't need to implement it
9a83f860 5121 wxFAIL_MSG( wxT("wxGenericListCtrl::OnGetItemText not supposed to be called") );
2c1f73ee
VZ
5122
5123 return wxEmptyString;
5124}
5125
3cd94a0d 5126int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2c1f73ee 5127{
611a725e
RD
5128 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
5129 -1,
208458a7 5130 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
2c1f73ee
VZ
5131 return -1;
5132}
5133
208458a7
RD
5134int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const
5135{
5136 if (!column)
5137 return OnGetItemImage(item);
5138
5139 return -1;
08a175ce 5140}
208458a7 5141
999836aa
VZ
5142wxListItemAttr *
5143wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
6c02c329
VZ
5144{
5145 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
9a83f860 5146 wxT("invalid item index in OnGetItemAttr()") );
6c02c329
VZ
5147
5148 // no attributes by default
5149 return NULL;
5150}
5151
3cd94a0d 5152void wxGenericListCtrl::SetItemCount(long count)
2c1f73ee 5153{
9a83f860 5154 wxASSERT_MSG( IsVirtual(), wxT("this is for virtual controls only") );
2c1f73ee
VZ
5155
5156 m_mainWin->SetItemCount(count);
5157}
5158
3cd94a0d 5159void wxGenericListCtrl::RefreshItem(long item)
1a6cb56f
VZ
5160{
5161 m_mainWin->RefreshLine(item);
5162}
5163
3cd94a0d 5164void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
1a6cb56f
VZ
5165{
5166 m_mainWin->RefreshLines(itemFrom, itemTo);
5167}
5168
277ea1b4
DS
5169// Generic wxListCtrl is more or less a container for two other
5170// windows which drawings are done upon. These are namely
5171// 'm_headerWin' and 'm_mainWin'.
5172// Here we override 'virtual wxWindow::Refresh()' to mimic the
5173// behaviour wxListCtrl has under wxMSW.
5174//
2f1e3c46
VZ
5175void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5176{
5177 if (!rect)
5178 {
5179 // The easy case, no rectangle specified.
5180 if (m_headerWin)
5181 m_headerWin->Refresh(eraseBackground);
5182
5183 if (m_mainWin)
5184 m_mainWin->Refresh(eraseBackground);
5185 }
5186 else
5187 {
5188 // Refresh the header window
5189 if (m_headerWin)
5190 {
5191 wxRect rectHeader = m_headerWin->GetRect();
5192 rectHeader.Intersect(*rect);
5193 if (rectHeader.GetWidth() && rectHeader.GetHeight())
5194 {
5195 int x, y;
5196 m_headerWin->GetPosition(&x, &y);
5197 rectHeader.Offset(-x, -y);
5198 m_headerWin->Refresh(eraseBackground, &rectHeader);
5199 }
2f1e3c46
VZ
5200 }
5201
5202 // Refresh the main window
5203 if (m_mainWin)
5204 {
5205 wxRect rectMain = m_mainWin->GetRect();
5206 rectMain.Intersect(*rect);
5207 if (rectMain.GetWidth() && rectMain.GetHeight())
5208 {
5209 int x, y;
5210 m_mainWin->GetPosition(&x, &y);
5211 rectMain.Offset(-x, -y);
5212 m_mainWin->Refresh(eraseBackground, &rectMain);
5213 }
5214 }
5215 }
5216}
5217
e1983cad
VZ
5218void wxGenericListCtrl::Update()
5219{
5220 if ( m_mainWin )
5221 {
5222 if ( m_mainWin->m_dirty )
5223 m_mainWin->RecalculatePositions();
5224
5225 m_mainWin->Update();
5226 }
5227
5228 if ( m_headerWin )
5229 m_headerWin->Update();
5230}
5231
1e6feb95 5232#endif // wxUSE_LISTCTRL