Patch by augiedoggie: Remove the hardcoded translator_id values from Screenshot.

Fixes ticket #5733. Thanks a lot! (Note: small coding style fixes by myself,
so expect merge conflicts.)


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36342 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus 2010-04-19 08:21:21 +00:00
parent 45538a5e31
commit c9ec9c7211
4 changed files with 75 additions and 42 deletions

View File

@ -29,8 +29,7 @@ Screenshot::Screenshot()
BApplication("application/x-vnd.Haiku-Screenshot"),
fArgvReceived(false),
fRefsReceived(false),
fImageFileType(B_PNG_FORMAT),
fTranslator(8)
fImageFileType(B_PNG_FORMAT)
{
be_locale->GetAppCatalog(&fCatalog);
}
@ -136,7 +135,7 @@ Screenshot::ArgvReceived(int32 argc, char** argv)
new ScreenshotWindow(delay, includeBorder, includeMouse, grabActiveWindow,
showConfigureWindow, saveScreenshotSilent, fImageFileType,
fTranslator, outputFilename);
outputFilename);
}
@ -164,26 +163,20 @@ Screenshot::_ShowHelp() const
void
Screenshot::_SetImageTypeSilence(const char* name)
{
if (strcmp(name, "bmp") == 0) {
if (strcmp(name, "bmp") == 0)
fImageFileType = B_BMP_FORMAT;
fTranslator = 1;
} else if (strcmp(name, "gif") == 0) {
else if (strcmp(name, "gif") == 0)
fImageFileType = B_GIF_FORMAT;
fTranslator = 3;
} else if (strcmp(name, "jpg") == 0) {
else if (strcmp(name, "jpg") == 0 || strcmp(name, "jpeg") == 0)
fImageFileType = B_JPEG_FORMAT;
fTranslator = 6;
} else if (strcmp(name, "ppm") == 0) {
else if (strcmp(name, "ppm") == 0)
fImageFileType = B_PPM_FORMAT;
fTranslator = 9;
} else if (strcmp(name, "targa") == 0) {
else if (strcmp(name, "targa") == 0 || strcmp(name, "tga") == 0)
fImageFileType = B_TGA_FORMAT;
fTranslator = 14;
} else if (strcmp(name, "tif") == 0) {
else if (strcmp(name, "tif") == 0 || strcmp(name, "tiff") == 0)
fImageFileType = B_TIFF_FORMAT;
fTranslator = 15;
} else { //png
else {
// Default to PNG.
fImageFileType = B_PNG_FORMAT;
fTranslator = 8;
}
}

View File

@ -30,7 +30,6 @@ private:
bool fArgvReceived;
bool fRefsReceived;
int32 fImageFileType;
int32 fTranslator;
BCatalog fCatalog;
};

View File

