From 08cf72ce977490ebdd1fe82d189fc448d756196a Mon Sep 17 00:00:00 2001 From: DarkWyrm Date: Sat, 6 Jan 2007 21:18:16 +0000 Subject: [PATCH] Initial checkin of a resource editor similar in style to QuickRes -- Resourcer's code is too difficult to maintain and the others on BeBits are either not good or the author could not be contacted. This is currently a work-in-progress - right now it can only view resources and attributes. Haiku target builds but R5 is broken because of a linking issue that will hopefully be resolved shortly. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@19734 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/Jamfile | 1 + src/apps/resedit/App.cpp | 101 +++++++++++++ src/apps/resedit/App.h | 36 +++++ src/apps/resedit/Editor.h | 21 +++ src/apps/resedit/Jamfile | 15 ++ src/apps/resedit/PreviewColumn.cpp | 72 +++++++++ src/apps/resedit/PreviewColumn.h | 21 +++ src/apps/resedit/ResView.cpp | 218 ++++++++++++++++++++++++++++ src/apps/resedit/ResView.h | 57 ++++++++ src/apps/resedit/ResWindow.cpp | 36 +++++ src/apps/resedit/ResWindow.h | 22 +++ src/apps/resedit/ResourceData.cpp | 151 +++++++++++++++++++ src/apps/resedit/ResourceData.h | 55 +++++++ src/apps/resedit/ResourceRoster.cpp | 146 +++++++++++++++++++ src/apps/resedit/ResourceRoster.h | 33 +++++ 15 files changed, 985 insertions(+) create mode 100644 src/apps/resedit/App.cpp create mode 100644 src/apps/resedit/App.h create mode 100644 src/apps/resedit/Editor.h create mode 100644 src/apps/resedit/Jamfile create mode 100644 src/apps/resedit/PreviewColumn.cpp create mode 100644 src/apps/resedit/PreviewColumn.h create mode 100644 src/apps/resedit/ResView.cpp create mode 100644 src/apps/resedit/ResView.h create mode 100644 src/apps/resedit/ResWindow.cpp create mode 100644 src/apps/resedit/ResWindow.h create mode 100644 src/apps/resedit/ResourceData.cpp create mode 100644 src/apps/resedit/ResourceData.h create mode 100644 src/apps/resedit/ResourceRoster.cpp create mode 100644 src/apps/resedit/ResourceRoster.h diff --git a/src/apps/Jamfile b/src/apps/Jamfile index e4b303afaa..b74bdd8bb2 100644 --- a/src/apps/Jamfile +++ b/src/apps/Jamfile @@ -22,6 +22,7 @@ SubInclude HAIKU_TOP src apps poorman ; SubInclude HAIKU_TOP src apps powerstatus ; SubInclude HAIKU_TOP src apps processcontroller ; SubInclude HAIKU_TOP src apps pulse ; +SubInclude HAIKU_TOP src apps resedit ; SubInclude HAIKU_TOP src apps showimage ; SubInclude HAIKU_TOP src apps soundrecorder ; SubInclude HAIKU_TOP src apps stylededit ; diff --git a/src/apps/resedit/App.cpp b/src/apps/resedit/App.cpp new file mode 100644 index 0000000000..bcae286f9e --- /dev/null +++ b/src/apps/resedit/App.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#include "App.h" +#include "ResWindow.h" +#include + +int +main(void) +{ + App app; + app.Run(); + return 0; +} + + +App::App(void) + : BApplication("application/x-vnd.Haiku-ResEdit"), + fWindowCount(0) +{ + fOpenPanel = new BFilePanel(); + fSavePanel = new BFilePanel(B_SAVE_PANEL); +} + + +App::~App(void) +{ + delete fOpenPanel; + delete fSavePanel; +} + + +void +App::ReadyToRun(void) +{ + if (fWindowCount < 1) { + ResWindow *win = new ResWindow(BRect(50,100,600,400)); + win->Show(); + } +} + + +void +App::MessageReceived(BMessage *msg) +{ + switch(msg->what) { + case M_REGISTER_WINDOW: { + fWindowCount++; + break; + } + case M_UNREGISTER_WINDOW: { + fWindowCount--; + if (fWindowCount == 0) + PostMessage(B_QUIT_REQUESTED); + break; + } + case M_SHOW_OPEN_PANEL: { + // Don't do anything if it's already open + if (fOpenPanel->IsShowing()) + break; + fOpenPanel->Show(); + break; + } + default: + BApplication::MessageReceived(msg); + } +} + + +void +App::RefsReceived(BMessage *msg) +{ + entry_ref ref; + int32 i=0; + while (msg->FindRef("refs",i,&ref) == B_OK) { + ResWindow *win = new ResWindow(BRect(50,100,600,400),&ref); + win->Show(); + i++; + } +} + + +bool +App::QuitRequested(void) +{ + for (int32 i = 0; i < CountWindows(); i++) { + BWindow *win = WindowAt(i); + if (fOpenPanel->Window() == win || fSavePanel->Window() == win) + continue; + + if (!win->QuitRequested()) + return false; + } + + return true; +} + diff --git a/src/apps/resedit/App.h b/src/apps/resedit/App.h new file mode 100644 index 0000000000..b5ddbc370c --- /dev/null +++ b/src/apps/resedit/App.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#ifndef APP_H +#define APP_H + +#include +#include + +enum { + M_REGISTER_WINDOW = 'regw', + M_UNREGISTER_WINDOW, + M_SHOW_OPEN_PANEL, + M_SHOW_SAVE_PANEL +}; + +class App : public BApplication +{ +public: + App(void); + ~App(void); + void MessageReceived(BMessage *msg); + void RefsReceived(BMessage *msg); + void ReadyToRun(void); + bool QuitRequested(void); + +private: + uint32 fWindowCount; + BFilePanel *fOpenPanel, *fSavePanel; +}; + +#endif diff --git a/src/apps/resedit/Editor.h b/src/apps/resedit/Editor.h new file mode 100644 index 0000000000..40b832a048 --- /dev/null +++ b/src/apps/resedit/Editor.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#ifndef EDITOR_H +#define EDITOR_H + +class Editor +{ +public: + Editor(void); + virtual ~Editor(void); + virtual BView * GetConfigView(void); + virtual void LoadData(unsigned char *data, const size_t &length); + virtual unsigned char * SaveData(size_t *data); +}; + +#endif diff --git a/src/apps/resedit/Jamfile b/src/apps/resedit/Jamfile new file mode 100644 index 0000000000..c399cf7df1 --- /dev/null +++ b/src/apps/resedit/Jamfile @@ -0,0 +1,15 @@ +SubDir HAIKU_TOP src apps resedit ; + +SetSubDirSupportedPlatformsBeOSCompatible ; + +UsePrivateHeaders shared interface ; + +Application ResEdit : + App.cpp + PreviewColumn.cpp + ResourceData.cpp + ResourceRoster.cpp + ResView.cpp + ResWindow.cpp + : be tracker translation +; diff --git a/src/apps/resedit/PreviewColumn.cpp b/src/apps/resedit/PreviewColumn.cpp new file mode 100644 index 0000000000..b0f4ccb8b6 --- /dev/null +++ b/src/apps/resedit/PreviewColumn.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#include "PreviewColumn.h" +#include + +PreviewColumn::PreviewColumn(const char *title, float width, + float minWidth, float maxWidth) + : BTitledColumn(title, width, minWidth, maxWidth) +{ +} + +void +PreviewColumn::DrawField(BField* field, BRect rect, BView* parent) +{ + BIntegerField *intField = dynamic_cast(field); + BStringField *strField = dynamic_cast(field); + BBitmapField *bmpField = dynamic_cast(field); + + if (intField) { + float width = rect.Width() - 16; + BString string; + string << intField->Value(); + parent->TruncateString(&string, B_TRUNCATE_MIDDLE, width + 2); + DrawString(string.String(), parent, rect); + } else if (strField) { + float width = rect.Width() - 16; + if (width != strField->Width()) { + BString out_string(strField->String()); + + parent->TruncateString(&out_string, B_TRUNCATE_END, width + 2); + strField->SetClippedString(out_string.String()); + strField->SetWidth(width); + } + DrawString(strField->ClippedString(), parent, rect); + } else if (bmpField) { + const BBitmap *bitmap = bmpField->Bitmap(); + + if (bitmap != NULL) { + // Scale the bitmap down to completely fit within the field's height + BRect drawrect(bitmap->Bounds().OffsetToCopy(rect.LeftTop())); + if (drawrect.Height() > rect.Height()) { + drawrect = rect; + drawrect.right = drawrect.left + + (bitmap->Bounds().Width() * + (rect.Height() / bitmap->Bounds().Height())); + } + + parent->SetDrawingMode(B_OP_ALPHA); + parent->DrawBitmap(bitmap, bitmap->Bounds(), drawrect); + parent->SetDrawingMode(B_OP_COPY); + + BString out; + out << bitmap->Bounds().IntegerWidth() << " x " + << bitmap->Bounds().IntegerHeight() << " x " + << (int32(bitmap->BytesPerRow() / bitmap->Bounds().Width()) * 8); + + BRect stringrect = rect; + stringrect.right -= 10; + stringrect.left = stringrect.right - parent->StringWidth(out.String()); + if (stringrect.left < drawrect.right + 5) + stringrect.left = drawrect.right + 5; + + parent->TruncateString(&out, B_TRUNCATE_END, stringrect.Width()); + DrawString(out.String(), parent, stringrect); + } + } +} diff --git a/src/apps/resedit/PreviewColumn.h b/src/apps/resedit/PreviewColumn.h new file mode 100644 index 0000000000..6053f39d26 --- /dev/null +++ b/src/apps/resedit/PreviewColumn.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#ifndef PREVIEW_COLUMN_H +#define PREVIEW_COLUMN_H + +#include + +class PreviewColumn : public BTitledColumn +{ +public: + PreviewColumn(const char *title, float width, + float minWidth, float maxWidth); + void DrawField(BField* field, BRect rect, BView* parent); +}; + +#endif diff --git a/src/apps/resedit/ResView.cpp b/src/apps/resedit/ResView.cpp new file mode 100644 index 0000000000..4990c47bb2 --- /dev/null +++ b/src/apps/resedit/ResView.cpp @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#include "ResView.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "App.h" +#include "ResourceData.h" +#include "ResWindow.h" +#include "PreviewColumn.h" + +static int32 sUntitled = 1; + +ResourceRoster gResRoster; + +enum { + M_NEW_FILE = 'nwfl', + M_OPEN_FILE, + M_SAVE_FILE, + M_SAVE_FILE_AS, + M_QUIT +}; + +ResView::ResView(const BRect &frame, const char *name, const int32 &resize, + const int32 &flags, const entry_ref *ref) + : BView(frame, name, resize, flags), + fIsDirty(false) +{ + SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); + if (ref) { + fRef = new entry_ref; + *fRef = *ref; + fFileName = fRef->name; + } else { + fRef = NULL; + fFileName = "Untitled "; + fFileName << sUntitled; + sUntitled++; + } + + BMenu *menu = new BMenu("File"); + menu->AddItem(new BMenuItem("New…", new BMessage(M_NEW_FILE), 'N')); + menu->AddSeparatorItem(); + menu->AddItem(new BMenuItem("Open…", new BMessage(M_OPEN_FILE), 'O')); + menu->AddItem(new BMenuItem("Quit", new BMessage(M_QUIT), 'Q')); + + BRect r(Bounds()); + r.bottom = 16; + fBar = new BMenuBar(r, "bar"); + AddChild(fBar); + + fBar->AddItem(menu); + + r = Bounds(); + r.top = fBar->Frame().bottom + 4; + fListView = new BColumnListView(r, "gridview", B_FOLLOW_ALL, B_WILL_DRAW, + B_FANCY_BORDER); + AddChild(fListView); + + rgb_color white = { 255,255,255,255 }; + fListView->SetColor(B_COLOR_BACKGROUND, white); + fListView->SetColor(B_COLOR_SELECTION, ui_color(B_MENU_BACKGROUND_COLOR)); + + float width = be_plain_font->StringWidth("00000") + 20; + fListView->AddColumn(new BStringColumn("ID",width,width,100,B_TRUNCATE_END),0); + + fListView->AddColumn(new BStringColumn("Type",width,width,100,B_TRUNCATE_END),1); + fListView->AddColumn(new BStringColumn("Name",150,50,300,B_TRUNCATE_END),2); + fListView->AddColumn(new PreviewColumn("Data",150,50,300),3); + + width = be_plain_font->StringWidth("1000 bytes") + 20; + fListView->AddColumn(new BSizeColumn("Size",width,10,100),4); + + if (ref) + OpenFile(*ref); +} + + +ResView::~ResView(void) +{ + delete fRef; +} + + +void +ResView::AttachedToWindow(void) +{ + for(int32 i = 0; i < fBar->CountItems(); i++) + fBar->SubmenuAt(i)->SetTargetForItems(this); +} + + +void +ResView::MessageReceived(BMessage *msg) +{ + switch (msg->what) { + case M_NEW_FILE: { + BRect r(100,100,400,400); + if (Window()) + r = Window()->Frame().OffsetByCopy(10,10); + ResWindow *win = new ResWindow(r); + win->Show(); + break; + } + case M_OPEN_FILE: { + be_app->PostMessage(M_SHOW_OPEN_PANEL); + break; + } + case M_QUIT: { + be_app->PostMessage(B_QUIT_REQUESTED); + break; + } + default: + BView::MessageReceived(msg); + } +} + + +void +ResView::OpenFile(const entry_ref &ref) +{ + // Add all the 133t resources and attributes of the file + + BFile file(&ref, B_READ_ONLY); + if (fResources.SetTo(&file) != B_OK) + return; + file.Unset(); + + fResources.PreloadResourceType(); + + int32 index = 0; + ResourceData data; + BRow *row; + while (data.SetFromResource(index, fResources)) { + row = new BRow(); + row->SetField(new BStringField(data.GetIDString()),0); + row->SetField(new BStringField(data.GetTypeString()),1); + row->SetField(new BStringField(data.GetName()),2); + BField *field = gResRoster.MakeFieldForType(data.GetType(), + data.GetData(), + data.GetLength()); + if (field) + row->SetField(field,3); + row->SetField(new BSizeField(data.GetLength()),4); + fListView->AddRow(row); + index++; + } + +//debugger(""); + BNode node; + if (node.SetTo(&ref) == B_OK) { + char attrName[B_ATTR_NAME_LENGTH]; + node.RewindAttrs(); + while (node.GetNextAttrName(attrName) == B_OK) { + if (data.SetFromAttribute(attrName, node)) { + row = new BRow(); + row->SetField(new BStringField(data.GetIDString()),0); + row->SetField(new BStringField(data.GetTypeString()),1); + row->SetField(new BStringField(data.GetName()),2); + BField *field = gResRoster.MakeFieldForType(data.GetType(), + data.GetData(), + data.GetLength()); + if (field) + row->SetField(field,3); + row->SetField(new BSizeField(data.GetLength()),4); + fListView->AddRow(row); + } + } + } +} + + +const void * +ResView::LoadResource(const type_code &type, const int32 &id, size_t *out_size) +{ + return fResources.LoadResource(type, id, out_size); +} + + +const void * +ResView::LoadResource(const type_code &type, const char *name, size_t *out_size) +{ + return fResources.LoadResource(type, name, out_size); +} + + +status_t +ResView::AddResource(const type_code &type, const int32 &id, const void *data, + const size_t &data_size, const char *name) +{ + return fResources.AddResource(type, id, data, data_size, name); +} + + +bool +ResView::HasResource(const type_code &type, const int32 &id) +{ + return fResources.HasResource(type, id); +} + + +bool +ResView::HasResource(const type_code &type, const char *name) +{ + return fResources.HasResource(type, name); +} + diff --git a/src/apps/resedit/ResView.h b/src/apps/resedit/ResView.h new file mode 100644 index 0000000000..05cff52d0c --- /dev/null +++ b/src/apps/resedit/ResView.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#ifndef RESVIEW_H +#define RESVIEW_H + +#include +#include +#include +#include +#include +#include +#include "ResourceRoster.h" + +class ResView : public BView +{ +public: + ResView(const BRect &frame, const char *name, + const int32 &resize, const int32 &flags, + const entry_ref *ref = NULL); + ~ResView(void); + void AttachedToWindow(void); + void MessageReceived(BMessage *msg); + + const char * Filename(void) const { return fFileName.String(); } + bool IsDirty(void) const { return fIsDirty; } + + void OpenFile(const entry_ref &ref); + + // BResources mirror API + const void * LoadResource(const type_code &type, const int32 &id, + size_t *out_size); + const void * LoadResource(const type_code &type, const char *name, + size_t *out_size); + status_t AddResource(const type_code &type, const int32 &id, + const void *data, const size_t &data_size, + const char *name = NULL); + bool HasResource(const type_code &type, const int32 &id); + bool HasResource(const type_code &type, const char *name); + +private: + BColumnListView *fListView; + entry_ref *fRef; + BString fFileName; + BMenuBar *fBar; + bool fIsDirty; + + BResources fResources; +}; + +extern ResourceRoster gResRoster; + +#endif diff --git a/src/apps/resedit/ResWindow.cpp b/src/apps/resedit/ResWindow.cpp new file mode 100644 index 0000000000..791e577f8a --- /dev/null +++ b/src/apps/resedit/ResWindow.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#include "ResWindow.h" +#include "ResView.h" +#include "App.h" + +ResWindow::ResWindow(const BRect &rect, const entry_ref *ref) + : BWindow(rect,"", B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS) +{ + be_app->PostMessage(M_REGISTER_WINDOW); + + ResView *child = new ResView(Bounds(), "resview", B_FOLLOW_ALL, B_WILL_DRAW, ref); + AddChild(child); + + SetTitle(child->Filename()); +} + + +ResWindow::~ResWindow(void) +{ +} + + +bool +ResWindow::QuitRequested(void) +{ + be_app->PostMessage(M_UNREGISTER_WINDOW); + return true; +} + + diff --git a/src/apps/resedit/ResWindow.h b/src/apps/resedit/ResWindow.h new file mode 100644 index 0000000000..5a7fea12d4 --- /dev/null +++ b/src/apps/resedit/ResWindow.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#ifndef RESWIN_H +#define RESWIN_H + +#include + +class ResWindow : public BWindow +{ +public: + ResWindow(const BRect &rect, + const entry_ref *ref=NULL); + ~ResWindow(void); + bool QuitRequested(void); +}; + +#endif diff --git a/src/apps/resedit/ResourceData.cpp b/src/apps/resedit/ResourceData.cpp new file mode 100644 index 0000000000..ef1a2244b5 --- /dev/null +++ b/src/apps/resedit/ResourceData.cpp @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#include "ResourceData.h" +#include + +ResourceData::ResourceData(void) + : fType(0), + fTypeString("Invalid"), + fID(-1), + fIDString("Invalid"), + fName(""), + fData(NULL), + fLength(0), + fFree(false) +{ +} + + +ResourceData::ResourceData(const type_code &code, const int32 &id, + const char *name, char *data, + const size_t &length) + : fType(code), + fID(id), + fName(name), + fData(data), + fLength(length), + fFree(false) +{ + fIDString = ""; + fIDString << fID; + MakeTypeString(); +} + + +ResourceData::ResourceData(const ResourceData &data) +{ + *this = data; +} + + +ResourceData::~ResourceData(void) +{ + if (fFree) + free(fData); +} + + +ResourceData & +ResourceData::operator=(const ResourceData &data) +{ + fType = data.fType; + fTypeString = data.fTypeString; + fID = data.fID; + fIDString = data.fIDString; + fName = data.fName; + fLength = data.fLength; + fFree = data.fFree; + + // This is an attribute, so we need to allocate and free the data + if (fFree) { + fData = data.fData; + } + return *this; +} + +bool +ResourceData::SetFromResource(const int32 &index, BResources &res) +{ + char *name; + if (!res.GetResourceInfo(index, (type_code*)&fType, &fID, + (const char **)&name, &fLength)) { + *this = ResourceData(); + return false; + } + fName = name; + MakeTypeString(); + fIDString = ""; + fIDString << fID; + fFree = false; + fData = (char *)res.LoadResource(fType,fID,&fLength); + + return true; +} + + +bool +ResourceData::SetFromAttribute(const char *name, BNode &node) +{ + attr_info info; + if (node.GetAttrInfo(name, &info) != B_OK) { + *this = ResourceData(); + return false; + } + + fType = info.type; + fID = -1; + fIDString = "(attr)"; + fName = name; + fLength = info.size; + fFree = true; + + MakeTypeString(); + + fData = (char *)malloc(fLength); + if (fData) { + ssize_t size = node.ReadAttr(name, info.type, 0, (void*)fData, fLength); + if (size >= 0) { + fLength = (size_t) size; + return true; + } + } + + *this = ResourceData(); + return false; +} + + +void +ResourceData::SetTo(const type_code &code, const int32 &id, + const char *name, char *data, const size_t &length) +{ + fType = code; + fID = id; + fName = name; + fData = data; + fLength = length; + + MakeTypeString(); +} + + +void +ResourceData::MakeTypeString(void) +{ + char typestring[7]; + char *typeptr = (char *) &fType; + typestring[0] = '\''; + typestring[1] = typeptr[3]; + typestring[2] = typeptr[2]; + typestring[3] = typeptr[1]; + typestring[4] = typeptr[0]; + typestring[5] = '\''; + typestring[6] = '\0'; + fTypeString = typestring; +} + diff --git a/src/apps/resedit/ResourceData.h b/src/apps/resedit/ResourceData.h new file mode 100644 index 0000000000..f280872d35 --- /dev/null +++ b/src/apps/resedit/ResourceData.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#ifndef RESOURCE_DATA_H +#define RESOURCE_DATA_H + +#include +#include +#include +#include + +class ResourceData +{ +public: + ResourceData(void); + ResourceData(const type_code &code, const int32 &id, + const char *name, char *data, + const size_t &length); + ResourceData(const ResourceData &data); + ~ResourceData(void); + void SetTo(const type_code &code, const int32 &id, + const char *name, char *data, const size_t &length); + ResourceData & operator=(const ResourceData &data); + + bool SetFromResource(const int32 &index, BResources &res); + bool SetFromAttribute(const char *name, BNode &node); + + type_code GetType(void) const { return fType; } + const char * GetTypeString(void) const { return fTypeString.String(); } + int32 GetID(void) const { return fID; } + const char * GetIDString(void) const { return fIDString.String(); } + const char * GetName(void) const { return fName.String(); } + char * GetData(void) { return fData; } + size_t GetLength(void) const { return fLength; } + + bool IsAttribute(void) const { return fFree; } + +private: + void MakeTypeString(void); + + int32 fType; + BString fTypeString; + int32 fID; + BString fIDString; + BString fName; + char *fData; + size_t fLength; + bool fFree; +}; + +#endif diff --git a/src/apps/resedit/ResourceRoster.cpp b/src/apps/resedit/ResourceRoster.cpp new file mode 100644 index 0000000000..9225d80803 --- /dev/null +++ b/src/apps/resedit/ResourceRoster.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#include "ResourceRoster.h" +#include +#include +#include +#include +#include +#include +#include +#include + +// For the MakeFieldForType temp code +#include +#include +#include +#include +#include + +class EditorInfo +{ +public: + EditorInfo(const image_id &id, const char *name, + create_editor *allocator); + ~EditorInfo(void); + + status_t ID(void) const { return fID; } + const char * Name(void) const { return fName.String(); } + Editor * Instantiate(void); + +private: + image_id fID; + BString fName; + create_editor * fAllocator; +}; + +EditorInfo::EditorInfo(const image_id &id, const char *name, + create_editor *allocator) + : fID(id), + fName(name), + fAllocator(allocator) +{ +} + + +EditorInfo::~EditorInfo(void) +{ +} + + +Editor * +EditorInfo::Instantiate(void) +{ + return fAllocator(); +} + + +ResourceRoster::ResourceRoster(void) +{ +} + + +ResourceRoster::~ResourceRoster(void) +{ +} + + +BField * +ResourceRoster::MakeFieldForType(const int32 &type, const char *data, + const size_t &length) +{ + // temporary code until editors are done + switch (type) { + case B_MIME_STRING_TYPE: + return new BStringField(data); + case B_GIF_FORMAT: + case B_PPM_FORMAT: + case B_TGA_FORMAT: + case B_BMP_FORMAT: + case B_TIFF_FORMAT: + case B_PNG_FORMAT: + case B_JPEG_FORMAT: { + BMemoryIO memio(data,length); + BBitmap *bitmap = BTranslationUtils::GetBitmap(&memio); + if (bitmap) { + // XXX WARNING! WARNING! Known memory leak + // This is temp code, so it's OK for the moment + BBitmapField *field = new BBitmapField(bitmap); + return field; + } + break; + } + default: + return NULL; + } + + return NULL; +} + + +void +ResourceRoster::LoadEditors(void) +{ + app_info info; + be_app->GetAppInfo(&info); + + BDirectory dir; + BEntry entry(&info.ref); + entry.GetParent(&dir); + entry.SetTo(&dir, "addons"); + dir.SetTo(&entry); + + entry_ref ref; + dir.Rewind(); + while (dir.GetNextRef(&ref) == B_OK) { + BPath path(&ref); + + image_id addon = load_add_on(path.Path()); + if (addon < 0) + continue; + + char *temp; + if (get_image_symbol(addon,"description",B_SYMBOL_TYPE_DATA,(void **)(&temp)) != B_OK) { + unload_add_on(addon); + continue; + } + + create_editor *createFunc; + if (get_image_symbol(addon,"instantiate_editor",B_SYMBOL_TYPE_TEXT,(void **)(&createFunc)) != B_OK) { + delete temp; + unload_add_on(addon); + continue; + } + + if (createFunc && temp) + fList.AddItem(new EditorInfo(addon, temp, createFunc)); + + delete temp; + } +} + diff --git a/src/apps/resedit/ResourceRoster.h b/src/apps/resedit/ResourceRoster.h new file mode 100644 index 0000000000..15ecf2da67 --- /dev/null +++ b/src/apps/resedit/ResourceRoster.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2005-2006, Haiku, Inc. + * Distributed under the terms of the MIT license. + * + * Author: + * DarkWyrm + */ +#ifndef RESROSTER_H +#define RESROSTER_H + +#include +#include +#include + +class Editor; + +class ResourceRoster +{ +public: + ResourceRoster(void); + ~ResourceRoster(void); + BField * MakeFieldForType(const int32 &type, const char *data, + const size_t &length); + +private: + void LoadEditors(void); + + BList fList; +}; + +typedef Editor* create_editor(void); + +#endif