Platforms to show: All Mac Windows Linux Cross-Platform

/MacControls/NSCollectionView Test


Required plugins for this example: MBS MacCocoa Plugin, MBS MacBase Plugin, MBS MacControls Plugin, MBS Main Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacControls/NSCollectionView Test

This example is the version from Tue, 8th Mar 2021.

Project "NSCollectionView Test.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open() End EventHandler
End Class
Class MainWindow Inherits Window
Control CollectionControl Inherits NSCollectionViewControlMBS
ControlInstance CollectionControl Inherits NSCollectionViewControlMBS
EventHandler Sub ItemPrepareForReuse(item as NSCollectionViewItemMBS) System.DebugLog CurrentMethodName+ " "+ str(item.Handle) // item will be reused, so remove it from our CollectionViewItem Dim it As CollectionViewItem = FindCollectionViewItem(item) If it <> Nil Then it.NSView.removeFromSuperview it.ViewItem = Nil End If End EventHandler
EventHandler Sub Open() Me.ScrollView.autohidesScrollers = True collectionView = Me.view collectionView.selectable = True collectionView.allowsMultipleSelection = True collectionView.setBackgroundColors Array(NSColorMBS.lightGrayColor) Dim flowLayout As New NSCollectionViewFlowLayoutMBS flowLayout.itemSize = New NSSizeMBS(300.0, 300.0) flowLayout.sectionInset = New NSEdgeInsetsMBS(10.0, 20.0, 10.0, 20.0) flowLayout.minimumInteritemSpacing = 20.0 flowLayout.minimumLineSpacing = 20.0 collectionView.collectionViewLayout = flowLayout End EventHandler
EventHandler Sub didDeselectItems(indexPaths() as NSIndexPathMBS) // items got deselected, so we make text non-bold System.DebugLog CurrentMethodName For Each indexPath As NSIndexPathMBS In indexPaths Dim item As CollectionViewItem = items(indexPath.item) item.ImageContainer.NameField.Bold = False Next End EventHandler
EventHandler Sub didEndDisplayingItem(item as NSCollectionViewItemMBS, indexPath as NSIndexPathMBS) // item left viewing area System.DebugLog CurrentMethodName+ " "+ str(item.Handle) End EventHandler
EventHandler Sub didSelectItems(indexPaths() as NSIndexPathMBS) // items got selected, so we make text bold System.DebugLog CurrentMethodName For Each indexPath As NSIndexPathMBS In indexPaths Dim item As CollectionViewItem = items(indexPath.item) item.ImageContainer.NameField.Bold = true Next End EventHandler
EventHandler Function itemForRepresentedObject(indexPath as NSIndexPathMBS) As NSCollectionViewItemMBS // we need an item to show an image System.DebugLog CurrentMethodName+ " "+ str(indexPath.item) // lookup the image item for the path. // Could use indexPath.section if you have multiple sections Dim item As CollectionViewItem = items(indexPath.item) // make a new item. May recycle old items Dim ViewItem As NSCollectionViewItemMBS = collectionView.makeItem(indexPath) // load image if needed item.load ViewItem item.ImageContainer.NameField.Text = Str(indexPath.item) Return ViewItem Exception n As NSExceptionMBS System.DebugLog n.message End EventHandler
EventHandler Function numberOfItemsInSection(section as Integer) As Integer // return number of items per section // as we only show image list, we return number of images Return items.Ubound+1 End EventHandler
EventHandler Function numberOfSections() As Integer // return how many sections. This example only has one Return 1 End EventHandler
EventHandler Sub willDisplayItem(item as NSCollectionViewItemMBS, indexPath as NSIndexPathMBS) // item will show soon. Prepare for drawing soon System.DebugLog CurrentMethodName + " "+ Str(item.Handle) End EventHandler
End Control
EventHandler Sub Open() // find some image files folder = GetFolderItem("/System/Library/Desktop Pictures/", folderitem.PathTypeNative) If folder <> Nil Then // use picture folder Dim c As Integer = folder.Count For i As Integer = 1 To c Dim file As FolderItem = folder.Item(i) If file <> Nil Then If Right(file.name, 4) = ".jpg" Then AddFile file End If End If Next end if If items.Ubound = -1 Then // no pictures? Break If DebugBuild Then // for debugging you can use dummy items For i As Integer = 1 To 100 Dim c As New CollectionViewItem items.Append c Next End If End If Title = str(items.Ubound+1)+" files" collectionView.reloadData End EventHandler
Sub AddFile(file as FolderItem) Dim c As New CollectionViewItem c.imageFile = file items.Append c End Sub
Function FindCollectionViewItem(item as NSCollectionViewItemMBS) As CollectionViewItem // find matching objects For Each it As CollectionViewItem In items If it is Nil Then // igmore Elseif it.ViewItem Is item Then Return it End If Next // not found Return Nil End Function
Property collectionView As NSCollectionViewMBS
Property folder As FolderItem
Property items() As CollectionViewItem
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class CollectionViewItem
Sub Constructor() IDCounter = IDCounter + 1 ID = str(IDCounter) End Sub
Sub Load(ViewItem as NSCollectionViewItemMBS) Self.ViewItem = ViewItem // lets make a new container if this is first time used If NSView = Nil Then // first time this item is shown ImageContainer = New ImageContainer // place new item outside visible area ImageContainer.EmbedWithin(MainWindow, -10000, 10000, ImageContainer.Width, ImageContainer.Height) Dim n As NSViewMBS = ImageContainer.NSViewMBS If n = Nil Then Break // no view? End If Dim autoresizingMask As Integer = NSViewMBS.NSViewWidthSizable + NSViewMBS.NSViewHeightSizable // remove from window where xojo placed it above n.removeFromSuperview n.frame = New NSRectMBS(0, 0, ImageContainer.Width, ImageContainer.Height) NSView = n End If If imageFile <> Nil Then // load image if needed Dim p As Picture = Picture.Open(ImageFile) ImageContainer.ImageField.Image = p ImageContainer.NameField.Text = ImageFile.DisplayName End If // now add it and show it in the controller Dim view As NSViewMBS = ViewItem.view view.addSubview NSView NSView.needsDisplay = True End Sub
Property ID As string
Property Shared IDCounter As Integer
Property ImageContainer As ImageContainer
Property NSView As NSViewMBS
Property ViewItem As NSCollectionViewItemMBS
Property imageFile As FolderItem
End Class
Class ImageContainer Inherits ContainerControl
Control ImageField Inherits ImageWell
ControlInstance ImageField Inherits ImageWell
End Control
Control NameField Inherits Label
ControlInstance NameField Inherits Label
End Control
End Class
End Project

The items on this page are in the following plugins: MBS MacControls Plugin.


The biggest plugin in space...