Annotation of chimera/MyBrowserView.mm, revision 1.33

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

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