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