Back to index

lightning-sunbird  0.9+nobinonly
EmbedPrivate.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Christopher Blizzard. Portions created by Christopher Blizzard are Copyright (C) Christopher Blizzard.  All Rights Reserved.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Christopher Blizzard <blizzard@mozilla.org>
00023  *   Brian Edmond <briane@qnx.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <nsIDocShell.h>
00040 #include <nsIURI.h>
00041 #include <nsIWebProgress.h>
00042 #include <nsIDOMDocument.h>
00043 #include <nsIDOMNodeList.h>
00044 #include <nsISelection.h>
00045 #include "nsReadableUtils.h"
00046 #include "nsNetUtil.h"
00047 #include "nsIWidget.h"
00048 
00049 // for do_GetInterface
00050 #include <nsIInterfaceRequestor.h>
00051 #include <nsIInterfaceRequestorUtils.h>
00052 // for do_CreateInstance
00053 #include <nsIComponentManager.h>
00054 
00055 #include <nsIPrintSettings.h>
00056 #include "nsPrintSettingsImpl.h"
00057 
00058 // for initializing our window watcher service
00059 #include <nsIWindowWatcher.h>
00060 
00061 #include <nsILocalFile.h>
00062 #include <nsEmbedAPI.h>
00063 #include <nsString.h>
00064 
00065 // all of the crap that we need for event listeners
00066 // and when chrome windows finish loading
00067 #include <nsIDOMWindow.h>
00068 #include <nsPIDOMWindow.h>
00069 #include <nsIDOMWindowInternal.h>
00070 #include <nsIChromeEventHandler.h>
00071 #include <nsIContentViewer.h>
00072 #include <nsIContentViewerEdit.h>
00073 #include <nsIWebBrowserSetup.h>
00074 #include "nsIWebBrowserPrint.h"
00075 #include "nsIClipboardCommands.h"
00076 
00077 // for the clipboard input group setting
00078 #include "nsClipboard.h"
00079 #include "nsWidgetsCID.h"
00080 
00081 // for the focus hacking we need to do
00082 #include <nsIFocusController.h>
00083 
00084 #include "nsIWebBrowserPrint.h"
00085 #include "nsIPrintOptions.h"
00086 
00087 // all of our local includes
00088 #include "EmbedPrivate.h"
00089 #include "EmbedWindow.h"
00090 #include "EmbedProgress.h"
00091 #include "EmbedContentListener.h"
00092 #include "EmbedEventListener.h"
00093 #include "EmbedWindowCreator.h"
00094 #include "EmbedStream.h"
00095 #include "EmbedPrintListener.h"
00096 
00097 #include "PtMozilla.h"
00098 
00099 /* globals */
00100 extern char *g_Print_Left_Header_String, *g_Print_Right_Header_String, *g_Print_Left_Footer_String, *g_Print_Right_Footer_String;
00101 
00102 static const char sWatcherContractID[] = "@mozilla.org/embedcomp/window-watcher;1";
00103 static NS_DEFINE_CID(kCClipboardCID, NS_CLIPBOARD_CID);
00104 
00105 nsIAppShell *EmbedPrivate::sAppShell    = nsnull;
00106 nsIPref     *EmbedPrivate::sPrefs       = nsnull;
00107 nsVoidArray *EmbedPrivate::sWindowList  = nsnull;
00108 nsClipboard *EmbedPrivate::sClipboard   = nsnull;
00109 
00110 EmbedPrivate::EmbedPrivate(void)
00111 {
00112   mOwningWidget     = nsnull;
00113   mWindow           = nsnull;
00114   mProgress         = nsnull;
00115   mContentListener  = nsnull;
00116   mEventListener    = nsnull;
00117   mStream           = nsnull;
00118   mChromeMask       = 0;
00119   mIsChrome         = PR_FALSE;
00120   mChromeLoaded     = PR_FALSE;
00121   mListenersAttached = PR_FALSE;
00122   mMozWindowWidget  = 0;
00123 
00124        if (!sWindowList) {
00125               sWindowList = new nsVoidArray();
00126        }
00127        sWindowList->AppendElement(this);
00128        if( !sClipboard ) {
00129               nsresult rv;
00130               nsCOMPtr<nsClipboard> s;
00131               s = do_GetService( kCClipboardCID, &rv );
00132               sClipboard = ( nsClipboard * ) s;
00133               if( NS_FAILED( rv ) ) sClipboard = 0;
00134        }
00135 }
00136 
00137 EmbedPrivate::~EmbedPrivate()
00138 {
00139 }
00140 
00141 
00142 static void mozilla_set_default_pref( nsIPref *pref );
00143 
00144 nsresult
00145 EmbedPrivate::Init(PtWidget_t *aOwningWidget)
00146 {
00147        // are we being re-initialized?
00148        if (mOwningWidget)
00149               return NS_OK;
00150 
00151        // hang on with a reference to the owning widget
00152        mOwningWidget = aOwningWidget;
00153 
00154        // Create our embed window, and create an owning reference to it and
00155        // initialize it.  It is assumed that this window will be destroyed
00156        // when we go out of scope.
00157        mWindow = new EmbedWindow();
00158        mWindowGuard = NS_STATIC_CAST(nsIWebBrowserChrome *, mWindow);
00159        mWindow->Init(this);
00160 
00161        // Create our progress listener object, make an owning reference,
00162        // and initialize it.  It is assumed that this progress listener
00163        // will be destroyed when we go out of scope.
00164        mProgress = new EmbedProgress();
00165        mProgressGuard = NS_STATIC_CAST(nsIWebProgressListener *,
00166                                       mProgress);
00167        mProgress->Init(this);
00168 
00169        // Create our content listener object, initialize it and attach it.
00170        // It is assumed that this will be destroyed when we go out of
00171        // scope.
00172        mContentListener = new EmbedContentListener();
00173        mContentListenerGuard = mContentListener;
00174        mContentListener->Init(this);
00175 
00176        // Create our key listener object and initialize it.  It is assumed
00177        // that this will be destroyed before we go out of scope.
00178        mEventListener = new EmbedEventListener();
00179        mEventListenerGuard =
00180        NS_STATIC_CAST(nsISupports *, NS_STATIC_CAST(nsIDOMKeyListener *,
00181                                            mEventListener));
00182        mEventListener->Init(this);
00183 
00184        // Create our print listener object, make an owning reference,
00185        // and initialize it.  It is assumed that this print listener
00186        // will be destroyed when we go out of scope.
00187        mPrint = new EmbedPrintListener();
00188        mPrintGuard = NS_STATIC_CAST(nsIWebProgressListener *, mPrint);
00189        mPrint->Init(this);
00190 
00191        // has the window creator service been set up?
00192        static int initialized = PR_FALSE;
00193        // Set up our window creator ( only once )
00194        if (!initialized) 
00195        {
00196               // We set this flag here instead of on success.  If it failed we
00197               // don't want to keep trying and leaking window creator objects.
00198               initialized = PR_TRUE;
00199 
00200               // create our local object
00201               EmbedWindowCreator *creator = new EmbedWindowCreator();
00202               nsCOMPtr<nsIWindowCreator> windowCreator;
00203               windowCreator = NS_STATIC_CAST(nsIWindowCreator *, creator);
00204 
00205               // Attach it via the watcher service
00206               nsCOMPtr<nsIWindowWatcher> watcher = do_GetService(sWatcherContractID);
00207               if (watcher)
00208               watcher->SetWindowCreator(windowCreator);
00209   }
00210 
00211        if (!sPrefs)
00212        {
00213               // get prefs
00214               nsresult rv;
00215               nsCOMPtr<nsIPref> pref(do_GetService(NS_PREF_CONTRACTID, &rv));
00216               if (pref)
00217               {
00218                      sPrefs = pref.get();
00219                      NS_ADDREF( sPrefs );
00220                      extern int sProfileDirCreated;
00221                      if( sProfileDirCreated ) mozilla_set_default_pref( pref );
00222                      sPrefs->ReadUserPrefs( nsnull );
00223               }
00224        }
00225 
00226   return NS_OK;
00227 }
00228 
00229 nsIPref *
00230 EmbedPrivate::GetPrefs()
00231 {
00232        return (sPrefs);
00233 }
00234 
00235 nsresult
00236 EmbedPrivate::Setup()
00237 {
00238        // Get the nsIWebBrowser object for our embedded window.
00239        nsCOMPtr<nsIWebBrowser> webBrowser;
00240        mWindow->GetWebBrowser(getter_AddRefs(webBrowser));
00241 
00242        // Configure what the web browser can and cannot do
00243        PRBool aAllowPlugins = PR_TRUE;
00244        nsCOMPtr<nsIWebBrowserSetup> webBrowserAsSetup(do_QueryInterface(webBrowser));
00245        webBrowserAsSetup->SetProperty(nsIWebBrowserSetup::SETUP_ALLOW_PLUGINS, aAllowPlugins);
00246 
00247        // get a handle on the navigation object
00248        mNavigation = do_QueryInterface(webBrowser);
00249 
00250        // Create our session history object and tell the navigation object
00251        // to use it.  We need to do this before we create the web browser
00252        // window.
00253        mSessionHistory = do_CreateInstance(NS_SHISTORY_CONTRACTID);
00254        mNavigation->SetSessionHistory(mSessionHistory);
00255 
00256        // create the window
00257        mWindow->CreateWindow();
00258 
00259        // bind the progress listener to the browser object
00260        nsCOMPtr<nsISupportsWeakReference> supportsWeak;
00261        supportsWeak = do_QueryInterface(mProgressGuard);
00262        nsCOMPtr<nsIWeakReference> weakRef;
00263        supportsWeak->GetWeakReference(getter_AddRefs(weakRef));
00264        webBrowser->AddWebBrowserListener(weakRef, nsIWebProgressListener::GetIID());
00265 
00266        // set ourselves as the parent uri content listener
00267        nsCOMPtr<nsIURIContentListener> uriListener;
00268        uriListener = do_QueryInterface(mContentListenerGuard);
00269        webBrowser->SetParentURIContentListener(uriListener);
00270 
00271        nsCOMPtr<nsIWebBrowserPrint> print(do_GetInterface(webBrowser));
00272 //     if (print)
00273 //            print->GetNewPrintSettings(getter_AddRefs(m_PrintSettings));
00274 
00275        return NS_OK;
00276 }
00277 
00278 void
00279 EmbedPrivate::Show(void)
00280 {
00281   // Get the nsIWebBrowser object for our embedded window.
00282   nsCOMPtr<nsIWebBrowser> webBrowser;
00283   mWindow->GetWebBrowser(getter_AddRefs(webBrowser));
00284 
00285   // and set the visibility on the thing
00286   nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(webBrowser);
00287   baseWindow->SetVisibility(PR_TRUE);
00288 }
00289 
00290 void
00291 EmbedPrivate::Hide(void)
00292 {
00293   // Get the nsIWebBrowser object for our embedded window.
00294   nsCOMPtr<nsIWebBrowser> webBrowser;
00295   mWindow->GetWebBrowser(getter_AddRefs(webBrowser));
00296 
00297   // and set the visibility on the thing
00298   nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(webBrowser);
00299   baseWindow->SetVisibility(PR_FALSE);
00300 }
00301 
00302 void
00303 EmbedPrivate::Position(PRUint32 aX, PRUint32 aY)
00304 {
00305        mWindow->mBaseWindow->SetPosition(aX, aY);
00306 }
00307 
00308 void
00309 EmbedPrivate::Size(PRUint32 aWidth, PRUint32 aHeight)
00310 {
00311        mWindow->mBaseWindow->SetSize(aWidth, aHeight, PR_TRUE);
00312 }
00313 
00314 
00315 
00316 void
00317 EmbedPrivate::Destroy(void)
00318 {
00319   // Get the nsIWebBrowser object for our embedded window.
00320   nsCOMPtr<nsIWebBrowser> webBrowser;
00321   mWindow->GetWebBrowser(getter_AddRefs(webBrowser));
00322 
00323   // Release our progress listener
00324   nsCOMPtr<nsISupportsWeakReference> supportsWeak;
00325   supportsWeak = do_QueryInterface(mProgressGuard);
00326   nsCOMPtr<nsIWeakReference> weakRef;
00327   supportsWeak->GetWeakReference(getter_AddRefs(weakRef));
00328   webBrowser->RemoveWebBrowserListener(weakRef,
00329                                    nsIWebProgressListener::GetIID());
00330   weakRef = nsnull;
00331   supportsWeak = nsnull;
00332 
00333   // Release our content listener
00334   webBrowser->SetParentURIContentListener(nsnull);
00335   mContentListenerGuard = nsnull;
00336   mContentListener = nsnull;
00337 
00338   // Now that we have removed the listener, release our progress
00339   // object
00340   mProgressGuard = nsnull;
00341   mProgress = nsnull;
00342 
00343   // detach our event listeners and release the event receiver
00344   DetachListeners();
00345   if (mEventReceiver)
00346     mEventReceiver = nsnull;
00347 
00348        // remove this from the window list
00349   sWindowList->RemoveElement(this);
00350   
00351   // destroy our child window
00352   mWindow->ReleaseChildren();
00353 
00354   // release navigation
00355   mNavigation = nsnull;
00356 
00357   //m_PrintSettings = nsnull;
00358 
00359   // release session history
00360   mSessionHistory = nsnull;
00361 
00362   mOwningWidget = nsnull;
00363 
00364   mMozWindowWidget = 0;
00365 }
00366 
00367 void
00368 EmbedPrivate::SetURI(const char *aURI)
00369 {
00370   mURI.AssignWithConversion(aURI);
00371 }
00372 
00373 void
00374 EmbedPrivate::LoadCurrentURI(void)
00375 {
00376   if (mURI.Length())
00377        mNavigation->LoadURI(mURI.get(),                        // URI string
00378                      nsIWebNavigation::LOAD_FLAGS_NONE, // Load flags
00379                      nsnull,                            // Refering URI
00380                      nsnull,                            // Post data
00381                      nsnull);                           // extra headers
00382 }
00383 
00384 void
00385 EmbedPrivate::Stop(void)
00386 {
00387        if (mNavigation)
00388               mNavigation->Stop(nsIWebNavigation::STOP_ALL);
00389 }
00390 
00391 void
00392 EmbedPrivate::Reload(int32_t flags)
00393 {
00394        PRUint32 reloadFlags = 0;
00395 
00396        // map the external API to the internal web navigation API.
00397        switch (flags) 
00398        {
00399               case MOZ_EMBED_FLAG_RELOADNORMAL:
00400                      reloadFlags = 0;
00401                      break;
00402               case MOZ_EMBED_FLAG_RELOADBYPASSCACHE:
00403                      reloadFlags = nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE;
00404                      break;
00405               case MOZ_EMBED_FLAG_RELOADBYPASSPROXY:
00406                      reloadFlags = nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY;
00407                      break;
00408               case MOZ_EMBED_FLAG_RELOADBYPASSPROXYANDCACHE:
00409                      reloadFlags = (nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY |
00410                              nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE);
00411                      break;
00412               case MOZ_EMBED_FLAG_RELOADCHARSETCHANGE:
00413                      reloadFlags = nsIWebNavigation::LOAD_FLAGS_CHARSET_CHANGE;
00414                      break;
00415               default:
00416                      reloadFlags = 0;
00417                      break;
00418        }
00419 
00420        if (mNavigation)
00421               mNavigation->Reload(reloadFlags);
00422 }
00423 
00424 void
00425 EmbedPrivate::Back(void)
00426 {
00427        if (mNavigation)
00428               mNavigation->GoBack();
00429 }
00430 
00431 void
00432 EmbedPrivate::Forward(void)
00433 {
00434        if (mNavigation)
00435               mNavigation->GoForward();
00436 }
00437 
00438 void
00439 EmbedPrivate::ScrollUp(int amount)
00440 {
00441        nsCOMPtr<nsIWebBrowser> webBrowser;
00442        mWindow->GetWebBrowser(getter_AddRefs(webBrowser));
00443 
00444        nsCOMPtr<nsIDOMWindow> oDomWindow;
00445        nsresult rv = webBrowser->GetContentDOMWindow(getter_AddRefs(oDomWindow));
00446 
00447        if (oDomWindow)
00448               rv = oDomWindow->ScrollBy(0, -amount);
00449 }
00450 void
00451 EmbedPrivate::ScrollDown(int amount)
00452 {
00453        nsCOMPtr<nsIWebBrowser> webBrowser;
00454        mWindow->GetWebBrowser(getter_AddRefs(webBrowser));
00455 
00456        nsCOMPtr<nsIDOMWindow> oDomWindow;
00457        nsresult rv = webBrowser->GetContentDOMWindow(getter_AddRefs(oDomWindow));
00458 
00459        if (oDomWindow)
00460               rv = oDomWindow->ScrollBy(0, amount);
00461 }
00462 void
00463 EmbedPrivate::ScrollLeft(int amount)
00464 {
00465        nsCOMPtr<nsIWebBrowser> webBrowser;
00466        mWindow->GetWebBrowser(getter_AddRefs(webBrowser));
00467 
00468        nsCOMPtr<nsIDOMWindow> oDomWindow;
00469        nsresult rv = webBrowser->GetContentDOMWindow(getter_AddRefs(oDomWindow));
00470 
00471        if (oDomWindow)
00472               rv = oDomWindow->ScrollBy(-amount, 0);
00473 }
00474 void
00475 EmbedPrivate::ScrollRight(int amount)
00476 {
00477        nsCOMPtr<nsIWebBrowser> webBrowser;
00478        mWindow->GetWebBrowser(getter_AddRefs(webBrowser));
00479 
00480        nsCOMPtr<nsIDOMWindow> oDomWindow;
00481        nsresult rv = webBrowser->GetContentDOMWindow(getter_AddRefs(oDomWindow));
00482 
00483        if (oDomWindow)
00484               rv = oDomWindow->ScrollBy(amount, 0);
00485 }
00486 
00487 void
00488 EmbedPrivate::ScrollTo(int x, int y)
00489 {
00490        nsCOMPtr<nsIWebBrowser> webBrowser;
00491        mWindow->GetWebBrowser(getter_AddRefs(webBrowser));
00492 
00493        nsCOMPtr<nsIDOMWindow> oDomWindow;
00494        nsresult rv = webBrowser->GetContentDOMWindow(getter_AddRefs(oDomWindow));
00495 
00496        if (oDomWindow)
00497               rv = oDomWindow->ScrollTo(x, y);
00498 }
00499 
00500 
00501 
00502 
00503 
00504 PRBool
00505 EmbedPrivate::CanGoBack()
00506 {
00507        PRBool nsresult = PR_FALSE;
00508 
00509        if (mNavigation)
00510        mNavigation->GetCanGoBack(&nsresult);
00511 
00512        return (nsresult);
00513 }
00514 
00515 PRBool
00516 EmbedPrivate::CanGoForward()
00517 {
00518        PRBool nsresult = PR_FALSE;
00519 
00520        if (mNavigation)
00521        mNavigation->GetCanGoForward(&nsresult);
00522 
00523        return (nsresult);
00524 }
00525 
00526 void
00527 EmbedPrivate::Cut(int ig)
00528 {
00529        nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(mWindow->mWebBrowser));
00530        if (clipboard) {
00531               //
00532               // Pass Voyager input group to clipboard functions.
00533               // Using Ctrl-C/V does not do this, only Edit->Copy/Paste.
00534               //
00535               if (sClipboard)
00536                      sClipboard->SetInputGroup(ig);
00537            clipboard->CutSelection();
00538               }
00539 }
00540 
00541 void
00542 EmbedPrivate::Copy(int ig)
00543 {
00544        nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(mWindow->mWebBrowser));
00545        if (clipboard) {
00546               //
00547               // Pass Voyager input group to clipboard functions.
00548               // Using Ctrl-C/V does not do this, only Edit->Copy/Paste.
00549               //
00550               if (sClipboard)
00551                      sClipboard->SetInputGroup(ig);
00552            clipboard->CopySelection();
00553               }
00554 }
00555 
00556 void
00557 EmbedPrivate::Paste(int ig)
00558 {
00559        nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(mWindow->mWebBrowser));
00560        if (clipboard) {
00561               //
00562               // Pass Voyager input group to clipboard functions.
00563               // Using Ctrl-C/V does not do this, only Edit->Copy/Paste.
00564               //
00565               if (sClipboard)
00566                      sClipboard->SetInputGroup(ig);
00567            clipboard->Paste();
00568               }
00569 }
00570 
00571 void
00572 EmbedPrivate::SelectAll()
00573 {
00574 /*
00575        nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(mWindow->mWebBrowser));
00576        if (clipboard)
00577            clipboard->SelectAll();
00578 */
00579 
00580        nsCOMPtr<nsIDOMWindow> domWindow;
00581        mWindow->mWebBrowser->GetContentDOMWindow( getter_AddRefs( domWindow ) );
00582        if( !domWindow ) {
00583               NS_WARNING( "no dom window in content finished loading\n" );
00584               return;
00585               }
00586 
00587        nsCOMPtr<nsIDOMDocument> domDocument;
00588        domWindow->GetDocument( getter_AddRefs( domDocument ) );
00589        if( !domDocument ) {
00590               NS_WARNING( "no dom document\n" );
00591               return;
00592               }
00593 
00594        nsCOMPtr<nsIDOMNodeList> list;
00595        domDocument->GetElementsByTagName( NS_LITERAL_STRING( "body" ), getter_AddRefs( list ) );
00596        if( !list ) {
00597               NS_WARNING( "no list\n" );
00598               return;
00599               }
00600 
00601        nsCOMPtr<nsIDOMNode> node;
00602        list->Item( 0, getter_AddRefs( node ) );
00603        if( !node ) {
00604               NS_WARNING( "no node\n" );
00605               return;
00606               }
00607 
00608        nsCOMPtr<nsISelection> selection;
00609        domWindow->GetSelection( getter_AddRefs( selection ) );
00610        if( !selection ) {
00611               NS_WARNING( "no selection\n" );
00612               return;
00613               }
00614 
00615        selection->SelectAllChildren( node );
00616 
00617 }
00618 
00619 void
00620 EmbedPrivate::Clear()
00621 {
00622        nsCOMPtr<nsIClipboardCommands> clipboard(do_GetInterface(mWindow->mWebBrowser));
00623        if (clipboard)
00624            clipboard->SelectNone();
00625 }
00626 
00627 void
00628 EmbedPrivate::Print(PpPrintContext_t *pc)
00629 {
00630     nsCOMPtr<nsIWebBrowserPrint> browserAsPrint = do_GetInterface(mWindow->mWebBrowser);
00631     NS_ASSERTION(browserAsPrint, "No nsIWebBrowserPrint!");
00632 
00633     nsCOMPtr<nsIPrintSettings> printSettings;
00634     browserAsPrint->GetGlobalPrintSettings(getter_AddRefs(printSettings));
00635     if (printSettings) 
00636     {
00637     printSettings->SetPrintSilent(PR_TRUE);
00638               printSettings->SetEndPageRange((PRInt32) pc);
00639 
00640               nsAutoString format_left_footer;
00641               PrintHeaderFooter_FormatSpecialCodes( g_Print_Left_Footer_String, format_left_footer );
00642               nsAutoString format_right_footer;
00643               PrintHeaderFooter_FormatSpecialCodes( g_Print_Right_Footer_String, format_right_footer );
00644               nsAutoString format_left_header;
00645               PrintHeaderFooter_FormatSpecialCodes( g_Print_Left_Header_String, format_left_header );
00646               nsAutoString format_right_header;
00647               PrintHeaderFooter_FormatSpecialCodes( g_Print_Right_Header_String, format_right_header );
00648 
00649               printSettings->SetFooterStrLeft( format_left_footer.get() );
00650               printSettings->SetFooterStrRight( format_right_footer.get() );
00651               printSettings->SetHeaderStrLeft( format_left_header.get() );
00652               printSettings->SetHeaderStrRight( format_right_header.get() );
00653     }
00654 
00655               nsIPref *pref = GetPrefs();
00656               pref->SetBoolPref( "print.show_print_progress", PR_FALSE );
00657 
00658     browserAsPrint->Print(printSettings, mPrint);
00659 }
00660 
00661 nsresult
00662 EmbedPrivate::OpenStream(const char *aBaseURI, const char *aContentType)
00663 {
00664   nsresult rv;
00665 
00666   if (!mStream) {
00667     mStream = new EmbedStream();
00668     mStreamGuard = do_QueryInterface(mStream);
00669     mStream->InitOwner(this);
00670     rv = mStream->Init();
00671     if (NS_FAILED(rv))
00672       return rv;
00673   }
00674 
00675   rv = mStream->OpenStream(aBaseURI, aContentType);
00676   return rv;
00677 }
00678 
00679 int
00680 EmbedPrivate::SaveAs(char *fname, char *dirname)
00681 {      
00682        if (mWindow)
00683               return (mWindow->SaveAs(fname, dirname));
00684        return (1);
00685 }
00686 
00687 nsresult
00688 EmbedPrivate::AppendToStream(const char *aData, PRInt32 aLen)
00689 {
00690   if (!mStream)
00691     return NS_ERROR_FAILURE;
00692 
00693   // Attach listeners to this document since in some cases we don't
00694   // get updates for content added this way.
00695   ContentStateChange();
00696 
00697   return mStream->AppendToStream(aData, aLen);
00698 }
00699 
00700 nsresult
00701 EmbedPrivate::CloseStream(void)
00702 {
00703   nsresult rv;
00704 
00705   if (!mStream)
00706     return NS_ERROR_FAILURE;
00707   rv = mStream->CloseStream();
00708 
00709   // release
00710   mStream = 0;
00711   mStreamGuard = 0;
00712 
00713   return rv;
00714 }
00715 
00716 /* static */
00717 EmbedPrivate *
00718 EmbedPrivate::FindPrivateForBrowser(nsIWebBrowserChrome *aBrowser)
00719 {
00720        if (!sWindowList)
00721          return nsnull;
00722 
00723        // Get the number of browser windows.
00724        PRInt32 count = sWindowList->Count();
00725        // This function doesn't get called very often at all ( only when
00726        // creating a new window ) so it's OK to walk the list of open
00727        // windows.
00728        for (int i = 0; i < count; i++) 
00729        {
00730          EmbedPrivate *tmpPrivate = NS_STATIC_CAST(EmbedPrivate *,
00731                                                  sWindowList->ElementAt(i));
00732          // get the browser object for that window
00733          nsIWebBrowserChrome *chrome = NS_STATIC_CAST(nsIWebBrowserChrome *,
00734                                              tmpPrivate->mWindow);
00735          if (chrome == aBrowser)
00736               return tmpPrivate;
00737        }
00738 
00739   return nsnull;
00740 }
00741 
00742 void
00743 EmbedPrivate::ContentStateChange(void)
00744 {
00745 
00746   // we don't attach listeners to chrome
00747   if (mListenersAttached && !mIsChrome)
00748     return;
00749 
00750   GetListener();
00751 
00752   if (!mEventReceiver)
00753     return;
00754   
00755   AttachListeners();
00756 
00757 }
00758 
00759 void
00760 EmbedPrivate::ContentFinishedLoading(void)
00761 {
00762   if (mIsChrome) {
00763     // We're done loading.
00764     mChromeLoaded = PR_TRUE;
00765 
00766     // get the web browser
00767     nsCOMPtr<nsIWebBrowser> webBrowser;
00768     mWindow->GetWebBrowser(getter_AddRefs(webBrowser));
00769     
00770     // get the content DOM window for that web browser
00771     nsCOMPtr<nsIDOMWindow> domWindow;
00772     webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
00773     if (!domWindow) {
00774       NS_WARNING("no dom window in content finished loading\n");
00775       return;
00776     }
00777     
00778     // resize the content
00779     domWindow->SizeToContent();
00780 
00781     // and since we're done loading show the window, assuming that the
00782     // visibility flag has been set.
00783     PRBool visibility;
00784     mWindow->GetVisibility(&visibility);
00785     if (visibility)
00786       mWindow->SetVisibility(PR_TRUE);
00787   }
00788 }
00789 
00790 
00791 
00792 #if 0
00793 
00794 /*
00795        not used - we handle focus in/out activate/deactivate in
00796        child_getting_focus/child_losing_focus methods
00797        of the PtMozilla widget class
00798 */
00799 
00800 
00801 // handle focus in and focus out events
00802 void
00803 EmbedPrivate::TopLevelFocusIn(void)
00804 {
00805   nsCOMPtr<nsPIDOMWindow> piWin;
00806   GetPIDOMWindow(getter_AddRefs(piWin));
00807 
00808   if (!piWin)
00809     return;
00810 
00811   nsIFocusController *focusController = piWin->GetRootFocusController();
00812   if (focusController)
00813     focusController->SetActive(PR_TRUE);
00814 }
00815 
00816 void
00817 EmbedPrivate::TopLevelFocusOut(void)
00818 {
00819   nsCOMPtr<nsPIDOMWindow> piWin;
00820   GetPIDOMWindow(getter_AddRefs(piWin));
00821 
00822   if (!piWin)
00823     return;
00824 
00825   nsIFocusController *focusController = piWin->GetRootFocusController();
00826   if (focusController)
00827     focusController->SetActive(PR_FALSE);
00828 }
00829 
00830 void
00831 EmbedPrivate::ChildFocusIn(void)
00832 {
00833   nsCOMPtr<nsPIDOMWindow> piWin;
00834   GetPIDOMWindow(getter_AddRefs(piWin));
00835 
00836   if (!piWin)
00837     return;
00838 
00839   piWin->Activate();
00840 }
00841 
00842 void
00843 EmbedPrivate::ChildFocusOut(void)
00844 {
00845   nsCOMPtr<nsPIDOMWindow> piWin;
00846   GetPIDOMWindow(getter_AddRefs(piWin));
00847 
00848   if (!piWin)
00849     return;
00850 
00851   piWin->Deactivate();
00852 
00853   // but the window is still active until the toplevel gets a focus
00854   // out
00855   nsIFocusController *focusController = piWin->GetRootFocusController();
00856   if (focusController)
00857     focusController->SetActive(PR_TRUE);
00858 
00859 }
00860 
00861 #endif
00862 
00863 
00864 // Get the event listener for the chrome event handler.
00865 
00866 void
00867 EmbedPrivate::GetListener(void)
00868 {
00869   if (mEventReceiver)
00870     return;
00871 
00872   nsCOMPtr<nsPIDOMWindow> piWin;
00873   GetPIDOMWindow(getter_AddRefs(piWin));
00874 
00875   if (!piWin)
00876     return;
00877 
00878   mEventReceiver = do_QueryInterface(piWin->GetChromeEventHandler());
00879 }
00880 
00881 // attach key and mouse event listeners
00882 
00883 void
00884 EmbedPrivate::AttachListeners(void)
00885 {
00886   if (!mEventReceiver || mListenersAttached)
00887     return;
00888 
00889   nsIDOMEventListener *eventListener =
00890     NS_STATIC_CAST(nsIDOMEventListener *,
00891                  NS_STATIC_CAST(nsIDOMKeyListener *, mEventListener));
00892 
00893   // add the key listener
00894   nsresult rv;
00895   rv = mEventReceiver->AddEventListenerByIID(eventListener,
00896                                         NS_GET_IID(nsIDOMKeyListener));
00897   if (NS_FAILED(rv)) {
00898     NS_WARNING("Failed to add key listener\n");
00899     return;
00900   }
00901 
00902   rv = mEventReceiver->AddEventListenerByIID(eventListener,
00903                                         NS_GET_IID(nsIDOMMouseListener));
00904   if (NS_FAILED(rv)) {
00905     NS_WARNING("Failed to add mouse listener\n");
00906     return;
00907   }
00908 
00909   // ok, all set.
00910   mListenersAttached = PR_TRUE;
00911 }
00912 
00913 void
00914 EmbedPrivate::DetachListeners(void)
00915 {
00916   if (!mListenersAttached || !mEventReceiver)
00917     return;
00918 
00919   nsIDOMEventListener *eventListener =
00920     NS_STATIC_CAST(nsIDOMEventListener *,
00921                  NS_STATIC_CAST(nsIDOMKeyListener *, mEventListener));
00922 
00923   nsresult rv;
00924   rv = mEventReceiver->RemoveEventListenerByIID(eventListener,
00925                                           NS_GET_IID(nsIDOMKeyListener));
00926   if (NS_FAILED(rv)) {
00927     NS_WARNING("Failed to remove key listener\n");
00928     return;
00929   }
00930 
00931   rv =
00932     mEventReceiver->RemoveEventListenerByIID(eventListener,
00933                                         NS_GET_IID(nsIDOMMouseListener));
00934   if (NS_FAILED(rv)) {
00935     NS_WARNING("Failed to remove mouse listener\n");
00936     return;
00937   }
00938 
00939 
00940   mListenersAttached = PR_FALSE;
00941 }
00942 
00943 nsresult
00944 EmbedPrivate::GetPIDOMWindow(nsPIDOMWindow **aPIWin)
00945 {
00946   *aPIWin = nsnull;
00947 
00948   // get the web browser
00949   nsCOMPtr<nsIWebBrowser> webBrowser;
00950   mWindow->GetWebBrowser(getter_AddRefs(webBrowser));
00951 
00952   // get the content DOM window for that web browser
00953   nsCOMPtr<nsIDOMWindow> domWindow;
00954   webBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
00955   if (!domWindow)
00956     return NS_ERROR_FAILURE;
00957 
00958   // get the private DOM window
00959   nsCOMPtr<nsPIDOMWindow> domWindowPrivate = do_QueryInterface(domWindow);
00960   // and the root window for that DOM window
00961        *aPIWin = domWindowPrivate->GetPrivateRoot();
00962   
00963   if (*aPIWin) {
00964     NS_ADDREF(*aPIWin);
00965     return NS_OK;
00966   }
00967 
00968   return NS_ERROR_FAILURE;
00969 
00970 }
00971 
00972 
00973 static void mozilla_set_default_pref( nsIPref *pref )
00974 {
00975 /*
00976        PtMozillaWidget_t *moz = ( PtMozillaWidget_t * ) widget;
00977        nsIPref *pref = moz->EmbedRef->GetPrefs();
00978 */
00979 
00980        /* translation set = Western European (ISO 8859-1) */
00981        pref->SetUnicharPref( "intl.charset.default", NS_ConvertASCIItoUCS2("iso8859-1").get());
00982 
00983        /* HTML Options */
00984        pref->SetUnicharPref( "browser.visited_color", NS_ConvertASCIItoUCS2("#008080").get() );
00985        pref->SetUnicharPref( "browser.anchor_color", NS_ConvertASCIItoUCS2("#0000ff").get() );
00986        pref->SetUnicharPref( "browser.display.foreground_color", NS_ConvertASCIItoUCS2("#000000").get() );
00987        pref->SetUnicharPref( "browser.display.background_color", NS_ConvertASCIItoUCS2("#ffffff").get() );
00988 
00989        pref->SetCharPref( "font.name.serif.x-western", "serif" );
00990        pref->SetCharPref( "font.name.sans-serif.x-western", "sans-serif" );
00991        pref->SetCharPref( "font.name.monospace.x-western", "monospace" );
00992        pref->SetCharPref( "font.name.cursive.x-western", "cursive" );
00993        pref->SetCharPref( "font.name.fantasy.x-western", "fantasy" );
00994 
00995        pref->SetBoolPref( "browser.display.use_document_colors", PR_TRUE );
00996        pref->SetBoolPref( "browser.underline_anchors", PR_TRUE );
00997        pref->SetIntPref( "font.size.variable.x-western", 14 );
00998        pref->SetIntPref( "font.size.fixed.x-western", 12 );
00999        pref->SetIntPref( "browser.history_expire_days", 4 );
01000        pref->SetIntPref( "browser.sessionhistory.max_entries", 50 );
01001 //     pref->SetIntPref( "browser.cache.check_doc_frequency", 2 );
01002        pref->SetBoolPref( "browser.cache.disk.enable", PR_TRUE );
01003        pref->SetIntPref( "browser.cache.disk.capacity", 5000 );
01004        pref->SetIntPref( "network.http.max-connections", 4 );
01005        pref->SetIntPref( "network.proxy.http_port", 80 );
01006        pref->SetIntPref( "network.proxy.ftp_port", 80 );
01007        pref->SetIntPref( "network.proxy.gopher_port", 80 );
01008 
01009        pref->SetCharPref( "general.skins.selectedSkin", "classic/1.0" );
01010        pref->SetIntPref( "browser.cache.memory.capacity", 100 ); /* 100k ( no cache ) */
01011        pref->SetCharPref( "user.print.print_frame", "print_frame_selected" );
01012 
01013        pref->SetCharPref( "print.print_headercenter", "" );
01014        pref->SetCharPref( "print.print_footercenter", "" );
01015 
01016        pref->SavePrefFile( nsnull );
01017 }
01018 
01019 
01020 //------------------------------------------------------------------------------
01021 #define FORMAT_ESCAPE_CHARACTER                '&'
01022 void EmbedPrivate::PrintHeaderFooter_FormatSpecialCodes(const char *original, nsString& aNewStr)
01023 {
01024        /* Think of this as a sprintf-variant. */
01025 
01026        const char *szPattern = original;
01027 
01028        time_t aclock;
01029        struct tm *tm;
01030 
01031        char workBuffer[20], *sz;
01032 
01033        nsAutoString result;
01034 
01035        while ( *szPattern )
01036        {
01037               if (*szPattern != FORMAT_ESCAPE_CHARACTER)
01038               {
01039                      workBuffer[0] = *szPattern;
01040                      szPattern++;
01041                      workBuffer[1] = 0;
01042 
01043                      nsAutoString ss;
01044                      ss.AssignWithConversion( workBuffer );
01045                      result += ss;
01046               }
01047               else
01048               {
01049                      szPattern++;                       /* skip over '&' */
01050                      switch (*szPattern)
01051                      {
01052                      case 'w':                                 /* window name */
01053                      case 'W':
01054                             szPattern++;                /* advance past "&w" */
01055 
01056                             /* add the title */
01057                             PRUnichar *uTitle;
01058                             mWindow->GetTitle( &uTitle );
01059                             result += uTitle;
01060                             break;
01061 
01062 
01063                      case 'u':                                 /* URL */
01064                      case 'U':                                 /* TODO should this be ifdef'd for kiosk's */
01065                             szPattern++;                /* advance past "&w" */
01066 
01067                             /* add the URL */
01068                             result += mURI.get();
01069                             break;
01070 
01071 
01072                      case 'd':            /* date -- american style "mmm dd yyyy" */
01073                      case 'D':            /* date -- european style "dd mmm yyyy" */
01074                             szPattern++;
01075                             
01076                             (void) time(&aclock);
01077                             tm = localtime(&aclock);
01078                             sz = asctime(tm);
01079 
01080                             /*  ..........1.........2.... .   */
01081                             /*  012345678901234567890123. .   */
01082                             /* "Fri Oct 22 09:15:00 1993\n\0" */
01083 
01084                             if (szPattern[1] == 'd')
01085                             {
01086                                    workBuffer[0] = sz[4];      /* O */
01087                                    workBuffer[1] = sz[5];      /* c */
01088                                    workBuffer[2] = sz[6];      /* t */
01089                                    workBuffer[3] = sz[7];      /* _ */
01090                                    workBuffer[4] = sz[8];      /* 2 */
01091                                    workBuffer[5] = sz[9];      /* 2 */
01092                             }
01093                             else
01094                             {
01095                                    workBuffer[0] = sz[8];      /* 2 */
01096                                    workBuffer[1] = sz[9];      /* 2 */
01097                                    workBuffer[2] = sz[7];      /* _ */
01098                                    workBuffer[3] = sz[4];      /* O */
01099                                    workBuffer[4] = sz[5];      /* c */
01100                                    workBuffer[5] = sz[6];      /* t */
01101                             }
01102                             workBuffer[6] = sz[10];     /* _ */
01103                             workBuffer[7] = sz[20];     /* 1 */
01104                             workBuffer[8] = sz[21];     /* 9 */
01105                             workBuffer[9] = sz[22];     /* 9 */
01106                             workBuffer[10] = sz[23];    /* 3 */
01107                             workBuffer[11] = 0;
01108 
01109                             /* add the content of workBuffer */
01110                             {
01111                             nsAutoString ss;
01112                             ss.AssignWithConversion( workBuffer );
01113                             result += ss;
01114                             }
01115 
01116                             break;
01117 
01118 
01119                      case 't':                                 /* time "HH:MM am" (12 hour format) */
01120                             szPattern++;
01121 
01122                             (void) time(&aclock);
01123                             tm = localtime(&aclock);
01124 
01125                             strftime(workBuffer, sizeof(workBuffer), "%I:%M %p", tm);
01126 
01127                             {
01128                             /* add the content of workBuffer */
01129                             nsAutoString ss;
01130                             ss.AssignWithConversion( workBuffer );
01131                             result += ss;
01132                             }
01133 
01134                             break;
01135 
01136                             
01137                      case 'T':                                 /* time "HH:MM" (24 hour format) */
01138                             szPattern++;
01139 
01140                             (void) time(&aclock);
01141                             tm = localtime(&aclock);
01142 
01143                             strftime(workBuffer, sizeof(workBuffer), "%H:%M", tm);
01144 
01145                             /* add the content of workBuffer */
01146                             {
01147                             nsAutoString ss;
01148                             ss.AssignWithConversion( workBuffer );
01149                             result += ss;
01150                             }
01151                             break;
01152 
01153                      case 'p':                                 /* current page number */
01154                             szPattern++;
01155 
01156                             {
01157                             /* add the page number */
01158                             const PRUnichar * uStr = NS_LITERAL_STRING( "&P" ).get();
01159                             result += uStr;
01160                             }
01161                             break;
01162 
01163                      case 'P': /* current of total number of pages */
01164                             /* add the page number */
01165                             {
01166                             const PRUnichar * uStr = NS_LITERAL_STRING( "&PT" ).get();
01167                             result += uStr;
01168                             }
01169                             break;
01170 
01171                      case FORMAT_ESCAPE_CHARACTER:      /* && expands to a single & */
01172 
01173                             workBuffer[0] = *szPattern;
01174                             szPattern++;
01175                             workBuffer[1] = 0;
01176                             {
01177                             nsAutoString ss;
01178                             ss.AssignWithConversion( workBuffer );
01179                             result += ss;
01180                             }
01181                             break;
01182 
01183                      case '\0':                                /* copy '&' to output */
01184 
01185                             workBuffer[0] = FORMAT_ESCAPE_CHARACTER;
01186                             workBuffer[1] = 0;
01187                             {
01188                             nsAutoString ss;
01189                             ss.AssignWithConversion( workBuffer );
01190                             result += ss;
01191                             }
01192                             break;
01193                             
01194                      default:                                  /* copy '&*' to output */
01195 #if 0
01196                             SM_STRNCPY(p,(const char *) &szPattern[-1],lenCopy);
01197 #endif
01198                             szPattern++;
01199 
01200                             /* add the &szPattern[-1] */
01201                             {
01202                             nsAutoString ss;
01203                             ss.AssignWithConversion( &szPattern[-1] );
01204                             result += ss;
01205                             }
01206                             break;
01207                      }
01208               }
01209        }
01210 
01211        aNewStr.Assign( result );
01212 }