/*********************************************************************************
   脚本说明： 柚子98五笔多功能版
   资源库:https://wubi98.gitee.io/ && http://98wb.ys168.com/
;~ 环境 版本:   Autohotkey v1.1.32.00
   柚子五笔多功能版改装自--@河许人@天黑请闭眼联合开发的「影子输入法」与@hello_srf的柚子输入法代码结构
*/
;*********************************************************************************

#NoEnv
;#NoTrayIcon
#SingleInstance, Force
#MaxThreadsPerHotkey 100
#Persistent
#WinActivateForce
#Include %A_ScriptDir%
;=============================启动文件判断=====================================
Loop, Files, main\*.exe
{
	if (A_LoopFileFullPath~="i)AutoHotkey|U64"&&A_PtrSize=4&&A_Is64bitOS){
		Run *RunAs "%A_LoopFileFullPath%" /restart "%A_ScriptFullPath%"
		Loop, Files, *.exe
			run, taskkill /im %A_LoopFileName% /F,,Hide 
		break
	}
}

;=============================================================================
#Include config\Gdip.ahk
#Include config\EasyIni.ahk
#Include config\Class_LV_Colors.ahk
#Include config\Class_SQLiteDB.ahk
#Include config\Class_ScrollGUI.ahk
#Include config\ToolTip.ahk
#Include config\Function.ahk
#Include config\Sql_Func.ahk

SetBatchLines, -1
SetKeyDelay, 30, 0
SetTitleMatchMode, RegEx
Process, Priority,, High
CoordMode, Caret, Screen
CoordMode, Mouse, Screen
CoordMode, ToolTip, Screen
OnMessage(0x204, "WM_RBUTTONDOWN")
OnMessage(0x201, "WM_LBUTTONDOWN")
OnMessage(0x200, "WM_MOUSEMOVE")
SetWorkingDir %A_ScriptDir%
WinGetPos,,,,Shell_Wnd ,ahk_class Shell_TrayWnd
global y2 :=A_ScreenHeight-Shell_Wnd-30    ;30是方案图标的高度
font_:=a_FontList~="i)Microsoft YaHei UI"?"Microsoft YaHei UI":"Microsoft YaHei"

;=====config.ini去重==============================
FileRead,content,config.ini
New_Content:=""
Loop,parse,content, `n ,`r
{
	IfNotInString,New_Content,%A_LoopField%
	{
		New_Content.=A_LoopField "`r`n"
	}
}
FileDelete, config.ini
FileAppend,%New_Content%,config.ini,utf-8
New_Content:=""
;===============================================

;读取配置
global srf_default_value,config_tip, mhyRegExstr, WubiIni:=class_EasyIni("config.ini")
;初始化默认配置
srf_default_value:={Settings:{Startup:"off", symb_mode:2, Prompt_Word:"off", Logo_X:"200", Logo_Y:y2, UIAccess:0, Logo_Switch:"on",Srf_Hotkey:"Shift", Select_Enter:"send", Initial_Mode:"off", symb_send:"on", set_color:"on", Wubi_Schema:"ci",Cut_Mode:"off", limit_code:"on", Trad_Mode:"off", IMEmode:"on"}
	, TipStyle:{FontType:font_, FontSize:20, FontColor:"002B36", FontStyle:"off", FontColor_Code:"800000",LineColor:"808080",BorderColor:"FF8040", Gdip_Line:"on", ToolTipStyle:"Gdip", Radius:"on", BgColor:"FFFFFF", ListNum:5, Textdirection:"horizontal", Set_Range:3, Fix_Switch:"off",Fix_X:A_ScreenWidth/2,Fix_Y:10}  ;竖排--vertical
	, CustomColors:{Color_Row1:"0x1C7399,0xEEEEEC,0x014E8B,0x444444,0x009FE8,0xDEF9FA,0xF8B62D,0x90FC0F", Color_Row2:"0x0078D7,0x0D1B0A,0xB9D497,0x00ADEF,0x1778BF,0xFDF6E3,0x002B36,0xDEDEDE"}
	, Versions:{Version:A_YYYY A_MM A_DD "-1"}
	, YSDllPath:{SQLDllPath_x86:"config\SQLite3_x86\SQLite3.dll", SQLDllPath_x64:"config\SQLite3_x64\SQLite3.dll"}}
