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

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui.Dialogs/ReplaceDialog.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//  ReplaceDialog.cs
2
 
//
3
 
//  This file was derived from a file from #Develop. 
4
 
//
5
 
//  Copyright (C) 2001-2007 Mike Krüger <mkrueger@novell.com>
6
 
// 
7
 
//  This program is free software; you can redistribute it and/or modify
8
 
//  it under the terms of the GNU General Public License as published by
9
 
//  the Free Software Foundation; either version 2 of the License, or
10
 
//  (at your option) any later version.
11
 
// 
12
 
//  This program is distributed in the hope that it will be useful,
13
 
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 
//  GNU General Public License for more details.
16
 
//  
17
 
//  You should have received a copy of the GNU General Public License
18
 
//  along with this program; if not, write to the Free Software
19
 
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
 
 
21
 
using System;
22
 
using System.IO;
23
 
using System.Drawing;
24
 
using System.ComponentModel;
25
 
using System.Collections.Specialized;
26
 
 
27
 
using MonoDevelop.Core.Gui;
28
 
using MonoDevelop.Core;
29
 
using MonoDevelop.Ide.Gui.Search;
30
 
using MonoDevelop.Ide.Gui;
31
 
 
32
 
using Gtk;
33
 
using Glade;
34
 
 
35
 
namespace MonoDevelop.Ide.Gui.Dialogs
36
 
