bug 1227008 use GtkPaperSize in GtkPageSetup directly, instead of maintaining a separate copy r=acomminos

Fixes a leak in nsPrintSettingsGTK::SetGtkPrintSettings() and
missed paper size copy in operator= (bug 965991).

GtkPageSetup always has a GtkPaperSize [1], which is returned from
gtk_page_setup_get_paper_size() as indicated in the docs [2]:
"Return value: the paper size" without "or NULL".

[1] https://git.gnome.org/browse/gtk+/tree/gtk/gtkpagesetup.c?h=2.18.0#n68
[2] https://git.gnome.org/browse/gtk+/tree/gtk/gtkpagesetup.c?h=2.18.0#n164

--HG--
extra : rebase_source : c554c9cb2c30380301fb4286efb9b52497c10d9d
This commit is contained in:
Karl Tomlinson 2015-11-25 11:07:21 +13:00
parent e486b02c34
commit 42c19dece3
3 changed files with 59 additions and 46 deletions

View File

@ -305,6 +305,7 @@ STUB(gtk_paper_size_get_display_name)
STUB(gtk_paper_size_get_height)
STUB(gtk_paper_size_get_name)
STUB(gtk_paper_size_get_width)
STUB(gtk_paper_size_is_custom)
STUB(gtk_paper_size_is_equal)
STUB(gtk_paper_size_new)
STUB(gtk_paper_size_new_custom)

View File

