home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / Yap / YapOutput.m < prev    next >
Text File  |  1996-04-12  |  8KB  |  159 lines

  1. /*
  2.  *  YapOutput.m
  3.  *  Author: Ali Ozer
  4.  *  Created: Mar 6, 1989
  5.  *  Modified: Jun & Jul 1989 for 1.0. Exception handling for error detection.
  6.  *  Modified: Aug 90 for 2.0. Added use of second context for robustness.
  7.  *  Modified: Jan 92 for 3.0. Localized.
  8.  *  Modified: Jan 96 for 4.0
  9.  *
  10.  *  This class is a subclass of view that manages the output in Yap. It
  11.  *  provides a method to execute random PostScript code whose output
  12.  *  is cached in an NSWindow for fast redraw response. (We use an NSWindow
  13.  *  rather than an NSImage because we would like to have it displayed
  14.  *  on screen as the PostScript is being executed.)                                                         
  15.  *
  16.  *  You may freely copy, distribute and reuse the code in this example.
  17.  *  NeXT disclaims any warranty of any kind, expressed or implied,
  18.  *  as to its fitness for any particular use.
  19.  */
  20.  
  21. #import <AppKit/AppKit.h>
  22. #import <AppKit/NSDPSServerContext.h>
  23. #import "YapOutput.h"
  24. #import "Controller.h"
  25. #import "Preferences.h"
  26. #import "YapWrap.h"
  27.  
  28. @implementation YapOutput
  29.  
  30. /* Lazily create the cache window... We make the window titled because we want it to come on screen (if the user has enabled the option).
  31. */
  32. - (NSWindow *)cacheWindow {
  33.     if (!cache) {
  34.         cache = [[NSWindow allocWithZone:[self zone]] initWithContentRect:[self frame] styleMask:NSTitledWindowMask backing:NSBackingStoreRetained defer:NO];
  35.         [cache setTitle:@"PostScript Execution Window"];
  36.     [cache setBackgroundColor:[NSColor whiteColor]];
  37.     [cache setMenu:nil];
  38.         [cache setExcludedFromWindowsMenu:YES];
  39.         [cache setReleasedWhenClosed:NO];
  40.     [cache setBackgroundColor:[NSColor whiteColor]];
  41.         [[cache contentView] allocateGState];    /* So that it has a gstate */
  42.     [cache displayIfNeeded];    /* The background and title */
  43.     }
  44.     return cache;
  45. }
  46.  
  47. - (void)updateSizeFromPreferences {
  48.     NSSize currentSize = [self frame].size;
  49.     NSSize newSize = {[[Preferences objectForKey:ViewWidth] floatValue], [[Preferences objectForKey:ViewHeight] floatValue]};
  50.     if (!NSEqualSizes(currentSize, newSize)) {
  51.         [self setFrameSize:newSize];
  52.         if (cache) [cache setContentSize:newSize];
  53.     }
  54. }
  55.  
  56. - (void)drawRect:(NSRect)rect {
  57.     PScomposite (rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, [[[self cacheWindow] contentView] gState], rect.origin.x, rect.origin.y, NSCompositeCopy);
  58. }
  59.  
  60. - (void)dealloc {
  61.     if (cache) [cache release];
  62.     [super dealloc];
  63. }
  64.  
  65. /* SwitchContextsWithFocus() will make the specified context the current context and make it focus on the same area the old context was focused on.
  66. */
  67. static void SwitchContextsWithFocus (NSDPSContext *newContext) {
  68.     float c1x, c1y, c2x, c2y;
  69.     float winCTM[6];
  70.     int realWinNum;
  71.     GetFocus (&c1x, &c1y, &c2x, &c2y, winCTM, &realWinNum);
  72.     [NSDPSContext setCurrentContext:newContext];
  73.     ReFocus (realWinNum, winCTM, c1x, c1y, c2x, c2y);
  74. }
  75.  
  76. /* executePostScriptData: treats the contents of the NSData it is given as a PostScript program and executes it.  
  77.  
  78. For protection against errors, the PostScript code is interpreted in a context separate from the NSApplication's own context (which is created in the -init method of NSApplication).  We first focus on the cache, note the various parameters (global window number, the transformation matrix, and the clip path), and then switch to the alternate context and reapply the parameters to establish a focus on the same area.
  79.  
  80. Protection against PostScript errors is provided through the use of NS_DURING/NS_HANDLER.   If an error occurs, we immediately blast the second context and report the first error encountered.  If no errors occur during the execution, then we hang on to the context as it can be reused.
  81.  
  82. Note that the NSXEPSImageRep class provides a similar (but more powerful) sort of functionality for EPS files.  Use that class rather than the code here if you wish to make use of EPS files in your application.  This code here is meant for unstructured, short pieces of PostScript code,  exactly the kind that Yap encounters...
  83. */
  84. - (void)executePostScriptData:(NSData *)data {
  85.     int utime; /* Time taken to execute the code */
  86.     NSString *statusString = @"";
  87.     BOOL showCache = [[Preferences objectForKey:ShowCache] boolValue];
  88.     BOOL clearCache = [[Preferences objectForKey:ClearCache] boolValue];
  89.     BOOL showGridLines = [[Preferences objectForKey:ShowGridLines] boolValue];
  90.     static NSDPSServerContext *yapContext = nil;    /* The second context */
  91.     NSDPSContext *curContext;
  92.     NSSize boundsSize = [self bounds].size;
  93.     NSString *startString = [NSString stringWithFormat:@"/yaptime usertime def /yapsave save def /yapwidth %f def /yapheight %f def /showpage {} def\n", boundsSize.width, boundsSize.height];
  94.     NSString *endString = @"\nyapsave restore /yaptime usertime yaptime sub def\n";
  95.  
  96.     /* Create the second context if it needs to be created. */
  97.  
  98.     if (yapContext == nil) {
  99.     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  100.     NSString *hostName = [userDefaults stringForKey:@"NSHost"];
  101.     NSString *serverName = [userDefaults stringForKey:@"NSPSName"];
  102.         NSTimeInterval timeout = [userDefaults floatForKey:@"NSNetTimeout"];
  103.     yapContext = [[NSDPSServerContext allocWithZone:[self zone]] initWithHostName:hostName ? hostName : @"" serverName:serverName ? serverName : @"" textProc:NULL errorProc:NULL timeout:(timeout == 0.0) ? 60.0 : timeout secure:NO encapsulated:NO];
  104.     if (yapContext == nil) {
  105.             [[self window] setTitle:NSLocalizedString(@"Could not connect to window server.", @"Shown if a connection cannot be created with the window server")];
  106.     }
  107.     }
  108.  
  109.     [[self window] setTitle:NSLocalizedString(@"BUSY", @"String shown when PostScript is being executed")];
  110.     [[self window] displayIfNeeded];    /* To get the title out */
  111.     [[self window] orderFront:nil];
  112.  
  113.     /* If the user changed the size of the view, update. */
  114.     [self updateSizeFromPreferences];
  115.  
  116.     /* Lock focus on the cache. If user wishes to see the cache, bring it up. */
  117.     [[[self cacheWindow] contentView] lockFocus];
  118.     if (clearCache) {
  119.         NSEraseRect ([self bounds]);
  120.     }
  121.     if (showCache) {
  122.         [[self cacheWindow] center];
  123.         [[self cacheWindow] orderFront:nil];
  124.     }
  125.     if (showGridLines) {
  126.        NSSize size = [self bounds].size;
  127.         DrawGrid(size.width, size.height, 72.0);
  128.     }
  129.  
  130.     curContext = [NSDPSContext currentContext];
  131.  
  132.     NS_DURING {
  133.         /* Focus the second context to whatever the first context is focused on. */
  134.         SwitchContextsWithFocus (yapContext);
  135.         [yapContext writePostScriptWithLanguageEncodingConversion:[startString dataUsingEncoding:NSASCIIStringEncoding]];
  136.         [yapContext writePostScriptWithLanguageEncodingConversion:data];
  137.         [yapContext writePostScriptWithLanguageEncodingConversion:[endString dataUsingEncoding:NSASCIIStringEncoding]];
  138.         [yapContext wait]; /* This does not return until the execution is done. If there were any errors, we jump to the handler. */
  139.         GetUserTime (&utime);
  140.         statusString = [NSString stringWithFormat:NSLocalizedString(@"Postscript Output (Execution Time %d ms)", @"Shown when PostScript has executed successfully"), utime];
  141.         [NSDPSContext setCurrentContext:curContext];       /* Restore the PS context */
  142.     } NS_HANDLER {
  143.         [NSDPSContext setCurrentContext:curContext];       /* Restore the PS context */
  144.         [yapContext release];                        /* Get rid of the questionable context */
  145.         yapContext = nil;
  146.         if (![[localException name] isEqualToString:DPSPostscriptErrorException]) {
  147.             [localException raise];
  148.         } else {
  149.             statusString = [NSDPSContext stringForDPSError:(const DPSBinObjSeqRec *)[[[localException userInfo] objectForKey:@"DPSArg1ExceptionUserInfo"] pointerValue]];
  150.         }
  151.     } NS_ENDHANDLER
  152.  
  153.     if (showCache) [[self cacheWindow] orderOut:nil];
  154.     [[[self cacheWindow] contentView] unlockFocus];
  155.     [self setNeedsDisplay:YES];
  156.     [[self window] setTitle:statusString];
  157. }
  158.  
  159. @end