OSDN Git Service

印刷オプションにシンタックスハイライトと行番号に関する項目を追加した
authorgdkhd812 <jbh03215@htmil.co.jp>
Sat, 28 Dec 2013 04:23:08 +0000 (13:23 +0900)
committergdkhd812 <jbh03215@htmil.co.jp>
Sat, 28 Dec 2013 04:23:08 +0000 (13:23 +0900)
Metro/FooEditEngine/FooEditEngine.csproj
Metro/FooEditEngine/FooPrintText.cs
Metro/FooEditEngine/Print/DocumentSource.cs
Metro/FooEditEngine/Print/PrintOptionBuilder.cs [new file with mode: 0644]
Metro/FooEditEngine/strings/en-US/Resources.resw
Metro/FooEditEngine/strings/ja-JP/Resources.resw

index dd3ccdc..8eb694d 100644 (file)
     <Compile Include="Print\D2DPrintRender.cs" />
     <Compile Include="Print\DocumentSource.cs" />
     <Compile Include="Print\D2DPrintPreviewRender.cs" />
+    <Compile Include="Print\PrintOptionBuilder.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>
index d457b7b..703a39f 100644 (file)
@@ -17,6 +17,7 @@ using FooEditEngine;
 using Windows.UI.Core;\r
 using Windows.Foundation;\r
 using Windows.Graphics.Printing;\r
+using Windows.Graphics.Printing.OptionDetails;\r
 \r
 namespace FooEditEngine.Metro\r
 {\r
@@ -82,7 +83,8 @@ namespace FooEditEngine.Metro
                 source.Header = Header;\r
                 source.Fotter = Fotter;\r
 \r
-                PrintTask task = req.CreatePrintTask(title, (e) =>\r
+                PrintTask task = null;\r
+                task = req.CreatePrintTask(title, (e) =>\r
                 {\r
                     e.SetSource(source);\r
                 });\r
@@ -92,6 +94,8 @@ namespace FooEditEngine.Metro
                         this.PrintComplete(this, null);\r
                     });\r
                 };\r
+                PrintOptionBuilder<DocumentSource> builder = new PrintOptionBuilder<DocumentSource>(source);\r
+                builder.BuildPrintOption(PrintTaskOptionDetails.GetFromPrintTaskOptions(task.Options));\r
             });\r
             Task t = WindowsRuntimeSystemExtensions.AsTask(async);\r
             t.Wait();\r
index b0ea785..8d24c7e 100644 (file)
@@ -56,6 +56,7 @@ namespace FooEditEngine
     /// <returns>処理後の文字列</returns>\r
     public delegate string ParseCommandHandler(object sender, ParseCommandEventArgs e);\r
 \r
+\r
     sealed class PrintableViewFactory\r
     {\r
         Windows.UI.Color foreground, comment, keyword1, keyword2, literal, url;\r
@@ -64,7 +65,7 @@ namespace FooEditEngine
         Document document;\r
         LineBreakMethod lineBreakMethod;\r
         int lineBreakCharCount, tabCount;\r
-        bool drawLineNumber, urlMark;\r
+        bool urlMark;\r
         IHilighter hilighter;\r
         float displayDpi;\r
         Padding padding;\r
@@ -87,6 +88,7 @@ namespace FooEditEngine
             this.hilighter = textbox.Hilighter;\r
             this.displayDpi = DisplayInformation.GetForCurrentView().LogicalDpi;\r
             this.padding = padding;\r
+            this.EnableHilight = true;\r
         }\r
         public D2DRenderBase CreateRender(PrintPageDescription pagedesc, IPrintDocumentPackageTarget docPackageTarget)\r
         {\r
@@ -125,14 +127,26 @@ namespace FooEditEngine
             view.Footer = footer;\r
             view.UrlMark = this.urlMark;\r
             view.PageBound = new Rectangle(pagedesc.ImageableRect.X, pagedesc.ImageableRect.Y, pagedesc.ImageableRect.Width, pagedesc.ImageableRect.Height);\r
-            view.Hilighter = this.hilighter;\r
+            view.Hilighter = this.EnableHilight ? this.hilighter : null;\r
             view.PerfomLayouts();\r
 \r
             return view;\r
         }\r
+\r
+        public bool drawLineNumber\r
+        {\r
+            get;\r
+            set;\r
+        }\r
+\r
+        public bool EnableHilight\r
+        {\r
+            get;\r
+            set;\r
+        }\r
     }\r
 \r
-    sealed class DocumentSource : IPrintDocumentPageSource, IPrintPreviewPageCollection, IPrintDocumentSource\r
+    sealed class DocumentSource : IPrintDocumentPageSource, IPrintPreviewPageCollection, IPrintDocumentSource,IPrintPreviewSource\r
     {\r
         IPrintPreviewDxgiPackageTarget dxgiPreviewTarget;\r
         bool paginateCalled = false;\r
@@ -151,7 +165,45 @@ namespace FooEditEngine
             this.factory = new PrintableViewFactory(textbox, padding);\r
         }\r
 \r
