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