Annotation of Examples/AppKit/Draw/Dragging.rtf, revision 1.1.1.1

1.1       root        1: {\rtf0\ansi{\fonttbl\f0\fswiss Helvetica;\f1\fmodern Courier;\f2\fnil Times-Roman;\f3\fmodern Ohlfs;}
                      2: \paperw11340
                      3: \paperh8400
                      4: \margl120
                      5: \margr120
                      6: {\colortbl;\red0\green0\blue0;}
                      7: \pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\f0\b0\i0\ulnone\fs48\fc1\cf1 Dragging stuff into Draw (file: 
                      8: \f1 gvDrag.m
                      9: \f0 )\
                     10: 
                     11: \fs24\fc0\cf0 \
                     12: 
                     13: \f2\fs28 This file is intended to handle things that are dragged into a Draw document (files and colors mostly).  There are a couple of things to keep in mind about the files case.  The first is whether the file being dragged in is being ``linked'' (via ObjectLinks).  The second is whether we are going to display the contents of the file or just the file's icon (or even just the little link button icon).\
                     14: \
                     15: 
                     16: \f0\fs36\fc1\cf1 Registering\
                     17: 
                     18: \f2\fs28\fc0\cf0 \
                     19: The basic idea is that we call 
                     20: \f1\b\fc1\cf1 registerForDragging
                     21: \f2\b0\fc0\cf0  in 
                     22: \f1\b\fc1\cf1 GraphicView
                     23: \f2\b0\fc0\cf0 's 
                     24: \f1\b\fc1\cf1 initFrame:
                     25: \f2\b0\fc0\cf0  and in 
                     26: \f1\fc1\cf1 awake
                     27: \f2\fc0\cf0 .  That calls the 
                     28: \f1\b\fc1\cf1 registerForDraggedTypes:count:
                     29: \f2\b0\fc0\cf0  method to let the system know which types of things (represented by 
                     30: \f1\b\fc1\cf1 Pasteboard
                     31: \f2\b0\fc0\cf0  types) we are interested in having dragged into our view.\
                     32: \
                     33: 
                     34: \f0\fs36\fc1\cf1 Reacting to a Dragging Session\
                     35: 
                     36: \f2\fs28\fc0\cf0 \
                     37: Next, we implement some of the dragging protocol as follows:\
                     38: \
                     39: 1. 
                     40: \f1\b\fc1\cf1 draggingEntered:
                     41: \f2\b0\fc0\cf0 \
                     42: \
                     43: This is sent to us the first time something that we are interested in enters our view.  We respond by letting the system know whether we are actually interested in the dragged thing at the time the thing is dragged into our view (depending both on the state of our view and where the drag is) and what operations we will support.\
                     44: \
                     45: Basically, if the dragged thing is a color, we return that we will perform the ``generic'' thing on that color if dropped only if the 
                     46: \f1\b\fc1\cf1 Graphic
                     47: \f2\b0\fc0\cf0  that is currently being dragged over can deal with having a color dropped on it (the 
                     48: \f1\b\fc1\cf1 acceptsColor:atPoint:
                     49: \f2\b0\fc0\cf0  method is what we have created to figure this out).\
                     50: \
                     51: Otherwise, we will accept the dragged thing if it is a file of any sort or if 
                     52: \f1\b\fc1\cf1 NXImage
                     53: \f2\b0\fc0\cf0  says that it can make some sense out of the 
                     54: \f1\b\fc1\cf1 Pasteboard
                     55: \f2\b0\fc0\cf0  of stuff being dragged in (this last case is very rare, applications rarely let you drag raw EPS out of themselves and into other apps--maybe more applications will start doing this in the future, we'll have to see).\
                     56: \
                     57: In reality, we shouldn't say that we can accept 
                     58: \b any
                     59: \b0  sort of file.  We can really only accept files that 
                     60: \f1\b\fc1\cf1 NXImage
                     61: \f2\b0\fc0\cf0  can handle, RTF files, and (most of the time) plain files (because their usually ASCII).  We can easily determine if a filename is one 
                     62: \f1\b\fc1\cf1 NXImage
                     63: \f2\b0\fc0\cf0  can handle (and we can obviously tell whether it's an RTF file), but the only way we can tell if a file is a ``plain'' file (i.e. not a WriteNow file or some such) is by asking the Workspace Manager (see the message we send to 
                     64: \f1\b\fc1\cf1 [Application workspace]
                     65: \f2\b0\fc0\cf0  below).  Unfortunately, since the drag protocol is a synchronous blocking protocol between the app and the Workspace, we can't talk to Workspace in the middle of the drag (what a drag, huh?).\
                     66: \
                     67: So, what we do is just accept any file, and, if you drop a WriteNow file in (without linking, of course), then we just ignore it.\
                     68: \
                     69: Speaking of linking, if the link key (the Control key) is down during the drag, then we really can accept any file because we can just drop its icon into the Draw document (like Mail does).  Then we just use ObjectLinks to make the double-clicking on it open the file up.  We also provide the user the option of creating a little Link Button instead of the file's icon.\
                     70: \
                     71: 2. 
                     72: \f1\b\fc1\cf1 draggingUpdated:
                     73: \f2\b0\fc0\cf0 \
                     74: \
                     75: This is called repeatedly as the dragged thing is dragged about our view.  Again, just like 
                     76: \f1\b\fc1\cf1 draggingEntered:
                     77: \f2\b0\fc0\cf0  we return whether we are interested in accepting the dragged thing depending on where it currently is in our view and our current state.  This method must be pretty fast, and it is, because we have already examined the contents of the 
                     78: \f1\b\fc1\cf1 Pasteboard
                     79: \f2\b0\fc0\cf0  in 
                     80: \f1\b\fc1\cf1 draggingEntered:
                     81: \f2\b0\fc0\cf0 .\
                     82: \
                     83: Basically all we really do of interest here is constantly reevaluate whether the 
                     84: \f1\b\fc1\cf1 Graphic
                     85: \f2\b0\fc0\cf0  underneath the dragging is willing to accept a color (but only, of course, if it is a color we are dragging).\
                     86: \
                     87: 3. 
                     88: \f1\b\fc1\cf1 performDragOperation:
                     89: \f2\b0\fc0\cf0 \
                     90: \
                     91: This method is called just as the user lets go of the thing she's dragging.  This is normally where you do the work that the drop of the dragged thing causes.  And, indeed, if the dropped thing is a color, we update the color of the dropped-on 
                     92: \f1\b\fc1\cf1 Graphic
                     93: \f2\b0\fc0\cf0  here.\
                     94: \
                     95: Unfortunately, often dropping something on a Draw document causes two very time-consuming things to happen.  First, a complicated PostScript or TIFF image might have to be drawn.  Second, a question might need to be asked of the user about how to deal with the dropped thing (this happens when you link a file in and Draw wants to know whether you want the contents of the file to appear in Draw, the file's icon, or a link button).\
                     96: \
                     97: Thus, we don't do the work that the drop results in in this method, instead, we wait until after the drag and drop is fully complete (as far as the system is concerned) and do the work in ...\
                     98: \
                     99: 4. 
                    100: \f1\b\fc1\cf1 concludeDragOperation:
                    101: \f2\b0\fc0\cf0 \
                    102: \
                    103: This is called after the drag and drop is completely done (and Workspace is out of the loop).  Thus, if there's an error, we can't do the ``slide-back'' animation.  It's a bummer, but there's really no way around it in 3.0.\
                    104: \
                    105: This method is implemented by first looping through any filenames that are in the dragged 
                    106: \f1\b\fc1\cf1 Pasteboard
                    107: \f2\b0\fc0\cf0  looking either for 
                    108: \f1\b\fc1\cf1 NXDataLink
                    109: \f2\b0\fc0\cf0  files (
                    110: \f1\b\fc1\cf1 .objlink
                    111: \f2\b0\fc0\cf0  files) which represent links (these are similar to the little things dropped in the filesystem in the Publish/Subscribe mechanism) or for files that 
                    112: \f1\b\fc1\cf1 NXImage
                    113: \f2\b0\fc0\cf0  can handle (TIFF, EPS, other formats if you have filters lying around), or for RTF or plain files that the 
                    114: \f1\fc1\cf1 Text
                    115: \f2\fc0\cf0  object can handle.\
                    116: \
                    117: 
                    118: \f1\b\fc1\cf1 createGraphicForDraggedLink:at:
                    119: \f2\b0\fc0\cf0  handles the 
                    120: \f1\b\fc1\cf1 .objlink
                    121: \f2\b0\fc0\cf0  files, and 
                    122: \f1\b\fc1\cf1 createGraphicForDraggedFile:withIcon:at:andLink:
                    123: \f2\b0\fc0\cf0  handles all other files.  
                    124: \f1\b\fc1\cf1 createGraphicForDraggedLink:at:
                    125: \f2\b0\fc0\cf0  is implemented simply by instantiating the link from the file and then calling the one method in all of Draw that actually adds a linked thing to the document (
                    126: \f1\b\fc1\cf1 addLink:toGraphic:at:update:
                    127: \f2\b0\fc0\cf0  -- see 
                    128: \f1\b\fc1\cf1 gvLinks.m
                    129: \f2\b0\fc0\cf0 ).  
                    130: \f1\b\fc1\cf1 createGraphicForDraggedFile:withIcon:at:andLink:
                    131: \f2\b0\fc0\cf0  also calls that same method if we are linking the dragged file in.  The method called when you do a Paste and Link from the menu also calls that method.\
                    132: \
                    133: Finally, if we can't find any files that we can do anything with, we just call the same method that is called when you hit Paste in the menu (except that we go straight to handling only non-Draw formats).  Nice code reuse, huh?  Again, this last thing is pretty rare (at least it is today, who knows what tomorrow will bring?).\
                    134: \
                    135: By the way, you may will ask what this 
                    136: \f1\b\fc1\cf1 ERROR
                    137: \f2\b0\fc0\cf0  thing is all about.  Well, the create functions return 
                    138: \f1\b\fc1\cf1 YES
                    139: \f2\b0\fc0\cf0  if they were able to successfully incorporate something into the document, 
                    140: \f1\b\fc1\cf1 NO
                    141: \f2\b0\fc0\cf0  if they weren't, and 
                    142: \f1\b\fc1\cf1 ERROR
                    143: \f2\b0\fc0\cf0  if they found a problem with the thing being incorporated (i.e. a PostScript error in a dragged EPS file, for example).\
                    144: 
                    145: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.