File:  [mozdev] / chimera / MyBrowserView.mm
Revision 1.23: download - view: text, annotated - select for diffs - revision graph
Thu Mar 7 18:42:42 2002 UTC (17 years, 1 month ago) by hyatt
Branches: MAIN
CVS tags: HEAD
Add the tri-license to all files.

    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)makePrimaryBrowserView: (NSTabViewItem*)tab urlbar: (id)aUrlbar status: (id)aStatus
  107:     progress: (id)aProgress windowController: aWindowController
  108: {
  109:   mTab = tab;
  110:   urlbar = aUrlbar;
  111:   status = aStatus;
  112:   progress = aProgress;
  113:   progressSuper = [aProgress superview];
  114:   mWindowController = aWindowController;
  115:   
  116:   if (!mIsBusy)
  117:       [progress removeFromSuperview];
  118:   
  119:   defaultStatus = NULL;
  120:   loadingStatus = DOCUMENT_DONE_STRING;
  121:   [status setStringValue:loadingStatus];
  122:   
  123:   mIsPrimary = YES;
  124:  
  125:   nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  126:   if (!ioService)
  127:     return;
  128:   PRBool offline = PR_FALSE;
  129:   ioService->GetOffline(&offline);
  130:   mOffline = offline;
  131:   
  132:   if (mWindowController) // Only register if we're the content area.
  133:     [[NSNotificationCenter defaultCenter] addObserver:self
  134:         selector:@selector(offlineModeChanged:)
  135:         name:@"offlineModeChanged"
  136:         object:nil];
  137:         
  138:   // Update the URL bar.
  139:   [mWindowController updateLocationFields:[self getCurrentURLSpec]];
  140:   
  141:   if (mWindowController && !mListenersAttached) {
  142:     mListenersAttached = YES;
  143:     
  144:     // We need to hook up our click and context menu listeners.
  145:     ContentClickListener* clickListener = new ContentClickListener(mWindowController);
  146:     if (!clickListener)
  147:       return;
  148:     
  149:     nsCOMPtr<nsIDOMWindow> contentWindow = getter_AddRefs([[self getBrowserView] getContentWindow]);
  150:     nsCOMPtr<nsPIDOMWindow> piWindow(do_QueryInterface(contentWindow));
  151:     nsCOMPtr<nsIChromeEventHandler> chromeHandler;
  152:     piWindow->GetChromeEventHandler(getter_AddRefs(chromeHandler));
  153:     nsCOMPtr<nsIDOMEventReceiver> rec(do_QueryInterface(chromeHandler));
  154:     rec->AddEventListenerByIID(clickListener, NS_GET_IID(nsIDOMMouseListener));
  155:   }
  156: }
  157: 
  158: -(NSString*)getCurrentURLSpec
  159: {
  160:   return [browserView getCurrentURLSpec];
  161: }
  162: 
  163: - (void)awakeFromNib 
  164: {
  165: }
  166: 
  167: - (void)setFrame:(NSRect)frameRect
  168: {
  169:   [super setFrame:frameRect];
  170:   NSRect bounds = [self bounds];
  171:   [browserView setFrame:bounds];
  172: }
  173: 
  174: -(BOOL)isBusy
  175: {
  176:   return mIsBusy;
  177: }
  178: 
  179: - (void)onLoadingStarted 
  180: {
  181:   if (defaultStatus) {
  182:     [defaultStatus release];
  183:     defaultStatus = NULL;
  184:   }
  185: 
  186:   [progressSuper addSubview:progress];
  187:   [progress setIndeterminate:YES];
  188:   [progress startAnimation:self];
  189: 
  190:   loadingStatus = LOADING_STRING;
  191:   [status setStringValue:loadingStatus];
  192: 
  193:   mIsBusy = YES;
  194:   [mTab setLabel: LOADING_STRING];
  195:   
  196:   if (mWindowController)
  197:     [mWindowController updateToolbarItems];
  198: }
  199: 
  200: - (void)onLoadingCompleted:(BOOL)succeeded
  201: {
  202:   [progress setIndeterminate:YES];
  203:   [progress stopAnimation:self];
  204:   [progress removeFromSuperview];
  205: 
  206:   loadingStatus = DOCUMENT_DONE_STRING;
  207:   if (defaultStatus) {
  208:     [status setStringValue:defaultStatus];
  209:   }
  210:   else {
  211:     [status setStringValue:loadingStatus];
  212:   }
  213: 
  214:   mIsBusy = NO;
  215:   
  216:   if (mWindowController)
  217:     [mWindowController updateToolbarItems];
  218: }
  219: 
  220: - (void)onProgressChange:(int)currentBytes outOf:(int)maxBytes 
  221: {
  222:   if (maxBytes > 0) {
  223:     BOOL isIndeterminate = [progress isIndeterminate];
  224:     if (isIndeterminate) {
  225:       [progress setIndeterminate:NO];
  226:     }
  227:     double val = ((double)currentBytes / (double)maxBytes) * 100.0;
  228:     [progress setDoubleValue:val];
  229:   }
  230: }
  231: 
  232: - (void)onLocationChange:(NSURL*)url 
  233: {
  234:   NSString* spec = [url absoluteString];
  235:   [mWindowController updateLocationFields:spec];
  236: }
  237: 
  238: - (void)onStatusChange:(NSString*)aStatusString
  239: {
  240:   [status setStringValue: aStatusString];
  241: }
  242: 
  243: - (void)setStatus:(NSString *)statusString ofType:(NSStatusType)type 
  244: {
  245:   if (type == NSStatusTypeScriptDefault) {
  246:     if (defaultStatus) {
  247:       [defaultStatus release];
  248:     }
  249:     defaultStatus = statusString;
  250:     if (defaultStatus) {
  251:       [defaultStatus retain];
  252:     }
  253:   }
  254:   else if (!statusString) {
  255:     if (defaultStatus) {
  256:       [status setStringValue:defaultStatus];
  257:     }
  258:     else {
  259:       [status setStringValue:loadingStatus];
  260:     }      
  261:   }
  262:   else {
  263:     [status setStringValue:statusString];
  264:   }
  265: }
  266: 
  267: - (NSString *)title 
  268: {
  269:   return [[mWindowController window] title];
  270: }
  271: 
  272: - (void)setTitle:(NSString *)title
  273: {
  274:     if (!mWindowController)
  275:         return; // Sidebar panels can't affect the window title.
  276:     
  277:     // We must be the primary content area.
  278:     if (mIsPrimary) {
  279:         if (mOffline) {
  280:             NSString* newTitle;
  281:             if (title && ![title isEqualToString:@""])
  282:                 newTitle = [title stringByAppendingString: @" [Working Offline]"];
  283:             else
  284:                 newTitle = @"Untitled [Working Offline]";
  285:             [[mWindowController window] setTitle: newTitle];
  286:         }
  287:         else {
  288:             if (title && ![title isEqualToString:@""])
  289:                 [[mWindowController window] setTitle:title];
  290:             else
  291:                 [[mWindowController window] setTitle:@"Untitled"];
  292:         }
  293:     }
  294:     
  295:     // Always set the tab.
  296:     if (title && ![title isEqualToString:@""])
  297:         [mTab setLabel:title];
  298:     else
  299:         [mTab setLabel:@"Untitled"];
  300: }
  301: 
  302: - (void)offlineModeChanged: (NSNotification*)aNotification
  303: {
  304:     nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
  305:     if (!ioService)
  306:         return;
  307:     PRBool offline = PR_FALSE;
  308:     ioService->GetOffline(&offline);
  309:     mOffline = offline;
  310:     
  311:     if (mOffline) {
  312:         NSString* newTitle = [[[mWindowController window] title] stringByAppendingString: @" [Working Offline]"];
  313:         [[mWindowController window] setTitle: newTitle];
  314:     }
  315:     else {
  316:         NSArray* titleItems = [[[mWindowController window] title] componentsSeparatedByString:@" [Working Offline]"];
  317:         [[mWindowController window] setTitle: [titleItems objectAtIndex: 0]];
  318:     }
  319: }
  320: 
  321: - (void)sizeBrowserTo:(NSSize)dimensions
  322: {
  323:   NSRect bounds = [self bounds];
  324:   float dx = dimensions.width - bounds.size.width;
  325:   float dy = dimensions.height - bounds.size.height;
  326: 
  327:   NSRect frame = [[self window] frame];
  328:   frame.size.width += dx;
  329:   frame.size.height += dy;
  330: 
  331:   [[self window] setFrame:frame display:YES];
  332: }
  333: 
  334: - (NSBrowserView*)createBrowserWindow:(unsigned int)mask
  335: {
  336:   BrowserWindowController* controller = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
  337:   [controller enterModalSession];
  338:   return [[controller getMyBrowserView] getBrowserView];
  339: }
  340: 
  341: - (NSBrowserView*)getBrowserView
  342: {
  343:   return browserView;
  344: }
  345: 
  346: @end

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