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