@ -51,15 +51,11 @@ nsPrintSettingsGTK::nsPrintSettingsGTK() :
// The aim here is to set up the objects enough that silent printing works well.
// These will be replaced anyway if the print dialog is used.
mPrintSettings = gtk_print_settings_new();
mPageSetup = gtk_page_setup_new();
InitUnwriteableMargin();
GtkPageSetup* pageSetup = gtk_page_setup_new();
SetGtkPageSetup(pageSetup);
g_object_unref(pageSetup);
SetOutputFormat(nsIPrintSettings::kOutputFormatNative);
GtkPaperSize* defaultPaperSize = gtk_paper_size_new(nullptr);
mPaperSize = moz_gtk_paper_size_copy_to_new_custom(defaultPaperSize);
gtk_paper_size_free(defaultPaperSize);
SaveNewPageSize();
}
/** ---------------------------------------------------
@ -78,7 +74,6 @@ nsPrintSettingsGTK::~nsPrintSettingsGTK()
g_object_unref(mGTKPrinter);
mGTKPrinter = nullptr;
}
gtk_paper_size_free(mPaperSize);
}
/** ---------------------------------------------------
@ -161,15 +156,16 @@ nsPrintSettingsGTK::SetGtkPageSetup(GtkPageSetup *aPageSetup)
mPageSetup = (GtkPageSetup*) g_object_ref(aPageSetup);
InitUnwriteableMargin();
// We make a custom copy of the GtkPaperSize so it can be mutable. If a
// GtkPaperSize wasn't made as custom, its properties are immutable.
GtkPaperSize* newPaperSize = gtk_page_setup_get_paper_size(aPageSetup);
if (newPaperSize) { // Yes, this can be null
gtk_paper_size_free(mPaperSize);
mPaperSize = moz_gtk_paper_size_copy_to_new_custom(newPaperSize);
// If the paper size is not custom, then we make a custom copy of the
// GtkPaperSize, so it can be mutable. If a GtkPaperSize wasn't made as
// custom, its properties are immutable.
GtkPaperSize* paperSize = gtk_page_setup_get_paper_size(aPageSetup);
if (!gtk_paper_size_is_custom(paperSize)) {
GtkPaperSize* customPaperSize =
moz_gtk_paper_size_copy_to_new_custom(paperSize);
gtk_page_setup_set_paper_size(mPageSetup, customPaperSize);
gtk_paper_size_free(customPaperSize);
}
// If newPaperSize was not null, we must update our twin too (GtkPrintSettings).
// If newPaperSize was null, we must set this object to use mPaperSize.
SaveNewPageSize();
}
@ -183,12 +179,18 @@ nsPrintSettingsGTK::SetGtkPrintSettings(GtkPrintSettings *aPrintSettings)
mPrintSettings = (GtkPrintSettings*) g_object_ref(aPrintSettings);
GtkPaperSize* newPaperSize = gtk_print_settings_get_paper_size(aPrintSettings);
if (newPaperSize) {
gtk_paper_size_free(mPaperSize);
mPaperSize = moz_gtk_paper_size_copy_to_new_custom(newPaperSize);
GtkPaperSize* paperSize = gtk_print_settings_get_paper_size(aPrintSettings);
if (paperSize) {
GtkPaperSize* customPaperSize =
moz_gtk_paper_size_copy_to_new_custom(paperSize);
gtk_paper_size_free(paperSize);
gtk_page_setup_set_paper_size(mPageSetup, customPaperSize);
gtk_paper_size_free(customPaperSize);
} else {
// paperSize was null, and so we add the paper size in the GtkPageSetup to
// the settings.
SaveNewPageSize();
}
SaveNewPageSize();
}
/** ---------------------------------------------------
@ -538,7 +540,9 @@ NS_IMETHODIMP
nsPrintSettingsGTK::GetPaperName(char16_t * *aPaperName)
{
NS_ENSURE_ARG_POINTER(aPaperName);
*aPaperName = ToNewUnicode(NS_ConvertUTF8toUTF16(gtk_paper_size_get_name(mPaperSize)));
const gchar* name =
gtk_paper_size_get_name(gtk_page_setup_get_paper_size(mPageSetup));
*aPaperName = ToNewUnicode(NS_ConvertUTF8toUTF16(name));
return NS_OK;
}
NS_IMETHODIMP
@ -552,19 +556,20 @@ nsPrintSettingsGTK::SetPaperName(const char16_t * aPaperName)
else if (gtkPaperName.EqualsIgnoreCase("legal"))
gtkPaperName.AssignLiteral(GTK_PAPER_NAME_LEGAL);
GtkPaperSize* oldPaperSize = gtk_page_setup_get_paper_size(mPageSetup);
gdouble width = gtk_paper_size_get_width(oldPaperSize, GTK_UNIT_INCH);
gdouble height = gtk_paper_size_get_height(oldPaperSize, GTK_UNIT_INCH);
// Try to get the display name from the name so our paper size fits in the Page Setup dialog.
GtkPaperSize* paperSize = gtk_paper_size_new(gtkPaperName.get());
char* displayName = strdup(gtk_paper_size_get_display_name(paperSize));
GtkPaperSize* customPaperSize =
gtk_paper_size_new_custom(gtkPaperName.get(),
gtk_paper_size_get_display_name(paperSize),
width, height, GTK_UNIT_INCH);
gtk_paper_size_free(paperSize);
paperSize = gtk_paper_size_new_custom(gtkPaperName.get(), displayName,
gtk_paper_size_get_width(mPaperSize, GTK_UNIT_INCH),
gtk_paper_size_get_height(mPaperSize, GTK_UNIT_INCH),
GTK_UNIT_INCH);
free(displayName);
gtk_paper_size_free(mPaperSize);
mPaperSize = paperSize;
gtk_page_setup_set_paper_size(mPageSetup, customPaperSize);
gtk_paper_size_free(customPaperSize);
SaveNewPageSize();
return NS_OK;
}
@ -581,8 +586,8 @@ nsPrintSettingsGTK::GetGTKUnit(int16_t aGeckoUnit)
void
nsPrintSettingsGTK::SaveNewPageSize()
{
gtk_print_settings_set_paper_size(mPrintSettings, mPaperSize);
gtk_page_setup_set_paper_size(mPageSetup, mPaperSize);
gtk_print_settings_set_paper_size(mPrintSettings,
gtk_page_setup_get_paper_size(mPageSetup));
}
void
@ -663,15 +668,18 @@ NS_IMETHODIMP
nsPrintSettingsGTK::GetPaperWidth(double *aPaperWidth)
{
NS_ENSURE_ARG_POINTER(aPaperWidth);
*aPaperWidth = gtk_paper_size_get_width(mPaperSize, GetGTKUnit(mPaperSizeUnit));
GtkPaperSize* paperSize = gtk_page_setup_get_paper_size(mPageSetup);
*aPaperWidth =
gtk_paper_size_get_width(paperSize, GetGTKUnit(mPaperSizeUnit));
return NS_OK;
}
NS_IMETHODIMP
nsPrintSettingsGTK::SetPaperWidth(double aPaperWidth)
{
gtk_paper_size_set_size(mPaperSize,
GtkPaperSize* paperSize = gtk_page_setup_get_paper_size(mPageSetup);
gtk_paper_size_set_size(paperSize,
aPaperWidth,
gtk_paper_size_get_height(mPaperSize, GetGTKUnit(mPaperSizeUnit)),
gtk_paper_size_get_height(paperSize, GetGTKUnit(mPaperSizeUnit)),
GetGTKUnit(mPaperSizeUnit));
SaveNewPageSize();
return NS_OK;
@ -681,14 +689,17 @@ NS_IMETHODIMP
nsPrintSettingsGTK::GetPaperHeight(double *aPaperHeight)
{
NS_ENSURE_ARG_POINTER(aPaperHeight);
*aPaperHeight = gtk_paper_size_get_height(mPaperSize, GetGTKUnit(mPaperSizeUnit));
GtkPaperSize* paperSize = gtk_page_setup_get_paper_size(mPageSetup);
*aPaperHeight =
gtk_paper_size_get_height(paperSize, GetGTKUnit(mPaperSizeUnit));
return NS_OK;
}
NS_IMETHODIMP
nsPrintSettingsGTK::SetPaperHeight(double aPaperHeight)
{
gtk_paper_size_set_size(mPaperSize,
gtk_paper_size_get_width(mPaperSize, GetGTKUnit(mPaperSizeUnit)),
GtkPaperSize* paperSize = gtk_page_setup_get_paper_size(mPageSetup);
gtk_paper_size_set_size(paperSize,
gtk_paper_size_get_width(paperSize, GetGTKUnit(mPaperSizeUnit)),
aPaperHeight,
GetGTKUnit(mPaperSizeUnit));
SaveNewPageSize();
@ -700,9 +711,10 @@ nsPrintSettingsGTK::SetPaperSizeUnit(int16_t aPaperSizeUnit)
{
// Convert units internally. e.g. they might have set the values while we're still in mm but
// they change to inch just afterwards, expecting that their sizes are in inches.
gtk_paper_size_set_size(mPaperSize,
gtk_paper_size_get_width(mPaperSize, GetGTKUnit(mPaperSizeUnit)),
gtk_paper_size_get_height(mPaperSize, GetGTKUnit(mPaperSizeUnit)),
GtkPaperSize* paperSize = gtk_page_setup_get_paper_size(mPageSetup);
gtk_paper_size_set_size(paperSize,
gtk_paper_size_get_width(paperSize, GetGTKUnit(mPaperSizeUnit)),
gtk_paper_size_get_height(paperSize, GetGTKUnit(mPaperSizeUnit)),
GetGTKUnit(aPaperSizeUnit));
SaveNewPageSize();
@ -713,8 +725,9 @@ nsPrintSettingsGTK::SetPaperSizeUnit(int16_t aPaperSizeUnit)
NS_IMETHODIMP
nsPrintSettingsGTK::GetEffectivePageSize(double *aWidth, double *aHeight)
{
*aWidth = NS_INCHES_TO_INT_TWIPS(gtk_paper_size_get_width(mPaperSize, GTK_UNIT_INCH));
*aHeight = NS_INCHES_TO_INT_TWIPS(gtk_paper_size_get_height(mPaperSize, GTK_UNIT_INCH));
GtkPaperSize* paperSize = gtk_page_setup_get_paper_size(mPageSetup);
*aWidth = NS_INCHES_TO_INT_TWIPS(gtk_paper_size_get_width(paperSize, GTK_UNIT_INCH));
*aHeight = NS_INCHES_TO_INT_TWIPS(gtk_paper_size_get_height(paperSize, GTK_UNIT_INCH));
GtkPageOrientation gtkOrient = gtk_page_setup_get_orientation(mPageSetup);

View File

@ -135,13 +135,12 @@ protected:
/**
* On construction:
* - mPrintSettings, mPageSetup and mPaperSize are just new objects with defaults determined by GTK.
* - mPrintSettings and mPageSetup are just new objects with defaults determined by GTK.
* - mGTKPrinter is nullptr!!! Remember to be careful when accessing this property.
*/
GtkPageSetup* mPageSetup;
GtkPrintSettings* mPrintSettings;
GtkPrinter* mGTKPrinter;
GtkPaperSize* mPaperSize;
bool mPrintSelectionOnly;
};