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 HexEditorMargin : Margin
35
int groupWidth, byteWidth;
37
public int LineHeight {
39
return Editor.LineHeight;
43
public override int Width {
45
return CalculateWidth (Editor.BytesInRow);
49
public override int CalculateWidth (int bytesInRow)
51
return (bytesInRow / Editor.Options.GroupBytes) * groupWidth;
54
Pango.TabArray tabArray = null;
56
public HexEditorMargin (HexEditor hexEditor) : base (hexEditor)
64
internal protected override void OptionsChanged ()
66
Pango.Layout layout = new Pango.Layout (Editor.PangoContext);
67
layout.FontDescription = Editor.Options.Font;
68
string groupString = new string ('0', Editor.Options.GroupBytes * 2);
69
layout.SetText (groupString + " ");
71
layout.GetPixelSize (out groupWidth, out lineHeight);
72
Data.LineHeight = lineHeight;
74
layout.SetText ("00");
75
layout.GetPixelSize (out byteWidth, out lineHeight);
79
tabArray = new Pango.TabArray (1, true);
80
tabArray.SetTab (0, Pango.TabAlign.Left, groupWidth);
82
bgGC = GetGC (Style.HexDigitBg);
83
fgGC = GetGC (Style.HexDigit);
87
protected override LayoutWrapper RenderLine (long line)
89
Pango.Layout layout = new Pango.Layout (Editor.PangoContext);
90
layout.FontDescription = Editor.Options.Font;
91
layout.Tabs = tabArray;
92
StringBuilder sb = new StringBuilder ();
93
long startOffset = line * Editor.BytesInRow;
94
long endOffset = System.Math.Min (startOffset + Editor.BytesInRow, Data.Length);
95
byte[] lineBytes = Data.GetBytes (startOffset, (int)(endOffset - startOffset));
96
for (int i = 0; i < lineBytes.Length; i++) {
97
sb.Append (string.Format ("{0:X2}", lineBytes[i]));
98
if (i % Editor.Options.GroupBytes == 0)
102
layout.SetText (sb.ToString ());
103
char[] lineChars = sb.ToString ().ToCharArray ();
104
Margin.LayoutWrapper result = new LayoutWrapper (layout);
105
uint curIndex = 0, byteIndex = 0;
106
if (Data.IsSomethingSelected) {
107
ISegment selection = Data.MainSelection.Segment;
108
HandleSelection (selection.Offset, selection.EndOffset, startOffset, endOffset, null, delegate(long start, long end) {
109
Pango.AttrForeground selectedForeground = new Pango.AttrForeground (Style.Selection.Red,
110
Style.Selection.Green,
111
Style.Selection.Blue);
112
selectedForeground.StartIndex = TranslateToUTF8Index (lineChars, TranslateColumn (start - startOffset), ref curIndex, ref byteIndex);
113
selectedForeground.EndIndex = TranslateToUTF8Index (lineChars, TranslateColumn (end - startOffset) - 1, ref curIndex, ref byteIndex);
114
result.Add (selectedForeground);
116
Pango.AttrBackground attrBackground = new Pango.AttrBackground (Style.SelectionBg.Red,
117
Style.SelectionBg.Green,
118
Style.SelectionBg.Blue);
119
attrBackground.StartIndex = selectedForeground.StartIndex;
120
attrBackground.EndIndex = selectedForeground.EndIndex;
121
result.Add (attrBackground);
125
result.SetAttributes ();
129
uint TranslateColumn (long column)
131
return (uint)(column * 3);
135
internal protected override void Draw (Gdk.Drawable drawable, Gdk.Rectangle area, long line, int x, int y)
137
drawable.DrawRectangle (bgGC, true, x, y, Width, Editor.LineHeight);
138
LayoutWrapper layout = GetLayout (line);
140
if (!Data.IsSomethingSelected && Caret.InTextEditor && line == Data.Caret.Line) {
141
drawable.DrawRectangle (GetGC (Style.HighlightOffset), true, CalculateCaretXPos (false), y, byteWidth, Editor.LineHeight);
144
drawable.DrawLayout (fgGC, x, y, layout.Layout);
145
if (layout.IsUncached)
149
public int CalculateCaretXPos ()
151
return CalculateCaretXPos (true);
153
int CalculateCaretXPos (bool useSubPositon)
155
int byteInRow = (int)Caret.Offset % BytesInRow;
156
int groupNumber = byteInRow / Editor.Options.GroupBytes;
157
int groupByte = byteInRow % Editor.Options.GroupBytes;
158
int caretIndex = groupNumber * (Editor.Options.GroupBytes * 2 + 1) + groupByte * 2;
160
caretIndex += Caret.SubPosition;
161
LayoutWrapper layout = GetLayout ((int)Caret.Line);
162
Pango.Rectangle rectangle = layout.Layout.IndexToPos (caretIndex);
163
if (layout.IsUncached)
165
return XOffset + (int)(rectangle.X / Pango.Scale.PangoScale);
168
internal protected override void MousePressed (MarginMouseEventArgs args)
170
base.MousePressed (args);
172
if (args.Button != 1)
175
Caret.InTextEditor = false;
177
Caret.Offset = GetOffset (args, out groupChar);
178
Caret.SubPosition = groupChar % 2;
181
long GetOffset (MarginMouseEventArgs args, out int groupChar)
183
int groupNumber = args.X / groupWidth;
184
groupChar = (args.X % groupWidth) / Editor.textEditorMargin.charWidth;
185
return args.Line * BytesInRow + groupNumber * Editor.Options.GroupBytes + groupChar / 2;
188
internal protected override void MouseHover (MarginMouseEventArgs args)
190
base.MouseHover (args);
192
if (args.Button != 1)
194
Caret.InTextEditor = false;
197
long hoverOffset = GetOffset (args, out groupChar);
198
if (Data.MainSelection == null) {
199
Data.SetSelection (hoverOffset, hoverOffset);
201
Data.ExtendSelectionTo (hoverOffset);
203
Caret.PreserveSelection = true;
204
Caret.Offset = hoverOffset;
205
Caret.PreserveSelection = false;