{"id":142,"date":"2006-06-07T10:44:57","date_gmt":"2006-06-07T17:44:57","guid":{"rendered":"http:\/\/www.red-sweater.com\/blog\/142\/drag-if-you-want-to"},"modified":"2006-06-07T11:01:56","modified_gmt":"2006-06-07T18:01:56","slug":"drag-if-you-want-to","status":"publish","type":"post","link":"https:\/\/redsweater.com\/blog\/142\/drag-if-you-want-to","title":{"rendered":"Drag If You Want To"},"content":{"rendered":"<p>Handling drags to your Cocoa application is pretty simple: you just have to implement the informal NSDraggingDestination protocol in some view hierarchy object that has been registered for particular drag types. <\/p>\n<p>\nUsually a drag is handled by a specific view that can make sense of the drag&#8217;s data type. For instance, an NSTextView will accept text from the dragging clipboard and plop it down into the edited documnt. Most of the time, drags should only be handled by these specific views, but sometimes it makes sense to intercept &#8220;unhandled drags&#8221; and make something useful happen for the user. For instance, when a user drags a URL to a web browser, you might as well assume that they want to open the URL, even if they carelessly let go somewhere in the browser window&#8217;s non-descript background texture.\n<\/p>\n<p>\nApple made it easy to handle such &#8220;drags of last resort&#8221; at the window level, by kindly passing along all dragging offers from NSWindow to the window&#8217;s delegate. If your delegate simply implements the required methods, you can snag and make useful any drag that would otherwise bounce back to the originator.\n<\/p>\n<p>\nBut handling such non-specific drags at any level lower than NSWindow becomes tricky. There is no corresponding &#8220;delegation&#8221; when it comes to NSView, so drags that hover over or release in an uncooperative view will not be accepted, even if you&#8217;ve asked the view to register for the desired types.\n<\/p>\n<p>\nWhen you are faced with the desire to &#8220;stake out&#8221; part of a window&#8217;s real-estate as a drag destination, the simplest way to do so is to sneak a custom NSView into the hierarchy that <em>does<\/em> accept drags. But instead of writing a custom view every time you need to do this, why don&#8217;t we follow AppKit&#8217;s lead and write our own NSView subclass that makes use of the delegation strategy.\n<\/p>\n<p>\n<a href=\"http:\/\/www.red-sweater.com\/blog\/downloads\/RSDragDelegationView.zip\">RSDragDelegationView<\/a> (MIT License) is a very simple NSView subclass that does just this. To mark the background area of a part of your UI as dragging-receptive, just select items in Interface Builder and &#8220;make subviews of custom view.&#8221; Set the class of the custom NSView to RSDragDelegationView (drag the header file to your IB document to teach it about the class), and connect the &#8220;mDraggingDelegate&#8221; outlet to the delegate instance. From your delegate code, you can now ask the custom view to register for your desired drag types, and all dragging messages will be forwarded on to you as they&#8217;re received.\n<\/p>\n<p>\nAn example of where I use this in my own code is in the &#8220;product registration pane.&#8221; I want to accept drags generously, because I advise users to drag their product registration email into the window after they&#8217;ve paid. If they can&#8217;t figure it out, they might email me, and while I&#8217;m always up for a nice customer chat, I&#8217;d just as soon talk about other things. There&#8217;s no reason for me not to make this as error-proof as possible, so I stake out the entire space of the tab view item in which the pane is installed. By using a custom drag-enabled view I avoid inflicting drag receptivity on the rest of the window, and keep the dragging-related code right near the controller code for the UI that needs it. If another section of the window&#8217;s UI needs to accept drags liberally, the same trick can be applied without complicating drag-handling at the window level.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Handling drags to your Cocoa application is pretty simple: you just have to implement the informal NSDraggingDestination protocol in some view hierarchy object that has been registered for particular drag types. Usually a drag is handled by a specific view that can make sense of the drag&#8217;s data type. For instance, an NSTextView will accept [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,29,15],"tags":[],"class_list":["post-142","post","type-post","status-publish","format-standard","hentry","category-cocoa","category-free-code","category-programming"],"_links":{"self":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":0,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"wp:attachment":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}