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

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.FindInFiles/SearchResultWidget.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
 
27
27
using System;
28
28
using System.Linq;
 
29
 
 
30
using Gdk;
 
31
 
29
32
using Gtk;
30
33
using Mono.TextEditor;
31
 
using MonoDevelop.Ide.Gui;
 
34
 
32
35
using Mono.TextEditor.Highlighting;
33
36
using System.Collections.Generic;
34
37
using MonoDevelop.Core;
43
46
namespace MonoDevelop.Ide.FindInFiles
44
47
{
45
48
        [System.ComponentModel.ToolboxItem(true)]
46
 
        public partial class SearchResultWidget : Gtk.Bin, ILocationList
 
49
        partial class SearchResultWidget : Bin, ILocationList
47
50
        {
 
51
 
48
52
                ListStore store;
49
 
                ToolButton buttonStop;
50
 
                ToggleToolButton buttonPin;
 
53
 
 
54
                readonly ToolButton buttonStop;
 
55
 
 
56
                readonly ToggleToolButton buttonPin;
51
57
                
52
58
                const int SearchResultColumn = 0;
53
59
                const int DidReadColumn      = 1;
72
78
                
73
79
                public SearchResultWidget ()
74
80
                {
75
 
                        this.Build ();
 
81
                        Build ();
76
82
                        
77
83
                        store = new ListStore (typeof (SearchResult), 
78
84
                                               typeof (bool)          // didRead
84
90
                        treeviewSearchResults.ButtonPressEvent += HandleButtonPressEvent;
85
91
                        treeviewSearchResults.RulesHint = true;
86
92
                        
87
 
                        TreeViewColumn fileNameColumn = new TreeViewColumn ();
88
 
                        fileNameColumn.Resizable = true;
89
 
                        fileNameColumn.SortColumnId  = 0;
90
 
                        fileNameColumn.Title = GettextCatalog.GetString ("File");
 
93
                        var fileNameColumn = new TreeViewColumn {
 
94
                                Resizable = false,
 
95
                                SortColumnId = 0,
 
96
                                Title = GettextCatalog.GetString("File")
 
97
                        };
 
98
 
 
99
                        fileNameColumn.FixedWidth = 200;
 
100
 
91
101
                        var fileNamePixbufRenderer = new CellRendererPixbuf ();
92
102
                        fileNameColumn.PackStart (fileNamePixbufRenderer, false);
93
 
                        fileNameColumn.SetCellDataFunc (fileNamePixbufRenderer, new Gtk.TreeCellDataFunc (FileIconDataFunc));
 
103
                        fileNameColumn.SetCellDataFunc (fileNamePixbufRenderer, FileIconDataFunc);
94
104
                        
95
 
                        CellRendererText fileNameRenderer = new CellRendererText ();
 
105
                        var fileNameRenderer = new CellRendererText ();
96
106
                        fileNameColumn.PackStart (fileNameRenderer, true);
97
 
                        fileNameColumn.SetCellDataFunc (fileNameRenderer, new Gtk.TreeCellDataFunc (FileNameDataFunc));
 
107
                        fileNameColumn.SetCellDataFunc (fileNameRenderer, FileNameDataFunc);
98
108
                        treeviewSearchResults.AppendColumn (fileNameColumn);
99
109
                        
100
 
                        TreeViewColumn lineColumn = treeviewSearchResults.AppendColumn (GettextCatalog.GetString ("Line"), new Gtk.CellRendererText (), new Gtk.TreeCellDataFunc (ResultLineDataFunc));
101
 
                        lineColumn.SortColumnId = 1;
 
110
//                      TreeViewColumn lineColumn = treeviewSearchResults.AppendColumn (GettextCatalog.GetString ("Line"), new CellRendererText (), ResultLineDataFunc);
 
111
//                      lineColumn.SortColumnId = 1;
 
112
//                      lineColumn.FixedWidth = 50;
 
113
//                      
102
114
                        
103
 
                        TreeViewColumn textColumn = treeviewSearchResults.AppendColumn (GettextCatalog.GetString ("Text"), new Gtk.CellRendererText (), new Gtk.TreeCellDataFunc (ResultTextDataFunc));
 
115
                        TreeViewColumn textColumn = treeviewSearchResults.AppendColumn (GettextCatalog.GetString ("Text"), new CellRendererText (), ResultTextDataFunc);
104
116
                        textColumn.SortColumnId = 2;
105
 
                        textColumn.Resizable = true;
 
117
                        textColumn.Resizable = false;
 
118
                        textColumn.FixedWidth = 300;
 
119
 
106
120
                        
107
 
                        TreeViewColumn pathColumn = treeviewSearchResults.AppendColumn (GettextCatalog.GetString ("Path"), new Gtk.CellRendererText (), new Gtk.TreeCellDataFunc (ResultPathDataFunc));
 
121
                        TreeViewColumn pathColumn = treeviewSearchResults.AppendColumn (GettextCatalog.GetString ("Path"), new CellRendererText (), ResultPathDataFunc);
108
122
                        pathColumn.SortColumnId = 3;
109
 
                        pathColumn.Resizable = true;
110
 
                        store.SetSortFunc (0, new TreeIterCompareFunc (CompareFileNames));
111
 
                        store.SetSortFunc (1, new TreeIterCompareFunc (CompareLineNumbers));
112
 
                        store.SetSortFunc (3, new TreeIterCompareFunc (CompareFilePaths));
 
123
                        pathColumn.Resizable = false;
 
124
                        pathColumn.FixedWidth = 500;
 
125
 
113
126
                        
 
127
                        store.SetSortFunc (0, CompareFileNames);
 
128
//                      store.SetSortFunc (1, CompareLineNumbers);
 
129
                        store.SetSortFunc (3, CompareFilePaths);
 
130
 
114
131
                        treeviewSearchResults.RowActivated += TreeviewSearchResultsRowActivated;
115
132
                        
116
 
                        buttonStop = new ToolButton ("gtk-stop");
117
 
                        buttonStop.Sensitive = false;
 
133
                        buttonStop = new ToolButton (Stock.Stop) { Sensitive = false };
 
134
 
118
135
                        buttonStop.Clicked += ButtonStopClicked;
119
136
                        
120
137
                        buttonStop.TooltipText = GettextCatalog.GetString ("Stop");
121
138
                        toolbar.Insert (buttonStop, -1);
122
139
 
123
 
                        ToolButton buttonClear = new ToolButton ("gtk-clear");
 
140
                        var buttonClear = new ToolButton (Gtk.Stock.Clear);
124
141
                        buttonClear.Clicked += ButtonClearClicked;
125
142
                        buttonClear.TooltipText = GettextCatalog.GetString ("Clear results");
126
143
                        toolbar.Insert (buttonClear, -1);
127
144
                        
128
 
                        ToggleToolButton buttonOutput = new ToggleToolButton (MonoDevelop.Ide.Gui.Stock.OutputIcon);
 
145
                        var buttonOutput = new ToggleToolButton (Gui.Stock.OutputIcon);
129
146
                        buttonOutput.Clicked += ButtonOutputClicked;
130
147
                        buttonOutput.TooltipText = GettextCatalog.GetString ("Show output");
131
148
                        toolbar.Insert (buttonOutput, -1);
132
149
                        
133
 
                        buttonPin = new ToggleToolButton ("md-pin-up");
 
150
                        buttonPin = new ToggleToolButton (Gui.Stock.PinUp);
134
151
                        buttonPin.Clicked += ButtonPinClicked;
135
152
                        buttonPin.TooltipText = GettextCatalog.GetString ("Pin results pad");
136
153
                        toolbar.Insert (buttonPin, -1);
144
161
                protected override void OnRealized ()
145
162
                {
146
163
                        base.OnRealized ();
147
 
                        highlightStyle = SyntaxModeService.GetColorStyle (this.Style, PropertyService.Get ("ColorScheme", "Default"));
 
164
                        highlightStyle = SyntaxModeService.GetColorStyle (Style, PropertyService.Get ("ColorScheme", "Default"));
148
165
                }
149
166
                
150
 
                protected override void OnStyleSet (Gtk.Style previous_style)
 
167
                protected override void OnStyleSet (Gtk.Style previousStyle)
151
168
                {
152
 
                        base.OnStyleSet (previous_style);
 
169
                        base.OnStyleSet (previousStyle);
153
170
                        if (highlightStyle != null)
154
171
                                highlightStyle.UpdateFromGtkStyle (Style);
155
172
                }
156
173
 
157
174
                void ButtonPinClicked (object sender, EventArgs e)
158
175
                {
159
 
                        buttonPin.StockId = buttonPin.Active ? "md-pin-down" : "md-pin-up";
 
176
                        buttonPin.StockId = buttonPin.Active? Gui.Stock.PinDown : Gui.Stock.PinUp;
160
177
                }
161
178
 
162
179
                void ButtonOutputClicked (object sender, EventArgs e)
170
187
 
171
188
                void ButtonClearClicked (object sender, EventArgs e)
172
189
                {
173
 
                        this.Reset ();
 
190
                        Reset ();
174
191
                }
175
192
 
176
193
                void ButtonStopClicked (object sender, EventArgs e)
187
204
                public void BeginProgress ()
188
205
                {
189
206
                        IdeApp.Workbench.ActiveLocationList = this;
190
 
                        
 
207
                        newStore = new ListStore (typeof (SearchResult), typeof (bool));
191
208
                        Reset ();
192
209
                        buttonStop.Sensitive = true;
193
210
                        treeviewSearchResults.FreezeChildNotify ();
194
 
                        store.SetSortFunc (0, new TreeIterCompareFunc (DefaultSortFunc));
195
 
                        store.SetSortFunc (1, new TreeIterCompareFunc (DefaultSortFunc));
196
 
                        store.SetSortFunc (3, new TreeIterCompareFunc (DefaultSortFunc));
197
211
                }
198
212
                
 
213
                ListStore newStore;
 
214
 
199
215
                public void EndProgress ()
200
216
                {
201
217
                        buttonStop.Sensitive = false;
202
 
                        store.SetSortFunc (0, new TreeIterCompareFunc (CompareFileNames));
203
 
                        store.SetSortFunc (1, new TreeIterCompareFunc (CompareLineNumbers));
204
 
                        store.SetSortFunc (3, new TreeIterCompareFunc (CompareFilePaths));
 
218
                        newStore.SetSortFunc (0, CompareFileNames);
 
219
                        newStore.SetSortFunc (1, CompareLineNumbers);
 
220
                        newStore.SetSortFunc (3, CompareFilePaths);
 
221
 
 
222
                        treeviewSearchResults.Model = newStore;
 
223
 
 
224
                        store.Dispose ();
 
225
                        store = newStore;
 
226
 
205
227
                        treeviewSearchResults.ThawChildNotify ();
206
 
                        
207
228
                }
 
229
 
208
230
                public void FocusPad ()
209
231
                {
210
232
                        treeviewSearchResults.GrabFocus ();
211
 
                        Gtk.TreeIter iter;
 
233
                        TreeIter iter;
212
234
                        if (store.GetIterFirst (out iter)) 
213
235
                                treeviewSearchResults.Selection.SelectIter (iter);
214
236
                }
237
259
                                args.RetVal = treeviewSearchResults.Selection.GetSelectedRows ().Length > 1;
238
260
                        }
239
261
                }
240
 
                
241
 
                Gdk.Color AdjustColor (Gdk.Color baseColor, Gdk.Color color)
 
262
 
 
263
 
 
264
 
 
265
                static Color AdjustColor (Color baseColor, Color color)
242
266
                {
243
267
                        double b1 = HslColor.Brightness (color);
244
268
                        double b2 = HslColor.Brightness (baseColor);
257
281
                
258
282
                string AdjustColors (string markup)
259
283
                {
260
 
                        StringBuilder result = new StringBuilder ();
 
284
                        var result = new StringBuilder ();
261
285
                        int idx = markup.IndexOf ("foreground=\"");
262
286
                        int offset = 0;
263
 
                        Gdk.Color baseColor;
 
287
 
 
288
 
 
289
 
264
290
                        // This is a workaround for Bug 559804 - Strings in search result pad are near-invisible
265
291
                        // On mac it's not possible to get the white background color with the Base or Background
266
292
                        //Ā methods. If this bug is fixed or a better work around is found - remove this hack.
267
 
                        if (Platform.IsMac)Ā {
268
 
                                baseColor =Ā treeviewSearchResults.Style.Light (treeviewSearchResults.State);
269
 
                        } else {
270
 
                                baseColor =Ā treeviewSearchResults.Style.Base (treeviewSearchResults.State);
271
 
                        }
 
293
                        Color baseColor = Platform.IsMac ?treeviewSearchResults.Style.Light (treeviewSearchResults.State) : treeviewSearchResults.Style.Base (treeviewSearchResults.State);
272
294
                        
273
295
                        while (idx > 0) {
274
296
                                idx += "foreground=\"".Length;
280
302
                                offset = idx + 7;
281
303
                                string colorStr = markup.Substring (idx, 7);
282
304
                                
283
 
                                Gdk.Color color = Gdk.Color.Zero;
284
 
                                if (Gdk.Color.Parse (colorStr, ref color)) {
285
 
                                        colorStr = SyntaxMode.ColorToPangoMarkup (AdjustColor (baseColor, color));
286
 
                                }
 
305
                                Color color = Color.Zero;
 
306
 
 
307
                                if (Color.Parse(colorStr, ref color))
 
308
 
 
309
                                        colorStr = SyntaxMode.ColorToPangoMarkup(AdjustColor(baseColor, color));
 
310
 
287
311
                                result.Append (colorStr);
288
312
                                idx = markup.IndexOf ("foreground=\"", idx);
289
313
                        }
293
317
                
294
318
                void OnPopupMenu (object sender, PopupMenuArgs args)
295
319
                {
296
 
                        CommandEntrySet contextMenu = new CommandEntrySet ();
 
320
                        var contextMenu = new CommandEntrySet ();
297
321
                        contextMenu.AddItem (ViewCommands.Open);
298
322
                        contextMenu.AddItem (EditCommands.Copy);
299
323
                        contextMenu.AddItem (EditCommands.SelectAll);
305
329
                        labelStatus.Text = text;
306
330
                }
307
331
                
308
 
                void FileIconDataFunc (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
 
332
                void FileIconDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
309
333
                {
310
334
                        if (TreeIter.Zero.Equals (iter))
311
335
                                return;
312
 
                        CellRendererPixbuf fileNamePixbufRenderer = (CellRendererPixbuf) cell;
313
 
                        SearchResult searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
 
336
                        var fileNamePixbufRenderer = (CellRendererPixbuf) cell;
 
337
                        var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
314
338
                        if (searchResult == null)
315
339
                                return;
316
 
                        fileNamePixbufRenderer.Pixbuf = DesktopService.GetPixbufForFile (searchResult.FileName, Gtk.IconSize.Menu);
 
340
                        fileNamePixbufRenderer.Pixbuf = DesktopService.GetPixbufForFile (searchResult.FileName, IconSize.Menu);
317
341
                }
318
 
                
319
 
                string MarkupText (string text, bool didRead, bool isSelected)
 
342
 
 
343
 
 
344
 
 
345
                static string MarkupText (string text, bool didRead)
320
346
                {
321
347
                        return string.Format ("<span weight=\"{1}\">{0}</span>", GLib.Markup.EscapeText (text), didRead ? "normal" : "bold");
322
348
                }
323
349
                
324
 
                void FileNameDataFunc (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
 
350
                void FileNameDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
325
351
                {
326
352
                        if (TreeIter.Zero.Equals (iter))
327
353
                                return;
328
 
                        CellRendererText fileNameRenderer = (CellRendererText)cell;
 
354
                        var fileNameRenderer = (CellRendererText)cell;
329
355
                        bool didRead = (bool)store.GetValue (iter, DidReadColumn);
330
 
                        SearchResult searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
 
356
                        var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
331
357
                        if (searchResult == null)
332
358
                                return;
333
 
                        bool isSelected = treeviewSearchResults.Selection.IterIsSelected (iter);
334
 
                        
335
 
                        fileNameRenderer.Markup = MarkupText (System.IO.Path.GetFileName (searchResult.FileName), didRead, isSelected);
 
359
                        Document doc = GetDocument (searchResult);
 
360
                        if (doc == null)
 
361
                                return;
 
362
                        int lineNr = doc.OffsetToLineNumber (searchResult.Offset);
 
363
                        fileNameRenderer.Markup = MarkupText (System.IO.Path.GetFileName (searchResult.FileName) + ":" + lineNr, didRead);
336
364
                }
337
365
                
338
366
                int CompareLineNumbers (TreeModel model, TreeIter first, TreeIter second)
339
367
                {
340
 
                        DocumentLocation loc1 = GetLocation ((SearchResult)model.GetValue (first, SearchResultColumn));
341
 
                        DocumentLocation loc2 = GetLocation ((SearchResult)model.GetValue (second, SearchResultColumn));
 
368
                        var loc1 = GetLocation ((SearchResult)model.GetValue (first, SearchResultColumn));
 
369
                        var loc2 = GetLocation ((SearchResult)model.GetValue (second, SearchResultColumn));
342
370
                        return loc1.Line.CompareTo (loc2.Line);
343
371
                }
344
372
                
349
377
                
350
378
                static int CompareFileNames (TreeModel model, TreeIter first, TreeIter second)
351
379
                {
352
 
                        SearchResult searchResult1 = (SearchResult)model.GetValue (first, SearchResultColumn);
353
 
                        SearchResult searchResult2 = (SearchResult)model.GetValue (second, SearchResultColumn);
 
380
                        var searchResult1 = (SearchResult)model.GetValue (first, SearchResultColumn);
 
381
                        var searchResult2 = (SearchResult)model.GetValue (second, SearchResultColumn);
354
382
                        if (searchResult1 == null || searchResult2 == null || searchResult1.FileName == null || searchResult2.FileName == null)
355
383
                                return -1;
356
384
                        return System.IO.Path.GetFileName (searchResult1.FileName).CompareTo (System.IO.Path.GetFileName (searchResult2.FileName));
358
386
                
359
387
                static int CompareFilePaths (TreeModel model, TreeIter first, TreeIter second)
360
388
                {
361
 
                        SearchResult searchResult1 = (SearchResult)model.GetValue (first, SearchResultColumn);
362
 
                        SearchResult searchResult2 = (SearchResult)model.GetValue (second, SearchResultColumn);
 
389
                        var searchResult1 = (SearchResult)model.GetValue (first, SearchResultColumn);
 
390
                        var searchResult2 = (SearchResult)model.GetValue (second, SearchResultColumn);
363
391
                        if (searchResult1 == null || searchResult2 == null || searchResult1.FileName == null || searchResult2.FileName == null)
364
392
                                return -1;
 
393
 
365
394
                        return System.IO.Path.GetDirectoryName (searchResult1.FileName).CompareTo (System.IO.Path.GetDirectoryName (searchResult2.FileName));
366
 
                }
367
 
                
368
 
                void ResultPathDataFunc (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
369
 
                {
370
 
                        if (TreeIter.Zero.Equals (iter))
371
 
                                return;
372
 
                        CellRendererText pathRenderer = (CellRendererText)cell;
373
 
                        SearchResult searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
374
 
                        if (searchResult == null)
375
 
                                return;
376
 
                        bool didRead = (bool)store.GetValue (iter, DidReadColumn);
377
 
                        bool isSelected = treeviewSearchResults.Selection.IterIsSelected (iter);
378
 
                        pathRenderer.Markup = MarkupText (System.IO.Path.GetDirectoryName (searchResult.FileName), didRead, isSelected);
379
 
                }
380
 
                
381
 
                void ResultLineDataFunc (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
382
 
                {
383
 
                        if (TreeIter.Zero.Equals (iter))
384
 
                                return;
385
 
                        CellRendererText lineRenderer = (CellRendererText)cell;
386
 
                        SearchResult searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
387
 
                        if (searchResult == null)
388
 
                                return;
389
 
                        
390
 
                        Mono.TextEditor.Document doc = GetDocument (searchResult);
391
 
                        int lineNr = doc.OffsetToLineNumber (searchResult.Offset) + 1;
392
 
                        bool didRead = (bool)store.GetValue (iter, DidReadColumn);
393
 
                        bool isSelected = treeviewSearchResults.Selection.IterIsSelected (iter);
394
 
                        lineRenderer.Markup = MarkupText (lineNr.ToString (), didRead, isSelected);
395
 
                }
396
 
                
397
 
                void ResultTextDataFunc (Gtk.TreeViewColumn column, Gtk.CellRenderer cell, Gtk.TreeModel model, Gtk.TreeIter iter)
398
 
                {
399
 
                        if (TreeIter.Zero.Equals (iter))
400
 
                                return;
401
 
                        CellRendererText textRenderer = (CellRendererText)cell;
402
 
                        SearchResult searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
403
 
                        if (searchResult == null)
404
 
                                return;
405
 
                        
406
 
                        Mono.TextEditor.Document doc = GetDocument (searchResult);
 
395
 
 
396
                }
 
397
                
 
398
                void ResultPathDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
 
399
                {
 
400
                        if (TreeIter.Zero.Equals (iter))
 
401
                                return;
 
402
                        var pathRenderer = (CellRendererText)cell;
 
403
                        var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
 
404
                        if (searchResult == null)
 
405
                                return;
 
406
                        bool didRead = (bool)store.GetValue (iter, DidReadColumn);
 
407
                        pathRenderer.Markup = MarkupText (System.IO.Path.GetDirectoryName (searchResult.FileName), didRead);
 
408
                }
 
409
                
 
410
//              void ResultLineDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
 
411
//              {
 
412
//                      if (TreeIter.Zero.Equals (iter))
 
413
//                              return;
 
414
//                      var lineRenderer = (CellRendererText)cell;
 
415
//                      var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
 
416
//                      if (searchResult == null)
 
417
//                              return;
 
418
//                      
 
419
//                      Document doc = GetDocument (searchResult);
 
420
//                      int lineNr = doc.OffsetToLineNumber (searchResult.Offset) + 1;
 
421
//                      bool didRead = (bool)store.GetValue (iter, DidReadColumn);
 
422
//                      lineRenderer.Markup = MarkupText (lineNr.ToString (), didRead);
 
423
//              }
 
424
//              
 
425
                void ResultTextDataFunc (TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
 
426
                {
 
427
                        if (TreeIter.Zero.Equals (iter))
 
428
                                return;
 
429
                        var textRenderer = (CellRendererText)cell;
 
430
                        var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
 
431
                        if (searchResult == null || searchResult.Offset < 0) {
 
432
                                textRenderer.Markup = "Invalid search result";
 
433
                                return;
 
434
                        }
 
435
                        
 
436
                        Document doc = GetDocument (searchResult);
 
437
                        if (doc == null) {
 
438
                                textRenderer.Markup = "Can't create document for:" + searchResult.FileName;
 
439
                                return;
 
440
                        }
407
441
                        int lineNr = doc.OffsetToLineNumber (searchResult.Offset);
408
442
                        LineSegment line = doc.GetLine (lineNr);
 
443
                        if (line == null) {
 
444
                                textRenderer.Markup = "Invalid line number " + lineNr + " from offset: " + searchResult.Offset;
 
445
                                return;
 
446
                        }
409
447
                        bool isSelected = treeviewSearchResults.Selection.IterIsSelected (iter);
410
 
                        
411
 
                        string markup;
412
 
                        if (doc.SyntaxMode != null) {
413
 
                                markup = doc.SyntaxMode.GetMarkup (doc, new TextEditorOptions (), highlightStyle, line.Offset, line.EditableLength, true, !isSelected, false);
414
 
                        } else {
415
 
                                markup = GLib.Markup.EscapeText (doc.GetTextAt (line.Offset, line.EditableLength));
416
 
                        }
 
448
                        int indent = line.GetIndentation (doc).Length;
 
449
                        string markup = doc.SyntaxMode != null ? 
 
450
                                doc.SyntaxMode.GetMarkup (doc, new TextEditorOptions (), highlightStyle, line.Offset + indent, line.EditableLength - indent, true, !isSelected, false) : 
 
451
                                GLib.Markup.EscapeText (doc.GetTextAt (line.Offset, line.EditableLength));
417
452
                        
418
453
                        if (!isSelected) {
419
 
                                int col = searchResult.Offset - line.Offset;
 
454
                                int col = searchResult.Offset - line.Offset - indent;
420
455
                                string tag;
421
456
                                int pos1 = FindPosition (markup, col, out tag);
422
457
                                int pos2 = FindPosition (markup, col + searchResult.Length, out tag);
423
458
                                if (pos1 >= 0 && pos2 >= 0) {
424
 
                                        if (tag.StartsWith ("span")) {
425
 
                                                markup = markup.Insert (pos2, "</span></span><" + tag + ">");
426
 
                                        } else {
427
 
                                                markup = markup.Insert (pos2, "</span>");
428
 
                                        }
429
 
                                        Gdk.Color searchColor = highlightStyle.SearchTextBg;
 
459
                                        markup = tag.StartsWith ("span") ? markup.Insert (pos2, "</span></span><" + tag + ">") : markup.Insert (pos2, "</span>");
 
460
                                        Color searchColor = Mono.TextEditor.Highlighting.Style.ToGdkColor (highlightStyle.SearchTextBg);
430
461
                                        double b1 = HslColor.Brightness (searchColor);
431
462
                                        double b2 = HslColor.Brightness (AdjustColor (Style.Base (StateType.Normal), highlightStyle.Default.Color));
432
463
                                        double delta = Math.Abs (b1 - b2);
455
486
                        bool inTag = false;
456
487
                        bool inChar = false;
457
488
                        int realPos = 0;
458
 
                        StringBuilder lastTag = new StringBuilder ();
 
489
                        var lastTag = new StringBuilder ();
459
490
                        for (int i = 0; i < markup.Length; i++) {
460
491
                                char ch = markup[i];
461
492
                                if (ch != '<' && ch != '&' && !inTag && !inChar && realPos >= pos) {
491
522
                                return markup.Length;
492
523
                        return -1;
493
524
                }
494
 
                
495
 
                Dictionary<string, Mono.TextEditor.Document> documents = new Dictionary<string, Mono.TextEditor.Document> ();
496
 
                
497
 
                Mono.TextEditor.Document GetDocument (SearchResult result)
 
525
 
 
526
 
 
527
 
 
528
                readonly Dictionary<string, Document> documents = new Dictionary<string, Document> ();
 
529
                
 
530
                Document GetDocument (SearchResult result)
498
531
                {
499
 
                        Mono.TextEditor.Document doc;
 
532
                        Document doc;
500
533
                        if (!documents.TryGetValue (result.FileName, out doc)) {
501
 
                                doc = new Mono.TextEditor.Document ();
 
534
                                TextReader reader = result.FileProvider.Open ();
 
535
                                if (reader == null)
 
536
                                        return null;
 
537
                                        doc = Document.CreateImmutableDocument (reader.ReadToEnd ());
502
538
                                doc.MimeType = DesktopService.GetMimeTypeForUri (result.FileName);
503
 
                                TextReader reader = result.FileProvider.Open ();
504
 
                                doc.Text = reader.ReadToEnd ();
 
539
                                
505
540
                                reader.Close ();
506
 
                                documents[result.FileName] = doc;
 
541
                                documents [result.FileName] = doc;
507
542
                        }
508
543
                        return doc;
509
544
                }
523
558
                
524
559
                public void Add (SearchResult result)
525
560
                {
526
 
                        store.InsertWithValues (ResultCount, result, false);
527
 
                        ResultCount++;
528
 
                }
529
 
                
530
 
                void OpenDocumentAt (Gtk.TreeIter iter)
531
 
                {
532
 
                        SearchResult result = store.GetValue (iter, SearchResultColumn) as SearchResult;
 
561
                        newStore.InsertWithValues (ResultCount++, result, false);
 
562
                }
 
563
                
 
564
                public void AddRange (IEnumerable<SearchResult> results)
 
565
                {
 
566
                        foreach (var result in results) {
 
567
                                Add (result);
 
568
                        }
 
569
                }
 
570
                
 
571
                void OpenDocumentAt (TreeIter iter)
 
572
                {
 
573
                        var result = store.GetValue (iter, SearchResultColumn) as SearchResult;
533
574
                        if (result != null) {
534
575
                                DocumentLocation loc = GetLocation (result);
535
576
                                store.SetValue (iter, DidReadColumn, true);
536
 
                                IdeApp.Workbench.OpenDocument (result.FileName, loc.Line, loc.Column, true);
 
577
                                IdeApp.Workbench.OpenDocument (result.FileName, loc.Line, loc.Column);
537
578
                        }
538
579
                }
 
580
                
539
581
                DocumentLocation GetLocation (SearchResult searchResult)
540
582
                {
541
 
                        Mono.TextEditor.Document doc = GetDocument (searchResult);
 
583
                        Document doc = GetDocument (searchResult);
 
584
                        if (doc == null)
 
585
                                return DocumentLocation.Empty;
542
586
                        int lineNr = doc.OffsetToLineNumber (searchResult.Offset);
543
587
                        LineSegment line = doc.GetLine (lineNr);
544
 
                        return new DocumentLocation (lineNr + 1, searchResult.Offset - line.Offset + 1);
 
588
                        return new DocumentLocation (lineNr, searchResult.Offset - line.Offset + 1);
545
589
                }
546
590
                
547
591
                public void OpenSelectedMatches ()
548
592
                {
549
 
                        foreach (Gtk.TreePath path in treeviewSearchResults.Selection.GetSelectedRows ()) {
550
 
                                Gtk.TreeIter iter;
 
593
                        foreach (TreePath path in treeviewSearchResults.Selection.GetSelectedRows ()) {
 
594
                                TreeIter iter;
551
595
                                if (!store.GetIter (out iter, path))
552
596
                                        continue;
553
597
                                OpenDocumentAt (iter);
563
607
                public void CopySelection ()
564
608
                {
565
609
                        TreeModel model;
566
 
                        StringBuilder sb = new StringBuilder ();
567
 
                        foreach (Gtk.TreePath p in treeviewSearchResults.Selection.GetSelectedRows (out model)) {
 
610
                        var sb = new StringBuilder ();
 
611
                        foreach (TreePath p in treeviewSearchResults.Selection.GetSelectedRows (out model)) {
568
612
                                TreeIter iter;
569
613
                                if (!model.GetIter (out iter, p))
570
614
                                        continue;
571
 
                                SearchResult result = store.GetValue (iter, SearchResultColumn) as SearchResult;
 
615
                                var result = store.GetValue (iter, SearchResultColumn) as SearchResult;
572
616
                                if (result == null)
573
617
                                        continue;
574
618
                                DocumentLocation loc = GetLocation (result);
575
 
                                Mono.TextEditor.Document doc = GetDocument (result);
576
 
                                LineSegment line = doc.GetLine (loc.Line - 1);
 
619
                                Document doc = GetDocument (result);
 
620
                                if (doc == null)
 
621
                                        continue;
 
622
                                LineSegment line = doc.GetLine (loc.Line);
577
623
                                
578
624
                                sb.AppendFormat ("{0} ({1}, {2}):{3}", result.FileName, loc.Line, loc.Column, doc.GetTextAt (line.Offset, line.EditableLength));
579
625
                                sb.AppendLine ();
580
626
                        }
581
 
                        Gtk.Clipboard clipboard = Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));
 
627
                        Clipboard clipboard = Clipboard.Get (Atom.Intern ("CLIPBOARD", false));
582
628
                        clipboard.Text = sb.ToString ();
583
629
                        
584
 
                        clipboard = Clipboard.Get (Gdk.Atom.Intern ("PRIMARY", false));
 
630
                        clipboard = Clipboard.Get (Atom.Intern ("PRIMARY", false));
585
631
                        clipboard.Text = sb.ToString ();
586
632
                }
587
633
                
593
639
                
594
640
                public NavigationPoint GetNextLocation ()
595
641
                {
596
 
                        TreeIter iter = TreeIter.Zero;
 
642
                        TreeIter iter;
597
643
                        TreePath[] path = treeviewSearchResults.Selection.GetSelectedRows ();
598
644
                        if (path != null && path.Length > 0 && store.GetIter (out iter, path[0])) {
599
645
                                if (!store.IterNext (ref iter)) 
631
677
 
632
678
                NavigationPoint GetLocation (TreeIter iter)
633
679
                {
634
 
                        this.treeviewSearchResults.Selection.UnselectAll ();
 
680
                        treeviewSearchResults.Selection.UnselectAll ();
635
681
                        if (!store.IterIsValid (iter))
636
682
                                return null;
637
683
 
638
 
                        this.treeviewSearchResults.Selection.SelectIter (iter);
639
 
                        this.treeviewSearchResults.ScrollToCell (store.GetPath (iter), this.treeviewSearchResults.Columns[0], false, 0, 0);
640
 
                        SearchResult searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
641
 
                        Mono.TextEditor.Document doc = GetDocument (searchResult);
 
684
                        treeviewSearchResults.Selection.SelectIter (iter);
 
685
                        treeviewSearchResults.ScrollToCell (store.GetPath (iter), treeviewSearchResults.Columns [0], false, 0, 0);
 
686
                        var searchResult = (SearchResult)store.GetValue (iter, SearchResultColumn);
 
687
                        Document doc = GetDocument (searchResult);
 
688
                        if (doc == null)
 
689
                                return null;
642
690
                        DocumentLocation location = doc.OffsetToLocation (searchResult.Offset);
643
 
                        return new SearchTextFileNavigationPoint (searchResult.FileName, location.Line + 1, location.Column + 1);
 
691
                        return new SearchTextFileNavigationPoint (searchResult.FileName, location.Line, location.Column);
644
692
                }
645
693
                
646
694
                class SearchTextFileNavigationPoint : TextFileNavigationPoint 
649
697
                        {
650
698
                        }
651
699
                        
652
 
                        protected override MonoDevelop.Ide.Gui.Document DoShow ()
 
700
                        protected override Gui.Document DoShow ()
653
701
                        {
654
702
                                var doc = base.DoShow ();
655
 
                                if (doc != null) {
656
 
                                        IEditableTextBuffer buf = doc.GetContent<IEditableTextBuffer> ();
657
 
                                        if (buf != null)
658
 
                                                buf.SetCaretTo (Math.Max (Line, 1), Math.Max (Column, 1));
659
 
                                }
 
703
                                if (doc == null)
 
704
                                        return null;
 
705
                                
 
706
                                var buf = doc.GetContent<IEditableTextBuffer> ();
 
707
                                if (buf != null)
 
708
                                        buf.SetCaretTo (Math.Max (Line, 1), Math.Max (Column, 1));
 
709
                                
660
710
                                return doc;
661
711
                        }
662
712