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, 8 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
A bunch of new stuff.  window.open now works, and security is getting closer.

    1: /*
    2:  *  MyBrowserView.mm
    3:  */
    4: 
    5: #import "MyBrowserView.h"
    6: #import "BrowserWindowController.h"
    7: 
    8: #include "nsCOMPtr.h"
    9: #include "nsIServiceManager.h"
   10: #include "nsIIOService.h"
   11: 
   12: #define DOCUMENT_DONE_STRING @"Document: Done"
   13: #define LOADING_STRING @"Loading..."
   14: 
   15: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
   16: 
   17: @implementation MyBrowserView
   18: 
   19: -(void)windowClosed
   20: {
   21:     // Break the cycle.
   22:     [browserView setContainer: nil];
   23:     [browserView removeListener: self];
   24:     [browserView destroyWebBrowser];
   25: }
   26: 
   27: -(void)dealloc
   28: {
   29:     printf("My browser view died.\n");
   30:     
   31:     [[NSNotificationCenter defaultCenter] removeObserver: self];
   32:     
   33: 	[defaultStatus release];
   34:     [loadingStatus release];
   35:     
   36: 	[super dealloc];
   37: }
   38: 
   39: - (IBAction)load:(id)sender
   40: {
   41:   [browserView loadURI:[NSURL URLWithString:[urlbar stringValue]] flags:NSLoadFlagsNone];
   42: }
   43: 
   44: -(void)disconnectView
   45: {
   46:     urlbar = nil;
   47:     status = nil;
   48:     progress = nil;
   49:     progressSuper = nil;
   50:     mIsPrimary = NO;
   51:     [[NSNotificationCenter defaultCenter] removeObserver:self];
   52:     
   53:     [browserView setActive: NO];
   54: }
   55: 
   56: - (id)initWithFrame:(NSRect)frameRect
   57: {
   58:     [super initWithFrame: frameRect];
   59:      browserView = [[[NSBrowserView alloc] initWithFrame:[self bounds]] autorelease];
   60:     [self addSubview:browserView];
   61:     [browserView setContainer:self];
   62:     [browserView addListener:self];
   63:     mIsBusy = NO;
   64:     return self;
   65: }
   66: 
   67: -(void)makePrimaryBrowserView: (NSTabViewItem*)tab urlbar: (id)aUrlbar status: (id)aStatus
   68:     progress: (id)aProgress windowController: aWindowController
   69: {
   70:   mTab = tab;
   71:   urlbar = aUrlbar;
   72:   status = aStatus;
   73:   progress = aProgress;
   74:   progressSuper = [aProgress superview];
   75:   mWindowController = aWindowController;
   76:   
   77:   if (!mIsBusy)
   78:       [progress removeFromSuperview];
   79:   
   80:   defaultStatus = NULL;
   81:   loadingStatus = DOCUMENT_DONE_STRING;
   82:   [status setStringValue:loadingStatus];
   83:   
   84:   mIsPrimary = YES;
   85:  
   86:   nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
   87:   if (!ioService)
   88:     return;
   89:   PRBool offline = PR_FALSE;
   90:   ioService->GetOffline(&offline);
   91:   mOffline = offline;
   92:   
   93:   if (mWindowController) // Only register if we're the content area.
   94:     [[NSNotificationCenter defaultCenter] addObserver:self
   95:         selector:@selector(offlineModeChanged:)
   96:         name:@"offlineModeChanged"
   97:         object:nil];
   98:         
   99:   // Update the URL bar.
  100:   [mWindowController updateLocationFields:[self getCurrentURLSpec]];
  101: }
  102: 
  103: -(NSString*)getCurrentURLSpec
  104: {
  105:   return [browserView getCurrentURLSpec];
  106: }
  107: 
  108: - (void)awakeFromNib 
  109: {
  110: }
  111: 
  112: - (void)setFrame:(NSRect)frameRect
  113: {
  114:   [super setFrame:frameRect];
  115:   NSRect bounds = [self bounds];
  116:   [browserView setFrame:bounds];
  117: }
  118: 
  119: -(BOOL)isBusy
  120: {
  121:   return mIsBusy;
  122: }
  123: 
  124: - (void)onLoadingStarted 
  125: {
  126:   if (defaultStatus) {
  127:     [defaultStatus release];
  128:     defaultStatus = NULL;
  129:   }
  130: 
  131:   [progressSuper addSubview:progress];
  132:   [progress setIndeterminate:YES];
  133:   [progress startAnimation:self];
  134: 
  135:   loadingStatus = LOADING_STRING;
  136:   [status setStringValue:loadingStatus];
  137: 
  138:   mIsBusy = YES;
  139:   [mTab setLabel: LOADING_STRING];
  140:   
  141:   if (mWindowController)
  142:     [mWindowController updateToolbarItems];
  143: }
  144: 
  145: - (void)onLoadingCompleted:(BOOL)succeeded
  146: {
  147:   [progress setIndeterminate:YES];
  148:   [progress stopAnimation:self];
  149:   [progress removeFromSuperview];
  150: 
  151:   loadingStatus = DOCUMENT_DONE_STRING;
  152:   if (defaultStatus) {
  153:     [status setStringValue:defaultStatus];
  154:   }
  155:   else {
  156:     [status setStringValue:loadingStatus];
  157:   }
  158: 
  159:   mIsBusy = NO;
  160:   
  161:   if (mWindowController)
  162:     [mWindowController updateToolbarItems];
  163: }
  164: 
  165: - (void)onProgressChange:(int)currentBytes outOf:(int)maxBytes 
  166: {
  167:   if (maxBytes > 0) {
  168:     BOOL isIndeterminate = [progress isIndeterminate];
  169:     if (isIndeterminate) {
  170:       [progress setIndeterminate:NO];
  171:     }
  172:     double val = ((double)currentBytes / (double)maxBytes) * 100.0;
  173:     [progress setDoubleValue:val];
  174:   }
  175: }
  176: 
  177: - (void)onLocationChange:(NSURL*)url 
  178: {
  179:   NSString* spec = [url absoluteString];
  180:   [mWindowController updateLocationFields:spec];
  181: }
  182: 
  183: - (void)onStatusChange:(NSString*)aStatusString
  184: {
  185:   [status setStringValue: aStatusString];
  186: }
  187: 
  188: - (void)setStatus:(NSString *)statusString ofType:(NSStatusType)type 
  189: {
  190:   if (type == NSStatusTypeScriptDefault) {
  191:     if (defaultStatus) {
  192:       [defaultStatus release];
  193:     }
  194:     defaultStatus = statusString;
  195:     if (defaultStatus) {
  196:       [defaultStatus retain];
  197:     }
  198:   }
  199:   else if (!statusString) {
  200:     if (defaultStatus) {
  201:       [status setStringValue:defaultStatus];
  202:     }
  203:     else {
  204:       [status setStringValue:loadingStatus];
  205:     }      
  206:   }
  207:   else {
  208:     [status setStringValue:statusString];
  209:   }
  210: }
  211: 
  212: - (NSString *)title 
  213: {
  214:   return [[mWindowController window] title];
  215: }
  216: 
  217: - (void)setTitle:(NSString *)title
  218: {
  219:     if (!mWindowController)
  220:         return; // Sidebar panels can't affect the window title.
  221:     
  222:     // We must be the primary content area.
  223:     if (mIsPrimary) {
  224:         if (mOffline) {
  225:             NSString* newTitle;
  226:             if (title && ![title isEqualToString:@""])
  227:                 newTitle = [title stringByAppendingString: @" [Working Offline]"];
  228:             else
  229:                 newTitle = @"Untitled [Working Offline]";
  230:             [[mWindowController window] setTitle: newTitle];
  231:         }
  232:         else {
  233:             if (title && ![title isEqualToString:@""])
  234:                 [[mWindowController window] setTitle:title];
  235:             else
  236:                 [[mWindowController window] setTitle:@"Untitled"];
  237:         }
  238:     }
  239:     
  240:     // Always set the tab.
  241:     if (title && ![title isEqualToString:@""])
  242:         [mTab setLabel:title];
  243:     else
  244:         [mTab setLabel:@"Untitled"];
  245: }
  246: 
  247: - (void)offlineModeChanged: (NSNotification*)aNotification
  248: {
  249:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  250:     if (!ioService)
  251:         return;
  252:     PRBool offline = PR_FALSE;
  253:     ioService->GetOffline(&offline);
  254:     mOffline = offline;
  255:     
  256:     if (mOffline) {
  257:         NSString* newTitle = [[[mWindowController window] title] stringByAppendingString: @" [Working Offline]"];
  258:         [[mWindowController window] setTitle: newTitle];
  259:     }
  260:     else {
  261:         NSArray* titleItems = [[[mWindowController window] title] componentsSeparatedByString:@" [Working Offline]"];
  262:         [[mWindowController window] setTitle: [titleItems objectAtIndex: 0]];
  263:     }
  264: }
  265: 
  266: - (void)sizeBrowserTo:(NSSize)dimensions
  267: {
  268:   NSRect bounds = [self bounds];
  269:   float dx = dimensions.width - bounds.size.width;
  270:   float dy = dimensions.height - bounds.size.height;
  271: 
  272:   NSRect frame = [[self window] frame];
  273:   frame.size.width += dx;
  274:   frame.size.height += dy;
  275: 
  276:   [[self window] setFrame:frame display:YES];
  277: }
  278: 
  279: - (NSBrowserView*)createBrowserWindow:(unsigned int)mask
  280: {
  281:   BrowserWindowController* controller = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  282:   [controller enterModalSession];
  283:   return [[controller getMyBrowserView] getBrowserView];
  284: }
  285: 
  286: - (NSBrowserView*)getBrowserView
  287: {
  288:   return browserView;
  289: }
  290: 
  291: @end

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