添加了1000多行注释

main^2
shou_gan_mian 1 month ago
parent 338358229a
commit 9d89ccd93a

@ -1,3 +1,6 @@
{
"Codegeex.RepoIndex": true
"Codegeex.RepoIndex": true,
"files.associations": {
"dialog.h": "c"
}
}

@ -1,301 +1,286 @@
/*
* inputbox.c -- implements the input box
* inputbox.c --
*
* ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
* MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
* Savio Lam (lam836@cs.cuhk.hk)
* Linux William Roadcap (roadcap@cfw.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* GNU / 2
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* GNU Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "dialog.h"
#include "dialog.h"
// 包含对话框相关的头文件
char dialog_input_result[MAX_LEN + 1];
char dialog_input_result[MAX_LEN + 1];
// 定义一个字符数组用于存储输入框的结果
/*
* Print the termination buttons
*
*/
static void print_buttons(WINDOW * dialog, int height, int width, int selected)
{
int x = width / 2 - 11;
int y = height - 2;
int x = width / 2 - 11;
// 计算按钮的起始 x 坐标
int y = height - 2;
// 计算按钮的起始 y 坐标
print_button(dialog, gettext(" Ok "), y, x, selected == 0);
print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
print_button(dialog, gettext(" Ok "), y, x, selected == 0);
// 打印 "Ok" 按钮
print_button(dialog, gettext(" Help "), y, x + 14, selected == 1);
// 打印 "Help" 按钮
wmove(dialog, y, x + 1 + 14 * selected);
wrefresh(dialog);
wmove(dialog, y, x + 1 + 14 * selected);
// 移动光标到选中的按钮位置
wrefresh(dialog);
// 刷新对话框窗口
}
/*
* Display a dialog box for inputing a string
*
*/
int dialog_inputbox(const char *title, const char *prompt, int height, int width,
const char *init)
{
int i, x, y, box_y, box_x, box_width;
int input_x = 0, key = 0, button = -1;
int show_x, len, pos;
char *instr = dialog_input_result;
WINDOW *dialog;
int i, x, y, box_y, box_x, box_width;
// 定义一些变量用于计算和存储位置和大小
int input_x = 0, key = 0, button = -1;
// 定义输入框的 x 坐标、按键和按钮状态变量
int show_x, len, pos;
// 定义显示 x 坐标、字符串长度和光标位置变量
char *instr = dialog_input_result;
// 指向输入结果的指针
WINDOW *dialog;
// 定义对话框窗口指针
if (!init)
instr[0] = '\0';
instr[0] = '\0';
// 如果没有初始值,将输入结果置为空字符串
else
strcpy(instr, init);
strcpy(instr, init);
// 否则,将初始值复制到输入结果中
do_resize:
if (getmaxy(stdscr) <= (height - INPUTBOX_HEIGTH_MIN))
return -ERRDISPLAYTOOSMALL;
if (getmaxx(stdscr) <= (width - INPUTBOX_WIDTH_MIN))
return -ERRDISPLAYTOOSMALL;
do_resize:
// 标签,用于处理窗口大小变化
if (getmaxy(stdscr) <= (height - INPUTBOX_HEIGTH_MIN))
// 如果屏幕高度不足以显示对话框
return -ERRDISPLAYTOOSMALL;
// 返回错误代码
if (getmaxx(stdscr) <= (width - INPUTBOX_WIDTH_MIN))
// 如果屏幕宽度不足以显示对话框
return -ERRDISPLAYTOOSMALL;
// 返回错误代码
/* center dialog box on screen */
x = (getmaxx(stdscr) - width) / 2;
y = (getmaxy(stdscr) - height) / 2;
/* 在屏幕上居中对话框 */
x = (getmaxx(stdscr) - width) / 2;
// 计算对话框的 x 坐标
y = (getmaxy(stdscr) - height) / 2;
// 计算对话框的 y 坐标
draw_shadow(stdscr, y, x, height, width);
draw_shadow(stdscr, y, x, height, width);
// 绘制对话框阴影
dialog = newwin(height, width, y, x);
keypad(dialog, TRUE);
dialog = newwin(height, width, y, x);
// 创建对话框窗口
keypad(dialog, TRUE);
// 启用对话框窗口的键盘功能
draw_box(dialog, 0, 0, height, width,
draw_box(dialog, 0, 0, height, width,
// 绘制对话框边框
dlg.dialog.atr, dlg.border.atr);
wattrset(dialog, dlg.border.atr);
mvwaddch(dialog, height - 3, 0, ACS_LTEE);
wattrset(dialog, dlg.border.atr);
// 设置对话框边框属性
mvwaddch(dialog, height - 3, 0, ACS_LTEE);
// 绘制左下角边框字符
for (i = 0; i < width - 2; i++)
waddch(dialog, ACS_HLINE);
wattrset(dialog, dlg.dialog.atr);
waddch(dialog, ACS_RTEE);
waddch(dialog, ACS_HLINE);
// 绘制水平边框线
wattrset(dialog, dlg.dialog.atr);
// 设置对话框属性
waddch(dialog, ACS_RTEE);
// 绘制右下角边框字符
print_title(dialog, title, width);
// 打印对话框标题
wattrset(dialog, dlg.dialog.atr);
print_autowrap(dialog, prompt, width - 2, 1, 3);
wattrset(dialog, dlg.dialog.atr);
// 设置对话框属性
print_autowrap(dialog, prompt, width - 2, 1, 3);
// 打印提示信息
/* Draw the input field box */
box_width = width - 6;
getyx(dialog, y, x);
box_y = y + 2;
box_x = (width - box_width) / 2;
draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
/* 绘制输入框 */
box_width = width - 6;
// 计算输入框宽度
getyx(dialog, y, x);
// 获取当前光标位置
box_y = y + 2;
// 计算输入框的 y 坐标
box_x = (width - box_width) / 2;
// 计算输入框的 x 坐标
draw_box(dialog, y + 1, box_x - 1, 3, box_width + 2,
// 绘制输入框边框
dlg.dialog.atr, dlg.border.atr);
print_buttons(dialog, height, width, 0);
print_buttons(dialog, height, width, 0);
// 打印按钮
/* Set up the initial value */
wmove(dialog, box_y, box_x);
wattrset(dialog, dlg.inputbox.atr);
/* 设置初始值 */
wmove(dialog, box_y, box_x);
// 移动光标到输入框起始位置
wattrset(dialog, dlg.inputbox.atr);
// 设置输入框属性
len = strlen(instr);
pos = len;
len = strlen(instr);
// 获取输入字符串的长度
pos = len;
// 设置光标位置为字符串长度
if (len >= box_width) {
show_x = len - box_width + 1;
input_x = box_width - 1;
for (i = 0; i < box_width - 1; i++)
if (len >= box_width) {
// 如果字符串长度超过输入框宽度
show_x = len - box_width + 1;
// 计算显示起始 x 坐标
input_x = box_width - 1;
// 设置输入框的 x 坐标
for (i = 0; i < box_width - 1; i++)
// 在输入框中显示字符串
waddch(dialog, instr[show_x + i]);
} else {
show_x = 0;
input_x = len;
waddstr(dialog, instr);
show_x = 0;
// 设置显示起始 x 坐标为 0
input_x = len;
// 设置输入框的 x 坐标为字符串长度
waddstr(dialog, instr);
// 在输入框中显示字符串
}
wmove(dialog, box_y, box_x + input_x);
wmove(dialog, box_y, box_x + input_x);
// 移动光标到字符串末尾
wrefresh(dialog);
wrefresh(dialog);
// 刷新对话框窗口
while (key != KEY_ESC) {
key = wgetch(dialog);
// 循环直到按下 ESC 键
key = wgetch(dialog);
// 获取按键
if (button == -1) { /* Input box selected */
if (button == -1) { /* 输入框被选中 */
switch (key) {
case TAB:
case KEY_UP:
case KEY_DOWN:
break;
case KEY_BACKSPACE:
case 127:
if (pos) {
wattrset(dialog, dlg.inputbox.atr);
if (input_x == 0) {
show_x--;
case TAB:
// 制表符键
case KEY_UP:
// 向上箭头键
case KEY_DOWN:
// 向下箭头键
break;
// 忽略这些按键
case KEY_BACKSPACE:
// 退格键
case 127:
// ASCII 退格键
if (pos) {
// 如果光标位置不为 0
wattrset(dialog, dlg.inputbox.atr);
// 设置输入框属性
if (input_x == 0) {
// 如果输入框的 x 坐标为 0
show_x--;
// 显示起始 x 坐标减 1
} else
input_x--;
input_x--;
// 输入框的 x 坐标减 1
if (pos < len) {
for (i = pos - 1; i < len; i++) {
if (pos < len) {
// 如果光标位置小于字符串长度
for (i = pos - 1; i < len; i++)
// 删除字符
instr[i] = instr[i+1];
}
}
pos--;
len--;
pos--;
// 光标位置减 1
len--;
// 字符串长度减 1
instr[len] = '\0';
wmove(dialog, box_y, box_x);
for (i = 0; i < box_width; i++) {
// 字符串末尾添加空字符
wmove(dialog, box_y, box_x);
// 移动光标到输入框起始位置
for (i = 0; i < box_width; i++) {
// 在输入框中显示字符串
if (!instr[show_x + i]) {
waddch(dialog, ' ');
waddch(dialog, ' ');
// 如果字符串结束,添加空格
break;
}
waddch(dialog, instr[show_x + i]);
}
wmove(dialog, box_y, input_x + box_x);
wrefresh(dialog);
}
continue;
case KEY_LEFT:
if (pos > 0) {
if (input_x > 0) {
wmove(dialog, box_y, --input_x + box_x);
} else if (input_x == 0) {
show_x--;
wmove(dialog, box_y, box_x);
for (i = 0; i < box_width; i++) {
if (!instr[show_x + i]) {
waddch(dialog, ' ');
break;
}
waddch(dialog, instr[show_x + i]);
}
wmove(dialog, box_y, box_x);
}
pos--;
}
continue;
case KEY_RIGHT:
if (pos < len) {
if (input_x < box_width - 1) {
wmove(dialog, box_y, ++input_x + box_x);
} else if (input_x == box_width - 1) {
show_x++;
wmove(dialog, box_y, box_x);
for (i = 0; i < box_width; i++) {
if (!instr[show_x + i]) {
waddch(dialog, ' ');
break;
}
waddch(dialog, instr[show_x + i]);
}
wmove(dialog, box_y, input_x + box_x);
}
pos++;
}
continue;
default:
if (key < 0x100 && isprint(key)) {
if (len < MAX_LEN) {
wattrset(dialog, dlg.inputbox.atr);
if (pos < len) {
for (i = len; i > pos; i--)
instr[i] = instr[i-1];
instr[pos] = key;
} else {
instr[len] = key;
}
pos++;
len++;
instr[len] = '\0';
if (input_x == box_width - 1) {
show_x++;
} else {
input_x++;
}
wmove(dialog, box_y, box_x);
for (i = 0; i < box_width; i++) {
if (!instr[show_x + i]) {
waddch(dialog, ' ');
break;
}
waddch(dialog, instr[show_x + i]);
}
wmove(dialog, box_y, input_x + box_x);
wrefresh(dialog);
} else
flash(); /* Alarm user about overflow */
continue;
wmove(dialog, box_y, input_x + box_x);
// 移动光标到字符串末尾
wrefresh(dialog);
// 刷新对话框窗口
}
}
}
switch (key) {
case 'O':
case 'o':
delwin(dialog);
return 0;
case 'H':
case 'h':
delwin(dialog);
return 1;
case KEY_UP:
case KEY_LEFT:
switch (button) {
case -1:
button = 1; /* Indicates "Help" button is selected */
print_buttons(dialog, height, width, 1);
continue;
// 继续循环
case KEY_LEFT:
// 向左箭头键
if (pos > 0) {
// 如果光标位置大于 0
if (input_x > 0) {
// 如果输入
print_buttons(dialog, height, width, 1); // 打印按钮,选中 "Help" 按钮
break;
case 0:
button = -1; /* Indicates input box is selected */
case 0: // 如果 "Ok" 按钮被选中
button = -1; // 表示输入框被选中
print_buttons(dialog, height, width, 0);
wmove(dialog, box_y, box_x + input_x);
wrefresh(dialog);
wmove(dialog, box_y, box_x + input_x); // 移动光标到输入框
wrefresh(dialog); // 刷新对话框窗口
break;
case 1:
button = 0; /* Indicates "OK" button is selected */
case 1: // 如果 "Help" 按钮被选中
button = 0; // 表示 "Ok" 按钮被选中
print_buttons(dialog, height, width, 0);
break;
}
break;
case TAB:
case KEY_DOWN:
case KEY_RIGHT:
case TAB: // 制表符键
case KEY_DOWN: // 向下箭头键
case KEY_RIGHT: // 向右箭头键
switch (button) {
case -1:
button = 0; /* Indicates "OK" button is selected */
case -1: // 如果输入框被选中
button = 0; // 表示 "Ok" 按钮被选中
print_buttons(dialog, height, width, 0);
break;
case 0:
button = 1; /* Indicates "Help" button is selected */
case 0: // 如果 "Ok" 按钮被选中
button = 1; // 表示 "Help" 按钮被选中
print_buttons(dialog, height, width, 1);
break;
case 1:
button = -1; /* Indicates input box is selected */
case 1: // 如果 "Help" 按钮被选中
button = -1; // 表示输入框被选中
print_buttons(dialog, height, width, 0);
wmove(dialog, box_y, box_x + input_x);
wrefresh(dialog);
wmove(dialog, box_y, box_x + input_x); // 移动光标到输入框
wrefresh(dialog); // 刷新对话框窗口
break;
}
break;
case ' ':
case '\n':
delwin(dialog);
return (button == -1 ? 0 : button);
case 'X':
case 'x':
key = KEY_ESC;
case ' ': // 空格键
case '\n': // 回车键
delwin(dialog); // 删除对话框窗口
return (button == -1 ? 0 : button); // 返回按钮状态
case 'X': // 'X' 键
case 'x': // 'x' 键
key = KEY_ESC; // 设置按键为 ESC 键
break;
case KEY_ESC:
key = on_key_esc(dialog);
case KEY_ESC: // ESC 键
key = on_key_esc(dialog); // 处理 ESC 键
break;
case KEY_RESIZE:
delwin(dialog);
on_key_resize();
goto do_resize;
case KEY_RESIZE: // 窗口大小变化键
delwin(dialog); // 删除对话框窗口
on_key_resize(); // 处理窗口大小变化
goto do_resize; // 跳转到 do_resize 标签重新绘制对话框
}
}
delwin(dialog);
return KEY_ESC; /* ESC pressed */
}
delwin(dialog); // 删除对话框窗口
return KEY_ESC; // 返回 ESC 键表示用户取消操作
}

File diff suppressed because it is too large Load Diff

@ -19,21 +19,25 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "dialog.h"
#include "dialog.h" // 包含对话框库的头文件
/*
* Display termination buttons
*/
static void print_buttons(WINDOW * dialog, int height, int width, int selected)
{
int x = width / 2 - 10;
int y = height - 2;
int x = width / 2 - 10; // 计算 "Yes" 按钮的起始位置
int y = height - 2; // 计算按钮的垂直位置
print_button(dialog, gettext(" Yes "), y, x, selected == 0);
print_button(dialog, gettext(" No "), y, x + 13, selected == 1);
print_button(dialog, gettext(" Yes "), y, x, selected == 0); // 打印 "Yes" 按钮
print_button(dialog, gettext(" No "), y, x + 13, selected == 1); // 打印 "No" 按钮
wmove(dialog, y, x + 1 + 13 * selected);
wrefresh(dialog);
wmove(dialog, y, x + 1 + 13 * selected); // 移动光标到选中的按钮
wrefresh(dialog); // 刷新窗口以显示更改
}
/*
@ -41,74 +45,74 @@ static void print_buttons(WINDOW * dialog, int height, int width, int selected)
*/
int dialog_yesno(const char *title, const char *prompt, int height, int width)
{
int i, x, y, key = 0, button = 0;
WINDOW *dialog;
int i, x, y, key = 0, button = 0; // 定义变量用于存储位置、按键和按钮状态
WINDOW *dialog; // 定义对话框窗口
do_resize:
if (getmaxy(stdscr) < (height + YESNO_HEIGTH_MIN))
return -ERRDISPLAYTOOSMALL;
return -ERRDISPLAYTOOSMALL; // 如果屏幕高度不足以显示对话框,返回错误
if (getmaxx(stdscr) < (width + YESNO_WIDTH_MIN))
return -ERRDISPLAYTOOSMALL;
return -ERRDISPLAYTOOSMALL; // 如果屏幕宽度不足以显示对话框,返回错误
/* center dialog box on screen */
x = (getmaxx(stdscr) - width) / 2;
y = (getmaxy(stdscr) - height) / 2;
x = (getmaxx(stdscr) - width) / 2; // 计算对话框的水平中心位置
y = (getmaxy(stdscr) - height) / 2; // 计算对话框的垂直中心位置
draw_shadow(stdscr, y, x, height, width);
draw_shadow(stdscr, y, x, height, width); // 绘制对话框阴影
dialog = newwin(height, width, y, x);
keypad(dialog, TRUE);
dialog = newwin(height, width, y, x); // 创建对话框窗口
keypad(dialog, TRUE); // 启用键盘输入处理
draw_box(dialog, 0, 0, height, width,
dlg.dialog.atr, dlg.border.atr);
wattrset(dialog, dlg.border.atr);
mvwaddch(dialog, height - 3, 0, ACS_LTEE);
dlg.dialog.atr, dlg.border.atr); // 绘制对话框边框
wattrset(dialog, dlg.border.atr); // 设置边框属性
mvwaddch(dialog, height - 3, 0, ACS_LTEE); // 添加左下角字符
for (i = 0; i < width - 2; i++)
waddch(dialog, ACS_HLINE);
wattrset(dialog, dlg.dialog.atr);
waddch(dialog, ACS_RTEE);
waddch(dialog, ACS_HLINE); // 添加水平线
wattrset(dialog, dlg.dialog.atr); // 设置对话框属性
waddch(dialog, ACS_RTEE); // 添加右下角字符
print_title(dialog, title, width);
print_title(dialog, title, width); // 打印标题
wattrset(dialog, dlg.dialog.atr);
print_autowrap(dialog, prompt, width - 2, 1, 3);
wattrset(dialog, dlg.dialog.atr); // 设置对话框属性
print_autowrap(dialog, prompt, width - 2, 1, 3); // 打印提示信息
print_buttons(dialog, height, width, 0);
print_buttons(dialog, height, width, 0); // 打印底部按钮
while (key != KEY_ESC) {
key = wgetch(dialog);
key = wgetch(dialog); // 获取用户按键输入
switch (key) {
case 'Y':
case 'y':
delwin(dialog);
return 0;
delwin(dialog); // 删除对话框窗口
return 0; // 返回0表示 "Yes" 按钮被选中
case 'N':
case 'n':
delwin(dialog);
return 1;
delwin(dialog); // 删除对话框窗口
return 1; // 返回1表示 "No" 按钮被选中
case TAB:
case KEY_LEFT:
case KEY_RIGHT:
button = ((key == KEY_LEFT ? --button : ++button) < 0) ? 1 : (button > 1 ? 0 : button);
print_buttons(dialog, height, width, button);
wrefresh(dialog);
print_buttons(dialog, height, width, button); // 打印按钮
wrefresh(dialog); // 刷新窗口以显示更改
break;
case ' ':
case '\n':
delwin(dialog);
return button;
delwin(dialog); // 删除对话框窗口
return button; // 返回按钮状态
case KEY_ESC:
key = on_key_esc(dialog);
key = on_key_esc(dialog); // 处理 ESC 键
break;
case KEY_RESIZE:
delwin(dialog);
on_key_resize();
goto do_resize;
delwin(dialog); // 删除对话框窗口
on_key_resize(); // 处理窗口大小变化
goto do_resize; // 重新调整对话框大小
}
}
delwin(dialog);
return key; /* ESC pressed */
}
delwin(dialog); // 删除对话框窗口
return key; /* ESC pressed */ // 返回 ESC 键
}
Loading…
Cancel
Save