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