OSDN Git Service

SimpleHTMLEditorの修正
[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 - (void)applicationDidFinishLaunching:(NSNotification *)notification
14 {
15     //エディタフォント設定
16     [sourceEditor setFont:[NSFont fontWithName:@"Source Code Pro" size:14]];
17     //自動改行無効
18     [[sourceEditor textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
19     [[sourceEditor textContainer] setWidthTracksTextView:NO];
20     [sourceEditor setHorizontallyResizable:YES];
21 }
22
23 - (IBAction)addressBarURIChanged:(id)sender {
24     mainWebView.mainFrameURL = addressBar.stringValue;
25 }
26
27 - (IBAction)openCurrentURIForEdit:(id)sender {
28     if([sourceEditor.string isEqualToString:@""]){
29         [self openCurrentURIForEdit_ReadURI];
30     } else{
31         NSBeginAlertSheet(@"SimpleHTMLEditor", @"No", @"Yes", nil, mainWindow, self.self, @selector(openCurrentURIForEdit_sheetClosed:returnCode:contextInfo:), nil, nil, @"Do you want to discard changes?");
32     }
33 }
34
35 - (void)openCurrentURIForEdit_sheetClosed:(id)sheet returnCode:(int)returnCode contextInfo:(id)contextInfo
36 {
37     switch (returnCode) {
38         case NSAlertDefaultReturn:
39             
40             break;
41             
42         case NSAlertAlternateReturn:
43             [self openCurrentURIForEdit_ReadURI];
44             break;
45     }
46 }
47
48 - (void)openCurrentURIForEdit_ReadURI
49 {
50     editingFileURILabel.stringValue = mainWebView.mainFrameURL;
51     
52     //sourceEditor.string = [mainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
53     sourceEditor.string = [NSString stringWithContentsOfURL:[NSURL URLWithString:mainWebView.mainFrameURL] encoding:NSUTF8StringEncoding error:nil];
54 }
55
56 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
57 {
58     if(frame == sender.mainFrame){
59         addressBar.stringValue = sender.mainFrameURL;
60     }
61 }
62
63 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
64 {
65     return YES;
66 }
67
68 - (void)saveEditingDocumentForFile:(id)sender
69 {
70     NSSavePanel *savePanel;
71     NSArray *allowedFileType;
72     NSInteger pressedButton;
73     NSURL *path;
74     NSError *error;
75     
76     if([[editingFileURILabel stringValue] isEqualToString:@""] || sender == MenuItem_SaveAs){
77         //ファイル名をユーザーに入力させる
78         savePanel = [NSSavePanel savePanel];
79         allowedFileType = [NSArray arrayWithObjects:@"htm", @"html", nil];
80         [savePanel setAllowedFileTypes:allowedFileType];
81         
82         pressedButton = [savePanel runModal];
83         
84         switch(pressedButton){
85             case NSOKButton:
86                 path = [savePanel URL];
87                 error = nil;
88                 [[sourceEditor string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
89                 if(error != nil){
90                     NSRunAlertPanel(@"SimpleHTMLEditor-Error-", [error localizedDescription], @"OK", nil, nil);
91                 } else{
92                     [editingFileURILabel setStringValue:[path path]];
93                 }
94                 
95                 break;
96             case NSCancelButton:
97                 
98                 break;
99         };
100     } else{
101         //開いたURIに保存する。
102         path = [NSURL fileURLWithPath:[[editingFileURILabel stringValue] stringByReplacingOccurrencesOfString:@"file://" withString:@""]];
103         
104         error = nil;
105         [[sourceEditor string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
106         if(error != nil){
107             NSRunAlertPanel(@"SimpleHTMLEditor-Error-", [error localizedDescription], @"OK", nil, nil);
108         }
109         
110         
111     }
112 }
113
114 - (IBAction)revertToSaved:(id)sender
115 {
116     NSBeginAlertSheet(@"SimpleHTMLEditor", @"No", @"Yes", nil, mainWindow, self.self, @selector(revertToSaved_sheetClosed: returnCode:contextInfo:), nil, nil, @"Do you want to revert to saved?");
117 }
118
119 - (void)revertToSaved_sheetClosed:(id)sheet returnCode:(int)returnCode contextInfo:(id)contextInfo
120 {
121     switch (returnCode) {
122         case NSAlertDefaultReturn:
123             
124             break;
125             
126         case NSAlertAlternateReturn:
127             sourceEditor.string = [mainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
128             break;
129     }
130 }
131
132 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
133 {
134     NSRunAlertPanel(@"alert", message, @"OK", nil, nil);
135 }
136
137 - (BOOL)webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
138 {
139     NSInteger result = NSRunAlertPanel(@"confirm", message, @"OK", @"Cancel", nil);
140     if (result == NSAlertDefaultReturn) {
141         return YES;
142     }
143     return NO;
144 }
145
146 - (void) webView:(WebView*)webView addMessageToConsole:(NSDictionary*)message
147 {
148     NSLog(@"%@", message);
149     logViewer.string = [logViewer.string stringByAppendingFormat:@"%@\n", message];
150 }
151
152 - (void) clearLogView:(id)sender
153 {
154     logViewer.string = @"";
155 }
156
157 @end