~ubuntu-branches/ubuntu/maverick/monodevelop/maverick

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.HexEditor/Mono.MHex.Rendering/TextEditorMargin.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
// TextEditorMargin.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 TextEditorMargin : Margin
 
34
        {
 
35
                internal int charWidth;
 
36
                public override int Width {
 
37
                        get {
 
38
                                return charWidth * Editor.BytesInRow;
 
39
                        }
 
40
                }
 
41
                
 
42
                public override int CalculateWidth (int bytesInRow)
 
43
                {
 
44
                        return charWidth * bytesInRow;
 
45
                }
 
46
                
 
47
                public TextEditorMargin (HexEditor hexEditor) : base (hexEditor)
 
48
                {
 
49
                }
 
50
                
 
51
                Gdk.GC bgGC;
 
52
                Gdk.GC fgGC;
 
53
                internal protected override void OptionsChanged ()
 
54
                {
 
55
                        Pango.Layout layout = new Pango.Layout (Editor.PangoContext);
 
56
                        layout.FontDescription = Editor.Options.Font;
 
57
                        layout.SetText (".");
 
58
                        int height;
 
59
                        layout.GetPixelSize (out charWidth, out height);
 
60
                        layout.Dispose ();
 
61
                        bgGC = GetGC (Style.HexDigitBg);
 
62
                        fgGC = GetGC (Style.HexDigit);
 
63
                }
 
64
                
 
65
                protected override LayoutWrapper RenderLine (long line)
 
66
                {
 
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];
 
75
                                char ch = (char)b;
 
76
                                if (b < 128 && (Char.IsLetterOrDigit (ch) || Char.IsPunctuation (ch))) {
 
77
                                        sb.Append (ch);
 
78
                                } else {
 
79
                                        sb.Append (".");
 
80
                                }
 
81
                        }
 
82
                        
 
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);
 
95
                                        
 
96
                                        result.Add (selectedForeground);
 
97
                                        
 
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);
 
104
 
 
105
                                });
 
106
                        }
 
107
                        result.SetAttributes ();
 
108
                        return result;
 
109
                }
 
110
                
 
111
                internal protected override void Draw (Gdk.Drawable drawable, Gdk.Rectangle area, long line, int x, int y)
 
112
                {
 
113
                        
 
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);
 
120
                        }
 
121
                        drawable.DrawLayout (fgGC, x, y, layout.Layout);
 
122
                        if (layout.IsUncached)
 
123
                                layout.Dispose ();
 
124
                }
 
125
 
 
126
                public int CalculateCaretXPos ()
 
127
                {
 
128
                        return (int)(XOffset + Caret.Offset % BytesInRow * charWidth);
 
129
                }
 
130
                
 
131
                internal protected override void MousePressed (MarginMouseEventArgs args)
 
132
                {
 
133
                        base.MousePressed (args);
 
134
                        
 
135
                        if (args.Button != 1)
 
136
                                return;
 
137
                        
 
138
                        Caret.InTextEditor = true;
 
139
                        Caret.Offset = GetOffset (args);
 
140
                }
 
141
 
 
142
                long GetOffset (MarginMouseEventArgs args)
 
143
                {
 
144
                        return args.Line * BytesInRow + args.X / charWidth;
 
145
                }
 
146
                
 
147
                internal protected override void MouseHover (MarginMouseEventArgs args)
 
148
                {
 
149
                        base.MouseHover (args);
 
150
 
 
151
                        if (args.Button != 1)
 
152
                                return;
 
153
                        Caret.InTextEditor = true;
 
154
                        
 
155
                        long hoverOffset = GetOffset (args);
 
156
                        if (Data.MainSelection == null) {
 
157
                                Data.SetSelection (hoverOffset, hoverOffset);
 
158
                        } else {
 
159
                                Data.ExtendSelectionTo (hoverOffset);
 
160
                        }
 
161
                        Caret.PreserveSelection = true;
 
162
                        Caret.Offset = hoverOffset;
 
163
                        Caret.PreserveSelection = false;
 
164
                }
 
165
        }
 
166
}