From 41f43d568fa1f2af68bd3209850cb0b349b49ff4 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Wed, 26 Aug 2015 14:36:59 -0400 Subject: [PATCH] Screen: Rework AlertView to just use BAlert. Fixes #12330. --- src/preferences/screen/AlertView.cpp | 176 ------------------------ src/preferences/screen/AlertView.h | 39 ------ src/preferences/screen/AlertWindow.cpp | 109 +++++++++++---- src/preferences/screen/AlertWindow.h | 29 ++-- src/preferences/screen/Jamfile | 4 +- src/preferences/screen/ScreenWindow.cpp | 8 +- 6 files changed, 105 insertions(+), 260 deletions(-) delete mode 100644 src/preferences/screen/AlertView.cpp delete mode 100644 src/preferences/screen/AlertView.h diff --git a/src/preferences/screen/AlertView.cpp b/src/preferences/screen/AlertView.cpp deleted file mode 100644 index 7d50749b85..0000000000 --- a/src/preferences/screen/AlertView.cpp +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright 2001-2008, Haiku. - * Distributed under the terms of the MIT License. - * - * Authors: - * Rafael Romo - * Stefano Ceccherini (burton666@libero.it) - * Axel Dörfler, axeld@pinc-software.de - */ - - -#include "AlertView.h" -#include "Constants.h" - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - - -#undef B_TRANSLATION_CONTEXT -#define B_TRANSLATION_CONTEXT "Screen" - - -AlertView::AlertView(BRect frame, const char *name) - : BView(frame, name, B_FOLLOW_ALL, B_WILL_DRAW | B_PULSE_NEEDED), - // we will wait 12 seconds until we send a message - fSeconds(12) -{ - SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); - fBitmap = InitIcon(); - - BRect rect(60, 8, 400, 36); - BStringView *stringView = new BStringView(rect, NULL, - B_TRANSLATE("Do you wish to keep these settings?")); - stringView->SetFont(be_bold_font); - stringView->ResizeToPreferred(); - AddChild(stringView); - - rect = stringView->Frame(); - rect.OffsetBy(0, rect.Height()); - fCountdownView = new BStringView(rect, "countdown", NULL); - UpdateCountdownView(); - fCountdownView->ResizeToPreferred(); - AddChild(fCountdownView); - - BButton* keepButton = new BButton(rect, "keep", B_TRANSLATE("Keep"), - new BMessage(BUTTON_KEEP_MSG), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); - keepButton->ResizeToPreferred(); - AddChild(keepButton); - - BButton* button = new BButton(rect, "undo", B_TRANSLATE("Undo"), - new BMessage(BUTTON_UNDO_MSG), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); - button->ResizeToPreferred(); - AddChild(button); - - // we're resizing ourselves to the right size - // (but we're not implementing GetPreferredSize(), bad style!) - float width = stringView->Frame().right; - if (fCountdownView->Frame().right > width) - width = fCountdownView->Frame().right; - if (width < Bounds().Width()) - width = Bounds().Width(); - - float height - = fCountdownView->Frame().bottom + 24 + button->Bounds().Height(); - ResizeTo(width, height); - - keepButton->MoveTo(Bounds().Width() - 8 - keepButton->Bounds().Width(), - Bounds().Height() - 8 - keepButton->Bounds().Height()); - button->MoveTo(keepButton->Frame().left - button->Bounds().Width() - 8, - keepButton->Frame().top); - - keepButton->MakeDefault(true); -} - - -void -AlertView::AttachedToWindow() -{ - // the view displays a decrementing counter - // (until the user must take action) - Window()->SetPulseRate(1000000); - // every second - - SetEventMask(B_KEYBOARD_EVENTS); -} - - -void -AlertView::Draw(BRect updateRect) -{ - rgb_color dark = tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), - B_DARKEN_1_TINT); - SetHighColor(dark); - - FillRect(BRect(0.0, 0.0, 30.0, Bounds().bottom)); - - if (fBitmap != NULL) { - SetDrawingMode(B_OP_ALPHA); - DrawBitmap(fBitmap, BPoint(18.0, 6.0)); - SetDrawingMode(B_OP_COPY); - } -} - - -void -AlertView::Pulse() -{ - if (--fSeconds == 0) - Window()->PostMessage(BUTTON_UNDO_MSG); - else - UpdateCountdownView(); -} - - -void -AlertView::KeyDown(const char* bytes, int32 numBytes) -{ - if (numBytes == 1 && bytes[0] == B_ESCAPE) - Window()->PostMessage(BUTTON_UNDO_MSG); -} - - -void -AlertView::UpdateCountdownView() -{ - BString string; - string = B_TRANSLATE("Settings will revert in %seconds."); - - BTimeUnitFormat format; - BString tmp; - format.Format(tmp, fSeconds, B_TIME_UNIT_SECOND); - - string.ReplaceFirst("%seconds", tmp); - fCountdownView->SetText(string.String()); -} - - -BBitmap* -AlertView::InitIcon() -{ - // This is how BAlert gets to its icon - BBitmap* icon = NULL; - BPath path; - if (find_directory(B_BEOS_SERVERS_DIRECTORY, &path) == B_OK) { - path.Append("app_server"); - BResources resources; - BFile file; - if (file.SetTo(path.Path(), B_READ_ONLY) == B_OK - && resources.SetTo(&file) == B_OK) { - size_t size; - const void* data = resources.LoadResource(B_VECTOR_ICON_TYPE, - "warn", &size); - if (data) { - icon = new BBitmap(BRect(0, 0, 31, 31), 0, B_RGBA32); - if (BIconUtils::GetVectorIcon((const uint8*)data, size, icon) - != B_OK) { - delete icon; - icon = NULL; - } - } - } - } - - return icon; -} diff --git a/src/preferences/screen/AlertView.h b/src/preferences/screen/AlertView.h deleted file mode 100644 index 66432b3c42..0000000000 --- a/src/preferences/screen/AlertView.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2001-2005, Haiku. - * Distributed under the terms of the MIT License. - * - * Authors: - * Rafael Romo - * Stefano Ceccherini (burton666@libero.it) - * Axel Dörfler, axeld@pinc-software.de - */ -#ifndef ALERT_VIEW_H -#define ALERT_VIEW_H - - -#include -#include - -class BBitmap; -class BStringView; - - -class AlertView : public BView { - public: - AlertView(BRect frame, const char* name); - - virtual void AttachedToWindow(); - virtual void Draw(BRect updateRect); - virtual void Pulse(); - virtual void KeyDown(const char* bytes, int32 numBytes); - - private: - void UpdateCountdownView(); - BBitmap* InitIcon(); - - BStringView* fCountdownView; - BBitmap* fBitmap; - int32 fSeconds; -}; - -#endif /* ALERT_VIEW_H */ diff --git a/src/preferences/screen/AlertWindow.cpp b/src/preferences/screen/AlertWindow.cpp index 51943fd429..8d9ec10469 100644 --- a/src/preferences/screen/AlertWindow.cpp +++ b/src/preferences/screen/AlertWindow.cpp @@ -1,60 +1,117 @@ /* - * Copyright 2001-2006, Haiku. + * Copyright 2001-2015, Haiku. * Distributed under the terms of the MIT License. * * Authors: * Rafael Romo * Stefano Ceccherini (burton666@libero.it) * Axel Dörfler, axeld@pinc-software.de + * Augustin Cavalier */ + #include "AlertWindow.h" -#include "AlertView.h" #include "Constants.h" +#include #include +#include +#include #include -#include +#include #undef B_TRANSLATION_CONTEXT #define B_TRANSLATION_CONTEXT "Screen" -AlertWindow::AlertWindow(BMessenger target) - : BWindow(BRect(100.0, 100.0, 400.0, 193.0), B_TRANSLATE("Undo"), - B_MODAL_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL, - B_NOT_RESIZABLE | B_NOT_ZOOMABLE, B_ALL_WORKSPACES), - fTarget(target) +AlertWindow::AlertWindow(BMessenger handler) + : BAlert(B_TRANSLATE("Confirm changes"), + "", B_TRANSLATE("Undo"), B_TRANSLATE("Keep")), + // we will wait 12 seconds until we send a message + fSeconds(12), + fHandler(handler) { - fAlertView = new AlertView(Bounds(), "AlertView"); - - ResizeTo(fAlertView->Bounds().Width(), fAlertView->Bounds().Height()); - AddChild(fAlertView); - - // center window on screen - BScreen screen(this); - MoveTo(screen.Frame().left + (screen.Frame().Width() - Frame().Width()) / 2, - screen.Frame().top + (screen.Frame().Height() - Frame().Height()) / 2); + SetType(B_WARNING_ALERT); + SetPulseRate(1000000); + TextView()->SetStylable(true); + TextView()->GetFontAndColor(0, &fOriginalFont); + fFont = fOriginalFont; + fFont.SetFace(B_BOLD_FACE); + UpdateCountdownView(); } void -AlertWindow::MessageReceived(BMessage *message) +AlertWindow::DispatchMessage(BMessage* message, BHandler* handler) +{ + if (message->what == B_PULSE) { + if (--fSeconds == 0) { + fHandler.SendMessage(BUTTON_UNDO_MSG); + PostMessage(B_QUIT_REQUESTED); + Hide(); + } else + UpdateCountdownView(); + } + + BAlert::DispatchMessage(message, handler); +} + + +void +AlertWindow::MessageReceived(BMessage* message) { switch (message->what) { - case BUTTON_KEEP_MSG: - fTarget.SendMessage(MAKE_INITIAL_MSG); - PostMessage(B_QUIT_REQUESTED); + case 'ALTB': // alert button message + { + int32 which; + if (message->FindInt32("which", &which) == B_OK) { + if (which == 1) + fHandler.SendMessage(MAKE_INITIAL_MSG); + else if (which == 0) + fHandler.SendMessage(BUTTON_UNDO_MSG); + PostMessage(B_QUIT_REQUESTED); + Hide(); + } break; + } - case BUTTON_UNDO_MSG: - fTarget.SendMessage(BUTTON_UNDO_MSG); - PostMessage(B_QUIT_REQUESTED); - break; + case B_KEY_DOWN: + { + int8 val; + if (message->FindInt8("byte", &val) == B_OK && val == B_ESCAPE) { + fHandler.SendMessage(BUTTON_UNDO_MSG); + PostMessage(B_QUIT_REQUESTED); + Hide(); + break; + } + // fall through + } default: - BWindow::MessageReceived(message); + BAlert::MessageReceived(message); break; } } + + +void +AlertWindow::UpdateCountdownView() +{ + BString str1 = B_TRANSLATE("Do you wish to keep these settings?"); + BString string = str1; + string += "\n"; + string += B_TRANSLATE("Settings will revert in %seconds."); + + BTimeUnitFormat format; + BString tmp; + format.Format(tmp, fSeconds, B_TIME_UNIT_SECOND); + + string.ReplaceFirst("%seconds", tmp); + // The below is black magic, do not touch. We really need to refactor + // BTextView sometime... + TextView()->SetFontAndColor(0, str1.Length() + 1, &fOriginalFont, + B_FONT_ALL); + TextView()->SetText(string.String()); + TextView()->SetFontAndColor(0, str1.Length(), &fFont, B_FONT_ALL); +} diff --git a/src/preferences/screen/AlertWindow.h b/src/preferences/screen/AlertWindow.h index d216757073..48a4d00282 100644 --- a/src/preferences/screen/AlertWindow.h +++ b/src/preferences/screen/AlertWindow.h @@ -1,34 +1,39 @@ /* - * Copyright 2001-2005, Haiku. + * Copyright 2001-2015, Haiku. * Distributed under the terms of the MIT License. * * Authors: * Rafael Romo * Stefano Ceccherini (burton666@libero.it) * Axel Dörfler, axeld@pinc-software.de + * Augustin Cavalier */ #ifndef ALERT_WINDOW_H #define ALERT_WINDOW_H -#include +#include +#include #include +#include + +class BWindow; -class BMessageRunner; -class BButton; -class AlertView; - - -class AlertWindow : public BWindow { +class AlertWindow : public BAlert { public: - AlertWindow(BMessenger target); + AlertWindow(BMessenger handler); - virtual void MessageReceived(BMessage *message); + virtual void MessageReceived(BMessage* message); + virtual void DispatchMessage(BMessage* message, BHandler* handler); private: - BMessenger fTarget; - AlertView* fAlertView; + void UpdateCountdownView(); + + int32 fSeconds; + BMessenger fHandler; + BFont fOriginalFont; + BFont fFont; }; #endif /* ALERT_WINDOW_H */ diff --git a/src/preferences/screen/Jamfile b/src/preferences/screen/Jamfile index d2370cd486..ba8bc57e53 100644 --- a/src/preferences/screen/Jamfile +++ b/src/preferences/screen/Jamfile @@ -1,14 +1,13 @@ SubDir HAIKU_TOP src preferences screen ; -SetSubDirSupportedPlatformsBeOSCompatible ; AddSubDirSupportedPlatforms libbe_test ; UsePrivateHeaders [ FDirName graphics common ] ; UsePrivateHeaders [ FDirName graphics radeon ] ; UsePrivateHeaders interface ; +SubDirC++Flags -O0 -g ; Preference Screen : - AlertView.cpp AlertWindow.cpp MonitorView.cpp multimon.cpp @@ -31,7 +30,6 @@ if $(TARGET_PLATFORM) = libbe_test { DoCatalogs Screen : x-vnd.Haiku-Screen : - AlertView.cpp AlertWindow.cpp RefreshSlider.cpp RefreshWindow.cpp diff --git a/src/preferences/screen/ScreenWindow.cpp b/src/preferences/screen/ScreenWindow.cpp index 3ccc5a3890..29f38ddb39 100644 --- a/src/preferences/screen/ScreenWindow.cpp +++ b/src/preferences/screen/ScreenWindow.cpp @@ -1185,7 +1185,7 @@ void ScreenWindow::_CheckApplyEnabled() { bool applyEnabled = true; - + if (fSelected == fActive) { applyEnabled = false; if (fAllWorkspacesItem->IsMarked()) { @@ -1200,7 +1200,7 @@ ScreenWindow::_CheckApplyEnabled() } } } - + fApplyButton->SetEnabled(applyEnabled); uint32 columns; @@ -1336,8 +1336,8 @@ ScreenWindow::_Apply() fActive = fSelected; // TODO: only show alert when this is an unknown mode - BWindow* window = new AlertWindow(this); - window->Show(); + BAlert* window = new AlertWindow(this); + window->Go(NULL); } else { char message[256]; snprintf(message, sizeof(message),