Annotation of chimera/MyBrowserView.mm, revision 1.26

1.23      hyatt       1: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
                      2: /* ***** BEGIN LICENSE BLOCK *****
                      3:  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
                      4:  *
                      5:  * The contents of this file are subject to the Netscape Public License
                      6:  * Version 1.1 (the "License"); you may not use this file except in
                      7:  * compliance with the License. You may obtain a copy of the License at
                      8:  * http://www.mozilla.org/NPL/
                      9:  *
                     10:  * Software distributed under the License is distributed on an "AS IS" basis,
                     11:  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
                     12:  * for the specific language governing rights and limitations under the
                     13:  * License.
                     14:  *
                     15:  * The Original Code is mozilla.org code.
                     16:  *
                     17:  * The Initial Developer of the Original Code is 
                     18:  * Netscape Communications Corporation.
                     19:  * Portions created by the Initial Developer are Copyright (C) 2002
                     20:  * the Initial Developer. All Rights Reserved.
                     21:  *
                     22:  * Contributor(s):
                     23:  *
                     24:  * Alternatively, the contents of this file may be used under the terms of
                     25:  * either the GNU General Public License Version 2 or later (the "GPL"), or 
                     26:  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
                     27:  * in which case the provisions of the GPL or the LGPL are applicable instead
                     28:  * of those above. If you wish to allow use of your version of this file only
                     29:  * under the terms of either the GPL or the LGPL, and not to allow others to
                     30:  * use your version of this file under the terms of the NPL, indicate your
                     31:  * decision by deleting the provisions above and replace them with the notice
                     32:  * and other provisions required by the GPL or the LGPL. If you do not delete
                     33:  * the provisions above, a recipient may use your version of this file under
                     34:  * the terms of any one of the NPL, the GPL or the LGPL.
                     35:  *
                     36:  * ***** END LICENSE BLOCK ***** */
1.2       macserv    37: 
1.1       hyatt      38: #import "MyBrowserView.h"
1.21      hyatt      39: #import "BrowserWindowController.h"
1.1       hyatt      40: 
1.10      hyatt      41: #include "nsCOMPtr.h"
                     42: #include "nsIServiceManager.h"
                     43: #include "nsIIOService.h"
1.22      hyatt      44: #include "ContentClickListener.h"
                     45: #include "nsIDOMWindow.h"
                     46: #include "nsIChromeEventHandler.h"
                     47: #include "nsPIDOMWindow.h"
                     48: #include "nsIDOMEventReceiver.h"
1.10      hyatt      49: 
1.1       hyatt      50: #define DOCUMENT_DONE_STRING @"Document: Done"
                     51: #define LOADING_STRING @"Loading..."
                     52: 
1.10      hyatt      53: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
                     54: 
1.1       hyatt      55: @implementation MyBrowserView
                     56: 
1.7       hyatt      57: -(void)windowClosed
                     58: {
1.26    ! hyatt      59:   // Break the cycle.
        !            60:   [browserView destroyWebBrowser];
        !            61:   [browserView setContainer: nil];
        !            62:   [browserView removeListener: self];
1.7       hyatt      63: }
                     64: 
1.1       hyatt      65: -(void)dealloc
                     66: {
1.26    ! hyatt      67:   printf("My browser view died.\n");
        !            68: 
        !            69:   [[NSNotificationCenter defaultCenter] removeObserver: self];
1.11      hyatt      70:     
1.3       macserv    71:  [defaultStatus release];
1.26    ! hyatt      72:   [loadingStatus release];
        !            73: 
        !            74:   [super dealloc];
1.1       hyatt      75: }
                     76: 
                     77: - (IBAction)load:(id)sender
                     78: {
1.4       macserv    79:   [browserView loadURI:[NSURL URLWithString:[urlbar stringValue]] flags:NSLoadFlagsNone];
1.1       hyatt      80: }
                     81: 
1.16      hyatt      82: -(void)disconnectView
                     83: {
                     84:     urlbar = nil;
                     85:     status = nil;
                     86:     progress = nil;
                     87:     progressSuper = nil;
                     88:     mIsPrimary = NO;
                     89:     [[NSNotificationCenter defaultCenter] removeObserver:self];
1.18      hyatt      90:     
1.19      hyatt      91:     [browserView setActive: NO];
1.16      hyatt      92: }
                     93: 
                     94: - (id)initWithFrame:(NSRect)frameRect
                     95: {
                     96:     [super initWithFrame: frameRect];
                     97:      browserView = [[[NSBrowserView alloc] initWithFrame:[self bounds]] autorelease];
                     98:     [self addSubview:browserView];
                     99:     [browserView setContainer:self];
                    100:     [browserView addListener:self];
                    101:     mIsBusy = NO;
1.22      hyatt     102:     mListenersAttached = NO;
1.16      hyatt     103:     return self;
                    104: }
                    105: 
