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.
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.
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/>.
12 using System.Collections.Generic;
19 using D2D = SharpDX.Direct2D1;
20 using DW = SharpDX.DirectWrite;
21 using DXGI = SharpDX.DXGI;
22 using System.Runtime.InteropServices;
24 namespace FooEditEngine.Windows
27 class D2DTextRender : D2DRenderCommon,IEditorRender, IDisposable
33 public D2DTextRender(FooTextBox textbox)
35 this.TextBox = textbox;
37 textbox.SizeChanged += new EventHandler(textbox_SizeChanged);
38 textbox.FontChanged += new EventHandler(textbox_FontChanged);
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);
46 public override void GetDpi(out float dpix, out float dpiy)
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);
54 void textbox_FontChanged(object sender, EventArgs e)
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);
65 void textbox_SizeChanged(object sender, EventArgs e)
67 FooTextBox textbox = (FooTextBox)sender;
68 this.ReConstructDeviceResource(this.TextBox.Width, this.TextBox.Height);
71 public static Color4 ToColor4(System.Drawing.Color color)
73 return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
77 public void DrawOneLine(LineToIndexTable lti, int row, double x, double y, IEnumerable<Selection> SelectRanges)
87 D2D.WindowRenderTarget WindowRender;
89 protected override D2D.RenderTarget ConstructRender(D2D.Factory factory, D2D.RenderTargetProperties prop, double width, double height)
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;
99 protected override void ConstrctedResource()
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);
116 protected override void DestructRender()
118 if (this.WindowRender != null)
119 this.WindowRender.Dispose();
123 internal static class NativeMethods
125 public const int LOGPIXELSX = 88;
126 public const int LOGPIXELSY = 90;
128 [DllImport("gdi32.dll")]
129 public static extern int GetDeviceCaps(IntPtr hDc, int nIndex);
131 [DllImport("user32.dll")]
132 public static extern IntPtr GetDC(IntPtr hWnd);
134 [DllImport("user32.dll")]
135 public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);