;配置项说明
config_tip:={Settings:{Startup:"开机自启设置<on/off>",UIAccess:"候选框UI层级权限提升,1为开启 0为关闭", symb_mode:"中英文符号模式，1为英文 2为中文", Prompt_Word:"空码提示设置<on/off>",Srf_Hotkey:"中英文切换热键", Logo_X:"输入法logo图标x坐标", Logo_Y:"输入法logo图标y坐标", Logo_Switch:"logo图标显示与隐藏<on/off>", Select_Enter:"回车键功能定义<send为上屏编码/clean为清空编码>", Initial_Mode:"剪切板上屏开关<on/off>", symb_send:"符号顶屏开关<on/off>", set_color:"取色开关", Wubi_Schema:"方案设置项<ci为含词方案/zi为单字方案/chaoji为超集方案>",Cut_Mode:"字根拆分开关<on/off>", limit_code:"四码上屏开关<on/off>", Trad_Mode:"简繁模式切换<on为繁体/off为简体>", IMEmode:"中英文状态<on为中文/off为英文>"}
	, TipStyle:{FontType:"候选字体设置",FontStyle:"候选词粗体开关", FontSize:"候选字号大小设置", FontColor:"候选项颜色", FontColor_Code:"候选编码颜色",BorderColor:"候选框边框线",LineColor:"候选分割线选色", Gdip_Line:"Gdip候选框样式边框及候选框编码与候选词分隔线", ToolTipStyle:"候选框风格<on为tooltip样式/off为Gui候选样式/Gdip为Gdip候选样式>", Radius:"Gdip候选样式圆角开关<on/off>", BgColor:"候选框背景色<16进制色值>", ListNum:"候选数量", Textdirection:"horizontal为横排/vertical为竖排", Set_Range:"ToolTip样式编码与候选词距离", Fix_Switch:"候选框固定开关",Fix_X:"候选框固定x坐标",Fix_Y:"候选框固定y坐标"}
	, CustomColors:{Color_Row1:"配色对话框自定义颜色区域，第一排", Color_Row2:"配色对话框自定义颜色区域，第二排"}
	, Versions:{Version:"版本日期"}
	, YSDllPath:{SQLDllPath_x86:"SQlite数据库dll 32位路径", SQLDllPath_x64:"SQlite数据库dll 64位路径"}}
;默认配置检测
For Section, element In srf_default_value
	For key, value In element
		If ((%key%:=WubiIni[Section, key])="")
			%key%:=WubiIni[Section, key]:=value
;配置项说明项写入
if !WubiIni.GetTopComments()
	WubiIni.AddTopComment("程序运行时，配置文件直接修改无效！退出后修改才有效！！！")

FileRead, ini_var, ../main/config.ini

For Section, element In config_tip
	For Comments, value In element
		If !WubiIni.GetKeyComments(Section, Comments)
			If ini_var not contains Comments
				WubiIni.AddKeyComment(Section, Comments, value)

if (Logo_X<0||Logo_X>A_ScreenWidth||Logo_Y<0||Logo_Y>A_ScreenHeight)
	Logo_X :=WubiIni.Settings["Logo_X"]:=200,Logo_Y :=WubiIni.Settings["Logo_Y"]:=y2
if (Wubi_Schema<>"ci"&&Wubi_Schema<>"zi"&&Wubi_Schema<>"chaoji")
	Wubi_Schema :=WubiIni.Settings["Wubi_Schema"]:="ci"

;写入系统计划任务开机自启名称（可修改）
global Startup_Name :="柚子98五笔"

