Fixed file saving code so that it is only in one place, (instead of two) and fixed File-->Save As so that it now writes out the type attribute
git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6127 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1010,18 +1010,44 @@ ShowImageView::OutputFormatForType(BBitmap* bitmap, const char* type, translatio
|
||||
}
|
||||
|
||||
void
|
||||
ShowImageView::SaveToFile(BDirectory* dir, const char* name, BBitmap* bitmap, translation_format* format)
|
||||
ShowImageView::SaveToFile(BDirectory* dir, const char* name, BBitmap* bitmap, const translation_format* format)
|
||||
{
|
||||
BTranslatorRoster *roster = BTranslatorRoster::Default();
|
||||
BBitmapStream stream(bitmap); // destructor deletes bitmap
|
||||
// write data
|
||||
BFile file(dir, name, B_WRITE_ONLY);
|
||||
roster->Translate(&stream, NULL, NULL, &file, format->type);
|
||||
// set mime type
|
||||
BNodeInfo info(&file);
|
||||
if (info.InitCheck() == B_OK) {
|
||||
info.SetType(format->MIME);
|
||||
if (!bitmap)
|
||||
// If no bitmap is supplied, write out the whole image
|
||||
bitmap = fBitmap;
|
||||
|
||||
BBitmapStream stream(bitmap);
|
||||
|
||||
bool loop = true;
|
||||
while (loop) {
|
||||
BTranslatorRoster *roster = BTranslatorRoster::Default();
|
||||
if (!roster)
|
||||
break;
|
||||
// write data
|
||||
BFile file(dir, name, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
|
||||
if (file.InitCheck() != B_OK)
|
||||
break;
|
||||
if (roster->Translate(&stream, NULL, NULL, &file, format->type) < B_OK)
|
||||
break;
|
||||
// set mime type
|
||||
BNodeInfo info(&file);
|
||||
if (info.InitCheck() == B_OK)
|
||||
info.SetType(format->MIME);
|
||||
|
||||
loop = false;
|
||||
// break out of loop gracefully (indicates no errors)
|
||||
}
|
||||
if (loop) {
|
||||
// If loop terminated because of a break, there was an error
|
||||
BString errText;
|
||||
errText << "Sorry, the file '" << name << "' could not be written.";
|
||||
BAlert *palert = new BAlert(NULL, errText.String(), "Ok");
|
||||
palert->Go();
|
||||
}
|
||||
|
||||
stream.DetachBitmap(&bitmap);
|
||||
// Don't allow the bitmap to be deleted, this is
|
||||
// especially important when using fBitmap as the bitmap
|
||||
}
|
||||
|
||||
void
|
||||
@@ -1065,6 +1091,7 @@ ShowImageView::HandleDrop(BMessage* msg)
|
||||
if (saveToFile) {
|
||||
BDirectory dir(&dirRef);
|
||||
SaveToFile(&dir, name.String(), bitmap, &format);
|
||||
delete bitmap;
|
||||
} else if (sendInMessage) {
|
||||
SendInMessage(msg, bitmap, &format);
|
||||
} else {
|
||||
|
||||
@@ -50,6 +50,7 @@ public:
|
||||
void Pulse();
|
||||
|
||||
status_t SetImage(const entry_ref *pref);
|
||||
void SaveToFile(BDirectory* dir, const char* name, BBitmap* bitmap, const translation_format* format);
|
||||
void SetDither(bool dither);
|
||||
bool GetDither() const { return fDither; }
|
||||
void SetShowCaption(bool show);
|
||||
@@ -166,7 +167,6 @@ private:
|
||||
BBitmap* CopySelection(uchar alpha = 255, bool imageSize = true);
|
||||
bool AddSupportedTypes(BMessage* msg, BBitmap* bitmap);
|
||||
void BeginDrag(BPoint sourcePoint);
|
||||
void SaveToFile(BDirectory* dir, const char* name, BBitmap* bitmap, translation_format* format);
|
||||
void SendInMessage(BMessage* msg, BBitmap* bitmap, translation_format* format);
|
||||
bool OutputFormatForType(BBitmap* bitmap, const char* type, translation_format* format);
|
||||
void HandleDrop(BMessage* msg);
|
||||
|
||||
@@ -816,26 +816,26 @@ ShowImageWindow::SaveToFile(BMessage *pmsg)
|
||||
reinterpret_cast<int32 *>(&outType)) != B_OK)
|
||||
return;
|
||||
|
||||
// Create the output file
|
||||
BDirectory dir(&dirref);
|
||||
BFile file(&dir, filename, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
|
||||
if (file.InitCheck() != B_OK)
|
||||
// Find the translator_format information needed to
|
||||
// write a MIME attribute for the image file
|
||||
BTranslatorRoster *roster = BTranslatorRoster::Default();
|
||||
const translation_format *pouts = NULL;
|
||||
int32 outsCount = 0;
|
||||
if (roster->GetOutputFormats(outTranslator, &pouts, &outsCount) != B_OK)
|
||||
return;
|
||||
if (outsCount < 1)
|
||||
return;
|
||||
int32 i;
|
||||
for (i = 0; i < outsCount; i++) {
|
||||
if (pouts[i].group == B_TRANSLATOR_BITMAP && pouts[i].type == outType)
|
||||
break;
|
||||
}
|
||||
if (i == outsCount)
|
||||
return;
|
||||
|
||||
// Translate the image and write it out to the output file
|
||||
BBitmapStream stream(fImageView->GetBitmap());
|
||||
BTranslatorRoster *proster = BTranslatorRoster::Default();
|
||||
if (proster->Translate(outTranslator, &stream, NULL,
|
||||
&file, outType) != B_OK) {
|
||||
BAlert *palert = new BAlert(NULL, "Error writing image file.", "Ok");
|
||||
palert->Go();
|
||||
} else
|
||||
fModified = false;
|
||||
|
||||
BBitmap *pout = NULL;
|
||||
stream.DetachBitmap(&pout);
|
||||
// bitmap used by stream still belongs to the view,
|
||||
// detach so it doesn't get deleted
|
||||
// Write out the image file
|
||||
BDirectory dir(&dirref);
|
||||
fImageView->SaveToFile(&dir, filename, NULL, &pouts[i]);
|
||||
}
|
||||
|
||||
bool
|
||||
|
||||
Reference in New Issue
Block a user