Screen: Rework AlertView to just use BAlert.

Fixes #12330.
This commit is contained in:
Augustin Cavalier
2015-08-26 14:42:14 -04:00
parent 1ea54e567e
commit 41f43d568f
6 changed files with 105 additions and 260 deletions
-176
View File
@@ -1,176 +0,0 @@
/*
* Copyright 2001-2008, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Rafael Romo
* Stefano Ceccherini ([email protected])
* Axel Dörfler, [email protected]
*/
#include "AlertView.h"
#include "Constants.h"
#include <Window.h>
#include <Bitmap.h>
#include <Button.h>
#include <Catalog.h>
#include <StringView.h>
#include <String.h>
#include <TimeUnitFormat.h>
#include <IconUtils.h>
#include <FindDirectory.h>
#include <Resources.h>
#include <File.h>
#include <Path.h>
#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;
}
-39
View File
@@ -1,39 +0,0 @@
/*
* Copyright 2001-2005, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Rafael Romo
* Stefano Ceccherini ([email protected])
* Axel Dörfler, [email protected]
*/
#ifndef ALERT_VIEW_H
#define ALERT_VIEW_H
#include <String.h>
#include <View.h>
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 */
+83 -26
View File
@@ -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 ([email protected])
* Axel Dörfler, [email protected]
* Augustin Cavalier <waddlesplash>
*/
#include "AlertWindow.h"
#include "AlertView.h"
#include "Constants.h"
#include <Button.h>
#include <Catalog.h>
#include <String.h>
#include <TextView.h>
#include <Window.h>
#include <Screen.h>
#include <TimeUnitFormat.h>
#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);
}
+17 -12
View File
@@ -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 ([email protected])
* Axel Dörfler, [email protected]
* Augustin Cavalier <waddlesplash>
*/
#ifndef ALERT_WINDOW_H
#define ALERT_WINDOW_H
#include <Window.h>
#include <Alert.h>
#include <Font.h>
#include <Messenger.h>
#include <String.h>
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 */
+1 -3
View File
@@ -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
+4 -4
View File
@@ -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),