-
-
Notifications
You must be signed in to change notification settings - Fork 339
/
IconEditorApp.cpp
460 lines (389 loc) · 10.3 KB
/
IconEditorApp.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* Copyright 2006, 2011, Stephan Aßmus <superstippi@gmx.de>.
* Copyright 2023 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Zardshard
*/
#include "IconEditorApp.h"
#include <new>
#include <stdio.h>
#include <string.h>
#include <Alert.h>
#include <Catalog.h>
#include <FilePanel.h>
#include <FindDirectory.h>
#include <IconEditorProtocol.h>
#include <Locale.h>
#include <Message.h>
#include <Mime.h>
#include <Path.h>
#include "support_settings.h"
#include "AutoLocker.h"
#include "Defines.h"
#include "MainWindow.h"
#include "SavePanel.h"
#include "ShapeListView.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Icon-O-Matic-Main"
using std::nothrow;
static const char* kAppSig = "application/x-vnd.haiku-icon_o_matic";
static const float kWindowOffset = 20;
IconEditorApp::IconEditorApp()
:
BApplication(kAppSig),
fWindowCount(0),
fLastWindowFrame(50, 50, 900, 750),
fOpenPanel(NULL),
fSavePanel(NULL),
fLastOpenPath(""),
fLastSavePath(""),
fLastExportPath("")
{
// create file panels
BMessenger messenger(this, this);
BMessage message(B_REFS_RECEIVED);
fOpenPanel = new BFilePanel(B_OPEN_PANEL, &messenger, NULL, B_FILE_NODE,
true, &message);
message.what = MSG_SAVE_AS;
fSavePanel = new SavePanel("save panel", &messenger, NULL, B_FILE_NODE
| B_DIRECTORY_NODE | B_SYMLINK_NODE, false, &message);
_RestoreSettings();
}
IconEditorApp::~IconEditorApp()
{
delete fOpenPanel;
delete fSavePanel;
}
// #pragma mark -
bool
IconEditorApp::QuitRequested()
{
// Run the QuitRequested() hook in each window's own thread. Otherwise
// the BAlert which a window shows when an icon is not saved will not
// repaint the window. (BAlerts check which thread is running Go() and
// will repaint windows when it's a BWindow.)
bool quit = true;
for (int32 i = 0; BWindow* window = WindowAt(i); i++) {
if (!window->Lock())
continue;
// Try to cast the window while the pointer must be valid.
MainWindow* mainWindow = dynamic_cast<MainWindow*>(window);
window->Unlock();
if (mainWindow == NULL)
continue;
BMessenger messenger(window, window);
BMessage reply;
if (messenger.SendMessage(B_QUIT_REQUESTED, &reply) != B_OK)
continue;
bool result;
if (reply.FindBool("result", &result) == B_OK && !result)
quit = false;
}
if (!quit)
return false;
_StoreSettings();
return true;
}
void
IconEditorApp::MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_NEW:
_NewWindow()->Show();
break;
case MSG_OPEN:
{
BMessage openMessage(B_REFS_RECEIVED);
MainWindow* window;
if (message->FindPointer("window", (void**)&window) == B_OK)
openMessage.AddPointer("window", window);
bool referenceImage;
if (message->FindBool("reference image", &referenceImage) == B_OK)
openMessage.AddBool("reference image", referenceImage);
fOpenPanel->SetMessage(&openMessage);
fOpenPanel->Show();
break;
}
case MSG_APPEND:
{
MainWindow* window;
if (message->FindPointer("window", (void**)&window) != B_OK)
break;
BMessage openMessage(B_REFS_RECEIVED);
openMessage.AddBool("append", true);
openMessage.AddPointer("window", window);
fOpenPanel->SetMessage(&openMessage);
fOpenPanel->Show();
break;
}
case B_EDIT_ICON_DATA:
{
BMessenger messenger;
if (message->FindMessenger("reply to", &messenger) < B_OK) {
// required
break;
}
const uint8* data;
ssize_t size;
if (message->FindData("icon data", B_VECTOR_ICON_TYPE,
(const void**)&data, &size) < B_OK) {
// optional (new icon will be created)
data = NULL;
size = 0;
}
MainWindow* window = _NewWindow();
window->Open(messenger, data, size);
window->Show();
break;
}
case MSG_SAVE_AS:
case MSG_EXPORT_AS:
{
BMessenger messenger;
if (message->FindMessenger("target", &messenger) != B_OK)
break;
fSavePanel->SetExportMode(message->what == MSG_EXPORT_AS);
// fSavePanel->Refresh();
const char* saveText;
if (message->FindString("save text", &saveText) == B_OK)
fSavePanel->SetSaveText(saveText);
fSavePanel->SetTarget(messenger);
fSavePanel->Show();
break;
}
case MSG_WINDOW_CLOSED:
{
fWindowCount--;
if (fWindowCount == 0)
PostMessage(B_QUIT_REQUESTED);
BMessage settings;
if (message->FindMessage("settings", &settings) == B_OK)
fLastWindowSettings = settings;
BRect frame;
if (message->FindRect("window frame", &frame) == B_OK) {
fLastWindowFrame = frame;
fLastWindowFrame.OffsetBy(-kWindowOffset, -kWindowOffset);
}
break;
}
default:
BApplication::MessageReceived(message);
break;
}
}
void
IconEditorApp::ReadyToRun()
{
// create main window
if (fWindowCount == 0)
_NewWindow()->Show();
_InstallDocumentMimeType();
}
void
IconEditorApp::RefsReceived(BMessage* message)
{
bool append;
if (message->FindBool("append", &append) != B_OK)
append = false;
bool referenceImage;
if (message->FindBool("reference image", &referenceImage) != B_OK)
referenceImage = false;
MainWindow* window;
if (message->FindPointer("window", (void**)&window) != B_OK)
window = NULL;
// When appending, we need to know a window.
if (append && window == NULL)
return;
entry_ref ref;
if (append || referenceImage) {
if (!window->Lock())
return;
for (int32 i = 0; message->FindRef("refs", i, &ref) == B_OK; i++) {
if (append)
window->Open(ref, true);
if (referenceImage)
window->AddReferenceImage(ref);
}
window->Unlock();
} else {
for (int32 i = 0; message->FindRef("refs", i, &ref) == B_OK; i++) {
if (window != NULL && i == 0) {
window->Lock();
window->Open(ref, false);
window->Unlock();
} else {
window = _NewWindow();
window->Open(ref, false);
window->Show();
}
}
}
if (fOpenPanel != NULL && fSavePanel != NULL)
_SyncPanels(fOpenPanel, fSavePanel);
}
void
IconEditorApp::ArgvReceived(int32 argc, char** argv)
{
if (argc < 2)
return;
entry_ref ref;
for (int32 i = 1; i < argc; i++) {
if (get_ref_for_path(argv[i], &ref) == B_OK) {
MainWindow* window = _NewWindow();
window->Open(ref);
window->Show();
}
}
}
// #pragma mark -
MainWindow*
IconEditorApp::_NewWindow()
{
fLastWindowFrame.OffsetBy(kWindowOffset, kWindowOffset);
MainWindow* window = new MainWindow(fLastWindowFrame, this,
&fLastWindowSettings);
fWindowCount++;
return window;
}
void
IconEditorApp::_SyncPanels(BFilePanel* from, BFilePanel* to)
{
if (from->Window()->Lock()) {
// location
if (to->Window()->Lock()) {
BRect frame = from->Window()->Frame();
to->Window()->MoveTo(frame.left, frame.top);
to->Window()->ResizeTo(frame.Width(), frame.Height());
to->Window()->Unlock();
}
// current folder
entry_ref panelDir;
from->GetPanelDirectory(&panelDir);
to->SetPanelDirectory(&panelDir);
from->Window()->Unlock();
}
}
const char*
IconEditorApp::_LastFilePath(path_kind which)
{
const char* path = NULL;
switch (which) {
case LAST_PATH_OPEN:
if (fLastOpenPath.Length() > 0)
path = fLastOpenPath.String();
else if (fLastSavePath.Length() > 0)
path = fLastSavePath.String();
else if (fLastExportPath.Length() > 0)
path = fLastExportPath.String();
break;
case LAST_PATH_SAVE:
if (fLastSavePath.Length() > 0)
path = fLastSavePath.String();
else if (fLastExportPath.Length() > 0)
path = fLastExportPath.String();
else if (fLastOpenPath.Length() > 0)
path = fLastOpenPath.String();
break;
case LAST_PATH_EXPORT:
if (fLastExportPath.Length() > 0)
path = fLastExportPath.String();
else if (fLastSavePath.Length() > 0)
path = fLastSavePath.String();
else if (fLastOpenPath.Length() > 0)
path = fLastOpenPath.String();
break;
}
if (path == NULL) {
BPath homePath;
if (find_directory(B_USER_DIRECTORY, &homePath) == B_OK)
path = homePath.Path();
else
path = "/boot/home";
}
return path;
}
// #pragma mark -
void
IconEditorApp::_StoreSettings()
{
BMessage settings('stns');
settings.AddRect("window frame", fLastWindowFrame);
settings.AddMessage("window settings", &fLastWindowSettings);
settings.AddInt32("export mode", fSavePanel->ExportMode());
save_settings(&settings, "Icon-O-Matic");
}
void
IconEditorApp::_RestoreSettings()
{
BMessage settings('stns');
load_settings(&settings, "Icon-O-Matic");
BRect frame;
if (settings.FindRect("window frame", &frame) == B_OK) {
fLastWindowFrame = frame;
// Compensate offset for next window...
fLastWindowFrame.OffsetBy(-kWindowOffset, -kWindowOffset);
}
BMessage lastSettings;
if (settings.FindMessage("window settings", &lastSettings)
== B_OK) {
fLastWindowSettings = lastSettings;
}
int32 mode;
if (settings.FindInt32("export mode", &mode) >= B_OK)
fSavePanel->SetExportMode(mode);
}
void
IconEditorApp::_InstallDocumentMimeType()
{
// install mime type of documents
BMimeType mime(kNativeIconMimeType);
status_t ret = mime.InitCheck();
if (ret < B_OK) {
fprintf(stderr, "Could not init native document mime type (%s): %s.\n",
kNativeIconMimeType, strerror(ret));
return;
}
if (mime.IsInstalled() && !(modifiers() & B_SHIFT_KEY)) {
// mime is already installed, and the user is not
// pressing the shift key to force a re-install
return;
}
ret = mime.Install();
if (ret < B_OK) {
fprintf(stderr, "Could not install native document mime type (%s): "
"%s.\n", kNativeIconMimeType, strerror(ret));
return;
}
// set preferred app
ret = mime.SetPreferredApp(kAppSig);
if (ret < B_OK)
fprintf(stderr, "Could not set native document preferred app: %s\n",
strerror(ret));
// set descriptions
ret = mime.SetShortDescription("Haiku Icon");
if (ret < B_OK)
fprintf(stderr, "Could not set short description of mime type: %s\n",
strerror(ret));
ret = mime.SetLongDescription("Native Haiku vector icon");
if (ret < B_OK)
fprintf(stderr, "Could not set long description of mime type: %s\n",
strerror(ret));
// set extensions
BMessage message('extn');
message.AddString("extensions", "icon");
ret = mime.SetFileExtensions(&message);
if (ret < B_OK)
fprintf(stderr, "Could not set extensions of mime type: %s\n",
strerror(ret));
// set sniffer rule
const char* snifferRule = "0.9 ('IMSG')";
ret = mime.SetSnifferRule(snifferRule);
if (ret < B_OK) {
BString parseError;
BMimeType::CheckSnifferRule(snifferRule, &parseError);
fprintf(stderr, "Could not set sniffer rule of mime type: %s\n",
parseError.String());
}
}