5 // Created by 西田 耀 on 13/04/13.
6 // Copyright (c) 2013年 CHNOSProject. All rights reserved.
9 #import "AppDelegate.h"
11 @implementation AppDelegate
13 - (void)applicationDidFinishLaunching:(NSNotification *)notification
16 [sourceEditor setFont:[NSFont fontWithName:@"Source Code Pro" size:14]];
18 [[sourceEditor textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
19 [[sourceEditor textContainer] setWidthTracksTextView:NO];
20 [sourceEditor setHorizontallyResizable:YES];
23 - (IBAction)addressBarURIChanged:(id)sender {
24 mainWebView.mainFrameURL = addressBar.stringValue;
27 - (IBAction)openCurrentURIForEdit:(id)sender {
28 if([sourceEditor.string isEqualToString:@""]){
29 [self openCurrentURIForEdit_ReadURI];
31 NSBeginAlertSheet(@"SimpleHTMLEditor", @"No", @"Yes", nil, mainWindow, self.self, @selector(openCurrentURIForEdit_sheetClosed:returnCode:contextInfo:), nil, nil, @"Do you want to discard changes?");
35 - (void)openCurrentURIForEdit_sheetClosed:(id)sheet returnCode:(int)returnCode contextInfo:(id)contextInfo
38 case NSAlertDefaultReturn:
42 case NSAlertAlternateReturn:
43 [self openCurrentURIForEdit_ReadURI];
48 - (void)openCurrentURIForEdit_ReadURI
50 editingFileURILabel.stringValue = mainWebView.mainFrameURL;
52 //sourceEditor.string = [mainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
53 sourceEditor.string = [NSString stringWithContentsOfURL:[NSURL URLWithString:mainWebView.mainFrameURL] encoding:NSUTF8StringEncoding error:nil];
56 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
58 if(frame == sender.mainFrame){
59 addressBar.stringValue = sender.mainFrameURL;
63 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
68 - (void)saveEditingDocumentForFile:(id)sender
70 NSSavePanel *savePanel;
71 NSArray *allowedFileType;
72 NSInteger pressedButton;
76 if([[editingFileURILabel stringValue] isEqualToString:@""] || sender == MenuItem_SaveAs){
78 savePanel = [NSSavePanel savePanel];
79 allowedFileType = [NSArray arrayWithObjects:@"htm", @"html", nil];
80 [savePanel setAllowedFileTypes:allowedFileType];
82 pressedButton = [savePanel runModal];
84 switch(pressedButton){
86 path = [savePanel URL];
88 [[sourceEditor string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
90 NSRunAlertPanel(@"SimpleHTMLEditor-Error-", [error localizedDescription], @"OK", nil, nil);
92 [editingFileURILabel setStringValue:[path path]];
102 path = [NSURL fileURLWithPath:[[editingFileURILabel stringValue] stringByReplacingOccurrencesOfString:@"file://" withString:@""]];
105 [[sourceEditor string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
107 NSRunAlertPanel(@"SimpleHTMLEditor-Error-", [error localizedDescription], @"OK", nil, nil);
114 - (IBAction)revertToSaved:(id)sender
116 NSBeginAlertSheet(@"SimpleHTMLEditor", @"No", @"Yes", nil, mainWindow, self.self, @selector(revertToSaved_sheetClosed: returnCode:contextInfo:), nil, nil, @"Do you want to revert to saved?");
119 - (void)revertToSaved_sheetClosed:(id)sheet returnCode:(int)returnCode contextInfo:(id)contextInfo
121 switch (returnCode) {
122 case NSAlertDefaultReturn:
126 case NSAlertAlternateReturn:
127 sourceEditor.string = [mainWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"];
132 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
134 NSRunAlertPanel(@"alert", message, @"OK", nil, nil);
137 - (BOOL)webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
139 NSInteger result = NSRunAlertPanel(@"confirm", message, @"OK", @"Cancel", nil);
140 if (result == NSAlertDefaultReturn) {
146 - (void) webView:(WebView*)webView addMessageToConsole:(NSDictionary*)message
148 NSLog(@"%@", message);
149 logViewer.string = [logViewer.string stringByAppendingFormat:@"%@\n", message];
152 - (void) clearLogView:(id)sender
154 logViewer.string = @"";