Ticket #6226: OverlayImageSavePosition2.diff

File OverlayImageSavePosition2.diff, 7.7 KB (added by jscipione, 14 years ago)

Patch to save the current postion on quit. (Good)

Line 
1Index: src/apps/overlayimage/OverlayWindow.h
2===================================================================
3--- src/apps/overlayimage/OverlayWindow.h (revision 37223)
4+++ src/apps/overlayimage/OverlayWindow.h (working copy)
5@@ -16,11 +16,17 @@
6
7 #include <Window.h>
8
9+class OverlayView;
10
11 class OverlayWindow : public BWindow {
12 public:
13- OverlayWindow();
14+ OverlayWindow(BRect frame, BMessage* settings);
15+ virtual ~OverlayWindow();
16+ void SetFrame(BRect frame, bool forceCenter = false);
17 virtual bool QuitRequested();
18+ status_t SaveSettings(BMessage* archive) const;
19+private:
20+ OverlayView* fOverlayView;
21 };
22
23 #endif // OVERLAY_WINDOW_H
24Index: src/apps/overlayimage/OverlayView.h
25===================================================================
26--- src/apps/overlayimage/OverlayView.h (revision 37223)
27+++ src/apps/overlayimage/OverlayView.h (working copy)
28@@ -38,10 +38,12 @@
29 static BArchivable *Instantiate(BMessage *archive);
30 virtual status_t Archive(BMessage *data, bool deep = true) const;
31 void OverlayAboutRequested();
32+ status_t SaveSettings(BMessage* archive) const;
33
34 private:
35 BBitmap *fBitmap;
36 bool fReplicated;
37+ status_t _LoadSettings(BMessage* archive);
38 };
39
40 #endif // OVERLAY_VIEW_H
41Index: src/apps/overlayimage/OverlayApp.cpp
42===================================================================
43--- src/apps/overlayimage/OverlayApp.cpp (revision 37223)
44+++ src/apps/overlayimage/OverlayApp.cpp (working copy)
45@@ -11,6 +11,15 @@
46 * Humdinger <humdingerb@gmail.com>
47 */
48
49+#include <stdlib.h>
50+#include <stdio.h>
51+#include <string.h>
52+
53+#include <Directory.h>
54+#include <File.h>
55+#include <FindDirectory.h>
56+#include <Path.h>
57+
58 #include "OverlayApp.h"
59 #include "OverlayWindow.h"
60
61@@ -20,11 +29,107 @@
62 {
63 be_locale->GetAppCatalog(&fCatalog);
64
65- OverlayWindow *theWindow = new OverlayWindow();
66- theWindow->Show();
67+ BMessage settings;
68+ _LoadSettings(settings);
69+
70+ BRect frame(0, 0, 320.0 - 1, 200.0 - 1);
71+ fOverlayWindow = new OverlayWindow(frame, &settings);
72+
73+ fOverlayWindow->Show();
74 }
75
76
77+OverlayApp::~OverlayApp()
78+{
79+}
80+
81+
82+bool
83+OverlayApp::QuitRequested()
84+{
85+ // save current user preferences
86+ _SaveSettings();
87+
88+ return true;
89+}
90+
91+
92+void
93+OverlayApp::_LoadSettings(BMessage& archive)
94+{
95+ // locate preferences file
96+ BFile prefsFile;
97+ if (_InitSettingsFile(&prefsFile, false) < B_OK) {
98+ printf("no preference file found.\n");
99+ return;
100+ }
101+
102+ // unflatten settings data
103+ if (archive.Unflatten(&prefsFile) < B_OK) {
104+ printf("error unflattening settings.\n");
105+ }
106+}
107+
108+
109+void
110+OverlayApp::_SaveSettings()
111+{
112+ if (!fOverlayWindow->Lock())
113+ return;
114+
115+ BMessage archive;
116+ status_t ret = fOverlayWindow->SaveSettings(&archive);
117+
118+ fOverlayWindow->Unlock();
119+
120+ if (ret < B_OK) {
121+ fprintf(stderr, "_SaveSettings() - "
122+ "error from window: %s\n", strerror(ret));
123+ return;
124+ }
125+
126+ // flatten entire acrhive and write to settings file
127+ BFile prefsFile;
128+ ret = _InitSettingsFile(&prefsFile, true);
129+ if (ret < B_OK) {
130+ fprintf(stderr, "_SaveSettings() - "
131+ "error creating file: %s\n", strerror(ret));
132+ return;
133+ }
134+
135+ ret = archive.Flatten(&prefsFile);
136+ if (ret < B_OK) {
137+ fprintf(stderr, "_SaveSettings() - "
138+ "error flattening to file: %s\n", strerror(ret));
139+ return;
140+ }
141+}
142+
143+
144+status_t
145+OverlayApp::_InitSettingsFile(BFile* file, bool write)
146+{
147+ // find user settings directory
148+ BPath prefsPath;
149+ status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &prefsPath);
150+
151+ if (ret < B_OK)
152+ return ret;
153+
154+ ret = prefsPath.Append("OverlayImage_settings");
155+ if (ret < B_OK)
156+ return ret;
157+
158+ if (write)
159+ ret = file->SetTo(prefsPath.Path(),
160+ B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
161+ else
162+ ret = file->SetTo(prefsPath.Path(), B_READ_ONLY);
163+
164+ return ret;
165+}
166+
167+
168 int
169 main()
170 {
171Index: src/apps/overlayimage/OverlayApp.h
172===================================================================
173--- src/apps/overlayimage/OverlayApp.h (revision 37223)
174+++ src/apps/overlayimage/OverlayApp.h (working copy)
175@@ -18,12 +18,19 @@
176 #include <Catalog.h>
177 #include <Locale.h>
178
179+class OverlayWindow;
180
181 class OverlayApp : public BApplication {
182 public:
183 OverlayApp();
184+ virtual ~OverlayApp();
185+ virtual bool QuitRequested();
186 private:
187- BCatalog fCatalog;
188+ void _LoadSettings(BMessage& settings);
189+ void _SaveSettings();
190+ status_t _InitSettingsFile(BFile* file, bool write);
191+ BCatalog fCatalog;
192+ OverlayWindow* fOverlayWindow;
193 };
194
195 #endif // OVERLAY_APP_H
196Index: src/apps/overlayimage/OverlayWindow.cpp
197===================================================================
198--- src/apps/overlayimage/OverlayWindow.cpp (revision 37223)
199+++ src/apps/overlayimage/OverlayWindow.cpp (working copy)
200@@ -17,6 +17,7 @@
201 #include <Application.h>
202 #include <Catalog.h>
203 #include <Locale.h>
204+#include <Screen.h>
205 #include <String.h>
206 #include <TextView.h>
207
208@@ -24,13 +25,13 @@
209 #define B_TRANSLATE_CONTEXT "Main window"
210
211
212-OverlayWindow::OverlayWindow()
213+OverlayWindow::OverlayWindow(BRect frame, BMessage* settings)
214 :
215- BWindow(BRect(100, 100, 450, 350), "OverlayImage",
216+ BWindow(frame, "OverlayImage",
217 B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
218 {
219- OverlayView *replView = new OverlayView(Bounds());
220- AddChild(replView);
221+ fOverlayView = new OverlayView(Bounds());
222+ AddChild(fOverlayView);
223
224 BView *bgView = new BView(Bounds(), "bgView", B_FOLLOW_ALL, B_WILL_DRAW);
225 bgView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
226@@ -42,13 +43,59 @@
227 text << B_TRANSLATE("Drag & drop an image.");
228 text << "\n";
229 text << B_TRANSLATE("Drag the replicant to the Desktop.");
230- replView->SetToolTip(text);
231+ fOverlayView->SetToolTip(text);
232+
233+ BRect rect;
234+ if (settings->FindRect("window frame", &rect) == B_OK)
235+ SetFrame(rect);
236+ else
237+ SetFrame(frame, true);
238 }
239
240
241+OverlayWindow::~OverlayWindow()
242+{
243+}
244+
245+
246 bool
247 OverlayWindow::QuitRequested()
248 {
249 be_app->PostMessage(B_QUIT_REQUESTED);
250- return true;
251+
252+ // NOTE: don't quit, since the app needs us
253+ // for saving settings yet...
254+ return false;
255 }
256+
257+
258+status_t
259+OverlayWindow::SaveSettings(BMessage* archive) const
260+{
261+ status_t ret = archive->AddRect("window frame", Frame());
262+ if (ret < B_OK)
263+ return ret;
264+
265+ return fOverlayView->SaveSettings(archive);
266+}
267+
268+
269+void
270+OverlayWindow::SetFrame(BRect frame, bool forceCenter)
271+{
272+ // make sure window frame is on screen (center, if not)
273+ BScreen screen(this);
274+
275+ BRect screenFrame = screen.Frame();
276+ if (forceCenter || !screenFrame.Contains(frame)) {
277+ float left = (screenFrame.Width() - frame.Width()) / 2.0;
278+ float top = (screenFrame.Height() - frame.Height()) / 2.0;
279+ left += screenFrame.left;
280+ top += screenFrame.top;
281+ frame.OffsetTo(left, top);
282+ }
283+
284+ MoveTo(frame.left, frame.top);
285+ ResizeTo(frame.Width(), frame.Height());
286+}
287+
288Index: src/apps/overlayimage/OverlayView.cpp
289===================================================================
290--- src/apps/overlayimage/OverlayView.cpp (revision 37223)
291+++ src/apps/overlayimage/OverlayView.cpp (working copy)
292@@ -110,6 +110,13 @@
293 {
294 BView::Archive(archive, deep);
295
296+ // passed on request to parent
297+ status_t ret = BView::Archive(archive);
298+
299+ // save all the options
300+ if (ret == B_OK)
301+ ret = SaveSettings(archive);
302+
303 archive->AddString("add_on", "application/x-vnd.Haiku-OverlayImage");
304 archive->AddString("class", "OverlayImage");
305
306@@ -120,7 +127,7 @@
307 }
308 //archive->PrintToStream();
309
310- return B_OK;
311+ return ret;
312 }
313
314
315@@ -144,3 +151,24 @@
316 view->SetFontAndColor(0, 12, &font);
317 alert->Go();
318 }
319+
320+
321+status_t
322+OverlayView::_LoadSettings(BMessage* archive)
323+{
324+ if (!archive)
325+ return B_BAD_VALUE;
326+
327+ return B_OK;
328+}
329+
330+
331+status_t
332+OverlayView::SaveSettings(BMessage* archive) const
333+{
334+ status_t ret = archive ? B_OK : B_BAD_VALUE;
335+
336+ return ret;
337+}
338+
339+