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