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