Annotation of chimera/MyBrowserView.mm, revision 1.17

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

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