;开机自启项检测是否开启，以便与配置文件同步
cmd_zq= schtasks /Query /TN %Startup_Name%
global zq_:= cmdClipReturn(cmd_zq)
If zq_~=Startup_Name{
	Startup :=WubiIni.Settings["Startup"]:="on"
}else{
	Startup :=WubiIni.Settings["Startup"]:="off"
}

if not Srf_Hotkey ~="i)Ctrl|Shift|Alt|LWin"||Srf_Hotkey ~="\&$"
	Srf_Hotkey:=WubiIni.Settings["Srf_Hotkey"]:="Shift"
WubiIni.Save()

Gosub TRAY_Menu

if (IMEmode~="off"){
	if GetKeyState("CapsLock", "T")
		Menu, Tray, Icon, config\wubi98.icl,21,1
	else
		Menu, Tray, Icon, config\wubi98.icl,12,1
}
else
{
	srf_mode :=1
	if GetKeyState("CapsLock", "T")
		Menu, Tray, Icon, config\wubi98.icl,21,1
	else
		Menu, Tray, Icon, config\wubi98.icl,9,1
}

if (ToolTipStyle~="i)Gdip"&&A_OSVersion not in WIN_7,WIN_8,WIN_8.1,10.,6.1,6.2,6.3){
	;Traytip,,你的系统不支持当前Gdip候选框样式,请切换!,,2
	ToolTipStyle:=WubiIni.TipStyle["ToolTipStyle"]:="on", FontSize:=WubiIni.Settings["FontSize"]:=14, WubiIni.Save()
}

hk_conv(Srf_Hotkey){
	if Srf_Hotkey~="``|`;" {
		Srf_Hotkey:=RegExReplace(Srf_Hotkey,"``|`;",Srf_Hotkey~="``"?"``":"`;")
	} else if not Srf_Hotkey~="&" {
		Srf_Hotkey_:=(not Srf_Hotkey~="LWin"?"~":"") . Srf_Hotkey_
		;Srf_Hotkey_:="~" . Srf_Hotkey_   ;注释上行 启用此行如果设定是单独win键 则不屏蔽打开开始菜单功能
	}
	Srf_Hotkey_ :=Srf_Hotkey~="i)Shift"?RegExReplace(Srf_Hotkey,"i)Shift\&","+"):(Srf_Hotkey~="i)Ctrl"?RegExReplace(Srf_Hotkey,"i)Ctrl\&","^"):(Srf_Hotkey~="i)Alt"?RegExReplace(Srf_Hotkey,"i)Alt\&","!"):RegExReplace(Srf_Hotkey,"i)LWin\&","<#")))
	Return Srf_Hotkey_
}

if not GetKeyState(RegExReplace(Srf_Hotkey,".+\&",""))~="\d"
	Srf_Hotkey:=RegExReplace(Srf_Hotkey,"\&.+","")
global Srf_Hotkey_ :=hk_conv(Srf_Hotkey)
Hotkey, %Srf_Hotkey_%, SetHotkey,on

DBFileName:="DB\wubi98.db"
global DB := New SQLiteDB
If !DB.OpenDB(DBFileName)
	MsgBox, 16, 数据库DB错误, % "消息:`t" DB.ErrorMsg "`n代码:`t" DB.ErrorCode

SwitchToEngIME()

versions :=WubiIni.Versions["Version"]:=A_YYYY A_MM A_DD "-1"      ;版本日期设置
ahkIni.save()

