Drag-and-Drop

Drag-and-drop is another way for applications to interoperate: the user simply drags information from one application into another. Drag-and-drop requires more work on the part of the user than the Services system, because the data must be manually dragged across application boundaries. Drag-and-drop is also less powerful than Services because it does not offer the bidirectional interaction of services that receive information, act on it, and return a result. Nevertheless, drag-and-drop is easier than Services for many people to understand, largely because drag-and-drop is more familiar: it is present both in Windows and in previous versions of the Macintosh operating system.

Although we’ve already done a lot in this chapter, with just a little more work we can implement drag-and-drop functionality as well. So let’s do it!

Being a Drag-and-Drop Source

We can make GraphPaper a drag-and-drop source by making a few small changes to the GraphView class. Unfortunately, one aspect of this process will be a little awkward because of our decision earlier in this chapter and the previous one to have the PDF- and TIFF-generation functionality centralized in the Controller class. (Fixing this design flaw is left to the user as an exercise.)

  1. Add the following three new method declarations to the GraphView.h interface file:

                            -(BOOL)acceptsFirstMouse:(NSEvent *)theEvent;
                            -(NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)flag;
                            -(void)mouseDragged:(NSEvent *)theEvent;

Get Building Cocoa Applications: A Step by Step Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.