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

#NoEnv
;#NoTrayIcon
#SingleInstance, Force
#MaxThreadsPerHotkey 10
#Persistent
#WinActivateForce
#Include %A_ScriptDir%
#Include ..\config\EasyIni.ahk
#Include ..\config\Gdip.ahk
#Include ..\config\ToolTip.ahk
#Include ..\config\Function.ahk
#Include ..\config\Class_LV_Colors.ahk
#Include ..\config\Class_SQLiteDB.ahk

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

;读取配置
global srf_default_value, mhyRegExstr, WubiIni:=class_EasyIni("config.ini")
srf_default_value:={Settings:{Startup:"off", Prompt_Word:"off", Logo_X:"200", Logo_Y:y2, Logo_Switch:"on", Select_Enter:"send", Initial_Mode:"off", symb_send:"on", set_color:"on", Wubi_Schema:"ci",Cut_Mode:"off", limit_code:"on", lookup_status:"off", Trad_Mode:"off", IMEmode:"on"}
	, TipStyle:{FontType:"宋体", FontSize:13, FontColor:"C0C0C0", FontColor_Code:"B00000", Gdip_Line:"on", ToolTipStyle:"on", Radius:"on", BgColor:"444444", ListNum:5, Textdirection:"horizontal", Set_Range:3, Gdip_Down:5}  ;竖排--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"}}
For Section, element In srf_default_value
	For key, value In element
		If ((%key%:=WubiIni[Section, key])="")
			%key%:=WubiIni[Section, key]:=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"
WubiIni.Save()

Gosub TRAY_Menu

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

Gosub houxuankuangguicreate

DBFileName:="..\DB\wubi86.db"
DBFileName_ci:="..\DB\wubi86-ci.db"
DBFileName_cj:="..\DB\wubi98-extend.db"
DB_CI := New SQLiteDB
DB_CJ := New SQLiteDB
DB := New SQLiteDB
If !DB.OpenDB(DBFileName)
	MsgBox, 16, 数据库DB错误, % "消息:`t" DB.ErrorMsg "`n代码:`t" DB.ErrorCode
else If !DB_CI.OpenDB(DBFileName_ci)
	MsgBox, 16, 数据库DB_CI错误, % "消息:`t" DB.ErrorMsg "`n代码:`t" DB.ErrorCode
else If !DB_CJ.OpenDB(DBFileName_cj)
	MsgBox, 16, 数据库DB_CJ错误, % "消息:`t" DB.ErrorMsg "`n代码:`t" DB.ErrorCode

;预设初始化连接
SQL :="SELECT aim_chars FROM " (Wubi_Schema="chaoji"?"chaoji":(Wubi_Schema="ci"?"ci":"zi")) " WHERE A_Key = 'aaaa'"
If (Wubi_Schema="chaoji"?(DB_CJ.GetTable(SQL, Result)):(Wubi_Schema="ci"?(DB_CI.GetTable(SQL, Result)):(DB.GetTable(SQL, Result))))
{
	if (Result.Rows[1,1]="")
		Traytip,,词库连接失败！
}

if IME_Return_0E1C(){
	SwitchToEngIME()
	Traytip,  柚子86五笔版:,已切换至英文键盘！
}
else
	SwitchToEngIME()
versions :=WubiIni.Versions["Version"]:=A_YYYY A_MM A_DD "-1"      ;版本日期设置
ahkIni.save()

;{{常用变量值初始化
valueindex:=Cut_Mode?2:1
waitnum:=0
num__:=""
recent:={}
select_sym:=0
;}}

;返回当前字体在字体列表中的位置，显示在样式面板
Font_Pos(){
	global a_FontList,FontType
	loop,parse,a_FontList,"|"
		if (A_LoopField = FontType)
			Return A_index
}
a_FontNow :=Font_Pos()
WM_RBUTTONDOWN(){
	If (A_Gui=3){
		Menu, TRAY, Show, x%Logo_X%, y%Logo_Y%
		;Menu, TRAY, Show, %A_CaretX%, %A_CaretY%
}}

;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)
	}
}

#Include ..\config\Label.ahk
#Include ..\config\Shortcut.ahk
#Include ..\config\SQL_Function.ahk
#Include ..\config\GdipText.ahk