144f919daSRyan Leavengood /*
244f919daSRyan Leavengood * Copyright (c) 2007, Haiku, Inc.
344f919daSRyan Leavengood * Distributed under the terms of the MIT license.
444f919daSRyan Leavengood *
544f919daSRyan Leavengood * Author:
644f919daSRyan Leavengood * Łukasz 'Sil2100' Zemczak <sil2100@vexillium.org>
744f919daSRyan Leavengood */
844f919daSRyan Leavengood
944f919daSRyan Leavengood
1044f919daSRyan Leavengood #include "PackageImageViewer.h"
1144f919daSRyan Leavengood
1244f919daSRyan Leavengood #include <BitmapStream.h>
13091440e0SStephan Aßmus #include <Catalog.h>
14091440e0SStephan Aßmus #include <Locale.h>
1544f919daSRyan Leavengood #include <Message.h>
1644f919daSRyan Leavengood #include <Screen.h>
1744f919daSRyan Leavengood #include <TranslatorRoster.h>
1844f919daSRyan Leavengood
1944f919daSRyan Leavengood
20546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT
21546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "PackageImageViewer"
2244f919daSRyan Leavengood
2344f919daSRyan Leavengood
ImageView(BPositionIO * imageIO)2479350ddeSStephan Aßmus ImageView::ImageView(BPositionIO* imageIO)
25091440e0SStephan Aßmus :
26091440e0SStephan Aßmus BView(BRect(0, 0, 1, 1), "image_view", B_FOLLOW_NONE, B_WILL_DRAW),
2779350ddeSStephan Aßmus fImage(NULL)
2844f919daSRyan Leavengood {
2979350ddeSStephan Aßmus if (imageIO == NULL)
3044f919daSRyan Leavengood return;
3179350ddeSStephan Aßmus
3244f919daSRyan Leavengood // Initialize and translate the image
3344f919daSRyan Leavengood BTranslatorRoster* roster = BTranslatorRoster::Default();
3444f919daSRyan Leavengood BBitmapStream stream;
3579350ddeSStephan Aßmus if (roster->Translate(imageIO, NULL, NULL, &stream, B_TRANSLATOR_BITMAP)
3679350ddeSStephan Aßmus != B_OK) {
3744f919daSRyan Leavengood return;
3844f919daSRyan Leavengood }
3944f919daSRyan Leavengood stream.DetachBitmap(&fImage);
4044f919daSRyan Leavengood }
4144f919daSRyan Leavengood
4244f919daSRyan Leavengood
~ImageView()4344f919daSRyan Leavengood ImageView::~ImageView()
4444f919daSRyan Leavengood {
4579350ddeSStephan Aßmus delete fImage;
4644f919daSRyan Leavengood }
4744f919daSRyan Leavengood
4844f919daSRyan Leavengood
4944f919daSRyan Leavengood void
AttachedToWindow()5044f919daSRyan Leavengood ImageView::AttachedToWindow()
5144f919daSRyan Leavengood {
5279350ddeSStephan Aßmus if (fImage == NULL) {
5344f919daSRyan Leavengood ResizeTo(75, 75);
5444f919daSRyan Leavengood return;
5544f919daSRyan Leavengood }
5644f919daSRyan Leavengood
5744f919daSRyan Leavengood // We need to resize the view depending on what size has the screen and
5844f919daSRyan Leavengood // the image we will be viewing
5944f919daSRyan Leavengood BScreen screen(Window());
6044f919daSRyan Leavengood BRect frame = screen.Frame();
6144f919daSRyan Leavengood BRect image = fImage->Bounds();
6244f919daSRyan Leavengood
63091440e0SStephan Aßmus if (image.Width() > (frame.Width() - 100.0f))
6444f919daSRyan Leavengood image.right = frame.Width() - 100.0f;
65091440e0SStephan Aßmus if (image.Height() > (frame.Height() - 100.0f))
6644f919daSRyan Leavengood image.bottom = frame.Height() - 100.f;
6744f919daSRyan Leavengood
6844f919daSRyan Leavengood ResizeTo(image.Width(), image.Height());
6944f919daSRyan Leavengood }
7044f919daSRyan Leavengood
7144f919daSRyan Leavengood
7244f919daSRyan Leavengood void
Draw(BRect updateRect)7344f919daSRyan Leavengood ImageView::Draw(BRect updateRect)
7444f919daSRyan Leavengood {
7579350ddeSStephan Aßmus if (fImage != NULL)
7644f919daSRyan Leavengood DrawBitmapAsync(fImage, Bounds());
7744f919daSRyan Leavengood else {
7879350ddeSStephan Aßmus const char* message = B_TRANSLATE("Image not loaded correctly");
7979350ddeSStephan Aßmus float width = StringWidth(message);
8079350ddeSStephan Aßmus DrawString(message, BPoint((Bounds().Width() - width) / 2.0f, 30.0f));
8144f919daSRyan Leavengood }
8244f919daSRyan Leavengood }
8344f919daSRyan Leavengood
8444f919daSRyan Leavengood
8544f919daSRyan Leavengood void
MouseUp(BPoint point)8644f919daSRyan Leavengood ImageView::MouseUp(BPoint point)
8744f919daSRyan Leavengood {
8834212840SStephan Aßmus BWindow* window = Window();
8934212840SStephan Aßmus if (window != NULL)
9034212840SStephan Aßmus window->PostMessage(B_QUIT_REQUESTED);
9144f919daSRyan Leavengood }
9244f919daSRyan Leavengood
9344f919daSRyan Leavengood
9444f919daSRyan Leavengood // #pragma mark -
9544f919daSRyan Leavengood
9644f919daSRyan Leavengood
PackageImageViewer(BPositionIO * imageIO)9779350ddeSStephan Aßmus PackageImageViewer::PackageImageViewer(BPositionIO* imageIO)
98091440e0SStephan Aßmus :
99*65688206SStephan Aßmus BlockingWindow(BRect(100, 100, 100, 100), "")
10044f919daSRyan Leavengood {
10179350ddeSStephan Aßmus fBackground = new ImageView(imageIO);
10244f919daSRyan Leavengood AddChild(fBackground);
10344f919daSRyan Leavengood
10444f919daSRyan Leavengood ResizeTo(fBackground->Bounds().Width(), fBackground->Bounds().Height());
10544f919daSRyan Leavengood
10644f919daSRyan Leavengood BScreen screen(this);
10744f919daSRyan Leavengood BRect frame = screen.Frame();
10844f919daSRyan Leavengood MoveTo((frame.Width() - Bounds().Width()) / 2.0f,
10944f919daSRyan Leavengood (frame.Height() - Bounds().Height()) / 2.0f);
11044f919daSRyan Leavengood }
11144f919daSRyan Leavengood
112