/*
###
### This file is part of
###
###                        TurboLinux  ZWinPro
###
###                 Copyright (C) 1999-2000 TurboLinux, Inc. 
###                        All Rights Reserved
### Distributed under the terms of the GNU General Public License (GPL)
###
###
### Authors:     TurboLinux Chinese Development Team:
###              Justin Yu   <justiny@turbolinux.com.cn>
###              Sean Chen   <seanc@turbolinux.com.cn>
###              Daniel Fang <danf@turbolinux.com.cn>
### WWW:         http://www.turbolinux.com.cn/ZWinPro/
### FTP:         ftp://ftp.turbolinux.com.cn/pub/ZWinPro/
###
*/

#include "all.h"

void chinput_set_2byte (int byte_flag)
{
	// Use a macro instead of '1'
	flag_corner = (byte_flag ? True : False);

	if (map_mode == 0) return;

	if (dmode == HZSERVER_DMODE_ROOT)
	{
		chinput_root_corner_flush ();
	}
	else if (dmode == HZSERVER_DMODE_ONSPOT)
	{
		chinput_onspot_flush ();
	}
	else if (dmode == HZSERVER_DMODE_OFFSPOT)
	{
		chinput_offspot_flush ();
	}
	else if (dmode == HZSERVER_DMODE_OVERSPOT)
	{
		chinput_overspot_flush();
	}
}

void chinput_set_punct (int punct)
{
	flag_punct = (punct ? True : False);

	if (map_mode == 0)
		return;

	if (dmode == HZSERVER_DMODE_ROOT)
	{
		chinput_root_punct_flush ();
	}
	else if (dmode == HZSERVER_DMODE_ONSPOT)
	{
		chinput_onspot_flush ();
	}
	else if (dmode == HZSERVER_DMODE_OFFSPOT)
	{
		chinput_offspot_flush ();
	}
	else if (dmode == HZSERVER_DMODE_OVERSPOT)
	{
		chinput_overspot_flush();
	}
}

void chinput_popup_window (void)
{
	map_mode = 1;
    flag_tip = 1;

	chinput_set_mode (dmode);

	if (dmode == HZSERVER_DMODE_ROOT)
	{
		XMapRaised(display, window1);
		chinput_root_flush();
	}
	else if (dmode == HZSERVER_DMODE_OVERSPOT)
	{
		XMapRaised(display, window1);
		chinput_overspot_flush ();
	}
	else if (dmode == HZSERVER_DMODE_ONSPOT)
	{
		chinput_onspot_flush ();
	}
	else if (dmode == HZSERVER_DMODE_OFFSPOT)
	{
		chinput_offspot_flush ();
	}
//	else if(dmode == HZSERVER_DMODE_HW)
//	{
//		XMapRaised(display, window6);
//		XMapRaised(display, window5);
//		XMapRaised(display, window4);
//	}

	if (HZServer.hzVKwin.onflag)
	{
		XMapRaised(display, window3);
	}

	XFlush (display);
//	chinput_dock_change_notify ();
}

void chinput_hide_window (void)
{
	map_mode = 0;

	if (dmode == HZSERVER_DMODE_ROOT)
	{
		XUnmapWindow(display, window1);
		XUnmapWindow(display, window2);
	}
	else if (dmode == HZSERVER_DMODE_OVERSPOT)
	{
		XUnmapWindow(display, window1);	
		XUnmapWindow(display, window2);
		XLowerWindow(display, window1);	
		XLowerWindow(display, window2);	
		chinput_overspot_hide_status_window ();
	}
	else if (dmode == HZSERVER_DMODE_ONSPOT)
	{
		XUnmapWindow(display, window1);
		XUnmapWindow(display, window2); //if any
		chinput_onspot_clear ();
	}
	else if (dmode == HZSERVER_DMODE_OFFSPOT)
	{
		chinput_offspot_hide_windows ();
	}
//	else if(dmode == HZSERVER_DMODE_HW)
//	{
//		XUnmapWindow(display, window6);
//		XUnmapWindow(display, window5);
//		XUnmapWindow(display, window4);
//		XUnmapWindow(display, window3);
//		XUnmapWindow(display, window2);
//		XUnmapWindow(display, window1);
//	}

	if (HZServer.hzVKwin.onflag)
	{
		XUnmapWindow(display, window3);
	}
    if (!strcmp(chinputime[cur_inputmethod].name,"ZNPY"))
	{
        IMM_FlushUserPhrase(chinput_imm);
	}

	XFlush (display);

//	chinput_dock_change_notify ();
}

