diff --git a/src/apps/icon-o-matic/IconEditorApp.cpp b/src/apps/icon-o-matic/IconEditorApp.cpp index c58ef8714a..36e546be3e 100644 --- a/src/apps/icon-o-matic/IconEditorApp.cpp +++ b/src/apps/icon-o-matic/IconEditorApp.cpp @@ -44,6 +44,7 @@ #include "SavePanel.h" #include "ShapeContainer.h" #include "SimpleFileSaver.h" +#include "SourceExporter.h" #include "SVGExporter.h" #include "SVGImporter.h" @@ -532,6 +533,9 @@ IconEditorApp::_CreateSaver(const entry_ref& ref, uint32 exportMode) case EXPORT_MODE_ICON_RDEF: saver = new SimpleFileSaver(new RDefExporter(), ref); break; + case EXPORT_MODE_ICON_SOURCE: + saver = new SimpleFileSaver(new SourceExporter(), ref); + break; case EXPORT_MODE_BITMAP: saver = new SimpleFileSaver(new BitmapExporter(64), ref); diff --git a/src/apps/icon-o-matic/IconEditorApp.h b/src/apps/icon-o-matic/IconEditorApp.h index d19ee5663d..794bbf7cfc 100644 --- a/src/apps/icon-o-matic/IconEditorApp.h +++ b/src/apps/icon-o-matic/IconEditorApp.h @@ -40,6 +40,7 @@ enum { EXPORT_MODE_ICON_ATTR, EXPORT_MODE_ICON_MIME_ATTR, EXPORT_MODE_ICON_RDEF, + EXPORT_MODE_ICON_SOURCE, }; typedef enum { diff --git a/src/apps/icon-o-matic/Jamfile b/src/apps/icon-o-matic/Jamfile index d2e6e99c3a..b5450d53e3 100644 --- a/src/apps/icon-o-matic/Jamfile +++ b/src/apps/icon-o-matic/Jamfile @@ -222,6 +222,7 @@ Application Icon-O-Matic : # import_export/flat_icon FlatIconExporter.cpp RDefExporter.cpp + SourceExporter.cpp # import_export/message MessageExporter.cpp diff --git a/src/apps/icon-o-matic/gui/SavePanel.cpp b/src/apps/icon-o-matic/gui/SavePanel.cpp index 3e4f0b184f..9f2126333c 100644 --- a/src/apps/icon-o-matic/gui/SavePanel.cpp +++ b/src/apps/icon-o-matic/gui/SavePanel.cpp @@ -243,6 +243,9 @@ SavePanel::SetExportMode(int32 mode) case EXPORT_MODE_ICON_RDEF: fRDefMI->SetMarked(true); break; + case EXPORT_MODE_ICON_SOURCE: + fSourceMI->SetMarked(true); + break; } if (mode != EXPORT_MODE_MESSAGE) @@ -386,6 +389,10 @@ SavePanel::_BuildMenu() EXPORT_MODE_ICON_RDEF); fFormatM->AddItem(fRDefMI); + fSourceMI = new SaveItem("HVIF Source Code", new BMessage(MSG_FORMAT), + EXPORT_MODE_ICON_SOURCE); + fFormatM->AddItem(fSourceMI); + fFormatM->AddSeparatorItem(); fSVGMI = new SaveItem("SVG", new BMessage(MSG_FORMAT), diff --git a/src/apps/icon-o-matic/gui/SavePanel.h b/src/apps/icon-o-matic/gui/SavePanel.h index ede2cf517e..19e799fb7e 100644 --- a/src/apps/icon-o-matic/gui/SavePanel.h +++ b/src/apps/icon-o-matic/gui/SavePanel.h @@ -75,6 +75,7 @@ class SavePanel : public BFilePanel, SaveItem* fNativeMI; SaveItem* fHVIFMI; SaveItem* fRDefMI; + SaveItem* fSourceMI; SaveItem* fSVGMI; SaveItem* fBitmapMI; SaveItem* fBitmapSetMI; diff --git a/src/apps/icon-o-matic/import_export/flat_icon/SourceExporter.cpp b/src/apps/icon-o-matic/import_export/flat_icon/SourceExporter.cpp new file mode 100644 index 0000000000..a59a946947 --- /dev/null +++ b/src/apps/icon-o-matic/import_export/flat_icon/SourceExporter.cpp @@ -0,0 +1,122 @@ +/* + * Copyright 2006-2007, Haiku. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Stephan Aßmus + */ + +#include "SourceExporter.h" + +#include + +#include + +// constructor +SourceExporter::SourceExporter() + : FlatIconExporter() +{ +} + +// destructor +SourceExporter::~SourceExporter() +{ +} + +// Export +status_t +SourceExporter::Export(const Icon* icon, BPositionIO* stream) +{ + BMallocIO buffer; + status_t ret = FlatIconExporter::Export(icon, &buffer); + if (ret < B_OK) + return ret; + + return _Export((const uint8*)buffer.Buffer(), buffer.BufferLength(), + stream); +} + +// MIMEType +const char* +SourceExporter::MIMEType() +{ + return "text/x-source-code"; +} + +// #pragma mark - + +// _Export +status_t +SourceExporter::_Export(const uint8* source, size_t sourceSize, + BPositionIO* stream) const +{ + char buffer[2048]; + // write header + sprintf(buffer, "\nconst unsigned char kIconName = {\n"); + size_t size = strlen(buffer); + + ssize_t written = stream->Write(buffer, size); + if (written < 0) + return (status_t)written; + if (written < (ssize_t)size) + return B_ERROR; + + status_t ret = B_OK; + const uint8* b = source; + + // print one line (12 values) + while (sourceSize > 12) { + sprintf(buffer, " 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x" + ", 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x" + ", 0x%.2x, 0x%.2x, 0x%.2x, 0x%.2x,\n", + b[0], b[1], b[2], b[3], + b[4], b[5], b[6], b[7], + b[8], b[9], b[10], b[11]); + + size = strlen(buffer); + written = stream->Write(buffer, size); + if (written != (ssize_t)size) { + if (written >= 0) + ret = B_ERROR; + else + ret = (status_t)written; + break; + } + + sourceSize -= 12; + b += 12; + } + // last line (up to 12 values) + if (ret >= B_OK && sourceSize > 0) { + for (size_t i = 0; i < sourceSize; i++) { + if (i == 0) + sprintf(buffer, " 0x%.2x", b[i]); + else + sprintf(buffer, ", 0x%.2x", b[i]); + size = strlen(buffer); + written = stream->Write(buffer, size); + if (written != (ssize_t)size) { + if (written >= 0) + ret = B_ERROR; + else + ret = (status_t)written; + break; + } + } + } + if (ret >= B_OK) { + // finish + sprintf(buffer, "\n};\n"); + size = strlen(buffer); + written = stream->Write(buffer, size); + if (written != (ssize_t)size) { + if (written >= 0) + ret = B_ERROR; + else + ret = (status_t)written; + } + } + + return ret; +} + diff --git a/src/apps/icon-o-matic/import_export/flat_icon/SourceExporter.h b/src/apps/icon-o-matic/import_export/flat_icon/SourceExporter.h new file mode 100644 index 0000000000..2bbe1f2351 --- /dev/null +++ b/src/apps/icon-o-matic/import_export/flat_icon/SourceExporter.h @@ -0,0 +1,31 @@ +/* + * Copyright 2006-2007, Haiku. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Stephan Aßmus + */ + +#ifndef SOURCE_EXPORTER_H +#define SOURCE_EXPORTER_H + +#include "FlatIconExporter.h" + +class SourceExporter : public FlatIconExporter { + public: + SourceExporter(); + virtual ~SourceExporter(); + + // FlatIconExporter interface + virtual status_t Export(const Icon* icon, + BPositionIO* stream); + + virtual const char* MIMEType(); + + private: + status_t _Export(const uint8* source, + size_t sourceSize, + BPositionIO* stream) const; +}; + +#endif // SOURCE_EXPORTER_H