-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathApp.cpp
More file actions
417 lines (381 loc) · 11.4 KB
/
App.cpp
File metadata and controls
417 lines (381 loc) · 11.4 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
413
414
415
416
417
/*
* Copyright 2014-2019 Kacper Kasper <kacperkasper@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "App.h"
#include <AboutWindow.h>
#include <Alert.h>
#include <Catalog.h>
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <FindDirectory.h>
#include <Language.h>
#include <LocaleRoster.h>
#include <Path.h>
#include <tracker_private.h>
#include <WindowStack.h>
#include <algorithm>
#include <iterator>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include "AppPreferencesWindow.h"
#include "EditorWindow.h"
#include "FindWindow.h"
#include "GoToLineWindow.h"
#include "Preferences.h"
#include "Styler.h"
#include "Utils.h"
#include "QuitAlert.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "App"
App::App()
:
BApplication(gAppMime),
fLastActiveWindow(nullptr),
fAppPreferencesWindow(nullptr),
fFindWindow(nullptr),
fPreferences(nullptr),
fSuppressInitialWindow(false)
{
}
App::~App()
{
EditorWindow* window;
while(fWindows.CountItems() > 0) {
window = fWindows.RemoveItemAt(0);
if(fWindows.CountItems() == 0) {
fPreferences->fWindowRect = window->Frame();
}
if(window->LockLooper())
{
window->Quit();
}
}
fPreferences->Save(fPreferencesFile.Path());
delete fPreferences;
}
void
App::Init()
{
BPath settingsPath;
find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath);
settingsPath.Append(gAppName);
BDirectory preferencesDir(settingsPath.Path());
if(preferencesDir.InitCheck() == B_ENTRY_NOT_FOUND) {
preferencesDir.CreateDirectory(settingsPath.Path(), &preferencesDir);
}
fPreferencesFile.SetTo(&preferencesDir, "settings");
fPreferences = new Preferences();
fPreferences->Load(fPreferencesFile.Path());
fPreferences->fSettingsPath = settingsPath;
EditorWindow::SetPreferences(fPreferences);
Languages::LoadLanguages();
}
void
App::AboutRequested()
{
std::vector<std::string> specialThanks{
B_TRANSLATE("Neil Hodgson, for Scintilla editing component and SciTE editor."),
B_TRANSLATE("zuMi, for toolbar icons."),
B_TRANSLATE("humdinger, for GUI design tips and artwork."),
B_TRANSLATE("Konrad77, for dark theme."),
B_TRANSLATE_COMMENT("translators to:", "to _ (language), e.g. German"),
};
const std::unordered_map<std::string, std::vector<std::string>> translatorsMap{
{ "ca", { "Davidmp" } },
{ "de", { "FabianReidinger", "humdinger" } },
{ "el", { "blu.256" } },
{ "en_AU", { "jt15s" } },
{ "en_GB", { "Adam Fowler", "jt15s" } },
{ "es", { "cafeina", "unspacyar"} },
{ "es_419", { "victroniko" } },
// { "fi", { "Karvjorm" } },
{ "fr", { "Briseur", "Loïc", "Jérôme Duval" } },
{ "fur", { "tmtfx" } },
{ "hu", { "miqlas" } },
// { "id", { "mazbrili" } },
{ "it", { "Gabriele Baldassarre" } },
{ "ja", { "Murai Takashi" } },
// { "lt", { "damoklas" } },
{ "nl", { "Schrijvers Luc" } },
{ "pt", { "louisdem", "Victor Domingos" } },
{ "ro", { "al-popa", "Florentina Mușat" } },
{ "ru", { "al-popa", "ArmanHayots", "Sergei Reznikov", "TK-313" } },
{ "sv", { "deejam", "Fredrik Modéen" } },
{ "tr", { "Emir Sarı" } },
{ "uk", { "Alexey Lugin" } },
{ "zh_Hans", { "玉堂白鹤" } }
};
BMessage languages;
if(BLocaleRoster::Default()->GetAvailableLanguages(&languages) == B_OK) {
BString langID;
for(int i = 0; languages.FindString("language", i, &langID) == B_OK; i++) {
const auto translators = translatorsMap.find(langID.String());
if(translators == translatorsMap.end())
continue;
BLanguage lang(langID.String());
BString name;
lang.GetName(name);
std::ostringstream languageRow;
languageRow << " " << name.String() << ": ";
std::copy(translators->second.begin(), translators->second.end(),
std::ostream_iterator<std::string>(languageRow, ", "));
std::string str(languageRow.str());
str.erase(str.end() - 2);
specialThanks.push_back(str);
}
}
// prepare something acceptable for BeAPI
std::vector<const char*> specialThanksC;
std::transform(specialThanks.begin(), specialThanks.end(),
std::back_inserter(specialThanksC), [](const std::string& s) { return s.c_str(); });
specialThanksC.push_back(nullptr);
BAboutWindow* window = new BAboutWindow(gAppName, gAppMime);
window->AddCopyright(2016, "Kacper Kasper");
window->AddDescription(
B_TRANSLATE("Code editor for Haiku based on Scintilla editing component."));
window->AddSpecialThanks(specialThanksC.data());
window->AddExtraInfo(B_TRANSLATE("Distributed on MIT license terms."));
window->Show();
}
bool
App::QuitRequested()
{
BObjectList<EditorWindow> unsaved;
std::vector<std::string> unsavedPaths;
EditorWindow* current;
for(int i = 0; (current = fWindows.ItemAt(i)); ++i) {
if(current->IsModified()) {
unsaved.AddItem(current);
unsavedPaths.push_back(std::string(current->OpenedFilePath()));
}
}
if(unsaved.IsEmpty()) {
return true;
}
QuitAlert* quitAlert = new QuitAlert(unsavedPaths);
auto filesToSave = quitAlert->Go();
if(filesToSave.empty()) {
fLastActiveWindow->Activate();
return false;
}
for(int i = 0; (current = unsaved.ItemAt(i)); ++i) {
if(filesToSave[i] == false) continue;
BMessage reply;
BMessage save(SAVE_FILE);
BMessenger messenger((BWindow*) current);
messenger.SendMessage(&save, &reply);
// FIXME: this is smelly
}
return true;
}
void
App::ReadyToRun()
{
if(fSuppressInitialWindow == false && CountWindows() == 0) {
PostMessage(WINDOW_NEW);
}
}
void
App::RefsReceived(BMessage* message)
{
int32 count;
if(message->GetInfo("refs", nullptr, &count) != B_OK) {
return;
}
std::unique_ptr<BWindowStack> windowStack;
BMessenger messenger(kTrackerSignature);
BMessage trackerMessage(B_REFS_RECEIVED);
entry_ref ref;
for(int32 i = 0; i < count; ++i) {
if(message->FindRef("refs", i, &ref) == B_OK) {
if(BNode(&ref).IsDirectory()) {
trackerMessage.AddRef("refs", &ref);
continue;
}
const int32 line = message->GetInt32("be:line", i, -1);
const int32 column = message->GetInt32("be:column", i, -1);
_ActivateOrCreateWindow(message, ref, line, column, windowStack);
}
}
if(!trackerMessage.IsEmpty()) {
messenger.SendMessage(&trackerMessage);
}
}
void
App::MessageReceived(BMessage* message)
{
switch(message->what) {
case SUPPRESS_INITIAL_WINDOW: {
fSuppressInitialWindow = true;
} break;
case ACTIVATE_WINDOW: {
BWindow* window = nullptr;
if(message->FindPointer("window", (void**) &window) == B_OK && window != nullptr) {
window->Activate();
}
} break;
case ACTIVE_WINDOW_CHANGED: {
if(message->FindPointer("window", (void**) &fLastActiveWindow) != B_OK) {
fLastActiveWindow = nullptr;
} else {
fPreferences->fWindowRect = fLastActiveWindow->Frame();
}
} break;
case APP_PREFERENCES_QUITTING: {
fAppPreferencesWindow = nullptr;
} break;
case FINDWINDOW_FIND:
case FINDWINDOW_REPLACE:
case FINDWINDOW_REPLACEFIND:
case FINDWINDOW_REPLACEALL: {
// TODO: == nullptr should never happen, alert if it somehow does?
if(fLastActiveWindow != nullptr) {
BMessenger messenger((BWindow*) fLastActiveWindow);
messenger.SendMessage(message);
}
fPreferences->fFindWindowState = *message;
} break;
case FINDWINDOW_BOOKMARKALL: {
if(fLastActiveWindow != nullptr) {
BMessenger messenger((BWindow*) fLastActiveWindow);
messenger.SendMessage(message);
}
} break;
case FINDWINDOW_QUITTING: {
fFindWindow = nullptr;
} break;
case MAINMENU_EDIT_APP_PREFERENCES: {
if(fAppPreferencesWindow == nullptr) {
fAppPreferencesWindow = new AppPreferencesWindow(fPreferences);
for(uint32 i = 0, count = fWindows.CountItems(); i < count; i++) {
fAppPreferencesWindow->StartWatching(fWindows.ItemAt(i), APP_PREFERENCES_CHANGED);
}
}
fAppPreferencesWindow->Show();
fAppPreferencesWindow->Activate();
} break;
case MAINMENU_SEARCH_FINDREPLACE: {
if(fFindWindow == nullptr) {
fFindWindow = new FindWindow(
&fPreferences->fFindWindowState, fPreferences->fSettingsPath);
}
fFindWindow->Show();
fFindWindow->Activate();
std::string findText = message->GetString("selection", "");
if(findText.empty() == false) {
if(fFindWindow->LockLooper()) {
fFindWindow->SetFindText(findText);
fFindWindow->UnlockLooper();
}
}
} break;
case WINDOW_NEW: {
std::unique_ptr<BWindowStack> stack;
auto window = _CreateWindow(message, stack);
window->Show();
} break;
case WINDOW_NEW_WITH_QUIT_REPLY: {
BMessage* detached = DetachCurrentMessage();
entry_ref ref;
if(message->FindRef("refs", &ref) == B_OK) {
const int32 line = message->GetInt32("be:line", -1);
const int32 column = message->GetInt32("be:column", -1);
_CreateWindowWithQuitReply(detached, &ref, line, column);
} else {
_CreateWindowWithQuitReply(detached);
}
} break;
case WINDOW_CLOSE: {
EditorWindow* window;
if(message->FindPointer("window", (void**) &window) == B_OK) {
fWindows.RemoveItem(window);
}
if(fWindows.CountItems() == 0) {
fPreferences->fWindowRect = window->Frame();
Quit();
}
} break;
default:
BApplication::MessageReceived(message);
break;
}
}
/**
* Takes into account current preferences: if files should not always be opened
* in new window it first looks for existing one with specified file and
* activates it if found; otherwise creates a new one. Then jumps to specified
* line and column. windowStack - see App::_CreateWindow.
*/
void
App::_ActivateOrCreateWindow(const BMessage* message, const entry_ref& ref,
const int32 line, const int32 column,
std::unique_ptr<BWindowStack>& windowStack)
{
if(!fPreferences->fAlwaysOpenInNewWindow) {
EditorWindow* current;
for(int i = 0; (current = fWindows.ItemAt(i)); ++i) {
if(std::string(current->OpenedFilePath()) == BPath(&ref).Path()) {
current->Activate();
if(line != -1) {
BMessage gotoMsg(GTLW_GO);
gotoMsg.AddInt32("line", line);
current->PostMessage(&gotoMsg);
}
return;
}
}
}
auto window = _CreateWindow(message, windowStack);
window->OpenFile(&ref, line, column);
window->Show();
}
/**
* Creates a window with parameters specified in message:
* (Pointer) window - new window will be attached this one
* Takes current preferences into account.
* Adds created window to the list of all open windows.
* Returns a window and a window stack to which it is attached.
*/
EditorWindow*
App::_CreateWindow(const BMessage* message, std::unique_ptr<BWindowStack>& windowStack)
{
if(message != nullptr) {
BWindow* windowPtr;
if(message->FindPointer("window", (void**) &windowPtr) == B_OK)
windowStack.reset(new BWindowStack(windowPtr));
}
bool stagger = fWindows.CountItems() > 0 &&
(!fPreferences->fOpenWindowsInStack || !windowStack);
EditorWindow* window = new EditorWindow(stagger);
if(fPreferences->fOpenWindowsInStack) {
if(!windowStack)
windowStack.reset(new BWindowStack(window));
else
windowStack->AddWindow(window);
}
if(fAppPreferencesWindow != nullptr) {
fAppPreferencesWindow->StartWatching(window, APP_PREFERENCES_CHANGED);
}
fWindows.AddItem(window);
return window;
}
/**
* Creates a window and tells it to reply to message when it quits.
* Then optionally opens the file and jumps to specified line and column.
* IMPORTANT! message has to be detached from any looper.
*/
void
App::_CreateWindowWithQuitReply(BMessage* message, const entry_ref* ref,
const int32 line, const int32 column)
{
auto emptyStack = std::unique_ptr<BWindowStack>();
EditorWindow* window = _CreateWindow(message, emptyStack);
window->SetOnQuitReplyToMessage(message);
if(ref != nullptr)
window->OpenFile(ref, line, column);
window->Show();
}