File:  [mozdev] / chimera / MyBrowserView.mm
Revision 1.16: download - view: text, annotated - select for diffs - revision graph
Mon Feb 11 07:22:13 2002 UTC (17 years, 10 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Tabbed browsing lives.

    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: 
   99: - (void)awakeFromNib 
  100: {
  101: }
  102: 
  103: - (void)setFrame:(NSRect)frameRect
  104: {
  105:   [super setFrame:frameRect];
  106:   NSRect bounds = [self bounds];
  107:   [browserView setFrame:bounds];
  108: }
  109: 
  110: -(BOOL)isBusy
  111: {
  112:   return mIsBusy;
  113: }
  114: 
  115: - (void)onLoadingStarted 
  116: {
  117:   if (defaultStatus) {
  118:     [defaultStatus release];
  119:     defaultStatus = NULL;
  120:   }
  121: 
  122:   [progressSuper addSubview:progress];
  123:   [progress release];
  124:   [progress setIndeterminate:YES];
  125:   [progress startAnimation:self];
  126: 
  127:   loadingStatus = LOADING_STRING;
  128:   [status setStringValue:loadingStatus];
  129: 
  130:   mIsBusy = YES;
  131:   [mTab setLabel: LOADING_STRING];
  132:   
  133:   if (mWindowController)
  134:     [mWindowController updateToolbarItems];
  135: }
  136: 
  137: - (void)onLoadingCompleted:(BOOL)succeeded
  138: {
  139:   [progress setIndeterminate:YES];
  140:   [progress stopAnimation:self];
  141:   [progress retain];
  142:   [progress removeFromSuperview];
  143: 
  144:   loadingStatus = DOCUMENT_DONE_STRING;
  145:   if (defaultStatus) {
  146:     [status setStringValue:defaultStatus];
  147:   }
  148:   else {
  149:     [status setStringValue:loadingStatus];
  150:   }
  151: 
  152:   mIsBusy = NO;
  153:   
  154:   if (mWindowController)
  155:     [mWindowController updateToolbarItems];
  156: }
  157: 
  158: - (void)onProgressChange:(int)currentBytes outOf:(int)maxBytes 
  159: {
  160:   if (maxBytes > 0) {
  161:     BOOL isIndeterminate = [progress isIndeterminate];
  162:     if (isIndeterminate) {
  163:       [progress setIndeterminate:NO];
  164:     }
  165:     double val = ((double)currentBytes / (double)maxBytes) * 100.0;
  166:     [progress setDoubleValue:val];
  167:   }
  168: }
  169: 
  170: - (void)onLocationChange:(NSURL*)url 
  171: {
  172:   NSString* spec = [url absoluteString];
  173:   [mWindowController updateLocationFields:spec];
  174: }
  175: 
  176: - (void)onStatusChange:(NSString*)aStatusString
  177: {
  178:   [status setStringValue: aStatusString];
  179: }
  180: 
  181: - (void)setStatus:(NSString *)statusString ofType:(NSStatusType)type 
  182: {
  183:   if (type == NSStatusTypeScriptDefault) {
  184:     if (defaultStatus) {
  185:       [defaultStatus release];
  186:     }
  187:     defaultStatus = statusString;
  188:     if (defaultStatus) {
  189:       [defaultStatus retain];
  190:     }
  191:   }
  192:   else if (!statusString) {
  193:     if (defaultStatus) {
  194:       [status setStringValue:defaultStatus];
  195:     }
  196:     else {
  197:       [status setStringValue:loadingStatus];
  198:     }      
  199:   }
  200:   else {
  201:     [status setStringValue:statusString];
  202:   }
  203: }
  204: 
  205: - (NSString *)title 
  206: {
  207:   return [[mWindowController window] title];
  208: }
  209: 
  210: - (void)setTitle:(NSString *)title
  211: {
  212:     if (!mWindowController)
  213:         return; // Sidebar panels can't affect the window title.
  214:     
  215:     // We must be the primary content area.
  216:     if (mIsPrimary) {
  217:         if (mOffline) {
  218:             NSString* newTitle;
  219:             if (title && ![title isEqualToString:@""])
  220:                 newTitle = [title stringByAppendingString: @" [Working Offline]"];
  221:             else
  222:                 newTitle = @"Untitled [Working Offline]";
  223:             [[mWindowController window] setTitle: newTitle];
  224:         }
  225:         else {
  226:             if (title && ![title isEqualToString:@""])
  227:                 [[mWindowController window] setTitle:title];
  228:             else
  229:                 [[mWindowController window] setTitle:@"Untitled"];
  230:         }
  231:     }
  232:     
  233:     // Always set the tab.
  234:     if (title && ![title isEqualToString:@""])
  235:         [mTab setLabel:title];
  236:     else
  237:         [mTab setLabel:@"Untitled"];
  238: }
  239: 
  240: - (void)offlineModeChanged: (NSNotification*)aNotification
  241: {
  242:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  243:     if (!ioService)
  244:         return;
  245:     PRBool offline = PR_FALSE;
  246:     ioService->GetOffline(&offline);
  247:     mOffline = offline;
  248:     
  249:     if (mOffline) {
  250:         NSString* newTitle = [[[mWindowController window] title] stringByAppendingString: @" [Working Offline]"];
  251:         [[mWindowController window] setTitle: newTitle];
  252:     }
  253:     else {
  254:         NSArray* titleItems = [[[mWindowController window] title] componentsSeparatedByString:@" [Working Offline]"];
  255:         [[mWindowController window] setTitle: [titleItems objectAtIndex: 0]];
  256:     }
  257: }
  258: 
  259: - (void)sizeBrowserTo:(NSSize)dimensions
  260: {
  261:   NSRect bounds = [self bounds];
  262:   float dx = dimensions.width - bounds.size.width;
  263:   float dy = dimensions.height - bounds.size.height;
  264: 
  265:   NSRect frame = [[self window] frame];
  266:   frame.size.width += dx;
  267:   frame.size.height += dy;
  268: 
  269:   [[self window] setFrame:frame display:YES];
  270: }
  271: 
  272: - (NSBrowserView*)createBrowserWindow:(unsigned int)mask
  273: {
  274:   // XXX not implemented 
  275:   return NULL;
  276: }
  277: 
  278: - (NSBrowserView*)getBrowserView
  279: {
  280:   return browserView;
  281: }
  282: 
  283: @end

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