File:  [mozdev] / chimera / MyBrowserView.mm
Revision 1.21: download - view: text, annotated - select for diffs - revision graph
Mon Feb 25 08:44:17 2002 UTC (17 years, 7 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
A bunch of new stuff.  window.open now works, and security is getting closer.

/*
 *  MyBrowserView.mm
 */

#import "MyBrowserView.h"
#import "BrowserWindowController.h"

#include "nsCOMPtr.h"
#include "nsIServiceManager.h"
#include "nsIIOService.h"

#define DOCUMENT_DONE_STRING @"Document: Done"
#define LOADING_STRING @"Loading..."

static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";

@implementation MyBrowserView

-(void)windowClosed
{
    // Break the cycle.
    [browserView setContainer: nil];
    [browserView removeListener: self];
    [browserView destroyWebBrowser];
}

-(void)dealloc
{
    printf("My browser view died.\n");
    
    [[NSNotificationCenter defaultCenter] removeObserver: self];
    
	[defaultStatus release];
    [loadingStatus release];
    
	[super dealloc];
}

- (IBAction)load:(id)sender
{
  [browserView loadURI:[NSURL URLWithString:[urlbar stringValue]] flags:NSLoadFlagsNone];
}

-(void)disconnectView
{
    urlbar = nil;
    status = nil;
    progress = nil;
    progressSuper = nil;
    mIsPrimary = NO;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    [browserView setActive: NO];
}

- (id)initWithFrame:(NSRect)frameRect
{
    [super initWithFrame: frameRect];
     browserView = [[[NSBrowserView alloc] initWithFrame:[self bounds]] autorelease];
    [self addSubview:browserView];
    [browserView setContainer:self];
    [browserView addListener:self];
    mIsBusy = NO;
    return self;
}

-(void)makePrimaryBrowserView: (NSTabViewItem*)tab urlbar: (id)aUrlbar status: (id)aStatus
    progress: (id)aProgress windowController: aWindowController
{
  mTab = tab;
  urlbar = aUrlbar;
  status = aStatus;
  progress = aProgress;
  progressSuper = [aProgress superview];
  mWindowController = aWindowController;
  
  if (!mIsBusy)
      [progress removeFromSuperview];
  
  defaultStatus = NULL;
  loadingStatus = DOCUMENT_DONE_STRING;
  [status setStringValue:loadingStatus];
  
  mIsPrimary = YES;
 
  nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  if (!ioService)
    return;
  PRBool offline = PR_FALSE;
  ioService->GetOffline(&offline);
  mOffline = offline;
  
  if (mWindowController) // Only register if we're the content area.
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(offlineModeChanged:)
        name:@"offlineModeChanged"
        object:nil];
        
  // Update the URL bar.
  [mWindowController updateLocationFields:[self getCurrentURLSpec]];
}

-(NSString*)getCurrentURLSpec
{
  return [browserView getCurrentURLSpec];
}

- (void)awakeFromNib 
{
}

- (void)setFrame:(NSRect)frameRect
{
  [super setFrame:frameRect];
  NSRect bounds = [self bounds];
  [browserView setFrame:bounds];
}

-(BOOL)isBusy
{
  return mIsBusy;
}

- (void)onLoadingStarted 
{
  if (defaultStatus) {
    [defaultStatus release];
    defaultStatus = NULL;
  }

  [progressSuper addSubview:progress];
  [progress setIndeterminate:YES];
  [progress startAnimation:self];

  loadingStatus = LOADING_STRING;
  [status setStringValue:loadingStatus];

  mIsBusy = YES;
  [mTab setLabel: LOADING_STRING];
  
  if (mWindowController)
    [mWindowController updateToolbarItems];
}

- (void)onLoadingCompleted:(BOOL)succeeded
{
  [progress setIndeterminate:YES];
  [progress stopAnimation:self];
  [progress removeFromSuperview];

  loadingStatus = DOCUMENT_DONE_STRING;
  if (defaultStatus) {
    [status setStringValue:defaultStatus];
  }
  else {
    [status setStringValue:loadingStatus];
  }

  mIsBusy = NO;
  
  if (mWindowController)
    [mWindowController updateToolbarItems];
}

- (void)onProgressChange:(int)currentBytes outOf:(int)maxBytes 
{
  if (maxBytes > 0) {
    BOOL isIndeterminate = [progress isIndeterminate];
    if (isIndeterminate) {
      [progress setIndeterminate:NO];
    }
    double val = ((double)currentBytes / (double)maxBytes) * 100.0;
    [progress setDoubleValue:val];
  }
}

- (void)onLocationChange:(NSURL*)url 
{
  NSString* spec = [url absoluteString];
  [mWindowController updateLocationFields:spec];
}

- (void)onStatusChange:(NSString*)aStatusString
{
  [status setStringValue: aStatusString];
}

- (void)setStatus:(NSString *)statusString ofType:(NSStatusType)type 
{
  if (type == NSStatusTypeScriptDefault) {
    if (defaultStatus) {
      [defaultStatus release];
    }
    defaultStatus = statusString;
    if (defaultStatus) {
      [defaultStatus retain];
    }
  }
  else if (!statusString) {
    if (defaultStatus) {
      [status setStringValue:defaultStatus];
    }
    else {
      [status setStringValue:loadingStatus];
    }      
  }
  else {
    [status setStringValue:statusString];
  }
}

- (NSString *)title 
{
  return [[mWindowController window] title];
}

- (void)setTitle:(NSString *)title
{
    if (!mWindowController)
        return; // Sidebar panels can't affect the window title.
    
    // We must be the primary content area.
    if (mIsPrimary) {
        if (mOffline) {
            NSString* newTitle;
            if (title && ![title isEqualToString:@""])
                newTitle = [title stringByAppendingString: @" [Working Offline]"];
            else
                newTitle = @"Untitled [Working Offline]";
            [[mWindowController window] setTitle: newTitle];
        }
        else {
            if (title && ![title isEqualToString:@""])
                [[mWindowController window] setTitle:title];
            else
                [[mWindowController window] setTitle:@"Untitled"];
        }
    }
    
    // Always set the tab.
    if (title && ![title isEqualToString:@""])
        [mTab setLabel:title];
    else
        [mTab setLabel:@"Untitled"];
}

- (void)offlineModeChanged: (NSNotification*)aNotification
{
    nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
    if (!ioService)
        return;
    PRBool offline = PR_FALSE;
    ioService->GetOffline(&offline);
    mOffline = offline;
    
    if (mOffline) {
        NSString* newTitle = [[[mWindowController window] title] stringByAppendingString: @" [Working Offline]"];
        [[mWindowController window] setTitle: newTitle];
    }
    else {
        NSArray* titleItems = [[[mWindowController window] title] componentsSeparatedByString:@" [Working Offline]"];
        [[mWindowController window] setTitle: [titleItems objectAtIndex: 0]];
    }
}

- (void)sizeBrowserTo:(NSSize)dimensions
{
  NSRect bounds = [self bounds];
  float dx = dimensions.width - bounds.size.width;
  float dy = dimensions.height - bounds.size.height;

  NSRect frame = [[self window] frame];
  frame.size.width += dx;
  frame.size.height += dy;

  [[self window] setFrame:frame display:YES];
}

- (NSBrowserView*)createBrowserWindow:(unsigned int)mask
{
  BrowserWindowController* controller = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  [controller enterModalSession];
  return [[controller getMyBrowserView] getBrowserView];
}

- (NSBrowserView*)getBrowserView
{
  return browserView;
}

@end

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>