2 * Copyright 2001-2009, Haiku.
3 * Copyright 2002, Thomas Kurschel.
4 * Distributed under the terms of the MIT License.
9 * Axel Dörfler, axeld@pinc-software.de
13 #include "MonitorView.h"
22 #include "Constants.h"
24 #undef B_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_CONTEXT "Monitor View"
28 MonitorView::MonitorView(BRect rect
, const char *name
, int32 width
, int32 height
)
29 : BView(rect
, name
, B_FOLLOW_ALL
, B_WILL_DRAW
| B_FULL_UPDATE_ON_RESIZE
),
36 BScreen
screen(B_MAIN_SCREEN_ID
);
37 fDesktopColor
= screen
.DesktopColor(current_workspace());
41 MonitorView::~MonitorView()
47 MonitorView::AttachedToWindow()
49 SetViewColor(B_TRANSPARENT_COLOR
);
51 fBackgroundColor
= Parent()->ViewColor();
53 fBackgroundColor
= ui_color(B_PANEL_BACKGROUND_COLOR
);
60 MonitorView::MouseDown(BPoint point
)
62 be_roster
->Launch(kBackgroundsSignature
);
67 MonitorView::Draw(BRect updateRect
)
69 rgb_color darkColor
= {160, 160, 160, 255};
70 rgb_color blackColor
= {0, 0, 0, 255};
71 rgb_color redColor
= {228, 0, 0, 255};
72 rgb_color whiteColor
= {255, 255, 255, 255};
73 BRect outerRect
= _MonitorBounds();
75 SetHighColor(fBackgroundColor
);
78 SetDrawingMode(B_OP_OVER
);
82 SetHighColor(darkColor
);
83 FillRoundRect(outerRect
, 3.0, 3.0);
85 SetHighColor(blackColor
);
86 StrokeRoundRect(outerRect
, 3.0, 3.0);
88 SetHighColor(fDesktopColor
);
90 BRect
innerRect(outerRect
.InsetByCopy(4, 4));
91 FillRoundRect(innerRect
, 2.0, 2.0);
93 SetHighColor(blackColor
);
94 StrokeRoundRect(innerRect
, 2.0, 2.0);
96 SetDrawingMode(B_OP_COPY
);
100 SetHighColor(redColor
);
101 BPoint
powerPos(outerRect
.left
+ 5, outerRect
.bottom
- 2);
102 StrokeLine(powerPos
, BPoint(powerPos
.x
+ 2, powerPos
.y
));
109 font_height fontHeight
;
110 GetFontHeight(&fontHeight
);
111 float height
= ceilf(fontHeight
.ascent
+ fontHeight
.descent
);
114 snprintf(text
, sizeof(text
), B_TRANSLATE("%ld dpi"), fDPI
);
116 float width
= StringWidth(text
);
117 if (width
> innerRect
.Width() || height
> innerRect
.Height())
120 SetLowColor(fDesktopColor
);
121 SetHighColor(whiteColor
);
123 DrawString(text
, BPoint(innerRect
.left
+ (innerRect
.Width() - width
) / 2,
124 innerRect
.top
+ fontHeight
.ascent
+ (innerRect
.Height() - height
) / 2));
129 MonitorView::SetResolution(int32 width
, int32 height
)
131 if (fWidth
== width
&& fHeight
== height
)
143 MonitorView::SetMaxResolution(int32 width
, int32 height
)
145 if (fMaxWidth
== width
&& fMaxHeight
== height
)
156 MonitorView::MessageReceived(BMessage
* message
)
158 switch (message
->what
) {
159 case UPDATE_DESKTOP_MSG
:
162 if (message
->FindInt32("width", &width
) == B_OK
163 && message
->FindInt32("height", &height
) == B_OK
)
164 SetResolution(width
, height
);
168 case UPDATE_DESKTOP_COLOR_MSG
:
170 BScreen
screen(Window());
171 rgb_color color
= screen
.DesktopColor(current_workspace());
172 if (color
!= fDesktopColor
) {
173 fDesktopColor
= color
;
180 BView::MessageReceived(message
);
187 MonitorView::_MonitorBounds()
189 float maxWidth
= Bounds().Width();
190 float maxHeight
= Bounds().Height();
191 if (maxWidth
/ maxHeight
> (float)fMaxWidth
/ fMaxHeight
)
192 maxWidth
= maxHeight
/ fMaxHeight
* fMaxWidth
;
194 maxHeight
= maxWidth
/ fMaxWidth
* fMaxHeight
;
196 float factorX
= (float)fWidth
/ fMaxWidth
;
197 float factorY
= (float)fHeight
/ fMaxHeight
;
199 if (factorX
> factorY
&& factorX
> 1) {
202 } else if (factorY
> factorX
&& factorY
> 1) {
207 float width
= maxWidth
* factorX
;
208 float height
= maxHeight
* factorY
;
210 BSize size
= Bounds().Size();
211 return BRect((size
.width
- width
) / 2, (size
.height
- height
) / 2,
212 (size
.width
+ width
) / 2, (size
.height
+ height
) / 2);
217 MonitorView::_UpdateDPI()
221 BScreen
screen(Window());
223 if (screen
.GetMonitorInfo(&info
) != B_OK
)
226 double x
= info
.width
/ 2.54;
227 double y
= info
.height
/ 2.54;
229 fDPI
= (int32
)round((fWidth
/ x
+ fHeight
/ y
) / 2);