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