xref: /haiku/src/apps/resedit/ResFields.cpp (revision 3961af9fe94e645328eb4b510e5daea351b1ceae)
1e234c034SDarkWyrm /*
2e234c034SDarkWyrm  * Copyright (c) 2005-2010, Haiku, Inc.
3e234c034SDarkWyrm  * Distributed under the terms of the MIT license.
4e234c034SDarkWyrm  *
5e234c034SDarkWyrm  * Author:
6e234c034SDarkWyrm  *		DarkWyrm <darkwyrm@gmail.com>
7e234c034SDarkWyrm  */
822c6ae69SDarkWyrm #include "ResFields.h"
922c6ae69SDarkWyrm #include "ResourceData.h"
1009a03cb8SDarkWyrm 
1122c6ae69SDarkWyrm #include <DataIO.h>
1222c6ae69SDarkWyrm #include <TranslationUtils.h>
1322c6ae69SDarkWyrm 
TypeCodeField(const type_code & code,ResourceData * data)1422c6ae69SDarkWyrm TypeCodeField::TypeCodeField(const type_code &code, ResourceData *data)
1522c6ae69SDarkWyrm   :	BStringField(MakeTypeString(code).String()),
1622c6ae69SDarkWyrm 	fTypeCode(code),
1722c6ae69SDarkWyrm 	fData(data)
1822c6ae69SDarkWyrm {
1922c6ae69SDarkWyrm }
2022c6ae69SDarkWyrm 
2122c6ae69SDarkWyrm 
PreviewField(void)2222c6ae69SDarkWyrm PreviewField::PreviewField(void)
2322c6ae69SDarkWyrm {
2422c6ae69SDarkWyrm }
2522c6ae69SDarkWyrm 
2622c6ae69SDarkWyrm 
~PreviewField(void)2722c6ae69SDarkWyrm PreviewField::~PreviewField(void)
2822c6ae69SDarkWyrm {
2922c6ae69SDarkWyrm }
3022c6ae69SDarkWyrm 
3122c6ae69SDarkWyrm 
BitmapPreviewField(BBitmap * bitmap)3222c6ae69SDarkWyrm BitmapPreviewField::BitmapPreviewField(BBitmap *bitmap)
3322c6ae69SDarkWyrm {
3422c6ae69SDarkWyrm 	fBitmap = bitmap;
3522c6ae69SDarkWyrm }
3622c6ae69SDarkWyrm 
3722c6ae69SDarkWyrm 
~BitmapPreviewField(void)3822c6ae69SDarkWyrm BitmapPreviewField::~BitmapPreviewField(void)
3922c6ae69SDarkWyrm {
4022c6ae69SDarkWyrm 	delete fBitmap;
4122c6ae69SDarkWyrm }
4222c6ae69SDarkWyrm 
4322c6ae69SDarkWyrm 
4422c6ae69SDarkWyrm void
DrawField(BRect rect,BView * parent)4522c6ae69SDarkWyrm BitmapPreviewField::DrawField(BRect rect, BView *parent)
4622c6ae69SDarkWyrm {
4722c6ae69SDarkWyrm 	if (fBitmap) {
4822c6ae69SDarkWyrm 		// Scale the fBitmap down to completely fit within the field's height
4922c6ae69SDarkWyrm 		BRect drawrect(fBitmap->Bounds().OffsetToCopy(rect.LeftTop()));
5022c6ae69SDarkWyrm 		if (drawrect.Height() > rect.Height()) {
5122c6ae69SDarkWyrm 			drawrect = rect;
5222c6ae69SDarkWyrm 			drawrect.right = drawrect.left +
5322c6ae69SDarkWyrm 							(fBitmap->Bounds().Width() *
5422c6ae69SDarkWyrm 							(rect.Height() / fBitmap->Bounds().Height()));
5522c6ae69SDarkWyrm 		}
5622c6ae69SDarkWyrm 
5722c6ae69SDarkWyrm 		parent->SetDrawingMode(B_OP_ALPHA);
5822c6ae69SDarkWyrm 		parent->DrawBitmap(fBitmap, fBitmap->Bounds(), drawrect);
5922c6ae69SDarkWyrm 		parent->SetDrawingMode(B_OP_COPY);
6022c6ae69SDarkWyrm 
6122c6ae69SDarkWyrm 		BString out;
62*3961af9fSHumdinger 		out.SetToFormat("%" B_PRId32" × %" B_PRId32" × %" B_PRId32,
63*3961af9fSHumdinger 			fBitmap->Bounds().IntegerWidth(), fBitmap->Bounds().IntegerHeight(),
64*3961af9fSHumdinger 			(int32(fBitmap->BytesPerRow() / fBitmap->Bounds().Width()) * 8));
6522c6ae69SDarkWyrm 
6622c6ae69SDarkWyrm 		BRect stringrect = rect;
6722c6ae69SDarkWyrm 		stringrect.right -= 10;
6822c6ae69SDarkWyrm 		stringrect.left = stringrect.right - parent->StringWidth(out.String());
6922c6ae69SDarkWyrm 		if (stringrect.left < drawrect.right + 5)
7022c6ae69SDarkWyrm 			stringrect.left = drawrect.right + 5;
7122c6ae69SDarkWyrm 
7222c6ae69SDarkWyrm 		parent->TruncateString(&out, B_TRUNCATE_END, stringrect.Width());
7322c6ae69SDarkWyrm 		parent->DrawString(out.String(), stringrect.LeftTop());
7422c6ae69SDarkWyrm 	}
7522c6ae69SDarkWyrm }
7622c6ae69SDarkWyrm 
7722c6ae69SDarkWyrm 
7822c6ae69SDarkWyrm void
SetData(char * data,const size_t & length)7922c6ae69SDarkWyrm BitmapPreviewField::SetData(char *data, const size_t &length)
8022c6ae69SDarkWyrm {
8122c6ae69SDarkWyrm 	BMemoryIO memio(data, length);
8222c6ae69SDarkWyrm 	fBitmap = BTranslationUtils::GetBitmap(&memio);
8322c6ae69SDarkWyrm }
8422c6ae69SDarkWyrm 
8522c6ae69SDarkWyrm 
IntegerPreviewField(const int64 & value)8622c6ae69SDarkWyrm IntegerPreviewField::IntegerPreviewField(const int64 &value)
8722c6ae69SDarkWyrm {
8822c6ae69SDarkWyrm 	fValue = value;
8922c6ae69SDarkWyrm }
9022c6ae69SDarkWyrm 
9122c6ae69SDarkWyrm 
~IntegerPreviewField(void)9222c6ae69SDarkWyrm IntegerPreviewField::~IntegerPreviewField(void)
9322c6ae69SDarkWyrm {
9422c6ae69SDarkWyrm }
9522c6ae69SDarkWyrm 
9622c6ae69SDarkWyrm 
9722c6ae69SDarkWyrm void
DrawField(BRect rect,BView * parent)9822c6ae69SDarkWyrm IntegerPreviewField::DrawField(BRect rect, BView *parent)
9922c6ae69SDarkWyrm {
10022c6ae69SDarkWyrm 	BPoint point(rect.LeftBottom());
10122c6ae69SDarkWyrm 	point.y -= 2;
10222c6ae69SDarkWyrm 
10322c6ae69SDarkWyrm 	BString	string;
10422c6ae69SDarkWyrm 	string << fValue;
10522c6ae69SDarkWyrm 	parent->TruncateString(&string, B_TRUNCATE_MIDDLE, rect.Width() - 14);
10622c6ae69SDarkWyrm 	parent->DrawString(string.String(), point);
10722c6ae69SDarkWyrm }
10822c6ae69SDarkWyrm 
10922c6ae69SDarkWyrm 
11022c6ae69SDarkWyrm void
SetData(char * data,const size_t & length)11122c6ae69SDarkWyrm IntegerPreviewField::SetData(char *data, const size_t &length)
11222c6ae69SDarkWyrm {
11322c6ae69SDarkWyrm 	switch(length) {
11422c6ae69SDarkWyrm 		case 8: {
11522c6ae69SDarkWyrm 			fValue = *((int8*)data);
11622c6ae69SDarkWyrm 			break;
11722c6ae69SDarkWyrm 		}
11822c6ae69SDarkWyrm 		case 16: {
11922c6ae69SDarkWyrm 			fValue = *((int16*)data);
12022c6ae69SDarkWyrm 			break;
12122c6ae69SDarkWyrm 		}
12222c6ae69SDarkWyrm 		case 32: {
12322c6ae69SDarkWyrm 			fValue = *((int32*)data);
12422c6ae69SDarkWyrm 			break;
12522c6ae69SDarkWyrm 		}
12622c6ae69SDarkWyrm 		case 64: {
12722c6ae69SDarkWyrm 			fValue = *((int32*)data);
12822c6ae69SDarkWyrm 			break;
12922c6ae69SDarkWyrm 		}
13022c6ae69SDarkWyrm 		default: {
13122c6ae69SDarkWyrm 			break;
13222c6ae69SDarkWyrm 		}
13322c6ae69SDarkWyrm 	}
13422c6ae69SDarkWyrm }
13522c6ae69SDarkWyrm 
13622c6ae69SDarkWyrm 
StringPreviewField(const char * string)13722c6ae69SDarkWyrm StringPreviewField::StringPreviewField(const char *string)
13822c6ae69SDarkWyrm {
13922c6ae69SDarkWyrm 	fClipped = fString = string;
14022c6ae69SDarkWyrm }
14122c6ae69SDarkWyrm 
14222c6ae69SDarkWyrm 
~StringPreviewField(void)14322c6ae69SDarkWyrm StringPreviewField::~StringPreviewField(void)
14422c6ae69SDarkWyrm {
14522c6ae69SDarkWyrm }
14622c6ae69SDarkWyrm 
14722c6ae69SDarkWyrm 
14822c6ae69SDarkWyrm void
DrawField(BRect rect,BView * parent)14922c6ae69SDarkWyrm StringPreviewField::DrawField(BRect rect, BView *parent)
15022c6ae69SDarkWyrm {
15122c6ae69SDarkWyrm 	BPoint point(rect.LeftBottom());
15222c6ae69SDarkWyrm 	point.y -= 2;
15322c6ae69SDarkWyrm 
15422c6ae69SDarkWyrm 	fClipped = fString;
15522c6ae69SDarkWyrm 	parent->TruncateString(&fClipped, B_TRUNCATE_END, rect.Width() - 14);
15622c6ae69SDarkWyrm 	parent->DrawString(fClipped.String(), point);
15722c6ae69SDarkWyrm }
15822c6ae69SDarkWyrm 
15922c6ae69SDarkWyrm 
16022c6ae69SDarkWyrm void
SetData(char * data,const size_t & length)16122c6ae69SDarkWyrm StringPreviewField::SetData(char *data, const size_t &length)
16222c6ae69SDarkWyrm {
16322c6ae69SDarkWyrm 	char *temp = fString.LockBuffer(length + 2);
16422c6ae69SDarkWyrm 	if (temp)
16522c6ae69SDarkWyrm 		memcpy(temp, data, length);
16622c6ae69SDarkWyrm 	temp[length] = '\0';
16722c6ae69SDarkWyrm 	fString.UnlockBuffer();
16822c6ae69SDarkWyrm 	fClipped = fString;
16922c6ae69SDarkWyrm }
17022c6ae69SDarkWyrm 
17122c6ae69SDarkWyrm 
17222c6ae69SDarkWyrm BString
MakeTypeString(int32 type)17322c6ae69SDarkWyrm MakeTypeString(int32 type)
17422c6ae69SDarkWyrm {
17522c6ae69SDarkWyrm 	char typestring[7];
17622c6ae69SDarkWyrm 	char *typeptr = (char *) &type;
17722c6ae69SDarkWyrm 	typestring[0] = '\'';
17822c6ae69SDarkWyrm 	typestring[1] = typeptr[3];
17922c6ae69SDarkWyrm 	typestring[2] = typeptr[2];
18022c6ae69SDarkWyrm 	typestring[3] = typeptr[1];
18122c6ae69SDarkWyrm 	typestring[4] = typeptr[0];
18222c6ae69SDarkWyrm 	typestring[5] = '\'';
18322c6ae69SDarkWyrm 	typestring[6] = '\0';
18422c6ae69SDarkWyrm 	return BString(typestring);
18522c6ae69SDarkWyrm }
186