1 /* 2 * Copyright (c) 2007, Haiku, Inc. 3 * Distributed under the terms of the MIT license. 4 * 5 * Author: 6 * Łukasz 'Sil2100' Zemczak <sil2100@vexillium.org> 7 */ 8 9 10 #include "PackageImageViewer.h" 11 12 #include <BitmapStream.h> 13 #include <Catalog.h> 14 #include <Locale.h> 15 #include <Message.h> 16 #include <Screen.h> 17 #include <TranslatorRoster.h> 18 19 20 #undef B_TRANSLATION_CONTEXT 21 #define B_TRANSLATION_CONTEXT "PackageImageViewer" 22 23 24 ImageView::ImageView(BPositionIO* imageIO) 25 : 26 BView(BRect(0, 0, 1, 1), "image_view", B_FOLLOW_NONE, B_WILL_DRAW), 27 fImage(NULL) 28 { 29 if (imageIO == NULL) 30 return; 31 32 // Initialize and translate the image 33 BTranslatorRoster* roster = BTranslatorRoster::Default(); 34 BBitmapStream stream; 35 if (roster->Translate(imageIO, NULL, NULL, &stream, B_TRANSLATOR_BITMAP) 36 != B_OK) { 37 return; 38 } 39 stream.DetachBitmap(&fImage); 40 } 41 42 43 ImageView::~ImageView() 44 { 45 delete fImage; 46 } 47 48 49 void 50 ImageView::AttachedToWindow() 51 { 52 if (fImage == NULL) { 53 ResizeTo(75, 75); 54 return; 55 } 56 57 // We need to resize the view depending on what size has the screen and 58 // the image we will be viewing 59 BScreen screen(Window()); 60 BRect frame = screen.Frame(); 61 BRect image = fImage->Bounds(); 62 63 if (image.Width() > (frame.Width() - 100.0f)) 64 image.right = frame.Width() - 100.0f; 65 if (image.Height() > (frame.Height() - 100.0f)) 66 image.bottom = frame.Height() - 100.f; 67 68 ResizeTo(image.Width(), image.Height()); 69 } 70 71 72 void 73 ImageView::Draw(BRect updateRect) 74 { 75 if (fImage != NULL) 76 DrawBitmapAsync(fImage, Bounds()); 77 else { 78 const char* message = B_TRANSLATE("Image not loaded correctly"); 79 float width = StringWidth(message); 80 DrawString(message, BPoint((Bounds().Width() - width) / 2.0f, 30.0f)); 81 } 82 } 83 84 85 void 86 ImageView::MouseUp(BPoint point) 87 { 88 BWindow* window = Window(); 89 if (window != NULL) 90 window->PostMessage(B_QUIT_REQUESTED); 91 } 92 93 94 // #pragma mark - 95 96 97 PackageImageViewer::PackageImageViewer(BPositionIO* imageIO) 98 : 99 BlockingWindow(BRect(100, 100, 100, 100), "") 100 { 101 fBackground = new ImageView(imageIO); 102 AddChild(fBackground); 103 104 ResizeTo(fBackground->Bounds().Width(), fBackground->Bounds().Height()); 105 106 BScreen screen(this); 107 BRect frame = screen.Frame(); 108 MoveTo((frame.Width() - Bounds().Width()) / 2.0f, 109 (frame.Height() - Bounds().Height()) / 2.0f); 110 } 111 112