~ubuntu-branches/ubuntu/natty/monodevelop/natty

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor/FoldMarkerMargin.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2010-01-07 19:06:58 UTC
  • mto: (1.6.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 46.
  • Revision ID: james.westby@ubuntu.com-20100107190658-z9z95lgk4kwfes7p
ImportĀ upstreamĀ versionĀ 2.2+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
using System.Collections.Generic;
30
30
using System.Linq;
31
31
using Gtk;
 
32
using System.Timers;
32
33
 
33
34
namespace Mono.TextEditor
34
35
{
50
51
                {
51
52
                        this.editor = editor;
52
53
                        layout = new Pango.Layout (editor.PangoContext);
 
54
                        delayTimer = new Timer (150);
 
55
                        delayTimer.AutoReset = false;
 
56
                        delayTimer.Elapsed += DelayTimerElapsed;
53
57
                }
 
58
 
54
59
                
55
60
                internal protected override void MousePressed (MarginMouseEventArgs args)
56
61
                {
79
84
                                }
80
85
                        } 
81
86
                        lineHover = lineSegment;
 
87
                        bool found = false;
 
88
                        foreach (FoldSegment segment in editor.Document.GetFoldingContaining (lineSegment)) {
 
89
                                if (segment.StartLine.Offset == lineSegment.Offset) {
 
90
                                        found = true;
 
91
                                        break;
 
92
                                }
 
93
                        }
82
94
                        
 
95
                        delayTimer.Stop ();
 
96
                        if (found) {
 
97
                                foldings = editor.Document.GetFoldingContaining (lineSegment);
 
98
                                if (editor.TextViewMargin.BackgroundRenderer == null) {
 
99
                                        delayTimer.Start ();
 
100
                                } else {
 
101
                                        DelayTimerElapsed (this, null);
 
102
                                }
 
103
                        } else {
 
104
                                RemoveBackgroundRenderer ();
 
105
                        }
 
106
                }
 
107
                
 
108
                Timer delayTimer;
 
109
                IEnumerable<FoldSegment> foldings;
 
110
                void DelayTimerElapsed (object sender, ElapsedEventArgs e)
 
111
                {
 
112
                        editor.TextViewMargin.BackgroundRenderer = new FoldingScreenbackgroundRenderer (editor, foldings);
 
113
                        editor.Repaint ();
 
114
                }
 
115
                
 
116
                void RemoveBackgroundRenderer ()
 
117
                {
 
118
                        if (editor.TextViewMargin.BackgroundRenderer != null) {
 
119
                                editor.TextViewMargin.BackgroundRenderer = null;
 
120
                                editor.Repaint ();
 
121
                        }
83
122
                }
84
123
                
85
124
                internal protected override void MouseLeft ()
90
129
                                lineHover = null;
91
130
                                editor.RedrawMargin (this);
92
131
                        }
 
132
                        delayTimer.Stop ();
 
133
                        RemoveBackgroundRenderer ();
93
134
                }
94
135
                
95
136
                internal protected override void OptionsChanged ()
104
145
                        foldLineHighlightedGC = new Gdk.GC (editor.GdkWindow);
105
146
                        foldLineHighlightedGC.RgbFgColor = editor.ColorStyle.FoldLineHighlighted;
106
147
                        
 
148
                        HslColor hslColor = new HslColor (editor.ColorStyle.Default.BackgroundColor);
 
149
                        double brightness = HslColor.Brightness (editor.ColorStyle.Default.BackgroundColor);
 
150
                        if (brightness < 0.5) {
 
151
                                hslColor.L = hslColor.L * 0.85 + hslColor.L * 0.25;
 
152
                        } else {
 
153
                                hslColor.L = hslColor.L * 0.9;
 
154
                        }
 
155
                        
 
156
                        foldLineHighlightedGCBg = new Gdk.GC (editor.GdkWindow);
 
157
                        foldLineHighlightedGCBg.RgbFgColor = hslColor;
 
158
                        
107
159
                        foldToggleMarkerGC = new Gdk.GC (editor.GdkWindow);
108
160
                        foldToggleMarkerGC.RgbFgColor = editor.ColorStyle.FoldToggleMarker;
109
161
 
113
165
                        lineStateDirtyGC = new Gdk.GC (editor.GdkWindow);
114
166
                        lineStateDirtyGC.RgbFgColor = new Gdk.Color (255, 238, 98);
115
167
 
116
 
                        foldDashedLineGC = new Gdk.GC (editor.GdkWindow);
117
 
                        foldDashedLineGC.RgbBgColor = editor.ColorStyle.FoldLine.Color;
118
 
                        foldDashedLineGC.SetLineAttributes (1, Gdk.LineStyle.OnOffDash, Gdk.CapStyle.NotLast, Gdk.JoinStyle.Bevel);
119
 
                        foldDashedLineGC.SetDashes (0, new sbyte[] { 1, 1 }, 2);
 
168
                        
120
169
                        
121
170
                        layout.FontDescription = editor.Options.Font;
122
171
                        layout.SetText ("!");
