CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / widget / src / gtk2 / nsPrintSettingsGTK.cpp
blob1f32c9f439b1350f752b0e1a9623d2e0e9f67928
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 Mozilla Foundation.
18 * Portions created by the Initial Developer are Copyright (C) 2008
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Michael Ventnor <m.ventnor@gmail.com>
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 ***** */
38 #include "nsPrintSettingsGTK.h"
39 #include "nsILocalFile.h"
40 #include "nsNetUtil.h"
41 #include <stdlib.h>
43 static
44 gboolean ref_printer(GtkPrinter *aPrinter, gpointer aData)
46 ((nsPrintSettingsGTK*) aData)->SetGtkPrinter(aPrinter);
47 return TRUE;
50 static
51 gboolean printer_enumerator(GtkPrinter *aPrinter, gpointer aData)
53 if (gtk_printer_is_default(aPrinter))
54 return ref_printer(aPrinter, aData);
56 return FALSE; // Keep 'em coming...
59 static
60 GtkPaperSize* moz_gtk_paper_size_copy_to_new_custom(GtkPaperSize* oldPaperSize)
62 // We make a "custom-ified" copy of the paper size so it can be changed later.
63 return gtk_paper_size_new_custom(gtk_paper_size_get_name(oldPaperSize),
64 gtk_paper_size_get_display_name(oldPaperSize),
65 gtk_paper_size_get_width(oldPaperSize, GTK_UNIT_INCH),
66 gtk_paper_size_get_height(oldPaperSize, GTK_UNIT_INCH),
67 GTK_UNIT_INCH);
70 NS_IMPL_ISUPPORTS_INHERITED1(nsPrintSettingsGTK,
71 nsPrintSettings,
72 nsPrintSettingsGTK)
74 /** ---------------------------------------------------
76 nsPrintSettingsGTK::nsPrintSettingsGTK() :
77 mPageSetup(NULL),
78 mPrintSettings(NULL),
79 mGTKPrinter(NULL),
80 mPrintSelectionOnly(PR_FALSE)
82 // The aim here is to set up the objects enough that silent printing works well.
83 // These will be replaced anyway if the print dialog is used.
84 mPrintSettings = gtk_print_settings_new();
85 mPageSetup = gtk_page_setup_new();
86 InitUnwriteableMargin();
88 SetOutputFormat(nsIPrintSettings::kOutputFormatNative);
90 GtkPaperSize* defaultPaperSize = gtk_paper_size_new(NULL);
91 mPaperSize = moz_gtk_paper_size_copy_to_new_custom(defaultPaperSize);
92 gtk_paper_size_free(defaultPaperSize);
93 SaveNewPageSize();
96 /** ---------------------------------------------------
98 nsPrintSettingsGTK::~nsPrintSettingsGTK()
100 if (mPageSetup) {
101 g_object_unref(mPageSetup);
102 mPageSetup = NULL;
104 if (mPrintSettings) {
105 g_object_unref(mPrintSettings);
106 mPrintSettings = NULL;
108 if (mGTKPrinter) {
109 g_object_unref(mGTKPrinter);
110 mGTKPrinter = NULL;
112 gtk_paper_size_free(mPaperSize);
115 /** ---------------------------------------------------
117 nsPrintSettingsGTK::nsPrintSettingsGTK(const nsPrintSettingsGTK& aPS) :
118 mPageSetup(NULL),
119 mPrintSettings(NULL),
120 mGTKPrinter(NULL),
121 mPrintSelectionOnly(PR_FALSE)
123 *this = aPS;
126 /** ---------------------------------------------------
128 nsPrintSettingsGTK& nsPrintSettingsGTK::operator=(const nsPrintSettingsGTK& rhs)
130 if (this == &rhs) {
131 return *this;
134 nsPrintSettings::operator=(rhs);
136 if (mPageSetup)
137 g_object_unref(mPageSetup);
138 mPageSetup = gtk_page_setup_copy(rhs.mPageSetup);
139 // NOTE: No need to re-initialize mUnwriteableMargin here (even
140 // though mPageSetup is changing). It'll be copied correctly by
141 // nsPrintSettings::operator=.
143 if (mPrintSettings)
144 g_object_unref(mPrintSettings);
145 mPrintSettings = gtk_print_settings_copy(rhs.mPrintSettings);
147 if (mGTKPrinter)
148 g_object_unref(mGTKPrinter);
149 mGTKPrinter = (GtkPrinter*) g_object_ref(rhs.mGTKPrinter);
151 mPrintSelectionOnly = rhs.mPrintSelectionOnly;
153 return *this;
156 /** -------------------------------------------
158 nsresult nsPrintSettingsGTK::_Clone(nsIPrintSettings **_retval)
160 NS_ENSURE_ARG_POINTER(_retval);
161 *_retval = nsnull;
163 nsPrintSettingsGTK *newSettings = new nsPrintSettingsGTK(*this);
164 if (!newSettings)
165 return NS_ERROR_FAILURE;
166 *_retval = newSettings;
167 NS_ADDREF(*_retval);
168 return NS_OK;
172 /** -------------------------------------------
174 NS_IMETHODIMP
175 nsPrintSettingsGTK::_Assign(nsIPrintSettings *aPS)
177 nsPrintSettingsGTK *printSettingsGTK = static_cast<nsPrintSettingsGTK*>(aPS);
178 if (!printSettingsGTK)
179 return NS_ERROR_UNEXPECTED;
180 *this = *printSettingsGTK;
181 return NS_OK;
184 /** ---------------------------------------------------
186 void
187 nsPrintSettingsGTK::SetGtkPageSetup(GtkPageSetup *aPageSetup)
189 if (mPageSetup)
190 g_object_unref(mPageSetup);
192 mPageSetup = (GtkPageSetup*) g_object_ref(aPageSetup);
193 InitUnwriteableMargin();
195 // We make a custom copy of the GtkPaperSize so it can be mutable. If a
196 // GtkPaperSize wasn't made as custom, its properties are immutable.
197 GtkPaperSize* newPaperSize = gtk_page_setup_get_paper_size(aPageSetup);
198 if (newPaperSize) { // Yes, this can be null
199 gtk_paper_size_free(mPaperSize);
200 mPaperSize = moz_gtk_paper_size_copy_to_new_custom(newPaperSize);
202 // If newPaperSize was not null, we must update our twin too (GtkPrintSettings).
203 // If newPaperSize was null, we must set this object to use mPaperSize.
204 SaveNewPageSize();
207 /** ---------------------------------------------------
209 void
210 nsPrintSettingsGTK::SetGtkPrintSettings(GtkPrintSettings *aPrintSettings)
212 if (mPrintSettings)
213 g_object_unref(mPrintSettings);
215 mPrintSettings = (GtkPrintSettings*) g_object_ref(aPrintSettings);
217 GtkPaperSize* newPaperSize = gtk_print_settings_get_paper_size(aPrintSettings);
218 if (newPaperSize) {
219 gtk_paper_size_free(mPaperSize);
220 mPaperSize = moz_gtk_paper_size_copy_to_new_custom(newPaperSize);
222 SaveNewPageSize();
225 /** ---------------------------------------------------
227 void
228 nsPrintSettingsGTK::SetGtkPrinter(GtkPrinter *aPrinter)
230 if (mGTKPrinter)
231 g_object_unref(mGTKPrinter);
233 mGTKPrinter = (GtkPrinter*) g_object_ref(aPrinter);
237 * Reimplementation of nsPrintSettings functions so that we get the values
238 * from the GTK objects rather than our own variables.
241 /* attribute long printRange; */
242 NS_IMETHODIMP nsPrintSettingsGTK::GetPrintRange(PRInt16 *aPrintRange)
244 NS_ENSURE_ARG_POINTER(aPrintRange);
245 if (mPrintSelectionOnly) {
246 *aPrintRange = kRangeSelection;
247 return NS_OK;
250 GtkPrintPages gtkRange = gtk_print_settings_get_print_pages(mPrintSettings);
251 if (gtkRange == GTK_PRINT_PAGES_RANGES)
252 *aPrintRange = kRangeSpecifiedPageRange;
253 else
254 *aPrintRange = kRangeAllPages;
256 return NS_OK;
258 NS_IMETHODIMP nsPrintSettingsGTK::SetPrintRange(PRInt16 aPrintRange)
260 if (aPrintRange == kRangeSelection) {
261 mPrintSelectionOnly = PR_TRUE;
262 return NS_OK;
265 mPrintSelectionOnly = PR_FALSE;
266 if (aPrintRange == kRangeSpecifiedPageRange)
267 gtk_print_settings_set_print_pages(mPrintSettings, GTK_PRINT_PAGES_RANGES);
268 else
269 gtk_print_settings_set_print_pages(mPrintSettings, GTK_PRINT_PAGES_ALL);
270 return NS_OK;
273 /* attribute long startPageRange; */
274 NS_IMETHODIMP
275 nsPrintSettingsGTK::GetStartPageRange(PRInt32 *aStartPageRange)
277 gint ctRanges;
278 GtkPageRange* lstRanges = gtk_print_settings_get_page_ranges(mPrintSettings, &ctRanges);
280 // Make sure we got a range.
281 if (ctRanges < 1) {
282 *aStartPageRange = 1;
283 } else {
284 // GTK supports multiple page ranges; gecko only supports 1. So find
285 // the lowest start page.
286 PRInt32 start(lstRanges[0].start);
287 for (gint ii = 1; ii < ctRanges; ii++) {
288 start = PR_MIN(lstRanges[ii].start, start);
290 *aStartPageRange = start + 1;
293 g_free(lstRanges);
294 return NS_OK;
296 NS_IMETHODIMP
297 nsPrintSettingsGTK::SetStartPageRange(PRInt32 aStartPageRange)
299 PRInt32 endRange;
300 GetEndPageRange(&endRange);
302 GtkPageRange gtkRange;
303 gtkRange.start = aStartPageRange - 1;
304 gtkRange.end = endRange - 1;
306 gtk_print_settings_set_page_ranges(mPrintSettings, &gtkRange, 1);
308 return NS_OK;
311 /* attribute long endPageRange; */
312 NS_IMETHODIMP
313 nsPrintSettingsGTK::GetEndPageRange(PRInt32 *aEndPageRange)
315 gint ctRanges;
316 GtkPageRange* lstRanges = gtk_print_settings_get_page_ranges(mPrintSettings, &ctRanges);
318 if (ctRanges < 1) {
319 *aEndPageRange = 1;
320 } else {
321 PRInt32 end(lstRanges[0].end);
322 for (gint ii = 1; ii < ctRanges; ii++) {
323 end = PR_MAX(lstRanges[ii].end, end);
325 *aEndPageRange = end + 1;
328 g_free(lstRanges);
329 return NS_OK;
331 NS_IMETHODIMP
332 nsPrintSettingsGTK::SetEndPageRange(PRInt32 aEndPageRange)
334 PRInt32 startRange;
335 GetStartPageRange(&startRange);
337 GtkPageRange gtkRange;
338 gtkRange.start = startRange - 1;
339 gtkRange.end = aEndPageRange - 1;
341 gtk_print_settings_set_page_ranges(mPrintSettings, &gtkRange, 1);
343 return NS_OK;
346 /* attribute boolean printReversed; */
347 NS_IMETHODIMP
348 nsPrintSettingsGTK::GetPrintReversed(PRBool *aPrintReversed)
350 *aPrintReversed = gtk_print_settings_get_reverse(mPrintSettings);
351 return NS_OK;
353 NS_IMETHODIMP
354 nsPrintSettingsGTK::SetPrintReversed(PRBool aPrintReversed)
356 gtk_print_settings_set_reverse(mPrintSettings, aPrintReversed);
357 return NS_OK;
360 /* attribute boolean printInColor; */
361 NS_IMETHODIMP
362 nsPrintSettingsGTK::GetPrintInColor(PRBool *aPrintInColor)
364 *aPrintInColor = gtk_print_settings_get_use_color(mPrintSettings);
365 return NS_OK;
367 NS_IMETHODIMP
368 nsPrintSettingsGTK::SetPrintInColor(PRBool aPrintInColor)
370 gtk_print_settings_set_use_color(mPrintSettings, aPrintInColor);
371 return NS_OK;
374 /* attribute short orientation; */
375 NS_IMETHODIMP
376 nsPrintSettingsGTK::GetOrientation(PRInt32 *aOrientation)
378 NS_ENSURE_ARG_POINTER(aOrientation);
380 GtkPageOrientation gtkOrient = gtk_page_setup_get_orientation(mPageSetup);
381 switch (gtkOrient) {
382 case GTK_PAGE_ORIENTATION_LANDSCAPE:
383 case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
384 *aOrientation = kLandscapeOrientation;
385 break;
387 case GTK_PAGE_ORIENTATION_PORTRAIT:
388 case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
389 default:
390 *aOrientation = kPortraitOrientation;
392 return NS_OK;
394 NS_IMETHODIMP
395 nsPrintSettingsGTK::SetOrientation(PRInt32 aOrientation)
397 GtkPageOrientation gtkOrient;
398 if (aOrientation == kLandscapeOrientation)
399 gtkOrient = GTK_PAGE_ORIENTATION_LANDSCAPE;
400 else
401 gtkOrient = GTK_PAGE_ORIENTATION_PORTRAIT;
403 gtk_print_settings_set_orientation(mPrintSettings, gtkOrient);
404 gtk_page_setup_set_orientation(mPageSetup, gtkOrient);
405 return NS_OK;
408 /* attribute wstring toFileName; */
409 NS_IMETHODIMP
410 nsPrintSettingsGTK::GetToFileName(PRUnichar * *aToFileName)
412 // Get the gtk output filename
413 const char* gtk_output_uri = gtk_print_settings_get(mPrintSettings, GTK_PRINT_SETTINGS_OUTPUT_URI);
414 if (!gtk_output_uri) {
415 *aToFileName = ToNewUnicode(mToFileName);
416 return NS_OK;
419 // Convert to an nsIFile
420 nsCOMPtr<nsIFile> file;
421 nsresult rv = NS_GetFileFromURLSpec(nsDependentCString(gtk_output_uri),
422 getter_AddRefs(file));
423 if (NS_FAILED(rv))
424 return rv;
426 // Extract the path
427 nsAutoString path;
428 rv = file->GetPath(path);
429 NS_ENSURE_SUCCESS(rv, rv);
431 *aToFileName = ToNewUnicode(path);
432 return NS_OK;
435 NS_IMETHODIMP
436 nsPrintSettingsGTK::SetToFileName(const PRUnichar * aToFileName)
438 if (aToFileName[0] == 0) {
439 mToFileName.SetLength(0);
440 gtk_print_settings_set(mPrintSettings, GTK_PRINT_SETTINGS_OUTPUT_URI, NULL);
441 return NS_OK;
444 if (StringEndsWith(nsDependentString(aToFileName), NS_LITERAL_STRING(".ps"))) {
445 gtk_print_settings_set(mPrintSettings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, "ps");
446 } else {
447 gtk_print_settings_set(mPrintSettings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, "pdf");
450 nsCOMPtr<nsILocalFile> file;
451 nsresult rv = NS_NewLocalFile(nsDependentString(aToFileName), PR_TRUE,
452 getter_AddRefs(file));
453 NS_ENSURE_SUCCESS(rv, rv);
455 // Convert the nsIFile to a URL
456 nsCAutoString url;
457 rv = NS_GetURLSpecFromFile(file, url);
458 NS_ENSURE_SUCCESS(rv, rv);
460 gtk_print_settings_set(mPrintSettings, GTK_PRINT_SETTINGS_OUTPUT_URI, url.get());
461 mToFileName = aToFileName;
463 return NS_OK;
466 NS_IMETHODIMP
467 nsPrintSettingsGTK::GetPrinterName(PRUnichar * *aPrinter)
469 const char* gtkPrintName = gtk_print_settings_get_printer(mPrintSettings);
470 if (!gtkPrintName) {
471 if (GTK_IS_PRINTER(mGTKPrinter)) {
472 gtkPrintName = gtk_printer_get_name(mGTKPrinter);
473 } else {
474 // This mimics what nsPrintSettingsImpl does when we try to Get before we Set
475 nsXPIDLString nullPrintName;
476 *aPrinter = ToNewUnicode(nullPrintName);
477 return NS_OK;
480 *aPrinter = ToNewUnicode(nsDependentCString(gtkPrintName));
481 return NS_OK;
484 NS_IMETHODIMP
485 nsPrintSettingsGTK::SetPrinterName(const PRUnichar * aPrinter)
487 NS_ConvertUTF16toUTF8 gtkPrinter(aPrinter);
489 if (StringBeginsWith(gtkPrinter, NS_LITERAL_CSTRING("CUPS/"))) {
490 // Strip off "CUPS/"; GTK might recognize the rest
491 gtkPrinter.Cut(0, strlen("CUPS/"));
494 // Give mPrintSettings the passed-in printer name if either...
495 // - it has no printer name stored yet
496 // - it has an existing printer name that's different from
497 // the name passed to this function.
498 const char* oldPrinterName = gtk_print_settings_get_printer(mPrintSettings);
499 if (!oldPrinterName || !gtkPrinter.Equals(oldPrinterName)) {
500 mIsInitedFromPrinter = PR_FALSE;
501 mIsInitedFromPrefs = PR_FALSE;
502 gtk_print_settings_set_printer(mPrintSettings, gtkPrinter.get());
505 return NS_OK;
508 /* attribute long numCopies; */
509 NS_IMETHODIMP
510 nsPrintSettingsGTK::GetNumCopies(PRInt32 *aNumCopies)
512 NS_ENSURE_ARG_POINTER(aNumCopies);
513 *aNumCopies = gtk_print_settings_get_n_copies(mPrintSettings);
514 return NS_OK;
516 NS_IMETHODIMP
517 nsPrintSettingsGTK::SetNumCopies(PRInt32 aNumCopies)
519 gtk_print_settings_set_n_copies(mPrintSettings, aNumCopies);
520 return NS_OK;
523 /* attribute double scaling; */
524 NS_IMETHODIMP
525 nsPrintSettingsGTK::GetScaling(double *aScaling)
527 *aScaling = gtk_print_settings_get_scale(mPrintSettings) / 100.0;
528 return NS_OK;
531 NS_IMETHODIMP
532 nsPrintSettingsGTK::SetScaling(double aScaling)
534 gtk_print_settings_set_scale(mPrintSettings, aScaling * 100.0);
535 return NS_OK;
538 /* attribute wstring paperName; */
539 NS_IMETHODIMP
540 nsPrintSettingsGTK::GetPaperName(PRUnichar * *aPaperName)
542 NS_ENSURE_ARG_POINTER(aPaperName);
543 *aPaperName = ToNewUnicode(NS_ConvertUTF8toUTF16(gtk_paper_size_get_name(mPaperSize)));
544 return NS_OK;
546 NS_IMETHODIMP
547 nsPrintSettingsGTK::SetPaperName(const PRUnichar * aPaperName)
549 NS_ConvertUTF16toUTF8 gtkPaperName(aPaperName);
551 // Convert these Gecko names to GTK names
552 if (gtkPaperName.EqualsIgnoreCase("letter"))
553 gtkPaperName.AssignLiteral(GTK_PAPER_NAME_LETTER);
554 else if (gtkPaperName.EqualsIgnoreCase("legal"))
555 gtkPaperName.AssignLiteral(GTK_PAPER_NAME_LEGAL);
557 // Try to get the display name from the name so our paper size fits in the Page Setup dialog.
558 GtkPaperSize* paperSize = gtk_paper_size_new(gtkPaperName.get());
559 char* displayName = strdup(gtk_paper_size_get_display_name(paperSize));
560 gtk_paper_size_free(paperSize);
562 paperSize = gtk_paper_size_new_custom(gtkPaperName.get(), displayName,
563 gtk_paper_size_get_width(mPaperSize, GTK_UNIT_INCH),
564 gtk_paper_size_get_height(mPaperSize, GTK_UNIT_INCH),
565 GTK_UNIT_INCH);
567 free(displayName);
568 gtk_paper_size_free(mPaperSize);
569 mPaperSize = paperSize;
570 SaveNewPageSize();
571 return NS_OK;
574 GtkUnit
575 nsPrintSettingsGTK::GetGTKUnit(PRInt16 aGeckoUnit)
577 if (aGeckoUnit == kPaperSizeMillimeters)
578 return GTK_UNIT_MM;
579 else
580 return GTK_UNIT_INCH;
583 void
584 nsPrintSettingsGTK::SaveNewPageSize()
586 gtk_print_settings_set_paper_size(mPrintSettings, mPaperSize);
587 gtk_page_setup_set_paper_size(mPageSetup, mPaperSize);
590 void
591 nsPrintSettingsGTK::InitUnwriteableMargin()
593 mUnwriteableMargin.SizeTo(
594 NS_INCHES_TO_INT_TWIPS(gtk_page_setup_get_left_margin(mPageSetup, GTK_UNIT_INCH)),
595 NS_INCHES_TO_INT_TWIPS(gtk_page_setup_get_top_margin(mPageSetup, GTK_UNIT_INCH)),
596 NS_INCHES_TO_INT_TWIPS(gtk_page_setup_get_right_margin(mPageSetup, GTK_UNIT_INCH)),
597 NS_INCHES_TO_INT_TWIPS(gtk_page_setup_get_bottom_margin(mPageSetup, GTK_UNIT_INCH))
602 * NOTE: Need a custom set of SetUnwriteableMargin functions, because
603 * whenever we change mUnwriteableMargin, we must pass the change
604 * down to our GTKPageSetup object. (This is needed in order for us
605 * to give the correct default values in nsPrintDialogGTK.)
607 * It's important that the following functions pass
608 * mUnwriteableMargin values rather than aUnwriteableMargin values
609 * to gtk_page_setup_set_[blank]_margin, because the two may not be
610 * the same. (Specifically, negative values of aUnwriteableMargin
611 * are ignored by the nsPrintSettings::SetUnwriteableMargin functions.)
613 NS_IMETHODIMP
614 nsPrintSettingsGTK::SetUnwriteableMarginInTwips(nsIntMargin& aUnwriteableMargin)
616 nsPrintSettings::SetUnwriteableMarginInTwips(aUnwriteableMargin);
617 gtk_page_setup_set_top_margin(mPageSetup,
618 NS_TWIPS_TO_INCHES(mUnwriteableMargin.top), GTK_UNIT_INCH);
619 gtk_page_setup_set_left_margin(mPageSetup,
620 NS_TWIPS_TO_INCHES(mUnwriteableMargin.left), GTK_UNIT_INCH);
621 gtk_page_setup_set_bottom_margin(mPageSetup,
622 NS_TWIPS_TO_INCHES(mUnwriteableMargin.bottom), GTK_UNIT_INCH);
623 gtk_page_setup_set_right_margin(mPageSetup,
624 NS_TWIPS_TO_INCHES(mUnwriteableMargin.right), GTK_UNIT_INCH);
625 return NS_OK;
628 /* attribute double unwriteableMarginTop; */
629 NS_IMETHODIMP
630 nsPrintSettingsGTK::SetUnwriteableMarginTop(double aUnwriteableMarginTop)
632 nsPrintSettings::SetUnwriteableMarginTop(aUnwriteableMarginTop);
633 gtk_page_setup_set_top_margin(mPageSetup,
634 NS_TWIPS_TO_INCHES(mUnwriteableMargin.top), GTK_UNIT_INCH);
635 return NS_OK;
638 /* attribute double unwriteableMarginLeft; */
639 NS_IMETHODIMP
640 nsPrintSettingsGTK::SetUnwriteableMarginLeft(double aUnwriteableMarginLeft)
642 nsPrintSettings::SetUnwriteableMarginLeft(aUnwriteableMarginLeft);
643 gtk_page_setup_set_left_margin(mPageSetup,
644 NS_TWIPS_TO_INCHES(mUnwriteableMargin.left), GTK_UNIT_INCH);
645 return NS_OK;
648 /* attribute double unwriteableMarginBottom; */
649 NS_IMETHODIMP
650 nsPrintSettingsGTK::SetUnwriteableMarginBottom(double aUnwriteableMarginBottom)
652 nsPrintSettings::SetUnwriteableMarginBottom(aUnwriteableMarginBottom);
653 gtk_page_setup_set_bottom_margin(mPageSetup,
654 NS_TWIPS_TO_INCHES(mUnwriteableMargin.bottom), GTK_UNIT_INCH);
655 return NS_OK;
658 /* attribute double unwriteableMarginRight; */
659 NS_IMETHODIMP
660 nsPrintSettingsGTK::SetUnwriteableMarginRight(double aUnwriteableMarginRight)
662 nsPrintSettings::SetUnwriteableMarginRight(aUnwriteableMarginRight);
663 gtk_page_setup_set_right_margin(mPageSetup,
664 NS_TWIPS_TO_INCHES(mUnwriteableMargin.right), GTK_UNIT_INCH);
665 return NS_OK;
668 /* attribute double paperWidth; */
669 NS_IMETHODIMP
670 nsPrintSettingsGTK::GetPaperWidth(double *aPaperWidth)
672 NS_ENSURE_ARG_POINTER(aPaperWidth);
673 *aPaperWidth = gtk_paper_size_get_width(mPaperSize, GetGTKUnit(mPaperSizeUnit));
674 return NS_OK;
676 NS_IMETHODIMP
677 nsPrintSettingsGTK::SetPaperWidth(double aPaperWidth)
679 gtk_paper_size_set_size(mPaperSize,
680 aPaperWidth,
681 gtk_paper_size_get_height(mPaperSize, GetGTKUnit(mPaperSizeUnit)),
682 GetGTKUnit(mPaperSizeUnit));
683 SaveNewPageSize();
684 return NS_OK;
687 /* attribute double paperHeight; */
688 NS_IMETHODIMP
689 nsPrintSettingsGTK::GetPaperHeight(double *aPaperHeight)
691 NS_ENSURE_ARG_POINTER(aPaperHeight);
692 *aPaperHeight = gtk_paper_size_get_height(mPaperSize, GetGTKUnit(mPaperSizeUnit));
693 return NS_OK;
695 NS_IMETHODIMP
696 nsPrintSettingsGTK::SetPaperHeight(double aPaperHeight)
698 gtk_paper_size_set_size(mPaperSize,
699 gtk_paper_size_get_width(mPaperSize, GetGTKUnit(mPaperSizeUnit)),
700 aPaperHeight,
701 GetGTKUnit(mPaperSizeUnit));
702 SaveNewPageSize();
703 return NS_OK;
706 NS_IMETHODIMP
707 nsPrintSettingsGTK::SetPaperSizeUnit(PRInt16 aPaperSizeUnit)
709 // Convert units internally. e.g. they might have set the values while we're still in mm but
710 // they change to inch just afterwards, expecting that their sizes are in inches.
711 gtk_paper_size_set_size(mPaperSize,
712 gtk_paper_size_get_width(mPaperSize, GetGTKUnit(mPaperSizeUnit)),
713 gtk_paper_size_get_height(mPaperSize, GetGTKUnit(mPaperSizeUnit)),
714 GetGTKUnit(aPaperSizeUnit));
715 SaveNewPageSize();
717 mPaperSizeUnit = aPaperSizeUnit;
718 return NS_OK;
721 NS_IMETHODIMP
722 nsPrintSettingsGTK::GetEffectivePageSize(double *aWidth, double *aHeight)
724 *aWidth = NS_INCHES_TO_INT_TWIPS(gtk_paper_size_get_width(mPaperSize, GTK_UNIT_INCH));
725 *aHeight = NS_INCHES_TO_INT_TWIPS(gtk_paper_size_get_height(mPaperSize, GTK_UNIT_INCH));
727 GtkPageOrientation gtkOrient = gtk_page_setup_get_orientation(mPageSetup);
729 if (gtkOrient == GTK_PAGE_ORIENTATION_LANDSCAPE ||
730 gtkOrient == GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE) {
731 double temp = *aWidth;
732 *aWidth = *aHeight;
733 *aHeight = temp;
735 return NS_OK;
738 NS_IMETHODIMP
739 nsPrintSettingsGTK::SetupSilentPrinting()
741 // We have to get a printer here, rather than when the print settings are constructed.
742 // This is because when we request sync, GTK makes us wait in the *event loop* while waiting
743 // for the enumeration to finish. We must do this when event loop runs are expected.
744 gtk_enumerate_printers(printer_enumerator, this, NULL, TRUE);
746 // XXX If no default printer set, get the first one.
747 if (!GTK_IS_PRINTER(mGTKPrinter))
748 gtk_enumerate_printers(ref_printer, this, NULL, TRUE);
750 return NS_OK;