File:  [mozdev] / chimera / NSBrowserView.mm
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Thu Jan 31 20:18:48 2002 UTC (17 years, 9 months ago) by hyatt
Branches: MAIN
CVS tags: HEAD
more project changes. Removed completely the dependency on another project.  We now have our own Chimera control and can tweak it as we see fit.

    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) 1998
   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 "NSBrowserView.h"
   39: #import "nsCocoaBrowserService.h"
   40: 
   41: // Embedding includes
   42: #include "nsCWebBrowser.h"
   43: #include "nsIInterfaceRequestor.h"
   44: #include "nsIWebBrowserChrome.h"
   45: #include "nsIEmbeddingSiteWindow.h"
   46: #include "nsIWebProgressListener.h"
   47: #include "nsIWebBrowser.h"
   48: #include "nsIWebNavigation.h"
   49: #include "nsIURI.h"
   50: #include "nsIDOMWindow.h"
   51: #include "nsWeakReference.h"
   52: 
   53: // XPCOM and String includes
   54: #include "nsCRT.h"
   55: #include "nsXPIDLString.h"
   56: #include "nsCOMPtr.h"
   57: 
   58: 
   59: class nsCocoaBrowserListener : public nsSupportsWeakReference,
   60:                                public nsIInterfaceRequestor,
   61: 			       public nsIWebBrowserChrome,
   62: 			       public nsIEmbeddingSiteWindow,
   63:                                public nsIWebProgressListener
   64: {
   65: public:
   66:   nsCocoaBrowserListener(NSBrowserView* aView);
   67:   virtual ~nsCocoaBrowserListener();
   68: 
   69:   NS_DECL_ISUPPORTS
   70:   NS_DECL_NSIINTERFACEREQUESTOR
   71:   NS_DECL_NSIWEBBROWSERCHROME
   72:   NS_DECL_NSIEMBEDDINGSITEWINDOW
   73:   NS_DECL_NSIWEBPROGRESSLISTENER
   74: 
   75:   void AddListener(id <NSBrowserListener> aListener);
   76:   void RemoveListener(id <NSBrowserListener> aListener);
   77:   void SetContainer(id <NSBrowserContainer> aContainer);
   78: 
   79: private:
   80:   NSBrowserView* mView;     // WEAK - it owns us
   81:   NSMutableArray* mListeners;
   82:   id <NSBrowserContainer> mContainer;
   83:   PRBool mIsModal;
   84:   PRUint32 mChromeFlags;
   85: };
   86: 
   87: nsCocoaBrowserListener::nsCocoaBrowserListener(NSBrowserView* aView)
   88:   : mView(aView), mContainer(nsnull), mIsModal(PR_FALSE), mChromeFlags(0)
   89: {
   90:   NS_INIT_ISUPPORTS();
   91:   mListeners = [[NSMutableArray alloc] init];
   92: }
   93: 
   94: nsCocoaBrowserListener::~nsCocoaBrowserListener()
   95: {
   96:   [mListeners release];
   97:   mView = nsnull;
   98:   if (mContainer) {
   99:     [mContainer release];
  100:   }
  101: }
  102: 
  103: NS_IMPL_ISUPPORTS5(nsCocoaBrowserListener,
  104: 		   nsIInterfaceRequestor,
  105: 		   nsIWebBrowserChrome,
  106: 		   nsIEmbeddingSiteWindow,
  107: 		   nsIWebProgressListener,
  108:      		   nsISupportsWeakReference)
  109: 
  110: // Implementation of nsIInterfaceRequestor
  111: NS_IMETHODIMP 
  112: nsCocoaBrowserListener::GetInterface(const nsIID &aIID, void** aInstancePtr)
  113: {
  114:   if (aIID.Equals(NS_GET_IID(nsIDOMWindow))) {
  115:     nsCOMPtr<nsIWebBrowser> browser = dont_AddRef([mView getWebBrowser]);
  116:     if (browser)
  117:       return browser->GetContentDOMWindow((nsIDOMWindow **) aInstancePtr);
  118:   }
  119:   
  120:   return QueryInterface(aIID, aInstancePtr);
  121: }
  122: 
  123: // Implementation of nsIWebBrowserChrome
  124: /* void setStatus (in unsigned long statusType, in wstring status); */
  125: NS_IMETHODIMP 
  126: nsCocoaBrowserListener::SetStatus(PRUint32 statusType, const PRUnichar *status)
  127: {
  128:   if (!mContainer) {
  129:     return NS_ERROR_FAILURE;
  130:   }
  131: 
  132:   NSString* str = nsnull;
  133:   if (status && (*status != PRUnichar(0))) {
  134:     str = [NSString stringWithCharacters:status length:nsCRT::strlen(status)];
  135:   }
  136: 
  137:   [mContainer setStatus:str ofType:(NSStatusType)statusType];
  138: 
  139:   return NS_OK;
  140: }
  141: 
  142: /* attribute nsIWebBrowser webBrowser; */
  143: NS_IMETHODIMP 
  144: nsCocoaBrowserListener::GetWebBrowser(nsIWebBrowser * *aWebBrowser)
  145: {
  146:   NS_ENSURE_ARG_POINTER(aWebBrowser);
  147:   if (!mView) {
  148:     return NS_ERROR_FAILURE;
  149:   }
  150:   *aWebBrowser = [mView getWebBrowser];
  151: 
  152:   return NS_OK;
  153: }
  154: NS_IMETHODIMP 
  155: nsCocoaBrowserListener::SetWebBrowser(nsIWebBrowser * aWebBrowser)
  156: {
  157:   if (!mView) {
  158:     return NS_ERROR_FAILURE;
  159:   }
  160: 
  161:   [mView setWebBrowser:aWebBrowser];
  162: 
  163:   return NS_OK;
  164: }
  165: 
  166: /* attribute unsigned long chromeFlags; */
  167: NS_IMETHODIMP 
  168: nsCocoaBrowserListener::GetChromeFlags(PRUint32 *aChromeFlags)
  169: {
  170:   NS_ENSURE_ARG_POINTER(aChromeFlags);
  171:   *aChromeFlags = mChromeFlags;
  172:   return NS_OK;
  173: }
  174: NS_IMETHODIMP 
  175: nsCocoaBrowserListener::SetChromeFlags(PRUint32 aChromeFlags)
  176: {
  177:   // XXX Do nothing with them for now
  178:   mChromeFlags = aChromeFlags;
  179:   return NS_OK;
  180: }
  181: 
  182: /* void destroyBrowserWindow (); */
  183: NS_IMETHODIMP 
  184: nsCocoaBrowserListener::DestroyBrowserWindow()
  185: {
  186:   // XXX Could send this up to the container, but for now,
  187:   // we just destroy the enclosing window.
  188:   NSWindow* window = [mView window];
  189: 
  190:   if (window) {
  191:     [window close];
  192:   }
  193: 
  194:   return NS_OK;
  195: }
  196: 
  197: /* void sizeBrowserTo (in long aCX, in long aCY); */
  198: NS_IMETHODIMP 
  199: nsCocoaBrowserListener::SizeBrowserTo(PRInt32 aCX, PRInt32 aCY)
  200: {
  201:   if (mContainer) {
  202:     NSSize size;
  203:     
  204:     size.width = (float)aCX;
  205:     size.height = (float)aCY;
  206: 
  207:     [mContainer sizeBrowserTo:size];
  208:   }
  209:   
  210:   return NS_OK;
  211: }
  212: 
  213: /* void showAsModal (); */
  214: NS_IMETHODIMP 
  215: nsCocoaBrowserListener::ShowAsModal()
  216: {
  217:   if (!mView) {
  218:     return NS_ERROR_FAILURE;
  219:   }
  220: 
  221:   NSWindow* window = [mView window];
  222: 
  223:   if (!window) {
  224:     return NS_ERROR_FAILURE;
  225:   }
  226: 
  227:   mIsModal = PR_TRUE;
  228:   int result = [NSApp runModalForWindow:window];
  229:   mIsModal = PR_FALSE;
  230: 
  231:   return (nsresult)result;
  232: }
  233: 
  234: /* boolean isWindowModal (); */
  235: NS_IMETHODIMP 
  236: nsCocoaBrowserListener::IsWindowModal(PRBool *_retval)
  237: {
  238:   NS_ENSURE_ARG_POINTER(_retval);
  239: 
  240:   *_retval = mIsModal;
  241: 
  242:   return NS_OK;
  243: }
  244: 
  245: /* void exitModalEventLoop (in nsresult aStatus); */
  246: NS_IMETHODIMP 
  247: nsCocoaBrowserListener::ExitModalEventLoop(nsresult aStatus)
  248: {
  249:   [NSApp stopModalWithCode:(int)aStatus];
  250: 
  251:   return NS_OK;
  252: }
  253: 
  254: // Implementation of nsIEmbeddingSiteWindow
  255: /* void setDimensions (in unsigned long flags, in long x, in long y, in long cx, in long cy); */
  256: NS_IMETHODIMP 
  257: nsCocoaBrowserListener::SetDimensions(PRUint32 flags, 
  258: 				      PRInt32 x, 
  259: 				      PRInt32 y, 
  260: 				      PRInt32 cx, 
  261: 				      PRInt32 cy)
  262: {
  263:   if (!mView) {
  264:     return NS_ERROR_FAILURE;
  265:   }
  266: 
  267:   NSWindow* window = [mView window];
  268:   if (!window) {
  269:     return NS_ERROR_FAILURE;
  270:   }
  271: 
  272:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION) {
  273:     NSPoint origin;
  274:     origin.x = (float)x;
  275:     origin.y = (float)y;
  276:     [window setFrameOrigin:origin];
  277:   }
  278: 
  279:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER) {
  280:     NSRect frame = [window frame];
  281:     frame.size.width = (float)cx;
  282:     frame.size.height = (float)cy;
  283:     [window setFrame:frame display:YES];
  284:   }
  285:   else if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER) {
  286:     NSSize size;
  287:     size.width = (float)cx;
  288:     size.height = (float)cy;
  289:     [window setContentSize:size];
  290:   }
  291: 
  292:   return NS_OK;
  293: }
  294: 
  295: /* void getDimensions (in unsigned long flags, out long x, out long y, out long cx, out long cy); */
  296: NS_IMETHODIMP 
  297: nsCocoaBrowserListener::GetDimensions(PRUint32 flags, 
  298: 				      PRInt32 *x, 
  299: 				      PRInt32 *y, 
  300: 				      PRInt32 *cx, 
  301: 				      PRInt32 *cy)
  302: {
  303:   NS_ENSURE_ARG_POINTER(x);
  304:   NS_ENSURE_ARG_POINTER(y);
  305:   NS_ENSURE_ARG_POINTER(cx);
  306:   NS_ENSURE_ARG_POINTER(cy);
  307: 
  308:   if (!mView) {
  309:     return NS_ERROR_FAILURE;
  310:   }
  311: 
  312:   NSWindow* window = [mView window];
  313:   if (!window) {
  314:     return NS_ERROR_FAILURE;
  315:   }
  316: 
  317:   NSRect frame = [window frame];
  318:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION) {
  319:     *x = (PRInt32)frame.origin.x;
  320:     *y = (PRInt32)frame.origin.y;
  321:   }
  322:   if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER) {
  323:     *cx = (PRInt32)frame.size.width;
  324:     *cy = (PRInt32)frame.size.height;
  325:   }
  326:   else if (flags & nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER) {
  327:     NSView* contentView = [window contentView];
  328:     NSRect contentFrame = [contentView frame];
  329:     *cx = (PRInt32)contentFrame.size.width;
  330:     *cy = (PRInt32)contentFrame.size.height;    
  331:   }
  332: 
  333:   return NS_OK;
  334: }
  335: 
  336: /* void setFocus (); */
  337: NS_IMETHODIMP 
  338: nsCocoaBrowserListener::SetFocus()
  339: {
  340:   if (!mView) {
  341:     return NS_ERROR_FAILURE;
  342:   }
  343: 
  344:   NSWindow* window = [mView window];
  345:   if (!window) {
  346:     return NS_ERROR_FAILURE;
  347:   }
  348: 
  349:   [window makeKeyAndOrderFront:window];
  350: 
  351:   return NS_OK;
  352: }
  353: 
  354: /* attribute boolean visibility; */
  355: NS_IMETHODIMP 
  356: nsCocoaBrowserListener::GetVisibility(PRBool *aVisibility)
  357: {
  358:   NS_ENSURE_ARG_POINTER(aVisibility);
  359: 
  360:   if (!mView) {
  361:     return NS_ERROR_FAILURE;
  362:   }
  363: 
  364:   NSWindow* window = [mView window];
  365:   if (!window) {
  366:     return NS_ERROR_FAILURE;
  367:   }
  368: 
  369:   *aVisibility = [window isMiniaturized];
  370: 
  371:   return NS_OK;
  372: }
  373: NS_IMETHODIMP 
  374: nsCocoaBrowserListener::SetVisibility(PRBool aVisibility)
  375: {
  376:   if (!mView) {
  377:     return NS_ERROR_FAILURE;
  378:   }
  379: 
  380:   NSWindow* window = [mView window];
  381:   if (!window) {
  382:     return NS_ERROR_FAILURE;
  383:   }
  384: 
  385:   if (aVisibility) {
  386:     [window deminiaturize:window];
  387:   }
  388:   else {
  389:     [window miniaturize:window];
  390:   }
  391: 
  392:   return NS_OK;
  393: }
  394: 
  395: /* attribute wstring title; */
  396: NS_IMETHODIMP 
  397: nsCocoaBrowserListener::GetTitle(PRUnichar * *aTitle)
  398: {
  399:   NS_ENSURE_ARG_POINTER(aTitle);
  400: 
  401:   if (!mContainer) {
  402:     return NS_ERROR_FAILURE;
  403:   }
  404: 
  405:   NSString* title = [mContainer title];
  406:   unsigned int length = [title length];
  407:   if (length) {
  408:     *aTitle = (PRUnichar*)nsMemory::Alloc((length+1)*sizeof(PRUnichar));
  409:     if (!*aTitle) {
  410:       return NS_ERROR_OUT_OF_MEMORY;
  411:     }
  412:     [title getCharacters:*aTitle];
  413:   }
  414:   else {
  415:     *aTitle = nsnull;
  416:   }
  417:   
  418:   return NS_OK;
  419: }
  420: NS_IMETHODIMP 
  421: nsCocoaBrowserListener::SetTitle(const PRUnichar * aTitle)
  422: {
  423:   NS_ENSURE_ARG(aTitle);
  424: 
  425:   if (!mContainer) {
  426:     return NS_ERROR_FAILURE;
  427:   }
  428: 
  429:   NSString* str = [NSString stringWithCharacters:aTitle length:nsCRT::strlen(aTitle)];
  430:   [mContainer setTitle:str];
  431: 
  432:   return NS_OK;
  433: }
  434: 
  435: /* [noscript] readonly attribute voidPtr siteWindow; */
  436: NS_IMETHODIMP 
  437: nsCocoaBrowserListener::GetSiteWindow(void * *aSiteWindow)
  438: {
  439:   NS_ENSURE_ARG_POINTER(aSiteWindow);
  440: 
  441:   if (!mView) {
  442:     return NS_ERROR_FAILURE;
  443:   }
  444: 
  445:   NSWindow* window = [mView window];
  446:   if (!window) {
  447:     return NS_ERROR_FAILURE;
  448:   }
  449: 
  450:   *aSiteWindow = (void*)window;
  451: 
  452:   return NS_OK;
  453: }
  454: 
  455: // Implementation of nsIWebProgressListener
  456: /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in unsigned long aStatus); */
  457: NS_IMETHODIMP 
  458: nsCocoaBrowserListener::OnStateChange(nsIWebProgress *aWebProgress, 
  459: 				      nsIRequest *aRequest, 
  460: 				      PRInt32 aStateFlags, 
  461: 				      PRUint32 aStatus)
  462: {
  463:   NSEnumerator* enumerator = [mListeners objectEnumerator];
  464:   id obj;
  465:   
  466:   if (aStateFlags & nsIWebProgressListener::STATE_IS_NETWORK) {
  467:     if (aStateFlags & nsIWebProgressListener::STATE_START) {
  468:       while ((obj = [enumerator nextObject])) {
  469: 	[obj onLoadingStarted];
  470:       }
  471:     }
  472:     else if (aStateFlags & nsIWebProgressListener::STATE_STOP) {
  473:       while ((obj = [enumerator nextObject])) {
  474: 	[obj onLoadingCompleted:(NS_SUCCEEDED(aStatus))];
  475:       }
  476:     }
  477:   }
  478: 
  479: 
  480:   return NS_OK;
  481: }
  482: 
  483: /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
  484: NS_IMETHODIMP 
  485: nsCocoaBrowserListener::OnProgressChange(nsIWebProgress *aWebProgress, 
  486: 					 nsIRequest *aRequest, 
  487: 					 PRInt32 aCurSelfProgress, 
  488: 					 PRInt32 aMaxSelfProgress, 
  489: 					 PRInt32 aCurTotalProgress, 
  490: 					 PRInt32 aMaxTotalProgress)
  491: {
  492:   NSEnumerator* enumerator = [mListeners objectEnumerator];
  493:   id obj;
  494:  
  495:   while ((obj = [enumerator nextObject])) {
  496:     [obj onProgressChange:aCurTotalProgress outOf:aMaxTotalProgress];
  497:   }
  498:   
  499:   return NS_OK;
  500: }
  501: 
  502: /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
  503: NS_IMETHODIMP 
  504: nsCocoaBrowserListener::OnLocationChange(nsIWebProgress *aWebProgress, 
  505: 					 nsIRequest *aRequest, 
  506: 					 nsIURI *location)
  507: {
  508:   nsXPIDLCString spec;
  509:   
  510:   location->GetSpec(getter_Copies(spec));
  511:   if (!spec) {
  512:     return NS_ERROR_FAILURE;
  513:   }
  514: 
  515:   const char* cstr = spec.get();
  516:   NSString* str = [NSString stringWithCString:cstr];
  517:   NSURL* url = [NSURL URLWithString:str];
  518: 
  519:   NSEnumerator* enumerator = [mListeners objectEnumerator];
  520:   id obj;
  521:  
  522:   while ((obj = [enumerator nextObject])) {
  523:     [obj onLocationChange:url];
  524:   }
  525: 
  526:   return NS_OK;
  527: }
  528: 
  529: /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
  530: NS_IMETHODIMP 
  531: nsCocoaBrowserListener::OnStatusChange(nsIWebProgress *aWebProgress, 
  532: 				       nsIRequest *aRequest, 
  533: 				       nsresult aStatus, 
  534: 				       const PRUnichar *aMessage)
  535: {
  536:   return NS_OK;
  537: }
  538: 
  539: /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long state); */
  540: NS_IMETHODIMP 
  541: nsCocoaBrowserListener::OnSecurityChange(nsIWebProgress *aWebProgress, 
  542: 					 nsIRequest *aRequest, 
  543: 					 PRInt32 state)
  544: {
  545:   return NS_OK;
  546: }
  547: 
  548: void 
  549: nsCocoaBrowserListener::AddListener(id <NSBrowserListener> aListener)
  550: {
  551:   [mListeners addObject:aListener];
  552: }
  553: 
  554: void 
  555: nsCocoaBrowserListener::RemoveListener(id <NSBrowserListener> aListener)
  556: {
  557:   [mListeners removeObject:aListener];
  558: }
  559: 
  560: void 
  561: nsCocoaBrowserListener::SetContainer(id <NSBrowserContainer> aContainer)
  562: {
  563:   [mContainer autorelease];
  564: 
  565:   mContainer = aContainer;
  566: 
  567:   [mContainer retain];
  568: }
  569: 
  570: 
  571: @implementation NSBrowserView
  572: 
  573: - (id)initWithFrame:(NSRect)frame
  574: {
  575:   [super initWithFrame:frame];
  576: 
  577:   nsresult rv = nsCocoaBrowserService::InitEmbedding();
  578:   if (NS_FAILED(rv)) {
  579:     // XXX need to throw
  580:   }
  581:   
  582:   _listener = new nsCocoaBrowserListener(self);
  583:   NS_ADDREF(_listener);
  584: 
  585:   // Create the web browser instance
  586:   nsCOMPtr<nsIWebBrowser> browser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID, &rv);
  587:   if (NS_FAILED(rv)) {
  588:     // XXX need to throw
  589:   }
  590: 
  591:   _webBrowser = browser;
  592:   NS_ADDREF(_webBrowser);
  593: 
  594:   // Set the container nsIWebBrowserChrome
  595:   _webBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome *, 
  596: 						 _listener));
  597:   
  598:   // Register as a listener for web progress
  599:   nsCOMPtr<nsIWeakReference> weak = do_GetWeakReference(NS_STATIC_CAST(nsIWebProgressListener*, _listener));
  600:   _webBrowser->AddWebBrowserListener(weak, NS_GET_IID(nsIWebProgressListener));
  601: 
  602:   // Hook up the widget hierarchy with us as the parent
  603:   nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
  604:   baseWin->InitWindow((NSView*)self, nsnull, 0, 0, 
  605: 		      frame.size.width, frame.size.height);
  606:   baseWin->Create();
  607: 
  608:   return self;
  609: }
  610: 
  611: - (void)dealloc 
  612: {
  613:   NS_RELEASE(_listener);
  614:   NS_IF_RELEASE(_webBrowser);
  615:   nsCocoaBrowserService::TermEmbedding();
  616: 
  617:   [super dealloc];
  618: }
  619: 
  620: - (void)setFrame:(NSRect)frameRect 
  621: {
  622:   [super setFrame:frameRect];
  623:   if (_webBrowser) {
  624:     nsCOMPtr<nsIBaseWindow> window = do_QueryInterface(_webBrowser);
  625:     window->SetSize((PRInt32)frameRect.size.width, 
  626: 		    (PRInt32)frameRect.size.height,
  627: 		    PR_TRUE);
  628:   }
  629: }
  630: 
  631: - (void)addListener:(id <NSBrowserListener>)listener
  632: {
  633:   _listener->AddListener(listener);
  634: }
  635: 
  636: - (void)removeListener:(id <NSBrowserListener>)listener
  637: {
  638:   _listener->RemoveListener(listener);
  639: }
  640: 
  641: - (void)setContainer:(id <NSBrowserContainer>)container
  642: {
  643:   _listener->SetContainer(container);
  644: }
  645: 
  646: - (nsIDOMWindow*)getContentWindow
  647: {
  648:   nsIDOMWindow* window;
  649: 
  650:   _webBrowser->GetContentDOMWindow(&window);
  651: 
  652:   return window;
  653: }
  654: 
  655: - (void)loadURI:(NSURL *)url flags:(unsigned int)flags
  656: {
  657:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
  658:   
  659:   NSString* spec = [url absoluteString];
  660:   int length = [spec length];
  661:   PRUnichar* specStr = nsMemory::Alloc((length+1) * sizeof(PRUnichar));
  662:   [spec getCharacters:specStr];
  663:   specStr[length] = PRUnichar(0);
  664:   
  665: 
  666:   PRUint32 navFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
  667:   if (flags & NSLoadFlagsDontPutInHistory) {
  668:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_HISTORY;
  669:   }
  670:   if (flags & NSLoadFlagsReplaceHistoryEntry) {
  671:     navFlags |= nsIWebNavigation::LOAD_FLAGS_REPLACE_HISTORY;
  672:   }
  673:   if (flags & NSLoadFlagsBypassCacheAndProxy) {
  674:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
  675:                 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
  676:   }
  677: 
  678:   nsresult rv = nav->LoadURI(specStr, navFlags, nsnull, nsnull, nsnull);
  679:   if (NS_FAILED(rv)) {
  680:     // XXX need to throw
  681:   }
  682: 
  683:   nsMemory::Free(specStr);
  684: }
  685: 
  686: - (void)reload:(unsigned int)flags
  687: {
  688:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
  689: 
  690:   PRUint32 navFlags = nsIWebNavigation::LOAD_FLAGS_NONE;
  691:   if (flags & NSLoadFlagsBypassCacheAndProxy) {
  692:     navFlags |= nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE | 
  693:                 nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
  694:   }
  695: 
  696:   nsresult rv = nav->Reload(navFlags);
  697:   if (NS_FAILED(rv)) {
  698:     // XXX need to throw
  699:   }  
  700: }
  701: 
  702: - (BOOL)canGoBack
  703: {
  704:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
  705: 
  706:   PRBool can;
  707:   nav->GetCanGoBack(&can);
  708: 
  709:   return can ? YES : NO;
  710: }
  711: 
  712: - (BOOL)canGoForward
  713: {
  714:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
  715: 
  716:   PRBool can;
  717:   nav->GetCanGoForward(&can);
  718: 
  719:   return can ? YES : NO;
  720: }
  721: 
  722: - (void)goBack
  723: {
  724:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
  725: 
  726:   nsresult rv = nav->GoBack();
  727:   if (NS_FAILED(rv)) {
  728:     // XXX need to throw
  729:   }  
  730: }
  731: 
  732: - (void)goForward
  733: {
  734:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
  735: 
  736:   nsresult rv = nav->GoForward();
  737:   if (NS_FAILED(rv)) {
  738:     // XXX need to throw
  739:   }  
  740: }
  741: 
  742: - (void)gotoIndex:(int)index
  743: {
  744:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
  745: 
  746:   nsresult rv = nav->GotoIndex(index);
  747:   if (NS_FAILED(rv)) {
  748:     // XXX need to throw
  749:   }    
  750: }
  751: 
  752: - (void)stop:(unsigned int)flags
  753: {
  754:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
  755: 
  756:   nsresult rv = nav->Stop(flags);
  757:   if (NS_FAILED(rv)) {
  758:     // XXX need to throw
  759:   }    
  760: }
  761: 
  762: - (NSURL*)getCurrentURI
  763: {
  764:   nsCOMPtr<nsIURI> uri;
  765:   nsCOMPtr<nsIWebNavigation> nav = do_QueryInterface(_webBrowser);
  766: 
  767:   nav->GetCurrentURI(getter_AddRefs(uri));
  768:   if (!uri) {
  769:     return nsnull;
  770:   }
  771: 
  772:   nsXPIDLCString spec;
  773:   uri->GetSpec(getter_Copies(spec));
  774:   
  775:   const char* cstr = spec.get();
  776:   NSString* str = [NSString stringWithCString:cstr];
  777:   NSURL* url = [NSURL URLWithString:str];
  778:   
  779:   return url;
  780: }
  781: 
  782: - (nsIWebBrowser*)getWebBrowser
  783: {
  784:   NS_IF_ADDREF(_webBrowser);
  785:   return _webBrowser;
  786: }
  787: 
  788: - (void)setWebBrowser:(nsIWebBrowser*)browser
  789: {
  790:   _webBrowser = browser;
  791: 
  792:   if (_webBrowser) {
  793:     // Set the container nsIWebBrowserChrome
  794:     _webBrowser->SetContainerWindow(NS_STATIC_CAST(nsIWebBrowserChrome *, 
  795: 						   _listener));
  796: 
  797:     NSRect frame = [self frame];
  798:  
  799:     // Hook up the widget hierarchy with us as the parent
  800:     nsCOMPtr<nsIBaseWindow> baseWin = do_QueryInterface(_webBrowser);
  801:     baseWin->InitWindow((NSView*)self, nsnull, 0, 0, 
  802: 			frame.size.width, frame.size.height);
  803:     baseWin->Create();
  804:   }
  805: 
  806: }
  807: 
  808: @end
  809: 

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