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