]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/listctrl_mac.cpp
wxMemoryDC constructor now optionally accepts a wxBitmap parameter,
[wxWidgets.git] / src / mac / carbon / listctrl_mac.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/listctrl_mac.cpp
3 // Purpose: wxListCtrl
4 // Author: Julian Smart
5 // Modified by: Agron Selimaj
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_LISTCTRL
28
29 #include "wx/listctrl.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #endif
34
35 #include "wx/mac/uma.h"
36
37 #include "wx/imaglist.h"
38 #include "wx/sysopt.h"
39
40 #define wxMAC_ALWAYS_USE_GENERIC_LISTCTRL wxT("mac.listctrl.always_use_generic")
41
42 #if wxUSE_EXTENDED_RTTI
43 WX_DEFINE_FLAGS( wxListCtrlStyle )
44
45 wxBEGIN_FLAGS( wxListCtrlStyle )
46 // new style border flags, we put them first to
47 // use them for streaming out
48 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
49 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
50 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
51 wxFLAGS_MEMBER(wxBORDER_RAISED)
52 wxFLAGS_MEMBER(wxBORDER_STATIC)
53 wxFLAGS_MEMBER(wxBORDER_NONE)
54
55 // old style border flags
56 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
57 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
58 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
59 wxFLAGS_MEMBER(wxRAISED_BORDER)
60 wxFLAGS_MEMBER(wxSTATIC_BORDER)
61 wxFLAGS_MEMBER(wxBORDER)
62
63 // standard window styles
64 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
65 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
66 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
67 wxFLAGS_MEMBER(wxWANTS_CHARS)
68 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
69 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
70 wxFLAGS_MEMBER(wxVSCROLL)
71 wxFLAGS_MEMBER(wxHSCROLL)
72
73 wxFLAGS_MEMBER(wxLC_LIST)
74 wxFLAGS_MEMBER(wxLC_REPORT)
75 wxFLAGS_MEMBER(wxLC_ICON)
76 wxFLAGS_MEMBER(wxLC_SMALL_ICON)
77 wxFLAGS_MEMBER(wxLC_ALIGN_TOP)
78 wxFLAGS_MEMBER(wxLC_ALIGN_LEFT)
79 wxFLAGS_MEMBER(wxLC_AUTOARRANGE)
80 wxFLAGS_MEMBER(wxLC_USER_TEXT)
81 wxFLAGS_MEMBER(wxLC_EDIT_LABELS)
82 wxFLAGS_MEMBER(wxLC_NO_HEADER)
83 wxFLAGS_MEMBER(wxLC_SINGLE_SEL)
84 wxFLAGS_MEMBER(wxLC_SORT_ASCENDING)
85 wxFLAGS_MEMBER(wxLC_SORT_DESCENDING)
86 wxFLAGS_MEMBER(wxLC_VIRTUAL)
87
88 wxEND_FLAGS( wxListCtrlStyle )
89
90 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListCtrl, wxControl,"wx/listctrl.h")
91
92 wxBEGIN_PROPERTIES_TABLE(wxListCtrl)
93 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
94
95 wxPROPERTY_FLAGS( WindowStyle , wxListCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
96 wxEND_PROPERTIES_TABLE()
97
98 wxBEGIN_HANDLERS_TABLE(wxListCtrl)
99 wxEND_HANDLERS_TABLE()
100
101 wxCONSTRUCTOR_5( wxListCtrl , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
102
103 /*
104 TODO : Expose more information of a list's layout etc. via appropriate objects (à la NotebookPageInfo)
105 */
106 #else
107 IMPLEMENT_DYNAMIC_CLASS(wxListCtrl, wxControl)
108 #endif
109
110 IMPLEMENT_DYNAMIC_CLASS(wxListView, wxListCtrl)
111 IMPLEMENT_DYNAMIC_CLASS(wxListItem, wxObject)
112
113 IMPLEMENT_DYNAMIC_CLASS(wxListEvent, wxNotifyEvent)
114
115 WX_DECLARE_EXPORTED_LIST(wxListItem, wxListItemList);
116 #include "wx/listimpl.cpp"
117 WX_DEFINE_LIST(wxListItemList)
118
119 // so we can check for column clicks
120 static const EventTypeSpec eventList[] =
121 {
122 { kEventClassControl, kEventControlHit },
123 };
124
125 static pascal OSStatus wxMacListCtrlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
126 {
127 OSStatus result = eventNotHandledErr ;
128
129 wxMacCarbonEvent cEvent( event ) ;
130
131 ControlRef controlRef ;
132 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
133
134 wxListCtrl *window = (wxListCtrl*) data ;
135 wxListEvent le( wxEVT_COMMAND_LIST_COL_CLICK, window->GetId() );
136 le.SetEventObject( window );
137
138 switch ( GetEventKind( event ) )
139 {
140 // check if the column was clicked on and fire an event if so
141 case kEventControlHit :
142 {
143 ControlPartCode result = cEvent.GetParameter<ControlPartCode>(kEventParamControlPart, typeControlPartCode) ;
144 if (result == kControlButtonPart){
145 DataBrowserPropertyID col;
146 GetDataBrowserSortProperty(controlRef, &col);
147 int column = col - kMinColumnId;
148 le.m_col = column;
149 window->GetEventHandler()->ProcessEvent( le );
150 }
151 result = CallNextEventHandler(handler, event);
152 break;
153 }
154 default :
155 break ;
156 }
157
158
159 return result ;
160 }
161
162 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacListCtrlEventHandler )
163
164 class wxMacListCtrlItem : public wxMacListBoxItem
165 {
166 public:
167 wxMacListCtrlItem();
168
169 virtual void Notification(wxMacDataItemBrowserControl *owner ,
170 DataBrowserItemNotification message,
171 DataBrowserItemDataRef itemData ) const;
172
173 virtual void SetColumnInfo( unsigned int column, wxListItem* item );
174 virtual wxListItem* GetColumnInfo( unsigned int column );
175 virtual bool HasColumnInfo( unsigned int column );
176
177 virtual void SetColumnTextValue( unsigned int column, const wxString& text );
178 virtual const wxString& GetColumnTextValue( unsigned int column );
179
180 virtual int GetColumnImageValue( unsigned int column );
181 virtual void SetColumnImageValue( unsigned int column, int imageIndex );
182
183 virtual ~wxMacListCtrlItem();
184 protected:
185 wxListItemList m_rowItems;
186 };
187
188 // TODO: Make a better name!!
189 class wxMacDataBrowserListCtrlControl : public wxMacDataItemBrowserControl
190 {
191 public:
192 wxMacDataBrowserListCtrlControl( wxWindow *peer, const wxPoint& pos, const wxSize& size, long style );
193 virtual ~wxMacDataBrowserListCtrlControl();
194
195 // create a list item (can be a subclass of wxMacListBoxItem)
196
197 virtual wxMacDataItem* CreateItem();
198
199 virtual void MacInsertItem( unsigned int n, wxListItem* item );
200 virtual void MacSetColumnInfo( unsigned int row, unsigned int column, wxListItem* item );
201 virtual void MacGetColumnInfo( unsigned int row, unsigned int column, wxListItem& item );
202 virtual void UpdateState(wxMacDataItem* dataItem, wxListItem* item);
203
204 protected:
205 // we need to override to provide specialized handling for virtual wxListCtrls
206 virtual OSStatus GetSetItemData(DataBrowserItemID itemID,
207 DataBrowserPropertyID property,
208 DataBrowserItemDataRef itemData,
209 Boolean changeValue );
210
211 virtual void ItemNotification(
212 DataBrowserItemID itemID,
213 DataBrowserItemNotification message,
214 DataBrowserItemDataRef itemData);
215
216 virtual Boolean CompareItems(DataBrowserItemID itemOneID,
217 DataBrowserItemID itemTwoID,
218 DataBrowserPropertyID sortProperty);
219
220 wxClientDataType m_clientDataItemsType;
221 bool m_isVirtual;
222
223 };
224
225 // TODO: This gives errors, find out why.
226 //BEGIN_EVENT_TABLE(wxListCtrl, wxControl)
227 // EVT_PAINT(wxListCtrl::OnPaint)
228 //END_EVENT_TABLE()
229
230 // ============================================================================
231 // implementation
232 // ============================================================================
233
234 wxMacListControl* wxListCtrl::GetPeer() const
235 {
236 return dynamic_cast<wxMacListControl*>(m_peer);
237 }
238
239 // ----------------------------------------------------------------------------
240 // wxListCtrl construction
241 // ----------------------------------------------------------------------------
242
243 void wxListCtrl::Init()
244 {
245 m_imageListNormal = NULL;
246 m_imageListSmall = NULL;
247 m_imageListState = NULL;
248
249 // keep track of if we created our own image lists, or if they were assigned
250 // to us.
251 m_ownsImageListNormal = m_ownsImageListSmall = m_ownsImageListState = false;
252 m_colCount = 0;
253 m_count = 0;
254 m_textCtrl = NULL;
255 m_genericImpl = NULL;
256 m_dbImpl = NULL;
257 m_compareFunc = NULL;
258 m_compareFuncData = 0;
259 }
260
261 class wxGenericListCtrlHook : public wxGenericListCtrl
262 {
263 public:
264 wxGenericListCtrlHook(wxListCtrl* parent,
265 wxWindowID id,
266 const wxPoint& pos,
267 const wxSize& size,
268 long style,
269 const wxValidator& validator,
270 const wxString& name)
271 : wxGenericListCtrl(parent, id, pos, size, style, validator, name),
272 m_nativeListCtrl(parent)
273 {
274 }
275
276 protected:
277 virtual wxListItemAttr * OnGetItemAttr(long item) const
278 {
279 return m_nativeListCtrl->OnGetItemAttr(item);
280 }
281
282 virtual int OnGetItemImage(long item) const
283 {
284 return m_nativeListCtrl->OnGetItemImage(item);
285 }
286
287 virtual int OnGetItemColumnImage(long item, long column) const
288 {
289 return m_nativeListCtrl->OnGetItemColumnImage(item, column);
290 }
291
292 virtual wxString OnGetItemText(long item, long column) const
293 {
294 return m_nativeListCtrl->OnGetItemText(item, column);
295 }
296
297 wxListCtrl* m_nativeListCtrl;
298
299 };
300
301 bool wxListCtrl::Create(wxWindow *parent,
302 wxWindowID id,
303 const wxPoint& pos,
304 const wxSize& size,
305 long style,
306 const wxValidator& validator,
307 const wxString& name)
308 {
309
310 // for now, we'll always use the generic list control for ICON and LIST views,
311 // because they dynamically change the number of columns on resize.
312 // Also, allow the user to set it to use the list ctrl as well.
313 // Also, use generic list control in VIRTUAL mode.
314 if ( (wxSystemOptions::HasOption( wxMAC_ALWAYS_USE_GENERIC_LISTCTRL )
315 && (wxSystemOptions::GetOptionInt( wxMAC_ALWAYS_USE_GENERIC_LISTCTRL ) == 1)) ||
316 (style & wxLC_ICON) || (style & wxLC_SMALL_ICON) || (style & wxLC_LIST) /* || (style & wxLC_VIRTUAL) */ )
317 {
318 m_macIsUserPane = true;
319
320 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
321 return false;
322 m_genericImpl = new wxGenericListCtrlHook(this, id, pos, size, style, validator, name);
323 return true;
324 }
325
326 else
327 {
328 m_macIsUserPane = false;
329
330 if ( !wxWindow::Create(parent, id, pos, size, style, name) )
331 return false;
332 m_dbImpl = new wxMacDataBrowserListCtrlControl( this, pos, size, style );
333 m_peer = m_dbImpl;
334
335 MacPostControlCreate( pos, size );
336
337 InstallControlEventHandler( m_peer->GetControlRef() , GetwxMacListCtrlEventHandlerUPP(),
338 GetEventTypeCount(eventList), eventList, this,
339 (EventHandlerRef *)&m_macListCtrlEventHandler);
340 }
341
342 return true;
343 }
344
345 wxListCtrl::~wxListCtrl()
346 {
347 if (m_ownsImageListNormal)
348 delete m_imageListNormal;
349 if (m_ownsImageListSmall)
350 delete m_imageListSmall;
351 if (m_ownsImageListState)
352 delete m_imageListState;
353 }
354
355 // ----------------------------------------------------------------------------
356 // set/get/change style
357 // ----------------------------------------------------------------------------
358
359 // Add or remove a single window style
360 void wxListCtrl::SetSingleStyle(long style, bool add)
361 {
362 long flag = GetWindowStyleFlag();
363
364 // Get rid of conflicting styles
365 if ( add )
366 {
367 if ( style & wxLC_MASK_TYPE)
368 flag = flag & ~wxLC_MASK_TYPE;
369 if ( style & wxLC_MASK_ALIGN )
370 flag = flag & ~wxLC_MASK_ALIGN;
371 if ( style & wxLC_MASK_SORT )
372 flag = flag & ~wxLC_MASK_SORT;
373 }
374
375 if ( add )
376 flag |= style;
377 else
378 flag &= ~style;
379
380 SetWindowStyleFlag(flag);
381 }
382
383 // Set the whole window style
384 void wxListCtrl::SetWindowStyleFlag(long flag)
385 {
386 if ( flag != m_windowStyle )
387 {
388 m_windowStyle = flag;
389
390 if (m_genericImpl)
391 {
392 m_genericImpl->SetWindowStyleFlag(flag);
393 }
394
395 Refresh();
396 }
397 }
398
399 void wxListCtrl::DoSetSize( int x, int y, int width, int height, int sizeFlags )
400 {
401 wxControl::DoSetSize(x, y, width, height, sizeFlags);
402
403 if (m_genericImpl)
404 m_genericImpl->SetSize(x, y, width, height, sizeFlags);
405 }
406
407 // ----------------------------------------------------------------------------
408 // accessors
409 // ----------------------------------------------------------------------------
410
411 // Gets information about this column
412 bool wxListCtrl::GetColumn(int col, wxListItem& item) const
413 {
414 if (m_genericImpl)
415 return m_genericImpl->GetColumn(col, item);
416
417 bool success = true;
418
419 if ( item.m_mask & wxLIST_MASK_TEXT )
420 {
421 }
422
423 if ( item.m_mask & wxLIST_MASK_FORMAT )
424 {
425 }
426
427 if ( item.m_mask & wxLIST_MASK_IMAGE )
428 {
429 }
430
431 if ( (item.m_mask & wxLIST_MASK_TEXT) )
432 {
433 }
434
435
436 return success;
437 }
438
439 // Sets information about this column
440 bool wxListCtrl::SetColumn(int col, wxListItem& item)
441 {
442 if (m_genericImpl)
443 return m_genericImpl->SetColumn(col, item);
444
445 return false;
446 }
447
448 int wxListCtrl::GetColumnCount() const
449 {
450 if (m_genericImpl)
451 return m_genericImpl->GetColumnCount();
452
453 if (m_dbImpl)
454 {
455 UInt32 count;
456 m_dbImpl->GetColumnCount(&count);
457 return count;
458 }
459
460 return m_colCount;
461 }
462
463 // Gets the column width
464 int wxListCtrl::GetColumnWidth(int col) const
465 {
466 if (m_genericImpl)
467 return m_genericImpl->GetColumnWidth(col);
468
469 if (m_dbImpl)
470 {
471 return m_dbImpl->GetColumnWidth(col);
472 }
473
474 return 0;
475 }
476
477 // Sets the column width
478 bool wxListCtrl::SetColumnWidth(int col, int width)
479 {
480 if (m_genericImpl)
481 return m_genericImpl->SetColumnWidth(col, width);
482
483 // TODO: This is setting the width of the first column
484 // to the entire window width; investigate why
485 // this is.
486
487 if (m_dbImpl)
488 {
489 int mywidth = width;
490 if (width == wxLIST_AUTOSIZE || width == wxLIST_AUTOSIZE_USEHEADER)
491 mywidth = 150;
492
493 if (col == -1)
494 {
495 for (int column = 0; column < GetColumnCount(); column++)
496 {
497 m_dbImpl->SetColumnWidth(col, mywidth);
498 }
499 }
500 else{
501 m_dbImpl->SetColumnWidth(col, mywidth);
502 }
503 return true;
504 }
505
506 return false;
507 }
508
509 // Gets the number of items that can fit vertically in the
510 // visible area of the list control (list or report view)
511 // or the total number of items in the list control (icon
512 // or small icon view)
513 int wxListCtrl::GetCountPerPage() const
514 {
515 if (m_genericImpl)
516 return m_genericImpl->GetCountPerPage();
517
518 if (m_dbImpl)
519 {
520 }
521
522 return 1;
523 }
524
525 // Gets the edit control for editing labels.
526 wxTextCtrl* wxListCtrl::GetEditControl() const
527 {
528 if (m_genericImpl)
529 return m_genericImpl->GetEditControl();
530
531 return NULL;
532 }
533
534 // Gets information about the item
535 bool wxListCtrl::GetItem(wxListItem& info) const
536 {
537 if (m_genericImpl)
538 return m_genericImpl->GetItem(info);
539
540 if (m_dbImpl)
541 m_dbImpl->MacGetColumnInfo(info.m_itemId, info.m_col, info);
542 bool success = true;
543 return success;
544 }
545
546 // Sets information about the item
547 bool wxListCtrl::SetItem(wxListItem& info)
548 {
549 if (m_genericImpl)
550 return m_genericImpl->SetItem(info);
551
552 if (m_dbImpl)
553 m_dbImpl->MacSetColumnInfo( info.m_itemId, info.m_col, &info );
554
555 return true;
556 }
557
558 long wxListCtrl::SetItem(long index, int col, const wxString& label, int imageId)
559 {
560 if (m_genericImpl)
561 return m_genericImpl->SetItem(index, col, label, imageId);
562
563 wxListItem info;
564 info.m_text = label;
565 info.m_mask = wxLIST_MASK_TEXT;
566 info.m_itemId = index;
567 info.m_col = col;
568 if ( imageId > -1 )
569 {
570 info.m_image = imageId;
571 info.m_mask |= wxLIST_MASK_IMAGE;
572 }
573 return SetItem(info);
574 }
575
576
577 // Gets the item state
578 int wxListCtrl::GetItemState(long item, long stateMask) const
579 {
580 if (m_genericImpl)
581 return m_genericImpl->GetItemState(item, stateMask);
582
583 wxListItem info;
584
585 info.m_mask = wxLIST_MASK_STATE;
586 info.m_stateMask = stateMask;
587 info.m_itemId = item;
588
589 if (!GetItem(info))
590 return 0;
591
592 return info.m_state;
593 }
594
595 // Sets the item state
596 bool wxListCtrl::SetItemState(long item, long state, long stateMask)
597 {
598 if (m_genericImpl)
599 return m_genericImpl->SetItemState(item, state, stateMask);
600
601 wxListItem info;
602 info.m_mask = wxLIST_MASK_STATE;
603 info.m_stateMask = stateMask;
604 info.m_state = state;
605 info.m_itemId = item;
606 return SetItem(info);
607 }
608
609 // Sets the item image
610 bool wxListCtrl::SetItemImage(long item, int image, int WXUNUSED(selImage))
611 {
612 return SetItemColumnImage(item, 0, image);
613 }
614
615 // Sets the item image
616 bool wxListCtrl::SetItemColumnImage(long item, long column, int image)
617 {
618 if (m_genericImpl)
619 return m_genericImpl->SetItemColumnImage(item, column, image);
620
621 wxListItem info;
622
623 info.m_mask = wxLIST_MASK_IMAGE;
624 info.m_image = image;
625 info.m_itemId = item;
626 info.m_col = column;
627
628 return SetItem(info);
629 }
630
631 // Gets the item text
632 wxString wxListCtrl::GetItemText(long item) const
633 {
634 if (m_genericImpl)
635 return m_genericImpl->GetItemText(item);
636
637 wxListItem info;
638
639 info.m_mask = wxLIST_MASK_TEXT;
640 info.m_itemId = item;
641
642 if (!GetItem(info))
643 return wxEmptyString;
644 return info.m_text;
645 }
646
647 // Sets the item text
648 void wxListCtrl::SetItemText(long item, const wxString& str)
649 {
650 if (m_genericImpl)
651 return m_genericImpl->SetItemText(item, str);
652
653 wxListItem info;
654
655 info.m_mask = wxLIST_MASK_TEXT;
656 info.m_itemId = item;
657 info.m_text = str;
658
659 SetItem(info);
660 }
661
662 // Gets the item data
663 long wxListCtrl::GetItemData(long item) const
664 {
665 if (m_genericImpl)
666 return m_genericImpl->GetItemData(item);
667
668 wxListItem info;
669
670 info.m_mask = wxLIST_MASK_DATA;
671 info.m_itemId = item;
672
673 if (!GetItem(info))
674 return 0;
675 return info.m_data;
676 }
677
678 // Sets the item data
679 bool wxListCtrl::SetItemData(long item, long data)
680 {
681 if (m_genericImpl)
682 return m_genericImpl->SetItemData(item, data);
683
684 wxListItem info;
685
686 info.m_mask = wxLIST_MASK_DATA;
687 info.m_itemId = item;
688 info.m_data = data;
689
690 return SetItem(info);
691 }
692
693 wxRect wxListCtrl::GetViewRect() const
694 {
695 wxASSERT_MSG( !HasFlag(wxLC_REPORT | wxLC_LIST),
696 _T("wxListCtrl::GetViewRect() only works in icon mode") );
697
698 if (m_genericImpl)
699 return m_genericImpl->GetViewRect();
700
701 wxRect rect;
702 return rect;
703 }
704
705 // Gets the item rectangle
706 bool wxListCtrl::GetItemRect(long item, wxRect& rect, int code) const
707 {
708 if (m_genericImpl)
709 return m_genericImpl->GetItemRect(item, rect, code);
710
711 return true;
712 }
713
714 // Gets the item position
715 bool wxListCtrl::GetItemPosition(long item, wxPoint& pos) const
716 {
717 if (m_genericImpl)
718 return m_genericImpl->GetItemPosition(item, pos);
719
720 bool success = false;
721
722 return success;
723 }
724
725 // Sets the item position.
726 bool wxListCtrl::SetItemPosition(long item, const wxPoint& pos)
727 {
728 if (m_genericImpl)
729 return m_genericImpl->SetItemPosition(item, pos);
730
731 return false;
732 }
733
734 // Gets the number of items in the list control
735 int wxListCtrl::GetItemCount() const
736 {
737 if (m_genericImpl)
738 return m_genericImpl->GetItemCount();
739
740 if (m_dbImpl)
741 return m_dbImpl->MacGetCount();
742
743 return m_count;
744 }
745
746 void wxListCtrl::SetItemSpacing( int spacing, bool isSmall )
747 {
748 if (m_genericImpl)
749 m_genericImpl->SetItemSpacing(spacing, isSmall);
750 }
751
752 wxSize wxListCtrl::GetItemSpacing() const
753 {
754 if (m_genericImpl)
755 return m_genericImpl->GetItemSpacing();
756
757 return wxSize(0, 0);
758 }
759
760 void wxListCtrl::SetItemTextColour( long item, const wxColour &col )
761 {
762 if (m_genericImpl)
763 {
764 m_genericImpl->SetItemTextColour(item, col);
765 return;
766 }
767
768 wxListItem info;
769 info.m_itemId = item;
770 info.SetTextColour( col );
771 SetItem( info );
772 }
773
774 wxColour wxListCtrl::GetItemTextColour( long item ) const
775 {
776 if (m_genericImpl)
777 return m_genericImpl->GetItemTextColour(item);
778
779 if (m_dbImpl)
780 {
781 wxListItem info;
782 if (GetItem(info))
783 return info.GetTextColour();
784 }
785 return wxNullColour;
786 }
787
788 void wxListCtrl::SetItemBackgroundColour( long item, const wxColour &col )
789 {
790 if (m_genericImpl)
791 {
792 m_genericImpl->SetItemBackgroundColour(item, col);
793 return;
794 }
795
796 wxListItem info;
797 info.m_itemId = item;
798 info.SetBackgroundColour( col );
799 SetItem( info );
800 }
801
802 wxColour wxListCtrl::GetItemBackgroundColour( long item ) const
803 {
804 if (m_genericImpl)
805 return m_genericImpl->GetItemBackgroundColour(item);
806
807 if (m_dbImpl)
808 {
809 wxListItem info;
810 if (GetItem(info))
811 return info.GetBackgroundColour();
812 }
813 return wxNullColour;
814 }
815
816 void wxListCtrl::SetItemFont( long item, const wxFont &f )
817 {
818 if (m_genericImpl)
819 {
820 m_genericImpl->SetItemFont(item, f);
821 return;
822 }
823
824 wxListItem info;
825 info.m_itemId = item;
826 info.SetFont( f );
827 SetItem( info );
828 }
829
830 wxFont wxListCtrl::GetItemFont( long item ) const
831 {
832 if (m_genericImpl)
833 return m_genericImpl->GetItemFont(item);
834
835 if (m_dbImpl)
836 {
837 wxListItem info;
838 if (GetItem(info))
839 return info.GetFont();
840 }
841
842 return wxNullFont;
843 }
844
845 // Gets the number of selected items in the list control
846 int wxListCtrl::GetSelectedItemCount() const
847 {
848 if (m_genericImpl)
849 return m_genericImpl->GetSelectedItemCount();
850
851 if (m_dbImpl)
852 return m_dbImpl->GetSelectedItemCount(NULL, true);
853
854 return 0;
855 }
856
857 // Gets the text colour of the listview
858 wxColour wxListCtrl::GetTextColour() const
859 {
860 if (m_genericImpl)
861 return m_genericImpl->GetTextColour();
862
863 // TODO: we need owner drawn list items to customize text color.
864 if (m_dbImpl)
865 return *wxBLACK;
866
867 return wxNullColour;
868 }
869
870 // Sets the text colour of the listview
871 void wxListCtrl::SetTextColour(const wxColour& col)
872 {
873 if (m_genericImpl)
874 {
875 m_genericImpl->SetTextColour(col);
876 return;
877 }
878
879 // TODO: if we add owner-drawn item support for DataBrowser,
880 // consider supporting this property
881 }
882
883 // Gets the index of the topmost visible item when in
884 // list or report view
885 long wxListCtrl::GetTopItem() const
886 {
887 if (m_genericImpl)
888 return m_genericImpl->GetTopItem();
889
890 return 0;
891 }
892
893 // Searches for an item, starting from 'item'.
894 // 'geometry' is one of
895 // wxLIST_NEXT_ABOVE/ALL/BELOW/LEFT/RIGHT.
896 // 'state' is a state bit flag, one or more of
897 // wxLIST_STATE_DROPHILITED/FOCUSED/SELECTED/CUT.
898 // item can be -1 to find the first item that matches the
899 // specified flags.
900 // Returns the item or -1 if unsuccessful.
901 long wxListCtrl::GetNextItem(long item, int geom, int state) const
902 {
903 if (m_genericImpl)
904 return m_genericImpl->GetNextItem(item, geom, state);
905
906 if (m_dbImpl && geom == wxLIST_NEXT_ALL && state == wxLIST_STATE_SELECTED )
907 {
908 long count = m_dbImpl->MacGetCount() ;
909 for ( long line = item + 1 ; line < count; line++ )
910 {
911 wxMacDataItem* id = m_dbImpl->GetItemFromLine(line);
912 if ( m_dbImpl->IsItemSelected(id ) )
913 return line;
914 }
915 return -1;
916 }
917
918 return 0;
919 }
920
921
922 wxImageList *wxListCtrl::GetImageList(int which) const
923 {
924 if (m_genericImpl)
925 return m_genericImpl->GetImageList(which);
926
927 if ( which == wxIMAGE_LIST_NORMAL )
928 {
929 return m_imageListNormal;
930 }
931 else if ( which == wxIMAGE_LIST_SMALL )
932 {
933 return m_imageListSmall;
934 }
935 else if ( which == wxIMAGE_LIST_STATE )
936 {
937 return m_imageListState;
938 }
939 return NULL;
940 }
941
942 void wxListCtrl::SetImageList(wxImageList *imageList, int which)
943 {
944 if (m_genericImpl)
945 {
946 m_genericImpl->SetImageList(imageList, which);
947 return;
948 }
949
950 if ( which == wxIMAGE_LIST_NORMAL )
951 {
952 if (m_ownsImageListNormal) delete m_imageListNormal;
953 m_imageListNormal = imageList;
954 m_ownsImageListNormal = false;
955 }
956 else if ( which == wxIMAGE_LIST_SMALL )
957 {
958 if (m_ownsImageListSmall) delete m_imageListSmall;
959 m_imageListSmall = imageList;
960 m_ownsImageListSmall = false;
961 }
962 else if ( which == wxIMAGE_LIST_STATE )
963 {
964 if (m_ownsImageListState) delete m_imageListState;
965 m_imageListState = imageList;
966 m_ownsImageListState = false;
967 }
968 }
969
970 void wxListCtrl::AssignImageList(wxImageList *imageList, int which)
971 {
972 if (m_genericImpl)
973 {
974 m_genericImpl->AssignImageList(imageList, which);
975 return;
976 }
977
978 SetImageList(imageList, which);
979 if ( which == wxIMAGE_LIST_NORMAL )
980 m_ownsImageListNormal = true;
981 else if ( which == wxIMAGE_LIST_SMALL )
982 m_ownsImageListSmall = true;
983 else if ( which == wxIMAGE_LIST_STATE )
984 m_ownsImageListState = true;
985 }
986
987 // ----------------------------------------------------------------------------
988 // Operations
989 // ----------------------------------------------------------------------------
990
991 // Arranges the items
992 bool wxListCtrl::Arrange(int flag)
993 {
994 if (m_genericImpl)
995 return m_genericImpl->Arrange(flag);
996 return false;
997 }
998
999 // Deletes an item
1000 bool wxListCtrl::DeleteItem(long item)
1001 {
1002 if (m_genericImpl)
1003 return m_genericImpl->DeleteItem(item);
1004
1005 if (m_dbImpl)
1006 {
1007 m_dbImpl->MacDelete(item);
1008 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ITEM, GetId() );
1009 event.SetEventObject( this );
1010 event.m_itemIndex = item;
1011 GetEventHandler()->ProcessEvent( event );
1012
1013 }
1014 return true;
1015 }
1016
1017 // Deletes all items
1018 bool wxListCtrl::DeleteAllItems()
1019 {
1020 if (m_genericImpl)
1021 return m_genericImpl->DeleteAllItems();
1022
1023 if (m_dbImpl)
1024 {
1025 m_dbImpl->MacClear();
1026 wxListEvent event( wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS, GetId() );
1027 event.SetEventObject( this );
1028 GetEventHandler()->ProcessEvent( event );
1029 }
1030 return true;
1031 }
1032
1033 // Deletes all items
1034 bool wxListCtrl::DeleteAllColumns()
1035 {
1036 if (m_genericImpl)
1037 return m_genericImpl->DeleteAllColumns();
1038
1039 if (m_dbImpl)
1040 {
1041 UInt32 cols;
1042 m_dbImpl->GetColumnCount(&cols);
1043 for (UInt32 col = 0; col < cols; col++)
1044 {
1045 DeleteColumn(col);
1046 }
1047 }
1048
1049 return true;
1050 }
1051
1052 // Deletes a column
1053 bool wxListCtrl::DeleteColumn(int col)
1054 {
1055 if (m_genericImpl)
1056 return m_genericImpl->DeleteColumn(col);
1057
1058 if (m_dbImpl)
1059 {
1060 OSStatus err = m_dbImpl->RemoveColumn(col);
1061 return err == noErr;
1062 }
1063
1064 return true;
1065 }
1066
1067 // Clears items, and columns if there are any.
1068 void wxListCtrl::ClearAll()
1069 {
1070 if (m_genericImpl)
1071 {
1072 m_genericImpl->ClearAll();
1073 return;
1074 }
1075
1076 if (m_dbImpl)
1077 {
1078 DeleteAllItems();
1079 DeleteAllColumns();
1080 }
1081 }
1082
1083 wxTextCtrl* wxListCtrl::EditLabel(long item, wxClassInfo* textControlClass)
1084 {
1085 if (m_genericImpl)
1086 return m_genericImpl->EditLabel(item, textControlClass);
1087
1088 if (m_dbImpl)
1089 {
1090 wxMacDataItem* id = m_dbImpl->GetItemFromLine(item);
1091 verify_noerr( SetDataBrowserEditItem(m_dbImpl->GetControlRef(), (DataBrowserItemID)id, kMinColumnId) );
1092 }
1093 return NULL;
1094 }
1095
1096 // End label editing, optionally cancelling the edit
1097 bool wxListCtrl::EndEditLabel(bool cancel)
1098 {
1099 // TODO: generic impl. doesn't have this method - is it needed for us?
1100 if (m_genericImpl)
1101 return true; // m_genericImpl->EndEditLabel(cancel);
1102
1103 if (m_dbImpl)
1104 verify_noerr( SetDataBrowserEditItem(m_dbImpl->GetControlRef(), kDataBrowserNoItem, kMinColumnId) );
1105 return true;
1106 }
1107
1108 // Ensures this item is visible
1109 bool wxListCtrl::EnsureVisible(long item)
1110 {
1111 if (m_genericImpl)
1112 return m_genericImpl->EnsureVisible(item);
1113
1114 if (m_dbImpl)
1115 {
1116 wxMacDataItem* dataItem = m_dbImpl->GetItemFromLine(item);
1117 m_dbImpl->RevealItem(dataItem, kDataBrowserRevealWithoutSelecting);
1118 }
1119
1120 return true;
1121 }
1122
1123 // Find an item whose label matches this string, starting from the item after 'start'
1124 // or the beginning if 'start' is -1.
1125 long wxListCtrl::FindItem(long start, const wxString& str, bool partial)
1126 {
1127 if (m_genericImpl)
1128 return m_genericImpl->FindItem(start, str, partial);
1129
1130 return -1;
1131 }
1132
1133 // Find an item whose data matches this data, starting from the item after 'start'
1134 // or the beginning if 'start' is -1.
1135 long wxListCtrl::FindItem(long start, long data)
1136 {
1137 if (m_genericImpl)
1138 return m_genericImpl->FindItem(start, data);
1139
1140 long idx = start + 1;
1141 long count = GetItemCount();
1142
1143 while (idx < count)
1144 {
1145 if (GetItemData(idx) == data)
1146 return idx;
1147 idx++;
1148 };
1149
1150 return -1;
1151 }
1152
1153 // Find an item nearest this position in the specified direction, starting from
1154 // the item after 'start' or the beginning if 'start' is -1.
1155 long wxListCtrl::FindItem(long start, const wxPoint& pt, int direction)
1156 {
1157 if (m_genericImpl)
1158 return m_genericImpl->FindItem(start, pt, direction);
1159 return -1;
1160 }
1161
1162 // Determines which item (if any) is at the specified point,
1163 // giving details in 'flags' (see wxLIST_HITTEST_... flags above)
1164 long
1165 wxListCtrl::HitTest(const wxPoint& point, int& flags, long *ptrSubItem) const
1166 {
1167 if (m_genericImpl)
1168 return m_genericImpl->HitTest(point, flags, ptrSubItem);
1169
1170 return -1;
1171 }
1172
1173
1174 // Inserts an item, returning the index of the new item if successful,
1175 // -1 otherwise.
1176 long wxListCtrl::InsertItem(wxListItem& info)
1177 {
1178 wxASSERT_MSG( !IsVirtual(), _T("can't be used with virtual controls") );
1179
1180 if (m_genericImpl)
1181 return m_genericImpl->InsertItem(info);
1182
1183 if (m_dbImpl)
1184 {
1185 int count = GetItemCount();
1186
1187 if (info.m_itemId > count)
1188 info.m_itemId = count;
1189
1190 m_dbImpl->MacInsertItem(info.m_itemId, &info );
1191 wxListEvent event( wxEVT_COMMAND_LIST_INSERT_ITEM, GetId() );
1192 event.SetEventObject( this );
1193 event.m_itemIndex = info.m_itemId;
1194 GetEventHandler()->ProcessEvent( event );
1195 }
1196
1197 return info.m_itemId;
1198 }
1199
1200 long wxListCtrl::InsertItem(long index, const wxString& label)
1201 {
1202 if (m_genericImpl)
1203 return m_genericImpl->InsertItem(index, label);
1204
1205 wxListItem info;
1206 info.m_text = label;
1207 info.m_mask = wxLIST_MASK_TEXT;
1208 info.m_itemId = index;
1209 return InsertItem(info);
1210 }
1211
1212 // Inserts an image item
1213 long wxListCtrl::InsertItem(long index, int imageIndex)
1214 {
1215 if (m_genericImpl)
1216 return m_genericImpl->InsertItem(index, imageIndex);
1217
1218 wxListItem info;
1219 info.m_image = imageIndex;
1220 info.m_mask = wxLIST_MASK_IMAGE;
1221 info.m_itemId = index;
1222 return InsertItem(info);
1223 }
1224
1225 // Inserts an image/string item
1226 long wxListCtrl::InsertItem(long index, const wxString& label, int imageIndex)
1227 {
1228 if (m_genericImpl)
1229 return m_genericImpl->InsertItem(index, label, imageIndex);
1230
1231 wxListItem info;
1232 info.m_image = imageIndex;
1233 info.m_text = label;
1234 info.m_mask = wxLIST_MASK_IMAGE | wxLIST_MASK_TEXT;
1235 info.m_itemId = index;
1236 return InsertItem(info);
1237 }
1238
1239 // For list view mode (only), inserts a column.
1240 long wxListCtrl::InsertColumn(long col, wxListItem& item)
1241 {
1242 if (m_genericImpl)
1243 return m_genericImpl->InsertColumn(col, item);
1244
1245 if (m_dbImpl)
1246 {
1247 if ( !(item.GetMask() & wxLIST_MASK_WIDTH) )
1248 item.SetWidth(150);
1249
1250 DataBrowserPropertyType type = kDataBrowserTextType;
1251 wxImageList* imageList = GetImageList(wxIMAGE_LIST_SMALL);
1252 if (imageList && imageList->GetImageCount() > 0)
1253 {
1254 wxBitmap bmp = imageList->GetBitmap(0);
1255 if (bmp.Ok())
1256 type = kDataBrowserIconAndTextType;
1257 }
1258
1259 SInt16 just = teFlushDefault;
1260 if (item.GetMask() & wxLIST_MASK_FORMAT)
1261 {
1262 if (item.GetAlign() == wxLIST_FORMAT_LEFT)
1263 just = teFlushLeft;
1264 else if (item.GetAlign() == wxLIST_FORMAT_CENTER)
1265 just = teCenter;
1266 else if (item.GetAlign() == wxLIST_FORMAT_RIGHT)
1267 just = teFlushRight;
1268 }
1269 m_dbImpl->InsertColumn(col, type, item.GetText(), just, item.GetWidth());
1270
1271 // set/remove options based on the wxListCtrl type.
1272 DataBrowserTableViewColumnID id;
1273 m_dbImpl->GetColumnIDFromIndex(col, &id);
1274 DataBrowserPropertyFlags flags;
1275 verify_noerr(m_dbImpl->GetPropertyFlags(id, &flags));
1276 if (GetWindowStyleFlag() & wxLC_EDIT_LABELS)
1277 flags |= kDataBrowserPropertyIsEditable;
1278
1279 if (GetWindowStyleFlag() & wxLC_VIRTUAL){
1280 flags &= ~kDataBrowserListViewSortableColumn;
1281 }
1282 verify_noerr(m_dbImpl->SetPropertyFlags(id, flags));
1283 }
1284
1285 return col;
1286 }
1287
1288 long wxListCtrl::InsertColumn(long col,
1289 const wxString& heading,
1290 int format,
1291 int width)
1292 {
1293 if (m_genericImpl)
1294 return m_genericImpl->InsertColumn(col, heading, format, width);
1295
1296 wxListItem item;
1297 item.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_FORMAT;
1298 item.m_text = heading;
1299 if ( width > -1 )
1300 {
1301 item.m_mask |= wxLIST_MASK_WIDTH;
1302 item.m_width = width;
1303 }
1304 item.m_format = format;
1305
1306 return InsertColumn(col, item);
1307 }
1308
1309 // scroll the control by the given number of pixels (exception: in list view,
1310 // dx is interpreted as number of columns)
1311 bool wxListCtrl::ScrollList(int dx, int dy)
1312 {
1313 if (m_genericImpl)
1314 return m_genericImpl->ScrollList(dx, dy);
1315
1316 if (m_dbImpl)
1317 {
1318 m_dbImpl->SetScrollPosition(dx, dy);
1319 }
1320 return true;
1321 }
1322
1323
1324 bool wxListCtrl::SortItems(wxListCtrlCompare fn, long data)
1325 {
1326 if (m_genericImpl)
1327 return m_genericImpl->SortItems(fn, data);
1328
1329 if (m_dbImpl)
1330 {
1331 m_compareFunc = fn;
1332 m_compareFuncData = data;
1333 }
1334
1335 return true;
1336 }
1337
1338 // ----------------------------------------------------------------------------
1339 // virtual list controls
1340 // ----------------------------------------------------------------------------
1341
1342 wxString wxListCtrl::OnGetItemText(long WXUNUSED(item), long WXUNUSED(col)) const
1343 {
1344 // this is a pure virtual function, in fact - which is not really pure
1345 // because the controls which are not virtual don't need to implement it
1346 wxFAIL_MSG( _T("wxListCtrl::OnGetItemText not supposed to be called") );
1347
1348 return wxEmptyString;
1349 }
1350
1351 int wxListCtrl::OnGetItemImage(long WXUNUSED(item)) const
1352 {
1353 wxCHECK_MSG(!GetImageList(wxIMAGE_LIST_SMALL),
1354 -1,
1355 wxT("List control has an image list, OnGetItemImage or OnGetItemColumnImage should be overridden."));
1356 return -1;
1357 }
1358
1359 int wxListCtrl::OnGetItemColumnImage(long item, long column) const
1360 {
1361 if (!column)
1362 return OnGetItemImage(item);
1363
1364 return -1;
1365 }
1366
1367 wxListItemAttr *wxListCtrl::OnGetItemAttr(long WXUNUSED_UNLESS_DEBUG(item)) const
1368 {
1369 wxASSERT_MSG( item >= 0 && item < GetItemCount(),
1370 _T("invalid item index in OnGetItemAttr()") );
1371
1372 // no attributes by default
1373 return NULL;
1374 }
1375
1376 void wxListCtrl::SetItemCount(long count)
1377 {
1378 wxASSERT_MSG( IsVirtual(), _T("this is for virtual controls only") );
1379
1380 if (m_genericImpl)
1381 {
1382 m_genericImpl->SetItemCount(count);
1383 return;
1384 }
1385
1386 if (m_dbImpl)
1387 {
1388 // we need to temporarily disable the new item creation notification
1389 // procedure to speed things up
1390 // FIXME: Even this doesn't seem to help much...
1391 DataBrowserCallbacks callbacks;
1392 DataBrowserItemNotificationUPP itemUPP;
1393 GetDataBrowserCallbacks(m_dbImpl->GetControlRef(), &callbacks);
1394 itemUPP = callbacks.u.v1.itemNotificationCallback;
1395 callbacks.u.v1.itemNotificationCallback = 0;
1396 m_dbImpl->SetCallbacks(&callbacks);
1397 ::AddDataBrowserItems(m_dbImpl->GetControlRef(), kDataBrowserNoItem,
1398 count, NULL, kDataBrowserItemNoProperty);
1399 callbacks.u.v1.itemNotificationCallback = itemUPP;
1400 m_dbImpl->SetCallbacks(&callbacks);
1401 }
1402 m_count = count;
1403 }
1404
1405 void wxListCtrl::RefreshItem(long item)
1406 {
1407 if (m_genericImpl)
1408 {
1409 m_genericImpl->RefreshItem(item);
1410 return;
1411 }
1412
1413 wxRect rect;
1414 GetItemRect(item, rect);
1415 RefreshRect(rect);
1416 }
1417
1418 void wxListCtrl::RefreshItems(long itemFrom, long itemTo)
1419 {
1420 if (m_genericImpl)
1421 {
1422 m_genericImpl->RefreshItems(itemFrom, itemTo);
1423 return;
1424 }
1425
1426 wxRect rect1, rect2;
1427 GetItemRect(itemFrom, rect1);
1428 GetItemRect(itemTo, rect2);
1429
1430 wxRect rect = rect1;
1431 rect.height = rect2.GetBottom() - rect1.GetTop();
1432
1433 RefreshRect(rect);
1434 }
1435
1436
1437 // wxMac internal data structures
1438
1439 wxMacListCtrlItem::~wxMacListCtrlItem()
1440 {
1441 }
1442
1443 void wxMacListCtrlItem::Notification(wxMacDataItemBrowserControl *owner ,
1444 DataBrowserItemNotification message,
1445 DataBrowserItemDataRef itemData ) const
1446 {
1447
1448 wxMacDataBrowserListCtrlControl *lb = dynamic_cast<wxMacDataBrowserListCtrlControl*>(owner);
1449
1450 // we want to depend on as little as possible to make sure tear-down of controls is safe
1451 if ( message == kDataBrowserItemRemoved)
1452 {
1453 if ( lb != NULL && lb->GetClientDataType() == wxClientData_Object )
1454 {
1455 delete (wxClientData*) (m_data);
1456 }
1457
1458 delete this;
1459 return;
1460 }
1461 else if ( message == kDataBrowserItemAdded )
1462 {
1463 // we don't issue events on adding, the item is not really stored in the list yet, so we
1464 // avoid asserts by gettting out now
1465 return ;
1466 }
1467
1468 wxListCtrl *list = wxDynamicCast( owner->GetPeer() , wxListCtrl );
1469 if ( list )
1470 {
1471 bool trigger = false;
1472
1473 wxListEvent event( wxEVT_COMMAND_LIST_ITEM_SELECTED, list->GetId() );
1474 bool isSingle = (list->GetWindowStyle() & wxLC_SINGLE_SEL) != 0;
1475
1476 event.SetEventObject( list );
1477 event.m_itemIndex = owner->GetLineFromItem( this ) ;
1478 if ( !list->IsVirtual() )
1479 {
1480 lb->MacGetColumnInfo(event.m_itemIndex,0,event.m_item);
1481 }
1482
1483 switch (message)
1484 {
1485 case kDataBrowserItemDeselected:
1486 event.SetEventType(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
1487 if ( !isSingle )
1488 trigger = !lb->IsSelectionSuppressed();
1489 break;
1490
1491 case kDataBrowserItemSelected:
1492 trigger = !lb->IsSelectionSuppressed();
1493 break;
1494
1495 case kDataBrowserItemDoubleClicked:
1496 event.SetEventType( wxEVT_LEFT_DCLICK );
1497 trigger = true;
1498 break;
1499
1500 case kDataBrowserEditStarted :
1501 // TODO : how to veto ?
1502 event.SetEventType( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT ) ;
1503 trigger = true ;
1504 break ;
1505
1506 case kDataBrowserEditStopped :
1507 // TODO probably trigger only upon the value store callback, because
1508 // here IIRC we cannot veto
1509 event.SetEventType( wxEVT_COMMAND_LIST_END_LABEL_EDIT ) ;
1510 trigger = true ;
1511 break ;
1512
1513 default:
1514 break;
1515 }
1516
1517 if ( trigger )
1518 {
1519 // direct notification is not always having the listbox GetSelection() having in synch with event
1520 wxPostEvent( list->GetEventHandler(), event );
1521 }
1522 }
1523
1524 }
1525
1526 wxMacDataBrowserListCtrlControl::wxMacDataBrowserListCtrlControl( wxWindow *peer, const wxPoint& pos, const wxSize& size, long style)
1527 : wxMacDataItemBrowserControl( peer, pos, size, style )
1528 {
1529 OSStatus err = noErr;
1530 m_clientDataItemsType = wxClientData_None;
1531 m_isVirtual = false;
1532
1533 if ( style & wxLC_VIRTUAL )
1534 m_isVirtual = true;
1535
1536 DataBrowserSelectionFlags options = kDataBrowserDragSelect;
1537 if ( style & wxLC_SINGLE_SEL )
1538 {
1539 options |= kDataBrowserSelectOnlyOne;
1540 }
1541 else
1542 {
1543 options |= kDataBrowserCmdTogglesSelection;
1544 }
1545
1546 err = SetSelectionFlags( options );
1547 verify_noerr( err );
1548
1549 if ( style & wxLC_LIST )
1550 {
1551 InsertColumn(0, kDataBrowserIconAndTextType, wxEmptyString, -1, -1);
1552 verify_noerr( AutoSizeColumns() );
1553 }
1554
1555 if ( style & wxLC_LIST || style & wxLC_NO_HEADER )
1556 verify_noerr( SetHeaderButtonHeight( 0 ) );
1557
1558 if ( m_isVirtual )
1559 SetSortProperty( kMinColumnId - 1 );
1560 else
1561 SetSortProperty( kMinColumnId );
1562 if ( style & wxLC_SORT_ASCENDING )
1563 {
1564 m_sortOrder = SortOrder_Text_Ascending;
1565 SetSortOrder( kDataBrowserOrderIncreasing );
1566 }
1567 else if ( style & wxLC_SORT_DESCENDING )
1568 {
1569 m_sortOrder = SortOrder_Text_Descending;
1570 SetSortOrder( kDataBrowserOrderDecreasing );
1571 }
1572 else
1573 {
1574 m_sortOrder = SortOrder_None;
1575 }
1576
1577 if ( style & wxLC_VRULES )
1578 {
1579 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
1580 verify_noerr( DataBrowserChangeAttributes(m_controlRef, kDataBrowserAttributeListViewDrawColumnDividers, kDataBrowserAttributeNone) );
1581 #endif
1582 }
1583
1584 verify_noerr( SetHiliteStyle(kDataBrowserTableViewFillHilite ) );
1585 err = SetHasScrollBars( (style & wxHSCROLL) != 0 , true );
1586 }
1587
1588 OSStatus wxMacDataBrowserListCtrlControl::GetSetItemData(DataBrowserItemID itemID,
1589 DataBrowserPropertyID property,
1590 DataBrowserItemDataRef itemData,
1591 Boolean changeValue )
1592 {
1593 wxString text;
1594 int imgIndex = -1;
1595 short listColumn = property - kMinColumnId;
1596
1597 OSStatus err = errDataBrowserPropertyNotSupported;
1598 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
1599 wxMacListCtrlItem* lcItem;
1600
1601 if (listColumn >= 0)
1602 {
1603 if (!m_isVirtual)
1604 {
1605 lcItem = (wxMacListCtrlItem*) itemID;
1606 if (lcItem->HasColumnInfo(listColumn)){
1607 wxListItem* item = lcItem->GetColumnInfo(listColumn);
1608 if (item->GetMask() & wxLIST_MASK_TEXT)
1609 text = item->GetText();
1610 if (item->GetMask() & wxLIST_MASK_IMAGE)
1611 imgIndex = item->GetImage();
1612 }
1613 }
1614 else
1615 {
1616 text = list->OnGetItemText( (long)itemID-1, listColumn );
1617 imgIndex = list->OnGetItemColumnImage( (long)itemID-1, listColumn );
1618 }
1619 }
1620
1621 if ( !changeValue )
1622 {
1623 switch (property)
1624 {
1625 case kDataBrowserItemIsEditableProperty :
1626 if ( list && list->HasFlag( wxLC_EDIT_LABELS ) )
1627 {
1628 verify_noerr(SetDataBrowserItemDataBooleanValue( itemData, true ));
1629 err = noErr ;
1630 }
1631 break ;
1632 default :
1633 if ( property >= kMinColumnId )
1634 {
1635 wxMacCFStringHolder cfStr;
1636
1637 if (text){
1638 cfStr.Assign( text, wxLocale::GetSystemEncoding() );
1639 err = ::SetDataBrowserItemDataText( itemData, cfStr );
1640 err = noErr;
1641 }
1642
1643
1644
1645 if ( imgIndex != -1 )
1646 {
1647 wxImageList* imageList = list->GetImageList(wxIMAGE_LIST_SMALL);
1648 if (imageList && imageList->GetImageCount() > 0){
1649 wxBitmap bmp = imageList->GetBitmap(imgIndex);
1650 IconRef icon = bmp.GetBitmapData()->GetIconRef();
1651 ::SetDataBrowserItemDataIcon(itemData, icon);
1652 }
1653 }
1654
1655 }
1656 break ;
1657 }
1658
1659 }
1660 else
1661 {
1662 switch (property)
1663 {
1664 default:
1665 if ( property >= kMinColumnId )
1666 {
1667 short listColumn = property - kMinColumnId;
1668
1669 // TODO probably send the 'end edit' from here, as we
1670 // can then deal with the veto
1671 CFStringRef sr ;
1672 verify_noerr( GetDataBrowserItemDataText( itemData , &sr ) ) ;
1673 wxMacCFStringHolder cfStr(sr) ;;
1674 if (m_isVirtual)
1675 list->SetItem( (long)itemData-1 , listColumn, cfStr.AsString() ) ;
1676 else
1677 {
1678 if (lcItem)
1679 lcItem->SetColumnTextValue( listColumn, cfStr.AsString() );
1680 }
1681 err = noErr ;
1682 }
1683 break;
1684 }
1685 }
1686 return err;
1687 }
1688
1689 void wxMacDataBrowserListCtrlControl::ItemNotification(DataBrowserItemID itemID,
1690 DataBrowserItemNotification message,
1691 DataBrowserItemDataRef itemData )
1692 {
1693 // we want to depend on as little as possible to make sure tear-down of controls is safe
1694 if ( message == kDataBrowserItemRemoved)
1695 {
1696 // make sure MacDelete does the proper teardown.
1697 return;
1698 }
1699 else if ( message == kDataBrowserItemAdded )
1700 {
1701 // we don't issue events on adding, the item is not really stored in the list yet, so we
1702 // avoid asserts by getting out now
1703 return ;
1704 }
1705
1706 wxListCtrl *list = wxDynamicCast( GetPeer() , wxListCtrl );
1707 if ( list )
1708 {
1709 bool trigger = false;
1710
1711 wxListEvent event( wxEVT_COMMAND_LIST_ITEM_SELECTED, list->GetId() );
1712 bool isSingle = (list->GetWindowStyle() & wxLC_SINGLE_SEL) != 0;
1713
1714 event.SetEventObject( list );
1715 if ( !list->IsVirtual() )
1716 {
1717 DataBrowserTableViewRowIndex result = 0;
1718 verify_noerr( GetItemRow( itemID, &result ) ) ;
1719 event.m_itemIndex = result;
1720
1721 if (event.m_itemIndex >= 0)
1722 MacGetColumnInfo(event.m_itemIndex,0,event.m_item);
1723 }
1724 else
1725 {
1726 event.m_itemIndex = (long)itemID;
1727 }
1728
1729 switch (message)
1730 {
1731 case kDataBrowserItemDeselected:
1732 event.SetEventType(wxEVT_COMMAND_LIST_ITEM_DESELECTED);
1733 if ( !isSingle )
1734 trigger = IsSelectionSuppressed();
1735 break;
1736
1737 case kDataBrowserItemSelected:
1738 trigger = IsSelectionSuppressed();
1739 break;
1740
1741 case kDataBrowserItemDoubleClicked:
1742 event.SetEventType( wxEVT_LEFT_DCLICK );
1743 trigger = true;
1744 break;
1745
1746 case kDataBrowserEditStarted :
1747 // TODO : how to veto ?
1748 event.SetEventType( wxEVT_COMMAND_LIST_BEGIN_LABEL_EDIT ) ;
1749 trigger = true ;
1750 break ;
1751
1752 case kDataBrowserEditStopped :
1753 // TODO probably trigger only upon the value store callback, because
1754 // here IIRC we cannot veto
1755 event.SetEventType( wxEVT_COMMAND_LIST_END_LABEL_EDIT ) ;
1756 trigger = true ;
1757 break ;
1758
1759 default:
1760 break;
1761 }
1762
1763 if ( trigger )
1764 {
1765 // direct notification is not always having the listbox GetSelection() having in synch with event
1766 wxPostEvent( list->GetEventHandler(), event );
1767 }
1768 }
1769 }
1770
1771 Boolean wxMacDataBrowserListCtrlControl::CompareItems(DataBrowserItemID itemOneID,
1772 DataBrowserItemID itemTwoID,
1773 DataBrowserPropertyID sortProperty)
1774 {
1775
1776 bool retval = false;
1777 wxString itemText;
1778 wxString otherItemText;
1779 int colId = sortProperty - kMinColumnId;
1780 long itemNum = 0;
1781 long otherItemNum = 0;
1782
1783 wxListCtrl* list = wxDynamicCast( GetPeer() , wxListCtrl );
1784
1785 // means we need to
1786 if (colId >= 0)
1787 {
1788 if (!m_isVirtual)
1789 {
1790 wxMacListCtrlItem* item = (wxMacListCtrlItem*)itemOneID;
1791 wxMacListCtrlItem* otherItem = (wxMacListCtrlItem*)itemTwoID;
1792 wxListCtrlCompare func = list->GetCompareFunc();
1793 if (func != NULL && item->HasColumnInfo(colId) && otherItem->HasColumnInfo(colId))
1794 return func(item->GetColumnInfo(colId)->GetData(), otherItem->GetColumnInfo(colId)->GetData(), list->GetCompareFuncData()) >= 0;
1795
1796 itemNum = item->GetOrder();
1797 otherItemNum = otherItem->GetOrder();
1798 if (item->HasColumnInfo(colId))
1799 itemText = item->GetColumnInfo(colId)->GetText();
1800 if (otherItem->HasColumnInfo(colId))
1801 otherItemText = otherItem->GetColumnInfo(colId)->GetText();
1802 }
1803 else
1804 {
1805 itemNum = (long)itemOneID;
1806 otherItemNum = (long)itemTwoID;
1807 itemText = list->OnGetItemText( itemNum-1, colId );
1808 otherItemText = list->OnGetItemText( otherItemNum-1, colId );
1809
1810 }
1811
1812 DataBrowserSortOrder sort;
1813 verify_noerr(GetSortOrder(&sort));
1814
1815 if ( sort == kDataBrowserOrderIncreasing )
1816 {
1817 retval = itemText.CmpNoCase( otherItemText ) > 0;
1818 }
1819 else if ( sort == kDataBrowserOrderDecreasing )
1820 {
1821 retval = itemText.CmpNoCase( otherItemText ) < 0;
1822 }
1823 }
1824 else{
1825 // fallback for undefined cases
1826 retval = itemOneID < itemTwoID;
1827 }
1828
1829 return retval;
1830 }
1831
1832 wxMacDataBrowserListCtrlControl::~wxMacDataBrowserListCtrlControl()
1833 {
1834 }
1835
1836 void wxMacDataBrowserListCtrlControl::MacSetColumnInfo( unsigned int row, unsigned int column, wxListItem* item )
1837 {
1838 wxMacDataItem* dataItem = GetItemFromLine(row);
1839 if (item)
1840 {
1841 wxMacListCtrlItem* listItem = dynamic_cast<wxMacListCtrlItem*>(dataItem);
1842 listItem->SetColumnInfo( column, item );
1843 UpdateState(dataItem, item);
1844 }
1845 }
1846
1847 // apply changes that need to happen immediately, rather than when the
1848 // databrowser control fires a callback.
1849 void wxMacDataBrowserListCtrlControl::UpdateState(wxMacDataItem* dataItem, wxListItem* listItem)
1850 {
1851 bool isSelected = IsItemSelected( dataItem );
1852 bool isSelectedState = (listItem->GetState() == wxLIST_STATE_SELECTED);
1853
1854 // toggle the selection state if wxListInfo state and actual state don't match.
1855 if ( isSelected != isSelectedState )
1856 {
1857 DataBrowserSetOption options = kDataBrowserItemsAdd;
1858 if (!isSelectedState)
1859 options = kDataBrowserItemsRemove;
1860 SetSelectedItem(dataItem, options);
1861 }
1862 // TODO: Set column width if item width > than current column width
1863 }
1864
1865 void wxMacDataBrowserListCtrlControl::MacGetColumnInfo( unsigned int row, unsigned int column, wxListItem& item )
1866 {
1867 wxMacDataItem* dataItem = GetItemFromLine(row);
1868 // CS should this guard against dataItem = 0 ? , as item is not a pointer if (item) is not appropriate
1869 //if (item)
1870 {
1871 wxMacListCtrlItem* listItem = dynamic_cast<wxMacListCtrlItem*>(dataItem);
1872 wxListItem* oldItem = listItem->GetColumnInfo( column );
1873
1874 long mask = item.GetMask();
1875 if ( !mask )
1876 // by default, get everything for backwards compatibility
1877 mask = -1;
1878
1879 if ( mask & wxLIST_MASK_TEXT )
1880 item.SetText(oldItem->GetText());
1881 if ( mask & wxLIST_MASK_IMAGE )
1882 item.SetImage(oldItem->GetImage());
1883 if ( mask & wxLIST_MASK_DATA )
1884 item.SetData(oldItem->GetData());
1885 if ( mask & wxLIST_MASK_STATE )
1886 item.SetState(oldItem->GetState());
1887 if ( mask & wxLIST_MASK_WIDTH )
1888 item.SetWidth(oldItem->GetWidth());
1889 if ( mask & wxLIST_MASK_FORMAT )
1890 item.SetAlign(oldItem->GetAlign());
1891
1892 item.SetTextColour(oldItem->GetTextColour());
1893 item.SetBackgroundColour(oldItem->GetBackgroundColour());
1894 item.SetFont(oldItem->GetFont());
1895 }
1896 }
1897
1898 void wxMacDataBrowserListCtrlControl::MacInsertItem( unsigned int n, wxListItem* item )
1899 {
1900 wxMacDataItemBrowserControl::MacInsert(n, item->GetText());
1901 MacSetColumnInfo(n, 0, item);
1902 }
1903
1904 wxMacDataItem* wxMacDataBrowserListCtrlControl::CreateItem()
1905 {
1906 return new wxMacListCtrlItem();
1907 }
1908
1909 wxMacListCtrlItem::wxMacListCtrlItem()
1910 {
1911 m_rowItems = wxListItemList();
1912 }
1913
1914 int wxMacListCtrlItem::GetColumnImageValue( unsigned int column )
1915 {
1916 return GetColumnInfo(column)->GetImage();
1917 }
1918
1919 void wxMacListCtrlItem::SetColumnImageValue( unsigned int column, int imageIndex )
1920 {
1921 GetColumnInfo(column)->SetImage(imageIndex);
1922 }
1923
1924 const wxString& wxMacListCtrlItem::GetColumnTextValue( unsigned int column )
1925 {
1926 if ( column == 0 )
1927 return GetLabel();
1928
1929 return GetColumnInfo(column)->GetText();
1930 }
1931
1932 void wxMacListCtrlItem::SetColumnTextValue( unsigned int column, const wxString& text )
1933 {
1934 GetColumnInfo(column)->SetText(text);
1935
1936 // for compatibility with superclass APIs
1937 if ( column == 0 )
1938 SetLabel(text);
1939 }
1940
1941 wxListItem* wxMacListCtrlItem::GetColumnInfo( unsigned int column )
1942 {
1943 wxListItemList::compatibility_iterator node = m_rowItems.Item( column );
1944 wxASSERT_MSG( node, _T("invalid column index in wxMacListCtrlItem") );
1945
1946 return node->GetData();
1947 }
1948
1949 bool wxMacListCtrlItem::HasColumnInfo( unsigned int column )
1950 {
1951 return m_rowItems.GetCount() > column;
1952 }
1953
1954 void wxMacListCtrlItem::SetColumnInfo( unsigned int column, wxListItem* item )
1955 {
1956
1957 if ( column >= m_rowItems.GetCount() )
1958 {
1959 wxListItem* listItem = new wxListItem(*item);
1960 m_rowItems.Append( listItem );
1961 }
1962 else
1963 {
1964 wxListItem* listItem = GetColumnInfo( column );
1965 long mask = item->GetMask();
1966 if (mask & wxLIST_MASK_TEXT)
1967 listItem->SetText(item->GetText());
1968 if (mask & wxLIST_MASK_DATA)
1969 listItem->SetData(item->GetData());
1970 if (mask & wxLIST_MASK_IMAGE)
1971 listItem->SetImage(item->GetImage());
1972 if (mask & wxLIST_MASK_STATE)
1973 listItem->SetState(item->GetState());
1974 if (mask & wxLIST_MASK_FORMAT)
1975 listItem->SetAlign(item->GetAlign());
1976 if (mask & wxLIST_MASK_WIDTH)
1977 listItem->SetWidth(item->GetWidth());
1978 }
1979 }
1980
1981 #endif // wxUSE_LISTCTRL