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