GVWindowController.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at http://www.graphviz.org/
  9. *************************************************************************/
  10. #import "GVWindowController.h"
  11. #import "GVZGraph.h"
  12. #import "GVDocument.h"
  13. @implementation GVWindowController
  14. - (id)init
  15. {
  16. if (self = [super initWithWindowNibName: @"Document"])
  17. ;
  18. return self;
  19. }
  20. - (void)setDocument: (NSDocument *)document
  21. {
  22. NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
  23. GVDocument *oldDocument = [self document];
  24. if (oldDocument)
  25. [defaultCenter removeObserver:self name:@"GVGraphDocumentDidChange" object:oldDocument];
  26. [super setDocument:document];
  27. [defaultCenter addObserver:self selector:@selector(graphDocumentDidChange:) name:@"GVGraphDocumentDidChange" object:document];
  28. }
  29. - (void)awakeFromNib
  30. {
  31. [self graphDocumentDidChange:nil];
  32. /* if window is not at standard size, make it standard size */
  33. NSWindow *window = [self window];
  34. if (![window isZoomed])
  35. [window zoom:self];
  36. [documentView setDelegate:self];
  37. }
  38. - (void)graphDocumentDidChange:(NSNotification*)notification
  39. {
  40. /* whenever the graph changes, rerender its PDF and display that */
  41. GVDocument *document = [self document];
  42. if ([document respondsToSelector:@selector(graph)])
  43. [documentView setDocument:[[[PDFDocument alloc] initWithData:[[document graph] renderWithFormat:@"pdf:quartz"]] autorelease]];
  44. }
  45. - (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)defaultFrame
  46. {
  47. /* standard size for zooming is whatever will fit the content exactly */
  48. NSRect currentFrame = [window frame];
  49. NSRect standardFrame = [window frameRectForContentRect:[[documentView documentView] bounds]];
  50. standardFrame.origin.x = currentFrame.origin.x;
  51. standardFrame.origin.y = currentFrame.origin.y + currentFrame.size.height - standardFrame.size.height;
  52. return standardFrame;
  53. }
  54. - (IBAction)actualSizeView:(id)sender
  55. {
  56. [documentView setScaleFactor:1.0];
  57. }
  58. - (IBAction)zoomInView:(id)sender
  59. {
  60. [documentView zoomIn:sender];
  61. }
  62. - (IBAction)zoomOutView:(id)sender
  63. {
  64. [documentView zoomOut:sender];
  65. }
  66. - (IBAction)zoomToFitView:(id)sender
  67. {
  68. [documentView setAutoScales:YES];
  69. [documentView setAutoScales:NO];
  70. }
  71. - (IBAction)printGraphDocument:(id)sender
  72. {
  73. [documentView printWithInfo:[[self document] printInfo] autoRotate:NO pageScaling:kPDFPrintPageScaleDownToFit];
  74. }
  75. - (void)PDFViewWillClickOnLink:(PDFView *)sender withURL:(NSURL *)URL
  76. {
  77. NSURL* baseURL = [[self document] fileURL];
  78. NSURL* targetURL = [NSURL URLWithString:[URL absoluteString] relativeToURL:baseURL];
  79. [[NSWorkspace sharedWorkspace] openURL:targetURL];
  80. }
  81. - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
  82. {
  83. /* validate toolbar or menu items */
  84. if ([anItem action] == @selector(actualSizeView:))
  85. return YES;
  86. else if ([anItem action] == @selector(zoomInView:))
  87. return [documentView canZoomIn];
  88. else if ([anItem action] == @selector(zoomOutView:))
  89. return [documentView canZoomOut];
  90. else if ([anItem action] == @selector(zoomToFitView:))
  91. return YES;
  92. else if ([anItem action] == @selector(printGraphDocument:))
  93. return YES;
  94. else
  95. return NO;
  96. }
  97. - (void)dealloc
  98. {
  99. [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GVGraphDocumentDidChange" object:[self document]];
  100. [super dealloc];
  101. }
  102. @end