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