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