Ticket #5724: screenshotEnhancement-parametricFilename.patch
File screenshotEnhancement-parametricFilename.patch, 10.2 KB (added by , 15 years ago) |
---|
-
src/apps/screenshot/ScreenshotWindow.cpp
5 5 * Authors: 6 6 * Karsten Heimrich 7 7 * Fredrik Modéen 8 * Christophe Huriaux 8 9 */ 9 10 10 11 #include "ScreenshotWindow.h" … … 37 38 #include <MenuItem.h> 38 39 #include <Message.h> 39 40 #include <NodeInfo.h> 41 #include <ObjectList.h> 40 42 #include <Path.h> 41 43 #include <RadioButton.h> 42 44 #include <Region.h> … … 91 93 92 94 ScreenshotWindow::ScreenshotWindow(bigtime_t delay, bool includeBorder, 93 95 bool includeMouse, bool grabActiveWindow, bool showConfigWindow, 94 bool saveScreenshotSilent, int32 imageFileType, int32 translator) 96 bool saveScreenshotSilent, int32 imageFileType, int32 translator, 97 BString outputFilename) 95 98 : 96 99 BWindow(BRect(0, 0, 200.0, 100.0), TR("Retake screenshot"), B_TITLED_WINDOW, 97 100 B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_QUIT_ON_WINDOW_CLOSE | … … 108 111 fShowConfigWindow(showConfigWindow), 109 112 fSaveScreenshotSilent(saveScreenshotSilent), 110 113 fExtension(""), 114 fOutputFilename(outputFilename), 111 115 fTranslator(translator), 112 116 fImageFileType(imageFileType) 113 { 117 { 118 _AddFileParameterRule(kImageHeight, "$h"); 119 _AddFileParameterRule(kImageWidth, "$w"); 120 _AddFileParameterRule(kImageFormat, "$t"); 121 114 122 if (fSaveScreenshotSilent) { 115 123 _TakeScreenshot(); 116 124 _SaveScreenshot(); … … 128 136 if (fOutputPathPanel) 129 137 delete fOutputPathPanel->RefFilter(); 130 138 139 for ( int32 i = 0; ; i++ ) { 140 void* item = fFilenameParameters.ItemAt(i); 141 if (item == NULL) 142 break; 143 144 delete static_cast<struct FileParameterRule*> (item); 145 } 146 131 147 delete fScreenshot; 132 148 delete fOutputPathPanel; 133 149 } … … 287 303 _TakeScreenshot(); 288 304 _UpdatePreviewPanel(); 289 305 layout->SetVisibleItem(1L); 290 fSaveScreenshot->MakeDefault(true);306 SetDefaultButton(fSaveScreenshot); 291 307 } else { 292 308 layout->SetVisibleItem(0L); 293 309 SetDefaultButton(NULL); … … 559 575 // to prevent a preview way too wide 560 576 if (width > 400.0f) { 561 577 width = 400.0f; 562 height = (fScreenshot->Bounds().Height() /563 fScreenshot->Bounds().Width()) * width;578 height = (fScreenshot->Bounds().Height() 579 / fScreenshot->Bounds().Width()) * width; 564 580 } 565 581 566 582 fPreview->SetExplicitMinSize(BSize(width, height)); … … 585 601 { 586 602 fNameControl->MakeFocus(true); 587 603 fNameControl->TextView()->Select(0, 588 fNameControl->TextView()->TextLength() -589 fExtension.Length());604 fNameControl->TextView()->TextLength() 605 - fExtension.Length()); 590 606 fNameControl->TextView()->ScrollToSelection(); 591 607 } 592 608 … … 741 757 _MakeTabSpaceTransparent(&frame); 742 758 } else 743 759 BScreen(this).GetBitmap(&fScreenshot, fIncludeMouse); 760 761 _UpdateFileParameters(); 744 762 } 745 763 746 764 … … 794 812 } 795 813 796 814 815 void 816 ScreenshotWindow::_AddFileParameterRule(FileParameter index, 817 const char* name) 818 { 819 FileParameterRule* rule = new FileParameterRule; 820 821 rule->parameter = index; 822 rule->name = BString(name); 823 rule->replacementValue = BString(""); 824 825 fFilenameParameters.AddItem(rule); 826 } 827 828 829 void 830 ScreenshotWindow::_UpdateFileParameters() 831 { 832 fFilenameParameters.EachElement(ScreenshotWindow::_UpdateFileParameterRule, 833 static_cast<void*> (this)); 834 } 835 836 837 struct FileParameterRule* 838 ScreenshotWindow::_UpdateFileParameterRule(struct FileParameterRule* rule, 839 void* parentPointer) 840 { 841 ScreenshotWindow* screenshotParent = 842 static_cast<ScreenshotWindow*> (parentPointer); 843 rule->replacementValue = BString(""); 844 845 switch (rule->parameter) { 846 case kImageWidth: 847 rule->replacementValue 848 << screenshotParent->fScreenshot->Bounds().IntegerWidth() + 1; 849 break; 850 851 case kImageHeight: 852 rule->replacementValue 853 << screenshotParent->fScreenshot->Bounds().IntegerHeight() + 1; 854 break; 855 856 case kImageFormat: 857 switch (screenshotParent->fImageFileType) { 858 case B_BMP_FORMAT: rule->replacementValue << "BMP"; break; 859 case B_PNG_FORMAT: rule->replacementValue << "PNG"; break; 860 case B_GIF_FORMAT: rule->replacementValue << "GIF"; break; 861 case B_PPM_FORMAT: rule->replacementValue << "PPM"; break; 862 case B_TGA_FORMAT: rule->replacementValue << "TGA"; break; 863 case B_JPEG_FORMAT: rule->replacementValue << "JPEG"; break; 864 case B_TIFF_FORMAT: rule->replacementValue << "TIFF"; break; 865 default: rule->replacementValue << ""; break; 866 } 867 break; 868 869 default: 870 break; 871 } 872 873 return NULL; 874 } 875 876 877 struct FileParameterRule* 878 ScreenshotWindow::_ApplyFileParameterRule(struct FileParameterRule* rule, 879 void* sourceStringPtr) 880 { 881 BString* sourceString = static_cast<BString*> (sourceStringPtr); 882 883 sourceString->ReplaceAll(rule->name.String(), 884 rule->replacementValue.String()); 885 886 return NULL; 887 } 888 889 890 BString 891 ScreenshotWindow::_ParseFilename(const char* originalFilename) 892 { 893 BString parsedFilename; 894 time_t currentTime = time(NULL); 895 struct tm* timeInfo = localtime(¤tTime); 896 char* parsedBuffer; 897 898 // Date & time parameters attribute 899 parsedBuffer = parsedFilename.LockBuffer(B_FILE_NAME_LENGTH); 900 size_t parsedSize = strftime(parsedBuffer, B_FILE_NAME_LENGTH, 901 originalFilename, timeInfo); 902 903 if (parsedSize == 0) 904 return BString(originalFilename); 905 906 parsedFilename.UnlockBuffer(parsedSize); 907 908 // File screenshot related attributes 909 fFilenameParameters.EachElement(ScreenshotWindow::_ApplyFileParameterRule, 910 static_cast<void*> (&parsedFilename)); 911 912 return BString(parsedFilename); 913 } 914 915 797 916 status_t 798 917 ScreenshotWindow::_SaveScreenshot() 799 918 { … … 805 924 if (path == NULL) 806 925 return B_ERROR; 807 926 808 if (fSaveScreenshotSilent) 809 path.Append(_FindValidFileName( 810 TR_CMT("screenshot1", "!! Filename of first screenshot !!")).String()); 811 else 812 path.Append(fNameControl->Text()); 927 if (fSaveScreenshotSilent) { 928 if (!fOutputFilename.Compare("")) 929 path.Append(_FindValidFileName( 930 TR_CMT("screenshot1", "!! Filename of first screenshot !!")).String()); 931 else 932 path.SetTo(_ParseFilename(fOutputFilename)); 933 } else 934 path.Append(_ParseFilename(fNameControl->Text())); 813 935 814 936 BEntry entry; 815 937 entry.SetTo(path.Path()); -
src/apps/screenshot/Jamfile
1 1 SubDir HAIKU_TOP src apps screenshot ; 2 2 3 3 UseLibraryHeaders zlib ; 4 UsePrivateHeaders interface ;4 UsePrivateHeaders interface shared ; 5 5 6 6 Application Screenshot : 7 7 main.cpp -
src/apps/screenshot/ScreenshotWindow.h
5 5 * Authors: 6 6 * Karsten Heimrich 7 7 * Fredrik Modéen 8 * Christophe Huriaux 8 9 */ 9 10 #ifndef SCREENSHOT_WINDOW_H 10 11 #define SCREENSHOT_WINDOW_H 11 12 12 13 14 #include <ObjectList.h> 13 15 #include <String.h> 14 16 #include <Window.h> 15 17 #include <TranslatorFormats.h> … … 28 30 class PreviewView; 29 31 30 32 33 typedef enum { 34 kImageWidth, 35 kImageHeight, 36 kImageFormat 37 } FileParameter; 38 39 40 struct FileParameterRule { 41 FileParameter parameter; 42 BString name; 43 BString replacementValue; 44 }; 45 46 31 47 class ScreenshotWindow : public BWindow { 32 48 public: 33 49 ScreenshotWindow(bigtime_t delay = 0, … … 37 53 bool showConfigWindow = false, 38 54 bool saveScreenshotSilent = false, 39 55 int32 imageFileType = B_PNG_FORMAT, 40 int32 translator = 8); 56 int32 translator = 8, 57 BString outputFilename = ""); 41 58 virtual ~ScreenshotWindow(); 42 59 43 60 virtual void MessageReceived(BMessage* message); … … 67 84 status_t _GetActiveWindowFrame(BRect* frame); 68 85 void _MakeTabSpaceTransparent(BRect* frame); 69 86 87 void _AddFileParameterRule( 88 FileParameter index, 89 const char* name); 90 void _UpdateFileParameters(); 91 static struct FileParameterRule* 92 _UpdateFileParameterRule( 93 struct FileParameterRule* rule, 94 void* parent); 95 static struct FileParameterRule* 96 _ApplyFileParameterRule( 97 struct FileParameterRule* rule, 98 void* sourceStringPtr); 99 100 BString _ParseFilename(const char* name); 70 101 status_t _SaveScreenshot(); 71 102 72 103 PreviewView* fPreview; … … 94 125 bool fShowConfigWindow; 95 126 bool fSaveScreenshotSilent; 96 127 BString fExtension; 128 BString fOutputFilename; 97 129 130 BObjectList<FileParameterRule> 131 fFilenameParameters; 132 98 133 int32 fTranslator; 99 134 int32 fImageFileType; 100 135 }; 101 136 137 102 138 #endif /* SCREENSHOT_WINDOW_H */ -
src/apps/screenshot/Screenshot.cpp
14 14 #include <stdio.h> 15 15 #include <stdlib.h> 16 16 #include <string.h> 17 #include <String.h> 17 18 #include <Catalog.h> 18 19 #include <Locale.h> 19 20 … … 85 86 { 86 87 bigtime_t delay = 0; 87 88 89 BString outputFilename(""); 88 90 bool includeBorder = false; 89 91 bool includeMouse = false; 90 92 bool grabActiveWindow = false; … … 127 129 , argv[i]); 128 130 exit(0); 129 131 } 130 } 132 } else if (i == argc - 1) 133 outputFilename = BString(argv[i]); 131 134 } 132 135 133 136 fArgvReceived = true; 134 137 135 138 new ScreenshotWindow(delay, includeBorder, includeMouse, grabActiveWindow, 136 139 showConfigureWindow, saveScreenshotSilent, fImageFileType, 137 fTranslator );140 fTranslator, outputFilename); 138 141 } 139 142 140 143 141 144 void 142 145 Screenshot::_ShowHelp() const 143 146 { 144 printf("Screenshot [OPTION]... Creates a bitmap of the current screen\n\n"); 145 printf("OPTION\n"); 147 printf("Screenshot [OPTIONS] [FILE] Creates a bitmap of the current screen\n\n"); 148 printf("FILE is the filename to store the screenshot to, if not specified, a filename will be generated (only for silent mode).\n\n"); 149 printf("FILE can contain date/time parameters preceded by a %%, just as strftime. Screenshot related parameters are preceded by a $ :\n"); 150 printf(" $w Screenshot width\n"); 151 printf(" $h Screenshot height\n"); 152 printf(" $t Screenshot format\n\n"); 153 154 printf("OPTIONS\n"); 146 155 printf(" -o, --options Show options window first\n"); 147 156 printf(" -m, --mouse-pointer Include the mouse pointer\n"); 148 157 printf(" -b, --border Include the window border\n");