Back to index

lightning-sunbird  0.9+nobinonly
nsDeviceContextSpecPh.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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsDeviceContextSpecPh.h"
00039 #include "nsPrintOptionsPh.h"
00040 #include "prmem.h"
00041 #include "plstr.h"
00042 #include "nsPhGfxLog.h"
00043 
00044 #include "nsGfxCIID.h"
00045 #include "nsIPrintOptions.h"
00046 #include "nsIDOMWindow.h"
00047 #include "nsIDialogParamBlock.h"
00048 #include "nsISupportsPrimitives.h"
00049 #include "nsIWindowWatcher.h"
00050 #include "nsIDOMWindowInternal.h"
00051 #include "nsVoidArray.h"
00052 #include "nsSupportsArray.h"
00053 
00054 #include "nsString.h"
00055 #include "nsIServiceManager.h"
00056 #include "nsReadableUtils.h"
00057 #include "nsIPref.h"
00058 
00059 static NS_DEFINE_CID(kPrefCID, NS_PREF_CID);
00060 
00061 nsDeviceContextSpecPh :: nsDeviceContextSpecPh()
00062 {
00063        mPC = PpCreatePC();
00064 }
00065 
00066 nsDeviceContextSpecPh :: ~nsDeviceContextSpecPh()
00067 {
00068        PpPrintReleasePC(mPC);
00069 }
00070 
00071 NS_IMPL_ISUPPORTS1(nsDeviceContextSpecPh, nsIDeviceContextSpec)
00072 
00073 
00074 #define Pp_COLOR_CMYK                     4
00075 #define Pp_COLOR_BW                       1
00076 
00077 NS_IMETHODIMP nsDeviceContextSpecPh :: Init(nsIWidget* aWidget,
00078                                              nsIPrintSettings* aPS,
00079                                              PRBool aQuiet)
00080 {
00081        nsresult rv = NS_OK;
00082        PRUnichar *printer        = nsnull;
00083        PRBool silent;
00084 
00085        aPS->GetPrinterName(&printer);
00086        aPS->GetPrintSilent( &silent );
00087 
00088        if( printer ) {
00089               int res = 111;
00090               NS_ConvertUCS2toUTF8 pname(printer);
00091               if( !strcmp( pname.get(), "<Preview>" ) ) {
00092                      char preview = 1;
00093                      PpSetPC( mPC, Pp_PC_DO_PREVIEW, &preview, 0 );
00094                      }
00095               else res = PpLoadPrinter( mPC, pname.get() );
00096               }
00097        else PpLoadDefaultPrinter( mPC );
00098 
00099        if( !silent ) 
00100        {
00101               PRBool tofile = PR_FALSE;
00102               PRUnichar *printfile = nsnull;
00103               PRInt32 copies = 1;
00104               PRBool color = PR_FALSE;
00105               PRInt32 orientation = nsIPrintSettings::kPortraitOrientation;
00106               PRBool reversed = PR_FALSE;
00107 
00108               aPS->GetPrintToFile(&tofile);
00109               if( tofile == PR_TRUE ) {
00110                      aPS->GetToFileName(&printfile);
00111                      if( printfile ) PpSetPC( mPC, Pp_PC_FILENAME, NS_ConvertUCS2toUTF8(printfile).get(), 0 );
00112                      }
00113 
00114               aPS->GetNumCopies(&copies);
00115               char pcopies = ( char ) copies;
00116               PpSetPC( mPC, Pp_PC_COPIES, (void *) &pcopies, 0 );
00117 
00118               aPS->GetPrintInColor(&color);
00119               char ink = color == PR_TRUE ? Pp_COLOR_CMYK : Pp_COLOR_BW;
00120               PpSetPC( mPC, Pp_PC_INKTYPE, &ink, 0 );
00121 
00122               aPS->GetOrientation(&orientation);
00123               char paper_orientation = orientation == nsIPrintSettings::kPortraitOrientation ? 0 : 1;
00124               PpSetPC( mPC, Pp_PC_ORIENTATION, &paper_orientation, 0 );
00125 
00126               aPS->GetPrintReversed(&reversed);
00127               char rev = reversed == PR_TRUE ? 1 : 0;
00128               PpSetPC( mPC, Pp_PC_REVERSED, &rev, 0 );
00129 
00130               double margin_top, margin_left, margin_right, margin_bottom;
00131               PhRect_t rmargin = { { 0, 0 }, { 0, 0 } };
00132               aPS->GetMarginTop( &margin_top );
00133               aPS->GetMarginLeft( &margin_left );
00134               aPS->GetMarginRight( &margin_right );
00135               aPS->GetMarginBottom( &margin_bottom );
00136 
00137 
00138               PRInt16 unit;
00139               double width, height;
00140               aPS->GetPaperSizeUnit(&unit);
00141               aPS->GetPaperWidth(&width);
00142               aPS->GetPaperHeight(&height);
00143 
00144               PhDim_t *pdim, dim;
00145               PpGetPC( mPC, Pp_PC_PAPER_SIZE, &pdim );
00146               dim = *pdim;
00147               if( unit == nsIPrintSettings::kPaperSizeInches ) {
00148                 dim.w  = width * 1000;
00149                 dim.h = height * 1000;
00150 
00151                      rmargin.ul.x = margin_left * 1000;
00152                      rmargin.ul.y = margin_top * 1000;
00153                      rmargin.lr.x = margin_right * 1000;
00154                      rmargin.lr.y = margin_bottom * 1000;
00155                      }
00156               else if( unit == nsIPrintSettings::kPaperSizeMillimeters ) {
00157                      dim.w = short(NS_TWIPS_TO_INCHES(NS_MILLIMETERS_TO_TWIPS(float(width*1000))));
00158                      dim.h = short(NS_TWIPS_TO_INCHES(NS_MILLIMETERS_TO_TWIPS(float(height*1000))));
00159 
00160                      rmargin.ul.x = short(NS_TWIPS_TO_INCHES(NS_MILLIMETERS_TO_TWIPS(float(margin_left*1000))));
00161                      rmargin.ul.y = short(NS_TWIPS_TO_INCHES(NS_MILLIMETERS_TO_TWIPS(float(margin_top*1000))));
00162                      rmargin.lr.x = short(NS_TWIPS_TO_INCHES(NS_MILLIMETERS_TO_TWIPS(float(margin_right*1000))));
00163                      rmargin.lr.y = short(NS_TWIPS_TO_INCHES(NS_MILLIMETERS_TO_TWIPS(float(margin_bottom*1000))));
00164                      }
00165 
00166               PpSetPC( mPC, Pp_PC_PAPER_SIZE, &dim, 0 );
00167               PpSetPC( mPC, Pp_PC_NONPRINT_MARGINS, &rmargin, 0 );
00168   }
00169        else { /* silent is set - used when the call is comming from the embedded version */
00170               PRInt32 p;
00171               aPS->GetEndPageRange( &p );
00172               PpPrintReleasePC(mPC);
00173               mPC = ( PpPrintContext_t *) p;
00174 
00175               /* set the print frame / BG colors and images settings, according to the Pt_ARG_WEB_OPTION setting */
00176               nsresult res;
00177               nsCOMPtr<nsIPref> prefs(do_GetService(kPrefCID, &res));
00178 
00179     PRInt16 howToEnableFrameUI = nsIPrintSettings::kFrameEnableNone;
00180     aPS->GetHowToEnableFrameUI(&howToEnableFrameUI);
00181 
00182     if( howToEnableFrameUI == nsIPrintSettings::kFrameEnableAll ||
00183         howToEnableFrameUI == nsIPrintSettings::kFrameEnableAsIsAndEach )
00184       {
00185       /* we have frames and we have a selected frame already only if kFrameEnableAll */
00186       /* look at the Pt_ARG_WEB_OPTION related to this */
00187 
00188       char *printFrame = NULL;
00189                      if( prefs ) prefs->CopyCharPref( "user.print.print_frame", &printFrame );
00190 
00191                      if( printFrame ) {
00192                             if( !stricmp( printFrame, "print_frame_as_is" ) ) 
00193            aPS->SetPrintFrameType(nsIPrintSettings::kFramesAsIs);
00194                             else if( !stricmp( printFrame, "print_frame_all" ) ) 
00195            aPS->SetPrintFrameType(nsIPrintSettings::kEachFrameSep);
00196                             else if( !stricmp( printFrame, "print_frame_selected" ) )  {
00197           if( howToEnableFrameUI == nsIPrintSettings::kFrameEnableAll )
00198             aPS->SetPrintFrameType(nsIPrintSettings::kSelectedFrame);
00199           else /* if no frame is selected, use the kFramesAsIs */
00200             aPS->SetPrintFrameType(nsIPrintSettings::kFramesAsIs);
00201                                           }
00202               }
00203               }
00204 
00205               char *SetPrintBGColors = NULL, *SetPrintBGImages = NULL;
00206               if( prefs ) prefs->CopyCharPref( "user.print.SetPrintBGColors", &SetPrintBGColors );
00207               if( prefs ) prefs->CopyCharPref( "user.print.SetPrintBGImages", &SetPrintBGImages );
00208 
00209        if( SetPrintBGColors && !stricmp( SetPrintBGColors, "true" ) )
00210                      aPS->SetPrintBGColors( PR_TRUE );
00211               else aPS->SetPrintBGColors( PR_FALSE );
00212 
00213        if( SetPrintBGImages && !stricmp( SetPrintBGImages, "true" ) )
00214          aPS->SetPrintBGImages( PR_TRUE );
00215        else aPS->SetPrintBGImages( PR_FALSE );
00216               }
00217 
00218        return rv;
00219 }
00220 
00221 //NS_IMETHODIMP nsDeviceContextSpecPh :: GetPrintContext(PpPrintContext_t *&aPrintContext) const
00222 PpPrintContext_t *nsDeviceContextSpecPh :: GetPrintContext()
00223 {
00224        return (mPC);
00225 }
00226 
00227 //***********************************************************
00228 //  Printer Enumerator
00229 //***********************************************************
00230 nsPrinterEnumeratorPh::nsPrinterEnumeratorPh()
00231 {
00232 }
00233 
00234 nsPrinterEnumeratorPh::~nsPrinterEnumeratorPh()
00235 {
00236 }
00237 
00238 NS_IMPL_ISUPPORTS1(nsPrinterEnumeratorPh, nsIPrinterEnumerator)
00239 
00240 //----------------------------------------------------------------------------------
00241 // Enumerate all the Printers from the global array and pass their
00242 // names back (usually to script)
00243 NS_IMETHODIMP
00244 nsPrinterEnumeratorPh::EnumeratePrinters(PRUint32* aCount, PRUnichar*** aResult)
00245 {
00246        return DoEnumeratePrinters(PR_FALSE, aCount, aResult);
00247 }
00248 
00249 /* readonly attribute wstring defaultPrinterName; */
00250 NS_IMETHODIMP nsPrinterEnumeratorPh::GetDefaultPrinterName(PRUnichar * *aDefaultPrinterName)
00251 {
00252        char *printer;
00253 
00254   NS_ENSURE_ARG_POINTER(aDefaultPrinterName);
00255 
00256        *aDefaultPrinterName = nsnull;
00257 
00258        PpPrintContext_t *pc = PpCreatePC();
00259        if( pc ) {
00260               PpLoadDefaultPrinter( pc );
00261               PpGetPC( pc, Pp_PC_NAME, &printer );
00262        if( printer ) *aDefaultPrinterName = ToNewUnicode( NS_STATIC_CAST(const nsAFlatString&, NS_MULTILINE_LITERAL_STRING(printer)) );
00263               PpReleasePC( pc );
00264               }
00265 
00266   return NS_OK;
00267 }
00268 
00269 /* void initPrintSettingsFromPrinter (in wstring aPrinterName, in nsIPrintSettings aPrintSettings); */
00270 NS_IMETHODIMP nsPrinterEnumeratorPh::InitPrintSettingsFromPrinter(const PRUnichar *aPrinterName, nsIPrintSettings *aPrintSettings)
00271 {
00272     return NS_OK;
00273 }
00274 
00275 //----------------------------------------------------------------------------------
00276 // Display the AdvancedDocumentProperties for the selected Printer
00277 NS_IMETHODIMP nsPrinterEnumeratorPh::DisplayPropertiesDlg(const PRUnichar *aPrinterName, nsIPrintSettings* aPrintSettings)
00278 {
00279        nsresult rv = NS_ERROR_FAILURE;
00280        return rv;
00281 }
00282 
00283 static void CleanupArray(PRUnichar**& aArray, PRInt32& aCount)
00284 {
00285        for (PRInt32 i = aCount - 1; i >= 0; i--) 
00286        {
00287               nsMemory::Free(aArray[i]);
00288        }
00289        nsMemory::Free(aArray);
00290        aArray = NULL;
00291        aCount = 0;
00292 }
00293 
00294 
00295 nsresult
00296 nsPrinterEnumeratorPh::DoEnumeratePrinters(PRBool aDoExtended, PRUint32* aCount,
00297                                            PRUnichar*** aResult)
00298 {
00299        NS_ENSURE_ARG(aCount);
00300        NS_ENSURE_ARG_POINTER(aResult);
00301 
00302        char   **plist = NULL;
00303        int           pcount = 0, count = 0;
00304 
00305        if (!(plist = PpLoadPrinterList()))
00306               return NS_ERROR_FAILURE;
00307 
00308        for (pcount = 0; plist[pcount] != NULL; pcount++);
00309 
00310        /* allow a fake <Preview> printer to do the photon native preview */
00311        pcount++;
00312 
00313        PRUnichar** array = (PRUnichar**) nsMemory::Alloc(pcount * sizeof(PRUnichar*));
00314        if (!array)
00315        {
00316               PpFreePrinterList(plist);
00317               return NS_ERROR_OUT_OF_MEMORY;
00318        }
00319 
00320        while (count < pcount) 
00321        {
00322               nsString newName;
00323 
00324               if( count < pcount-1 )
00325                      newName.AssignWithConversion(plist[count]);
00326               else newName.AssignWithConversion( "<Preview>" );
00327 
00328               PRUnichar *str = ToNewUnicode(newName);
00329               if (!str) 
00330               {
00331                      CleanupArray(array, count);
00332                      PpFreePrinterList(plist);
00333                      return NS_ERROR_OUT_OF_MEMORY;
00334               }
00335               array[count++] = str;
00336        }
00337 
00338        *aCount  = count;
00339        *aResult = array;
00340 
00341        return NS_OK;
00342 }