]> git.saurik.com Git - wxWidgets.git/blob - src/generic/listctrl.cpp
2e26022e284d679f312006238b24a79d89a3d83c
[wxWidgets.git] / src / generic / listctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: generic/listctrl.cpp
3 // Purpose: generic implementation of wxListCtrl
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "listctrl.h"
12 #pragma implementation "listctrlbase.h"
13 #endif
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #if wxUSE_LISTCTRL
23
24 #include "wx/dcscreen.h"
25 #include "wx/app.h"
26 #include "wx/listctrl.h"
27 #include "wx/generic/imaglist.h"
28 #include "wx/dynarray.h"
29
30 #ifdef __WXGTK__
31 #include <gtk/gtk.h>
32 #include "wx/gtk/win_gtk.h"
33 #endif
34
35 #ifndef wxUSE_GENERIC_LIST_EXTENSIONS
36 #define wxUSE_GENERIC_LIST_EXTENSIONS 1
37 #endif
38
39 // ----------------------------------------------------------------------------
40 // events
41 // ----------------------------------------------------------------------------
42
43 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_DRAG)
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_RDRAG)
45 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_END_LABEL_EDIT)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ITEM)
48 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_GET_INFO)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_SET_INFO)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_SELECTED)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_DESELECTED)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_KEY_DOWN)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_INSERT_ITEM)
55 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_COL_CLICK)
56 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK)
57 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK)
58 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LIST_ITEM_ACTIVATED)
59
60 // ============================================================================
61 // private classes
62 // ============================================================================
63
64 //-----------------------------------------------------------------------------
65 // wxListItemData (internal)
66 //-----------------------------------------------------------------------------
67
68 class WXDLLEXPORT wxListItemData : public wxObject
69 {
70 public:
71 wxListItemData();
72 ~wxListItemData() { delete m_attr; }
73
74 wxListItemData( const wxListItem &info );
75 void SetItem( const wxListItem &info );
76 void SetImage( int image );
77 void SetData( long data );
78 void SetPosition( int x, int y );
79 void SetSize( int width, int height );
80 bool HasImage() const;
81
82 bool HasText() const { return !m_text.empty(); }
83 const wxString& GetText() const { return m_text; }
84 void SetText(const wxString& text) { m_text = text; }
85
86 bool IsHit( int x, int y ) const;
87
88 int GetX( void ) const;
89 int GetY( void ) const;
90 int GetWidth() const;
91 int GetHeight() const;
92 int GetImage() const;
93 void GetItem( wxListItem &info ) const;
94
95 wxListItemAttr *GetAttributes() const { return m_attr; }
96
97 public:
98 int m_image;
99 long m_data;
100 int m_xpos,
101 m_ypos;
102 int m_width,
103 m_height;
104
105 wxListItemAttr *m_attr;
106
107 protected:
108 wxString m_text;
109
110 private:
111 DECLARE_DYNAMIC_CLASS(wxListItemData);
112 };
113
114 //-----------------------------------------------------------------------------
115 // wxListHeaderData (internal)
116 //-----------------------------------------------------------------------------
117
118 class WXDLLEXPORT wxListHeaderData : public wxObject
119 {
120 protected:
121 long m_mask;
122 int m_image;
123 wxString m_text;
124 int m_format;
125 int m_width;
126 int m_xpos,
127 m_ypos;
128 int m_height;
129
130 public:
131 wxListHeaderData();
132 wxListHeaderData( const wxListItem &info );
133 void SetItem( const wxListItem &item );
134 void SetPosition( int x, int y );
135 void SetWidth( int w );
136 void SetFormat( int format );
137 void SetHeight( int h );
138 bool HasImage() const;
139
140 bool HasText() const { return !m_text.empty(); }
141 const wxString& GetText() const { return m_text; }
142 void SetText(const wxString& text) { m_text = text; }
143
144 void GetItem( wxListItem &item );
145
146 bool IsHit( int x, int y ) const;
147 int GetImage() const;
148 int GetWidth() const;
149 int GetFormat() const;
150
151 private:
152 DECLARE_DYNAMIC_CLASS(wxListHeaderData);
153 };
154
155 //-----------------------------------------------------------------------------
156 // wxListLineData (internal)
157 //-----------------------------------------------------------------------------
158
159 WX_DECLARE_LIST(wxListItemData, wxListItemDataList);
160 #include "wx/listimpl.cpp"
161 WX_DEFINE_LIST(wxListItemDataList);
162
163 class WXDLLEXPORT wxListLineData : public wxObject
164 {
165 public:
166 wxListItemDataList m_items;
167 wxRect m_bound_all;
168 wxRect m_bound_label;
169 wxRect m_bound_icon;
170 wxRect m_bound_hilight;
171 int m_mode;
172 bool m_hilighted;
173 wxBrush *m_hilightBrush;
174 int m_spacing;
175 wxListMainWindow *m_owner;
176
177 void DoDraw( wxDC *dc, bool hilight, bool paintBG );
178
179 public:
180 wxListLineData() {}
181 wxListLineData( wxListMainWindow *owner, int mode, wxBrush *hilightBrush );
182 void CalculateSize( wxDC *dc, int spacing );
183 void SetPosition( wxDC *dc, int x, int y, int window_width );
184 void SetColumnPosition( int index, int x );
185 void GetSize( int &width, int &height );
186 void GetExtent( int &x, int &y, int &width, int &height );
187 void GetLabelExtent( int &x, int &y, int &width, int &height );
188 long IsHit( int x, int y );
189 void InitItems( int num );
190 void SetItem( int index, const wxListItem &info );
191 void GetItem( int index, wxListItem &info );
192 wxString GetText(int index) const;
193 void SetText( int index, const wxString s );
194 int GetImage( int index );
195 void GetRect( wxRect &rect );
196 void Hilight( bool on );
197 void ReverseHilight();
198 void DrawRubberBand( wxDC *dc, bool on );
199 void Draw( wxDC *dc );
200 bool IsInRect( int x, int y, const wxRect &rect );
201 bool IsHilighted();
202 void AssignRect( wxRect &dest, int x, int y, int width, int height );
203 void AssignRect( wxRect &dest, const wxRect &source );
204
205 private:
206 void SetAttributes(wxDC *dc,
207 const wxListItemAttr *attr,
208 const wxColour& colText, const wxFont& font,
209 bool hilight);
210
211 DECLARE_DYNAMIC_CLASS(wxListLineData);
212 };
213
214
215 WX_DECLARE_EXPORTED_OBJARRAY(wxListLineData, wxListLineDataArray);
216 #include "wx/arrimpl.cpp"
217 WX_DEFINE_OBJARRAY(wxListLineDataArray);
218
219 //-----------------------------------------------------------------------------
220 // wxListHeaderWindow (internal)
221 //-----------------------------------------------------------------------------
222
223 class WXDLLEXPORT wxListHeaderWindow : public wxWindow
224 {
225 protected:
226 wxListMainWindow *m_owner;
227 wxCursor *m_currentCursor;
228 wxCursor *m_resizeCursor;
229 bool m_isDragging;
230
231 // column being resized
232 int m_column;
233
234 // divider line position in logical (unscrolled) coords
235 int m_currentX;
236
237 // minimal position beyond which the divider line can't be dragged in
238 // logical coords
239 int m_minX;
240
241 public:
242 wxListHeaderWindow();
243 virtual ~wxListHeaderWindow();
244
245 wxListHeaderWindow( wxWindow *win,
246 wxWindowID id,
247 wxListMainWindow *owner,
248 const wxPoint &pos = wxDefaultPosition,
249 const wxSize &size = wxDefaultSize,
250 long style = 0,
251 const wxString &name = "wxlistctrlcolumntitles" );
252
253 void DoDrawRect( wxDC *dc, int x, int y, int w, int h );
254 void DrawCurrent();
255 void AdjustDC(wxDC& dc);
256
257 void OnPaint( wxPaintEvent &event );
258 void OnMouse( wxMouseEvent &event );
259 void OnSetFocus( wxFocusEvent &event );
260
261 // needs refresh
262 bool m_dirty;
263
264 private:
265 DECLARE_DYNAMIC_CLASS(wxListHeaderWindow)
266 DECLARE_EVENT_TABLE()
267 };
268
269 //-----------------------------------------------------------------------------
270 // wxListRenameTimer (internal)
271 //-----------------------------------------------------------------------------
272
273 class WXDLLEXPORT wxListRenameTimer: public wxTimer
274 {
275 private:
276 wxListMainWindow *m_owner;
277
278 public:
279 wxListRenameTimer( wxListMainWindow *owner );
280 void Notify();
281 };
282
283 //-----------------------------------------------------------------------------
284 // wxListTextCtrl (internal)
285 //-----------------------------------------------------------------------------
286
287 class WXDLLEXPORT wxListTextCtrl: public wxTextCtrl
288 {
289 private:
290 bool *m_accept;
291 wxString *m_res;
292 wxListMainWindow *m_owner;
293 wxString m_startValue;
294
295 public:
296 wxListTextCtrl() {}
297 wxListTextCtrl( wxWindow *parent, const wxWindowID id,
298 bool *accept, wxString *res, wxListMainWindow *owner,
299 const wxString &value = "",
300 const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize,
301 int style = 0,
302 const wxValidator& validator = wxDefaultValidator,
303 const wxString &name = "listctrltextctrl" );
304 void OnChar( wxKeyEvent &event );
305 void OnKeyUp( wxKeyEvent &event );
306 void OnKillFocus( wxFocusEvent &event );
307
308 private:
309 DECLARE_DYNAMIC_CLASS(wxListTextCtrl);
310 DECLARE_EVENT_TABLE()
311 };
312
313 //-----------------------------------------------------------------------------
314 // wxListMainWindow (internal)
315 //-----------------------------------------------------------------------------
316
317 class WXDLLEXPORT wxListMainWindow: public wxScrolledWindow
318 {
319 public:
320 long m_mode;
321 wxListLineDataArray m_lines;
322 wxList m_columns;
323 wxListLineData *m_current;
324 wxListLineData *m_currentEdit;
325 int m_visibleLines;
326 wxBrush *m_hilightBrush;
327 wxColour *m_hilightColour;
328 int m_xScroll,m_yScroll;
329 bool m_dirty;
330 wxImageList *m_small_image_list;
331 wxImageList *m_normal_image_list;
332 int m_small_spacing;
333 int m_normal_spacing;
334 bool m_hasFocus;
335 bool m_usedKeys;
336 bool m_lastOnSame;
337 wxTimer *m_renameTimer;
338 bool m_renameAccept;
339 wxString m_renameRes;
340 bool m_isCreated;
341 int m_dragCount;
342 wxPoint m_dragStart;
343
344 // for double click logic
345 wxListLineData *m_lineLastClicked,
346 *m_lineBeforeLastClicked;
347
348 public:
349 wxListMainWindow();
350 wxListMainWindow( wxWindow *parent, wxWindowID id,
351 const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize,
352 long style = 0, const wxString &name = "listctrlmainwindow" );
353 ~wxListMainWindow();
354 void RefreshLine( wxListLineData *line );
355 void OnPaint( wxPaintEvent &event );
356 void HilightAll( bool on );
357 void SendNotify( wxListLineData *line,
358 wxEventType command,
359 wxPoint point = wxDefaultPosition );
360 void FocusLine( wxListLineData *line );
361 void UnfocusLine( wxListLineData *line );
362 void SelectLine( wxListLineData *line );
363 void DeselectLine( wxListLineData *line );
364 void DeleteLine( wxListLineData *line );
365
366 void EditLabel( long item );
367 void Edit( long item ) { EditLabel(item); } // deprecated
368 void OnRenameTimer();
369 void OnRenameAccept();
370
371 void OnMouse( wxMouseEvent &event );
372 void MoveToFocus();
373 void OnArrowChar( wxListLineData *newCurrent, bool shiftDown );
374 void OnChar( wxKeyEvent &event );
375 void OnKeyDown( wxKeyEvent &event );
376 void OnSetFocus( wxFocusEvent &event );
377 void OnKillFocus( wxFocusEvent &event );
378 void OnSize( wxSizeEvent &event );
379 void OnScroll(wxScrollWinEvent& event) ;
380
381 void DrawImage( int index, wxDC *dc, int x, int y );
382 void GetImageSize( int index, int &width, int &height );
383 int GetIndexOfLine( const wxListLineData *line );
384 int GetTextLength( wxString &s ); // should be const
385
386 void SetImageList( wxImageList *imageList, int which );
387 void SetItemSpacing( int spacing, bool isSmall = FALSE );
388 int GetItemSpacing( bool isSmall = FALSE );
389 void SetColumn( int col, wxListItem &item );
390 void SetColumnWidth( int col, int width );
391 void GetColumn( int col, wxListItem &item );
392 int GetColumnWidth( int vol );
393 int GetColumnCount();
394 int GetCountPerPage();
395 void SetItem( wxListItem &item );
396 void GetItem( wxListItem &item );
397 void SetItemState( long item, long state, long stateMask );
398 int GetItemState( long item, long stateMask );
399 int GetItemCount();
400 void GetItemRect( long index, wxRect &rect );
401 bool GetItemPosition( long item, wxPoint& pos );
402 int GetSelectedItemCount();
403 void SetMode( long mode );
404 long GetMode() const;
405 void CalculatePositions();
406 void RealizeChanges();
407 long GetNextItem( long item, int geometry, int state );
408 void DeleteItem( long index );
409 void DeleteAllItems();
410 void DeleteColumn( int col );
411 void DeleteEverything();
412 void EnsureVisible( long index );
413 long FindItem( long start, const wxString& str, bool partial = FALSE );
414 long FindItem( long start, long data);
415 long HitTest( int x, int y, int &flags );
416 void InsertItem( wxListItem &item );
417 // void AddItem( wxListItem &item );
418 void InsertColumn( long col, wxListItem &item );
419 // void AddColumn( wxListItem &item );
420 void SortItems( wxListCtrlCompare fn, long data );
421
422 void SetItemCount(long count);
423
424 private:
425 DECLARE_DYNAMIC_CLASS(wxListMainWindow);
426 DECLARE_EVENT_TABLE()
427 };
428
429 // ============================================================================
430 // implementation
431 // ============================================================================
432
433 //-----------------------------------------------------------------------------
434 // wxListItemData
435 //-----------------------------------------------------------------------------
436
437 IMPLEMENT_DYNAMIC_CLASS(wxListItemData,wxObject);
438
439 wxListItemData::wxListItemData()
440 {
441 m_image = -1;
442 m_data = 0;
443 m_xpos = 0;
444 m_ypos = 0;
445 m_width = 0;
446 m_height = 0;
447 m_attr = NULL;
448 }
449
450 wxListItemData::wxListItemData( const wxListItem &info )
451 {
452 m_image = -1;
453 m_data = 0;
454 m_attr = NULL;
455
456 SetItem( info );
457 }
458
459 void wxListItemData::SetItem( const wxListItem &info )
460 {
461 if (info.m_mask & wxLIST_MASK_TEXT)
462 SetText(info.m_text);
463 if (info.m_mask & wxLIST_MASK_IMAGE)
464 m_image = info.m_image;
465 if (info.m_mask & wxLIST_MASK_DATA)
466 m_data = info.m_data;
467
468 if ( info.HasAttributes() )
469 {
470 if ( m_attr )
471 *m_attr = *info.GetAttributes();
472 else
473 m_attr = new wxListItemAttr(*info.GetAttributes());
474 }
475
476 m_xpos = 0;
477 m_ypos = 0;
478 m_width = info.m_width;
479 m_height = 0;
480 }
481
482 void wxListItemData::SetImage( int image )
483 {
484 m_image = image;
485 }
486
487 void wxListItemData::SetData( long data )
488 {
489 m_data = data;
490 }
491
492 void wxListItemData::SetPosition( int x, int y )
493 {
494 m_xpos = x;
495 m_ypos = y;
496 }
497
498 void wxListItemData::SetSize( int width, int height )
499 {
500 if (width != -1) m_width = width;
501 if (height != -1) m_height = height;
502 }
503
504 bool wxListItemData::HasImage() const
505 {
506 return (m_image >= 0);
507 }
508
509 bool wxListItemData::IsHit( int x, int y ) const
510 {
511 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
512 }
513
514 int wxListItemData::GetX() const
515 {
516 return m_xpos;
517 }
518
519 int wxListItemData::GetY() const
520 {
521 return m_ypos;
522 }
523
524 int wxListItemData::GetWidth() const
525 {
526 return m_width;
527 }
528
529 int wxListItemData::GetHeight() const
530 {
531 return m_height;
532 }
533
534 int wxListItemData::GetImage() const
535 {
536 return m_image;
537 }
538
539 void wxListItemData::GetItem( wxListItem &info ) const
540 {
541 info.m_text = m_text;
542 info.m_image = m_image;
543 info.m_data = m_data;
544
545 if ( m_attr )
546 {
547 if ( m_attr->HasTextColour() )
548 info.SetTextColour(m_attr->GetTextColour());
549 if ( m_attr->HasBackgroundColour() )
550 info.SetBackgroundColour(m_attr->GetBackgroundColour());
551 if ( m_attr->HasFont() )
552 info.SetFont(m_attr->GetFont());
553 }
554 }
555
556 //-----------------------------------------------------------------------------
557 // wxListHeaderData
558 //-----------------------------------------------------------------------------
559
560 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderData,wxObject);
561
562 wxListHeaderData::wxListHeaderData()
563 {
564 m_mask = 0;
565 m_image = 0;
566 m_format = 0;
567 m_width = 0;
568 m_xpos = 0;
569 m_ypos = 0;
570 m_height = 0;
571 }
572
573 wxListHeaderData::wxListHeaderData( const wxListItem &item )
574 {
575 SetItem( item );
576 m_xpos = 0;
577 m_ypos = 0;
578 m_height = 0;
579 }
580
581 void wxListHeaderData::SetItem( const wxListItem &item )
582 {
583 m_mask = item.m_mask;
584 m_text = item.m_text;
585 m_image = item.m_image;
586 m_format = item.m_format;
587 m_width = item.m_width;
588 if (m_width < 0) m_width = 80;
589 if (m_width < 6) m_width = 6;
590 }
591
592 void wxListHeaderData::SetPosition( int x, int y )
593 {
594 m_xpos = x;
595 m_ypos = y;
596 }
597
598 void wxListHeaderData::SetHeight( int h )
599 {
600 m_height = h;
601 }
602
603 void wxListHeaderData::SetWidth( int w )
604 {
605 m_width = w;
606 if (m_width < 0) m_width = 80;
607 if (m_width < 6) m_width = 6;
608 }
609
610 void wxListHeaderData::SetFormat( int format )
611 {
612 m_format = format;
613 }
614
615 bool wxListHeaderData::HasImage() const
616 {
617 return (m_image != 0);
618 }
619
620 bool wxListHeaderData::IsHit( int x, int y ) const
621 {
622 return ((x >= m_xpos) && (x <= m_xpos+m_width) && (y >= m_ypos) && (y <= m_ypos+m_height));
623 }
624
625 void wxListHeaderData::GetItem( wxListItem &item )
626 {
627 item.m_mask = m_mask;
628 item.m_text = m_text;
629 item.m_image = m_image;
630 item.m_format = m_format;
631 item.m_width = m_width;
632 }
633
634 int wxListHeaderData::GetImage() const
635 {
636 return m_image;
637 }
638
639 int wxListHeaderData::GetWidth() const
640 {
641 return m_width;
642 }
643
644 int wxListHeaderData::GetFormat() const
645 {
646 return m_format;
647 }
648
649 //-----------------------------------------------------------------------------
650 // wxListLineData
651 //-----------------------------------------------------------------------------
652
653 IMPLEMENT_DYNAMIC_CLASS(wxListLineData,wxObject);
654
655 wxListLineData::wxListLineData( wxListMainWindow *owner, int mode, wxBrush *hilightBrush )
656 {
657 m_mode = mode;
658 m_hilighted = FALSE;
659 m_owner = owner;
660 m_hilightBrush = hilightBrush;
661 m_items.DeleteContents( TRUE );
662 m_spacing = 0;
663 }
664
665 void wxListLineData::CalculateSize( wxDC *dc, int spacing )
666 {
667 wxListItemDataList::Node *node = m_items.GetFirst();
668
669 m_spacing = spacing;
670 switch (m_mode)
671 {
672 case wxLC_ICON:
673 {
674 m_bound_all.width = m_spacing;
675 if (node)
676 {
677 wxListItemData *item = node->GetData();
678 wxString s = item->GetText();
679 if (s.IsEmpty())
680 s = wxT("H");
681
682 wxCoord lw,lh;
683 dc->GetTextExtent( s, &lw, &lh );
684 if (lh < 15)
685 lh = 15;
686 lw += 4;
687 lh += 3;
688
689 m_bound_all.height = m_spacing+lh;
690 if (lw > m_spacing)
691 m_bound_all.width = lw;
692 m_bound_label.width = lw;
693 m_bound_label.height = lh;
694
695 if (item->HasImage())
696 {
697 int w = 0;
698 int h = 0;
699 m_owner->GetImageSize( item->GetImage(), w, h );
700 m_bound_icon.width = w + 8;
701 m_bound_icon.height = h + 8;
702
703 if ( m_bound_icon.width > m_bound_all.width )
704 m_bound_all.width = m_bound_icon.width;
705 if ( h + lh > m_bound_all.height - 4 )
706 m_bound_all.height = h + lh + 4;
707 }
708
709 if (!item->HasText())
710 {
711 m_bound_hilight.width = m_bound_icon.width;
712 m_bound_hilight.height = m_bound_icon.height;
713 }
714 else
715 {
716 m_bound_hilight.width = m_bound_label.width;
717 m_bound_hilight.height = m_bound_label.height;
718 }
719 }
720 break;
721 }
722 case wxLC_LIST:
723 {
724 if (node)
725 {
726 wxListItemData *item = node->GetData();
727
728 wxString s = item->GetText();
729 if (s.IsEmpty())
730 s = wxT("H");
731
732 wxCoord lw,lh;
733 dc->GetTextExtent( s, &lw, &lh );
734 if (lh < 15)
735 lh = 15;
736 lw += 4;
737 lh += 3;
738
739 m_bound_label.width = lw;
740 m_bound_label.height = lh;
741
742 m_bound_all.width = lw;
743 m_bound_all.height = lh;
744
745 if (item->HasImage())
746 {
747 int w = 0;
748 int h = 0;
749 m_owner->GetImageSize( item->GetImage(), w, h );
750 m_bound_icon.width = w;
751 m_bound_icon.height = h;
752
753 m_bound_all.width += 4 + w;
754 if (h > m_bound_all.height) m_bound_all.height = h;
755 }
756
757 m_bound_hilight.width = m_bound_all.width;
758 m_bound_hilight.height = m_bound_all.height;
759 }
760 break;
761 }
762 case wxLC_REPORT:
763 {
764 m_bound_all.width = 0;
765 m_bound_all.height = 0;
766 if (node)
767 {
768 wxListItemData *item = node->GetData();
769 if (item->HasImage())
770 {
771 int w = 0;
772 int h = 0;
773 m_owner->GetImageSize( item->GetImage(), w, h );
774 m_bound_icon.width = w;
775 m_bound_icon.height = h;
776 }
777 else
778 {
779 m_bound_icon.width = 0;
780 m_bound_icon.height = 0;
781 }
782 }
783
784 while (node)
785 {
786 wxListItemData *item = node->GetData();
787 wxString s = item->GetText();
788 if (s.IsEmpty())
789 s = wxT("H");
790
791 wxCoord lw,lh;
792 dc->GetTextExtent( s, &lw, &lh );
793 if (lh < 15)
794 lh = 15;
795 lw += 4;
796 lh += 3;
797
798 item->SetSize( item->GetWidth(), lh );
799 m_bound_all.width += lw;
800 m_bound_all.height = lh;
801 node = node->GetNext();
802 }
803 m_bound_label.width = m_bound_all.width;
804 m_bound_label.height = m_bound_all.height;
805 break;
806 }
807 }
808 }
809
810 void wxListLineData::SetPosition( wxDC * WXUNUSED(dc),
811 int x, int y, int window_width )
812 {
813 wxListItemDataList::Node *node = m_items.GetFirst();
814
815 m_bound_all.x = x;
816 m_bound_all.y = y;
817
818 switch (m_mode)
819 {
820 case wxLC_ICON:
821 {
822 if (node)
823 {
824 wxListItemData *item = node->GetData();
825 if (item->HasImage())
826 {
827 m_bound_icon.x = m_bound_all.x + 4
828 + (m_spacing - m_bound_icon.width)/2;
829 m_bound_icon.y = m_bound_all.y + 4;
830 }
831 if (item->HasText())
832 {
833 if (m_bound_all.width > m_spacing)
834 m_bound_label.x = m_bound_all.x + 2;
835 else
836 m_bound_label.x = m_bound_all.x + 2 + (m_spacing/2) - (m_bound_label.width/2);
837 m_bound_label.y = m_bound_all.y + m_bound_all.height + 2 - m_bound_label.height;
838 m_bound_hilight.x = m_bound_label.x - 2;
839 m_bound_hilight.y = m_bound_label.y - 2;
840 }
841 else
842 {
843 m_bound_hilight.x = m_bound_icon.x - 4;
844 m_bound_hilight.y = m_bound_icon.y - 4;
845 }
846 }
847 break;
848 }
849 case wxLC_LIST:
850 {
851 m_bound_hilight.x = m_bound_all.x;
852 m_bound_hilight.y = m_bound_all.y;
853 m_bound_label.y = m_bound_all.y + 2;
854 if (node)
855 {
856 wxListItemData *item = node->GetData();
857 if (item->HasImage())
858 {
859 m_bound_icon.x = m_bound_all.x + 2;
860 m_bound_icon.y = m_bound_all.y + 2;
861 m_bound_label.x = m_bound_all.x + 6 + m_bound_icon.width;
862 }
863 else
864 {
865 m_bound_label.x = m_bound_all.x + 2;
866 }
867 }
868 break;
869 }
870 case wxLC_REPORT:
871 {
872 m_bound_all.x = 0;
873 m_bound_all.width = window_width;
874 AssignRect( m_bound_hilight, m_bound_all );
875 m_bound_label.x = m_bound_all.x + 2;
876 m_bound_label.y = m_bound_all.y + 2;
877 if (node)
878 {
879 wxListItemData *item = node->GetData();
880 if (item->HasImage())
881 {
882 m_bound_icon.x = m_bound_all.x + 2;
883 m_bound_icon.y = m_bound_all.y + 2;
884 m_bound_label.x += 4 + m_bound_icon.width;
885 }
886 }
887 break;
888 }
889 }
890 }
891
892 void wxListLineData::SetColumnPosition( int index, int x )
893 {
894 wxListItemDataList::Node *node = m_items.Item( (size_t)index );
895 if (node)
896 {
897 wxListItemData *item = node->GetData();
898 item->SetPosition( x, m_bound_all.y+1 );
899 }
900 }
901
902 void wxListLineData::GetSize( int &width, int &height )
903 {
904 width = m_bound_all.width;
905 height = m_bound_all.height;
906 }
907
908 void wxListLineData::GetExtent( int &x, int &y, int &width, int &height )
909 {
910 x = m_bound_all.x;
911 y = m_bound_all.y;
912 width = m_bound_all.width;
913 height = m_bound_all.height;
914 }
915
916 void wxListLineData::GetLabelExtent( int &x, int &y, int &width, int &height )
917 {
918 x = m_bound_label.x;
919 y = m_bound_label.y;
920 width = m_bound_label.width;
921 height = m_bound_label.height;
922 }
923
924 void wxListLineData::GetRect( wxRect &rect )
925 {
926 AssignRect( rect, m_bound_all );
927 }
928
929 long wxListLineData::IsHit( int x, int y )
930 {
931 wxListItemDataList::Node *node = m_items.GetFirst();
932 if (node)
933 {
934 wxListItemData *item = node->GetData();
935 if (item->HasImage() && IsInRect( x, y, m_bound_icon ))
936 return wxLIST_HITTEST_ONITEMICON;
937 if (item->HasText() && IsInRect( x, y, m_bound_label ))
938 return wxLIST_HITTEST_ONITEMLABEL;
939
940 }
941
942 // if there is no icon or text = empty
943 if (IsInRect( x, y, m_bound_all ))
944 return wxLIST_HITTEST_ONITEMICON;
945
946 return 0;
947 }
948
949 void wxListLineData::InitItems( int num )
950 {
951 for (int i = 0; i < num; i++)
952 m_items.Append( new wxListItemData() );
953 }
954
955 void wxListLineData::SetItem( int index, const wxListItem &info )
956 {
957 wxListItemDataList::Node *node = m_items.Item( index );
958 if (node)
959 {
960 wxListItemData *item = node->GetData();
961 item->SetItem( info );
962 }
963 }
964
965 void wxListLineData::GetItem( int index, wxListItem &info )
966 {
967 wxListItemDataList::Node *node = m_items.Item( index );
968 if (node)
969 {
970 wxListItemData *item = node->GetData();
971 item->GetItem( info );
972 }
973 }
974
975 wxString wxListLineData::GetText(int index) const
976 {
977 wxString s;
978
979 wxListItemDataList::Node *node = m_items.Item( index );
980 if (node)
981 {
982 wxListItemData *item = node->GetData();
983 s = item->GetText();
984 }
985
986 return s;
987 }
988
989 void wxListLineData::SetText( int index, const wxString s )
990 {
991 wxListItemDataList::Node *node = m_items.Item( index );
992 if (node)
993 {
994 wxListItemData *item = node->GetData();
995 item->SetText( s );
996 }
997 }
998
999 int wxListLineData::GetImage( int index )
1000 {
1001 wxListItemDataList::Node *node = m_items.Item( index );
1002 if (node)
1003 {
1004 wxListItemData *item = node->GetData();
1005 return item->GetImage();
1006 }
1007 return -1;
1008 }
1009
1010 void wxListLineData::SetAttributes(wxDC *dc,
1011 const wxListItemAttr *attr,
1012 const wxColour& colText,
1013 const wxFont& font,
1014 bool hilight)
1015 {
1016 // don't use foregroud colour for drawing highlighted items - this might
1017 // make them completely invisible (and there is no way to do bit
1018 // arithmetics on wxColour, unfortunately)
1019 if ( !hilight && attr && attr->HasTextColour() )
1020 {
1021 dc->SetTextForeground(attr->GetTextColour());
1022 }
1023 else
1024 {
1025 dc->SetTextForeground(colText);
1026 }
1027
1028 if ( attr && attr->HasFont() )
1029 {
1030 dc->SetFont(attr->GetFont());
1031 }
1032 else
1033 {
1034 dc->SetFont(font);
1035 }
1036 }
1037
1038 void wxListLineData::DoDraw( wxDC *dc, bool hilight, bool paintBG )
1039 {
1040 int dev_x = 0;
1041 int dev_y = 0;
1042 m_owner->CalcScrolledPosition( m_bound_all.x, m_bound_all.y, &dev_x, &dev_y );
1043 wxCoord dev_w = m_bound_all.width;
1044 wxCoord dev_h = m_bound_all.height;
1045
1046 if (!m_owner->IsExposed( dev_x, dev_y, dev_w, dev_h ))
1047 return;
1048
1049 wxWindow *listctrl = m_owner->GetParent();
1050
1051 // default foreground colour
1052 wxColour colText;
1053 if ( hilight )
1054 {
1055 colText = wxSystemSettings::GetSystemColour( wxSYS_COLOUR_HIGHLIGHTTEXT );
1056 }
1057 else
1058 {
1059 colText = listctrl->GetForegroundColour();
1060 }
1061
1062 // default font
1063 wxFont font = listctrl->GetFont();
1064
1065 // VZ: currently we set the colours/fonts only once, but like this (i.e.
1066 // using SetAttributes() inside the loop), it will be trivial to
1067 // customize the subitems (in report mode) too.
1068 wxListItemData *item = m_items.GetFirst()->GetData();
1069 wxListItemAttr *attr = item->GetAttributes();
1070 SetAttributes(dc, attr, colText, font, hilight);
1071
1072 bool hasBgCol = attr && attr->HasBackgroundColour();
1073 if ( paintBG || hasBgCol )
1074 {
1075 if (hilight)
1076 {
1077 dc->SetBrush( * m_hilightBrush );
1078 }
1079 else
1080 {
1081 if ( hasBgCol )
1082 dc->SetBrush(wxBrush(attr->GetBackgroundColour(), wxSOLID));
1083 else
1084 dc->SetBrush( * wxWHITE_BRUSH );
1085 }
1086
1087 dc->SetPen( * wxTRANSPARENT_PEN );
1088 dc->DrawRectangle( m_bound_hilight.x, m_bound_hilight.y,
1089 m_bound_hilight.width, m_bound_hilight.height );
1090 }
1091
1092 wxListItemDataList::Node *node = m_items.GetFirst();
1093
1094 if (m_mode == wxLC_REPORT)
1095 {
1096 while (node)
1097 {
1098 wxListItemData *item = node->GetData();
1099 int x = item->GetX();
1100 if (item->HasImage())
1101 {
1102 int y = 0;
1103 m_owner->DrawImage( item->GetImage(), dc, x, item->GetY() );
1104 m_owner->GetImageSize( item->GetImage(), x, y );
1105 x += item->GetX() + 5;
1106 }
1107 dc->SetClippingRegion( item->GetX(), item->GetY(), item->GetWidth()-3, item->GetHeight() );
1108 if (item->HasText())
1109 {
1110 dc->DrawText( item->GetText(), x, item->GetY()+1 );
1111 }
1112 dc->DestroyClippingRegion();
1113 node = node->GetNext();
1114 }
1115 }
1116 else // !report
1117 {
1118 if (node)
1119 {
1120 wxListItemData *item = node->GetData();
1121 if (item->HasImage())
1122 {
1123 m_owner->DrawImage( item->GetImage(), dc, m_bound_icon.x, m_bound_icon.y );
1124 }
1125 if (item->HasText())
1126 {
1127 dc->DrawText( item->GetText(), m_bound_label.x, m_bound_label.y );
1128 }
1129 }
1130 }
1131 }
1132
1133 void wxListLineData::Hilight( bool on )
1134 {
1135 if (on == m_hilighted) return;
1136 m_hilighted = on;
1137 if (on)
1138 m_owner->SelectLine( this );
1139 else
1140 m_owner->DeselectLine( this );
1141 }
1142
1143 void wxListLineData::ReverseHilight( void )
1144 {
1145 m_hilighted = !m_hilighted;
1146 if (m_hilighted)
1147 m_owner->SelectLine( this );
1148 else
1149 m_owner->DeselectLine( this );
1150 }
1151
1152 void wxListLineData::DrawRubberBand( wxDC *dc, bool on )
1153 {
1154 if (on)
1155 {
1156 dc->SetPen( * wxBLACK_PEN );
1157 dc->SetBrush( * wxTRANSPARENT_BRUSH );
1158 dc->DrawRectangle( m_bound_hilight.x, m_bound_hilight.y,
1159 m_bound_hilight.width, m_bound_hilight.height );
1160 }
1161 }
1162
1163 void wxListLineData::Draw( wxDC *dc )
1164 {
1165 DoDraw( dc, m_hilighted, m_hilighted );
1166 }
1167
1168 bool wxListLineData::IsInRect( int x, int y, const wxRect &rect )
1169 {
1170 return ((x >= rect.x) && (x <= rect.x+rect.width) &&
1171 (y >= rect.y) && (y <= rect.y+rect.height));
1172 }
1173
1174 bool wxListLineData::IsHilighted( void )
1175 {
1176 return m_hilighted;
1177 }
1178
1179 void wxListLineData::AssignRect( wxRect &dest, int x, int y, int width, int height )
1180 {
1181 dest.x = x;
1182 dest.y = y;
1183 dest.width = width;
1184 dest.height = height;
1185 }
1186
1187 void wxListLineData::AssignRect( wxRect &dest, const wxRect &source )
1188 {
1189 dest.x = source.x;
1190 dest.y = source.y;
1191 dest.width = source.width;
1192 dest.height = source.height;
1193 }
1194
1195 //-----------------------------------------------------------------------------
1196 // wxListHeaderWindow
1197 //-----------------------------------------------------------------------------
1198
1199 IMPLEMENT_DYNAMIC_CLASS(wxListHeaderWindow,wxWindow);
1200
1201 BEGIN_EVENT_TABLE(wxListHeaderWindow,wxWindow)
1202 EVT_PAINT (wxListHeaderWindow::OnPaint)
1203 EVT_MOUSE_EVENTS (wxListHeaderWindow::OnMouse)
1204 EVT_SET_FOCUS (wxListHeaderWindow::OnSetFocus)
1205 END_EVENT_TABLE()
1206
1207 wxListHeaderWindow::wxListHeaderWindow( void )
1208 {
1209 m_owner = (wxListMainWindow *) NULL;
1210 m_currentCursor = (wxCursor *) NULL;
1211 m_resizeCursor = (wxCursor *) NULL;
1212 m_isDragging = FALSE;
1213 }
1214
1215 wxListHeaderWindow::wxListHeaderWindow( wxWindow *win, wxWindowID id, wxListMainWindow *owner,
1216 const wxPoint &pos, const wxSize &size,
1217 long style, const wxString &name ) :
1218 wxWindow( win, id, pos, size, style, name )
1219 {
1220 m_owner = owner;
1221 // m_currentCursor = wxSTANDARD_CURSOR;
1222 m_currentCursor = (wxCursor *) NULL;
1223 m_resizeCursor = new wxCursor( wxCURSOR_SIZEWE );
1224 m_isDragging = FALSE;
1225 m_dirty = FALSE;
1226
1227 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNFACE ) );
1228 }
1229
1230 wxListHeaderWindow::~wxListHeaderWindow( void )
1231 {
1232 delete m_resizeCursor;
1233 }
1234
1235 void wxListHeaderWindow::DoDrawRect( wxDC *dc, int x, int y, int w, int h )
1236 {
1237 #ifdef __WXGTK__
1238 GtkStateType state = GTK_STATE_NORMAL;
1239 if (!m_parent->IsEnabled()) state = GTK_STATE_INSENSITIVE;
1240
1241 x = dc->XLOG2DEV( x );
1242
1243 gtk_paint_box (m_wxwindow->style, GTK_PIZZA(m_wxwindow)->bin_window, state, GTK_SHADOW_OUT,
1244 (GdkRectangle*) NULL, m_wxwindow, "button", x-1, y-1, w+2, h+2);
1245 #else
1246 const int m_corner = 1;
1247
1248 dc->SetBrush( *wxTRANSPARENT_BRUSH );
1249
1250 dc->SetPen( *wxBLACK_PEN );
1251 dc->DrawLine( x+w-m_corner+1, y, x+w, y+h ); // right (outer)
1252 dc->DrawRectangle( x, y+h, w+1, 1 ); // bottom (outer)
1253
1254 wxPen pen( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_BTNSHADOW ), 1, wxSOLID );
1255
1256 dc->SetPen( pen );
1257 dc->DrawLine( x+w-m_corner, y, x+w-1, y+h ); // right (inner)
1258 dc->DrawRectangle( x+1, y+h-1, w-2, 1 ); // bottom (inner)
1259
1260 dc->SetPen( *wxWHITE_PEN );
1261 dc->DrawRectangle( x, y, w-m_corner+1, 1 ); // top (outer)
1262 dc->DrawRectangle( x, y, 1, h ); // left (outer)
1263 dc->DrawLine( x, y+h-1, x+1, y+h-1 );
1264 dc->DrawLine( x+w-1, y, x+w-1, y+1 );
1265 #endif
1266 }
1267
1268 // shift the DC origin to match the position of the main window horz
1269 // scrollbar: this allows us to always use logical coords
1270 void wxListHeaderWindow::AdjustDC(wxDC& dc)
1271 {
1272 #if wxUSE_GENERIC_LIST_EXTENSIONS
1273 int xpix;
1274 m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
1275
1276 int x;
1277 m_owner->GetViewStart( &x, NULL );
1278
1279 // account for the horz scrollbar offset
1280 dc.SetDeviceOrigin( -x * xpix, 0 );
1281 #endif // wxUSE_GENERIC_LIST_EXTENSIONS
1282 }
1283
1284 void wxListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1285 {
1286 #ifdef __WXGTK__
1287 wxClientDC dc( this );
1288 #else
1289 wxPaintDC dc( this );
1290 #endif
1291
1292 PrepareDC( dc );
1293 AdjustDC( dc );
1294
1295 dc.BeginDrawing();
1296
1297 dc.SetFont( GetFont() );
1298
1299 // width and height of the entire header window
1300 int w, h;
1301 GetClientSize( &w, &h );
1302 #if wxUSE_GENERIC_LIST_EXTENSIONS
1303 m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
1304 #endif // wxUSE_GENERIC_LIST_EXTENSIONS
1305
1306 dc.SetBackgroundMode(wxTRANSPARENT);
1307
1308 // do *not* use the listctrl colour for headers - one day we will have a
1309 // function to set it separately
1310 //dc.SetTextForeground( *wxBLACK );
1311 dc.SetTextForeground(wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOWTEXT ));
1312
1313 int x = 1; // left of the header rect
1314 const int y = 1; // top
1315 int numColumns = m_owner->GetColumnCount();
1316 wxListItem item;
1317 for (int i = 0; i < numColumns; i++)
1318 {
1319 m_owner->GetColumn( i, item );
1320 int wCol = item.m_width;
1321 int cw = wCol - 2; // the width of the rect to draw
1322
1323 int xEnd = x + wCol;
1324
1325 // VZ: no, draw it normally - this is better now as we allow resizing
1326 // of the last column as well
1327 #if 0
1328 // let the last column occupy all available space
1329 if ( i == numColumns - 1 )
1330 cw = w-x-1;
1331 #endif // 0
1332
1333 dc.SetPen( *wxWHITE_PEN );
1334
1335 DoDrawRect( &dc, x, y, cw, h-2 );
1336 dc.SetClippingRegion( x, y, cw-5, h-4 );
1337 dc.DrawText( item.GetText(), x+4, y+3 );
1338 dc.DestroyClippingRegion();
1339 x += wCol;
1340
1341 if (xEnd > w+5)
1342 break;
1343 }
1344 dc.EndDrawing();
1345 }
1346
1347 void wxListHeaderWindow::DrawCurrent()
1348 {
1349 int x1 = m_currentX;
1350 int y1 = 0;
1351 ClientToScreen( &x1, &y1 );
1352
1353 int x2 = m_currentX-1;
1354 int y2 = 0;
1355 m_owner->GetClientSize( NULL, &y2 );
1356 m_owner->ClientToScreen( &x2, &y2 );
1357
1358 wxScreenDC dc;
1359 dc.SetLogicalFunction( wxINVERT );
1360 dc.SetPen( wxPen( *wxBLACK, 2, wxSOLID ) );
1361 dc.SetBrush( *wxTRANSPARENT_BRUSH );
1362
1363 AdjustDC(dc);
1364
1365 dc.DrawLine( x1, y1, x2, y2 );
1366
1367 dc.SetLogicalFunction( wxCOPY );
1368
1369 dc.SetPen( wxNullPen );
1370 dc.SetBrush( wxNullBrush );
1371 }
1372
1373 void wxListHeaderWindow::OnMouse( wxMouseEvent &event )
1374 {
1375 // we want to work with logical coords
1376 #if wxUSE_GENERIC_LIST_EXTENSIONS
1377 int x;
1378 m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
1379 #else // !wxUSE_GENERIC_LIST_EXTENSIONS
1380 int x = event.GetX();
1381 #endif // wxUSE_GENERIC_LIST_EXTENSIONS
1382 int y = event.GetY();
1383
1384 if (m_isDragging)
1385 {
1386 // we don't draw the line beyond our window, but we allow dragging it
1387 // there
1388 int w = 0;
1389 GetClientSize( &w, NULL );
1390 #if wxUSE_GENERIC_LIST_EXTENSIONS
1391 m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
1392 #endif // wxUSE_GENERIC_LIST_EXTENSIONS
1393 w -= 6;
1394
1395 // erase the line if it was drawn
1396 if ( m_currentX < w )
1397 DrawCurrent();
1398
1399 if (event.ButtonUp())
1400 {
1401 ReleaseMouse();
1402 m_isDragging = FALSE;
1403 m_dirty = TRUE;
1404 m_owner->SetColumnWidth( m_column, m_currentX - m_minX );
1405 }
1406 else
1407 {
1408 if (x > m_minX + 7)
1409 m_currentX = x;
1410 else
1411 m_currentX = m_minX + 7;
1412
1413 // draw in the new location
1414 if ( m_currentX < w )
1415 DrawCurrent();
1416 }
1417 }
1418 else // not dragging
1419 {
1420 m_minX = 0;
1421 bool hit_border = FALSE;
1422
1423 // end of the current column
1424 int xpos = 0;
1425
1426 // find the column where this event occured
1427 int countCol = m_owner->GetColumnCount();
1428 for (int j = 0; j < countCol; j++)
1429 {
1430 xpos += m_owner->GetColumnWidth( j );
1431 m_column = j;
1432
1433 if ( (abs(x-xpos) < 3) && (y < 22) )
1434 {
1435 // near the column border
1436 hit_border = TRUE;
1437 break;
1438 }
1439
1440 if ( x < xpos )
1441 {
1442 // inside the column
1443 break;
1444 }
1445
1446 m_minX = xpos;
1447 }
1448
1449 if (event.LeftDown())
1450 {
1451 if (hit_border)
1452 {
1453 m_isDragging = TRUE;
1454 m_currentX = x;
1455 DrawCurrent();
1456 CaptureMouse();
1457 }
1458 else
1459 {
1460 wxWindow *parent = GetParent();
1461 wxListEvent le( wxEVT_COMMAND_LIST_COL_CLICK, parent->GetId() );
1462 le.SetEventObject( parent );
1463 le.m_col = m_column;
1464 parent->GetEventHandler()->ProcessEvent( le );
1465 }
1466 }
1467 else if (event.Moving())
1468 {
1469 bool setCursor;
1470 if (hit_border)
1471 {
1472 setCursor = m_currentCursor == wxSTANDARD_CURSOR;
1473 m_currentCursor = m_resizeCursor;
1474 }
1475 else
1476 {
1477 setCursor = m_currentCursor != wxSTANDARD_CURSOR;
1478 m_currentCursor = wxSTANDARD_CURSOR;
1479 }
1480
1481 if ( setCursor )
1482 SetCursor(*m_currentCursor);
1483 }
1484 }
1485 }
1486
1487 void wxListHeaderWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
1488 {
1489 m_owner->SetFocus();
1490 }
1491
1492 //-----------------------------------------------------------------------------
1493 // wxListRenameTimer (internal)
1494 //-----------------------------------------------------------------------------
1495
1496 wxListRenameTimer::wxListRenameTimer( wxListMainWindow *owner )
1497 {
1498 m_owner = owner;
1499 }
1500
1501 void wxListRenameTimer::Notify()
1502 {
1503 m_owner->OnRenameTimer();
1504 }
1505
1506 //-----------------------------------------------------------------------------
1507 // wxListTextCtrl (internal)
1508 //-----------------------------------------------------------------------------
1509
1510 IMPLEMENT_DYNAMIC_CLASS(wxListTextCtrl,wxTextCtrl);
1511
1512 BEGIN_EVENT_TABLE(wxListTextCtrl,wxTextCtrl)
1513 EVT_CHAR (wxListTextCtrl::OnChar)
1514 EVT_KEY_UP (wxListTextCtrl::OnKeyUp)
1515 EVT_KILL_FOCUS (wxListTextCtrl::OnKillFocus)
1516 END_EVENT_TABLE()
1517
1518 wxListTextCtrl::wxListTextCtrl( wxWindow *parent,
1519 const wxWindowID id,
1520 bool *accept,
1521 wxString *res,
1522 wxListMainWindow *owner,
1523 const wxString &value,
1524 const wxPoint &pos,
1525 const wxSize &size,
1526 int style,
1527 const wxValidator& validator,
1528 const wxString &name )
1529 : wxTextCtrl( parent, id, value, pos, size, style, validator, name )
1530 {
1531 m_res = res;
1532 m_accept = accept;
1533 m_owner = owner;
1534 (*m_accept) = FALSE;
1535 (*m_res) = "";
1536 m_startValue = value;
1537 }
1538
1539 void wxListTextCtrl::OnChar( wxKeyEvent &event )
1540 {
1541 if (event.m_keyCode == WXK_RETURN)
1542 {
1543 (*m_accept) = TRUE;
1544 (*m_res) = GetValue();
1545
1546 if (!wxPendingDelete.Member(this))
1547 wxPendingDelete.Append(this);
1548
1549 if ((*m_accept) && ((*m_res) != m_startValue))
1550 m_owner->OnRenameAccept();
1551
1552 return;
1553 }
1554 if (event.m_keyCode == WXK_ESCAPE)
1555 {
1556 (*m_accept) = FALSE;
1557 (*m_res) = "";
1558
1559 if (!wxPendingDelete.Member(this))
1560 wxPendingDelete.Append(this);
1561
1562 return;
1563 }
1564
1565 event.Skip();
1566 }
1567
1568 void wxListTextCtrl::OnKeyUp( wxKeyEvent &event )
1569 {
1570 // auto-grow the textctrl:
1571 wxSize parentSize = m_owner->GetSize();
1572 wxPoint myPos = GetPosition();
1573 wxSize mySize = GetSize();
1574 int sx, sy;
1575 GetTextExtent(GetValue() + _T("MM"), &sx, &sy);
1576 if (myPos.x + sx > parentSize.x) sx = parentSize.x - myPos.x;
1577 if (mySize.x > sx) sx = mySize.x;
1578 SetSize(sx, -1);
1579
1580 event.Skip();
1581 }
1582
1583 void wxListTextCtrl::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
1584 {
1585 if (!wxPendingDelete.Member(this))
1586 wxPendingDelete.Append(this);
1587
1588 if ((*m_accept) && ((*m_res) != m_startValue))
1589 m_owner->OnRenameAccept();
1590 }
1591
1592 //-----------------------------------------------------------------------------
1593 // wxListMainWindow
1594 //-----------------------------------------------------------------------------
1595
1596 IMPLEMENT_DYNAMIC_CLASS(wxListMainWindow,wxScrolledWindow);
1597
1598 BEGIN_EVENT_TABLE(wxListMainWindow,wxScrolledWindow)
1599 EVT_PAINT (wxListMainWindow::OnPaint)
1600 EVT_SIZE (wxListMainWindow::OnSize)
1601 EVT_MOUSE_EVENTS (wxListMainWindow::OnMouse)
1602 EVT_CHAR (wxListMainWindow::OnChar)
1603 EVT_KEY_DOWN (wxListMainWindow::OnKeyDown)
1604 EVT_SET_FOCUS (wxListMainWindow::OnSetFocus)
1605 EVT_KILL_FOCUS (wxListMainWindow::OnKillFocus)
1606 EVT_SCROLLWIN (wxListMainWindow::OnScroll)
1607 END_EVENT_TABLE()
1608
1609 wxListMainWindow::wxListMainWindow()
1610 {
1611 m_mode = 0;
1612 m_columns.DeleteContents( TRUE );
1613 m_current = (wxListLineData *) NULL;
1614 m_visibleLines = 0;
1615 m_hilightBrush = (wxBrush *) NULL;
1616 m_xScroll = 0;
1617 m_yScroll = 0;
1618 m_dirty = TRUE;
1619 m_small_image_list = (wxImageList *) NULL;
1620 m_normal_image_list = (wxImageList *) NULL;
1621 m_small_spacing = 30;
1622 m_normal_spacing = 40;
1623 m_hasFocus = FALSE;
1624 m_usedKeys = TRUE;
1625 m_lastOnSame = FALSE;
1626 m_renameTimer = new wxListRenameTimer( this );
1627 m_isCreated = FALSE;
1628 m_dragCount = 0;
1629
1630 m_lineLastClicked =
1631 m_lineBeforeLastClicked = (wxListLineData *)NULL;
1632 }
1633
1634 wxListMainWindow::wxListMainWindow( wxWindow *parent, wxWindowID id,
1635 const wxPoint &pos, const wxSize &size,
1636 long style, const wxString &name ) :
1637 wxScrolledWindow( parent, id, pos, size, style|wxHSCROLL|wxVSCROLL, name )
1638 {
1639 m_mode = style;
1640 m_columns.DeleteContents( TRUE );
1641 m_current = (wxListLineData *) NULL;
1642 m_dirty = TRUE;
1643 m_visibleLines = 0;
1644 m_hilightBrush = new wxBrush( wxSystemSettings::GetSystemColour(wxSYS_COLOUR_HIGHLIGHT), wxSOLID );
1645 m_small_image_list = (wxImageList *) NULL;
1646 m_normal_image_list = (wxImageList *) NULL;
1647 m_small_spacing = 30;
1648 m_normal_spacing = 40;
1649 m_hasFocus = FALSE;
1650 m_dragCount = 0;
1651 m_isCreated = FALSE;
1652 wxSize sz = size;
1653 sz.y = 25;
1654
1655 if (m_mode & wxLC_REPORT)
1656 {
1657 #if wxUSE_GENERIC_LIST_EXTENSIONS
1658 m_xScroll = 15;
1659 #else
1660 m_xScroll = 0;
1661 #endif
1662 m_yScroll = 15;
1663 }
1664 else
1665 {
1666 m_xScroll = 15;
1667 m_yScroll = 0;
1668 }
1669 SetScrollbars( m_xScroll, m_yScroll, 0, 0, 0, 0 );
1670
1671 m_usedKeys = TRUE;
1672 m_lastOnSame = FALSE;
1673 m_renameTimer = new wxListRenameTimer( this );
1674 m_renameAccept = FALSE;
1675
1676 SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) );
1677 }
1678
1679 wxListMainWindow::~wxListMainWindow()
1680 {
1681 DeleteEverything();
1682
1683 if (m_hilightBrush) delete m_hilightBrush;
1684
1685 delete m_renameTimer;
1686 }
1687
1688 void wxListMainWindow::RefreshLine( wxListLineData *line )
1689 {
1690 if (m_dirty) return;
1691
1692 if (!line) return;
1693
1694 int x = 0;
1695 int y = 0;
1696 int w = 0;
1697 int h = 0;
1698 line->GetExtent( x, y, w, h );
1699 CalcScrolledPosition( x, y, &x, &y );
1700 wxRect rect( x, y, w, h );
1701 Refresh( TRUE, &rect );
1702 }
1703
1704 void wxListMainWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
1705 {
1706 // Note: a wxPaintDC must be constructed even if no drawing is
1707 // done (a Windows requirement).
1708 wxPaintDC dc( this );
1709
1710 if (m_dirty)
1711 {
1712 // postpone redrawing until the next OnIdle() call to minimize flicker
1713 return;
1714 }
1715
1716 PrepareDC( dc );
1717
1718 int dev_x, dev_y;
1719 CalcScrolledPosition( 0, 0, &dev_x, &dev_y );
1720
1721 if (m_lines.GetCount() == 0)
1722 {
1723 // empty control. nothing to draw
1724 return;
1725 }
1726
1727 dc.BeginDrawing();
1728
1729 dc.SetFont( GetFont() );
1730
1731 if (m_mode & wxLC_REPORT)
1732 {
1733 wxPen pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
1734 wxSize clientSize = GetClientSize();
1735
1736 int lineSpacing = 0;
1737 wxListLineData *line = &m_lines[0];
1738 int dummy = 0;
1739 line->GetSize( dummy, lineSpacing );
1740 lineSpacing += 1;
1741
1742 int y_s = m_yScroll*GetScrollPos( wxVERTICAL );
1743
1744 size_t i_to = y_s / lineSpacing + m_visibleLines+2;
1745 if (i_to >= m_lines.GetCount()) i_to = m_lines.GetCount();
1746 size_t i;
1747 for (i = y_s / lineSpacing; i < i_to; i++)
1748 {
1749 m_lines[i].Draw( &dc );
1750 // Draw horizontal rule if required
1751 if (m_mode & wxLC_HRULES)
1752 {
1753 dc.SetPen(pen);
1754 dc.SetBrush(* wxTRANSPARENT_BRUSH);
1755 dc.DrawLine(0 - dev_x , i*lineSpacing , clientSize.x - dev_x , i*lineSpacing );
1756 }
1757 }
1758
1759 // Draw last horizontal rule
1760 if ((i > (size_t) (y_s / lineSpacing)) && (m_mode & wxLC_HRULES))
1761 {
1762 dc.SetPen(pen);
1763 dc.SetBrush(* wxTRANSPARENT_BRUSH);
1764 dc.DrawLine(0 - dev_x , i*lineSpacing , clientSize.x - dev_x , i*lineSpacing );
1765 }
1766
1767 // Draw vertical rules if required
1768 if ((m_mode & wxLC_VRULES) && (GetItemCount() > 0))
1769 {
1770 int col = 0;
1771 wxRect firstItemRect;
1772 wxRect lastItemRect;
1773 GetItemRect(0, firstItemRect);
1774 GetItemRect(GetItemCount() - 1, lastItemRect);
1775 int x = firstItemRect.GetX();
1776 dc.SetPen(pen);
1777 dc.SetBrush(* wxTRANSPARENT_BRUSH);
1778 for (col = 0; col < GetColumnCount(); col++)
1779 {
1780 int colWidth = GetColumnWidth(col);
1781 x += colWidth ;
1782 dc.DrawLine(x - dev_x, firstItemRect.GetY() - 1 - dev_y, x - dev_x, lastItemRect.GetBottom() + 1 - dev_y);
1783 }
1784 }
1785 }
1786 else // !report mode
1787 {
1788 for (size_t i = 0; i < m_lines.GetCount(); i++)
1789 m_lines[i].Draw( &dc );
1790 }
1791
1792 if (m_current)
1793 m_current->DrawRubberBand( &dc, m_hasFocus );
1794
1795 dc.EndDrawing();
1796 }
1797
1798 void wxListMainWindow::HilightAll( bool on )
1799 {
1800 for (size_t i = 0; i < m_lines.GetCount(); i++)
1801 {
1802 wxListLineData *line = &m_lines[i];
1803 if (line->IsHilighted() != on)
1804 {
1805 line->Hilight( on );
1806 RefreshLine( line );
1807 }
1808 }
1809 }
1810
1811 void wxListMainWindow::SendNotify( wxListLineData *line,
1812 wxEventType command,
1813 wxPoint point )
1814 {
1815 wxListEvent le( command, GetParent()->GetId() );
1816 le.SetEventObject( GetParent() );
1817 le.m_itemIndex = GetIndexOfLine( line );
1818
1819 // set only for events which have position
1820 if ( point != wxDefaultPosition )
1821 le.m_pointDrag = point;
1822
1823 line->GetItem( 0, le.m_item );
1824 GetParent()->GetEventHandler()->ProcessEvent( le );
1825 // GetParent()->GetEventHandler()->AddPendingEvent( le );
1826 }
1827
1828 void wxListMainWindow::FocusLine( wxListLineData *WXUNUSED(line) )
1829 {
1830 // SendNotify( line, wxEVT_COMMAND_LIST_ITEM_FOCUSSED );
1831 }
1832
1833 void wxListMainWindow::UnfocusLine( wxListLineData *WXUNUSED(line) )
1834 {
1835 // SendNotify( line, wxEVT_COMMAND_LIST_ITEM_UNFOCUSSED );
1836 }
1837
1838 void wxListMainWindow::SelectLine( wxListLineData *line )
1839 {
1840 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_SELECTED );
1841 }
1842
1843 void wxListMainWindow::DeselectLine( wxListLineData *line )
1844 {
1845 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_DESELECTED );
1846 }
1847
1848 void wxListMainWindow::DeleteLine( wxListLineData *line )
1849 {
1850 SendNotify( line, wxEVT_COMMAND_LIST_DELETE_ITEM );
1851 }
1852
1853 /* *** */
1854
1855 void wxListMainWindow::EditLabel( long item )
1856 {
1857 wxCHECK_RET( ((size_t)item < m_lines.GetCount()),
1858 wxT("wrong index in wxListCtrl::Edit()") );
1859
1860 m_currentEdit = &m_lines[(size_t)item];
1861
1862 wxListEvent le( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT, GetParent()->GetId() );
1863 le.SetEventObject( GetParent() );
1864 le.m_itemIndex = GetIndexOfLine( m_currentEdit );
1865 m_currentEdit->GetItem( 0, le.m_item );
1866 GetParent()->GetEventHandler()->ProcessEvent( le );
1867
1868 if (!le.IsAllowed())
1869 return;
1870
1871 // We have to call this here because the label in
1872 // question might just have been added and no screen
1873 // update taken place.
1874 if (m_dirty) wxYield();
1875
1876 wxString s = m_currentEdit->GetText(0);
1877 int x = 0;
1878 int y = 0;
1879 int w = 0;
1880 int h = 0;
1881 m_currentEdit->GetLabelExtent( x, y, w, h );
1882
1883 wxClientDC dc(this);
1884 PrepareDC( dc );
1885 x = dc.LogicalToDeviceX( x );
1886 y = dc.LogicalToDeviceY( y );
1887
1888 wxListTextCtrl *text = new wxListTextCtrl(
1889 this, -1, &m_renameAccept, &m_renameRes, this, s, wxPoint(x-4,y-4), wxSize(w+11,h+8) );
1890 text->SetFocus();
1891 }
1892
1893 void wxListMainWindow::OnRenameTimer()
1894 {
1895 wxCHECK_RET( m_current, wxT("invalid m_current") );
1896
1897 Edit( m_lines.Index( *m_current ) );
1898 }
1899
1900 void wxListMainWindow::OnRenameAccept()
1901 {
1902 wxListEvent le( wxEVT_COMMAND_LIST_END_LABEL_EDIT, GetParent()->GetId() );
1903 le.SetEventObject( GetParent() );
1904 le.m_itemIndex = GetIndexOfLine( m_currentEdit );
1905 m_currentEdit->GetItem( 0, le.m_item );
1906 le.m_item.m_text = m_renameRes;
1907 GetParent()->GetEventHandler()->ProcessEvent( le );
1908
1909 if (!le.IsAllowed()) return;
1910
1911 wxListItem info;
1912 info.m_mask = wxLIST_MASK_TEXT;
1913 info.m_itemId = le.m_itemIndex;
1914 info.m_text = m_renameRes;
1915 info.SetTextColour(le.m_item.GetTextColour());
1916 SetItem( info );
1917 }
1918
1919 void wxListMainWindow::OnMouse( wxMouseEvent &event )
1920 {
1921 event.SetEventObject( GetParent() );
1922 if (GetParent()->GetEventHandler()->ProcessEvent( event)) return;
1923
1924 if (!m_current) return;
1925 if (m_dirty) return;
1926 if ( !(event.Dragging() || event.ButtonDown() || event.LeftUp() || event.ButtonDClick()) ) return;
1927
1928 int x = event.GetX();
1929 int y = event.GetY();
1930 CalcUnscrolledPosition( x, y, &x, &y );
1931
1932 /* Did we actually hit an item ? */
1933 long hitResult = 0;
1934 wxListLineData *line = (wxListLineData *) NULL;
1935 for (size_t i = 0; i < m_lines.GetCount(); i++)
1936 {
1937 line = &m_lines[i];
1938 hitResult = line->IsHit( x, y );
1939 if (hitResult) break;
1940 line = (wxListLineData *) NULL;
1941 }
1942
1943 if (event.Dragging())
1944 {
1945 if (m_dragCount == 0)
1946 m_dragStart = wxPoint(x,y);
1947
1948 m_dragCount++;
1949
1950 if (m_dragCount != 3) return;
1951
1952 int command = event.RightIsDown() ? wxEVT_COMMAND_LIST_BEGIN_RDRAG
1953 : wxEVT_COMMAND_LIST_BEGIN_DRAG;
1954
1955 wxListEvent le( command, GetParent()->GetId() );
1956 le.SetEventObject( GetParent() );
1957 le.m_pointDrag = m_dragStart;
1958 GetParent()->GetEventHandler()->ProcessEvent( le );
1959
1960 return;
1961 }
1962 else
1963 {
1964 m_dragCount = 0;
1965 }
1966
1967 if (!line) return;
1968
1969 bool forceClick = FALSE;
1970 if (event.ButtonDClick())
1971 {
1972 m_renameTimer->Stop();
1973 m_lastOnSame = FALSE;
1974
1975 if ( line == m_lineBeforeLastClicked )
1976 {
1977 m_usedKeys = FALSE;
1978
1979 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_ACTIVATED );
1980
1981 return;
1982 }
1983 else
1984 {
1985 // the first click was on another item, so don't interpret this as
1986 // a double click, but as a simple click instead
1987 forceClick = TRUE;
1988 }
1989 }
1990
1991 if (event.LeftUp() && m_lastOnSame)
1992 {
1993 m_usedKeys = FALSE;
1994 if ((line == m_current) &&
1995 (hitResult == wxLIST_HITTEST_ONITEMLABEL) &&
1996 (m_mode & wxLC_EDIT_LABELS) )
1997 {
1998 m_renameTimer->Start( 100, TRUE );
1999 }
2000 m_lastOnSame = FALSE;
2001 return;
2002 }
2003
2004 if (event.RightDown())
2005 {
2006 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_RIGHT_CLICK,
2007 event.GetPosition() );
2008 return;
2009 }
2010
2011 if (event.MiddleDown())
2012 {
2013 SendNotify( line, wxEVT_COMMAND_LIST_ITEM_MIDDLE_CLICK );
2014 return;
2015 }
2016
2017 if ( event.LeftDown() || forceClick )
2018 {
2019 m_lineBeforeLastClicked = m_lineLastClicked;
2020 m_lineLastClicked = line;
2021
2022 m_usedKeys = FALSE;
2023 wxListLineData *oldCurrent = m_current;
2024 if (m_mode & wxLC_SINGLE_SEL)
2025 {
2026 m_current = line;
2027 HilightAll( FALSE );
2028 m_current->ReverseHilight();
2029 RefreshLine( m_current );
2030 }
2031 else
2032 {
2033 if (event.ControlDown())
2034 {
2035 m_current = line;
2036 m_current->ReverseHilight();
2037 RefreshLine( m_current );
2038 }
2039 else if (event.ShiftDown())
2040 {
2041 size_t j;
2042
2043 m_current = line;
2044
2045 int numOfCurrent = -1;
2046 for (j = 0; j < m_lines.GetCount(); j++)
2047 {
2048 wxListLineData *test_line = &m_lines[j];
2049 numOfCurrent++;
2050 if (test_line == oldCurrent) break;
2051 }
2052
2053 int numOfLine = -1;
2054
2055 for (j = 0; j < m_lines.GetCount(); j++)
2056 {
2057 wxListLineData *test_line = &m_lines[j];
2058 numOfLine++;
2059 if (test_line == line) break;
2060 }
2061
2062 if (numOfLine < numOfCurrent)
2063 {
2064 int i = numOfLine;
2065 numOfLine = numOfCurrent;
2066 numOfCurrent = i;
2067 }
2068
2069 for (int i = 0; i <= numOfLine-numOfCurrent; i++)
2070 {
2071 wxListLineData *test_line= &m_lines[numOfCurrent + i];
2072 test_line->Hilight(TRUE);
2073 RefreshLine( test_line );
2074 }
2075 }
2076 else
2077 {
2078 m_current = line;
2079 HilightAll( FALSE );
2080 m_current->ReverseHilight();
2081 RefreshLine( m_current );
2082 }
2083 }
2084 if (m_current != oldCurrent)
2085 {
2086 RefreshLine( oldCurrent );
2087 UnfocusLine( oldCurrent );
2088 FocusLine( m_current );
2089 }
2090
2091 // forceClick is only set if the previous click was on another item
2092 m_lastOnSame = !forceClick && (m_current == oldCurrent);
2093
2094 return;
2095 }
2096 }
2097
2098 void wxListMainWindow::MoveToFocus()
2099 {
2100 if (!m_current) return;
2101
2102 int item_x = 0;
2103 int item_y = 0;
2104 int item_w = 0;
2105 int item_h = 0;
2106 m_current->GetExtent( item_x, item_y, item_w, item_h );
2107
2108 int client_w = 0;
2109 int client_h = 0;
2110 GetClientSize( &client_w, &client_h );
2111
2112 int view_x = m_xScroll*GetScrollPos( wxHORIZONTAL );
2113 int view_y = m_yScroll*GetScrollPos( wxVERTICAL );
2114
2115 if (m_mode & wxLC_REPORT)
2116 {
2117 if (item_y < view_y )
2118 Scroll( -1, (item_y)/m_yScroll );
2119 if (item_y+item_h+5 > view_y+client_h)
2120 Scroll( -1, (item_y+item_h-client_h+15)/m_yScroll );
2121 }
2122 else
2123 {
2124 if (item_x-view_x < 5)
2125 Scroll( (item_x-5)/m_xScroll, -1 );
2126 if (item_x+item_w-5 > view_x+client_w)
2127 Scroll( (item_x+item_w-client_w+15)/m_xScroll, -1 );
2128 }
2129 }
2130
2131 void wxListMainWindow::OnArrowChar( wxListLineData *newCurrent, bool shiftDown )
2132 {
2133 if ((m_mode & wxLC_SINGLE_SEL) || (m_usedKeys == FALSE)) m_current->Hilight( FALSE );
2134 wxListLineData *oldCurrent = m_current;
2135 m_current = newCurrent;
2136 if (shiftDown || (m_mode & wxLC_SINGLE_SEL)) m_current->Hilight( TRUE );
2137 RefreshLine( m_current );
2138 RefreshLine( oldCurrent );
2139 FocusLine( m_current );
2140 UnfocusLine( oldCurrent );
2141 MoveToFocus();
2142 }
2143
2144 void wxListMainWindow::OnKeyDown( wxKeyEvent &event )
2145 {
2146 wxWindow *parent = GetParent();
2147
2148 /* we propagate the key event up */
2149 wxKeyEvent ke( wxEVT_KEY_DOWN );
2150 ke.m_shiftDown = event.m_shiftDown;
2151 ke.m_controlDown = event.m_controlDown;
2152 ke.m_altDown = event.m_altDown;
2153 ke.m_metaDown = event.m_metaDown;
2154 ke.m_keyCode = event.m_keyCode;
2155 ke.m_x = event.m_x;
2156 ke.m_y = event.m_y;
2157 ke.SetEventObject( parent );
2158 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
2159
2160 event.Skip();
2161 }
2162
2163 void wxListMainWindow::OnChar( wxKeyEvent &event )
2164 {
2165 wxWindow *parent = GetParent();
2166
2167 /* we send a list_key event up */
2168 if ( m_current )
2169 {
2170 wxListEvent le( wxEVT_COMMAND_LIST_KEY_DOWN, GetParent()->GetId() );
2171 le.m_itemIndex = GetIndexOfLine( m_current );
2172 m_current->GetItem( 0, le.m_item );
2173 le.m_code = (int)event.KeyCode();
2174 le.SetEventObject( parent );
2175 parent->GetEventHandler()->ProcessEvent( le );
2176 }
2177
2178 /* we propagate the char event up */
2179 wxKeyEvent ke( wxEVT_CHAR );
2180 ke.m_shiftDown = event.m_shiftDown;
2181 ke.m_controlDown = event.m_controlDown;
2182 ke.m_altDown = event.m_altDown;
2183 ke.m_metaDown = event.m_metaDown;
2184 ke.m_keyCode = event.m_keyCode;
2185 ke.m_x = event.m_x;
2186 ke.m_y = event.m_y;
2187 ke.SetEventObject( parent );
2188 if (parent->GetEventHandler()->ProcessEvent( ke )) return;
2189
2190 if (event.KeyCode() == WXK_TAB)
2191 {
2192 wxNavigationKeyEvent nevent;
2193 nevent.SetWindowChange( event.ControlDown() );
2194 nevent.SetDirection( !event.ShiftDown() );
2195 nevent.SetEventObject( GetParent()->GetParent() );
2196 nevent.SetCurrentFocus( m_parent );
2197 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent )) return;
2198 }
2199
2200 /* no item -> nothing to do */
2201 if (!m_current)
2202 {
2203 event.Skip();
2204 return;
2205 }
2206
2207 switch (event.KeyCode())
2208 {
2209 case WXK_UP:
2210 {
2211 int index = m_lines.Index(*m_current);
2212 if (index != wxNOT_FOUND && index > 0)
2213 OnArrowChar( &m_lines[index-1], event.ShiftDown() );
2214 break;
2215 }
2216 case WXK_DOWN:
2217 {
2218 int index = m_lines.Index(*m_current);
2219 if (index != wxNOT_FOUND && (size_t)index < m_lines.GetCount()-1)
2220 OnArrowChar( &m_lines[index+1], event.ShiftDown() );
2221 break;
2222 }
2223 case WXK_END:
2224 {
2225 if (!m_lines.IsEmpty())
2226 OnArrowChar( &m_lines.Last(), event.ShiftDown() );
2227 break;
2228 }
2229 case WXK_HOME:
2230 {
2231 if (!m_lines.IsEmpty())
2232 OnArrowChar( &m_lines[0], event.ShiftDown() );
2233 break;
2234 }
2235 case WXK_PRIOR:
2236 {
2237 int steps = 0;
2238 int index = m_lines.Index(*m_current);
2239 if (m_mode & wxLC_REPORT)
2240 {
2241 steps = m_visibleLines-1;
2242 }
2243 else
2244 {
2245 steps = index % m_visibleLines;
2246 }
2247 if (index != wxNOT_FOUND)
2248 {
2249 index -= steps;
2250 if (index < 0) index = 0;
2251 OnArrowChar( &m_lines[index], event.ShiftDown() );
2252 }
2253 break;
2254 }
2255 case WXK_NEXT:
2256 {
2257 int steps = 0;
2258 int index = m_lines.Index(*m_current);
2259 if (m_mode & wxLC_REPORT)
2260 {
2261 steps = m_visibleLines-1;
2262 }
2263 else
2264 {
2265 steps = m_visibleLines-(index % m_visibleLines)-1;
2266 }
2267
2268 if (index != wxNOT_FOUND)
2269 {
2270 index += steps;
2271 if ((size_t)index >= m_lines.GetCount())
2272 index = m_lines.GetCount()-1;
2273 OnArrowChar( &m_lines[index], event.ShiftDown() );
2274 }
2275 break;
2276 }
2277 case WXK_LEFT:
2278 {
2279 if (!(m_mode & wxLC_REPORT))
2280 {
2281 int index = m_lines.Index(*m_current);
2282 if (index != wxNOT_FOUND)
2283 {
2284 index -= m_visibleLines;
2285 if (index < 0) index = 0;
2286 OnArrowChar( &m_lines[index], event.ShiftDown() );
2287 }
2288 }
2289 break;
2290 }
2291 case WXK_RIGHT:
2292 {
2293 if (!(m_mode & wxLC_REPORT))
2294 {
2295 int index = m_lines.Index(*m_current);
2296 if (index != wxNOT_FOUND)
2297 {
2298 index += m_visibleLines;
2299 if ((size_t)index >= m_lines.GetCount())
2300 index = m_lines.GetCount()-1;
2301 OnArrowChar( &m_lines[index], event.ShiftDown() );
2302 }
2303 }
2304 break;
2305 }
2306 case WXK_SPACE:
2307 {
2308 if (m_mode & wxLC_SINGLE_SEL)
2309 {
2310 wxListEvent le( wxEVT_COMMAND_LIST_ITEM_ACTIVATED, GetParent()->GetId() );
2311 le.SetEventObject( GetParent() );
2312 le.m_itemIndex = GetIndexOfLine( m_current );
2313 m_current->GetItem( 0, le.m_item );
2314 GetParent()->GetEventHandler()->ProcessEvent( le );
2315 }
2316 else
2317 {
2318 m_current->ReverseHilight();
2319 RefreshLine( m_current );
2320 }
2321 break;
2322 }
2323 case WXK_INSERT:
2324 {
2325 if (!(m_mode & wxLC_SINGLE_SEL))
2326 {
2327 wxListLineData *oldCurrent = m_current;
2328 m_current->ReverseHilight();
2329 int index = m_lines.Index( *m_current ) + 1;
2330 if ( (size_t)index < m_lines.GetCount() )
2331 m_current = &m_lines[index];
2332 RefreshLine( oldCurrent );
2333 RefreshLine( m_current );
2334 UnfocusLine( oldCurrent );
2335 FocusLine( m_current );
2336 MoveToFocus();
2337 }
2338 break;
2339 }
2340 case WXK_RETURN:
2341 case WXK_EXECUTE:
2342 {
2343 wxListEvent le( wxEVT_COMMAND_LIST_ITEM_ACTIVATED, GetParent()->GetId() );
2344 le.SetEventObject( GetParent() );
2345 le.m_itemIndex = GetIndexOfLine( m_current );
2346 m_current->GetItem( 0, le.m_item );
2347 GetParent()->GetEventHandler()->ProcessEvent( le );
2348 break;
2349 }
2350 default:
2351 {
2352 event.Skip();
2353 return;
2354 }
2355 }
2356 m_usedKeys = TRUE;
2357 }
2358
2359 #ifdef __WXGTK__
2360 extern wxWindow *g_focusWindow;
2361 #endif
2362
2363 void wxListMainWindow::OnSetFocus( wxFocusEvent &WXUNUSED(event) )
2364 {
2365 m_hasFocus = TRUE;
2366 RefreshLine( m_current );
2367
2368 if (!GetParent()) return;
2369
2370 #ifdef __WXGTK__
2371 g_focusWindow = GetParent();
2372 #endif
2373
2374 wxFocusEvent event( wxEVT_SET_FOCUS, GetParent()->GetId() );
2375 event.SetEventObject( GetParent() );
2376 GetParent()->GetEventHandler()->ProcessEvent( event );
2377 }
2378
2379 void wxListMainWindow::OnKillFocus( wxFocusEvent &WXUNUSED(event) )
2380 {
2381 m_hasFocus = FALSE;
2382 RefreshLine( m_current );
2383 }
2384
2385 void wxListMainWindow::OnSize( wxSizeEvent &WXUNUSED(event) )
2386 {
2387 /*
2388 We don't even allow the wxScrolledWindow::AdjustScrollbars() call
2389
2390 */
2391 m_dirty = TRUE;
2392 }
2393
2394 void wxListMainWindow::DrawImage( int index, wxDC *dc, int x, int y )
2395 {
2396 if ((m_mode & wxLC_ICON) && (m_normal_image_list))
2397 {
2398 m_normal_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2399 return;
2400 }
2401 if ((m_mode & wxLC_SMALL_ICON) && (m_small_image_list))
2402 {
2403 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2404 }
2405 if ((m_mode & wxLC_LIST) && (m_small_image_list))
2406 {
2407 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2408 }
2409 if ((m_mode & wxLC_REPORT) && (m_small_image_list))
2410 {
2411 m_small_image_list->Draw( index, *dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
2412 return;
2413 }
2414 }
2415
2416 void wxListMainWindow::GetImageSize( int index, int &width, int &height )
2417 {
2418 if ((m_mode & wxLC_ICON) && (m_normal_image_list))
2419 {
2420 m_normal_image_list->GetSize( index, width, height );
2421 return;
2422 }
2423 if ((m_mode & wxLC_SMALL_ICON) && (m_small_image_list))
2424 {
2425 m_small_image_list->GetSize( index, width, height );
2426 return;
2427 }
2428 if ((m_mode & wxLC_LIST) && (m_small_image_list))
2429 {
2430 m_small_image_list->GetSize( index, width, height );
2431 return;
2432 }
2433 if ((m_mode & wxLC_REPORT) && (m_small_image_list))
2434 {
2435 m_small_image_list->GetSize( index, width, height );
2436 return;
2437 }
2438 width = 0;
2439 height = 0;
2440 }
2441
2442 int wxListMainWindow::GetTextLength( wxString &s )
2443 {
2444 wxClientDC dc( this );
2445 wxCoord lw = 0;
2446 wxCoord lh = 0;
2447 dc.GetTextExtent( s, &lw, &lh );
2448 return lw + 6;
2449 }
2450
2451 int wxListMainWindow::GetIndexOfLine( const wxListLineData *line )
2452 {
2453 int i = m_lines.Index(*line);
2454 if (i == wxNOT_FOUND) return -1;
2455 else return i;
2456 }
2457
2458 void wxListMainWindow::SetImageList( wxImageList *imageList, int which )
2459 {
2460 m_dirty = TRUE;
2461
2462 // calc the spacing from the icon size
2463 int width = 0,
2464 height = 0;
2465 if ((imageList) && (imageList->GetImageCount()) )
2466 {
2467 imageList->GetSize(0, width, height);
2468 }
2469
2470 if (which == wxIMAGE_LIST_NORMAL)
2471 {
2472 m_normal_image_list = imageList;
2473 m_normal_spacing = width + 8;
2474 }
2475
2476 if (which == wxIMAGE_LIST_SMALL)
2477 {
2478 m_small_image_list = imageList;
2479 m_small_spacing = width + 14;
2480 }
2481 }
2482
2483 void wxListMainWindow::SetItemSpacing( int spacing, bool isSmall )
2484 {
2485 m_dirty = TRUE;
2486 if (isSmall)
2487 {
2488 m_small_spacing = spacing;
2489 }
2490 else
2491 {
2492 m_normal_spacing = spacing;
2493 }
2494 }
2495
2496 int wxListMainWindow::GetItemSpacing( bool isSmall )
2497 {
2498 return isSmall ? m_small_spacing : m_normal_spacing;
2499 }
2500
2501 void wxListMainWindow::SetColumn( int col, wxListItem &item )
2502 {
2503 m_dirty = TRUE;
2504 wxNode *node = m_columns.Nth( col );
2505 if (node)
2506 {
2507 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
2508 item.m_width = GetTextLength( item.m_text )+7;
2509 wxListHeaderData *column = (wxListHeaderData*)node->Data();
2510 column->SetItem( item );
2511 }
2512
2513 wxListHeaderWindow *headerWin = ((wxListCtrl*) GetParent())->m_headerWin;
2514 if ( headerWin )
2515 headerWin->m_dirty = TRUE;
2516 }
2517
2518 void wxListMainWindow::SetColumnWidth( int col, int width )
2519 {
2520 wxCHECK_RET( m_mode & wxLC_REPORT,
2521 _T("SetColumnWidth() can only be called in report mode.") );
2522
2523 m_dirty = TRUE;
2524
2525 wxNode *node = (wxNode*) NULL;
2526
2527 if (width == wxLIST_AUTOSIZE_USEHEADER)
2528 {
2529 // TODO do use the header
2530 width = 80;
2531 }
2532 else if (width == wxLIST_AUTOSIZE)
2533 {
2534 wxClientDC dc(this);
2535 dc.SetFont( GetFont() );
2536 int max = 10;
2537
2538 for (size_t i = 0; i < m_lines.GetCount(); i++)
2539 {
2540 wxListLineData *line = &m_lines[i];
2541 wxListItemDataList::Node *n = line->m_items.Item( col );
2542 if (n)
2543 {
2544 wxListItemData *item = n->GetData();
2545 int current = 0, ix = 0, iy = 0;
2546 wxCoord lx = 0, ly = 0;
2547 if (item->HasImage())
2548 {
2549 GetImageSize( item->GetImage(), ix, iy );
2550 current = ix + 5;
2551 }
2552 if (item->HasText())
2553 {
2554 wxString str = item->GetText();
2555 dc.GetTextExtent( str, &lx, &ly );
2556 current += lx;
2557 }
2558 if (current > max)
2559 max = current;
2560 }
2561 }
2562 width = max+10;
2563 }
2564
2565 node = m_columns.Nth( col );
2566 if (node)
2567 {
2568 wxListHeaderData *column = (wxListHeaderData*)node->Data();
2569 column->SetWidth( width );
2570 }
2571
2572 size_t count = m_lines.GetCount();
2573 for (size_t i = 0; i < count; i++)
2574 {
2575 wxListLineData *line = &m_lines[i];
2576 wxListItemDataList::Node *n = line->m_items.Item( col );
2577 if (n)
2578 {
2579 wxListItemData *item = n->GetData();
2580 item->SetSize( width, -1 );
2581 }
2582 }
2583
2584 wxListHeaderWindow *headerWin = ((wxListCtrl*) GetParent())->m_headerWin;
2585 if ( headerWin )
2586 headerWin->m_dirty = TRUE;
2587 }
2588
2589 void wxListMainWindow::GetColumn( int col, wxListItem &item )
2590 {
2591 wxNode *node = m_columns.Nth( col );
2592 if (node)
2593 {
2594 wxListHeaderData *column = (wxListHeaderData*)node->Data();
2595 column->GetItem( item );
2596 }
2597 else
2598 {
2599 item.m_format = 0;
2600 item.m_width = 0;
2601 item.m_text = _T("");
2602 item.m_image = 0;
2603 item.m_data = 0;
2604 }
2605 }
2606
2607 int wxListMainWindow::GetColumnWidth( int col )
2608 {
2609 wxNode *node = m_columns.Nth( col );
2610 if (node)
2611 {
2612 wxListHeaderData *column = (wxListHeaderData*)node->Data();
2613 return column->GetWidth();
2614 }
2615 else
2616 {
2617 return 0;
2618 }
2619 }
2620
2621 int wxListMainWindow::GetColumnCount()
2622 {
2623 return m_columns.Number();
2624 }
2625
2626 int wxListMainWindow::GetCountPerPage()
2627 {
2628 return m_visibleLines;
2629 }
2630
2631 void wxListMainWindow::SetItem( wxListItem &item )
2632 {
2633 m_dirty = TRUE;
2634 if (item.m_itemId >= 0 && (size_t)item.m_itemId < m_lines.GetCount())
2635 {
2636 wxListLineData *line = &m_lines[(size_t)item.m_itemId];
2637 if (m_mode & wxLC_REPORT) item.m_width = GetColumnWidth( item.m_col )-3;
2638 line->SetItem( item.m_col, item );
2639 }
2640 }
2641
2642 void wxListMainWindow::SetItemState( long item, long state, long stateMask )
2643 {
2644 wxCHECK_RET( item >= 0 && (size_t)item < m_lines.GetCount(),
2645 _T("invalid list ctrl item index in SetItem") );
2646
2647 // m_dirty = TRUE; no recalcs needed
2648
2649 wxListLineData *oldCurrent = m_current;
2650
2651 if ( stateMask & wxLIST_STATE_FOCUSED )
2652 {
2653 wxListLineData *line = &m_lines[(size_t)item];
2654 if ( state & wxLIST_STATE_FOCUSED )
2655 {
2656 // don't do anything if this item is already focused
2657 if ( line != m_current )
2658 {
2659 UnfocusLine( m_current );
2660 m_current = line;
2661 FocusLine( m_current );
2662 if ( (m_mode & wxLC_SINGLE_SEL) && oldCurrent )
2663 oldCurrent->Hilight( FALSE );
2664
2665 RefreshLine( m_current );
2666 if ( oldCurrent )
2667 RefreshLine( oldCurrent );
2668 }
2669 }
2670 else // unfocus
2671 {
2672 // don't do anything if this item is not focused
2673 if ( line == m_current )
2674 {
2675 UnfocusLine( m_current );
2676 m_current = NULL;
2677 }
2678 }
2679 }
2680
2681 if ( stateMask & wxLIST_STATE_SELECTED )
2682 {
2683 bool on = (state & wxLIST_STATE_SELECTED) != 0;
2684 if (!on && (m_mode & wxLC_SINGLE_SEL))
2685 return;
2686
2687 wxListLineData *line = &m_lines[(size_t)item];
2688 if (m_mode & wxLC_SINGLE_SEL)
2689 {
2690 UnfocusLine( m_current );
2691 m_current = line;
2692 FocusLine( m_current );
2693 if (oldCurrent)
2694 oldCurrent->Hilight( FALSE );
2695 RefreshLine( m_current );
2696 if (oldCurrent)
2697 RefreshLine( oldCurrent );
2698 }
2699
2700 if (on != line->IsHilighted())
2701 {
2702 line->Hilight( on );
2703 RefreshLine( line );
2704 }
2705 }
2706 }
2707
2708 int wxListMainWindow::GetItemState( long item, long stateMask )
2709 {
2710 int ret = wxLIST_STATE_DONTCARE;
2711 if (stateMask & wxLIST_STATE_FOCUSED)
2712 {
2713 if (item >= 0 && (size_t)item < m_lines.GetCount())
2714 {
2715 wxListLineData *line = &m_lines[(size_t)item];
2716 if (line == m_current) ret |= wxLIST_STATE_FOCUSED;
2717 }
2718 }
2719 if (stateMask & wxLIST_STATE_SELECTED)
2720 {
2721 if (item >= 0 && (size_t)item < m_lines.GetCount())
2722 {
2723 wxListLineData *line = &m_lines[(size_t)item];
2724 if (line->IsHilighted()) ret |= wxLIST_STATE_SELECTED;
2725 }
2726 }
2727 return ret;
2728 }
2729
2730 void wxListMainWindow::GetItem( wxListItem &item )
2731 {
2732 if (item.m_itemId >= 0 && (size_t)item.m_itemId < m_lines.GetCount())
2733 {
2734 wxListLineData *line = &m_lines[(size_t)item.m_itemId];
2735 line->GetItem( item.m_col, item );
2736 }
2737 else
2738 {
2739 item.m_mask = 0;
2740 item.m_text = _T("");
2741 item.m_image = 0;
2742 item.m_data = 0;
2743 }
2744 }
2745
2746 int wxListMainWindow::GetItemCount()
2747 {
2748 return m_lines.GetCount();
2749 }
2750
2751 void wxListMainWindow::GetItemRect( long index, wxRect &rect )
2752 {
2753 if (index >= 0 && (size_t)index < m_lines.GetCount())
2754 {
2755 m_lines[(size_t)index].GetRect( rect );
2756 this->CalcScrolledPosition(rect.x,rect.y,&rect.x,&rect.y);
2757 }
2758 else
2759 {
2760 rect.x = 0;
2761 rect.y = 0;
2762 rect.width = 0;
2763 rect.height = 0;
2764 }
2765 }
2766
2767 bool wxListMainWindow::GetItemPosition(long item, wxPoint& pos)
2768 {
2769 wxRect rect;
2770 this->GetItemRect(item,rect);
2771 pos.x=rect.x; pos.y=rect.y;
2772 return TRUE;
2773 }
2774
2775 int wxListMainWindow::GetSelectedItemCount()
2776 {
2777 int ret = 0;
2778 for (size_t i = 0; i < m_lines.GetCount(); i++)
2779 {
2780 if (m_lines[i].IsHilighted()) ret++;
2781 }
2782 return ret;
2783 }
2784
2785 void wxListMainWindow::SetMode( long mode )
2786 {
2787 m_dirty = TRUE;
2788 m_mode = mode;
2789
2790 DeleteEverything();
2791
2792 if (m_mode & wxLC_REPORT)
2793 {
2794 #if wxUSE_GENERIC_LIST_EXTENSIONS
2795 m_xScroll = 15;
2796 #else
2797 m_xScroll = 0;
2798 #endif
2799 m_yScroll = 15;
2800 }
2801 else
2802 {
2803 m_xScroll = 15;
2804 m_yScroll = 0;
2805 }
2806 }
2807
2808 long wxListMainWindow::GetMode() const
2809 {
2810 return m_mode;
2811 }
2812
2813 void wxListMainWindow::CalculatePositions()
2814 {
2815 if (m_lines.IsEmpty()) return;
2816
2817 wxClientDC dc( this );
2818 dc.SetFont( GetFont() );
2819
2820 int iconSpacing = 0;
2821 if (m_mode & wxLC_ICON) iconSpacing = m_normal_spacing;
2822 if (m_mode & wxLC_SMALL_ICON) iconSpacing = m_small_spacing;
2823
2824 // we take the first line (which also can be an icon or
2825 // an a text item in wxLC_ICON and wxLC_LIST modes) to
2826 // measure the size of the line
2827
2828 int lineWidth = 0;
2829 int lineHeight = 0;
2830 int lineSpacing = 0;
2831
2832 wxListLineData *line = &m_lines[0];
2833 line->CalculateSize( &dc, iconSpacing );
2834 int dummy = 0;
2835 line->GetSize( dummy, lineSpacing );
2836 lineSpacing += 1;
2837
2838 int clientWidth = 0;
2839 int clientHeight = 0;
2840
2841 if (m_mode & wxLC_REPORT)
2842 {
2843 // scroll one line per step
2844 m_yScroll = lineSpacing;
2845
2846 int x = 4;
2847 int y = 1;
2848 int entireHeight = m_lines.GetCount() * lineSpacing + 2;
2849 int scroll_pos = GetScrollPos( wxVERTICAL );
2850 #if wxUSE_GENERIC_LIST_EXTENSIONS
2851 int x_scroll_pos = GetScrollPos( wxHORIZONTAL );
2852 #else
2853 SetScrollbars( m_xScroll, m_yScroll, 0, entireHeight/m_yScroll +1, 0, scroll_pos, TRUE );
2854 #endif
2855 GetClientSize( &clientWidth, &clientHeight );
2856
2857 int entireWidth = 0 ;
2858 for (size_t j = 0; j < m_lines.GetCount(); j++)
2859 {
2860 wxListLineData *line = &m_lines[j];
2861 line->CalculateSize( &dc, iconSpacing );
2862 line->SetPosition( &dc, x, y, clientWidth );
2863 int col_x = 2;
2864 for (int i = 0; i < GetColumnCount(); i++)
2865 {
2866 line->SetColumnPosition( i, col_x );
2867 col_x += GetColumnWidth( i );
2868 }
2869 entireWidth = wxMax( entireWidth , col_x ) ;
2870 #if wxUSE_GENERIC_LIST_EXTENSIONS
2871 line->SetPosition( &dc, x, y, col_x );
2872 #endif
2873 y += lineSpacing; // one pixel blank line between items
2874 }
2875 m_visibleLines = clientHeight / lineSpacing;
2876 #if wxUSE_GENERIC_LIST_EXTENSIONS
2877 SetScrollbars( m_xScroll, m_yScroll, entireWidth/m_xScroll +1, entireHeight/m_yScroll +1, x_scroll_pos , scroll_pos, TRUE );
2878 #endif
2879 }
2880 else
2881 {
2882 // at first we try without any scrollbar. if the items don't
2883 // fit into the window, we recalculate after subtracting an
2884 // approximated 15 pt for the horizontal scrollbar
2885
2886 GetSize( &clientWidth, &clientHeight );
2887 clientHeight -= 4; // sunken frame
2888
2889 int entireWidth = 0;
2890
2891 for (int tries = 0; tries < 2; tries++)
2892 {
2893 entireWidth = 0;
2894 int x = 2;
2895 int y = 2;
2896 int maxWidth = 0;
2897 m_visibleLines = 0;
2898 int m_currentVisibleLines = 0;
2899 for (size_t i = 0; i < m_lines.GetCount(); i++)
2900 {
2901 m_currentVisibleLines++;
2902 wxListLineData *line = &m_lines[i];
2903 line->CalculateSize( &dc, iconSpacing );
2904 line->SetPosition( &dc, x, y, clientWidth );
2905 line->GetSize( lineWidth, lineHeight );
2906 if (lineWidth > maxWidth) maxWidth = lineWidth;
2907 y += lineSpacing;
2908 if (m_currentVisibleLines > m_visibleLines)
2909 m_visibleLines = m_currentVisibleLines;
2910 if (y+lineSpacing-6 >= clientHeight) // -6 for earlier "line breaking"
2911 {
2912 m_currentVisibleLines = 0;
2913 y = 2;
2914 x += maxWidth+6;
2915 entireWidth += maxWidth+6;
2916 maxWidth = 0;
2917 }
2918 if (i == m_lines.GetCount()-1) entireWidth += maxWidth;
2919 if ((tries == 0) && (entireWidth > clientWidth))
2920 {
2921 clientHeight -= 15; // scrollbar height
2922 m_visibleLines = 0;
2923 m_currentVisibleLines = 0;
2924 break;
2925 }
2926 if (i == m_lines.GetCount()-1) tries = 1; // everything fits, no second try required
2927 }
2928 }
2929
2930 int scroll_pos = GetScrollPos( wxHORIZONTAL );
2931 SetScrollbars( m_xScroll, m_yScroll, (entireWidth+15) / m_xScroll, 0, scroll_pos, 0, TRUE );
2932 }
2933 }
2934
2935 void wxListMainWindow::RealizeChanges()
2936 {
2937 if (!m_current)
2938 {
2939 if (!m_lines.IsEmpty())
2940 m_current = &m_lines[0];
2941 }
2942 if (m_current)
2943 {
2944 FocusLine( m_current );
2945 // TODO: MSW doesn't automatically hilight the
2946 // first item.
2947 // if (m_mode & wxLC_SINGLE_SEL) m_current->Hilight( TRUE );
2948 }
2949 }
2950
2951 long wxListMainWindow::GetNextItem( long item,
2952 int WXUNUSED(geometry),
2953 int state )
2954 {
2955 long ret = item,
2956 max = GetItemCount();
2957 wxCHECK_MSG( (ret == -1) || (ret < max), -1,
2958 _T("invalid listctrl index in GetNextItem()") );
2959
2960 // notice that we start with the next item (or the first one if item == -1)
2961 // and this is intentional to allow writing a simple loop to iterate over
2962 // all selected items
2963 ret++;
2964 if ( ret == max )
2965 {
2966 // this is not an error because the index was ok initially, just no
2967 // such item
2968 return -1;
2969 }
2970
2971 for (size_t i = (size_t)ret; i < m_lines.GetCount(); i++)
2972 {
2973 wxListLineData *line = &m_lines[i];
2974 if ((state & wxLIST_STATE_FOCUSED) && (line == m_current))
2975 return ret;
2976 if ((state & wxLIST_STATE_SELECTED) && (line->IsHilighted()))
2977 return ret;
2978 if (!state)
2979 return ret;
2980 ret++;
2981 }
2982
2983 return -1;
2984 }
2985
2986 void wxListMainWindow::DeleteItem( long index )
2987 {
2988 m_dirty = TRUE;
2989 if (index >= 0 && (size_t)index < m_lines.GetCount())
2990 {
2991 wxListLineData *line = &m_lines[(size_t)index];
2992 if (m_current == line) m_current = (wxListLineData *) NULL;
2993 DeleteLine( line );
2994 m_lines.RemoveAt( (size_t)index );
2995 }
2996 }
2997
2998 void wxListMainWindow::DeleteColumn( int col )
2999 {
3000 wxCHECK_RET( col < (int)m_columns.GetCount(),
3001 wxT("attempting to delete inexistent column in wxListView") );
3002
3003 m_dirty = TRUE;
3004 wxNode *node = m_columns.Nth( col );
3005 if (node) m_columns.DeleteNode( node );
3006 }
3007
3008 void wxListMainWindow::DeleteAllItems()
3009 {
3010 m_dirty = TRUE;
3011 m_current = (wxListLineData *) NULL;
3012
3013 // to make the deletion of all items faster, we don't send the
3014 // notifications in this case: this is compatible with wxMSW and
3015 // documented in DeleteAllItems() description
3016
3017 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetParent()->GetId() );
3018 event.SetEventObject( GetParent() );
3019 GetParent()->GetEventHandler()->ProcessEvent( event );
3020
3021 m_lines.Clear();
3022 }
3023
3024 void wxListMainWindow::DeleteEverything()
3025 {
3026 DeleteAllItems();
3027
3028 m_columns.Clear();
3029 }
3030
3031 void wxListMainWindow::EnsureVisible( long index )
3032 {
3033 // We have to call this here because the label in
3034 // question might just have been added and no screen
3035 // update taken place.
3036 if (m_dirty) wxYield();
3037
3038 wxListLineData *oldCurrent = m_current;
3039 m_current = (wxListLineData *) NULL;
3040 if (index >= 0 && (size_t)index < m_lines.GetCount())
3041 m_current = &m_lines[(size_t)index];
3042 if (m_current) MoveToFocus();
3043 m_current = oldCurrent;
3044 }
3045
3046 long wxListMainWindow::FindItem(long start, const wxString& str, bool WXUNUSED(partial) )
3047 {
3048 long pos = start;
3049 wxString tmp = str;
3050 if (pos < 0) pos = 0;
3051 for (size_t i = (size_t)pos; i < m_lines.GetCount(); i++)
3052 {
3053 wxListLineData *line = &m_lines[i];
3054 wxString s = line->GetText(0);
3055 if (s == tmp)
3056 return pos;
3057
3058 pos++;
3059 }
3060 return -1;
3061 }
3062
3063 long wxListMainWindow::FindItem(long start, long data)
3064 {
3065 long pos = start;
3066 if (pos < 0) pos = 0;
3067 for (size_t i = (size_t)pos; i < m_lines.GetCount(); i++)
3068 {
3069 wxListLineData *line = &m_lines[i];
3070 wxListItem item;
3071 line->GetItem( 0, item );
3072 if (item.m_data == data) return pos;
3073 pos++;
3074 }
3075 return -1;
3076 }
3077
3078 long wxListMainWindow::HitTest( int x, int y, int &flags )
3079 {
3080 CalcUnscrolledPosition( x, y, &x, &y );
3081
3082 int count = 0;
3083 for (size_t i = 0; i < m_lines.GetCount(); i++)
3084 {
3085 wxListLineData *line = &m_lines[i];
3086 long ret = line->IsHit( x, y );
3087 if (ret) // & flags) // No: flags is output-only so may be garbage at this point
3088 {
3089 flags = (int)ret;
3090 return count;
3091 }
3092 count++;
3093 }
3094 return -1;
3095 }
3096
3097 void wxListMainWindow::InsertItem( wxListItem &item )
3098 {
3099 m_dirty = TRUE;
3100 int mode = 0;
3101 if (m_mode & wxLC_REPORT) mode = wxLC_REPORT;
3102 else if (m_mode & wxLC_LIST) mode = wxLC_LIST;
3103 else if (m_mode & wxLC_ICON) mode = wxLC_ICON;
3104 else if (m_mode & wxLC_SMALL_ICON) mode = wxLC_ICON; // no typo
3105
3106 wxListLineData *line = new wxListLineData( this, mode, m_hilightBrush );
3107
3108 if (m_mode & wxLC_REPORT)
3109 {
3110 line->InitItems( GetColumnCount() );
3111 item.m_width = GetColumnWidth( 0 )-3;
3112 }
3113 else
3114 {
3115 line->InitItems( 1 );
3116 }
3117
3118 line->SetItem( 0, item );
3119 if ((item.m_itemId >= 0) && ((size_t)item.m_itemId < m_lines.GetCount()))
3120 {
3121 m_lines.Insert( line, (size_t)item.m_itemId );
3122 }
3123 else
3124 {
3125 m_lines.Add( line );
3126 item.m_itemId = m_lines.GetCount()-1;
3127 }
3128 }
3129
3130 void wxListMainWindow::InsertColumn( long col, wxListItem &item )
3131 {
3132 m_dirty = TRUE;
3133 if (m_mode & wxLC_REPORT)
3134 {
3135 if (item.m_width == wxLIST_AUTOSIZE_USEHEADER)
3136 item.m_width = GetTextLength( item.m_text );
3137 wxListHeaderData *column = new wxListHeaderData( item );
3138 if ((col >= 0) && (col < (int)m_columns.GetCount()))
3139 {
3140 wxNode *node = m_columns.Nth( (size_t)col );
3141 if (node)
3142 m_columns.Insert( node, column );
3143 }
3144 else
3145 {
3146 m_columns.Append( column );
3147 }
3148 }
3149 }
3150
3151 wxListCtrlCompare list_ctrl_compare_func_2;
3152 long list_ctrl_compare_data;
3153
3154 int LINKAGEMODE list_ctrl_compare_func_1( wxListLineData **arg1, wxListLineData **arg2 )
3155 {
3156 wxListLineData *line1 = *arg1;
3157 wxListLineData *line2 = *arg2;
3158 wxListItem item;
3159 line1->GetItem( 0, item );
3160 long data1 = item.m_data;
3161 line2->GetItem( 0, item );
3162 long data2 = item.m_data;
3163 return list_ctrl_compare_func_2( data1, data2, list_ctrl_compare_data );
3164 }
3165
3166 void wxListMainWindow::SortItems( wxListCtrlCompare fn, long data )
3167 {
3168 list_ctrl_compare_func_2 = fn;
3169 list_ctrl_compare_data = data;
3170 m_lines.Sort( list_ctrl_compare_func_1 );
3171 m_dirty = TRUE;
3172 }
3173
3174 void wxListMainWindow::OnScroll(wxScrollWinEvent& event)
3175 {
3176 // FIXME
3177 #if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
3178 wxScrolledWindow::OnScroll(event);
3179 #else
3180 HandleOnScroll( event );
3181 #endif
3182
3183 #if wxUSE_GENERIC_LIST_EXTENSIONS
3184
3185 if (event.GetOrientation() == wxHORIZONTAL && ( m_mode & wxLC_REPORT ))
3186 {
3187 wxListCtrl* lc = wxDynamicCast( GetParent() , wxListCtrl ) ;
3188 if ( lc )
3189 {
3190 lc->m_headerWin->Refresh() ;
3191 #ifdef __WXMAC__
3192 lc->m_headerWin->MacUpdateImmediately() ;
3193 #endif
3194 }
3195 }
3196 #endif
3197 }
3198
3199 // -------------------------------------------------------------------------------------
3200 // wxListItem
3201 // -------------------------------------------------------------------------------------
3202
3203 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
3204
3205 wxListItem::wxListItem()
3206 {
3207 m_mask = 0;
3208 m_itemId = 0;
3209 m_col = 0;
3210 m_state = 0;
3211 m_stateMask = 0;
3212 m_image = 0;
3213 m_data = 0;
3214 m_format = wxLIST_FORMAT_CENTRE;
3215 m_width = 0;
3216
3217 m_attr = NULL;
3218 }
3219
3220 void wxListItem::Clear()
3221 {
3222 m_mask = 0;
3223 m_itemId = 0;
3224 m_col = 0;
3225 m_state = 0;
3226 m_stateMask = 0;
3227 m_image = 0;
3228 m_data = 0;
3229 m_format = wxLIST_FORMAT_CENTRE;
3230 m_width = 0;
3231 m_text = _T("");
3232
3233 if (m_attr) delete m_attr;
3234 m_attr = NULL;
3235 }
3236
3237 void wxListItem::ClearAttributes()
3238 {
3239 if (m_attr) delete m_attr;
3240 m_attr = NULL;
3241 }
3242
3243 // -------------------------------------------------------------------------------------
3244 // wxListEvent
3245 // -------------------------------------------------------------------------------------
3246
3247 IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
3248
3249 wxListEvent::wxListEvent( wxEventType commandType, int id ):
3250 wxNotifyEvent( commandType, id )
3251 {
3252 m_code = 0;
3253 m_itemIndex = 0;
3254 m_oldItemIndex = 0;
3255 m_col = 0;
3256 m_cancelled = FALSE;
3257 m_pointDrag.x = 0;
3258 m_pointDrag.y = 0;
3259 }
3260
3261 void wxListEvent::CopyObject(wxObject& object_dest) const
3262 {
3263 wxListEvent *obj = (wxListEvent *)&object_dest;
3264
3265 wxNotifyEvent::CopyObject(object_dest);
3266
3267 obj->m_code = m_code;
3268 obj->m_itemIndex = m_itemIndex;
3269 obj->m_oldItemIndex = m_oldItemIndex;
3270 obj->m_col = m_col;
3271 obj->m_cancelled = m_cancelled;
3272 obj->m_pointDrag = m_pointDrag;
3273 obj->m_item.m_mask = m_item.m_mask;
3274 obj->m_item.m_itemId = m_item.m_itemId;
3275 obj->m_item.m_col = m_item.m_col;
3276 obj->m_item.m_state = m_item.m_state;
3277 obj->m_item.m_stateMask = m_item.m_stateMask;
3278 obj->m_item.m_text = m_item.m_text;
3279 obj->m_item.m_image = m_item.m_image;
3280 obj->m_item.m_data = m_item.m_data;
3281 obj->m_item.m_format = m_item.m_format;
3282 obj->m_item.m_width = m_item.m_width;
3283
3284 if ( m_item.HasAttributes() )
3285 {
3286 obj->m_item.SetTextColour(m_item.GetTextColour());
3287 }
3288 }
3289
3290 // -------------------------------------------------------------------------------------
3291 // wxListCtrl
3292 // -------------------------------------------------------------------------------------
3293
3294 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
3295
3296 BEGIN_EVENT_TABLE(wxListCtrl,wxControl)
3297 EVT_SIZE (wxListCtrl::OnSize)
3298 EVT_IDLE (wxListCtrl::OnIdle)
3299 END_EVENT_TABLE()
3300
3301 wxListCtrl::wxListCtrl()
3302 {
3303 m_imageListNormal = (wxImageList *) NULL;
3304 m_imageListSmall = (wxImageList *) NULL;
3305 m_imageListState = (wxImageList *) NULL;
3306 m_ownsImageListNormal = m_ownsImageListSmall = m_ownsImageListState = FALSE;
3307 m_mainWin = (wxListMainWindow*) NULL;
3308 m_headerWin = (wxListHeaderWindow*) NULL;
3309 }
3310
3311 wxListCtrl::~wxListCtrl()
3312 {
3313 if (m_ownsImageListNormal) delete m_imageListNormal;
3314 if (m_ownsImageListSmall) delete m_imageListSmall;
3315 if (m_ownsImageListState) delete m_imageListState;
3316 }
3317
3318 bool wxListCtrl::Create(wxWindow *parent,
3319 wxWindowID id,
3320 const wxPoint &pos,
3321 const wxSize &size,
3322 long style,
3323 const wxValidator &validator,
3324 const wxString &name)
3325 {
3326 m_imageListNormal = (wxImageList *) NULL;
3327 m_imageListSmall = (wxImageList *) NULL;
3328 m_imageListState = (wxImageList *) NULL;
3329 m_ownsImageListNormal = m_ownsImageListSmall = m_ownsImageListState = FALSE;
3330 m_mainWin = (wxListMainWindow*) NULL;
3331 m_headerWin = (wxListHeaderWindow*) NULL;
3332
3333 if ( !(style & (wxLC_REPORT | wxLC_LIST | wxLC_ICON)) )
3334 {
3335 style = style | wxLC_LIST;
3336 }
3337
3338 bool ret = wxControl::Create( parent, id, pos, size, style, validator, name );
3339
3340
3341 if (style & wxSUNKEN_BORDER)
3342 style -= wxSUNKEN_BORDER;
3343
3344 m_mainWin = new wxListMainWindow( this, -1, wxPoint(0,0), size, style );
3345
3346 if (HasFlag(wxLC_REPORT))
3347 {
3348 m_headerWin = new wxListHeaderWindow( this, -1, m_mainWin, wxPoint(0,0), wxSize(size.x,23), wxTAB_TRAVERSAL );
3349 if (HasFlag(wxLC_NO_HEADER))
3350 m_headerWin->Show( FALSE );
3351 }
3352 else
3353 {
3354 m_headerWin = (wxListHeaderWindow *) NULL;
3355 }
3356
3357 return ret;
3358 }
3359
3360 void wxListCtrl::OnSize( wxSizeEvent &WXUNUSED(event) )
3361 {
3362 /* handled in OnIdle */
3363
3364 if (m_mainWin) m_mainWin->m_dirty = TRUE;
3365 }
3366
3367 void wxListCtrl::SetSingleStyle( long style, bool add )
3368 {
3369 long flag = GetWindowStyle();
3370
3371 if (add)
3372 {
3373 if (style & wxLC_MASK_TYPE) flag = flag & ~wxLC_MASK_TYPE;
3374 if (style & wxLC_MASK_ALIGN) flag = flag & ~wxLC_MASK_ALIGN;
3375 if (style & wxLC_MASK_SORT) flag = flag & ~wxLC_MASK_SORT;
3376 }
3377
3378 if (add)
3379 {
3380 flag |= style;
3381 }
3382 else
3383 {
3384 if (flag & style) flag -= style;
3385 }
3386
3387 SetWindowStyleFlag( flag );
3388 }
3389
3390 void wxListCtrl::SetWindowStyleFlag( long flag )
3391 {
3392 if (m_mainWin)
3393 {
3394 m_mainWin->DeleteEverything();
3395
3396 int width = 0;
3397 int height = 0;
3398 GetClientSize( &width, &height );
3399
3400 m_mainWin->SetMode( flag );
3401
3402 if (flag & wxLC_REPORT)
3403 {
3404 if (!HasFlag(wxLC_REPORT))
3405 {
3406 if (!m_headerWin)
3407 {
3408 m_headerWin = new wxListHeaderWindow( this, -1, m_mainWin,
3409 wxPoint(0,0), wxSize(width,23), wxTAB_TRAVERSAL );
3410 if (HasFlag(wxLC_NO_HEADER))
3411 m_headerWin->Show( FALSE );
3412 }
3413 else
3414 {
3415 if (flag & wxLC_NO_HEADER)
3416 m_headerWin->Show( FALSE );
3417 else
3418 m_headerWin->Show( TRUE );
3419 }
3420 }
3421 }
3422 else
3423 {
3424 if (HasFlag(wxLC_REPORT) && !(HasFlag(wxLC_NO_HEADER)))
3425 {
3426 m_headerWin->Show( FALSE );
3427 }
3428 }
3429 }
3430
3431 wxWindow::SetWindowStyleFlag( flag );
3432 }
3433
3434 bool wxListCtrl::GetColumn(int col, wxListItem &item) const
3435 {
3436 m_mainWin->GetColumn( col, item );
3437 return TRUE;
3438 }
3439
3440 bool wxListCtrl::SetColumn( int col, wxListItem& item )
3441 {
3442 m_mainWin->SetColumn( col, item );
3443 return TRUE;
3444 }
3445
3446 int wxListCtrl::GetColumnWidth( int col ) const
3447 {
3448 return m_mainWin->GetColumnWidth( col );
3449 }
3450
3451 bool wxListCtrl::SetColumnWidth( int col, int width )
3452 {
3453 m_mainWin->SetColumnWidth( col, width );
3454 return TRUE;
3455 }
3456
3457 int wxListCtrl::GetCountPerPage() const
3458 {
3459 return m_mainWin->GetCountPerPage(); // different from Windows ?
3460 }
3461
3462 bool wxListCtrl::GetItem( wxListItem &info ) const
3463 {
3464 m_mainWin->GetItem( info );
3465 return TRUE;
3466 }
3467
3468 bool wxListCtrl::SetItem( wxListItem &info )
3469 {
3470 m_mainWin->SetItem( info );
3471 return TRUE;
3472 }
3473
3474 long wxListCtrl::SetItem( long index, int col, const wxString& label, int imageId )
3475 {
3476 wxListItem info;
3477 info.m_text = label;
3478 info.m_mask = wxLIST_MASK_TEXT;
3479 info.m_itemId = index;
3480 info.m_col = col;
3481 if ( imageId > -1 )
3482 {
3483 info.m_image = imageId;
3484 info.m_mask |= wxLIST_MASK_IMAGE;
3485 };
3486 m_mainWin->SetItem(info);
3487 return TRUE;
3488 }
3489
3490 int wxListCtrl::GetItemState( long item, long stateMask ) const
3491 {
3492 return m_mainWin->GetItemState( item, stateMask );
3493 }
3494
3495 bool wxListCtrl::SetItemState( long item, long state, long stateMask )
3496 {
3497 m_mainWin->SetItemState( item, state, stateMask );
3498 return TRUE;
3499 }
3500
3501 bool wxListCtrl::SetItemImage( long item, int image, int WXUNUSED(selImage) )
3502 {
3503 wxListItem info;
3504 info.m_image = image;
3505 info.m_mask = wxLIST_MASK_IMAGE;
3506 info.m_itemId = item;
3507 m_mainWin->SetItem( info );
3508 return TRUE;
3509 }
3510
3511 wxString wxListCtrl::GetItemText( long item ) const
3512 {
3513 wxListItem info;
3514 info.m_itemId = item;
3515 m_mainWin->GetItem( info );
3516 return info.m_text;
3517 }
3518
3519 void wxListCtrl::SetItemText( long item, const wxString &str )
3520 {
3521 wxListItem info;
3522 info.m_mask = wxLIST_MASK_TEXT;
3523 info.m_itemId = item;
3524 info.m_text = str;
3525 m_mainWin->SetItem( info );
3526 }
3527
3528 long wxListCtrl::GetItemData( long item ) const
3529 {
3530 wxListItem info;
3531 info.m_itemId = item;
3532 m_mainWin->GetItem( info );
3533 return info.m_data;
3534 }
3535
3536 bool wxListCtrl::SetItemData( long item, long data )
3537 {
3538 wxListItem info;
3539 info.m_mask = wxLIST_MASK_DATA;
3540 info.m_itemId = item;
3541 info.m_data = data;
3542 m_mainWin->SetItem( info );
3543 return TRUE;
3544 }
3545
3546 bool wxListCtrl::GetItemRect( long item, wxRect &rect, int WXUNUSED(code) ) const
3547 {
3548 m_mainWin->GetItemRect( item, rect );
3549 return TRUE;
3550 }
3551
3552 bool wxListCtrl::GetItemPosition( long item, wxPoint& pos ) const
3553 {
3554 m_mainWin->GetItemPosition( item, pos );
3555 return TRUE;
3556 }
3557
3558 bool wxListCtrl::SetItemPosition( long WXUNUSED(item), const wxPoint& WXUNUSED(pos) )
3559 {
3560 return 0;
3561 }
3562
3563 int wxListCtrl::GetItemCount() const
3564 {
3565 return m_mainWin->GetItemCount();
3566 }
3567
3568 int wxListCtrl::GetColumnCount() const
3569 {
3570 return m_mainWin->GetColumnCount();
3571 }
3572
3573 void wxListCtrl::SetItemSpacing( int spacing, bool isSmall )
3574 {
3575 m_mainWin->SetItemSpacing( spacing, isSmall );
3576 }
3577
3578 int wxListCtrl::GetItemSpacing( bool isSmall ) const
3579 {
3580 return m_mainWin->GetItemSpacing( isSmall );
3581 }
3582
3583 int wxListCtrl::GetSelectedItemCount() const
3584 {
3585 return m_mainWin->GetSelectedItemCount();
3586 }
3587
3588 wxColour wxListCtrl::GetTextColour() const
3589 {
3590 return GetForegroundColour();
3591 }
3592
3593 void wxListCtrl::SetTextColour(const wxColour& col)
3594 {
3595 SetForegroundColour(col);
3596 }
3597
3598 long wxListCtrl::GetTopItem() const
3599 {
3600 return 0;
3601 }
3602
3603 long wxListCtrl::GetNextItem( long item, int geom, int state ) const
3604 {
3605 return m_mainWin->GetNextItem( item, geom, state );
3606 }
3607
3608 wxImageList *wxListCtrl::GetImageList(int which) const
3609 {
3610 if (which == wxIMAGE_LIST_NORMAL)
3611 {
3612 return m_imageListNormal;
3613 }
3614 else if (which == wxIMAGE_LIST_SMALL)
3615 {
3616 return m_imageListSmall;
3617 }
3618 else if (which == wxIMAGE_LIST_STATE)
3619 {
3620 return m_imageListState;
3621 }
3622 return (wxImageList *) NULL;
3623 }
3624
3625 void wxListCtrl::SetImageList( wxImageList *imageList, int which )
3626 {
3627 if ( which == wxIMAGE_LIST_NORMAL )
3628 {
3629 if (m_ownsImageListNormal) delete m_imageListNormal;
3630 m_imageListNormal = imageList;
3631 m_ownsImageListNormal = FALSE;
3632 }
3633 else if ( which == wxIMAGE_LIST_SMALL )
3634 {
3635 if (m_ownsImageListSmall) delete m_imageListSmall;
3636 m_imageListSmall = imageList;
3637 m_ownsImageListSmall = FALSE;
3638 }
3639 else if ( which == wxIMAGE_LIST_STATE )
3640 {
3641 if (m_ownsImageListState) delete m_imageListState;
3642 m_imageListState = imageList;
3643 m_ownsImageListState = FALSE;
3644 }
3645
3646 m_mainWin->SetImageList( imageList, which );
3647 }
3648
3649 void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
3650 {
3651 SetImageList(imageList, which);
3652 if ( which == wxIMAGE_LIST_NORMAL )
3653 m_ownsImageListNormal = TRUE;
3654 else if ( which == wxIMAGE_LIST_SMALL )
3655 m_ownsImageListSmall = TRUE;
3656 else if ( which == wxIMAGE_LIST_STATE )
3657 m_ownsImageListState = TRUE;
3658 }
3659
3660 bool wxListCtrl::Arrange( int WXUNUSED(flag) )
3661 {
3662 return 0;
3663 }
3664
3665 bool wxListCtrl::DeleteItem( long item )
3666 {
3667 m_mainWin->DeleteItem( item );
3668 return TRUE;
3669 }
3670
3671 bool wxListCtrl::DeleteAllItems()
3672 {
3673 m_mainWin->DeleteAllItems();
3674 return TRUE;
3675 }
3676
3677 bool wxListCtrl::DeleteAllColumns()
3678 {
3679 for ( size_t n = 0; n < m_mainWin->m_columns.GetCount(); n++ )
3680 DeleteColumn(n);
3681
3682 return TRUE;
3683 }
3684
3685 void wxListCtrl::ClearAll()
3686 {
3687 m_mainWin->DeleteEverything();
3688 }
3689
3690 bool wxListCtrl::DeleteColumn( int col )
3691 {
3692 m_mainWin->DeleteColumn( col );
3693 return TRUE;
3694 }
3695
3696 void wxListCtrl::Edit( long item )
3697 {
3698 m_mainWin->Edit( item );
3699 }
3700
3701 bool wxListCtrl::EnsureVisible( long item )
3702 {
3703 m_mainWin->EnsureVisible( item );
3704 return TRUE;
3705 }
3706
3707 long wxListCtrl::FindItem( long start, const wxString& str, bool partial )
3708 {
3709 return m_mainWin->FindItem( start, str, partial );
3710 }
3711
3712 long wxListCtrl::FindItem( long start, long data )
3713 {
3714 return m_mainWin->FindItem( start, data );
3715 }
3716
3717 long wxListCtrl::FindItem( long WXUNUSED(start), const wxPoint& WXUNUSED(pt),
3718 int WXUNUSED(direction))
3719 {
3720 return 0;
3721 }
3722
3723 long wxListCtrl::HitTest( const wxPoint &point, int &flags )
3724 {
3725 return m_mainWin->HitTest( (int)point.x, (int)point.y, flags );
3726 }
3727
3728 long wxListCtrl::InsertItem( wxListItem& info )
3729 {
3730 m_mainWin->InsertItem( info );
3731 return info.m_itemId;
3732 }
3733
3734 long wxListCtrl::InsertItem( long index, const wxString &label )
3735 {
3736 wxListItem info;
3737 info.m_text = label;
3738 info.m_mask = wxLIST_MASK_TEXT;
3739 info.m_itemId = index;
3740 return InsertItem( info );
3741 }
3742
3743 long wxListCtrl::InsertItem( long index, int imageIndex )
3744 {
3745 wxListItem info;
3746 info.m_mask = wxLIST_MASK_IMAGE;
3747 info.m_image = imageIndex;
3748 info.m_itemId = index;
3749 return InsertItem( info );
3750 }
3751
3752 long wxListCtrl::InsertItem( long index, const wxString &label, int imageIndex )
3753 {
3754 wxListItem info;
3755 info.m_text = label;
3756 info.m_image = imageIndex;
3757 info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_IMAGE;
3758 info.m_itemId = index;
3759 return InsertItem( info );
3760 }
3761
3762 long wxListCtrl::InsertColumn( long col, wxListItem &item )
3763 {
3764 wxASSERT( m_headerWin );
3765 m_mainWin->InsertColumn( col, item );
3766 m_headerWin->Refresh();
3767
3768 return 0;
3769 }
3770
3771 long wxListCtrl::InsertColumn( long col, const wxString &heading,
3772 int format, int width )
3773 {
3774 wxListItem item;
3775 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
3776 item.m_text = heading;
3777 if (width >= -2)
3778 {
3779 item.m_mask |= wxLIST_MASK_WIDTH;
3780 item.m_width = width;
3781 }
3782 item.m_format = format;
3783
3784 return InsertColumn( col, item );
3785 }
3786
3787 bool wxListCtrl::ScrollList( int WXUNUSED(dx), int WXUNUSED(dy) )
3788 {
3789 return 0;
3790 }
3791
3792 // Sort items.
3793 // fn is a function which takes 3 long arguments: item1, item2, data.
3794 // item1 is the long data associated with a first item (NOT the index).
3795 // item2 is the long data associated with a second item (NOT the index).
3796 // data is the same value as passed to SortItems.
3797 // The return value is a negative number if the first item should precede the second
3798 // item, a positive number of the second item should precede the first,
3799 // or zero if the two items are equivalent.
3800 // data is arbitrary data to be passed to the sort function.
3801
3802 bool wxListCtrl::SortItems( wxListCtrlCompare fn, long data )
3803 {
3804 m_mainWin->SortItems( fn, data );
3805 return TRUE;
3806 }
3807
3808 void wxListCtrl::OnIdle( wxIdleEvent &WXUNUSED(event) )
3809 {
3810 if (!m_mainWin->m_dirty) return;
3811
3812 int cw = 0;
3813 int ch = 0;
3814 GetClientSize( &cw, &ch );
3815
3816 int x = 0;
3817 int y = 0;
3818 int w = 0;
3819 int h = 0;
3820
3821 if (HasFlag(wxLC_REPORT) && !HasFlag(wxLC_NO_HEADER))
3822 {
3823 m_headerWin->GetPosition( &x, &y );
3824 m_headerWin->GetSize( &w, &h );
3825 if ((x != 0) || (y != 0) || (w != cw) || (h != 23))
3826 m_headerWin->SetSize( 0, 0, cw, 23 );
3827
3828 m_mainWin->GetPosition( &x, &y );
3829 m_mainWin->GetSize( &w, &h );
3830 if ((x != 0) || (y != 24) || (w != cw) || (h != ch-24))
3831 m_mainWin->SetSize( 0, 24, cw, ch-24 );
3832 }
3833 else
3834 {
3835 m_mainWin->GetPosition( &x, &y );
3836 m_mainWin->GetSize( &w, &h );
3837 if ((x != 0) || (y != 24) || (w != cw) || (h != ch))
3838 m_mainWin->SetSize( 0, 0, cw, ch );
3839 }
3840
3841 m_mainWin->CalculatePositions();
3842 m_mainWin->RealizeChanges();
3843 m_mainWin->m_dirty = FALSE;
3844 m_mainWin->Refresh();
3845
3846 if ( m_headerWin && m_headerWin->m_dirty )
3847 {
3848 m_headerWin->m_dirty = FALSE;
3849 m_headerWin->Refresh();
3850 }
3851 }
3852
3853 bool wxListCtrl::SetBackgroundColour( const wxColour &colour )
3854 {
3855 if (m_mainWin)
3856 {
3857 m_mainWin->SetBackgroundColour( colour );
3858 m_mainWin->m_dirty = TRUE;
3859 }
3860
3861 return TRUE;
3862 }
3863
3864 bool wxListCtrl::SetForegroundColour( const wxColour &colour )
3865 {
3866 if ( !wxWindow::SetForegroundColour( colour ) )
3867 return FALSE;
3868
3869 if (m_mainWin)
3870 {
3871 m_mainWin->SetForegroundColour( colour );
3872 m_mainWin->m_dirty = TRUE;
3873 }
3874
3875 if (m_headerWin)
3876 {
3877 m_headerWin->SetForegroundColour( colour );
3878 }
3879
3880 return TRUE;
3881 }
3882
3883 bool wxListCtrl::SetFont( const wxFont &font )
3884 {
3885 if ( !wxWindow::SetFont( font ) )
3886 return FALSE;
3887
3888 if (m_mainWin)
3889 {
3890 m_mainWin->SetFont( font );
3891 m_mainWin->m_dirty = TRUE;
3892 }
3893
3894 if (m_headerWin)
3895 {
3896 m_headerWin->SetFont( font );
3897 }
3898
3899 return TRUE;
3900 }
3901
3902 #if wxUSE_DRAG_AND_DROP
3903
3904 void wxListCtrl::SetDropTarget( wxDropTarget *dropTarget )
3905 {
3906 m_mainWin->SetDropTarget( dropTarget );
3907 }
3908
3909 wxDropTarget *wxListCtrl::GetDropTarget() const
3910 {
3911 return m_mainWin->GetDropTarget();
3912 }
3913
3914 #endif // wxUSE_DRAG_AND_DROP
3915
3916 bool wxListCtrl::SetCursor( const wxCursor &cursor )
3917 {
3918 return m_mainWin ? m_mainWin->wxWindow::SetCursor(cursor) : FALSE;
3919 }
3920
3921 wxColour wxListCtrl::GetBackgroundColour() const
3922 {
3923 return m_mainWin ? m_mainWin->GetBackgroundColour() : wxColour();
3924 }
3925
3926 wxColour wxListCtrl::GetForegroundColour() const
3927 {
3928 return m_mainWin ? m_mainWin->GetForegroundColour() : wxColour();
3929 }
3930
3931 bool wxListCtrl::DoPopupMenu( wxMenu *menu, int x, int y )
3932 {
3933 #if wxUSE_MENUS
3934 return m_mainWin->PopupMenu( menu, x, y );
3935 #else
3936 return FALSE;
3937 #endif // wxUSE_MENUS
3938 }
3939
3940 void wxListCtrl::SetFocus()
3941 {
3942 /* The test in window.cpp fails as we are a composite
3943 window, so it checks against "this", but not m_mainWin. */
3944 if ( FindFocus() != this )
3945 m_mainWin->SetFocus();
3946 }
3947
3948 // ----------------------------------------------------------------------------
3949 // virtual list control support
3950 // ----------------------------------------------------------------------------
3951
3952 wxString wxListCtrl::OnGetItemText(long item, long col) const
3953 {
3954 // this is a pure virtual function, in fact - which is not really pure
3955 // because the controls which are not virtual don't need to implement it
3956 wxFAIL_MSG( _T("not supposed to be called") );
3957
3958 return wxEmptyString;
3959 }
3960
3961 int wxListCtrl::OnGetItemImage(long item) const
3962 {
3963 // same as above
3964 wxFAIL_MSG( _T("not supposed to be called") );
3965
3966 return -1;
3967 }
3968
3969 void wxListCtrl::SetItemCount(long count)
3970 {
3971 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
3972
3973 m_mainWin->SetItemCount(count);
3974 }
3975
3976 #endif // wxUSE_LISTCTRL