drawRect not called on PDFView subclass - cocoa

I have a custom PDFView subclass and have overridden -mouseDown, -mouseDragged etc to draw a rectangle over the view. Here is the code I am using:
#implementation MyPDFView {
NSPoint clickLocation;
NSRect selection;
}
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint clickLocationOnWindow = [self.window mouseLocationOutsideOfEventStream];
clickLocation = [self convertPoint:clickLocationOnWindow fromView:nil];
NSLog(#"%#", NSStringFromPoint(clickLocation));
}
- (void)mouseDragged:(NSEvent *)theEvent {
NSPoint mouseLocationOnWindow = [self.window mouseLocationOutsideOfEventStream];
NSPoint currentLocation = [self convertPoint:mouseLocationOnWindow fromView:nil];
CGFloat lowerX = fmin(clickLocation.x, currentLocation.x);
CGFloat lowerY = fmin(clickLocation.y, currentLocation.y);
CGFloat upperX = fmax(clickLocation.x, currentLocation.x);
CGFloat upperY = fmax(clickLocation.y, currentLocation.y);
selection = NSMakeRect(lowerX, lowerY, upperX-lowerX, upperY-lowerY);
[self setNeedsDisplay:YES];
NSLog(#"%#", NSStringFromRect(selection));
}
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
NSLog(#"drawRect");
NSBezierPath *bp = [NSBezierPath bezierPathWithRect:selection];
[[NSColor blueColor] set];
[bp fill];
}
#end
The NSRect is calculated correctly, but when I call [self setNeedsDisplay], -drawRect is never called, and the rectangle is never drawn.
Is there any reason why -drawRect is never called on a PDFView subclass?

I have a similar use case. According to the docs, override PDFView's drawPage method instead. Continue to call setNeedsDisplay on the PDFView. It works, but it's a little slow. Working on overlaying a view right now instead.

Related

Custom NSButtonCell, drawBezelWithFrame not called

I'm trying to figure out how to custom draw Buttons in Cocoa/OSX. Since my view is custom drawn I will not use IB and want to do it all in code. I created a subclass of NSButtonCell and a subclass of NSButton. In the Subclass of NSButtonCell I override the Method drawBezelWithFrame:inView: and in the initWithFrame Method of my subclassed NSButton I use setCell to set my CustomCell in the Button. However, drawBezelWithFrame gets not called and I don't understand why. Can someone point out what I've done wrong or what I miss here?
Subclass of NSButtonCell:
#import "TWIButtonCell.h"
#implementation TWIButtonCell
-(void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView
{
//// General Declarations
[[NSGraphicsContext currentContext] saveGraphicsState];
//// Color Declarations
NSColor* fillColor = [NSColor colorWithCalibratedRed: 0 green: 0.59 blue: 0.886 alpha: 1];
//// Rectangle Drawing
NSBezierPath* rectanglePath = [NSBezierPath bezierPathWithRect: NSMakeRect(8.5, 7.5, 85, 25)];
[fillColor setFill];
[rectanglePath fill];
[NSGraphicsContext restoreGraphicsState];
}
#end
Subclass of NSButton:
#import "TWIButton.h"
#import "TWIButtonCell.h"
#implementation TWIButton
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
TWIButtonCell *cell = [[TWIButtonCell alloc]init];
[self setCell:cell];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
}
#end
Usage:
- (void)addSendButton:(NSRect)btnSendRectRect
{
TWIButton *sendButton = [[TWIButton alloc] initWithFrame:btnSendRectRect];
[self addSubview:sendButton];
[sendButton setTitle:#"Send"];
[sendButton setTarget:self];
[sendButton setAction:#selector(send:)];
}
Following are the things seems to be missing out from your code.
You are not calling the [super drawRect:dirtyRect]
You are not overriding + (Class)cellClass in the Class(TWIButton) which is derived from NSButton.
Below is the code after changes:
#implementation TWIButton
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
TWIButtonCell *cell = [[TWIButtonCell alloc]init];
[self setCell:cell];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
// Drawing code here.
//Changes Added!!!
[super drawRect:dirtyRect];
}
//Changes Added!!!!
+ (Class)cellClass
{
return [TWIButtonCell class];
}
#end
Now keep the break point at drawBezelWithFrame and check it will get called.
One might forgo the NSButton subclass, since it looks like you are ONLY using it to instantiate the Cell type within the initializer.
Simply
NSButton *button ...
[button setCell: [[TWIButtonCell alloc] init] autorelease]];
btw. you will probably have a leak in the previous examples since you init and then call setCell that likely has its own retain.

Changing view in NSWindow removes contents of view

I am using an NSToolbar and NSWindowController to change the views of an NSWindow. When the toolbar items are selected, the view is successfully changed for the window and the window changes it's size according to the view size. Upon the initial loading of the window, the contents of the view appear as expected. However, once a toolbar item is selected the contents of the new view are not visible and when the view is switched back to the original view, it's contents are no longer visible either. Not sure what is causing this so any help would be appreciated.
#import <Cocoa/Cocoa.h>
#interface WindowController : NSWindowController {
IBOutlet NSView *firstView;
IBOutlet NSView *secondView;
int currentViewTag;
}
- (IBAction)switchView:(id)sender;
#end
and
#import "WindowController.h"
#interface WindowController ()
#end
#implementation WindowController
- (id)init {
self = [super initWithWindowNibName:#"WindowController"];
if (self) {
}
return self;
}
- (void)windowDidLoad {
[super windowDidLoad];
}
- (NSRect)newFrameForNewContentView:(NSView*)view {
NSWindow *window = [self window];
NSRect newFrameRect = [window frameRectForContentRect:[view frame]];
NSRect oldFrameRect = [window frame];
NSSize newSize = newFrameRect.size;
NSSize oldSize = oldFrameRect.size;
NSRect frame = [window frame];
frame.size = newSize;
frame.origin.y -= (newSize.height - oldSize.height);
return frame;
}
- (NSView *)viewForTag:(int)tag {
NSView *view = nil;
switch (tag) {
case 0:
view = firstView;
break;
case 1:
view = secondView;
break;
}
return view;
}
- (BOOL)validateToolbarItem:(NSToolbarItem *)item {
if ([item tag] == currentViewTag) return NO;
else return YES;
}
- (void)awakeFromNib {
[[self window] setContentSize:[firstView frame].size];
[[[self window] contentView] addSubview:firstView];
[[[self window] contentView] setWantsLayer:YES];
}
- (IBAction)switchView:(id)sender {
double tag = [sender tag];
NSView *view = [self viewForTag:tag];
NSView *previousView = [self viewForTag:currentViewTag];
currentViewTag = tag;
NSRect newFrame = [self newFrameForNewContentView:view];
[NSAnimationContext beginGrouping];
if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask)
[[NSAnimationContext currentContext] setDuration:1.0];
[[[[self window] contentView] animator] replaceSubview:previousView with:view];
[[[self window] animator] setFrame:newFrame display:YES];
[NSAnimationContext endGrouping];
}
#end
I tried out your code, and what I saw was not that the views disappeared, but that they were positioned wrongly and moved out of view. I fixed this by turning off auto layout in IB, and deselecting all the struts and springs in the size inspector. I also deselected the window's "restorable" property so that if you closed the program with view 2 visible, and reopened, the window (with view 1 inside) would be the correct size for view 1.

