CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / widget / src / windows / nsPrintSettingsWin.cpp
blobb3787173524d17b44ce58380583e515d7aa5ddbd
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
37 #include "nsPrintSettingsWin.h"
38 #include "nsCRT.h"
40 NS_IMPL_ISUPPORTS_INHERITED1(nsPrintSettingsWin,
41 nsPrintSettings,
42 nsIPrintSettingsWin)
44 /** ---------------------------------------------------
45 * See documentation in nsPrintSettingsWin.h
46 * @update
48 nsPrintSettingsWin::nsPrintSettingsWin() :
49 nsPrintSettings(),
50 mDeviceName(nsnull),
51 mDriverName(nsnull),
52 mDevMode(nsnull)
57 /** ---------------------------------------------------
58 * See documentation in nsPrintSettingsWin.h
59 * @update
61 nsPrintSettingsWin::nsPrintSettingsWin(const nsPrintSettingsWin& aPS) :
62 mDeviceName(nsnull),
63 mDriverName(nsnull),
64 mDevMode(nsnull)
66 *this = aPS;
69 /** ---------------------------------------------------
70 * See documentation in nsPrintSettingsWin.h
71 * @update
73 nsPrintSettingsWin::~nsPrintSettingsWin()
75 if (mDeviceName) nsMemory::Free(mDeviceName);
76 if (mDriverName) nsMemory::Free(mDriverName);
77 if (mDevMode) ::HeapFree(::GetProcessHeap(), 0, mDevMode);
80 /* [noscript] attribute charPtr deviceName; */
81 NS_IMETHODIMP nsPrintSettingsWin::SetDeviceName(const PRUnichar * aDeviceName)
83 if (mDeviceName) {
84 nsMemory::Free(mDeviceName);
86 mDeviceName = aDeviceName?wcsdup(aDeviceName):nsnull;
87 return NS_OK;
89 NS_IMETHODIMP nsPrintSettingsWin::GetDeviceName(PRUnichar **aDeviceName)
91 NS_ENSURE_ARG_POINTER(aDeviceName);
92 *aDeviceName = mDeviceName?wcsdup(mDeviceName):nsnull;
93 return NS_OK;
96 /* [noscript] attribute charPtr driverName; */
97 NS_IMETHODIMP nsPrintSettingsWin::SetDriverName(const PRUnichar * aDriverName)
99 if (mDriverName) {
100 nsMemory::Free(mDriverName);
102 mDriverName = aDriverName?wcsdup(aDriverName):nsnull;
103 return NS_OK;
105 NS_IMETHODIMP nsPrintSettingsWin::GetDriverName(PRUnichar **aDriverName)
107 NS_ENSURE_ARG_POINTER(aDriverName);
108 *aDriverName = mDriverName?wcsdup(mDriverName):nsnull;
109 return NS_OK;
112 void nsPrintSettingsWin::CopyDevMode(DEVMODEW* aInDevMode, DEVMODEW *& aOutDevMode)
114 aOutDevMode = nsnull;
115 size_t size = aInDevMode->dmSize + aInDevMode->dmDriverExtra;
116 aOutDevMode = (LPDEVMODEW)::HeapAlloc (::GetProcessHeap(), HEAP_ZERO_MEMORY, size);
117 if (aOutDevMode) {
118 memcpy(aOutDevMode, aInDevMode, size);
123 /* [noscript] attribute nsDevMode devMode; */
124 NS_IMETHODIMP nsPrintSettingsWin::GetDevMode(DEVMODEW * *aDevMode)
126 NS_ENSURE_ARG_POINTER(aDevMode);
128 if (mDevMode) {
129 CopyDevMode(mDevMode, *aDevMode);
130 } else {
131 *aDevMode = nsnull;
133 return NS_OK;
136 NS_IMETHODIMP nsPrintSettingsWin::SetDevMode(DEVMODEW * aDevMode)
138 if (mDevMode) {
139 ::HeapFree(::GetProcessHeap(), 0, mDevMode);
140 mDevMode = NULL;
143 if (aDevMode) {
144 CopyDevMode(aDevMode, mDevMode);
146 return NS_OK;
149 //-------------------------------------------
150 nsresult
151 nsPrintSettingsWin::_Clone(nsIPrintSettings **_retval)
153 nsPrintSettingsWin* printSettings = new nsPrintSettingsWin(*this);
154 return printSettings->QueryInterface(NS_GET_IID(nsIPrintSettings), (void**)_retval); // ref counts
157 //-------------------------------------------
158 nsPrintSettingsWin& nsPrintSettingsWin::operator=(const nsPrintSettingsWin& rhs)
160 if (this == &rhs) {
161 return *this;
164 ((nsPrintSettings&) *this) = rhs;
166 if (mDeviceName) {
167 nsCRT::free(mDeviceName);
170 if (mDriverName) {
171 nsCRT::free(mDriverName);
174 // Use free because we used the native malloc to create the memory
175 if (mDevMode) {
176 ::HeapFree(::GetProcessHeap(), 0, mDevMode);
179 mDeviceName = rhs.mDeviceName?wcsdup(rhs.mDeviceName):nsnull;
180 mDriverName = rhs.mDriverName?wcsdup(rhs.mDriverName):nsnull;
182 if (rhs.mDevMode) {
183 CopyDevMode(rhs.mDevMode, mDevMode);
184 } else {
185 mDevMode = nsnull;
188 return *this;
191 //-------------------------------------------
192 /* void assign (in nsIPrintSettings aPS); */
193 nsresult
194 nsPrintSettingsWin::_Assign(nsIPrintSettings *aPS)
196 nsPrintSettingsWin *psWin = static_cast<nsPrintSettingsWin*>(aPS);
197 *this = *psWin;
198 return NS_OK;
201 //----------------------------------------------------------------------
202 // Testing of assign and clone
203 // This define turns on the testing module below
204 // so at start up it writes and reads the prefs.
205 #ifdef DEBUG_rodsX
206 #include "nsIPrintOptions.h"
207 #include "nsIServiceManager.h"
208 class Tester {
209 public:
210 Tester();
212 Tester::Tester()
214 nsCOMPtr<nsIPrintSettings> ps;
215 nsresult rv;
216 nsCOMPtr<nsIPrintOptions> printService = do_GetService("@mozilla.org/gfx/printsettings-service;1", &rv);
217 if (NS_SUCCEEDED(rv)) {
218 rv = printService->CreatePrintSettings(getter_AddRefs(ps));
221 if (ps) {
222 ps->SetPrintOptions(nsIPrintSettings::kPrintOddPages, PR_TRUE);
223 ps->SetPrintOptions(nsIPrintSettings::kPrintEvenPages, PR_FALSE);
224 ps->SetMarginTop(1.0);
225 ps->SetMarginLeft(1.0);
226 ps->SetMarginBottom(1.0);
227 ps->SetMarginRight(1.0);
228 ps->SetScaling(0.5);
229 ps->SetPrintBGColors(PR_TRUE);
230 ps->SetPrintBGImages(PR_TRUE);
231 ps->SetPrintRange(15);
232 ps->SetHeaderStrLeft(NS_ConvertUTF8toUTF16("Left").get());
233 ps->SetHeaderStrCenter(NS_ConvertUTF8toUTF16("Center").get());
234 ps->SetHeaderStrRight(NS_ConvertUTF8toUTF16("Right").get());
235 ps->SetFooterStrLeft(NS_ConvertUTF8toUTF16("Left").get());
236 ps->SetFooterStrCenter(NS_ConvertUTF8toUTF16("Center").get());
237 ps->SetFooterStrRight(NS_ConvertUTF8toUTF16("Right").get());
238 ps->SetPaperName(NS_ConvertUTF8toUTF16("Paper Name").get());
239 ps->SetPaperSizeType(10);
240 ps->SetPaperData(1);
241 ps->SetPaperWidth(100.0);
242 ps->SetPaperHeight(50.0);
243 ps->SetPaperSizeUnit(nsIPrintSettings::kPaperSizeMillimeters);
244 ps->SetPrintReversed(PR_TRUE);
245 ps->SetPrintInColor(PR_TRUE);
246 ps->SetOrientation(nsIPrintSettings::kLandscapeOrientation);
247 ps->SetPrintCommand(NS_ConvertUTF8toUTF16("Command").get());
248 ps->SetNumCopies(2);
249 ps->SetPrinterName(NS_ConvertUTF8toUTF16("Printer Name").get());
250 ps->SetPrintToFile(PR_TRUE);
251 ps->SetToFileName(NS_ConvertUTF8toUTF16("File Name").get());
252 ps->SetPrintPageDelay(1000);
254 nsCOMPtr<nsIPrintSettings> ps2;
255 if (NS_SUCCEEDED(rv)) {
256 rv = printService->CreatePrintSettings(getter_AddRefs(ps2));
259 ps2->Assign(ps);
261 nsCOMPtr<nsIPrintSettings> psClone;
262 ps2->Clone(getter_AddRefs(psClone));
267 Tester gTester;
268 #endif