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