xref: /haiku/src/apps/gradients/GradientsView.cpp (revision d374a27286b8a52974a97dba0d5966ea026a665d)
1 /*
2  * Copyright (c) 2008-2009, Haiku, Inc.
3  * Distributed under the terms of the MIT license.
4  *
5  * Authors:
6  *		Artur Wyszynski <harakash@gmail.com>
7  */
8 
9 
10 #include "GradientsView.h"
11 #include <GradientLinear.h>
12 #include <GradientRadial.h>
13 #include <GradientRadialFocus.h>
14 #include <GradientDiamond.h>
15 #include <GradientConic.h>
16 
17 
18 GradientsView::GradientsView(const BRect &rect)
19 	: BView(rect, "gradientsview", B_FOLLOW_ALL, B_WILL_DRAW | B_PULSE_NEEDED),
20 	fType(BGradient::TYPE_LINEAR)
21 {
22 }
23 
24 
25 GradientsView::~GradientsView()
26 {
27 }
28 
29 
30 void
31 GradientsView::Draw(BRect update)
32 {
33 	switch (fType) {
34 		case BGradient::TYPE_LINEAR:
35 			DrawLinear(update);
36 			break;
37 
38 		case BGradient::TYPE_RADIAL:
39 			DrawRadial(update);
40 			break;
41 
42 		case BGradient::TYPE_RADIAL_FOCUS:
43 			DrawRadialFocus(update);
44 			break;
45 
46 		case BGradient::TYPE_DIAMOND:
47 			DrawDiamond(update);
48 			break;
49 
50 		case BGradient::TYPE_CONIC:
51 			DrawConic(update);
52 			break;
53 
54 		case BGradient::TYPE_NONE:
55 		default:
56 			break;
57 	}
58 }
59 
60 
61 void
62 GradientsView::DrawLinear(BRect update)
63 {
64 	BGradientLinear gradient;
65 	rgb_color c;
66 	c.red = 255;
67 	c.green = 0;
68 	c.blue = 0;
69 	gradient.AddColor(c, 0);
70 	c.red = 0;
71 	c.green = 255;
72 	c.blue = 0;
73 	gradient.AddColor(c, 127);
74 	c.red = 0;
75 	c.green = 0;
76 	c.blue = 255;
77 	gradient.AddColor(c, 255);
78 
79 	// RoundRect
80 	SetHighColor(0, 0, 0);
81 	FillRoundRect(BRect(10, 10, 110, 110), 5, 5);
82 	gradient.SetStart(BPoint(120, 10));
83 	gradient.SetEnd(BPoint(220, 110));
84 	FillRoundRect(BRect(120, 10, 220, 110), 5, 5, gradient);
85 
86 	// Rect
87 	SetHighColor(0, 0, 0);
88 	FillRect(BRect(10, 120, 110, 220));
89 	gradient.SetStart(BPoint(120, 120));
90 	gradient.SetEnd(BPoint(220, 220));
91 	FillRect(BRect(120, 120, 220, 220), gradient);
92 
93 	// Triangle
94 	SetHighColor(0, 0, 0);
95 	FillTriangle(BPoint(60, 230), BPoint(10, 330), BPoint(110, 330));
96 	gradient.SetStart(BPoint(60, 230));
97 	gradient.SetEnd(BPoint(60, 330));
98 	FillTriangle(BPoint(170, 230), BPoint(120, 330), BPoint(220, 330),
99 		gradient);
100 
101 	// Ellipse
102 	SetHighColor(0, 0, 0);
103 	FillEllipse(BPoint(60, 390), 50, 50);
104 	gradient.SetStart(BPoint(60, 340));
105 	gradient.SetEnd(BPoint(60, 440));
106 	FillEllipse(BPoint(170, 390), 50, 50, gradient);
107 }
108 
109 
110 void
111 GradientsView::DrawRadial(BRect update)
112 {
113 	BGradientRadial gradient;
114 	rgb_color c;
115 	c.red = 255;
116 	c.green = 0;
117 	c.blue = 0;
118 	gradient.AddColor(c, 0);
119 	c.red = 0;
120 	c.green = 255;
121 	c.blue = 0;
122 	gradient.AddColor(c, 127);
123 	c.red = 0;
124 	c.green = 0;
125 	c.blue = 255;
126 	gradient.AddColor(c, 255);
127 
128 	// RoundRect
129 	SetHighColor(0, 0, 0);
130 	FillRoundRect(BRect(10, 10, 110, 110), 5, 5);
131 	gradient.SetCenter(BPoint(170, 60));
132 	FillRoundRect(BRect(120, 10, 220, 110), 5, 5, gradient);
133 
134 	// Rect
135 	SetHighColor(0, 0, 0);
136 	FillRect(BRect(10, 120, 110, 220));
137 	gradient.SetCenter(BPoint(170, 170));
138 	FillRect(BRect(120, 120, 220, 220), gradient);
139 
140 	// Triangle
141 	SetHighColor(0, 0, 0);
142 	FillTriangle(BPoint(60, 230), BPoint(10, 330), BPoint(110, 330));
143 	gradient.SetCenter(BPoint(170, 280));
144 	FillTriangle(BPoint(170, 230), BPoint(120, 330), BPoint(220, 330),
145 		gradient);
146 
147 	// Ellipse
148 	SetHighColor(0, 0, 0);
149 	FillEllipse(BPoint(60, 390), 50, 50);
150 	gradient.SetCenter(BPoint(170, 390));
151 	FillEllipse(BPoint(170, 390), 50, 50, gradient);
152 }
153 
154 
155 void
156 GradientsView::DrawRadialFocus(BRect update)
157 {
158 	BGradientRadialFocus gradient;
159 	rgb_color c;
160 	c.red = 255;
161 	c.green = 0;
162 	c.blue = 0;
163 	gradient.AddColor(c, 0);
164 	c.red = 0;
165 	c.green = 255;
166 	c.blue = 0;
167 	gradient.AddColor(c, 127);
168 	c.red = 0;
169 	c.green = 0;
170 	c.blue = 255;
171 	gradient.AddColor(c, 255);
172 
173 	// RoundRect
174 	SetHighColor(0, 0, 0);
175 	FillRoundRect(BRect(10, 10, 110, 110), 5, 5);
176 	gradient.SetCenter(BPoint(170, 60));
177 	FillRoundRect(BRect(120, 10, 220, 110), 5, 5, gradient);
178 
179 	// Rect
180 	SetHighColor(0, 0, 0);
181 	FillRect(BRect(10, 120, 110, 220));
182 	gradient.SetCenter(BPoint(170, 170));
183 	FillRect(BRect(120, 120, 220, 220), gradient);
184 
185 	// Triangle
186 	SetHighColor(0, 0, 0);
187 	FillTriangle(BPoint(60, 230), BPoint(10, 330), BPoint(110, 330));
188 	gradient.SetCenter(BPoint(170, 280));
189 	FillTriangle(BPoint(170, 230), BPoint(120, 330), BPoint(220, 330),
190 		gradient);
191 
192 	// Ellipse
193 	SetHighColor(0, 0, 0);
194 	FillEllipse(BPoint(60, 390), 50, 50);
195 	gradient.SetCenter(BPoint(170, 390));
196 	FillEllipse(BPoint(170, 390), 50, 50, gradient);
197 }
198 
199 
200 void
201 GradientsView::DrawDiamond(BRect update)
202 {
203 	BGradientDiamond gradient;
204 	rgb_color c;
205 	c.red = 255;
206 	c.green = 0;
207 	c.blue = 0;
208 	gradient.AddColor(c, 0);
209 	c.red = 0;
210 	c.green = 255;
211 	c.blue = 0;
212 	gradient.AddColor(c, 127);
213 	c.red = 0;
214 	c.green = 0;
215 	c.blue = 255;
216 	gradient.AddColor(c, 255);
217 
218 	// RoundRect
219 	SetHighColor(0, 0, 0);
220 	FillRoundRect(BRect(10, 10, 110, 110), 5, 5);
221 	gradient.SetCenter(BPoint(170, 60));
222 	FillRoundRect(BRect(120, 10, 220, 110), 5, 5, gradient);
223 
224 	// Rect
225 	SetHighColor(0, 0, 0);
226 	FillRect(BRect(10, 120, 110, 220));
227 	gradient.SetCenter(BPoint(170, 170));
228 	FillRect(BRect(120, 120, 220, 220), gradient);
229 
230 	// Triangle
231 	SetHighColor(0, 0, 0);
232 	FillTriangle(BPoint(60, 230), BPoint(10, 330), BPoint(110, 330));
233 	gradient.SetCenter(BPoint(170, 280));
234 	FillTriangle(BPoint(170, 230), BPoint(120, 330), BPoint(220, 330),
235 		gradient);
236 
237 	// Ellipse
238 	SetHighColor(0, 0, 0);
239 	FillEllipse(BPoint(60, 390), 50, 50);
240 	gradient.SetCenter(BPoint(170, 390));
241 	FillEllipse(BPoint(170, 390), 50, 50, gradient);
242 }
243 
244 
245 void
246 GradientsView::DrawConic(BRect update)
247 {
248 	BGradientConic gradient;
249 	rgb_color c;
250 	c.red = 255;
251 	c.green = 0;
252 	c.blue = 0;
253 	gradient.AddColor(c, 0);
254 	c.red = 0;
255 	c.green = 255;
256 	c.blue = 0;
257 	gradient.AddColor(c, 127);
258 	c.red = 0;
259 	c.green = 0;
260 	c.blue = 255;
261 	gradient.AddColor(c, 255);
262 
263 	// RoundRect
264 	SetHighColor(0, 0, 0);
265 	FillRoundRect(BRect(10, 10, 110, 110), 5, 5);
266 	gradient.SetCenter(BPoint(170, 60));
267 	FillRoundRect(BRect(120, 10, 220, 110), 5, 5, gradient);
268 
269 	// Rect
270 	SetHighColor(0, 0, 0);
271 	FillRect(BRect(10, 120, 110, 220));
272 	gradient.SetCenter(BPoint(170, 170));
273 	FillRect(BRect(120, 120, 220, 220), gradient);
274 
275 	// Triangle
276 	SetHighColor(0, 0, 0);
277 	FillTriangle(BPoint(60, 230), BPoint(10, 330), BPoint(110, 330));
278 	gradient.SetCenter(BPoint(170, 280));
279 	FillTriangle(BPoint(170, 230), BPoint(120, 330), BPoint(220, 330),
280 		gradient);
281 
282 	// Ellipse
283 	SetHighColor(0, 0, 0);
284 	FillEllipse(BPoint(60, 390), 50, 50);
285 	gradient.SetCenter(BPoint(170, 390));
286 	FillEllipse(BPoint(170, 390), 50, 50, gradient);
287 }
288 
289 
290 void
291 GradientsView::SetType(BGradient::Type type)
292 {
293 	fType = type;
294 	Invalidate();
295 }
296