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