5
// Mike Krüger <mkrueger@novell.com>
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31
namespace Mono.MHex.Rendering
33
public class TextEditorMargin : Margin
35
internal int charWidth;
36
public override int Width {
38
return charWidth * Editor.BytesInRow;
42
public override int CalculateWidth (int bytesInRow)
44
return charWidth * bytesInRow;
47
public TextEditorMargin (HexEditor hexEditor) : base (hexEditor)
53
internal protected override void OptionsChanged ()
55
Pango.Layout layout = new Pango.Layout (Editor.PangoContext);
56
layout.FontDescription = Editor.Options.Font;
59
layout.GetPixelSize (out charWidth, out height);
61
bgGC = GetGC (Style.HexDigitBg);
62
fgGC = GetGC (Style.HexDigit);
65
protected override LayoutWrapper RenderLine (long line)
67
Pango.Layout layout = new Pango.Layout (Editor.PangoContext);
68
layout.FontDescription = Editor.Options.Font;
69
StringBuilder sb = new StringBuilder ();
70
long startOffset = line * Editor.BytesInRow;
71
long endOffset = System.Math.Min (startOffset + Editor.BytesInRow, Data.Length);
72
byte[] lineBytes = Data.GetBytes (startOffset, (int)(endOffset - startOffset));
73
for (int i = 0; i < lineBytes.Length; i++) {
74
byte b = lineBytes[i];
76
if (b < 128 && (Char.IsLetterOrDigit (ch) || Char.IsPunctuation (ch))) {
83
layout.SetText (sb.ToString ());
84
char[] lineChars = layout.Text.ToCharArray ();
85
Margin.LayoutWrapper result = new LayoutWrapper (layout);
86
uint curIndex = 0, byteIndex = 0;
87
if (Data.IsSomethingSelected) {
88
ISegment selection = Data.MainSelection.Segment;
89
HandleSelection (selection.Offset, selection.EndOffset, startOffset, endOffset, null, delegate(long start, long end) {
90
Pango.AttrForeground selectedForeground = new Pango.AttrForeground (Style.Selection.Red,
91
Style.Selection.Green,
92
Style.Selection.Blue);
93
selectedForeground.StartIndex = TranslateToUTF8Index (lineChars, (uint)(start - startOffset), ref curIndex, ref byteIndex);
94
selectedForeground.EndIndex = TranslateToUTF8Index (lineChars, (uint)(end - startOffset), ref curIndex, ref byteIndex);
96
result.Add (selectedForeground);
98
Pango.AttrBackground attrBackground = new Pango.AttrBackground (Style.SelectionBg.Red,
99
Style.SelectionBg.Green,
100
Style.SelectionBg.Blue);
101
attrBackground.StartIndex = selectedForeground.StartIndex;
102
attrBackground.EndIndex = selectedForeground.EndIndex;
103
result.Add (attrBackground);
107
result.SetAttributes ();
111
internal protected override void Draw (Gdk.Drawable drawable, Gdk.Rectangle area, long line, int x, int y)
114
drawable.DrawRectangle (bgGC, true, x, y, Width, Editor.LineHeight);
115
LayoutWrapper layout = GetLayout (line);
116
if (!Data.IsSomethingSelected && !Caret.InTextEditor && line == Data.Caret.Line) {
117
int column = (int)(Caret.Offset % BytesInRow);
118
int xOffset = charWidth * column;
119
drawable.DrawRectangle (GetGC (Style.HighlightOffset), true, x + xOffset, y, charWidth, Editor.LineHeight);
121
drawable.DrawLayout (fgGC, x, y, layout.Layout);
122
if (layout.IsUncached)
126
public int CalculateCaretXPos ()
128
return (int)(XOffset + Caret.Offset % BytesInRow * charWidth);
131
internal protected override void MousePressed (MarginMouseEventArgs args)
133
base.MousePressed (args);
135
if (args.Button != 1)
138
Caret.InTextEditor = true;
139
Caret.Offset = GetOffset (args);
142
long GetOffset (MarginMouseEventArgs args)
144
return args.Line * BytesInRow + args.X / charWidth;
147
internal protected override void MouseHover (MarginMouseEventArgs args)
149
base.MouseHover (args);
151
if (args.Button != 1)
153
Caret.InTextEditor = true;
155
long hoverOffset = GetOffset (args);
156
if (Data.MainSelection == null) {
157
Data.SetSelection (hoverOffset, hoverOffset);
159
Data.ExtendSelectionTo (hoverOffset);
161
Caret.PreserveSelection = true;
162
Caret.Offset = hoverOffset;
163
Caret.PreserveSelection = false;