12 Commits

Author SHA1 Message Date
Serendipity 2ec22eaae0 chore: 移除未使用的CD库文件并优化构建配置
删除libs/CD目录下的所有二进制库文件(.dll和.a)及对应的头文件,这些文件在项目中并未实际使用。
同时更新CMakeLists.txt,仅复制必需的IUP DLL文件到构建输出目录,减少不必要的文件拷贝。
2026-03-19 20:07:39 +08:00
Serendipity c92d20b4b3 refactor: 将IUP控件从IupMatrix迁移到IupFlatList
简化代码结构并移除对iupcontrols库的依赖。主要变更包括:
- 用IupFlatList替换IupMatrix,简化列表操作API
- 移除iupcontrols.dll和iupimglib.dll依赖,减少部署文件大小
- 更新CMake配置和安装脚本以匹配新的依赖关系
- 重写列表操作函数以使用IupFlatList的API(VALUE、COUNT等属性)
- 调整回调函数签名以匹配新控件的事件参数
- 简化UI创建代码,移除不再需要的图标和属性设置
2026-03-19 20:03:29 +08:00
Serendipity 6a22202ce5 build: 重构IUP库依赖和项目结构
- 删除旧版iup-3.31_Win64_dllw6_lib目录及其所有二进制文件
- 将IUP和CD库重新组织为独立目录结构,包含头文件和静态库
- 更新CMakeLists.txt以链接新的静态库(iupcontrols、iupimglib、cd等)
- 修改main.c以显式初始化IupControls和IupImageLib扩展库
- 更新回调函数签名以匹配IUP Matrix控件的实际参数
- 调整UI工具函数以使用Matrix控件的属性(MARKED、NUMLIN等)
- 更新安装脚本仅复制实际依赖的DLL文件
- 添加所有缺失的IUP和CD头文件以支持完整功能
2026-03-19 19:51:51 +08:00
Serendipity db01c1ad53 chore: 移除构建安装程序前的DLL复制步骤
不再需要手动复制IUP DLL文件到bin目录,因为安装程序脚本已直接引用libs目录中的文件。
2026-03-19 13:22:19 +08:00
Serendipity 1681473357 build: 修复IUP DLL复制命令的路径和条件逻辑
将CMAKE_SOURCE_DIR改为CMAKE_CURRENT_SOURCE_DIR以获取正确的相对路径。
移除不必要的条件判断,确保DLL复制命令始终执行。
使用引号包裹目标目录变量,避免路径包含空格时出现问题。
2026-03-19 12:43:09 +08:00
Serendipity 50f9f95ad5 build: 禁用C编译器扩展以强制使用标准C17
在CMakeLists.txt中添加CMAKE_C_EXTENSIONS OFF设置,确保编译器使用纯C17标准而非特定编译器扩展(如gnu17),提高代码的可移植性和标准符合性。
2026-03-19 12:33:52 +08:00
Serendipity 6ccdc696d2 build: 将C语言标准从C99升级至C17
更新CMakeLists.txt中的CMAKE_C_STANDARD变量,以使用更新的C17语言标准。这能启用新的语言特性并确保更好的兼容性。
2026-03-19 12:22:31 +08:00
Serendipity 1219a53391 build: 迁移构建系统至 CMake 并清理旧的二进制文件
- 删除过时的 Makefile 和 bin/ 目录下的二进制文件
- 新增 CMakeLists.txt 以支持更标准的跨平台构建
- 更新 README.md 中的编译步骤和依赖说明
- 调整 main.c 中的注释和 installer.iss 中的源文件路径
- 构建输出现在位于 build/ 目录,DLL 文件在构建后自动复制
2026-03-19 12:18:51 +08:00
Serendipity 3302132644 docs: 更新注释与README,补充编译说明和架构描述
- 在 main.c 中添加编译和打包命令的注释
- 在 README.md 中新增“架构与二次开发”章节,说明项目的模块化设计和配置管理
2026-03-18 22:23:54 +08:00
Serendipity 59db3dc33b refactor(ui): 提取UI常量到配置文件并重构全局变量
- 新增 config.h 集中管理所有UI常量,如窗口标题、尺寸、颜色等
- 将全局变量和控件指针移至 globals.c 进行统一管理
- 更新 Makefile 以包含新增的源文件
- 修改 ui.c 使用配置常量替代硬编码值,提高可维护性
2026-03-18 22:21:36 +08:00
Serendipity 197b318c61 docs: 更新 README 以反映新增功能与改进
更新 README.md 文件,详细说明新增的深色模式、多选操作、撤销/重做、导入导出、变量展开预览、合并预览标签页以及全局快捷键等功能。同时完善了使用步骤,使其与当前版本的功能特性保持一致。
2026-03-17 21:35:20 +08:00
Serendipity b35fac5358 refactor: 重构项目结构,拆分回调函数并新增功能模块
- 将 callbacks.c 拆分为 cb_edit.c、cb_file.c、cb_main.c 三个模块,提高代码可维护性
- 新增 ui_utils.c 提供通用 UI 辅助函数
- 新增历史记录功能(撤销/重做)和主题切换支持
- 增加合并预览标签页,优化路径有效性检查和环境变量展开
- 更新 Makefile 以支持新的源文件结构
2026-03-17 21:18:21 +08:00
105 changed files with 1608 additions and 803 deletions
+75
View File
@@ -0,0 +1,75 @@
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/ui.c
src/globals.c
src/ui_utils.c
src/cb_edit.c
src/cb_file.c
src/cb_main.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..."
)
-57
View File
@@ -1,57 +0,0 @@
CC = gcc
WINDRES = windres
# Paths - specific to user environment
IUP_DIR = libs/iup-3.31_Win64_dllw6_lib
INCLUDE_DIR = $(IUP_DIR)/include
LIB_DIR = $(IUP_DIR)
LOCAL_INCLUDE_DIR = include
# Output Directories
OBJ_DIR = obj
BIN_DIR = bin
# Flags
# -mwindows: Create GUI app (no console)
# -DUNICODE -D_UNICODE: Use Wide Character API
CFLAGS = -Wall -O2 -I$(INCLUDE_DIR) -I$(LOCAL_INCLUDE_DIR) -D_WIN32 -DUNICODE -D_UNICODE -fexec-charset=UTF-8
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/callbacks.c src/ui.c
RES = ico/resources.rc
OBJ = $(OBJ_DIR)/main.o $(OBJ_DIR)/utils.o $(OBJ_DIR)/registry.o $(OBJ_DIR)/callbacks.o $(OBJ_DIR)/ui.o $(OBJ_DIR)/resources.o
EXE = $(BIN_DIR)/PathEditor.exe
all: $(BIN_DIR) $(OBJ_DIR) $(EXE)
$(BIN_DIR):
if not exist $(BIN_DIR) mkdir $(BIN_DIR)
$(OBJ_DIR):
if not exist $(OBJ_DIR) mkdir $(OBJ_DIR)
$(EXE): $(OBJ)
$(CC) -o $@ $^ $(LDFLAGS)
$(OBJ_DIR)/main.o: src/main.c
$(CC) $(CFLAGS) -c -o $@ $<
$(OBJ_DIR)/utils.o: src/utils.c
$(CC) $(CFLAGS) -c -o $@ $<
$(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)/resources.o: ico/resources.rc
$(WINDRES) -i $< -o $@
clean:
if exist $(OBJ_DIR)\*.o del /Q $(OBJ_DIR)\*.o
if exist $(BIN_DIR)\*.exe del /Q $(BIN_DIR)\*.exe
+39 -15
View File
@@ -10,19 +10,25 @@
* **只读模式**:非管理员运行时自动切换到只读模式,防止误操作。
* **权限检测**:智能检测当前运行权限。
* **📑 双视图管理**
* 完美支持 **System (系统变量)****User (用户变量)** 的分离查看与编辑。
* 清晰的 Tab 标签页切换
* **📑 双视图与预览**
* **分离管理**完美支持 **System (系统变量)****User (用户变量)** 的分离查看与编辑。
* **合并预览**:新增标签页,展示最终解析后的完整 PATH 顺序(系统在前,用户在后),方便排查冲突
* **变量展开**:鼠标悬停在包含变量(如 `%JAVA_HOME%`)的路径上时,自动显示解析后的绝对路径。
* **🔴 智能诊断与维护**
* **无效路径高亮**:自动检测路径是否存在,不存在的显示为红色。
* **重复路径高亮**:自动检测重复项,重复的显示为橙色。
* **一键清理**:智能移除所有无效和重复的路径,保持环境整洁。
* **📂 高交互**
* **📂 高交互**
* **多选支持**:支持使用 `Ctrl``Shift` 进行多选,批量删除或移动路径。
* **撤销/重做**:支持 `Ctrl+Z` 撤销和 `Ctrl+Y` 重做,防止误操作,并提供直观的工具栏按钮。
* **拖拽支持**:直接将文件夹拖入窗口即可添加(支持管理员模式下的 UIPI 穿透)。
* **实时搜索**:顶部搜索框支持不区分大小写的实时过滤查找。
* **快捷键**:支持 Delete 键快速删除选中项
* **实时搜索**:顶部搜索框支持不区分大小写的实时过滤查找(支持快捷键 `Ctrl+F`
* **全局快捷键**:支持 `Ctrl+N` 新建、`Ctrl+S` 保存、`Delete` 删除等快捷操作
* **🎨 个性化**
* **深色模式**:一键切换深色主题,保护视力。
* **便捷管理**
* **新建**:添加新路径到列表。
@@ -30,9 +36,17 @@
* ✏️ **编辑**:双击或点击按钮修改现有路径。
* 🗑️ **删除**:移除不需要的路径。
* ⬆️⬇️ **排序**:上移/下移调整路径优先级。
* 📥/📤 **导入导出**:支持将环境变量导出为 `.txt` 文本文件备份,或从文件导入恢复。
* **轻量级**:原生 C 语言编写,无臃肿依赖,运行速度极快。
## 🏗️ 架构与二次开发
本项目注重代码的模块化和可维护性,非常适合作为 C 语言桌面程序开发的参考:
* **统一配置中心**:所有的 UI 尺寸、间距、颜色等常量配置均提取在 `include/config.h` 中,只需修改宏定义即可轻松定制属于你的专属界面风格。
* **清晰的全局状态**:全局变量和常量被独立分离在 `src/globals.c` / `include/globals.h` 中管理,使得核心业务逻辑更加整洁。
## 📦 下载与安装
您可以从 [Releases](https://github.com/LHY0125/PathEditor/releases) 页面下载最新的安装包 (`PathEditorSetup.exe`)。
@@ -47,12 +61,14 @@
* Windows 操作系统
* GCC 编译器 (推荐 MinGW-w64)
* Make 工具
* CMake 工具
* IUP 库 (已包含在 `libs` 目录下)
* Inno Setup 6 (仅打包需要)
### 编译步骤
本项目使用 CMake 构建系统,支持生成更标准的构建文件并集成到各大 IDE。
1. 克隆仓库:
```bash
@@ -60,14 +76,18 @@
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`。
### 打包 (可选)
@@ -83,12 +103,16 @@
2. **查看**:程序启动后会自动加载当前的系统 PATH 变量。
* **红色**条目表示路径不存在。
* **橙色**条目表示路径重复。
3. **搜索**:在顶部输入关键词可快速筛选
* **变量预览**:鼠标悬停在带 `%` 的变量上可查看实际路径
3. **搜索**:在顶部输入关键词或按 `Ctrl+F` 可快速筛选。
4. **修改**
* 拖拽文件夹列表可直接添加。
* 使用右侧按钮栏进行常规操作
* 点击“一键清理”可自动删除无效和重复项
5. **保存**:操作完成后,务必点击底部的【确定】按钮保存更改
* **拖拽**:将文件夹拖入列表可直接添加。
* **多选**:按住 `Ctrl` 或 `Shift` 可选中多项进行批量删除
* **撤销/重做**:误操作时可使用 `Ctrl+Z` / `Ctrl+Y` 或工具栏按钮回退
* **常规操作**:使用右侧按钮栏进行新建、编辑、移动等操作
* **清理**:点击“一键清理”可自动删除无效和重复项。
* **导入/导出**:使用导入导出功能备份或恢复配置。
5. **保存**:操作完成后,务必点击底部的【确定】按钮(或按 `Ctrl+S`)保存更改。
6. **生效**:保存后,某些正在运行的程序可能需要重启才能识别新的环境变量。CMD 或 PowerShell 窗口需要重新打开。
## 👤 作者信息
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
@@ -1,30 +0,0 @@
#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
@@ -0,0 +1,15 @@
#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
@@ -0,0 +1,14 @@
#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
@@ -0,0 +1,17 @@
#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
+23
View File
@@ -0,0 +1,23 @@
#ifndef CONFIG_H
#define CONFIG_H
// UI 常量定义
#define UI_WINDOW_TITLE "Path Editor" // 窗口标题
#define UI_WINDOW_SIZE "800x800" // 窗口默认大小 (像素)
// 按钮尺寸
#define UI_BUTTON_SIZE "100x32" // 按钮默认大小 (像素)
#define UI_BUTTON_SMALL_SIZE "80x32" // 小按钮大小 (像素)
// 布局间距
#define UI_MARGIN_MAIN "10x10" // 主布局外边距 (像素)
#define UI_GAP_MAIN "10" // 主布局间距 (像素)
#define UI_GAP_BUTTONS "5" // 按钮间距 (像素)
#define UI_GAP_BOTTOM "10" // 底部间距 (像素)
// 列表属性
#define UI_LIST_ITEM_PADDING "5x5" // 列表项内边距 (像素)
#define UI_LIST_BGCOLOR "255 255 255" // 列表背景颜色 (RGB)
#define UI_LIST_MERGED_BGCOLOR "240 240 240"// 合并列表背景颜色 (RGB)
#endif // CONFIG_H
+32
View File
@@ -13,6 +13,7 @@ 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; // 编辑按钮句柄
@@ -21,7 +22,13 @@ 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; // 搜索框
@@ -36,9 +43,34 @@ 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,4 +12,6 @@ Ihandle *create_main_buttons();
// 创建底部按钮区域
Ihandle *create_bottom_buttons();
Ihandle *create_main_dialog();
#endif // UI_H
+18
View File
@@ -0,0 +1,18 @@
#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,6 +14,9 @@ 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
@@ -1,547 +0,0 @@
#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
@@ -0,0 +1,368 @@
#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
@@ -0,0 +1,260 @@
#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
@@ -0,0 +1,214 @@
#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;
}
+24
View File
@@ -0,0 +1,24 @@
#include <stdlib.h>
#include "globals.h"
// 全局控件定义
Ihandle *dlg = NULL; // 主对话框
Ihandle *tabs_main = NULL; // 主选项卡
Ihandle *list_sys = NULL, *list_user = NULL, *list_merged = NULL; // 列表控件
Ihandle *lbl_status = NULL; // 状态栏
Ihandle *btn_new = NULL, *btn_edit = NULL, *btn_browse = NULL, *btn_del = NULL, *btn_up = NULL, *btn_down = NULL; // 右侧按钮
Ihandle *btn_undo = NULL, *btn_redo = NULL; // 撤销重做按钮
Ihandle *btn_import = NULL, *btn_export = NULL; // 导入导出按钮
Ihandle *btn_ok = NULL, *btn_cancel = NULL, *btn_help = NULL; // 确认取消帮助按钮
Ihandle *btn_clean = NULL; // 一键清理按钮
Ihandle *btn_theme = NULL; // 主题切换按钮
Ihandle *txt_search = NULL; // 搜索框
// 历史记录栈
HistoryStack undo_stack = {0};
HistoryStack redo_stack = {0};
// 全局变量定义
StringList raw_sys_paths = {0};
StringList raw_user_paths = {0};
int is_dark_mode = 0; // 默认浅色模式

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