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 test", BPoint(30, 20)); 81 } 82 83 84 // OriginalView 85 OriginalView::OriginalView(BRect frame) 86 : BBox(frame, "original_view", B_FOLLOW_ALL_SIDES, B_WILL_DRAW) 87 { 88 } 89 90 91 void 92 OriginalView::Draw(BRect updateRect) 93 { 94 DrawStuff(this); 95 } 96 97 98 // PictureView 99 PictureView::PictureView(BRect frame) 100 : BBox(frame, "pict_view", B_FOLLOW_ALL_SIDES, B_WILL_DRAW), 101 fPicture(NULL) 102 { 103 } 104 105 PictureView::~PictureView() 106 { 107 delete fPicture; 108 } 109 110 void 111 PictureView::AllAttached() 112 { 113 BeginPicture(new BPicture); 114 115 DrawStuff(this); 116 117 BPicture *picture = EndPicture(); 118 if (picture == NULL) 119 return; 120 121 BMessage message; 122 picture->Archive(&message); 123 message.PrintToStream(); 124 125 BMallocIO stream; 126 127 status_t status = picture->Flatten(&stream); 128 delete picture; 129 130 if (status != B_OK) 131 printf("Error flattening BPicture: %s\n", strerror(status)); 132 133 if (status == B_OK) { 134 stream.Seek(0, SEEK_SET); 135 fPicture = new BPicture(); 136 status = fPicture->Unflatten(&stream); 137 if (status != B_OK) { 138 printf("Error unflattening BPicture: %s\n", strerror(status)); 139 return; 140 } 141 } 142 143 BMessage message2; 144 fPicture->Archive(&message2); 145 message2.PrintToStream(); 146 } 147 148 149 void 150 PictureView::Draw(BRect update) 151 { 152 if (fPicture) 153 DrawPicture(fPicture, B_ORIGIN); 154 } 155 156 157 // #pragma mark - 158 159 160 int 161 main() 162 { 163 BApplication pictureApp("application/x-vnd.picture"); 164 165 BWindow *pictureWindow = new BWindow(BRect(100, 100, 500, 400), 166 "BPicture test", B_TITLED_WINDOW, 167 B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_QUIT_ON_WINDOW_CLOSE); 168 169 BRect rect(pictureWindow->Bounds()); 170 rect.right -= (rect.Width() + 1) / 2; 171 OriginalView *testView = new OriginalView(rect); 172 173 rect.OffsetBy(rect.Width() + 1, 0); 174 PictureView *pictureView = new PictureView(rect); 175 176 pictureWindow->AddChild(testView); 177 pictureWindow->AddChild(pictureView); 178 pictureWindow->Show(); 179 180 BPath path; 181 if (find_directory(B_SYSTEM_DATA_DIRECTORY, &path) == B_OK) { 182 path.Append("artwork/lion.svg"); 183 BEntry entry(path.Path()); 184 if (entry.Exists()) { 185 BWindow *svgWindow = new Svg2PictureWindow(BRect(300, 300, 600, 600), 186 path.Path()); 187 svgWindow->Show(); 188 } 189 } 190 191 pictureApp.Run(); 192 return 0; 193 } 194 195