Skip to content

Commit f128330

Browse files
Implement default button in ConfigValueDialog
1 parent 429d209 commit f128330

5 files changed

Lines changed: 123 additions & 67 deletions

File tree

far_plugin/facade/far3/include/far3/config_value_dialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ namespace Far3
2121
static std::unique_ptr<const ConfigValueDialog> Create(PluginStartupInfo const& i, Guid const& pluginGuid);
2222

2323
virtual ~ConfigValueDialog() = default;
24-
virtual std::pair<bool, std::string> Show(std::string const& initValue, Plugin::ConfigFieldType type, int textId) const = 0;
24+
virtual std::pair<bool, std::string> Show(std::string const& initValue, std::string const& defaultValue, Plugin::ConfigFieldType type, int textId) const = 0;
2525
};
2626
}

far_plugin/facade/far3/include/far3/text.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,5 @@ enum{
9494
MTooManyErrorsInConfigFile,
9595
MTooManyLinesInConfigFile,
9696
MFailedLoadConfig,
97+
MDefault,
9798
};

far_plugin/facade/far3/src/config_menu.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ namespace
172172

173173
auto const selectedField = FieldsTexts.at(selected);
174174
auto const selectedFieldData = dataMapper.Get(static_cast<int>(selectedField.first), config);
175-
auto value = configureValueDialog->Show(selectedFieldData.value, selectedFieldData.type, selectedField.second);
175+
auto const defaultValue = dataMapper.Get(static_cast<int>(selectedField.first), Plugin::Config()).value;
176+
auto value = configureValueDialog->Show(selectedFieldData.value, defaultValue, selectedFieldData.type, selectedField.second);
176177
if (value.first)
177178
return {selectedField.first, value.second};
178179
}

far_plugin/facade/far3/src/config_value_dialog.cpp

Lines changed: 118 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -38,123 +38,176 @@ namespace
3838
return result;
3939
}
4040

41-
class ConfigValueDialogImpl : public Far3::ConfigValueDialog
41+
enum class DialogResult
42+
{
43+
Ok,
44+
Cancel,
45+
Default,
46+
};
47+
48+
class Dialog
4249
{
4350
public:
44-
ConfigValueDialogImpl(PluginStartupInfo const& i, Guid const& pluginGuid);
45-
std::pair<bool, std::string> Show(std::string const& initValue, ConfigFieldType type, int textId) const override;
51+
Dialog(std::string const& value, ConfigFieldType type, int textId, PluginStartupInfo const& i, Guid const& pluginGuid);
52+
DialogResult Show();
53+
std::string GetValue() const;
4654

4755
private:
48-
intptr_t PushCheckbox(std::string const& value, int textId, intptr_t& y, std::vector<FarDialogItem>& items) const;
49-
intptr_t PushSizebox(WideString::value_type const* value, int textId, intptr_t& y, std::vector<FarDialogItem>& items) const;
50-
intptr_t PushEditbox(WideString::value_type const* value, int textId, intptr_t& y, std::vector<FarDialogItem>& items) const;
51-
WideString GetText(intptr_t ctrlId, void* handle) const;
52-
std::string GetCheckboxValue(intptr_t ctrlId, void* handle) const;
53-
std::string GetSizeboxValue(intptr_t ctrlId, void* handle) const;
54-
std::string GetEditboxValue(intptr_t ctrlId, void* handle) const;
56+
intptr_t Bottom() const;
57+
void PushCheckbox(std::string const& value, int textId);
58+
void PushSizebox(std::string const& value, int textId);
59+
void PushEditbox(std::string const& value, int textId);
60+
WideString GetText() const;
61+
std::string GetCheckboxValue() const;
62+
std::string GetSizeboxValue() const;
63+
std::string GetEditboxValue() const;
5564

5665
private:
57-
PluginStartupInfo const I;
58-
Guid const PluginGuid;
66+
ConfigFieldType const Type;
67+
PluginStartupInfo const& I;
68+
Guid const& PluginGuid;
69+
intptr_t CtrlId;
70+
intptr_t CancelId;
71+
intptr_t DefaultId;
72+
WideString Value;
73+
void* Handle;
74+
std::vector<FarDialogItem> Items;
5975
};
6076

