Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion far_plugin/facade/far3/include/far3/config_value_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ namespace Far3
static std::unique_ptr<const ConfigValueDialog> Create(PluginStartupInfo const& i, Guid const& pluginGuid);

virtual ~ConfigValueDialog() = default;
virtual std::pair<bool, std::string> Show(std::string const& initValue, Plugin::ConfigFieldType type, int textId) const = 0;
virtual std::pair<bool, std::string> Show(std::string const& initValue, std::string const& defaultValue, Plugin::ConfigFieldType type, int textId) const = 0;
};
}
1 change: 1 addition & 0 deletions far_plugin/facade/far3/include/far3/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,5 @@ enum{
MTooManyErrorsInConfigFile,
MTooManyLinesInConfigFile,
MFailedLoadConfig,
MDefault,
};
3 changes: 2 additions & 1 deletion far_plugin/facade/far3/src/config_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ namespace

auto const selectedField = FieldsTexts.at(selected);
auto const selectedFieldData = dataMapper.Get(static_cast<int>(selectedField.first), config);
auto value = configureValueDialog->Show(selectedFieldData.value, selectedFieldData.type, selectedField.second);
auto const defaultValue = dataMapper.Get(static_cast<int>(selectedField.first), Plugin::Config()).value;
auto value = configureValueDialog->Show(selectedFieldData.value, defaultValue, selectedFieldData.type, selectedField.second);
if (value.first)
return {selectedField.first, value.second};
}
Expand Down
183 changes: 118 additions & 65 deletions far_plugin/facade/far3/src/config_value_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,123 +38,176 @@ namespace
return result;
}

class ConfigValueDialogImpl : public Far3::ConfigValueDialog
enum class DialogResult
{
Ok,
Cancel,
Default,
};

class Dialog
{
public:
ConfigValueDialogImpl(PluginStartupInfo const& i, Guid const& pluginGuid);
std::pair<bool, std::string> Show(std::string const& initValue, ConfigFieldType type, int textId) const override;
Dialog(std::string const& value, ConfigFieldType type, int textId, PluginStartupInfo const& i, Guid const& pluginGuid);
DialogResult Show();
std::string GetValue() const;

private:
intptr_t PushCheckbox(std::string const& value, int textId, intptr_t& y, std::vector<FarDialogItem>& items) const;
intptr_t PushSizebox(WideString::value_type const* value, int textId, intptr_t& y, std::vector<FarDialogItem>& items) const;
intptr_t PushEditbox(WideString::value_type const* value, int textId, intptr_t& y, std::vector<FarDialogItem>& items) const;
WideString GetText(intptr_t ctrlId, void* handle) const;
std::string GetCheckboxValue(intptr_t ctrlId, void* handle) const;
std::string GetSizeboxValue(intptr_t ctrlId, void* handle) const;
std::string GetEditboxValue(intptr_t ctrlId, void* handle) const;
intptr_t Bottom() const;
void PushCheckbox(std::string const& value, int textId);
void PushSizebox(std::string const& value, int textId);
void PushEditbox(std::string const& value, int textId);
WideString GetText() const;
std::string GetCheckboxValue() const;
std::string GetSizeboxValue() const;
std::string GetEditboxValue() const;

private:
PluginStartupInfo const I;
Guid const PluginGuid;
ConfigFieldType const Type;
PluginStartupInfo const& I;
Guid const& PluginGuid;
intptr_t CtrlId;
intptr_t CancelId;
intptr_t DefaultId;
WideString Value;
void* Handle;
std::vector<FarDialogItem> Items;
};

ConfigValueDialogImpl::ConfigValueDialogImpl(PluginStartupInfo const& i, Guid const& pluginGuid)
: I(i)
Dialog::Dialog(std::string const& value, ConfigFieldType type, int textId, PluginStartupInfo const& i, Guid const& pluginGuid)
: Type(type)
, I(i)
, PluginGuid(pluginGuid)
{
}

