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