From b599f8f44eb5175c39e013a0f86a5356878225e8 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sat, 31 Aug 2019 02:09:04 -0400 Subject: [PATCH] Preliminary support for Printing with PrintHandlers --- .../UniversalEditor.Printing/PrintHandler.cs | 46 ++++++ .../PrintHandlerReference.cs | 147 ++++++++++++++++++ .../Properties/AssemblyInfo.cs | 26 ++++ .../UniversalEditor.Printing.csproj | 43 +++++ .../MainWindow.cs | 81 +++++++++- .../UniversalEditor.UserInterface.csproj | 4 + CSharp/UniversalEditor.sln | 7 + 7 files changed, 352 insertions(+), 2 deletions(-) create mode 100644 CSharp/Libraries/UniversalEditor.Printing/PrintHandler.cs create mode 100644 CSharp/Libraries/UniversalEditor.Printing/PrintHandlerReference.cs create mode 100644 CSharp/Libraries/UniversalEditor.Printing/Properties/AssemblyInfo.cs create mode 100644 CSharp/Libraries/UniversalEditor.Printing/UniversalEditor.Printing.csproj diff --git a/CSharp/Libraries/UniversalEditor.Printing/PrintHandler.cs b/CSharp/Libraries/UniversalEditor.Printing/PrintHandler.cs new file mode 100644 index 00000000..8186b438 --- /dev/null +++ b/CSharp/Libraries/UniversalEditor.Printing/PrintHandler.cs @@ -0,0 +1,46 @@ +// +// PrintHandler.cs +// +// Author: +// Mike Becker +// +// Copyright (c) 2019 Mike Becker +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +using System; +namespace UniversalEditor.Printing +{ + public abstract class PrintHandler : References + { + public PrintHandlerReference Reference { get; internal set; } + + protected virtual PrintHandlerReference MakeReferenceInternal() + { + return new PrintHandlerReference(this.GetType()); + } + public PrintHandlerReference MakeReference() + { + PrintHandlerReference phr = MakeReferenceInternal(); + PrintHandlerReference.Register(phr); + return phr; + } + + protected abstract void PrintInternal(ObjectModel objectModel); + public void Print(ObjectModel objectModel) + { + PrintInternal(objectModel); + } + } +} diff --git a/CSharp/Libraries/UniversalEditor.Printing/PrintHandlerReference.cs b/CSharp/Libraries/UniversalEditor.Printing/PrintHandlerReference.cs new file mode 100644 index 00000000..e1f70e69 --- /dev/null +++ b/CSharp/Libraries/UniversalEditor.Printing/PrintHandlerReference.cs @@ -0,0 +1,147 @@ +// +// PrintHandlerReference.cs +// +// Author: +// Mike Becker +// +// Copyright (c) 2019 Mike Becker +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using System.Collections.Generic; + +namespace UniversalEditor.Printing +{ + public class PrintHandlerReference : ReferencedBy + { + public PrintHandlerReference(Guid id) + { + mvarID = id; + } + public PrintHandlerReference(string dataFormatTypeName) + { + mvarTypeName = dataFormatTypeName; + } + public PrintHandlerReference(Type dataFormatType) + { + if (!dataFormatType.IsSubclassOf(typeof(PrintHandler))) + { + throw new InvalidCastException("Cannot create a data format reference to a non-PrintHandler type"); + } + else if (dataFormatType.IsAbstract) + { + throw new InvalidOperationException("Cannot create a data format reference to an abstract type"); + } + + mvarType = dataFormatType; + } + + private Guid mvarID = Guid.Empty; + public Guid ID { get { return mvarID; } set { mvarID = value; } } + + private Type mvarType = null; + public Type Type { get { return mvarType; } } + + private string mvarTypeName = null; + public string TypeName { get { return mvarTypeName; } set { mvarTypeName = value; } } + + public virtual PrintHandler Create() + { + PrintHandler df = (mvarType.Assembly.CreateInstance(mvarType.FullName) as PrintHandler); + df.Reference = this; + return df; + } + + public string[] GetDetails() + { + throw new NotImplementedException(); + } + + + private static Dictionary _referencesByGUID = new Dictionary(); + private static Dictionary _referencesByTypeName = new Dictionary(); + + public static bool Register(PrintHandlerReference dfr) + { + bool retval = false; + if (dfr.Type != null) + { + dfr.TypeName = dfr.Type.FullName; + } + if (dfr.ID != Guid.Empty) + { + _referencesByGUID[dfr.ID] = dfr; + retval = true; + } + if (dfr.TypeName != null) + { + _referencesByTypeName[dfr.TypeName] = dfr; + retval = true; + } + return retval; + } + public static bool Unregister(PrintHandlerReference dfr) + { + bool retval = false; + if (dfr.ID != Guid.Empty && _referencesByGUID.ContainsKey(dfr.ID)) + { + _referencesByGUID.Remove(dfr.ID); + retval = true; + } + if (dfr.TypeName != null && _referencesByTypeName.ContainsKey(dfr.TypeName)) + { + _referencesByTypeName.Remove(dfr.TypeName); + retval = true; + } + return retval; + } + + public static PrintHandlerReference FromTypeName(string typeName) + { + if (_referencesByTypeName.ContainsKey(typeName)) return _referencesByTypeName[typeName]; + return null; + } + public static PrintHandlerReference FromGUID(Guid guid) + { + if (_referencesByGUID.ContainsKey(guid)) return _referencesByGUID[guid]; + return null; + } + public override bool Equals(object obj) + { + PrintHandlerReference omr = (obj as PrintHandlerReference); + if (omr == null) return false; + if (mvarID == Guid.Empty) + { + // do not compare ID + if (mvarTypeName == null) return false; + return mvarTypeName.Equals(omr.TypeName); + } + return mvarID.Equals(omr.ID); + } + public int CompareTo(PrintHandlerReference other) + { + if (mvarID == Guid.Empty) + { + // do not compare ID + if (mvarTypeName == null) + { + if (other.ID == Guid.Empty && other.TypeName == null) return 0; + return -1; + } + return mvarTypeName.CompareTo(other.TypeName); + } + return mvarID.CompareTo(other.ID); + } + } +} diff --git a/CSharp/Libraries/UniversalEditor.Printing/Properties/AssemblyInfo.cs b/CSharp/Libraries/UniversalEditor.Printing/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..a47e58ca --- /dev/null +++ b/CSharp/Libraries/UniversalEditor.Printing/Properties/AssemblyInfo.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("UniversalEditor.Printing")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("Mike Becker")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/CSharp/Libraries/UniversalEditor.Printing/UniversalEditor.Printing.csproj b/CSharp/Libraries/UniversalEditor.Printing/UniversalEditor.Printing.csproj new file mode 100644 index 00000000..c592d299 --- /dev/null +++ b/CSharp/Libraries/UniversalEditor.Printing/UniversalEditor.Printing.csproj @@ -0,0 +1,43 @@ + + + + Debug + AnyCPU + {868A6888-EDB9-407E-A710-40F297D4EAB8} + Library + UniversalEditor.Printing + UniversalEditor.Printing + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + false + + + true + bin\Release + prompt + 4 + false + + + + + + + + + + + + {2D4737E6-6D95-408A-90DB-8DFF38147E85} + UniversalEditor.Core + + + + \ No newline at end of file diff --git a/CSharp/Libraries/UniversalEditor.UserInterface/MainWindow.cs b/CSharp/Libraries/UniversalEditor.UserInterface/MainWindow.cs index a60c46ab..dede4710 100644 --- a/CSharp/Libraries/UniversalEditor.UserInterface/MainWindow.cs +++ b/CSharp/Libraries/UniversalEditor.UserInterface/MainWindow.cs @@ -22,6 +22,8 @@ using MBS.Framework.Drawing; // TODO: We need to work on UWT signaling to native objects... using UniversalWidgetToolkit.Layouts; using UniversalWidgetToolkit.Controls.Ribbon; +using UniversalEditor.Printing; +using UniversalWidgetToolkit.Printing; namespace UniversalEditor.UserInterface { @@ -628,16 +630,91 @@ namespace UniversalEditor.UserInterface this.Destroy (); } + private class TestPrintHandler : PrintHandler + { + private static PrintHandlerReference _phr = null; + protected override PrintHandlerReference MakeReferenceInternal() + { + if (_phr == null) + { + _phr = base.MakeReferenceInternal(); + } + return _phr; + } + + protected override void PrintInternal(ObjectModel objectModel) + { + } + } + public void PrintDocument() { Editor editor = GetCurrentEditor (); if (editor != null) { - PrintDialog dlg = new PrintDialog (); - if (dlg.ShowDialog () == DialogResult.OK) { + PrintHandler ph1 = new TestPrintHandler(); + PrintHandlerReference[] phrs = { ph1.MakeReference() }; // PrintHandler.GetAvailablePrintHandlers(editor.ObjectModel); + if (phrs.Length > 0) + { + PrintDialog dlg = new PrintDialog(); + if (dlg.ShowDialog(this) == DialogResult.OK) + { + PrintHandler ph = phrs[0].Create(); + if (ph != null) + { + PrintJob job = new PrintJob("Test Page", dlg.SelectedPrinter, dlg.Settings); + job.BeginPrint += Job_BeginPrint; + job.DrawPage += Job_DrawPage; + job.SetExtraData("ph", ph); + job.SetExtraData("om", editor.ObjectModel); + + job.Send(); + } + } } } } + void Job_DrawPage(object sender, PrintEventArgs e) + { + PrintJob job = (sender as PrintJob); + PrintHandler ph = job.GetExtraData("ph"); + ObjectModel om = job.GetExtraData("om"); + + + e.Graphics.DrawRectangle(new Pen(MBS.Framework.Drawing.Colors.Gray, new Measurement(1.0, MeasurementUnit.Pixel)), new Rectangle(20, 20, 120, 80)); + e.Graphics.DrawRectangle(new Pen(MBS.Framework.Drawing.Colors.Gray, new Measurement(1.0, MeasurementUnit.Pixel)), new Rectangle(180, 20, 80, 80)); + + e.Graphics.FillRectangle(new SolidBrush(MBS.Framework.Drawing.Colors.Gray), new Rectangle(20, 20, 120, 80)); + e.Graphics.FillRectangle(new SolidBrush(MBS.Framework.Drawing.Colors.Gray), new Rectangle(180, 20, 80, 80)); + + + // if (settings != NULL) + // print.Settings = settings; // gtk_print_operation_set_print_settings(print, settings); + + // res = gtk_print_operation_run(print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, + // GTK_WINDOW(main_window), NULL); + + // if (res == GTK_PRINT_OPERATION_RESULT_APPLY) + // { + // if (settings != NULL)x + // g_object_unref(settings); + // settings = g_object_ref(gtk_print_operation_get_print_settings(print)); + //} + + //g_object_unref(print); + // ph.Print(om); + } + + + void Job_BeginPrint(object sender, PrintEventArgs e) + { + PrintJob job = (sender as PrintJob); + + PrintHandler ph = job.GetExtraData("ph"); + ObjectModel om = job.GetExtraData("om"); + } + + public Editor GetCurrentEditor() { Pages.EditorPage page = GetCurrentEditorPage (); diff --git a/CSharp/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj b/CSharp/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj index f46a228e..3cc37441 100644 --- a/CSharp/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj +++ b/CSharp/Libraries/UniversalEditor.UserInterface/UniversalEditor.UserInterface.csproj @@ -149,6 +149,10 @@ {23ACD09E-E096-4B05-A6CE-6853EDDC589A} MBS.Framework + + {868A6888-EDB9-407E-A710-40F297D4EAB8} + UniversalEditor.Printing + diff --git a/CSharp/UniversalEditor.sln b/CSharp/UniversalEditor.sln index 057a55d7..1e9959c7 100644 --- a/CSharp/UniversalEditor.sln +++ b/CSharp/UniversalEditor.sln @@ -135,6 +135,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniversalEditor.Plugins.Gen EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniversalEditor.TestProject", "Applications\UniversalEditor.TestProject\UniversalEditor.TestProject.csproj", "{242A32D8-9A3A-4FE3-902B-AB9C3154723A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniversalEditor.Printing", "Libraries\UniversalEditor.Printing\UniversalEditor.Printing.csproj", "{868A6888-EDB9-407E-A710-40F297D4EAB8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -391,6 +393,10 @@ Global {242A32D8-9A3A-4FE3-902B-AB9C3154723A}.Debug|Any CPU.Build.0 = Debug|Any CPU {242A32D8-9A3A-4FE3-902B-AB9C3154723A}.Release|Any CPU.ActiveCfg = Release|Any CPU {242A32D8-9A3A-4FE3-902B-AB9C3154723A}.Release|Any CPU.Build.0 = Release|Any CPU + {868A6888-EDB9-407E-A710-40F297D4EAB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {868A6888-EDB9-407E-A710-40F297D4EAB8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {868A6888-EDB9-407E-A710-40F297D4EAB8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {868A6888-EDB9-407E-A710-40F297D4EAB8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {6F0AB1AF-E1A1-4D19-B19C-05BBB15C94B2} = {05D15661-E684-4EC9-8FBD-C014BA433CC5} @@ -454,6 +460,7 @@ Global {74A835FD-93B8-4268-B120-1ACAA128AC7B} = {2ED32D16-6C06-4450-909A-40D32DA67FB4} {E6C8A539-F219-4BD0-A2F4-E7064ABF6777} = {2ED32D16-6C06-4450-909A-40D32DA67FB4} {242A32D8-9A3A-4FE3-902B-AB9C3154723A} = {05D15661-E684-4EC9-8FBD-C014BA433CC5} + {868A6888-EDB9-407E-A710-40F297D4EAB8} = {0399182F-AF56-4E86-B229-EAB38C2EE6AF} EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution Policies = $0