File:  [mozdev] / chimera / MyBrowserView.mm
Revision 1.34: download - view: text, annotated - select for diffs - revision graph
Sat Apr 20 00:44:56 2002 UTC (17 years ago) by macserv
Branches: MAIN
CVS tags: HEAD
A bunch of tweaks here... no changes in functionality.  Updated trilicense header on all files that were missing it.  Fixed initialization code syntax errors all over the place.  Put back fade code, and switched fading off for now...

    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 ***** */
   37: 
   38: #import "MyBrowserView.h"
   39: #import "BrowserWindowController.h"
   40: 
   41: #include "nsCOMPtr.h"
   42: #include "nsIServiceManager.h"
   43: #include "nsIIOService.h"
   44: #include "ContentClickListener.h"
   45: #include "nsIDOMWindow.h"
   46: #include "nsIChromeEventHandler.h"
   47: #include "nsPIDOMWindow.h"
   48: #include "nsIDOMEventReceiver.h"
   49: #include "nsIPrefBranch.h"
   50: #include "nsIPrefService.h"
   51: #include "nsCocoaBrowserService.h"
   52: 
   53: #define DOCUMENT_DONE_STRING @"Document: Done"
   54: #define LOADING_STRING @"Loading..."
   55: 
   56: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
   57: 
   58: @implementation MyBrowserView
   59: 
   60: -(void)windowClosed
   61: {
   62:   // Break the cycle.
   63:   [browserView destroyWebBrowser];
   64:   [browserView setContainer: nil];
   65:   [browserView removeListener: self];
   66: }
   67: 
   68: -(void)dealloc
   69: {
   70:   printf("My browser view died.\n");
   71: 
   72:   [[NSNotificationCenter defaultCenter] removeObserver: self];
   73:     
   74: 	[defaultStatus release];
   75:   [loadingStatus release];
   76: 
   77:   [super dealloc];
   78: }
   79: 
   80: - (IBAction)load:(id)sender
   81: {
   82:   [browserView loadURI:[NSURL URLWithString:[urlbar stringValue]] flags:NSLoadFlagsNone];
   83: }
   84: 
   85: -(void)disconnectView
   86: {
   87:     urlbar = nil;
   88:     status = nil;
   89:     progress = nil;
   90:     progressSuper = nil;
   91:     mIsPrimary = NO;
   92:     [[NSNotificationCenter defaultCenter] removeObserver:self];
   93:     
   94:     [browserView setActive: NO];
   95: }
   96: 
   97: -(id)initWithTab:(id)aTab andWindow: (NSWindow*)aWindow
   98: {
   99:   mTab = aTab;
  100:   mWindow = aWindow;
  101:   return [self initWithFrame: NSZeroRect];
  102: }
  103: 
  104: - (id)initWithFrame:(NSRect)frameRect
  105: {
  106:     if ( (self = [super initWithFrame: frameRect]) ) {
  107:     	browserView = [[[NSBrowserView alloc] initWithFrame:[self bounds] andWindow: [self getNativeWindow]] autorelease];
  108:         [self addSubview:browserView];
  109:         [browserView setContainer:self];
  110:         [browserView addListener:self];
  111:         mIsBusy = NO;
  112:         mListenersAttached = NO;
  113:     }
  114:     return self;
  115: }
  116: 
  117: -(void)setTab: (NSTabViewItem*)tab
  118: {
  119:   mTab = tab;
  120: }
  121: 
  122: -(void)makePrimaryBrowserView: (id)aUrlbar status: (id)aStatus
  123:     progress: (id)aProgress windowController: aWindowController
  124: {
  125:   urlbar = aUrlbar;
  126:   status = aStatus;
  127:   progress = aProgress;
  128:   progressSuper = [aProgress superview];
  129:   mWindowController = aWindowController;
  130:   
  131:   if (!mIsBusy)
  132:     [progress removeFromSuperview];
  133:   
  134:   defaultStatus = NULL;
  135:   loadingStatus = DOCUMENT_DONE_STRING;
  136:   [status setStringValue:loadingStatus];
  137:   
  138:   mIsPrimary = YES;
  139: 
  140:   if ([[self window] isKeyWindow])
  141:     [browserView setActive: YES];
  142:   
  143:   nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  144:   if (!ioService)
  145:     return;
  146:   PRBool offline = PR_FALSE;
  147:   ioService->GetOffline(&offline);
  148:   mOffline = offline;
  149:   
  150:   if (mWindowController) // Only register if we're the content area.
  151:     [[NSNotificationCenter defaultCenter] addObserver:self
  152:         selector:@selector(offlineModeChanged:)
  153:         name:@"offlineModeChanged"
  154:         object:nil];
  155:         
  156:   // Update the URL bar.
  157:   [mWindowController updateLocationFields:[self getCurrentURLSpec]];
  158:   
  159:   if (mWindowController && !mListenersAttached) {
  160:     mListenersAttached = YES;
  161:     
  162:     // We need to hook up our click and context menu listeners.
  163:     ContentClickListener* clickListener = new ContentClickListener(mWindowController);
  164:     if (!clickListener)
  165:       return;
  166:     
  167:     nsCOMPtr<nsIDOMWindow> contentWindow = getter_AddRefs([[self getBrowserView] getContentWindow]);
  168:     nsCOMPtr<nsPIDOMWindow> piWindow(do_QueryInterface(contentWindow));
  169:     nsCOMPtr<nsIChromeEventHandler> chromeHandler;
  170:     piWindow->GetChromeEventHandler(getter_AddRefs(chromeHandler));
  171:     nsCOMPtr<nsIDOMEventReceiver> rec(do_QueryInterface(chromeHandler));
  172:     rec->AddEventListenerByIID(clickListener, NS_GET_IID(nsIDOMMouseListener));
  173:   }
  174: }
  175: 
  176: -(NSString*)getCurrentURLSpec
  177: {
  178:   return [browserView getCurrentURLSpec];
  179: }
  180: 
  181: - (void)awakeFromNib 
  182: {
  183: }
  184: 
  185: - (void)setFrame:(NSRect)frameRect
  186: {
  187:   [super setFrame:frameRect];
  188:   NSRect bounds = [self bounds];
  189:   [browserView setFrame:bounds];
  190: }
  191: 
  192: -(BOOL)isBusy
  193: {
  194:   return mIsBusy;
  195: }
  196: 
  197: - (void)onLoadingStarted 
  198: {
  199:   if (defaultStatus) {
  200:     [defaultStatus release];
  201:     defaultStatus = NULL;
  202:   }
  203: 
  204:   [progressSuper addSubview:progress];
  205:   [progress setIndeterminate:YES];
  206:   [progress startAnimation:self];
  207: 
  208:   loadingStatus = LOADING_STRING;
  209:   [status setStringValue:loadingStatus];
  210: 
  211:   mIsBusy = YES;
  212:   [mTab setLabel: LOADING_STRING];
  213:   
  214:   if (mWindowController)
  215:     [mWindowController updateToolbarItems];
  216:   else {
  217:     printf("background load.\n");
  218:   }
  219: }
  220: 
  221: - (void)onLoadingCompleted:(BOOL)succeeded
  222: {
  223:   [progress setIndeterminate:YES];
  224:   [progress stopAnimation:self];
  225:   [progress removeFromSuperview];
  226: 
  227:   loadingStatus = DOCUMENT_DONE_STRING;
  228:   if (defaultStatus) {
  229:     [status setStringValue:defaultStatus];
  230:   }
  231:   else {
  232:     [status setStringValue:loadingStatus];
  233:   }
  234: 
  235:   mIsBusy = NO;
  236:   
  237:   if (mWindowController)
  238:     [mWindowController updateToolbarItems];
  239: }
  240: 
  241: - (void)onProgressChange:(int)currentBytes outOf:(int)maxBytes 
  242: {
  243:   if (maxBytes > 0) {
  244:     BOOL isIndeterminate = [progress isIndeterminate];
  245:     if (isIndeterminate) {
  246:       [progress setIndeterminate:NO];
  247:     }
  248:     double val = ((double)currentBytes / (double)maxBytes) * 100.0;
  249:     [progress setDoubleValue:val];
  250:   }
  251: }
  252: 
  253: - (void)onLocationChange:(NSURL*)url 
  254: {
  255:   NSString* spec = [url absoluteString];
  256:   [mWindowController updateLocationFields:spec];
  257: }
  258: 
  259: - (void)onStatusChange:(NSString*)aStatusString
  260: {
  261:   [status setStringValue: aStatusString];
  262: }
  263: 
  264: - (void)setStatus:(NSString *)statusString ofType:(NSStatusType)type 
  265: {
  266:   if (type == NSStatusTypeScriptDefault) {
  267:     if (defaultStatus) {
  268:       [defaultStatus release];
  269:     }
  270:     defaultStatus = statusString;
  271:     if (defaultStatus) {
  272:       [defaultStatus retain];
  273:     }
  274:   }
  275:   else if (!statusString) {
  276:     if (defaultStatus) {
  277:       [status setStringValue:defaultStatus];
  278:     }
  279:     else {
  280:       [status setStringValue:loadingStatus];
  281:     }      
  282:   }
  283:   else {
  284:     [status setStringValue:statusString];
  285:   }
  286: }
  287: 
  288: - (NSString *)title 
  289: {
  290:   return [[mWindowController window] title];
  291: }
  292: 
  293: - (void)setTitle:(NSString *)title
  294: {
  295:     // We must be the primary content area.
  296:     if (mIsPrimary && mWindowController) {
  297:         if (mOffline) {
  298:             NSString* newTitle;
  299:             if (title && ![title isEqualToString:@""])
  300:                 newTitle = [title stringByAppendingString: @" [Working Offline]"];
  301:             else
  302:                 newTitle = @"Untitled [Working Offline]";
  303:             [[mWindowController window] setTitle: newTitle];
  304:         }
  305:         else {
  306:             if (title && ![title isEqualToString:@""])
  307:                 [[mWindowController window] setTitle:title];
  308:             else
  309:                 [[mWindowController window] setTitle:@"Untitled"];
  310:         }
  311:     }
  312:     
  313:     // Always set the tab.
  314:     if (title && ![title isEqualToString:@""])
  315:         [mTab setLabel:title];
  316:     else
  317:         [mTab setLabel:@"Untitled"];
  318: }
  319: 
  320: // Called when a context menu should be shown.
  321: - (void)onShowContextMenu:(int)flags domEvent:(nsIDOMEvent*)aEvent domNode:(nsIDOMNode*)aNode
  322: {
  323:   [mWindowController onShowContextMenu: flags domEvent: aEvent domNode: aNode];
  324: }
  325: 
  326: -(NSMenu*)getContextMenu
  327: {
  328:   return [mWindowController getContextMenu];
  329: }
  330: 
  331: -(NSWindow*)getNativeWindow
  332: {
  333:   NSWindow* window = [self window];
  334:   if (window)
  335:     return window;
  336: 
  337:   if (mWindow)
  338:     return mWindow;
  339:   
  340:   return nil;
  341: }
  342: 
  343: - (void)offlineModeChanged: (NSNotification*)aNotification
  344: {
  345:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  346:     if (!ioService)
  347:         return;
  348:     PRBool offline = PR_FALSE;
  349:     ioService->GetOffline(&offline);
  350:     mOffline = offline;
  351:     
  352:     if (mOffline) {
  353:         NSString* newTitle = [[[mWindowController window] title] stringByAppendingString: @" [Working Offline]"];
  354:         [[mWindowController window] setTitle: newTitle];
  355:     }
  356:     else {
  357:         NSArray* titleItems = [[[mWindowController window] title] componentsSeparatedByString:@" [Working Offline]"];
  358:         [[mWindowController window] setTitle: [titleItems objectAtIndex: 0]];
  359:     }
  360: }
  361: 
  362: - (void)sizeBrowserTo:(NSSize)dimensions
  363: {
  364:   NSRect bounds = [self bounds];
  365:   float dx = dimensions.width - bounds.size.width;
  366:   float dy = dimensions.height - bounds.size.height;
  367: 
  368:   NSRect frame = [[self window] frame];
  369:   frame.size.width += dx;
  370:   frame.size.height += dy;
  371: 
  372:   [[self window] setFrame:frame display:YES];
  373: }
  374: 
  375: #define NS_POPUP_BLOCK @"This Web site is attempting to open an unrequested popup window.  Navigator can \
  376: automatically prevent Web sites from opening popup advertisements.  Click OK to prevent all \
  377: unrequested popups (including this one) from opening."
  378: 
  379: - (NSBrowserView*)createBrowserWindow:(unsigned int)aMask
  380: {
  381:   nsCOMPtr<nsIPrefBranch> pref(do_GetService("@mozilla.org/preferences-service;1"));
  382:   if (!pref)
  383:     return NS_OK; // Something bad happened if we can't get prefs.
  384: 
  385:   PRBool showBlocker;
  386:   pref->GetBoolPref("browser.popups.showPopupBlocker", &showBlocker);
  387: 
  388:   if (showBlocker) {
  389:     nsCOMPtr<nsIDOMWindow> domWindow = getter_AddRefs([browserView getContentWindow]);
  390:     nsCOMPtr<nsPIDOMWindow> piWindow(do_QueryInterface(domWindow));
  391:     PRBool isUnrequested;
  392:     piWindow->IsLoadingOrRunningTimeout(&isUnrequested);
  393:     if (isUnrequested) {
  394:       // A popup is being opened while the page is currently loading.  Offer to block the
  395:       // popup.
  396:       nsAlertController* controller = nsCocoaBrowserService::GetAlertController();
  397:       BOOL confirm = [controller confirm: [self window] title: @"Unrequested Popup Detected"
  398:                                 text: NS_POPUP_BLOCK];
  399: 
  400:       // This is a one-time dialog.
  401:       pref->SetBoolPref("browser.popups.showPopupBlocker", PR_FALSE);
  402:       
  403:       if (confirm) {
  404:         pref->SetBoolPref("dom.disable_open_during_load", PR_TRUE);
  405:         pref->SetIntPref("dom.disable_open_click_delay", 1000);
  406:       }
  407: 
  408:       nsCOMPtr<nsIPrefService> prefService(do_QueryInterface(pref));
  409:       prefService->SavePrefFile(nsnull);
  410:       
  411:       if (confirm)
  412:         return nil;
  413:     }
  414:   }
  415:   
  416:   BrowserWindowController* controller = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  417:   [controller setChromeMask: aMask];
  418:   [controller disableAutosave]; // The Web page opened this window, so we don't ever use its settings.
  419:   [controller enterModalSession];
  420:   [[[controller getMyBrowserView] getBrowserView] setActive: YES];
  421:   return [[controller getMyBrowserView] getBrowserView];
  422: }
  423: 
  424: - (NSBrowserView*)getBrowserView
  425: {
  426:   return browserView;
  427: }
  428: 
  429: @end

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