#
# Copyright (C) 2014 Fulvio Benini
#
# This file is part of Scid (Shane's Chess Information Database).
# Scid 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.


# CUSTOMIZATION:
# By editing this file you can customize the keyboard shortcuts,
# in order to best suit your preferences.
# It is not necessary to recompile scid after changing this file.

proc keyboardShortcuts {w} {
	# Go back/forward
	bind $w <Left>  { excludeTextWidget %W; ::move::Back }
	bind $w <Right> { excludeTextWidget %W; ::move::Forward }
	bind $w <Up>    { excludeTextWidget %W; ::move::ExitVariationToMainline }
	bind $w <Down>  { excludeTextWidget %W; ::move::EnterFirstVariation }
	bind $w <Home>  { excludeTextWidget %W; ::move::ExitVarOrStart }
	bind $w <End>   { excludeTextWidget %W; ::move::End }

	# Load game
	bind $w <Control-Up>   { excludeTextWidget %W; ::game::LoadNextPrev previous }
	bind $w <Control-Down> { excludeTextWidget %W; ::game::LoadNextPrev next }
	bind $w <Control-question> { excludeTextWidget %W; ::game::LoadRandom }

	# Rotate the chess board
	bind $w <period> { excludeTextWidget %W; toggleRotateBoard }

	# Open "Setup Board" dialog
	bind $w <s> { excludeTextWidget %W; ::setupBoard }

	# Open a database
	bind $w <Control-o> { ::file::Open }

	# Close the current database
	# TODO: is better to use control-w to close the focused window?
	bind $w <Control-w> { ::file::Close }

	# Undo
	bind $w <Control-z> { undoFeature undo }

	# Redo
	bind $w <Control-y> { undoFeature redo }

	# New game
	bind $w <Control-n> { ::game::Clear }

	# Save current game
	bind $w <Control-s> { ::gameReplace }

	# Save current game as new
	bind $w <Control-S> { ::gameAdd }

	# Toggle fullscreen
	bind $w <F11> { wm attributes . -fullscreen [expr ![wm attributes . -fullscreen]] }

	# Open the enter/create variation dialog
	# TODO: <v> is not intuitive: <space> or <up> <down> may be better
	bind $w <KeyPress-v> {
		excludeTextWidget %W
		::showVars
	}

	# Change current database
	set totalBaseSlots [sc_info limit bases]
	for {set i 1} { $i <= $totalBaseSlots} {incr i} {
		bind $w <Control-Key-$i> "::file::SwitchToBase $i"
	}

	# Open the help window
	bind $w <F1> { helpWindowPertinent %W }

	# Engines
	bind $w <F2> "::enginewin::toggleStartStop 1"
	bind $w <F3> "::enginewin::toggleStartStop 2"

	# Toggle the active window between docked/undocked
	bind $w <F9> { ::win::toggleDocked %W }

	#TODO: to be checked
	bind $w <F6>	::book::open
	bind $w <Control-d> ::windows::switcher::Open
	bind $w <Control-e> "::makeCommentWin toggle"
	bind $w <Control-i> ::windows::stats::Open
	bind $w <Control-l> ::windows::gamelist::Open
	bind $w <Control-m> ::maint::OpenClose
	bind $w <Control-p> ::pgn::OpenClose
	bind $w <Control-t> ::tree::make
	bind $w <Control-E> ::windows::eco::OpenClose
	bind $w <Control-K> ::ptrack::make
	bind $w <Control-O> ::optable::makeReportWin
	bind $w <Control-P> ::plist::toggle
	bind $w <Control-T> ::tourney::toggle
	bind $w <Control-X> ::crosstab::Open


	#TODO: to be improved
	bind $w <Control-a> {
		excludeTextWidget %W
		sc_var create
		::notify::PosChanged -pgn
	}

	#TODO: are these shortcuts useful?
	bind $w <Control-B> ::search::board
	bind $w <Control-H> ::search::header
	bind $w <Control-M> ::search::material
	bind $w <Control-KeyPress-U> ::search:::usefile

	bind $w <Control-C> ::copyFEN
	bind $w <Control-V> ::pasteFEN
	bind $w <Control-I> importPgnGame
	bind $w <Control-D> {sc_move ply [sc_eco game ply]; updateBoard}
	bind $w <Control-G> tools::graphs::filter::Open
	bind $w <Control-J> tools::graphs::absfilter::Open
	bind $w <Control-u> ::game::GotoMoveNumber
	bind $w <Control-Y> findNovelty
	bind $w <Control-N> nameEditor
}

proc excludeTextWidget {w} {
	if { [regexp ".*(Entry|Text|Combobox|Spinbox)" [winfo class $w] ] } {
		# HACK: enable binding for .pgnWin
		# TODO: replace this using the new %M (available since Tk 8.6.4)
		if {$w ne ".pgnWin.text"} {
			return -code continue
		}
	}
}
