OSDN Git Service

メトロ版でもカーソルを点滅できるようにしてみた&コードの整理
[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 override void DrawCachedBitmap(Rectangle rect)
72         {
73             if (this.WindowRender == null || this.WindowRender.IsDisposed)
74                 return;
75             this.WindowRender.DrawBitmap(this.CachedBitmap, rect, 1.0f, D2D.BitmapInterpolationMode.Linear, rect);
76         }
77
78         public override void CacheContent()
79         {
80             if (this.WindowRender == null || this.CachedBitmap == null || this.CachedBitmap.IsDisposed || this.WindowRender.IsDisposed)
81                 return;
82             this.WindowRender.Flush();
83             this.CachedBitmap.CopyFromRenderTarget(this.WindowRender, new SharpDX.Point(), new SharpDX.Rectangle(0, 0, (int)this.renderSize.Width, (int)this.renderSize.Height));
84             this.hasCache = true;
85         }
86
87         public static Color4 ToColor4(System.Drawing.Color color)
88         {
89             return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
90         }
91
92
93         public void DrawOneLine(LineToIndexTable lti, int row, double x, double y, IEnumerable<Selection> SelectRanges)
94         {
95             this.DrawOneLine(lti,
96                 row,
97                 x,
98                 y,
99                 SelectRanges,
100                 null);
101         }
102
103         D2D.WindowRenderTarget WindowRender;
104
105         protected override D2D.RenderTarget ConstructRender(D2D.Factory factory, D2D.RenderTargetProperties prop, double width, double height)
106         {
107             D2D.HwndRenderTargetProperties hwndProp = new D2D.HwndRenderTargetProperties();
108             hwndProp.Hwnd = this.TextBox.Handle;
109             hwndProp.PixelSize = new SharpDX.Size2(this.TextBox.Size.Width,this.TextBox.Size.Height);
110             hwndProp.PresentOptions = D2D.PresentOptions.Immediately;
111             this.WindowRender = new D2D.WindowRenderTarget(factory,prop,hwndProp);
112             return this.WindowRender;
113         }
114
115         protected override void ConstrctedResource()
116         {
117             this.Foreground = ToColor4(this.TextBox.Foreground);
118             this.Background = ToColor4(this.TextBox.Background);
119             this.ControlChar = ToColor4(this.TextBox.ControlChar);
120             this.Url = ToColor4(this.TextBox.Url);
121             this.Keyword1 = ToColor4(this.TextBox.Keyword1);
122             this.Keyword2 = ToColor4(this.TextBox.Keyword2);
123             this.Literal = ToColor4(this.TextBox.Literal);
124             this.Comment = ToColor4(this.TextBox.Comment);
125             this.Hilight = ToColor4(this.TextBox.Hilight);
126             this.LineMarker = ToColor4(this.TextBox.LineMarker);
127             this.InsertCaret = ToColor4(this.TextBox.InsertCaret);
128             this.OverwriteCaret = ToColor4(this.TextBox.OverwriteCaret);
129             this.UpdateArea = ToColor4(this.TextBox.UpdateArea);
130         }
131
132         protected override void DestructRender()
133         {
134             if (this.WindowRender != null)
135                 this.WindowRender.Dispose();
136         }
137     }
138
139     internal static class NativeMethods
140     {
141         public const int LOGPIXELSX = 88;
142         public const int LOGPIXELSY = 90;
143
144         [DllImport("gdi32.dll")]
145         public static extern int GetDeviceCaps(IntPtr hDc, int nIndex);
146
147         [DllImport("user32.dll")]
148         public static extern IntPtr GetDC(IntPtr hWnd);
149
150         [DllImport("user32.dll")]
151         public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);
152     }
153 }