void chinput_toggle_window_map (void)
{
	if (map_mode == 1)
	{
		chinput_hide_window ();
	}
	else
	{
		if (cur_inputmethod == -1)
		{
			switch (HZServer.encoding)
			{
				case HZSERVER_ENCODING_GB2312:
					chinput_switch_inputmethod
						(chinput_get_inputmethod (input_method_gb2312));
					break;
				case HZSERVER_ENCODING_GB18030:
					chinput_switch_inputmethod
						(chinput_get_inputmethod (input_method_gb18030));
					break;
				case HZSERVER_ENCODING_BIG5:
					chinput_switch_inputmethod
						(chinput_get_inputmethod (input_method_big5));
					break;
				case HZSERVER_ENCODING_BIG5HKSCS:
					chinput_switch_inputmethod
						(chinput_get_inputmethod (input_method_big5hkscs));
					break;
				default:
					break;
			}
		}
		chinput_popup_window ();
	}
}

void chinput_switch_style (void)
{
	if (map_mode == 0)
		return;
	
	if (dmode == HZSERVER_DMODE_OVERSPOT)
	{
		dmode = HZSERVER_DMODE_ROOT;
		XUnmapWindow (display, window2);
		XResizeWindow (display, window1,
				HZServer.hzIwin.width + HZServer.hzBwin.width,
				HZServer.hzIwin.height + 
				((HZServer.hzKwin.onflag)?HZServer.hzKwin.height : 0));
	}
	else if (dmode == HZSERVER_DMODE_ROOT)
	{
		dmode = HZSERVER_DMODE_ONSPOT;
		XUnmapWindow (display, window2);
		XUnmapWindow (display, window1);
	}
	else if(dmode == HZSERVER_DMODE_ONSPOT)
	{
		dmode = HZSERVER_DMODE_OFFSPOT;
	}
	else if(dmode == HZSERVER_DMODE_OFFSPOT)
	{
		dmode = HZSERVER_DMODE_OVERSPOT;
		XResizeWindow (display, window1, 
				HZSERVER_INPUT_WIN16_WIDTH,
				HZSERVER_INPUT_WIN16_HEIGHT);
		XMapRaised (display, window1);
	}
}

void chinput_set_mode (int m)
{
	dmode = m;
	if (map_mode == 0)
		return;

	if (dmode == HZSERVER_DMODE_ROOT)
	{
		XUnmapWindow (display, window3);
		XUnmapWindow (display, window2);
		XResizeWindow (display, window1,
			HZServer.hzIwin.width + HZServer.hzBwin.width,
			HZServer.hzIwin.height + 
			((HZServer.hzKwin.onflag)?HZServer.hzKwin.height : 0));
//		XMapRaised (display, window1);
	}
	else if (dmode == HZSERVER_DMODE_ONSPOT)
	{
		XUnmapWindow (display, window3);
		XUnmapWindow (display, window2);
		XUnmapWindow (display, window1);
	}
	else if (dmode == HZSERVER_DMODE_OFFSPOT)
	{
		XUnmapWindow (display, window3);
		XUnmapWindow (display, window2);
		XUnmapWindow (display, window1);
		//popup
//		chinput_offspot_map_windows ();
	}
	else if (dmode == HZSERVER_DMODE_OVERSPOT)
	{
		XUnmapWindow (display, window3);
		XResizeWindow (display, window1, 
			HZSERVER_INPUT_WIN16_WIDTH,
			HZSERVER_INPUT_WIN16_HEIGHT);
//		XMapRaised (display, window1);
	}
}

int chinput_get_inputmethod (char *im_name)
{
	int i;
	if (HZServer.encoding == HZSERVER_ENCODING_GB2312)
	{
		for (i=0; i<index_ime; i++)
		{
			if(!strcmp(chinputime[i].name, im_name) && 
				   !strcmp(chinputime[i].encoding, "GB2312"))
			{
				return i;
			}
		}
	}
	if (HZServer.encoding == HZSERVER_ENCODING_GB18030)
	{
		for (i=0; i<index_ime; i++)
		{
			if(!strcmp(chinputime[i].name, im_name) && 
				   !strcmp(chinputime[i].encoding, "GB18030"))
			{
				return i;
			}
		}
	}
	else if (HZServer.encoding == HZSERVER_ENCODING_BIG5)
	{
		for (i=0; i<index_ime; i++)
		{
			if (!strcmp(chinputime[i].name, im_name) && 
				   !strcmp(chinputime[i].encoding, "BIG5"))
			{
				return i;
			}
		}
	}
	else if (HZServer.encoding == HZSERVER_ENCODING_BIG5HKSCS)
	{
		for (i=0; i<index_ime; i++)
		{
			if (!strcmp(chinputime[i].name, im_name) && 
				   !strcmp(chinputime[i].encoding, "BIG5HKSCS"))
			{
				return i;
			}
		}
	}
	return 0;
}