1.24      hyatt     106: -(void)setTab: (NSTabViewItem*)tab
                    107: {
                    108:   mTab = tab;
                    109: }
                    110: 
                    111: -(void)makePrimaryBrowserView: (id)aUrlbar status: (id)aStatus
1.14      hyatt     112:     progress: (id)aProgress windowController: aWindowController
1.1       hyatt     113: {
1.14      hyatt     114:   urlbar = aUrlbar;
                    115:   status = aStatus;
                    116:   progress = aProgress;
                    117:   progressSuper = [aProgress superview];
                    118:   mWindowController = aWindowController;
                    119:   
1.20      hyatt     120:   if (!mIsBusy)
                    121:       [progress removeFromSuperview];
1.16      hyatt     122:   
1.14      hyatt     123:   defaultStatus = NULL;
                    124:   loadingStatus = DOCUMENT_DONE_STRING;
                    125:   [status setStringValue:loadingStatus];
                    126:   
                    127:   mIsPrimary = YES;
                    128:  
1.10      hyatt     129:   nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
                    130:   if (!ioService)
                    131:     return;
                    132:   PRBool offline = PR_FALSE;
                    133:   ioService->GetOffline(&offline);
                    134:   mOffline = offline;
1.11      hyatt     135:   
1.12      hyatt     136:   if (mWindowController) // Only register if we're the content area.
                    137:     [[NSNotificationCenter defaultCenter] addObserver:self
                    138:         selector:@selector(offlineModeChanged:)
                    139:         name:@"offlineModeChanged"
                    140:         object:nil];
1.17      hyatt     141:         
                    142:   // Update the URL bar.
                    143:   [mWindowController updateLocationFields:[self getCurrentURLSpec]];
1.22      hyatt     144:   
                    145:   if (mWindowController && !mListenersAttached) {
                    146:     mListenersAttached = YES;
                    147:     
                    148:     // We need to hook up our click and context menu listeners.
                    149:     ContentClickListener* clickListener = new ContentClickListener(mWindowController);
                    150:     if (!clickListener)
                    151:       return;
                    152:     
                    153:     nsCOMPtr<nsIDOMWindow> contentWindow = getter_AddRefs([[self getBrowserView] getContentWindow]);
                    154:     nsCOMPtr<nsPIDOMWindow> piWindow(do_QueryInterface(contentWindow));
                    155:     nsCOMPtr<nsIChromeEventHandler> chromeHandler;
                    156:     piWindow->GetChromeEventHandler(getter_AddRefs(chromeHandler));
                    157:     nsCOMPtr<nsIDOMEventReceiver> rec(do_QueryInterface(chromeHandler));
                    158:     rec->AddEventListenerByIID(clickListener, NS_GET_IID(nsIDOMMouseListener));
                    159:   }
1.17      hyatt     160: }
                    161: 
                    162: -(NSString*)getCurrentURLSpec
                    163: {
                    164:   return [browserView getCurrentURLSpec];
1.1       hyatt     165: }
                    166: 
1.14      hyatt     167: - (void)awakeFromNib 
                    168: {
                    169: }
                    170: 
