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 : IEditorRender, IDisposable
29 D2DRenderCommon common;
31 System.Drawing.Color ForegroundColor = SystemColors.ControlText,
32 BackgroundColor = SystemColors.Control,
33 HilightColor = System.Drawing.Color.DeepSkyBlue,
34 Keyword1Color = System.Drawing.Color.Blue,
35 Keyword2Color = System.Drawing.Color.DarkCyan,
36 LiteralColor = System.Drawing.Color.Brown,
37 UrlColor = System.Drawing.Color.Blue,
38 ControlCharColor = System.Drawing.Color.Gray,
39 CommentColor = System.Drawing.Color.Green,
40 InsertCaretColor = System.Drawing.Color.Black,
41 OverwriteCaretColor = System.Drawing.Color.Black,
42 LineMarkerColor = System.Drawing.Color.WhiteSmoke,
43 UpdateAreaColor = System.Drawing.Color.MediumSeaGreen;
47 public D2DTextRender(FooTextBox textbox)
49 this.TextBox = textbox;
51 textbox.SizeChanged += new EventHandler(textbox_SizeChanged);
52 textbox.FontChanged += new EventHandler(textbox_FontChanged);
53 textbox.HandleCreated += new EventHandler(textbox_HandleCreated);
54 textbox.HandleDestroyed += new EventHandler(textbox_HandleDestroyed);
56 Size size = textbox.Size;
57 this.common = new D2DRenderCommon();
58 this.common.ConstructRender = ConstructRenderHandler;
59 this.common.ConstrctedResource = ConstructedResourceHandler;
60 this.common.DestructRender = this.DestructRenderHandler;
61 this.common.GetDpi = this.GetDpiHandler;
62 this.common.ConstructDeviceResource(size.Width, size.Height);
63 this.fontName = textbox.Font.Name;
64 this.fontSize = textbox.Font.Size;
65 this.common.InitTextFormat(textbox.Font.Name, (float)textbox.Font.Size);
68 public void GetDpiHandler(out float dpix, out float dpiy)
70 IntPtr hDc = NativeMethods.GetDC(IntPtr.Zero);
71 dpix = NativeMethods.GetDeviceCaps(hDc, NativeMethods.LOGPIXELSX);
72 dpiy = NativeMethods.GetDeviceCaps(hDc, NativeMethods.LOGPIXELSY);
73 NativeMethods.ReleaseDC(IntPtr.Zero, hDc);
76 public event EventHandler ChangedRightToLeft;
78 void textbox_HandleDestroyed(object sender, EventArgs e)
80 this.common.DestructDeviceResource();
83 void textbox_HandleCreated(object sender, EventArgs e)
85 FooTextBox textbox = (FooTextBox)sender;
86 this.TextBox = textbox;
87 this.common.ConstructDeviceResource(textbox.Width, textbox.Height);
90 void textbox_FontChanged(object sender, EventArgs e)
92 FooTextBox textbox = (FooTextBox)sender;
93 Font font = textbox.Font;
94 this.fontName = font.Name;
95 this.fontSize = font.Size;
96 DW.FontWeight weigth = font.Bold ? DW.FontWeight.Bold : DW.FontWeight.Normal;
97 DW.FontStyle style = font.Italic ? DW.FontStyle.Italic : DW.FontStyle.Normal;
98 this.common.InitTextFormat(font.Name, font.Size,weigth,style);
101 void textbox_SizeChanged(object sender, EventArgs e)
103 FooTextBox textbox = (FooTextBox)sender;
104 this.common.ReConstructDeviceResource(this.TextBox.Width, this.TextBox.Height);
107 Color4 ToColor4(System.Drawing.Color color)
109 return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
112 public bool RightToLeft
116 return this.common.RightToLeft;
120 this.common.RightToLeft = value;
121 this.ChangedRightToLeft(this, null);
125 public TextAntialiasMode TextAntialiasMode
129 return this.common.TextAntialiasMode;
133 this.common.TextAntialiasMode = value;
137 public System.Drawing.Color Foreground
141 return this.ForegroundColor;
145 this.ForegroundColor = value;
146 this.common.Foreground = this.ToColor4(value);
150 public System.Drawing.Color UpdateArea
154 return this.UpdateArea;
158 this.UpdateArea = value;
159 this.common.UpdateArea = this.ToColor4(value);
163 public System.Drawing.Color Background
167 return this.BackgroundColor;
171 this.BackgroundColor = value;
172 this.common.Background = this.ToColor4(value);
176 public System.Drawing.Color InsertCaret
180 return this.InsertCaretColor;
184 this.InsertCaretColor = value;
185 this.common.InsertCaret = this.ToColor4(value);
189 public System.Drawing.Color OverwriteCaret
193 return this.OverwriteCaretColor;
197 this.OverwriteCaretColor = value;
198 this.common.OverwriteCaret = this.ToColor4(value);
202 public System.Drawing.Color LineMarker
206 return this.LineMarkerColor;
210 this.LineMarkerColor = value;
211 this.common.LineMarker = this.ToColor4(value);
215 public System.Drawing.Color ControlChar
219 return this.ControlCharColor;
223 this.ControlCharColor = value;
224 this.common.ControlChar = this.ToColor4(value);
228 public System.Drawing.Color Url
232 return this.UrlColor;
236 this.UrlColor = value;
237 this.common.Url = this.ToColor4(value);
241 public System.Drawing.Color Hilight
245 return this.HilightColor;
249 this.HilightColor = value;
250 this.common.Hilight = this.ToColor4(this.HilightColor);
254 public System.Drawing.Color Comment
258 return this.CommentColor;
262 this.CommentColor = value;
263 this.common.Comment = this.ToColor4(value);
267 public System.Drawing.Color Literal
271 return this.LiteralColor;
275 this.LiteralColor = value;
276 this.common.Literal = this.ToColor4(value);
280 public System.Drawing.Color Keyword1
284 return this.Keyword1Color;
288 this.Keyword1Color = value;
289 this.common.Keyword1 = this.ToColor4(value);
293 public System.Drawing.Color Keyword2
297 return this.Keyword2Color;
301 this.Keyword2Color = value;
302 this.common.Keyword2 = this.ToColor4(value);
306 public Rectangle TextArea
308 get { return this.common.ClipRect; }
309 set { this.common.ClipRect = value; }
312 public double LineNemberWidth
316 return this.common.LineNemberWidth;
324 return this.common.emSize;
328 public int TabWidthChar
330 get { return this.common.TabWidthChar; }
335 this.common.TabWidthChar = value;
339 public double FoldingWidth
343 return this.common.FoldingWidth;
347 public bool ShowFullSpace
351 return this.common.ShowFullSpace;
355 this.common.ShowFullSpace = value;
359 public bool ShowHalfSpace
363 return this.common.ShowHalfSpace;
367 this.common.ShowHalfSpace = value;
375 return this.common.ShowTab;
379 this.common.ShowTab = value;
383 public bool ShowLineBreak
387 return this.common.ShowLineBreak;
391 this.common.ShowLineBreak = value;
395 public event ChangedRenderResourceEventHandler ChangedRenderResource
399 this.common.ChangedRenderResource += value;
403 this.common.ChangedRenderResource -= value;
407 public void DrawLine(Point from, Point to)
409 this.common.DrawLine(from, to);
412 public void DrawCachedBitmap(Rectangle rect)
414 this.common.DrawCachedBitmap(rect);
417 public void CacheContent()
419 this.common.CacheContent();
422 public bool IsVaildCache()
424 return this.common.IsVaildCache();
427 public void BeginDraw()
429 this.common.BegineDraw();
432 public void EndDraw()
434 this.common.EndDraw();
437 public void DrawString(string str, double x, double y, StringAlignment align, Size layoutRect)
439 this.common.DrawString(str, x, y,align,layoutRect);
442 public void FillRectangle(Rectangle rect, FillRectType type)
444 this.common.FillRectangle(rect, type);
447 public void DrawFoldingMark(bool expand, double x, double y)
449 this.common.DrawFoldingMark(expand, x, y);
452 public void FillBackground(Rectangle rect)
454 this.common.FillBackground(rect);
457 public void DrawOneLine(LineToIndexTable lti, int row, double x, double y, IEnumerable<Selection> SelectRanges)
459 this.common.DrawOneLine(lti,
467 public ITextLayout CreateLaytout(string str, SyntaxInfo[] syntaxCollection, IEnumerable<Marker> MarkerRanges)
469 return this.common.CreateLaytout(str,syntaxCollection,MarkerRanges);
472 public List<LineToIndexTableData> BreakLine(Document doc, LineToIndexTable layoutLineCollection, int startIndex, int endIndex, double wrapwidth)
474 return this.common.BreakLine(doc,layoutLineCollection, startIndex, endIndex, wrapwidth);
477 public void Dispose()
479 this.common.Dispose();
482 D2D.WindowRenderTarget WindowRender;
484 D2D.RenderTarget ConstructRenderHandler(D2D.Factory factory, D2D.RenderTargetProperties prop, double width, double height)
486 D2D.HwndRenderTargetProperties hwndProp = new D2D.HwndRenderTargetProperties();
487 hwndProp.Hwnd = this.TextBox.Handle;
488 hwndProp.PixelSize = new SharpDX.Size2(this.TextBox.Size.Width,this.TextBox.Size.Height);
489 hwndProp.PresentOptions = D2D.PresentOptions.Immediately;
490 this.WindowRender = new D2D.WindowRenderTarget(factory,prop,hwndProp);
491 return this.WindowRender;
494 void ConstructedResourceHandler()
496 this.common.Foreground = this.ToColor4(this.ForegroundColor);
497 this.common.Background = this.ToColor4(this.BackgroundColor);
498 this.common.ControlChar = this.ToColor4(this.ControlCharColor);
499 this.common.Url = this.ToColor4(this.UrlColor);
500 this.common.Keyword1 = this.ToColor4(this.Keyword1Color);
501 this.common.Keyword2 = this.ToColor4(this.Keyword2Color);
502 this.common.Literal = this.ToColor4(this.LiteralColor);
503 this.common.Comment = this.ToColor4(this.CommentColor);
504 this.common.Hilight = this.ToColor4(this.HilightColor);
505 this.common.LineMarker = this.ToColor4(this.LineMarkerColor);
506 this.common.InsertCaret = this.ToColor4(this.InsertCaretColor);
507 this.common.OverwriteCaret = this.ToColor4(this.OverwriteCaretColor);
508 this.common.UpdateArea = this.ToColor4(this.UpdateAreaColor);
511 void DestructRenderHandler()
513 if (this.WindowRender != null)
514 this.WindowRender.Dispose();
518 internal static class NativeMethods
520 public const int LOGPIXELSX = 88;
521 public const int LOGPIXELSY = 90;
523 [DllImport("gdi32.dll")]
524 public static extern int GetDeviceCaps(IntPtr hDc, int nIndex);
526 [DllImport("user32.dll")]
527 public static extern IntPtr GetDC(IntPtr hWnd);
529 [DllImport("user32.dll")]
530 public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);