Back to index

lightning-sunbird  0.9+nobinonly
nsDeviceContextSpecX.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Patrick C. Beard <beard@netscape.com>
00024  *   Simon Fraser     <sfraser@netscape.com>
00025  *   Conrad Carlen    <ccarlen@netscape.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsDeviceContextSpecX.h"
00042 
00043 #include "prmem.h"
00044 #include "plstr.h"
00045 #include "nsCRT.h"
00046 
00047 #include "nsIServiceManager.h"
00048 #include "nsIPrintOptions.h"
00049 #include "nsIPrintSettingsX.h"
00050 
00055 nsDeviceContextSpecX::nsDeviceContextSpecX()
00056 : mPrintSession(0)
00057 , mPageFormat(kPMNoPageFormat)
00058 , mPrintSettings(kPMNoPrintSettings)
00059 , mSavedPort(0)
00060 , mBeganPrinting(PR_FALSE)
00061 {
00062 }
00063 
00068 nsDeviceContextSpecX::~nsDeviceContextSpecX()
00069 {
00070   ClosePrintManager();
00071 }
00072 
00073 NS_IMPL_ISUPPORTS2(nsDeviceContextSpecX, nsIDeviceContextSpec, nsIPrintingContext)
00074 
00075 
00079 NS_IMETHODIMP nsDeviceContextSpecX::Init(nsIPrintSettings* aPS, PRBool       aIsPrintPreview)
00080 {
00081   nsresult rv;
00082     
00083   nsCOMPtr<nsIPrintSettingsX> printSettingsX(do_QueryInterface(aPS));
00084   if (!printSettingsX)
00085     return NS_ERROR_NO_INTERFACE;
00086   
00087   rv = printSettingsX->GetNativePrintSession(&mPrintSession);
00088   if (NS_FAILED(rv))
00089     return rv;  
00090   rv = printSettingsX->GetPMPageFormat(&mPageFormat);
00091   if (NS_FAILED(rv))
00092     return rv;
00093   rv = printSettingsX->GetPMPrintSettings(&mPrintSettings);
00094   if (NS_FAILED(rv))
00095     return rv;
00096 
00097   return NS_OK;
00098 }
00099 
00100 NS_IMETHODIMP nsDeviceContextSpecX::PrintManagerOpen(PRBool* aIsOpen)
00101 {
00102     *aIsOpen = mBeganPrinting;
00103     return NS_OK;
00104 }
00105 
00110 NS_IMETHODIMP nsDeviceContextSpecX::ClosePrintManager()
00111 {
00112        return NS_OK;
00113 }  
00114 
00115 NS_IMETHODIMP nsDeviceContextSpecX::BeginDocument(PRUnichar*  aTitle, 
00116                                                   PRUnichar*  aPrintToFileName,
00117                                                   PRInt32     aStartPage, 
00118                                                   PRInt32     aEndPage)
00119 {
00120     if (aTitle) {
00121       CFStringRef cfString = ::CFStringCreateWithCharacters(NULL, aTitle, nsCRT::strlen(aTitle));
00122       if (cfString) {
00123         ::PMSetJobNameCFString(mPrintSettings, cfString);
00124         ::CFRelease(cfString);
00125       }
00126     }
00127 
00128     OSStatus status;
00129     status = ::PMSetFirstPage(mPrintSettings, aStartPage, false);
00130     NS_ASSERTION(status == noErr, "PMSetFirstPage failed");
00131     status = ::PMSetLastPage(mPrintSettings, aEndPage, false);
00132     NS_ASSERTION(status == noErr, "PMSetLastPage failed");
00133 
00134     status = ::PMSessionBeginDocument(mPrintSession, mPrintSettings, mPageFormat);
00135     if (status != noErr) return NS_ERROR_ABORT;
00136     
00137     return NS_OK;
00138 }
00139 
00140 NS_IMETHODIMP nsDeviceContextSpecX::EndDocument()
00141 {
00142     ::PMSessionEndDocument(mPrintSession);
00143     return NS_OK;
00144 }
00145 
00146 NS_IMETHODIMP nsDeviceContextSpecX::AbortDocument()
00147 {
00148     return EndDocument();
00149 }
00150 
00151 NS_IMETHODIMP nsDeviceContextSpecX::BeginPage()
00152 {
00153     OSStatus status = ::PMSessionBeginPage(mPrintSession, mPageFormat, NULL);
00154     if (status != noErr) return NS_ERROR_ABORT;
00155     
00156     ::GetPort(&mSavedPort);
00157     void *graphicsContext;
00158     status = ::PMSessionGetGraphicsContext(mPrintSession, kPMGraphicsContextQuickdraw, &graphicsContext);
00159     if (status != noErr)
00160       return NS_ERROR_ABORT;
00161     ::SetPort((CGrafPtr)graphicsContext);
00162     return NS_OK;
00163 }
00164 
00165 NS_IMETHODIMP nsDeviceContextSpecX::EndPage()
00166 {
00167     OSStatus status = ::PMSessionEndPage(mPrintSession);
00168     if (mSavedPort)
00169     {
00170         ::SetPort(mSavedPort);
00171         mSavedPort = 0;
00172     }
00173     if (status != noErr)
00174       return NS_ERROR_ABORT;
00175     return NS_OK;
00176 }
00177 
00178 NS_IMETHODIMP nsDeviceContextSpecX::GetPrinterResolution(double* aResolution)
00179 {
00180     PMPrinter printer;
00181     OSStatus status = ::PMSessionGetCurrentPrinter(mPrintSession, &printer);
00182     if (status != noErr)
00183       return NS_ERROR_FAILURE;
00184       
00185     PMResolution defaultResolution;
00186     status = ::PMPrinterGetPrinterResolution(printer, kPMDefaultResolution, &defaultResolution);
00187     if (status != noErr)
00188       return NS_ERROR_FAILURE;
00189     
00190     *aResolution = defaultResolution.hRes;
00191     return NS_OK;
00192 }
00193 
00194 NS_IMETHODIMP nsDeviceContextSpecX::GetPageRect(double* aTop, double* aLeft, double* aBottom, double* aRight)
00195 {
00196     PMRect pageRect;
00197     ::PMGetAdjustedPageRect(mPageFormat, &pageRect);
00198     *aTop = pageRect.top, *aLeft = pageRect.left;
00199     *aBottom = pageRect.bottom, *aRight = pageRect.right;
00200     return NS_OK;
00201 }