-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditor.cpp
More file actions
412 lines (343 loc) · 10.5 KB
/
Copy patheditor.cpp
File metadata and controls
412 lines (343 loc) · 10.5 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "editor.h"
#include "application.h"
#include "codeedit.h"
#include "tinyfiledialogs.h"
#include "imageview.h"
Editor::Editor(Widget* parent) : Widget(parent) {
id = icu::UnicodeString::fromUTF8("Editor");
tab_bar = new Tabs(this);
tab_bar->tab_clicked_callback = [&](TabInfo info){
tabinfoclicked(info);
};
tab_bar->all_tabs_closed_callback = [&](){
createNew(nullptr); // let's fix that...
};
tab_bar->add_new_tab_callback = [&](){
createNew(nullptr); // can do...
};
tab_bar->erasing_tab = [&](TabInfo info) {
std::lock_guard<std::mutex> lock(App::canMakeChanges);
auto it = editors.find(info.id);
if (it != editors.end()) {
Widget* w = it->second;
App::RemoveWidgetFromParent(w);
editors.erase(info.id);
App::deleteWidget(w);
}
};
tabid = 0;
createNew(nullptr);
}
void Editor::executeAction(WidgetActionType typ) {
for (auto it : editors) { // must send this to all of them even if they're not in our children list.
it.second->executeAction(typ);
}
tab_bar->executeAction(typ);
}
void Editor::tabinfoclicked(TabInfo info) {
for (auto it : editors) {
if (it.first == info.id){
if (it.second->parent != this){
App::MoveWidget(it.second, this);
}
it.second->show();
if (auto ce = dynamic_cast<CodeEdit*>(it.second)) {
App::setActiveLeafNode(ce->textedit);
}else if (auto iv = dynamic_cast<ImageView*>(it.second)) {
App::setActiveLeafNode(iv);
}
}else if (it.first != info.id && it.second->parent == this) {
App::RemoveWidgetFromParent(it.second);
it.second->hide();
}
}
}
void Editor::save() {
for (auto it : editors) { // not all of these are explicitly children so the default doesn't get it.
it.second->save();
}
}
bool Editor::is_image(std::string path) {
return std::string(path).find(".png") != std::string::npos ||
std::string(path).find(".jpg") != std::string::npos ||
std::string(path).find(".jpeg") != std::string::npos ||
std::string(path).find(".bmp") != std::string::npos ||
std::string(path).find(".gif") != std::string::npos;
}
void Editor::createNew(FileInfo* fn) {
auto ti = TabInfo();
if (fn) {
ti.title = icu::UnicodeString::fromUTF8(fn->filename);
}else{
ti.title = icu::UnicodeString::fromUTF8("Untitled");
}
ti.id = tabid;
tab_bar->addTab(ti);
tabid ++;
if (fn && is_image(fn->filepath)) {
ImageView* imgv = new ImageView(this);
editors[ti.id] = imgv;
}else{
CodeEdit* edtr = new CodeEdit(this, ti.id, [&](Widget* edtr){
if (App::settings->getValue("use_tabs", true)) {
edtr->t_y = t_y+tab_bar->t_h;
edtr->t_h = t_h-tab_bar->t_h;
}else{
edtr->t_y = t_y;
edtr->t_h = t_h;
}
edtr->t_x = t_x;
edtr->t_w = t_w;
}, [&](Widget* widget, FileInfo* info) {
TabInfo tab;
CodeEdit* edtr = dynamic_cast<CodeEdit*>(widget);
tab.id = edtr->TABID;
tab.title = icu::UnicodeString::fromUTF8(info->filename);
tab_bar->updateTab(tab);
});
editors[ti.id] = edtr;
}
auto edtr = editors[ti.id];
for (auto it : editors) {
if (it.first == ti.id) {
it.second->show();
}else{
it.second->hide();
}
}
tab_bar->selected_id = ti.id;
if (auto ce = dynamic_cast<CodeEdit*>(edtr)){
App::setActiveLeafNode(ce->textedit);
}else{
App::setActiveLeafNode(edtr);
}
for (auto it : editors) {
if (it.first == ti.id && it.second->parent != this){
App::MoveWidget(it.second, this);
}else if (it.first != ti.id && it.second->parent == this) {
App::RemoveWidgetFromParent(it.second);
}
}
if (fn) {
if (auto ce = dynamic_cast<CodeEdit*>(edtr)){
ce->openFile(fn);
}else if (auto iv = dynamic_cast<ImageView*>(edtr)){
iv->openFile(fn);
}
}
}
void Editor::render() {
auto e = editors[tab_bar->selected_id];
if (App::settings->getValue("use_tabs", true)){
App::runWithSKIZ(tab_bar->t_x, tab_bar->t_y, tab_bar->t_w, tab_bar->t_h, [&](){
tab_bar->render();
});
}
App::runWithSKIZ(e->t_x, e->t_y, e->t_w, e->t_h, [&](){
e->render();
});
}
void Editor::position(int x, int y, int w, int h) {
t_x = x;
t_y = y;
t_w = w;
t_h = h;
if (App::settings->getValue("use_tabs", true)) {
if (!tab_bar->is_visible) {
tab_bar->show();
}
tab_bar->position(x, y, w, h);
editors[tab_bar->selected_id]->position(x, y+tab_bar->t_h, w, h-tab_bar->t_h);
}else{
if (tab_bar->is_visible) {
tab_bar->hide();
}
editors[tab_bar->selected_id]->position(x, y, w, h);
}
}
void Editor::closeFile(int file_id) {
tab_bar->removeTab(file_id); // this triggers a thing in the tabbar which calls back to here to delete it (:
}
void Editor::fileOpenRequested(FileInfo* f, int lns, int chrs, int ln, int chr) {
if (!f) {
const char * fp = tinyfd_openFileDialog(
"Select a file", // dialog title
"", // default path and filename
0, NULL, NULL, // filter count and filters
0 // allow multiple selections (0 = no)
);
if (fp) {
std::string filePath(fp);
std::filesystem::path fullPath = filePath;
std::string filename = fullPath.filename().string();
f = new FileInfo();
f->filepath = filePath;
f->filename = filename;
}else{
App::commandUnfocused();
return;
}
}
for (auto itm : tab_bar->tabs_list) {
if (auto te = dynamic_cast<CodeEdit*>(editors[itm.id])) {
if (te->file && areSameFile(te->file->filepath, f->filepath)) {
tab_bar->selected_id = itm.id;
tabinfoclicked(itm);
moveto(lns, chrs, ln, chr);
App::commandUnfocused();
return;
}
}else if (auto iv = dynamic_cast<ImageView*>(editors[itm.id])) {
if (iv->file && areSameFile(iv->file->filepath, f->filepath)) {
tab_bar->selected_id = itm.id;
tabinfoclicked(itm);
moveto(lns, chrs, ln, chr);
App::commandUnfocused();
return;
}
}
}
// here we know it's not here
if (auto othereditor = dynamic_cast<Editor*>(App::rootelement->fileOpen(f->filepath))) {
othereditor->fileOpenRequested(f, lns, chrs, ln, chr);
return;
}
// here we know no other editors have the file either.
int tabbeforetab = tab_bar->selected_id;
createNew(f);
if (auto ce = dynamic_cast<CodeEdit*>(editors[tabbeforetab])) { // we do this cast because not all widgets have FileInfo file; variables.
if (!ce->file) {
if (ce->textedit->getFullText() == "") { // empty file only in memory - let's remove it.
closeFile(tabbeforetab);
}
}
}
// if (auto ce = dynamic_cast<CodeEdit*>(editors[tab_bar->selected_id])) { // we do this cast because not all widgets have FileInfo file; variables.
// App::setActiveLeafNode(ce->textedit);
// }
moveto(lns, chrs, ln, chr);
App::commandUnfocused();
}
void Editor::moveto(int lns, int chrs, int ln, int chr) {
if (auto ce = dynamic_cast<CodeEdit*>(editors[tab_bar->selected_id])) {
if (ln < 0 || ln >= ce->textedit->lines.size()) {
return;
}
if (lns < 0 || lns >= ce->textedit->lines.size()) {
return;
}
if (chr < 0 || ce->textedit->lines[ln].line_text.length() < chr) {
return;
}
if (chrs < 0 || ce->textedit->lines[lns].line_text.length() < chrs) {
return;
}
ce->textedit->cursors = { { lns, chrs, ln, chr, chr } };
ce->textedit->tryingToEnsureCursorPos = true;
}
}
bool Editor::on_key_event(int key, int scancode, int action, int mods) {
// here we will detect ctrl+o, maybe hotkeys for tabs? ctrl+q?
bool holding_control = (mods & GLFW_MOD_CONTROL) != 0;
bool holding_shift = (mods & GLFW_MOD_SHIFT) != 0;
if (this == App::activeEditor) {
if (key == GLFW_KEY_O && action == GLFW_PRESS && holding_control) {
fileOpenRequested(nullptr);
return true;
}else if (key == GLFW_KEY_N && action == GLFW_PRESS && holding_control) {
createNew(nullptr);
return true;
}else if (key == GLFW_KEY_W && action == GLFW_PRESS && holding_control) {
closeFile(tab_bar->selected_id);
return true;
}else if (key == GLFW_KEY_TAB && action == GLFW_PRESS && holding_control && holding_shift) {
tab_bar->nextTab();
return true;
}
}
return Widget::on_key_event(key, scancode, action, mods);
}
icu::UnicodeString Editor::getPaletteName() {
if (auto ce = dynamic_cast<CodeEdit*>(editors[tab_bar->selected_id])) {
if (!ce->file || ce->file->filename == "") {
return icu::UnicodeString::fromUTF8("Untitled");
}
return icu::UnicodeString::fromUTF8(ce->file->filename);
}else if (auto iv = dynamic_cast<ImageView*>(editors[tab_bar->selected_id])) {
if (!iv->file || iv->file->filename == "") {
return icu::UnicodeString::fromUTF8("Untitled Image");
}
return icu::UnicodeString::fromUTF8(iv->file->filename);
}
return icu::UnicodeString::fromUTF8("");
}
Widget* Editor::fileOpen(std::string fname) { // this is a widget function to find an editor which has a filename already open (let's not re-open it, no reason to.)
for (auto itm : tab_bar->tabs_list) {
if (auto te = dynamic_cast<CodeEdit*>(editors[itm.id])) {
if (te->file && areSameFile(te->file->filepath, fname)) {
return this;
}
}else if (auto iv = dynamic_cast<ImageView*>(editors[itm.id])) {
if (iv->file && areSameFile(iv->file->filepath, fname)) {
return this;
}
}
}
return nullptr;
}
Widget* Editor::getFirstEditor() {
return this;
}
std::vector<std::vector<std::string>> Editor::getOpenFiles(bool includeText) {
std::vector<std::vector<std::string>> out = {};
for (auto itm : tab_bar->tabs_list) {
if (auto te = dynamic_cast<CodeEdit*>(editors[itm.id])) {
std::vector<std::string> res;
if (te->file) {
res = {te->file->filename, te->file->filepath, "TEXT"};
}else{
res = {"Untitled", "", "TEXT"};
}
if (includeText) { // defaults to false
icu::UnicodeString fulltext = te->textedit->getFullText();
std::string text;
fulltext.toUTF8String(text);
res.push_back(text);
}
out.push_back(res);
}else if(auto te = dynamic_cast<ImageView*>(editors[itm.id])){
if (te->file) {
out.push_back({te->file->filename, te->file->filepath, "IMAGE"});
}else{
out.push_back({"Untitled Image", "", "IMAGE"});
}
}
}
return out;
}
int Editor::openUnnamedFile(int count) {
for (auto itm : tab_bar->tabs_list) {
if (auto te = dynamic_cast<CodeEdit*>(editors[itm.id])) {
if (!te->file) {
if (count == 0) {
tab_bar->selected_id = itm.id;
tabinfoclicked(itm);
App::commandUnfocused();
return count - 1;
}
count --;
}
}else if(auto te = dynamic_cast<ImageView*>(editors[itm.id])){
if (!te->file) {
if (count == 0) {
tab_bar->selected_id = itm.id;
tabinfoclicked(itm);
App::commandUnfocused();
return count - 1;
}
count --;
}
}
}
return count;
}