File:  [mozdev] / chimera / MyBrowserView.mm
Revision 1.17: download - view: text, annotated - select for diffs - revision graph
Tue Feb 12 00:18:34 2002 UTC (17 years, 3 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Tabs now update everything properly except for progress/status on a switch.

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

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