xref: /haiku/src/preferences/screen/MonitorView.cpp (revision 16d5c24e533eb14b7b8a99ee9f3ec9ba66335b1e)
1 /*
2  * Copyright 2001-2006, 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 #ifndef __HAIKU__
21 inline bool
22 operator!=(const rgb_color& x, const rgb_color& y)
23 {
24 	return x.red != y.red || x.blue != y.blue || x.green != y.green;
25 }
26 #endif
27 
28 
29 MonitorView::MonitorView(BRect rect, char *name, int32 width, int32 height)
30 	: BView(rect, name, B_FOLLOW_ALL, B_WILL_DRAW),
31 	fWidth(width),
32 	fHeight(height)
33 {
34 	BScreen screen(B_MAIN_SCREEN_ID);
35 	fDesktopColor = screen.DesktopColor(current_workspace());
36 }
37 
38 
39 MonitorView::~MonitorView()
40 {
41 }
42 
43 
44 void
45 MonitorView::AttachedToWindow()
46 {
47 	if (Parent())
48 		SetViewColor(Parent()->ViewColor());
49 	else
50 		SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
51 }
52 
53 
54 void
55 MonitorView::MouseDown(BPoint point)
56 {
57 	be_roster->Launch(kBackgroundsSignature);
58 }
59 
60 
61 BRect
62 MonitorView::MonitorBounds()
63 {
64 	float picWidth, picHeight;
65 
66 	float maxSize = min_c(Bounds().Width(), Bounds().Height());
67 
68 	picWidth = maxSize * fWidth / 1600;
69 	picHeight = maxSize * fHeight / 1600;
70 
71 	if (picWidth > maxSize) {
72 		picHeight = picHeight * maxSize / picWidth;
73 		picWidth = maxSize;
74 	}
75 
76 	if (picHeight > maxSize) {
77 		picWidth = picWidth * maxSize / picHeight;
78 		picHeight = maxSize;
79 	}
80 
81 	BPoint size = BPoint(Bounds().Width(), Bounds().Height());
82 	return BRect((size.x - picWidth) / 2, (size.y - picHeight) / 2,
83 		(size.x + picWidth) / 2, (size.y + picHeight) / 2);
84 }
85 
86 
87 void
88 MonitorView::Draw(BRect updateRect)
89 {
90 	rgb_color darkColor = {160, 160, 160, 255};
91 	rgb_color blackColor = {0, 0, 0, 255};
92 	rgb_color redColor = {228, 0, 0, 255};
93 	BRect outerRect = MonitorBounds();
94 
95 	SetDrawingMode(B_OP_OVER);
96 
97 	// frame & background
98 
99 	SetHighColor(darkColor);
100 	FillRoundRect(outerRect, 3.0, 3.0);
101 
102 	SetHighColor(blackColor);
103 	StrokeRoundRect(outerRect, 3.0, 3.0);
104 
105 	SetHighColor(fDesktopColor);
106 
107 	BRect innerRect(outerRect.InsetByCopy(4, 4));
108 	FillRoundRect(innerRect, 2.0, 2.0);
109 
110 	SetHighColor(blackColor);
111 	StrokeRoundRect(innerRect, 2.0, 2.0);
112 
113 	SetDrawingMode(B_OP_COPY);
114 
115 	// power light
116 
117 	SetHighColor(redColor);
118 	BPoint powerPos(outerRect.left + 5, outerRect.bottom - 2);
119 	StrokeLine(powerPos, BPoint(powerPos.x + 2, powerPos.y));
120 }
121 
122 
123 void
124 MonitorView::SetResolution(int32 width, int32 height)
125 {
126 	if (fWidth == width && fHeight == height)
127 		return;
128 
129 	fWidth = width;
130 	fHeight = height;
131 
132 	Invalidate();
133 }
134 
135 
136 void
137 MonitorView::MessageReceived(BMessage* message)
138 {
139 	switch (message->what) {
140 		case UPDATE_DESKTOP_MSG:
141 		{
142 			int32 width, height;
143 			if (message->FindInt32("width", &width) == B_OK
144 				&& message->FindInt32("height", &height) == B_OK)
145 				SetResolution(width, height);
146 			break;
147 		}
148 
149 		case UPDATE_DESKTOP_COLOR_MSG:
150 		{
151 			BScreen screen(Window());
152 			rgb_color color = screen.DesktopColor(current_workspace());
153 			if (color != fDesktopColor) {
154 				fDesktopColor = color;
155 				Invalidate();
156 			}
157 		}
158 
159 		default:
160 			BView::MessageReceived(message);
161 			break;
162 	}
163 }
164