Annotation of chimera/MyBrowserView.mm, revision 1.19

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

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