/* BSD License: Based on SimpleWebView by Sean Heber  <sean@spiffytech.com>, J. Zdziarski  */

#include <sys/select.h>

#import <UIKit/UIView-Geometry.h>
#import <UIKit/UIView-Rendering.h>

#import "GreenstoneApplication.h"
#import "SimpleWebView.h"

// ****** change name away from SimpleWebView


@implementation MyWebView
- (void) gestureEnded:(struct __GSEvent *)event
{
    [ super gestureEnded: event ];
    [ _delegate gestureEnded: event ];
}

- (void) doubleTap:(struct __GSEvent *)event
{
    [ super doubleTap: event ];
    [ _delegate doubleTap: event ];
}


@end


@implementation SimpleWebView

-(void)view: (UIView*)v didSetFrame:(CGRect)f
{
    if (v == webView) {
        [ scroller setContentSize: f.size ];
    }
}

-(void)view:(id)v didDrawInRect:(CGRect)f duration:(float)d
{
    if (v == webView) {
        size = [ webView bounds ].size;
        if (size.height != lastSize.height
        || size.width != lastSize.width)
        {
            lastSize = size;
            [ scroller setContentSize: size ];
        }
    }
}

- (void)gestureEnded:(struct __GSEvent *)event {
    [ webView redrawScaledDocument ];
    [ webView setNeedsDisplay ];
    [ scroller setContentSize: [ webView bounds ].size ];
}

- (void)doubleTap:(struct __GSEvent *)event {
    struct timeval tv;
    tv.tv_sec = 2;
    tv.tv_usec = 0;
    select(NULL, NULL, NULL, NULL, &tv);
    [ webView redrawScaledDocument ];
    [ webView setNeedsDisplay ];
    [ scroller setContentSize: [ webView bounds ].size ];
}


-(void)dealloc
{
        [ urlRequest release ];
        [ webView release ];
        [ scroller release ];

        [ super dealloc ];
}

-(id)initWithFrame: (CGRect)frame
{
  return [self initWithFrame:frame withParent:nil];
}


-(id)initWithFrame: (CGRect)frame withParent:(UIView*)parent
{
    [ super initWithFrame: frame ];

    _parent = parent;
    urlRequest = nil;

    int frame_x_dim = frame.size.width;
    int frame_y_dim = frame.size.height;

    int nav_x_org = 0.0f;
    int nav_y_org = 0.0f;
    int nav_x_dim = frame_x_dim;
    int nav_y_dim = TOP_NAVBAR_HEIGHT;
    
    CGRect navrect = CGRectMake(nav_x_org,nav_y_org,nav_x_dim,nav_y_dim);
    UINavigationBar* navbar = [[UINavigationBar alloc] initWithFrame: navrect];

    [navbar showButtonsWithLeftTitle:@"Back" rightTitle:nil leftBack:YES];
    [navbar enableAnimation];
    [navbar setBarStyle: 2];  // Dark, transparent // ****
    [navbar setDelegate: self];

    int scroll_x_org = 0.0f;
    int scroll_y_org = 0.0f;
    int scroll_x_dim = frame_x_dim;
    int scroll_y_dim = frame_y_dim;

    CGRect scrollrect = CGRectMake(scroll_x_org,scroll_y_org,scroll_x_dim,scroll_y_dim);

    scroller = [ [ UIScroller alloc ] initWithFrame: scrollrect ];
    [ scroller setScrollingEnabled: YES ];
    [ scroller setAdjustForContentSizeChange: YES ];
    [ scroller setClipsSubviews: NO ];
    [ scroller setAllowsRubberBanding: YES ];
    [ scroller setDelegate: self ];

    webView = [ [ MyWebView alloc ] initWithFrame: [ scroller bounds ] ];
    [ webView setTilingEnabled: YES ];
    [ webView setTileSize: scrollrect.size ];
    [ webView setAutoresizes: YES];
    [ webView setDelegate: self];
    [ webView setEnabledGestures: 0xFF ];
    [ webView setSmoothsFonts: YES ];


    [ self addSubview: scroller ];
    [ scroller addSubview: webView ];

    [self addSubview: navbar];
	
    return self;
}


-(void) setParent:(UIView*)parent
{
  _parent = parent;
}


-(id) loadURL:(NSURL*)url
{
    CGPoint zero;
    zero.x = zero.y = 0;
    [scroller scrollPointVisibleAtTopLeft: zero];

    if (urlRequest != nil) { // ***** 
      [urlRequest release];
      urlRequest = nil;
    }

    urlRequest = [[NSURLRequest requestWithURL:url] retain];
    [webView loadRequest:urlRequest];
}


//***********
//* Delegates
//***********



-(void) navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button 
{
  switch (button) 
  {
    case 1:  // left
      if (_parent != nil) {
	// only do transition if there is a parent view to go back to
	int transStyle = UITransitionBothShiftRight;
	[GreenstoneApplication transitionView:transStyle fromView:self toView:_parent];
      }

      // need to think about deallocating self? // *****
      break;

    default:
      NSLog(@"Unrecognised button = %d",button);
      break;
  }
}

@end






