-
Notifications
You must be signed in to change notification settings - Fork 3
/
STLApp.cpp
202 lines (177 loc) · 4.94 KB
/
STLApp.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
/* STLover - A powerful tool for viewing and manipulating 3D STL models
* Copyright (C) 2020 Gerasim Troeglazov <3dEyes@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "STLApp.h"
#include "STLWindow.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "STLoverApplication"
STLoverApplication::STLoverApplication() : BApplication(APP_SIGNATURE),
lastActivatedWindow(NULL)
{
InstallMimeType();
}
STLWindow*
STLoverApplication::CreateWindow(void)
{
STLWindow *activeWindow = NULL;
STLWindow *lastWindow = NULL;
for (int32 i = 0; i < CountWindows(); i++) {
STLWindow* window = dynamic_cast<STLWindow*>(WindowAt(i));
if (window != NULL) {
if (window == lastActivatedWindow) {
activeWindow = window;
break;
}
lastWindow = window;
}
}
STLWindow *stlWindow = new STLWindow();
if (activeWindow != NULL ) {
BWindowStack stack(activeWindow);
stack.AddWindow(stlWindow);
} else if (lastWindow != NULL ) {
BWindowStack stack(lastWindow);
stack.AddWindow(stlWindow);
}
stlWindow->Show();
return stlWindow;
}
void
STLoverApplication::MessageReceived(BMessage *message)
{
switch (message->what) {
case MSG_WINDOW_ACTIVATED:
{
void *winPtr = NULL;
if (message->FindPointer("window", &winPtr) == B_OK)
lastActivatedWindow = static_cast<STLWindow*>(winPtr);
break;
}
case MSG_WINDOW_CLOSED:
{
lastActivatedWindow = NULL;
for (int32 i = CountWindows() - 1; i >= 0 ; i--) {
STLWindow* window = dynamic_cast<STLWindow*>(WindowAt(i));
if (window != NULL) {
lastActivatedWindow = window;
window->Activate();
break;
}
}
if (lastActivatedWindow == NULL)
PostMessage(B_QUIT_REQUESTED);
break;
}
default:
BApplication::MessageReceived(message);
break;
}
}
void
STLoverApplication::RefsReceived(BMessage* message)
{
for (int32 i = 0; i < CountWindows(); i++) {
STLWindow* window = dynamic_cast<STLWindow*>(WindowAt(i));
if (window != NULL) {
if (!window->IsLoaded() && !window->IsLoading()) {
window->PostMessage(message);
return;
}
}
}
STLWindow *stlWindow = CreateWindow();
stlWindow->PostMessage(message);
}
void
STLoverApplication::ReadyToRun()
{
if (CountWindows() == 0)
CreateWindow();
}
void
STLoverApplication::ArgvReceived(int32 argc, char** argv)
{
BMessage *message = NULL;
for (int32 i = 1; i < argc; i++) {
entry_ref ref;
status_t err = get_ref_for_path(argv[i], &ref);
if (err == B_OK) {
if (!message) {
message = new BMessage;
message->what = B_REFS_RECEIVED;
}
message->AddRef("refs", &ref);
}
}
if (message) {
RefsReceived(message);
delete message;
}
}
void
STLoverApplication::InstallMimeType(void)
{
BMimeType mime(STL_SIGNATURE);
status_t ret = mime.InitCheck();
if (ret != B_OK)
return;
ret = mime.Install();
if (ret != B_OK && ret != B_FILE_EXISTS)
return;
mime.SetShortDescription(B_TRANSLATE("STL file"));
mime.SetLongDescription(B_TRANSLATE("A file format native to the STereoLithography CAD software"));
BMessage message('extn');
message.AddString("extensions", "stl");
mime.SetFileExtensions(&message);
BResources* resources = AppResources();
if (resources != NULL) {
size_t size;
const void* iconData = resources->LoadResource(B_VECTOR_ICON_TYPE, "BEOS:" STL_SIGNATURE,
&size);
if (iconData != NULL && size > 0) {
mime.SetIcon(reinterpret_cast<const uint8*>(iconData), size);
}
}
}
BBitmap *
STLoverApplication::GetIcon(const char *iconName, int iconSize)
{
if (iconName == NULL) {
app_info inf;
be_app->GetAppInfo(&inf);
BFile file(&inf.ref, B_READ_ONLY);
BAppFileInfo appMime(&file);
if (appMime.InitCheck() != B_OK)
return NULL;
BBitmap* icon = new BBitmap(BRect(0, 0, iconSize - 1, iconSize -1), B_RGBA32);
if (appMime.GetIcon(icon, (icon_size)iconSize) == B_OK)
return icon;
delete icon;
return NULL;
} else {
BResources* resources = AppResources();
if (resources != NULL) {
size_t size;
const void* iconData = resources->LoadResource(B_VECTOR_ICON_TYPE, iconName, &size);
if (iconData != NULL && size > 0) {
BBitmap* bitmap = new BBitmap(BRect(0, 0, iconSize - 1, iconSize - 1), B_RGBA32);
status_t status = BIconUtils::GetVectorIcon((uint8*)iconData, size, bitmap);
if (status == B_OK)
return bitmap;
}
}
return NULL;
}
}