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