xref: /haiku/src/apps/resedit/PreviewColumn.cpp (revision be3db2942c0e8dda63cdd226ec3c99309d3eab0c)
1 /*
2  * Copyright (c) 2005-2006, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  *
5  * Author:
6  *		DarkWyrm <darkwyrm@earthlink.net>
7  */
8 #include "PreviewColumn.h"
9 #include <stdio.h>
10 
11 PreviewColumn::PreviewColumn(const char *title, float width,
12 						 float minWidth, float maxWidth)
13   :	BTitledColumn(title, width, minWidth, maxWidth)
14 {
15 }
16 
17 void
18 PreviewColumn::DrawField(BField* field, BRect rect, BView* parent)
19 {
20 	BIntegerField *intField = dynamic_cast<BIntegerField*>(field);
21 	BStringField *strField = dynamic_cast<BStringField*>(field);
22 	BBitmapField *bmpField = dynamic_cast<BBitmapField*>(field);
23 
24 	if (intField) {
25 		float width = rect.Width() - 16;
26 		BString	string;
27 		string << intField->Value();
28 		parent->TruncateString(&string, B_TRUNCATE_MIDDLE, width + 2);
29 		DrawString(string.String(), parent, rect);
30 	} else if (strField) {
31 		float width = rect.Width() - 16;
32 		if (width != strField->Width()) {
33 			BString out_string(strField->String());
34 
35 			parent->TruncateString(&out_string, B_TRUNCATE_END, width + 2);
36 			strField->SetClippedString(out_string.String());
37 			strField->SetWidth(width);
38 		}
39 		DrawString(strField->ClippedString(), parent, rect);
40 	} else if (bmpField) {
41 		const BBitmap *bitmap = bmpField->Bitmap();
42 
43 		if (bitmap != NULL) {
44 			// Scale the bitmap down to completely fit within the field's height
45 			BRect drawrect(bitmap->Bounds().OffsetToCopy(rect.LeftTop()));
46 			if (drawrect.Height() > rect.Height()) {
47 				drawrect = rect;
48 				drawrect.right = drawrect.left +
49 								(bitmap->Bounds().Width() *
50 								(rect.Height() / bitmap->Bounds().Height()));
51 			}
52 
53 			parent->SetDrawingMode(B_OP_ALPHA);
54 			parent->DrawBitmap(bitmap, bitmap->Bounds(), drawrect);
55 			parent->SetDrawingMode(B_OP_COPY);
56 
57 			BString out;
58 			out << bitmap->Bounds().IntegerWidth() << " x "
59 				<< bitmap->Bounds().IntegerHeight()	<< " x "
60 				<< (int32(bitmap->BytesPerRow() / bitmap->Bounds().Width()) * 8);
61 
62 			BRect stringrect = rect;
63 			stringrect.right -= 10;
64 			stringrect.left = stringrect.right - parent->StringWidth(out.String());
65 			if (stringrect.left < drawrect.right + 5)
66 				stringrect.left = drawrect.right + 5;
67 
68 			parent->TruncateString(&out, B_TRUNCATE_END, stringrect.Width());
69 			DrawString(out.String(), parent, stringrect);
70 		}
71 	}
72 }
73