From 4aa636331fa38f0756b862567fe93e321de6e234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sun, 9 Jan 2011 14:58:48 +0000 Subject: [PATCH] * Removed the slide show code from ShowImageView, and reimplemented them in ShowImageWindow using a BMessageRunner. * Note, this is completely untested, as it turns out my Haiku installation on this box is too old to run it. It compiles, at least, and shouldn't break anything else. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40178 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/showimage/ShowImageSettings.cpp | 52 ++++++-- src/apps/showimage/ShowImageSettings.h | 56 ++++---- src/apps/showimage/ShowImageView.cpp | 68 ++-------- src/apps/showimage/ShowImageView.h | 16 +-- src/apps/showimage/ShowImageWindow.cpp | 160 +++++++++++++++-------- src/apps/showimage/ShowImageWindow.h | 17 ++- 6 files changed, 209 insertions(+), 160 deletions(-) diff --git a/src/apps/showimage/ShowImageSettings.cpp b/src/apps/showimage/ShowImageSettings.cpp index a36832a402..cc55565dff 100644 --- a/src/apps/showimage/ShowImageSettings.cpp +++ b/src/apps/showimage/ShowImageSettings.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010 Haiku Inc. All rights reserved. + * Copyright 2003-2011 Haiku Inc. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -15,15 +15,20 @@ ShowImageSettings::ShowImageSettings() + : + fLock("settings lock"), + fUpdated(false) { - Load(); + _Load(); } ShowImageSettings::~ShowImageSettings() { if (Lock()) { - Save(); + if (fUpdated) + _Save(); + Unlock(); } } @@ -87,6 +92,17 @@ ShowImageSettings::GetRect(const char* name, BRect defaultValue) } +bigtime_t +ShowImageSettings::GetTime(const char* name, bigtime_t defaultValue) +{ + int64 value; + if (fSettings.FindInt64(name, &value) == B_OK) + return value; + + return defaultValue; +} + + const char* ShowImageSettings::GetString(const char* name, const char* defaultValue) { @@ -105,6 +121,8 @@ ShowImageSettings::SetBool(const char* name, bool value) fSettings.ReplaceBool(name, value); else fSettings.AddBool(name, value); + + fUpdated = true; } @@ -115,6 +133,8 @@ ShowImageSettings::SetInt32(const char* name, int32 value) fSettings.ReplaceInt32(name, value); else fSettings.AddInt32(name, value); + + fUpdated = true; } @@ -125,6 +145,8 @@ ShowImageSettings::SetFloat(const char* name, float value) fSettings.ReplaceFloat(name, value); else fSettings.AddFloat(name, value); + + fUpdated = true; } @@ -135,6 +157,18 @@ ShowImageSettings::SetRect(const char* name, BRect value) fSettings.ReplaceRect(name, value); else fSettings.AddRect(name, value); + + fUpdated = true; +} + + +void +ShowImageSettings::SetTime(const char* name, bigtime_t value) +{ + if (fSettings.ReplaceInt64(name, value) != B_OK) + fSettings.AddInt64(name, value); + + fUpdated = true; } @@ -145,11 +179,13 @@ ShowImageSettings::SetString(const char* name, const char* value) fSettings.ReplaceString(name, value); else fSettings.AddString(name, value); + + fUpdated = true; } bool -ShowImageSettings::OpenSettingsFile(BFile* file, bool forReading) +ShowImageSettings::_OpenSettingsFile(BFile* file, bool forReading) { BPath path; status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path); @@ -164,18 +200,18 @@ ShowImageSettings::OpenSettingsFile(BFile* file, bool forReading) void -ShowImageSettings::Load() +ShowImageSettings::_Load() { BFile file; - if (OpenSettingsFile(&file, true)) + if (_OpenSettingsFile(&file, true)) fSettings.Unflatten(&file); } void -ShowImageSettings::Save() +ShowImageSettings::_Save() { BFile file; - if (OpenSettingsFile(&file, false)) + if (_OpenSettingsFile(&file, false)) fSettings.Flatten(&file); } diff --git a/src/apps/showimage/ShowImageSettings.h b/src/apps/showimage/ShowImageSettings.h index 4ed3b4d265..68775d2d99 100644 --- a/src/apps/showimage/ShowImageSettings.h +++ b/src/apps/showimage/ShowImageSettings.h @@ -1,5 +1,5 @@ /* - * Copyright 2003-2009 Haiku Inc. All rights reserved. + * Copyright 2003-2011 Haiku Inc. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -9,36 +9,46 @@ #define SHOW_IMAGE_SETTINGS_H -#include -#include #include +#include + + +class BFile; class ShowImageSettings { public: - ShowImageSettings(); - ~ShowImageSettings(); + ShowImageSettings(); + virtual ~ShowImageSettings(); + + bool Lock(); + void Unlock(); + + bool GetBool(const char* name, bool defaultValue); + int32 GetInt32(const char* name, int32 defaultValue); + float GetFloat(const char* name, float defaultValue); + BRect GetRect(const char* name, BRect defaultValue); + bigtime_t GetTime(const char* name, + bigtime_t defaultValue); + const char* GetString(const char* name, + const char* defaultValue); + + void SetBool(const char* name, bool value); + void SetInt32(const char* name, int32 value); + void SetFloat(const char* name, float value); + void SetRect(const char* name, BRect value); + void SetTime(const char* name, bigtime_t value); + void SetString(const char* name, const char* value); - bool Lock(); - bool GetBool(const char* name, bool defaultValue); - int32 GetInt32(const char* name, int32 defaultValue); - float GetFloat(const char* name, float defaultValue); - BRect GetRect(const char* name, BRect defaultValue); - const char* GetString(const char* name, const char* defaultValue); - void SetBool(const char* name, bool value); - void SetInt32(const char* name, int32 value); - void SetFloat(const char* name, float value); - void SetRect(const char* name, BRect value); - void SetString(const char* name, const char* value); - void Unlock(); - private: - bool OpenSettingsFile(BFile* file, bool forReading); - void Load(); - void Save(); + bool _OpenSettingsFile(BFile* file, bool forReading); + void _Load(); + void _Save(); - BLocker fLock; - BMessage fSettings; +private: + BLocker fLock; + BMessage fSettings; + bool fUpdated; }; diff --git a/src/apps/showimage/ShowImageView.cpp b/src/apps/showimage/ShowImageView.cpp index 6e9924e51a..57ab8c9c2b 100644 --- a/src/apps/showimage/ShowImageView.cpp +++ b/src/apps/showimage/ShowImageView.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010, Haiku, Inc. All Rights Reserved. + * Copyright 2003-2011, Haiku, Inc. All Rights Reserved. * Copyright 2004-2005 yellowTAB GmbH. All Rights Reserverd. * Copyright 2006 Bernd Korz. All Rights Reserved * Distributed under the terms of the MIT License. @@ -192,20 +192,15 @@ ShowImageView::ShowImageView(BRect rect, const char *name, uint32 resizingMode, fSelectionMode(false), fAnimateSelection(true), fHasSelection(false), - fSlideShow(false), - fSlideShowDelay(3 * 10), // 3 seconds - fSlideShowCountDown(0), fShowCaption(false), fShowingPopUpMenu(false), fHideCursorCountDown(HIDE_CURSOR_DELAY_TIME), fIsActiveWin(true) { - ShowImageSettings* settings; - settings = my_app->Settings(); + ShowImageSettings* settings = my_app->Settings(); if (settings->Lock()) { fStretchToBounds = settings->GetBool("StretchToBounds", fStretchToBounds); - fSlideShowDelay = settings->GetInt32("SlideShowDelay", fSlideShowDelay); fScaleBilinear = settings->GetBool("ScaleBilinear", fScaleBilinear); settings->Unlock(); } @@ -237,17 +232,6 @@ ShowImageView::Pulse() fSelectionBox.Animate(); fSelectionBox.Draw(this, Bounds()); } -#if 0 - if (fSlideShow) { - fSlideShowCountDown --; - if (fSlideShowCountDown <= 0) { - fSlideShowCountDown = fSlideShowDelay; - if (!NextFile()) { - _FirstFile(); - } - } - } -#endif if (fHideCursor && !fHasSelection && !fShowingPopUpMenu && fIsActiveWin) { if (fHideCursorCountDown <= 0) @@ -1280,9 +1264,7 @@ ShowImageView::KeyDown(const char* bytes, int32 numBytes) break; case B_ESCAPE: // stop slide show - if (fSlideShow) - _ToggleSlideShow(); - + _StopSlideShow(); _ExitFullScreen(); ClearSelection(); @@ -1642,43 +1624,6 @@ ShowImageView::FitToBounds() } -void -ShowImageView::SetSlideShowDelay(float seconds) -{ - ShowImageSettings* settings; - int32 delay = (int)(seconds * 10.0); - if (fSlideShowDelay != delay) { - // update counter - fSlideShowCountDown = delay - (fSlideShowDelay - fSlideShowCountDown); - if (fSlideShowCountDown <= 0) { - // show next image on next Pulse() - fSlideShowCountDown = 1; - } - fSlideShowDelay = delay; - settings = my_app->Settings(); - if (settings->Lock()) { - settings->SetInt32("SlideShowDelay", fSlideShowDelay); - settings->Unlock(); - } - } -} - - -void -ShowImageView::StartSlideShow() -{ - fSlideShow = true; - fSlideShowCountDown = fSlideShowDelay; -} - - -void -ShowImageView::StopSlideShow() -{ - fSlideShow = false; -} - - void ShowImageView::_DoImageOperation(ImageProcessor::operation op, bool quiet) { @@ -1858,6 +1803,13 @@ ShowImageView::_ToggleSlideShow() } +void +ShowImageView::_StopSlideShow() +{ + _SendMessageToWindow(kMsgStopSlideShow); +} + + void ShowImageView::_ExitFullScreen() { diff --git a/src/apps/showimage/ShowImageView.h b/src/apps/showimage/ShowImageView.h index eff4e24394..ab3a9b1e07 100644 --- a/src/apps/showimage/ShowImageView.h +++ b/src/apps/showimage/ShowImageView.h @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010, Haiku, Inc. All Rights Reserved. + * Copyright 2003-2011, Haiku, Inc. All Rights Reserved. * Copyright 2004-2005 yellowTAB GmbH. All Rights Reserverd. * Copyright 2006 Bernd Korz. All Rights Reserved * Distributed under the terms of the MIT License. @@ -87,13 +87,6 @@ public: void CopySelectionToClipboard(); - void SetSlideShowDelay(float seconds); - float GetSlideShowDelay() const - { return fSlideShowDelay / 10.0; } - bool SlideShowStarted() const { return fSlideShow; } - void StartSlideShow(); - void StopSlideShow(); - void FitToBounds(); void SetZoom(float zoom, BPoint where = BPoint(-1, -1)); @@ -183,6 +176,7 @@ private: void _SettingsSetBool(const char* name, bool value); void _SetIcon(bool clear, icon_size which); void _ToggleSlideShow(); + void _StopSlideShow(); void _ExitFullScreen(); private: @@ -214,12 +208,6 @@ private: // the portion of the background bitmap the selection is made // from - bool fSlideShow; - int fSlideShowDelay; - // in pulse rate units - int fSlideShowCountDown; - // shows next image if it reaches zero - bool fShowCaption; BString fCaption; diff --git a/src/apps/showimage/ShowImageWindow.cpp b/src/apps/showimage/ShowImageWindow.cpp index bf2ed5c271..6d0de39f45 100644 --- a/src/apps/showimage/ShowImageWindow.cpp +++ b/src/apps/showimage/ShowImageWindow.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010, Haiku, Inc. All Rights Reserved. + * Copyright 2003-2011, Haiku, Inc. All Rights Reserved. * Copyright 2004-2005 yellowTAB GmbH. All Rights Reserverd. * Copyright 2006 Bernd Korz. All Rights Reserved * Distributed under the terms of the MIT License. @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -57,6 +58,9 @@ const char* kTypeField = "be:type"; const char* kTranslatorField = "be:translator"; +const bigtime_t kDefaultSlideShowDelay = 3000000; + // 3 seconds + // message constants enum { @@ -88,7 +92,8 @@ enum { MSG_PREPARE_PRINT = 'mPPT', kMsgFitToWindow = 'mFtW', kMsgOriginalSize = 'mOSZ', - kMsgStretchToWindow = 'mStW' + kMsgStretchToWindow = 'mStW', + kMsgNextSlide = 'mNxS' }; @@ -120,14 +125,16 @@ ShowImageWindow::ShowImageWindow(const entry_ref& ref, fBar(NULL), fBrowseMenu(NULL), fGoToPageMenu(NULL), - fSlideShowDelay(NULL), + fSlideShowDelayMenu(NULL), fImageView(NULL), fStatusView(NULL), fProgressWindow(new ProgressWindow()), fModified(false), fFullScreen(false), fShowCaption(true), - fPrintSettings(NULL) + fPrintSettings(NULL), + fSlideShowRunner(NULL), + fSlideShowDelay(kDefaultSlideShowDelay) { _ApplySettings(); @@ -210,6 +217,8 @@ ShowImageWindow::~ShowImageWindow() { fProgressWindow->Lock(); fProgressWindow->Quit(); + + _StopSlideShow(); } @@ -228,25 +237,23 @@ void ShowImageWindow::_BuildViewMenu(BMenu* menu, bool popupMenu) { _AddItemMenu(menu, B_TRANSLATE("Slide show"), MSG_SLIDE_SHOW, 0, 0, this); - _MarkMenuItem(menu, MSG_SLIDE_SHOW, fImageView->SlideShowStarted()); + _MarkMenuItem(menu, MSG_SLIDE_SHOW, fSlideShowRunner != NULL); BMenu* delayMenu = new BMenu(B_TRANSLATE("Slide delay")); - if (fSlideShowDelay == NULL) - fSlideShowDelay = delayMenu; + if (fSlideShowDelayMenu == NULL) + fSlideShowDelayMenu = delayMenu; delayMenu->SetRadioMode(true); - // Note: ShowImage loads images in window thread so it becomes unresponsive - // if slide show delay is too short! (Especially if loading the image - // takes as long as or longer than the slide show delay). Should load - // in background thread! - _AddDelayItem(delayMenu, B_TRANSLATE("3 seconds"), 3); - _AddDelayItem(delayMenu, B_TRANSLATE("4 seconds"), 4); - _AddDelayItem(delayMenu, B_TRANSLATE("5 seconds"), 5); - _AddDelayItem(delayMenu, B_TRANSLATE("6 seconds"), 6); - _AddDelayItem(delayMenu, B_TRANSLATE("7 seconds"), 7); - _AddDelayItem(delayMenu, B_TRANSLATE("8 seconds"), 8); - _AddDelayItem(delayMenu, B_TRANSLATE("9 seconds"), 9); - _AddDelayItem(delayMenu, B_TRANSLATE("10 seconds"), 10); - _AddDelayItem(delayMenu, B_TRANSLATE("20 seconds"), 20); + + int32 kDelays[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 20}; + for (uint32 i = 0; i < sizeof(kDelays) / sizeof(kDelays[0]); i++) { + BString text(B_TRANSLATE_COMMENT("%SECONDS seconds", + "Don't translate %SECONDS")); + char seconds[32]; + snprintf(seconds, sizeof(seconds), "%" B_PRIi32, kDelays[i]); + text.ReplaceFirst("%SECONDS", seconds); + + _AddDelayItem(delayMenu, text.String(), kDelays[i] * 1000000LL); + } menu->AddItem(delayMenu); menu->AddSeparatorItem(); @@ -387,16 +394,15 @@ ShowImageWindow::_AddItemMenu(BMenu* menu, const char* label, uint32 what, BMenuItem* -ShowImageWindow::_AddDelayItem(BMenu* menu, const char* label, float value) +ShowImageWindow::_AddDelayItem(BMenu* menu, const char* label, bigtime_t delay) { BMessage* message = new BMessage(MSG_SLIDE_SHOW_DELAY); - message->AddFloat("value", value); + message->AddInt64("delay", delay); BMenuItem* item = new BMenuItem(label, message, 0); item->SetTarget(this); - bool marked = fImageView->GetSlideShowDelay() == value; - if (marked) + if (delay == fSlideShowDelay) item->SetMarked(true); menu->AddItem(item); @@ -479,16 +485,16 @@ ShowImageWindow::_MarkMenuItem(BMenu* menu, uint32 what, bool marked) void -ShowImageWindow::_MarkSlideShowDelay(float value) +ShowImageWindow::_MarkSlideShowDelay(bigtime_t delay) { - const int32 n = fSlideShowDelay->CountItems(); - float v; - for (int32 i = 0; i < n; i ++) { - BMenuItem* item = fSlideShowDelay->ItemAt(i); - if (item) { - if (item->Message()->FindFloat("value", &v) == B_OK && v == value) { - if (!item->IsMarked()) - item->SetMarked(true); + const int32 count = fSlideShowDelayMenu->CountItems(); + for (int32 i = 0; i < count; i ++) { + BMenuItem* item = fSlideShowDelayMenu->ItemAt(i); + if (item != NULL) { + bigtime_t itemDelay; + if (item->Message()->FindInt64("delay", &itemDelay) == B_OK + && itemDelay == delay) { + item->SetMarked(true); return; } } @@ -760,6 +766,7 @@ ShowImageWindow::MessageReceived(BMessage* message) break; case MSG_FILE_NEXT: + case kMsgNextSlide: if (_ClosePrompt() && fNavigator.NextFile()) _LoadImage(); break; @@ -792,25 +799,36 @@ ShowImageWindow::MessageReceived(BMessage* message) case MSG_SLIDE_SHOW: { BMenuItem* item = fBar->FindItem(message->what); - if (!item) + if (item == NULL) break; + if (item->IsMarked()) { item->SetMarked(false); - fImageView->StopSlideShow(); + _StopSlideShow(); } else if (_ClosePrompt()) { item->SetMarked(true); - fImageView->StartSlideShow(); + _StartSlideShow(); } break; } + case kMsgStopSlideShow: + { + BMenuItem* item = fBar->FindItem(MSG_SLIDE_SHOW); + if (item != NULL) + item->SetMarked(false); + + _StopSlideShow(); + break; + } + case MSG_SLIDE_SHOW_DELAY: { - float value; - if (message->FindFloat("value", &value) == B_OK) { - fImageView->SetSlideShowDelay(value); + bigtime_t delay; + if (message->FindInt64("delay", &delay) == B_OK) { + _SetSlideShowDelay(delay); // in case message is sent from popup menu - _MarkSlideShowDelay(value); + _MarkSlideShowDelay(delay); } break; } @@ -1108,20 +1126,17 @@ ShowImageWindow::_ApplySettings() fShowCaption = settings->GetBool("ShowCaption", fShowCaption); fPrintOptions.SetBounds(BRect(0, 0, 1023, 767)); - int32 op = settings->GetInt32("PO:Option", fPrintOptions.Option()); - fPrintOptions.SetOption((enum PrintOptions::Option)op); + fSlideShowDelay = settings->GetTime("SlideShowDelay", fSlideShowDelay); - float f = settings->GetFloat("PO:ZoomFactor", fPrintOptions.ZoomFactor()); - fPrintOptions.SetZoomFactor(f); - - f = settings->GetFloat("PO:DPI", fPrintOptions.DPI()); - fPrintOptions.SetDPI(f); - - f = settings->GetFloat("PO:Width", fPrintOptions.Width()); - fPrintOptions.SetWidth(f); - - f = settings->GetFloat("PO:Height", fPrintOptions.Height()); - fPrintOptions.SetHeight(f); + fPrintOptions.SetOption((enum PrintOptions::Option)settings->GetInt32( + "PO:Option", fPrintOptions.Option())); + fPrintOptions.SetZoomFactor( + settings->GetFloat("PO:ZoomFactor", fPrintOptions.ZoomFactor())); + fPrintOptions.SetDPI(settings->GetFloat("PO:DPI", fPrintOptions.DPI())); + fPrintOptions.SetWidth( + settings->GetFloat("PO:Width", fPrintOptions.Width())); + fPrintOptions.SetHeight( + settings->GetFloat("PO:Height", fPrintOptions.Height())); settings->Unlock(); } @@ -1249,6 +1264,45 @@ ShowImageWindow::_Print(BMessage* msg) } +void +ShowImageWindow::_SetSlideShowDelay(bigtime_t delay) +{ + if (fSlideShowDelay == delay) + return; + + fSlideShowDelay = delay; + + ShowImageSettings* settings = my_app->Settings(); + if (settings->Lock()) { + settings->SetTime("SlideShowDelay", fSlideShowDelay); + settings->Unlock(); + } + + if (fSlideShowRunner != NULL) + _StartSlideShow(); +} + + +void +ShowImageWindow::_StartSlideShow() +{ + _StopSlideShow(); + + BMessage nextSlide(kMsgNextSlide); + fSlideShowRunner = new BMessageRunner(this, &nextSlide, fSlideShowDelay); +} + + +void +ShowImageWindow::_StopSlideShow() +{ + if (fSlideShowRunner != NULL) { + delete fSlideShowRunner; + fSlideShowRunner = NULL; + } +} + + bool ShowImageWindow::QuitRequested() { diff --git a/src/apps/showimage/ShowImageWindow.h b/src/apps/showimage/ShowImageWindow.h index 6c6987f870..4139584b0d 100644 --- a/src/apps/showimage/ShowImageWindow.h +++ b/src/apps/showimage/ShowImageWindow.h @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010, Haiku, Inc. All Rights Reserved. + * Copyright 2003-2011, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -21,6 +21,7 @@ class BFilePanel; class BMenu; class BMenuBar; class BMenuItem; +class BMessageRunner; class ProgressWindow; class ShowImageView; class ShowImageStatusView; @@ -36,6 +37,7 @@ enum { MSG_FILE_PREV = 'mFLP', kMsgDeleteCurrentFile = 'mDcF', MSG_SLIDE_SHOW = 'mSSW', + kMsgStopSlideShow = 'msss', MSG_EXIT_FULL_SCREEN = 'mEFS' }; @@ -62,14 +64,14 @@ private: const BHandler* target, bool enabled = true); BMenuItem* _AddDelayItem(BMenu* menu, const char* label, - float value); + bigtime_t delay); bool _ToggleMenuItem(uint32 what); void _EnableMenuItem(BMenu* menu, uint32 what, bool enable); void _MarkMenuItem(BMenu* menu, uint32 what, bool marked); - void _MarkSlideShowDelay(float value); + void _MarkSlideShowDelay(bigtime_t delay); void _UpdateStatusText(const BMessage* message); void _LoadError(const entry_ref& ref); @@ -87,13 +89,17 @@ private: void _PrepareForPrint(); void _Print(BMessage* msg); + void _SetSlideShowDelay(bigtime_t delay); + void _StartSlideShow(); + void _StopSlideShow(); + private: ImageFileNavigator fNavigator; BFilePanel* fSavePanel; BMenuBar* fBar; BMenu* fBrowseMenu; BMenu* fGoToPageMenu; - BMenu* fSlideShowDelay; + BMenu* fSlideShowDelayMenu; ShowImageView* fImageView; ShowImageStatusView* fStatusView; ProgressWindow* fProgressWindow; @@ -105,6 +111,9 @@ private: PrintOptions fPrintOptions; BString fImageType; + + BMessageRunner* fSlideShowRunner; + bigtime_t fSlideShowDelay; };