108cf72ceSDarkWyrm/*
2e234c034SDarkWyrm * Copyright (c) 2005-2010, Haiku, Inc.
308cf72ceSDarkWyrm * Distributed under the terms of the MIT license.
408cf72ceSDarkWyrm *
508cf72ceSDarkWyrm * Author:
6e234c034SDarkWyrm *		DarkWyrm <darkwyrm@gmail.com>
708cf72ceSDarkWyrm */
808cf72ceSDarkWyrm#include "ResView.h"
908cf72ceSDarkWyrm
1008cf72ceSDarkWyrm#include <Application.h>
11e234c034SDarkWyrm#include <File.h>
1208cf72ceSDarkWyrm#include <Menu.h>
1308cf72ceSDarkWyrm#include <MenuItem.h>
148b671a47SDarkWyrm#include <Path.h>
158b671a47SDarkWyrm#include <ScrollView.h>
16e234c034SDarkWyrm#include <TranslatorRoster.h>
17e234c034SDarkWyrm#include <TypeConstants.h>
1809a03cb8SDarkWyrm
1909a03cb8SDarkWyrm#include <stdio.h>
2009a03cb8SDarkWyrm#include <stdlib.h>
2108cf72ceSDarkWyrm
2208cf72ceSDarkWyrm#include "App.h"
2309a03cb8SDarkWyrm#include "ColumnTypes.h"
2408cf72ceSDarkWyrm#include "ResourceData.h"
25e61a091fSDarkWyrm#include "ResFields.h"
26e234c034SDarkWyrm#include "ResListView.h"
2708cf72ceSDarkWyrm#include "ResWindow.h"
2808cf72ceSDarkWyrm#include "PreviewColumn.h"
29e61a091fSDarkWyrm#include "Editor.h"
3008cf72ceSDarkWyrm
3108cf72ceSDarkWyrmstatic int32 sUntitled = 1;
3208cf72ceSDarkWyrm
3308cf72ceSDarkWyrmResourceRoster gResRoster;
3408cf72ceSDarkWyrm
3508cf72ceSDarkWyrmenum {
3608cf72ceSDarkWyrm	M_NEW_FILE = 'nwfl',
3708cf72ceSDarkWyrm	M_OPEN_FILE,
3808cf72ceSDarkWyrm	M_SAVE_FILE,
39e61a091fSDarkWyrm	M_QUIT,
40e234c034SDarkWyrm	M_SELECT_FILE,
41e234c034SDarkWyrm	M_DELETE_RESOURCE,
42e61a091fSDarkWyrm	M_EDIT_RESOURCE
4308cf72ceSDarkWyrm};
4408cf72ceSDarkWyrm
4508cf72ceSDarkWyrmResView::ResView(const BRect &frame, const char *name, const int32 &resize,
4608cf72ceSDarkWyrm				const int32 &flags, const entry_ref *ref)
4708cf72ceSDarkWyrm  :	BView(frame, name, resize, flags),
488b671a47SDarkWyrm  	fRef(NULL),
498b671a47SDarkWyrm	fSaveStatus(FILE_INIT),
508b671a47SDarkWyrm  	fOpenPanel(NULL),
518b671a47SDarkWyrm  	fSavePanel(NULL)
5208cf72ceSDarkWyrm{
53fa19dd44Slooncraz	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
5408cf72ceSDarkWyrm	if (ref) {
5508cf72ceSDarkWyrm		fRef = new entry_ref;
5608cf72ceSDarkWyrm		*fRef = *ref;
5708cf72ceSDarkWyrm		fFileName = fRef->name;
5808cf72ceSDarkWyrm	} else {
5908cf72ceSDarkWyrm		fFileName = "Untitled ";
6008cf72ceSDarkWyrm		fFileName << sUntitled;
6108cf72ceSDarkWyrm		sUntitled++;
6208cf72ceSDarkWyrm	}
6308cf72ceSDarkWyrm
6408cf72ceSDarkWyrm	BRect r(Bounds());
6508cf72ceSDarkWyrm	r.bottom = 16;
6608cf72ceSDarkWyrm	fBar = new BMenuBar(r, "bar");
6708cf72ceSDarkWyrm	AddChild(fBar);
6808cf72ceSDarkWyrm
69e234c034SDarkWyrm	BuildMenus(fBar);
7008cf72ceSDarkWyrm
7108cf72ceSDarkWyrm	r = Bounds();
7208cf72ceSDarkWyrm	r.top = fBar->Frame().bottom + 4;
73e234c034SDarkWyrm	fListView = new ResListView(r, "gridview", B_FOLLOW_ALL, B_WILL_DRAW, B_FANCY_BORDER);
7408cf72ceSDarkWyrm	AddChild(fListView);
7508cf72ceSDarkWyrm
7609a03cb8SDarkWyrm	rgb_color white = { 255, 255, 255, 255 };
7708cf72ceSDarkWyrm	fListView->SetColor(B_COLOR_BACKGROUND, white);
7808cf72ceSDarkWyrm	fListView->SetColor(B_COLOR_SELECTION, ui_color(B_MENU_BACKGROUND_COLOR));
7908cf72ceSDarkWyrm
8008cf72ceSDarkWyrm	float width = be_plain_font->StringWidth("00000") + 20;
8109a03cb8SDarkWyrm	fListView->AddColumn(new BStringColumn("ID", width, width, 100, B_TRUNCATE_END), 0);
8208cf72ceSDarkWyrm
8309a03cb8SDarkWyrm	fListView->AddColumn(new BStringColumn("Type", width, width, 100, B_TRUNCATE_END), 1);
8409a03cb8SDarkWyrm	fListView->AddColumn(new BStringColumn("Name", 150, 50, 300, B_TRUNCATE_END), 2);
8509a03cb8SDarkWyrm	fListView->AddColumn(new PreviewColumn("Data", 150, 50, 300), 3);
86e234c034SDarkWyrm
87e234c034SDarkWyrm	// Editing is disabled for now
88e61a091fSDarkWyrm	fListView->SetInvocationMessage(new BMessage(M_EDIT_RESOURCE));
8908cf72ceSDarkWyrm
9008cf72ceSDarkWyrm	width = be_plain_font->StringWidth("1000 bytes") + 20;
9109a03cb8SDarkWyrm	fListView->AddColumn(new BSizeColumn("Size", width, 10, 100), 4);
9208cf72ceSDarkWyrm
938b671a47SDarkWyrm	fOpenPanel = new BFilePanel(B_OPEN_PANEL);
9408cf72ceSDarkWyrm	if (ref)
9508cf72ceSDarkWyrm		OpenFile(*ref);
968b671a47SDarkWyrm
978b671a47SDarkWyrm	fSavePanel = new BFilePanel(B_SAVE_PANEL);
9808cf72ceSDarkWyrm}
9908cf72ceSDarkWyrm
10008cf72ceSDarkWyrm
10108cf72ceSDarkWyrmResView::~ResView(void)
10208cf72ceSDarkWyrm{
103e61a091fSDarkWyrm	EmptyDataList();
10408cf72ceSDarkWyrm	delete fRef;
1058b671a47SDarkWyrm	delete fOpenPanel;
1068b671a47SDarkWyrm	delete fSavePanel;
10708cf72ceSDarkWyrm}
10808cf72ceSDarkWyrm
10908cf72ceSDarkWyrm
11008cf72ceSDarkWyrmvoid
11108cf72ceSDarkWyrmResView::AttachedToWindow(void)
11208cf72ceSDarkWyrm{
1138b671a47SDarkWyrm	for (int32 i = 0; i < fBar->CountItems(); i++)
11408cf72ceSDarkWyrm		fBar->SubmenuAt(i)->SetTargetForItems(this);
115e61a091fSDarkWyrm	fListView->SetTarget(this);
1168b671a47SDarkWyrm
1178b671a47SDarkWyrm	BMessenger messenger(this);
1188b671a47SDarkWyrm	fOpenPanel->SetTarget(messenger);
1198b671a47SDarkWyrm	fSavePanel->SetTarget(messenger);
1208b671a47SDarkWyrm
1218b671a47SDarkWyrm	Window()->Lock();
1228b671a47SDarkWyrm	BString title("ResEdit: ");
1238b671a47SDarkWyrm	title << fFileName;
1248b671a47SDarkWyrm	Window()->SetTitle(title.String());
1258b671a47SDarkWyrm	Window()->Unlock();
12608cf72ceSDarkWyrm}
12708cf72ceSDarkWyrm
12808cf72ceSDarkWyrm
12908cf72ceSDarkWyrmvoid
13008cf72ceSDarkWyrmResView::MessageReceived(BMessage *msg)
13108cf72ceSDarkWyrm{
13208cf72ceSDarkWyrm	switch (msg->what) {
13308cf72ceSDarkWyrm		case M_NEW_FILE: {
13409a03cb8SDarkWyrm			BRect r(100, 100, 400, 400);
13508cf72ceSDarkWyrm			if (Window())
13609a03cb8SDarkWyrm				r = Window()->Frame().OffsetByCopy(10, 10);
13708cf72ceSDarkWyrm			ResWindow *win = new ResWindow(r);
13808cf72ceSDarkWyrm			win->Show();
13908cf72ceSDarkWyrm			break;
14008cf72ceSDarkWyrm		}
14108cf72ceSDarkWyrm		case M_OPEN_FILE: {
14208cf72ceSDarkWyrm			be_app->PostMessage(M_SHOW_OPEN_PANEL);
14308cf72ceSDarkWyrm			break;
14408cf72ceSDarkWyrm		}
1458b671a47SDarkWyrm		case B_CANCEL: {
1468b671a47SDarkWyrm			if (fSaveStatus == FILE_QUIT_AFTER_SAVE)
1478b671a47SDarkWyrm				SetSaveStatus(FILE_DIRTY);
1488b671a47SDarkWyrm			break;
1498b671a47SDarkWyrm		}
1508b671a47SDarkWyrm		case B_SAVE_REQUESTED: {
1518b671a47SDarkWyrm			entry_ref saveDir;
1528b671a47SDarkWyrm			BString name;
1538b671a47SDarkWyrm			if (msg->FindRef("directory",&saveDir) == B_OK &&
1548b671a47SDarkWyrm				msg->FindString("name",&name) == B_OK) {
1558b671a47SDarkWyrm				SetTo(saveDir,name);
1568b671a47SDarkWyrm				SaveFile();
1578b671a47SDarkWyrm			}
1588b671a47SDarkWyrm			break;
1598b671a47SDarkWyrm		}
1608b671a47SDarkWyrm		case M_SAVE_FILE: {
1618b671a47SDarkWyrm			if (!fRef)
1628b671a47SDarkWyrm				fSavePanel->Show();
1638b671a47SDarkWyrm			else
1648b671a47SDarkWyrm				SaveFile();
1658b671a47SDarkWyrm			break;
1668b671a47SDarkWyrm		}
1678b671a47SDarkWyrm		case M_SHOW_SAVE_PANEL: {
1688b671a47SDarkWyrm			fSavePanel->Show();
1698b671a47SDarkWyrm			break;
1708b671a47SDarkWyrm		}
17108cf72ceSDarkWyrm		case M_QUIT: {
17208cf72ceSDarkWyrm			be_app->PostMessage(B_QUIT_REQUESTED);
17308cf72ceSDarkWyrm			break;
17408cf72ceSDarkWyrm		}
175e234c034SDarkWyrm		case B_REFS_RECEIVED: {
176e234c034SDarkWyrm			int32 i = 0;
177e234c034SDarkWyrm			entry_ref ref;
17809a03cb8SDarkWyrm			while (msg->FindRef("refs", i++, &ref) == B_OK)
179e234c034SDarkWyrm				AddResource(ref);
180e234c034SDarkWyrm			break;
181e234c034SDarkWyrm		}
182e234c034SDarkWyrm		case M_SELECT_FILE: {
1838b671a47SDarkWyrm			fOpenPanel->Show();
184e234c034SDarkWyrm			break;
185e234c034SDarkWyrm		}
186e234c034SDarkWyrm		case M_DELETE_RESOURCE: {
187e234c034SDarkWyrm			DeleteSelectedResources();
188e234c034SDarkWyrm			break;
189e234c034SDarkWyrm		}
190e61a091fSDarkWyrm		case M_EDIT_RESOURCE: {
191e61a091fSDarkWyrm			BRow *row = fListView->CurrentSelection();
192e61a091fSDarkWyrm			TypeCodeField *field = (TypeCodeField*)row->GetField(1);
19309a03cb8SDarkWyrm			gResRoster.SpawnEditor(field->GetResourceData(), this);
194e61a091fSDarkWyrm			break;
195e61a091fSDarkWyrm		}
196e61a091fSDarkWyrm		case M_UPDATE_RESOURCE: {
197e61a091fSDarkWyrm			ResourceData *item;
19809a03cb8SDarkWyrm			if (msg->FindPointer("item", (void **)&item) != B_OK)
199e61a091fSDarkWyrm				break;
200e61a091fSDarkWyrm
201e61a091fSDarkWyrm			for (int32 i = 0; i < fListView->CountRows(); i++) {
202e61a091fSDarkWyrm				BRow *row = fListView->RowAt(i);
203e61a091fSDarkWyrm				TypeCodeField *field = (TypeCodeField*)row->GetField(1);
204e61a091fSDarkWyrm				if (!field || field->GetResourceData() != item)
205e61a091fSDarkWyrm					continue;
206e61a091fSDarkWyrm
207e61a091fSDarkWyrm				UpdateRow(row);
208e61a091fSDarkWyrm				break;
209e61a091fSDarkWyrm			}
210e61a091fSDarkWyrm			break;
211e61a091fSDarkWyrm		}
21208cf72ceSDarkWyrm		default:
21308cf72ceSDarkWyrm			BView::MessageReceived(msg);
21408cf72ceSDarkWyrm	}
21508cf72ceSDarkWyrm}
21608cf72ceSDarkWyrm
21708cf72ceSDarkWyrm
2188b671a47SDarkWyrmstatus_t
2198b671a47SDarkWyrmResView::SetTo(const entry_ref &dir, const BString &name)
2208b671a47SDarkWyrm{
2218b671a47SDarkWyrm	entry_ref fileRef;
2228b671a47SDarkWyrm
2238b671a47SDarkWyrm	BPath path(&dir);
2248b671a47SDarkWyrm	path.Append(name.String());
2258b671a47SDarkWyrm	BFile file(path.Path(), B_CREATE_FILE | B_READ_WRITE);
2268b671a47SDarkWyrm	if (file.InitCheck() != B_OK)
2278b671a47SDarkWyrm		return B_ERROR;
2288b671a47SDarkWyrm
2298b671a47SDarkWyrm	if (!fRef)
2308b671a47SDarkWyrm		fRef = new entry_ref();
2318b671a47SDarkWyrm
2328b671a47SDarkWyrm	BEntry entry(path.Path());
2338b671a47SDarkWyrm	entry.GetRef(fRef);
2348b671a47SDarkWyrm	fFileName = name;
2358b671a47SDarkWyrm	return B_OK;
2368b671a47SDarkWyrm}
2378b671a47SDarkWyrm
2388b671a47SDarkWyrm
23908cf72ceSDarkWyrmvoid
24008cf72ceSDarkWyrmResView::OpenFile(const entry_ref &ref)
24108cf72ceSDarkWyrm{
24208cf72ceSDarkWyrm	// Add all the 133t resources and attributes of the file
24308cf72ceSDarkWyrm	BFile file(&ref, B_READ_ONLY);
244e61a091fSDarkWyrm	BResources resources;
245e61a091fSDarkWyrm	if (resources.SetTo(&file) != B_OK)
24608cf72ceSDarkWyrm		return;
24708cf72ceSDarkWyrm	file.Unset();
24808cf72ceSDarkWyrm
249e61a091fSDarkWyrm	resources.PreloadResourceType();
25008cf72ceSDarkWyrm
25108cf72ceSDarkWyrm	int32 index = 0;
252e234c034SDarkWyrm	ResDataRow *row;
253e61a091fSDarkWyrm	ResourceData *resData = new ResourceData();
254e61a091fSDarkWyrm	while (resData->SetFromResource(index, resources)) {
255e234c034SDarkWyrm		row = new ResDataRow(resData);
25608cf72ceSDarkWyrm		fListView->AddRow(row);
257e61a091fSDarkWyrm		fDataList.AddItem(resData);
258e61a091fSDarkWyrm		resData = new ResourceData();
25908cf72ceSDarkWyrm		index++;
26008cf72ceSDarkWyrm	}
261e61a091fSDarkWyrm	delete resData;
26208cf72ceSDarkWyrm
26308cf72ceSDarkWyrm	BNode node;
26408cf72ceSDarkWyrm	if (node.SetTo(&ref) == B_OK) {
26508cf72ceSDarkWyrm		char attrName[B_ATTR_NAME_LENGTH];
26608cf72ceSDarkWyrm		node.RewindAttrs();
267e61a091fSDarkWyrm		resData = new ResourceData();
26808cf72ceSDarkWyrm		while (node.GetNextAttrName(attrName) == B_OK) {
269e61a091fSDarkWyrm			if (resData->SetFromAttribute(attrName, node)) {
270e234c034SDarkWyrm				row = new ResDataRow(resData);
27108cf72ceSDarkWyrm				fListView->AddRow(row);
272e61a091fSDarkWyrm				fDataList.AddItem(resData);
273e61a091fSDarkWyrm				resData = new ResourceData();
27408cf72ceSDarkWyrm			}
27508cf72ceSDarkWyrm		}
276e61a091fSDarkWyrm		delete resData;
27708cf72ceSDarkWyrm	}
27808cf72ceSDarkWyrm}
27908cf72ceSDarkWyrm
28008cf72ceSDarkWyrm
2818b671a47SDarkWyrmvoid
2828b671a47SDarkWyrmResView::SaveFile(void)
2838b671a47SDarkWyrm{
2848b671a47SDarkWyrm	if (fSaveStatus == FILE_CLEAN || !fRef)
2858b671a47SDarkWyrm		return;
2868b671a47SDarkWyrm
2878b671a47SDarkWyrm	BFile file(fRef,B_READ_WRITE);
2888b671a47SDarkWyrm	BResources res(&file,true);
2898b671a47SDarkWyrm	file.Unset();
2908b671a47SDarkWyrm
2918b671a47SDarkWyrm	for (int32 i = 0; i < fListView->CountRows(); i++) {
2928b671a47SDarkWyrm		ResDataRow *row = (ResDataRow*)fListView->RowAt(i);
2938b671a47SDarkWyrm		ResourceData *data = row->GetData();
2948b671a47SDarkWyrm		res.AddResource(data->GetType(), data->GetID(), data->GetData(),
2958b671a47SDarkWyrm						data->GetLength(), data->GetName());
2968b671a47SDarkWyrm	}
2978b671a47SDarkWyrm
2988b671a47SDarkWyrm	res.Sync();
2998b671a47SDarkWyrm
3008b671a47SDarkWyrm	if (fSaveStatus == FILE_QUIT_AFTER_SAVE && Window())
3018b671a47SDarkWyrm		Window()->PostMessage(B_QUIT_REQUESTED);
3028b671a47SDarkWyrm	SetSaveStatus(FILE_CLEAN);
3038b671a47SDarkWyrm}
3048b671a47SDarkWyrm
3058b671a47SDarkWyrm
3068b671a47SDarkWyrmvoid
3078b671a47SDarkWyrmResView::SaveAndQuit(void)
3088b671a47SDarkWyrm{
3098b671a47SDarkWyrm	SetSaveStatus(FILE_QUIT_AFTER_SAVE);
3108b671a47SDarkWyrm	if (!fRef) {
3118b671a47SDarkWyrm		fSavePanel->Show();
3128b671a47SDarkWyrm		return;
3138b671a47SDarkWyrm	}
3148b671a47SDarkWyrm
3158b671a47SDarkWyrm	SaveFile();
3168b671a47SDarkWyrm}
3178b671a47SDarkWyrm
3188b671a47SDarkWyrm
319e234c034SDarkWyrmvoid
320e234c034SDarkWyrmResView::BuildMenus(BMenuBar *menuBar)
321e234c034SDarkWyrm{
322e234c034SDarkWyrm	BMenu *menu = new BMenu("File");
323e234c034SDarkWyrm	menu->AddItem(new BMenuItem("New" B_UTF8_ELLIPSIS, new BMessage(M_NEW_FILE), 'N'));
324e234c034SDarkWyrm	menu->AddSeparatorItem();
325e234c034SDarkWyrm	menu->AddItem(new BMenuItem("Open" B_UTF8_ELLIPSIS, new BMessage(M_OPEN_FILE), 'O'));
3268b671a47SDarkWyrm	menu->AddSeparatorItem();
3278b671a47SDarkWyrm	menu->AddItem(new BMenuItem("Save", new BMessage(M_SAVE_FILE), 'S'));
3288b671a47SDarkWyrm	menu->AddItem(new BMenuItem("Save As" B_UTF8_ELLIPSIS, new BMessage(M_SHOW_SAVE_PANEL), 'S',
3298b671a47SDarkWyrm								B_COMMAND_KEY | B_SHIFT_KEY));
330e234c034SDarkWyrm	menuBar->AddItem(menu);
331e234c034SDarkWyrm
332e234c034SDarkWyrm	menu = new BMenu("Resource");
333e234c034SDarkWyrm	menu->AddItem(new BMenuItem("Add" B_UTF8_ELLIPSIS, new BMessage(M_SELECT_FILE), 'F'));
334e234c034SDarkWyrm	menu->AddItem(new BMenuItem("Delete", new BMessage(M_DELETE_RESOURCE), 'D'));
335e234c034SDarkWyrm	menuBar->AddItem(menu);
336e234c034SDarkWyrm}
337e234c034SDarkWyrm
338e234c034SDarkWyrm
339e61a091fSDarkWyrmvoid
340e61a091fSDarkWyrmResView::EmptyDataList(void)
34108cf72ceSDarkWyrm{
342e61a091fSDarkWyrm	for (int32 i = 0; i < fDataList.CountItems(); i++) {
343e61a091fSDarkWyrm		ResourceData *data = (ResourceData*) fDataList.ItemAt(i);
344e61a091fSDarkWyrm		delete data;
345e61a091fSDarkWyrm	}
346e61a091fSDarkWyrm	fDataList.MakeEmpty();
34708cf72ceSDarkWyrm}
34808cf72ceSDarkWyrm
34908cf72ceSDarkWyrm
350e61a091fSDarkWyrmvoid
351e61a091fSDarkWyrmResView::UpdateRow(BRow *row)
35208cf72ceSDarkWyrm{
353e61a091fSDarkWyrm	TypeCodeField *typeField = (TypeCodeField*) row->GetField(1);
354e61a091fSDarkWyrm	ResourceData *resData = typeField->GetResourceData();
355e61a091fSDarkWyrm	BStringField *strField = (BStringField *)row->GetField(0);
356e61a091fSDarkWyrm
357e61a091fSDarkWyrm	if (strcmp("(attr)", strField->String()) != 0)
358e61a091fSDarkWyrm		strField->SetString(resData->GetIDString());
359e61a091fSDarkWyrm
360e61a091fSDarkWyrm	strField = (BStringField *)row->GetField(2);
36155779173SDarkWyrm	if (strField)
36255779173SDarkWyrm		strField->SetString(resData->GetName());
363e61a091fSDarkWyrm
364e61a091fSDarkWyrm	PreviewField *preField = (PreviewField*)row->GetField(3);
36555779173SDarkWyrm	if (preField)
36655779173SDarkWyrm		preField->SetData(resData->GetData(), resData->GetLength());
367e61a091fSDarkWyrm
368e61a091fSDarkWyrm	BSizeField *sizeField = (BSizeField*)row->GetField(4);
36955779173SDarkWyrm	if (sizeField)
37055779173SDarkWyrm		sizeField->SetSize(resData->GetLength());
37108cf72ceSDarkWyrm}
372e234c034SDarkWyrm
373e234c034SDarkWyrm
374e234c034SDarkWyrmvoid
375e234c034SDarkWyrmResView::AddResource(const entry_ref &ref)
376e234c034SDarkWyrm{
37709a03cb8SDarkWyrm	BFile file(&ref, B_READ_ONLY);
378e234c034SDarkWyrm	if (file.InitCheck() != B_OK)
379e234c034SDarkWyrm		return;
380e234c034SDarkWyrm
381e234c034SDarkWyrm	BString mime;
38209a03cb8SDarkWyrm	file.ReadAttrString("BEOS:TYPE", &mime);
383e234c034SDarkWyrm
384e234c034SDarkWyrm	if (mime == "application/x-be-resource") {
385e234c034SDarkWyrm		BMessage msg(B_REFS_RECEIVED);
38609a03cb8SDarkWyrm		msg.AddRef("refs", &ref);
387e234c034SDarkWyrm		be_app->PostMessage(&msg);
388e234c034SDarkWyrm		return;
389e234c034SDarkWyrm	}
390e234c034SDarkWyrm
391e234c034SDarkWyrm	type_code fileType = 0;
392e234c034SDarkWyrm
393e234c034SDarkWyrm	BTranslatorRoster *roster = BTranslatorRoster::Default();
394e234c034SDarkWyrm	translator_info info;
39509a03cb8SDarkWyrm	if (roster->Identify(&file, NULL, &info, 0, mime.String()) == B_OK)
396e234c034SDarkWyrm		fileType = info.type;
397e234c034SDarkWyrm	else
398e234c034SDarkWyrm		fileType = B_RAW_TYPE;
399e234c034SDarkWyrm
400e234c034SDarkWyrm	int32 lastID = -1;
401e234c034SDarkWyrm	for (int32 i = 0; i < fDataList.CountItems(); i++) {
402e234c034SDarkWyrm		ResourceData *resData = (ResourceData*)fDataList.ItemAt(i);
403e234c034SDarkWyrm		if (resData->GetType() == fileType && resData->GetID() > lastID)
404e234c034SDarkWyrm			lastID = resData->GetID();
405e234c034SDarkWyrm	}
406e234c034SDarkWyrm
407e234c034SDarkWyrm	off_t fileSize;
408e234c034SDarkWyrm	file.GetSize(&fileSize);
409e234c034SDarkWyrm
410e234c034SDarkWyrm	if (fileSize < 1)
411e234c034SDarkWyrm		return;
412e234c034SDarkWyrm
413e234c034SDarkWyrm	char *fileData = (char *)malloc(fileSize);
41409a03cb8SDarkWyrm	file.Read(fileData, fileSize);
415e234c034SDarkWyrm
41609a03cb8SDarkWyrm	ResourceData *resData = new ResourceData(fileType, lastID + 1, ref.name,
41709a03cb8SDarkWyrm											fileData, fileSize);
418e234c034SDarkWyrm	fDataList.AddItem(resData);
419e234c034SDarkWyrm
420e234c034SDarkWyrm	ResDataRow *row = new ResDataRow(resData);
421e234c034SDarkWyrm	fListView->AddRow(row);
422e234c034SDarkWyrm
4238b671a47SDarkWyrm	SetSaveStatus(FILE_DIRTY);
424e234c034SDarkWyrm}
425e234c034SDarkWyrm
426e234c034SDarkWyrm
427e234c034SDarkWyrmvoid
428e234c034SDarkWyrmResView::DeleteSelectedResources(void)
429e234c034SDarkWyrm{
430e234c034SDarkWyrm	ResDataRow *selection = (ResDataRow*)fListView->CurrentSelection();
4318b671a47SDarkWyrm	if (!selection)
4328b671a47SDarkWyrm		return;
433e234c034SDarkWyrm
4348b671a47SDarkWyrm	SetSaveStatus(FILE_DIRTY);
435e234c034SDarkWyrm
436e234c034SDarkWyrm	while (selection) {
437e234c034SDarkWyrm		ResourceData *data = selection->GetData();
438e234c034SDarkWyrm		fListView->RemoveRow(selection);
439e234c034SDarkWyrm		fDataList.RemoveItem(data);
440e234c034SDarkWyrm		delete data;
441e234c034SDarkWyrm		selection = (ResDataRow*)fListView->CurrentSelection();
442e234c034SDarkWyrm	}
443e234c034SDarkWyrm}
444e234c034SDarkWyrm
445e234c034SDarkWyrm
4468b671a47SDarkWyrmvoid
4478b671a47SDarkWyrmResView::SetSaveStatus(uint8 value)
4488b671a47SDarkWyrm{
4498b671a47SDarkWyrm	if (value == fSaveStatus)
4508b671a47SDarkWyrm		return;
4518b671a47SDarkWyrm
4528b671a47SDarkWyrm	fSaveStatus = value;
4538b671a47SDarkWyrm
4548b671a47SDarkWyrm	BString title("ResEdit: ");
4558b671a47SDarkWyrm	title << fFileName;
4568b671a47SDarkWyrm	if (fSaveStatus == FILE_DIRTY)
4578b671a47SDarkWyrm		title << "*";
4588b671a47SDarkWyrm
4598b671a47SDarkWyrm	if (Window()) {
4608b671a47SDarkWyrm		Window()->Lock();
4618b671a47SDarkWyrm		Window()->SetTitle(title.String());
4628b671a47SDarkWyrm		Window()->Unlock();
4638b671a47SDarkWyrm	}
4648b671a47SDarkWyrm}
4658b671a47SDarkWyrm
4668b671a47SDarkWyrm
467e234c034SDarkWyrmResDataRow::ResDataRow(ResourceData *data)
468e234c034SDarkWyrm  :	fResData(data)
469e234c034SDarkWyrm{
470e234c034SDarkWyrm	if (data) {
47109a03cb8SDarkWyrm		SetField(new BStringField(fResData->GetIDString()), 0);
47209a03cb8SDarkWyrm		SetField(new TypeCodeField(fResData->GetType(), fResData), 1);
47309a03cb8SDarkWyrm		SetField(new BStringField(fResData->GetName()), 2);
474e234c034SDarkWyrm		BField *field = gResRoster.MakeFieldForType(fResData->GetType(),
475e234c034SDarkWyrm													fResData->GetData(),
476e234c034SDarkWyrm													fResData->GetLength());
477e234c034SDarkWyrm		if (field)
47809a03cb8SDarkWyrm			SetField(field, 3);
47909a03cb8SDarkWyrm		SetField(new BSizeField(fResData->GetLength()), 4);
480e234c034SDarkWyrm	}
481e234c034SDarkWyrm}
482e234c034SDarkWyrm
483e234c034SDarkWyrm
484e234c034SDarkWyrmResourceData *
485e234c034SDarkWyrmResDataRow::GetData(void) const
486e234c034SDarkWyrm{
487e234c034SDarkWyrm	return fResData;
488e234c034SDarkWyrm}
489