File:  [mozdev] / chimera / MyBrowserView.mm
Revision 1.13: download - view: text, annotated - select for diffs - revision graph
Sun Feb 10 01:12:05 2002 UTC (17 years, 3 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
The status bar is hooked up now.  Also adding debugging printfs.

    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)awakeFromNib 
   44: {
   45:   browserView = [[[NSBrowserView alloc] initWithFrame:[self bounds]] autorelease];
   46:   [self addSubview:browserView];
   47:   [browserView setContainer:self];
   48:   [browserView addListener:self];
   49: 
   50:   defaultStatus = NULL;
   51:   loadingStatus = DOCUMENT_DONE_STRING;
   52:   [status setStringValue:loadingStatus];
   53:   mIsBusy = NO;
   54:   
   55:   [progress retain];
   56:   [progress removeFromSuperview];
   57:   
   58:   nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
   59:   if (!ioService)
   60:     return;
   61:   PRBool offline = PR_FALSE;
   62:   ioService->GetOffline(&offline);
   63:   mOffline = offline;
   64:   
   65:   if (mWindowController) // Only register if we're the content area.
   66:     [[NSNotificationCenter defaultCenter] addObserver:self
   67:         selector:@selector(offlineModeChanged:)
   68:         name:@"offlineModeChanged"
   69:         object:nil];
   70: }
   71: 
   72: - (void)setFrame:(NSRect)frameRect
   73: {
   74:   [super setFrame:frameRect];
   75:   NSRect bounds = [self bounds];
   76:   [browserView setFrame:bounds];
   77: }
   78: 
   79: -(BOOL)isBusy
   80: {
   81:   return mIsBusy;
   82: }
   83: 
   84: - (void)onLoadingStarted 
   85: {
   86:   if (defaultStatus) {
   87:     [defaultStatus release];
   88:     defaultStatus = NULL;
   89:   }
   90: 
   91:   [progressSuper addSubview:progress];
   92:   [progress release];
   93:   [progress setIndeterminate:YES];
   94:   [progress startAnimation:self];
   95: 
   96:   loadingStatus = LOADING_STRING;
   97:   [status setStringValue:loadingStatus];
   98: 
   99:   mIsBusy = YES;
  100:   if (mWindowController)
  101:     [mWindowController updateToolbarItems];
  102: }
  103: 
  104: - (void)onLoadingCompleted:(BOOL)succeeded
  105: {
  106:   [progress setIndeterminate:YES];
  107:   [progress stopAnimation:self];
  108:   [progress retain];
  109:   [progress removeFromSuperview];
  110: 
  111:   loadingStatus = DOCUMENT_DONE_STRING;
  112:   if (defaultStatus) {
  113:     [status setStringValue:defaultStatus];
  114:   }
  115:   else {
  116:     [status setStringValue:loadingStatus];
  117:   }
  118: 
  119:   mIsBusy = NO;
  120:   
  121:   if (mWindowController)
  122:     [mWindowController updateToolbarItems];
  123: }
  124: 
  125: - (void)onProgressChange:(int)currentBytes outOf:(int)maxBytes 
  126: {
  127:   if (maxBytes > 0) {
  128:     BOOL isIndeterminate = [progress isIndeterminate];
  129:     if (isIndeterminate) {
  130:       [progress setIndeterminate:NO];
  131:     }
  132:     double val = ((double)currentBytes / (double)maxBytes) * 100.0;
  133:     [progress setDoubleValue:val];
  134:   }
  135: }
  136: 
  137: - (void)onLocationChange:(NSURL*)url 
  138: {
  139:   NSString* spec = [url absoluteString];
  140:   [mWindowController updateLocationFields:spec];
  141: }
  142: 
  143: - (void)onStatusChange:(NSString*)aStatusString
  144: {
  145:   [status setStringValue: aStatusString];
  146: }
  147: 
  148: - (void)setStatus:(NSString *)statusString ofType:(NSStatusType)type 
  149: {
  150:   if (type == NSStatusTypeScriptDefault) {
  151:     if (defaultStatus) {
  152:       [defaultStatus release];
  153:     }
  154:     defaultStatus = statusString;
  155:     if (defaultStatus) {
  156:       [defaultStatus retain];
  157:     }
  158:   }
  159:   else if (!statusString) {
  160:     if (defaultStatus) {
  161:       [status setStringValue:defaultStatus];
  162:     }
  163:     else {
  164:       [status setStringValue:loadingStatus];
  165:     }      
  166:   }
  167:   else {
  168:     [status setStringValue:statusString];
  169:   }
  170: }
  171: 
  172: - (NSString *)title 
  173: {
  174:   return [[mWindowController window] title];
  175: }
  176: 
  177: - (void)setTitle:(NSString *)title
  178: {
  179:     if (!mWindowController)
  180:         return; // Sidebar panels can't affect the window title.
  181:         
  182:     if (mOffline) {
  183:         NSString* newTitle;
  184:         if (title && ![title isEqualToString:@""])
  185:             newTitle = [title stringByAppendingString: @" [Working Offline]"];
  186:         else
  187:             newTitle = @"Untitled [Working Offline]";
  188:         [[mWindowController window] setTitle: newTitle];
  189:     }
  190:     else {
  191:         if (title && ![title isEqualToString:@""])
  192:             [[mWindowController window] setTitle:title];
  193:         else
  194:             [[mWindowController window] setTitle:@"Untitled"];
  195:     }
  196: }
  197: 
  198: - (void)offlineModeChanged: (NSNotification*)aNotification
  199: {
  200:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  201:     if (!ioService)
  202:         return;
  203:     PRBool offline = PR_FALSE;
  204:     ioService->GetOffline(&offline);
  205:     mOffline = offline;
  206:     
  207:     if (mOffline) {
  208:         NSString* newTitle = [[[mWindowController window] title] stringByAppendingString: @" [Working Offline]"];
  209:         [[mWindowController window] setTitle: newTitle];
  210:     }
  211:     else {
  212:         NSArray* titleItems = [[[mWindowController window] title] componentsSeparatedByString:@" [Working Offline]"];
  213:         [[mWindowController window] setTitle: [titleItems objectAtIndex: 0]];
  214:     }
  215: }
  216: 
  217: - (void)sizeBrowserTo:(NSSize)dimensions
  218: {
  219:   NSRect bounds = [self bounds];
  220:   float dx = dimensions.width - bounds.size.width;
  221:   float dy = dimensions.height - bounds.size.height;
  222: 
  223:   NSRect frame = [[self window] frame];
  224:   frame.size.width += dx;
  225:   frame.size.height += dy;
  226: 
  227:   [[self window] setFrame:frame display:YES];
  228: }
  229: 
  230: - (NSBrowserView*)createBrowserWindow:(unsigned int)mask
  231: {
  232:   // XXX not implemented 
  233:   return NULL;
  234: }
  235: 
  236: - (NSBrowserView*)getBrowserView
  237: {
  238:   return browserView;
  239: }
  240: 
  241: @end

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