/**
 * \file intl.h
 * This file is part of LyX, the document processor.
 * Licence details can be found in the file COPYING.
 *
 * \author unknown
 * \author Lars Gullik Bjønnes
 * \author Angus Leeming
 * \author John Levon
 *
 * Full author contact details are available in file CREDITS.
 */

#include <config.h>

#include "intl.h"
#include "debug.h"
#include "lyxrc.h"


using std::endl;


Intl::Intl()
	: keymap(Intl::PRIMARY), keymapon(lyxrc.use_kbmap),
	prim_lang(lyxrc.primary_kbmap), sec_lang(lyxrc.secondary_kbmap)
{
}


void Intl::KeyMapOn(bool on)
{
	keymapon = on;

	if (on) {
		if (keymap == PRIMARY)
			KeyMapPrim();
		else
			KeyMapSec();
	} else
		trans.DisableKeymap();
}


void Intl::ToggleKeyMap()
{
	if (keymapon && (keymap == PRIMARY)) {
		KeyMapSec();
	} else if (keymapon) {
		KeyMapOn(false);
	} else
		KeyMapPrim();
}


void Intl::KeyMapPrim()
{
	if (!trans.SetPrimary(prim_lang))
		trans.EnablePrimary();

	keymapon = true;
	keymap = PRIMARY;
}


void Intl::KeyMapSec()
{
	if (!trans.SetSecondary(sec_lang))
		trans.EnableSecondary();

	keymapon = true;
	keymap = SECONDARY;
}


void Intl::InitKeyMapper(bool on)
{
	lyxerr[Debug::INIT] << "Initializing key mappings..." << endl;

	if (trans.SetPrimary(prim_lang) == -1)
		prim_lang.erase();
	if (trans.SetSecondary(sec_lang) == -1)
		sec_lang.erase();
	trans.setCharset(lyxrc.font_norm);

	if (prim_lang.empty() && sec_lang.empty())
		keymapon = false;
	else
		keymapon = on;

	KeyMapOn(keymapon);

	if (keymapon)
		KeyMapPrim();
}
