Cocoa Programming for Mac OS X - Aaron Hillegass [81]
- (IBAction)myAction:(id)sender {
id theCell = [sender selectedCell];
int theTag = [theCell tag];
...
}
The cell’s tag can be set in Interface Builder.
Cells are used in several other types of objects. The data in a cell-based NSTableView, for example, is drawn by cells.
For the More Curious: isFlipped
Both PDF and PostScript use the standard Cartesian coordinate system, whereby y increases as you move up the page. Quartz follows this model by default. The origin is usually at the lower-left corner of the view.
For some types of drawing, the math becomes easier if the upper-left corner is the origin and y increases as you move down the page. We say that such a view is flipped.
To flip a view, you override isFlipped in your view class to return YES:
- (BOOL)isFlipped
{
return YES;
}
While we are discussing the coordinate system, note that x- and y-coordinates are measured in points. A point is typically defined as “72.0 points = 1 inch.” In reality, by default “1.0 point = 1 pixel” on your screen. You can, however, change the size of a point by changing the coordinate system:
// Make everything in the view twice as large
NSSize newScale;
newScale.width = 2.0;
newScale.height = 2.0;
[myView scaleUnitSquareToSize:newScale];
[myView setNeedsDisplay:YES];
Challenge
NSBezierPath can also draw Bezier curves. Replace the straight lines with randomly curved ones. (Hint: Look in the documentation for NSBezierPath.)
Chapter 18. Images and Mouse Events
In the previous chapter, you drew lines connecting random points. A more interesting project would have been to write a drawing application. To write this sort of application, you will need to be able to get and handle mouse events.
NSResponder
NSView inherits from NSResponder. All the event-handling methods are declared in NSResponder. We will discuss keyboard events in the next chapter. For now, we are interested just in mouse events. NSResponder declares these methods:
- (void)mouseDown:(NSEvent *)theEvent;
- (void)rightMouseDown:(NSEvent *)theEvent;
- (void)otherMouseDown:(NSEvent *)theEvent;
- (void)mouseUp:(NSEvent *)theEvent;
- (void)rightMouseUp:(NSEvent *)theEvent;
- (void)otherMouseUp:(NSEvent *)theEvent;
- (void)mouseDragged:(NSEvent *)theEvent;
- (void)scrollWheel:(NSEvent *)theEvent;
- (void)rightMouseDragged:(NSEvent *)theEvent;
- (void)otherMouseDragged:(NSEvent *)theEvent;
Notice that the argument is always an NSEvent object.
NSEvent
An event object has all the information about what the user did to trigger the event. When you are dealing with mouse events, you might be interested in the following methods:
- (NSPoint)locationInWindow
Returns the location of the mouse event.
- (NSUInteger)modifierFlags
Tells you which modifier keys the user is holding down on the keyboard. This enables the programmer to tell a Control-click from a Shift-click, for example. The code would look like this:
- (void)mouseDown:(NSEvent *)e
{
NSUInteger flags;
flags = [e modifierFlags];
if (flags & NSControlKeyMask) {
...handle control click...
}
if (flags & NSShiftKeyMask) {
...handle shift click...
}
}
Here are the constants that you commonly AND (&) against the modifier flags:
NSShiftKeyMask
NSControlKeyMask
NSAlternateKeyMask
NSCommandKeyMask
- (NSTimeInterval)timestamp
Gives the time interval in seconds between the time the machine booted and the time of the event. NSTimeInterval is a double.
- (NSWindow *)window
Returns the window associated with the event.
- (NSInteger)clickCount
Indicates whether the click was single, double, or triple.
- (float)pressure
Returns the pressure if the user is using an input device that gives pressure (a tablet, for example). It is between 0 and 1.
- (float)deltaX;
- (float)deltaY;
- (float)deltaZ;
Give the change in the position of the mouse or scroll wheel.
Getting Mouse Events
To get mouse events,