@ -98,8 +98,7 @@ public:
ScreenshotWindow::ScreenshotWindow(bigtime_t delay, bool includeBorder,
bool includeMouse, bool grabActiveWindow, bool showConfigWindow,
bool saveScreenshotSilent, int32 imageFileType, int32 translator,
const char* outputFilename)
bool saveScreenshotSilent, int32 imageFileType, const char* outputFilename)
:
BWindow(BRect(0, 0, 200.0, 100.0), TR("Retake screenshot"), B_TITLED_WINDOW,
B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_QUIT_ON_WINDOW_CLOSE |
@ -117,7 +116,6 @@ ScreenshotWindow::ScreenshotWindow(bigtime_t delay, bool includeBorder,
fSaveScreenshotSilent(saveScreenshotSilent),
fOutputFilename(outputFilename),
fExtension(""),
fTranslator(translator),
fImageFileType(imageFileType)
{
if (fSaveScreenshotSilent) {
@ -185,7 +183,6 @@ ScreenshotWindow::MessageReceived(BMessage* message)
case kImageOutputFormat:
message->FindInt32("be:type", &fImageFileType);
message->FindInt32("be:translator", &fTranslator);
fNameControl->SetText(_FindValidFileName(fNameControl->Text()).String());
_UpdateFilenameSelection();
break;
@ -616,27 +613,32 @@ ScreenshotWindow::_FindValidFileName(const char* name)
return baseName;
BTranslatorRoster* roster = BTranslatorRoster::Default();
const translation_format* formats = NULL;
int32 numFormats;
if (roster->GetOutputFormats(fTranslator, &formats, &numFormats) == B_OK) {
for (int32 i = 0; i < numFormats; ++i) {
if (formats[i].type == uint32(fImageFileType)) {
BMimeType mimeType(formats[i].MIME);
BMessage msgExtensions;
if (mimeType.GetFileExtensions(&msgExtensions) == B_OK) {
const char* extension;
if (msgExtensions.FindString("extensions", 0, &extension) == B_OK) {
fExtension.SetTo(extension);
fExtension.Prepend(".");
} else
fExtension.SetTo("");
translator_id id = 0;
if (_FindTranslator(fImageFileType, &id) == B_OK) {
const translation_format* formats = NULL;
int32 numFormats;
if (roster->GetOutputFormats(id, &formats, &numFormats) == B_OK) {
for (int32 i = 0; i < numFormats; ++i) {
if (formats[i].type == uint32(fImageFileType)) {
BMimeType mimeType(formats[i].MIME);
BMessage msgExtensions;
if (mimeType.GetFileExtensions(&msgExtensions) == B_OK) {
const char* extension;
if (msgExtensions.FindString("extensions", 0, &extension) == B_OK) {
fExtension.SetTo(extension);
fExtension.Prepend(".");
} else
fExtension.SetTo("");
}
break;
}
break;
}
}
}
BPath outputPath = orgPath;
BString fileName;
fileName << baseName << fExtension;
@ -855,10 +857,15 @@ ScreenshotWindow::_SaveScreenshot()
if (nodeInfo.InitCheck() != B_OK)
return B_ERROR;
translator_id id = 0;
if (_FindTranslator(fImageFileType, &id) != B_OK)
return B_ERROR;
int32 numFormats;
const translation_format* formats = NULL;
if (roster->GetOutputFormats(fTranslator, &formats, &numFormats) != B_OK)
return B_OK;
if (roster->GetOutputFormats(id, &formats, &numFormats) != B_OK)
return B_ERROR;
for (int32 i = 0; i < numFormats; ++i) {
if (formats[i].type == uint32(fImageFileType)) {
@ -866,6 +873,7 @@ ScreenshotWindow::_SaveScreenshot()
break;
}
}
return B_OK;
}
@ -958,3 +966,35 @@ ScreenshotWindow::_MakeTabSpaceTransparent(BRect* frame)
}
fScreenshot->RemoveChild(&view);
}
status_t
ScreenshotWindow::_FindTranslator(uint32 imageType, translator_id* id)
{
translator_id* translators = NULL;
int32 numTranslators = 0;
BTranslatorRoster* roster = BTranslatorRoster::Default();
status_t status = roster->GetAllTranslators(&translators, &numTranslators);
if (status != B_OK)
return status;
status = B_ERROR;
for (int32 x = 0; x < numTranslators && status != B_OK; x++) {
int32 numFormats;
const translation_format* formats = NULL;
if (roster->GetOutputFormats(x, &formats, &numFormats) == B_OK) {
for (int32 i = 0; i < numFormats; ++i) {
if (formats[i].type == imageType) {
*id = x;
status = B_OK;
break;
}
}
}
}
delete [] translators;
return status;
}

View File

@ -12,6 +12,7 @@
#include <String.h>
#include <Window.h>
#include <TranslationDefs.h>
#include <TranslatorFormats.h>
@ -37,7 +38,6 @@ public:
bool showConfigWindow = false,
bool saveScreenshotSilent = false,
int32 imageFileType = B_PNG_FORMAT,
int32 translator = 8,
const char* outputFilename = NULL);
virtual ~ScreenshotWindow();
@ -70,6 +70,8 @@ private:
status_t _SaveScreenshot();
status_t _FindTranslator(uint32 imageType, translator_id* id);
PreviewView* fPreview;
BRadioButton* fActiveWindow;
BRadioButton* fWholeDesktop;
@ -97,7 +99,6 @@ private:
BString fOutputFilename;
BString fExtension;
int32 fTranslator;
int32 fImageFileType;
};