1/*
2 * Copyright 2003-2008 Haiku Inc.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Jérôme Duval
7 */
8
9#include "MediaAlert.h"
10
11#include <string.h>
12
13#include <File.h>
14#include <FindDirectory.h>
15#include <IconUtils.h>
16#include <Path.h>
17#include <Resources.h>
18#include <Screen.h>
19#include <View.h>
20
21
22const int kWindowIconOffset = 27;
23const int kIconStripeWidth = 30;
24const int kTextIconOffset = kWindowIconOffset + kIconStripeWidth - 2;
25const int kTextTopOffset = 6;
26
27class TAlertView : public BView {
28public:
29							TAlertView(BRect frame);
30							TAlertView(BMessage* archive);
31							~TAlertView();
32
33	virtual void			Draw(BRect updateRect);
34
35			void			SetBitmap(BBitmap* Icon)	{ fIconBitmap = Icon; }
36			BBitmap*		Bitmap()					{ return fIconBitmap; }
37
38private:
39		BBitmap*			fIconBitmap;
40};
41
42
43MediaAlert::MediaAlert(BRect _rect, const char* title, const char* text)
44	:
45	BWindow(_rect, title, B_MODAL_WINDOW, B_NOT_CLOSABLE | B_NOT_RESIZABLE),
46	fTextView(NULL)
47{
48	// Set up the "_master_" view
49	TAlertView* masterView = new TAlertView(Bounds());
50	masterView->SetBitmap(InitIcon());
51	AddChild(masterView);
52
53	// Set up the text view
54	BRect textViewRect(kTextIconOffset, kTextTopOffset,
55		Bounds().right, Bounds().bottom);
56	BRect rect = textViewRect;
57	rect.OffsetTo(B_ORIGIN);
58	rect.InsetBy(4, 2);
59
60	fTextView = new BTextView(textViewRect, "_tv_", rect,
61		B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
62	fTextView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
63	fTextView->SetText(text, strlen(text));
64	fTextView->MakeEditable(false);
65	fTextView->MakeSelectable(false);
66	fTextView->SetWordWrap(true);
67	fTextView->SetFontAndColor(be_bold_font);
68
69	masterView->AddChild(fTextView);
70
71	BRect screenFrame = BScreen(B_MAIN_SCREEN_ID).Frame();
72	BPoint pt;
73	pt.x = screenFrame.Width() / 2 - Bounds().Width() / 2;
74	pt.y = screenFrame.Height() / 2 - Bounds().Height() / 2;
75
76	if (screenFrame.Contains(pt))
77		MoveTo(pt);
78}
79
80
81MediaAlert::~MediaAlert()
82{
83}
84
85
86BTextView*
87MediaAlert::TextView() const
88{
89	return fTextView;
90}
91
92
93BBitmap*
94MediaAlert::InitIcon()
95{
96	// The alert icons are in the app_server resources
97	BBitmap* icon = NULL;
98	BPath path;
99	if (find_directory(B_BEOS_SERVERS_DIRECTORY, &path) == B_OK) {
100		path.Append("app_server");
101		BFile file;
102		if (file.SetTo(path.Path(), B_READ_ONLY) == B_OK) {
103			BResources resources;
104			if (resources.SetTo(&file) == B_OK) {
105				// Which icon are we trying to load?
106				const char* iconName = "warn";
107
108				// Load the raw icon data
109				size_t size;
110				const void* rawIcon =
111					resources.LoadResource(B_VECTOR_ICON_TYPE, iconName, &size);
112
113				if (rawIcon != NULL) {
114					// Now build the bitmap
115					icon = new BBitmap(BRect(0, 0, 31, 31), B_RGBA32);
116					if (BIconUtils::GetVectorIcon((const uint8*)rawIcon, size,
117							icon) != B_OK) {
118						delete icon;
119						return NULL;
120					}
121				}
122			}
123		}
124	}
125
126	return icon;
127}
128
129
130//	#pragma mark - TAlertView
131
132
133TAlertView::TAlertView(BRect frame)
134	:
135	BView(frame, "TAlertView", B_FOLLOW_ALL_SIDES, B_WILL_DRAW),
136	fIconBitmap(NULL)
137{
138	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
139}
140
141
142TAlertView::~TAlertView()
143{
144	delete fIconBitmap;
145}
146
147
148void
149TAlertView::Draw(BRect updateRect)
150{
151	// Here's the fun stuff
152	if (fIconBitmap) {
153		BRect stripeRect = Bounds();
154		stripeRect.right = kIconStripeWidth;
155		SetHighColor(tint_color(ViewColor(), B_DARKEN_1_TINT));
156		FillRect(stripeRect);
157
158		SetDrawingMode(B_OP_ALPHA);
159		DrawBitmapAsync(fIconBitmap, BPoint(18, 6));
160		SetDrawingMode(B_OP_COPY);
161	}
162}
163