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;
46 public D2DTextRender(FooTextBox textbox)
48 this.TextBox = textbox;
50 textbox.SizeChanged += new EventHandler(textbox_SizeChanged);
51 textbox.FontChanged += new EventHandler(textbox_FontChanged);
52 textbox.HandleCreated += new EventHandler(textbox_HandleCreated);
53 textbox.HandleDestroyed += new EventHandler(textbox_HandleDestroyed);
55 Size size = textbox.Size;
56 this.common = new D2DRenderCommon();
57 this.common.ConstructRender = ConstructRenderHandler;
58 this.common.ConstrctedResource = ConstructedResourceHandler;
59 this.common.DestructRender = this.DestructRenderHandler;
60 this.common.GetDpi = this.GetDpiHandler;
61 this.common.ConstructDeviceResource(size.Width, size.Height);
62 this.fontName = textbox.Font.Name;
63 this.fontSize = textbox.Font.Size;
64 this.common.InitTextFormat(textbox.Font.Name, (float)textbox.Font.Size);
67 private void GetDpiHandler(out int dpix, out int dpiy)
69 IntPtr hDc = NativeMethods.GetDC(IntPtr.Zero);
70 dpix = NativeMethods.GetDeviceCaps(hDc, NativeMethods.LOGPIXELSX);
71 dpiy = NativeMethods.GetDeviceCaps(hDc, NativeMethods.LOGPIXELSY);
72 NativeMethods.ReleaseDC(IntPtr.Zero, hDc);
75 public event EventHandler ChangedRightToLeft;
77 void textbox_HandleDestroyed(object sender, EventArgs e)
79 this.common.DestructDeviceResource();
82 void textbox_HandleCreated(object sender, EventArgs e)
84 FooTextBox textbox = (FooTextBox)sender;
85 this.TextBox = textbox;
86 this.common.ConstructDeviceResource(textbox.Width, textbox.Height);
89 void textbox_FontChanged(object sender, EventArgs e)
91 FooTextBox textbox = (FooTextBox)sender;
92 Font font = textbox.Font;
93 this.fontName = font.Name;
94 this.fontSize = font.Size;
95 DW.FontWeight weigth = font.Bold ? DW.FontWeight.Bold : DW.FontWeight.Normal;
96 DW.FontStyle style = font.Italic ? DW.FontStyle.Italic : DW.FontStyle.Normal;
97 this.common.InitTextFormat(font.Name, font.Size,weigth,style);
100 void textbox_SizeChanged(object sender, EventArgs e)
102 FooTextBox textbox = (FooTextBox)sender;
103 this.common.ReConstructDeviceResource(this.TextBox.Width, this.TextBox.Height);
106 Color4 ToColor4(System.Drawing.Color color)
108 return new Color4(color.R / 255.0f, color.G / 255.0f, color.B / 255.0f, color.A / 255.0f);
111 public bool RightToLeft
115 return this.common.RightToLeft;
119 this.common.RightToLeft = value;
120 this.ChangedRightToLeft(this, null);
124 public TextAntialiasMode TextAntialiasMode
128 return this.common.TextAntialiasMode;
132 this.common.TextAntialiasMode = value;
136 public System.Drawing.Color Foreground
140 return this.ForegroundColor;
144 this.ForegroundColor = value;
145 this.common.Foreground = this.ToColor4(value);
149 public System.Drawing.Color Background
153 return this.BackgroundColor;
157 this.BackgroundColor = value;
158 this.common.Background = this.ToColor4(value);
162 public System.Drawing.Color InsertCaret
166 return this.InsertCaretColor;
170 this.InsertCaretColor = value;
171 this.common.InsertCaret = this.ToColor4(value);
175 public System.Drawing.Color OverwriteCaret
179 return this.OverwriteCaretColor;
183 this.OverwriteCaretColor = value;
184 this.common.OverwriteCaret = this.ToColor4(value);
188 public System.Drawing.Color LineMarker
192 return this.LineMarkerColor;
196 this.LineMarkerColor = value;
197 this.common.LineMarker = this.ToColor4(value);
201 public System.Drawing.Color ControlChar
205 return this.ControlCharColor;
209 this.ControlCharColor = value;
210 this.common.ControlChar = this.ToColor4(value);
214 public System.Drawing.Color Url
218 return this.UrlColor;
222 this.UrlColor = value;
223 this.common.Url = this.ToColor4(value);
227 public System.Drawing.Color Hilight
231 return this.HilightColor;
235 this.HilightColor = value;
236 this.common.Hilight = this.ToColor4(this.HilightColor);
240 public System.Drawing.Color Comment
244 return this.CommentColor;
248 this.CommentColor = value;
249 this.common.Comment = this.ToColor4(value);
253 public System.Drawing.Color Literal
257 return this.LiteralColor;
261 this.LiteralColor = value;
262 this.common.Literal = this.ToColor4(value);
266 public System.Drawing.Color Keyword1
270 return this.Keyword1Color;
274 this.Keyword1Color = value;
275 this.common.Keyword1 = this.ToColor4(value);
279 public System.Drawing.Color Keyword2
283 return this.Keyword2Color;
287 this.Keyword2Color = value;
288 this.common.Keyword2 = this.ToColor4(value);
292 public Rectangle TextArea
294 get { return this.common.ClipRect; }
295 set { this.common.ClipRect = value; }
298 public double LineNemberWidth
302 return this.common.LineNemberWidth;
310 return this.common.emSize;
314 public int TabWidthChar
316 get { return this.common.TabWidthChar; }
321 this.common.TabWidthChar = value;
325 public double FoldingWidth
329 return this.common.FoldingWidth;
333 public bool ShowFullSpace
337 return this.common.ShowFullSpace;
341 this.common.ShowFullSpace = value;
345 public bool ShowHalfSpace
349 return this.common.ShowHalfSpace;
353 this.common.ShowHalfSpace = value;
361 return this.common.ShowTab;
365 this.common.ShowTab = value;
369 public event ChangedRenderResourceEventHandler ChangedRenderResource
373 this.common.ChangedRenderResource += value;
377 this.common.ChangedRenderResource -= value;
381 public void DrawLine(Point from, Point to)
383 this.common.DrawLine(from, to);
386 public void DrawCachedBitmap(Rectangle rect)
388 this.common.DrawCachedBitmap(rect);
391 public void CacheContent()
393 this.common.CacheContent();
396 public bool IsVaildCache()
398 return this.common.IsVaildCache();
401 public void BeginDraw()
403 this.common.BegineDraw();
406 public void EndDraw()
408 this.common.EndDraw();
411 public void DrawString(string str, double x, double y, StringAlignment align, Size layoutRect)
413 this.common.DrawString(str, x, y,align,layoutRect);
416 public void FillRectangle(Rectangle rect, FillRectType type)
418 this.common.FillRectangle(rect, type);
421 public void DrawFoldingMark(bool expand, double x, double y)
423 this.common.DrawFoldingMark(expand, x, y);
426 public void FillBackground(Rectangle rect)
428 this.common.FillBackground(rect);
431 public void DrawOneLine(LineToIndexTable lti, int row, double x, double y, IEnumerable<Selection> SelectRanges)
433 this.common.DrawOneLine(lti,
441 public ITextLayout CreateLaytout(string str, SyntaxInfo[] syntaxCollection, IEnumerable<Marker> MarkerRanges)
443 return this.common.CreateLaytout(str,syntaxCollection,MarkerRanges);
446 public List<LineToIndexTableData> BreakLine(Document doc, int startIndex, int endIndex, double wrapwidth)
448 return this.BreakLine(doc, startIndex, endIndex, wrapwidth);
451 public void Dispose()
453 this.common.Dispose();
456 D2D.WindowRenderTarget WindowRender;
458 D2D.RenderTarget ConstructRenderHandler(D2D.Factory factory, D2D.RenderTargetProperties prop, double width, double height)
460 D2D.HwndRenderTargetProperties hwndProp = new D2D.HwndRenderTargetProperties();
461 hwndProp.Hwnd = this.TextBox.Handle;
462 hwndProp.PixelSize = new SharpDX.Size2(this.TextBox.Size.Width,this.TextBox.Size.Height);
463 hwndProp.PresentOptions = D2D.PresentOptions.Immediately;
464 this.WindowRender = new D2D.WindowRenderTarget(factory,prop,hwndProp);
465 return this.WindowRender;
468 void ConstructedResourceHandler()
470 this.common.Foreground = this.ToColor4(this.ForegroundColor);
471 this.common.Background = this.ToColor4(this.BackgroundColor);
472 this.common.ControlChar = this.ToColor4(this.ControlCharColor);
473 this.common.Url = this.ToColor4(this.UrlColor);
474 this.common.Keyword1 = this.ToColor4(this.Keyword1Color);
475 this.common.Keyword2 = this.ToColor4(this.Keyword2Color);
476 this.common.Literal = this.ToColor4(this.LiteralColor);
477 this.common.Comment = this.ToColor4(this.CommentColor);
478 this.common.Hilight = this.ToColor4(this.HilightColor);
479 this.common.LineMarker = this.ToColor4(this.LineMarkerColor);
480 this.common.InsertCaret = this.ToColor4(this.InsertCaretColor);
481 this.common.OverwriteCaret = this.ToColor4(this.OverwriteCaretColor);
484 void DestructRenderHandler()
486 if (this.WindowRender != null)
487 this.WindowRender.Dispose();
491 internal static class NativeMethods
493 public const int LOGPIXELSX = 88;
494 public const int LOGPIXELSY = 90;
496 [DllImport("gdi32.dll")]
497 public static extern int GetDeviceCaps(IntPtr hDc, int nIndex);
499 [DllImport("user32.dll")]
500 public static extern IntPtr GetDC(IntPtr hWnd);
502 [DllImport("user32.dll")]
503 public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);