Annotation of chimera/MyBrowserView.mm, revision 1.15

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

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