From b8f97a8df4f897127e997a6f526a3d0d7ca6d918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Sat, 12 Oct 2013 12:45:15 +0000 Subject: [PATCH] [GB.GTK] * NEW: Printer.Default is a new property that returns the name of the default printer. * NEW: Printer.List is a new property that returns the list of the names of all available printers. [GB.QT4] * NEW: Printer.Default is a new property that returns the name of the default printer. * NEW: Printer.List is a new property that returns the list of the names of all available printers. git-svn-id: svn://localhost/gambas/trunk@5874 867c0c6c-44f3-4631-809d-bfa615b0a4ec --- gb.gtk/src/cprinter.cpp | 42 +++++++++++++++++++++++++++++++++++++++ gb.gtk/src/gprinter.cpp | 12 +++++++++++ gb.gtk/src/gprinter.h | 2 ++ gb.qt4/src/cprinter.cpp | 44 ++++++++++++++++++++++++++++++++++++----- 4 files changed, 95 insertions(+), 5 deletions(-) diff --git a/gb.gtk/src/cprinter.cpp b/gb.gtk/src/cprinter.cpp index c2dfc03a5..e34d986ed 100644 --- a/gb.gtk/src/cprinter.cpp +++ b/gb.gtk/src/cprinter.cpp @@ -273,11 +273,53 @@ BEGIN_PROPERTY(Printer_OutputFile) END_PROPERTY +static bool find_default_printer(const char *name, bool def) +{ + if (def) + { + GB.ReturnNewZeroString(name); + return true; + } + else + return false; +} + +BEGIN_PROPERTY(Printer_Default) + + GB.ReturnNull(); + gPrinter::enumeratePrinters(find_default_printer); + +END_PROPERTY + +static GB_ARRAY _list = NULL; + +static bool add_printer(const char *name, bool def) +{ + *((char **)GB.Array.Add(_list)) = GB.NewZeroString(name); + return FALSE; +} + +BEGIN_PROPERTY(Printer_List) + + GB_ARRAY array; + + GB.Array.New(&array, GB_T_STRING, 0); + _list = array; + gPrinter::enumeratePrinters(add_printer); + _list = NULL; + GB.ReturnObject(array); + +END_PROPERTY + + GB_DESC PrinterDesc[] = { GB_DECLARE("Printer", sizeof(CPRINTER)), + GB_STATIC_PROPERTY_READ("Default", "s", Printer_Default), + GB_STATIC_PROPERTY_READ("List", "String[]", Printer_List), + GB_CONSTANT("Portrait", "i", GB_PRINT_PORTRAIT), GB_CONSTANT("Landscape", "i", GB_PRINT_LANDSCAPE), //GB_CONSTANT("ReversePortrait", "i", GB_PRINT_REVERSE_PORTRAIT), diff --git a/gb.gtk/src/gprinter.cpp b/gb.gtk/src/gprinter.cpp index c94b82f62..39caa8256 100644 --- a/gb.gtk/src/gprinter.cpp +++ b/gb.gtk/src/gprinter.cpp @@ -702,3 +702,15 @@ void gPrinter::fixPrintDialog(GtkPrintUnixDialog *dialog) dump_tree(GTK_WIDGET(dialog), dialog); } +static gboolean find_all_printers(GtkPrinter *gtk_printer, bool (*callback)(const char *, bool)) +{ + if (strcmp(G_OBJECT_TYPE_NAME(gtk_printer_get_backend(gtk_printer)), "GtkPrintBackendFile")) + return (*callback)(gtk_printer_get_name(gtk_printer), gtk_printer_is_default(gtk_printer)); + + return FALSE; +} + +void gPrinter::enumeratePrinters(bool (*callback)(const char *, bool)) +{ + gtk_enumerate_printers((GtkPrinterFunc)find_all_printers, (gpointer)callback, NULL, TRUE); +} diff --git a/gb.gtk/src/gprinter.h b/gb.gtk/src/gprinter.h index 9ecdaafbd..f5ada067b 100644 --- a/gb.gtk/src/gprinter.h +++ b/gb.gtk/src/gprinter.h @@ -96,6 +96,8 @@ public: static void fixPrintDialog(GtkPrintUnixDialog *dialog); static gPrinter *_current; + + static void enumeratePrinters(bool (*callback)(const char *name, bool)); private: bool run(bool configure); diff --git a/gb.qt4/src/cprinter.cpp b/gb.qt4/src/cprinter.cpp index ae911835b..fecd1a907 100644 --- a/gb.qt4/src/cprinter.cpp +++ b/gb.qt4/src/cprinter.cpp @@ -25,6 +25,7 @@ #include #include +#include #include "gb.form.print.h" #include "gb.form.properties.h" @@ -295,8 +296,12 @@ BEGIN_PROPERTY(Printer_PaperWidth) GB.ReturnFloat(floor((double)size.width() * 1E6) / 1E6); else { - size.setWidth((qreal)VPROP(GB_FLOAT)); - PRINTER->setPaperSize(size, QPrinter::Millimeter); + qreal width = (qreal)VPROP(GB_FLOAT); + if (width != size.width()) + { + size.setWidth(width); + PRINTER->setPaperSize(size, QPrinter::Millimeter); + } } END_PROPERTY @@ -309,8 +314,12 @@ BEGIN_PROPERTY(Printer_PaperHeight) GB.ReturnFloat(floor((double)size.height() * 1E6) / 1E6); else { - size.setHeight((qreal)VPROP(GB_FLOAT)); - PRINTER->setPaperSize(size, QPrinter::Millimeter); + qreal height = (qreal)VPROP(GB_FLOAT); + if (height != size.height()) + { + size.setHeight(height); + PRINTER->setPaperSize(size, QPrinter::Millimeter); + } } END_PROPERTY @@ -416,11 +425,37 @@ BEGIN_PROPERTY(Printer_OutputFile) END_PROPERTY +BEGIN_PROPERTY(Printer_Default) + + QPrinterInfo info = QPrinterInfo::defaultPrinter(); + + if (info.isNull()) + GB.ReturnNull(); + else + GB.ReturnNewZeroString(info.printerName()); + +END_PROPERTY + +BEGIN_PROPERTY(Printer_List) + + GB_ARRAY array; + QList list = QPrinterInfo::availablePrinters(); + + GB.Array.New(&array, GB_T_STRING, list.length()); + for (int i = 0; i < list.length(); i++) + *((char **)GB.Array.Get(array, i)) = GB.NewZeroString(list.at(i).printerName()); + + GB.ReturnObject(array); + +END_PROPERTY GB_DESC PrinterDesc[] = { GB_DECLARE("Printer", sizeof(CPRINTER)), + GB_STATIC_PROPERTY_READ("Default", "s", Printer_Default), + GB_STATIC_PROPERTY_READ("List", "String[]", Printer_List), + GB_CONSTANT("Portrait", "i", GB_PRINT_PORTRAIT), GB_CONSTANT("Landscape", "i", GB_PRINT_LANDSCAPE), //GB_CONSTANT("ReversePortrait", "i", GB_PRINT_REVERSE_PORTRAIT), @@ -476,4 +511,3 @@ GB_DESC PrinterDesc[] = GB_END_DECLARE }; -