Back to index

lightning-sunbird  0.9+nobinonly
nsPrintSettingsWin.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 #include "nsPrintSettingsWin.h"
00038 #include "nsCRT.h"
00039 
00040 NS_IMPL_ISUPPORTS_INHERITED1(nsPrintSettingsWin, 
00041                              nsPrintSettings, 
00042                              nsIPrintSettingsWin)
00043 
00044 
00048 nsPrintSettingsWin::nsPrintSettingsWin() :
00049   nsPrintSettings(),
00050   mDeviceName(nsnull),
00051   mDriverName(nsnull),
00052   mDevMode(nsnull)
00053 {
00054 
00055 }
00056 
00061 nsPrintSettingsWin::nsPrintSettingsWin(const nsPrintSettingsWin& aPS) :
00062   mDeviceName(nsnull),
00063   mDriverName(nsnull),
00064   mDevMode(nsnull)
00065 {
00066   *this = aPS;
00067 }
00068 
00073 nsPrintSettingsWin::~nsPrintSettingsWin()
00074 {
00075   if (mDeviceName) nsMemory::Free(mDeviceName);
00076   if (mDriverName) nsMemory::Free(mDriverName);
00077   if (mDevMode) ::HeapFree(::GetProcessHeap(), 0, mDevMode);
00078 }
00079 
00080 /* [noscript] attribute charPtr deviceName; */
00081 NS_IMETHODIMP nsPrintSettingsWin::SetDeviceName(char * aDeviceName)
00082 {
00083   if (mDeviceName) {
00084     nsMemory::Free(mDeviceName);
00085   }
00086   mDeviceName = aDeviceName?nsCRT::strdup(aDeviceName):nsnull;
00087   return NS_OK;
00088 }
00089 NS_IMETHODIMP nsPrintSettingsWin::GetDeviceName(char * *aDeviceName)
00090 {
00091   NS_ENSURE_ARG_POINTER(aDeviceName);
00092   *aDeviceName = mDeviceName?nsCRT::strdup(mDeviceName):nsnull;
00093   return NS_OK;
00094 }
00095 
00096 /* [noscript] attribute charPtr driverName; */
00097 NS_IMETHODIMP nsPrintSettingsWin::SetDriverName(char * aDriverName)
00098 {
00099   if (mDriverName) {
00100     nsMemory::Free(mDriverName);
00101   }
00102   mDriverName = aDriverName?nsCRT::strdup(aDriverName):nsnull;
00103   return NS_OK;
00104 }
00105 NS_IMETHODIMP nsPrintSettingsWin::GetDriverName(char * *aDriverName)
00106 {
00107   NS_ENSURE_ARG_POINTER(aDriverName);
00108   *aDriverName = mDriverName?nsCRT::strdup(mDriverName):nsnull;
00109   return NS_OK;
00110 }
00111 
00112 void nsPrintSettingsWin::CopyDevMode(DEVMODE* aInDevMode, DEVMODE *& aOutDevMode)
00113 {
00114   aOutDevMode = nsnull;
00115   size_t size = aInDevMode->dmSize + aInDevMode->dmDriverExtra;
00116   aOutDevMode = (LPDEVMODE)::HeapAlloc (::GetProcessHeap(), HEAP_ZERO_MEMORY, size);
00117   if (aOutDevMode) {
00118     memcpy(aOutDevMode, aInDevMode, size);
00119   }
00120 
00121 }
00122 
00123 /* [noscript] attribute nsDevMode devMode; */
00124 NS_IMETHODIMP nsPrintSettingsWin::GetDevMode(DEVMODE * *aDevMode)
00125 {
00126   NS_ENSURE_ARG_POINTER(aDevMode);
00127 
00128   if (mDevMode) {
00129     CopyDevMode(mDevMode, *aDevMode);
00130   } else {
00131     *aDevMode = nsnull;
00132   }
00133   return NS_OK;
00134 }
00135 
00136 NS_IMETHODIMP nsPrintSettingsWin::SetDevMode(DEVMODE * aDevMode)
00137 {
00138   if (mDevMode) {
00139     ::HeapFree(::GetProcessHeap(), 0, mDevMode);
00140     mDevMode = NULL;
00141   }
00142 
00143   if (aDevMode) {
00144     CopyDevMode(aDevMode, mDevMode);
00145   }
00146   return NS_OK;
00147 }
00148 
00149 //-------------------------------------------
00150 nsresult 
00151 nsPrintSettingsWin::_Clone(nsIPrintSettings **_retval)
00152 {
00153   nsPrintSettingsWin* printSettings = new nsPrintSettingsWin(*this);
00154   return printSettings->QueryInterface(NS_GET_IID(nsIPrintSettings), (void**)_retval); // ref counts
00155 }
00156 
00157 //-------------------------------------------
00158 nsPrintSettingsWin& nsPrintSettingsWin::operator=(const nsPrintSettingsWin& rhs)
00159 {
00160   if (this == &rhs) {
00161     return *this;
00162   }
00163 
00164   ((nsPrintSettings&) *this) = rhs;
00165 
00166   if (mDeviceName) {
00167     nsCRT::free(mDeviceName);
00168   }
00169 
00170   if (mDriverName) {
00171     nsCRT::free(mDriverName);
00172   }
00173 
00174   // Use free because we used the native malloc to create the memory
00175   if (mDevMode) {
00176     ::HeapFree(::GetProcessHeap(), 0, mDevMode);
00177   }
00178 
00179   mDeviceName = rhs.mDeviceName?nsCRT::strdup(rhs.mDeviceName):nsnull;
00180   mDriverName = rhs.mDriverName?nsCRT::strdup(rhs.mDriverName):nsnull;
00181 
00182   if (rhs.mDevMode) {
00183     CopyDevMode(rhs.mDevMode, mDevMode);
00184   } else {
00185     mDevMode = nsnull;
00186   }
00187 
00188   return *this;
00189 }
00190 
00191 //-------------------------------------------
00192 /* void assign (in nsIPrintSettings aPS); */
00193 nsresult 
00194 nsPrintSettingsWin::_Assign(nsIPrintSettings *aPS)
00195 {
00196   nsPrintSettingsWin *psWin = NS_STATIC_CAST(nsPrintSettingsWin*, aPS);
00197   *this = *psWin;
00198   return NS_OK;
00199 }
00200 
00201 //----------------------------------------------------------------------
00202 // Testing of assign and clone
00203 // This define turns on the testing module below
00204 // so at start up it writes and reads the prefs.
00205 #ifdef DEBUG_rodsX
00206 #include "nsIPrintOptions.h"
00207 #include "nsIServiceManager.h"
00208 class Tester {
00209 public:
00210   Tester();
00211 };
00212 Tester::Tester()
00213 {
00214   nsCOMPtr<nsIPrintSettings> ps;
00215   nsresult rv;
00216   nsCOMPtr<nsIPrintOptions> printService = do_GetService("@mozilla.org/gfx/printsettings-service;1", &rv);
00217   if (NS_SUCCEEDED(rv)) {
00218     rv = printService->CreatePrintSettings(getter_AddRefs(ps));
00219   }
00220 
00221   if (ps) {
00222     ps->SetPrintOptions(nsIPrintSettings::kPrintOddPages,  PR_TRUE);
00223     ps->SetPrintOptions(nsIPrintSettings::kPrintEvenPages,  PR_FALSE);
00224     ps->SetMarginTop(1.0);
00225     ps->SetMarginLeft(1.0);
00226     ps->SetMarginBottom(1.0);
00227     ps->SetMarginRight(1.0);
00228     ps->SetScaling(0.5);
00229     ps->SetPrintBGColors(PR_TRUE);
00230     ps->SetPrintBGImages(PR_TRUE);
00231     ps->SetPrintRange(15);
00232     ps->SetHeaderStrLeft(NS_ConvertUTF8toUCS2("Left").get());
00233     ps->SetHeaderStrCenter(NS_ConvertUTF8toUCS2("Center").get());
00234     ps->SetHeaderStrRight(NS_ConvertUTF8toUCS2("Right").get());
00235     ps->SetFooterStrLeft(NS_ConvertUTF8toUCS2("Left").get());
00236     ps->SetFooterStrCenter(NS_ConvertUTF8toUCS2("Center").get());
00237     ps->SetFooterStrRight(NS_ConvertUTF8toUCS2("Right").get());
00238     ps->SetPaperName(NS_ConvertUTF8toUCS2("Paper Name").get());
00239     ps->SetPaperSizeType(10);
00240     ps->SetPaperData(1);
00241     ps->SetPaperWidth(100.0);
00242     ps->SetPaperHeight(50.0);
00243     ps->SetPaperSizeUnit(nsIPrintSettings::kPaperSizeMillimeters);
00244     ps->SetPrintReversed(PR_TRUE);
00245     ps->SetPrintInColor(PR_TRUE);
00246     ps->SetPaperSize(5);
00247     ps->SetOrientation(nsIPrintSettings::kLandscapeOrientation);
00248     ps->SetPrintCommand(NS_ConvertUTF8toUCS2("Command").get());
00249     ps->SetNumCopies(2);
00250     ps->SetPrinterName(NS_ConvertUTF8toUCS2("Printer Name").get());
00251     ps->SetPrintToFile(PR_TRUE);
00252     ps->SetToFileName(NS_ConvertUTF8toUCS2("File Name").get());
00253     ps->SetPrintPageDelay(1000);
00254 
00255     nsCOMPtr<nsIPrintSettings> ps2;
00256     if (NS_SUCCEEDED(rv)) {
00257       rv = printService->CreatePrintSettings(getter_AddRefs(ps2));
00258     }
00259 
00260     ps2->Assign(ps);
00261 
00262     nsCOMPtr<nsIPrintSettings> psClone;
00263     ps2->Clone(getter_AddRefs(psClone));
00264 
00265   }
00266 
00267 }
00268 Tester gTester;
00269 #endif
00270