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