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