Platforms to show: All Mac Windows Linux Cross-Platform

/MacControls/Custom NSView Drag


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

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

This example is the version from Mon, 22th Feb 2015.

Project "Custom NSView Drag.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control CocoaControlMBS1 Inherits CocoaControlMBS
ControlInstance CocoaControlMBS1 Inherits CocoaControlMBS
EventHandler Function GetView() As NSViewMBS n=new CustomNSView(0,0,me.Width,me.Height) n.list = list Return n End EventHandler
End Control
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
EventHandler Sub Open() if not CocoaControlMBS1.Available then MsgBox "This example requires Mac OS X 10.5." quit end if End EventHandler
Property n As CustomNSView
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class CustomNSView Inherits CustomNSViewMBS
EventHandler Sub DrawRect(g as NSGraphicsMBS, left as double, top as double, width as double, height as double) g.SetColorRGB 1.0,1.0,1.0,1.0 g.fillRect left,top,width,height 'g.drawInRect(image, 0, 0, myImage.width, myImage.height, 0, 0, anotherImage.width, anotherImage.height, myGraphics.NSCompositeSourceOver, 1.0) End EventHandler
EventHandler Function acceptsFirstMouse(e as NSEventMBS) As boolean Return true End EventHandler
EventHandler Function acceptsFirstResponder() As boolean Return true End EventHandler
EventHandler Function becomeFirstResponder() As boolean Return true End EventHandler
EventHandler Function canBecomeKeyView() As boolean Return true End EventHandler
EventHandler Sub draggingSessionEndedAtPoint(session as NSDraggingSessionMBS, screenPoint as NSPointMBS, operation as integer) log CurrentMethodName+" "+Str(screenPoint.X)+"/"+Str(screenPoint.Y) End EventHandler
EventHandler Sub draggingSessionMovedToPoint(session as NSDraggingSessionMBS, screenPoint as NSPointMBS) log CurrentMethodName+" "+Str(screenPoint.X)+"/"+Str(screenPoint.Y) End EventHandler
EventHandler Function draggingSessionSourceOperationMaskForDraggingContext(session as NSDraggingSessionMBS, context as integer) As integer log CurrentMethodName if context = NSDraggingSessionMBS.NSDraggingContextOutsideApplication then Return NSDraggingInfoMBS.NSDragOperationCopy end if if context = NSDraggingSessionMBS.NSDraggingContextWithinApplication then Return NSDraggingInfoMBS.NSDragOperationCopy end if End EventHandler
EventHandler Sub draggingSessionWillBeginAtPoint(session as NSDraggingSessionMBS, screenPoint as NSPointMBS) log CurrentMethodName+" "+Str(screenPoint.X)+"/"+Str(screenPoint.Y) End EventHandler
EventHandler Function ignoreModifierKeysForDraggingSession(session as NSDraggingSessionMBS) As boolean log CurrentMethodName End EventHandler
EventHandler Function mouseDown(e as NSEventMBS, x as double, y as double) As boolean logo = LogoMBS(500) image = new NSImageMBS(logo) '/*------------------------------------------------------ 'catch mouse down events in order to start drag '--------------------------------------------------------*/ dataProvider = new MyNSPasteboardItemDataProviderMBS dataProvider.image = image '/* Dragging operation occur within the context of a special pasteboard (NSDragPboard). '* All items written or read from a pasteboard must conform to NSPasteboardWriting or '* NSPasteboardReading respectively. NSPasteboardItem implements both these protocols '* and is as a container for any object that can be serialized to NSData. */ item = new NSPasteboardItemMBS '/* Our pasteboard item will support public.tiff, public.pdf, and our custom UTI (see comment in -draggingEntered) '* representations of our data (the image). Rather than compute both of these representations now, promise that '* we will provide either of these representations when asked. When a receiver wants our data in one of the above '* representations, we'll get a call to the NSPasteboardItemDataProvider protocol method –pasteboard:item:provideDataForType:. */ dim types() as string types.Append NSPasteboardMBS.NSPasteboardTypeTIFF 'types.Append NSPasteboardMBS.NSPasteboardTypePDF call item.setDataProviderForType(dataprovider, types) //create a new NSDraggingItem with our pasteboard item. dragItem = new NSDraggingItemMBS(item) '/* The coordinates of the dragging frame are relative to our view. Setting them to our view's bounds will cause the drag image '* to be the same size as our view. Alternatively, you can set the draggingFrame to an NSRect that is the size of the image in '* the view but this can cause the dragged image to not line up with the mouse if the actual image is smaller than the size of the '* our view. */ dim draggingRect as NSRectMBS = self.bounds '/* While our dragging item is represented by an image, this image can be made up of multiple images which '* are automatically composited together in painting order. However, since we are only dragging a single '* item composed of a single image, we can use the convince method below. For a more complex example '* please see the MultiPhotoFrame sample. */ dragItem.setDraggingFrame(draggingRect, image) //create a dragging session with our drag item and ourself as the source. dim dragItems() as Variant dragItems.Append dragItem draggingSession = self.beginDraggingSessionWithItems(dragItems, e, self) //causes the dragging item to slide back to the source if the drag fails. if draggingSession<>Nil then draggingSession.animatesToStartingPositionsOnCancelOrFail = true draggingSession.draggingFormation = draggingSession.NSDraggingFormationNone end if Return true End EventHandler
Sub log(s as string) window1.List.AddRow s window1.List.ScrollPosition = window1.List.ListCount End Sub
Property dataProvider As MyNSPasteboardItemDataProviderMBS
Property dragItem As NSDraggingItemMBS
Property draggingSession As NSDraggingSessionMBS
Property image As NSImageMBS
Property item As NSPasteboardItemMBS
Property list As listbox
Property logo As Picture
End Class
Class MyNSPasteboardItemDataProviderMBS Inherits NSPasteboardItemDataProviderMBS
EventHandler Sub provideDataForType(Pasteboard as NSPasteboardMBS, item as NSPasteboardItemMBS, type as string) '/*------------------------------------------------------ 'method called by pasteboard to support promised 'drag types. '--------------------------------------------------------*/ '//sender has accepted the drag and now we need to send the data for the type we promised if type = NSPasteboardMBS.NSPasteboardTypeTIFF then dim tiff as MemoryBlock = image.TIFFRepresentation //set data for TIFF type on the pasteboard as requested Pasteboard.dataForType(NSPasteboardMBS.NSPasteboardTypeTIFF) = tiff end if End EventHandler
Property image As NSImageMBS
End Class
End Project

See also:

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


The biggest plugin in space...