1/*
2 Project: Graphos
3 GRDocument.m
4
5 Copyright (C) 2000-2017 GNUstep Application Project
6
7 Author: Enrico Sersale (original implementation)
8 Author: Ing. Riccardo Mottola
9
10 This application is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
14
15 This application is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 Library General Public License for more details.
19
20 You should have received a copy of the GNU General Public
21 License along with this library; if not, write to the Free
22 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 */
24
25#import "GRDocument.h"
26
27@implementation GRDocument
28
29- (id)init
30{
31  self = [super init];
32  if (self)
33    {
34      documentDictionary = nil;
35    }
36  return self;
37}
38
39- (NSString *) windowNibName
40{
41    return @"GRDocument";
42}
43
44- (void)windowControllerDidLoadNib:(NSWindowController *) aController
45{
46    NSPrintInfo *p;
47
48    [super windowControllerDidLoadNib:aController];
49    if (aController == [[self windowControllers] objectAtIndex: 0])
50    {
51      NSScrollView *sv;
52      NSClipView *cv;
53
54      sv = [[[[aController window] contentView] subviews] objectAtIndex: 0];
55      [sv setDocumentView: docView];
56      cv = [sv contentView];
57      [cv scrollToPoint: NSMakePoint(0, NSMaxY([docView bounds]))];
58    }
59
60    if (documentDictionary)
61      [docView createObjectsFromDictionary: documentDictionary];
62
63    /* initialize the image view to the default size if possible */
64    p = [self printInfo];
65    if (p != nil)
66      [docView updatePrintInfo: p];
67    else
68      NSLog(@"printInfo nil!");
69
70    /* set undo levels */
71    [[self undoManager] setLevelsOfUndo:1];
72
73}
74
75- (BOOL)revertToSavedFromFile:(NSString *)fileName ofType:(NSString *)type
76{
77  BOOL r;
78
79  r = [super revertToSavedFromFile:fileName ofType:type];
80  if (r)
81    {
82      [docView createObjectsFromDictionary: documentDictionary];
83    }
84
85  return r;
86}
87
88- (NSData *)dataRepresentationOfType:(NSString *)aType
89{
90    return [[[docView objectDictionary] description] dataUsingEncoding: NSASCIIStringEncoding];
91}
92
93- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
94{
95  NSString *tmp = [[[NSString alloc] initWithData: data
96                                           encoding: NSASCIIStringEncoding] autorelease];
97
98  if( [aType isEqualToString: @"graphos design"] && tmp != nil )
99    {
100      if( [tmp rangeOfString: @"<?xml"].length > 0 )
101        {
102	  tmp = [[[NSString alloc] initWithData: data
103				   encoding: NSUTF8StringEncoding] autorelease];
104        }
105
106      if (documentDictionary != nil)
107	{
108	  [documentDictionary release];
109	  documentDictionary = nil;
110	}
111      documentDictionary = [[tmp propertyList] retain];
112      return (documentDictionary != nil);
113    }
114  return NO;
115}
116
117- (GRDocView *)docView
118{
119  return docView;
120}
121
122
123- (void)printShowingPrintPanel:(BOOL)flag
124{
125    NSPrintOperation *op;
126
127    op = [NSPrintOperation printOperationWithView:docView
128                                        printInfo:[self printInfo]];
129    [op setShowPanels:flag];
130    [op runOperationModalForWindow:[[[self windowControllers] objectAtIndex: 0] window]
131                          delegate:nil
132                    didRunSelector:NULL
133                       contextInfo:nil];
134}
135
136/**
137 * after the page layout is changed, update the view
138 */
139- (void)setPrintInfo:(NSPrintInfo *)printInfo
140{
141  [super setPrintInfo: printInfo];
142  [docView updatePrintInfo: printInfo];
143}
144
145/**
146 * overridden so to allow changing the page layout
147 */
148- (BOOL)shouldChangePrintInfo:(NSPrintInfo *)newPrintInfo
149{
150    return YES;
151}
152
153@end
154