12 Commits

Author SHA1 Message Date
Serendipity 6509ef98e4 chore: 清理旧版IUP库文件并更新头文件路径
删除旧的iup-3.31_Win64_dllw6_lib目录下的所有DLL和静态库文件
将IUP头文件从旧目录迁移到新的libs/IUP/include统一路径
更新CMakeLists.txt中的包含路径和库链接配置
简化DLL复制逻辑,只复制核心iup.dll文件
2026-03-19 20:14:06 +08:00
Serendipity c928c271e8 chore: 移除构建安装程序前的DLL复制步骤
不再需要手动复制IUP DLL文件,因为安装脚本已直接引用库目录。
2026-03-19 13:23:03 +08:00
Serendipity 02e702b285 fix(构建): 修复IUP DLL复制命令的路径变量
将CMAKE_SOURCE_DIR更改为CMAKE_CURRENT_SOURCE_DIR以确保在子目录中也能正确找到DLL文件。
移除不必要的条件判断,使复制命令始终执行。
2026-03-19 12:40:01 +08:00
Serendipity af3138c146 build: 重构 CMakeLists.txt 以使用现代 CMake 最佳实践
- 将项目声明更新为包含版本和语言
- 启用 RC 语言以正确处理资源文件
- 使用 target_* 命令替代全局命令(如 include_directories、link_directories)
- 将资源文件直接加入源文件列表,简化构建定义
- 优化 DLL 复制逻辑,使用 file(GLOB) 和 copy_if_different
- 改进编译器选项的条件设置,增强跨编译器兼容性
2026-03-19 12:37:41 +08:00
Serendipity 6e6adf3b85 chore: 迁移构建系统并清理遗留的二进制文件
- 删除 bin/ 目录下遗留的 DLL 和可执行文件,它们现在由 CMake 构建过程自动复制
- 更新 CMakeLists.txt,明确设置 C17 标准并优化编译选项
- 更新 Inno Setup 安装脚本,使其从 build/ 目录获取构建产物
- 更新 main.c 中的编译说明,反映当前基于 CMake 的构建流程
2026-03-19 12:32:54 +08:00
Serendipity e84b33c5ca build: 迁移项目构建系统至 CMake
- 新增 CMakeLists.txt 文件,定义项目构建规则、依赖和编译选项。
- 更新 README.md 文档,推荐使用 CMake 进行构建,并说明新旧构建方式。
- 保留原有的 Makefile 支持以保持向后兼容。
2026-03-19 12:07:01 +08:00
Serendipity ac6b409f3a feat: 为只读模式添加专用应用程序标题
在非管理员权限下运行时,将对话框标题从硬编码字符串改为使用配置文件中定义的 APP_NAME_READONLY 宏。这提高了代码的可维护性和一致性,使标题文本集中管理,便于未来修改。
2026-03-18 22:37:33 +08:00
Serendipity 1bbe95582a refactor: 将应用程序名称提取为配置常量
- 在 config.h 中定义 APP_NAME 常量,提高可维护性
- 将 main.c 中的对话框标题硬编码替换为使用 APP_NAME
2026-03-18 22:33:25 +08:00
Serendipity 3ecf35963d feat(ui): 增加对话框最小尺寸并调整默认大小
- 将对话框默认大小从500x400调整为800x800
- 添加MINSIZE属性确保对话框不可缩小
- 清理ui.c中的多余空白字符
- 在main.c中添加编译和打包说明注释
2026-03-18 22:03:57 +08:00
Serendipity 276d2c5fe3 docs(config.h): 为UI配置常量添加注释说明 2026-03-18 21:09:13 +08:00
Serendipity a9339f9b9f style(config): 统一宏定义格式并更新文档
调整 config.h 中 UI_DLG_SIZE 宏定义的对齐格式以保持代码风格一致。
在 README.md 中新增“架构与二次开发”章节,说明项目的模块化设计和配置管理方式。
2026-03-18 21:06:55 +08:00
Serendipity 7fac2aab35 refactor: 重构代码以提取配置和全局变量
- 将 Windows 消息常量和 UI 配置常量分别提取到 globals.h 和 config.h 头文件中,提高可维护性
- 将全局变量和控件定义从 main.c 移至独立的 globals.c 源文件,实现关注点分离
- 更新 Makefile 以包含新的源文件 globals.c
- 在 ui.c 和 main.c 中引用 config.h,使用配置常量替代硬编码的 UI 参数
2026-03-18 21:01:50 +08:00
106 changed files with 861 additions and 1520 deletions
+72
View File
@@ -0,0 +1,72 @@
cmake_minimum_required(VERSION 3.10)
project(PathEditor VERSION 3.0 LANGUAGES C)
# 启用资源编译器以处理 .rc 文件
enable_language(RC)
# 设置 C 标准
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF) # 禁用特定编译器的扩展(如 gnu17),强制使用标准 C17
# 定义源文件
set(SOURCES
src/main.c
src/utils.c
src/registry.c
src/callbacks.c
src/ui.c
src/globals.c
ico/resources.rc
)
# 创建 GUI 可执行文件(WIN32 属性会自动添加 -mwindows 参数)
add_executable(${PROJECT_NAME} WIN32 ${SOURCES})
# 添加宏定义
target_compile_definitions(${PROJECT_NAME} PRIVATE
_WIN32
UNICODE
_UNICODE
)
# 添加编译选项
if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
target_compile_options(${PROJECT_NAME} PRIVATE
-Wall
-O2
-fexec-charset=UTF-8
)
endif()
# 设置头文件搜索路径
target_include_directories(${PROJECT_NAME} PRIVATE
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/libs/IUP/include
)
# 设置库文件搜索路径
target_link_directories(${PROJECT_NAME} PRIVATE
${CMAKE_SOURCE_DIR}/libs/IUP
)
# 链接所需库
target_link_libraries(${PROJECT_NAME} PRIVATE
iup
iupcd
gdi32
comdlg32
comctl32
uuid
ole32
advapi32
)
# 编译完成后,仅将程序实际需要的核心 DLL 文件复制到构建输出目录
set(IUP_REQUIRED_DLLS "${CMAKE_CURRENT_SOURCE_DIR}/libs/IUP/iup.dll")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${IUP_REQUIRED_DLLS}
"$<TARGET_FILE_DIR:${PROJECT_NAME}>"
COMMENT "Copying required DLLs to build directory..."
)
+6 -15
View File
@@ -18,9 +18,9 @@ CFLAGS = -Wall -O2 -I$(INCLUDE_DIR) -I$(LOCAL_INCLUDE_DIR) -D_WIN32 -DUNICODE -D
LDFLAGS = -L$(LIB_DIR) -liup -liupcd -lgdi32 -lcomdlg32 -lcomctl32 -luuid -lole32 -ladvapi32 -mwindows
# Source
SRC = src/main.c src/utils.c src/registry.c src/ui.c src/ui_utils.c src/cb_edit.c src/cb_file.c src/cb_main.c src/globals.c
SRC = src/main.c src/utils.c src/registry.c src/callbacks.c src/ui.c src/globals.c
RES = ico/resources.rc
OBJ = $(OBJ_DIR)/main.o $(OBJ_DIR)/utils.o $(OBJ_DIR)/registry.o $(OBJ_DIR)/ui.o $(OBJ_DIR)/ui_utils.o $(OBJ_DIR)/cb_edit.o $(OBJ_DIR)/cb_file.o $(OBJ_DIR)/cb_main.o $(OBJ_DIR)/globals.o $(OBJ_DIR)/resources.o
OBJ = $(OBJ_DIR)/main.o $(OBJ_DIR)/utils.o $(OBJ_DIR)/registry.o $(OBJ_DIR)/callbacks.o $(OBJ_DIR)/ui.o $(OBJ_DIR)/globals.o $(OBJ_DIR)/resources.o
EXE = $(BIN_DIR)/PathEditor.exe
all: $(BIN_DIR) $(OBJ_DIR) $(EXE)
@@ -43,21 +43,12 @@ $(OBJ_DIR)/utils.o: src/utils.c
$(OBJ_DIR)/registry.o: src/registry.c
$(CC) $(CFLAGS) -c -o $@ $<
$(OBJ_DIR)/callbacks.o: src/callbacks.c
$(CC) $(CFLAGS) -c -o $@ $<
$(OBJ_DIR)/ui.o: src/ui.c
$(CC) $(CFLAGS) -c -o $@ $<
$(OBJ_DIR)/ui_utils.o: src/ui_utils.c
$(CC) $(CFLAGS) -c -o $@ $<
$(OBJ_DIR)/cb_edit.o: src/cb_edit.c
$(CC) $(CFLAGS) -c -o $@ $<
$(OBJ_DIR)/cb_file.o: src/cb_file.c
$(CC) $(CFLAGS) -c -o $@ $<
$(OBJ_DIR)/cb_main.o: src/cb_main.c
$(CC) $(CFLAGS) -c -o $@ $<
$(OBJ_DIR)/globals.o: src/globals.c
$(CC) $(CFLAGS) -c -o $@ $<
@@ -66,4 +57,4 @@ $(OBJ_DIR)/resources.o: ico/resources.rc
clean:
if exist $(OBJ_DIR)\*.o del /Q $(OBJ_DIR)\*.o
if exist $(BIN_DIR)\*.exe del /Q $(BIN_DIR)\*.exe
if exist $(BIN_DIR)\*.exe del /Q $(BIN_DIR)\*.exe
+24 -27
View File
@@ -10,25 +10,19 @@
* **只读模式**:非管理员运行时自动切换到只读模式,防止误操作。
* **权限检测**:智能检测当前运行权限。
* **📑 双视图与预览**
* **分离管理**完美支持 **System (系统变量)****User (用户变量)** 的分离查看与编辑。
* **合并预览**:新增标签页,展示最终解析后的完整 PATH 顺序(系统在前,用户在后),方便排查冲突
* **变量展开**:鼠标悬停在包含变量(如 `%JAVA_HOME%`)的路径上时,自动显示解析后的绝对路径。
* **📑 双视图管理**
* 完美支持 **System (系统变量)****User (用户变量)** 的分离查看与编辑。
* 清晰的 Tab 标签页切换
* **🔴 智能诊断与维护**
* **无效路径高亮**:自动检测路径是否存在,不存在的显示为红色。
* **重复路径高亮**:自动检测重复项,重复的显示为橙色。
* **一键清理**:智能移除所有无效和重复的路径,保持环境整洁。
* **📂 高交互**
* **多选支持**:支持使用 `Ctrl``Shift` 进行多选,批量删除或移动路径。
* **撤销/重做**:支持 `Ctrl+Z` 撤销和 `Ctrl+Y` 重做,防止误操作,并提供直观的工具栏按钮。
* **📂 高交互**
* **拖拽支持**:直接将文件夹拖入窗口即可添加(支持管理员模式下的 UIPI 穿透)。
* **实时搜索**:顶部搜索框支持不区分大小写的实时过滤查找(支持快捷键 `Ctrl+F`
* **全局快捷键**:支持 `Ctrl+N` 新建、`Ctrl+S` 保存、`Delete` 删除等快捷操作
* **🎨 个性化**
* **深色模式**:一键切换深色主题,保护视力。
* **实时搜索**:顶部搜索框支持不区分大小写的实时过滤查找。
* **快捷键**:支持 Delete 键快速删除选中项
* **便捷管理**
* **新建**:添加新路径到列表。
@@ -36,7 +30,6 @@
* ✏️ **编辑**:双击或点击按钮修改现有路径。
* 🗑️ **删除**:移除不需要的路径。
* ⬆️⬇️ **排序**:上移/下移调整路径优先级。
* 📥/📤 **导入导出**:支持将环境变量导出为 `.txt` 文本文件备份,或从文件导入恢复。
* **轻量级**:原生 C 语言编写,无臃肿依赖,运行速度极快。
@@ -61,11 +54,13 @@
* Windows 操作系统
* GCC 编译器 (推荐 MinGW-w64)
* Make 工具
* CMake 工具 (推荐使用 CMake 构建)
* IUP 库 (已包含在 `libs` 目录下)
* Inno Setup 6 (仅打包需要)
### 编译步骤
### 编译步骤 (推荐使用 CMake)
本项目已迁移至 CMake 构建系统,支持生成更标准的构建文件并集成到各大 IDE。
1. 克隆仓库:
@@ -74,14 +69,20 @@
cd PathEditor
```
2. 编译项目
2. 使用 CMake 配置和编译:
```bash
mingw32-make
# 生成构建系统 (以 MinGW 为例)
cmake -B build -G "MinGW Makefiles"
# 编译项目
cmake --build build
```
3. 运行:
编译成功后,可执行文件位于 `bin/PathEditor.exe`。
编译成功后,可执行文件位于 `build/PathEditor.exe`。
*(注:项目依然保留了传统的 `mingw32-make` 方式,您可以直接在根目录运行 `mingw32-make` 进行编译,产物位于 `bin/` 目录。)*
### 打包 (可选)
@@ -97,16 +98,12 @@
2. **查看**:程序启动后会自动加载当前的系统 PATH 变量。
* **红色**条目表示路径不存在。
* **橙色**条目表示路径重复。
* **变量预览**:鼠标悬停在带 `%` 的变量上可查看实际路径
3. **搜索**:在顶部输入关键词或按 `Ctrl+F` 可快速筛选。
3. **搜索**:在顶部输入关键词可快速筛选
4. **修改**
* **拖拽**:将文件夹拖入列表可直接添加。
* **多选**:按住 `Ctrl` 或 `Shift` 可选中多项进行批量删除
* **撤销/重做**:误操作时可使用 `Ctrl+Z` / `Ctrl+Y` 或工具栏按钮回退
* **常规操作**:使用右侧按钮栏进行新建、编辑、移动等操作
* **清理**:点击“一键清理”可自动删除无效和重复项。
* **导入/导出**:使用导入导出功能备份或恢复配置。
5. **保存**:操作完成后,务必点击底部的【确定】按钮(或按 `Ctrl+S`)保存更改。
* 拖拽文件夹列表可直接添加。
* 使用右侧按钮栏进行常规操作
* 点击“一键清理”可自动删除无效和重复项
5. **保存**:操作完成后,务必点击底部的【确定】按钮保存更改
6. **生效**:保存后,某些正在运行的程序可能需要重启才能识别新的环境变量。CMD 或 PowerShell 窗口需要重新打开。
## 👤 作者信息
Binary file not shown.
Binary file not shown.
-3
View File
@@ -1,7 +1,4 @@
@echo off
echo Copying DLLs...
if not exist bin mkdir bin
copy /Y "libs\iup-3.31_Win64_dllw6_lib\*.dll" bin\
echo Building Installer...
"D:\Program Files (x86)\Inno Setup 6\ISCC.exe" "dist\installer.iss"
+2 -2
View File
@@ -37,8 +37,8 @@ Name: "english"; MessagesFile: "compiler:Default.isl"
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
[Files]
Source: "d:\Code\doing_exercises\programs\PathEditor\bin\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
Source: "d:\Code\doing_exercises\programs\PathEditor\bin\*.dll"; DestDir: "{app}"; Flags: ignoreversion
Source: "d:\Code\doing_exercises\programs\PathEditor\build\{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion
Source: "d:\Code\doing_exercises\programs\PathEditor\build\*.dll"; DestDir: "{app}"; Flags: ignoreversion
; NOTE: Don't use "Flags: ignoreversion" on any shared system files
[Icons]
+30
View File
@@ -0,0 +1,30 @@
#ifndef CALLBACKS_H
#define CALLBACKS_H
#include <iup.h>
// 按钮回调
int btn_new_cb(Ihandle *self);
int btn_edit_cb(Ihandle *self);
int btn_browse_cb(Ihandle *self);
int btn_del_cb(Ihandle *self);
int btn_up_cb(Ihandle *self);
int btn_down_cb(Ihandle *self);
int btn_clean_cb(Ihandle *self);
int btn_ok_cb(Ihandle *self);
int btn_cancel_cb(Ihandle *self);
int btn_help_cb(Ihandle *self);
// 搜索回调
int txt_search_cb(Ihandle *self);
// 双击回调
int list_dblclick_cb(Ihandle *self, int item, char *text);
// 拖拽回调
int list_dropfiles_cb(Ihandle *self, const char *filename, int num, int x, int y);
// 键盘按键回调
int list_k_any_cb(Ihandle *self, int c);
#endif // CALLBACKS_H
-15
View File
@@ -1,15 +0,0 @@
#ifndef CB_EDIT_H
#define CB_EDIT_H
#include <iup.h>
// 编辑相关回调
int btn_new_cb(Ihandle *self);
int btn_edit_cb(Ihandle *self);
int list_dblclick_cb(Ihandle *self, int item, char *text);
int btn_del_cb(Ihandle *self);
int btn_up_cb(Ihandle *self);
int btn_down_cb(Ihandle *self);
int btn_clean_cb(Ihandle *self);
#endif // CB_EDIT_H
-14
View File
@@ -1,14 +0,0 @@
#ifndef CB_FILE_H
#define CB_FILE_H
#include <iup.h>
// 文件和历史记录回调
int btn_browse_cb(Ihandle *self);
int btn_undo_cb(Ihandle *self);
int btn_redo_cb(Ihandle *self);
int btn_export_cb(Ihandle *self);
int btn_import_cb(Ihandle *self);
int list_dropfiles_cb(Ihandle *self, const char *filename, int num, int x, int y);
#endif // CB_FILE_H
-17
View File
@@ -1,17 +0,0 @@
#ifndef CB_MAIN_H
#define CB_MAIN_H
#include <iup.h>
// 主界面交互回调
int txt_search_cb(Ihandle *self);
int list_k_any_cb(Ihandle *self, int c);
int list_motion_cb(Ihandle *self, int x, int y, char *status);
int dialog_k_any_cb(Ihandle *self, int c);
int btn_ok_cb(Ihandle *self);
int btn_cancel_cb(Ihandle *self);
int btn_help_cb(Ihandle *self);
int tabs_tabchange_cb(Ihandle *self, int new_pos, int old_pos);
int btn_theme_cb(Ihandle *self);
#endif // CB_MAIN_H
+25 -15
View File
@@ -1,23 +1,33 @@
#ifndef CONFIG_H
#define CONFIG_H
// UI 常量定义
#define UI_WINDOW_TITLE "Path Editor" // 窗口标题
#define UI_WINDOW_SIZE "800x800" // 窗口默认大小 (像素)
// ============================================================================
// UI的配置常量
// ============================================================================
// 按钮尺寸
#define UI_BUTTON_SIZE "100x32" // 按钮默认大小 (像素)
#define UI_BUTTON_SMALL_SIZE "80x32" // 小按钮大小 (像素)
// 应用程序名称
#define APP_NAME "PathEditor" // 编辑环境变量应用程序名称
#define APP_NAME_READONLY "PathEditor (只读模式)" // 编辑环境变量只读模式标题
// 布局间距
#define UI_MARGIN_MAIN "10x10" // 主布局外边距 (像素)
#define UI_GAP_MAIN "10" // 主布局间距 (像素)
#define UI_GAP_BUTTONS "5" // 按钮间距 (像素)
#define UI_GAP_BOTTOM "10" // 底部间距 (像素)
// 对话框设置
#define UI_DLG_SIZE "800x800" // 对话框默认大小 (像素)
#define UI_DLG_MINSIZE "800x800" // 对话框最小大小 (像素)
// 列表属性
#define UI_LIST_ITEM_PADDING "5x5" // 列表项内边距 (像素)
#define UI_LIST_BGCOLOR "255 255 255" // 列表背景颜色 (RGB)
#define UI_LIST_MERGED_BGCOLOR "240 240 240"// 合并列表背景颜色 (RGB)
// 列表控件设置
#define UI_LIST_ITEM_PADDING "5x5" // 列表项内边距
#define UI_LIST_BACKCOLOR "255 255 255" // 列表背景颜色
// 按钮设置
#define UI_BTN_RASTERSIZE "100x32" // 按钮默认大小
// 布局间隙和边距
#define UI_VBOX_GAP "5" // 垂直布局项间隙
#define UI_VBOX_MARGIN "0x0" // 垂直布局外边距
#define UI_VBOX_ALL_MARGIN "10x10" // 垂直布局总外边距
#define UI_VBOX_ALL_GAP "5" // 垂直布局总间隙
#define UI_HBOX_GAP "10" // 水平布局项间隙
#define UI_HBOX_MARGIN "10x10" // 水平布局外边距
#define UI_HBOX_ALIGNMENT "ACENTER" // 水平布局对齐方式
#endif // CONFIG_H
+26 -48
View File
@@ -3,35 +3,38 @@
#include <iup.h>
// 定义 Windows 消息常量
#ifndef WM_COPYGLOBALDATA
#define WM_COPYGLOBALDATA 0x0049
#endif
// 消息过滤器常量
#ifndef MSGFLT_ADD
#define MSGFLT_ADD 1
#endif
// 注册表路径常量
#define REG_PATH_SYS L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"
#define REG_PATH_USER L"Environment"
#define REG_VALUE L"Path"
// 全局控件句柄声明
extern Ihandle *dlg; // 主对话框句柄
extern Ihandle *tabs_main; // 标签页容器
extern Ihandle *list_sys; // 系统变量列表
extern Ihandle *list_user; // 用户变量列表
extern Ihandle *list_merged; // 合并变量列表
extern Ihandle *lbl_status; // 状态标签句柄
extern Ihandle *btn_new; // 新增按钮句柄
extern Ihandle *btn_edit; // 编辑按钮句柄
extern Ihandle *btn_browse; // 浏览按钮句柄
extern Ihandle *btn_del; // 删除按钮句柄
extern Ihandle *btn_up; // 移按钮句柄
extern Ihandle *btn_down; // 下移按钮句柄
extern Ihandle *btn_clean; // 一键清理按钮句柄
extern Ihandle *btn_undo; // 撤销按钮句柄
extern Ihandle *btn_redo; // 重做按钮句柄
extern Ihandle *btn_import; // 导入按钮句柄
extern Ihandle *btn_export; // 导出按钮句柄
extern Ihandle *btn_theme; // 主题切换按钮句柄
extern Ihandle *btn_ok; // 确认按钮句柄
extern int is_dark_mode; // 深色模式状态
extern Ihandle *btn_cancel; // 取消按钮句柄
extern Ihandle *btn_help; // 帮助按钮句柄
extern Ihandle *txt_search; // 搜索框
extern Ihandle *dlg; // 主对话框句柄
extern Ihandle *tabs_main; // 标签页容器
extern Ihandle *list_sys; // 系统变量列表
extern Ihandle *list_user; // 用户变量列表
extern Ihandle *lbl_status; // 状态标签句柄
extern Ihandle *btn_new; // 新增按钮句柄
extern Ihandle *btn_edit; // 编辑按钮句柄
extern Ihandle *btn_browse; // 浏览按钮句柄
extern Ihandle *btn_del; // 删除按钮句柄
extern Ihandle *btn_up; // 上移按钮句柄
extern Ihandle *btn_down; // 移按钮句柄
extern Ihandle *btn_clean; // 一键清理按钮句柄
extern Ihandle *btn_ok; // 确认按钮句柄
extern Ihandle *btn_cancel; // 取消按钮句柄
extern Ihandle *btn_help; // 帮助按钮句柄
extern Ihandle *txt_search; // 搜索框
// 简单字符串列表结构,用于搜索缓存
typedef struct {
@@ -43,34 +46,9 @@ typedef struct {
extern StringList raw_sys_paths;
extern StringList raw_user_paths;
// 历史记录节点
typedef struct HistoryNode {
StringList sys_paths;
StringList user_paths;
struct HistoryNode *next;
} HistoryNode;
// 历史记录栈
typedef struct {
HistoryNode *top;
int count;
} HistoryStack;
extern HistoryStack undo_stack;
extern HistoryStack redo_stack;
extern Ihandle *btn_undo;
extern Ihandle *btn_redo;
// 缓存操作函数声明
void init_string_list(StringList *list);
void add_string_list(StringList *list, const char *str);
void clear_string_list(StringList *list);
void copy_string_list(StringList *dest, StringList *src);
// 历史记录操作
void init_history_stack(HistoryStack *stack);
void push_history(HistoryStack *stack, StringList *sys, StringList *user);
int pop_history(HistoryStack *stack, StringList *out_sys, StringList *out_user);
void clear_history_stack(HistoryStack *stack);
#endif // GLOBALS_H
-2
View File
@@ -12,6 +12,4 @@ Ihandle *create_main_buttons();
// 创建底部按钮区域
Ihandle *create_bottom_buttons();
Ihandle *create_main_dialog();
#endif // UI_H
-18
View File
@@ -1,18 +0,0 @@
#ifndef UI_UTILS_H
#define UI_UTILS_H
#include <iup.h>
#include "globals.h"
// 辅助函数声明
int get_first_selected_index(Ihandle *list);
void set_single_selection(Ihandle *list, int index);
void refresh_ui_from_raw(Ihandle *list, StringList *raw);
void record_history();
int custom_input_dialog(const char *title, const char *label_text, char *buffer, int buffer_size);
Ihandle *get_current_list();
void remove_from_raw_data(StringList *list, const char *str);
void toggle_edit_buttons(int enable);
void apply_theme();
#endif // UI_UTILS_H
-3
View File
@@ -14,9 +14,6 @@ wchar_t* utf8_to_wide(const char* str);
// 检查管理员权限
int check_admin();
// 展开环境变量
char* expand_env_vars(const char* path);
// 检查路径是否有效(存在且为目录)
int is_path_valid(const char *path);
View File
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+547
View File
@@ -0,0 +1,547 @@
#include "callbacks.h"
#include "globals.h"
#include "registry.h"
#include "utils.h"
#include <string.h>
#include <stdio.h>
// 简单的自定义输入对话框,支持更宽的输入框
// 返回 1 表示确定,0 表示取消
int show_custom_input_dialog(const char *title, const char *label_text, char *buffer, int buffer_size)
{
Ihandle *text = IupText(NULL);
IupSetAttribute(text, "VALUE", buffer);
IupSetAttribute(text, "EXPAND", "HORIZONTAL");
IupSetAttribute(text, "RASTERSIZE", "600x"); // 设置宽度为600像素
IupSetAttribute(text, "VISIBLECOLUMNS", "80"); // 设置可见列数
// 如果是编辑模式,全选文本
if (strlen(buffer) > 0)
{
IupSetAttribute(text, "SELECTION", "ALL");
}
Ihandle *dlg = IupDialog(
IupVbox(
IupLabel(label_text),
text,
IupHbox(
IupFill(),
IupButton("确定", "1"), // "1" 会被返回
IupButton("取消", "0"), // "0" 会被返回
NULL),
NULL));
// 布局设置
IupSetAttribute(dlg, "TITLE", title);
IupSetAttribute(dlg, "MINBOX", "NO");
IupSetAttribute(dlg, "MAXBOX", "NO");
IupSetAttribute(dlg, "RESIZE", "NO");
IupSetAttribute(dlg, "MARGIN", "10x10");
IupSetAttribute(dlg, "GAP", "10");
// 按钮响应
// 这是一个简单的 hackIupPopup 的参数 x, y 如果是 IUP_CENTER 等,它是一个模态循环。
// 但是标准的 IupPopup 不返回按钮值。
// 我们使用 IupAlarm 类似的逻辑,或者使用 IUP 提供的 IupGetParam。
// 为了最简单实现,我们使用 IUP 的 IupGetParam,但是它很难调整宽度。
// 所以还是手动构建对话框。
// 为了获取返回值,我们需要设置按钮回调。
// 但为了避免定义额外的全局函数,我们可以使用 IupPopup 阻塞特性。
// 我们需要定义两个简单的回调函数。
// 为了简化,这里定义两个静态辅助函数。
// 由于 C 语言闭包限制,我们需要用全局或静态变量传递状态,或者使用 Dialog 的 Attribute。
IupSetAttribute(dlg, "MY_STATUS", "0");
// 注册回调 (使用 IupSetCallback 注册 lambda 类似的逻辑比较难,这里用名字)
// 必须定义全局函数。为了避免污染,我们在文件顶部定义静态函数。
// 见下文的 static int on_dialog_ok...
// 由于不能在函数内部定义函数,我们需要调整代码结构。
// 见下文重构。
return 0; // 占位
}
// 静态辅助函数:对话框确定
static int on_dialog_ok(Ihandle *self)
{
Ihandle *dlg = IupGetDialog(self);
IupSetAttribute(dlg, "MY_STATUS", "1");
return IUP_CLOSE;
}
// 静态辅助函数:对话框取消
static int on_dialog_cancel(Ihandle *self)
{
Ihandle *dlg = IupGetDialog(self);
IupSetAttribute(dlg, "MY_STATUS", "0");
return IUP_CLOSE;
}
// 真正的实现函数
int custom_input_dialog(const char *title, const char *label_text, char *buffer, int buffer_size)
{
Ihandle *text = IupText(NULL);
IupSetAttribute(text, "VALUE", buffer);
IupSetAttribute(text, "EXPAND", "HORIZONTAL");
IupSetAttribute(text, "RASTERSIZE", "500x");
IupSetAttribute(text, "NAME", "INPUT_TEXT");
Ihandle *btn_ok = IupButton("确定", NULL);
IupSetCallback(btn_ok, "ACTION", on_dialog_ok);
IupSetAttribute(btn_ok, "RASTERSIZE", "100x32");
Ihandle *btn_cancel = IupButton("取消", NULL);
IupSetCallback(btn_cancel, "ACTION", on_dialog_cancel);
IupSetAttribute(btn_cancel, "RASTERSIZE", "100x32");
Ihandle *vbox = IupVbox(
IupLabel(label_text),
text,
IupHbox(IupFill(), btn_ok, btn_cancel, NULL),
NULL);
IupSetAttribute(vbox, "MARGIN", "15x15");
IupSetAttribute(vbox, "GAP", "10");
Ihandle *dlg = IupDialog(vbox);
IupSetAttribute(dlg, "TITLE", title);
IupSetAttribute(dlg, "MINBOX", "NO");
IupSetAttribute(dlg, "MAXBOX", "NO");
IupSetAttribute(dlg, "RESIZE", "NO");
IupSetAttributeHandle(dlg, "DEFAULTENTER", btn_ok);
IupSetAttributeHandle(dlg, "DEFAULTESC", btn_cancel);
IupPopup(dlg, IUP_CENTER, IUP_CENTER);
int result = IupGetInt(dlg, "MY_STATUS");
if (result == 1)
{
char *val = IupGetAttribute(text, "VALUE");
if (val)
{
strncpy(buffer, val, buffer_size);
buffer[buffer_size - 1] = '\0';
}
}
IupDestroy(dlg);
return result;
}
// 辅助函数:获取当前选中的列表
Ihandle *get_current_list()
{
// 获取当前选中的 Tab 索引
// 注意:IupTabs 的 VALUE 属性在某些版本可能返回 handle,某些版本返回 pos
// 这里使用 IupGetInt(tabs_main, "VALUEPOS") 更稳妥
int pos = IupGetInt(tabs_main, "VALUEPOS");
if (pos == 0)
return list_sys;
if (pos == 1)
return list_user;
return list_sys; // 默认
}
// 按钮回调:新建
int btn_new_cb(Ihandle *self)
{
char buffer[1024] = "";
if (custom_input_dialog("新建环境变量", "请输入路径:", buffer, sizeof(buffer)))
{
if (strlen(buffer) > 0)
{
Ihandle *current_list = get_current_list();
int count = IupGetInt(current_list, "COUNT");
count++;
IupSetAttributeId(current_list, "", count, buffer);
IupSetInt(current_list, "COUNT", count);
IupSetInt(current_list, "VALUE", count);
refresh_single_list_style(current_list);
}
}
return IUP_DEFAULT;
}
// 按钮回调:编辑
int btn_edit_cb(Ihandle *self)
{
Ihandle *current_list = get_current_list();
int selected = IupGetInt(current_list, "VALUE");
if (selected == 0)
return IUP_DEFAULT;
char *current_val = IupGetAttributeId(current_list, "", selected);
char buffer[4096]; // 假设单个路径不超过4096
if (current_val)
{
strncpy(buffer, current_val, 4096);
buffer[4095] = '\0';
}
else
{
buffer[0] = '\0';
}
if (custom_input_dialog("编辑环境变量", "编辑路径:", buffer, sizeof(buffer)))
{
if (strlen(buffer) > 0)
{
IupSetAttributeId(current_list, "", selected, buffer);
refresh_single_list_style(current_list);
}
}
return IUP_DEFAULT;
}
// 双击回调
int list_dblclick_cb(Ihandle *self, int item, char *text)
{
// 这里的 self 就是触发双击的列表控件
if (item > 0)
{
// 选中该行
IupSetInt(self, "VALUE", item);
// 调用编辑逻辑
btn_edit_cb(NULL);
}
return IUP_DEFAULT;
}
// 按钮回调:浏览
int btn_browse_cb(Ihandle *self)
{
Ihandle *filedlg = IupFileDlg();
IupSetAttribute(filedlg, "DIALOGTYPE", "DIR");
IupSetAttribute(filedlg, "TITLE", "选择目录");
IupPopup(filedlg, IUP_CENTER, IUP_CENTER);
if (IupGetInt(filedlg, "STATUS") != -1)
{
char *value = IupGetAttribute(filedlg, "VALUE");
if (value)
{
Ihandle *current_list = get_current_list();
int count = IupGetInt(current_list, "COUNT");
count++;
IupSetAttributeId(current_list, "", count, value);
IupSetInt(current_list, "COUNT", count);
IupSetInt(current_list, "VALUE", count);
refresh_single_list_style(current_list);
}
}
IupDestroy(filedlg);
return IUP_DEFAULT;
}
// 辅助函数:从 raw_data 中删除指定字符串
static void remove_from_raw_data(StringList *list, const char *str)
{
if (!list || !str)
return;
for (int i = 0; i < list->count; i++)
{
if (strcmp(list->items[i], str) == 0)
{
free(list->items[i]);
// 移动后续元素
for (int j = i; j < list->count - 1; j++)
{
list->items[j] = list->items[j + 1];
}
list->count--;
break; // 假设没有重复,只删除第一个匹配
}
}
}
// 按钮回调:删除
int btn_del_cb(Ihandle *self)
{
Ihandle *current_list = get_current_list();
int selected = IupGetInt(current_list, "VALUE");
if (selected == 0)
{
IupMessage("提示", "请先选择要删除的项");
return IUP_DEFAULT;
}
// 获取当前要删除的内容
char *val = IupGetAttributeId(current_list, "", selected);
// 确认删除
// char msg[1024];
// snprintf(msg, sizeof(msg), "确定要删除以下路径吗?\n\n%s", val ? val : "(空)");
// if (IupAlarm("确认删除", msg, "是", "否", NULL) != 1)
// return IUP_DEFAULT;
// 从 raw_data 缓存中同步删除
int pos = IupGetInt(tabs_main, "VALUEPOS");
StringList *raw_data = (pos == 0) ? &raw_sys_paths : &raw_user_paths;
// 注意:必须先保存 val 的副本,因为 REMOVEITEM 可能会导致 val 指针失效(如果它是指向列表内部缓冲区的)
char *val_copy = val ? _strdup(val) : NULL;
// 先从界面删除
// IupSetAttribute(current_list, "REMOVEITEM", "SELECTED");
// 改为按索引删除,防止失去焦点导致 SELECTED 失效
IupSetInt(current_list, "REMOVEITEM", selected);
// 再从缓存删除
if (val_copy && raw_data)
{
remove_from_raw_data(raw_data, val_copy);
free(val_copy);
}
// 重新刷新
refresh_single_list_style(current_list);
// 更新状态栏,告知用户删除了什么
IupSetAttribute(lbl_status, "TITLE", "状态: 已删除选中项");
return IUP_DEFAULT;
}
// 按钮回调:上移
int btn_up_cb(Ihandle *self)
{
Ihandle *current_list = get_current_list();
int selected = IupGetInt(current_list, "VALUE");
if (selected <= 1)
return IUP_DEFAULT; // 已经是第一个或未选中
char *current = IupGetAttributeId(current_list, "", selected);
char *prev = IupGetAttributeId(current_list, "", selected - 1);
// 交换内容
char buf_curr[4096], buf_prev[4096];
strncpy(buf_curr, current, 4096);
buf_curr[4095] = '\0';
strncpy(buf_prev, prev, 4096);
buf_prev[4095] = '\0';
IupSetAttributeId(current_list, "", selected, buf_prev);
IupSetAttributeId(current_list, "", selected - 1, buf_curr);
IupSetInt(current_list, "VALUE", selected - 1);
// 刷新样式(虽然颜色不需要变,但为了保险)
refresh_single_list_style(current_list);
return IUP_DEFAULT;
}
// 按钮回调:下移
int btn_down_cb(Ihandle *self)
{
Ihandle *current_list = get_current_list();
int selected = IupGetInt(current_list, "VALUE");
int count = IupGetInt(current_list, "COUNT");
if (selected == 0 || selected >= count)
return IUP_DEFAULT;
char *current = IupGetAttributeId(current_list, "", selected);
char *next = IupGetAttributeId(current_list, "", selected + 1);
char buf_curr[4096], buf_next[4096];
strncpy(buf_curr, current, 4096);
buf_curr[4095] = '\0';
strncpy(buf_next, next, 4096);
buf_next[4095] = '\0';
IupSetAttributeId(current_list, "", selected, buf_next);
IupSetAttributeId(current_list, "", selected + 1, buf_curr);
IupSetInt(current_list, "VALUE", selected + 1);
refresh_single_list_style(current_list);
return IUP_DEFAULT;
}
// 按钮回调:一键清理
int btn_clean_cb(Ihandle *self)
{
Ihandle *current_list = get_current_list();
int count = IupGetInt(current_list, "COUNT");
if (count == 0)
return IUP_DEFAULT;
// 弹出确认对话框
if (IupAlarm("确认清理", "此操作将移除当前列表中所有【无效路径】和【重复路径】。\n确定要继续吗?", "确定", "取消", NULL) != 1)
{
return IUP_DEFAULT;
}
// 从后往前遍历删除,避免索引错位
for (int i = count; i >= 1; i--)
{
char *item = IupGetAttributeId(current_list, "", i);
if (!item)
continue;
int should_remove = 0;
// 1. 检查有效性
if (!is_path_valid(item))
{
should_remove = 1;
}
else
{
// 2. 检查重复 (检查当前项之前是否出现过)
// 注意:这里需要再次遍历,性能稍低但最稳妥
for (int j = 1; j < i; j++)
{
char *prev_item = IupGetAttributeId(current_list, "", j);
if (prev_item && _stricmp(item, prev_item) == 0)
{
should_remove = 1;
break;
}
}
}
if (should_remove)
{
IupSetAttributeId(current_list, "REMOVEITEM", i, NULL);
}
}
refresh_single_list_style(current_list);
IupMessage("提示", "清理完成!");
return IUP_DEFAULT;
}
// 搜索回调
int txt_search_cb(Ihandle *self)
{
char *filter = IupGetAttribute(self, "VALUE");
if (!filter)
return IUP_DEFAULT;
// 获取当前选中的 Tab 索引
int pos = IupGetInt(tabs_main, "VALUEPOS");
Ihandle *current_list = (pos == 0) ? list_sys : list_user;
StringList *raw_data = (pos == 0) ? &raw_sys_paths : &raw_user_paths;
// 清空列表
IupSetAttribute(current_list, "REMOVEITEM", "ALL");
// 重新填充
int count = 0;
for (int i = 0; i < raw_data->count; i++)
{
// 如果 filter 为空,或包含 filter (不区分大小写)
if (strlen(filter) == 0 || stristr(raw_data->items[i], filter) != NULL)
{
count++;
IupSetAttributeId(current_list, "", count, raw_data->items[i]);
}
}
IupSetInt(current_list, "COUNT", count);
refresh_single_list_style(current_list);
return IUP_DEFAULT;
}
// 拖拽回调
int list_dropfiles_cb(Ihandle *self, const char *filename, int num, int x, int y)
{
// 获取当前列表和原始数据
// 注意:拖拽的目标列表可能是 list_sys 或 list_user,由 self 参数决定
// 但为了确保数据一致性,我们还是重新获取一下
Ihandle *current_list = self;
StringList *raw_data = NULL;
if (self == list_sys)
raw_data = &raw_sys_paths;
else if (self == list_user)
raw_data = &raw_user_paths;
else
return IUP_DEFAULT; // 异常情况
// 检查拖入的是否为目录
DWORD attr = GetFileAttributesA(filename);
if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
{
// 如果正在搜索,先清空搜索框
IupSetAttribute(txt_search, "VALUE", "");
// 添加到列表末尾
int count = IupGetInt(current_list, "COUNT");
count++;
IupSetAttributeId(current_list, "", count, filename);
IupSetInt(current_list, "COUNT", count);
IupSetInt(current_list, "VALUE", count); // 选中新添加的项
// 同时添加到原始数据缓存,确保搜索时能搜到
if (raw_data)
{
add_string_list(raw_data, filename);
}
refresh_single_list_style(current_list);
}
else
{
// 如果拖入的不是文件夹,可以在状态栏提示
IupSetAttribute(lbl_status, "TITLE", "提示: 只能拖拽文件夹添加到 PATH");
}
return IUP_DEFAULT;
}
// 键盘按键回调
int list_k_any_cb(Ihandle *self, int c)
{
// 处理 Delete 键
if (c == K_DEL)
{
btn_del_cb(NULL);
return IUP_IGNORE; // 阻止默认处理
}
return IUP_DEFAULT;
}
// 按钮回调:确定
int btn_ok_cb(Ihandle *self)
{
save_all_paths();
return IUP_DEFAULT;
}
// 按钮回调:取消
int btn_cancel_cb(Ihandle *self)
{
IupExitLoop();
return IUP_DEFAULT;
}
// 按钮回调:帮助
int btn_help_cb(Ihandle *self)
{
IupMessage("使用说明",
"1. 本程序用于编辑系统环境变量 PATH。\n"
"2. 必须以【管理员身份】运行才能保存更改。\n"
"3. 操作说明:\n"
" - 新建:添加新路径到列表末尾。\n"
" - 编辑:修改选中的路径。\n"
" - 浏览:从文件系统选择目录添加。\n"
" - 删除:移除选中的路径。\n"
" - 上移/下移:调整路径优先级。\n"
"4. 点击【确定】保存更改并生效。\n"
"5. 注意:某些正在运行的程序可能需要重启才能识别新的环境变量。\n\n"
"--------------------------------------------------\n"
"作者:LHY\n"
"邮箱:3364451258@qq.com\n"
"GitHubhttps://github.com/LHY0125/PathEditor\n"
"记得给我的项目点个star");
return IUP_DEFAULT;
}
-368
View File
@@ -1,368 +0,0 @@
#include "cb_edit.h"
#include "ui_utils.h"
#include "globals.h"
#include "utils.h"
#include <string.h>
#include <stdlib.h>
// 按钮回调:新建
int btn_new_cb(Ihandle *self)
{
char buffer[1024] = "";
if (custom_input_dialog("新建环境变量", "请输入路径:", buffer, sizeof(buffer)))
{
if (strlen(buffer) > 0)
{
// 记录历史
record_history();
Ihandle *current_list = get_current_list();
int count = IupGetInt(current_list, "COUNT");
count++;
IupSetAttributeId(current_list, "", count, buffer);
IupSetInt(current_list, "COUNT", count);
// 更新选中状态
set_single_selection(current_list, count);
// 同时添加到 raw_data
int pos = IupGetInt(tabs_main, "VALUEPOS");
StringList *raw_data = (pos == 0) ? &raw_sys_paths : &raw_user_paths;
if (raw_data) {
add_string_list(raw_data, buffer);
}
refresh_single_list_style(current_list);
}
}
return IUP_DEFAULT;
}
// 按钮回调:编辑
int btn_edit_cb(Ihandle *self)
{
Ihandle *current_list = get_current_list();
// 获取第一个选中的项
int selected = get_first_selected_index(current_list);
if (selected == 0)
return IUP_DEFAULT;
char *current_val = IupGetAttributeId(current_list, "", selected);
char buffer[4096]; // 假设单个路径不超过4096
if (current_val)
{
strncpy(buffer, current_val, 4096);
buffer[4095] = '\0';
}
else
{
buffer[0] = '\0';
}
if (custom_input_dialog("编辑环境变量", "编辑路径:", buffer, sizeof(buffer)))
{
if (strlen(buffer) > 0)
{
// 记录历史
record_history();
// 更新 UI
IupSetAttributeId(current_list, "", selected, buffer);
// 更新 raw_data
int pos = IupGetInt(tabs_main, "VALUEPOS");
StringList *raw_data = (pos == 0) ? &raw_sys_paths : &raw_user_paths;
char *filter = IupGetAttribute(txt_search, "VALUE");
if (!filter || strlen(filter) == 0) {
if (raw_data && selected <= raw_data->count) {
free(raw_data->items[selected-1]);
raw_data->items[selected-1] = _strdup(buffer);
}
} else {
// 搜索状态下,忽略同步问题,或者编辑后清除搜索。
}
refresh_single_list_style(current_list);
}
}
return IUP_DEFAULT;
}
// 双击回调
int list_dblclick_cb(Ihandle *self, int item, char *text)
{
// 这里的 self 就是触发双击的列表控件
if (item > 0)
{
// 选中该行 (单选)
set_single_selection(self, item);
// 调用编辑逻辑
btn_edit_cb(NULL);
}
return IUP_DEFAULT;
}
// 按钮回调:删除 (支持多选)
int btn_del_cb(Ihandle *self)
{
Ihandle *current_list = get_current_list();
char *value = IupGetAttribute(current_list, "VALUE");
if (!value)
return IUP_DEFAULT;
int len = strlen(value);
int has_selection = 0;
for (int i = 0; i < len; i++) {
if (value[i] == '+') {
has_selection = 1;
break;
}
}
if (!has_selection)
{
IupMessage("提示", "请先选择要删除的项");
return IUP_DEFAULT;
}
// 记录历史
record_history();
// 获取 raw_data 缓存
int pos = IupGetInt(tabs_main, "VALUEPOS");
StringList *raw_data = (pos == 0) ? &raw_sys_paths : &raw_user_paths;
// 从后往前遍历删除,避免索引错位
for (int i = len - 1; i >= 0; i--)
{
if (value[i] == '+')
{
int item_index = i + 1; // IUP 索引从 1 开始
char *val = IupGetAttributeId(current_list, "", item_index);
// 从缓存删除
if (val && raw_data)
{
char *val_copy = _strdup(val);
remove_from_raw_data(raw_data, val_copy);
free(val_copy);
}
// 从界面删除
IupSetInt(current_list, "REMOVEITEM", item_index);
}
}
// 重新刷新
refresh_single_list_style(current_list);
// 更新状态栏
IupSetAttribute(lbl_status, "TITLE", "状态: 已删除选中项");
return IUP_DEFAULT;
}
// 按钮回调:上移 (支持多选)
int btn_up_cb(Ihandle *self)
{
Ihandle *current_list = get_current_list();
char *value = IupGetAttribute(current_list, "VALUE");
if (!value)
return IUP_DEFAULT;
int len = strlen(value);
char *new_value = _strdup(value);
int moved = 0;
// 预检查是否有移动
for (int i = 1; i < len; i++) {
if (new_value[i] == '+' && new_value[i - 1] == '-') {
moved = 1;
break;
}
}
if (moved) {
// 记录历史
record_history();
// 同步 raw_data (假设非搜索状态,raw_data 与 UI 一致)
int pos = IupGetInt(tabs_main, "VALUEPOS");
StringList *raw_data = (pos == 0) ? &raw_sys_paths : &raw_user_paths;
// 从前往后遍历,如果当前项被选中且前一项未选中,则交换
for (int i = 1; i < len; i++)
{
if (new_value[i] == '+' && new_value[i - 1] == '-')
{
// 交换列表项内容
char *curr_text = IupGetAttributeId(current_list, "", i + 1);
char *prev_text = IupGetAttributeId(current_list, "", i);
// 需要复制,防止指针失效
char *curr_copy = curr_text ? _strdup(curr_text) : NULL;
char *prev_copy = prev_text ? _strdup(prev_text) : NULL;
IupSetAttributeId(current_list, "", i, curr_copy);
IupSetAttributeId(current_list, "", i + 1, prev_copy);
if (curr_copy) free(curr_copy);
if (prev_copy) free(prev_copy);
// 交换 raw_data
if (raw_data && i < raw_data->count) {
char *temp = raw_data->items[i];
raw_data->items[i] = raw_data->items[i-1];
raw_data->items[i-1] = temp;
}
// 交换选中状态
new_value[i] = '-';
new_value[i - 1] = '+';
}
}
// 更新选中状态
IupSetAttribute(current_list, "VALUE", new_value);
refresh_single_list_style(current_list);
}
free(new_value);
return IUP_DEFAULT;
}
// 按钮回调:下移 (支持多选)
int btn_down_cb(Ihandle *self)
{
Ihandle *current_list = get_current_list();
char *value = IupGetAttribute(current_list, "VALUE");
if (!value)
return IUP_DEFAULT;
int len = strlen(value);
char *new_value = _strdup(value);
int moved = 0;
// 预检查
for (int i = len - 2; i >= 0; i--) {
if (new_value[i] == '+' && new_value[i + 1] == '-') {
moved = 1;
break;
}
}
if (moved) {
// 记录历史
record_history();
// 同步 raw_data
int pos = IupGetInt(tabs_main, "VALUEPOS");
StringList *raw_data = (pos == 0) ? &raw_sys_paths : &raw_user_paths;
// 从后往前遍历,如果当前项被选中且后一项未选中,则交换
for (int i = len - 2; i >= 0; i--)
{
if (new_value[i] == '+' && new_value[i + 1] == '-')
{
// 交换列表项内容
char *curr_text = IupGetAttributeId(current_list, "", i + 1);
char *next_text = IupGetAttributeId(current_list, "", i + 2);
// 需要复制
char *curr_copy = curr_text ? _strdup(curr_text) : NULL;
char *next_copy = next_text ? _strdup(next_text) : NULL;
IupSetAttributeId(current_list, "", i + 2, curr_copy);
IupSetAttributeId(current_list, "", i + 1, next_copy);
if (curr_copy) free(curr_copy);
if (next_copy) free(next_copy);
// 交换 raw_data
if (raw_data && i + 1 < raw_data->count) {
char *temp = raw_data->items[i];
raw_data->items[i] = raw_data->items[i+1];
raw_data->items[i+1] = temp;
}
// 交换选中状态
new_value[i] = '-';
new_value[i + 1] = '+';
}
}
// 更新选中状态
IupSetAttribute(current_list, "VALUE", new_value);
refresh_single_list_style(current_list);
}
free(new_value);
return IUP_DEFAULT;
}
// 按钮回调:一键清理
int btn_clean_cb(Ihandle *self)
{
Ihandle *current_list = get_current_list();
int count = IupGetInt(current_list, "COUNT");
if (count == 0)
return IUP_DEFAULT;
// 弹出确认对话框
if (IupAlarm("确认清理", "此操作将移除当前列表中所有【无效路径】和【重复路径】。\n确定要继续吗?", "确定", "取消", NULL) != 1)
{
return IUP_DEFAULT;
}
// 记录历史 (放在循环外,一次操作)
record_history();
// 获取 raw_data 用于同步删除
int pos = IupGetInt(tabs_main, "VALUEPOS");
StringList *raw_data = (pos == 0) ? &raw_sys_paths : &raw_user_paths;
// 从后往前遍历删除,避免索引错位
for (int i = count; i >= 1; i--)
{
char *item = IupGetAttributeId(current_list, "", i);
if (!item)
continue;
int should_remove = 0;
// 1. 检查有效性
if (!is_path_valid(item))
{
should_remove = 1;
}
else
{
// 2. 检查重复 (检查当前项之前是否出现过)
// 注意:这里需要再次遍历,性能稍低但最稳妥
for (int j = 1; j < i; j++)
{
char *prev_item = IupGetAttributeId(current_list, "", j);
if (prev_item && _stricmp(item, prev_item) == 0)
{
should_remove = 1;
break;
}
}
}
if (should_remove)
{
// 从 raw_data 删除
if (raw_data) {
char *val_copy = _strdup(item);
remove_from_raw_data(raw_data, val_copy);
free(val_copy);
}
IupSetAttributeId(current_list, "REMOVEITEM", i, NULL);
}
}
refresh_single_list_style(current_list);
IupMessage("提示", "清理完成!");
return IUP_DEFAULT;
}
-260
View File
@@ -1,260 +0,0 @@
#include "cb_file.h"
#include "ui_utils.h"
#include "globals.h"
#include "utils.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h> // for GetFileAttributesA
// 按钮回调:浏览
int btn_browse_cb(Ihandle *self)
{
Ihandle *filedlg = IupFileDlg();
IupSetAttribute(filedlg, "DIALOGTYPE", "DIR");
IupSetAttribute(filedlg, "TITLE", "选择目录");
IupPopup(filedlg, IUP_CENTER, IUP_CENTER);
if (IupGetInt(filedlg, "STATUS") != -1)
{
char *value = IupGetAttribute(filedlg, "VALUE");
if (value)
{
// 记录历史
record_history();
Ihandle *current_list = get_current_list();
int count = IupGetInt(current_list, "COUNT");
count++;
IupSetAttributeId(current_list, "", count, value);
IupSetInt(current_list, "COUNT", count);
// 更新选中状态
set_single_selection(current_list, count);
// 同步 raw_data
int pos = IupGetInt(tabs_main, "VALUEPOS");
StringList *raw_data = (pos == 0) ? &raw_sys_paths : &raw_user_paths;
if (raw_data) {
add_string_list(raw_data, value);
}
refresh_single_list_style(current_list);
}
}
IupDestroy(filedlg);
return IUP_DEFAULT;
}
// 撤销回调
int btn_undo_cb(Ihandle *self)
{
StringList sys = {0}, user = {0};
if (pop_history(&undo_stack, &sys, &user)) {
// Push current state to redo
push_history(&redo_stack, &raw_sys_paths, &raw_user_paths);
// Restore
clear_string_list(&raw_sys_paths);
clear_string_list(&raw_user_paths);
raw_sys_paths = sys;
raw_user_paths = user;
// Refresh UI
refresh_ui_from_raw(list_sys, &raw_sys_paths);
refresh_ui_from_raw(list_user, &raw_user_paths);
IupSetAttribute(lbl_status, "TITLE", "状态: 已撤销");
} else {
IupSetAttribute(lbl_status, "TITLE", "状态: 没有可撤销的操作");
}
return IUP_DEFAULT;
}
// 重做回调
int btn_redo_cb(Ihandle *self)
{
StringList sys = {0}, user = {0};
if (pop_history(&redo_stack, &sys, &user)) {
// Push current state to undo
push_history(&undo_stack, &raw_sys_paths, &raw_user_paths);
// Restore
clear_string_list(&raw_sys_paths);
clear_string_list(&raw_user_paths);
raw_sys_paths = sys;
raw_user_paths = user;
// Refresh UI
refresh_ui_from_raw(list_sys, &raw_sys_paths);
refresh_ui_from_raw(list_user, &raw_user_paths);
IupSetAttribute(lbl_status, "TITLE", "状态: 已重做");
} else {
IupSetAttribute(lbl_status, "TITLE", "状态: 没有可重做的操作");
}
return IUP_DEFAULT;
}
// 导出配置
int btn_export_cb(Ihandle *self)
{
Ihandle *current_list = get_current_list();
int count = IupGetInt(current_list, "COUNT");
if (count == 0) {
IupMessage("提示", "当前列表为空,无法导出");
return IUP_DEFAULT;
}
Ihandle *filedlg = IupFileDlg();
IupSetAttribute(filedlg, "DIALOGTYPE", "SAVE");
IupSetAttribute(filedlg, "TITLE", "导出配置");
IupSetAttribute(filedlg, "FILTER", "*.txt");
IupSetAttribute(filedlg, "FILTERINFO", "Text Files (*.txt)");
IupPopup(filedlg, IUP_CENTER, IUP_CENTER);
if (IupGetInt(filedlg, "STATUS") != -1) {
char *filename = IupGetAttribute(filedlg, "VALUE");
if (filename) {
char final_path[1024];
strncpy(final_path, filename, sizeof(final_path));
final_path[sizeof(final_path)-1] = '\0';
// 检查是否以 .txt 结尾 (不区分大小写)
size_t len = strlen(final_path);
if (len < 4 || _stricmp(final_path + len - 4, ".txt") != 0) {
if (len + 4 < sizeof(final_path)) {
strcat(final_path, ".txt");
}
}
FILE *fp = fopen(final_path, "w");
if (fp) {
for (int i = 1; i <= count; i++) {
char *item = IupGetAttributeId(current_list, "", i);
if (item) fprintf(fp, "%s\n", item);
}
fclose(fp);
IupMessage("提示", "导出成功!");
} else {
IupMessage("错误", "无法打开文件进行写入");
}
}
}
IupDestroy(filedlg);
return IUP_DEFAULT;
}
// 导入配置
int btn_import_cb(Ihandle *self)
{
Ihandle *filedlg = IupFileDlg();
IupSetAttribute(filedlg, "DIALOGTYPE", "OPEN");
IupSetAttribute(filedlg, "TITLE", "导入配置");
IupSetAttribute(filedlg, "FILTER", "*.txt");
IupSetAttribute(filedlg, "FILTERINFO", "Text Files (*.txt)");
IupPopup(filedlg, IUP_CENTER, IUP_CENTER);
if (IupGetInt(filedlg, "STATUS") != -1) {
char *filename = IupGetAttribute(filedlg, "VALUE");
if (filename) {
FILE *fp = fopen(filename, "r");
if (fp) {
// Record history
record_history();
Ihandle *current_list = get_current_list();
int pos = IupGetInt(tabs_main, "VALUEPOS");
StringList *raw_data = (pos == 0) ? &raw_sys_paths : &raw_user_paths;
char line[4096];
int imported_count = 0;
while (fgets(line, sizeof(line), fp)) {
// Trim newline
size_t len = strlen(line);
while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r')) {
line[len-1] = '\0';
len--;
}
if (len > 0) {
// Add to UI
int count = IupGetInt(current_list, "COUNT");
count++;
IupSetAttributeId(current_list, "", count, line);
IupSetInt(current_list, "COUNT", count);
// Add to raw_data
if (raw_data) add_string_list(raw_data, line);
imported_count++;
}
}
fclose(fp);
refresh_single_list_style(current_list);
char msg[64];
snprintf(msg, sizeof(msg), "导入成功!共导入 %d 条路径。", imported_count);
IupMessage("提示", msg);
} else {
IupMessage("错误", "无法打开文件进行读取");
}
}
}
IupDestroy(filedlg);
return IUP_DEFAULT;
}
// 拖拽回调
int list_dropfiles_cb(Ihandle *self, const char *filename, int num, int x, int y)
{
// 获取当前列表和原始数据
// 注意:拖拽的目标列表可能是 list_sys 或 list_user,由 self 参数决定
// 但为了确保数据一致性,我们还是重新获取一下
Ihandle *current_list = self;
StringList *raw_data = NULL;
if (self == list_sys)
raw_data = &raw_sys_paths;
else if (self == list_user)
raw_data = &raw_user_paths;
else
return IUP_DEFAULT; // 异常情况
// 检查拖入的是否为目录
DWORD attr = GetFileAttributesA(filename);
if (attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY))
{
// 记录历史
record_history();
// 如果正在搜索,先清空搜索框
IupSetAttribute(txt_search, "VALUE", "");
// 添加到列表末尾
int count = IupGetInt(current_list, "COUNT");
count++;
IupSetAttributeId(current_list, "", count, filename);
IupSetInt(current_list, "COUNT", count);
// 更新选中状态
set_single_selection(current_list, count);
// 同时添加到原始数据缓存,确保搜索时能搜到
if (raw_data)
{
add_string_list(raw_data, filename);
}
refresh_single_list_style(current_list);
}
else
{
// 如果拖入的不是文件夹,可以在状态栏提示
IupSetAttribute(lbl_status, "TITLE", "提示: 只能拖拽文件夹添加到 PATH");
}
return IUP_DEFAULT;
}
-214
View File
@@ -1,214 +0,0 @@
#include "cb_main.h"
#include "ui_utils.h"
#include "globals.h"
#include "registry.h"
#include "utils.h"
#include "cb_edit.h"
#include "cb_file.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
// 搜索回调
int txt_search_cb(Ihandle *self)
{
char *filter = IupGetAttribute(self, "VALUE");
if (!filter)
return IUP_DEFAULT;
// 获取当前选中的 Tab 索引
int pos = IupGetInt(tabs_main, "VALUEPOS");
Ihandle *current_list = (pos == 0) ? list_sys : list_user;
StringList *raw_data = (pos == 0) ? &raw_sys_paths : &raw_user_paths;
// 清空列表
IupSetAttribute(current_list, "REMOVEITEM", "ALL");
// 重新填充
int count = 0;
for (int i = 0; i < raw_data->count; i++)
{
// 如果 filter 为空,或包含 filter (不区分大小写)
if (strlen(filter) == 0 || stristr(raw_data->items[i], filter) != NULL)
{
count++;
IupSetAttributeId(current_list, "", count, raw_data->items[i]);
}
}
IupSetInt(current_list, "COUNT", count);
refresh_single_list_style(current_list);
return IUP_DEFAULT;
}
// 键盘按键回调
int list_k_any_cb(Ihandle *self, int c)
{
// 处理 Delete 键
if (c == K_DEL)
{
btn_del_cb(NULL);
return IUP_IGNORE; // 阻止默认处理
}
return IUP_DEFAULT;
}
// 鼠标移动回调
int list_motion_cb(Ihandle *self, int x, int y, char *status)
{
int pos = IupConvertXYToPos(self, x, y);
if (pos > 0)
{
char *item = IupGetAttributeId(self, "", pos);
if (item)
{
char *expanded = expand_env_vars(item);
if (expanded)
{
IupSetAttribute(self, "TIP", expanded);
free(expanded);
}
else
{
IupSetAttribute(self, "TIP", item);
}
}
else
{
IupSetAttribute(self, "TIP", NULL);
}
}
else
{
IupSetAttribute(self, "TIP", NULL);
}
return IUP_DEFAULT;
}
// 对话框全局按键回调
int dialog_k_any_cb(Ihandle *self, int c)
{
switch (c)
{
case K_cN: // Ctrl+N 新建
btn_new_cb(NULL);
return IUP_IGNORE;
case K_cS: // Ctrl+S 保存
btn_ok_cb(NULL);
return IUP_IGNORE;
case K_cF: // Ctrl+F 搜索
if (txt_search)
{
IupSetFocus(txt_search);
}
return IUP_IGNORE;
case K_cZ: // Ctrl+Z 撤销
btn_undo_cb(NULL);
return IUP_IGNORE;
case K_cY: // Ctrl+Y 重做
btn_redo_cb(NULL);
return IUP_IGNORE;
}
return IUP_DEFAULT;
}
// 按钮回调:确定
int btn_ok_cb(Ihandle *self)
{
save_all_paths();
return IUP_DEFAULT;
}
// 按钮回调:取消
int btn_cancel_cb(Ihandle *self)
{
IupExitLoop();
return IUP_DEFAULT;
}
// 按钮回调:帮助
int btn_help_cb(Ihandle *self)
{
IupMessage("使用说明",
"1. 本程序用于编辑系统环境变量 PATH。\n"
"2. 必须以【管理员身份】运行才能保存更改。\n"
"3. 操作说明:\n"
" - 新建:添加新路径到列表末尾。\n"
" - 编辑:修改选中的路径。\n"
" - 浏览:从文件系统选择目录添加。\n"
" - 删除:移除选中的路径。\n"
" - 上移/下移:调整路径优先级。\n"
" - 导入/导出:备份和恢复配置。\n"
" - 快捷键:\n"
" Ctrl+N: 新建路径\n"
" Ctrl+S: 保存更改\n"
" Ctrl+F: 聚焦搜索框\n"
" Ctrl+Z: 撤销\n"
" Ctrl+Y: 重做\n"
"4. 点击【确定】保存更改并生效。\n"
"5. 注意:某些正在运行的程序可能需要重启才能识别新的环境变量。\n\n"
"--------------------------------------------------\n"
"作者:LHY\n"
"邮箱:3364451258@qq.com\n"
"GitHubhttps://github.com/LHY0125/PathEditor\n"
"记得给我的项目点个star");
return IUP_DEFAULT;
}
// 标签页切换回调
int tabs_tabchange_cb(Ihandle *self, int new_pos, int old_pos)
{
if (new_pos == 2)
{
// 合并预览模式
IupSetAttribute(list_merged, "REMOVEITEM", "ALL");
int count = 0;
// 添加系统变量
for (int i = 0; i < raw_sys_paths.count; i++)
{
count++;
IupSetAttributeId(list_merged, "", count, raw_sys_paths.items[i]);
}
// 添加用户变量
for (int i = 0; i < raw_user_paths.count; i++)
{
count++;
IupSetAttributeId(list_merged, "", count, raw_user_paths.items[i]);
}
IupSetInt(list_merged, "COUNT", count);
refresh_single_list_style(list_merged);
// 禁用编辑按钮
toggle_edit_buttons(0);
}
else
{
// 编辑模式 (检查管理员权限)
if (check_admin())
{
toggle_edit_buttons(1);
}
else
{
toggle_edit_buttons(0);
}
}
return IUP_DEFAULT;
}
// 主题切换回调
int btn_theme_cb(Ihandle *self)
{
is_dark_mode = !is_dark_mode;
if (is_dark_mode)
IupSetAttribute(btn_theme, "TITLE", "浅色模式");
else
IupSetAttribute(btn_theme, "TITLE", "深色模式");
apply_theme();
return IUP_DEFAULT;
}

Some files were not shown because too many files have changed in this diff Show More