-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind.cpp
95 lines (85 loc) · 2.67 KB
/
find.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Input.H>
#include <FL/Fl_Button.H>
#include <FL/fl_ask.H>
#include <cstring>
#include "find.hpp"
#include "ui.hpp"
#include "editor.hpp"
Fl_Window * replace_dialog;
Fl_Input * replace_find;
Fl_Input * replace_replace;
Fl_Button * replace_replace_all;
Fl_Button * replace_replace_next;
Fl_Button * replace_cancel;
void find_replace_next(Fl_Text_Editor * editor, Fl_Text_Buffer * textbuf) {
const char * tfind = replace_find->value();
const char * treplace = replace_replace->value();
if(!strlen(tfind)) {
replace_dialog->show();
return;
}
int pos = editor->insert_position();
int found = textbuf->search_forward(pos,tfind,&pos);
if(found) {
textbuf->select(pos,pos+strlen(tfind));
textbuf->remove_selection();
textbuf->insert(pos,treplace);
textbuf->select(pos,pos+strlen(treplace));
editor->insert_position(pos+strlen(treplace));
editor->show_insert_position();
} else {
fl_alert("No occurrences found\n");
}
return;
}
void find_replace_all(Fl_Text_Editor * editor, Fl_Text_Buffer * textbuf) {
const char * tfind = replace_find->value();
const char * treplace = replace_replace->value();
if(!strlen(tfind)) {
replace_dialog->show();
return;
}
int pos = editor->insert_position();
int found = textbuf->search_forward(pos,tfind,&pos);
while(found) {
if(found) {
textbuf->select(pos,pos+strlen(tfind));
textbuf->remove_selection();
textbuf->insert(pos,treplace);
textbuf->select(pos,pos+strlen(treplace));
editor->insert_position(pos+strlen(treplace));
editor->show_insert_position();
}
found = textbuf->search_forward(pos,tfind,&pos);
}
return;
}
void find_show_dialog(Fl_Widget *, void *) {
replace_dialog->show();
return;
}
void find_hide_dialog(Fl_Widget *, void *) {
replace_dialog->hide();
return;
}
void find_create_dialog(void) {
// Create replaceace dialog
replace_dialog = new Fl_Window(300,75,"Replace text");
replace_find = new Fl_Input(100,0,200,globalFontsize,"Find:");
replace_replace = new Fl_Input(100,globalFontsize,200,globalFontsize,"Replace:");
replace_replace_all = new Fl_Button(5,50,90,globalFontsize,"Replace all");
replace_replace_next = new Fl_Button(100,50,120,globalFontsize,"Replace next");
replace_cancel = new Fl_Button(225,50,60,globalFontsize,"Cancel");
replace_cancel->callback(find_hide_dialog);
replace_replace_next->callback(editor_replace_next_callback);
replace_replace_all->callback(editor_replace_all_callback);
replace_dialog->add(replace_find);
replace_dialog->add(replace_replace);
replace_dialog->add(replace_replace_all);
replace_dialog->add(replace_replace_next);
replace_dialog->add(replace_cancel);
replace_dialog->end();
return;
}