1 namespace gui::dialogs;
7 public class ReplaceDialog : Window
12 background = formColor;
13 minClientSize = { 432, 144 };
17 property EditBox editBox
25 // Selection Only Default Check / Disabled
27 value.GetSelPos(null, &y1, &x1, null, &y2, &x2, true);
28 if(!editBox || (x1 == x2 && y1 == y2))
29 selection.disabled = true;
30 if(y1 == y2 || !editBox)
31 wholeFile.checked = true;
33 selection.checked = true;
35 if(value && y1 == y2 && x1 != x2)
37 char * selection = new char[value.SelSize() + 1];
38 value.GetSel(selection, false);
39 findWhat.contents = selection;
41 findNext.disabled = !selection[0];
42 replace.disabled = !selection[0];
43 replaceAll.disabled = !selection[0];
46 replace.isDefault = true;
50 property char * searchString
54 findWhat.contents = value;
55 findNext.disabled = !value || !value[0];
56 replace.disabled = !value || !value[0];
57 replaceAll.disabled = !value || !value[0];
59 get { return findWhat.contents; }
61 property char * replaceString { set { replaceWith.contents = replaceString; } get { return replaceWith.contents; } }
62 property bool wholeWord { set { wholeWord.checked = value; } get { return wholeWord.checked; } };
63 property bool matchCase { set { matchCase.checked = value; } get { return matchCase.checked; } };
67 bool FindClicked(Button control, int x, int y, Modifiers mods)
69 EditBoxFindResult findResult;
72 EditLine line1, line2;
73 int sel1X, sel1Y, sel2X, sel2Y;
75 bool selectionOnly = selection.checked;
76 bool wholeWord = this.wholeWord.checked;
77 bool matchCase = this.matchCase.checked;
78 char * searchString = findWhat.contents;
79 char * replaceString = replaceWith.contents;
81 if(!editBox) return false;
85 entriesReplaced = entriesFound = 0;
87 // If in selection mode, the end of the selection is the end point of the replace
89 editBox.GetSelPos(null, null, null,
90 &line, &this.y, &this.x, true);
91 // If in whole file mode, the starting position is the end of the replace
94 null, &this.y, &this.x,
95 null, null, null, true);
98 editBox.GetSelPos(&line1, &sel1Y, &sel1X, &line2, &sel2Y, &sel2X, true);
100 if(sel1X != sel2X || sel1Y != sel2Y)
101 editBox.Select(line2, sel2Y, sel2X, line1, sel1Y, sel1X);
103 lastX = editBox.column;
104 lastY = editBox.lineNumber;
105 lastLine = editBox.line;
108 findResult = editBox.FindInSelection(searchString, wholeWord, matchCase, line, this.y, this.x);
110 findResult = editBox.Find(searchString, wholeWord, matchCase, true);
114 int newSel1X, newSel1Y, newSel2X, newSel2Y;
115 editBox.GetSelPos(null, &newSel1Y, &newSel1X, null, &newSel2Y, &newSel2X, true);
116 if(newSel1X == sel1X && newSel1Y == sel1Y && newSel2X == sel2X && newSel2Y == sel2Y)
118 if(control.id == ID_REPLACE) // Replace
120 if(this.y == sel1Y && sel1X <= this.x)
121 this.x += strlen(replaceString) - (newSel2X - newSel1X);
122 editBox.PutS(replaceString);
123 editBox.UpdateDisplay();
128 findResult = editBox.FindInSelection(searchString, wholeWord, matchCase, line, this.y, this.x);
130 findResult = editBox.Find(searchString, wholeWord, matchCase, true);
135 // Reselect text in proper order if no search result
136 else if(sel1X != sel2X || sel1Y != sel2Y)
137 editBox.Select(line1, sel1Y, sel1X, line2, sel2Y, sel2X);
139 // Only for whole file search...
140 if(findResult == wrapped) wrapped++;
141 if(findResult != notFound && wrapped)
144 editBox.GetSelPos(null, &curY, &curX, null, null, null, true);
146 curY > this.y || (curY == this.y && curX >= this.x))
148 editBox.GoToPosition(lastLine, lastY, lastX);
149 findResult = notFound;
153 if(findResult == notFound)
158 sprintf(contents, $"%d occurences found, %d replaced", entriesFound, entriesReplaced);
159 MessageBox { type = ok, master = this, text = $"Search Finished", contents = contents }.Modal();
162 MessageBox { type = ok, master = this, text = $"Search Finished", contents = $"Search string not found." }.Modal();
165 selection.disabled = true;
166 wholeFile.checked = true;
167 findNext.isDefault = true;
172 replace.isDefault = true;
174 // findWhat.Activate();
175 return false; //true;
178 // Any UNUSED CODE???
179 bool EditBoxChanged(EditBox control)
183 selection.disabled = true;
184 wholeFile.checked = true;
186 findNext.isDefault = true;
192 bool OtherClicked(Button control, int x, int y, Modifiers mods)
196 selection.disabled = true;
197 wholeFile.checked = true;
199 findNext.isDefault = true;
209 int entriesFound, entriesReplaced;
213 this, text = $"Replace with:", anchor = { left = 124, top = 35, right = 120 }, size.h = 20, hotKey = altP;
216 Label replaceWithLabel
218 this, position = { 10, 35 }, labeledWindow = replaceWith
223 this, isCheckbox = true, text = $"Whole word only", position = { 10, 65 }, hotKey = altW;
224 NotifyClicked = OtherClicked;
229 this, isCheckbox = true, text = $"Match case", position = { 10, 85 }, hotKey = altC;
230 NotifyClicked = OtherClicked;
235 this, isRadio = true, text = $"Selection", position = { 180, 85 }, hotKey = altS;
236 NotifyClicked = OtherClicked;
241 this, isRadio = true, text = $"Whole File", position = { 180, 105 }, hotKey = altH, checked = true;
242 NotifyClicked = OtherClicked;
247 this, text = $"Replace In", position = { 180, 65 }
252 this, isDefault = true, keyRepeat = true, text = $"Find Next", anchor = { top = 10, right = 10 }, size = { 100 }, hotKey = altF, disabled = true;
253 NotifyClicked = FindClicked;
258 this, keyRepeat = true, text = $"Replace", anchor = { top = 42, right = 10 }, size = { 100 }, hotKey = altR, id = ID_REPLACE, disabled = true;
259 NotifyClicked = FindClicked;
264 this, text = $"Replace All", anchor = { top = 75, right = 10 }, size = { 100 }, hotKey = altA, disabled = true;
266 bool NotifyClicked(Button control, int x, int y, Modifiers mods)
268 int searchLen, replaceLen;
269 EditLine line1, line2;
270 int sel1X, sel1Y, sel2X, sel2Y;
272 EditLine bakLine1, bakLine2;
273 int bakSel1X, bakSel1Y, bakSel2X, bakSel2Y;
275 bool selectionOnly = selection.checked;
276 bool wholeWord = this.wholeWord.checked;
277 bool matchCase = this.matchCase.checked;
278 char * searchString = findWhat.contents;
279 char * replaceString = replaceWith.contents;
281 if(!editBox) return false;
285 entriesReplaced = entriesFound = 0;
287 editBox.GetSelPos(&bakLine1, &bakSel1Y, &bakSel1X, &bakLine2, &bakSel2Y, &bakSel2X, true);
289 // If in selection mode, the end of the selection is the end point of the replace
291 editBox.GetSelPos(null, null, null,
292 &line, &this.y, &this.x, true);
293 // If in whole file mode, the starting position is the end of the replace
296 null, &this.y, &this.x,
297 null, null, null, true);
300 // Invert Selection to find the word again
302 &line1, &sel1Y, &sel1X, &line2, &sel2Y, &sel2X, true);
303 if(sel1X != sel2X || sel1Y != sel2Y)
304 editBox.Select(line2, sel2Y, sel2X, line1, sel1Y, sel1X);
306 searchLen = strlen(searchString);
307 replaceLen = strlen(replaceString);
311 int lastX = editBox.column;
312 int lastY = editBox.lineNumber;
314 EditLine lastLine = editBox.line;
315 EditBoxFindResult findResult;
318 findResult = editBox.FindInSelection(searchString,
319 wholeWord, matchCase,
320 line, this.y, this.x);
322 findResult = editBox.Find(searchString, wholeWord, matchCase, true);
324 if(findResult == notFound) break;
325 if(findResult == wrapped) wrapped++;
327 editBox.GetSelPos(null, &curY, &curX, null, null, null, true);
331 if(wrapped == 2 || curY > this.y || (curY == this.y && curX >= this.x))
333 editBox.GoToPosition(lastLine, lastY, lastX);
337 if(this.y == curY && curX <= this.x)
338 this.x += replaceLen - searchLen;
339 editBox.PutS(replaceString);
346 MessageBox { type = ok, master = this, text = $"Search Finished", contents = $"Search string not found." }.Modal();
348 // Reselect text in proper order if no search result
349 if(selectionOnly && (sel1X != sel2X || sel1Y != sel2Y))
350 editBox.Select(line1, sel1Y, sel1X, line2, sel2Y, sel2X);
355 sprintf(contents, $"%d occurences found, %d replaced", entriesFound, entriesReplaced);
356 MessageBox { type = ok, master = this, text = $"Search Finished", contents = contents }.Modal();
360 editBox.Select(bakLine2, bakSel2Y, bakSel2X, bakLine1, bakSel1Y, bakSel1X);
363 selection.disabled = true;
364 wholeFile.checked = true;
367 findNext.isDefault = true;
374 this, text = $"Cancel", anchor = { top = 106, right = 10 }, size = { 100 }, hotKey = escape, NotifyClicked = ButtonCloseDialog;
379 this, text = $"Find what:", anchor = { left = 124, top = 10, right = 120 }, size.h = 20, hotKey = altN;
381 void NotifyUpdate(EditBox control)
383 bool disabled = findWhat.contents[0] ? false : true;
384 findNext.disabled = disabled;
385 replace.disabled = disabled;
386 replaceAll.disabled = disabled;
390 Label findWhatLabel { this, labeledWindow = findWhat, position = { 10, 10 } };
392 bool OnKeyHit(Key key, unichar ch)
397 return findWhat.OnKeyHit(key, ch);