xref: /haiku/src/tests/kits/interface/picture/PictureTest.cpp (revision 93a78ecaa45114d68952d08c4778f073515102f2)
1 #include <Application.h>
2 #include <Box.h>
3 #include <Picture.h>
4 #include <Shape.h>
5 #include <View.h>
6 #include <Window.h>
7 
8 #include "SVGViewView.h"
9 
10 class Svg2PictureWindow : public BWindow {
11 public:
12 	Svg2PictureWindow(BRect frame, const char *filename)
13 		:   BWindow(frame, "Svg2Picture", B_TITLED_WINDOW, 0) {
14 
15         	BView *view = new Svg2PictureView(Bounds(), filename);
16         	AddChild(view);
17 	}
18 };
19 
20 class OriginalView : public BBox {
21 public:
22 	OriginalView(BRect frame);
23 	virtual void Draw(BRect update);
24 };
25 
26 
27 class PictureView : public BBox {
28 public:
29 	PictureView(BRect frame);
30 	~PictureView();
31 
32 	virtual void Draw(BRect update);
33 	virtual void AllAttached();
34 
35 private:
36 	BPicture *fPicture;
37 };
38 
39 
40 static void
41 DrawStuff(BView *view)
42 {
43 	// StrokeShape
44 	BShape shape;
45 	BPoint bezier[3] = {BPoint(100,0), BPoint(100, 100), BPoint(25, 50)};
46 	shape.MoveTo(BPoint(150,0));
47 	shape.LineTo(BPoint(200,100));
48 	shape.BezierTo(bezier);
49 	shape.Close();
50 	view->StrokeShape(&shape);
51 
52 	// Stroke/FillRect, Push/PopState, SetHighColor, SetLineMode, SetPenSize
53 	view->PushState();
54 	const rgb_color blue = { 0, 0, 240, 0 };
55 	view->SetHighColor(blue);
56 	view->SetLineMode(B_BUTT_CAP, B_BEVEL_JOIN);
57 	view->SetPenSize(7);
58 	view->StrokeRect(BRect(10, 220, 50, 260));
59 	view->FillRect(BRect(65, 245, 120, 300));
60 	view->PopState();
61 
62 	// Stroke/FillEllipse
63 	view->StrokeEllipse(BPoint(50, 150), 50, 50);
64 	view->FillEllipse(BPoint(100, 120), 50, 50);
65 
66 	// Stroke/FillArc
67 	view->StrokeArc(BRect(0, 200, 50, 250), 180, 180);
68 	view->FillArc(BPoint(150, 250), 50, 50, 0, 125);
69 
70 	// DrawString, SetHighColor, SetFontSize
71 	const rgb_color red = { 240, 0, 0, 0 };
72 	view->SetHighColor(red);
73 	view->SetFontSize(20);
74 	view->DrawString("BPicture test", BPoint(30, 20));
75 }
76 
77 
78 // OriginalView
79 OriginalView::OriginalView(BRect frame)
80 	:	BBox(frame, "original_view", B_FOLLOW_ALL_SIDES, B_WILL_DRAW)
81 {
82 }
83 
84 
85 void
86 OriginalView::Draw(BRect updateRect)
87 {
88 	DrawStuff(this);
89 }
90 
91 
92 // PictureView
93 PictureView::PictureView(BRect frame)
94 	:	BBox(frame, "pict_view", B_FOLLOW_ALL_SIDES, B_WILL_DRAW),
95 		fPicture(NULL)
96 {
97 }
98 
99 PictureView::~PictureView()
100 {
101 	delete fPicture;
102 }
103 
104 void
105 PictureView::AllAttached()
106 {
107 	BeginPicture(new BPicture);
108 
109 	DrawStuff(this);
110 
111 	BPicture *picture = EndPicture();
112 	if (picture == NULL)
113 		return;
114 
115 	BMessage message;
116 	picture->Archive(&message);
117 	message.PrintToStream();
118 
119 	BMallocIO stream;
120 
121 	status_t status = picture->Flatten(&stream);
122 	delete picture;
123 
124 	if (status != B_OK)
125 		printf("Error flattening BPicture: %s\n", strerror(status));
126 
127 	if (status == B_OK) {
128 		stream.Seek(0, SEEK_SET);
129 		fPicture = new BPicture();
130 		status = fPicture->Unflatten(&stream);
131 		if (status != B_OK)
132 			printf("Error unflattening BPicture: %s\n", strerror(status));
133 	}
134 
135 	BMessage message2;
136 	fPicture->Archive(&message2);
137 	message2.PrintToStream();
138 }
139 
140 void
141 PictureView::Draw(BRect update)
142 {
143 	if (fPicture)
144 		DrawPicture(fPicture, B_ORIGIN);
145 }
146 
147 
148 // main
149 int
150 main()
151 {
152 	BApplication pictureApp("application/x-vnd.picture");
153 	BWindow *pictureWindow = new BWindow(BRect(100, 100, 500, 400),
154 				"BPicture test", B_TITLED_WINDOW,
155 				B_NOT_RESIZABLE|B_NOT_ZOOMABLE|B_QUIT_ON_WINDOW_CLOSE);
156 
157 	BWindow *svgWindow = new Svg2PictureWindow(BRect(300, 300, 600, 600), "/boot/beos/etc/artwork/lion.svg");
158 
159 
160 	BRect rect(pictureWindow->Bounds());
161 	rect.right -= (rect.Width() + 1) / 2;
162 	OriginalView *testView = new OriginalView(rect);
163 
164 	rect.OffsetBy(rect.Width() + 1, 0);
165 	PictureView *pictureView = new PictureView(rect);
166 
167 	pictureWindow->AddChild(testView);
168 	pictureWindow->AddChild(pictureView);
169 
170 	pictureWindow->Show();
171 	svgWindow->Show();
172 
173 	pictureApp.Run();
174 
175 	return 0;
176 }
177 
178