124
173
                        layout.GetPixelSize (out tmp, out this.marginWidth);
125
174
                        marginWidth *= 8;
126
175
                        marginWidth /= 10;
 
176
                        marginWidth--; // was dashed line.
127
177
                }
128
178
                
129
 
                Gdk.GC foldBgGC, foldLineGC, foldLineHighlightedGC, foldToggleMarkerGC, foldDashedLineGC;
 
179
                Gdk.GC foldBgGC, foldLineGC, foldLineHighlightedGC, foldLineHighlightedGCBg, foldToggleMarkerGC;
130
180
                Gdk.GC lineStateChangedGC, lineStateDirtyGC;
131
181
                public override void Dispose ()
132
182
                {
 
183
                        base.Dispose ();
 
184
                        if (delayTimer != null) {
 
185
                                delayTimer.Stop ();
 
186
                                delayTimer.Dispose ();
 
187
                                delayTimer = null;
 
188
                        }
133
189
                        layout = layout.Kill ();
134
190
                        DisposeGCs ();
135
191
                }
139
195
                        foldBgGC = foldBgGC.Kill ();
140
196
                        foldLineGC = foldLineGC.Kill ();
141
197
                        foldLineHighlightedGC = foldLineHighlightedGC.Kill ();
 
198
                        foldLineHighlightedGCBg = foldLineHighlightedGCBg.Kill ();
142
199
                        foldToggleMarkerGC = foldToggleMarkerGC.Kill ();
143
200
                        lineStateChangedGC = lineStateChangedGC.Kill ();
144
201
                        lineStateDirtyGC = lineStateDirtyGC.Kill ();
145
 
                        foldDashedLineGC = foldDashedLineGC.Kill ();
146
202
                }
147
203
                
148
204
                void DrawFoldSegment (Gdk.Drawable win, int x, int y, bool isOpen, bool isSelected)
165
221
                                              drawArea.Bottom - drawArea.Height * 3 / 10);
166
222
                }
167
223
                
168
 
                void DrawDashedVLine (Gdk.Drawable win, int x, int top, int bottom, int nline)
169
 
                {
170
 
                        if ((top + (int)editor.VAdjustment.Value) % 2 != 0)
171
 
                                top++;
172
 
                        
173
 
                        win.DrawLine (foldDashedLineGC, x, top, x, bottom);
174
 
                }
175
224
                
176
225
                bool IsMouseHover (IEnumerable<FoldSegment> foldings)
177
226
                {
190
239
                        Gdk.Rectangle drawArea = new Gdk.Rectangle (x, y, Width, editor.LineHeight);
191
240
                        Document.LineState state = editor.Document.GetLineState (line);
192
241
                        
193
 
                        if (state == Document.LineState.Changed) {
194
 
                                win.DrawRectangle (lineStateChangedGC, true, x , y, 4, editor.LineHeight);
195
 
                                win.DrawRectangle (foldBgGC, true, x + 3 , y, Width, editor.LineHeight);
196
 
                        } else if (state == Document.LineState.Dirty) {
197
 
                                win.DrawRectangle (lineStateDirtyGC, true, x , y, 4, editor.LineHeight);
198
 
                                win.DrawRectangle (foldBgGC, true, x + 3 , y, Width, editor.LineHeight);
199
 
                        } else {
200
 
                                win.DrawRectangle (foldBgGC, true, drawArea);
201
 
                        }
202
 
                        DrawDashedVLine (win, x, drawArea.Top, drawArea.Bottom, line);
 
242
                        bool isFoldStart = false;
 
243
                        bool isContaining = false;
 
244
                        bool isFoldEnd = false;
 
245
                        
 
246
                        bool isStartSelected = false;
 
247
                        bool isContainingSelected = false;
 
248
                        bool isEndSelected = false;
203
249
                        
204
250
                        if (line < editor.Document.LineCount) {
205
251
                                LineSegment lineSegment = editor.Document.GetLine (line);
216
262
                                        }
217
263
                                }
218
264
                                
219
 
                                bool isFoldStart  = startFoldings.Count > 0;
220
 
                                bool isContaining = containingFoldings.Count > 0;
221
 
                                bool isFoldEnd    = endFoldings.Count > 0;
 
265
                                isFoldStart  = startFoldings.Count > 0;
 
266
                                isContaining = containingFoldings.Count > 0;
 
267
                                isFoldEnd    = endFoldings.Count > 0;
222
268
                                
223
 
                                bool isStartSelected      = this.lineHover != null && IsMouseHover (startFoldings);
224
 
                                bool isContainingSelected = this.lineHover != null && IsMouseHover (containingFoldings);
225
 
                                bool isEndSelected        = this.lineHover != null && IsMouseHover (endFoldings);
 
269
                                isStartSelected      = this.lineHover != null && IsMouseHover (startFoldings);
 
270
                                isContainingSelected = this.lineHover != null && IsMouseHover (containingFoldings);
 
271
                                isEndSelected        = this.lineHover != null && IsMouseHover (endFoldings);
 
272
                        }
 
273
                        
 
274
                        Gdk.GC bgGC = foldBgGC;
 
