]> git.saurik.com Git - wxWidgets.git/blame - src/generic/listctrl.cpp
Applied patch [ 1427626 ] Fix wxFileConfig so that it still works after DeleteAll()
[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
cf1dfa6b
VZ
523 bool HasFlag(int flag) const { return m_parent->HasFlag(flag); }
524
1370703e 525 // return true if this is a virtual list control
cf1dfa6b
VZ
526 bool IsVirtual() const { return HasFlag(wxLC_VIRTUAL); }
527
5cd89174
VZ
528 // return true if the control is in report mode
529 bool InReportView() const { return HasFlag(wxLC_REPORT); }
530
b54e41c5
VZ
531 // return true if we are in single selection mode, false if multi sel
532 bool IsSingleSel() const { return HasFlag(wxLC_SINGLE_SEL); }
533
cf1dfa6b
VZ
534 // do we have a header window?
535 bool HasHeader() const
b5d43d1d 536 { return InReportView() && !HasFlag(wxLC_NO_HEADER); }
1370703e 537
b54e41c5 538 void HighlightAll( bool on );
cf1dfa6b
VZ
539
540 // all these functions only do something if the line is currently visible
541
ca65c044
WS
542 // change the line "selected" state, return true if it really changed
543 bool HighlightLine( size_t line, bool highlight = true);
b54e41c5
VZ
544
545 // as HighlightLine() but do it for the range of lines: this is incredibly
546 // more efficient for virtual list controls!
547 //
548 // NB: unlike HighlightLine() this one does refresh the lines on screen
ca65c044 549 void HighlightLines( size_t lineFrom, size_t lineTo, bool on = true );
cf1dfa6b
VZ
550
551 // toggle the line state and refresh it
b54e41c5
VZ
552 void ReverseHighlight( size_t line )
553 { HighlightLine(line, !IsHighlighted(line)); RefreshLine(line); }
cf1dfa6b 554
6b4a8d93
VZ
555 // return true if the line is highlighted
556 bool IsHighlighted(size_t line) const;
557
cf1dfa6b
VZ
558 // refresh one or several lines at once
559 void RefreshLine( size_t line );
560 void RefreshLines( size_t lineFrom, size_t lineTo );
561
49ecb029
VZ
562 // refresh all selected items
563 void RefreshSelected();
564
6b4a8d93
VZ
565 // refresh all lines below the given one: the difference with
566 // RefreshLines() is that the index here might not be a valid one (happens
567 // when the last line is deleted)
568 void RefreshAfter( size_t lineFrom );
efbb7287 569
5cd89174
VZ
570 // the methods which are forwarded to wxListLineData itself in list/icon
571 // modes but are here because the lines don't store their positions in the
572 // report mode
573
574 // get the bound rect for the entire line
575 wxRect GetLineRect(size_t line) const;
576
577 // get the bound rect of the label
578 wxRect GetLineLabelRect(size_t line) const;
579
580 // get the bound rect of the items icon (only may be called if we do have
581 // an icon!)
582 wxRect GetLineIconRect(size_t line) const;
583
584 // get the rect to be highlighted when the item has focus
585 wxRect GetLineHighlightRect(size_t line) const;
586
587 // get the size of the total line rect
588 wxSize GetLineSize(size_t line) const
589 { return GetLineRect(line).GetSize(); }
590
591 // return the hit code for the corresponding position (in this line)
592 long HitTestLine(size_t line, int x, int y) const;
593
34bbbc27
VZ
594 // bring the selected item into view, scrolling to it if necessary
595 void MoveToItem(size_t item);
596
597 // bring the current item into view
598 void MoveToFocus() { MoveToItem(m_current); }
599
c5c528fc 600 // start editing the label of the given item
efbb7287 601 void EditLabel( long item );
c5c528fc
VZ
602
603 // suspend/resume redrawing the control
604 void Freeze();
605 void Thaw();
a77ec46d 606
efbb7287 607 void OnRenameTimer();
62d89eb4 608 bool OnRenameAccept(size_t itemEdit, const wxString& value);
86586459 609 void OnRenameCancelled(size_t itemEdit);
efbb7287
VZ
610
611 void OnMouse( wxMouseEvent &event );
cf1dfa6b
VZ
612
613 // called to switch the selection from the current item to newCurrent,
614 void OnArrowChar( size_t newCurrent, const wxKeyEvent& event );
615
efbb7287
VZ
616 void OnChar( wxKeyEvent &event );
617 void OnKeyDown( wxKeyEvent &event );
618 void OnSetFocus( wxFocusEvent &event );
619 void OnKillFocus( wxFocusEvent &event );
277ea1b4 620 void OnScroll( wxScrollWinEvent& event );
f6bcfd97 621
cf1dfa6b
VZ
622 void OnPaint( wxPaintEvent &event );
623
efbb7287 624 void DrawImage( int index, wxDC *dc, int x, int y );
5cd89174
VZ
625 void GetImageSize( int index, int &width, int &height ) const;
626 int GetTextLength( const wxString &s ) const;
efbb7287 627
80020b62 628 void SetImageList( wxImageListType *imageList, int which );
ca65c044
WS
629 void SetItemSpacing( int spacing, bool isSmall = false );
630 int GetItemSpacing( bool isSmall = false );
cf1dfa6b 631
efbb7287
VZ
632 void SetColumn( int col, wxListItem &item );
633 void SetColumnWidth( int col, int width );
cf1dfa6b
VZ
634 void GetColumn( int col, wxListItem &item ) const;
635 int GetColumnWidth( int col ) const;
636 int GetColumnCount() const { return m_columns.GetCount(); }
637
638 // returns the sum of the heights of all columns
639 int GetHeaderWidth() const;
640
5cd89174 641 int GetCountPerPage() const;
cf1dfa6b 642
efbb7287 643 void SetItem( wxListItem &item );
62d89eb4 644 void GetItem( wxListItem &item ) const;
efbb7287 645 void SetItemState( long item, long state, long stateMask );
0afa5859 646 void SetItemStateAll( long state, long stateMask );
62d89eb4
VZ
647 int GetItemState( long item, long stateMask ) const;
648 void GetItemRect( long index, wxRect &rect ) const;
f58fc140 649 wxRect GetViewRect() const;
62d89eb4
VZ
650 bool GetItemPosition( long item, wxPoint& pos ) const;
651 int GetSelectedItemCount() const;
652
653 wxString GetItemText(long item) const
654 {
655 wxListItem info;
39a7f085 656 info.m_mask = wxLIST_MASK_TEXT;
62d89eb4
VZ
657 info.m_itemId = item;
658 GetItem( info );
659 return info.m_text;
660 }
661
662 void SetItemText(long item, const wxString& value)
663 {
664 wxListItem info;
665 info.m_mask = wxLIST_MASK_TEXT;
666 info.m_itemId = item;
667 info.m_text = value;
668 SetItem( info );
669 }
cf1dfa6b
VZ
670
671 // set the scrollbars and update the positions of the items
ca65c044 672 void RecalculatePositions(bool noRefresh = false);
b54e41c5
VZ
673
674 // refresh the window and the header
675 void RefreshAll();
cf1dfa6b 676
62d89eb4 677 long GetNextItem( long item, int geometry, int state ) const;
efbb7287
VZ
678 void DeleteItem( long index );
679 void DeleteAllItems();
680 void DeleteColumn( int col );
681 void DeleteEverything();
682 void EnsureVisible( long index );
ca65c044 683 long FindItem( long start, const wxString& str, bool partial = false );
81ce36aa 684 long FindItem( long start, wxUIntPtr data);
8158e0e1 685 long FindItem( const wxPoint& pt );
efbb7287
VZ
686 long HitTest( int x, int y, int &flags );
687 void InsertItem( wxListItem &item );
efbb7287 688 void InsertColumn( long col, wxListItem &item );
7d4813a0 689 int GetItemWidthWithImage(wxListItem * item);
efbb7287
VZ
690 void SortItems( wxListCtrlCompare fn, long data );
691
cf1dfa6b 692 size_t GetItemCount() const;
1370703e 693 bool IsEmpty() const { return GetItemCount() == 0; }
2c1f73ee
VZ
694 void SetItemCount(long count);
695
0ddefeb0
VZ
696 // change the current (== focused) item, send a notification event
697 void ChangeCurrent(size_t current);
698 void ResetCurrent() { ChangeCurrent((size_t)-1); }
cf1dfa6b
VZ
699 bool HasCurrent() const { return m_current != (size_t)-1; }
700
701 // send out a wxListEvent
702 void SendNotify( size_t line,
703 wxEventType command,
fbfb8bcc 704 const wxPoint& point = wxDefaultPosition );
cf1dfa6b 705
b54e41c5
VZ
706 // override base class virtual to reset m_lineHeight when the font changes
707 virtual bool SetFont(const wxFont& font)
708 {
709 if ( !wxScrolledWindow::SetFont(font) )
ca65c044 710 return false;
b54e41c5
VZ
711
712 m_lineHeight = 0;
713
ca65c044 714 return true;
b54e41c5 715 }
cf1dfa6b
VZ
716
717 // these are for wxListLineData usage only
718
719 // get the backpointer to the list ctrl
3cd94a0d 720 wxGenericListCtrl *GetListCtrl() const
cf1dfa6b 721 {
3cd94a0d 722 return wxStaticCast(GetParent(), wxGenericListCtrl);
cf1dfa6b
VZ
723 }
724
725 // get the height of all lines (assuming they all do have the same height)
726 wxCoord GetLineHeight() const;
727
728 // get the y position of the given line (only for report view)
729 wxCoord GetLineY(size_t line) const;
730
49ecb029
VZ
731 // get the brush to use for the item highlighting
732 wxBrush *GetHighlightBrush() const
733 {
734 return m_hasFocus ? m_highlightBrush : m_highlightUnfocusedBrush;
735 }
736
cf1dfa6b 737//protected:
904ccf52
VZ
738 // the array of all line objects for a non virtual list control (for the
739 // virtual list control we only ever use m_lines[0])
cf1dfa6b
VZ
740 wxListLineDataArray m_lines;
741
742 // the list of column objects
743 wxListHeaderDataList m_columns;
744
745 // currently focused item or -1
746 size_t m_current;
747
cf1dfa6b
VZ
748 // the number of lines per page
749 int m_linesPerPage;
750
751 // this flag is set when something which should result in the window
752 // redrawing happens (i.e. an item was added or deleted, or its appearance
753 // changed) and OnPaint() doesn't redraw the window while it is set which
754 // allows to minimize the number of repaintings when a lot of items are
755 // being added. The real repainting occurs only after the next OnIdle()
756 // call
757 bool m_dirty;
758
b54e41c5 759 wxColour *m_highlightColour;
80020b62
JS
760 wxImageListType *m_small_image_list;
761 wxImageListType *m_normal_image_list;
cf1dfa6b
VZ
762 int m_small_spacing;
763 int m_normal_spacing;
764 bool m_hasFocus;
765
766 bool m_lastOnSame;
767 wxTimer *m_renameTimer;
cf1dfa6b
VZ
768 bool m_isCreated;
769 int m_dragCount;
770 wxPoint m_dragStart;
7d4813a0 771 ColWidthArray m_aColWidths;
cf1dfa6b
VZ
772
773 // for double click logic
774 size_t m_lineLastClicked,
8df44392
RR
775 m_lineBeforeLastClicked,
776 m_lineSelectSingleOnUp;
cf1dfa6b 777
b6bc47ef 778 wxListTextCtrl* m_textctrl;
277ea1b4 779
1370703e 780protected:
6f02a879
VZ
781 wxWindow *GetMainWindowOfCompositeControl() { return GetParent(); }
782
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.SetFont( GetFont() );
bd8289c1 1687
f6bcfd97
BP
1688 // width and height of the entire header window
1689 int w, h;
63852e78 1690 GetClientSize( &w, &h );
f6bcfd97 1691 m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
c801d85f 1692
f60d0f94 1693 dc.SetBackgroundMode(wxTRANSPARENT);
35d4c967 1694 dc.SetTextForeground(GetForegroundColour());
ca65c044 1695
cf1dfa6b 1696 int x = HEADER_OFFSET_X;
63852e78
RR
1697 int numColumns = m_owner->GetColumnCount();
1698 wxListItem item;
c5b7bb59 1699 for ( int i = 0; i < numColumns && x < w; i++ )
63852e78
RR
1700 {
1701 m_owner->GetColumn( i, item );
f6bcfd97 1702 int wCol = item.m_width;
f6bcfd97 1703
0a816d95
VZ
1704 // the width of the rect to draw: make it smaller to fit entirely
1705 // inside the column rect
d108f236 1706#ifdef __WXMAC__
277ea1b4
DS
1707 int cw = wCol;
1708 int ch = h;
d108f236 1709#else
0a816d95 1710 int cw = wCol - 2;
277ea1b4 1711 int ch = h - 2;
d108f236 1712#endif
277ea1b4 1713
9c7f49f5
VZ
1714 wxRendererNative::Get().DrawHeaderButton
1715 (
1716 this,
1717 dc,
d108f236 1718 wxRect(x, HEADER_OFFSET_Y, cw, ch),
9c7f49f5 1719 m_parent->IsEnabled() ? 0
2ac08aeb 1720 : (int)wxCONTROL_DISABLED
9c7f49f5 1721 );
0a816d95 1722
e1e1272f
VZ
1723 // see if we have enough space for the column label
1724
1725 // for this we need the width of the text
1726 wxCoord wLabel;
ca65c044 1727 wxCoord hLabel;
cabeffb0 1728 dc.GetTextExtent(item.GetText(), &wLabel, &hLabel);
277ea1b4 1729 wLabel += 2 * EXTRA_WIDTH;
e1e1272f
VZ
1730
1731 // and the width of the icon, if any
1732 static const int MARGIN_BETWEEN_TEXT_AND_ICON = 2;
277ea1b4 1733 int ix = 0, iy = 0; // init them just to suppress the compiler warnings
e1e1272f
VZ
1734 const int image = item.m_image;
1735 wxImageListType *imageList;
0a816d95
VZ
1736 if ( image != -1 )
1737 {
e1e1272f 1738 imageList = m_owner->m_small_image_list;
0a816d95
VZ
1739 if ( imageList )
1740 {
0a816d95 1741 imageList->GetSize(image, ix, iy);
e1e1272f
VZ
1742 wLabel += ix + MARGIN_BETWEEN_TEXT_AND_ICON;
1743 }
1744 }
1745 else
1746 {
1747 imageList = NULL;
1748 }
1749
1750 // ignore alignment if there is not enough space anyhow
1751 int xAligned;
1752 switch ( wLabel < cw ? item.GetAlign() : wxLIST_FORMAT_LEFT )
1753 {
1754 default:
1755 wxFAIL_MSG( _T("unknown list item format") );
1756 // fall through
0a816d95 1757
e1e1272f
VZ
1758 case wxLIST_FORMAT_LEFT:
1759 xAligned = x;
1760 break;
0a816d95 1761
e1e1272f
VZ
1762 case wxLIST_FORMAT_RIGHT:
1763 xAligned = x + cw - wLabel;
1764 break;
1765
1766 case wxLIST_FORMAT_CENTER:
1767 xAligned = x + (cw - wLabel) / 2;
1768 break;
1769 }
1770
e1e1272f
VZ
1771 // if we have an image, draw it on the right of the label
1772 if ( imageList )
1773 {
1774 imageList->Draw
1775 (
1776 image,
1777 dc,
1778 xAligned + wLabel - ix - MARGIN_BETWEEN_TEXT_AND_ICON,
1779 HEADER_OFFSET_Y + (h - 4 - iy)/2,
1780 wxIMAGELIST_DRAW_TRANSPARENT
1781 );
1782
1783 cw -= ix + MARGIN_BETWEEN_TEXT_AND_ICON;
0a816d95
VZ
1784 }
1785
1786 // draw the text clipping it so that it doesn't overwrite the column
1787 // boundary
1788 wxDCClipper clipper(dc, x, HEADER_OFFSET_Y, cw, h - 4 );
06b781c7
VZ
1789
1790 dc.DrawText( item.GetText(),
cabeffb0 1791 xAligned + EXTRA_WIDTH, h / 2 - hLabel / 2 ); //HEADER_OFFSET_Y + EXTRA_HEIGHT );
06b781c7 1792
0a816d95 1793 x += wCol;
63852e78 1794 }
e1e955e1 1795}
c801d85f 1796
0208334d
RR
1797void wxListHeaderWindow::DrawCurrent()
1798{
63852e78
RR
1799 int x1 = m_currentX;
1800 int y1 = 0;
f16ba4e6 1801 m_owner->ClientToScreen( &x1, &y1 );
f6bcfd97 1802
f16ba4e6 1803 int x2 = m_currentX;
63852e78 1804 int y2 = 0;
f6bcfd97 1805 m_owner->GetClientSize( NULL, &y2 );
63852e78 1806 m_owner->ClientToScreen( &x2, &y2 );
0208334d 1807
63852e78 1808 wxScreenDC dc;
3c679789 1809 dc.SetLogicalFunction( wxINVERT );
63852e78
RR
1810 dc.SetPen( wxPen( *wxBLACK, 2, wxSOLID ) );
1811 dc.SetBrush( *wxTRANSPARENT_BRUSH );
0208334d 1812
f6bcfd97
BP
1813 AdjustDC(dc);
1814
63852e78 1815 dc.DrawLine( x1, y1, x2, y2 );
0208334d 1816
63852e78 1817 dc.SetLogicalFunction( wxCOPY );
0208334d 1818
63852e78
RR
1819 dc.SetPen( wxNullPen );
1820 dc.SetBrush( wxNullBrush );
0208334d
RR
1821}
1822
c801d85f
KB
1823void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
1824{
f6bcfd97 1825 // we want to work with logical coords
3ca6a5f0
BP
1826 int x;
1827 m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
3ca6a5f0 1828 int y = event.GetY();
f6bcfd97 1829
cfb50f14 1830 if (m_isDragging)
0208334d 1831 {
355f2407 1832 SendListEvent(wxEVT_COMMAND_LIST_COL_DRAGGING, event.GetPosition());
a77ec46d 1833
f6bcfd97
BP
1834 // we don't draw the line beyond our window, but we allow dragging it
1835 // there
1836 int w = 0;
1837 GetClientSize( &w, NULL );
f6bcfd97 1838 m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
f6bcfd97
BP
1839 w -= 6;
1840
1841 // erase the line if it was drawn
1842 if ( m_currentX < w )
1843 DrawCurrent();
1844
63852e78
RR
1845 if (event.ButtonUp())
1846 {
1847 ReleaseMouse();
ca65c044
WS
1848 m_isDragging = false;
1849 m_dirty = true;
f6bcfd97 1850 m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
355f2407 1851 SendListEvent(wxEVT_COMMAND_LIST_COL_END_DRAG, event.GetPosition());
63852e78
RR
1852 }
1853 else
1854 {
f6bcfd97 1855 if (x > m_minX + 7)
63852e78
RR
1856 m_currentX = x;
1857 else
f6bcfd97 1858 m_currentX = m_minX + 7;
bd8289c1 1859
f6bcfd97
BP
1860 // draw in the new location
1861 if ( m_currentX < w )
1862 DrawCurrent();
bffa1c77 1863 }
0208334d 1864 }
f6bcfd97 1865 else // not dragging
c801d85f 1866 {
f6bcfd97 1867 m_minX = 0;
ca65c044 1868 bool hit_border = false;
f6bcfd97
BP
1869
1870 // end of the current column
1871 int xpos = 0;
1872
3103e8a9 1873 // find the column where this event occurred
62313c27
VZ
1874 int col,
1875 countCol = m_owner->GetColumnCount();
1876 for (col = 0; col < countCol; col++)
bffa1c77 1877 {
cf1dfa6b
VZ
1878 xpos += m_owner->GetColumnWidth( col );
1879 m_column = col;
f6bcfd97
BP
1880
1881 if ( (abs(x-xpos) < 3) && (y < 22) )
1882 {
1883 // near the column border
ca65c044 1884 hit_border = true;
f6bcfd97
BP
1885 break;
1886 }
1887
1888 if ( x < xpos )
1889 {
1890 // inside the column
1891 break;
1892 }
1893
1894 m_minX = xpos;
bffa1c77 1895 }
63852e78 1896
62313c27
VZ
1897 if ( col == countCol )
1898 m_column = -1;
1899
11358d39 1900 if (event.LeftDown() || event.RightUp())
63852e78 1901 {
11358d39 1902 if (hit_border && event.LeftDown())
f6bcfd97 1903 {
355f2407
VZ
1904 if ( SendListEvent(wxEVT_COMMAND_LIST_COL_BEGIN_DRAG,
1905 event.GetPosition()) )
1906 {
ca65c044 1907 m_isDragging = true;
355f2407 1908 m_currentX = x;
355f2407 1909 CaptureMouse();
03eccb83 1910 DrawCurrent();
355f2407
VZ
1911 }
1912 //else: column resizing was vetoed by the user code
f6bcfd97 1913 }
11358d39 1914 else // click on a column
f6bcfd97 1915 {
a77ec46d 1916 SendListEvent( event.LeftDown()
11358d39
VZ
1917 ? wxEVT_COMMAND_LIST_COL_CLICK
1918 : wxEVT_COMMAND_LIST_COL_RIGHT_CLICK,
a77ec46d 1919 event.GetPosition());
f6bcfd97 1920 }
63852e78 1921 }
f6bcfd97 1922 else if (event.Moving())
63852e78 1923 {
f6bcfd97
BP
1924 bool setCursor;
1925 if (hit_border)
1926 {
1927 setCursor = m_currentCursor == wxSTANDARD_CURSOR;
1928 m_currentCursor = m_resizeCursor;
1929 }
1930 else
1931 {
1932 setCursor = m_currentCursor != wxSTANDARD_CURSOR;
1933 m_currentCursor = wxSTANDARD_CURSOR;
1934 }
1935
1936 if ( setCursor )
1937 SetCursor(*m_currentCursor);
63852e78 1938 }
e1e955e1 1939 }
e1e955e1 1940}
c801d85f
KB
1941
1942void wxListHeaderWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1943{
63852e78 1944 m_owner->SetFocus();
03eccb83 1945 m_owner->Update();
e1e955e1 1946}
c801d85f 1947
fbfb8bcc 1948bool wxListHeaderWindow::SendListEvent(wxEventType type, const wxPoint& pos)
a77ec46d
RD
1949{
1950 wxWindow *parent = GetParent();
1951 wxListEvent le( type, parent->GetId() );
1952 le.SetEventObject( parent );
1953 le.m_pointDrag = pos;
1954
1955 // the position should be relative to the parent window, not
1956 // this one for compatibility with MSW and common sense: the
1957 // user code doesn't know anything at all about this header
1958 // window, so why should it get positions relative to it?
1959 le.m_pointDrag.y -= GetSize().y;
1960
1961 le.m_col = m_column;
355f2407 1962 return !parent->GetEventHandler()->ProcessEvent( le ) || le.IsAllowed();
a77ec46d
RD
1963}
1964
c801d85f
KB
1965//-----------------------------------------------------------------------------
1966// wxListRenameTimer (internal)
1967//-----------------------------------------------------------------------------
1968
bd8289c1
VZ
1969wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
1970{
63852e78 1971 m_owner = owner;
e1e955e1 1972}
c801d85f 1973
bd8289c1
VZ
1974void wxListRenameTimer::Notify()
1975{
63852e78 1976 m_owner->OnRenameTimer();
e1e955e1 1977}
c801d85f 1978
ee7ee469
RR
1979//-----------------------------------------------------------------------------
1980// wxListTextCtrl (internal)
1981//-----------------------------------------------------------------------------
1982
ee7ee469 1983BEGIN_EVENT_TABLE(wxListTextCtrl,wxTextCtrl)
63852e78 1984 EVT_CHAR (wxListTextCtrl::OnChar)
2c1f73ee 1985 EVT_KEY_UP (wxListTextCtrl::OnKeyUp)
63852e78 1986 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus)
ee7ee469
RR
1987END_EVENT_TABLE()
1988
62d89eb4
VZ
1989wxListTextCtrl::wxListTextCtrl(wxListMainWindow *owner, size_t itemEdit)
1990 : m_startValue(owner->GetItemText(itemEdit)),
1991 m_itemEdited(itemEdit)
1992{
63852e78 1993 m_owner = owner;
ca65c044 1994 m_finished = false;
33fe475a 1995 m_aboutToFinish = false;
62d89eb4
VZ
1996
1997 wxRect rectLabel = owner->GetLineLabelRect(itemEdit);
1998
1999 m_owner->CalcScrolledPosition(rectLabel.x, rectLabel.y,
2000 &rectLabel.x, &rectLabel.y);
2001
2002 (void)Create(owner, wxID_ANY, m_startValue,
2003 wxPoint(rectLabel.x-4,rectLabel.y-4),
2004 wxSize(rectLabel.width+11,rectLabel.height+8));
ee7ee469
RR
2005}
2006
62d89eb4 2007void wxListTextCtrl::Finish()
ee7ee469 2008{
62d89eb4 2009 if ( !m_finished )
63852e78 2010 {
62d89eb4 2011 wxPendingDelete.Append(this);
b6bc47ef 2012 m_owner->m_textctrl = NULL;
277ea1b4 2013
ca65c044 2014 m_finished = true;
62d89eb4 2015
ababa106 2016 m_owner->SetFocusIgnoringChildren();
62d89eb4
VZ
2017 }
2018}
dd5a32cc 2019
62d89eb4
VZ
2020bool wxListTextCtrl::AcceptChanges()
2021{
2022 const wxString value = GetValue();
2023
2024 if ( value == m_startValue )
62d89eb4 2025 // nothing changed, always accept
ca65c044 2026 return true;
62d89eb4
VZ
2027
2028 if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
62d89eb4 2029 // vetoed by the user
ca65c044 2030 return false;
f6bcfd97 2031
62d89eb4
VZ
2032 // accepted, do rename the item
2033 m_owner->SetItemText(m_itemEdited, value);
f6bcfd97 2034
ca65c044 2035 return true;
62d89eb4 2036}
dd5a32cc 2037
b6bc47ef
RD
2038void wxListTextCtrl::AcceptChangesAndFinish()
2039{
2040 m_aboutToFinish = true;
277ea1b4 2041
b6bc47ef
RD
2042 // Notify the owner about the changes
2043 AcceptChanges();
277ea1b4 2044
b6bc47ef
RD
2045 // Even if vetoed, close the control (consistent with MSW)
2046 Finish();
2047}
2048
62d89eb4
VZ
2049void wxListTextCtrl::OnChar( wxKeyEvent &event )
2050{
2051 switch ( event.m_keyCode )
2052 {
2053 case WXK_RETURN:
b6bc47ef 2054 AcceptChangesAndFinish();
768276f6 2055 break;
f6bcfd97 2056
62d89eb4
VZ
2057 case WXK_ESCAPE:
2058 Finish();
86586459 2059 m_owner->OnRenameCancelled( m_itemEdited );
62d89eb4
VZ
2060 break;
2061
2062 default:
2063 event.Skip();
2064 }
63852e78
RR
2065}
2066
c13cace1
VS
2067void wxListTextCtrl::OnKeyUp( wxKeyEvent &event )
2068{
dd5a32cc
RR
2069 if (m_finished)
2070 {
2071 event.Skip();
2072 return;
2073 }
2074
c13cace1
VS
2075 // auto-grow the textctrl:
2076 wxSize parentSize = m_owner->GetSize();
2077 wxPoint myPos = GetPosition();
2078 wxSize mySize = GetSize();
2079 int sx, sy;
a6d57d03 2080 GetTextExtent(GetValue() + _T("MM"), &sx, &sy);
cf1dfa6b
VZ
2081 if (myPos.x + sx > parentSize.x)
2082 sx = parentSize.x - myPos.x;
2083 if (mySize.x > sx)
2084 sx = mySize.x;
422d0ff0 2085 SetSize(sx, wxDefaultCoord);
2c1f73ee 2086
c13cace1
VS
2087 event.Skip();
2088}
2089
dd5a32cc 2090void wxListTextCtrl::OnKillFocus( wxFocusEvent &event )
63852e78 2091{
277ea1b4 2092 if ( !m_finished && !m_aboutToFinish )
dd5a32cc 2093 {
daa44d5b
VS
2094 // We must finish regardless of success, otherwise we'll get
2095 // focus problems:
62d89eb4 2096 Finish();
86351e4a 2097
86586459
RR
2098 if ( !AcceptChanges() )
2099 m_owner->OnRenameCancelled( m_itemEdited );
62d89eb4 2100 }
86351e4a 2101
daa44d5b 2102 // We must let the native text control handle focus, too, otherwise
33fe475a 2103 // it could have problems with the cursor (e.g., in wxGTK).
62d89eb4 2104 event.Skip();
ee7ee469
RR
2105}
2106
c801d85f
KB
2107//-----------------------------------------------------------------------------
2108// wxListMainWindow
2109//-----------------------------------------------------------------------------
2110
5a1cad6e 2111IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow,wxScrolledWindow)
bd8289c1 2112
c801d85f
KB
2113BEGIN_EVENT_TABLE(wxListMainWindow,wxScrolledWindow)
2114 EVT_PAINT (wxListMainWindow::OnPaint)
c801d85f
KB
2115 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse)
2116 EVT_CHAR (wxListMainWindow::OnChar)
3dfb93fd 2117 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown)
c801d85f
KB
2118 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus)
2119 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus)
2fa7c206 2120 EVT_SCROLLWIN (wxListMainWindow::OnScroll)
c801d85f
KB
2121END_EVENT_TABLE()
2122
1370703e 2123void wxListMainWindow::Init()
c801d85f 2124{
ca65c044 2125 m_dirty = true;
cf1dfa6b
VZ
2126 m_countVirt = 0;
2127 m_lineFrom =
b54e41c5 2128 m_lineTo = (size_t)-1;
cf1dfa6b
VZ
2129 m_linesPerPage = 0;
2130
2131 m_headerWidth =
2132 m_lineHeight = 0;
1370703e 2133
80020b62
JS
2134 m_small_image_list = (wxImageListType *) NULL;
2135 m_normal_image_list = (wxImageListType *) NULL;
1370703e 2136
139adb6a
RR
2137 m_small_spacing = 30;
2138 m_normal_spacing = 40;
1370703e 2139
ca65c044 2140 m_hasFocus = false;
1370703e 2141 m_dragCount = 0;
ca65c044 2142 m_isCreated = false;
1370703e 2143
ca65c044 2144 m_lastOnSame = false;
139adb6a 2145 m_renameTimer = new wxListRenameTimer( this );
b6bc47ef 2146 m_textctrl = NULL;
277ea1b4 2147
cf1dfa6b 2148 m_current =
efbb7287 2149 m_lineLastClicked =
8df44392 2150 m_lineSelectSingleOnUp =
1370703e 2151 m_lineBeforeLastClicked = (size_t)-1;
c5c528fc
VZ
2152
2153 m_freezeCount = 0;
e1e955e1 2154}
c801d85f 2155
1370703e 2156wxListMainWindow::wxListMainWindow()
c801d85f 2157{
1370703e
VZ
2158 Init();
2159
49ecb029
VZ
2160 m_highlightBrush =
2161 m_highlightUnfocusedBrush = (wxBrush *) NULL;
1370703e
VZ
2162}
2163
2164wxListMainWindow::wxListMainWindow( wxWindow *parent,
2165 wxWindowID id,
2166 const wxPoint& pos,
2167 const wxSize& size,
2168 long style,
2169 const wxString &name )
2170 : wxScrolledWindow( parent, id, pos, size,
2171 style | wxHSCROLL | wxVSCROLL, name )
2172{
2173 Init();
2174
49ecb029
VZ
2175 m_highlightBrush = new wxBrush
2176 (
a756f210 2177 wxSystemSettings::GetColour
49ecb029
VZ
2178 (
2179 wxSYS_COLOUR_HIGHLIGHT
2180 ),
2181 wxSOLID
2182 );
2183
2184 m_highlightUnfocusedBrush = new wxBrush
2185 (
a756f210 2186 wxSystemSettings::GetColour
49ecb029
VZ
2187 (
2188 wxSYS_COLOUR_BTNSHADOW
2189 ),
2190 wxSOLID
2191 );
2192
6d78bbe6 2193 SetScrollbars( 0, 0, 0, 0, 0, 0 );
bd8289c1 2194
ca65c044 2195 wxVisualAttributes attr = wxGenericListCtrl::GetClassDefaultAttributes();
fa47d7a7
VS
2196 SetOwnForegroundColour( attr.colFg );
2197 SetOwnBackgroundColour( attr.colBg );
92a4e4de
VZ
2198 if (!m_hasFont)
2199 SetOwnFont( attr.font );
e1e955e1 2200}
c801d85f 2201
fd9811b1 2202wxListMainWindow::~wxListMainWindow()
c801d85f 2203{
5fe143df 2204 DoDeleteAllItems();
222ed1d6 2205 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
8d36b216 2206 WX_CLEAR_ARRAY(m_aColWidths);
12c1b46a 2207
b54e41c5 2208 delete m_highlightBrush;
49ecb029 2209 delete m_highlightUnfocusedBrush;
139adb6a 2210 delete m_renameTimer;
e1e955e1 2211}
c801d85f 2212
cf1dfa6b 2213void wxListMainWindow::CacheLineData(size_t line)
c801d85f 2214{
3cd94a0d 2215 wxGenericListCtrl *listctrl = GetListCtrl();
25e3a937 2216
b54e41c5 2217 wxListLineData *ld = GetDummyLine();
f6bcfd97 2218
cf1dfa6b
VZ
2219 size_t countCol = GetColumnCount();
2220 for ( size_t col = 0; col < countCol; col++ )
2221 {
2222 ld->SetText(col, listctrl->OnGetItemText(line, col));
e062c6a4 2223 ld->SetImage(col, listctrl->OnGetItemColumnImage(line, col));
cf1dfa6b
VZ
2224 }
2225
6c02c329 2226 ld->SetAttr(listctrl->OnGetItemAttr(line));
e1e955e1 2227}
c801d85f 2228
b54e41c5 2229wxListLineData *wxListMainWindow::GetDummyLine() const
c801d85f 2230{
cf1dfa6b 2231 wxASSERT_MSG( !IsEmpty(), _T("invalid line index") );
904ccf52
VZ
2232 wxASSERT_MSG( IsVirtual(), _T("GetDummyLine() shouldn't be called") );
2233
2234 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
2235
2236 // we need to recreate the dummy line if the number of columns in the
2237 // control changed as it would have the incorrect number of fields
2238 // otherwise
2239 if ( !m_lines.IsEmpty() &&
2240 m_lines[0].m_items.GetCount() != (size_t)GetColumnCount() )
2c1f73ee 2241 {
904ccf52
VZ
2242 self->m_lines.Clear();
2243 }
cf1dfa6b 2244
904ccf52
VZ
2245 if ( m_lines.IsEmpty() )
2246 {
5cd89174 2247 wxListLineData *line = new wxListLineData(self);
cf1dfa6b 2248 self->m_lines.Add(line);
904ccf52
VZ
2249
2250 // don't waste extra memory -- there never going to be anything
2251 // else/more in this array
2252 self->m_lines.Shrink();
2c1f73ee
VZ
2253 }
2254
cf1dfa6b
VZ
2255 return &m_lines[0];
2256}
2257
5cd89174
VZ
2258// ----------------------------------------------------------------------------
2259// line geometry (report mode only)
2260// ----------------------------------------------------------------------------
2261
cf1dfa6b
VZ
2262wxCoord wxListMainWindow::GetLineHeight() const
2263{
cf1dfa6b
VZ
2264 // we cache the line height as calling GetTextExtent() is slow
2265 if ( !m_lineHeight )
2c1f73ee 2266 {
cf1dfa6b 2267 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
bd8289c1 2268
cf1dfa6b
VZ
2269 wxClientDC dc( self );
2270 dc.SetFont( GetFont() );
c801d85f 2271
cf1dfa6b
VZ
2272 wxCoord y;
2273 dc.GetTextExtent(_T("H"), NULL, &y);
004fd0c8 2274
ca7353de
RD
2275 if ( m_small_image_list && m_small_image_list->GetImageCount() )
2276 {
277ea1b4 2277 int iw = 0, ih = 0;
ca7353de
RD
2278 m_small_image_list->GetSize(0, iw, ih);
2279 y = wxMax(y, ih);
2280 }
2281
2282 y += EXTRA_HEIGHT;
cf1dfa6b
VZ
2283 self->m_lineHeight = y + LINE_SPACING;
2284 }
bffa1c77 2285
cf1dfa6b
VZ
2286 return m_lineHeight;
2287}
bffa1c77 2288
cf1dfa6b
VZ
2289wxCoord wxListMainWindow::GetLineY(size_t line) const
2290{
b5d43d1d 2291 wxASSERT_MSG( InReportView(), _T("only works in report mode") );
1370703e 2292
277ea1b4 2293 return LINE_SPACING + line * GetLineHeight();
cf1dfa6b 2294}
206b0a67 2295
5cd89174
VZ
2296wxRect wxListMainWindow::GetLineRect(size_t line) const
2297{
2298 if ( !InReportView() )
2299 return GetLine(line)->m_gi->m_rectAll;
2300
2301 wxRect rect;
2302 rect.x = HEADER_OFFSET_X;
2303 rect.y = GetLineY(line);
2304 rect.width = GetHeaderWidth();
2305 rect.height = GetLineHeight();
2306
2307 return rect;
2308}
2309
2310wxRect wxListMainWindow::GetLineLabelRect(size_t line) const
2311{
2312 if ( !InReportView() )
2313 return GetLine(line)->m_gi->m_rectLabel;
2314
2315 wxRect rect;
2316 rect.x = HEADER_OFFSET_X;
2317 rect.y = GetLineY(line);
2318 rect.width = GetColumnWidth(0);
2319 rect.height = GetLineHeight();
2320
2321 return rect;
2322}
2323
2324wxRect wxListMainWindow::GetLineIconRect(size_t line) const
2325{
2326 if ( !InReportView() )
2327 return GetLine(line)->m_gi->m_rectIcon;
2328
2329 wxListLineData *ld = GetLine(line);
2330 wxASSERT_MSG( ld->HasImage(), _T("should have an image") );
2331
2332 wxRect rect;
2333 rect.x = HEADER_OFFSET_X;
2334 rect.y = GetLineY(line);
2335 GetImageSize(ld->GetImage(), rect.width, rect.height);
2336
2337 return rect;
2338}
2339
2340wxRect wxListMainWindow::GetLineHighlightRect(size_t line) const
2341{
2342 return InReportView() ? GetLineRect(line)
2343 : GetLine(line)->m_gi->m_rectHighlight;
2344}
2345
2346long wxListMainWindow::HitTestLine(size_t line, int x, int y) const
2347{
fc4f1d5f
VZ
2348 wxASSERT_MSG( line < GetItemCount(), _T("invalid line in HitTestLine") );
2349
5cd89174
VZ
2350 wxListLineData *ld = GetLine(line);
2351
2352 if ( ld->HasImage() && GetLineIconRect(line).Inside(x, y) )
2353 return wxLIST_HITTEST_ONITEMICON;
2354
acf4d858
VS
2355 // VS: Testing for "ld->HasText() || InReportView()" instead of
2356 // "ld->HasText()" is needed to make empty lines in report view
2357 // possible
2358 if ( ld->HasText() || InReportView() )
5cd89174
VZ
2359 {
2360 wxRect rect = InReportView() ? GetLineRect(line)
2361 : GetLineLabelRect(line);
2362
2363 if ( rect.Inside(x, y) )
2364 return wxLIST_HITTEST_ONITEMLABEL;
2365 }
2366
2367 return 0;
2368}
2369
2370// ----------------------------------------------------------------------------
2371// highlight (selection) handling
2372// ----------------------------------------------------------------------------
2373
b54e41c5 2374bool wxListMainWindow::IsHighlighted(size_t line) const
cf1dfa6b
VZ
2375{
2376 if ( IsVirtual() )
2377 {
b54e41c5 2378 return m_selStore.IsSelected(line);
cf1dfa6b
VZ
2379 }
2380 else // !virtual
2381 {
2382 wxListLineData *ld = GetLine(line);
ca65c044 2383 wxCHECK_MSG( ld, false, _T("invalid index in IsHighlighted") );
cf1dfa6b 2384
b54e41c5 2385 return ld->IsHighlighted();
cf1dfa6b
VZ
2386 }
2387}
2388
68a9ef0e
VZ
2389void wxListMainWindow::HighlightLines( size_t lineFrom,
2390 size_t lineTo,
2391 bool highlight )
cf1dfa6b 2392{
cf1dfa6b
VZ
2393 if ( IsVirtual() )
2394 {
68a9ef0e
VZ
2395 wxArrayInt linesChanged;
2396 if ( !m_selStore.SelectRange(lineFrom, lineTo, highlight,
2397 &linesChanged) )
2398 {
2399 // meny items changed state, refresh everything
2400 RefreshLines(lineFrom, lineTo);
2401 }
2402 else // only a few items changed state, refresh only them
2403 {
2404 size_t count = linesChanged.GetCount();
2405 for ( size_t n = 0; n < count; n++ )
2406 {
2407 RefreshLine(linesChanged[n]);
2408 }
2409 }
b54e41c5 2410 }
68a9ef0e 2411 else // iterate over all items in non report view
b54e41c5 2412 {
b54e41c5 2413 for ( size_t line = lineFrom; line <= lineTo; line++ )
cf1dfa6b 2414 {
b54e41c5 2415 if ( HighlightLine(line, highlight) )
68a9ef0e 2416 RefreshLine(line);
cf1dfa6b 2417 }
b54e41c5
VZ
2418 }
2419}
2420
2421bool wxListMainWindow::HighlightLine( size_t line, bool highlight )
2422{
2423 bool changed;
2424
2425 if ( IsVirtual() )
2426 {
2427 changed = m_selStore.SelectItem(line, highlight);
cf1dfa6b
VZ
2428 }
2429 else // !virtual
2430 {
2431 wxListLineData *ld = GetLine(line);
ca65c044 2432 wxCHECK_MSG( ld, false, _T("invalid index in HighlightLine") );
cf1dfa6b 2433
b54e41c5 2434 changed = ld->Highlight(highlight);
cf1dfa6b
VZ
2435 }
2436
2437 if ( changed )
2438 {
b54e41c5 2439 SendNotify( line, highlight ? wxEVT_COMMAND_LIST_ITEM_SELECTED
5cd89174 2440 : wxEVT_COMMAND_LIST_ITEM_DESELECTED );
cf1dfa6b
VZ
2441 }
2442
2443 return changed;
2444}
2445
2446void wxListMainWindow::RefreshLine( size_t line )
2447{
b5d43d1d 2448 if ( InReportView() )
6ea1323a
VZ
2449 {
2450 size_t visibleFrom, visibleTo;
2451 GetVisibleLinesRange(&visibleFrom, &visibleTo);
c1c4c551 2452
6ea1323a
VZ
2453 if ( line < visibleFrom || line > visibleTo )
2454 return;
2455 }
c1c4c551 2456
5cd89174 2457 wxRect rect = GetLineRect(line);
cf1dfa6b
VZ
2458
2459 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2460 RefreshRect( rect );
2461}
2462
2463void wxListMainWindow::RefreshLines( size_t lineFrom, size_t lineTo )
2464{
2465 // we suppose that they are ordered by caller
2466 wxASSERT_MSG( lineFrom <= lineTo, _T("indices in disorder") );
2467
6b4a8d93
VZ
2468 wxASSERT_MSG( lineTo < GetItemCount(), _T("invalid line range") );
2469
b5d43d1d 2470 if ( InReportView() )
cf1dfa6b 2471 {
b54e41c5
VZ
2472 size_t visibleFrom, visibleTo;
2473 GetVisibleLinesRange(&visibleFrom, &visibleTo);
2474
2475 if ( lineFrom < visibleFrom )
2476 lineFrom = visibleFrom;
2477 if ( lineTo > visibleTo )
2478 lineTo = visibleTo;
cf1dfa6b
VZ
2479
2480 wxRect rect;
2481 rect.x = 0;
2482 rect.y = GetLineY(lineFrom);
2483 rect.width = GetClientSize().x;
91c6cc0e 2484 rect.height = GetLineY(lineTo) - rect.y + GetLineHeight();
cf1dfa6b
VZ
2485
2486 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2487 RefreshRect( rect );
2488 }
2489 else // !report
2490 {
2491 // TODO: this should be optimized...
2492 for ( size_t line = lineFrom; line <= lineTo; line++ )
2493 {
2494 RefreshLine(line);
2495 }
2496 }
2497}
2498
6b4a8d93
VZ
2499void wxListMainWindow::RefreshAfter( size_t lineFrom )
2500{
b5d43d1d 2501 if ( InReportView() )
6b4a8d93 2502 {
c29582d0
JS
2503 size_t visibleFrom, visibleTo;
2504 GetVisibleLinesRange(&visibleFrom, &visibleTo);
6b4a8d93
VZ
2505
2506 if ( lineFrom < visibleFrom )
2507 lineFrom = visibleFrom;
c29582d0
JS
2508 else if ( lineFrom > visibleTo )
2509 return;
6b4a8d93
VZ
2510
2511 wxRect rect;
2512 rect.x = 0;
2513 rect.y = GetLineY(lineFrom);
c29582d0 2514 CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
6b4a8d93
VZ
2515
2516 wxSize size = GetClientSize();
2517 rect.width = size.x;
277ea1b4 2518
6b4a8d93
VZ
2519 // refresh till the bottom of the window
2520 rect.height = size.y - rect.y;
2521
6b4a8d93 2522 RefreshRect( rect );
6b4a8d93
VZ
2523 }
2524 else // !report
2525 {
2526 // TODO: how to do it more efficiently?
ca65c044 2527 m_dirty = true;
6b4a8d93
VZ
2528 }
2529}
2530
49ecb029
VZ
2531void wxListMainWindow::RefreshSelected()
2532{
2533 if ( IsEmpty() )
2534 return;
2535
2536 size_t from, to;
2537 if ( InReportView() )
2538 {
2539 GetVisibleLinesRange(&from, &to);
2540 }
2541 else // !virtual
2542 {
2543 from = 0;
2544 to = GetItemCount() - 1;
2545 }
2546
cf30ae13 2547 if ( HasCurrent() && m_current >= from && m_current <= to )
49ecb029 2548 RefreshLine(m_current);
49ecb029
VZ
2549
2550 for ( size_t line = from; line <= to; line++ )
2551 {
2552 // NB: the test works as expected even if m_current == -1
2553 if ( line != m_current && IsHighlighted(line) )
49ecb029 2554 RefreshLine(line);
49ecb029
VZ
2555 }
2556}
2557
c5c528fc
VZ
2558void wxListMainWindow::Freeze()
2559{
2560 m_freezeCount++;
2561}
2562
2563void wxListMainWindow::Thaw()
2564{
2565 wxCHECK_RET( m_freezeCount > 0, _T("thawing unfrozen list control?") );
2566
2567 if ( !--m_freezeCount )
c5c528fc 2568 Refresh();
c5c528fc
VZ
2569}
2570
cf1dfa6b
VZ
2571void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
2572{
2573 // Note: a wxPaintDC must be constructed even if no drawing is
2574 // done (a Windows requirement).
2575 wxPaintDC dc( this );
2576
c5c528fc 2577 if ( IsEmpty() || m_freezeCount )
c5c528fc 2578 // nothing to draw or not the moment to draw it
cf1dfa6b 2579 return;
cf1dfa6b 2580
1e4d446b 2581 if ( m_dirty )
1e4d446b
VZ
2582 // delay the repainting until we calculate all the items positions
2583 return;
1e4d446b 2584
cf1dfa6b
VZ
2585 PrepareDC( dc );
2586
2587 int dev_x, dev_y;
2588 CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
2589
cf1dfa6b
VZ
2590 dc.SetFont( GetFont() );
2591
b5d43d1d 2592 if ( InReportView() )
cf1dfa6b 2593 {
b54e41c5 2594 int lineHeight = GetLineHeight();
cf1dfa6b 2595
b54e41c5
VZ
2596 size_t visibleFrom, visibleTo;
2597 GetVisibleLinesRange(&visibleFrom, &visibleTo);
b84839ae
VZ
2598
2599 wxRect rectLine;
2600 wxCoord xOrig, yOrig;
2601 CalcUnscrolledPosition(0, 0, &xOrig, &yOrig);
2602
ff3d11a0 2603 // tell the caller cache to cache the data
91c6cc0e
VZ
2604 if ( IsVirtual() )
2605 {
2606 wxListEvent evCache(wxEVT_COMMAND_LIST_CACHE_HINT,
2607 GetParent()->GetId());
2608 evCache.SetEventObject( GetParent() );
2609 evCache.m_oldItemIndex = visibleFrom;
2610 evCache.m_itemIndex = visibleTo;
2611 GetParent()->GetEventHandler()->ProcessEvent( evCache );
2612 }
ff3d11a0 2613
b54e41c5 2614 for ( size_t line = visibleFrom; line <= visibleTo; line++ )
cf1dfa6b 2615 {
b84839ae
VZ
2616 rectLine = GetLineRect(line);
2617
2618 if ( !IsExposed(rectLine.x - xOrig, rectLine.y - yOrig,
2619 rectLine.width, rectLine.height) )
2620 {
2621 // don't redraw unaffected lines to avoid flicker
2622 continue;
2623 }
2624
5cd89174 2625 GetLine(line)->DrawInReportMode( &dc,
b84839ae 2626 rectLine,
5cd89174 2627 GetLineHighlightRect(line),
49ecb029 2628 IsHighlighted(line) );
cf1dfa6b
VZ
2629 }
2630
2631 if ( HasFlag(wxLC_HRULES) )
2632 {
b54e41c5 2633 wxPen pen(GetRuleColour(), 1, wxSOLID);
cf1dfa6b
VZ
2634 wxSize clientSize = GetClientSize();
2635
e7d073c3 2636 // Don't draw the first one
277ea1b4 2637 for ( size_t i = visibleFrom + 1; i <= visibleTo; i++ )
cf1dfa6b
VZ
2638 {
2639 dc.SetPen(pen);
2640 dc.SetBrush( *wxTRANSPARENT_BRUSH );
277ea1b4
DS
2641 dc.DrawLine(0 - dev_x, i * lineHeight,
2642 clientSize.x - dev_x, i * lineHeight);
cf1dfa6b
VZ
2643 }
2644
2645 // Draw last horizontal rule
e7d073c3 2646 if ( visibleTo == GetItemCount() - 1 )
cf1dfa6b 2647 {
277ea1b4 2648 dc.SetPen( pen );
cf1dfa6b 2649 dc.SetBrush( *wxTRANSPARENT_BRUSH );
277ea1b4
DS
2650 dc.DrawLine(0 - dev_x, (m_lineTo + 1) * lineHeight,
2651 clientSize.x - dev_x , (m_lineTo + 1) * lineHeight );
cf1dfa6b 2652 }
2c1f73ee 2653 }
206b0a67
JS
2654
2655 // Draw vertical rules if required
cf1dfa6b 2656 if ( HasFlag(wxLC_VRULES) && !IsEmpty() )
206b0a67 2657 {
b54e41c5 2658 wxPen pen(GetRuleColour(), 1, wxSOLID);
277ea1b4 2659 wxRect firstItemRect, lastItemRect;
cf1dfa6b 2660
e7d073c3
RD
2661 GetItemRect(visibleFrom, firstItemRect);
2662 GetItemRect(visibleTo, lastItemRect);
206b0a67 2663 int x = firstItemRect.GetX();
673dfcfa
JS
2664 dc.SetPen(pen);
2665 dc.SetBrush(* wxTRANSPARENT_BRUSH);
277ea1b4 2666
999836aa 2667 for (int col = 0; col < GetColumnCount(); col++)
206b0a67
JS
2668 {
2669 int colWidth = GetColumnWidth(col);
cf1dfa6b 2670 x += colWidth;
e7d073c3
RD
2671 dc.DrawLine(x - dev_x - 2, firstItemRect.GetY() - 1 - dev_y,
2672 x - dev_x - 2, lastItemRect.GetBottom() + 1 - dev_y);
206b0a67 2673 }
d786bf87 2674 }
139adb6a 2675 }
cf1dfa6b 2676 else // !report
139adb6a 2677 {
1370703e 2678 size_t count = GetItemCount();
cf1dfa6b
VZ
2679 for ( size_t i = 0; i < count; i++ )
2680 {
2681 GetLine(i)->Draw( &dc );
2682 }
139adb6a 2683 }
004fd0c8 2684
65a1f350
RR
2685#ifndef __WXMAC__
2686 // Don't draw rect outline under Mac at all.
c25f61f1 2687 if ( HasCurrent() )
cf1dfa6b 2688 {
49ecb029 2689 if ( m_hasFocus )
c25f61f1
VZ
2690 {
2691 dc.SetPen( *wxBLACK_PEN );
2692 dc.SetBrush( *wxTRANSPARENT_BRUSH );
277ea1b4 2693 dc.DrawRectangle( GetLineHighlightRect( m_current ) );
c25f61f1 2694 }
cf1dfa6b 2695 }
65a1f350 2696#endif
e1e955e1 2697}
c801d85f 2698
b54e41c5 2699void wxListMainWindow::HighlightAll( bool on )
c801d85f 2700{
b54e41c5 2701 if ( IsSingleSel() )
c801d85f 2702 {
277ea1b4 2703 wxASSERT_MSG( !on, _T("can't do this in a single selection control") );
b54e41c5
VZ
2704
2705 // we just have one item to turn off
2706 if ( HasCurrent() && IsHighlighted(m_current) )
139adb6a 2707 {
ca65c044 2708 HighlightLine(m_current, false);
b54e41c5 2709 RefreshLine(m_current);
139adb6a 2710 }
e1e955e1 2711 }
b54e41c5 2712 else // multi sel
cf1dfa6b 2713 {
b54e41c5 2714 HighlightLines(0, GetItemCount() - 1, on);
cf1dfa6b 2715 }
e1e955e1 2716}
c801d85f 2717
cf1dfa6b 2718void wxListMainWindow::SendNotify( size_t line,
05a7f61d 2719 wxEventType command,
fbfb8bcc 2720 const wxPoint& point )
c801d85f 2721{
139adb6a
RR
2722 wxListEvent le( command, GetParent()->GetId() );
2723 le.SetEventObject( GetParent() );
cf1dfa6b 2724 le.m_itemIndex = line;
05a7f61d
VZ
2725
2726 // set only for events which have position
2727 if ( point != wxDefaultPosition )
2728 le.m_pointDrag = point;
2729
c1c4c551
VZ
2730 // don't try to get the line info for virtual list controls: the main
2731 // program has it anyhow and if we did it would result in accessing all
2732 // the lines, even those which are not visible now and this is precisely
2733 // what we're trying to avoid
2734 if ( !IsVirtual() && (command != wxEVT_COMMAND_LIST_DELETE_ITEM) )
91c6cc0e 2735 {
0ddefeb0
VZ
2736 if ( line != (size_t)-1 )
2737 {
2738 GetLine(line)->GetItem( 0, le.m_item );
2739 }
2740 //else: this happens for wxEVT_COMMAND_LIST_ITEM_FOCUSED event
91c6cc0e
VZ
2741 }
2742 //else: there may be no more such item
2743
6e228e42 2744 GetParent()->GetEventHandler()->ProcessEvent( le );
e1e955e1 2745}
c801d85f 2746
0ddefeb0 2747void wxListMainWindow::ChangeCurrent(size_t current)
c801d85f 2748{
0ddefeb0 2749 m_current = current;
c801d85f 2750
0ddefeb0 2751 SendNotify(current, wxEVT_COMMAND_LIST_ITEM_FOCUSED);
e1e955e1 2752}
c801d85f 2753
5f1ea0ee 2754void wxListMainWindow::EditLabel( long item )
c801d85f 2755{
cf1dfa6b 2756 wxCHECK_RET( (item >= 0) && ((size_t)item < GetItemCount()),
3cd94a0d 2757 wxT("wrong index in wxGenericListCtrl::EditLabel()") );
004fd0c8 2758
62d89eb4 2759 size_t itemEdit = (size_t)item;
e179bd65 2760
fd9811b1 2761 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
139adb6a 2762 le.SetEventObject( GetParent() );
1370703e 2763 le.m_itemIndex = item;
62d89eb4 2764 wxListLineData *data = GetLine(itemEdit);
1370703e
VZ
2765 wxCHECK_RET( data, _T("invalid index in EditLabel()") );
2766 data->GetItem( 0, le.m_item );
277ea1b4 2767
62d89eb4 2768 if ( GetParent()->GetEventHandler()->ProcessEvent( le ) && !le.IsAllowed() )
62d89eb4 2769 // vetoed by user code
5f1ea0ee 2770 return;
004fd0c8 2771
cf1dfa6b
VZ
2772 // We have to call this here because the label in question might just have
2773 // been added and no screen update taken place.
62d89eb4 2774 if ( m_dirty )
cf1dfa6b 2775 wxSafeYield();
004fd0c8 2776
b6bc47ef
RD
2777 m_textctrl = new wxListTextCtrl(this, itemEdit);
2778 m_textctrl->SetFocus();
e1e955e1 2779}
c801d85f 2780
e179bd65
RR
2781void wxListMainWindow::OnRenameTimer()
2782{
cf1dfa6b 2783 wxCHECK_RET( HasCurrent(), wxT("unexpected rename timer") );
004fd0c8 2784
cf1dfa6b 2785 EditLabel( m_current );
e179bd65
RR
2786}
2787
62d89eb4 2788bool wxListMainWindow::OnRenameAccept(size_t itemEdit, const wxString& value)
c801d85f 2789{
e179bd65
RR
2790 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
2791 le.SetEventObject( GetParent() );
62d89eb4 2792 le.m_itemIndex = itemEdit;
1370703e 2793
62d89eb4 2794 wxListLineData *data = GetLine(itemEdit);
277ea1b4 2795
ca65c044 2796 wxCHECK_MSG( data, false, _T("invalid index in OnRenameAccept()") );
1370703e
VZ
2797
2798 data->GetItem( 0, le.m_item );
62d89eb4
VZ
2799 le.m_item.m_text = value;
2800 return !GetParent()->GetEventHandler()->ProcessEvent( le ) ||
2801 le.IsAllowed();
e1e955e1 2802}
c801d85f 2803
18dd54e3 2804void wxListMainWindow::OnRenameCancelled(size_t itemEdit)
86586459 2805{
86586459
RR
2806 // let owner know that the edit was cancelled
2807 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
86351e4a 2808
ca65c044 2809 le.SetEditCanceled(true);
86351e4a 2810
86586459
RR
2811 le.SetEventObject( GetParent() );
2812 le.m_itemIndex = itemEdit;
2813
2814 wxListLineData *data = GetLine(itemEdit);
2815 wxCHECK_RET( data, _T("invalid index in OnRenameCancelled()") );
2816
2817 data->GetItem( 0, le.m_item );
86586459
RR
2818 GetEventHandler()->ProcessEvent( le );
2819}
2820
c801d85f
KB
2821void wxListMainWindow::OnMouse( wxMouseEvent &event )
2822{
b6bc47ef
RD
2823#ifdef __WXMAC__
2824 // On wxMac we can't depend on the EVT_KILL_FOCUS event to properly
2825 // shutdown the edit control when the mouse is clicked elsewhere on the
2826 // listctrl because the order of events is different (or something like
277ea1b4 2827 // that), so explicitly end the edit if it is active.
b6bc47ef 2828 if ( event.LeftDown() && m_textctrl)
b6bc47ef 2829 m_textctrl->AcceptChangesAndFinish();
b6bc47ef 2830#endif
277ea1b4 2831
3e1c4e00 2832 event.SetEventObject( GetParent() );
cf1dfa6b
VZ
2833 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
2834 return;
2835
185f6036
RD
2836 if (event.GetEventType() == wxEVT_MOUSEWHEEL)
2837 {
2838 // let the base handle mouse wheel events.
2839 event.Skip();
2840 return;
2841 }
2ac08aeb 2842
cf1dfa6b
VZ
2843 if ( !HasCurrent() || IsEmpty() )
2844 return;
2845
2846 if (m_dirty)
2847 return;
e3e65dac 2848
cf1dfa6b
VZ
2849 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() ||
2850 event.ButtonDClick()) )
2851 return;
c801d85f 2852
aaef15bf
RR
2853 int x = event.GetX();
2854 int y = event.GetY();
2855 CalcUnscrolledPosition( x, y, &x, &y );
004fd0c8 2856
cc734310 2857 // where did we hit it (if we did)?
92976ab6 2858 long hitResult = 0;
1370703e 2859
cf1dfa6b
VZ
2860 size_t count = GetItemCount(),
2861 current;
2862
b5d43d1d 2863 if ( InReportView() )
92976ab6 2864 {
5cd89174 2865 current = y / GetLineHeight();
cc734310
VZ
2866 if ( current < count )
2867 hitResult = HitTestLine(current, x, y);
cf1dfa6b
VZ
2868 }
2869 else // !report
2870 {
2871 // TODO: optimize it too! this is less simple than for report view but
2872 // enumerating all items is still not a way to do it!!
4e3ace65 2873 for ( current = 0; current < count; current++ )
cf1dfa6b 2874 {
5cd89174 2875 hitResult = HitTestLine(current, x, y);
4e3ace65
VZ
2876 if ( hitResult )
2877 break;
cf1dfa6b 2878 }
92976ab6 2879 }
bd8289c1 2880
fd9811b1 2881 if (event.Dragging())
92976ab6 2882 {
fd9811b1 2883 if (m_dragCount == 0)
8d1d2284
VZ
2884 {
2885 // we have to report the raw, physical coords as we want to be
2886 // able to call HitTest(event.m_pointDrag) from the user code to
2887 // get the item being dragged
2888 m_dragStart = event.GetPosition();
2889 }
bffa1c77 2890
fd9811b1 2891 m_dragCount++;
bffa1c77 2892
cf1dfa6b
VZ
2893 if (m_dragCount != 3)
2894 return;
bffa1c77 2895
05a7f61d
VZ
2896 int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
2897 : wxEVT_COMMAND_LIST_BEGIN_DRAG;
bffa1c77 2898
fd9811b1 2899 wxListEvent le( command, GetParent()->GetId() );
92976ab6 2900 le.SetEventObject( GetParent() );
8df44392 2901 le.m_itemIndex = m_lineLastClicked;
bffa1c77
VZ
2902 le.m_pointDrag = m_dragStart;
2903 GetParent()->GetEventHandler()->ProcessEvent( le );
2904
2905 return;
92976ab6 2906 }
fd9811b1
RR
2907 else
2908 {
2909 m_dragCount = 0;
2910 }
bd8289c1 2911
cf1dfa6b
VZ
2912 if ( !hitResult )
2913 {
2914 // outside of any item
2915 return;
2916 }
bd8289c1 2917
ca65c044 2918 bool forceClick = false;
92976ab6
RR
2919 if (event.ButtonDClick())
2920 {
92976ab6 2921 m_renameTimer->Stop();
ca65c044 2922 m_lastOnSame = false;
efbb7287 2923
ddba340d 2924 if ( current == m_lineLastClicked )
efbb7287 2925 {
cf1dfa6b 2926 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
004fd0c8 2927
efbb7287
VZ
2928 return;
2929 }
2930 else
2931 {
8df44392 2932 // The first click was on another item, so don't interpret this as
efbb7287 2933 // a double click, but as a simple click instead
ca65c044 2934 forceClick = true;
efbb7287 2935 }
92976ab6 2936 }
bd8289c1 2937
8df44392 2938 if (event.LeftUp())
c801d85f 2939 {
277ea1b4 2940 if (m_lineSelectSingleOnUp != (size_t)-1)
92976ab6 2941 {
8df44392
RR
2942 // select single line
2943 HighlightAll( false );
2944 ReverseHighlight(m_lineSelectSingleOnUp);
2945 }
277ea1b4 2946
2cb1f3da 2947 if (m_lastOnSame)
8df44392
RR
2948 {
2949 if ((current == m_current) &&
2950 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
2cb1f3da 2951 HasFlag(wxLC_EDIT_LABELS) )
8df44392
RR
2952 {
2953 m_renameTimer->Start( 100, true );
2954 }
92976ab6 2955 }
277ea1b4 2956
ca65c044 2957 m_lastOnSame = false;
277ea1b4 2958 m_lineSelectSingleOnUp = (size_t)-1;
8df44392
RR
2959 }
2960 else
2961 {
3103e8a9 2962 // This is necessary, because after a DnD operation in
2997ca30 2963 // from and to ourself, the up event is swallowed by the
8df44392
RR
2964 // DnD code. So on next non-up event (which means here and
2965 // now) m_lineSelectSingleOnUp should be reset.
277ea1b4 2966 m_lineSelectSingleOnUp = (size_t)-1;
e1e955e1 2967 }
8df44392 2968 if (event.RightDown())
b204641e 2969 {
8df44392
RR
2970 m_lineBeforeLastClicked = m_lineLastClicked;
2971 m_lineLastClicked = current;
277ea1b4 2972
eaefbb88
KH
2973 // If the item is already selected, do not update the selection.
2974 // Multi-selections should not be cleared if a selected item is clicked.
2975 if (!IsHighlighted(current))
2976 {
2977 HighlightAll(false);
2978 ChangeCurrent(current);
2979 ReverseHighlight(m_current);
2980 }
277ea1b4
DS
2981
2982 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK, event.GetPosition() );
2983
18216ae3
RR
2984 // Allow generation of context menu event
2985 event.Skip();
b204641e 2986 }
cf1dfa6b 2987 else if (event.MiddleDown())
b204641e 2988 {
cf1dfa6b 2989 SendNotify( current, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
92976ab6 2990 }
cf1dfa6b 2991 else if ( event.LeftDown() || forceClick )
92976ab6 2992 {
efbb7287 2993 m_lineBeforeLastClicked = m_lineLastClicked;
cf1dfa6b
VZ
2994 m_lineLastClicked = current;
2995
2996 size_t oldCurrent = m_current;
2cb1f3da
JS
2997 bool oldWasSelected = IsHighlighted(m_current);
2998
39e39d39 2999 bool cmdModifierDown = event.CmdDown();
2fe417e4 3000 if ( IsSingleSel() || !(cmdModifierDown || event.ShiftDown()) )
8df44392 3001 {
277ea1b4 3002 if ( IsSingleSel() || !IsHighlighted(current) )
eb3d60a2
KH
3003 {
3004 HighlightAll( false );
0ddefeb0 3005
eb3d60a2 3006 ChangeCurrent(current);
cf1dfa6b 3007
eb3d60a2
KH
3008 ReverseHighlight(m_current);
3009 }
3010 else // multi sel & current is highlighted & no mod keys
8df44392 3011 {
2997ca30 3012 m_lineSelectSingleOnUp = current;
8df44392
RR
3013 ChangeCurrent(current); // change focus
3014 }
e1e955e1 3015 }
b54e41c5 3016 else // multi sel & either ctrl or shift is down
b204641e 3017 {
015fd9ef 3018 if (cmdModifierDown)
92976ab6 3019 {
0ddefeb0 3020 ChangeCurrent(current);
cf1dfa6b 3021
b54e41c5 3022 ReverseHighlight(m_current);
92976ab6 3023 }
473d087e 3024 else if (event.ShiftDown())
92976ab6 3025 {
0ddefeb0 3026 ChangeCurrent(current);
bffa1c77 3027
cf1dfa6b
VZ
3028 size_t lineFrom = oldCurrent,
3029 lineTo = current;
f6bcfd97 3030
cf1dfa6b 3031 if ( lineTo < lineFrom )
92976ab6 3032 {
cf1dfa6b
VZ
3033 lineTo = lineFrom;
3034 lineFrom = m_current;
92976ab6
RR
3035 }
3036
b54e41c5 3037 HighlightLines(lineFrom, lineTo);
92976ab6 3038 }
cf1dfa6b 3039 else // !ctrl, !shift
92976ab6 3040 {
cf1dfa6b
VZ
3041 // test in the enclosing if should make it impossible
3042 wxFAIL_MSG( _T("how did we get here?") );
92976ab6 3043 }
e1e955e1 3044 }
cf1dfa6b 3045
92976ab6 3046 if (m_current != oldCurrent)
92976ab6 3047 RefreshLine( oldCurrent );
efbb7287
VZ
3048
3049 // forceClick is only set if the previous click was on another item
2cb1f3da 3050 m_lastOnSame = !forceClick && (m_current == oldCurrent) && oldWasSelected;
e1e955e1 3051 }
e1e955e1 3052}
c801d85f 3053
34bbbc27 3054void wxListMainWindow::MoveToItem(size_t item)
c801d85f 3055{
34bbbc27 3056 if ( item == (size_t)-1 )
cf1dfa6b 3057 return;
004fd0c8 3058
34bbbc27 3059 wxRect rect = GetLineRect(item);
cf3da716 3060
cf1dfa6b 3061 int client_w, client_h;
cf3da716 3062 GetClientSize( &client_w, &client_h );
f6bcfd97 3063
6d78bbe6
VZ
3064 const int hLine = GetLineHeight();
3065
277ea1b4
DS
3066 int view_x = SCROLL_UNIT_X * GetScrollPos( wxHORIZONTAL );
3067 int view_y = hLine * GetScrollPos( wxVERTICAL );
004fd0c8 3068
b5d43d1d 3069 if ( InReportView() )
92976ab6 3070 {
277ea1b4
DS
3071 // the next we need the range of lines shown it might be different,
3072 // so recalculate it
b54e41c5
VZ
3073 ResetVisibleLinesRange();
3074
277ea1b4
DS
3075 if (rect.y < view_y)
3076 Scroll( -1, rect.y / hLine );
3077 if (rect.y + rect.height + 5 > view_y + client_h)
3078 Scroll( -1, (rect.y + rect.height - client_h + hLine) / hLine );
92976ab6 3079 }
b54e41c5 3080 else // !report
92976ab6 3081 {
cf1dfa6b 3082 if (rect.x-view_x < 5)
277ea1b4
DS
3083 Scroll( (rect.x - 5) / SCROLL_UNIT_X, -1 );
3084 if (rect.x + rect.width - 5 > view_x + client_w)
3085 Scroll( (rect.x + rect.width - client_w + SCROLL_UNIT_X) / SCROLL_UNIT_X, -1 );
92976ab6 3086 }
e1e955e1 3087}
c801d85f 3088
cf1dfa6b
VZ
3089// ----------------------------------------------------------------------------
3090// keyboard handling
3091// ----------------------------------------------------------------------------
3092
3093void wxListMainWindow::OnArrowChar(size_t newCurrent, const wxKeyEvent& event)
c801d85f 3094{
cf1dfa6b
VZ
3095 wxCHECK_RET( newCurrent < (size_t)GetItemCount(),
3096 _T("invalid item index in OnArrowChar()") );
3097
3098 size_t oldCurrent = m_current;
3099
3100 // in single selection we just ignore Shift as we can't select several
3101 // items anyhow
b54e41c5 3102 if ( event.ShiftDown() && !IsSingleSel() )
cf1dfa6b 3103 {
0ddefeb0 3104 ChangeCurrent(newCurrent);
cf1dfa6b 3105
65a1f350
RR
3106 // refresh the old focus to remove it
3107 RefreshLine( oldCurrent );
3108
cf1dfa6b
VZ
3109 // select all the items between the old and the new one
3110 if ( oldCurrent > newCurrent )
3111 {
3112 newCurrent = oldCurrent;
3113 oldCurrent = m_current;
3114 }
3115
b54e41c5 3116 HighlightLines(oldCurrent, newCurrent);
cf1dfa6b
VZ
3117 }
3118 else // !shift
3119 {
b54e41c5
VZ
3120 // all previously selected items are unselected unless ctrl is held
3121 if ( !event.ControlDown() )
ca65c044 3122 HighlightAll(false);
b54e41c5 3123
0ddefeb0 3124 ChangeCurrent(newCurrent);
ca65c044 3125
65a1f350
RR
3126 // refresh the old focus to remove it
3127 RefreshLine( oldCurrent );
cf1dfa6b
VZ
3128
3129 if ( !event.ControlDown() )
3130 {
ca65c044 3131 HighlightLine( m_current, true );
cf1dfa6b
VZ
3132 }
3133 }
ca65c044 3134
92976ab6 3135 RefreshLine( m_current );
cf1dfa6b 3136
cf3da716 3137 MoveToFocus();
e1e955e1 3138}
c801d85f 3139
3dfb93fd
RR
3140void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
3141{
3142 wxWindow *parent = GetParent();
004fd0c8 3143
277ea1b4 3144 // propagate the key event upwards
3dfb93fd
RR
3145 wxKeyEvent ke( wxEVT_KEY_DOWN );
3146 ke.m_shiftDown = event.m_shiftDown;
3147 ke.m_controlDown = event.m_controlDown;
3148 ke.m_altDown = event.m_altDown;
3149 ke.m_metaDown = event.m_metaDown;
3150 ke.m_keyCode = event.m_keyCode;
3151 ke.m_x = event.m_x;
3152 ke.m_y = event.m_y;
3153 ke.SetEventObject( parent );
3154 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
004fd0c8 3155
3dfb93fd
RR
3156 event.Skip();
3157}
004fd0c8 3158
c801d85f
KB
3159void wxListMainWindow::OnChar( wxKeyEvent &event )
3160{
51cc4dad 3161 wxWindow *parent = GetParent();
004fd0c8 3162
277ea1b4 3163 // send a list_key event up
cf1dfa6b 3164 if ( HasCurrent() )
f6bcfd97
BP
3165 {
3166 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() );
cf1dfa6b
VZ
3167 le.m_itemIndex = m_current;
3168 GetLine(m_current)->GetItem( 0, le.m_item );
12a3f227 3169 le.m_code = event.GetKeyCode();
f6bcfd97
BP
3170 le.SetEventObject( parent );
3171 parent->GetEventHandler()->ProcessEvent( le );
3172 }
51cc4dad 3173
277ea1b4 3174 // propagate the char event upwards
3dfb93fd 3175 wxKeyEvent ke( wxEVT_CHAR );
51cc4dad
RR
3176 ke.m_shiftDown = event.m_shiftDown;
3177 ke.m_controlDown = event.m_controlDown;
3178 ke.m_altDown = event.m_altDown;
3179 ke.m_metaDown = event.m_metaDown;
3180 ke.m_keyCode = event.m_keyCode;
3181 ke.m_x = event.m_x;
3182 ke.m_y = event.m_y;
3183 ke.SetEventObject( parent );
3184 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
004fd0c8 3185
12a3f227 3186 if (event.GetKeyCode() == WXK_TAB)
012a03e0
RR
3187 {
3188 wxNavigationKeyEvent nevent;
c5145d41 3189 nevent.SetWindowChange( event.ControlDown() );
012a03e0 3190 nevent.SetDirection( !event.ShiftDown() );
8253c7fd 3191 nevent.SetEventObject( GetParent()->GetParent() );
012a03e0 3192 nevent.SetCurrentFocus( m_parent );
87948f22
VZ
3193 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent ))
3194 return;
012a03e0 3195 }
004fd0c8 3196
277ea1b4 3197 // no item -> nothing to do
cf1dfa6b 3198 if (!HasCurrent())
c801d85f 3199 {
51cc4dad
RR
3200 event.Skip();
3201 return;
e1e955e1 3202 }
51cc4dad 3203
12a3f227 3204 switch (event.GetKeyCode())
c801d85f 3205 {
51cc4dad 3206 case WXK_UP:
cf1dfa6b
VZ
3207 if ( m_current > 0 )
3208 OnArrowChar( m_current - 1, event );
51cc4dad 3209 break;
cf1dfa6b 3210
51cc4dad 3211 case WXK_DOWN:
cf1dfa6b
VZ
3212 if ( m_current < (size_t)GetItemCount() - 1 )
3213 OnArrowChar( m_current + 1, event );
51cc4dad 3214 break;
cf1dfa6b 3215
51cc4dad 3216 case WXK_END:
1370703e 3217 if (!IsEmpty())
cf1dfa6b 3218 OnArrowChar( GetItemCount() - 1, event );
51cc4dad 3219 break;
cf1dfa6b 3220
51cc4dad 3221 case WXK_HOME:
1370703e 3222 if (!IsEmpty())
cf1dfa6b 3223 OnArrowChar( 0, event );
51cc4dad 3224 break;
cf1dfa6b 3225
51cc4dad 3226 case WXK_PRIOR:
f6bcfd97 3227 {
b5d43d1d 3228 int steps = InReportView() ? m_linesPerPage - 1 : m_current % m_linesPerPage;
cf1dfa6b
VZ
3229
3230 int index = m_current - steps;
3231 if (index < 0)
3232 index = 0;
3233
3234 OnArrowChar( index, event );
51cc4dad 3235 }
51cc4dad 3236 break;
cf1dfa6b 3237
51cc4dad 3238 case WXK_NEXT:
bffa1c77 3239 {
b5d43d1d 3240 int steps = InReportView()
999836aa
VZ
3241 ? m_linesPerPage - 1
3242 : m_linesPerPage - (m_current % m_linesPerPage) - 1;
f6bcfd97 3243
cf1dfa6b
VZ
3244 size_t index = m_current + steps;
3245 size_t count = GetItemCount();
3246 if ( index >= count )
3247 index = count - 1;
3248
3249 OnArrowChar( index, event );
51cc4dad 3250 }
51cc4dad 3251 break;
cf1dfa6b 3252
51cc4dad 3253 case WXK_LEFT:
b5d43d1d 3254 if ( !InReportView() )
51cc4dad 3255 {
cf1dfa6b
VZ
3256 int index = m_current - m_linesPerPage;
3257 if (index < 0)
3258 index = 0;
3259
3260 OnArrowChar( index, event );
51cc4dad
RR
3261 }
3262 break;
cf1dfa6b 3263
51cc4dad 3264 case WXK_RIGHT:
b5d43d1d 3265 if ( !InReportView() )
51cc4dad 3266 {
cf1dfa6b
VZ
3267 size_t index = m_current + m_linesPerPage;
3268
3269 size_t count = GetItemCount();
3270 if ( index >= count )
3271 index = count - 1;
3272
3273 OnArrowChar( index, event );
51cc4dad
RR
3274 }
3275 break;
cf1dfa6b 3276
51cc4dad 3277 case WXK_SPACE:
b54e41c5 3278 if ( IsSingleSel() )
33d0e17c 3279 {
87948f22 3280 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
70541533
VZ
3281
3282 if ( IsHighlighted(m_current) )
3283 {
3284 // don't unselect the item in single selection mode
3285 break;
3286 }
3287 //else: select it in ReverseHighlight() below if unselected
33d0e17c 3288 }
70541533
VZ
3289
3290 ReverseHighlight(m_current);
51cc4dad 3291 break;
cf1dfa6b 3292
51cc4dad
RR
3293 case WXK_RETURN:
3294 case WXK_EXECUTE:
87948f22 3295 SendNotify( m_current, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
51cc4dad 3296 break;
cf1dfa6b 3297
51cc4dad 3298 default:
51cc4dad 3299 event.Skip();
e1e955e1 3300 }
e1e955e1 3301}
c801d85f 3302
cf1dfa6b
VZ
3303// ----------------------------------------------------------------------------
3304// focus handling
3305// ----------------------------------------------------------------------------
3306
c801d85f
KB
3307void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
3308{
dc76287d
RD
3309 if ( GetParent() )
3310 {
3311 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
3312 event.SetEventObject( GetParent() );
3313 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3314 return;
3315 }
3316
bde9072f
VZ
3317 // wxGTK sends us EVT_SET_FOCUS events even if we had never got
3318 // EVT_KILL_FOCUS before which means that we finish by redrawing the items
3319 // which are already drawn correctly resulting in horrible flicker - avoid
3320 // it
47724389
VZ
3321 if ( !m_hasFocus )
3322 {
ca65c044 3323 m_hasFocus = true;
bde9072f 3324
47724389
VZ
3325 RefreshSelected();
3326 }
e1e955e1 3327}
c801d85f
KB
3328
3329void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
3330{
dc76287d
RD
3331 if ( GetParent() )
3332 {
3333 wxFocusEvent event( wxEVT_KILL_FOCUS, GetParent()->GetId() );
3334 event.SetEventObject( GetParent() );
3335 if ( GetParent()->GetEventHandler()->ProcessEvent( event) )
3336 return;
3337 }
277ea1b4 3338
ca65c044 3339 m_hasFocus = false;
49ecb029 3340 RefreshSelected();
e1e955e1 3341}
c801d85f 3342
1e6d9499 3343void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
c801d85f 3344{
cf1dfa6b 3345 if ( HasFlag(wxLC_ICON) && (m_normal_image_list))
63852e78
RR
3346 {
3347 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
63852e78 3348 }
cf1dfa6b 3349 else if ( HasFlag(wxLC_SMALL_ICON) && (m_small_image_list))
63852e78
RR
3350 {
3351 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3352 }
cf1dfa6b 3353 else if ( HasFlag(wxLC_LIST) && (m_small_image_list))
0b855868
RR
3354 {
3355 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
3356 }
b5d43d1d 3357 else if ( InReportView() && (m_small_image_list))
63852e78
RR
3358 {
3359 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
63852e78 3360 }
e1e955e1 3361}
c801d85f 3362
5cd89174 3363void wxListMainWindow::GetImageSize( int index, int &width, int &height ) const
c801d85f 3364{
cf1dfa6b 3365 if ( HasFlag(wxLC_ICON) && m_normal_image_list )
63852e78
RR
3366 {
3367 m_normal_image_list->GetSize( index, width, height );
63852e78 3368 }
cf1dfa6b 3369 else if ( HasFlag(wxLC_SMALL_ICON) && m_small_image_list )
63852e78
RR
3370 {
3371 m_small_image_list->GetSize( index, width, height );
63852e78 3372 }
cf1dfa6b 3373 else if ( HasFlag(wxLC_LIST) && m_small_image_list )
0b855868
RR
3374 {
3375 m_small_image_list->GetSize( index, width, height );
0b855868 3376 }
b5d43d1d 3377 else if ( InReportView() && m_small_image_list )
63852e78
RR
3378 {
3379 m_small_image_list->GetSize( index, width, height );
63852e78 3380 }
cf1dfa6b
VZ
3381 else
3382 {
3383 width =
3384 height = 0;
3385 }
e1e955e1 3386}
c801d85f 3387
5cd89174 3388int wxListMainWindow::GetTextLength( const wxString &s ) const
c801d85f 3389{
5cd89174 3390 wxClientDC dc( wxConstCast(this, wxListMainWindow) );
cf1dfa6b 3391 dc.SetFont( GetFont() );
c801d85f 3392
cf1dfa6b
VZ
3393 wxCoord lw;
3394 dc.GetTextExtent( s, &lw, NULL );
3395
3396 return lw + AUTOSIZE_COL_MARGIN;
e1e955e1 3397}
c801d85f 3398
80020b62 3399void wxListMainWindow::SetImageList( wxImageListType *imageList, int which )
c801d85f 3400{
ca65c044 3401 m_dirty = true;
f6bcfd97
BP
3402
3403 // calc the spacing from the icon size
277ea1b4
DS
3404 int width = 0, height = 0;
3405
f6bcfd97 3406 if ((imageList) && (imageList->GetImageCount()) )
f6bcfd97 3407 imageList->GetSize(0, width, height);
f6bcfd97
BP
3408
3409 if (which == wxIMAGE_LIST_NORMAL)
3410 {
3411 m_normal_image_list = imageList;
3412 m_normal_spacing = width + 8;
3413 }
3414
3415 if (which == wxIMAGE_LIST_SMALL)
3416 {
3417 m_small_image_list = imageList;
3418 m_small_spacing = width + 14;
ca7353de 3419 m_lineHeight = 0; // ensure that the line height will be recalc'd
f6bcfd97 3420 }
e1e955e1 3421}
c801d85f 3422
debe6624 3423void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
c801d85f 3424{
ca65c044 3425 m_dirty = true;
139adb6a 3426 if (isSmall)
139adb6a 3427 m_small_spacing = spacing;
139adb6a 3428 else
139adb6a 3429 m_normal_spacing = spacing;
e1e955e1 3430}
c801d85f 3431
debe6624 3432int wxListMainWindow::GetItemSpacing( bool isSmall )
c801d85f 3433{
f6bcfd97 3434 return isSmall ? m_small_spacing : m_normal_spacing;
e1e955e1 3435}
c801d85f 3436
cf1dfa6b
VZ
3437// ----------------------------------------------------------------------------
3438// columns
3439// ----------------------------------------------------------------------------
3440
debe6624 3441void wxListMainWindow::SetColumn( int col, wxListItem &item )
c801d85f 3442{
222ed1d6 3443 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
f6bcfd97 3444
cf1dfa6b
VZ
3445 wxCHECK_RET( node, _T("invalid column index in SetColumn") );
3446
3447 if ( item.m_width == wxLIST_AUTOSIZE_USEHEADER )
3448 item.m_width = GetTextLength( item.m_text );
3449
3450 wxListHeaderData *column = node->GetData();
3451 column->SetItem( item );
3452
3453 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
f6bcfd97 3454 if ( headerWin )
ca65c044 3455 headerWin->m_dirty = true;
cf1dfa6b 3456
ca65c044 3457 m_dirty = true;
cf1dfa6b
VZ
3458
3459 // invalidate it as it has to be recalculated
3460 m_headerWidth = 0;
e1e955e1 3461}
c801d85f 3462
debe6624 3463void wxListMainWindow::SetColumnWidth( int col, int width )
c801d85f 3464{
cf1dfa6b
VZ
3465 wxCHECK_RET( col >= 0 && col < GetColumnCount(),
3466 _T("invalid column index") );
3467
b5d43d1d 3468 wxCHECK_RET( InReportView(),
f6bcfd97 3469 _T("SetColumnWidth() can only be called in report mode.") );
0208334d 3470
ca65c044 3471 m_dirty = true;
abe4a4f1
VS
3472 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
3473 if ( headerWin )
ca65c044 3474 headerWin->m_dirty = true;
bd8289c1 3475
222ed1d6 3476 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
cf1dfa6b
VZ
3477 wxCHECK_RET( node, _T("no column?") );
3478
3479 wxListHeaderData *column = node->GetData();
3480
3481 size_t count = GetItemCount();
3482
f6bcfd97
BP
3483 if (width == wxLIST_AUTOSIZE_USEHEADER)
3484 {
cf1dfa6b 3485 width = GetTextLength(column->GetText());
f6bcfd97 3486 }
cf1dfa6b 3487 else if ( width == wxLIST_AUTOSIZE )
0180dad6 3488 {
cf1dfa6b
VZ
3489 if ( IsVirtual() )
3490 {
3491 // TODO: determine the max width somehow...
3492 width = WIDTH_COL_DEFAULT;
3493 }
3494 else // !virtual
0180dad6 3495 {
cf1dfa6b
VZ
3496 wxClientDC dc(this);
3497 dc.SetFont( GetFont() );
3498
3499 int max = AUTOSIZE_COL_MARGIN;
3500
7d4813a0
VZ
3501 // if the cached column width isn't valid then recalculate it
3502 if (m_aColWidths.Item(col)->bNeedsUpdate)
0180dad6 3503 {
7d4813a0
VZ
3504 for (size_t i = 0; i < count; i++)
3505 {
277ea1b4 3506 wxListLineData *line = GetLine( i );
7d4813a0 3507 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
cf1dfa6b 3508
7d4813a0 3509 wxCHECK_RET( n, _T("no subitem?") );
cf1dfa6b 3510
7d4813a0
VZ
3511 wxListItemData *itemData = n->GetData();
3512 wxListItem item;
cf1dfa6b 3513
7d4813a0
VZ
3514 itemData->GetItem(item);
3515 int itemWidth = GetItemWidthWithImage(&item);
3516 if (itemWidth > max)
3517 max = itemWidth;
bffa1c77 3518 }
cf1dfa6b 3519
7d4813a0
VZ
3520 m_aColWidths.Item(col)->bNeedsUpdate = false;
3521 m_aColWidths.Item(col)->nMaxWidth = max;
0180dad6 3522 }
cf1dfa6b 3523
7d4813a0 3524 max = m_aColWidths.Item(col)->nMaxWidth;
cf1dfa6b 3525 width = max + AUTOSIZE_COL_MARGIN;
0180dad6 3526 }
0180dad6
RR
3527 }
3528
cf1dfa6b 3529 column->SetWidth( width );
bd8289c1 3530
cf1dfa6b
VZ
3531 // invalidate it as it has to be recalculated
3532 m_headerWidth = 0;
3533}
3534
3535int wxListMainWindow::GetHeaderWidth() const
3536{
3537 if ( !m_headerWidth )
0208334d 3538 {
cf1dfa6b
VZ
3539 wxListMainWindow *self = wxConstCast(this, wxListMainWindow);
3540
3541 size_t count = GetColumnCount();
3542 for ( size_t col = 0; col < count; col++ )
63852e78 3543 {
cf1dfa6b 3544 self->m_headerWidth += GetColumnWidth(col);
63852e78 3545 }
0208334d 3546 }
bd8289c1 3547
cf1dfa6b 3548 return m_headerWidth;
e1e955e1 3549}
c801d85f 3550
cf1dfa6b 3551void wxListMainWindow::GetColumn( int col, wxListItem &item ) const
c801d85f 3552{
222ed1d6 3553 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
cf1dfa6b
VZ
3554 wxCHECK_RET( node, _T("invalid column index in GetColumn") );
3555
3556 wxListHeaderData *column = node->GetData();
3557 column->GetItem( item );
e1e955e1 3558}
c801d85f 3559
cf1dfa6b 3560int wxListMainWindow::GetColumnWidth( int col ) const
c801d85f 3561{
222ed1d6 3562 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
24b9f055
VZ
3563 wxCHECK_MSG( node, 0, _T("invalid column index") );
3564
3565 wxListHeaderData *column = node->GetData();
3566 return column->GetWidth();
e1e955e1 3567}
c801d85f 3568
cf1dfa6b
VZ
3569// ----------------------------------------------------------------------------
3570// item state
3571// ----------------------------------------------------------------------------
c801d85f
KB
3572
3573void wxListMainWindow::SetItem( wxListItem &item )
3574{
cf1dfa6b
VZ
3575 long id = item.m_itemId;
3576 wxCHECK_RET( id >= 0 && (size_t)id < GetItemCount(),
3577 _T("invalid item index in SetItem") );
3578
6c02c329
VZ
3579 if ( !IsVirtual() )
3580 {
3581 wxListLineData *line = GetLine((size_t)id);
3582 line->SetItem( item.m_col, item );
7d4813a0 3583
39a7f085
VZ
3584 // Set item state if user wants
3585 if ( item.m_mask & wxLIST_MASK_STATE )
3586 SetItemState( item.m_itemId, item.m_state, item.m_state );
3587
7d4813a0
VZ
3588 if (InReportView())
3589 {
3590 // update the Max Width Cache if needed
3591 int width = GetItemWidthWithImage(&item);
3592
3593 if (width > m_aColWidths.Item(item.m_col)->nMaxWidth)
3594 m_aColWidths.Item(item.m_col)->nMaxWidth = width;
3595 }
6c02c329
VZ
3596 }
3597
285013b3
VZ
3598 // update the item on screen
3599 wxRect rectItem;
3600 GetItemRect(id, rectItem);
3601 RefreshRect(rectItem);
e1e955e1 3602}
c801d85f 3603
0afa5859
VZ
3604void wxListMainWindow::SetItemStateAll(long state, long stateMask)
3605{
3606 if ( IsEmpty() )
3607 return;
3608
3609 // first deal with selection
3610 if ( stateMask & wxLIST_STATE_SELECTED )
3611 {
3612 // set/clear select state
3613 if ( IsVirtual() )
3614 {
3615 // optimized version for virtual listctrl.
3616 m_selStore.SelectRange(0, GetItemCount() - 1, state == wxLIST_STATE_SELECTED);
3617 Refresh();
3618 }
3619 else if ( state & wxLIST_STATE_SELECTED )
3620 {
3621 const long count = GetItemCount();
3622 for( long i = 0; i < count; i++ )
3623 {
3624 SetItemState( i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
3625 }
3626
3627 }
3628 else
3629 {
3630 // clear for non virtual (somewhat optimized by using GetNextItem())
3631 long i = -1;
3632 while ( (i = GetNextItem(i, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)) != -1 )
3633 {
3634 SetItemState( i, 0, wxLIST_STATE_SELECTED );
3635 }
3636 }
3637 }
3638
3639 if ( HasCurrent() && (state == 0) && (stateMask & wxLIST_STATE_FOCUSED) )
3640 {
3641 // unfocus all: only one item can be focussed, so clearing focus for
3642 // all items is simply clearing focus of the focussed item.
3643 SetItemState(m_current, state, stateMask);
3644 }
3645 //(setting focus to all items makes no sense, so it is not handled here.)
3646}
3647
cf1dfa6b 3648void wxListMainWindow::SetItemState( long litem, long state, long stateMask )
c801d85f 3649{
0afa5859
VZ
3650 if ( litem == -1 )
3651 {
3652 SetItemStateAll(state, stateMask);
3653 return;
3654 }
3655
7448de8d
WS
3656 wxCHECK_RET( litem >= 0 && (size_t)litem < GetItemCount(),
3657 _T("invalid list ctrl item index in SetItem") );
54442116 3658
cf1dfa6b 3659 size_t oldCurrent = m_current;
88b792af 3660 size_t item = (size_t)litem; // safe because of the check above
bd8289c1 3661
88b792af 3662 // do we need to change the focus?
54442116 3663 if ( stateMask & wxLIST_STATE_FOCUSED )
c801d85f 3664 {
54442116 3665 if ( state & wxLIST_STATE_FOCUSED )
92976ab6 3666 {
54442116 3667 // don't do anything if this item is already focused
cf1dfa6b 3668 if ( item != m_current )
92976ab6 3669 {
0ddefeb0 3670 ChangeCurrent(item);
cf1dfa6b 3671
88b792af 3672 if ( oldCurrent != (size_t)-1 )
cf1dfa6b 3673 {
88b792af
VZ
3674 if ( IsSingleSel() )
3675 {
ca65c044 3676 HighlightLine(oldCurrent, false);
88b792af
VZ
3677 }
3678
cf1dfa6b
VZ
3679 RefreshLine(oldCurrent);
3680 }
54442116 3681
92976ab6 3682 RefreshLine( m_current );
92976ab6 3683 }
54442116
VZ
3684 }
3685 else // unfocus
3686 {
3687 // don't do anything if this item is not focused
cf1dfa6b 3688 if ( item == m_current )
bffa1c77 3689 {
0ddefeb0 3690 ResetCurrent();
88b792af 3691
943f6ad3
VZ
3692 if ( IsSingleSel() )
3693 {
3694 // we must unselect the old current item as well or we
3695 // might end up with more than one selected item in a
3696 // single selection control
ca65c044 3697 HighlightLine(oldCurrent, false);
943f6ad3
VZ
3698 }
3699
88b792af 3700 RefreshLine( oldCurrent );
bffa1c77 3701 }
92976ab6 3702 }
e1e955e1 3703 }
54442116 3704
88b792af 3705 // do we need to change the selection state?
54442116
VZ
3706 if ( stateMask & wxLIST_STATE_SELECTED )
3707 {
3708 bool on = (state & wxLIST_STATE_SELECTED) != 0;
54442116 3709
b54e41c5 3710 if ( IsSingleSel() )
54442116 3711 {
cf1dfa6b
VZ
3712 if ( on )
3713 {
3714 // selecting the item also makes it the focused one in the
3715 // single sel mode
3716 if ( m_current != item )
3717 {
0ddefeb0 3718 ChangeCurrent(item);
cf1dfa6b
VZ
3719
3720 if ( oldCurrent != (size_t)-1 )
3721 {
ca65c044 3722 HighlightLine( oldCurrent, false );
cf1dfa6b
VZ
3723 RefreshLine( oldCurrent );
3724 }
3725 }
3726 }
3727 else // off
3728 {
3729 // only the current item may be selected anyhow
3730 if ( item != m_current )
3731 return;
3732 }
54442116
VZ
3733 }
3734
b54e41c5 3735 if ( HighlightLine(item, on) )
54442116 3736 {
cf1dfa6b 3737 RefreshLine(item);
54442116
VZ
3738 }
3739 }
e1e955e1 3740}
c801d85f 3741
62d89eb4 3742int wxListMainWindow::GetItemState( long item, long stateMask ) const
c801d85f 3743{
cf1dfa6b
VZ
3744 wxCHECK_MSG( item >= 0 && (size_t)item < GetItemCount(), 0,
3745 _T("invalid list ctrl item index in GetItemState()") );
3746
92976ab6 3747 int ret = wxLIST_STATE_DONTCARE;
cf1dfa6b
VZ
3748
3749 if ( stateMask & wxLIST_STATE_FOCUSED )
c801d85f 3750 {
cf1dfa6b
VZ
3751 if ( (size_t)item == m_current )
3752 ret |= wxLIST_STATE_FOCUSED;
e1e955e1 3753 }
cf1dfa6b
VZ
3754
3755 if ( stateMask & wxLIST_STATE_SELECTED )
c801d85f 3756 {
b54e41c5 3757 if ( IsHighlighted(item) )
cf1dfa6b 3758 ret |= wxLIST_STATE_SELECTED;
e1e955e1 3759 }
cf1dfa6b 3760
92976ab6 3761 return ret;
e1e955e1 3762}
c801d85f 3763
62d89eb4 3764void wxListMainWindow::GetItem( wxListItem &item ) const
c801d85f 3765{
cf1dfa6b
VZ
3766 wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId < GetItemCount(),
3767 _T("invalid item index in GetItem") );
3768
3769 wxListLineData *line = GetLine((size_t)item.m_itemId);
3770 line->GetItem( item.m_col, item );
39a7f085
VZ
3771
3772 // Get item state if user wants it
3773 if ( item.m_mask & wxLIST_MASK_STATE )
3774 item.m_state = GetItemState( item.m_itemId, wxLIST_STATE_SELECTED |
3775 wxLIST_STATE_FOCUSED );
e1e955e1 3776}
c801d85f 3777
cf1dfa6b
VZ
3778// ----------------------------------------------------------------------------
3779// item count
3780// ----------------------------------------------------------------------------
3781
3782size_t wxListMainWindow::GetItemCount() const
1370703e
VZ
3783{
3784 return IsVirtual() ? m_countVirt : m_lines.GetCount();
3785}
3786
3787void wxListMainWindow::SetItemCount(long count)
c801d85f 3788{
b54e41c5 3789 m_selStore.SetItemCount(count);
1370703e
VZ
3790 m_countVirt = count;
3791
850ed6e7
VZ
3792 ResetVisibleLinesRange();
3793
5fe143df 3794 // scrollbars must be reset
ca65c044 3795 m_dirty = true;
e1e955e1 3796}
c801d85f 3797
62d89eb4 3798int wxListMainWindow::GetSelectedItemCount() const
c801d85f 3799{
cf1dfa6b 3800 // deal with the quick case first
b54e41c5 3801 if ( IsSingleSel() )
ca65c044 3802 return HasCurrent() ? IsHighlighted(m_current) : false;
cf1dfa6b
VZ
3803
3804 // virtual controls remmebers all its selections itself
3805 if ( IsVirtual() )
b54e41c5 3806 return m_selStore.GetSelectedCount();
cf1dfa6b
VZ
3807
3808 // TODO: we probably should maintain the number of items selected even for
3809 // non virtual controls as enumerating all lines is really slow...
3810 size_t countSel = 0;
3811 size_t count = GetItemCount();
3812 for ( size_t line = 0; line < count; line++ )
92976ab6 3813 {
b54e41c5 3814 if ( GetLine(line)->IsHighlighted() )
cf1dfa6b 3815 countSel++;
92976ab6 3816 }
c801d85f 3817
cf1dfa6b 3818 return countSel;
e1e955e1 3819}
e3e65dac 3820
cf1dfa6b
VZ
3821// ----------------------------------------------------------------------------
3822// item position/size
3823// ----------------------------------------------------------------------------
3824
11ebea16
VZ
3825wxRect wxListMainWindow::GetViewRect() const
3826{
3827 wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
3828 _T("wxListCtrl::GetViewRect() only works in icon mode") );
3829
3830 // we need to find the longest/tallest label
277ea1b4 3831 wxCoord xMax = 0, yMax = 0;
11ebea16
VZ
3832 const int count = GetItemCount();
3833 if ( count )
3834 {
3835 for ( int i = 0; i < count; i++ )
3836 {
3837 wxRect r;
3838 GetItemRect(i, r);
3839
3840 wxCoord x = r.GetRight(),
3841 y = r.GetBottom();
3842
3843 if ( x > xMax )
3844 xMax = x;
3845 if ( y > yMax )
3846 yMax = y;
3847 }
3848 }
3849
eb6c4508 3850 // some fudge needed to make it look prettier
277ea1b4
DS
3851 xMax += 2 * EXTRA_BORDER_X;
3852 yMax += 2 * EXTRA_BORDER_Y;
eb6c4508
VZ
3853
3854 // account for the scrollbars if necessary
3855 const wxSize sizeAll = GetClientSize();
3856 if ( xMax > sizeAll.x )
3857 yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
3858 if ( yMax > sizeAll.y )
3859 xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
11ebea16
VZ
3860
3861 return wxRect(0, 0, xMax, yMax);
3862}
3863
62d89eb4 3864void wxListMainWindow::GetItemRect( long index, wxRect &rect ) const
c801d85f 3865{
cf1dfa6b
VZ
3866 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
3867 _T("invalid index in GetItemRect") );
3868
1e54be64
VZ
3869 // ensure that we're laid out, otherwise we could return nonsense
3870 if ( m_dirty )
3871 {
3872 wxConstCast(this, wxListMainWindow)->
ca65c044 3873 RecalculatePositions(true /* no refresh */);
1e54be64
VZ
3874 }
3875
5cd89174 3876 rect = GetLineRect((size_t)index);
b54e41c5 3877
cf1dfa6b 3878 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
e1e955e1 3879}
c801d85f 3880
62d89eb4 3881bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
c801d85f 3882{
cf1dfa6b
VZ
3883 wxRect rect;
3884 GetItemRect(item, rect);
bd8289c1 3885
cf1dfa6b
VZ
3886 pos.x = rect.x;
3887 pos.y = rect.y;
bd8289c1 3888
ca65c044 3889 return true;
e1e955e1 3890}
c801d85f 3891
cf1dfa6b
VZ
3892// ----------------------------------------------------------------------------
3893// geometry calculation
3894// ----------------------------------------------------------------------------
3895
34bbbc27 3896void wxListMainWindow::RecalculatePositions(bool noRefresh)
c801d85f 3897{
1e6d9499 3898 wxClientDC dc( this );
92976ab6 3899 dc.SetFont( GetFont() );
c801d85f 3900
13602ebd
VZ
3901 const size_t count = GetItemCount();
3902
cf1dfa6b
VZ
3903 int iconSpacing;
3904 if ( HasFlag(wxLC_ICON) )
3905 iconSpacing = m_normal_spacing;
3906 else if ( HasFlag(wxLC_SMALL_ICON) )
3907 iconSpacing = m_small_spacing;
3908 else
3909 iconSpacing = 0;
004fd0c8 3910
bf632edd 3911 // Note that we do not call GetClientSize() here but
34621cc5 3912 // GetSize() and subtract the border size for sunken
bf632edd
RR
3913 // borders manually. This is technically incorrect,
3914 // but we need to know the client area's size WITHOUT
3915 // scrollbars here. Since we don't know if there are
3916 // any scrollbars, we use GetSize() instead. Another
3917 // solution would be to call SetScrollbars() here to
3918 // remove the scrollbars and call GetClientSize() then,
3919 // but this might result in flicker and - worse - will
3920 // reset the scrollbars to 0 which is not good at all
3921 // if you resize a dialog/window, but don't want to
3922 // reset the window scrolling. RR.
3923 // Furthermore, we actually do NOT subtract the border
3924 // width as 2 pixels is just the extra space which we
3925 // need around the actual content in the window. Other-
3926 // wise the text would e.g. touch the upper border. RR.
cf1dfa6b
VZ
3927 int clientWidth,
3928 clientHeight;
bf632edd 3929 GetSize( &clientWidth, &clientHeight );
a77ec46d 3930
6d78bbe6
VZ
3931 const int lineHeight = GetLineHeight();
3932
b5d43d1d 3933 if ( InReportView() )
cf1dfa6b 3934 {
13602ebd 3935 // all lines have the same height and we scroll one line per step
277ea1b4 3936 int entireHeight = count * lineHeight + LINE_SPACING;
bd8289c1 3937
b54e41c5
VZ
3938 m_linesPerPage = clientHeight / lineHeight;
3939
3940 ResetVisibleLinesRange();
2c1f73ee 3941
13602ebd
VZ
3942 SetScrollbars( SCROLL_UNIT_X, lineHeight,
3943 GetHeaderWidth() / SCROLL_UNIT_X,
3944 (entireHeight + lineHeight - 1) / lineHeight,
cf1dfa6b
VZ
3945 GetScrollPos(wxHORIZONTAL),
3946 GetScrollPos(wxVERTICAL),
ca65c044 3947 true );
e1e955e1 3948 }
cf1dfa6b 3949 else // !report
92976ab6 3950 {
13602ebd
VZ
3951 // we have 3 different layout strategies: either layout all items
3952 // horizontally/vertically (wxLC_ALIGN_XXX styles explicitly given) or
3953 // to arrange them in top to bottom, left to right (don't ask me why
3954 // not the other way round...) order
3955 if ( HasFlag(wxLC_ALIGN_LEFT | wxLC_ALIGN_TOP) )
e487524e 3956 {
13602ebd
VZ
3957 int x = EXTRA_BORDER_X;
3958 int y = EXTRA_BORDER_Y;
cf1dfa6b 3959
94dd23ae
VZ
3960 wxCoord widthMax = 0;
3961
3962 size_t i;
3963 for ( i = 0; i < count; i++ )
92976ab6 3964 {
cf1dfa6b 3965 wxListLineData *line = GetLine(i);
92976ab6 3966 line->CalculateSize( &dc, iconSpacing );
13602ebd 3967 line->SetPosition( x, y, iconSpacing );
cf1dfa6b 3968
5cd89174 3969 wxSize sizeLine = GetLineSize(i);
cf1dfa6b 3970
13602ebd
VZ
3971 if ( HasFlag(wxLC_ALIGN_TOP) )
3972 {
94dd23ae
VZ
3973 if ( sizeLine.x > widthMax )
3974 widthMax = sizeLine.x;
3975
13602ebd
VZ
3976 y += sizeLine.y;
3977 }
3978 else // wxLC_ALIGN_LEFT
3979 {
3980 x += sizeLine.x + MARGIN_BETWEEN_ROWS;
3981 }
3982 }
3983
94dd23ae
VZ
3984 if ( HasFlag(wxLC_ALIGN_TOP) )
3985 {
3986 // traverse the items again and tweak their sizes so that they are
3987 // all the same in a row
3988 for ( i = 0; i < count; i++ )
3989 {
3990 wxListLineData *line = GetLine(i);
3991 line->m_gi->ExtendWidth(widthMax);
3992 }
3993 }
3994
13602ebd
VZ
3995 SetScrollbars
3996 (
3997 SCROLL_UNIT_X,
6d78bbe6 3998 lineHeight,
13602ebd 3999 (x + SCROLL_UNIT_X) / SCROLL_UNIT_X,
6d78bbe6 4000 (y + lineHeight) / lineHeight,
13602ebd
VZ
4001 GetScrollPos( wxHORIZONTAL ),
4002 GetScrollPos( wxVERTICAL ),
ca65c044 4003 true
13602ebd
VZ
4004 );
4005 }
4006 else // "flowed" arrangement, the most complicated case
4007 {
4008 // at first we try without any scrollbars, if the items don't fit into
4009 // the window, we recalculate after subtracting the space taken by the
4010 // scrollbar
4011
8a39593e 4012 int entireWidth = 0;
cf1dfa6b 4013
13602ebd
VZ
4014 for (int tries = 0; tries < 2; tries++)
4015 {
277ea1b4 4016 entireWidth = 2 * EXTRA_BORDER_X;
cf1dfa6b 4017
13602ebd 4018 if (tries == 1)
92976ab6 4019 {
13602ebd
VZ
4020 // Now we have decided that the items do not fit into the
4021 // client area, so we need a scrollbar
4022 entireWidth += SCROLL_UNIT_X;
92976ab6 4023 }
a77ec46d 4024
13602ebd
VZ
4025 int x = EXTRA_BORDER_X;
4026 int y = EXTRA_BORDER_Y;
4027 int maxWidthInThisRow = 0;
4028
4029 m_linesPerPage = 0;
4030 int currentlyVisibleLines = 0;
a77ec46d 4031
13602ebd 4032 for (size_t i = 0; i < count; i++)
92976ab6 4033 {
13602ebd 4034 currentlyVisibleLines++;
277ea1b4 4035 wxListLineData *line = GetLine( i );
13602ebd
VZ
4036 line->CalculateSize( &dc, iconSpacing );
4037 line->SetPosition( x, y, iconSpacing );
4038
277ea1b4 4039 wxSize sizeLine = GetLineSize( i );
a77ec46d 4040
13602ebd
VZ
4041 if ( maxWidthInThisRow < sizeLine.x )
4042 maxWidthInThisRow = sizeLine.x;
4043
4044 y += sizeLine.y;
4045 if (currentlyVisibleLines > m_linesPerPage)
4046 m_linesPerPage = currentlyVisibleLines;
4047
4048 if ( y + sizeLine.y >= clientHeight )
4049 {
4050 currentlyVisibleLines = 0;
4051 y = EXTRA_BORDER_Y;
4052 maxWidthInThisRow += MARGIN_BETWEEN_ROWS;
4053 x += maxWidthInThisRow;
4054 entireWidth += maxWidthInThisRow;
4055 maxWidthInThisRow = 0;
4056 }
4057
4058 // We have reached the last item.
4059 if ( i == count - 1 )
4060 entireWidth += maxWidthInThisRow;
4061
4062 if ( (tries == 0) &&
4063 (entireWidth + SCROLL_UNIT_X > clientWidth) )
4064 {
4065 clientHeight -= wxSystemSettings::
4066 GetMetric(wxSYS_HSCROLL_Y);
4067 m_linesPerPage = 0;
13602ebd
VZ
4068 break;
4069 }
4070
4071 if ( i == count - 1 )
4072 tries = 1; // Everything fits, no second try required.
4073 }
92976ab6 4074 }
bffa1c77 4075
13602ebd
VZ
4076 SetScrollbars
4077 (
4078 SCROLL_UNIT_X,
6d78bbe6 4079 lineHeight,
13602ebd
VZ
4080 (entireWidth + SCROLL_UNIT_X) / SCROLL_UNIT_X,
4081 0,
4082 GetScrollPos( wxHORIZONTAL ),
4083 0,
ca65c044 4084 true
13602ebd
VZ
4085 );
4086 }
e1e955e1 4087 }
cf1dfa6b 4088
34bbbc27
VZ
4089 if ( !noRefresh )
4090 {
4091 // FIXME: why should we call it from here?
4092 UpdateCurrent();
b54e41c5 4093
34bbbc27
VZ
4094 RefreshAll();
4095 }
b54e41c5
VZ
4096}
4097
4098void wxListMainWindow::RefreshAll()
4099{
ca65c044 4100 m_dirty = false;
b54e41c5
VZ
4101 Refresh();
4102
4103 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
70541533 4104 if ( headerWin && headerWin->m_dirty )
b54e41c5 4105 {
ca65c044 4106 headerWin->m_dirty = false;
b54e41c5
VZ
4107 headerWin->Refresh();
4108 }
e1e955e1 4109}
c801d85f 4110
cf1dfa6b 4111void wxListMainWindow::UpdateCurrent()
c801d85f 4112{
b54e41c5 4113 if ( !HasCurrent() && !IsEmpty() )
0ddefeb0 4114 ChangeCurrent(0);
e1e955e1 4115}
c801d85f 4116
19695fbd
VZ
4117long wxListMainWindow::GetNextItem( long item,
4118 int WXUNUSED(geometry),
62d89eb4 4119 int state ) const
c801d85f 4120{
d1022fd6
VZ
4121 long ret = item,
4122 max = GetItemCount();
4123 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
13771c08 4124 _T("invalid listctrl index in GetNextItem()") );
19695fbd
VZ
4125
4126 // notice that we start with the next item (or the first one if item == -1)
4127 // and this is intentional to allow writing a simple loop to iterate over
4128 // all selected items
d1022fd6
VZ
4129 ret++;
4130 if ( ret == max )
277ea1b4
DS
4131 // this is not an error because the index was OK initially,
4132 // just no such item
d1022fd6 4133 return -1;
d1022fd6 4134
cf1dfa6b 4135 if ( !state )
cf1dfa6b
VZ
4136 // any will do
4137 return (size_t)ret;
cf1dfa6b
VZ
4138
4139 size_t count = GetItemCount();
4140 for ( size_t line = (size_t)ret; line < count; line++ )
63852e78 4141 {
cf1dfa6b
VZ
4142 if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
4143 return line;
4144
b54e41c5 4145 if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
cf1dfa6b 4146 return line;
63852e78 4147 }
19695fbd 4148
63852e78 4149 return -1;
e1e955e1 4150}
c801d85f 4151
cf1dfa6b
VZ
4152// ----------------------------------------------------------------------------
4153// deleting stuff
4154// ----------------------------------------------------------------------------
4155
b54e41c5 4156void wxListMainWindow::DeleteItem( long lindex )
c801d85f 4157{
cf1dfa6b
VZ
4158 size_t count = GetItemCount();
4159
b54e41c5 4160 wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
cf1dfa6b
VZ
4161 _T("invalid item index in DeleteItem") );
4162
b54e41c5
VZ
4163 size_t index = (size_t)lindex;
4164
d6ddcd57
VZ
4165 // we don't need to adjust the index for the previous items
4166 if ( HasCurrent() && m_current >= index )
cf1dfa6b 4167 {
d6ddcd57
VZ
4168 // if the current item is being deleted, we want the next one to
4169 // become selected - unless there is no next one - so don't adjust
4170 // m_current in this case
4171 if ( m_current != index || m_current == count - 1 )
d6ddcd57 4172 m_current--;
cf1dfa6b
VZ
4173 }
4174
938b652b 4175 if ( InReportView() )
63852e78 4176 {
7d4813a0
VZ
4177 // mark the Column Max Width cache as dirty if the items in the line
4178 // we're deleting contain the Max Column Width
4179 wxListLineData * const line = GetLine(index);
4180 wxListItemDataList::compatibility_iterator n;
4181 wxListItemData *itemData;
4182 wxListItem item;
4183 int itemWidth;
4184
4185 for (size_t i = 0; i < m_columns.GetCount(); i++)
4186 {
4187 n = line->m_items.Item( i );
4188 itemData = n->GetData();
4189 itemData->GetItem(item);
4190
4191 itemWidth = GetItemWidthWithImage(&item);
4192
4193 if (itemWidth >= m_aColWidths.Item(i)->nMaxWidth)
4194 m_aColWidths.Item(i)->bNeedsUpdate = true;
4195 }
4196
6b4a8d93 4197 ResetVisibleLinesRange();
938b652b
VZ
4198 }
4199
938b652b
VZ
4200 if ( IsVirtual() )
4201 {
4202 m_countVirt--;
b54e41c5
VZ
4203 m_selStore.OnItemDelete(index);
4204 }
4205 else
4206 {
4207 m_lines.RemoveAt( index );
63852e78 4208 }
6b4a8d93 4209
70541533 4210 // we need to refresh the (vert) scrollbar as the number of items changed
ca65c044 4211 m_dirty = true;
91c6cc0e
VZ
4212
4213 SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM );
4214
6b4a8d93 4215 RefreshAfter(index);
e1e955e1 4216}
c801d85f 4217
debe6624 4218void wxListMainWindow::DeleteColumn( int col )
c801d85f 4219{
222ed1d6 4220 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
24b9f055
VZ
4221
4222 wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
bd8289c1 4223
ca65c044 4224 m_dirty = true;
222ed1d6
MB
4225 delete node->GetData();
4226 m_columns.Erase( node );
ae409cb8 4227
df6f82f0
VZ
4228 if ( !IsVirtual() )
4229 {
4230 // update all the items
4231 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4232 {
4233 wxListLineData * const line = GetLine(i);
222ed1d6
MB
4234 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
4235 delete n->GetData();
4236 line->m_items.Erase(n);
df6f82f0
VZ
4237 }
4238 }
4239
7d4813a0
VZ
4240 if ( InReportView() ) // we only cache max widths when in Report View
4241 {
4242 delete m_aColWidths.Item(col);
4243 m_aColWidths.RemoveAt(col);
4244 }
4245
ae409cb8
VZ
4246 // invalidate it as it has to be recalculated
4247 m_headerWidth = 0;
e1e955e1 4248}
c801d85f 4249
5fe143df 4250void wxListMainWindow::DoDeleteAllItems()
c801d85f 4251{
cf1dfa6b 4252 if ( IsEmpty() )
cf1dfa6b
VZ
4253 // nothing to do - in particular, don't send the event
4254 return;
cf1dfa6b 4255
b54e41c5 4256 ResetCurrent();
7c0ea335
VZ
4257
4258 // to make the deletion of all items faster, we don't send the
cf1dfa6b
VZ
4259 // notifications for each item deletion in this case but only one event
4260 // for all of them: this is compatible with wxMSW and documented in
4261 // DeleteAllItems() description
bffa1c77 4262
12c1b46a
RR
4263 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
4264 event.SetEventObject( GetParent() );
4265 GetParent()->GetEventHandler()->ProcessEvent( event );
7c0ea335 4266
b54e41c5
VZ
4267 if ( IsVirtual() )
4268 {
4269 m_countVirt = 0;
850ed6e7 4270 m_selStore.Clear();
b54e41c5
VZ
4271 }
4272
6b4a8d93
VZ
4273 if ( InReportView() )
4274 {
4275 ResetVisibleLinesRange();
7d4813a0
VZ
4276 for (size_t i = 0; i < m_aColWidths.GetCount(); i++)
4277 {
8d36b216 4278 m_aColWidths.Item(i)->bNeedsUpdate = true;
7d4813a0 4279 }
6b4a8d93
VZ
4280 }
4281
5b077d48 4282 m_lines.Clear();
5fe143df 4283}
cf1dfa6b 4284
5fe143df
VZ
4285void wxListMainWindow::DeleteAllItems()
4286{
4287 DoDeleteAllItems();
4288
4289 RecalculatePositions();
e1e955e1 4290}
c801d85f 4291
12c1b46a 4292void wxListMainWindow::DeleteEverything()
c801d85f 4293{
222ed1d6 4294 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
8d36b216 4295 WX_CLEAR_ARRAY(m_aColWidths);
904ccf52
VZ
4296
4297 DeleteAllItems();
e1e955e1 4298}
c801d85f 4299
cf1dfa6b
VZ
4300// ----------------------------------------------------------------------------
4301// scanning for an item
4302// ----------------------------------------------------------------------------
4303
debe6624 4304void wxListMainWindow::EnsureVisible( long index )
c801d85f 4305{
cf1dfa6b
VZ
4306 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4307 _T("invalid index in EnsureVisible") );
4308
4309 // We have to call this here because the label in question might just have
34bbbc27
VZ
4310 // been added and its position is not known yet
4311 if ( m_dirty )
ca65c044 4312 RecalculatePositions(true /* no refresh */);
34bbbc27
VZ
4313
4314 MoveToItem((size_t)index);
e1e955e1 4315}
c801d85f 4316
debe6624 4317long wxListMainWindow::FindItem(long start, const wxString& str, bool WXUNUSED(partial) )
c801d85f 4318{
5b077d48
RR
4319 long pos = start;
4320 wxString tmp = str;
cf1dfa6b
VZ
4321 if (pos < 0)
4322 pos = 0;
54442116 4323
cf1dfa6b
VZ
4324 size_t count = GetItemCount();
4325 for ( size_t i = (size_t)pos; i < count; i++ )
4326 {
4327 wxListLineData *line = GetLine(i);
4328 if ( line->GetText(0) == tmp )
4329 return i;
5b077d48 4330 }
cf1dfa6b
VZ
4331
4332 return wxNOT_FOUND;
e1e955e1 4333}
c801d85f 4334
81ce36aa 4335long wxListMainWindow::FindItem(long start, wxUIntPtr data)
c801d85f 4336{
5b077d48 4337 long pos = start;
cf1dfa6b
VZ
4338 if (pos < 0)
4339 pos = 0;
4340
4341 size_t count = GetItemCount();
4342 for (size_t i = (size_t)pos; i < count; i++)
5b077d48 4343 {
cf1dfa6b 4344 wxListLineData *line = GetLine(i);
5b077d48
RR
4345 wxListItem item;
4346 line->GetItem( 0, item );
cf1dfa6b
VZ
4347 if (item.m_data == data)
4348 return i;
5b077d48 4349 }
cf1dfa6b
VZ
4350
4351 return wxNOT_FOUND;
e1e955e1 4352}
c801d85f 4353
8158e0e1
VZ
4354long wxListMainWindow::FindItem( const wxPoint& pt )
4355{
e6bf9573 4356 size_t topItem;
277ea1b4 4357 GetVisibleLinesRange( &topItem, NULL );
8158e0e1 4358
e6bf9573 4359 wxPoint p;
277ea1b4
DS
4360 GetItemPosition( GetItemCount() - 1, p );
4361 if ( p.y == 0 )
e6bf9573 4362 return topItem;
277ea1b4
DS
4363
4364 long id = (long)floor( pt.y * double(GetItemCount() - topItem - 1) / p.y + topItem );
4365 if ( id >= 0 && id < (long)GetItemCount() )
e6bf9573 4366 return id;
8158e0e1 4367
e6bf9573 4368 return wxNOT_FOUND;
8158e0e1
VZ
4369}
4370
debe6624 4371long wxListMainWindow::HitTest( int x, int y, int &flags )
c801d85f 4372{
aaef15bf 4373 CalcUnscrolledPosition( x, y, &x, &y );
e8741cca 4374
fc4f1d5f
VZ
4375 size_t count = GetItemCount();
4376
b5d43d1d 4377 if ( InReportView() )
c801d85f 4378 {
5cd89174 4379 size_t current = y / GetLineHeight();
fc4f1d5f
VZ
4380 if ( current < count )
4381 {
4382 flags = HitTestLine(current, x, y);
4383 if ( flags )
4384 return current;
4385 }
5cd89174
VZ
4386 }
4387 else // !report
4388 {
4389 // TODO: optimize it too! this is less simple than for report view but
4390 // enumerating all items is still not a way to do it!!
5cd89174 4391 for ( size_t current = 0; current < count; current++ )
5b077d48 4392 {
5cd89174
VZ
4393 flags = HitTestLine(current, x, y);
4394 if ( flags )
4395 return current;
5b077d48 4396 }
e1e955e1 4397 }
cf1dfa6b
VZ
4398
4399 return wxNOT_FOUND;
e1e955e1 4400}
c801d85f 4401
cf1dfa6b
VZ
4402// ----------------------------------------------------------------------------
4403// adding stuff
4404// ----------------------------------------------------------------------------
4405
c801d85f
KB
4406void wxListMainWindow::InsertItem( wxListItem &item )
4407{
cf1dfa6b
VZ
4408 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4409
c7cf7a78 4410 int count = GetItemCount();
33c956e6 4411 wxCHECK_RET( item.m_itemId >= 0, _T("invalid item index") );
cf1dfa6b 4412
33c956e6
RD
4413 if (item.m_itemId > count)
4414 item.m_itemId = count;
b713f891 4415
cf1dfa6b
VZ
4416 size_t id = item.m_itemId;
4417
ca65c044 4418 m_dirty = true;
cf1dfa6b 4419
b5d43d1d 4420 if ( InReportView() )
2b5f62a0 4421 {
2b5f62a0 4422 ResetVisibleLinesRange();
7d4813a0
VZ
4423
4424 // calculate the width of the item and adjust the max column width
4425 wxColWidthInfo *pWidthInfo = m_aColWidths.Item(item.GetColumn());
f5c479cc 4426 int width = GetItemWidthWithImage(&item);
7d4813a0
VZ
4427 item.SetWidth(width);
4428 if (width > pWidthInfo->nMaxWidth)
4429 pWidthInfo->nMaxWidth = width;
1370703e 4430 }
004fd0c8 4431
5cd89174 4432 wxListLineData *line = new wxListLineData(this);
004fd0c8 4433
1e6601c7 4434 line->SetItem( item.m_col, item );
cf1dfa6b
VZ
4435
4436 m_lines.Insert( line, id );
5cd89174 4437
ca65c044 4438 m_dirty = true;
a67e6e54 4439
ab96d680
VZ
4440 // If an item is selected at or below the point of insertion, we need to
4441 // increment the member variables because the current row's index has gone
4442 // up by one
4443 if ( HasCurrent() && m_current >= id )
ab96d680 4444 m_current++;
ab96d680 4445
a67e6e54
VZ
4446 SendNotify(id, wxEVT_COMMAND_LIST_INSERT_ITEM);
4447
5cd89174 4448 RefreshLines(id, GetItemCount() - 1);
e1e955e1 4449}
c801d85f 4450
debe6624 4451void wxListMainWindow::InsertColumn( long col, wxListItem &item )
c801d85f 4452{
ca65c044 4453 m_dirty = true;
b5d43d1d 4454 if ( InReportView() )
3db7be80 4455 {
54442116
VZ
4456 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
4457 item.m_width = GetTextLength( item.m_text );
df6f82f0 4458
5b077d48 4459 wxListHeaderData *column = new wxListHeaderData( item );
7d4813a0
VZ
4460 wxColWidthInfo *colWidthInfo = new wxColWidthInfo();
4461
df6f82f0
VZ
4462 bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4463 if ( insert )
5b077d48 4464 {
7d4813a0
VZ
4465 wxListHeaderDataList::compatibility_iterator
4466 node = m_columns.Item( col );
24b9f055 4467 m_columns.Insert( node, column );
7d4813a0 4468 m_aColWidths.Insert( colWidthInfo, col );
5b077d48
RR
4469 }
4470 else
4471 {
4472 m_columns.Append( column );
7d4813a0 4473 m_aColWidths.Add( colWidthInfo );
5b077d48 4474 }
ae409cb8 4475
df6f82f0
VZ
4476 if ( !IsVirtual() )
4477 {
4478 // update all the items
4479 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4480 {
4481 wxListLineData * const line = GetLine(i);
4482 wxListItemData * const data = new wxListItemData(this);
4483 if ( insert )
4484 line->m_items.Insert(col, data);
4485 else
4486 line->m_items.Append(data);
4487 }
4488 }
4489
ae409cb8
VZ
4490 // invalidate it as it has to be recalculated
4491 m_headerWidth = 0;
3db7be80 4492 }
e1e955e1 4493}
c801d85f 4494
7d4813a0
VZ
4495int wxListMainWindow::GetItemWidthWithImage(wxListItem * item)
4496{
4497 int width = 0;
4498 wxClientDC dc(this);
4499
4500 dc.SetFont( GetFont() );
4501
4502 if (item->GetImage() != -1)
4503 {
4504 int ix, iy;
4505 GetImageSize( item->GetImage(), ix, iy );
4506 width += ix + 5;
4507 }
4508
4509 if (!item->GetText().empty())
4510 {
4511 wxCoord w;
4512 dc.GetTextExtent( item->GetText(), &w, NULL );
4513 width += w;
4514 }
4515
4516 return width;
4517}
4518
cf1dfa6b
VZ
4519// ----------------------------------------------------------------------------
4520// sorting
4521// ----------------------------------------------------------------------------
4522
c801d85f
KB
4523wxListCtrlCompare list_ctrl_compare_func_2;
4524long list_ctrl_compare_data;
4525
f6bcfd97 4526int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
c801d85f 4527{
f6bcfd97
BP
4528 wxListLineData *line1 = *arg1;
4529 wxListLineData *line2 = *arg2;
5b077d48
RR
4530 wxListItem item;
4531 line1->GetItem( 0, item );
81ce36aa 4532 wxUIntPtr data1 = item.m_data;
5b077d48 4533 line2->GetItem( 0, item );
81ce36aa 4534 wxUIntPtr data2 = item.m_data;
5b077d48 4535 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
e1e955e1 4536}
c801d85f
KB
4537
4538void wxListMainWindow::SortItems( wxListCtrlCompare fn, long data )
4539{
5b077d48
RR
4540 list_ctrl_compare_func_2 = fn;
4541 list_ctrl_compare_data = data;
4542 m_lines.Sort( list_ctrl_compare_func_1 );
ca65c044 4543 m_dirty = true;
e1e955e1 4544}
c801d85f 4545
cf1dfa6b
VZ
4546// ----------------------------------------------------------------------------
4547// scrolling
4548// ----------------------------------------------------------------------------
4549
7c74e7fe
SC
4550void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4551{
b54e41c5
VZ
4552 // update our idea of which lines are shown when we redraw the window the
4553 // next time
4554 ResetVisibleLinesRange();
cf1dfa6b 4555
29149a64 4556 // FIXME
4e6b6f21 4557#if ( defined(__WXGTK__) || defined(__WXMAC__) ) && !defined(__WXUNIVERSAL__)
29149a64
VZ
4558 wxScrolledWindow::OnScroll(event);
4559#else
1e6feb95 4560 HandleOnScroll( event );
29149a64 4561#endif
30954328 4562
cf1dfa6b 4563 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
7c74e7fe 4564 {
3cd94a0d 4565 wxGenericListCtrl* lc = GetListCtrl();
cf1dfa6b
VZ
4566 wxCHECK_RET( lc, _T("no listctrl window?") );
4567
afbe906a
RR
4568 lc->m_headerWin->Refresh();
4569 lc->m_headerWin->Update();
7c74e7fe 4570 }
cf1dfa6b
VZ
4571}
4572
5cd89174
VZ
4573int wxListMainWindow::GetCountPerPage() const
4574{
4575 if ( !m_linesPerPage )
4576 {
4577 wxConstCast(this, wxListMainWindow)->
4578 m_linesPerPage = GetClientSize().y / GetLineHeight();
4579 }
4580
4581 return m_linesPerPage;
4582}
4583
b54e41c5 4584void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
cf1dfa6b 4585{
b5d43d1d 4586 wxASSERT_MSG( InReportView(), _T("this is for report mode only") );
cf1dfa6b 4587
b54e41c5
VZ
4588 if ( m_lineFrom == (size_t)-1 )
4589 {
b54e41c5 4590 size_t count = GetItemCount();
6522713c
VZ
4591 if ( count )
4592 {
4593 m_lineFrom = GetScrollPos(wxVERTICAL);
cf1dfa6b 4594
152c57f2
VZ
4595 // this may happen if SetScrollbars() hadn't been called yet
4596 if ( m_lineFrom >= count )
bcc0da5c 4597 m_lineFrom = count - 1;
cf1dfa6b 4598
6522713c
VZ
4599 // we redraw one extra line but this is needed to make the redrawing
4600 // logic work when there is a fractional number of lines on screen
4601 m_lineTo = m_lineFrom + m_linesPerPage;
4602 if ( m_lineTo >= count )
4603 m_lineTo = count - 1;
4604 }
4605 else // empty control
4606 {
4607 m_lineFrom = 0;
4608 m_lineTo = (size_t)-1;
4609 }
cf1dfa6b 4610 }
b54e41c5 4611
ae167d2f
VZ
4612 wxASSERT_MSG( IsEmpty() ||
4613 (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
6b4a8d93
VZ
4614 _T("GetVisibleLinesRange() returns incorrect result") );
4615
b54e41c5
VZ
4616 if ( from )
4617 *from = m_lineFrom;
4618 if ( to )
4619 *to = m_lineTo;
7c74e7fe
SC
4620}
4621
c801d85f 4622// -------------------------------------------------------------------------------------
3cd94a0d 4623// wxGenericListCtrl
c801d85f
KB
4624// -------------------------------------------------------------------------------------
4625
3cd94a0d
JS
4626IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl, wxControl)
4627
3cd94a0d
JS
4628BEGIN_EVENT_TABLE(wxGenericListCtrl,wxControl)
4629 EVT_SIZE(wxGenericListCtrl::OnSize)
c801d85f
KB
4630END_EVENT_TABLE()
4631
3cd94a0d 4632wxGenericListCtrl::wxGenericListCtrl()
c801d85f 4633{
80020b62
JS
4634 m_imageListNormal = (wxImageListType *) NULL;
4635 m_imageListSmall = (wxImageListType *) NULL;
4636 m_imageListState = (wxImageListType *) NULL;
cf1dfa6b
VZ
4637
4638 m_ownsImageListNormal =
4639 m_ownsImageListSmall =
ca65c044 4640 m_ownsImageListState = false;
cf1dfa6b 4641
5b077d48
RR
4642 m_mainWin = (wxListMainWindow*) NULL;
4643 m_headerWin = (wxListHeaderWindow*) NULL;
f8252483 4644 m_headerHeight = 0;
c801d85f
KB
4645}
4646
3cd94a0d 4647wxGenericListCtrl::~wxGenericListCtrl()
c801d85f 4648{
cf1dfa6b
VZ
4649 if (m_ownsImageListNormal)
4650 delete m_imageListNormal;
4651 if (m_ownsImageListSmall)
4652 delete m_imageListSmall;
4653 if (m_ownsImageListState)
4654 delete m_imageListState;
4655}
4656
f8252483
JS
4657void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4658{
86351e4a
VZ
4659 if ( m_headerWin )
4660 {
fec8346a 4661#ifdef __WXMAC__
277ea1b4 4662 SInt32 h;
0afa5859 4663 GetThemeMetric( kThemeMetricListHeaderHeight, &h );
fec8346a 4664#else
86351e4a
VZ
4665 // we use 'g' to get the descent, too
4666 int w, h, d;
4667 m_headerWin->GetTextExtent(wxT("Hg"), &w, &h, &d);
4668 h += d + 2 * HEADER_OFFSET_Y + EXTRA_HEIGHT;
fec8346a 4669#endif
277ea1b4 4670
8b1ed8d9
VZ
4671 // only update if changed
4672 if ( h != m_headerHeight )
86351e4a
VZ
4673 {
4674 m_headerHeight = h;
4675
4676 m_headerWin->SetSize(m_headerWin->GetSize().x, m_headerHeight);
4677
b5d43d1d 4678 if ( HasHeader() )
ca65c044 4679 ResizeReportView(true);
86351e4a
VZ
4680 }
4681 }
f8252483
JS
4682}
4683
3cd94a0d 4684void wxGenericListCtrl::CreateHeaderWindow()
cf1dfa6b
VZ
4685{
4686 m_headerWin = new wxListHeaderWindow
4687 (
ca65c044 4688 this, wxID_ANY, m_mainWin,
c47addef 4689 wxPoint(0,0),
f8252483 4690 wxSize(GetClientSize().x, m_headerHeight),
cf1dfa6b
VZ
4691 wxTAB_TRAVERSAL
4692 );
f8252483 4693 CalculateAndSetHeaderHeight();
c801d85f
KB
4694}
4695
3cd94a0d 4696bool wxGenericListCtrl::Create(wxWindow *parent,
25e3a937
VZ
4697 wxWindowID id,
4698 const wxPoint &pos,
4699 const wxSize &size,
4700 long style,
25e3a937 4701 const wxValidator &validator,
25e3a937 4702 const wxString &name)
c801d85f 4703{
cf1dfa6b
VZ
4704 m_imageListNormal =
4705 m_imageListSmall =
80020b62 4706 m_imageListState = (wxImageListType *) NULL;
cf1dfa6b
VZ
4707 m_ownsImageListNormal =
4708 m_ownsImageListSmall =
ca65c044 4709 m_ownsImageListState = false;
cf1dfa6b 4710
5b077d48
RR
4711 m_mainWin = (wxListMainWindow*) NULL;
4712 m_headerWin = (wxListHeaderWindow*) NULL;
bd8289c1 4713
3fc93ebd
JS
4714 m_headerHeight = 0;
4715
b54e41c5 4716 if ( !(style & wxLC_MASK_TYPE) )
5b077d48 4717 {
25e3a937 4718 style = style | wxLC_LIST;
5b077d48 4719 }
f6bcfd97 4720
07215d86 4721 if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) )
ca65c044 4722 return false;
f6bcfd97 4723
b54e41c5 4724 // don't create the inner window with the border
6d460f76 4725 style &= ~wxBORDER_MASK;
bd8289c1 4726
277ea1b4 4727 m_mainWin = new wxListMainWindow( this, wxID_ANY, wxPoint(0, 0), size, style );
bd8289c1 4728
b713f891 4729#ifdef __WXMAC_CARBON__
309e4c14
SC
4730 // Human Interface Guidelines ask us for a special font in this case
4731 if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL )
4732 {
277ea1b4
DS
4733 wxFont font;
4734 font.MacCreateThemeFont( kThemeViewsFont );
4735 SetFont( font );
309e4c14 4736 }
f89d65ea 4737#endif
277ea1b4 4738
b5d43d1d 4739 if ( InReportView() )
ea451729 4740 {
cf1dfa6b 4741 CreateHeaderWindow();
277ea1b4 4742
d108f236
SC
4743#ifdef __WXMAC_CARBON__
4744 if (m_headerWin)
4745 {
277ea1b4
DS
4746 wxFont font;
4747 font.MacCreateThemeFont( kThemeSmallSystemFont );
d108f236
SC
4748 m_headerWin->SetFont( font );
4749 CalculateAndSetHeaderHeight();
4750 }
4751#endif
277ea1b4 4752
b54e41c5 4753 if ( HasFlag(wxLC_NO_HEADER) )
cf1dfa6b 4754 // VZ: why do we create it at all then?
ca65c044 4755 m_headerWin->Show( false );
ea451729 4756 }
bd8289c1 4757
35d4c967 4758 SetBestSize(size);
ca65c044
WS
4759
4760 return true;
e1e955e1 4761}
c801d85f 4762
3cd94a0d 4763void wxGenericListCtrl::SetSingleStyle( long style, bool add )
c801d85f 4764{
b54e41c5
VZ
4765 wxASSERT_MSG( !(style & wxLC_VIRTUAL),
4766 _T("wxLC_VIRTUAL can't be [un]set") );
4767
f03fc89f 4768 long flag = GetWindowStyle();
bd8289c1 4769
5b077d48
RR
4770 if (add)
4771 {
cf1dfa6b 4772 if (style & wxLC_MASK_TYPE)
b54e41c5 4773 flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
cf1dfa6b
VZ
4774 if (style & wxLC_MASK_ALIGN)
4775 flag &= ~wxLC_MASK_ALIGN;
4776 if (style & wxLC_MASK_SORT)
4777 flag &= ~wxLC_MASK_SORT;
5b077d48 4778 }
c801d85f 4779
5b077d48 4780 if (add)
5b077d48 4781 flag |= style;
5b077d48 4782 else
cf1dfa6b 4783 flag &= ~style;
bd8289c1 4784
5b077d48 4785 SetWindowStyleFlag( flag );
e1e955e1 4786}
c801d85f 4787
3cd94a0d 4788void wxGenericListCtrl::SetWindowStyleFlag( long flag )
c801d85f 4789{
121a3581
RR
4790 if (m_mainWin)
4791 {
4792 m_mainWin->DeleteEverything();
c801d85f 4793
a95cdab8 4794 // has the header visibility changed?
b5d43d1d
VZ
4795 bool hasHeader = HasHeader();
4796 bool willHaveHeader = (flag & wxLC_REPORT) && !(flag & wxLC_NO_HEADER);
c801d85f 4797
a95cdab8 4798 if ( hasHeader != willHaveHeader )
5b077d48 4799 {
a95cdab8
VZ
4800 // toggle it
4801 if ( hasHeader )
4802 {
4803 if ( m_headerWin )
4804 {
4805 // don't delete, just hide, as we can reuse it later
ca65c044 4806 m_headerWin->Show(false);
a95cdab8
VZ
4807 }
4808 //else: nothing to do
4809 }
4810 else // must show header
5b077d48 4811 {
121a3581
RR
4812 if (!m_headerWin)
4813 {
cf1dfa6b 4814 CreateHeaderWindow();
121a3581 4815 }
a95cdab8 4816 else // already have it, just show
004fd0c8 4817 {
ca65c044 4818 m_headerWin->Show( true );
004fd0c8 4819 }
5b077d48 4820 }
a95cdab8
VZ
4821
4822 ResizeReportView(willHaveHeader);
bffa1c77 4823 }
e1e955e1 4824 }
004fd0c8 4825
5b077d48 4826 wxWindow::SetWindowStyleFlag( flag );
e1e955e1 4827}
c801d85f 4828
3cd94a0d 4829bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
c801d85f 4830{
5b077d48 4831 m_mainWin->GetColumn( col, item );
ca65c044 4832 return true;
e1e955e1 4833}
c801d85f 4834
3cd94a0d 4835bool wxGenericListCtrl::SetColumn( int col, wxListItem& item )
c801d85f 4836{
5b077d48 4837 m_mainWin->SetColumn( col, item );
ca65c044 4838 return true;
e1e955e1 4839}
c801d85f 4840
3cd94a0d 4841int wxGenericListCtrl::GetColumnWidth( int col ) const
c801d85f 4842{
5b077d48 4843 return m_mainWin->GetColumnWidth( col );
e1e955e1 4844}
c801d85f 4845
3cd94a0d 4846bool wxGenericListCtrl::SetColumnWidth( int col, int width )
c801d85f 4847{
5b077d48 4848 m_mainWin->SetColumnWidth( col, width );
ca65c044 4849 return true;
e1e955e1 4850}
c801d85f 4851
3cd94a0d 4852int wxGenericListCtrl::GetCountPerPage() const
c801d85f
KB
4853{
4854 return m_mainWin->GetCountPerPage(); // different from Windows ?
e1e955e1 4855}
c801d85f 4856
3cd94a0d 4857bool wxGenericListCtrl::GetItem( wxListItem &info ) const
c801d85f 4858{
5b077d48 4859 m_mainWin->GetItem( info );
ca65c044 4860 return true;
e1e955e1 4861}
c801d85f 4862
3cd94a0d 4863bool wxGenericListCtrl::SetItem( wxListItem &info )
c801d85f 4864{
5b077d48 4865 m_mainWin->SetItem( info );
ca65c044 4866 return true;
e1e955e1 4867}
c801d85f 4868
3cd94a0d 4869long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
c801d85f 4870{
5b077d48
RR
4871 wxListItem info;
4872 info.m_text = label;
4873 info.m_mask = wxLIST_MASK_TEXT;
4874 info.m_itemId = index;
4875 info.m_col = col;
4876 if ( imageId > -1 )
4877 {
4878 info.m_image = imageId;
4879 info.m_mask |= wxLIST_MASK_IMAGE;
277ea1b4
DS
4880 }
4881
5b077d48 4882 m_mainWin->SetItem(info);
ca65c044 4883 return true;
e1e955e1 4884}
c801d85f 4885
3cd94a0d 4886int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
c801d85f 4887{
5b077d48 4888 return m_mainWin->GetItemState( item, stateMask );
e1e955e1 4889}
c801d85f 4890
3cd94a0d 4891bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
c801d85f 4892{
5b077d48 4893 m_mainWin->SetItemState( item, state, stateMask );
ca65c044 4894 return true;
e1e955e1 4895}
c801d85f 4896
aba3d35e
VZ
4897bool
4898wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
c3627a00
JS
4899{
4900 wxListItem info;
4901 info.m_image = image;
4902 info.m_mask = wxLIST_MASK_IMAGE;
4903 info.m_itemId = item;
4904 m_mainWin->SetItem( info );
4905 return true;
4906}
c801d85f 4907
3cd94a0d 4908wxString wxGenericListCtrl::GetItemText( long item ) const
c801d85f 4909{
62d89eb4 4910 return m_mainWin->GetItemText(item);
e1e955e1 4911}
c801d85f 4912
3cd94a0d 4913void wxGenericListCtrl::SetItemText( long item, const wxString& str )
c801d85f 4914{
62d89eb4 4915 m_mainWin->SetItemText(item, str);
e1e955e1 4916}
c801d85f 4917
81ce36aa 4918wxUIntPtr wxGenericListCtrl::GetItemData( long item ) const
c801d85f 4919{
5b077d48 4920 wxListItem info;
39a7f085 4921 info.m_mask = wxLIST_MASK_DATA;
5b077d48
RR
4922 info.m_itemId = item;
4923 m_mainWin->GetItem( info );
4924 return info.m_data;
e1e955e1 4925}
c801d85f 4926
3cd94a0d 4927bool wxGenericListCtrl::SetItemData( long item, long data )
c801d85f 4928{
5b077d48
RR
4929 wxListItem info;
4930 info.m_mask = wxLIST_MASK_DATA;
4931 info.m_itemId = item;
4932 info.m_data = data;
4933 m_mainWin->SetItem( info );
ca65c044 4934 return true;
e1e955e1 4935}
c801d85f 4936
11ebea16
VZ
4937wxRect wxGenericListCtrl::GetViewRect() const
4938{
4939 return m_mainWin->GetViewRect();
4940}
4941
277ea1b4 4942bool wxGenericListCtrl::GetItemRect( long item, wxRect &rect, int WXUNUSED(code) ) const
c801d85f 4943{
5b077d48 4944 m_mainWin->GetItemRect( item, rect );
7e6874c0 4945 if ( m_mainWin->HasHeader() )
f8252483 4946 rect.y += m_headerHeight + 1;
ca65c044 4947 return true;
e1e955e1 4948}
c801d85f 4949
3cd94a0d 4950bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
c801d85f 4951{
5b077d48 4952 m_mainWin->GetItemPosition( item, pos );
ca65c044 4953 return true;
e1e955e1 4954}
c801d85f 4955
3cd94a0d 4956bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
c801d85f 4957{
5b077d48 4958 return 0;
e1e955e1 4959}
c801d85f 4960
3cd94a0d 4961int wxGenericListCtrl::GetItemCount() const
c801d85f 4962{
5b077d48 4963 return m_mainWin->GetItemCount();
e1e955e1 4964}
c801d85f 4965
3cd94a0d 4966int wxGenericListCtrl::GetColumnCount() const
92976ab6 4967{
5b077d48 4968 return m_mainWin->GetColumnCount();
92976ab6
RR
4969}
4970
3cd94a0d 4971void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
33d0b396 4972{
5b077d48 4973 m_mainWin->SetItemSpacing( spacing, isSmall );
e1e955e1 4974}
33d0b396 4975
5db8d758
VZ
4976wxSize wxGenericListCtrl::GetItemSpacing() const
4977{
f58fc140 4978 const int spacing = m_mainWin->GetItemSpacing(HasFlag(wxLC_SMALL_ICON));
5db8d758
VZ
4979
4980 return wxSize(spacing, spacing);
4981}
4982
3cd94a0d 4983int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const
c801d85f 4984{
5b077d48 4985 return m_mainWin->GetItemSpacing( isSmall );
e1e955e1 4986}
c801d85f 4987
3cd94a0d 4988void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
5b98eb2f
RL
4989{
4990 wxListItem info;
4991 info.m_itemId = item;
4992 info.SetTextColour( col );
4993 m_mainWin->SetItem( info );
4994}
4995
3cd94a0d 4996wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
5b98eb2f
RL
4997{
4998 wxListItem info;
4999 info.m_itemId = item;
5000 m_mainWin->GetItem( info );
5001 return info.GetTextColour();
5002}
5003
3cd94a0d 5004void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
5b98eb2f
RL
5005{
5006 wxListItem info;
5007 info.m_itemId = item;
5008 info.SetBackgroundColour( col );
5009 m_mainWin->SetItem( info );
5010}
5011
3cd94a0d 5012wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
5b98eb2f
RL
5013{
5014 wxListItem info;
5015 info.m_itemId = item;
5016 m_mainWin->GetItem( info );
5017 return info.GetBackgroundColour();
5018}
5019
35c2acd4
MW
5020void wxGenericListCtrl::SetItemFont( long item, const wxFont &f )
5021{
5022 wxListItem info;
5023 info.m_itemId = item;
5024 info.SetFont( f );
5025 m_mainWin->SetItem( info );
5026}
5027
5028wxFont wxGenericListCtrl::GetItemFont( long item ) const
5029{
5030 wxListItem info;
5031 info.m_itemId = item;
5032 m_mainWin->GetItem( info );
5033 return info.GetFont();
5034}
5035
3cd94a0d 5036int wxGenericListCtrl::GetSelectedItemCount() const
c801d85f 5037{
5b077d48 5038 return m_mainWin->GetSelectedItemCount();
e1e955e1 5039}
c801d85f 5040
3cd94a0d 5041wxColour wxGenericListCtrl::GetTextColour() const
c801d85f 5042{
0530737d 5043 return GetForegroundColour();
e1e955e1 5044}
c801d85f 5045
3cd94a0d 5046void wxGenericListCtrl::SetTextColour(const wxColour& col)
c801d85f 5047{
0530737d 5048 SetForegroundColour(col);
e1e955e1 5049}
c801d85f 5050
3cd94a0d 5051long wxGenericListCtrl::GetTopItem() const
c801d85f 5052{
2b5f62a0
VZ
5053 size_t top;
5054 m_mainWin->GetVisibleLinesRange(&top, NULL);
5055 return (long)top;
e1e955e1 5056}
c801d85f 5057
3cd94a0d 5058long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
c801d85f 5059{
5b077d48 5060 return m_mainWin->GetNextItem( item, geom, state );
e1e955e1 5061}
c801d85f 5062
80020b62 5063wxImageListType *wxGenericListCtrl::GetImageList(int which) const
c801d85f 5064{
5b077d48 5065 if (which == wxIMAGE_LIST_NORMAL)
5b077d48 5066 return m_imageListNormal;
5b077d48 5067 else if (which == wxIMAGE_LIST_SMALL)
5b077d48 5068 return m_imageListSmall;
5b077d48 5069 else if (which == wxIMAGE_LIST_STATE)
5b077d48 5070 return m_imageListState;
277ea1b4 5071
80020b62 5072 return (wxImageListType *) NULL;
e1e955e1 5073}
c801d85f 5074
80020b62 5075void wxGenericListCtrl::SetImageList( wxImageListType *imageList, int which )
c801d85f 5076{
2e12c11a
VS
5077 if ( which == wxIMAGE_LIST_NORMAL )
5078 {
277ea1b4
DS
5079 if (m_ownsImageListNormal)
5080 delete m_imageListNormal;
2e12c11a 5081 m_imageListNormal = imageList;
ca65c044 5082 m_ownsImageListNormal = false;
2e12c11a
VS
5083 }
5084 else if ( which == wxIMAGE_LIST_SMALL )
5085 {
277ea1b4
DS
5086 if (m_ownsImageListSmall)
5087 delete m_imageListSmall;
2e12c11a 5088 m_imageListSmall = imageList;
ca65c044 5089 m_ownsImageListSmall = false;
2e12c11a
VS
5090 }
5091 else if ( which == wxIMAGE_LIST_STATE )
5092 {
277ea1b4
DS
5093 if (m_ownsImageListState)
5094 delete m_imageListState;
2e12c11a 5095 m_imageListState = imageList;
ca65c044 5096 m_ownsImageListState = false;
2e12c11a
VS
5097 }
5098
5b077d48 5099 m_mainWin->SetImageList( imageList, which );
e1e955e1 5100}
c801d85f 5101
80020b62 5102void wxGenericListCtrl::AssignImageList(wxImageListType *imageList, int which)
2e12c11a
VS
5103{
5104 SetImageList(imageList, which);
5105 if ( which == wxIMAGE_LIST_NORMAL )
ca65c044 5106 m_ownsImageListNormal = true;
2e12c11a 5107 else if ( which == wxIMAGE_LIST_SMALL )
ca65c044 5108 m_ownsImageListSmall = true;
2e12c11a 5109 else if ( which == wxIMAGE_LIST_STATE )
ca65c044 5110 m_ownsImageListState = true;
2e12c11a
VS
5111}
5112
3cd94a0d 5113bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
c801d85f 5114{
5b077d48 5115 return 0;
e1e955e1 5116}
c801d85f 5117
3cd94a0d 5118bool wxGenericListCtrl::DeleteItem( long item )
c801d85f 5119{
5b077d48 5120 m_mainWin->DeleteItem( item );
ca65c044 5121 return true;
e1e955e1 5122}
c801d85f 5123
3cd94a0d 5124bool wxGenericListCtrl::DeleteAllItems()
c801d85f 5125{
5b077d48 5126 m_mainWin->DeleteAllItems();
ca65c044 5127 return true;
e1e955e1 5128}
c801d85f 5129
3cd94a0d 5130bool wxGenericListCtrl::DeleteAllColumns()
bd8289c1 5131{
24b9f055
VZ
5132 size_t count = m_mainWin->m_columns.GetCount();
5133 for ( size_t n = 0; n < count; n++ )
277ea1b4 5134 DeleteColumn( 0 );
ca65c044 5135 return true;
4f22cf8d
RR
5136}
5137
3cd94a0d 5138void wxGenericListCtrl::ClearAll()
4f22cf8d 5139{
5b077d48 5140 m_mainWin->DeleteEverything();
bd8289c1
VZ
5141}
5142
3cd94a0d 5143bool wxGenericListCtrl::DeleteColumn( int col )
c801d85f 5144{
5b077d48 5145 m_mainWin->DeleteColumn( col );
40c08808
VZ
5146
5147 // if we don't have the header any longer, we need to relayout the window
5148 if ( !GetColumnCount() )
ca65c044 5149 ResizeReportView(false /* no header */);
ca65c044 5150 return true;
e1e955e1 5151}
c801d85f 5152
3cd94a0d 5153void wxGenericListCtrl::Edit( long item )
c801d85f 5154{
cf1dfa6b 5155 m_mainWin->EditLabel( item );
e1e955e1 5156}
c801d85f 5157
3cd94a0d 5158bool wxGenericListCtrl::EnsureVisible( long item )
c801d85f 5159{
5b077d48 5160 m_mainWin->EnsureVisible( item );
ca65c044 5161 return true;
e1e955e1 5162}
c801d85f 5163
277ea1b4 5164long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
c801d85f 5165{
5b077d48 5166 return m_mainWin->FindItem( start, str, partial );
e1e955e1 5167}
c801d85f 5168
81ce36aa 5169long wxGenericListCtrl::FindItem( long start, wxUIntPtr data )
c801d85f 5170{
5b077d48 5171 return m_mainWin->FindItem( start, data );
e1e955e1 5172}
c801d85f 5173
8158e0e1 5174long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& pt,
debe6624 5175 int WXUNUSED(direction))
c801d85f 5176{
8158e0e1 5177 return m_mainWin->FindItem( pt );
e1e955e1 5178}
c801d85f 5179
3cd94a0d 5180long wxGenericListCtrl::HitTest( const wxPoint &point, int &flags )
c801d85f 5181{
5b077d48 5182 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
e1e955e1 5183}
c801d85f 5184
3cd94a0d 5185long wxGenericListCtrl::InsertItem( wxListItem& info )
c801d85f 5186{
5b077d48 5187 m_mainWin->InsertItem( info );
2ebcd5f5 5188 return info.m_itemId;
e1e955e1 5189}
c801d85f 5190
3cd94a0d 5191long wxGenericListCtrl::InsertItem( long index, const wxString &label )
c801d85f 5192{
51cc4dad
RR
5193 wxListItem info;
5194 info.m_text = label;
5195 info.m_mask = wxLIST_MASK_TEXT;
5196 info.m_itemId = index;
5197 return InsertItem( info );
e1e955e1 5198}
c801d85f 5199
3cd94a0d 5200long wxGenericListCtrl::InsertItem( long index, int imageIndex )
c801d85f 5201{
51cc4dad
RR
5202 wxListItem info;
5203 info.m_mask = wxLIST_MASK_IMAGE;
5204 info.m_image = imageIndex;
5205 info.m_itemId = index;
5206 return InsertItem( info );
e1e955e1 5207}
c801d85f 5208
3cd94a0d 5209long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
c801d85f 5210{
51cc4dad
RR
5211 wxListItem info;
5212 info.m_text = label;
5213 info.m_image = imageIndex;
5214 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
5215 info.m_itemId = index;
5216 return InsertItem( info );
e1e955e1 5217}
c801d85f 5218
3cd94a0d 5219long wxGenericListCtrl::InsertColumn( long col, wxListItem &item )
c801d85f 5220{
40c08808
VZ
5221 wxCHECK_MSG( m_headerWin, -1, _T("can't add column in non report mode") );
5222
51cc4dad 5223 m_mainWin->InsertColumn( col, item );
40c08808 5224
277ea1b4
DS
5225 // if we hadn't had a header before but have one now
5226 // then we need to relayout the window
ac53090c 5227 if ( GetColumnCount() == 1 && m_mainWin->HasHeader() )
ca65c044 5228 ResizeReportView(true /* have header */);
40c08808 5229
d3e90957 5230 m_headerWin->Refresh();
25e3a937 5231
51cc4dad 5232 return 0;
e1e955e1 5233}
c801d85f 5234
3cd94a0d 5235long wxGenericListCtrl::InsertColumn( long col, const wxString &heading,
debe6624 5236 int format, int width )
c801d85f 5237{
51cc4dad
RR
5238 wxListItem item;
5239 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
5240 item.m_text = heading;
5241 if (width >= -2)
5242 {
5243 item.m_mask |= wxLIST_MASK_WIDTH;
5244 item.m_width = width;
5245 }
277ea1b4 5246
51cc4dad 5247 item.m_format = format;
c801d85f 5248
51cc4dad 5249 return InsertColumn( col, item );
e1e955e1 5250}
c801d85f 5251
3cd94a0d 5252bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx), int WXUNUSED(dy) )
c801d85f 5253{
51cc4dad 5254 return 0;
e1e955e1 5255}
c801d85f
KB
5256
5257// Sort items.
5258// fn is a function which takes 3 long arguments: item1, item2, data.
5259// item1 is the long data associated with a first item (NOT the index).
5260// item2 is the long data associated with a second item (NOT the index).
5261// data is the same value as passed to SortItems.
5262// The return value is a negative number if the first item should precede the second
5263// item, a positive number of the second item should precede the first,
5264// or zero if the two items are equivalent.
5265// data is arbitrary data to be passed to the sort function.
5266
3cd94a0d 5267bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn, long data )
c801d85f 5268{
51cc4dad 5269 m_mainWin->SortItems( fn, data );
ca65c044 5270 return true;
e1e955e1 5271}
c801d85f 5272
cf1dfa6b 5273// ----------------------------------------------------------------------------
b54e41c5 5274// event handlers
cf1dfa6b
VZ
5275// ----------------------------------------------------------------------------
5276
3cd94a0d 5277void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
53010e52 5278{
b54e41c5 5279 if ( !m_mainWin )
cf1dfa6b 5280 return;
53010e52 5281
a95cdab8 5282 ResizeReportView(m_mainWin->HasHeader());
a95cdab8
VZ
5283 m_mainWin->RecalculatePositions();
5284}
5285
3cd94a0d 5286void wxGenericListCtrl::ResizeReportView(bool showHeader)
a95cdab8 5287{
b54e41c5 5288 int cw, ch;
51cc4dad 5289 GetClientSize( &cw, &ch );
bd8289c1 5290
a95cdab8 5291 if ( showHeader )
51cc4dad 5292 {
f8252483
JS
5293 m_headerWin->SetSize( 0, 0, cw, m_headerHeight );
5294 m_mainWin->SetSize( 0, m_headerHeight + 1, cw, ch - m_headerHeight - 1 );
51cc4dad 5295 }
cf1dfa6b 5296 else // no header window
51cc4dad 5297 {
cf1dfa6b 5298 m_mainWin->SetSize( 0, 0, cw, ch );
51cc4dad 5299 }
b54e41c5 5300}
cf1dfa6b 5301
5180055b 5302void wxGenericListCtrl::OnInternalIdle()
b54e41c5 5303{
5180055b 5304 wxWindow::OnInternalIdle();
86351e4a 5305
b54e41c5
VZ
5306 // do it only if needed
5307 if ( !m_mainWin->m_dirty )
5308 return;
5309
5310 m_mainWin->RecalculatePositions();
e1e955e1 5311}
53010e52 5312
cf1dfa6b
VZ
5313// ----------------------------------------------------------------------------
5314// font/colours
5315// ----------------------------------------------------------------------------
5316
3cd94a0d 5317bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
bd8289c1 5318{
51cc4dad
RR
5319 if (m_mainWin)
5320 {
5321 m_mainWin->SetBackgroundColour( colour );
ca65c044 5322 m_mainWin->m_dirty = true;
51cc4dad 5323 }
004fd0c8 5324
ca65c044 5325 return true;
e4d06860
RR
5326}
5327
3cd94a0d 5328bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
bd8289c1 5329{
f03fc89f 5330 if ( !wxWindow::SetForegroundColour( colour ) )
ca65c044 5331 return false;
004fd0c8 5332
51cc4dad
RR
5333 if (m_mainWin)
5334 {
5335 m_mainWin->SetForegroundColour( colour );
ca65c044 5336 m_mainWin->m_dirty = true;
51cc4dad 5337 }
004fd0c8 5338
51cc4dad 5339 if (m_headerWin)
51cc4dad 5340 m_headerWin->SetForegroundColour( colour );
f03fc89f 5341
ca65c044 5342 return true;
e4d06860 5343}
bd8289c1 5344
3cd94a0d 5345bool wxGenericListCtrl::SetFont( const wxFont &font )
bd8289c1 5346{
f03fc89f 5347 if ( !wxWindow::SetFont( font ) )
ca65c044 5348 return false;
004fd0c8 5349
51cc4dad
RR
5350 if (m_mainWin)
5351 {
5352 m_mainWin->SetFont( font );
ca65c044 5353 m_mainWin->m_dirty = true;
51cc4dad 5354 }
004fd0c8 5355
51cc4dad
RR
5356 if (m_headerWin)
5357 {
5358 m_headerWin->SetFont( font );
f8252483 5359 CalculateAndSetHeaderHeight();
51cc4dad 5360 }
f03fc89f 5361
f8252483
JS
5362 Refresh();
5363
ca65c044 5364 return true;
e4d06860 5365}
c801d85f 5366
944b1f5e 5367#if _USE_VISATTR
35d4c967 5368#include "wx/listbox.h"
944b1f5e 5369#endif
35d4c967 5370
277ea1b4 5371// static
35d4c967
RD
5372wxVisualAttributes
5373wxGenericListCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
5374{
5375#if _USE_VISATTR
5376 // Use the same color scheme as wxListBox
5377 return wxListBox::GetClassDefaultAttributes(variant);
5378#else
e7045784 5379 wxUnusedVar(variant);
35d4c967
RD
5380 wxVisualAttributes attr;
5381 attr.colFg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT);
5382 attr.colBg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX);
5383 attr.font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
5384 return attr;
5385#endif
5386}
5387
cf1dfa6b
VZ
5388// ----------------------------------------------------------------------------
5389// methods forwarded to m_mainWin
5390// ----------------------------------------------------------------------------
5391
efbb7287
VZ
5392#if wxUSE_DRAG_AND_DROP
5393
3cd94a0d 5394void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
efbb7287
VZ
5395{
5396 m_mainWin->SetDropTarget( dropTarget );
5397}
5398
3cd94a0d 5399wxDropTarget *wxGenericListCtrl::GetDropTarget() const
efbb7287
VZ
5400{
5401 return m_mainWin->GetDropTarget();
5402}
5403
277ea1b4 5404#endif
efbb7287 5405
3cd94a0d 5406bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
efbb7287 5407{
ca65c044 5408 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : false;
efbb7287
VZ
5409}
5410
3cd94a0d 5411wxColour wxGenericListCtrl::GetBackgroundColour() const
efbb7287
VZ
5412{
5413 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5414}
5415
3cd94a0d 5416wxColour wxGenericListCtrl::GetForegroundColour() const
efbb7287
VZ
5417{
5418 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5419}
5420
3cd94a0d 5421bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
efbb7287 5422{
3a8c693a 5423#if wxUSE_MENUS
efbb7287 5424 return m_mainWin->PopupMenu( menu, x, y );
3a8c693a 5425#else
ca65c044 5426 return false;
277ea1b4 5427#endif
efbb7287
VZ
5428}
5429
3cd94a0d 5430void wxGenericListCtrl::SetFocus()
efbb7287 5431{
277ea1b4
DS
5432 // The test in window.cpp fails as we are a composite
5433 // window, so it checks against "this", but not m_mainWin.
66370e38 5434 if ( DoFindFocus() != this )
efbb7287
VZ
5435 m_mainWin->SetFocus();
5436}
1e6feb95 5437
3872d96d
RD
5438wxSize wxGenericListCtrl::DoGetBestSize() const
5439{
5440 // Something is better than nothing...
5441 // 100x80 is what the MSW version will get from the default
5442 // wxControl::DoGetBestSize
277ea1b4 5443 return wxSize(100, 80);
3872d96d
RD
5444}
5445
2c1f73ee
VZ
5446// ----------------------------------------------------------------------------
5447// virtual list control support
5448// ----------------------------------------------------------------------------
5449
3cd94a0d 5450wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2c1f73ee
VZ
5451{
5452 // this is a pure virtual function, in fact - which is not really pure
5453 // because the controls which are not virtual don't need to implement it
3cd94a0d 5454 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
2c1f73ee
VZ
5455
5456 return wxEmptyString;
5457}
5458
3cd94a0d 5459int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2c1f73ee 5460{
611a725e
RD
5461 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
5462 -1,
208458a7 5463 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
2c1f73ee
VZ
5464 return -1;
5465}
5466
208458a7
RD
5467int wxGenericListCtrl::OnGetItemColumnImage(long item, long column) const
5468{
5469 if (!column)
5470 return OnGetItemImage(item);
5471
5472 return -1;
08a175ce 5473}
208458a7 5474
999836aa
VZ
5475wxListItemAttr *
5476wxGenericListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
6c02c329
VZ
5477{
5478 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
5479 _T("invalid item index in OnGetItemAttr()") );
5480
5481 // no attributes by default
5482 return NULL;
5483}
5484
3cd94a0d 5485void wxGenericListCtrl::SetItemCount(long count)
2c1f73ee
VZ
5486{
5487 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5488
5489 m_mainWin->SetItemCount(count);
5490}
5491
3cd94a0d 5492void wxGenericListCtrl::RefreshItem(long item)
1a6cb56f
VZ
5493{
5494 m_mainWin->RefreshLine(item);
5495}
5496
3cd94a0d 5497void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
1a6cb56f
VZ
5498{
5499 m_mainWin->RefreshLines(itemFrom, itemTo);
5500}
5501
277ea1b4
DS
5502// Generic wxListCtrl is more or less a container for two other
5503// windows which drawings are done upon. These are namely
5504// 'm_headerWin' and 'm_mainWin'.
5505// Here we override 'virtual wxWindow::Refresh()' to mimic the
5506// behaviour wxListCtrl has under wxMSW.
5507//
2f1e3c46
VZ
5508void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5509{
5510 if (!rect)
5511 {
5512 // The easy case, no rectangle specified.
5513 if (m_headerWin)
5514 m_headerWin->Refresh(eraseBackground);
5515
5516 if (m_mainWin)
5517 m_mainWin->Refresh(eraseBackground);
5518 }
5519 else
5520 {
5521 // Refresh the header window
5522 if (m_headerWin)
5523 {
5524 wxRect rectHeader = m_headerWin->GetRect();
5525 rectHeader.Intersect(*rect);
5526 if (rectHeader.GetWidth() && rectHeader.GetHeight())
5527 {
5528 int x, y;
5529 m_headerWin->GetPosition(&x, &y);
5530 rectHeader.Offset(-x, -y);
5531 m_headerWin->Refresh(eraseBackground, &rectHeader);
5532 }
2f1e3c46
VZ
5533 }
5534
5535 // Refresh the main window
5536 if (m_mainWin)
5537 {
5538 wxRect rectMain = m_mainWin->GetRect();
5539 rectMain.Intersect(*rect);
5540 if (rectMain.GetWidth() && rectMain.GetHeight())
5541 {
5542 int x, y;
5543 m_mainWin->GetPosition(&x, &y);
5544 rectMain.Offset(-x, -y);
5545 m_mainWin->Refresh(eraseBackground, &rectMain);
5546 }
5547 }
5548 }
5549}
5550
3cd94a0d 5551void wxGenericListCtrl::Freeze()
c5c528fc
VZ
5552{
5553 m_mainWin->Freeze();
5554}
5555
3cd94a0d 5556void wxGenericListCtrl::Thaw()
c5c528fc
VZ
5557{
5558 m_mainWin->Thaw();
5559}
5560
1e6feb95 5561#endif // wxUSE_LISTCTRL
5b98eb2f 5562