Annotation of chimera/MyBrowserView.mm, revision 1.21

1.2       macserv     1: /*
                      2:  *  MyBrowserView.mm
                      3:  */
                      4: 
1.1       hyatt       5: #import "MyBrowserView.h"
1.21    ! hyatt       6: #import "BrowserWindowController.h"
1.1       hyatt       7: 
1.10      hyatt       8: #include "nsCOMPtr.h"
                      9: #include "nsIServiceManager.h"
                     10: #include "nsIIOService.h"
                     11: 
1.1       hyatt      12: #define DOCUMENT_DONE_STRING @"Document: Done"
                     13: #define LOADING_STRING @"Loading..."
                     14: 
1.10      hyatt      15: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
                     16: 
1.1       hyatt      17: @implementation MyBrowserView
                     18: 
1.7       hyatt      19: -(void)windowClosed
                     20: {
                     21:     // Break the cycle.
                     22:     [browserView setContainer: nil];
                     23:     [browserView removeListener: self];
1.9       hyatt      24:     [browserView destroyWebBrowser];
1.7       hyatt      25: }
                     26: 
1.1       hyatt      27: -(void)dealloc
                     28: {
1.13      hyatt      29:     printf("My browser view died.\n");
                     30:     
1.11      hyatt      31:     [[NSNotificationCenter defaultCenter] removeObserver: self];
                     32:     
1.3       macserv    33:        [defaultStatus release];
                     34:     [loadingStatus release];
                     35:     
                     36:        [super dealloc];
1.1       hyatt      37: }
                     38: 
                     39: - (IBAction)load:(id)sender
                     40: {
1.4       macserv    41:   [browserView loadURI:[NSURL URLWithString:[urlbar stringValue]] flags:NSLoadFlagsNone];
1.1       hyatt      42: }
                     43: 
1.16      hyatt      44: -(void)disconnectView
                     45: {
                     46:     urlbar = nil;
                     47:     status = nil;
                     48:     progress = nil;
                     49:     progressSuper = nil;
                     50:     mIsPrimary = NO;
                     51:     [[NSNotificationCenter defaultCenter] removeObserver:self];
1.18      hyatt      52:     
1.19      hyatt      53:     [browserView setActive: NO];
1.16      hyatt      54: }
                     55: 
                     56: - (id)initWithFrame:(NSRect)frameRect
                     57: {
                     58:     [super initWithFrame: frameRect];
                     59:      browserView = [[[NSBrowserView alloc] initWithFrame:[self bounds]] autorelease];
                     60:     [self addSubview:browserView];
                     61:     [browserView setContainer:self];
                     62:     [browserView addListener:self];
                     63:     mIsBusy = NO;
                     64:     return self;
                     65: }
                     66: 
1.14      hyatt      67: -(void)makePrimaryBrowserView: (NSTabViewItem*)tab urlbar: (id)aUrlbar status: (id)aStatus
                     68:     progress: (id)aProgress windowController: aWindowController
1.1       hyatt      69: {
1.14      hyatt      70:   mTab = tab;
                     71:   urlbar = aUrlbar;
                     72:   status = aStatus;
                     73:   progress = aProgress;
                     74:   progressSuper = [aProgress superview];
                     75:   mWindowController = aWindowController;
                     76:   
1.20      hyatt      77:   if (!mIsBusy)
                     78:       [progress removeFromSuperview];
1.16      hyatt      79:   
1.14      hyatt      80:   defaultStatus = NULL;
                     81:   loadingStatus = DOCUMENT_DONE_STRING;
                     82:   [status setStringValue:loadingStatus];
                     83:   
                     84:   mIsPrimary = YES;
                     85:  
1.10      hyatt      86:   nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
                     87:   if (!ioService)
                     88:     return;
                     89:   PRBool offline = PR_FALSE;
                     90:   ioService->GetOffline(&offline);
                     91:   mOffline = offline;
1.11      hyatt      92:   
1.12      hyatt      93:   if (mWindowController) // Only register if we're the content area.
                     94:     [[NSNotificationCenter defaultCenter] addObserver:self
                     95:         selector:@selector(offlineModeChanged:)
                     96:         name:@"offlineModeChanged"
                     97:         object:nil];
1.17      hyatt      98:         
                     99:   // Update the URL bar.
                    100:   [mWindowController updateLocationFields:[self getCurrentURLSpec]];
                    101: }
                    102: 
                    103: -(NSString*)getCurrentURLSpec
                    104: {
                    105:   return [browserView getCurrentURLSpec];
1.1       hyatt     106: }
                    107: 
1.14      hyatt     108: - (void)awakeFromNib 
                    109: {
                    110: }
                    111: 
