Include "HjGlobal.jsh" ; default HJ global variables
Include "hjconst.jsh" ; default HJ constants
Include "common.jsm" ; message file
include "MyHomeLib.jsh"
include "MyHomeLib.jsm"

Globals
int giCurrentView,
int giCurrentKey,
int vkladka,
string sRealWindowClass

Script AlfavitRus ()
var
string s,
int x,
int y

SaveCursor () JAWSCursor ()
FindString (GetAppMainWindow (GetCurrentWindow ()), "А-Я", s_top, s_unrestricted)
SayChunk ()
let x=GetCursorCol ()
let y=GetCursorRow ()
RestoreCursor ()
InputBox ("от а до я", "введите букву", s)
if s < "а"  || s > "я" || StringLength (s) !=1 then 
Say (msgWrongSimbol,OT_ERROR)
return
EndIf
SaveCursor ()
JAWSCursor ()
MoveTo (x, y)
if FindString (GetAppMainWindow (GetCurrentWindow ()), s, s_Next, s_unrestricted) then
LeftMouseButton ()
SayChunk ()
EndIf
RestoreCursor ()
EndScript

;количество просканированых архивов.
Script TotalArhifs ()
if GetWindowClass (GetParent (GetFocus ())) == "TImportProgressFormEx" then
if FindString (GetCurrentWindow (), "Обработано архивов:", s_top, s_unrestricted) then
JAWSCursor ()
SayChunk ()
PCCursor ()
EndIf
EndIf
EndScript

Script AlfavitEnu ()
var
string s,
int x,
int y

SaveCursor () JAWSCursor ()
FindString (GetAppMainWindow (GetCurrentWindow ()), "A-Z", s_top, s_unrestricted)
NextWord ()
NextWord ()
let x=GetCursorCol () let y=GetCursorRow ()
RestoreCursor ()
InputBox ("от A до Z", "введите букву", s)
if s < "a"  || s > "z" || StringLength (s) !=1 then 
Say (msgWrongSimbol,OT_ERROR)
return
EndIf

SaveCursor () JAWSCursor ()
MoveTo (x, y)
if FindString (GetAppMainWindow (GetCurrentWindow ()), s, s_Next, s_unrestricted) then
LeftMouseButton ()
SayChunk ()
EndIf
RestoreCursor ()
EndScript
	
Script HotKeyHelp ()
UserBufferClear ()
SayFormattedMessage (OT_USER_BUFFER, msgHelp) 
EndScript


;Void Function AutoStartEvent ()
;SayTab (0)
;EndFunction

Void Function MenuModeEvent (handle WinHandle, int mode)
let giCurrentView=0
MenuModeEvent (WinHandle, mode)
EndFunction

Void Function FocusChangedEvent (handle FocusWindow, handle PrevWindow)
var
	int PrevView,
	string sClass

let giCurrentView=0
;Pause ()
;SpeechOn ()
SayTab (0)
;if FocusWindow == PrevWindow && !DialogActive() then
;	return
;EndIf
let sRealWindowClass=GetWindowClass (GetRealWindow (FocusWindow))
let sClass=GetWindowClass (FocusWindow )
if sClass == "TRzPageControl" then
	let giCurrentView=tab
	SayTab (1)
EndIf
;SayString (sClass)
if sClass == "TRzEdit" then
	let giCurrentView=edit
EndIf
if vkladka == MHL_autors || vkladka == MHL_series then
	if sClass == "TVirtualStringTree" then
		if GetPriorWindow (FocusWindow) then
			let giCurrentView=tray1
			else
			let giCurrentView=tray2
			;			PerformScript File()
			if not GetWindowTextEx (FocusWindow , 1, 1) then
			TypeKey (cksHome)
			EndIf
			EndIf
	EndIf
EndIf
if vkladka == MHL_janrs then
	if sClass == "TVirtualStringTree" && GetWindowClass (GetParent (GetParent (FocusWindow))) == "TRzSplitterPane" then
		let giCurrentView=tray3
	elif sClass == "TVirtualStringTree" && GetWindowClass (GetPriorWindow (GetPriorWindow (GetParent (FocusWindow)))) == "TMHLInfoPanel" then
		let giCurrentView=tray4
			if not GetWindowTextEx (FocusWindow , 1, 1) then
			TypeKey (cksHome)
			EndIf
