libroot/posix/stdio: Remove unused portions.
[haiku.git] / src / apps / resedit / ResFields.cpp
blob0634857ad31b10341938735f048e156c403a7f5a
1 /*
2 * Copyright (c) 2005-2010, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
5 * Author:
6 * DarkWyrm <darkwyrm@gmail.com>
7 */
8 #include "ResFields.h"
9 #include "ResourceData.h"
11 #include <DataIO.h>
12 #include <TranslationUtils.h>
14 TypeCodeField::TypeCodeField(const type_code &code, ResourceData *data)
15 : BStringField(MakeTypeString(code).String()),
16 fTypeCode(code),
17 fData(data)
22 PreviewField::PreviewField(void)
27 PreviewField::~PreviewField(void)
32 BitmapPreviewField::BitmapPreviewField(BBitmap *bitmap)
34 fBitmap = bitmap;
38 BitmapPreviewField::~BitmapPreviewField(void)
40 delete fBitmap;
44 void
45 BitmapPreviewField::DrawField(BRect rect, BView *parent)
47 if (fBitmap) {
48 // Scale the fBitmap down to completely fit within the field's height
49 BRect drawrect(fBitmap->Bounds().OffsetToCopy(rect.LeftTop()));
50 if (drawrect.Height() > rect.Height()) {
51 drawrect = rect;
52 drawrect.right = drawrect.left +
53 (fBitmap->Bounds().Width() *
54 (rect.Height() / fBitmap->Bounds().Height()));
57 parent->SetDrawingMode(B_OP_ALPHA);
58 parent->DrawBitmap(fBitmap, fBitmap->Bounds(), drawrect);
59 parent->SetDrawingMode(B_OP_COPY);
61 BString out;
62 out << fBitmap->Bounds().IntegerWidth() << " x "
63 << fBitmap->Bounds().IntegerHeight() << " x "
64 << (int32(fBitmap->BytesPerRow() / fBitmap->Bounds().Width()) * 8);
66 BRect stringrect = rect;
67 stringrect.right -= 10;
68 stringrect.left = stringrect.right - parent->StringWidth(out.String());
69 if (stringrect.left < drawrect.right + 5)
70 stringrect.left = drawrect.right + 5;
72 parent->TruncateString(&out, B_TRUNCATE_END, stringrect.Width());
73 parent->DrawString(out.String(), stringrect.LeftTop());
78 void
79 BitmapPreviewField::SetData(char *data, const size_t &length)
81 BMemoryIO memio(data, length);
82 fBitmap = BTranslationUtils::GetBitmap(&memio);
86 IntegerPreviewField::IntegerPreviewField(const int64 &value)
88 fValue = value;
92 IntegerPreviewField::~IntegerPreviewField(void)
97 void
98 IntegerPreviewField::DrawField(BRect rect, BView *parent)
100 BPoint point(rect.LeftBottom());
101 point.y -= 2;
103 BString string;
104 string << fValue;
105 parent->TruncateString(&string, B_TRUNCATE_MIDDLE, rect.Width() - 14);
106 parent->DrawString(string.String(), point);
110 void
111 IntegerPreviewField::SetData(char *data, const size_t &length)
113 switch(length) {
114 case 8: {
115 fValue = *((int8*)data);
116 break;
118 case 16: {
119 fValue = *((int16*)data);
120 break;
122 case 32: {
123 fValue = *((int32*)data);
124 break;
126 case 64: {
127 fValue = *((int32*)data);
128 break;
130 default: {
131 break;
137 StringPreviewField::StringPreviewField(const char *string)
139 fClipped = fString = string;
143 StringPreviewField::~StringPreviewField(void)
148 void
149 StringPreviewField::DrawField(BRect rect, BView *parent)
151 BPoint point(rect.LeftBottom());
152 point.y -= 2;
154 fClipped = fString;
155 parent->TruncateString(&fClipped, B_TRUNCATE_END, rect.Width() - 14);
156 parent->DrawString(fClipped.String(), point);
160 void
161 StringPreviewField::SetData(char *data, const size_t &length)
163 char *temp = fString.LockBuffer(length + 2);
164 if (temp)
165 memcpy(temp, data, length);
166 temp[length] = '\0';
167 fString.UnlockBuffer();
168 fClipped = fString;
172 BString
173 MakeTypeString(int32 type)
175 char typestring[7];
176 char *typeptr = (char *) &type;
177 typestring[0] = '\'';
178 typestring[1] = typeptr[3];
179 typestring[2] = typeptr[2];
180 typestring[3] = typeptr[1];
181 typestring[4] = typeptr[0];
182 typestring[5] = '\'';
183 typestring[6] = '\0';
184 return BString(typestring);