OSDN Git Service

SimpleHTMLEditorはそれなりに使えるようになった。
[chnosproject/CHNOSProject.git] / CHNOSProject / SimpleHTMLEditor / SimpleHTMLEditor / AppDelegate.m
index b9df623..c4f3781 100644 (file)
 
 @implementation AppDelegate
 
-NSString *strNotOpened = @"Not opened a file.";
+- (IBAction)addressBarURIChanged:(id)sender {
+    mainWebView.mainFrameURL = addressBar.stringValue;
+}
+
+- (IBAction)openCurrentURIForEdit:(id)sender {
+    if([sourceEditor.string isEqualToString:@""]){
+        editingFileURILabel.stringValue = mainWebView.mainFrameURL;
+        sourceEditor.string = [mainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
+    } else{
+        NSBeginAlertSheet(@"SimpleHTMLEditor", @"No", @"Yes", nil, mainWindow, self.self, @selector(openCurrentURIForEdit_sheetClosed:returnCode:contextInfo:), nil, nil, @"Do you want to discard changes?");
+    }
+}
 
-- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
+- (void)openCurrentURIForEdit_sheetClosed:(id)sheet returnCode:(int)returnCode contextInfo:(id)contextInfo
 {
-    // Insert code here to initialize your application
-    [_filePathLabel setStringValue:strNotOpened];
+    switch (returnCode) {
+        case NSAlertDefaultReturn:
+            
+            break;
+            
+        case NSAlertAlternateReturn:
+            editingFileURILabel.stringValue = mainWebView.mainFrameURL;
+            sourceEditor.string = [mainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
+            break;
+    }
 }
 
-- (IBAction)display:(id)sender {
-    if(![[_filePathLabel stringValue] isEqualToString:strNotOpened]){
-        [mainWebView setMainFrameURL:[_filePathLabel stringValue]];
-        [_addressBar setStringValue:[_filePathLabel stringValue]];
+
+- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
+{
+    if(frame == sender.mainFrame){
+        addressBar.stringValue = sender.mainFrameURL;
     }
 }
 
 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
 {
-    //最後のwindowが閉じたときに終了するか否か
     return YES;
 }
 
-- (void)saveHTMLForFile:(id)sender
+- (void)saveEditingDocumentForFile:(id)sender
 {
     NSSavePanel *savePanel;
     NSArray *allowedFileType;
@@ -39,7 +58,8 @@ NSString *strNotOpened = @"Not opened a file.";
     NSURL *path;
     NSError *error;
     
-    if([[_filePathLabel stringValue] isEqualToString:strNotOpened] || sender == _menuSaveAsButton){
+    if([[editingFileURILabel stringValue] isEqualToString:@""] || sender == MenuItem_SaveAs){
+        //ファイル名をユーザーに入力させる
         savePanel = [NSSavePanel savePanel];
         allowedFileType = [NSArray arrayWithObjects:@"htm", @"html", nil];
         [savePanel setAllowedFileTypes:allowedFileType];
@@ -50,11 +70,11 @@ NSString *strNotOpened = @"Not opened a file.";
             case NSOKButton:
                 path = [savePanel URL];
                 error = nil;
-                [[_editBox string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
+                [[sourceEditor string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
                 if(error != nil){
                     NSRunAlertPanel(@"SimpleHTMLEditor-Error-", [error localizedDescription], @"OK", nil, nil);
                 } else{
-                    [_filePathLabel setStringValue:[path path]];
+                    [editingFileURILabel setStringValue:[path path]];
                 }
                 
                 break;
@@ -63,10 +83,11 @@ NSString *strNotOpened = @"Not opened a file.";
                 break;
         };
     } else{
-        path = [NSURL fileURLWithPath:[_filePathLabel stringValue]];
+        //開いたURIに保存する。
+        path = [NSURL fileURLWithPath:[[editingFileURILabel stringValue] stringByReplacingOccurrencesOfString:@"file://" withString:@""]];
         
         error = nil;
-        [[_editBox string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
+        [[sourceEditor string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
         if(error != nil){
             NSRunAlertPanel(@"SimpleHTMLEditor-Error-", [error localizedDescription], @"OK", nil, nil);
         }
@@ -75,10 +96,22 @@ NSString *strNotOpened = @"Not opened a file.";
     }
 }
 
-- (void)webViewDidChange:(NSNotification *)notification
+- (IBAction)revertToSaved:(id)sender
 {
-    [_addressBar setStringValue:[[[[[mainWebView mainFrame] dataSource] request] URL] absoluteString]];
+    NSBeginAlertSheet(@"SimpleHTMLEditor", @"No", @"Yes", nil, mainWindow, self.self, @selector(revertToSaved_sheetClosed: returnCode:contextInfo:), nil, nil, @"Do you want to revert to saved?");
+}
 
+- (void)revertToSaved_sheetClosed:(id)sheet returnCode:(int)returnCode contextInfo:(id)contextInfo
+{
+    switch (returnCode) {
+        case NSAlertDefaultReturn:
+            
+            break;
+            
+        case NSAlertAlternateReturn:
+            sourceEditor.string = [mainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
+            break;
+    }
 }
 
 @end