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