srf_symblos:={"``":["``","·"], "~":["~","~"], "!":["+1","！"], "@":["+2","@"], "#":["+3","#"]
	, "$":["+4","￥"], "%":["+5","％"], "^":["+6","……"], "&":["+7","&"], "*":["+8","*"], "(":["+9","（）{Left}"]
	, ")":["+0","）"], "-":["-","-"], "=":["=","="], "[":["[","「"], "]":["]","」"]
	, "{":["{","{}{Left}"], "}":["}","}"], "\":["\","、"], "|":["|","|"], ";":[";","；"], ":":[":","："]
	, "'":["'","‘’{Left}"], "<":["<","《》{Left}"],">":[">","》"],",":[",","，"]
	,".":[".","。"], "/":["/","/"], "?":["?","？"], """":["""","“”{Left}"]}

;{{常用变量值初始化
global valueindex:=Cut_Mode?2:1
global waitnum:=select_sym:=0
global num__:=Result_Char:=Select_result :=""
global recent:={}
count_ci:=1
Select_Code=gfdsahjklm;'space    ;字母选词
citiao_arr:=select_arr:=select_value:=[]
;}}

WM_RBUTTONDOWN(){
	If (A_Gui=3||A_Gui="houxuankuang"||A_Gui="TSF"){
		Menu, TRAY, Show, x%Logo_X%, y%Logo_Y%
	}
}

global Tip_Show:={LineColor:"Gdip样式中间分隔线颜色",BorderColor : "Gdip样式四周边框线颜色", UIAccess:"候选框UI层级权限提升", SBA0 :"候选框固定坐标设置",About:"软件更新信息",ciku3:"英文词库导入`n（单行单义格式，以tab符隔开）`n「英文词条+Tab+词频」",ciku4:"英文词库导出`n（导出为单行单义格式txt码表）",ciku5:"特殊符号词库导入`n（格式「/引导字母+Tab+多符号以英文逗号隔开」）"
	, SBA5 : "固定候选框的位置，不跟随光标",BgColor:"候选框背景色", FontColor:"候选词字体颜色", FontColor_Code:"候选框编码字体颜色", SBA1:"繁体开关（输简出繁）", SBA4:"加入开机自启动任务",ciku6:"特殊符号词库导出`n（导出为txt）"
	, SBA13:"显示/隐藏桌面Logo图标", SBA12 : "候选词显示粗体",ciku1:"导入txt词库至数据库`ntxt码表格式需为「单行单义」",ciku2:"导出词库为「单行单义」的txt格式文本",SBA2:"候选词条显示拆分字根（需特殊字体支持，Ctrl+Shift+w打开下载页）"
	, SBA3:"当编码无词条时模糊匹配提示",SBA6:"符号顶首选屏并上屏该键符号",SBA7:"四位编码候选唯一时自动上屏，五码时顶首选上屏",SBA9:"Gdip候选框圆角开关",SBA10:"Gdip候选样式中间分隔线"
	, Save:"无码造词和自由模式可以同时进行需分行输入。格式如下：`nuqid=http://98wb.ys168.com/`nggte=五笔`n柚子输入法"}

WM_MOUSEMOVE()
{
	static CurRControl, PrevControl
	CurRControl := A_GuiControl
	if (CurRControl <> PrevControl and not InStr(CurRControl, " "))
	{
		ToolTip ; 关闭之前的 ToolTip.
		SetTimer, DisplayToolTip, 500
		PrevControl := CurRControl
	}
	return
	DisplayToolTip:
		SetTimer, DisplayToolTip, Off
		ToolTip % Tip_Show[CurRControl],%A_CaretX%,%A_CaretY%
		SetTimer, RMToolTip, 5000
	return
	RMToolTip:
		SetTimer, RMToolTip, Off
		ToolTip
	return
}

Gosub houxuankuangguicreate

;Z键记录历史记录，最大数目为ListNum
updateRecent(date){
	global
	loop % length:=objLength(recent){
		if(recent[a_index]==date){    ;删除重复的
			objRemoveAt(recent,a_index)
			break
		}
	}
	objInsertAt(recent,1,date)    ;新的放于最前
	if((length:=objLength(recent))>ListNum)    ;删除多的
	{
		objdelete(recent,ListNum+1,length)
	}
}
EnableUIAccess()
#Include config\Label.ahk
#Include config\Srf_Conf.ahk

