Preliminary support for Printing with PrintHandlers

This commit is contained in:
Michael Becker 2019-08-31 02:09:04 -04:00
parent b7bc270a4e
commit b599f8f44e
7 changed files with 352 additions and 2 deletions

View File

@ -0,0 +1,46 @@
//
// PrintHandler.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// 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 <http://www.gnu.org/licenses/>.
using System;
namespace UniversalEditor.Printing
{
public abstract class PrintHandler : References<PrintHandlerReference>
{
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);
}
}
}

View File

@ -0,0 +1,147 @@
//
// PrintHandlerReference.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// 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 <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
namespace UniversalEditor.Printing
{
public class PrintHandlerReference : ReferencedBy<PrintHandler>
{
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<Guid, PrintHandlerReference> _referencesByGUID = new Dictionary<Guid, PrintHandlerReference>();
private static Dictionary<string, PrintHandlerReference> _referencesByTypeName = new Dictionary<string, PrintHandlerReference>();
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);
}
}
}

View File

@ -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("")]

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{868A6888-EDB9-407E-A710-40F297D4EAB8}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>UniversalEditor.Printing</RootNamespace>
<AssemblyName>UniversalEditor.Printing</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="PrintHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PrintHandlerReference.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\UniversalEditor.Core\UniversalEditor.Core.csproj">
<Project>{2D4737E6-6D95-408A-90DB-8DFF38147E85}</Project>
<Name>UniversalEditor.Core</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -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<PrintHandler>("ph", ph);
job.SetExtraData<ObjectModel>("om", editor.ObjectModel);
job.Send();
}
}
}
}
}
void Job_DrawPage(object sender, PrintEventArgs e)
{
PrintJob job = (sender as PrintJob);
PrintHandler ph = job.GetExtraData<PrintHandler>("ph");
ObjectModel om = job.GetExtraData<ObjectModel>("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<PrintHandler>("ph");
ObjectModel om = job.GetExtraData<ObjectModel>("om");
}
public Editor GetCurrentEditor()
{
Pages.EditorPage page = GetCurrentEditorPage ();

View File

@ -149,6 +149,10 @@
<Project>{23ACD09E-E096-4B05-A6CE-6853EDDC589A}</Project>
<Name>MBS.Framework</Name>
</ProjectReference>
<ProjectReference Include="..\UniversalEditor.Printing\UniversalEditor.Printing.csproj">
<Project>{868A6888-EDB9-407E-A710-40F297D4EAB8}</Project>
<Name>UniversalEditor.Printing</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Panels\" />

View File

@ -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