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