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