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