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