Annotation of chimera/MyBrowserView.mm, revision 1.28

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

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