xref: /haiku/src/apps/pulse/ProgressBar.cpp (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 //****************************************************************************************
2 //
3 //	File:		ProgressBar.cpp
4 //
5 //	Written by:	David Ramsey and Daniel Switkin
6 //
7 //	Copyright 1999, Be Incorporated
8 //
9 //****************************************************************************************
10 
11 
12 #include "ProgressBar.h"
13 #include "PulseApp.h"
14 
15 
16 ProgressBar::ProgressBar(BRect r, char *name) :	BView(r, name, B_FOLLOW_NONE, B_WILL_DRAW)
17 {
18 	previous_value = current_value = 0;
19 
20 	// Create 20 segments
21 	float height = (r.bottom - r.top) - 8;
22 	for (int32 counter = 0; counter < 20; counter++) {
23 		segments[counter].rect.Set(r.left + (counter * 7), r.top,
24 			(r.left + (counter * 7) + 5), r.top + height);
25 		segments[counter].rect.OffsetTo(BPoint((counter * 7) + 4, 4));
26 	}
27 	SetLowColor(255, 0, 0);
28 	SetViewColor(B_TRANSPARENT_COLOR);
29 }
30 
31 
32 // New - real time updating of bar colors
33 void
34 ProgressBar::UpdateColors(int32 color, bool fade)
35 {
36 	unsigned char red = (color & 0xff000000) >> 24;
37 	unsigned char green = (color & 0x00ff0000) >> 16;
38 	unsigned char blue = (color & 0x0000ff00) >> 8;
39 
40 	if (fade) {
41 		unsigned char red_base = red / 3;
42 		unsigned char green_base = green / 3;
43 		unsigned char blue_base = blue / 3;
44 
45 		for (int x = 0; x < 20; x++) {
46 			segments[x].color.red = (uint8)(red_base + ((red - red_base) * ((float)x / 19.0)));
47 			segments[x].color.green = (uint8)(green_base + ((green - green_base) * ((float)x / 19.0)));
48 			segments[x].color.blue = (uint8)(blue_base + ((blue - blue_base) * ((float)x / 19.0)));
49 			segments[x].color.alpha = 0xff;
50 		}
51 	} else {
52 		for (int x = 0; x < 20; x++) {
53 			segments[x].color.red = red;
54 			segments[x].color.green = green;
55 			segments[x].color.blue = blue;
56 			segments[x].color.alpha = 0xff;
57 		}
58 	}
59 	Render(true);
60 }
61 
62 
63 void
64 ProgressBar::AttachedToWindow()
65 {
66 	Prefs *prefs = ((PulseApp *)be_app)->prefs;
67 	UpdateColors(prefs->normal_bar_color, prefs->normal_fade_colors);
68 }
69 
70 
71 void
72 ProgressBar::Set(int32 value)
73 {
74 	// How many segments to light up
75 	current_value = (int32)(value / 4.9);
76 	if (current_value > 20)
77 		current_value = 20;
78 
79 	Render(false);
80 }
81 
82 
83 // Draws the progress bar. If "all" is true the entire bar is redrawn rather
84 // than just the part that changed.
85 void
86 ProgressBar::Render(bool all)
87 {
88 	if (all) {
89 		// Black border
90 		BRect bounds = Bounds();
91 		bounds.InsetBy(2, 2);
92 		SetHighColor(0, 0, 0);
93 		StrokeRect(bounds);
94 		bounds.InsetBy(1, 1);
95 		StrokeRect(bounds);
96 
97 		// Black dividers
98 		float left = bounds.left;
99 		BPoint start, end;
100 		for (int x = 0; x < 19; x++) {
101 			left += 7;
102 			start.Set(left, bounds.top);
103 			end.Set(left, bounds.bottom);
104 			StrokeLine(start, end);
105 		}
106 
107 		for (int x = 0; x < current_value; x++) {
108 			SetHighColor(segments[x].color.red, segments[x].color.green,
109 				segments[x].color.blue);
110 			FillRect(segments[x].rect);
111 		}
112 
113 		SetHighColor(75, 75, 75);
114 		if (current_value < 20) {
115 			for (int x = 19; x >= current_value; x--) {
116 				FillRect(segments[x].rect);
117 			}
118 		}
119 	} else if (current_value > previous_value) {
120 		for (int x = previous_value; x < current_value; x++) {
121 			SetHighColor(segments[x].color.red, segments[x].color.green,
122 				segments[x].color.blue);
123 			FillRect(segments[x].rect);
124 		}
125 	} else if (current_value < previous_value) {
126 		SetHighColor(75, 75, 75);
127 		for (int x = previous_value - 1; x >= current_value; x--) {
128 			FillRect(segments[x].rect);
129 		}
130 		// Special case to make sure the lowest light gets turned off
131 		if (current_value == 0) FillRect(segments[0].rect);
132 	}
133 
134 	Sync();
135 	previous_value = current_value;
136 }
137 
138 
139 void
140 ProgressBar::Draw(BRect rect)
141 {
142 	// Add bevels
143 	SetHighColor(dkgray, dkgray, dkgray);
144 	BRect frame = Bounds();
145 	StrokeLine(BPoint(frame.left, frame.top), BPoint(frame.right, frame.top));
146 	StrokeLine(BPoint(frame.left, frame.top + 1), BPoint(frame.right, frame.top + 1));
147 	StrokeLine(BPoint(frame.left, frame.top), BPoint(frame.left, frame.bottom));
148 	StrokeLine(BPoint(frame.left + 1, frame.top), BPoint(frame.left + 1, frame.bottom));
149 
150 	SetHighColor(ltgray, ltgray, ltgray);
151 	StrokeLine(BPoint(frame.right-1, frame.top + 2), BPoint(frame.right - 1, frame.bottom));
152 	StrokeLine(BPoint(frame.right, frame.top + 1), BPoint(frame.right, frame.bottom));
153 	StrokeLine(BPoint(frame.left+1, frame.bottom - 1), BPoint(frame.right - 1, frame.bottom - 1));
154 	StrokeLine(BPoint(frame.left, frame.bottom), BPoint(frame.right, frame.bottom));
155 
156 	Render(true);
157 }
158