3
// This file was derived from a file from #Develop.
5
// Copyright (C) 2001-2007 Mike Krüger <mkrueger@novell.com>
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.
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.
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
24
using System.ComponentModel;
25
using System.Collections.Specialized;
27
using MonoDevelop.Core.Gui;
28
using MonoDevelop.Core;
29
using MonoDevelop.Ide.Gui.Search;
30
using MonoDevelop.Ide.Gui;
35
namespace MonoDevelop.Ide.Gui.Dialogs
37
internal partial class ReplaceDialog: Gtk.Dialog
39
private const int historyLimit = 20;
40
private const char historySeparator = '\n';
42
public bool replaceMode;
43
StringCollection findHistory = new StringCollection();
44
StringCollection replaceHistory = new StringCollection();
50
findButton.UseUnderline = true;
51
closeButton.UseUnderline = true;
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);
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;
74
searchPatternEntry.Model = new ListStore(typeof(string));
75
searchPatternEntry.TextColumn = 0;
77
// set button sensitivity
78
findHelpButton.Sensitive = false;
80
// set replace dialog properties
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;
89
replacePatternEntry.Model = new ListStore(typeof(string));
90
replacePatternEntry.TextColumn = 0;
92
// set the label properties
93
replaceButton.UseUnderline = true;
94
replaceAllButton.UseUnderline = true;
96
// set te size groups to include the replace dialog
97
labels.AddWidget(labelReplace);
98
combos.AddWidget(replacePatternEntry);
99
helpButtons.AddWidget(replaceHelpButton);
101
replaceHelpButton.Sensitive = false;
105
labelReplace.Visible = replacePatternEntry.Visible = false;
106
replaceAllButton.Visible = replaceHelpButton.Visible = replaceButton.Visible = false;
107
markAllButton.UseUnderline = true;
109
this.Resize (500, 200);
110
TransientFor = IdeApp.Workbench.RootWindow;
113
public ReplaceDialog (bool replaceMode)
117
// some members needed to initialise this dialog based on replace mode
118
this.replaceMode = replaceMode;
124
ignoreCaseCheckBox.Active = !SearchReplaceManager.SearchOptions.IgnoreCase;
125
searchWholeWordOnlyCheckBox.Active = SearchReplaceManager.SearchOptions.SearchWholeWordOnly;
127
useSpecialSearchStrategyCheckBox.Active = SearchReplaceManager.SearchOptions.SearchStrategyType != SearchStrategyType.Normal;
128
useSpecialSearchStrategyCheckBox.Toggled += new EventHandler(SpecialSearchStrategyCheckBoxChangedEvent);
130
ListStore store = new ListStore (typeof (string));
131
store.AppendValues (GettextCatalog.GetString ("Wildcards"));
132
store.AppendValues (GettextCatalog.GetString ("Regular Expressions"));
133
specialSearchStrategyComboBox.Model = store;
135
CellRendererText cr = new CellRendererText ();
136
specialSearchStrategyComboBox.PackStart (cr, true);
137
specialSearchStrategyComboBox.AddAttribute (cr, "text", 0);
140
switch (SearchReplaceManager.SearchOptions.SearchStrategyType) {
141
case SearchStrategyType.Normal:
142
case SearchStrategyType.Wildcard:
143
searchWholeWordOnlyCheckBox.Sensitive = true;
145
case SearchStrategyType.RegEx:
146
searchWholeWordOnlyCheckBox.Sensitive = false;
150
specialSearchStrategyComboBox.Active = index;
151
specialSearchStrategyComboBox.Changed += new EventHandler (OnSpecialSearchStrategyChanged);
153
store = new ListStore (typeof (string));
154
store.AppendValues (GettextCatalog.GetString ("Current File"));
155
store.AppendValues (GettextCatalog.GetString ("All Open Files"));
157
searchLocationComboBox.Model = store;
158
searchLocationComboBox.PackStart (cr, true);
159
searchLocationComboBox.AddAttribute (cr, "text", 0);
162
if (SearchReplaceManager.SearchOptions.DocumentIteratorType == DocumentIteratorType.AllOpenFiles)
165
searchLocationComboBox.Active = index;
167
searchPatternEntry.Entry.Text = SearchReplaceManager.SearchOptions.SearchPattern;
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);
176
Title = GettextCatalog.GetString ("Replace");
177
replaceButton.Clicked += new EventHandler(ReplaceEvent);
178
replaceAllButton.Clicked += new EventHandler(ReplaceAllEvent);
179
replacePatternEntry.Entry.Text = SearchReplaceManager.SearchOptions.ReplacePattern;
181
Title = GettextCatalog.GetString ("Find");
182
markAllButton.Clicked += new EventHandler(MarkAllEvent);
184
searchPatternEntry.Entry.SelectRegion(0, searchPatternEntry.ActiveText.Length);
186
SpecialSearchStrategyCheckBoxChangedEvent(null, null);
187
SearchReplaceManager.ReplaceDialog = this;
190
protected void OnClosed()
193
SearchReplaceManager.ReplaceDialog = null;
196
void OnDeleted (object o, DeleteEventArgs args)
198
// perform the standard closing windows event
200
SearchReplaceManager.ReplaceDialog = null;
203
public void SetSearchPattern(string pattern)
205
searchPatternEntry.Entry.Text = pattern;
208
void OnSpecialSearchStrategyChanged (object o, EventArgs e)
210
if (specialSearchStrategyComboBox.Active != 1) {
211
searchWholeWordOnlyCheckBox.Sensitive = true;
213
searchWholeWordOnlyCheckBox.Sensitive = false;
217
void SetupSearchReplaceManager()
219
SearchReplaceManager.SearchOptions.SearchPattern = searchPatternEntry.ActiveText;
221
SearchReplaceManager.SearchOptions.ReplacePattern = replacePatternEntry.ActiveText;
224
SearchReplaceManager.SearchOptions.IgnoreCase = !ignoreCaseCheckBox.Active;
225
SearchReplaceManager.SearchOptions.SearchWholeWordOnly = searchWholeWordOnlyCheckBox.Active;
227
if (useSpecialSearchStrategyCheckBox.Active) {
228
switch (specialSearchStrategyComboBox.Active) {
230
SearchReplaceManager.SearchOptions.SearchStrategyType = SearchStrategyType.Wildcard;
233
SearchReplaceManager.SearchOptions.SearchStrategyType = SearchStrategyType.RegEx;
237
SearchReplaceManager.SearchOptions.SearchStrategyType = SearchStrategyType.Normal;
240
switch (searchLocationComboBox.Active) {
242
SearchReplaceManager.SearchOptions.DocumentIteratorType = DocumentIteratorType.CurrentDocument;
245
SearchReplaceManager.SearchOptions.DocumentIteratorType = DocumentIteratorType.AllOpenFiles;
250
void FindNextEvent(object sender, EventArgs e)
252
if (searchPatternEntry.ActiveText.Length == 0)
255
SetupSearchReplaceManager();
256
SearchReplaceManager.FindNext();
258
AddSearchHistoryItem(findHistory, searchPatternEntry.ActiveText);
261
void ReplaceEvent(object sender, EventArgs e)
263
if (searchPatternEntry.ActiveText.Length == 0)
266
SetupSearchReplaceManager();
267
SearchReplaceManager.Replace();
269
AddSearchHistoryItem(replaceHistory, replacePatternEntry.ActiveText);
272
void ReplaceAllEvent(object sender, EventArgs e)
274
if (searchPatternEntry.ActiveText.Length == 0)
277
SetupSearchReplaceManager();
278
SearchReplaceManager.ReplaceAll();
280
AddSearchHistoryItem(replaceHistory, replacePatternEntry.ActiveText);
283
void MarkAllEvent(object sender, EventArgs e)
285
if (searchPatternEntry.ActiveText.Length == 0)
288
SetupSearchReplaceManager();
289
SearchReplaceManager.MarkAll();
291
AddSearchHistoryItem(findHistory, searchPatternEntry.ActiveText);
294
void CloseDialogEvent(object sender, EventArgs e)
300
void SpecialSearchStrategyCheckBoxChangedEvent(object sender, EventArgs e)
302
if (useSpecialSearchStrategyCheckBox != null) {
303
specialSearchStrategyComboBox.Sensitive = useSpecialSearchStrategyCheckBox.Active;
304
if (useSpecialSearchStrategyCheckBox.Active) {
305
if (specialSearchStrategyComboBox.Active == 1) {
306
searchWholeWordOnlyCheckBox.Sensitive = false;
309
searchWholeWordOnlyCheckBox.Sensitive = true;
314
// generic method to add a string to a history item
315
private void AddSearchHistoryItem (StringCollection history, string toAdd)
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);
322
// make sure there is only 20
323
if (history.Count == historyLimit) {
324
history.RemoveAt(historyLimit - 1);
326
history.Insert(0, toAdd);
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]);
333
if (history == findHistory) {
334
searchPatternEntry.Entry.Completion.Model = store;
335
searchPatternEntry.Model = store;
337
else if( history == replaceHistory) {
338
replacePatternEntry.Entry.Completion.Model = store;
339
replacePatternEntry.Model = store;
343
// loads the history arrays from the property service
344
// NOTE: a newline character separates the search history strings
345
private void LoadHistoryValues()
348
// set the history in properties
349
stringArray = PropertyService.Get<string> ("MonoDevelop.FindReplaceDialogs.FindHistory");
351
if (stringArray != null) {
352
string[] items = stringArray.ToString ().Split (historySeparator);
353
ListStore store = new ListStore (typeof (string));
356
findHistory.AddRange (items);
357
foreach (string i in items) {
358
store.AppendValues (i);
362
searchPatternEntry.Entry.Completion.Model = store;
363
searchPatternEntry.Model = store;
366
// now do the replace history
367
stringArray = PropertyService.Get<string> ("MonoDevelop.FindReplaceDialogs.ReplaceHistory");
370
if (stringArray != null) {
371
string[] items = stringArray.ToString ().Split (historySeparator);
372
ListStore store = new ListStore (typeof (string));
375
replaceHistory.AddRange (items);
376
foreach (string i in items) {
377
store.AppendValues (i);
381
replacePatternEntry.Entry.Completion.Model = store;
382
replacePatternEntry.Model = store;
387
// saves the history arrays to the property service
388
// NOTE: a newline character separates the search history strings
389
private void SaveHistoryValues()
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));
397
// now do the replace history
399
stringArray = new string[replaceHistory.Count];
400
replaceHistory.CopyTo (stringArray, 0);
401
PropertyService.Set ("MonoDevelop.FindReplaceDialogs.ReplaceHistory", string.Join(historySeparator.ToString(), stringArray));
405
public new void Destroy()
407
// save the search and replace history to properties
412
public new void ShowAll()
415
searchPatternEntry.Entry.SelectRegion (0, searchPatternEntry.ActiveText.Length);