fix bug when modifying option

This commit is contained in:
gb 2023-05-08 10:26:05 +08:00
parent f59e175aea
commit 423d54cce1
3 changed files with 36 additions and 10 deletions

View File

@ -649,7 +649,7 @@ void CDlgOptJson::to_ui(const SANEOPT& sop)
} }
} }
} }
HTREEITEM CDlgOptJson::insert_to_tree(const SANEOPT& opt) HTREEITEM CDlgOptJson::insert_new_item(const SANEOPT& opt, bool sel)
{ {
HTREEITEM item = tree_.InsertItem(opt.title.c_str()); HTREEITEM item = tree_.InsertItem(opt.title.c_str());
wchar_t* name = new wchar_t[opt.name.length() + 2]; wchar_t* name = new wchar_t[opt.name.length() + 2];
@ -658,6 +658,13 @@ HTREEITEM CDlgOptJson::insert_to_tree(const SANEOPT& opt)
if (!tree_.SetItemData(item, (DWORD_PTR)name)) if (!tree_.SetItemData(item, (DWORD_PTR)name))
delete[] name; delete[] name;
opts_.push_back(opt);
if (sel)
{
tree_.SelectItem(item);
tree_.EnsureVisible(item);
}
return item; return item;
} }
void CDlgOptJson::delete_from_tree(const wchar_t* name) void CDlgOptJson::delete_from_tree(const wchar_t* name)
@ -691,6 +698,25 @@ void CDlgOptJson::delete_from_tree(const wchar_t* name)
tree_.DeleteAllItems(); tree_.DeleteAllItems();
} }
} }
HTREEITEM CDlgOptJson::modify_tree_title(const wchar_t* name, const wchar_t* title)
{
HTREEITEM item = tree_.GetRootItem(), found = NULL;
while (item)
{
wchar_t* buf = (wchar_t*)tree_.GetItemData(item);
if (buf && wcscmp(buf, name) == 0)
{
tree_.SetItemText(item, title);
found = item;
break;
}
item = tree_.GetNextSiblingItem(item);
}
return found;
}
bool CDlgOptJson::load_from_file(const wchar_t* path_file) bool CDlgOptJson::load_from_file(const wchar_t* path_file)
{ {
@ -776,8 +802,7 @@ bool CDlgOptJson::load_from_json_text(const wchar_t* txt, std::wstring* err_msg)
if (opt.from_json(child)) if (opt.from_json(child))
{ {
opts_.push_back(opt); insert_new_item(opt, false);
insert_to_tree(opt);
} }
child->release(); child->release();
@ -1175,12 +1200,12 @@ void CDlgOptJson::OnBnClickedButtonModify()
{ {
sop.range = std::move(v.range); sop.range = std::move(v.range);
v = std::move(sop); v = std::move(sop);
modify_tree_title(v.name.c_str(), v.title.c_str());
return; return;
} }
} }
opts_.push_back(sop); insert_new_item(sop);
insert_to_tree(sop);
} }
@ -1340,6 +1365,7 @@ void CDlgOptJson::OnBnClickedButtonSet()
GotoDlgCtrl(GetDlgItem(IDC_EDIT_NAME)); GotoDlgCtrl(GetDlgItem(IDC_EDIT_NAME));
return; return;
} }
opt.name = val;
for (auto& v : opts_) for (auto& v : opts_)
{ {
if (v.name == val) if (v.name == val)
@ -1348,9 +1374,9 @@ void CDlgOptJson::OnBnClickedButtonSet()
break; break;
} }
} }
val = get_item_text(IDC_COMBO_DATA_TYPE);
if (!opt.name.empty()) if (!opt.name.empty())
{ {
val = get_item_text(IDC_COMBO_DATA_TYPE);
if (val == L"int") if (val == L"int")
{ {
dlg.init_ = std::to_wstring(*(int*)opt.def_val.c_str()); dlg.init_ = std::to_wstring(*(int*)opt.def_val.c_str());
@ -1423,9 +1449,8 @@ void CDlgOptJson::OnBnClickedButtonSet()
from_ui(so); from_ui(so);
so.range = opt.range; so.range = opt.range;
so.def_val = opt.def_val; so.def_val = opt.def_val;
opts_.push_back(opt); insert_new_item(so);
insert_to_tree(opt); to_ui(so);
to_ui(opt);
} }
} }

View File

@ -145,8 +145,9 @@ protected:
void from_ui(SANEOPT& sop); void from_ui(SANEOPT& sop);
void to_ui(const SANEOPT& sop); void to_ui(const SANEOPT& sop);
HTREEITEM insert_to_tree(const SANEOPT& opt); HTREEITEM insert_new_item(const SANEOPT& opt, bool sel = true);
void delete_from_tree(const wchar_t* name = NULL); void delete_from_tree(const wchar_t* name = NULL);
HTREEITEM modify_tree_title(const wchar_t* name, const wchar_t* title);
bool load_from_file(const wchar_t* path_file); bool load_from_file(const wchar_t* path_file);
bool load_from_json_text(const wchar_t* txt, std::wstring* err_msg = NULL); bool load_from_json_text(const wchar_t* txt, std::wstring* err_msg = NULL);