This repository was archived by the owner on Aug 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBSCWindow.cpp
More file actions
444 lines (383 loc) · 11.4 KB
/
BSCWindow.cpp
File metadata and controls
444 lines (383 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
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
/*
* Copyright 2013-2023, Stefano Ceccherini <stefano.ceccherini@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "BSCWindow.h"
#define USE_INFOVIEW 0
#include "AdvancedOptionsView.h"
#include "BSCApp.h"
#include "CamStatusView.h"
#include "ControllerObserver.h"
#if USE_INFOVIEW
#include "InfoView.h"
#endif
#include "OutputView.h"
#include "PublicMessages.h"
#include "Settings.h"
#include <Alert.h>
#include <Button.h>
#include <Catalog.h>
#include <Debug.h>
#include <GroupLayoutBuilder.h>
#include <LayoutBuilder.h>
#include <MenuBar.h>
#include <Roster.h>
#include <Screen.h>
#include <String.h>
#include <TabView.h>
#include <cstdio>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "BSCWindow"
const static BRect kWindowRect(0, 0, 400, 600);
const static uint32 kGUIDockWindow = 'j90d';
const static uint32 kGUIResetSettings = 'j91d';
const static uint32 kGUIShowHelp = 'j92d';
const char* LABEL_START = B_TRANSLATE("Start");
const char* LABEL_STOP = B_TRANSLATE("Stop");
const char* LABEL_PAUSE = B_TRANSLATE("Pause");
const char* LABEL_RESUME = B_TRANSLATE("Resume");
BSCWindow::BSCWindow()
:
BDirectWindow(kWindowRect, B_TRANSLATE_SYSTEM_NAME("BeScreenCapture"), B_TITLED_WINDOW,
B_ASYNCHRONOUS_CONTROLS|B_NOT_RESIZABLE|B_NOT_ZOOMABLE),
fMenuBar(NULL),
fStartStopButton(NULL),
fCamStatus(NULL),
fOutputView(NULL),
fAdvancedOptionsView(NULL),
fInfoView(NULL)
{
fOutputView = new OutputView();
fAdvancedOptionsView = new AdvancedOptionsView();
#if USE_INFOVIEW
fInfoView = new InfoView();
#endif
fMenuBar = new BMenuBar("menubar");
_BuildMenu();
fStartStopButton = new BButton("start", LABEL_START,
new BMessage(kMsgGUIToggleCapture));
fStartStopButton->SetTarget(be_app);
fStartStopButton->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT, B_ALIGN_MIDDLE));
// TODO: Trying to avoid button shrinking when label changes.
// that won't work with translations, since the "Stop" label could be wider
fStartStopButton->SetExplicitMinSize(fStartStopButton->PreferredSize());
fPauseButton = new BButton("pause", LABEL_PAUSE,
new BMessage(kMsgGUITogglePause));
fPauseButton->SetTarget(be_app);
fPauseButton->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT, B_ALIGN_MIDDLE));
fPauseButton->SetEnabled(false);
fCamStatus = new CamStatusView();
fCamStatus->SetExplicitAlignment(BAlignment(B_ALIGN_LEFT, B_ALIGN_MIDDLE));
_LayoutWindow();
if (be_app->LockLooper()) {
be_app->StartWatching(this, kMsgControllerEncodeStarted);
be_app->StartWatching(this, kMsgControllerEncodeProgress);
be_app->StartWatching(this, kMsgControllerEncodeFinished);
be_app->StartWatching(this, kMsgControllerTargetFrameChanged);
be_app->StartWatching(this, kMsgControllerCaptureStarted);
be_app->StartWatching(this, kMsgControllerCaptureStopped);
be_app->StartWatching(this, kMsgControllerCapturePaused);
be_app->StartWatching(this, kMsgControllerCaptureResumed);
be_app->StartWatching(this, kMsgControllerSelectionWindowClosed);
be_app->UnlockLooper();
}
}
BSCWindow::~BSCWindow()
{
}
void
BSCWindow::DispatchMessage(BMessage *message, BHandler *handler)
{
BDirectWindow::DispatchMessage(message, handler);
}
bool
BSCWindow::QuitRequested()
{
BString reason;
bool canQuit = false;
BSCApp* app = dynamic_cast<BSCApp*>(be_app);
ASSERT(app != NULL);
if (!Settings::Current().WarnOnQuit()) {
app->StopThreads();
canQuit = true;
} else if ((canQuit = app->CanQuit(reason)) != true) {
BString text = B_TRANSLATE("Do you really want to quit BeScreenCapture?\n");
text.Append(" ");
text.Append(reason);
BAlert* alert = new BAlert(B_TRANSLATE("Really quit?"), text,
B_TRANSLATE("Quit"), B_TRANSLATE("Continue"));
alert->SetShortcut(1, B_ESCAPE);
int32 result = alert->Go();
if (result == 0) {
app->StopThreads();
canQuit = true;
}
}
if (canQuit)
be_app->PostMessage(B_QUIT_REQUESTED);
return canQuit && BWindow::QuitRequested();
}
void
BSCWindow::MessageReceived(BMessage *message)
{
BSCApp* app = dynamic_cast<BSCApp*>(be_app);
ASSERT(app != NULL);
switch (message->what) {
case B_ABOUT_REQUESTED:
be_app->PostMessage(B_ABOUT_REQUESTED);
break;
case kGUIDockWindow:
_LayoutWindow();
break;
case kGUIResetSettings:
app->ResetSettings();
break;
case kGUIShowHelp:
app->ShowHelp();
break;
case B_OBSERVER_NOTICE_CHANGE:
{
int32 code;
message->FindInt32(B_OBSERVE_WHAT_CHANGE, &code);
switch (code) {
case kMsgControllerSelectionWindowClosed:
{
if (IsHidden())
Show();
break;
}
case kMsgControllerCaptureStarted:
_CaptureStarted();
break;
case kMsgControllerCaptureStopped:
{
_CaptureFinished();
status_t status = B_OK;
message->FindInt32("status", &status);
if (status != B_OK) {
BString errorString;
errorString.SetToFormat(B_TRANSLATE("Could not record clip:\n%s"),
strerror(status));
(new BAlert(B_TRANSLATE("Capture failed"), errorString,
B_TRANSLATE("OK")))->Go();
fStartStopButton->SetEnabled(true);
}
break;
}
case kMsgControllerCapturePaused:
{
fPauseButton->SetLabel(LABEL_RESUME);
break;
}
case kMsgControllerCaptureResumed:
{
fPauseButton->SetLabel(LABEL_PAUSE);
break;
}
case kMsgControllerEncodeStarted:
fStartStopButton->SetEnabled(false);
break;
case kMsgControllerEncodeFinished:
{
// We're set to quit, bail out
if (Settings::Current().QuitWhenFinished())
break;
fStartStopButton->SetEnabled(true);
status_t status = B_OK;
if (message->FindInt32("status", reinterpret_cast<int32*>(&status)) == B_OK
&& status != B_OK) {
BString errorString;
errorString.SetToFormat(B_TRANSLATE("Could not create clip:\n%s"),
strerror(status));
(new BAlert(B_TRANSLATE("Encoding failed"), errorString,
B_TRANSLATE("OK")))->Go();
} else {
// TODO: Should be asynchronous
const char* destName = NULL;
message->FindString("file_name", &destName);
const BEntry entry(destName);
if (entry.Exists()) {
BString buttonName;
BString successString;
if (entry.IsDirectory()) {
buttonName.SetTo(B_TRANSLATE("Open folder"));
successString.SetTo(B_TRANSLATE("Finished recording"));
} else {
buttonName.SetTo(B_TRANSLATE("Play"));
successString.Append(B_TRANSLATE("Finished recording '%filename%'."));
successString.ReplaceFirst("%filename%", entry.Name());
}
BAlert* alert = new BAlert(B_TRANSLATE("Success"), successString,
B_TRANSLATE("OK"), buttonName.String());
alert->SetShortcut(0, B_ESCAPE);
int32 choice = alert->Go();
if (choice == 1) {
entry_ref ref;
if (entry.GetRef(&ref) == B_OK)
be_roster->Launch(&ref);
}
}
}
if (app->WasLaunchedSilently())
app->PostMessage(B_QUIT_REQUESTED);
break;
}
default:
break;
}
break;
}
default:
BWindow::MessageReceived(message);
break;
}
}
void
BSCWindow::ScreenChanged(BRect screen_size, color_space depth)
{
BDirectWindow::ScreenChanged(screen_size, depth);
}
void
BSCWindow::DirectConnected(direct_buffer_info *info)
{
BDirectWindow::DirectConnected(info);
BSCApp* app = dynamic_cast<BSCApp*>(be_app);
if (app == NULL)
return;
switch (info->buffer_state & B_DIRECT_MODE_MASK) {
case B_DIRECT_START:
case B_DIRECT_MODIFY:
app->UpdateDirectInfo(info);
break;
case B_DIRECT_STOP:
break;
default:
break;
}
}
/* virtual */
void
BSCWindow::MenusBeginning()
{
BWindow::MenusBeginning();
BMenuItem* recordingItem = fMenuBar->FindItem(B_TRANSLATE("Recording"));
if (recordingItem == NULL)
return;
BMenu* menu = recordingItem->Menu();
if (menu == NULL)
return;
BSCApp* app = dynamic_cast<BSCApp*>(be_app);
if (app == NULL)
return;
BMenuItem* start = menu->FindItem(LABEL_START);
BMenuItem* stop = menu->FindItem(LABEL_STOP);
BMenuItem* pause = menu->FindItem(LABEL_PAUSE);
int state = app->State();
if (start != NULL)
start->SetEnabled(state == BSCApp::STATE_IDLE);
if (stop != NULL)
stop->SetEnabled(state == BSCApp::STATE_RECORDING);
if (pause != NULL) {
pause->SetEnabled(state == BSCApp::STATE_RECORDING);
pause->SetMarked(app->Paused());
}
}
void
BSCWindow::_BuildMenu()
{
BMenu* menu = new BMenu(B_TRANSLATE("App"));
BMenuItem* helpItem = new BMenuItem(B_TRANSLATE("Help" B_UTF8_ELLIPSIS),
new BMessage(kGUIShowHelp), 'H');
BMenuItem* aboutItem = new BMenuItem(B_TRANSLATE("About BeScreenCapture"),
new BMessage(B_ABOUT_REQUESTED));
BMenuItem* quitItem = new BMenuItem(B_TRANSLATE("Quit"), new BMessage(B_QUIT_REQUESTED), 'Q');
menu->AddItem(helpItem);
menu->AddItem(aboutItem);
menu->AddSeparatorItem();
menu->AddItem(quitItem);
fMenuBar->AddItem(menu);
menu = new BMenu(B_TRANSLATE("Recording"));
BMenuItem* startRecording = new BMenuItem(LABEL_START,
new BMessage(kMsgGUIToggleCapture), 'S');
startRecording->SetTarget(be_app);
BMenuItem* stopRecording = new BMenuItem(LABEL_STOP,
new BMessage(kMsgGUIToggleCapture), 'T');
stopRecording->SetTarget(be_app);
BMenuItem* pauseRecording = new BMenuItem(LABEL_PAUSE,
new BMessage(kMsgGUITogglePause), 'P');
pauseRecording->SetTarget(be_app);
menu->AddItem(startRecording);
menu->AddItem(stopRecording);
menu->AddSeparatorItem();
menu->AddItem(pauseRecording);
fMenuBar->AddItem(menu);
menu = new BMenu(B_TRANSLATE("Settings"));
BMenuItem* resetSettings = new BMenuItem(B_TRANSLATE("Reset to defaults"),
new BMessage(kGUIResetSettings));
menu->AddItem(resetSettings);
fMenuBar->AddItem(menu);
}
void
BSCWindow::_LayoutWindow()
{
SetFlags((Flags() | B_AUTO_UPDATE_SIZE_LIMITS) & ~(B_NOT_MOVABLE));
BTabView* tabView = new BTabView("tabview", B_WIDTH_FROM_LABEL);
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
.Add(fMenuBar)
.AddGroup(B_VERTICAL)
.SetInsets(-2, B_USE_SMALL_SPACING, -2, 0)
.Add(tabView)
.End()
.AddGroup(B_HORIZONTAL)
.Add(fCamStatus)
.Add(fPauseButton)
.Add(fStartStopButton)
.SetInsets(B_USE_DEFAULT_SPACING, 0)
.End();
BGroupView* outputGroup = new BGroupView(B_HORIZONTAL);
outputGroup->SetName(B_TRANSLATE("Capture"));
outputGroup->GroupLayout()->SetInsets(B_USE_DEFAULT_SPACING);
tabView->AddTab(outputGroup);
BLayoutBuilder::Group<>(outputGroup)
.Add(fOutputView);
BGroupView* advancedGroup = new BGroupView(B_HORIZONTAL);
advancedGroup->SetName(B_TRANSLATE("Options"));
advancedGroup->GroupLayout()->SetInsets(B_USE_DEFAULT_SPACING);
tabView->AddTab(advancedGroup);
BLayoutBuilder::Group<>(advancedGroup)
.Add(fAdvancedOptionsView);
if (fInfoView != NULL) {
BGroupView* infoGroup = new BGroupView(B_HORIZONTAL);
infoGroup->SetName(B_TRANSLATE("Info"));
infoGroup->GroupLayout()->SetInsets(B_USE_DEFAULT_SPACING);
tabView->AddTab(infoGroup);
BLayoutBuilder::Group<>(infoGroup)
.Add(fInfoView);
}
CenterOnScreen();
}
status_t
BSCWindow::_CaptureStarted()
{
const Settings& settings = Settings::Current();
if (settings.MinimizeOnRecording())
Hide();
fStartStopButton->SetLabel(LABEL_STOP);
fPauseButton->SetLabel(LABEL_PAUSE);
fPauseButton->SetEnabled(true);
return B_OK;
}
status_t
BSCWindow::_CaptureFinished()
{
fStartStopButton->SetLabel(LABEL_START);
fPauseButton->SetLabel(LABEL_PAUSE);
fPauseButton->SetEnabled(false);
// TODO: maybe don't show window if launched with
// the Shift-Alt-Control-R combo
if (IsHidden())
Show();
if (IsMinimized())
Minimize(false);
return B_OK;
}