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