1//*****************************************************************************
2//
3//	File:		MiniPulseView.cpp
4//
5//	Written by:	Arve Hjonnevag and Daniel Switkin
6//
7//	Copyright 1999, Be Incorporated
8//
9//*****************************************************************************
10
11#include "MiniPulseView.h"
12#include "Common.h"
13#include <Catalog.h>
14#include <interface/Window.h>
15
16#undef B_TRANSLATION_CONTEXT
17#define B_TRANSLATION_CONTEXT "MiniPulseView"
18
19
20MiniPulseView::MiniPulseView(BRect rect, const char *name, Prefs *prefs) :
21	PulseView(rect, name) {
22
23	mode1->SetLabel(B_TRANSLATE("Normal mode"));
24	mode1->SetMessage(new BMessage(PV_NORMAL_MODE));
25	mode2->SetLabel(B_TRANSLATE("Deskbar mode"));
26	mode2->SetMessage(new BMessage(PV_DESKBAR_MODE));
27	quit = new BMenuItem(B_TRANSLATE("Quit"), new BMessage(PV_QUIT), 0, 0);
28	popupmenu->AddSeparatorItem();
29	popupmenu->AddItem(quit);
30
31	// Our drawing covers every pixel in the view, so no reason to
32	// take the time (and to flicker) by resetting the view color
33	SetViewColor(B_TRANSPARENT_COLOR);
34
35	active_color.red = (prefs->mini_active_color & 0xff000000) >> 24;
36	active_color.green = (prefs->mini_active_color & 0x00ff0000) >> 16;
37	active_color.blue = (prefs->mini_active_color & 0x0000ff00) >> 8;
38
39	idle_color.red = (prefs->mini_idle_color & 0xff000000) >> 24;
40	idle_color.green = (prefs->mini_idle_color & 0x00ff0000) >> 16;
41	idle_color.blue = (prefs->mini_idle_color & 0x0000ff00) >> 8;
42
43	frame_color.red = (prefs->mini_frame_color & 0xff000000) >> 24;
44	frame_color.green = (prefs->mini_frame_color & 0x00ff0000) >> 16;
45	frame_color.blue = (prefs->mini_frame_color & 0x0000ff00) >> 8;
46}
47
48// These two are only used by DeskbarPulseView, and so do nothing
49MiniPulseView::MiniPulseView(BRect rect, const char *name)
50 :
51 PulseView(rect, name)
52{
53
54}
55
56MiniPulseView::MiniPulseView(BMessage *message)
57 :
58 PulseView(message)
59{
60
61}
62
63// This method is used by DeskbarPulseView as well
64void MiniPulseView::Draw(BRect rect) {
65	system_info sys_info;
66	get_system_info(&sys_info);
67	if (sys_info.cpu_count <= 0)
68		return;
69
70	BRect bounds(Bounds());
71	SetDrawingMode(B_OP_COPY);
72
73	int h = bounds.IntegerHeight() - 2;
74	float top = 1, left = 1;
75	float bottom = top + h;
76	float bar_width = (bounds.Width()) / sys_info.cpu_count - 2;
77	float right = bar_width + left;
78
79	for (unsigned int x = 0; x < sys_info.cpu_count; x++) {
80		int bar_height = (int)(cpu_times[x] * (h + 1));
81		if (bar_height > h) bar_height = h;
82		double rem = cpu_times[x] * (h + 1) - bar_height;
83
84		rgb_color fraction_color;
85		fraction_color.red = (uint8)(idle_color.red + rem
86			* (active_color.red - idle_color.red));
87		fraction_color.green = (uint8)(idle_color.green + rem
88			* (active_color.green - idle_color.green));
89		fraction_color.blue = (uint8)(idle_color.blue + rem
90			* (active_color.blue - idle_color.blue));
91		fraction_color.alpha = 0xff;
92
93		int idle_height = h - bar_height;
94		SetHighColor(frame_color);
95		StrokeRect(BRect(left - 1, top - 1, right + 1, bottom + 1));
96		if (idle_height > 0) {
97			SetHighColor(idle_color);
98			FillRect(BRect(left, top, right, top + idle_height - 1));
99		}
100		SetHighColor(fraction_color);
101		FillRect(BRect(left, bottom - bar_height, right, bottom - bar_height));
102		if (bar_height > 0) {
103			SetHighColor(active_color);
104			FillRect(BRect(left, bottom - bar_height + 1, right, bottom));
105		}
106		left += bar_width + 2;
107		right += bar_width + 2;
108	}
109}
110
111void MiniPulseView::Pulse() {
112	// Don't recalculate and redraw if this view is hidden
113	if (!IsHidden()) {
114		Update();
115		Draw(Bounds());
116	}
117}
118
119void MiniPulseView::FrameResized(float width, float height) {
120	Draw(Bounds());
121}
122
123void MiniPulseView::AttachedToWindow() {
124	BMessenger messenger(Window());
125	mode1->SetTarget(messenger);
126	mode2->SetTarget(messenger);
127	preferences->SetTarget(messenger);
128	about->SetTarget(messenger);
129	quit->SetTarget(messenger);
130
131	system_info sys_info;
132	get_system_info(&sys_info);
133	if (sys_info.cpu_count >= 2) {
134		for (unsigned int x = 0; x < sys_info.cpu_count; x++)
135			cpu_menu_items[x]->SetTarget(messenger);
136	}
137}
138
139// Redraw the view with the new colors but do not call
140// Update() again - we don't want to recalculate activity
141void MiniPulseView::UpdateColors(BMessage *message) {
142	int32 ac = message->FindInt32("active_color");
143	int32 ic = message->FindInt32("idle_color");
144	int32 fc = message->FindInt32("frame_color");
145
146	active_color.red = (ac & 0xff000000) >> 24;
147	active_color.green = (ac & 0x00ff0000) >> 16;
148	active_color.blue = (ac & 0x0000ff00) >> 8;
149
150	idle_color.red = (ic & 0xff000000) >> 24;
151	idle_color.green = (ic & 0x00ff0000) >> 16;
152	idle_color.blue = (ic & 0x0000ff00) >> 8;
153
154	frame_color.red = (fc & 0xff000000) >> 24;
155	frame_color.green = (fc & 0x00ff0000) >> 16;
156	frame_color.blue = (fc & 0x0000ff00) >> 8;
157
158	Draw(Bounds());
159}
160
161MiniPulseView::~MiniPulseView() {
162
163}
164