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