xref: /haiku/src/apps/resedit/ResFields.cpp (revision 893988af824e65e49e55f517b157db8386e8002b)
1 #include "ResFields.h"
2 #include "ResourceData.h"
3 #include <DataIO.h>
4 #include <TranslationUtils.h>
5 
6 TypeCodeField::TypeCodeField(const type_code &code, ResourceData *data)
7   :	BStringField(MakeTypeString(code).String()),
8 	fTypeCode(code),
9 	fData(data)
10 {
11 }
12 
13 
14 PreviewField::PreviewField(void)
15 {
16 }
17 
18 
19 PreviewField::~PreviewField(void)
20 {
21 }
22 
23 
24 BitmapPreviewField::BitmapPreviewField(BBitmap *bitmap)
25 {
26 	fBitmap = bitmap;
27 }
28 
29 
30 BitmapPreviewField::~BitmapPreviewField(void)
31 {
32 	delete fBitmap;
33 }
34 
35 
36 void
37 BitmapPreviewField::DrawField(BRect rect, BView *parent)
38 {
39 	if (fBitmap) {
40 		// Scale the fBitmap down to completely fit within the field's height
41 		BRect drawrect(fBitmap->Bounds().OffsetToCopy(rect.LeftTop()));
42 		if (drawrect.Height() > rect.Height()) {
43 			drawrect = rect;
44 			drawrect.right = drawrect.left +
45 							(fBitmap->Bounds().Width() *
46 							(rect.Height() / fBitmap->Bounds().Height()));
47 		}
48 
49 		parent->SetDrawingMode(B_OP_ALPHA);
50 		parent->DrawBitmap(fBitmap, fBitmap->Bounds(), drawrect);
51 		parent->SetDrawingMode(B_OP_COPY);
52 
53 		BString out;
54 		out << fBitmap->Bounds().IntegerWidth() << " x "
55 			<< fBitmap->Bounds().IntegerHeight()	<< " x "
56 			<< (int32(fBitmap->BytesPerRow() / fBitmap->Bounds().Width()) * 8);
57 
58 		BRect stringrect = rect;
59 		stringrect.right -= 10;
60 		stringrect.left = stringrect.right - parent->StringWidth(out.String());
61 		if (stringrect.left < drawrect.right + 5)
62 			stringrect.left = drawrect.right + 5;
63 
64 		parent->TruncateString(&out, B_TRUNCATE_END, stringrect.Width());
65 		parent->DrawString(out.String(), stringrect.LeftTop());
66 	}
67 }
68 
69 
70 void
71 BitmapPreviewField::SetData(char *data, const size_t &length)
72 {
73 	BMemoryIO memio(data,length);
74 	fBitmap = BTranslationUtils::GetBitmap(&memio);
75 }
76 
77 
78 IntegerPreviewField::IntegerPreviewField(const int64 &value)
79 {
80 	fValue = value;
81 }
82 
83 
84 IntegerPreviewField::~IntegerPreviewField(void)
85 {
86 }
87 
88 
89 void
90 IntegerPreviewField::DrawField(BRect rect, BView *parent)
91 {
92 	BPoint point(rect.LeftBottom());
93 	point.y -= 2;
94 
95 	BString	string;
96 	string << fValue;
97 	parent->TruncateString(&string, B_TRUNCATE_MIDDLE, rect.Width() - 14);
98 	parent->DrawString(string.String(), point);
99 }
100 
101 
102 void
103 IntegerPreviewField::SetData(char *data, const size_t &length)
104 {
105 	switch(length) {
106 		case 8: {
107 			fValue = *((int8*)data);
108 			break;
109 		}
110 		case 16: {
111 			fValue = *((int16*)data);
112 			break;
113 		}
114 		case 32: {
115 			fValue = *((int32*)data);
116 			break;
117 		}
118 		case 64: {
119 			fValue = *((int32*)data);
120 			break;
121 		}
122 		default: {
123 			break;
124 		}
125 	}
126 }
127 
128 
129 StringPreviewField::StringPreviewField(const char *string)
130 {
131 	fClipped = fString = string;
132 }
133 
134 
135 StringPreviewField::~StringPreviewField(void)
136 {
137 }
138 
139 
140 void
141 StringPreviewField::DrawField(BRect rect, BView *parent)
142 {
143 	BPoint point(rect.LeftBottom());
144 	point.y -= 2;
145 
146 	fClipped = fString;
147 	parent->TruncateString(&fClipped, B_TRUNCATE_END, rect.Width() - 14);
148 	parent->DrawString(fClipped.String(), point);
149 }
150 
151 
152 void
153 StringPreviewField::SetData(char *data, const size_t &length)
154 {
155 	char *temp = fString.LockBuffer(length + 2);
156 	if (temp)
157 		memcpy(temp, data, length);
158 	temp[length] = '\0';
159 	fString.UnlockBuffer();
160 	fClipped = fString;
161 }
162 
163 
164 BString
165 MakeTypeString(int32 type)
166 {
167 	char typestring[7];
168 	char *typeptr = (char *) &type;
169 	typestring[0] = '\'';
170 	typestring[1] = typeptr[3];
171 	typestring[2] = typeptr[2];
172 	typestring[3] = typeptr[1];
173 	typestring[4] = typeptr[0];
174 	typestring[5] = '\'';
175 	typestring[6] = '\0';
176 	return BString(typestring);
177 }
178