SearchReplaceForm.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using FastReport.Utils;
  9. using FastReport.Design;
  10. using FastReport.Design.PageDesigners.Code;
  11. #if !MONO
  12. using FastReport.Editor;
  13. #endif
  14. namespace FastReport.Forms
  15. {
  16. internal partial class SearchReplaceForm : BaseDialogForm
  17. {
  18. private Designer designer;
  19. private bool replace;
  20. private bool firstSearch;
  21. private int index;
  22. private static string[] FLastSearch;
  23. private static string[] FLastReplace;
  24. private static bool FMatchCase;
  25. private static bool FMatchWholeWord;
  26. public bool Replace
  27. {
  28. get { return replace; }
  29. set
  30. {
  31. replace = value;
  32. pnReplace.Visible = value;
  33. pnReplaceButton.Visible = value;
  34. PerformLayout();
  35. ClientSize = new Size(gbOptions.Right + gbOptions.Left, (value ? pnReplaceButton.Bottom : pnFindButton.Bottom) + 4);
  36. }
  37. }
  38. private List<CharacterRange> FindText(string text, int startIndex)
  39. {
  40. string textToFind = cbxFind.Text;
  41. bool matchCase = cbMatchCase.Checked;
  42. bool wholeWord = cbMatchWholeWord.Checked;
  43. List<CharacterRange> ranges = new List<CharacterRange>();
  44. string nonWordChars = " `-=[];',./~!@#$%^&*()+{}:\"<>?\\|";
  45. while (startIndex < text.Length)
  46. {
  47. int i = text.IndexOf(textToFind, startIndex, matchCase ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase);
  48. if (i >= 0)
  49. {
  50. bool skip = false;
  51. if (wholeWord)
  52. {
  53. if (i > 0 && nonWordChars.IndexOf(text[i - 1]) != -1)
  54. skip = true;
  55. if (i < text.Length - 1 && nonWordChars.IndexOf(text[i + 1]) != -1)
  56. skip = true;
  57. }
  58. if (!skip)
  59. ranges.Add(new CharacterRange(i, textToFind.Length));
  60. startIndex = i + textToFind.Length;
  61. }
  62. else
  63. break;
  64. }
  65. return ranges;
  66. }
  67. private void FindNext(bool replace, bool replaceAll)
  68. {
  69. bool found = false;
  70. if (designer.ActiveReportTab.ActivePage == null)
  71. {
  72. #if !MONO
  73. // search in the code tab
  74. SyntaxEdit Edit = (designer.ActiveReportTab.ActivePageDesigner as CodePageDesigner).Edit;
  75. if (firstSearch)
  76. Edit.FirstSearch = true;
  77. SearchOptions options = SearchOptions.None;
  78. if (cbMatchCase.Checked)
  79. options |= SearchOptions.CaseSensitive;
  80. if (cbMatchWholeWord.Checked)
  81. options |= SearchOptions.WholeWordsOnly;
  82. if (Edit.Selection.SelectedText == cbxFind.Text && replace && !replaceAll)
  83. {
  84. Edit.Selection.SelectedText = cbxReplace.Text;
  85. found = true;
  86. }
  87. else
  88. {
  89. while (true)
  90. {
  91. if (Edit.Find(cbxFind.Text, options))
  92. {
  93. found = true;
  94. if (replace)
  95. Edit.Selection.SelectedText = cbxReplace.Text;
  96. if (!replaceAll)
  97. break;
  98. }
  99. else
  100. break;
  101. }
  102. }
  103. #endif
  104. }
  105. else
  106. {
  107. ObjectCollection allObjects = designer.ActiveReportTab.ActivePage.AllObjects;
  108. for (; index < allObjects.Count; index++)
  109. {
  110. Base c = allObjects[index];
  111. string text = "";
  112. if (c is TextObject)
  113. text = (c as TextObject).Text;
  114. else if (c is RichObject)
  115. text = (c as RichObject).Text;
  116. List<CharacterRange> ranges = FindText(text, 0);
  117. if (ranges.Count > 0)
  118. {
  119. found = true;
  120. if (!replaceAll)
  121. {
  122. designer.SelectedObjects.Clear();
  123. designer.SelectedObjects.Add(c);
  124. designer.SelectionChanged(null);
  125. }
  126. if (replace)
  127. {
  128. int correction = 0;
  129. foreach (CharacterRange range in ranges)
  130. {
  131. text = text.Remove(range.First + correction, range.Length);
  132. text = text.Insert(range.First + correction, cbxReplace.Text);
  133. correction += cbxReplace.Text.Length - range.Length;
  134. }
  135. if (c is TextObject)
  136. (c as TextObject).Text = text;
  137. else if (c is RichObject)
  138. (c as RichObject).Text = text;
  139. }
  140. if (!replaceAll)
  141. {
  142. index++;
  143. break;
  144. }
  145. }
  146. }
  147. }
  148. if (found && replace)
  149. designer.SetModified(null, "Change");
  150. if (firstSearch)
  151. {
  152. if (!found)
  153. FRMessageBox.Information(Res.Get("Forms,SearchReplace,NotFound"));
  154. firstSearch = false;
  155. }
  156. else
  157. {
  158. if (!found)
  159. FRMessageBox.Information(Res.Get("Forms,SearchReplace,NoMoreFound"));
  160. }
  161. }
  162. private void cbxFind_TextChanged(object sender, EventArgs e)
  163. {
  164. bool enabled = !String.IsNullOrEmpty(cbxFind.Text);
  165. btnFindNext.Enabled = enabled;
  166. btnReplace.Enabled = enabled;
  167. btnReplaceAll.Enabled = enabled;
  168. }
  169. private void btnFindNext_Click(object sender, EventArgs e)
  170. {
  171. FindNext(false, false);
  172. }
  173. private void btnReplace_Click(object sender, EventArgs e)
  174. {
  175. FindNext(true, false);
  176. }
  177. private void btnReplaceAll_Click(object sender, EventArgs e)
  178. {
  179. FindNext(true, true);
  180. }
  181. private void SearchReplaceForm_KeyDown(object sender, KeyEventArgs e)
  182. {
  183. if (e.KeyData == Keys.Escape)
  184. Close();
  185. }
  186. private void SearchReplaceForm_FormClosed(object sender, FormClosedEventArgs e)
  187. {
  188. Done();
  189. }
  190. private void Init()
  191. {
  192. if (FLastSearch != null)
  193. {
  194. foreach (string s in FLastSearch)
  195. {
  196. cbxFind.Items.Add(s);
  197. }
  198. }
  199. if (FLastReplace != null)
  200. {
  201. foreach (string s in FLastReplace)
  202. {
  203. cbxReplace.Items.Add(s);
  204. }
  205. }
  206. cbMatchCase.Checked = FMatchCase;
  207. cbMatchWholeWord.Checked = FMatchWholeWord;
  208. firstSearch = true;
  209. index = 0;
  210. cbxFind_TextChanged(this, EventArgs.Empty);
  211. Config.RestoreFormState(this);
  212. }
  213. private void Done()
  214. {
  215. List<string> items = new List<string>();
  216. foreach (object item in cbxFind.Items)
  217. {
  218. items.Add(item.ToString());
  219. }
  220. if (items.IndexOf(cbxFind.Text) == -1)
  221. items.Add(cbxFind.Text);
  222. FLastSearch = items.ToArray();
  223. foreach (object item in cbxReplace.Items)
  224. {
  225. items.Add(item.ToString());
  226. }
  227. if (items.IndexOf(cbxReplace.Text) == -1)
  228. items.Add(cbxReplace.Text);
  229. FLastReplace = items.ToArray();
  230. FMatchCase = cbMatchCase.Checked;
  231. FMatchWholeWord = cbMatchWholeWord.Checked;
  232. Config.SaveFormState(this);
  233. }
  234. public override void Localize()
  235. {
  236. base.Localize();
  237. MyRes res = new MyRes("Forms,SearchReplace");
  238. Text = res.Get("");
  239. lblFind.Text = res.Get("Find");
  240. lblReplace.Text = res.Get("Replace");
  241. gbOptions.Text = res.Get("Options");
  242. cbMatchCase.Text = res.Get("MatchCase");
  243. cbMatchWholeWord.Text = res.Get("MatchWholeWord");
  244. btnFindNext.Text = res.Get("FindNext");
  245. btnReplace.Text = res.Get("ReplaceBtn");
  246. btnReplaceAll.Text = res.Get("ReplaceAll");
  247. }
  248. public SearchReplaceForm(Designer designer, bool isReplace)
  249. {
  250. this.designer = designer;
  251. InitializeComponent();
  252. Localize();
  253. replace = isReplace;
  254. Init();
  255. UIUtils.CheckRTL(this);
  256. }
  257. }
  258. }