Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [96]

By Root 893 0
is part of the Foundation framework. However, NSAttributedString’s drawInRect: method is part of a category from the AppKit framework. As a result, the documention for the methods on NSAttributedString are distributed between the two frameworks. There are also separate header files for NSAttributedString and its categories, which tends to cause some confusion.

For the More Curious: Declaring Private Methods


Often, you will have in your .m file methods defined that you do not want to advertise by declaring them in your .h file. These are known as private methods.

If you call a private method before you declare or define it, you will get a warning from the compiler. One common technique to prevent these warnings is to declare the private methods in a category at the beginning of the .m file:

#import "Megatron.h"

// Declare the private methods

@interface Megatron ()

- (void)blowTheLidOff;

- (void)putTheLidBackOn;

@end

@implementation Megatron

...actually implement all the private and public methods...

@end

Chapter 23. Drag-and-Drop


Drag-and-drop is little more than a flashy copy-and-paste operation. When the drag starts, some data is copied onto the dragging pasteboard. When the drop occurs, the data is read off the dragging pasteboard. The only thing that makes this technique trickier than copy-and-paste is that users need feedback: an image that appears as they drag, a view that becomes highlighted when they drag into it, and maybe a big gulping sound when they drop the image.

Several things can happen when data is dragged from one application to another: nothing may happen, a copy of the data may be created, or a link to the existing data may be created. Constants represent these operations:

NSDragOperationNone

NSDragOperationCopy

NSDragOperationLink

There are several other operations that you see less frequently:

NSDragOperationGeneric

NSDragOperationPrivate

NSDragOperationMove

NSDragOperationDelete

NSDragOperationEvery

Both the source and the destination must agree on the operation that will occur when the user drops the image.

When you add drag-and-drop to a view, there are two distinct parts of the change:

1. Make it a drag source.

2. Make it a drag destination.

Let’s take these steps separately. First, you will make your view a drag source. When that is working, you will make it a drag destination.

Make BigLetterView a Drag Source


When you finish this section, you will be able to drag a letter off the BigLetterView and drop it into any text editor. It will look like Figure 23.1.

Figure 23.1. Completed Application

To be a drag source, your view must implement draggingSourceOperationMaskForLocal:. This method declares what operations the view is willing to participate in as a source. Add the following method to BigLetterView.m:

- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal

{

return NSDragOperationCopy;

}

This method is automatically called twice: once with isLocal as YES, which determines what operations it is willing to participate in for destinations within your application; and a second time, with isLocal as NO, which determines what operations it is willing to participate in for destinations in other applications.

Starting a Drag


To start a drag operation, you will use a method on NSView:

- (void)dragImage:(NSImage *)anImage

at:(NSPoint)imageLoc

offset:(NSSize)mouseOffset

event:(NSEvent *)theEvent

pasteboard:(NSPasteboard *)pboard

source:(id)sourceObject

slideBack:(BOOL)slideBack

You will supply the method with the image to be dragged and the point at which you want the drag to begin. The event supplied should be the mouseDown event. The offset is completely ignored. The pasteboard is usually the standard drag pasteboard. If the drop does not occur, you can choose whether the icon should slide back to the place from which it came.

Add an instance variable to BigLetterView.h to hold the mouseDown event:

NSEvent *mouseDownEvent;

In BigLetterView.m,

Return Main Page Previous Page Next Page

®Online Book Reader