1 //*****************************************************************************
3 // File: ProgressBar.cpp
5 // Written by: David Ramsey and Daniel Switkin
7 // Copyright 1999, Be Incorporated
9 //*****************************************************************************
12 #include "ProgressBar.h"
16 ProgressBar::ProgressBar(BRect r
, char *name
) : BView(r
, name
, B_FOLLOW_NONE
,
19 previous_value
= current_value
= 0;
22 float height
= (r
.bottom
- r
.top
) - 8;
23 for (int32 counter
= 0; counter
< 20; counter
++) {
24 segments
[counter
].rect
.Set(r
.left
+ (counter
* 7), r
.top
,
25 (r
.left
+ (counter
* 7) + 5), r
.top
+ height
);
26 segments
[counter
].rect
.OffsetTo(BPoint((counter
* 7) + 4, 4));
28 SetLowColor(255, 0, 0);
29 SetViewColor(B_TRANSPARENT_COLOR
);
33 // New - real time updating of bar colors
35 ProgressBar::UpdateColors(int32 color
, bool fade
)
37 unsigned char red
= (color
& 0xff000000) >> 24;
38 unsigned char green
= (color
& 0x00ff0000) >> 16;
39 unsigned char blue
= (color
& 0x0000ff00) >> 8;
42 unsigned char red_base
= red
/ 3;
43 unsigned char green_base
= green
/ 3;
44 unsigned char blue_base
= blue
/ 3;
46 for (int x
= 0; x
< 20; x
++) {
47 segments
[x
].color
.red
= (uint8
)(red_base
+ ((red
- red_base
)
48 * ((float)x
/ 19.0)));
49 segments
[x
].color
.green
= (uint8
)(green_base
50 + ((green
- green_base
) * ((float)x
/ 19.0)));
51 segments
[x
].color
.blue
= (uint8
)(blue_base
+ ((blue
- blue_base
)
52 * ((float)x
/ 19.0)));
53 segments
[x
].color
.alpha
= 0xff;
56 for (int x
= 0; x
< 20; x
++) {
57 segments
[x
].color
.red
= red
;
58 segments
[x
].color
.green
= green
;
59 segments
[x
].color
.blue
= blue
;
60 segments
[x
].color
.alpha
= 0xff;
68 ProgressBar::AttachedToWindow()
70 Prefs
*prefs
= ((PulseApp
*)be_app
)->prefs
;
71 UpdateColors(prefs
->normal_bar_color
, prefs
->normal_fade_colors
);
76 ProgressBar::MouseDown(BPoint point
)
78 point
= ConvertToParent(point
);
79 Parent()->MouseDown(point
);
84 ProgressBar::Set(int32 value
)
86 // How many segments to light up
87 current_value
= (int32
)(value
/ 4.9);
88 if (current_value
> 20)
95 // Draws the progress bar. If "all" is true the entire bar is redrawn rather
96 // than just the part that changed.
98 ProgressBar::Render(bool all
)
102 BRect bounds
= Bounds();
103 bounds
.InsetBy(2, 2);
104 SetHighColor(0, 0, 0);
106 bounds
.InsetBy(1, 1);
110 float left
= bounds
.left
;
112 for (int x
= 0; x
< 19; x
++) {
114 start
.Set(left
, bounds
.top
);
115 end
.Set(left
, bounds
.bottom
);
116 StrokeLine(start
, end
);
119 for (int x
= 0; x
< current_value
; x
++) {
120 SetHighColor(segments
[x
].color
.red
, segments
[x
].color
.green
,
121 segments
[x
].color
.blue
);
122 FillRect(segments
[x
].rect
);
125 SetHighColor(75, 75, 75);
126 if (current_value
< 20) {
127 for (int x
= 19; x
>= current_value
; x
--) {
128 FillRect(segments
[x
].rect
);
131 } else if (current_value
> previous_value
) {
132 for (int x
= previous_value
; x
< current_value
; x
++) {
133 SetHighColor(segments
[x
].color
.red
, segments
[x
].color
.green
,
134 segments
[x
].color
.blue
);
135 FillRect(segments
[x
].rect
);
137 } else if (current_value
< previous_value
) {
138 SetHighColor(75, 75, 75);
139 for (int x
= previous_value
- 1; x
>= current_value
; x
--) {
140 FillRect(segments
[x
].rect
);
142 // Special case to make sure the lowest light gets turned off
143 if (current_value
== 0) FillRect(segments
[0].rect
);
147 previous_value
= current_value
;
152 ProgressBar::Draw(BRect rect
)
155 SetHighColor(dkgray
, dkgray
, dkgray
);
156 BRect frame
= Bounds();
157 StrokeLine(BPoint(frame
.left
, frame
.top
), BPoint(frame
.right
, frame
.top
));
158 StrokeLine(BPoint(frame
.left
, frame
.top
+ 1), BPoint(frame
.right
,
160 StrokeLine(BPoint(frame
.left
, frame
.top
), BPoint(frame
.left
,
162 StrokeLine(BPoint(frame
.left
+ 1, frame
.top
),
163 BPoint(frame
.left
+ 1, frame
.bottom
));
165 SetHighColor(ltgray
, ltgray
, ltgray
);
166 StrokeLine(BPoint(frame
.right
-1, frame
.top
+ 2),
167 BPoint(frame
.right
- 1, frame
.bottom
));
168 StrokeLine(BPoint(frame
.right
, frame
.top
+ 1),
169 BPoint(frame
.right
, frame
.bottom
));
170 StrokeLine(BPoint(frame
.left
+1, frame
.bottom
- 1),
171 BPoint(frame
.right
- 1, frame
.bottom
- 1));
172 StrokeLine(BPoint(frame
.left
, frame
.bottom
),
173 BPoint(frame
.right
, frame
.bottom
));