1 /* 2 * Copyright 2003-2008, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Jérôme Duval 7 * Oliver Ruiz Dorantes 8 * Atsushi Takamatsu 9 */ 10 11 12 #include "HWindow.h" 13 #include "HEventList.h" 14 15 #include <stdio.h> 16 17 #include <Alert.h> 18 #include <Application.h> 19 #include <Beep.h> 20 #include <Box.h> 21 #include <Button.h> 22 #include <Catalog.h> 23 #include <ControlLook.h> 24 #include <FindDirectory.h> 25 #include <fs_attr.h> 26 #include <LayoutBuilder.h> 27 #include <Locale.h> 28 #include <MediaFiles.h> 29 #include <MenuBar.h> 30 #include <MenuField.h> 31 #include <MenuItem.h> 32 #include <Node.h> 33 #include <NodeInfo.h> 34 #include <Path.h> 35 #include <Roster.h> 36 #include <ScrollView.h> 37 #include <StringView.h> 38 #include <Sound.h> 39 40 41 #undef B_TRANSLATION_CONTEXT 42 #define B_TRANSLATION_CONTEXT "HWindow" 43 44 static const char kSettingsFile[] = "Sounds_Settings"; 45 46 47 HWindow::HWindow(BRect rect, const char* name) 48 : 49 BWindow(rect, name, B_TITLED_WINDOW, B_AUTO_UPDATE_SIZE_LIMITS), 50 fFilePanel(NULL), 51 fPlayButton(NULL), 52 fPlayer(NULL) 53 { 54 _InitGUI(); 55 56 fFilePanel = new BFilePanel(); 57 fFilePanel->SetTarget(this); 58 59 BPath path; 60 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) { 61 path.Append(kSettingsFile); 62 BFile file(path.Path(), B_READ_ONLY); 63 64 BMessage msg; 65 if (file.InitCheck() == B_OK && msg.Unflatten(&file) == B_OK 66 && msg.FindRect("frame", &fFrame) == B_OK) { 67 MoveTo(fFrame.LeftTop()); 68 ResizeTo(fFrame.Width(), fFrame.Height()); 69 } 70 } 71 72 MoveOnScreen(); 73 } 74 75 76 HWindow::~HWindow() 77 { 78 delete fFilePanel; 79 delete fPlayer; 80 81 BPath path; 82 BMessage msg; 83 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK) { 84 path.Append(kSettingsFile); 85 BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE); 86 87 if (file.InitCheck() == B_OK) { 88 msg.AddRect("frame", fFrame); 89 msg.Flatten(&file); 90 } 91 } 92 } 93 94 95 void 96 HWindow::DispatchMessage(BMessage* message, BHandler* handler) 97 { 98 if (message->what == B_PULSE) 99 _Pulse(); 100 BWindow::DispatchMessage(message, handler); 101 } 102 103 104 void 105 HWindow::MessageReceived(BMessage* message) 106 { 107 switch (message->what) { 108 case M_OTHER_MESSAGE: 109 { 110 BMenuField* menufield 111 = dynamic_cast<BMenuField*>(FindView("filemenu")); 112 if (menufield == NULL) 113 return; 114 BMenu* menu = menufield->Menu(); 115 116 HEventRow* row = (HEventRow*)fEventList->CurrentSelection(); 117 if (row != NULL) { 118 BPath path(row->Path()); 119 if (path.InitCheck() != B_OK) { 120 BMenuItem* item = menu->FindItem(B_TRANSLATE("<none>")); 121 if (item != NULL) 122 item->SetMarked(true); 123 } else { 124 BMenuItem* item = menu->FindItem(path.Leaf()); 125 if (item != NULL) 126 item->SetMarked(true); 127 } 128 } 129 fFilePanel->Show(); 130 break; 131 } 132 133 case B_SIMPLE_DATA: 134 case B_REFS_RECEIVED: 135 { 136 entry_ref ref; 137 HEventRow* row = (HEventRow*)fEventList->CurrentSelection(); 138 if (message->FindRef("refs", &ref) == B_OK && row != NULL) { 139 BMenuField* menufield 140 = dynamic_cast<BMenuField*>(FindView("filemenu")); 141 if (menufield == NULL) 142 return; 143 BMenu* menu = menufield->Menu(); 144 145 // check audio file 146 BNode node(&ref); 147 BNodeInfo ninfo(&node); 148 char type[B_MIME_TYPE_LENGTH + 1]; 149 ninfo.GetType(type); 150 BMimeType mtype(type); 151 BMimeType superType; 152 mtype.GetSupertype(&superType); 153 if (superType.Type() == NULL 154 || strcmp(superType.Type(), "audio") != 0) { 155 beep(); 156 BAlert* alert = new BAlert("", 157 B_TRANSLATE("This is not an audio file."), 158 B_TRANSLATE("OK"), NULL, NULL, 159 B_WIDTH_AS_USUAL, B_STOP_ALERT); 160 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 161 alert->Go(); 162 break; 163 } 164 165 // add file item 166 BMessage* msg = new BMessage(M_ITEM_MESSAGE); 167 BPath path(&ref); 168 msg->AddRef("refs", &ref); 169 BMenuItem* menuitem = menu->FindItem(path.Leaf()); 170 if (menuitem == NULL) 171 menu->AddItem(menuitem = new BMenuItem(path.Leaf(), msg), 0); 172 // refresh item 173 fEventList->SetPath(BPath(&ref).Path()); 174 // check file menu 175 if (menuitem != NULL) 176 menuitem->SetMarked(true); 177 178 fPlayButton->SetEnabled(true); 179 } 180 break; 181 } 182 183 case M_PLAY_MESSAGE: 184 { 185 HEventRow* row = (HEventRow*)fEventList->CurrentSelection(); 186 if (row != NULL) { 187 const char* path = row->Path(); 188 if (path != NULL) { 189 entry_ref ref; 190 ::get_ref_for_path(path, &ref); 191 delete fPlayer; 192 fPlayer = new BFileGameSound(&ref, false); 193 fPlayer->StartPlaying(); 194 } 195 } 196 break; 197 } 198 199 case M_STOP_MESSAGE: 200 { 201 if (fPlayer == NULL) 202 break; 203 if (fPlayer->IsPlaying()) { 204 fPlayer->StopPlaying(); 205 delete fPlayer; 206 fPlayer = NULL; 207 } 208 break; 209 } 210 211 case M_EVENT_CHANGED: 212 { 213 const char* path; 214 BMenuField* menufield 215 = dynamic_cast<BMenuField*>(FindView("filemenu")); 216 if (menufield == NULL) 217 return; 218 219 menufield->SetEnabled(true); 220 221 BMenu* menu = menufield->Menu(); 222 223 if (message->FindString("path", &path) == B_OK) { 224 BPath path(path); 225 if (path.InitCheck() != B_OK) { 226 BMenuItem* item = menu->FindItem(B_TRANSLATE("<none>")); 227 if (item != NULL) 228 item->SetMarked(true); 229 } else { 230 BMenuItem* item = menu->FindItem(path.Leaf()); 231 if (item != NULL) 232 item->SetMarked(true); 233 } 234 235 HEventRow* row = (HEventRow*)fEventList->CurrentSelection(); 236 if (row != NULL) { 237 menufield->SetEnabled(true); 238 239 const char* path = row->Path(); 240 fPlayButton->SetEnabled(path != NULL && strcmp(path, "") != 0); 241 } else { 242 menufield->SetEnabled(false); 243 fPlayButton->SetEnabled(false); 244 } 245 } 246 break; 247 } 248 249 case M_ITEM_MESSAGE: 250 { 251 entry_ref ref; 252 if (message->FindRef("refs", &ref) == B_OK) { 253 fEventList->SetPath(BPath(&ref).Path()); 254 _UpdateZoomLimits(); 255 256 HEventRow* row = (HEventRow*)fEventList->CurrentSelection(); 257 fPlayButton->SetEnabled(row != NULL && row->Path() != NULL); 258 } 259 break; 260 } 261 262 case M_NONE_MESSAGE: 263 { 264 fPlayButton->SetEnabled(false); 265 fEventList->SetPath(NULL); 266 break; 267 } 268 269 default: 270 BWindow::MessageReceived(message); 271 } 272 } 273 274 275 bool 276 HWindow::QuitRequested() 277 { 278 fFrame = Frame(); 279 280 fEventList->RemoveAll(); 281 be_app->PostMessage(B_QUIT_REQUESTED); 282 return true; 283 } 284 285 286 void 287 HWindow::_InitGUI() 288 { 289 fEventList = new HEventList(); 290 fEventList->SetType(BMediaFiles::B_SOUNDS); 291 fEventList->SetSelectionMode(B_SINGLE_SELECTION_LIST); 292 293 BMenu* menu = new BMenu("file"); 294 menu->SetRadioMode(true); 295 menu->SetLabelFromMarked(true); 296 menu->AddSeparatorItem(); 297 menu->AddItem(new BMenuItem(B_TRANSLATE("<none>"), 298 new BMessage(M_NONE_MESSAGE))); 299 menu->AddItem(new BMenuItem(B_TRANSLATE("Other" B_UTF8_ELLIPSIS), 300 new BMessage(M_OTHER_MESSAGE))); 301 302 BString label(B_TRANSLATE("Sound file:")); 303 BMenuField* menuField = new BMenuField("filemenu", label, menu); 304 menuField->SetDivider(menuField->StringWidth(label) + 10); 305 306 BSize buttonsSize(be_plain_font->Size() * 2.5, be_plain_font->Size() * 2.5); 307 308 BButton* stopbutton = new BButton("stop", "\xE2\x96\xA0", 309 new BMessage(M_STOP_MESSAGE)); 310 stopbutton->SetEnabled(false); 311 stopbutton->SetExplicitSize(buttonsSize); 312 313 // We need at least one view to trigger B_PULSE_NEEDED events which we will 314 // intercept in DispatchMessage to trigger the buttons enabling or disabling. 315 stopbutton->SetFlags(stopbutton->Flags() | B_PULSE_NEEDED); 316 317 fPlayButton = new BButton("play", "\xE2\x96\xB6", 318 new BMessage(M_PLAY_MESSAGE)); 319 fPlayButton->SetEnabled(false); 320 fPlayButton->SetExplicitSize(buttonsSize); 321 322 BLayoutBuilder::Group<>(this, B_VERTICAL) 323 .SetInsets(B_USE_WINDOW_SPACING) 324 .Add(fEventList) 325 .AddGroup(B_HORIZONTAL) 326 .Add(menuField) 327 .AddGroup(B_HORIZONTAL, 0) 328 .Add(fPlayButton) 329 .Add(stopbutton) 330 .End() 331 .End(); 332 333 // setup file menu 334 _SetupMenuField(); 335 BMenuItem* noneItem = menu->FindItem(B_TRANSLATE("<none>")); 336 if (noneItem != NULL) 337 noneItem->SetMarked(true); 338 339 menuField->SetEnabled(false); 340 341 _UpdateZoomLimits(); 342 } 343 344 345 void 346 HWindow::_Pulse() 347 { 348 BButton* stop = dynamic_cast<BButton*>(FindView("stop")); 349 350 if (stop == NULL) 351 return; 352 353 if (fPlayer != NULL) { 354 if (fPlayer->IsPlaying()) 355 stop->SetEnabled(true); 356 else 357 stop->SetEnabled(false); 358 } else 359 stop->SetEnabled(false); 360 } 361 362 363 void 364 HWindow::_SetupMenuField() 365 { 366 BMenuField* menufield = dynamic_cast<BMenuField*>(FindView("filemenu")); 367 if (menufield == NULL) 368 return; 369 BMenu* menu = menufield->Menu(); 370 int32 count = fEventList->CountRows(); 371 for (int32 i = 0; i < count; i++) { 372 HEventRow* row = (HEventRow*)fEventList->RowAt(i); 373 if (row == NULL) 374 continue; 375 376 BPath path(row->Path()); 377 if (path.InitCheck() != B_OK) 378 continue; 379 if (menu->FindItem(path.Leaf())) 380 continue; 381 382 BMessage* msg = new BMessage(M_ITEM_MESSAGE); 383 entry_ref ref; 384 ::get_ref_for_path(path.Path(), &ref); 385 msg->AddRef("refs", &ref); 386 menu->AddItem(new BMenuItem(path.Leaf(), msg), 0); 387 } 388 389 directory_which whichDirectories[] = { 390 B_SYSTEM_SOUNDS_DIRECTORY, 391 B_SYSTEM_NONPACKAGED_SOUNDS_DIRECTORY, 392 B_USER_SOUNDS_DIRECTORY, 393 B_USER_NONPACKAGED_SOUNDS_DIRECTORY, 394 }; 395 396 for (size_t i = 0; 397 i < sizeof(whichDirectories) / sizeof(whichDirectories[0]); i++) { 398 BPath path; 399 BDirectory dir; 400 BEntry entry; 401 BPath item_path; 402 403 status_t err = find_directory(whichDirectories[i], &path); 404 if (err == B_OK) 405 err = dir.SetTo(path.Path()); 406 while (err == B_OK) { 407 err = dir.GetNextEntry(&entry, true); 408 if (entry.InitCheck() != B_NO_ERROR) 409 break; 410 411 entry.GetPath(&item_path); 412 413 if (menu->FindItem(item_path.Leaf())) 414 continue; 415 416 BMessage* msg = new BMessage(M_ITEM_MESSAGE); 417 entry_ref ref; 418 ::get_ref_for_path(item_path.Path(), &ref); 419 msg->AddRef("refs", &ref); 420 menu->AddItem(new BMenuItem(item_path.Leaf(), msg), 0); 421 } 422 } 423 } 424 425 426 void 427 HWindow::_UpdateZoomLimits() 428 { 429 const float kInset = be_control_look->DefaultItemSpacing(); 430 431 BSize size = fEventList->PreferredSize(); 432 SetZoomLimits(size.width + 2 * kInset + B_V_SCROLL_BAR_WIDTH, 433 size.height + 5 * kInset + 2 * B_H_SCROLL_BAR_HEIGHT 434 + 2 * be_plain_font->Size() * 2.5); 435 } 436