tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / preferences / screen / AlertView.cpp
blob7d50749b85c27a0c338346759eda518d3c3ce211
1 /*
2 * Copyright 2001-2008, 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 */
12 #include "AlertView.h"
13 #include "Constants.h"
15 #include <Window.h>
16 #include <Bitmap.h>
17 #include <Button.h>
18 #include <Catalog.h>
19 #include <StringView.h>
20 #include <String.h>
21 #include <TimeUnitFormat.h>
23 #include <IconUtils.h>
24 #include <FindDirectory.h>
25 #include <Resources.h>
26 #include <File.h>
27 #include <Path.h>
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "Screen"
34 AlertView::AlertView(BRect frame, const char *name)
35 : BView(frame, name, B_FOLLOW_ALL, B_WILL_DRAW | B_PULSE_NEEDED),
36 // we will wait 12 seconds until we send a message
37 fSeconds(12)
39 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
40 fBitmap = InitIcon();
42 BRect rect(60, 8, 400, 36);
43 BStringView *stringView = new BStringView(rect, NULL,
44 B_TRANSLATE("Do you wish to keep these settings?"));
45 stringView->SetFont(be_bold_font);
46 stringView->ResizeToPreferred();
47 AddChild(stringView);
49 rect = stringView->Frame();
50 rect.OffsetBy(0, rect.Height());
51 fCountdownView = new BStringView(rect, "countdown", NULL);
52 UpdateCountdownView();
53 fCountdownView->ResizeToPreferred();
54 AddChild(fCountdownView);
56 BButton* keepButton = new BButton(rect, "keep", B_TRANSLATE("Keep"),
57 new BMessage(BUTTON_KEEP_MSG), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
58 keepButton->ResizeToPreferred();
59 AddChild(keepButton);
61 BButton* button = new BButton(rect, "undo", B_TRANSLATE("Undo"),
62 new BMessage(BUTTON_UNDO_MSG), B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
63 button->ResizeToPreferred();
64 AddChild(button);
66 // we're resizing ourselves to the right size
67 // (but we're not implementing GetPreferredSize(), bad style!)
68 float width = stringView->Frame().right;
69 if (fCountdownView->Frame().right > width)
70 width = fCountdownView->Frame().right;
71 if (width < Bounds().Width())
72 width = Bounds().Width();
74 float height
75 = fCountdownView->Frame().bottom + 24 + button->Bounds().Height();
76 ResizeTo(width, height);
78 keepButton->MoveTo(Bounds().Width() - 8 - keepButton->Bounds().Width(),
79 Bounds().Height() - 8 - keepButton->Bounds().Height());
80 button->MoveTo(keepButton->Frame().left - button->Bounds().Width() - 8,
81 keepButton->Frame().top);
83 keepButton->MakeDefault(true);
87 void
88 AlertView::AttachedToWindow()
90 // the view displays a decrementing counter
91 // (until the user must take action)
92 Window()->SetPulseRate(1000000);
93 // every second
95 SetEventMask(B_KEYBOARD_EVENTS);
99 void
100 AlertView::Draw(BRect updateRect)
102 rgb_color dark = tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
103 B_DARKEN_1_TINT);
104 SetHighColor(dark);
106 FillRect(BRect(0.0, 0.0, 30.0, Bounds().bottom));
108 if (fBitmap != NULL) {
109 SetDrawingMode(B_OP_ALPHA);
110 DrawBitmap(fBitmap, BPoint(18.0, 6.0));
111 SetDrawingMode(B_OP_COPY);
116 void
117 AlertView::Pulse()
119 if (--fSeconds == 0)
120 Window()->PostMessage(BUTTON_UNDO_MSG);
121 else
122 UpdateCountdownView();
126 void
127 AlertView::KeyDown(const char* bytes, int32 numBytes)
129 if (numBytes == 1 && bytes[0] == B_ESCAPE)
130 Window()->PostMessage(BUTTON_UNDO_MSG);
134 void
135 AlertView::UpdateCountdownView()
137 BString string;
138 string = B_TRANSLATE("Settings will revert in %seconds.");
140 BTimeUnitFormat format;
141 BString tmp;
142 format.Format(tmp, fSeconds, B_TIME_UNIT_SECOND);
144 string.ReplaceFirst("%seconds", tmp);
145 fCountdownView->SetText(string.String());
149 BBitmap*
150 AlertView::InitIcon()
152 // This is how BAlert gets to its icon
153 BBitmap* icon = NULL;
154 BPath path;
155 if (find_directory(B_BEOS_SERVERS_DIRECTORY, &path) == B_OK) {
156 path.Append("app_server");
157 BResources resources;
158 BFile file;
159 if (file.SetTo(path.Path(), B_READ_ONLY) == B_OK
160 && resources.SetTo(&file) == B_OK) {
161 size_t size;
162 const void* data = resources.LoadResource(B_VECTOR_ICON_TYPE,
163 "warn", &size);
164 if (data) {
165 icon = new BBitmap(BRect(0, 0, 31, 31), 0, B_RGBA32);
166 if (BIconUtils::GetVectorIcon((const uint8*)data, size, icon)
167 != B_OK) {
168 delete icon;
169 icon = NULL;
175 return icon;