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