std::pair<bool, std::string> ConfigValueDialogImpl::Show(std::string const& initValue, ConfigFieldType type, int textId) const
{
intptr_t y = 0;
std::vector<FarDialogItem> items;
items.push_back(MakeItem(DI_DOUBLEBOX, BoxPadding, ++y, Width - 1 - BoxPadding, 0));
auto init = type != ConfigFieldType::Flag ? ToString(initValue) : WideString();
intptr_t ctrlId = type == ConfigFieldType::Flag ? PushCheckbox(initValue, textId, y, items)
: type == ConfigFieldType::Size ? PushSizebox(init.c_str(), textId, y, items)
: PushEditbox(init.c_str(), textId, y, items);
items.push_back(MakeItem(DI_TEXT, InnerPadding, ++y, Width - 1 - InnerPadding, 0, nullptr, DIF_SEPARATOR|DIF_BOXCOLOR));
items.push_back(MakeItem(DI_BUTTON, 0, ++y, 0, 0, I.GetMsg(&PluginGuid, MOk), DIF_CENTERGROUP));
items.push_back(MakeItem(DI_BUTTON, 0, y, 0, 0, I.GetMsg(&PluginGuid, MCancel), DIF_CENTERGROUP));
intptr_t const cancelIndex = items.size() - 1;
items.front().Y2 = ++y;

Items.push_back(MakeItem(DI_DOUBLEBOX, BoxPadding, Bottom(), Width - 1 - BoxPadding, 0));
if (type == ConfigFieldType::Flag) PushCheckbox(value, textId);
else if (type == ConfigFieldType::Size) PushSizebox(value, textId);
else PushEditbox(value, textId);
CtrlId = Items.size() - 1;
Items.push_back(MakeItem(DI_TEXT, InnerPadding, Bottom(), Width - 1 - InnerPadding, 0, nullptr, DIF_SEPARATOR|DIF_BOXCOLOR));
Items.push_back(MakeItem(DI_BUTTON, 0, Bottom(), 0, 0, I.GetMsg(&PluginGuid, MOk), DIF_CENTERGROUP));
Items.push_back(MakeItem(DI_BUTTON, 0, Bottom() - 1, 0, 0, I.GetMsg(&PluginGuid, MDefault), DIF_CENTERGROUP));
DefaultId = Items.size() - 1;
Items.push_back(MakeItem(DI_BUTTON, 0, Bottom() - 1, 0, 0, I.GetMsg(&PluginGuid, MCancel), DIF_CENTERGROUP));
CancelId = Items.size() - 1;
Items.front().Y2 = Bottom();
intptr_t const autocenterX = -1, autocenterY = -1;
auto handle = I.DialogInit(
Handle = I.DialogInit(
&PluginGuid,
&DialogGuid,
autocenterX,
autocenterY,
Width,
y + 2,
Bottom() + 2,
nullptr,
items.data(),
items.size(),
Items.data(),
Items.size(),
0,
FDLG_NONE,
nullptr,
nullptr
);

if (handle == INVALID_HANDLE_VALUE)
if (Handle == INVALID_HANDLE_VALUE)
throw std::runtime_error("DialogInit: failed to create config value dialog");
}

auto const selected = I.DialogRun(handle);
if (selected == -1 || selected == cancelIndex)
return std::make_pair(false, "");
DialogResult Dialog::Show()
{
auto const selected = I.DialogRun(Handle);
return selected == -1 || selected == CancelId ? DialogResult::Cancel
: selected == DefaultId ? DialogResult::Default
: DialogResult::Ok;
}

std::string result = type == ConfigFieldType::Flag ? GetCheckboxValue(ctrlId, handle)
: type == ConfigFieldType::Size ? GetSizeboxValue(ctrlId, handle)
: GetEditboxValue(ctrlId, handle);
return std::make_pair(true, std::move(result));
std::string Dialog::GetValue() const
{
return Type == ConfigFieldType::Flag ? GetCheckboxValue()
: Type == ConfigFieldType::Size ? GetSizeboxValue()
: GetEditboxValue();
}

intptr_t Dialog::Bottom() const
{
return !Items.empty() ? Items.back().Y1 + 1 : 1;
}

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

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

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

WideString ConfigValueDialogImpl::GetText(intptr_t ctrlId, void* handle) const
WideString Dialog::GetText() const
{
FarDialogItemData item = {sizeof(FarDialogItemData)};
item.PtrLength = I.SendDlgMessage(handle, DM_GETTEXT, ctrlId, 0);
item.PtrLength = I.SendDlgMessage(Handle, DM_GETTEXT, CtrlId, 0);
std::vector<WideString::value_type> buf(item.PtrLength + 1);
item.PtrData = buf.data();
I.SendDlgMessage(handle, DM_GETTEXT, ctrlId, &item);
I.SendDlgMessage(Handle, DM_GETTEXT, CtrlId, &item);
return WideString(item.PtrData, item.PtrLength);
}

std::string ConfigValueDialogImpl::GetCheckboxValue(intptr_t ctrlId, void* handle) const
std::string Dialog::GetCheckboxValue() const
{
return I.SendDlgMessage(handle, DM_GETCHECK, ctrlId, 0) == BSTATE_CHECKED ? "true" : "false";
return I.SendDlgMessage(Handle, DM_GETCHECK, CtrlId, 0) == BSTATE_CHECKED ? "true" : "false";
}

std::string ConfigValueDialogImpl::GetSizeboxValue(intptr_t ctrlId, void* handle) const
std::string Dialog::GetSizeboxValue() const
{
auto text = GetText(ctrlId, handle);
auto const text = GetText();
return ToStdString(text.substr(0, text.find_first_not_of(L"0123456789")));
}

std::string ConfigValueDialogImpl::GetEditboxValue(intptr_t ctrlId, void* handle) const
std::string Dialog::GetEditboxValue() const
{
return ToStdString(GetText());
}

class ConfigValueDialogImpl : public Far3::ConfigValueDialog
{
public:
ConfigValueDialogImpl(PluginStartupInfo const& i, Guid const& pluginGuid);
std::pair<bool, std::string> Show(std::string const& initValue, std::string const& defaultValue, ConfigFieldType type, int textId) const override;

private:
PluginStartupInfo const I;
Guid const PluginGuid;
};

ConfigValueDialogImpl::ConfigValueDialogImpl(PluginStartupInfo const& i, Guid const& pluginGuid)
: I(i)
, PluginGuid(pluginGuid)
{
}

std::pair<bool, std::string> ConfigValueDialogImpl::Show(std::string const& initValue, std::string const& defaultValue, ConfigFieldType type, int textId) const
{
return ToStdString(GetText(ctrlId, handle));
auto result = DialogResult::Ok;
do
{
Dialog dialog(result == DialogResult::Default ? defaultValue : initValue, type, textId, I, PluginGuid);
result = dialog.Show();
if (result == DialogResult::Ok)
return std::make_pair(true, dialog.GetValue());
}
while(result == DialogResult::Default);
return std::make_pair(false, "");
}
}

Expand Down
1 change: 1 addition & 0 deletions far_plugin/facade/far3/src/tagseng.lng
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@
"Too many errors in config file.\nPlease remove config file"
"Too many lines in config file.\nPlease remove config file"
"Failed to load config"
"&Default"