//
//  MyApplication.m
//
//  Created by Dr. H. Nikolaus Schaller on Sun Aug 17 2003.
//  Copyright (c) 2003 __MyCompanyName__. All rights reserved.
//

#import "MyApplication.h"

@implementation MyApplication

- (void) awakeFromNib
{
	NSLog(@"AppController awakeFromNib");
	[[webView mainFrame] loadHTMLString:@"<html><head><title>Document Title</title></head><body bgcolor=\"#f0f0f0f0\">Welcome to the mySTEP/GNUstep SimpleWebKit browser</body></html>" baseURL:nil];
}

- (void) showStatus:(NSString *) str;
{
	[status setStringValue:str];
	NSLog(@"status: %@", str);
}

- (IBAction) newDocument:(id) sender
{
	[[webView window] makeKeyAndOrderFront:sender];
}

- (IBAction) script:(id) sender
{ // evaluate java script
	NSString *r=[webView stringByEvaluatingJavaScriptFromString:[sender stringValue]];
	if(!r)
		r=@"<nil>";
	[result setStringValue:r];
	[sender setStringValue:@""];	// clear
	[[sender window] makeFirstResponder:sender];	// stay first responder
}

// delegate methods

- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request
{
	[self showStatus:[NSString stringWithFormat:@"asked to create web view for request: %@", request]];
	return nil;
}

- (void) webViewShow:(WebView *)sender
{
	[self showStatus:@"asked to show window of web view"];
}

- (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame
{
    // Only report feedback for the main frame.
    if(frame == [sender mainFrame])
		{
		NSURL *url=[[[frame provisionalDataSource] request] URL];
		[self showStatus:[NSString stringWithFormat:@"Loading %@ ...", url]];
		}
}

- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
{
#if 1
	NSLog(@"title %@ for frame %@", title, frame);
#endif
    if(frame == [sender mainFrame])
        [[sender window] setTitle:title];
}

- (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
    // Only report feedback for the main frame.
	NSLog(@"webview=%@", sender);
	NSLog(@"webview subviews=%@", [sender subviews]);
	NSLog(@"webview mainFrame=%@", [sender mainFrame]);
	NSLog(@"frame=%@", frame);
	NSLog(@"frame childFrames=%@", [frame childFrames]);
	NSLog(@"frame dataSource=%@", [frame dataSource]);
	NSLog(@"frame dataSource pageTitle=%@", [[frame dataSource] pageTitle]);
	NSLog(@"frame dataSource textEncodingName=%@", [[frame dataSource] textEncodingName]);
	NSLog(@"frame dataSource initialRequest=%@", [[frame dataSource] initialRequest]);
	NSLog(@"frame dataSource request=%@", [[frame dataSource] request]);
	NSLog(@"frame dataSource subresources=%@", [[frame dataSource] subresources]);
	NSLog(@"frame dataSource mainresource=%@", [[frame dataSource] mainResource]);
	NSLog(@"frame DOMDocument=%@", [frame DOMDocument]);
	NSLog(@"frame frameElement=%@", [frame frameElement]);
	NSLog(@"frame frameView=%@", [frame frameView]);
	NSLog(@"frame name=%@", [frame name]);
	NSLog(@"frame parentFrame=%@", [frame parentFrame]);
	NSLog(@"frame provisionalDataSource=%@", [frame provisionalDataSource]);
	NSLog(@"frame webView=%@", [frame webView]);
	// test: print subviews hierarchy
    if(frame == [sender mainFrame])
		{
		[self showStatus:@"Done."];
		}
	else
		{
		[self showStatus:@"Subframe Done."];
		}
}

@end
