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