101dfbc3aSAxel Dörfler/*
241f43d56SAugustin Cavalier * Copyright 2001-2015, Haiku.
301dfbc3aSAxel Dörfler * Distributed under the terms of the MIT License.
401dfbc3aSAxel Dörfler *
501dfbc3aSAxel Dörfler * Authors:
601dfbc3aSAxel Dörfler *		Rafael Romo
701dfbc3aSAxel Dörfler *		Stefano Ceccherini (burton666@libero.it)
801dfbc3aSAxel Dörfler *		Axel D��rfler, axeld@pinc-software.de
941f43d56SAugustin Cavalier *		Augustin Cavalier <waddlesplash>
1001dfbc3aSAxel Dörfler */
1152a38012Sejakowatz
1241f43d56SAugustin Cavalier
1352a38012Sejakowatz#include "AlertWindow.h"
1452a38012Sejakowatz#include "Constants.h"
1552a38012Sejakowatz
1641f43d56SAugustin Cavalier#include <Button.h>
17c9e8f97aSAdrien Destugues#include <Catalog.h>
1841f43d56SAugustin Cavalier#include <String.h>
1941f43d56SAugustin Cavalier#include <TextView.h>
2001dfbc3aSAxel Dörfler#include <Window.h>
2141f43d56SAugustin Cavalier#include <TimeUnitFormat.h>
2252a38012Sejakowatz
23a4cd2d3fSStefano Ceccherini
24546208a5SOliver Tappe#undef B_TRANSLATION_CONTEXT
25546208a5SOliver Tappe#define B_TRANSLATION_CONTEXT "Screen"
26c9e8f97aSAdrien Destugues
27c9e8f97aSAdrien Destugues
2841f43d56SAugustin CavalierAlertWindow::AlertWindow(BMessenger handler)
2941f43d56SAugustin Cavalier	: BAlert(B_TRANSLATE("Confirm changes"),
3041f43d56SAugustin Cavalier			 "", B_TRANSLATE("Undo"), B_TRANSLATE("Keep")),
3141f43d56SAugustin Cavalier	// we will wait 12 seconds until we send a message
3241f43d56SAugustin Cavalier	fSeconds(12),
3341f43d56SAugustin Cavalier	fHandler(handler)
3452a38012Sejakowatz{
3541f43d56SAugustin Cavalier	SetType(B_WARNING_ALERT);
3641f43d56SAugustin Cavalier	SetPulseRate(1000000);
3741f43d56SAugustin Cavalier	TextView()->SetStylable(true);
3841f43d56SAugustin Cavalier	TextView()->GetFontAndColor(0, &fOriginalFont);
3941f43d56SAugustin Cavalier	fFont = fOriginalFont;
4041f43d56SAugustin Cavalier	fFont.SetFace(B_BOLD_FACE);
4141f43d56SAugustin Cavalier	UpdateCountdownView();
4241f43d56SAugustin Cavalier}
4301dfbc3aSAxel Dörfler
44223706b2SAxel Dörfler
4541f43d56SAugustin Cavaliervoid
4641f43d56SAugustin CavalierAlertWindow::DispatchMessage(BMessage* message, BHandler* handler)
4741f43d56SAugustin Cavalier{
4841f43d56SAugustin Cavalier	if (message->what == B_PULSE) {
4941f43d56SAugustin Cavalier		if (--fSeconds == 0) {
5041f43d56SAugustin Cavalier			fHandler.SendMessage(BUTTON_UNDO_MSG);
5141f43d56SAugustin Cavalier			PostMessage(B_QUIT_REQUESTED);
5241f43d56SAugustin Cavalier			Hide();
5341f43d56SAugustin Cavalier		} else
5441f43d56SAugustin Cavalier			UpdateCountdownView();
5541f43d56SAugustin Cavalier	}
5641f43d56SAugustin Cavalier
5741f43d56SAugustin Cavalier	BAlert::DispatchMessage(message, handler);
5852a38012Sejakowatz}
5952a38012Sejakowatz
60a4cd2d3fSStefano Ceccherini
61a4cd2d3fSStefano Ceccherinivoid
6241f43d56SAugustin CavalierAlertWindow::MessageReceived(BMessage* message)
6352a38012Sejakowatz{
6401dfbc3aSAxel Dörfler	switch (message->what) {
6541f43d56SAugustin Cavalier		case 'ALTB': // alert button message
6641f43d56SAugustin Cavalier		{
6741f43d56SAugustin Cavalier			int32 which;
6841f43d56SAugustin Cavalier			if (message->FindInt32("which", &which) == B_OK) {
6941f43d56SAugustin Cavalier				if (which == 1)
7041f43d56SAugustin Cavalier					fHandler.SendMessage(MAKE_INITIAL_MSG);
7141f43d56SAugustin Cavalier				else if (which == 0)
7241f43d56SAugustin Cavalier					fHandler.SendMessage(BUTTON_UNDO_MSG);
7341f43d56SAugustin Cavalier				PostMessage(B_QUIT_REQUESTED);
7441f43d56SAugustin Cavalier				Hide();
7541f43d56SAugustin Cavalier			}
7652a38012Sejakowatz			break;
7741f43d56SAugustin Cavalier		}
7801dfbc3aSAxel Dörfler
7941f43d56SAugustin Cavalier		case B_KEY_DOWN:
8041f43d56SAugustin Cavalier		{
8141f43d56SAugustin Cavalier			int8 val;
8241f43d56SAugustin Cavalier			if (message->FindInt8("byte", &val) == B_OK && val == B_ESCAPE) {
8341f43d56SAugustin Cavalier				fHandler.SendMessage(BUTTON_UNDO_MSG);
8441f43d56SAugustin Cavalier				PostMessage(B_QUIT_REQUESTED);
8541f43d56SAugustin Cavalier				Hide();
8641f43d56SAugustin Cavalier				break;
8741f43d56SAugustin Cavalier			}
8841f43d56SAugustin Cavalier			// fall through
8941f43d56SAugustin Cavalier		}
9001dfbc3aSAxel Dörfler
9152a38012Sejakowatz		default:
9241f43d56SAugustin Cavalier			BAlert::MessageReceived(message);
9352a38012Sejakowatz			break;
9452a38012Sejakowatz	}
9552a38012Sejakowatz}
9641f43d56SAugustin Cavalier
9741f43d56SAugustin Cavalier
9841f43d56SAugustin Cavaliervoid
9941f43d56SAugustin CavalierAlertWindow::UpdateCountdownView()
10041f43d56SAugustin Cavalier{
10141f43d56SAugustin Cavalier	BString str1 = B_TRANSLATE("Do you wish to keep these settings?");
10241f43d56SAugustin Cavalier	BString string = str1;
10341f43d56SAugustin Cavalier	string += "\n";
10441f43d56SAugustin Cavalier	string += B_TRANSLATE("Settings will revert in %seconds.");
10541f43d56SAugustin Cavalier
10641f43d56SAugustin Cavalier	BTimeUnitFormat format;
10741f43d56SAugustin Cavalier	BString tmp;
10841f43d56SAugustin Cavalier	format.Format(tmp, fSeconds, B_TIME_UNIT_SECOND);
10941f43d56SAugustin Cavalier
11041f43d56SAugustin Cavalier	string.ReplaceFirst("%seconds", tmp);
11141f43d56SAugustin Cavalier	// The below is black magic, do not touch. We really need to refactor
11241f43d56SAugustin Cavalier	// BTextView sometime...
11341f43d56SAugustin Cavalier	TextView()->SetFontAndColor(0, str1.Length() + 1, &fOriginalFont,
11441f43d56SAugustin Cavalier		B_FONT_ALL);
11541f43d56SAugustin Cavalier	TextView()->SetText(string.String());
11641f43d56SAugustin Cavalier	TextView()->SetFontAndColor(0, str1.Length(), &fFont, B_FONT_ALL);
11741f43d56SAugustin Cavalier}
118