OSDN Git Service

c4f37812e55b2744432850f84aaa26b64f17105d
[chnosproject/CHNOSProject.git] / CHNOSProject / SimpleHTMLEditor / SimpleHTMLEditor / AppDelegate.m
1 //
2 //  AppDelegate.m
3 //  SimpleHTMLEditor
4 //
5 //  Created by 西田 耀 on 13/04/13.
6 //  Copyright (c) 2013年 CHNOSProject. All rights reserved.
7 //
8
9 #import "AppDelegate.h"
10
11 @implementation AppDelegate
12
13 - (IBAction)addressBarURIChanged:(id)sender {
14     mainWebView.mainFrameURL = addressBar.stringValue;
15 }
16
17 - (IBAction)openCurrentURIForEdit:(id)sender {
18     if([sourceEditor.string isEqualToString:@""]){
19         editingFileURILabel.stringValue = mainWebView.mainFrameURL;
20         sourceEditor.string = [mainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
21     } else{
22         NSBeginAlertSheet(@"SimpleHTMLEditor", @"No", @"Yes", nil, mainWindow, self.self, @selector(openCurrentURIForEdit_sheetClosed:returnCode:contextInfo:), nil, nil, @"Do you want to discard changes?");
23     }
24 }
25
26 - (void)openCurrentURIForEdit_sheetClosed:(id)sheet returnCode:(int)returnCode contextInfo:(id)contextInfo
27 {
28     switch (returnCode) {
29         case NSAlertDefaultReturn:
30             
31             break;
32             
33         case NSAlertAlternateReturn:
34             editingFileURILabel.stringValue = mainWebView.mainFrameURL;
35             sourceEditor.string = [mainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
36             break;
37     }
38 }
39
40
41 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
42 {
43     if(frame == sender.mainFrame){
44         addressBar.stringValue = sender.mainFrameURL;
45     }
46 }
47
48 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
49 {
50     return YES;
51 }
52
53 - (void)saveEditingDocumentForFile:(id)sender
54 {
55     NSSavePanel *savePanel;
56     NSArray *allowedFileType;
57     NSInteger pressedButton;
58     NSURL *path;
59     NSError *error;
60     
61     if([[editingFileURILabel stringValue] isEqualToString:@""] || sender == MenuItem_SaveAs){
62         //ファイル名をユーザーに入力させる
63         savePanel = [NSSavePanel savePanel];
64         allowedFileType = [NSArray arrayWithObjects:@"htm", @"html", nil];
65         [savePanel setAllowedFileTypes:allowedFileType];
66         
67         pressedButton = [savePanel runModal];
68         
69         switch(pressedButton){
70             case NSOKButton:
71                 path = [savePanel URL];
72                 error = nil;
73                 [[sourceEditor string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
74                 if(error != nil){
75                     NSRunAlertPanel(@"SimpleHTMLEditor-Error-", [error localizedDescription], @"OK", nil, nil);
76                 } else{
77                     [editingFileURILabel setStringValue:[path path]];
78                 }
79                 
80                 break;
81             case NSCancelButton:
82                 
83                 break;
84         };
85     } else{
86         //開いたURIに保存する。
87         path = [NSURL fileURLWithPath:[[editingFileURILabel stringValue] stringByReplacingOccurrencesOfString:@"file://" withString:@""]];
88         
89         error = nil;
90         [[sourceEditor string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
91         if(error != nil){
92             NSRunAlertPanel(@"SimpleHTMLEditor-Error-", [error localizedDescription], @"OK", nil, nil);
93         }
94         
95         
96     }
97 }
98
99 - (IBAction)revertToSaved:(id)sender
100 {
101     NSBeginAlertSheet(@"SimpleHTMLEditor", @"No", @"Yes", nil, mainWindow, self.self, @selector(revertToSaved_sheetClosed: returnCode:contextInfo:), nil, nil, @"Do you want to revert to saved?");
102 }
103
104 - (void)revertToSaved_sheetClosed:(id)sheet returnCode:(int)returnCode contextInfo:(id)contextInfo
105 {
106     switch (returnCode) {
107         case NSAlertDefaultReturn:
108             
109             break;
110             
111         case NSAlertAlternateReturn:
112             sourceEditor.string = [mainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
113             break;
114     }
115 }
116
117 @end