2 * Copyright 2006-2015 Haiku, Inc. All Rights Reserved.
3 * Copyright 1997, 1998 R3 Software Ltd. All Rights Reserved.
4 * Distributed under the terms of the MIT License.
7 * Stephan Aßmus, superstippi@gmx.de
8 * John Scipione, jscipione@gmail.com
9 * Timothy Wayper, timmy@wunderbear.com
13 #include "CalcApplication.h"
20 #include <Directory.h>
22 #include <FindDirectory.h>
26 #include "CalcWindow.h"
29 #undef B_TRANSLATION_CONTEXT
30 #define B_TRANSLATION_CONTEXT "CalcApplication"
33 static const char* kSettingsFileName
= "DeskCalc_settings";
34 const char* kAppName
= B_TRANSLATE_SYSTEM_NAME("DeskCalc");
35 const char* kSignature
= "application/x-vnd.Haiku-DeskCalc";
37 static const float kDefaultWindowWidth
= 220.0;
38 static const float kDefaultWindowHeight
= 140.0;
41 CalcApplication::CalcApplication()
43 BApplication(kSignature
),
49 CalcApplication::~CalcApplication()
55 CalcApplication::ReadyToRun()
58 _LoadSettings(settings
);
60 float scaling
= be_plain_font
->Size() / 12.0f
;
61 BRect
frame(0, 0, (kDefaultWindowWidth
* scaling
) - 1,
62 (kDefaultWindowHeight
* scaling
) - 1);
63 fCalcWindow
= new CalcWindow(frame
, &settings
);
71 CalcApplication::AboutRequested()
73 fCalcWindow
->View()->MessageReceived(new BMessage(B_ABOUT_REQUESTED
));
78 CalcApplication::QuitRequested()
80 // save current user preferences
91 CalcApplication::_LoadSettings(BMessage
& archive
)
93 // locate preferences file
95 if (_InitSettingsFile(&prefsFile
, false) < B_OK
) {
96 printf("no preference file found.\n");
100 // unflatten settings data
101 if (archive
.Unflatten(&prefsFile
) < B_OK
) {
102 printf("error unflattening settings.\n");
108 CalcApplication::_SaveSettings()
110 if (!fCalcWindow
->Lock())
113 // archive the current state of the calculator
115 status_t ret
= fCalcWindow
->SaveSettings(&archive
);
117 fCalcWindow
->Unlock();
120 fprintf(stderr
, "CalcApplication::_SaveSettings() - error from window: "
121 "%s\n", strerror(ret
));
125 // flatten entire acrhive and write to settings file
127 ret
= _InitSettingsFile(&prefsFile
, true);
129 fprintf(stderr
, "CalcApplication::_SaveSettings() - "
130 "error creating file: %s\n", strerror(ret
));
134 ret
= archive
.Flatten(&prefsFile
);
136 fprintf(stderr
, "CalcApplication::_SaveSettings() - error flattening "
137 "to file: %s\n", strerror(ret
));
144 CalcApplication::_InitSettingsFile(BFile
* file
, bool write
)
146 // find user settings directory
148 status_t ret
= find_directory(B_USER_SETTINGS_DIRECTORY
, &prefsPath
);
152 ret
= prefsPath
.Append(kSettingsFileName
);
157 ret
= file
->SetTo(prefsPath
.Path(),
158 B_CREATE_FILE
| B_ERASE_FILE
| B_WRITE_ONLY
);
160 ret
= file
->SetTo(prefsPath
.Path(), B_READ_ONLY
);