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