David PHAM-VAN

Improve Windows directPrint

... ... @@ -6,6 +6,7 @@
- Document.save() now returns a Future
- Implement pan and zoom on PdfPreview widget
- Improve orientation handling
- Improve directPrint
## 3.7.2
... ...
... ... @@ -165,6 +165,9 @@ class MethodChannelPrinting extends PrintingPlatform {
name: printer['name'],
model: printer['model'],
location: printer['location'],
comment: printer['comment'],
isDefault: printer['default'],
available: printer['available'],
));
}
... ...
... ... @@ -25,6 +25,9 @@ class Printer {
this.name,
this.model,
this.location,
this.comment,
this.isDefault = false,
this.available = true,
}) : assert(url != null);
/// The platform specific printer identification
... ... @@ -39,6 +42,15 @@ class Printer {
/// The physical location of the printer
final String location;
/// A user comment about the printer
final String comment;
/// Is this the default printer on the system
final bool isDefault;
/// The printer is available for printing
final bool available;
@override
String toString() => name ?? url;
}
... ...
... ... @@ -65,6 +65,7 @@ mixin Printing {
static Future<Printer> pickPrinter({
@required BuildContext context,
Rect bounds,
String title,
}) async {
final _info = await info();
... ... @@ -74,17 +75,35 @@ mixin Printing {
'Pass a BuildContext to pickPrinter to display a selection list',
);
final printers = await listPrinters();
printers.sort((a, b) {
if (a.isDefault) {
return -1;
}
if (b.isDefault) {
return 1;
}
return a.name.compareTo(b.name);
});
return await showDialog<Printer>(
context: context,
builder: (context) => SimpleDialog(
children: printers
.map<Widget>(
(e) => SimpleDialogOption(
child: Text(e.name),
onPressed: () => Navigator.of(context).pop(e),
title: Text(title ?? 'Select Printer'),
children: [
for (final printer in printers)
if (printer.available)
SimpleDialogOption(
child: Text(
printer.name,
style: TextStyle(
fontStyle: printer.isDefault
? FontStyle.italic
: FontStyle.normal,
),
),
onPressed: () => Navigator.of(context).pop(printer),
),
)
.toList(),
],
),
);
}
... ...
... ... @@ -134,6 +134,7 @@ bool PrintJob::printPdf(std::string name) {
ZeroMemory(dm, sizeof(DEVMODE));
dm->dmFields = DM_ORIENTATION;
dm->dmOrientation = 2;
printf("Printing ... \n");
// Initialize PRINTDLG
ZeroMemory(&pd, sizeof(pd));
... ... @@ -186,19 +187,28 @@ bool PrintJob::printPdf(std::string name) {
}
std::vector<Printer> PrintJob::listPrinters() {
LPTSTR defaultPrinter;
DWORD size = 0;
GetDefaultPrinter(nullptr, &size);
defaultPrinter = static_cast<LPTSTR>(malloc(size * sizeof(TCHAR)));
if (!GetDefaultPrinter(defaultPrinter, &size)) {
size = 0;
}
auto printers = std::vector<Printer>{};
DWORD needed = 0;
DWORD returned = 0;
const auto flags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS;
EnumPrinters(flags, nullptr, 1, nullptr, 0, &needed, &returned);
EnumPrinters(flags, nullptr, 2, nullptr, 0, &needed, &returned);
auto buffer = (PRINTER_INFO_1*)malloc(needed);
auto buffer = (PRINTER_INFO_2*)malloc(needed);
if (!buffer) {
return printers;
}
auto result = EnumPrinters(flags, nullptr, 1, (LPBYTE)buffer, needed, &needed,
auto result = EnumPrinters(flags, nullptr, 2, (LPBYTE)buffer, needed, &needed,
&returned);
if (result == 0) {
... ... @@ -208,14 +218,17 @@ std::vector<Printer> PrintJob::listPrinters() {
for (DWORD i = 0; i < returned; i++) {
printers.push_back(Printer{
toUtf8(buffer[i].pName),
toUtf8(buffer[i].pName),
toUtf8(buffer[i].pDescription),
toUtf8(buffer[i].pPrinterName), toUtf8(buffer[i].pPrinterName),
toUtf8(buffer[i].pDriverName), toUtf8(buffer[i].pLocation),
toUtf8(buffer[i].pComment),
});
size > 0 && _tcsncmp(buffer[i].pPrinterName, defaultPrinter, size) == 0,
(buffer[i].Status &
(PRINTER_STATUS_NOT_AVAILABLE | PRINTER_STATUS_ERROR |
PRINTER_STATUS_OFFLINE | PRINTER_STATUS_PAUSED)) == 0});
}
free(buffer);
free(defaultPrinter);
return printers;
}
... ...
... ... @@ -34,13 +34,25 @@ struct Printer {
const std::string name;
const std::string url;
const std::string model;
const std::string description;
const std::string location;
const std::string comment;
const bool default;
const bool available;
Printer(std::string name,
std::string url,
std::string model,
std::string description)
: name(name), url(url), model(model), description(description) {}
std::string location,
std::string comment,
bool default,
bool available)
: name(name),
url(url),
model(model),
location(location),
comment(comment),
default(default),
available(available) {}
};
class PrintJob {
... ...
... ... @@ -125,8 +125,14 @@ class PrintingPlugin : public flutter::Plugin {
flutter::EncodableValue(printer.url);
mp[flutter::EncodableValue("model")] =
flutter::EncodableValue(printer.model);
mp[flutter::EncodableValue("description")] =
flutter::EncodableValue(printer.description);
mp[flutter::EncodableValue("location")] =
flutter::EncodableValue(printer.location);
mp[flutter::EncodableValue("comment")] =
flutter::EncodableValue(printer.comment);
mp[flutter::EncodableValue("default")] =
flutter::EncodableValue(printer.default);
mp[flutter::EncodableValue("available")] =
flutter::EncodableValue(printer.available);
pl.push_back(mp);
}
result->Success(pl);
... ...