2 * Copyright 2003-2008, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
13 #include "HEventList.h"
18 #include <Application.h>
23 #include <ControlLook.h>
24 #include <FindDirectory.h>
26 #include <LayoutBuilder.h>
28 #include <MediaFiles.h>
30 #include <MenuField.h>
36 #include <ScrollView.h>
37 #include <StringView.h>
41 #undef B_TRANSLATION_CONTEXT
42 #define B_TRANSLATION_CONTEXT "HWindow"
44 static const char kSettingsFile
[] = "Sounds_Settings";
47 HWindow::HWindow(BRect rect
, const char* name
)
49 BWindow(rect
, name
, B_TITLED_WINDOW
, B_AUTO_UPDATE_SIZE_LIMITS
),
55 fFilePanel
= new BFilePanel();
56 fFilePanel
->SetTarget(this);
59 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &path
) == B_OK
) {
60 path
.Append(kSettingsFile
);
61 BFile
file(path
.Path(), B_READ_ONLY
);
64 if (file
.InitCheck() == B_OK
&& msg
.Unflatten(&file
) == B_OK
65 && msg
.FindRect("frame", &fFrame
) == B_OK
) {
66 MoveTo(fFrame
.LeftTop());
67 ResizeTo(fFrame
.Width(), fFrame
.Height());
82 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &path
) == B_OK
) {
83 path
.Append(kSettingsFile
);
84 BFile
file(path
.Path(), B_WRITE_ONLY
| B_CREATE_FILE
);
86 if (file
.InitCheck() == B_OK
) {
87 msg
.AddRect("frame", fFrame
);
95 HWindow::DispatchMessage(BMessage
* message
, BHandler
* handler
)
97 if (message
->what
== B_PULSE
)
99 BWindow::DispatchMessage(message
, handler
);
104 HWindow::MessageReceived(BMessage
* message
)
106 switch (message
->what
) {
107 case M_OTHER_MESSAGE
:
109 BMenuField
* menufield
110 = dynamic_cast<BMenuField
*>(FindView("filemenu"));
111 if (menufield
== NULL
)
113 BMenu
* menu
= menufield
->Menu();
115 HEventRow
* row
= (HEventRow
*)fEventList
->CurrentSelection();
117 BPath
path(row
->Path());
118 if (path
.InitCheck() != B_OK
) {
119 BMenuItem
* item
= menu
->FindItem(B_TRANSLATE("<none>"));
121 item
->SetMarked(true);
123 BMenuItem
* item
= menu
->FindItem(path
.Leaf());
125 item
->SetMarked(true);
133 case B_REFS_RECEIVED
:
136 HEventRow
* row
= (HEventRow
*)fEventList
->CurrentSelection();
137 if (message
->FindRef("refs", &ref
) == B_OK
&& row
!= NULL
) {
138 BMenuField
* menufield
139 = dynamic_cast<BMenuField
*>(FindView("filemenu"));
140 if (menufield
== NULL
)
142 BMenu
* menu
= menufield
->Menu();
146 BNodeInfo
ninfo(&node
);
147 char type
[B_MIME_TYPE_LENGTH
+ 1];
149 BMimeType
mtype(type
);
151 mtype
.GetSupertype(&superType
);
152 if (superType
.Type() == NULL
153 || strcmp(superType
.Type(), "audio") != 0) {
155 BAlert
* alert
= new BAlert("",
156 B_TRANSLATE("This is not an audio file."),
157 B_TRANSLATE("OK"), NULL
, NULL
,
158 B_WIDTH_AS_USUAL
, B_STOP_ALERT
);
159 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
165 BMessage
* msg
= new BMessage(M_ITEM_MESSAGE
);
167 msg
->AddRef("refs", &ref
);
168 BMenuItem
* menuitem
= menu
->FindItem(path
.Leaf());
169 if (menuitem
== NULL
)
170 menu
->AddItem(menuitem
= new BMenuItem(path
.Leaf(), msg
), 0);
172 fEventList
->SetPath(BPath(&ref
).Path());
174 if (menuitem
!= NULL
)
175 menuitem
->SetMarked(true);
182 HEventRow
* row
= (HEventRow
*)fEventList
->CurrentSelection();
184 const char* path
= row
->Path();
187 ::get_ref_for_path(path
, &ref
);
189 fPlayer
= new BFileGameSound(&ref
, false);
190 fPlayer
->StartPlaying();
200 if (fPlayer
->IsPlaying()) {
201 fPlayer
->StopPlaying();
208 case M_EVENT_CHANGED
:
211 BMenuField
* menufield
212 = dynamic_cast<BMenuField
*>(FindView("filemenu"));
213 if (menufield
== NULL
)
215 BMenu
* menu
= menufield
->Menu();
217 if (message
->FindString("path", &path
) == B_OK
) {
219 if (path
.InitCheck() != B_OK
) {
220 BMenuItem
* item
= menu
->FindItem(B_TRANSLATE("<none>"));
222 item
->SetMarked(true);
224 BMenuItem
* item
= menu
->FindItem(path
.Leaf());
226 item
->SetMarked(true);
229 HEventRow
* row
= (HEventRow
*)fEventList
->CurrentSelection();
230 BButton
* button
= dynamic_cast<BButton
*>(FindView("play"));
232 menufield
->SetEnabled(true);
234 const char* path
= row
->Path();
235 if (path
!= NULL
&& strcmp(path
, ""))
236 button
->SetEnabled(true);
238 button
->SetEnabled(false);
240 menufield
->SetEnabled(false);
241 button
->SetEnabled(false);
250 if (message
->FindRef("refs", &ref
) == B_OK
) {
251 fEventList
->SetPath(BPath(&ref
).Path());
259 fEventList
->SetPath(NULL
);
264 BWindow::MessageReceived(message
);
270 HWindow::QuitRequested()
274 fEventList
->RemoveAll();
275 be_app
->PostMessage(B_QUIT_REQUESTED
);
283 fEventList
= new HEventList();
284 fEventList
->SetType(BMediaFiles::B_SOUNDS
);
285 fEventList
->SetSelectionMode(B_SINGLE_SELECTION_LIST
);
287 BMenu
* menu
= new BMenu("file");
288 menu
->SetRadioMode(true);
289 menu
->SetLabelFromMarked(true);
290 menu
->AddSeparatorItem();
291 menu
->AddItem(new BMenuItem(B_TRANSLATE("<none>"),
292 new BMessage(M_NONE_MESSAGE
)));
293 menu
->AddItem(new BMenuItem(B_TRANSLATE("Other" B_UTF8_ELLIPSIS
),
294 new BMessage(M_OTHER_MESSAGE
)));
296 BString
label(B_TRANSLATE("Sound file:"));
297 BMenuField
* menuField
= new BMenuField("filemenu", label
, menu
);
298 menuField
->SetDivider(menuField
->StringWidth(label
) + 10);
300 BSize
buttonsSize(be_plain_font
->Size() * 2.5, be_plain_font
->Size() * 2.5);
302 BButton
* stopbutton
= new BButton("stop", "\xE2\x96\xA0",
303 new BMessage(M_STOP_MESSAGE
));
304 stopbutton
->SetEnabled(false);
305 stopbutton
->SetExplicitSize(buttonsSize
);
307 // We need at least one view to trigger B_PULSE_NEEDED events which we will
308 // intercept in DispatchMessage to trigger the buttons enabling or disabling.
309 stopbutton
->SetFlags(stopbutton
->Flags() | B_PULSE_NEEDED
);
311 BButton
* playbutton
= new BButton("play", "\xE2\x96\xB6",
312 new BMessage(M_PLAY_MESSAGE
));
313 playbutton
->SetEnabled(false);
314 playbutton
->SetExplicitSize(buttonsSize
);
316 BLayoutBuilder::Group
<>(this, B_VERTICAL
)
317 .SetInsets(B_USE_WINDOW_SPACING
)
319 .AddGroup(B_HORIZONTAL
)
321 .AddGroup(B_HORIZONTAL
, 0)
329 BMenuItem
* noneItem
= menu
->FindItem(B_TRANSLATE("<none>"));
330 if (noneItem
!= NULL
)
331 noneItem
->SetMarked(true);
340 BButton
* stop
= dynamic_cast<BButton
*>(FindView("stop"));
345 if (fPlayer
!= NULL
) {
346 if (fPlayer
->IsPlaying())
347 stop
->SetEnabled(true);
349 stop
->SetEnabled(false);
351 stop
->SetEnabled(false);
356 HWindow::_SetupMenuField()
358 BMenuField
* menufield
= dynamic_cast<BMenuField
*>(FindView("filemenu"));
359 if (menufield
== NULL
)
361 BMenu
* menu
= menufield
->Menu();
362 int32 count
= fEventList
->CountRows();
363 for (int32 i
= 0; i
< count
; i
++) {
364 HEventRow
* row
= (HEventRow
*)fEventList
->RowAt(i
);
368 BPath
path(row
->Path());
369 if (path
.InitCheck() != B_OK
)
371 if (menu
->FindItem(path
.Leaf()))
374 BMessage
* msg
= new BMessage(M_ITEM_MESSAGE
);
376 ::get_ref_for_path(path
.Path(), &ref
);
377 msg
->AddRef("refs", &ref
);
378 menu
->AddItem(new BMenuItem(path
.Leaf(), msg
), 0);
381 directory_which whichDirectories
[] = {
382 B_SYSTEM_SOUNDS_DIRECTORY
,
383 B_SYSTEM_NONPACKAGED_SOUNDS_DIRECTORY
,
384 B_USER_SOUNDS_DIRECTORY
,
385 B_USER_NONPACKAGED_SOUNDS_DIRECTORY
,
389 i
< sizeof(whichDirectories
) / sizeof(whichDirectories
[0]); i
++) {
395 status_t err
= find_directory(whichDirectories
[i
], &path
);
397 err
= dir
.SetTo(path
.Path());
398 while (err
== B_OK
) {
399 err
= dir
.GetNextEntry(&entry
, true);
400 if (entry
.InitCheck() != B_NO_ERROR
)
403 entry
.GetPath(&item_path
);
405 if (menu
->FindItem(item_path
.Leaf()))
408 BMessage
* msg
= new BMessage(M_ITEM_MESSAGE
);
410 ::get_ref_for_path(item_path
.Path(), &ref
);
411 msg
->AddRef("refs", &ref
);
412 menu
->AddItem(new BMenuItem(item_path
.Leaf(), msg
), 0);
419 HWindow::_UpdateZoomLimits()
421 const float kInset
= be_control_look
->DefaultItemSpacing();
423 BSize size
= fEventList
->PreferredSize();
424 SetZoomLimits(size
.width
+ 2 * kInset
+ B_V_SCROLL_BAR_WIDTH
,
425 size
.height
+ 5 * kInset
+ 2 * B_H_SCROLL_BAR_HEIGHT
426 + 2 * be_plain_font
->Size() * 2.5);