Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
LedLineIt/Sources/Options.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
5#include "Stroika/Foundation/StroikaPreComp.h"
6#include <afxadv.h>
7
10#include "Stroika/Foundation/Execution/ModuleGetterSetter.h"
12
13#include "Options.h"
14
15using std::byte;
16
17using namespace Stroika::Foundation;
19using namespace Stroika::Foundation::Execution;
20
21using namespace Stroika::Frameworks::Led;
22
23using Memory::BLOB;
24
25using SearchParameters = TextInteractor::SearchParameters;
26#if qSupportSyntaxColoring
27using SyntaxColoringOption = Options::SyntaxColoringOption;
28#endif
29
30namespace {
31 struct Options_ {
32 BLOB fDockBarState;
33 SearchParameters fSearchParameters{};
34 bool fSmartCutAndPaste{true};
35 bool fAutoIndent{true};
36 bool fTabsAutoShiftsText{true};
37#if qSupportSyntaxColoring
38 SyntaxColoringOption fSyntaxColoring{SyntaxColoringOption::eSyntaxColoringNone};
39#endif
40#if qStroika_Foundation_Common_Platform_Windows
41 bool fCheckFileAssocAtStartup{true};
42 BLOB fDefaultNewDocFont;
43#endif
44 };
45
46 struct Options_Storage_IMPL_ {
47 Options_Storage_IMPL_ ()
48 : fOptionsFile_{L"AppSettings"sv,
49 [] () -> ObjectVariantMapper {
51
52 // really should use String, no longer Led_tString, but for now... (note this only works as is for wchar_t Led_tString
53 mapper.Add<Led_tString> ([] (const ObjectVariantMapper& /*mapper*/,
54 const Led_tString* obj) -> VariantValue { return String{*obj}; },
55 [] (const ObjectVariantMapper& /*mapper*/, const VariantValue& d,
56 Led_tString* intoObj) -> void { *intoObj = d.As<String> ().As<Led_tString> (); });
57 mapper.AddCommonType<vector<Led_tString>> ();
58 mapper.AddCommonType<Memory::BLOB> ();
59
60#if qSupportSyntaxColoring
61 mapper.AddCommonType<SyntaxColoringOption> ();
62#endif
63
64 mapper.AddClass<SearchParameters> ({
65 {L"Match-String", &SearchParameters::fMatchString},
66 {L"Wrap-Search", &SearchParameters::fWrapSearch},
67 {L"Whole-Word-Search", &SearchParameters::fWholeWordSearch},
68 {L"Case-Sensative-Search", &SearchParameters::fCaseSensativeSearch},
69 {L"Recent-Match-Strings", &SearchParameters::fRecentFindStrings},
70 });
71
72 mapper.AddClass<Options_> ({
73 {L"Dock-Bar-State", &Options_::fDockBarState},
74 {L"Search-Parameters", &Options_::fSearchParameters},
75 {L"Smart-Cut-And-Paste", &Options_::fSmartCutAndPaste},
76 {L"Auto-Indent", &Options_::fAutoIndent},
77 {L"Tabs-Auto-Shifts-Text", &Options_::fTabsAutoShiftsText},
78
79#if qSupportSyntaxColoring
80 {L"Syntax-Coloring", &Options_::fSyntaxColoring},
81#endif
82
83#if qStroika_Foundation_Common_Platform_Windows
84 {L"Check-File-Assoc-At-Startup", &Options_::fCheckFileAssocAtStartup},
85 {L"Default-New-Doc-Font", &Options_::fDefaultNewDocFont},
86#endif
87 });
88 return mapper;
89 }(),
90
92
93 OptionsFile::mkFilenameMapper (L"LedLineIt"sv)}
94 , fActualCurrentConfigData_{fOptionsFile_.Read<Options_> (Options_{})}
95 {
96 Set (fActualCurrentConfigData_); // assure derived data (and changed fields etc) up to date
97 }
98 Options_ Get () const
99 {
100 return fActualCurrentConfigData_;
101 }
102 void Set (const Options_& v)
103 {
104 fActualCurrentConfigData_ = v;
105 fOptionsFile_.Write (v);
106 }
107
108 private:
109 OptionsFile fOptionsFile_;
110 Options_ fActualCurrentConfigData_;
111 };
112
114}
115
116/*
117 ********************************************************************************
118 *********************************** Options ************************************
119 ********************************************************************************
120 */
121SearchParameters Options::GetSearchParameters () const
122{
123 return sOptions_.Get ().fSearchParameters;
124}
125
126void Options::SetSearchParameters (const SearchParameters& searchParameters)
127{
128 sOptions_.Update ([=] (Options_ d) {
129 d.fSearchParameters = searchParameters;
130 return d;
131 });
132}
133
134#if qStroika_Foundation_Common_Platform_Windows
135const CDockState& Options::GetDocBarState () const
136{
137 static CDockState dockState; // keep static copy and clear each time cuz CDocState doesn't support copy CTOR - LGP971214
138 static std::once_flag sOnce_;
139 std::call_once (sOnce_, [] () {
140 dockState.Clear ();
141 BLOB bytes = sOptions_.Get ().fDockBarState;
142 if (not bytes.empty ()) {
143 CMemFile file;
144 file.Write (Traversal::Iterator2Pointer (bytes.begin ()), static_cast<UINT> (bytes.size ()));
145 file.SeekToBegin ();
146 CArchive ar (&file, CArchive::load);
147 dockState.Serialize (ar);
148 ar.Close ();
149 }
150 });
151 return dockState;
152}
153
154void Options::SetDocBarState (const CDockState& dockState)
155{
156 CMemFile file;
157 CArchive ar (&file, CArchive::store);
158 CDockState& ds = const_cast<CDockState&> (dockState); // Serialize/Write shouldn't change object!
159 ds.Serialize (ar);
160 ar.Close ();
161 ULONG nSize = static_cast<ULONG> (file.GetLength ());
162 ASSERT (nSize < 4096);
163 byte* p = new byte[nSize];
164 file.SeekToBegin ();
165 file.Read (p, nSize);
166 sOptions_.Update ([=] (Options_ d) {
167 d.fDockBarState = BLOB{p, p + nSize};
168 return d;
169 });
170 delete[] p;
171}
172#endif
173
174bool Options::GetSmartCutAndPaste () const
175{
176 return sOptions_.Get ().fSmartCutAndPaste;
177}
178
179void Options::SetSmartCutAndPaste (bool smartCutAndPaste)
180{
181 sOptions_.Update ([=] (Options_ d) {
182 d.fSmartCutAndPaste = smartCutAndPaste;
183 return d;
184 });
185}
186
187bool Options::GetAutoIndent () const
188{
189 return sOptions_.Get ().fAutoIndent;
190}
191
192void Options::SetAutoIndent (bool autoIndent)
193{
194 sOptions_.Update ([=] (Options_ d) {
195 d.fAutoIndent = autoIndent;
196 return d;
197 });
198}
199
200bool Options::GetTreatTabAsIndentChar () const
201{
202 return sOptions_.Get ().fTabsAutoShiftsText;
203}
204
205void Options::SetTreatTabAsIndentChar (bool tabAsIndentChar)
206{
207 sOptions_.Update ([=] (Options_ d) {
208 d.fTabsAutoShiftsText = tabAsIndentChar;
209 return d;
210 });
211}
212
213#if qSupportSyntaxColoring
214Options::SyntaxColoringOption Options::GetSyntaxColoringOption () const
215{
216 return sOptions_.Get ().fSyntaxColoring;
217}
218
219void Options::SetSyntaxColoringOption (SyntaxColoringOption syntaxColoringOption)
220{
221 sOptions_.Update ([=] (Options_ d) {
222 d.fSyntaxColoring = syntaxColoringOption;
223 return d;
224 });
225}
226#endif
227
228#if qStroika_Foundation_Common_Platform_Windows
229bool Options::GetCheckFileAssocsAtStartup () const
230{
231 return sOptions_.Get ().fCheckFileAssocAtStartup;
232}
233
234void Options::SetCheckFileAssocsAtStartup (bool checkFileAssocsAtStartup)
235{
236 sOptions_.Update ([=] (Options_ d) {
237 d.fCheckFileAssocAtStartup = checkFileAssocsAtStartup;
238 return d;
239 });
240}
241#endif
242
243FontSpecification Options::GetDefaultNewDocFont () const
244{
245#if qStroika_Foundation_Common_Platform_Windows
246 BLOB bytes = sOptions_.Get ().fDefaultNewDocFont;
247 if (not bytes.empty ()) {
248 if (bytes.size () == sizeof (LOGFONT)) {
250 fsp.SetOSRep (bytes.As<LOGFONT> ());
251 return fsp;
252 }
253 }
254#endif
255 // A good default font for LedLineIt - really just want something monospace,
256 // but don't know how better to choose...
257 // Not TOO important what we do here. Really we should get/save a user-chosen default in the
258 // prefs file!
259 FontSpecification defFont = GetStaticDefaultFont ();
260 defFont.SetFontName (_T ("Courier New"));
261 defFont.SetPointSize (10);
262 return defFont;
263}
264
265void Options::SetDefaultNewDocFont ([[maybe_unused]] const FontSpecification& defaultNewDocFont)
266{
267#if qStroika_Foundation_Common_Platform_Windows
268 sOptions_.Update ([&] (Options_ d) {
269 d.fDefaultNewDocFont = BLOB::FromRaw (defaultNewDocFont.GetOSRep ());
270 return d;
271 });
272#endif
273}
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
Set<T> is a container of T, where once an item is added, additionally adds () do nothing.
ObjectVariantMapper can be used to map C++ types to and from variant-union types, which can be transp...
static const ModuleDataUpgraderType kDefaultUpgrader
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
nonvirtual bool empty() const
Definition BLOB.inl:246
static BLOB FromRaw(const T *s, const T *e)
Convert pointed to/referenced data to BLOB (treating the argument as raw bytes).
Definition BLOB.inl:145
nonvirtual size_t size() const
Definition BLOB.inl:281
Helper to define synchronized, lazy constructed, module initialization (intended to work with DataExc...