protected override LayoutWrapper RenderLine(long line) { Pango.Layout layout = new Pango.Layout(Editor.PangoContext); layout.FontDescription = Editor.Options.Font; StringBuilder sb = new StringBuilder(); long startOffset = line * Editor.BytesInRow; long endOffset = System.Math.Min(startOffset + Editor.BytesInRow, Data.Length); byte[] lineBytes = Data.GetBytes(startOffset, (int)(endOffset - startOffset)); for (int i = 0; i < lineBytes.Length; i++) { byte b = lineBytes[i]; char ch = (char)b; if (b < 128 && (Char.IsLetterOrDigit(ch) || Char.IsPunctuation(ch))) { sb.Append(ch); } else { sb.Append("."); } } layout.SetText(sb.ToString()); char[] lineChars = layout.Text.ToCharArray(); Margin.LayoutWrapper result = new LayoutWrapper(layout); uint curIndex = 0, byteIndex = 0; if (Data.IsSomethingSelected) { ISegment selection = Data.MainSelection.Segment; HandleSelection(selection.Offset, selection.EndOffset, startOffset, endOffset, null, delegate(long start, long end) { Pango.AttrForeground selectedForeground = new Pango.AttrForeground(Style.Selection.Red, Style.Selection.Green, Style.Selection.Blue); selectedForeground.StartIndex = TranslateToUTF8Index(lineChars, (uint)(start - startOffset), ref curIndex, ref byteIndex); selectedForeground.EndIndex = TranslateToUTF8Index(lineChars, (uint)(end - startOffset), ref curIndex, ref byteIndex); result.Add(selectedForeground); Pango.AttrBackground attrBackground = new Pango.AttrBackground(Style.SelectionBg.Red, Style.SelectionBg.Green, Style.SelectionBg.Blue); attrBackground.StartIndex = selectedForeground.StartIndex; attrBackground.EndIndex = selectedForeground.EndIndex; result.Add(attrBackground); }); } result.SetAttributes(); return(result); }
public void SetForegroundBrush(IBrush brush, int startIndex, int count) { var scb = brush as SolidColorBrush; if (scb != null) { var color = new Pango.Color(); color.Parse(string.Format("#{0}", scb.Color.ToString().Substring(3))); var brushAttr = new Pango.AttrForeground(color); brushAttr.StartIndex = (uint)TextIndexToPangoIndex(startIndex); brushAttr.EndIndex = (uint)TextIndexToPangoIndex(startIndex + count); Layout.Attributes.Insert(brushAttr); } }
protected override LayoutWrapper RenderLine(long line) { Pango.Layout layout = new Pango.Layout(Editor.PangoContext); layout.FontDescription = Editor.Options.Font; layout.Tabs = tabArray; StringBuilder sb = new StringBuilder(); long startOffset = line * Editor.BytesInRow; long endOffset = System.Math.Min(startOffset + Editor.BytesInRow, Data.Length); byte[] lineBytes = Data.GetBytes(startOffset, (int)(endOffset - startOffset)); for (int i = 0; i < lineBytes.Length; i++) { sb.Append(string.Format("{0:X2}", lineBytes[i])); if (i % Editor.Options.GroupBytes == 0) { sb.Append("\t"); } } layout.SetText(sb.ToString()); char[] lineChars = sb.ToString().ToCharArray(); Margin.LayoutWrapper result = new LayoutWrapper(layout); uint curIndex = 0, byteIndex = 0; if (Data.IsSomethingSelected) { ISegment selection = Data.MainSelection.Segment; HandleSelection(selection.Offset, selection.EndOffset, startOffset, endOffset, null, delegate(long start, long end) { Pango.AttrForeground selectedForeground = new Pango.AttrForeground(Style.Selection.Red, Style.Selection.Green, Style.Selection.Blue); selectedForeground.StartIndex = TranslateToUTF8Index(lineChars, TranslateColumn(start - startOffset), ref curIndex, ref byteIndex); selectedForeground.EndIndex = TranslateToUTF8Index(lineChars, TranslateColumn(end - startOffset) - 1, ref curIndex, ref byteIndex); result.Add(selectedForeground); Pango.AttrBackground attrBackground = new Pango.AttrBackground(Style.SelectionBg.Red, Style.SelectionBg.Green, Style.SelectionBg.Blue); attrBackground.StartIndex = selectedForeground.StartIndex; attrBackground.EndIndex = selectedForeground.EndIndex; result.Add(attrBackground); }); } result.SetAttributes(); return(result); }
public FormattedTextImpl( Pango.Context context, string text, Typeface typeface, TextAlignment textAlignment, TextWrapping wrapping, Size constraint, IReadOnlyList <FormattedTextStyleSpan> spans) { Contract.Requires <ArgumentNullException>(context != null); Contract.Requires <ArgumentNullException>(text != null); Layout = new Pango.Layout(context); Layout.SetText(text); Layout.FontDescription = new Pango.FontDescription { Family = typeface?.FontFamilyName ?? "monospace", Size = Pango.Units.FromDouble(CorrectScale(typeface?.FontSize ?? 12)), Style = (Pango.Style)(typeface?.Style ?? FontStyle.Normal), Weight = (typeface?.Weight ?? FontWeight.Normal).ToCairo(), }; Layout.Alignment = textAlignment.ToCairo(); Layout.Attributes = new Pango.AttrList(); Layout.Width = double.IsPositiveInfinity(constraint.Width) ? -1 : Pango.Units.FromDouble(constraint.Width); if (spans != null) { foreach (var span in spans) { if (span.ForegroundBrush is SolidColorBrush scb) { var color = new Pango.Color(); color.Parse(string.Format("#{0}", scb.Color.ToString().Substring(3))); var brushAttr = new Pango.AttrForeground(color); brushAttr.StartIndex = (uint)TextIndexToPangoIndex(span.StartIndex); brushAttr.EndIndex = (uint)TextIndexToPangoIndex(span.StartIndex + span.Length); this.Layout.Attributes.Insert(brushAttr); } } } Size = Measure(); }
// TODO: GTK+ 3.2 adds a placeholder-text property, but for now // we have to handle it ourselves protected override bool OnDrawn(Cairo.Context cr) { if (Text.Length > 0 || HasFocus || EmptyMessage == null) { return(base.OnDrawn(cr)); } Layout.SetMarkup(EmptyMessage); Gdk.RGBA color; if (!StyleContext.LookupColor("placeholder_text_color", out color)) { color = StyleContext.GetColor(StateFlags.Insensitive); } Pango.Attribute attr = new Pango.AttrForeground(Convert.ToUInt16(color.Red * 65535), Convert.ToUInt16(color.Green * 65535), Convert.ToUInt16(color.Blue * 65535)); Layout.Attributes.Insert(attr); return(base.OnDrawn(cr)); }
public double ColumnToX (DocumentLine line, int column) { column--; // calculate virtual indentation if (column > 0 && line.Length == 0 && textEditor.GetTextEditorData ().HasIndentationTracker) { using (var l = PangoUtil.CreateLayout (textEditor, textEditor.GetTextEditorData ().IndentationTracker.GetIndentationString (line.Offset))) { l.Alignment = Pango.Alignment.Left; l.FontDescription = textEditor.Options.Font; l.Tabs = tabArray; Pango.Rectangle ink_rect, logical_rect; l.GetExtents (out ink_rect, out logical_rect); return (logical_rect.Width + Pango.Scale.PangoScale - 1) / Pango.Scale.PangoScale; } } if (line == null || line.Length == 0 || column < 0) return 0; int logicalRulerColumn = line.GetLogicalColumn (textEditor.GetTextEditorData (), textEditor.Options.RulerColumn); int lineOffset = line.Offset; StringBuilder textBuilder = new StringBuilder (); ISyntaxMode mode = Document.SyntaxMode != null && textEditor.Options.EnableSyntaxHighlighting ? Document.SyntaxMode : new SyntaxMode (Document); var startChunk = GetCachedChunks (mode, Document, textEditor.ColorStyle, line, lineOffset, line.Length); foreach (Chunk chunk in startChunk) { try { textBuilder.Append (Document.GetTextAt (chunk)); } catch (Exception e) { Console.WriteLine (e); return 0; } } string lineText = textBuilder.ToString (); char[] lineChars = lineText.ToCharArray (); bool containsPreedit = textEditor.ContainsPreedit (lineOffset, line.Length); uint preeditLength = 0; if (containsPreedit) { lineText = lineText.Insert (textEditor.preeditOffset - lineOffset, textEditor.preeditString); preeditLength = (uint)textEditor.preeditString.Length; } if (column < lineText.Length) lineText = lineText.Substring (0, column); var layout = PangoUtil.CreateLayout (textEditor, lineText); layout.Alignment = Pango.Alignment.Left; layout.FontDescription = textEditor.Options.Font; layout.Tabs = tabArray; int startOffset = lineOffset, endOffset = lineOffset + line.Length; uint curIndex = 0, byteIndex = 0; uint curChunkIndex = 0, byteChunkIndex = 0; List<Pango.Attribute> attributes = new List<Pango.Attribute> (); uint oldEndIndex = 0; Cairo.Color curFgColor = textEditor.ColorStyle.PlainText.Foreground; Cairo.Color curBgColor = textEditor.ColorStyle.PlainText.Background; var curWeight = Xwt.Drawing.FontWeight.Normal; var curStyle = Xwt.Drawing.FontStyle.Normal; foreach (Chunk chunk in startChunk) { ChunkStyle chunkStyle = chunk != null ? textEditor.ColorStyle.GetChunkStyle (chunk) : null; foreach (TextLineMarker marker in line.Markers) chunkStyle = marker.GetStyle (chunkStyle); if (chunkStyle != null) { startOffset = chunk.Offset; endOffset = chunk.EndOffset; uint startIndex = (uint)(oldEndIndex); uint endIndex = (uint)(startIndex + chunk.Length); oldEndIndex = endIndex; if (containsPreedit) { if (textEditor.preeditOffset < startOffset) startIndex += preeditLength; if (textEditor.preeditOffset < endOffset) endIndex += preeditLength; } HandleSelection (lineOffset, logicalRulerColumn, - 1, -1, chunk.Offset, chunk.EndOffset, delegate(int start, int end) { var color = textEditor.ColorStyle.GetForeground (chunkStyle); var si = TranslateToUTF8Index (lineChars, (uint)(startIndex + start - chunk.Offset), ref curIndex, ref byteIndex); var ei = TranslateToUTF8Index (lineChars, (uint)(startIndex + end - chunk.Offset), ref curIndex, ref byteIndex); if (!color.Equals (curFgColor)) { curFgColor = color; var foreGround = new Pango.AttrForeground ( (ushort)(color.R * ushort.MaxValue), (ushort)(color.G * ushort.MaxValue), (ushort)(color.B * ushort.MaxValue)); foreGround.StartIndex = si; foreGround.EndIndex = ei; attributes.Add (foreGround); } if (!chunkStyle.TransparentBackground) { color = chunkStyle.Background; if (!color.Equals (curBgColor)) { var background = new Pango.AttrBackground ( (ushort)(color.R * ushort.MaxValue), (ushort)(color.G * ushort.MaxValue), (ushort)(color.B * ushort.MaxValue)); background.StartIndex = si; background.EndIndex = ei; attributes.Add (background); } } }, delegate(int start, int end) { Pango.AttrForeground selectedForeground; if (!SelectionColor.TransparentForeground) { var color = SelectionColor.Foreground; if (color.Equals (curFgColor)) return; curFgColor = color; selectedForeground = new Pango.AttrForeground ( (ushort)(color.R * ushort.MaxValue), (ushort)(color.G * ushort.MaxValue), (ushort)(color.B * ushort.MaxValue)); } else { var color = ColorStyle.GetForeground (chunkStyle); if (color.Equals (curFgColor)) return; curFgColor = color; selectedForeground = new Pango.AttrForeground ( (ushort)(color.R * ushort.MaxValue), (ushort)(color.G * ushort.MaxValue), (ushort)(color.B * ushort.MaxValue)); } selectedForeground.StartIndex = TranslateToUTF8Index (lineChars, (uint)(startIndex + start - chunk.Offset), ref curIndex, ref byteIndex); selectedForeground.EndIndex = TranslateToUTF8Index (lineChars, (uint)(startIndex + end - chunk.Offset), ref curIndex, ref byteIndex); attributes.Add (selectedForeground); }); var translatedStartIndex = TranslateToUTF8Index (lineChars, (uint)startIndex, ref curChunkIndex, ref byteChunkIndex); var translatedEndIndex = TranslateToUTF8Index (lineChars, (uint)endIndex, ref curChunkIndex, ref byteChunkIndex); if (chunkStyle.FontWeight != curWeight) { curWeight = chunkStyle.FontWeight; var attrWeight = new Pango.AttrWeight ((Pango.Weight)chunkStyle.FontWeight); attrWeight.StartIndex = translatedStartIndex; attrWeight.EndIndex = translatedEndIndex; attributes.Add (attrWeight); } if (chunkStyle.FontStyle != curStyle) { curStyle = chunkStyle.FontStyle; Pango.AttrStyle attrStyle = new Pango.AttrStyle ((Pango.Style)chunkStyle.FontStyle); attrStyle.StartIndex = translatedStartIndex; attrStyle.EndIndex = translatedEndIndex; attributes.Add (attrStyle); } if (chunkStyle.Underline) { var attrUnderline = new Pango.AttrUnderline (Pango.Underline.Single); attrUnderline.StartIndex = translatedStartIndex; attrUnderline.EndIndex = translatedEndIndex; attributes.Add (attrUnderline); } } } Pango.AttrList attributeList = new Pango.AttrList (); attributes.ForEach (attr => attributeList.Insert (attr)); layout.Attributes = attributeList; Pango.Rectangle inkrect, logicalrect; layout.GetExtents (out inkrect, out logicalrect); attributes.ForEach (attr => attr.Dispose ()); attributeList.Dispose (); layout.Dispose (); return (logicalrect.Width + Pango.Scale.PangoScale - 1) / Pango.Scale.PangoScale; }
public double ColumnToX (LineSegment line, int column) { column--; if (line == null || line.Length == 0 || column < 0) return 0; int logicalRulerColumn = line.GetLogicalColumn (textEditor.GetTextEditorData (), textEditor.Options.RulerColumn); int lineOffset = line.Offset; StringBuilder textBuilder = new StringBuilder (); ISyntaxMode mode = Document.SyntaxMode != null && textEditor.Options.EnableSyntaxHighlighting ? Document.SyntaxMode : new SyntaxMode (Document); var startChunk = GetCachedChunks (mode, Document, textEditor.ColorStyle, line, lineOffset, line.Length); foreach (Chunk chunk in startChunk) { try { textBuilder.Append (Document.GetTextAt (chunk)); } catch (Exception e) { Console.WriteLine (e); return 0; } } string lineText = textBuilder.ToString (); char[] lineChars = lineText.ToCharArray (); bool containsPreedit = textEditor.ContainsPreedit (lineOffset, line.Length); uint preeditLength = 0; if (containsPreedit) { lineText = lineText.Insert (textEditor.preeditOffset - lineOffset, textEditor.preeditString); preeditLength = (uint)textEditor.preeditString.Length; } if (column < lineText.Length) lineText = lineText.Substring (0, column); var layout = PangoUtil.CreateLayout (textEditor, lineText); layout.Alignment = Pango.Alignment.Left; layout.FontDescription = textEditor.Options.Font; layout.Tabs = tabArray; int startOffset = lineOffset, endOffset = lineOffset + line.Length; uint curIndex = 0, byteIndex = 0; uint curChunkIndex = 0, byteChunkIndex = 0; List<Pango.Attribute> attributes = new List<Pango.Attribute> (); uint oldEndIndex = 0; foreach (Chunk chunk in startChunk) { ChunkStyle chunkStyle = chunk != null ? textEditor.ColorStyle.GetChunkStyle (chunk) : null; foreach (TextMarker marker in line.Markers) chunkStyle = marker.GetStyle (chunkStyle); if (chunkStyle != null) { startOffset = chunk.Offset; endOffset = chunk.EndOffset; uint startIndex = (uint)(oldEndIndex); uint endIndex = (uint)(startIndex + chunk.Length); oldEndIndex = endIndex; if (containsPreedit) { if (textEditor.preeditOffset < startOffset) startIndex += preeditLength; if (textEditor.preeditOffset < endOffset) endIndex += preeditLength; } HandleSelection (lineOffset, logicalRulerColumn, - 1, -1, chunk.Offset, chunk.EndOffset, delegate(int start, int end) { Pango.AttrForeground foreGround = new Pango.AttrForeground (chunkStyle.Color.Red, chunkStyle.Color.Green, chunkStyle.Color.Blue); foreGround.StartIndex = TranslateToUTF8Index (lineChars, (uint)(startIndex + start - chunk.Offset), ref curIndex, ref byteIndex); foreGround.EndIndex = TranslateToUTF8Index (lineChars, (uint)(startIndex + end - chunk.Offset), ref curIndex, ref byteIndex); attributes.Add (foreGround); if (!chunkStyle.TransparentBackround) { var background = new Pango.AttrBackground (chunkStyle.BackgroundColor.Red, chunkStyle.BackgroundColor.Green, chunkStyle.BackgroundColor.Blue); background.StartIndex = foreGround.StartIndex; background.EndIndex = foreGround.EndIndex; attributes.Add (background); } }, delegate(int start, int end) { Pango.AttrForeground selectedForeground = new Pango.AttrForeground (SelectionColor.Color.Red, SelectionColor.Color.Green, SelectionColor.Color.Blue); selectedForeground.StartIndex = TranslateToUTF8Index (lineChars, (uint)(startIndex + start - chunk.Offset), ref curIndex, ref byteIndex); selectedForeground.EndIndex = TranslateToUTF8Index (lineChars, (uint)(startIndex + end - chunk.Offset), ref curIndex, ref byteIndex); attributes.Add (selectedForeground); }); var translatedStartIndex = TranslateToUTF8Index (lineChars, (uint)startIndex, ref curChunkIndex, ref byteChunkIndex); var translatedEndIndex = TranslateToUTF8Index (lineChars, (uint)endIndex, ref curChunkIndex, ref byteChunkIndex); if (chunkStyle.Bold) { Pango.AttrWeight attrWeight = new Pango.AttrWeight (Pango.Weight.Bold); attrWeight.StartIndex = translatedStartIndex; attrWeight.EndIndex = translatedEndIndex; attributes.Add (attrWeight); } if (chunkStyle.Italic) { Pango.AttrStyle attrStyle = new Pango.AttrStyle (Pango.Style.Italic); attrStyle.StartIndex = translatedStartIndex; attrStyle.EndIndex = translatedEndIndex; attributes.Add (attrStyle); } if (chunkStyle.Underline) { Pango.AttrUnderline attrUnderline = new Pango.AttrUnderline (Pango.Underline.Single); attrUnderline.StartIndex = translatedStartIndex; attrUnderline.EndIndex = translatedEndIndex; attributes.Add (attrUnderline); } } } Pango.AttrList attributeList = new Pango.AttrList (); attributes.ForEach (attr => attributeList.Insert (attr)); layout.Attributes = attributeList; Pango.Rectangle ink_rect, logical_rect; layout.GetExtents (out ink_rect, out logical_rect); attributes.ForEach (attr => attr.Dispose ()); attributeList.Dispose (); layout.Dispose (); return (logical_rect.Width + Pango.Scale.PangoScale - 1) / Pango.Scale.PangoScale; }
protected override LayoutWrapper RenderLine (long line) { Pango.Layout layout = new Pango.Layout (Editor.PangoContext); layout.FontDescription = Editor.Options.Font; layout.Tabs = tabArray; StringBuilder sb = new StringBuilder (); long startOffset = line * Editor.BytesInRow; long endOffset = System.Math.Min (startOffset + Editor.BytesInRow, Data.Length); byte[] lineBytes = Data.GetBytes (startOffset, (int)(endOffset - startOffset)); for (int i = 0; i < lineBytes.Length; i++) { sb.Append (string.Format ("{0:X2}", lineBytes[i])); if (i % Editor.Options.GroupBytes == 0) sb.Append ("\t"); } layout.SetText (sb.ToString ()); char[] lineChars = sb.ToString ().ToCharArray (); Margin.LayoutWrapper result = new LayoutWrapper (layout); uint curIndex = 0, byteIndex = 0; if (Data.IsSomethingSelected) { ISegment selection = Data.MainSelection.Segment; HandleSelection (selection.Offset, selection.EndOffset, startOffset, endOffset, null, delegate(long start, long end) { Pango.AttrForeground selectedForeground = new Pango.AttrForeground (Style.Selection.Red, Style.Selection.Green, Style.Selection.Blue); selectedForeground.StartIndex = TranslateToUTF8Index (lineChars, TranslateColumn (start - startOffset), ref curIndex, ref byteIndex); selectedForeground.EndIndex = TranslateToUTF8Index (lineChars, TranslateColumn (end - startOffset) - 1, ref curIndex, ref byteIndex); result.Add (selectedForeground); Pango.AttrBackground attrBackground = new Pango.AttrBackground (Style.SelectionBg.Red, Style.SelectionBg.Green, Style.SelectionBg.Blue); attrBackground.StartIndex = selectedForeground.StartIndex; attrBackground.EndIndex = selectedForeground.EndIndex; result.Add (attrBackground); }); } result.SetAttributes (); return result; }
protected virtual void FindEntry_ExposeEvent(object o, Gtk.ExposeEventArgs args) { if (m_findEntry.Text != s_DefaultSearchText) return; var length = m_findEntry.Text.Length; var attr = new Pango.AttrForeground (0xFFFF / 2, 0xFFFF / 2, 0xFFFF / 2); attr.StartIndex = (uint)0; attr.EndIndex = (uint)length; var attrs = m_findEntry.Layout.Attributes; attrs.Insert (attr); m_findEntry.Layout.Attributes = attrs; }
protected override LayoutWrapper RenderLine (long line) { Pango.Layout layout = new Pango.Layout (Editor.PangoContext); layout.FontDescription = Editor.Options.Font; StringBuilder sb = new StringBuilder (); long startOffset = line * Editor.BytesInRow; long endOffset = System.Math.Min (startOffset + Editor.BytesInRow, Data.Length); byte[] lineBytes = Data.GetBytes (startOffset, (int)(endOffset - startOffset)); for (int i = 0; i < lineBytes.Length; i++) { byte b = lineBytes[i]; char ch = (char)b; if (b < 128 && (Char.IsLetterOrDigit (ch) || Char.IsPunctuation (ch))) { sb.Append (ch); } else { sb.Append ("."); } } layout.SetText (sb.ToString ()); char[] lineChars = layout.Text.ToCharArray (); Margin.LayoutWrapper result = new LayoutWrapper (layout); uint curIndex = 0, byteIndex = 0; if (Data.IsSomethingSelected) { ISegment selection = Data.MainSelection.Segment; HandleSelection (selection.Offset, selection.EndOffset, startOffset, endOffset, null, delegate(long start, long end) { Pango.AttrForeground selectedForeground = new Pango.AttrForeground (Style.Selection.Red, Style.Selection.Green, Style.Selection.Blue); selectedForeground.StartIndex = TranslateToUTF8Index (lineChars, (uint)(start - startOffset), ref curIndex, ref byteIndex); selectedForeground.EndIndex = TranslateToUTF8Index (lineChars, (uint)(end - startOffset), ref curIndex, ref byteIndex); result.Add (selectedForeground); Pango.AttrBackground attrBackground = new Pango.AttrBackground (Style.SelectionBg.Red, Style.SelectionBg.Green, Style.SelectionBg.Blue); attrBackground.StartIndex = selectedForeground.StartIndex; attrBackground.EndIndex = selectedForeground.EndIndex; result.Add (attrBackground); }); } result.SetAttributes (); return result; }
// TODO: GTK+ 3.2 adds a placeholder-text property, but for now // we have to handle it ourselves protected override bool OnDrawn(Cairo.Context cr) { bool ret = base.OnDrawn (cr); if(Text.Length > 0 || HasFocus || EmptyMessage == null) { return ret; } Layout.SetMarkup (EmptyMessage); Gdk.RGBA color; if (!StyleContext.LookupColor ("placeholder_text_color", out color)) { color = StyleContext.GetColor (StateFlags.Insensitive); } Pango.Attribute attr = new Pango.AttrForeground (Convert.ToUInt16 (color.Red * 65535), Convert.ToUInt16 (color.Green * 65535), Convert.ToUInt16 (color.Blue * 65535)); Layout.Attributes.Insert (attr); return ret; }