xref: /haiku/src/add-ons/tracker/zipomatic/ZipOMaticActivity.cpp (revision 220d04022750f40f8bac8f01fa551211e28d04f2)
1 /*
2  * Copyright 2003-2009, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Jonas Sundström, jonas@kirilla.com
7  */
8 
9 
10 #include "ZipOMaticActivity.h"
11 
12 #include <stdio.h>
13 
14 
15 Activity::Activity(const char* name)
16 	:
17 	BView(name, B_WILL_DRAW | B_FRAME_EVENTS),
18 	fIsRunning(false),
19 	fBitmap(NULL)
20 {
21 	fPattern.data[0] = 0x0f;
22 	fPattern.data[1] = 0x1e;
23 	fPattern.data[2] = 0x3c;
24 	fPattern.data[3] = 0x78;
25 	fPattern.data[4] = 0xf0;
26 	fPattern.data[5] = 0xe1;
27 	fPattern.data[6] = 0xc3;
28 	fPattern.data[7] = 0x87;
29 
30 	SetExplicitMinSize(BSize(17, 17));
31 	SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 17));
32 };
33 
34 
35 Activity::~Activity()
36 {
37 	delete fBitmap;
38 }
39 
40 
41 void
42 Activity::AllAttached()
43 {
44 	SetViewColor(B_TRANSPARENT_COLOR);
45 	_CreateBitmap();
46 }
47 
48 
49 void
50 Activity::Start()
51 {
52 	fIsRunning = true;
53 	Window()->SetPulseRate(100000);
54 	SetFlags(Flags() | B_PULSE_NEEDED);
55 	Invalidate();
56 }
57 
58 
59 void
60 Activity::Pause()
61 {
62 	Window()->SetPulseRate(500000);
63 	SetFlags(Flags() & (~B_PULSE_NEEDED));
64 	Invalidate();
65 }
66 
67 
68 void
69 Activity::Stop()
70 {
71 	fIsRunning = false;
72 	Window()->SetPulseRate(500000);
73 	SetFlags(Flags() & (~B_PULSE_NEEDED));
74 	Invalidate();
75 }
76 
77 
78 bool
79 Activity::IsRunning()
80 {
81 	return fIsRunning;
82 }
83 
84 
85 void
86 Activity::Pulse()
87 {
88 	uchar tmp = fPattern.data[7];
89 
90 	for (int j = 7; j > 0; --j)
91 		fPattern.data[j] = fPattern.data[j - 1];
92 
93 	fPattern.data[0] = tmp;
94 
95 	Invalidate();
96 }
97 
98 
99 void
100 Activity::Draw(BRect rect)
101 {
102 	BRect viewRect = Bounds();
103 	BRect bitmapRect = fBitmap->Bounds();
104 
105 	if (bitmapRect != viewRect) {
106 		delete fBitmap;
107 		_CreateBitmap();
108 	}
109 
110 	_DrawOnBitmap(IsRunning());
111 	SetDrawingMode(B_OP_COPY);
112 	DrawBitmap(fBitmap);
113 }
114 
115 
116 void
117 Activity::_DrawOnBitmap(bool running)
118 {
119 	if (fBitmap->Lock())
120 	{
121 		BRect rect = fBitmap->Bounds();
122 
123 		fBitmapView->SetDrawingMode(B_OP_COPY);
124 
125 		rgb_color color;
126 		color.red = 0;
127 		color.green = 0;
128 		color.blue = 0;
129 		color.alpha = 255;
130 
131 		if (running)
132 			color.blue = 200;
133 
134 		fBitmapView->SetHighColor(color);
135 
136 		// draw the pole
137 		rect.InsetBy(2, 2);
138 		fBitmapView->FillRect(rect, fPattern);
139 
140 		// draw frame
141 
142 		// left
143 		color.red = 150;
144 		color.green = 150;
145 		color.blue = 150;
146 		fBitmapView->SetHighColor(color);
147 		fBitmapView->SetDrawingMode(B_OP_OVER);
148 		BPoint point_a = fBitmap->Bounds().LeftTop();
149 		BPoint point_b = fBitmap->Bounds().LeftBottom();
150 		point_b.y -= 1;
151 		fBitmapView->StrokeLine(point_a, point_b);
152 		point_a.x += 1;
153 		point_b.x += 1;
154 		point_b.y -= 1;
155 		fBitmapView->StrokeLine(point_a, point_b);
156 
157 		// top
158 		point_a = fBitmap->Bounds().LeftTop();
159 		point_b = fBitmap->Bounds().RightTop();
160 		point_b.x -= 1;
161 		fBitmapView->StrokeLine(point_a, point_b);
162 		point_a.y += 1;
163 		point_b.y += 1;
164 		point_b.x -= 1;
165 		fBitmapView->StrokeLine(point_a, point_b);
166 
167 		// right
168 		color.red = 255;
169 		color.green = 255;
170 		color.blue = 255;
171 		fBitmapView->SetHighColor(color);
172 		point_a = fBitmap->Bounds().RightTop();
173 		point_b = fBitmap->Bounds().RightBottom();
174 		fBitmapView->StrokeLine(point_a, point_b);
175 		point_a.y += 1;
176 		point_a.x -= 1;
177 		point_b.x -= 1;
178 		fBitmapView->StrokeLine(point_a, point_b);
179 
180 		// bottom
181 		point_a = fBitmap->Bounds().LeftBottom();
182 		point_b = fBitmap->Bounds().RightBottom();
183 		fBitmapView->StrokeLine(point_a, point_b);
184 		point_a.x += 1;
185 		point_a.y -= 1;
186 		point_b.y -= 1;
187 		fBitmapView->StrokeLine(point_a, point_b);
188 
189 		// some blending
190 		color.red = 150;
191 		color.green = 150;
192 		color.blue = 150;
193 		fBitmapView->SetHighColor(color);
194 		fBitmapView->SetDrawingMode(B_OP_SUBTRACT);
195 		fBitmapView->StrokeRect(rect);
196 
197 		rect.InsetBy(1, 1);
198 		_LightenBitmapHighColor(& color);
199 		fBitmapView->StrokeRect(rect);
200 
201 		rect.InsetBy(1, 1);
202 		_LightenBitmapHighColor(& color);
203 		fBitmapView->StrokeRect(rect);
204 
205 		rect.InsetBy(1, 1);
206 		_LightenBitmapHighColor(& color);
207 		fBitmapView->StrokeRect(rect);
208 
209 		rect.InsetBy(1, 1);
210 		_LightenBitmapHighColor(& color);
211 		fBitmapView->StrokeRect(rect);
212 
213 		fBitmapView->Sync();
214 		fBitmap->Unlock();
215 	}
216 }
217 
218 
219 void
220 Activity::_LightenBitmapHighColor(rgb_color* color)
221 {
222 	color->red -= 30;
223 	color->green -= 30;
224 	color->blue -= 30;
225 
226 	fBitmapView->SetHighColor(*color);
227 }
228 
229 
230 void
231 Activity::_CreateBitmap(void)
232 {
233 	BRect rect = Bounds();
234 	fBitmap = new BBitmap(rect, B_CMAP8, true);
235 	fBitmapView = new BView(Bounds(), "buffer", B_FOLLOW_NONE, 0);
236 	fBitmap->AddChild(fBitmapView);
237 }
238 
239 
240 void
241 Activity::FrameResized(float width, float height)
242 {
243 	delete fBitmap;
244 	_CreateBitmap();
245 	Invalidate();
246 }
247 
248