-        public void GetPreviewPageCollection(IPrintDocumentPackageTarget docPackageTarget,out IPrintPreviewPageCollection docPageCollection)\r
+        internal enum SyntaxHilightApplibility\r
+        {\r
+            Apply,\r
+            NoApply,\r
+        }\r
+\r
+        [DisplayPrintOptionResourceID("SyntaxHilight")]\r
+        internal SyntaxHilightApplibility EnableHilight\r
+        {\r
+            get\r
+            {\r
+                return this.factory.EnableHilight ? SyntaxHilightApplibility.Apply : SyntaxHilightApplibility.NoApply;\r
+            }\r
+            set\r
+            {\r
+                this.factory.EnableHilight = value == SyntaxHilightApplibility.Apply;\r
+            }\r
+        }\r
+\r
+        internal enum LineNumberVisiblity\r
+        {\r
+            Visible,\r
+            Hidden\r
+        }\r
+\r
+        [DisplayPrintOptionResourceID("ShowLineNumber")]\r
+        internal LineNumberVisiblity ShowLineNumber\r
+        {\r
+            get\r
+            {\r
+                return this.factory.drawLineNumber ? LineNumberVisiblity.Visible : LineNumberVisiblity.Hidden;\r
+            }\r
+            set\r
+            {\r
+                this.factory.drawLineNumber = value == LineNumberVisiblity.Visible;\r
+            }\r
+        }\r
+\r
+        public void GetPreviewPageCollection(IPrintDocumentPackageTarget docPackageTarget, out IPrintPreviewPageCollection docPageCollection)\r
         {\r
             Guid guid = new Guid(PreviewPackageIds.IID_PREVIEWPACKAGETARGET_DXGI);\r
             IntPtr target;\r
@@ -212,6 +264,11 @@ namespace FooEditEngine
             this.paginateCalled = true;\r
         }\r
 \r
