Back to index

lightning-sunbird  0.9+nobinonly
CPrintAttachment.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Conrad Carlen <ccarlen@netscape.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 // Local
00040 #include "CPrintAttachment.h"
00041 #include "CBrowserShell.h"
00042 
00043 // Gecko
00044 #include "nsIPrintingPromptService.h"
00045 #include "nsIDOMWindow.h"
00046 #include "nsServiceManagerUtils.h"
00047 #include "nsIWebBrowserPrint.h"
00048 #include "nsIInterfaceRequestorUtils.h"
00049 #include "nsIWebProgressListener.h"
00050 #include "nsIWebBrowserChrome.h"
00051 #include "nsIEmbeddingSiteWindow.h"
00052 
00053 
00054 //*****************************************************************************
00055 //***    CPrintAttachment
00056 //*****************************************************************************
00057 
00058 CPrintAttachment::CPrintAttachment(PaneIDT inBrowserPaneID,
00059                                    MessageT inMessage,
00060                                    Boolean inExecuteHost) :
00061     LAttachment(inMessage,inExecuteHost),
00062     mBrowserShell(nil), mBrowserShellPaneID(inBrowserPaneID)
00063 {
00064 }
00065 
00066 
00067 CPrintAttachment::CPrintAttachment(LStream* inStream) :
00068     LAttachment(inStream),
00069     mBrowserShell(nil), mBrowserShellPaneID(PaneIDT_Unspecified)
00070 {
00071        *inStream >> mBrowserShellPaneID;
00072 }
00073 
00074 
00075 CPrintAttachment::~CPrintAttachment()
00076 {
00077 }
00078 
00079 //*****************************************************************************
00080 //***    CPrintAttachment::LAttachment
00081 //*****************************************************************************
00082 
00083 void CPrintAttachment::SetOwnerHost(LAttachable* inOwnerHost)
00084 {
00085     LAttachment::SetOwnerHost(inOwnerHost);
00086     
00087        if (mBrowserShell == nil) {
00088               if (mBrowserShellPaneID != PaneIDT_Unspecified) {
00089                      LView* container = GetTopmostView(dynamic_cast<LPane*>(mOwnerHost));
00090                      if (container != nil) {
00091                             LPane* targetPane = container->FindPaneByID(mBrowserShellPaneID);
00092                             if (targetPane != nil)
00093                                    mBrowserShell = dynamic_cast<CBrowserShell*>(targetPane);
00094                      }
00095               }
00096               else
00097                      mBrowserShell = dynamic_cast<CBrowserShell*>(mOwnerHost);
00098 
00099            Assert_(mBrowserShell != nil);        // Programmer error
00100        }
00101 }
00102 
00103 void CPrintAttachment::ExecuteSelf(MessageT inMessage, void *ioParam)
00104 {
00105        mExecuteHost = true;
00106 
00107        if (inMessage == msg_CommandStatus) {
00108               SCommandStatus       *status = (SCommandStatus *)ioParam;
00109               if (status->command == cmd_Print) {
00110                      *status->enabled = true;
00111                      *status->usesMark = false;
00112                      mExecuteHost = false; // we handled it
00113               }
00114               else if (status->command == cmd_PageSetup) {
00115                      *status->enabled = true;
00116                      *status->usesMark = false;
00117                      mExecuteHost = false; // we handled it
00118               }
00119        }
00120        else if (inMessage == cmd_Print) {
00121            DoPrint();
00122            mExecuteHost = false; // we handled it
00123        }
00124        else if (inMessage == cmd_PageSetup) {
00125            DoPageSetup();
00126            mExecuteHost = false; // we handled it
00127        }
00128 }
00129 
00130 //*****************************************************************************
00131 //***    CPrintAttachment::CPrintAttachment
00132 //*****************************************************************************
00133 
00134 void CPrintAttachment::DoPrint()
00135 {
00136     nsCOMPtr<nsIWebBrowser> wb;
00137     mBrowserShell->GetWebBrowser(getter_AddRefs(wb));
00138     ThrowIfNil_(wb);
00139     nsCOMPtr<nsIWebBrowserPrint> wbPrint(do_GetInterface(wb));
00140     ThrowIfNil_(wbPrint);
00141     nsCOMPtr<nsIPrintSettings> settings;
00142     mBrowserShell->GetPrintSettings(getter_AddRefs(settings));
00143     ThrowIfNil_(settings);
00144        
00145     nsresult rv = wbPrint->Print(settings, nsnull);
00146     if (rv != NS_ERROR_ABORT)
00147         ThrowIfError_(rv);
00148 }
00149 
00150 
00151 void CPrintAttachment::DoPageSetup()
00152 {
00153     nsCOMPtr<nsIWebBrowser> wb;
00154     mBrowserShell->GetWebBrowser(getter_AddRefs(wb));
00155     ThrowIfNil_(wb);
00156     nsCOMPtr<nsIDOMWindow> domWindow;
00157     wb->GetContentDOMWindow(getter_AddRefs(domWindow));
00158     ThrowIfNil_(domWindow);
00159     nsCOMPtr<nsIPrintSettings> settings;
00160     mBrowserShell->GetPrintSettings(getter_AddRefs(settings));
00161     ThrowIfNil_(settings);
00162     
00163     nsCOMPtr<nsIPrintingPromptService> printingPromptService =
00164              do_GetService("@mozilla.org/embedcomp/printingprompt-service;1");
00165     ThrowIfNil_(printingPromptService);
00166 
00167     nsresult rv = printingPromptService->ShowPageSetup(domWindow, settings, nsnull);
00168     if (rv != NS_ERROR_ABORT)
00169         ThrowIfError_(rv);
00170 }
00171 
00172 
00173 LView* CPrintAttachment::GetTopmostView(LPane*   inStartPane)
00174 {
00175                                                                       // Begin with the start Pane as a
00176                                                                       //   View. Will be nil if start Pane
00177                                                                       //   is nil or is not an LView.
00178        LView* theView = dynamic_cast<LView*>(inStartPane);
00179        
00180        if (inStartPane != nil) {
00181                                                                       // Look at SuperView of start Pane
00182               LView* superView = inStartPane->GetSuperView();
00183               
00184               while (superView != nil) {         // Move up view hierarchy until
00185                      theView = superView;        //   reaching a nil SuperView
00186                      superView = theView->GetSuperView();
00187               }
00188        }
00189        
00190        return theView;
00191 }
00192