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