61-
ConfigValueDialogImpl::ConfigValueDialogImpl(PluginStartupInfo const& i, Guid const& pluginGuid)
62-
: I(i)
77+
Dialog::Dialog(std::string const& value, ConfigFieldType type, int textId, PluginStartupInfo const& i, Guid const& pluginGuid)
78+
: Type(type)
79+
, I(i)
6380
, PluginGuid(pluginGuid)
6481
{
65-
}
66-
67-
std::pair<bool, std::string> ConfigValueDialogImpl::Show(std::string const& initValue, ConfigFieldType type, int textId) const
68-
{
69-
intptr_t y = 0;
70-
std::vector<FarDialogItem> items;
71-
items.push_back(MakeItem(DI_DOUBLEBOX, BoxPadding, ++y, Width - 1 - BoxPadding, 0));
72-
auto init = type != ConfigFieldType::Flag ? ToString(initValue) : WideString();
73-
intptr_t ctrlId = type == ConfigFieldType::Flag ? PushCheckbox(initValue, textId, y, items)
74-
: type == ConfigFieldType::Size ? PushSizebox(init.c_str(), textId, y, items)
75-
: PushEditbox(init.c_str(), textId, y, items);
76-
items.push_back(MakeItem(DI_TEXT, InnerPadding, ++y, Width - 1 - InnerPadding, 0, nullptr, DIF_SEPARATOR|DIF_BOXCOLOR));
77-
items.push_back(MakeItem(DI_BUTTON, 0, ++y, 0, 0, I.GetMsg(&PluginGuid, MOk), DIF_CENTERGROUP));
78-
items.push_back(MakeItem(DI_BUTTON, 0, y, 0, 0, I.GetMsg(&PluginGuid, MCancel), DIF_CENTERGROUP));
79-
intptr_t const cancelIndex = items.size() - 1;
80-
items.front().Y2 = ++y;
81-
82+
Items.push_back(MakeItem(DI_DOUBLEBOX, BoxPadding, Bottom(), Width - 1 - BoxPadding, 0));
83+
if (type == ConfigFieldType::Flag) PushCheckbox(value, textId);
84+
else if (type == ConfigFieldType::Size) PushSizebox(value, textId);
85+
else PushEditbox(value, textId);
86+
CtrlId = Items.size() - 1;
87+
Items.push_back(MakeItem(DI_TEXT, InnerPadding, Bottom(), Width - 1 - InnerPadding, 0, nullptr, DIF_SEPARATOR|DIF_BOXCOLOR));
88+
Items.push_back(MakeItem(DI_BUTTON, 0, Bottom(), 0, 0, I.GetMsg(&PluginGuid, MOk), DIF_CENTERGROUP));
89+
Items.push_back(MakeItem(DI_BUTTON, 0, Bottom() - 1, 0, 0, I.GetMsg(&PluginGuid, MDefault), DIF_CENTERGROUP));
90+
DefaultId = Items.size() - 1;
91+
Items.push_back(MakeItem(DI_BUTTON, 0, Bottom() - 1, 0, 0, I.GetMsg(&PluginGuid, MCancel), DIF_CENTERGROUP));
92+
CancelId = Items.size() - 1;
93+
Items.front().Y2 = Bottom();
8294
intptr_t const autocenterX = -1, autocenterY = -1;
83-
auto handle = I.DialogInit(
95+
Handle = I.DialogInit(
8496
&PluginGuid,
8597
&DialogGuid,
8698
autocenterX,
8799
autocenterY,
88100
Width,
89-
y + 2,
101+
Bottom() + 2,
90102
nullptr,
91-
items.data(),
92-
items.size(),
103+
Items.data(),
104+
Items.size(),
93105
0,
94106
FDLG_NONE,
95107
nullptr,
96108
nullptr
97109
);
98110

99-
if (handle == INVALID_HANDLE_VALUE)
111+
if (Handle == INVALID_HANDLE_VALUE)
100112
throw std::runtime_error("DialogInit: failed to create config value dialog");
113+
}
101114

102-
auto const selected = I.DialogRun(handle);
103-
if (selected == -1 || selected == cancelIndex)
104-
return std::make_pair(false, "");
115+
DialogResult Dialog::Show()
116+
{
117+
auto const selected = I.DialogRun(Handle);
118+
return selected == -1 || selected == CancelId ? DialogResult::Cancel
119+
: selected == DefaultId ? DialogResult::Default
120+
: DialogResult::Ok;
121+
}
105122

106-
std::string result = type == ConfigFieldType::Flag ? GetCheckboxValue(ctrlId, handle)
107-
: type == ConfigFieldType::Size ? GetSizeboxValue(ctrlId, handle)
108-
: GetEditboxValue(ctrlId, handle);
109-
return std::make_pair(true, std::move(result));
123+
std::string Dialog::GetValue() const
124+
{
125+
return Type == ConfigFieldType::Flag ? GetCheckboxValue()
126+
: Type == ConfigFieldType::Size ? GetSizeboxValue()
127+
: GetEditboxValue();
128+
}
129+
130+
intptr_t Dialog::Bottom() const
131+
{
132+
return !Items.empty() ? Items.back().Y1 + 1 : 1;
110133
}
111134

112-
intptr_t ConfigValueDialogImpl::PushCheckbox(std::string const& value, int textId, intptr_t& y, std::vector<FarDialogItem>& items) const
135+
void Dialog::PushCheckbox(std::string const& value, int textId)
113136
{
114-
items.push_back(MakeItem(DI_CHECKBOX, InnerPadding, ++y, 0, 0, I.GetMsg(&PluginGuid, textId), DIF_FOCUS));
115-
items.back().Selected = value == "true" ? 1 : 0;
116-
return items.size() - 1;
137+
Items.push_back(MakeItem(DI_CHECKBOX, InnerPadding, Bottom(), 0, 0, I.GetMsg(&PluginGuid, textId), DIF_FOCUS));
138+
Items.back().Selected = value == "true" ? 1 : 0;
117139
}
118140

119-
intptr_t ConfigValueDialogImpl::PushSizebox(WideString::value_type const* value, int textId, intptr_t& y, std::vector<FarDialogItem>& items) const
141+
void Dialog::PushSizebox(std::string const& value, int textId)
120142
{
121-
items.push_back(MakeItem(DI_TEXT, InnerPadding, ++y, 0, 0, I.GetMsg(&PluginGuid, textId)));
122-
items.push_back(MakeItem(DI_FIXEDIT, InnerPadding, ++y, Width - 1 - InnerPadding, 0, value, DIF_MASKEDIT|DIF_FOCUS));
123-
items.back().Mask = SizeboxMask;
124-
return items.size() - 1;
143+
Value = ToString(value);
144+
Items.push_back(MakeItem(DI_TEXT, InnerPadding, Bottom(), 0, 0, I.GetMsg(&PluginGuid, textId)));
145+
Items.push_back(MakeItem(DI_FIXEDIT, InnerPadding, Bottom(), Width - 1 - InnerPadding, 0, Value.c_str(), DIF_MASKEDIT|DIF_FOCUS));
146+
Items.back().Mask = SizeboxMask;
125147
}
126148

127-
intptr_t ConfigValueDialogImpl::PushEditbox(WideString::value_type const* value, int textId, intptr_t& y, std::vector<FarDialogItem>& items) const
149+
void Dialog::PushEditbox(std::string const& value, int textId)
128150
{
129-
items.push_back(MakeItem(DI_TEXT, InnerPadding, ++y, 0, 0, I.GetMsg(&PluginGuid, textId)));
130-
items.push_back(MakeItem(DI_EDIT, InnerPadding, ++y, Width - 1 - InnerPadding, 0, value, DIF_FOCUS));
131-
return items.size() - 1;
151+
Value = ToString(value);
152+
Items.push_back(MakeItem(DI_TEXT, InnerPadding, Bottom(), 0, 0, I.GetMsg(&PluginGuid, textId)));
153+
Items.push_back(MakeItem(DI_EDIT, InnerPadding, Bottom(), Width - 1 - InnerPadding, 0, Value.c_str(), DIF_FOCUS));
132154
}
133155

134-
WideString ConfigValueDialogImpl::GetText(intptr_t ctrlId, void* handle) const
156+
WideString Dialog::GetText() const
135157
{
136158
FarDialogItemData item = {sizeof(FarDialogItemData)};
137-
item.PtrLength = I.SendDlgMessage(handle, DM_GETTEXT, ctrlId, 0);
159+
item.PtrLength = I.SendDlgMessage(Handle, DM_GETTEXT, CtrlId, 0);
138160
std::vector<WideString::value_type> buf(item.PtrLength + 1);
139161
item.PtrData = buf.data();
140-
I.SendDlgMessage(handle, DM_GETTEXT, ctrlId, &item);
162+
I.SendDlgMessage(Handle, DM_GETTEXT, CtrlId, &item);
141163
return WideString(item.PtrData, item.PtrLength);
142164
}
143165

144-
std::string ConfigValueDialogImpl::GetCheckboxValue(intptr_t ctrlId, void* handle) const
166+
std::string Dialog::GetCheckboxValue() const
145167
{
146-
return I.SendDlgMessage(handle, DM_GETCHECK, ctrlId, 0) == BSTATE_CHECKED ? "true" : "false";
168+
return I.SendDlgMessage(Handle, DM_GETCHECK, CtrlId, 0) == BSTATE_CHECKED ? "true" : "false";
147169
}
148170

149-
std::string ConfigValueDialogImpl::GetSizeboxValue(intptr_t ctrlId, void* handle) const
171+
std::string Dialog::GetSizeboxValue() const
150172
{
151-
auto text = GetText(ctrlId, handle);
173+
auto const text = GetText();
152174
return ToStdString(text.substr(0, text.find_first_not_of(L"0123456789")));
153175
}
154176

155-
std::string ConfigValueDialogImpl::GetEditboxValue(intptr_t ctrlId, void* handle) const
177+
std::string Dialog::GetEditboxValue() const
178+
{
179+
return ToStdString(GetText());
180+
}
181+
182+
class ConfigValueDialogImpl : public Far3::ConfigValueDialog
183+
{
184+
public:
185+
ConfigValueDialogImpl(PluginStartupInfo const& i, Guid const& pluginGuid);
186+
std::pair<bool, std::string> Show(std::string const& initValue, std::string const& defaultValue, ConfigFieldType type, int textId) const override;
187+
188+
private:
189+
PluginStartupInfo const I;
190+
Guid const PluginGuid;
191+
};
192+
193+
ConfigValueDialogImpl::ConfigValueDialogImpl(PluginStartupInfo const& i, Guid const& pluginGuid)
194+
: I(i)
195+
, PluginGuid(pluginGuid)
196+
{
197+
}
198+
199+
std::pair<bool, std::string> ConfigValueDialogImpl::Show(std::string const& initValue, std::string const& defaultValue, ConfigFieldType type, int textId) const
156200
{
157-
return ToStdString(GetText(ctrlId, handle));
201+
auto result = DialogResult::Ok;
202+
do
203+
{
204+
Dialog dialog(result == DialogResult::Default ? defaultValue : initValue, type, textId, I, PluginGuid);
205+
result = dialog.Show();
206+
if (result == DialogResult::Ok)
207+
return std::make_pair(true, dialog.GetValue());
208+
}
209+
while(result == DialogResult::Default);
210+
return std::make_pair(false, "");
158211
}
159212
}
160213

far_plugin/facade/far3/src/tagseng.lng

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,4 @@
9393
"Too many errors in config file.\nPlease remove config file"
9494
"Too many lines in config file.\nPlease remove config file"
9595
"Failed to load config"
96+
"&Default"

0 commit comments

Comments
 (0)