Rev 4745 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
/** R.app : a Cocoa front end to: "R A Computer Language for Statistical Data Analysis"** R.app Copyright notes:* Copyright (C) 2004-5 The R Foundation* written by Stefano M. Iacus and Simon Urbanek*** R Copyright notes:* Copyright (C) 1995-1996 Robert Gentleman and Ross Ihaka* Copyright (C) 1998-2001 The R Development Core Team* Copyright (C) 2002-2004 The R Foundation** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** A copy of the GNU General Public License is available via WWW at* http://www.gnu.org/copyleft/gpl.html. You can also obtain it by* writing to the Free Software Foundation, Inc., 59 Temple Place,* Suite 330, Boston, MA 02111-1307 USA.*/#import "../RGUI.h"#import "RDeviceView.h"#import "RQuartz.h"#import "../RController.h"#include <R.h>#include <R_ext/Boolean.h>#include <R_ext/Rdynload.h>#include <Rdefines.h>#include <Rinternals.h>#include <R_ext/Parse.h>#include <Graphics.h>#include <Rdevices.h>extern void RQuartz_DiplayGList(RDeviceView * devView);extern void RQuartz_SyncContext(RDeviceView * devView);#define POOL NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]@implementation RDeviceView- (id)initWithFrame:(NSRect)frameRect{if ((self = [super initWithFrame:frameRect]) != nil) {// Add initialization code heremyContext = 0;inReplay = NO;resizeCache = nil;SLog(@"RDeviceView initWithFrame");}return self;}- (void) setPDFDrawing: (BOOL)flag{PDFDrawing = flag;}- (BOOL) isPDFDrawing{return PDFDrawing;}- (void) setDevNum: (int)dnum{deviceNum = dnum;}- (int) getDevNum{return deviceNum;}- (BOOL)isFlipped { return YES; }- (void) dealloc{[super dealloc];}- (BOOL) inReplay{return inReplay;}static void drawStringInRect(NSRect rect, NSString *str, int fontSize){NSDictionary *dict = [NSDictionarydictionaryWithObject: [NSFont boldSystemFontOfSize: fontSize]forKey: NSFontAttributeName];NSAttributedString *astr = [[NSAttributedString alloc]initWithString: strattributes: dict];NSSize strSize = [astr size];NSPoint pt = NSMakePoint((rect.size.width - strSize.width) / 2,(rect.size.height - strSize.height) / 2);// clear the rectrect.origin.x = 0;rect.origin.y = 0;[[NSColor whiteColor] set];NSRectFill(rect);// draw the string[astr drawAtPoint:pt];[astr release];}/* FIXME: zoom, minimize don't work. With grid, it waits for event before rewriting, itessentialy blocks R*/- (void)viewDidEndLiveResize{[super viewDidEndLiveResize];[resizeCache release];resizeCache = nil;[self setNeedsDisplay:YES];}- (void)viewWillStartLiveResize{[super viewWillStartLiveResize];NSRect b = [self bounds];if (resizeCache) [resizeCache release];resizeCache = [[self bitmapImageRepForCachingDisplayInRect:b] retain];if (resizeCache) [self cacheDisplayInRect:b toBitmapImageRep:resizeCache];}- (CGContextRef) currentContext {return myContext;}- (void)drawRect:(NSRect)aRect{SLog(@"RDeviceView drawRect: %fx%f [%fx%f]", aRect.origin.x, aRect.origin.y, aRect.size.width, aRect.size.height);myContext = [[NSGraphicsContext currentContext] graphicsPort];RQuartz_SyncContext(self);CGContextSelectFont(myContext,"Helvetica",10.0,kCGEncodingFontSpecific);//CGContextTranslateCTM(myContext, 0.0, frame.size.height);//CGContextScaleCTM(myContext,1.0,-1.0);CGContextSaveGState(myContext);if ([self inLiveResize]){if (resizeCache) {[self lockFocus];CGContextSaveGState(myContext);CGContextTranslateCTM(myContext, 0.0, [self bounds].size.height);CGContextScaleCTM(myContext,1.0,-1.0);[resizeCache drawInRect:[self bounds]];CGContextRestoreGState(myContext);[self unlockFocus];}return;}inReplay = YES;[self lockFocus];RQuartz_DiplayGList(self);[self unlockFocus];inReplay = NO;CGContextRestoreGState(myContext);myContext = nil;RQuartz_SyncContext(self);}- (void) saveAsBitmap: (NSString*) fname usingType: (NSBitmapImageFileType) ftype {[self lockFocus];NSBitmapImageRep* bitmap = [ [NSBitmapImageRep alloc]initWithFocusedViewRect: [self bounds] ];[self unlockFocus];NSData* data1 = [bitmap representationUsingType:ftype properties:nil];[[NSFileManager defaultManager] createFileAtPath:fname contents:data1 attributes:nil];[bitmap release];}- (void) saveAsPDF: (NSString*) fname {[self setPDFDrawing:YES];[self lockFocus];NSData *data = [self dataWithPDFInsideRect:[self bounds]];[self unlockFocus];[self setPDFDrawing:NO];[data writeToFile:fname atomically:YES];}@end