275
                        if (editor.TextViewMargin.BackgroundRenderer != null) {
 
276
                                if (isContainingSelected || isStartSelected || isEndSelected) {
 
277
                                        bgGC = foldBgGC;
 
278
                                } else {
 
279
                                        bgGC = foldLineHighlightedGCBg;
 
280
                                }
 
281
                        }
 
282
                        
 
283
                        if (state == Document.LineState.Changed) {
 
284
                                win.DrawRectangle (lineStateChangedGC, true, x , y, 4, editor.LineHeight);
 
285
                                win.DrawRectangle (bgGC, true, x + 3 , y, Width  - 3, editor.LineHeight);
 
286
                        } else if (state == Document.LineState.Dirty) {
 
287
                                win.DrawRectangle (lineStateDirtyGC, true, x , y, 4, editor.LineHeight);
 
288
                                win.DrawRectangle (bgGC, true, x + 3 , y, Width - 3, editor.LineHeight);
 
289
                        } else {
 
290
                                win.DrawRectangle (bgGC, true, drawArea);
 
291
                        }
 
292
                        
 
293
                        if (line < editor.Document.LineCount) {
226
294
                        
227
295
                                int foldSegmentYPos = y + (editor.LineHeight - foldSegmentSize) / 2;
228
296
                                int xPos = x + Width / 2;
229
297
                                
230
 
                                if (isFoldStart) {
231
 
                                        bool isVisible         = true;
232
 
                                        bool moreLinedOpenFold = false;
233
 
                                        foreach (FoldSegment foldSegment in startFoldings) {
234
 
                                                if (foldSegment.IsFolded) {
235
 
                                                        isVisible = false;
236
 
                                                } else {
237
 
                                                        moreLinedOpenFold = foldSegment.EndLine.Offset > foldSegment.StartLine.Offset;
238
 
                                                }
239
 
                                        }
240
 
                                        bool isFoldEndFromUpperFold = false;
241
 
                                        foreach (FoldSegment foldSegment in endFoldings) {
242
 
                                                if (foldSegment.EndLine.Offset > foldSegment.StartLine.Offset && !foldSegment.IsFolded) 
243
 
                                                        isFoldEndFromUpperFold = true;
 
298
                                if (isFoldStart) {
 
299
                                        bool isVisible         = true;
 
300
                                        bool moreLinedOpenFold = false;
 
301
                                        foreach (FoldSegment foldSegment in startFoldings) {
 
302
                                                if (foldSegment.IsFolded) {
 
303
                                                        isVisible = false;
 
304
                                                } else {
 
305
                                                        moreLinedOpenFold = foldSegment.EndLine.Offset > foldSegment.StartLine.Offset;
 
306
                                                }
 
307
                                        }
 
308
                                        bool isFoldEndFromUpperFold = false;
 
309
                                        foreach (FoldSegment foldSegment in endFoldings) {
 
310
                                                if (foldSegment.EndLine.Offset > foldSegment.StartLine.Offset && !foldSegment.IsFolded) 
 
311
                                                        isFoldEndFromUpperFold = true;
244
312
                                        }
245
313
                                        DrawFoldSegment (win, x, y, isVisible, isStartSelected);
246
314
                                        if (isContaining || isFoldEndFromUpperFold) 
247
 
                                                win.DrawLine (isContainingSelected ? foldLineHighlightedGC : foldLineGC, xPos, drawArea.Top, xPos, foldSegmentYPos - 1);
248
 
                                        if (isContaining || moreLinedOpenFold) 
 
315
                                                win.DrawLine (isContainingSelected ? foldLineHighlightedGC : foldLineGC, xPos, drawArea.Top, xPos, foldSegmentYPos - 1);
 
316
                                        if (isContaining || moreLinedOpenFold) 
249
317
                                                win.DrawLine (isEndSelected || (isStartSelected && isVisible) || isContainingSelected ? foldLineHighlightedGC : foldLineGC, xPos, foldSegmentYPos + foldSegmentSize + 1, xPos, drawArea.Bottom);
250
318
                                } else {
251
319
                                        if (isFoldEnd) {
253
321
                                                win.DrawLine (isEndSelected ? foldLineHighlightedGC : foldLineGC, xPos, yMid, xPos + foldSegmentSize / 2, yMid);
254
322
                                                win.DrawLine (isContainingSelected || isEndSelected ? foldLineHighlightedGC : foldLineGC, xPos, drawArea.Top, xPos, yMid);
255
323
                                                if (isContaining) 
256
 
                                                        win.DrawLine (isContainingSelected ? foldLineHighlightedGC : foldLineGC, xPos, yMid + 1, xPos, drawArea.Bottom);
 
324
                                                        win.DrawLine (isContainingSelected ? foldLineHighlightedGC : foldLineGC, xPos, yMid + 1, xPos, drawArea.Bottom);
257
325
                                        } else if (isContaining) {
258
326
                                                win.DrawLine (isContainingSelected ? foldLineHighlightedGC : foldLineGC, xPos, drawArea.Top, xPos, drawArea.Bottom);
259
327
                                        }