File:  [mozdev] / chimera / MyBrowserView.mm
Revision 1.12: download - view: text, annotated - select for diffs - revision graph
Thu Feb 7 05:11:38 2002 UTC (17 years, 10 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Checking in a prototype sidebar.

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

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