File:  [mozdev] / chimera / MyBrowserView.mm
Revision 1.25: download - view: text, annotated - select for diffs - revision graph
Sun Mar 10 19:26:16 2002 UTC (17 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Full implementation of window autosaving (including correct ignoring of windows opened as popups by web pages.

    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: 
   50: #define DOCUMENT_DONE_STRING @"Document: Done"
   51: #define LOADING_STRING @"Loading..."
   52: 
   53: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
   54: 
   55: @implementation MyBrowserView
   56: 
   57: -(void)windowClosed
   58: {
   59:     // Break the cycle.
   60:     [browserView setContainer: nil];
   61:     [browserView removeListener: self];
   62:     [browserView destroyWebBrowser];
   63: }
   64: 
   65: -(void)dealloc
   66: {
   67:     printf("My browser view died.\n");
   68:     
   69:     [[NSNotificationCenter defaultCenter] removeObserver: self];
   70:     
   71: 	[defaultStatus release];
   72:     [loadingStatus release];
   73:     
   74: 	[super dealloc];
   75: }
   76: 
   77: - (IBAction)load:(id)sender
   78: {
   79:   [browserView loadURI:[NSURL URLWithString:[urlbar stringValue]] flags:NSLoadFlagsNone];
   80: }
   81: 
   82: -(void)disconnectView
   83: {
   84:     urlbar = nil;
   85:     status = nil;
   86:     progress = nil;
   87:     progressSuper = nil;
   88:     mIsPrimary = NO;
   89:     [[NSNotificationCenter defaultCenter] removeObserver:self];
   90:     
   91:     [browserView setActive: NO];
   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;
  102:     mListenersAttached = NO;
  103:     return self;
  104: }
  105: 
  106: -(void)setTab: (NSTabViewItem*)tab
  107: {
  108:   mTab = tab;
  109: }
  110: 
  111: -(void)makePrimaryBrowserView: (id)aUrlbar status: (id)aStatus
  112:     progress: (id)aProgress windowController: aWindowController
  113: {
  114:   urlbar = aUrlbar;
  115:   status = aStatus;
  116:   progress = aProgress;
  117:   progressSuper = [aProgress superview];
  118:   mWindowController = aWindowController;
  119:   
  120:   if (!mIsBusy)
  121:       [progress removeFromSuperview];
  122:   
  123:   defaultStatus = NULL;
  124:   loadingStatus = DOCUMENT_DONE_STRING;
  125:   [status setStringValue:loadingStatus];
  126:   
  127:   mIsPrimary = YES;
  128:  
  129:   nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  130:   if (!ioService)
  131:     return;
  132:   PRBool offline = PR_FALSE;
  133:   ioService->GetOffline(&offline);
  134:   mOffline = offline;
  135:   
  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];
  141:         
  142:   // Update the URL bar.
  143:   [mWindowController updateLocationFields:[self getCurrentURLSpec]];
  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:   }
  160: }
  161: 
  162: -(NSString*)getCurrentURLSpec
  163: {
  164:   return [browserView getCurrentURLSpec];
  165: }
  166: 
  167: - (void)awakeFromNib 
  168: {
  169: }
  170: 
  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;
  198:   [mTab setLabel: LOADING_STRING];
  199:   
  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];
  239:   [mWindowController updateLocationFields:spec];
  240: }
  241: 
  242: - (void)onStatusChange:(NSString*)aStatusString
  243: {
  244:   [status setStringValue: aStatusString];
  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: {
  273:   return [[mWindowController window] title];
  274: }
  275: 
  276: - (void)setTitle:(NSString *)title
  277: {
  278:     // We must be the primary content area.
  279:     if (mIsPrimary && mWindowController) {
  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
  300:         [mTab setLabel:@"Untitled"];
  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]];
  319:     }
  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: 
  328:   NSRect frame = [[self window] frame];
  329:   frame.size.width += dx;
  330:   frame.size.height += dy;
  331: 
  332:   [[self window] setFrame:frame display:YES];
  333: }
  334: 
  335: - (NSBrowserView*)createBrowserWindow:(unsigned int)mask
  336: {
  337:   BrowserWindowController* controller = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  338:   [controller disableAutosave]; // The Web page opened this window, so we don't ever use its settings.
  339:   [controller enterModalSession];
  340:   return [[controller getMyBrowserView] getBrowserView];
  341: }
  342: 
  343: - (NSBrowserView*)getBrowserView
  344: {
  345:   return browserView;
  346: }
  347: 
  348: @end

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