Ticket #6226: OverlayImageSavePosition.diff

File OverlayImageSavePosition.diff, 7.8 KB (added by jscipione, 14 years ago)

Patch to save the current postion on quit

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,17 +25,18 @@
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
223 BView *bgView = new BView(Bounds(), "bgView", B_FOLLOW_ALL, B_WILL_DRAW);
224 bgView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
225- AddChild(bgView);
226+
227+ AddChild(fOverlayView);
228+ fOverlayView->AddChild(bgView);
229
230 BString text;
231 text << B_TRANSLATE("Enable \"Show replicants\" in Deskbar.");
232@@ -42,13 +44,59 @@
233 text << B_TRANSLATE("Drag & drop an image.");
234 text << "\n";
235 text << B_TRANSLATE("Drag the replicant to the Desktop.");
236- replView->SetToolTip(text);
237+ fOverlayView->SetToolTip(text);
238+
239+ BRect rect;
240+ if (settings->FindRect("window frame", &rect) == B_OK)
241+ SetFrame(rect);
242+ else
243+ SetFrame(frame, true);
244 }
245
246
247+OverlayWindow::~OverlayWindow()
248+{
249+}
250+
251+
252 bool
253 OverlayWindow::QuitRequested()
254 {
255 be_app->PostMessage(B_QUIT_REQUESTED);
256- return true;
257+
258+ // NOTE: don't quit, since the app needs us
259+ // for saving settings yet...
260+ return false;
261 }
262+
263+
264+status_t
265+OverlayWindow::SaveSettings(BMessage* archive) const
266+{
267+ status_t ret = archive->AddRect("window frame", Frame());
268+ if (ret < B_OK)
269+ return ret;
270+
271+ return fOverlayView->SaveSettings(archive);
272+}
273+
274+
275+void
276+OverlayWindow::SetFrame(BRect frame, bool forceCenter)
277+{
278+ // make sure window frame is on screen (center, if not)
279+ BScreen screen(this);
280+
281+ BRect screenFrame = screen.Frame();
282+ if (forceCenter || !screenFrame.Contains(frame)) {
283+ float left = (screenFrame.Width() - frame.Width()) / 2.0;
284+ float top = (screenFrame.Height() - frame.Height()) / 2.0;
285+ left += screenFrame.left;
286+ top += screenFrame.top;
287+ frame.OffsetTo(left, top);
288+ }
289+
290+ MoveTo(frame.left, frame.top);
291+ ResizeTo(frame.Width(), frame.Height());
292+}
293+
294Index: src/apps/overlayimage/OverlayView.cpp
295===================================================================
296--- src/apps/overlayimage/OverlayView.cpp (revision 37223)
297+++ src/apps/overlayimage/OverlayView.cpp (working copy)
298@@ -110,6 +110,13 @@
299 {
300 BView::Archive(archive, deep);
301
302+ // passed on request to parent
303+ status_t ret = BView::Archive(archive);
304+
305+ // save all the options
306+ if (ret == B_OK)
307+ ret = SaveSettings(archive);
308+
309 archive->AddString("add_on", "application/x-vnd.Haiku-OverlayImage");
310 archive->AddString("class", "OverlayImage");
311
312@@ -120,7 +127,7 @@
313 }
314 //archive->PrintToStream();
315
316- return B_OK;
317+ return ret;
318 }
319
320
321@@ -144,3 +151,24 @@
322 view->SetFontAndColor(0, 12, &font);
323 alert->Go();
324 }
325+
326+
327+status_t
328+OverlayView::_LoadSettings(BMessage* archive)
329+{
330+ if (!archive)
331+ return B_BAD_VALUE;
332+
333+ return B_OK;
334+}
335+
336+
337+status_t
338+OverlayView::SaveSettings(BMessage* archive) const
339+{
340+ status_t ret = archive ? B_OK : B_BAD_VALUE;
341+
342+ return ret;
343+}
344+
345+