2008-01-21 06:47:25 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
2012-09-14 05:56:59 +04:00
|
|
|
#include <gtk/gtkunixprint.h>
|
2008-01-21 06:47:25 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2020-05-01 06:02:13 +03:00
|
|
|
#include "mozilla/Services.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2020-05-13 16:11:49 +03:00
|
|
|
#include "MozContainer.h"
|
2008-01-21 06:47:25 +03:00
|
|
|
#include "nsIPrintSettings.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsPrintDialogGTK.h"
|
|
|
|
#include "nsPrintSettingsGTK.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsIPrintSettingsService.h"
|
2008-05-01 10:43:04 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2018-04-05 17:44:35 +03:00
|
|
|
#include "nsIGIOService.h"
|
2010-06-26 01:51:17 +04:00
|
|
|
#include "WidgetUtils.h"
|
2018-04-05 17:44:35 +03:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
|
|
|
|
// for gdk_x11_window_get_xid
|
|
|
|
#include <gdk/gdkx.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <gio/gunixfdlist.h>
|
2019-11-07 17:28:55 +03:00
|
|
|
#include "gfxPlatformGtk.h"
|
2018-04-05 17:44:35 +03:00
|
|
|
|
|
|
|
// for dlsym
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include "MainThreadUtils.h"
|
2010-06-26 01:51:17 +04:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
2010-06-26 01:51:17 +04:00
|
|
|
using namespace mozilla::widget;
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
static const char header_footer_tags[][4] = {"", "&T", "&U", "&D", "&P", "&PT"};
|
|
|
|
|
2013-01-29 06:58:07 +04:00
|
|
|
#define CUSTOM_VALUE_INDEX gint(ArrayLength(header_footer_tags))
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2008-05-01 10:43:04 +04:00
|
|
|
static GtkWindow* get_gtk_window_for_nsiwidget(nsIWidget* widget) {
|
2012-11-07 01:50:37 +04:00
|
|
|
return GTK_WINDOW(widget->GetNativeData(NS_NATIVE_SHELLWIDGET));
|
2008-05-01 10:43:04 +04:00
|
|
|
}
|
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
static void ShowCustomDialog(GtkComboBox* changed_box, gpointer user_data) {
|
2008-03-08 14:33:17 +03:00
|
|
|
if (gtk_combo_box_get_active(changed_box) != CUSTOM_VALUE_INDEX) {
|
|
|
|
g_object_set_data(G_OBJECT(changed_box), "previous-active",
|
|
|
|
GINT_TO_POINTER(gtk_combo_box_get_active(changed_box)));
|
2008-01-21 06:47:25 +03:00
|
|
|
return;
|
2008-03-08 14:33:17 +03:00
|
|
|
}
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2010-02-12 01:40:03 +03:00
|
|
|
GtkWindow* printDialog = GTK_WINDOW(user_data);
|
2008-01-21 06:47:25 +03:00
|
|
|
nsCOMPtr<nsIStringBundleService> bundleSvc =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundle> printBundle;
|
2009-10-09 09:20:37 +04:00
|
|
|
bundleSvc->CreateBundle("chrome://global/locale/printdialog.properties",
|
|
|
|
getter_AddRefs(printBundle));
|
2017-08-04 07:40:52 +03:00
|
|
|
nsAutoString intlString;
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2017-08-04 07:40:52 +03:00
|
|
|
printBundle->GetStringFromName("headerFooterCustom", intlString);
|
2010-02-12 01:40:03 +03:00
|
|
|
GtkWidget* prompt_dialog = gtk_dialog_new_with_buttons(
|
|
|
|
NS_ConvertUTF16toUTF8(intlString).get(), printDialog,
|
2008-01-21 06:47:25 +03:00
|
|
|
(GtkDialogFlags)(GTK_DIALOG_MODAL), GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
|
2008-01-22 10:40:23 +03:00
|
|
|
GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, nullptr);
|
|
|
|
gtk_dialog_set_default_response(GTK_DIALOG(prompt_dialog),
|
|
|
|
GTK_RESPONSE_ACCEPT);
|
2009-09-11 10:14:13 +04:00
|
|
|
gtk_dialog_set_alternative_button_order(
|
2008-01-22 10:40:23 +03:00
|
|
|
GTK_DIALOG(prompt_dialog), GTK_RESPONSE_ACCEPT, GTK_RESPONSE_REJECT, -1);
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2017-08-04 07:40:52 +03:00
|
|
|
printBundle->GetStringFromName("customHeaderFooterPrompt", intlString);
|
2008-01-21 06:47:25 +03:00
|
|
|
GtkWidget* custom_label =
|
|
|
|
gtk_label_new(NS_ConvertUTF16toUTF8(intlString).get());
|
|
|
|
GtkWidget* custom_entry = gtk_entry_new();
|
|
|
|
GtkWidget* question_icon =
|
|
|
|
gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
|
|
|
|
|
|
|
|
// To be convenient, prefill the textbox with the existing value, if any, and
|
|
|
|
// select it all so they can easily both edit it and type in a new one.
|
|
|
|
const char* current_text =
|
|
|
|
(const char*)g_object_get_data(G_OBJECT(changed_box), "custom-text");
|
|
|
|
if (current_text) {
|
|
|
|
gtk_entry_set_text(GTK_ENTRY(custom_entry), current_text);
|
|
|
|
gtk_editable_select_region(GTK_EDITABLE(custom_entry), 0, -1);
|
|
|
|
}
|
2010-02-12 01:40:03 +03:00
|
|
|
gtk_entry_set_activates_default(GTK_ENTRY(custom_entry), TRUE);
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
GtkWidget* custom_vbox = gtk_vbox_new(TRUE, 2);
|
|
|
|
gtk_box_pack_start(GTK_BOX(custom_vbox), custom_label, FALSE, FALSE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(custom_vbox), custom_entry, FALSE, FALSE,
|
|
|
|
5); // Make entry 5px underneath label
|
|
|
|
GtkWidget* custom_hbox = gtk_hbox_new(FALSE, 2);
|
|
|
|
gtk_box_pack_start(GTK_BOX(custom_hbox), question_icon, FALSE, FALSE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(custom_hbox), custom_vbox, FALSE, FALSE,
|
|
|
|
10); // Make question icon 10px away from content
|
|
|
|
gtk_container_set_border_width(GTK_CONTAINER(custom_hbox), 2);
|
|
|
|
gtk_widget_show_all(custom_hbox);
|
|
|
|
|
2017-12-19 13:38:59 +03:00
|
|
|
gtk_box_pack_start(
|
|
|
|
GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(prompt_dialog))),
|
2012-09-14 05:56:59 +04:00
|
|
|
custom_hbox, FALSE, FALSE, 0);
|
2011-07-14 05:58:32 +04:00
|
|
|
gint diag_response = gtk_dialog_run(GTK_DIALOG(prompt_dialog));
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
if (diag_response == GTK_RESPONSE_ACCEPT) {
|
|
|
|
const gchar* response_text = gtk_entry_get_text(GTK_ENTRY(custom_entry));
|
|
|
|
g_object_set_data_full(G_OBJECT(changed_box), "custom-text",
|
|
|
|
strdup(response_text), (GDestroyNotify)free);
|
2008-03-08 14:33:17 +03:00
|
|
|
g_object_set_data(G_OBJECT(changed_box), "previous-active",
|
|
|
|
GINT_TO_POINTER(CUSTOM_VALUE_INDEX));
|
2008-01-21 06:47:25 +03:00
|
|
|
} else {
|
2008-03-08 14:33:17 +03:00
|
|
|
// Go back to the previous index
|
|
|
|
gint previous_active = GPOINTER_TO_INT(
|
|
|
|
g_object_get_data(G_OBJECT(changed_box), "previous-active"));
|
|
|
|
gtk_combo_box_set_active(changed_box, previous_active);
|
2008-01-21 06:47:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_destroy(prompt_dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
class nsPrintDialogWidgetGTK {
|
|
|
|
public:
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPrintDialogWidgetGTK(nsPIDOMWindowOuter* aParent,
|
|
|
|
nsIPrintSettings* aPrintSettings);
|
2008-01-21 06:47:25 +03:00
|
|
|
~nsPrintDialogWidgetGTK() { gtk_widget_destroy(dialog); }
|
|
|
|
NS_ConvertUTF16toUTF8 GetUTF8FromBundle(const char* aKey);
|
2016-01-06 04:08:45 +03:00
|
|
|
gint Run();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
nsresult ImportSettings(nsIPrintSettings* aNSSettings);
|
|
|
|
nsresult ExportSettings(nsIPrintSettings* aNSSettings);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
private:
|
|
|
|
GtkWidget* dialog;
|
|
|
|
GtkWidget* shrink_to_fit_toggle;
|
|
|
|
GtkWidget* print_bg_colors_toggle;
|
|
|
|
GtkWidget* print_bg_images_toggle;
|
|
|
|
GtkWidget* selection_only_toggle;
|
|
|
|
GtkWidget* header_dropdown[3]; // {left, center, right}
|
|
|
|
GtkWidget* footer_dropdown[3];
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
nsCOMPtr<nsIStringBundle> printBundle;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool useNativeSelection;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
GtkWidget* ConstructHeaderFooterDropdown(const char16_t* currentString);
|
2008-01-21 06:47:25 +03:00
|
|
|
const char* OptionWidgetToString(GtkWidget* dropdown);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
/* Code to copy between GTK and NS print settings structures.
|
2017-12-19 13:38:59 +03:00
|
|
|
* In the following,
|
2008-01-21 06:47:25 +03:00
|
|
|
* "Import" means to copy from NS to GTK
|
|
|
|
* "Export" means to copy from GTK to NS
|
|
|
|
*/
|
|
|
|
void ExportHeaderFooter(nsIPrintSettings* aNS);
|
|
|
|
};
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPrintDialogWidgetGTK::nsPrintDialogWidgetGTK(nsPIDOMWindowOuter* aParent,
|
|
|
|
nsIPrintSettings* aSettings) {
|
2010-06-26 01:51:17 +04:00
|
|
|
nsCOMPtr<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(aParent);
|
|
|
|
NS_ASSERTION(widget, "Need a widget for dialog to be modal.");
|
|
|
|
GtkWindow* gtkParent = get_gtk_window_for_nsiwidget(widget);
|
2008-05-01 10:43:04 +04:00
|
|
|
NS_ASSERTION(gtkParent, "Need a GTK window for dialog to be modal.");
|
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
nsCOMPtr<nsIStringBundleService> bundleSvc =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID);
|
2009-10-09 09:20:37 +04:00
|
|
|
bundleSvc->CreateBundle("chrome://global/locale/printdialog.properties",
|
|
|
|
getter_AddRefs(printBundle));
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2009-10-09 09:20:37 +04:00
|
|
|
dialog = gtk_print_unix_dialog_new(GetUTF8FromBundle("printTitleGTK").get(),
|
|
|
|
gtkParent);
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
gtk_print_unix_dialog_set_manual_capabilities(
|
|
|
|
GTK_PRINT_UNIX_DIALOG(dialog),
|
|
|
|
GtkPrintCapabilities(
|
|
|
|
GTK_PRINT_CAPABILITY_PAGE_SET | GTK_PRINT_CAPABILITY_COPIES |
|
|
|
|
GTK_PRINT_CAPABILITY_COLLATE | GTK_PRINT_CAPABILITY_REVERSE |
|
2010-02-18 03:45:38 +03:00
|
|
|
GTK_PRINT_CAPABILITY_SCALE | GTK_PRINT_CAPABILITY_GENERATE_PDF));
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
// The vast majority of magic numbers in this widget construction are padding.
|
|
|
|
// e.g. for the set_border_width below, 12px matches that of just about every
|
|
|
|
// other window.
|
|
|
|
GtkWidget* custom_options_tab = gtk_vbox_new(FALSE, 0);
|
|
|
|
gtk_container_set_border_width(GTK_CONTAINER(custom_options_tab), 12);
|
2009-10-09 09:20:37 +04:00
|
|
|
GtkWidget* tab_label =
|
|
|
|
gtk_label_new(GetUTF8FromBundle("optionsTabLabelGTK").get());
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
// Check buttons for shrink-to-fit and print selection
|
|
|
|
GtkWidget* check_buttons_container = gtk_vbox_new(TRUE, 2);
|
|
|
|
shrink_to_fit_toggle = gtk_check_button_new_with_mnemonic(
|
|
|
|
GetUTF8FromBundle("shrinkToFit").get());
|
2010-02-12 01:39:33 +03:00
|
|
|
gtk_box_pack_start(GTK_BOX(check_buttons_container), shrink_to_fit_toggle,
|
|
|
|
FALSE, FALSE, 0);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-02-12 01:39:33 +03:00
|
|
|
// GTK+2.18 and above allow us to add a "Selection" option to the main
|
|
|
|
// settings screen, rather than adding an option on a custom tab like we must
|
|
|
|
// do on older versions.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool canSelectText;
|
2008-01-21 06:47:25 +03:00
|
|
|
aSettings->GetPrintOptions(nsIPrintSettings::kEnableSelectionRB,
|
|
|
|
&canSelectText);
|
2010-02-12 01:39:33 +03:00
|
|
|
if (gtk_major_version > 2 ||
|
|
|
|
(gtk_major_version == 2 && gtk_minor_version >= 18)) {
|
2011-10-03 11:56:21 +04:00
|
|
|
useNativeSelection = true;
|
2010-02-12 01:39:33 +03:00
|
|
|
g_object_set(dialog, "support-selection", TRUE, "has-selection",
|
2010-03-26 05:03:18 +03:00
|
|
|
canSelectText, "embed-page-setup", TRUE, nullptr);
|
2010-02-12 01:39:33 +03:00
|
|
|
} else {
|
2011-10-03 11:56:21 +04:00
|
|
|
useNativeSelection = false;
|
2010-02-12 01:39:33 +03:00
|
|
|
selection_only_toggle = gtk_check_button_new_with_mnemonic(
|
|
|
|
GetUTF8FromBundle("selectionOnly").get());
|
|
|
|
gtk_widget_set_sensitive(selection_only_toggle, canSelectText);
|
|
|
|
gtk_box_pack_start(GTK_BOX(check_buttons_container), selection_only_toggle,
|
|
|
|
FALSE, FALSE, 0);
|
|
|
|
}
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
// Check buttons for printing background
|
|
|
|
GtkWidget* appearance_buttons_container = gtk_vbox_new(TRUE, 2);
|
|
|
|
print_bg_colors_toggle = gtk_check_button_new_with_mnemonic(
|
|
|
|
GetUTF8FromBundle("printBGColors").get());
|
|
|
|
print_bg_images_toggle = gtk_check_button_new_with_mnemonic(
|
|
|
|
GetUTF8FromBundle("printBGImages").get());
|
|
|
|
gtk_box_pack_start(GTK_BOX(appearance_buttons_container),
|
|
|
|
print_bg_colors_toggle, FALSE, FALSE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(appearance_buttons_container),
|
|
|
|
print_bg_images_toggle, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
// "Appearance" options label, bold and center-aligned
|
2013-10-08 22:47:37 +04:00
|
|
|
GtkWidget* appearance_label = gtk_label_new(nullptr);
|
2019-05-15 22:13:46 +03:00
|
|
|
char* pangoMarkup = g_markup_printf_escaped(
|
2008-01-21 06:47:25 +03:00
|
|
|
"<b>%s</b>", GetUTF8FromBundle("printBGOptions").get());
|
|
|
|
gtk_label_set_markup(GTK_LABEL(appearance_label), pangoMarkup);
|
|
|
|
g_free(pangoMarkup);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(appearance_label), 0, 0);
|
|
|
|
|
|
|
|
GtkWidget* appearance_container = gtk_alignment_new(0, 0, 0, 0);
|
|
|
|
gtk_alignment_set_padding(GTK_ALIGNMENT(appearance_container), 8, 0, 12, 0);
|
|
|
|
gtk_container_add(GTK_CONTAINER(appearance_container),
|
|
|
|
appearance_buttons_container);
|
|
|
|
|
|
|
|
GtkWidget* appearance_vertical_squasher = gtk_vbox_new(FALSE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(appearance_vertical_squasher), appearance_label,
|
|
|
|
FALSE, FALSE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(appearance_vertical_squasher),
|
|
|
|
appearance_container, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
// "Header & Footer" options label, bold and center-aligned
|
2013-10-08 22:47:37 +04:00
|
|
|
GtkWidget* header_footer_label = gtk_label_new(nullptr);
|
2008-01-21 06:47:25 +03:00
|
|
|
pangoMarkup = g_markup_printf_escaped(
|
|
|
|
"<b>%s</b>", GetUTF8FromBundle("headerFooter").get());
|
|
|
|
gtk_label_set_markup(GTK_LABEL(header_footer_label), pangoMarkup);
|
|
|
|
g_free(pangoMarkup);
|
|
|
|
gtk_misc_set_alignment(GTK_MISC(header_footer_label), 0, 0);
|
|
|
|
|
|
|
|
GtkWidget* header_footer_container = gtk_alignment_new(0, 0, 0, 0);
|
|
|
|
gtk_alignment_set_padding(GTK_ALIGNMENT(header_footer_container), 8, 0, 12,
|
|
|
|
0);
|
|
|
|
|
|
|
|
// --- Table for making the header and footer options ---
|
|
|
|
GtkWidget* header_footer_table = gtk_table_new(3, 3, FALSE); // 3x3 table
|
2017-08-08 09:07:55 +03:00
|
|
|
nsString header_footer_str[3];
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2017-10-09 02:08:09 +03:00
|
|
|
aSettings->GetHeaderStrLeft(header_footer_str[0]);
|
|
|
|
aSettings->GetHeaderStrCenter(header_footer_str[1]);
|
|
|
|
aSettings->GetHeaderStrRight(header_footer_str[2]);
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(header_dropdown); i++) {
|
2008-01-21 06:47:25 +03:00
|
|
|
header_dropdown[i] =
|
|
|
|
ConstructHeaderFooterDropdown(header_footer_str[i].get());
|
|
|
|
// Those 4 magic numbers in the middle provide the position in the table.
|
|
|
|
// The last two numbers mean 2 px padding on every side.
|
|
|
|
gtk_table_attach(GTK_TABLE(header_footer_table), header_dropdown[i], i,
|
|
|
|
(i + 1), 0, 1, (GtkAttachOptions)0, (GtkAttachOptions)0, 2,
|
|
|
|
2);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char labelKeys[][7] = {"left", "center", "right"};
|
2011-10-11 09:50:08 +04:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(labelKeys); i++) {
|
2008-01-21 06:47:25 +03:00
|
|
|
gtk_table_attach(GTK_TABLE(header_footer_table),
|
|
|
|
gtk_label_new(GetUTF8FromBundle(labelKeys[i]).get()), i,
|
|
|
|
(i + 1), 1, 2, (GtkAttachOptions)0, (GtkAttachOptions)0, 2,
|
|
|
|
2);
|
|
|
|
}
|
|
|
|
|
2017-10-09 02:08:09 +03:00
|
|
|
aSettings->GetFooterStrLeft(header_footer_str[0]);
|
|
|
|
aSettings->GetFooterStrCenter(header_footer_str[1]);
|
|
|
|
aSettings->GetFooterStrRight(header_footer_str[2]);
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(footer_dropdown); i++) {
|
2008-01-21 06:47:25 +03:00
|
|
|
footer_dropdown[i] =
|
|
|
|
ConstructHeaderFooterDropdown(header_footer_str[i].get());
|
|
|
|
gtk_table_attach(GTK_TABLE(header_footer_table), footer_dropdown[i], i,
|
|
|
|
(i + 1), 2, 3, (GtkAttachOptions)0, (GtkAttachOptions)0, 2,
|
|
|
|
2);
|
|
|
|
}
|
|
|
|
// ---
|
|
|
|
|
|
|
|
gtk_container_add(GTK_CONTAINER(header_footer_container),
|
|
|
|
header_footer_table);
|
|
|
|
|
|
|
|
GtkWidget* header_footer_vertical_squasher = gtk_vbox_new(FALSE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(header_footer_vertical_squasher),
|
|
|
|
header_footer_label, FALSE, FALSE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(header_footer_vertical_squasher),
|
|
|
|
header_footer_container, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
// Construction of everything
|
|
|
|
gtk_box_pack_start(GTK_BOX(custom_options_tab), check_buttons_container,
|
|
|
|
FALSE, FALSE, 10); // 10px padding
|
|
|
|
gtk_box_pack_start(GTK_BOX(custom_options_tab), appearance_vertical_squasher,
|
|
|
|
FALSE, FALSE, 10);
|
|
|
|
gtk_box_pack_start(GTK_BOX(custom_options_tab),
|
|
|
|
header_footer_vertical_squasher, FALSE, FALSE, 0);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
gtk_print_unix_dialog_add_custom_tab(GTK_PRINT_UNIX_DIALOG(dialog),
|
|
|
|
custom_options_tab, tab_label);
|
|
|
|
gtk_widget_show_all(custom_options_tab);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 nsPrintDialogWidgetGTK::GetUTF8FromBundle(
|
|
|
|
const char* aKey) {
|
2017-08-04 07:40:52 +03:00
|
|
|
nsAutoString intlString;
|
|
|
|
printBundle->GetStringFromName(aKey, intlString);
|
2008-01-21 06:47:25 +03:00
|
|
|
return NS_ConvertUTF16toUTF8(
|
|
|
|
intlString); // Return the actual object so we don't lose reference
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* nsPrintDialogWidgetGTK::OptionWidgetToString(GtkWidget* dropdown) {
|
|
|
|
gint index = gtk_combo_box_get_active(GTK_COMBO_BOX(dropdown));
|
|
|
|
|
|
|
|
NS_ASSERTION(index <= CUSTOM_VALUE_INDEX,
|
|
|
|
"Index of dropdown is higher than expected!");
|
|
|
|
|
|
|
|
if (index == CUSTOM_VALUE_INDEX)
|
|
|
|
return (const char*)g_object_get_data(G_OBJECT(dropdown), "custom-text");
|
|
|
|
else
|
|
|
|
return header_footer_tags[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
gint nsPrintDialogWidgetGTK::Run() {
|
2011-07-14 05:58:32 +04:00
|
|
|
const gint response = gtk_dialog_run(GTK_DIALOG(dialog));
|
2008-01-21 06:47:25 +03:00
|
|
|
gtk_widget_hide(dialog);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsPrintDialogWidgetGTK::ExportHeaderFooter(nsIPrintSettings* aNS) {
|
|
|
|
const char* header_footer_str;
|
|
|
|
header_footer_str = OptionWidgetToString(header_dropdown[0]);
|
2017-10-09 02:08:09 +03:00
|
|
|
aNS->SetHeaderStrLeft(NS_ConvertUTF8toUTF16(header_footer_str));
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
header_footer_str = OptionWidgetToString(header_dropdown[1]);
|
2017-10-09 02:08:09 +03:00
|
|
|
aNS->SetHeaderStrCenter(NS_ConvertUTF8toUTF16(header_footer_str));
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
header_footer_str = OptionWidgetToString(header_dropdown[2]);
|
2017-10-09 02:08:09 +03:00
|
|
|
aNS->SetHeaderStrRight(NS_ConvertUTF8toUTF16(header_footer_str));
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
header_footer_str = OptionWidgetToString(footer_dropdown[0]);
|
2017-10-09 02:08:09 +03:00
|
|
|
aNS->SetFooterStrLeft(NS_ConvertUTF8toUTF16(header_footer_str));
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
header_footer_str = OptionWidgetToString(footer_dropdown[1]);
|
2017-10-09 02:08:09 +03:00
|
|
|
aNS->SetFooterStrCenter(NS_ConvertUTF8toUTF16(header_footer_str));
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
header_footer_str = OptionWidgetToString(footer_dropdown[2]);
|
2017-10-09 02:08:09 +03:00
|
|
|
aNS->SetFooterStrRight(NS_ConvertUTF8toUTF16(header_footer_str));
|
2008-01-21 06:47:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsPrintDialogWidgetGTK::ImportSettings(nsIPrintSettings* aNSSettings) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aNSSettings, "aSettings must not be null");
|
2008-01-21 06:47:25 +03:00
|
|
|
NS_ENSURE_TRUE(aNSSettings, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsPrintSettingsGTK> aNSSettingsGTK(do_QueryInterface(aNSSettings));
|
|
|
|
if (!aNSSettingsGTK) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
GtkPrintSettings* settings = aNSSettingsGTK->GetGtkPrintSettings();
|
|
|
|
GtkPageSetup* setup = aNSSettingsGTK->GetGtkPageSetup();
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool geckoBool;
|
2008-01-21 06:47:25 +03:00
|
|
|
aNSSettings->GetShrinkToFit(&geckoBool);
|
|
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(shrink_to_fit_toggle),
|
|
|
|
geckoBool);
|
|
|
|
|
|
|
|
aNSSettings->GetPrintBGColors(&geckoBool);
|
|
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(print_bg_colors_toggle),
|
|
|
|
geckoBool);
|
|
|
|
|
|
|
|
aNSSettings->GetPrintBGImages(&geckoBool);
|
|
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(print_bg_images_toggle),
|
|
|
|
geckoBool);
|
|
|
|
|
|
|
|
gtk_print_unix_dialog_set_settings(GTK_PRINT_UNIX_DIALOG(dialog), settings);
|
|
|
|
gtk_print_unix_dialog_set_page_setup(GTK_PRINT_UNIX_DIALOG(dialog), setup);
|
2017-12-19 13:38:59 +03:00
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsPrintDialogWidgetGTK::ExportSettings(nsIPrintSettings* aNSSettings) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aNSSettings, "aSettings must not be null");
|
2008-01-21 06:47:25 +03:00
|
|
|
NS_ENSURE_TRUE(aNSSettings, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
GtkPrintSettings* settings =
|
|
|
|
gtk_print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(dialog));
|
|
|
|
GtkPageSetup* setup =
|
|
|
|
gtk_print_unix_dialog_get_page_setup(GTK_PRINT_UNIX_DIALOG(dialog));
|
|
|
|
GtkPrinter* printer =
|
|
|
|
gtk_print_unix_dialog_get_selected_printer(GTK_PRINT_UNIX_DIALOG(dialog));
|
|
|
|
if (settings && setup && printer) {
|
|
|
|
ExportHeaderFooter(aNSSettings);
|
|
|
|
|
|
|
|
aNSSettings->SetOutputFormat(nsIPrintSettings::kOutputFormatNative);
|
|
|
|
|
|
|
|
// Print-to-file is true by default. This must be turned off or else
|
|
|
|
// printing won't occur! (We manually copy the spool file when this flag is
|
|
|
|
// set, because we love our embedders) Even if it is print-to-file in GTK's
|
|
|
|
// case, GTK does The Right Thing when we send the job.
|
2011-10-03 11:56:21 +04:00
|
|
|
aNSSettings->SetPrintToFile(false);
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
aNSSettings->SetShrinkToFit(
|
|
|
|
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(shrink_to_fit_toggle)));
|
|
|
|
|
|
|
|
aNSSettings->SetPrintBGColors(gtk_toggle_button_get_active(
|
|
|
|
GTK_TOGGLE_BUTTON(print_bg_colors_toggle)));
|
|
|
|
aNSSettings->SetPrintBGImages(gtk_toggle_button_get_active(
|
|
|
|
GTK_TOGGLE_BUTTON(print_bg_images_toggle)));
|
|
|
|
|
|
|
|
// Try to save native settings in the session object
|
|
|
|
nsCOMPtr<nsPrintSettingsGTK> aNSSettingsGTK(do_QueryInterface(aNSSettings));
|
|
|
|
if (aNSSettingsGTK) {
|
|
|
|
aNSSettingsGTK->SetGtkPrintSettings(settings);
|
|
|
|
aNSSettingsGTK->SetGtkPageSetup(setup);
|
|
|
|
aNSSettingsGTK->SetGtkPrinter(printer);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool printSelectionOnly;
|
2010-02-12 01:39:33 +03:00
|
|
|
if (useNativeSelection) {
|
|
|
|
_GtkPrintPages pageSetting =
|
|
|
|
(_GtkPrintPages)gtk_print_settings_get_print_pages(settings);
|
|
|
|
printSelectionOnly = (pageSetting == _GTK_PRINT_PAGES_SELECTION);
|
|
|
|
} else {
|
|
|
|
printSelectionOnly = gtk_toggle_button_get_active(
|
|
|
|
GTK_TOGGLE_BUTTON(selection_only_toggle));
|
|
|
|
}
|
|
|
|
aNSSettingsGTK->SetForcePrintSelectionOnly(printSelectionOnly);
|
2008-01-21 06:47:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings) g_object_unref(settings);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
GtkWidget* nsPrintDialogWidgetGTK::ConstructHeaderFooterDropdown(
|
|
|
|
const char16_t* currentString) {
|
2012-09-14 05:56:59 +04:00
|
|
|
GtkWidget* dropdown = gtk_combo_box_text_new();
|
2008-01-21 06:47:25 +03:00
|
|
|
const char hf_options[][22] = {"headerFooterBlank", "headerFooterTitle",
|
|
|
|
"headerFooterURL", "headerFooterDate",
|
|
|
|
"headerFooterPage", "headerFooterPageTotal",
|
|
|
|
"headerFooterCustom"};
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(hf_options); i++) {
|
2018-01-10 10:52:04 +03:00
|
|
|
gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(dropdown), nullptr,
|
2012-09-14 05:56:59 +04:00
|
|
|
GetUTF8FromBundle(hf_options[i]).get());
|
2008-01-21 06:47:25 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool shouldBeCustom = true;
|
2008-01-21 06:47:25 +03:00
|
|
|
NS_ConvertUTF16toUTF8 currentStringUTF8(currentString);
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(header_footer_tags); i++) {
|
2008-01-21 06:47:25 +03:00
|
|
|
if (!strcmp(currentStringUTF8.get(), header_footer_tags[i])) {
|
|
|
|
gtk_combo_box_set_active(GTK_COMBO_BOX(dropdown), i);
|
2008-03-08 14:33:17 +03:00
|
|
|
g_object_set_data(G_OBJECT(dropdown), "previous-active",
|
|
|
|
GINT_TO_POINTER(i));
|
2011-10-03 11:56:21 +04:00
|
|
|
shouldBeCustom = false;
|
2008-01-21 06:47:25 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldBeCustom) {
|
|
|
|
gtk_combo_box_set_active(GTK_COMBO_BOX(dropdown), CUSTOM_VALUE_INDEX);
|
2008-03-08 14:33:17 +03:00
|
|
|
g_object_set_data(G_OBJECT(dropdown), "previous-active",
|
|
|
|
GINT_TO_POINTER(CUSTOM_VALUE_INDEX));
|
2008-01-21 06:47:25 +03:00
|
|
|
char* custom_string = strdup(currentStringUTF8.get());
|
|
|
|
g_object_set_data_full(G_OBJECT(dropdown), "custom-text", custom_string,
|
|
|
|
(GDestroyNotify)free);
|
|
|
|
}
|
|
|
|
|
2010-02-12 01:40:03 +03:00
|
|
|
g_signal_connect(dropdown, "changed", (GCallback)ShowCustomDialog, dialog);
|
2008-01-21 06:47:25 +03:00
|
|
|
return dropdown;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsPrintDialogServiceGTK, nsIPrintDialogService)
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2020-03-16 13:56:57 +03:00
|
|
|
nsPrintDialogServiceGTK::nsPrintDialogServiceGTK() = default;
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2020-03-16 13:56:57 +03:00
|
|
|
nsPrintDialogServiceGTK::~nsPrintDialogServiceGTK() = default;
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPrintDialogServiceGTK::Init() { return NS_OK; }
|
|
|
|
|
2018-04-05 17:44:35 +03:00
|
|
|
// Used to obtain window handle. The portal use this handle
|
|
|
|
// to ensure that print dialog is modal.
|
|
|
|
typedef void (*WindowHandleExported)(GtkWindow* window, const char* handle,
|
|
|
|
gpointer user_data);
|
|
|
|
|
|
|
|
typedef void (*GtkWindowHandleExported)(GtkWindow* window, const char* handle,
|
|
|
|
gpointer user_data);
|
|
|
|
#ifdef MOZ_WAYLAND
|
2018-11-01 14:09:40 +03:00
|
|
|
# if !GTK_CHECK_VERSION(3, 22, 0)
|
|
|
|
typedef void (*GdkWaylandWindowExported)(GdkWindow* window, const char* handle,
|
|
|
|
gpointer user_data);
|
|
|
|
# endif
|
|
|
|
|
2018-04-05 17:44:35 +03:00
|
|
|
typedef struct {
|
|
|
|
GtkWindow* window;
|
|
|
|
WindowHandleExported callback;
|
|
|
|
gpointer user_data;
|
|
|
|
} WaylandWindowHandleExportedData;
|
|
|
|
|
|
|
|
static void wayland_window_handle_exported(GdkWindow* window,
|
|
|
|
const char* wayland_handle_str,
|
|
|
|
gpointer user_data) {
|
|
|
|
WaylandWindowHandleExportedData* data =
|
|
|
|
static_cast<WaylandWindowHandleExportedData*>(user_data);
|
|
|
|
char* handle_str;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-05 17:44:35 +03:00
|
|
|
handle_str = g_strdup_printf("wayland:%s", wayland_handle_str);
|
|
|
|
data->callback(data->window, handle_str, data->user_data);
|
|
|
|
g_free(handle_str);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Get window handle for the portal, taken from gtk/gtkwindow.c
|
|
|
|
// (currently not exported)
|
|
|
|
static gboolean window_export_handle(GtkWindow* window,
|
|
|
|
GtkWindowHandleExported callback,
|
|
|
|
gpointer user_data) {
|
2019-11-07 17:28:55 +03:00
|
|
|
if (gfxPlatformGtk::GetPlatform()->IsX11Display()) {
|
2018-04-05 17:44:35 +03:00
|
|
|
GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
|
|
|
|
char* handle_str;
|
|
|
|
guint32 xid = (guint32)gdk_x11_window_get_xid(gdk_window);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-05 17:44:35 +03:00
|
|
|
handle_str = g_strdup_printf("x11:%x", xid);
|
|
|
|
callback(window, handle_str, user_data);
|
|
|
|
g_free(handle_str);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#ifdef MOZ_WAYLAND
|
|
|
|
else {
|
|
|
|
GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(window));
|
|
|
|
WaylandWindowHandleExportedData* data;
|
|
|
|
|
|
|
|
data = g_new0(WaylandWindowHandleExportedData, 1);
|
|
|
|
data->window = window;
|
|
|
|
data->callback = callback;
|
|
|
|
data->user_data = user_data;
|
|
|
|
|
|
|
|
static auto s_gdk_wayland_window_export_handle =
|
|
|
|
reinterpret_cast<gboolean (*)(GdkWindow*, GdkWaylandWindowExported,
|
|
|
|
gpointer, GDestroyNotify)>(
|
|
|
|
dlsym(RTLD_DEFAULT, "gdk_wayland_window_export_handle"));
|
|
|
|
if (!s_gdk_wayland_window_export_handle ||
|
|
|
|
!s_gdk_wayland_window_export_handle(
|
|
|
|
gdk_window, wayland_window_handle_exported, data, g_free)) {
|
|
|
|
g_free(data);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2018-04-05 17:44:35 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
g_warning("Couldn't export handle, unsupported windowing system");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Communication class with the GTK print portal handler
|
|
|
|
*
|
|
|
|
* To print document from flatpak we need to use print portal because
|
|
|
|
* printers are not directly accessible in the sandboxed environment.
|
|
|
|
*
|
|
|
|
* At first we request portal to show the print dialog to let user choose
|
|
|
|
* printer settings. We use DBUS interface for that (PreparePrint method).
|
|
|
|
*
|
|
|
|
* Next we force application to print to temporary file and after the writing
|
|
|
|
* to the file is finished we pass its file descriptor to the portal.
|
|
|
|
* Portal will pass duplicate of the file descriptor to the printer which
|
|
|
|
* user selected before (by DBUS Print method).
|
|
|
|
*
|
|
|
|
* Since DBUS communication is done async while nsPrintDialogServiceGTK::Show
|
|
|
|
* is expecting sync execution, we need to create a new GMainLoop during the
|
|
|
|
* print portal dialog is running. The loop is stopped after the dialog
|
|
|
|
* is closed.
|
|
|
|
*/
|
|
|
|
class nsFlatpakPrintPortal : public nsIObserver {
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
public:
|
|
|
|
explicit nsFlatpakPrintPortal(nsPrintSettingsGTK* aPrintSettings);
|
|
|
|
nsresult PreparePrintRequest(GtkWindow* aWindow);
|
|
|
|
static void OnWindowExportHandleDone(GtkWindow* aWindow,
|
|
|
|
const char* aWindowHandleStr,
|
|
|
|
gpointer aUserData);
|
|
|
|
void PreparePrint(GtkWindow* aWindow, const char* aWindowHandleStr);
|
|
|
|
static void OnPreparePrintResponse(GDBusConnection* connection,
|
|
|
|
const char* sender_name,
|
|
|
|
const char* object_path,
|
|
|
|
const char* interface_name,
|
|
|
|
const char* signal_name,
|
|
|
|
GVariant* parameters, gpointer data);
|
|
|
|
GtkPrintOperationResult GetResult();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2018-04-05 17:44:35 +03:00
|
|
|
private:
|
|
|
|
virtual ~nsFlatpakPrintPortal();
|
|
|
|
void FinishPrintDialog(GVariant* parameters);
|
|
|
|
nsCOMPtr<nsPrintSettingsGTK> mPrintAndPageSettings;
|
|
|
|
GDBusProxy* mProxy;
|
|
|
|
guint32 mToken;
|
|
|
|
GMainLoop* mLoop;
|
|
|
|
GtkPrintOperationResult mResult;
|
|
|
|
guint mResponseSignalId;
|
|
|
|
GtkWindow* mParentWindow;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsFlatpakPrintPortal, nsIObserver)
|
|
|
|
|
|
|
|
nsFlatpakPrintPortal::nsFlatpakPrintPortal(nsPrintSettingsGTK* aPrintSettings)
|
|
|
|
: mPrintAndPageSettings(aPrintSettings),
|
|
|
|
mProxy(nullptr),
|
|
|
|
mLoop(nullptr),
|
2018-08-24 11:03:54 +03:00
|
|
|
mResponseSignalId(0),
|
2018-04-05 17:44:35 +03:00
|
|
|
mParentWindow(nullptr) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates GDBusProxy, query for window handle and create a new GMainLoop.
|
|
|
|
*
|
|
|
|
* The GMainLoop is to be run from GetResult() and be quitted during
|
|
|
|
* FinishPrintDialog.
|
|
|
|
*
|
|
|
|
* @param aWindow toplevel application window which is used as parent of print
|
|
|
|
* dialog
|
|
|
|
*/
|
|
|
|
nsresult nsFlatpakPrintPortal::PreparePrintRequest(GtkWindow* aWindow) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aWindow, "aWindow must not be null");
|
|
|
|
MOZ_ASSERT(mPrintAndPageSettings, "mPrintAndPageSettings must not be null");
|
2018-04-05 17:44:35 +03:00
|
|
|
|
|
|
|
GError* error = nullptr;
|
|
|
|
mProxy = g_dbus_proxy_new_for_bus_sync(
|
|
|
|
G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, nullptr,
|
|
|
|
"org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop",
|
|
|
|
"org.freedesktop.portal.Print", nullptr, &error);
|
|
|
|
if (mProxy == nullptr) {
|
|
|
|
NS_WARNING(
|
|
|
|
nsPrintfCString("Unable to create dbus proxy: %s", error->message)
|
|
|
|
.get());
|
|
|
|
g_error_free(error);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The window handler is returned async, we will continue by PreparePrint
|
|
|
|
// method when it is returned.
|
|
|
|
if (!window_export_handle(
|
|
|
|
aWindow, &nsFlatpakPrintPortal::OnWindowExportHandleDone, this)) {
|
|
|
|
NS_WARNING("Unable to get window handle for creating modal print dialog.");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLoop = g_main_loop_new(NULL, FALSE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsFlatpakPrintPortal::OnWindowExportHandleDone(
|
|
|
|
GtkWindow* aWindow, const char* aWindowHandleStr, gpointer aUserData) {
|
|
|
|
nsFlatpakPrintPortal* printPortal =
|
|
|
|
static_cast<nsFlatpakPrintPortal*>(aUserData);
|
|
|
|
printPortal->PreparePrint(aWindow, aWindowHandleStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ask print portal to show the print dialog.
|
|
|
|
*
|
|
|
|
* Print and page settings and window handle are passed to the portal to prefill
|
|
|
|
* last used settings.
|
|
|
|
*/
|
|
|
|
void nsFlatpakPrintPortal::PreparePrint(GtkWindow* aWindow,
|
|
|
|
const char* aWindowHandleStr) {
|
|
|
|
GtkPrintSettings* gtkSettings = mPrintAndPageSettings->GetGtkPrintSettings();
|
|
|
|
GtkPageSetup* pageSetup = mPrintAndPageSettings->GetGtkPageSetup();
|
|
|
|
|
|
|
|
// We need to remember GtkWindow to unexport window handle after it is
|
|
|
|
// no longer needed by the portal dialog (apply only on non-X11 sessions).
|
2019-11-07 17:28:55 +03:00
|
|
|
if (gfxPlatformGtk::GetPlatform()->IsWaylandDisplay()) {
|
2018-04-05 17:44:35 +03:00
|
|
|
mParentWindow = aWindow;
|
|
|
|
}
|
|
|
|
|
|
|
|
GVariantBuilder opt_builder;
|
|
|
|
g_variant_builder_init(&opt_builder, G_VARIANT_TYPE_VARDICT);
|
|
|
|
char* token = g_strdup_printf("mozilla%d", g_random_int_range(0, G_MAXINT));
|
|
|
|
g_variant_builder_add(&opt_builder, "{sv}", "handle_token",
|
|
|
|
g_variant_new_string(token));
|
|
|
|
g_free(token);
|
|
|
|
GVariant* options = g_variant_builder_end(&opt_builder);
|
|
|
|
static auto s_gtk_print_settings_to_gvariant =
|
|
|
|
reinterpret_cast<GVariant* (*)(GtkPrintSettings*)>(
|
|
|
|
dlsym(RTLD_DEFAULT, "gtk_print_settings_to_gvariant"));
|
|
|
|
static auto s_gtk_page_setup_to_gvariant =
|
|
|
|
reinterpret_cast<GVariant* (*)(GtkPageSetup*)>(
|
|
|
|
dlsym(RTLD_DEFAULT, "gtk_page_setup_to_gvariant"));
|
|
|
|
if (!s_gtk_print_settings_to_gvariant || !s_gtk_page_setup_to_gvariant) {
|
|
|
|
mResult = GTK_PRINT_OPERATION_RESULT_ERROR;
|
|
|
|
FinishPrintDialog(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get translated window title
|
|
|
|
nsCOMPtr<nsIStringBundleService> bundleSvc =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID);
|
|
|
|
nsCOMPtr<nsIStringBundle> printBundle;
|
|
|
|
bundleSvc->CreateBundle("chrome://global/locale/printdialog.properties",
|
|
|
|
getter_AddRefs(printBundle));
|
|
|
|
nsAutoString intlPrintTitle;
|
|
|
|
printBundle->GetStringFromName("printTitleGTK", intlPrintTitle);
|
|
|
|
|
|
|
|
GError* error = nullptr;
|
|
|
|
GVariant* ret = g_dbus_proxy_call_sync(
|
|
|
|
mProxy, "PreparePrint",
|
|
|
|
g_variant_new(
|
|
|
|
"(ss@a{sv}@a{sv}@a{sv})", aWindowHandleStr,
|
|
|
|
NS_ConvertUTF16toUTF8(intlPrintTitle).get(), // Title of the window
|
|
|
|
s_gtk_print_settings_to_gvariant(gtkSettings),
|
|
|
|
s_gtk_page_setup_to_gvariant(pageSetup), options),
|
|
|
|
G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error);
|
|
|
|
if (ret == nullptr) {
|
|
|
|
NS_WARNING(
|
|
|
|
nsPrintfCString("Unable to call dbus proxy: %s", error->message).get());
|
|
|
|
g_error_free(error);
|
|
|
|
mResult = GTK_PRINT_OPERATION_RESULT_ERROR;
|
|
|
|
FinishPrintDialog(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* handle = nullptr;
|
|
|
|
g_variant_get(ret, "(&o)", &handle);
|
|
|
|
if (strcmp(aWindowHandleStr, handle) != 0) {
|
|
|
|
aWindowHandleStr = g_strdup(handle);
|
2018-08-24 11:03:54 +03:00
|
|
|
if (mResponseSignalId) {
|
|
|
|
g_dbus_connection_signal_unsubscribe(
|
|
|
|
g_dbus_proxy_get_connection(G_DBUS_PROXY(mProxy)), mResponseSignalId);
|
|
|
|
}
|
2018-04-05 17:44:35 +03:00
|
|
|
}
|
|
|
|
mResponseSignalId = g_dbus_connection_signal_subscribe(
|
|
|
|
g_dbus_proxy_get_connection(G_DBUS_PROXY(mProxy)),
|
|
|
|
"org.freedesktop.portal.Desktop", "org.freedesktop.portal.Request",
|
|
|
|
"Response", aWindowHandleStr, NULL, G_DBUS_SIGNAL_FLAGS_NO_MATCH_RULE,
|
|
|
|
&nsFlatpakPrintPortal::OnPreparePrintResponse, this, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsFlatpakPrintPortal::OnPreparePrintResponse(
|
|
|
|
GDBusConnection* connection, const char* sender_name,
|
|
|
|
const char* object_path, const char* interface_name,
|
|
|
|
const char* signal_name, GVariant* parameters, gpointer data) {
|
|
|
|
nsFlatpakPrintPortal* printPortal = static_cast<nsFlatpakPrintPortal*>(data);
|
|
|
|
printPortal->FinishPrintDialog(parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When the dialog is accepted, read print and page settings and token.
|
|
|
|
*
|
|
|
|
* Token is later used for printing portal as print operation identifier.
|
|
|
|
* Print and page settings are modified in-place and stored to
|
|
|
|
* mPrintAndPageSettings.
|
|
|
|
*/
|
|
|
|
void nsFlatpakPrintPortal::FinishPrintDialog(GVariant* parameters) {
|
|
|
|
// This ends GetResult() method
|
|
|
|
if (mLoop) {
|
|
|
|
g_main_loop_quit(mLoop);
|
|
|
|
mLoop = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parameters) {
|
|
|
|
// mResult should be already defined
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
guint32 response;
|
|
|
|
GVariant* options;
|
|
|
|
|
|
|
|
g_variant_get(parameters, "(u@a{sv})", &response, &options);
|
|
|
|
mResult = GTK_PRINT_OPERATION_RESULT_CANCEL;
|
|
|
|
if (response == 0) {
|
|
|
|
GVariant* v;
|
|
|
|
|
|
|
|
char* filename;
|
|
|
|
char* uri;
|
|
|
|
v = g_variant_lookup_value(options, "settings", G_VARIANT_TYPE_VARDICT);
|
|
|
|
static auto s_gtk_print_settings_new_from_gvariant =
|
|
|
|
reinterpret_cast<GtkPrintSettings* (*)(GVariant*)>(
|
|
|
|
dlsym(RTLD_DEFAULT, "gtk_print_settings_new_from_gvariant"));
|
|
|
|
|
|
|
|
GtkPrintSettings* printSettings = s_gtk_print_settings_new_from_gvariant(v);
|
|
|
|
g_variant_unref(v);
|
|
|
|
|
|
|
|
v = g_variant_lookup_value(options, "page-setup", G_VARIANT_TYPE_VARDICT);
|
|
|
|
static auto s_gtk_page_setup_new_from_gvariant =
|
|
|
|
reinterpret_cast<GtkPageSetup* (*)(GVariant*)>(
|
|
|
|
dlsym(RTLD_DEFAULT, "gtk_page_setup_new_from_gvariant"));
|
|
|
|
GtkPageSetup* pageSetup = s_gtk_page_setup_new_from_gvariant(v);
|
|
|
|
g_variant_unref(v);
|
|
|
|
|
|
|
|
g_variant_lookup(options, "token", "u", &mToken);
|
|
|
|
|
|
|
|
// Force printing to file because only filedescriptor of the file
|
|
|
|
// can be passed to portal
|
|
|
|
int fd = g_file_open_tmp("gtkprintXXXXXX", &filename, NULL);
|
|
|
|
uri = g_filename_to_uri(filename, NULL, NULL);
|
|
|
|
gtk_print_settings_set(printSettings, GTK_PRINT_SETTINGS_OUTPUT_URI, uri);
|
|
|
|
g_free(uri);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
// Save native settings in the session object
|
|
|
|
mPrintAndPageSettings->SetGtkPrintSettings(printSettings);
|
|
|
|
mPrintAndPageSettings->SetGtkPageSetup(pageSetup);
|
|
|
|
|
|
|
|
// Portal consumes PDF file
|
|
|
|
mPrintAndPageSettings->SetOutputFormat(nsIPrintSettings::kOutputFormatPDF);
|
|
|
|
|
|
|
|
// We need to set to print to file
|
|
|
|
mPrintAndPageSettings->SetPrintToFile(true);
|
|
|
|
|
|
|
|
mResult = GTK_PRINT_OPERATION_RESULT_APPLY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get result of the print dialog.
|
|
|
|
*
|
|
|
|
* This call blocks until FinishPrintDialog is called.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
GtkPrintOperationResult nsFlatpakPrintPortal::GetResult() {
|
|
|
|
// If the mLoop has not been initialized we haven't go thru PreparePrint
|
|
|
|
// method
|
|
|
|
if (!NS_IsMainThread() || !mLoop) {
|
|
|
|
return GTK_PRINT_OPERATION_RESULT_ERROR;
|
|
|
|
}
|
|
|
|
// Calling g_main_loop_run stops current code until g_main_loop_quit is called
|
|
|
|
g_main_loop_run(mLoop);
|
|
|
|
|
|
|
|
// Free resources we've allocated in order to show print dialog.
|
|
|
|
#ifdef MOZ_WAYLAND
|
|
|
|
if (mParentWindow) {
|
|
|
|
GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(mParentWindow));
|
|
|
|
static auto s_gdk_wayland_window_unexport_handle =
|
|
|
|
reinterpret_cast<void (*)(GdkWindow*)>(
|
|
|
|
dlsym(RTLD_DEFAULT, "gdk_wayland_window_unexport_handle"));
|
|
|
|
if (s_gdk_wayland_window_unexport_handle) {
|
|
|
|
s_gdk_wayland_window_unexport_handle(gdk_window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return mResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send file descriptor of the file which contains document to the portal to
|
|
|
|
* finish the print operation.
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsFlatpakPrintPortal::Observe(nsISupports* aObject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
// Check that written file match to the stored filename in case multiple
|
|
|
|
// print operations are in progress.
|
|
|
|
nsAutoString filenameStr;
|
|
|
|
mPrintAndPageSettings->GetToFileName(filenameStr);
|
|
|
|
if (!nsDependentString(aData).Equals(filenameStr)) {
|
|
|
|
// Different file is finished, not for this instance
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
int fd, idx;
|
|
|
|
fd = open(NS_ConvertUTF16toUTF8(filenameStr).get(), O_RDONLY | O_CLOEXEC);
|
|
|
|
static auto s_g_unix_fd_list_new = reinterpret_cast<GUnixFDList* (*)(void)>(
|
|
|
|
dlsym(RTLD_DEFAULT, "g_unix_fd_list_new"));
|
|
|
|
NS_ASSERTION(s_g_unix_fd_list_new,
|
|
|
|
"Cannot find g_unix_fd_list_new function.");
|
|
|
|
|
|
|
|
GUnixFDList* fd_list = s_g_unix_fd_list_new();
|
|
|
|
static auto s_g_unix_fd_list_append =
|
|
|
|
reinterpret_cast<gint (*)(GUnixFDList*, gint, GError**)>(
|
|
|
|
dlsym(RTLD_DEFAULT, "g_unix_fd_list_append"));
|
|
|
|
idx = s_g_unix_fd_list_append(fd_list, fd, NULL);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
GVariantBuilder opt_builder;
|
|
|
|
g_variant_builder_init(&opt_builder, G_VARIANT_TYPE_VARDICT);
|
|
|
|
g_variant_builder_add(&opt_builder, "{sv}", "token",
|
|
|
|
g_variant_new_uint32(mToken));
|
|
|
|
g_dbus_proxy_call_with_unix_fd_list(
|
|
|
|
mProxy, "Print",
|
|
|
|
g_variant_new("(ssh@a{sv})", "", /* window */
|
|
|
|
"Print", /* title */
|
|
|
|
idx, g_variant_builder_end(&opt_builder)),
|
|
|
|
G_DBUS_CALL_FLAGS_NONE, -1, fd_list, NULL,
|
|
|
|
NULL, // TODO portal result cb function
|
|
|
|
nullptr); // data
|
|
|
|
g_object_unref(fd_list);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
// Let the nsFlatpakPrintPortal instance die
|
|
|
|
os->RemoveObserver(this, "print-to-file-finished");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsFlatpakPrintPortal::~nsFlatpakPrintPortal() {
|
2018-08-24 11:03:54 +03:00
|
|
|
if (mProxy) {
|
|
|
|
if (mResponseSignalId) {
|
|
|
|
g_dbus_connection_signal_unsubscribe(
|
|
|
|
g_dbus_proxy_get_connection(G_DBUS_PROXY(mProxy)), mResponseSignalId);
|
|
|
|
}
|
2018-04-05 17:44:35 +03:00
|
|
|
g_object_unref(mProxy);
|
2018-08-24 11:03:54 +03:00
|
|
|
}
|
2018-04-05 17:44:35 +03:00
|
|
|
if (mLoop) g_main_loop_quit(mLoop);
|
|
|
|
}
|
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
NS_IMETHODIMP
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPrintDialogServiceGTK::Show(nsPIDOMWindowOuter* aParent,
|
2019-12-09 12:04:29 +03:00
|
|
|
nsIPrintSettings* aSettings) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aParent, "aParent must not be null");
|
|
|
|
MOZ_ASSERT(aSettings, "aSettings must not be null");
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2018-04-05 17:44:35 +03:00
|
|
|
// Check for the flatpak portal first
|
|
|
|
nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
|
|
|
|
bool shouldUsePortal;
|
|
|
|
giovfs->ShouldUseFlatpakPortal(&shouldUsePortal);
|
|
|
|
if (shouldUsePortal && gtk_check_version(3, 22, 0) == nullptr) {
|
|
|
|
nsCOMPtr<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(aParent);
|
|
|
|
NS_ASSERTION(widget, "Need a widget for dialog to be modal.");
|
|
|
|
GtkWindow* gtkParent = get_gtk_window_for_nsiwidget(widget);
|
|
|
|
NS_ASSERTION(gtkParent, "Need a GTK window for dialog to be modal.");
|
|
|
|
|
|
|
|
nsCOMPtr<nsPrintSettingsGTK> printSettingsGTK(do_QueryInterface(aSettings));
|
|
|
|
RefPtr<nsFlatpakPrintPortal> fpPrintPortal =
|
|
|
|
new nsFlatpakPrintPortal(printSettingsGTK);
|
|
|
|
|
|
|
|
nsresult rv = fpPrintPortal->PreparePrintRequest(gtkParent);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// This blocks until nsFlatpakPrintPortal::FinishPrintDialog is called
|
|
|
|
GtkPrintOperationResult printDialogResult = fpPrintPortal->GetResult();
|
|
|
|
|
|
|
|
switch (printDialogResult) {
|
|
|
|
case GTK_PRINT_OPERATION_RESULT_APPLY: {
|
|
|
|
nsCOMPtr<nsIObserverService> os =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
NS_ENSURE_STATE(os);
|
|
|
|
// Observer waits until notified that the file with the content
|
|
|
|
// to print has been written.
|
2018-10-02 00:38:01 +03:00
|
|
|
rv = os->AddObserver(fpPrintPortal, "print-to-file-finished", false);
|
2018-04-05 17:44:35 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GTK_PRINT_OPERATION_RESULT_CANCEL:
|
|
|
|
rv = NS_ERROR_ABORT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_WARNING("Unexpected response");
|
|
|
|
rv = NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-05-01 10:43:04 +04:00
|
|
|
nsPrintDialogWidgetGTK printDialog(aParent, aSettings);
|
2008-01-21 06:47:25 +03:00
|
|
|
nsresult rv = printDialog.ImportSettings(aSettings);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
const gint response = printDialog.Run();
|
|
|
|
|
|
|
|
// Handle the result
|
|
|
|
switch (response) {
|
|
|
|
case GTK_RESPONSE_OK: // Proceed
|
|
|
|
rv = printDialog.ExportSettings(aSettings);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GTK_RESPONSE_CANCEL:
|
|
|
|
case GTK_RESPONSE_CLOSE:
|
|
|
|
case GTK_RESPONSE_DELETE_EVENT:
|
|
|
|
case GTK_RESPONSE_NONE:
|
|
|
|
rv = NS_ERROR_ABORT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GTK_RESPONSE_APPLY: // Print preview
|
|
|
|
default:
|
|
|
|
NS_WARNING("Unexpected response");
|
|
|
|
rv = NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPrintDialogServiceGTK::ShowPageSetup(nsPIDOMWindowOuter* aParent,
|
2008-05-01 10:43:04 +04:00
|
|
|
nsIPrintSettings* aNSSettings) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aParent, "aParent must not be null");
|
|
|
|
MOZ_ASSERT(aNSSettings, "aSettings must not be null");
|
2008-01-21 06:47:25 +03:00
|
|
|
NS_ENSURE_TRUE(aNSSettings, NS_ERROR_FAILURE);
|
|
|
|
|
2010-06-26 01:51:17 +04:00
|
|
|
nsCOMPtr<nsIWidget> widget = WidgetUtils::DOMWindowToWidget(aParent);
|
|
|
|
NS_ASSERTION(widget, "Need a widget for dialog to be modal.");
|
|
|
|
GtkWindow* gtkParent = get_gtk_window_for_nsiwidget(widget);
|
2008-05-01 10:43:04 +04:00
|
|
|
NS_ASSERTION(gtkParent, "Need a GTK window for dialog to be modal.");
|
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
nsCOMPtr<nsPrintSettingsGTK> aNSSettingsGTK(do_QueryInterface(aNSSettings));
|
|
|
|
if (!aNSSettingsGTK) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// We need to init the prefs here because aNSSettings in its current form is a
|
|
|
|
// dummy in both uses of the word
|
|
|
|
nsCOMPtr<nsIPrintSettingsService> psService =
|
|
|
|
do_GetService("@mozilla.org/gfx/printsettings-service;1");
|
|
|
|
if (psService) {
|
2017-08-08 09:07:55 +03:00
|
|
|
nsString printName;
|
2017-10-09 02:08:09 +03:00
|
|
|
aNSSettings->GetPrinterName(printName);
|
2017-08-08 09:07:55 +03:00
|
|
|
if (printName.IsVoid()) {
|
2020-05-29 20:37:01 +03:00
|
|
|
psService->GetLastUsedPrinterName(printName);
|
2017-10-09 02:08:09 +03:00
|
|
|
aNSSettings->SetPrinterName(printName);
|
2008-01-21 06:47:25 +03:00
|
|
|
}
|
2011-10-03 11:56:21 +04:00
|
|
|
psService->InitPrintSettingsFromPrefs(aNSSettings, true,
|
|
|
|
nsIPrintSettings::kInitSaveAll);
|
2008-01-21 06:47:25 +03:00
|
|
|
}
|
|
|
|
|
2020-08-01 13:23:27 +03:00
|
|
|
// Frustratingly, gtk_print_run_page_setup_dialog doesn't tell us whether
|
|
|
|
// the user cancelled or confirmed the dialog! So to avoid needlessly
|
|
|
|
// refreshing the preview when Page Setup was cancelled, we compare the
|
|
|
|
// serializations of old and new settings; if they're the same, bail out.
|
2008-01-21 06:47:25 +03:00
|
|
|
GtkPrintSettings* gtkSettings = aNSSettingsGTK->GetGtkPrintSettings();
|
|
|
|
GtkPageSetup* oldPageSetup = aNSSettingsGTK->GetGtkPageSetup();
|
2020-08-01 13:23:27 +03:00
|
|
|
GKeyFile* oldKeyFile = g_key_file_new();
|
|
|
|
gtk_page_setup_to_key_file(oldPageSetup, oldKeyFile, nullptr);
|
|
|
|
gsize oldLength;
|
|
|
|
gchar* oldData = g_key_file_to_data(oldKeyFile, &oldLength, nullptr);
|
|
|
|
g_key_file_free(oldKeyFile);
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2008-05-01 10:43:04 +04:00
|
|
|
GtkPageSetup* newPageSetup =
|
|
|
|
gtk_print_run_page_setup_dialog(gtkParent, oldPageSetup, gtkSettings);
|
2008-01-21 06:47:25 +03:00
|
|
|
|
2020-08-01 13:23:27 +03:00
|
|
|
GKeyFile* newKeyFile = g_key_file_new();
|
|
|
|
gtk_page_setup_to_key_file(newPageSetup, newKeyFile, nullptr);
|
|
|
|
gsize newLength;
|
|
|
|
gchar* newData = g_key_file_to_data(newKeyFile, &newLength, nullptr);
|
|
|
|
g_key_file_free(newKeyFile);
|
|
|
|
|
|
|
|
bool unchanged =
|
|
|
|
(oldLength == newLength && !memcmp(oldData, newData, oldLength));
|
|
|
|
g_free(oldData);
|
|
|
|
g_free(newData);
|
|
|
|
if (unchanged) {
|
|
|
|
g_object_unref(newPageSetup);
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2008-01-21 06:47:25 +03:00
|
|
|
aNSSettingsGTK->SetGtkPageSetup(newPageSetup);
|
|
|
|
|
|
|
|
// Now newPageSetup has a refcount of 2 (SetGtkPageSetup will addref), put it
|
|
|
|
// to 1 so if this gets replaced we don't leak.
|
|
|
|
g_object_unref(newPageSetup);
|
|
|
|
|
|
|
|
if (psService)
|
2020-05-21 04:17:09 +03:00
|
|
|
psService->SavePrintSettingsToPrefs(
|
|
|
|
aNSSettings, true,
|
|
|
|
nsIPrintSettings::kInitSaveOrientation |
|
|
|
|
nsIPrintSettings::kInitSavePaperSize |
|
|
|
|
nsIPrintSettings::kInitSaveUnwriteableMargins);
|
2008-01-21 06:47:25 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|