xref: /haiku/src/preferences/screen/MonitorView.cpp (revision 0b2dbe7d46ee888392907c60131b7f7652314175)
1 /*
2  * Copyright 2001-2005, Haiku.
3  * Copyright 2002, Thomas Kurschel.
4  * Distributed under the terms of the MIT License.
5  *
6  * Authors:
7  *		Rafael Romo
8  *		Thomas Kurschel
9  *		Axel Dörfler, axeld@pinc-software.de
10  */
11 
12 
13 #include "MonitorView.h"
14 #include "Constants.h"
15 
16 #include <Roster.h>
17 #include <Screen.h>
18 
19 
20 MonitorView::MonitorView(BRect rect, char *name, int32 width, int32 height)
21 	: BView(rect, name, B_FOLLOW_ALL, B_WILL_DRAW),
22 	fWidth(width),
23 	fHeight(height)
24 {
25 	BScreen screen(B_MAIN_SCREEN_ID);
26 	fDesktopColor = screen.DesktopColor(current_workspace());
27 }
28 
29 
30 MonitorView::~MonitorView()
31 {
32 }
33 
34 
35 void
36 MonitorView::AttachedToWindow()
37 {
38 	if (Parent())
39 		SetViewColor(Parent()->ViewColor());
40 	else
41 		SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
42 }
43 
44 
45 void
46 MonitorView::MouseDown(BPoint point)
47 {
48 	be_roster->Launch("application/x-vnd.Be-BACK");
49 }
50 
51 
52 BRect
53 MonitorView::MonitorBounds()
54 {
55 	float maxWidth, maxHeight;
56 	float picWidth, picHeight;
57 
58 	maxWidth = Bounds().Width();
59 	maxHeight = Bounds().Height();
60 
61 	picWidth = maxWidth * fWidth / 1600;
62 	picHeight = maxHeight * fHeight / 1200;
63 
64 	if (picWidth > maxWidth) {
65 		picHeight = picHeight * maxWidth / picWidth;
66 		picWidth = maxWidth;
67 	}
68 
69 	if (picHeight > maxHeight) {
70 		picWidth = picWidth * maxHeight / picHeight;
71 		picHeight = maxHeight;
72 	}
73 
74 	return BRect((maxWidth - picWidth) / 2, (maxHeight - picHeight) / 2,
75 		(maxWidth + picWidth) / 2, (maxHeight + picHeight) / 2);
76 }
77 
78 
79 void
80 MonitorView::Draw(BRect updateRect)
81 {
82 	rgb_color darkColor = {160, 160, 160, 255};
83 	rgb_color blackColor = {0, 0, 0, 255};
84 	rgb_color redColor = {228, 0, 0, 255};
85 	BRect outerRect = MonitorBounds();
86 
87 	SetDrawingMode(B_OP_OVER);
88 
89 	// frame & background
90 
91 	SetHighColor(darkColor);
92 	FillRoundRect(outerRect, 3.0, 3.0);
93 
94 	SetHighColor(blackColor);
95 	StrokeRoundRect(outerRect, 3.0, 3.0);
96 
97 	SetHighColor(fDesktopColor);
98 
99 	BRect innerRect(outerRect.InsetByCopy(4, 4));
100 	FillRoundRect(innerRect, 2.0, 2.0);
101 
102 	SetHighColor(blackColor);
103 	StrokeRoundRect(innerRect, 2.0, 2.0);
104 
105 	SetDrawingMode(B_OP_COPY);
106 
107 	// power light
108 
109 	SetHighColor(redColor);
110 	BPoint powerPos(outerRect.left + 5, outerRect.bottom - 2);
111 	StrokeLine(powerPos, BPoint(powerPos.x + 2, powerPos.y));
112 }
113 
114 
115 void
116 MonitorView::SetResolution(int32 width, int32 height)
117 {
118 	if (fWidth == width && fHeight == height)
119 		return;
120 
121 	fWidth = width;
122 	fHeight = height;
123 
124 	Invalidate();
125 }
126 
127 
128 void
129 MonitorView::MessageReceived(BMessage* message)
130 {
131 	switch (message->what) {
132 		case UPDATE_DESKTOP_MSG:
133 		{
134 			int32 width, height;
135 			if (message->FindInt32("width", &width) == B_OK
136 				&& message->FindInt32("height", &height) == B_OK)
137 				SetResolution(width, height);
138 			break;
139 		}
140 
141 		case UPDATE_DESKTOP_COLOR_MSG:
142 		{
143 			BScreen screen(Window());
144 			rgb_color color = screen.DesktopColor(current_workspace());
145 			if (color != fDesktopColor) {
146 				fDesktopColor = color;
147 				Invalidate();
148 			}
149 		}
150 
151 		default:
152 			BView::MessageReceived(message);
153 			break;
154 	}
155 }
156