Equivalent of clipToBounds for Mac (NSView's layer)

I have a class "GlobalView" which extends NSView and has two subviews GreenRectView (which extends NSView).
I use the CALayer from my GreenRectView in order to be able to rotate (with setAffineTransform:) my view around a point other than the default bottom-left point. Thus I used "setAnchorPoint". Everything works well except that when my green rect is rotated , it is not draw outside the bounds of its view. In iOS it is possible to use clipToBounds:NO to accept that drawing is display outside bounds but in Mac it doesn't work with masksToBounds... How can I rotate my greenrect and display it entirely whatever the angle of rotation.
See picture :
And here is the code :
#implementation GlobalView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
g = [[GreenRectView alloc]initWithPoint:NSMakePoint(200, 200)];
[self addSubview:g];
g = [[GreenRectView alloc]initWithPoint:NSMakePoint(100, 400)];
[self addSubview:g];
}
return self;
}
#import <Quartz/Quartz.h>
#include <ApplicationServices/ApplicationServices.h>
#implementation GreenRectView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setWantsLayer:YES];
layer = [self layer];
[self setLayer:layer];
[layer setAnchorPoint:NSMakePoint(0.0,0.5)];
[layer setMasksToBounds:NO];
[menuLayer addSublayer:layer];
}
return self;
}
- (id)initWithPoint:(CGPoint)p {
return [self initWithFrame:NSMakeRect(p.x, p.y, 120, 100)];
}
- (void)drawRect:(NSRect)dirtyRect {
[[NSColor greenColor] setFill];
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:NSMakeRect(0,0, self.frame.size.width, self.frame.size.height) xRadius:5.5 yRadius:5.5];
[path stroke];
}

setNeedsDisplay for DrawRect after button press

I have this in a custom class on my NSView and when I press a button using my drawMyRec IBAction I want to change the color of my NSRect however this is not working, can anyone help?
#import "myView.h"
#implementation myView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
RectColor = [[NSColor blackColor] init];
}
return self;
}
- (IBAction)drawMyRec:(id)sender;
{
NSLog(#"pressed");
RectColor = [[NSColor blueColor] init];
[self setNeedsDisplay:YES];
}
- (void)drawRect:(NSRect)dirtyRect
{
NSRectFill(dirtyRect);
[RectColor set];
}
#end
Your drawRect function is incorrect. change to this:
- (void)drawRect:(NSRect)dirtyRect
{
[RectColor set];
NSRectFill(dirtyRect);
}
First, why are you calling -init on an NSColor? Second, you're setting the color after you've drawn the rect, so it won't take affect until the next redraw. 3rd, what's dirtyRect when -drawRect: is called? Why not just fill the entire rect regardless of what's dirty?

Cocoa -- getting a simple NSImageView to work

I am confused about why this code does not display any image:
In the app delegate:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSRect rect = window.frame;
rect.origin.x = 0;
rect.origin.y = 0;
BlueImageView *blueImageView = [[BlueImageView alloc]initWithFrame:rect];
window.contentView = blueImageView; // also tried [window.contentView addSubview: blueImageView];
}
BlueImageView.h:
#interface BlueImageView : NSImageView {
}
#end
BlueImageView.m:
#implementation BlueImageView
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setImage: [NSImage imageNamed:#"imagefile.png"]];
NSAssert(self.image, #"");
NSLog (#"Initialized");
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect {
}
#end
The file imagefile.png exists. The NSAssert is not causing an exception. The NSLog is firing. But no image shows up in the window.
The drawRect: method is called to draw the view, and your implementation immediately returns. To get NSImageView to draw the image for you, call [super drawRect:dirtyRect]; in your implementation of drawRect:. If you aren't going to do any other drawing in drawRect:, just remove the method to speed up drawing.

Resources