-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
App.cpp
163 lines (135 loc) · 3.03 KB
/
App.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
/*
* Copyright 2006-2011, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "App.h"
#include <stdio.h>
#include <Catalog.h>
#include <Entry.h>
#include <Message.h>
#include <String.h>
#include "support.h"
#include "MainWindow.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "LaunchBox"
App::App()
:
BApplication("application/x-vnd.Haiku-LaunchBox"),
fSettingsChanged(false),
fNamePanelSize(200, 50)
{
SetPulseRate(3000000);
}
App::~App()
{
}
bool
App::QuitRequested()
{
_StoreSettingsIfNeeded();
return true;
}
void
App::ReadyToRun()
{
bool windowAdded = false;
BRect frame(50.0, 50.0, 65.0, 100.0);
BMessage settings;
status_t status = load_settings(&settings, "main_settings", "LaunchBox");
if (status >= B_OK) {
BMessage windowMessage;
for (int32 i = 0; settings.FindMessage("window", i, &windowMessage)
>= B_OK; i++) {
BString string;
string << i + 1;
BString name(B_TRANSLATE("Pad %1"));
name.ReplaceFirst("%1", string);
BMessage* windowSettings = new BMessage(windowMessage);
MainWindow* window = new MainWindow(name.String(), frame,
windowSettings);
window->Show();
windowAdded = true;
frame.OffsetBy(10.0, 10.0);
windowMessage.MakeEmpty();
}
BSize size;
if (settings.FindSize("name panel size", &size) == B_OK)
fNamePanelSize = size;
}
if (!windowAdded) {
MainWindow* window = new MainWindow(B_TRANSLATE("Pad 1"), frame, true);
window->Show();
}
}
void
App::MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_ADD_WINDOW: {
BMessage* settings = new BMessage('sett');
bool wasCloned = message->FindMessage("window", settings) == B_OK;
BString string;
string << CountWindows() + 1;
BString name(B_TRANSLATE("Pad %1"));
name.ReplaceFirst("%1", string);
MainWindow* window = new MainWindow(name.String(),
BRect(50.0, 50.0, 65.0, 100.0), settings);
if (wasCloned)
window->MoveBy(10, 10);
window->Show();
fSettingsChanged = true;
break;
}
case MSG_SETTINGS_CHANGED:
fSettingsChanged = true;
break;
default:
BApplication::MessageReceived(message);
break;
}
}
void
App::Pulse()
{
_StoreSettingsIfNeeded();
}
void
App::SetNamePanelSize(const BSize& size)
{
if (Lock()) {
fNamePanelSize = size;
Unlock();
}
}
BSize
App::NamePanelSize()
{
BSize size;
if (Lock()) {
size = fNamePanelSize;
Unlock();
}
return size;
}
void
App::_StoreSettingsIfNeeded()
{
if (!fSettingsChanged)
return;
BMessage settings('sett');
for (int32 i = 0; BWindow* window = WindowAt(i); i++) {
if (MainWindow* padWindow = dynamic_cast<MainWindow*>(window)) {
if (padWindow->Lock()) {
BMessage* windowSettings = padWindow->Settings();
if (windowSettings) {
padWindow->SaveSettings(windowSettings);
settings.AddMessage("window", windowSettings);
}
padWindow->Unlock();
}
}
}
settings.AddSize("name panel size", fNamePanelSize);
save_settings(&settings, "main_settings", "LaunchBox");
fSettingsChanged = false;
}