From 1a515725b3ed905b0a0b7db1612ff4605358f79a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 15 May 2021 23:20:02 +0200 Subject: [PATCH 1/5] Suppress ASAN leak reports when using print dialog in wxGTK There is nothing we can do about them, as memory is allocated from a CUPS functions used by GTK itself, so suppress them. --- misc/suppressions/lsan | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/misc/suppressions/lsan b/misc/suppressions/lsan index 0180aa9303..daea70f4ef 100644 --- a/misc/suppressions/lsan +++ b/misc/suppressions/lsan @@ -43,3 +43,8 @@ leak:_cairo_surface_create_similar_image leak:_cairo_surface_create_similar_image leak:gtk_css_image_surface_draw leak:_gtk_css_image_draw + +# Avoid leak reports whenever GtkPrintOperation is used (hopefully this is just +# a one off allocation not freed and not an unbounded leak). +leak:cups_request_printer_list_cb +leak:cups_dispatch_watch_dispatch From 16ca2df0c4a55421c0568049b509c6c15ec74be3 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 16 May 2021 00:46:27 +0200 Subject: [PATCH 2/5] Refactor return code in wxGtkPrinter::Print() No real changes, just make it simpler to do other things before returning successfully by handling error returns separately. This is also more consistent with PrintDialog() method of the same class. No real changes. --- src/gtk/print.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/gtk/print.cpp b/src/gtk/print.cpp index 1e758eac43..f3376a6fb5 100644 --- a/src/gtk/print.cpp +++ b/src/gtk/print.cpp @@ -977,19 +977,23 @@ bool wxGtkPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt ) // doesn't necessarily show int ret = dialog.ShowModal(); - if (ret == wxID_CANCEL) - { - sm_lastError = wxPRINTER_CANCELLED; - } - if (ret == wxID_NO) - { - sm_lastError = wxPRINTER_ERROR; - } printout->SetDC(NULL); wxDELETE(m_dc); - return (sm_lastError == wxPRINTER_NO_ERROR); + if (ret == wxID_CANCEL) + { + sm_lastError = wxPRINTER_CANCELLED; + return false; + } + if (ret == wxID_NO) + { + sm_lastError = wxPRINTER_ERROR; + return false; + } + + sm_lastError = wxPRINTER_NO_ERROR; + return true; } void wxGtkPrinter::BeginPrint(wxPrintout *printout, GtkPrintOperation *operation, GtkPrintContext *context) From 131d07b4304fe364ed68dfb27194d1dd14aba05b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 16 May 2021 00:47:50 +0200 Subject: [PATCH 3/5] Update print dialog data after printing successfully This is necessary in order to get the information entered by the user in the dialog and was already done in PrintDialog(), but not Print() itself -- now do it there as well. --- src/gtk/print.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gtk/print.cpp b/src/gtk/print.cpp index f3376a6fb5..a6a3b6a01c 100644 --- a/src/gtk/print.cpp +++ b/src/gtk/print.cpp @@ -992,6 +992,8 @@ bool wxGtkPrinter::Print(wxWindow *parent, wxPrintout *printout, bool prompt ) return false; } + m_printDialogData = dialog.GetPrintDialogData(); + sm_lastError = wxPRINTER_NO_ERROR; return true; } From 2514945ab3e181f32d0c6495f38f493f4682193c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 16 May 2021 00:49:30 +0200 Subject: [PATCH 4/5] Fix retrieving paper size and orientation from GTK print dialog The paper size and orientation in wxPrintData were never updated because we didn't retrieve them from GTK correctly: they need to be extracted from "default-page-setup" property and not the main GtkPrintSettings themselves, at least with GTK 3. --- src/gtk/print.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/gtk/print.cpp b/src/gtk/print.cpp index a6a3b6a01c..3cc38f6bf0 100644 --- a/src/gtk/print.cpp +++ b/src/gtk/print.cpp @@ -724,6 +724,19 @@ int wxGtkPrintDialog::ShowModal() // Now get the settings and save it. GtkPrintSettings* newSettings = gtk_print_operation_get_print_settings(printOp); + + // When embedding the page setup tab into the dialog, as we do, changes to + // the settings such as the paper size and orientation there are not + // reflected in the print settings, but must be retrieved from the page + // setup struct itself separately. + GtkPageSetup* defPageSetup = NULL; + g_object_get(printOp, "default-page-setup", &defPageSetup, NULL); + if ( defPageSetup ) + { + native->SetPageSetupToSettings(newSettings, defPageSetup); + g_object_unref(defPageSetup); + } + native->SetPrintConfig(newSettings); data.ConvertFromNative(); From fc2e01d9a262c6ae48acabc3ff1605eb804acf1e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 16 May 2021 00:52:55 +0200 Subject: [PATCH 5/5] Make GtkPageSetup-related functions private These functions don't need to be members of wxGtkPrintNativeData as they don't use this object at all, so one shouldn't be required to call them. And rather than making them static, just make them private functions instead. No real changes, this is just a refactoring. --- include/wx/gtk/print.h | 4 ---- src/gtk/print.cpp | 17 +++++++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/wx/gtk/print.h b/include/wx/gtk/print.h index 9458ce7643..22b2b86f21 100644 --- a/include/wx/gtk/print.h +++ b/include/wx/gtk/print.h @@ -197,10 +197,6 @@ public: GtkPrintContext *GetPrintContext() { return m_context; } void SetPrintContext(GtkPrintContext *context) {m_context = context; } - - GtkPageSetup* GetPageSetupFromSettings(GtkPrintSettings* settings); - void SetPageSetupToSettings(GtkPrintSettings* settings, GtkPageSetup* page_setup); - private: // NB: m_config is created and owned by us, but the other objects are not // and their accessors don't change their ref count. diff --git a/src/gtk/print.cpp b/src/gtk/print.cpp index 3cc38f6bf0..751fb2afc5 100644 --- a/src/gtk/print.cpp +++ b/src/gtk/print.cpp @@ -583,8 +583,11 @@ void wxGtkPrintNativeData::SetPrintConfig( GtkPrintSettings * config ) } } +namespace +{ + // Extract page setup from settings. -GtkPageSetup* wxGtkPrintNativeData::GetPageSetupFromSettings(GtkPrintSettings* settings) +GtkPageSetup* GetPageSetupFromSettings(GtkPrintSettings* settings) { GtkPageSetup* page_setup = gtk_page_setup_new(); gtk_page_setup_set_orientation (page_setup, gtk_print_settings_get_orientation (settings)); @@ -600,12 +603,14 @@ GtkPageSetup* wxGtkPrintNativeData::GetPageSetupFromSettings(GtkPrintSettings* s } // Insert page setup into a given GtkPrintSettings. -void wxGtkPrintNativeData::SetPageSetupToSettings(GtkPrintSettings* settings, GtkPageSetup* page_setup) +void SetPageSetupToSettings(GtkPrintSettings* settings, GtkPageSetup* page_setup) { gtk_print_settings_set_orientation ( settings, gtk_page_setup_get_orientation (page_setup)); gtk_print_settings_set_paper_size ( settings, gtk_page_setup_get_paper_size (page_setup)); } +} // anonymous namespace + //---------------------------------------------------------------------------- // wxGtkPrintDialog //---------------------------------------------------------------------------- @@ -692,7 +697,7 @@ int wxGtkPrintDialog::ShowModal() // If the settings are OK, we restore it. if (settings != NULL) gtk_print_operation_set_print_settings (printOp, settings); - GtkPageSetup* pgSetup = native->GetPageSetupFromSettings(settings); + GtkPageSetup* pgSetup = GetPageSetupFromSettings(settings); gtk_print_operation_set_default_page_setup (printOp, pgSetup); g_object_unref(pgSetup); @@ -733,7 +738,7 @@ int wxGtkPrintDialog::ShowModal() g_object_get(printOp, "default-page-setup", &defPageSetup, NULL); if ( defPageSetup ) { - native->SetPageSetupToSettings(newSettings, defPageSetup); + SetPageSetupToSettings(newSettings, defPageSetup); g_object_unref(defPageSetup); } @@ -816,7 +821,7 @@ int wxGtkPageSetupDialog::ShowModal() GtkPrintSettings* nativeData = native->GetPrintConfig(); // We only need the pagesetup data which are part of the settings. - GtkPageSetup* oldPageSetup = native->GetPageSetupFromSettings(nativeData); + GtkPageSetup* oldPageSetup = GetPageSetupFromSettings(nativeData); // If the user used a custom paper format the last time he printed, we have to restore it too. wxPaperSize paperId = m_pageDialogData.GetPrintData().GetPaperId(); @@ -871,7 +876,7 @@ int wxGtkPageSetupDialog::ShowModal() wxGtkObject newPageSetup(gtk_page_setup_unix_dialog_get_page_setup( GTK_PAGE_SETUP_UNIX_DIALOG(dlg))); - native->SetPageSetupToSettings(nativeData, newPageSetup); + SetPageSetupToSettings(nativeData, newPageSetup); m_pageDialogData.GetPrintData().ConvertFromNative();