From 79350dde31b8053910b2a33772d7095695f548af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Tue, 11 Feb 2014 22:25:42 +0100 Subject: [PATCH] PackageInstaller: Yet more cleanup and code simplifications --- .../packageinstaller/PackageImageViewer.cpp | 75 ++++++++++--------- .../packageinstaller/PackageImageViewer.h | 41 +++++----- 2 files changed, 58 insertions(+), 58 deletions(-) diff --git a/src/apps/packageinstaller/PackageImageViewer.cpp b/src/apps/packageinstaller/PackageImageViewer.cpp index ea3e5124f2..e204832c91 100644 --- a/src/apps/packageinstaller/PackageImageViewer.cpp +++ b/src/apps/packageinstaller/PackageImageViewer.cpp @@ -25,21 +25,19 @@ enum { }; -ImageView::ImageView(BPositionIO *image) +ImageView::ImageView(BPositionIO* imageIO) : BView(BRect(0, 0, 1, 1), "image_view", B_FOLLOW_NONE, B_WILL_DRAW), - fSuccess(true) + fImage(NULL) { - if (!image) { - fSuccess = false; + if (imageIO == NULL) return; - } + // Initialize and translate the image - BTranslatorRoster *roster = BTranslatorRoster::Default(); + BTranslatorRoster* roster = BTranslatorRoster::Default(); BBitmapStream stream; - if (roster->Translate(image, NULL, NULL, &stream, B_TRANSLATOR_BITMAP) - < B_OK) { - fSuccess = false; + if (roster->Translate(imageIO, NULL, NULL, &stream, B_TRANSLATOR_BITMAP) + != B_OK) { return; } stream.DetachBitmap(&fImage); @@ -48,13 +46,14 @@ ImageView::ImageView(BPositionIO *image) ImageView::~ImageView() { + delete fImage; } void ImageView::AttachedToWindow() { - if (!fSuccess) { + if (fImage == NULL) { ResizeTo(75, 75); return; } @@ -77,12 +76,12 @@ ImageView::AttachedToWindow() void ImageView::Draw(BRect updateRect) { - if (fSuccess) + if (fImage != NULL) DrawBitmapAsync(fImage, Bounds()); else { - float length = StringWidth(B_TRANSLATE("Image not loaded correctly")); - DrawString(B_TRANSLATE("Image not loaded correctly"), - BPoint((Bounds().Width() - length) / 2.0f, 30.0f)); + const char* message = B_TRANSLATE("Image not loaded correctly"); + float width = StringWidth(message); + DrawString(message, BPoint((Bounds().Width() - width) / 2.0f, 30.0f)); } } @@ -90,7 +89,7 @@ ImageView::Draw(BRect updateRect) void ImageView::MouseUp(BPoint point) { - BWindow *parent = Window(); + BWindow* parent = Window(); if (parent) parent->PostMessage(P_MSG_CLOSE); } @@ -99,12 +98,12 @@ ImageView::MouseUp(BPoint point) // #pragma mark - -PackageImageViewer::PackageImageViewer(BPositionIO *image) +PackageImageViewer::PackageImageViewer(BPositionIO* imageIO) : BWindow(BRect(100, 100, 100, 100), "", B_MODAL_WINDOW, B_NOT_ZOOMABLE | B_NOT_RESIZABLE | B_NOT_CLOSABLE) { - fBackground = new ImageView(image); + fBackground = new ImageView(imageIO); AddChild(fBackground); ResizeTo(fBackground->Bounds().Width(), fBackground->Bounds().Height()); @@ -112,7 +111,7 @@ PackageImageViewer::PackageImageViewer(BPositionIO *image) BScreen screen(this); BRect frame = screen.Frame(); MoveTo((frame.Width() - Bounds().Width()) / 2.0f, - (frame.Height() - Bounds().Height()) / 2.0f); + (frame.Height() - Bounds().Height()) / 2.0f); } @@ -121,6 +120,19 @@ PackageImageViewer::~PackageImageViewer() } +void +PackageImageViewer::MessageReceived(BMessage* message) +{ + if (message->what == P_MSG_CLOSE) { + if (fSemaphore >= B_OK) { + delete_sem(fSemaphore); + fSemaphore = -1; + } + } else + BWindow::MessageReceived(message); +} + + void PackageImageViewer::Go() { @@ -133,11 +145,14 @@ PackageImageViewer::Go() return; } - BWindow *parent = - dynamic_cast(BLooper::LooperForThread(find_thread(NULL))); + thread_id callingThread = find_thread(NULL); + BWindow* window = dynamic_cast(BLooper::LooperForThread( + callingThread)); Show(); - if (parent) { + if (window != NULL) { + // Make sure calling window thread, which is blocked here, is updating + // the window from time to time. status_t ret; for (;;) { do { @@ -146,10 +161,9 @@ PackageImageViewer::Go() if (ret == B_BAD_SEM_ID) break; - parent->UpdateIfNeeded(); + window->UpdateIfNeeded(); } - } - else { + } else { // Since there are no spinlocks, wait until the semaphore is free while (acquire_sem(fSemaphore) == B_INTERRUPTED) { } @@ -159,16 +173,3 @@ PackageImageViewer::Go() Quit(); } - -void -PackageImageViewer::MessageReceived(BMessage *msg) -{ - if (msg->what == P_MSG_CLOSE) { - if (fSemaphore >= B_OK) { - delete_sem(fSemaphore); - fSemaphore = -1; - } - } else - BWindow::MessageReceived(msg); -} - diff --git a/src/apps/packageinstaller/PackageImageViewer.h b/src/apps/packageinstaller/PackageImageViewer.h index ec6aa694cf..9230ff9e5d 100644 --- a/src/apps/packageinstaller/PackageImageViewer.h +++ b/src/apps/packageinstaller/PackageImageViewer.h @@ -5,8 +5,8 @@ * Author: * Ɓukasz 'Sil2100' Zemczak */ -#ifndef PACKAGEIMAGEVIEWER_H -#define PACKAGEIMAGEVIEWER_H +#ifndef PACKAGE_IMAGE_VIEWER_H +#define PACKAGE_IMAGE_VIEWER_H #include #include @@ -16,35 +16,34 @@ class ImageView : public BView { - public: - ImageView(BPositionIO *image); - ~ImageView(); +public: + ImageView(BPositionIO* image); + virtual ~ImageView(); - void AttachedToWindow(); - void Draw(BRect updateRect); - void MouseUp(BPoint point); + virtual void AttachedToWindow(); + virtual void Draw(BRect updateRect); + virtual void MouseUp(BPoint point); - private: - BBitmap *fImage; - bool fSuccess; +private: + BBitmap* fImage; }; class PackageImageViewer : public BWindow { - public: - PackageImageViewer(BPositionIO *image); - ~PackageImageViewer(); +public: + PackageImageViewer(BPositionIO* image); + virtual ~PackageImageViewer(); - void Go(); - - void MessageReceived(BMessage *msg); + virtual void MessageReceived(BMessage* message); - private: - ImageView *fBackground; + void Go(); - sem_id fSemaphore; +private: + ImageView* fBackground; + + sem_id fSemaphore; }; -#endif +#endif // PACKAGE_IMAGE_VIEWER_H