xref: /haiku/src/apps/pulse/NormalPulseView.cpp (revision 508f54795f39c3e7552d87c95aae9dd8ec6f505b)
1 //*****************************************************************************
2 //
3 //	File:		NormalPulseView.cpp
4 //
5 //	Written by:	Daniel Switkin
6 //
7 //	Copyright 1999, Be Incorporated
8 //
9 //*****************************************************************************
10 
11 
12 #include "NormalPulseView.h"
13 #include "Common.h"
14 #include "Pictures"
15 
16 #include <Catalog.h>
17 #include <Bitmap.h>
18 #include <Dragger.h>
19 #include <Locale.h>
20 #include <Window.h>
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include <cpu_type.h>
27 
28 #undef B_TRANSLATE_CONTEXT
29 #define B_TRANSLATE_CONTEXT "NormalPulseView"
30 
31 
32 float
33 max_font_size(BFont font, const char* text, float maxSize, float maxWidth)
34 {
35 	const float steps = 0.5f;
36 
37 	for (float size = maxSize; size > 4; size -= steps) {
38 		font.SetSize(size);
39 		if (font.StringWidth(text) <= maxWidth)
40 			return size;
41 	}
42 
43 	return 4;
44 }
45 
46 
47 //	#pragma mark -
48 
49 
50 NormalPulseView::NormalPulseView(BRect rect)
51 	: PulseView(rect, "NormalPulseView"),
52 	fHasBrandLogo(false)
53 {
54 	rgb_color color = { 168, 168, 168, 0xff };
55 	SetViewColor(color);
56 	SetLowColor(color);
57 
58 	mode1->SetLabel(B_TRANSLATE("Mini mode"));
59 	mode1->SetMessage(new BMessage(PV_MINI_MODE));
60 	mode2->SetLabel(B_TRANSLATE("Deskbar mode"));
61 	mode2->SetMessage(new BMessage(PV_DESKBAR_MODE));
62 
63 	DetermineVendorAndProcessor();
64 
65 	// Allocate progress bars and button pointers
66 	system_info systemInfo;
67 	get_system_info(&systemInfo);
68 	fCpuCount = systemInfo.cpu_count;
69 	fProgressBars = new ProgressBar *[fCpuCount];
70 	fCpuButtons = new CPUButton *[fCpuCount];
71 
72 	// Set up the CPU activity bars and buttons
73 	for (int x = 0; x < fCpuCount; x++) {
74 		BRect r(PROGRESS_MLEFT, PROGRESS_MTOP + ITEM_OFFSET * x,
75 			PROGRESS_MLEFT + ProgressBar::PROGRESS_WIDTH,
76 			PROGRESS_MTOP + ITEM_OFFSET * x + ProgressBar::PROGRESS_HEIGHT);
77 		char* str2 = (char *)B_TRANSLATE("CPU progress bar");
78 		fProgressBars[x] = new ProgressBar(r, str2);
79 		AddChild(fProgressBars[x]);
80 
81 		r.Set(CPUBUTTON_MLEFT, CPUBUTTON_MTOP + ITEM_OFFSET * x,
82 			CPUBUTTON_MLEFT + CPUBUTTON_WIDTH + 7,
83 			CPUBUTTON_MTOP + ITEM_OFFSET * x + CPUBUTTON_HEIGHT + 7);
84 		char temp[4];
85 		sprintf(temp, "%d", x + 1);
86 		fCpuButtons[x] = new CPUButton(r, B_TRANSLATE("Pulse"), temp, NULL);
87 		AddChild(fCpuButtons[x]);
88 	}
89 
90 	if (fCpuCount == 1) {
91 		fProgressBars[0]->MoveBy(-3, 12);
92 		fCpuButtons[0]->Hide();
93 	}
94 }
95 
96 
97 NormalPulseView::~NormalPulseView()
98 {
99 	delete fCpuLogo;
100 	delete[] fCpuButtons;
101 	delete[] fProgressBars;
102 }
103 
104 
105 void
106 NormalPulseView::CalculateFontSizes()
107 {
108 	BFont font;
109 	GetFont(&font);
110 
111 	fProcessorFontSize = max_font_size(font, fProcessor, 11.0f, 46.0f);
112 
113 	if (!fHasBrandLogo)
114 		fVendorFontSize = max_font_size(font, fVendor, 13.0f, 46.0f);
115 }
116 
117 
118 void
119 NormalPulseView::DetermineVendorAndProcessor()
120 {
121 	system_info sys_info;
122 	get_system_info(&sys_info);
123 
124 	// Initialize logo
125 
126 	fCpuLogo = new BBitmap(BRect(0, 0, 63, 62), B_CMAP8);
127 	unsigned char *logo = BlankLogo;
128 
129 #if __POWERPC__
130 	logo = PowerPCLogo;
131 #endif
132 #if __INTEL__
133 
134 	switch (sys_info.cpu_type & B_CPU_x86_VENDOR_MASK) {
135 	case B_CPU_INTEL_x86:
136 		logo = IntelLogo;
137 		break;
138 
139 	case B_CPU_AMD_x86:
140 		logo = AmdLogo;
141 		break;
142 	};
143 #endif
144 
145 	fCpuLogo->SetBits(logo, fCpuLogo->BitsLength(), 0, B_CMAP8);
146 	fHasBrandLogo = (logo != BlankLogo);
147 
148 	get_cpu_type(fVendor, sizeof(fVendor), fProcessor, sizeof(fProcessor));
149 }
150 
151 
152 void
153 NormalPulseView::Draw(BRect rect)
154 {
155 	PushState();
156 
157 	// Black frame
158 	SetHighColor(0, 0, 0);
159 	BRect frame = Bounds();
160 	frame.right--;
161 	frame.bottom--;
162 	StrokeRect(frame);
163 
164 	// Bevelled edges
165 	SetHighColor(255, 255, 255);
166 	StrokeLine(BPoint(1, 1), BPoint(frame.right - 1, 1));
167 	StrokeLine(BPoint(1, 1), BPoint(1, frame.bottom - 1));
168 	SetHighColor(80, 80, 80);
169 	StrokeLine(BPoint(frame.right, 1), BPoint(frame.right, frame.bottom));
170 	StrokeLine(BPoint(2, frame.bottom), BPoint(frame.right - 1, frame.bottom));
171 
172 	// Dividing line
173 	SetHighColor(96, 96, 96);
174 	StrokeLine(BPoint(1, frame.bottom + 1), BPoint(frame.right, frame.bottom + 1));
175 	SetHighColor(255, 255, 255);
176 	StrokeLine(BPoint(1, frame.bottom + 2), BPoint(frame.right, frame.bottom + 2));
177 
178 	// Processor picture
179 	DrawBitmap(fCpuLogo, BPoint(10, 10));
180 
181 #if __INTEL__
182 	// Do nothing in the case of non-Intel CPUs - they already have a logo
183 	if (!fHasBrandLogo) {
184 		SetDrawingMode(B_OP_OVER);
185 		SetHighColor(240, 240, 240);
186 		SetFontSize(fVendorFontSize);
187 
188 		float width = StringWidth(fVendor);
189 		MovePenTo(10 + (32 - width / 2), 30);
190 		DrawString(fVendor);
191 	}
192 #endif
193 
194 	// Draw processor type and speed
195 	SetDrawingMode(B_OP_OVER);
196 	SetHighColor(240, 240, 240);
197 
198 	SetFontSize(fProcessorFontSize);
199 	float width = StringWidth(fProcessor);
200 	MovePenTo(10 + (32 - width / 2), 48);
201 	DrawString(fProcessor);
202 
203 	char buffer[64];
204 	int32 cpuSpeed = get_rounded_cpu_speed();
205 	if (cpuSpeed > 1000 && (cpuSpeed % 10) == 0)
206 		snprintf(buffer, sizeof(buffer), "%.2f GHz", cpuSpeed / 1000.0f);
207 	else
208 		snprintf(buffer, sizeof(buffer), "%ld MHz", cpuSpeed);
209 
210 	// We can't assume anymore that a CPU clock speed is always static.
211 	// Let's compute the best font size for the CPU speed string each time...
212 	BFont font;
213 	GetFont(&font);
214 	SetFontSize(max_font_size(font, buffer, fProcessorFontSize, 46.0f));
215 	width = StringWidth(buffer);
216 	MovePenTo(10 + (32 - width / 2), 60);
217 	DrawString(buffer);
218 
219 	PopState();
220 }
221 
222 
223 void
224 NormalPulseView::Pulse()
225 {
226 	// Don't recalculate and redraw if this view is hidden
227 	if (!IsHidden()) {
228 		Update();
229 		if (Window()->Lock()) {
230 			// Set the value of each CPU bar
231 			for (int x = 0; x < fCpuCount; x++) {
232 				fProgressBars[x]->Set((int32)max_c(0, cpu_times[x] * 100));
233 			}
234 
235 			Sync();
236 			Window()->Unlock();
237 		}
238 	}
239 }
240 
241 
242 void
243 NormalPulseView::AttachedToWindow()
244 {
245 	SetFont(be_bold_font);
246 	CalculateFontSizes();
247 
248 	fPreviousTime = system_time();
249 
250 	BMessenger messenger(Window());
251 	mode1->SetTarget(messenger);
252 	mode2->SetTarget(messenger);
253 	preferences->SetTarget(messenger);
254 	about->SetTarget(messenger);
255 
256 	system_info sys_info;
257 	get_system_info(&sys_info);
258 	if (sys_info.cpu_count >= 2) {
259 		for (int x = 0; x < sys_info.cpu_count; x++) {
260 			cpu_menu_items[x]->SetTarget(messenger);
261 		}
262 	}
263 }
264 
265 
266 void
267 NormalPulseView::UpdateColors(BMessage *message)
268 {
269 	int32 color = message->FindInt32("color");
270 	bool fade = message->FindBool("fade");
271 	system_info sys_info;
272 	get_system_info(&sys_info);
273 
274 	for (int x = 0; x < sys_info.cpu_count; x++) {
275 		fProgressBars[x]->UpdateColors(color, fade);
276 		fCpuButtons[x]->UpdateColors(color);
277 	}
278 }
279 
280