Annotation of chimera/MyBrowserView.mm, revision 1.18

1.2       macserv     1: /*
                      2:  *  MyBrowserView.mm
                      3:  */
                      4: 
1.1       hyatt       5: #import "MyBrowserView.h"
                      6: 
1.10      hyatt       7: #include "nsCOMPtr.h"
                      8: #include "nsIServiceManager.h"
                      9: #include "nsIIOService.h"
                     10: 
1.1       hyatt      11: #define DOCUMENT_DONE_STRING @"Document: Done"
                     12: #define LOADING_STRING @"Loading..."
                     13: 
1.10      hyatt      14: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
                     15: 
1.1       hyatt      16: @implementation MyBrowserView
                     17: 
1.7       hyatt      18: -(void)windowClosed
                     19: {
                     20:     // Break the cycle.
                     21:     [browserView setContainer: nil];
                     22:     [browserView removeListener: self];
1.9       hyatt      23:     [browserView destroyWebBrowser];
1.7       hyatt      24: }
                     25: 
1.1       hyatt      26: -(void)dealloc
                     27: {
1.13      hyatt      28:     printf("My browser view died.\n");
                     29:     
1.11      hyatt      30:     [[NSNotificationCenter defaultCenter] removeObserver: self];
                     31:     
1.3       macserv    32:        [defaultStatus release];
                     33:     [loadingStatus release];
                     34:     
                     35:        [super dealloc];
1.1       hyatt      36: }
                     37: 
                     38: - (IBAction)load:(id)sender
                     39: {
1.4       macserv    40:   [browserView loadURI:[NSURL URLWithString:[urlbar stringValue]] flags:NSLoadFlagsNone];
1.1       hyatt      41: }
                     42: 
1.16      hyatt      43: -(void)disconnectView
                     44: {
                     45:     urlbar = nil;
                     46:     status = nil;
                     47:     progress = nil;
                     48:     progressSuper = nil;
                     49:     mIsPrimary = NO;
                     50:     [[NSNotificationCenter defaultCenter] removeObserver:self];
1.18    ! hyatt      51:     
        !            52:     //[browserView setActive: NO];
1.16      hyatt      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: 
1.14      hyatt      66: -(void)makePrimaryBrowserView: (NSTabViewItem*)tab urlbar: (id)aUrlbar status: (id)aStatus
                     67:     progress: (id)aProgress windowController: aWindowController
1.1       hyatt      68: {
1.14      hyatt      69:   mTab = tab;
                     70:   urlbar = aUrlbar;
                     71:   status = aStatus;
                     72:   progress = aProgress;
                     73:   progressSuper = [aProgress superview];
                     74:   mWindowController = aWindowController;
                     75:   
1.16      hyatt      76:   if (!mIsBusy) {
                     77:     [progress retain];
                     78:     [progress removeFromSuperview];
                     79:   }
                     80:   
1.14      hyatt      81:   defaultStatus = NULL;
                     82:   loadingStatus = DOCUMENT_DONE_STRING;
                     83:   [status setStringValue:loadingStatus];
                     84:   
                     85:   mIsPrimary = YES;
                     86:  
1.10      hyatt      87:   nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
                     88:   if (!ioService)
                     89:     return;
                     90:   PRBool offline = PR_FALSE;
                     91:   ioService->GetOffline(&offline);
                     92:   mOffline = offline;
1.11      hyatt      93:   
1.12      hyatt      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];
1.17      hyatt      99:         
                    100:   // Update the URL bar.
                    101:   [mWindowController updateLocationFields:[self getCurrentURLSpec]];
1.18    ! hyatt     102:   
        !           103:   //[browserView setActive: YES];
1.17      hyatt     104: }
                    105: 
                    106: -(NSString*)getCurrentURLSpec
                    107: {
                    108:   return [browserView getCurrentURLSpec];
1.1       hyatt     109: }
                    110: 
1.14      hyatt     111: - (void)awakeFromNib 
                    112: {
                    113: }
                    114: 
1.1       hyatt     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;
1.16      hyatt     143:   [mTab setLabel: LOADING_STRING];
                    144:   
1.1       hyatt     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];
1.4       macserv   185:   [mWindowController updateLocationFields:spec];
1.13      hyatt     186: }
                    187: 
                    188: - (void)onStatusChange:(NSString*)aStatusString
                    189: {
                    190:   [status setStringValue: aStatusString];
1.1       hyatt     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: {
1.4       macserv   219:   return [[mWindowController window] title];
1.1       hyatt     220: }
                    221: 
                    222: - (void)setTitle:(NSString *)title
                    223: {
1.12      hyatt     224:     if (!mWindowController)
                    225:         return; // Sidebar panels can't affect the window title.
1.14      hyatt     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
1.15      hyatt     249:         [mTab setLabel:@"Untitled"];
1.11      hyatt     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]];
1.4       macserv   268:     }
1.1       hyatt     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: 
1.4       macserv   277:   NSRect frame = [[self window] frame];
1.1       hyatt     278:   frame.size.width += dx;
                    279:   frame.size.height += dy;
                    280: 
1.4       macserv   281:   [[self window] setFrame:frame display:YES];
1.1       hyatt     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>