;		 if GetInfo (1,msgF1,0) != msgWrong1 then
;			Say (GetInfo (1,msgF1,0),OT_SELECT)
;		EndIf
	EndIf
	elif sClass == "TRzButtonEdit" then
		let giCurrentView=FindEdit
		SaveCursor ()
		InvisibleCursor ()
		RouteInvisibleToPC ()
		SpeechOff ()
		NextLine ()
		;		SayLine ()
		JAWSEnd ()
		SpeechOn ()
		SayChunk ()
		RestoreCursor ()
EndIf

;SayInteger (giCurrentView)
FocusChangedEvent(FocusWindow, PrevWindow)
EndFunction

Void Function NewTextEvent (handle hwnd, string buffer, int nAttributes, int nTextColor, int nBackgroundColor, int nEcho, string sFrameName)
var
string sClass

let sClass=GetWindowClass (hwnd) 

if sClass == "TPanel" 
|| sClass == "TVirtualStringTree"
then
return
EndIf
if sClass == "TRzPanel" || sClass == "TRzStatusBar" then
SayString (buffer)
return
EndIf
;SayString (GetWindowClass (hwnd))
;SayString (buffer)

EndFunction

Void Function KeyPressedEvent (int nKey, string strKeyName, int nIsBrailleKey, int nIsScriptKey)
var
string s1,
int s2,
string s3


if giCurrentView == tray1 || giCurrentView == tray2 || giCurrentView == tray3 then
;KeyboardLanguageChangedEvent(s1, s2, s3)
;SayString (gstrKeyboardLayout )




;if nKey > 15 && nKey <28 || 
;nKey > 29 && nKey <41 ||
;nKey > 43 && nKey <53 
;then
if StringLength (strKeyName ) == 1 && (strKeyName >= "A" && strKeyName <= "Z" ||strKeyName >= "a" && strKeyName <= "z") then
if GetCharacter ()  == strKeyName then
EndIf

;JAWSHome ()
;While GetCharacter ()  != strKeyName 
;NextLine ()
;EndWhile
EndIf


EndIf
;SayInteger (nKey)
let giCurrentKey=nKey
KeyPressedEvent (nKey, strKeyName, nIsBrailleKey, nIsScriptKey)
EndFunction

Script SayLine ()
;SayInteger (vkladka)
;SayInteger (giCurrentView)
var
string class

if UserBufferIsActive () then
SayLine ()
return
EndIf

If  GetWindowClass (GetFocus ()) != "TfrmMain" && IsPCCursor () then
let class=GetWindowClass (GetFocus ())
if class == "TMemo" then
let giCurrentView=0
	Say (GetWindowText (GetCurrentWindow (), 0),	OT_SELECTED_ITEM)
	return
EndIf
if giCurrentView== tray1 then
	sayObjectActiveItem()
	return
elif giCurrentView== edit then
	sayObjectActiveItem(1)
return
elif giCurrentView== tray2 then
	SayTree2 (1)
	return
elif giCurrentView== tray3 then
sayObjectActiveItem()
return
elif giCurrentView==tray4 then
return SayObjectActiveItem (1)
EndIf
EndIf
SpeechOn ()
;TypeCurrentScriptKey ()
SayLine ()
EndScript


Script SayNextCharacter ()
;SayInteger (giCurrentView)
if IsPCCursor () then
If  giCurrentView== tab && GetWindowClass (GetFocus ()) != "TfrmMain" && IsPCCursor () then
SpeechOff ()
NextCharacter ()
Pause ()
	SayTab (1)
return
elif giCurrentView==tray3 then
NextCharacter ()
Pause ()
			return ;SayObjectActiveItem ()
EndIf
EndIf
PerformScript SayNextCharacter()
EndScript

Script SayPriorCharacter ()
if IsPCCursor () then
If  giCurrentView== tab && GetWindowClass (GetFocus ()) != "TfrmMain" && IsPCCursor () then
SpeechOff ()
PriorCharacter ()
Pause ()
SayTab (1)
EndIf
EndIf
PerformScript SayPriorCharacter()
EndScript

String Function SayTab (int say)
var
string s

Pause ()
let s=StringSegment (GetWindowText (FindWindow (GetAppMainWindow (GetFocus ()), "TRzPageControl", ""), 1), " ",1)
if s == "Список" then
let s=FormatString ("%1%2", s, " закачек")
EndIf
let s=FormatString ("%1%2", s, " вкладка")
if say then
Say (s, OT_SELECTED_ITEM)
EndIf

