File:  [mozdev] / chimera / MyBrowserView.mm
Revision 1.18: download - view: text, annotated - select for diffs - revision graph
Wed Feb 13 01:58:55 2002 UTC (17 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Textifleds show up now.

    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:     //[browserView setActive: NO];
   53: }
   54: 
   55: - (id)initWithFrame:(NSRect)frameRect
   56: {
   57:     [super initWithFrame: frameRect];
   58:      browserView = [[[NSBrowserView alloc] initWithFrame:[self bounds]] autorelease];
   59:     [self addSubview:browserView];
   60:     [browserView setContainer:self];
   61:     [browserView addListener:self];
   62:     mIsBusy = NO;
   63:     return self;
   64: }
   65: 
   66: -(void)makePrimaryBrowserView: (NSTabViewItem*)tab urlbar: (id)aUrlbar status: (id)aStatus
   67:     progress: (id)aProgress windowController: aWindowController
   68: {
   69:   mTab = tab;
   70:   urlbar = aUrlbar;
   71:   status = aStatus;
   72:   progress = aProgress;
   73:   progressSuper = [aProgress superview];
   74:   mWindowController = aWindowController;
   75:   
   76:   if (!mIsBusy) {
   77:     [progress retain];
   78:     [progress removeFromSuperview];
   79:   }
   80:   
   81:   defaultStatus = NULL;
   82:   loadingStatus = DOCUMENT_DONE_STRING;
   83:   [status setStringValue:loadingStatus];
   84:   
   85:   mIsPrimary = YES;
   86:  
   87:   nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
   88:   if (!ioService)
   89:     return;
   90:   PRBool offline = PR_FALSE;
   91:   ioService->GetOffline(&offline);
   92:   mOffline = offline;
   93:   
   94:   if (mWindowController) // Only register if we're the content area.
   95:     [[NSNotificationCenter defaultCenter] addObserver:self
   96:         selector:@selector(offlineModeChanged:)
   97:         name:@"offlineModeChanged"
   98:         object:nil];
   99:         
  100:   // Update the URL bar.
  101:   [mWindowController updateLocationFields:[self getCurrentURLSpec]];
  102:   
  103:   //[browserView setActive: YES];
  104: }
  105: 
  106: -(NSString*)getCurrentURLSpec
  107: {
  108:   return [browserView getCurrentURLSpec];
  109: }
  110: 
  111: - (void)awakeFromNib 
  112: {
  113: }
  114: 
  115: - (void)setFrame:(NSRect)frameRect
  116: {
  117:   [super setFrame:frameRect];
  118:   NSRect bounds = [self bounds];
  119:   [browserView setFrame:bounds];
  120: }
  121: 
  122: -(BOOL)isBusy
  123: {
  124:   return mIsBusy;
  125: }
  126: 
  127: - (void)onLoadingStarted 
  128: {
  129:   if (defaultStatus) {
  130:     [defaultStatus release];
  131:     defaultStatus = NULL;
  132:   }
  133: 
  134:   [progressSuper addSubview:progress];
  135:   [progress release];
  136:   [progress setIndeterminate:YES];
  137:   [progress startAnimation:self];
  138: 
  139:   loadingStatus = LOADING_STRING;
  140:   [status setStringValue:loadingStatus];
  141: 
  142:   mIsBusy = YES;
  143:   [mTab setLabel: LOADING_STRING];
  144:   
  145:   if (mWindowController)
  146:     [mWindowController updateToolbarItems];
  147: }
  148: 
  149: - (void)onLoadingCompleted:(BOOL)succeeded
  150: {
  151:   [progress setIndeterminate:YES];
  152:   [progress stopAnimation:self];
  153:   [progress retain];
  154:   [progress removeFromSuperview];
  155: 
  156:   loadingStatus = DOCUMENT_DONE_STRING;
  157:   if (defaultStatus) {
  158:     [status setStringValue:defaultStatus];
  159:   }
  160:   else {
  161:     [status setStringValue:loadingStatus];
  162:   }
  163: 
  164:   mIsBusy = NO;
  165:   
  166:   if (mWindowController)
  167:     [mWindowController updateToolbarItems];
  168: }
  169: 
  170: - (void)onProgressChange:(int)currentBytes outOf:(int)maxBytes 
  171: {
  172:   if (maxBytes > 0) {
  173:     BOOL isIndeterminate = [progress isIndeterminate];
  174:     if (isIndeterminate) {
  175:       [progress setIndeterminate:NO];
  176:     }
  177:     double val = ((double)currentBytes / (double)maxBytes) * 100.0;
  178:     [progress setDoubleValue:val];
  179:   }
  180: }
  181: 
  182: - (void)onLocationChange:(NSURL*)url 
  183: {
  184:   NSString* spec = [url absoluteString];
  185:   [mWindowController updateLocationFields:spec];
  186: }
  187: 
  188: - (void)onStatusChange:(NSString*)aStatusString
  189: {
  190:   [status setStringValue: aStatusString];
  191: }
  192: 
  193: - (void)setStatus:(NSString *)statusString ofType:(NSStatusType)type 
  194: {
  195:   if (type == NSStatusTypeScriptDefault) {
  196:     if (defaultStatus) {
  197:       [defaultStatus release];
  198:     }
  199:     defaultStatus = statusString;
  200:     if (defaultStatus) {
  201:       [defaultStatus retain];
  202:     }
  203:   }
  204:   else if (!statusString) {
  205:     if (defaultStatus) {
  206:       [status setStringValue:defaultStatus];
  207:     }
  208:     else {
  209:       [status setStringValue:loadingStatus];
  210:     }      
  211:   }
  212:   else {
  213:     [status setStringValue:statusString];
  214:   }
  215: }
  216: 
  217: - (NSString *)title 
  218: {
  219:   return [[mWindowController window] title];
  220: }
  221: 
  222: - (void)setTitle:(NSString *)title
  223: {
  224:     if (!mWindowController)
  225:         return; // Sidebar panels can't affect the window title.
  226:     
  227:     // We must be the primary content area.
  228:     if (mIsPrimary) {
  229:         if (mOffline) {
  230:             NSString* newTitle;
  231:             if (title && ![title isEqualToString:@""])
  232:                 newTitle = [title stringByAppendingString: @" [Working Offline]"];
  233:             else
  234:                 newTitle = @"Untitled [Working Offline]";
  235:             [[mWindowController window] setTitle: newTitle];
  236:         }
  237:         else {
  238:             if (title && ![title isEqualToString:@""])
  239:                 [[mWindowController window] setTitle:title];
  240:             else
  241:                 [[mWindowController window] setTitle:@"Untitled"];
  242:         }
  243:     }
  244:     
  245:     // Always set the tab.
  246:     if (title && ![title isEqualToString:@""])
  247:         [mTab setLabel:title];
  248:     else
  249:         [mTab setLabel:@"Untitled"];
  250: }
  251: 
  252: - (void)offlineModeChanged: (NSNotification*)aNotification
  253: {
  254:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  255:     if (!ioService)
  256:         return;
  257:     PRBool offline = PR_FALSE;
  258:     ioService->GetOffline(&offline);
  259:     mOffline = offline;
  260:     
  261:     if (mOffline) {
  262:         NSString* newTitle = [[[mWindowController window] title] stringByAppendingString: @" [Working Offline]"];
  263:         [[mWindowController window] setTitle: newTitle];
  264:     }
  265:     else {
  266:         NSArray* titleItems = [[[mWindowController window] title] componentsSeparatedByString:@" [Working Offline]"];
  267:         [[mWindowController window] setTitle: [titleItems objectAtIndex: 0]];
  268:     }
  269: }
  270: 
  271: - (void)sizeBrowserTo:(NSSize)dimensions
  272: {
  273:   NSRect bounds = [self bounds];
  274:   float dx = dimensions.width - bounds.size.width;
  275:   float dy = dimensions.height - bounds.size.height;
  276: 
  277:   NSRect frame = [[self window] frame];
  278:   frame.size.width += dx;
  279:   frame.size.height += dy;
  280: 
  281:   [[self window] setFrame:frame display:YES];
  282: }
  283: 
  284: - (NSBrowserView*)createBrowserWindow:(unsigned int)mask
  285: {
  286:   // XXX not implemented 
  287:   return NULL;
  288: }
  289: 
  290: - (NSBrowserView*)getBrowserView
  291: {
  292:   return browserView;
  293: }
  294: 
  295: @end

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