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