1.1       hyatt     171: - (void)setFrame:(NSRect)frameRect
                    172: {
                    173:   [super setFrame:frameRect];
                    174:   NSRect bounds = [self bounds];
                    175:   [browserView setFrame:bounds];
                    176: }
                    177: 
                    178: -(BOOL)isBusy
                    179: {
                    180:   return mIsBusy;
                    181: }
                    182: 
                    183: - (void)onLoadingStarted 
                    184: {
                    185:   if (defaultStatus) {
                    186:     [defaultStatus release];
                    187:     defaultStatus = NULL;
                    188:   }
                    189: 
                    190:   [progressSuper addSubview:progress];
                    191:   [progress setIndeterminate:YES];
                    192:   [progress startAnimation:self];
                    193: 
                    194:   loadingStatus = LOADING_STRING;
                    195:   [status setStringValue:loadingStatus];
                    196: 
                    197:   mIsBusy = YES;
1.16      hyatt     198:   [mTab setLabel: LOADING_STRING];
                    199:   
1.1       hyatt     200:   if (mWindowController)
                    201:     [mWindowController updateToolbarItems];
                    202: }
                    203: 
                    204: - (void)onLoadingCompleted:(BOOL)succeeded
                    205: {
                    206:   [progress setIndeterminate:YES];
                    207:   [progress stopAnimation:self];
                    208:   [progress removeFromSuperview];
                    209: 
                    210:   loadingStatus = DOCUMENT_DONE_STRING;
                    211:   if (defaultStatus) {
                    212:     [status setStringValue:defaultStatus];
                    213:   }
                    214:   else {
                    215:     [status setStringValue:loadingStatus];
                    216:   }
                    217: 
                    218:   mIsBusy = NO;
                    219:   
                    220:   if (mWindowController)
                    221:     [mWindowController updateToolbarItems];
                    222: }
                    223: 
                    224: - (void)onProgressChange:(int)currentBytes outOf:(int)maxBytes 
                    225: {
                    226:   if (maxBytes > 0) {
                    227:     BOOL isIndeterminate = [progress isIndeterminate];
                    228:     if (isIndeterminate) {
                    229:       [progress setIndeterminate:NO];
                    230:     }
                    231:     double val = ((double)currentBytes / (double)maxBytes) * 100.0;
                    232:     [progress setDoubleValue:val];
                    233:   }
                    234: }
                    235: 
                    236: - (void)onLocationChange:(NSURL*)url 
                    237: {
                    238:   NSString* spec = [url absoluteString];
1.4       macserv   239:   [mWindowController updateLocationFields:spec];
1.13      hyatt     240: }
                    241: 
                    242: - (void)onStatusChange:(NSString*)aStatusString
                    243: {
                    244:   [status setStringValue: aStatusString];
1.1       hyatt     245: }
                    246: 
                    247: - (void)setStatus:(NSString *)statusString ofType:(NSStatusType)type 
                    248: {
                    249:   if (type == NSStatusTypeScriptDefault) {
                    250:     if (defaultStatus) {
                    251:       [defaultStatus release];
                    252:     }
                    253:     defaultStatus = statusString;
                    254:     if (defaultStatus) {
                    255:       [defaultStatus retain];
                    256:     }
                    257:   }
                    258:   else if (!statusString) {
                    259:     if (defaultStatus) {
                    260:       [status setStringValue:defaultStatus];
                    261:     }
                    262:     else {
                    263:       [status setStringValue:loadingStatus];
                    264:     }      
                    265:   }
                    266:   else {
                    267:     [status setStringValue:statusString];
                    268:   }
                    269: }
                    270: 
                    271: - (NSString *)title 
                    272: {
1.4       macserv   273:   return [[mWindowController window] title];
1.1       hyatt     274: }
                    275: 
                    276: - (void)setTitle:(NSString *)title
                    277: {
1.14      hyatt     278:     // We must be the primary content area.
1.24      hyatt     279:     if (mIsPrimary && mWindowController) {
1.14      hyatt     280:         if (mOffline) {
                    281:             NSString* newTitle;
                    282:             if (title && ![title isEqualToString:@""])
                    283:                 newTitle = [title stringByAppendingString: @" [Working Offline]"];
                    284:             else
                    285:                 newTitle = @"Untitled [Working Offline]";
                    286:             [[mWindowController window] setTitle: newTitle];
                    287:         }
                    288:         else {
                    289:             if (title && ![title isEqualToString:@""])
                    290:                 [[mWindowController window] setTitle:title];
                    291:             else
                    292:                 [[mWindowController window] setTitle:@"Untitled"];
                    293:         }
                    294:     }
                    295:     
                    296:     // Always set the tab.
                    297:     if (title && ![title isEqualToString:@""])
                    298:         [mTab setLabel:title];
                    299:     else
1.15      hyatt     300:         [mTab setLabel:@"Untitled"];
1.11      hyatt     301: }
                    302: 
                    303: - (void)offlineModeChanged: (NSNotification*)aNotification
                    304: {
                    305:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
                    306:     if (!ioService)
                    307:         return;
                    308:     PRBool offline = PR_FALSE;
                    309:     ioService->GetOffline(&offline);
                    310:     mOffline = offline;
                    311:     
                    312:     if (mOffline) {
                    313:         NSString* newTitle = [[[mWindowController window] title] stringByAppendingString: @" [Working Offline]"];
                    314:         [[mWindowController window] setTitle: newTitle];
                    315:     }
                    316:     else {
                    317:         NSArray* titleItems = [[[mWindowController window] title] componentsSeparatedByString:@" [Working Offline]"];
                    318:         [[mWindowController window] setTitle: [titleItems objectAtIndex: 0]];
1.4       macserv   319:     }
1.1       hyatt     320: }
                    321: 
                    322: - (void)sizeBrowserTo:(NSSize)dimensions
                    323: {
                    324:   NSRect bounds = [self bounds];
                    325:   float dx = dimensions.width - bounds.size.width;
                    326:   float dy = dimensions.height - bounds.size.height;
                    327: 
1.4       macserv   328:   NSRect frame = [[self window] frame];
1.1       hyatt     329:   frame.size.width += dx;
                    330:   frame.size.height += dy;
                    331: 
1.4       macserv   332:   [[self window] setFrame:frame display:YES];
1.1       hyatt     333: }
                    334: 
                    335: - (NSBrowserView*)createBrowserWindow:(unsigned int)mask
                    336: {
1.21      hyatt     337:   BrowserWindowController* controller = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
1.25      hyatt     338:   [controller disableAutosave]; // The Web page opened this window, so we don't ever use its settings.
1.21      hyatt     339:   [controller enterModalSession];
                    340:   return [[controller getMyBrowserView] getBrowserView];
1.1       hyatt     341: }
                    342: 
                    343: - (NSBrowserView*)getBrowserView
                    344: {
                    345:   return browserView;
                    346: }
                    347: 
                    348: @end

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