xref: /haiku/src/apps/resedit/ImageEditor.cpp (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 #include "InternalEditors.h"
2 #include "BitmapView.h"
3 #include "ResourceData.h"
4 #include <TranslationUtils.h>
5 #include <DataIO.h>
6 
7 #define M_IMAGE_CHANGED 'imch'
8 
9 ImageEditor::ImageEditor(const BRect &frame, ResourceData *data, BHandler *owner)
10   :	Editor(frame, data, owner)
11 {
12 	SetFlags(Flags()  | B_NOT_RESIZABLE | B_NOT_ZOOMABLE);
13 	SetTitle(data->GetName());
14 
15 	// Set up the image and the viewer for such
16 	BMemoryIO memio(data->GetData(),data->GetLength());
17 	fImage = BTranslationUtils::GetBitmap(&memio);
18 
19 	BRect imgsize;
20 	if (fImage)
21 		imgsize = ScaleRectToFit(fImage->Bounds(),BRect(0,0,200,200));
22 	else
23 		imgsize.Set(0,0,200,200);
24 
25 	BView *back = new BView(Bounds(),"back",B_FOLLOW_ALL,B_WILL_DRAW);
26 	back->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
27 	AddChild(back);
28 
29 	BRect r;
30 
31 	float labelwidth = be_plain_font->StringWidth("ID: ");
32 	float strwidth = be_plain_font->StringWidth("(attr) ");
33 
34 	font_height fh;
35 	be_plain_font->GetHeight(&fh);
36 	float strheight = fh.ascent + fh.descent + fh.leading + 5;
37 
38 	fIDBox = new BTextControl(BRect(10,10,10 + (strwidth + labelwidth) + 15,
39 									10 + strheight),
40 							  "id","ID: ",data->GetIDString(), NULL);
41 	fIDBox->SetDivider(labelwidth + 5);
42 	back->AddChild(fIDBox);
43 
44 	r = fIDBox->Frame();
45 	r.OffsetBy(r.Width() + 10, 0);
46 	r.right = Bounds().right - 10;
47 	fNameBox = new BTextControl(r,"name","Name: ",data->GetName(), NULL,
48 								B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP);
49 	fNameBox->SetDivider(be_plain_font->StringWidth("Name: ") + 5);
50 	back->AddChild(fNameBox);
51 
52 	r = imgsize;
53 
54 	r.OffsetTo( (Bounds().Width() - r.Width()) / 2,fNameBox->Frame().bottom + 10);
55 	fImageView = new BitmapView(r,"imageview",new BMessage(M_IMAGE_CHANGED),fImage,
56 								NULL, B_PLAIN_BORDER, B_FOLLOW_NONE,
57 								B_WILL_DRAW | B_FRAME_EVENTS);
58 	back->AddChild(fImageView);
59 	fImageView->SetConstrainDrops(false);
60 
61 	// No limit on bitmap size
62 	fImageView->SetMaxBitmapSize(0,0);
63 	fImageView->SetBitmapRemovable(false);
64 
65 	fOK = new BButton(BRect(10,10,11,11),"ok","Cancel",new BMessage(M_UPDATE_RESOURCE),
66 					  B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
67 	fOK->ResizeToPreferred();
68 	fOK->SetLabel("OK");
69 	fOK->MakeDefault(true);
70 	back->AddChild(fOK);
71 
72 	fOK->MoveTo(Bounds().right - fOK->Bounds().Width() - 10,
73 				Bounds().bottom - fOK->Bounds().Height() - 10);
74 	r = fOK->Frame();
75 
76 	r.OffsetBy(-r.Width() - 10, 0);
77 	fCancel = new BButton(r,"cancel","Cancel",new BMessage(B_QUIT_REQUESTED),
78 					  B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
79 	back->AddChild(fCancel);
80 
81 	ResizeTo(MAX(fImageView->Frame().right, fNameBox->Frame().right) + 20,
82 			fImageView->Frame().bottom + fOK->Frame().Height() + 20);
83 }
84 
85 
86 ImageEditor::~ImageEditor(void)
87 {
88 	delete fImage;
89 }
90 
91 
92 void
93 ImageEditor::MessageReceived(BMessage *msg)
94 {
95 	if (msg->WasDropped()) {
96 		fImageView->MessageReceived(msg);
97 	} else if (msg->what == M_IMAGE_CHANGED) {
98 		fImage = fImageView->GetBitmap();
99 		BRect r = ScaleRectToFit(fImage->Bounds(), BRect(0,0,200,200));
100 		fImageView->ResizeTo(r.Width(),r.Height());
101 		ResizeTo(MAX(fImageView->Frame().right, fNameBox->Frame().right) + 20,
102 				fImageView->Frame().bottom + fOK->Frame().Height() + 20);
103 	} else
104 		BWindow::MessageReceived(msg);
105 }
106 
107 
108 void
109 ImageEditor::FrameResized(float w, float h)
110 {
111 	fImageView->MoveTo( (w - fImageView->Bounds().Width()) / 2,
112 						fNameBox->Frame().bottom + 10);
113 }
114