-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSettingsWindow.cpp
More file actions
133 lines (114 loc) · 4.24 KB
/
SettingsWindow.cpp
File metadata and controls
133 lines (114 loc) · 4.24 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
/*
* 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)
:
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("Save text on exit"), 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);
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));
BLayoutBuilder::Group<>(this, B_VERTICAL)
.Add(fInsertClipboard)
.Add(fSaveTextCheck)
.Add(fSaveFieldsCheck)
.Add(fClearSettingsAfterUse)
.Add(fFontFamilyField)
.Add(fFontSizeSlider)
.AddGlue()
.Add(fApplyButton)
.SetInsets(B_USE_WINDOW_INSETS);
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(); // Optional visual separator
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.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;
}