-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSettingsWindow.cpp
More file actions
168 lines (143 loc) · 5.43 KB
/
SettingsWindow.cpp
File metadata and controls
168 lines (143 loc) · 5.43 KB
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
/*
* Copyright 2025, Johan Wagenheim <johan@dospuntos.no>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "SettingsWindow.h"
#include "Constants.h"
#include <Alert.h>
#include <Application.h>
#include <Catalog.h>
#include <Font.h>
#include <LayoutBuilder.h>
#include <MenuItem.h>
#include <StringView.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Settings"
SettingsWindow::SettingsWindow(bool saveText, bool saveSettings, bool clipboard, bool clearSettings,
int32 fontSize, BString fontFamily, bool closeOnEsc, bool askToSave)
:
BWindow(BRect(200, 200, 500, 400), B_TRANSLATE("Settings"), B_TITLED_WINDOW,
B_NOT_RESIZABLE | B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS
| B_CLOSE_ON_ESCAPE),
fFontFamily(fontFamily)
{
fSaveTextCheck = new BCheckBox("SaveText", B_TRANSLATE("Restore last session on startup"), new BMessage(M_APPLY_SETTINGS));
fSaveTextCheck->SetValue(saveText ? B_CONTROL_ON : B_CONTROL_OFF);
fSaveFieldsCheck
= new BCheckBox("SaveSettings", B_TRANSLATE("Save field values on exit"), new BMessage(M_APPLY_SETTINGS));
fSaveFieldsCheck->SetValue(saveSettings ? B_CONTROL_ON : B_CONTROL_OFF);
fInsertClipboard = new BCheckBox("Clipboard", B_TRANSLATE("Paste clipboard contents on open"),
new BMessage(M_APPLY_SETTINGS));
fInsertClipboard->SetValue(clipboard ? B_CONTROL_ON : B_CONTROL_OFF);
fClearSettingsAfterUse = new BCheckBox("ClearSettings", B_TRANSLATE("Clear field values after use"),
new BMessage(M_APPLY_SETTINGS));
fClearSettingsAfterUse->SetValue(clearSettings ? B_CONTROL_ON : B_CONTROL_OFF);
fCloseOnEsc = new BCheckBox("CloseSettings", B_TRANSLATE("Close on Escape"), new BMessage(M_APPLY_SETTINGS));
fCloseOnEsc->SetValue(closeOnEsc ? B_CONTROL_ON : B_CONTROL_OFF);
fAskToSave = new BCheckBox("SaveSettings", B_TRANSLATE("Prompt to save changes before closing"), new BMessage(M_APPLY_SETTINGS));
fAskToSave->SetValue(askToSave ? B_CONTROL_ON : B_CONTROL_OFF);
BPopUpMenu* fontMenu = new BPopUpMenu("FontFamily");
PopulateFontMenu(fontMenu);
fFontFamilyField = new BMenuField("Font", B_TRANSLATE("Font family:"), fontMenu);
fFontSizeSlider
= new BSlider("FontSize", "", new BMessage(M_APPLY_SETTINGS), 6, 72, B_HORIZONTAL);
fFontSizeSlider->SetLimitLabels("6", "72");
fFontSizeSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
fFontSizeSlider->SetHashMarkCount(10);
fFontSizeSlider->SetValue(fontSize);
fFontSizeSlider->SetModificationMessage(new BMessage(M_APPLY_SETTINGS));
BString sliderLabel = B_TRANSLATE("Font size:");
sliderLabel << " " << fontSize << "pt";
fFontSizeSlider->SetLabel(sliderLabel.String());
fApplyButton = new BButton("Close", B_TRANSLATE("Close"), new BMessage(M_CLOSE_SETTINGS));
BBox* behaviorBox = new BBox("behaviorBox");
behaviorBox->SetLabel(B_TRANSLATE("Preferences"));
BLayoutBuilder::Group<>(behaviorBox, B_VERTICAL,
B_USE_DEFAULT_SPACING)
.SetInsets(B_USE_BIG_INSETS)
.Add(fInsertClipboard)
.Add(fSaveTextCheck)
.Add(fSaveFieldsCheck)
.Add(fClearSettingsAfterUse)
.Add(fCloseOnEsc)
.Add(fAskToSave);
BBox* appearanceBox = new BBox("appearanceBox");
appearanceBox->SetLabel(B_TRANSLATE("Appearance"));
BLayoutBuilder::Group<>(appearanceBox, B_VERTICAL,
B_USE_DEFAULT_SPACING)
.SetInsets(B_USE_BIG_INSETS)
.Add(fFontFamilyField)
.Add(fFontSizeSlider)
.AddGlue();
BLayoutBuilder::Group<>(this, B_VERTICAL,
B_USE_DEFAULT_SPACING)
.SetInsets(B_USE_WINDOW_INSETS)
.AddGroup(B_HORIZONTAL,
B_USE_DEFAULT_SPACING)
.Add(behaviorBox, 1)
.Add(appearanceBox, 1)
.End()
.AddGroup(B_HORIZONTAL,
B_USE_DEFAULT_SPACING)
.AddGlue()
.Add(fApplyButton)
.End();
ResizeToPreferred();
}
void
SettingsWindow::PopulateFontMenu(BPopUpMenu* menu)
{
// Add a default system font option
BMenuItem* defaultItem = new BMenuItem("System default", new BMessage(M_APPLY_SETTINGS));
menu->AddItem(defaultItem);
menu->AddSeparatorItem();
defaultItem->SetMarked(true);
font_family family;
int32 count = count_font_families();
for (int32 i = 0; i < count; ++i) {
if (get_font_family(i, &family) == B_OK) {
BMenuItem* item = new BMenuItem(family, new BMessage(M_APPLY_SETTINGS));
menu->AddItem(item);
if (family == fFontFamily)
item->SetMarked(true);
}
}
}
void
SettingsWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case M_APPLY_SETTINGS:
{
BMenuItem* selected = fFontFamilyField->Menu()->FindMarked();
BString fontFamily = selected->Label();
BMessage applyMsg(M_APPLY_SETTINGS);
applyMsg.AddBool("saveText", fSaveTextCheck->Value() == B_CONTROL_ON);
applyMsg.AddBool("saveSettings", fSaveFieldsCheck->Value() == B_CONTROL_ON);
applyMsg.AddBool("insertClipboard", fInsertClipboard->Value() == B_CONTROL_ON);
applyMsg.AddBool("clearSettings", fClearSettingsAfterUse->Value() == B_CONTROL_ON);
applyMsg.AddBool("closeOnEsc", fCloseOnEsc->Value() == B_CONTROL_ON);
applyMsg.AddBool("askToSave", fAskToSave->Value() == B_CONTROL_ON);
applyMsg.AddInt32("fontSize", fFontSizeSlider->Value());
applyMsg.AddString("fontFamily", fontFamily);
be_app->WindowAt(0)->PostMessage(&applyMsg);
BString sliderLabel = B_TRANSLATE("Font size:");
sliderLabel << " " << fFontSizeSlider->Value() << "pt";
fFontSizeSlider->SetLabel(sliderLabel.String());
break;
}
case M_CLOSE_SETTINGS:
Hide();
break;
default:
BWindow::MessageReceived(message);
break;
}
}
bool
SettingsWindow::QuitRequested()
{
if (!IsHidden())
Hide();
return false;
}