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