Annotation of chimera/MyBrowserView.mm, revision 1.22

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

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