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