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
|
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();
|
if (!bitmap)
|
||||||
BBitmapStream stream(bitmap); // destructor deletes bitmap
|
// If no bitmap is supplied, write out the whole image
|
||||||
// write data
|
bitmap = fBitmap;
|
||||||
BFile file(dir, name, B_WRITE_ONLY);
|
|
||||||
roster->Translate(&stream, NULL, NULL, &file, format->type);
|
BBitmapStream stream(bitmap);
|
||||||
// set mime type
|
|
||||||
BNodeInfo info(&file);
|
bool loop = true;
|
||||||
if (info.InitCheck() == B_OK) {
|
while (loop) {
|
||||||
info.SetType(format->MIME);
|
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
|
void
|
||||||
@@ -1065,6 +1091,7 @@ ShowImageView::HandleDrop(BMessage* msg)
|
|||||||
if (saveToFile) {
|
if (saveToFile) {
|
||||||
BDirectory dir(&dirRef);
|
BDirectory dir(&dirRef);
|
||||||
SaveToFile(&dir, name.String(), bitmap, &format);
|
SaveToFile(&dir, name.String(), bitmap, &format);
|
||||||
|
delete bitmap;
|
||||||
} else if (sendInMessage) {
|
} else if (sendInMessage) {
|
||||||
SendInMessage(msg, bitmap, &format);
|
SendInMessage(msg, bitmap, &format);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ public:
|
|||||||
void Pulse();
|
void Pulse();
|
||||||
|
|
||||||
status_t SetImage(const entry_ref *pref);
|
status_t SetImage(const entry_ref *pref);
|
||||||
|
void SaveToFile(BDirectory* dir, const char* name, BBitmap* bitmap, const translation_format* format);
|
||||||
void SetDither(bool dither);
|
void SetDither(bool dither);
|
||||||
bool GetDither() const { return fDither; }
|
bool GetDither() const { return fDither; }
|
||||||
void SetShowCaption(bool show);
|
void SetShowCaption(bool show);
|
||||||
@@ -166,7 +167,6 @@ private:
|
|||||||
BBitmap* CopySelection(uchar alpha = 255, bool imageSize = true);
|
BBitmap* CopySelection(uchar alpha = 255, bool imageSize = true);
|
||||||
bool AddSupportedTypes(BMessage* msg, BBitmap* bitmap);
|
bool AddSupportedTypes(BMessage* msg, BBitmap* bitmap);
|
||||||
void BeginDrag(BPoint sourcePoint);
|
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);
|
void SendInMessage(BMessage* msg, BBitmap* bitmap, translation_format* format);
|
||||||
bool OutputFormatForType(BBitmap* bitmap, const char* type, translation_format* format);
|
bool OutputFormatForType(BBitmap* bitmap, const char* type, translation_format* format);
|
||||||
void HandleDrop(BMessage* msg);
|
void HandleDrop(BMessage* msg);
|
||||||
|
|||||||
@@ -816,26 +816,26 @@ ShowImageWindow::SaveToFile(BMessage *pmsg)
|
|||||||
reinterpret_cast<int32 *>(&outType)) != B_OK)
|
reinterpret_cast<int32 *>(&outType)) != B_OK)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Create the output file
|
// Find the translator_format information needed to
|
||||||
BDirectory dir(&dirref);
|
// write a MIME attribute for the image file
|
||||||
BFile file(&dir, filename, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
|
BTranslatorRoster *roster = BTranslatorRoster::Default();
|
||||||
if (file.InitCheck() != B_OK)
|
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;
|
return;
|
||||||
|
|
||||||
// Translate the image and write it out to the output file
|
// Write out the image file
|
||||||
BBitmapStream stream(fImageView->GetBitmap());
|
BDirectory dir(&dirref);
|
||||||
BTranslatorRoster *proster = BTranslatorRoster::Default();
|
fImageView->SaveToFile(&dir, filename, NULL, &pouts[i]);
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
|||||||
Reference in New Issue
Block a user