imThaiIc.c revision 0f8248bf
1/******************************************************************
2
3          Copyright 1992, 1993, 1994 by FUJITSU LIMITED
4          Copyright 1993 by Digital Equipment Corporation
5
6Permission to use, copy, modify, distribute, and sell this software
7and its documentation for any purpose is hereby granted without fee,
8provided that the above copyright notice appear in all copies and that
9both that copyright notice and this permission notice appear in
10supporting documentation, and that the name of FUJITSU LIMITED and
11Digital Equipment Corporation not be used in advertising or publicity
12pertaining to distribution of the software without specific, written
13prior permission.  FUJITSU LIMITED and Digital Equipment Corporation
14makes no representations about the suitability of this software for
15any purpose.  It is provided "as is" without express or implied
16warranty.
17
18FUJITSU LIMITED AND DIGITAL EQUIPMENT CORPORATION DISCLAIM ALL
19WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
20WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
21FUJITSU LIMITED AND DIGITAL EQUIPMENT CORPORATION BE LIABLE FOR
22ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
23WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
24IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
25ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
26THIS SOFTWARE.
27
28  Author:    Takashi Fujiwara     FUJITSU LIMITED
29                               	  fujiwara@a80.tech.yk.fujitsu.co.jp
30  Modifier:  Franky Ling          Digital Equipment Corporation
31	                          frankyling@hgrd01.enet.dec.com
32
33******************************************************************/
34
35#ifdef HAVE_CONFIG_H
36#include <config.h>
37#endif
38#include <stdio.h>
39#include <X11/Xlib.h>
40#include <X11/Xmd.h>
41#include "Xlibint.h"
42#include "Xlcint.h"
43#include "Ximint.h"
44
45static void
46_XimThaiUnSetFocus(
47    XIC	 xic)
48{
49    Xic  ic = (Xic)xic;
50    ((Xim)ic->core.im)->private.local.current_ic = (XIC)NULL;
51
52    if (ic->core.focus_window)
53	_XUnregisterFilter(ic->core.im->core.display, ic->core.focus_window,
54			_XimThaiFilter, (XPointer)ic);
55    return;
56}
57
58static void
59_XimThaiDestroyIC(
60    XIC	 xic)
61{
62    Xic	 ic = (Xic)xic;
63    DefTreeBase *b = &ic->private.local.base;
64
65    if(((Xim)ic->core.im)->private.local.current_ic == (XIC)ic) {
66	_XimThaiUnSetFocus(xic);
67    }
68
69    Xfree(ic->private.local.ic_resources);
70    ic->private.local.ic_resources = NULL;
71
72    Xfree (b->tree);
73    b->tree = NULL;
74
75    Xfree (b->mb);
76    b->mb   = NULL;
77
78    Xfree (b->wc);
79    b->wc   = NULL;
80
81    Xfree (b->utf8);
82    b->utf8 = NULL;
83    return;
84}
85
86static void
87_XimThaiSetFocus(
88    XIC	 xic)
89{
90    Xic	 ic = (Xic)xic;
91    XIC	 current_ic = ((Xim)ic->core.im)->private.local.current_ic;
92
93    if (current_ic == (XIC)ic)
94	return;
95
96    if (current_ic != (XIC)NULL) {
97	_XimThaiUnSetFocus(current_ic);
98    }
99    ((Xim)ic->core.im)->private.local.current_ic = (XIC)ic;
100
101    if (ic->core.focus_window)
102	_XRegisterFilterByType(ic->core.im->core.display, ic->core.focus_window,
103			KeyPress, KeyPress, _XimThaiFilter, (XPointer)ic);
104    return;
105}
106
107static void
108_XimThaiReset(
109    XIC	 xic)
110{
111    Xic	 ic = (Xic)xic;
112    DefTreeBase *b   = &ic->private.local.base;
113    ic->private.local.thai.comp_state = 0;
114    ic->private.local.thai.keysym = 0;
115    b->mb[b->tree[ic->private.local.composed].mb] = '\0';
116    b->wc[b->tree[ic->private.local.composed].wc] = '\0';
117    b->utf8[b->tree[ic->private.local.composed].utf8] = '\0';
118}
119
120static char *
121_XimThaiMbReset(
122    XIC	 xic)
123{
124    _XimThaiReset(xic);
125    return (char *)NULL;
126}
127
128static wchar_t *
129_XimThaiWcReset(
130    XIC	 xic)
131{
132    _XimThaiReset(xic);
133    return (wchar_t *)NULL;
134}
135
136static XICMethodsRec Thai_ic_methods = {
137    _XimThaiDestroyIC, 	/* destroy */
138    _XimThaiSetFocus,  	/* set_focus */
139    _XimThaiUnSetFocus,	/* unset_focus */
140    _XimLocalSetICValues,	/* set_values */
141    _XimLocalGetICValues,	/* get_values */
142    _XimThaiMbReset,		/* mb_reset */
143    _XimThaiWcReset,		/* wc_reset */
144    _XimThaiMbReset,		/* utf8_reset */
145    _XimLocalMbLookupString,	/* mb_lookup_string */
146    _XimLocalWcLookupString,	/* wc_lookup_string */
147    _XimLocalUtf8LookupString	/* utf8_lookup_string */
148};
149
150XIC
151_XimThaiCreateIC(
152    XIM			 im,
153    XIMArg		*values)
154{
155    Xic			 ic;
156    XimDefICValues	 ic_values;
157    XIMResourceList	 res;
158    unsigned int	 num;
159    int			 len;
160    DefTree             *tree;
161
162    if((ic = Xcalloc(1, sizeof(XicRec))) == (Xic)NULL) {
163	return ((XIC)NULL);
164    }
165
166    ic->methods = &Thai_ic_methods;
167    ic->core.im = im;
168    ic->core.filter_events = KeyPressMask;
169
170    if (! (ic->private.local.base.tree = tree = Xmalloc(sizeof(DefTree)*3)) )
171	goto Set_Error;
172    if (! (ic->private.local.base.mb = Xmalloc(21)) )
173	goto Set_Error;
174    if (! (ic->private.local.base.wc = Xmalloc(sizeof(wchar_t)*21)) )
175	goto Set_Error;
176    if (! (ic->private.local.base.utf8 = Xmalloc(21)) )
177	goto Set_Error;
178    ic->private.local.context = 1;
179    tree[1].mb   = 1;
180    tree[1].wc   = 1;
181    tree[1].utf8 = 1;
182    ic->private.local.composed = 2;
183    tree[2].mb   = 11;
184    tree[2].wc   = 11;
185    tree[2].utf8 = 11;
186
187    ic->private.local.thai.comp_state = 0;
188    ic->private.local.thai.keysym = 0;
189    ic->private.local.thai.input_mode = 0;
190
191    num = im->core.ic_num_resources;
192    len = sizeof(XIMResource) * num;
193    if((res = Xmalloc(len)) == (XIMResourceList)NULL) {
194	goto Set_Error;
195    }
196    (void)memcpy((char *)res, (char *)im->core.ic_resources, len);
197    ic->private.local.ic_resources     = res;
198    ic->private.local.ic_num_resources = num;
199
200    bzero((char *)&ic_values, sizeof(XimDefICValues));
201    if(_XimCheckLocalInputStyle(ic, (XPointer)&ic_values, values,
202				 im->core.styles, res, num) == False) {
203	goto Set_Error;
204    }
205
206    _XimSetICMode(res, num, ic_values.input_style);
207
208    if(_XimSetICValueData(ic, (XPointer)&ic_values,
209			ic->private.local.ic_resources,
210			ic->private.local.ic_num_resources,
211			values, XIM_CREATEIC, True)) {
212	goto Set_Error;
213    }
214    if(_XimSetICDefaults(ic, (XPointer)&ic_values,
215				XIM_SETICDEFAULTS, res, num) == False) {
216	goto Set_Error;
217    }
218    ic_values.filter_events = KeyPressMask;
219    _XimSetCurrentICValues(ic, &ic_values);
220
221    return ((XIC)ic);
222
223Set_Error :
224    if (ic->private.local.ic_resources) {
225	Xfree(ic->private.local.ic_resources);
226    }
227    Xfree(ic);
228    return((XIC)NULL);
229}
230