Back to index

lightning-sunbird  0.9+nobinonly
nsDeviceContextXP.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  *   Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
00024  *   Bradley Baetz <bbaetz@cs.mcgill.ca>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifdef MOZ_LOGGING
00041 #define FORCE_PR_LOG 1 /* Allow logging in the release build */
00042 #endif /* MOZ_LOGGING */
00043 #include "prlog.h"
00044  
00045 #include "nsDeviceContextXP.h"
00046 #include "nsRenderingContextXp.h"
00047 #include "nsFontMetricsXlib.h"
00048 #include "nsIDeviceContext.h"
00049 #include "nsIDeviceContextSpecXPrint.h"
00050 #include "nspr.h"
00051 #include "nsXPrintContext.h"
00052 
00053 #ifdef PR_LOGGING
00054 static PRLogModuleInfo *nsDeviceContextXpLM = PR_NewLogModule("nsDeviceContextXp");
00055 #endif /* PR_LOGGING */
00056 
00060 nsDeviceContextXp :: nsDeviceContextXp()
00061   : nsDeviceContextX(),
00062     mPrintContext(nsnull),
00063     mSpec(nsnull),
00064     mParentDeviceContext(nsnull),
00065     mFontMetricsContext(nsnull),
00066     mRCContext(nsnull)
00067 {
00068 }
00069 
00074 nsDeviceContextXp :: ~nsDeviceContextXp() 
00075 { 
00076   DestroyXPContext();
00077 }
00078 
00079 
00080 NS_IMETHODIMP
00081 nsDeviceContextXp::SetSpec(nsIDeviceContextSpec* aSpec)
00082 {
00083   nsresult  rv = NS_ERROR_FAILURE;
00084   PR_LOG(nsDeviceContextXpLM, PR_LOG_DEBUG, ("nsDeviceContextXp::SetSpec()\n"));
00085 
00086   nsCOMPtr<nsIDeviceContextSpecXp> xpSpec;
00087 
00088   mSpec = aSpec;
00089 
00090   if (mPrintContext)
00091     DestroyXPContext(); // we cannot reuse that...
00092     
00093   mPrintContext = new nsXPrintContext();
00094   if (!mPrintContext)
00095     return  NS_ERROR_OUT_OF_MEMORY;
00096     
00097   xpSpec = do_QueryInterface(mSpec, &rv);
00098   if (NS_SUCCEEDED(rv)) {
00099     rv = mPrintContext->Init(this, xpSpec);
00100 
00101     if (NS_FAILED(rv)) {
00102       DestroyXPContext();
00103     }
00104   }
00105  
00106   return rv;
00107 }
00108 
00109 NS_IMPL_ISUPPORTS_INHERITED1(nsDeviceContextXp,
00110                              DeviceContextImpl,
00111                              nsIDeviceContextXp)
00112 
00113 
00118 NS_IMETHODIMP
00119 nsDeviceContextXp::InitDeviceContextXP(nsIDeviceContext *aCreatingDeviceContext,nsIDeviceContext *aParentContext)
00120 {
00121   nsresult rv;
00122 
00123   // Initialization moved to SetSpec to be done after creating the Print Context
00124   float origscale, newscale;
00125   float t2d, a2d;
00126   int   print_x_resolution,
00127         print_y_resolution;
00128 
00129   mPrintContext->GetPrintResolution(print_x_resolution, print_y_resolution);
00130   
00131   if (print_x_resolution != print_y_resolution) {
00132     PR_LOG(nsDeviceContextXpLM, PR_LOG_DEBUG, ("print_x_resolution != print_y_resolution not yet supported by Mozilla's layout engine\n"));
00133     return NS_ERROR_NOT_IMPLEMENTED; /* this error code should be more detailed */
00134   }
00135 
00136   mPixelsToTwips = (float)NSIntPointsToTwips(72) / (float)print_x_resolution;
00137   mTwipsToPixels = 1.0f / mPixelsToTwips;
00138 
00139   newscale = TwipsToDevUnits();
00140   origscale = aParentContext->TwipsToDevUnits();
00141 
00142   mCPixelScale = newscale / origscale;
00143 
00144   t2d = aParentContext->TwipsToDevUnits();
00145   a2d = aParentContext->AppUnitsToDevUnits();
00146 
00147   mAppUnitsToDevUnits = (a2d / t2d) * mTwipsToPixels;
00148   mDevUnitsToAppUnits = 1.0f / mAppUnitsToDevUnits;
00149 
00150   NS_ASSERTION(aParentContext, "aCreatingDeviceContext cannot be NULL!!!");
00151   mParentDeviceContext = aParentContext;
00152 
00153   /* be sure we've cleaned-up old rubbish - new values will re-populate nsFontMetricsXlib soon... */
00154   DeleteRenderingContextXlibContext(mRCContext);
00155   DeleteFontMetricsXlibContext(mFontMetricsContext);
00156   mRCContext          = nsnull;
00157   mFontMetricsContext = nsnull;
00158  
00159   rv = CreateFontMetricsXlibContext(this, PR_TRUE, &mFontMetricsContext);
00160   if (NS_FAILED(rv))
00161     return rv;
00162 
00163   rv = CreateRenderingContextXlibContext(this, &mRCContext);
00164   if (NS_FAILED(rv))
00165     return rv;
00166    
00167   return NS_OK;
00168 }
00169 
00172 NS_IMETHODIMP nsDeviceContextXp :: CreateRenderingContext(nsIRenderingContext *&aContext)
00173 {
00174   nsresult rv;
00175    
00176   aContext = nsnull;
00177 
00178   NS_ENSURE_TRUE(mPrintContext != nsnull, NS_ERROR_NULL_POINTER);
00179 
00180   nsCOMPtr<nsRenderingContextXp> renderingContext = new nsRenderingContextXp();
00181   if (!renderingContext)
00182     return NS_ERROR_OUT_OF_MEMORY;
00183      
00184   rv = renderingContext->Init(this);
00185 
00186   if (NS_SUCCEEDED(rv)) {
00187     aContext = renderingContext;
00188     NS_ADDREF(aContext);
00189   }
00190 
00191   return rv;
00192 }
00193 
00194 NS_IMETHODIMP nsDeviceContextXp::CreateRenderingContextInstance(nsIRenderingContext *&aContext)
00195 {
00196   nsCOMPtr<nsIRenderingContext> renderingContext = new nsRenderingContextXp();
00197   if (!renderingContext)
00198     return NS_ERROR_OUT_OF_MEMORY;
00199          
00200   aContext = renderingContext;
00201   NS_ADDREF(aContext);
00202   
00203   return NS_OK;
00204 }
00205 
00209 NS_IMETHODIMP nsDeviceContextXp :: SupportsNativeWidgets(PRBool &aSupportsWidgets)
00210 {
00211   aSupportsWidgets = PR_FALSE;
00212   return NS_OK;
00213 }
00214 
00218 NS_IMETHODIMP nsDeviceContextXp :: GetScrollBarDimensions(float &aWidth, 
00219                                         float &aHeight) const
00220 {
00221   // XXX Oh, yeah.  These are hard coded.
00222   float scale;
00223   GetCanonicalPixelScale(scale);
00224   aWidth  = 15.f * mPixelsToTwips * scale;
00225   aHeight = 15.f * mPixelsToTwips * scale;
00226 
00227   return NS_OK;
00228 }
00229 
00234 NS_IMETHODIMP nsDeviceContextXp :: CheckFontExistence(const nsString& aFontName)
00235 {
00236   return nsFontMetricsXlib::FamilyExists(mFontMetricsContext, aFontName);
00237 }
00238 
00239 NS_IMETHODIMP nsDeviceContextXp :: GetSystemFont(nsSystemFontID aID, 
00240                                                  nsFont *aFont) const
00241 {
00242   if (mParentDeviceContext != nsnull) {
00243     return mParentDeviceContext->GetSystemFont(aID, aFont);
00244   }
00245   return NS_ERROR_FAILURE;
00246 }
00247 
00251 NS_IMETHODIMP nsDeviceContextXp::GetDeviceSurfaceDimensions(PRInt32 &aWidth, 
00252                                                         PRInt32 &aHeight)
00253 {
00254   NS_ENSURE_TRUE(mPrintContext != nsnull, NS_ERROR_NULL_POINTER);
00255 
00256   float width, height;
00257   width  = (float) mPrintContext->GetWidth();
00258   height = (float) mPrintContext->GetHeight();
00259 
00260   aWidth  = NSToIntRound(width  * mDevUnitsToAppUnits);
00261   aHeight = NSToIntRound(height * mDevUnitsToAppUnits);
00262 
00263   return NS_OK;
00264 }
00265 
00266 NS_IMETHODIMP nsDeviceContextXp::GetRect(nsRect &aRect)
00267 {
00268   NS_ENSURE_TRUE(mPrintContext != nsnull, NS_ERROR_NULL_POINTER);
00269 
00270   PRInt32 width, height;
00271   nsresult rv;
00272   rv = GetDeviceSurfaceDimensions(width, height);
00273   aRect.x = 0;
00274   aRect.y = 0;
00275   aRect.width = width;
00276   aRect.height = height;
00277   return rv;
00278 }
00279 
00283 NS_IMETHODIMP nsDeviceContextXp::GetClientRect(nsRect &aRect)
00284 {
00285   return GetRect(aRect);
00286 }
00287 
00291 NS_IMETHODIMP nsDeviceContextXp::GetDeviceContextFor(nsIDeviceContextSpec *aDevice, nsIDeviceContext *&aContext)
00292 {
00293   aContext = nsnull;
00294   return NS_OK;
00295 }
00296 
00300 NS_IMETHODIMP nsDeviceContextXp::BeginDocument(PRUnichar * aTitle, PRUnichar* aPrintToFileName, PRInt32 aStartPage, PRInt32 aEndPage)
00301 {  
00302   PR_LOG(nsDeviceContextXpLM, PR_LOG_DEBUG, ("nsDeviceContextXp::BeginDocument()\n"));
00303 
00304   NS_ENSURE_TRUE(mPrintContext != nsnull, NS_ERROR_NULL_POINTER);
00305 
00306   return mPrintContext->BeginDocument(aTitle, aPrintToFileName, aStartPage, aEndPage);
00307 }
00308 
00309 
00310 void nsDeviceContextXp::DestroyXPContext()
00311 {
00312   PR_LOG(nsDeviceContextXpLM, PR_LOG_DEBUG, ("nsDeviceContextXp::DestroyXPContext()\n"));
00313 
00314   if (!mPrintContext)
00315     return;
00316 
00317   /* gisburn: mPrintContext cannot be reused between to print
00318    * tasks as the destination print server may be a different one
00319    * or the printer used on the same print server has other
00320    * properties (build-in fonts for example ) than the printer
00321    * previously used. */
00322   FlushFontCache();
00323   DeleteRenderingContextXlibContext(mRCContext);
00324   DeleteFontMetricsXlibContext(mFontMetricsContext);
00325   mRCContext          = nsnull;
00326   mFontMetricsContext = nsnull;
00327 
00328   mPrintContext = nsnull; // nsCOMPtr will call |delete mPrintContext;|
00329 }
00330 
00334 NS_IMETHODIMP nsDeviceContextXp::EndDocument(void)
00335 {
00336   PR_LOG(nsDeviceContextXpLM, PR_LOG_DEBUG, ("nsDeviceContextXp::EndDocument()\n"));
00337 
00338   NS_ENSURE_TRUE(mPrintContext != nsnull, NS_ERROR_NULL_POINTER);
00339 
00340   nsresult rv = mPrintContext->EndDocument();
00341   DestroyXPContext();
00342   return rv;
00343 }
00344 
00348 NS_IMETHODIMP nsDeviceContextXp::AbortDocument(void)
00349 {
00350   PR_LOG(nsDeviceContextXpLM, PR_LOG_DEBUG, ("nsDeviceContextXp::AbortDocument()\n"));
00351 
00352   NS_ENSURE_TRUE(mPrintContext != nsnull, NS_ERROR_NULL_POINTER);
00353 
00354   nsresult rv = mPrintContext->AbortDocument();
00355   DestroyXPContext();
00356   return rv;
00357 }
00358 
00362 NS_IMETHODIMP nsDeviceContextXp::BeginPage(void)
00363 {
00364   NS_ENSURE_TRUE(mPrintContext != nsnull, NS_ERROR_NULL_POINTER);
00365 
00366   return mPrintContext->BeginPage();
00367 }
00368 
00373 NS_IMETHODIMP nsDeviceContextXp::EndPage(void)
00374 {
00375   NS_ENSURE_TRUE(mPrintContext != nsnull, NS_ERROR_NULL_POINTER);
00376 
00377   return mPrintContext->EndPage();
00378 }
00379 
00380 NS_IMETHODIMP
00381 nsDeviceContextXp::GetPrintContext(nsXPrintContext*& aContext)
00382 {
00383   aContext = mPrintContext;
00384   return NS_OK;
00385 }
00386 
00387 class nsFontCacheXp : public nsFontCache
00388 {
00389 public:
00390   /* override DeviceContextImpl::CreateFontCache() */
00391   virtual nsresult CreateFontMetricsInstance(nsIFontMetrics** aResult);
00392 };
00393 
00394 
00395 nsresult nsFontCacheXp::CreateFontMetricsInstance(nsIFontMetrics** aResult)
00396 {
00397   NS_PRECONDITION(aResult, "null out param");
00398   nsIFontMetrics *fm = new nsFontMetricsXlib();
00399   if (!fm)
00400     return NS_ERROR_OUT_OF_MEMORY;
00401   NS_ADDREF(fm);
00402   *aResult = fm;
00403   return NS_OK;
00404 }
00405 
00406 /* override DeviceContextImpl::CreateFontCache() */
00407 NS_IMETHODIMP nsDeviceContextXp::CreateFontCache()
00408 {
00409   mFontCache = new nsFontCacheXp();
00410   if (nsnull == mFontCache) {
00411     return NS_ERROR_OUT_OF_MEMORY;
00412   }
00413   return mFontCache->Init(this);
00414 }
00415 
00416