forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnsDeviceContextSpecOS2.cpp
693 lines (581 loc) · 22.2 KB
/
nsDeviceContextSpecOS2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/* vim: set sw=2 sts=2 et cin: */
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <stdlib.h>
#include "nsDeviceContextSpecOS2.h"
#include "nsReadableUtils.h"
#include "nsTArray.h"
#include "prenv.h" /* for PR_GetEnv */
#include "prtime.h"
#include "nsIServiceManager.h"
#include "nsUnicharUtils.h"
#include "nsStringFwd.h"
#include "nsStringEnumerator.h"
#include "nsOS2Uni.h"
#include "nsIFile.h"
#include "nsDirectoryServiceDefs.h"
#include "nsIFileStreams.h"
#include "gfxPDFSurface.h"
#include "gfxOS2Surface.h"
#include "nsIPrintSettingsService.h"
#include "mozilla/Preferences.h"
using namespace mozilla;
PRINTDLG nsDeviceContextSpecOS2::PrnDlg;
//----------------------------------------------------------------------------------
// The printer data is shared between the PrinterEnumerator and the nsDeviceContextSpecOS2
// The PrinterEnumerator creates the printer info
// but the nsDeviceContextSpecOS2 cleans it up
// If it gets created (via the Page Setup Dialog) but the user never prints anything
// then it will never be delete, so this class takes care of that.
class GlobalPrinters {
public:
static GlobalPrinters* GetInstance() { return &mGlobalPrinters; }
~GlobalPrinters() { FreeGlobalPrinters(); }
void FreeGlobalPrinters();
nsresult InitializeGlobalPrinters();
bool PrintersAreAllocated() { return mGlobalPrinterList != nullptr; }
uint32_t GetNumPrinters() { return mGlobalNumPrinters; }
nsString* GetStringAt(int32_t aInx) { return &mGlobalPrinterList->ElementAt(aInx); }
void GetDefaultPrinterName(char16_t*& aDefaultPrinterName);
protected:
GlobalPrinters() {}
static GlobalPrinters mGlobalPrinters;
static nsTArray<nsString>* mGlobalPrinterList;
static ULONG mGlobalNumPrinters;
};
//---------------
// static members
GlobalPrinters GlobalPrinters::mGlobalPrinters;
nsTArray<nsString>* GlobalPrinters::mGlobalPrinterList = nullptr;
ULONG GlobalPrinters::mGlobalNumPrinters = 0;
//---------------
nsDeviceContextSpecOS2::nsDeviceContextSpecOS2()
: mQueue(nullptr), mPrintDC(nullptr), mPrintingStarted(false)
{
}
nsDeviceContextSpecOS2::~nsDeviceContextSpecOS2()
{
if (mQueue)
PrnClosePrinter(mQueue);
}
NS_IMPL_ISUPPORTS1(nsDeviceContextSpecOS2, nsIDeviceContextSpec)
void SetupDevModeFromSettings(ULONG printer, nsIPrintSettings* aPrintSettings)
{
if (aPrintSettings) {
int bufferSize = 3 * sizeof(DJP_ITEM);
PBYTE pDJP_Buffer = new BYTE[bufferSize];
memset(pDJP_Buffer, 0, bufferSize);
PDJP_ITEM pDJP = (PDJP_ITEM) pDJP_Buffer;
HDC hdc = nsDeviceContextSpecOS2::PrnDlg.GetDCHandle(printer);
char* driver = nsDeviceContextSpecOS2::PrnDlg.GetDriverType(printer);
// Setup Orientation
int32_t orientation;
aPrintSettings->GetOrientation(&orientation);
if (!strcmp(driver, "LASERJET"))
pDJP->lType = DJP_ALL;
else
pDJP->lType = DJP_CURRENT;
pDJP->cb = sizeof(DJP_ITEM);
pDJP->ulNumReturned = 1;
pDJP->ulProperty = DJP_SJ_ORIENTATION;
pDJP->ulValue = orientation == nsIPrintSettings::kPortraitOrientation?DJP_ORI_PORTRAIT:DJP_ORI_LANDSCAPE;
pDJP++;
// Setup Number of Copies
int32_t copies;
aPrintSettings->GetNumCopies(&copies);
pDJP->cb = sizeof(DJP_ITEM);
pDJP->lType = DJP_CURRENT;
pDJP->ulNumReturned = 1;
pDJP->ulProperty = DJP_SJ_COPIES;
pDJP->ulValue = copies;
pDJP++;
pDJP->cb = sizeof(DJP_ITEM);
pDJP->lType = DJP_NONE;
pDJP->ulNumReturned = 1;
pDJP->ulProperty = 0;
pDJP->ulValue = 0;
LONG driverSize = nsDeviceContextSpecOS2::PrnDlg.GetPrintDriverSize(printer);
GreEscape (hdc, DEVESC_SETJOBPROPERTIES, bufferSize, pDJP_Buffer,
&driverSize, PBYTE(nsDeviceContextSpecOS2::PrnDlg.GetPrintDriver(printer)));
delete [] pDJP_Buffer;
DevCloseDC(hdc);
}
}
nsresult nsDeviceContextSpecOS2::SetPrintSettingsFromDevMode(nsIPrintSettings* aPrintSettings, ULONG printer)
{
if (aPrintSettings == nullptr)
return NS_ERROR_FAILURE;
int bufferSize = 3 * sizeof(DJP_ITEM);
PBYTE pDJP_Buffer = new BYTE[bufferSize];
memset(pDJP_Buffer, 0, bufferSize);
PDJP_ITEM pDJP = (PDJP_ITEM) pDJP_Buffer;
HDC hdc = nsDeviceContextSpecOS2::PrnDlg.GetDCHandle(printer);
//Get Number of Copies from Job Properties
pDJP->lType = DJP_CURRENT;
pDJP->cb = sizeof(DJP_ITEM);
pDJP->ulNumReturned = 1;
pDJP->ulProperty = DJP_SJ_COPIES;
pDJP->ulValue = 1;
pDJP++;
//Get Orientation from Job Properties
pDJP->lType = DJP_CURRENT;
pDJP->cb = sizeof(DJP_ITEM);
pDJP->ulNumReturned = 1;
pDJP->ulProperty = DJP_SJ_ORIENTATION;
pDJP->ulValue = 1;
pDJP++;
pDJP->lType = DJP_NONE;
pDJP->cb = sizeof(DJP_ITEM);
pDJP->ulNumReturned = 1;
pDJP->ulProperty = 0;
pDJP->ulValue = 0;
LONG driverSize = nsDeviceContextSpecOS2::PrnDlg.GetPrintDriverSize(printer);
LONG rc = GreEscape(hdc, DEVESC_QUERYJOBPROPERTIES, bufferSize, pDJP_Buffer,
&driverSize, PBYTE(nsDeviceContextSpecOS2::PrnDlg.GetPrintDriver(printer)));
pDJP = (PDJP_ITEM) pDJP_Buffer;
if ((rc == DEV_OK) || (rc == DEV_WARNING)) {
while (pDJP->lType != DJP_NONE) {
if ((pDJP->ulProperty == DJP_SJ_ORIENTATION) && (pDJP->lType > 0)){
if ((pDJP->ulValue == DJP_ORI_PORTRAIT) || (pDJP->ulValue == DJP_ORI_REV_PORTRAIT))
aPrintSettings->SetOrientation(nsIPrintSettings::kPortraitOrientation);
else
aPrintSettings->SetOrientation(nsIPrintSettings::kLandscapeOrientation);
}
if ((pDJP->ulProperty == DJP_SJ_COPIES) && (pDJP->lType > 0)){
aPrintSettings->SetNumCopies(int32_t(pDJP->ulValue));
}
pDJP = DJP_NEXT_STRUCTP(pDJP);
}
}
delete [] pDJP_Buffer;
DevCloseDC(hdc);
return NS_OK;
}
NS_IMETHODIMP nsDeviceContextSpecOS2::Init(nsIWidget *aWidget,
nsIPrintSettings* aPS,
bool aIsPrintPreview)
{
nsresult rv = NS_ERROR_FAILURE;
mPrintSettings = aPS;
NS_ASSERTION(aPS, "Must have a PrintSettings!");
rv = GlobalPrinters::GetInstance()->InitializeGlobalPrinters();
if (NS_FAILED(rv)) {
return rv;
}
if (aPS) {
bool tofile = false;
int32_t copies = 1;
char16_t *printer = nullptr;
char16_t *printfile = nullptr;
mPrintSettings->GetPrinterName(&printer);
mPrintSettings->GetToFileName(&printfile);
mPrintSettings->GetPrintToFile(&tofile);
mPrintSettings->GetNumCopies(&copies);
if ((copies == 0) || (copies > 999)) {
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
return NS_ERROR_FAILURE;
}
if (printfile != nullptr) {
// ToDo: Use LocalEncoding instead of UTF-8 (see bug 73446)
strcpy(mPrData.path, NS_ConvertUTF16toUTF8(printfile).get());
}
if (printer != nullptr)
strcpy(mPrData.printer, NS_ConvertUTF16toUTF8(printer).get());
if (aIsPrintPreview)
mPrData.destination = printPreview;
else if (tofile)
mPrData.destination = printToFile;
else
mPrData.destination = printToPrinter;
mPrData.copies = copies;
rv = GlobalPrinters::GetInstance()->InitializeGlobalPrinters();
if (NS_FAILED(rv))
return rv;
const nsAFlatString& printerUCS2 = NS_ConvertUTF8toUTF16(mPrData.printer);
ULONG numPrinters = GlobalPrinters::GetInstance()->GetNumPrinters();
if (numPrinters) {
for(ULONG i = 0; (i < numPrinters) && !mQueue; i++) {
if ((GlobalPrinters::GetInstance()->GetStringAt(i)->Equals(printerUCS2, nsCaseInsensitiveStringComparator()))) {
SetupDevModeFromSettings(i, aPS);
mQueue = PrnDlg.SetPrinterQueue(i);
}
}
}
if (printfile != nullptr)
nsMemory::Free(printfile);
if (printer != nullptr)
nsMemory::Free(printer);
}
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
return rv;
}
NS_IMETHODIMP nsDeviceContextSpecOS2 :: GetDestination( int &aDestination )
{
aDestination = mPrData.destination;
return NS_OK;
}
NS_IMETHODIMP nsDeviceContextSpecOS2 :: GetPrinterName ( char **aPrinter )
{
*aPrinter = &mPrData.printer[0];
return NS_OK;
}
NS_IMETHODIMP nsDeviceContextSpecOS2 :: GetCopies ( int &aCopies )
{
aCopies = mPrData.copies;
return NS_OK;
}
NS_IMETHODIMP nsDeviceContextSpecOS2 :: GetPath ( char **aPath )
{
*aPath = &mPrData.path[0];
return NS_OK;
}
NS_IMETHODIMP nsDeviceContextSpecOS2 :: GetUserCancelled( bool &aCancel )
{
aCancel = mPrData.cancel;
return NS_OK;
}
/** -------------------------------------------------------
* Closes the printmanager if it is open.
* @update dc 2/15/98
*/
NS_IMETHODIMP nsDeviceContextSpecOS2 :: ClosePrintManager()
{
return NS_OK;
}
nsresult nsDeviceContextSpecOS2::GetPRTQUEUE( PRTQUEUE *&p)
{
p = mQueue;
return NS_OK;
}
NS_IMETHODIMP nsDeviceContextSpecOS2::GetSurfaceForPrinter(gfxASurface **surface)
{
NS_ASSERTION(mQueue, "Queue can't be NULL here");
nsRefPtr<gfxASurface> newSurface;
int16_t outputFormat;
mPrintSettings->GetOutputFormat(&outputFormat);
int printerDest;
GetDestination(printerDest);
if (printerDest != printPreview) {
// for now always set the output format to PDF, see bug 415522
outputFormat = nsIPrintSettings::kOutputFormatPDF;
mPrintSettings->SetOutputFormat(outputFormat); // save PDF format in settings
}
if (outputFormat == nsIPrintSettings::kOutputFormatPDF) {
nsXPIDLString filename;
mPrintSettings->GetToFileName(getter_Copies(filename));
nsresult rv;
if (filename.IsEmpty()) {
// print to a file that is visible, like one on the Desktop
nsCOMPtr<nsIFile> pdfLocation;
rv = NS_GetSpecialDirectory(NS_OS_DESKTOP_DIR, getter_AddRefs(pdfLocation));
NS_ENSURE_SUCCESS(rv, rv);
// construct a print output name using the current time, to make it
// unique and not overwrite existing files
char printName[CCHMAXPATH];
PRExplodedTime time;
PR_ExplodeTime(PR_Now(), PR_LocalTimeParameters, &time);
snprintf(printName, CCHMAXPATH-1, "%s_%04d%02d%02d_%02d%02d%02d.pdf",
MOZ_APP_DISPLAYNAME, time.tm_year, time.tm_month+1, time.tm_mday,
time.tm_hour, time.tm_min, time.tm_sec);
printName[CCHMAXPATH-1] = '\0';
nsAutoCString printString(printName);
rv = pdfLocation->AppendNative(printString);
NS_ENSURE_SUCCESS(rv, rv);
rv = pdfLocation->GetPath(filename);
NS_ENSURE_SUCCESS(rv, rv);
}
#ifdef debug_thebes_print
printf("nsDeviceContextSpecOS2::GetSurfaceForPrinter(): print to filename=%s\n",
NS_LossyConvertUTF16toASCII(filename).get());
#endif
double width, height;
mPrintSettings->GetEffectivePageSize(&width, &height);
// convert twips to points
width /= TWIPS_PER_POINT_FLOAT;
height /= TWIPS_PER_POINT_FLOAT;
nsCOMPtr<nsIFile> file = do_CreateInstance("@mozilla.org/file/local;1");
rv = file->InitWithPath(filename);
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsIFileOutputStream> stream = do_CreateInstance("@mozilla.org/network/file-output-stream;1");
rv = stream->Init(file, -1, -1, 0);
if (NS_FAILED(rv))
return rv;
newSurface = new(std::nothrow) gfxPDFSurface(stream, gfxSize(width, height));
} else {
int numCopies = 0;
GetCopies(numCopies);
char *filename = nullptr;
if (printerDest == printToFile) {
GetPath(&filename);
}
mPrintingStarted = true;
mPrintDC = PrnOpenDC(mQueue, "Mozilla", numCopies, printerDest, filename);
double width, height;
mPrintSettings->GetEffectivePageSize(&width, &height);
#ifdef debug_thebes_print
printf("nsDeviceContextSpecOS2::GetSurfaceForPrinter(): %fx%ftwips, copies=%d\n",
width, height, numCopies);
#endif
// we need pixels, so scale from twips to the printer resolution
// and take into account that CAPS_*_RESOLUTION are in px/m, default
// to approx. 100dpi
double hDPI = 3937., vDPI = 3937.;
LONG value;
if (DevQueryCaps(mPrintDC, CAPS_HORIZONTAL_RESOLUTION, 1, &value))
hDPI = value * 0.0254;
if (DevQueryCaps(mPrintDC, CAPS_VERTICAL_RESOLUTION, 1, &value))
vDPI = value * 0.0254;
width = width * hDPI / 1440;
height = height * vDPI / 1440;
#ifdef debug_thebes_print
printf("nsDeviceContextSpecOS2::GetSurfaceForPrinter(): %fx%fpx (res=%fx%f)\n"
" expected size: %7.2f MiB\n",
width, height, hDPI, vDPI, width*height*4./1024./1024.);
#endif
// perhaps restrict to usable area
// (this or scaling down won't help, we will just get more pages and still crash!)
if (DevQueryCaps(mPrintDC, CAPS_WIDTH, 1, &value) && width > (double)value)
width = (double)value;
if (DevQueryCaps(mPrintDC, CAPS_HEIGHT, 1, &value) && height > (double)value)
height = (double)value;
#ifdef debug_thebes_print
printf("nsDeviceContextSpecOS2::GetSurfaceForPrinter(): capped? %fx%fpx (res=%fx%f)\n"
" expected size: %7.2f MiB per page\n",
width, height, hDPI, vDPI, width*height*4./1024./1024.);
#endif
// Now pass the created DC into the thebes surface for printing.
// It gets destroyed there.
newSurface = new(std::nothrow)
gfxOS2Surface(mPrintDC, gfxIntSize(int(ceil(width)), int(ceil(height))));
}
if (!newSurface) {
*surface = nullptr;
return NS_ERROR_FAILURE;
}
*surface = newSurface;
NS_ADDREF(*surface);
return NS_OK;
}
// Helper function to convert the string to the native codepage,
// similar to UnicodeToCodepage() in nsDragService.cpp.
char *GetACPString(const nsAString& aStr)
{
if (aStr.Length() == 0) {
return nullptr;
}
nsAutoCharBuffer buffer;
int32_t bufLength;
WideCharToMultiByte(0, PromiseFlatString(aStr).get(), aStr.Length(),
buffer, bufLength);
return ToNewCString(nsDependentCString(buffer.Elements()));
}
// Helper function to convert the string to the native codepage,
// similar to UnicodeToCodepage() in nsDragService.cpp.
char *GetACPString(const char16_t* aStr)
{
nsString str(aStr);
return GetACPString(str);
}
NS_IMETHODIMP nsDeviceContextSpecOS2::BeginDocument(const nsAString& aTitle,
char16_t* aPrintToFileName,
int32_t aStartPage,
int32_t aEndPage)
{
#ifdef debug_thebes_print
printf("nsDeviceContextSpecOS2[%#x]::BeginPrinting(%s, %s)\n", (unsigned)this,
NS_LossyConvertUTF16toASCII(aTitle).get(),
NS_LossyConvertUTF16toASCII(nsString(aPrintToFileName)).get());
#endif
// don't try to send device escapes for non-native output (like PDF)
int16_t outputFormat;
mPrintSettings->GetOutputFormat(&outputFormat);
if (outputFormat != nsIPrintSettings::kOutputFormatNative) {
return NS_OK;
}
char *title = GetACPString(aTitle);
PCSZ pszGenericDocName = "Mozilla Document";
PCSZ pszDocName = title ? title : pszGenericDocName;
LONG lResult = DevEscape(mPrintDC, DEVESC_STARTDOC,
strlen(pszDocName) + 1, const_cast<BYTE*>(pszDocName),
(PLONG)nullptr, (PBYTE)nullptr);
mPrintingStarted = true;
if (title) {
nsMemory::Free(title);
}
return lResult == DEV_OK ? NS_OK : NS_ERROR_GFX_PRINTER_STARTDOC;
}
NS_IMETHODIMP nsDeviceContextSpecOS2::EndDocument()
{
// don't try to send device escapes for non-native output (like PDF)
// but clear the filename to make sure that we don't overwrite it next time
int16_t outputFormat;
mPrintSettings->GetOutputFormat(&outputFormat);
if (outputFormat != nsIPrintSettings::kOutputFormatNative) {
mPrintSettings->SetToFileName(nullptr);
nsCOMPtr<nsIPrintSettingsService> pss = do_GetService("@mozilla.org/gfx/printsettings-service;1");
if (pss)
pss->SavePrintSettingsToPrefs(mPrintSettings, true, nsIPrintSettings::kInitSaveToFileName);
return NS_OK;
}
LONG lOutCount = 2;
USHORT usJobID = 0;
LONG lResult = DevEscape(mPrintDC, DEVESC_ENDDOC, 0L, (PBYTE)nullptr,
&lOutCount, (PBYTE)&usJobID);
return lResult == DEV_OK ? NS_OK : NS_ERROR_GFX_PRINTER_ENDDOC;
}
NS_IMETHODIMP nsDeviceContextSpecOS2::BeginPage()
{
int16_t outputFormat;
mPrintSettings->GetOutputFormat(&outputFormat);
if (outputFormat != nsIPrintSettings::kOutputFormatNative) {
return NS_OK;
}
if (mPrintingStarted) {
// we don't want an extra page break at the start of the document
mPrintingStarted = false;
return NS_OK;
}
LONG lResult = DevEscape(mPrintDC, DEVESC_NEWFRAME, 0L, (PBYTE)nullptr,
(PLONG)nullptr, (PBYTE)nullptr);
return lResult == DEV_OK ? NS_OK : NS_ERROR_GFX_PRINTER_STARTPAGE;
}
NS_IMETHODIMP nsDeviceContextSpecOS2::EndPage()
{
return NS_OK;
}
// Printer Enumerator
nsPrinterEnumeratorOS2::nsPrinterEnumeratorOS2()
{
}
NS_IMPL_ISUPPORTS1(nsPrinterEnumeratorOS2, nsIPrinterEnumerator)
NS_IMETHODIMP nsPrinterEnumeratorOS2::GetPrinterNameList(nsIStringEnumerator **aPrinterNameList)
{
NS_ENSURE_ARG_POINTER(aPrinterNameList);
*aPrinterNameList = nullptr;
nsDeviceContextSpecOS2::PrnDlg.RefreshPrintQueue();
nsresult rv = GlobalPrinters::GetInstance()->InitializeGlobalPrinters();
if (NS_FAILED(rv)) {
return rv;
}
ULONG numPrinters = GlobalPrinters::GetInstance()->GetNumPrinters();
nsTArray<nsString> *printers = new nsTArray<nsString>(numPrinters);
if (!printers) {
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
return NS_ERROR_OUT_OF_MEMORY;
}
ULONG count = 0;
while( count < numPrinters )
{
printers->AppendElement(*GlobalPrinters::GetInstance()->GetStringAt(count++));
}
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
return NS_NewAdoptingStringEnumerator(aPrinterNameList, printers);
}
NS_IMETHODIMP nsPrinterEnumeratorOS2::GetDefaultPrinterName(char16_t * *aDefaultPrinterName)
{
NS_ENSURE_ARG_POINTER(aDefaultPrinterName);
GlobalPrinters::GetInstance()->GetDefaultPrinterName(*aDefaultPrinterName);
return NS_OK;
}
/* void initPrintSettingsFromPrinter (in wstring aPrinterName, in nsIPrintSettings aPrintSettings); */
NS_IMETHODIMP nsPrinterEnumeratorOS2::InitPrintSettingsFromPrinter(const char16_t *aPrinterName, nsIPrintSettings *aPrintSettings)
{
NS_ENSURE_ARG_POINTER(aPrinterName);
NS_ENSURE_ARG_POINTER(aPrintSettings);
if (!*aPrinterName)
return NS_OK;
if (NS_FAILED(GlobalPrinters::GetInstance()->InitializeGlobalPrinters()))
return NS_ERROR_FAILURE;
ULONG numPrinters = GlobalPrinters::GetInstance()->GetNumPrinters();
for(ULONG i = 0; i < numPrinters; i++) {
if ((GlobalPrinters::GetInstance()->GetStringAt(i)->Equals(aPrinterName, nsCaseInsensitiveStringComparator())))
nsDeviceContextSpecOS2::SetPrintSettingsFromDevMode(aPrintSettings, i);
}
// Free them, we won't need them for a while
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
aPrintSettings->SetIsInitializedFromPrinter(true);
return NS_OK;
}
NS_IMETHODIMP nsPrinterEnumeratorOS2::DisplayPropertiesDlg(const char16_t *aPrinter, nsIPrintSettings *aPrintSettings)
{
nsresult rv = GlobalPrinters::GetInstance()->InitializeGlobalPrinters();
if (NS_FAILED(rv)) {
return rv;
}
ULONG numPrinters = GlobalPrinters::GetInstance()->GetNumPrinters();
for(ULONG i = 0; i < numPrinters; i++) {
if ((GlobalPrinters::GetInstance()->GetStringAt(i)->Equals(aPrinter, nsCaseInsensitiveStringComparator()))) {
SetupDevModeFromSettings(i, aPrintSettings);
if ( nsDeviceContextSpecOS2::PrnDlg.ShowProperties(i) ) {
nsDeviceContextSpecOS2::SetPrintSettingsFromDevMode(aPrintSettings, i);
return NS_OK;
} else {
return NS_ERROR_FAILURE;
}
}
}
return NS_ERROR_FAILURE;
}
nsresult GlobalPrinters::InitializeGlobalPrinters ()
{
if (PrintersAreAllocated())
return NS_OK;
mGlobalNumPrinters = 0;
mGlobalNumPrinters = nsDeviceContextSpecOS2::PrnDlg.GetNumPrinters();
if (!mGlobalNumPrinters)
return NS_ERROR_GFX_PRINTER_NO_PRINTER_AVAILABLE;
mGlobalPrinterList = new nsTArray<nsString>();
if (!mGlobalPrinterList)
return NS_ERROR_OUT_OF_MEMORY;
// don't return on failure, optional feature
BOOL prefFailed = (Preferences::GetRootBranch() == nullptr);
for (ULONG i = 0; i < mGlobalNumPrinters; i++) {
nsXPIDLCString printer;
nsDeviceContextSpecOS2::PrnDlg.GetPrinter(i, getter_Copies(printer));
nsAutoChar16Buffer printerName;
int32_t printerNameLength;
nsresult rv = MultiByteToWideChar(0, printer, strlen(printer),
printerName, printerNameLength);
mGlobalPrinterList->AppendElement(nsDependentString(printerName.Elements()));
// store printer description in prefs for the print dialog
if (!prefFailed) {
nsAutoCString printerDescription;
printerDescription = nsAutoCString(nsDeviceContextSpecOS2::PrnDlg.GetPrintDriver(i)->szDeviceName);
printerDescription += " (";
printerDescription += nsAutoCString(nsDeviceContextSpecOS2::PrnDlg.GetDriverType(i));
printerDescription += ")";
nsAutoCString prefName("print.printer_");
prefName += printer;
prefName += ".printer_description";
Preferences::SetCString(prefName.get(), printerDescription);
}
}
return NS_OK;
}
void GlobalPrinters::GetDefaultPrinterName(char16_t*& aDefaultPrinterName)
{
aDefaultPrinterName = nullptr;
nsresult rv = GlobalPrinters::GetInstance()->InitializeGlobalPrinters();
if (NS_FAILED(rv))
return;
if (GetNumPrinters() == 0)
return;
// the default printer is always index 0
nsXPIDLCString printer;
nsDeviceContextSpecOS2::PrnDlg.GetPrinter(0, getter_Copies(printer));
nsAutoChar16Buffer printerName;
int32_t printerNameLength;
MultiByteToWideChar(0, printer, strlen(printer), printerName,
printerNameLength);
aDefaultPrinterName = ToNewUnicode(nsDependentString(printerName.Elements()));
GlobalPrinters::GetInstance()->FreeGlobalPrinters();
}
void GlobalPrinters::FreeGlobalPrinters()
{
delete mGlobalPrinterList;
mGlobalPrinterList = nullptr;
mGlobalNumPrinters = 0;
}