1260f2d09SPhilippe Houdoin /*
2260f2d09SPhilippe Houdoin * Copyright 2011, Haiku.
3260f2d09SPhilippe Houdoin * Distributed under the terms of the MIT License.
4260f2d09SPhilippe Houdoin *
5260f2d09SPhilippe Houdoin * Authors:
6260f2d09SPhilippe Houdoin * Philippe Houdoin
7260f2d09SPhilippe Houdoin */
8260f2d09SPhilippe Houdoin
9260f2d09SPhilippe Houdoin
10260f2d09SPhilippe Houdoin #include "TestPageView.h"
11260f2d09SPhilippe Houdoin
12bc597290SPhilippe Houdoin #include <math.h>
13bc597290SPhilippe Houdoin
14bc597290SPhilippe Houdoin #include <AffineTransform.h>
15260f2d09SPhilippe Houdoin #include <Catalog.h>
16bc597290SPhilippe Houdoin #include <Font.h>
17260f2d09SPhilippe Houdoin #include <GradientLinear.h>
18bc597290SPhilippe Houdoin #include <GradientRadial.h>
19bc597290SPhilippe Houdoin #include <GridLayoutBuilder.h>
20bc597290SPhilippe Houdoin #include <GroupLayoutBuilder.h>
21bc597290SPhilippe Houdoin #include <LayoutUtils.h>
22bc597290SPhilippe Houdoin #include <TextView.h>
23bc597290SPhilippe Houdoin #include <Shape.h>
24bc597290SPhilippe Houdoin #include <String.h>
25bc597290SPhilippe Houdoin #include <StringView.h>
26260f2d09SPhilippe Houdoin
27260f2d09SPhilippe Houdoin #include "PrinterListView.h"
28260f2d09SPhilippe Houdoin
29260f2d09SPhilippe Houdoin
30546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT
31546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "TestPageView"
32260f2d09SPhilippe Houdoin
33260f2d09SPhilippe Houdoin
34bc597290SPhilippe Houdoin // #pragma mark LeafView
35bc597290SPhilippe Houdoin
36bc597290SPhilippe Houdoin
37bc597290SPhilippe Houdoin // path data for the leaf shape
38bc597290SPhilippe Houdoin static const BPoint kLeafBegin(56.24793f, 15.46287f);
39bc597290SPhilippe Houdoin static BPoint kLeafCurves[][3] = {
40bc597290SPhilippe Houdoin { BPoint(61.14, 28.89), BPoint(69.78, 38.25), BPoint(83.48, 44.17) },
41bc597290SPhilippe Houdoin { BPoint(99.46, 37.52), BPoint(113.27, 29.61), BPoint(134.91, 30.86) },
42bc597290SPhilippe Houdoin { BPoint(130.58, 36.53), BPoint(126.74, 42.44), BPoint(123.84, 48.81) },
43bc597290SPhilippe Houdoin { BPoint(131.81, 42.22), BPoint(137.53, 38.33), BPoint(144.37, 33.10) },
44bc597290SPhilippe Houdoin { BPoint(169.17, 23.55), BPoint(198.90, 15.55), BPoint(232.05, 10.51) },
45bc597290SPhilippe Houdoin { BPoint(225.49, 18.37), BPoint(219.31, 28.17), BPoint(217.41, 40.24) },
46bc597290SPhilippe Houdoin { BPoint(227.70, 26.60), BPoint(239.97, 14.63), BPoint(251.43, 8.36) },
47bc597290SPhilippe Houdoin { BPoint(288.89, 9.12), BPoint(322.73, 14.33), BPoint(346.69, 31.67) },
48bc597290SPhilippe Houdoin { BPoint(330.49, 37.85), BPoint(314.36, 44.25), BPoint(299.55, 54.17) },
49bc597290SPhilippe Houdoin { BPoint(292.48, 52.54), BPoint(289.31, 49.70), BPoint(285.62, 47.03) },
50bc597290SPhilippe Houdoin { BPoint(283.73, 54.61), BPoint(284.46, 57.94), BPoint(285.62, 60.60) },
51bc597290SPhilippe Houdoin { BPoint(259.78, 76.14), BPoint(233.24, 90.54), BPoint(202.41, 98.10) },
52bc597290SPhilippe Houdoin { BPoint(194.43, 95.36), BPoint(185.96, 92.39), BPoint(179.63, 88.33) },
53bc597290SPhilippe Houdoin { BPoint(180.15, 94.75), BPoint(182.73, 99.76), BPoint(185.62, 104.53) },
54bc597290SPhilippe Houdoin { BPoint(154.83, 119.46), BPoint(133.21, 118.97), BPoint(125.62, 94.88) },
55bc597290SPhilippe Houdoin { BPoint(124.70, 98.79), BPoint(124.11, 103.67), BPoint(124.19, 110.60) },
56bc597290SPhilippe Houdoin { BPoint(116.42, 111.81), BPoint(85.82, 99.60), BPoint(83.25, 51.96) },
57bc597290SPhilippe Houdoin { BPoint(62.50, 42.57), BPoint(58.12, 33.18), BPoint(50.98, 23.81) } };
58bc597290SPhilippe Houdoin static const int kNumLeafCurves = sizeof(kLeafCurves) / sizeof(kLeafCurves[0]);
59bc597290SPhilippe Houdoin static const float kLeafWidth = 372.f;
60bc597290SPhilippe Houdoin static const float kLeafHeight = 121.f;
61bc597290SPhilippe Houdoin
62bc597290SPhilippe Houdoin
63bc597290SPhilippe Houdoin class LeafView : public BView {
64bc597290SPhilippe Houdoin public:
65bc597290SPhilippe Houdoin LeafView();
66bc597290SPhilippe Houdoin virtual void Draw(BRect updateRect);
67bc597290SPhilippe Houdoin };
68bc597290SPhilippe Houdoin
69bc597290SPhilippe Houdoin
LeafView()70bc597290SPhilippe Houdoin LeafView::LeafView()
71bc597290SPhilippe Houdoin :
72bc597290SPhilippe Houdoin BView("leafview", B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE)
73bc597290SPhilippe Houdoin {
74bc597290SPhilippe Houdoin SetViewColor(255, 255, 255);
75bc597290SPhilippe Houdoin }
76bc597290SPhilippe Houdoin
77bc597290SPhilippe Houdoin
78bc597290SPhilippe Houdoin void
Draw(BRect updateRect)79bc597290SPhilippe Houdoin LeafView::Draw(BRect updateRect)
80bc597290SPhilippe Houdoin {
81bc597290SPhilippe Houdoin float scale = Bounds().Width() / kLeafWidth;
82bc597290SPhilippe Houdoin BAffineTransform transform;
83bc597290SPhilippe Houdoin transform.ScaleBy(scale);
84bc597290SPhilippe Houdoin
85bc597290SPhilippe Houdoin // BGradientRadial gradient(BPoint(kLeafWidth * 0.75, kLeafHeight * 1.5),
86bc597290SPhilippe Houdoin // kLeafWidth * 2);
87bc597290SPhilippe Houdoin BGradientLinear gradient(B_ORIGIN,
88bc597290SPhilippe Houdoin transform.Apply(BPoint(kLeafWidth, kLeafHeight)));
89bc597290SPhilippe Houdoin rgb_color lightBlue = make_color(6, 169, 255);
90bc597290SPhilippe Houdoin rgb_color darkBlue = make_color(0, 50, 126);
91bc597290SPhilippe Houdoin gradient.AddColor(darkBlue, 0.0);
92bc597290SPhilippe Houdoin gradient.AddColor(lightBlue, 255.0);
93bc597290SPhilippe Houdoin
94bc597290SPhilippe Houdoin // build leaf shape
95bc597290SPhilippe Houdoin BShape leafShape;
96bc597290SPhilippe Houdoin leafShape.MoveTo(transform.Apply(kLeafBegin));
97bc597290SPhilippe Houdoin for (int i = 0; i < kNumLeafCurves; ++i) {
98bc597290SPhilippe Houdoin BPoint controlPoints[3];
99bc597290SPhilippe Houdoin for (int j = 0; j < 3; ++j)
100bc597290SPhilippe Houdoin controlPoints[j] = transform.Apply(kLeafCurves[i][j]);
101bc597290SPhilippe Houdoin leafShape.BezierTo(controlPoints);
102bc597290SPhilippe Houdoin }
103bc597290SPhilippe Houdoin leafShape.Close();
104bc597290SPhilippe Houdoin
105bc597290SPhilippe Houdoin PushState();
106bc597290SPhilippe Houdoin SetDrawingMode(B_OP_ALPHA);
107bc597290SPhilippe Houdoin SetHighColor(0, 0, 0, 50);
108bc597290SPhilippe Houdoin for (int i = 2; i >= 0; --i) {
109bc597290SPhilippe Houdoin SetOrigin(i * 0.1, i * 0.3);
110bc597290SPhilippe Houdoin SetPenSize(i * 2);
111bc597290SPhilippe Houdoin StrokeShape(&leafShape);
112bc597290SPhilippe Houdoin }
113bc597290SPhilippe Houdoin PopState();
114bc597290SPhilippe Houdoin
115bc597290SPhilippe Houdoin FillShape(&leafShape, gradient);
116bc597290SPhilippe Houdoin }
117bc597290SPhilippe Houdoin
118bc597290SPhilippe Houdoin
119bc597290SPhilippe Houdoin // #pragma mark -
120bc597290SPhilippe Houdoin
121bc597290SPhilippe Houdoin
122bc597290SPhilippe Houdoin class RadialLinesView : public BView {
123bc597290SPhilippe Houdoin public:
124bc597290SPhilippe Houdoin RadialLinesView();
125bc597290SPhilippe Houdoin
126bc597290SPhilippe Houdoin virtual void Draw(BRect updateRect);
127bc597290SPhilippe Houdoin
HasHeightForWidth()128bc597290SPhilippe Houdoin virtual bool HasHeightForWidth() { return true; }
129bc597290SPhilippe Houdoin virtual void GetHeightForWidth(float width, float* min,
130bc597290SPhilippe Houdoin float* max, float* preferred);
131bc597290SPhilippe Houdoin };
132bc597290SPhilippe Houdoin
133bc597290SPhilippe Houdoin
RadialLinesView()134bc597290SPhilippe Houdoin RadialLinesView::RadialLinesView()
135bc597290SPhilippe Houdoin : BView("radiallinesview", B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE)
136bc597290SPhilippe Houdoin {
137bc597290SPhilippe Houdoin SetViewColor(255, 255, 255);
138bc597290SPhilippe Houdoin }
139bc597290SPhilippe Houdoin
140bc597290SPhilippe Houdoin
141bc597290SPhilippe Houdoin void
GetHeightForWidth(float width,float * min,float * max,float * preferred)142bc597290SPhilippe Houdoin RadialLinesView::GetHeightForWidth(float width,
143bc597290SPhilippe Houdoin float* min, float* max, float* preferred)
144bc597290SPhilippe Houdoin {
145bc597290SPhilippe Houdoin // Enforce a width/height ratio of 1
146bc597290SPhilippe Houdoin
147bc597290SPhilippe Houdoin if (min)
148bc597290SPhilippe Houdoin *min = width;
149bc597290SPhilippe Houdoin
150bc597290SPhilippe Houdoin if (max)
151bc597290SPhilippe Houdoin *max = width;
152bc597290SPhilippe Houdoin
153bc597290SPhilippe Houdoin if (preferred)
154bc597290SPhilippe Houdoin *preferred = width;
155bc597290SPhilippe Houdoin }
156bc597290SPhilippe Houdoin
157bc597290SPhilippe Houdoin
158bc597290SPhilippe Houdoin void
Draw(BRect updateRect)159bc597290SPhilippe Houdoin RadialLinesView::Draw(BRect updateRect)
160bc597290SPhilippe Houdoin {
161bc597290SPhilippe Houdoin const rgb_color black = { 0, 0, 0, 255 };
162bc597290SPhilippe Houdoin const int angleStep = 4;
163bc597290SPhilippe Houdoin
164bc597290SPhilippe Houdoin BRect rect(Bounds());
165bc597290SPhilippe Houdoin float size = rect.Width();
166bc597290SPhilippe Houdoin if (size > rect.Height())
167bc597290SPhilippe Houdoin size = rect.Height();
168bc597290SPhilippe Houdoin size *= 0.45; // leave 10% of margin
169bc597290SPhilippe Houdoin
170bc597290SPhilippe Houdoin BPoint center(rect.Width() / 2, rect.Height() / 2);
171bc597290SPhilippe Houdoin
172bc597290SPhilippe Houdoin BeginLineArray(360 / angleStep);
173bc597290SPhilippe Houdoin for (int i = 0; i < 360; i += angleStep) {
174bc597290SPhilippe Houdoin double angle = i * M_PI / 180;
175bc597290SPhilippe Houdoin BPoint pt(size * cos(angle), size * sin(angle));
176bc597290SPhilippe Houdoin AddLine(center, center + pt, black);
177bc597290SPhilippe Houdoin }
178bc597290SPhilippe Houdoin EndLineArray();
179bc597290SPhilippe Houdoin }
180bc597290SPhilippe Houdoin
181bc597290SPhilippe Houdoin
182bc597290SPhilippe Houdoin // #pragma mark -
183bc597290SPhilippe Houdoin
184bc597290SPhilippe Houdoin
185bc597290SPhilippe Houdoin static const struct {
186bc597290SPhilippe Houdoin const char* name;
187bc597290SPhilippe Houdoin rgb_color color;
188bc597290SPhilippe Houdoin } kColorGradients[] = {
189bc597290SPhilippe Houdoin { B_TRANSLATE_MARK("Red"), {255, 0, 0, 255} },
190bc597290SPhilippe Houdoin { B_TRANSLATE_MARK("Green"), {0, 255, 0, 255} },
191bc597290SPhilippe Houdoin { B_TRANSLATE_MARK("Blue"), {0, 0, 255, 255} },
192bc597290SPhilippe Houdoin { B_TRANSLATE_MARK("Yellow"), {255, 255, 0, 255} },
193bc597290SPhilippe Houdoin { B_TRANSLATE_MARK("Magenta"), {255, 0, 255, 255} },
194bc597290SPhilippe Houdoin { B_TRANSLATE_MARK("Cyan"), {0, 255, 255, 255} },
195bc597290SPhilippe Houdoin { B_TRANSLATE_MARK("Black"), {0, 0, 0, 255} }
196bc597290SPhilippe Houdoin };
197bc597290SPhilippe Houdoin static const int kNumColorGradients = sizeof(kColorGradients)
198bc597290SPhilippe Houdoin / sizeof(kColorGradients[0]);
199bc597290SPhilippe Houdoin
200bc597290SPhilippe Houdoin
201bc597290SPhilippe Houdoin class ColorGradientView : public BView {
202bc597290SPhilippe Houdoin public:
203bc597290SPhilippe Houdoin ColorGradientView(rgb_color color);
204bc597290SPhilippe Houdoin virtual void Draw(BRect updateRect);
205bc597290SPhilippe Houdoin
206bc597290SPhilippe Houdoin private:
207bc597290SPhilippe Houdoin rgb_color fColor;
208bc597290SPhilippe Houdoin };
209bc597290SPhilippe Houdoin
210bc597290SPhilippe Houdoin
ColorGradientView(rgb_color color)211bc597290SPhilippe Houdoin ColorGradientView::ColorGradientView(rgb_color color)
212bc597290SPhilippe Houdoin :
213bc597290SPhilippe Houdoin BView("colorgradientview", B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
214bc597290SPhilippe Houdoin fColor(color)
215260f2d09SPhilippe Houdoin {
216260f2d09SPhilippe Houdoin }
217260f2d09SPhilippe Houdoin
218260f2d09SPhilippe Houdoin
219260f2d09SPhilippe Houdoin void
Draw(BRect updateRect)220bc597290SPhilippe Houdoin ColorGradientView::Draw(BRect updateRect)
221260f2d09SPhilippe Houdoin {
222bc597290SPhilippe Houdoin BRect rect(Bounds());
223260f2d09SPhilippe Houdoin
224bc597290SPhilippe Houdoin BGradientLinear gradient(rect.LeftTop(), rect.RightBottom());
225bc597290SPhilippe Houdoin rgb_color white = make_color(255, 255, 255);
226bc597290SPhilippe Houdoin gradient.AddColor(white, 0.0);
227bc597290SPhilippe Houdoin gradient.AddColor(fColor, 255.0);
228bc597290SPhilippe Houdoin
229bc597290SPhilippe Houdoin FillRect(rect, gradient);
230bc597290SPhilippe Houdoin StrokeRect(rect);
231bc597290SPhilippe Houdoin }
232bc597290SPhilippe Houdoin
233bc597290SPhilippe Houdoin
234bc597290SPhilippe Houdoin // #pragma mark -
235bc597290SPhilippe Houdoin
236bc597290SPhilippe Houdoin
TestPageView(BRect frame,PrinterItem * printer)237bc597290SPhilippe Houdoin TestPageView::TestPageView(BRect frame, PrinterItem* printer)
2389c87cb1bSPhilippe Houdoin : BView(frame, "testpage", B_FOLLOW_ALL,
2399c87cb1bSPhilippe Houdoin B_DRAW_ON_CHILDREN | B_FULL_UPDATE_ON_RESIZE),
240bc597290SPhilippe Houdoin fPrinter(printer)
241bc597290SPhilippe Houdoin {
242bc597290SPhilippe Houdoin SetViewColor(255, 255, 255);
243bc597290SPhilippe Houdoin }
244bc597290SPhilippe Houdoin
245bc597290SPhilippe Houdoin
246bc597290SPhilippe Houdoin void
AttachedToWindow()247bc597290SPhilippe Houdoin TestPageView::AttachedToWindow()
248bc597290SPhilippe Houdoin {
249bc597290SPhilippe Houdoin BTextView* statusView = new BTextView("statusView",
250bc597290SPhilippe Houdoin be_plain_font, NULL, B_WILL_DRAW);
251bc597290SPhilippe Houdoin
252bc597290SPhilippe Houdoin statusView->SetInsets(10, 10, 10, 10);
253bc597290SPhilippe Houdoin statusView->MakeEditable(false);
254bc597290SPhilippe Houdoin statusView->MakeSelectable(false);
255bc597290SPhilippe Houdoin
2565f050a9eSHumdinger const char* title = B_TRANSLATE("Test page");
257bc597290SPhilippe Houdoin BString text;
258bc597290SPhilippe Houdoin text << title << "\n\n";
259bc597290SPhilippe Houdoin text << B_TRANSLATE(
260bc597290SPhilippe Houdoin "Printer: %printer_name%\n"
261bc597290SPhilippe Houdoin "Driver: %driver%\n");
262bc597290SPhilippe Houdoin
263bc597290SPhilippe Houdoin text.ReplaceFirst("%printer_name%", fPrinter->Name());
264bc597290SPhilippe Houdoin text.ReplaceFirst("%driver%", fPrinter->Driver());
265bc597290SPhilippe Houdoin if (strlen(fPrinter->Transport()) > 0) {
266bc597290SPhilippe Houdoin text << B_TRANSLATE("Transport: %transport% %transport_address%");
267bc597290SPhilippe Houdoin
268bc597290SPhilippe Houdoin text.ReplaceFirst("%transport%", fPrinter->Transport());
269bc597290SPhilippe Houdoin text.ReplaceFirst("%transport_address%", fPrinter->TransportAddress());
270bc597290SPhilippe Houdoin }
271bc597290SPhilippe Houdoin
272bc597290SPhilippe Houdoin statusView->SetText(text.String());
273bc597290SPhilippe Houdoin BFont font;
274bc597290SPhilippe Houdoin statusView->SetStylable(true);
275bc597290SPhilippe Houdoin statusView->GetFont(&font);
276bc597290SPhilippe Houdoin font.SetFace(B_BOLD_FACE);
277bc597290SPhilippe Houdoin font.SetSize(font.Size() * 1.7);
278bc597290SPhilippe Houdoin statusView->SetFontAndColor(0, strlen(title), &font);
279bc597290SPhilippe Houdoin
280*d0e9e3caSAdrien Destugues BGridLayoutBuilder gradients(2.0);
281*d0e9e3caSAdrien Destugues gradients.View()->SetViewColor(B_TRANSPARENT_COLOR);
282bc597290SPhilippe Houdoin
283bc597290SPhilippe Houdoin for (int i = 0; i < kNumColorGradients; ++i) {
284a1c2394aSjjpx BStringView* label = new BStringView(
285a1c2394aSjjpx kColorGradients[i].name,
286a1c2394aSjjpx B_TRANSLATE(kColorGradients[i].name));
287bc597290SPhilippe Houdoin // label->SetAlignment(B_ALIGN_RIGHT);
288*d0e9e3caSAdrien Destugues gradients.Add(label, 0, i);
289*d0e9e3caSAdrien Destugues gradients.Add(new ColorGradientView(kColorGradients[i].color), 1, i);
290bc597290SPhilippe Houdoin }
291bc597290SPhilippe Houdoin
292bc597290SPhilippe Houdoin SetLayout(new BGroupLayout(B_HORIZONTAL));
293bc597290SPhilippe Houdoin AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
294bc597290SPhilippe Houdoin .Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
295bc597290SPhilippe Houdoin .Add(statusView)
296bc597290SPhilippe Houdoin .Add(new LeafView())
297bc597290SPhilippe Houdoin )
298bc597290SPhilippe Houdoin .Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
299*d0e9e3caSAdrien Destugues .Add(gradients, 0.60)
300bc597290SPhilippe Houdoin .Add(new RadialLinesView(), 0.40)
301bc597290SPhilippe Houdoin )
302bc597290SPhilippe Houdoin .AddGlue()
303bc597290SPhilippe Houdoin .End()
304bc597290SPhilippe Houdoin );
305bc597290SPhilippe Houdoin
306bc597290SPhilippe Houdoin // set layout background color to transparent instead
307bc597290SPhilippe Houdoin // of default UI panel color
308bc597290SPhilippe Houdoin ChildAt(0)->SetViewColor(B_TRANSPARENT_COLOR);
309bc597290SPhilippe Houdoin }
310bc597290SPhilippe Houdoin
311bc597290SPhilippe Houdoin
312bc597290SPhilippe Houdoin void
DrawAfterChildren(BRect updateRect)313bc597290SPhilippe Houdoin TestPageView::DrawAfterChildren(BRect updateRect)
314bc597290SPhilippe Houdoin {
315bc597290SPhilippe Houdoin // Draw corners marks
316260f2d09SPhilippe Houdoin
317260f2d09SPhilippe Houdoin float width = Bounds().Width();
318260f2d09SPhilippe Houdoin float height = Bounds().Height();
319260f2d09SPhilippe Houdoin float minDimension = MIN(width, height);
320260f2d09SPhilippe Houdoin
321bc597290SPhilippe Houdoin float size = minDimension * 0.05;
322260f2d09SPhilippe Houdoin
3239c87cb1bSPhilippe Houdoin SetPenSize(3.0);
3249c87cb1bSPhilippe Houdoin
325260f2d09SPhilippe Houdoin BPoint pt = Bounds().LeftTop();
326260f2d09SPhilippe Houdoin StrokeLine(pt, BPoint(pt.x + size, pt.y));
327260f2d09SPhilippe Houdoin StrokeLine(pt, BPoint(pt.x, pt.y + size));
328260f2d09SPhilippe Houdoin
329260f2d09SPhilippe Houdoin pt = Bounds().RightTop();
330260f2d09SPhilippe Houdoin StrokeLine(pt, BPoint(pt.x - size, pt.y));
331260f2d09SPhilippe Houdoin StrokeLine(pt, BPoint(pt.x, pt.y + size));
332260f2d09SPhilippe Houdoin
333260f2d09SPhilippe Houdoin pt = Bounds().RightBottom();
334260f2d09SPhilippe Houdoin StrokeLine(pt, BPoint(pt.x - size, pt.y));
335260f2d09SPhilippe Houdoin StrokeLine(pt, BPoint(pt.x, pt.y - size));
336260f2d09SPhilippe Houdoin
337260f2d09SPhilippe Houdoin pt = Bounds().LeftBottom();
338260f2d09SPhilippe Houdoin StrokeLine(pt, BPoint(pt.x + size, pt.y));
339260f2d09SPhilippe Houdoin StrokeLine(pt, BPoint(pt.x, pt.y - size));
340260f2d09SPhilippe Houdoin }
341