void chinput_load_inputmethod (int id)
{
	char fname[256];
	long mode = IMM_DOUBLE_BYTE_MODE;

	//printf ("chinput_load_inputmethod:\n");
	//We allow absolute path for input method
	if (chinputime[id].fname[0] == '/')
		strcpy (fname, chinputime[id].fname);
	else
		sprintf (fname, "%s/%s", dict_path, chinputime[id].fname);
//	printf (">> To load: %s\n", fname);

//	printf ("chinput_load_inputmethod >> calling IMM_OpenInput\n");

	if (chinput_imm)
		IMM_CloseInput(chinput_imm);
	if (HZServer.encoding == HZSERVER_ENCODING_GB2312)
		chinput_imm = IMM_OpenInput (chinput_imserver, 
				chinputime[id].module, fname, IMM_LC_GB2312);
	if (HZServer.encoding == HZSERVER_ENCODING_GB18030)
		chinput_imm = IMM_OpenInput (chinput_imserver, 
				chinputime[id].module, fname, IMM_LC_GB18030);
	else if (HZServer.encoding == HZSERVER_ENCODING_BIG5)
		chinput_imm = IMM_OpenInput (chinput_imserver, 
				chinputime[id].module, fname, IMM_LC_BIG5);
	else if (HZServer.encoding == HZSERVER_ENCODING_BIG5HKSCS)
		chinput_imm = IMM_OpenInput (chinput_imserver, 
				chinputime[id].module, fname, IMM_LC_BIG5HKSCS);

	if (!chinput_imm)
		chinput_util_err_exit ();

	IMM_SetInputMode (chinput_imm, mode);

	IMM_ConfigInputArea (chinput_imm, width_inputbar);
	chinput_root_resize_window (width_inputbar);

	if (chinput_imm == NULL)
	{
		//printf ("Cannot load input method: %s\n", fname);
		chinput_util_exit ();
	}
		
	// Alert Docking?
//	chinput_dock_change_notify ();
}

void chinput_switch_inputmethod (int id)
{
	flag_refresh = 1;

//	printf ("chinput_switch_inputmethod...\n");

	// Fallback to a default
	if (id == -1)
		id = 1;

	// If requested IM is different from the current state, load the requested IM
	if (id != cur_inputmethod)
	{
		cur_inputmethod = id;
		if (!strcmp(chinputime[cur_inputmethod].encoding, "GB2312"))
			HZServer.encoding = HZSERVER_ENCODING_GB2312;
		else if (!strcmp(chinputime[cur_inputmethod].encoding, "GB18030"))
			HZServer.encoding = HZSERVER_ENCODING_GB18030;
		else if (!strcmp(chinputime[cur_inputmethod].encoding, "BIG5"))
			HZServer.encoding = HZSERVER_ENCODING_BIG5;
		else if (!strcmp(chinputime[cur_inputmethod].encoding, "BIG5HKSCS"))
			HZServer.encoding = HZSERVER_ENCODING_BIG5HKSCS;
		
//		printf ("chinput_switch_input:\n");
//		printf (">> HZServer.encoding is %d\n", HZServer.encoding);
		chinput_load_inputmethod (cur_inputmethod);
	}

	// Refresh display
	if (dmode == HZSERVER_DMODE_ROOT)
	{
		chinput_root_draw_panel ();
		chinput_root_input_flush ();
		chinput_root_draw_input_area ("", 0);
		if (HZServer.hzKwin.onflag)
		{
			chinput_root_ka_draw_panel ();
			chinput_root_ka_draw_button();
			chinput_root_ka_draw_window ();
		}
	}
	else if (dmode == HZSERVER_DMODE_OVERSPOT)
	{
		chinput_overspot_flush ();
	}
	else if (dmode == HZSERVER_DMODE_ONSPOT)
	{
		chinput_onspot_flush  ();
	}
	else if (dmode == HZSERVER_DMODE_OFFSPOT)
	{
		chinput_offspot_flush  ();
	}
	// Alert Docking??
}

void chinput_circulate_inputmethod (int direction)
{
	int id = cur_inputmethod;

	do
	{
		id += direction;

		if (id == index_ime)
			id = 0;

		if (id == -1)
			id = index_ime - 1;
	}
	while (!chinput_util_file_exist(chinputime[id].fname));

	chinput_switch_inputmethod (id);
}
