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