Annotation of chimera/MyBrowserView.mm, revision 1.20

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:     
1.19      hyatt      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.20    ! hyatt      76:   if (!mIsBusy)
        !            77:       [progress removeFromSuperview];
1.16      hyatt      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 setIndeterminate:YES];
                    132:   [progress startAnimation:self];
                    133: 
                    134:   loadingStatus = LOADING_STRING;
                    135:   [status setStringValue:loadingStatus];
                    136: 
                    137:   mIsBusy = YES;
1.16      hyatt     138:   [mTab setLabel: LOADING_STRING];
                    139:   
1.1       hyatt     140:   if (mWindowController)
                    141:     [mWindowController updateToolbarItems];
                    142: }
                    143: 
                    144: - (void)onLoadingCompleted:(BOOL)succeeded
                    145: {
                    146:   [progress setIndeterminate:YES];
                    147:   [progress stopAnimation:self];
                    148:   [progress removeFromSuperview];
                    149: 
                    150:   loadingStatus = DOCUMENT_DONE_STRING;
                    151:   if (defaultStatus) {
                    152:     [status setStringValue:defaultStatus];
                    153:   }
                    154:   else {
                    155:     [status setStringValue:loadingStatus];
                    156:   }
                    157: 
                    158:   mIsBusy = NO;
                    159:   
                    160:   if (mWindowController)
                    161:     [mWindowController updateToolbarItems];
                    162: }
                    163: 
                    164: - (void)onProgressChange:(int)currentBytes outOf:(int)maxBytes 
                    165: {
                    166:   if (maxBytes > 0) {
                    167:     BOOL isIndeterminate = [progress isIndeterminate];
                    168:     if (isIndeterminate) {
                    169:       [progress setIndeterminate:NO];
                    170:     }
                    171:     double val = ((double)currentBytes / (double)maxBytes) * 100.0;
                    172:     [progress setDoubleValue:val];
                    173:   }
                    174: }
                    175: 
                    176: - (void)onLocationChange:(NSURL*)url 
                    177: {
                    178:   NSString* spec = [url absoluteString];
1.4       macserv   179:   [mWindowController updateLocationFields:spec];
1.13      hyatt     180: }
                    181: 
                    182: - (void)onStatusChange:(NSString*)aStatusString
                    183: {
                    184:   [status setStringValue: aStatusString];
1.1       hyatt     185: }
                    186: 
                    187: - (void)setStatus:(NSString *)statusString ofType:(NSStatusType)type 
                    188: {
                    189:   if (type == NSStatusTypeScriptDefault) {
                    190:     if (defaultStatus) {
                    191:       [defaultStatus release];
                    192:     }
                    193:     defaultStatus = statusString;
                    194:     if (defaultStatus) {
                    195:       [defaultStatus retain];
                    196:     }
                    197:   }
                    198:   else if (!statusString) {
                    199:     if (defaultStatus) {
                    200:       [status setStringValue:defaultStatus];
                    201:     }
                    202:     else {
                    203:       [status setStringValue:loadingStatus];
                    204:     }      
                    205:   }
                    206:   else {
                    207:     [status setStringValue:statusString];
                    208:   }
                    209: }
                    210: 
                    211: - (NSString *)title 
                    212: {
1.4       macserv   213:   return [[mWindowController window] title];
1.1       hyatt     214: }
                    215: 
                    216: - (void)setTitle:(NSString *)title
                    217: {
1.12      hyatt     218:     if (!mWindowController)
                    219:         return; // Sidebar panels can't affect the window title.
1.14      hyatt     220:     
                    221:     // We must be the primary content area.
                    222:     if (mIsPrimary) {
                    223:         if (mOffline) {
                    224:             NSString* newTitle;
                    225:             if (title && ![title isEqualToString:@""])
                    226:                 newTitle = [title stringByAppendingString: @" [Working Offline]"];
                    227:             else
                    228:                 newTitle = @"Untitled [Working Offline]";
                    229:             [[mWindowController window] setTitle: newTitle];
                    230:         }
                    231:         else {
                    232:             if (title && ![title isEqualToString:@""])
                    233:                 [[mWindowController window] setTitle:title];
                    234:             else
                    235:                 [[mWindowController window] setTitle:@"Untitled"];
                    236:         }
                    237:     }
                    238:     
                    239:     // Always set the tab.
                    240:     if (title && ![title isEqualToString:@""])
                    241:         [mTab setLabel:title];
                    242:     else
1.15      hyatt     243:         [mTab setLabel:@"Untitled"];
1.11      hyatt     244: }
                    245: 
                    246: - (void)offlineModeChanged: (NSNotification*)aNotification
                    247: {
                    248:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
                    249:     if (!ioService)
                    250:         return;
                    251:     PRBool offline = PR_FALSE;
                    252:     ioService->GetOffline(&offline);
                    253:     mOffline = offline;
                    254:     
                    255:     if (mOffline) {
                    256:         NSString* newTitle = [[[mWindowController window] title] stringByAppendingString: @" [Working Offline]"];
                    257:         [[mWindowController window] setTitle: newTitle];
                    258:     }
                    259:     else {
                    260:         NSArray* titleItems = [[[mWindowController window] title] componentsSeparatedByString:@" [Working Offline]"];
                    261:         [[mWindowController window] setTitle: [titleItems objectAtIndex: 0]];
1.4       macserv   262:     }
1.1       hyatt     263: }
                    264: 
                    265: - (void)sizeBrowserTo:(NSSize)dimensions
                    266: {
                    267:   NSRect bounds = [self bounds];
                    268:   float dx = dimensions.width - bounds.size.width;
                    269:   float dy = dimensions.height - bounds.size.height;
                    270: 
1.4       macserv   271:   NSRect frame = [[self window] frame];
1.1       hyatt     272:   frame.size.width += dx;
                    273:   frame.size.height += dy;
                    274: 
1.4       macserv   275:   [[self window] setFrame:frame display:YES];
1.1       hyatt     276: }
                    277: 
                    278: - (NSBrowserView*)createBrowserWindow:(unsigned int)mask
                    279: {
                    280:   // XXX not implemented 
                    281:   return NULL;
                    282: }
                    283: 
                    284: - (NSBrowserView*)getBrowserView
                    285: {
                    286:   return browserView;
                    287: }
                    288: 
                    289: @end

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