-
Notifications
You must be signed in to change notification settings - Fork 12
/
DeskNoteApp.cpp
144 lines (118 loc) · 3.18 KB
/
DeskNoteApp.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
/*
* Original copyright 2000 by Colin Stewart (http://www.owlfish.com/).
* All rights reserved.
* Distributed under the terms of the BSD (3-clause) License.
*
* Authors:
* Janus2, 2015
* Humdinger, 2021
*
*/
#include "DeskNoteApp.h"
#include <Catalog.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "App"
int main() {
DeskNoteApp* myApp;
myApp = new DeskNoteApp();
myApp->Run();
delete myApp;
return 0;
}
const char DeskNoteApp::header[] = "DESKNOTE";
const char* app_signature = "application/x-vnd.cms103-DeskNotes";
DeskNoteApp::DeskNoteApp()
:
BApplication(app_signature)
{
BRect rect, scrRect;
BPath dir;
BFile* file;
BMessage* msg;
BScreen* currentScreen;
bool SettingsOK = true;
int fileVer;
char* buffer = new char[1024];
find_directory(B_USER_SETTINGS_DIRECTORY, &dir);
dir.Append("DeskNotes Settings");
file = new BFile(dir.Path(), B_READ_ONLY);
msg = new BMessage();
if (file->InitCheck() == B_OK) {
if (file->Read(buffer, strlen(header)) != (ssize_t)strlen(header))
SettingsOK = false;
if (strcasecmp(header, buffer) != 0)
SettingsOK = false;
file->Read(&fileVer, sizeof(NotesVersion));
if (fileVer != NotesVersion)
SettingsOK = false;
msg->Unflatten(file);
} else
SettingsOK = false;
delete file;
delete buffer;
if (SettingsOK) {
currentScreen = new BScreen(); // This locks the screen.
scrRect = currentScreen->Frame(); // Find out how big the screen is.
delete currentScreen; // Delete the object, unlock the screen.
msg->FindRect("windowPos", &rect);
if (rect.right > scrRect.right) {
// If the window would be off-screen then try correcting it...
float width, scrWidth;
scrWidth = scrRect.right - scrRect.left;
width = rect.right - rect.left;
if (width > scrWidth)
width = scrWidth - 20;
rect.right = scrRect.right - 2;
rect.left = rect.right - width;
}
if (rect.bottom > scrRect.bottom) {
// If the window would be off-screen then try correcting it...
float height, scrHeight;
scrHeight = scrRect.bottom - scrRect.top;
height = rect.bottom - rect.top;
if (height > scrHeight)
height = scrHeight - 20;
rect.bottom = scrRect.bottom - 2;
rect.top = rect.bottom - height;
}
} else
rect.Set(100, 80, 460, 320); // Inital size for the window.
myNote = new DeskNoteWindow(rect);
myNote->RestoreNote(msg);
myNote->Show();
delete msg;
}
bool DeskNoteApp::QuitRequested()
{
BAlert* alert;
BPath dir;
BFile* file;
BMessage* msg;
int ver = NotesVersion;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &dir) == B_OK) {
// This is where we save our notes position, size, and text content.
dir.Append("DeskNotes Settings");
file = new BFile(dir.Path(), B_READ_WRITE | B_CREATE_FILE);
file->Write(header, strlen(header));
file->Write(&ver, sizeof(ver));
msg = getSettings();
msg->Flatten(file);
delete file;
delete msg;
} else {
alert = new BAlert(B_TRANSLATE_SYSTEM_NAME("DeskNotes"),
B_TRANSLATE("Unable to save note!"), B_TRANSLATE("OK"));
alert->Go();
}
return true;
}
BMessage*
DeskNoteApp::getSettings()
{
BMessage* msg = new BMessage();
BRect rect;
rect = myNote->Frame();
msg->AddRect("windowPos", rect);
myNote->SaveNote(msg);
return msg;
}