Ticket #5733: screenshot3.diff

File screenshot3.diff, 6.7 KB (added by augiedoggie, 14 years ago)

revised alternate patch which looks up translator id values on demand

  • src/apps/screenshot/Screenshot.h

     
    3030    bool                fArgvReceived;
    3131    bool                fRefsReceived;
    3232    int32               fImageFileType;
    33     int32               fTranslator;
    3433    BCatalog                fCatalog;
    3534};
    3635
  • src/apps/screenshot/ScreenshotWindow.cpp

     
    9898
    9999ScreenshotWindow::ScreenshotWindow(bigtime_t delay, bool includeBorder,
    100100    bool includeMouse, bool grabActiveWindow, bool showConfigWindow,
    101     bool saveScreenshotSilent, int32 imageFileType, int32 translator,
    102     const char* outputFilename)
     101    bool saveScreenshotSilent, int32 imageFileType, const char* outputFilename)
    103102    :
    104103    BWindow(BRect(0, 0, 200.0, 100.0), TR("Retake screenshot"), B_TITLED_WINDOW,
    105104        B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_QUIT_ON_WINDOW_CLOSE |
     
    117116    fSaveScreenshotSilent(saveScreenshotSilent),
    118117    fOutputFilename(outputFilename),
    119118    fExtension(""),
    120     fTranslator(translator),
    121119    fImageFileType(imageFileType)
    122120{
    123121    if (fSaveScreenshotSilent) {
     
    185183       
    186184        case kImageOutputFormat:
    187185            message->FindInt32("be:type", &fImageFileType);
    188             message->FindInt32("be:translator", &fTranslator);
    189186            fNameControl->SetText(_FindValidFileName(fNameControl->Text()).String());
    190187            _UpdateFilenameSelection();
    191188            break;
     
    616613        return baseName;
    617614   
    618615    BTranslatorRoster* roster = BTranslatorRoster::Default();
    619     const translation_format* formats = NULL;
    620616
    621     int32 numFormats;
    622     if (roster->GetOutputFormats(fTranslator, &formats, &numFormats) == B_OK) {
    623         for (int32 i = 0; i < numFormats; ++i) {
    624             if (formats[i].type == uint32(fImageFileType)) {
    625                 BMimeType mimeType(formats[i].MIME);
    626                 BMessage msgExtensions;             
    627                 if (mimeType.GetFileExtensions(&msgExtensions) == B_OK) {
    628                     const char* extension;
    629                     if (msgExtensions.FindString("extensions", 0, &extension) == B_OK) {
    630                         fExtension.SetTo(extension);
    631                         fExtension.Prepend(".");
    632                     } else
    633                         fExtension.SetTo("");
     617    translator_id id = 0;
     618    if (_FindTranslator(fImageFileType, &id) == B_OK) {
     619
     620        const translation_format* formats = NULL;
     621   
     622        int32 numFormats;
     623        if (roster->GetOutputFormats(id, &formats, &numFormats) == B_OK) {
     624            for (int32 i = 0; i < numFormats; ++i) {
     625                if (formats[i].type == uint32(fImageFileType)) {
     626                    BMimeType mimeType(formats[i].MIME);
     627                    BMessage msgExtensions;             
     628                    if (mimeType.GetFileExtensions(&msgExtensions) == B_OK) {
     629                        const char* extension;
     630                        if (msgExtensions.FindString("extensions", 0, &extension) == B_OK) {
     631                            fExtension.SetTo(extension);
     632                            fExtension.Prepend(".");
     633                        } else
     634                            fExtension.SetTo("");
     635                    }
     636                    break;
    634637                }
    635                 break;
    636638            }
    637639        }
    638640    }
    639    
     641
    640642    BPath outputPath = orgPath;
    641643    BString fileName;
    642644    fileName << baseName << fExtension;
     
    855857    if (nodeInfo.InitCheck() != B_OK)
    856858        return B_ERROR;
    857859
     860    translator_id id = 0;
     861    if (_FindTranslator(fImageFileType, &id) != B_OK)
     862        return B_ERROR;
     863
    858864    int32 numFormats;
    859865    const translation_format* formats = NULL;
    860     if (roster->GetOutputFormats(fTranslator, &formats, &numFormats) != B_OK)
    861         return B_OK;
    862866
     867    if (roster->GetOutputFormats(id, &formats, &numFormats) != B_OK)
     868        return B_ERROR;
     869
    863870    for (int32 i = 0; i < numFormats; ++i) {
    864871        if (formats[i].type == uint32(fImageFileType)) {
    865872            nodeInfo.SetType(formats[i].MIME);
    866873            break;
    867874        }
    868875    }
     876
    869877    return B_OK;
    870878}
    871879
     
    958966    }
    959967    fScreenshot->RemoveChild(&view);
    960968}
     969
     970
     971status_t
     972ScreenshotWindow::_FindTranslator(uint32 imageType, translator_id* id)
     973{
     974    status_t status = B_ERROR;
     975
     976    translator_id* translators = NULL;
     977    int32 numTranslators = 0;
     978    BTranslatorRoster* roster = BTranslatorRoster::Default();
     979    roster->GetAllTranslators(&translators, &numTranslators);
     980
     981    for (int32 x = 0; x < numTranslators && status != B_OK; x++) {
     982        int32 numFormats;
     983        const translation_format* formats = NULL;
     984
     985        if (roster->GetOutputFormats(x, &formats, &numFormats) == B_OK) {
     986            for (int32 i = 0; i < numFormats; ++i) {
     987                if (formats[i].type == imageType) {
     988                    *id = x;
     989                    status = B_OK;
     990                    break;
     991                }
     992            }
     993        }
     994    }
     995
     996    delete [] translators;
     997
     998    return status;
     999}
  • src/apps/screenshot/ScreenshotWindow.h

     
    1212
    1313#include <String.h>
    1414#include <Window.h>
     15#include <TranslationDefs.h>
    1516#include <TranslatorFormats.h>
    1617
    1718
     
    3738                                bool showConfigWindow = false,
    3839                                bool saveScreenshotSilent = false,
    3940                                int32 imageFileType = B_PNG_FORMAT,
    40                                 int32 translator = 8,
    4141                                const char* outputFilename = NULL);
    4242    virtual                 ~ScreenshotWindow();
    4343
     
    7070
    7171            status_t        _SaveScreenshot();
    7272
     73            status_t        _FindTranslator(uint32 imageType, translator_id* id);
     74
    7375            PreviewView*    fPreview;
    7476            BRadioButton*   fActiveWindow;
    7577            BRadioButton*   fWholeDesktop;
     
    9799            BString         fOutputFilename;
    98100            BString         fExtension;
    99101
    100             int32           fTranslator;
    101102            int32           fImageFileType;
    102103};
    103104
  • src/apps/screenshot/Screenshot.cpp

     
    2929    BApplication("application/x-vnd.Haiku-Screenshot"),
    3030    fArgvReceived(false),
    3131    fRefsReceived(false),
    32     fImageFileType(B_PNG_FORMAT),
    33     fTranslator(8)
     32    fImageFileType(B_PNG_FORMAT)
    3433{
    3534    be_locale->GetAppCatalog(&fCatalog);
    3635}
     
    136135   
    137136    new ScreenshotWindow(delay, includeBorder, includeMouse, grabActiveWindow,
    138137        showConfigureWindow, saveScreenshotSilent, fImageFileType,
    139         fTranslator, outputFilename);
     138        outputFilename);
    140139}
    141140
    142141
     
    166165{
    167166    if (strcmp(name, "bmp") == 0) {
    168167        fImageFileType = B_BMP_FORMAT;
    169         fTranslator = 1;
    170168    } else if (strcmp(name, "gif") == 0) {
    171169        fImageFileType = B_GIF_FORMAT;
    172         fTranslator = 3;
    173170    } else if (strcmp(name, "jpg") == 0) {
    174171        fImageFileType = B_JPEG_FORMAT;
    175         fTranslator = 6;
    176172    } else if (strcmp(name, "ppm") == 0) {
    177173        fImageFileType = B_PPM_FORMAT;
    178         fTranslator = 9;
    179174    } else if (strcmp(name, "targa") == 0) {
    180175        fImageFileType = B_TGA_FORMAT;
    181         fTranslator = 14;
    182176    } else if (strcmp(name, "tif") == 0) {
    183177        fImageFileType = B_TIFF_FORMAT;
    184         fTranslator = 15;
    185178    } else { //png
    186179        fImageFileType = B_PNG_FORMAT;
    187         fTranslator = 8;
    188180    }
    189181}