if s=="Авторы" then
let vkladka=MHL_autors
elif s=="Серии" then
let vkladka=MHL_series
elif s=="Жанры" then
let vkladka=MHL_janrs
elif s=="Поиск" then
let vkladka=MHL_poisk
elif s == "Группы" then
let vkladka=MHL_groops
elif s == "Список" then
let vkladka=MHL_Spisok
EndIf
EndFunction

Int Function SayTree2 (int Movement)
var
handle hand,
int restrict,
string FinalString


let hand=FindWindow (GetAppMainWindow (GetFocus ()), "TMHLInfoPanel", "")
if hand then
SaveCursor ()
InvisibleCursor ()
MoveToWindow (hand)
let restrict=GetRestriction ()
SetRestriction (3)
NextLine ()
let FinalString=GetLine ()
if Movement then
RestoreCursor ()
if StringCompare (FinalString, strFolder, 0) == 0 then
Say (strFolder, OT_SELECT)
EndIf
SayObjectActiveItem ()
return
EndIf
if GetTreeViewLevel () == 0 && StringCompare (GetLine (), strFolder, 0) == 0 then
let FinalString=FinalString+GetSelectedText ()
EndIf
SetRestriction (restrict)
RestoreCursor ()
Say (FinalString, OT_SELECT)
return
EndIf
EndFunction

Void Function SayLineUnit (int unitMovement, int bMoved)
if IsPCCursor () && not UserBufferIsActive () && not MenusActive () then
	If  giCurrentView== edit then
	SayLine ()	
	return
	EndIf
	if giCurrentView==tray1 || giCurrentView==tray2 || giCurrentView==tray4 then
		SpeechOff ()
SayLineUnit (unitMovement, bMoved)
		Pause ()
		SpeechOn ()
	if giCurrentView==tray1 then
		return	 SayObjectActiveItem (0)
	elif giCurrentView==tray2 then
		return SayTree2 (0)
	elif giCurrentView==tray4 then
		return SayObjectActiveItem (0)
	EndIf
	EndIf
EndIf
SayLineUnit (unitMovement, bMoved)
EndFunction


Script jarn ()
Say (GetInfo (2,"",0),OT_SELECT)
EndScript

Script File ()
Say (GetInfo (1,msgF1,1),OT_SELECT)
EndScript

Script avtor ()
Say (GetInfo (0,msgF0,1),OT_SELECT)
EndScript

Script folder ()
Say (GetInfo (3,"",0),OT_SELECT)
EndScript

String Function GetInfo (int strok, string stroka, int type)
var
handle hand,
int restrict,
string s

let hand=FindWindow (GetAppMainWindow (GetFocus ()), "TMHLInfoPanel", "")
if hand then
SaveCursor ()
MoveToWindow (hand)
let restrict=GetRestriction ()
SetRestriction (3)
let s=GetChunk ()
if s == msgJanr then
SetRestriction (restrict)
RestoreCursor ()
return ;msgWrong1
EndIf
While strok >0
NextLine ()
let strok=strok-1
EndWhile
if type then
let stroka=stroka+GetLine ()
else
let stroka=GetLine ()
EndIf
SetRestriction (restrict)
RestoreCursor ()
EndIf
return stroka
EndFunction

Script NextDocumentWindow ()
if StringCompare (GetWindowClass (GetRealWindow (GetCurrentWindow ())), "TfrmMain", 0) == 0 then
SpeechOff ()
TypeKey(cksControlTab)
Pause ()
SpeechOn ()
SayTab (1)
return
EndIf

PerformScript NextDocumentWindow()
EndScript

Script PreviousDocumentWindow ()
if StringCompare (GetWindowClass (GetRealWindow (GetCurrentWindow ())), "TfrmMain", 0) == 0 then
SpeechOff ()
TypeKey(cksControlShiftTab)
Pause ()
SpeechOn ()
SayTab (1)
return
EndIf
PerformScript PreviousDocumentWindow ()
EndScript

Script Izbrannoe ()
{Control+D}
Pause ()
Say (msgAdd,	OT_MESSAGE)
PerformScript jarn()
PerformScript avtor()
PerformScript File()
EndScript


