vfs: check userland buffers before reading them.
[haiku.git] / src / preferences / screen / AlertWindow.cpp
blob8d9ec104693c77c3645a43d6b88b1f15d774680d
1 /*
2 * Copyright 2001-2015, Haiku.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Rafael Romo
7 * Stefano Ceccherini (burton666@libero.it)
8 * Axel Dörfler, axeld@pinc-software.de
9 * Augustin Cavalier <waddlesplash>
13 #include "AlertWindow.h"
14 #include "Constants.h"
16 #include <Button.h>
17 #include <Catalog.h>
18 #include <String.h>
19 #include <TextView.h>
20 #include <Window.h>
21 #include <TimeUnitFormat.h>
24 #undef B_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_CONTEXT "Screen"
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)
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();
45 void
46 AlertWindow::DispatchMessage(BMessage* message, BHandler* handler)
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();
57 BAlert::DispatchMessage(message, handler);
61 void
62 AlertWindow::MessageReceived(BMessage* message)
64 switch (message->what) {
65 case 'ALTB': // alert button message
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();
76 break;
79 case B_KEY_DOWN:
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;
88 // fall through
91 default:
92 BAlert::MessageReceived(message);
93 break;
98 void
99 AlertWindow::UpdateCountdownView()
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.");
106 BTimeUnitFormat format;
107 BString tmp;
108 format.Format(tmp, fSeconds, B_TIME_UNIT_SECOND);
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);