Adding and deleting resources now implemented

Added drag and drop support to add resources to the current project
Updated e-mail address and copyright information
Converted window registration to use a static variable to simplify and correct window closing code


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36499 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
DarkWyrm
2010-04-26 23:38:41 +00:00
parent 4c688b93e3
commit e234c03470
22 changed files with 284 additions and 122 deletions
+7 -34
View File
@@ -1,9 +1,9 @@
/*
* Copyright (c) 2005-2006, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#include "App.h"
#include "ResWindow.h"
@@ -19,25 +19,25 @@ main(void)
App::App(void)
: BApplication("application/x-vnd.Haiku-ResEdit"),
fWindowCount(0)
: BApplication("application/x-vnd.Haiku-ResEdit")
{
fOpenPanel = new BFilePanel();
fSavePanel = new BFilePanel(B_SAVE_PANEL);
}
App::~App(void)
{
delete fOpenPanel;
delete fSavePanel;
}
void
App::ReadyToRun(void)
{
if (fWindowCount < 1)
// CountWindows() needs to be used instead of fWindowCount because the registration
// message isn't processed in time. One of the windows belong to the BFilePanels and is
// counted in CountWindows().
if (CountWindows() < 2)
new ResWindow(BRect(50, 100, 600, 400));
}
@@ -46,16 +46,6 @@ 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())
@@ -90,20 +80,3 @@ App::RefsReceived(BMessage *msg)
while (msg->FindRef("refs", i++, &ref) == B_OK)
new ResWindow(BRect(50, 100, 600, 400), &ref);
}
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;
}
+3 -7
View File
@@ -1,9 +1,9 @@
/*
* Copyright (c) 2005-2006, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#ifndef APP_H
#define APP_H
@@ -12,8 +12,6 @@
#include <FilePanel.h>
enum {
M_REGISTER_WINDOW = 'regw',
M_UNREGISTER_WINDOW,
M_SHOW_OPEN_PANEL,
M_SHOW_SAVE_PANEL
};
@@ -27,11 +25,9 @@ public:
void ArgvReceived(int32 argc, char** argv);
void RefsReceived(BMessage *msg);
void ReadyToRun(void);
bool QuitRequested(void);
private:
uint32 fWindowCount;
BFilePanel *fOpenPanel, *fSavePanel;
BFilePanel *fOpenPanel;
};
#endif
+2 -2
View File
@@ -1,9 +1,9 @@
/*
* Copyright (c) 2005-2006, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#ifndef EDITOR_H
#define EDITOR_H
+9
View File
@@ -1,3 +1,10 @@
/*
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <[email protected]>
*/
#include "InternalEditors.h"
#include "BitmapView.h"
#include "ResourceData.h"
@@ -80,6 +87,8 @@ ImageEditor::ImageEditor(const BRect &frame, ResourceData *data, BHandler *owner
ResizeTo(MAX(fImageView->Frame().right, fNameBox->Frame().right) + 20,
fImageView->Frame().bottom + fOK->Frame().Height() + 20);
SetSizeLimits(Bounds().right,30000,Bounds().bottom,30000);
}
+7
View File
@@ -1,3 +1,10 @@
/*
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <[email protected]>
*/
#include "InlineEditor.h"
#include <MessageFilter.h>
#include <Handler.h>
+7
View File
@@ -1,3 +1,10 @@
/*
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <[email protected]>
*/
#ifndef INLINE_EDITOR
#define INLINE_EDITOR
+7
View File
@@ -1,3 +1,10 @@
/*
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <[email protected]>
*/
#ifndef INTERNALEDITORS_H
#define INTERNALEDITORS_H
+1
View File
@@ -22,6 +22,7 @@ Application ResEdit :
NumberEditors.cpp
PreviewColumn.cpp
ResFields.cpp
ResListView.cpp
ResourceData.cpp
ResourceRoster.cpp
ResView.cpp
+8 -1
View File
@@ -1,3 +1,10 @@
/*
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <[email protected]>
*/
#include "InternalEditors.h"
#include "ResourceData.h"
#include <Messenger.h>
@@ -138,7 +145,7 @@ StringEditView::GetPreferredWidth(void) const
be_plain_font->StringWidth("(attr) ") + 15;
float namewidth = be_plain_font->StringWidth("Name: ") +
be_plain_font->StringWidth(fNameBox->Text()) + 15;
return idwidth + namewidth + 30;
return idwidth + namewidth + 100;
}
+7
View File
@@ -1,3 +1,10 @@
/*
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@gmail.com>
*/
#include "InternalEditors.h"
#include "ResourceData.h"
#include <Messenger.h>
+2 -2
View File
@@ -1,9 +1,9 @@
/*
* Copyright (c) 2005-2006, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#include "PreviewColumn.h"
#include "ResFields.h"
+2 -2
View File
@@ -1,9 +1,9 @@
/*
* Copyright (c) 2005-2006, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#ifndef PREVIEW_COLUMN_H
#define PREVIEW_COLUMN_H
+7
View File
@@ -1,3 +1,10 @@
/*
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@gmail.com>
*/
#include "ResFields.h"
#include "ResourceData.h"
#include <DataIO.h>
+7
View File
@@ -1,3 +1,10 @@
/*
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@gmail.com>
*/
#ifndef RESFIELDS_H
#define RESFIELDS_H
+149 -40
View File
@@ -1,23 +1,27 @@
/*
* Copyright (c) 2005-2006, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#include "ResView.h"
#include <Application.h>
#include <File.h>
#include <ScrollView.h>
#include <MenuBar.h>
#include <malloc.h>
#include <Menu.h>
#include <MenuItem.h>
#include <stdio.h>
#include <ColumnTypes.h>
#include <TranslatorRoster.h>
#include <TypeConstants.h>
#include "ColumnTypes.h"
#include "App.h"
#include "ResourceData.h"
#include "ResFields.h"
#include "ResListView.h"
#include "ResWindow.h"
#include "PreviewColumn.h"
#include "Editor.h"
@@ -32,6 +36,8 @@ enum {
M_SAVE_FILE,
M_SAVE_FILE_AS,
M_QUIT,
M_SELECT_FILE,
M_DELETE_RESOURCE,
M_EDIT_RESOURCE
};
@@ -52,23 +58,16 @@ ResView::ResView(const BRect &frame, const char *name, const int32 &resize,
sUntitled++;
}
BMenu *menu = new BMenu("File");
menu->AddItem(new BMenuItem("New" B_UTF8_ELLIPSIS, new BMessage(M_NEW_FILE), 'N'));
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem("Open" B_UTF8_ELLIPSIS, 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);
BuildMenus(fBar);
r = Bounds();
r.top = fBar->Frame().bottom + 4;
fListView = new BColumnListView(r, "gridview", B_FOLLOW_ALL, B_WILL_DRAW,
B_FANCY_BORDER);
fListView = new ResListView(r, "gridview", B_FOLLOW_ALL, B_WILL_DRAW, B_FANCY_BORDER);
AddChild(fListView);
rgb_color white = { 255,255,255,255 };
@@ -81,11 +80,14 @@ ResView::ResView(const BRect &frame, const char *name, const int32 &resize,
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);
// Editing is disabled for now
fListView->SetInvocationMessage(new BMessage(M_EDIT_RESOURCE));
width = be_plain_font->StringWidth("1000 bytes") + 20;
fListView->AddColumn(new BSizeColumn("Size",width,10,100),4);
fFilePanel = new BFilePanel(B_OPEN_PANEL);
if (ref)
OpenFile(*ref);
}
@@ -95,6 +97,7 @@ ResView::~ResView(void)
{
EmptyDataList();
delete fRef;
delete fFilePanel;
}
@@ -104,17 +107,13 @@ ResView::AttachedToWindow(void)
for(int32 i = 0; i < fBar->CountItems(); i++)
fBar->SubmenuAt(i)->SetTargetForItems(this);
fListView->SetTarget(this);
fFilePanel->SetTarget(BMessenger(this));
}
void
ResView::MessageReceived(BMessage *msg)
{
if (msg->WasDropped()) {
be_app->PostMessage(msg);
return;
}
switch (msg->what) {
case M_NEW_FILE: {
BRect r(100,100,400,400);
@@ -132,6 +131,21 @@ ResView::MessageReceived(BMessage *msg)
be_app->PostMessage(B_QUIT_REQUESTED);
break;
}
case B_REFS_RECEIVED: {
int32 i = 0;
entry_ref ref;
while (msg->FindRef("refs",i++,&ref) == B_OK)
AddResource(ref);
break;
}
case M_SELECT_FILE: {
fFilePanel->Show();
break;
}
case M_DELETE_RESOURCE: {
DeleteSelectedResources();
break;
}
case M_EDIT_RESOURCE: {
BRow *row = fListView->CurrentSelection();
TypeCodeField *field = (TypeCodeField*)row->GetField(1);
@@ -164,7 +178,6 @@ void
ResView::OpenFile(const entry_ref &ref)
{
// Add all the 133t resources and attributes of the file
BFile file(&ref, B_READ_ONLY);
BResources resources;
if (resources.SetTo(&file) != B_OK)
@@ -174,19 +187,10 @@ ResView::OpenFile(const entry_ref &ref)
resources.PreloadResourceType();
int32 index = 0;
BRow *row;
ResDataRow *row;
ResourceData *resData = new ResourceData();
while (resData->SetFromResource(index, resources)) {
row = new BRow();
row->SetField(new BStringField(resData->GetIDString()),0);
row->SetField(new TypeCodeField(resData->GetType(),resData),1);
row->SetField(new BStringField(resData->GetName()),2);
BField *field = gResRoster.MakeFieldForType(resData->GetType(),
resData->GetData(),
resData->GetLength());
if (field)
row->SetField(field,3);
row->SetField(new BSizeField(resData->GetLength()),4);
row = new ResDataRow(resData);
fListView->AddRow(row);
fDataList.AddItem(resData);
resData = new ResourceData();
@@ -201,16 +205,7 @@ ResView::OpenFile(const entry_ref &ref)
resData = new ResourceData();
while (node.GetNextAttrName(attrName) == B_OK) {
if (resData->SetFromAttribute(attrName, node)) {
row = new BRow();
row->SetField(new BStringField(resData->GetIDString()),0);
row->SetField(new TypeCodeField(resData->GetType(),resData),1);
row->SetField(new BStringField(resData->GetName()),2);
BField *field = gResRoster.MakeFieldForType(resData->GetType(),
resData->GetData(),
resData->GetLength());
if (field)
row->SetField(field,3);
row->SetField(new BSizeField(resData->GetLength()),4);
row = new ResDataRow(resData);
fListView->AddRow(row);
fDataList.AddItem(resData);
resData = new ResourceData();
@@ -221,6 +216,25 @@ ResView::OpenFile(const entry_ref &ref)
}
void
ResView::BuildMenus(BMenuBar *menuBar)
{
BMenu *menu = new BMenu("File");
menu->AddItem(new BMenuItem("New" B_UTF8_ELLIPSIS, new BMessage(M_NEW_FILE), 'N'));
menu->AddSeparatorItem();
menu->AddItem(new BMenuItem("Open" B_UTF8_ELLIPSIS, new BMessage(M_OPEN_FILE), 'O'));
menuBar->AddItem(menu);
menu = new BMenu("Resource");
menu->AddItem(new BMenuItem("Add" B_UTF8_ELLIPSIS, new BMessage(M_SELECT_FILE), 'F'));
menu->AddItem(new BMenuItem("Delete", new BMessage(M_DELETE_RESOURCE), 'D'));
menuBar->AddItem(menu);
}
void
ResView::EmptyDataList(void)
{
@@ -251,3 +265,98 @@ ResView::UpdateRow(BRow *row)
BSizeField *sizeField = (BSizeField*)row->GetField(4);
sizeField->SetSize(resData->GetLength());
}
void
ResView::AddResource(const entry_ref &ref)
{
BFile file(&ref,B_READ_ONLY);
if (file.InitCheck() != B_OK)
return;
BString mime;
file.ReadAttrString("BEOS:TYPE",&mime);
if (mime == "application/x-be-resource") {
BMessage msg(B_REFS_RECEIVED);
msg.AddRef("refs",&ref);
be_app->PostMessage(&msg);
return;
}
type_code fileType = 0;
BTranslatorRoster *roster = BTranslatorRoster::Default();
translator_info info;
if (roster->Identify(&file,NULL,&info,0,mime.String()) == B_OK)
fileType = info.type;
else
fileType = B_RAW_TYPE;
int32 lastID = -1;
for (int32 i = 0; i < fDataList.CountItems(); i++) {
ResourceData *resData = (ResourceData*)fDataList.ItemAt(i);
if (resData->GetType() == fileType && resData->GetID() > lastID)
lastID = resData->GetID();
}
off_t fileSize;
file.GetSize(&fileSize);
if (fileSize < 1)
return;
char *fileData = (char *)malloc(fileSize);
file.Read(fileData,fileSize);
ResourceData *resData = new ResourceData(fileType,lastID + 1, ref.name,
fileData,fileSize);
fDataList.AddItem(resData);
ResDataRow *row = new ResDataRow(resData);
fListView->AddRow(row);
fIsDirty = true;
}
void
ResView::DeleteSelectedResources(void)
{
ResDataRow *selection = (ResDataRow*)fListView->CurrentSelection();
if (selection)
fIsDirty = true;
while (selection) {
ResourceData *data = selection->GetData();
fListView->RemoveRow(selection);
fDataList.RemoveItem(data);
delete data;
selection = (ResDataRow*)fListView->CurrentSelection();
}
}
ResDataRow::ResDataRow(ResourceData *data)
: fResData(data)
{
if (data) {
SetField(new BStringField(fResData->GetIDString()),0);
SetField(new TypeCodeField(fResData->GetType(),fResData),1);
SetField(new BStringField(fResData->GetName()),2);
BField *field = gResRoster.MakeFieldForType(fResData->GetType(),
fResData->GetData(),
fResData->GetLength());
if (field)
SetField(field,3);
SetField(new BSizeField(fResData->GetLength()),4);
}
}
ResourceData *
ResDataRow::GetData(void) const
{
return fResData;
}
+26 -7
View File
@@ -1,24 +1,27 @@
/*
* Copyright (c) 2005-2006, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#ifndef RESVIEW_H
#define RESVIEW_H
#include <View.h>
#include <ColumnListView.h>
#include <Entry.h>
#include <String.h>
#include <ListItem.h>
#include <FilePanel.h>
#include <List.h>
#include <ListItem.h>
#include <MenuBar.h>
#include <Resources.h>
#include <String.h>
#include <View.h>
#include "ResourceRoster.h"
class BMenuBar;
class ResListView;
class ResView : public BView {
public:
@@ -35,15 +38,31 @@ public:
void OpenFile(const entry_ref &ref);
private:
void BuildMenus(BMenuBar *menuBar);
void EmptyDataList(void);
void UpdateRow(BRow *row);
void HandleDrop(BMessage *msg);
void AddResource(const entry_ref &ref);
void DeleteSelectedResources(void);
BColumnListView *fListView;
ResListView *fListView;
entry_ref *fRef;
BString fFileName;
BMenuBar *fBar;
bool fIsDirty;
BList fDataList;
BFilePanel *fFilePanel;
};
class ResDataRow : public BRow
{
public:
ResDataRow(ResourceData *data);
ResourceData * GetData(void) const;
private:
ResourceData *fResData;
};
extern ResourceRoster gResRoster;
+11 -5
View File
@@ -1,21 +1,23 @@
/*
* Copyright (c) 2005-2006, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#include "ResWindow.h"
#include "ResView.h"
#include "App.h"
static int32 sWindowCount = 0;
ResWindow::ResWindow(const BRect &rect, const entry_ref *ref)
: BWindow(rect,"", B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS)
{
be_app->PostMessage(M_REGISTER_WINDOW);
atomic_add(&sWindowCount,1);
ResView *child = new ResView(Bounds(), "resview", B_FOLLOW_ALL,
B_WILL_DRAW, ref);
B_WILL_DRAW, ref);
AddChild(child);
SetTitle(child->Filename());
@@ -32,7 +34,11 @@ ResWindow::~ResWindow(void)
bool
ResWindow::QuitRequested(void)
{
be_app->PostMessage(M_UNREGISTER_WINDOW);
atomic_add(&sWindowCount,-1);
if (sWindowCount == 0)
be_app->PostMessage(B_QUIT_REQUESTED);
// be_app->PostMessage(M_UNREGISTER_WINDOW);
return true;
}
+2 -2
View File
@@ -1,9 +1,9 @@
/*
* Copyright (c) 2005-2008, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#ifndef RESWIN_H
#define RESWIN_H
+2 -2
View File
@@ -1,9 +1,9 @@
/*
* Copyright (c) 2005-2006, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#include "ResourceData.h"
#include "ResFields.h"
+2 -2
View File
@@ -1,9 +1,9 @@
/*
* Copyright (c) 2005-2006, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#ifndef RESOURCE_DATA_H
#define RESOURCE_DATA_H
+3 -3
View File
@@ -1,9 +1,9 @@
/*
* Copyright (c) 2005-2006, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#include "ResourceRoster.h"
#include <Roster.h>
@@ -152,7 +152,7 @@ ResourceRoster::SpawnEditor(ResourceData *data, BHandler *handler)
// temporary code until editors are done
switch (data->GetType()) {
case B_MIME_STRING_TYPE: {
StringEditor *strEd = new StringEditor(BRect(100,100,300,200),
StringEditor *strEd = new StringEditor(BRect(100,100,400,200),
data, handler);
strEd->Show();
break;
+13 -13
View File
@@ -1,16 +1,16 @@
/*
* Copyright (c) 2005-2006, Haiku, Inc.
* Copyright (c) 2005-2010, Haiku, Inc.
* Distributed under the terms of the MIT license.
*
* Author:
* DarkWyrm <darkwyrm@earthlink.net>
* DarkWyrm <darkwyrm@gmail.com>
*/
#ifndef RESROSTER_H
#define RESROSTER_H
#include <List.h>
#include <ColumnTypes.h>
#include <ColumnListView.h>
#include "ColumnTypes.h"
#include "ColumnListView.h"
class Editor;
class ResourceData;
@@ -18,16 +18,16 @@ class ResourceData;
class ResourceRoster
{
public:
ResourceRoster(void);
~ResourceRoster(void);
BField * MakeFieldForType(const int32 &type, const char *data,
const size_t &length);
void SpawnEditor(ResourceData *data, BHandler *handler);
private:
void LoadEditors(void);
ResourceRoster(void);
~ResourceRoster(void);
BField * MakeFieldForType(const int32 &type, const char *data,
const size_t &length);
void SpawnEditor(ResourceData *data, BHandler *handler);
BList fList;
private:
void LoadEditors(void);
BList fList;
};
typedef Editor* create_editor(void);