OSDN Git Service

一部クラスにsealedを付けた
[fooeditengine/FooEditEngine.git] / Windows / FooEditEngine / D2DTextRender.cs
1 /*
2  * Copyright (C) 2013 FooProject
3  * * 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
4  * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
5
6  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
7  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8
9 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
10  */
11 using System;
12 using System.Collections.Generic;
13 using System.Linq;
14 using System.Text;
15 using System.Windows;
16 using System.Drawing;
17 using FooEditEngine;
18 using SharpDX;
19 using D2D = SharpDX.Direct2D1;
20 using DW = SharpDX.DirectWrite;
21 using DXGI = SharpDX.DXGI;
22 using System.Runtime.InteropServices;
23
24 namespace FooEditEngine.Windows
25 {
26
27     sealed class D2DTextRender : D2DRenderCommon, IEditorRender, IDisposable
28     {
29         FooTextBox TextBox;
30         string fontName;
31         float fontSize;
32
33         public D2DTextRender(FooTextBox textbox)
34         {
35             this.TextBox = textbox;
36
37             textbox.SizeChanged += new EventHandler(textbox_SizeChanged);
38             textbox.FontChanged += new EventHandler(textbox_FontChanged);
39
40             Size size = textbox.Size;
41             this.fontName = textbox.Font.Name;
42             this.fontSize = textbox.Font.Size;
43             this.InitTextFormat(textbox.Font.Name, (float)textbox.Font.Size);
44         }
45
46         public override void GetDpi(out float dpix, out float dpiy)
47         {
48             IntPtr hDc = NativeMethods.GetDC(IntPtr.Zero);
49             dpix = NativeMethods.GetDeviceCaps(hDc, NativeMethods.LOGPIXELSX);
50             dpiy = NativeMethods.GetDeviceCaps(hDc, NativeMethods.LOGPIXELSY);
51             NativeMethods.ReleaseDC(IntPtr.Zero, hDc);
52         }
53
54         void textbox_FontChanged(object sender, EventArgs e)
55         {
56             FooTextBox textbox = (FooTextBox)sender;
57             Font font = textbox.Font;
58             this.fontName = font.Name;
59             this.fontSize = font.Size;
60             DW.FontWeight weigth = font.Bold ? DW.FontWeight.Bold : DW.FontWeight.Normal;
61             DW.FontStyle style = font.Italic ? DW.FontStyle.Italic : DW.FontStyle.Normal;
62             this.InitTextFormat(font.Name, font.Size,weigth,style);
63         }
64
65         void textbox_SizeChanged(object sender, EventArgs e)
66         {
67             FooTextBox textbox = (FooTextBox)sender;
68             this.ReConstructDeviceResource(this.TextBox.Width, this.TextBox.Height);
69         }
70
71         public static Color4 ToColor4(System.Drawing.Color color)
72         {
73             return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
74         }
75
76
77         public void DrawOneLine(LineToIndexTable lti, int row, double x, double y, IEnumerable<Selection> SelectRanges)
78         {
79             this.DrawOneLine(lti,
80                 row,
81                 x,
82                 y,
83                 SelectRanges,
84                 null);
85         }
86
87         D2D.WindowRenderTarget WindowRender;
88
89         protected override D2D.RenderTarget ConstructRender(D2D.Factory factory, D2D.RenderTargetProperties prop, double width, double height)
90         {
91             D2D.HwndRenderTargetProperties hwndProp = new D2D.HwndRenderTargetProperties();
92             hwndProp.Hwnd = this.TextBox.Handle;
93             hwndProp.PixelSize = new SharpDX.Size2(this.TextBox.Size.Width,this.TextBox.Size.Height);
94             hwndProp.PresentOptions = D2D.PresentOptions.Immediately;
95             this.WindowRender = new D2D.WindowRenderTarget(factory,prop,hwndProp);
96             return this.WindowRender;
97         }
98
99         protected override void ConstrctedResource()
100         {
101             this.Foreground = ToColor4(this.TextBox.Foreground);
102             this.Background = ToColor4(this.TextBox.Background);
103             this.ControlChar = ToColor4(this.TextBox.ControlChar);
104             this.Url = ToColor4(this.TextBox.Url);
105             this.Keyword1 = ToColor4(this.TextBox.Keyword1);
106             this.Keyword2 = ToColor4(this.TextBox.Keyword2);
107             this.Literal = ToColor4(this.TextBox.Literal);
108             this.Comment = ToColor4(this.TextBox.Comment);
109             this.Hilight = ToColor4(this.TextBox.Hilight);
110             this.LineMarker = ToColor4(this.TextBox.LineMarker);
111             this.InsertCaret = ToColor4(this.TextBox.InsertCaret);
112             this.OverwriteCaret = ToColor4(this.TextBox.OverwriteCaret);
113             this.UpdateArea = ToColor4(this.TextBox.UpdateArea);
114         }
115
116         protected override void DestructRender()
117         {
118             if (this.WindowRender != null)
119                 this.WindowRender.Dispose();
120         }
121     }
122
123     internal static class NativeMethods
124     {
125         public const int LOGPIXELSX = 88;
126         public const int LOGPIXELSY = 90;
127
128         [DllImport("gdi32.dll")]
129         public static extern int GetDeviceCaps(IntPtr hDc, int nIndex);
130
131         [DllImport("user32.dll")]
132         public static extern IntPtr GetDC(IntPtr hWnd);
133
134         [DllImport("user32.dll")]
135         public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);
136     }
137 }