Ticket #5724: screenshotEnhancement-parametricFilename.patch

File screenshotEnhancement-parametricFilename.patch, 10.2 KB (added by Shisui, 14 years ago)

Patch implementing the parametric filenames functionality

  • src/apps/screenshot/ScreenshotWindow.cpp

     
    55 * Authors:
    66 *      Karsten Heimrich
    77 *      Fredrik Modéen
     8 *      Christophe Huriaux
    89 */
    910
    1011#include "ScreenshotWindow.h"
     
    3738#include <MenuItem.h>
    3839#include <Message.h>
    3940#include <NodeInfo.h>
     41#include <ObjectList.h>
    4042#include <Path.h>
    4143#include <RadioButton.h>
    4244#include <Region.h>
     
    9193
    9294ScreenshotWindow::ScreenshotWindow(bigtime_t delay, bool includeBorder,
    9395    bool includeMouse, bool grabActiveWindow, bool showConfigWindow,
    94     bool saveScreenshotSilent, int32 imageFileType, int32 translator)
     96    bool saveScreenshotSilent, int32 imageFileType, int32 translator,
     97    BString outputFilename)
    9598    :
    9699    BWindow(BRect(0, 0, 200.0, 100.0), TR("Retake screenshot"), B_TITLED_WINDOW,
    97100        B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_QUIT_ON_WINDOW_CLOSE |
     
    108111    fShowConfigWindow(showConfigWindow),
    109112    fSaveScreenshotSilent(saveScreenshotSilent),
    110113    fExtension(""),
     114    fOutputFilename(outputFilename),
    111115    fTranslator(translator),
    112116    fImageFileType(imageFileType)
    113 {
     117{   
     118    _AddFileParameterRule(kImageHeight, "$h");
     119    _AddFileParameterRule(kImageWidth, "$w");
     120    _AddFileParameterRule(kImageFormat, "$t");
     121
    114122    if (fSaveScreenshotSilent) {
    115123        _TakeScreenshot();
    116124        _SaveScreenshot();
     
    128136    if (fOutputPathPanel)
    129137        delete fOutputPathPanel->RefFilter();
    130138
     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
    131147    delete fScreenshot;
    132148    delete fOutputPathPanel;
    133149}
     
    287303        _TakeScreenshot();
    288304        _UpdatePreviewPanel();
    289305        layout->SetVisibleItem(1L);
    290         fSaveScreenshot->MakeDefault(true);
     306        SetDefaultButton(fSaveScreenshot);
    291307    } else {
    292308        layout->SetVisibleItem(0L);
    293309        SetDefaultButton(NULL);
     
    559575    // to prevent a preview way too wide
    560576    if (width > 400.0f) {
    561577        width = 400.0f;
    562         height = (fScreenshot->Bounds().Height() /
    563             fScreenshot->Bounds().Width()) * width;
     578        height = (fScreenshot->Bounds().Height()
     579            / fScreenshot->Bounds().Width()) * width;
    564580    }
    565581
    566582    fPreview->SetExplicitMinSize(BSize(width, height));
     
    585601{
    586602    fNameControl->MakeFocus(true);
    587603    fNameControl->TextView()->Select(0,
    588         fNameControl->TextView()->TextLength() -
    589         fExtension.Length());
     604        fNameControl->TextView()->TextLength()
     605        - fExtension.Length());
    590606    fNameControl->TextView()->ScrollToSelection();
    591607}
    592608
     
    741757            _MakeTabSpaceTransparent(&frame);
    742758    } else
    743759        BScreen(this).GetBitmap(&fScreenshot, fIncludeMouse);
     760
     761    _UpdateFileParameters();
    744762}
    745763
    746764
     
    794812}
    795813
    796814
     815void
     816ScreenshotWindow::_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
     829void
     830ScreenshotWindow::_UpdateFileParameters()
     831{
     832    fFilenameParameters.EachElement(ScreenshotWindow::_UpdateFileParameterRule,
     833        static_cast<void*> (this));
     834}
     835
     836
     837struct FileParameterRule*
     838ScreenshotWindow::_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
     877struct FileParameterRule*
     878ScreenshotWindow::_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
     890BString
     891ScreenshotWindow::_ParseFilename(const char* originalFilename)
     892{
     893    BString parsedFilename;
     894    time_t currentTime = time(NULL);
     895    struct tm* timeInfo = localtime(&currentTime);
     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
    797916status_t
    798917ScreenshotWindow::_SaveScreenshot()
    799918{
     
    805924    if (path == NULL)
    806925        return B_ERROR;
    807926
    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()));
    813935   
    814936    BEntry entry;
    815937    entry.SetTo(path.Path());
  • src/apps/screenshot/Jamfile

     
    11SubDir HAIKU_TOP src apps screenshot ;
    22
    33UseLibraryHeaders zlib ;
    4 UsePrivateHeaders interface ;
     4UsePrivateHeaders interface shared ;
    55
    66Application Screenshot :
    77    main.cpp
  • src/apps/screenshot/ScreenshotWindow.h

     
    55 * Authors:
    66 *      Karsten Heimrich
    77 *      Fredrik Modéen
     8 *      Christophe Huriaux
    89 */
    910#ifndef SCREENSHOT_WINDOW_H
    1011#define SCREENSHOT_WINDOW_H
    1112
    1213
     14#include <ObjectList.h>
    1315#include <String.h>
    1416#include <Window.h>
    1517#include <TranslatorFormats.h>
     
    2830class PreviewView;
    2931
    3032
     33typedef enum {
     34    kImageWidth,
     35    kImageHeight,
     36    kImageFormat
     37} FileParameter;
     38
     39
     40struct FileParameterRule {
     41    FileParameter       parameter;
     42    BString             name;
     43    BString             replacementValue;
     44};
     45
     46
    3147class ScreenshotWindow : public BWindow {
    3248public:
    3349                            ScreenshotWindow(bigtime_t delay = 0,
     
    3753                                bool showConfigWindow = false,
    3854                                bool saveScreenshotSilent = false,
    3955                                int32 imageFileType = B_PNG_FORMAT,
    40                                 int32 translator = 8);
     56                                int32 translator = 8,
     57                                BString outputFilename = "");
    4158    virtual                 ~ScreenshotWindow();
    4259
    4360    virtual void            MessageReceived(BMessage* message);
     
    6784            status_t        _GetActiveWindowFrame(BRect* frame);
    6885            void            _MakeTabSpaceTransparent(BRect* frame);
    6986
     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);
    70101            status_t        _SaveScreenshot();
    71102
    72103            PreviewView*    fPreview;
     
    94125            bool            fShowConfigWindow;
    95126            bool            fSaveScreenshotSilent;
    96127            BString         fExtension;
     128            BString         fOutputFilename;
    97129
     130            BObjectList<FileParameterRule>
     131                            fFilenameParameters;
     132
    98133            int32           fTranslator;
    99134            int32           fImageFileType;
    100135};
    101136
     137
    102138#endif  /* SCREENSHOT_WINDOW_H */
  • src/apps/screenshot/Screenshot.cpp

     
    1414#include <stdio.h>
    1515#include <stdlib.h>
    1616#include <string.h>
     17#include <String.h>
    1718#include <Catalog.h>
    1819#include <Locale.h>
    1920
     
    8586{
    8687    bigtime_t delay = 0;
    8788
     89    BString outputFilename("");
    8890    bool includeBorder = false;
    8991    bool includeMouse = false;
    9092    bool grabActiveWindow = false;
     
    127129                    , argv[i]);
    128130                exit(0);
    129131            }
    130         }
     132        } else if (i == argc - 1)
     133            outputFilename = BString(argv[i]);
    131134    }
    132135   
    133136    fArgvReceived = true;
    134137   
    135138    new ScreenshotWindow(delay, includeBorder, includeMouse, grabActiveWindow,
    136139        showConfigureWindow, saveScreenshotSilent, fImageFileType,
    137         fTranslator);
     140        fTranslator, outputFilename);
    138141}
    139142
    140143
    141144void
    142145Screenshot::_ShowHelp() const
    143146{
    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");
    146155    printf("  -o, --options         Show options window first\n");
    147156    printf("  -m, --mouse-pointer   Include the mouse pointer\n");
    148157    printf("  -b, --border          Include the window border\n");