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