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