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