~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.HexEditor/Mono.MHex.Rendering/HexEditorMargin.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-07-05 13:00:05 UTC
  • mfrom: (1.2.8 upstream) (1.3.9 experimental)
  • Revision ID: james.westby@ubuntu.com-20100705130005-d6hp4k5gcn1xkj8c
Tags: 2.4+dfsg-1ubuntu1
* debian/patches/remove_support_for_moonlight.patch,
  debian/patches/dont_add_moonlight_to_core_addins.patch,
  debian/control:
  + Enable support for Moonlight
* debian/rules:
  + Ensure Moonlight addin isn't shipped in main MonoDevelop package by
    mistake

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// HexEditorMargin.cs
 
3
//  
 
4
// Author:
 
5
//       Mike Krüger <mkrueger@novell.com>
 
6
// 
 
7
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Text;
 
29
using Mono.MHex.Data;
 
30
 
 
31
namespace Mono.MHex.Rendering
 
32
{
 
33
        public class HexEditorMargin : Margin
 
34
        {
 
35
                int groupWidth, byteWidth;
 
36
                        
 
37
                public int LineHeight {
 
38
                        get {
 
39
                                return Editor.LineHeight;
 
40
                        }
 
41
                }
 
42
                
 
43
                public override int Width {
 
44
                        get {
 
45
                                return CalculateWidth (Editor.BytesInRow);
 
46
                        }
 
47
                }
 
48
                
 
49
                public override int CalculateWidth (int bytesInRow)
 
50
                {
 
51
                        return (bytesInRow / Editor.Options.GroupBytes) * groupWidth;
 
52
                }
 
53
                
 
54
                Pango.TabArray tabArray = null;
 
55
                
 
56
                public HexEditorMargin (HexEditor hexEditor) : base (hexEditor)
 
57
                {
 
58
                        
 
59
                }
 
60
                
 
61
                Gdk.GC bgGC;
 
62
                Gdk.GC fgGC;
 
63
 
 
64
                internal protected override void OptionsChanged ()
 
65
                {
 
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 + " ");
 
70
                        int lineHeight;
 
71
                        layout.GetPixelSize (out groupWidth, out lineHeight);
 
72
                        Data.LineHeight = lineHeight;
 
73
                        
 
74
                        layout.SetText ("00");
 
75
                        layout.GetPixelSize (out byteWidth, out lineHeight);
 
76
                        
 
77
                        layout.Dispose ();
 
78
                        
 
79
                        tabArray = new Pango.TabArray (1, true);
 
80
                        tabArray.SetTab (0, Pango.TabAlign.Left, groupWidth);
 
81
                        
 
82
                        bgGC = GetGC (Style.HexDigitBg);
 
83
                        fgGC = GetGC (Style.HexDigit);
 
84
                }
 
85
 
 
86
                
 
87
                protected override LayoutWrapper RenderLine (long line)
 
88
                {
 
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)
 
99
                                        sb.Append ("\t");
 
100
                        }
 
101
                        
 
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);
 
115
                                        
 
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);
 
122
 
 
123
                                });
 
124
                        }
 
125
                        result.SetAttributes ();
 
126
                        return result;
 
127
                }
 
128
 
 
129
                uint TranslateColumn (long column)
 
130
                {
 
131
                        return (uint)(column * 3);
 
132
                }
 
133
 
 
134
                
 
135
                internal protected override void Draw (Gdk.Drawable drawable, Gdk.Rectangle area, long line, int x, int y)
 
136
                {
 
137
                        drawable.DrawRectangle (bgGC, true, x, y, Width, Editor.LineHeight);
 
138
                        LayoutWrapper layout = GetLayout (line);
 
139
                        
 
140
                        if (!Data.IsSomethingSelected && Caret.InTextEditor && line == Data.Caret.Line) {
 
141
                                drawable.DrawRectangle (GetGC (Style.HighlightOffset), true, CalculateCaretXPos (false), y, byteWidth, Editor.LineHeight);
 
142
                        }
 
143
                        
 
144
                        drawable.DrawLayout (fgGC, x, y, layout.Layout);
 
145
                        if (layout.IsUncached)
 
146
                                layout.Dispose ();
 
147
                }
 
148
                
 
149
                public int CalculateCaretXPos ()
 
150
                {
 
151
                        return CalculateCaretXPos (true);
 
152
                }
 
153
                int CalculateCaretXPos (bool useSubPositon)
 
154
                {
 
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;
 
159
                        if (useSubPositon)
 
160
                                caretIndex += Caret.SubPosition;
 
161
                        LayoutWrapper layout = GetLayout ((int)Caret.Line);
 
162
                        Pango.Rectangle rectangle = layout.Layout.IndexToPos (caretIndex);
 
163
                        if (layout.IsUncached)
 
164
                                layout.Dispose ();
 
165
                        return XOffset + (int)(rectangle.X / Pango.Scale.PangoScale);
 
166
                }
 
167
                
 
168
                internal protected override void MousePressed (MarginMouseEventArgs args)
 
169
                {
 
170
                        base.MousePressed (args);
 
171
                        
 
172
                        if (args.Button != 1)
 
173
                                return;
 
174
                        
 
175
                        Caret.InTextEditor = false;
 
176
                        int groupChar;
 
177
                        Caret.Offset      = GetOffset (args, out groupChar);
 
178
                        Caret.SubPosition = groupChar % 2;
 
179
                }
 
180
 
 
181
                long GetOffset (MarginMouseEventArgs args, out int groupChar)
 
182
                {
 
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;
 
186
                }
 
187
                
 
188
                internal protected override void MouseHover (MarginMouseEventArgs args)
 
189
                {
 
190
                        base.MouseHover (args);
 
191
 
 
192
                        if (args.Button != 1)
 
193
                                return;
 
194
                        Caret.InTextEditor = false;
 
195
                        
 
196
                        int groupChar;
 
197
                        long hoverOffset = GetOffset (args, out groupChar);
 
198
                        if (Data.MainSelection == null) {
 
199
                                Data.SetSelection (hoverOffset, hoverOffset);
 
200
                        } else {
 
201
                                Data.ExtendSelectionTo (hoverOffset);
 
202
                        }
 
203
                        Caret.PreserveSelection = true;
 
204
                        Caret.Offset = hoverOffset;
 
205
                        Caret.PreserveSelection = false;
 
206
                }
 
207
 
 
208
        }
 
209
}