1.1       hyatt     112: - (void)setFrame:(NSRect)frameRect
                    113: {
                    114:   [super setFrame:frameRect];
                    115:   NSRect bounds = [self bounds];
                    116:   [browserView setFrame:bounds];
                    117: }
                    118: 
                    119: -(BOOL)isBusy
                    120: {
                    121:   return mIsBusy;
                    122: }
                    123: 
                    124: - (void)onLoadingStarted 
                    125: {
                    126:   if (defaultStatus) {
                    127:     [defaultStatus release];
                    128:     defaultStatus = NULL;
                    129:   }
                    130: 
                    131:   [progressSuper addSubview:progress];
                    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 removeFromSuperview];
                    150: 
                    151:   loadingStatus = DOCUMENT_DONE_STRING;
                    152:   if (defaultStatus) {
                    153:     [status setStringValue:defaultStatus];
                    154:   }
                    155:   else {
                    156:     [status setStringValue:loadingStatus];
                    157:   }
                    158: 
                    159:   mIsBusy = NO;
                    160:   
                    161:   if (mWindowController)
                    162:     [mWindowController updateToolbarItems];
                    163: }
                    164: 
                    165: - (void)onProgressChange:(int)currentBytes outOf:(int)maxBytes 
                    166: {
                    167:   if (maxBytes > 0) {
                    168:     BOOL isIndeterminate = [progress isIndeterminate];
                    169:     if (isIndeterminate) {
                    170:       [progress setIndeterminate:NO];
                    171:     }
                    172:     double val = ((double)currentBytes / (double)maxBytes) * 100.0;
                    173:     [progress setDoubleValue:val];
                    174:   }
                    175: }
                    176: 
                    177: - (void)onLocationChange:(NSURL*)url 
                    178: {
                    179:   NSString* spec = [url absoluteString];
1.4       macserv   180:   [mWindowController updateLocationFields:spec];
1.13      hyatt     181: }
                    182: 
                    183: - (void)onStatusChange:(NSString*)aStatusString
                    184: {
                    185:   [status setStringValue: aStatusString];
1.1       hyatt     186: }
                    187: 
                    188: - (void)setStatus:(NSString *)statusString ofType:(NSStatusType)type 
                    189: {
                    190:   if (type == NSStatusTypeScriptDefault) {
                    191:     if (defaultStatus) {
                    192:       [defaultStatus release];
                    193:     }
                    194:     defaultStatus = statusString;
                    195:     if (defaultStatus) {
                    196:       [defaultStatus retain];
                    197:     }
                    198:   }
                    199:   else if (!statusString) {
                    200:     if (defaultStatus) {
                    201:       [status setStringValue:defaultStatus];
                    202:     }
                    203:     else {
                    204:       [status setStringValue:loadingStatus];
                    205:     }      
                    206:   }
                    207:   else {
                    208:     [status setStringValue:statusString];
                    209:   }
                    210: }
                    211: 
                    212: - (NSString *)title 
                    213: {
1.4       macserv   214:   return [[mWindowController window] title];
1.1       hyatt     215: }
                    216: 
                    217: - (void)setTitle:(NSString *)title
                    218: {
1.12      hyatt     219:     if (!mWindowController)
                    220:         return; // Sidebar panels can't affect the window title.
1.14      hyatt     221:     
                    222:     // We must be the primary content area.
                    223:     if (mIsPrimary) {
                    224:         if (mOffline) {
                    225:             NSString* newTitle;
                    226:             if (title && ![title isEqualToString:@""])
                    227:                 newTitle = [title stringByAppendingString: @" [Working Offline]"];
                    228:             else
                    229:                 newTitle = @"Untitled [Working Offline]";
                    230:             [[mWindowController window] setTitle: newTitle];
                    231:         }
                    232:         else {
                    233:             if (title && ![title isEqualToString:@""])
                    234:                 [[mWindowController window] setTitle:title];
                    235:             else
                    236:                 [[mWindowController window] setTitle:@"Untitled"];
                    237:         }
                    238:     }
                    239:     
                    240:     // Always set the tab.
                    241:     if (title && ![title isEqualToString:@""])
                    242:         [mTab setLabel:title];
                    243:     else
1.15      hyatt     244:         [mTab setLabel:@"Untitled"];
1.11      hyatt     245: }
                    246: 
                    247: - (void)offlineModeChanged: (NSNotification*)aNotification
                    248: {
                    249:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
                    250:     if (!ioService)
                    251:         return;
                    252:     PRBool offline = PR_FALSE;
                    253:     ioService->GetOffline(&offline);
                    254:     mOffline = offline;
                    255:     
                    256:     if (mOffline) {
                    257:         NSString* newTitle = [[[mWindowController window] title] stringByAppendingString: @" [Working Offline]"];
                    258:         [[mWindowController window] setTitle: newTitle];
                    259:     }
                    260:     else {
                    261:         NSArray* titleItems = [[[mWindowController window] title] componentsSeparatedByString:@" [Working Offline]"];
                    262:         [[mWindowController window] setTitle: [titleItems objectAtIndex: 0]];
1.4       macserv   263:     }
1.1       hyatt     264: }
                    265: 
                    266: - (void)sizeBrowserTo:(NSSize)dimensions
                    267: {
                    268:   NSRect bounds = [self bounds];
                    269:   float dx = dimensions.width - bounds.size.width;
                    270:   float dy = dimensions.height - bounds.size.height;
                    271: 
1.4       macserv   272:   NSRect frame = [[self window] frame];
1.1       hyatt     273:   frame.size.width += dx;
                    274:   frame.size.height += dy;
                    275: 
1.4       macserv   276:   [[self window] setFrame:frame display:YES];
1.1       hyatt     277: }
                    278: 
                    279: - (NSBrowserView*)createBrowserWindow:(unsigned int)mask
                    280: {
1.21    ! hyatt     281:   BrowserWindowController* controller = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
        !           282:   [controller enterModalSession];
        !           283:   return [[controller getMyBrowserView] getBrowserView];
1.1       hyatt     284: }
                    285: 
                    286: - (NSBrowserView*)getBrowserView
                    287: {
                    288:   return browserView;
                    289: }
                    290: 
                    291: @end

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