1 //******************************************************************************
2 //
3 //******************************************************************************
4
5 /*
6 Copyright 1999, Be Incorporated. All Rights Reserved.
7 This file may be used under the terms of the Be Sample Code License.
8 */
9
10 #define DEBUG 1
11 #include <Debug.h>
12
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <stdlib.h>
17
18 #include <Application.h>
19 #include <Window.h>
20 #include <Box.h>
21 #include <File.h>
22
23 #include <Path.h>
24 #include <Entry.h>
25 #include <FindDirectory.h>
26 #include <Shelf.h>
27 #include <ByteOrder.h>
28
29 #define MAGIC_1 'pjpp'
30 #define MAGIC_2 'jahh'
31 #define MAGIC_1_SWAP 'ppjp'
32 #define MAGIC_2_SWAP 'hhaj'
33
34 /*------------------------------------------------------------*/
35 BEntry *archive_file(bool create = TRUE);
36
37 class TWindow : public BWindow {
38 public:
39 TWindow(BRect frame, const char *title, BPositionIO *stream);
40 ~TWindow();
41 virtual bool QuitRequested();
42 virtual void Quit();
43
44 private:
45 BBox *fMainView;
46 BShelf *fShelf;
47 BPositionIO *fArchiveStream;
48 };
49
50 /*------------------------------------------------------------*/
51
main(int argc,char * argv[])52 int main(int argc, char* argv[])
53 {
54 BApplication *app = new BApplication("application/x-vnd.Be-MYTE");
55 BEntry *entry;
56 BRect frame;
57 bool frame_set = FALSE;
58 TWindow *w = NULL;
59 BFile *stream = NULL;
60
61 if ((entry = archive_file())) {
62 long d1 = 0;
63 long d2 = 0;
64 long err;
65
66 stream = new BFile(entry, O_RDWR);
67
68 // need to consider Endian issues here!
69 err = stream->Read(&d1, sizeof(d1));
70 err = stream->Read(&d2, sizeof(d2));
71
72 if ((d1 == MAGIC_1) && (d2 == MAGIC_2)) {
73 err = stream->Read(&frame, sizeof(frame));
74 frame_set = TRUE;
75 } else if ((d1 == MAGIC_1_SWAP) &&
76 (d2 == MAGIC_2_SWAP)) {
77 err = stream->Read(&frame, sizeof(frame));
78 swap_data(B_RECT_TYPE, &frame, sizeof(BRect), B_SWAP_ALWAYS);
79 frame_set = TRUE;
80 }
81 delete entry;
82 }
83 if (!frame_set)
84 frame = BRect(100, 50, 300, 400);
85
86 w = new TWindow(frame, "Container", stream);
87 w->Show();
88 app->Run();
89 delete stream;
90 delete app;
91 return 0;
92 }
93
94 /* ---------------------------------------------------------------- */
95
archive_file(bool create)96 BEntry *archive_file(bool create)
97 {
98 BPath path;
99 BEntry *entry = NULL;
100 // long err;
101
102
103 if (find_directory (B_USER_SETTINGS_DIRECTORY, &path, true) != B_OK)
104 return NULL;
105 path.Append ("Container_data");
106
107 if (create) {
108 int fd;
109 fd = open(path.Path(), O_RDWR);
110 if (fd < 0)
111 fd = creat(path.Path(), 0777);
112 if (fd > 0)
113 close(fd);
114 }
115
116 entry = new BEntry(path.Path());
117 if (entry->InitCheck() != B_NO_ERROR) {
118 delete entry;
119 entry = NULL;
120 }
121 return entry;
122 }
123
124 /*------------------------------------------------------------*/
125
TWindow(BRect frame,const char * title,BPositionIO * stream)126 TWindow::TWindow(BRect frame, const char *title, BPositionIO *stream)
127 : BWindow(frame, title, B_TITLED_WINDOW, 0)
128 {
129 BRect b;
130 Lock();
131
132 b = frame;
133 b.OffsetTo(B_ORIGIN);
134
135 BView *parent = new BBox(b, "parent", B_FOLLOW_ALL, B_WILL_DRAW | B_FRAME_EVENTS, B_PLAIN_BORDER);
136 parent->SetViewColor(216, 216, 216, 0);
137 AddChild(parent);
138
139 fArchiveStream = stream;
140
141 b.InsetBy(5,5);
142 fMainView = new BBox(b, "MainView", B_FOLLOW_ALL);
143 fMainView->SetViewColor(216, 216, 216, 0);
144 fMainView->SetLabel("The Drop Zone");
145 parent->AddChild(fMainView);
146
147 fShelf = new BShelf(fArchiveStream, fMainView);
148 fShelf->SetDisplaysZombies(true);
149 fArchiveStream->Seek(0, SEEK_SET);
150
151 Unlock();
152 }
153
154 /*------------------------------------------------------------*/
~TWindow()155 TWindow::~TWindow()
156 {
157 }
158
159 /*------------------------------------------------------------*/
160
Quit()161 void TWindow::Quit()
162 {
163 delete fShelf; // by deleting the Shelf we'll save the state
164 fShelf = NULL;
165 BWindow::Quit();
166 }
167
168 /*------------------------------------------------------------*/
169
QuitRequested()170 bool TWindow::QuitRequested()
171 {
172 long c = be_app->CountWindows();
173
174 if (c == 1) {
175 be_app->PostMessage(B_QUIT_REQUESTED);
176
177 // BFile *file;
178 if (fArchiveStream) {
179 long err;
180 long d1 = MAGIC_1;
181 long d2 = MAGIC_2;
182 err = fArchiveStream->Write(&d1, sizeof(d1));
183 err = fArchiveStream->Write(&d2, sizeof(d2));
184
185 BRect frame = Frame();
186 err = fArchiveStream->Write(&frame, sizeof(frame));
187
188 //+ delete fShelf; // by deleting the Shelf we'll save the state
189 }
190 }
191 return TRUE;
192 }
193