Annotation of chimera/MyBrowserView.mm, revision 1.34

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: {
1.34    ! macserv   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:     }
1.16      hyatt     114:     return self;
                    115: }
                    116: 
1.24      hyatt     117: -(void)setTab: (NSTabViewItem*)tab
                    118: {
                    119:   mTab = tab;
                    120: }
                    121: 
                    122: -(void)makePrimaryBrowserView: (id)aUrlbar status: (id)aStatus
1.14      hyatt     123:     progress: (id)aProgress windowController: aWindowController
1.1       hyatt     124: {
1.14      hyatt     125:   urlbar = aUrlbar;
                    126:   status = aStatus;
                    127:   progress = aProgress;
                    128:   progressSuper = [aProgress superview];
                    129:   mWindowController = aWindowController;
                    130:   
1.20      hyatt     131:   if (!mIsBusy)
1.33      hyatt     132:     [progress removeFromSuperview];
1.16      hyatt     133:   
1.14      hyatt     134:   defaultStatus = NULL;
                    135:   loadingStatus = DOCUMENT_DONE_STRING;
                    136:   [status setStringValue:loadingStatus];
                    137:   
                    138:   mIsPrimary = YES;
1.29      hyatt     139: 
                    140:   if ([[self window] isKeyWindow])
                    141:     [browserView setActive: YES];
                    142:   
1.10      hyatt     143:   nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
                    144:   if (!ioService)
                    145:     return;
                    146:   PRBool offline = PR_FALSE;
                    147:   ioService->GetOffline(&offline);
                    148:   mOffline = offline;
1.11      hyatt     149:   
1.12      hyatt     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];
1.17      hyatt     155:         
                    156:   // Update the URL bar.
                    157:   [mWindowController updateLocationFields:[self getCurrentURLSpec]];
1.22      hyatt     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:   }
1.17      hyatt     174: }
                    175: 
                    176: -(NSString*)getCurrentURLSpec
                    177: {
                    178:   return [browserView getCurrentURLSpec];
1.1       hyatt     179: }
                    180: 
1.14      hyatt     181: - (void)awakeFromNib 
                    182: {
                    183: }
                    184: 
1.1       hyatt     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;
1.16      hyatt     212:   [mTab setLabel: LOADING_STRING];
                    213:   
1.1       hyatt     214:   if (mWindowController)
                    215:     [mWindowController updateToolbarItems];
1.33      hyatt     216:   else {
                    217:     printf("background load.\n");
                    218:   }
1.1       hyatt     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];
1.4       macserv   256:   [mWindowController updateLocationFields:spec];
1.13      hyatt     257: }
                    258: 
                    259: - (void)onStatusChange:(NSString*)aStatusString
                    260: {
                    261:   [status setStringValue: aStatusString];
1.1       hyatt     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: {
1.4       macserv   290:   return [[mWindowController window] title];
1.1       hyatt     291: }
                    292: 
                    293: - (void)setTitle:(NSString *)title
                    294: {
1.14      hyatt     295:     // We must be the primary content area.
1.24      hyatt     296:     if (mIsPrimary && mWindowController) {
1.14      hyatt     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
1.15      hyatt     317:         [mTab setLabel:@"Untitled"];
1.11      hyatt     318: }
                    319: 
1.27      hyatt     320: // Called when a context menu should be shown.
                    321: - (void)onShowContextMenu:(int)flags domEvent:(nsIDOMEvent*)aEvent domNode:(nsIDOMNode*)aNode
                    322: {
1.30      hyatt     323:   [mWindowController onShowContextMenu: flags domEvent: aEvent domNode: aNode];
                    324: }
1.27      hyatt     325: 
1.30      hyatt     326: -(NSMenu*)getContextMenu
                    327: {
                    328:   return [mWindowController getContextMenu];
1.33      hyatt     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;
1.27      hyatt     341: }
                    342: 
1.11      hyatt     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]];
1.4       macserv   359:     }
1.1       hyatt     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: 
1.4       macserv   368:   NSRect frame = [[self window] frame];
1.1       hyatt     369:   frame.size.width += dx;
                    370:   frame.size.height += dy;
                    371: 
1.4       macserv   372:   [[self window] setFrame:frame display:YES];
1.1       hyatt     373: }
                    374: 
1.28      hyatt     375: #define NS_POPUP_BLOCK @"This Web site is attempting to open an unrequested popup window.  Navigator can \
1.32      hyatt     376: automatically prevent Web sites from opening popup advertisements.  Click OK to prevent all \
                    377: unrequested popups (including this one) from opening."
1.28      hyatt     378: 
1.29      hyatt     379: - (NSBrowserView*)createBrowserWindow:(unsigned int)aMask
1.1       hyatt     380: {
1.28      hyatt     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();
1.32      hyatt     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);
1.28      hyatt     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: 
1.32      hyatt     408:       nsCOMPtr<nsIPrefService> prefService(do_QueryInterface(pref));
                    409:       prefService->SavePrefFile(nsnull);
1.28      hyatt     410:       
                    411:       if (confirm)
                    412:         return nil;
                    413:     }
                    414:   }
                    415:   
1.21      hyatt     416:   BrowserWindowController* controller = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
1.29      hyatt     417:   [controller setChromeMask: aMask];
1.25      hyatt     418:   [controller disableAutosave]; // The Web page opened this window, so we don't ever use its settings.
1.21      hyatt     419:   [controller enterModalSession];
1.31      hyatt     420:   [[[controller getMyBrowserView] getBrowserView] setActive: YES];
1.21      hyatt     421:   return [[controller getMyBrowserView] getBrowserView];
1.1       hyatt     422: }
                    423: 
                    424: - (NSBrowserView*)getBrowserView
                    425: {
                    426:   return browserView;
                    427: }
                    428: 
                    429: @end

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