+        public void InvalidatePreview()\r
+        {\r
+            this.dxgiPreviewTarget.InvalidatePreview();\r
+        }\r
+\r
         public void MakePage(uint desiredJobPage, float width, float height)\r
         {\r
             if (width <= 0 || height <= 0)\r
diff --git a/Metro/FooEditEngine/Print/PrintOptionBuilder.cs b/Metro/FooEditEngine/Print/PrintOptionBuilder.cs
new file mode 100644 (file)
index 0000000..546a46f
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2013 FooProject
+ * * 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 Windows.Graphics.Printing.OptionDetails;
+using System.Reflection;
+using Windows.ApplicationModel.Resources.Core;
+
+namespace FooEditEngine
+{
+    /// <summary>
+    /// 表示に使用するリソースID
+    /// </summary>
+    class DisplayPrintOptionResourceIDAttribute : Attribute
+    {
+        public string ResourceID
+        {
+            get;
+            private set;
+        }
+        /// <summary>
+        /// コンストラクター
+        /// </summary>
+        /// <param name="resourceID">リソースID</param>
+        public DisplayPrintOptionResourceIDAttribute(string resourceID)
+        {
+            this.ResourceID = resourceID;
+        }
+    }
+    /// <summary>
+    /// IPrintPreviewSourceインターフェイス
+    /// </summary>
+    interface IPrintPreviewSource
+    {
+        /// <summary>
+        /// 再描写する
+        /// </summary>
+        void InvalidatePreview();
+    }
+    /// <summary>
+    /// 印刷プレビューのカスタムオプションリストビルダー
+    /// <para>
+    /// 印刷プレビューに表示させたいプロパティには「DisplayPrintOptionResourceID」属性を付ける必要があります。
+    /// また、このプロパティはenumを継承した型でなければならず、リソースに「型名.値」という形で印刷プレビューに表示される名前を定義する必要があります。
+    /// 定義しない場合、値がそのまま表示されます
+    /// </para>
+    /// </summary>
+    /// <typeparam name="T">IPrintPreviewSourceを継承したクラス</typeparam>
+    class PrintOptionBuilder<T> where T : IPrintPreviewSource
+    {
+        T PrintDocument;
+        /// <summary>
+        /// コンストラクター
+        /// </summary>
+        /// <param name="source">対象となるオブジェクト</param>
+        public PrintOptionBuilder(T source)
+        {
+            this.PrintDocument = source;
+        }
+
+        /// <summary>
+        /// 印刷プレビューのオプションを作成する
+        /// </summary>
+        /// <param name="details">PrintTaskOptionDetailsオブジェクト</param>
+        public void BuildPrintOption(PrintTaskOptionDetails details)
+        {
+            ResourceMap map = ResourceManager.Current.MainResourceMap.GetSubtree("FooEditEngine.Metro/Resources");
+            ResourceContext context = ResourceContext.GetForCurrentView();
+
+            var properties = this.PrintDocument.GetType().GetRuntimeProperties();
+            foreach(PropertyInfo property in properties)
+            {
+                DisplayPrintOptionResourceIDAttribute attr = property.GetCustomAttribute<DisplayPrintOptionResourceIDAttribute>();
+                if (attr == null)
+                    continue;
+                string resourceName;
+                if(property.PropertyType.GetTypeInfo().IsEnum)
+                {
+                    PrintCustomItemListOptionDetails iteminfo = details.CreateItemListOption(property.Name, map.GetValue(attr.ResourceID, context).ValueAsString);
+                    foreach (var enumvalue in Enum.GetValues(property.PropertyType))
+                    {
+                        string enumvalueStr = enumvalue.ToString();
+                        //リソース内部の"."は"/"に変換される
+                        resourceName = property.PropertyType.Name + "/" + enumvalueStr;
+                        ResourceCandidate resValue = map.GetValue(resourceName, context);
+#if DEBUG
+                        if(resValue == null)
+                            System.Diagnostics.Debug.WriteLine("{0} is not defined by resource",resourceName.Replace('/','.'));
+#endif
+                        iteminfo.AddItem(enumvalueStr, resValue != null ? resValue.ValueAsString : enumvalueStr);
+                    }
+                    iteminfo.TrySetValue(property.GetValue(this.PrintDocument).ToString());
+                    details.DisplayedOptions.Add(property.Name);
+                }
+            }
+
+            details.OptionChanged += details_OptionChanged;
+        }
+
+        void details_OptionChanged(PrintTaskOptionDetails sender, PrintTaskOptionChangedEventArgs args)
+        {
+            string optionID = args.OptionId as string;
+            if (optionID == null)
+                return;
+            string optionValue = sender.Options[optionID].Value as string;
+            var property = this.GetRuntimeProperty(this.PrintDocument.GetType(),optionID);
+            if (property == null)
+                return;
+            if (property.PropertyType.GetTypeInfo().IsEnum)
+            {
+                Enum output = (Enum)Enum.Parse(property.PropertyType, optionValue);
+                property.SetMethod.Invoke(this.PrintDocument, new object[] { output });
+            }
+
+            this.PrintDocument.InvalidatePreview();
+        }
+
+        PropertyInfo GetRuntimeProperty(Type t,string name)
+        {
+            foreach(PropertyInfo property in t.GetRuntimeProperties())
+            {
+                if (property.Name == name)
+                    return property;
+            }
+            return null;
+        }
+    }
+}
index 02d3528..28533c4 100644 (file)
   <data name="CutMenuName" xml:space="preserve">
     <value>Cut</value>
   </data>
+  <data name="LineNumberVisiblity.Hidden" xml:space="preserve">
+    <value>Hidden</value>
+  </data>
+  <data name="LineNumberVisiblity.Visible" xml:space="preserve">
+    <value>Visible</value>
+  </data>
   <data name="LineSelectMenuName" xml:space="preserve">
     <value>LineSelect</value>
   </data>
   <data name="RectSelectMenuName" xml:space="preserve">
     <value>RectangleSelect</value>
   </data>
+  <data name="ShowLineNumber" xml:space="preserve">
+    <value>LineNumber</value>
+  </data>
+  <data name="SyntaxHilight" xml:space="preserve">
+    <value>SyntaxHilight</value>
+  </data>
+  <data name="SyntaxHilightApplibility.Apply" xml:space="preserve">
+    <value>Apply</value>
+  </data>
+  <data name="SyntaxHilightApplibility.NoApply" xml:space="preserve">
+    <value>Not Apply</value>
+  </data>
 </root>
\ No newline at end of file
index 0df1d99..e0b8dc9 100644 (file)
   <data name="CutMenuName" xml:space="preserve">
     <value>切り取り</value>
   </data>
+  <data name="LineNumberVisiblity.Hidden" xml:space="preserve">
+    <value>表示しない</value>
+  </data>
+  <data name="LineNumberVisiblity.Visible" xml:space="preserve">
+    <value>表示する</value>
+  </data>
   <data name="LineSelectMenuName" xml:space="preserve">
     <value>行選択</value>
   </data>
   <data name="RectSelectMenuName" xml:space="preserve">
     <value>矩形選択</value>
   </data>
+  <data name="ShowLineNumber" xml:space="preserve">
+    <value>行番号</value>
+  </data>
+  <data name="SyntaxHilight" xml:space="preserve">
+    <value>シンタックスハイライト</value>
+  </data>
+  <data name="SyntaxHilightApplibility.Apply" xml:space="preserve">
+    <value>反映させる</value>
+  </data>
+  <data name="SyntaxHilightApplibility.NoApply" xml:space="preserve">
+    <value>反映させない</value>
+  </data>
 </root>
\ No newline at end of file