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 NSString *strNotOpened = @"Not opened a file.";
14
15 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
16 {
17     // Insert code here to initialize your application
18     [_filePathLabel setStringValue:strNotOpened];
19 }
20
21 - (IBAction)display:(id)sender {
22     if(![[_filePathLabel stringValue] isEqualToString:strNotOpened]){
23         [mainWebView setMainFrameURL:[_filePathLabel stringValue]];
24         [_addressBar setStringValue:[_filePathLabel stringValue]];
25     }
26 }
27
28 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
29 {
30     //最後のwindowが閉じたときに終了するか否か
31     return YES;
32 }
33
34 - (void)saveHTMLForFile:(id)sender
35 {
36     NSSavePanel *savePanel;
37     NSArray *allowedFileType;
38     NSInteger pressedButton;
39     NSURL *path;
40     NSError *error;
41     
42     if([[_filePathLabel stringValue] isEqualToString:strNotOpened] || sender == _menuSaveAsButton){
43         savePanel = [NSSavePanel savePanel];
44         allowedFileType = [NSArray arrayWithObjects:@"htm", @"html", nil];
45         [savePanel setAllowedFileTypes:allowedFileType];
46         
47         pressedButton = [savePanel runModal];
48         
49         switch(pressedButton){
50             case NSOKButton:
51                 path = [savePanel URL];
52                 error = nil;
53                 [[_editBox string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
54                 if(error != nil){
55                     NSRunAlertPanel(@"SimpleHTMLEditor-Error-", [error localizedDescription], @"OK", nil, nil);
56                 } else{
57                     [_filePathLabel setStringValue:[path path]];
58                 }
59                 
60                 break;
61             case NSCancelButton:
62                 
63                 break;
64         };
65     } else{
66         path = [NSURL fileURLWithPath:[_filePathLabel stringValue]];
67         
68         error = nil;
69         [[_editBox string] writeToURL:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
70         if(error != nil){
71             NSRunAlertPanel(@"SimpleHTMLEditor-Error-", [error localizedDescription], @"OK", nil, nil);
72         }
73         
74         
75     }
76 }
77
78 - (void)webViewDidChange:(NSNotification *)notification
79 {
80     [_addressBar setStringValue:[[[[[mainWebView mainFrame] dataSource] request] URL] absoluteString]];
81
82 }
83
84 @end