1 /* 2 * Copyright 2010-2014, Haiku Inc. All rights reserved. 3 * Copyright 2010 Wim van der Meer <WPJvanderMeer@gmail.com> 4 * Copyright Karsten Heimrich, host.haiku@gmx.de. 5 * All rights reserved. Distributed under the terms of the MIT License. 6 * 7 * Authors: 8 * Karsten Heimrich 9 * Fredrik Modéen 10 * Christophe Huriaux 11 * Wim van der Meer 12 */ 13 14 15 #include "ScreenshotWindow.h" 16 17 #include <stdlib.h> 18 19 #include <Alert.h> 20 #include <Application.h> 21 #include <Bitmap.h> 22 #include <Box.h> 23 #include <Button.h> 24 #include <Catalog.h> 25 #include <CheckBox.h> 26 #include <ControlLook.h> 27 #include <File.h> 28 #include <FilePanel.h> 29 #include <FindDirectory.h> 30 #include <LayoutBuilder.h> 31 #include <Locale.h> 32 #include <Menu.h> 33 #include <MenuField.h> 34 #include <MenuItem.h> 35 #include <MessageFilter.h> 36 #include <Path.h> 37 #include <Roster.h> 38 #include <SpaceLayoutItem.h> 39 #include <String.h> 40 #include <StringView.h> 41 #include <TextControl.h> 42 #include <TranslationUtils.h> 43 #include <TranslatorRoster.h> 44 45 #include "Utility.h" 46 47 48 #undef B_TRANSLATION_CONTEXT 49 #define B_TRANSLATION_CONTEXT "ScreenshotWindow" 50 51 52 enum { 53 kActiveWindow, 54 kIncludeBorder, 55 kIncludeCursor, 56 kNewScreenshot, 57 kImageFormat, 58 kLocationChanged, 59 kChooseLocation, 60 kSaveScreenshot, 61 kSettings, 62 kCloseTranslatorSettings 63 }; 64 65 66 // #pragma mark - QuitMessageFilter 67 68 69 class QuitMessageFilter : public BMessageFilter { 70 public: 71 QuitMessageFilter(BWindow* window) 72 : 73 BMessageFilter((uint32)B_QUIT_REQUESTED), 74 fWindow(window) 75 { 76 } 77 78 virtual filter_result Filter(BMessage* message, BHandler** target) 79 { 80 BMessenger(fWindow).SendMessage(kCloseTranslatorSettings); 81 return B_SKIP_MESSAGE; 82 } 83 84 private: 85 BWindow* fWindow; 86 }; 87 88 89 // #pragma mark - DirectoryRefFilter 90 91 92 class DirectoryRefFilter : public BRefFilter { 93 public: 94 virtual ~DirectoryRefFilter() 95 { 96 } 97 98 virtual bool Filter(const entry_ref* ref, BNode* node, 99 struct stat_beos* stat, const char* filetype) 100 { 101 return node->IsDirectory(); 102 } 103 }; 104 105 106 // #pragma mark - ScreenshotWindow 107 108 109 ScreenshotWindow::ScreenshotWindow(const Utility& utility, bool silent, 110 bool clipboard) 111 : 112 BWindow(BRect(0, 0, 200.0, 100.0), B_TRANSLATE_SYSTEM_NAME("Screenshot"), 113 B_TITLED_WINDOW, B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_AVOID_FRONT 114 | B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS 115 | B_CLOSE_ON_ESCAPE), 116 fUtility(utility), 117 fDelayControl(NULL), 118 fScreenshot(NULL), 119 fOutputPathPanel(NULL), 120 fLastSelectedPath(NULL), 121 fSettingsWindow(NULL), 122 fDelay(0), 123 fIncludeBorder(false), 124 fIncludeCursor(false), 125 fGrabActiveWindow(false), 126 fOutputFilename(NULL), 127 fExtension(""), 128 fImageFileType(B_PNG_FORMAT) 129 { 130 // _ReadSettings() needs a valid fOutputPathMenu 131 fOutputPathMenu = new BMenu(B_TRANSLATE("Please select")); 132 _ReadSettings(); 133 134 // _NewScreenshot() needs a valid fNameControl 135 BString name(B_TRANSLATE_NOCOLLECT(fUtility.sDefaultFileNameBase)); 136 name << 1; 137 name = _FindValidFileName(name.String()); 138 fNameControl = new BTextControl("", B_TRANSLATE("Name:"), name, NULL); 139 140 // Check if fUtility contains valid data 141 if (fUtility.wholeScreen == NULL) { 142 _NewScreenshot(silent, clipboard); 143 return; 144 } 145 146 fScreenshot = fUtility.MakeScreenshot(fIncludeCursor, fGrabActiveWindow, 147 fIncludeBorder); 148 149 fActiveWindow = new BCheckBox(B_TRANSLATE("Capture active window"), 150 new BMessage(kActiveWindow)); 151 if (fGrabActiveWindow) 152 fActiveWindow->SetValue(B_CONTROL_ON); 153 154 fWindowBorder = new BCheckBox(B_TRANSLATE("Include window border"), 155 new BMessage(kIncludeBorder)); 156 if (fIncludeBorder) 157 fWindowBorder->SetValue(B_CONTROL_ON); 158 if (!fGrabActiveWindow) 159 fWindowBorder->SetEnabled(false); 160 161 fShowCursor = new BCheckBox(B_TRANSLATE("Include mouse pointer"), 162 new BMessage(kIncludeCursor)); 163 if (fIncludeCursor) 164 fShowCursor->SetValue(B_CONTROL_ON); 165 166 BString delay; 167 delay << fDelay / 1000000; 168 fDelayControl = new BTextControl("", B_TRANSLATE("Delay:"), delay.String(), 169 NULL); 170 _DisallowChar(fDelayControl->TextView()); 171 fDelayControl->TextView()->SetAlignment(B_ALIGN_RIGHT); 172 BStringView* seconds = new BStringView("", B_TRANSLATE("seconds")); 173 seconds->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); 174 175 BMenuField* menuLocation = new BMenuField(B_TRANSLATE("Save in:"), 176 fOutputPathMenu); 177 menuLocation->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 178 179 fTranslatorMenu = new BMenu(B_TRANSLATE("Please select")); 180 _SetupTranslatorMenu(); 181 BMenuField* menuFormat = new BMenuField(B_TRANSLATE("Save as:"), 182 fTranslatorMenu); 183 menuFormat->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 184 185 BButton* showSettings = new BButton("", 186 B_TRANSLATE("Settings" B_UTF8_ELLIPSIS), new BMessage(kSettings)); 187 showSettings->SetExplicitAlignment( 188 BAlignment(B_ALIGN_RIGHT, B_ALIGN_BOTTOM)); 189 190 BBox* divider = new BBox(B_FANCY_BORDER, NULL); 191 divider->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1)); 192 193 BButton* saveScreenshot = new BButton("", B_TRANSLATE("Save"), 194 new BMessage(kSaveScreenshot)); 195 196 const float kSpacing = be_control_look->DefaultItemSpacing(); 197 const float kLabelSpacing = be_control_look->DefaultLabelSpacing(); 198 199 fPreview = new BView("preview", B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE); 200 BBox* previewBox = new BBox(B_FANCY_BORDER, fPreview); 201 202 BLayoutBuilder::Group<>(this, B_VERTICAL, 0) 203 .SetInsets(kSpacing) 204 .AddGroup(B_HORIZONTAL, kSpacing) 205 .Add(previewBox) 206 .AddGroup(B_VERTICAL, 0) 207 .Add(fActiveWindow) 208 .Add(fWindowBorder) 209 .Add(fShowCursor) 210 .AddStrut(kSpacing) 211 .AddGrid(0.0, kSpacing / 2) 212 .Add(fDelayControl->CreateLabelLayoutItem(), 0, 0) 213 .Add(fDelayControl->CreateTextViewLayoutItem(), 1, 0) 214 .Add(BSpaceLayoutItem::CreateHorizontalStrut(kLabelSpacing), 215 2, 0) 216 .Add(seconds, 3, 0) 217 .Add(fNameControl->CreateLabelLayoutItem(), 0, 1) 218 .Add(fNameControl->CreateTextViewLayoutItem(), 1, 1, 3, 1) 219 .Add(menuLocation->CreateLabelLayoutItem(), 0, 2) 220 .Add(menuLocation->CreateMenuBarLayoutItem(), 1, 2, 3, 1) 221 .Add(menuFormat->CreateLabelLayoutItem(), 0, 3) 222 .Add(menuFormat->CreateMenuBarLayoutItem(), 1, 3, 3, 1) 223 .End() 224 .AddStrut(kSpacing / 2) 225 .Add(showSettings) 226 .AddGlue() 227 .End() 228 .End() 229 .AddStrut(kSpacing) 230 .Add(divider) 231 .AddStrut(kSpacing) 232 .AddGroup(B_HORIZONTAL, kSpacing) 233 .Add(new BButton("", B_TRANSLATE("Copy to clipboard"), 234 new BMessage(B_COPY))) 235 .Add(new BButton("", B_TRANSLATE("New screenshot"), 236 new BMessage(kNewScreenshot))) 237 .AddGlue() 238 .Add(saveScreenshot); 239 240 saveScreenshot->MakeDefault(true); 241 242 _UpdatePreviewPanel(); 243 _UpdateFilenameSelection(); 244 245 CenterOnScreen(); 246 Show(); 247 } 248 249 250 ScreenshotWindow::~ScreenshotWindow() 251 { 252 if (fOutputPathPanel) 253 delete fOutputPathPanel->RefFilter(); 254 255 delete fOutputPathPanel; 256 delete fScreenshot; 257 } 258 259 260 void 261 ScreenshotWindow::MessageReceived(BMessage* message) 262 { 263 switch (message->what) { 264 case kActiveWindow: 265 fGrabActiveWindow = false; 266 if (fActiveWindow->Value() == B_CONTROL_ON) 267 fGrabActiveWindow = true; 268 269 fWindowBorder->SetEnabled(fGrabActiveWindow); 270 271 delete fScreenshot; 272 fScreenshot = fUtility.MakeScreenshot(fIncludeCursor, 273 fGrabActiveWindow, fIncludeBorder); 274 _UpdatePreviewPanel(); 275 break; 276 277 case kIncludeBorder: 278 fIncludeBorder = (fWindowBorder->Value() == B_CONTROL_ON); 279 delete fScreenshot; 280 fScreenshot = fUtility.MakeScreenshot(fIncludeCursor, 281 fGrabActiveWindow, fIncludeBorder); 282 _UpdatePreviewPanel(); 283 break; 284 285 case kIncludeCursor: 286 fIncludeCursor = (fShowCursor->Value() == B_CONTROL_ON); 287 delete fScreenshot; 288 fScreenshot = fUtility.MakeScreenshot(fIncludeCursor, 289 fGrabActiveWindow, fIncludeBorder); 290 _UpdatePreviewPanel(); 291 break; 292 293 case kNewScreenshot: 294 fDelay = (atoi(fDelayControl->Text()) * 1000000) + 50000; 295 _NewScreenshot(); 296 break; 297 298 case kImageFormat: 299 message->FindInt32("be:type", &fImageFileType); 300 fNameControl->SetText(_FindValidFileName( 301 fNameControl->Text()).String()); 302 _UpdateFilenameSelection(); 303 _ShowSettings(false); 304 break; 305 306 case kLocationChanged: 307 { 308 void* source = NULL; 309 if (message->FindPointer("source", &source) == B_OK) 310 fLastSelectedPath = static_cast<BMenuItem*> (source); 311 312 fNameControl->SetText(_FindValidFileName( 313 fNameControl->Text()).String()); 314 315 _UpdateFilenameSelection(); 316 break; 317 } 318 319 case kChooseLocation: 320 { 321 if (!fOutputPathPanel) { 322 BMessenger target(this); 323 fOutputPathPanel = new BFilePanel(B_OPEN_PANEL, &target, NULL, 324 B_DIRECTORY_NODE, false, NULL, new DirectoryRefFilter()); 325 fOutputPathPanel->Window()->SetTitle( 326 B_TRANSLATE("Choose folder")); 327 fOutputPathPanel->SetButtonLabel(B_DEFAULT_BUTTON, 328 B_TRANSLATE("Select")); 329 fOutputPathPanel->SetButtonLabel(B_CANCEL_BUTTON, 330 B_TRANSLATE("Cancel")); 331 } 332 fOutputPathPanel->Show(); 333 break; 334 } 335 336 case B_REFS_RECEIVED: 337 { 338 entry_ref ref; 339 if (message->FindRef("refs", &ref) == B_OK) { 340 BEntry entry(&ref, true); 341 if (entry.InitCheck() == B_OK) { 342 BPath path; 343 if (entry.GetPath(&path) == B_OK) { 344 BString label(path.Path()); 345 _AddItemToPathMenu(path.Path(), label, 3, true); 346 } 347 } 348 } 349 break; 350 } 351 352 case B_CANCEL: 353 fLastSelectedPath->SetMarked(true); 354 break; 355 356 case kSaveScreenshot: 357 if (_SaveScreenshot() == B_OK) 358 be_app->PostMessage(B_QUIT_REQUESTED); 359 break; 360 361 case B_COPY: 362 fUtility.CopyToClipboard(fScreenshot); 363 break; 364 365 case kSettings: 366 _ShowSettings(true); 367 break; 368 369 case kCloseTranslatorSettings: 370 fSettingsWindow->Lock(); 371 fSettingsWindow->Quit(); 372 fSettingsWindow = NULL; 373 break; 374 375 default: 376 BWindow::MessageReceived(message); 377 break; 378 } 379 } 380 381 382 void 383 ScreenshotWindow::Quit() 384 { 385 if (fUtility.wholeScreen != NULL) 386 _WriteSettings(); 387 BWindow::Quit(); 388 } 389 390 391 void 392 ScreenshotWindow::_NewScreenshot(bool silent, bool clipboard) 393 { 394 BMessage message(B_ARGV_RECEIVED); 395 int32 argc = 3; 396 BString delay; 397 delay << fDelay / 1000000; 398 message.AddString("argv", "screenshot"); 399 message.AddString("argv", "--delay"); 400 message.AddString("argv", delay); 401 402 if (silent || clipboard) { 403 if (silent) { 404 argc++; 405 message.AddString("argv", "--silent"); 406 } 407 if (clipboard) { 408 argc++; 409 message.AddString("argv", "--clipboard"); 410 } 411 if (fIncludeBorder) { 412 argc++; 413 message.AddString("argv", "--border"); 414 } 415 if (fIncludeCursor) { 416 argc++; 417 message.AddString("argv", "--mouse-pointer"); 418 } 419 if (fGrabActiveWindow) { 420 argc++; 421 message.AddString("argv", "--window"); 422 } 423 if (fLastSelectedPath) { 424 BPath path(_GetDirectory()); 425 if (path != NULL) { 426 path.Append(fNameControl->Text()); 427 argc++; 428 message.AddString("argv", path.Path()); 429 } 430 } 431 } 432 message.AddInt32("argc", argc); 433 434 be_roster->Launch("application/x-vnd.haiku-screenshot-cli", &message); 435 be_app->PostMessage(B_QUIT_REQUESTED); 436 } 437 438 439 void 440 ScreenshotWindow::_UpdatePreviewPanel() 441 { 442 float height = 150.0f; 443 float width = (fScreenshot->Bounds().Width() 444 / fScreenshot->Bounds().Height()) * height; 445 446 // to prevent a preview way too wide 447 if (width > 400.0f) { 448 width = 400.0f; 449 height = (fScreenshot->Bounds().Height() 450 / fScreenshot->Bounds().Width()) * width; 451 } 452 453 fPreview->SetExplicitMinSize(BSize(width, height)); 454 455 fPreview->ClearViewBitmap(); 456 fPreview->SetViewBitmap(fScreenshot, fScreenshot->Bounds(), 457 fPreview->Bounds(), B_FOLLOW_ALL, B_FILTER_BITMAP_BILINEAR); 458 } 459 460 461 void 462 ScreenshotWindow::_DisallowChar(BTextView* textView) 463 { 464 for (uint32 i = 0; i < '0'; ++i) 465 textView->DisallowChar(i); 466 467 for (uint32 i = '9' + 1; i < 255; ++i) 468 textView->DisallowChar(i); 469 } 470 471 472 void 473 ScreenshotWindow::_SetupOutputPathMenu(const BMessage& settings) 474 { 475 fOutputPathMenu->SetLabelFromMarked(true); 476 477 BString lastSelectedPath; 478 settings.FindString("lastSelectedPath", &lastSelectedPath); 479 480 BPath path; 481 find_directory(B_USER_DIRECTORY, &path); 482 483 BString label(B_TRANSLATE("Home folder")); 484 _AddItemToPathMenu(path.Path(), label, 0, 485 path.Path() == lastSelectedPath, 'H'); 486 487 path.Append("Desktop"); 488 label.SetTo(B_TRANSLATE("Desktop")); 489 _AddItemToPathMenu(path.Path(), label, 0, 490 path.Path() == lastSelectedPath, 'D'); 491 492 find_directory(B_BEOS_ETC_DIRECTORY, &path); 493 path.Append("artwork"); 494 495 label.SetTo(B_TRANSLATE("Artwork folder")); 496 _AddItemToPathMenu(path.Path(), label, 2, 497 path.Path() == lastSelectedPath, 'A'); 498 499 int32 i = 0; 500 BString userPath; 501 while (settings.FindString("path", ++i, &userPath) == B_OK) { 502 _AddItemToPathMenu(userPath.String(), userPath, 3, 503 userPath == lastSelectedPath); 504 } 505 506 if (!fLastSelectedPath) { 507 if (settings.IsEmpty() || lastSelectedPath.Length() == 0) { 508 fOutputPathMenu->ItemAt(1)->SetMarked(true); 509 fLastSelectedPath = fOutputPathMenu->ItemAt(1); 510 } else { 511 _AddItemToPathMenu(lastSelectedPath.String(), lastSelectedPath, 3, 512 true); 513 } 514 } 515 516 fOutputPathMenu->AddItem(new BSeparatorItem()); 517 fOutputPathMenu->AddItem(new BMenuItem( 518 B_TRANSLATE("Choose folder" B_UTF8_ELLIPSIS), 519 new BMessage(kChooseLocation), 'F')); 520 } 521 522 523 void 524 ScreenshotWindow::_AddItemToPathMenu(const char* path, BString& label, 525 int32 index, bool markItem, uint32 shortcutKey) 526 { 527 // Make sure that item won't be a duplicate of an existing one 528 for (int32 i = fOutputPathMenu->CountItems() - 1; i >= 0; --i) { 529 BMenuItem* menuItem = fOutputPathMenu->ItemAt(i); 530 BMessage* message = menuItem->Message(); 531 const char* pathFromItem; 532 if (message != NULL && message->what == kLocationChanged 533 && message->FindString("path", &pathFromItem) == B_OK 534 && !strcmp(path, pathFromItem)) { 535 536 if (markItem) { 537 fOutputPathMenu->ItemAt(i)->SetMarked(true); 538 fLastSelectedPath = fOutputPathMenu->ItemAt(i); 539 } 540 return; 541 } 542 } 543 544 BMessage* message = new BMessage(kLocationChanged); 545 message->AddString("path", path); 546 547 fOutputPathMenu->TruncateString(&label, B_TRUNCATE_MIDDLE, 548 fOutputPathMenu->StringWidth("SomethingLongHere")); 549 550 fOutputPathMenu->AddItem(new BMenuItem(label.String(), message, 551 shortcutKey), index); 552 553 if (markItem) { 554 fOutputPathMenu->ItemAt(index)->SetMarked(true); 555 fLastSelectedPath = fOutputPathMenu->ItemAt(index); 556 } 557 } 558 559 560 void 561 ScreenshotWindow::_UpdateFilenameSelection() 562 { 563 fNameControl->MakeFocus(true); 564 fNameControl->TextView()->Select(0, fNameControl->TextView()->TextLength() 565 - fExtension.Length()); 566 567 fNameControl->TextView()->ScrollToSelection(); 568 } 569 570 571 void 572 ScreenshotWindow::_SetupTranslatorMenu() 573 { 574 BMessage message(kImageFormat); 575 fTranslatorMenu = new BMenu("Please select"); 576 BTranslationUtils::AddTranslationItems(fTranslatorMenu, B_TRANSLATOR_BITMAP, 577 &message, NULL, NULL, NULL); 578 579 fTranslatorMenu->SetLabelFromMarked(true); 580 581 if (fTranslatorMenu->ItemAt(0)) 582 fTranslatorMenu->ItemAt(0)->SetMarked(true); 583 584 int32 imageFileType; 585 for (int32 i = 0; i < fTranslatorMenu->CountItems(); ++i) { 586 BMenuItem* item = fTranslatorMenu->ItemAt(i); 587 if (item != NULL && item->Message()) { 588 item->Message()->FindInt32("be:type", &imageFileType); 589 if (fImageFileType == imageFileType) { 590 item->SetMarked(true); 591 MessageReceived(item->Message()); 592 break; 593 } 594 } 595 } 596 } 597 598 599 status_t 600 ScreenshotWindow::_SaveScreenshot() 601 { 602 if (!fScreenshot || !fLastSelectedPath) 603 return B_ERROR; 604 605 BPath path(_GetDirectory()); 606 607 if (path == NULL) 608 return B_ERROR; 609 610 path.Append(fNameControl->Text()); 611 612 BEntry entry; 613 entry.SetTo(path.Path()); 614 615 if (entry.Exists()) { 616 BAlert* overwriteAlert = new BAlert( 617 B_TRANSLATE("overwrite"), 618 B_TRANSLATE("This file already exists.\n Are you sure you would " 619 "like to overwrite it?"), 620 B_TRANSLATE("Cancel"), 621 B_TRANSLATE("Overwrite"), 622 NULL, B_WIDTH_AS_USUAL, B_EVEN_SPACING, B_WARNING_ALERT); 623 624 overwriteAlert->SetShortcut(0, B_ESCAPE); 625 626 if (overwriteAlert->Go() == 0) 627 return B_CANCELED; 628 } 629 630 return fUtility.Save(fScreenshot, path.Path(), fImageFileType); 631 } 632 633 634 void 635 ScreenshotWindow::_ShowSettings(bool activate) 636 { 637 if (!fSettingsWindow && !activate) 638 return; 639 640 // Find a translator 641 translator_id translator; 642 if (fUtility.FindTranslator(fImageFileType, translator) != B_OK) 643 return; 644 645 // Create a window with a configuration view 646 BView* view; 647 BRect rect(0, 0, 239, 239); 648 649 status_t status = BTranslatorRoster::Default()->MakeConfigurationView( 650 translator, NULL, &view, &rect); 651 if (status != B_OK || view == NULL) { 652 // TODO: proper translation, better error dialog 653 BAlert* alert = new BAlert(NULL, strerror(status), "OK"); 654 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 655 alert->Go(); 656 } else if (fSettingsWindow != NULL) { 657 fSettingsWindow->RemoveChild(fSettingsWindow->ChildAt(0)); 658 float width, height; 659 view->GetPreferredSize(&width, &height); 660 fSettingsWindow->ResizeTo(width, height); 661 fSettingsWindow->AddChild(view); 662 if (activate) 663 fSettingsWindow->Activate(); 664 } else { 665 fSettingsWindow = new BWindow(rect, 666 B_TRANSLATE("Translator Settings"), 667 B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, 668 B_NOT_ZOOMABLE | B_NOT_RESIZABLE); 669 fSettingsWindow->AddFilter(new QuitMessageFilter(this)); 670 fSettingsWindow->AddChild(view); 671 fSettingsWindow->CenterOnScreen(); 672 fSettingsWindow->Show(); 673 } 674 } 675 676 677 BString 678 ScreenshotWindow::_FindValidFileName(const char* name) 679 { 680 BString baseName(name); 681 682 if (!fExtension.IsEmpty()) 683 baseName.RemoveLast(fExtension); 684 685 if (!fLastSelectedPath) 686 return baseName; 687 688 BPath orgPath(_GetDirectory()); 689 if (orgPath == NULL) 690 return baseName; 691 692 fExtension = fUtility.FileNameExtension(fImageFileType); 693 694 BPath outputPath = orgPath; 695 BString fileName; 696 fileName << baseName << fExtension; 697 outputPath.Append(fileName); 698 699 if (!BEntry(outputPath.Path()).Exists()) 700 return fileName; 701 702 if (baseName.FindFirst(B_TRANSLATE_NOCOLLECT( 703 fUtility.sDefaultFileNameBase)) == 0) 704 baseName.SetTo(fUtility.sDefaultFileNameBase); 705 706 BEntry entry; 707 int32 index = 1; 708 709 do { 710 fileName = ""; 711 fileName << baseName << index++ << fExtension; 712 outputPath.SetTo(orgPath.Path()); 713 outputPath.Append(fileName); 714 entry.SetTo(outputPath.Path()); 715 } while (entry.Exists()); 716 717 return fileName; 718 } 719 720 721 BPath 722 ScreenshotWindow::_GetDirectory() 723 { 724 BPath path; 725 726 BMessage* message = fLastSelectedPath->Message(); 727 const char* stringPath; 728 if (message && message->FindString("path", &stringPath) == B_OK) 729 path.SetTo(stringPath); 730 731 return path; 732 } 733 734 735 void 736 ScreenshotWindow::_ReadSettings() 737 { 738 BMessage settings; 739 740 BPath settingsPath; 741 if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK) 742 return; 743 744 settingsPath.Append("Screenshot_settings"); 745 746 BFile file(settingsPath.Path(), B_READ_ONLY); 747 if (file.InitCheck() == B_OK) 748 settings.Unflatten(&file); 749 750 if (settings.FindInt32("type", &fImageFileType) != B_OK) 751 fImageFileType = B_PNG_FORMAT; 752 settings.FindBool("includeBorder", &fIncludeBorder); 753 settings.FindBool("includeCursor", &fIncludeCursor); 754 settings.FindBool("grabActiveWindow", &fGrabActiveWindow); 755 settings.FindInt64("delay", &fDelay); 756 settings.FindString("outputFilename", &fOutputFilename); 757 758 _SetupOutputPathMenu(settings); 759 } 760 761 762 void 763 ScreenshotWindow::_WriteSettings() 764 { 765 if (fDelayControl) 766 fDelay = (atoi(fDelayControl->Text()) * 1000000) + 50000; 767 768 BMessage settings; 769 770 settings.AddInt32("type", fImageFileType); 771 settings.AddBool("includeBorder", fIncludeBorder); 772 settings.AddBool("includeCursor", fIncludeCursor); 773 settings.AddBool("grabActiveWindow", fGrabActiveWindow); 774 settings.AddInt64("delay", fDelay); 775 settings.AddString("outputFilename", fOutputFilename); 776 777 BString path; 778 int32 count = fOutputPathMenu->CountItems(); 779 if (count > 5) { 780 for (int32 i = count - 3; i > count - 8 && i > 2; --i) { 781 BMenuItem* item = fOutputPathMenu->ItemAt(i); 782 if (item) { 783 BMessage* msg = item->Message(); 784 if (msg && msg->FindString("path", &path) == B_OK) 785 settings.AddString("path", path.String()); 786 } 787 } 788 } 789 790 if (fLastSelectedPath) { 791 BMessage* msg = fLastSelectedPath->Message(); 792 if (msg && msg->FindString("path", &path) == B_OK) 793 settings.AddString("lastSelectedPath", path.String()); 794 } 795 796 BPath settingsPath; 797 if (find_directory(B_USER_SETTINGS_DIRECTORY, &settingsPath) != B_OK) 798 return; 799 settingsPath.Append("Screenshot_settings"); 800 801 BFile file(settingsPath.Path(), B_CREATE_FILE | B_ERASE_FILE 802 | B_WRITE_ONLY); 803 if (file.InitCheck() == B_OK) { 804 ssize_t size; 805 settings.Flatten(&file, &size); 806 } 807 } 808