-
Notifications
You must be signed in to change notification settings - Fork 2
/
ColorsApplication.cpp
171 lines (134 loc) · 3.62 KB
/
ColorsApplication.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright 2009-2012 Haiku, Inc. All Rights Reserved.
* Copyright 2001-2008 Werner Freytag.
* Distributed under the terms of the MIT License.
*
* Original Author:
* Werner Freytag <[email protected]>
* Authors:
* John Scipione <[email protected]>
*/
#include "ColorsApplication.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <Alert.h>
#include <Directory.h>
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
#include "ColorsWindow.h"
static const char* kSettingsFileName = "Colors!_settings";
static const char* kAppSig = "application/x-vnd.Haiku-Colors!";
static const float kDefaultWindowWidth = 400.0;
static const float kDefaultWindowHeight = 300.0;
ColorsApplication::ColorsApplication()
:
BApplication(kAppSig),
fColorsWindow(NULL)
{
}
ColorsApplication::~ColorsApplication()
{
}
void
ColorsApplication::ReadyToRun()
{
_LoadSettings();
BRect frame(0, 0, kDefaultWindowWidth - 1, kDefaultWindowHeight - 1);
fColorsWindow = new ColorsWindow(frame);
// reveal window
fColorsWindow->Show();
}
void
ColorsApplication::AboutRequested()
{
(new BAlert("About", "Colors! 2.3\n\n"
"©2009-2013 John Scipione.\n"
"©2001-2008 Werner Freytag\n"
"Colors! icon by meanwhile\n\n"
"History\n"
" 2.3 (Feb 23, 2013) Remove the ForeBackSelector control and "
"replace it with more color containers.\n"
" 2.2 (Dec 1, 2012) Added web-safe selector control. Degree and % "
"symbols return.\n"
" 2.1 (Aug 29, 2012) Updated look of controls, slider now updates "
"live.\n"
" 2.0 (Apr 30, 2012) Layout with the Haiku layout APIs, update "
"the icon, add more color containers, update the interface, "
"improve the fore back color container control.\n"
" 1.6 (Sep 11, 2001) Added the eye-dropper to pick a color from "
"anywhere on the desktop. Added a popup menu with some useful window "
"specific settings.\n"
" 1.5.1 (Sep 5, 2001) Fixed some minor bugs, improved the text "
"color container\n"
" 1.5 (Sep 3, 2001) Added color containers, removed some minor "
"bugs, improved speed.\n"
" 1.0 (Aug 3, 2001) Initial release. Updates to come!",
"Close"))->Go();
}
bool
ColorsApplication::QuitRequested()
{
fColorsWindow->QuitRequested();
_SaveSettings();
return true;
}
// #pragma mark -
void
ColorsApplication::_LoadSettings()
{
// locate preferences file
BFile prefsFile;
if (_InitSettingsFile(&prefsFile, false) < B_OK) {
printf("no preference file found.\n");
return;
}
// unflatten settings data
if (fSettings.Unflatten(&prefsFile) < B_OK) {
printf("error unflattening settings.\n");
}
}
void
ColorsApplication::_SaveSettings()
{
// flatten entire archive and write to settings file
BFile prefsFile;
status_t ret = _InitSettingsFile(&prefsFile, true);
if (ret < B_OK) {
fprintf(stderr, "ColorsApplication::_SaveSettings() - "
"error creating file: %s\n", strerror(ret));
return;
}
ret = fSettings.Flatten(&prefsFile);
if (ret < B_OK) {
fprintf(stderr, "ColorsApplication::_SaveSettings() - error flattening "
"to file: %s\n", strerror(ret));
return;
}
}
status_t
ColorsApplication::_InitSettingsFile(BFile* file, bool write)
{
// find user settings directory
BPath prefsPath;
status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &prefsPath);
if (ret < B_OK)
return ret;
ret = prefsPath.Append(kSettingsFileName);
if (ret < B_OK)
return ret;
if (write) {
ret = file->SetTo(prefsPath.Path(),
B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
} else
ret = file->SetTo(prefsPath.Path(), B_READ_ONLY);
return ret;
}
int
main()
{
ColorsApplication* app = new ColorsApplication();
app->Run();
return 0;
}