Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/mac-app/MyDocument.m
8815 views
1
//
2
// MyDocument.m
3
// Sage
4
//
5
// Created by Ivan Andrus on 26/6/10.
6
// Copyright 2010 __MyCompanyName__. All rights reserved.
7
//
8
9
#import "MyDocument.h"
10
#import <WebKit/WebFrame.h>
11
#import <WebKit/WebUIDelegate.h>
12
#import <WebKit/WebDataSource.h>
13
14
@implementation MyDocument
15
16
- (id)init{
17
self = [super init];
18
if (self) {
19
20
// Add your subclass-specific initialization here.
21
// If an error occurs here, send a [self release] message and return nil.
22
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
23
[nc addObserver:self selector:@selector(webViewProgressStarted:) name:WebViewProgressStartedNotification object:webView];
24
[nc addObserver:self selector:@selector(webViewProgressFinished:) name:WebViewProgressFinishedNotification object:webView];
25
26
// We don't want undo
27
[self setHasUndoManager:NO];
28
}
29
return self;
30
}
31
32
- (NSString *)windowNibName{
33
// Override returning the nib file name of the document
34
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
35
return @"MyDocument";
36
}
37
38
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
39
{
40
// TODO: this is slightly underhanded but easier than building our own from scratch.
41
[webView setApplicationNameForUserAgent:@"Safari/528.16 SageBrowser"];
42
43
[super windowControllerDidLoadNib:aController];
44
45
// Set up some delegates. Perhaps this could/should be done in the nib file
46
[webView setGroupName:@"MyDocument"];
47
[webView setUIDelegate:self];
48
[webView setFrameLoadDelegate:self];
49
}
50
51
// TODO: this will allow saving
52
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError
53
{
54
// Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil.
55
56
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
57
58
// For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:. In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead.
59
NSLog(@"well at least I made it there");
60
61
if ( outError != NULL ) {
62
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
63
}
64
65
return nil;
66
}
67
68
69
- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError {
70
71
NSLog(@"well at least I made it to open a url");
72
if ( outError != NULL ) {
73
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
74
}
75
return YES;
76
}
77
78
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
79
{
80
// Insert code here to read your document from the given data of the specified type. If the given outError != NULL, ensure that you set *outError when returning NO.
81
82
// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
83
84
// For applications targeted for Panther or earlier systems, you should use the deprecated API -loadDataRepresentation:ofType. In this case you can also choose to override -readFromFile:ofType: or -loadFileWrapperRepresentation:ofType: instead.
85
NSLog(@"well at least I made it here");
86
if ( outError != NULL ) {
87
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL];
88
}
89
return YES;
90
}
91
92
// From Fluidium
93
- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError {
94
NSData *archiveData = [[[[webView mainFrame] dataSource] webArchive] data];
95
return [archiveData writeToURL:absoluteURL options:0 error:outError];
96
}
97
98
- (id)webView{
99
return webView;
100
}
101
102
- (IBAction)connectURL:(id)sender{
103
[urlString setStringValue:[sender stringValue]];
104
[[webView mainFrame] loadRequest:
105
[NSURLRequest requestWithURL:
106
[NSURL URLWithString:
107
[sender stringValue]]]];
108
}
109
110
- (WebView *)webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request{
111
112
NSError *outError = nil;
113
id myDocument = [[NSDocumentController sharedDocumentController]
114
openUntitledDocumentAndDisplay:YES error:&outError];
115
if ( myDocument == nil ) {
116
[NSApp presentError:outError];
117
NSLog(@"sageBrowser: Error creating document: %@", [outError localizedDescription]);
118
} else {
119
[[[myDocument webView] mainFrame]
120
loadRequest:request];
121
122
}
123
124
return [myDocument webView];
125
}
126
127
- (void)webViewShow:(WebView *)sender{
128
id myDocument = [[NSDocumentController sharedDocumentController] documentForWindow:[sender window]];
129
[myDocument showWindows];
130
}
131
132
- (IBAction)browseURL:(NSString*)theURL{
133
134
id myDocument = [[NSDocumentController sharedDocumentController]
135
openUntitledDocumentOfType:@"DocumentType"
136
display:YES];
137
138
[[[myDocument webView] mainFrame]
139
loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:theURL]]];
140
}
141
142
- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame{
143
// Only report feedback for the main frame.
144
if (frame == [sender mainFrame]){
145
NSString *url = [[[[frame provisionalDataSource] request] URL] absoluteString];
146
[urlString setStringValue:url];
147
}
148
}
149
150
// Taken from Fluidium
151
152
- (void)webView:(WebView *)wv runOpenPanelForFileButtonWithResultListener:(id <WebOpenPanelResultListener>)listener {
153
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
154
[openPanel beginSheetForDirectory:nil
155
file:nil
156
modalForWindow:[[self webView] window]
157
modalDelegate:self
158
didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:)
159
contextInfo:[listener retain]]; // retained
160
}
161
162
- (void)openPanelDidEnd:(NSSavePanel *)openPanel returnCode:(int)code contextInfo:(id <WebOpenPanelResultListener>)listener {
163
[listener autorelease]; // released
164
165
if (NSOKButton == code) {
166
[listener chooseFilename:[openPanel filename]];
167
}
168
}
169
170
- (IBAction)printDocument:(id)sender{
171
[[[[webView mainFrame] frameView] documentView] print:sender];
172
}
173
174
#pragma mark WebProgressNotifications
175
176
- (void)webViewProgressStarted:(NSNotification *)n {
177
// NSLog(@"progress started: %@", progressIndicator);
178
[progressIndicator startAnimation:self];
179
}
180
181
182
- (void)webViewProgressFinished:(NSNotification *)n {
183
// NSLog(@"progress stopped");
184
[progressIndicator stopAnimation:self];
185
}
186
187
@end
188
189