David PHAM-VAN

Improve Linux and Windows printing

... ... @@ -27,6 +27,7 @@ class PdfPreview extends StatefulWidget {
this.pageFormats,
this.onError,
this.onPrinted,
this.onPrintError,
this.onShared,
this.scrollViewDecoration,
this.pdfPreviewPageDecoration,
... ... @@ -65,12 +66,15 @@ class PdfPreview extends StatefulWidget {
/// List of page formats the user can choose
final Map<String, PdfPageFormat>? pageFormats;
/// Called if an error creating the Pdf occured
/// Widget to display if the PDF document cannot be displayed
final Widget Function(BuildContext context)? onError;
/// Called if the user prints the pdf document
final void Function(BuildContext context)? onPrinted;
/// Called if an error creating the Pdf occured
final void Function(BuildContext context, dynamic error)? onPrintError;
/// Called if the user shares the pdf document
final void Function(BuildContext context)? onShared;
... ... @@ -514,8 +518,8 @@ class _PdfPreviewState extends State<PdfPreview> {
widget.onPrinted!(context);
}
} catch (e) {
if (widget.onError != null) {
widget.onError!(context);
if (widget.onPrintError != null) {
widget.onPrintError!(context, e);
}
}
}
... ...
... ... @@ -138,29 +138,34 @@ bool print_job::print_pdf(const gchar* name,
GTK_PRINT_UNIX_DIALOG(gtk_print_unix_dialog_new(name, nullptr));
gtk_print_unix_dialog_set_manual_capabilities(
dialog, (GtkPrintCapabilities)(GTK_PRINT_CAPABILITY_GENERATE_PDF));
gtk_print_unix_dialog_set_embed_page_setup(dialog, true);
gtk_print_unix_dialog_set_support_selection(dialog, false);
gtk_widget_realize(GTK_WIDGET(dialog));
auto response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_hide(GTK_WIDGET(dialog));
switch (response) {
case GTK_RESPONSE_OK: {
_printer = gtk_print_unix_dialog_get_selected_printer(
GTK_PRINT_UNIX_DIALOG(dialog));
settings =
gtk_print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(dialog));
setup =
gtk_print_unix_dialog_get_page_setup(GTK_PRINT_UNIX_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
} break;
case GTK_RESPONSE_DELETE_EVENT: // Fall through.
case GTK_RESPONSE_CANCEL: // Cancel
case GTK_RESPONSE_APPLY: // Preview
default:
gtk_widget_destroy(GTK_WIDGET(dialog));
on_completed(this, false, nullptr);
return true;
auto loop = true;
while (loop) {
auto response = gtk_dialog_run(GTK_DIALOG(dialog));
switch (response) {
case GTK_RESPONSE_OK: {
_printer = gtk_print_unix_dialog_get_selected_printer(
GTK_PRINT_UNIX_DIALOG(dialog));
settings =
gtk_print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(dialog));
setup = gtk_print_unix_dialog_get_page_setup(
GTK_PRINT_UNIX_DIALOG(dialog));
gtk_widget_destroy(GTK_WIDGET(dialog));
loop = false;
} break;
case GTK_RESPONSE_APPLY: // Preview
break;
default: // Cancel
gtk_widget_destroy(GTK_WIDGET(dialog));
on_completed(this, false, nullptr);
return true;
}
}
}
... ... @@ -172,8 +177,8 @@ bool print_job::print_pdf(const gchar* name,
return false;
}
auto _width = gtk_page_setup_get_page_width(setup, GTK_UNIT_POINTS);
auto _height = gtk_page_setup_get_page_height(setup, GTK_UNIT_POINTS);
auto _width = gtk_page_setup_get_paper_width(setup, GTK_UNIT_POINTS);
auto _height = gtk_page_setup_get_paper_height(setup, GTK_UNIT_POINTS);
auto _marginLeft = gtk_page_setup_get_left_margin(setup, GTK_UNIT_POINTS);
auto _marginTop = gtk_page_setup_get_top_margin(setup, GTK_UNIT_POINTS);
auto _marginRight = gtk_page_setup_get_right_margin(setup, GTK_UNIT_POINTS);
... ...
... ... @@ -71,7 +71,7 @@ std::wstring fromUtf8(std::string str) {
}
PrintJob::PrintJob(Printing* printing, int index)
: printing(printing), index(index) {}
: printing{printing}, index{index} {}
bool PrintJob::printPdf(std::string name, std::string printer) {
documentName = name;
... ... @@ -102,11 +102,18 @@ bool PrintJob::printPdf(std::string name, std::string printer) {
auto r = PrintDlg(&pd);
if (r == 1) {
if (r != 1) {
printing->onCompleted(this, false, "");
DeleteDC(hDC);
GlobalFree(hDevNames);
ClosePrinter(hDevMode);
return true;
}
hDC = pd.hDC;
hDevMode = pd.hDevMode;
hDevNames = pd.hDevNames;
}
} else {
hDC = CreateDC(TEXT("WINSPOOL"), fromUtf8(printer).c_str(), NULL, NULL);
if (!hDC) {
... ... @@ -235,6 +242,8 @@ void PrintJob::writeJob(std::vector<uint8_t> data) {
DeleteDC(hDC);
GlobalFree(hDevNames);
ClosePrinter(hDevMode);
printing->onCompleted(this, true, "");
}
void PrintJob::cancelJob(std::string error) {}
... ...
... ... @@ -55,7 +55,7 @@ void Printing::onPageRasterEnd(PrintJob* job) {
class OnLayoutResult : public flutter::MethodResult<flutter::EncodableValue> {
public:
OnLayoutResult(PrintJob* job) : job(job) {}
OnLayoutResult(PrintJob* job) : job{job} {}
private:
PrintJob* job;
... ...