]> git.saurik.com Git - wxWidgets.git/blame - src/generic/listctrl.cpp
use newly added GetViewRect() instead of trying to guess the list ctrl size ourselves
[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
f8252483
JS
106// // the height of the header window (FIXME: should depend on its font!)
107// static const int HEADER_HEIGHT = 23;
cf1dfa6b
VZ
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
11ebea16
VZ
3776wxRect wxListMainWindow::GetViewRect() const
3777{
3778 wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
3779 _T("wxListCtrl::GetViewRect() only works in icon mode") );
3780
3781 // we need to find the longest/tallest label
3782 wxCoord xMax = 0,
3783 yMax = 0;
3784 const int count = GetItemCount();
3785 if ( count )
3786 {
3787 for ( int i = 0; i < count; i++ )
3788 {
3789 wxRect r;
3790 GetItemRect(i, r);
3791
3792 wxCoord x = r.GetRight(),
3793 y = r.GetBottom();
3794
3795 if ( x > xMax )
3796 xMax = x;
3797 if ( y > yMax )
3798 yMax = y;
3799 }
3800 }
3801
3802#if 0
3803 // account for the scrollbar
3804 yMax += wxSystemSettings::GetMetric(wxSYS_HSCROLL_Y);
3805 xMax += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
3806#endif
3807
3808 return wxRect(0, 0, xMax, yMax);
3809}
3810
62d89eb4 3811void wxListMainWindow::GetItemRect( long index, wxRect &rect ) const
c801d85f 3812{
cf1dfa6b
VZ
3813 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
3814 _T("invalid index in GetItemRect") );
3815
1e54be64
VZ
3816 // ensure that we're laid out, otherwise we could return nonsense
3817 if ( m_dirty )
3818 {
3819 wxConstCast(this, wxListMainWindow)->
3820 RecalculatePositions(TRUE /* no refresh */);
3821 }
3822
5cd89174 3823 rect = GetLineRect((size_t)index);
b54e41c5 3824
cf1dfa6b 3825 CalcScrolledPosition(rect.x, rect.y, &rect.x, &rect.y);
e1e955e1 3826}
c801d85f 3827
62d89eb4 3828bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos) const
c801d85f 3829{
cf1dfa6b
VZ
3830 wxRect rect;
3831 GetItemRect(item, rect);
bd8289c1 3832
cf1dfa6b
VZ
3833 pos.x = rect.x;
3834 pos.y = rect.y;
bd8289c1 3835
cf1dfa6b 3836 return TRUE;
e1e955e1 3837}
c801d85f 3838
cf1dfa6b
VZ
3839// ----------------------------------------------------------------------------
3840// geometry calculation
3841// ----------------------------------------------------------------------------
3842
34bbbc27 3843void wxListMainWindow::RecalculatePositions(bool noRefresh)
c801d85f 3844{
1e6d9499 3845 wxClientDC dc( this );
92976ab6 3846 dc.SetFont( GetFont() );
c801d85f 3847
cf1dfa6b
VZ
3848 int iconSpacing;
3849 if ( HasFlag(wxLC_ICON) )
3850 iconSpacing = m_normal_spacing;
3851 else if ( HasFlag(wxLC_SMALL_ICON) )
3852 iconSpacing = m_small_spacing;
3853 else
3854 iconSpacing = 0;
004fd0c8 3855
bf632edd
RR
3856 // Note that we do not call GetClientSize() here but
3857 // GetSize() and substract the border size for sunken
3858 // borders manually. This is technically incorrect,
3859 // but we need to know the client area's size WITHOUT
3860 // scrollbars here. Since we don't know if there are
3861 // any scrollbars, we use GetSize() instead. Another
3862 // solution would be to call SetScrollbars() here to
3863 // remove the scrollbars and call GetClientSize() then,
3864 // but this might result in flicker and - worse - will
3865 // reset the scrollbars to 0 which is not good at all
3866 // if you resize a dialog/window, but don't want to
3867 // reset the window scrolling. RR.
3868 // Furthermore, we actually do NOT subtract the border
3869 // width as 2 pixels is just the extra space which we
3870 // need around the actual content in the window. Other-
3871 // wise the text would e.g. touch the upper border. RR.
cf1dfa6b
VZ
3872 int clientWidth,
3873 clientHeight;
bf632edd 3874 GetSize( &clientWidth, &clientHeight );
a77ec46d 3875
cf1dfa6b
VZ
3876 if ( HasFlag(wxLC_REPORT) )
3877 {
3878 // all lines have the same height
b54e41c5 3879 int lineHeight = GetLineHeight();
c801d85f 3880
cf1dfa6b 3881 // scroll one line per step
b54e41c5 3882 m_yScroll = lineHeight;
bd8289c1 3883
cf1dfa6b 3884 size_t lineCount = GetItemCount();
b54e41c5 3885 int entireHeight = lineCount*lineHeight + LINE_SPACING;
bd8289c1 3886
b54e41c5
VZ
3887 m_linesPerPage = clientHeight / lineHeight;
3888
3889 ResetVisibleLinesRange();
2c1f73ee 3890
cf1dfa6b 3891 SetScrollbars( m_xScroll, m_yScroll,
c020c47e 3892 GetHeaderWidth() / m_xScroll,
cf1dfa6b
VZ
3893 (entireHeight + m_yScroll - 1)/m_yScroll,
3894 GetScrollPos(wxHORIZONTAL),
3895 GetScrollPos(wxVERTICAL),
3896 TRUE );
e1e955e1 3897 }
cf1dfa6b 3898 else // !report
92976ab6
RR
3899 {
3900 // at first we try without any scrollbar. if the items don't
3901 // fit into the window, we recalculate after subtracting an
3902 // approximated 15 pt for the horizontal scrollbar
004fd0c8 3903
b54e41c5 3904 int entireWidth = 0;
bd8289c1 3905
92976ab6 3906 for (int tries = 0; tries < 2; tries++)
e487524e 3907 {
5d684756
RR
3908 // We start with 4 for the border around all items
3909 entireWidth = 4;
a77ec46d 3910
5d684756
RR
3911 if (tries == 1)
3912 {
3913 // Now we have decided that the items do not fit into the
3914 // client area. Unfortunately, wxWindows sometimes thinks
3915 // that it does fit and therefore NO horizontal scrollbar
3916 // is inserted. This looks ugly, so we fudge here and make
3917 // the calculated width bigger than was actually has been
3918 // calculated. This ensures that wxScrolledWindows puts
3919 // a scrollbar at the bottom of its client area.
3920 entireWidth += SCROLL_UNIT_X;
3921 }
a77ec46d 3922
bf632edd 3923 // Start at 2,2 so the text does not touch the border
5d25c050
RR
3924 int x = 2;
3925 int y = 2;
92976ab6 3926 int maxWidth = 0;
cf1dfa6b
VZ
3927 m_linesPerPage = 0;
3928 int currentlyVisibleLines = 0;
3929
3930 size_t count = GetItemCount();
3931 for (size_t i = 0; i < count; i++)
92976ab6 3932 {
cf1dfa6b
VZ
3933 currentlyVisibleLines++;
3934 wxListLineData *line = GetLine(i);
92976ab6 3935 line->CalculateSize( &dc, iconSpacing );
5d684756 3936 line->SetPosition( x, y, clientWidth, iconSpacing ); // Why clientWidth? (FIXME)
cf1dfa6b 3937
5cd89174 3938 wxSize sizeLine = GetLineSize(i);
cf1dfa6b
VZ
3939
3940 if ( maxWidth < sizeLine.x )
3941 maxWidth = sizeLine.x;
3942
3943 y += sizeLine.y;
3944 if (currentlyVisibleLines > m_linesPerPage)
3945 m_linesPerPage = currentlyVisibleLines;
3946
5d684756 3947 // Assume that the size of the next one is the same... (FIXME)
bf632edd 3948 if ( y + sizeLine.y >= clientHeight )
92976ab6 3949 {
cf1dfa6b 3950 currentlyVisibleLines = 0;
e1208c31 3951 y = 2;
8b53e5a2
RR
3952 x += maxWidth+6;
3953 entireWidth += maxWidth+6;
92976ab6
RR
3954 maxWidth = 0;
3955 }
a77ec46d 3956
5d684756 3957 // We have reached the last item.
cf1dfa6b
VZ
3958 if ( i == count - 1 )
3959 entireWidth += maxWidth;
a77ec46d 3960
5d684756 3961 if ( (tries == 0) && (entireWidth+SCROLL_UNIT_X > clientWidth) )
92976ab6 3962 {
5d684756 3963 clientHeight -= 15; // We guess the scrollbar height. (FIXME)
cf1dfa6b
VZ
3964 m_linesPerPage = 0;
3965 currentlyVisibleLines = 0;
92976ab6
RR
3966 break;
3967 }
a77ec46d 3968
cf1dfa6b 3969 if ( i == count - 1 )
5d684756 3970 tries = 1; // Everything fits, no second try required.
92976ab6 3971 }
e487524e 3972 }
bffa1c77 3973
92976ab6 3974 int scroll_pos = GetScrollPos( wxHORIZONTAL );
cf1dfa6b 3975 SetScrollbars( m_xScroll, m_yScroll, (entireWidth+SCROLL_UNIT_X) / m_xScroll, 0, scroll_pos, 0, TRUE );
e1e955e1 3976 }
cf1dfa6b 3977
34bbbc27
VZ
3978 if ( !noRefresh )
3979 {
3980 // FIXME: why should we call it from here?
3981 UpdateCurrent();
b54e41c5 3982
34bbbc27
VZ
3983 RefreshAll();
3984 }
b54e41c5
VZ
3985}
3986
3987void wxListMainWindow::RefreshAll()
3988{
3989 m_dirty = FALSE;
3990 Refresh();
3991
3992 wxListHeaderWindow *headerWin = GetListCtrl()->m_headerWin;
70541533 3993 if ( headerWin && headerWin->m_dirty )
b54e41c5
VZ
3994 {
3995 headerWin->m_dirty = FALSE;
3996 headerWin->Refresh();
3997 }
e1e955e1 3998}
c801d85f 3999
cf1dfa6b 4000void wxListMainWindow::UpdateCurrent()
c801d85f 4001{
b54e41c5 4002 if ( !HasCurrent() && !IsEmpty() )
92976ab6 4003 {
0ddefeb0 4004 ChangeCurrent(0);
92976ab6 4005 }
e1e955e1 4006}
c801d85f 4007
19695fbd
VZ
4008long wxListMainWindow::GetNextItem( long item,
4009 int WXUNUSED(geometry),
62d89eb4 4010 int state ) const
c801d85f 4011{
d1022fd6
VZ
4012 long ret = item,
4013 max = GetItemCount();
4014 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
13771c08 4015 _T("invalid listctrl index in GetNextItem()") );
19695fbd
VZ
4016
4017 // notice that we start with the next item (or the first one if item == -1)
4018 // and this is intentional to allow writing a simple loop to iterate over
4019 // all selected items
d1022fd6
VZ
4020 ret++;
4021 if ( ret == max )
4022 {
4023 // this is not an error because the index was ok initially, just no
4024 // such item
4025 return -1;
4026 }
4027
cf1dfa6b
VZ
4028 if ( !state )
4029 {
4030 // any will do
4031 return (size_t)ret;
4032 }
4033
4034 size_t count = GetItemCount();
4035 for ( size_t line = (size_t)ret; line < count; line++ )
63852e78 4036 {
cf1dfa6b
VZ
4037 if ( (state & wxLIST_STATE_FOCUSED) && (line == m_current) )
4038 return line;
4039
b54e41c5 4040 if ( (state & wxLIST_STATE_SELECTED) && IsHighlighted(line) )
cf1dfa6b 4041 return line;
63852e78 4042 }
19695fbd 4043
63852e78 4044 return -1;
e1e955e1 4045}
c801d85f 4046
cf1dfa6b
VZ
4047// ----------------------------------------------------------------------------
4048// deleting stuff
4049// ----------------------------------------------------------------------------
4050
b54e41c5 4051void wxListMainWindow::DeleteItem( long lindex )
c801d85f 4052{
cf1dfa6b
VZ
4053 size_t count = GetItemCount();
4054
b54e41c5 4055 wxCHECK_RET( (lindex >= 0) && ((size_t)lindex < count),
cf1dfa6b
VZ
4056 _T("invalid item index in DeleteItem") );
4057
b54e41c5
VZ
4058 size_t index = (size_t)lindex;
4059
d6ddcd57
VZ
4060 // we don't need to adjust the index for the previous items
4061 if ( HasCurrent() && m_current >= index )
cf1dfa6b 4062 {
d6ddcd57
VZ
4063 // if the current item is being deleted, we want the next one to
4064 // become selected - unless there is no next one - so don't adjust
4065 // m_current in this case
4066 if ( m_current != index || m_current == count - 1 )
4067 {
4068 m_current--;
4069 }
cf1dfa6b
VZ
4070 }
4071
938b652b 4072 if ( InReportView() )
63852e78 4073 {
6b4a8d93 4074 ResetVisibleLinesRange();
938b652b
VZ
4075 }
4076
938b652b
VZ
4077 if ( IsVirtual() )
4078 {
4079 m_countVirt--;
b54e41c5
VZ
4080
4081 m_selStore.OnItemDelete(index);
4082 }
4083 else
4084 {
4085 m_lines.RemoveAt( index );
63852e78 4086 }
6b4a8d93 4087
70541533 4088 // we need to refresh the (vert) scrollbar as the number of items changed
b84839ae 4089 m_dirty = TRUE;
91c6cc0e
VZ
4090
4091 SendNotify( index, wxEVT_COMMAND_LIST_DELETE_ITEM );
4092
6b4a8d93 4093 RefreshAfter(index);
e1e955e1 4094}
c801d85f 4095
debe6624 4096void wxListMainWindow::DeleteColumn( int col )
c801d85f 4097{
222ed1d6 4098 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
24b9f055
VZ
4099
4100 wxCHECK_RET( node, wxT("invalid column index in DeleteColumn()") );
bd8289c1 4101
5b077d48 4102 m_dirty = TRUE;
222ed1d6
MB
4103 delete node->GetData();
4104 m_columns.Erase( node );
ae409cb8 4105
df6f82f0
VZ
4106 if ( !IsVirtual() )
4107 {
4108 // update all the items
4109 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4110 {
4111 wxListLineData * const line = GetLine(i);
222ed1d6
MB
4112 wxListItemDataList::compatibility_iterator n = line->m_items.Item( col );
4113 delete n->GetData();
4114 line->m_items.Erase(n);
df6f82f0
VZ
4115 }
4116 }
4117
ae409cb8
VZ
4118 // invalidate it as it has to be recalculated
4119 m_headerWidth = 0;
e1e955e1 4120}
c801d85f 4121
5fe143df 4122void wxListMainWindow::DoDeleteAllItems()
c801d85f 4123{
cf1dfa6b
VZ
4124 if ( IsEmpty() )
4125 {
4126 // nothing to do - in particular, don't send the event
4127 return;
4128 }
4129
b54e41c5 4130 ResetCurrent();
7c0ea335
VZ
4131
4132 // to make the deletion of all items faster, we don't send the
cf1dfa6b
VZ
4133 // notifications for each item deletion in this case but only one event
4134 // for all of them: this is compatible with wxMSW and documented in
4135 // DeleteAllItems() description
bffa1c77 4136
12c1b46a
RR
4137 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
4138 event.SetEventObject( GetParent() );
4139 GetParent()->GetEventHandler()->ProcessEvent( event );
7c0ea335 4140
b54e41c5
VZ
4141 if ( IsVirtual() )
4142 {
4143 m_countVirt = 0;
4144
850ed6e7 4145 m_selStore.Clear();
b54e41c5
VZ
4146 }
4147
6b4a8d93
VZ
4148 if ( InReportView() )
4149 {
4150 ResetVisibleLinesRange();
4151 }
4152
5b077d48 4153 m_lines.Clear();
5fe143df 4154}
cf1dfa6b 4155
5fe143df
VZ
4156void wxListMainWindow::DeleteAllItems()
4157{
4158 DoDeleteAllItems();
4159
4160 RecalculatePositions();
e1e955e1 4161}
c801d85f 4162
12c1b46a 4163void wxListMainWindow::DeleteEverything()
c801d85f 4164{
222ed1d6 4165 WX_CLEAR_LIST(wxListHeaderDataList, m_columns);
904ccf52
VZ
4166
4167 DeleteAllItems();
e1e955e1 4168}
c801d85f 4169
cf1dfa6b
VZ
4170// ----------------------------------------------------------------------------
4171// scanning for an item
4172// ----------------------------------------------------------------------------
4173
debe6624 4174void wxListMainWindow::EnsureVisible( long index )
c801d85f 4175{
cf1dfa6b
VZ
4176 wxCHECK_RET( index >= 0 && (size_t)index < GetItemCount(),
4177 _T("invalid index in EnsureVisible") );
4178
4179 // We have to call this here because the label in question might just have
34bbbc27
VZ
4180 // been added and its position is not known yet
4181 if ( m_dirty )
4182 {
34bbbc27
VZ
4183 RecalculatePositions(TRUE /* no refresh */);
4184 }
4185
4186 MoveToItem((size_t)index);
e1e955e1 4187}
c801d85f 4188
debe6624 4189long wxListMainWindow::FindItem(long start, const wxString& str, bool WXUNUSED(partial) )
c801d85f 4190{
5b077d48
RR
4191 long pos = start;
4192 wxString tmp = str;
cf1dfa6b
VZ
4193 if (pos < 0)
4194 pos = 0;
54442116 4195
cf1dfa6b
VZ
4196 size_t count = GetItemCount();
4197 for ( size_t i = (size_t)pos; i < count; i++ )
4198 {
4199 wxListLineData *line = GetLine(i);
4200 if ( line->GetText(0) == tmp )
4201 return i;
5b077d48 4202 }
cf1dfa6b
VZ
4203
4204 return wxNOT_FOUND;
e1e955e1 4205}
c801d85f 4206
debe6624 4207long wxListMainWindow::FindItem(long start, long data)
c801d85f 4208{
5b077d48 4209 long pos = start;
cf1dfa6b
VZ
4210 if (pos < 0)
4211 pos = 0;
4212
4213 size_t count = GetItemCount();
4214 for (size_t i = (size_t)pos; i < count; i++)
5b077d48 4215 {
cf1dfa6b 4216 wxListLineData *line = GetLine(i);
5b077d48
RR
4217 wxListItem item;
4218 line->GetItem( 0, item );
cf1dfa6b
VZ
4219 if (item.m_data == data)
4220 return i;
5b077d48 4221 }
cf1dfa6b
VZ
4222
4223 return wxNOT_FOUND;
e1e955e1 4224}
c801d85f 4225
debe6624 4226long wxListMainWindow::HitTest( int x, int y, int &flags )
c801d85f 4227{
aaef15bf 4228 CalcUnscrolledPosition( x, y, &x, &y );
e8741cca 4229
fc4f1d5f
VZ
4230 size_t count = GetItemCount();
4231
5cd89174 4232 if ( HasFlag(wxLC_REPORT) )
c801d85f 4233 {
5cd89174 4234 size_t current = y / GetLineHeight();
fc4f1d5f
VZ
4235 if ( current < count )
4236 {
4237 flags = HitTestLine(current, x, y);
4238 if ( flags )
4239 return current;
4240 }
5cd89174
VZ
4241 }
4242 else // !report
4243 {
4244 // TODO: optimize it too! this is less simple than for report view but
4245 // enumerating all items is still not a way to do it!!
5cd89174 4246 for ( size_t current = 0; current < count; current++ )
5b077d48 4247 {
5cd89174
VZ
4248 flags = HitTestLine(current, x, y);
4249 if ( flags )
4250 return current;
5b077d48 4251 }
e1e955e1 4252 }
cf1dfa6b
VZ
4253
4254 return wxNOT_FOUND;
e1e955e1 4255}
c801d85f 4256
cf1dfa6b
VZ
4257// ----------------------------------------------------------------------------
4258// adding stuff
4259// ----------------------------------------------------------------------------
4260
c801d85f
KB
4261void wxListMainWindow::InsertItem( wxListItem &item )
4262{
cf1dfa6b
VZ
4263 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual control") );
4264
4265 size_t count = GetItemCount();
4266 wxCHECK_RET( item.m_itemId >= 0 && (size_t)item.m_itemId <= count,
4267 _T("invalid item index") );
4268
4269 size_t id = item.m_itemId;
4270
5b077d48 4271 m_dirty = TRUE;
cf1dfa6b 4272
5b077d48 4273 int mode = 0;
cf1dfa6b 4274 if ( HasFlag(wxLC_REPORT) )
2b5f62a0 4275 {
1370703e 4276 mode = wxLC_REPORT;
2b5f62a0
VZ
4277 ResetVisibleLinesRange();
4278 }
cf1dfa6b 4279 else if ( HasFlag(wxLC_LIST) )
1370703e 4280 mode = wxLC_LIST;
cf1dfa6b 4281 else if ( HasFlag(wxLC_ICON) )
1370703e 4282 mode = wxLC_ICON;
cf1dfa6b 4283 else if ( HasFlag(wxLC_SMALL_ICON) )
1370703e
VZ
4284 mode = wxLC_ICON; // no typo
4285 else
4286 {
4287 wxFAIL_MSG( _T("unknown mode") );
4288 }
004fd0c8 4289
5cd89174 4290 wxListLineData *line = new wxListLineData(this);
004fd0c8 4291
5b077d48 4292 line->SetItem( 0, item );
cf1dfa6b
VZ
4293
4294 m_lines.Insert( line, id );
5cd89174 4295
b84839ae 4296 m_dirty = TRUE;
a67e6e54
VZ
4297
4298 SendNotify(id, wxEVT_COMMAND_LIST_INSERT_ITEM);
4299
5cd89174 4300 RefreshLines(id, GetItemCount() - 1);
e1e955e1 4301}
c801d85f 4302
debe6624 4303void wxListMainWindow::InsertColumn( long col, wxListItem &item )
c801d85f 4304{
5b077d48 4305 m_dirty = TRUE;
cf1dfa6b 4306 if ( HasFlag(wxLC_REPORT) )
3db7be80 4307 {
54442116
VZ
4308 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
4309 item.m_width = GetTextLength( item.m_text );
df6f82f0 4310
5b077d48 4311 wxListHeaderData *column = new wxListHeaderData( item );
df6f82f0
VZ
4312 bool insert = (col >= 0) && ((size_t)col < m_columns.GetCount());
4313 if ( insert )
5b077d48 4314 {
222ed1d6 4315 wxListHeaderDataList::compatibility_iterator node = m_columns.Item( col );
24b9f055 4316 m_columns.Insert( node, column );
5b077d48
RR
4317 }
4318 else
4319 {
4320 m_columns.Append( column );
4321 }
ae409cb8 4322
df6f82f0
VZ
4323 if ( !IsVirtual() )
4324 {
4325 // update all the items
4326 for ( size_t i = 0; i < m_lines.GetCount(); i++ )
4327 {
4328 wxListLineData * const line = GetLine(i);
4329 wxListItemData * const data = new wxListItemData(this);
4330 if ( insert )
4331 line->m_items.Insert(col, data);
4332 else
4333 line->m_items.Append(data);
4334 }
4335 }
4336
ae409cb8
VZ
4337 // invalidate it as it has to be recalculated
4338 m_headerWidth = 0;
3db7be80 4339 }
e1e955e1 4340}
c801d85f 4341
cf1dfa6b
VZ
4342// ----------------------------------------------------------------------------
4343// sorting
4344// ----------------------------------------------------------------------------
4345
c801d85f
KB
4346wxListCtrlCompare list_ctrl_compare_func_2;
4347long list_ctrl_compare_data;
4348
f6bcfd97 4349int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
c801d85f 4350{
f6bcfd97
BP
4351 wxListLineData *line1 = *arg1;
4352 wxListLineData *line2 = *arg2;
5b077d48
RR
4353 wxListItem item;
4354 line1->GetItem( 0, item );
4355 long data1 = item.m_data;
4356 line2->GetItem( 0, item );
4357 long data2 = item.m_data;
4358 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
e1e955e1 4359}
c801d85f
KB
4360
4361void wxListMainWindow::SortItems( wxListCtrlCompare fn, long data )
4362{
5b077d48
RR
4363 list_ctrl_compare_func_2 = fn;
4364 list_ctrl_compare_data = data;
4365 m_lines.Sort( list_ctrl_compare_func_1 );
af7c1052 4366 m_dirty = TRUE;
e1e955e1 4367}
c801d85f 4368
cf1dfa6b
VZ
4369// ----------------------------------------------------------------------------
4370// scrolling
4371// ----------------------------------------------------------------------------
4372
7c74e7fe
SC
4373void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
4374{
b54e41c5
VZ
4375 // update our idea of which lines are shown when we redraw the window the
4376 // next time
4377 ResetVisibleLinesRange();
cf1dfa6b 4378
29149a64 4379 // FIXME
3a8c693a 4380#if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
29149a64
VZ
4381 wxScrolledWindow::OnScroll(event);
4382#else
1e6feb95 4383 HandleOnScroll( event );
29149a64 4384#endif
30954328 4385
cf1dfa6b 4386 if ( event.GetOrientation() == wxHORIZONTAL && HasHeader() )
7c74e7fe 4387 {
3cd94a0d 4388 wxGenericListCtrl* lc = GetListCtrl();
cf1dfa6b
VZ
4389 wxCHECK_RET( lc, _T("no listctrl window?") );
4390
afbe906a
RR
4391 lc->m_headerWin->Refresh();
4392 lc->m_headerWin->Update();
7c74e7fe 4393 }
cf1dfa6b
VZ
4394}
4395
5cd89174
VZ
4396int wxListMainWindow::GetCountPerPage() const
4397{
4398 if ( !m_linesPerPage )
4399 {
4400 wxConstCast(this, wxListMainWindow)->
4401 m_linesPerPage = GetClientSize().y / GetLineHeight();
4402 }
4403
4404 return m_linesPerPage;
4405}
4406
b54e41c5 4407void wxListMainWindow::GetVisibleLinesRange(size_t *from, size_t *to)
cf1dfa6b 4408{
b54e41c5 4409 wxASSERT_MSG( HasFlag(wxLC_REPORT), _T("this is for report mode only") );
cf1dfa6b 4410
b54e41c5
VZ
4411 if ( m_lineFrom == (size_t)-1 )
4412 {
b54e41c5 4413 size_t count = GetItemCount();
6522713c
VZ
4414 if ( count )
4415 {
4416 m_lineFrom = GetScrollPos(wxVERTICAL);
cf1dfa6b 4417
152c57f2
VZ
4418 // this may happen if SetScrollbars() hadn't been called yet
4419 if ( m_lineFrom >= count )
bcc0da5c 4420 m_lineFrom = count - 1;
cf1dfa6b 4421
6522713c
VZ
4422 // we redraw one extra line but this is needed to make the redrawing
4423 // logic work when there is a fractional number of lines on screen
4424 m_lineTo = m_lineFrom + m_linesPerPage;
4425 if ( m_lineTo >= count )
4426 m_lineTo = count - 1;
4427 }
4428 else // empty control
4429 {
4430 m_lineFrom = 0;
4431 m_lineTo = (size_t)-1;
4432 }
cf1dfa6b 4433 }
b54e41c5 4434
ae167d2f
VZ
4435 wxASSERT_MSG( IsEmpty() ||
4436 (m_lineFrom <= m_lineTo && m_lineTo < GetItemCount()),
6b4a8d93
VZ
4437 _T("GetVisibleLinesRange() returns incorrect result") );
4438
b54e41c5
VZ
4439 if ( from )
4440 *from = m_lineFrom;
4441 if ( to )
4442 *to = m_lineTo;
7c74e7fe
SC
4443}
4444
c801d85f 4445// -------------------------------------------------------------------------------------
3cd94a0d 4446// wxGenericListCtrl
c801d85f
KB
4447// -------------------------------------------------------------------------------------
4448
3cd94a0d
JS
4449IMPLEMENT_DYNAMIC_CLASS(wxGenericListCtrl, wxControl)
4450
3cd94a0d
JS
4451BEGIN_EVENT_TABLE(wxGenericListCtrl,wxControl)
4452 EVT_SIZE(wxGenericListCtrl::OnSize)
c801d85f
KB
4453END_EVENT_TABLE()
4454
3cd94a0d 4455wxGenericListCtrl::wxGenericListCtrl()
c801d85f 4456{
80020b62
JS
4457 m_imageListNormal = (wxImageListType *) NULL;
4458 m_imageListSmall = (wxImageListType *) NULL;
4459 m_imageListState = (wxImageListType *) NULL;
cf1dfa6b
VZ
4460
4461 m_ownsImageListNormal =
4462 m_ownsImageListSmall =
4463 m_ownsImageListState = FALSE;
4464
5b077d48
RR
4465 m_mainWin = (wxListMainWindow*) NULL;
4466 m_headerWin = (wxListHeaderWindow*) NULL;
f8252483 4467 m_headerHeight = 0;
c801d85f
KB
4468}
4469
3cd94a0d 4470wxGenericListCtrl::~wxGenericListCtrl()
c801d85f 4471{
cf1dfa6b
VZ
4472 if (m_ownsImageListNormal)
4473 delete m_imageListNormal;
4474 if (m_ownsImageListSmall)
4475 delete m_imageListSmall;
4476 if (m_ownsImageListState)
4477 delete m_imageListState;
4478}
4479
f8252483
JS
4480void wxGenericListCtrl::CalculateAndSetHeaderHeight()
4481{
4482 // we use the letter "H" for calculating the needed space, basing on the current font
4483 int w, h;
4484 m_headerWin->GetTextExtent(wxT("H"), &w, &h);
4485 m_headerHeight = h + 2 * HEADER_OFFSET_Y + EXTRA_HEIGHT;
4486 m_headerWin->SetSize(m_headerWin->GetSize().x, m_headerHeight);
4487}
4488
3cd94a0d 4489void wxGenericListCtrl::CreateHeaderWindow()
cf1dfa6b
VZ
4490{
4491 m_headerWin = new wxListHeaderWindow
4492 (
4493 this, -1, m_mainWin,
4494 wxPoint(0, 0),
f8252483 4495 wxSize(GetClientSize().x, m_headerHeight),
cf1dfa6b
VZ
4496 wxTAB_TRAVERSAL
4497 );
f8252483 4498 CalculateAndSetHeaderHeight();
c801d85f
KB
4499}
4500
3cd94a0d 4501bool wxGenericListCtrl::Create(wxWindow *parent,
25e3a937
VZ
4502 wxWindowID id,
4503 const wxPoint &pos,
4504 const wxSize &size,
4505 long style,
25e3a937 4506 const wxValidator &validator,
25e3a937 4507 const wxString &name)
c801d85f 4508{
cf1dfa6b
VZ
4509 m_imageListNormal =
4510 m_imageListSmall =
80020b62 4511 m_imageListState = (wxImageListType *) NULL;
cf1dfa6b
VZ
4512 m_ownsImageListNormal =
4513 m_ownsImageListSmall =
4514 m_ownsImageListState = FALSE;
4515
5b077d48
RR
4516 m_mainWin = (wxListMainWindow*) NULL;
4517 m_headerWin = (wxListHeaderWindow*) NULL;
bd8289c1 4518
b54e41c5 4519 if ( !(style & wxLC_MASK_TYPE) )
5b077d48 4520 {
25e3a937 4521 style = style | wxLC_LIST;
5b077d48 4522 }
f6bcfd97 4523
07215d86 4524 if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) )
cf1dfa6b 4525 return FALSE;
f6bcfd97 4526
b54e41c5 4527 // don't create the inner window with the border
6d460f76 4528 style &= ~wxBORDER_MASK;
bd8289c1 4529
25e3a937 4530 m_mainWin = new wxListMainWindow( this, -1, wxPoint(0,0), size, style );
bd8289c1 4531
b54e41c5 4532 if ( HasFlag(wxLC_REPORT) )
ea451729 4533 {
cf1dfa6b
VZ
4534 CreateHeaderWindow();
4535
b54e41c5 4536 if ( HasFlag(wxLC_NO_HEADER) )
cf1dfa6b
VZ
4537 {
4538 // VZ: why do we create it at all then?
ea451729 4539 m_headerWin->Show( FALSE );
cf1dfa6b 4540 }
ea451729 4541 }
bd8289c1 4542
cf1dfa6b 4543 return TRUE;
e1e955e1 4544}
c801d85f 4545
3cd94a0d 4546void wxGenericListCtrl::SetSingleStyle( long style, bool add )
c801d85f 4547{
b54e41c5
VZ
4548 wxASSERT_MSG( !(style & wxLC_VIRTUAL),
4549 _T("wxLC_VIRTUAL can't be [un]set") );
4550
f03fc89f 4551 long flag = GetWindowStyle();
bd8289c1 4552
5b077d48
RR
4553 if (add)
4554 {
cf1dfa6b 4555 if (style & wxLC_MASK_TYPE)
b54e41c5 4556 flag &= ~(wxLC_MASK_TYPE | wxLC_VIRTUAL);
cf1dfa6b
VZ
4557 if (style & wxLC_MASK_ALIGN)
4558 flag &= ~wxLC_MASK_ALIGN;
4559 if (style & wxLC_MASK_SORT)
4560 flag &= ~wxLC_MASK_SORT;
5b077d48 4561 }
c801d85f 4562
5b077d48
RR
4563 if (add)
4564 {
4565 flag |= style;
4566 }
4567 else
4568 {
cf1dfa6b 4569 flag &= ~style;
5b077d48 4570 }
bd8289c1 4571
5b077d48 4572 SetWindowStyleFlag( flag );
e1e955e1 4573}
c801d85f 4574
3cd94a0d 4575void wxGenericListCtrl::SetWindowStyleFlag( long flag )
c801d85f 4576{
121a3581
RR
4577 if (m_mainWin)
4578 {
4579 m_mainWin->DeleteEverything();
c801d85f 4580
a95cdab8
VZ
4581 // has the header visibility changed?
4582 bool hasHeader = HasFlag(wxLC_REPORT) && !HasFlag(wxLC_NO_HEADER),
4583 willHaveHeader = (flag & wxLC_REPORT) && !(flag & wxLC_NO_HEADER);
c801d85f 4584
a95cdab8 4585 if ( hasHeader != willHaveHeader )
5b077d48 4586 {
a95cdab8
VZ
4587 // toggle it
4588 if ( hasHeader )
4589 {
4590 if ( m_headerWin )
4591 {
4592 // don't delete, just hide, as we can reuse it later
4593 m_headerWin->Show(FALSE);
4594 }
4595 //else: nothing to do
4596 }
4597 else // must show header
5b077d48 4598 {
121a3581
RR
4599 if (!m_headerWin)
4600 {
cf1dfa6b 4601 CreateHeaderWindow();
121a3581 4602 }
a95cdab8 4603 else // already have it, just show
004fd0c8 4604 {
a95cdab8 4605 m_headerWin->Show( TRUE );
004fd0c8 4606 }
5b077d48 4607 }
a95cdab8
VZ
4608
4609 ResizeReportView(willHaveHeader);
bffa1c77 4610 }
e1e955e1 4611 }
004fd0c8 4612
5b077d48 4613 wxWindow::SetWindowStyleFlag( flag );
e1e955e1 4614}
c801d85f 4615
3cd94a0d 4616bool wxGenericListCtrl::GetColumn(int col, wxListItem &item) const
c801d85f 4617{
5b077d48
RR
4618 m_mainWin->GetColumn( col, item );
4619 return TRUE;
e1e955e1 4620}
c801d85f 4621
3cd94a0d 4622bool wxGenericListCtrl::SetColumn( int col, wxListItem& item )
c801d85f 4623{
5b077d48
RR
4624 m_mainWin->SetColumn( col, item );
4625 return TRUE;
e1e955e1 4626}
c801d85f 4627
3cd94a0d 4628int wxGenericListCtrl::GetColumnWidth( int col ) const
c801d85f 4629{
5b077d48 4630 return m_mainWin->GetColumnWidth( col );
e1e955e1 4631}
c801d85f 4632
3cd94a0d 4633bool wxGenericListCtrl::SetColumnWidth( int col, int width )
c801d85f 4634{
5b077d48
RR
4635 m_mainWin->SetColumnWidth( col, width );
4636 return TRUE;
e1e955e1 4637}
c801d85f 4638
3cd94a0d 4639int wxGenericListCtrl::GetCountPerPage() const
c801d85f
KB
4640{
4641 return m_mainWin->GetCountPerPage(); // different from Windows ?
e1e955e1 4642}
c801d85f 4643
3cd94a0d 4644bool wxGenericListCtrl::GetItem( wxListItem &info ) const
c801d85f 4645{
5b077d48
RR
4646 m_mainWin->GetItem( info );
4647 return TRUE;
e1e955e1 4648}
c801d85f 4649
3cd94a0d 4650bool wxGenericListCtrl::SetItem( wxListItem &info )
c801d85f 4651{
5b077d48
RR
4652 m_mainWin->SetItem( info );
4653 return TRUE;
e1e955e1 4654}
c801d85f 4655
3cd94a0d 4656long wxGenericListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
c801d85f 4657{
5b077d48
RR
4658 wxListItem info;
4659 info.m_text = label;
4660 info.m_mask = wxLIST_MASK_TEXT;
4661 info.m_itemId = index;
4662 info.m_col = col;
4663 if ( imageId > -1 )
4664 {
4665 info.m_image = imageId;
4666 info.m_mask |= wxLIST_MASK_IMAGE;
4667 };
4668 m_mainWin->SetItem(info);
4669 return TRUE;
e1e955e1 4670}
c801d85f 4671
3cd94a0d 4672int wxGenericListCtrl::GetItemState( long item, long stateMask ) const
c801d85f 4673{
5b077d48 4674 return m_mainWin->GetItemState( item, stateMask );
e1e955e1 4675}
c801d85f 4676
3cd94a0d 4677bool wxGenericListCtrl::SetItemState( long item, long state, long stateMask )
c801d85f 4678{
5b077d48
RR
4679 m_mainWin->SetItemState( item, state, stateMask );
4680 return TRUE;
e1e955e1 4681}
c801d85f 4682
3cd94a0d 4683bool wxGenericListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
c801d85f 4684{
5b077d48
RR
4685 wxListItem info;
4686 info.m_image = image;
4687 info.m_mask = wxLIST_MASK_IMAGE;
4688 info.m_itemId = item;
4689 m_mainWin->SetItem( info );
4690 return TRUE;
e1e955e1 4691}
c801d85f 4692
3cd94a0d 4693wxString wxGenericListCtrl::GetItemText( long item ) const
c801d85f 4694{
62d89eb4 4695 return m_mainWin->GetItemText(item);
e1e955e1 4696}
c801d85f 4697
3cd94a0d 4698void wxGenericListCtrl::SetItemText( long item, const wxString& str )
c801d85f 4699{
62d89eb4 4700 m_mainWin->SetItemText(item, str);
e1e955e1 4701}
c801d85f 4702
3cd94a0d 4703long wxGenericListCtrl::GetItemData( long item ) const
c801d85f 4704{
5b077d48
RR
4705 wxListItem info;
4706 info.m_itemId = item;
4707 m_mainWin->GetItem( info );
4708 return info.m_data;
e1e955e1 4709}
c801d85f 4710
3cd94a0d 4711bool wxGenericListCtrl::SetItemData( long item, long data )
c801d85f 4712{
5b077d48
RR
4713 wxListItem info;
4714 info.m_mask = wxLIST_MASK_DATA;
4715 info.m_itemId = item;
4716 info.m_data = data;
4717 m_mainWin->SetItem( info );
4718 return TRUE;
e1e955e1 4719}
c801d85f 4720
11ebea16
VZ
4721wxRect wxGenericListCtrl::GetViewRect() const
4722{
4723 return m_mainWin->GetViewRect();
4724}
4725
3cd94a0d 4726bool wxGenericListCtrl::GetItemRect( long item, wxRect &rect, int WXUNUSED(code) ) const
c801d85f 4727{
5b077d48 4728 m_mainWin->GetItemRect( item, rect );
7e6874c0 4729 if ( m_mainWin->HasHeader() )
f8252483 4730 rect.y += m_headerHeight + 1;
5b077d48 4731 return TRUE;
e1e955e1 4732}
c801d85f 4733
3cd94a0d 4734bool wxGenericListCtrl::GetItemPosition( long item, wxPoint& pos ) const
c801d85f 4735{
5b077d48
RR
4736 m_mainWin->GetItemPosition( item, pos );
4737 return TRUE;
e1e955e1 4738}
c801d85f 4739
3cd94a0d 4740bool wxGenericListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
c801d85f 4741{
5b077d48 4742 return 0;
e1e955e1 4743}
c801d85f 4744
3cd94a0d 4745int wxGenericListCtrl::GetItemCount() const
c801d85f 4746{
5b077d48 4747 return m_mainWin->GetItemCount();
e1e955e1 4748}
c801d85f 4749
3cd94a0d 4750int wxGenericListCtrl::GetColumnCount() const
92976ab6 4751{
5b077d48 4752 return m_mainWin->GetColumnCount();
92976ab6
RR
4753}
4754
3cd94a0d 4755void wxGenericListCtrl::SetItemSpacing( int spacing, bool isSmall )
33d0b396 4756{
5b077d48 4757 m_mainWin->SetItemSpacing( spacing, isSmall );
e1e955e1 4758}
33d0b396 4759
3cd94a0d 4760int wxGenericListCtrl::GetItemSpacing( bool isSmall ) const
c801d85f 4761{
5b077d48 4762 return m_mainWin->GetItemSpacing( isSmall );
e1e955e1 4763}
c801d85f 4764
3cd94a0d 4765void wxGenericListCtrl::SetItemTextColour( long item, const wxColour &col )
5b98eb2f
RL
4766{
4767 wxListItem info;
4768 info.m_itemId = item;
4769 info.SetTextColour( col );
4770 m_mainWin->SetItem( info );
4771}
4772
3cd94a0d 4773wxColour wxGenericListCtrl::GetItemTextColour( long item ) const
5b98eb2f
RL
4774{
4775 wxListItem info;
4776 info.m_itemId = item;
4777 m_mainWin->GetItem( info );
4778 return info.GetTextColour();
4779}
4780
3cd94a0d 4781void wxGenericListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
5b98eb2f
RL
4782{
4783 wxListItem info;
4784 info.m_itemId = item;
4785 info.SetBackgroundColour( col );
4786 m_mainWin->SetItem( info );
4787}
4788
3cd94a0d 4789wxColour wxGenericListCtrl::GetItemBackgroundColour( long item ) const
5b98eb2f
RL
4790{
4791 wxListItem info;
4792 info.m_itemId = item;
4793 m_mainWin->GetItem( info );
4794 return info.GetBackgroundColour();
4795}
4796
3cd94a0d 4797int wxGenericListCtrl::GetSelectedItemCount() const
c801d85f 4798{
5b077d48 4799 return m_mainWin->GetSelectedItemCount();
e1e955e1 4800}
c801d85f 4801
3cd94a0d 4802wxColour wxGenericListCtrl::GetTextColour() const
c801d85f 4803{
0530737d 4804 return GetForegroundColour();
e1e955e1 4805}
c801d85f 4806
3cd94a0d 4807void wxGenericListCtrl::SetTextColour(const wxColour& col)
c801d85f 4808{
0530737d 4809 SetForegroundColour(col);
e1e955e1 4810}
c801d85f 4811
3cd94a0d 4812long wxGenericListCtrl::GetTopItem() const
c801d85f 4813{
2b5f62a0
VZ
4814 size_t top;
4815 m_mainWin->GetVisibleLinesRange(&top, NULL);
4816 return (long)top;
e1e955e1 4817}
c801d85f 4818
3cd94a0d 4819long wxGenericListCtrl::GetNextItem( long item, int geom, int state ) const
c801d85f 4820{
5b077d48 4821 return m_mainWin->GetNextItem( item, geom, state );
e1e955e1 4822}
c801d85f 4823
80020b62 4824wxImageListType *wxGenericListCtrl::GetImageList(int which) const
c801d85f 4825{
5b077d48
RR
4826 if (which == wxIMAGE_LIST_NORMAL)
4827 {
4828 return m_imageListNormal;
4829 }
4830 else if (which == wxIMAGE_LIST_SMALL)
4831 {
4832 return m_imageListSmall;
4833 }
4834 else if (which == wxIMAGE_LIST_STATE)
4835 {
4836 return m_imageListState;
4837 }
80020b62 4838 return (wxImageListType *) NULL;
e1e955e1 4839}
c801d85f 4840
80020b62 4841void wxGenericListCtrl::SetImageList( wxImageListType *imageList, int which )
c801d85f 4842{
2e12c11a
VS
4843 if ( which == wxIMAGE_LIST_NORMAL )
4844 {
4845 if (m_ownsImageListNormal) delete m_imageListNormal;
4846 m_imageListNormal = imageList;
4847 m_ownsImageListNormal = FALSE;
4848 }
4849 else if ( which == wxIMAGE_LIST_SMALL )
4850 {
4851 if (m_ownsImageListSmall) delete m_imageListSmall;
4852 m_imageListSmall = imageList;
4853 m_ownsImageListSmall = FALSE;
4854 }
4855 else if ( which == wxIMAGE_LIST_STATE )
4856 {
4857 if (m_ownsImageListState) delete m_imageListState;
4858 m_imageListState = imageList;
4859 m_ownsImageListState = FALSE;
4860 }
4861
5b077d48 4862 m_mainWin->SetImageList( imageList, which );
e1e955e1 4863}
c801d85f 4864
80020b62 4865void wxGenericListCtrl::AssignImageList(wxImageListType *imageList, int which)
2e12c11a
VS
4866{
4867 SetImageList(imageList, which);
4868 if ( which == wxIMAGE_LIST_NORMAL )
4869 m_ownsImageListNormal = TRUE;
4870 else if ( which == wxIMAGE_LIST_SMALL )
4871 m_ownsImageListSmall = TRUE;
4872 else if ( which == wxIMAGE_LIST_STATE )
4873 m_ownsImageListState = TRUE;
4874}
4875
3cd94a0d 4876bool wxGenericListCtrl::Arrange( int WXUNUSED(flag) )
c801d85f 4877{
5b077d48 4878 return 0;
e1e955e1 4879}
c801d85f 4880
3cd94a0d 4881bool wxGenericListCtrl::DeleteItem( long item )
c801d85f 4882{
5b077d48
RR
4883 m_mainWin->DeleteItem( item );
4884 return TRUE;
e1e955e1 4885}
c801d85f 4886
3cd94a0d 4887bool wxGenericListCtrl::DeleteAllItems()
c801d85f 4888{
5b077d48
RR
4889 m_mainWin->DeleteAllItems();
4890 return TRUE;
e1e955e1 4891}
c801d85f 4892
3cd94a0d 4893bool wxGenericListCtrl::DeleteAllColumns()
bd8289c1 4894{
24b9f055
VZ
4895 size_t count = m_mainWin->m_columns.GetCount();
4896 for ( size_t n = 0; n < count; n++ )
85cdcb7a 4897 DeleteColumn(0);
bffa1c77 4898
5b077d48 4899 return TRUE;
4f22cf8d
RR
4900}
4901
3cd94a0d 4902void wxGenericListCtrl::ClearAll()
4f22cf8d 4903{
5b077d48 4904 m_mainWin->DeleteEverything();
bd8289c1
VZ
4905}
4906
3cd94a0d 4907bool wxGenericListCtrl::DeleteColumn( int col )
c801d85f 4908{
5b077d48 4909 m_mainWin->DeleteColumn( col );
40c08808
VZ
4910
4911 // if we don't have the header any longer, we need to relayout the window
4912 if ( !GetColumnCount() )
4913 {
4914 ResizeReportView(FALSE /* no header */);
4915 }
4916
5b077d48 4917 return TRUE;
e1e955e1 4918}
c801d85f 4919
3cd94a0d 4920void wxGenericListCtrl::Edit( long item )
c801d85f 4921{
cf1dfa6b 4922 m_mainWin->EditLabel( item );
e1e955e1 4923}
c801d85f 4924
3cd94a0d 4925bool wxGenericListCtrl::EnsureVisible( long item )
c801d85f 4926{
5b077d48
RR
4927 m_mainWin->EnsureVisible( item );
4928 return TRUE;
e1e955e1 4929}
c801d85f 4930
3cd94a0d 4931long wxGenericListCtrl::FindItem( long start, const wxString& str, bool partial )
c801d85f 4932{
5b077d48 4933 return m_mainWin->FindItem( start, str, partial );
e1e955e1 4934}
c801d85f 4935
3cd94a0d 4936long wxGenericListCtrl::FindItem( long start, long data )
c801d85f 4937{
5b077d48 4938 return m_mainWin->FindItem( start, data );
e1e955e1 4939}
c801d85f 4940
3cd94a0d 4941long wxGenericListCtrl::FindItem( long WXUNUSED(start), const wxPoint& WXUNUSED(pt),
debe6624 4942 int WXUNUSED(direction))
c801d85f 4943{
5b077d48 4944 return 0;
e1e955e1 4945}
c801d85f 4946
3cd94a0d 4947long wxGenericListCtrl::HitTest( const wxPoint &point, int &flags )
c801d85f 4948{
5b077d48 4949 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
e1e955e1 4950}
c801d85f 4951
3cd94a0d 4952long wxGenericListCtrl::InsertItem( wxListItem& info )
c801d85f 4953{
5b077d48 4954 m_mainWin->InsertItem( info );
2ebcd5f5 4955 return info.m_itemId;
e1e955e1 4956}
c801d85f 4957
3cd94a0d 4958long wxGenericListCtrl::InsertItem( long index, const wxString &label )
c801d85f 4959{
51cc4dad
RR
4960 wxListItem info;
4961 info.m_text = label;
4962 info.m_mask = wxLIST_MASK_TEXT;
4963 info.m_itemId = index;
4964 return InsertItem( info );
e1e955e1 4965}
c801d85f 4966
3cd94a0d 4967long wxGenericListCtrl::InsertItem( long index, int imageIndex )
c801d85f 4968{
51cc4dad
RR
4969 wxListItem info;
4970 info.m_mask = wxLIST_MASK_IMAGE;
4971 info.m_image = imageIndex;
4972 info.m_itemId = index;
4973 return InsertItem( info );
e1e955e1 4974}
c801d85f 4975
3cd94a0d 4976long wxGenericListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
c801d85f 4977{
51cc4dad
RR
4978 wxListItem info;
4979 info.m_text = label;
4980 info.m_image = imageIndex;
4981 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
4982 info.m_itemId = index;
4983 return InsertItem( info );
e1e955e1 4984}
c801d85f 4985
3cd94a0d 4986long wxGenericListCtrl::InsertColumn( long col, wxListItem &item )
c801d85f 4987{
40c08808
VZ
4988 wxCHECK_MSG( m_headerWin, -1, _T("can't add column in non report mode") );
4989
51cc4dad 4990 m_mainWin->InsertColumn( col, item );
40c08808
VZ
4991
4992 // if we hadn't had header before and have it now we need to relayout the
4993 // window
ac53090c 4994 if ( GetColumnCount() == 1 && m_mainWin->HasHeader() )
40c08808
VZ
4995 {
4996 ResizeReportView(TRUE /* have header */);
4997 }
4998
d3e90957 4999 m_headerWin->Refresh();
25e3a937 5000
51cc4dad 5001 return 0;
e1e955e1 5002}
c801d85f 5003
3cd94a0d 5004long wxGenericListCtrl::InsertColumn( long col, const wxString &heading,
debe6624 5005 int format, int width )
c801d85f 5006{
51cc4dad
RR
5007 wxListItem item;
5008 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
5009 item.m_text = heading;
5010 if (width >= -2)
5011 {
5012 item.m_mask |= wxLIST_MASK_WIDTH;
5013 item.m_width = width;
5014 }
5015 item.m_format = format;
c801d85f 5016
51cc4dad 5017 return InsertColumn( col, item );
e1e955e1 5018}
c801d85f 5019
3cd94a0d 5020bool wxGenericListCtrl::ScrollList( int WXUNUSED(dx), int WXUNUSED(dy) )
c801d85f 5021{
51cc4dad 5022 return 0;
e1e955e1 5023}
c801d85f
KB
5024
5025// Sort items.
5026// fn is a function which takes 3 long arguments: item1, item2, data.
5027// item1 is the long data associated with a first item (NOT the index).
5028// item2 is the long data associated with a second item (NOT the index).
5029// data is the same value as passed to SortItems.
5030// The return value is a negative number if the first item should precede the second
5031// item, a positive number of the second item should precede the first,
5032// or zero if the two items are equivalent.
5033// data is arbitrary data to be passed to the sort function.
5034
3cd94a0d 5035bool wxGenericListCtrl::SortItems( wxListCtrlCompare fn, long data )
c801d85f 5036{
51cc4dad
RR
5037 m_mainWin->SortItems( fn, data );
5038 return TRUE;
e1e955e1 5039}
c801d85f 5040
cf1dfa6b 5041// ----------------------------------------------------------------------------
b54e41c5 5042// event handlers
cf1dfa6b
VZ
5043// ----------------------------------------------------------------------------
5044
3cd94a0d 5045void wxGenericListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
53010e52 5046{
b54e41c5 5047 if ( !m_mainWin )
cf1dfa6b 5048 return;
53010e52 5049
a95cdab8
VZ
5050 ResizeReportView(m_mainWin->HasHeader());
5051
5052 m_mainWin->RecalculatePositions();
5053}
5054
3cd94a0d 5055void wxGenericListCtrl::ResizeReportView(bool showHeader)
a95cdab8 5056{
b54e41c5 5057 int cw, ch;
51cc4dad 5058 GetClientSize( &cw, &ch );
bd8289c1 5059
a95cdab8 5060 if ( showHeader )
51cc4dad 5061 {
f8252483
JS
5062 m_headerWin->SetSize( 0, 0, cw, m_headerHeight );
5063 m_mainWin->SetSize( 0, m_headerHeight + 1, cw, ch - m_headerHeight - 1 );
51cc4dad 5064 }
cf1dfa6b 5065 else // no header window
51cc4dad 5066 {
cf1dfa6b 5067 m_mainWin->SetSize( 0, 0, cw, ch );
51cc4dad 5068 }
b54e41c5 5069}
cf1dfa6b 5070
5180055b 5071void wxGenericListCtrl::OnInternalIdle()
b54e41c5 5072{
5180055b
JS
5073 wxWindow::OnInternalIdle();
5074
b54e41c5
VZ
5075 // do it only if needed
5076 if ( !m_mainWin->m_dirty )
5077 return;
5078
5079 m_mainWin->RecalculatePositions();
e1e955e1 5080}
53010e52 5081
cf1dfa6b
VZ
5082// ----------------------------------------------------------------------------
5083// font/colours
5084// ----------------------------------------------------------------------------
5085
3cd94a0d 5086bool wxGenericListCtrl::SetBackgroundColour( const wxColour &colour )
bd8289c1 5087{
51cc4dad
RR
5088 if (m_mainWin)
5089 {
5090 m_mainWin->SetBackgroundColour( colour );
5091 m_mainWin->m_dirty = TRUE;
5092 }
004fd0c8 5093
f03fc89f 5094 return TRUE;
e4d06860
RR
5095}
5096
3cd94a0d 5097bool wxGenericListCtrl::SetForegroundColour( const wxColour &colour )
bd8289c1 5098{
f03fc89f
VZ
5099 if ( !wxWindow::SetForegroundColour( colour ) )
5100 return FALSE;
004fd0c8 5101
51cc4dad
RR
5102 if (m_mainWin)
5103 {
5104 m_mainWin->SetForegroundColour( colour );
5105 m_mainWin->m_dirty = TRUE;
5106 }
004fd0c8 5107
51cc4dad
RR
5108 if (m_headerWin)
5109 {
5110 m_headerWin->SetForegroundColour( colour );
5111 }
f03fc89f
VZ
5112
5113 return TRUE;
e4d06860 5114}
bd8289c1 5115
3cd94a0d 5116bool wxGenericListCtrl::SetFont( const wxFont &font )
bd8289c1 5117{
f03fc89f
VZ
5118 if ( !wxWindow::SetFont( font ) )
5119 return FALSE;
004fd0c8 5120
51cc4dad
RR
5121 if (m_mainWin)
5122 {
5123 m_mainWin->SetFont( font );
5124 m_mainWin->m_dirty = TRUE;
5125 }
004fd0c8 5126
51cc4dad
RR
5127 if (m_headerWin)
5128 {
5129 m_headerWin->SetFont( font );
f8252483 5130 CalculateAndSetHeaderHeight();
51cc4dad 5131 }
f03fc89f 5132
f8252483
JS
5133 Refresh();
5134
f03fc89f 5135 return TRUE;
e4d06860 5136}
c801d85f 5137
cf1dfa6b
VZ
5138// ----------------------------------------------------------------------------
5139// methods forwarded to m_mainWin
5140// ----------------------------------------------------------------------------
5141
efbb7287
VZ
5142#if wxUSE_DRAG_AND_DROP
5143
3cd94a0d 5144void wxGenericListCtrl::SetDropTarget( wxDropTarget *dropTarget )
efbb7287
VZ
5145{
5146 m_mainWin->SetDropTarget( dropTarget );
5147}
5148
3cd94a0d 5149wxDropTarget *wxGenericListCtrl::GetDropTarget() const
efbb7287
VZ
5150{
5151 return m_mainWin->GetDropTarget();
5152}
5153
5154#endif // wxUSE_DRAG_AND_DROP
5155
3cd94a0d 5156bool wxGenericListCtrl::SetCursor( const wxCursor &cursor )
efbb7287
VZ
5157{
5158 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : FALSE;
5159}
5160
3cd94a0d 5161wxColour wxGenericListCtrl::GetBackgroundColour() const
efbb7287
VZ
5162{
5163 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
5164}
5165
3cd94a0d 5166wxColour wxGenericListCtrl::GetForegroundColour() const
efbb7287
VZ
5167{
5168 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
5169}
5170
3cd94a0d 5171bool wxGenericListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
efbb7287 5172{
3a8c693a 5173#if wxUSE_MENUS
efbb7287 5174 return m_mainWin->PopupMenu( menu, x, y );
3a8c693a
VZ
5175#else
5176 return FALSE;
5177#endif // wxUSE_MENUS
efbb7287
VZ
5178}
5179
3cd94a0d 5180void wxGenericListCtrl::SetFocus()
efbb7287
VZ
5181{
5182 /* The test in window.cpp fails as we are a composite
5183 window, so it checks against "this", but not m_mainWin. */
5184 if ( FindFocus() != this )
5185 m_mainWin->SetFocus();
5186}
1e6feb95 5187
2c1f73ee
VZ
5188// ----------------------------------------------------------------------------
5189// virtual list control support
5190// ----------------------------------------------------------------------------
5191
3cd94a0d 5192wxString wxGenericListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
2c1f73ee
VZ
5193{
5194 // this is a pure virtual function, in fact - which is not really pure
5195 // because the controls which are not virtual don't need to implement it
3cd94a0d 5196 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemText not supposed to be called") );
2c1f73ee
VZ
5197
5198 return wxEmptyString;
5199}
5200
3cd94a0d 5201int wxGenericListCtrl::OnGetItemImage(long WXUNUSED(item)) const
2c1f73ee
VZ
5202{
5203 // same as above
3cd94a0d 5204 wxFAIL_MSG( _T("wxGenericListCtrl::OnGetItemImage not supposed to be called") );
2c1f73ee
VZ
5205
5206 return -1;
5207}
5208
3cd94a0d 5209wxListItemAttr *wxGenericListCtrl::OnGetItemAttr(long item) const
6c02c329
VZ
5210{
5211 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
5212 _T("invalid item index in OnGetItemAttr()") );
5213
5214 // no attributes by default
5215 return NULL;
5216}
5217
3cd94a0d 5218void wxGenericListCtrl::SetItemCount(long count)
2c1f73ee
VZ
5219{
5220 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
5221
5222 m_mainWin->SetItemCount(count);
5223}
5224
3cd94a0d 5225void wxGenericListCtrl::RefreshItem(long item)
1a6cb56f
VZ
5226{
5227 m_mainWin->RefreshLine(item);
5228}
5229
3cd94a0d 5230void wxGenericListCtrl::RefreshItems(long itemFrom, long itemTo)
1a6cb56f
VZ
5231{
5232 m_mainWin->RefreshLines(itemFrom, itemTo);
5233}
5234
2f1e3c46
VZ
5235/*
5236 * Generic wxListCtrl is more or less a container for two other
5237 * windows which drawings are done upon. These are namely
5238 * 'm_headerWin' and 'm_mainWin'.
5239 * Here we override 'virtual wxWindow::Refresh()' to mimic the
5240 * behaviour wxListCtrl has under wxMSW.
5241 */
5242void wxGenericListCtrl::Refresh(bool eraseBackground, const wxRect *rect)
5243{
5244 if (!rect)
5245 {
5246 // The easy case, no rectangle specified.
5247 if (m_headerWin)
5248 m_headerWin->Refresh(eraseBackground);
5249
5250 if (m_mainWin)
5251 m_mainWin->Refresh(eraseBackground);
5252 }
5253 else
5254 {
5255 // Refresh the header window
5256 if (m_headerWin)
5257 {
5258 wxRect rectHeader = m_headerWin->GetRect();
5259 rectHeader.Intersect(*rect);
5260 if (rectHeader.GetWidth() && rectHeader.GetHeight())
5261 {
5262 int x, y;
5263 m_headerWin->GetPosition(&x, &y);
5264 rectHeader.Offset(-x, -y);
5265 m_headerWin->Refresh(eraseBackground, &rectHeader);
5266 }
5267
5268 }
5269
5270 // Refresh the main window
5271 if (m_mainWin)
5272 {
5273 wxRect rectMain = m_mainWin->GetRect();
5274 rectMain.Intersect(*rect);
5275 if (rectMain.GetWidth() && rectMain.GetHeight())
5276 {
5277 int x, y;
5278 m_mainWin->GetPosition(&x, &y);
5279 rectMain.Offset(-x, -y);
5280 m_mainWin->Refresh(eraseBackground, &rectMain);
5281 }
5282 }
5283 }
5284}
5285
3cd94a0d 5286void wxGenericListCtrl::Freeze()
c5c528fc
VZ
5287{
5288 m_mainWin->Freeze();
5289}
5290
3cd94a0d 5291void wxGenericListCtrl::Thaw()
c5c528fc
VZ
5292{
5293 m_mainWin->Thaw();
5294}
5295
1e6feb95 5296#endif // wxUSE_LISTCTRL
5b98eb2f 5297