Annotation of chimera/MyBrowserView.mm, revision 1.13

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

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