mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-05-10 02:09:46 +08:00
refactor(controller): 拆分回调函数到模块化文件并添加字符串列表访问器
- 将 callbacks.c 拆分为多个按功能分类的文件(callbacks_basic.c、callbacks_nav.c、callbacks_search.c、callbacks_io.c、callbacks_sys.c) - 新增 callbacks_internal.h 提供内部辅助函数声明,减少代码重复 - 在 string_ext 模块中添加 string_list_get 和 string_list_set 安全访问器函数 - 更新 CMakeLists.txt 和 ui_utils.c 以使用新的模块结构和访问器 - 重构旨在提高代码可维护性和可读性,便于后续功能扩展
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
#include "controller/callbacks.h"
|
||||
#include "controller/callbacks_internal.h"
|
||||
#include "core/path_manager.h"
|
||||
#include "core/lua_config.h"
|
||||
#include "utils/string_ext.h"
|
||||
#include "utils/safe_string.h"
|
||||
#include "utils/error_code.h"
|
||||
#include "utils/logger.h"
|
||||
#include "utils/ui_constants.h"
|
||||
#include "ui/ui_utils.h"
|
||||
#include "ui/dialogs.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// 按钮回调:新建
|
||||
int btn_new_cb(Ihandle *self)
|
||||
{
|
||||
Ihandle *dlg = IupGetDialog(self);
|
||||
char buffer[PATH_BUFFER_SIZE] = "";
|
||||
if (custom_input_dialog("新建环境变量", "请输入路径:", buffer, sizeof(buffer)))
|
||||
{
|
||||
if (strlen(buffer) > 0)
|
||||
{
|
||||
StringList *raw_data = get_current_raw_data(dlg);
|
||||
add_string_list(raw_data, buffer);
|
||||
|
||||
Ihandle *current_list = get_current_list(dlg);
|
||||
sync_string_list_to_ui(current_list, raw_data);
|
||||
|
||||
int count = IupGetInt(current_list, "COUNT");
|
||||
IupSetInt(current_list, "VALUE", count);
|
||||
}
|
||||
}
|
||||
return IUP_DEFAULT;
|
||||
}
|
||||
|
||||
// 按钮回调:编辑
|
||||
int btn_edit_cb(Ihandle *self)
|
||||
{
|
||||
Ihandle *dlg = IupGetDialog(self);
|
||||
Ihandle *current_list = get_current_list(dlg);
|
||||
int selected = IupGetInt(current_list, "VALUE");
|
||||
if (selected == 0)
|
||||
return IUP_DEFAULT;
|
||||
|
||||
StringList *raw_data = get_current_raw_data(dlg);
|
||||
if (selected - 1 >= raw_data->count)
|
||||
return IUP_DEFAULT;
|
||||
|
||||
char buffer[PATH_BUFFER_SIZE];
|
||||
safe_strcpy(buffer, sizeof(buffer), string_list_get(raw_data, selected - 1));
|
||||
|
||||
if (custom_input_dialog("编辑环境变量", "编辑路径:", buffer, sizeof(buffer)))
|
||||
{
|
||||
if (strlen(buffer) > 0)
|
||||
{
|
||||
string_list_set(raw_data, selected - 1, buffer);
|
||||
|
||||
sync_string_list_to_ui(current_list, raw_data);
|
||||
IupSetInt(current_list, "VALUE", selected);
|
||||
}
|
||||
}
|
||||
return IUP_DEFAULT;
|
||||
}
|
||||
|
||||
// 双击回调
|
||||
int list_dblclick_cb(Ihandle *self, int item, char *text)
|
||||
{
|
||||
if (item > 0)
|
||||
{
|
||||
IupSetInt(self, "VALUE", item);
|
||||
btn_edit_cb(self);
|
||||
}
|
||||
return IUP_DEFAULT;
|
||||
}
|
||||
|
||||
// 按钮回调:浏览
|
||||
int btn_browse_cb(Ihandle *self)
|
||||
{
|
||||
Ihandle *dlg = IupGetDialog(self);
|
||||
Ihandle *filedlg = IupFileDlg();
|
||||
IupSetAttribute(filedlg, "DIALOGTYPE", "DIR");
|
||||
IupSetAttribute(filedlg, "TITLE", lua_config_get_string("dialog", "select_dir"));
|
||||
|
||||
IupPopup(filedlg, IUP_CENTER, IUP_CENTER);
|
||||
|
||||
if (IupGetInt(filedlg, "STATUS") != -1)
|
||||
{
|
||||
char *value = IupGetAttribute(filedlg, "VALUE");
|
||||
if (value)
|
||||
{
|
||||
StringList *raw_data = get_current_raw_data(dlg);
|
||||
add_string_list(raw_data, value);
|
||||
|
||||
Ihandle *current_list = get_current_list(dlg);
|
||||
sync_string_list_to_ui(current_list, raw_data);
|
||||
|
||||
int count = IupGetInt(current_list, "COUNT");
|
||||
IupSetInt(current_list, "VALUE", count);
|
||||
}
|
||||
}
|
||||
IupDestroy(filedlg);
|
||||
return IUP_DEFAULT;
|
||||
}
|
||||
|
||||
// 按钮回调:删除
|
||||
int btn_del_cb(Ihandle *self)
|
||||
{
|
||||
Ihandle *dlg = IupGetDialog(self);
|
||||
Ihandle *current_list = get_current_list(dlg);
|
||||
int selected = IupGetInt(current_list, "VALUE");
|
||||
|
||||
if (selected == 0)
|
||||
{
|
||||
IupMessage("提示", "请先选择要删除的项");
|
||||
return IUP_DEFAULT;
|
||||
}
|
||||
|
||||
StringList *raw_data = get_current_raw_data(dlg);
|
||||
ErrorCode result = path_manager_remove_at(raw_data, selected - 1);
|
||||
if (result != ERR_OK)
|
||||
{
|
||||
log_error("Failed to remove path at index %d", selected - 1);
|
||||
}
|
||||
|
||||
sync_string_list_to_ui(current_list, raw_data);
|
||||
|
||||
Ihandle *lbl_status = IupGetDialogChild(dlg, CTRL_LBL_STATUS);
|
||||
if (lbl_status)
|
||||
IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "deleted"));
|
||||
|
||||
return IUP_DEFAULT;
|
||||
}
|
||||
Reference in New Issue
Block a user