1 // main.cpp 2 3 #include <stdio.h> 4 #include <string.h> 5 6 #include <Application.h> 7 #include <Archivable.h> 8 #include <Dragger.h> 9 #include <File.h> 10 #include <Message.h> 11 #include <View.h> 12 #include <Window.h> 13 14 static const char* kAppSignature = "application/x.vnd-Haiku.ArchivedView"; 15 16 class TestView : public BView { 17 18 public: 19 TestView(BRect frame, const char* name, 20 uint32 resizeFlags, uint32 flags); 21 22 TestView(BMessage* archive); 23 24 virtual status_t Archive(BMessage* into, bool deep = true) const; 25 26 static BArchivable* Instantiate(BMessage* archive); 27 28 virtual void AttachedToWindow(); 29 virtual void DetachedFromWindow(); 30 31 virtual void Draw(BRect updateRect); 32 33 private: 34 int32 fData; 35 }; 36 37 38 // constructor 39 TestView::TestView(BRect frame, const char* name, 40 uint32 resizeFlags, uint32 flags) 41 : BView(frame, name, resizeFlags, flags), 42 fData(42) 43 { 44 SetViewColor(216, 216, 216); 45 46 BRect r = Bounds(); 47 r.top = r.bottom - 7; 48 r.left = r.right - 7; 49 BDragger *dw = new BDragger(r, this, 0); 50 AddChild(dw); 51 } 52 53 // constructor 54 TestView::TestView(BMessage* archive) 55 : BView(archive) 56 { 57 if (archive->FindInt32("data", &fData) < B_OK) 58 fData = 5; 59 60 printf("data: %ld\n", fData); 61 } 62 63 // AttachedToWindow 64 void 65 TestView::AttachedToWindow() 66 { 67 } 68 69 // DetachedFromWindow 70 void 71 TestView::DetachedFromWindow() 72 { 73 BFile file("/tmp/archived_view", 74 B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY); 75 status_t err = file.InitCheck(); 76 if (err != B_OK) { 77 printf("error creating file for archiving: %s\n", strerror(err)); 78 return; 79 } 80 BMessage archive; 81 err = Archive(&archive); 82 if (err != B_OK) { 83 printf("error archiving: %s\n", strerror(err)); 84 return; 85 } 86 err = archive.Flatten(&file); 87 if (err != B_OK) { 88 printf("error flattening: %s\n", strerror(err)); 89 return; 90 } 91 } 92 93 // Draw 94 void 95 TestView::Draw(BRect updateRect) 96 { 97 printf("TestView::Draw("); 98 updateRect.PrintToStream(); 99 BRect r(Bounds()); 100 101 rgb_color light = tint_color(ViewColor(), B_LIGHTEN_2_TINT); 102 rgb_color shadow = tint_color(ViewColor(), B_DARKEN_2_TINT); 103 104 BeginLineArray(4); 105 AddLine(r.LeftTop(), r.RightTop(), light); 106 AddLine(r.RightTop(), r.RightBottom(), shadow); 107 AddLine(r.RightBottom(), r.LeftBottom(), shadow); 108 AddLine(r.LeftBottom(), r.LeftTop(), light); 109 EndLineArray(); 110 } 111 112 // Archive 113 status_t 114 TestView::Archive(BMessage* into, bool deep = true) const 115 { 116 status_t ret = BView::Archive(into, deep); 117 118 if (ret == B_OK) 119 ret = into->AddInt32("data", fData); 120 121 if (ret == B_OK) 122 ret = into->AddString("add_on", kAppSignature); 123 124 if (ret == B_OK) 125 ret = into->AddString("class", "TestView"); 126 127 return ret; 128 } 129 130 // Instantiate 131 BArchivable * 132 TestView::Instantiate(BMessage* archive) 133 { 134 if (!validate_instantiation(archive , "TestView")) 135 return NULL; 136 137 return new TestView(archive); 138 } 139 140 // #pragma mark - 141 142 // show_window 143 void 144 show_window(BRect frame, const char* name) 145 { 146 BWindow* window = new BWindow(frame, name, 147 B_TITLED_WINDOW, 148 B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE); 149 150 BView* view = NULL; 151 BFile file("/tmp/archived_view", B_READ_ONLY); 152 if (file.InitCheck() == B_OK) { 153 printf("found archive file\n"); 154 BMessage archive; 155 if (archive.Unflatten(&file) == B_OK) { 156 printf("unflattened archive message\n"); 157 BArchivable* archivable = instantiate_object(&archive); 158 view = dynamic_cast<BView*>(archivable); 159 if (view) 160 printf("instatiated BView\n"); 161 } 162 } 163 164 if (!view) 165 view = new TestView(window->Bounds(), "test", B_FOLLOW_ALL, B_WILL_DRAW); 166 167 window->Show(); 168 169 window->Lock(); 170 window->AddChild(view); 171 window->Unlock(); 172 } 173 174 // main 175 int 176 main(int argc, char** argv) 177 { 178 BApplication* app = new BApplication(kAppSignature); 179 180 BRect frame(50.0, 50.0, 300.0, 250.0); 181 show_window(frame, "BView Archiving Test"); 182 183 app->Run(); 184 185 delete app; 186 return 0; 187 } 188