{
37
 
        internal partial class ReplaceDialog: Gtk.Dialog
38
 
        {
39
 
                private const int historyLimit = 20;
40
 
                private const char historySeparator = '\n';
41
 
                // regular members
42
 
                public bool replaceMode;
43
 
                StringCollection findHistory = new StringCollection();
44
 
                StringCollection replaceHistory = new StringCollection();
45
 
                
46
 
                // services
47
 
 
48
 
                void InitDialog ()
49
 
                {
50
 
                        findButton.UseUnderline = true;                 
51
 
                        closeButton.UseUnderline = true;                        
52
 
                        
53
 
                        //set up the size groups
54
 
                        SizeGroup labels = new SizeGroup(SizeGroupMode.Horizontal);
55
 
                        SizeGroup combos = new SizeGroup(SizeGroupMode.Horizontal);
56
 
                        SizeGroup options = new SizeGroup(SizeGroupMode.Horizontal);
57
 
                        SizeGroup helpButtons = new SizeGroup(SizeGroupMode.Horizontal);
58
 
                        SizeGroup checkButtons = new SizeGroup(SizeGroupMode.Horizontal);
59
 
                        labels.AddWidget(label1);                       
60
 
                        combos.AddWidget(searchPatternEntry);
61
 
                        helpButtons.AddWidget(findHelpButton);
62
 
                        checkButtons.AddWidget(ignoreCaseCheckBox);
63
 
                        checkButtons.AddWidget(searchWholeWordOnlyCheckBox);
64
 
                        checkButtons.AddWidget(useSpecialSearchStrategyCheckBox);
65
 
                        checkButtons.AddWidget(searchLocationLabel);
66
 
                        options.AddWidget(specialSearchStrategyComboBox);
67
 
                        options.AddWidget(searchLocationComboBox);
68
 
 
69
 
                        searchPatternEntry.Entry.Completion = new EntryCompletion ();
70
 
                        searchPatternEntry.Entry.Completion.Model = new ListStore (typeof (string));
71
 
                        searchPatternEntry.Entry.Completion.TextColumn = 0;
72
 
                        searchPatternEntry.Entry.ActivatesDefault = true;
73
 
                        
74
 
                        searchPatternEntry.Model = new ListStore(typeof(string));
75
 
                        searchPatternEntry.TextColumn = 0;
76
 
                        
77
 
                        // set button sensitivity
78
 
                        findHelpButton.Sensitive = false;
79
 
                        
80
 
                        // set replace dialog properties 
81
 
                        if (replaceMode)
82
 
                        {
83
 
                                markAllButton.Visible = false;
84
 
                                replacePatternEntry.Entry.Completion = new EntryCompletion ();
85
 
                                replacePatternEntry.Entry.Completion.Model = new ListStore (typeof (string));
86
 
                                replacePatternEntry.Entry.Completion.TextColumn = 0;
87
 
                                replacePatternEntry.Entry.ActivatesDefault = true;
88
 
                                
89
 
                                replacePatternEntry.Model = new ListStore(typeof(string));
90
 
                                replacePatternEntry.TextColumn = 0;
91
 
 
92
 
                                // set the label properties
93
 
                                replaceButton.UseUnderline = true;
94
 
                                replaceAllButton.UseUnderline = true;
95
 
                                
96
 
                                // set te size groups to include the replace dialog
97
 
                                labels.AddWidget(labelReplace);
98
 
                                combos.AddWidget(replacePatternEntry);
99
 
                                helpButtons.AddWidget(replaceHelpButton);
100
 
                                
101
 
                                replaceHelpButton.Sensitive = false;
102
 
                        }
103
 
                        else
104
 
                        {
105
 
                                labelReplace.Visible = replacePatternEntry.Visible = false;
106
 
                                replaceAllButton.Visible = replaceHelpButton.Visible = replaceButton.Visible = false;
107
 
                                markAllButton.UseUnderline = true;
108
 
                        }
109
 
                        this.Resize (500, 200);
110
 
                        TransientFor = IdeApp.Workbench.RootWindow;
111
 
                }
112
 
                
113
 
                public ReplaceDialog (bool replaceMode)
114
 
                {
115
 
                        Build ();
116
 
                        
117
 
                        // some members needed to initialise this dialog based on replace mode
118
 
                        this.replaceMode = replaceMode;
119
 
                        
120
 
                        InitDialog ();
121
 
                        
122
 
                        LoadHistoryValues();
123
 
                        
124
 
                        ignoreCaseCheckBox.Active = !SearchReplaceManager.SearchOptions.IgnoreCase;
125
 
                        searchWholeWordOnlyCheckBox.Active = SearchReplaceManager.SearchOptions.SearchWholeWordOnly;
126
 
                        
127
 
                        useSpecialSearchStrategyCheckBox.Active  = SearchReplaceManager.SearchOptions.SearchStrategyType != SearchStrategyType.Normal;
128
 
                        useSpecialSearchStrategyCheckBox.Toggled += new EventHandler(SpecialSearchStrategyCheckBoxChangedEvent);
129
 
                        
130
 
                        ListStore store = new ListStore (typeof (string));
131
 
                        store.AppendValues (GettextCatalog.GetString ("Wildcards"));
132
 
                        store.AppendValues (GettextCatalog.GetString ("Regular Expressions"));
133
 
                        specialSearchStrategyComboBox.Model = store;
134
 
 
135
 
                        CellRendererText cr = new CellRendererText ();
136
 
                        specialSearchStrategyComboBox.PackStart (cr, true);
137
 
                        specialSearchStrategyComboBox.AddAttribute (cr, "text", 0);
138
 
                        
139
 
                        int index = 0;
140
 
                        switch (SearchReplaceManager.SearchOptions.SearchStrategyType) {
141
 
                                case SearchStrategyType.Normal:
142
 
                                case SearchStrategyType.Wildcard:
143
 
                                        searchWholeWordOnlyCheckBox.Sensitive = true;
144
 
                                        break;
145
 
                                case SearchStrategyType.RegEx:
146
 
                                        searchWholeWordOnlyCheckBox.Sensitive = false;
147
 
                                        index = 1;
148
 
                                        break;
149
 
                        }
150
 
                        specialSearchStrategyComboBox.Active = index;
151
 
                        specialSearchStrategyComboBox.Changed += new EventHandler (OnSpecialSearchStrategyChanged);
152
 
                        
153
 
                        store = new ListStore (typeof (string));
154
 
                        store.AppendValues (GettextCatalog.GetString ("Current File"));
155
 
                        store.AppendValues (GettextCatalog.GetString ("All Open Files"));
156
 
                        
157
 
                        searchLocationComboBox.Model = store;
158
 
                        searchLocationComboBox.PackStart (cr, true);
159
 
                        searchLocationComboBox.AddAttribute (cr, "text", 0);
160
 
                        
161
 
                        index = 0;
162
 
                        if (SearchReplaceManager.SearchOptions.DocumentIteratorType == DocumentIteratorType.AllOpenFiles)
163
 
                                index = 1;
164
 
                        
165
 
                        searchLocationComboBox.Active = index;
166
 
                        
167
 
                        searchPatternEntry.Entry.Text = SearchReplaceManager.SearchOptions.SearchPattern;
168
 
                        
169
 
                        // insert event handlers
170
 
                        findButton.Clicked  += new EventHandler(FindNextEvent);
171
 
                        closeButton.Clicked += new EventHandler(CloseDialogEvent);
172
 
                        Close += new EventHandler(CloseDialogEvent);
173
 
                        DeleteEvent += new DeleteEventHandler (OnDeleted);
174
 
                        
175
 
                        if (replaceMode) {
176
 
                                Title = GettextCatalog.GetString ("Replace");
177
 
                                replaceButton.Clicked    += new EventHandler(ReplaceEvent);
178
 
                                replaceAllButton.Clicked += new EventHandler(ReplaceAllEvent);
179
 
                                replacePatternEntry.Entry.Text = SearchReplaceManager.SearchOptions.ReplacePattern;
180
 
                        } else {
181
 
                                Title = GettextCatalog.GetString ("Find");
182
 
                                markAllButton.Clicked    += new EventHandler(MarkAllEvent);
183
 
                        }
184
 
                        searchPatternEntry.Entry.SelectRegion(0, searchPatternEntry.ActiveText.Length);
185
 
                        
186
 
                        SpecialSearchStrategyCheckBoxChangedEvent(null, null);
187
 
                        SearchReplaceManager.ReplaceDialog     = this;
188
 
                }
189
 
                
190
 
                protected void OnClosed()
191
 
                {
192
 
                        SaveHistoryValues();
193
 
                        SearchReplaceManager.ReplaceDialog = null;
194
 
                }
195
 
                
196
 
                void OnDeleted (object o, DeleteEventArgs args)
197
 
                {
198
 
                        // perform the standard closing windows event
199
 
                        OnClosed();
200
 
                        SearchReplaceManager.ReplaceDialog = null;
201
 
                }
202
 
 
203
 
                public void SetSearchPattern(string pattern)
204
 
                {
205
 
                        searchPatternEntry.Entry.Text = pattern;
206
 
                }
207
 
 
208
 
                void OnSpecialSearchStrategyChanged (object o, EventArgs e)
209
 
                {
210
 
                        if (specialSearchStrategyComboBox.Active != 1) {
211
 
                                searchWholeWordOnlyCheckBox.Sensitive = true;
212
 
                        } else {
213
 
                                searchWholeWordOnlyCheckBox.Sensitive = false;
214
 
                        }
215
 
                }
216
 
                
217
 
                void SetupSearchReplaceManager()
218
 
                {
219
 
                        SearchReplaceManager.SearchOptions.SearchPattern  = searchPatternEntry.ActiveText;
220
 
                        if (replaceMode) {
221
 
                                SearchReplaceManager.SearchOptions.ReplacePattern = replacePatternEntry.ActiveText;
222
 
                        }
223
 
                        
224
 
                        SearchReplaceManager.SearchOptions.IgnoreCase          = !ignoreCaseCheckBox.Active;
225
 
                        SearchReplaceManager.SearchOptions.SearchWholeWordOnly = searchWholeWordOnlyCheckBox.Active;
226
 
                        
227
 
                        if (useSpecialSearchStrategyCheckBox.Active) {
228
 
                                switch (specialSearchStrategyComboBox.Active) {
229
 
                                        case 0:
230
 
                                                SearchReplaceManager.SearchOptions.SearchStrategyType = SearchStrategyType.Wildcard;
231
 
                                                break;
232
 
                                        case 1:
233
 
                                                SearchReplaceManager.SearchOptions.SearchStrategyType = SearchStrategyType.RegEx;
234
 
                                                break;
235
 
                                }
236
 
                        } else {
237
 
                                SearchReplaceManager.SearchOptions.SearchStrategyType = SearchStrategyType.Normal;
238
 
                        }
239
 
                        
240
 
                        switch (searchLocationComboBox.Active) {
241
 
                                case 0:
242
 
                                        SearchReplaceManager.SearchOptions.DocumentIteratorType = DocumentIteratorType.CurrentDocument;
243
 
                                        break;
244
 
                                case 1:
245
 
                                        SearchReplaceManager.SearchOptions.DocumentIteratorType = DocumentIteratorType.AllOpenFiles;
246
 
                                        break;
247
 
                        }
248
 
                }
249
 
                
250
 
                void FindNextEvent(object sender, EventArgs e)
251
 
                {
252
 
                        if (searchPatternEntry.ActiveText.Length == 0)
253
 
                                return;
254
 
                        
255
 
                        SetupSearchReplaceManager();
256
 
                        SearchReplaceManager.FindNext();
257
 
                        
258
 
                        AddSearchHistoryItem(findHistory, searchPatternEntry.ActiveText);
259
 
                }
260
 
                
261
 
                void ReplaceEvent(object sender, EventArgs e)
262
 
                {
263
 
                        if (searchPatternEntry.ActiveText.Length == 0)
264
 
                                return;
265
 
                        
266
 
                        SetupSearchReplaceManager();
267
 
                        SearchReplaceManager.Replace();
268
 
                        
269
 
                        AddSearchHistoryItem(replaceHistory, replacePatternEntry.ActiveText);
270
 
                }
271
 
                
272
 
                void ReplaceAllEvent(object sender, EventArgs e)
273
 
                {
274
 
                        if (searchPatternEntry.ActiveText.Length == 0)
275
 
                                return;
276
 
                        
277
 
                        SetupSearchReplaceManager();
278
 
                        SearchReplaceManager.ReplaceAll();
279
 
                        
280
 
                        AddSearchHistoryItem(replaceHistory, replacePatternEntry.ActiveText);
281
 
                }
282
 
                
283
 
                void MarkAllEvent(object sender, EventArgs e)
284
 
                {
285
 
                        if (searchPatternEntry.ActiveText.Length == 0)
286
 
                                return;
287
 
                        
288
 
                        SetupSearchReplaceManager();
289
 
                        SearchReplaceManager.MarkAll();                 
290
 
                        
291
 
                        AddSearchHistoryItem(findHistory, searchPatternEntry.ActiveText);
292
 
                }
293
 
                
294
 
                void CloseDialogEvent(object sender, EventArgs e)
295
 
                {
296
 
                        Hide();
297
 
                        OnClosed ();
298
 
                }
299
 
                
300
 
                void SpecialSearchStrategyCheckBoxChangedEvent(object sender, EventArgs e)
301
 
                {
302
 
                        if (useSpecialSearchStrategyCheckBox != null) {
303
 
                                specialSearchStrategyComboBox.Sensitive = useSpecialSearchStrategyCheckBox.Active;
304
 
                                if (useSpecialSearchStrategyCheckBox.Active) {
305
 
                                        if (specialSearchStrategyComboBox.Active == 1) {
306
 
                                                searchWholeWordOnlyCheckBox.Sensitive = false;
307
 
                                        }
308
 
                                } else {
309
 
                                        searchWholeWordOnlyCheckBox.Sensitive = true;
310
 
                                }
311
 
                        }
312
 
                }
313
 
                
314
 
                // generic method to add a string to a history item
315
 
                private void AddSearchHistoryItem (StringCollection history, string toAdd)
316
 
                {
317
 
                        // add the item to the find history
318
 
                        if (history.Contains(toAdd)) {
319
 
                                // remove it so it gets added at the top
320
 
                                history.Remove(toAdd);
321
 
                        }
322
 
                        // make sure there is only 20
323
 
                        if (history.Count == historyLimit) {
324
 
                                history.RemoveAt(historyLimit - 1);
325
 
                        }
326
 
                        history.Insert(0, toAdd);
327
 
                        
328
 
                        // update the drop down for the combobox
329
 
                        ListStore store = new ListStore (typeof (string));
330
 
                        for (int i = 0; i < history.Count; i ++)
331
 
                                store.AppendValues (history[i]);
332
 
 
333
 
                        if (history == findHistory) {
334
 
                                searchPatternEntry.Entry.Completion.Model = store;
335
 
                                searchPatternEntry.Model = store;
336
 
                        }
337
 
                        else if( history == replaceHistory) {
338
 
                                replacePatternEntry.Entry.Completion.Model = store;
339
 
                                replacePatternEntry.Model = store;
340
 
                        }
341
 
                }
342
 
                
343
 
                // loads the history arrays from the property service
344
 
                // NOTE: a newline character separates the search history strings
345
 
                private void LoadHistoryValues()
346
 
                {
347
 
                        object stringArray;
348
 
                        // set the history in properties
349
 
                        stringArray = PropertyService.Get<string> ("MonoDevelop.FindReplaceDialogs.FindHistory");
350
 
                
351
 
                        if (stringArray != null) {
352
 
                                string[] items = stringArray.ToString ().Split (historySeparator);
353
 
                                ListStore store = new ListStore (typeof (string));
354
 
 
355
 
                                if(items != null) {
356
 
                                        findHistory.AddRange (items);
357
 
                                        foreach (string i in items) {
358
 
                                                store.AppendValues (i);
359
 
                                        }
360
 
                                }
361
 
 
362
 
                                searchPatternEntry.Entry.Completion.Model = store;
363
 
                                searchPatternEntry.Model = store;
364
 
                        }
365
 
                        
366
 
                        // now do the replace history
367
 
                        stringArray = PropertyService.Get<string> ("MonoDevelop.FindReplaceDialogs.ReplaceHistory");
368
 
                        
369
 
                        if (replaceMode) {
370
 
                                if (stringArray != null) {
371
 
                                        string[] items = stringArray.ToString ().Split (historySeparator);
372
 
                                        ListStore store = new ListStore (typeof (string));
373
 
                                        
374
 
                                        if(items != null) {
375
 
                                                replaceHistory.AddRange (items);
376
 
                                                foreach (string i in items) {
377
 
                                                        store.AppendValues (i);
378
 
                                                }
379
 
                                        }
380
 
                                        
381
 
                                        replacePatternEntry.Entry.Completion.Model = store;
382
 
                                        replacePatternEntry.Model = store;
383
 
                                }
384
 
                        }
385
 
                }
386
 
                
387
 
                // saves the history arrays to the property service
388
 
                // NOTE: a newline character separates the search history strings
389
 
                private void SaveHistoryValues()
390
 
                {
391
 
                        string[] stringArray;
392
 
                        // set the history in properties
393
 
                        stringArray = new string[findHistory.Count];
394
 
                        findHistory.CopyTo (stringArray, 0);                    
395
 
                        PropertyService.Set ("MonoDevelop.FindReplaceDialogs.FindHistory", string.Join(historySeparator.ToString(), stringArray));
396
 
                        
397
 
                        // now do the replace history
398
 
                        if (replaceMode)        {
399
 
                                stringArray = new string[replaceHistory.Count];
400
 
                                replaceHistory.CopyTo (stringArray, 0);                         
401
 
                                PropertyService.Set ("MonoDevelop.FindReplaceDialogs.ReplaceHistory", string.Join(historySeparator.ToString(), stringArray));
402
 
                        }
403
 
                }
404
 
                
405
 
                public new void Destroy()
406
 
                {
407
 
                        // save the search and replace history to properties
408
 
                        OnClosed ();
409
 
                        base.Destroy();
410
 
                }
411
 
                
412
 
                public new void ShowAll()
413
 
                {
414
 
                        base.Show();
415
 
                        searchPatternEntry.Entry.SelectRegion (0, searchPatternEntry.ActiveText.Length);
416
 
                }
417
 
        }
418
 
}