From 4c1138198d3c61dd3915f8be1da5d61ccfccb11a Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Mon, 2 Sep 2019 00:27:10 -0400 Subject: [PATCH] Preliminary support for PrintHandlers and basic PlainTextPrintHandler (needs a lot of work) --- .../UniversalEditor.Printing/PrintHandler.cs | 8 +- .../PrintHandlerReference.cs | 2 + .../UniversalEditor.Printing/Reflection.cs | 86 +++++++++++++++++++ .../UniversalEditor.Printing.csproj | 9 ++ .../Editors/Text/Plain/PlainTextEditor.cs | 13 ++- .../MainWindow.cs | 46 +--------- .../Properties/AssemblyInfo.cs | 46 ++++++++++ .../Text/Plain/PlainTextPrintHandler.cs | 50 +++++++++++ ...rsalEditor.Plugins.Generic.Printing.csproj | 58 +++++++++++++ CSharp/UniversalEditor.sln | 9 ++ 10 files changed, 278 insertions(+), 49 deletions(-) create mode 100644 CSharp/Libraries/UniversalEditor.Printing/Reflection.cs create mode 100644 CSharp/Plugins.Printing/UniversalEditor.Plugins.Generic.Printing/Properties/AssemblyInfo.cs create mode 100644 CSharp/Plugins.Printing/UniversalEditor.Plugins.Generic.Printing/Text/Plain/PlainTextPrintHandler.cs create mode 100644 CSharp/Plugins.Printing/UniversalEditor.Plugins.Generic.Printing/UniversalEditor.Plugins.Generic.Printing.csproj diff --git a/CSharp/Libraries/UniversalEditor.Printing/PrintHandler.cs b/CSharp/Libraries/UniversalEditor.Printing/PrintHandler.cs index 8186b438..b7b0c1c3 100644 --- a/CSharp/Libraries/UniversalEditor.Printing/PrintHandler.cs +++ b/CSharp/Libraries/UniversalEditor.Printing/PrintHandler.cs @@ -20,6 +20,8 @@ // along with this program. If not, see . using System; +using UniversalWidgetToolkit.Drawing; + namespace UniversalEditor.Printing { public abstract class PrintHandler : References @@ -37,10 +39,10 @@ namespace UniversalEditor.Printing return phr; } - protected abstract void PrintInternal(ObjectModel objectModel); - public void Print(ObjectModel objectModel) + protected abstract void PrintInternal(ObjectModel objectModel, Graphics g); + public void Print(ObjectModel objectModel, Graphics g) { - PrintInternal(objectModel); + PrintInternal(objectModel, g); } } } diff --git a/CSharp/Libraries/UniversalEditor.Printing/PrintHandlerReference.cs b/CSharp/Libraries/UniversalEditor.Printing/PrintHandlerReference.cs index e1f70e69..974b1f79 100644 --- a/CSharp/Libraries/UniversalEditor.Printing/PrintHandlerReference.cs +++ b/CSharp/Libraries/UniversalEditor.Printing/PrintHandlerReference.cs @@ -47,6 +47,8 @@ namespace UniversalEditor.Printing mvarType = dataFormatType; } + public ObjectModelReference.ObjectModelReferenceCollection SupportedObjectModels { get; } = new ObjectModelReference.ObjectModelReferenceCollection(); + private Guid mvarID = Guid.Empty; public Guid ID { get { return mvarID; } set { mvarID = value; } } diff --git a/CSharp/Libraries/UniversalEditor.Printing/Reflection.cs b/CSharp/Libraries/UniversalEditor.Printing/Reflection.cs new file mode 100644 index 00000000..6c498dbb --- /dev/null +++ b/CSharp/Libraries/UniversalEditor.Printing/Reflection.cs @@ -0,0 +1,86 @@ +// +// Reflection.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; +using System.Reflection; + +namespace UniversalEditor.Printing +{ + public static class Reflection + { + private static PrintHandlerReference[] _phrs = null; + public static PrintHandlerReference[] GetAvailablePrintHandlers() + { + if (_phrs == null) + Initialize(); + + return _phrs; + } + + public static PrintHandlerReference[] GetAvailablePrintHandlers(ObjectModel om) + { + List list = new List(); + PrintHandlerReference[] phrs = GetAvailablePrintHandlers(); + foreach (PrintHandlerReference phr in phrs) + { + if (phr.SupportedObjectModels.Contains(om.GetType())) + list.Add(phr); + } + return list.ToArray(); + } + + private static Assembly[] _asms = null; + private static void Initialize() + { + if (_asms == null) + { + List list = new List(); + _asms = UniversalEditor.Common.Reflection.GetAvailableAssemblies(); + foreach (Assembly asm in _asms) + { + Type[] types = null; + try + { + types = asm.GetTypes(); + } + catch (ReflectionTypeLoadException ex) + { + types = ex.Types; + } + + foreach (Type type in types) + { + if (type == null) continue; + + if (type.IsSubclassOf(typeof(PrintHandler))) + { + PrintHandler ph = (type.Assembly.CreateInstance(type.FullName) as PrintHandler); + PrintHandlerReference phr = ph.MakeReference(); + + list.Add(phr); + } + } + } + _phrs = list.ToArray(); + } + } + } +} diff --git a/CSharp/Libraries/UniversalEditor.Printing/UniversalEditor.Printing.csproj b/CSharp/Libraries/UniversalEditor.Printing/UniversalEditor.Printing.csproj index c592d299..5ac3b607 100644 --- a/CSharp/Libraries/UniversalEditor.Printing/UniversalEditor.Printing.csproj +++ b/CSharp/Libraries/UniversalEditor.Printing/UniversalEditor.Printing.csproj @@ -32,12 +32,21 @@ + {2D4737E6-6D95-408A-90DB-8DFF38147E85} UniversalEditor.Core + + {29E1C1BB-3EA5-4062-B62F-85EEC703FE07} + UniversalWidgetToolkit + + + {30467E5C-05BC-4856-AADC-13906EF4CADD} + UniversalEditor.Essential + \ No newline at end of file diff --git a/CSharp/Libraries/UniversalEditor.UserInterface/Editors/Text/Plain/PlainTextEditor.cs b/CSharp/Libraries/UniversalEditor.UserInterface/Editors/Text/Plain/PlainTextEditor.cs index 09d261a7..6fface16 100644 --- a/CSharp/Libraries/UniversalEditor.UserInterface/Editors/Text/Plain/PlainTextEditor.cs +++ b/CSharp/Libraries/UniversalEditor.UserInterface/Editors/Text/Plain/PlainTextEditor.cs @@ -46,7 +46,7 @@ namespace UniversalEditor.Editors.Text.Plain throw new NotImplementedException(); } - private SyntaxTextBox txt = null; + private TextBox txt = null; private static EditorReference _er = null; public override EditorReference MakeReference () @@ -58,10 +58,17 @@ namespace UniversalEditor.Editors.Text.Plain return _er; } + private void txt_Changed(object sender, EventArgs e) + { + PlainTextObjectModel om = (this.ObjectModel as PlainTextObjectModel); + om.Text = txt.Text; + } + public PlainTextEditor () { - txt = new SyntaxTextBox(); - // txt.Multiline = true; + txt = new TextBox(); + txt.Changed += txt_Changed; + txt.Multiline = true; this.Layout = new BoxLayout(Orientation.Vertical); this.Controls.Add(txt, new BoxLayout.Constraints(true, true)); diff --git a/CSharp/Libraries/UniversalEditor.UserInterface/MainWindow.cs b/CSharp/Libraries/UniversalEditor.UserInterface/MainWindow.cs index dede4710..bcfab0af 100644 --- a/CSharp/Libraries/UniversalEditor.UserInterface/MainWindow.cs +++ b/CSharp/Libraries/UniversalEditor.UserInterface/MainWindow.cs @@ -630,29 +630,11 @@ 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) { - PrintHandler ph1 = new TestPrintHandler(); - PrintHandlerReference[] phrs = { ph1.MakeReference() }; // PrintHandler.GetAvailablePrintHandlers(editor.ObjectModel); + PrintHandlerReference[] phrs = UniversalEditor.Printing.Reflection.GetAvailablePrintHandlers(editor.ObjectModel); if (phrs.Length > 0) { PrintDialog dlg = new PrintDialog(); @@ -661,7 +643,7 @@ namespace UniversalEditor.UserInterface PrintHandler ph = phrs[0].Create(); if (ph != null) { - PrintJob job = new PrintJob("Test Page", dlg.SelectedPrinter, dlg.Settings); + PrintJob job = new PrintJob(editor.Title, dlg.SelectedPrinter, dlg.Settings); job.BeginPrint += Job_BeginPrint; job.DrawPage += Job_DrawPage; job.SetExtraData("ph", ph); @@ -680,29 +662,7 @@ namespace UniversalEditor.UserInterface 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); + ph.Print(om, e.Graphics); } diff --git a/CSharp/Plugins.Printing/UniversalEditor.Plugins.Generic.Printing/Properties/AssemblyInfo.cs b/CSharp/Plugins.Printing/UniversalEditor.Plugins.Generic.Printing/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..01789717 --- /dev/null +++ b/CSharp/Plugins.Printing/UniversalEditor.Plugins.Generic.Printing/Properties/AssemblyInfo.cs @@ -0,0 +1,46 @@ +// +// AssemblyInfo.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.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.Plugins.Generic.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/Plugins.Printing/UniversalEditor.Plugins.Generic.Printing/Text/Plain/PlainTextPrintHandler.cs b/CSharp/Plugins.Printing/UniversalEditor.Plugins.Generic.Printing/Text/Plain/PlainTextPrintHandler.cs new file mode 100644 index 00000000..a26e18e7 --- /dev/null +++ b/CSharp/Plugins.Printing/UniversalEditor.Plugins.Generic.Printing/Text/Plain/PlainTextPrintHandler.cs @@ -0,0 +1,50 @@ +// +// PlainTextPrintHandler.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 UniversalEditor.ObjectModels.Text.Plain; +using UniversalEditor.Printing; +using UniversalWidgetToolkit.Drawing; + +namespace UniversalEditor.Plugins.Generic.Printing.Text.Plain +{ + public class PlainTextPrintHandler : PrintHandler + { + private static PrintHandlerReference _phr = null; + protected override PrintHandlerReference MakeReferenceInternal() + { + if (_phr == null) + { + _phr = base.MakeReferenceInternal(); + _phr.SupportedObjectModels.Add(typeof(PlainTextObjectModel)); + } + return _phr; + } + + protected override void PrintInternal(ObjectModel objectModel, Graphics g) + { + PlainTextObjectModel text = (objectModel as PlainTextObjectModel); + if (text == null) + throw new ObjectModelNotSupportedException(); + + g.DrawText(text.Text, Font.FromFamily("Liberation Serif", 12), new Rectangle(64, 64, 1400, 1400), Brushes.Black); + } + } +} diff --git a/CSharp/Plugins.Printing/UniversalEditor.Plugins.Generic.Printing/UniversalEditor.Plugins.Generic.Printing.csproj b/CSharp/Plugins.Printing/UniversalEditor.Plugins.Generic.Printing/UniversalEditor.Plugins.Generic.Printing.csproj new file mode 100644 index 00000000..28cb690f --- /dev/null +++ b/CSharp/Plugins.Printing/UniversalEditor.Plugins.Generic.Printing/UniversalEditor.Plugins.Generic.Printing.csproj @@ -0,0 +1,58 @@ + + + + Debug + AnyCPU + {A3231B32-A0E4-4B67-9A09-310B25BBB9B6} + Library + UniversalEditor.Plugins.Generic.Printing + UniversalEditor.Plugins.Generic.Printing + + + true + full + false + ..\..\Output\Debug\Plugins + DEBUG; + prompt + 4 + false + + + true + ..\..\Output\Release\Plugins + prompt + 4 + false + + + + + + + + + + + + + + + {2D4737E6-6D95-408A-90DB-8DFF38147E85} + UniversalEditor.Core + + + {868A6888-EDB9-407E-A710-40F297D4EAB8} + UniversalEditor.Printing + + + {29E1C1BB-3EA5-4062-B62F-85EEC703FE07} + UniversalWidgetToolkit + + + {30467E5C-05BC-4856-AADC-13906EF4CADD} + UniversalEditor.Essential + + + + \ No newline at end of file diff --git a/CSharp/UniversalEditor.sln b/CSharp/UniversalEditor.sln index 1e9959c7..f0ba2a28 100644 --- a/CSharp/UniversalEditor.sln +++ b/CSharp/UniversalEditor.sln @@ -137,6 +137,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniversalEditor.TestProject EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniversalEditor.Printing", "Libraries\UniversalEditor.Printing\UniversalEditor.Printing.csproj", "{868A6888-EDB9-407E-A710-40F297D4EAB8}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Plugins.Printing", "Plugins.Printing", "{FB678C6D-29A0-405B-BA86-E1EC12A96AA9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniversalEditor.Plugins.Generic.Printing", "Plugins.Printing\UniversalEditor.Plugins.Generic.Printing\UniversalEditor.Plugins.Generic.Printing.csproj", "{A3231B32-A0E4-4B67-9A09-310B25BBB9B6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -397,6 +401,10 @@ Global {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 + {A3231B32-A0E4-4B67-9A09-310B25BBB9B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A3231B32-A0E4-4B67-9A09-310B25BBB9B6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A3231B32-A0E4-4B67-9A09-310B25BBB9B6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A3231B32-A0E4-4B67-9A09-310B25BBB9B6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {6F0AB1AF-E1A1-4D19-B19C-05BBB15C94B2} = {05D15661-E684-4EC9-8FBD-C014BA433CC5} @@ -461,6 +469,7 @@ Global {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} + {A3231B32-A0E4-4B67-9A09-310B25BBB9B6} = {FB678C6D-29A0-405B-BA86-E1EC12A96AA9} EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution Policies = $0