1 /* 2 * Copyright (c) 2005-2010, Haiku, Inc. 3 * Distributed under the terms of the MIT license. 4 * 5 * Author: 6 * DarkWyrm <darkwyrm@gmail.com> 7 */ 8 #include "ResFields.h" 9 #include "ResourceData.h" 10 11 #include <DataIO.h> 12 #include <TranslationUtils.h> 13 14 TypeCodeField::TypeCodeField(const type_code &code, ResourceData *data) 15 : BStringField(MakeTypeString(code).String()), 16 fTypeCode(code), 17 fData(data) 18 { 19 } 20 21 22 PreviewField::PreviewField(void) 23 { 24 } 25 26 27 PreviewField::~PreviewField(void) 28 { 29 } 30 31 32 BitmapPreviewField::BitmapPreviewField(BBitmap *bitmap) 33 { 34 fBitmap = bitmap; 35 } 36 37 38 BitmapPreviewField::~BitmapPreviewField(void) 39 { 40 delete fBitmap; 41 } 42 43 44 void 45 BitmapPreviewField::DrawField(BRect rect, BView *parent) 46 { 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())); 55 } 56 57 parent->SetDrawingMode(B_OP_ALPHA); 58 parent->DrawBitmap(fBitmap, fBitmap->Bounds(), drawrect); 59 parent->SetDrawingMode(B_OP_COPY); 60 61 BString out; 62 out.SetToFormat("%" B_PRId32" × %" B_PRId32" × %" B_PRId32, 63 fBitmap->Bounds().IntegerWidth(), fBitmap->Bounds().IntegerHeight(), 64 (int32(fBitmap->BytesPerRow() / fBitmap->Bounds().Width()) * 8)); 65 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; 71 72 parent->TruncateString(&out, B_TRUNCATE_END, stringrect.Width()); 73 parent->DrawString(out.String(), stringrect.LeftTop()); 74 } 75 } 76 77 78 void 79 BitmapPreviewField::SetData(char *data, const size_t &length) 80 { 81 BMemoryIO memio(data, length); 82 fBitmap = BTranslationUtils::GetBitmap(&memio); 83 } 84 85 86 IntegerPreviewField::IntegerPreviewField(const int64 &value) 87 { 88 fValue = value; 89 } 90 91 92 IntegerPreviewField::~IntegerPreviewField(void) 93 { 94 } 95 96 97 void 98 IntegerPreviewField::DrawField(BRect rect, BView *parent) 99 { 100 BPoint point(rect.LeftBottom()); 101 point.y -= 2; 102 103 BString string; 104 string << fValue; 105 parent->TruncateString(&string, B_TRUNCATE_MIDDLE, rect.Width() - 14); 106 parent->DrawString(string.String(), point); 107 } 108 109 110 void 111 IntegerPreviewField::SetData(char *data, const size_t &length) 112 { 113 switch(length) { 114 case 8: { 115 fValue = *((int8*)data); 116 break; 117 } 118 case 16: { 119 fValue = *((int16*)data); 120 break; 121 } 122 case 32: { 123 fValue = *((int32*)data); 124 break; 125 } 126 case 64: { 127 fValue = *((int32*)data); 128 break; 129 } 130 default: { 131 break; 132 } 133 } 134 } 135 136 137 StringPreviewField::StringPreviewField(const char *string) 138 { 139 fClipped = fString = string; 140 } 141 142 143 StringPreviewField::~StringPreviewField(void) 144 { 145 } 146 147 148 void 149 StringPreviewField::DrawField(BRect rect, BView *parent) 150 { 151 BPoint point(rect.LeftBottom()); 152 point.y -= 2; 153 154 fClipped = fString; 155 parent->TruncateString(&fClipped, B_TRUNCATE_END, rect.Width() - 14); 156 parent->DrawString(fClipped.String(), point); 157 } 158 159 160 void 161 StringPreviewField::SetData(char *data, const size_t &length) 162 { 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; 169 } 170 171 172 BString 173 MakeTypeString(int32 type) 174 { 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); 185 } 186