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