xref: /haiku/src/apps/pulse/ProgressBar.cpp (revision 89755088d790ff4fe36f8aa77dacb2bd15507108)
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::MouseDown(BPoint point)
73 {
74 	point = ConvertToParent(point);
75 	Parent()->MouseDown(point);
76 }
77 
78 
79 void
80 ProgressBar::Set(int32 value)
81 {
82 	// How many segments to light up
83 	current_value = (int32)(value / 4.9);
84 	if (current_value > 20)
85 		current_value = 20;
86 
87 	Render(false);
88 }
89 
90 
91 // Draws the progress bar. If "all" is true the entire bar is redrawn rather
92 // than just the part that changed.
93 void
94 ProgressBar::Render(bool all)
95 {
96 	if (all) {
97 		// Black border
98 		BRect bounds = Bounds();
99 		bounds.InsetBy(2, 2);
100 		SetHighColor(0, 0, 0);
101 		StrokeRect(bounds);
102 		bounds.InsetBy(1, 1);
103 		StrokeRect(bounds);
104 
105 		// Black dividers
106 		float left = bounds.left;
107 		BPoint start, end;
108 		for (int x = 0; x < 19; x++) {
109 			left += 7;
110 			start.Set(left, bounds.top);
111 			end.Set(left, bounds.bottom);
112 			StrokeLine(start, end);
113 		}
114 
115 		for (int x = 0; x < current_value; x++) {
116 			SetHighColor(segments[x].color.red, segments[x].color.green,
117 				segments[x].color.blue);
118 			FillRect(segments[x].rect);
119 		}
120 
121 		SetHighColor(75, 75, 75);
122 		if (current_value < 20) {
123 			for (int x = 19; x >= current_value; x--) {
124 				FillRect(segments[x].rect);
125 			}
126 		}
127 	} else if (current_value > previous_value) {
128 		for (int x = previous_value; x < current_value; x++) {
129 			SetHighColor(segments[x].color.red, segments[x].color.green,
130 				segments[x].color.blue);
131 			FillRect(segments[x].rect);
132 		}
133 	} else if (current_value < previous_value) {
134 		SetHighColor(75, 75, 75);
135 		for (int x = previous_value - 1; x >= current_value; x--) {
136 			FillRect(segments[x].rect);
137 		}
138 		// Special case to make sure the lowest light gets turned off
139 		if (current_value == 0) FillRect(segments[0].rect);
140 	}
141 
142 	Sync();
143 	previous_value = current_value;
144 }
145 
146 
147 void
148 ProgressBar::Draw(BRect rect)
149 {
150 	// Add bevels
151 	SetHighColor(dkgray, dkgray, dkgray);
152 	BRect frame = Bounds();
153 	StrokeLine(BPoint(frame.left, frame.top), BPoint(frame.right, frame.top));
154 	StrokeLine(BPoint(frame.left, frame.top + 1), BPoint(frame.right, frame.top + 1));
155 	StrokeLine(BPoint(frame.left, frame.top), BPoint(frame.left, frame.bottom));
156 	StrokeLine(BPoint(frame.left + 1, frame.top), BPoint(frame.left + 1, frame.bottom));
157 
158 	SetHighColor(ltgray, ltgray, ltgray);
159 	StrokeLine(BPoint(frame.right-1, frame.top + 2), BPoint(frame.right - 1, frame.bottom));
160 	StrokeLine(BPoint(frame.right, frame.top + 1), BPoint(frame.right, frame.bottom));
161 	StrokeLine(BPoint(frame.left+1, frame.bottom - 1), BPoint(frame.right - 1, frame.bottom - 1));
162 	StrokeLine(BPoint(frame.left, frame.bottom), BPoint(frame.right, frame.bottom));
163 
164 	Render(true);
165 }
166