xref: /haiku/src/preferences/screen/AlertWindow.cpp (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 /*
2  * Copyright 2001-2015, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Rafael Romo
7  *		Stefano Ceccherini (burton666@libero.it)
8  *		Axel Dörfler, axeld@pinc-software.de
9  *		Augustin Cavalier <waddlesplash>
10  */
11 
12 
13 #include "AlertWindow.h"
14 #include "Constants.h"
15 
16 #include <Button.h>
17 #include <Catalog.h>
18 #include <String.h>
19 #include <TextView.h>
20 #include <Window.h>
21 #include <TimeUnitFormat.h>
22 
23 
24 #undef B_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_CONTEXT "Screen"
26 
27 
28 AlertWindow::AlertWindow(BMessenger handler)
29 	: BAlert(B_TRANSLATE("Confirm changes"),
30 			 "", B_TRANSLATE("Undo"), B_TRANSLATE("Keep")),
31 	// we will wait 12 seconds until we send a message
32 	fSeconds(12),
33 	fHandler(handler)
34 {
35 	SetType(B_WARNING_ALERT);
36 	SetPulseRate(1000000);
37 	TextView()->SetStylable(true);
38 	TextView()->GetFontAndColor(0, &fOriginalFont);
39 	fFont = fOriginalFont;
40 	fFont.SetFace(B_BOLD_FACE);
41 	UpdateCountdownView();
42 }
43 
44 
45 void
46 AlertWindow::DispatchMessage(BMessage* message, BHandler* handler)
47 {
48 	if (message->what == B_PULSE) {
49 		if (--fSeconds == 0) {
50 			fHandler.SendMessage(BUTTON_UNDO_MSG);
51 			PostMessage(B_QUIT_REQUESTED);
52 			Hide();
53 		} else
54 			UpdateCountdownView();
55 	}
56 
57 	BAlert::DispatchMessage(message, handler);
58 }
59 
60 
61 void
62 AlertWindow::MessageReceived(BMessage* message)
63 {
64 	switch (message->what) {
65 		case 'ALTB': // alert button message
66 		{
67 			int32 which;
68 			if (message->FindInt32("which", &which) == B_OK) {
69 				if (which == 1)
70 					fHandler.SendMessage(MAKE_INITIAL_MSG);
71 				else if (which == 0)
72 					fHandler.SendMessage(BUTTON_UNDO_MSG);
73 				PostMessage(B_QUIT_REQUESTED);
74 				Hide();
75 			}
76 			break;
77 		}
78 
79 		case B_KEY_DOWN:
80 		{
81 			int8 val;
82 			if (message->FindInt8("byte", &val) == B_OK && val == B_ESCAPE) {
83 				fHandler.SendMessage(BUTTON_UNDO_MSG);
84 				PostMessage(B_QUIT_REQUESTED);
85 				Hide();
86 				break;
87 			}
88 			// fall through
89 		}
90 
91 		default:
92 			BAlert::MessageReceived(message);
93 			break;
94 	}
95 }
96 
97 
98 void
99 AlertWindow::UpdateCountdownView()
100 {
101 	BString str1 = B_TRANSLATE("Do you wish to keep these settings?");
102 	BString string = str1;
103 	string += "\n";
104 	string += B_TRANSLATE("Settings will revert in %seconds.");
105 
106 	BTimeUnitFormat format;
107 	BString tmp;
108 	format.Format(tmp, fSeconds, B_TIME_UNIT_SECOND);
109 
110 	string.ReplaceFirst("%seconds", tmp);
111 	// The below is black magic, do not touch. We really need to refactor
112 	// BTextView sometime...
113 	TextView()->SetFontAndColor(0, str1.Length() + 1, &fOriginalFont,
114 		B_FONT_ALL);
115 	TextView()->SetText(string.String());
116 	TextView()->SetFontAndColor(0, str1.Length(), &fFont, B_FONT_ALL);
117 }
118