TabEditor/main.aardio

556 lines
16 KiB
Plaintext
Raw Normal View History

2024-06-16 18:49:26 +08:00
//RUNAS
import win;
import win.ui;
import win.ui.toolbar;
import win.ui.accelerator;
import fonts.fontAwesome;
import console;
import config;
import flexcell;
import teUtils;
import teMenu;
/*DSG{{*/
2024-07-20 21:43:20 +08:00
var mainForm = win.form(text="TabEditor++";right=1399;bottom=799;bgcolor=16777215)
2024-06-16 18:49:26 +08:00
mainForm.add(
2024-07-20 21:43:20 +08:00
custom={cls="custom";text="自定义控件";left=0;top=36;right=1400;bottom=800;ah=1;aw=1;bgcolor=12639424;db=1;dl=1;dr=1;dt=1;z=2};
plus={cls="plus";left=0;top=0;right=1400;bottom=36;bgcolor=16777215;border={left=1;top=1;right=1;color=-6250332};clipBk=false;clipch=1;dl=1;dr=1;dt=1;font=LOGFONT(h=-18);forecolor=16777215;notify=1;repeat="stretch";z=1}
2024-06-16 18:49:26 +08:00
)
/*}}*/
//整理内存
import process;
import raw;
mainForm.release = function(){
collectgarbage("collect");
collectgarbage("collect");
2024-06-16 18:49:26 +08:00
}
var frmChild = mainForm.loadForm("\dlg\searchForm.aardio");
var g_mainform_text_default = mainForm.text;
2024-06-16 18:49:26 +08:00
//菜单控件初始化
g_teMenus = teMenu.TeMenu(mainForm.plus);
//flexcell控件初始化
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell = flexcell.FlexGrid(mainForm.custom,,,1);
mainForm.g_flexcell.setTheme(flexcell.FlexGrid.themeWps);
mainForm.g_flexcell.isModified = false;
mainForm.g_flexcell.ShowResizeTip = true;
teUtils.initFlexCellShow(mainForm.g_flexcell);
mainForm.g_flexcell.AllowFilter = false;
2024-06-16 18:49:26 +08:00
var showModifyStyle = function(){
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.isModified = true;
2024-06-16 18:49:26 +08:00
if(teUtils.getCurFilePath() == null){
g_teMenus.disabled(0, 3, true);
}
else {
g_teMenus.disabled(0, 3, false);
}
g_teMenus.disabled(0, 4, false);
mainForm.text = string.replace(mainForm.text, '[ ][\*]', "");
mainForm.text += " *";
}
var showUnmodifyStyle = function(){
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.isModified = false;
2024-06-16 18:49:26 +08:00
g_teMenus.disabled(0, 3, true);
g_teMenus.disabled(0, 4, true);
mainForm.text = string.replace(mainForm.text, '[ ][\*]', "");
}
showUnmodifyStyle();
/*菜单{{*/
//menu-新建
g_teMenus.newFile = function(ctrl){
return function(){
2024-06-17 23:41:57 +08:00
var curRow = mainForm.g_flexcell.ActiveCell().Row;
var curCol = mainForm.g_flexcell.ActiveCell().Col;
mainForm.g_flexcell.FrozenRows = 0;
mainForm.g_flexcell.FrozenCols = 0;
mainForm.g_flexcell.range(0,0,mainForm.g_flexcell.rowCount,mainForm.g_flexcell.colCount).ClearBackColor();
mainForm.g_flexcell.isModified = true;//避免 ModifyStyle, UnmodifyStyle 切换闪烁
mainForm.g_flexcell.clear();
teUtils.initFlexCellShow(mainForm.g_flexcell);
2024-06-16 18:49:26 +08:00
showUnmodifyStyle();
mainForm.release();
2024-06-16 18:49:26 +08:00
}
}
//menu-打开
g_teMenus.openFile = function(ctrl){
return function(){
var t = teUtils.getTxtTableData();
if(t){
mainForm.text = g_mainform_text_default;
mainForm.text += "[ " + teUtils.getCurFilePath() + " ]";
2024-06-17 23:41:57 +08:00
g_teMenus.newFile();
mainForm.g_flexcell.AutoRedraw = false;
mainForm.g_flexcell.setTable(t,,t.fields);
mainForm.g_flexcell.column(0).AutoFit();
if(mainForm.g_flexcell.Column(0).Width < 40){
mainForm.g_flexcell.Column(0).Width = 40;
}
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.AutoRedraw = true;
mainForm.g_flexcell.Refresh();
collectgarbage("collect");
collectgarbage("collect");
showModifyStyle();
2024-06-16 18:49:26 +08:00
}
}
}
//menu-保存
g_teMenus.saveFile = function(ctrl){
return function(){
2024-06-17 23:41:57 +08:00
teUtils.saveTxtTableData(mainForm.g_flexcell.getVlistData());
2024-06-16 18:49:26 +08:00
showUnmodifyStyle();
}
}
//menu-另存为
g_teMenus.saveAsFile = function(ctrl){
return function(){
2024-06-17 23:41:57 +08:00
teUtils.saveAsTxtTableData(mainForm.g_flexcell.getVlistData());
mainForm.text += "[ " + teUtils.getCurFilePath() + " ]";
2024-06-16 18:49:26 +08:00
showUnmodifyStyle();
}
}
//menu-设置背景色
g_teMenus.setBackground = function(ctrl){
return function(){
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.selection().BackColor = color.rgbReverse(ctrl.backgroundColor);
2024-06-16 18:49:26 +08:00
}
}
import win.ui.ctrl.pick;
g_teMenus.setBackgroundRClick = function(ctrl){
var colorPick = win.ui.ctrl.pick(mainForm);
colorPick.onColorChange = function(clr){
ctrl.skin({
background={
active=clr;
default=clr;
hover=clr
};
foreground={
hover = color.argb(0,0,0,20);
active = color.argb(0,0,0,40)
};
color={
active=0xE8000000;
default=0xFF000000;
disabled=0xFF6D6D6D
}
})
}
colorPick.setColor(ctrl.backgroundColor);
var x,y = win.getMessagePos();
colorPick.setPos(x,y);
colorPick.doModal();
}
//menu-设置前景色
g_teMenus.setForeground = function(ctrl){
return function(){
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.selection().ForeColor = color.rgbReverse(ctrl.argbColor);
2024-06-16 18:49:26 +08:00
}
}
g_teMenus.setForegroundRClick = function(ctrl){
var colorPick = win.ui.ctrl.pick(mainForm);
colorPick.onColorChange = function(clr){
ctrl.skin({
background={};
foreground={
hover = color.argb(0,0,0,20);
active = color.argb(0,0,0,40)
};
color={
active=clr;
default=clr;
disabled=clr
}
})
}
colorPick.setColor(ctrl.argbColor);
var x,y = win.getMessagePos();
colorPick.setPos(x,y);
colorPick.doModal();
}
class FlexFreeeItem {
bool change = false;
int start_row = 0;
int start_col = 0;
int end_row = 0;
int end_col = 0;
}
var flexFreeeCol = FlexFreeeItem();
var flexFreeeRow = FlexFreeeItem();
var setFlexFreee = function(freee){
if(flexFreeeCol.change){
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.range(flexFreeeCol.start_row,
2024-06-16 18:49:26 +08:00
flexFreeeCol.start_col,
flexFreeeCol.end_row,
flexFreeeCol.end_col).setBorders(2/*_FlexCell2_Edge_Right*/, 0/*_FlexCell2_BorderStyle_cellFlat*/ );
}
if(flexFreeeRow.change){
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.range(flexFreeeRow.start_row,
2024-06-16 18:49:26 +08:00
flexFreeeRow.start_col,
flexFreeeRow.end_row,
flexFreeeRow.end_col).setBorders(8/*_FlexCell2_Edge_Bottom*/ , 0/*_FlexCell2_BorderStyle_cellFlat*/ );
}
if(!freee){return true;}
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.range(flexFreeeCol.start_row, flexFreeeCol.start_col, flexFreeeCol.end_row, flexFreeeCol.end_col
2024-06-16 18:49:26 +08:00
).setBorders(2/*_FlexCell2_Edge_Right*/, 1/*_FlexCell2_BorderStyle_cellFlat*/);
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.range(flexFreeeRow.start_row, flexFreeeRow.start_col, flexFreeeRow.end_row, flexFreeeRow.end_col
2024-06-16 18:49:26 +08:00
).setBorders(8/*_FlexCell2_Edge_Bottom*/ , 1/*_FlexCell2_BorderStyle_cellFlat*/);;
}
//menu-冻结行&列
g_teMenus.freeeRowCol = function(ctrl){
return function(){
2024-06-17 23:41:57 +08:00
var curRow = mainForm.g_flexcell.ActiveCell().Row;
var curCol = mainForm.g_flexcell.ActiveCell().Col;
2024-06-16 18:49:26 +08:00
setFlexFreee(false);
//绘制冻结列的右侧边框线
flexFreeeCol.change = true;
flexFreeeCol.start_row = curRow+1;
flexFreeeCol.start_col = curCol;
2024-06-17 23:41:57 +08:00
flexFreeeCol.end_row = mainForm.g_flexcell.rowCount;
2024-06-16 18:49:26 +08:00
flexFreeeCol.end_col = curCol;
//绘制冻结行的底部边框线
flexFreeeRow.change = true;
flexFreeeRow.start_row = curRow;
flexFreeeRow.start_col = curCol+1;
flexFreeeRow.end_row = curRow;
2024-06-17 23:41:57 +08:00
flexFreeeRow.end_col = mainForm.g_flexcell.colCount;
2024-06-16 18:49:26 +08:00
setFlexFreee(true);
//冻结行
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.FrozenRows = curRow;
2024-06-16 18:49:26 +08:00
//冻结列
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.FrozenCols = curCol;
2024-06-16 18:49:26 +08:00
}
}
//menu-取消冻结
g_teMenus.unfreeeRowCol = function(ctrl){
return function(){
setFlexFreee(false);
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.FrozenRows = 0;
mainForm.g_flexcell.FrozenCols = 0;
2024-06-16 18:49:26 +08:00
}
}
//menu-导出 Sqlite3
g_teMenus.exportSqlite3 = function(ctrl){
return function(){
}
}
//menu-导出 txt
g_teMenus.exportTxt = function(ctrl){
return function(){
}
}
//menu-关于
g_teMenus.showAbout = function(ctrl){
return function(){
2024-07-07 22:49:15 +08:00
import fsys.version;
var version = fsys.version.getInfo(io._exepath).productVersion;
mainForm.msgbox(version);
2024-06-16 18:49:26 +08:00
}
}
//menu-帮助
g_teMenus.showHelp = function(ctrl){
return function(){
mainForm.release();
}
}
g_teMenus.setCommandFunc(0, 1, g_teMenus.newFile);
g_teMenus.setCommandFunc(0, 2, g_teMenus.openFile);
g_teMenus.setCommandFunc(0, 3, g_teMenus.saveFile);
g_teMenus.setCommandFunc(0, 4, g_teMenus.saveAsFile);
g_teMenus.setCommandFunc(0, 5, g_teMenus.setBackground);
g_teMenus.setRightClickFunc(0, 5, g_teMenus.setBackgroundRClick);
g_teMenus.setCommandFunc(0, 6, g_teMenus.setForeground);
g_teMenus.setRightClickFunc(0, 6, g_teMenus.setForegroundRClick);
g_teMenus.setCommandFunc(0, 7, g_teMenus.freeeRowCol);
g_teMenus.setCommandFunc(0, 8, g_teMenus.unfreeeRowCol);
g_teMenus.setCommandFunc(0, 9, g_teMenus.exportSqlite3);
g_teMenus.setCommandFunc(0, 10, g_teMenus.exportTxt);
g_teMenus.setCommandFunc(0, 11, g_teMenus.showAbout);
g_teMenus.setCommandFunc(1, 1, g_teMenus.showHelp);
/*}}*/
2024-06-16 22:19:47 +08:00
pre_searched_row = null;
pre_searched_col = null;
2024-06-16 18:49:26 +08:00
/*消息回调{{*/
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.onCellChange = function(Row,Col){
if(!mainForm.g_flexcell.isModified){
2024-06-16 18:49:26 +08:00
showModifyStyle();
}
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.isModified = true;
2024-06-16 18:49:26 +08:00
}
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.onClick = function(Row,Col){
2024-06-16 18:49:26 +08:00
var row,col=owner.toRelativeRow(Row),owner.toRelativeCol(Col);
}
//左侧弹出菜单
mainForm.popmenuLeft = win.ui.popmenu(mainForm);
mainForm.popmenuLeft.add('向上插入一行',function(id){
var curSelection = mainForm.g_flexcell.selection();
teUtils.insertFlexCellNewRow(mainForm.g_flexcell, curSelection.FirstRow, 1, mainForm.g_flexcell.colCount);
curSelection.release();
mainForm.release();
} )
mainForm.popmenuLeft.add('向下插入一行',function(id){
var curSelection = mainForm.g_flexcell.selection();
teUtils.insertFlexCellNewRow(mainForm.g_flexcell, curSelection.FirstRow + 1, 1, mainForm.g_flexcell.colCount);
curSelection.release();
mainForm.release();
} )
2024-06-18 23:18:39 +08:00
mainForm.popmenuLeft.add('删除行',function(id){
var curSelection = mainForm.g_flexcell.selection();
mainForm.g_flexcell.delRow(curSelection.FirstRow, 1/*删除数量*/);
curSelection.release();
mainForm.release();
} )
//右侧弹出菜单
mainForm.popmenuRight = win.ui.popmenu(mainForm);
mainForm.popmenuRight.add('向左插入一列',function(id){
var curSelection = mainForm.g_flexcell.selection();
teUtils.insertFlexCellNewCol(mainForm.g_flexcell, curSelection.FirstCol, 1, mainForm.g_flexcell.colCount);
curSelection.release();
mainForm.release();
} )
mainForm.popmenuRight.add('向右插入一列',function(id){
var curSelection = mainForm.g_flexcell.selection();
teUtils.insertFlexCellNewCol(mainForm.g_flexcell, curSelection.FirstCol+1, 1, mainForm.g_flexcell.colCount);
curSelection.release();
mainForm.release();
} )
2024-06-18 23:18:39 +08:00
mainForm.popmenuRight.add('删除列',function(id){
var curSelection = mainForm.g_flexcell.selection();
//TODO
mainForm.msgbox("暂无删除列功能--TODO");
2024-06-18 23:18:39 +08:00
curSelection.release();
mainForm.release();
} )
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.onMouseUp = function(Button , Shift, x, y){
2024-06-16 18:49:26 +08:00
/*** 鼠标按键放开。
Button 当前按下的鼠标按钮1左按钮2右按钮4中间按钮同时按下时执行位或操作。
Shift 功能键状态1SHIFT2CTRL4ALT同时按下时执行位或操作。
x,y 鼠标指针当前位置 ***/
if(Button == 2){
mainForm.g_flexcell.cell(mainForm.g_flexcell.MouseRow, mainForm.g_flexcell.MouseCol).Select();
var x,y = win.getMessagePos();
if(mainForm.g_flexcell.MouseCol == 0){
mainForm.popmenuLeft.popup(x,y,true)
}
elseif(mainForm.g_flexcell.MouseRow == 0){
mainForm.popmenuRight.popup(x,y,true)
}
2024-06-16 18:49:26 +08:00
}
2024-06-16 22:19:47 +08:00
if(pre_searched_row != null and pre_searched_col != null){
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.Cell(pre_searched_row, pre_searched_col).BackColor = 0xffffff;
pre_searched_row = null;
pre_searched_col = null;
2024-06-16 22:19:47 +08:00
}
}
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.onSelChange = function(FirstRow,FirstCol,LastRow,LastCol){
2024-06-16 22:19:47 +08:00
/*** 在Selection选择范围改变时发生。参数起始行起始列结束行结束列 ***/
var row1,col1=owner.toRelativeRow(FirstRow),owner.toRelativeCol(FirstCol);
var row2,col2=owner.toRelativeRow(LastRow),owner.toRelativeCol(LastCol);
console.log(row1, col1, row2, col2)
2024-06-16 18:49:26 +08:00
}
/*}}*/
research_flag = true;
2024-06-20 22:55:52 +08:00
searchNextContent = function(data, firstRow, firstCol, lastRow, lastCol, isRangeSearch, selFirstRow, selFirstCol, searchContent, isCaseSensitive, isCellMatching){
2024-06-16 18:49:26 +08:00
if(isRangeSearch){// 从一个范围开始搜索
var tempRow = firstRow;
var tempCol = firstCol;
firstRow = pre_searched_row != null and research_flag ? pre_searched_row : firstRow;
firstCol = pre_searched_col != null and research_flag ? pre_searched_col : firstCol;
for i,v in table.eachIndex(data){
if(i < firstRow or i > lastRow){
continue;
}
for i2,v2 in table.eachIndex(data.fields){
if(i2 < firstCol or i2 > lastCol){
continue;
}
if (i == pre_searched_row and i2 == pre_searched_col){
continue;
}
2024-06-20 22:55:52 +08:00
var currStr = v[v2];
if(#currStr == 0 and #searchContent != 0){
continue;
}
2024-06-20 22:55:52 +08:00
if(!isCaseSensitive){
currStr = string.lower(currStr);
searchContent = string.lower(searchContent);
}
2024-06-17 23:41:57 +08:00
if (isCellMatching){
2024-06-20 22:55:52 +08:00
if(currStr == searchContent){
2024-06-17 23:41:57 +08:00
research_flag = true;
pre_searched_row = i;
pre_searched_col = i2;
return i, i2;
}
}
else {
if(string.find(currStr, searchContent) or currStr == searchContent){
2024-06-17 23:41:57 +08:00
research_flag = true;
pre_searched_row = i;
pre_searched_col = i2;
return i, i2;
}
2024-06-16 18:49:26 +08:00
}
}
firstCol = tempRow;
firstCol = tempCol;
}
// 搜不到了,重头搜索
if(research_flag){
research_flag = false;
pre_searched_row = null;
pre_searched_col = null;
2024-06-20 22:55:52 +08:00
row, col = searchNextContent(data, selFirstRow, selFirstCol, lastRow, lastCol, isRangeSearch, selFirstRow, selFirstCol, searchContent, isCaseSensitive, isCellMatching);
2024-06-16 18:49:26 +08:00
return row, col;
}
research_flag = true;
}
else {// 从一个点开始搜索
for i,v in table.eachIndex(data){
if(i < firstRow){
continue;
}
for i2,v2 in table.eachIndex(data.fields){
if(i2 < firstCol){
continue
}
if(i == firstRow and i2 == firstCol){
continue;
}
2024-06-20 22:55:52 +08:00
var currStr = v[v2];
if(#currStr == 0 and #searchContent != 0){
continue;
}
2024-06-20 22:55:52 +08:00
if(!isCaseSensitive){
currStr = string.lower(currStr);
searchContent = string.lower(searchContent);
}
2024-06-17 23:41:57 +08:00
if (isCellMatching){
2024-06-20 22:55:52 +08:00
if(currStr == searchContent){
2024-06-17 23:41:57 +08:00
research_flag = true;
return i, i2;
}
}
else {
if(string.find(currStr, searchContent) or currStr == searchContent){
2024-06-17 23:41:57 +08:00
research_flag = true;
return i, i2;
}
2024-06-16 18:49:26 +08:00
}
}
firstCol = 1;
}
// 搜不到了,重头搜索
if(research_flag){
research_flag = false;
2024-06-20 22:55:52 +08:00
row, col = searchNextContent(data, 1, 1, 1, 1, isRangeSearch, selFirstRow, selFirstCol, searchContent, isCaseSensitive, isCellMatching);
2024-06-16 18:49:26 +08:00
return row, col;
}
research_flag = true;
}
}
subscribe("search_prev",function(...){
2024-06-16 22:19:47 +08:00
// TODO
2024-06-16 18:49:26 +08:00
console.dump(...);
} )
subscribe("search_next",function(...){
2024-06-17 23:41:57 +08:00
var args = {...};
var searchContent = args[1];
2024-06-20 22:55:52 +08:00
var isCaseSensitive = args[2];
var isCellMatching = args[3];
2024-06-17 23:41:57 +08:00
var curSelection = mainForm.g_flexcell.selection();
2024-06-16 18:49:26 +08:00
var firstRow = curSelection.FirstRow;
var firstCol = curSelection.FirstCol;
var lastRow = curSelection.LastRow;
var lastCol = curSelection.LastCol;
var isRangeSearch = false;
if(firstRow != lastRow or firstCol != lastCol){
isRangeSearch = true;
}
2024-06-16 22:19:47 +08:00
if(pre_searched_row != null and pre_searched_col != null){
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.Cell(pre_searched_row, pre_searched_col).BackColor = 0xffffff;
2024-06-16 22:19:47 +08:00
}
2024-06-20 22:55:52 +08:00
var row, col = searchNextContent(mainForm.g_flexcell.getVlistData(), firstRow, firstCol, lastRow, lastCol, isRangeSearch, firstRow, firstCol, searchContent, isCaseSensitive, isCellMatching);
2024-06-16 18:49:26 +08:00
//console.dump(row, col);
if(row != null && col != null){
if(isRangeSearch){
if(pre_searched_row != null and pre_searched_col != null){
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.Cell(pre_searched_row, pre_searched_col).BackColor = 0xff00ff;
2024-06-16 18:49:26 +08:00
}
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.Cell(row, col).EnsureVisible();
2024-06-16 18:49:26 +08:00
}
else {
pre_searched_row = null;
pre_searched_col = null;
2024-06-17 23:41:57 +08:00
mainForm.g_flexcell.cell(row, col).Select();
2024-06-16 18:49:26 +08:00
}
}
else {
frmChild.msgbox("找不到正在搜索的数据")
}
curSelection.release();
} )
2024-07-20 21:43:20 +08:00
//搜索窗口
var openSearchForm = function(){
frmChild.show();
frmChild.editSearchInput.setFocus();
}
var accelerator = win.ui.accelerator({
{
ctrl = true; vkey = 'F'#;
oncommand = function() openSearchForm();
};
{
ctrl = true; vkey = 'S'#;
oncommand = function(){
var curRow = mainForm.g_flexcell.ActiveCell().Row;
var curCol = mainForm.g_flexcell.ActiveCell().Col;
mainForm.g_flexcell.Cell(curRow, curCol).SetFocus(); // 不加这个,光标处于的单元格内容不会被保存----规避方案
teUtils.saveTxtTableData(mainForm.g_flexcell.getVlistData());
showUnmodifyStyle();
};
};
},mainForm );
2024-06-16 18:49:26 +08:00
import win.ui.minmax;
win.ui.minmax(mainForm,860,400);
mainForm.show();
return win.loopMessage();