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 sealed 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 override void DrawCachedBitmap(Rectangle rect)
73 if (this.WindowRender == null || this.WindowRender.IsDisposed)
75 this.WindowRender.DrawBitmap(this.CachedBitmap, rect, 1.0f, D2D.BitmapInterpolationMode.Linear, rect);
78 public override void CacheContent()
80 if (this.WindowRender == null || this.CachedBitmap == null || this.CachedBitmap.IsDisposed || this.WindowRender.IsDisposed)
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));
87 public static Color4 ToColor4(System.Drawing.Color color)
89 return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
93 public void DrawOneLine(LineToIndexTable lti, int row, double x, double y, IEnumerable<Selection> SelectRanges)
103 D2D.WindowRenderTarget WindowRender;
105 protected override D2D.RenderTarget ConstructRender(D2D.Factory factory, D2D.RenderTargetProperties prop, double width, double height)
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;
115 protected override void ConstrctedResource()
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);
132 protected override void DestructRender()
134 if (this.WindowRender != null)
135 this.WindowRender.Dispose();
139 internal static class NativeMethods
141 public const int LOGPIXELSX = 88;
142 public const int LOGPIXELSY = 90;
144 [DllImport("gdi32.dll")]
145 public static extern int GetDeviceCaps(IntPtr hDc, int nIndex);
147 [DllImport("user32.dll")]
148 public static extern IntPtr GetDC(IntPtr hWnd);
150 [DllImport("user32.dll")]
151 public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);