-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
HWindow.cpp
455 lines (383 loc) · 10.1 KB
/
HWindow.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
/*
* Copyright 2003-2008, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Jérôme Duval
* Oliver Ruiz Dorantes
* Atsushi Takamatsu
*/
#include "HWindow.h"
#include "HEventList.h"
#include <stdio.h>
#include <Alert.h>
#include <Application.h>
#include <Beep.h>
#include <Box.h>
#include <Button.h>
#include <Catalog.h>
#include <ControlLook.h>
#include <FindDirectory.h>
#include <fs_attr.h>
#include <GroupLayoutBuilder.h>
#include <Locale.h>
#include <MediaFiles.h>
#include <MenuBar.h>
#include <MenuField.h>
#include <MenuItem.h>
#include <Node.h>
#include <NodeInfo.h>
#include <Path.h>
#include <Roster.h>
#include <ScrollView.h>
#include <StringView.h>
#include <Sound.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "HWindow"
static const char kSettingsFile[] = "Sounds_Settings";
HWindow::HWindow(BRect rect, const char* name)
:
BWindow(rect, name, B_TITLED_WINDOW, B_AUTO_UPDATE_SIZE_LIMITS),
fFilePanel(NULL),
fPlayer(NULL)
{
InitGUI();
fFilePanel = new BFilePanel();
fFilePanel->SetTarget(this);
BPath path;
BMessage msg;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
path.Append(kSettingsFile);
BFile file(path.Path(), B_READ_ONLY);
if (file.InitCheck() == B_OK && msg.Unflatten(&file) == B_OK
&& msg.FindRect("frame", &fFrame) == B_OK) {
MoveTo(fFrame.LeftTop());
ResizeTo(fFrame.Width(), fFrame.Height());
}
}
}
HWindow::~HWindow()
{
delete fFilePanel;
delete fPlayer;
BPath path;
BMessage msg;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) {
path.Append(kSettingsFile);
BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE);
if (file.InitCheck() == B_OK) {
msg.AddRect("frame", fFrame);
msg.Flatten(&file);
}
}
}
void
HWindow::InitGUI()
{
fEventList = new HEventList();
fEventList->SetType(BMediaFiles::B_SOUNDS);
fEventList->SetSelectionMode(B_SINGLE_SELECTION_LIST);
BGroupView* view = new BGroupView();
BBox* box = new BBox("", B_WILL_DRAW | B_FRAME_EVENTS
| B_NAVIGABLE_JUMP | B_PULSE_NEEDED);
BMenu* menu = new BMenu("file");
menu->SetRadioMode(true);
menu->SetLabelFromMarked(true);
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem(B_TRANSLATE("<none>"),
new BMessage(M_NONE_MESSAGE)));
menu->AddItem(new BMenuItem(B_TRANSLATE("Other" B_UTF8_ELLIPSIS),
new BMessage(M_OTHER_MESSAGE)));
BString label(B_TRANSLATE("Sound file:"));
BMenuField* menuField = new BMenuField("filemenu", label, menu);
menuField->SetDivider(menuField->StringWidth(label) + 10);
BButton* stopbutton = new BButton("stop", B_TRANSLATE("Stop"),
new BMessage(M_STOP_MESSAGE));
stopbutton->SetEnabled(false);
BButton* playbutton = new BButton("play", B_TRANSLATE("Play"),
new BMessage(M_PLAY_MESSAGE));
playbutton->SetEnabled(false);
const float kInset = be_control_look->DefaultItemSpacing();
view->SetLayout(new BGroupLayout(B_HORIZONTAL));
view->AddChild(BGroupLayoutBuilder(B_VERTICAL, kInset)
.AddGroup(B_HORIZONTAL)
.Add(menuField)
.AddGlue()
.End()
.AddGroup(B_HORIZONTAL, kInset)
.AddGlue()
.Add(playbutton)
.Add(stopbutton)
.End()
.SetInsets(kInset, kInset, kInset, kInset)
);
box->AddChild(view);
SetLayout(new BGroupLayout(B_HORIZONTAL));
AddChild(BGroupLayoutBuilder(B_VERTICAL)
.AddGroup(B_VERTICAL, kInset)
.Add(fEventList)
.Add(box)
.End()
.SetInsets(kInset, kInset, kInset, kInset)
);
// setup file menu
SetupMenuField();
BMenuItem* noneItem = menu->FindItem(B_TRANSLATE("<none>"));
if (noneItem != NULL)
noneItem->SetMarked(true);
}
void
HWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case M_OTHER_MESSAGE:
{
BMenuField* menufield
= dynamic_cast<BMenuField*>(FindView("filemenu"));
if (menufield == NULL)
return;
BMenu* menu = menufield->Menu();
HEventRow* row = (HEventRow*)fEventList->CurrentSelection();
if (row != NULL) {
BPath path(row->Path());
if (path.InitCheck() != B_OK) {
BMenuItem* item = menu->FindItem(B_TRANSLATE("<none>"));
if (item != NULL)
item->SetMarked(true);
} else {
BMenuItem* item = menu->FindItem(path.Leaf());
if (item != NULL)
item->SetMarked(true);
}
}
fFilePanel->Show();
break;
}
case B_SIMPLE_DATA:
case B_REFS_RECEIVED:
{
entry_ref ref;
HEventRow* row = (HEventRow*)fEventList->CurrentSelection();
if (message->FindRef("refs", &ref) == B_OK && row != NULL) {
BMenuField* menufield
= dynamic_cast<BMenuField*>(FindView("filemenu"));
if (menufield == NULL)
return;
BMenu* menu = menufield->Menu();
// check audio file
BNode node(&ref);
BNodeInfo ninfo(&node);
char type[B_MIME_TYPE_LENGTH + 1];
ninfo.GetType(type);
BMimeType mtype(type);
BMimeType superType;
mtype.GetSupertype(&superType);
if (superType.Type() == NULL
|| strcmp(superType.Type(), "audio") != 0) {
beep();
BAlert* alert = new BAlert("",
B_TRANSLATE("This is not an audio file."),
B_TRANSLATE("OK"), NULL, NULL,
B_WIDTH_AS_USUAL, B_STOP_ALERT);
alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
alert->Go();
break;
}
// add file item
BMessage* msg = new BMessage(M_ITEM_MESSAGE);
BPath path(&ref);
msg->AddRef("refs", &ref);
BMenuItem* menuitem = menu->FindItem(path.Leaf());
if (menuitem == NULL)
menu->AddItem(menuitem = new BMenuItem(path.Leaf(), msg), 0);
// refresh item
fEventList->SetPath(BPath(&ref).Path());
// check file menu
if (menuitem != NULL)
menuitem->SetMarked(true);
}
break;
}
case M_PLAY_MESSAGE:
{
HEventRow* row = (HEventRow*)fEventList->CurrentSelection();
if (row != NULL) {
const char* path = row->Path();
if (path != NULL) {
entry_ref ref;
::get_ref_for_path(path, &ref);
delete fPlayer;
fPlayer = new BFileGameSound(&ref, false);
fPlayer->StartPlaying();
}
}
break;
}
case M_STOP_MESSAGE:
{
if (fPlayer == NULL)
break;
if (fPlayer->IsPlaying()) {
fPlayer->StopPlaying();
delete fPlayer;
fPlayer = NULL;
}
break;
}
case M_EVENT_CHANGED:
{
const char* path;
BMenuField* menufield
= dynamic_cast<BMenuField*>(FindView("filemenu"));
if (menufield == NULL)
return;
BMenu* menu = menufield->Menu();
if (message->FindString("path", &path) == B_OK) {
BPath path(path);
if (path.InitCheck() != B_OK) {
BMenuItem* item = menu->FindItem(B_TRANSLATE("<none>"));
if (item != NULL)
item->SetMarked(true);
} else {
BMenuItem* item = menu->FindItem(path.Leaf());
if (item != NULL)
item->SetMarked(true);
}
}
break;
}
case M_ITEM_MESSAGE:
{
entry_ref ref;
if (message->FindRef("refs", &ref) == B_OK)
fEventList->SetPath(BPath(&ref).Path());
break;
}
case M_NONE_MESSAGE:
{
fEventList->SetPath(NULL);
break;
}
default:
BWindow::MessageReceived(message);
}
}
void
HWindow::SetupMenuField()
{
BMenuField* menufield = dynamic_cast<BMenuField*>(FindView("filemenu"));
if (menufield == NULL)
return;
BMenu* menu = menufield->Menu();
int32 count = fEventList->CountRows();
for (int32 i = 0; i < count; i++) {
HEventRow* row = (HEventRow*)fEventList->RowAt(i);
if (row == NULL)
continue;
BPath path(row->Path());
if (path.InitCheck() != B_OK)
continue;
if (menu->FindItem(path.Leaf()))
continue;
BMessage* msg = new BMessage(M_ITEM_MESSAGE);
entry_ref ref;
::get_ref_for_path(path.Path(), &ref);
msg->AddRef("refs", &ref);
menu->AddItem(new BMenuItem(path.Leaf(), msg), 0);
}
BPath path;
BDirectory dir;
BEntry entry;
BPath item_path;
status_t err = find_directory(B_BEOS_SOUNDS_DIRECTORY, &path);
if (err == B_OK)
err = dir.SetTo(path.Path());
while (err == B_OK) {
err = dir.GetNextEntry(&entry, true);
if (entry.InitCheck() != B_NO_ERROR)
break;
entry.GetPath(&item_path);
if (menu->FindItem(item_path.Leaf()))
continue;
BMessage* msg = new BMessage(M_ITEM_MESSAGE);
entry_ref ref;
::get_ref_for_path(item_path.Path(), &ref);
msg->AddRef("refs", &ref);
menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0);
}
err = find_directory(B_USER_SOUNDS_DIRECTORY, &path);
if (err == B_OK)
err = dir.SetTo(path.Path());
while (err == B_OK) {
err = dir.GetNextEntry(&entry, true);
if (entry.InitCheck() != B_NO_ERROR)
break;
entry.GetPath(&item_path);
if (menu->FindItem(item_path.Leaf()))
continue;
BMessage* msg = new BMessage(M_ITEM_MESSAGE);
entry_ref ref;
::get_ref_for_path(item_path.Path(), &ref);
msg->AddRef("refs", &ref);
menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0);
}
err = find_directory(B_COMMON_SOUNDS_DIRECTORY, &path);
if (err == B_OK)
err = dir.SetTo(path.Path());
while (err == B_OK) {
err = dir.GetNextEntry(&entry, true);
if (entry.InitCheck() != B_NO_ERROR)
break;
entry.GetPath(&item_path);
if (menu->FindItem(item_path.Leaf()))
continue;
BMessage* msg = new BMessage(M_ITEM_MESSAGE);
entry_ref ref;
::get_ref_for_path(item_path.Path(), &ref);
msg->AddRef("refs", &ref);
menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0);
}
}
void
HWindow::Pulse()
{
HEventRow* row = (HEventRow*)fEventList->CurrentSelection();
BMenuField* menufield = dynamic_cast<BMenuField*>(FindView("filemenu"));
BButton* button = dynamic_cast<BButton*>(FindView("play"));
BButton* stop = dynamic_cast<BButton*>(FindView("stop"));
if (menufield == NULL || button == NULL || stop == NULL)
return;
if (row != NULL) {
menufield->SetEnabled(true);
const char* path = row->Path();
if (path != NULL && strcmp(path, ""))
button->SetEnabled(true);
else
button->SetEnabled(false);
} else {
menufield->SetEnabled(false);
button->SetEnabled(false);
}
if (fPlayer != NULL) {
if (fPlayer->IsPlaying())
stop->SetEnabled(true);
else
stop->SetEnabled(false);
} else
stop->SetEnabled(false);
}
void
HWindow::DispatchMessage(BMessage* message, BHandler* handler)
{
if (message->what == B_PULSE)
Pulse();
BWindow::DispatchMessage(message, handler);
}
bool
HWindow::QuitRequested()
{
fFrame = Frame();
fEventList->RemoveAll();
be_app->PostMessage(B_QUIT_REQUESTED);
return true;
}