2 * Copyright 2001-2008, Haiku.
3 * Distributed under the terms of the MIT License.
7 * Stefano Ceccherini (burton666@libero.it)
8 * Axel Dörfler, axeld@pinc-software.de
12 #include "AlertView.h"
13 #include "Constants.h"
19 #include <StringView.h>
21 #include <TimeUnitFormat.h>
23 #include <IconUtils.h>
24 #include <FindDirectory.h>
25 #include <Resources.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
39 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR
));
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();
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();
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();
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();
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);
88 AlertView::AttachedToWindow()
90 // the view displays a decrementing counter
91 // (until the user must take action)
92 Window()->SetPulseRate(1000000);
95 SetEventMask(B_KEYBOARD_EVENTS
);
100 AlertView::Draw(BRect updateRect
)
102 rgb_color dark
= tint_color(ui_color(B_PANEL_BACKGROUND_COLOR
),
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
);
120 Window()->PostMessage(BUTTON_UNDO_MSG
);
122 UpdateCountdownView();
127 AlertView::KeyDown(const char* bytes
, int32 numBytes
)
129 if (numBytes
== 1 && bytes
[0] == B_ESCAPE
)
130 Window()->PostMessage(BUTTON_UNDO_MSG
);
135 AlertView::UpdateCountdownView()
138 string
= B_TRANSLATE("Settings will revert in %seconds.");
140 BTimeUnitFormat format
;
142 format
.Format(tmp
, fSeconds
, B_TIME_UNIT_SECOND
);
144 string
.ReplaceFirst("%seconds", tmp
);
145 fCountdownView
->SetText(string
.String());
150 AlertView::InitIcon()
152 // This is how BAlert gets to its icon
153 BBitmap
* icon
= NULL
;
155 if (find_directory(B_BEOS_SERVERS_DIRECTORY
, &path
) == B_OK
) {
156 path
.Append("app_server");
157 BResources resources
;
159 if (file
.SetTo(path
.Path(), B_READ_ONLY
) == B_OK
160 && resources
.SetTo(&file
) == B_OK
) {
162 const void* data
= resources
.LoadResource(B_VECTOR_ICON_TYPE
,
165 icon
= new BBitmap(BRect(0, 0, 31, 31), 0, B_RGBA32
);
166 if (BIconUtils::GetVectorIcon((const uint8
*)data
, size
, icon
)