LCOV - code coverage report
Current view: directory - widget/gtk2 - nsPrintSettingsGTK.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 338 0 0.0 %
Date: 2012-06-02 Functions: 54 0 0.0 %

       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
       4                 :  *
       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/
       9                 :  *
      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.
      14                 :  *
      15                 :  * The Original Code is mozilla.org code.
      16                 :  *
      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.
      20                 :  *
      21                 :  * Contributor(s):
      22                 :  *   Michael Ventnor <m.ventnor@gmail.com>
      23                 :  *
      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.
      35                 :  *
      36                 :  * ***** END LICENSE BLOCK ***** */
      37                 : 
      38                 : #include "nsPrintSettingsGTK.h"
      39                 : #include "nsILocalFile.h"
      40                 : #include "nsNetUtil.h"
      41                 : #include <stdlib.h>
      42                 : 
      43                 : static
      44               0 : gboolean ref_printer(GtkPrinter *aPrinter, gpointer aData)
      45                 : {
      46               0 :   ((nsPrintSettingsGTK*) aData)->SetGtkPrinter(aPrinter);
      47               0 :   return TRUE;
      48                 : }
      49                 : 
      50                 : static
      51               0 : gboolean printer_enumerator(GtkPrinter *aPrinter, gpointer aData)
      52                 : {
      53               0 :   if (gtk_printer_is_default(aPrinter))
      54               0 :     return ref_printer(aPrinter, aData);
      55                 : 
      56               0 :   return FALSE; // Keep 'em coming...
      57                 : }
      58                 : 
      59                 : static
      60               0 : GtkPaperSize* moz_gtk_paper_size_copy_to_new_custom(GtkPaperSize* oldPaperSize)
      61                 : {
      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               0 :                                    GTK_UNIT_INCH);
      68                 : }
      69                 : 
      70               0 : NS_IMPL_ISUPPORTS_INHERITED1(nsPrintSettingsGTK, 
      71                 :                              nsPrintSettings, 
      72                 :                              nsPrintSettingsGTK)
      73                 : 
      74                 : /** ---------------------------------------------------
      75                 :  */
      76               0 : nsPrintSettingsGTK::nsPrintSettingsGTK() :
      77                 :   mPageSetup(NULL),
      78                 :   mPrintSettings(NULL),
      79                 :   mGTKPrinter(NULL),
      80               0 :   mPrintSelectionOnly(false)
      81                 : {
      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               0 :   mPrintSettings = gtk_print_settings_new();
      85               0 :   mPageSetup = gtk_page_setup_new();
      86               0 :   InitUnwriteableMargin();
      87                 : 
      88               0 :   SetOutputFormat(nsIPrintSettings::kOutputFormatNative);
      89                 : 
      90               0 :   GtkPaperSize* defaultPaperSize = gtk_paper_size_new(NULL);
      91               0 :   mPaperSize = moz_gtk_paper_size_copy_to_new_custom(defaultPaperSize);
      92               0 :   gtk_paper_size_free(defaultPaperSize);
      93               0 :   SaveNewPageSize();
      94               0 : }
      95                 : 
      96                 : /** ---------------------------------------------------
      97                 :  */
      98               0 : nsPrintSettingsGTK::~nsPrintSettingsGTK()
      99                 : {
     100               0 :   if (mPageSetup) {
     101               0 :     g_object_unref(mPageSetup);
     102               0 :     mPageSetup = NULL;
     103                 :   }
     104               0 :   if (mPrintSettings) {
     105               0 :     g_object_unref(mPrintSettings);
     106               0 :     mPrintSettings = NULL;
     107                 :   }
     108               0 :   if (mGTKPrinter) {
     109               0 :     g_object_unref(mGTKPrinter);
     110               0 :     mGTKPrinter = NULL;
     111                 :   }
     112               0 :   gtk_paper_size_free(mPaperSize);
     113               0 : }
     114                 : 
     115                 : /** ---------------------------------------------------
     116                 :  */
     117               0 : nsPrintSettingsGTK::nsPrintSettingsGTK(const nsPrintSettingsGTK& aPS) :
     118                 :   mPageSetup(NULL),
     119                 :   mPrintSettings(NULL),
     120                 :   mGTKPrinter(NULL),
     121               0 :   mPrintSelectionOnly(false)
     122                 : {
     123               0 :   *this = aPS;
     124               0 : }
     125                 : 
     126                 : /** ---------------------------------------------------
     127                 :  */
     128               0 : nsPrintSettingsGTK& nsPrintSettingsGTK::operator=(const nsPrintSettingsGTK& rhs)
     129                 : {
     130               0 :   if (this == &rhs) {
     131               0 :     return *this;
     132                 :   }
     133                 :   
     134               0 :   nsPrintSettings::operator=(rhs);
     135                 : 
     136               0 :   if (mPageSetup)
     137               0 :     g_object_unref(mPageSetup);
     138               0 :   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=.
     142                 : 
     143               0 :   if (mPrintSettings)
     144               0 :     g_object_unref(mPrintSettings);
     145               0 :   mPrintSettings = gtk_print_settings_copy(rhs.mPrintSettings);
     146                 : 
     147               0 :   if (mGTKPrinter)
     148               0 :     g_object_unref(mGTKPrinter);
     149               0 :   mGTKPrinter = (GtkPrinter*) g_object_ref(rhs.mGTKPrinter);
     150                 : 
     151               0 :   mPrintSelectionOnly = rhs.mPrintSelectionOnly;
     152                 : 
     153               0 :   return *this;
     154                 : }
     155                 : 
     156                 : /** -------------------------------------------
     157                 :  */
     158               0 : nsresult nsPrintSettingsGTK::_Clone(nsIPrintSettings **_retval)
     159                 : {
     160               0 :   NS_ENSURE_ARG_POINTER(_retval);
     161               0 :   *_retval = nsnull;
     162                 :   
     163               0 :   nsPrintSettingsGTK *newSettings = new nsPrintSettingsGTK(*this);
     164               0 :   if (!newSettings)
     165               0 :     return NS_ERROR_FAILURE;
     166               0 :   *_retval = newSettings;
     167               0 :   NS_ADDREF(*_retval);
     168               0 :   return NS_OK;
     169                 : }
     170                 : 
     171                 : 
     172                 : /** -------------------------------------------
     173                 :  */
     174                 : NS_IMETHODIMP
     175               0 : nsPrintSettingsGTK::_Assign(nsIPrintSettings *aPS)
     176                 : {
     177               0 :   nsPrintSettingsGTK *printSettingsGTK = static_cast<nsPrintSettingsGTK*>(aPS);
     178               0 :   if (!printSettingsGTK)
     179               0 :     return NS_ERROR_UNEXPECTED;
     180               0 :   *this = *printSettingsGTK;
     181               0 :   return NS_OK;
     182                 : }
     183                 : 
     184                 : /** ---------------------------------------------------
     185                 :  */
     186                 : void
     187               0 : nsPrintSettingsGTK::SetGtkPageSetup(GtkPageSetup *aPageSetup)
     188                 : {
     189               0 :   if (mPageSetup)
     190               0 :     g_object_unref(mPageSetup);
     191                 :   
     192               0 :   mPageSetup = (GtkPageSetup*) g_object_ref(aPageSetup);
     193               0 :   InitUnwriteableMargin();
     194                 : 
     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               0 :   GtkPaperSize* newPaperSize = gtk_page_setup_get_paper_size(aPageSetup);
     198               0 :   if (newPaperSize) { // Yes, this can be null
     199               0 :     gtk_paper_size_free(mPaperSize);
     200               0 :     mPaperSize = moz_gtk_paper_size_copy_to_new_custom(newPaperSize);
     201                 :   }
     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               0 :   SaveNewPageSize();
     205               0 : }
     206                 : 
     207                 : /** ---------------------------------------------------
     208                 :  */
     209                 : void
     210               0 : nsPrintSettingsGTK::SetGtkPrintSettings(GtkPrintSettings *aPrintSettings)
     211                 : {
     212               0 :   if (mPrintSettings)
     213               0 :     g_object_unref(mPrintSettings);
     214                 :   
     215               0 :   mPrintSettings = (GtkPrintSettings*) g_object_ref(aPrintSettings);
     216                 : 
     217               0 :   GtkPaperSize* newPaperSize = gtk_print_settings_get_paper_size(aPrintSettings);
     218               0 :   if (newPaperSize) {
     219               0 :     gtk_paper_size_free(mPaperSize);
     220               0 :     mPaperSize = moz_gtk_paper_size_copy_to_new_custom(newPaperSize);
     221                 :   }
     222               0 :   SaveNewPageSize();
     223               0 : }
     224                 : 
     225                 : /** ---------------------------------------------------
     226                 :  */
     227                 : void
     228               0 : nsPrintSettingsGTK::SetGtkPrinter(GtkPrinter *aPrinter)
     229                 : {
     230               0 :   if (mGTKPrinter)
     231               0 :     g_object_unref(mGTKPrinter);
     232                 :   
     233               0 :   mGTKPrinter = (GtkPrinter*) g_object_ref(aPrinter);
     234               0 : }
     235                 : 
     236                 : /**
     237                 :  * Reimplementation of nsPrintSettings functions so that we get the values
     238                 :  * from the GTK objects rather than our own variables.
     239                 :  */
     240                 : 
     241                 : /* attribute long printRange; */
     242               0 : NS_IMETHODIMP nsPrintSettingsGTK::GetPrintRange(PRInt16 *aPrintRange)
     243                 : {
     244               0 :   NS_ENSURE_ARG_POINTER(aPrintRange);
     245               0 :   if (mPrintSelectionOnly) {
     246               0 :     *aPrintRange = kRangeSelection;
     247               0 :     return NS_OK;
     248                 :   }
     249                 : 
     250               0 :   GtkPrintPages gtkRange = gtk_print_settings_get_print_pages(mPrintSettings);
     251               0 :   if (gtkRange == GTK_PRINT_PAGES_RANGES)
     252               0 :     *aPrintRange = kRangeSpecifiedPageRange;
     253                 :   else
     254               0 :     *aPrintRange = kRangeAllPages;
     255                 : 
     256               0 :   return NS_OK;
     257                 : }
     258               0 : NS_IMETHODIMP nsPrintSettingsGTK::SetPrintRange(PRInt16 aPrintRange)
     259                 : {
     260               0 :   if (aPrintRange == kRangeSelection) {
     261               0 :     mPrintSelectionOnly = true;
     262               0 :     return NS_OK;
     263                 :   }
     264                 : 
     265               0 :   mPrintSelectionOnly = false;
     266               0 :   if (aPrintRange == kRangeSpecifiedPageRange)
     267               0 :     gtk_print_settings_set_print_pages(mPrintSettings, GTK_PRINT_PAGES_RANGES);
     268                 :   else
     269               0 :     gtk_print_settings_set_print_pages(mPrintSettings, GTK_PRINT_PAGES_ALL);
     270               0 :   return NS_OK;
     271                 : }
     272                 : 
     273                 : /* attribute long startPageRange; */
     274                 : NS_IMETHODIMP
     275               0 : nsPrintSettingsGTK::GetStartPageRange(PRInt32 *aStartPageRange)
     276                 : {
     277                 :   gint ctRanges;
     278               0 :   GtkPageRange* lstRanges = gtk_print_settings_get_page_ranges(mPrintSettings, &ctRanges);
     279                 : 
     280                 :   // Make sure we got a range.
     281               0 :   if (ctRanges < 1) {
     282               0 :     *aStartPageRange = 1;
     283                 :   } else {
     284                 :     // GTK supports multiple page ranges; gecko only supports 1. So find
     285                 :     // the lowest start page.
     286               0 :     PRInt32 start(lstRanges[0].start);
     287               0 :     for (gint ii = 1; ii < ctRanges; ii++) {
     288               0 :       start = NS_MIN(lstRanges[ii].start, start);
     289                 :     }
     290               0 :     *aStartPageRange = start + 1;
     291                 :   }
     292                 : 
     293               0 :   g_free(lstRanges);
     294               0 :   return NS_OK;
     295                 : }
     296                 : NS_IMETHODIMP
     297               0 : nsPrintSettingsGTK::SetStartPageRange(PRInt32 aStartPageRange)
     298                 : {
     299                 :   PRInt32 endRange;
     300               0 :   GetEndPageRange(&endRange);
     301                 : 
     302                 :   GtkPageRange gtkRange;
     303               0 :   gtkRange.start = aStartPageRange - 1;
     304               0 :   gtkRange.end = endRange - 1;
     305                 : 
     306               0 :   gtk_print_settings_set_page_ranges(mPrintSettings, &gtkRange, 1);
     307                 : 
     308               0 :   return NS_OK;
     309                 : }
     310                 : 
     311                 : /* attribute long endPageRange; */
     312                 : NS_IMETHODIMP
     313               0 : nsPrintSettingsGTK::GetEndPageRange(PRInt32 *aEndPageRange)
     314                 : {
     315                 :   gint ctRanges;
     316               0 :   GtkPageRange* lstRanges = gtk_print_settings_get_page_ranges(mPrintSettings, &ctRanges);
     317                 : 
     318               0 :   if (ctRanges < 1) {
     319               0 :     *aEndPageRange = 1;
     320                 :   } else {
     321               0 :     PRInt32 end(lstRanges[0].end);
     322               0 :     for (gint ii = 1; ii < ctRanges; ii++) {
     323               0 :       end = NS_MAX(lstRanges[ii].end, end);
     324                 :     }
     325               0 :     *aEndPageRange = end + 1;
     326                 :   }
     327                 : 
     328               0 :   g_free(lstRanges);
     329               0 :   return NS_OK;
     330                 : }
     331                 : NS_IMETHODIMP
     332               0 : nsPrintSettingsGTK::SetEndPageRange(PRInt32 aEndPageRange)
     333                 : {
     334                 :   PRInt32 startRange;
     335               0 :   GetStartPageRange(&startRange);
     336                 : 
     337                 :   GtkPageRange gtkRange;
     338               0 :   gtkRange.start = startRange - 1;
     339               0 :   gtkRange.end = aEndPageRange - 1;
     340                 : 
     341               0 :   gtk_print_settings_set_page_ranges(mPrintSettings, &gtkRange, 1);
     342                 : 
     343               0 :   return NS_OK;
     344                 : }
     345                 : 
     346                 : /* attribute boolean printReversed; */
     347                 : NS_IMETHODIMP
     348               0 : nsPrintSettingsGTK::GetPrintReversed(bool *aPrintReversed)
     349                 : {
     350               0 :   *aPrintReversed = gtk_print_settings_get_reverse(mPrintSettings);
     351               0 :   return NS_OK;
     352                 : }
     353                 : NS_IMETHODIMP
     354               0 : nsPrintSettingsGTK::SetPrintReversed(bool aPrintReversed)
     355                 : {
     356               0 :   gtk_print_settings_set_reverse(mPrintSettings, aPrintReversed);
     357               0 :   return NS_OK;
     358                 : }
     359                 : 
     360                 : /* attribute boolean printInColor; */
     361                 : NS_IMETHODIMP
     362               0 : nsPrintSettingsGTK::GetPrintInColor(bool *aPrintInColor)
     363                 : {
     364               0 :   *aPrintInColor = gtk_print_settings_get_use_color(mPrintSettings);
     365               0 :   return NS_OK;
     366                 : }
     367                 : NS_IMETHODIMP
     368               0 : nsPrintSettingsGTK::SetPrintInColor(bool aPrintInColor)
     369                 : {
     370               0 :   gtk_print_settings_set_use_color(mPrintSettings, aPrintInColor);
     371               0 :   return NS_OK;
     372                 : }
     373                 : 
     374                 : /* attribute short orientation; */
     375                 : NS_IMETHODIMP
     376               0 : nsPrintSettingsGTK::GetOrientation(PRInt32 *aOrientation)
     377                 : {
     378               0 :   NS_ENSURE_ARG_POINTER(aOrientation);
     379                 : 
     380               0 :   GtkPageOrientation gtkOrient = gtk_page_setup_get_orientation(mPageSetup);
     381               0 :   switch (gtkOrient) {
     382                 :     case GTK_PAGE_ORIENTATION_LANDSCAPE:
     383                 :     case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
     384               0 :       *aOrientation = kLandscapeOrientation;
     385               0 :       break;
     386                 : 
     387                 :     case GTK_PAGE_ORIENTATION_PORTRAIT:
     388                 :     case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
     389                 :     default:
     390               0 :       *aOrientation = kPortraitOrientation;
     391                 :   }
     392               0 :   return NS_OK;
     393                 : }
     394                 : NS_IMETHODIMP
     395               0 : nsPrintSettingsGTK::SetOrientation(PRInt32 aOrientation)
     396                 : {
     397                 :   GtkPageOrientation gtkOrient;
     398               0 :   if (aOrientation == kLandscapeOrientation)
     399               0 :     gtkOrient = GTK_PAGE_ORIENTATION_LANDSCAPE;
     400                 :   else
     401               0 :     gtkOrient = GTK_PAGE_ORIENTATION_PORTRAIT;
     402                 : 
     403               0 :   gtk_print_settings_set_orientation(mPrintSettings, gtkOrient);
     404               0 :   gtk_page_setup_set_orientation(mPageSetup, gtkOrient);
     405               0 :   return NS_OK;
     406                 : }
     407                 : 
     408                 : /* attribute wstring toFileName; */
     409                 : NS_IMETHODIMP
     410               0 : nsPrintSettingsGTK::GetToFileName(PRUnichar * *aToFileName)
     411                 : {
     412                 :   // Get the gtk output filename
     413               0 :   const char* gtk_output_uri = gtk_print_settings_get(mPrintSettings, GTK_PRINT_SETTINGS_OUTPUT_URI);
     414               0 :   if (!gtk_output_uri) {
     415               0 :     *aToFileName = ToNewUnicode(mToFileName);
     416               0 :     return NS_OK;
     417                 :   }
     418                 : 
     419                 :   // Convert to an nsIFile
     420               0 :   nsCOMPtr<nsIFile> file;
     421               0 :   nsresult rv = NS_GetFileFromURLSpec(nsDependentCString(gtk_output_uri),
     422               0 :                                       getter_AddRefs(file));
     423               0 :   if (NS_FAILED(rv))
     424               0 :     return rv;
     425                 : 
     426                 :   // Extract the path
     427               0 :   nsAutoString path;
     428               0 :   rv = file->GetPath(path);
     429               0 :   NS_ENSURE_SUCCESS(rv, rv);
     430                 : 
     431               0 :   *aToFileName = ToNewUnicode(path);
     432               0 :   return NS_OK;
     433                 : }
     434                 : 
     435                 : NS_IMETHODIMP
     436               0 : nsPrintSettingsGTK::SetToFileName(const PRUnichar * aToFileName)
     437                 : {
     438               0 :   if (aToFileName[0] == 0) {
     439               0 :     mToFileName.SetLength(0);
     440               0 :     gtk_print_settings_set(mPrintSettings, GTK_PRINT_SETTINGS_OUTPUT_URI, NULL);
     441               0 :     return NS_OK;
     442                 :   }
     443                 : 
     444               0 :   if (StringEndsWith(nsDependentString(aToFileName), NS_LITERAL_STRING(".ps"))) {
     445               0 :     gtk_print_settings_set(mPrintSettings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, "ps");
     446                 :   } else {
     447               0 :     gtk_print_settings_set(mPrintSettings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, "pdf");
     448                 :   }
     449                 : 
     450               0 :   nsCOMPtr<nsILocalFile> file;
     451               0 :   nsresult rv = NS_NewLocalFile(nsDependentString(aToFileName), true,
     452               0 :                                 getter_AddRefs(file));
     453               0 :   NS_ENSURE_SUCCESS(rv, rv);
     454                 : 
     455                 :   // Convert the nsIFile to a URL
     456               0 :   nsCAutoString url;
     457               0 :   rv = NS_GetURLSpecFromFile(file, url);
     458               0 :   NS_ENSURE_SUCCESS(rv, rv);
     459                 : 
     460               0 :   gtk_print_settings_set(mPrintSettings, GTK_PRINT_SETTINGS_OUTPUT_URI, url.get());
     461               0 :   mToFileName = aToFileName;
     462                 : 
     463               0 :   return NS_OK;
     464                 : }
     465                 : 
     466                 : NS_IMETHODIMP
     467               0 : nsPrintSettingsGTK::GetPrinterName(PRUnichar * *aPrinter)
     468                 : {
     469               0 :   const char* gtkPrintName = gtk_print_settings_get_printer(mPrintSettings);
     470               0 :   if (!gtkPrintName) {
     471               0 :     if (GTK_IS_PRINTER(mGTKPrinter)) {
     472               0 :       gtkPrintName = gtk_printer_get_name(mGTKPrinter);
     473                 :     } else {
     474                 :       // This mimics what nsPrintSettingsImpl does when we try to Get before we Set
     475               0 :       nsXPIDLString nullPrintName;
     476               0 :       *aPrinter = ToNewUnicode(nullPrintName);
     477               0 :       return NS_OK;
     478                 :     }
     479                 :   }
     480               0 :   *aPrinter = ToNewUnicode(nsDependentCString(gtkPrintName));
     481               0 :   return NS_OK;
     482                 : }
     483                 : 
     484                 : NS_IMETHODIMP
     485               0 : nsPrintSettingsGTK::SetPrinterName(const PRUnichar * aPrinter)
     486                 : {
     487               0 :   NS_ConvertUTF16toUTF8 gtkPrinter(aPrinter);
     488                 : 
     489               0 :   if (StringBeginsWith(gtkPrinter, NS_LITERAL_CSTRING("CUPS/"))) {
     490                 :     // Strip off "CUPS/"; GTK might recognize the rest
     491               0 :     gtkPrinter.Cut(0, strlen("CUPS/"));
     492                 :   }
     493                 : 
     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               0 :   const char* oldPrinterName = gtk_print_settings_get_printer(mPrintSettings);
     499               0 :   if (!oldPrinterName || !gtkPrinter.Equals(oldPrinterName)) {
     500               0 :     mIsInitedFromPrinter = false;
     501               0 :     mIsInitedFromPrefs = false;
     502               0 :     gtk_print_settings_set_printer(mPrintSettings, gtkPrinter.get());
     503                 :   }
     504                 : 
     505               0 :   return NS_OK;
     506                 : }
     507                 : 
     508                 : /* attribute long numCopies; */
     509                 : NS_IMETHODIMP
     510               0 : nsPrintSettingsGTK::GetNumCopies(PRInt32 *aNumCopies)
     511                 : {
     512               0 :   NS_ENSURE_ARG_POINTER(aNumCopies);
     513               0 :   *aNumCopies = gtk_print_settings_get_n_copies(mPrintSettings);
     514               0 :   return NS_OK;
     515                 : }
     516                 : NS_IMETHODIMP
     517               0 : nsPrintSettingsGTK::SetNumCopies(PRInt32 aNumCopies)
     518                 : {
     519               0 :   gtk_print_settings_set_n_copies(mPrintSettings, aNumCopies);
     520               0 :   return NS_OK;
     521                 : }
     522                 : 
     523                 : /* attribute double scaling; */
     524                 : NS_IMETHODIMP
     525               0 : nsPrintSettingsGTK::GetScaling(double *aScaling)
     526                 : {
     527               0 :   *aScaling = gtk_print_settings_get_scale(mPrintSettings) / 100.0;
     528               0 :   return NS_OK;
     529                 : }
     530                 : 
     531                 : NS_IMETHODIMP
     532               0 : nsPrintSettingsGTK::SetScaling(double aScaling)
     533                 : {
     534               0 :   gtk_print_settings_set_scale(mPrintSettings, aScaling * 100.0);
     535               0 :   return NS_OK;
     536                 : }
     537                 : 
     538                 : /* attribute wstring paperName; */
     539                 : NS_IMETHODIMP
     540               0 : nsPrintSettingsGTK::GetPaperName(PRUnichar * *aPaperName)
     541                 : {
     542               0 :   NS_ENSURE_ARG_POINTER(aPaperName);
     543               0 :   *aPaperName = ToNewUnicode(NS_ConvertUTF8toUTF16(gtk_paper_size_get_name(mPaperSize)));
     544               0 :   return NS_OK;
     545                 : }
     546                 : NS_IMETHODIMP
     547               0 : nsPrintSettingsGTK::SetPaperName(const PRUnichar * aPaperName)
     548                 : {
     549               0 :   NS_ConvertUTF16toUTF8 gtkPaperName(aPaperName);
     550                 : 
     551                 :   // Convert these Gecko names to GTK names
     552               0 :   if (gtkPaperName.EqualsIgnoreCase("letter"))
     553               0 :     gtkPaperName.AssignLiteral(GTK_PAPER_NAME_LETTER);
     554               0 :   else if (gtkPaperName.EqualsIgnoreCase("legal"))
     555               0 :     gtkPaperName.AssignLiteral(GTK_PAPER_NAME_LEGAL);
     556                 : 
     557                 :   // Try to get the display name from the name so our paper size fits in the Page Setup dialog.
     558               0 :   GtkPaperSize* paperSize = gtk_paper_size_new(gtkPaperName.get());
     559               0 :   char* displayName = strdup(gtk_paper_size_get_display_name(paperSize));
     560               0 :   gtk_paper_size_free(paperSize);
     561                 : 
     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               0 :                                         GTK_UNIT_INCH);
     566                 : 
     567               0 :   free(displayName);
     568               0 :   gtk_paper_size_free(mPaperSize);
     569               0 :   mPaperSize = paperSize;
     570               0 :   SaveNewPageSize();
     571               0 :   return NS_OK;
     572                 : }
     573                 : 
     574                 : GtkUnit
     575               0 : nsPrintSettingsGTK::GetGTKUnit(PRInt16 aGeckoUnit)
     576                 : {
     577               0 :   if (aGeckoUnit == kPaperSizeMillimeters)
     578               0 :     return GTK_UNIT_MM;
     579                 :   else
     580               0 :     return GTK_UNIT_INCH;
     581                 : }
     582                 : 
     583                 : void
     584               0 : nsPrintSettingsGTK::SaveNewPageSize()
     585                 : {
     586               0 :   gtk_print_settings_set_paper_size(mPrintSettings, mPaperSize);
     587               0 :   gtk_page_setup_set_paper_size(mPageSetup, mPaperSize);
     588               0 : }
     589                 : 
     590                 : void
     591               0 : nsPrintSettingsGTK::InitUnwriteableMargin()
     592                 : {
     593                 :   mUnwriteableMargin.SizeTo(
     594               0 :    NS_INCHES_TO_INT_TWIPS(gtk_page_setup_get_left_margin(mPageSetup, GTK_UNIT_INCH)),
     595               0 :    NS_INCHES_TO_INT_TWIPS(gtk_page_setup_get_top_margin(mPageSetup, GTK_UNIT_INCH)),
     596               0 :    NS_INCHES_TO_INT_TWIPS(gtk_page_setup_get_right_margin(mPageSetup, GTK_UNIT_INCH)),
     597               0 :    NS_INCHES_TO_INT_TWIPS(gtk_page_setup_get_bottom_margin(mPageSetup, GTK_UNIT_INCH))
     598               0 :   );
     599               0 : }
     600                 : 
     601                 : /**
     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.)
     606                 :  *
     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.)
     612                 :  */
     613                 : NS_IMETHODIMP 
     614               0 : nsPrintSettingsGTK::SetUnwriteableMarginInTwips(nsIntMargin& aUnwriteableMargin)
     615                 : {
     616               0 :   nsPrintSettings::SetUnwriteableMarginInTwips(aUnwriteableMargin);
     617                 :   gtk_page_setup_set_top_margin(mPageSetup,
     618               0 :            NS_TWIPS_TO_INCHES(mUnwriteableMargin.top), GTK_UNIT_INCH);
     619                 :   gtk_page_setup_set_left_margin(mPageSetup,
     620               0 :            NS_TWIPS_TO_INCHES(mUnwriteableMargin.left), GTK_UNIT_INCH);
     621                 :   gtk_page_setup_set_bottom_margin(mPageSetup,
     622               0 :            NS_TWIPS_TO_INCHES(mUnwriteableMargin.bottom), GTK_UNIT_INCH);
     623                 :   gtk_page_setup_set_right_margin(mPageSetup,
     624               0 :            NS_TWIPS_TO_INCHES(mUnwriteableMargin.right), GTK_UNIT_INCH);
     625               0 :   return NS_OK;
     626                 : }
     627                 : 
     628                 : /* attribute double unwriteableMarginTop; */
     629                 : NS_IMETHODIMP
     630               0 : nsPrintSettingsGTK::SetUnwriteableMarginTop(double aUnwriteableMarginTop)
     631                 : {
     632               0 :   nsPrintSettings::SetUnwriteableMarginTop(aUnwriteableMarginTop);
     633                 :   gtk_page_setup_set_top_margin(mPageSetup,
     634               0 :            NS_TWIPS_TO_INCHES(mUnwriteableMargin.top), GTK_UNIT_INCH);
     635               0 :   return NS_OK;
     636                 : }
     637                 : 
     638                 : /* attribute double unwriteableMarginLeft; */
     639                 : NS_IMETHODIMP
     640               0 : nsPrintSettingsGTK::SetUnwriteableMarginLeft(double aUnwriteableMarginLeft)
     641                 : {
     642               0 :   nsPrintSettings::SetUnwriteableMarginLeft(aUnwriteableMarginLeft);
     643                 :   gtk_page_setup_set_left_margin(mPageSetup,
     644               0 :            NS_TWIPS_TO_INCHES(mUnwriteableMargin.left), GTK_UNIT_INCH);
     645               0 :   return NS_OK;
     646                 : }
     647                 : 
     648                 : /* attribute double unwriteableMarginBottom; */
     649                 : NS_IMETHODIMP
     650               0 : nsPrintSettingsGTK::SetUnwriteableMarginBottom(double aUnwriteableMarginBottom)
     651                 : {
     652               0 :   nsPrintSettings::SetUnwriteableMarginBottom(aUnwriteableMarginBottom);
     653                 :   gtk_page_setup_set_bottom_margin(mPageSetup,
     654               0 :            NS_TWIPS_TO_INCHES(mUnwriteableMargin.bottom), GTK_UNIT_INCH);
     655               0 :   return NS_OK;
     656                 : }
     657                 : 
     658                 : /* attribute double unwriteableMarginRight; */
     659                 : NS_IMETHODIMP
     660               0 : nsPrintSettingsGTK::SetUnwriteableMarginRight(double aUnwriteableMarginRight)
     661                 : {
     662               0 :   nsPrintSettings::SetUnwriteableMarginRight(aUnwriteableMarginRight);
     663                 :   gtk_page_setup_set_right_margin(mPageSetup,
     664               0 :            NS_TWIPS_TO_INCHES(mUnwriteableMargin.right), GTK_UNIT_INCH);
     665               0 :   return NS_OK;
     666                 : }
     667                 : 
     668                 : /* attribute double paperWidth; */
     669                 : NS_IMETHODIMP
     670               0 : nsPrintSettingsGTK::GetPaperWidth(double *aPaperWidth)
     671                 : {
     672               0 :   NS_ENSURE_ARG_POINTER(aPaperWidth);
     673               0 :   *aPaperWidth = gtk_paper_size_get_width(mPaperSize, GetGTKUnit(mPaperSizeUnit));
     674               0 :   return NS_OK;
     675                 : }
     676                 : NS_IMETHODIMP
     677               0 : nsPrintSettingsGTK::SetPaperWidth(double aPaperWidth)
     678                 : {
     679                 :   gtk_paper_size_set_size(mPaperSize,
     680                 :                           aPaperWidth,
     681                 :                           gtk_paper_size_get_height(mPaperSize, GetGTKUnit(mPaperSizeUnit)),
     682               0 :                           GetGTKUnit(mPaperSizeUnit));
     683               0 :   SaveNewPageSize();
     684               0 :   return NS_OK;
     685                 : }
     686                 : 
     687                 : /* attribute double paperHeight; */
     688                 : NS_IMETHODIMP
     689               0 : nsPrintSettingsGTK::GetPaperHeight(double *aPaperHeight)
     690                 : {
     691               0 :   NS_ENSURE_ARG_POINTER(aPaperHeight);
     692               0 :   *aPaperHeight = gtk_paper_size_get_height(mPaperSize, GetGTKUnit(mPaperSizeUnit));
     693               0 :   return NS_OK;
     694                 : }
     695                 : NS_IMETHODIMP
     696               0 : nsPrintSettingsGTK::SetPaperHeight(double aPaperHeight)
     697                 : {
     698                 :   gtk_paper_size_set_size(mPaperSize,
     699                 :                           gtk_paper_size_get_width(mPaperSize, GetGTKUnit(mPaperSizeUnit)),
     700                 :                           aPaperHeight,
     701               0 :                           GetGTKUnit(mPaperSizeUnit));
     702               0 :   SaveNewPageSize();
     703               0 :   return NS_OK;
     704                 : }
     705                 : 
     706                 : NS_IMETHODIMP
     707               0 : nsPrintSettingsGTK::SetPaperSizeUnit(PRInt16 aPaperSizeUnit)
     708                 : {
     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               0 :                           GetGTKUnit(aPaperSizeUnit));
     715               0 :   SaveNewPageSize();
     716                 : 
     717               0 :   mPaperSizeUnit = aPaperSizeUnit;
     718               0 :   return NS_OK;
     719                 : }
     720                 : 
     721                 : NS_IMETHODIMP
     722               0 : nsPrintSettingsGTK::GetEffectivePageSize(double *aWidth, double *aHeight)
     723                 : {
     724               0 :   *aWidth  = NS_INCHES_TO_INT_TWIPS(gtk_paper_size_get_width(mPaperSize, GTK_UNIT_INCH));
     725               0 :   *aHeight = NS_INCHES_TO_INT_TWIPS(gtk_paper_size_get_height(mPaperSize, GTK_UNIT_INCH));
     726                 : 
     727               0 :   GtkPageOrientation gtkOrient = gtk_page_setup_get_orientation(mPageSetup);
     728                 : 
     729               0 :   if (gtkOrient == GTK_PAGE_ORIENTATION_LANDSCAPE ||
     730                 :       gtkOrient == GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE) {
     731               0 :     double temp = *aWidth;
     732               0 :     *aWidth = *aHeight;
     733               0 :     *aHeight = temp;
     734                 :   }
     735               0 :   return NS_OK;
     736                 : }
     737                 : 
     738                 : NS_IMETHODIMP
     739               0 : nsPrintSettingsGTK::SetupSilentPrinting()
     740                 : {
     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               0 :   gtk_enumerate_printers(printer_enumerator, this, NULL, TRUE);
     745                 : 
     746                 :   // XXX If no default printer set, get the first one.
     747               0 :   if (!GTK_IS_PRINTER(mGTKPrinter))
     748               0 :     gtk_enumerate_printers(ref_printer, this, NULL, TRUE);
     749                 : 
     750               0 :   return NS_OK;
     751                 : }
     752                 : 
     753                 : NS_IMETHODIMP
     754               0 : nsPrintSettingsGTK::GetPageRanges(nsTArray<PRInt32> &aPages)
     755                 : {
     756                 :   gint ctRanges;
     757               0 :   GtkPageRange* lstRanges = gtk_print_settings_get_page_ranges(mPrintSettings, &ctRanges);
     758                 : 
     759               0 :   aPages.Clear();
     760                 : 
     761               0 :   if (ctRanges > 1) {
     762               0 :     for (gint i = 0; i < ctRanges; i++) {
     763               0 :       aPages.AppendElement(lstRanges[i].start+1);
     764               0 :       aPages.AppendElement(lstRanges[i].end+1);
     765                 :     }
     766                 :   }
     767                 : 
     768               0 :   g_free(lstRanges);
     769               0 :   return NS_OK;
     770                 : }
     771                 : 

Generated by: LCOV version 1.7